`

[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.

 

 

  • 大小: 103.1 KB
  • 大小: 87.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics