Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Linux kernel module

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use the Linux kernel module". In daily operation, I believe many people have doubts about how to use the Linux kernel module. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the Linux kernel module"! Next, please follow the editor to study!

1. Module definition

The Linux kernel uses macros of xx_initcall_xx (fn) to define kernel modules.

Macro definition file: include/linux/init.h, defined as follows:

/ * Early initcalls run before initializing SMP. * Only for built-in code, not modules. * / # define early_initcall (fn) _ define_initcall (fn, early) / * A "pure" initcall has no dependencies on anything else, and purely * initializes variables that couldn't be statically initialized. * This only exists for built-in code, not for modules. * Keep main.c:initcall_level_names [] in sync. * / # define pure_initcall (fn) _ define_initcall (fn, 0) # define core_initcall (fn) _ define_initcall (fn, 1) # define core_initcall_sync (fn) _ define_initcall (fn, 1s) # define postcore_initcall (fn) _ define_initcall (fn, 2) # define postcore_initcall_sync (fn) _ define_initcall (fn, 2s) # define arch_initcall (fn) _ define_initcall (fn 3) # define arch_initcall_sync (fn) _ define_initcall (fn, 3s) # define subsys_initcall (fn) _ define_initcall (fn, 4) # define subsys_initcall_sync (fn) _ define_initcall (fn, 4s) # define fs_initcall (fn) _ define_initcall (fn, 5) # define fs_initcall_sync (fn) _ define_initcall (fn 5s) # define rootfs_initcall (fn) _ define_initcall (fn, rootfs) # define device_initcall (fn) _ define_initcall (fn, 6) # define device_initcall_sync (fn) _ define_initcall (fn, 6s) # define late_initcall (fn) _ define_initcall (fn, 7) # define late_initcall_sync (fn) _ define_initcall (fn, 7s)

Among them: early_initcall (fn) only aims at the core code of the kernel and cannot describe the module.

As you can see from the above code, the implementation of each macro is _ _ define_initcall (), which is defined as follows:

# define_ _ define_initcall (fn, id)\ static initcall_t _ _ initcall_##fn##id _ _ used\ _ _ attribute__ ((_ _ section__ (".initcall" # id ".init")) = fn;\ LTO_REFERENCE_INITCALL (_ _ initcall_##fn##id) typedef int (* initcall_t) (void)

For the above definition, you need to pay attention to the following:

1. Initcall_t: is a function pointer type that defines _ _ initcall_##fn##id and points to fn.

2. _ _ used: defined as # define _ _ used__ attribute__ ((_ _ used__)) in the file include/linux/compiler-gcc.h, telling the compiler to keep a static function in the target file, even if the function is not used.

