Interrupts & System Calls

Similar documents
Interrupts and System Calls

Interrupts and System Calls

Interrupts and System Calls

Interrupts and System Calls. Don Porter CSE 506

W4118: interrupt and system call. Junfeng Yang

CS 550 Operating Systems Spring Interrupt

CS 550 Operating Systems Spring System Call

CSE 153 Design of Operating Systems

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

Mechanisms for entering the system

William Stallings Computer Organization and Architecture. Chapter 11 CPU Structure and Function

System Calls. A contract between processes and the operating system. Michael E. Locasto. Department of Computer Science UofC CPSC 457

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

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

CSE 153 Design of Operating Systems Fall 18

Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

Anne Bracy CS 3410 Computer Science Cornell University

Processes. Johan Montelius KTH

CPU Structure and Function. Chapter 12, William Stallings Computer Organization and Architecture 7 th Edition

A process. the stack

UNIT- 5. Chapter 12 Processor Structure and Function

Protection and System Calls. Otto J. Anshus

Syscalls, exceptions, and interrupts, oh my!

Overview. This Lecture. Interrupts and exceptions Source: ULK ch 4, ELDD ch1, ch2 & ch4. COSC440 Lecture 3: Interrupts 1

Hardware OS & OS- Application interface

CIS Operating Systems CPU Mode. Professor Qiang Zeng Spring 2018

Anne Bracy CS 3410 Computer Science Cornell University

Computer Architecture Background

Windows Interrupts

Hakim Weatherspoon CS 3410 Computer Science Cornell University

CSE 451 Autumn Final Solutions mean 77.53, median 79, stdev 12.03

Operating systems offer processes running in User Mode a set of interfaces to interact with hardware devices such as

Microkernel Construction

CS 104 Computer Organization and Design

Even coarse architectural trends impact tremendously the design of systems

Intel VMX technology

Even coarse architectural trends impact tremendously the design of systems. Even coarse architectural trends impact tremendously the design of systems

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Operating Systems. Operating System Structure. Lecture 2 Michael O Boyle

CSE 451: Operating Systems Winter Module 2 Architectural Support for Operating Systems

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Traps, Exceptions, System Calls, & Privileged Mode

COS 318: Operating Systems. Overview. Prof. Margaret Martonosi Computer Science Department Princeton University

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

+ Overview. Projects: Developing an OS Kernel for x86. ! Handling Intel Processor Exceptions: the Interrupt Descriptor Table (IDT)

Process Scheduling Queues

COS 318: Operating Systems

Xen and the Art of Virtualization. CSE-291 (Cloud Computing) Fall 2016

Lecture 2: Architectural Support for OSes

William Stallings Computer Organization and Architecture 8 th Edition. Chapter 12 Processor Structure and Function

Operating system organizaton

Concurrent programming: Introduction I

CPU Structure and Function

CS5460: Operating Systems

CSC 2405: Computer Systems II

Background: Operating Systems

Last time: introduction. Networks and Operating Systems ( ) Chapter 2: Processes. This time. General OS structure. The kernel is a program!

Microkernel Construction

x86 segmentation, page tables, and interrupts 3/17/08 Frans Kaashoek MIT

