Traps, Exceptions, System Calls, & Privileged Mode Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University P&H Chapter 4.9, pages 509–515,

Slides:



Advertisements
Similar presentations
Traps, Exceptions, System Calls, & Privileged Mode Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University P&H Chapter 4.9, pages 509–515,
Advertisements

1 Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007.
Computer Organization CS224 Fall 2012 Lesson 44. Virtual Memory  Use main memory as a “cache” for secondary (disk) storage l Managed jointly by CPU hardware.
The Kernel Abstraction. Challenge: Protection How do we execute code with restricted privileges? – Either because the code is buggy or if it might be.
Chapter 6 Limited Direct Execution
Architectural Support for Operating Systems Prof. Sirer CS 4410 Cornell University.
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Interrupts STOP!!! Do this!!!. CMPE12cGabriel Hugh Elkaim 2 Interrupts Have device tell OS/CPU it is ready Requires hardware to support. OS/CPU can then.
Disco Running Commodity Operating Systems on Scalable Multiprocessors.
OS Spring’03 Introduction Operating Systems Spring 2003.
Pipeline Exceptions & ControlCSCE430/830 Pipeline: Exceptions & Control CSCE430/830 Computer Architecture Lecturer: Prof. Hong Jiang Courtesy of Yifeng.
1 Last Class: Introduction Operating system = interface between user & architecture Importance of OS OS history: Change is only constant User-level Applications.
OS Spring’04 Introduction Operating Systems Spring 2004.
Exceptions, Interrupts & Traps
The Processor Data Path & Control Chapter 5 Part 4 – Exception Handling N. Guydosh 3/1/04+
1 OS & Computer Architecture Modern OS Functionality (brief review) Architecture Basics Hardware Support for OS Features.
What are Exception and Interrupts? MIPS terminology Exception: any unexpected change in the internal control flow – Invoking an operating system service.
Deniz ALTINBUKEN CS 3410, Spring 2015 Computer Science Cornell University P&H Chapter 4.9, pages 445–452, appendix A.7.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Protection and the Kernel: Mode, Space, and Context.
Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University P&H Chapter 4.9, pages 445–452, appendix A.7.
Architecture Support for OS CSCI 444/544 Operating Systems Fall 2008.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
Virtual Memory Expanding Memory Multiple Concurrent Processes.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
1 CSE451 Architectural Supports for Operating Systems Autumn 2002 Gary Kimura Lecture #2 October 2, 2002.
Exceptional Control Flow Topics Exceptions except1.ppt CS 105 “Tour of the Black Holes of Computing”
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Microprogramming and Exceptions Spring 2005 Ilam University.
Constructive Computer Architecture Interrupts/Exceptions/Faults Arvind Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology.
Processes and Virtual Memory
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Lecture 26 Virtual Machine Monitors. Virtual Machines Goal: run an guest OS over an host OS Who has done this? Why might it be useful? Examples: Vmware,
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Virtual Memory Ch. 8 & 9 Silberschatz Operating Systems Book.
Deniz ALTINBUKEN CS 3410, Spring 2015 Computer Science Cornell University P&H Chapter 4.9, pages 445–452, appendix A.7.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Traps, Exceptions, System Calls, & Privileged Mode Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University P&H Chapter 4.9, pages 509–515,
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Traps, Exceptions, System Calls, & Privileged Mode Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University P&H Chapter 4.9, pages 509–515,
Of Privilege, Traps, Interrupts & Exceptions Prof. Sirer CS 316 Cornell University.
Computer System Structures Interrupts
Introduction to Operating Systems
Interrupts and exceptions
Traps, Exceptions, System Calls, & Privileged Mode
Protection and OS Structure
Exceptional Control Flow
Modeling Page Replacement Algorithms
CSE 120 Principles of Operating
Syscalls, exceptions, and interrupts, …oh my!
Modeling Page Replacement Algorithms
Exceptions Control Flow
Architectural Support for OS
Interrupts and Exception Handling
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
CSE 451: Operating Systems Winter 2007 Module 2 Architectural Support for Operating Systems Brian Bershad 562 Allen Center 1.
Control Hazards Branches (conditional, unconditional, call-return)
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Architectural Support for OS
ECE/CS 552: Pipelining and Exceptions
Syscalls, exceptions, and interrupts, …oh my!
Interrupts and exceptions
Syscalls, exceptions, and interrupts, …oh my!
Interrupts and Exception Handling
Presentation transcript:

