A heterogeneous instruction set program loading method based on kernel data reconstruction

Through Hook system calls and modifying the kernel module, the problem of heterogeneous instruction set program loading when the Binfmt_misc configuration item is not enabled in Linux system is solved, and the dynamic loading and running of heterogeneous instruction set programs is realized, avoiding the requirement of kernel recompilation.

CN118838745BActive Publication Date: 2024-11-15北京麟卓信息科技有限公司
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202411314370.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-20
Publication Date
2024-11-15
Estimated Expiration
2044-09-20

AI Technical Summary

Technical Problem

In Linux systems, when the Binfmt_misc configuration item is not enabled, heterogeneous instruction set programs cannot be loaded dynamically, and the kernel module cannot be loaded dynamically, which limits the operation of programs with different instruction set architectures on the current processor.

Method used

The Hook system calls the execve function to monitor exceptions, build a dynamically loadable kernel module, modify the search_binary_handler function and binary program processor list, realize the loading of heterogeneous instruction set programs, and bypass the configuration limitations during kernel compilation.

Benefits of technology

Without recompiling the kernel and modifying the application, the heterogeneous instruction set program is successfully loaded, which realizes the operation of programs with different instruction set architectures on the current processor, solving the problem that the Binfmt_misc configuration item is not enabled.

✦ Generated by Eureka AI based on patent content.
Patent Text Reader

Abstract

The invention discloses a heterogeneous instruction set program loading method based on kernel data reconstruction. After determining that the Linux system kernel does not open the Binfmt_misc related configuration items, the system is monitored for exceptions related to program loading. For exceptions related to the heterogeneous instruction set program, a search_binary_handler function is hooked or a binary program processor list maintained by the system is modified, so that the heterogeneous instruction set program loader is used to complete the loading of the heterogeneous instruction set program. For irrelevant exceptions, the corresponding execve function entry parameter is added to the tail of the suspended program list. After the heterogeneous instruction set program is loaded and restored according to the search_binary_handler function or the binary program processor list, the loading of the related program is completed according to the suspended program list, so that the loading of the heterogeneous instruction set program is realized through the Binfmt_misc mechanism without recompiling the kernel and modifying the application.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of computer software development, and in particular relates to a heterogeneous instruction set program loading method based on kernel data reconstruction. Background Art

[0002] When running programs built with different instruction sets in the system, system support is usually required. The Binfmt_misc mechanism is a function provided by the Linux kernel that is similar to the file association function in the Windows system. However, unlike file association, it can determine the program that opens the file not only based on the file suffix, but also based on the file content. One of the typical applications of the Binfmt_misc mechanism is to support the use of the instruction conversion engine to run executable files on other instruction set platforms.

[0003] Specifically, during the initialization phase of Linux, for each executable file format, the corresponding struct linux_binfmt structure will be registered in the kernel. When the program actually loads the executable file, the corresponding load_binary callback function is called to load the executable file. For ELF format executable files, the corresponding registration program is usually in fs / binfmt_elf.c.

[0004] However, the use of the Binfmt_misc mechanism requires setting the corresponding configuration item when the kernel is compiled, that is, turning on the CONFIG_BINFMT_MISC configuration item. If this configuration item is not turned on when the kernel is compiled, the Binfmt_misc mechanism cannot be used. In addition, the kernel module cannot be loaded dynamically. Summary of the invention

[0005] In view of this, the present invention provides a heterogeneous instruction set program loading method based on kernel data reconstruction, which realizes the running of programs under different instruction set architectures on the current processor through the Binfmt_misc mechanism when the Binfmt_misc related configuration items are not enabled during kernel compilation.

[0006] The present invention provides a method for loading a heterogeneous instruction set program based on kernel data reconstruction, which specifically includes the following steps:

[0007] Step 1, determine that the system kernel does not enable the Binfmt_misc related configuration items, record the program path of the heterogeneous instruction set program as the target program path, record the call parameters of the execve function to load the heterogeneous instruction set program as the target program parameters, and record the environment variable set as the target program environment variables;

[0008] Step 2: The Hook system calls the execve function to monitor the exceptions related to the loading of heterogeneous instruction set programs in the system. When the loading of heterogeneous instruction set programs is detected to be abnormal, step 3 is executed.

[0009] Step 3: intercept the return value of the execve function so that the value is not sent to the user state, and obtain the entry parameter of the execve function. If the first parameter is the same as the target program path, execute step 4; otherwise, add the entry parameter to the end of the suspended program list and then execute step 2;

[0010] Step 4: construct a dynamically loadable kernel module, recorded as a heterogeneous instruction set program loader, for loading a first data structure of a heterogeneous instruction set program, the first data structure comprising a first circular bidirectional linked list and a pointer to a first loading function, the first loading function being used to read and load a function of the heterogeneous instruction set program; load the heterogeneous instruction set program loader;

[0011] Step 5, if the symbol search_binary_handler exists in the kernel symbol table, execute step 6, otherwise execute step 7;

[0012] Step 6: Hook the search_binary_handler function to return the heterogeneous instruction set program loader when the function is called, and then execute step 8.

[0013] Step 7, calling the insert_binfmt function with the heterogeneous instruction set program loader as a parameter, traversing the binary program processor list of the system according to the first circular bidirectional linked list, taking the loader related to the ELF file as the loader to be set, modifying the pointer of the original loading function in the loader to be set to the pointer of the first loading function, and executing step 8;

[0014] Step 8. Execute the execve function again with the target program path, target program parameters and target program environment variables obtained in step 1 as parameters to complete the loading of the heterogeneous instruction set program; when the process of the heterogeneous instruction set program is created, if the symbol search_binary_handler exists, remove the Hook operation of the search_binary_handler function; if it does not exist, restore the binary program processor list to an unmodified state; disable the judgment and processing of the first parameter of the execve function, and then read each list item in turn according to the first-in-first-out order according to the suspended program list, and call the execve function with each list item as a parameter to complete the loading of the suspended program.

[0015] Furthermore, the Binfmt_misc related configuration item is the CONFIG_BINFMT_MISC configuration item.

[0016] Furthermore, the method of determining in step 1 that the system kernel does not enable the Binfmt_misc related configuration item is: determining by viewing the kernel configuration file.

[0017] Furthermore, the method for determining the abnormal loading of the heterogeneous instruction set program monitored in step 2 is:

[0018] Step 2.1, when the result returned by the execve function is monitored as -ENOEXEC, read and parse the file header of the heterogeneous instruction set program to obtain the file header data structure;

[0019] Step 2.2, search the file header data structure. If e_ident[0] is equal to 0x7f, header.e_ident[1] is equal to E, header.e_ident[2] is equal to L, and header.e_ident[3] is equal to F, and the instruction set represented by e_machine is different from the instruction set of the current processor, then the current exception is determined to be a loading exception of a heterogeneous instruction set program; otherwise, the current exception is determined to be a loading exception of a non-heterogeneous instruction set program. Beneficial Effects

[0020] After determining that the Linux system kernel does not enable Binfmt_misc related configuration items, the present invention monitors exceptions related to program loading in the system, and for exceptions related to heterogeneous instruction set programs, a search_binary_handler function is hooked or a binary program processor list maintained by the system is modified, so that the loading of the heterogeneous instruction set program is completed by using a heterogeneous instruction set program loader, and for irrelevant exceptions, a corresponding execve function entry parameter is added to the end of the suspended program list, and after the heterogeneous instruction set program is loaded and restored according to the search_binary_handler function or the binary program processor list, the loading of related programs is completed according to the suspended program list, thereby realizing the loading of heterogeneous instruction set programs through the Binfmt_misc mechanism without recompiling the kernel and modifying the application. DETAILED DESCRIPTION

[0021] The present invention is described in detail with reference to the following embodiments.

[0022] The present invention provides a method for loading a heterogeneous instruction set program based on kernel data reconstruction, the core idea of ​​which is: after determining that the Linux system kernel does not enable Binfmt_misc related configuration items, monitoring the system for exceptions related to program loading, and judging whether the exception is related to the heterogeneous instruction set program according to the entry parameters of the execve function, for related exceptions, by performing a Hook operation on the search_binary_handler function or modifying the binary program processor list maintained by the system, the heterogeneous instruction set program loader is used to complete the loading of the heterogeneous instruction set program, and for unrelated exceptions, the corresponding execve function entry parameters are added to the end of the suspended program list, and after the heterogeneous instruction set program is loaded and restored according to the search_binary_handler function or the binary program processor list, the loading of the related program is completed according to the suspended program list.

[0023] The present invention provides a method for loading a heterogeneous instruction set program based on kernel data reconstruction, which specifically includes the following steps:

[0024] Step 1. After confirming that the CONFIG_BINFMT_MISC configuration item is not enabled in the Linux system kernel, the program path of the heterogeneous instruction set program is recorded as the target program path, the calling parameters that should be used to load the heterogeneous instruction set program using the execve function are recorded as the target program parameters, and the environment variable set is recorded as the target program environment variables.

[0025] Specifically, there are three main ways to determine whether the CONFIG_BINFMT_MISC configuration item is not enabled in the Linux system kernel:

[0026] One is to check the configuration file. If the Linux system retains the configuration file used to compile the currently running kernel, you can check the configuration file. Generally, the configuration file is saved in the / boot directory with a name similar to config-$(uname -r).

[0027] For example, execute the command: grep CONFIG_BINFMT_MISC / boot / config-$(uname -r). If the returned result is "CONFIG_BINFMT_MISC=y", it means that CONFIG_BINFMT_MISC is enabled.

[0028] The second method is to judge through the ` / proc` file system, that is, to check whether there is a misc file in the ` / proc` file system. If it exists, it means that CONFIG_BINFMT_MISC is enabled, otherwise it is not enabled.

[0029] For example, execute the command: ls / proc / sys / fs / binfmt_misc. If the returned result contains the directory structure instead of an error, it means that CONFIG_BINFMT_MISC is enabled, that is, Binfmt_misc has been loaded and is in use.

[0030] The third method is to execute the findmnt command to check whether the Binfmt_misc file system has been loaded. If the findmnt command exists in the current Linux system, you can execute the findmnt command to check whether the Binfmt_misc file system has been loaded.

[0031] For example, when executing the command: findmnt / proc / sys / fs / binfmt_misc, the result returned is:

[0032] TARGET SOURCE FSTYPE OPTIONS

[0033] / proc / sys / fs / binfmt_misc binfmt_misc binfmt_misc rw,relatime

[0034] This means that Binfmt_misc has been loaded.

[0035] Step 2: Monitor the exceptions related to the loading of heterogeneous instruction set programs in the Linux system by hooking the kernel-level system call execve function. When the loading of the heterogeneous instruction set program is monitored to be abnormal, execute step 3.

[0036] On Linux, the execve function is usually used to execute heterogeneous instruction set programs, such as: execve(const char *filename, char *const argv[], char *const envp[]). The execve function entry parameters include the program path, call parameters and environment variable set. When the execve function is used to execute heterogeneous instruction set programs, an error may be reported, and the error information may be: cannot execute binary file: Exec format error. The present invention monitors the exceptions related to the loading of heterogeneous instruction set programs in the Linux system by hooking the kernel-level system call execve function, specifically:

[0037] Step 2.1: When the result returned by the execve function is monitored as -ENOEXEC, read and parse the file header of the heterogeneous instruction set program to obtain the file header data structure.

[0038] The definition of the file header data structure is as follows:

[0039] #define EI_NIDENT 16

[0040] typedef struct {

[0041] unsigned char e_ident[EI_NIDENT];

[0042] uint16_t e_type;

[0043] uint16_t e_machine;

[0044] uint32_t e_version;

[0045] uint64_t e_entry;

[0046] uint64_t e_phoff;

[0047] uint64_t e_shoff;

[0048] uint32_t e_flags;

[0049] uint16_t e_ehsize;

[0050] uint16_t e_phentsize;

[0051] uint16_t e_phnum;

[0052] uint16_t e_shentsize;

[0053] uint16_t e_shnum;

[0054] uint16_t e_shstrndx;

[0055] } Elf64_Ehdr;

[0056] Step 2.2, search the file header data structure. If e_ident[0] is equal to 0x7f, header.e_ident[1] is equal to E, header.e_ident[2] is equal to L, and header.e_ident[3] is equal to F, and the instruction set identified by e_machine is different from the instruction set of the current processor, then the current exception is determined to be an exception related to the loading of a heterogeneous instruction set program; otherwise, the current exception is determined to be an exception related to the loading of a non-heterogeneous instruction set program.

[0057] Among them, when e_ident[0] is equal to 0x7f, header.e_ident[1] is equal to E, header.e_ident[2] is equal to L, and header.e_ident[3] is equal to F, it indicates that the file of the heterogeneous instruction set program is an ELF file.

[0058] Common instruction sets identified by e_machine, such as:

[0059] 0x02: SPARC

[0060] 0x03: x86

[0061] 0x08: MIPS

[0062] 0x14: PowerPC

[0063] 0x28: ARM

[0064] 0x3E: x86-64

[0065] 0xB7: AArch64

[0066] Step 3: intercept the return value of the execve function so that the value is not sent to the user state, and obtain the entry parameter of the execve function. If the first parameter is the same as the target program path, execute step 4; otherwise, add the entry parameter to the end of the suspended program list and then execute step 2.

[0067] Step 4, build a heterogeneous instruction set program loader. The heterogeneous instruction set program loader is a dynamically loadable kernel module. The heterogeneous instruction set program loader defines a first data structure for loading a heterogeneous instruction set program. The first data structure includes a first circular bidirectional linked list and a pointer to a first loading function. The first loading function is a function for reading and loading a heterogeneous instruction set program; dynamically load the heterogeneous instruction set program loader.

