CS201 - Lecture 17 Exceptions

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.
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
OS Spring’03 Introduction Operating Systems Spring 2003.
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.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Process Description and Control A process is sometimes called a task, it is a program in execution.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
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”
Processes and Virtual Memory
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
University of Washington Exceptional Control Flow The Hardware/Software Interface CSE351 Winter 2013.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts Slides adapted from Bryant.
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.
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);
Computer Organization & Design 计算机组成与设计
Exceptional Control Flow
CS 3214 Computer Systems Lecture 9 Godmar Back.
Introduction to Operating Systems
Processes and threads.
Operating Systems CMPSC 473
Exceptional Control Flow & Processes
Exceptional Control Flow
Exceptions and Processes
CS 6560: Operating Systems Design
Session 3 Memory Management
Exceptional Control Flow
Today How was the midterm review? Lab4 due today.
Anton Burtsev February, 2017
Exceptional Control Flow
Overview of today’s lecture
Exceptional Control Flow Part I
Exceptional Control Flow
Cache Wrap-up, System Control Flow CSE 410 Winter 2017
Exceptional Control Flow: System Calls, Page Faults etc.
Structure of Processes
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory: Concepts /18-213/14-513/15-513: Introduction to Computer Systems 17th Lecture, October 23, 2018.
Introduction to Operating Systems
Exceptional Control Flow I
CSE 351 Section 10 The END…Almost 3/7/12
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process & its States Lecture 5.
Exceptional Control Flow Part I
Exceptions Control Flow
Architectural Support for OS
CSCE 313 – Introduction to UNIx process
Exceptions and Processes
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
System Control Flow and Processes CSE 351 Winter 2018
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
CS510 Operating System Foundations
Architectural Support for OS
CSE 153 Design of Operating Systems Winter 2019
Process Description and Control in Unix
Process Description and Control in Unix
Exceptional Control Flow & Processes October 2, 2008
Structure of Processes
Interrupts and System Calls
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Instructor: Phil Gibbons
Presentation transcript:

CS201 - Lecture 17 Exceptions Raoul rivas Portland state university

Announcements CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

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 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Altering 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” CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

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 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

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 Exception Event I_current I_next Exception processing by exception handler Return to I_current Return to I_next Abort CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Interrupt Descriptor Tables (Exception Tables) 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 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Interrupts (Asynchronous Exceptions) 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 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

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 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

User and Kernel Space Operating System Hardware User Space User To ensure integrity of the Operating System The execution and memory are divided in two spaces Kernel Space (Operating System) User Space (Programs) User Space User Program User Program User Program Operating System Kernel Space Hardware CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

System Calls TRAP Kernel Space I D T User Space 0xff023 Linux Kernel Code Sys_write(handle, size); Functions that call the Operating System are isolated to ensure security and system protection Implemented as Traps (Exceptions) User specifies the System Call number and parameters Generates a Software Interrupt and Traps into the Operating System OS executes operation on behalf of the user OS returns control to user I D T Return TRAP Int 0x80 C library syscall(WRITE, handle, size); UserCode.c write(handle, size); User Space CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

System Call Table Number Name Description read Read file 1 write 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 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

