System Calls
Contents
System calls are special function calls to access kernel functionality in a controlled and managed way.
They allow the processor to enter kernel mode and exit back into user mode without the user needing to explicitly implement that functionality. They also prevent access of secure locations
Fetch-Execute Cycle
- Fetch
- Set PC (??? before or after ???)
- Execute
x86 - Privileged-only Instructions
cli
- clear interrupt (disable interrupts)sti
- set interrupt (enable interrupts)
Only executes in kernel mode.
Divide the memory into a kernel-only region, and a shared region.
--
Steps to make a System Call
- Push arguments to the stack
- Set up relevant registers
- Execute syscall call
- Kernel Trap!!!
- We are now in Kernel Space (the OS)
- Dispatcher checks trapframe
- Do whatever needs to be done for the syscall
- Return to caller
- Increment SP
User -> Privileged Mode change
- Processor mode switch
- Stack pointer switch
- Program counter switch
- Registers used to pass data to and from kernel.
- Memory used to pass data to and from kernel.
MIPS R3000
Has a co-processor (CP0) that manages the state of the main processor.
c0_cause
register - cause of the most recent exceptionc0_status
register - current status of the CPUc0_epc
register - address of the instruction that triggered an exception
Accessing CP0 is done through the two kernel mode instructions mtc0
(move to c0), and mfc0
(move from c0).
Important Bits
c0_status
c0_cause
Important Exception Codes
0
- Interrupt8
- Syscall
General Exception Vector is at
0x8000 0800
Returning from an Exception
- Set the PC to the value of the EPC (using the
jr
instruction) - Return from the exception (using the
rfe
instruction)
OS/161 System Calls
Arguments are passed and returned via normal C function calling convention.
Register v0
contains the system call number
- When a syscall returns successfully:
- Register
a3 = 0
on success
- Register
- Register
a3
is non-zero if a failure has occurredv0
contains the error numberv0
is then stored inerrno
v0
set to-1
// TODO: Slides