Carnegie Mellon 1 CMSC os1: OS and Exceptions Chapter 8.1 Instructors: Haryadi Gunawi Henry Hoffmann Matthew Wachs.

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

1 Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007.
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes /18-243: Introduction to Computer Systems 13 th Lecture, June 15, 2011 Instructors:
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
University of Washington Today Midterm grades posted  76. HW 3 due Lab 4 fun! 1.
Chapter 6 Limited Direct Execution
CMPT 300: Operating Systems I Dr. Mohamed Hefeeda
University of Washington Today Finish up cache-aware programming example Processes  So we can move on to virtual memory 1.
University of Washington Today Move on to … 1. University of Washington Hmm… Lets look at this again Disk Main Memory L2 unified cache L1 I-cache L1 D-cache.
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
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.
Cs238 Lecture 3 Operating System Structures Dr. Alan R. Davis.
Introduction to Interrupts
OS Spring’04 Introduction Operating Systems Spring 2004.
Exceptions, Interrupts & Traps
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
COMP201 Computer Systems Exceptions and Interrupts.
3/11/2002CSE Input/Output Input/Output Control Datapath Memory Processor Input Output Memory Input Output Network Control Datapath Processor.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Architecture Support for OS CSCI 444/544 Operating Systems Fall 2008.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Chapter 8 I/O. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-2 I/O: Connecting to Outside World So far,
1 CS/COE0447 Computer Organization & Assembly Language Chapter 5 part 4 Exceptions.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
Virtual Memory Review Goal: give illusion of a large memory Allow many processes to share single memory Strategy Break physical memory up into blocks (pages)
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes : Introduction to Computer Systems 13 th Lecture, Oct. 7, 2014 Instructors:
CSC 2405 Computer Systems II Exceptions Mini-Lecture Traps & Interrupts.
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”
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 4 Computer Systems Review.
University of Washington Exceptional Control Flow The Hardware/Software Interface CSE351 Winter 2013.
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.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Introduction to Operating Systems Concepts
Exceptional Control Flow
CS 3214 Computer Systems Lecture 9 Godmar Back.
Exceptional Control Flow & Processes
Exceptional Control Flow
Microprocessor Systems Design I
Session 3 Memory Management
Exceptional Control Flow
Today How was the midterm review? Lab4 due today.
Exceptional Control Flow
Overview of today’s lecture
Exceptional Control Flow
os1: OS and Exceptions Chapter 8.1
Exceptional Control Flow: System Calls, Page Faults etc.
Exceptional Control Flow I
Chapter 8 I/O.
COMPUTER PERIPHERALS AND INTERFACES
Exceptions Control Flow
Architectural Support for OS
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
Exceptions and Processes
CS201 - Lecture 17 Exceptions
Operating Systems Lecture 3.
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.
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Chapter 8 I/O.
CSE 451: Operating Systems Winter 2007 Module 2 Architectural Support for Operating Systems Brian Bershad 562 Allen Center 1.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Architectural Support for OS
Interrupts and System Calls
Presentation transcript:

Carnegie Mellon 1 CMSC os1: OS and Exceptions Chapter 8.1 Instructors: Haryadi Gunawi Henry Hoffmann Matthew Wachs

Carnegie Mellon 2 CMSC Control Flow inst 1 inst 2 inst 3 … inst n A processor does only “one thing”:  From startup to shutdown …  Reads an instruction from memory  Executes the instruction This sequence is the CPU’s control flow Time

Carnegie Mellon 3 CMSC Processor != Computer Difference?

Carnegie Mellon 4 CMSC Processor != Computer A processor is a “passive” component  (A simple perspective; also complex in reality) A computer does much more  Run multiple processes  Deal with errors  Receive network packets (e.g. Skype messages)  Read/write data to disk/SSD  Input/output: keyboard, mouse, screen  But, who manages all of these? The answer: the operating system  Lots of mechanisms and policies  We’ll begin with exception

Carnegie Mellon 5 CMSC What is an OS? A top-down description  Wiki:  An operating system (OS) is software that manages computer hardware and software resources and provides common services for computer programs. The operating system is an essential component of the system software in a computer system. Application programs usually require an operating system to function.  Book: … This lecture: A bottom-up description  Need both!  (don’t forget to read the book)

Carnegie Mellon 6 CMSC OS and program executions OS manages program executions  Tens/hundreds of programs are running on your laptop!  (Including the execution of itself, i.e. the OS code) What does a program look like?  Hello.c  hello (a binary file) What does an OS look like?  Whole tree:  CPU mgmt:  Bluetooth:  A bunch of C files! (just like hello.c)  Compiled into /boot/vmlinux… (100s MB)

