7. Exceptions and System Calls - University College...
Transcript of 7. Exceptions and System Calls - University College...
1
7. Exceptions andSystem Calls
Mark Handley
CPU modes
Recall the mode bit in the CPU PSW register:user-mode: only a subset of instructions and features are
accessible.kernel-mode: all instructions and features are accessible.
How does a user process ask the OS to perform a function for it? It is running in user-mode, so cannot directly access kernel
memory or the hardware. If it could just directly switch to kernel-mode, there would
be no security.
2
Exceptions
What happens when a user process does somethingillegal?Divide-by-zero.Runs an illegal instruction.
How does the Java Virtual Machine manage to throwan floating exception, rather than simply crash?
Example: Illegal Instruction
The “halt” instruction is illegal in user-mode.(in kernel mode, it halts the system).
If a user-mode process calls halt, this generates atrap in the CPU, which suspends the user process,switches into kernel-mode, and calls a correspondingexception handler.What happens next depends on the OS.
3
Process Address Spaces
Process 1User Area
Process 2User Area
Process 3User Area
Shared Kernel Memory
0x00000000
0xBFFFFFFF0xC0000000
0xFFFFFFFF
VirtualMemory
AddressesProcess 1User Area
Process 2User Area
Shared Kernel Memory
Kernel Stack and Kernel Memory
User Mode
kernel stack user stack
SP
Kernel Mode
kernel stack user stack
USPSP
Kernel memory is only accessible in kernel mode. User memory is mapped by the MMU, but kernel memory is
generally shared (unmapped) by all processes. There is a separate kernel stack
4
Kernel Exception Handler
When the OS starts, it fills in the Exception VectorTable.This contains pointers to code to run when a
particular exception occurs. Interrupts are handled through the same table.
CPU Exception Handling
Save PCand PSW
Exception Vectors Exception Handlers
code to handle exception A
code to handle exception B
code to handle exception C
CPU switchesto kernel-mode
Load PC fromException Vector
Use PC to fetch next instruction
5
Handling a Trap
Trap occurs. CPU switches to kernel mode. CPU saves information about the current running program
(PSW, PC, etc) on the kernel stack. CPU calls OS exception handler for this exception from the
exception vector table. OS handles exception.
Unix: look up exception handler in signal table. Win32: call a dispatcher with relevant interrupt object. Call relevant signal handler code.
In some cases, terminates the process.In some cases, returns control to user process.
If not fatal, calls “return from exception” CPU restores PSW and PC. Switches back to user-mode.
User Exception HandlingOS Exception HandlerUser ProgramExecution by
user process
Exceptionoccurs
Exceptionhandler canpass controlback to wherethe exceptionoccurred
Exceptionhandler called
Control passed touser program’sexception handler
6
System Calls
A system call is the mechanism by which a user process calls akernel procedure. Used to do I/O, read/write files, etc.
Basic mechanism is very similar to exception handling: Generate a trap. Kernel exception handler called. OS identifies which system call is required, and calls
relevant procedure. Returns execution to user process on completion.
System Calls:
Generating a Trap TRAP instruction (68000)
4-bit opcode: TRAP #0 to TRAP #16
INT (interrupt) instruction (Pentium, etc)8-bit opcode allows 256 different exceptions to be
identified.Linux: INT 0x80 is a system call.Windows: INT 0x2E is a system call.
7
Processing a System Call
User program Kernel
libc API library
Validate Parameters
Find and call correct procedure
Return result
System call table
System callimplementation
libc API call
Set up parameters
System call
Return result
Example System Call
Call from C program:count = read(fd, &buffer, length); read up to length bytes of data from from file
descriptor fd into buffer, and returns the number ofbytes actually read, or -1 if an error occurred.
C library call for read() calls the read system call.
8
Steps in Making a System Callread (fd, &buffer, nbytes)
User program Kernel
libc API library
Save state
Validate syscall code
Call call_table_base+code
Validate read parameters from user stack
Do read function
Return
Push nbytes;Push &buffer;
Push fdCall read function
Put code for readsyscall in register;
Trap to kernel (int 0x80)
Increment SP
Return to caller
Return result
System call table
System Call sanity checking
Kernel does not trust user processes. Needs to validate any parameters from system call
very carefully.Eg: buffers for I/O must be in user memory.Pentium has special instuctions:
VERR - verify read instructionVERW - verify write instruction
9
System Calls For Process Management
System Calls For File Management
10
System Calls For Directory Management
System Calls For Miscellaneous Tasks
11
Unix vs Win32 System calls
Summary
System calls are how a user process gains access tofunctions provided by the OS.
Implemented via trap to kernel. Much in common with exception/interrupt handling. Different systems have different system calls.