A memory allocation method and computing device

By configuring intervals of different page sizes in the virtual address space and allocating virtual pages based on the control group, the problem of diversity of memory allocation requirements in the ARM architecture is solved, and flexible and efficient memory management is achieved.

CN118210622BActive Publication Date: 2025-04-11XFUSION DIGITAL TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202410232194.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-02-29
Publication Date
2025-04-11
Estimated Expiration
2044-02-29

AI Technical Summary

Technical Problem

It is difficult for existing ARM architectures to take into account different memory allocation needs, especially in terms of page size granularity, and it is impossible to use multiple page sizes for memory allocation at the same time.

Method used

Memory allocation of different page sizes and granularity is achieved by configuring two intervals with different page sizes in the virtual address space and allocating the first virtual page or the second virtual page to different tasks based on the preset control group.

Benefits of technology

It meets more flexible memory allocation needs, improves the hit rate of TLB, and enhances memory usage efficiency and security.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118210622B_ABST
    Figure CN118210622B_ABST
Patent Text Reader

Abstract

The embodiment of the present application discloses a memory allocation method and a computing device, which is applied to a computing device of an ARM architecture; the method includes: configuring the page size of a first virtual page of a first virtual address space, and the page size of a second virtual page of a second virtual address space, wherein the page size of the first virtual page is larger than the page size of the second virtual page; determining whether the task to be allocated memory belongs to a preset control group; if the task belongs to the preset control group, allocating the first virtual page or the second virtual page to the task based on the control group to which the task belongs. By configuring the first virtual address space and the second virtual address space with different page size granularities, and allocating the first virtual page or the second virtual page to different tasks based on the preset control group, memory allocation with different page size granularities can be implemented under an ARM architecture server, meeting the more diverse and flexible memory allocation requirements of an ARM architecture server.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of memory management technology, and in particular to a memory allocation method and computing device. Background Art

[0002] In the advanced RISC machine (ARM) architecture, the memory in a computing device is managed by physical pages. When the central processing unit (CPU) of a computing device executes a task, the CPU can read and write the corresponding physical page in the memory by accessing the virtual page in the virtual address space of the task.

[0003] A page table entry (PTE) is a data structure used to organize and manage the mapping relationship between virtual addresses and physical addresses. Each page table entry is used to associate a virtual page with a physical page. In this way, when a task needs to access a physical page, the CPU can query the corresponding page table entry based on the virtual page and then access the corresponding physical page.

[0004] Currently, the ARM architecture supports mapping between physical pages and virtual pages with a page size of 4KB, 16KB, or 64KB. It is understandable that using large pages for memory allocation can improve the hit rate of the page table cache (translation lookaside buffer, TLB), and using small pages for memory allocation is more flexible. Therefore, it is difficult to take into account different memory allocation requirements by using a single page size granularity for memory allocation. Summary of the invention

[0005] The embodiments of the present application provide a memory allocation method and a computing device, which can implement memory allocation of different page size granularities under the ARM architecture to meet more diverse and flexible memory allocation requirements.

[0006] A first aspect of an embodiment of the present application provides a memory allocation method, which is applied to a computing device with an ARM architecture, and the method includes:

[0007] Configure the page size of the first virtual page of the first virtual address space and the page size of the second virtual page of the second virtual address space; determine whether the task to which memory is to be allocated belongs to a preset control group; if the task belongs to the preset control group, allocate the first virtual page or the second virtual page to the task based on the control group to which the task belongs.

[0008] The page size of the first virtual page is larger than the page size of the second virtual page.

[0009] The preset control group includes a first control group and a second control group; the first control group is used to control the allocation of the first virtual page to the preset kernel mode task; the second control group is used to control the allocation of the first virtual page or the second virtual page to the preset user mode task.

[0010] In an embodiment of the present application, by configuring two intervals with different page size granularities in the virtual address space, and allocating the first virtual page or the second virtual page to different tasks based on a preset control group, memory allocation with different page size granularities can be achieved to meet more memory allocation requirements.

[0011] In a possible implementation, the first virtual address space includes a first sub-virtual address space and a second sub-virtual address space; wherein, the first sub-virtual address space is a kernel mode address space; the second sub-virtual address space is a user mode address space; and when the task belongs to the preset control group, the first virtual page or the second virtual page is allocated to the task based on the control group to which the task belongs, including: when the task belongs to the first control group, the first virtual page located in the first sub-virtual address space is allocated to the task; when the task belongs to the second control group, the first virtual page located in the second sub-virtual address space is allocated to the task.

[0012] In an embodiment of the present application, by dividing the first virtual address space to include a first sub-virtual address space and a second sub-virtual address space, and using the first sub-virtual address space as a kernel mode address space and the second sub-virtual address space as a user mode address space, both user mode tasks and kernel mode tasks can use large pages in the first virtual address space, thereby improving the hit rate of the TLB.

[0013] In a possible implementation, the first virtual address space includes a first sub-virtual address space, and the second virtual address space includes a third sub-virtual address space; wherein, the first sub-virtual address space is a kernel mode address space; the third sub-virtual address space is a user mode address space; and when the task belongs to the preset control group, the first virtual page or the second virtual page is allocated to the task based on the control group to which the task belongs, including: when the task belongs to the first control group, the first virtual page located in the first sub-virtual address space is allocated to the task; when the task belongs to the second control group, the second virtual page located in the third sub-virtual address space is allocated to the task.

[0014] In an embodiment of the present application, by using the third sub-virtual address space in the second virtual address space as the user state address space and allocating memory for user state tasks based on the smaller second virtual page, the memory utilization efficiency can be improved; the kernel state address space and the user state address space can be further isolated to improve the security of memory access.

[0015] In a possible implementation, the method further includes: when the task does not belong to the preset control group, allocating the second virtual page to the task.

[0016] In the embodiment of the present application, for tasks that are not managed by a preset control group, a second virtual page is uniformly allocated, which can reduce the workload of the staff in pre-setting the control group and improve work efficiency.

[0017] In one possible implementation, the second virtual address space includes a fourth sub-virtual address space, which is a kernel mode address space; when the task does not belong to the preset control group, the second virtual page is allocated to the task, including: when the task does not belong to the preset control group, determining whether the task is a kernel mode task; when the task is the kernel mode task, allocating the second virtual page located in the fourth sub-virtual address space to the task.

[0018] In the embodiment of the present application, by dividing the fourth sub-virtual space in the second virtual space and allocating the second virtual page to the kernel-mode task, the memory allocation demand of the kernel-mode task for the small page can be met.

[0019] In a possible implementation, before determining whether the task to which memory is to be allocated belongs to a preset control group, the method further includes: determining whether the task is a target task, and the determining whether the task to which memory is to be allocated belongs to a preset control group includes: when the task is the target task, determining whether the task belongs to the second control group; when the task belongs to the preset control group, allocating the first virtual page or the second virtual page to the task based on the control group to which the task belongs, including: when the task belongs to the second control group, determining whether the offset in the MMAP request corresponding to the task matches the page size of the first virtual page; when the offset matches the page size of the first virtual page, determining whether the MMAP request is a fixed mapping and whether the mapping address range of the MMAP request is located in the third sub-virtual address space; when the MMAP request is not a fixed mapping, or the mapping address range is located in the second sub-virtual address space, allocating the first virtual page located in the second sub-virtual address space to the task.

[0020] Wherein, when the MMAP request is a fixed mapping and the mapping address range is located in the third sub-virtual address space, the first virtual page located in the second sub-virtual address space is allocated to the task.

[0021] The second control group is specifically used to control the allocation of the first virtual page to the preset user mode task; the first virtual address space includes a second sub-virtual address space; and the second virtual address space includes a third sub-virtual address space.

