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

NodeBots Advice Part I: General Advice for the Aspiring NodeBotter

Top 7 VS Code Extensions You Must Have To Boost Your Productivity

Colony Tips for Remote Working

Factory Method Design Pattern Explained.

Performance analysis of EF/EF Core read operation with or without AsNoTracking() using Benchmark.net

Lets Grow More Experience

Configuring Kafka Connect on Kubernetes

CamOp Photo Winners: October — December 2019

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

Checking MySQL Replication Integrity using Percona Toolkit

Throughput of Splunk Ingest Actions with Regular Expressions: Best Practices

Apache Flux: Frictionless STORM topology management — Part 1

Install Cloudwatch Agent install in the Elasticbeanstalk Environment