kern

view src/intr.c @ 41:928b0ebfff4d

merged the timer/rtc/etc from the misc branch
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 14 Jun 2011 01:19:07 +0300
parents a2c6110bd24b 06172322fb76
children e6de3c6015cb
line source
1 #include <stdio.h>
2 #include "intr.h"
3 #include "desc.h"
4 #include "segm.h"
5 #include "asmops.h"
6 #include "panic.h"
8 /* IDT gate descriptor bits */
9 #define GATE_TASK (5 << 8)
10 #define GATE_INTR (6 << 8)
11 #define GATE_TRAP (7 << 8)
12 #define GATE_DEFAULT (1 << 11)
13 #define GATE_PRESENT (1 << 15)
15 /* PIC command and data ports */
16 #define PIC1_CMD 0x20
17 #define PIC1_DATA 0x21
18 #define PIC2_CMD 0xa0
19 #define PIC2_DATA 0xa1
21 /* PIC initialization command word 1 bits */
22 #define ICW1_ICW4_NEEDED (1 << 0)
23 #define ICW1_SINGLE (1 << 1)
24 #define ICW1_INTERVAL4 (1 << 2)
25 #define ICW1_LEVEL (1 << 3)
26 #define ICW1_INIT (1 << 4)
27 /* PIC initialization command word 4 bits */
28 #define ICW4_8086 (1 << 0)
29 #define ICW4_AUTO_EOI (1 << 1)
30 #define ICW4_BUF_SLAVE (1 << 3) /* 1000 */
31 #define ICW4_BUF_MASTER (3 << 2) /* 1100 */
32 #define ICW4_SPECIAL (1 << 4)
34 /* PIC operation command word 2 bits */
35 #define OCW2_EOI (1 << 5)
38 /* structure used to pass the interrupt stack frame from the
39 * entry points to the C dispatch function.
40 */
41 struct intr_frame {
42 /* registers pushed by pusha in intr_entry_* */
43 uint32_t edi, esi, ebp, esp;
44 uint32_t ebx, edx, ecx, eax;
45 /* interrupt number and error code pushed in intr_entry_* */
46 uint32_t inum, err;
47 /* pushed by CPU during interrupt entry */
48 uint32_t eip, cs, eflags;
49 };
52 static void init_pic(int offset);
53 static void gate_desc(desc_t *desc, uint16_t sel, uint32_t addr, int dpl, int type);
54 static void set_intr_entry(int num, void (*handler)(void));
55 static void end_of_irq(int irq);
57 /* defined in intr-asm.S */
58 void set_idt(uint32_t addr, uint16_t limit);
59 void intr_entry_default(void);
61 /* the IDT (interrupt descriptor table) */
62 static desc_t idt[256];
63 /* table of handler functions for all interrupts */
64 static intr_func_t intr_func[256];
67 void init_intr(void)
68 {
69 int i;
71 set_idt((uint32_t)idt, sizeof idt - 1);
73 /* initialize all entry points and interrupt handlers */
74 for(i=0; i<256; i++) {
75 set_intr_entry(i, intr_entry_default);
76 interrupt(i, 0);
77 }
79 /* by including interrupts.h here (without ASM being defined)
80 * the series of INTR_ENTRY_* macros will be expanded to a series
81 * of function prototypes for all interrupt entry points and the
82 * corresponding calls to set_intr_entry to set up the IDT slots
83 */
84 #include "interrupts.h"
86 /* initialize the programmable interrupt controller
87 * setting up the maping of IRQs [0, 15] to interrupts [32, 47]
88 */
89 init_pic(IRQ_OFFSET);
90 }
92 /* set an interrupt handler function for a particular interrupt */
93 void interrupt(int intr_num, intr_func_t func)
94 {
95 intr_func[intr_num] = func;
96 }
98 /* this function is called from all interrupt entry points
99 * it calls the appropriate interrupt handlers if available and handles
100 * sending an end-of-interrupt command to the PICs when finished.
101 */
102 void dispatch_intr(struct intr_frame frm)
103 {
104 if(intr_func[frm.inum]) {
105 intr_func[frm.inum](frm.inum, frm.err);
106 } else {
107 if(frm.inum < 32) {
108 panic("unhandled exception %d, error code: %d\n", frm.inum, frm.err);
109 }
110 printf("unhandled interrupt %d\n", frm.inum);
111 }
113 if(IS_IRQ(frm.inum)) {
114 end_of_irq(INTR_TO_IRQ(frm.inum));
115 }
116 }
118 static void init_pic(int offset)
119 {
120 /* send ICW1 saying we'll follow with ICW4 later on */
121 outb(ICW1_INIT | ICW1_ICW4_NEEDED, PIC1_CMD);
122 outb(ICW1_INIT | ICW1_ICW4_NEEDED, PIC2_CMD);
123 /* send ICW2 with IRQ remapping */
124 outb(offset, PIC1_DATA);
125 outb(offset + 8, PIC2_DATA);
126 /* send ICW3 to setup the master/slave relationship */
127 /* ... set bit3 = 3rd interrupt input has a slave */
128 outb(4, PIC1_DATA);
129 /* ... set slave ID to 2 */
130 outb(2, PIC2_DATA);
131 /* send ICW4 to set 8086 mode (no calls generated) */
132 outb(ICW4_8086, PIC1_DATA);
133 outb(ICW4_8086, PIC2_DATA);
134 /* done, just reset the data port to 0 */
135 outb(0, PIC1_DATA);
136 outb(0, PIC2_DATA);
137 }
139 static void gate_desc(desc_t *desc, uint16_t sel, uint32_t addr, int dpl, int type)
140 {
141 /* first 16bit part is the low 16bits of the entry address */
142 desc->d[0] = addr & 0xffff;
143 /* second 16bit part is the segment selector for the entry code */
144 desc->d[1] = sel;
145 /* third 16bit part has the privilege level, type, and present bit */
146 desc->d[2] = ((dpl & 3) << 13) | type | GATE_DEFAULT | GATE_PRESENT;
147 /* last 16bit part is the high 16bits of the entry address */
148 desc->d[3] = (addr & 0xffff0000) >> 16;
149 }
151 #define IS_TRAP(n) ((n) >= 32 && !IS_IRQ(n))
152 static void set_intr_entry(int num, void (*handler)(void))
153 {
154 int type = IS_TRAP(num) ? GATE_TRAP : GATE_INTR;
155 gate_desc(idt + num, selector(SEGM_KCODE, 0), (uint32_t)handler, 0, type);
156 }
158 static void end_of_irq(int irq)
159 {
160 if(irq > 7) {
161 outb(OCW2_EOI, PIC2_CMD);
162 }
163 outb(OCW2_EOI, PIC1_CMD);
164 }