[0022] The second sub-virtual address space and the third sub-virtual address space are user-mode address spaces.

[0023] The target task is to execute the memory mapping MMAP task.

[0024] In an embodiment of the present application, the memory mapping behavior requested by the task can be authenticated through the control group to which the task belongs and the mapping address range indicated by the MMAP request, and a virtual page of a corresponding size can be allocated, which can meet different memory mapping requirements while ensuring the security of the memory mapping.

[0025] In one possible implementation, after the control group to which the task belongs allocates the first virtual page or the second virtual page to the task, the method also includes: determining the first physical page accessed by the task based on the target virtual page and the page table; in response to the missing of the first physical page, determining whether the page size of the target virtual page matches the page size of the first virtual page; in the case where the page size of the target virtual page matches the page size of the first virtual page, determining whether the virtual address corresponding to the target virtual page is located in the second sub-virtual address space; in the case where the virtual address corresponding to the target virtual page is located in the second sub-virtual address space, allocating a second physical page to the task; and adding the mapping relationship between the second physical page and the target virtual page to the page table.

[0026] The target virtual page is the virtual page indicated by the virtual address in the access request of the task.

[0027] The second physical page has the same page size as the first virtual page.

[0028] In an embodiment of the present application, when a page miss is detected, the access behavior is authenticated, and when it is determined that the access target is the user space, the corresponding physical page allocation is performed, which can improve the security of the access behavior.

[0029] In one possible implementation, the first virtual address space includes a second sub-virtual address space; the second virtual address space includes a fourth sub-virtual address space; the method also includes: in response to creating a first task, adding a first page table entry in the task_struct data structure of the first task and adding the second page table entry in the second page table base address register of the current task to the task_struct data structure of the first task.

[0030] The second page table base address register is used to store the page table base address of the second virtual address space.

[0031] The first page table entry is used to indicate the page table base address of the second sub-virtual address space; and the second page table entry is used to indicate the page table base address of the fourth sub-virtual address space.

[0032] In an embodiment of the present application, by adding the first page table entry of the first task and the second page table entry of the current task in the task_struct data structure of the first task when creating the first task, the correctness of the user state address space and the kernel state address space can be guaranteed, avoiding exceptions or failures when the processor accesses memory.

[0033] In one possible implementation, after adding the second page table entry in the second page table base address register of the current task to the task_struct data structure of the first task, the method further includes: in response to switching the first task to the second task, updating the corresponding page table entry in the first page table base address register according to the first page table entry in the task_struct data structure of the second task; updating the task_struct data structure of the second task according to the second page table entry in the second page table base address register of the first task; and then, loading the PGD in the task_struct data structure of the second task into the second page table base address register.

[0034] The first page table base address register is used to store the page table base address of the first virtual address space.

[0035] In an embodiment of the present application, by updating the page table entries in the first page table base address register and the second page table base address register when switching the first task to the second task, the page table base address of the user space of the second task is loaded accordingly, and the page table base address of the kernel space remains unchanged, thereby ensuring the correctness of the user state address space and the kernel state address space, and avoiding exceptions or failures when the processor accesses memory.

[0036] A second aspect of an embodiment of the present application provides a computing device, which includes: a memory and a processor; wherein the processor is an ARM architecture; the processor is coupled to the memory; computer program instructions are stored in the memory; the computer program instructions are loaded and executed by the processor, so that the computing device implements any possible implementation method described in the first aspect.

[0037] A third aspect of an embodiment of the present application provides a computer-readable storage medium, in which computer-executable instructions are stored. The computer-executable instructions are executed by a computing device to implement any possible method described in the first aspect.

[0038] A fourth aspect of an embodiment of the present application further provides a computer program product, which includes computer execution instructions, which are stored in a computer-readable storage medium; at least one processor of the device can read the computer execution instructions from the computer-readable storage medium, and at least one processor executes the computer execution instructions so that the device executes any method that may be implemented as described in the first aspect.

[0039] It should be understood that the beneficial effects of the above-mentioned various aspects can be referenced to each other. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] Figure 1 A schematic diagram of the distribution of a virtual address space;

[0041] Figure 2 A schematic diagram of an application scenario of an embodiment of the present application;

[0042] Figure 3 A flowchart of a memory allocation method provided in an embodiment of the present application;

[0043] Figure 4 A schematic diagram of the distribution of a virtual address space provided in an embodiment of the present application;

[0044] Figure 5 A schematic diagram of a memory mapping process provided in an embodiment of the present application;

[0045] Figure 6 A schematic diagram of a page error processing process provided by an embodiment of the present application;

[0046] Figure 7 A schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0047] The following describes the embodiments of the present application in conjunction with the accompanying drawings. Obviously, the described embodiments are only embodiments of a part of the present application, rather than all embodiments. It is known to those skilled in the art that with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.

[0048] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0049] The following is an explanation of some terminology concepts involved in the embodiments of the present application.

[0050] (1) Advanced RISC machine (ARM) architecture

[0051] ARM architecture is also known as ARM system architecture. ARM architecture is a hardware specification that is mainly used to agree on the instruction set and the internal architecture of the chip. Computing devices using ARM architecture were called advanced reduced instruction set machines in the past. They use the reduced instruction set computer (RISC) central processor architecture. ARM architecture computing devices are widely used in many embedded system designs.

[0052] (2) Operating system

[0053] An operating system is system software used to manage computer hardware and software resources. It is also the cornerstone of a computer system. An operating system needs to handle basic tasks such as managing and configuring memory, determining the priority of system resource supply and demand, controlling input and output devices, operating networks, and managing file systems. An operating system can also provide an operating interface for users to interact with the system.

[0054] (3) Kernel

[0055] The kernel is a program used to manage data input and output requests from 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 a modern operating system.

[0056] (4) Memory

[0057] Memory is an important component of computing devices, also known as internal memory or main memory. It is used to temporarily store the calculation data in the CPU and the data exchanged with external memory such as hard disk. Memory is the bridge for communication between external memory and CPU. All programs in the computer run in memory. The strength of memory performance affects the overall performance of the computer.

[0058] (5) Mission

[0059] A program is a description of instructions, data and their organizational form, and a process is the running entity of a program. Multiple threads can run concurrently in a process, and a thread can be the actual operating unit of a process, where a thread can also be called a lightweight process. In the ARM architecture, processes and threads are not distinguished and are uniformly managed by the task_struct data structure. In the embodiments of this application, tasks are used to uniformly describe processes and threads.

[0060] (6) Virtual Address Space

[0061] The virtual address space is a collection of virtual addresses. A virtual address is a logical address used by the CPU when executing a task. The CPU accesses the corresponding physical address in the physical memory through the virtual address. The virtual address space can also be called the process address space. The virtual address space includes the user space that each task owns independently, which can avoid access conflicts and access errors caused by mutual access between tasks. The virtual address space also includes the kernel space shared by all tasks.

[0062] (7) Physical page, virtual page, page table

[0063] The operating system divides the memory space into partitions of equal size, which are called physical blocks. A physical page is a collection of one or more physical blocks, which is also called a page frame.

[0064] The operating system divides the virtual address space into areas of the same size as physical pages. These areas are called virtual pages.

[0065] The page mapping table created by the operating system for each process is called a page table. For all virtual pages in the virtual address space of a process, there is a page table entry in the page table of the process, which records the mapping relationship between the virtual page and the corresponding physical page in the memory.

[0066] In memory management, in order to save memory space and facilitate discrete storage, a multi-level page table is usually used to manage page table entries. In the multi-level page table memory management model, taking the four-level page table as an example, the four-level page tables are page global directory (PGD), page upper directory (PUD), page middle directory (PMD) and page table.

[0067] (8) Kernel mode tasks and user mode tasks

