CS444/544 Operating Systems II Virtual Memory

Slides:



Advertisements
Similar presentations
1/1/ / faculty of Electrical Engineering eindhoven university of technology Memory Management and Protection Part 3:Virtual memory, mode switching,
Advertisements

Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU
Intel MP.
Memory Management Questions answered in this lecture: How do processes share memory? What is static relocation? What is dynamic relocation? What is segmentation?
OS Memory Addressing.
Operating Systems: Segments 1 Segmentation Hardware Support single user program system: – wish somehow to relocate address 0 to after operating system.
IA-32 Processor Architecture
1 Hardware and Software Architecture Chapter 2 n The Intel Processor Architecture n History of PC Memory Usage (Real Mode)
X86 segmentation, page tables, and interrupts 3/17/08 Frans Kaashoek MIT
Memory Management (II)
CE6105 Linux 作業系統 Linux Operating System 許 富 皓. Chapter 2 Memory Addressing.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
CS2422 Assembly Language & System Programming September 22, 2005.
Microprocessor Systems Design I Instructor: Dr. Michael Geiger Fall 2012 Lecture 15: Protected mode intro.
UNIT 2 Memory Management Unit and Segment Description and Paging
Intel MP (32-bit microprocessor) Designed to overcome the limits of its predecessor while maintaining the software compatibility with the.
1 / 40 Implementation of Paging in uCOS-II Ke, Dian Chia.
Memory Addressing in Linux  Logical Address machine language instruction location  Linear address (virtual address) a single 32 but unsigned integer.
Microprocessor system architectures – IA32 segmentation Jakub Yaghob.
The Pentium Processor.
The Intel Microprocessors. Real Mode Memory Addressing Real mode, also called real address mode, is an operating mode of and later x86-compatible.
Laface Operating System Design Booting a PC to run a kernel from Low memory VGA display.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Memory Addressing / Kernel Modules.
1 Linux Operating System 許 富 皓. 2 Memory Addressing.
80386DX.
1 i386 Memory Management Professor Ching-Chi Hsu 1998 年 4 月.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Hardware.
Processes and Virtual Memory
1 Microprocessors CSE Protected Mode Memory Addressing Remember using real mode addressing we were previously able to address 1M Byte of memory.
Page Replacement Implementation Issues Text: –Tanenbaum ch. 4.7.
10. Epilogue ENGI 3655 Lab Sessions.  We took control of the computer as early as possible, right after the end of the BIOS  Our multi-stage bootloader.
Address Translation Mark Stanovich Operating Systems COP 4610.
X86 (32-bit) Paging Reference –text: Tanenbaum ch.4.3 Reference on Win2K memory management –text: Tanenbaum ch
OS Memory Addressing. Architecture CPU – Processing units – Caches – Interrupt controllers – MMU Memory Interconnect North bridge South bridge PCI, etc.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Memory Management Chapter 5 Advanced Operating System.
18-447: Computer Architecture Lecture 17: Virtual Memory II Yoongu Kim Carnegie Mellon University Spring 2013, 2/27.
EEL 4709C Prof. Watson Herman Group 4 Ali Alshamma, Derek Montgomery, David Ortiz 11/11/2008.
Lecture on Real Mode Memory Addressing
16.317: Microprocessor System Design I
A Real Problem What if you wanted to run a program that needs more memory than you have? September 11, 2018.
CS703 - Advanced Operating Systems
Outline Paging Swapping and demand paging Virtual memory.
COMBINED PAGING AND SEGMENTATION
143A: Principles of Operating Systems Lecture 7: System boot
Microprocessor Systems Design I
Anton Burtsev February, 2017
Address Translation Mechanism of 80386
Memory Management References text: Tanenbaum ch.4.
Computer Architecture Lecture 13: Virtual Memory II
143A: Principles of Operating Systems Lecture 5: Address translation
Protection UQ: Explain the protection mechanism of X86 Intel family microprocessor(10 Marks)
x86 segmentation, page tables, and interrupts
Page Replacement Implementation Issues
Instructors: Haryadi Gunawi
CSE 153 Design of Operating Systems Winter 2018
Sarah Diesburg Operating Systems CS 3430
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
Memory Management References text: Tanenbaum ch.4.
CS399 New Beginnings Jonathan Walpole.
Page Replacement Implementation Issues
Introduction to the Intel x86’s support for “virtual” memory
Virtual Memory Hardware
Lecture 37 Syed Mansoor Sarwar
Information Security - 2
Lecture 7: Flexible Address Translation
CSE 153 Design of Operating Systems Winter 2019
Assembly Language for Intel-Based Computers, 5th Edition
Cache writes and examples
CS 444/544 Operating Systems II Virtual Memory Translation
Presentation transcript:

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

