1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup ia32interrupt
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <arch/interrupt.h>
|
---|
36 | #include <syscall/syscall.h>
|
---|
37 | #include <print.h>
|
---|
38 | #include <debug.h>
|
---|
39 | #include <panic.h>
|
---|
40 | #include <arch/drivers/i8259.h>
|
---|
41 | #include <func.h>
|
---|
42 | #include <cpu.h>
|
---|
43 | #include <arch/asm.h>
|
---|
44 | #include <mm/tlb.h>
|
---|
45 | #include <mm/as.h>
|
---|
46 | #include <arch.h>
|
---|
47 | #include <proc/thread.h>
|
---|
48 | #include <proc/task.h>
|
---|
49 | #include <synch/spinlock.h>
|
---|
50 | #include <arch/ddi/ddi.h>
|
---|
51 | #include <ipc/sysipc.h>
|
---|
52 | #include <interrupt.h>
|
---|
53 | #include <ddi/irq.h>
|
---|
54 | #include <symtab.h>
|
---|
55 | #include <stacktrace.h>
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * Interrupt and exception dispatching.
|
---|
59 | */
|
---|
60 |
|
---|
61 | void (* disable_irqs_function)(uint16_t irqmask) = NULL;
|
---|
62 | void (* enable_irqs_function)(uint16_t irqmask) = NULL;
|
---|
63 | void (* eoi_function)(void) = NULL;
|
---|
64 |
|
---|
65 | void decode_istate(istate_t *istate)
|
---|
66 | {
|
---|
67 | const char *symbol = symtab_fmt_name_lookup(istate->eip);
|
---|
68 |
|
---|
69 | if (CPU)
|
---|
70 | printf("----------------EXCEPTION OCCURED (cpu%u)----------------\n", CPU->id);
|
---|
71 | else
|
---|
72 | printf("----------------EXCEPTION OCCURED----------------\n");
|
---|
73 |
|
---|
74 | printf("%%eip: %#lx (%s)\n", istate->eip, symbol);
|
---|
75 | printf("ERROR_WORD=%#lx\n", istate->error_word);
|
---|
76 | printf("%%cs=%#lx,flags=%#lx\n", istate->cs, istate->eflags);
|
---|
77 | printf("%%eax=%#lx, %%ecx=%#lx, %%edx=%#lx, %%esp=%p\n", istate->eax, istate->ecx, istate->edx, &istate->stack[0]);
|
---|
78 | printf("stack: %#lx, %#lx, %#lx, %#lx\n", istate->stack[0], istate->stack[1], istate->stack[2], istate->stack[3]);
|
---|
79 | printf(" %#lx, %#lx, %#lx, %#lx\n", istate->stack[4], istate->stack[5], istate->stack[6], istate->stack[7]);
|
---|
80 |
|
---|
81 | stack_trace_istate(istate);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static void trap_virtual_eoi(void)
|
---|
85 | {
|
---|
86 | if (eoi_function)
|
---|
87 | eoi_function();
|
---|
88 | else
|
---|
89 | panic("No eoi_function.");
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | static void null_interrupt(int n, istate_t *istate)
|
---|
94 | {
|
---|
95 | fault_if_from_uspace(istate, "Unserviced interrupt: %d.", n);
|
---|
96 |
|
---|
97 | decode_istate(istate);
|
---|
98 | panic("Unserviced interrupt: %d.", n);
|
---|
99 | }
|
---|
100 |
|
---|
101 | static void de_fault(int n, istate_t *istate)
|
---|
102 | {
|
---|
103 | fault_if_from_uspace(istate, "Divide error.");
|
---|
104 |
|
---|
105 | decode_istate(istate);
|
---|
106 | panic("Divide error.");
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** General Protection Fault. */
|
---|
110 | static void gp_fault(int n __attribute__((unused)), istate_t *istate)
|
---|
111 | {
|
---|
112 | if (TASK) {
|
---|
113 | irq_spinlock_lock(&TASK->lock, false);
|
---|
114 | size_t ver = TASK->arch.iomapver;
|
---|
115 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
116 |
|
---|
117 | if (CPU->arch.iomapver_copy != ver) {
|
---|
118 | /*
|
---|
119 | * This fault can be caused by an early access
|
---|
120 | * to I/O port because of an out-dated
|
---|
121 | * I/O Permission bitmap installed on CPU.
|
---|
122 | * Install the fresh copy and restart
|
---|
123 | * the instruction.
|
---|
124 | */
|
---|
125 | io_perm_bitmap_install();
|
---|
126 | return;
|
---|
127 | }
|
---|
128 | fault_if_from_uspace(istate, "General protection fault.");
|
---|
129 | }
|
---|
130 |
|
---|
131 | decode_istate(istate);
|
---|
132 | panic("General protection fault.");
|
---|
133 | }
|
---|
134 |
|
---|
135 | static void ss_fault(int n __attribute__((unused)), istate_t *istate)
|
---|
136 | {
|
---|
137 | fault_if_from_uspace(istate, "Stack fault.");
|
---|
138 |
|
---|
139 | decode_istate(istate);
|
---|
140 | panic("Stack fault.");
|
---|
141 | }
|
---|
142 |
|
---|
143 | static void simd_fp_exception(int n __attribute__((unused)), istate_t *istate)
|
---|
144 | {
|
---|
145 | uint32_t mxcsr;
|
---|
146 | asm volatile (
|
---|
147 | "stmxcsr %[mxcsr]\n"
|
---|
148 | : [mxcsr] "=m" (mxcsr)
|
---|
149 | );
|
---|
150 |
|
---|
151 | fault_if_from_uspace(istate, "SIMD FP exception(19), MXCSR: %#zx.",
|
---|
152 | (unative_t) mxcsr);
|
---|
153 |
|
---|
154 | decode_istate(istate);
|
---|
155 | printf("MXCSR: %#lx\n", mxcsr);
|
---|
156 | panic("SIMD FP exception(19).");
|
---|
157 | }
|
---|
158 |
|
---|
159 | static void nm_fault(int n __attribute__((unused)),
|
---|
160 | istate_t *istate __attribute__((unused)))
|
---|
161 | {
|
---|
162 | #ifdef CONFIG_FPU_LAZY
|
---|
163 | scheduler_fpu_lazy_request();
|
---|
164 | #else
|
---|
165 | fault_if_from_uspace(istate, "FPU fault.");
|
---|
166 | panic("FPU fault.");
|
---|
167 | #endif
|
---|
168 | }
|
---|
169 |
|
---|
170 | #ifdef CONFIG_SMP
|
---|
171 | static void tlb_shootdown_ipi(int n __attribute__((unused)),
|
---|
172 | istate_t *istate __attribute__((unused)))
|
---|
173 | {
|
---|
174 | trap_virtual_eoi();
|
---|
175 | tlb_shootdown_ipi_recv();
|
---|
176 | }
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | /** Handler of IRQ exceptions */
|
---|
180 | static void irq_interrupt(int n, istate_t *istate __attribute__((unused)))
|
---|
181 | {
|
---|
182 | ASSERT(n >= IVT_IRQBASE);
|
---|
183 |
|
---|
184 | int inum = n - IVT_IRQBASE;
|
---|
185 | bool ack = false;
|
---|
186 | ASSERT(inum < IRQ_COUNT);
|
---|
187 | ASSERT((inum != IRQ_PIC_SPUR) && (inum != IRQ_PIC1));
|
---|
188 |
|
---|
189 | irq_t *irq = irq_dispatch_and_lock(inum);
|
---|
190 | if (irq) {
|
---|
191 | /*
|
---|
192 | * The IRQ handler was found.
|
---|
193 | */
|
---|
194 |
|
---|
195 | if (irq->preack) {
|
---|
196 | /* Send EOI before processing the interrupt */
|
---|
197 | trap_virtual_eoi();
|
---|
198 | ack = true;
|
---|
199 | }
|
---|
200 | irq->handler(irq);
|
---|
201 | irq_spinlock_unlock(&irq->lock, false);
|
---|
202 | } else {
|
---|
203 | /*
|
---|
204 | * Spurious interrupt.
|
---|
205 | */
|
---|
206 | #ifdef CONFIG_DEBUG
|
---|
207 | printf("cpu%u: spurious interrupt (inum=%d)\n", CPU->id, inum);
|
---|
208 | #endif
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (!ack)
|
---|
212 | trap_virtual_eoi();
|
---|
213 | }
|
---|
214 |
|
---|
215 | void interrupt_init(void)
|
---|
216 | {
|
---|
217 | int i;
|
---|
218 |
|
---|
219 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
220 | exc_register(i, "null", (iroutine) null_interrupt);
|
---|
221 |
|
---|
222 | for (i = 0; i < IRQ_COUNT; i++) {
|
---|
223 | if ((i != IRQ_PIC_SPUR) && (i != IRQ_PIC1))
|
---|
224 | exc_register(IVT_IRQBASE + i, "irq", (iroutine) irq_interrupt);
|
---|
225 | }
|
---|
226 |
|
---|
227 | exc_register(0, "de_fault", (iroutine) de_fault);
|
---|
228 | exc_register(7, "nm_fault", (iroutine) nm_fault);
|
---|
229 | exc_register(12, "ss_fault", (iroutine) ss_fault);
|
---|
230 | exc_register(13, "gp_fault", (iroutine) gp_fault);
|
---|
231 | exc_register(19, "simd_fp", (iroutine) simd_fp_exception);
|
---|
232 |
|
---|
233 | #ifdef CONFIG_SMP
|
---|
234 | exc_register(VECTOR_TLB_SHOOTDOWN_IPI, "tlb_shootdown", (iroutine) tlb_shootdown_ipi);
|
---|
235 | #endif
|
---|
236 | }
|
---|
237 |
|
---|
238 | void trap_virtual_enable_irqs(uint16_t irqmask)
|
---|
239 | {
|
---|
240 | if (enable_irqs_function)
|
---|
241 | enable_irqs_function(irqmask);
|
---|
242 | else
|
---|
243 | panic("No enable_irqs_function.");
|
---|
244 | }
|
---|
245 |
|
---|
246 | void trap_virtual_disable_irqs(uint16_t irqmask)
|
---|
247 | {
|
---|
248 | if (disable_irqs_function)
|
---|
249 | disable_irqs_function(irqmask);
|
---|
250 | else
|
---|
251 | panic("No disable_irqs_function.");
|
---|
252 | }
|
---|
253 |
|
---|
254 | /** @}
|
---|
255 | */
|
---|