수색…


빈 커널 모듈

#include <linux/init.h>
#include <linux/module.h>

/**
 * This function is called when the module is first loaded.
 */    
static int __init hello_kernel_init(void)
{
    printk("Hello, World!\n");
    return 0;
}

/**
 * This function is called when is called if and when the module is unloaded.
 */
static void __exit hello_kernel_exit(void)
{
    printk("Goodbye, cruel world...\n");
}

/* The names of the init/exit functions are arbitrary, and they are bound using the following macro definitions */
module_init(hello_kernel_init);
module_exit(hello_kernel_exit);

Linux 장치 드라이버 (Character-device, Block-device 등)를 작성하려면 진입 점과 종료점이있는 커널 모듈을 작성해야합니다.

자체적으로 커널 모듈은 아무 것도하지 않습니다. 사용자 공간과 통신하는 의미있는 방법이 없습니다. 진입 점을 사용하여 예를 들어 새로운 문자 - 장치를 생성 할 수 있으며,이 문자 장치는 사용자 공간과 통신하는 데 사용됩니다.

모듈 빌드 및 실행

드라이버를 컴파일하려면 Linux 커널 소스 트리가 있어야합니다.

소스가 /lib/modules/<kernel-version> 있다고 가정하면 다음 Makefile은 driver.c 파일을 driver.ko 커널 오브젝트로 컴파일합니다

obj-m := driver.o
KDIR := /lib/modules/$(shell uname -r)/build/
PWD := $(shell pwd)

all:
    $(MAKE) -C $(KDIR) M=$(PWD) modules

이 Makefile을 호출하는 방법을 알 수 make 커널의 빌드 디렉토리에.

컴파일 단계가 성공적으로 완료되면 드라이버의 src 디렉토리는 다음과 같이 보일 것입니다 :

driver.c  driver.ko  driver.mod.c  driver.mod.o  driver.o  Makefile  modules.order  Module.symvers

모듈을 "실행"하려면, 실행중인 커널에 삽입해야합니다 :

$ insmod driver.ko
$ dmesg | tail -n 1
[133790.762185] Hello, World!

$ rmmod driver.ko
$ dmesg | tail -n 1
[133790.762185] Goodbye, cruel world...


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow