Binary application compatible operation method and device
By designing a device that is translated and executed while on the LoongArch platform, combining multi-threading and context switching optimization, the problem of inefficient compatibility and operation of binary applications on the LoongArch platform is solved, and efficient cross-platform operation is achieved.
Patent Information
- Application Number
- CN202411284008.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-13
- Publication Date
- 2024-12-13
AI Technical Summary
The compatibility and operation of binary applications between different processor platforms is inefficient, especially between the instruction set architecture of Loongson processor and the source processor platform, resulting in inaccurate translation and simulation, affecting the cross-platform migration and operation of applications.
A device including source platform binary application preprocessing module, translation module, optimization module and target platform binary application compilation and operation module is designed. It adopts the method of translation and execution, and optimizes multi-threaded optimization and process context switching optimization methods. Implement compatible operation of binary applications under the MIPS platform on the LoongArch platform.
It improves the compatibility and operation efficiency of binary applications on the LoongArch platform. Through multi-threading and context switching optimization methods, the number of context switching times and processor resource waste is reduced, and efficient cross-platform operation is achieved.
Smart Images

Figure CN119127219A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of binary translation, and in particular relates to a binary application compatible running method and device. Background Art
[0002] In recent years, with the continuous improvement of national information security protection requirements, domestic processors have made great progress. As domestic processors are constantly upgraded and iterated, how to migrate binary applications on low-version domestic processors across platforms to high-version processors and run them compatibly has become an urgent problem to be solved. Binary translation technology is an important method to achieve compatible operation of binary applications. By performing instruction translation, binary applications can be run directly on other platforms without recompiling the source code, which can effectively solve the problem of compatible operation of binary applications and is of great significance.
[0003] Loongson processors are currently the most popular and advanced processors in my country, and a large number of binary applications need to be ported to high-version processors for cross-platform compatibility. The current method for translating Loongson binary applications is to load the binary files of the source processor platform application on the target processor platform, translate the instructions, and execute the binary code generated by the translation on the target platform.
[0004] In view of the different instruction set architectures between the target processor platform and the source processor platform, in order to achieve instruction equivalent translation and correct simulation of different platform states, the binary application compatible operation has the problem of low efficiency. Summary of the invention
[0005] 1. Technical issues to be resolved
[0006] The technical problem to be solved by the present invention is how to provide a binary application compatible operation method and device to solve the problem of low efficiency in the compatible operation of binary applications.
[0007] (II) Technical solution
[0008] In order to solve the above technical problems, the present invention proposes a binary application compatible running device, which includes: a source platform binary application preprocessing module, a translation module, an optimization module and a target platform binary application compiling and running module;
[0009] A source platform binary application preprocessing module, used for loading and parsing the source platform binary application;
[0010] A translation module, used for decoding and translating the parsed binary application;
[0011] Optimization module, used to optimize the translation and operation process;
[0012] The target platform binary application compilation and execution module is used to compile and execute the translated binary application that conforms to the target platform instruction set architecture.
[0013] Furthermore, the device runs on a target platform and uses a translation-while-execution approach to achieve binary application program compatibility.
[0014] Furthermore, binfmt_misc will be mounted when the operating system starts. When the source platform program is run, the operating system will automatically call the device through binfmt_misc to preprocess, translate, optimize and run the binary application, so as to achieve compatible running of the binary application under the source platform on the target platform.
[0015] Furthermore, the source platform is a MIPS platform, and the target platform is a Loongson LoongArch platform.
[0016] The present invention also provides a binary application compatible running method, which includes two stages: the first stage is binary application preprocessing, and the second stage is binary application translation, optimization and running;
[0017] Phase 1: Binary Application Preprocessing
[0018] When running a MIPS program, the operating system determines that the running program is a MIPS program according to binfmt_misc and calls the device. The source platform binary application preprocessing module loads and parses the MIPS ELF file. The source platform binary application preprocessing module loads all code segments and data segments into the virtual address space of the device and stores the program entry address in the MIPS ELF file into the PC.
[0019] Phase 2: Binary application translation, optimization, and execution
[0020] The device obtains the program entry address in the MIPS ELF file from the PC and starts to translate the code, disassembles the MIPS code, and calls the compiler to divide the basic blocks; the device maintains a hash table and stores the translated basic block information in the hash table;
[0021] When translating, first enter the hash table to query whether the basic block has been translated. If it has been translated, run the translated LoongArch platform code through the target platform binary application compilation and operation module, and update the PC to continue to obtain the next basic block for translation until the entire translation process is completed; if it has not been translated, translate, optimize and run through the translation module, optimization module and target platform binary application compilation and operation module: first store the basic block as an intermediate code; then translate the intermediate code for instruction equivalent replacement. During this process, the optimization module optimizes through multi-threaded optimization methods and process context switching optimization methods. After the translation is completed, maintain the hash table to add the translated basic block information, then exit the translation and generate the LoongArch platform code, and run the LoongArch platform code through the target platform binary application compilation and operation module; finally, update the PC to continue to obtain the next basic block for translation until the entire translation process is completed.
[0022] Furthermore, the MIPS ELF file stores the program's machine information, architecture, program entry address, code segment, and data segment.
[0023] Furthermore, the multi-thread optimization method includes: the device creates two translation threads and one execution thread, the translation thread is used to translate the source platform binary application file, and the execution thread is used to execute the translated target platform binary application program.
[0024] Furthermore, the code translation thread and the code execution thread are executed in parallel.
[0025] Furthermore, the process context switch optimization method includes: the device determines whether the basic block ends with a direct address transfer instruction. The function of the direct address transfer instruction is to send the address of the instruction to be executed next to the PC. If a basic block ends with a direct address transfer instruction, the device directly obtains the next basic block from the PC for translation during translation without performing a context switch. Otherwise, a context switch is performed. After the translation of the current basic block is completed, the device adds a jump instruction at the end of the translated previous basic block to point to the current basic block.
[0026] Furthermore, when executing the translated basic block, it is executed continuously through jump instructions.
[0027] (III) Beneficial effects
[0028] The present invention proposes a binary application compatible operation method and device. The present invention proposes a binary application compatible operation method and device. The device runs on the LoongArch platform, can realize the binary application compatible operation of the MIPS platform, and improves the binary application compatible operation efficiency through multi-threading and process context switching optimization. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 This is a functional architecture diagram of the binary application compatible operation method and device of the present invention;
[0030] Figure 2 The binary application compatible operation method and device workflow of the present invention;
[0031] Figure 3 Translating, optimizing and executing processes for binary applications of the present invention;
[0032] Figure 4 This is a schematic diagram of multi-thread optimization of the present invention;
[0033] Figure 5 Schematic diagram before optimizing for context switching;
[0034] Figure 6 This is a schematic diagram after the context switching optimization of the present invention. DETAILED DESCRIPTION
[0035] In order to make the purpose, content and advantages of the present invention more clear, the specific implementation methods of the present invention are further described in detail below in conjunction with the drawings and examples.
[0036] like Figure 1 As shown, the binary application compatible operation method and device proposed by the present invention mainly include four modules: a source platform binary application preprocessing module, a translation module, an optimization module and a target platform binary application compilation and operation module.
[0037] A source platform binary application preprocessing module, used for loading and parsing the source platform binary application;
[0038] A translation module, used for decoding and translating the parsed binary application;
[0039] Optimization module, used to optimize the translation and operation process;
[0040] The target platform binary application compilation and execution module is used to compile and execute the translated binary application that conforms to the target platform instruction set architecture.
[0041] Among them, the source platform is the MIPS platform and the target platform is the Loongson LoongArch platform.
[0042] The device runs on a target platform and uses a translation-while-execution approach to achieve binary application program compatibility.
[0043] Among them, binfmt_misc will be mounted when the operating system starts. When running the source platform program, the operating system will automatically call the device through binfmt_misc to preprocess, translate, optimize and run the binary application, so as to achieve compatible running of the binary application under the source platform on the target platform.
[0044] The binary application compatible operation method and device proposed in the present invention run on the LoongArch platform, and realize the compatible operation of binary applications by translating and executing at the same time. When the operating system starts, binfmt_misc will be mounted. Binfmt_misc is a function in the kernel, which can automatically match binary files in non-native formats with the parser for binary parsing. When running a MIPS program, the operating system will automatically call the device through binfmt_misc to perform binary application preprocessing, translation, optimization and operation, so as to realize the compatible operation of binary applications under the MIPS platform on the LoongArch platform.
[0045] like Figure 2 As shown, the workflow of the binary application compatible operation method proposed in the present invention is divided into two stages: the first stage is binary application preprocessing, and the second stage is binary application translation, optimization and operation.
[0046] Phase 1: Binary Application Preprocessing
[0047] When running a MIPS program, the operating system determines that the program being run is a MIPS program based on binfmt_misc and calls the device. The device source platform binary application preprocessing module loads and parses the MIPS ELF file. The MIPSELF file stores the program's machine information, architecture, program entry address, code segment, data segment, etc. The device source platform binary application preprocessing module loads all code segments and data segments into the device virtual address space and stores the program entry address in the MIPS ELF file into the PC.
[0048] Phase 2: Binary application translation, optimization, and execution
[0049] After preprocessing, the binary application enters the translation, optimization and runtime stages.
[0050] The device obtains the program entry address in the MIPS ELF file from the PC and starts to translate the code, disassembles the MIPS code, and calls the compiler to divide the basic blocks (a basic block is a sequence of statements in the program that is executed in the most sequential order). The device maintains a hash table and stores the translated basic block information in the hash table.
[0051] When translating, first enter the hash table to query whether the basic block has been translated. If it has been translated, the translated LoongArch platform code is run through the target platform binary application compilation and operation module, and the PC is updated to continue to obtain the next basic block for translation until the entire translation process is completed. If it has not been translated, it is translated, optimized and run through the translation module, optimization module and target platform binary application compilation and operation module: first, the basic block is stored as an intermediate code; then the intermediate code is translated for instruction equivalent replacement. During this process, the optimization module optimizes through multi-threaded optimization methods and process context switching optimization methods. After the translation is completed, the hash table is maintained to add the translated basic block information, and then the translation is exited and the LoongArch platform code is generated, and the LoongArch platform code is run through the target platform binary application compilation and operation module; finally, the PC is updated to continue to obtain the next basic block for translation until the entire translation process is completed. The binary application translation, optimization and execution process is as follows Figure 3 shown.
[0052] At this stage, the device used two optimization methods.
[0053] One is multi-threaded optimization, such as Figure 4 As shown, since the translation process takes longer than the execution process, the device creates two translation threads and one execution thread to improve the compatible operation efficiency of binary applications. The translation thread is used to translate the source platform binary application file, and the execution thread is used to execute the translated target platform binary application. The code translation thread and the code execution thread are executed in parallel to improve the processor resource utilization, so as to achieve the purpose of improving the compatible operation efficiency of binary applications.
[0054] The second is process context switching optimization. When the CPU switches from one process to another, it needs to save and restore the execution status (context) of the two processes. A large number of context switches will affect the program execution efficiency. Figure 5 As shown, during the binary application translation and operation process, there are two process contexts in the operating system: the target platform binary application execution process and the binary application compatible operation device execution process. After translating a basic block, the translated basic block needs to be executed. The operating system will switch the binary application and translation program contexts back and forth, reducing the binary application compatible operation efficiency. Figure 6As shown, the device determines whether a basic block ends with a direct address transfer instruction. The function of the direct address transfer instruction is to send the address of the instruction to be executed next to the PC. If a basic block ends with a direct address transfer instruction, the device directly obtains the next basic block from the PC for translation without context switching. Otherwise, it performs context switching. After the translation of this basic block is completed, the device adds a jump instruction at the end of the translated previous basic block to point to this basic block, such as Figure 6 For example, basic blocks n, n+1, and n+2 are translated continuously; when the translated basic blocks are executed, they can be executed continuously through jump instructions, such as Figure 6 For example, the translated basic blocks n, n+1, and n+2 are executed. Through process context switching optimization, context switching can be reduced and the compatibility running efficiency of binary applications can be improved.
[0055] The present invention proposes a binary application compatible operation method and device, which runs on the LoongArch platform, can realize the binary application compatible operation of the MIPS platform, and improve the binary application compatible operation efficiency through multi-threading and process context switching optimization.
[0056] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the technical principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A binary application compatible running device, characterized in that: The device comprises: a source platform binary application preprocessing module, a translation module, an optimization module and a target platform binary application compiling and running module; A source platform binary application preprocessing module, used for loading and parsing the source platform binary application; A translation module, used for decoding and translating the parsed binary application; Optimization module, used to optimize the translation and operation process; The target platform binary application compilation and execution module is used to compile and execute the translated binary application that conforms to the target platform instruction set architecture.
2. The binary application compatible running device according to claim 1, characterized in that: The device runs on a target platform and uses a translation-while-execution approach to achieve binary application program compatibility.
3. The binary application compatible running device according to claim 2, characterized in that: When the operating system starts, binfmt_misc will be mounted. When the source platform program is run, the operating system will automatically call the device through binfmt_misc to preprocess, translate, optimize and run the binary application, so as to achieve the compatibility of the binary application under the source platform to run on the target platform.
4. The binary application compatible running device according to claim 2 or 3, characterized in that: The source platform is MIPS platform and the target platform is Loongson LoongArch platform.
5. A binary application compatible operation method based on the device of claim 4, characterized in that: The method consists of two stages: the first stage is binary application preprocessing, and the second stage is binary application translation, optimization and operation; Phase 1: Binary Application Preprocessing When running a MIPS program, the operating system determines that the running program is a MIPS program according to binfmt_misc and calls the device. The source platform binary application preprocessing module loads and parses the MIPS ELF file. The source platform binary application preprocessing module loads all code segments and data segments into the virtual address space of the device and stores the program entry address in the MIPS ELF file into the PC. Phase 2: Binary application translation, optimization, and execution The device obtains the program entry address in the MIPS ELF file from the PC and starts to translate the code, disassembles the MIPS code, and calls the compiler to divide the basic blocks; the device maintains a hash table and stores the translated basic block information in the hash table; When translating, first enter the hash table to query whether the basic block has been translated. If it has been translated, run the translated LoongArch platform code through the target platform binary application compilation and operation module, and update the PC to continue to obtain the next basic block for translation until the entire translation process is completed; if it has not been translated, translate, optimize and run through the translation module, optimization module and target platform binary application compilation and operation module: first store the basic block as an intermediate code; then translate the intermediate code for instruction equivalent replacement. During this process, the optimization module optimizes through multi-threaded optimization methods and process context switching optimization methods. After the translation is completed, maintain the hash table to add the translated basic block information, then exit the translation and generate the LoongArch platform code, and run the LoongArch platform code through the target platform binary application compilation and operation module; finally, update the PC to continue to obtain the next basic block for translation until the entire translation process is completed.
6. The binary application compatible operation method according to claim 4, characterized in that: The MIPS ELF file stores the program's machine information, architecture, program entry address, code segment, and data segment.
7. The binary application compatible operation method according to claim 4, characterized in that: The multi-thread optimization method comprises: the device creates two translation threads and one execution thread, the translation thread is used for translating the source platform binary application file, and the execution thread is used for executing the translated target platform binary application program.
8. The binary application compatible operation method according to claim 7, characterized in that: The code translation thread and the code execution thread are executed in parallel.
9. The binary application compatible operation method according to claim 4, characterized in that: The process context switch optimization method includes: the device determines whether a basic block ends with a direct address transfer instruction. The function of the direct address transfer instruction is to send the address of the instruction to be executed next to the PC. If a basic block ends with a direct address transfer instruction, the device directly obtains the next basic block from the PC for translation during translation without context switching. Otherwise, context switching is performed. After the translation of the current basic block is completed, the device adds a jump instruction at the end of the translated previous basic block to point to the current basic block.
10. The binary application compatible operation method according to claim 9, characterized in that: When executing the translated basic block, execution continues via jump instructions.
Citation Information
Cited By
Multi-thread dynamic binary translation method, device, equipment and product
CN119718337A
Multi-threaded Dynamic Binary Translation Method, Device, Equipment and Product
CN119718337B