Traps, Exceptions, System Calls, & Privileged Mode Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University P&H Chapter 4.9, pages 509–515, appendix B.7

2 Operating Systems

3 Control Transfers Control Transfers to OS Case 1: Program invokes OS eg: sbrk(), mmap(), sleep() Like a function call: invoke, do stuff, return results Attempt #1: OS as a library Just a function call: JAL sbrk Standard calling conventions

4 VM Hardware/Software Boundary Virtual to physical address translation Hardware (typical): Traverse PageTables on TLB miss, install TLB entries Update dirty bit in PTE when evicting Flush when PTBR changes Software (typical): Decide when to do context switches, update PTBR Decide when to add, remove, modify PTEs and PDEs –and invoke MMU to invalidate TLB entries Handle page faults: swap to/from disk, kill processes Hardware (minimal): Notify OS on TLB miss; software does everything else

5 Control Transfers Control Transfers to OS Case 1: Program invokes OS eg: sbrk(), mmap(), sleep() Like a function call: invoke, do stuff, return results Case 2: Hardware invokes OS on behalf of program Page fault, divide by zero, arithmetic overflow, … OS takes corrective action; then restarts/kills program Can CPU simply fake this: a0 = cause JAL exception_handler

6 Attempt #2: Attempt #2: OS as a library + Exception Handler Program invokes OS: regular calling convention HW invokes OS: New registers: EPC, Cause, Vector*, … On exception, CPU does… EPC  PC Cause  error/reason code PC  Vector Code at Vector does… take corrective action based on Cause return to EPC * x86: via IDTR register and IDT; MIPS used a constant

7 Sketch of Exception Handler # MIPS exception vector is 0x ktext 0x # EPC has offending PC, Cause has errcode # (step 1) save *everything* but $k0, $k1 lui $k0, 0xB000 sw $1, 0($k0) sw $2, 4($k0) sw $3, 8($k0) sw $4, 12($k0) … sw $31, 120($k0) mflo $1 sw $1, 124($k0) mfhi $1 sw $1, 128($k0) … * approximate

8 Sketch of Exception Handler # MIPS exception vector is 0x ktext 0x # EPC has offending PC, Cause has errcode # (step 1) save *everything* but $k0, $k1 # (step 2) set up a usable OS context li $sp, 0xFFFFFF00 li $fp, 0xFFFFFFFF li $gp, … * approximate

9 Sketch of Exception Handler # MIPS exception vector is 0x ktext 0x # EPC has offending PC, Cause has errcode # (step 1) save *everything* but $k0, $k1 # (step 2) set up a usable OS context # (step 3) examine Cause register, and take corrective action mfc0 $t0, Cause # move-from-coprocessor-0 if ($t0 == PAGE_FAULT) { mfc0 $a0, BadVAddr # another dedicated register jal kernel_handle_pagefault } else if ($t0 == PROTECTION_FAULT) { … } else if ($t0 == DIV_BY_ZERO) { … } * approximate

10 Sketch of Exception Handler # MIPS exception vector is 0x ktext 0x # EPC has offending PC, Cause has errcode # (step 1) save *everything* but $k0, $k1 # (step 2) set up a usable OS context # (step 3) examine Cause register, and take corrective action # (step 4) restore registers and return to where program left off lui $k0, 0xB000 lw $1, 0($k0) lw $2, 4($k0) lw $3, 8($k0) … lw $31, 120($k0) … mfc0 $k1, EPC jr $k1 * approximate

