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

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

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

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

Mechanisms for entering the system

Microkernel Construction

Microkernel Construction

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

Tutorial 10 Protection Cont.

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

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

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

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

IA32 Intel 32-bit Architecture

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

The Instruction Set. Chapter 5

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

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

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

The Purpose of Interrupt

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

3. Process Management in xv6

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

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

CS 550 Operating Systems Spring Interrupt

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

MICROPROCESSOR TECHNOLOGY

W4118: PC Hardware and x86. Junfeng Yang

Protection and System Calls. Otto J. Anshus

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture Included elements of the IA-64 bit

Complex Instruction Set Computer (CISC)

Windows Kernel Internals Traps, Interrupts, Exceptions

Buffer Overflow Attack

BASIC INTERRUPT PROCESSING

x86 Assembly Tutorial COS 318: Fall 2017

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

Systems Architecture I

Hardware and Software Architecture. Chapter 2

PROTECTION CHAPTER 4 PROTECTION

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam

Reverse Engineering II: The Basics

Assembly Language Lab # 9

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

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

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

Basic Execution Environment

W4118: interrupt and system call. Junfeng Yang

Assembly Language. Lecture 2 x86 Processor Architecture

Introduction to IA-32. Jo, Heeseung

Chapter 8 PC Peripheral Chips - Pt 3 Interrupts

INTRODUCTION TO IA-32. Jo, Heeseung

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

The Microprocessor and its Architecture

Computer Labs: I/O and Interrupts

Interrupts. Chapter 20 S. Dandamudi. Outline. Exceptions

History of the Intel 80x86

Chapter 11. Addressing Modes

IA32/Linux Virtual Memory Architecture

Computer Organization (II) IA-32 Processor Architecture. Pu-Jen Cheng

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

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

AMD-K5. Software Development Guide PROCESSOR

Computer System Architecture

Program Exploitation Intro

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

Chapter 12: INTERRUPTS

MICROPROCESSOR ARCHITECTURE

6.828: Using Virtual Memory. Adam Belay

Computer Systems Lecture 9

SYSC3601 Microprocessor Systems. Unit 2: The Intel 8086 Architecture and Programming Model

x86 architecture et similia

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

Assembly Language for Intel-Based Computers, 4 th Edition. Kip R. Irvine. Chapter 2: IA-32 Processor Architecture

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

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

Chapter 12: Interrupts

Section 4: Threads and Context Switching

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

Assembler Programming. Lecture 2

IA-32 Architecture COE 205. Computer Organization and Assembly Language. Computer Engineering Department

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

Lecture Dependable Systems Practical Report Software Implemented Fault Injection. July 31, 2010

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

Processes (Intro) Yannis Smaragdakis, U. Athens

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

UMBC. 1 (Feb. 9, 2002) seg_base + base + index. Systems Design & Programming 80x86 Assembly II CMPE 310. Base-Plus-Index addressing:

Writing an Operating System Jonas Freiknecht

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

MOV Move INSTRUCTION SET REFERENCE, A-M. Description. Opcode Instruction 64-Bit Mode. Compat/ Leg Mode

Computer Organization & Assembly Language Programming

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture. Chapter Overview.

Memory Models. Registers

Moodle WILLINGDON COLLEGE SANGLI (B. SC.-II) Digital Electronics

ECE 485/585 Microprocessor System Design

Operating Systems and Protection CS 217

Reverse Engineering II: The Basics

6/17/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

x86 assembly CS449 Fall 2017

16.317: Microprocessor Systems Design I Spring 2015

Windows Interrupts

Transcription:

+ Projects: Developing an OS Kernel for x86 Low-Level x86 Programming: Exceptions, Interrupts, and Timers + Overview! Handling Intel Processor Exceptions: the Interrupt Descriptor Table (IDT)! Handling Hardware Device Interrupts the old fashioned style: IRQs and the 8259 Programmable Interrupt Controller.! Example Interrupt Handlers:! The Programmable Interval Timer! References: www.osdever.net! Brandon Friesen s beginner OS development tutorial by warmaster199.! others 1

+ Interrupt Descriptor Table! Interrupt Vector Table x86-style:! processor exceptions, hardware interrupts, software interrupts IDT! 256 entries: Each entry contains address (segment id and offset) of interrupt handler (interrupt service routine). exception handler! The first 32 entries reserved for processor exceptions (division by zero, page fault, etc.)! Hardware interrupts can be mapped to any of the other entries using the Programmable Interrupt Controller (e.g. 8259 PIC, see later) interrupt handler + Processor Exceptions Exception # Description Error Code? 0 Division By Zero Exception No 1 Debug Exception No 2 Non Maskable Interrupt Exception No 3 Breakpoint Exception No 4 Into Detected Overflow Exception No 5 Out of Bounds Exception No 6 Invalid Opcode Exception No 7 No Coprocessor Exception No 8 Double Fault Exception Yes 9 Coproc. Segment Overrun Exception No 10 TSS Exception Yes 11 Segment Not Present Exception Yes 12 Stack Fault Exception Yes 13 General Protection Fault Exception Yes 14 Page Fault Exception Yes 15 Unknown Interrupt Exception No 16 Coprocessor Fault Exception No 17 Alignment Check Exception (486+) No 18 Machine Check Exception (Pentium/586+) No 19 to 31 Reserved Exceptions No 2

