Linux 單核心架構透過核心模組(LKM)實現功能動態擴展,無需重新編譯核心。本文解析模組機制、編程基礎、Hello World 範例、Makefile 撰寫,並提供字符設備驅動程式標準框架。
insmod/rmmod、modprobe 自動處理相依性。copy_to_user、copy_from_user、put_user、get_user 等專用函數。CONFIG_MODVERSIONS),否則載入失敗或系統崩潰。int init_module(void):載入時執行,註冊功能(字符/區塊設備、檔案系統、系統呼叫等)。void cleanup_module(void):卸載時執行,釋放資源、註銷註冊。#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Author");
MODULE_DESCRIPTION("Simple Hello World Module");
static int __init hello_init(void) {
printk(KERN_INFO "Hello World! Module loaded.\n");
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_INFO "Goodbye, kernel!\n");
}
module_init(hello_init);
module_exit(hello_exit);
obj-m += hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
執行 make 產生 hello.ko(核心 2.6+ 使用 .ko 副檔名)。
insmod hello.ko dmesg | tail lsmod | grep hello rmmod hello dmesg | tail
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#define DEVICE_NAME "mychardev"
static int major;
static struct cdev my_cdev;
static char *kernel_buffer;
static int buffer_size = 1024;
static int dev_open(struct inode *inode, struct file *filp) { return 0; }
static int dev_release(struct inode *inode, struct file *filp) { return 0; }
static ssize_t dev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) {
if (*f_pos >= buffer_size) return 0;
if (count > buffer_size - *f_pos) count = buffer_size - *f_pos;
if (copy_to_user(buf, kernel_buffer + *f_pos, count)) return -EFAULT;
*f_pos += count;
return count;
}
static ssize_t dev_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) {
if (*f_pos >= buffer_size) return -ENOSPC;
if (count > buffer_size - *f_pos) count = buffer_size - *f_pos;
if (copy_from_user(kernel_buffer + *f_pos, buf, count)) return -EFAULT;
*f_pos += count;
return count;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = dev_open,
.release = dev_release,
.read = dev_read,
.write = dev_write,
};
static int __init chardev_init(void) {
dev_t dev = MKDEV(0, 0);
int ret = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
if (ret < 0) return ret;
major = MAJOR(dev);
cdev_init(&my_cdev, &fops);
my_cdev.owner = THIS_MODULE;
ret = cdev_add(&my_cdev, dev, 1);
if (ret) { unregister_chrdev_region(dev, 1); return ret; }
kernel_buffer = kmalloc(buffer_size, GFP_KERNEL);
if (!kernel_buffer) { cdev_del(&my_cdev); unregister_chrdev_region(dev, 1); return -ENOMEM; }
printk(KERN_INFO "%s: major %d assigned\n", DEVICE_NAME, major);
return 0;
}
static void __exit chardev_exit(void) {
dev_t dev = MKDEV(major, 0);
kfree(kernel_buffer);
cdev_del(&my_cdev);
unregister_chrdev_region(dev, 1);
printk(KERN_INFO "%s: unloaded\n", DEVICE_NAME);
}
module_init(chardev_init);
module_exit(chardev_exit);
MODULE_LICENSE("GPL");
| 項目 | 舊版 (2.0/2.2/2.4) | 現代 (2.6+) |
|---|---|---|
| 模組副檔名 | .o | .ko |
| 註冊字符設備 | register_chrdev() | alloc_chrdev_region() + cdev_add() |
| 使用計數 | MOD_INC/DEC_USE_COUNT | try_module_get() / module_put() |
| 初始化/清理標記 | 無 | __init / __exit |
| 模組資訊 | 無標準 | MODULE_LICENSE 等巨集 |