The K Project Descriptor Table Interrupt and Exception Handling - - PowerPoint PPT Presentation

the k project
SMART_READER_LITE
LIVE PREVIEW

The K Project Descriptor Table Interrupt and Exception Handling - - PowerPoint PPT Presentation

The K Project LSE Team Introduction Interrupt The K Project Descriptor Table Interrupt and Exception Handling Interrupt Request Keyboard Timer LSE Team Conclusion EPITA May 06, 2019 LSE Team (EPITA) The K Project May 06, 2019 1 /


slide-1
SLIDE 1

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

The K Project

Interrupt and Exception Handling LSE Team

EPITA

May 06, 2019

LSE Team (EPITA) The K Project May 06, 2019 1 / 37

slide-2
SLIDE 2

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Interrupt and Exception Handling

Exception : Synchronous with program execution (e.g. division by zero, accessing an invalid address) Interrupt : Asynchronous with program execution. Generated by devices external to the CPU

LSE Team (EPITA) The K Project May 06, 2019 2 / 37

slide-3
SLIDE 3

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Interrupt Descriptor Table

Figure: IDT

LSE Team (EPITA) The K Project May 06, 2019 3 / 37

slide-4
SLIDE 4

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Interrupt Descriptor Table Register

Figure: IDTR

LSE Team (EPITA) The K Project May 06, 2019 4 / 37

slide-5
SLIDE 5

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Load IDT

idt_r idtr; idtr.base = idt; /* idt base address */ idtr.limit = sizeof(idt) - 1; /* idt size - 1 */ __asm__ volatile("lidt %0\n" : /* no output */ : "m" (idtr) : "memory");

LSE Team (EPITA) The K Project May 06, 2019 5 / 37

slide-6
SLIDE 6

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

x86 Exceptions

Int. Description Divide by 0 1 Debug 2 NMI 3 Breakpoint 4 Overflow 5 Bound Range Exceeded 6 Invalid Opcode

LSE Team (EPITA) The K Project May 06, 2019 6 / 37

slide-7
SLIDE 7

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

x86 Exceptions

Int. Description 7 Device Not Available 8 Double Fault 9 Coprocessor 10 Invalid TSS 11 Segment not present 12 Stack Segment Fault 13 General Protection 14 Page Fault

LSE Team (EPITA) The K Project May 06, 2019 7 / 37

slide-8
SLIDE 8

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

x86 Exceptions and Interrupts

Int. Description 15 (Intel reserved) 16 x87 FPU (Math Fault) 17 Alignment Check 18 Machine Check 19 SMD Floating Point Ex. 20 Virtualization Ex. 21-31 (Intel reserved) 32-255 User Defined

LSE Team (EPITA) The K Project May 06, 2019 8 / 37

slide-9
SLIDE 9

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

x86 Interrupt

int $3 ; Breakpoint int $0x80 ; Syscall Intel’s definition, Vol2a “The INT n instruction generates a call to the interrupt or exception handler specified with the destination operand”

LSE Team (EPITA) The K Project May 06, 2019 9 / 37

slide-10
SLIDE 10

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Context Switching

Figure: Stack Usage

LSE Team (EPITA) The K Project May 06, 2019 10 / 37

slide-11
SLIDE 11

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Context Switching

isr: ;<save registers> pushl %esp call generic_c_handler add $4, %esp ;<restore registers> add $8, %esp iret .global isr_keyboard isr_keyboard: pushl $0 pushl $33 ; IRQ1 jmp isr

LSE Team (EPITA) The K Project May 06, 2019 11 / 37

slide-12
SLIDE 12

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Programmable Interrupt Controller

1 2 3 4 5 6 7 IRQ 0 IRQ 1 IRQ 3 IRQ 4 IRQ 5 IRQ 6 IRQ 7 CPU 1 2 3 4 5 6 7 IRQ 8 IRQ 9 IRQ 10 IRQ 11 IRQ 12 IRQ 13 IRQ 14 IRQ 15 INT INT

Master S lave

Figure: PIC

LSE Team (EPITA) The K Project May 06, 2019 12 / 37

slide-13
SLIDE 13

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Typical wiring of the PIC (Master)

