In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample analysis of linux driver development", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of linux driver development".
Premise, generally speaking, errors in kernel code may cause the death of a user process, or paralysis of the entire system, and more serious consequences, which may lead to disk damage. Therefore, it is recommended that it is best to have an experimental machine to test the system.
First kernel module (Hello World module)
The code is as follows:
View Code
# include
# include
MODULE_LICENSE ("Dual BSD/GPL")
Static _ init int hello_init (void)
{
/ / the printk function is defined in the kernel to be available to modules, and the kernel needs its own print count.
/ / because it runs on its own, and there is no corresponding library function.
/ / the module can call printk because after the insmod is loaded, the module is linked to the kernel
/ / because of the common symbol of the callable kernel, KERN_ALERT is the priority of the message
Printk (KERN_ALERT "HELLO WORLD\ n")
Return 0
}
Static _ exit void hello_exit (void)
{
Printk (KERN_ALERT "GoodBye\ n")
}
Module_init (hello_init)
Module_exit (hello_exit)
Two functions are defined in this module, one is called when the module is loaded into the kernel (hello_init), and the other is called when the module is removed from the kernel (hello_exit); in the above code, module_init and module_exit are two kernel macro definitions that tell the kernel where to start and where to exit, and the MODULE_ license macro is used to declare that the module is subject to a free license, otherwise a warning will appear when the kernel loads.
OK, now you can test the above program accordingly. Before testing, you must write the corresponding Makefile file. The compilation of the module is different from that of the ordinary program.
Makefile file
The code is as follows:
View Code
# makefile for hello world
# KERNELRELEASE is the first variable defined in the kernel source code
Ifneq ($(KERNELRELEASE),) # determines whether the variable is empty (not defined the first time it is executed)
# execute else statement when it is not defined
Obj-m: = HelloWorld.o# indicates that there is a module to be built from the directory file HelloWorld.o, and then set it
# named HelloWorld.ko
# if there is a module named module.ko, which comes from two source files, assuming file1.c and file2.c
# it should be like this obj-m: = module.o
# module-objs:=file1.o file2.o
Else
KDIR:=/lib/modules/$ (shell uname-r) / build
All:
# when the target of make is all,-C $(KDIR) jumps to the kernel source directory to read Makefile
# Makefile $(PWD) indicates that the current directory is returned to continue reading, and the current Makefile is executed. When executed again
# $(KERNELRELEASE) has been defined, and make will read the content before else
Make-C $(KDIR) masking $(PWD) modules
Clean:
Rm-rf * .ko * .o * .mod.o * .mod.c * .symvers
Endif
The corresponding explanation is as follows:
Kairu compiles the kernel: must be a superuser
Enter make under the current path
After the compilation is completed, enter insmd HelloWorld.ko to load the kernel, and use dmesg | tail to view the kernel output.
Remove the kernel using rmmod HelloWorld. Use dmesg accordingly. | tail can see GoodBye printed out.
The Printk may not be output to the screen, which is related to the priority of KERN_ALERT, which means that the kernel output is not high enough, and the kernel output is actually in / var/log/kern.log, which can be viewed by vim / var/log/kern.log.
The difference between kernel modules and applications:
1: after the application is run, the corresponding tasks will be processed, while the kernel module is registered to serve future requests, and after the initialization function is added with _ _ init, the memory space will be released immediately after the call.
2: when the application terminates, it is not responsible for recycling resources and is maintained by the operating system, but the kernel module must release resources when it is removed.
3: the application can call the corresponding library functions, while the kernel module can call only those functions entered in the kernel. In kernel module programming, source files should not include the usual header files, but there are exceptions, such as a small number of header files are the only exceptions.
4: errors are handled differently. Errors in the middle of the application can be checked for changes by the corresponding debugger, but in the kernel module, segment errors will terminate the current process if the entire system is not terminated.
User space and kernel space:
Applications run in user space, while kernel modules run in kernel space. Each mode has its own memory mapping and its own address space.
Relationship between the kernel and the current process:
Most of the actions done by the kernel module represent a specific process, and the kernel code can reference the current process by accessing the global item current, which defines:
# define current get_current () / / the task pointer to task_struct can be obtained through this macro definition
Kernel code can use process-specific information through current.
Kernel symbol table:
The kernel module solves the undefined symbols by looking up the kernel symbol table when loading. The kernel symbol table contains the address of the global kernel item. When a module is loaded, the symbols output in the module will also become a part of the kernel symbol table.
The input symbols of a module usually take the following two forms:
EXPORT_SYMBOL (name)
EXPORT_SYMBOL_GPL (name)
Any of the above macro definitions makes the given symbol used outside the module, and the _ GPL version of the macro definition only makes the symbol available to _ GPL-licensed modules.
Version dependency:
The module code must be recompiled for each kernel version to which it is connected. In the process of module compilation, one of the steps is to read the Makefile file to the current kernel, and in the process of compilation, it will use the vermagic.o in the kernel tree to connect your module. There is a lot of information about the kernel in this file, including the version.
Module parameters:
Module parameters are specified by insmod and modprobe at load time.
Modify the previous HelloWorld.c as follows:
Enter on the terminal
The code is as follows:
Make
Insmod HelloWorld.ko who= "test" num=10
Dmesg | tail-3
That is, you can see.
Use module_param_array (name,type,num,perm) when declaring array parameters
Name is the name of the array, type is the type of array element, num is the countless number of arrays, and perm is the permission
Attached: insmod
Insmod loads the kernel module into memory, it relies on a system call defined in kernel/module.c, the function sys_init_module allocates kernel memory to store the module, then the code segment of the copy module goes to this memory area, solves the kernel reference in the module with the help of the kernel symbol table, and calls the module's initial meridian function to start everything.
The Modprobe tool is also used to load a kernel module into memory, and unlike insmod, it looks at the module to load to see if it references symbols that are not defined by the current kernel. If so, it will look for other modules under the current search path to see if the symbol is defined, and if so, load this module into the kernel as well.
Rmmod is used to remove kernel modules. If the kernel believes that the module is still in use, or if the kernel is configured not to allow module removal, the unloading of the module will fail.
The Lsmod example gives a list of all modules loaded in the current system.
Functions are usually declared static in kernel module programming because they are not visible outside the file.
The above is all the contents of the article "sample Analysis of linux driver Development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.