xv6, line by line
kernel/memlayout.h

kernel/memlayout.h

C header · 63 lines · annotated 100% · kernel · upstream

Included by 19 fileskernel/console.c, kernel/exec.c, kernel/kalloc.c, kernel/main.c, kernel/plic.c, kernel/printk.c, kernel/proc.c, kernel/sleeplock.c, kernel/spinlock.c, kernel/start.c, kernel/syscall.c, kernel/sysproc.c, kernel/trampoline.S, kernel/trap.c, kernel/uart.c, kernel/virtio_disk.c, kernel/vm.c, user/grind.c, user/usertests.c
About this file

The map of the machine. This header names the physical addresses where QEMU’s virt machine puts its devices and RAM, and the virtual addresses where xv6 places the pages that every address space shares. kvmmake reads it to build the kernel’s page table, device drivers read it to find their registers (memory-mapped I/O (MMIO)), and kernel/trampoline.S reads it to find the trapframe.

Physical memory (what the bus sees, on QEMU with -m 128M):

0x88000000  PHYSTOP   end of the RAM xv6 uses
            ...       free pages, handed out by kalloc()
            end       end of the kernel image (from kernel.ld)
            ...       kernel data and bss
            etext     end of the kernel code (page-aligned)
            ...       kernel code; its last page is the trampoline
0x80000000  KERNBASE  start of RAM; entry.S is the first byte
            ...       (nothing xv6 uses)
0x10001000  VIRTIO0   virtio disk registers
0x10000000  UART0     serial-port registers
0x0c000000  PLIC      interrupt controller
0x02000000  CLINT     core-local interruptor (not used by this xv6)
0x00001000            QEMU's boot ROM

Kernel virtual memory (kernel_pagetable). Below PHYSTOP every mapping is the direct map: virtual address = physical address. Above it, only the top of the 39-bit space is used:

0x4000000000  MAXVA       (one past the highest usable address)
0x3ffffff000  TRAMPOLINE  trampoline code             R X  -> physical page at trampoline
0x3fffffe000              unmapped (guard)
0x3fffffd000  KSTACK(0)   kernel stack of proc[0]     R W
0x3fffffc000              unmapped (guard)
0x3fffffb000  KSTACK(1)   kernel stack of proc[1]     R W
              ...         64 stacks in all (NPROC), each with a guard page below it
0x3ffff7f000  KSTACK(63)
              ...         unmapped
0x88000000    PHYSTOP
                          free RAM                    R W   (direct map)
etext                     kernel data, bss            R W   (direct map)
0x80000000    KERNBASE    kernel code                 R X   (direct map)
0x10001000    VIRTIO0     one page                    R W   (direct map)
0x10000000    UART0       one page                    R W   (direct map)
0x0c000000    PLIC        64 MiB                      R W   (direct map)

The user memory layout of each process is at the end of this file.

Read before: kernel/kernel.ld. Read next: kernel/vm.c, which builds these mappings.

1// Physical memory layout
3// qemu -machine virt is set up like this,
4// based on qemu's hw/riscv/virt.c:
5//
6// 00001000 -- boot ROM, provided by qemu
7// 02000000 -- CLINT
8// 0C000000 -- PLIC
9// 10000000 -- uart0
10// 10001000 -- virtio disk
11// 80000000 -- qemu's boot ROM loads the kernel here,
12// then jumps here.
13// unused RAM after 80000000.
15// the kernel uses physical memory thus:
16// 80000000 -- entry.S, then kernel text and data
17// end -- start of kernel page allocation area
18// PHYSTOP -- end RAM used by the kernel
20// qemu puts UART registers here in physical memory.
21#define UART0 0x10000000L
22#define UART0_IRQ 10
24// virtio mmio interface
25#define VIRTIO0 0x10001000
26#define VIRTIO0_IRQ 1
28// core-local interrupt controller (CLINT)
29#define CLINT_BASE 0x02000000L
30#define CLINT(hart) (CLINT_BASE + (hart) * 4)
32// qemu puts platform-level interrupt controller (PLIC) here.
33#define PLIC 0x0c000000L
34#define PLIC_PRIORITY (PLIC + 0x0)
35#define PLIC_PENDING (PLIC + 0x1000)
36#define PLIC_SENABLE(hart) (PLIC + 0x2080 + (hart) * 0x100)
37#define PLIC_SPRIORITY(hart) (PLIC + 0x201000 + (hart) * 0x2000)
38#define PLIC_SCLAIM(hart) (PLIC + 0x201004 + (hart) * 0x2000)
40// the kernel expects there to be RAM
41// for use by the kernel and user pages
42// from physical address 0x80000000 to PHYSTOP.
43#define KERNBASE 0x80000000L
44#define PHYSTOP (KERNBASE + 128 * 1024 * 1024)
46// map the trampoline page to the highest address,
47// in both user and kernel space.
50// map kernel stacks beneath the trampoline,
51// each surrounded by invalid guard pages.
52#define KSTACK(p) (TRAMPOLINE - ((p) + 1) * 2 * PGSIZE)
54// User memory layout.
55// Address zero first:
56// text
57// original data and bss
58// fixed-size stack
59// expandable heap
60// ...
61// TRAPFRAME (p->trapframe, used by the trampoline)
62// TRAMPOLINE (the same page as in the kernel)