source: mainline/kernel/arch/ia32/src/interrupt.c

Last change on this file was 2a103b5, checked in by Jakub Jermar <jakub@…>, 6 years ago

Introduce PIC operations indirection mechanism

Some architectures switch from one interrupt controller implementation
to another during runtime. By providing a cleaner indirection mechanism,
it is possible e.g. for the ia32 IRQ 7 handler to distinguish i8259
spurious interrupts from actual IRQ 7 device interrupts, even when the
i8259 interrupt controller is no longer active.

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