source: mainline/kernel/arch/amd64/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: 6.4 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_amd64_interrupt
30 * @{
31 */
32/** @file
33 */
34
35#include <arch/interrupt.h>
36#include <assert.h>
37#include <log.h>
38#include <panic.h>
39#include <genarch/drivers/i8259/i8259.h>
40#include <genarch/pic/pic_ops.h>
41#include <halt.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/scheduler.h>
48#include <proc/thread.h>
49#include <proc/task.h>
50#include <synch/spinlock.h>
51#include <arch/ddi/ddi.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 */
60pic_ops_t *pic_ops = NULL;
61
62void istate_decode(istate_t *istate)
63{
64 log_printf("cs =%0#18" PRIx64 "\trip=%0#18" PRIx64 "\t"
65 "rfl=%0#18" PRIx64 "\terr=%0#18" PRIx64 "\n",
66 istate->cs, istate->rip, istate->rflags, istate->error_word);
67
68 if (istate_from_uspace(istate))
69 log_printf("ss =%0#18" PRIx64 "\n", istate->ss);
70
71 log_printf("rax=%0#18" PRIx64 "\trbx=%0#18" PRIx64 "\t"
72 "rcx=%0#18" PRIx64 "\trdx=%0#18" PRIx64 "\n",
73 istate->rax, istate->rbx, istate->rcx, istate->rdx);
74
75 log_printf("rsi=%0#18" PRIx64 "\trdi=%0#18" PRIx64 "\t"
76 "rbp=%0#18" PRIx64 "\trsp=%0#18" PRIx64 "\n",
77 istate->rsi, istate->rdi, istate->rbp,
78 istate_from_uspace(istate) ? istate->rsp :
79 (uintptr_t) &istate->rsp);
80
81 log_printf("r8 =%0#18" PRIx64 "\tr9 =%0#18" PRIx64 "\t"
82 "r10=%0#18" PRIx64 "\tr11=%0#18" PRIx64 "\n",
83 istate->r8, istate->r9, istate->r10, istate->r11);
84
85 log_printf("r12=%0#18" PRIx64 "\tr13=%0#18" PRIx64 "\t"
86 "r14=%0#18" PRIx64 "\tr15=%0#18" PRIx64 "\n",
87 istate->r12, istate->r13, istate->r14, istate->r15);
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.");
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
102/** General Protection Fault.
103 *
104 */
105static void gp_fault(unsigned int n, istate_t *istate)
106{
107 if (TASK) {
108 irq_spinlock_lock(&TASK->lock, false);
109 size_t ver = TASK->arch.iomapver;
110 irq_spinlock_unlock(&TASK->lock, false);
111
112 if (CPU->arch.iomapver_copy != ver) {
113 /*
114 * This fault can be caused by an early access
115 * to I/O port because of an out-dated
116 * I/O Permission bitmap installed on CPU.
117 * Install the fresh copy and restart
118 * the instruction.
119 */
120 io_perm_bitmap_install();
121 return;
122 }
123 fault_if_from_uspace(istate, "General protection fault.");
124 }
125 panic_badtrap(istate, n, "General protection fault.");
126}
127
128static void ss_fault(unsigned int n, istate_t *istate)
129{
130 fault_if_from_uspace(istate, "Stack fault.");
131 panic_badtrap(istate, n, "Stack fault.");
132}
133
134static void nm_fault(unsigned int n, istate_t *istate)
135{
136#ifdef CONFIG_FPU_LAZY
137 scheduler_fpu_lazy_request();
138#else
139 fault_if_from_uspace(istate, "FPU fault.");
140 panic("FPU fault.");
141#endif
142}
143
144#ifdef CONFIG_SMP
145static void tlb_shootdown_ipi(unsigned int n, istate_t *istate)
146{
147 pic_ops->eoi(0);
148 tlb_shootdown_ipi_recv();
149}
150#endif
151
152/** Handler of IRQ exceptions.
153 *
154 */
155static void irq_interrupt(unsigned int n, istate_t *istate)
156{
157 assert(n >= IVT_IRQBASE);
158
159 unsigned int inum = n - IVT_IRQBASE;
160 bool ack = false;
161 assert(inum < IRQ_COUNT);
162 assert(inum != IRQ_PIC1);
163
164 irq_t *irq = irq_dispatch_and_lock(inum);
165 if (irq) {
166 /*
167 * The IRQ handler was found.
168 */
169
170 if (irq->preack) {
171 /* Send EOI before processing the interrupt */
172 pic_ops->eoi(inum);
173 ack = true;
174 }
175 irq->handler(irq);
176 irq_spinlock_unlock(&irq->lock, false);
177 } else {
178#ifdef CONFIG_DEBUG
179 log(LF_ARCH, LVL_DEBUG, "cpu%u: unhandled IRQ %u", CPU->id,
180 inum);
181#endif
182 }
183
184 if (!ack)
185 pic_ops->eoi(inum);
186}
187
188static void pic_spurious(unsigned int n, istate_t *istate)
189{
190 unsigned int inum = n - IVT_IRQBASE;
191 if (!pic_ops->is_spurious(inum)) {
192 /* This is actually not a spurious IRQ, so proceed as usual. */
193 irq_interrupt(n, istate);
194 return;
195 }
196 pic_ops->handle_spurious(n);
197#ifdef CONFIG_DEBUG
198 log(LF_ARCH, LVL_DEBUG, "cpu%u: PIC spurious interrupt %u", CPU->id,
199 inum);
200#endif
201}
202
203void interrupt_init(void)
204{
205 unsigned int i;
206
207 for (i = 0; i < IVT_ITEMS; i++)
208 exc_register(i, "null", false, (iroutine_t) null_interrupt);
209
210 for (i = 0; i < IRQ_COUNT; i++) {
211 if ((i != IRQ_PIC0_SPUR) && (i != IRQ_PIC1_SPUR) &&
212 (i != IRQ_PIC1))
213 exc_register(IVT_IRQBASE + i, "irq", true,
214 (iroutine_t) irq_interrupt);
215 }
216
217 exc_register(VECTOR_DE, "de_fault", true, (iroutine_t) de_fault);
218 exc_register(VECTOR_NM, "nm_fault", true, (iroutine_t) nm_fault);
219 exc_register(VECTOR_SS, "ss_fault", true, (iroutine_t) ss_fault);
220 exc_register(VECTOR_GP, "gp_fault", true, (iroutine_t) gp_fault);
221 exc_register(VECTOR_PIC0_SPUR, "pic0_spurious", true,
222 (iroutine_t) pic_spurious);
223 exc_register(VECTOR_PIC1_SPUR, "pic1_spurious", true,
224 (iroutine_t) pic_spurious);
225
226#ifdef CONFIG_SMP
227 exc_register(VECTOR_TLB_SHOOTDOWN_IPI, "tlb_shootdown", true,
228 (iroutine_t) tlb_shootdown_ipi);
229#endif
230}
231
232/** @}
233 */
Note: See TracBrowser for help on using the repository browser.