Lecture 05
Lecture 05
✤ Lab 2 (xv6) is currently out; due Monday before the next lab
✤ Small Group meetings for Lab 1 (mex) today and tomorrow (Knuth)
✤ No of ce hours on Friday.
2
fi
Kernel Structure
4
fi
fi
fi
Privilege Modes
8
Booting and First Process
✤ Boot code:
✤ Bootloader is stored in ROM. Loads kernel into memory at 0x8000 0000 (devices below)
✤ Control transferred to kernel (at _entry), in machine mode.
✤ Kernel’s _entry:
✤ Sets up a simple 4K stack (one per hardware thread or hart), jumps to C code, start.
✤ This routine sets up initial page tables, initializes timer interrupts.
✤ Sets registers to appear as though there had been a supervisor->machine mode call
✤ “Returns” to main.
✤ Kernel’s main:
✤ Initializes devices (eg. console)
✤ From userinit routine, creates the initial process, init, with pid 1.
✤ Initial process (userland!)
✤ Calls exec (reentering kernel) and runs /init
✤ This program creates the console (if necessary) and establishes descriptors 0, 1, and 2; forks sh.
9
The RISC-V Machine
11
Instruction Set Overview
✤ To call a routine:
✤ Put arguments in a0 through a7 (rest on stack, in reverse order).
✤ Call routine:
✤ PC+4 saved in ra.
✤ jump to routine.
✤ The call instruction is a shorthand macro for common case.
✤ Routine does its work.
✤ Result found in a0 (and possibly a1).
13
Calling Convention* — Callee
✤ On entry:
✤ sp points to top of stack
✤ fp points to base of the caller frame
✤ Typical exit protocol:
✤ ra is the return address
✤ Store return value in a0.
✤ a0..a7 contain arguments
✤ Restore old fp.
✤ Typical entry protocol (effectively). Stack is 8-byte aligned. ✤ Restore sp to entry level
✤ The current stack pointer will become the next frame
✤ return, using ra
pointer.
✤ Push on the return address.
✤ On exit:
✤ a0 is result
✤ Push on the old frame pointer
✤ Other a-regs garbage
✤ Push on saved registers
✤ Push on locals.
✤ Actual entry protocol is tricky.
14