The first LINUX driver, in general, kernel code errors may cause the death of a user process, or the paralysis of the entire system. more serious consequences may cause disk damage ~ Therefore, it is recommended that you have a testing machine for system testing. The first kernel module (HelloWorld module )... the first LINUX driver, in general, kernel code errors may cause the death of a user process, or the paralysis of the entire system. more serious consequences may cause disk damage ~ Therefore, it is recommended that you have a testing machine for system testing. The code for the first kernel module (Hello World module) www.2cto.com is as follows: # include # Include MODULE_LICENSE ("Dual BSD/GPL"); static _ init int hello_init (void) {// The printk function defines Available modules in the kernel, the kernel needs its own print Han number // because it depends on its own operation, there is no corresponding library function. Www.2cto.com // The module can call printk because after insmod is loaded, the module is linked to the kernel // for some common symbols that can call the kernel, KERN_ALERT is the priority of the message printk (KERN_ALERT "HELLO WORLDn"); return 0;} static _ exit void hello_exit (void) {printk (KERN_ALERT "GoodByen ");} module_init (hello_init); module_exit (hello_exit); two functions are defined in this module. one function is called when the module is loaded to the kernel (hello_init ), the other is called (hello_exit) when the module is removed from the kernel. in the code above, module_init and module_exit are two kernel macros, which are used to tell the kernel where to start and exit, MODULE_LICENSE macro is used to declare that the module complies with a certain free license. no A warning is reported when the kernel is loaded. Now, you can test the above program. before the test, you must compile the Makefile file. the code of the Makefile file is different from that of the normal program: # makefile for hello world # KERNELRELEASE is the first variable defined in the kernel source code ifneq ($ (KERNELRELEASE),) # judge whether the variable is null (not defined during the first execution) # execute the else statement obj-m: = HelloWorld. o # indicates that there is a module to be pulled from the directory file HelloWorld. o. create and name # HelloWorld. ko # if there is a module named module. ko is from two source files. for example, if file1.c and file2.c # are used, obj-m: = module. o # module-objs: = file1.o file2.oelseKDIR: =/lib/modules/$ (shell un Ame-r)/buildall: # when the make target is all,-C $ (KDIR) jumps to the kernel source code directory to read Makefile # M =$ (PWD) indicates that the current directory is returned to continue reading and the current Makefile is executed. when the current directory is re-executed, # $ (KERNELRELEASE) has been defined. make will read the content before else make-C $ (KDIR) M = $ (PWD) modulesclean: rm-rf *. ko *. o *. mod. o *. mod. c *. what is the description of symversendif as above? To enable or compile the kernel, you must enter "make" in the current path and enter "insmd HelloWorld. ko" to load the kernel. run "dmesg | tail" to view the output information of the kernel. Remove the kernel using rmmod HelloWorld and use dmesg. | tail can see that the printed GoodByePrintk may not be output to the screen. this is related to the priority of KERN_ALERT, which indicates that it is not high enough, the content output by the kernel is actually in/var/log/kern. log, vim/var/log/kern. view logs. Different kernel modules and applications: 1. after the application runs, it processes the corresponding tasks, and the kernel module is registered to serve future requests, after _ init is added to the initialization function, the memory space is immediately released after the call. 2: When an application is terminated, resources are not recycled and maintained by the operating system. However, the kernel module must release resources when division. 3: The application can call the corresponding library functions, and the kernel module can call only those functions entered in the kernel. In kernel module programming, the source file should not include the common header file, but there are exceptions, such A few header files are only some exceptions. 4: errors are handled in different ways. you can check and change errors in the middle section of the application by the corresponding debugging program. However, the kernel module contains a segment error. if the entire system is not terminated, the current process is terminated. User space and kernel space: applications run in user space, while kernel modules run in kernel space. Each mode has its own memory ING and its own address space. The relationship between the kernel and the current process: most of the actions performed by the kernel module represent a specific process. The kernel code can reference the current process. by accessing the global item current, it defines: # define current get_current () // This macro definition can be used to obtain the task pointer to task_struct. the kernel code can use process-specific information through current. Kernel symbol table: When the kernel module is loaded, it finds the kernel symbol table to solve undefined symbols. the kernel symbol table contains the addresses of global kernel items. when a module is loaded, the symbols output in the module will also be part of the kernel symbol table. The input symbol of the module is generally in the following two forms: any macro in the EXPORT_SYMBOL (name) EXPORT_SYMBOL_GPL (name) to enable the given symbol to be used outside the module, the macro definition of the _ GPL version can only make the symbol available to the _ GPL-licensed module. Version dependency: the module code must be re-compiled for each kernel version to be connected. during the module compilation process, one step is to read the Makefile file from the current kernel pair, during the compilation process, vermagic uses the files in the kernel tree. o) connect to your module. there is a lot of information about the kernel in this file, including the version... module parameters: the module parameters are specified when insmod and modprobe are loaded. Modify HelloWorld. c as follows: Enter the following code on the terminal: MakeInsmod HelloWorld. ko who = "test" num = 10Dmesg | tail-3. When declaring an array parameter, module_param_array (name, type, num, perm) Name is used to indicate the name of the array, type is the type of the array element, num is the countless number of arrays, and perm is the permission attachment: insmodInsmod loads the kernel module into the memory. it depends on a kernel/module. the system call defined in c. the Function sys_init_module allocates the kernel memory to store the module. it then copies the code segment of the module to this memory area and uses the kernel symbol table to solve the kernel reference in the module, and call the initial function of the module to start everything. The Modprobe tool is also used to load a kernel module to the memory. Unlike insmod, it will check the module to be loaded to see if any symbols not defined in the current kernel are referenced. If necessary, it will search for other modules in the current search path to see if this symbol is defined. if so, it will also load this module into the kernel. Rmmod is used to remove the kernel module. if the kernel considers that the module is still in use or the kernel configuration does not allow the removal of the module, the uninstallation of the module will fail. Lsmod lists all modules loaded in the current system. In kernel module programming, functions are usually declared static because they are not visible outside the file.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service