How To Build An Operating System: Interrupts and Inputs- Part 05

What Does Interrupt Do?

Interrupt Descriptor Table (IDT)

  • Task handler
  • Interrupt handler
  • Trap handler

Creating an Entry in the IDT

Bit:        | 31 16 |15| 14 13| 12 | 11| 10 9 8 | 7 6 5| 4 3 2 1 0 |
Content:|offset high|P | DPL | 0 | D | 1 1 0 | 0 0 0| reserved |
Bit:     | 31              16 | 15              0 |
Content: | segment selector | offset low |
0xDEAD8E00
0x0008BEEF
idt[0] = 0xDEAD8E00
idt[1] = 0x0008BEEF

Handling an Interrupt

[esp + 12] eflags
[esp + 8] cs
[esp + 4] eip
[esp] error code?
struct cpu_state {
unsigned int eax;
unsigned int ebx;
unsigned int ecx;
.
.
.
unsigned int esp;
} __attribute__((packed));
struct stack_state {
unsigned int error_code;
unsigned int eip;
unsigned int cs;
unsigned int eflags;
} __attribute__((packed));

Creating a Generic Interrupt Handler

  • Push the registers on the stack.
  • Call the C function interrupt_handler.
  • Pop the registers from the stack.
  • Add 8 to esp (because of the error code and the interrupt number pushed earlier).
  • Execute iret to return to the interrupted code.

Loading the IDT

Programmable Interrupt Controller (PIC)

  • Remap the interrupts. The PIC uses interrupts 0–15 for hardware interrupts by default, which conflicts with the CPU interrupts. Therefore the PIC interrupts must be remapped to another interval.
  • Select which interrupts to receive. You probably don’t want to receive interrupts from all devices since you don’t have code that handles these interrupts anyway.
  • Set up the correct mode for the PIC.
--------------------------------------------------------------------
PIC 1 | Hardware | PIC 2 | Hardware --------------------------------------------------------------------
0 | Timer | 8 | Real Time Clock
1 | Keyboard | 9 | General I/O
2 | PIC 2 | 10 | General I/O
3 | COM 2 | 11 | General I/O
4 | COM 1 | 12 | General I/O
5 | LPT 2 | 13 | Coprocessor
6 | Floppy disk | 14 | IDE Bus
7 | LPT 1 | 15 | IDE Bus

Reading Input from the Keyboard

--

--

--

Software Engineering Undergraduate at University of Kelaniya

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Go with AWS Lambda + DynamoDB

speakOut

Chrono Adventures NFT Dev update 34 — Map update

Introducing QLogic: A Quantum Computing Puzzle Game

Frog Jump

What is the future of AI in Manufacturing Industries?

RISE OF THE TITAN

Configure Flutter development environment on a low-end laptop

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pubudu Wickramathunge

Pubudu Wickramathunge

Software Engineering Undergraduate at University of Kelaniya

More from Medium

CS371p Spring 2022: Santi Dasari

Migrating to Multi-tenancy with Istio and Kubernetes

Service MisConfiguration Vulnerability

How to Fix High CPU Usage in Linux