Paging mechanism

 Pagination

A way for the operating system to manage memory. He divides the memory into pages, and then maps logical addresses to physical addresses through the page table to access specific physical addresses. Paging better divides and accesses memory, but it also increases system overhead. For example, the page table itself occupies a certain amount of memory and the page table must be accessed first when accessing the actual physical memory. Generally speaking, each process will be allocated a page table. Finally , the design of the page table needs to be cautious.

 

Page table structure

The above is a logical address space generated by a cpu, which needs to be mapped into a specific physical address space through the page table. The page index points to a specific page table entry. The page table is a one-dimensional array stored in memory. Assuming a 32-bit system, the address is also 32-bit. Assuming that the page index only uses 20 bits, how does it point to the page table entry in memory? (You can add all 0s in the upper 12 bits of the address to the lower 20 bits. Of course, it cannot start from memory 0. There will be a base address register plus his address as the actual address. To put it bluntly, it is in this The offset in the page table). Another point to note is that this page table must also be distributed in contiguous memory locations .

Assuming a computer is 32 bits (that is, the cpu can handle 32 bits at a time), the address space range is 0 ~ 2^32. The system uses a paging mechanism, and the size of each page is 4k (2^12), so 2^20 (2^32/2^12) pages can be generated. We only need to use 20bit to locate the position of a page, because the address of the page is a multiple of 2^12, that is, the last 12 bits must be all 0s. The page offset 12 can be positioned to a specific position within a page.

Let's simply calculate the page table size that a 32-bit system needs to generate for the entire memory. First, the size of a page table entry is 4B (even if 32 bits), and the total page table size is 4B*2^20, which is 4MB. RAM. This requires 4MB for each process to store the page table, which is obviously a waste. Next, we will introduce the secondary page table to see if this problem can be solved.

 

Secondary page table

We still assume that the system is 32-bit, and each page is 4k. We divide the page into a 10-bit page directory as the offset in the page directory, and the entry in the page directory points to the first address of the page table. The 10-bit page table is used as the offset of the page table (page table in memory). Finally, the final physical address is obtained by adding the 20 bits in the entry of the page table and the offset of the logical address. There is also a CR3 register at the far left of the above figure, which points to the first address of the page directory (a page table in memory). It is also worth noting that the entries of the page directory and page table in the memory are both 4B (32bit) . This has been bothering me, why not 10bit? Later I thought about it and found that the page table is in memory, and the entry in the page directory must point to the address of the page table in 32 bits to be able to determine it. (Here I talk about why I would think it is 10 bits? Because our original purpose of designing the secondary page table is to reduce the storage space of the page table in memory, and the high 10 bits of many entries in the page table may be the same Yes, and then we can take out the high 10 bits and store them in another table, where the entries point to the table with the low 10 bits (there are many such tables). In this way, the method of proposing the public part can reduce the use of memory Now. But this is not the case.)

At this point we will find that the memory of the page table has not decreased, but has increased (because the page table entry in the memory is 4B). The added part is the space occupied by the page directory. This is another confusion that I have created, so I read the article and found out. Although the page directory here increases the memory, it disperses the location of the secondary page table in the memory, that is, the secondary page table can be distributed in the memory. It is not necessary to have one when there is only one page table as mentioned earlier. Contiguous 4MB memory. There is also an existence bit in the page directory entry. When this bit is 0, it means that the corresponding secondary page table does not exist in the memory, and an exception may occur if it is accessed. There is also a bit indicating whether the corresponding secondary page table is on the disk or in the memory. If it is on the disk, the operating system will change it from the disk to the memory if it is accessed. Of course, there are many bits that respectively represent executable, read-only and read-write. . . What makes me wonder here is that 32 bits here can not only represent the address of the secondary page table in memory, but also can use several of them to represent other information. At this point, we found that although the page directory must exist in the memory, the secondary page tables do not need to be all in the memory, and are scattered in various locations in the memory. This fulfills the need to save memory for the purpose we started.

 

To summarize : here from one page table to two pages, the middle page tables in the memory can be distributed, and the memory space occupied by the page tables is also saved.

 

 

Guess you like

Origin blog.csdn.net/weixin_41237676/article/details/100974132