The MMU is made up of one or a group of chips, whose function is to map the logical address to a physical address for address translation (the MMU is part of the CPU)
Machine instructions still specify the address of an operand or an address of an instruction with a logical address
Each logical address consists of a segment selector (16 bits) and a relative offset (32 bits) within the segment. The only purpose of the segment register is to hold the segment selector.
The MMU consists of two parts: a segmented part and a pagination part, which translates the logical address into a linear address, and the paging mechanism translates the linear address into a physical address.
The read or write on the RAM chip must be executed serially, so a memory arbiter hardware circuit is inserted between the bus and each RAM chip.
In the whole system the Global descriptor (GDT) only one, can be stored in memory anywhere, but the CPU must know the entrance of the GDT, the GDT not only stored the segment descriptor, and other descriptors, are 64bit long, it is globally visible, for any task is the case.
The first item of the GDT is always set to 0, which ensures that the logical address of the empty segment selector is considered invalid and therefore causes a processor exception.?????
Segments can assign different linear address spaces to each process, while paging can map the same linear address space to different physical address spaces.
All processes running in the user state use a pair of identical fields to address instructions and data, and these two segments are called user code snippets and user data segments.
All segments begin with 0x00000000, and one important conclusion is that the Linux logical address is consistent with the linear address, where the offset quantum segment of the logical address is always the same as the value of the corresponding linear address.
When a pointer to an instruction or data structure is saved, the kernel does not need to set the segment selector for its logical address at all, because the CS register contains the current segment selector.
Each processor has a task status segment (TSS), and the corresponding linear address space is a small subset of the corresponding linear address space for the kernel data segment.
Only a few of the GDT may depend on the process the CPU is executing (the LDT and TLS segment descriptors).
A key task of the paging unit is that the requested access type is compared to the access rights of the linear address, and if this memory access is not valid, a page fault is generated.
Linear addresses are divided into groups of fixed-length units, called pages. The continuous linear address within the page is mapped to a contiguous physical address.
The paging unit divides all the ram into a fixed-length page box.
A data structure that maps a linear address to a physical address is called a page table. The page table is stored in main memory, and the page table must be initialized appropriately by the kernel before starting the paging unit.
Secondary mode reduces memory usage by requesting page tables only for those virtual memory regions that the process actually uses.
Extended paging is used to convert large segments of contiguous linear addresses into corresponding physical addresses, in which case the kernel can save memory and retain TLB entries without using an intermediate page table for address translation.
Unlike the 3 access rights of a segment (read and write execution), there are only two types of access to the page (read and write).
The kernel cannot directly address more than 1GB of RAM due to the need for a user-process linear addressing space.
Only the kernel can modify the process's page table, so processes running under the user state cannot use physical addresses.
The number of levels used depends on the CPU type.
The hardware cache is based on the well-known local principle, which applies to both the program structure and the data structure.
The cache unit is inserted between the paging unit and the main memory. It contains a hardware cache memory and a cache controller. Cache memory holds real rows in memory, and the cache controller holds an array of items.
The operating speed of the system is generally limited by the CPU getting instructions and data rate from memory.
When a cache is hit, the cache controller operates differently depending on the type of access.
Cache Access: 1 writeback operation (write back) 2 write through. Write-back only updates the cache lines, without altering the contents of the RAM, providing a quicker effect. The high-speed controller writes the cache line back to the channel RAM only if the CPU executes an instruction that requires the cache table entry to be flushed, or when a flush hardware signal is generated (usually after a cache miss).
Each processor in a multiprocessor system has a separate hardware cache, so additional hardware circuitry is required to keep the cache content synchronized.
The cache for TLB (translation lookaside buffer) is used to speed up the conversion of linear addresses. When a linear address is used for the first time, the corresponding physical address is calculated from a page table in a slow access RAM. At the same time, the physical address is stored in a TLB table entry so that a reference to the same linear address can be quickly converted later.
In multiprocessor systems, each CPU has its own TLB.
The process of processing Linux relies heavily on paging.
During the initialization phase, the kernel must establish a physical address mapping to specify which physical address ranges are available to the kernel and which are not (either because they map the shared memory of hardware device I/O or because the corresponding page box contains BIOS data).
The value of macro Page_offset is the range size that distinguishes between user space and kernel space, and is also the offset of the process in the linear address. Page_shift refers to the page size. ****
The kernel maintains a set of page tables that are used by itself, resides in the so-called Master Kernel Global Directory, and the set of page tables has never been used directly by any kernel thread in any process shipment after the system has been initialized.
After the kernel has just been loaded into memory, the CPU is still running in real mode, so the paging feature is not enabled.
The global catalog for the temporary page is placed in the Swapper_pg_dir variable.
The final mapping provided by the kernel page table must convert the linear address starting from page_offset to a physical address starting at 0.
The main kernel Global directory is still stored in the Swapper_pg_dir variable, initialized by the Paging_init () function.
Kernel linear address the initial part of the fourth GB of the system's physical memory at least 128MB of linear address is always reserved for him, because the kernel uses these linear addresses to implement non-contiguous memory allocations and fixed mappings for linear addresses.
Indirectly drinking a pointer variable is more than a memory access to an immediate constant address that is indirectly referenced.
Some parts of RAM are permanently allocated to the kernel and are used to store kernel code and static kernel data structures. The rest of RAM is called dynamic memory, which is not only a valuable resource for the process, but also a valuable resource for the kernel itself.
The kernel must record the current state of each page box.
In the case of the page box is not idle, contains the user state process data, a software cache data, dynamically allocated kernel data structure, device driver buffer data, kernel module code and so on.
The state information for the page box is saved in a page descriptor of type page, and all page descriptors are stored in the Mem_map array.
In a NUMA model, the access time of a given CPU to different memory units may vary, and the system's physical memory is divided into several nodes. Within a single node, the time required for any given CPU to access the page is the same. Each node has a descriptor of type gd_data_t.
Each page descriptor has a link to the memory node and to the node management area (including the corresponding box).
When the kernel calls a memory allocation function, it must indicate the admin area where the request page box resides.
Atomic requests (gfp_atomic) are never blocked, and if there is not enough space, they are simply allocation failures.
The amount of reserved memory in kilobytes (KB) is stored in the min_free_kbytes variable. Its initial value is set when the kernel is initialized.
The parameter gfp_mask is a set of flags that indicate how to find a free page box.
At the end of the physical memory directly mapped, the linear address corresponding to the beginning of the high-end memory is stored in the high_memory variable.
The page allocator that returns the linear address of the assigned page box does not apply to high-end memory, that is, the page box in the Zpone_highmem memory management area.
High-end memory page box allocations can only be alloc_page () through the Alloc_pages () function and its shortcut function. These functions do not return the linear address of the first assigned page box, because if the page box belongs to high-end memory, then such a linear address does not exist at all. Instead, these functions return a linear address of the page descriptor of the first assigned page box.
Once all page descriptors are allocated in low-end memory, they do not change during the kernel initialization phase.
The kernel can use three different mechanisms to map page frames to high-end memory, called permanent kernel mappings, temporary kernel mappings, and discontinuous memory allocations.
Establishing a permanent kernel mapping may block the current process, which occurs when the Free page table entry does not exist. Therefore, permanent kernel mappings cannot be used for interrupt handlers and for deferred functions.
Creating a temporary kernel map will never require blocking the current process, but the disadvantage is that only a few temporary kernel mappings can be established at the same time.
The permanent kernel mapping uses the I special page table in the Kernel page table, whose address is stored in the pkmap_page_table variable, the number of table entries in the page table is generated by the LAST_PKMAP macro, and the linear address of the table map starts with the pkmap_base.
To record the connection between the memory page frame and the linear address that the permanent kernel map contains, the kernel uses the Page_address_htable hash list.
The page_address () function returns the linear address of the page box, or null if the page box is in high-end memory and is not mapped.
The Kmap () function establishes a permanent kernel mapping, and if the page box does belong to high-end memory, call Kmap_high ();
Any page box in high-end memory can be mapped to the kernel address space through a "window". The number of Windows left for temporary kernel mappings is very small.
The kernel must ensure that the same window is never used simultaneously by two different control paths. Therefore, the symbol in the KM_TYPE structure can only be used by a kernel component and named by that component. The last symbol, KM_TYPE_NR, does not itself represent a linear address, but is used by each CPU to produce a different number of available windows.
In order to establish a temporary kernel mapping, the kernel calls the Kmap_atomic () function.
Linux uses the well-known partner system algorithm to solve the problem of external fragmentation, all the free page boxes are grouped into 11 linked lists, each of which contains the size 1,2,4,8,16,32,64,128,256,512,1024 a contiguous page box. The maximum request for 1024 page boxes corresponds to a 4MB size contiguous block of RAM.
The LRU chain list is collectively known as: broken down into the active chain list, inactive linked list, the list is the process user State address space or page cache all pages. The former is a page that has been recently visited, which is a page that has not been visited for some time.
The primary data structure used by each partner system: The Mem_map array, the Free_area array (the element k element of the Free_list array identifies all free blocks of size 2^k, which contains the page descriptor of the Start Page box for each free page box block. A pointer to an adjacent element in the linked list is placed in the LRU field of the page descriptor. Finally, the private field of the first page descriptor of a 2^k free page block holds the order of the block, which is the number K.
__rmqueue (); the function is used to find a free block in the admin area. The function requires two parameters, the address and order of the admin area descriptor.
The __rmqueue () function assumes that the caller has disabled the local interrupt and obtained a zone->lock spin lock that protects the partner system data structure.
The __free_pcppages_bulk () function releases the page box according to the policy of the partner system.
To improve system performance, each memory management area defines a per-CPU page box cache. All "per CPU" caches contain pre-allocated page boxes that are used to satisfy a single memory request made by the local CPU.
The main data structure that implements the per-CPU page-box cache is a per_cpu_pageset array of data structures stored in the (zone) memory Management Area Descriptor Pageset field.
The Buffered_rmqueue () function allocates a page box in a specified memory management area, which uses a per-CPU page-box cache to process a single page-box request.
In order to free up a single page box to the per-CPU page-box cache, the kernel uses the Free_hot_page () and Free_cold_page () functions.
The admin area allocator is the front end of the kernel page-box allocator, which must be assigned a memory region that contains enough free page boxes.
The partner system algorithm uses a page box as the base memory area, which is suitable for large chunks of memory.
Kernel functions tend to repeatedly request the same type of memory area.
Slab allocator The objects are grouped into caches, and each cache is a "reserve" of homogeneous linear objects.
The main memory area that contains the cache is divided into multiple slab, each of which consists of more than one contiguous page box that contains both the allocated object and the Idle object.
Each cache is described by a data structure of type Kmem_cache.
The cache is divided into two types, normal and private, and the normal cache is used only by the slab allocator for its own purposes (Kmem_cache), while the dedicated cache is used by the rest of the kernel.
Call Kmem_cache_init () and Kmem_sizes_init () during system initialization to establish a normal cache.
The dedicated cache is created by the kmem_cache_create () function.
To avoid wasting memory space, the kernel must revoke all of its slab before revoking the cache itself. The Kmem_cache_shrink () function revokes all slab in the cache by repeatedly calling Slab_destroy ().
All common and private cache names can be obtained during runtime by reading the/proc/slabinfo file.
When the slab allocator creates a new slab, it relies on the page box allocator to obtain a contiguous set of free page boxes that it calls the Kmem_getpages () function for this purpose.
In the opposite operation, you can release the page box assigned to slab by calling the Kmem_freepages () function.
A newly created cache does not contain any slab, so there are no idle objects.
Only by when the condition is true (1. A request has been made for an allocated object 2. The cache does not contain any idle objects) to assign slab to the cache.
The slab allocator assigns a new slab to the cache by calling the Cache_grow () function, and this function calls Kmem_getpages () obtains a set of page boxes from the partitioned page table allocator to hold a separate slab, and then calls ALLOC_SLABMGMT ( ) to obtain a new slab descriptor.
The slab allocator function uses the LRU field only when the page box is idle, and the page box processed by the PG_SLAB flag is not idle and is set when the partner system is involved.
DMA ignores paged cells and accesses address bus directly, so the requested buffer must be in a contiguous page box.
The frequent modification of the page table will inevitably result in an increase in the average number of access memory as this causes the CPU to frequently flush the contents of the transform backup buffer tlb.
Memory under Linux