1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
//+----------------------------------------------------------------------------
//
// File: interrupts.c
//
// Module:
//
// Synopsis:
//
// Created: sgasch 6 Jul 2003
//
//+----------------------------------------------------------------------------
#include "kernel.h"
#include "hal.h"
#include "rtl.h"
#include "interrupts.h"
IDT_ENTRY g_IDT[NUM_IDT_ENTRIES];
INTERRUPT_HANDLER g_InterruptHandlerTable[NUM_IDT_ENTRIES];
ULONG g_uInterruptsEnabled = 0;
void
HalEnableInterrupts(void)
{
g_uInterruptsEnabled++;
ASSERT(g_uInterruptsEnabled == 1);
__asm__ __volatile__ (
"sti"
);
}
void
HalDisableInterrupts(void)
{
g_uInterruptsEnabled--;
ASSERT(g_uInterruptsEnabled == 0);
__asm__ __volatile__ (
"cli"
);
}
//+----------------------------------------------------------------------------
//
// Function: DoNothing
//
// Synopsis: The default interrupt handler, do nothing
//
// Arguments: INTERRUPT_STATE *p
//
// Returns: void
//
//+----------------------------------------------------------------------------
void HalDoNothing(INTERRUPT_STATE *p)
{
;
}
//+----------------------------------------------------------------------------
//
// Function: InterruptSendIrqEoi
//
// Synopsis: Send Interrupt ReQuest End Of Interrupt to the PIC.
//
// Arguments: INTERRUPT_STATE *is
//
// Returns: void
//
//+----------------------------------------------------------------------------
void
HalInterruptSendIrqEoi(INTERRUPT_STATE *is)
{
ULONG uIrq;
BYTE bCommand;
BYTE *p = (BYTE *)0x000B8000;
*p = '3';
*(p + 1) = ' ';
if ((is->uIntNum >= FIRST_EXTERNAL_INT) &&
(is->uIntNum <= LAST_EXTERNAL_INT))
{
uIrq = (is->uIntNum - FIRST_EXTERNAL_INT);
ASSERT(uIrq >= 0);
ASSERT(uIrq <= 15);
bCommand = 0x60 | (uIrq & 0x7);
if (uIrq < 8)
{
// EOI to master PIC
out(0x20, bCommand);
HalIoDelay();
}
else
{
// EOI to slave PIC and cascade line EOI to master PIC
out(0xA0, bCommand);
out(0x20, 0x62);
HalIoDelay();
}
}
*p = '4';
*(p + 1) = ' ';
}
//+----------------------------------------------------------------------------
//
// Function: InterruptInitializeGate
//
// Synopsis: Initializes an interrupt gate with given handler address
// and descriptor privilege level.
//
// Arguments: IDT_ENTRY *pEntry,
// void *pPreamble,
// ULONG uDpl
//
// Returns: void
//
//+----------------------------------------------------------------------------
void
HalInterruptInitializeGate(IDT_ENTRY *pEntry,
void *pPreamble,
ULONG uDpl)
{
ULONG u = (ULONG)pPreamble;
pEntry->i.offsetLow = u & 0xffff;
pEntry->i.segmentSelector = (1<<3);
pEntry->i.reserved = 0;
pEntry->i.signature = 0x70; // == 01110000b
pEntry->i.dpl = uDpl;
pEntry->i.present = 1;
pEntry->i.offsetHigh = u >> 16;
}
//+----------------------------------------------------------------------------
//
// Function: InterruptInstallHandler
//
// Synopsis: Installs an interrupt handler routine for a given IRQ number
//
// Arguments: ULONG uInterruptNumber,
// INTERRUPT_HANDLER pHandler
//
// Returns: void
//
//+----------------------------------------------------------------------------
void
HalInterruptInstallHandler(ULONG uInterruptNumber,
INTERRUPT_HANDLER pHandler)
{
ASSERT(uInterruptNumber < ARRAY_LENGTH(g_InterruptHandlerTable));
g_InterruptHandlerTable[uInterruptNumber] = pHandler;
}
//+----------------------------------------------------------------------------
//
// Function: InterruptInitialize
//
// Synopsis: Initialize the interrupt system
//
// Arguments: void
//
// Returns: void
//
//+----------------------------------------------------------------------------
void
HalInitializeInterrupts(void)
{
ULONG uPreambleEntrySize = (ULONG)&HalInterruptHandlerPreambleAfter;
ULONG u;
ULONG uDpl;
USHORT uLimitAndBase[3];
BYTE *p;
//
// Determine the size of an interrupt preamble table entry.
//
uPreambleEntrySize -= (ULONG)&HalInterruptHandlerPreambleBefore;
//
// Set the base interrupt number for each PIC. See comments in
// intsupport.asm.
//
HalInterruptInitializePICs();
//
// Build interrupt descriptor table
//
for (u = 0, p = &HalInterruptHandlerPreamble;
u < ARRAY_LENGTH(g_IDT);
u++, p += uPreambleEntrySize)
{
uDpl = (u == 0x2E) ? USER_PRIVILEGE : KERNEL_PRIVILEGE;
HalInterruptInitializeGate(&(g_IDT[u]), p, uDpl);
HalInterruptInstallHandler(u, HalDoNothing);
}
//
// Install the IDT
//
u = (ULONG)&(g_IDT);
uLimitAndBase[0] = 8 * NUM_IDT_ENTRIES;
uLimitAndBase[1] = (USHORT)(u & 0xffff);
uLimitAndBase[2] = (USHORT)(u >> 16);
HalInterruptLoadIDTR((void *)uLimitAndBase);
//
// Install interrupt handlers
//
HalInterruptInstallHandler(IRQ0, HalTimerInt);
HalInterruptInstallHandler(IRQ1, HalKeyboardInt);
out(0x21, 0xFC); // turn on keyboard and timer
//
// Enable interrupts
//
HalEnableInterrupts();
}
|