makefile
1 LINUX_SRC :=/home/liuye/tiny4412/FriendlyARM_kernel/linux- 3.5 2 #obj-m += module.o #Generate module.ko but we can write module.o 3 #module-objs= param.o #here Write the file to be compiled 4 obj-m += mmc_byte.o #This is a single-file compilation: the above two lines are multi-file compilation, which is more convenient 5 6 all: 7 make -C $(LINUX_SRC) M= `pwd` modules 8 clean: 9 make -C $(LINUX_SRC) M= `pwd` modules clean 10
mmc_byte.c
1 #include <linux/init.h> 2 #include <linux/sched.h> 3 #include <linux/module.h> 4 #include <linux/mm.h> 5 #include <linux/highmem.h> 6 #include <linux/vmalloc.h> 7 #include <linux/slab.h> 8 // find 9 // kernel code works in two environments: 10 // 1/interrupt context does not allow sleep 11 // 2 /process context allows sleep 12 13 /* 14 Many page structures stored in the kernel are consecutive: page+1 is the address of the next page 15 page 16 page 17 page 18 page 19 page 20 page 21 */ 22 // In the kernel program, the requested memory must not have memory leaks. If it is used up, the kernel will crash. 23 // In the application program, if the requested memory is not used, more and more If more, the kernel will kill the process, but no one can kill the kernel due to memory leaks in the kernel. 24 static __init int test_init( void ) 25 { 26 char * v; 27 // curent: pointer: point to this The task_struct structure of the process: used to describe the process information: comm process name 28 printk( " process:%s\n " ,current-> comm); 29 // The printed process should be insmod, because the insmod process is installed this code 30 // method 1: 31 //kmalloc: The data stored in the allocated memory is random; 32 // kzalloc: The allocated memory is cleared; equivalent to memset() after kmalloc; 33 // kmalloc:kzalloc: The physical address of the allocated memory and virtual addresses are contiguous. In other words, this function requires contiguous space in physical memory for the allocation to succeed! 34 // v = kmalloc(100, GFP_KERNEL); 35 v = kzalloc( 100 , GFP_KERNEL); 36 memcpy(v, " 123 " , 3 ); 37 kfree(v); 38 39 // Method 2: 40 // It can only be guaranteed that the virtual address is continuous, and the scattered physical memory is centrally sorted and mapped to virtual memory On. Of course, physical memory may also be continuous. But we must not rely on luck. 41 // vmalloc: The function may sleep 42 //vmalloc: The data stored in the allocated memory is random; 43 // vzalloc: The allocated memory is cleared; equivalent to memset() after vmalloc; 44 // v = vmalloc(100); 45 v = vzalloc( 100 ); 46 memcpy(v, " 123 " , 3 ); 47 vfree(v); 48 49 50 51 return 0 ; 52 } 53 static __exit void test_exit( void ) 54 { 55 printk( " mmc exit! \n "); 56 } 57 58 module_init(test_init); 59 module_exit(test_exit); 60 MODULE_LICENSE("GPL");