Today: Computer System Overview (Stallings, chapter ) Next: Operating System Overview (Stallings, chapter ,

Inf2C - Computer Systems Lecture 16 Exceptions and Processor Management

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Tutorial 10 Protection Cont.

9/19/18. COS 318: Operating Systems. Overview. Important Times. Hardware of A Typical Computer. Today CPU. I/O bus. Network

COS 318: Operating Systems

The Kernel Abstraction

Distributed Systems Operation System Support

3. Process Management in xv6

Embedded Systems Programming

Overhead Evaluation about Kprobes and Djprobe (Direct Jump Probe)

238P: Operating Systems. Lecture 5: Address translation. Anton Burtsev January, 2018

CSCE Introduction to Computer Systems Spring 2019

CSE 120 Principles of Operating Systems

Allocating Memory. Where does malloc get memory? See mmap.c 1-2

CSC369 Lecture 2. Larry Zhang

IA32 Intel 32-bit Architecture

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Part I. X86 architecture overview. Secure Operating System Design and Implementation x86 architecture. x86 processor modes. X86 architecture overview

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

CSE 120 Principles of Operating Systems

CS510 Operating System Foundations. Jonathan Walpole

Process Time. Steven M. Bellovin January 25,

CS 5460/6460 Operating Systems

Embedded Systems Dr. Santanu Chaudhury Department of Electrical Engineering Indian Institute of Technology, Delhi

Operating Systems ECE344

143A: Principles of Operating Systems. Lecture 6: Address translation. Anton Burtsev January, 2017

Linux and Xen. Andrea Sarro. andrea.sarro(at)quadrics.it. Linux Kernel Hacking Free Course IV Edition

Lec 22: Interrupts. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

Traps and Faults. Review: Mode and Space

Operating System Architecture. CS3026 Operating Systems Lecture 03

Introduction to System Programming

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

143A: Principles of Operating Systems. Lecture 5: Address translation. Anton Burtsev October, 2018

Homework. Reading. Machine Projects. Labs. Intel 8254 Programmable Interval Timer (PIT) Data Sheet. Continue on MP3

Processes (Intro) Yannis Smaragdakis, U. Athens

Architecture and OS. To do. q Architecture impact on OS q OS impact on architecture q Next time: OS components and structure

CPSC 213. Introduction to Computer Systems. The Operating System. Unit 2e

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Computer Architecture and OS. EECS678 Lecture 2

Lecture 7. Xen and the Art of Virtualization. Paul Braham, Boris Dragovic, Keir Fraser et al. 16 November, Advanced Operating Systems

Transcription:

Interrupts & System Calls Nima Honarmand

Previously on CSE306 Open file hw1.txt App Ok, here s handle App 4 App Libraries Libraries Libraries User System Call Table (350 1200) Supervisor Kernel Hardware

Regular Control Flow Regular instruction flow in a processor Fetch the instruction pointed to by ip (instruction pointer) register Execute the current instruction Increment ip to point to the next instruction If current inst is a jump, branch or call, set ip to its target instead of incrementing This is called regular control flow because the program itself determines the next instruction at any step Instruction flow logically follows the course code

Regular Control Flow ip x = 2, y = true if (y) { x /= 2; printf(x); void printf(va_args) { } //... } //... Regular control flow: branches and calls (logically follows source code)

Irregular Control Flow Some times, due to special events, control has to be transferred to somewhere outside the program Since the program does not determine the target in this case, we call it irregular control flow Three cases in an OS: External interrupt: caused by a hardware device, e.g., timer ticks, network card interrupts Trap: Explicitly caused by the current execution, e.g., a system call Exception (or Fault): Implicitly caused by the current execution, e.g., a page fault or a device-by-zero fault

External Interrupt Example Stack Stack SP Disk Interrupt! SP if (x) { printf( Boo );... printf(va_args ){... IP IP User Kernel Disk_handler (){... }

How to Handle? Five general steps 1) Transfer control to a pre-specified instruction in the kernel code Who should specify this location? 2) Save current thread s context on the kernel stack Why? 3) Execute a service routine to handle the situation 4) Restore the current thread context 5) Return to the interrupted code, right after the last executed instruction

How to Handle? External interrupts, traps and exceptions can all use the same five-step procedure So they do: Intel provides a single mechanism to handle all of them We use the general term interrupt to refer to all of them, unless stated otherwise

How it works: Hardware

Interrupt Number (Vector) Each interrupt identified a number indicating its type E.g., in x86, 14 is a page fault, 3 is a debug breakpoint This number is the index into an Interrupt Descriptor Table (IDT) stored in memory

