OS: Note of Chapter 6
Memory Management
Virtual Menory
Note: This is not the concept in Windows, which refers to the swap on the disk.
The executable files of a program should be loaded into the memory, however, the memory is limited, and the program will run only a little “part” in a short period. So, it’s better to load a part of the program, rather than the whole program.
But for the program, it should “know” that all the program is in the memory, so the virtual memory was introduced. there’s a bunch of memory addresses allocated to the process space, and the program will think that all the program is in the memory. And the virtual memory will map the virtual memory to the physical memory. It will greatly reduce the memory occupation, which is friendly to cost, but the time consumption increases.
Each time the program is moved from the disk to the memory, it’s called a page. So, it’s necessary to construct a map from the virtual memory to the physical memory. The map is called the page table.
The 2 level page table uses linear address, with the first 10 bits as the index of the first level page table, and the next 10 bits as the index of the second level page table. The last 12 bits are the offset of the page. As for the 3 or more level page table, the linear address are divided into more parts, but the essence is the same. It’s like that finding a word in the dictionary, for example, “memory”, we will find the words start with “M”, then “E”, …, and finally “Y”.
Factly, we have added an empty character implicitly, like “\0” in C++, to block the words with the prefix “memory-“.
The linear translation is like the following:
- Find the CR3, in Linux 0.11, CR3 is alwals 0x0.
- For example, the linear address is 0x6b80, it can be devided into page index: 0x0, page table: 0x6, and the offset: 0xb80.
- See the physical memory address 0x0, the bytes are 0x00001027, the first 20 bits are 0x1, then multiply them by 4K.