Kernel module compilation method, device, computer equipment and storage medium
By adapting to different versions of Linux kernels, the kernel module source code is adapted and recompiled, which solves the problem of kernel module compilation and loading on different versions of kernels, and realizes cross-version compatibility of kernel modules.
Patent Information
- Application Number
- CN202111283120.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-01
- Publication Date
- 2025-02-21
- Estimated Expiration
- 2041-11-01
AI Technical Summary
In a Linux-compatible Android environment, the kernel module needs to be compiled and run on different versions of the Linux kernel, resulting in compilation and loading problems.
By adapting to different versions of the kernel, the kernel module source code to be compiled and loaded is adapted, including adapting to kernel header files, APIs and symbol tables. Determine whether the compiled kernel module matches the kernel. If it does not match, decrypt and recompile the kernel module.
It realizes normal compilation and operation of kernel modules between different versions of kernels, solving the problem of kernel module compilation and loading in Linux-compatible Android environments.
Smart Images

Figure CN114003235B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer operating systems, and in particular to a kernel module compilation method, device, computer equipment and storage medium. Background Art
[0002] In the scenario where the Linux operating system is compatible with the Android environment, there is a need to compile kernel modules on different kernel versions. In the Linux compatible Android environment, the Android system needs to load some kernel modules in advance when it is running, such as ashmem and binder. The kernel modules of different versions of the Android system depend on different versions of the Linux kernel. For example, Android 7.0 depends on Linux kernel version 4.4.1, and Android 8.0 depends on Linux kernel version 4.10. The version of the Android system is fixed, so the kernel modules required by the Android system also depend on a fixed version of the Linux kernel. Since the Android system runs on the Linux system, but the Linux kernels of various Linux systems are not fixed, this results in the kernel modules required by the Android system needing to be compiled and run on different versions of the Linux kernel. Therefore, it is necessary to solve the problem of compiling and loading kernel modules on different kernel versions. Summary of the invention
[0003] In order to solve the problem of compiling and loading kernel modules on different kernel versions, the present invention provides a kernel module compilation method, device, computer equipment and storage medium.
[0004] In a first aspect, the present invention provides a kernel module compilation method, the method comprising:
[0005] Adapt the kernel module source code to be compiled and loaded according to different versions of the kernel;
[0006] Determine whether there is a compiled kernel module on the specified path; if so, compare the compiled kernel module with the kernel to see if they match: if they match, load the compiled kernel module; if not, decrypt the encrypted kernel module source code and copy it to the specified hidden directory; if not, decrypt the encrypted kernel module source code and copy it to the specified hidden directory; compile the decrypted kernel module source code on the kernel; load the compiled kernel module.
[0007] Furthermore, the method adapts the kernel module source code to be compiled and loaded according to different versions of the kernel, including:
[0008] Adapt the kernel header files to be referenced according to the Linux kernel version number;
[0009] Adapt the kernel API to be called according to the Linux kernel version number;
[0010] Adapt the kernel symbol table according to the Linux kernel version number.
[0011] Furthermore, the method further comprises:
[0012] After compiling the decrypted kernel module source code on the kernel,
[0013] Copy the compiled kernel module to the specified path and delete the decrypted kernel module source code.
[0014] Furthermore, in the method, the kernel header file to be referenced is adapted according to the Linux kernel version number, specifically:
[0015] In the kernel module source code to be compiled and loaded, different versions of the Linux kernel are adapted by judging the macro definition of LINUX_VERSION_CODE, and kernel header files of different paths are referenced according to the Linux kernel version number.
[0016] Furthermore, in the method, the kernel API to be called is adapted according to the Linux kernel version number, specifically:
[0017] In the kernel module source code to be compiled and loaded, set the function parameters passed to the API according to the Linux kernel version number: set the type and number of function parameters.
[0018] Furthermore, in the method, the kernel symbol table is adapted according to the Linux kernel version number, specifically:
[0019] Export the kernel API to be used and obtain the address of the kernel API to be used so that it can be called by the kernel module source code to be compiled and loaded.
[0020] Furthermore, the method compares whether the compiled kernel module matches the kernel, specifically:
[0021] Use the modinfo mymodule.ko command to obtain the vermagic information of the kernel module, and then use the uname –a command to obtain the kernel version number. Compare the kernel version information with the vermagic information in the kernel module to confirm whether the compiled kernel module matches the kernel.
[0022] In a second aspect, the present invention provides a kernel module compiling device, the device comprising:
[0023] Memory, used to store programs;
[0024] The processor is used to execute the program stored in the memory. When the program stored in the memory is executed, the processor is used to execute the steps of the above method.
[0025] In a third aspect, the present invention provides a computer device, comprising a memory and a processor, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the processor executes the steps of the above method.
[0026] In a fourth aspect, the present invention provides a storage medium storing computer-readable instructions, which, when executed by one or more processors, causes the one or more processors to perform the steps of the above method.
[0027] Compared with the prior art, the present invention has the following advantages:
[0028] In a Linux-compatible Android environment, the problem of compiling kernel modules in different kernel versions can be completely solved, and the kernel modules can be compiled and run normally between kernels of different versions. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 A schematic diagram of a kernel module compiling method provided by an embodiment of the present invention;
[0030] Figure 2 A schematic diagram of the composition of a kernel module compiling device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0031] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0032] For environments that are compatible with running Android on the Linux operating system, the Android runtime environment runs in a container.
[0033] Through container technology, a new Linux system (including other systems customized based on Linux system, such as Android) can be run on the Linux system. The container isolates the client system and the host system from each other. Different from the traditional solution based on virtual machine technology, in container technology, the client system and the host system share CPU, memory, storage and other resources, and the resource overhead is very low. In addition, through proper configuration, the client system can also directly access the hardware resources of the host, further improving the performance of the client system.
[0034] Figure 1A schematic diagram of a kernel module compilation method provided by an embodiment of the present invention, the method is:
[0035] Adapt the kernel module source code to be compiled and loaded according to different versions of the kernel;
[0036] Determine whether there is a compiled kernel module on the specified path; if so, compare the compiled kernel module with the kernel to see if they match: if they match, load the compiled kernel module; if not, decrypt the encrypted kernel module source code and copy it to the specified hidden directory; if not, decrypt the encrypted kernel module source code and copy it to the specified hidden directory; compile the decrypted kernel module source code on the kernel; load the compiled kernel module.
[0037] Furthermore, the method adapts the kernel module source code to be compiled and loaded according to different versions of the kernel, including:
[0038] Adapt the kernel header files to be referenced according to the Linux kernel version number;
[0039] Adapt the kernel API to be called according to the Linux kernel version number;
[0040] Adapt the kernel symbol table according to the Linux kernel version number.
[0041] Furthermore, the method further comprises:
[0042] After compiling the decrypted kernel module source code on the kernel,
[0043] Copy the compiled kernel module to the specified path and delete the decrypted kernel module source code.
[0044] Compile the decrypted kernel module source code on the kernel, and copy the compiled kernel module to the specified path. When the method needs to be executed next time to query whether there is a compiled kernel module in the specified path, the compiled kernel module can be queried.
[0045] Since the kernel module source code is stored in the specified path of the Linux platform, it is necessary to encrypt the kernel module source code from the perspective of source code security. When the kernel source code needs to be compiled, the encrypted kernel module source code is decrypted from the specified path and copied to the specified hidden directory. Copying to the specified hidden directory is also considered from the perspective of source code security. After the kernel module source code is copied to the hidden directory of the Linux system, the user cannot view this hidden directory using the file manager. After the kernel module is compiled, the kernel module ko file will be generated, and then the decrypted kernel module source code will be deleted to prevent users from viewing the unencrypted kernel module source code and ensure source code security.
[0046] Furthermore, in the method, the kernel header file to be referenced is adapted according to the Linux kernel version number, specifically:
[0047] In the kernel module source code to be compiled and loaded, different versions of the Linux kernel are adapted by judging the macro definition of LINUX_VERSION_CODE, and kernel header files of different paths are referenced according to the Linux kernel version number.
[0048] Since the kernel module source code needs to be compiled and run on different versions of the kernel, and there are some differences between different versions of the Linux kernel, the kernel module source code needs to be adapted to different versions of the kernel. In order to adapt to different versions of the kernel, the following methods are mainly used:
[0049] Adapt the kernel header files that need to be referenced according to the Linux kernel version number.
[0050] The Linux kernel version can be checked on the Linux platform using the uname -r command. In the Linux kernel module source code, by including the linux / version.h header file, use the LINUX_VERSION_CODE in the linux / version.h header file to obtain the Linux kernel version information. Due to different Linux kernel versions, the path of the header file may change, so the kernel header file that needs to be referenced needs to be adapted according to the Linux kernel version.
[0051] In this method, in the kernel module source code, the following code is a sample code for adapting the kernel header file that needs to be referenced according to the Linux kernel version number:
[0052] #include<linux / version.h>
[0053] #if LINUX_VERSION_CODE>=KERNEL_VERSION(4,11,0)
[0054] #include<linux / sched / signal.h>
[0055] #include<linux / sched / mm.h>
[0056] #else
[0057] #include<linux / signal.h>
[0058] #include<linux / mm.h>
[0059] #endif
[0060] The above code indicates that the linux / version.h header file is included. The macro definition of LINUX_VERSION_CODE is used to adapt to different versions of the Linux kernel.
[0061] #if, #else, and #endif are a conditional compilation syntax in the C language, which means compiling different codes according to different conditions.
[0062] Since the Linux kernel version has changed since 4.11.0, the paths of the signal.h and mm.h header files have changed. Therefore, for kernels with Linux kernel versions greater than or equal to 4.11.0 and less than 4.11.0, you need to reference header files with different paths. Otherwise, the compilation will fail and prompt that the header files cannot be found. Specifically, if the Linux kernel version is greater than or equal to 4.11.0, conditional compilation is triggered, and only the following two lines are compiled:
[0063] #include<linux / sched / signal.h>
[0064] #include<linux / sched / mm.h>
[0065] That is, reference the linux / sched / signal.h and linux / sched / mm.h header files. If the Linux kernel version is less than 4.11.0, conditional compilation is triggered and only the following two lines are compiled:
[0066] #include<linux / signal.h>
[0067] #include<linux / mm.h>
[0068] That is, reference the linux / signal.h and linux / mm.h header files.
[0069] Furthermore, in the method, the kernel API to be called is adapted according to the Linux kernel version number, specifically:
[0070] In the kernel module source code to be compiled and loaded, set the function parameters passed to the API according to the Linux kernel version number: set the type and number of function parameters.
[0071] Due to different Linux kernel versions, the kernel API may change. It is possible that a new API is added to a certain version of the Linux kernel, or the parameter type or number of parameters of the kernel API is modified. In order to ensure that the kernel module can be compiled and run on different versions of the Linux kernel, it is necessary to adapt the called kernel API according to the Linux kernel version number, so as to ensure that the corresponding kernel API is correctly called for the corresponding version of the Linux kernel.
[0072] In this method, in the kernel module source code, the following code is a sample code for adapting the called kernel API according to the Linux kernel version number:
[0073] #include<linux / version.h>
[0074] #if LINUX_VERSION_CODE>=KERNEL_VERSION(4,11,0)
[0075] zap_page_range(vma,page_addr,PAGE_SIZE);
[0076] #else
[0077] zap_page_range(vma,page_addr,PAGE_SIZE,NULL);
[0078] #endif
[0079] The above code indicates that the linux / version.h header file is included. Different versions of the Linux kernel can be adapted by judging the macro definition of LINUX_VERSION_CODE.
[0080] Since the Linux kernel version started from 4.11.0, the function parameters of the zap_page_range interface have been reduced from four to three. If the parameters for calling the zap_page_range interface are not adapted on different Linux kernel versions, a compilation error will occur, and it will prompt that the function parameters are too many or too few. Therefore, if the Linux kernel version is greater than or equal to 4.11.0, conditional compilation is triggered, and only the following source code is compiled: zap_page_range(vma,page_addr,PAGE_SIZE); that is, calling zap_page_range(vma,page_addr,PAGE_SIZE); passing three functions to the API interface. If the Linux kernel version is less than 4.11.0, conditional compilation is triggered, and only the following source code is compiled: zap_page_range(vma,page_addr,PAGE_SIZE,NULL); that is, calling zap_page_range(vma,page_addr,PAGE_SIZE,NULL); passing four parameters to the API interface.
[0081] Furthermore, in the method, the kernel symbol table is adapted according to the Linux kernel version number, specifically:
[0082] Export the kernel API to be used and obtain the address of the kernel API to be used so that it can be called by the kernel module source code to be compiled and loaded.
[0083] The Linux kernel symbol table refers to the symbol table of functions and variables that can be referenced externally in the kernel's internal functions or variables. In fact, it is an index file, and its purpose is to let external software know the actual allocation location inside the kernel file. Starting from version 2.6 of the Linux kernel, the kernel introduced a mechanism for exporting symbols. Only symbols exported in the kernel using EXPORT_SYMBOL or EXPORT_SYMBOL_GPL can be used directly in kernel modules. Functions or symbols defined in the EXPORT_SYMBOL or EXPORT_SYMBOL_GPL tags are open to all kernel codes and can be called directly in kernel modules without modifying the kernel code, that is, using EXPORT_SYMBOL or EXPORT_SYMBOL_GPL can export a function as a symbol for use by other modules. However, not all functions or symbols in the kernel have EXPORT_SYMBOL or EXPORT_SYMBOL_GPL tags, that is, not all symbols are exported in the kernel. For example, in the 3.8.0 kernel, do_page_fault is not exported.
[0084] In this method, some kernel APIs needed in the kernel module are not exported, so these APIs cannot be used directly in the kernel module. In order to adapt to various versions of the Linux kernel, it is necessary to export the kernel APIs used in the kernel module that are not exported, that is, to obtain the addresses of these APIs for the kernel module to call.
[0085] Furthermore, the method compares whether the compiled kernel module matches the kernel, specifically:
[0086] Use the modinfo mymodule.ko command to obtain the vermagic information of the kernel module, and then use the uname –a command to obtain the kernel version number. Compare the kernel version information with the vermagic information in the kernel module to confirm whether the compiled kernel module matches the kernel.
[0087] The main purpose of executing this step is to solve the problem that after the Linux target platform system is upgraded, the kernel version of the Linux target platform changes, resulting in the compiled kernel module and the kernel not matching, so that the compiled kernel module cannot be loaded in the kernel. After executing this step, it can be identified that the compiled kernel module and the kernel do not match, so the kernel module can be recompiled on the upgraded target kernel.
[0088] Figure 2 A schematic diagram of a kernel module compiling device provided by an embodiment of the present invention, the device comprising:
[0089] Memory, used to store programs;
[0090] The processor is used to execute the program stored in the memory. When the program stored in the memory is executed, the processor is used to execute the steps of the above method.
[0091] In addition, an embodiment of the present invention provides a computer device, including a memory and a processor, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the processor executes the steps of the above method.
[0092] An embodiment of the present invention provides a storage medium storing computer-readable instructions. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the above method.
[0093] Finally, it should be noted that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art can still modify the technical solutions described in the aforementioned embodiments or replace some of the technical features therein by equivalents. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A kernel module compilation method, characterized in that: The method is: Adapt the kernel module source code to be compiled and loaded according to different versions of the kernel, including: Adapt the kernel header files to be referenced according to the Linux kernel version number, specifically: In the kernel module source code to be compiled and loaded, the macro definition of LINUX_VERSION_CODE is judged to adapt to different versions of the Linux kernel, and the kernel header files of different paths are referenced according to the Linux kernel version number; Adapt the kernel API to be called according to the Linux kernel version number, specifically: In the kernel module source code to be compiled and loaded, the function parameters passed to the API are set according to the Linux kernel version number: the type and number of function parameters are set; the code contains the linux / version.h header file; the macro definition of LINUX_VERSION_CODE is judged to adapt to different versions of the Linux kernel; if the Linux kernel version is greater than or equal to 4.11.0, conditional compilation is triggered, and only the following source code is compiled: zap_page_range(vma, page_addr, PAGE_SIZE), that is, zap_page_range is called to pass three functions to the API interface; if the Linux kernel version is less than 4.11.0, conditional compilation is triggered, and only the following source code is compiled: zap_page_range(vma, page_addr, PAGE_SIZE, NULL), that is, zap_page_range(vma, page_addr, PAGE_SIZE, NULL) is called to pass four parameters to the API interface; Adapt the kernel symbol table according to the Linux kernel version number, specifically: Export the kernel API to be used, and obtain the address of the kernel API to be used so that the kernel module source code to be compiled and loaded can call it; Determine whether there is a compiled kernel module on the specified path; if so, compare the compiled kernel module with the kernel to see if it matches, specifically: Get the vermagic information of the kernel module through the modinfo mymodule.ko command, and then get the kernel version number through the uname –a command. Compare the kernel version information with the vermagic information in the kernel module to confirm whether the compiled kernel module matches the kernel. If it matches, the compiled kernel module is loaded. If it does not match, the encrypted kernel module source code is decrypted and copied to the specified hidden directory. If it does not exist, the encrypted kernel module source code is decrypted and copied to the specified hidden directory. The decrypted kernel module source code is compiled on the kernel. The compiled kernel module is loaded. After compiling the decrypted kernel module source code on the kernel, copy the compiled kernel module to the specified path and delete the decrypted kernel module source code.
2. A kernel module compiling device, characterized in that: The device comprises: Memory, used to store programs; A processor is used to execute the program stored in the memory. When the program stored in the memory is executed, the processor is used to execute the steps of the method as claimed in claim 1.
3. A computer device comprising a memory and a processor, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the processor executes the steps of the method according to claim 1.
4. A storage medium storing computer-readable instructions, wherein when the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the method according to claim 1.
Citation Information
Patent Citations
Kernel module compilation and loading methods and apparatuses
CN108052327A
Method and system for compiling kernel module
CN113220303A