System Call Example User code Kernel code 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 %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 User code Kernel code Exception syscall cmp Open file Returns CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

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” CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Process Definition: A process is an instance of a running program. One of the most profound ideas in computer science Process provides each program with two key abstractions: Logical control flow Each program seems to have exclusive use of the CPU Provided by kernel mechanism called context switching Private address space Each program seems to have exclusive use of main memory. Provided by kernel mechanism called virtual memory Memory Stack Heap Code Data CPU Registers CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Private Address Space Ideal case! So far we have been concerned with running one program at a time Executable Objects are non relocatable Compiler generates code with relative offsets to a base address What happens when two programs are loaded into memory? Physical Memory Program 2 Operating System 0x8000 0x9000 Program 1 0x8000 0x7000 Program 1 Ideal case! 0x7000 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Can we assign each program its own address space? Private Address Space Most likely we are not going to be so lucky Physical Memory Program 2 Operating System 0x7000 0x9000 Program 1 0x8000 0x7000 Program 1 0x7000 Can we assign each program its own address space? CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Virtual Memory Each process has its own virtual address space ... ... Each process can view memory as a simple linear array Hardware converts Virtual Addresses to Physical Addresses Address translation Virtual Address Space for Process 1: Physical Address Space (DRAM) VP 1 VP 2 PP 2 ... N-1 (e.g., read-only library code) PP 6 Virtual Address Space for Process 2: PP 8 VP 1 VP 2 ... ... CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Virtual Memory: Page Tables A page table is an array of page table entries (PTEs) that maps virtual pages to physical pages. Physical memory (DRAM) Physical page number or disk address Valid VP 1 PP 0 VP 2 PTE 0 null VP 7 1 VP 4 PP 3 1 1 Swap Page File (disk) null PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Linking and Loading Linking Loading Memory invisible to user code Linking Each program has similar virtual address space Code, stack, and shared libraries always start at the same address Loading execve allocates virtual pages for .text and .data sections & creates PTEs marked as invalid The .text and .data sections are copied, page by page from the disk, on demand by the virtual memory system Operating System User stack (created at runtime) %rsp (stack pointer) Memory-mapped region for shared libraries brk Run-time heap (created by malloc) Read/write data segment .data Loaded from the executable file Read-only code segment .text Unused 0x400000 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Memory Protection Virtual Memory creates a separate virtual address space for each process Processes cannot access each other’s memory space unless explicitly allowed They do not know which Physical Address the other process is located! Physical Memory Program 2 Operating System 0x7000 0x4000 Program 2 Virtual Addresses 0x3000 Program 1 Program 1 0x2000 0x7000 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Multiprocessing … Memory Memory Memory CPU CPU CPU Stack Stack Stack Heap Heap … Heap Data Data Data Code Code Code CPU CPU CPU Registers Registers Registers Computer runs many processes simultaneously Applications for one or more users Web browsers, email clients, editors, … Background tasks Monitoring network & I/O devices CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Multiprocessing Running program “top” on Mac System has 123 processes, 5 of which are active Identified by Process ID (PID) CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Multiprocessing … Memory CPU Stack Stack Stack Heap Heap Heap … Data Data Data Code Code Code Saved registers Saved registers Saved registers CPU Registers Single processor executes multiple processes concurrently Process executions interleaved (multitasking) Address spaces managed by virtual memory system (later in course) Register values for nonexecuting processes saved in memory CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Multiprocessing … Memory CPU Save current registers in memory Stack Heap Heap Heap … Data Data Data Code Code Code Saved registers Saved registers Saved registers CPU Registers Save current registers in memory CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Multiprocessing … Memory CPU Stack Stack Stack Heap Heap Heap … Data Data Data Code Code Code Saved registers Saved registers Saved registers CPU Registers Schedule next process for execution (Context Switch) Load registers of next process CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Concurrent Execution Each process is a logical control flow. Two processes run concurrently (are concurrent) if their flows overlap in time Otherwise, they are sequential Examples (running on single core): Concurrent: A & B, A & C Sequential: B & C Process A Process B Process C Time CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Concurrent Execution: User View Control flows for concurrent processes are physically disjoint in time However, we can think of concurrent processes as running in parallel with each other Process A Process B Process C Time CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

SaveRegs[A], LoadRegs[B] SaveRegs[B], LoadRegs[A] Context Switching Processes are managed by a shared chunk of memory-resident OS code called the kernel Important: the kernel is not a separate process, but rather runs as part of some existing process. Control flow passes from one process to another via a context switch Process A Process B user code SaveRegs[A], LoadRegs[B] kernel code context switch Time user code kernel code context switch SaveRegs[B], LoadRegs[A] user code CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Summary An exception is a transfer of control to the OS kernel in response to some event User and Kernel Space provide separate memory address spaces for Operating System and User Programs System Calls are implemented as traps to ensure inetrgity of the Operating System A process is an instance of a running program. Private address space Logical control flow Control flow passes from one process to another via a context switch CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)