x86 Interrupt Overview Support 256 interrupts (assigned an index from 0-255) #0-31 are for processor interrupts; generally fixed by Intel E.g., 14 is always for page faults 32-255 are software configured 32-47 are for device interrupts (IRQs) in xv6 Most device s IRQ line can be configured Look Chapter 4 of Bovet and Cesati for more details xv6 uses #64 (0x40) for its system call Linux uses #128 (0x80) for its system call

x86/xv6 Interrupts Device IRQs 64 = xv6 System Call 128 = Linux System Call 0 31 47 255 Pre-defined by x86 OS Configurable

Traps (Software Interrupts) In x86, the int <num> instruction allows software to raise an interrupt So in an xv6 user-mode program, if you see int 0x40, it s a system call OS sets ring level required to raise an interrupt Generally, user programs can t manually issue an int 14 (page fault) An unauthorized int instruction causes a General Protection (#GP) fault Interrupt #13

How Is This Configured? Kernel creates an array of Interrupt Descriptors in memory, called Interrupt Descriptor Table, or IDT Can be anywhere in memory Pointed to by special processor register (idtr) Entry 0 configures interrupt 0, and so on 0 31 47 255 idtr

Interrupt Descriptor Code segment selector Almost always the same (kernel code segment) Address of the code to run Privilege Level (Ring) What is the minimum privilege level that can invoke the interrupt (using int instruction) Present bit disable unused interrupts And a bunch of other stuff

idtr IDT Example: Page Fault 0 31 47 255 14 (page fault) Code Segment: Kernel Code Segment Offset: &page_fault_handler Ring: 0 // user code may not raise this exception Present: 1

idtr IDT Example: xv6 Syscall 0 31 64 255 64 (syscall) Code Segment: Kernel Code Segment Offset: &syscall_handler Ring: 3 // user code may raise this exception Present: 1

x86 Interrupt Descriptors x86 interrupt descriptors support many other (legacy) features that are rarely used Makes their working and in-memory layout a bit confusing Look at the architecture manual for more details

xv6 code review Five general steps 1) Transfer control to a pre-specified instruction in the kernel code 2) Save current thread s context on the kernel stack 3) Execute a service routine to handle the situation 4) Restore the current thread context 5) Return to the interrupted code, right after the last executed instruction Read the xv6-book (chapter 3) for a detailed review.

How it works: Software

xv6 code review Five general steps 1) Transfer control to a pre-specified instruction in the kernel code 2) Save current thread s context on the kernel stack 3) Execute a service routine to handle the situation 4) Restore the current thread context 5) Return to the interrupted code, right after the last executed instruction Read the xv6-book (chapter 3) for a detailed review.

System Calls

System Call Interrupt System calls issued using int instruction int 0x40 in xv6 int 0x80 in Linux Dispatch routine is just an interrupt handler System calls are arranged in a table See syscall.h and syscall.c in xv6 Program selects the one it wants by placing index in eax register before executing the int instruction Arguments go in the other registers or on the stack, as specified by the OS Return value goes in eax

How many system calls? Linux exports about 350 system calls Windows exports about 400 system calls for core APIs, and another 800 for GUI methods

xv6 code review System call table Remember, you will add your very own system call in Lab 1! Again, Read the xv6-book (chapter 3) for a detailed review.

New System Call Instructions (1) Around Pentium 4 era (2000): Processors got very deeply pipelined Pipeline stalls/flushes became very expensive Cache misses can cause pipeline stalls System calls took twice as long from Pentium 3 to Pentium 4 Why? IDT entry may not be in the cache Different permissions constrain instruction reordering

New System Call Instructions (2) Idea: what if we cache the IDT entry for a system call in a special CPU register? No more cache misses for the IDT! Maybe we can also do more optimizations Assumption: system calls are frequent enough to be worth the transistor budget to implement this

AMD: syscall & sysret These instructions uses an MSR (machine specific registers) to store syscall entry point and code segment A drop-in replacement for int 0x80 Everyone loved it and adopted it wholesale Even Intel! Intel later added its own instructions sysenter and sysexit