[0068] For example, the first data structure looks like this:

[0069] struct linux_binfmt {

[0070] struct list_head lh;

[0071] struct module *module;

[0072] int (*load_binary)(struct linux_binprm *);

[0073] int (*load_shlib)(struct file *);

[0074] }

[0075] Step 5: Traverse the kernel symbol table to find the symbol search_binary_handler. If it exists, execute step 6; otherwise, it means that the symbol has not been exported, so execute step 7.

[0076] Step 6: Hook the search_binary_handler function to return the heterogeneous instruction set program loader as the result when the search_binary_handler function is called, and then execute step 8.

[0077] Step 7, call the insert_binfmt function with the heterogeneous instruction set program loader as a parameter, implement traversal of the binary program processor list currently maintained by the system according to the first circular bidirectional linked list, find the loader related to the ELF file as the loader to be set, and modify the pointer of the original loading function in the loader to be set to the pointer of the first loading function, and then execute step 8.

[0078] Step 8. Execute the execve function again with the target program path, target program parameters and target program environment variables obtained in step 1 as parameters to complete the loading of the heterogeneous instruction set program; when the process of the heterogeneous instruction set program is created, if the symbol search_binary_handler exists, remove the Hook operation of the search_binary_handler function; if the symbol search_binary_handler does not exist, restore the binary program processor list to an unmodified state; disable the judgment and processing of the first parameter of the execve function, and then read each list item in turn in the first-in-first-out order according to the information recorded in the suspended program list, and call the execve function with each list item as a parameter to complete the loading of the corresponding suspended program. Example

[0079] In this embodiment, a heterogeneous instruction set program loading method based on kernel data reconstruction provided by the present invention is used to implement the loading of heterogeneous instruction set programs on a Linux system, which specifically includes the following steps:

[0080] S1. Determine whether the CONFIG_BINFMT_MISC configuration item is enabled in the Linux kernel. If not, execute S2. Otherwise, terminate this process.

[0081] S2. Monitor the system for abnormal loading of heterogeneous instruction set programs, including:

[0082] S2.1, the execve function call of the heterogeneous instruction set program, including the target program path recorded as heteroInstrProgramPath, the target program parameters recorded as heteroInstrProgramParams, and the target program environment variables recorded as heteroInstrProgramEnvs;

[0083] For example: execve(const char *filename, char *const argv[], char *const envp[]).

[0084] S2.2. Hook the execve function and monitor whether the return value of the function is "-ENOEXEC" and whether a "bad format" error is caused by unsupported instruction set.

[0085] S3, intercept the execve function so that its return value cannot be sent to the user state, and perform the Hook operation on the execve function, that is, start the judgment of the entry parameters at the entry of the execve function. If the first parameter of the entry parameter is heteroInstrProgramPath, execute S4; if not, add the data structure composed of the entry parameters to the end of the suspended program list pendingExecPrograms, and then execute S2.

[0086] S4. Modify the corresponding list item of the ELF file processing in the binary program processor list currently maintained by the system, modify the value therein to point to the heterogeneous instruction set program loader, and then execute S5.

[0087] The specific steps are as follows:

[0088] S4.1. Build a dynamically loadable kernel module. The core of the kernel module is to define the linux_binfmt data structure for loading heterogeneous instruction set programs, denoted as heteroInstrProgramLoader, and the load_binary function denoted as heteroInstrProgramLoadFunction. The load_library function pointer of heteroInstrProgramLoader points to heteroInstrProgramLoadFunction.

[0089] In the present invention, heteroInstrProgramLoadFunction can realize the reading and loading of heterogeneous instruction set programs.

[0090] S4.2. Locate the symbol search_binary_handler in the kernel. If the symbol is found, a Hook operation is performed on the function. That is, when the function is called, the function returns heteroInstrProgramLoader and executes S5; otherwise, execute S4.3.

[0091] S4.3. Call the insert_binfmt function with heteroInstrProgramLoader as a parameter. The lh member in heteroInstrProgramLoader is used to link heteroInstrProgramLoader to the binary program processor list, and execute S5.

[0092] Among them, lh is a pointer to a doubly linked list. The binary program processor list can be traversed through lh. For the name member of the module member of each item in the list read, it is defined as char name[MODULE_NAME_LEN] in the kernel. If the content of name is binfmt-elf, the load_library function pointer of the linux_binfmt data structure corresponding to lh is modified to heteroInstrProgramLoadFunction.