[0068] The operating system divides the virtual address space into kernel space and user space. The kernel space is used to map the kernel code, and the user space is used to map the application code of the corresponding task. When a task is executed in the application code, the task can be called a user running task, or simply a user state task; when a task executes a system call and falls into the kernel code, the task can be called a kernel running task, or simply a kernel state task.

[0069] (9) Control group (Cgroup)

[0070] The group members of the control group are tasks. The control group can be used to limit the resource usage objects, resource usage quantities, and resource usage priorities of its group members.

[0071] (10) Memory map (MMAP)

[0072] Memory mapping is a method of memory mapping files, which is used to map files or other objects into the virtual address space of a process.

[0073] (11) Page fault

[0074] A page fault, also known as a page miss, is an interruption caused by the CPU finding that the target virtual page being accessed does not have a corresponding physical page in memory when executing a task. In actual applications, when the CPU is executing a newly created task, page faults will continue to occur in the process of accessing the virtual address space of the task. The page fault handler will then allocate an actual physical page for the task, and then write the mapping relationship between the physical page and the target virtual page being accessed into the corresponding page table.

[0075] (12) Translation table base register (TTBR)

[0076] In the ARM architecture, there are TTBR0 and TTBR1, which are used to store the first-level page table base address of the user space and kernel space, namely PGD. Because all tasks share the same kernel space, the content in TTBR1 is unchanged; and the user space of each task is independent, so the content in TTBR0 is the PGD in the task_struct data structure of the task currently executed by the CPU.

[0077] After understanding the basic meanings of the above terms, the application scenarios of the embodiments of the present application are introduced below.

[0078] First, see Figure 1 , Figure 1 The figure is a diagram of the distribution of a virtual address space. In the current ARMv8 architecture, the ARM processor supports a 64-bit virtual address space, but the memory requirements of current applications do not require the use of the entire 64-bit virtual address space. Therefore, the ARMv8 architecture usually uses a 48-bit address line width, dividing the 64-bit virtual address space into 48-bit user space and 48-bit kernel space.

[0079] like Figure 1 As shown in the figure, the kernel space is at the top of the virtual address space, with the upper 16 bits all 1, and the address range is [0xFFFF 000000000000, 0xFFFF FFFF FFFF FFFF]; the user space is at the bottom of the virtual address space, with the upper 16 bits all 0, and the address range is [0x0000000000000000, 0x0000 FFFF FFFF FFFF]; the address with the upper 16 bits all 1 or all 0 is called a standard address, and the addresses in between are non-standard addresses and are not allowed to be used.

[0080] Then you can refer to Figure 2 , Figure 2 Schematic diagram of an application scenario of an embodiment of the present application; Figure 2 As shown, the computing device 10 includes a processor 110, a memory 120, a first page table base address register TTBR1 and a second page table base address register TTBR0, the processor 110 includes a memory management unit (memory management unit, MMU) 111 and a page table cache (translation lookaside buffer, TLB) 112, and the memory 120 stores a page table 121. Among them, the processor 110, TTBR0 and TTBR1 can be integrated into the same chip. Exemplarily, the processor 110 is a processor of ARM architecture.

[0081] When the processor 110 needs to access the memory 120 during the execution of a task, it generates a memory access request, which includes a virtual address in the virtual address space of the task; the processor 110 queries TLB112 through MMU 111 whether there is a page table entry corresponding to the virtual address; if so, the processor 110 can allocate the corresponding memory resources to the task according to the page table entry; if not, the processor 110 can further search the page table entry corresponding to the virtual address in the page table 121 through MMU 111.

[0082] If there is no page table entry corresponding to the virtual address in TLB 112 and page table 121, a page fault is triggered. At this time, processor 110 can execute a page fault handler to allocate an actual physical page for the task, and then write the mapping relationship between the physical address of the physical page and the virtual address into the corresponding page table.

[0083] In the process of searching the page table 121, if the highest bit of the virtual address in the memory allocation request is 0, the processor 110 can obtain the PGD of the user space from TTBR0 and query the page table corresponding to the virtual address according to the PGD. If the highest bit of the virtual address is 1, the processor 110 can obtain the page table base address of the kernel space from TTBR1 and query the page table corresponding to the virtual address according to the page table base address.

[0084] Exemplarily, [38:30] of the virtual address may be used to place the index of the PGD, [29:21] may be used to place the index of the PMD, [20:12] may be used to place the index of the PTE, and [11:0] may be used to place the page offset. After obtaining the physical page base address from TTBR0 or TTBR1, the processor 110 may perform the following steps:

[0085] The first step is to use the PGD index in [38:30] of the virtual address as the offset of the PGD, and then add the first address in the TTBR0 or TTBR1 to the PGD offset to obtain an item in the PGD, and determine the PMD pointed to by the item in the PGD; the second step is to use the PMD index in [29:21] of the virtual address as the offset of the PMD, and add the first address of the PMD determined in the first step to the offset of the PMD to obtain an item in the PMD, and determine the PTE pointed to by the item in the PMD. The third step is to use the PTE index in [20:12] of the virtual address as the offset of the PTE, and add the first address of the PTE determined in the second step to the offset of the PTE to obtain an item in the PTE, determine the page table entry corresponding to the item in the PTE, and determine the address of the physical page corresponding to the virtual address based on the page table entry; after determining the address of the physical page, the offset of the physical address placed in [11:0] of the virtual address can be added to the address of the physical page to obtain the physical address.

[0086] exist Figure 2 In the application scenario shown, if the MMU 111 can find the corresponding page table entry when querying the TLB 112, that is, a direct access hit, the processor 110 does not need to query in the page table 121, which can save a lot of computing resources; therefore, pages with a larger page size granularity are used for mapping between physical pages and virtual pages, and the same number of page table entries in the TLB 112 can indicate a larger memory range, thereby improving the hit rate of the TLB 112.

[0087] Using pages with a smaller page table size to map between physical pages and virtual pages can improve memory utilization, reduce internal fragmentation, and avoid the situation where the memory required for a task is much smaller than a memory page, resulting in most of the memory of the memory page being wasted.

[0088] However, the current ARM architecture supports mapping between physical pages and virtual pages at one of the page size granularities of 4KB, 16KB or 64KB, and cannot use two or more page size granularities for memory allocation at the same time, making it difficult to meet different memory allocation requirements.

[0089] Based on this, an embodiment of the present application provides a memory allocation method and related equipment, which can realize memory allocation of different page size granularities under the ARM architecture to meet more memory allocation requirements.

[0090] See also Figure 3 , Figure 3 A flowchart of a memory allocation method provided in an embodiment of the present application is provided. The method is applied to Figure 2 The computing device shown includes an ARM architecture processor (hereinafter referred to as the processor), so the computing device can also be called an ARM architecture computing device; the method specifically includes steps 301 to 304.

[0091] Step 301: Configure the page size of a first virtual page of a first virtual address space and the page size of a second virtual page of a second virtual address space.

[0092] Among them, in the startup phase after the computing device is powered on, the processor can configure the TG1 field in the TCR_EL1 register in the computing device, so that the first virtual address space in the virtual address space is divided according to the preset page size (page length), thereby configuring the page size of the first virtual page in the first virtual address space; and then configure the page table base address in TTBR1 according to the page size of the first virtual page.

[0093] Exemplarily, when the TG1 field is set to 0b10, the page size of the first virtual page is 4KB; when the TG1 field is set to 0b01, the page size of the first virtual page is 16KB; when the TG1 field is set to 0b11, the page size of the first virtual page is 64KB.

[0094] The processor can configure the TG0 field in the TCR_EL1 register so that the second virtual address space of the virtual address space is divided according to a preset page size, thereby configuring the page size of the second virtual page in the second virtual address space; and then configure the virtual address part in TTBR0 according to the page size of the second virtual page.

