| 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 | #include <arch/interrupt.h> | 
|---|
| 30 | #include <syscall/syscall.h> | 
|---|
| 31 | #include <print.h> | 
|---|
| 32 | #include <debug.h> | 
|---|
| 33 | #include <panic.h> | 
|---|
| 34 | #include <arch/i8259.h> | 
|---|
| 35 | #include <func.h> | 
|---|
| 36 | #include <cpu.h> | 
|---|
| 37 | #include <arch/asm.h> | 
|---|
| 38 | #include <mm/tlb.h> | 
|---|
| 39 | #include <mm/as.h> | 
|---|
| 40 | #include <arch.h> | 
|---|
| 41 | #include <symtab.h> | 
|---|
| 42 | #include <proc/thread.h> | 
|---|
| 43 |  | 
|---|
| 44 | /* | 
|---|
| 45 | * Interrupt and exception dispatching. | 
|---|
| 46 | */ | 
|---|
| 47 |  | 
|---|
| 48 | void (* disable_irqs_function)(__u16 irqmask) = NULL; | 
|---|
| 49 | void (* enable_irqs_function)(__u16 irqmask) = NULL; | 
|---|
| 50 | void (* eoi_function)(void) = NULL; | 
|---|
| 51 |  | 
|---|
| 52 | #define PRINT_INFO_ERRCODE(st) { \ | 
|---|
| 53 | __native *x = (__native *) st; \ | 
|---|
| 54 | char *symbol = get_symtab_entry(x[1]); \ | 
|---|
| 55 | if (!symbol) \ | 
|---|
| 56 | symbol = ""; \ | 
|---|
| 57 | printf("----------------EXCEPTION OCCURED----------------\n"); \ | 
|---|
| 58 | printf("%%eip: %X (%s)\n",x[1],symbol); \ | 
|---|
| 59 | printf("ERROR_WORD=%X\n", x[0]); \ | 
|---|
| 60 | printf("%%cs=%X,flags=%X\n", x[2], x[3]); \ | 
|---|
| 61 | printf("%%eax=%X, %%ebx=%X, %%ecx=%X, %%edx=%X\n",\ | 
|---|
| 62 | x[-2],x[-5],x[-3],x[-4]); \ | 
|---|
| 63 | printf("%%esi=%X, %%edi=%X, %%ebp=%X, %%esp=%X\n",\ | 
|---|
| 64 | x[-8],x[-9],x[-1],x); \ | 
|---|
| 65 | printf("stack: %X, %X, %X, %X\n", x[4], x[5], x[6], x[7]); \ | 
|---|
| 66 | printf("       %X, %X, %X, %X\n", x[8], x[9], x[10], x[11]); \ | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | void null_interrupt(int n, void *st) | 
|---|
| 70 | { | 
|---|
| 71 | __native *stack = (__native *) st; | 
|---|
| 72 |  | 
|---|
| 73 | printf("int %d: null_interrupt\n", n); | 
|---|
| 74 | printf("stack: %L, %L, %L, %L\n", stack[0], stack[1], stack[2], stack[3]); | 
|---|
| 75 | panic("unserviced interrupt\n"); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void gp_fault(int n, void *stack) | 
|---|
| 79 | { | 
|---|
| 80 | PRINT_INFO_ERRCODE(stack); | 
|---|
| 81 | panic("general protection fault\n"); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | void ss_fault(int n, void *stack) | 
|---|
| 85 | { | 
|---|
| 86 | PRINT_INFO_ERRCODE(stack); | 
|---|
| 87 | panic("stack fault\n"); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | void nm_fault(int n, void *stack) | 
|---|
| 92 | { | 
|---|
| 93 | #ifdef CONFIG_FPU_LAZY | 
|---|
| 94 | scheduler_fpu_lazy_request(); | 
|---|
| 95 | #else | 
|---|
| 96 | panic("fpu fault"); | 
|---|
| 97 | #endif | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | void page_fault(int n, void *stack) | 
|---|
| 103 | { | 
|---|
| 104 | __address page; | 
|---|
| 105 |  | 
|---|
| 106 | page = read_cr2(); | 
|---|
| 107 | if (!as_page_fault(page)) { | 
|---|
| 108 | PRINT_INFO_ERRCODE(stack); | 
|---|
| 109 | printf("page fault address: %X\n", page); | 
|---|
| 110 | panic("page fault\n"); | 
|---|
| 111 | } | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | void syscall(int n, void *st) | 
|---|
| 115 | { | 
|---|
| 116 | __native *stack = (__native *) st; | 
|---|
| 117 |  | 
|---|
| 118 | if (stack[-2] < SYSCALL_END) | 
|---|
| 119 | stack[-2] = syscall_table[stack[-2]](stack[-5], stack[-3], stack[-4]); | 
|---|
| 120 | else | 
|---|
| 121 | panic("Undefined syscall %d", stack[-2]); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | void tlb_shootdown_ipi(int n, void *stack) | 
|---|
| 125 | { | 
|---|
| 126 | trap_virtual_eoi(); | 
|---|
| 127 | tlb_shootdown_ipi_recv(); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | void wakeup_ipi(int n, void *stack) | 
|---|
| 131 | { | 
|---|
| 132 | trap_virtual_eoi(); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | void trap_virtual_enable_irqs(__u16 irqmask) | 
|---|
| 136 | { | 
|---|
| 137 | if (enable_irqs_function) | 
|---|
| 138 | enable_irqs_function(irqmask); | 
|---|
| 139 | else | 
|---|
| 140 | panic("no enable_irqs_function\n"); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | void trap_virtual_disable_irqs(__u16 irqmask) | 
|---|
| 144 | { | 
|---|
| 145 | if (disable_irqs_function) | 
|---|
| 146 | disable_irqs_function(irqmask); | 
|---|
| 147 | else | 
|---|
| 148 | panic("no disable_irqs_function\n"); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void trap_virtual_eoi(void) | 
|---|
| 152 | { | 
|---|
| 153 | if (eoi_function) | 
|---|
| 154 | eoi_function(); | 
|---|
| 155 | else | 
|---|
| 156 | panic("no eoi_function\n"); | 
|---|
| 157 |  | 
|---|
| 158 | } | 
|---|