5. Interrupt and exception - System calls - Why do we need the system call? - Allow the kernel to expose certain key pieces of functionality to user programs - What kinds of work that the system call can do? - Access the file system (open(), write(), read()) - Destroy processes (kill()) - Allocate more memory - How does the system call issue a signal to the kernel space? - Trap - System call table - e.g. fwrite("hello", 5, 1, f) -> test.c write() -> glibc sys_write() -> kernel space - A system call table contains and the system call number and types e.g. 0 sys_read, 1 sys_write ... - What are the orders of a system call ? - Program puts syscall parameters in registers - Program executes a trap - Processor state (PC, PSW (program status word)) pushed on stack - PSW is used to describe the condition of a processor at each instant - e.g. use to switch from the user to the kernel space - CPU switches to kernel mode - Trap handler uses parameters to jump to desired handler - When a system call completes its work, reserve operation - place return code in register - return from exception - See system call slide 10 - System call number - used to distinguish between system calls - See syscall.h - A prototype of a typical system call - int system_call (resource_descriptor, parameters) - resource_descriptor: file, device ..., means the current process if not specified - return value indicates the completion status of the system call - sometimes return value means the number of bytes written to file - How do pass parameters of the system call to the kernel space ? - Passing parameters in system calls - Typical method - pass by registers (Linux) - % eax (sys call number), %ebx, %ecx, %esi, %edi, %ebp - e.g. mov x, %eax INT 64 - pass via user mode stack (xv6) - use user stack and trapframe, see slide 18 - pass via a designated memory region - Trap instruction - jumps into the kernel and raises the privilege level to kernel mode - use return-from-trap instruction to return the user space when finished - How does the trap know which code to run inside the OS? - Using the trap table - When does the OS kernel create the trap table? - at boot time - When will we use trap? - invalid memory access - float point exception ... - wait() - delay the parent's process until the child finishes executing - How to implement wait() in the OS kernel? - see slide 32 - exit() - exit all other process except "init", the first process - see slide 34 - Interrupt - An event that alters the sequence of instructions executed by a processor - Raised by hardware or programs to get OS attention - Hardware interrupt: - a device (programmable interrupt controller (PIC)) asserts a pin in the CPU - When will we use hardware interrupt ? - the I/O device signals to the CPU that it wants to be serviced - .e.g tell CPU that a keypad has been pressed - 2 pins on the CPU - INT (interrupt) - maskable interrupt - NMI - non maskable interrupt for very critical signal - Software interrupt: INT x, an executed instruction causes an interrupt - Synchronous interrupt - The control unit issues interrupts only after terminating the execution of an instruction - Asynchronous interrupt - Generated by hardware devices at arbitrary time - What happens when there is an interript? - Basic program state saved - save SS, ESP, EFLAGS, CS, EIP .. - CPU suspends current task - Jump to interrupt handler - Interrupt handler (top half) -- software - Respond to interrupt - storing more program state - schedule bottom half - IRET (interrupt return) - Return from interrupt -- CPU - Restore flags and registers saved eariler - Restore running task - Interrupt handler (bottom half) -- software - Interrupt vectors - Each interrupt/exception provides a number - The index of an interrupt descriptor table (IDT) - IDT provides the entry point into an interrupt/exception handler - 0 to 255 vectors possible - 0 - 31: exception and nonmaskable interrupts - 32- 47: maskable interrupts caused by IRQs - 48 - 255: identify software interrupt - e.g. Linux uses 128 (0x80) vector to implement system calls - interrupt descriptor table (IDT) - store in memory - initialized by OS at boot time - small interrupt handlers - Top and bottom half technique (Linux) - Top half - Do minimum work and return from interrupt handler - Bottom half - deferred processing - implemented in Linux as softirqs, tasklets or workqueues - Exceptions - Due to illegal operations - Generated when the CPU detects an anomalous condition while executing an instruction - Exception sources - program-error exceptions - divide by zero - Software generation exceptions - INT 3: a break point exception - INT 0: overflow instruction - Machine-check exceptions - Hardware error such as system bus error