[0095] Exemplarily, when the TG0 field is set to 0b00, the page size of the first virtual page is 4KB; when the TG1 field is set to 0b10, the page size of the first virtual page is 16KB; when the TG1 field is set to 0b01, the page size of the first virtual page is 64KB.

[0096] The page size of the first virtual page is larger than the page size of the second virtual page. Exemplarily, the page size of the first virtual page is 64 KB, and the page size of the second virtual page is 4 KB.

[0097] Among them, after configuring the virtual page sizes in the first virtual address space and the second virtual address space, the processor can configure the access rights of the first virtual address space and the second virtual address space to control the access behavior of different tasks to the virtual address space to avoid access conflicts or access errors.

[0098] Specifically, each page table entry in the page table includes a mapping relationship between a virtual page and a physical page, and also includes the access rights of the virtual page corresponding to the page table entry; the processor can first perform authentication after finding the corresponding page table entry, and perform subsequent access actions after determining that the currently executed task has the corresponding authority.

[0099] The first virtual address space configured by the TG1 field belongs to the high-order space in the virtual address space, the first n bits of the address in the high-order space are 1, and n is an integer greater than 0 and less than 64; the second virtual address space configured by the TG0 field belongs to the low-order space in the virtual address space, and the first n bits of the address in the low-order space are 0. Exemplarily, n can be 16.

[0100] Among them, the first virtual address space may include a first sub-virtual address space and a second sub-virtual address space; the processor may configure the access rights of the first sub-virtual address space to be accessible to kernel-mode tasks, and the access rights of the second sub-virtual address space to be accessible to both kernel-mode tasks and user-mode tasks.

[0101] In one possible implementation, Figure 4 As shown, the second virtual address space includes a third sub-virtual address space and a fourth sub-virtual address space; the access rights of the third sub-virtual address space are accessible to both kernel mode tasks and user mode tasks, and the access rights of the fourth sub-virtual address space are accessible to kernel mode tasks.

[0102] Therefore, in Figure 4 In the specific example, the first sub-virtual address space and the fourth sub-virtual address space are used as kernel state address space (hereinafter referred to as kernel space), and the second sub-virtual address space and the third sub-virtual address space are used as user state address space (hereinafter referred to as user space).

[0103] Optionally, the processor may further configure the access rights of the second sub-virtual address space to be accessible only to user-mode tasks, thereby preventing the second sub-virtual address space reserved for user-mode tasks from being occupied by kernel-mode tasks.

[0104] The embodiment of the present application further divides the high-order first virtual address space and the low-order second virtual address space, and uses a part of the high-order space and a part of the low-order space together as the kernel space, and another part of the high-order space and another part of the low-order space together as the user space; thereby being able to flexibly allocate physical pages of different sizes according to the needs of kernel-mode tasks and user-mode tasks.

[0105] Understandably, Figure 4 The interval distribution shown is for example only and is not limiting. In a specific implementation, the second sub-virtual address space may also be located before the first sub-virtual address space, or the first sub-virtual address space may be divided into two parts; the location of the fourth sub-virtual address space is similar.

[0106] Specifically, the processor can configure the access rights of the page table entries corresponding to one or more page table base addresses in TTBR0 to be accessible to kernel-mode tasks, so that the address intervals corresponding to the one or more page table base addresses serve as the fourth sub-virtual address space in the second virtual address space. In this way, when the driver module or other functional modules in the kernel need to use a smaller (such as 4KB) physical page, the processor can allocate a second virtual page of the corresponding size to the corresponding task.

[0107] Similarly, the processor can configure the access rights of the page table entries corresponding to one or more page table base addresses in TTBR1 to be accessible to user-mode tasks, so that the address interval corresponding to the one or more page table base addresses is the second sub-virtual address space in the first virtual address space. In this way, the processor can allocate the first virtual page to the application, thereby improving the hit rate of the TLB.

[0108] Optionally, the space size of the first sub-virtual address space is larger than the space size of the second sub-virtual address space, and the space size of the third sub-virtual address space is larger than the space size of the fourth sub-virtual address space. In this way, it can be ensured that kernel state tasks are allocated more large page memory, thereby improving kernel performance.

[0109] It is understandable that, usually, the virtual address space is divided into two spaces as kernel space and user space, where the high space is reserved for the operating system and hardware, and the low space is provided for application programs. Specifically, the hardware registers are usually mapped to the high address area of ​​the physical address, so the high space in the virtual address space is used as the kernel space, so the kernel can access the hardware faster; at the same time, for the convenience of programming, it is more reasonable for the application to start from the low address. The isolation of kernel space and user space can prevent conflicts between user programs and the kernel and better protect the kernel.

[0110] In an embodiment of the present application, using a first virtual address space with more high bits as a kernel space and a second virtual address space with more low bits as a user space can better be compatible with existing applications, operating systems, and hardware; after the first virtual page of the first virtual address space is configured, the processor can map the first virtual page to the memory and the input / output (IO) space of various hardware respectively, so that the operating system can call the kernel code stored in the memory and various hardware resources based on the first virtual address space.

[0111] At the same time, kernel-mode tasks can use more first virtual pages with larger page size granularity, which can reduce the number of memory allocations and improve kernel performance.

[0112] Step 302: Determine whether the task to which memory is to be allocated belongs to a preset control group.

[0113] After completing the page configuration of the first virtual address space and the second virtual address space, when the processor detects that virtual memory needs to be allocated for the task, step 302 can be executed. The task to be allocated memory refers to the task to be allocated virtual memory. Exemplarily, when the processor maps the relevant file of the task in the external memory of the computing device, such as the hard disk, to the virtual address space, the processor will allocate a virtual page in its virtual address space to the task, and establish a mapping between the virtual page and the relevant file.

[0114] The preset control group is used to control memory allocation for tasks. Specifically, the preset control group includes a first control group and a second control group; the first control group is used to control allocation of a first virtual page for a preset kernel-mode task; and the second control group is used to control allocation of a first virtual page or a second virtual page for a preset user-mode task.

[0115] Specifically, the processor may first determine whether the task is a kernel mode task or a user mode task; and then determine whether the task belongs to a control group in the preset control group for controlling kernel mode tasks, or whether the task belongs to a control group in the preset control group for controlling user mode tasks.

[0116] Optionally, the second control group includes a first sub-control group and a second sub-control group; the first sub-control group is used to control allocation of a first virtual page for a preset user mode task, and the second sub-control group is used to control allocation of a second virtual page for a preset user mode task.

[0117] Optionally, the second control group is used to control the priority allocation of one of the first virtual page or the second virtual page to the preset user state task, and when the priority allocated virtual page is insufficient, the other of the first virtual page or the second virtual page is allocated to the preset user state task.

[0118] Among them, the members of the control group are tasks. When the processor calls the kernel code or application code to execute a task, the operating system running on the processor can obtain the identifier of the currently executed task, and then determine the control group to which the task belongs based on the identifier. Specifically, the operating system can obtain the first identifier of the task to be allocated memory, and then traverse the second identifiers of the members in the preset control group to determine whether the first identifier matches the second identifier, thereby determining whether the task belongs to the preset control group. Exemplarily, the first identifier and / or the second identifier can be information in the task_struct data structure of the corresponding task that is used to uniquely identify the task.

[0119] Step 303: When the task belongs to a preset control group, allocate the first virtual page or the second virtual page based on the control group to which the task belongs.

[0120] When it is determined that the task belongs to the preset control group, the processor may allocate virtual pages to the task based on the memory usage permission or memory usage priority set for the corresponding control group.

[0121] Specifically, the processor may first obtain the virtual memory size required by the task, and then allocate a corresponding number of first virtual pages or second virtual pages to the task based on the memory usage permission or memory usage priority set by the corresponding control group.

