CS 444544 Operating Systems II Virtual Memory Yeongjin

  • Slides: 55
Download presentation
CS 444/544 Operating Systems II Virtual Memory Yeongjin Jang 04/09/19

CS 444/544 Operating Systems II Virtual Memory Yeongjin Jang 04/09/19

Recap – Real Mode • Real mode segmentation, how? • seg * 16 +

Recap – Real Mode • Real mode segmentation, how? • seg * 16 + offset • [b 000: b 7 ff] => 0 xb 000 * 16 + 0 xb 7 ff = 0 xbb 7 ff • What is A 20? • [f 800: 8001] => 0 x 100001? • [f 800: 8001] => 0 x 1? • FYI, segment registers are: • • • %cs – code segment %ds – data segment %es – extra segment %fs %gs %ss – stack segment Image from: https: //upload. wikimedia. org/wikipedia/commons/thumb/d/db/Overlapping_realmode_segments. svg

Recap - JOS Boot Sequence • 0 xf 000: 0 xffff – BIOS •

Recap - JOS Boot Sequence • 0 xf 000: 0 xffff – BIOS • Loads boot sector – runs 0 x 7 c 00 • Enable A 20 • Enable protected mode (enabling 4 GB memory access) • Read kernel ELF (Executable Linkable Format) • …

No Memory Privilege in Real Mode • Suppose two program runs at the same

No Memory Privilege in Real Mode • Suppose two program runs at the same time Main Memory Program A • Program A attempts to modify memory used by program B • No SECURITY! Program B

i 386 Protected Mode • Look at GDT (Global Descriptor Table) • Indexed by

i 386 Protected Mode • Look at GDT (Global Descriptor Table) • Indexed by a segment register • (selector) • Retrieve base address • Address = base + offset • Can access if (offset < limit) or • Can access if (offset < limit * 4096) • Depending on the values in flags! Images from: https: //sipb. mit. edu/iap/6. 828/files/x 86_translation_and_registers. pdf

i 386 Protected Mode • Base • Any 32 -bit address • Limit •

i 386 Protected Mode • Base • Any 32 -bit address • Limit • 20 -bit, but could be multiplied by 4096 bytes • E. g. , 1 means 4096, 2 means 8192, etc. Images from: https: //sipb. mit. edu/iap/6. 828/files/x 86_translation_and_registers. pdf https: //wiki. osdev. org/Global_Descriptor_Table

i 386 Protected Mode • G - Granularity (0 = byte, 1 = page)

i 386 Protected Mode • G - Granularity (0 = byte, 1 = page) • 0: Limit will be byte granularity (i. e. , limit, only access 220, 1 MB) • 1: Limit will be page granularity (i. e. , limit * 4096, 220 * 216 = 232) • D – Default operand size (0 = 16 -bit, 1 = 32 -bit) • Set the values of IP/SP with respect to this bit • R, X – Readable/Executable • DPL – Descriptor Privilege Level (a. k. a. Ring Level) • 0 (highest priv), 1, 2, 3 (lowest priv) For more information: https: //en. wikipedia. org/wiki/Protected_mode

Protected Mode - Examples • 0 x 0: 0 x 8080 • Base: 0

Protected Mode - Examples • 0 x 0: 0 x 8080 • Base: 0 x 0 • Limit (addr): 0 xfffff 000 • Offset: 0 x 8080 • Address: 0 x 8080 GDT index 32 -bit Base 20 -bit Limit 12 -bit Flags 16 0 x 31310000 0 x 1 G=1 8 0 x 40000000 0 x 80000 G=1 0 0 xfffff G=1

Protected Mode - Examples • 0 x 8: 0 x 8080 • Base: 0

Protected Mode - Examples • 0 x 8: 0 x 8080 • Base: 0 x 40000000 • Limit (addr): 0 x 8000000 • Offset: 0 x 8080 • Address: 0 x 40008080 GDT index 32 -bit Base 20 -bit Limit 12 -bit Flags 16 0 x 31310000 0 x 1 G=1 8 0 x 40000000 0 x 80000 G=1 0 0 xfffff G=1

Protected Mode - Examples • 0 x 10: 0 x 333 • Base: 0

