📂
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

Was this helpful?

  1. Memory Management
  2. xv6 buddy allocator

How to display physical memory

in OS kernel code, a physical address is uint64, but it is often being cast as a void, or char, then carry around. I think this makes a lot of interface like memset, memcpy, etc more convenient to use.

void vmprint_helper(pagetable_t *pagetable*, char* prefix, int level) {
  // there are 2^9 = 512 PTEs in a page table.
  for(int k = 0; k < 512; k++) {
    pte_t pte = pagetable[k];
    if(pte & PTE_V) {
      printf(“%s%d: pte %p pa %p\n”, prefix, k, pte, PTE2PA(pte));
      int next_level = level - 1;
      if (level > 0) {
        uint64 child = PTE2PA(pte);
        pagetable_t pt = (pagetable_t)child;
        vmprint_helper(pt, print_prefix(next_level), next_level);
      }
    }
  }
}

void vmprint(pagetable_t *pagetable*)
{
  printf(“page table %p\n”, pagetable);
  vmprint_helper(pagetable, “..”, 2);
}
page table 0x0000000087f6e000
..0: pte 0x0000000021fda801 pa 0x0000000087f6a000
....0: pte 0x0000000021fda401 pa 0x0000000087f69000
......0: pte 0x0000000021fdac1f pa 0x0000000087f6b000
......1: pte 0x0000000021fda00f pa 0x0000000087f68000
......2: pte 0x0000000021fd9c1f pa 0x0000000087f67000
..255: pte 0x0000000021fdb401 pa 0x0000000087f6d000
....511: pte 0x0000000021fdb001 pa 0x0000000087f6c000
......510: pte 0x0000000021fdd807 pa 0x0000000087f76000
......511: pte 0x000000002000200b pa 0x0000000080008000
init: starting sh
page table 0x0000000087f63000
..0: pte 0x0000000021fd7c01 pa 0x0000000087f5f000
....0: pte 0x0000000021fd7801 pa 0x0000000087f5e000
......0: pte 0x0000000021fd801f pa 0x0000000087f60000
......1: pte 0x0000000021fd741f pa 0x0000000087f5d000
......2: pte 0x0000000021fd700f pa 0x0000000087f5c000
......3: pte 0x0000000021fd6c1f pa 0x0000000087f5b000
..255: pte 0x0000000021fd8801 pa 0x0000000087f62000
....511: pte 0x0000000021fd8401 pa 0x0000000087f61000
......510: pte 0x0000000021fdbc07 pa 0x0000000087f6f000
......511: pte 0x000000002000200b pa 0x0000000080008000

Another reason cast uint64 to void* is easy to print. (see above code about how to print page tables)

Previousxv6 buddy allocatorNextMemory Management Walk Through

Last updated 5 years ago

Was this helpful?