The present invention relates to the field of computers, and in particular, to a memory management method and device, and a memory controller.
In a computer system, a memory controller is mainly responsible for data exchanges between a CPU and a memory, and physical address management of the memory is implemented by an operating system. The operating system allocates a virtual address to a process. If a translation look-aside buffer (TLB) does not cache the virtual address, and a memory management unit (MMU) does not find a page table entry of the virtual address, a page fault occurs and the CPU is trapped in the kernel of the operating system. When the page fault occurs, the operating system enters an interrupt service routine, performs site protection, pushes various status information of a current instruction such as a program counter onto a stack, and checks validity of the virtual address. If determining that the access is invalid, the operating system usually feeds back a signal to the process or directly kills the process.
In the prior art, if the MMU does not find the page table entry of the virtual address, the MMU directly triggers a page fault, and causes the CPU to be trapped in the kernel of the operating system for a long time. When the page fault occurs, the operating system needs to perform a series of processing, which increases load on the operating system, and management efficiency is relatively low.
Embodiments of the present invention provide a memory management method and device, and a memory controller, to improve memory management efficiency.
According to a first aspect, embodiments of the present invention provide a memory management method, where the method is applied to a memory management device, and the memory management device is located in a memory controller. The method includes receiving a fetch request sent by a processor, where the fetch request carries a virtual address. The method also includes finding whether a translation look-aside buffer TLB caches a page table corresponding to the virtual address carried in the fetch request; and if yes, obtaining a physical address corresponding to the virtual address from the page table; or if not, obtaining a base address field of the page table and searching for the page table, and if the page table includes the virtual address carried in the fetch request, obtaining a physical address corresponding to the virtual address, where the page table includes a correspondence between virtual addresses and physical addresses, and the TLB is located in the memory management device; and if the page table does not include the virtual address carried in the fetch request, determining whether the virtual address is valid, and applying for a blank page when it is determined that the virtual address is valid.
In a first possible implementation manner of the first aspect, the method further includes: if the application for the blank page succeeds starting memory reading and writing; or if the application for the blank page fails, selecting a replacement page and providing the replacement page to an operating system so that the operating system replace.
With reference to the first aspect, or the first possible implementation manner of the first aspect, in a second possible implementation manner, the determining whether the virtual address is valid specifically includes: determining whether the virtual address falls within an allocated virtual address space, and if yes, determining that the virtual address is valid; or if not, finding whether the virtual address exists in a virtual memory address VMA space, and if yes, determining that the virtual address is valid.
With reference to the first aspect, or the first possible implementation manner or the second possible implementation manner of the first aspect, in a third possible implementation manner, before the applying for a blank page, the method further includes: counting a frequency of use of each cached blank page in a preset period; and selecting a blank page having a lowest frequency of use in the preset period when applying for the blank page.
With reference to the first possible implementation manner of the first aspect, in a fourth possible implementation manner, after the application for the blank page succeeds, the method further includes: updating the page table so that the page table includes the virtual address carried in the fetch request, and saving the page table to the TLB.
With reference to any one of the first aspect, or the first to the fourth possible implementation manners of the first aspect, in a fifth possible implementation manner, if it is found that the page table includes the virtual address carried in the fetch request, before the step of obtaining a physical address corresponding to the virtual address, the method further includes: saving the page table to the TLB.
According to a second aspect, embodiments of the present invention provide a memory management device, where the memory management device is located in a memory controller. The memory management device includes a translation look-aside buffer (TLB), a memory management unit (MMU) and a microcontroller. The TLB is configured to: receive a fetch request sent by a processor, where the fetch request carries a virtual address, and find whether the TLB caches a page table corresponding to the virtual address carried in the fetch request; and if yes, obtain a physical address corresponding to the virtual address from the page table, where the page table includes a correspondence between virtual addresses and physical addresses. The MMU is configured to: when the TLB does not cache a page table corresponding to the virtual address carried in the fetch request, obtain a base address field of the page table and search for the page table, and if the page table includes the virtual address, obtain a physical address corresponding to the virtual address. The microcontroller is configured to: when the page table does not include the virtual address, determine whether the virtual address is valid, and when it is determined that the virtual address is valid, apply for a blank page.
In a first possible implementation manner of the second aspect, the microcontroller is further configured to: when the application for the blank page succeeds, start memory reading and writing, and when the application for the blank page fails, select a replacement page and provide the replacement page to an operating system, so that the operating system replaces a page.
With reference to the second aspect, or the first possible implementation manner of the second aspect, in a second possible implementation manner, the microcontroller is further configured to: determine whether the virtual address falls within an allocated virtual address space, and if yes, determine that the virtual address is valid; or if not, find whether the virtual address exists in a virtual memory address VMA space, and if yes, determine that the virtual address is valid.
With reference to the second aspect, or the first possible implementation manner or the second possible implementation manner of the second aspect, in a third possible implementation manner, the microcontroller is further configured to: receive the fetch request, obtain a base address field of a page table of a process corresponding to the virtual address carried in the fetch request, and send the base address field to the MMU, so that the MMU searches for the page table according to the base address field.
With reference to any one of the second aspect, or the first to the third possible implementation manners of the second aspect, in a fourth possible implementation manner, the microcontroller is further configured to: count a frequency of use of each cached blank page in a preset period, and select a blank page having a lowest frequency of use in the preset period when applying for the blank page.
According to a third aspect, the present invention provides a memory controller, including: any memory management device according to the second aspect.
It can be learned from the foregoing technical solutions that the embodiments of the present invention have the following advantages.
In the technical solutions of the present invention, a memory is managed by a storage management device, so that load on an operating system can be reduced. When the storage management device determines that a page table does not include a virtual address carried in a fetch request, the storage management device does not trigger an interrupt of an operating system, but instead the memory management device manages the memory, and when determining that the virtual address is valid, applies for a blank page. The memory management device is located in a memory controller and is closer to a physical memory, so that memory management efficiency can be effectively improved.
To describe the technical solutions in the embodiments of the present invention more clearly, the following briefly describes the accompanying drawings required for describing the embodiments. Apparently, the accompanying drawings in the following description show merely some embodiments of the present invention, and persons skilled in the art may still derive other drawings from these accompanying drawings without creative efforts.
The following clearly describes the technical solutions in the embodiments of the present invention with reference to the accompanying drawings in the embodiments of the present invention. Apparently, the described embodiments are merely some but not all of the embodiments of the present invention. All other embodiments obtained by a person skilled in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
First, a memory management method provided in an embodiment of the present invention may be applied to a communications system. The memory management method may be applied to a memory management device, and the memory management device is located in a memory controller.
For details, refer to
When a cached resource requested by a processor is not hit, a fetch request may be sent to the memory controller 100 through a request channel to access a memory. The memory is mainly managed by the memory management device 101 in the memory controller 100 according to the buffer request.
A memory management method is applied to the memory management device 101. The memory management device 101 is located in the memory controller 100. The method includes: receiving a fetch request sent by a processor, where the fetch request carries a virtual address; finding whether a translation look-aside buffer (TLB) caches a page table corresponding to the virtual address carried in the fetch request; and if yes, obtaining a physical address corresponding to the virtual address from the page table; or if not, obtaining a base address field of the page table and searching for the page table, and if the page table includes the virtual address carried in the fetch request, obtaining a physical address corresponding to the virtual address, where the page table includes a correspondence between virtual addresses and physical addresses, and the TLB is located in the memory management device; and if the page table does not include the virtual address carried in the fetch request, determining whether the virtual address is valid, and applying for a blank page when it is determined that the virtual address is valid.
Referring to
Step S201: Receive a fetch request sent by a processor, where the fetch request carries a virtual address.
When a cached resource requested by a processor is not hit, a fetch request may be sent to the memory management device 101 in the memory controller 100 through a request channel to access a memory. The fetch request carries the virtual address. A translation look-aside buffer (TLB) and a microcontroller in the memory management device 101 can receive the fetch request sent by the processor. The TLB is mainly responsible for fast address translation and searching for a page table, and the microcontroller is mainly responsible for managing a memory (including checking validity of the fetch request and so on). That is, the microcontroller needs to perform an operation such as memory management only when the TLB does not cache a relevant page table.
A computer system as an example to describe the present invention, the processor may be a CPU core.
Step S202: Find whether the translation look-aside buffer TLB caches a page table corresponding to the virtual address carried in the fetch request; and if not, step S203 is executed; or if yes, step S205 is executed, where the page table includes a correspondence between virtual addresses and physical addresses.
The page table is usually stored in a primary storage, and the page table stores the correspondence between the virtual addresses and the physical addresses. The TLB can cache a page table file. Therefore, if the TLB caches the TLB, the physical address corresponding to the virtual address may be directly obtained from the TLB; and if the TLB does not cache the page table, the page table may be searched for by using a memory management unit (MMU) in the memory management device 101.
Step S203: If the TLB does not cache the page table corresponding to the virtual address carried in the fetch request, obtain a base address field of the page table.
Because the page table is usually stored in the primary storage, the MMU may fail to obtain the base address field of the page table and therefore cannot directly search for the page table. In this case, the base address field of this page table may be provided by the microcontroller to the MMU, where a portal address of the page table portal address=the base address field+several highest bits of the virtual address.
Specifically, before the MMU searches for the page table and after the microcontroller receives a cache request, the base address field of the page table of a process corresponding to the virtual address carried in the fetch request may be obtained, and the base address field of the page table is provided to the MMU, so that the MMU searches for the page table according to the base address field of the page table. In an implementation manner, the microcontroller may search a process table to obtain the base address field of the page table of the process corresponding to the virtual address, and send the base address field of the page table to the MMU.
Step S204: Search for the page table according to the base address field of the page table, if it is found that the page table includes the virtual address carried in the fetch request, execute step S205, or if not, execute step S206.
If the page table includes the virtual address carried in the fetch request, step S205 is executed, and after the physical address corresponding to the virtual address is obtained, memory data may be read according to the physical address. If the page table does not include the virtual address carried in the fetch request, step S206 is executed.
Step S205: Obtain the physical address corresponding to the virtual address carried in the fetch request.
In step S202, if it is found that the translation look-aside buffer TLB caches the page table corresponding to the virtual address carried in the fetch request, the physical address corresponding to the virtual address is obtained from the page table by the TLB in step S205; and if the MMU finds that the page table includes the virtual address carried in the fetch request in step S204, the physical address corresponding to the virtual address carried in the fetch request is obtained by the MMU in step S205.
Step S206: If it is found that the page table does not include the virtual address carried in the fetch request, determine whether the virtual address is valid and apply for a blank page when it is determined that the virtual address is valid.
If the TLB does not cache the page table, and the page table in the MMU is not hit, the microcontroller can determine whether the virtual address carried in the fetch request is valid. The determining whether the virtual address carried in the fetch request is valid specifically includes: determining whether the virtual address falls within an allocated virtual address space, and if yes, determining that the virtual address is accessed for the first time and that the virtual address is valid; or if not, finding whether the virtual address exists in a virtual memory address (VMA) space, and if yes, determining that the virtual address is valid.
After determining that the virtual address exists in the VMA space, a page fault may be fed back to an operating system, and the microcontroller applies for the blank page.
If the virtual address is valid, the microcontroller applies for the blank page, and if the application for the blank page succeeds, memory reading and writing can be started directly; if the application for the blank page fails, a replacement page is selected, and the replacement page is provided to the operating system, and data is written by the operating system to a hard disk from the memory. If a replaced page is “clean”, the operating system can directly use the replaced page after modifying a page table entry of the replaced page; or if the replaced page is “dirty”, the operating system needs to first write the “dirty” page to a magnetic disk, generate a context switch, and suspend this process until the operation of the magnetic disk ends; and then pop a status before the page fault occurs from a stack. From then on, the process enters ready mode, and can be scheduled for execution by the operating system at any time.
In addition, after the application for the blank page succeeds, the method may further include: determining whether the applied blank page is validly accessed for the first time; and if yes, the operation of memory reading and writing can be started directly, or if not, a feedback is sent to the operating system to indicate that the blank page is ready, so that the operating system in the processor writes the data from the hard disk to the memory.
If it is found that in step S206 that the virtual address does not exist in the VMA space, the virtual address is determined as invalid, that is, the fetch request is determined as invalid access, a fault is fed back to the operating system, and the operating system executes the following step.
As can be learned from the above, in the technical solutions of the present invention, when a storage management device determines that a page table does not include a virtual address carried in a fetch request, the storage management device does not trigger an interrupt of an operating system, but instead the memory management device manages a memory, and when determining that the virtual address is valid, applies for a blank page. The memory management device is located in the memory controller and is closer to a physical memory, so that memory management efficiency can be effectively improved.
To better understand the foregoing solution, an embodiment of the present invention will be described in detail by using a specific implementation example. For details, refer to
Step S301: A TLB receives a fetch request sent by a processor, where the fetch request carries a virtual address.
Step S302: The TLB finds whether the TLB caches a page table corresponding to the virtual address carried in the fetch request; and if not, execute step S303; or if yes, execute step S305.
Step S303: A microcontroller receives the fetch request sent by the processor, where the fetch request carries the virtual address, and the microcontroller searches a process table to obtain a base address field of a page table of a process corresponding to the virtual address, and sends the base address field of the page table to the MMU; and execute step S304.
The process table is a data structure table that is maintained locally by a memory management processor core and that may be stored in an on-chip SRAM, and the process table may specifically include table entries such as: a process ID, a processor core ID, a physical space quota and a management policy. The table entries are transmitted to a memory controller by using an interaction channel during process switching, a portal base address of the page table is created and maintained by the memory management processor, and the process table is invisible to the operating system. For details of the specific implementation manner, refer to Table 1:
The microcontroller receives the fetch request sent by the processor, where the buffer request may carry a process ID, and the microcontroller may find a location of the page table corresponding to the process according to the process ID, obtain the base address field of the page table and provide the base address field to the MMU, so that the MMU can search for the page table according to the virtual address carried in the fetch request.
It can be understood that there is no chronological relationship between step S301 and step S303, and the microcontroller and the TLB may receive the fetch request sent by the processor at the same time.
Step S304: The MMU searches for the page table according to the base address field of the page table sent by the microcontroller. If the page table is hit, execute step S305, or if the page table is not hit, execute step S306.
The MMU searches for the page table according to the base address field of the page table sent by the microcontroller, and if the page table is hit, it indicates that the page table includes the virtual address carried in the fetch request, so a physical address corresponding to the virtual address can be obtained.
Step S305: Obtain the physical address corresponding to the virtual address.
If the process proceeds from step S302 to step S305, the physical address corresponding to the virtual address is obtained by the TLB, and memory data may be accessed by using the physical address; and if the process proceeds from step S304 to step S305, the physical address corresponding to the virtual address is obtained by the MMU, and the memory data may be accessed by using the physical address.
Step S306: The microcontroller checks whether the virtual address carried in the fetch request falls within a virtual address space that is allocated recently; and if not, execute step S307; or if yes, the microcontroller determines that the virtual address is valid, and execute step S310.
For example, a memory allocation table may be integrated in the memory controller, content recorded in table entries of the memory allocation table may include: a process ID, a virtual address of a process Malloc and a size of the virtual address. The information is transmitted by the operating system to the memory controller by using an interactive interface when a process calls a Malloc function.
When neither the TLB nor the MMU is hit, the microcontroller searches for the memory allocation table. Moreover, the microcontroller can search for the memory allocation table after receiving the fetch request, that is, during the processing of the TLB and the MMU, the microcontroller performs processing at the same time. When neither the TLB nor the MMU is hit, but the memory allocation table is hit, it indicates that the fetch request is valid and is accessed for the first time, and has not yet been allocated a page. The microcontroller can directly apply for a blank page to use for the request.
If none of the TLB, the MMU and the memory allocation table is hit, step S307 is executed, and the microcontroller needs to check the VMA space of the process in the memory, thereby further determining a reason why the MMU is not hit.
In this case, few resources are occupied, logic is simple and determining can be made quickly. After Malloc entries increase, the memory allocation table can implement a certain replacement policy, for example, the simplest first in first out (FIFO) policy.
Step S307: The microcontroller looks for a virtual memory address VMA space of the process in the memory, and determines whether the virtual address exists; if yes, execute step S309; or if not, execute step S308.
Step S308: The microcontroller determines the request as an invalid access, and feeds back an interrupt to the operating system.
A next operation is executed by the operating system, and the process ends.
Step S309: The microcontroller determines whether virtual address data of the request is in a SWAP partition or a file, and feeds back a page fault to the operating system; and execute step S310.
Step S310: The microcontroller searches for a free list and applies for a blank page; and if the application fails, the process proceeds to step S311; or if the application succeeds, the process proceeds to step S313.
Step S311: The microcontroller runs a page replacement algorithm to select a page and replace the page in the memory.
Step S312: The microcontroller updates page table entries and the TLB, and writes an address of the selected to-be-replaced page to a register of a feedback space, and feeds back a signal indicating that the replacement page is ready to the operating system.
If the replacement page is selected, a page table entry needs to be constructed for the new page, and the microcontroller needs to update the page table and the TLB.
After the microcontroller feeds back a signal to the operating system that the replacement page is ready, a next operation is executed by the operating system, and the process ends.
Step S313: The microcontroller updates the TLB and the page table entries.
If the application for the blank page succeeds, the page table entry needs to be constructed for the new page, and the microcontroller needs to update the page table and the TLB.
Step S314: The microcontroller determines whether the blank page is applied for the first time; if the fetch request is not a first valid access, the process proceeds to step S315; or if yes, execute step S316.
Step S315: The microcontroller writes a register value to the feedback space, and sends the signal that the replacement page is ready to the operating system.
The operating system determines a next operation after reading various information of the feedback space.
Step S316: The microcontroller directly starts a memory writing and reading operation.
As can be learned from the above, in the technical solution of the present invention, when an operating system needs to modify or search for a page table, the operating system only needs to send an instruction to a microcontroller, and the microcontroller can find a location of the page table in a memory according to a process ID, and automatically perform a corresponding operation. Because the memory is managed by the memory management device, load on the operating system can be reduced and memory management efficiency can be effectively improved.
To better implement the memory management method provided in the embodiments of the present invention, an embodiment of the present invention further provides a device based on the foregoing memory management method. Meanings of terms are the same as those in the foregoing memory management, and for details of implementation, refer to the description in the method embodiments.
A memory management device 101 is located in a memory controller. The memory management device 101 specifically includes a translation look-aside buffer TLB 1011, a memory management unit MMU 1012 and a microcontroller 1013. Referring to
The TLB 1011 is configured to: receive a fetch request sent by a processor, where the fetch request carries a virtual address, and find whether the TLB caches a page table corresponding to the virtual address carried in the fetch request; and if yes, obtain a physical address corresponding to the virtual address from the page table, where the page table includes a correspondence between virtual addresses and physical addresses.
The MMU 1012 is configured to: when the TLB does not cache the page table corresponding to the virtual address carried in the fetch request, obtain a base address field of the page table and search for the page table, and if the page table includes the virtual address, obtain a physical address corresponding to the virtual address; and
The microcontroller 1013 is configured to: when the page table does not include the virtual address, determine whether the virtual address is valid, and when it is determined that the virtual address is valid, apply for a blank page.
In an embodiment, the microcontroller 1013 is further configured to: when the application for the blank page fails, select a replacement page and provide the replacement page to an operating system, so that the operating system replaces a page.
In an embodiment, the microcontroller 1013 is specifically configured to: determine whether the virtual address falls within an allocated virtual address space; and if yes, determine that the virtual address is valid, or if not, find whether the virtual address exists in a virtual memory address VMA space, and if yes, determine that the virtual address is valid.
In an embodiment, the microcontroller 1013 is further configured to: receive the fetch request, and obtain a base address field of a page table of a process corresponding to the virtual address carried in the fetch request, and send the base address field to the MMU, so that the MMU searches for the page table according to the base address field.
In an embodiment, the microcontroller 1013 is further configured to: count a frequency of use of each cached blank page in a preset period, and select a blank page having a lowest frequency of use in the preset period when applying for the blank page.
Moreover, the memory management device 101 may further include: a configuration space 1014 and a feedback space 1015, where the configuration space and the feedback space are spaces for direct information exchange between the operating system and the memory controller. For example, the configuration space and the feedback space may be implemented as a pile of addressable registers. The operating system can send information to the configuration space through an interaction channel, and may obtain information from the feedback space. The microcontroller can directly read information from the configuration space, and can write information to the feedback space.
Moreover, the memory management device 101 may further include: a data cache module 1016, configured to cache two-way data of reading and writing.
In an implementation manner, code of the microcontroller may run in an on-chip SRAM.
Both the TLB and the MMU in this embodiment of the present invention are located in the memory controller and are closer to a physical memory, facilitating memory management by hardware.
As can be learned from the above, in this embodiment of the present invention, a memory is managed by a storage management device, so that load on an operating system can be reduced. When the storage management device determines that a page table does not include a virtual address carried in a fetch request, the storage management device does not trigger an interrupt of an operating system, but instead the memory management device manages the memory, and when determining that the virtual address is valid, applies for a blank page. The memory management device is located in a memory controller and is closer to a physical memory, so that memory management efficiency can be effectively improved.
Embodiments of the present invention further provide a memory controller. For details, refer to
The memory controller in this embodiment may be integrated in a CPU, or may be an independent chip.
In the foregoing embodiments, the description of each embodiment has respective focuses. For a part that is not described in detail in an embodiment, refer to related descriptions in other embodiments.
In the several embodiments provided in this application, it should be understood that the disclosed system, apparatus, and method may be implemented in other manners. For example, the described apparatus embodiment is merely exemplary. For example, the unit division is merely logical function division and may be other division in actual implementation. For example, a plurality of units or components may be combined or integrated into another system, or some features may be ignored or not performed. In addition, the displayed or discussed mutual couplings or direct couplings or communication connections may be implemented by using some interfaces. The indirect couplings or communication connections between the apparatuses or units may be implemented in electronic, mechanical, or other forms.
Functional units in the embodiments of the present invention may be integrated into one processing unit, or each of the units may exist alone physically, or two or more units are integrated into one unit. The integrated unit may be implemented in a form of hardware, or may be implemented in a form of a software functional unit.
The memory management method and device and the memory controller provided in the embodiments of the present invention are described in detail above, and the principle and implementation manners of the present invention are described herein through specific examples. The foregoing description about the embodiments is merely provided to help understand the method and core ideas of the present invention. In addition, persons of ordinary skill in the art can make modifications to the specific implementation manners and application scopes according to the ideas of the present invention. In conclusion, the content of the specification should not be construed as a limit to the present invention.
Number | Date | Country | Kind |
---|---|---|---|
201410373858.2 | Jul 2014 | CN | national |
This application is a continuation of International Application No. PCT/CN2015/084798, filed on Jul. 22, 2015, which claims priority to Chinese Patent Application No. 201410373858.2, filed on Jul. 31, 2014. The disclosures of the aforementioned applications are hereby incorporated by reference in their entireties.
Number | Date | Country | |
---|---|---|---|
Parent | PCT/CN2015/084798 | Jul 2015 | US |
Child | 15415344 | US |