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

Preparation:

  1. Come up with a kernel source tree. (The latest stable kernel is 2.6.36.1, you could get it from kernel.org )
  2. 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.
else
	KERNELDIR ?= /lib/modules/$(shell uname -r)/build
	PWD := $(shell pwd)
default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

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 清空

 

 

 

 

 

 

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.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值