W4118: interrupt and system call. Junfeng Yang

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

CS 550 Operating Systems Spring System Call

CS 550 Operating Systems Spring Interrupt

Homework / Exam. Return and Review Exam #1 Reading. Machine Projects. Labs. S&S Extracts , PIC Data Sheet. Start on mp3 (Due Class 19)

Interrupts & System Calls

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

Mechanisms for entering the system

Microkernel Construction

CIS Operating Systems CPU Mode. Professor Qiang Zeng Spring 2018

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

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

IA32 Intel 32-bit Architecture

Changes made in this version not seen in first lecture:

Hardware OS & OS- Application interface

6.828: Using Virtual Memory. Adam Belay

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

Tutorial 10 Protection Cont.

Protection and System Calls. Otto J. Anshus

Interrupts and System Calls

CS3210: Isolation Mechanisms

Microkernel Construction

Interrupts and System Calls

Interrupts and System Calls. Don Porter CSE 506

CS 4414 Operating System Introduction

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

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

PROTECTION CHAPTER 4 PROTECTION

Processes. Johan Montelius KTH

A process. the stack

The Purpose of Interrupt

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

COP4610: Operating Systems Project 1

3. Process Management in xv6

Operating Systems. System calls. Guillaume Salagnac. Fall Insa-Lyon IST Semester

W4118: PC Hardware and x86. Junfeng Yang

Interrupts and System Calls

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

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

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

Chapter 12: INTERRUPTS

W4118: virtual machines

COS 318: Operating Systems

Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

CS 5460/6460 Operating Systems

7/19/2013. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to: Chapter Objectives 12 1 BASIC INTERRUPT PROCESSING

Chapter 12: Interrupts

CanSecWest Nicolás A. Economou Andrés Lopez Luksenberg

Intel VMX technology

Windows Interrupts

COS 318: Operating Systems

Pre-virtualization internals

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

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

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

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

Systems Programming and Computer Architecture ( ) Timothy Roscoe

x86 architecture et similia

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).

BASIC INTERRUPT PROCESSING

Protection. OS central role. Fundamental to other OS goals. OS kernel. isolation of misbehaving applications. Relaibility Security Privacy fairness

CSE 153 Design of Operating Systems

MICROPROCESSOR MICROPROCESSOR ARCHITECTURE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

EEE3052: Introduction to Operating Systems. Fall Project #1

What You Need to Know for Project Three. Dave Eckhardt Steve Muckle

Anne Bracy CS 3410 Computer Science Cornell University

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

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

Anne Bracy CS 3410 Computer Science Cornell University

Processes (Intro) Yannis Smaragdakis, U. Athens

Syscalls, exceptions, and interrupts, oh my!

Information Security II Prof. Kamakoti Department of Computer Science and Engineering Indian Institute of Technology, Madras

CSC369 Lecture 2. Larry Zhang

Embedded Systems Programming

Summer 2003 Lecture 27 07/28/03

CSC369 Lecture 2. Larry Zhang, September 21, 2015

ECE 485/585 Microprocessor System Design

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

Ausgewählte Betriebssysteme. Anatomy of a system call

Handling of Interrupts & Exceptions

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

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

The Kernel Abstraction

EE458 - Embedded Systems Exceptions and Interrupts

MICROPROCESSOR MICROPROCESSOR ARCHITECTURE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

CHAPTER 6 INTERRUPT AND EXCEPTION HANDLING

MICROPROCESSOR ALL IN ONE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

Interrupt Handler: Top Half. Changwoo Min

The Kernel Abstrac/on

The Kernel Abstraction. Chapter 2 OSPP Part I

int $0x32 // call interrupt number 50

UNIT- 5. Chapter 12 Processor Structure and Function

Interrupts. Chapter 20 S. Dandamudi. Outline. Exceptions

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

Background: Operating Systems

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

Introduction to the Process David E. Culler CS162 Operating Systems and Systems Programming Lecture 2 Sept 3, 2014

Operating Systems ECE344

[537] Virtual Machines. Tyler Harter

CSE 153 Design of Operating Systems Fall 18

iapx86 Protection Electronic Computers M

Transcription:

W4118: interrupt and system call Junfeng Yang

Outline Motivation for protection Interrupt System call 2

Need for protection Kernel privileged, cannot trust user processes User processes may be malicious or buggy Must protect User processes from one another Kernel from user processes 3

Hardware mechanisms for protection Dual model of operation Privileged (+ non-privileged) operations in kernel mode Non-privileged operations in user mode Memory protection Segmentation and paging E.g., kernel sets page table when creating process Timer interrupt Kernel periodically gets back control 4

What operations are privileged? Read raw keyboard input Call printf() Call write() Write global descriptor table Divide by 0 Set timer interrupt handler Set segment registers Load cr3 5

x86 protection modes Four modes (0-3), but often only 0 & 3 used Kernel mode: 0 User mode: 3 Ring 0, Ring 3 Segment has Descriptor Privilege Level (DPL) DPL of kernel code and data segments: 0 DPL of user code and data segments: 3 Current Privilege Level (CPL) = current code segment s DPL Can only access data segments when CPL <= DPL 6

