Operating Systems Engineering xv6 & page tables

  • Upload
    skah

  • View
    99

  • Download
    11

Embed Size (px)

DESCRIPTION

Operating Systems Engineering xv6 & page tables. Dan Tsafrir, 2013-03-20, 2013-04-03. Reminder: goal of “processes”. Bring a program to life Give each process a private memory area For code, data, stack Illusion of its own dedicated machine - PowerPoint PPT Presentation

Citation preview

MAMAS

Operating Systems Engineering

xv6 & page tablesDan Tsafrir, 2013-03-20, 2013-04-03 OSE 2013 xv6 & page tables (lec2)#1Reminder: goal of processesBring a program to life

Give each process a private memory areaFor code, data, stackIllusion of its own dedicated machine

Prevent each process from reading/writing outside its address spaceBut allow sharing when needed

OSE 2013 xv6 & page tables (lec2)#HW & OS collaborationOS manages processes(De)allocate their physical memory (create, grow, remove)Implement multiprogramming (multiplex HW)Keep track of processes when theyre not executingConfigure HW

HW performs address translation & protectionTranslate user addresses to physical addressesDetect & prevent accesses outside address spaceAllow cross-space transfers (system calls, interrupts, )

Note thatOS needs its own address spaceOS should be able to easily read/write user memory

OSE 2013 xv6 & page tables (lec2)#HW & OS collaborationHW support not necessarily corresponds well to what OS wants For example: virtual memory management in Linux/PPC vs. AIX/PPC

Two main approaches to x86 memory protectionSegments & page tables

Paging has wonMost OSes are designed for pagingThankfully, segments stopped being relevant for us; xv6 much easier to understandA2: its similar enoughTo those other modern OSesOnce you've explored xv6, youll find your way inside kernels such as LinuxA3: as noted, itll help you To build your own (J)OS OSE 2013 xv6 & page tables (lec2)#OS *engineering*JOSOccupies a very different point in the design & implementation space from xv6Types of OSesMicro kernelQNX, L4, MinixMonolithic kernelxv6, Unix family (Linux, FreeBSD, NetBSD, OpenBSD, Solaris/SunOS, AIX, HPUX, IRIX, Darwin), Windows familyActually, most are nowadays hybridExokernelJOS

OSE 2013 xv6 & page tables (lec2)#The 1st processIn previous tutorialChapter #1 in xv6 commentaryThis lecture: Chapter #2

OSE 2013 xv6 & page tables (lec2)#Implementingvirtual memory in Xv6From the perspective of the boot process OSE 2013 xv6 & page tables (lec2)#xv6 address spacexv6 enforces memory address space isolationNo process can write on anothers space or the kernelsxv6 does memory multiplexingevery processs memory starts at 0 & (appears) contiguousC, as well as the linker, expect contiguityxv6 does simple page-table tricksMapping the same memory (kernels) in several address spacesMapping the same memory (kernels) more than once in one address space Kernel can access user pages using user virtual addresses (0)Kernel can also access user pages through kernels physical view of memoryGuarding a user stack with an unmapped page

OSE 2013 xv6 & page tables (lec2)#xv6 address spacex86 defines three kinds of memory addressesVirtual (used by program), which is transformed toLinear (accounting for segments), which is transformed toPhysical (actual DRAM address)xv6 nearly doesnt use segmentsAll their bases set to 0 (and their limits to the max)virtual address = linear addressHenceforth well use only the term virtual OSE 2013 xv6 & page tables (lec2)#Reminder: paging in a nutshellVirtual address (we assume 32bit space & 4KB pages):

Given a process to run, set cr3 = pgdir (page directory)Accessing pgdir[pdx], we find this PDE (page directory entry)

Flags (bits): PTE_P (present), PTE_W (write) , PTE_U (user)20bits are enough, as we count pages of the size 2^12 (=4KB)The page table pgtab = pgdir[pdx] & 0x ffff f000An actual physical addressAccessing pgtab[ptx], we find this PTE (page table entry)

Target physical address =(pgtab[ptx] & 0x ffff f000) | (virt_adrs & 0x 0000 0fff)

page offset (12bit)ptx (10bit)pdx (10bit)flags (12bit)physical address (20bit)flags (12bit)physical address (20bit) OSE 2013 xv6 & page tables (lec2)#paging HW

OSE 2013 xv6 & page tables (lec2)#xv6 virtual memoryEach process has its own unique pgdir (= address space)When the process is about to runcr3 is assigned with the corresponding pgdirEvery process has at most KERNBASE (=2GB) memory(Actually less, since we assume PHYSTOP = 224 MB) VA:KERNBASE KERNBASE+PHYSTOPmapped to PA:0 PHYSTOPSuch mapping exists in every v-space of every processPTEs corresponding to addresses higher than KERNBASE have the PTE_U bit off, so processes cant access themBenefit: Kernel can use each process v-space to access physical memoryThere exists a simple mapping from kernel v-space to all physicalPA = VA - KERNBASE