+ Handling Interrupts/Exceptions global _isr0 global _isr1 global _isr2... global _isr8 global _isr30 global _isr31 ; 0: Divide By Zero Exception _isr0: push byte 0 ; push dummy push byte 0 ; push interrupt no jmp isr_common_stub ; 8: Double Fault Exception (Error Code!) _isr8: ; don t push dummy! push byte 8 jmp isr_common_stub /* This defines what the stack looks like when the exception/interrupt reaches the exception dispatcher. */ typedef struct regs { unsigned int gs, fs, es, ds; /* pushed in common stub */ unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax; /* pushed by */ unsigned int int_no, err_code; /* pushed in _isrxx */ unsigned int eip, cs, eflags, useresp, ss; /* pushed by processor */ REGS; extern _dispatch_exception ; The common ISR stub. isr_common_stub: mov eax, _dispatch_exc add esp, 8 ; pop code and int no + High-Level Int/Exc Handler extern _dispatch_exception ; The common ISR stub. isr_common_stub: mov eax, _dispatch_exception add esp, 8 ; pop code and int no /* Exception handlers are functions that take a pointer to a REGS structure as input and return void. */ typedef void (* ExceptionHandler)(REGS*); /* List of registered exception handlers. */ static ExceptionHandler handler_table[exception_table_size]; void dispatch_exception(regs * _r) { unsigned int exc_no = _r->int_no; /* get exception no */ ExceptionHandler handler = handler_table[exc_no]; if (handler) /* Is a handle registered? */ handler(_r); /* If so, fire it up! */ 3

+ Initializing the IDT extern void isr0(); extern void isr1(); /* */ extern void isr31(); void init_exception_dispatcher() { /* Add any new ISRs to the IDT here. */ idt_set_gate( 0, (unsigned) isr0, 0x08, 0x8E); idt_set_gate( 1, (unsigned) isr1, 0x08, 0x8E); /* */ idt_set_gate(31, (unsigned)isr31, 0x08, 0x8E); /* Initialize high-level exception handler */ for(int i = 0; i < EXCEPTION_TABLE_SIZE; i++) { handler_table[i] = NULL; For details of idt_set_gate() and installation of IDT check out the code (file idt.c ). + Hardware Interrupts: The Programmable Interrupt Controller (PIC)! Interrupt control on PC/AT by cascaded (master/slave) pair of 8259 PICs.! Interrupts (IRQs) are mapped to IDT entries. (Typically inconvenient ones, can be remapped.)! Interrupt service routine must send EOI (end-of-interrupt) signal to PIC (to both PICs if interrupt from slave) 4

+ Assigned Interrupt Lines in the PIC! Master 8259! IRQ0 Intel 8253 or Intel 8254 PIT, aka the system timer! IRQ1 Intel 8042 keyboard controller! IRQ2 not assigned in PC/XT; cascaded to slave 8259 INT line in PC/AT! IRQ3 8250 UART serial port COM2 and COM4! IRQ4 8250 UART serial port COM1 and COM3! IRQ5 hard disk controller in PC/XT; Intel 8255 parallel port LPT2 in PC/AT! IRQ6 Intel 82072A floppy disk controller! IRQ7 Intel 8255 parallel port LPT1 / spurious interrupt! Slave 8259 (PC/AT and later only)! IRQ8 real-time clock (RTC)! IRQ9 no common assignment! IRQ10 no common assignment! IRQ11 no common assignment! IRQ12 Intel 8042 PS/2 mouse controller! IRQ13 math coprocessor! IRQ14 hard disk controller 1! IRQ15 hard disk controller 2 + Handling Hardware Interrupts global _irq0 /* */ global _irq15 ; 32: IRQ0 start at IDT entry 32 _irq0: push byte 0 push byte 32 jmp irq_common_stub ; 33: IRQ1 _irq1: push byte 0 push byte 33 jmp irq_common_stub extern _dispatch_interrupt /* similar to ISRs */ irq_common_stub: mov eax, _dispatch_interrupt add esp, 8 5

+ Hardware Interrupts: High Level extern _dispatch_interrupt /* similar to ISRs */ irq_common_stub: mov eax, _dispatch_interrupt add esp, 8 void dispatch_interrupt(regs * _r) { unsigned int int_no = _r->int_no - IRQ_BASE; InterruptHandler handler = handler_table[int_no]; if (!handler) { /* --- NO DEFAULT HANDLER REGISTERED. */ abort(); handler(_r); /* This is an interrupt that was raised by the interrupt controller. We need to send and end-of-interrupt (EOI) signal to the controller after the interrupt has been handled. */ /* Check if the interrupt was generated by the slave interrupt controller. If so, send an End-of-Interrupt (EOI) message to the slave controller. */ if (generated_by_slave_pic(int_no)) { /* i.e. int_no < 8 */ outportb(0xa0, 0x20); /* Send an EOI message to the master interrupt controller. */ outportb(0x20, 0x20); + Example: Periodic Timer /* timer interrupt handler*/ void SimpleTimer::handler(REGS *r) { /* Increment our "ticks" count */ ticks++; /* Whenever a second is over, we update counter. */ if (ticks >= hz ) { seconds++; ticks = 0; Console::puts("One second has passed\n"); void main() { GDT::init(); Console::init(); IDT::init(); init_exception_dispatcher(); IRQ::init(); init_interrupt_dispatcher(); /* Set the interrupt frequency for the simple timer. */ SimpleTimer::init(); /* Preferably set this before installing the timer handler! */ SimpleTimer::set_frequency(100); void SimpleTimer::set_frequency(int _hz) { /* timer ticks to 10ms. */ hz = _hz; /* Remember the frequency. reg_int_handler(0, */ SimpleTimer::handler); int divisor = 1193180 / _hz; /* The input clock runs at 1.19MHz*/ outportb(0x43, 0x34); /* Set command byte to be 0x36. asm */ volatile ("sti"); outportb(0x40, divisor & 0xFF); /* Set low byte of divisor. */ outportb(0x40, divisor >> 8); /* Set high byte of divisor. Console::puts("Hello */ World!\n"); for (;;); 6