11 Hardware/Software Boundary Hardware Support: registers: EPC, Cause, Vector, BadVAddr, … instructions: mfc0, TLB flush/invalidate, cache flush, … Hardware guarantees for precise exceptions: EPC points at offending instruction Earlier instructions are finished EPC and later instructions have not started Returning to EPC will pick up where we left off

12 Double Faults, Triple Faults EPC points at offending inst Earlier inst are finished; EPC and later inst not started Returning to EPC will pick up where we left off What could possibly go wrong? Exception happens during exception handler… original EPC and Cause are lost Disable exceptions until current exception is resolved? –MIPS: Status register has a bit for enable/disable –turn exceptions back on just when returning to EPC –works for issues that can be (temporarily) ignored Use a “double fault” exception handler for rest –BSOD And if that faults? Triple fault  instant shutdown

13 Precise Exceptions EPC points at offending inst Earlier inst are finished; EPC and later inst not started Returning to EPC will pick up where we left off What could possibly go wrong? Multiple simultaneous exceptions in pipeline lw $4, 0($0) # page fault xxx $4, $5, $5 # illegal instruction add $2, $2, $3 # overflow need stalls to let earlier inst raise exception first even worse with speculative / “out-of-order” execution

14 Precise Exceptions EPC points at offending inst Earlier inst are finished; EPC and later inst not started Returning to EPC will pick up where we left off What could possibly go wrong? Exception happened in delay slot jal prints lw $4, 0($0) # page fault … need more than just EPC to identify “where we left off”

15 Precise Exceptions EPC points at offending inst Earlier inst are finished; EPC and later inst not started Returning to EPC will pick up where we left off What could possibly go wrong? Instructions with multiple faults or side effects store-and-update-register memory-to-memory-copy memory-fill, x86 “string” prefix, x86 “loop” prefix need more than just EPC to identify “where we left off” or: try to undo effects that have already happened or: have software try to finish the partially finished EPC or: all of the above

16 “The interaction between branch delay slots and exception handling is extremely unpleasant and you'll be happier if you don't think about it.” – Matt Welch

17 Attempt #2: Recap Program invokes OS regular calling convention HW invokes OS: precise exceptions vector to OS exception handler Drawbacks?

18 Attempt #2 is broken Drawbacks: Any program can muck with TLB, PageTables, OS code… A program can intercept exceptions of other programs OS can crash if program messes up $sp, $fp, $gp, … Wrong: Make these instructions and registers available only to “OS Code” “OS Code” == any code above 0x Program can still JAL into middle of OS functions Program can still muck with OS memory, pagetables, …

19 Privileged Mode aka Kernel Mode

20 Operating System Some things not available to untrusted programs: Exception registers, HALT instruction, MMU instructions, talk to I/O devices, OS memory,... Need trusted mediator: Operating System (OS) Safe control transfer Data isolation P1P2P3P4 VMfilesystemnet driver disk eth MMU

21 Privilege Mode CPU Mode Bit / Privilege Level Status Register Mode 0 = untrusted = user domain “Privileged” instructions and registers are disabled by CPU Mode 1 = trusted = kernel domain All instructions and registers are enabled Boot sequence: load first sector of disk (containing OS code) to well known address in memory Mode  1; PC  well known address OS takes over… initialize devices, MMU, timers, etc. loads programs from disk, sets up pagetables, etc. Mode  0; PC  program entry point (note: x86 has 4 levels x 3 dimensions, but nobody uses any but the 2 extremes)

22 Privilege Mode CPU Mode Bit / Privilege Level Status Register Mode 0 = untrusted = user domain “Privileged” instructions and registers are disabled by CPU Mode 1 = trusted = kernel domain All instructions and registers are enabled Boot sequence: load first sector of disk (containing OS code) to well known address in memory Mode  1; PC  well known address OS takes over… initialize devices, MMU, timers, etc. loads programs from disk, sets up pagetables, etc. Mode  0; PC  program entry point (note: x86 has 4 levels x 3 dimensions, but nobody uses any but the 2 extremes)

