- Books: Linux Device Drivers
- Ch1. Introduction
- 說明驅動程式的角色
- 設計原則: 提供機制 (mechanism)-要提供什麼能力, 而非法則 (policy)-如何使用這些能力
- Kernel 概論
- Module
- Device分類: character device, block device, network interface
- Ch2. Module
- Kernel preparation
- Download Linux Kernel source codes
apt-get source linux-image-unsigned-$(uname -r)
Install build tools
sudo apt-get install libncurses-dev flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf
Modify config file (backup first)
cp /boot/config-4.15.0-91-generic .
make menuconfig
Build (add -fno-pie @Makefile KBUILD_FLAGS if build error)
make -j 16 clean
make -j 16
Reference:
展示 Hello World Module
- build -
- include kernel headers (/usr/src/linux-headers)
- make file - obj-m
- sample module file
#include
#include
MODULE_DESCRIPTION("Hello_world");
MODULE_LICENSE("GPL");
static int hello_init(void)
{
printk(KERN_INFO "Hello world !\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_INFO "Bye !\n");
}
module_init(hello_init);
module_exit(hello_exit);
sample Makefile
PWD := $(shell pwd)
KVERSION := $(shell uname -r)
KERNEL_DIR = /usr/src/linux-headers-$(KVERSION)/
MODULE_NAME = hello
obj-m := $(MODULE_NAME).o
all:
make -C $(KERNEL_DIR) M=$(PWD) modules
clean:
make -C $(KERNEL_DIR) M=$(PWD) clean
測試
insmod hello.ko
dmseg
程式架構
- header: 重要 headers: linux/init.h, linux/module.h 含有後面要用到的 marcos
- license
- module_init()
- module_exit
Kernel module v.s. user space application
insmod/modprobe/rmmod/lsmod
module lifecycle - facility
module parameter
- module_param(parameterName, type, permission)
沒有留言:
張貼留言