| 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 ia32interrupt
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <arch/interrupt.h>
|
|---|
| 36 | #include <syscall/syscall.h>
|
|---|
| 37 | #include <print.h>
|
|---|
| 38 | #include <debug.h>
|
|---|
| 39 | #include <panic.h>
|
|---|
| 40 | #include <arch/drivers/i8259.h>
|
|---|
| 41 | #include <func.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/thread.h>
|
|---|
| 48 | #include <proc/task.h>
|
|---|
| 49 | #include <synch/spinlock.h>
|
|---|
| 50 | #include <arch/ddi/ddi.h>
|
|---|
| 51 | #include <ipc/sysipc.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 | */
|
|---|
| 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 |
|
|---|
| 65 | void istate_decode(istate_t *istate)
|
|---|
| 66 | {
|
|---|
| 67 | printf("cs =%p\teip=%p\tefl=%p\terr=%p\n",
|
|---|
| 68 | istate->cs, istate->eip, istate->eflags, istate->error_word);
|
|---|
| 69 |
|
|---|
| 70 | printf("ds =%p\tes =%p\tfs =%p\tgs =%p\n",
|
|---|
| 71 | istate->ds, istate->es, istate->fs, istate->gs);
|
|---|
| 72 | if (istate_from_uspace(istate))
|
|---|
| 73 | printf("ss =%p\n", istate->ss);
|
|---|
| 74 |
|
|---|
| 75 | printf("eax=%p\tebx=%p\tecx=%p\tedx=%p\n",
|
|---|
| 76 | istate->eax, istate->ebx, istate->ecx, istate->edx);
|
|---|
| 77 | printf("esi=%p\tedi=%p\tebp=%p\tesp=%p\n",
|
|---|
| 78 | istate->esi, istate->edi, istate->ebp,
|
|---|
| 79 | istate_from_uspace(istate) ? istate->esp : (uintptr_t)&istate->esp);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | static void trap_virtual_eoi(void)
|
|---|
| 83 | {
|
|---|
| 84 | if (eoi_function)
|
|---|
| 85 | eoi_function();
|
|---|
| 86 | else
|
|---|
| 87 | panic("No eoi_function.");
|
|---|
| 88 |
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static void null_interrupt(unsigned int n, istate_t *istate)
|
|---|
| 92 | {
|
|---|
| 93 | fault_if_from_uspace(istate, "Unserviced interrupt: %u.", n);
|
|---|
| 94 | panic_badtrap(istate, n, "Unserviced interrupt: %u.", n);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static void de_fault(unsigned int n, istate_t *istate)
|
|---|
| 98 | {
|
|---|
| 99 | fault_if_from_uspace(istate, "Divide error.");
|
|---|
| 100 | panic_badtrap(istate, n, "Divide error.");
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /** General Protection Fault. */
|
|---|
| 104 | static void gp_fault(unsigned int n __attribute__((unused)), istate_t *istate)
|
|---|
| 105 | {
|
|---|
| 106 | if (TASK) {
|
|---|
| 107 | irq_spinlock_lock(&TASK->lock, false);
|
|---|
| 108 | size_t ver = TASK->arch.iomapver;
|
|---|
| 109 | irq_spinlock_unlock(&TASK->lock, false);
|
|---|
| 110 |
|
|---|
| 111 | if (CPU->arch.iomapver_copy != ver) {
|
|---|
| 112 | /*
|
|---|
| 113 | * This fault can be caused by an early access
|
|---|
| 114 | * to I/O port because of an out-dated
|
|---|
| 115 | * I/O Permission bitmap installed on CPU.
|
|---|
| 116 | * Install the fresh copy and restart
|
|---|
| 117 | * the instruction.
|
|---|
| 118 | */
|
|---|
| 119 | io_perm_bitmap_install();
|
|---|
| 120 | return;
|
|---|
| 121 | }
|
|---|
| 122 | fault_if_from_uspace(istate, "General protection fault.");
|
|---|
| 123 | }
|
|---|
| 124 | panic_badtrap(istate, n, "General protection fault.");
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | static void ss_fault(unsigned int n __attribute__((unused)), istate_t *istate)
|
|---|
| 128 | {
|
|---|
| 129 | fault_if_from_uspace(istate, "Stack fault.");
|
|---|
| 130 | panic_badtrap(istate, n, "Stack fault.");
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | static void simd_fp_exception(unsigned int n __attribute__((unused)), istate_t *istate)
|
|---|
| 134 | {
|
|---|
| 135 | uint32_t mxcsr;
|
|---|
| 136 | asm volatile (
|
|---|
| 137 | "stmxcsr %[mxcsr]\n"
|
|---|
| 138 | : [mxcsr] "=m" (mxcsr)
|
|---|
| 139 | );
|
|---|
| 140 |
|
|---|
| 141 | fault_if_from_uspace(istate, "SIMD FP exception(19), MXCSR=%#0.8x.",
|
|---|
| 142 | (unative_t) mxcsr);
|
|---|
| 143 | panic_badtrap(istate, n, "SIMD FP exception, MXCSR=%#0.8x");
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | static void nm_fault(unsigned int n __attribute__((unused)),
|
|---|
| 147 | istate_t *istate __attribute__((unused)))
|
|---|
| 148 | {
|
|---|
| 149 | #ifdef CONFIG_FPU_LAZY
|
|---|
| 150 | scheduler_fpu_lazy_request();
|
|---|
| 151 | #else
|
|---|
| 152 | fault_if_from_uspace(istate, "FPU fault.");
|
|---|
| 153 | panic_badtrap(istate, n, "FPU fault.");
|
|---|
| 154 | #endif
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | #ifdef CONFIG_SMP
|
|---|
| 158 | static void tlb_shootdown_ipi(unsigned int n __attribute__((unused)),
|
|---|
| 159 | istate_t *istate __attribute__((unused)))
|
|---|
| 160 | {
|
|---|
| 161 | trap_virtual_eoi();
|
|---|
| 162 | tlb_shootdown_ipi_recv();
|
|---|
| 163 | }
|
|---|
| 164 | #endif
|
|---|
| 165 |
|
|---|
| 166 | /** Handler of IRQ exceptions */
|
|---|
| 167 | static void irq_interrupt(unsigned int n, istate_t *istate __attribute__((unused)))
|
|---|
| 168 | {
|
|---|
| 169 | ASSERT(n >= IVT_IRQBASE);
|
|---|
| 170 |
|
|---|
| 171 | unsigned int inum = n - IVT_IRQBASE;
|
|---|
| 172 | bool ack = false;
|
|---|
| 173 | ASSERT(inum < IRQ_COUNT);
|
|---|
| 174 | ASSERT((inum != IRQ_PIC_SPUR) && (inum != IRQ_PIC1));
|
|---|
| 175 |
|
|---|
| 176 | irq_t *irq = irq_dispatch_and_lock(inum);
|
|---|
| 177 | if (irq) {
|
|---|
| 178 | /*
|
|---|
| 179 | * The IRQ handler was found.
|
|---|
| 180 | */
|
|---|
| 181 |
|
|---|
| 182 | if (irq->preack) {
|
|---|
| 183 | /* Send EOI before processing the interrupt */
|
|---|
| 184 | trap_virtual_eoi();
|
|---|
| 185 | ack = true;
|
|---|
| 186 | }
|
|---|
| 187 | irq->handler(irq);
|
|---|
| 188 | irq_spinlock_unlock(&irq->lock, false);
|
|---|
| 189 | } else {
|
|---|
| 190 | /*
|
|---|
| 191 | * Spurious interrupt.
|
|---|
| 192 | */
|
|---|
| 193 | #ifdef CONFIG_DEBUG
|
|---|
| 194 | printf("cpu%u: spurious interrupt (inum=%u)\n", CPU->id, inum);
|
|---|
| 195 | #endif
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | if (!ack)
|
|---|
| 199 | trap_virtual_eoi();
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | void interrupt_init(void)
|
|---|
| 203 | {
|
|---|
| 204 | unsigned int i;
|
|---|
| 205 |
|
|---|
| 206 | for (i = 0; i < IVT_ITEMS; i++)
|
|---|
| 207 | exc_register(i, "null", false, (iroutine_t) null_interrupt);
|
|---|
| 208 |
|
|---|
| 209 | for (i = 0; i < IRQ_COUNT; i++) {
|
|---|
| 210 | if ((i != IRQ_PIC_SPUR) && (i != IRQ_PIC1))
|
|---|
| 211 | exc_register(IVT_IRQBASE + i, "irq", true,
|
|---|
| 212 | (iroutine_t) irq_interrupt);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | exc_register(0, "de_fault", true, (iroutine_t) de_fault);
|
|---|
| 216 | exc_register(7, "nm_fault", true, (iroutine_t) nm_fault);
|
|---|
| 217 | exc_register(12, "ss_fault", true, (iroutine_t) ss_fault);
|
|---|
| 218 | exc_register(13, "gp_fault", true, (iroutine_t) gp_fault);
|
|---|
| 219 | exc_register(19, "simd_fp", true, (iroutine_t) simd_fp_exception);
|
|---|
| 220 |
|
|---|
| 221 | #ifdef CONFIG_SMP
|
|---|
| 222 | exc_register(VECTOR_TLB_SHOOTDOWN_IPI, "tlb_shootdown", true,
|
|---|
| 223 | (iroutine_t) tlb_shootdown_ipi);
|
|---|
| 224 | #endif
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | void trap_virtual_enable_irqs(uint16_t irqmask)
|
|---|
| 228 | {
|
|---|
| 229 | if (enable_irqs_function)
|
|---|
| 230 | enable_irqs_function(irqmask);
|
|---|
| 231 | else
|
|---|
| 232 | panic("No enable_irqs_function.");
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | void trap_virtual_disable_irqs(uint16_t irqmask)
|
|---|
| 236 | {
|
|---|
| 237 | if (disable_irqs_function)
|
|---|
| 238 | disable_irqs_function(irqmask);
|
|---|
| 239 | else
|
|---|
| 240 | panic("No disable_irqs_function.");
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /** @}
|
|---|
| 244 | */
|
|---|