程序的加载过程——阅读CSAPP第九章虚拟内存总结

启动一个程序即开启一个进程,系统先要加载磁盘文件,系统把磁盘的程序文件按图1(a)的组织形式映射到虚拟内存。虚拟内存的组织形式是由内核的一些特殊结构维护的,如图1(b)所示,linux内核为系统的每一个进程维护着一个单独的task结构。task结构包含或指向所有的内核需要运行一个进程的信息(例如,PID,用户栈指针,可执行目标文件的名字,程序计数器(program counter ))。task结构中有一条记录指向描述虚拟内存当前状态的mm_struct。它有两个字段让我们感兴趣,pgd指向page-table, mmap指向一个vm_area_structs 的链表,每个vm_area_structs结构表示当前虚拟地址空间的一块区域。

虚拟地址到物理地址的映射是在page-table表中记录的,每个进程都维护着自己的一个page-table表,如图2所示。如果valid位设置了,那个address字段表示对应的在DRAM 中的物理页的起始地址,也就是virtual page缓存的位置。如果valid没有设置,address为空表明virtual page没有分配,否则address指向virtual page在硬盘上的起始地址。

图1(a) virtual memory orgnization form

 

 

 

图 1(b) How Linux organizes virtual memory.

 

 

 

图2 page talbe 与物理内存和硬盘的映射关系

 

 

 

 

进程的运行是基于自己的虚拟内存模型(图1),所以当处理器开始执行指令的时候需要将虚拟内存的地址翻译成对应的物理地址以便从物理内存中获取指令或数据。MMU (memory management unit) 就是做这件事情的。地址翻译的过程如图3所示,一个虚拟地址分成两部分Virtual Page No 和Virtual Page Offset,MMU用Virtual Page No选择相应的页表记录(PTE)。相应的物理地址是由页表记录中的physical page number (PPN)和虚拟地址中的VPO拼接而成。注意因为物理页与虚拟页的大小是相同的,所以物理页的偏移地址(PPO)与虚拟页的偏移地址(VPO)是相同的。

MMU根据valid值判断page是否命中,即在内存中是否有对应的拷贝。CPU根据命中与否会展现出不同的行为。

Figure 4(a) shows the steps that the CPU hardware performs when there is a page hit.

     Step 1. The processor generates a virtual address and sends it to the MMU.

     Step 2. The MMU generates the PTE address and requests it from the cache/ main memory.

     Step 3. The cache/main memory returns the PTE to the MMU.

     Step4. TheMMU constructs the physical address and sends it to the cache/main memory.

     Step 5. The cache/main memory returns the requested data word to the processor.

Unlike a page hit, which is handled entirely by hardware, handling a page fault requires cooperation between hardware and the operating system kernel (Figure 4(b)).

      Steps 1 to 3. The same as steps 1 to 3 in 图4(a).

      Step 4. The valid bit in the PTE is zero, so the MMU triggers an exception, which transfers control in the CPU to a page fault exception handler in the operating system kernel.

       Step 5. The fault handler identifies a victim page in physical memory, and if that page has been modified, pages it out to disk.If exit a Page Table Entry realtative to the victim page,   mdoify PTE's Valid field form 1 to 0 and address field from phsical memory address to disk adress .

       Step6. The fault handler pages in the new page and updates the PTE in memory,which modify Valid field from 0 to1 and Address field from disk page number to phisical memory page number.

       Step 7. The fault handler returns to the original process, causing the faulting instruction to be restarted. The CPU resends the offending virtual address to the MMU. Because the virtual page is now cached in physical memory, there is a hit, and after the MMU performs the steps in Figure 9.13(a), the main memory returns the requested word to the processor.

 

图3 Address translation with a page table.
图3 Address translation with a page table.

 

 

 

图4 Operational view of page hits and page faults. VA: virtual address. PTEA: page table entry address. PTE: page table entry. PA: physical address.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章