IRQ0 - PIT IRQ1 - Keyboard IRQ2 - Not assigned in PC/XT; cascaded to slave 8256 IRQ3 - UART (COM2 and COM4) IRQ4 - UART (COM1 and COM3) IRQ5 - Hard disk in PC/XT; Parallel port LPT2 in PC/AT IRQ6 - Floppy disk controller IRQ7 - Parallel port LPT1

LSE Team (EPITA) The K Project May 06, 2019 13 / 37

slide-14
SLIDE 14

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Typical wiring of the PIC (Slave)

IRQ8 - RTC IRQ9 - IRQ10 - IRQ11 - IRQ12 - PS/2 mouse controller IRQ13 - Math coprocessor IRQ14 - Hard disk controller 1 IRQ15 - Hard disk controller 2

LSE Team (EPITA) The K Project May 06, 2019 14 / 37

slide-15
SLIDE 15

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

PIC ports

0x20, the master PIC’s port A 0x21, the master PIC’s port B 0xA0, the slave PIC’s port A 0xA1, the slave PIC’s port B

LSE Team (EPITA) The K Project May 06, 2019 15 / 37

slide-16
SLIDE 16

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

ICW1

0 0 0 1 x 0 x x | | | | | +---- (1) | +------ (2) +---------- (3)

1 ICW4 present (set) or not (clear) 2 single controller (set) or cascade mode (clear) 3 level triggered mode (set) or edge triggered mode(clear)

LSE Team (EPITA) The K Project May 06, 2019 16 / 37

slide-17
SLIDE 17

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

ICW2

x x x x x 0 0 0 | | | | | | | | +------------------ (1)

1 Interrupt vector base address

LSE Team (EPITA) The K Project May 06, 2019 17 / 37

slide-18
SLIDE 18

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

ICW3

Master PIC x x x x x x x x | | | | | | | | +------------------ (1) Slave PIC 0 0 0 0 0 x x x | | | +-------- (2)

1 For each bit, indicate whether a slave PIC is connected to

this pin (set) or not (clear)

2 Indicate to the slave his slave ID (which pin of the master

it is connected to)

LSE Team (EPITA) The K Project May 06, 2019 18 / 37

slide-19
SLIDE 19

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

ICW4

0 0 0 x x x x 1 | --- | | | +------ (1) | +--------- (2) +------------ (3)

1 Automatic (set) EOI or normal (clear) EOI 2 Buffering mode (no buffering) 3 Special mode fully nested (set) or not (clear)

LSE Team (EPITA) The K Project May 06, 2019 19 / 37

slide-20
SLIDE 20

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

OCW1

x x x x x x x x | | | | | | | | +------------------ (1)

1 For each bit, indicate whether the corresponding IRQ is

masked (set) or not (clear)

LSE Team (EPITA) The K Project May 06, 2019 20 / 37

slide-21
SLIDE 21

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

OCW2

x x x 0 0 x x x | | |

  • | | |

| | | | +------ (1) | | +-------------- (2) | +---------------- (3) +------------------ (4)

1 Interrupt level to be acted upon when sending a specific

command

2 Send an EOI (end of interrupt command) (set) 3 Send a specific (set) or a non-specific (clear) command 4 Rotate priorities (set) or not (clear)

LSE Team (EPITA) The K Project May 06, 2019 21 / 37

slide-22
SLIDE 22

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Recommended methodology

Write IDT management fonctions

allocate/clean IDT set an interrupt gate in the IDT set a trap gate in the IDT

Write the context saving/restoring routines in assembly code Implement the exceptions and interrupts wrappers Write a function which builds the IDT and loads it Initialize the PIC

send ICWs to both master and slave PICs mask all interrupts

Write very simple debug handlers like:

printing an error message when executing a division by zero printing a string when a key is pressed

Do not forget to enable hardware interrupts using sti

LSE Team (EPITA) The K Project May 06, 2019 22 / 37

slide-23
SLIDE 23

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Possible bug causes

You forgot to save or restore some registers in your context You did not consider that gcc automatically generates the prolog/epilog for C functions The stack is misaligned when iret is executed PICs are not acknowledged after returning from an ISR (Interrupt SubRoutine)

LSE Team (EPITA) The K Project May 06, 2019 23 / 37

slide-24
SLIDE 24

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Keyboard Registers

0x60: I/0 buffer 0x64: Status register