[0122] In one possible implementation, when the task belongs to the first control group, that is, the task is a preset kernel-mode task, the processor can allocate a first virtual page to the task; when the task belongs to the second control group, that is, the task is a preset user-mode task, the processor can allocate a first virtual page or a second virtual page to the task.

[0123] Wherein, when the task belongs to the first control group, the processor may allocate a first virtual page in the first sub-virtual address space to the task.

[0124] Optionally, when the task belongs to the second control group, the processor may allocate a first virtual page in the second sub-virtual address space to the task.

[0125] Optionally, the second virtual address space includes a third sub-virtual address space, and the third sub-virtual address space is a user space; when the task belongs to the second control group, the processor allocates a second virtual page in the third sub-virtual address space to the task.

[0126] In one possible implementation, the second control group includes a first sub-control group and a second sub-control group; when the task belongs to the first sub-control group, the processor can allocate a first virtual page located in the second sub-virtual address space for the task; when the task belongs to the second sub-control group, the processor can allocate a second virtual page located in the third sub-virtual address space for the task.

[0127] In another possible implementation, when the task belongs to the second control group, the processor preferentially allocates the first virtual page located in the second sub-virtual address space to the task; when the first virtual page of the second sub-virtual address space is insufficient, the second virtual page located in the third sub-virtual address space is allocated to the task.

[0128] Step 304: When the task does not belong to the preset control group, allocate a second virtual page to the task.

[0129] Among them, by using the preset control group, it is possible to allocate virtual pages of preset page sizes to different tasks, and to allocate virtual pages of corresponding sizes based on the actual needs of different tasks, so that these tasks can use physical pages of corresponding sizes, making memory use more efficient. It is understandable that during the operation of the computing device, the number of tasks that the processor needs to perform is huge, and it is difficult and unnecessary to divide and control these tasks one by one through the preset control group; therefore, the staff can divide tasks with high importance, high operating requirements, or high memory read and write requirements into the preset control group; for tasks that do not belong to the preset control group, the processor can uniformly allocate a second virtual page with a smaller granularity to them, so as to avoid these tasks with lower importance, lower operating requirements, or lower memory read and write requirements from occupying the first virtual page.

[0130] In one possible implementation, the second virtual address space includes a third sub-virtual address space and a fourth sub-virtual address space, the third sub-virtual address space serves as a user space, and the fourth sub-virtual address space serves as a kernel space; when the task does not belong to a preset control group, the processor can first determine whether the task is a kernel state task; when the task is a kernel state task, the second virtual page located in the fourth sub-virtual address space is allocated to the task; when the task is a user state task, the second virtual page located in the third sub-virtual address space is allocated to the task.

[0131] In an embodiment of the present application, by configuring two intervals with different page size granularities in the virtual address space, and allocating the first virtual page or the second virtual page to preset different tasks based on a preset control group, memory allocation with different page size granularities can be achieved to meet more memory allocation requirements.

[0132] It is understandable that the user space of a task is described and managed by the task_struct data structure of the task. Figure 3 In the embodiment shown, when using Figure 4 In the virtual address space division method shown in FIG. 1 , when both the user space and the kernel space include high-order addresses and low-order addresses, the processor needs to adjust and update the task_struct data structure of the corresponding task when creating or switching tasks to avoid access conflicts under the new access permission settings.

[0133] Specifically, after completing the configuration of the access rights of the virtual address space, the processor can update the task_struct data structure of the corresponding task and the page table entries in TTBR0 and TTBR1 when creating a task or switching tasks.

[0134] There are five basic states of tasks, namely: new state, which is the state when a new task is created by the processor; ready state, which means that the task has been allocated all necessary resources except processor resources, and is waiting for the system to allocate processor resources to run; running state, the task is being run by the processor; waiting state, also called blocking state or sleeping state, the task does not have the running conditions and is suspended, waiting for a certain event to occur; terminated state, which is the state when the task ends normally after completion, or terminates abnormally due to an insurmountable error, or is terminated by the operating system and a process with termination rights.

[0135] Specifically, after creating a task, the processor may configure the task_struct data structure of the task.

[0136] In one possible implementation, after configuring the page size of the first virtual address space and the second virtual address space, and the access permissions of each sub-virtual address space, the processor can add a first page table entry to the task_struct data structure of the first task in response to creating the first task; and then add the second page table entry in the second page table base address register of the current task to the task_struct data structure of the first task.

[0137] The current task is the task that the processor is executing.

[0138] The first page table entry is used to indicate the page table base address of the second sub-virtual address space; the second page table entry is used to indicate the page table base address of the fourth sub-virtual address space.

[0139] The computing device includes a first page table base address register corresponding to Figure 2 TTBR1 in the embodiment shown; and the second page table base address register, corresponding to Figure 2 TTBR0 in the illustrated embodiment. For ease of description, TTBR1 will be used to refer to the first page table base address register, and TTBR0 will be used to refer to the second page table base address register.

[0140] Specifically, TTBR1 is used to store the page table base address of the first virtual address space, and TTBR0 is used to store the page table base address of the second virtual address space.

[0141] Among them, each task has its own user space. When creating a task, the processor will divide the second virtual address space according to the TG0 field in the TCR_EL1 register to obtain the PGD of the second virtual address space of the task; then allocate a physical page to store the PGD, and map the physical address of the physical page with the first address of the PGD; finally, when configuring the management information of the task, the PGD is written into the task_struct data structure of the task. In this way, when executing the task, the processor can obtain the PGD from the task_struct data structure of the task and load the PGD into TTBR0, so that the MMU can find the page table corresponding to the task through the page table base address in TTBR0.

[0142] Since in this embodiment, the processor configures the access rights of the second sub-virtual address space in the first virtual address space and regards the second sub-virtual address space as part of the user space, the processor can add a first page table entry for indicating the page table base address of the second sub-virtual address space of the task in the task_struct data structure of the newly created task, so that the processor can synchronize the corresponding first page table entry to TTBR1 when executing the task, so that the processor can access the second sub-virtual address space of the currently executing task through TTBR1 to avoid access errors.

[0143] Specifically, the processor can obtain the PGD of the first virtual address space of the first task, and then obtain the first address of the second sub-virtual address space from it; then allocate a physical page for the first task and map it with the first address of the second sub-virtual address space; finally, write the mapping relationship and the page table base address of the second sub-virtual address space, that is, the first page table entry, into the task_struct data structure of the first task.

[0144] In addition, since the processor in this embodiment uses the fourth sub-virtual address space as the kernel space by configuring the access rights of the fourth sub-virtual address space of the second virtual address space, and each task shares the same kernel space but has an independent user space; therefore, it is necessary to keep the third sub-virtual address space of each task independent of each other, and the page table base address of the fourth sub-virtual address space consistent. And because the PGD in the task_struct data structure of each task is used to manage the second virtual address space of the corresponding task, the processor can update the second page table entry of the currently executed task in TTBR0 to the PGD in the task_struct data structure of the newly created first task, thereby maintaining the consistency of the kernel space of the processor when executing different tasks, and avoiding exceptions or failures when the processor accesses the kernel space.

[0145] Specifically, the processor may write the second page table entry into the PGD, overwriting the page table entry with the same virtual address portion in the PGD.

[0146] After completing the configuration of the task_struct data structure of the task, the task is converted from the new state to the ready state; when the task switches from the ready state or the waiting state to the running state, the processor needs to switch the relevant context to ensure the normal operation of the task.

[0147] The context is a static description of the entire process of task execution. The executed task instructions and data in the related registers and stack are called the previous context, the instructions being executed and the data in the related registers and stack are called the main text, and the instructions to be executed and the data in the related registers and stack are called the following context. Context switching includes switching of virtual address space to ensure that when the CPU accesses instructions and data during task execution, it accesses the address space in memory belonging to the task.

