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 <assert.h>
|
---|
37 | #include <syscall/syscall.h>
|
---|
38 | #include <print.h>
|
---|
39 | #include <debug.h>
|
---|
40 | #include <panic.h>
|
---|
41 | #include <arch/drivers/i8259.h>
|
---|
42 | #include <func.h>
|
---|
43 | #include <cpu.h>
|
---|
44 | #include <arch/asm.h>
|
---|
45 | #include <mm/tlb.h>
|
---|
46 | #include <mm/as.h>
|
---|
47 | #include <arch.h>
|
---|
48 | #include <proc/thread.h>
|
---|
49 | #include <proc/task.h>
|
---|
50 | #include <synch/spinlock.h>
|
---|
51 | #include <arch/ddi/ddi.h>
|
---|
52 | #include <ipc/sysipc.h>
|
---|
53 | #include <interrupt.h>
|
---|
54 | #include <ddi/irq.h>
|
---|
55 | #include <symtab.h>
|
---|
56 | #include <stacktrace.h>
|
---|
57 | #include <smp/smp_call.h>
|
---|
58 | #include <proc/task.h>
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * Interrupt and exception dispatching.
|
---|
62 | */
|
---|
63 |
|
---|
64 | void (* disable_irqs_function)(uint16_t irqmask) = NULL;
|
---|
65 | void (* enable_irqs_function)(uint16_t irqmask) = NULL;
|
---|
66 | void (* eoi_function)(void) = NULL;
|
---|
67 | const char *irqs_info = NULL;
|
---|
68 |
|
---|
69 | void istate_decode(istate_t *istate)
|
---|
70 | {
|
---|
71 | printf("cs =%0#10" PRIx32 "\teip=%0#10" PRIx32 "\t"
|
---|
72 | "efl=%0#10" PRIx32 "\terr=%0#10" PRIx32 "\n",
|
---|
73 | istate->cs, istate->eip, istate->eflags, istate->error_word);
|
---|
74 |
|
---|
75 | printf("ds =%0#10" PRIx32 "\tes =%0#10" PRIx32 "\t"
|
---|
76 | "fs =%0#10" PRIx32 "\tgs =%0#10" PRIx32 "\n",
|
---|
77 | istate->ds, istate->es, istate->fs, istate->gs);
|
---|
78 |
|
---|
79 | if (istate_from_uspace(istate))
|
---|
80 | printf("ss =%0#10" PRIx32 "\n", istate->ss);
|
---|
81 |
|
---|
82 | printf("eax=%0#10" PRIx32 "\tebx=%0#10" PRIx32 "\t"
|
---|
83 | "ecx=%0#10" PRIx32 "\tedx=%0#10" PRIx32 "\n",
|
---|
84 | istate->eax, istate->ebx, istate->ecx, istate->edx);
|
---|
85 |
|
---|
86 | printf("esi=%0#10" PRIx32 "\tedi=%0#10" PRIx32 "\t"
|
---|
87 | "ebp=%0#10" PRIx32 "\tesp=%0#10" PRIxn "\n",
|
---|
88 | istate->esi, istate->edi, istate->ebp,
|
---|
89 | istate_from_uspace(istate) ? istate->esp :
|
---|
90 | (uintptr_t) &istate->esp);
|
---|
91 | }
|
---|
92 |
|
---|
93 | static void trap_virtual_eoi(void)
|
---|
94 | {
|
---|
95 | if (eoi_function)
|
---|
96 | eoi_function();
|
---|
97 | else
|
---|
98 | panic("No eoi_function.");
|
---|
99 |
|
---|
100 | }
|
---|
101 |
|
---|
102 | static void null_interrupt(unsigned int n, istate_t *istate)
|
---|
103 | {
|
---|
104 | fault_if_from_uspace(istate, "Unserviced interrupt: %u.", n);
|
---|
105 | panic_badtrap(istate, n, "Unserviced interrupt: %u.", n);
|
---|
106 | }
|
---|
107 |
|
---|
108 | static void de_fault(unsigned int n, istate_t *istate)
|
---|
109 | {
|
---|
110 | fault_if_from_uspace(istate, "Divide error.");
|
---|
111 | panic_badtrap(istate, n, "Divide error.");
|
---|
112 | }
|
---|
113 |
|
---|
114 | /** General Protection Fault. */
|
---|
115 | static void gp_fault(unsigned int n __attribute__((unused)), istate_t *istate)
|
---|
116 | {
|
---|
117 | if (TASK) {
|
---|
118 | irq_spinlock_lock(&TASK->lock, false);
|
---|
119 | size_t ver = TASK->arch.iomapver;
|
---|
120 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
121 |
|
---|
122 | if (CPU->arch.iomapver_copy != ver) {
|
---|
123 | /*
|
---|
124 | * This fault can be caused by an early access
|
---|
125 | * to I/O port because of an out-dated
|
---|
126 | * I/O Permission bitmap installed on CPU.
|
---|
127 | * Install the fresh copy and restart
|
---|
128 | * the instruction.
|
---|
129 | */
|
---|
130 | io_perm_bitmap_install();
|
---|
131 | return;
|
---|
132 | }
|
---|
133 | fault_if_from_uspace(istate, "General protection fault.");
|
---|
134 | }
|
---|
135 | panic_badtrap(istate, n, "General protection fault.");
|
---|
136 | }
|
---|
137 |
|
---|
138 | static void ss_fault(unsigned int n __attribute__((unused)), istate_t *istate)
|
---|
139 | {
|
---|
140 | fault_if_from_uspace(istate, "Stack fault.");
|
---|
141 | panic_badtrap(istate, n, "Stack fault.");
|
---|
142 | }
|
---|
143 |
|
---|
144 | static void simd_fp_exception(unsigned int n __attribute__((unused)), istate_t *istate)
|
---|
145 | {
|
---|
146 | uint32_t mxcsr;
|
---|
147 | asm volatile (
|
---|
148 | "stmxcsr %[mxcsr]\n"
|
---|
149 | : [mxcsr] "=m" (mxcsr)
|
---|
150 | );
|
---|
151 |
|
---|
152 | fault_if_from_uspace(istate, "SIMD FP exception(19), MXCSR=%#0" PRIx32 ".",
|
---|
153 | mxcsr);
|
---|
154 | panic_badtrap(istate, n, "SIMD FP exception");
|
---|
155 | }
|
---|
156 |
|
---|
157 | static void nm_fault(unsigned int n __attribute__((unused)),
|
---|
158 | istate_t *istate __attribute__((unused)))
|
---|
159 | {
|
---|
160 | #ifdef CONFIG_FPU_LAZY
|
---|
161 | scheduler_fpu_lazy_request();
|
---|
162 | #else
|
---|
163 | fault_if_from_uspace(istate, "FPU fault.");
|
---|
164 | panic_badtrap(istate, n, "FPU fault.");
|
---|
165 | #endif
|
---|
166 | }
|
---|
167 |
|
---|
168 | #ifdef CONFIG_SMP
|
---|
169 | static void tlb_shootdown_ipi(unsigned int n __attribute__((unused)),
|
---|
170 | istate_t *istate __attribute__((unused)))
|
---|
171 | {
|
---|
172 | trap_virtual_eoi();
|
---|
173 | tlb_shootdown_ipi_recv();
|
---|
174 | }
|
---|
175 |
|
---|
176 | static void arch_smp_call_ipi_recv(unsigned int n, istate_t *istate)
|
---|
177 | {
|
---|
178 | trap_virtual_eoi();
|
---|
179 | smp_call_ipi_recv();
|
---|
180 | }
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | /** Handler of IRQ exceptions */
|
---|
184 | static void irq_interrupt(unsigned int n, istate_t *istate __attribute__((unused)))
|
---|
185 | {
|
---|
186 | assert(n >= IVT_IRQBASE);
|
---|
187 |
|
---|
188 | unsigned int inum = n - IVT_IRQBASE;
|
---|
189 | bool ack = false;
|
---|
190 | assert(inum < IRQ_COUNT);
|
---|
191 | assert((inum != IRQ_PIC_SPUR) && (inum != IRQ_PIC1));
|
---|
192 |
|
---|
193 | irq_t *irq = irq_dispatch_and_lock(inum);
|
---|
194 | if (irq) {
|
---|
195 | /*
|
---|
196 | * The IRQ handler was found.
|
---|
197 | */
|
---|
198 |
|
---|
199 | if (irq->preack) {
|
---|
200 | /* Send EOI before processing the interrupt */
|
---|
201 | trap_virtual_eoi();
|
---|
202 | ack = true;
|
---|
203 | }
|
---|
204 | irq->handler(irq);
|
---|
205 | irq_spinlock_unlock(&irq->lock, false);
|
---|
206 | } else {
|
---|
207 | /*
|
---|
208 | * Spurious interrupt.
|
---|
209 | */
|
---|
210 | #ifdef CONFIG_DEBUG
|
---|
211 | printf("cpu%u: spurious interrupt (inum=%u)\n", CPU->id, inum);
|
---|
212 | #endif
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (!ack)
|
---|
216 | trap_virtual_eoi();
|
---|
217 | }
|
---|
218 |
|
---|
219 | void interrupt_init(void)
|
---|
220 | {
|
---|
221 | unsigned int i;
|
---|
222 |
|
---|
223 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
224 | exc_register(i, "null", false, (iroutine_t) null_interrupt);
|
---|
225 |
|
---|
226 | for (i = 0; i < IRQ_COUNT; i++) {
|
---|
227 | if ((i != IRQ_PIC_SPUR) && (i != IRQ_PIC1))
|
---|
228 | exc_register(IVT_IRQBASE + i, "irq", true,
|
---|
229 | (iroutine_t) irq_interrupt);
|
---|
230 | }
|
---|
231 |
|
---|
232 | exc_register(VECTOR_DE, "de_fault", true, (iroutine_t) de_fault);
|
---|
233 | exc_register(VECTOR_NM, "nm_fault", true, (iroutine_t) nm_fault);
|
---|
234 | exc_register(VECTOR_SS, "ss_fault", true, (iroutine_t) ss_fault);
|
---|
235 | exc_register(VECTOR_GP, "gp_fault", true, (iroutine_t) gp_fault);
|
---|
236 | exc_register(VECTOR_XM, "simd_fp", true, (iroutine_t) simd_fp_exception);
|
---|
237 |
|
---|
238 | #ifdef CONFIG_SMP
|
---|
239 | exc_register(VECTOR_TLB_SHOOTDOWN_IPI, "tlb_shootdown", true,
|
---|
240 | (iroutine_t) tlb_shootdown_ipi);
|
---|
241 | exc_register(VECTOR_SMP_CALL_IPI, "smp_call", true,
|
---|
242 | (iroutine_t) arch_smp_call_ipi_recv);
|
---|
243 | #endif
|
---|
244 | }
|
---|
245 |
|
---|
246 | void trap_virtual_enable_irqs(uint16_t irqmask)
|
---|
247 | {
|
---|
248 | if (enable_irqs_function)
|
---|
249 | enable_irqs_function(irqmask);
|
---|
250 | else
|
---|
251 | panic("No enable_irqs_function.");
|
---|
252 | }
|
---|
253 |
|
---|
254 | void trap_virtual_disable_irqs(uint16_t irqmask)
|
---|
255 | {
|
---|
256 | if (disable_irqs_function)
|
---|
257 | disable_irqs_function(irqmask);
|
---|
258 | else
|
---|
259 | panic("No disable_irqs_function.");
|
---|
260 | }
|
---|
261 |
|
---|
262 | /** @}
|
---|
263 | */
|
---|