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

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

W4118: interrupt and system call. Junfeng Yang

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

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

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

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

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

W4118: PC Hardware and x86. Junfeng Yang

IA32 Intel 32-bit Architecture

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

Microkernel Construction

Computer Labs: I/O and Interrupts

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

Mechanisms for entering the system

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

x86 architecture et similia

x86 Assembly Tutorial COS 318: Fall 2017

Tutorial 10 Protection Cont.

3. Process Management in xv6

Pre-virtualization internals

Processes (Intro) Yannis Smaragdakis, U. Athens

ECE 485/585 Microprocessor System Design

Assembly Language Lab # 9

Week 11 Programmable Interrupt Controller

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

PC Interrupt Structure and 8259 DMA Controllers

Operating Systems and Protection CS 217

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

Buffer Overflow Attack

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

CS 550 Operating Systems Spring Interrupt

Chapters 2, 3: bits and pieces. Chapters 2 & 3. Chapters 2, 3: bits and pieces. Chapters 2, 3: bits and pieces. Using C. A last word about hardware

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

Microkernel Construction

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

CS 3803 Lab 6 Interrupts Assigned 2/25/10 Point Value: 30

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

MICROPROCESSOR TECHNOLOGY

The Instruction Set. Chapter 5

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

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Assembly Language: Function Calls

Computer Systems Lecture 9

Section 4: Threads and Context Switching

Assembly Language: Function Calls" Goals of this Lecture"

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

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

Assembly Language: Function Calls" Goals of this Lecture"

8086 Interrupts and Interrupt Responses:

1. state the priority of interrupts of Draw and explain MSW format of List salient features of

Hardware OS & OS- Application interface

X86 Stack Calling Function POV

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Basic Execution Environment

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Chapter 12: INTERRUPTS

real-time kernel documentation

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

CS 318 Principles of Operating Systems

Project 1: Bootloader. COS 318 Fall 2015

6.828: Using Virtual Memory. Adam Belay

x86 assembly CS449 Fall 2017

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

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

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

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

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

Interrupts. Chapter 20 S. Dandamudi. Outline. Exceptions

CS 550 Operating Systems Spring System Call

Interrupt handling and context switching

For more notes of DAE

Exceptions. user mode. software should run in supervisor mode Certain features/privileges il are only allowed to code running in supervisor mode

System calls and assembler

An Introduction to x86 ASM

The Purpose of Interrupt

Come and join us at WebLyceum

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Module 3. Embedded Systems I/O. Version 2 EE IIT, Kharagpur 1

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

Machine Programming 3: Procedures

Low Level Programming Lecture 2. International Faculty of Engineerig, Technical University of Łódź

16.317: Microprocessor Systems Design I Fall 2014

Program Exploitation Intro

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Windows Interrupts

CS241 Computer Organization Spring 2015 IA

System Programming. Prof. Dr. Antônio Augusto Fröhlich. Nov 2006

Practical Malware Analysis

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

CS , Fall 2004 Exam 1

Changes made in this version not seen in first lecture:

Transcription:

Homework / Exam Return and Review Exam #1 Reading S&S Extracts 385-393, PIC Data Sheet Machine Projects Start on mp3 (Due Class 19) Labs Continue in labs with your assigned section 1

Interrupts An interrupt acts as a hardware generated function call External versus Internal I/O device generates a signal to processor Processor interrupts software execution Processor executes appropriate interrupt service routine (ISR) for the I/O device ISR returns control to the software that was executing before the interrupt occurred 2