Recap – Real Mode Real mode segmentation, how? What is A20? seg * 16 + offset [b000:b7ff] => 0xb000 * 16 + 0xb7ff = 0xbb7ff What is A20? [f800:8001] => 0x100001? [f800:8001] => 0x1? 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 0xf000:0xffff – BIOS Loads boot sector – runs 0x7c00 Enable A20 Enable protected mode (enabling 4GB memory access) Read kernel ELF (Executable Linkable Format) …

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

i386 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/x86_translation_and_registers.pdf

i386 Protected Mode Base Limit Any 32-bit address 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/x86_translation_and_registers.pdf https://wiki.osdev.org/Global_Descriptor_Table

i386 Protected Mode G - Granularity (0 = byte, 1 = page) 0: Limit will be byte granularity (i.e., limit, only access 220, 1MB) 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 0x0:0x8080 Base: 0x0 Limit (addr): 0xfffff000 Offset: 0x8080 Address: 0x8080 GDT index 32-bit Base 20-bit Limit 12-bit Flags 16 0x31310000 0x1 G=1 8 0x40000000 0x80000 0x0 0xfffff

Protected Mode - Examples 0x8:0x8080 Base: 0x40000000 Limit (addr): 0x8000000 Offset: 0x8080 Address: 0x40008080 GDT index 32-bit Base 20-bit Limit 12-bit Flags 16 0x31310000 0x1 G=1 8 0x40000000 0x80000 0x0 0xfffff

Protected Mode - Examples 0x10:0x333 Base: 0x31310000 Limit (addr): 0x1000 Offset: 0x333 Address: 0x31310333 0x10:0x8080 Offset: 0x8080 Offset > limit Access denied! GDT index 32-bit Base 20-bit Limit 12-bit Flags 16 0x31310000 0x1 G=1 8 0x40000000 0x80000 0x0 0xfffff

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 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 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 0x31310000 0x1 G=1, DPL=3 8 KERNEL 0x40000000 0x80000 G=1, DPL=0 0 KERNEL 0x0 0xfffff

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 == 0x8 == 1000 in binary, last 2 bits are ZERO -> KERNEL! %cs == 0x13 == 10011 in binary, last 2 bits are 3 -> USER! %cs == 0x10 == 10000 in binary, last 2 bits are 0 -> KERNEL! %cs == 0xb == 1011…. GDT index 32-bit Base 20-bit Limit 12-bit Flags 16 USER 0x31310000 0x1 G=1, DPL=3 8 KERNEL 0x40000000 0x80000 G=1, DPL=0 0 KERNEL 0x0 0xfffff

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 0x3:0x1234 is possible CPL==3, then ljmp 0x0:0x1234 is impossible GDT index 32-bit Base 20-bit Limit 12-bit Flags 16 USER 0x31310000 0x1 G=1, DPL=3 8 KERNEL 0x40000000 0x80000 G=1, DPL=0 0 KERNEL 0x0 0xfffff

x86 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 Defined in the last 2 bits of any segment register DPL = Descriptor Privilege Level Defined in the GDT Ring switch rule CPL <= DPL RPL <= DPL Must meet both conditions..

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

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

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

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 CR0 to enable protected mode CR0_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

Uniprogramming Environment Free (576 KB) 0x10000 ~ 0xa0000 (64KB ~ 640KB) Run one program The program can use memory space freely… Stack - 1 Program Data - 1 Program Code - 1 OS 0x00000 ~ 0x10000 (0 ~ 64KB)

Uniprogramming Environment Free (576 KB) 0x10000 ~ 0xa0000 (64KB ~ 640KB) Run one program The program can use memory space freely… Stack - 1 Stack (64KB) 0x80000 ~ 0x90000 (512KB ~ 576KB) Program Data - 1 Program Data (64 KB) 0x40000 ~ 0x50000 (256KB ~ 320KB) Program Code - 1 OS 0x00000 ~ 0x10000 (0 ~ 64KB)

