Exceptional Control Flow

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.
Chap 2 System Structures.
Operating-System Structures
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Exceptions, Interrupts & Traps
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
3/11/2002CSE Input/Output Input/Output Control Datapath Memory Processor Input Output Memory Input Output Network Control Datapath Processor.
Traps and Faults. Review: Mode and Space data user mode kernel mode A B C “kernel space”
OS provide a user-friendly environment and manage resources of the computer system. Operating systems manage: –Processes –Memory –Storage –I/O subsystem.
Chapter 2: Operating-System Structures. 2.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 2: Operating-System Structures Operating.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
CSC 2405 Computer Systems II Exceptions Mini-Lecture Traps & Interrupts.
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.
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.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
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);
Carnegie Mellon 1 CMSC os1: OS and Exceptions Chapter 8.1 Instructors: Haryadi Gunawi Henry Hoffmann Matthew Wachs.
Computer System Structures Interrupts
System Calls, Interrupts and Exceptions
Introduction to Operating Systems
CS 3214 Computer Systems Lecture 9 Godmar Back.
Interrupts and exceptions
Exceptional Control Flow & Processes
Exceptional Control Flow
Exceptions and Processes
Chapter 2: Operating-System Structures
Exceptional Control Flow
Exceptional Control Flow
Chapter 2: System Structures
Overview of today’s lecture
Exceptional Control Flow
Cache Wrap-up, System Control Flow CSE 410 Winter 2017
os1: OS and Exceptions Chapter 8.1
Introduction to Operating System (OS)
Instructors: Anthony Rowe, Seth Goldstein and Gregory Kesden
Exceptional Control Flow: System Calls, Page Faults etc.
Cache Wrap-Up, System Control Flow CSE 351 Autumn 2017
Chapter 2: Operating-System Structures
Exceptional Control Flow
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S
Chapter 2: System Structures
Exceptional Control Flow I
I/O Systems I/O Hardware Application I/O Interface
Exceptional Control Flow Part I
Exceptions Control Flow
Architectural Support for OS
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.
System Control Flow and Processes CSE 351 Winter 2018
Chapter 2: Operating-System Structures
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Architectural Support for OS
System Calls System calls are the user API to the OS
Exceptional Control Flow & Processes October 2, 2008
Interrupts and System Calls
In Today’s Class.. General Kernel Responsibilities Kernel Organization
III. Operating System Structures
Presentation transcript:

Exceptional Control Flow Slides adapted from: Gregory Kesden and Markus Püschel of Carnegie Mellon University

Control Flow Processors do only one thing: Physical control flow From startup to shutdown, a CPU simply reads and executes (interprets) a sequence of instructions, one at a time This sequence is the CPU’s control flow (or flow of control) Physical control flow <startup> inst1 inst2 inst3 … instn <shutdown> Time

Altering the Control Flow Up to now: two mechanisms for changing control flow: Jumps and branches Call and return React to changes in program state Insufficient for a useful system: Difficult to react to changes in system state Data arrives from a disk or a network adapter Instruction divides by zero User hits Ctrl-C at the keyboard System timer expires System needs mechanisms for “exceptional control flow”

Exceptional Control Flow Exists at all levels of a computer system Low level mechanisms 1. Exceptions Change in control flow in response to a system event (i.e., change in system state) Implemented using combination of hardware and OS software Higher level mechanisms 2. Process context switch Implemented by OS software and hardware timer 3. Signals Implemented by OS software 4. Nonlocal jumps: setjmp() and longjmp() Implemented by C runtime library

Exceptions An exception is a transfer of control to the OS kernel in response to some event (i.e., change in processor state) Kernel is the memory-resident part of the OS Examples of events: Divide by 0, arithmetic overflow, page fault, I/O request completes, typing Ctrl-C User code Kernel code Event Exception I_current I_next Exception processing by exception handler Return to I_current Return to I_next Abort

Exception Tables Exception numbers 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 Code for exception handler 0 Exception Table Code for exception handler 1 1 Code for exception handler 2 2 ... n-1 ... Code for exception handler n-1

Asynchronous Exceptions (Interrupts) Caused by events external to the processor Indicated by setting the processor’s interrupt pin Handler returns to “next” instruction Examples: Timer interrupt Every few ms, an external timer chip triggers an interrupt Used by the kernel to take back control from user programs I/O interrupt from external device Hitting Ctrl-C at the keyboard Arrival of a packet from a network Arrival of data from a disk

Synchronous Exceptions Caused by events that occur as a result of executing an instruction: Traps Intentional Examples: system calls, breakpoint traps, special instructions Returns control to “next” instruction Faults Unintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptions Either re-executes faulting (“current”) instruction or aborts Aborts Unintentional and unrecoverable Examples: illegal instruction, parity error, machine check Aborts current program

Fault Example: Page Fault int a[1000]; main () { a[500] = 13; } User writes to memory location That portion (page) of user’s memory is currently on disk 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10 User code Kernel code Exception: page fault movl Copy page from disk to memory Return and reexecute movl

Fault Example: Invalid Memory Reference int a[1000]; main () { a[5000] = 13; } 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360 User code Kernel code Exception: page fault movl Detect invalid address Signal process Sends SIGSEGV signal to user process User process exits with “segmentation fault”

Traps: System Calls Each x86-64 system call has a unique ID number Examples: Number Name Description read Read file 1 write Write file 2 open Open file 3 close Close file 4 stat Get info about file 57 fork Create process 59 execve Execute a program 60 _exit Terminate process 62 kill Send signal to process

System Call Example: Opening File User calls: open(filename, options) Calls __open function, which invokes system call instruction syscall 00000000000e5d70 <__open>: ... e5d79: b8 02 00 00 00 mov $0x2,%eax # open is syscall #2 e5d7e: 0f 05 syscall # Return value in %rax e5d80: 48 3d 01 f0 ff ff cmp $0xfffffffffffff001,%rax e5dfa: c3 retq User code Kernel code %rax contains syscall number Other arguments in %rdi, %rsi, %rdx, %r10, %r8, %r9 Return value in %rax Negative value is an error corresponding to negative errno Exception syscall cmp Open file Returns

System call Applications should be prevented to directly access hardware such as Physical memory, disk, network, halt But nevertheless, they need to access these resources in a controlled way: Read/write their own memory Access the files that they have permission Access the network for its own communications Halt Processors run at different security levels: User level: Kernel-level:

System calls Programming interface to the services provided by the OS A set of functions (“API” (Application Programming Interface)) provided by the OS to the user applications Allow the user applications to access hardware in a controlled way System calls are functions that can directly access

Library example

System Calls Process Control File management Device Management Load, execute end, abort create and terminate process File management create file, delete file open, close, read, write, seek Device Management request device, release device read, write, reposition Information Maintenance get/set time or date, get/set system data Communication create, delete communication connection send, receive messages

Most common System API Most common system API POSIX API (most versions of UNIX, Linux, and Mac OS X) Win32 API for Windows POSIX (IEEE 1003.1, ISO/IEC 9945) Very widely used standard based on (and including) C-language Defines both system calls and compulsory system programs together with their functionality and command-line format E.g. ls –w dir prints the list of files in a directory in a ‘wide’ format Complete specification is at http://www.opengroup.org/onlinepubs/9699919799/nframe.html Win32 (Microsoft Windows based systems) Specifies system calls together with many Windows GUI routines VERY complex, no really complete specification