Week 5.b CS6640 02/05 2026 https://naizhengtan.github.io/26spring/ □ 1. kernel ~= three handlers □ 2. egos syscall implementation --- 0. last time Q: What are the two primary functions of an operating system? A: 1. managing resources 2. providing abstraction Last time, about "CPU": * timer interrupt + scheduling -> managing resources, CPU * process -> an abstraction for execution unit Other two major resources: * memory => virtual memory * disk => files Virtual memory => linear address space Q: What to do when accessing a piece of memory that is not yet allocated? A: exception, page fault File, file systems Q: How to read/write from/to a file? A: system calls 1. kernel ~= three handlers Q: what are the three ways to trap to kernel * kernel ~= 3 handlers -- interrupt handlers respond to asynchronous events from hardware (e.g., timer, I/O). -- exception handlers handle synchronous faults triggered by the current instruction (e.g., page fault). -- syscall handlers handle controlled entry from user space to request kernel services. a) interrupts We've learned this from last time Q: How does a CPU know what to execute when an interrupt is triggered? A: mtvec Q: How does the kernel know which interrupt has been triggered? A: mcause Q: After handling interrupts, where does the CPU jump to? A: [see handout, grass/kernel.s] [check RISC-V instruciton list for "mret"] [check RISC-V manual for "mepc"] depend on mepc Q: how it works? [draw a cpu + memory] - cpu running a proc - timer interrupt (when? mtime > mtimecmp) - CPU jump to the handler (where? mtvec) - switch to the kernel space - execute interrupt handler - return (where? mepc) b) exceptions CPU: "I don't know how to process". Show trap reason table [see handout] Q: ask students which exception the examples will trigger Examples: * read/write invalid memory * run invalid instructions * run privileged instructions in unprivileged mode These are synchronized traps. Q: how the exception works on RISC-V CPUs? Similar to interrupts. Again, mtvec, mcause, mepc, Q: if the same, how to tell exceptions from interrupts? Check the first bit of mcause. c) syscalls Interfaces for user applications to "talk" to kernel. Multiple design questions for kernel developers (you): Q1: How does an application trap into the kernel? // Kernel has a function proc_yield(). // A user app which is another program want to call proc_yield(). // How to do this? Q2: How does the kernel determine the operation requested by the application? That is, what information is required to handle a system call? // Syscall type // Syscall arguments // Syscall return value Q3: Where is this information stored? // registers? // stacks? // well-known memory places? 2. egos syscall implementation Overview, take a look at the workflow. [see handout] a) answers to Q1: trap() so far, MISP, using interrupt Example: mip.MISP (in RISC-V spec) (gdb) display/t $mip (gdb) b syscall.c:57 Q: why we have a loop here? Q: can we do better? A: yes! using exception. b) answers to Q2: [read library/syscall.h] struct syscall c) answers to Q3: [read library/syscall.c] SYSCALL_ARGS (a well-known memory address)