5 DERECHA

linux 4 본문

Linux

linux 4

kay30 2023. 4. 28. 17:49

0. Systemcall 사용

 1) SYSCALL_DEFINE0 or SYSCALL_DEFINE1 할때 괄호({}) 사용하기

1. 커널 모듈 만들기

 1) LKM(Loadable Kernel Module)

usb를 꽃을 것도 아닌데 커널에 올리지 않기 위해서, -> 부팅시간이랑 메모리 절약 가능 근데 문제는 버전이 정확히 맞아야한다.

2) 새로운 커널 설정(KConfig) 추가하기

make menuConfig 할때 보임

2-1) 커널 설정 문법

1) 원하는 폴더에서 Kconfig 만듬

2) linux/drivers에서 Kconfig에서 마지막에 더한 폴더 붙이기 (마지막일 필요는 없으나 그 순서대로 빌드함)

3) ARCH=arm64 make menuconfig로 설정

4) 새로 추가한 driver? 모듈?이 들어간걸 알수있음

5) drivers/ 쪽 Makefile 수정 -1

6) 만든 폴더에서 Makefile 수정-2

 

발생한 문제

2. 커널의 기본 API 익히기

1) 빌드한 모듈을 rootfs에 옮겨놓고

2) QEMU에서 실행하

3. 디바이스 드라이버 만들기

 1) 링크드리스트

2) 해쉬

3) 뮤텍스

4) 스핀락 => read_lock, write_lock 따로 놓고 사

커널은 주소공간, 자원을 공유하기 때문에 스핀락이 필요.

커널 쓰레드에서 프로세스를 실행함. 즉, 모든 프로세스는 쓰레드에서 동작

5) current task

 : 현재 프로세스의 정보를 담고있는 구조체를 커널의 어디서든 사용이 가능

* 디바이스 드라이버 종류

1) 문자 디바이스 드라이버  ** 대부분 이걸로 구현됨 **

2) 블록 디바이스 드라이버

3) 네트워크 디바이스 드라이버

4) 버스 디바이스 드라이버

* 디바이스 노드

mkmod 주번호, 부번호  (근데 요즘은 udev사용)

부번호는 디바이스 내에세 노드 파일의 부번호를 읽어서 사용

* 파일 읽기/쓰기 구현

- ppos는 현재의 위치를 나타내는 커널이 넘겨주는 인자니까 동작 이후에 읽은/쓴 만큼 갱신해줘야함

- 버퍼를 해당 디바이스 드라이버 쓰는 사람이 공유하니까 스핀락 필요!

* 파일 특수 제어(ioctl) 구현

 

4. udev와 연동하기

: 디바이스 노드 추가를 해야하는데 요즘은 이거를 자동으로 만들어주는 udev라는게 생김 

 

전역변수로 static 함수 vs 클래스 안에서 static 함수 

파일 안에서만 사용하겠다  vs class에서만 사용한다.

static = build 타임에 전역변수로 저장해버리겠다.

 

질문1) 맞게 추가해준건가?

질문2) commento.ko 이런거 없는뎅?

질문3) rootfs.ext4가 있는데 왜 없다고 하면서 복사가 안 될깡?

 

** Kernel Module **

A kernel module is a piece of code that can be dynamically loaded into or unloaded from the kernel of an operating system. It extends the functionality of the kernel by adding new features, drivers, or support for specific hardware or software components.

Here are a few key points about kernel modules:

1. Dynamic loading and unloading: Unlike the monolithic kernel, which has all its functionalities built into it during compilation, a kernel module can be loaded and unloaded on demand without restarting the entire system. This dynamic nature allows for easier customization and expansion of the kernel's capabilities.

2. Modularity: Kernel modules promote modularity by allowing developers to separate functionality into smaller, self-contained units. Each module focuses on a specific task or device, making it easier to manage, maintain, and update the kernel.

3. Kernel-Space execution: Kernel modules execute in the privileged kernel space, which gives them direct access to hardware resources and the ability to interact with core kernel functions. This allows modules to perform low-level operations and implement device drivers, file systems, network protocols, and other kernel-level tasks.

4.Interaction with the kernel: Kernel modules can interact with the kernel by registering callbacks, hooks, or new system calls. They can modify or extend existing kernel behavior, handle interrupts, manage hardware devices, or provide additional functionality to user-space applications.

5. Security and stability: Kernel modules require proper permissions to be loaded into the kernel. This helps ensure the security and stability of the system by preventing unauthorized or poorly designed modules from compromising the kernel's integrity.

6. Platform-specific: Kernel modules are typically specific to a particular operating system and kernel version. They must be developed and compiled specifically for the target kernel, taking into account the kernel's APIs, data structures, and internal mechanisms.

7. Lifecycle management : Kernel modules have a lifecycle that involves loading, initializing, using, and unloading. The kernel provides mechanisms to manage this lifecycle, allowing modules to be safely loaded and unloaded without causing system instability or resource leaks.

Overall, kernel modules provide a flexible and efficient way to extend the functionality of the operating system kernel without the need for recompilation or system restarts. They play a vital role in supporting new hardware, adding features, improving performance, and enabling customization in the kernel environment.

 

https://bit.ly/3D9XCOz

#코멘토 #코멘토실무PT #실무PT후기

'Linux' 카테고리의 다른 글

Linux 6 week  (0) 2023.05.06
linux 5  (0) 2023.05.05
linux 3  (0) 2023.04.28
linux 2  (0) 2023.04.10
Linux2(Shell script)  (1) 2023.04.05