Protected Mode - Examples • 0 x 10: 0 x 333 • Base: 0 x 31310000 • Limit (addr): 0 x 1000 • Offset: 0 x 333 • Address: 0 x 31310333 • 0 x 10: 0 x 8080 • • • Base: 0 x 31310000 Limit (addr): 0 x 1000 Offset: 0 x 8080 Offset > limit Access denied! GDT index 32 -bit Base 20 -bit Limit 12 -bit Flags 16 0 x 31310000 0 x 1 G=1 8 0 x 40000000 0 x 80000 G=1 0 0 xfffff G=1

Protected Mode – Memory Privilege • DPL (Descriptor Privilege Level) • Protected mode –

Protected Mode – Memory Privilege • DPL (Descriptor Privilege Level) • Protected mode – four levels of memory privilege • 0 – highest, OS kernel • 1 – OS kernel ----------------------- • 2 – highest user-level privilege • 3 – user-level privilege Kernel: for privileged OS operations… User: for unprivileged applications…

Protected Mode – Memory Privilege • No memory privilege in real mode • Protected

Protected Mode – Memory Privilege • No memory privilege in real mode • Protected mode – four levels of memory privilege • 0 – highest, OS kernel • 1 – OS kernel ---------------------- • 2 – highest user-level privilege • 3 – user-level privilege • Typically, 0 is for kernel, 3 is for user… Image from: https: //en. wikipedia. org/wiki/File: Priv_rings. svg

Descriptor Privilege Level Defines Ring Level • CPL = Current Privilege Level • Defined

Descriptor Privilege Level Defines Ring Level • CPL = Current Privilege Level • Defined in the last 2 bits of the %cs register • You can change %cs only via lcall/ljmp/trap/int GDT index 32 -bit Base 20 -bit Limit 12 -bit Flags 16 USER 0 x 31310000 0 x 1 G=1, DPL=3 8 KERNEL 0 x 40000000 0 x 80000 G=1, DPL=0 0 KERNEL 0 x 0 0 xfffff G=1, DPL=0

Descriptor Privilege Level Defines Ring Level • CPL = Current Privilege Level • Defined

Descriptor Privilege Level Defines Ring Level • CPL = Current Privilege Level • Defined in the last 2 bits of the %cs register • You can change %cs only via lcall/ljmp/trap/int • Examples • • %cs == 0 x 8 %cs == 0 x 13 %cs == 0 x 10 %cs == 0 xb == 1000 in binary, last 2 bits are ZERO -> KERNEL! == 10011 in binary, last 2 bits are 3 -> USER! == 10000 in binary, last 2 bits are 0 -> KERNEL! == 1011…. GDT index 32 -bit Base 20 -bit Limit 12 -bit Flags 16 USER 0 x 31310000 0 x 1 G=1, DPL=3 8 KERNEL 0 x 40000000 0 x 80000 G=1, DPL=0 0 KERNEL 0 x 0 0 xfffff G=1, DPL=0

Descriptor Privilege Level Defines Ring Level • CPL = Current Privilege Level • Defined

Descriptor Privilege Level Defines Ring Level • CPL = Current Privilege Level • Defined in the last 2 bits of the %cs register • You can change %cs only via lcall/ljmp/trap/int • mov %ax, %cs impossible! • Can only move down… • CPL==0, then ljmp 0 x 3: 0 x 1234 is possible • CPL==3, then ljmp 0 x 0: 0 x 1234 is impossible GDT index 32 -bit Base 20 -bit Limit 12 -bit Flags 16 USER 0 x 31310000 0 x 1 G=1, DPL=3 8 KERNEL 0 x 40000000 0 x 80000 G=1, DPL=0 0 KERNEL 0 x 0 0 xfffff G=1, DPL=0

x 86 Memory Privileges • CPL = Current Privilege Level • Defined in the

x 86 Memory Privileges • CPL = Current Privilege Level • Defined in the last 2 bits of the %cs register • You can only change %cs via lcall/ljmp/trap/int • RPL = Requester Privilege Level • Ring switch rule • CPL <= DPL • RPL <= DPL • Defined in the last 2 bits of any segment register • Must meet both conditions. . • DPL = Descriptor Privilege Level • Defined in the GDT

Enabling Protected Mode (part 1): Create Global Descriptor Table (GDT) • In boot/boot. S

Enabling Protected Mode (part 1): Create Global Descriptor Table (GDT) • In boot/boot. S • %cs to point 0 ~ 0 xffff in DPL 0 • %ds to point 0 ~ 0 xffff in DPL 0 • Only kernel can access those two segment GDT index 32 -bit Base 20 -bit Limit 12 -bit Flags 16 0 x 0 0 xfffff G=1, W DPL=0 8 0 x 0 0 xfffff G=1, XR DPL=0 0 0

