Systems Programming and Computer Architecture ( ) Timothy Roscoe

Similar documents
CS 201. Exceptions and Processes. Gerson Robboy Portland State University

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

Exceptions and Processes

Control Flow. Systemprogrammering 2007 Föreläsning 2 Exceptional Control Flow Part I. Exceptional Control Flow. Altering the Control Flow

Today. Exceptional Control Flow Processes. Exceptions and Processes. Control Flow. Altering the Control Flow

Exceptional Control Flow Part I

Exceptional Control Flow: Exceptions and Processes

Altering the Control Flow

Altering the Control Flow

Exceptional Control Flow Part I Oct. 17, 2002

Exceptional Control Flow Part I September 22, 2008

Exceptional Control Flow Part I

Giving credit where credit is due

Introduction to Unix. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Operating System. Chapter 3. Process. Lynn Choi School of Electrical Engineering

OS Structure. Hardware protection & privilege levels Control transfer to and from the operating system

An Interrupt is either a Hardware generated CALL (externally derived from a hardware signal)

OS Interaction and Processes

The Purpose of Interrupt

The control of I/O devices is a major concern for OS designers

ECE 485/585 Microprocessor System Design

Cache Example, System Control Flow

8086 Interrupts and Interrupt Responses:

Las time: Large Pages Last time: VAX translation. Today: I/O Devices

Processes, Exceptional

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 09, SPRING 2013

Introduction to Computer Systems , fall th Lecture, Oct. 7 th

Operating Systems Engineering Recitation #3 (part 2): Interrupt and Exception Handling on the x86. (heavily) based on MIT 6.

EEL 4744C: Microprocessor Applications. Lecture 7. Part 1. Interrupt. Dr. Tao Li 1

Reading Assignment. Interrupt. Interrupt. Interrupt. EEL 4744C: Microprocessor Applications. Lecture 7. Part 1

Reset, Interrupts, Exceptions, and Break ECE 3534

An Interrupt is either a Hardware generated CALL (externally derived from a hardware signal)

COS 318: Operating Systems

CSC 2405: Computer Systems II

COS 318: Operating Systems

An Operating System in Action

Cache Wrap Up, System Control Flow

CS 550 Operating Systems Spring Interrupt

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

CSE 153 Design of Operating Systems

Architectural Support for OS

Architectural Support for Operating Systems. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

UNIT- 5. Chapter 12 Processor Structure and Function

eaymanelshenawy.wordpress.com

Excep onal Control Flow: Excep ons and Processes

Last class: Today: Course administration OS definition, some history. Background on Computer Architecture

OPERATING SYSTEMS OVERVIEW. Operating Systems 2015 Spring by Euiseong Seo

Mechanisms for entering the system

