📂
build a OS
  • Learn OS with me
  • OS Interfaces
    • OS interfaces
    • I/O and File descriptors
    • Process and Memory
    • Pipes
    • File
  • OS Organization
    • OS Organization
    • Challenge yourself
  • Memory Management
    • XV6 Virtual Memory
    • Page Table
      • Part 1: How to translate address
      • Part 2: Create an Address Space
      • Part 3: How Page Table is used
      • Part 4: Page Fault and Swap
      • Part 5: How to operate on page tables
    • xv6 buddy allocator
      • How to display physical memory
    • Memory Management Walk Through
  • Traps and Interrupts
    • Trap Home Page
      • 系统调用的核心原理
    • What is trapframe
    • What is trampoline
    • Traps from kernel space
    • How fork() works
    • How system calls get into/out of the kernel
    • How exec() works
  • Scheduling
    • XV6 CPU Scheduling
    • How unix pipes work?
    • How does wait(), exit(), kill() work?
  • File System
    • Overview and Disk Layout
    • Buffer Cache
    • Design Inode Layer
    • Inode Content
    • Block Allocator
    • Design a log system for crash recovery
    • Directory Layer
    • Path names
    • File Descriptor Layer
    • FS System Calls
    • XV6 VS Real World
    • Make Xv6 File disk management system
    • Write FS simulator in python
    • How Redirect Shell command works
  • Concurrency
    • Spinlock
    • How linux select work
    • Hardware Support Locking
    • Exercise: Implement atomic counter
    • Locking in Xv6
    • Concurrency in Xv6
    • Exercise: Socket Programming with Event loop
  • Labs
    • Lab 1 Xv6 and Unix utilities
    • Lab 2 Shell
    • Lab 3 Buddy Allocator
    • Lab 4 Lazy
    • Lab 5 Copy-on-Write Fork for xv6
    • Lab 6 RISC-V assembly
    • Lab 6 Uthread: switching between threads
    • Lab 6 Alarm
    • Lab 7 Lock
    • Lab 8 File System: Large Files
    • Lab 8 File System: Symbolic links
    • Lab 9 mmap
    • Lab 10 Networking Part 1
    • Lab 10 Networking Part 2
  • Hardware, Device, Assembly
    • RISC-V assembly
    • Assembly: Access and Store information in Memory
    • Start xv6 and the first process
    • Why first user process loads another program?
    • What does kernel.ld do in XV6?
    • XV6 Device Driver
Powered by GitBook
On this page
  • Create an Address Space
  • Create a direct-map page table for kernel
  • Turn on Paging
  • Create Process Table
  • Process has its own Page Table.

Was this helpful?

  1. Memory Management
  2. Page Table

Part 2: Create an Address Space

Create an Address Space

Create a direct-map page table for kernel

There is a walk() to find PTE for a given virtual address mappages() installs PTE for new mappings

In kvminit(), first allocate a page of physical memory to hold the root page-table page. Then calls to install a bunch of things that kernel needs, including: instructions, data, physical memory range, and memory range for devices.

The install mapping mappages works as follow: 1. For given virtual address, find the pte 2. Convert physical address to pte and set permission bits 3. Return success or not.

static pte_t * walk(pagetable_t pagetable, uint64 va, int alloc)

  1. Returns the address of the PTE in page table pagetable that corresponds to a virtual address.

  2. Loop from level 2 to level 0

  3. Find the pte entry using level, and va from current page table pageable.

  4. Find page that pte points to.

  5. If pte not exist or page not valid, allocate a new page.

  6. End of loop.

  7. Return the pte entry.

Turn on Paging

Write physical address of the root page-table page into per CPU register satp. After this, CPU will begin translate addr using kernel page table. Flush the TLB to clear cache.

Create Process Table

  1. For process p, allocate 1 pages using buddy allocator for this process’s kernel stack.

  2. Map it high in memory with permission X | W.

  3. Set it to be followed by an invalid guard page. This virtual page does not allocated in physical memory, it just occupies some space in process VM address space. Any access to those will trigger kernel panic.

  4. Reloads the kernel page table into satp so that hardware knows about the new PTEs. (Including flush TLB buffer cache)

/// map kernel stacks beneath the trampoline,/
/// each surrounded by invalid guard pages./
#define KSTACK(p) (TRAMPOLINE - ((p)+1)* 2*PGSIZE)

// Allocate a page for the process's kernel stack./
// Map it high in memory, followed by an invalid/
// guard page
char *pa = kalloc();
if(pa == 0)
  panic(“kalloc”);
uint64 va = KSTACK((int) (p - proc));
kvmmap(va, (uint64)pa, PGSIZE, PTE_R | PTE_W);
p->kstack = va;

Process has its own Page Table.

Different than the kernel one. The root page-table page needs to set in satp register when CPU is currently running this process. Process struct saves kernel root page-table address, so allows to switch to/from kernel.

PreviousPart 1: How to translate addressNextPart 3: How Page Table is used

Last updated 5 years ago

Was this helpful?

Part 3: How Page Table is used