virtual memory 2
play

virtual memory 2 1 xv6 memory layout 0x80000000 (KERNBASE) page - PowerPoint PPT Presentation

virtual memory 2 1 xv6 memory layout 0x80000000 (KERNBASE) page tables store this mapping (one kernel, one user) for user memory two virtual address same in every process = PA VA 0x8000000 + kernel-only memory 2 Virtual 4 Gig Device


  1. xv6: extracting top-level page table entry if (top_level_pte & PTE_W) { } cprintf("has base address %x\n", PTE_ADDR(top_level_pte)); } cprintf("is user-accessible (may be overriden in next level)\n"); if (top_level_pte & PTE_U) { } cprintf("is writable (may be overriden in next level)\n"); } cprintf("is present (valid)\n"); if (top_level_pte & PTE_P) { pde_t top_level_pte = top_level_page_table[index_into_pgdir]; int index_into_pgdir = PDX(address); // next level uses PTX(....) // PDX = Page Directory indeX 14 void output_top_level_pte_for( struct proc *p, void *address) { pde_t *top_level_page_table = p − >pgdir; cprintf("top level PT for %x in PID %d\n", address, p − >pid);

  2. xv6: extracting top-level page table entry if (top_level_pte & PTE_W) { } cprintf("has base address %x\n", PTE_ADDR(top_level_pte)); } cprintf("is user-accessible (may be overriden in next level)\n"); if (top_level_pte & PTE_U) { } cprintf("is writable (may be overriden in next level)\n"); } cprintf("is present (valid)\n"); if (top_level_pte & PTE_P) { int index_into_pgdir = PDX(address); // next level uses PTX(....) // PDX = Page Directory indeX 14 void output_top_level_pte_for( struct proc *p, void *address) { pde_t *top_level_page_table = p − >pgdir; pde_t top_level_pte = top_level_page_table[index_into_pgdir]; cprintf("top level PT for %x in PID %d\n", address, p − >pid);

  3. xv6: extracting top-level page table entry if (top_level_pte & PTE_W) { } cprintf("has base address %x\n", PTE_ADDR(top_level_pte)); } cprintf("is user-accessible (may be overriden in next level)\n"); if (top_level_pte & PTE_U) { } cprintf("is writable (may be overriden in next level)\n"); } cprintf("is present (valid)\n"); if (top_level_pte & PTE_P) { pde_t top_level_pte = top_level_page_table[index_into_pgdir]; int index_into_pgdir = PDX(address); // next level uses PTX(....) // PDX = Page Directory indeX 14 void output_top_level_pte_for( struct proc *p, void *address) { pde_t *top_level_page_table = p − >pgdir; cprintf("top level PT for %x in PID %d\n", address, p − >pid);

  4. xv6: extracting top-level page table entry if (top_level_pte & PTE_W) { } cprintf("has base address %x\n", PTE_ADDR(top_level_pte)); } cprintf("is user-accessible (may be overriden in next level)\n"); if (top_level_pte & PTE_U) { } cprintf("is writable (may be overriden in next level)\n"); } cprintf("is present (valid)\n"); if (top_level_pte & PTE_P) { pde_t top_level_pte = top_level_page_table[index_into_pgdir]; int index_into_pgdir = PDX(address); // next level uses PTX(....) // PDX = Page Directory indeX 14 void output_top_level_pte_for( struct proc *p, void *address) { pde_t *top_level_page_table = p − >pgdir; cprintf("top level PT for %x in PID %d\n", address, p − >pid);

  5. xv6: manually setting page table entry // if top-level table // if next-level table ... ... some_page_table[index] = PTE_P | PTE_W | PTE_U | base_physical_address; 15 pde_t *some_page_table; pte_t *some_page_table; /* P = present; W = writable; U = user-mode accessible */

  6. xv6 page table-related functions kalloc / kfree — allocate physical page, return kernel address walkpgdir — get pointer to second-level page table entry …to check it/make it valid/invalid/point somewhere/etc. mappages — set range of page table entries implementation: loop using walkpgdir allockvm — create new set of page tables, set kernel (high) part entries for 0x8000 0000 and up set allocate new fjrst-level table plus several second-level tables allocuvm — allocate new user memory setup user-accessible memory allocate new second-level tables as needed deallocuvm — deallocate user memory 16

  7. xv6 page table-related functions kalloc / kfree — allocate physical page, return kernel address walkpgdir — get pointer to second-level page table entry …to check it/make it valid/invalid/point somewhere/etc. mappages — set range of page table entries implementation: loop using walkpgdir allockvm — create new set of page tables, set kernel (high) part entries for 0x8000 0000 and up set allocate new fjrst-level table plus several second-level tables allocuvm — allocate new user memory setup user-accessible memory allocate new second-level tables as needed deallocuvm — deallocate user memory 17

  8. xv6: fjnding page table entries fjrst-level table (‘page directory’) phys. page# pgtab return value PTX(va) pgdir: pointer to fjrst-level page table (‘page directory’) retrieve (pointer to) page table entry from retrieve location of second-level page table PDX(va) PTE_ADDR(*pde) — return physical page address from page table entry convert page-table physical address to virtual retrieve (pointer to) second-level page table entry from second-level table check if fjrst-level page table entry is valid possibly create new second-level table + update fjrst-level table if it is not second-level PT 18 // Return the address of the PTE in page table pgdir pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); // that corresponds to virtual address va. If alloc!=0, // create any required page table pages. { pde = &pgdir[PDX(va)]; if (*pde & PTE_P){ } return &pgtab[PTX(va)]; } fjrst-level PT static pte_t * walkpgdir(pde_t *pgdir, const void *va, int alloc) pde_t *pde; pgdir → pte_t *pgtab; pde → } else { ... /* create new second-level page table */

  9. xv6: fjnding page table entries fjrst-level table (‘page directory’) phys. page# pgtab return value PTX(va) pgdir: pointer to fjrst-level page table (‘page directory’) retrieve (pointer to) page table entry from retrieve location of second-level page table PDX(va) PTE_ADDR(*pde) — return physical page address from page table entry convert page-table physical address to virtual retrieve (pointer to) second-level page table entry from second-level table check if fjrst-level page table entry is valid possibly create new second-level table + update fjrst-level table if it is not second-level PT 18 // Return the address of the PTE in page table pgdir pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); // that corresponds to virtual address va. If alloc!=0, // create any required page table pages. { pde = &pgdir[PDX(va)]; if (*pde & PTE_P){ } return &pgtab[PTX(va)]; } fjrst-level PT static pte_t * walkpgdir(pde_t *pgdir, const void *va, int alloc) pde_t *pde; pgdir → pte_t *pgtab; pde → } else { ... /* create new second-level page table */

  10. xv6: fjnding page table entries fjrst-level table (‘page directory’) phys. page# pgtab return value PTX(va) pgdir: pointer to fjrst-level page table (‘page directory’) retrieve (pointer to) page table entry from retrieve location of second-level page table PDX(va) PTE_ADDR(*pde) — return physical page address from page table entry convert page-table physical address to virtual retrieve (pointer to) second-level page table entry from second-level table check if fjrst-level page table entry is valid possibly create new second-level table + update fjrst-level table if it is not second-level PT 18 // Return the address of the PTE in page table pgdir pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); // that corresponds to virtual address va. If alloc!=0, // create any required page table pages. { pde = &pgdir[PDX(va)]; if (*pde & PTE_P){ } return &pgtab[PTX(va)]; } fjrst-level PT static pte_t * walkpgdir(pde_t *pgdir, const void *va, int alloc) pde_t *pde; pgdir → pte_t *pgtab; pde → } else { ... /* create new second-level page table */

  11. xv6: fjnding page table entries fjrst-level table (‘page directory’) phys. page# pgtab return value PTX(va) pgdir: pointer to fjrst-level page table (‘page directory’) retrieve (pointer to) page table entry from retrieve location of second-level page table PDX(va) PTE_ADDR(*pde) — return physical page address from page table entry convert page-table physical address to virtual retrieve (pointer to) second-level page table entry from second-level table check if fjrst-level page table entry is valid possibly create new second-level table + update fjrst-level table if it is not second-level PT 19 // Return the address of the PTE in page table pgdir pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); // that corresponds to virtual address va. If alloc!=0, // create any required page table pages. { pde = &pgdir[PDX(va)]; if (*pde & PTE_P){ } return &pgtab[PTX(va)]; } fjrst-level PT static pte_t * walkpgdir(pde_t *pgdir, const void *va, int alloc) pde_t *pde; pgdir → pte_t *pgtab; pde → } else { ... /* create new second-level page table */

  12. xv6: fjnding page table entries fjrst-level table (‘page directory’) phys. page# pgtab return value PTX(va) pgdir: pointer to fjrst-level page table (‘page directory’) retrieve (pointer to) page table entry from retrieve location of second-level page table PDX(va) PTE_ADDR(*pde) — return physical page address from page table entry convert page-table physical address to virtual retrieve (pointer to) second-level page table entry from second-level table check if fjrst-level page table entry is valid possibly create new second-level table + update fjrst-level table if it is not second-level PT 20 // Return the address of the PTE in page table pgdir pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); // that corresponds to virtual address va. If alloc!=0, // create any required page table pages. { pde = &pgdir[PDX(va)]; if (*pde & PTE_P){ } return &pgtab[PTX(va)]; } fjrst-level PT static pte_t * walkpgdir(pde_t *pgdir, const void *va, int alloc) pde_t *pde; pgdir → pte_t *pgtab; pde → } else { ... /* create new second-level page table */

  13. xv6: fjnding page table entries fjrst-level table (‘page directory’) phys. page# pgtab return value PTX(va) pgdir: pointer to fjrst-level page table (‘page directory’) retrieve (pointer to) page table entry from retrieve location of second-level page table PDX(va) PTE_ADDR(*pde) — return physical page address from page table entry convert page-table physical address to virtual retrieve (pointer to) second-level page table entry from second-level table check if fjrst-level page table entry is valid possibly create new second-level table + update fjrst-level table if it is not second-level PT 20 // Return the address of the PTE in page table pgdir pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); // that corresponds to virtual address va. If alloc!=0, // create any required page table pages. { pde = &pgdir[PDX(va)]; if (*pde & PTE_P){ } return &pgtab[PTX(va)]; } fjrst-level PT static pte_t * walkpgdir(pde_t *pgdir, const void *va, int alloc) pde_t *pde; pgdir → pte_t *pgtab; pde → } else { ... /* create new second-level page table */

  14. xv6: fjnding page table entries fjrst-level table (‘page directory’) phys. page# pgtab return value PTX(va) pgdir: pointer to fjrst-level page table (‘page directory’) retrieve (pointer to) page table entry from retrieve location of second-level page table PDX(va) PTE_ADDR(*pde) — return physical page address from page table entry convert page-table physical address to virtual retrieve (pointer to) second-level page table entry from second-level table check if fjrst-level page table entry is valid possibly create new second-level table + update fjrst-level table if it is not second-level PT 20 // Return the address of the PTE in page table pgdir pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); // that corresponds to virtual address va. If alloc!=0, // create any required page table pages. { pde = &pgdir[PDX(va)]; if (*pde & PTE_P){ } return &pgtab[PTX(va)]; } fjrst-level PT static pte_t * walkpgdir(pde_t *pgdir, const void *va, int alloc) pde_t *pde; pgdir → pte_t *pgtab; pde → } else { ... /* create new second-level page table */

  15. xv6: fjnding page table entries fjrst-level table (‘page directory’) phys. page# pgtab return value PTX(va) pgdir: pointer to fjrst-level page table (‘page directory’) retrieve (pointer to) page table entry from retrieve location of second-level page table PDX(va) PTE_ADDR(*pde) — return physical page address from page table entry convert page-table physical address to virtual retrieve (pointer to) second-level page table entry from second-level table check if fjrst-level page table entry is valid possibly create new second-level table + update fjrst-level table if it is not second-level PT 20 // Return the address of the PTE in page table pgdir pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); // that corresponds to virtual address va. If alloc!=0, // create any required page table pages. { pde = &pgdir[PDX(va)]; if (*pde & PTE_P){ } return &pgtab[PTX(va)]; } fjrst-level PT static pte_t * walkpgdir(pde_t *pgdir, const void *va, int alloc) pde_t *pde; pgdir → pte_t *pgtab; pde → } else { ... /* create new second-level page table */

  16. xv6: fjnding page table entries fjrst-level table (‘page directory’) phys. page# pgtab return value PTX(va) pgdir: pointer to fjrst-level page table (‘page directory’) retrieve (pointer to) page table entry from retrieve location of second-level page table PDX(va) PTE_ADDR(*pde) — return physical page address from page table entry convert page-table physical address to virtual retrieve (pointer to) second-level page table entry from second-level table check if fjrst-level page table entry is valid possibly create new second-level table + update fjrst-level table if it is not second-level PT 21 // Return the address of the PTE in page table pgdir pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); // that corresponds to virtual address va. If alloc!=0, // create any required page table pages. { pde = &pgdir[PDX(va)]; if (*pde & PTE_P){ } return &pgtab[PTX(va)]; } fjrst-level PT static pte_t * walkpgdir(pde_t *pgdir, const void *va, int alloc) pde_t *pde; pgdir → pte_t *pgtab; pde → } else { ... /* create new second-level page table */

  17. xv6: creating second-level page tables return NULL if not trying to make new page table U for “user-mode” (in addition to kernel) W for “writable” P for “present” (valid) with physical address of second-level page table create a fjrst-level page entry present = 0 PTE = 0 clear the new second-level page table (and return NULL if that fails) otherwise use kalloc to allocate it } ... *pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U; // entries, if necessary. // be further restricted by the permissions in the page table // The permissions here are overly generous, but they can memset(pgtab, 0, PGSIZE); // Make sure all those PTE_P bits are zero. return 0; if (!alloc || (pgtab = (pte_t*)kalloc()) == 0) pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); if (*pde & PTE_P){ 22 } else {

  18. xv6: creating second-level page tables return NULL if not trying to make new page table U for “user-mode” (in addition to kernel) W for “writable” P for “present” (valid) with physical address of second-level page table create a fjrst-level page entry present = 0 PTE = 0 clear the new second-level page table (and return NULL if that fails) otherwise use kalloc to allocate it } ... *pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U; // entries, if necessary. // be further restricted by the permissions in the page table // The permissions here are overly generous, but they can memset(pgtab, 0, PGSIZE); // Make sure all those PTE_P bits are zero. return 0; if (!alloc || (pgtab = (pte_t*)kalloc()) == 0) pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); if (*pde & PTE_P){ 22 } else {

  19. xv6: creating second-level page tables } U for “user-mode” (in addition to kernel) W for “writable” P for “present” (valid) with physical address of second-level page table create a fjrst-level page entry clear the new second-level page table (and return NULL if that fails) otherwise use kalloc to allocate it return NULL if not trying to make new page table *pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U; ... // entries, if necessary. // be further restricted by the permissions in the page table // The permissions here are overly generous, but they can memset(pgtab, 0, PGSIZE); // Make sure all those PTE_P bits are zero. return 0; if (!alloc || (pgtab = (pte_t*)kalloc()) == 0) pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); if (*pde & PTE_P){ 23 PTE = 0 → present = 0 } else {

  20. xv6: creating second-level page tables return NULL if not trying to make new page table U for “user-mode” (in addition to kernel) W for “writable” P for “present” (valid) with physical address of second-level page table create a fjrst-level page entry present = 0 PTE = 0 clear the new second-level page table (and return NULL if that fails) otherwise use kalloc to allocate it } ... // entries, if necessary. // be further restricted by the permissions in the page table // The permissions here are overly generous, but they can memset(pgtab, 0, PGSIZE); // Make sure all those PTE_P bits are zero. return 0; if (!alloc || (pgtab = (pte_t*)kalloc()) == 0) pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); if (*pde & PTE_P){ 23 } else { *pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U;

  21. xv6: creating second-level page tables ... U for “user-mode” (in addition to kernel) W for “writable” P for “present” (valid) create a fjrst-level page entry present = 0 PTE = 0 clear the new second-level page table (and return NULL if that fails) otherwise use kalloc to allocate it return NULL if not trying to make new page table } // entries, if necessary. // be further restricted by the permissions in the page table // The permissions here are overly generous, but they can memset(pgtab, 0, PGSIZE); // Make sure all those PTE_P bits are zero. return 0; if (!alloc || (pgtab = (pte_t*)kalloc()) == 0) pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); if (*pde & PTE_P){ 23 with physical address of second-level page table } else { *pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U;

  22. xv6: creating second-level page tables return NULL if not trying to make new page table U for “user-mode” (in addition to kernel) W for “writable” P for “present” (valid) with physical address of second-level page table create a fjrst-level page entry present = 0 PTE = 0 clear the new second-level page table (and return NULL if that fails) otherwise use kalloc to allocate it } ... // entries, if necessary. // be further restricted by the permissions in the page table // The permissions here are overly generous, but they can memset(pgtab, 0, PGSIZE); // Make sure all those PTE_P bits are zero. return 0; if (!alloc || (pgtab = (pte_t*)kalloc()) == 0) pgtab = (pte_t*)P2V(PTE_ADDR(*pde)); if (*pde & PTE_P){ 23 } else { *pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U;

  23. aside: permissions xv6: sets fjrst-level page table entries with all permissons …but second-level entries can override 24

  24. xv6 page table-related functions kalloc / kfree — allocate physical page, return kernel address walkpgdir — get pointer to second-level page table entry …to check it/make it valid/invalid/point somewhere/etc. mappages — set range of page table entries implementation: loop using walkpgdir allockvm — create new set of page tables, set kernel (high) part entries for 0x8000 0000 and up set allocate new fjrst-level table plus several second-level tables allocuvm — allocate new user memory setup user-accessible memory allocate new second-level tables as needed deallocuvm — deallocate user memory 25

  25. xv6: setting last-level page entries return 0; and next virtual page ( va ) advance to next physical page ( pa ) and P for present with specifjed permission bits (write and/or user-mode) pointing to physical page at pa set page table entry to valid value in upcoming homework: this is not true in stock xv6: never change valid page table entry make sure it’s not already set (or fail if out of memory) get its page table entry for each virtual page in range: loop for a = va to va + size and pa = pa to pa + size } } static int pa += PGSIZE; { a = ( char *)PGROUNDDOWN((uint)va); for (;;){ if ((pte = walkpgdir(pgdir, a, 1)) == 0) 26 if (*pte & PTE_P) panic("remap"); *pte = pa | perm | PTE_P; if (a == last) break ; a += PGSIZE; mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm) char *a, *last; pte_t *pte; last = ( char *)PGROUNDDOWN(((uint)va) + size − 1); return − 1;

  26. xv6: setting last-level page entries return 0; and next virtual page ( va ) advance to next physical page ( pa ) and P for present with specifjed permission bits (write and/or user-mode) pointing to physical page at pa set page table entry to valid value in upcoming homework: this is not true in stock xv6: never change valid page table entry make sure it’s not already set (or fail if out of memory) get its page table entry for each virtual page in range: loop for a = va to va + size and pa = pa to pa + size } } static int pa += PGSIZE; { a = ( char *)PGROUNDDOWN((uint)va); for (;;){ if ((pte = walkpgdir(pgdir, a, 1)) == 0) 26 if (*pte & PTE_P) panic("remap"); *pte = pa | perm | PTE_P; if (a == last) break ; a += PGSIZE; mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm) char *a, *last; pte_t *pte; last = ( char *)PGROUNDDOWN(((uint)va) + size − 1); return − 1;

  27. xv6: setting last-level page entries return 0; and next virtual page ( va ) advance to next physical page ( pa ) and P for present with specifjed permission bits (write and/or user-mode) pointing to physical page at pa set page table entry to valid value in upcoming homework: this is not true in stock xv6: never change valid page table entry make sure it’s not already set (or fail if out of memory) get its page table entry for each virtual page in range: loop for a = va to va + size and pa = pa to pa + size } } static int pa += PGSIZE; { a = ( char *)PGROUNDDOWN((uint)va); for (;;){ if ((pte = walkpgdir(pgdir, a, 1)) == 0) 26 if (*pte & PTE_P) panic("remap"); *pte = pa | perm | PTE_P; if (a == last) break ; a += PGSIZE; mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm) char *a, *last; pte_t *pte; last = ( char *)PGROUNDDOWN(((uint)va) + size − 1); return − 1;

  28. xv6: setting last-level page entries return 0; and next virtual page ( va ) advance to next physical page ( pa ) and P for present with specifjed permission bits (write and/or user-mode) pointing to physical page at pa set page table entry to valid value in upcoming homework: this is not true in stock xv6: never change valid page table entry make sure it’s not already set (or fail if out of memory) get its page table entry for each virtual page in range: loop for a = va to va + size and pa = pa to pa + size } } static int pa += PGSIZE; { a = ( char *)PGROUNDDOWN((uint)va); for (;;){ if ((pte = walkpgdir(pgdir, a, 1)) == 0) 26 if (*pte & PTE_P) panic("remap"); *pte = pa | perm | PTE_P; if (a == last) break ; a += PGSIZE; mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm) char *a, *last; pte_t *pte; last = ( char *)PGROUNDDOWN(((uint)va) + size − 1); return − 1;

  29. xv6: setting last-level page entries return 0; and next virtual page ( va ) advance to next physical page ( pa ) and P for present with specifjed permission bits (write and/or user-mode) pointing to physical page at pa set page table entry to valid value in upcoming homework: this is not true in stock xv6: never change valid page table entry make sure it’s not already set (or fail if out of memory) get its page table entry for each virtual page in range: loop for a = va to va + size and pa = pa to pa + size } } static int pa += PGSIZE; { a = ( char *)PGROUNDDOWN((uint)va); for (;;){ if ((pte = walkpgdir(pgdir, a, 1)) == 0) 26 if (*pte & PTE_P) panic("remap"); *pte = pa | perm | PTE_P; if (a == last) break ; a += PGSIZE; mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm) char *a, *last; pte_t *pte; last = ( char *)PGROUNDDOWN(((uint)va) + size − 1); return − 1;

  30. xv6 page table-related functions kalloc / kfree — allocate physical page, return kernel address walkpgdir — get pointer to second-level page table entry …to check it/make it valid/invalid/point somewhere/etc. mappages — set range of page table entries implementation: loop using walkpgdir allockvm — create new set of page tables, set kernel (high) part entries for 0x8000 0000 and up set allocate new fjrst-level table plus several second-level tables allocuvm — allocate new user memory setup user-accessible memory allocate new second-level tables as needed deallocuvm — deallocate user memory 27

  31. xv6: setting process page tables ( exec() ) exec step 1: create new page table with kernel mappings setupkvm() (recall: kernel mappings — high addresses) exec step 2a: allocate memory for executable pages allocuvm() in loop new physical pages chosen by kalloc() exec step 2b: load executable pages from executable fjle loaduvm() in a loop copy from disk into newly allocated pages (in loaduvm() ) exec step 3: allocate pages for heap, stack ( allocuvm() calls) 28

  32. xv6: setting process page tables ( exec() ) exec step 1: create new page table with kernel mappings setupkvm() (recall: kernel mappings — high addresses) exec step 2a: allocate memory for executable pages allocuvm() in loop new physical pages chosen by kalloc() exec step 2b: load executable pages from executable fjle loaduvm() in a loop copy from disk into newly allocated pages (in loaduvm() ) exec step 3: allocate pages for heap, stack ( allocuvm() calls) 29

  33. create new page table (setupkvm()) use kalloc() to allocate fjrst-level table call mappages() (several times) for kernel mappings (hard-coded lists of calls to make to mappages()) 30

  34. xv6: setting process page tables ( exec() ) exec step 1: create new page table with kernel mappings setupkvm() (recall: kernel mappings — high addresses) exec step 2a: allocate memory for executable pages allocuvm() in loop new physical pages chosen by kalloc() exec step 2b: load executable pages from executable fjle loaduvm() in a loop copy from disk into newly allocated pages (in loaduvm() ) exec step 3: allocate pages for heap, stack ( allocuvm() calls) 31

  35. reading executables (headers) uint paddr; }; uint align; uint flags; uint memsz; uint filesz; xv6 executables contain list of sections to load, represented by: 32 uint vaddr; uint off; uint type; struct proghdr { /* <-- debugging-only or not? */ /* <-- location in file */ /* <-- location in memory */ /* <-- confusing ignored field */ /* <-- amount to load */ /* <-- amount to allocate */ /* <-- readable/writeable (ignored) */

  36. reading executables (headers) xv6 executables contain list of sections to load, represented by: goto bad; if (loaduvm(pgdir, ( char *)ph.vaddr, ip, ph.off, ph.filesz) < 0) ... goto bad; if ((sz = allocuvm(pgdir, sz, ph.vaddr + ph.memsz)) == 0) ... }; uint align; uint flags; uint memsz; uint filesz; uint paddr; uint vaddr; uint off; uint type; struct proghdr { 32 /* <-- debugging-only or not? */ /* <-- location in file */ /* <-- location in memory */ /* <-- confusing ignored field */ /* <-- amount to load */ /* <-- amount to allocate */ /* <-- readable/writeable (ignored) */

  37. reading executables (headers) xv6 executables contain list of sections to load, represented by: name of the fjeld in struct proc sz — top of heap of new program goto bad; if (loaduvm(pgdir, ( char *)ph.vaddr, ip, ph.off, ph.filesz) < 0) ... goto bad; if ((sz = allocuvm(pgdir, sz, ph.vaddr + ph.memsz)) == 0) ... }; uint align; uint flags; 32 uint memsz; uint filesz; uint paddr; uint vaddr; uint off; uint type; struct proghdr { /* <-- debugging-only or not? */ /* <-- location in file */ /* <-- location in memory */ /* <-- confusing ignored field */ /* <-- amount to load */ /* <-- amount to allocate */ /* <-- readable/writeable (ignored) */

  38. allocating user pages if (mappages(pgdir, ( char *)a, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){ plus expanding heap on request this function used for initial allocation add page to second-level page table allocate a new, zero page } } return 0; kfree(mem); deallocuvm(pgdir, newsz, oldsz); cprintf("allocuvm out of memory (2)\n"); memset(mem, 0, PGSIZE); } return 0; deallocuvm(pgdir, newsz, oldsz); cprintf("allocuvm out of memory\n"); if (mem == 0){ mem = kalloc(); for (; a < newsz; a += PGSIZE){ a = PGROUNDUP(oldsz); ... { 33 allocuvm(pde_t *pgdir, uint oldsz, uint newsz)

  39. allocating user pages if (mappages(pgdir, ( char *)a, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){ plus expanding heap on request this function used for initial allocation add page to second-level page table allocate a new, zero page } } return 0; kfree(mem); deallocuvm(pgdir, newsz, oldsz); cprintf("allocuvm out of memory (2)\n"); memset(mem, 0, PGSIZE); } return 0; deallocuvm(pgdir, newsz, oldsz); cprintf("allocuvm out of memory\n"); if (mem == 0){ mem = kalloc(); for (; a < newsz; a += PGSIZE){ a = PGROUNDUP(oldsz); ... { 33 allocuvm(pde_t *pgdir, uint oldsz, uint newsz)

  40. allocating user pages if (mappages(pgdir, ( char *)a, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){ plus expanding heap on request this function used for initial allocation add page to second-level page table allocate a new, zero page } } return 0; kfree(mem); deallocuvm(pgdir, newsz, oldsz); cprintf("allocuvm out of memory (2)\n"); memset(mem, 0, PGSIZE); } return 0; deallocuvm(pgdir, newsz, oldsz); cprintf("allocuvm out of memory\n"); if (mem == 0){ mem = kalloc(); for (; a < newsz; a += PGSIZE){ a = PGROUNDUP(oldsz); ... { 33 allocuvm(pde_t *pgdir, uint oldsz, uint newsz)

  41. allocating user pages if (mappages(pgdir, ( char *)a, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){ plus expanding heap on request this function used for initial allocation add page to second-level page table allocate a new, zero page } } return 0; kfree(mem); deallocuvm(pgdir, newsz, oldsz); cprintf("allocuvm out of memory (2)\n"); memset(mem, 0, PGSIZE); } return 0; deallocuvm(pgdir, newsz, oldsz); cprintf("allocuvm out of memory\n"); if (mem == 0){ mem = kalloc(); for (; a < newsz; a += PGSIZE){ a = PGROUNDUP(oldsz); ... { 33 allocuvm(pde_t *pgdir, uint oldsz, uint newsz)

  42. loaduvm() loaduvm(pgdir, address, file, offset, sz) for each virtual page between address and address + sz: fjnd the physical address of that page ( walkpgdir() ) fjnd the kernel address for that physical address ( P2V() ) copy from disk into that kernel address 34

  43. xv6 page table-related functions kalloc / kfree — allocate physical page, return kernel address walkpgdir — get pointer to second-level page table entry …to check it/make it valid/invalid/point somewhere/etc. mappages — set range of page table entries implementation: loop using walkpgdir allockvm — create new set of page tables, set kernel (high) part entries for 0x8000 0000 and up set allocate new fjrst-level table plus several second-level tables allocuvm — allocate new user memory setup user-accessible memory allocate new second-level tables as needed deallocuvm — deallocate user memory 35

  44. kalloc/kfree kalloc/kfree — xv6’s physical memory allocator keep linked list of free pages list nodes — stored in corresponding free page itself kalloc — return fjrst page in list kfree — add page to list linked list created at boot usuable memory fjxed size (224MB) determined by PHYSTOP in memlayout.h 36 allocates/deallocates whole pages only

  45. xv6 program memory invalid adjusted by sbrk() system call myproc()->sz initial stack pointer 37 KERNBASE argument 0 ... heap argument N 0 nul-terminated string address of argument 0 argv[argc] ... address of argument N argv[0] stack PAGESIZE address of address of argv argument of main guard page argument 0 argc argc argument of main data 0xFFFFFFF return PC for main (empty) text 0

  46. guard page 1 page after stack at lower addresses since stack grows towards lower addresses marked as kernel-mode-only 38 idea: stack overfmow → protection fault → kills program

  47. skipping the guard page void example() { int array[2000]; array[0] = 1000; ... } example: subl $8024, %esp // allocate 8024 bytes on stack movl $1000, 12(%esp) // write near bottom of allocation // goes beyond guard page // since not all of array init'd .... 39

  48. xv6 program memory invalid adjusted by sbrk() system call myproc()->sz initial stack pointer 40 KERNBASE argument 0 ... heap argument N 0 nul-terminated string address of argument 0 argv[argc] ... address of argument N argv[0] stack PAGESIZE address of address of argv argument of main guard page argument 0 argc argc argument of main data 0xFFFFFFF return PC for main (empty) text 0

  49. xv6 program memory invalid myproc()->sz initial stack pointer 40 KERNBASE argument 0 ← adjusted by sbrk() system call ... heap argument N 0 nul-terminated string address of argument 0 argv[argc] ... address of argument N argv[0] stack PAGESIZE address of address of argv argument of main guard page argument 0 argc argc argument of main data 0xFFFFFFF return PC for main (empty) text 0

  50. xv6 heap allocation xv6: every process has a heap at the top of its address space yes, this is unlike Linux where heap is below stack tracked in struct proc with sz = last valid address in process position changed via sbrk(amount) system call sets sz += amount same call exists in Linux, etc. — but also others 41

  51. sbrk sys_sbrk() { if (argint(0, &n) < 0) if (growproc(n) < 0) return addr; } sz : current top of heap sbrk(N) : grow heap by (shrink if negative) returns old top of heap (or -1 on out-of-memory) 42 return − 1; addr = myproc() − >sz; return − 1;

  52. sbrk sys_sbrk() { if (argint(0, &n) < 0) if (growproc(n) < 0) return addr; } sz : current top of heap sbrk(N) : grow heap by (shrink if negative) returns old top of heap (or -1 on out-of-memory) 42 return − 1; addr = myproc() − >sz; return − 1;

  53. sbrk sys_sbrk() { if (argint(0, &n) < 0) if (growproc(n) < 0) return addr; } sz : current top of heap returns old top of heap (or -1 on out-of-memory) 42 sbrk(N) : grow heap by N (shrink if negative) return − 1; addr = myproc() − >sz; return − 1;

  54. sbrk sys_sbrk() { if (argint(0, &n) < 0) if (growproc(n) < 0) return addr; } sz : current top of heap sbrk(N) : grow heap by (shrink if negative) returns old top of heap (or -1 on out-of-memory) 42 return − 1; addr = myproc() − >sz; return − 1;

  55. growproc growproc( int n) calls kalloc to get each page maps pages for addresses sz to sz + n allocuvm — same function used to allocate initial space } return 0; switchuvm(curproc); } 43 uint sz; if (n > 0){ { struct proc *curproc = myproc(); sz = curproc − >sz; if ((sz = allocuvm(curproc − >pgdir, sz, sz + n)) == 0) return − 1; } else if (n < 0){ if ((sz = deallocuvm(curproc − >pgdir, sz, sz + n)) == 0) return − 1; curproc − >sz = sz;

  56. growproc growproc( int n) calls kalloc to get each page maps pages for addresses sz to sz + n allocuvm — same function used to allocate initial space } return 0; switchuvm(curproc); } 43 uint sz; if (n > 0){ { struct proc *curproc = myproc(); sz = curproc − >sz; if ((sz = allocuvm(curproc − >pgdir, sz, sz + n)) == 0) return − 1; } else if (n < 0){ if ((sz = deallocuvm(curproc − >pgdir, sz, sz + n)) == 0) return − 1; curproc − >sz = sz;

  57. /* in some user program: */ /* in trap() in trap.c: */ xv6 page faults (now) accessing page marked invalid (not-present) — triggers page fault xv6 now: default case in trap() function *(( int *) 0x800444) = 1; ... cprintf("pid %d %s: trap %d err %d on cpu %d " "eip 0x%x addr 0x%x--kill proc\n", myproc() >pid, myproc() >name, tf >trapno, tf >err, cpuid(), tf >eip, rcr2()); myproc() >killed = 1; pid 4 processname: trap 14 err 6 on cpu 0 eip 0x1a addr 0x800444--kill proc trap 14 = T_PGFLT special register CR2 contains faulting address 44

  58. xv6 page faults (now) accessing page marked invalid (not-present) — triggers page fault xv6 now: default case in trap() function *(( int *) 0x800444) = 1; ... cprintf("pid %d %s: trap %d err %d on cpu %d " "eip 0x%x addr 0x%x--kill proc\n", pid 4 processname: trap 14 err 6 on cpu 0 eip 0x1a addr 0x800444--kill proc trap 14 = T_PGFLT special register CR2 contains faulting address 44 /* in some user program: */ /* in trap() in trap.c: */ myproc() − >pid, myproc() − >name, tf − >trapno, tf − >err, cpuid(), tf − >eip, rcr2()); myproc() − >killed = 1;

  59. xv6 page faults (now) accessing page marked invalid (not-present) — triggers page fault xv6 now: default case in trap() function *(( int *) 0x800444) = 1; ... cprintf("pid %d %s: trap %d err %d on cpu %d " "eip 0x%x addr 0x%x--kill proc\n", trap 14 = T_PGFLT special register CR2 contains faulting address 44 /* in some user program: */ /* in trap() in trap.c: */ myproc() − >pid, myproc() − >name, tf − >trapno, tf − >err, cpuid(), tf − >eip, rcr2()); myproc() − >killed = 1; pid 4 processname: trap 14 err 6 on cpu 0 eip 0x1a addr 0x800444--kill proc

  60. xv6 page faults (now) accessing page marked invalid (not-present) — triggers page fault xv6 now: default case in trap() function *(( int *) 0x800444) = 1; ... cprintf("pid %d %s: trap %d err %d on cpu %d " "eip 0x%x addr 0x%x--kill proc\n", pid 4 processname: trap 14 err 6 on cpu 0 eip 0x1a addr 0x800444--kill proc trap 14 = T_PGFLT special register CR2 contains faulting address 44 /* in some user program: */ /* in trap() in trap.c: */ myproc() − >pid, myproc() − >name, tf − >trapno, tf − >err, cpuid(), tf − >eip, rcr2()); myproc() − >killed = 1;

  61. void *address = ( void *) rcr2(); xv6: if one handled page faults // actual segfault, kill process that is, immediately after returning from fault if so, setup the page table so it works next time check process control block to see if access okay } } myproc() >killed = 1; cprintf("..."); // return from fault, retry access } else { alternative to crashing: update the page table and return setup_page_table_entry_for(myproc(), address); if (is_address_okay(myproc(), address)) { if (tf >trapno == T_PGFLT) { pseudocode for xv6 implementation (for trap()) example: don’t actually allocate memory until it’s needed “just in time” update of the process’s memory 45 returning from page fault handler normally retries failing instruction

  62. xv6: if one handled page faults } else { that is, immediately after returning from fault if so, setup the page table so it works next time check process control block to see if access okay } } cprintf("..."); // actual segfault, kill process // return from fault, retry access alternative to crashing: update the page table and return setup_page_table_entry_for(myproc(), address); if (is_address_okay(myproc(), address)) { pseudocode for xv6 implementation (for trap()) example: don’t actually allocate memory until it’s needed “just in time” update of the process’s memory 45 returning from page fault handler normally retries failing instruction if (tf − >trapno == T_PGFLT) { void *address = ( void *) rcr2(); myproc() − >killed = 1;

  63. xv6: if one handled page faults } else { that is, immediately after returning from fault if so, setup the page table so it works next time check process control block to see if access okay } } cprintf("..."); // actual segfault, kill process // return from fault, retry access alternative to crashing: update the page table and return setup_page_table_entry_for(myproc(), address); if (is_address_okay(myproc(), address)) { pseudocode for xv6 implementation (for trap()) example: don’t actually allocate memory until it’s needed “just in time” update of the process’s memory 45 returning from page fault handler normally retries failing instruction if (tf − >trapno == T_PGFLT) { void *address = ( void *) rcr2(); myproc() − >killed = 1;

  64. xv6: if one handled page faults } else { that is, immediately after returning from fault if so, setup the page table so it works next time check process control block to see if access okay } } cprintf("..."); // actual segfault, kill process // return from fault, retry access alternative to crashing: update the page table and return setup_page_table_entry_for(myproc(), address); if (is_address_okay(myproc(), address)) { pseudocode for xv6 implementation (for trap()) example: don’t actually allocate memory until it’s needed “just in time” update of the process’s memory 45 returning from page fault handler normally retries failing instruction if (tf − >trapno == T_PGFLT) { void *address = ( void *) rcr2(); myproc() − >killed = 1;

  65. page fault tricks OS can do all sorts of ‘tricks’ with page tables key idea: what processes think they have in memory != their actual memory OS fjxes disagreement from page fault handler 46

  66. space on demand Used by OS Program Memory Stack Heap / other dynamic Writable data Code + Constants used stack space (12 KB) wasted space? (huge??) OS would like to allocate space only if needed 47

  67. space on demand Used by OS Program Memory Stack Heap / other dynamic Writable data Code + Constants used stack space (12 KB) wasted space? (huge??) OS would like to allocate space only if needed 47

  68. space on demand Used by OS Program Memory Stack Heap / other dynamic Writable data Code + Constants used stack space (12 KB) wasted space? (huge??) OS would like to allocate space only if needed 47

  69. allocating space on demand … 0x7FFFE 1 0x12347 0x7FFFF 1 0x12345 … … 1 pushq triggers exception hardware says “accessing address 0x7FFFBFF8 ” OS looks up what’s should be there — “stack” page fault! in exception handler, OS allocates more stack space OS updates the page table then returns to retry the instruction restarted 0x12340 0x7FFFD ... 0x200DF // requires more stack space A: pushq %rbx C: addq %rbx, %rax ... %rsp = 0x7FFFC000 VPN page … … … 0x7FFFB 0 --- 0x7FFFC 1 48 valid? physical B: movq 8(%rcx), %rbx

  70. allocating space on demand … 0x7FFFE 1 0x12347 0x7FFFF 1 0x12345 … … 1 pushq triggers exception hardware says “accessing address 0x7FFFBFF8 ” OS looks up what’s should be there — “stack” page fault! in exception handler, OS allocates more stack space OS updates the page table then returns to retry the instruction restarted 0x12340 0x7FFFD ... 0x200DF // requires more stack space A: pushq %rbx C: addq %rbx, %rax ... %rsp = 0x7FFFC000 VPN page … … … 0x7FFFB 0 --- 0x7FFFC 1 48 valid? physical B: movq 8(%rcx), %rbx

  71. allocating space on demand … 0x7FFFE 1 0x12347 0x7FFFF 1 0x12345 … … 1 pushq triggers exception hardware says “accessing address 0x7FFFBFF8 ” OS looks up what’s should be there — “stack” page fault! in exception handler, OS allocates more stack space OS updates the page table then returns to retry the instruction restarted 0x12340 0x7FFFD ... 0x200DF // requires more stack space A: pushq %rbx C: addq %rbx, %rax ... %rsp = 0x7FFFC000 VPN page … … … 0x7FFFB 1 0x200D8 0x7FFFC 1 48 valid? physical B: movq 8(%rcx), %rbx

  72. space on demand really common for OSes to allocate a lot space on demand sometimes new heap allocations sometimes global variables that are initially zero benefjt: malloc/new and starting processes is faster also, similar strategy used to load programs on demand (more on this later) future assigment: add allocate heap on demand in xv6 49

  73. xv6: adding space on demand struct proc { uint sz; // Size of process memory (bytes) ... }; xv6 tracks “end of heap” (now just for sbrk() ) adding allocate on demand logic for the heap: on sbrk(): don’t change page table right away kill process — out of bounds fjnd virtual page number of address allocate page of memory, add to page table return from interrupt 50 on page fault: if address ≥ sz on page fault: if address < sz

  74. versus more complicated OSes typical desktop/server: range of valid addresses is not just 0 to maximum need some more complicated data structure to represent 51

  75. fast copies recall : fork() (usually, the copy then calls execve — replaces itself with another program) how isn’t this really slow? 52 creates a copy of an entire program!

  76. do we really need a complete copy? Used by OS bash Stack Heap / other dynamic Writable data Code + Constants Used by OS new copy of bash Stack Heap / other dynamic Writable data Code + Constants shared as read-only can’t be shared? 53

  77. do we really need a complete copy? Used by OS bash Stack Heap / other dynamic Writable data Code + Constants Used by OS new copy of bash Stack Heap / other dynamic Writable data Code + Constants shared as read-only can’t be shared? 53

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend