CS 452/652 Fall 2019 - Lecture 6
September 16, 2019
prev next
Context Switch (cont'd)
- "execution state" = context
- started consideration with subroutine call, voluntary stack switch
- here: system call and interrupt handling
- every kernel invocation reschedules ⇒ preemption
Mode Switch
- asymmetric: enter_kernel() vs. leave_kernel()
- direct stack switch: user → kernel and kernel → user
- indirect switch (through kernel): task A ↔ task B
- multiple variants of enter_kernel() for system call, interrupt...
ARM Instructions
- mov - copy register values
- str/ldr - store/load registers ↔ memory
- stm/ldm - store/load multiple registers
- special case: access user registers from exception modes
- etc...
- RISC architecture: instructions + operands encoded in 32 bits
SWI Instruction
- user call into kernel ("software interrupt"); implements system calls
swi N
- hard-coded branch to 0x08
- after-reset contents of 0x08: ldr pc, [pc, #0x18]
- branches to value from pc-relative memory location
- evaluating pc in ARM's pipeline results in pc+8
- i.e., branch to destination stored in 0x28
- set up SWI: change instruction at 0x08 or branch destination at 0x28
- branch to your hard-coded enter_kernel() handler
After SWI
- control at your hard-coded enter_kernel() handler
- processor in supervisor mode
- user sp, lr saved in respective banks
- user cpsr saved in spsr
- sp set to dedicated stack pointer
- lr holds user return address (location following swi instruction)
- must be saved (as part of user context) for later return to user task
- choices for getting user context:
- enter system mode, or
- use special version of stm instruction
- choices for storing user context:
- on user stack, or
- in task descriptor
- then restore kernel context
Kernel Exit
- store kernel context (similar to voluntary stack switch)
- restore given user context (next task to be run)
- return to user mode, for example
movs pc, lr
- variant of mov that changes processor mode
System Call Processing
- these are suggestions - other implementations are possible...
- SWI argument N can be retrieved as follows
- kernel needs access to system call parameters
- leave first four arguments in r0-r3: save during context switch
- additional arguments on user stack; see ABI
Outlook: Hardware Interrupt
- can happen any time (anywhere in code)
- need to save all user registers
- also need to save spsr (interrupt between arithmetic operation conditional branch)
- processor in irq: banked version of sp different from supervisor mode
Task Management
Additional Information
An earlier document by Bill Cowan is available here.
This is certainly not the only way to write a context switch and I do not necessarily recommend (or not recommend) this particular approach, but I figure every bit of information can help.