Linux中代碼段和數(shù)據(jù)段的加載
Linux中代碼段和數(shù)據(jù)段的加載
load_aout_binary中關(guān)于數(shù)據(jù)段代碼段的加載片段如下:
/*加載代碼段*/
error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
PROT_READ | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
fd_offset);
/*加載數(shù)據(jù)段*/
error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
fd_offset + ex.a_text);
elf文件的處理比較特殊:
if (elf_ppnt->p_flags & PF_R)
· elf_prot |= PROT_READ;
· if (elf_ppnt->p_flags & PF_W)
· elf_prot |= PROT_WRITE;
· if (elf_ppnt->p_flags & PF_X)
· elf_prot |= PROT_EXEC;
· elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
......
error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
· elf_prot, elf_flags, 0);
這里面關(guān)鍵的問題是,無論代碼段還是數(shù)據(jù)段,一定是MAP_PRIVATE 的映射。
關(guān)于do_mmap的處理:
do_mmap做兩件事情:1)對要映射的虛存區(qū)域建立vma 2)設(shè)置vma的vm_ops ,重點(diǎn)是其中的缺頁處理的函數(shù)filemap_nopage(do_mmap的基本思路就是:只有在程序具體訪問到該頁時(shí)才建立真正的物理page)
在do_mmap_pgoff 中,有如下片段處理vma的讀寫屬性:
if (file) {
VM_ClearReadHint(vma);
vma->vm_raend = 0;
if (file->f_mode & FMODE_READ)
vma->vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
if (flags & MAP_SHARED) {
vma->vm_flags |= VM_SHARED | VM_MAYSHARE;
...........
}
vma->vm_page_prot = protection_map[vma->vm_flags & 0x0f]; /*pte中的頁屬性位就來自該變量*/
vma屬性的定義如下:
* vm_flags..
*/
#define VM_READ 0x00000001 /* currently active flags */
#define VM_WRITE 0x00000002
#define VM_EXEC 0x00000004
#define VM_SHARED 0x00000008
以數(shù)據(jù)段為例:它是r/w、private的,所以它在執(zhí)行到屬性處理語句的時(shí)候:
vma->vm_page_prot = protection_map[vma->vm_flags & 0x0f];數(shù)組里面的索引值要么是3.要么是7.
pgprot_t protection_map[16] = {
__P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
__S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
};
由上述圖可以知道,3對應(yīng)的是__P011, 7對應(yīng)的是__P111
#define __P000 PAGE_NONE
#define __P001 PAGE_READONLY
#define __P010 PAGE_COPY
#define __P011 PAGE_COPY
#define __P100 PAGE_READONLY
#define __P101 PAGE_READONLY
#define __P110 PAGE_COPY
#define __P111 PAGE_COPY
#define PAGE_COPY __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
所以,不管是3還是7,這個(gè)頁的屬性都是PAGE_COPY。 至此代碼段、數(shù)據(jù)段的vma已經(jīng)建立成功,等到程序要訪問相關(guān)頁的時(shí)候,會觸發(fā)do_page_fault對相應(yīng)的虛存頁進(jìn)行物理頁框的分配。
do_page_fault的處理:
do_page_fault -> handle_mm_fault -> handle_pte_fault
if (!pte_present(entry)) {
/*
* If it truly wasn't present, we know that kswapd
* and the PTE updates will not touch it later. So
* drop the lock.
*/
spin_unlock(&mm->page_table_lock);
if (pte_none(entry))
return do_no_page(mm, vma, address, write_access, pte);
return do_swap_page(mm, vma, address, pte, pte_to_swp_entry(entry), write_access);
}
第一次處理數(shù)據(jù)段缺頁的時(shí)候,顯然是pte表項(xiàng)為空的場景。但此時(shí)觸發(fā)page_fault的操作是讀還是寫?(write_access正是標(biāo)志本次觸發(fā)標(biāo)志是讀還是寫的操作)
假設(shè)現(xiàn)在的操作是寫,那么write_access就是1.
do_no_page是這么處理的:
........
new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, (vma->vm_flags & VM_SHARED)?0:write_access);
entry = mk_pte(new_page, vma->vm_page_prot); /*這里是PAGE_COPY*/
if (write_access)
entry = pte_mkwrite(pte_mkdirty(entry));
set_pte(page_table, entry);
vma中的vm_ops是filemap_nopage函數(shù):
filemap_nopage(struct vm_area_struct * area,unsigned long address, int no_share)/* no_share = write_access*/
page = __find_get_page(mapping, pgoff, hash); /*首先在page_cache中找到elf中的data頁*/
............
old_page = page;
if (no_share) {
struct page *new_page = page_cache_alloc();
if (new_page) {
copy_user_highpage(new_page, old_page, address);
flush_page_to_ram(new_page);
} else
new_page = NOPAGE_OOM;
page_cache_release(page);
return new_page;
}
由此可見: 數(shù)據(jù)段的頁只是一份用戶態(tài)本地的拷貝,它沒有任何mapping,他可以被換出到swap中。它和bss、堆區(qū)、棧區(qū)的頁沒有任何本質(zhì)上的區(qū)別。
假設(shè)現(xiàn)在的操作是讀:
write_access就是0,所以filemap_nopage中返回的就是old_page。 old_page是page_cache中的頁,他一定會mapping到elf_inode->address_mapping。似乎這樣接下去推會得到一個(gè)相反的結(jié)論。[!--empirenews.page--]
請注意:
entry = mk_pte(new_page, vma->vm_page_prot); /*這里是PAGE_COPY,_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED */
當(dāng)我們對這樣的頁進(jìn)行寫操作的時(shí)候,mmu當(dāng)然會觸發(fā)page_fault.
if (write_access) {
if (!pte_write(entry))
return do_wp_page(mm, vma, address, pte, entry);
entry = pte_mkdirty(entry);
}
當(dāng)一個(gè)vma是可寫的,而vma中對應(yīng)的pte又是不可寫的,這就觸發(fā)了copy_on_write機(jī)制:do_wp_page會進(jìn)行相應(yīng)的copy_on_write處理過程。
所以,不存在是否第一份不能被換出的情況。如果對數(shù)據(jù)段都是只讀的操作,那么你可以把數(shù)據(jù)段的頁想象成一個(gè)軟鏈接。一旦有了寫操作,那么數(shù)據(jù)段就變成了一份本地的page。