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:
- 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.
- 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:
- Send some data to the PIC (
outb
and what-not) to initialize it. - 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.
- 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?