In protected mode the IDT holds 8-byte gate descriptors pointing to handlers with privilege and type information. LIDT loads the table base and limit; each interrupt index selects an entry. For example, entry 32 (IRQ0 after PIC remap) needs a present interrupt gate targeting your timer ISR at ring 0.
Gate entry
nasm
Loading…
256 entries cover CPU exceptions and IRQs
IST not used in basic setups; keep gate type 0x8E for 32-bit interrupt gates
Remap PIC to vectors 0x20-0x2F before unmasking IRQs
Handlers end with IRETD in 32-bit code
Exception stubs
Divide-by-zero and invalid opcode handlers help catch early PM bugs. Place stubs in low memory or identity-mapped regions so their addresses fit gate offset fields. Use interrupt gates (type 0x8E) not trap gates unless you need nested faults on the same vector. Double-fault requires a valid TSS stack if you run ring 3 later; for ring 0 only labs, a simple halt loop in the handler is enough.
Why for this exercise
You will pack and unpack the 8-byte gate descriptors, register handlers in a simulated IDT, and trace interrupt dispatch, including which PICs need an EOI. This exercise requires matching gate selectors to your GDT code segment and using IRETD return.
Implement IDT gate encoding, decoding, and dispatch simulation in C, driven by a command script on stdin.
Protocol (all numbers in hex):
reg <vector> <offset> <selector> <attr>
Register one 8-byte gate in the IDT and print "IDT[%llu]=0x%016llX".
Layout: bytes 0-1 = offset[15:0]; bytes 2-3 = selector; byte 4 = 0;
byte 5 = attributes; bytes 6-7 = offset[31:16]. Vector must be 0-255
(a range check the real LIDT-permitted indices enforce); print
"BADVECTOR" and skip otherwise.
dec <descriptor>
Unpack one gate and print
"OFF=0x%08X SEL=0x%04X P=%u DPL=%u TYPE=0x%X INTGATE|TRAPGATE|OTHER"
where P = attr bit 7, DPL = attr bits 6-5, TYPE = attr bits 3-0
(0xE interrupt gate, 0xF trap gate).
int <vector>
Simulate raising that interrupt. If no gate is registered print
"UNHANDLED". If the gate is not present print "NOTPRESENT".
Otherwise print "HANDLED AT 0x%08X EOI=..." where the EOI part is:
"0x20>0x20" for vectors 32-39 (master PIC), "0x20>0xA0,0x20>0x20"
for vectors 40-47 (slave + master PIC), "NONE" for everything else.
Process every command on stdin until EOF, printing one line per command.