CS 153 Design of Operating Systems Spring 18 Lectre 3: OS model and Architectral Spport Instrctor: Chengy Song Slide contribtions from Nael Ab-Ghazaleh, Harsha Madhyvasta and Zhiyn Qian
Last time/today Historic evoltion of Operating Systems (and compting!) Today: We start or jorney in exploring Operating Systems Try to answer qestions sch as:» How/When does the OS rn?» How do programs interact with it?» How is this spported by hardware? 2
Some qestions to get yo thinking What is the OS? Software? Is the OS always execting? If not, how do we make sre it gets to rn? How do we prevent ser programs from directly maniplating hardware? 3
Sleeping Beaty Model Answer: Sleeping beaty model Technically known as controlled direct exection OS rns in response to events ; we spport the switch in hardware Only the OS can maniplate hardware or critical system state Most of the time the OS is sleeping Good! Less overhead Good! Applications are rnning directly on the hardware 4
What do we need from the architectre/cpu? Maniplating privileged machine state Protected instrctions Maniplate device registers, TLB entries, etc. Controlling access Generating and handling events Interrpts, exceptions, system calls, etc. Respond to external events CPU reqires software intervention to handle falt or trap Other stff Mechanisms to handle concrrency, isolation, virtalization 5
Types of Arch Spport Maniplating privileged machine state Protected instrctions Maniplate device registers, TLB entries, etc. Controlling access Generating and handling events Interrpts, exceptions, system calls, etc. Respond to external events CPU reqires software intervention to handle falt or trap Other stff Interrpts, atomic instrctions, isolation 6
Protected Instrctions OS mst have exclsive access to hardware and critical data strctres Only the operating system can Directly access I/O devices (disks, printers, etc.)» Secrity, fairness (why?) Maniplate memory management state» Page table pointers, page protection, TLB management, etc. Maniplate protected control registers» Kernel mode, interrpt level Halt instrction (why?) 7
Privilege mode Hardware restricts privileged instrctions to OS Q: How does the HW know if the exected program is OS? HW mst spport (at least) two exection modes: OS (kernel) mode and ser mode Mode kept in a stats bit in a protected control register User programs execte in ser mode OS exectes in kernel mode (OS == kernel ) CPU checks mode bit when protected instrction exectes Attempts to execte in ser mode trap to OS 8
Switching back and forth Going from higher privilege to lower privilege Easy: can directly modify the mode register to drop privilege Bt how do we escalate privilege? Special instrctions to change mode» System calls (int 0x80, syscall, svc)» Saves context and invokes designated handler Yo jmp to the privileged code; yo cannot execte yor own» OS checks yor syscall reqest and honors it only if safe Or, some kind of event happens in the system 9
Types of Arch Spport Maniplating privileged machine state Protected instrctions Maniplate device registers, TLB entries, etc. Controlling access Generating and handling events Interrpts, exceptions, system calls, etc. Respond to external events CPU reqires software intervention to handle falt or trap Other stff 10
Review: Compter Organization Branch Address +4 Select PC New PC Program Conter CPU Instrctions Fetch Exec opcode 11
Events An event is an nnatral change in control flow Events immediately stop crrent exection Changes mode, context (machine state), or both The kernel defines a handler for each event type Event handlers always execte in kernel mode The specific types of events are defined by the machine Once the system is booted, OS is one big event handler all entry to the kernel occrs as the reslt of an event 12
Handling events Interrpt vector table Processor Register Interrpt Vector...... handletimerinterrpt() {... } handledividebyzero() {... } handlesystemcall() {... } 13
Categorizing Events Two kinds of events: synchronos and asynchronos Sync events are cased by execting instrctions Example? Async events are cased by an external event Example? Asynchronos events CPU ticks Synchronos events 14
Categorizing Events Two kinds of events: synchronos and asynchronos Sync events are cased by execting instrctions Async events are cased by an external event Two reasons for events: nexpected and deliberate Unexpected events are, well, nexpected Example? Deliberate events are schedled by OS or application Why wold this be sefl? 15
Categorizing Events This gives s a convenient table: Unexpected Deliberate Synchronos falt syscall trap Asynchronos interrpt signal Terms may be slightly different by OS and architectre E.g., POSIX signals, asynch system traps, async or deferred procedre calls 16
Falts Hardware detects and reports exceptional conditions Page falt, memory access violation (naligned, permission, not mapped, bonds ), illegal instrction, divide by zero Upon exception, hardware falts (verb) Mst save state (PC, regs, mode, etc.) so that the falting process can be restarted Invokes registered handler 17
Handling Falts Some falts are handled by fixing the exceptional condition and retrning to the falting context Page falts case the OS to place the missing page into memory Falt handler resets PC of falting context to re-execte instrction that cased the page falt 18
Handling Falts The kernel may handle nrecoverable falts by killing the ser process Program falt with no registered handler Halt process, write process state to file, destroy process In Unix, the defalt action for many signals (e.g., SIGSEGV) What abot falts in the kernel? Dereference NULL, divide by zero, ndefined instrction These falts considered fatal, operating system crashes Unix panic, Windows Ble screen of death» Kernel is halted, state dmped to a core file, machine locked p 19
Categorizing Events Unexpected Deliberate Synchronos falt syscall trap Asynchronos interrpt signal 20
System Calls For a ser program to do something privileged (e.g., I/O) it mst call an OS procedre Known as crossing the protection bondary, or a protected procedre call Hardware provides a system call instrction that: Cases an exception, which invokes a kernel handler» Passes a parameter determining the system rotine to call Saves caller state (PC, regs, mode) so it can be restored» Why save mode? Retrning from system call restores this state 21
System Call emacs: read() User mode Kernel mode Trap to kernel mode, save state Trap handler Find read handler Restore state, retrn to ser level, resme exection read() kernel rotine 22
Another view 0xFFFFFFFF 1G 0xC0000000 Kernel Stack Kernel Code SP2 PC2 Address Space 3G User Stack SP1 0x00000000 User Code PC1 25
System Call Qestions There are hndreds of syscalls. How do we let the kernel know which one we intend to invoke? Before issing int $0x80 or sysenter, set %eax/%rax with the syscall nmber System calls are like fnction calls, bt how to pass parameters? Jst like calling convention in syscalls, typically passed throgh %ebx, %ecx, %edx, %esi, %edi, %ebp 26
More qestions How to reference kernel objects (e.g., files, sockets)? Naming problem an integer mapped to a niqe object» int fd = open( file ); read(fd, bffer); Why can t we reference the kernel objects by memory address? 27
System calls in xv6 Look at trap.h and trap.c Interrpt handlers are initialized in two arrays (idt and vectors)» Tvinit() fnction does the initialization Syscalls have a single trap handler (T_SYSCALL, 64) Trap() handles all exceptions, inclding system calls» If the exception is a system call, it calls syscall() Keep digging from there to nderstand how system calls are spported Yo will be adding a new system call in Lab 1 28
Categorizing Events Unexpected Deliberate Synchronos falt syscall trap Asynchronos interrpt software interrpt Interrpts signal asynchronos events I/O hardware interrpts Software and hardware timers 29
Timer The key to a timesharing OS The fallback mechanism by which the OS reclaims control Timer is set to generate an interrpt after a period of time» Setting timer is a privileged instrction» When timer expires, generates an interrpt Handled by the OS, forcing a switch from the ser program» Basis for OS schedler (more later ) Also sed for time-based fnctions (e.g., sleep()) 31
I/O sing Interrpts Interrpts are the basis for asynchronos I/O OS initiates I/O Device operates independently of rest of machine Device sends an interrpt signal to CPU when done OS maintains a vector table containing a list of addresses of kernel rotines to handle varios events CPU looks p kernel address indexed by interrpt nmber, context switches to rotine 33
I/O Example 1. Ethernet receives packet, writes packet into memory 2. Ethernet signals an interrpt 3. CPU stops crrent operation, switches to kernel mode, saves machine state (PC, mode, etc.) on kernel stack 4. CPU reads address from vector table indexed by interrpt nmber, branches to address (Ethernet device driver) 5. Ethernet device driver processes packet (reads device registers to find packet in memory) 6. Upon completion, restores saved state from stack 34
Interrpt Qestions Interrpts halt the exection of a process and transfer control (exection) to the operating system Can the OS be interrpted? (Consider why there might be different interrpt levels) Interrpts are sed by devices to have the OS do stff What is an alternative approach to sing interrpts? What are the drawbacks of that approach? 35
Memory Isolation OS mst be able to protect programs from each other OS mst protect itself from ser programs OS may or may not protect ser programs from itself Memory management nit (MMU) Hardware nit provides memory protection mechanisms Virtal memory Segmentation Maniplating memory management hardware ses protected (privileged) operations 37
Example memory protection Physical Memory INSTR DATA HEAP STACK Base Bonds CPU Memory Reference OK? Yes Contine No Exception 38
Smmary Protection User/kernel modes Protected instrctions System calls Used by ser-level processes to access OS fnctions Access what is in the OS Exceptions Unexpected event dring exection (e.g., divide by zero) Interrpts Timer, I/O 39
Next Time Processes Project: Contine to get familiar with the environment» In particlar, Chapter 0 Read the system call/interrpt chapter in the xv6 book (Chapter 3) If yo have time, work throgh at least some of the booting seqence ttorial» Read appendix A and B in xv6 book Ask qestions on Piazza 40