There are two kinds of memory management mechanism in modern operating system: segment Management and page-type management.
Segment memory management is to divide the memory into segments, the starting address of each segment is the Janki address. When address mapping, the physical address is obtained by the logical address plus the segment base site. The disadvantage of pure segment memory management is that flexibility and efficiency are poor. First, the length of the segment is variable, which brings a lot of inconvenience to the memory swapping, how to choose the length of a segment is a tricky problem; Secondly, during the process of running, the address space may be expanded, which will increase the segment, thus causing the address space of the process to be composed of many small segments, when the process is running, access to different segments , it is necessary to switch segment base site frequently; a little bit, segment memory management If there are too many small segments, it can cause external fragmentation when the segment is released.
Page memory management, memory is divided into a fixed length of a piece of page. Address mapping, you need to first create a page table, each item in the page table records the base address of the page. Through the page table, the high part of the logical address first finds the page base address of the logical address, and then the page base site offsets a certain length to get the final physical address, and the length of the offset is decided by the low part of the logical address. In general, this process can be completed by the hardware, so the efficiency is relatively high. The advantage of page memory management is that it is more flexible, and memory management makes it easier for memory to swap out and expand the address space in smaller pages.
Strictly speaking, Linux uses Segment-page memory management, which is both segmented and paginated. Address mapping, first determine the corresponding segment, determine the section base site, section of the page, and then find the corresponding page table entries, determine the page base site, and then by the logical address of the page offset determined by the low, you can find the final physical address. But, in fact, Linux is using page-memory management. The reason is that the Janki address in Linux is 0, which is equivalent to all segments. The reason for this is the hardware limitations of some architectures, such as Intel's i386. As a software operating system, it must conform to the hardware system. Although all segments are 0, the concept of segments does exist in the Linux kernel. For example, common kernel code segment, kernel data segment, User State Code section, user state data segment, etc. In addition to meeting the hardware requirements, the paragraph also has practical significance.
Computer Memory Addressing
There are two basic ways in which a computer can manage memory: segment management and page-style management. When using the 80x86 microprocessor, the memory address is divided into three different addresses: the logical address, the linear address, and the physical address. What is the relationship between them, memory is how to address, this article is mainly about memory addressing.
1. Basic Concepts:
CPU Segment Management: The basic principle of segment management is to divide a program into several segments (segment) for storage, each segment being a logical entity (logical entity). A user job or process contains segments corresponding to a two-dimensional linear virtual space, the program is divided into modules (segmentation), so you can write and compile each module of the program. The Segment management program allocates memory in segments, then converts the segment virtual address to virtual address through the address mapping mechanism.
CPU-page Management: The basic principle of page management divides the virtual space of each process into a number of pages of equal length (typically 4K), page management divides the memory space by the page size into pieces or pages (page frame), and then sets the page virtual address and the memory address to create one by one corresponding page table, and the corresponding hardware address transform mechanism is used to solve the problem of discrete address transformation.
Logical address: An address that is contained in a machine language instruction that specifies an operand or an instruction, and each logical address consists of a segment and an offset expressed as [segment identifier: offset within paragraph]. For example, we use pointers to manipulate variable addresses in C + + programs, which is the logical address (which should be exactly the offset within the segment of the logical address). For the above section management, the logical address is the program address before the conversion of segment management.
Linear address: Also known as a virtual address, it is a 32-bit unsigned integer, so it can be used to express up to 4GB of address. Linear addresses are also untrue addresses, as are logical addresses. For the above page type management, the linear address is the address before the page management transformation.
Physical Address: For memory-chip-level memory cell addressing, which corresponds to the address bus of the processor and CPU connections. In general, we say that the memory in the computer's memory line is it (though inaccurate).
With the above basic concepts, obviously, the CPU converts an address in a virtual memory space to a physical address, which takes two steps: First a logical address is given, and the CPU uses its segment memory snap-in to convert each logical address into a thread address and then use its page memory snap-in. Converted to the final physical address. This is what we know about Segment-page management, so the benefits of two conversions can overcome the drawbacks of segment management and page management.
2. CPU Segment Memory Management: logical address conversion to linear address
A logical address consists of two parts: a segment identifier and an offset (in short, offset) of the relative address within a specified segment, [segment identifier: offset in paragraph].
The segment identifier is composed of a 16-bit long field, called a segment selector, where the processor provides a segment register to hold the segment identifier, and the segment register has 6 kinds:
(1) CS code segment registers, points to contain the program instruction paragraph;
(2) SS Stack Register, pointing to the segment containing the current program;
(3) DS data Segment register, pointing to contain static data or global data segment;
(4) Other three registers es, FS, GS called additional segment registers, for general purposes, can point to any data segment.
The offset indicates the distance from the beginning of the segment to the actual address, with an offset of 32 digits.
As shown above, the segment identifier (segment selector) format, where the most critical part is the index number.
The paragraph identifier, by its literal meaning, can be understood to be used to identify a segment, and how the paragraph is expressed.
This extends to another concept: segment descriptors, each of which is represented by a 8-byte segment descriptor that describes the characteristics of the segment (segment descriptor is a segment). The segment identifier can find its corresponding segment (segment descriptor) by index number (13-bit), and the format of the segment is shown in the following illustration:
Paragraph descriptor we need to focus on: Base, which describes the linear address at the beginning of a segment.
The segment descriptor is placed in the global descriptor (GDT), stored in a GDTR register) or a local descriptor (LDT, stored in a LDTR register), typically defines only one GDT, and each process can have its own GDT in addition to the paragraph in LDT if you are learning to create additional segments.
Well, through the above explanation, we can focus on how a logical address is translated into the corresponding linear address, the following steps:
(1) The Ti field of the segment selector of the logical address is first checked to determine which descriptor table the segment description Fu Paocun in. (ti=0 indicates that in GDT, Ti=1 indicates presence in LDT)
(2) The address of the lookup segment descriptor is computed according to the index number of the segment selector, by means of the index number *8 + GDTR or the contents of the LDTR Register = Base.
(3) The corresponding linear address can be obtained by adding the offset of the logical address to the base field value obtained in step (2).
The conversion diagram of the logical address is shown in the following illustration:
3, CPU page memory management: Linear address conversion to physical address
First of all, we need to know some linear address related things.
(1) The linear address is divided into fixed-length units, called pages. A continuous linear address within a page is mapped to a contiguous physical address.
(2) The paging section element divides all physical addresses into fixed-length page frames called physical pages.
(3) The data structure of linear address mapping to physical address is called page table.
(4) 32-bit linear address, divided into 3 domains: directory (directories) high 10 Bits, page table (table) in the middle 10 digits, offset (offset) Low 12 digits, from the offset of 12bit, each page contains 4096 bytes of data.
The conversion of a linear address is accomplished in two steps, each of which is based on a single conversion table, the first of which is called page Catalog table conversion, and the second is called page table conversion. This two-level pattern is used to reduce the amount of RAM required for each Process page table. Just as we read a book catalogue, convenient and quick. The exact transformation is shown in the following illustration:
Conversion steps:
(1) Removing the page directory address of the process from the CR3 (the operating system is responsible for loading the address into the corresponding register at the time of the scheduling process);
(2) According to the top ten of the linear address, in the array, find the corresponding index entry, because the introduction of the level two management mode, the page directory of items, is no longer the address of the page, but the address of a page table. (another array is introduced), and the address of the page is placed in the page table.
(3) Locate the starting address of the page in the page table (also array) according to the middle 10 digits of the linear address;
(4) Add the starting address of the page to the last 12 digits of the linear address, and get the corresponding physical address that we want eventually.
Outside the question:
In memory management, internal fragmentation is a memory space that has been allocated more than the required memory space for the request.
External fragmentation is the memory space of a new process that has not been allocated, but is too small to be allocated to the application space.
There is internal fragmentation in the fixed partition, and there are external fragments in the variable partition allocation;
There is internal fragmentation in the page-type virtual storage System, segment virtual storage system, external fragmentation