works and returns to DOS on dosbox
[com32] / 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 #define INTR_ENTRY_EC(n, name)          \
77         void intr_entry_##name(void);   \
78         set_intr_entry(n, intr_entry_##name);
79 #define INTR_ENTRY_NOEC(n, name)        INTR_ENTRY_EC(n, name)
80
81 void init_intr(void)
82 {
83         int i;
84
85         set_idt((uint32_t)idt, sizeof idt - 1);
86
87         /* initialize all entry points and interrupt handlers */
88         for(i=0; i<256; i++) {
89                 set_intr_entry(i, intr_entry_default);
90                 interrupt(i, 0);
91         }
92
93         /* by including intrtab.h here the series of INTR_ENTRY_* macros will be
94          * expanded to a series of function prototypes for all interrupt entry
95          * points and the corresponding calls to set_intr_entry to set up the IDT
96          * slots
97          */
98 #include "intrtab.h"
99
100         /* change irq7 and irq15 to special entry points which first
101          * make sure we didn't get a spurious interrupt before proceeding
102          */
103         set_intr_entry(IRQ_TO_INTR(7), irq7_entry_check_spurious);
104         set_intr_entry(IRQ_TO_INTR(15), irq15_entry_check_spurious);
105
106         /* initialize the programmable interrupt controller
107          * setting up the maping of IRQs [0, 15] to interrupts [32, 47]
108          */
109         prog_pic(IRQ_OFFSET);
110         eoi_pending = 0;
111 }
112
113 void cleanup_intr(void)
114 {
115         disable_intr();
116         /* reprogram the PIC to the default BIOS offset (8) */
117         prog_pic(8);
118 }
119
120 /* retrieve the current interrupt frame.
121  * returns 0 when called during init.
122  */
123 struct intr_frame *get_intr_frame(void)
124 {
125         return cur_intr_frame;
126 }
127
128 /* set an interrupt handler function for a particular interrupt */
129 void interrupt(int intr_num, intr_func_t func)
130 {
131         int iflag = get_intr_flag();
132         disable_intr();
133         intr_func[intr_num] = func;
134         set_intr_flag(iflag);
135 }
136
137 /* this function is called from all interrupt entry points
138  * it calls the appropriate interrupt handlers if available and handles
139  * sending an end-of-interrupt command to the PICs when finished.
140  */
141 void dispatch_intr(struct intr_frame frm)
142 {
143         cur_intr_frame = &frm;
144
145         if(IS_IRQ(frm.inum)) {
146                 eoi_pending = frm.inum;
147         }
148
149         if(intr_func[frm.inum]) {
150                 intr_func[frm.inum](frm.inum);
151         } else {
152                 if(frm.inum < 32) {
153                         panic("unhandled exception %u, error code: %u, at cs:eip=%x:%x\n",
154                                         frm.inum, frm.err, frm.cs, frm.eip);
155                 }
156                 /*printf("unhandled interrupt %d\n", frm.inum);*/
157         }
158
159         disable_intr();
160         if(eoi_pending) {
161                 end_of_irq(INTR_TO_IRQ(eoi_pending));
162         }
163 }
164
165 void prog_pic(int offs)
166 {
167         /* send ICW1 saying we'll follow with ICW4 later on */
168         outp(PIC1_CMD, ICW1_INIT | ICW1_ICW4_NEEDED);
169         outp(PIC2_CMD, ICW1_INIT | ICW1_ICW4_NEEDED);
170         /* send ICW2 with IRQ remapping */
171         outp(PIC1_DATA, offs);
172         outp(PIC2_DATA, offs + 8);
173         /* send ICW3 to setup the master/slave relationship */
174         /* ... set bit3 = 3rd interrupt input has a slave */
175         outp(PIC1_DATA, 4);
176         /* ... set slave ID to 2 */
177         outp(PIC2_DATA, 2);
178         /* send ICW4 to set 8086 mode (no calls generated) */
179         outp(PIC1_DATA, ICW4_8086);
180         outp(PIC2_DATA, ICW4_8086);
181         /* done, just reset the data port to 0 */
182         outp(PIC1_DATA, 0);
183         outp(PIC2_DATA, 0);
184 }
185
186 static void gate_desc(desc_t *desc, uint16_t sel, uint32_t addr, int dpl, int type)
187 {
188         /* first 16bit part is the low 16bits of the entry address */
189         desc->d[0] = addr & 0xffff;
190         /* second 16bit part is the segment selector for the entry code */
191         desc->d[1] = sel;
192         /* third 16bit part has the privilege level, type, and present bit */
193         desc->d[2] = ((dpl & 3) << 13) | type | GATE_DEFAULT | GATE_PRESENT;
194         /* last 16bit part is the high 16bits of the entry address */
195         desc->d[3] = (addr & 0xffff0000) >> 16;
196 }
197
198 #define IS_TRAP(n)      ((n) >= 32 && !IS_IRQ(n))
199 void set_intr_entry(int num, void (*handler)(void))
200 {
201         int type = IS_TRAP(num) ? GATE_TRAP : GATE_INTR;
202
203         /* the syscall interrupt has to have a dpl of 3 otherwise calling it from
204          * user space will raise a general protection exception. All the rest should
205          * have a dpl of 0 to disallow user programs to execute critical interrupt
206          * handlers and possibly crashing the system.
207          */
208         int dpl = (num == SYSCALL_INT) ? 3 : 0;
209
210         gate_desc(idt + num, selector(SEGM_CODE, 0), (uint32_t)handler, dpl, type);
211 }
212
213 void set_pic_mask(int pic, unsigned char mask)
214 {
215         outp(pic > 0 ? PIC2_DATA : PIC1_DATA, mask);
216 }
217
218 unsigned char get_pic_mask(int pic)
219 {
220         return inp(pic > 0 ? PIC2_DATA : PIC1_DATA);
221 }
222
223 void mask_irq(int irq)
224 {
225         int port;
226         unsigned char mask;
227
228         if(irq < 8) {
229                 port = PIC1_DATA;
230         } else {
231                 port = PIC2_DATA;
232                 irq -= 8;
233         }
234
235         mask = inp(port) | (1 << irq);
236         outp(port, mask);
237 }
238
239 void unmask_irq(int irq)
240 {
241         int port;
242         unsigned char mask;
243
244         if(irq < 8) {
245                 port = PIC1_DATA;
246         } else {
247                 port = PIC2_DATA;
248                 irq -= 8;
249         }
250
251         mask = inp(port) & ~(1 << irq);
252         outp(port, mask);
253 }
254
255
256 void end_of_irq(int irq)
257 {
258         int intr_state = get_intr_flag();
259         disable_intr();
260
261         if(!eoi_pending) {
262                 set_intr_flag(intr_state);
263                 return;
264         }
265         eoi_pending = 0;
266
267         if(irq > 7) {
268                 outp(PIC2_CMD, OCW2_EOI);
269         }
270         outp(PIC1_CMD, OCW2_EOI);
271
272         set_intr_flag(intr_state);
273 }
274
275 #ifdef ENABLE_GDB_STUB
276 void exceptionHandler(int id, void (*func)())
277 {
278         set_intr_entry(id, func);
279 }
280 #endif