LSE Team (EPITA) The K Project May 06, 2019 24 / 37

slide-25
SLIDE 25

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Keyboard Status Register

x x x x x x x x | | | | | | | | | | | | | | | +---- Output buffer full | | | | | | +------ Input buffer full | | | | | +-------- System flag | | | | +---------- Command/Data | | | +------------ Keyboard inhibit | | +-------------- Auxiliary device output buffer | +---------------- General purpose time-out +------------------ Parity error

LSE Team (EPITA) The K Project May 06, 2019 25 / 37

slide-26
SLIDE 26

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Keyboard scancode

x x x x x x x x | ------------- | | | +--------- Key number +----------------- Key press (clear) or release (set)

LSE Team (EPITA) The K Project May 06, 2019 26 / 37

slide-27
SLIDE 27

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Keyboard Recommended methodology

Ensure that the event manager is working Initialize driver

Add the keyboard interrupt gate to the IDT Update the PIC to unmask the IRQ line 1

Write a very simple keyboard handler:

Read scancodes from I/O port 0x60 Extra features such as modifier keys or simultaneous key strikes support are not mandatory

Implement getkey() You must implement a queue for the keyboard buffer

LSE Team (EPITA) The K Project May 06, 2019 27 / 37

slide-28
SLIDE 28

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Keyboard - Possible bug causes

Execution context may be corrupted. Hardware interrupts may have been disabled with cli or may not have been enabled with sti The keyboard interrupt may be masked in the PIC The previous IRQ has not been ACKED The previous key has not been fetched

LSE Team (EPITA) The K Project May 06, 2019 28 / 37

slide-29
SLIDE 29

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Programmable Interval Timer

Counter 0: fire an interrupt at a user-defined frequency. Counter 1: historically used in order to periodically refresh the RAM, but it not used anymore. Counter 2: linked with the PC speaker, so you can use it in order to generate sound

LSE Team (EPITA) The K Project May 06, 2019 29 / 37

slide-30
SLIDE 30

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

PIT Operation Modes

Mode 0: Interrupt on terminal count Mode 1: hardware retriggerable one-shot Mode 2: rate generator Mode 3: square generator Mode 4: Software Triggered Strobe Mode 5: Hardware Triggered Strobe

LSE Team (EPITA) The K Project May 06, 2019 30 / 37

slide-31
SLIDE 31

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

PIT Configuration

c c io io m m m b

  • ---- -------

| | | | +-- (1) | | +-------- (2) | +--------------- (3) +---------------------- (4)

1 Binary counter (unset) or BCD counter (set) 2 Mode to use 3 Registers read/write policy (LSB first then MSB) 4 Counter to setup

LSE Team (EPITA) The K Project May 06, 2019 31 / 37

slide-32
SLIDE 32

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

PIT Registers

0x40 : Counter 0 0x41 : Counter 1 0x42 : Counter 2 0x43 : Control Register

LSE Team (EPITA) The K Project May 06, 2019 32 / 37

slide-33
SLIDE 33

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

PIT Programming

Write into control register Write the divider on the counter register divider = internal frequency

desired frequency

Internal frequency : 1193182

LSE Team (EPITA) The K Project May 06, 2019 33 / 37

slide-34
SLIDE 34

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

PIT Programming

unsigned long gettick(void); Counter 0 Mode 2 Interrupt rate : 100 Hz

LSE Team (EPITA) The K Project May 06, 2019 34 / 37

slide-35
SLIDE 35

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

PIT - Possible bug causes

Execution context may be corrupted. Hardware interrupts may have been disabled with cli or may not have been enabled with sti The timer interrupt may be masked in the PIC.

LSE Team (EPITA) The K Project May 06, 2019 35 / 37

slide-36
SLIDE 36

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Advices

Use X Macro

LSE Team (EPITA) The K Project May 06, 2019 36 / 37

slide-37
SLIDE 37

The K Project LSE Team Introduction Interrupt Descriptor Table Interrupt Request Keyboard Timer Conclusion

Contact

k[at]lse.epita.fr labos.lse with [K] tag #k (irc.rezosup.org) guillaume.pagnoux[at]lse.epita.fr tom.decrette[at]lse.epita.fr

LSE Team (EPITA) The K Project May 06, 2019 37 / 37