A base address-based transparent transmission and compile-time constant-based operating system page table folding management method and system
By introducing first-level constants and base address pass-through mechanisms into the operating system page table software model, the problems of strong architectural coupling and root node misrelease caused by page table folding in existing technologies are solved, and a unified page table management and protection mechanism is realized.
Patent Information
- Application Number
- CN202611422391.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-14
- Publication Date
- 2026-11-27
AI Technical Summary
In existing technologies, operating systems rely on decentralized preprocessing macro masking mechanisms when handling page table folding, which leads to strong architectural coupling and the risk of root node accidental release, and lacks a unified folding layer protection mechanism.
By adopting a method based on base address pass-through and compile-time constants, a first-level constant is set in the multi-level page table software model. The folding level is identified by a preset folding value. Base address pass-through, allocation prohibition and reclamation protection are performed during page table traversal, page fault handling and reclamation to ensure unified code logic processing.
It achieves address pass-through, allocation prohibition, and reclamation protection in multi-level page table folding scenarios, eliminates the risks of strong architectural coupling and root node accidental release, and provides a unified operating system memory management solution.
Smart Images

Figure CN123178288A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of operating system memory management technology, and in particular to an operating system page table folding management method and system based on base address pass-through and compile-time constants. Background Technology
[0002] Operating systems typically employ a multi-level page table structure to manage the mapping from virtual addresses to physical addresses. As hardware architecture evolves, different processors support varying numbers of page table levels. To ensure unified management, the operating system establishes a page table model with a fixed number of levels at the software level, performing folding operations on levels not supported by the hardware.
[0003] In existing technologies, typical implementations of page table folding rely on decentralized preprocessing macro masking mechanisms. Specifically, for page table levels that do not exist in the hardware, their functionality is masked by defining the operation functions of that level as no-operations or returning zero values in architecture-related header files. However, this approach leads to strong coupling between general memory management code and the underlying architecture. A complete set of masking macros needs to be maintained for each new hardware architecture. Furthermore, during recursive page table reclamation, there is a lack of a unified folding layer protection mechanism, posing a risk of root nodes being mistakenly released. Summary of the Invention
[0004] To address the shortcomings of existing operating systems that rely on fragmented preprocessing macro masking mechanisms when handling page table folding, leading to strong architectural coupling, type safety violations, and the risk of accidental root node release, this application provides an operating system page table folding management method and system based on base address pass-through and compile-time constants. This method sets a first-level constant at each level in a multi-level page table software model and identifies the folding level with a preset folding value. During page table traversal, base address pass-through or actual dereferencing is performed based on the first-level constant. During page fault handling, the first-level constant determines whether to allocate the first physical page. During page table reclamation, the first-level constant determines whether to perform a release operation. This approach enables unified code logic to correctly handle address pass-through, allocation prohibition, and reclamation protection at the folding level. Specifically, this application provides the following technical solutions: This application provides an operating system page table folding management method, applied to operating system memory management including a multi-level page table software model, wherein each level in the multi-level page table software model has a first-level constant, and the first-level constant of the folding level is equal to a preset folding value. The method includes: When the first level constant of the parent layer is equal to the preset folding value, the pointer of the parent layer is passed through as the current layer base address; when the first level constant is not equal to the preset folding value, the child table base address is obtained from the page table entry of the parent layer, and the intra-layer offset is determined according to the virtual address to be accessed. When the first level constant of the parent layer is equal to the preset folding value, the allocation of the first physical page is skipped; when the first level constant is not equal to the preset folding value and the page table entry is empty, the first physical page is allocated and mounted as needed. If the first-level constant of the current layer is equal to the preset folding value, only the recycling request is passed to the next layer without performing a release operation; if the first-level constant is not equal to the preset folding value, if the current layer is a page table entry layer, the corresponding physical page mapping within the recycling range is cleared; if the current layer is not a page table entry layer, the page table physical pages of the next layer are released after the recycling of the next layer is completed.
[0005] Optionally, the first-level constants are determined based on the bit-field decomposition of the hardware virtual address, including: Allocate the index bit width according to the hierarchy of the effective bit width of the virtual address; The first level constant of each level is calculated as the number of entries corresponding to the index bit width of that level, and the index bit width of the folded level is zero.
[0006] Optionally, determining the intra-layer offset includes the following steps: Shift the virtual address to be accessed logically to the right by the current layer's bit offset; Based on the right shift result and the number of entries in the current level minus 1, a bitwise AND operation is performed to obtain the intra-level offset.
[0007] Optionally, the on-demand allocation of the first physical page includes: Request a physical page of the same size from the partner allocator as the first physical page, and perform zero-value initialization during allocation; If the buddy allocator is unable to provide free physical pages, it returns an error result to trigger out-of-memory processing.
[0008] Optionally, before the first physical page is mounted, the method further includes: A first memory barrier is executed to ensure that the initial contents of the first physical page are visible to other processing cores; After locking the page containing the page table entry of the parent layer, perform a second check on the page table entry; If the second check confirms that the page table entry is still empty, mount the first physical page; otherwise, return the first physical page to the partner allocator.
[0009] Optionally, releasing the first physical page of the next level includes: Read the physical page address of the next level from the current level's page table entry; Clear the current page table entry and set all its bit fields to zero. Return the first physical page corresponding to the physical page address to the partner allocator.
[0010] Optionally, the method further includes performing compile-time optimization steps: The functions at each level in the multi-level page table software model are expanded to the call point using inline tags; The compiler replaces the references to the first-level constants with integer literals and performs compile-time evaluation on the criteria for the folding level. Dead code elimination is performed on folded level code branches where the condition is always false, generating machine code corresponding to the actual number of page table levels in the hardware.
[0011] Secondly, this application provides an operating system page table folding management system, the system comprising: a first configuration module, configured to configure a first-level constant for each level in a multi-level page table software model, and set the first-level constant of the folding level to a preset folding value; The first traversal module is used to, during the page table traversal process, pass through the pointer of the parent layer as the current layer base address when the first level constant of the parent layer is equal to the preset fold value, and when the first level constant is not equal to the preset fold value, obtain the sub-table base address from the page table entry of the parent layer and determine the intra-layer offset according to the virtual address to be accessed. The first allocation module is used to skip the allocation of the first physical page when the first level constant of the parent layer is equal to the preset folding value during the page fault handling process, and to allocate the first physical page as needed when the first level constant is not equal to the preset folding value and the page table entry is empty. The first recycling module is used to, during the page table recycling process, if the first level constant of the current layer is equal to the preset folding value, only pass the recycling request to the next level without performing a release operation; if the first level constant is not equal to the preset folding value, release the first physical page of the next level after the recycling of the next level is completed.
[0012] Optionally, when the first level constant of the parent layer is equal to the preset fold value, the first traversal module performs base address pass-through without dereferencing the pointer of the parent layer.
[0013] Thirdly, this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described above.
[0014] The beneficial effects of the technical solution provided in this application are as follows: By introducing a first-level constant for each level in the multi-level page table software model and identifying the folded level with a preset folding value, base address pass-through is performed during page table traversal to avoid invalid dereferences to non-existent levels. During page fault handling, the first physical page allocation of the folded level is skipped based on the first-level constant to prevent erroneous overwriting of the root node. During page table reclamation, the folded level only passes through reclamation requests without performing release operations, thus naturally forming root node protection. This achieves correct handling of address pass-through, allocation prohibition, and reclamation protection in multi-level page table folding scenarios with unified code logic, eliminating the strong architectural coupling and root node erroneous release risks caused by the architecture-related distributed preprocessing macro masking mechanism in the prior art. Attached Figure Description
[0015] Figure 1 This is a schematic diagram of the overall process of the operating system page table folding management method provided in the embodiments of this application.
[0016] Figure 2 A schematic diagram showing the mapping between the five-level software page table model and the hardware page table hierarchy provided in the embodiments of this application.
[0017] Figure 3 This is a schematic diagram illustrating the full-link allocation simulation of page fault handling provided in the embodiments of this application.
[0018] Figure 4 This is a schematic diagram illustrating the recursive recycling and root node firewall effect provided in the embodiments of this application.
[0019] Figure 5 This is a schematic diagram of the operating system page table folding management system module architecture provided in an embodiment of this application. Detailed Implementation
[0020] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below in conjunction with the accompanying drawings and specific embodiments. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0021] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this application, "multiple" means two or more, unless otherwise explicitly specified.
[0022] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.
[0023] The operating system page table folding management method and system described in this application can be applied to various electronic devices that require virtual address to physical address mapping management, including but not limited to servers, desktop computers, laptops, workstations, embedded computing platforms, routers, network switches, and virtualized hosts deployed in data centers. In terms of deployment scenarios, the method and system are applicable to the memory management subsystem of a general-purpose operating system kernel, the second-stage address translation management of a hypervisor, the address space management of an embedded real-time operating system, and the memory isolation management of a container runtime environment.
[0024] The operating system page table folding management method described in this application operates in the privileged mode of the processor. The processor supports a hardware multi-level page table traversal mechanism, and its page table root directory base address is specified through a dedicated register. The processor, in conjunction with the memory management unit, performs hardware translation from virtual address to physical address. During the startup phase, the operating system kernel completes the detection and initialization of physical memory, establishes a buddy allocator to manage free physical pages, and maintains an independent page table root directory for each user process. When the memory management unit encounters a missing page table entry, it generates a page fault exception, triggering the operating system's page fault handling process. The buddy allocator organizes all available physical memory into a free page linked list according to powers of 2, supporting allocation and release operations from order 0 (single page) to the maximum order (contiguous physical page block). During allocation, a physical page is retrieved from the free linked list of the corresponding order; during release, the physical page is returned to the free linked list of the corresponding order and attempts to merge it with adjacent physical pages into a contiguous page block of a higher order.
[0025] like Figure 1 As shown, the operating system page table folding management method provided in this application embodiment includes the following steps: S100, during page table traversal, when the compile-time level constant (i.e., the first level constant) of the parent layer is equal to the preset folding value, the pointer of the parent layer is passed through as the current layer base address; when the compile-time level constant is not equal to the preset folding value, the sub-table base address is obtained from the page table entry of the parent layer, and the layer offset is determined according to the virtual address to be accessed.
[0026] like Figure 2 As shown, this embodiment uses a five-level software page table model as the specific implementation of the multi-level page table software model. The five-level software page table model includes, from top to bottom, a global page directory layer, a fourth-level page directory layer, a parent page directory layer, a middle-level page directory layer, and a page table entry layer.
[0027] In this multi-level page table software model, each level defines a compile-time level constant, which represents the number of entries that a page in that level's page table can hold. The compile-time level constant for a folded level is set to a preset folding value. In this embodiment, the preset folding value is 1, meaning that when the compile-time level constant for a certain level is equal to 1, that level does not have a corresponding physical level in the hardware page table structure and is considered a folded level.
[0028] In another embodiment, the preset folding value can also be implemented by setting it to 0, in which case the compile-time layer constant equal to 0 is used as the folding criterion. In the embodiment where the preset folding value is 0, the masking operation of the intra-layer offset needs to be adjusted accordingly: because When generating a value of all 1s in unsigned integer representation (overflowing to the maximum value), the mask cannot be directly obtained by subtracting 1 from the compile-time level constant. An alternative mask calculation method is to explicitly set the mask to 0 when the compile-time level constant is equal to 0 to ensure that the in-layer offset is always 0; when the compile-time level constant is not equal to 0, the mask remains the compile-time level constant minus 1.
[0029] The implementation method with a preset fold value of 1 allows the mask to naturally return to zero without additional conditional checks. This approach is simpler than the implementation with a preset fold value of 0. Regarding allocation determination, when the preset fold value is 0, the folding criterion is... Change to While logically equivalent, the readability and semantics of the decision conditions ("zero entries mean non-existence" versus "one entries mean degradation") vary depending on engineering preferences.
[0030] The compile-time hierarchical constants are determined based on the bit-field decomposition of the hardware virtual address. Taking a configuration with a page size of 4096 bytes as an example, the page offset field occupies the lower 12 bits of the virtual address, i.e. The remaining valid bits of the virtual address are allocated sequentially from lowest to highest level to the page table entry level, intermediate page directory level, upper-level page directory level, fourth-level page directory level, and global page directory level as index bit fields for each level. The compile-time level constant for each level is equal to the number of entries corresponding to the index bit width of that level, calculated using the following formula: ,in hierarchical The index bit width. The index bit width of a folding level that does not exist in the hardware is 0, thus obtaining the compile-time level constant. , which is equal to the preset folding value.
[0031] For example, a hardware architecture with a 39-bit virtual address and a page size of 4096 bytes is used for specific illustration. The hardware page table in this architecture has a 3-level structure. The 39 valid bits of the virtual address are decomposed as follows: the lower 12 bits are the page offset; bits 12 to 20 are the page table entry level index field (9 bits, compile-time level constant of 512); bits 21 to 29 are the intermediate page directory level index field (9 bits, compile-time level constant of 512); and bits 30 to 38 are the parent page directory level index field (9 bits, compile-time level constant of 512). The global page directory level and the fourth-level page directory level do not have a physical level in this hardware architecture; their index bit width is 0, and their compile-time level constant is 1, which is equal to the preset folding value. It can be verified that the sum of the page offset bit width and the index bit width of each level is equal to the valid bit width of the virtual address. The product of the compile-time level constants of each level equals the number of addressable pages. Page, corresponding A byte is equivalent to 512GB of virtual address space.
[0032] In another implementation, the compile-time hierarchical constants can also be implemented through a generic constant parameter mechanism, that is, the number of entries is passed as a compile-time generic constant parameter to the type definition of the page table type, so that each architecture configuration generates a corresponding page table type instance, thus maintaining the compile-time constant characteristics.
[0033] For example, in a hardware architecture with a 48-bit virtual address and a page size of 4096 bytes, the hardware page table of this architecture has a 4-level structure. The configuration of each layer of the five-level software page table model is as follows: the index width of the global page directory layer is 0, and the compile-time level constant is 1 (folded level); the index width of the fourth-level page directory layer is 9 bits, and the compile-time level constant is 512; the index width of the upper-level page directory layer is 9 bits, and the compile-time level constant is 512; the index width of the middle-level page directory layer is 9 bits, and the compile-time level constant is 512; the index width of the page table entry layer is 9 bits, and the compile-time level constant is 512. It can be verified that the sum of the page offset width and the index width of each level is equal to the effective width of the virtual address. .
[0034] Compared to the 39-bit virtual address architecture, the 48-bit virtual address architecture has only one collapsed layer (instead of two), making the fourth-level page directory layer an actual, non-collapsed layer. During page table traversal in this architecture, only the global page directory layer performs one base address pass-through operation; the other four layers perform actual physical page address reading and index offset calculation. During page fault handling in this architecture, a maximum of three physical page table pages (i.e., the first physical page) need to be dynamically allocated (corresponding to the sub-page tables of the fourth-level page directory layer, the parent page directory layer, and the intermediate page directory layer, respectively), which is one more allocation operation than in the 39-bit virtual address architecture.
[0035] The page table traversal process decomposes the operation of locating the target page table entry into two stages: obtaining the base address of the current layer page table and determining the offset within the layer.
[0036] The acquisition of the base address depends on whether the parent layer's compile-time hierarchy constant is equal to the preset folding value. When the parent layer's compile-time hierarchy constant is equal to the preset folding value, the parent layer does not have a real page table in the hardware, and the passed-in parent layer pointer actually points directly to the lower-level valid page table. Here, the parent layer's pointer is directly used as the base address of the current layer through type-safe pointer conversion; this is the base address pass-through operation, and no dereference operation is performed on the parent layer's pointer.
[0037] The reason for choosing type-safe pointer conversion over direct address value copying is that type-safe pointer conversion preserves the pointer's type information, allowing the compiler to perform type checking and alignment verification on the converted pointer and catch incorrect type usage at compile time (for example, if a page table entry level pointer is mistakenly converted to an intermediate page directory level pointer, the compiler will report a type mismatch compilation error). In contrast, direct address value copying degenerates the pointer into an untyped integer value, losing the ability to perform compile-time type checking.
[0038] It should be noted that the type-safe pointer conversion is equivalent to zero-overhead register value passing at the compilation artifact level. Since the source pointer and the target pointer point to the same memory address and the memory layout remains unchanged, the pointer conversion does not generate any machine instructions, but only changes the associated type label of the pointer in the compiler's type system.
[0039] When the parent layer's compile-time hierarchy constant is not equal to the preset folding value, the parent layer has a real physical page table. The method reads the physical page address of the child layer's page table recorded in the parent layer's page table entry, converts the physical page address into a kernel virtual address, and obtains the child table base address. The conversion from physical page address to kernel virtual address is implemented in the operating system kernel through a pre-established linear mapping relationship; that is, the physical address plus a fixed linear mapping offset yields the corresponding kernel virtual address. This linear mapping is established during kernel initialization, covering the entire physical memory range, so that any physical page address can be converted into a directly accessible kernel virtual address within a constant time.
[0040] The intra-layer offset is obtained by performing a bitwise shift operation on the virtual address to be accessed and then calculating the offset range corresponding to the compile-time layer constant. Specifically, the value of the virtual address to be accessed is logically right-shifted by the current layer's bit offset, and the resulting intermediate value is then bitwise ANDed with the result of subtracting 1 from the current layer's entry count. When the compile-time level constant is a power of 2, the binary representation of the compile-time level constant minus 1 is a value of all 1s for the corresponding number of bits. This operation is equivalent to extracting a bit segment from the current level index field of the virtual address to be accessed. When the compile-time level constant is equal to the preset folding value 1, The mask is all 0s, and the result of the bitwise AND operation is always 0, meaning the intra-layer offset of the folded level is always 0. The base address is then used to perform a pointer offset operation based on the intra-layer offset to obtain the pointer address of the target page table entry.
[0041] For example, the virtual address is traversed using the 39-bit virtual address architecture. The page table traversal process is illustrated using an example. In the offset calculation from the global page directory layer to the fourth-level page directory layer, the compile-time level constant of the global page directory layer is 1, equal to the preset folding value. The method directly passes the pointer of the process root page table to the base address of the fourth-level page directory layer without performing dereferencing. The compile-time level constant of the fourth-level page directory layer is also 1, and the method continues to pass this pointer to the base address of the parent page directory layer. In both pass-through operations, the intra-layer offset is... The base address pointer value remains unchanged. The compile-time level constant of the upper-level page directory layer is 512, which is not equal to the preset folding value. The method reads the physical page address from the page table entry of this layer and converts it to a virtual address to obtain the base address of the intermediate-level page directory layer. The offset within the layer is... And so on, the in-level offset of the intermediate page directory is... The in-layer offset of the page table entry layer is .
[0042] In another implementation, the base address pass-through can also be achieved by directly copying the address value, that is, directly copying the address value of the parent level pointer to the address value of the current level base address without relying on pointer conversion of the type system. This method is functionally equivalent but abandons the protection of compile-time type checking.
[0043] In another embodiment, the sub-table base address can also be obtained through direct offset mapping, i.e., there is a linear mapping relationship between physical addresses and virtual addresses with a fixed offset, and address translation is completed by adding or subtracting the fixed offset. The fixed offset is determined during the operating system kernel initialization phase and remains unchanged throughout the entire operation; its value is equal to the virtual starting address of the kernel linear mapping region minus the starting address of the physical memory.
[0044] The direct mapping offset method simplifies the conversion from physical address to virtual address to a single addition instruction (the virtual address is obtained by adding the fixed offset to the physical address), which reduces one memory access operation compared to the method of querying and converting through the page descriptor array.
[0045] S200, during page fault handling, when the compile-time hierarchical constant of the parent layer is equal to the preset folding value, the allocation of physical page table pages is skipped; when the compile-time hierarchical constant is not equal to the preset folding value and the page table entry is empty, the physical page table pages are allocated and mounted as needed.
[0046] like Figure 3 As shown, the page fault handling process is triggered when a page fault exception occurs in the memory management unit. The method receives the page table entry pointer of the parent layer and the user virtual address that triggered the page fault exception as input, and determines whether the current level needs to dynamically allocate physical page table pages based on the compile-time level constant of the parent layer.
[0047] When the compile-time level constant of the parent layer equals the preset folding value, the parent layer does not have a physical page table in the hardware, and the current level is the root node of the hardware page table. The root node of the hardware page table is statically allocated by the process descriptor initialization logic when the user process is created, and its lifecycle is bound to the entire runtime of the user process. The method directly skips the allocation logic of the physical page table, calls the offset calculation function in the page table traversal process, and returns the pointer to the current layer page table entry through base address pass-through.
[0048] When the compile-time hierarchical constant of the parent layer is not equal to the preset folding value, the parent layer has a real physical page table, and the method enters the allocation determination phase. The method first checks if the page table entry of the parent layer is empty. If the page table entry is empty, it means that the current layer does not yet have a corresponding physical page table and needs to be dynamically allocated. If the page table entry is not empty, it means that the physical page table of the current layer has been mounted, and the method directly jumps to offset calculation and returns the pointer to the current layer's page table entry.
[0049] When a page table entry is empty and needs to be allocated, the method requests a physical page of size equal to the page size from the buddy allocator as a new physical page table page. The newly allocated physical page table page is zero-initialized during allocation, ensuring that all entries in the physical page table page are empty. This zero-initialization is necessary because if the initial content of the physical page table page is random, any non-zero entry will be interpreted as a valid physical address mapping, leading to incorrect mapping of virtual addresses to unexpected physical memory regions.
[0050] For example, taking a page size of 4096 bytes as an example, each physical page table page can hold Each page table entry is 8 bytes long. The zero-value initialization operation sets all 4096 bytes of the physical page to 0. The zero-value initialization is implemented by immediately calling a memory zeroing function after the buddy allocator allocates the physical page, filling the entire physical page with zero values. This memory zeroing function is typically provided by an optimized implementation in the processor architecture; for example, multiple bytes can be zeroed at once using vector storage instructions, requiring only a few dozen machine instructions to complete the full page zeroing for a 4096-byte physical page.
[0051] If the buddy allocator cannot provide a free physical page (physical memory exhausted), the method immediately returns an error result to trigger out-of-memory handling. The buddy allocator manages the allocation and release of physical pages in units of order. The allocation order of the physical page table page is 0, i.e., a single physical page, and the corresponding memory size is equal to the page size (4096 bytes in a 4096-byte page size configuration). The allocation with an order of 0 is the smallest granularity allocation unit in the buddy allocator and will not generate internal fragmentation. The error result is received by the operating system's out-of-memory handling mechanism, which can choose to terminate the user process occupying the most memory to release physical memory, or directly terminate the current process that triggered the page fault. Before the execution of the out-of-memory handling mechanism, intermediate-level physical page table pages that have been allocated but not mounted in the page fault handling process do not need to be rolled back and released, because the allocation and mounting operations of each level are completed within the same function. When allocation fails, no physical pages have been allocated in that level. The physical page table pages of the preceding layer that have been successfully mounted become valid components of the page table structure and will be released uniformly in the subsequent page table reclamation process.
[0052] After the physical page table is successfully allocated, the method performs a memory barrier operation to ensure that the zero-value initialization of the physical page table is visible to other processing cores. The memory barrier is a write memory barrier, its function being to ensure that all write operations completed before the memory barrier (i.e., the zero-value initialization) are visible to all processing cores in the system before any write operations after the memory barrier (i.e., writing the physical page address to the parent page table entry). It should be noted that if the memory barrier is placed after the mount operation rather than before, in a processor architecture with weak memory ordering, other processing cores may access the uninitialized physical page table through the updated parent page table entry, interpreting the uninitialized random data as a physical address and performing memory access, leading to a kernel crash.
[0053] After executing the memory barrier, the physical page containing the page table entry of the parent layer is locked. The locking operation employs a fine-grained page-level spinlock, where the spinlock locks the physical page descriptor containing the parent layer's page table entry, rather than a global lock or hierarchical lock. The spinlock uses a busy-wait strategy when lock contention occurs; that is, the processing core that has not acquired the lock continuously executes atomic comparison instructions to continuously check the lock status, without actively yielding processor time slices. It should be noted that the lock holding operation only involves one page table entry null check and one pointer write, with a very short holding time (exemplarily, no more than tens of clock cycles on a typical processor), and the overhead of busy-waiting is lower than the overhead of context switching. Taking a hierarchy with a compile-time hierarchy constant of 512 as an example, a physical page table contains 512 page table entries. The page-level spinlock ensures that 512 concurrent page faults will not block each other as long as they target different page table entries. Lock contention only occurs when two processing cores simultaneously trigger a page fault on the same empty page table entry of the parent layer.
[0054] After acquiring the spinlock, the method performs a secondary check on the page table entry of the parent layer to re-determine whether the page table entry is still empty. The purpose of this secondary check is to prevent duplicate allocation in a concurrent environment. Within the time window of allocating physical pages and acquiring spinlocks as described in the method, another processing core may have already completed the allocation and mounting operation for the same page table entry. If the secondary check confirms that the page table entry is still empty, the method writes the physical address of the newly allocated physical page table page to the parent layer's page table entry, sets the protection bit of the page table entry to indicate that the entry points to the next-level page table, completes the mounting operation, and then releases the spinlock. If the secondary check finds that the page table entry has been mounted by another processing core (and is no longer empty), the method releases the spinlock and then returns the physical page table page it just allocated to the buddy allocator to avoid memory leaks. The return operation of the physical page table page must be performed after the spinlock is released to avoid performing potentially blocking memory release operations during the lock holding period.
[0055] For example, a user process using the 39-bit virtual address architecture accesses the virtual address for the first time. Taking a page fault as an example, the hierarchical allocation process of the physical page table is explained. In the allocation determination from the global page directory layer to the fourth-level page directory layer, the compile-time hierarchical constant of the global page directory layer is 1, equal to the preset folding value, and the method skips allocation and directly performs base address pass-through. The compile-time hierarchical constant of the fourth-level page directory layer is also 1, and the method again skips allocation and performs base address pass-through.
[0056] In the allocation from the upper-level page directory layer to the middle-level page directory layer, the compile-time level constant of the upper-level page directory layer is 512, which is not equal to the preset folding value. The method checks the page table entry (index position 511) of the upper-level page directory layer and finds that the page table entry is empty. It then requests a 4096-byte physical page from the buddy allocator as the physical page table page of the middle-level page directory layer, executes a write memory barrier, locks the page where the page table entry of the upper-level page directory layer is located, and mounts it after a second check confirms that it is still empty.
[0057] In the allocation from the intermediate page directory layer to the page table entry layer, the compile-time level constant of the intermediate page directory layer is 512. The method also checks the corresponding page table entry (index position 256), and after confirming that it is empty, allocates and mounts the physical page table page of the page table entry layer. In the five-level function call, the allocation of the first two levels is skipped because the compile-time level constant is equal to the preset folding value. The allocation of the last two levels is one 4096-byte physical page, for a total of two physical page table pages occupying 8192 bytes, which is consistent with the expected overhead of the 39-bit virtual address hardware three-level page table architecture.
[0058] In an alternative implementation, the allocation of physical page tables can employ a pre-allocation pooling scheme. This involves pre-allocating a number of physical page tables to the per-core cache pool when the user process is created. During page fault handling, pages are retrieved from this cache pool instead of directly invoking the buddy allocator, thereby reducing allocation latency and lock contention. The size of the per-core cache pool can be configured based on the process's expected virtual address space usage. For example, pre-allocating 4 to 8 physical page tables for a typical user process can cover the common pattern of initial mapping.
[0059] In another alternative implementation, the concurrency safety mechanism can also use a compare-swap atomic operation instead of the spinlock plus secondary check mode. That is, a single compare-swap atomic instruction directly writes the physical address of the physical page table to the parent layer's page table entry. The compare-swap atomic instruction receives an expected old value (null) and a new value (the physical address of the allocated physical page) as parameters, and atomically compares the current value of the parent layer's page table entry with the expected old value: if they are equal, the new value is written to the page table entry and success is returned; if they are not equal, the page table entry is not modified and the current actual value is returned. If failure is returned, the method determines it as a concurrency conflict, returns the allocated physical page table to the buddy allocator, and continues subsequent operations using a physical page table already mounted by another processing core.
[0060] S300, during the page table reclamation process, when the compile-time level constant of the current layer is equal to the preset folding value, only the reclamation request is passed to the next level without performing a release operation; when the compile-time level constant is not equal to the preset folding value, the physical page table of the next level is released after the reclamation of the next level is completed.
[0061] like Figure 4 As shown, the page table reclamation process is triggered when a user process releases a virtual address mapping or when the process exits. The method receives a pointer to the current layer's page table entry and the range of virtual addresses to be reclaimed as input, and processes each page table entry of the current layer according to the virtual address range.
[0062] The method first calculates the starting page table entry offset and address boundaries of the virtual address range in the current layer. The calculation of the address boundaries is implemented using an address alignment function, which takes the currently processed virtual address and the ending address of the reclaimed range as input, and returns the end point of the address interval covered by the current page table entry. Specifically, the address alignment function aligns the current virtual address upwards to the address granularity boundary covered by each page table entry in the current layer, where the address granularity is equal to... That is, the address span corresponding to the sum of the starting bit and the width of the index bit in the current layer index field; if the alignment result exceeds the termination address of the recycling range, then the termination address is taken as the actual boundary.
[0063] For the folding level (compile-time level constant is 1), since the index bit width is zero, the address granularity covered by each virtual entry is equal to the granularity covered by a single entry in the next level multiplied by the compile-time level constant of the next level. The address alignment function returns the terminating address after one calculation, so that the traversal loop is executed only once to complete the pass-through. The method traverses all page table entries within the reclamation range with the address granularity as the step size.
[0064] For each page table entry in the current layer, the method branches based on the compile-time layer constant of the current layer. When the compile-time layer constant equals the preset folding value, the current layer is a folded layer, and the method executes the transparency layer rule. The core constraint of the transparency layer rule is: The folded hierarchy acts only as a pass-through channel throughout the entire reclamation process, passing the reclamation request to the reclamation function of the next lower level without modification. It does not perform any physical page address reading operations or any physical page release operations itself.
[0065] It should be noted that the compile-time level constant of the folded level is 1, and the folded level has only one virtual entry (with an intra-level offset always of 0), and there are no actual physical page table pages dynamically allocated by the buddy allocator. The pointer passed to the folded level is actually the result of base address pass-through; this pointer points to the valid page table page at the next lower level, not to a page table entry. If a physical page address read operation is performed on this pointer, a meaningless address value will be obtained; if a release operation is further performed on this meaningless address value, the valid page table page at the next lower level will be released, leading to catastrophic page table structure corruption.
[0066] When the compile-time level constant is not equal to the preset folding value, the current level is an unfolded level and contains actual physical page tables. The method first checks if the current page table entry is empty. If the current page table entry is empty, it means that the virtual address range covered by this entry has never been mapped and does not need to be reclaimed; the method moves the pointer to the next page table entry to continue processing. If the current page table entry is not empty, the method recursively calls the reclamation function of the next level, passing in the sub-level page table pointed to by the current page table entry and the corresponding virtual address sub-range. The purpose of the recursive call is to first clear all valid entries in the sub-level page table of the next level, ensuring that the sub-level page table no longer contains valid physical address mappings before being released.
[0067] After the recursive recycling at the next level returns, the method performs a parent-child cleanup operation. The specific steps of the parent-child cleanup operation are as follows: The method reads the next-level physical page address recorded in the current page table entry and converts the physical page address into a page descriptor recognizable by the buddy allocator. The method then clears the current page table entry, setting all bit fields of the entry to zero, restoring it to an empty state. This clearing operation is performed before the release operation, ensuring that even if a processor interrupt occurs during the release process, other processing cores will not access the physical page being released through the cleared page table entry.
[0068] If executed in reverse order—that is, releasing the physical page first and then clearing the page table entry—the page table entry will still point to the released physical page within the time window between the release and clearing. Other processing cores' page table traversal operations may read this dangling pointer and access the released memory region. Finally, the method calls the buddy allocator's release interface to return the next-level physical page table entry to the buddy allocator's free page pool. After the release is complete, the method advances the current-level page table entry pointer to the next entry and continues processing subsequent page table entries within the reclamation scope.
[0069] The page table reclamation process establishes a root node firewall effect. Taking the 39-bit virtual address architecture as an example, the physical pages of the upper-level page directory layer are the root nodes of the hardware page tables, and their lifecycle is bound to the user process. During the page table reclamation process, the intermediate-level page directory layer (non-folding layer, with a compile-time layer constant of 512) reads and releases the physical page addresses of the page table entry layer recorded in its page table entries after completing the recursive reclamation of the physical pages of the page table entry layer.
[0070] After recursively reclaiming the physical pages of the intermediate page directory layer, the parent page directory layer (non-collapsed layer, compile-time layer constant 512) also performs the parent-child cleanup operation, releasing the physical pages of the intermediate page directory layer. The fourth-level page directory layer (collapsed layer, compile-time layer constant 1) only passes the reclamation request to the parent page directory layer during the reclamation process, without performing any physical page read or release operations. The global page directory layer (collapsed layer, compile-time layer constant 1) also only passes the reclamation request. Since both the fourth-level page directory layer and the global page directory layer are collapsed layers, there is no operation that releases the physical pages of the parent page directory layer. The physical pages of the parent page directory layer (i.e., the hardware root page table) are naturally retained throughout the reclamation process, without requiring any explicit root node protection flags or reference counting mechanisms. The root node is ultimately released uniformly by the process descriptor destructor of the user process after the process has completely exited.
[0071] The root node firewall effect holds true under any number of consecutive folded layers because the transparency rule of the folded layer depends only on whether the current layer's own compile-time layer constant is equal to the preset fold value, and does not depend on the folding state of adjacent layers. Therefore, the pass-through operation of multiple consecutive folded layers forms a serial transmission chain, and each layer independently satisfies the constraint of not performing a release operation.
[0072] For example, in a hardware architecture with 30-bit virtual addresses and 4096-byte page sizes, the global page directory layer, fourth-level page directory layer, and upper-level page directory layer of the five-level software page table model are all collapsed levels (all with a compile-time level constant of 1), while only the intermediate page directory layer and page table entry layer are non-collapsed levels (all with a compile-time level constant of 512). During the reclamation process of this architecture, the three consecutive collapses of the global page directory layer, fourth-level page directory layer, and upper-level page directory layer only perform pass-through operations, and the intermediate page directory layer (hardware root node) is naturally retained without being released. This generalization characteristic ensures that the same code produces correct reclamation behavior in all scenarios, including 2-level collapse (39-bit virtual address), 1-level collapse (48-bit virtual address), 3-level collapse (30-bit virtual address), and even zero-level collapse (57-bit virtual address, where the reclamation logic degenerates into a pure upper-level cleanup of lower-level operations when all five levels are non-collapsed).
[0073] For example, the virtual address range is reclaimed using the 39-bit virtual address architecture. Taking an example, the complete chain of the recursive reclamation is illustrated. The reclamation scope covers a 2MB virtual address space, corresponding to the address range governed by the 511th entry of the parent page directory layer.
[0074] The global page directory layer (with a compile-time level constant of 1) executes the transparency layer rule, passing through to the fourth-level page directory layer. The fourth-level page directory layer (with a compile-time level constant of 1) also executes the transparency layer rule, passing through to the parent page directory layer. If the 511th page table entry of the parent page directory layer (with a compile-time level constant of 512) is not empty, the garbage collection function of the intermediate page directory layer is recursively called. If the 0th page table entry of the intermediate page directory layer (with a compile-time level constant of 512) is not empty, the garbage collection function of the page table entry layer is recursively called. The page table entry layer clears all physical page mappings corresponding to the garbage collection range and returns. The intermediate page directory layer performs a parent-child cleanup operation, reads the physical page address recorded in the 0th page table entry, clears the page table entry, and releases the physical page table of the page table entry layer. The parent page directory layer checks whether all intermediate page directories governed by the 511th page table entry are empty. The null value check is performed by traversing all page table entries of the sub-level page table one by one: Null value checks are performed on each page table entry in the physical page table of the intermediate page directory layer. If all page table entries are zero, the sub-layer page table is considered to have been completely reclaimed. If all are empty, the parent page directory layer also releases the physical page table of the intermediate page directory layer and clears the 511th page table entry. The physical page of the parent page directory layer itself cannot be released (folded layers do not perform release) and is retained.
[0075] In another embodiment, the upper-level cleanup of lower-level operations can also adopt a bottom-up release method, that is, each level releases its own physical page table after recursion, instead of being released by the parent level. This method requires setting an additional protection flag for the root node page table to prevent it from being released by itself, which increases runtime judgment overhead compared to the method in this embodiment where folded layers naturally do not perform release operations.
[0076] In another alternative implementation, the root node protection can also be achieved by maintaining an atomic reference count for each physical page table page, automatically triggering release when the reference count reaches zero. This reference counting scheme suffers from over-design in page table scenarios because the tree structure of the page table dictates that each child page table page is referenced by exactly one parent page table entry, resulting in a constant reference count of 1.
[0077] S400 utilizes the characteristic that the compile-time level constant is a compile-time constant to enable the compiler to perform dead code elimination on the code branches of the folded level during the optimization phase, generating machine code that strictly corresponds to the actual number of page table levels in the hardware.
[0078] The compile-time constant characteristic of the aforementioned compile-time hierarchy constants enables the compiler's optimization pipeline to automatically identify and trim all code paths at the folding hierarchy. The optimization pipeline executes the following stages sequentially: During the function inlining phase, the functions at each level involved in page table traversal, page fault handling, and page table reclamation are expanded to the respective call points using forced inlining markers. After expansion, the specific compile-time level constant values for each level are directly visible in the call point context. These forced inlining markers are a necessary prerequisite for compile-time optimization because, by default, compiler constant propagation does not cross function call boundaries. If these functions are not inlined, the compiler only sees the function signatures at the call points and cannot determine the specific values of the compile-time level constants; therefore, constant folding and dead code elimination cannot be initiated.
[0079] During the constant propagation phase, the compiler identifies the compile-time level constant as a compile-time determinable constant value and replaces all references to the compile-time level constant with specific integer literals. This constant propagation not only replaces the compile-time level constant itself but also recursively replaces derived expressions that depend on it. For example, subtracting 1 from the compile-time level constant in a mask expression is also replaced with a specific literal (e.g., 512 minus 1 is replaced with 511).
[0080] During the constant folding phase, the compiler performs compile-time evaluation on comparison expressions that contain only constants, evaluates the decision condition of the folding level (the compile-time level constant is equal to the preset folding value) to a compile-time Boolean constant true value, and evaluates the decision condition of the non-folding level to a compile-time Boolean constant false value.
[0081] During the dead code elimination phase, the compiler deletes the entire code branch where the condition is always false, retaining only the code branch where the condition is always true. Dead code elimination not only deletes the instruction sequence within the branch, but also deletes local variable definitions and intermediate calculation results referenced only by that branch, ensuring that physical page address read functions, virtual address translation functions, and physical page release functions at the folded level have no corresponding machine instructions in the compilation artifacts.
[0082] For example, the page table traversal process of the 39-bit virtual address architecture is used as an example to illustrate the compile-time optimization effect. The offset calculation function from the global page directory layer to the fourth-level page directory layer contains complete code for the base address pass-through branch and the actual dereference branch before compilation. After constant propagation, the compiler replaces the compile-time level constant of the global page directory layer with the literal 1, and the constant folding condition is determined... If the evaluation is true, after dead code elimination, all code in the actual dereference branch is deleted, and only the type conversion operation passed through the base address is retained.
[0083] At the machine code level, the type conversion operation is equivalent to a zero-overhead address pass instruction; the physical page address read and virtual address translation code in the original function are completely absent from the generated binary file. The offset calculation function of the fourth-level page directory layer undergoes the same optimization process and is also trimmed to pure pass-through.
[0084] After the aforementioned compile-time optimization, the complete page fault handling process of the 39-bit virtual address architecture is reduced from 5 levels of function calls in the source code to an equivalent 3 levels of inline code through inline expansion. The comparison before and after optimization is as follows: the number of function calls is reduced from 5 (including 2 folded level calls) to 0 (all inlined); the number of compile-time level constant comparisons is reduced from 5 to 0 (all evaluated at compile time); the number of physical page address read operations is reduced from 3 to 2 (folded level reads are eliminated); and the number of pointer type conversion operations is reduced from 2 to 0 (the compiler eliminates pure pass-through type conversions). The optimization effect can be verified by disassembling the compilation artifacts: in optimized compilation mode, searching for offset calculation and allocation function symbols at the folded level will not find independent function entries, confirming that they have been completely eliminated through inlining.
[0085] The prerequisites for the compile-time optimization include: the compile-time hierarchical constants must be defined as compile-time constants, and runtime variables cannot be recognized by constant propagation; the compiler's optimization level is usually no lower than the first-level optimization to enable dead code elimination; and each level of function must be marked with the forced inline attribute to ensure that the compiler expands the function body at the call point.
[0086] In level zero optimization (debug mode), the compiler retains all conditional branches in the source code without performing dead code elimination. The decision branches at the folded level still exist at runtime but function correctly (the values of the compile-time level constants are evaluated to 1 at runtime, determining the branch result), resulting only in performance loss without affecting semantic correctness. In level one optimization and above, the compiler enables constant propagation and dead code elimination, and the code branches at the folded level are completely pruned.
[0087] Under Level 2 and Level 3 optimizations, the compiler may perform more aggressive optimizations, for example, inlining the offset calculation of the non-folded level into a single shift plus mask instruction sequence, further eliminating the register save and restore overhead of function calls.
[0088] In another embodiment, the compile-time optimization can also be achieved through conditional compilation attributes, that is, selectively compiling different code paths directly at compile time using feature flags. This method can eliminate all code at the folded level, but it requires maintaining independent code branches for each hardware architecture, and its code maintenance cost is higher than the method in this embodiment that achieves the equivalent effect in a unified code through compile-time level constants.
[0089] S500, after allocating the physical page table page on demand, a memory barrier is executed to ensure that the initialization content of the physical page table page is visible to other processing cores. After locking the page where the parent page table entry is located, a secondary check is performed. If it is confirmed that the page table entry is still empty, the allocated physical page table page is mounted.
[0090] The concurrency safety mechanism covers the multi-core collaborative operations during the page fault handling process and the page table reclamation process. In the allocation path of the page fault handling process, the method executes a write memory barrier after completing the allocation and zero-value initialization of the physical page table. The write memory barrier ensures that all write operations during zero-value initialization are visible to all processing cores in the system before the mount write operations following the memory barrier. In processor architectures with weak memory ordering, the processor allows out-of-order execution of write operations at different addresses to improve pipeline efficiency. The write memory barrier prohibits such cross-address write reordering, ensuring the strict sequential execution of the initialization operation, the memory barrier, and the mount operation.
[0091] On different processor architectures, the write memory barrier is mapped to different hardware instructions: On weak memory ordering processor architectures, the write memory barrier is mapped to an explicit memory barrier instruction, which forces the processor to flush all data to be written in the memory buffer to the cache coherency domain, ensuring that all write operations before the barrier are visible to other processing cores before allowing write operations after the barrier to be executed; On fully ordered memorying processor architectures, since the hardware inherently guarantees that the visibility order of write operations to all processing cores is consistent with the program order, the write memory barrier degenerates into a compiler barrier, which only prevents the compiler from reordering the instruction order of write operations without generating additional hardware instruction overhead.
[0092] After executing the write memory barrier, the physical page containing the parent layer's page table entry is locked to obtain a page-level spinlock. During the holding of the spinlock, a secondary check is performed on the parent layer's page table entry. If the secondary check confirms that the page table entry is still empty, the method writes the physical address and protection bits of the allocated physical page table page into the page table entry to complete the mounting, and then releases the spinlock. If the secondary check finds that the page table entry has been mounted by another processing core, the method releases the spinlock and returns the allocated physical page table page to the buddy allocator.
[0093] During the page table reclamation process, the serialization of the method ensures that the process-level read-write lock already held by the caller is utilized. The process-level read-write lock employs read-write separation locking semantics: Multiple readers can concurrently hold read locks and access the page table structure, but writers must exclusively hold write locks, blocking all other readers and writers while holding the write lock. Page table reclamation is triggered only in two scenarios: when the user process actively releases the virtual address mapping or the process completely exits. In the active release scenario, the caller already holds the write lock of the process's memory-mapped read-write lock (exclusive mode), all other page table operations are blocked, and the reclamation path is executed in a serialized environment. In the process exit scenario, all threads of the user process have terminated, there are no concurrent page table accessors, and the reclamation path is naturally serialized.
[0094] The serialization guarantee of the process exit scenario is based on the operating system process management: when a process exits, the operating system kernel first terminates all user-mode threads of the process and waits for it to exit completely. Then, it performs the cleanup of process resources in a single-threaded environment. The page table reclamation, as a component of resource cleanup, is safely executed in the single-threaded environment.
[0095] In an alternative implementation, concurrency safety of the reclamation path can also be achieved through a delayed release mechanism. This involves delaying the release of physical page tables until a grace period ends, during which the reclamation path and read-side traversal are allowed to execute concurrently. The delayed release mechanism ensures safety by performing the actual physical page release operation in a callback function after the grace period ends. During this grace period, all processing cores that entered the read-side critical section before the start of the grace period have exited, and therefore the delayed-released physical page tables are no longer referenced by any reader.
[0096] This application also provides an operating system page table folding management system. For example... Figure 5 As shown, the system includes a hierarchical constant configuration module (i.e., the first configuration module), a page table traversal module (i.e., the first traversal module), a page table allocation module (i.e., the first allocation module), and a page table recycling module (i.e., the first recycling module).
[0097] The hierarchical constant configuration module is used to define compile-time hierarchical constants for each level in the multi-level page table software model, and to set the compile-time hierarchical constants of folded levels to preset folding values. During compilation, the hierarchical constant configuration module selects the corresponding compile-time hierarchical constant configuration set based on the characteristic identifier of the target hardware architecture.
[0098] The hierarchical constant configuration module defines a unified feature interface for each level type of the five-level software page table model. The feature interface includes a compile-time hierarchical constant access method, a bit offset access method, and an index calculation method.
[0099] The unified feature interface enables the page table traversal module, the page table allocation module, and the page table recycling module to call the above methods at any level in a generic manner, without having to write independent processing logic for each level type.
[0100] The five-level software page table model implements the aforementioned feature interface in its global page directory layer, fourth-level page directory layer, upper-level page directory layer, middle-level page directory layer, and page table entry layer, respectively, and sets their respective compile-time hierarchical constant values through architectural conditional compilation. The hierarchical constant configuration module does not contain any runtime state; all outputs are compile-time constants.
[0101] The page table traversal module is used to, during the page table traversal process, pass through the pointer of the parent layer as the base address of the current layer when the compile-time level constant of the parent layer is equal to the preset folding value, and when the compile-time level constant is not equal to the preset folding value, obtain the base address of the child table from the page table entry of the parent layer and determine the offset within the layer according to the virtual address to be accessed.
[0102] The page table traversal module comprises two core components: an offset calculation method and an index calculation method. The offset calculation method receives the parent layer page table entry pointer and the user virtual address as input. It obtains the current layer's base address by determining whether to perform base address pass-through or actual dereference using the parent layer's compile-time layer constants. Combined with the intra-layer offset determined by the index calculation method, it returns the target page table entry pointer.
[0103] When the compile-time hierarchical constant of the parent layer equals the preset folding value, the page table traversal module performs base address pass-through without dereferencing the pointer of the parent layer. The page table traversal module does not contain runtime state; all calculations are based on the compile-time hierarchical constant and the input parameters.
[0104] The page table allocation module is used to skip the allocation of physical page table pages during page fault handling when the parent layer's compile-time hierarchical constant equals the preset folding value, and to allocate physical page table pages on demand when the compile-time hierarchical constant does not equal the preset folding value and the page table entry is empty. The page table allocation module encapsulates the on-demand allocation logic of physical page table pages in page fault exception paths and interacts with the buddy allocator to request and release physical pages.
[0105] The page table allocation module includes a physical page request interface, a physical page release interface, a page table entry mounting interface, and a page table entry null value detection interface. The physical page request interface requests a zero-initialized physical page from the partner allocator. The page table entry mounting interface writes the physical page address and protection bits into the page table entry. The page table allocation module does not hold any persistent state; all state is stored within the page table entry itself.
[0106] The page table reclamation module is configured to, during the page table reclamation process, only pass the reclamation request to the next level without performing a release operation when the compile-time level constant of the current layer equals the preset folding value; and release the physical page table pages of the next level after the reclamation of the next level is completed when the compile-time level constant does not equal the preset folding value. The page table reclamation module implements the safe release logic for physical page table pages corresponding to virtual address ranges.
[0107] The page table reclamation module includes a recursive reclamation entry method, an address range boundary calculation method, a physical page address reading method, and a physical page release method. The recursive reclamation entry method is called hierarchically; for collapsed levels, the transparent layer rule is executed to only pass through the reclamation request; for non-collapsed levels, the upper-level cleanup of lower-level operations is performed to release the child-level physical pages. The page table reclamation module returns the released physical pages to the free page pool of the buddy allocator.
[0108] The compile-time hierarchical constants of the hierarchical constant configuration module are jointly consumed by the page table traversal module, the page table allocation module, and the page table reclamation module, serving as a unified basis for determining collapsed and non-collapsed hierarchies. After completing the allocation of physical page tables, the page table allocation module calls the offset calculation method of the page table traversal module to return the target page table entry pointer; the two work in series during the page fault handling process. The page table reclamation module implements multi-level, step-by-step reclamation through recursive calls, with each level's reclamation operation based on the compile-time hierarchical constant determination execution path provided by the hierarchical constant configuration module.
[0109] In the page table traversal scenario, the page table traversal module obtains the compile-time hierarchical constants from the hierarchical constant configuration module and independently completes the multi-level search from the virtual address to the page table entry pointer, without involving the page table allocation module and the page table reclamation module.
[0110] In the page fault handling scenario, the page table allocation module first calls the hierarchical constant configuration module to determine whether the current layer needs to be allocated. After completing the allocation and mounting, the offset calculation method of the page table traversal module is called to obtain the target page table entry pointer.
[0111] In the page table reclamation scenario, the page table reclamation module independently executes recursive reclamation logic, determining the reclamation strategy (transparent transmission or release) for each layer through the hierarchical constant configuration module. The four modules of the system are implemented by the processor executing computer program instructions stored in memory.
[0112] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit described above can be implemented in hardware.
[0113] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A method for managing operating system page table folding based on base address pass-through and compile-time constants, applied to operating system memory management including a multi-level page table software model, wherein each level of the multi-level page table software model has a first-level constant, and the first-level constant of the folding level is equal to a preset folding value, characterized in that, The method includes: When the first level constant of the parent layer is equal to the preset folding value, the pointer of the parent layer is passed through as the current layer base address; when the first level constant is not equal to the preset folding value, the child table base address is obtained from the page table entry of the parent layer, and the intra-layer offset is determined according to the virtual address to be accessed. When the first level constant of the parent layer is equal to the preset folding value, the allocation of the first physical page is skipped; when the first level constant is not equal to the preset folding value and the page table entry is empty, the first physical page is allocated and mounted as needed. If the first-level constant of the current layer is equal to the preset folding value, only the recycling request is passed to the next layer without performing a release operation; if the first-level constant is not equal to the preset folding value, if the current layer is a page table entry layer, the corresponding physical page mapping within the recycling range is cleared; if the current layer is not a page table entry layer, the page table physical pages of the next layer are released after the recycling of the next layer is completed.
2. The method according to claim 1, characterized in that, The first-level constants are determined based on the bit-field decomposition of the hardware virtual address and include: Allocate the index bit width according to the hierarchy of the effective bit width of the virtual address; The first level constant of each level is calculated as the number of entries corresponding to the index bit width of that level, and the index bit width of the folded level is zero.
3. The method according to claim 2, characterized in that, The determination of the intra-layer offset includes the following steps: Shift the virtual address to be accessed logically to the right by the current layer's bit offset; Based on the right shift result and the number of entries in the current level minus 1, a bitwise AND operation is performed to obtain the intra-level offset.
4. The method according to claim 1, characterized in that, The on-demand allocation of the first physical page includes: Request a physical page of the same size from the partner allocator as the first physical page, and perform zero-value initialization during allocation; If the buddy allocator is unable to provide free physical pages, it returns an error result to trigger out-of-memory processing.
5. The method according to claim 1, characterized in that, Before the first physical page is mounted, the method further includes: A first memory barrier is executed to ensure that the initial contents of the first physical page are visible to other processing cores; After locking the page containing the page table entry of the parent layer, perform a second check on the page table entry; If the second check confirms that the page table entry is still empty, mount the first physical page; otherwise, return the first physical page to the partner allocator.
6. The method according to claim 1, characterized in that, Releasing the first physical page of the next level includes: Read the physical page address of the next level from the current level's page table entry; Clear the current page table entry and set all its bit fields to zero. Return the first physical page corresponding to the physical page address to the partner allocator.
7. The method according to claim 1, characterized in that, The method also includes performing compile-time optimization steps: The functions at each level in the multi-level page table software model are expanded to the call point using inline tags; The compiler replaces the references to the first-level constants with integer literals and performs compile-time evaluation on the criteria for the folding level. Dead code elimination is performed on folded level code branches where the condition is always false, generating machine code corresponding to the actual number of page table levels in the hardware.
8. An operating system page table folding management system based on base address pass-through and compile-time constants, characterized in that, The system includes: The first configuration module is used to configure the first level constant for each level in the multi-level page table software model, and to set the first level constant of the folded level to a preset folding value. The first traversal module is used to, during the page table traversal process, pass through the pointer of the parent layer as the current layer base address when the first level constant of the parent layer is equal to the preset fold value, and when the first level constant is not equal to the preset fold value, obtain the sub-table base address from the page table entry of the parent layer and determine the intra-layer offset according to the virtual address to be accessed. The first allocation module is used to skip the allocation of the first physical page when the first level constant of the parent layer is equal to the preset folding value during the page fault handling process, and to allocate the first physical page as needed when the first level constant is not equal to the preset folding value and the page table entry is empty. The first recycling module is used to, during the page table recycling process, if the first level constant of the current layer is equal to the preset folding value, only pass the recycling request to the next level without performing a release operation; if the first level constant is not equal to the preset folding value, if the current layer is a page table entry layer, then clear the physical page mapping corresponding to the recycling range; if the current layer is not a page table entry layer, then release the page table physical pages of the next level after the recycling of the next level is completed.
9. The system according to claim 8, characterized in that, When the first traversal module is equal to the preset fold value in the first level constant of the parent layer, it performs base address pass-through without dereferencing the pointer of the parent layer.
10. A computer-readable storage medium, characterized in that, It stores a computer program that, when executed by a processor, implements the method as described in any one of claims 1 to 7.
