A method for processing memory access exception, computing device, storage medium and program product
By registering signal processing functions in the user program and simulating the execution of memory access exception instructions, it is possible to allocate kernel space to the user program when the user space is exhausted, solving the problem of user space expansion, ensuring normal program execution and improving memory allocation efficiency.
Patent Information
- Application Number
- CN202410491350.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-23
- Publication Date
- 2024-08-23
- Estimated Expiration
- 2044-04-23
AI Technical Summary
When the user space is exhausted, the user program cannot continue to obtain memory allocation, causing the program to crash. It is difficult to expand the user space without changing the hardware and kernel with existing technology.
Register the page exception signal processing function through the user program, simulate the memory access exception instruction triggered by the execution, allocate and expand the user space in the kernel space, ensure the normal execution of the user program, and give priority to the memory pool and backing storage when the memory is exhausted.
It realizes allocating kernel space for user programs when user space is exhausted, ensuring normal program execution, avoiding program crashes, improving memory allocation efficiency, and reducing system call overhead.
Smart Images

Figure CN118093202B
Abstract
Description
Technical Field
[0001] The present application relates to the field of computers, and in particular to a method for processing memory access exceptions, a computing device, a storage medium, and a program product. Background Art
[0002] Virtual memory is generally divided into non-overlapping kernel space and user space. User programs can only access user space, which means that the maximum memory space a user program can use depends on the size of the user space. For example, for some instruction set architectures, the total virtual memory is 4GB, of which 2GB is user space and 2GB is kernel space. If the 2GB corresponding to the user space has been used up by the user program, then when the user program requests memory from the kernel again, the kernel will no longer be able to allocate memory to the user program, eventually causing the program to crash.
[0003] So, how to continue to allocate memory to user programs when the user space is exhausted so that the user programs can execute normally remains to be solved. Summary of the invention
[0004] The present application provides a method for processing memory access exceptions, a computing device, a storage medium, and a program product, which can continue to allocate memory to a user program when the user space is exhausted, so that the user program can be executed normally.
[0005] In the first aspect, an embodiment of the present application provides a method for processing memory access exceptions, which can be executed by a memory access exception processing device, which 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 includes: a user program registers a signal processing function for page exceptions and allocates an extended user space in the kernel space to the user program; the signal processing function is used to replace the kernel's default processing method for the page exception; after the user program triggers the page exception, the kernel triggers the signal processing function and passes the context information corresponding to the page exception to the signal processing function; after determining that the page exception is the first memory access exception, the signal processing function simulates the execution of the instruction that triggers the first memory access exception; the first memory access exception is triggered when the user program accesses the extended user space.
[0006] The above scheme, on the one hand, can allocate an extended user space in the kernel space to the user program, that is, when the user space is exhausted, the kernel space can be allocated to the user program, which expands the user space to a certain extent. On the other hand, since the user program registers a signal processing function for the page exception, after determining that the page exception is the first memory access exception, the user program will not be interrupted, but the signal processing function is used to simulate the execution of the instruction that triggers the first memory access exception, so that the user program is executed normally. On the other hand, since the instruction that triggers the first memory access exception is executed in simulation, it can be ensured that it cannot access the real data in the kernel space when necessary, thereby ensuring the safe operation of the kernel. Therefore, this scheme can achieve the expansion of the user space without changing the hardware or the kernel.
[0007] In a possible implementation method, the signal processing function simulates the execution of the instruction that triggers the first memory access exception to obtain a simulated execution result; the simulated execution result is fed back to the kernel in the form of context information; and the kernel uses the new context information to resume execution of the user program.
[0008] The above scheme feeds back the simulation execution result to the kernel in the form of context information, which can help the kernel to accurately and effectively resume the execution of the user program.
[0009] In a possible implementation method, the signal processing function includes a first parameter; the first parameter is used to obtain context information corresponding to the page exception through the kernel.
[0010] In the above solution, the signal processing function includes the first parameter, which enables the signal processing function to accurately and effectively obtain the context information corresponding to the page exception.
[0011] In a possible implementation method, when the user program registers the signal processing function with the kernel, the kernel is instructed through a second parameter whether to use a backup stack when calling the signal processing function, and the backup stack is different from the stack used when the first memory access exception is triggered.
[0012] In the above scheme, a second parameter indicating that a backup stack needs to be used is also provided when registering a signal processing function, so that recursive or nested stack overflow can be avoided, thereby ensuring accurate and effective execution of user programs.
[0013] In a possible implementation method, the signal processing function is registered when the user program is started; or, the signal processing function is registered when an extended user space is allocated to the user program for the first time.
[0014] The above solution increases the flexibility of registering signal processing functions.
[0015] In a possible implementation method, a memory allocation request triggered by the user program is obtained; after receiving the indication information of memory exhaustion sent by the kernel, it is determined whether there is a virtual address space in the kernel space that meets the memory allocation request through the kernel space allocation table stored in itself; if so, the virtual address space is used as the extended user space of the user program.
[0016] In the above solution, kernel space is allocated by user program, so that user space can be expanded without modifying hardware or kernel.
[0017] In one possible implementation method, the memory exhaustion indication information includes: the size of available memory in physical memory and swap memory does not meet the memory size indicated by the memory allocation request; or the size of available memory in the virtual address space corresponding to the user space does not meet the memory size indicated by the memory allocation request.
[0018] The above scheme can not only allocate kernel space for user programs when the user space is exhausted, but also allocate kernel space for user programs when the physical memory and swap memory are exhausted.
[0019] In a possible implementation method, the user program determines whether there is a continuous area through a kernel space allocation table stored in the user program, the continuous area is located in the kernel space, and the memory size formed by the continuous area is greater than or equal to the memory size indicated by the memory allocation request; the kernel space allocation table is maintained by the user program and is used to query whether there is unallocated continuous space in the kernel space.
[0020] The above scheme can realize accurate and effective allocation of kernel space by user programs.
[0021] In a possible implementation method, the user program determines that the size of the backup storage satisfies the memory size indicated by the memory allocation request.
[0022] The above scheme can not only allocate kernel space for user programs when the user space is exhausted, but also allocate kernel space for user programs when the physical memory and swap memory are exhausted.
[0023] In one possible implementation method, the user program determines that the remaining space on the physical disk satisfies the memory size indicated by the memory allocation request; or, the user program determines that the remaining space on the memory disk satisfies the memory size indicated by the memory allocation request, and the memory disk refers to a fixed or dynamically sized area divided by the operating system on the physical memory, virtual memory, or swap space, and the area has read and write permissions; or, the user program determines that the remaining space on the network storage satisfies the memory size indicated by the memory allocation request.
[0024] The above scheme can not only allocate kernel space for user programs when the user space is exhausted, but also allocate kernel space for user programs when the physical memory and swap memory are exhausted.
[0025] In a possible implementation method, after determining that there is no free memory block in the memory pool that satisfies the memory size required by the memory allocation request, the memory allocation request is sent to the kernel.
[0026] The above scheme gives priority to allocating free memory in the memory pool to user programs. When the free memory in the memory pool does not meet the free memory block of the memory size required by the memory allocation request, the memory allocation request is sent to the kernel. Giving priority to allocating free memory in the memory pool to user programs can improve the efficiency of allocating memory to user programs, reduce memory fragmentation, and reduce the overhead generated by system calls.
[0027] In a possible implementation method, the memory size corresponding to the memory allocation in the extended user space is greater than or equal to the memory size indicated by the memory allocation request;
[0028] After allocating an extended user space in the kernel space to the user program, it also includes: placing the remaining memory blocks in the extended user space into the memory pool; the remaining memory blocks refer to the part of the memory size corresponding to the memory size used for this memory allocation in the extended user space minus the memory size indicated by the memory allocation request.
[0029] The above solution puts the remaining memory blocks into the memory pool. When the user program requests memory allocation, the free memory in the memory pool is allocated to the user program first, which can improve the efficiency of memory allocation for the user program, reduce memory fragmentation, and reduce the overhead generated by system calls.
[0030] In a possible implementation method, an extended user space in the kernel space is allocated to the user program through a runtime library corresponding to the user program; or, an extended user space in the kernel space is allocated to the user program through a memory allocation wrapper in the user program.
[0031] The above scheme increases the flexibility of allocating an extended user space in the kernel space to a user program, and can realize the expansion of the user space without modifying the hardware and the kernel.
[0032] In a possible implementation method, a memory allocation request is sent to the runtime library through a memory allocation wrapper in the user program; after the runtime library determines that there is no free memory block in the memory pool that meets the memory size indicated by the memory allocation request, it sends the memory allocation request to the kernel.
[0033] In the above scheme, when allocating memory for a user program, the memory pool of the runtime library is first considered to meet the demand, and then the user space is considered to meet the demand. If both are not met, the memory allocation wrapper in the user program allocates kernel space for the user program by itself to achieve the expansion of the user space. This can ensure that the user program does not abuse the kernel space and ensure the rationality of the use of the kernel space.
[0034] In a possible implementation method, a page corresponding to the extended user space in a kernel space allocation table is set as allocated, and a starting address corresponding to the extended user space is returned to the user program.
[0035] In the above scheme, the kernel space allocation table is maintained by the user program itself, so setting the page corresponding to the extended user space in the kernel space allocation table as allocated can determine which addresses of the kernel space are occupied by the user program, so that the kernel space will not be repeatedly allocated, causing the program to crash. Furthermore, the user space can be expanded without modifying the hardware and the kernel.
[0036] In a possible implementation method, the state of the page corresponding to the kernel space allocation table is set to unallocated.
[0037] In the above scheme, the kernel space allocation table is maintained by the user program itself, so the corresponding kernel space cannot be released through the kernel. Setting the state of the page corresponding to the kernel space allocation table to unallocated can realize the simple and effective release of kernel space.
[0038] In a second aspect, an embodiment of the present application provides a device for processing memory access exceptions, including: a registration unit and a processing unit.
[0039] The registration unit is used for the user program to register a signal processing function for page exceptions and to allocate an extended user space in the kernel space to the user program; the signal processing function is used to replace the kernel's default processing method for the page exception; the processing unit is used to trigger the signal processing function by the kernel after the user program triggers the page exception, and pass the context information corresponding to the page exception to the signal processing function; after determining that the page exception is the first memory access exception, the signal processing function simulates the execution of the instruction that triggers the first memory access exception; the first memory access exception is triggered when the user program accesses the extended user space.
[0040] In a possible implementation method, the processing unit is also used to simulate the execution of the instruction that triggers the first memory access exception through the signal processing function to obtain a simulated execution result; feed back the simulated execution result to the kernel in the form of context information; and the kernel uses the new context information to resume the execution of the user program.
[0041] In a possible implementation method, the signal processing function includes a first parameter; the first parameter is used to obtain context information corresponding to the page exception through the kernel.
[0042] In one possible implementation method, the processing unit is also used to indicate through a second parameter whether the kernel should use a backup stack when calling the signal processing function when the user program registers the signal processing function with the kernel, and the backup stack is different from the stack used when the first memory access exception is triggered.
[0043] In a possible implementation method, the registration unit is used to register the signal processing function when the user program is started; or, to register the signal processing function when an extended user space is allocated to the user program for the first time.
[0044] In a possible implementation method, the above-mentioned device also includes an acquisition unit, which is used to obtain a memory allocation request triggered by the user program; the processing unit is also used to determine whether there is a virtual address space in the kernel space that meets the memory allocation request after receiving the indication information of memory exhaustion sent by the kernel through the kernel space allocation table stored in itself; if so, use the virtual address space as the extended user space of the user program.
[0045] In one possible implementation method, the memory exhaustion indication information includes: the size of available memory in physical memory and swap memory does not meet the memory size indicated by the memory allocation request; or the size of available memory in the virtual address space corresponding to the user space does not meet the memory size indicated by the memory allocation request.
[0046] In a possible implementation method, the processing unit is also used to determine whether there is a continuous area through a kernel space allocation table stored in itself, the continuous area is located in the kernel space, and the memory size formed by the continuous area is greater than or equal to the memory size indicated by the memory allocation request; the kernel space allocation table is maintained by the user program and is used to query whether there is unallocated continuous space in the kernel space.
[0047] In a possible implementation method, the processing unit is further configured to determine whether the size of the backup storage satisfies the memory size indicated by the memory allocation request.
[0048] In one possible implementation method, the processing unit is also used to determine whether the remaining space on the physical disk satisfies the memory size indicated by the memory allocation request; or, the user program determines that the remaining space on the memory disk satisfies the memory size indicated by the memory allocation request, and the memory disk refers to a fixed or dynamically sized area divided by the operating system on the physical memory, virtual memory, or swap space, and the area has read and write permissions; or, the user program determines that the remaining space on the network storage satisfies the memory size indicated by the memory allocation request.
[0049] In a possible implementation method, the processing unit is further configured to send the memory allocation request to the kernel after determining that there is no free memory block in the memory pool that satisfies the memory size required by the memory allocation request.
[0050] In a possible implementation method, the memory size corresponding to the memory allocation in the extended user space is greater than or equal to the memory size indicated by the memory allocation request;
[0051] The processing unit is also used to put the remaining memory blocks in the extended user space into the memory pool; the remaining memory blocks refer to the portion of the memory size corresponding to the memory size used for this memory allocation in the extended user space minus the memory size indicated by the memory allocation request.
[0052] In one possible implementation method, the processing unit is also used to allocate an extended user space in the kernel space to the user program through a runtime library corresponding to the user program; or, allocate an extended user space in the kernel space to the user program through a memory allocation wrapper in the user program.
[0053] In a possible implementation method, the processing unit is also used to send a memory allocation request to the runtime library through a memory allocation wrapper in the user program; after the runtime library determines that there is no free memory block in the memory pool that meets the memory size indicated by the memory allocation request, it sends the memory allocation request to the kernel.
[0054] In a possible implementation method, the processing unit is further used to set the page corresponding to the extended user space in the kernel space allocation table as allocated, and return the starting address corresponding to the extended user space to the user program.
[0055] In a possible implementation method, the processing unit is further configured to set the state of the page corresponding to the kernel space allocation table to unallocated.
[0056] In a third aspect, an embodiment of the present application further provides a computing device, including:
[0057] A memory for storing program instructions;
[0058] 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.
[0059] 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.
[0060] 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
[0061] Figure 1 A flowchart of a method for handling memory access exceptions provided in an embodiment of the present application;
[0062] Figure 2 A flowchart of a method for handling memory access exceptions provided in an embodiment of the present application;
[0063] Figure 3 A flowchart of a method for handling memory access exceptions provided in an embodiment of the present application;
[0064] Figure 4 A flowchart of a method for allocating an extended user space provided in an embodiment of the present application;
[0065] Figure 5 A flowchart of a method for allocating an extended user space provided in an embodiment of the present application;
[0066] Figure 6 A flowchart of a method for allocating an extended user space provided in an embodiment of the present application;
[0067] Figure 7 A timing diagram of memory access exception processing provided in an embodiment of the present application;
[0068] Figure 8 A schematic diagram of the structure of a device for processing memory access exceptions provided in an embodiment of the present application;
[0069] Fig. 9 A schematic diagram of the structure of a device for processing memory access exceptions provided in an embodiment of the present application. DETAILED DESCRIPTION
[0070] The following is an explanation of some of the professional terms used in this application.
[0071] 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.
[0072] 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.
[0073] 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.
[0074] 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.
[0075] 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).
[0076] 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.
[0077] 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.
[0078] 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.
[0079] 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.
[0080] 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).
[0081] 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.
[0082] 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.
[0083] 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.
[0084] 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.
[0085] 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).
[0086] 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.
[0087] 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.
[0088] Just-in-time compilation (JIT): In computer technology, just-in-time compilation (English: Just-in-time compilation, abbreviated as JIT; also translated as timely compilation, real-time compilation), also known as dynamic translation or runtime compilation, is a method of executing computer code that is designed to be compiled during the execution of the program (at the execution stage) rather than before execution. Typically, this includes the conversion of source code or more commonly bytecode to machine code, which is then executed directly. Systems that implement JIT compilers usually continuously analyze the code being executed and determine the parts of the code that can be accelerated by just-in-time compilation, where the performance improvement brought by compiling or recompiling will exceed the overhead of compiling the code. From the perspective of memory usage, JITing a section of source code or bytecode requires first applying for a section of memory, then writing the compiled machine code to that section of memory, then setting that section of memory to be executable, and finally jumping to that section of memory to execute the aforementioned machine code.
[0089] Runtime library (also called runtime library): In the field of computer programming, it refers to a special computer program library required when a programming language program is running (executed). The compiler will call the runtime library into the compiled executable binary code. This library generally includes basic input and output or memory management support. It is a group of functions that support the executing program, and cooperates with the operating system to provide functions such as mathematical operations, input and output, so that programmers do not need to "reinvent the wheel" and make good use of the functions provided by the operating system.
[0090] Dynamic linking and static linking: A program (executable file) is usually composed of one or more object files (object files) generated by a compiler or assembler and a library (library). The process of assembling these object files and libraries is called linking, and the tool for implementing the linking is called a linker. According to the linking period, it can be divided into static linking and dynamic linking. The linking is completed when the executable file is generated, which is called static linking; the linking is completed when the program is executed, which is called dynamic linking. The library can be compiled as a static library (static library) or a dynamic library (dynamic library). Dynamic library files are often called dynamic-link libraries (DLL) or dynamic shared objects (DSO). Using dynamic libraries has the following advantages: if multiple executable files need to use the same library, only one DSO is needed (in contrast, the same static library will be repeated in multiple statically linked executable files); when the library needs to be upgraded, only the DSO needs to be replaced, and the executable file of the program does not need to be changed (in contrast, the statically linked program needs to be compiled and statically linked again to form a new executable file). The aforementioned runtime library can be either statically linked to the executable file or dynamically linked.
[0091] Figure 1 A flowchart of a method for handling a memory access exception provided in an embodiment of the present application, the method can be executed by a device for handling a memory access exception, the device for handling a memory access exception 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.
[0092] The method comprises the following steps:
[0093] Step 101 : A user program registers a signal processing function for a page exception and allocates an extended user space in a kernel space to the user program.
[0094] The signal processing function is used to replace the kernel's default processing method for the page exception.
[0095] Step 102: After the user program triggers a page exception, the kernel triggers the signal processing function and passes context information corresponding to the page exception to the signal processing function.
[0096] Step 103: After determining that the page exception is the first memory access exception, the signal processing function simulates execution of the instruction that triggers the first memory access exception.
[0097] The first memory access exception is triggered when the user program accesses memory in the extended user space.
[0098] In a possible implementation method, the user program accesses memory of the extended user space, including: the user program is executed in the extended user space; wherein, the execution of the user program in the extended user space may be caused by jumping to the extended user space, or may be caused by executing to the extended user space during sequential execution.
[0099] The above scheme, on the one hand, can allocate an extended user space in the kernel space to the user program, that is, when the user space is exhausted, the kernel space can be allocated to the user program, which expands the user space to a certain extent. On the other hand, since the user program registers a signal processing function for page exceptions, after determining that the page exception is the first memory access exception, the user program will not be interrupted, but the signal processing function is used to simulate the execution of the instruction that triggers the first memory access exception, so that the user program is executed normally. On the other hand, since the instruction that triggers the first memory access exception is executed in simulation, it cannot access the real data in the kernel space, thereby ensuring the safe operation of the kernel. Therefore, this scheme can achieve the expansion of the user space without changing the hardware or the kernel.
[0100] In a possible implementation method, in step 102, after the user program triggers a page exception, it is necessary to determine the type of the page exception. If the type of the page exception is a segmentation error, the signal processing function is triggered by the kernel; if the type of the page exception is not a segmentation error, the system default processing method corresponding to the page exception type is used.
[0101] In a possible implementation method, the memory access address that causes the page exception is retrieved to determine whether the memory access address is located in the user space; if the memory access address is not located in the user space, the type of the page exception is determined to be a segmentation fault.
[0102] In another possible implementation method, if the memory access address is located in the user space, the page table entry corresponding to the memory access address is retrieved, and it is determined whether the corresponding page has been allocated; if the corresponding page has not been allocated, it is determined that the type of the page exception is a segmentation fault.
[0103] In another possible implementation method, if the corresponding page has been allocated, it is determined whether the user program behavior complies with the page permissions, and if not, the type of the page exception is determined to be a segmentation fault. Exemplarily, not complying with page permissions includes: attempting to write to a read-only page, or attempting to jump to a non-executable page for execution, etc.
[0104] In one possible implementation method, the user program registers a signal processing function, i.e., a handler, for a SIGSEGV (Segmentation Fault Signal) signal. SIGSEGV is a type of signal indicating that a segmentation fault has occurred in a computer program.
[0105] In a possible implementation method, after the signal processing function determines that the type of page exception is a segmentation fault, and determines that the segmentation fault is caused by the user program accessing the extended user space, the user program simulates the execution of the instruction that triggers the first memory access exception. In other words, the user program accesses the previously allocated extended user space. Since it is actually the kernel space, the CPU will generate a page exception, so it will fall into the kernel for exception processing (equivalent to a trap). Since the program has registered a handler for the SIGSEGV signal, the kernel will trigger this signal handler, and the signalhandler will emulate in the user space, so as to simulate the program's access to the extended user space.
[0106] In a possible implementation method, the signal processing function is registered when the user program is started; or, the signal processing function is registered when the extended user space is allocated to the user program for the first time. This solution increases the flexibility of registering the signal processing function.
[0107] In a possible implementation method, the signal processing function includes a first parameter; the first parameter is used to obtain context information corresponding to the page exception through the kernel. In this solution, the signal processing function includes the first parameter, which enables the signal processing function to accurately and effectively obtain context information corresponding to the page exception.
[0108] In another possible implementation method, when the user program registers the signal processing function with the kernel, the kernel is instructed through a second parameter whether to use a backup stack when calling the signal processing function, and the backup stack is different from the stack used when the first memory access exception is triggered. The second parameter is a parameter passed to the kernel when registering the signal processing function. In this solution, a second parameter indicating that a backup stack needs to be used is also provided when registering the signal processing function, which can avoid recursive or nested stack overflows and ensure accurate and effective execution of the user program.
[0109] Take Linux as an example. In general, the function prototype of the siganl handler is: void handler(intsig), which means that it has no return value and only receives one parameter, which is an integer type and represents the signal number. If the SA_SIGINFO flag is provided when registering the signal handler, the function prototype of the siganl handler becomes void handler(int sig, siginfo_t *info, void *ucontext). Among them, siginfo_t is the type of the parameter info, which represents a structure used to store information related to the signal, so info is used to receive information related to the signal; ucontext represents the first parameter, which is similar to the kernel's exception frame, saving all the states of the program, especially the values of each register. This structure contains the signal context information (context) saved by the kernel on the user space stack. Through this mechanism, the signal handler can obtain the context of the program at the moment the exception occurs through the parameters passed by the kernel, and ucontext is the first parameter included in the signal processing function.
[0110] In a possible implementation method, when registering a signal handler, an SA_ONSTACK flag is provided. The SA_ONSTACK flag is used to indicate that a backup stack needs to be used. If the SA_ONSTACK flag is provided, the kernel will prepare a backup stack and use the backup stack to trigger the signal processing function before the signal processing function needs to be triggered later due to the corresponding signal. If the SA_ONSTACK flag is not provided, it indicates that the backup stack is not used, and when the kernel needs to trigger the signal processing function later due to the corresponding signal, the signal processing function will be directly triggered on the default stack, and the SA_ONSTACK flag is the second parameter passed in when registering the signal processing function.
[0111] The reason for using a backup stack is that when a stack overflow occurs, an unallocated page will be accessed, based on which the kernel will issue a SIGSEGV signal. If the SIGSEGV handler is called on the original stack at this time, the stack will overflow again because the signal handler is likely to use the stack as well. The kernel will then issue a SIGSEGV signal based on this and enter the SIGSEGV handler again, resulting in an infinite loop (i.e., a recursive or nested stack overflow). Using a backup stack and calling the signal processing function on the backup stack avoids the above problem.
[0112] Below Figure 2 Take html as an example to illustrate how to handle page exceptions.
[0113] Step 201: The kernel receives a page exception triggered by a user program.
[0114] Step 202, the kernel saves the context of the user program at the moment the exception is raised.
[0115] Step 203: The kernel retrieves the memory access address that caused the exception.
[0116] Step 204: The kernel determines whether the memory access address is in the user space.
[0117] In a possible implementation method, if the memory access address is located in the user space, step 205 is executed; if the memory access address is not located in the user space, the type of the page exception is determined to be a segmentation fault, that is, step 208 is executed.
[0118] Step 205: The kernel retrieves the page table entry corresponding to the memory access address and determines whether the page has been allocated.
[0119] In a possible implementation method, if the page has been allocated, step 206 is executed; if the page has not been allocated, it is determined that the type of the page exception is a segmentation fault, that is, step 208 is executed.
[0120] Step 206: The kernel determines whether the user program behavior complies with the page permissions.
[0121] In a possible implementation method, if the user program behavior complies with the page permissions, step 207 is executed; if the user program behavior does not comply with the page permissions, the type of the page exception is determined to be a segmentation fault, that is, step 208 is executed.
[0122] Step 207: The kernel searches for a free physical page in the physical memory, creates a page mapping for the page corresponding to the memory access address, and returns to the location where the user program was originally executed.
[0123] In one possible implementation method, before searching for a free physical page in the physical memory, it is necessary to determine whether the page corresponding to the memory access address has been used. If the page corresponding to the memory access address has not been used, that is, it is used for the first time after the memory is allocated, a free physical page is searched in the physical memory to establish a user space page mapping; if the page corresponding to the memory access address has been used, that is, it has been swapped out to the external memory, a free physical page is searched in the physical memory, the page corresponding to the memory access address is read from the external memory and written to the physical page (swapped in), and then a user space mapping relationship between the page corresponding to the memory access address and the physical page is established.
[0124] Step 208: The kernel determines that the type of the page exception is a segment fault.
[0125] Step 209: The kernel determines whether the user program has registered a signal processing function.
[0126] In a possible implementation method, if the user program has registered a signal handler, the signal handler is entered to handle the page exception, that is, step 210; if the user program has not registered a signal handler, the system default signal handler processing method is used, that is, step 211.
[0127] Generally speaking, there are three ways for the operating system to handle signals: (1) ignore; (2) call the signal handler registered by the user program; (3) call the kernel's default processing method.
[0128] In the initial state, the operating system handles signals in method (3). User programs can modify the signal processing method for specific signals through system calls.
[0129] User programs register a signal handler for SIGSEGV through system calls. When the kernel does not register a signal handler for SIGSEGV, the default signal handler will kill the process; in fact, the kernel will use the kernel's default signal handler for all signals (except SIGKILL and SIGSTOP, which cannot register a signal handler) when the user program does not register a corresponding signal handler; as long as a signalhandler is registered, the default behavior can be overwritten and the signal handler registered by the user program can be used.
[0130] When the type of page exception is determined to be a segmentation fault, a signalhandler is registered for SIGSEGV through a system call to handle the page exception. The segmentation fault can be handled without making any modifications to the kernel.
[0131] Step 210, enter the signal processing function to handle page exceptions.
[0132] Step 211, the program crashes.
[0133] In a possible implementation method, the above step 210 enters the step of processing the page exception by the signal processing function as follows: Figure 3 As shown, this step includes the following:
[0134] Step 301 : The signal processing function obtains the signal information and the context A at the time when the page exception occurs from the parameters passed in by the kernel.
[0135] In a possible implementation method, the signal information refers to some miscellaneous information related to the signal, and the address causing the page exception can be obtained from the signal information.
[0136] Step 302: The signal processing function obtains the memory access address that triggers the page exception from the signal information or context A.
[0137] Step 303: The signal processing function determines whether the memory access address is located in the kernel space.
[0138] In a possible implementation method, if the memory access address is located in the kernel space, the page exception is determined to be the first memory access exception, and step 304 is executed; if the memory access address is not located in the kernel space, a program crash is triggered, that is, step 306.
[0139] Step 304: The signal processing function determines whether the memory access address is located in the extended user space allocation table.
[0140] In a possible implementation method, if the memory access address is located in the extended user space allocation table, step 305 is executed; if the memory access address is not located in the extended user space allocation table, a program crash is triggered, that is, step 306.
[0141] Step 305: The signal processing function determines whether the memory access behavior complies with the page authority.
[0142] In a possible implementation method, if the memory access behavior complies with the page permissions, step 307 is executed; if the memory access behavior does not comply with the page permissions, a program crash is triggered, that is, step 306.
[0143] Step 306, the program crashes.
[0144] Step 307: The signal processing function obtains the address of the instruction that caused the exception from the signal information or context A, and further obtains the instruction.
[0145] Step 308: The signal processing function simulates the execution of the instruction.
[0146] In a possible implementation method, the instruction that triggers the first memory access exception is simulated and executed by the signal processing function to obtain a simulated execution result; the simulated execution result is fed back to the kernel in the form of context information; and the kernel uses the new context information to resume the execution of the user program. This solution feeds back the simulated execution result to the kernel in the form of context information, which can help the kernel to accurately and effectively resume the execution of the user program.
[0147] In a possible implementation method, if the simulation is executed successfully, it is determined whether the simulation result causes other exceptions; if the simulation result does not cause other exceptions, the kernel is requested to use a new context to resume normal execution of the program. If the simulation result causes other exceptions and the other exceptions can be repaired, after the other exceptions are repaired, the simulation result is modified according to the repair result, and the kernel is requested to use a new context to resume normal execution of the program. If the simulation result causes other exceptions and the other exceptions cannot be repaired, the other exceptions are passed to the kernel for processing, or the program is directly crashed.
[0148] In a possible implementation method, if the simulation execution fails, a program crash is triggered, namely step 306 .
[0149] When the kernel handles the page exception of the segment fault, it will call the signal handler based on the registered SIGSEGV handler. This looks like the signal handler hijacks the exception, and simulates the instructions when the error occurs based on the context information passed, and uses the simulation results to modify the context, and returns the new context to the kernel. The kernel uses the new context to resume program execution, which is equivalent to applying the simulation results, so that the process can proceed normally.
[0150] It can be seen that for the processing of page exceptions, this application does not require any modification to the kernel and can run under the existing operating system kernel. The only requirement is that the operating system kernel supports user programs to register signal handlers, and that (the kernel believes) page misuse can be converted into signals (such as SIGSEGV in the embodiment of this application). Since the instruction that triggers the first memory access exception is executed in simulation, it cannot access the real data in the kernel space, thereby ensuring the safe operation of the kernel.
[0151] In step 103, after determining that the page exception is the first memory access exception, the signal processing function simulates the execution of the instruction that triggers the first memory access exception, and the first memory access exception is triggered when the user program accesses the extended user space. Among them, the extended user space is the kernel space, that is to say, the first memory access exception of the present application is due to the unauthorized access of the user program to the kernel space, and the unauthorized access of the user program to the kernel space can be divided into two types: ordinary memory access to the kernel space; jump instruction attempts to jump to the kernel space for execution.
[0152] So, why do we need to simulate the execution of the instruction that triggers the first memory access exception when the user program accesses the kernel space beyond its authority? This is because virtual memory is generally divided into non-overlapping kernel space and user space. When the user program applies for memory, the kernel allocates the user space to the user program. In other words, the maximum memory space that a user program can use depends on the size of the user space. Then, as the user program continues to apply for memory, when the user space is exhausted, the result faced by the user program is program crash.
[0153] In order to prevent the user program from crashing when the user space is exhausted, the present application allows the user program to allocate an extended user space in the kernel space. The present application provides a method for a user program to allocate an extended user space in the kernel space, such as Figure 4 As shown, the method comprises the following steps:
[0154] Step 401, obtaining a memory allocation request triggered by the user program.
[0155] Step 402: after receiving the indication information of memory exhaustion sent by the kernel, determine whether there is a virtual address space in the kernel space that meets the memory allocation request through the kernel space allocation table stored in itself.
[0156] In a possible implementation method, the indication information of memory exhaustion includes: the size of available memory in physical memory and swap memory does not meet the memory size indicated by the memory allocation request; or the size of available memory in the virtual address space corresponding to the user space does not meet the memory size indicated by the memory allocation request. This solution can not only allocate kernel space for user programs when user space is exhausted, but also allocate kernel space for user programs when physical memory and swap memory are exhausted.
[0157] In a possible implementation method, the user program itself maintains a memory allocation table, namely, a kernel space allocation table, and the addresses in the memory allocation table are the same as the kernel space addresses.
[0158] In a possible implementation method, the user program determines whether there is a continuous area through a kernel space allocation table stored in the user program, the continuous area is located in the kernel space, and the memory size formed by the continuous area is greater than or equal to the memory size indicated by the memory allocation request; the kernel space allocation table is maintained by the user program and is used to query whether there is an unallocated continuous space in the kernel space. This solution can realize accurate and effective allocation of kernel space by the user program.
[0159] Step 403: If it exists, use the virtual address space as the extended user space of the user program.
[0160] In a possible implementation method, if the user program determines that a continuous area exists in the kernel space allocation table stored in the user program, a page corresponding to the continuous area is set as allocated, and a starting address corresponding to the continuous area is returned to the user program.
[0161] In the above solution, kernel space is allocated by user program, so that user space can be expanded without modifying hardware or kernel.
[0162] In a possible implementation method, after the user program is executed, the page corresponding to the continuous area is set to unallocated.
[0163] In a possible implementation method, after step 401 and before step 402, it is also included to send the memory allocation request to the kernel after determining that there is no free memory block in the memory pool that meets the memory size required by the memory allocation request. The memory pool is a memory management technology that pre-allocates a larger memory area when the program is running, and allocates and recycles small blocks of memory from this area for use by different parts of the program. The main purpose of using the memory pool is to improve the efficiency of memory allocation and release, reduce memory fragmentation, and improve memory utilization.
[0164] In one possible implementation method, when the corresponding memory size for this memory allocation in the extended user space is larger than the memory size indicated by the memory allocation request, the remaining memory blocks in the extended user space are placed in the memory pool; the remaining memory blocks refer to the portion of the corresponding memory size for this memory allocation in the extended user space minus the memory size indicated by the memory allocation request.
[0165] In a possible implementation method, in the above step 402, before determining whether there is a virtual address space in the kernel space that satisfies the memory allocation request, it also includes: the user program determines that the size of the backup storage satisfies the memory size indicated by the memory allocation request. Wherein, the user program determines that the size of the backup storage satisfies the memory size indicated by the memory allocation request, including: the user program determines that the remaining space on the physical disk satisfies the memory size indicated by the memory allocation request; or, the user program determines that the remaining space on the memory disk satisfies the memory size indicated by the memory allocation request, and the memory disk refers to a fixed or dynamic size area divided by the operating system on the physical memory or virtual memory or swap space, and the area has read and write permissions; or, the user program determines that the remaining space on the network storage satisfies the memory size indicated by the memory allocation request. This solution can allocate kernel space to the user program when the physical memory and swap memory are exhausted.
[0166] In one possible implementation method, the data in the extended user space needs to be stored in a certain location, which is called the backing store.
[0167] When the data in the extended user space is stored on the physical disk, if the signal handler needs to simulate memory access, it can complete the reading and writing of data indirectly (such as calling the file read and write functions provided by libc, or creating a named memory page bound to the file through mmap) or directly through system calls.
[0168] When the data in the extended user space is stored on the memory disk, the signal handler behaves exactly the same as on the physical disk. This is because there is no difference between the memory disk and the disk for the user program. The difference is hidden by the kernel and the file system provided by the kernel. The system calls used to read and write files on the memory disk are exactly the same as those on the physical disk. The common implementation of the memory disk is to divide a fixed or dynamic size area on the physical or virtual memory, and then treat it as equivalent to the disk for reading and writing. For example, the tmpfs and ramfs file systems provided by the Linux kernel.
[0169] The data in the extended user space is stored on the network storage. If the signal handler needs to simulate memory access, it can complete the network transmission indirectly (such as calling the network transmission function provided by libc or other program libraries) or directly through system calls.
[0170] When the signal handler implements simulated memory access, it must be done through system calls. Therefore, all methods that can complete data transfer through system calls can be used as backup storage.
[0171] In a possible implementation method, the allocation of an extended user space in the kernel space to a user program includes: allocating an extended user space in the kernel space to the user program through a runtime library corresponding to the user program; or, allocating an extended user space in the kernel space to the user program through a memory allocation wrapper in the user program. The memory allocation wrapper may also be referred to as a memory allocation wrapper function. This solution increases the flexibility of allocating an extended user space in the kernel space to a user program, and can achieve the expansion of the user space without modifying the hardware and the kernel.
[0172] Figure 5 The runtime library provided in the embodiment of the present application allocates an extended user space in the kernel space to a user program for implementation, and the method comprises the following steps:
[0173] Step 501: The runtime library obtains a memory allocation request triggered by a user program.
[0174] Step 502: The runtime library determines whether the size of the free memory block in the memory pool satisfies the memory allocation request.
[0175] In a possible implementation method, if the size of the free memory block in the memory pool meets the memory allocation request, step 503 is executed; if the size of the free memory block in the memory pool does not meet the memory allocation request, step 504 is executed.
[0176] Step 503, the runtime library takes out a memory block whose size is not less than the size requested by the user program from the free memory block list, and takes out a memory block whose size is equal to the size requested by the user program from the memory block.
[0177] In a possible implementation method, the runtime library maintains a free memory block list, which records the information of free memory blocks in the memory pool.
[0178] In a possible implementation method, a memory equal to the size requested by the user program is taken out from the memory block and allocated to the user program, that is, step 512 .
[0179] Step 504: The runtime library sends a memory allocation request to the kernel.
[0180] The memory allocation request is used to request a memory block from the kernel whose size is not less than the size requested by the user program.
[0181] Step 505: After receiving the memory allocation request sent by the runtime library, the kernel determines whether the available memory satisfies the memory allocation request.
[0182] Among them, available memory refers to physical memory and swap memory, which can also be the backup storage mentioned above.
[0183] In a possible implementation method, if the available memory satisfies the memory allocation request, step 506 is executed; if the available memory does not satisfy the memory allocation request, the kernel sends an indication of memory exhaustion to the runtime library, that is, step 507 is executed.
[0184] Step 506: The kernel determines whether there is a continuous area in the user space that satisfies the memory allocation request.
[0185] In one possible implementation method, if there is no continuous area in the user space that satisfies the memory allocation request, that is, the user space is exhausted, the kernel sends an indication of memory exhaustion to the runtime library, that is, executes step 507; if there is a continuous area in the user space that satisfies the memory allocation request, executes step 508.
[0186] Step 507: The runtime library receives the memory exhaustion indication sent by the kernel.
[0187] In a possible implementation method, after the runtime receives the memory exhaustion indication sent by the kernel, step 509 is executed.
[0188] Step 508: The kernel modifies the status of the pages covered by the memory area to allocated, and returns the starting address of the memory area to the runtime library.
[0189] In a possible implementation method, a memory block of a size equal to the size requested by the user program is taken out from the memory block of the user space returned by the kernel and allocated to the user program, that is, step 512 .
[0190] In one possible implementation method, if the memory block returned by the kernel to the user space is larger than the memory requested by the user program, the remaining portion is placed in a list of free memory blocks.
[0191] Step 509: The runtime library determines whether there is a continuous area in the kernel space that satisfies the memory allocation request.
[0192] In a possible implementation method, the runtime library determines whether there is a continuous area through the kernel space allocation table stored in itself, the continuous area is located in the kernel space, and the memory size formed by the continuous area is greater than or equal to the memory size indicated by the memory allocation request; the kernel space allocation table is maintained by the runtime library and is used to query whether there is unallocated continuous space in the kernel space.
[0193] In a possible implementation method, if there is a continuous area in the kernel space that meets the memory allocation request, step 510 is executed; if there is no continuous area in the kernel space that meets the memory allocation request, an indication of memory exhaustion is sent to the user program, that is, step 513 is executed.
[0194] Step 510: The runtime library sets the state of the continuous area in the kernel space allocation table to allocated.
[0195] In one possible implementation, when the runtime library allocates kernel space, it does not send a memory allocation request to the kernel. Instead, the runtime library records which parts of the kernel space have been allocated in the kernel space allocation table, and then finds an unallocated continuous area, marks its status as allocated, and directly returns the pointer. Therefore, unlike the conventional memory allocation method, the kernel knows nothing about such memory allocation.
[0196] Step 511, take out a memory block of a size equal to the size requested by the user program from the above continuous area.
[0197] In a possible implementation method, if the memory block in the continuous area is larger than the memory requested by the user program, the remaining portion is put into a free memory block list.
[0198] In a possible implementation method, a memory block having a size equal to that requested by the user program is taken out from the continuous area and allocated to the user program, namely, step 512 .
[0199] Step 512: The user program obtains the required memory.
[0200] Step 513: the user program receives the memory exhaustion indication information sent by the runtime library.
[0201] The above scheme can achieve the use of kernel space to expand the available memory of user programs through signal processing without modifying the hardware or the kernel. Furthermore, if the runtime library that provides memory allocation functions for user programs is dynamically linked, then this application may not even require recompiling the user program, let alone modifying the user program. The old binary program can be used directly, and only the runtime library needs to be replaced with a new version that applies this application.
[0202] The biggest reason why the runtime library implements a memory pool is that the memory allocation granularity provided by the runtime library is inconsistent with the memory allocation granularity provided by the operating system (kernel). An example is as follows: the memory allocation granularity provided by the operating system (kernel) is generally the size of a page, which is 4KiB (4096Byte) in most cases, while the memory allocation granularity provided by the runtime library is generally Byte, which is thousands of times different. The memory allocation provided by the runtime library is built on the memory allocation provided by the operating system kernel, and the memory obtained by the runtime library will be a multiple of the page size. Therefore, when the memory requested by the user program is not a multiple of the page size, a spare part will be generated, which will be managed and reserved for future memory allocation. Another important reason is that requesting the operating system (kernel) to allocate / release memory is achieved through system calls, and the overhead of a system call is relatively high; therefore, once the runtime library needs to request memory allocation from the kernel, it will request memory that is much larger than the current required size, and reserve the excess for subsequent use to avoid frequent system calls; similarly when releasing memory, the runtime library will first retain the memory requested to be released by the user program, and reserve it for direct use in subsequent memory allocations. Only when the retained released memory is too much will it request the kernel to release them.
[0203] In one possible implementation method, when the runtime library releases memory, it will do a series of checks and optimizations. The released memory may be reserved for subsequent memory allocation (i.e., memory pool). Only when certain conditions are met (e.g., there is too much free memory in the memory pool) will the kernel be requested to release the memory through a system call. For this application, since the extended user space is essentially the kernel space, the runtime library obviously cannot request the kernel to release it. Therefore, it only needs to mark the corresponding area as unallocated in the kernel space allocation table maintained by itself after the release conditions of the free memory are met, which has no substantial impact on the implementation of the memory pool.
[0204] Figure 6 The memory allocation wrapper provided in the embodiment of the present application allocates an extended user space in the kernel space to a user program for implementation, and the method comprises the following steps:
[0205] Step 601: The memory allocation wrapper obtains a memory allocation request triggered by a user program.
[0206] In one possible implementation method, the memory allocation wrapper is a function in the user program that is used to allocate user space memory or expand user space memory. In other words, there is a memory allocation function in the user program that wraps the memory allocation function provided by the runtime library, and allocates the expanded user space when the runtime library cannot allocate. The wrapper means that it will definitely call the memory allocation function of the runtime library, but after the call, the behavior of allocating and expanding the user space is added. On the other hand, it also shows that the user program should change all calls to the memory allocation function of the runtime library to calls to this function.
[0207] Step 602: The memory allocation wrapper sends a memory allocation request to the runtime library.
[0208] Step 603: The runtime library determines whether the size of the free memory block in the memory pool satisfies the memory allocation request.
[0209] In a possible implementation method, if the size of the free memory block in the memory pool meets the memory allocation request, the runtime library takes out a memory block whose size is not less than the size requested by the user program from the free memory block list, and takes out a memory block whose size is equal to the size requested by the user program from the memory block and sends it to the memory allocation wrapper, and the memory allocation wrapper allocates the memory in the runtime library memory pool to the user program, that is, step 611. If the size of the free memory block in the memory pool does not meet the memory allocation request, step 604 is executed. In this scheme, when allocating memory for the user program, it first considers whether the memory pool of the runtime library meets the demand, and then considers whether the user space meets the demand. If both are not satisfied, the memory allocation wrapper in the user program allocates kernel space for the user program by itself to achieve the expansion of the user space. It can ensure that the user program does not abuse the kernel space, and ensure the rationality of the use of the kernel space.
[0210] Step 604: The runtime library triggers a memory allocation request to the kernel.
[0211] In a possible implementation method, after receiving the memory allocation request from the runtime library, the kernel performs the processing from step 505 to step 508 as described above, which will not be described in detail here.
[0212] Step 605: The runtime library receives the memory exhaustion indication information sent by the kernel, and sends the memory exhaustion indication information to the memory allocation wrapper.
[0213] In one possible implementation method, the kernel determines that the size of available memory in physical memory and swap memory does not meet the memory size indicated by the memory allocation request; or, the size of available memory in the virtual address space corresponding to the user space does not meet the memory size indicated by the memory allocation request, and sends memory exhaustion indication information to the runtime library.
[0214] Step 606: The memory allocation wrapper determines whether the size of the free memory block in the memory pool corresponding to the memory allocation wrapper satisfies the memory allocation request.
[0215] In a possible implementation method, if the size of the free memory block in the memory pool meets the memory allocation request, step 607 is executed; if the size of the free memory block in the memory pool does not meet the memory allocation request, step 608 is executed.
[0216] Step 607: the memory allocation wrapper takes out a memory block whose size is not less than the size requested by the user program from the free memory block list, and takes out a memory block whose size is equal to the size requested by the user program from the memory block.
[0217] In a possible implementation method, the memory allocation wrapper maintains a free memory block list, which records the information of free memory blocks in the memory pool.
[0218] In a possible implementation method, the memory allocation wrapper takes out a memory block equal to the size requested by the user program and allocates it to the user program, that is, step 611.
[0219] Step 608, determine whether there is a continuous area in the kernel space to meet the memory allocation request.
[0220] In a possible implementation method, a memory allocation wrapper determines whether there is a continuous area through a kernel space allocation table stored in itself, wherein the continuous area is located in the kernel space, and the memory size formed by the continuous area is greater than or equal to the memory size indicated by the memory allocation request; the kernel space allocation table is maintained by the memory allocation wrapper and is used to query whether there is unallocated continuous space in the kernel space.
[0221] In a possible implementation method, if there is a continuous area in the kernel space that meets the memory allocation request, step 609 is executed; if there is no continuous area in the kernel space that meets the memory allocation request, an indication of memory exhaustion is sent to the user program, that is, step 612 is executed.
[0222] Step 609: The memory allocation wrapper sets the state of the continuous area in the kernel space allocation table to allocated.
[0223] In one possible implementation method, when the memory allocation wrapper allocates kernel space, it does not send a memory allocation request to the kernel. Instead, the memory allocation wrapper records which parts of the kernel space have been allocated in the kernel space allocation table, and then finds an unallocated continuous area, marks its status as allocated, and directly returns the pointer. Therefore, unlike the conventional memory allocation method, the kernel knows nothing about such memory allocation. In this scheme, the kernel space allocation table is maintained by the user program itself, so the page corresponding to the extended user space in the kernel space allocation table is set to allocated, which can determine which addresses of the kernel space are occupied by the user program, so that the kernel space will not be allocated repeatedly, causing the program to crash. Furthermore, the expansion of the user space can be achieved without modifying the hardware and the kernel.
[0224] Step 610: The memory allocation wrapper takes out a piece of memory from the continuous area, the size of which is equal to the memory requested by the user program.
[0225] In a possible implementation method, if the memory block in the continuous area is larger than the memory requested by the user program, the memory allocation wrapper puts the remaining part into a free memory block list corresponding to the memory allocation wrapper.
[0226] In a possible implementation method, the memory allocation wrapper takes out a piece of memory with a size equal to the memory requested by the user program from the above continuous area, and allocates it to the user program, that is, step 611.
[0227] Step 611, the user program obtains the required memory.
[0228] Step 612: The user program receives the memory exhaustion indication information sent by the memory allocation wrapper.
[0229] In one possible implementation method, for releasing memory allocation, determine whether it is located in the extended user space based on the memory address. If the memory address is not located in the extended user space, directly call the memory release function (such as free()) to release the memory allocation; if the memory address is located in the extended user space, directly mark the corresponding area as unallocated in the kernel space allocation table maintained by itself. For the memory pool, the above method can also be used, that is, when the conditions for releasing the free memory block are met, mark the area corresponding to the free memory block list as unallocated.
[0230] Figure 7 The timing diagram of memory access exception processing provided in an embodiment of the present application includes the following steps:
[0231] Step 701: The user program accesses a virtual address located in the kernel space, triggering a page exception.
[0232] Step 702: The kernel saves context A of the user program when a page exception occurs.
[0233] Step 703: The kernel runs exception handling in the new context B.
[0234] Step 704: The kernel determines the type of page exception and determines that the page exception is caused by the user program unauthorized access to the kernel space.
[0235] Step 705 , the kernel triggers a segmentation fault signal SIGSEGV, and the user program has registered a handler corresponding to SIGSEGV.
[0236] In one possible implementation, the kernel calls the handler corresponding to SIGSEGV in a new context C and passes the context in the first parameter corresponding to the handler.
[0237] Step 706 : The signal processing function obtains context A from the first parameter, and obtains from context A the information required for simulation execution.
[0238] In one possible implementation method, the signal processing function determines that the page exception is caused by the user program's unauthorized access to the kernel space, and the corresponding memory access address is allocated in the kernel space allocation table, and the memory access behavior complies with the permission information in the memory space allocation table; if these conditions are not met, the signal processing function will not continue to complete the simulation execution, but may terminate the program execution.
[0239] Step 707: The signal processing function simulates the execution of the instruction that causes the page exception.
[0240] Step 708 : The signal processing function modifies context A using the simulation result to obtain context D.
[0241] Step 709: The signal processing function requests the kernel to use context D to resume normal execution of the user program through a system call.
[0242] Step 710, the kernel uses context D to resume normal execution of the user program.
[0243] Step 711, the user program resumes normal execution.
[0244] In a possible implementation method, in step 707, the signal processing function simulates the execution of the instruction that causes the page exception. The instruction includes two situations: one is that the virtual address corresponding to the instruction is located in the user space; the other is that the virtual address corresponding to the instruction is located in the kernel space.
[0245] The signal handler obtains the register data and the virtual memory address of the instruction that triggers the page exception and eventually triggers SIGSEGV from the ucontext, and then fetches the instruction from the address. Then the instruction is parsed to determine its behavior and the memory address and register data that need to be accessed. The virtual memory address where the instruction is located must be somewhere in the virtual memory space, that is, it may be in user space or in kernel space (that is, extended user space).
[0246] If the instruction is in user space, a page exception may be caused if and only if this instruction is a memory access instruction. Since we have determined that the page exception is caused by the user program unauthorized access to kernel space, it means that the memory access address is in kernel space. This is an obvious memory access to extended user space, and this is also the most common case. The signal handler only needs to simulate the memory access and then simulate the effect of the instruction (such as reading data from memory to registers and writing data in registers to memory). At this point, this instruction must be a memory access instruction that accesses extended user space. Based on this, simulated memory access will be performed during instruction simulation (unless it is detected that the corresponding memory access address is an unallocated extended user space, or there is a permission violation, such as Figure 3 ).
[0247] If the instruction is in the extended user space, that is, the PC is currently in the kernel space, so when the CPU fetches the instruction to be executed ("fetching"), it has to access the kernel space. This is an implicit memory access. There are actually two ways to use memory: 1) read / write data; 2) the CPU fetches instructions from it and executes them. If the extended user space is used for execution by a user program, such as jumping to an address in the extended user space through a jump instruction, a page exception will occur. From the CPU's perspective, the extended user space is the kernel space, and user-mode programs cannot execute machine code in the kernel space. Since this application allows the kernel space to be used as an extended user space for memory allocation, the signal handler must make special handling for the SIGSEGV caused in this case, that is, it is necessary to simulate memory access and simulate execution (the exception is the same as the previous example).
[0248] Based on the same technical concept, Figure 8 An exemplary embodiment of the present application provides a memory access exception processing device 800. Figure 8 As shown, it includes: a registration unit 801 and a processing unit 802.
[0249] The registration unit 801 is used for the user program to register a signal processing function for page exceptions and to allocate an extended user space in the kernel space to the user program; the signal processing function is used to replace the kernel's default processing method for the page exception; the processing unit 802 is used to trigger the signal processing function by the kernel after the user program triggers the page exception, and pass the context information corresponding to the page exception to the signal processing function; after determining that the page exception is the first memory access exception, the signal processing function simulates the execution of the instruction that triggers the first memory access exception; the first memory access exception is triggered when the user program accesses the extended user space.
[0250] In a possible implementation method, the processing unit 802 is also used to simulate the execution of the instruction that triggers the first memory access exception through the signal processing function to obtain a simulated execution result; feed back the simulated execution result to the kernel in the form of context information; and the kernel uses the new context information to resume the execution of the user program.
[0251] In a possible implementation method, the signal processing function includes a first parameter; the first parameter is used to obtain context information corresponding to the page exception through the kernel.
[0252] In a possible implementation method, the processing unit 802 is also used to indicate through a second parameter whether the kernel should use a backup stack when calling the signal processing function when the user program registers the signal processing function with the kernel, and the backup stack is different from the stack used when the first memory access exception is triggered.
[0253] In a possible implementation method, the registration unit 801 is used to register the signal processing function when the user program is started; or, to register the signal processing function when an extended user space is allocated to the user program for the first time.
[0254] In a possible implementation method, the above-mentioned device also includes an acquisition unit 803, and the acquisition unit 803 is used to obtain the memory allocation request triggered by the user program; the processing unit 802 is also used to determine whether there is a virtual address space in the kernel space that meets the memory allocation request after receiving the indication information of memory exhaustion sent by the kernel through the kernel space allocation table stored in itself; if so, use the virtual address space as the extended user space of the user program.
[0255] In one possible implementation method, the memory exhaustion indication information includes: the size of available memory in physical memory and swap memory does not meet the memory size indicated by the memory allocation request; or the size of available memory in the virtual address space corresponding to the user space does not meet the memory size indicated by the memory allocation request.
[0256] In a possible implementation method, the processing unit 802 is also used to determine whether there is a continuous area through the kernel space allocation table stored in itself, and the continuous area is located in the kernel space, and the memory size formed by the continuous area is greater than or equal to the memory size indicated by the memory allocation request; the kernel space allocation table is maintained by the user program and is used to query whether there is unallocated continuous space in the kernel space.
[0257] In a possible implementation method, the processing unit 802 is further configured to determine whether the size of the backup storage satisfies the memory size indicated by the memory allocation request.
[0258] In one possible implementation method, the processing unit 802 is also used to determine whether the remaining space on the physical disk satisfies the memory size indicated by the memory allocation request; or, the user program determines that the remaining space on the memory disk satisfies the memory size indicated by the memory allocation request, and the memory disk refers to a fixed or dynamically sized area divided by the operating system on the physical memory, virtual memory, or swap space, and the area has read and write permissions; or, the user program determines that the remaining space on the network storage satisfies the memory size indicated by the memory allocation request.
[0259] In a possible implementation method, the processing unit 802 is further configured to send the memory allocation request to the kernel after determining that there is no free memory block in the memory pool that satisfies the memory size required by the memory allocation request.
[0260] In a possible implementation method, the memory size corresponding to the memory allocation in the extended user space is greater than or equal to the memory size indicated by the memory allocation request;
[0261] The processing unit 802 is further used to put the remaining memory blocks in the extended user space into the memory pool; the remaining memory blocks refer to the portion of the memory size corresponding to the memory size used for this memory allocation in the extended user space minus the memory size indicated by the memory allocation request.
[0262] In a possible implementation method, the processing unit 802 is also used to allocate an extended user space in the kernel space to the user program through a runtime library corresponding to the user program; or, allocate an extended user space in the kernel space to the user program through a memory allocation wrapper in the user program.
[0263] In a possible implementation method, the processing unit 802 is also used to send a memory allocation request to the runtime library through the memory allocation wrapper in the user program; after the runtime library determines that there is no free memory block in the memory pool that meets the memory size indicated by the memory allocation request, it sends the memory allocation request to the kernel.
[0264] In a possible implementation method, the processing unit 802 is further configured to set a page corresponding to the extended user space in the kernel space allocation table as allocated, and return a starting address corresponding to the extended user space to the user program.
[0265] In a possible implementation method, the processing unit 802 is further configured to set the state of the page corresponding to the kernel space allocation table to unallocated.
[0266] Based on the same technical concept, the embodiment of the present application provides a memory access exception processing device 900, which can be a computing device, for example. Fig. 9 As shown, a memory access exception 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.
[0267] In an embodiment of the present application, the memory 902 stores instructions that can be executed by at least one processor 901, and the at least one processor 901 can execute the above-mentioned method for handling memory access exceptions by executing the instructions stored in the memory 902.
[0268] Among them, the processor 901 is a control center of a memory access exception processing device 900, which 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 application programs, and the modem processor mainly processes wireless communications. 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.
[0269] 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.
[0270] 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.
[0271] 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 handling memory access exceptions listed in any of the above-mentioned methods.
[0272] 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 handling memory access exceptions listed in any of the above-mentioned ways.
[0273] 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.
[0274] 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.
[0275] 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.
[0276] 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 A step that specifies a function in one or more boxes.
[0277] 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 handling memory access exceptions, characterized in that: include: User program registers signal processing function for page exception; The signal processing function is used to replace the default processing mode of the kernel for the page exception; Sending a memory allocation request to a runtime library through a memory allocation wrapper in the user program; After the runtime library determines that there is no free memory block in the memory pool that satisfies the memory size indicated by the memory allocation request, the runtime library sends the memory allocation request to the kernel; The kernel obtains the memory allocation request triggered by the user program; After receiving the indication information of memory exhaustion sent by the kernel, the user program determines whether there is a virtual address space in the kernel space that satisfies the memory allocation request through the kernel space allocation table stored in the user program; If it exists, the virtual address space is used as the extended user space of the user program; After the user program triggers the page exception, the kernel triggers the signal processing function and passes context information corresponding to the page exception to the signal processing function; After determining that the page exception is a first memory access exception, the signal processing function simulates execution of an instruction that triggers the first memory access exception; the first memory access exception is triggered when the user program accesses memory in the extended user space; Simulating the execution of the instruction that triggers the first memory access exception by the signal processing function to obtain a simulation execution result; Feedback the simulation execution result to the kernel in the form of context information; The kernel resumes execution of the user program using the new context information.
2. The method according to claim 1, characterized in that The transferring the context information corresponding to the page exception to the signal processing function comprises: The signal processing function includes a first parameter; the first parameter is used to obtain context information corresponding to the page exception through the kernel.
3. The method according to claim 1, characterized in that The user program registers a signal processing function for page exceptions, including: When the user program is started, register the signal processing function; or, The signal processing function is registered when the extended user space is allocated for the user program for the first time.
4. The method according to claim 3, characterized in that The registering of the signal processing function comprises: When the user program registers the signal processing function with the kernel, the kernel is instructed through a second parameter whether to use a backup stack when calling the signal processing function, and the backup stack is different from the stack used when the first memory access exception is triggered.
5. The method according to claim 1, characterized in that The memory exhaustion indication information includes: The size of the available memory in the physical memory and the swap memory does not satisfy the memory size indicated by the memory allocation request; or, The size of the available memory in the virtual address space corresponding to the user space does not satisfy the memory size indicated by the memory allocation request.
6. The method according to claim 1, characterized in that The determining whether there is a virtual address space in the kernel space that satisfies the memory allocation request by using the kernel space allocation table stored in the kernel space includes: The user program determines whether there is a continuous area through the kernel space allocation table stored in itself, the continuous area is located in the kernel space, and the memory size formed by the continuous area is greater than or equal to the memory size indicated by the memory allocation request; the kernel space allocation table is maintained by the user program and is used to query whether there is unallocated continuous space in the kernel space.
7. The method according to claim 6, characterized in that Before determining whether there is a virtual address space in the kernel space that satisfies the memory allocation request, the method further includes: The user program determines that the size of the backup storage satisfies the memory size indicated by the memory allocation request; the backup storage is the location where the data in the extended user space needs to be stored.
8. The method according to claim 7, characterized in that The user program determines that the size of the backup storage satisfies the memory size indicated by the memory allocation request, including: The user program determines that the remaining space on the physical disk satisfies the memory size indicated by the memory allocation request; or, The user program determines that the remaining space on the memory disk satisfies the memory size indicated by the memory allocation request, wherein the memory disk refers to an area of fixed or dynamic size allocated by the operating system on the physical memory, virtual memory or swap space, and the area has read and write permissions; or The user program determines that the remaining space on the network storage satisfies the memory size indicated by the memory allocation request.
9. The method according to claim 8, characterized in that The corresponding memory size for this memory allocation in the extended user space is greater than or equal to the memory size indicated by the memory allocation request; After the virtual address space is used as the extended user space of the user program, it also includes: Putting the remaining memory blocks in the extended user space into a memory pool; The remaining memory block refers to the portion of the memory size corresponding to the memory size used for this memory allocation in the extended user space minus the memory size indicated by the memory allocation request.
10. The method according to claim 1, characterized in that After using the virtual address space as the extended user space of the user program, the method further includes: The page corresponding to the extended user space in the kernel space allocation table is set to be allocated, and the starting address corresponding to the extended user space is returned to the user program.
11. The method according to claim 10, characterized in that The method further comprises: After the user program is executed or when the user program requests to release memory allocation, the pages corresponding to the allocated continuous areas in the kernel space allocation table are set to unallocated.
12. A computing device, 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 11 according to the obtained program instructions.
13. A computer-readable storage medium, 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 11 is implemented.
14. A computer program product, 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 11.