[0148] In one possible implementation, the processor creates a first task and a second task, and executes the first task; in response to switching the first task to the second task, the processor may update the corresponding page table entry in TTBR1 according to the first page table entry in the task_struct data structure of the second task; and then update the task_struct data structure of the second task according to the second page table entry of the first task in TTBR0.

[0149] In the process of switching the first task to the second task, the processor may switch the context of the first task to the context of the second task.

[0150] Among them, the page table entry in TTBR1 is used to indicate the page table base address of the first virtual address space, that is, TTBR1 includes the first page table entry for indicating the page table base address of the second sub-virtual address space of the current task; therefore, when switching the first task to the second task, it is necessary to update the first page table entry corresponding to the first task in TTBR1 to the first page table entry corresponding to the second task, so that the processor can access its own user space through TTBR1.

[0151] Specifically, the processor may write the page table entry value of the first page table entry corresponding to the second task into TTBR1, overwriting the first page table entry corresponding to the first task.

[0152] Among them, the processor can obtain the second page table entry of the first task in TTBR0, that is, the page table base address of the fourth sub-virtual address space of the first task; and copy and write the PGD corresponding to the second task according to the second page table entry corresponding to the first task, and update the second page table entry corresponding to the second task; then, write the PGD of the second task into TTBR0 to keep the page table base address of the kernel space unchanged.

[0153] It is understandable that the processor may first update the page table entries in TTBR1 and then update the page table entries in TTBR0; or the processor may first update the page table entries in TTBR0 and then update the page table entries in TTBR1. This embodiment of the present application does not specifically limit this.

[0154] In another possible implementation, the processor can obtain the third page table entry in PGD in the task_struct data structure of the second task, where the third page table entry is used to indicate the page table base address of the third sub-virtual address space of the second task; and then update the page table entry corresponding to the third sub-virtual address space in TTBR0 according to the third page table entry.

[0155] Among them, the processor can keep the page table entry of the fourth sub-virtual address space in TTBR0 unchanged, and then update the third page table entry corresponding to the third sub-virtual address space as the user space to TTBR0.

[0156] Specifically, the processor may write the page table entry value of the third page table entry corresponding to the second task into TTBR0, overwriting the third page table entry corresponding to the first task in TTBR0.

[0157] In another possible implementation, the processor may first obtain the second page table entry of the first task in TTBR0, then write the PGD in the task_struct data structure of the second task into TTBR0, and then update the second page table entry of the second task in TTBR0 according to the second page table entry corresponding to the first task.

[0158] Through the above configuration and update actions when creating and switching tasks, access conflicts can be avoided and the security of the kernel space can be improved when both the user space and the kernel space include high addresses and low addresses.

[0159] The memory allocation process in the embodiment of the present application will be further described below in combination with two application scenarios.

[0160] See also Figure 5 , Figure 5 Schematic diagram of a memory mapping process in an embodiment of the present application, the process is executed by a computing device, the computing device includes an ARM architecture processor; Figure 5 As shown, the memory mapping process includes steps 501 to 506.

[0161] Step 501: Determine the task to be allocated memory as the target task.

[0162] Among them, the virtual address space distribution in this embodiment is as follows: Figure 4 shown.

[0163] The target task is a task that performs memory mapping (MMAP). After the processor creates a new task, it needs to allocate virtual pages in the virtual address space for the task to map the related files of the task into the virtual address space of the task; at this time, the processor can determine that the task to be allocated memory is the target task. Specifically, when the task performs MMAP, it will send an MMAP request to the operating system.

[0164] The MMAP request includes the starting address of the file in the memory, as well as the mapping length and the offset of the mapping starting address in the file.

[0165] When the processor is executing a task, if the task needs to allocate virtual memory, the processor may first determine whether the task is a target task; if the task is a target task, the processor may obtain the MMAP request and execute step 502.

[0166] If the task is not the target task, you can execute Figure 3 In steps 302 to 304 of the illustrated embodiment, virtual memory is allocated for the task.

[0167] Step 502: Determine whether the task belongs to the second control group.

[0168] After obtaining the MMAP request, the processor may first determine whether the task is in the second control group, wherein the second control group is a preset control group, and the second control group is used to control the allocation of the first virtual page to the preset user mode task.

[0169] It can be understood that, in the memory mapping scenario of this embodiment, the processor will preferentially allocate the first virtual page to the user mode tasks belonging to the second control group.

[0170] In some other possible implementations, the first control group is used to control its group members to use only the first virtual page.

[0171] In this embodiment, when the first virtual page of the second sub-virtual address space has been allocated, or the first virtual page cannot be allocated temporarily, the processor may execute step 506 .

[0172] If the task is in the second control group, step 503 is executed; if the task is not in the second control group, step 506 is executed.

[0173] Step 503: When the task belongs to the second control group, determine whether the offset of the MMAP request corresponding to the task matches the page size of the first virtual page.

[0174] After determining that the task is in the second control group, the processor may further determine whether the offset in the MMAP request matches the page size of the first virtual page.

[0175] Specifically, the processor may determine whether the offset is an integer multiple of the page size of the first virtual page. If so, the processor may determine that the MMAP request meets the requirement of using the first virtual page for memory mapping, and execute step 504; if not, the processor may determine that the MMAP request does not meet the requirement, and execute step 506.

[0176] Step 504: When the offset matches the page size of the first virtual page, determine whether the MMAP request is a fixed mapping and the mapping address range is located in the third sub-virtual address space.

[0177] Among them, after determining that the task belongs to the second control group, the processor needs to allocate a first virtual page for the task, and after determining that the offset of the MMAP request matches the page size of the first virtual page and meets the requirements for using the first virtual page for memory mapping, the processor can further determine whether the current memory mapping of the task specifically specifies the use of the second virtual page for mapping.

[0178] At this time, the processor can determine whether the MMAP request is a fixed mapping and the mapping address range is located in the third sub-virtual address space.

[0179] The processor may obtain a mapping function of the memory mapping request. If the mapping function is a fixed mapping function map_fixed, it may be determined that the memory mapping request is a fixed mapping; otherwise, it may be determined that the memory mapping request is not a fixed mapping.

[0180] If the memory mapping request is a fixed mapping, the fixed mapping function specifies the first address of the mapping in the virtual address space; and the mapping address range can be obtained according to the first address and the mapping length.

[0181] If the MMAP request is a fixed mapping and the mapping address range is within the third sub-virtual address space, it means that the current memory mapping of the task specifically specifies the use of the second virtual page for mapping and the mapping request is legal, and the processor can execute step 506 at this time.

[0182] If the MMAP request is a fixed mapping and the mapping address range is not located in the second sub-virtual address space and the third sub-virtual address space, that is, the mapping address range is located in the kernel space (the first sub-virtual address space or the fourth sub-virtual address space), the processor can return a message to the task that the memory mapping failed or that the mapping is not authorized.

[0183] If the MMAP request is not a fixed mapping, or the mapping address range of the fixed mapping is within the second sub-virtual address space, it means that the task's request to use the first virtual page for memory mapping is legal, and the processor can execute step 505.

[0184] Step 505: When the MMAP request is not a fixed mapping, or the mapping address range is located in the second sub-virtual address space, a first virtual page in the second sub-virtual address space is allocated to the task.

[0185] Among them, if the MMAP request is not a fixed mapping, the processor can obtain the second first address of the mapping, which is determined by the operating system according to the current free status of the second sub-virtual address space; and then allocate the first virtual page in the second sub-virtual address space to the task starting from the first address.

[0186] If the MMAP request is a fixed mapping and the mapping address range is located in the second sub-virtual address space, the processor may allocate the first virtual page in the second sub-virtual address space for the task starting from the first first address specified in the MMAP request.