Uniprogramming Environment Free (64 KB) 0x90000 ~ 0xa0000 (576KB ~ 640KB) Run one program The program can use memory space freely… Stack - 1 (64KB) 0x80000 ~ 0x90000 (512KB ~ 576KB) Free (192 KB) 0x50000 ~ 0x80000 (320KB ~ 512KB) Program Data - 1 (64 KB) 0x40000 ~ 0x50000 (256KB ~ 320KB) Free (64 KB) 0x30000 ~ 0x40000 (192KB ~ 256KB) Program Code - 1 (128KB) 0x10000 ~ 0x30000 (64KB ~ 192KB) OS 0x00000 ~ 0x10000 (0 ~ 64KB)

Multi-programming Environment Free (64 KB) 0x90000 ~ 0xa0000 (576KB ~ 640KB) Run two programs Stack - 1 (64KB) 0x80000 ~ 0x90000 (512KB ~ 576KB) Stack - 2 (64KB) Free (192 KB) 0x50000 ~ 0x80000 (320KB ~ 512KB) Program Data - 2 (64 KB) Program Data - 1 (64 KB) 0x40000 ~ 0x50000 (256KB ~ 320KB) Free (64 KB) 0x30000 ~ 0x40000 (192KB ~ 256KB) Program Code - 2 (128KB) Program Code - 1 (128KB) 0x10000 ~ 0x30000 (64KB ~ 192KB) OS 0x00000 ~ 0x10000 (0 ~ 64KB)

Multi-programming Environment Stack - 2 (64KB) Free (64 KB) 0x90000 ~ 0xa0000 (576KB ~ 640KB) 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.. Stack - 1 (64KB) 0x80000 ~ 0x90000 (512KB ~ 576KB) Free (192 KB) 0x50000 ~ 0x80000 (320KB ~ 512KB) Program Code - 2 (128KB) Program Data - 1 (64 KB) 0x40000 ~ 0x50000 (256KB ~ 320KB) Program Data - 2 (64 KB) Free (64 KB) 0x30000 ~ 0x40000 (192KB ~ 256KB) Program Code - 1 (128KB) 0x10000 ~ 0x30000 (64KB ~ 192KB) No Transparency… OS 0x00000 ~ 0x10000 (0 ~ 64KB)

Multi-programming Environment Free (64 KB) 0x90000 ~ 0xa0000 (576KB ~ 640KB) Run two programs Stack - 1 (64KB) 0x80000 ~ 0x90000 (512KB ~ 576KB) Stack - 2 (64KB) Free (96 KB) 0x68000 ~ 0x80000 (416KB ~ 512KB) Program Data - 2 (64 KB) Program Data - 1 (64 KB) 0x58000 ~ 0x68000 (352KB ~ 416KB) Free (128 KB) 0x38000 ~ 0x58000 (224KB ~ 352KB) Program Code - 2 (160KB) Program Code - 1 (160KB) 0x10000 ~ 0x38000 (64KB ~ 224KB) OS 0x00000 ~ 0x10000 (0 ~ 64KB)

Multi-programming Environment Stack - 2 (64KB) Free (64 KB) 0x90000 ~ 0xa0000 (576KB ~ 640KB) Run two programs Program size: 64KB + 64KB + 160K = 288KB Free mem 64 + 96 + 128 = 288KB Cannot run Program – 2 Can’t fit… Stack - 1 (64KB) 0x80000 ~ 0x90000 (512KB ~ 576KB) Program Data - 2 (64 KB) Free (96 KB) 0x68000 ~ 0x80000 (416KB ~ 512KB) Program Data - 1 (64 KB) 0x58000 ~ 0x68000 (352KB ~ 416KB) Program Code - 2 (160KB) Free (128 KB) 0x38000 ~ 0x58000 (224KB ~ 352KB) Program Code - 1 (160KB) 0x10000 ~ 0x38000 (64KB ~ 224KB) OS 0x00000 ~ 0x10000 (0 ~ 64KB) Not efficient.. Suffers memory fragmentation problem..

Multi-programming Environment Stack - 2 (64KB) Free (64 KB) 0x90000 ~ 0xa0000 (576KB ~ 640KB) 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 Stack (64KB) 0x80000 ~ 0x90000 (512KB ~ 576KB) Program Code - 2 (128KB) Free (192 KB) 0x50000 ~ 0x80000 (320KB ~ 512KB) Program Data (64 KB) 0x40000 ~ 0x50000 (256KB ~ 320KB) Program Data - 2 (64 KB) Free (64 KB) 0x30000 ~ 0x40000 (192KB ~ 256KB) Program Code (128KB) 0x10000 ~ 0x30000 (64KB ~ 192KB) OS 0x00000 ~ 0x10000 (0 ~ 64KB) No isolation. Security problem.

