程序的加載過程——閱讀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.

 

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