Outline Motivation for protection Interrupt System call 7

OS: event driven Events causing mode switches System calls: issued by user processes to request system services Exceptions: illegal instructions (e.g., division by 0) Interrupts: raised by devices to get OS attention Often handled using same hardware mechanism: interrupt Also called trap 8

Interrupt view of CPU while (fetch next instruction) { run instruction; if (there is an interrupt) { process interrupt } } 9

x86 interrupt view while (fetch next instruction) { run instruction; if (there is an interrupt) { switch to kernel stack if necessary save CPU context and error code if any find OS-provided interrupt handler jump to handler restore CPU context when handler returns } } Q1: how does hardware find OS-provided interrupt handler? Q2: why switch stack? Q3: what CPU context to save and restore? Q4: what does handler do? 10

Q1: how to find interrupt handler? Hardware maps interrupt type to interrupt number OS sets up Interrupt Descriptor Table (IDT) at boot Also called interrupt vector IDT is in memory Each entry is an interrupt handler OS lets hardware know IDT base Defines all kernel entry points Hardware finds handler using interrupt number as index into IDT handler = IDT[intr_number] 11

x86 interrupt hardware (legacy) IRQs PIC intr # INTR CPU idtr 0 Bus IDT intr # Interrupt handler Mask points 255 12

x86 interrupt numbers Total 256 number [0, 255] Intel reserved first 32, OS can use 224 0: divide by 0 1: debug (for single stepping) 2: non-maskable interrupt 3: breakpoint 14: page fault 64: system call in xv6 xv6 traps.h 13

x86 interrupt descriptor table Interrupt gate descriptor Code segment selector and offset of handler Descriptor Privilege Level (DPL) To invoke int x in software, must have CPL <= DPL Trap or exception flag. If exception, hardware clears the IF flag in EFLAGS to disable further maskable interrupts lidt instruction loads CPU with IDT base xv6 Handler entry points: vector.s Interrupt gate format: SETGATE in mmu.h IDT initialization: tvinit() & lidt() in trap.c 14

Q2: why switch stack? Cannot trust stack of user process! x86 hardware switches stack when interrupt handling requires user-kernel mode switch That is, when CPL <= DPL of handler s code segment Where to find kernel stack? task gate descriptor has SS and ESP for interrupt ltr loads CPU with task gate descriptor xv6 uses current process s kernel stack switchuvm() in vm.c 15

Q3: what CPU context to save and restore? ESP SS ESP EFLAGS CS EIP Err code when switch stack for some exceptions x86 saves SS, ESP, EFLAGS, CS, EIP, Err code Restored by iret OS can save more context 16

Q4: what does interrupt handler do? Typical steps xv6 Assembly to save additional CPU context Invoke C handler to process interrupt E.g., communicate with I/O devices Invoke kernel scheduler Assembly to restore CPU context and return Interrupt handler entries: vector.s Saves & restore additional CPU context: trapasm.s C handler: trap.c, struct trapframe in x86.h 17

Interrupt v.s. Polling Instead for device to interrupt CPU, CPU can poll the status of device Intr: I want to see a movie. Poll: for(each week) { Do you want to see a movie? } Good or bad? For mostly-idle device? For busy device? 18

Outline Motivation for protection Interrupt System call 19

System call User processes cannot perform privileged operations themselves Must request OS to do so on their behalf by issuing system calls OS must validate system call parameters 20

System call dispatch 1. Kernel assigns system call type a system call number 2. Kernel initializes system call table, mapping system call number to functions implementing the system call Also called system call vector 3. User process sets up system call number and arguments 4. User process runs int X 5. Hardware switches to kernel mode and invokes kernel s interrupt handler for X (interrupt dispatch) 6. Kernel looks up system call table using system call number 7. Kernel invokes the corresponding function 8. Kernel returns by running iret (interrupt return) 21

xv6 system call dispatch User mode write(fd, buf, sz) movl $SYS_write, %eax int 64 ret // usys.s User program kernel mode IDT syscall() { fn = syscalls[%eax] } // trap.c syscalls table sys_write 64 syscall sys_write( ) { // do real work } //sysfile.c 22

System call parameter passing Typical methods Pass via registers (e.g., Linux) Pass via user-mode stack (e.g., xv6) Pass via designated memory region xv6 system call parameter passing Arguments pushed onto user stack based on gcc calling convention Kernel function uses special routines to fetch these arguments syscall.c Why? 23

xv6 system call naming convention Usually a library function foo() will do some work and then call a system call sys_foo() sys_foo() implemented in sys*.c Often wrappers to foo() in kernel System call number for foo() is SYS_foo syscalls.h All system calls begin with sys_ 24

Tracing system calls Use the strace command (man strace for info) Linux has a powerful mechanism for tracing system call execution for a compiled application Output is printed for each system call as it is executed, including parameters and return codes ptrace() system call is used to implement strace Also used by debuggers (breakpoint, singlestep, etc) Use the ltrace command to trace dynamically loaded library calls 25