initializing memory and memory management in xv6
play

Initializing Memory and Memory Management in xv6 Chester Rebeiro - PowerPoint PPT Presentation

Initializing Memory and Memory Management in xv6 Chester Rebeiro IIT Madras Outline Memory Management in x86 Segmentation Virtual Memory Initializing memory in xv6 Initializing Pages Initializing Segments


  1. Initializing Memory and Memory Management in xv6 Chester Rebeiro IIT Madras

  2. Outline • Memory Management in x86 – Segmentation – Virtual Memory • Initializing memory in xv6 – Initializing Pages – Initializing Segments • Implementation of kalloc 2

  3. x86 address translation Logical Address (segment Linear Physical + Address Address Segmentation Paging offset) CPU Unit Unit Physical Memory 3

  4. x86 address translation Logical Address (segment Linear Physical + Address Address Segmentation Paging offset) CPU Unit Unit Physical Memory 4

  5. Segmentation Unit • Virtual address space of process divided into separate logical segments • Each segment associated with a segment selector and offset Heap Stack Heap Stack Data Data Text Text Address Map of Process 5

  6. Segmentation ( logical to linear address) 6

  7. Example 0x3000 0x3000 Segment Base Limit 0 - - pointer to descriptor table 10000 1 1000 1000 Heap 2 4000 500 9000 3 8000 1000 Stack 4 9000 1000 8000 4500 Data 4000 3000 1 2000 + Text segment register (eg %CS) 1000 100 0 offset register (eg %eip) Address Map of Process 7

  8. Pointer to Descriptor Table • Global Descriptor Table (GDT) • Stored in memory • Pointed to by GDTR (GDT Register) – lgdt (instruction used to load the GDT register) • Similar table called LDT present in x86 (not used by xv6!) Segment Descriptor Segment Descriptor base size Segment Descriptor 47 16 0 Segment Descriptor Segment Descriptor GDTR Segment Descriptor Size : size of GDT GDT Base : pointer to GDT 8

  9. Segment Descriptor • Base Address Access Limit – 0 to 4GB Base Address • Limit – 0 to 4GB • Access Rights – Execute, Read, Write – Privilege Level (0-3) 9

  10. Segment Registers • Holds 16 bit segment selectors – Points to offsets in GDT • Segments associated with one of three types of storage – Code • %CS register holds segment selector • %EIP register holds offset – Data • %DS, %ES, %FS, %GS registers hold segment selector – Stack • %SS register holds segment selector • %SP register holds stack pointer (Note: Only one code segment and stack segment can be accessible at a time. But 4 data segments can be accessed simultaneously) 10

  11. x86 address translation Logical Address (segment Linear Physical + Address Address Segmentation Paging offset) CPU Unit Unit Physical Memory 11

  12. Linear to Physical Address • 2 level page translation • How many page tables are present? • What is the maximum size of the process’ address space? – 4G ref : mmu.h (PGADDR, NPDENTRIES, NPTENTRIES, PGSIZE) 12

  13. The full Picture Each proces Initialized once has one common for all processes 13

  14. Virtual Address Advantages (Isolation between Processes) Process A Process A Process B Process B Stack Stack Heap Heap Process B Process A Data Page Data Page Table Table Text Text (instructions) (instructions) Virtual Memory Virtual Memory Physical Memory 14

  15. Virtual Addressing Advantages Paging on Demand • RAM only loads pages into memory whenever needed • When new program is executed page table is empty Present/absent 0 0 0 0 1 0 0 1 2 0 0 2 3 0 0 4 0 0 3 0 5 0 0 6 0 0 4 1 Page table 5 2 for process (is stored in RAM) 6 3 Present/Absent bit : 1 if entry in page table is valid, 0 if invalid In x86 : PTE_P 15

  16. Paging on Demand (2) • As data gets referenced, page table gets filled up. • Page frame loaded from hard disk Present/absent 0 0 0 0 1 0 0 1 2 1 3 2 3 0 0 4 0 0 3 0 5 0 0 6 0 0 4 1 Page table 5 2 for process (is stored in RAM) 6 3 16

  17. Paging on Demand(3) • As execution progresses, more entries in page table get filled. • Similarly, more frames in RAM get used • Eventually, entire RAM is filled What next? Present/absent 0 1 1 0 1 0 0 1 2 1 3 2 3 1 2 4 1 0 3 0 5 0 0 6 0 0 4 1 Page table 5 2 for process (is stored in RAM) 6 3 17

  18. Paging on Demand (4) • A particular frame is selected and swapped out into disk • A new page swapped in Disk • Page table is updated (swap space) Present/absent Swap in 0 0 1 0 1 0 0 1 Swap out 2 1 3 2 3 1 2 4 1 0 3 0 5 0 0 6 1 1 4 1 Page table 5 2 for process (is stored in RAM) 6 3 18

  19. Virtual Memory achieves Security Security • Security – Page tables augmented by protection bits protection bits Page table for process 19

  20. Protection Bits in x86 • PTE_W : controls if instructions are allowed to write to the page • PTE_U : controls if user process can use the page. If not only kernel can use the page These are checked by the MMU for each memory access! 20

  21. Virtual Addressing Advantages (easy to make copies of a process) • Making a copy of a process is called forking. Parent – Parent (is the original) Page Table – child (is the new process) • When fork is invoked, – child is an exact copy of parent Child • When fork is called all pages Page are shared between parent Table and child Physical Memory • Easily done by copying the parent’s page tables 21

  22. Virtual Addressing Advantages (Shared libraries) • Many common functions such as printf implemented in shared libraries • Pages from shared libraries, shared between processes Process A Process A Process B Process B Process B Process A Page Page Table Table printf(){ …} printf(){ …} printf(){ …} Virtual Memory Virtual Memory Physical Memory 22

  23. Virtual Addressing Advantages (Shared Memory) userspace • Shared memory between processes Process 1 easily implemented using virtual memories – Shared memory mapped to the same Shared page memory – Writes from one process visible to Process 2 another process kernel 23

  24. back to booting… 24

  25. so far… Power on Reset • executes on reset. • does POST, initializes devices BIOS • loads boot loader to 0x07c00 and jump to it (all in real mode) bootloader • disable interrupts • Setup GDT (8941) • switch real mode to protected mode • setup an initial stack (8967) • load kernel from second sector of disk to 0x100000 • executes kernel (_start) 25

  26. Memory when kernel is invoked (just after the bootloader) • Segmentation enabled but no paging Segmentation physical CPU logical physical Unit memory address address • Memory map stack kernel code logical data memory physical memory 0x100000 bootloader Slide taken from Anton Burtsev, Univ. of Utah 26

  27. Memory Management Analysis Segmentation physical CPU logical physical Unit memory address address • Advantages – Got the kernel into protected mode (32 bit code) with minimum trouble • Disadvantages – Protection of kernel memory from user writes – Protection between user processes – User space restricted by physical memory • The plan ahead – Need to get paging up and running 27

  28. OS code are not Relocatable • kernel.asm (xv6) • The linker sets the executable so that the kernel starts from 0x80100000 • 0x80100000 is a virtual address and not a physical address 28

  29. Virtual Address Space Virtual 0xffffffff • Kernel memory mapped into every process - easy to switch between kernel and user modes Device memory • VA(KERNBASE:+PHYSTOP)  PA(0:PHYSTOP ) - convert from VA to PA just by +/- KERNBASE - easily write to physical page - limits size of physical memory to 2GB Kernel Memory +0x100000 Physical KERNBASE 0x80000000 PHYSTOP 0x100000 0 0 ref : memlayout.h (0200) 29

  30. Converting virtual to physical in kernel space What would be the address generated before and immediately after paging is enabled? before : 0x001000xx Immediately after : 0x8001000xx So the OS needs to be present at two memory ranges 30

  31. Early Kernel Paging Initialization • Kernel entry point : _start ( 1036 ) Turn on Page size extension Why 4MB pages? Simplicity Set Page Directory (We just want 2 pages) 31

  32. 4MB Pages 32

  33. Kernel memory setup • First setup two 4MB pages – Entry 0: Virtual addresses 0 to 0x04000000  Physical addresses 0 to 4MB – Entry 512: Virtual addresses 0x80000000 to 0x84000000  Physical addresses 0 to 4MB Why do we need to map this twice? 33

  34. First Page Table logical memory virtual memory physical memory courtesy Anton Burtsev, Univ. of Utah 34

  35. Enable Paging • Entry point : _start ( 1036 ) Turn on Page size extension Set Page Directory Enable Paging 35

  36. Stack setup • Entry point : _start ( 1036 ) Turn on Page size extension Set Page Directory Enable Paging Stack setup 36

  37. Stack courtesy Anton Burtsev, Univ. of Utah 37

  38. Execute main • entry point : _start ( 1036 ) Turn on Page size extension Set Page Directory Enable Paging Stack setup Jump to main 38

  39. New Address Scheme Analysis Scheme : enable paging with 2 pages of 4MB each • Advantages, – Useful for initializing the rest of memory • (issues with kmalloc …. later!!!) • Disadvantages – Kernel mapped twice, reducing user space area – Only 4MB of physical memory is mapped. Remaining is unutilized xv6 next goes into the final addressing scheme 39

Recommend


More recommend