Recently read the source code of Linux, want to see things written out, think the memory of this part of the simplest, first written out. Please correct me!
The minimum memory 4K address is a page directory (Page_dir) with a total of 1024 entries, each 4 bytes. The directory entries are structured as follows:
____________________________________
|32-12 bit for page box address | |u|r|p| |
| |s|w| |
|_______ __________|______ |_|_ |_|
The 16K is then used to make 4 page tables, with the same page table entry structure as the page catalog item structure. Each item in the page table points to a physical page, which points to a 4K size space in memory. With these 4 page tables, 16M of memory has been addressable. The following is the code that sets a page directory and four page tables in the HEAD.S program when the system initializes. Only the first 4 entries in the page catalog are valid, pointing to the 4 page tables below it, and the 4 page tables address the minimum 16M of memory.
198 setup_paging:
199 movl 24*5,%ecx/* 5 pages-pg_dir+4 page Tables/
xorl%eax,%eax
20 1 xorl%edi,%edi/* Pg_dir is at 0x000 * * CLD;REP;STOSL 203 movl
$pg 0+7,_pg_dir * s ET present bit/user r/w *
204 MOVL $pg 1+7,_pg_dir+4/*---------""---------/
205 MOVL $pg 2+7,_p G_dir+8/*---------""---------/
206 MOVL $pg 3+7,_pg_dir+12/*---------""---------* *
207 MOVL $pg 3+4092,%edi
movl xfff007,%eax/* 16mb-4096 + 7 (r/w user,p) * *
209 STD
210 1: STOSL/* Fill pages Backwards-more efficient:-)/211 Subl x1000,%eax 212 Jge
1b
Each time a new process is fork, the new process is allocated memory. But the concrete is how to do it, I also want to know, together see. When executing fork, it uses int0x80 to invoke the Sys_fork function, and the Sys_fork code is in System_call.s, very short as follows:
_sys_fork:
209 Call _find_empty_process
210 testl%eax,%eax 211
JS 1f
212 Push%gs
213 PUSHL%esi 214 PUSHL%edi 215 pushl%ebp 216 pushl%eax 217 call
_copy_process
218 Addl,%esp
219 1:ret