Week 5 CS4973/CS6640 02/03 2025 https://naizhengtan.github.io/25spring/ □ 1. OS implementation: the first three steps □ 2. OS organization □ 3. egos desgin □ 4. egos-2k+ (sifive_e) implementation --- Discussion: - Too many printf() / INFO() can cause issue? (Shujun) OS organization: part I: OS implementation, the first three steps part II: design and implementation of egos-2k+ Expectation: after this, you should have a bird view of egos Why we have this now, instead of the first class? - an analogy of OS organization vs. Software architect - for software engineering, one studies how to program, then algorithms and data structures, then design patterns and software engineering (as in management), then software architecture => low-code programmers, programmers, software engineers, software architect - where OS education is the other way around: OS users, [?] OS architect 1. OSI: the first three steps [play slides] a) Question: Where (in memory) is the first kernel instruction executed by CPU? -- use gdb -- see CPU manual -- (read linker script) b) Question: How does printf() work? use gdb: -- (gdb) b grass.c:main -- go with code until to vprintf -- (gdb) b uart_putc [see printing a letter at a time] c) Question: How does the handler function switch to the context to a different process? -- use "mepc" and "mret", instead of "pc" and "ret" [pingpong example] d) Question: There are three ways to trap to kernel. What are they? -- interrupts -- exceptions -- syscalls 2. OS organization - types of OSes [see fig] * monolithic OS * microkernel OS: 1970s * exokernel (LibOS): 1995 * multikernel: 2009 - more about these architectures * monolithic kernel: a single piece of code serving all requests high in coupling * microkernel: kerenl is mostly responsible for IPCs; services are running in user-level low in coupling IPC can be the bottleneck * exokernel kernel only handles multiplexing resources (securely) extra performance due to having hardware primitives * multikernel target heterogeneous hardware and many-core machines replace shared-memory model with shared-nothing model (use message passing) treat OS as a distributed system - [story] an anecdote of today's microkernel 3. egos design * earth, grass, and apps [see handout, panel 1] * earth interface and syscalls [read library/egos.h] * three root syscalls: sys_send, sys_recv, and sys_yield (grass/syscall.h) * system services -- sys_proc (GPID_PROC) -- sys_file (GPID_FILE) -- sys_dir (GPID_DIR) -- sys_shell (GPID_SHELL) They have well-known pids, from 1 to 4. 4. egos-2k+ implementation overview The botting process: [see handout, panel 2] Q: why a process died already? CPU jmps to 0x20400000 | +-> earth.S:_enter | +-> earth.c:main | +-> grass.S:_enter | +-> grass.c:main | +-> app.S:_enter | +-> sys_proc.c:main | ... | +-> sys_shell.c:main [skipped] * in earth.S Q: what does "csrrc t1, mstatus, t0" suppose to mean? (1) read "csrrc" (2) read "mstatus" Q: "call main"; there are multiple main(), how does the CPU know which main it jumps to? [Answer: mains are in different binary files (.elf)] * in earth.c Q: Why not directly jump to GRASS_ENTRY? Does it work? [Answer: it works, but the privilege level will be different] Q: when jumping to GRASS_ENTRY, there is garbage on stack. Will the garbage be there forever? [Answer: no, grass resets the stack pointer] * Use gdb to see the transition from "mret" to "call main" * in grass.c Q: where does sys_proc_entry() jump to? [Answer: app.S:_enter and then sys_proc.c:main] * in sys_shell.c Q: why a process died? [Answer: that's "cd"]