9 void intr0(void), intr1(void), intr2(void), intr3(void);
10 void intr4(void), intr5(void), intr6(void), intr7(void);
11 void intr8(void), intr9(void), intr10(void), intr11(void);
12 void intr12(void), intr13(void), intr14(void), intr15(void);
14 void intr24(void), intr25(void), intr26(void), intr27(void);
15 void intr28(void), intr29(void), intr30(void), intr31(void);
16 void intr32(void), intr33(void), intr34(void), intr35(void);
17 void intr36(void), intr37(void), intr38(void), intr39(void);
22 * 8259 interrupt controllers
26 Int0ctl= 0x20, /* control port (ICW1, OCW2, OCW3) */
27 Int0aux= 0x21, /* everything else (ICW2, ICW3, ICW4, OCW1) */
28 Int1ctl= 0xA0, /* control port */
29 Int1aux= 0xA1, /* everything else (ICW2, ICW3, ICW4, OCW1) */
31 Icw1= 0x10, /* select bit in ctl register */
35 EOI= 0x20, /* non-specific end of interrupt */
37 Elcr1= 0x4D0, /* Edge/Level Triggered Register */
41 int int0mask = 0xff; /* interrupts enabled for first 8259 */
42 int int1mask = 0xff; /* interrupts enabled for second 8259 */
43 int i8259elcr; /* mask of level-triggered interrupts */
46 * trap/interrupt gates
52 Maxhandler= 32, /* max number of interrupt handlers */
55 typedef struct Handler Handler;
58 void (*r)(Ureg*, void*);
66 Handler h[Maxhandler];
71 sethvec(int v, void (*r)(void), int type, int pri)
73 ilt[v].d0 = ((ulong)r)&0xFFFF|(KESEL<<16);
74 ilt[v].d1 = ((ulong)r)&0xFFFF0000|SEGP|SEGPL(pri)|type;
78 setvec(int v, void (*r)(Ureg*, void*), void *arg)
82 if(halloc.nextfree >= Maxhandler)
83 panic("out of interrupt handlers");
84 h = &halloc.h[halloc.nextfree++];
85 h->next = halloc.ivec[v];
91 * enable corresponding interrupt in 8259
93 if((v&~0x7) == VectorPIC){
94 int0mask &= ~(1<<(v&7));
95 outb(Int0aux, int0mask);
96 } else if((v&~0x7) == VectorPIC+8){
97 int1mask &= ~(1<<(v&7));
98 outb(Int1aux, int1mask);
112 outb(Int0aux, int0mask);
113 outb(Int1aux, int1mask);
118 * set up the interrupt/trap gates
126 * set all interrupts to panics
128 for(i = 0; i < 256; i++)
129 sethvec(i, intrbad, SEGTG, 0);
132 * 80386 processor (and coprocessor) traps
134 sethvec(0, intr0, SEGTG, 0);
135 sethvec(1, intr1, SEGTG, 0);
136 sethvec(2, intr2, SEGTG, 0);
137 sethvec(3, intr3, SEGTG, 0);
138 sethvec(4, intr4, SEGTG, 0);
139 sethvec(5, intr5, SEGTG, 0);
140 sethvec(6, intr6, SEGTG, 0);
141 sethvec(7, intr7, SEGTG, 0);
142 sethvec(8, intr8, SEGTG, 0);
143 sethvec(9, intr9, SEGTG, 0);
144 sethvec(10, intr10, SEGTG, 0);
145 sethvec(11, intr11, SEGTG, 0);
146 sethvec(12, intr12, SEGTG, 0);
147 sethvec(13, intr13, SEGTG, 0);
148 sethvec(14, intr14, SEGTG, 0);
149 sethvec(15, intr15, SEGTG, 0);
150 sethvec(16, intr16, SEGTG, 0);
155 sethvec(24, intr24, SEGIG, 0);
156 sethvec(25, intr25, SEGIG, 0);
157 sethvec(26, intr26, SEGIG, 0);
158 sethvec(27, intr27, SEGIG, 0);
159 sethvec(28, intr28, SEGIG, 0);
160 sethvec(29, intr29, SEGIG, 0);
161 sethvec(30, intr30, SEGIG, 0);
162 sethvec(31, intr31, SEGIG, 0);
163 sethvec(32, intr32, SEGIG, 0);
164 sethvec(33, intr33, SEGIG, 0);
165 sethvec(34, intr34, SEGIG, 0);
166 sethvec(35, intr35, SEGIG, 0);
167 sethvec(36, intr36, SEGIG, 0);
168 sethvec(37, intr37, SEGIG, 0);
169 sethvec(38, intr38, SEGIG, 0);
170 sethvec(39, intr39, SEGIG, 0);
173 * tell the hardware where the table is (and how long)
175 putidt(ilt, sizeof(ilt)-1);
178 * Set up the first 8259 interrupt processor.
179 * Make 8259 interrupts start at CPU vector VectorPIC.
180 * Set the 8259 as master with edge triggered
181 * input with fully nested interrupts.
183 outb(Int0ctl, Icw1|0x01); /* ICW1 - edge triggered, master,
185 outb(Int0aux, VectorPIC); /* ICW2 - interrupt vector offset */
186 outb(Int0aux, 0x04); /* ICW3 - have slave on level 2 */
187 outb(Int0aux, 0x01); /* ICW4 - 8086 mode, not buffered */
190 * Set up the second 8259 interrupt processor.
191 * Make 8259 interrupts start at CPU vector VectorPIC+8.
192 * Set the 8259 as master with edge triggered
193 * input with fully nested interrupts.
195 outb(Int1ctl, Icw1|0x01); /* ICW1 - edge triggered, master,
197 outb(Int1aux, VectorPIC+8); /* ICW2 - interrupt vector offset */
198 outb(Int1aux, 0x02); /* ICW3 - I am a slave on level 2 */
199 outb(Int1aux, 0x01); /* ICW4 - 8086 mode, not buffered */
200 outb(Int1aux, int1mask);
203 * pass #2 8259 interrupts to #1
206 outb(Int0aux, int0mask);
209 * Set Ocw3 to return the ISR when ctl read.
211 outb(Int0ctl, Ocw3|0x03);
212 outb(Int1ctl, Ocw3|0x03);
215 * Check for Edge/Level register.
216 * This check may not work for all chipsets.
217 * First try a non-intrusive test - the bits for
218 * IRQs 13, 8, 2, 1 and 0 must be edge (0). If
219 * that's OK try a R/W test.
221 x = (inb(Elcr2)<<8)|inb(Elcr1);
226 if(inb(Elcr1) == 0x20)
228 outb(Elcr1, x & 0xFF);
229 print("ELCR: %4.4uX\n", i8259elcr);
240 print("FLAGS=%lux TRAP=%lux ECODE=%lux PC=%lux\n",
241 ur->flags, ur->trap, ur->ecode, ur->pc);
242 print(" AX %8.8lux BX %8.8lux CX %8.8lux DX %8.8lux\n",
243 ur->ax, ur->bx, ur->cx, ur->dx);
244 print(" SI %8.8lux DI %8.8lux BP %8.8lux\n",
245 ur->si, ur->di, ur->bp);
246 print(" CS %4.4lux DS %4.4lux ES %4.4lux FS %4.4lux GS %4.4lux\n",
247 ur->cs & 0xFF, ur->ds & 0xFFFF, ur->es & 0xFFFF, ur->fs & 0xFFFF, ur->gs & 0xFFFF);
248 print(" CR0 %8.8lux CR2 %8.8lux CR3 %8.8lux\n",
249 getcr0(), getcr2(), getcr3());
265 * tell the 8259 that we're done with the
266 * highest level interrupt (interrupts are still
271 if(c==VectorPIC || c==VectorPIC+8){
274 if(c == VectorPIC+8){
275 isr |= inb(Int1ctl)<<8;
280 if(v>=256 || (h = halloc.ivec[v]) == 0){
281 if(v >= VectorPIC && v < VectorPIC+16){
284 * Check for a default IRQ7. This can happen when
285 * the IRQ input goes away before the acknowledge.
286 * In this case, a 'default IRQ7' is generated, but
287 * the corresponding bit in the ISR isn't set.
288 * In fact, just ignore all such interrupts.
291 print("unknown interrupt %d pc=0x%lux\n", v, ur->pc);
298 print("NMI: nmisc=0x%2.2ux, nmiertc=0x%2.2ux, nmiesc=0x%2.2ux\n",
299 inb(0x61), inb(0x70), inb(0x461));
304 panic("exception/interrupt %d", v);
310 * call the trap routines
318 extern void realmode0(void); /* in l.s */
320 extern int realmodeintr;
321 extern Ureg realmoderegs;
324 realmode(int intr, Ureg *ureg)
326 realmoderegs = *ureg;
331 *ureg = realmoderegs;