[0187] Afterwards, the processor may map the related file of the task to the first virtual page allocated to the task.

[0188] Step 506: Allocate a second virtual page in the third sub-virtual address space to the task.

[0189] Among them, if the task is not in the second control group, or the offset in the MMAP request does not match the page size of the first virtual page, the processor can obtain the mapped third first address, which is determined by the operating system based on the current free status of the third sub-virtual address space, and then allocate the second virtual page for the task starting from this first address.

[0190] If the memory mapping request is a fixed mapping and the mapping address range is within the third sub-virtual address space, the processor may allocate a second virtual page within the third sub-virtual address space to the task based on the first first address specified in the fixed mapping function.

[0191] Afterwards, the processor may map the related file of the task to the second virtual page allocated to the task.

[0192] In an embodiment of the present application, the memory mapping behavior requested by the task can be authenticated through the control group to which the task belongs and the mapping address range indicated by the MMAP request, and a virtual page of a corresponding size can be allocated, which can meet different memory mapping requirements while ensuring the security of the memory mapping.

[0193] See also Figure 6 , Figure 6 Schematic diagram of a page fault processing process in an embodiment of the present application, the process is executed by a computing device, the computing device includes an ARM architecture processor; Figure 6 As shown, the page fault processing flow includes steps 601 to 608. The virtual address space distribution in this embodiment is as follows Figure 4 shown.

[0194] Step 601: Determine the first physical page accessed by the task based on the target virtual page and the page table.

[0195] The target virtual page is the virtual page indicated by the virtual address in the access request of the task.

[0196] In the process of executing a task, the processor needs to access the corresponding physical page loaded with the relevant file data of the task through the virtual page in the user space of the task. At this time, the processor can obtain the virtual address of the target virtual page in the memory access request, and then determine the corresponding first physical page based on the page table entry of the page table.

[0197] It is understandable that after establishing a mapping between a virtual page and the relevant files or data of a corresponding task, the processor may not immediately allocate a physical page corresponding to the virtual page to the task, but instead allocates a physical page to the task by triggering a page fault handler when the task accesses the virtual page.

[0198] Therefore, in the case of determining that the first physical page is missing, the processor may execute step 602 .

[0199] Step 602: In response to a first physical page miss, determine whether the page size of the target virtual page matches the page size of the first virtual page.

[0200] Among them, a page fault usually occurs in the access of user space. This embodiment takes the page fault that occurs when the processor accesses the user space as an example for explanation. When the processor executes a user-mode task and accesses the memory through the second sub-virtual address space or the third sub-virtual address space, if the accessed target virtual page cannot find the corresponding first physical page in both the TLB and the memory page table, the processor can determine that the first physical page is missing and a page fault occurs.

[0201] After determining that a page error occurs, the processor may first determine whether the page size of the target virtual page matches the page size of the first virtual page, and then determine the page size of the first physical page.

[0202] Exemplarily, the page size of the first virtual page is 64KB, and the page size of the second virtual page is 4KB; if the page size of the target virtual page is 64KB, the processor can determine that the page size of the target virtual page matches the page size of the first virtual page; if the page size of the target virtual page is 4KB, the processor can determine that the page size of the target virtual page matches the page size of the second virtual page.

[0203] If the page size of the target virtual page matches the page size of the first virtual page, the processor executes step 603 ; if the page size of the target virtual page matches the page size of the second virtual page, the processor executes step 605 .

[0204] It can be understood that in this embodiment, the virtual pages of the entire virtual address space include a first virtual page and a second virtual page. That is, when the page size of the target virtual page does not match the page size of the first virtual page, the page size of the target virtual page matches the page size of the second virtual page.

[0205] In some other embodiments, when the virtual address space includes virtual pages of greater granularity, embodiments with the same effect can be obtained based on the adaptation and adjustment of this embodiment, and these embodiments should be within the protection scope of the embodiments of the present application.

[0206] Step 603: When the page size of the target virtual page matches the page size of the first virtual page, determine whether the virtual address corresponding to the target virtual page is located in the second sub-virtual address space.

[0207] After determining the page size of the target virtual page, the processor may further determine the sub-virtual address space where the target virtual page is located to determine whether the access of the task is legal.

[0208] Specifically, when the page size of the target virtual page matches the page size of the first virtual page, the processor may further determine whether the target virtual page is in the second sub-virtual address space.

[0209] If the target virtual page is in the second sub-virtual address space, it means that the access target of the task is the first virtual page in the user space, and the processor has the corresponding access rights when executing the task, so step 604 can be executed.

[0210] If the target virtual page is not in the second sub-virtual address space, that is, in the first sub-virtual address space, it means that the access target of the task is the kernel space, and the processor does not have the corresponding access rights when executing the task. At this time, the processor can execute step 608.

[0211] Step 604: When the virtual address is located in the second sub-virtual address space, allocate a second physical page for the task.

[0212] The second physical page has the same page size as the first virtual page.

[0213] When it is determined that a page miss occurs and the access behavior of the task is legal, the processor may allocate a second physical page having the same page size as the first virtual page to the task, so as to map the second physical page to the target virtual page.

[0214] In a possible implementation, when the page fault is a soft page fault, that is, when the data content corresponding to the target virtual page has been loaded into the second physical page of the memory, the processor may allocate the second physical page to the task and then execute step 607 .

[0215] In another possible implementation, when the page miss is a hard page miss, that is, the data content corresponding to the target virtual page is not loaded into the second physical page of the memory, the processor can determine an idle physical page with the same page size as the first virtual page as the second physical page, and load the data content of the task corresponding to the target virtual page into the second physical page, and then allocate the second physical page to the task, and then execute step 607.

[0216] Step 605: When the page size of the target virtual page does not match the page size of the first virtual page, determine whether the virtual address corresponding to the target virtual page is located in the third sub-virtual address space.

[0217] If the target virtual page is not the first virtual page, then it is the second virtual page. At this time, the processor can determine whether the target virtual page is in the third sub-virtual address space to determine whether the access behavior is legal.

[0218] If the target virtual page is in the third sub-virtual address space, it means that the access target of the task is the second virtual page corresponding to the user space, and the processor has the corresponding access rights when executing the task, so step 606 can be executed; if the target virtual page is not in the third sub-virtual address space, that is, it is in the fourth sub-virtual address space, it means that the access target of the user-state task is the kernel space, and the processor does not have the corresponding access rights when executing the user-state task, and the processor can execute step 608 at this time.

[0219] Step 606: When the virtual address is located in the third sub-virtual address space, allocate a third physical page to the task.

[0220] The third physical page has the same page size as the second virtual page.

[0221] It can be understood that the specific implementation of step 606 is similar to that of step 604 , and the third physical page is equivalent to the second physical page of step 604 , which will not be described in detail here.

[0222] Step 607: Add the mapping relationship between the physical page assigned to the task and the target virtual page to the page table.

[0223] After allocating the physical page to the task, the processor may write the mapping relationship between the physical page and the target virtual page into the corresponding page table so that the task can access it again later.

[0224] Step 608: Return access failure information.

[0225] When the processor determines that the access target of the current user-mode task is the kernel space, it may return an access failure message or an insufficient access permission message to the task.

[0226] In an embodiment of the present application, when a page error is detected, the access behavior is authenticated, and when it is determined that the access target is the user space, the corresponding physical page allocation is performed, which can improve the security of the access behavior.

[0227] See also Figure 7 , Figure 7 This is a schematic diagram of the structure of a computing device provided in an embodiment of the present application. Figure 7 As shown, the computing device 700 provided in the embodiment of the present application includes a processor 710 and a memory 720. The processor 710 is an ARM architecture processor.