Virtual Memory Three goals Transparency: does not need to know system’s internal state Program A is loaded at 0x8048000. Can Program B be loaded at 0x8048000? Efficiency: do not waste memory; manage memory fragmentation Can Program B (288KB) 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,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., 0x81815000 Set a physical address to that page as a map, e.g., 0x32000 0x81815000 ~ 0x81815fff will be translated into 0x32000 ~ 0x32fff

Virtual Memory - Paging Physical Memory Having an indirect table that maps virt-addr to phys-addr Stack 0xbffdf000 Virtual Physical 0x8048000 0x10000 0x8049000 0x11000 0x804a000 0x14000 0xbffdf000 0x12000 … Program code 0x14000 Program code 0x804a000 Program code 0x8049000 Stack 0x12000 Program code 0x8048000 Program code 0x11000 Program code 0x10000

Paging: Virtual Memory Physical Memory Stack-2 0x17000 Having an indirect table that maps virt-addr to phys-addr Stack-2 0xbffdf000 Stack 0xbffdf000 Virtual Physical 0x8048000 0x10000 0x8049000 0x11000 0x804a000 0x14000 0xbffdf000 0x12000 … Program code-2 0x16000 Program code-2 0x15000 Program code 0x14000 Program code-2 0x804a000 Program code 0x804a000 Program code-2 0x13000 Virtual-2 Physical-2 0x8048000 0x13000 0x8049000 0x15000 0x804a000 0x16000 0xbffdf000 0x17000 … Program code-2 0x8049000 Program code 0x8049000 Stack 0x12000 Program code-2 0x8048000 Program code 0x8048000 Program code 0x11000 Program code 0x10000

Paging: Virtual Memory Transparency: does not need to know system’s internal state Program A is loaded at 0x8048000. Can Program B be loaded at 0x8048000? Paging: Virtual Memory Physical Memory Stack-2 0x17000 Having an indirect table that maps virt-addr to phys-addr Stack-2 0xbffdf000 Stack 0xbffdf000 Virtual Physical 0x8048000 0x10000 0x8049000 0x11000 0x804a000 0x14000 0xbffdf000 0x12000 … Program code-2 0x16000 Program code-2 0x15000 Program code 0x14000 Program code-2 0x804a000 Program code 0x804a000 Program code-2 0x13000 Virtual-2 Physical-2 0x8048000 0x13000 0x8049000 0x15000 0x804a000 0x16000 0xbffdf000 0x17000 … Program code-2 0x8049000 Program code 0x8049000 Stack 0x12000 Program code-2 0x8048000 Program code 0x8048000 Program code 0x11000 Program code 0x10000

Paging: Virtual Memory Efficiency: do not waste memory Can Program B (288KB) be loaded if only 288 KB of memory is free, regardless of its allocation? Paging: Virtual Memory Physical Memory Stack-2 0x17000 Having an indirect table that maps virt-addr to phys-addr Stack-2 0xbffdf000 Stack 0xbffdf000 Virtual Physical 0x8048000 0x10000 0x8049000 0x11000 0x804a000 0x14000 0xbffdf000 0x12000 … Program code-2 0x16000 Program code-2 0x15000 Program code 0x14000 Program code-2 0x804a000 Program code 0x804a000 Program code-2 0x13000 Virtual-2 Physical-2 0x8048000 0x13000 0x8049000 0x15000 0x804a000 0x16000 0xbffdf000 0x17000 … Program code-2 0x8049000 Program code 0x8049000 Stack 0x12000 Program code-2 0x8048000 Program code 0x8048000 Program code 0x11000 Program code 0x10000

Paging: Virtual Memory Protection: isolate program’s execution environment Can we prevent an overflow from Program A from overwriting Program B’s data? Paging: Virtual Memory Stack-2 0xbffdf000 Stack 0xbffdf000 No mappings, FAULT! No mappings, FAULT! Program code-2 0x804a000 Program code 0x804a000 Program code-2 0x8049000 Program code 0x8049000 Program code-2 0x8048000 Program code 0x8048000

Page Table A table that stores virtual address to physical address mapping… We need to create this per each process… Virtual Physical 0x8048000 0x10000 0x8049000 0x11000 0x804a000 0x14000 0xbffdf000 0x12000 … Virtual-2 Physical-2 0x8048000 0x13000 0x8049000 0x15000 0x804a000 0x16000 0xbffdf000 0x17000 …