Architectural Support for Operating Systems. Jinkyu Jeong ( Computer Systems Laboratory Sungkyunkwan University

by I.-C. Lin, Dept. CS, NCTU. Textbook: Operating System Concepts 8ed CHAPTER 13: I/O SYSTEMS

INTERRUPTS in microprocessor systems

The K Project. Interrupt and Exception Handling. LSE Team. May 14, 2018 EPITA. The K Project. LSE Team. Introduction. Interrupt Descriptor Table

for Operating Systems Computer Systems Laboratory Sungkyunkwan University

Excep&onal Control Flow: Excep&ons and Processes

W4118: interrupt and system call. Junfeng Yang

Interrupts. Chapter 20 S. Dandamudi. Outline. Exceptions

Hardware OS & OS- Application interface

Exception Handling. Precise Exception Handling. Exception Types. Exception Handling Terminology

Instruction Set Architecture of MIPS Processor

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

PC Interrupt Structure and 8259 DMA Controllers

BASIC INTERRUPT PROCESSING

Operating Systems 2010/2011

CSE351 Inaugural Edi7on Spring

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

2.5 Address Space. The IBM 6x86 CPU can directly address 64 KBytes of I/O space and 4 GBytes of physical memory (Figure 2-24).

Syscalls, exceptions, and interrupts, oh my!

ECE 650 Systems Programming & Engineering. Spring 2018

Anne Bracy CS 3410 Computer Science Cornell University

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

Review: Hardware user/kernel boundary

I/O Systems. Amir H. Payberah. Amirkabir University of Technology (Tehran Polytechnic)

Chapter 13: I/O Systems

Chapter 12. CPU Structure and Function. Yonsei University

Operating System: Chap13 I/O Systems. National Tsing-Hua University 2016, Fall Semester

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

INPUT/OUTPUT ORGANIZATION

These three counters can be programmed for either binary or BCD count.

CS 104 Computer Organization and Design

What are Exceptions? EE 457 Unit 8. Exception Processing. Exception Examples 1. Exceptions What Happens When Things Go Wrong

Yielding, General Switching. November Winter Term 2008/2009 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

POLITECNICO DI MILANO. Exception handling. Donatella Sciuto:

CS 5460/6460 Operating Systems

CPUs. Input and output. Supervisor mode, exceptions, traps. Co-processors. Computers as Components 4e 2016 Marilyn Wolf

Chapter 12: I/O Systems

Chapter 13: I/O Systems

Chapter 12: I/O Systems. Operating System Concepts Essentials 8 th Edition

Chapter 12: INTERRUPTS

Interrupts (Exceptions) Gary J. Minden September 11, 2014

Interrupt/Timer/DMA 1

COMP I/O, interrupts, exceptions April 3, 2016

DVD :50 PM Page 1 BIOS

Computer System Overview

Chapter 13: I/O Systems

Computer System Overview OPERATING SYSTEM TOP-LEVEL COMPONENTS. Simplified view: Operating Systems. Slide 1. Slide /S2. Slide 2.

ECE 372 Microcontroller Design Parallel IO Ports - Interrupts. ECE 372 Microcontroller Design Parallel IO Ports - Interrupts

COEN-4720 Embedded Systems Design Lecture 4 Interrupts (Part 1) Cristinel Ababei Dept. of Electrical and Computer Engineering Marquette University

Interrupts (Exceptions) (From LM3S1968) Gary J. Minden August 29, 2016

操作系统概念 13. I/O Systems

Transcription:

Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 AS 2016 Exceptions 1

17: Exceptions Computer Architecture and Systems Programming 252-0061-00, Herbstsemester 2016 Timothy Roscoe AS 2016 Exceptions 2

Control flow Processors do only one thing: 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 Time <startup> inst 1 inst 2 inst 3 inst n <shutdown> AS 2016 Exceptions 3

Altering the control flow Up to now: two mechanisms for changing control flow: Jumps and branches Call and return Both 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 AS 2016 Exceptions 4

Exceptional control flow Exists at all levels of a computer system Low level mechanisms Exceptions change in control flow in response to a system event (i.e., change in system state) Combination of hardware and OS software Higher level mechanisms Process context switch Signals Nonlocal jumps: setjmp()/longjmp() Implemented by either: OS software (context switch and signals) C language runtime library (nonlocal jumps) Language-level exceptions (Java, etc.) AS 2016 Exceptions 5

Exceptions An exception is a transfer of control to the OS in response to some event (i.e., change in processor state) User Process OS event I_current I_next exception return to I_current return to I_next abort exception processing by exception handler Examples: div by 0, machine check, page fault, I/O request completes, Ctrl-C AS 2016 Exceptions 6

17.1: Exception vectors and kernel mode AS 2016 Exceptions 7

Exception vectors 0 1 2 n-1 Exception numbers Exception Table... code for exception handler 0 code for exception handler 1 code for exception handler 2... code for exception handler n-1 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 AS 2016 Exceptions 8

x86 exception vectors 0 Divide error 1 Debug exception 2 Non-maskable interrupt (NMI) 3 Breakpoint 4 Overflow 5 Bounds check 6 Invalid opcode 7 Coprocessor not available 8 Double fault 9 Coprocessor segment overrun (386 or earlier) A B C D E F Invalid task state segment Segment not present Stack fault General protection fault Page Fault Reserved 10 Math fault 11 Alignment check 12 Machine check 13 SIMD floating point exception 14-1F Reserved to Intel Check pp. 183: http://download.intel.com/design/processor/manuals/253665.pdf AS 2016 20-FF Available for external interrupts Exceptions 9

Kernel mode Exceptions cause a switch to kernel mode Also: supervisor mode, privileged mode, ring 0, etc. Things are very different: Access to system state (virtual memory, etc.) Some exceptions are disabled Some new instructions and registers MMU behavior may change Details vary between processors Even in the same architecture AS 2016 Exceptions 10

The Kernel Most operating systems have a kernel That part of the OS that runs in kernel mode Think of the kernel as: A set of trap handling functions (always) A set of threads in a special address space (sometimes) Code to create the illusion of user-space processes (always) Many ways to structure the kernel and OS Microkernels, monolithic kernels, multikernels, etc. See the OS course next semester AS 2016 Exceptions 11

17.2: Synchronous exceptions AS 2016 Exceptions 12

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: parity error, machine check Aborts current program AS 2016 Exceptions 13

Fault example: page fault 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 Process movl exception: page fault returns OS Create page and load into memory int a[1000]; main () { a[500] = 13; } Page handler must load page into physical memory Returns to faulting instruction Successful on second try AS 2016 Exceptions 14

Fault example: invalid memory int a[1000]; main () { a[5000] = 13; } reference 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360 User Process OS movl exception: page fault detect invalid address signal process Page handler detects invalid address Sends SIGSEGV signal to user process User process exits with segmentation fault AS 2016 Exceptions 15

Trap example: opening a file User calls: open(filename, options) Function open executes system call instruction int 0804d070 < libc_open>:... 804d082: cd 80 int $0x80 804d084: 5b pop %ebx... User Process OS int pop exception returns open file OS must find or create file, get it ready for reading or writing Returns integer file descriptor AS 2016 Exceptions 16

17.3: Asynchronous exceptions AS 2016 Exceptions 17

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 Hard reset interrupt hitting the reset button Soft reset interrupt hitting Ctrl-Alt-Delete on a PC AS 2016 Exceptions 18

Interrupts CPU Interrupt-request line triggered by I/O device Might be edge- or level-triggered Interrupt handler receives interrupts Maskable to ignore or delay some interrupts Interrupt vector to dispatch interrupt to correct handler Based on priority Some nonmaskable Interrupt mechanism also used for exceptions AS 2016 Exceptions 19

Basic x86 interrupts x86 CPU NMI INTR INTA D0 D7 Two interrupt pins: 1. INTR: interrupt request 2. NMI: non-maskable interrupt Fairly typical even on other architectures E.g. ARM, etc. AS 2016 Exceptions 20

x86 interrupts: NMI x86 CPU NMI INTR INTA D0 D7 NMI asserted CPU completes current instruction Issues Exception #2 Always. Cannot be disabled by the processor. Reserved for Major hardware faults, e.g. memory parity error Watchdog timer AS 2016 Exceptions 21

x86 interrupts: NMI NMI INTR Multiple sources of interrupts? INTA Device 1 x86 CPU Device 2 D0 D7 Device n AS 2016 Exceptions 22

x86 interrupts: NMI x86 CPU NMI INTR INTA D0 D7 What caused the NMI? No obvious way to tell: OS (INT 0x2 handler) must poll potential sources For normal use: plenty of devices slow reaction, inefficient Might get a second NMI while first is still being handled! AS 2016 Exceptions 23

x86 interrupts: IRQ x86 CPU NMI INTR INTA D0 D7 Interrupt request Can be disabled by IE status flag (CLI/STI) If enabled, complete current instruction, then: CPU acknowledges using INTA pin Interrupt vector is then supplied on the data bus CPU issues exception # from the vector AS 2016 Exceptions 24

x86 exception vectors 0 Divide error 1 Debug exception 2 Non-maskable interrupt (NMI) 3 Breakpoint 4 Overflow 5 Bounds check 6 Invalid opcode 7 Coprocessor not available 8 Double fault 9 Coprocessor segment overrun (386 or earlier) A Invalid task state segment B Segment not present C Stack fault D General protection fault E Page Fault F Reserved 10 Math fault 11 Alignment check 12 Machine check 13 SIMD floating point exception 14-1F Reserved to Intel AS 2016 20-FF Available for external interrupts Exceptions 25

17.4: Interrupt controllers AS 2016 Exceptions 26

Remaining problems What caused the interrupt? Can tell from the vector, but How does the device know which vector to give? Most early devices just had a single pin (for IRQ) Vector could only be changed using switches or jumpers Led to interrupt conflicts in early PCs What happens when more than one interrupt happens? Should not miss interrupts bad! AS 2016 Exceptions 27

Programmable Interrupt Controllers NMI x86 CPU INTR INTA D0 D7 PIC IRQ0 IRQ1 IRQ<n-1> Device 1 Device 2 Device n AS 2016 Exceptions 28

Programmable Interrupt Controllers Map physical interrupt pins (from device, slot, socket, etc.) to interrupt vectors Mapping picked by the OS flexible Buffer simultaneous interrupts Deliver each interrupt vector separately Won t lose some device s interrupt Prioritize interrupts Allow some device IRQs to interrupt others Again, selected by the OS Selectively mask any individual device s interrupts Useful for high-interrupt rate devices Useful at boot! AS 2016 Exceptions 29

Modern PICs Every processor in a modern PC has a Local APIC Local Advanced Programmable Interrupt Controller PIC APIC LAPIC xapic x2apic Additional capabilities: Inter-processor interrupts Programmable timer Sophisticated interrupt scheduling Etc. Integrated onto same die as the core itself Also: IOxAPIC / MSI-X Off-chip, attached to device I/O interconnect AS 2016 Exceptions 30

LAPIC IOAPIC LAPIC LAPIC IRTE Mapper IOMMU LAPIC LAPIC LAPIC LAPIC Modern PCs (simplified) MSIx device MSIx RT CPU CPU MSI device MSI RT CPU CPU PCI device CPU CPU CPU PCI device PCI device PIR Any OS must correctly discover, configure, and reconfigure all of the green boxes in the right order. AS 2016 Exceptions 31

Exceptions Summary Events that require nonstandard control flow Generated externally (interrupts) or internally (traps and faults) We ll more of these when we look at: Device handling Virtual memory Multiprocessors Operating Systems AS 2016 Exceptions 32