[0228] Among them, the processor 710 may include one or more processing cores. The processor 710 uses various interfaces and lines to connect various parts within the computing device 700, and executes the methods provided by any one or more of the above embodiments by running or executing instructions, programs, code sets or instruction sets stored in the memory 720, and calling data stored in the memory 720. Optionally, the processor 710 can be implemented in at least one hardware form of digital signal processing (digital signal processing, DSP), field-programmable gate array (field-programmable gate array, FPGA), and programmable logic array (programmable logic array, PLA). The processor 710 can integrate one or a combination of several of a CPU, a graphics processing unit (graphics processing unit, GPU) and a modem. It is understandable that the above-mentioned modem may not be integrated into the processor 710, but may be implemented separately through a communication chip.

[0229] The memory 720 may include a random access memory (RAM) or a read-only memory (ROM). Optionally, the memory 720 includes a non-transitory computer-readable storage medium. The memory 720 may be used to store instructions, programs, codes, code sets, or instruction sets. The memory 720 may include a program storage area. The program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the method of the embodiment of the present application, etc.

[0230] The processor 710 and the memory 720 are connected to each other through a bus in the computing device 700, which may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 7 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0231] In another embodiment of the present application, a computer-readable storage medium is further provided, wherein the computer-readable storage medium stores computer-executable instructions. When at least one processor of the device executes the computer-executable instructions, the device executes the above Figure 3 , Figure 5 or Figure 6 The method flow described in any embodiment.

[0232] In another embodiment of the present application, a computer program product is also provided. The computer program product includes computer-executable instructions, which are stored in a computer-readable storage medium; at least one processor of the device can read the computer-executable instructions from the computer-readable storage medium, and at least one processor executes the computer-executable instructions so that the device performs the above Figure 3 , Figure 5 or Figure 6 The method flow described in any embodiment.

[0233] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the embodiments of the present application.

[0234] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0235] In the several embodiments provided in the embodiments of the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0236] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0237] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0238] If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), disk or optical disk and other media that can store program codes.

Claims

1. A memory allocation method, characterized in that: The memory allocation method is applied to a computing device of an ARM architecture; the method comprises: Configuring a page size of a first virtual page of a first virtual address space and a page size of a second virtual page of a second virtual address space; wherein the page size of the first virtual page is larger than the page size of the second virtual page; Determine whether the task to be allocated memory belongs to a preset control group; the preset control group includes a first control group and a second control group; wherein the first control group is used to control the allocation of the first virtual page to the preset kernel mode task; the second control group is used to control the allocation of the first virtual page or the second virtual page to the preset user mode task; In the case where the task belongs to the preset control group, allocating the first virtual page or the second virtual page to the task based on the control group to which the task belongs; The first virtual address space includes a second sub-virtual address space; the second virtual address space includes a fourth sub-virtual address space; In response to creating a first task, a first page table entry is added to a task_struct data structure of the first task; the first page table entry is used to indicate a page table base address of the second sub-virtual address space, and the second sub-virtual address space is a user state address space; The second page table entry in the second page table base address register of the current task is added to the task_struct data structure of the first task to maintain the consistency of the kernel space when the processor executes different tasks, and avoid exceptions or failures when the processor accesses the kernel space, wherein the second page table base address register is used to store the page table base address of the second virtual address space; the second page table entry is used to indicate the page table base address of the fourth sub-virtual address space, the fourth sub-virtual address space is the kernel space shared by each task, the current task is the task being executed, the first task is the task executed after the current task is completed, and the fourth sub-virtual address space is the kernel state address space.

2. The method according to claim 1, characterized in that The first virtual address space includes a first sub-virtual address space and a second sub-virtual address space; wherein the first sub-virtual address space is a kernel state address space; In the case where the task belongs to the preset control group, allocating the first virtual page or the second virtual page to the task based on the control group to which the task belongs includes: In the case where the task belongs to the first control group, allocating a first virtual page located in the first sub-virtual address space to the task; In a case where the task belongs to the second control group, a first virtual page located in the second sub-virtual address space is allocated to the task.

3. The method according to claim 1, characterized in that The first virtual address space includes a first sub-virtual address space, and the second virtual address space includes a third sub-virtual address space; wherein the first sub-virtual address space is a kernel state address space; and the third sub-virtual address space is a user state address space; In the case where the task belongs to the preset control group, allocating the first virtual page or the second virtual page to the task based on the control group to which the task belongs includes: In the case where the task belongs to the first control group, allocating a first virtual page located in the first sub-virtual address space to the task; In a case where the task belongs to the second control group, a second virtual page located in the third sub-virtual address space is allocated to the task.

4. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: When the task does not belong to the preset control group, the second virtual page is allocated to the task.

5. The method according to claim 4, characterized in that The second virtual address space includes a fourth sub-virtual address space; When the task does not belong to the preset control group, allocating the second virtual page to the task includes: In the case that the task does not belong to the preset control group, determining whether the task is a kernel state task; In the case that the task is the kernel mode task, a second virtual page located in the fourth sub-virtual address space is allocated to the task.

6. The method according to claim 1, characterized in that The second control group is used to control the allocation of the first virtual page to the preset user mode task; the first virtual address space includes a second sub-virtual address space; the second virtual address space includes a third sub-virtual address space; wherein the second sub-virtual address space and the third sub-virtual address space are user mode address spaces; Before determining whether the task to be allocated memory belongs to a preset control group, the method further includes: Determine whether the task is a target task, wherein the target task is a task that performs memory mapping MMAP; The step of determining whether the task to be allocated memory belongs to a preset control group includes: In the case where the task is the target task, determining whether the task belongs to the second control group; In the case where the task belongs to the preset control group, allocating the first virtual page or the second virtual page to the task based on the control group to which the task belongs includes: In a case where the task belongs to the second control group, determining whether an offset in the MMAP request corresponding to the task matches a page size of the first virtual page; In a case where the offset matches the page size of the first virtual page, determining whether the MMAP request is a fixed mapping, and a mapping address range of the MMAP request is located in the third sub-virtual address space; When the MMAP request is not a fixed mapping, or the mapping address range is located in the second sub-virtual address space, the first virtual page located in the second sub-virtual address space is allocated to the task.

7. The method according to claim 2, characterized in that After the control group to which the task belongs allocates the first virtual page or the second virtual page to the task, the method further includes: Determining a first physical page accessed by the task based on a target virtual page and a page table, wherein the target virtual page is a virtual page indicated by a virtual address in an access request of the task; In response to the first physical page being missing, determining whether a page size of the target virtual page matches a page size of the first virtual page; In a case where the page size of the target virtual page matches the page size of the first virtual page, determining whether the virtual address corresponding to the target virtual page is located in the second sub-virtual address space; In a case where the virtual address corresponding to the target virtual page is located in the second sub-virtual address space, allocating a second physical page to the task, wherein the second physical page has the same page size as the first virtual page; A mapping relationship between the second physical page and the target virtual page is added to the page table.

8. The method according to claim 1, characterized in that After adding the second page table entry of the current task in the second page table base address register to the task_struct data structure of the current task, the method further comprises: In response to switching the current task to a first task, updating a corresponding page table entry in a first page table base address register according to a first page table entry in a task_struct data structure of the first task; wherein the first page table base address register is used to store a page table base address of the first virtual address space; According to the second page table entry of the current task in the second page table base address register, the task_struct data structure of the first task is updated.

9. A computing device, characterized in that The computing device includes a memory and a processor; wherein the processor is an ARM architecture; The memory is coupled to the processor; The memory is used to store computer program instructions; The processor is used to call the computer program instructions to execute the memory allocation method as described in any one of claims 1-8.

Citation Information

Patent Citations

  • User state double-control memory mirroring implement method

    CN103530241A

  • Method and device for managing memories

    CN104750557A

  • Memory access method and apparatus

    WO2024021995A1