KFS 2

What the processor demands before it runs

Second module of the series: the segment table the processor demands in protected mode, placed at a known, hard-coded address, then loaded by reloading every segment register, far jump included. Add the kernel stack and a built-in shell to read them back.

Area
  • 42
  • system
  • kernel
Stack
C, x86, Assembly
Grade
118%
Date
2026

What is still missing

The kernel boots, prints to the screen and reads the keyboard. Yet it runs on structures that are not its own: the segment registers still hold whatever GRUB left in them, and the slightest processor fault would send the machine into a reboot loop, with nowhere to go.

This module fixes that. A segment table of its own, placed at a chosen address, a stack whose location is known, and a way to check both from inside the kernel.

The table the processor demands

In protected mode no address reaches memory without going through a segment, described in a table the kernel has to supply. A segment carries a base, a limit and rights: where it starts, how far it runs, and who may execute or write it.

The usual answer is to render the whole mechanism harmless. Seven entries, all with a base of zero and a limit of four gigabytes, overlapping exactly: a null entry the processor insists on, then code, data and stack, twice over, once for the kernel and once for user space. Memory goes flat again, and the only difference between the two sets is the privilege level written into their rights: zero for the kernel, three for the user.

So the division has no effect on addresses at all. It has one on permissions, and that is the only thing being asked of it.

At a known address

The table does not live in a global variable but at address 0x0800, written in. That is the subject’s requirement, and it is not an arbitrary one: a fixed address is an address you can find again from a debugger, or check by hand in a memory dump, without depending on where the linker happened to put its data that day.

The pointer loaded into the processor carries that base and the size of the table, expressed as the index of its last byte. Not its size, its last byte: an empty table cannot exist, and the convention saves the value zero.

Reloading the registers

Loading the table is not enough. The segment registers keep whatever GRUB left in them until they are written again, and the code segment register cannot be set by assignment: it takes a far jump whose target address carries the new selector.

That is the last instruction of the install routine, and without it none of the rest counts for anything: the table would be in place, but the processor would go on reading descriptors from the old one.

The shell prints the segment table: its base address, its size, then all seven entries with their base, limit and rights

The stack

The kernel stack is eight kilobytes reserved in the uninitialised data section, and its top is the very first thing the entry code writes, before it even calls into C. It grows downwards, so the pointer starts at the top of the block.

The stack command goes and reads that pointer and prints the few values sitting below it. It is modest, but it is exactly what the module asks for, and it is the only way to see from the inside that a stack exists, that it sits where it was put, and that it moves when a function is called.

The shell prints the address of the top of the stack and the first values sitting on it, then the timer tick count

Interrupts

Next comes the table that tells the processor what to do when it is interrupted. Two hundred and fifty-six entries, of which the first thirty-two are its own faults: divide by zero, invalid opcode, page fault. Without them the smallest mistake turns into a triple fault, and the machine reboots without a word.

Hardware interrupts arrive through a controller inherited from the original PC, which by default presents them on numbers those faults have already claimed. It has to be reprogrammed to shift them out of the way, or a keypress would arrive disguised as an exception. Once that move is done, the timer and the keyboard each have their routine, and one instruction lets interrupts back in.

A shell to read it all back

The rest of the module is a command interpreter, which is not a shell in any real sense: no processes, no files, no redirection. Its commands exist to look at the kernel from the inside.

gdt asks the processor for the address of its segment table, then walks the seven entries, base, limit and rights. What gets read back is not the structure written in C but the processor’s own register, so the kernel checks what it actually installed rather than what it believes it installed. stack does the same for the stack, ticks gives the number of timer interrupts since boot, which is the most direct way to confirm they are arriving at all, and halt cuts interrupts and stops the processor, with no way back.

None of this is visible from the outside. It is the first time, though, that the kernel can answer a question about itself.