OSE 2013 xv6 & page tables (lec2)#xv6 virtual memoryAssumeProcess P size if 12KB (3 pages) P sbrk-s (dynamically allocates) another pageAssume the free page xv6 decides to give P is in PA:0x 2010 0000Thus, to ensure contiguity, the 4th PTE of P(Which covers VAs: 0x 0000 3000 0x 0000 3fff)(4096 = 16^3 = 0x 0000 1000)should be mapped to physical page 0x20100 (= the upper 20 bits of PA 0x 2010 0000)So 2 different PTEs now refer to PA = 0x 2010 0000kernel:PTE of VA = KERNBASE + 0x 2010 0000, andprocess:PTE of VA = 0x 0000 3000The kernel can use both

OSE 2013 xv6 & page tables (lec2)#Code: entry page tableIn virtual space, kernel starts inline: 0208 (memlayout.h)KERNLINK = KERNBASE + EXTMEM = 2GB + 1MB = 0x 8010 0000Why KERNBASE (= 2GB) so high in v space?b/c kernel v space mapped in each process v spaceleave enough room to allow process v space to growBoot loader loads xv6 kernel into physical address:0x 0010 0000 (= 1MB)Why not at 0x 8010 0000 (where, in terms of VA, the kernel expects to find its instructions & data)?Might not existWhy not at 0x0?First 1MB for legacy devices

OSE 2013 xv6 & page tables (lec2)#device memory4 GBDEVSPACE = 0x FE00 0000 = 4GB 32MBuser program textuser program datauser program stack(PGSIZE)

user programdata & heap0mapped to arbitrary locations in thephysical memoryKERNBASE = 0x 8000 0000 = 2GB2GB + 1MBkernel textkernel dataend

free memoryKERNBASE+PHYSTOP = 2GB + 224MBvirtualbase memory0I/O spaceextended memory640 KB1MBPHYSTOP = 224MBdevicesphysical4 GBRWURWURWURW-R--RW-RW-RW- OSE 2013 xv6 & page tables (lec2)#First page directoryentrypgdirThe first page directory line: 1311 (flags at 0800, mmu.h)Used by...entryBoot-loader loads xv6 from disk & starts executing hereline: 1040 1061 (V2P_WO at 0200 page 2) OSE 2013 xv6 & page tables (lec2)#Creating an address spacemainline: 12172nd line of main: kvmalloc (well get back to the 1st)line: 1757; switch to page table that maps all memorysetupkvm (1737) + kmap array (1728); mappages ();walpgdir (); OSE 2013 xv6 & page tables (lec2)#Physical memory allocatorMaintain a free-list of 4KB-pagesFrom end of kernel to PHYSTOPBootstrap problemAll of physical memory must be mapped in order for the allocator to initialize the free listBut creating a page table with those mappings involves allocating page-table pagesxv6 solves this problem by using a separate page allocator during entry, which allocates memory just after the end of the kernels data segmentThis allocator does not support freeing & is limited by 4 MB mapping in the entrypgdir, but that is sufficient to allocate the first kernel page table OSE 2013 xv6 & page tables (lec2)#Physical memory allocatorThe 2 free listsRepresentation: struct run (2764) and kmem (2772)next saved in chained pageskinit1 (2780), kinit2 (2788)Called from main (rows: 1219, 1238)freerange (2801), kfree (2815)Allocation of a page: kalloc (2838)

OSE 2013 xv6 & page tables (lec2)#User part of an address space

(unmapped) OSE 2013 xv6 & page tables (lec2)#HomeworkRead pages 30 32 in xv6 commentary OSE 2013 xv6 & page tables (lec2)#END OSE 2013 xv6 & page tables (lec2)#Process creationstruct proc (lines: 2053-2067)Why do we need a kernel stack for every process?Why cant the kernel use the user stack?main (line 1237) => userinit (line 2202) => allocproc (line 2155)userinit creates the first process (init) using allocprocallocproc creates all processes (used by forkallocproc (lines: 2155-2194)find empty proc entry allcate pid and set statecreate the kernel stack of the new process as in the next slideuserinit (lines: 2202-2226)inituvn (1786), initcode.S (7500)

OSE 2013 xv6 & page tables (lec2)#

struct trapframe forthe new processstruct context forthe new process;will be consumedby the switch func(future lecture)forkret OSE 2013 xv6 & page tables (lec2)#Running a processmain => userinit => mpmain (1267) => scheduler (2408)=> switchuvm (1764) OSE 2013 xv6 & page tables (lec2)#