[0093] S5. Re-call the execve function with heteroInstrProgramPath, heteroInstrProgramParams, and heteroInstrProgramEnvs as parameters to complete the loading of the heterogeneous instruction set program and create a process for the heterogeneous instruction set program.

[0094] S6. If the symbol search_binary_handler exists, delete the Hook operation of the search_binary_handler function, that is, no longer return only heteroInstrProgramLoader; otherwise, restore the processing list item of the ELF file in the binary program processor list; then disable the program path judgment at the entry of the execve function; starting from the header of the list pendingExecPrograms, in a first-in-first-out order, take out the parameters of each list item in turn, including the program path, calling parameters and environment variable set, etc., and call the execve function in turn to execute the programs corresponding to these list items.

[0095] In summary, the above are only preferred embodiments of the present invention and are not intended to limit the protection scope of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.

Claims

1. A method for loading a heterogeneous instruction set program based on kernel data reconstruction, characterized in that: The specific steps include: Step 1, determine that the system kernel does not enable the Binfmt_misc related configuration items, record the program path of the heterogeneous instruction set program as the target program path, record the call parameters of the execve function to load the heterogeneous instruction set program as the target program parameters, and record the environment variable set as the target program environment variables; Step 2: The Hook system calls the execve function to monitor the exceptions related to the loading of heterogeneous instruction set programs in the system. When the loading of heterogeneous instruction set programs is detected to be abnormal, step 3 is executed. Step 3: intercept the return value of the execve function so that the value is not sent to the user state, and obtain the entry parameter of the execve function. If the first parameter is the same as the target program path, execute step 4; otherwise, add the entry parameter to the end of the suspended program list and then execute step 2; Step 4: construct a dynamically loadable kernel module, recorded as a heterogeneous instruction set program loader, for loading a first data structure of a heterogeneous instruction set program, the first data structure comprising a first circular bidirectional linked list and a pointer to a first loading function, the first loading function being used to read and load a function of the heterogeneous instruction set program; load the heterogeneous instruction set program loader; Step 5, if the symbol search_binary_handler exists in the kernel symbol table, execute step 6, otherwise execute step 7; Step 6: Hook the search_binary_handler function to return the heterogeneous instruction set program loader when the function is called, and then execute step 8. Step 7, calling the insert_binfmt function with the heterogeneous instruction set program loader as a parameter, traversing the binary program processor list of the system according to the first circular bidirectional linked list, taking the loader related to the ELF file as the loader to be set, modifying the pointer of the original loading function in the loader to be set to the pointer of the first loading function, and executing step 8; Step 8. Execute the execve function again with the target program path, target program parameters and target program environment variables obtained in step 1 as parameters to complete the loading of the heterogeneous instruction set program; when the process of the heterogeneous instruction set program is created, if the symbol search_binary_handler exists, remove the Hook operation of the search_binary_handler function; if it does not exist, restore the binary program processor list to an unmodified state; disable the judgment and processing of the first parameter of the execve function, and then read each list item in turn according to the first-in-first-out order according to the suspended program list, and call the execve function with each list item as a parameter to complete the loading of the suspended program.

2. The method for loading a heterogeneous instruction set program according to claim 1, characterized in that: The Binfmt_misc related configuration item is the CONFIG_BINFMT_MISC configuration item.

3. The method for loading a heterogeneous instruction set program according to claim 2, characterized in that: The method of determining in step 1 that the system kernel does not enable the Binfmt_misc related configuration items is: determining by viewing the kernel configuration file.

4. The method for loading a heterogeneous instruction set program according to claim 1, characterized in that: The method for determining the abnormal loading of the heterogeneous instruction set program monitored in step 2 is as follows: Step 2.1, when the result returned by the execve function is monitored as -ENOEXEC, read and parse the file header of the heterogeneous instruction set program to obtain the file header data structure; Step 2.2, search the file header data structure. If e_ident[0] is equal to 0x7f, header.e_ident[1] is equal to E, header.e_ident[2] is equal to L, and header.e_ident[3] is equal to F, and the instruction set represented by e_machine is different from the instruction set of the current processor, then the current exception is determined to be a loading exception of a heterogeneous instruction set program; otherwise, the current exception is determined to be a loading exception of a non-heterogeneous instruction set program.

Citation Information

Patent Citations

  • Dynamic binary translation method under multi-processor platform

    CN102087609A

  • Instruction set architectures for fine-grained heterogeneous processing

    CN108572851A

  • System operation method and device across different instruction sets, medium and program product

    CN116414512A

  • Apparatus and method for efficiently implementing a processor pipeline

    US20150378731A1

  • System, Apparatus And Method For Dynamic Update To Code Stored In A Read-Only Memory (ROM)

    US20200104119A1