2 * GeekOS IDT initialization code
3 * Copyright (c) 2001, David H. Hovemeyer <daveho@cs.umd.edu>
6 * This is free software. You are permitted to use,
7 * redistribute, and modify it as specified in the file "COPYING".
10 #include <geekos/kassert.h>
11 #include <geekos/defs.h>
12 #include <geekos/idt.h>
13 #include <geekos/debug.h>
15 /* ----------------------------------------------------------------------
16 * Private data and functions
17 * ---------------------------------------------------------------------- */
22 static union IDT_Descriptor s_IDT[ NUM_IDT_ENTRIES ];
24 // JRL: why??? Should just call Alloc_Page(), otherwise we have dependencies all over the place
25 //static union IDT_Descriptor *s_IDT = (union IDT_Descriptor *)IDT_LOCATION;
28 * These symbols are defined in lowlevel.asm, and define the
29 * size of the interrupt entry point table and the sizes
30 * of the individual entry points. This gives us sufficient
31 * information to build the IDT.
33 extern char g_entryPointTableStart, g_entryPointTableEnd;
34 extern int g_handlerSizeNoErr, g_handlerSizeErr;
37 * Table of C interrupt handler functions.
38 * Note that this is public only because it is used
39 * in lowlevel.asm. Other code should not refer to it.
41 Interrupt_Handler g_interruptTable[ NUM_IDT_ENTRIES ];
51 PrintBoth("IDT Contents:\n");
53 for (i=0;i<NUM_IDT_ENTRIES;i++) {
54 if (s_IDT[i].ig.present) {
55 PrintBoth("%d: segmentselector=%u, offset=%u, offsetLow=%u, segmentSelector=%u, reserved=%u, signature=%u, dpl=%u, present=%u, offsetHigh=%u\n",
57 s_IDT[i].ig.segmentSelector,
58 (s_IDT[i].ig.offsetHigh<<16) + s_IDT[i].ig.offsetLow,
59 s_IDT[i].ig.offsetLow,
60 s_IDT[i].ig.segmentSelector,
62 s_IDT[i].ig.signature,
65 s_IDT[i].ig.offsetHigh);
73 /* ----------------------------------------------------------------------
75 * ---------------------------------------------------------------------- */
78 * Initialize the Interrupt Descriptor Table.
79 * This will allow us to install C handler functions
80 * for interrupts, both processor-generated and
81 * those generated by external hardware.
86 ushort_t limitAndBase[3];
87 ulong_t idtBaseAddr = (ulong_t) s_IDT;
88 ulong_t tableBaseAddr = (ulong_t) &g_entryPointTableStart;
91 PrintBoth("Initializing IDT\n");
93 /* Make sure the layout of the entry point table is as we expect. */
94 KASSERT(g_handlerSizeNoErr == g_handlerSizeErr);
95 KASSERT((&g_entryPointTableEnd - &g_entryPointTableStart) ==
96 g_handlerSizeNoErr * NUM_IDT_ENTRIES);
100 * We're taking advantage of the fact that all of the
101 * entry points are laid out consecutively, and that they
102 * are all padded to be the same size.
104 for (i = 0, addr = tableBaseAddr; i < NUM_IDT_ENTRIES; ++i) {
106 * All interrupts except for the syscall interrupt
107 * must have kernel privilege to access.
109 int dpl = (i == SYSCALL_INT) ? USER_PRIVILEGE : KERNEL_PRIVILEGE;
110 Init_Interrupt_Gate(&s_IDT[i], addr, dpl);
111 addr += g_handlerSizeNoErr;
115 * Cruft together a 16 bit limit and 32 bit base address
116 * to load into the IDTR.
118 limitAndBase[0] = 8 * NUM_IDT_ENTRIES;
119 limitAndBase[1] = idtBaseAddr & 0xffff;
120 limitAndBase[2] = idtBaseAddr >> 16;
122 /* Install the new table in the IDTR. */
123 Load_IDTR(limitAndBase);
127 * Initialize an interrupt gate with given handler address
128 * and descriptor privilege level.
130 void Init_Interrupt_Gate(union IDT_Descriptor* desc, ulong_t addr,
133 desc->ig.offsetLow = addr & 0xffff;
134 desc->ig.segmentSelector = KERNEL_CS;
135 desc->ig.reserved = 0;
136 desc->ig.signature = 0x70; /* == 01110000b */
138 desc->ig.present = 1;
139 desc->ig.offsetHigh = addr >> 16;
143 * Install a C handler function for given interrupt.
144 * This is a lower-level notion than an "IRQ", which specifically
145 * means an interrupt triggered by external hardware.
146 * This function can install a handler for ANY interrupt.
148 void Install_Interrupt_Handler(int interrupt, Interrupt_Handler handler)
150 KASSERT(interrupt >= 0 && interrupt < NUM_IDT_ENTRIES);
151 g_interruptTable[interrupt] = handler;