Modestus Moon OS  R4
CS 450 project
interrupts.c
Go to the documentation of this file.
1 /*
2  ----- interrupt.c -----
3 
4  Description..: Interrupt handling routines for
5  traps, gates, exceptions.
6 */
7 
8 #include <system.h>
9 
10 #include <core/io.h>
11 #include <core/serial.h>
12 #include <core/tables.h>
13 #include <core/interrupts.h>
14 
15 // Programmable Interrupt Controllers
16 #define PIC1 0x20
17 #define PIC2 0xA0
18 
19 // Initialization Code Words
20 #define ICW1 0x11
21 #define ICW4 0x01
22 
23 /*
24  Procedure..: io_wait
25  Description..: The i386 can do an io wait by accessing another port.
26  Mainly used in initializing the PIC.
27 */
28 #define io_wait() asm volatile ("outb $0x80")
29 
30 extern void divide_error();
31 extern void debug();
32 extern void nmi();
33 extern void breakpoint();
34 extern void overflow();
35 extern void bounds();
36 extern void invalid_op();
37 extern void device_not_available();
38 extern void double_fault();
39 extern void coprocessor_segment();
40 extern void invalid_tss();
41 extern void segment_not_present();
42 extern void stack_segment();
43 extern void general_protection();
44 extern void page_fault();
45 extern void reserved();
46 extern void coprocessor();
47 extern void rtc_isr();
48 
49 extern void sys_call_isr();
50 
51 extern idt_entry idt_entries[256];
52 
53 //Current serial handler
54 extern void isr0();
55 void do_isr()
56 {
57  char in = inb(COM2);
58  serial_print(&in);
59  serial_println("here");
60  outb(0x20,0x20); //EOI
61 }
62 
63 /*
64  Procedure..: irq_init
65  Description..: Installs the initial interrupt handlers for
66  the first 32 irq lines. Most do a panic for now.
67 */
68 void init_irq(void)
69 {
70  int i;
71 
72  // Necessary interrupt handlers for protected mode
73  u32int isrs[17] = {
75  (u32int)debug,
76  (u32int)nmi,
79  (u32int)bounds,
91  };
92 
93  // Install handlers; 0x08=sel, 0x8e=flags
94  for(i=0; i<32; i++){
95  if (i<17) idt_set_gate(i, isrs[i], 0x08, 0x8e);
96  else idt_set_gate(i, (u32int)reserved, 0x08, 0x8e);
97  }
98  // Ignore interrupts from the real time clock
99  idt_set_gate(0x08, (u32int)rtc_isr, 0x08, 0x8e);
100  idt_set_gate(60, (u32int)sys_call_isr, 0x08, 0x8e);
101 }
102 
103 /*
104  Procedure..: pic_init
105  Description..: Initializes the programmable interrupt controllers
106  and performs the necessary remapping of IRQs. Leaves interrupts
107  turned off.
108 */
109 void init_pic(void)
110 {
111  outb(PIC1,ICW1); //send initialization code words 1 to PIC1
112  io_wait();
113  outb(PIC2,ICW1); //send icw1 to PIC2
114  io_wait();
115  outb(PIC1+1,0x20); //icw2: remap irq0 to 32
116  io_wait();
117  outb(PIC2+1,0x28); //icw2: remap irq8 to 40
118  io_wait();
119  outb(PIC1+1,4); //icw3
120  io_wait();
121  outb(PIC2+1,2); //icw3
122  io_wait();
123  outb(PIC1+1,ICW4); //icw4: 80x86, automatic handling
124  io_wait();
125  outb(PIC2+1,ICW4); //icw4: 80x86, automatic handling
126  io_wait();
127  outb(PIC1+1,0xFF); //disable irqs for PIC1
128  io_wait();
129  outb(PIC2+1,0xFF); //disable irqs for PIC2
130 }
131 
133 {
134  kpanic("Division-by-zero");
135 }
136 void do_debug()
137 {
138  kpanic("Debug");
139 }
140 void do_nmi()
141 {
142  kpanic("NMI");
143 }
145 {
146  kpanic("Breakpoint");
147 }
149 {
150  kpanic("Overflow error");
151 }
152 void do_bounds()
153 {
154  kpanic("Bounds error");
155 }
157 {
158  kpanic("Invalid operation");
159 }
161 {
162  kpanic("Device not available");
163 }
165 {
166  kpanic("Double fault");
167 }
169 {
170  kpanic("Coprocessor segment error");
171 }
173 {
174  kpanic("Invalid TSS");
175 }
177 {
178  kpanic("Segment not present");
179 }
181 {
182  kpanic("Stack segment error");
183 }
185 {
186  kpanic("General protection fault");
187 }
189 {
190  kpanic("Page Fault");
191 }
193 {
194  serial_println("die: reserved");
195 }
197 {
198  kpanic("Coprocessor error");
199 }
void divide_error()
#define PIC1
Definition: interrupts.c:16
void do_general_protection()
Definition: interrupts.c:184
void kpanic(const char *msg)
Definition: system.c:24
void invalid_op()
void isr0()
void breakpoint()
void sys_call_isr()
#define COM2
Definition: serial.h:7
int serial_print(const char *msg)
Definition: serial.c:59
void do_breakpoint()
Definition: interrupts.c:144
void do_invalid_tss()
Definition: interrupts.c:172
void overflow()
void do_isr()
Definition: interrupts.c:55
void do_bounds()
Definition: interrupts.c:152
#define PIC2
Definition: interrupts.c:17
void page_fault()
void stack_segment()
void do_page_fault()
Definition: interrupts.c:188
void general_protection()
void do_coprocessor()
Definition: interrupts.c:196
void double_fault()
void do_divide_error()
Definition: interrupts.c:132
void segment_not_present()
idt_entry idt_entries[256]
Definition: tables.c:17
#define ICW1
Definition: interrupts.c:20
void do_device_not_available()
Definition: interrupts.c:160
void do_double_fault()
Definition: interrupts.c:164
int serial_println(const char *msg)
Definition: serial.c:44
void do_segment_not_present()
Definition: interrupts.c:176
void init_pic(void)
Definition: interrupts.c:109
void do_stack_segment()
Definition: interrupts.c:180
void idt_set_gate(u8int idx, u32int base, u16int sel, u8int flags)
Definition: tables.c:27
void debug()
void coprocessor()
void bounds()
unsigned long u32int
Definition: system.h:27
void do_debug()
Definition: interrupts.c:136
#define inb(port)
Definition: io.h:15
void invalid_tss()
void rtc_isr()
void do_overflow()
Definition: interrupts.c:148
void do_nmi()
Definition: interrupts.c:140
void nmi()
void device_not_available()
void do_coprocessor_segment()
Definition: interrupts.c:168
void do_reserved()
Definition: interrupts.c:192
void coprocessor_segment()
void do_invalid_op()
Definition: interrupts.c:156
#define ICW4
Definition: interrupts.c:21
void reserved()
void init_irq(void)
Definition: interrupts.c:68
#define io_wait()
Definition: interrupts.c:28
#define outb(port, data)
Definition: io.h:8