CS 444544 Operating Systems II Virtual Memory Layout
- Slides: 31
CS 444/544 Operating Systems II Virtual Memory Layout Yeongjin Jang 1
Lab 1 Due Passed • Late submissions: Tokens will be deducted automatically from your token jar based on the tagged commit/push time in the repository • Please tag your commit accurately • Tag name: lab 1 -final • How to delete local/remote tag: https: //devconnected. com/how-to-deletelocal-and-remote-tags-on-git/ • Extra credits for challenge • Printing 5 colors and getting back to the original color: +1% 2
Recap – Page Table & Addr Translation 31 12 0 0 x 08048000 Offset (12 -bits) 0 x 000 Page number (20 -bits) 0 x 08048 Mem access #1 CR 3[0 x 20] Directory Index (10 -bits) 0 x 20 Physical 0 x 8048000 0 x 10000 0 x 8049000 0 x 11000 0 x 804 a 000 0 x 50000 Table index (10 -bits) 0 x 48 22 31 Virtual 12 Page Directory Entry Phy. Page number (20 -bits) 0 x 10000 Page Table Entry 0 Addr PT . . Addr PT 0 x 48 0 x 10000 0 x 20 Addr PT 0 x 3 ff Addr PT PDE[0 x 48] Mem access 0 x 49 #2 0 x 4 a Mem access #3 (required) 0 x 11000 0 x 50000 3 Offset (12 -bits) 0 x 000
Today’s Topic • Page Permissions • Virtual Memory Layout • How JOS Manages Physical Memory? 4
Page Directory / Table Entry (PDE/PTE) • Top 20 bits: physical page number • Physical page number of a page table (PDE) • Physical page number of the requested virtual address (PTE) • Lower 12 bits: some flags • Permission • Etc. 5
Permission Flags • PTE_P (PRESENT) • 0: invalid entry • 1: valid entry • PTE_W (WRITABLE) Page Table Entry 0 Addr PT 0 x 48 0 x 10000 << 12 | PTE_U | PTE_W Invalid 0 x 49 0 x 11000 << 12 | PTE_P | PTE_W Kernel, writable 0 x 4 a 0 x 50000 << 12 | PTE_P | PTE_U User, read-only • 0: read only • 1: writable • PTE_U (USER) • 0: kernel (only ring 0 can access) • 1: user (accessible by ring 3) 6
When CPU Checks Permission Bits? 31 0 0 x 08048000 • In address translation • 1. Virtual address 12 • 2. PDE = CR 3[PDX] Directory Index (10 -bits) 0 x 20 • Checks permission bits in PTE 22 31 • Checks permission bits in PDE • 3. PTE = PDE[PTX] Offset (12 -bits) 0 x 000 Page number (20 -bits) 0 x 08048 Mem access #1 CR 3[0 x 20] Table index (10 -bits) 0 x 48 PDE[0 x 48] 12 Mem access Page Directory Entry #2 Perm check! Page Table Entry 0 Addr PT | P | U | W 0 0 x 0 | P | U | W 0 x 20 Addr PT | P | U | W 0 x 48 0 x 10000 | P | U | W 0 x 30 Addr PT | P | U | W 0 x 49 0 x 11000 | P | W 0 x 3 ff Addr PT | | U | W 0 x 4 a 0 x 50000 | U | W 7 Perm check!
CPU checks PDE permission first and then PTE permission next… • A virtual memory address is inaccessible if PDE disallows the access • A virtual memory address is inaccessible if PTE disallows the access • Both PDE and PTE should allow the access… 8
PDE/PTE Permission Examples 0 • Virtual address 0 x 01020304 • PTE_P (PRESENT) • 0: invalid entry • 1: valid entry • PDE: PTE_P | PTE_W | PTE_U • PTE_W (WRITABLE) • 0: read only • 1: writable • PTE: PTE_P | PTE_W | PTE_U • Valid, accessible by ring 3, and writable 9 • PTE_U (USER) • 0: kernel (only ring 0 can access) • 1: user (accessible by ring 3)
PDE/PTE Permission Examples 1 • Virtual address 0 x 01020304 • PTE_P (PRESENT) • 0: invalid entry • 1: valid entry • PDE: PTE_P | PTE_W | PTE_U • PTE_W (WRITABLE) • 0: read only • 1: writable • PTE: PTE_P | PTE_U • Valid, accessible by ring 3, but not writable 10 • PTE_U (USER) • 0: kernel (only ring 0 can access) • 1: user (accessible by ring 3)
PDE/PTE Permission Examples 2 • Virtual address 0 x 01020304 • PTE_P (PRESENT) • 0: invalid entry • 1: valid entry • PDE: PTE_P | PTE_U • PTE_W (WRITABLE) • 0: read only • 1: writable • PTE: PTE_P | PTE_W | PTE_U • Valid, accessible by ring 3, but not writable 11 • PTE_U (USER) • 0: kernel (only ring 0 can access) • 1: user (accessible by ring 3)
PDE/PTE Permission Examples 3 • Virtual address 0 x 01020304 • PTE_P (PRESENT) • 0: invalid entry • 1: valid entry • PDE: PTE_P | PTE_W | PTE_U • PTE_W (WRITABLE) • 0: read only • 1: writable • PTE: PTE_P • valid, inaccessible by ring 3, not writable 12 • PTE_U (USER) • 0: kernel (only ring 0 can access) • 1: user (accessible by ring 3)
PDE/PTE Permission Examples 4 • Virtual address 0 x 01020304 • PTE_P (PRESENT) • 0: invalid entry • 1: valid entry • PDE: PTE_P | PTE_W • PTE_W (WRITABLE) • 0: read only • 1: writable • PTE: PTE_P | PTE_U • valid, inaccessible by ring 3, not writable 13 • PTE_U (USER) • 0: kernel (only ring 0 can access) • 1: user (accessible by ring 3)
PDE/PTE Permission Examples 5 • Virtual address 0 x 01020304 • PTE_P (PRESENT) • 0: invalid entry • 1: valid entry • PDE: PTE_P | PTE_U • PTE_W (WRITABLE) • 0: read only • 1: writable • PTE: PTE_U • PTE_U (USER) • 0: kernel (only ring 0 can access) • 1: user (accessible by ring 3) • invalid 14
PDE/PTE Permission Examples 6 • Virtual address 0 x 01020304 • PTE_P (PRESENT) • 0: invalid entry • 1: valid entry • PDE: PTE_U • PTE_W (WRITABLE) • 0: read only • 1: writable • PTE: PTE_P | PTE_U • PTE_U (USER) • 0: kernel (only ring 0 can access) • 1: user (accessible by ring 3) • invalid 15
Can you setup a page permission as… • Kernel: R, User: - • PTE_P • Kernel: R, User: R • PTE_P | PTE_U • Kernel: RW, User: RW • PTE_P | PTE_U | PTE_W 16
You can’t setup a page permission as… • 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… 17
You can enable such a conflicting permission setup by having N-to-1 mapping • Virtual to physical address mapping is in N-to-1 relation • N number of virtual addresses could be mapped to 1 physical address • E. g. , for a physical address 0 x 100000 • JOS maps VA 0 x 100000 to PA 0 x 100000 • JOS maps VA 0 xf 0100000 to PA 0 x 100000 • Why? • EIP before enabling paging: 0 x 100025 • EIP after enabling paging: 0 x 100028 18
Sharing a Physical Page • Example: Loading of the same program Page Table Entry Process 0, runs /bin/bash, loads at virt addr 0 x 35555000 0 … 0 x 155 0 x 10303 | FLAG Physical memory /bin/bash at Phys addr 0 x 10303000 Process 1, runs /bin/bash, loads at virt addr 0 x 43132000 Page Table Entry 0 … 0 x 132 0 x 10303 | FLAG 2 or more mappings to 0 x 10303000 is possible! 19
You can’t setup a page permission as… • 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… 20
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 21
How To Create a Page Directory? • For a 32 -bit Intel processor, we use only 1 page for a Page Directory PDE 0 PDE 1 One page, 4 KB 4096 / 4 = 1024 entries PDE 2 PDE 3 1024 == 210 PDE … 10 -bit index for PD PDE 1022 PDE 1023 Each entry is 4 -byte (32 bits) 22
How To Create a Page Table? • For a 32 -bit Intel processor, we use only 1 page for a Page Table PTE 0 PTE 1 One page, 4 KB 4096 / 4 = 1024 entries PTE 2 PTE 3 1024 == 210 PTE … 10 -bit index for PT PTE 1022 PTE 1023 Each entry is 4 -byte (32 bits) 23
Intel 32 -bit Processor uses a 2 -level page table • Virtual address • Page directory (level 1) • Page table (level 2) • Physical page! 24
What will be the size of full PD/PT? PDE 0 PTE 0 PDE 1 One page, 4 KB PTE 1 PDE 2 PTE 2 PDE 3 PTE 3 PDE … PTE … PDE 1022 PTE 1022 PDE 1023 PTE 1023 Each entry is 4 -byte (32 bits) 4 KB per each Page Table… 4 KB for page directory 25
Size of Page Table • 4 KB for a Page Directory (only one per each process) • 1024 Page Tables available • 4 KB for a Page Table • 4 KB (PD) + 1024 * 4 KB (PT) • 4 KB + 4 MB • ~ 4 MB, 4, 198, 400 bytes… 26
Virtual Memory Layout 0 xffff • OS allocates a separate virtual memory space to each process 0 xc 0000000 OS 0 xc 000000 ~ 0 xffff (1 GB) KERNEL Stack • Transparency Libraries • Do not have to worry about a system’s memory usage status • Isolation heap • Others can’t access my virtual memory space Program code/data 0 x 08048000 27 User (PTE_U)
Virtual Memory Layout 0 xffff • Each process will have almost the same mapping for the kernel but having a different mapping for userspace 0 xc 0000000 Shared kernel mapping OS 0 xc 000000 ~ 0 xffffffff (1 GB) Stack Libraries • Why? • Kernel is shared among processes • Each process could run different apps heap Program code/data 0 x 08048000 28 Program code/data
Example: Linux on 32 -bit Intel 29
Example: Linux on 32 -bit Intel 30
Summary • Page Directory Entry / Page Table Entry • Permission bits (P, W, U) • Permission: {bits in PDE} ⋂ {bits in PTE} • Virtual memory is N-to-1 mapping • Sharing physical page • Allowing conflicting permission assignment • Kernel RW and User R • Virtual Memory Layout • Shares kernel space (typically at the top of virtual memory space) • Can user space arbitrarily (full transparency and isolation) 31
- 444544
- Virtual memory in os
- Virtual memory in operating system
- Virtual memory
- Virtual memory in memory hierarchy consists of
- Stack memory layout
- Alpha axp page table entry
- Prototype in semantics
- Difference between implicit and explicit memory
- Long term memory vs short term memory
- Internal memory and external memory
- Primary memory and secondary memory
- Logical address
- Which memory is the actual working memory?
- Eidetic memory vs iconic memory
- Symmetric shared memory architecture
- Process memory layout
- Memory layout
- Hoon programming language
- Numerator layout
- Language
- Smil head layout root-layout
- Fixed layout website example
- Virtual memory
- Belady's anomaly example
- Advantage
- Explain virtual memory in computer architecture
- Demand paging in virtual memory
- Virtual memory in os
- Virtual memory linux
- Virtual memory os
- Virtual memory