source: mainline/kernel/arch/ia32/src/interrupt.c@ 7e752b2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7e752b2 was 7e752b2, checked in by Martin Decky <martin@…>, 15 years ago
  • correct printf() formatting strings and corresponding arguments
  • minor cstyle changes and other small fixes
  • Property mode set to 100644
File size: 6.7 KB
Line 
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
61void (* disable_irqs_function)(uint16_t irqmask) = NULL;
62void (* enable_irqs_function)(uint16_t irqmask) = NULL;
63void (* eoi_function)(void) = NULL;
64
65void istate_decode(istate_t *istate)
66{
67 printf("cs =%#0" PRIx32 "\teip=%p\t"
68 "efl=%#0" PRIx32 "\terr=%#0" PRIx32 "\n",
69 istate->cs, (void *) istate->eip,
70 istate->eflags, istate->error_word);
71
72 printf("ds =%#0" PRIx32 "\tes =%#0" PRIx32 "\t"
73 "fs =%#0" PRIx32 "\tgs =%#0" PRIx32 "\n",
74 istate->ds, istate->es, istate->fs, istate->gs);
75
76 if (istate_from_uspace(istate))
77 printf("ss =%#0" PRIx32 "\n", istate->ss);
78
79 printf("eax=%#0" PRIx32 "\tebx=%#0" PRIx32 "\t"
80 "ecx=%#0" PRIx32 "\tedx=%#0" PRIx32 "\n",
81 istate->eax, istate->ebx, istate->ecx, istate->edx);
82
83 printf("esi=%p\tedi=%p\tebp=%p\tesp=%p\n",
84 (void *) istate->esi, (void *) istate->edi,
85 (void *) istate->ebp,
86 istate_from_uspace(istate) ? ((void *) istate->esp) :
87 &istate->esp);
88}
89
90static void trap_virtual_eoi(void)
91{
92 if (eoi_function)
93 eoi_function();
94 else
95 panic("No eoi_function.");
96
97}
98
99static void null_interrupt(unsigned int n, istate_t *istate)
100{
101 fault_if_from_uspace(istate, "Unserviced interrupt: %u.", n);
102 panic_badtrap(istate, n, "Unserviced interrupt: %u.", n);
103}
104
105static void de_fault(unsigned int n, istate_t *istate)
106{
107 fault_if_from_uspace(istate, "Divide error.");
108 panic_badtrap(istate, n, "Divide error.");
109}
110
111/** General Protection Fault. */
112static void gp_fault(unsigned int n __attribute__((unused)), istate_t *istate)
113{
114 if (TASK) {
115 irq_spinlock_lock(&TASK->lock, false);
116 size_t ver = TASK->arch.iomapver;
117 irq_spinlock_unlock(&TASK->lock, false);
118
119 if (CPU->arch.iomapver_copy != ver) {
120 /*
121 * This fault can be caused by an early access
122 * to I/O port because of an out-dated
123 * I/O Permission bitmap installed on CPU.
124 * Install the fresh copy and restart
125 * the instruction.
126 */
127 io_perm_bitmap_install();
128 return;
129 }
130 fault_if_from_uspace(istate, "General protection fault.");
131 }
132 panic_badtrap(istate, n, "General protection fault.");
133}
134
135static void ss_fault(unsigned int n __attribute__((unused)), istate_t *istate)
136{
137 fault_if_from_uspace(istate, "Stack fault.");
138 panic_badtrap(istate, n, "Stack fault.");
139}
140
141static void simd_fp_exception(unsigned int n __attribute__((unused)), istate_t *istate)
142{
143 uint32_t mxcsr;
144 asm volatile (
145 "stmxcsr %[mxcsr]\n"
146 : [mxcsr] "=m" (mxcsr)
147 );
148
149 fault_if_from_uspace(istate, "SIMD FP exception(19), MXCSR=%#0" PRIx32 ".",
150 mxcsr);
151 panic_badtrap(istate, n, "SIMD FP exception");
152}
153
154static void nm_fault(unsigned int n __attribute__((unused)),
155 istate_t *istate __attribute__((unused)))
156{
157#ifdef CONFIG_FPU_LAZY
158 scheduler_fpu_lazy_request();
159#else
160 fault_if_from_uspace(istate, "FPU fault.");
161 panic_badtrap(istate, n, "FPU fault.");
162#endif
163}
164
165#ifdef CONFIG_SMP
166static void tlb_shootdown_ipi(unsigned int n __attribute__((unused)),
167 istate_t *istate __attribute__((unused)))
168{
169 trap_virtual_eoi();
170 tlb_shootdown_ipi_recv();
171}
172#endif
173
174/** Handler of IRQ exceptions */
175static void irq_interrupt(unsigned int n, istate_t *istate __attribute__((unused)))
176{
177 ASSERT(n >= IVT_IRQBASE);
178
179 unsigned int inum = n - IVT_IRQBASE;
180 bool ack = false;
181 ASSERT(inum < IRQ_COUNT);
182 ASSERT((inum != IRQ_PIC_SPUR) && (inum != IRQ_PIC1));
183
184 irq_t *irq = irq_dispatch_and_lock(inum);
185 if (irq) {
186 /*
187 * The IRQ handler was found.
188 */
189
190 if (irq->preack) {
191 /* Send EOI before processing the interrupt */
192 trap_virtual_eoi();
193 ack = true;
194 }
195 irq->handler(irq);
196 irq_spinlock_unlock(&irq->lock, false);
197 } else {
198 /*
199 * Spurious interrupt.
200 */
201#ifdef CONFIG_DEBUG
202 printf("cpu%u: spurious interrupt (inum=%u)\n", CPU->id, inum);
203#endif
204 }
205
206 if (!ack)
207 trap_virtual_eoi();
208}
209
210void interrupt_init(void)
211{
212 unsigned int i;
213
214 for (i = 0; i < IVT_ITEMS; i++)
215 exc_register(i, "null", false, (iroutine_t) null_interrupt);
216
217 for (i = 0; i < IRQ_COUNT; i++) {
218 if ((i != IRQ_PIC_SPUR) && (i != IRQ_PIC1))
219 exc_register(IVT_IRQBASE + i, "irq", true,
220 (iroutine_t) irq_interrupt);
221 }
222
223 exc_register(0, "de_fault", true, (iroutine_t) de_fault);
224 exc_register(7, "nm_fault", true, (iroutine_t) nm_fault);
225 exc_register(12, "ss_fault", true, (iroutine_t) ss_fault);
226 exc_register(13, "gp_fault", true, (iroutine_t) gp_fault);
227 exc_register(19, "simd_fp", true, (iroutine_t) simd_fp_exception);
228
229#ifdef CONFIG_SMP
230 exc_register(VECTOR_TLB_SHOOTDOWN_IPI, "tlb_shootdown", true,
231 (iroutine_t) tlb_shootdown_ipi);
232#endif
233}
234
235void trap_virtual_enable_irqs(uint16_t irqmask)
236{
237 if (enable_irqs_function)
238 enable_irqs_function(irqmask);
239 else
240 panic("No enable_irqs_function.");
241}
242
243void trap_virtual_disable_irqs(uint16_t irqmask)
244{
245 if (disable_irqs_function)
246 disable_irqs_function(irqmask);
247 else
248 panic("No disable_irqs_function.");
249}
250
251/** @}
252 */
Note: See TracBrowser for help on using the repository browser.