[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 |
|
---|
[06e1e95] | 29 | /** @addtogroup amd64interrupt
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[db3341e] | 35 | #include <arch/interrupt.h>
|
---|
| 36 | #include <print.h>
|
---|
| 37 | #include <debug.h>
|
---|
| 38 | #include <panic.h>
|
---|
[80d31883] | 39 | #include <arch/drivers/i8259.h>
|
---|
[db3341e] | 40 | #include <func.h>
|
---|
| 41 | #include <cpu.h>
|
---|
| 42 | #include <arch/asm.h>
|
---|
| 43 | #include <mm/tlb.h>
|
---|
[20d50a1] | 44 | #include <mm/as.h>
|
---|
[db3341e] | 45 | #include <arch.h>
|
---|
[3396f59] | 46 | #include <arch/asm.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 | */
|
---|
| 60 |
|
---|
| 61 | void (* disable_irqs_function)(uint16_t irqmask) = NULL;
|
---|
| 62 | void (* enable_irqs_function)(uint16_t irqmask) = NULL;
|
---|
| 63 | void (* eoi_function)(void) = NULL;
|
---|
| 64 |
|
---|
[22a28a69] | 65 | void istate_decode(istate_t *istate)
|
---|
[8ec9bae] | 66 | {
|
---|
[a043e39] | 67 | printf("error_word=%#llx\n", istate->error_word);
|
---|
| 68 | printf("cs =%#0.16llx\trflags=%#0.16llx\n", istate->cs,
|
---|
| 69 | istate->rflags);
|
---|
| 70 | printf("rax=%#0.16llx\trbx=%#0.16llx\trcx=%#0.16llx\n", istate->rax,
|
---|
[2829b354] | 71 | istate->rcx, istate->rdx);
|
---|
[a043e39] | 72 | printf("rsi=%#0.16llx\trdi=%#0.16llx\tr8 =%#0.16llx\n", istate->rsi,
|
---|
[2829b354] | 73 | istate->rdi, istate->r8);
|
---|
[a043e39] | 74 | printf("r9 =%#0.16llx\tr10=%#0.16llx\tr11=%#0.16llx\n", istate->r9,
|
---|
[2829b354] | 75 | istate->r10, istate->r11);
|
---|
[8ec9bae] | 76 | }
|
---|
[db3341e] | 77 |
|
---|
[8607db8] | 78 | static void trap_virtual_eoi(void)
|
---|
| 79 | {
|
---|
| 80 | if (eoi_function)
|
---|
| 81 | eoi_function();
|
---|
| 82 | else
|
---|
[f651e80] | 83 | panic("No eoi_function.");
|
---|
[db3341e] | 84 |
|
---|
[8607db8] | 85 | }
|
---|
[db3341e] | 86 |
|
---|
[214ec25c] | 87 | static void null_interrupt(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 88 | {
|
---|
[214ec25c] | 89 | fault_if_from_uspace(istate, "Unserviced interrupt: %u.", n);
|
---|
[a043e39] | 90 | panic_badtrap(istate, n, "Unserviced interrupt.");
|
---|
[db3341e] | 91 | }
|
---|
| 92 |
|
---|
[214ec25c] | 93 | static void de_fault(unsigned int n, istate_t *istate)
|
---|
[4491338] | 94 | {
|
---|
| 95 | fault_if_from_uspace(istate, "Divide error.");
|
---|
[a043e39] | 96 | panic_badtrap(istate, n, "Divide error.");
|
---|
[4491338] | 97 | }
|
---|
| 98 |
|
---|
[da1bafb] | 99 | /** General Protection Fault.
|
---|
| 100 | *
|
---|
| 101 | */
|
---|
[214ec25c] | 102 | static void gp_fault(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 103 | {
|
---|
[2382d09] | 104 | if (TASK) {
|
---|
[da1bafb] | 105 | irq_spinlock_lock(&TASK->lock, false);
|
---|
| 106 | size_t ver = TASK->arch.iomapver;
|
---|
| 107 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
| 108 |
|
---|
[2382d09] | 109 | if (CPU->arch.iomapver_copy != ver) {
|
---|
| 110 | /*
|
---|
| 111 | * This fault can be caused by an early access
|
---|
| 112 | * to I/O port because of an out-dated
|
---|
| 113 | * I/O Permission bitmap installed on CPU.
|
---|
| 114 | * Install the fresh copy and restart
|
---|
| 115 | * the instruction.
|
---|
| 116 | */
|
---|
| 117 | io_perm_bitmap_install();
|
---|
| 118 | return;
|
---|
| 119 | }
|
---|
[f651e80] | 120 | fault_if_from_uspace(istate, "General protection fault.");
|
---|
[2382d09] | 121 | }
|
---|
[a043e39] | 122 | panic_badtrap(istate, n, "General protection fault.");
|
---|
[db3341e] | 123 | }
|
---|
| 124 |
|
---|
[214ec25c] | 125 | static void ss_fault(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 126 | {
|
---|
[f651e80] | 127 | fault_if_from_uspace(istate, "Stack fault.");
|
---|
[a043e39] | 128 | panic_badtrap(istate, n, "Stack fault.");
|
---|
[db3341e] | 129 | }
|
---|
| 130 |
|
---|
[214ec25c] | 131 | static void nm_fault(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 132 | {
|
---|
[6da1013f] | 133 | #ifdef CONFIG_FPU_LAZY
|
---|
[b49f4ae] | 134 | scheduler_fpu_lazy_request();
|
---|
| 135 | #else
|
---|
[f651e80] | 136 | fault_if_from_uspace(istate, "FPU fault.");
|
---|
| 137 | panic("FPU fault.");
|
---|
[b49f4ae] | 138 | #endif
|
---|
[db3341e] | 139 | }
|
---|
| 140 |
|
---|
[cbc8ac6] | 141 | #ifdef CONFIG_SMP
|
---|
[214ec25c] | 142 | static void tlb_shootdown_ipi(unsigned int n, istate_t *istate)
|
---|
[db3341e] | 143 | {
|
---|
| 144 | trap_virtual_eoi();
|
---|
| 145 | tlb_shootdown_ipi_recv();
|
---|
| 146 | }
|
---|
[cbc8ac6] | 147 | #endif
|
---|
[db3341e] | 148 |
|
---|
[da1bafb] | 149 | /** Handler of IRQ exceptions.
|
---|
| 150 | *
|
---|
| 151 | */
|
---|
[214ec25c] | 152 | static void irq_interrupt(unsigned int n, istate_t *istate)
|
---|
[8607db8] | 153 | {
|
---|
| 154 | ASSERT(n >= IVT_IRQBASE);
|
---|
| 155 |
|
---|
[214ec25c] | 156 | unsigned int inum = n - IVT_IRQBASE;
|
---|
[7bcfbbc] | 157 | bool ack = false;
|
---|
[8607db8] | 158 | ASSERT(inum < IRQ_COUNT);
|
---|
| 159 | ASSERT((inum != IRQ_PIC_SPUR) && (inum != IRQ_PIC1));
|
---|
[7bcfbbc] | 160 |
|
---|
[8607db8] | 161 | irq_t *irq = irq_dispatch_and_lock(inum);
|
---|
| 162 | if (irq) {
|
---|
| 163 | /*
|
---|
| 164 | * The IRQ handler was found.
|
---|
| 165 | */
|
---|
[da1bafb] | 166 |
|
---|
[7bcfbbc] | 167 | if (irq->preack) {
|
---|
| 168 | /* Send EOI before processing the interrupt */
|
---|
| 169 | trap_virtual_eoi();
|
---|
| 170 | ack = true;
|
---|
| 171 | }
|
---|
[6cd9aa6] | 172 | irq->handler(irq);
|
---|
[da1bafb] | 173 | irq_spinlock_unlock(&irq->lock, false);
|
---|
[8607db8] | 174 | } else {
|
---|
| 175 | /*
|
---|
| 176 | * Spurious interrupt.
|
---|
| 177 | */
|
---|
| 178 | #ifdef CONFIG_DEBUG
|
---|
[214ec25c] | 179 | printf("cpu%u: spurious interrupt (inum=%u)\n", CPU->id, inum);
|
---|
[8607db8] | 180 | #endif
|
---|
| 181 | }
|
---|
[7bcfbbc] | 182 |
|
---|
| 183 | if (!ack)
|
---|
| 184 | trap_virtual_eoi();
|
---|
[8607db8] | 185 | }
|
---|
| 186 |
|
---|
| 187 | void interrupt_init(void)
|
---|
| 188 | {
|
---|
[b3b7e14a] | 189 | unsigned int i;
|
---|
[8607db8] | 190 |
|
---|
| 191 | for (i = 0; i < IVT_ITEMS; i++)
|
---|
[b3b7e14a] | 192 | exc_register(i, "null", false, (iroutine_t) null_interrupt);
|
---|
[8607db8] | 193 |
|
---|
| 194 | for (i = 0; i < IRQ_COUNT; i++) {
|
---|
| 195 | if ((i != IRQ_PIC_SPUR) && (i != IRQ_PIC1))
|
---|
[b3b7e14a] | 196 | exc_register(IVT_IRQBASE + i, "irq", true,
|
---|
| 197 | (iroutine_t) irq_interrupt);
|
---|
[8607db8] | 198 | }
|
---|
| 199 |
|
---|
[b3b7e14a] | 200 | exc_register(0, "de_fault", true, (iroutine_t) de_fault);
|
---|
| 201 | exc_register(7, "nm_fault", true, (iroutine_t) nm_fault);
|
---|
| 202 | exc_register(12, "ss_fault", true, (iroutine_t) ss_fault);
|
---|
| 203 | exc_register(13, "gp_fault", true, (iroutine_t) gp_fault);
|
---|
[8607db8] | 204 |
|
---|
| 205 | #ifdef CONFIG_SMP
|
---|
[b3b7e14a] | 206 | exc_register(VECTOR_TLB_SHOOTDOWN_IPI, "tlb_shootdown", true,
|
---|
| 207 | (iroutine_t) tlb_shootdown_ipi);
|
---|
[8607db8] | 208 | #endif
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[7f1c620] | 211 | void trap_virtual_enable_irqs(uint16_t irqmask)
|
---|
[db3341e] | 212 | {
|
---|
| 213 | if (enable_irqs_function)
|
---|
| 214 | enable_irqs_function(irqmask);
|
---|
| 215 | else
|
---|
[f651e80] | 216 | panic("No enable_irqs_function.");
|
---|
[db3341e] | 217 | }
|
---|
| 218 |
|
---|
[7f1c620] | 219 | void trap_virtual_disable_irqs(uint16_t irqmask)
|
---|
[db3341e] | 220 | {
|
---|
| 221 | if (disable_irqs_function)
|
---|
| 222 | disable_irqs_function(irqmask);
|
---|
| 223 | else
|
---|
[f651e80] | 224 | panic("No disable_irqs_function.");
|
---|
[db3341e] | 225 | }
|
---|
| 226 |
|
---|
[06e1e95] | 227 | /** @}
|
---|
[b45c443] | 228 | */
|
---|