Carnegie Mellon 7 CMSC Objdump Objdump:  objdump –t  functions  objdump –D  all instructions Program (binary) files  objdump –t hello | grep -w F | wc –l  12 functions, 8.2 KB  objdump -t /boot/vmlinux | grep -w F | wc –l  Linux: 30,000+ functions, 146MB (excluding loadable drivers)  “vmlinuz”  compressed version of vmlinux Where does a program live?  In disk, and loaded to memory

Carnegie Mellon 8 CMSC Memory and Addresses High-speed volatile storage Addressable  load R1 Mem[Addr] Address range  8 bytes of memory  [0x 0 – 0x 8)  16 bytes of memory  [0x 0 – 0x 10)  32 bytes of memory  [0x 0 -- ??  2 GB  [0x 0 – 0x )  4 GB  [0x 0 -- ?? Largest address -- 0x 0

Carnegie Mellon 9 CMSC Programs and OS Where are they?  So far abstract view. Real view:  On the disk:  (yourPath) /p1/hello  /boot/vmlinux  Gets loaded to memory  Who loads hello?  Who loads vmlinux? Memory 0 2 n -1 0 OS (vmlinux) hello: Main() 0x0800C040 main() 0xC CPU (need instructions) OS registers the next instruction address e.g. 0x800C040 /boot/vmlinux …/p1/hello

Carnegie Mellon 10 CMSC But … an infinite program? Ex:  While(true) { i++ };  0x800C040: mov Mem[i] eax  0x800C050: inc eax  0x800C060: mov eax Mem[i]  0x800C070: jump 0x800C040 A new program also runs  Will you see print “A”? Yes/No?  But how come?  (CPU is just a passive component) Memory 2 n -1 0 Infinite Program 0x0800C040 CPU (running infinite Prog ) New prog: Print “A”

Carnegie Mellon 11 CMSC Or, a network “interrupt” A long program is using the CPU But then …  An “important” tweet about Justin Bieber just came in  From where? NIC card When to process the message?  (Process message needs CPU)  After the long program ends? Memory 0 2 n -1 Long program CPU (running long prog ) Twitter “Bieber got arrested again”

Carnegie Mellon 12 CMSC Or, a buggy program Ex: divide by zero  x = y / 0;  0x800C040: Div R1 R2 0  0x800C050: Store R1 Mem[x] What will happen?  Execute the next instruction?  What will happen to the buggy program?  But how? What’s the next instruction to execute by the CPU? Memory 0 2 n -1 0 Vmlinux divByZero() ; Buggy program 0x0800C040 0xC CPU (Div R1 R2 0)

Carnegie Mellon 13 CMSC Or, make a system call E.g. read()/write()  X = write(fileDesc, buf, …)  Where are the actual functions that perform reading/writing bytes from the disk? Memory 0 2 n -1 0 Vmlinux Diskrw() {} Program with I/Os CPU 0xC

Carnegie Mellon 14 CMSC The problem statement Computer is complex  System calls, network interrupts, error handling, fairness,..  But processor is “passive” We need a mechanism to deal with these scenarios  That is, we need to break the control flow of a running program Solution: Exceptions  In the book: “exceptional control flow”  Alter the program flow of the program The mechanism:  Exception handlers (written by OS Developers)  Exception table (provided in the HW)  HW and SW (i.e. OS) developers work together to solve the problem inst 1 inst 2 inst 3 … inst n

Carnegie Mellon 15 CMSC Exceptions Exception handlers and Exception table

Carnegie Mellon 16 CMSC OS contains exception handlers TimerInterrupt() { // every 10 ms ScheduleProcesses(); Ex: P1 just ran, so now P2’s turn; } DivideByZeroError() { // e.g. currentP just did x/0 kill(currentProcess); popUpWindow(“hey something wrong”); scheduleProcesses(); } Syscall_handler (int arg) { if (arg==READ) sys_read(…); … } nicInterrupt() { // read data from NIC }

Carnegie Mellon 17 CMSC Exception handlers Compiled and get loaded to the memory  (during boot time by the boot loaded)  E.g. divideByZero() is loaded to address 0xC  E.g. timerInterrupt, 0xC  E.g. syscall_handler, 0xC  (Addresses are not static, known at boot time) Memory vmlinux/OS divByZero(){} timerInt(){} sysHandler() {} 0xC xC xC

Carnegie Mellon 18 CMSC Exception/Interrupt Table Built inside the CPU  256 entries Initialization  The boot loader boots the OS  OS initialization code install addresses of exception handlers to exception table Excep- tion # DescriptionActual Functions 0Divide ErrorDivideByZeroError() 128SyscallSyscallHandler() 200Timer interruptTimerInterrupt() (See Fig. 8.9) Exc#Exception handler addresses 00xC …… 1280xC …… 2000xC

Carnegie Mellon 19 CMSC Example … Memory 0 0 Vmlinux DivByZero() { } Buggy program 0x0800C040 0xC CPU (Div X = Y / Z) 0x0800C040 CPU (Exec DivByZero()) 0xC In CPU/ALU circuit: if (Z == 0) fetch exc[0] Exception Table 00xC

Carnegie Mellon 20 CMSC Example … Memory 0 0 Vmlinux timerIntr() { } Long program 0x0800C040 0xC CPU (Long program) 0x0800C040 In CPU circuit: (every 10ms) fetch exc[200] CPU (Exec timerIntr()) 0xC Another program 0x CPU (another program) 0x Exception Table 2000xC

Carnegie Mellon 21 CMSC Summary Computer is complex  Need exceptional control flow  ECF is important because its how we implement: Input/output, processes (and user-level control of process creation), virtual memory, interactions with OS, software exceptions Exception handlers – in SW  Basically OS functions, compiled, then get loaded to certain memory addresses Exception table (or interrupt descriptor table) – in HW  Built-in the CPU, initialized by the OS, contains addresses  HW and SW developers work together  HW provides the interrupt table  SW (OS) decides how to handle each exception/interrupt

Carnegie Mellon 22 CMSC Exceptional Control Flow (Cont’d) (From the book)

Carnegie Mellon 23 CMSC Exceptions An exception is a transfer of control to the OS in response to some event (i.e., change in processor state) Examples: page fault, I/O request completes, Ctrl-C, divide by 0 … User ProcessOS exception exception processing by exception handler return to I_current return to I_next abort event I_current I_next

Carnegie Mellon 24 CMSC Asynchronous Exceptions (Interrupts) Caused by events external to the processor  Indicated by setting the processor’s interrupt pin  Handler returns to “next” instruction Examples:  I/O interrupts  hitting Ctrl-C at the keyboard  arrival of a packet from a network  arrival of data from a disk  Timer interrupt  Hard reset interrupt  hitting the reset button  Soft reset interrupt  hitting Ctrl-Alt-Delete on a PC

Carnegie Mellon 25 CMSC Synchronous Exceptions Caused by events that occur as a result of executing an instruction:  Traps (example in next slide)  Intentional, returns control to “next” instruction  Examples: system calls, breakpoint traps, special instructions  Faults  Unintentional but possibly recoverable  Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptions –(Imagine a 1 MB microsoft office program, not all of them are loaded to memory; if next instruction (PC++) is not in memory, go to OS and fetch next code page from disk)  Either re-executes faulting (“current”) instruction or aborts  Aborts  unintentional and unrecoverable  Examples: parity error, machine check  Aborts current program

Carnegie Mellon 26 CMSC In CPU circuit: fetch code address at exc[128] (the syscall_handler() in OS code, which will put the string to the monitor screen)

Carnegie Mellon 27 CMSC Summary Exceptions  Events that require nonstandard control flow  Generated externally (interrupts) or internally (traps and faults)

Carnegie Mellon 28 CMSC EXTRA (Quick recap)

Carnegie Mellon 29 CMSC Trap Example: Opening File User calls: open(filename, options) Function open executes trap instruction int (with syscall code x80=128) OS must find or create file, get it ready for reading or writing Returns integer file descriptor 0804d070 : d082:cd 80 int $0x80 804d084:5b pop %ebx... User ProcessOS exception open file returns int pop

Carnegie Mellon 30 CMSC Fault Example: Page Fault (You can ignore for now, until we learn about VM) User writes to memory location That portion (page) of user’s memory is currently on disk Page handler must load page into physical memory Returns to faulting instruction Successful on second try int a[1000]; main () { a[500] = 13; } 80483b7:c d d movl $0xd,0x8049d10 User ProcessOS exception: page fault Create page and load into memory returns movl

Carnegie Mellon 31 CMSC Fault Example: Invalid Memory Reference Page handler detects invalid address Sends SIGSEGV signal to user process User process exits with “segmentation fault” int a[1000]; main () { a[5000] = 13; } 80483b7:c e d movl $0xd,0x804e360 User ProcessOS exception: page fault detect invalid address movl signal process

Carnegie Mellon 32 CMSC n-1 Interrupt Vectors Each type of event has a unique exception number k k = index into exception table (a.k.a. interrupt vector) Handler k is called each time exception k occurs Exception Table code for exception handler 0 code for exception handler 1 code for exception handler 2 code for exception handler n-1... Exception numbers

Carnegie Mellon 33 CMSC Exception Table IA32 (Excerpt) Exception NumberDescriptionException Class 0Divide errorFault 13General protection faultFault 14Page faultFault 18Machine checkAbort OS-definedInterrupt or trap 128 (0x80)System callTrap OS-definedInterrupt or trap Check Table 6-1: are Intel- defined are OS-defined

Carnegie Mellon 34 CMSC Linux/IA32 system call codes