source: mainline/kernel/arch/ia32/src/interrupt.c@ 40043e8

Last change on this file since 40043e8 was 2cc569a3, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Removing inclusion of halt.h

Several files used to include halt.h even though
this was not necessary. The inclusion has therefore
been removed

  • Property mode set to 100644
File size: 7.2 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 kernel_ia32_interrupt
30 * @{
31 */
32/** @file
33 */
34
35#include <arch/interrupt.h>
36#include <assert.h>
37#include <syscall/syscall.h>
38#include <stdio.h>
39#include <debug.h>
40#include <panic.h>
41#include <genarch/drivers/i8259/i8259.h>
42#include <genarch/pic/pic_ops.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 <proc/task.h>
58
59/*
60 * Interrupt and exception dispatching.
61 */
62
63pic_ops_t *pic_ops = NULL;
64
65void istate_decode(istate_t *istate)
66{
67 printf("cs =%0#10" PRIx32 "\teip=%0#10" PRIx32 "\t"
68 "efl=%0#10" PRIx32 "\terr=%0#10" PRIx32 "\n",
69 istate->cs, istate->eip, istate->eflags, istate->error_word);
70
71 printf("ds =%0#10" PRIx32 "\tes =%0#10" PRIx32 "\t"
72 "fs =%0#10" PRIx32 "\tgs =%0#10" PRIx32 "\n",
73 istate->ds, istate->es, istate->fs, istate->gs);
74
75 if (istate_from_uspace(istate))
76 printf("ss =%0#10" PRIx32 "\n", istate->ss);
77
78 printf("eax=%0#10" PRIx32 "\tebx=%0#10" PRIx32 "\t"
79 "ecx=%0#10" PRIx32 "\tedx=%0#10" PRIx32 "\n",
80 istate->eax, istate->ebx, istate->ecx, istate->edx);
81
82 printf("esi=%0#10" PRIx32 "\tedi=%0#10" PRIx32 "\t"
83 "ebp=%0#10" PRIx32 "\tesp=%0#10" PRIx32 "\n",
84 istate->esi, istate->edi, istate->ebp,
85 istate_from_uspace(istate) ? istate->esp :
86 (uint32_t) &istate->esp);
87}
88
89static void null_interrupt(unsigned int n, istate_t *istate)
90{
91 fault_if_from_uspace(istate, "Unserviced interrupt: %u.", n);
92 panic_badtrap(istate, n, "Unserviced interrupt: %u.", n);
93}
94
95static void de_fault(unsigned int n, istate_t *istate)
96{
97 fault_if_from_uspace(istate, "Divide error.");
98 panic_badtrap(istate, n, "Divide error.");
99}
100
101static void db_exception(unsigned int n, istate_t *istate)
102{
103 /*
104 * We need to provide at least an empty handler that does not panic
105 * if the exception appears to come from the kernel because the
106 * userspace can inject a kernel-level #DB after e.g. the SYSENTER
107 * instruction if the EFLAGS.TF is set.
108 */
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 pic_ops->eoi(0);
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_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 pic_ops->eoi(inum);
193 ack = true;
194 }
195 irq->handler(irq);
196 irq_spinlock_unlock(&irq->lock, false);
197 } else {
198#ifdef CONFIG_DEBUG
199 log(LF_ARCH, LVL_DEBUG, "cpu%u: unhandled IRQ %u", CPU->id,
200 inum);
201#endif
202 }
203
204 if (!ack)
205 pic_ops->eoi(inum);
206}
207
208static void pic_spurious(unsigned int n, istate_t *istate)
209{
210 unsigned int inum = n - IVT_IRQBASE;
211 if (!pic_ops->is_spurious(inum)) {
212 /* This is actually not a spurious IRQ, so proceed as usual. */
213 irq_interrupt(n, istate);
214 return;
215 }
216 pic_ops->handle_spurious(n);
217#ifdef CONFIG_DEBUG
218 log(LF_ARCH, LVL_DEBUG, "cpu%u: PIC spurious interrupt %u", CPU->id,
219 inum);
220#endif
221}
222
223void interrupt_init(void)
224{
225 unsigned int i;
226
227 for (i = 0; i < IVT_ITEMS; i++)
228 exc_register(i, "null", false, (iroutine_t) null_interrupt);
229
230 for (i = 0; i < IRQ_COUNT; i++) {
231 if ((i != IRQ_PIC0_SPUR) && (i != IRQ_PIC1_SPUR) &&
232 (i != IRQ_PIC1))
233 exc_register(IVT_IRQBASE + i, "irq", true,
234 (iroutine_t) irq_interrupt);
235 }
236
237 exc_register(VECTOR_DE, "de_fault", true, (iroutine_t) de_fault);
238 exc_register(VECTOR_DB, "db_exc", true, (iroutine_t) db_exception);
239 exc_register(VECTOR_NM, "nm_fault", true, (iroutine_t) nm_fault);
240 exc_register(VECTOR_SS, "ss_fault", true, (iroutine_t) ss_fault);
241 exc_register(VECTOR_GP, "gp_fault", true, (iroutine_t) gp_fault);
242 exc_register(VECTOR_XM, "simd_fp", true, (iroutine_t) simd_fp_exception);
243 exc_register(VECTOR_PIC0_SPUR, "pic0_spurious", true,
244 (iroutine_t) pic_spurious);
245 exc_register(VECTOR_PIC1_SPUR, "pic1_spurious", true,
246 (iroutine_t) pic_spurious);
247
248#ifdef CONFIG_SMP
249 exc_register(VECTOR_TLB_SHOOTDOWN_IPI, "tlb_shootdown", true,
250 (iroutine_t) tlb_shootdown_ipi);
251#endif
252}
253
254/** @}
255 */
Note: See TracBrowser for help on using the repository browser.