Page Directory / Table We access page table by virtual address Page size: 4 KB (12bits) Page number: 20 bits What is the page number and offset of 0x8048000 0xb7ff3100 31 12 Page number Offset

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

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

Address Translation Example 31 12 0x08048000 Page number (20-bits) 0x08048 Offset (12-bits) 0x000 Directory Index (10-bits) 0x20 Table index (10-bits) 0x48 31 22 12 Virtual Physical 0x8048000 0x10000 0x8049000 0x11000 0x804a000 0x50000 Page Directory Entry Addr PT1 .. Addr PT2 0x20 Addr PT10 0x3ff Addr PT500 PT 10 Page Table Entry NO MAPPING 0x48 0x10000 0x49 0x11000 0x4a 0x50000 Physical access

Page Table Entry Address translation is from virtual page to physical page E.g., 0x8048000 -> 0x11000 We do not need to translate lower 12 bits E.g., 0x8048001 -> 0x8048000 + 0x001 -> 0x11000 + 0x001 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 Physical 0x8048000 0x10000 0x8049000 0x11000 0x804a000 0x14000 0xbffdf000 0x12000 0x44332000 0x33885000 Virtual addresss 0x8048338 Virtual page number: 0x8048 Offset: 0x338 Physical page number: 0x10 Physical address: 0x10 (000) + 0x338 = 0x10338 Virtual address 0x443325af Virtual page number: 0x44332 Offset: 0x5af Physical page number: 0x33885 Physical address: 0x33885000 + 0x5af = 0x338855af Virtual Physical 0x8048 0x10 0x8049 0x11 0x804a 0x14 0xbffdf 0x12 0x44332 0x33885

PDE, PTE 0: kernel, 1: user Page Directory Entry Addr PT1 .. Addr PT2 Addr PT1 .. Addr PT2 0x20 Addr PT10 0x3ff Addr PT500 PT 10 Page Table Entry NO MAPPING 0x48 0x10000 0x49 0x11000 0x4a 0x50000

Paging in x86 Can be enabled via CR0 Page table address (physical) need to be stored at CR3

Paging in x86 Virtual Address

Paging in x86 In JOS (kern/entry.S) After CR0_PG is up (paging is turned on) All address access regarded as virtual address Set cr3 to point the address of page directory Turn on CR0_PG!

How can we access physical address After CR0_PG is up (paging is turned on) All address access regarded as virtual address What if we would like to access the physical address 0x100010? Our kernel is at physical address 0x100000 ~ 0x110000 What’s the virtual addresses of those area?

How can we access physical address 4GB virtual address space 256 MB physical address space In JOS, we will map 0xf0000000 ~ 0xffffffff to Physical address 0x00000000 ~ 0x0fffffff 256 MB Region 0xf0100010 -> 0x00100010 0xf1333358 -> 0x01333358 0xf0000000 – 0xffffffff (256MB)

In kern/entrypgdir.c Entry_pgdir only contains two entries; 0 ~ 0x400000 to 0 ~ 0x400000 (not writable) 0xf0000000 ~ 0xf0400000 to 0 ~ 0x400000 (writable)

In kern/entrypgdir.c 0xf0001000 will consult entry_pgtable[1] -> 0x001000 | PTE_P | PTE_W Phyaddr: 0x1000

Page A page Why having a block? A 4,096 (4 KB) block of memory Why having a block? How large does the page table should be to map 4GB (32-bit space)? If a block is 1GB – 4 entries * pointer (32-bit, 4byte) = 16 byte long If a block is 4MB – 1024 entries * pointer = 4,096 bytes, 4KB long If a block is 4KB – 1048576 entries * pointer = 4MB long If a block is 1 byte – 4294967296 entries = 4GB… NONO… Virtual Physical 0x8048000 0x10000 0x8049000 0x11000 0x804a000 0x50000 Design consideration: Size of page table matters!

Page (cont’d) A page Why 4KB in Intel x86? A 4,096 (4 KB) block of memory Why 4KB in Intel x86? How much memory do you need to allocate 1 byte? 1GB page – 1GB 4MB page – 4MB 4KB page – 4KB 1B page – 1B Virtual Physical 0x8048000 0x10000 0x8049000 0x11000 0x804a000 0x50000 Design consideration: Memory fragmentation matters!

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

x86 Memory Access