Enabling Protected Mode (part 2): Change CR 0 (Control Register 0) Set PE (Protected

Enabling Protected Mode (part 2): Change CR 0 (Control Register 0) Set PE (Protected enabled) to 1 will enable Protected Mode In JOS: 1. Load GDT 2. Read CR 0, store it to eax 3. Set PE_ON (1) on eax 4. Put eax back to CR 0 (PE_ON to CR 0!!)

How to Change CPL? • ljmp (instruction) • Long jump 0 x 8 ==

How to Change CPL? • ljmp (instruction) • Long jump 0 x 8 == 1000, Last 2 bits are zero. .

Protected Mode Summary • Segment access via GDT • Base + Offset < Limit

Protected Mode Summary • Segment access via GDT • Base + Offset < Limit * 4096 • Last two bits in %cs segment - CPL • Memory Privilege - Ring level • 0 for OS kernel • 3 for user application • Changing CR 0 to enable protected mode • CR 0_PE_ON == 1, set via eax • Changing CPL? • ljmp %cs: xxxxx, set the last 2 bits of %cs as 0 for kernel. .

Virtual Memory • Three goals • Transparency • Efficiency • Protection

Virtual Memory • Three goals • Transparency • Efficiency • Protection

Uniprogramming Environment • Run one program Stack - 1 Program Data - 1 •

Uniprogramming Environment • Run one program Stack - 1 Program Data - 1 • The program can use memory space freely… Program Code - 1 Free (576 KB) 0 x 10000 ~ 0 xa 0000 (64 KB ~ 640 KB) OS 0 x 00000 ~ 0 x 10000 (0 ~ 64 KB)

Uniprogramming Environment • Run one program • The program can use memory space freely…

Uniprogramming Environment • Run one program • The program can use memory space freely… Stack (64 KB) Stack -1 0 x 80000 ~ 0 x 90000 (512 KB ~ 576 KB) Free (576 KB) 0 x 10000 Data ~ 0 xa 0000 Program (64 KB) (64 KB ~~640 KB) Program Data -1 0 x 40000 0 x 50000 (256 KB ~ 320 KB) Program Code - 1 OS 0 x 00000 ~ 0 x 10000 (0 ~ 64 KB)

Uniprogramming Environment • Run one program • The program can use memory space freely…

Uniprogramming Environment • Run one program • The program can use memory space freely… Free (64 KB) 0 x 90000 ~ 0 xa 0000 (576 KB ~ 640 KB) Stack - 1 (64 KB) 0 x 80000 ~ 0 x 90000 (512 KB ~ 576 KB) Free (192 KB) 0 x 50000 ~ 0 x 80000 (320 KB ~ 512 KB) Program Data - 1 (64 KB) 0 x 40000 ~ 0 x 50000 (256 KB ~ 320 KB) Free (64 KB) 0 x 30000 ~ 0 x 40000 (192 KB ~ 256 KB) Program Code - 1 (128 KB) 0 x 10000 ~ 0 x 30000 (64 KB ~ 192 KB) OS 0 x 00000 ~ 0 x 10000 (0 ~ 64 KB)

Multi-programming Environment • Run two programs Stack - 2 (64 KB) Program Data -

Multi-programming Environment • Run two programs Stack - 2 (64 KB) Program Data - 2 (64 KB) Program Code - 2 (128 KB) Free (64 KB) 0 x 90000 ~ 0 xa 0000 (576 KB ~ 640 KB) Stack - 1 (64 KB) 0 x 80000 ~ 0 x 90000 (512 KB ~ 576 KB) Free (192 KB) 0 x 50000 ~ 0 x 80000 (320 KB ~ 512 KB) Program Data - 1 (64 KB) 0 x 40000 ~ 0 x 50000 (256 KB ~ 320 KB) Free (64 KB) 0 x 30000 ~ 0 x 40000 (192 KB ~ 256 KB) Program Code - 1 (128 KB) 0 x 10000 ~ 0 x 30000 (64 KB ~ 192 KB) OS 0 x 00000 ~ 0 x 10000 (0 ~ 64 KB)

Multi-programming Environment • Run two programs • System’s memory usage determines allocation • Program

Multi-programming Environment • Run two programs • System’s memory usage determines allocation • Program need to be aware of the environment • Where does system loads my code? • You can’t determine… system does. . No Transparency… Free (64 KB) 0 x 90000 ~ 0 xa 0000 Stack - 2 (64 KB) (576 KB ~ 640 KB) Stack - 1 (64 KB) 0 x 80000 ~ 0 x 90000 (512 KB ~ 576 KB) Free (192 KB) 0 x 50000 ~ 0 x 80000 Program Code - 2 (128 KB) (320 KB ~ 512 KB) Program Data - 1 (64 KB) 0 x 40000 ~ 0 x 50000 (256 KB ~ 320 KB) Free (64 KB) Program Data - 2 (64 KB) 0 x 30000 ~ 0 x 40000 (192 KB ~ 256 KB) Program Code - 1 (128 KB) 0 x 10000 ~ 0 x 30000 (64 KB ~ 192 KB) OS 0 x 00000 ~ 0 x 10000 (0 ~ 64 KB)

Multi-programming Environment • Run two programs Stack - 2 (64 KB) Program Data -

Multi-programming Environment • Run two programs Stack - 2 (64 KB) Program Data - 2 (64 KB) Program Code - 2 (160 KB) Free (64 KB) 0 x 90000 ~ 0 xa 0000 (576 KB ~ 640 KB) Stack - 1 (64 KB) 0 x 80000 ~ 0 x 90000 (512 KB ~ 576 KB) Free (96 KB) 0 x 68000 ~ 0 x 80000 (416 KB ~ 512 KB) Program Data - 1 (64 KB) 0 x 58000 ~ 0 x 68000 (352 KB ~ 416 KB) Free (128 KB) 0 x 38000 ~ 0 x 58000 (224 KB ~ 352 KB) Program Code - 1 (160 KB) 0 x 10000 ~ 0 x 38000 (64 KB ~ 224 KB) OS 0 x 00000 ~ 0 x 10000 (0 ~ 64 KB)

Multi-programming Environment • Run two programs • Program size: 64 KB + 160 K

Multi-programming Environment • Run two programs • Program size: 64 KB + 160 K = • Free mem • 64 + 96 + 128 = 288 KB • Cannot run Program – 2 • Can’t fit… Free (64 KB) 0 x 90000 ~ 0 xa 0000 Stack - 2 (64 KB) (576 KB ~ 640 KB) Stack - 1 (64 KB) 0 x 80000 ~ 0 x 90000 288 KB (512 KB ~ 576 KB) Free (96 KB) Program Data - 2 (64 KB) 0 x 68000 ~ 0 x 80000 (416 KB ~ 512 KB) Program Data - 1 (64 KB) 0 x 58000 ~ 0 x 68000 (352 KB ~ 416 KB) Free (128 KB) Program Code - 2 (160 KB) 0 x 38000 ~ 0 x 58000 (224 KB ~ 352 KB) Program Code - 1 (160 KB) 0 x 10000 ~ 0 x 38000 (64 KB ~ 224 KB) Not efficient. . Suffers memory fragmentation problem. . OS 0 x 00000 ~ 0 x 10000 (0 ~ 64 KB)

Multi-programming Environment • Run two programs • What if Program-2’s stack underflows? • What

Multi-programming Environment • Run two programs • What if Program-2’s stack underflows? • What if Program-2’s data overflows? • Without virtual memory • Programs can affect to the other’s execution No isolation. Security problem. Free (64 KB) Stack - 2~ (64 KB) 0 x 90000 0 xa 0000 (576 KB ~ 640 KB) Stack (64 KB) 0 x 80000 ~ 0 x 90000 (512 KB ~ 576 KB) Free (192 KB) Program Code - 2 (128 KB) 0 x 50000 ~ 0 x 80000 (320 KB ~ 512 KB) Program Data (64 KB) 0 x 40000 ~ 0 x 50000 (256 KB ~ 320 KB) Free (64 KB) Program Data - 2 (64 KB) 0 x 30000 ~ 0 x 40000 (192 KB ~ 256 KB) Program Code (128 KB) 0 x 10000 ~ 0 x 30000 (64 KB ~ 192 KB) OS 0 x 00000 ~ 0 x 10000 (0 ~ 64 KB)

Virtual Memory • Three goals • Transparency: does not need to know system’s internal

Virtual Memory • Three goals • Transparency: does not need to know system’s internal state • Program A is loaded at 0 x 8048000. Can Program B be loaded at 0 x 8048000? • Efficiency: do not waste memory; manage memory fragmentation • Can Program B (288 KB) be loaded if 288 KB of memory is free, regardless of its allocation? • Protection: isolate program’s execution environment • Can we prevent an overflow from Program A from overwriting Program B’s data?

Paging • A method of implementing virtual memory • Split memory into multiple 4,

Paging • A method of implementing virtual memory • Split memory into multiple 4, 096 byte blocks (12 -bit) • Last 3 digits of page address are ZERO (in hexadecimal) • Having an indirect map between virtual page and physical page • • Set an arbitrary virtual address for a page, e. g. , 0 x 81815000 Set a physical address to that page as a map, e. g. , 0 x 32000 0 x 81815000 ~ 0 x 81815 fff will be translated into 0 x 32000 ~ 0 x 32 fff

Virtual Memory - Paging Physical Memory • Having an indirect table that maps virt-addr

Virtual Memory - Paging Physical Memory • Having an indirect table that maps virt-addr to phys-addr Stack 0 xbffdf 000 Virtual Physical 0 x 8048000 0 x 10000 Program code 0 x 804 a 000 0 x 8049000 0 x 11000 Program code 0 x 8049000 0 x 804 a 000 0 x 14000 0 xbffdf 000 0 x 12000 … … Program code 0 x 8048000 Program code 0 x 14000 Stack 0 x 12000 Program code 0 x 11000 Program code 0 x 10000

Paging: Virtual Memory • Having an indirect table that maps virt-addr to phys-addr Stack-2

Paging: Virtual Memory • Having an indirect table that maps virt-addr to phys-addr Stack-2 0 xbffdf 000 Stack 0 xbffdf 000 Program code-2 0 x 804 a 000 Program code-2 0 x 8049000 Program code-2 0 x 8048000 Program code 0 x 8048000 Virtual Physical 0 x 8048000 0 x 10000 0 x 8049000 0 x 11000 0 x 804 a 000 0 x 14000 0 xbffdf 000 0 x 12000 … … Virtual-2 Physical-2 0 x 8048000 0 x 13000 0 x 8049000 0 x 15000 0 x 804 a 000 0 x 16000 0 xbffdf 000 0 x 17000 … … Physical Memory Stack-2 0 x 17000 Program code-2 0 x 16000 Program code-2 0 x 15000 Program code 0 x 14000 Program code-2 0 x 13000 Stack 0 x 12000 Program code 0 x 11000 Program code 0 x 10000

Transparency: does not need to know system’s internal state Program A is loaded at

Transparency: does not need to know system’s internal state Program A is loaded at 0 x 8048000. Paging: Virtual Memory Can Program B be loaded at 0 x 8048000? • Having an indirect table that maps virt-addr to phys-addr Stack-2 0 xbffdf 000 Stack 0 xbffdf 000 Program code-2 0 x 804 a 000 Program code-2 0 x 8049000 Program code-2 0 x 8048000 Program code 0 x 8048000 Virtual Physical 0 x 8048000 0 x 10000 0 x 8049000 0 x 11000 0 x 804 a 000 0 x 14000 0 xbffdf 000 0 x 12000 … … Virtual-2 Physical-2 0 x 8048000 0 x 13000 0 x 8049000 0 x 15000 0 x 804 a 000 0 x 16000 0 xbffdf 000 0 x 17000 … … Physical Memory Stack-2 0 x 17000 Program code-2 0 x 16000 Program code-2 0 x 15000 Program code 0 x 14000 Program code-2 0 x 13000 Stack 0 x 12000 Program code 0 x 11000 Program code 0 x 10000

Efficiency: do not waste memory Can Program B (288 KB) be loaded if Paging:

Efficiency: do not waste memory Can Program B (288 KB) be loaded if Paging: Virtual Memory only 288 KB of memory is free, regardless of its allocation? • Having an indirect table that maps virt-addr to phys-addr Stack-2 0 xbffdf 000 Stack 0 xbffdf 000 Program code-2 0 x 804 a 000 Program code-2 0 x 8049000 Program code-2 0 x 8048000 Program code 0 x 8048000 Virtual Physical 0 x 8048000 0 x 10000 0 x 8049000 0 x 11000 0 x 804 a 000 0 x 14000 0 xbffdf 000 0 x 12000 … … Virtual-2 Physical-2 0 x 8048000 0 x 13000 0 x 8049000 0 x 15000 0 x 804 a 000 0 x 16000 0 xbffdf 000 0 x 17000 … … Physical Memory Stack-2 0 x 17000 Program code-2 0 x 16000 Program code-2 0 x 15000 Program code 0 x 14000 Program code-2 0 x 13000 Stack 0 x 12000 Program code 0 x 11000 Program code 0 x 10000

Protection: isolate program’s execution environment Can we prevent an overflow from Program A from

Protection: isolate program’s execution environment Can we prevent an overflow from Program A from Paging: Virtual Memory overwriting Program B’s data? Stack-2 0 xbffdf 000 No mappings, FAULT! Stack 0 xbffdf 000 No mappings, FAULT! Program code-2 0 x 804 a 000 Program code-2 0 x 8049000 Program code-2 0 x 8048000 Program code 0 x 8048000

Page Table • A table that stores virtual address to physical address mapping… •

Page Table • A table that stores virtual address to physical address mapping… • We need to create this per each process… Virtual Physical Virtual-2 Physical-2 0 x 8048000 0 x 10000 0 x 8048000 0 x 13000 0 x 8049000 0 x 11000 0 x 8049000 0 x 15000 0 x 804 a 000 0 x 14000 0 x 804 a 000 0 x 16000 0 xbffdf 000 0 x 12000 0 xbffdf 000 0 x 17000 … …

Page Directory / Table • We access page table by virtual address • Page

Page Directory / Table • We access page table by virtual address • Page size: 4 KB (12 bits) 31 • Page number: 20 bits • What is the page number and offset of • 0 x 8048000 • 0 xb 7 ff 3100 12 Page number 0 Offset

Page Directory / Table • In x 86 (32 -bit), CPU uses 2 -level

Page Directory / Table • In x 86 (32 -bit), CPU uses 2 -level page table • 10 -bit directory index 31 Directory Index (10 -bits) 31 Table index (10 -bits) 22 0 Offset Page number • 10 -bit page table index • 12 -bit offset 12 12

Address Translation 31 12 Virtual address (32 -bits) Offset (12 -bits) Page number (20

Address Translation 31 12 Virtual address (32 -bits) Offset (12 -bits) Page number (20 -bits) Directory Index (10 -bits) 31 Table index (10 -bits) 22 12 Page Directory Entry Page Table Entry 0 Addr PT 0 Addr page 1 Addr PT 1 Addr page 2 Addr PT 2 Addr page 1024 Addr PT 1024 Addr page 0

Address Translation Example 31 12 0 0 x 08048000 Offset (12 -bits) 0 x

Address Translation Example 31 12 0 0 x 08048000 Offset (12 -bits) 0 x 000 Page number (20 -bits) 0 x 08048 Directory Index (10 -bits) 0 x 20 31 Table index (10 -bits) 0 x 48 22 12 Page Directory Entry PT 10 Page Table Entry 0 Addr PT 1 0 NO MAPPING . . Addr PT 2 0 x 48 0 x 10000 0 x 20 Addr PT 10 0 x 49 0 x 11000 0 x 3 ff Addr PT 500 0 x 4 a 0 x 50000 Physical access Virtual Physical 0 x 8048000 0 x 10000 0 x 8049000 0 x 11000 0 x 804 a 000 0 x 50000

Page Table Entry • Address translation is from virtual page to physical page •

Page Table Entry • Address translation is from virtual page to physical page • E. g. , 0 x 8048000 -> 0 x 11000 • We do not need to translate lower 12 bits • E. g. , 0 x 8048001 -> 0 x 8048000 + 0 x 001 -> 0 x 11000 + 0 x 001 • So page table entry only uses higher 20 bits to store physical page address • Lower 12 bits remaining as the same • We do not translate the lower 12 bits!

Address Translation Examples • Virtual addresss 0 x 8048338 • • Virtual page number:

Address Translation Examples • Virtual addresss 0 x 8048338 • • Virtual page number: 0 x 8048 Offset: 0 x 338 Physical page number: 0 x 10 Physical address: 0 x 10 (000) + 0 x 338 = 0 x 10338 • Virtual address 0 x 443325 af • • Virtual page number: 0 x 44332 Offset: 0 x 5 af Physical page number: 0 x 33885 Physical address: 0 x 33885000 + 0 x 5 af = 0 x 338855 af Virtual Physical 0 x 8048000 0 x 10000 0 x 8049000 0 x 11000 0 x 804 a 000 0 x 14000 0 xbffdf 000 0 x 12000 0 x 44332000 0 x 33885000 Virtual Physical 0 x 8048 0 x 10 0 x 8049 0 x 11 0 x 804 a 0 x 14 0 xbffdf 0 x 12 0 x 44332 0 x 33885

PDE, PTE 0: kernel, 1: user Page Directory Entry PT 10 Page Table Entry

PDE, PTE 0: kernel, 1: user Page Directory Entry PT 10 Page Table Entry 0 Addr PT 1 0 NO MAPPING . . Addr PT 2 0 x 48 0 x 10000 0 x 20 Addr PT 10 0 x 49 0 x 11000 0 x 3 ff Addr PT 500 0 x 4 a 0 x 50000

Paging in x 86 • Can be enabled via CR 0 • Page table

Paging in x 86 • Can be enabled via CR 0 • Page table address (physical) need to be stored at CR 3

Paging in x 86 Virtual Address

Paging in x 86 Virtual Address

Paging in x 86 • In JOS (kern/entry. S) Set cr 3 to point

Paging in x 86 • In JOS (kern/entry. S) Set cr 3 to point the address of page directory Turn on CR 0_PG! • After CR 0_PG is up (paging is turned on) • All address access regarded as virtual address

How can we access physical address • After CR 0_PG is up (paging is

How can we access physical address • After CR 0_PG is up (paging is turned on) • All address access regarded as virtual address • What if we would like to access the physical address 0 x 100010? • Our kernel is at physical address 0 x 100000 ~ 0 x 110000 • What’s the virtual addresses of those area?

How can we access physical address 4 GB virtual address space • In JOS,

How can we access physical address 4 GB virtual address space • In JOS, we will map 0 xf 0000000 ~ 0 xffff to • Physical address • 0 x 0000 ~ 0 x 0 fffffff • 256 MB Region • 0 xf 0100010 -> 0 x 0010 • 0 xf 1333358 -> 0 x 01333358 0 xf 0000000 – 0 xffff (256 MB) 256 MB physical address space

In kern/entrypgdir. c Entry_pgdir only contains two entries; 0 ~ 0 x 400000 to

In kern/entrypgdir. c Entry_pgdir only contains two entries; 0 ~ 0 x 400000 to 0 ~ 0 x 400000 (not writable) 0 xf 0000000 ~ 0 xf 0400000 to 0 ~ 0 x 400000 (writable)

In kern/entrypgdir. c 0 xf 0001000 will consult entry_pgtable[1] -> 0 x 001000 |

In kern/entrypgdir. c 0 xf 0001000 will consult entry_pgtable[1] -> 0 x 001000 | PTE_P | PTE_W Phyaddr: 0 x 1000

Page • A page • A 4, 096 (4 KB) block of memory •

Page • A page • A 4, 096 (4 KB) block of memory • Why having a block? • How large does the page table should be to map 4 GB (32 -bit space)? • • If a block is 1 GB – 4 entries * pointer (32 -bit, 4 byte) = 16 byte long If a block is 4 MB – 1024 entries * pointer = 4, 096 bytes, 4 KB long If a block is 4 KB – 1048576 entries * pointer = 4 MB long If a block is 1 byte – 4294967296 entries = 4 GB… NONO… Design consideration: Size of page table matters! Virtual Physical 0 x 8048000 0 x 10000 0 x 8049000 0 x 11000 0 x 804 a 000 0 x 50000

Page (cont’d) • A page • A 4, 096 (4 KB) block of memory

Page (cont’d) • A page • A 4, 096 (4 KB) block of memory • Why 4 KB in Intel x 86? • How much memory do you need to allocate 1 byte? • • 1 GB page – 1 GB 4 MB page – 4 MB 4 KB page – 4 KB 1 B page – 1 B Design consideration: Memory fragmentation matters! Virtual Physical 0 x 8048000 0 x 10000 0 x 8049000 0 x 11000 0 x 804 a 000 0 x 50000

Page Size / Page Table Size • If a page size is too small,

Page Size / Page Table Size • If a page size is too small, it requires a big page table • • 1 B, 4 GB 4 KB, 4 MB, 4 KB 1 G, 16 B • If a page size is too big, unused memory in a page will be wasted • • 1 B - 1 B (no waste) 4 KB – 1 B 4 MB – 1 B 1 G – 1 B

x 86 Memory Access

x 86 Memory Access