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/serial.h>
14 #include <geekos/debug.h>
15 /* ----------------------------------------------------------------------
16 * Private data and functions
17 * ---------------------------------------------------------------------- */
22 static union IDT_Descriptor s_IDT[ NUM_IDT_ENTRIES ];
23 //static union IDT_Descriptor *s_IDT = (union IDT_Descriptor *)IDT_LOCATION;
27 * These symbols are defined in lowlevel.asm, and define the
28 * size of the interrupt entry point table and the sizes
29 * of the individual entry points. This gives us sufficient
30 * information to build the IDT.
32 extern char g_entryPointTableStart, g_entryPointTableEnd;
33 extern int g_handlerSizeNoErr, g_handlerSizeErr;
36 * Table of C interrupt handler functions.
37 * Note that this is public only because it is used
38 * in lowlevel.asm. Other code should not refer to it.
40 Interrupt_Handler g_interruptTable[ NUM_IDT_ENTRIES ];
47 Print("IDT Contents:\n");
49 for (i=0;i<NUM_IDT_ENTRIES/16;i++) {
50 if (s_IDT[i].ig.present) {
51 Print("%d: segmentselector=%u, offset=%u, offsetLow=%u, segmentSelector=%u, reserved=%u, signature=%u, dpl=%u, present=%u, offsetHigh=%u\n",
53 s_IDT[i].ig.segmentSelector,
54 (s_IDT[i].ig.offsetHigh<<16) + s_IDT[i].ig.offsetLow,
55 s_IDT[i].ig.offsetLow,
56 s_IDT[i].ig.segmentSelector,
58 s_IDT[i].ig.signature,
61 s_IDT[i].ig.offsetHigh);
70 SerialPrint("IDT Contents:\n");
72 for (i=0;i<NUM_IDT_ENTRIES;i++) {
73 if (s_IDT[i].ig.present) {
74 SerialPrint("%d: segmentselector=%u, offset=%u, offsetLow=%u, segmentSelector=%u, reserved=%u, signature=%u, dpl=%u, present=%u, offsetHigh=%u\n",
76 s_IDT[i].ig.segmentSelector,
77 (s_IDT[i].ig.offsetHigh<<16) + s_IDT[i].ig.offsetLow,
78 s_IDT[i].ig.offsetLow,
79 s_IDT[i].ig.segmentSelector,
81 s_IDT[i].ig.signature,
84 s_IDT[i].ig.offsetHigh);
92 /* ----------------------------------------------------------------------
94 * ---------------------------------------------------------------------- */
97 * Initialize the Interrupt Descriptor Table.
98 * This will allow us to install C handler functions
99 * for interrupts, both processor-generated and
100 * those generated by external hardware.
105 ushort_t limitAndBase[3];
106 ulong_t idtBaseAddr = (ulong_t) s_IDT;
107 ulong_t tableBaseAddr = (ulong_t) &g_entryPointTableStart;
110 PrintBoth("Initializing IDT\n");
112 /* Make sure the layout of the entry point table is as we expect. */
113 KASSERT(g_handlerSizeNoErr == g_handlerSizeErr);
114 KASSERT((&g_entryPointTableEnd - &g_entryPointTableStart) ==
115 g_handlerSizeNoErr * NUM_IDT_ENTRIES);
119 * We're taking advantage of the fact that all of the
120 * entry points are laid out consecutively, and that they
121 * are all padded to be the same size.
123 for (i = 0, addr = tableBaseAddr; i < NUM_IDT_ENTRIES; ++i) {
125 * All interrupts except for the syscall interrupt
126 * must have kernel privilege to access.
128 int dpl = (i == SYSCALL_INT) ? USER_PRIVILEGE : KERNEL_PRIVILEGE;
129 Init_Interrupt_Gate(&s_IDT[i], addr, dpl);
130 addr += g_handlerSizeNoErr;
134 * Cruft together a 16 bit limit and 32 bit base address
135 * to load into the IDTR.
137 limitAndBase[0] = 8 * NUM_IDT_ENTRIES;
138 limitAndBase[1] = idtBaseAddr & 0xffff;
139 limitAndBase[2] = idtBaseAddr >> 16;
141 /* Install the new table in the IDTR. */
142 Load_IDTR(limitAndBase);
146 * Initialize an interrupt gate with given handler address
147 * and descriptor privilege level.
149 void Init_Interrupt_Gate(union IDT_Descriptor* desc, ulong_t addr,
152 desc->ig.offsetLow = addr & 0xffff;
153 desc->ig.segmentSelector = KERNEL_CS;
154 desc->ig.reserved = 0;
155 desc->ig.signature = 0x70; /* == 01110000b */
157 desc->ig.present = 1;
158 desc->ig.offsetHigh = addr >> 16;
162 * Install a C handler function for given interrupt.
163 * This is a lower-level notion than an "IRQ", which specifically
164 * means an interrupt triggered by external hardware.
165 * This function can install a handler for ANY interrupt.
167 void Install_Interrupt_Handler(int interrupt, Interrupt_Handler handler)
169 KASSERT(interrupt >= 0 && interrupt < NUM_IDT_ENTRIES);
170 g_interruptTable[interrupt] = handler;