CS 444544 Operating Systems II JOS Memory Management
- Slides: 33
CS 444/544 Operating Systems II JOS Memory Management Yeongjin Jang 04/18/19
Recap: PDE/PTE Permission Examples • Virtual address 0 x 01020304 • PDE: PTE_P | PTE_W • PTE: PTE_P | PTE_U • valid, inaccessible by ring 3, not writable • PTE_P (PRESENT) • 0: invalid entry • 1: valid entry • PTE_W (WRITABLE) • 0: read only • 1: writable • PTE_U (USER) • 0: kernel (only ring 0 can access) • 1: user (accessible by ring 3)
Recap: PDE/PTE Permissions CAVEAT • A virtual address access is allowed if both PDE and PTE entries allows the access… • General practice: put a more permissive permission bits in PDE, and be strict on setting permission bits in PTE • For a conflicting permission setup for Kernel/User, add an additional virtual address mapping can enable such a setup
Recap: You can’t setup the following page permissions… • Kernel: RW, User: R • PTE_P | PTE_W | PTE_U -> User RW… • PTE_P | PTE _W -> User -- • Kernel: R, User: RW • PTE_P | PTE_U | PTE_W -> Kernel RW… • PTE_P | PTE_U -> User R… • Kernel: --, User: RW • PTE_P | PTE_U | PTE_W -> Kernel RW…
Recap: You can setup the following page permissions… • Kernel: RW, User: R • VA 0 x 00001000 -> PA 0 x 50000, PTE_P | PTE_U (User R) • VA 0 xf 0050000 -> PA 0 x 50000, PTE_P | PTE_W (Kernel RW) • Kernel: R, User: RW • VA 0 x 00002000 -> PA 0 x 60000, PTE_P | PTE_U | PTE_W (User RW) • VA 0 xf 0060000 -> PA 0 x 60000, PTE_P (Kernel R) • Kernel: --, User: RW • VA 0 x 00003000 -> PA 0 x 70000, PTE_P | PTE_U | PTE_ W • VA 0 xf 0070000 -> PA 0 x 70000, 0 for flag…
Today’s Topic • Managing Physical Memory in JOS • Tips for Tools
Assigning VA -> PA mapping • Suppose a process would like to use a virtual address • 0 x 800000 (RW from user) • Allocation procedure • Check page directory entry (PDE) • If not set with PTE_P, allocate a physical page for a new page table • Check page table entry (PTE) • If not set with PTE_P, allocate a physical page to enable access • We need to keep track of ‘free’ physical pages…
Struct Page. Info *pages in JOS • A one-to-one mapping from a struct Page. Info to a physical page • An 8 byte struct per each physical memory page • If we support 128 MB memory, then we will use • 128 * 1048576 / 4096 * 8 = 262, 144 = 256 KB for pages (1/512) • A linked-list for managing free physical pages • Starting from page_free_list->pp_link… • pp_ref • Count references • Non-zero – in-use • Zero – free
Physical memory Page N Example Struct Page. Info * pages (array) idx pp_ref N 0 … 0 3 0 2 0 1 0 0 0 pp_link … Page 3 Page 2 Page 1 Page 0
Physical memory (256 MB) Page 65536 Example 256 * 1048576 / 4096 = 65536 Pages 8 byte per each entry = 64 K * 8 = 512 KB Struct Page. Info * pages (array) idx pp_ref 64 K 0 … 0 3 0 2 0 1 0 0 0 pp_link We can put this array into our physical memory … Page 3 Page 2 Page 1 Page 0
Physical memory Free Physical Memory (init) In kern/pmap. c, boot_alloc nextfree end 0 x 100000 nextfree will point to the end of the kernel code/data Kernel Code
Physical memory Free Physical Memory (init) In kern/pmap. c, boot_alloc Free Physical Memory (256 MB – 1 MB) nextfree end 0 x 100000 nextfree will point to the end of the kernel code/data Kernel Code
Physical memory Allocating struct Page. Info Free Physical Memory nextfree idx pp_ref pp_link 6 0 5 0 4 0 3 0 2 0 Physical page 2 1 0 Physical page 1 0 0 Physical page 0 struct Page. Info * pages end Kernel Code 0 x 100000 0 x 2000 0 x 1000 0 x 0000
Physical memory Where are the free pages? Free Physical Memory • in page_init() nextfree struct Page. Info * pages end 0 x 100000 Kernel Code
Physical memory Where are the free pages? Free Physical Memory • in page_init() nextfree struct Page. Info * pages (in use) end EXTPHYSMEM Kernel Code (in use) IOPHYSMEM (in use) Page 0, in use
Physical memory Where are the free pages? • Page 0 is in-use • Pages in [IOPHYSMEM ~ EXTPHYSMEM] are in-use • Pages for the kernel code are in-use nextfree • Pages for struct Page. Info *pages are in-use • How can you point this? • pages + npages ? • boot_alloc(0)? boot_alloc(0) is better… end EXTPHYSMEM Free Physical Memory struct Page. Info * pages (in use) Kernel Code (in use) IOPHYSMEM (in use) Page 0, in use
Reference Counting • A general mechanism for tracking free memory blocks • Mechanism • Count up the value (pp_ref++) if referenced (in use!) • Count down the value (pp_ref--) if not used for one of usage anymore • Free if value gets 0 • In C++, shared_ptr • When a pointer is assigned to a variable, count up! • When the variable no longer uses the variable, count down! • Free the memory when the count become 0
Ref. Counting with struct Page. Info • For in-use memory • Set pp_ref = 1 • For not-in-use memory • assert(pp_ref == 0) • Must be linked with free_pages_list • When assigning the page to a virtual address • pp_ref-- • When releasing the page from a virtual address • pp_ref++
Caveat • Some pages are mapped but does not have to be marked as in-use • Make sure you do not count up pages for dirmap • 0 xf 0000000 ~ 0 xffff • Read the comment at the top of boot_map_region thoroughly
How to build a linked list? • Start with NULL at the head • page_free_list = NULL; • After set pp_ref of all pages, do something like the following. . This will build a linked list…
page 2 pa • Changes a pointer to struct Page. Info to a physical address • idx = (pp – pages) • Gets the index of pp in pages • E. g. , &pages[idx] == pp • idx here is a physical page number pp pages idx pp_ref 4 0 3 0 2 0 1 0 0 0 pp_link pp – pages = 4 0 x 4000 physical page address!
pa 2 page • PGNUM(pa) • Returns page number • &pages[PGNUM(pa)] • Returns struct Page. Info * of that pa. . idx pp_ref 4 0 3 0 2 0 1 0 0 0 pp_link
Tools • vim • : sp • horizontal split • : vs • vertical split • : e. / • Open a file viewer • ctags -R. • Move to the function/struct/macro definition • Ctrl + ], Ctrl + t • /asdf • Search for ‘asdf’ • n: move to the next occurrence of the word
Tools • git add kern/pmap. c kern/init. c • Will add files for commit • git commit • Commit your changes locally • git push • Uploads your commits to the server • git push --tags • Push your tags (e. g. , lab 1 -final)
Tools • tmux • ` + % • Split the window vertically • ` + “ • Split the window horizontally • ` + c • Create a new window • ` + [ • Getting into the scrolling mode (up/down, page-up/page-down) • tmux ls • Show available sessions • tmux new -s asdf • Starts a new session with name ‘asdf’ • tmux attach -t asdf • Attach to a session named ‘asdf’
USE GDB for Debugging • GDB will be your great friend when you face the screen like this…
How GDB can help this? • Run make qemu-nox-gdb • Attach gdb • Continue! (c)
Triple Fault • An irrecoverable fault – your code contains a bug that causes this
GDB will let you know where the bug is
Use GDB command ‘bt’ to see the stacktrace… • Yes, this is something similar to what you implemented for • Exercise 12!
Use GDB command ‘bt’ to see the stacktrace… I did not implemented boot_alloc, so ityou generates an error byfor having • Yes, this is something similar to what implemented • Exercise 12! v = 0 x 0, a NULL pointer!
Use GDB command ‘bt’ to see the stacktrace… I did not implemented boot_alloc, so ityou generates an error byfor having • Yes, this is something similar to what implemented • Exercise 12! v = 0 x 0, a NULL pointer!
Use cprintf a lot if you prefer non-GDB debugging…
- 444544
- Jos operating system
- Address binding
- Module 4 operating systems and file management
- Nimble page management for tiered memory systems
- Virtual memory
- Single partition allocation
- Semantic memory example
- Implicit memory
- Long term memory vs short term memory
- Internal memory and external memory
- Primary memory and secondary memory
- Physical address vs logical address
- Which memory is the actual working memory?
- Virtual memory and cache memory
- Virtual memory in memory hierarchy consists of
- Eidetic memory vs iconic memory
- Shared vs distributed memory
- Example of an os
- Evolution of operating systems
- Components of operating systems
- Introduction of operating system
- Wsn operating systems
- Remzi h arpaci-dusseau
- Operating systems lab
- Open source operating system
- Operating systems structure
- Components of operating system
- Design issues of distributed system
- Early operating systems
- Real-time operating systems
- Can we make operating systems reliable and secure
- Alternative operating systems
- Mit operating systems