Skip to content

Instantly share code, notes, and snippets.

@aagontuk
Last active November 2, 2021 21:03
Show Gist options
  • Save aagontuk/cd59228f110d8cb5722aa41e90d7f3a3 to your computer and use it in GitHub Desktop.
Save aagontuk/cd59228f110d8cb5722aa41e90d7f3a3 to your computer and use it in GitHub Desktop.
xv6-riscv notes

How system calls in xv6 works

  • user.h
  • syscalls.h
  • syscall.c
  • usys.pl

How trap works

Trap generates from system calls, device interrupts and illegal instructions.

  • stvec points to uservec.
  • uservec mapped in same address in user and kernel so that kernel can continue running insruction after switch.
  • uservec switches satp to point to kernel page table.
  • kernel set sscratch to point to user trapframe.
  • uservec swaps a0 with sscratch. Now a0 contains the trapframe.
  • uservec saves all the user registers in the trapframe including a0 which is saved from sscratch.
  • trapframe holds kernel page table, pointer to process's kernel stack, current cpu's heart id and pointer to usertrap.
  • uservec retrieve these values and set setp to kernel page table and then call usertrap.
  • usertrap determines the cause of trap, process it and return.
  • usertrap set stvec to kernelvec, saves sepc again as there might be a process switch in usertrap(?), calls syscall/devintr/kill the process (faulty)
  • To return to userspace usertrapret is called.

Interrupts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment