backported fixes from 256boss
[bootcensus] / src / intr.c
1 /*
2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY, without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include "config.h"
20 #include "intr.h"
21 #include "desc.h"
22 #include "segm.h"
23 #include "asmops.h"
24 #include "panic.h"
25
26 #define SYSCALL_INT             0x80
27
28 /* IDT gate descriptor bits */
29 #define GATE_TASK               (5 << 8)
30 #define GATE_INTR               (6 << 8)
31 #define GATE_TRAP               (7 << 8)
32 #define GATE_DEFAULT    (1 << 11)
33 #define GATE_PRESENT    (1 << 15)
34
35 /* PIC command and data ports */
36 #define PIC1_CMD        0x20
37 #define PIC1_DATA       0x21
38 #define PIC2_CMD        0xa0
39 #define PIC2_DATA       0xa1
40
41 /* PIC initialization command word 1 bits */
42 #define ICW1_ICW4_NEEDED        (1 << 0)
43 #define ICW1_SINGLE                     (1 << 1)
44 #define ICW1_INTERVAL4          (1 << 2)
45 #define ICW1_LEVEL                      (1 << 3)
46 #define ICW1_INIT                       (1 << 4)
47 /* PIC initialization command word 4 bits */
48 #define ICW4_8086                       (1 << 0)
49 #define ICW4_AUTO_EOI           (1 << 1)
50 #define ICW4_BUF_SLAVE          (1 << 3) /* 1000 */
51 #define ICW4_BUF_MASTER         (3 << 2) /* 1100 */
52 #define ICW4_SPECIAL            (1 << 4)
53
54 /* PIC operation command word 2 bits */
55 #define OCW2_EOI        (1 << 5)
56
57
58 void init_pic(void);
59 static void gate_desc(desc_t *desc, uint16_t sel, uint32_t addr, int dpl, int type);
60
61 /* defined in intr_asm.S */
62 void set_idt(uint32_t addr, uint16_t limit);
63 void intr_entry_default(void);
64 void irq7_entry_check_spurious(void);
65 void irq15_entry_check_spurious(void);
66
67 /* the IDT (interrupt descriptor table) */
68 static desc_t idt[256] __attribute__((aligned(8)));
69
70 /* table of handler functions for all interrupts */
71 static intr_func_t intr_func[256];
72
73 static struct intr_frame *cur_intr_frame;
74 static int eoi_pending;
75
76
77 void init_intr(void)
78 {
79         int i;
80
81         set_idt((uint32_t)idt, sizeof idt - 1);
82
83         /* initialize all entry points and interrupt handlers */
84         for(i=0; i<256; i++) {
85                 set_intr_entry(i, intr_entry_default);
86                 interrupt(i, 0);
87         }
88
89         /* by including intrtab.h here (without ASM being defined)
90          * the series of INTR_ENTRY_* macros will be expanded to a series
91          * of function prototypes for all interrupt entry points and the
92          * corresponding calls to set_intr_entry to set up the IDT slots
93          */
94 #include "intrtab.h"
95
96         /* change irq7 and irq15 to special entry points which first
97          * make sure we didn't get a spurious interrupt before proceeding
98          */
99         set_intr_entry(IRQ_TO_INTR(7), irq7_entry_check_spurious);
100         set_intr_entry(IRQ_TO_INTR(15), irq15_entry_check_spurious);
101
102         /* initialize the programmable interrupt controller
103          * setting up the maping of IRQs [0, 15] to interrupts [32, 47]
104          */
105         init_pic();
106         eoi_pending = 0;
107 }
108
109 /* retrieve the current interrupt frame.
110  * returns 0 when called during init.
111  */
112 struct intr_frame *get_intr_frame(void)
113 {
114         return cur_intr_frame;
115 }
116
117 /* set an interrupt handler function for a particular interrupt */
118 void interrupt(int intr_num, intr_func_t func)
119 {
120         int iflag = get_intr_flag();
121         disable_intr();
122         intr_func[intr_num] = func;
123         set_intr_flag(iflag);
124 }
125
126 /* this function is called from all interrupt entry points
127  * it calls the appropriate interrupt handlers if available and handles
128  * sending an end-of-interrupt command to the PICs when finished.
129  */
130 void dispatch_intr(struct intr_frame frm)
131 {
132         cur_intr_frame = &frm;
133
134         if(IS_IRQ(frm.inum)) {
135                 eoi_pending = frm.inum;
136         }
137
138         if(intr_func[frm.inum]) {
139                 intr_func[frm.inum](frm.inum);
140         } else {
141                 if(frm.inum < 32) {
142                         panic("unhandled exception %d, error code: %d\n", frm.inum, frm.err);
143                 }
144                 printf("unhandled interrupt %d\n", frm.inum);
145         }
146
147         disable_intr();
148         if(eoi_pending) {
149                 end_of_irq(INTR_TO_IRQ(eoi_pending));
150         }
151 }
152
153 void init_pic(void)
154 {
155         prog_pic(IRQ_OFFSET);
156 }
157
158 void prog_pic(int offs)
159 {
160         /* send ICW1 saying we'll follow with ICW4 later on */
161         outb(ICW1_INIT | ICW1_ICW4_NEEDED, PIC1_CMD);
162         outb(ICW1_INIT | ICW1_ICW4_NEEDED, PIC2_CMD);
163         /* send ICW2 with IRQ remapping */
164         outb(offs, PIC1_DATA);
165         outb(offs + 8, PIC2_DATA);
166         /* send ICW3 to setup the master/slave relationship */
167         /* ... set bit3 = 3rd interrupt input has a slave */
168         outb(4, PIC1_DATA);
169         /* ... set slave ID to 2 */
170         outb(2, PIC2_DATA);
171         /* send ICW4 to set 8086 mode (no calls generated) */
172         outb(ICW4_8086, PIC1_DATA);
173         outb(ICW4_8086, PIC2_DATA);
174         /* done, just reset the data port to 0 */
175         outb(0, PIC1_DATA);
176         outb(0, PIC2_DATA);
177 }
178
179 static void gate_desc(desc_t *desc, uint16_t sel, uint32_t addr, int dpl, int type)
180 {
181         /* first 16bit part is the low 16bits of the entry address */
182         desc->d[0] = addr & 0xffff;
183         /* second 16bit part is the segment selector for the entry code */
184         desc->d[1] = sel;
185         /* third 16bit part has the privilege level, type, and present bit */
186         desc->d[2] = ((dpl & 3) << 13) | type | GATE_DEFAULT | GATE_PRESENT;
187         /* last 16bit part is the high 16bits of the entry address */
188         desc->d[3] = (addr & 0xffff0000) >> 16;
189 }
190
191 #define IS_TRAP(n)      ((n) >= 32 && !IS_IRQ(n))
192 void set_intr_entry(int num, void (*handler)(void))
193 {
194         int type = IS_TRAP(num) ? GATE_TRAP : GATE_INTR;
195
196         /* the syscall interrupt has to have a dpl of 3 otherwise calling it from
197          * user space will raise a general protection exception. All the rest should
198          * have a dpl of 0 to disallow user programs to execute critical interrupt
199          * handlers and possibly crashing the system.
200          */
201         int dpl = (num == SYSCALL_INT) ? 3 : 0;
202
203         gate_desc(idt + num, selector(SEGM_KCODE, 0), (uint32_t)handler, dpl, type);
204 }
205
206 void set_pic_mask(int pic, unsigned char mask)
207 {
208         outb(mask, pic > 0 ? PIC2_DATA : PIC1_DATA);
209 }
210
211 unsigned char get_pic_mask(int pic)
212 {
213         return inb(pic > 0 ? PIC2_DATA : PIC1_DATA);
214 }
215
216 void mask_irq(int irq)
217 {
218         int port;
219         unsigned char mask;
220
221         if(irq < 8) {
222                 port = PIC1_DATA;
223         } else {
224                 port = PIC2_DATA;
225                 irq -= 8;
226         }
227
228         mask = inb(port) | (1 << irq);
229         outb(mask, port);
230 }
231
232 void unmask_irq(int irq)
233 {
234         int port;
235         unsigned char mask;
236
237         if(irq < 8) {
238                 port = PIC1_DATA;
239         } else {
240                 port = PIC2_DATA;
241                 irq -= 8;
242         }
243
244         mask = inb(port) & ~(1 << irq);
245         outb(mask, port);
246 }
247
248
249 void end_of_irq(int irq)
250 {
251         int intr_state = get_intr_flag();
252         disable_intr();
253
254         if(!eoi_pending) {
255                 return;
256         }
257         eoi_pending = 0;
258
259         if(irq > 7) {
260                 outb(OCW2_EOI, PIC2_CMD);
261         }
262         outb(OCW2_EOI, PIC1_CMD);
263
264         set_intr_flag(intr_state);
265 }
266
267 #ifdef ENABLE_GDB_STUB
268 void exceptionHandler(int id, void (*func)())
269 {
270         set_intr_entry(id, func);
271 }
272 #endif