23 Terminology Trap: Any kind of a control transfer to the OS Syscall: Synchronous (planned), program-to-kernel transfer SYSCALL instruction in MIPS (various on x86) Exception: Asynchronous, program-to-kernel transfer exceptional events: div by zero, page fault, page protection err, … Interrupt: Aysnchronous, device-initiated transfer e.g. Network packet arrived, keyboard event, timer ticks * real mechanisms, but nobody agrees on these terms

24 Sample System Calls System call examples: putc(): Print character to screen Need to multiplex screen between competing programs send(): Send a packet on the network Need to manipulate the internals of a device sbrk(): Allocate a page Needs to update page tables & MMU sleep(): put current prog to sleep, wake other Need to update page table base register

25 System Calls System call: Not just a function call Don’t let program jump just anywhere in OS code OS can’t trust program’s registers (sp, fp, gp, etc.) SYSCALL instruction: safe transfer of control to OS Mode  0; Cause  syscall; PC  exception vector MIPS system call convention: user program mostly normal (save temps, save ra, …) but: $v0 = system call number

26 Invoking System Calls int getc() { asm("addiu $2, $0, 4"); asm("syscall"); } char *gets(char *buf) { while (...) { buf[i] = getc(); } }

27 Libraries and Wrappers Compilers do not emit SYSCALL instructions Compiler doesn’t know OS interface Libraries implement standard API from system API libc (standard C library): getc()  syscall sbrk()  syscall write()  syscall gets()  getc() printf()  write() malloc()  sbrk() …

28 Protection Boundaries user kernel

29 Where does OS live? Kernel code and data lives above 0x In same virtual address space as user process? but… user code can modify kernel code and data!

30 Where does OS live? Kernel code and data lives above 0x In its own address space? all traps switch to a different address space [expensive] prints(“hi”) syscall is tricky [why?]

31 Where does OS live? Kernel code and data lives above 0x Solution map kernel code/data into all processes at same vaddr but use supervisor=1 protection bit on PTEs VM hardware enforces user/kernel isolation

32 Interrupts

33 Recap: Traps  Map kernel into every process using supervisor PTEs  Switch to kernel mode on trap, user mode on return Syscall: Synchronous, program-to-kernel transfer user does caller-saves, invokes kernel via syscall kernel handles request, puts result in v0, and returns Exception: Asynchronous, program-to-kernel transfer user div/load/store/… faults, CPU invokes kernel kernel saves everything, handles fault, restores, and returns Interrupt: Aysnchronous, device-initiated transfer e.g. Network packet arrived, keyboard event, timer ticks kernel saves everything, handles event, restores, and returns

34 Example: Clock Interrupt Example: Clock Interrupt* Every N cycles, CPU causes exception with Cause = CLOCK_TICK OS can select N to get e.g TICKs per second.ktext 0x # (step 1) save *everything* but $k0, $k1 to 0xB # (step 2) set up a usable OS context # (step 3) examine Cause register, take action if (Cause == PAGE_FAULT) handle_pfault(BadVaddr) else if (Cause == SYSCALL) dispatch_syscall($v0) else if (Cause == CLOCK_TICK) schedule() # (step 4) restore registers and return to where program left off * not the CPU clock, but a programmable timer clock

35 Scheduler struct regs context[]; int ptbr[]; schedule() { i = current_process; j = pick_some_process(); if (i != j) { current_process = j; memcpy(context[i], 0xB ); memcpy(0xB , context[j]); asm(“mtc0 Context, ptbr[j]”); } }

36 Syscall vs. Interrupt Syscall vs. Exceptions vs. Interrupts Same mechanisms, but… Syscall saves and restores much less state Others save and restore full processor state Interrupt arrival is unrelated to user code