3. _ _ attribute__ ((_ _ section__ (".initcall" # id ".init")): the function pointer defined is in the .initcall * .init section.

Second, link location

In the file include/asm-generic/vmlinux.lds.h, the macro INIT _ CALLS is defined as follows:

# define INIT_CALLS_LEVEL (level)\ VMLINUX_SYMBOL (_ _ initcall##level##_start) =. \ * (.initcall # # level##.init)\ * (.initcall # # level##s.init)\ # define INIT_CALLS\ VMLINUX_SYMBOL (_ _ initcall_start) =. \ * (.initcallearly.init)\ INIT_CALLS_LEVEL (0)\ INIT_CALLS_LEVEL (1)\ INIT_CALLS_LEVEL (2)\ INIT_CALLS_LEVEL (3)\ INIT_CALLS_LEVEL (4)\ INIT_CALLS_LEVEL (5)\ INIT_CALLS_LEVEL (rootfs)\ INIT_CALLS_LEVEL (6)\ INIT_CALLS_LEVEL (7)\ VMLINUX_SYMBOL (_ _ initcall_end) =.

In the file arch/arm64/kernel/vmlinux.lds.S, set the distribution location of the macro INIT _ CALLS in the .init. Data section, as follows:

.init.data: {INIT_DATA INIT_SETUP (16) INIT_CALLS CON_INITCALL SECURITY_INITCALL INIT_RAM_FS}

After the Linux kernel compiles the link, the file arch/arm64/kernel/vmlinux.lds is generated. In this file, the macro INIT _ CALLS will be expanded and assigned to the .init.data section, as follows:

.init.data: {... _ initcall_start =.; * (.initcallearly.init) _ initcall0_start =.; * (.initcall0.init) * (.initcall0s.init) _ _ initcall1_start =.; * (.initcall1.init) * (.initcall1s.init) _ _ initcall2_start =.; * (.initcall2.init) * (.initcall2s.init) _ _ initcall3_start =. * (.initcall3.init) * (.initcall3s.init) _ initcall4_start =.; * (.initcall4.init) * (.initcall4s.init) _ _ initcall5_start =.; * (.initcall5.init) * (.initcall5s.init) _ _ initcallrootfs_start =.; * (.initcallrootfs.init) * (.initcallrootfs.init) _ initcall6_start =.; * (.initcall6.init) * (.initcall6s.init) _ initcall7_start =. * (.initcall7.init) * (.initcall7s.init) _ _ initcall_end =.;..}

Note: the order between _ _ initcall_start and _ _ initcall_end is from 0 to 7, corresponding to pure_initcall (fn) to late_initcall_sync (fn).

III. Module loading

The Linux kernel module can be compiled directly into the kernel image and loaded when the kernel starts, or it can be loaded into the kernel through the insmod command after the system starts.

When the Linux kernel starts, the kernel_init thread loads the statically compiled kernel module.

1. Program call process

# # kernel/init/main.cstart_kernel ()-> rest_init ()-> kernel_thread (kernel_init, NULL, CLONE_FS) # # 1. Create kernel thread kernel_init ()-> kernel_init_freeable ()-> do_basic_setup ()-> do_initcalls ()-> # # 2. Module loading process do_initcall_level ()-> do_one_initcall ()

2. Do_initcalls () function

Function function: scan each level in the .init.data section sequentially, that is, from _ _ initcall0_start to _ _ initcall_end.

# # _ _ initdata is defined in static initcall_t * initcall_levels [] _ _ initdata = {_ _ initcall0_start, _ _ initcall1_start, _ _ initcall2_start, _ _ initcall3_start, _ _ initcall4_start, _ _ initcall5_start, _ _ initcall6_start, _ _ initcall7_start, _ _ initcall_end,}; static void _ init do_initcalls (void) {int level; for (level = 0) Level < ARRAY_SIZE (initcall_levels)-1; level++) do_initcall_level (level);}

3. Do_initcall_level () function

Function function: scan .initcall * .init to .initcall * s.init sequentially at the same level

Static void _ _ init do_initcall_level (int level) {... For (fn = initcall_ levels; fn < initcall_ levels [level + 1]; fn++) do_one_initcall (* fn);}

4. Do_one_initcall () function

Function function: call each defined module function.

Int _ _ init_or_module do_one_initcall (initcall_t fn) {... If (initcall_debug) ret = do_one_initcall_debug (fn); else ret = fn (); # # call a defined initcall function. Return ret;}

Note: each fn () corresponds to the fn in the first part of the module definition, for example: device_initcall (fn).

IV. Module priority

From the execution flow of the above function, we can see that the kernel module loading priority is as follows:

Early_initcall (fn) # # highest priority Lower pure_initcall (fn) core_initcall (fn) core_initcall_sync (fn) postcore_initcall (fn) postcore_initcall_sync (fn) arch_initcall (fn) arch_initcall_sync (fn) subsys_initcall (fn) subsys_initcall_sync (fn) fs_initcall_sync (fn) rootfs_initcall (fn) device_initcall (fn) device_initcall_sync (fn) late_initcall (fn) late_initcall_sync (fn) # # the lowest priority

The priority of Linux kernel modules determines the loading order of modules. In driver development, we need to pay attention to the definition of modules with startup sequence requirements.

At this point, the study on "how to use the Linux kernel module" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report