- Written
- Published
- Updated
ALL TESTS PASSED!
Congratulations!
9 labs are all completed and passed all tests with only some help of GPT.
Begin at 2025 Aug 16 and finish at 2025 Oct 20.
Lab1: Utilities (3 days)
Aug 16 - Aug 18.
Lab2: System calls (3 days)
Aug 19 - Aug 21.
Lab3: Page tables (4 days)
Aug 23, Aug 24, Aug 26, Aug 27.
Lab4: Traps (2 days)
Aug 29 - Aug 30.
Lab5: Copy on-write (3 days)
Sep 2 - Sep 4
Lab6: Network driver (5 days)
Sep 21 - Sep 25
Lab7: Lock (3 days)
Oct 11 - Oct 12, Oct 15.
Lab8: File system (3 days)
Oct 13 - Oct 15
Lab9: mmap (3 days)
Oct 15, Oct 19 - Oct 20
Updated at 2025.10.20
Lab: utilities
system call
pipe
int pipe(int * pipefd): Note: pipefd[0] is read end and pipefd[1] is write end.
dup
Duplicate a file descriptor.
Lab: system calls
RISC-V assembly
Registers
RISC-V has 32 general-purpose registers, each 64-bit (RV64) or 32-bit (RV32) depending on the system.
| Register | ABI Name | Usage |
|---|---|---|
x0 | zero | Always 0 |
x1 | ra | Return address |
x2 | sp | Stack pointer |
x5 | t0 | Temporary |
x10 | a0 | Function arg 0 / return value |
x11 | a1 | Function arg 1 / return value 2 |
x12-x17 | a2-a7 | Function args |
x28-x31 | t3-t6 | More temporaries |
Instructions
Arithmetic Instructions
# i stands for immediate
addi a0, zero, 5 # a0 = 0 + 5
addi a1, zero, 10 # a1 = 0 + 10
add a2, a0, a1 # a2 = a0 + a1 = 15
sub a3, a1, a0 # a3 = a1 - a0 = 5
Memory Access
# Assume sp points to stack
addi sp, sp, -16 # Allocate 16 bytes on stack i.e., sp = sp - 16
# s, l, w stands for store, load, word respectively.
sw a0, 0(sp) # Store a0 at memory[sp]
sw a1, 4(sp) # Store a1 at memory[sp+4]
lw t0, 0(sp) # Load memory[sp] into t0
lw t1, 4(sp) # Load memory[sp+4] into t1
# li stands for Load Immediate
li a0, 42 # a0 = 42
# Loads the address of a label or global symbol into a register.
# la rd, symbol
la sp, stack0 # sp = address of stack0
Branching and Control Flow
addi a0, zero, 5
addi a1, zero, 5
# beq stands for branch equal
beq a0, a1, equal # if (a0 == a1) jump to label "equal"
# bne stands for branch not equal
bne a0, a1, noteq # if (a0 != a1) jump
# bne stands for branch less than
blt a0, a1, less # if (a0 < a1) jump
# j is used for an unconditinal jump.
equal:
addi a2, zero, 1 # a2 = 1
j end
noteq:
addi a2, zero, 2 # a2 = 2
j end
less:
addi a2, zero, 3 # a2 = 3
end:
CSR
In RISC-V, CSRs are special-purpose registers inside the CPU.
They control and report on things like:
-
privilege levels (Machine, Supervisor, User)
-
interrupt enable/disable
-
exception causes
-
performance counters
-
etc.
csrr rd, csr # read from a CSR into a general-purpose register.
csrw csr, rs # write a value from a register into a CSR.
csrr a0, mstatus # read mstatus into a0
csrw mstatus, a1 # write value of a1 into mstatus
Commom CSRs:
mstatus: Machine Status Register, holds the machine’s status bits (interrupt enable, privilege level, etc.).satp: Supervisor Address Translation and Protection, This register store the physical page number of page table. OS find current page table according this.sie: Supervisor Interrupt Enable Register.mpec: machine exception program counter, holds the instruction address to which a return from exception will go.sepc: Supervisor Exception Program Counter. When a trap is taken into S-mode, sepc is written with the virtual address of the instruction that was interrupted or that encountered the exception.stvec: Supervisor Trap Vector Base Address Register. All traps into supervisor mode cause the pc to be set to the address in the BASE field.
Sytem call Process (Rough understanding)
When call system call like exec(argc, argv); in user program, program will jump to a global symbol .global exec in usys.S which is generated by usys.pl.
.global exec
exec:
li a7, SYS_exec
ecall
ret
This is assemble implementation of such system call function.
It stores a number SYS_exec to register a7. SYS_exec is a Micro definition in syscall.h.
Then ecall traps into S-mode. Hardware
-
Sets the
sepcCSR (Supervisor Exception Program Counter) to the address of the trapped instruction (the instruction that caused the trap). -
Sets
scauseto indicate the trap cause. -
Optionally sets
stvalfor extra info (like a faulting virtual address). -
Switches privilege to S-mode and jumps to the trap vector set in
stvec.
When trap into S-mode happens, pc will to set to stvec which is uservec in trampoline.S.
It will store current status into p->trapframe memory area which is owned by each process. And then jump to usertrap(void) in trap.c
Now after some if, usertrap(void) will call syscall(void) in syscall.c.
That is where really implementation of system call function in.
See source code for more details.
See lab: traps for deep understanding.
Lab: page tables
Kernel address space
See xv6 book at page 34 for kernel memory layout.
- First call
kinit()inmain.c. It callfreerange()to collect a free physical memory fromendtoPHYSTOP. It's using a linked list to manage free space. - Then call
kvminit()inmain.c. It create a kernel page table and set virtual memory mapping. kvminithart()set CSRsatpto such kernel page table and clean up TLB. Now page mechanism turns on.
Process address space
See xv6 book at page 39 for user address space layout.
Each process has its own page table, and when xv6 switches between processes, it also changes page tables (and must clean up TLB).
The stack is a single page.
A process’s user memory starts at virtual address zero and can grow up to MAXVA (kernel/riscv.h:375).
ELF file
Using objdump we can check the file type and its details.
e.g.:
saber@Saber:~/xv6-labs-2024$ objdump -p user/_init
user/_init: file format elf64-little
Program Header:
0x70000003 off 0x00000000000075ac vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**0
filesz 0x0000000000000053 memsz 0x0000000000000000 flags r--
LOAD off 0x0000000000001000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**12
filesz 0x0000000000000d74 memsz 0x0000000000000d74 flags r-x
LOAD off 0x0000000000002000 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**12
filesz 0x0000000000000010 memsz 0x0000000000000030 flags rw-
STACK off 0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**4
filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-
Program Header: 0x70000003 off 0x00000000000075ac vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**0 filesz 0x0000000000000053 memsz 0x0000000000000000 flags r--
This is a special header (type 0x70000003, which is OS/ABI specific, not a normal LOAD).
- It starts at file offset
0x75ac. - It doesn’t actually get mapped into memory (
memsz = 0). - Just contains metadata, like ELF interpreter info or note sections.
LOAD off 0x0000000000001000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**12 filesz 0x0000000000000d74 memsz 0x0000000000000d74 flags r-x
- Type:
LOAD→ means loader must map this part of the file into memory. - off 0x1000: in the ELF file, code starts at offset 0x1000.
- vaddr 0x0: mapped into user virtual address 0x0 (first page of process).
- filesz = memsz = 0x0d74 (≈ 3444 bytes) → so this is the program code (.text).
- flags r-x → Read + eXecute (but not write).
So this is the text segment since it is executable.
LOAD off 0x0000000000002000 vaddr 0x0000000000001000 paddr 0x0000000000001000 align 2**12 filesz 0x0000000000000010 memsz 0x0000000000000030 flags rw-
- Another
LOADsegment. - off 0x2000 → comes from file offset 0x2000.
- vaddr 0x1000 → mapped into virtual memory at 0x1000 (right after text).
- filesz = 0x10 (16 bytes) → this is
.data. - memsz = 0x30 (48 bytes) → this is
.data + .bss(uninitialized globals). - flags rw- → Read + Write (not executable).
So this is the data segment.
NOTE:
A program section header’s filesz may be less than the memsz, indicating that the gap between them should be filled with zeroes (for C global variables) rather than read from the file (.bss).
For /init, the data filesz is 0x10 bytes and memsz is 0x30 bytes, and thus uvmalloc allocates enough physical memory to hold 0x30 bytes, but reads only 0x10 bytes from the file /init.
STACK off 0x0000000000000000 vaddr 0x0000000000000000 ... filesz 0x0 memsz 0x0 flags rw-
This is just an ELF hint (for some OSes) saying "create a stack".
- xv6 ignores this and instead allocates its own stack when
exec()runs.
paddr tells the loader where in physical memory to put this segment. This will be ignored since virtual memory is enabled.;
align 2**12: That means alignment = 4096 bytes = 1 page.
exec() (Awesome !!)
See xv6-book at page 40 for more details.
This is really awesome! Congratulations!!
lab: Inspect a user-process page table
va 0x0 pte 0x21FC905B pa 0x87F24000 perm 0x5B
va 0x1000 pte 0x21FD141B pa 0x87F45000 perm 0x1B
va 0x2000 pte 0x21FD1017 pa 0x87F44000 perm 0x17
va 0x3000 pte 0x21FD4407 pa 0x87F51000 perm 0x7
va 0x4000 pte 0x21FC80D7 pa 0x87F20000 perm 0xD7
va 0x5000 pte 0x0 pa 0x0 perm 0x0
va 0x6000 pte 0x0 pa 0x0 perm 0x0
va 0x7000 pte 0x0 pa 0x0 perm 0x0
va 0x8000 pte 0x0 pa 0x0 perm 0x0
va 0x9000 pte 0x0 pa 0x0 perm 0x0
va 0xFFFF6000 pte 0x0 pa 0x0 perm 0x0
va 0xFFFF7000 pte 0x0 pa 0x0 perm 0x0
va 0xFFFF8000 pte 0x0 pa 0x0 perm 0x0
va 0xFFFF9000 pte 0x0 pa 0x0 perm 0x0
va 0xFFFFA000 pte 0x0 pa 0x0 perm 0x0
va 0xFFFFB000 pte 0x0 pa 0x0 perm 0x0
va 0xFFFFC000 pte 0x0 pa 0x0 perm 0x0
va 0xFFFFD000 pte 0x0 pa 0x0 perm 0x0
va 0xFFFFE000 pte 0x21FD48C7 pa 0x87F52000 perm 0xC7
va 0xFFFFF000 pte 0x2000184B pa 0x80006000 perm 0x4B
print_pgtbl: OK
First two pages are code setion.
Third page is data setion.
Fourth page is used for stack guard page.
Fifth page is used for stack.
The the second to last page is TRAPFRAME code.
Last page is TRAMPOLINE code.
Sv39
Sv39: Page-Based 39-bit Virtual-Memory System.
Comments
No approved comments yet.
Your display name and plain-text comment become public only after owner approval. No account or email is required.