Adding Interrupts to the Hardware Add Programmable Interrupt Controller Chip IRQ line from I/O device to PIC Interrupt line from PIC to CPU Control Bus (M/IO#, W/R#, D/C#) Address Bus x86 CPU Memory I/O IRQ Line Program. Device Data Bus Interrupt Controller Interrupt Line 3

Adding Interrupts to the Hardware Programmable Interrupt Controller: The 8259A chip Each Chip supports eight interrupt request (IRQ) lines Asserts INTR to CPU, responds to resulting INTA# with an 8-bit interrupt vector ( 0xdd ) on the data bus Industry Standard Architecture (ISA) Bus Priority: highest to lowest order is IRQ0-1, IRQ8-15, IRQ3-7 To CPU Master 8259 IRQ0 IRQ1 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Slave 8259 IRQ8 IRQ9 IRQ10 IRQ11 IRQ12 IRQ13 IRQ14 IRQ15 4

Interrupt Handling Software that was executing never knew what hit it execution is suspended until ISR ends Processor does the following: Pushes %eflags, %cs, and %eip on stack (similar to making a function call but it saves more context) Inhibits other interrupts (similar to cli instruction) Initiates an Interrupt Acknowledge Cycle to get IV Uses IV to get address of Interrupt Service Routine Sets %eip to entry point of the ISR 5

Interrupt Acknowledge Cycle CPU Interrupt Acknowledge (INTA#) bus cycle M/IO# = 0, W/R# = 0, D/C# = 0 PIC responds with Interrupt Vector (IV) to CPU IV is an 8 bit number (0 255) equal to the IRQ number plus a constant (0x20 for Linux) passed to the processor on the data bus Processor uses IV as an index into the Interrupt Descriptor Table (IDT) to get entry point for ISR 6

Interrupt Descriptor Table IDT is an array of 8-byte entries (gates) in memory A special register contains the address of the IDT In our Tutor VM systems, the IDT is located in the Tutor memory area At startup, S/W loads that register using instruction: lidt idt_48 # load idt with 0,0x5604c idt_48:.word 0x400 # idt limit=0x400.word 0x604c,0x5 # idt base=0x0005604c 7

Interrupt Gate Descriptor Contents of each Interrupt Gate Descriptor: typedef struct desc_struct { unsigned short addr_lo; /* bits 0-15: handler offset lsbs */ unsigned short selector; /* bits 16-31: selector of handler */ unsigned char zero; /* bits 32-39: all 0 */ unsigned char flags; /* bits 40-47: valid, dpl, type */ unsigned short addr_hi;/* bits 48-63: handler offset msbs */ } Gate_descriptor; An example from Tutor VM memory 25 eb 10 00 00 8e 05 00 ISR address = 0x0005eb25, CS = 0x0010, flags = 0x8e 8

Interrupt Service Routine What does an ISR do to handle interrupt? Saves any additional registers it uses Must make I/O device turn off interrupt signal If it does not infinite loop re-executing the ISR Usually accomplished by reading/writing a port Performs in or out instructions as needed Uses out to send End of Interrupt (EOI) to PIC Restores any additional saved registers Executes iret instruction to return 9

Interrupt Return When the ISR executes iret instruction Processor pops %eip, %cs, and %eflags (Note: Done as a single atomic operation) Popping %eflags may have side effect of re-enabling interrupts (The IF flag bit is in %eflags register) The software that was interrupted resumes its normal execution like nothing happened (It s context has been preserved) 10

Implementing ISR Code Want to write most of ISR code in C, but Can t push/pop scratch registers in C code Can t tell C compiler to generate iret instead of ret Write a small assembly ISR or use inline Assy Push System registers / C compiler scratch registers Call C function for body of ISR code Pop System Registers / C compiler scratch registers Execute iret 11

Assembly Language ISR Wrapper.text.globl _irq0inthand KERNEL_DS = 0x18 # Example for MP3 IRQ0 timer chip interrupt # Linux kernel data segment value _irq0inthand: cld # D bit gets restored to old val by iret push %es # in case user code changes data segments push %ds pushl %eax # save C scratch regs pushl %edx pushl %ecx movl $KERNEL_DS, %edx mov %dx, %ds # make sure our data segments are in use now mov %dx, %es 12

Assembly Language ISR Wrapper call _irq0inthandc popl %ecx popl %edx popl %eax # call C interrupt handler # restore C scratch registers pop %ds pop %es iret # restore data segment registers # return to interrupted background code /* C interrupt Handler Code for MP3 IRQ0 timer chip interrupt */ void irq0inthandc() { pic_end_int(); /* notify PIC that its part is done */ tickcount++; /* count the tick in global variable */ } 13

In-line Assembly ISR Allows us to write MP3 IRQ0 ISR without need for an assembly language wrapper function Store the address of irq0inthandc ISR directly in the IDT instead of address of irq0inthand( ) Compare to the existing IRQ0 ISR code in mp3 and previous Assembly Language ISR Wrapper Since our exercises do not change the Direction Flag and the CS or ES registers, we don t need to save and restore that part of the context 14

In-line Assembly Code Modified MP3 C interrupt handler code for IRQ0 void irq0inthandc(void) { /* hardware pushes 3 registers before entry here */ /* compiler generated code adds stack frame */ asm("pushl %eax"); /* save scratch registers */ asm("pushl %ecx"); asm("pushl %edx"); pic_end_int(); /* notify PIC that its part is done */ tickcount++; /* count the tick in global var */ 15

In-Line Assembly Code State of the Stack during irq0inthandc execution: Push for call to pic_end_int %esp Pushed by in-line assembly code %ebp Pushed by compiler Stack frame code Pushed by HW interrupt * * * %edx %ecx %eax %ebp %eip %cs %eflags Background Stack State Pop from ret in pic_end_int Popped by in-line assembly code Popped by in-line assembly iret 16

In-line Assembly Code asm("popl %edx"); /* restore scratch registers */ asm("popl %ecx"); asm("popl %eax"); asm("movl %ebp, %esp"); /* undo stack frame */ asm("popl %ebp"); asm("iret"); /* return from interrupt */ /* compiler generated ret instruction is never reached */ } 17

Advantage of External Interrupts Processor can start an I/O device and go off to do other useful work not wait for it When I/O device needs attention, the ISR for that I/O device is invoked automatically Example: When the COM1 THRE goes to the 1 state, the COM1 port ISR is invoked to load another ASCII character into the data port and returns 18

Exceptions and SW Interrupts Internal not based on external I/O device Exceptions Possible side effects of executing an instruction Divide by zero Execute an exception ISR Software Interrupts Instruction int $n deliberately placed in the code System Call Execute system call (e.g. Linux OS) Tutor Breakpoint (int $3) Return to debugger 19