[db3341e] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[db3341e] | 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 |
|
---|
[c5429fe] | 29 | /** @addtogroup kernel_amd64_interrupt
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[db3341e] | 35 | #include <arch/interrupt.h>
|
---|
[63e27ef] | 36 | #include <assert.h>
|
---|
[b2fa1204] | 37 | #include <log.h>
|
---|
[db3341e] | 38 | #include <panic.h>
|
---|
[87a5796] | 39 | #include <genarch/drivers/i8259/i8259.h>
|
---|
[2a103b5] | 40 | #include <genarch/pic/pic_ops.h>
|
---|
[b2e121a] | 41 | #include <halt.h>
|
---|
[db3341e] | 42 | #include <cpu.h>
|
---|
| 43 | #include <arch/asm.h>
|
---|
| 44 | #include <mm/tlb.h>
|
---|
[20d50a1] | 45 | #include <mm/as.h>
|
---|
[db3341e] | 46 | #include <arch.h>
|
---|
[b49f4ae] | 47 | #include <proc/scheduler.h>
|
---|
[1084a784] | 48 | #include <proc/thread.h>
|
---|
[2382d09] | 49 | #include <proc/task.h>
|
---|
| 50 | #include <synch/spinlock.h>
|
---|
| 51 | #include <arch/ddi/ddi.h>
|
---|
[5626277] | 52 | #include <interrupt.h>
|
---|
[8607db8] | 53 | #include <ddi/irq.h>
|
---|
[e2b762ec] | 54 | #include <symtab.h>
|
---|
[304342e] | 55 | #include <stacktrace.h>
|
---|
[e2b762ec] | 56 |
|
---|
[8607db8] | 57 | /*
|
---|
| 58 | * Interrupt and exception dispatching.
|
---|
| 59 | */
|
---|
[2a103b5] | 60 | pic_ops_t *pic_ops = NULL;
|
---|
[8607db8] | 61 |
|
---|
[22a28a69] | 62 | void istate_decode(istate_t *istate)
|
---|
[8ec9bae] | 63 | {
|
---|
[b2fa1204] | 64 | log_printf("cs =%0#18" PRIx64 "\trip=%0#18" PRIx64 "\t"
|
---|
[2438fa6] | 65 | "rfl=%0#18" PRIx64 "\terr=%0#18" PRIx64 "\n",
|
---|
| 66 | istate->cs, istate->rip, istate->rflags, istate->error_word);
|
---|
[a35b458] | 67 |
|
---|
[9171f12] | 68 | if (istate_from_uspace(istate))
|
---|
[b2fa1204] | 69 | log_printf("ss =%0#18" PRIx64 "\n", istate->ss);
|
---|
[a35b458] | 70 |
|
---|
[b2fa1204] | 71 | log_printf("rax=%0#18" PRIx64 "\trbx=%0#18" PRIx64 "\t"
|
---|
[2438fa6] | 72 | "rcx=%0#18" PRIx64 "\trdx=%0#18" PRIx64 "\n",
|
---|
[9171f12] | 73 | istate->rax, istate->rbx, istate->rcx, istate->rdx);
|
---|
[a35b458] | 74 |
|
---|
[b2fa1204] | 75 | log_printf("rsi=%0#18" PRIx64 "\trdi=%0#18" PRIx64 "\t"
|
---|
[2438fa6] | 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);
|
---|
[a35b458] | 80 |
|
---|
[b2fa1204] | 81 | log_printf("r8 =%0#18" PRIx64 "\tr9 =%0#18" PRIx64 "\t"
|
---|
[2438fa6] | 82 | "r10=%0#18" PRIx64 "\tr11=%0#18" PRIx64 "\n",
|
---|
[9171f12] | 83 | istate->r8, istate->r9, istate->r10, istate->r11);
|
---|
[a35b458] | 84 |
|
---|
[b2fa1204] | 85 | log_printf("r12=%0#18" PRIx64 "\tr13=%0#18" PRIx64 "\t"
|
---|
[2438fa6] | 86 | "r14=%0#18" PRIx64 "\tr15=%0#18" PRIx64 "\n",
|
---|
[9171f12] | 87 | istate->r12, istate->r13, istate->r14, istate->r15);
|
---|
[8ec9bae] | 88 | }
|
---|
[db3341e] | 89 |
|
---|
[214ec25c] | 90 | static void null_interrupt(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 91 | {
|
---|
[214ec25c] | 92 | fault_if_from_uspace(istate, "Unserviced interrupt: %u.", n);
|
---|
[a043e39] | 93 | panic_badtrap(istate, n, "Unserviced interrupt.");
|
---|
[db3341e] | 94 | }
|
---|
| 95 |
|
---|
[214ec25c] | 96 | static void de_fault(unsigned int n, istate_t *istate)
|
---|
[4491338] | 97 | {
|
---|
| 98 | fault_if_from_uspace(istate, "Divide error.");
|
---|
[a043e39] | 99 | panic_badtrap(istate, n, "Divide error.");
|
---|
[4491338] | 100 | }
|
---|
| 101 |
|
---|
[da1bafb] | 102 | /** General Protection Fault.
|
---|
| 103 | *
|
---|
| 104 | */
|
---|
[214ec25c] | 105 | static void gp_fault(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 106 | {
|
---|
[2382d09] | 107 | if (TASK) {
|
---|
[da1bafb] | 108 | irq_spinlock_lock(&TASK->lock, false);
|
---|
| 109 | size_t ver = TASK->arch.iomapver;
|
---|
| 110 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
[a35b458] | 111 |
|
---|
[2382d09] | 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 | }
|
---|
[f651e80] | 123 | fault_if_from_uspace(istate, "General protection fault.");
|
---|
[2382d09] | 124 | }
|
---|
[a043e39] | 125 | panic_badtrap(istate, n, "General protection fault.");
|
---|
[db3341e] | 126 | }
|
---|
| 127 |
|
---|
[214ec25c] | 128 | static void ss_fault(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 129 | {
|
---|
[f651e80] | 130 | fault_if_from_uspace(istate, "Stack fault.");
|
---|
[a043e39] | 131 | panic_badtrap(istate, n, "Stack fault.");
|
---|
[db3341e] | 132 | }
|
---|
| 133 |
|
---|
[214ec25c] | 134 | static void nm_fault(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 135 | {
|
---|
[6da1013f] | 136 | #ifdef CONFIG_FPU_LAZY
|
---|
[b49f4ae] | 137 | scheduler_fpu_lazy_request();
|
---|
| 138 | #else
|
---|
[f651e80] | 139 | fault_if_from_uspace(istate, "FPU fault.");
|
---|
| 140 | panic("FPU fault.");
|
---|
[b49f4ae] | 141 | #endif
|
---|
[db3341e] | 142 | }
|
---|
| 143 |
|
---|
[cbc8ac6] | 144 | #ifdef CONFIG_SMP
|
---|
[214ec25c] | 145 | static void tlb_shootdown_ipi(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 146 | {
|
---|
[2a103b5] | 147 | pic_ops->eoi(0);
|
---|
[db3341e] | 148 | tlb_shootdown_ipi_recv();
|
---|
| 149 | }
|
---|
[cbc8ac6] | 150 | #endif
|
---|
[db3341e] | 151 |
|
---|
[da1bafb] | 152 | /** Handler of IRQ exceptions.
|
---|
| 153 | *
|
---|
| 154 | */
|
---|
[214ec25c] | 155 | static void irq_interrupt(unsigned int n, istate_t *istate)
|
---|
[8607db8] | 156 | {
|
---|
[63e27ef] | 157 | assert(n >= IVT_IRQBASE);
|
---|
[a35b458] | 158 |
|
---|
[214ec25c] | 159 | unsigned int inum = n - IVT_IRQBASE;
|
---|
[7bcfbbc] | 160 | bool ack = false;
|
---|
[63e27ef] | 161 | assert(inum < IRQ_COUNT);
|
---|
[f6cf76f] | 162 | assert(inum != IRQ_PIC1);
|
---|
[a35b458] | 163 |
|
---|
[8607db8] | 164 | irq_t *irq = irq_dispatch_and_lock(inum);
|
---|
| 165 | if (irq) {
|
---|
| 166 | /*
|
---|
| 167 | * The IRQ handler was found.
|
---|
| 168 | */
|
---|
[a35b458] | 169 |
|
---|
[7bcfbbc] | 170 | if (irq->preack) {
|
---|
| 171 | /* Send EOI before processing the interrupt */
|
---|
[2a103b5] | 172 | pic_ops->eoi(inum);
|
---|
[7bcfbbc] | 173 | ack = true;
|
---|
| 174 | }
|
---|
[6cd9aa6] | 175 | irq->handler(irq);
|
---|
[da1bafb] | 176 | irq_spinlock_unlock(&irq->lock, false);
|
---|
[8607db8] | 177 | } else {
|
---|
| 178 | #ifdef CONFIG_DEBUG
|
---|
[fd67c9f] | 179 | log(LF_ARCH, LVL_DEBUG, "cpu%u: unhandled IRQ %u", CPU->id,
|
---|
| 180 | inum);
|
---|
[8607db8] | 181 | #endif
|
---|
| 182 | }
|
---|
[a35b458] | 183 |
|
---|
[7bcfbbc] | 184 | if (!ack)
|
---|
[2a103b5] | 185 | pic_ops->eoi(inum);
|
---|
[8607db8] | 186 | }
|
---|
| 187 |
|
---|
[f6cf76f] | 188 | static void pic_spurious(unsigned int n, istate_t *istate)
|
---|
| 189 | {
|
---|
[fd67c9f] | 190 | unsigned int inum = n - IVT_IRQBASE;
|
---|
[2a103b5] | 191 | if (!pic_ops->is_spurious(inum)) {
|
---|
[fd67c9f] | 192 | /* This is actually not a spurious IRQ, so proceed as usual. */
|
---|
| 193 | irq_interrupt(n, istate);
|
---|
| 194 | return;
|
---|
| 195 | }
|
---|
[2a103b5] | 196 | pic_ops->handle_spurious(n);
|
---|
[f6cf76f] | 197 | #ifdef CONFIG_DEBUG
|
---|
[fd67c9f] | 198 | log(LF_ARCH, LVL_DEBUG, "cpu%u: PIC spurious interrupt %u", CPU->id,
|
---|
| 199 | inum);
|
---|
[f6cf76f] | 200 | #endif
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[8607db8] | 203 | void interrupt_init(void)
|
---|
| 204 | {
|
---|
[b3b7e14a] | 205 | unsigned int i;
|
---|
[a35b458] | 206 |
|
---|
[8607db8] | 207 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
[b3b7e14a] | 208 | exc_register(i, "null", false, (iroutine_t) null_interrupt);
|
---|
[a35b458] | 209 |
|
---|
[8607db8] | 210 | for (i = 0; i < IRQ_COUNT; i++) {
|
---|
[29beac8] | 211 | if ((i != IRQ_PIC0_SPUR) && (i != IRQ_PIC1_SPUR) &&
|
---|
| 212 | (i != IRQ_PIC1))
|
---|
[b3b7e14a] | 213 | exc_register(IVT_IRQBASE + i, "irq", true,
|
---|
| 214 | (iroutine_t) irq_interrupt);
|
---|
[8607db8] | 215 | }
|
---|
[a35b458] | 216 |
|
---|
[4b0206c] | 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);
|
---|
[f6cf76f] | 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);
|
---|
[a35b458] | 225 |
|
---|
[8607db8] | 226 | #ifdef CONFIG_SMP
|
---|
[b3b7e14a] | 227 | exc_register(VECTOR_TLB_SHOOTDOWN_IPI, "tlb_shootdown", true,
|
---|
| 228 | (iroutine_t) tlb_shootdown_ipi);
|
---|
[8607db8] | 229 | #endif
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[06e1e95] | 232 | /** @}
|
---|
[b45c443] | 233 | */
|
---|