首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

[Linux Device Drivers 3rd Notes] 二. Modules and kernel programming concepts

2013-11-22 
[Linux Device Drivers 3rd Notes] 2. Modules and kernel programming conceptsPreparation: Come up wit

[Linux Device Drivers 3rd Notes] 2. Modules and kernel programming concepts

Preparation:

    Come up with a kernel source tree. (The latest stable kernel is 2.6.36.1, you could get it from kernel.org)Build kernel and install it on your system. 具体可以参考在ubuntu10.10上编译安装linux-2.6.36.1 kernel

Hello module coding:

hello.c

#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");static int hello_init(void){printk(KERN_ALERT "Hello, allenpettle\n");return 0;}static void hello_exit(void){printk(KERN_ALERT "Goodbye, allenpettle\n");}module_init(hello_init);module_exit(hello_exit);

?linux/module.h and linux/init.h must appear in every loadable modules.

module.h contains many definitions of symbols and functions.

init.h specify initialization and cleanup functions.

module_init and module_exit are mandatory.

?

MAKEFILE 注意前面不是空格而是tab,不然编译不过

# If KERNELRELEASE is defined, we've been invoked from the# kernel build system and can use its language.ifneq ($(KERNELRELEASE),)obj-m := hello.o# Otherwise we were called directly from the command# line; invoke the kernel build system.elseKERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesendif

obj-m
A makefile symbol used by the kernel build system to determine which modules
should be built in the current directory.

?

make / insmod / rmmod操作, dmesg看log,如果log太多,dmesg -c 清空

?

?

?

[Linux Device Drivers 3rd Notes] 二. Modules and kernel programming concepts

?

[Linux Device Drivers 3rd Notes] 二. Modules and kernel programming concepts

?

?

Kernel Space VS. User Space

System memory in Linux can be divided into two distinct regions: kernel space and user space.

Kernel space is where the kernel(i.e., the core of the operating system) executes and provides its services.

User space is that set of memory locations in which user processes run. A process is an excuting instance of a program.

Kernel space can be accessed by user processes only through the use of system calls.

?

?

热点排行