A method for processing abnormal instructions
By setting a reserved area in user space and using signal processing functions to replace the kernel processing method, the problem of improper handling of abnormal instructions in user programs is solved, and accurate simulated execution of abnormal instructions and system stability are achieved.
Patent Information
- Application Number
- CN202410466612.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-18
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2044-04-18
AI Technical Summary
It is difficult for the existing technology to effectively handle abnormal instructions caused by user programs during operation. Especially when these abnormal instructions are not properly handled, they may cause the user program to stop running or cause system damage, and there is a lack of methods for handling abnormal instructions to ensure that they are Executed correctly.
Execute exception instructions by setting a reserved area in user space, and use signal processing functions to replace the kernel's default processing method, obtain and modify the context, so that the kernel jumps correctly and resumes program execution, ensuring the simulation execution of exception instructions and the correctness of data access. .
It realizes the accurate and effective simulation execution of abnormal instructions of the user program, avoids the suspension of the user program, and ensures the stability and correctness of the system, especially the data processing when accessing the kernel space.
Smart Images

Figure CN118069403B
Abstract
Description
Technical Field
[0001] The present application relates to the field of computers, and in particular to a method for processing abnormal instructions. Background Art
[0002] During the running process of user programs, different exceptions are often generated due to various reasons. For example, the user program uses floating-point instructions to perform floating-point type operations, but the hardware does not support floating-point operations, which causes exceptions. For another example, when the user space is exhausted, the kernel space is allocated to the user program for use, but because the user program lacks the privilege to access the kernel space, when the user program accesses the kernel space, a page exception will be caused. These instructions that cause exceptions during execution are also called "abnormal instructions."
[0003] In the prior art, most of the exception handling methods are to terminate the user program, so that the exception instruction is not successfully executed; or, if the operating system kernel implements proper handling of the exception, the exception instruction may be simulated and executed by the kernel. However, if the operating system kernel has not yet implemented the corresponding proper handling, and these exception instructions do not cause damage to the user program or the computer system; or, these exception instructions are artificially designed and it is hoped that the computer can execute them correctly, then how to handle these exception instructions remains to be solved. Summary of the invention
[0004] The present application provides a method for processing abnormal instructions, which can enable user programs to accurately and effectively simulate the execution of abnormal instructions.
[0005] In a first aspect, an embodiment of the present application provides a method for processing an abnormal instruction, which can be executed by a processing device for an abnormal instruction, and the processing device for the abnormal instruction can be a terminal device or a module for a terminal device, or a server or a module for a server. The present application does not limit the execution subject of the method. The method comprises: a signal processing function obtains a first context corresponding to a user program when running to an abnormal instruction; the signal processing function is registered by the user program and is used to replace the default processing mode of the kernel for the abnormal instruction; the signal processing function writes the abnormal instruction and a first jump instruction into a first reserved area; the abnormal instruction is obtained based on the first context; the first reserved area is located in the user space and is used to temporarily store and execute simulated instructions; the first jump instruction is located after the abnormal instruction and is used to indicate a jump back to the signal processing function; the signal processing function modifies the first context and determines a second context; wherein the program counter in the second context points to the address of the first reserved area; the signal processing function sends a first request to the kernel; the first request is used to request the kernel to resume program execution using the second context; after executing the abnormal instruction in the first reserved area and obtaining a first simulation result, the signal processing function is returned according to the first jump instruction, and the signal processing function obtains the first simulation result.
[0006] In the above scheme, on the one hand, a first reserved area is set in the user space, and abnormal instructions can be executed in the first reserved area, so that the user program can simulate the execution of abnormal instructions without causing the user program to terminate running due to the abnormal instructions; on the other hand, the signal processing function modifies the first context to obtain the second context, and the program counter in the second context points to the address of the first reserved area, so that the kernel can correctly jump to the first reserved area to execute the abnormal instruction according to the second context; on the other hand, the first jump instruction is written into the first reserved area, so that after the simulation execution of the abnormal instruction is completed, it can correctly jump back to the signal processing function, and enable the signal processing function to obtain the first simulation result.
[0007] In a possible implementation method, the kernel receives the page exception triggered by the user program and determines that the type of the page exception is a segmentation fault; the kernel triggers the signal processing function and sends the first context when the page exception occurs as a parameter to the signal processing function; the signal processing function obtains the memory access address that triggers the page exception from the first context, and determines that the memory access address is located in the kernel space.
[0008] The above solution can effectively handle page exceptions caused by user programs accessing kernel space.
[0009] In a possible implementation method, the signal processing function parses the exception instruction to determine whether the exception instruction includes a read operation; if the exception instruction includes a read operation, and the memory access address corresponding to the read operation is located in the kernel space, the signal processing function sends a third request to the kernel; the third request is used to request the kernel to read data; the signal processing function writes the data into a second reserved area; the second reserved area is located in the user space, and the second reserved area is used to temporarily store data required to be accessed by the simulated instruction; the signal processing function modifies the exception instruction and / or the first context according to the address corresponding to the data in the second reserved area; the signal processing function writes the exception instruction into the first reserved area, including: the signal processing function writes the modified exception instruction into the first reserved area.
[0010] In the above scheme, a second reserved area is set in the user space, and the second reserved area is used to temporarily store the data that needs to be accessed by the simulated instruction; since the user program lacks the privilege of accessing the kernel space, when the memory access address corresponding to the read operation is located in the kernel space, a third request can be sent to the kernel, requesting the kernel to read the data from the redirection space and return the data to the signal processing function; after the signal processing function obtains the data, it writes the data to the second reserved area; then, when the abnormal instruction is executed in the first reserved area, the corresponding data can be obtained from the second reserved area to ensure the normal execution of the user program, thereby enabling the user program to simulate the execution of instructions accessing the kernel space.
[0011] In one possible implementation method, the signal processing function parses the exception instruction to determine whether the exception instruction includes a write operation; if the exception instruction includes a write operation, and the memory access address corresponding to the write operation is located in the kernel space, the signal processing function presets an address in the second reserved area for the write operation, and modifies the exception instruction and / or the first context according to the preset address.
[0012] The above scheme can accurately and effectively process write operations where the memory access address is located in the kernel space, and can enable the user program to simulate the execution of instructions for accessing the kernel space.
[0013] In a possible implementation method, the signal processing function parses the exception instruction to determine whether the exception instruction includes a write operation; if the exception instruction includes a write operation, and the memory address corresponding to the data to be written is located in the kernel space, the signal processing function obtains the data to be written from the second reserved area; the signal processing function sends a fourth request to the kernel; the fourth request is used to request the kernel to write the data to be written to the redirection space; the redirection space refers to an address space that has a mapping relationship with the kernel space.
[0014] The above scheme can realize accurate and effective writing of the data to be written into the redirection space, and can enable the user program to simulate the execution of instructions for accessing the kernel space.
[0015] In a possible implementation method, the signal processing function obtains a third context, where the third context is generated by executing the abnormal instruction in the first reserved area; and the signal processing function obtains the first simulation result from the third context.
[0016] The above scheme can accurately and quickly obtain the first simulation result from the third context. In a possible implementation method, the signal processing function modifies the third context to determine the fourth context; wherein the program counter in the fourth context points to the next instruction of the abnormal instruction; the signal processing function sends a fifth request to the kernel; the fifth request is used to request the kernel to use the fourth context to resume program execution.
[0017] The above scheme can accurately and effectively execute the next instruction of the abnormal instruction and resume program execution after the simulation execution of the abnormal instruction is completed.
[0018] In a possible implementation method, the signal processing function obtains N instructions in the user program that are located after the exception instruction; N is an integer greater than or equal to 0; the signal processing function writes the exception instruction and the N instructions into the first reserved area; the signal processing function writes the first jump instruction into the first reserved area; the first jump instruction is located after the exception instruction and the N instructions.
[0019] The above solution can simulate the execution of abnormal instructions and N instructions at one time, improve program execution efficiency, reduce unnecessary context switching and signal processing, and improve performance.
[0020] In a possible implementation method, the abnormal instruction is determined to be a complex instruction; the complex instruction includes a memory access operation and a calculation operation, multiple memory access operations or multiple calculation operations.
[0021] The above scheme can realize accurate and effective simulation and execution of complex instructions.
[0022] In a possible implementation method, the abnormal instruction is determined to be a memory access instruction; the signal processing function parses the abnormal instruction and determines that the memory access address of the abnormal instruction is located in the kernel space; the signal processing function determines a first redirection address corresponding to the memory access address; the first redirection address is an address located in the redirection space; the signal processing function sends a second request to the kernel; the second request is used to request the kernel to perform data read and write operations according to the first redirection address; the signal processing function obtains the read and write results of the kernel, simulates the execution of the abnormal instruction, and obtains a second simulation result.
[0023] In the above scheme, if the abnormal instruction is a memory access instruction, the abnormal instruction may not be written into the first reserved area, but the kernel performs a read and write operation on the data, and then the signal processing function directly simulates the execution of the abnormal instruction. This scheme can effectively process memory access instructions and improve the execution efficiency of user programs.
[0024] In a possible implementation method, when the user program is started, a first reserved area and a second reserved area are set; or,
[0025] When the kernel space is allocated to the user program for the first time, a first reserved area and a second reserved area are set.
[0026] The above solution can improve the flexibility of allocating the first reserved area and the second reserved area.
[0027] In a possible implementation method, when allocating the first reserved area, the permission corresponding to the first reserved area is set to be writable and executable; or,
[0028] When allocating the first reserved area, setting the permission corresponding to the first reserved area to be unreadable, unwritable, and unexecutable; when writing an abnormal instruction to the first reserved area, setting the permission corresponding to the first reserved area to be writable but not executable; when executing the first reserved area, setting the permission corresponding to the first reserved area to be unwritable but executable; after executing the first reserved area, setting the permission corresponding to the first reserved area to be unreadable, unwritable, and unexecutable; or,
[0029] When allocating the first reserved area, the permission corresponding to the first reserved area is set to be writable but not executable; when executing the first reserved area, the permission corresponding to the first reserved area is set to be non-writable but executable; after executing the first reserved area, the permission corresponding to the first reserved area is set to be writable but not executable.
[0030] In the above scheme, setting permissions for the first reserved area can increase the security of the first reserved area; setting permissions in different ways can improve the flexibility of setting permissions for the first reserved area.
[0031] In a possible implementation method, when allocating the second reserved area, the permission corresponding to the second reserved area is set to be readable and writable but not executable; or,
[0032] When allocating the second reserved area, the permissions corresponding to the second reserved area are set to be unreadable, unwritable, and unexecutable; when performing a read operation or a write operation on the second reserved area, the permissions corresponding to the second reserved area are set to be readable, writable, and unexecutable; when completing the read operation or the write operation on the second reserved area, the permissions corresponding to the second reserved area are set to be unreadable, unwritable, and unexecutable.
[0033] The above scheme sets permissions for the second reserved area, which can increase the security of the second reserved area; and adopts different methods to set permissions, which can improve the flexibility of setting permissions for the second reserved area.
[0034] In a second aspect, an embodiment of the present application provides a device for processing abnormal instructions, comprising: an acquisition unit and a processing unit. The acquisition unit is used to acquire a first context corresponding to a user program when running to an abnormal instruction; the signal processing function is registered by the user program and is used to replace the kernel's default processing method for the abnormal instruction; the processing unit is used to write the abnormal instruction and the first jump instruction into a first reserved area; the abnormal instruction is acquired based on the first context; the first reserved area is located in the user space and is used to temporarily store and execute simulated instructions; the first jump instruction is located after the abnormal instruction and is used to indicate a jump back to the signal processing function; the signal processing function modifies the first context and determines a second context; wherein the program counter in the second context points to the address of the first reserved area; the signal processing function sends a first request to the kernel; the first request is used to request the kernel to resume program execution using the second context; after executing the abnormal instruction in the first reserved area and obtaining the first simulation result, the signal processing function is returned according to the first jump instruction, and the signal processing function obtains the first simulation result.
[0035] In a possible implementation method, the processing unit is used to receive a page exception triggered by the user program and determine that the type of the page exception is a segmentation fault; the kernel triggers the signal processing function and sends the first context when the page exception occurs as a parameter to the signal processing function; the signal processing function obtains the memory access address that triggers the page exception from the first context, and determines that the memory access address is located in the kernel space.
[0036] In a possible implementation method, the processing unit is used to parse the exception instruction and determine whether the exception instruction includes a read operation; if the exception instruction includes a read operation, and the memory access address corresponding to the read operation is located in the kernel space, the signal processing function sends a third request to the kernel; the third request is used to request the kernel to read data; the signal processing function writes the data to a second reserved area; the second reserved area is located in the user space, and the second reserved area is used to temporarily store data required to be accessed by the simulated instruction; the signal processing function modifies the exception instruction and / or the first context according to the address corresponding to the data in the second reserved area; the signal processing function writes the exception instruction to the first reserved area, including: the signal processing function writes the modified exception instruction to the first reserved area.
[0037] In one possible implementation method, the processing unit is used to parse the exception instruction and determine whether the exception instruction includes a write operation; if the exception instruction includes a write operation, and the memory access address corresponding to the write operation is located in the kernel space, the signal processing function presets an address in the second reserved area for the write operation, and modifies the exception instruction and / or the first context according to the preset address.
[0038] In a possible implementation method, the processing unit is used to parse the abnormal instruction and determine whether the abnormal instruction includes a write operation; if the abnormal instruction includes a write operation, and the memory address corresponding to the data to be written is located in the kernel space, the signal processing function obtains the data to be written from the second reserved area; the signal processing function sends a fourth request to the kernel; the fourth request is used to request the kernel to write the data to be written into the redirection space; the redirection space refers to an address space that has a mapping relationship with the kernel space.
[0039] In a possible implementation method, the processing unit is used to obtain a third context, where the third context is generated by executing the abnormal instruction in the first reserved area; and the signal processing function obtains the first simulation result from the third context.
[0040] In one possible implementation method, the processing unit is used to modify the third context and determine the fourth context; wherein the program counter in the fourth context points to the next instruction of the exception instruction; the signal processing function sends a fifth request to the kernel; and the fifth request is used to request the kernel to use the fourth context to resume program execution.
[0041] In a possible implementation method, the processing unit is used to obtain N instructions in the user program that are located after the exception instruction; N is an integer greater than or equal to 0; the signal processing function writes the exception instruction and the N instructions into the first reserved area; the signal processing function writes the first jump instruction into the first reserved area; the first jump instruction is located after the exception instruction and the N instructions.
[0042] In a possible implementation method, the processing unit is used to determine that the abnormal instruction is a complex instruction; the complex instruction includes a memory access operation and a calculation operation, multiple memory access operations or multiple calculation operations.
[0043] In a possible implementation method, the processing unit is used to determine that the abnormal instruction is a memory access instruction; the signal processing function parses the abnormal instruction and determines that the memory access address of the abnormal instruction is located in the kernel space; the signal processing function determines a first redirection address corresponding to the memory access address; the first redirection address is an address located in the redirection space; the signal processing function sends a second request to the kernel; the second request is used to request the kernel to perform data read and write operations according to the first redirection address; the signal processing function obtains the read and write results of the kernel, simulates the execution of the abnormal instruction, and obtains a second simulation result.
[0044] In a possible implementation method, the processing unit is used to set the first reserved area and the second reserved area when the user program is started; or,
[0045] When the kernel space is allocated to the user program for the first time, a first reserved area and a second reserved area are set.
[0046] In a possible implementation method, the processing unit is configured to set the permission corresponding to the first reserved area to be writable and executable when allocating the first reserved area; or,
[0047] When allocating the first reserved area, setting the permission corresponding to the first reserved area to be unreadable, unwritable, and unexecutable; when writing an abnormal instruction to the first reserved area, setting the permission corresponding to the first reserved area to be writable but not executable; when executing the first reserved area, setting the permission corresponding to the first reserved area to be unwritable but executable; after executing the first reserved area, setting the permission corresponding to the first reserved area to be unreadable, unwritable, and unexecutable; or,
[0048] When allocating the first reserved area, the permission corresponding to the first reserved area is set to be writable but not executable; when executing the first reserved area, the permission corresponding to the first reserved area is set to be non-writable but executable; after executing the first reserved area, the permission corresponding to the first reserved area is set to be writable but not executable.
[0049] In a possible implementation method, the processing unit is used to set the permission corresponding to the second reserved area to be readable and writable but not executable when allocating the second reserved area; or,
[0050] When allocating the second reserved area, the permissions corresponding to the second reserved area are set to be unreadable, unwritable, and unexecutable; when performing a read operation or a write operation on the second reserved area, the permissions corresponding to the second reserved area are set to be readable, writable, and unexecutable; when completing the read operation or the write operation on the second reserved area, the permissions corresponding to the second reserved area are set to be unreadable, unwritable, and unexecutable.
[0051] In a third aspect, an embodiment of the present application further provides a computing device, including:
[0052] A memory for storing program instructions;
[0053] The processor is used to call the program instructions stored in the memory, and execute any method of implementing the above-mentioned first aspect according to the obtained program instructions.
[0054] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, in which computer-readable instructions are stored. When a computer reads and executes the computer-readable instructions, any method of the above-mentioned first aspect is implemented.
[0055] In a fifth aspect, an embodiment of the present application provides a computer program product, comprising a computer program executable by a computer device, wherein when the program is run on the computer device, the computer device executes any method for implementing the above-mentioned first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] Figure 1 A flowchart of a method for processing abnormal instructions provided in an embodiment of the present application;
[0057] Figure 2 A flowchart of a method for processing abnormal instructions provided in an embodiment of the present application;
[0058] Figure 3 A flowchart of a method for processing abnormal instructions provided in an embodiment of the present application;
[0059] Figure 4A flowchart of a method for processing abnormal instructions provided in an embodiment of the present application;
[0060] Figure 5 A flowchart of a method for processing abnormal instructions provided in an embodiment of the present application;
[0061] Figure 6 A flowchart of a method for processing abnormal instructions provided in an embodiment of the present application;
[0062] Figure 7 A flowchart of a method for processing abnormal instructions provided in an embodiment of the present application;
[0063] Figure 8 A schematic diagram of the structure of a device for processing abnormal instructions provided in an embodiment of the present application;
[0064] Fig. 9 A schematic diagram of the structure of a device for processing abnormal instructions provided in an embodiment of the present application. DETAILED DESCRIPTION
[0065] The following is an explanation of some of the professional terms used in this application.
[0066] Kernel (also known as core): In computer science, it is a computer program used to manage data I / O (input and output) requests issued by software, translating these requests into data processing instructions and handing them over to the central processing unit (CPU) and other electronic components in the computer for processing. It is the most basic part of modern operating systems. It is a part of the software that provides secure access to computer hardware for many applications. This access is limited, and the kernel determines when a program can operate on a certain part of the hardware and for how long. Directly operating the hardware is very complicated. Therefore, the kernel usually provides a hardware abstraction method to complete these operations. With this, through inter-process communication mechanisms and system calls, the application process can indirectly control the required hardware resources (especially processors and IO devices). In summary, since user programs cannot be trusted arbitrarily, operations involving hardware must be taken over by the kernel. To perform such operations, user programs must interact with the abstraction layer provided by the kernel. Sometimes we say "kernel state", emphasizing a state of the CPU that has high privileges and is used to run the kernel. In practice, this word and "kernel" can basically be understood as synonyms. Most of the time, the operating system refers to the operating system kernel.
[0067] Exceptions, traps and interrupts: During the execution of a program, if an error that the CPU cannot handle is encountered, an exception will be caused. Exceptions will cause the CPU to fall into kernel mode and be handled by the kernel. After the exception is handled, it will return to user mode. Traps are sometimes used to refer to intentionally caused exceptions. For example, when a user-mode program needs to interact with the kernel, a system call (syscall) will be used to cause the CPU to fall into kernel mode so that the kernel can process requests from user-mode programs. After processing, it will return to user mode. From the perspective of the CPU, exceptions and traps are almost exactly the same. Interrupts are caused by external events (that is, not caused by programs executed on the CPU). These external events usually come from clocks or external hardware and are unpredictable. The arrival of an interrupt will cause the CPU to fall into kernel mode and be handled by the kernel (that is, to process requests from the outside). After processing, it will also return to user mode. Many technicians, especially x86 technicians, like to call all three "interrupts" because these three have many substantial similarities. This application mainly adopts the habit of MIPS. Exceptions and traps are almost synonymous, and interrupts specifically refer to those caused by external events.
[0068] User program: In contrast to the kernel, a program running in "user mode". This is what we usually call "process" (emphasizing that the program is running), "program", and "software". "User mode" emphasizes a state of the CPU. This state does not have privileges, and the operations that can be performed are limited. It is used to run user programs. In practice, this word and "user program" can basically be understood as synonyms.
[0069] Physical memory / virtual memory: Since user programs should not be able to access physical memory directly (otherwise they would be too privileged), it is necessary to establish a mapping that allows user programs to access physical memory indirectly. On the other hand, since the operating system needs to be able to run an unlimited number of processes, such a mapping is also required so that all programs can run in the same address space. After the mapping is established, the memory address used by the user program is called a virtual memory address or virtual address.
[0070] Address space: Whether it is virtual memory or physical memory, we need to use memory addresses to determine the specific location to be accessed. The width of the address determines the maximum accessible memory size. In fact, when we say that a CPU is "xx bits", we are referring to the width of its virtual address. The width of the physical address (actually the width of the address bus) is not necessarily the same as the width of the virtual address. For example, a 32-bit CPU may have a physical address greater than 32 bits in order to support larger physical memory (greater than 4GiB); and because the address space that can be encoded by 64 bits is too large, such a large memory cannot be achieved in the foreseeable future, so generally speaking, the width of the physical address of a 64-bit CPU will not exceed 50 bits (it can already encode 1PiB = 1024 TiB = 1048576 GiB of physical memory).
[0071] Kernel space / user space: Virtual memory is divided into two segments: kernel space and user space. Since both the kernel and user programs need memory to run, they both need to occupy a certain amount of virtual memory space. Therefore, it can be said that the kernel runs in kernel space and user programs run in user space. The kernel can access user space due to its privileges; accessing kernel space will cause an exception due to the lack of privileges. The size of the two spaces in virtual memory is usually architecture-specific. For the 32-bit MIPS instruction set architecture, the kernel space and user space are equally divided into 4GiB of 32-bit virtual memory space, of which the kernel space is divided into the upper 2GiB (0x80000000~0xFFFFFFFF) and the user space is divided into the lower 2GiB (0x0~0x7FFFFFFF), and they do not overlap. For x86, the kernel space occupies the upper 1GiB and the user space occupies the lower 3GiB. In the following, the mapping of kernel space is called kernel mapping, and the mapping of user space is called user mapping. There is usually only one kernel mapping, because only one kernel can run at a time. Each process has its own user mapping.
[0072] Memory paging: One of the most important technologies in virtual memory. Because the operating system can run countless processes, the total amount of memory used by these processes may be greater than the capacity of physical memory, so it is necessary to use external memory (usually hard disk) for swap (swap, also called paging file / page file page file or swap file swap file). Therefore, paging was born, and paging is usually implemented as a page table. The finest granularity of the page table is the page table entry (pagetable entry, PTE, referred to as page page). The memory size that can be managed by a page is usually 4KiB, which is also the finest granularity of memory management. The page table describes how the virtual address of a specific process should be mapped to the physical address. The operating system establishes separate page tables for different processes, and they are not shared (that is, the same user space address has inconsistent mappings in the page tables of different processes). The page table also describes whether a specific page is resident in memory. If a process accesses a virtual address in a page that does not reside in memory ("page fault"), a page exception (page fault, often called "page fault interrupt", this is an idiomatic expression, not only page faults can cause such exceptions, so this application does not use this translation that is easy to cause misunderstanding), causing the CPU to enter kernel state, and the kernel reads the pages stored in swap into memory, and then returns to user state to continue execution.
[0073] Page fault: refers to a user program accessing a virtual address that does not have a valid mapping. Page faults need to be corrected by the kernel. One of the situations is described above. Another situation is the first access after memory allocation. Not all page faults can be corrected by the kernel. For example, if the page where the virtual address accessed by the user program is not allocated at all, it is a memory misuse and should cause a segmentation fault.
[0074] Address translation: Since memory accesses must ultimately be implemented on physical memory, virtual addresses must be translated into physical addresses in some way. Address translation is actually parsing and querying the address mapping in the page table in some way. Address translation is the responsibility of the memory management unit MMU in the CPU, which parses and queries the page table independently or with the assistance of the kernel. The page table may specify permissions for the page (readable, writable, executable, etc.), and the MMU is also responsible for causing page exceptions when the user program performs operations that exceed the page permissions. If there is no corresponding address mapping in the page table, or the user program accesses the kernel space, it will also cause a page exception. However, it should be noted that in kernel mode, address translation can be performed normally when accessing kernel space.
[0075] Out of memory (OOM, also known as "memory overflow"): The maximum amount of memory that the operating system (kernel) can provide is the size of physical memory plus the size of swap. If they are all close to being allocated, there is very little memory available at this time, which makes the user program's memory request unable to be satisfied, and then it enters the OOM state. At this time, the kernel has no choice but to pick a process that occupies the most memory and kill it to relieve the OOM state. As mentioned earlier, the virtual memory space is divided into kernel space and user space. The maximum memory space that a user program can use depends on the size of the user space, such as 2GiB on 32-bit MIPS. If these 2GiB are all used up, then when the user program requests memory from the kernel again, even if the physical memory and swap are still sufficient, the kernel can no longer allocate memory to the program. At this time, it can only return an error to the process and let the user program handle it by itself (the user program may choose to exit or clean up the used memory by itself).
[0076] Segment fault: Segment fault is an abstraction provided by the kernel to user programs. The kernel sends segment faults as a signal to the process. User programs can choose to handle segment faults by themselves, but this is very difficult, so generally speaking, programs can only choose to exit immediately to avoid more serious consequences. Basically, except for page faults, page exceptions caused by other reasons are due to some kind of memory misusage, or in other words, they are software defects, so they should all lead to segment faults and eventually cause the program to exit.
[0077] Memory misuse: The so-called misuse refers to the user program's incorrect use of memory. In terms of results, memory misuse will lead to: MMU triggers a page exception -> the kernel handles the page exception -> the kernel believes that the page exception cannot be corrected -> a segmentation fault signal is sent to the program. In terms of the process, it is usually the user program: 1) accesses the kernel space; 2) accesses an unallocated page; 3) the operation performed does not match the page permissions.
[0078] Program Counter (PC): Also called Instruction Pointer (IP) in x86, it refers to a register in the CPU that stores the memory address (generally a virtual address) of the next instruction to be executed by the CPU. In other words, the instruction pointed to by the PC has not yet been executed, but is the one to be executed next. If no branches or jumps are encountered, the value of the PC will increase monotonically as the program is executed, that is, it will always point to the address of the next instruction of the current instruction; however, branches and jumps will change the value of the PC so that it does not point to the address of the next instruction of the current instruction, but to the destination address specified by the branch / jump instruction.
[0079] Exception Program Counter (EPC): This is a register defined by the MIPS instruction set, but it may have different names in other instruction sets; some instruction sets may not provide this register, but implement it by automatically reading and writing exception frames in the kernel stack during exception handling. This register stores the memory address of the instruction that caused the exception. In other words, the instruction pointed to by EPC is actually equivalent to not being executed, or, because the exception is caused, all states are automatically rolled back by the CPU to the state before this instruction was executed ("precise exception"). The existence of this register has two meanings: 1) The kernel often needs to return to the original position after handling an exception. The CPU reads this register and changes the PC when returning from the exception; 2) Sometimes the kernel needs to change the return position when handling an exception, such as trap-and-emulate. It often needs to skip an instruction when returning, so the kernel modifies the value of this register; 3) The kernel often needs to read the EPC to determine how to handle exceptions. Trap-and-emulate is one example. The kernel must read the EPC and then read the instruction pointed to by the EPC before it can further parse and simulate that instruction. Another example is, as mentioned earlier, memory misuse includes page permission mismatch. One of the situations is trying to jump to an address on a non-executable page. At this time, the EPC will actually point to the destination address after the jump. Therefore, the kernel only needs to determine whether the page where the address pointed to by the EPC is executable to know whether the memory is misused.
[0080] trap-and-emulate: "trap and emulate". When some instructions are executed, they will cause exceptions for various reasons and need to be handled by the kernel. Usually, these exceptions are not what we want to see. If they occur, it means that there is a defect in the program. Therefore, the final result of the kernel handling the exception is often destructive, such as killing the process. However, there is an exception, that is, we do not want to see this exception, but this exception is not caused by a defect in the program, but by some force majeure. In this case, we hope that the program can still run normally. At this time, the kernel needs to simulate the instruction that caused the exception, and the result of the simulation is the same as if it was executed correctly. A typical case is that for cost-saving considerations, the FPU (floating-point unit) is omitted in the CPU, so the floating-point operation instruction executed by the user program will cause an exception. At this time, we certainly hope that the kernel will simulate this floating-point operation instruction for it. Here, trap means that the kernel captures the exception, and emulate means that the kernel simulates the instruction that causes the exception when handling the exception. Trap-and-emulate is a simulation that is completely transparent to user mode, that is, the user program will not know whether an instruction has been trap-and-emulated, and does not need to make any modifications to benefit from it. Generally speaking, because the instruction that caused the exception has been simulated by the kernel, the kernel must modify the EPC before returning from the exception to point to the next instruction of the instruction that caused the exception (if the simulated instruction is a branch / jump instruction, it may also be changed to the jump destination address).
[0081] Context: When a program or program fragment is suddenly interrupted during execution, at least how much information needs to be saved to ensure that its execution can be fully restored in the future after the resources it occupies are used by the kernel or other programs or other fragments in the program. This minimum information that needs to be saved is the context. Therefore, the context can be understood as the background environment of program execution, which contains all the information required by the program at a specific moment.
[0082] Signal: used to notify the process of asynchronous events. The kernel can send signals to the process due to internal events to notify the process of an event. In addition to the basic notification function, signals can also transmit additional information. The process that receives the signal has different processing methods for various signals. One of the methods is to retain the system's default value for the processing of the signal. This default operation terminates the process for most signals. The process specifies the process's processing behavior for a certain signal through a specific system call (in other words, registering a signal handler for a certain signal), which will override the default processing behavior. In other words, as long as the process does not register a signalhandler for a certain signal, the kernel will use the default behavior to process the signal. The SIGSEGV signal is a segmentation error signal sent by the system to the program, indicating that some kind of memory misuse has occurred. The default action is to terminate the program. The SIGKILL signal is a signal that forces the program to be killed. No program can capture this signal (that is, a signal handler cannot be registered for it). The default action is program termination.
[0083] Figure 1 A flowchart of a method for processing an abnormal instruction provided in an embodiment of the present application, the method can be executed by a processing device for abnormal instructions, the processing device for abnormal instructions can be a terminal device or a module for a terminal device, or a server or a module for a server. The present application does not limit the execution subject of the method.
[0084] The method comprises the following steps:
[0085] Step 101: The signal processing function obtains a first context corresponding to when the user program runs to an abnormal instruction.
[0086] The signal processing function is registered by the user program and is used to replace the default processing method of the kernel for the abnormal instruction.
[0087] In a possible implementation method, the present application does not limit the type of abnormal instruction. For example, the abnormal instruction can be an instruction that causes a segmentation fault SIGSEGV, or an instruction that causes a floating point exception SIGFPE, as long as the abnormal signal caused by the abnormal instruction can be captured by the signal processing function. In the introduction of subsequent embodiments of the present application, the abnormal instruction is described as the instruction corresponding to the segmentation fault SIGSEGV, which will not be repeated here.
[0088] In a possible implementation method, before the above step 101, the user program registers the corresponding signal processing function to replace the kernel's default processing method for abnormal instructions. For example, the default processing method of the signal processing function corresponding to the segmentation fault SIGSEGV is to terminate the user program; the signal processing function corresponding to the segmentation fault SIGSEGV after registration can capture the first context and simulate the execution of abnormal execution.
[0089] In a possible implementation method, before the signal processing function obtains the first context corresponding to the user program when running to the exception instruction, it also includes: the kernel receives the page exception triggered by the user program, and determines that the type of the page exception is a segmentation fault; the kernel triggers the signal processing function, and sends the first context when the page exception occurs as a parameter to the signal processing function; the signal processing function obtains the memory access address that triggers the page exception from the first context, and determines that the memory access address is located in the kernel space. In other words, the segmentation fault of the present application is triggered by the user program accessing the kernel space. Of course, the segmentation fault can also be triggered by other reasons, such as the segmentation fault triggered by the page not being allocated or the segmentation fault triggered by the user program not meeting the page access rights. The present application does not limit the type of segmentation fault triggered. This solution can effectively handle the page exception caused by the user program accessing the kernel space.
[0090] In one possible implementation method, when the user space is exhausted, the kernel space can be allocated to the user program. This application does not limit the object of allocating kernel space. It can be that the kernel allocates kernel space to the user program; or it can be that the user program allocates kernel space to the user program.
[0091] Step 102: The signal processing function writes the exception instruction and the first jump instruction into the first reserved area.
[0092] Among them, the exception instruction is obtained based on the first context; the first reserved area is located in the user space, and is used to temporarily store and execute simulated instructions; the first jump instruction is located after the exception instruction, and is used to instruct to jump back to the signal processing function.
[0093] In a possible implementation method, the address of the exception instruction is obtained from the context, and the address is accessed to obtain the exception instruction. If the address is in the user space, the signal processing function accesses the address to obtain the exception instruction; if the address is in the kernel space, the kernel accesses the address to obtain the exception instruction.
[0094] In a possible implementation method, a separate area is allocated in the user space as a first reserved area, and the first reserved area is used to temporarily store and execute simulated instructions.
[0095] In a possible implementation method, after the abnormal instruction is written into the first reserved area, the first jump instruction is written into the first reserved area, and the position of the first jump instruction in the first reserved area is after the abnormal instruction.
[0096] In a possible implementation method, the first jump instruction includes an immediate jump instruction or an unconditional trap instruction. Of course, the first jump instruction can also be other types of instructions, and this application does not limit this, as long as it can jump back to the signal processing function according to the first jump instruction.
[0097] Step 103: The signal processing function modifies the first context and determines the second context.
[0098] The program counter in the second context points to the address of the first reserved area.
[0099] In a possible implementation method, the first context is modified to obtain a second context, and the program counter in the second context points to the address of the first reserved area, so that when the kernel uses the second context to resume program execution, the instructions in the first reserved area are executed.
[0100] Step 104: The signal processing function sends a first request to the kernel.
[0101] The first request is used to request the kernel to use the second context to resume program execution.
[0102] Step 105, after executing the abnormal instruction in the first reserved area and obtaining the first simulation result, return to the signal processing function according to the first jump instruction, and the signal processing function obtains the first simulation result.
[0103] In a possible implementation method, the abnormal instruction is executed in the first reserved area, and a first simulation result of the abnormal instruction is obtained, and then the first jump instruction in the first reserved area is executed, and the signal processing function is returned. The signal processing function obtains a third context, and the third context is generated by executing the abnormal instruction in the first reserved area; the signal processing function obtains the first simulation result from the third context. This solution can accurately and quickly obtain the first simulation result from the third context.
[0104] In a possible implementation method, after the signal processing function obtains the first simulation result, it also includes: the signal processing function modifies the third context and determines the fourth context; wherein the program counter in the fourth context points to the next instruction of the abnormal instruction; the signal processing function sends a fifth request to the kernel; the fifth request is used to request the kernel to use the fourth context to resume program execution. This solution can accurately and effectively execute the next instruction of the abnormal instruction and resume program execution after the abnormal instruction simulation execution is completed.
[0105] In the above scheme, on the one hand, a first reserved area is set in the user space, and abnormal instructions can be executed in the first reserved area, so that the user program can simulate the execution of abnormal instructions without causing the user program to terminate its operation due to the abnormal instructions; on the other hand, the signal processing function modifies the first context to obtain the second context, and the program counter in the second context points to the address of the first reserved area, so that the kernel can correctly jump to the first reserved area to execute the abnormal instruction according to the second context; on the other hand, the first jump instruction is written into the first reserved area, so that after the simulation execution of the abnormal instruction is completed, it can correctly jump back to the signal processing function, and enable the signal processing function to obtain the first simulation result.
[0106] In a possible implementation method, a region is separately allocated in the user space as a second reserved region, the second reserved region is located in the user space, and the second reserved region is used to temporarily store data required to be accessed by the simulated instruction;
[0107] In a possible implementation method, before the above step 102, it also includes: the signal processing function parses the abnormal instruction to determine whether the abnormal instruction includes a read operation; if the abnormal instruction includes a read operation, and the memory access address corresponding to the read operation is located in the kernel space, the signal processing function sends a third request to the kernel; the third request is used to request the kernel to read data; the signal processing function writes the data into a second reserved area; the signal processing function modifies the abnormal instruction and / or the first context according to the address corresponding to the data in the second reserved area. That is to say, before writing the abnormal instruction into the first reserved area, it is also necessary to determine whether the memory access address corresponding to the read operation in the abnormal instruction is located in the kernel space. If it is located in the kernel space, a third request is sent to the kernel to request the kernel to read data from the redirection space, and the read data is returned to the signal processing function, and the signal processing function writes the data into the second reserved area, and modifies the abnormal instruction and / or the first context according to the address of the second reserved area, such as modifying the address stored in the register used by the abnormal instruction to the address of the second reserved area. This solution sets a second reserved area in the user space, and the second reserved area is used to temporarily store data that needs to be accessed by the simulated instructions; since the user program lacks the privilege of accessing the kernel space, when the memory access address corresponding to the read operation is located in the kernel space, a third request can be sent to the kernel, requesting the kernel to read data from the redirected space and return the data to the signal processing function; after the signal processing function obtains the data, it writes the data to the second reserved area; then, when an abnormal instruction is executed in the first reserved area, the corresponding data can be obtained from the second reserved area to ensure the normal execution of the user program, thereby enabling the user program to simulate the execution of instructions accessing the kernel space.
[0108] In a possible implementation method, before the above step 102, it also includes: the signal processing function parses the abnormal instruction to determine whether the abnormal instruction includes a write operation; if the abnormal instruction includes a write operation, and the memory access address corresponding to the write operation is located in the kernel space, the signal processing function reserves an address in the second reserved area for the write operation, and modifies the abnormal instruction and / or the first context according to the address. That is to say, before writing the abnormal instruction to the first reserved area, it is also necessary to determine whether the abnormal instruction needs to write data to the redirection space. If data needs to be written to the redirection space, an address is reserved in the second reserved area for the write operation, and the abnormal instruction and / or the first context is modified according to the address, such as modifying the address stored in the register used in the abnormal instruction to the address of the second reserved area. This solution sets a second reserved area in the user space, and the second reserved area is used to temporarily store data written by the simulated instructions; since the user program lacks the privilege of accessing the kernel space, when the user program needs to write data to the redirected space, it can first be written to the second reserved area, and then retrieved from the second reserved area by the signal processing function; after the signal processing function obtains the data, it will also request the kernel to write the data to the redirected space in the subsequent steps; then, when the abnormal instruction is executed in the first reserved area, the data to be written can be written to the second reserved area to ensure the normal execution of the user program, thereby enabling the user program to simulate the execution of the instruction to access the redirected space.
[0109] In a possible implementation method, the signal processing function writes the modified exception instruction into a first reserved area.
[0110] In a possible implementation method, in the above step 105, after the signal processing function obtains the first simulation result, it also includes: the signal processing function parses the abnormal instruction to determine whether the abnormal instruction includes a write operation; if the abnormal instruction includes a write operation, and the memory address corresponding to the data to be written is located in the kernel space, the signal processing function obtains the data to be written from the second reserved area; the signal processing function sends a fourth request to the kernel; the fourth request is used to request the kernel to write data to the redirection space, and the redirection space refers to an address space that has a mapping relationship with the kernel space. Specifically, the redirection space refers to an address space that may or may not be located on the memory, and there is a certain mapping relationship between it and the address on the kernel space; through the mapping relationship, the memory access address of the abnormal instruction can be mapped to the redirection address.
[0111] That is to say, after obtaining the first simulation result, it is determined whether the data to be written corresponding to the first simulation result needs to be written into the redirection space. If it needs to be written, the signal processing function sends a fourth request to the kernel, requesting the kernel to write the data into the redirection space. This solution can accurately and effectively process the write operation of the memory access address located in the kernel space to the redirection space, and can enable the user program to simulate the execution of instructions to access the kernel space.
[0112] In one possible implementation method, after requesting the kernel to write data to the redirection space, the signal processing function modifies the third context and determines the fourth context; the signal processing function sends a fifth request to the kernel; the fifth request is used to request the kernel to use the fourth context to resume program execution.
[0113] The first reserved area and the second reserved area are explained below.
[0114] In a possible implementation method, when the user program is started, the first reserved area and the second reserved area are set; or, when the kernel space is allocated for the user program for the first time, the first reserved area and the second reserved area are set. The first reserved area and the second reserved area can be allocated by the runtime library or the memory allocation wrapper of the user program. There is no substantial difference between the first reserved area and the second reserved area and an ordinary user space memory allocation. As long as these two areas are not released, it is equivalent to retaining them. The only requirement is that before executing the abnormal instruction, these two areas exist and can at least meet the memory size and permissions required to simulate an abnormal instruction. This solution can improve the flexibility of allocating the first reserved area and the second reserved area.
[0115] In one possible implementation method, the first reserved area and the second reserved area can be dynamically set from the time the user program is started to the time the user program is exited. That is, the two areas can be dynamically expanded or reduced as needed (and may be accompanied by movement of positions), or the size may not change but only the position may be moved.
[0116] The first reserved area is used to temporarily store the simulated instructions when implementing the simulation execution of abnormal instructions. These instructions are executed in this area. This area is set because: 1) the redirection space cannot be directly accessed, and the user program has no right to access the real kernel space, so the instructions in the redirection space must be copied here before they can be executed; 2) regardless of whether the simulated instructions are in the user space or the non-user space, they need to return immediately after executing the instructions to be simulated, so a special area is needed to place the instructions for returning to the signalhandler after the instructions to be simulated. These instructions to be simulated and temporarily stored here can be an unmodified copy of the original instructions, or they may have been modified in the pre-processing stage (for example, for memory access instructions using immediate numbers, if you want to access the kernel space, you need to modify the immediate number so that the memory access address points to the first reserved area). The size of this area determines the maximum number of instructions that can be simulated at one time during the merge simulation. For example, if the size of the first reserved area, excluding the space required for placing instructions for returning to the signal handler, is 4092 bytes, then a maximum of 1023 instructions with a length of 4 bytes can be simulated at one time during the merge simulation (most RISC architectures use 4-byte fixed-length instructions). For variable-length instruction sets (typically, such as x86), since the length of each instruction is inconsistent, it is impossible to accurately predict the maximum number of instructions that can be simulated at one time during the merge simulation.
[0117] Since the first reserved area is in user space, and since the key to executing the simulation is to complete the execution in the first reserved area, the page corresponding to the area must have executable permissions before entering the first reserved area for execution; and copying (writing) instructions to this area requires write permissions. To achieve this goal, it is necessary to request the kernel to modify page permissions through system calls (such as the mprotect() system call can be used to modify the permissions of allocated pages, or bring page permission parameters when requesting the kernel to allocate memory such as mmap()). For example, this can be achieved in three ways:
[0118] 1. Set it to non-readable, writable and executable when or immediately after it is allocated. This way, there is no need to change its permissions later. This is convenient, requires fewer system calls, and has high performance. Of course, for some architectures that require executable to be readable, or writable to be readable, etc., set it to writable and executable when or immediately after it is allocated. This application still uses these architectures and will not be repeated later.
[0119] 2. Set it to unreadable, unwritable, and unexecutable when or immediately after it is allocated. Then, before using the first reserved area each time, set it to writable, write the corresponding instructions into it, and then set it to unreadable, unwritable (to prevent the simulated instructions from reading and writing to this area by mistake) and executable; after the instructions in the first reserved area are executed, restore it to unreadable, unwritable, and unexecutable. This is highly secure and can immediately detect any memory misuse.
[0120] 3. Set it to unreadable, writable, and unexecutable when or immediately after allocating it, and then set it to unreadable, unwritable, and executable after writing the corresponding instructions to the first reserved area. After the instructions in the first reserved area are executed, it is restored to unreadable, writable, and unexecutable. This is similar to the second method, but reduces the page permission setting once, and the performance is improved.
[0121] In a possible implementation method, after placing the instruction to be executed in the first reserved area, an instruction for immediately jumping back to the signal handler should also be placed immediately after the instruction to be executed. Such instructions should not change any context other than the PC (e.g., they cannot change the value in the register, etc.), for example: immediate jump instructions, unconditional trap instructions, etc. Immediate jump instructions will jump back to the signal handler without the assistance of the kernel. At this time, the signal handler needs to save the context for subsequent use. Unconditional trap instructions will trigger a trap, thereby falling into the kernel state, and the kernel will generate a signal (such as SIGTRAP). As long as the signal handler is registered for the corresponding signal, the context passed by the kernel can be obtained in the signal handler, and the signal handler does not need to save the context by itself. The former has higher performance, but the implementation is complex; the latter is simple to implement, but the overhead is large (because it involves switching between user mode and kernel mode).
[0122] The second reserved area temporarily stores the data located at the address in the kernel space that the simulated instruction needs to access when implementing the simulation execution of the abnormal instruction. This area is set because the kernel space cannot be directly accessed, so this area is used as a data transfer station. By modifying the context before implementing the simulation execution of the abnormal instruction, the memory access address of the instruction that needs to access the kernel space can be changed to the address in the second reserved area, so as to achieve the correct memory access and execution of the instruction. The size of this area determines the maximum number of instructions that can be simulated at one time during the merge simulation. For example: if the size of the second reserved area is 4096Byte, then at most 4096 instructions that read and write 1 Byte at different addresses can be simulated at one time during the merge simulation, or 1024 instructions that read and write 4 Byte at different addresses can be simulated at one time, or instructions that do not access memory can be simulated without limit. It should be noted that the "different addresses" need to be emphasized here because: if several instructions access the same address, they occupy the same position in the second reserved area, so the number of instructions accessing the same address can be unlimited (as long as the second reserved area is not completely occupied by other instructions to the point where there is no space left).
[0123] Since the second reserved area is in user space, and since it may be read and written to perform simulation. Therefore, if the second reserved area is needed for simulation, the page corresponding to the second reserved area must have readable and / or writable permissions before entering the second reserved area for execution. To achieve this goal, it is necessary to request the kernel to modify the page permissions through a system call (such as the mprotect() system call can be used to modify the permissions of the allocated page, or bring the page permission parameters when requesting the kernel to allocate memory such as mmap()). For example, this can be achieved in two ways:
[0124] 1. Set it to readable, writable and non-executable when or immediately after it is allocated, so that there is no need to change its permissions later. This is convenient, requires fewer system calls, and has high performance. If the user program jumps to the second reserved area for execution due to error or negligence, the permission of the second reserved area is non-executable at this time, and it can be identified that this is a memory misuse.
[0125] 2. Set it to unreadable, unwritable, and unexecutable when or immediately after it is allocated. Then, each time before using the second reserved area, set it to readable, writable, and unexecutable. Then write the data required for simulation execution into the second reserved area, and then enter the first reserved area to complete the execution of the instruction. Later, if necessary, after obtaining the required data from the second reserved area, restore it to unreadable, unwritable, and unexecutable. This is highly secure and can immediately detect any memory misuse of the second reserved area by the user program.
[0126] In a possible implementation method, the signal processing function obtains N instructions in the user program that are located after the abnormal instruction; N is an integer greater than or equal to 0; the signal processing function writes the abnormal instruction and the N instructions into the first reserved area; the signal processing function writes the first jump instruction into the first reserved area; the first jump instruction is located after the abnormal instruction and the N instructions. Among them, the N instructions are not necessarily abnormal instructions, but can also be normal instructions. For this method, this application refers to it as "merged simulation". This solution can simulate the execution of abnormal instructions and N instructions at one time, improve program execution efficiency, reduce unnecessary context switching and signal processing, and improve performance.
[0127] In the combined simulation, multiple instructions can be written into the first reserved area at one time, and then multiple instructions can be executed in the first reserved area at one time to obtain the simulation results. This method can greatly save the overhead of jumping back and forth and save some unnecessary intermediate state processing. However, the maximum number of instructions that can be simulated at one time by the combined simulation depends on the instruction type, the size of the first reserved area and the size of the second reserved area. For example: Assume that the size of the second reserved area is 2048Byte, and the first reserved area is 4092Byte after removing the space required for placing instructions for returning the signal handler. Assume that all instructions are fixed-length, and the instruction length is 4Byte. If there are already 512 instructions that read and write 4Bytes at different addresses, the second reserved area is completely used up, but there are still 2044Bytes available in the first reserved area; at this time, instructions whose memory access address is not the same as the memory access address of any instruction that already exists in the second reserved area cannot be added, but instructions that do not access memory or instructions whose memory access address is the same as the memory access address of any existing instruction can continue to be added until the first reserved area is also used up. If there are already 1023 instructions to read or write 1 Byte at different addresses, the first reserved area is completely used up. At this time, there are still 1024Bytes available in the second reserved area, but it is impossible to add more instructions. If there are already 1023 non-memory access instructions, the first reserved area is completely used up. At this time, although the second reserved area is not used, it is impossible to add more instructions.
[0128] In a possible implementation method, the signal processing function obtains the next instruction of the abnormal instruction and takes the next instruction as the abnormal instruction; continues to execute and writes the abnormal instruction and the first jump instruction into the first reserved area until M instructions are completed, where M is an integer greater than or equal to 1. The M instructions are not necessarily abnormal instructions, but may also be normal instructions. This method is referred to as "continuous simulation" in this application.
[0129] Continuous simulation means that after simulating an abnormal instruction, it does not necessarily return to normal execution, but can continue to fetch an instruction and simulate its execution.
[0130] For the exception instruction to be simulated, if the exception instruction itself is located in the kernel space, based on the fact that most instructions are executed sequentially except for jump (branch) instructions, after a single exception instruction is simulated, the next instruction to be executed is very likely to be located in the kernel space, so the next instruction to be executed is also likely to be simulated. Therefore, after simulating an instruction, it is possible not to return to normal execution, but to continue to fetch the next instruction of this instruction (if it is a jump instruction, it is the instruction at the jump target) to continue the simulation execution. The number of instructions simulated each time can be unlimited, and it can be repeated until an instruction that no longer needs to be simulated is encountered. This is different from trap-and-emulate. Since trap-and-emulate runs in kernel mode, it cannot simulate instructions indefinitely each time it runs, otherwise staying in kernel mode for a long time will cause the system to pause. In this application, the signal handler is located in user mode, and it can continue to simulate indefinitely without worrying about causing the entire system to pause.
[0131] For the abnormal instruction to be simulated, if the simulated instruction is a memory access instruction that accesses the kernel space, based on the fact that each memory access often does not only access a single address, but multiple addresses (often adjacent) are accessed. For example, the data at several addresses are taken out and put into registers, and after calculation, they are put back to these addresses. It can be seen that memory access instructions often appear in clusters, and since the addresses accessed by clustered memory access instructions are likely to be adjacent, if one of the memory access instructions in this cluster is to be simulated, the rest are likely to be simulated.
[0132] Continuous simulation can reduce the situation where exceptions caused by accessing kernel space are triggered immediately after returning to normal execution of the user program, thereby reducing unnecessary context switching and signal processing and improving performance.
[0133] Figure 2 A method for processing abnormal instructions provided in an embodiment of the present application is a specific implementation process of steps 101 to 105 above, and the method includes the following steps:
[0134] Step 201: The signal processing function obtains a first context corresponding to when the user program runs to an abnormal instruction.
[0135] Step 202: The signal processing function fetches an exception instruction from the first context.
[0136] Step 203: The signal processing function parses the abnormal instruction and determines whether the abnormal instruction includes a read operation.
[0137] In a possible implementation method, if the abnormal instruction does not include a read operation, the abnormal instruction is written into the first reserved area, that is, step 208 is executed; if the abnormal instruction includes a read operation, step 204 is executed.
[0138] Step 204: The signal processing function determines whether the memory access address corresponding to the read operation is located in the kernel space.
[0139] In a possible implementation method, if the memory access address corresponding to the read operation is not located in the kernel space, the abnormal instruction is written into the first reserved area, that is, step 208 is executed; if the memory access address corresponding to the read operation is located in the kernel space, step 205 is executed.
[0140] Step 205: The signal processing function sends a third request to the kernel.
[0141] In a possible implementation method, the signal processing function sends a third request to the kernel, requesting the kernel to read data located in the kernel space; after the kernel reads the data from the kernel space, it returns the data to the signal processing function.
[0142] Step 206: The signal processing function obtains the data sent by the kernel and writes the data into the second reserved area.
[0143] Step 207: The signal processing function modifies the abnormal instruction and / or the first context.
[0144] In one possible implementation method, if the memory access address is determined by an immediate value, the exception instruction is modified so that the address that the instruction needs to access is located in the second reserved area; if the memory access address is determined by a register, the first context is modified, that is, the data corresponding to the register is modified so that the data stored in the register is related to the address corresponding to the second reserved area.
[0145] Step 208: The signal processing function writes the exception instruction and the first jump instruction into the first reserved area.
[0146] In a possible implementation method, if the above steps modify the exception instruction and / or the first context, the modified exception instruction is written into the first reserved area; and then the first jump instruction is written into the first reserved area; if the above steps do not modify the exception instruction and the first context, the original exception instruction is written into the first reserved area; and then the first jump instruction is written into the first reserved area.
[0147] Step 209: The signal processing function modifies the first context and determines the second context.
[0148] The program counter in the second context points to the address of the first reserved area.
[0149] Step 210: The signal processing function requests the kernel to resume execution of the program using the second context.
[0150] In one possible implementation, Figure 3 The following exemplary steps are shown after the kernel receives a request to restore the program using the second context sent by the signal processing function. The steps include:
[0151] Step 301 , the kernel resumes program execution using a second context.
[0152] Step 302: The user program executes an abnormal instruction in the first reserved area to obtain a simulation result.
[0153] Step 303: Due to the effect of the execution of the abnormal instruction, the context is changed and is recorded as the third context.
[0154] Step 304: execute the first jump instruction to jump to the signal processing function.
[0155] In a possible implementation method, if the first jump instruction is an immediate jump instruction, the instruction directly jumps to the signal processing function, and the signal processing function retains the third context.
[0156] In a possible implementation method, the signal processing function is returned through signal processing. For example, the signal processing function can be jumped to by an unconditional trap instruction. Of course, the signal processing function can also be jumped to by other instructions, which is not limited in this application.
[0157] In a possible implementation method, the specific process of jumping to the signal processing function through the unconditional trap instruction is as follows: executing the unconditional trap instruction to trigger an exception, after the kernel receives the exception signal, retains the context of the user program when the exception is triggered, that is, the third context; judging the exception type, if it is determined that the exception is a trap (SIGTRAP) caused by the unconditional trap instruction, the third context is passed to the signal handler corresponding to SIGTRAP, and the signal processing function obtains the third context from the passed-in parameters.
[0158] In a possible implementation method, before the user program is executed or when the kernel space is allocated for the user program, a signal handler corresponding to SIGTRAP is registered to override the default processing method of SIGTRAP.
[0159] Step 305: The signal processing function obtains the simulation result from the third context.
[0160] In one possible implementation, Figure 4The steps of the signal processing function after obtaining the simulation results are shown as follows:
[0161] Step 401: The signal processing function parses the abnormal instruction and determines whether the abnormal instruction includes a write operation.
[0162] In a possible implementation method, if the abnormal instruction does not include a write operation, step 405 is executed; if the abnormal instruction includes a write operation, step 402 is executed.
[0163] Step 402: The signal processing function determines whether to write data into the kernel space.
[0164] In a possible implementation method, if the abnormal instruction does not need to write data to the kernel space, step 405 is executed; if data needs to be written to the kernel space, step 403 is executed.
[0165] Step 403: The signal processing function obtains data written after the abnormal instruction is executed from the second reserved area.
[0166] Step 404: The signal processing function sends a fourth request to the kernel, requesting the kernel to write data into the redirection space.
[0167] Step 405: The signal processing function modifies the third context and determines a fourth context.
[0168] The program counter in the fourth context points to the next instruction of the abnormal instruction.
[0169] In a possible implementation method, since in the aforementioned Figure 1 In the step of , the first context may be modified to change the memory access address of the abnormal instruction, and the third context may inherit such modifications at this time; in order to prevent these modifications from affecting the subsequent normal execution of the program, these modifications are rolled back in the fourth context.
[0170] Step 406: The signal processing function requests the kernel to resume program execution according to the fourth context.
[0171] In a possible implementation method, after obtaining the simulation result, the result to be simulated is written into the redirection space.
[0172] The following are some specific examples to explain how to handle exception instructions.
[0173] (1) If the exception instruction is ADDU $1, $2, $3, this instruction is expressed in pseudo code as $1=$2+$3. Then the second reserved area is not needed, and the exception instruction and register data do not need to be modified. After writing the instruction to the first reserved area in the user space, modify context A (corresponding to the first context) to obtain context A1 (corresponding to the second context), where PC is modified to the starting address of the first reserved area; use context A1 to resume program execution (equivalent to jumping to the first reserved area for execution); after the instruction is executed, based on the effect of the execution of the instruction, context A1 has changed and is recorded as A2 (corresponding to the third context); since the first reserved area includes the first jump instruction, the first jump instruction will immediately return to the signal handler after execution, and the signal handler can extract context A2 and modify context A2 to obtain context D (corresponding to the fourth context), where PC is modified to the next instruction of the instruction.
[0174] (2) If the exception instruction is LW $1, 8($2), this instruction means: take the address y from register $2, add 8 to it to get y+8, read a word (word, 32 bits, or 4 bytes) z from address y+8, and write z to register $1. Then take the value y of register $2 from context A, and then read a word z from address y+8. If address y+8 is in kernel space, read z using simulated memory access, write z to the second reserved area (address m), and modify context A (corresponding to the first context) to get A1, where the value of $2 is modified to m-8; if not, the second reserved area does not need to be used and the context does not need to be modified temporarily. After writing the instruction to the first reserved area in the user space, the context A1 (if the address y+8 is in the kernel space) or A (if the address y+8 is not in the kernel space) is further modified and still recorded as A1 (corresponding to the second context), wherein the PC is modified to the starting address of the first reserved area; the program execution is resumed using the context A1 (equivalent to jumping to the first reserved area for execution); after the instruction is executed, based on the effect of the execution of the instruction, the context A1 has been changed and recorded as A2 (corresponding to the third context); since the first reserved area includes the first jump instruction, the first jump instruction will immediately return to the signal handler after execution, and the signal handler can thereby extract the context A2, modify the context A2 to obtain the context D (corresponding to the fourth context), wherein the value of $2 is changed back to y, and the PC is modified to the next instruction of the instruction.
[0175] (3) If the exception instruction is SB $1, -16($2), this instruction means: take the address u from register $2, subtract 16 from it to get u-16, where u-16 is in the kernel space, and intercept the value v in register $1 into a byte (8 bits) and write it to address u-16. Then take the value v of register $1 and the value u of register $2 from context A (corresponding to the first context). Assume that the address of the second reserved area is m. If the address u-16 is in the kernel space, the context A will be modified to get A1, where the value of $2 is modified to m+16; if not, the second reserved area does not need to be used and the context does not need to be modified temporarily. After writing the instruction to the first reserved area in the user space, the context A1 (if the address u-16 is in the kernel space) or A (if the address u-16 is not in the kernel space) is further modified and still recorded as A1 (corresponding to the second context), wherein the PC is modified to the starting address of the first reserved area; the program execution is resumed using the context A1 (equivalent to jumping to the first reserved area for execution); after the instruction is executed, the context A1 has been changed based on the effect of the execution of the instruction, and is recorded as A2 (corresponding to the third context); since the first reserved area includes the first jump instruction, the first jump instruction will immediately return to the signal handler after execution, and the signal handler can extract the context A2, modify the context A2 to obtain the context D (corresponding to the fourth context), wherein the value of $2 is changed back to u, and the PC is modified to the next instruction of the instruction. Moreover, if the address u-16 is in the kernel space, a byte at the address m is read out and then written to the address u-16 using simulated memory access.
[0176] In a possible implementation method, before the signal processing function writes the exception instruction and the first jump instruction into the first reserved area, it also includes: determining that the exception instruction is a complex instruction; the complex instruction includes a memory access operation and a calculation operation, multiple memory access operations or multiple calculation operations.
[0177] For the RISC (Reduced Instruction Set Computer) architecture, the memory access instructions of the RISC architecture are always responsible for one-way memory access (that is, a memory access instruction either only completes memory reading or only completes memory writing, and cannot complete both at one time), and do not carry any calculation operations on the target data of the memory access (the data read or the data to be written).
[0178] For instructions of CISC (Complex Instruction Set Computer) architecture, it can complete the operation of data at a certain memory address in one instruction (which is equivalent to completing all three steps in one instruction: reading the data at the address, performing the corresponding operation, and writing the operation result to the address). In other words, the instructions of CISC architecture are more complex than those of RISC architecture.
[0179] That is to say, the above-mentioned method for handling abnormal instructions is more suitable for instructions of CISC (complex instruction set computer) architecture, and for instructions of RISC (reduced instruction set computer) architecture, this application provides another implementation method, which can improve the efficiency of simulation execution. Of course, although this method is more suitable for instructions of RISC (reduced instruction set computer) architecture, it can also be used for instructions of CISC (complex instruction set computer) architecture, and this application does not limit this.
[0180] This implementation method is as follows Figure 5 As shown, the following steps are included:
[0181] Step 501: The signal processing function determines that the abnormal instruction is a memory access instruction.
[0182] Step 502: The signal processing function parses the abnormal instruction and determines that the memory access address of the abnormal instruction is located in the kernel space.
[0183] Step 503: The signal processing function determines a first redirection address corresponding to the memory access address.
[0184] The first redirection address refers to an address in the redirection space.
[0185] Step 504: The signal processing function sends a second request to the kernel.
[0186] The second request is used to request the kernel to perform data read and write operations according to the first redirection address.
[0187] Step 505: The signal processing function obtains the read and write results of the kernel, simulates the execution of the abnormal instruction, and obtains a second simulation result.
[0188] In a possible implementation method, if the above memory access instruction is to read data from kernel space, the method includes the following steps: Figure 6 shown.
[0189] Step 601: The signal processing function determines that the abnormal instruction is a memory access instruction.
[0190] Step 602: The signal processing function determines whether the memory access instruction needs to read data from the kernel space.
[0191] In a possible implementation method, the memory access instruction does not need to read data from the kernel space, that is, the memory access instruction may need to read data from the user space or the register, that is, execute step 606.
[0192] In a possible implementation method, if the memory access instruction needs to read data from the kernel space, step 603 is executed.
[0193] Step 603: The signal processing function determines a first redirection address corresponding to the memory access address.
[0194] In a possible implementation method, the memory access instruction needs to read data from the kernel space, and the corresponding redirection address is mapped according to the virtual address corresponding to the memory access instruction; then the kernel is requested to read the data corresponding to the first redirection address through a system call.
[0195] Step 604: The signal processing function sends a second request to the kernel, requesting the kernel to read data corresponding to the first redirection address.
[0196] Step 605: The kernel sends the data corresponding to the first redirection address to the signal processing function.
[0197] Step 606: The signal processing function reads corresponding data from the user space and / or registers.
[0198] In one possible implementation method, if the memory access instruction is to read data in the user space, the corresponding data is read from the user space address that the memory access instruction needs to read; if the memory access instruction is to read data in the register, the data corresponding to the register that the memory access instruction needs to read is read from the first context.
[0199] In a possible implementation method, the memory access instruction may need to read data from the kernel space and also need to read corresponding data from the user space and / or registers. Therefore, after determining in step 602 that data needs to be read from the kernel space, step 606 may also be executed.
[0200] In a possible implementation method, the memory access instruction may not need to read data from the user space and / or registers, so step 606 does not need to actually read data.
[0201] Step 607: The signal processing function directly simulates and executes the memory access instruction based on the read data and the behavior of the memory access instruction to obtain a second simulation result.
[0202] In a possible implementation method, the signal processing function modifies the first context, modifies the PC to point to the next instruction of the abnormal instruction or the jump target of the jump instruction, and sends a request to the kernel to enable the kernel to resume program execution according to the modified first context.
[0203] In a possible implementation method, if the above memory access instruction needs to write data to the kernel space, the method includes the following steps: Figure 7 Show.
[0204] Step 701: The signal processing function determines that the abnormal instruction is a memory access instruction.
[0205] Step 702: The signal processing function determines whether the memory access instruction needs to write data into the kernel space.
[0206] In a possible implementation method, the memory access instruction does not need to write data to the kernel space, that is, the memory access instruction may need to write data to the user space or register, that is, execute step 705.
[0207] In a possible implementation method, if the memory access instruction needs to write data into the kernel space, step 703 is executed.
[0208] Step 703: The signal processing function determines a first redirection address corresponding to the memory access address.
[0209] In a possible implementation method, if the memory access instruction needs to write data to the kernel space, the corresponding redirection address is mapped according to the virtual address corresponding to the memory access instruction; then the kernel is requested to write data to the first redirection address through a system call.
[0210] Step 704: The signal processing function sends a second request to the kernel, requesting the kernel to write data to the first redirection address.
[0211] Step 705: The signal processing function writes corresponding data to the user space and / or register.
[0212] In a possible implementation method, if the memory access instruction needs to write data to the user space, the corresponding data is written to the user space; if the memory access instruction needs to write data to the register, the data corresponding to the target register in the first context is modified.
[0213] In a possible implementation method, the second simulation result is written into kernel space or user space or register.
[0214] In a possible implementation method, the memory access instruction may not need to write data to the user space and / or register, so step 705 does not need to actually write data.
[0215] Step 706: The signal processing function modifies the first context.
[0216] In a possible implementation method, the signal processing function modifies the first context, modifies the PC to point to the next instruction of the abnormal instruction or the jump target of the jump instruction, and sends a request to the kernel to enable the kernel to resume program execution according to the modified first context.
[0217] In the above scheme, if the abnormal instruction is a memory access instruction, the abnormal instruction may not be written into the first reserved area, but the kernel performs a read and write operation on the data, and then the signal processing function directly simulates the execution of the abnormal instruction. This scheme can effectively process memory access instructions and improve the execution efficiency of user programs.
[0218] Several specific examples are given below to explain how to process abnormal instructions according to steps 501 to 505.
[0219] (1) If the exception instruction is ADDU $1, $2, $3, this instruction is expressed in pseudo code as $1=$2+$3, then the exception instruction is simulated and the simulation result is: the values of registers $2 and $3 are taken from context A (corresponding to the first context), and the value x is obtained after adding them; context A is modified to obtain context D (corresponding to the modified first context), where $1 is modified to x and PC is modified to the next instruction of the instruction.
[0220] (2) If the exception instruction is LW $1, 8($2), this instruction means: take the address y from register $2, add 8 to it to get y+8, read a word (word, 32 bits, or 4 bytes) z from address y+8, and write z to register $1, then simulate the execution of the exception instruction, and obtain the simulation result: take the value y of register $2 from context A (corresponding to the first context), and then read a word z from address y+8 (if address y+8 is in the kernel space, request the kernel to simulate memory access; if not, access memory normally); modify context A to obtain context D (corresponding to the modified first context), where $1 is modified to z and PC is modified to the next instruction of the exception instruction.
[0221] (3) If the exception instruction is SB $1, -16 ($2), this instruction means: take the address u from register $2, subtract 16 from it to get u-16, where u-16 is located in the kernel space, and intercept the value v in register $1 into a byte (byte, 8 bits) and write it to address u-16. Then, the exception instruction is simulated and the simulation result is: take the value v of register $1 and the value u of register $2 from context A (corresponding to the first context); modify context A to get context D (corresponding to the modified first context), where PC is modified to the next instruction of the exception instruction, and intercept v into a byte and request the kernel to simulate memory access and write to address u-16.
[0222] (4) If the exception instruction is JR $31, this instruction indicates that address w is taken from register $31 and execution is continued at address w. Then, the exception instruction is simulated and the simulation result is: the value w of register $31 is taken from context A (corresponding to the first context); context A is modified to obtain context D (corresponding to the modified first context), where PC is modified to w.
[0223] Based on the same technical concept, Figure 8 An apparatus 800 for processing an abnormal instruction provided by an embodiment of the present application is exemplarily shown, comprising: an acquisition unit 801 and a processing unit 802. The acquisition unit 801 is used to acquire a first context corresponding to a user program when running to an abnormal instruction; the signal processing function is registered by the user program and is used to replace the default processing method of the kernel for the abnormal instruction; the processing unit 802 is used to write the abnormal instruction and the first jump instruction into a first reserved area; the abnormal instruction is acquired based on the first context; the first reserved area is located in the user space and is used to temporarily store and execute simulated instructions; the first jump instruction is located after the abnormal instruction and is used to indicate a jump back to the signal processing function; the signal processing function modifies the first context and determines a second context; wherein the program counter in the second context points to the address of the first reserved area; the signal processing function sends a first request to the kernel; the first request is used to request the kernel to resume program execution using the second context; after executing the abnormal instruction in the first reserved area and obtaining the first simulation result, the signal processing function is returned according to the first jump instruction, and the signal processing function obtains the first simulation result.
[0224] In a possible implementation method, the processing unit 802 is used to receive the page exception triggered by the user program and determine that the type of the page exception is a segmentation fault; the kernel triggers the signal processing function and sends the first context when the page exception occurs as a parameter to the signal processing function; the signal processing function obtains the memory access address that triggers the page exception from the first context, and determines that the memory access address is located in the kernel space.
[0225] In a possible implementation method, the processing unit 802 is used to parse the abnormal instruction and determine whether the abnormal instruction includes a read operation; if the abnormal instruction includes a read operation, and the memory access address corresponding to the read operation is located in the kernel space, the signal processing function sends a third request to the kernel; the third request is used to request the kernel to read data; the signal processing function writes the data to a second reserved area; the second reserved area is located in the user space, and the second reserved area is used to temporarily store data required to be accessed by the simulated instruction; the signal processing function modifies the abnormal instruction and / or the first context according to the address corresponding to the data in the second reserved area; the signal processing function writes the abnormal instruction to the first reserved area, including: the signal processing function writes the modified abnormal instruction to the first reserved area.
[0226] In one possible implementation method, the processing unit 802 is used to parse the exception instruction and determine whether the exception instruction includes a write operation; if the exception instruction includes a write operation, and the memory access address corresponding to the write operation is located in the kernel space, the signal processing function presets an address in the second reserved area for the write operation, and modifies the exception instruction and / or the first context according to the preset address.
[0227] In a possible implementation method, the processing unit 802 is used to parse the abnormal instruction and determine whether the abnormal instruction includes a write operation; if the abnormal instruction includes a write operation, and the memory address corresponding to the data to be written is located in the kernel space, the signal processing function obtains the data to be written from the second reserved area; the signal processing function sends a fourth request to the kernel; the fourth request is used to request the kernel to write the data to be written into the redirection space; the redirection space refers to an address space that has a mapping relationship with the kernel space.
[0228] In a possible implementation method, the processing unit 802 is used to obtain a third context, where the third context is generated by executing the abnormal instruction in the first reserved area; and the signal processing function obtains the first simulation result from the third context.
[0229] In a possible implementation method, the processing unit 802 is used to modify the third context and determine the fourth context; wherein the program counter in the fourth context points to the next instruction of the abnormal instruction; the signal processing function sends a fifth request to the kernel; the fifth request is used to request the kernel to use the fourth context to resume program execution.
[0230] In a possible implementation method, the processing unit 802 is used to obtain N instructions in the user program that are located after the exception instruction; N is an integer greater than or equal to 0; the signal processing function writes the exception instruction and the N instructions into the first reserved area; the signal processing function writes the first jump instruction into the first reserved area; the first jump instruction is located after the exception instruction and the N instructions.
[0231] In a possible implementation method, the processing unit 802 is used to determine that the abnormal instruction is a complex instruction; the complex instruction includes a memory access operation and a calculation operation, multiple memory access operations or multiple calculation operations.
[0232] In a possible implementation method, the processing unit 802 is used to determine that the abnormal instruction is a memory access instruction; the signal processing function parses the abnormal instruction and determines that the memory access address of the abnormal instruction is located in the kernel space; the signal processing function determines a first redirection address corresponding to the memory access address; the first redirection address is an address located in the redirection space; the signal processing function sends a second request to the kernel; the second request is used to request the kernel to perform data read and write operations according to the first redirection address; the signal processing function obtains the read and write results of the kernel, simulates the execution of the abnormal instruction, and obtains a second simulation result.
[0233] In a possible implementation method, the processing unit 802 is used to set the first reserved area and the second reserved area when the user program is started; or,
[0234] When the kernel space is allocated to the user program for the first time, a first reserved area and a second reserved area are set.
[0235] In a possible implementation method, the processing unit 802 is configured to set the permission corresponding to the first reserved area to be writable and executable when allocating the first reserved area; or,
[0236] When allocating the first reserved area, setting the permission corresponding to the first reserved area to be unreadable, unwritable, and unexecutable; when writing an abnormal instruction to the first reserved area, setting the permission corresponding to the first reserved area to be writable but not executable; when executing the first reserved area, setting the permission corresponding to the first reserved area to be unwritable but executable; after executing the first reserved area, setting the permission corresponding to the first reserved area to be unreadable, unwritable, and unexecutable; or,
[0237] When allocating the first reserved area, the permission corresponding to the first reserved area is set to be writable but not executable; when executing the first reserved area, the permission corresponding to the first reserved area is set to be non-writable but executable; after executing the first reserved area, the permission corresponding to the first reserved area is set to be writable but not executable.
[0238] In a possible implementation method, the processing unit 802 is used to set the permission corresponding to the second reserved area to be readable and writable but not executable when allocating the second reserved area; or,
[0239] When allocating the second reserved area, the permissions corresponding to the second reserved area are set to be unreadable, unwritable, and unexecutable; when performing a read operation or a write operation on the second reserved area, the permissions corresponding to the second reserved area are set to be readable, writable, and unexecutable; when completing the read operation or the write operation on the second reserved area, the permissions corresponding to the second reserved area are set to be unreadable, unwritable, and unexecutable.
[0240] Based on the same technical concept, the embodiment of the present application provides a device 900 for processing abnormal instructions, and the device 900 for processing abnormal instructions may be a computing device, for example. Fig. 9 As shown, an abnormal instruction processing device 900 includes at least one processor 901 and a memory 902 connected to the at least one processor. The specific connection medium between the processor 901 and the memory 902 is not limited in the embodiment of the present application. Fig. 9 For example, the processor 901 and the memory 902 are connected via a bus. The bus can be divided into an address bus, a data bus, a control bus, etc.
[0241] In an embodiment of the present application, the memory 902 stores instructions that can be executed by at least one processor 901. The at least one processor 901 can execute the above-mentioned method for processing abnormal instructions by executing the instructions stored in the memory 902.
[0242] Among them, the processor 901 is a control center of the processing device 900 for abnormal instructions, and can use various interfaces and lines to connect various parts of the computer device, and perform resource settings by running or executing instructions stored in the memory 902 and calling data stored in the memory 902. Optionally, the processor 901 may include one or more determination units, and the processor 901 may integrate an application processor and a modem processor, wherein the application processor mainly processes an operating system, a user interface, and an application program, and the modem processor mainly processes wireless communication. It is understandable that the above-mentioned modem processor may not be integrated into the processor 901. In some embodiments, the processor 901 and the memory 902 may be implemented on the same chip, and in some embodiments, they may also be implemented separately on independent chips.
[0243] The processor 901 may be a general-purpose processor, such as a central processing unit (CPU), a digital signal processor, an application-specific integrated circuit (ASIC), a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component, and may implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of the present application. A general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application may be directly embodied as being executed by a hardware processor, or may be executed by a combination of hardware and software modules in the processor.
[0244] The memory 902 is a non-volatile computer-readable storage medium that can be used to store non-volatile software programs, non-volatile computer executable programs and modules. The memory 902 may include at least one type of storage medium, such as a flash memory, a hard disk, a multimedia card, a card-type memory, a random access memory (Random Access Memory, RAM), a static random access memory (Static Random Access Memory, SRAM), a programmable read-only memory (Programmable Read Only Memory, PROM), a read-only memory (Read Only Memory, ROM), an electrically erasable programmable read-only memory (Electrically Erasable Programmable Read-Only Memory, EEPROM), a magnetic memory, a disk, an optical disk, and the like. The memory 902 is any other medium that can be used to carry or store the desired program code in the form of an instruction or data structure and can be accessed by a computer, but is not limited thereto. The memory 902 in the embodiment of the present application can also be a circuit or any other device that can realize a storage function, for storing program instructions and / or data.
[0245] An embodiment of the present application also provides a computer-readable storage medium, which stores a computer-executable program. The computer-executable program is used to enable a computer to execute a method for processing an abnormal instruction listed in any of the above methods.
[0246] An embodiment of the present application provides a computer program product, including a computer program executable by a computer device. When the program is run on the computer device, the computer device executes a method for processing abnormal instructions listed in any of the above-mentioned ways.
[0247] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0248] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0249] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0250] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0251] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A method for processing abnormal instructions, It is characterized in that include: Setting a first reserved area and a second reserved area; The first reserved area is located in the user space and is used for temporarily storing and executing simulated instructions; The second reserved area is located in the user space, and temporarily stores data at an address in the kernel space that is required to be accessed by the simulated instruction when simulating the execution of the abnormal instruction; The signal processing function obtains the first context corresponding to the user program when it runs to the abnormal instruction; the signal processing function is registered by the user program and is used to replace the default processing method of the kernel for the abnormal instruction; The signal processing function writes the exception instruction and the first jump instruction into the first reserved area; The exception instruction is obtained based on the first context; the first jump instruction is located after the exception instruction and is used to instruct to jump back to the signal processing function; The signal processing function modifies the first context and determines a second context; wherein the program counter in the second context points to the address of the first reserved area; The signal processing function sends a first request to the kernel; the first request is used to request the kernel to use the second context to resume execution of the program; After the exception instruction is executed in the first reserved area and the first simulation result is obtained, the signal processing function is returned according to the first jump instruction, and the signal processing function obtains the first simulation result.
2. The method according to claim 1, It is characterized in that Before the signal processing function obtains the first context corresponding to the user program when running to the abnormal instruction, it also includes: The kernel receives the page exception triggered by the user program, and determines that the type of the page exception is a segment fault; The kernel triggers the signal processing function and sends the first context when the page exception occurs as a parameter to the signal processing function; The signal processing function obtains the memory access address that causes the page exception from the first context, and determines that the memory access address is located in the kernel space.
3. The method according to claim 1, It is characterized in that Before the signal processing function writes the exception instruction and the first jump instruction into the first reserved area, the signal processing function further includes: The signal processing function parses the abnormal instruction to determine whether the abnormal instruction includes a read operation; If the abnormal instruction includes a read operation, and the memory access address corresponding to the read operation is located in the kernel space, the signal processing function sends a third request to the kernel; the third request is used to request the kernel to read data; The signal processing function writes the data into the second reserved area; the second reserved area is located in the user space, and the second reserved area is used to temporarily store the data required to be accessed by the simulated instruction; The signal processing function modifies the abnormal instruction and / or the first context according to the address corresponding to the data in the second reserved area; The signal processing function writes the abnormal instruction into the first reserved area, including: The signal processing function writes the modified exception instruction into the first reserved area.
4. The method according to claim 1, It is characterized in that Before the signal processing function writes the exception instruction and the first jump instruction into the first reserved area, the signal processing function further includes: The signal processing function parses the abnormal instruction to determine whether the abnormal instruction includes a write operation; If the abnormal instruction includes a write operation, and the memory access address corresponding to the write operation is located in the kernel space, the signal processing function presets an address in the second reserved area for the write operation, and modifies the abnormal instruction and / or the first context according to the preset address; The signal processing function writes the abnormal instruction into the first reserved area, including: The signal processing function writes the modified exception instruction into the first reserved area.
5. The method according to claim 1, It is characterized in that After the signal processing function obtains the first simulation result, it also includes: The signal processing function parses the abnormal instruction to determine whether the abnormal instruction includes a write operation; If the abnormal instruction includes the write operation, and the memory address corresponding to the data to be written is located in the kernel space, the signal processing function obtains the data to be written from the second reserved area; The signal processing function sends a fourth request to the kernel; the fourth request is used to request the kernel to write the data to be written into the redirection space; the redirection space refers to an address space that has a mapping relationship with the kernel space.
6. The method according to claim 1, It is characterized in that The signal processing function obtains the first simulation result, including: The signal processing function obtains a third context, where the third context is generated by executing the abnormal instruction in the first reserved area; The signal processing function obtains the first simulation result from the third context.
7. The method according to claim 6, It is characterized in that After the signal processing function obtains the first simulation result, it also includes: The signal processing function modifies the third context to determine a fourth context; wherein the program counter in the fourth context points to the next instruction of the abnormal instruction; The signal processing function sends a fifth request to the kernel; the fifth request is used to request the kernel to use the fourth context to resume program execution.
8. The method according to any one of claims 1 to 7, It is characterized in that The signal processing function writes the exception instruction and the first jump instruction into the first reserved area, including: The signal processing function obtains N instructions in the user program that are located after the abnormal instruction; N is an integer greater than or equal to 0; The signal processing function writes the abnormal instruction and the N instructions into the first reserved area; The signal processing function writes the first jump instruction into the first reserved area; the first jump instruction is located after the exception instruction and the N instructions.
9. The method according to any one of claims 1 to 7, It is characterized in that Before the signal processing function writes the exception instruction and the first jump instruction into the first reserved area, the signal processing function further includes: The abnormal instruction is determined to be a complex instruction; the complex instruction includes a memory access operation and a calculation operation, multiple memory access operations or multiple calculation operations.
10. The method according to claim 9, It is characterized in that The method further comprises: Determining that the abnormal instruction is a memory access instruction; The signal processing function parses the abnormal instruction and determines that the memory access address of the abnormal instruction is located in the kernel space; The signal processing function determines a first redirection address corresponding to the memory access address; the first redirection address is an address located in the redirection space; The signal processing function sends a second request to the kernel; the second request is used to request the kernel to perform data read and write operations according to the first redirection address; The signal processing function obtains the read and write results of the kernel, simulates the execution of the abnormal instruction, and obtains a second simulation result.
11. The method according to claim 3, It is characterized in that The method further comprises: When the user program is started, the first reserved area and the second reserved area are set; or, When the kernel space is allocated to the user program for the first time, the first reserved area and the second reserved area are set.
12. The method according to claim 11, It is characterized in that The method further comprises: When allocating the first reserved area, setting the permission corresponding to the first reserved area to be writable and executable; or, When allocating the first reserved area, setting the permission corresponding to the first reserved area to be unreadable, unwritable, and unexecutable; when writing an abnormal instruction to the first reserved area, setting the permission corresponding to the first reserved area to be writable but not executable; when executing the first reserved area, setting the permission corresponding to the first reserved area to be unwritable but executable; after executing the first reserved area, setting the permission corresponding to the first reserved area to be unreadable, unwritable, and unexecutable; or, When allocating the first reserved area, the permission corresponding to the first reserved area is set to be writable but not executable; when executing the first reserved area, the permission corresponding to the first reserved area is set to be non-writable but executable; after executing the first reserved area, the permission corresponding to the first reserved area is set to be writable but not executable.
13. The method according to claim 12, It is characterized in that The method further comprises: When allocating the second reserved area, setting the permission corresponding to the second reserved area to be readable and writable but not executable; or, When allocating the second reserved area, the permissions corresponding to the second reserved area are set to be unreadable, unwritable, and unexecutable; when performing a read operation or a write operation on the second reserved area, the permissions corresponding to the second reserved area are set to be readable, writable, and unexecutable; when completing the read operation or the write operation on the second reserved area, the permissions corresponding to the second reserved area are set to be unreadable, unwritable, and unexecutable.
14. A computing device, It is characterized in that include: A memory for storing program instructions; A processor is used to call the program instructions stored in the memory, and execute the method according to any one of claims 1 to 13 according to the obtained program instructions.
15. A computer-readable storage medium, It is characterized in that The method comprises computer-readable instructions, and when a computer reads and executes the computer-readable instructions, the method according to any one of claims 1 to 13 is implemented.
16. A computer program product, It is characterized in that The invention comprises a computer program executable by a computer device, and when the program is run on the computer device, the computer device is caused to execute the steps of any one of the methods of claims 1 to 13.
