13

I am writing a small kernel just to poke around the low level stuff a bit. Right now, it boots in Virtual Box and I can display text to the screen, allocate some memory, and other really basic things. It's written in C++ and a little asm.

One of the things I wanted to explore was the mechanism behind multi-tasking. As I understand it, it goes something like this:

  1. The kernel initializes the Interrupt Descriptor Table so that an interrupt is issued periodically (e.g. a millisecond) and calls a routine defined in the kernel.
  2. When the routine is called, it can decide to set the code/data segments and stack pointer to those of another program's context, a "context-switch."

So, it seemed simple in concept, but I knew the details were going to be a lot hairier. I've found a few things on line, but the terminology varies a lot, and the examples seem to be from contexts I don't have (like from within the Linux kernel).

However, the way to set the descriptor tables seems to be like this:

  1. Send some data to the PIC (outb and what-not) to initialize it.
  2. Prepare an interrupt table in memory, with function pointers to the routines you want, being careful that the functions are capable of being signal handlers.
  3. Load the table with lidt.

However, I can't find much on specifically doing these things, or whether this is even correct. Does anyone have resources for a bewildered kernel writer?

rovaughn
  • 1,213
  • 15
  • 24
  • Interesting question, here is a cool community: http://www.osdever.net/ Not that it answers your question directly, but there is some useful info on there. – Austin Henley Apr 29 '12 at 04:51
  • Yet better community (with forum and wiki, as well as kind-of associated irc channel - #osdev @ irc.freenode.net) can be found at osdev.org. – Griwes Apr 29 '12 at 13:14

1 Answers1

6

When your PC boots, the BIOS programs the PIC in such a way that IRQ0 through IRQ15 are bound to int 8 through int 0Fh and int 70h through int 77h. This is OK for the real address mode, in which the BIOS operates and MSDOS works.

But you need to change this mapping when switching to the protected mode because some important exceptions are on int 8 through int 0Fh (most notably, #GP, #SS, #PF). You want that because you want to be able to easily distinguish between these exceptions and hardware interrupts coming from the timer and the real-time clock, the keyboard and the mouse, the disks and I/O ports (serial and parallel).

This is probably the first step you've outlined. So, look up online "PIC interrupt remapping" or something of that sort. Also, download some specs for the 8259 chip (the PIC) to have a better idea of what you're doing and how it actually works. "HelpPC" is a good old reference that contains some of the information on the various PC hardware.

There are also "PCGPE" (PC Game Programming Encyclopedia) and "RBIL" (Ralf Brown's Interrupt List) that may help a lot.

IVT/IDT setup is described in the Intel and AMD CPU documentation. It's all there. Not the most pleasant read, but the most detailed and authoritative.

There are many websites of home-brew OS hobbyists and the like, where you can find more details and snippets of code.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • I did see some remapping code floating around before. So, I may be misunderstanding a detail on protected and real mode, but as I understand it, the kernel runs in real mode, but can switch into protected mode when a user program runs. However, doesn't it switch into real mode on a context switch? That's done by the interrupt, right? Do the interrupts need to be remapped just once before a protected program ever runs? – rovaughn Apr 29 '12 at 16:22
  • Normally, if there's any switch, it's from real to protected and it's done only once, very early, when the kernel starts. – Alexey Frunze Apr 29 '12 at 22:35