1 |
/* Exception handling. |
/* Exception handling. |
2 |
Copyright 1999, 2000, 2001, 2002 Johan Rydberg, jrydberg@opencores.org. |
Copyright 1999, 2000, 2001, 2002 Johan Rydberg, jrydberg@rtmk.org. |
3 |
|
|
4 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
24 |
#include "processor.h" |
#include "processor.h" |
25 |
#include "thread.h" |
#include "thread.h" |
26 |
#include "task.h" |
#include "task.h" |
27 |
|
#include "exception.h" |
28 |
|
|
29 |
#include "pc-multiboot.h" |
#include "pc-multiboot.h" |
30 |
#include "i386-trap.h" |
#include "i386-trap.h" |
33 |
|
|
34 |
#include "vm-fault.h" |
#include "vm-fault.h" |
35 |
|
|
36 |
|
/* Dump exception frame FP to trace output. */ |
37 |
static void |
static void |
38 |
dump_exception_frame (struct exception_frame *fp) |
dump_exception_frame (struct exception_frame *fp) |
39 |
{ |
{ |
58 |
fp->eip, fp->cs, fp->uesp, fp->efl); |
fp->eip, fp->cs, fp->uesp, fp->efl); |
59 |
} |
} |
60 |
|
|
61 |
|
/* Exception handling function. FRAME is the user-land state |
62 |
|
hardware registers. If we can recover from the exception this |
63 |
|
function returns, otherwize it kills the thread. */ |
64 |
void |
void |
65 |
cpu_exception_kernel (struct exception_frame *frame) |
cpu_exception_kernel (struct exception_frame *frame) |
66 |
{ |
{ |
67 |
|
int exception = 0, code = 0, subcode = 0; |
68 |
kern_return_t kr; |
kern_return_t kr; |
69 |
bool kernel_p; |
bool kernel_p; |
70 |
|
|
71 |
/* This is pretty ugly! We test for a special case: copyout/copyin. */ |
/* This is pretty ugly! We test for a special case: copyout/copyin. */ |
|
|
|
72 |
kernel_p = (frame->cs & SEL_PL_U) |
kernel_p = (frame->cs & SEL_PL_U) |
73 |
? false |
? false |
74 |
: (frame->es & SEL_PL_U) ? false /* copyout */ |
: (frame->es & SEL_PL_U) ? false /* copyout */ |
75 |
: (frame->gs & SEL_PL_U) ? false : true; /* copyin */ |
: (frame->gs & SEL_PL_U) ? false : true; /* copyin */ |
76 |
|
|
77 |
|
/* Per-default we set subcode to machine-dependent trap number. */ |
78 |
|
subcode = frame->trapno; |
79 |
|
|
80 |
|
/* Take different actions depending on type of exception. */ |
81 |
switch (frame->trapno) |
switch (frame->trapno) |
82 |
{ |
{ |
83 |
|
case TRAP_DIVIDE_ERROR: |
84 |
|
exception = EXCEPTION_ARITHMETIC; |
85 |
|
code = EXCEPTION_ARITHMETIC_DIVZERO; |
86 |
|
break; |
87 |
|
|
88 |
|
case TRAP_BREAKPOINT: |
89 |
|
exception = EXCEPTION_BREAKPOINT; |
90 |
|
break; |
91 |
|
|
92 |
|
case TRAP_DEBUG_EXCEPTION: |
93 |
|
exception = EXCEPTION_SOFTWARE; |
94 |
|
break; |
95 |
|
|
96 |
|
case TRAP_OVERFLOW: |
97 |
|
case TRAP_BOUNDS_CHECK: |
98 |
|
case TRAP_INVALID_OPCODE: |
99 |
|
case TRAP_FPU_NOT_AVAILABLE: |
100 |
|
case TRAP_DOUBLE_FAULT: |
101 |
|
case TRAP_FPU_FAULT: |
102 |
|
case TRAP_INVALID_TSS: |
103 |
|
case TRAP_GENERAL_PROTECTION: |
104 |
|
case TRAP_FPU_ERROR: |
105 |
|
case TRAP_ALIGNMENT_CHECK: |
106 |
|
case TRAP_MACHINE_CHECK: |
107 |
|
case TRAP_SEGMENT_NOT_PRESENT: |
108 |
|
case TRAP_STACK_EXCEPTION: |
109 |
|
default: |
110 |
|
exception = EXCEPTION_BAD_INSN; |
111 |
|
break; |
112 |
|
|
113 |
case TRAP_PAGE_FAULT: |
case TRAP_PAGE_FAULT: |
114 |
kr = vm_fault (THREAD_CURRENT()->task->map, vm_trunc_page (frame->cr2), |
kr = vm_fault (THREAD_CURRENT()->task->map, vm_trunc_page (frame->cr2), |
115 |
frame->err & 0x2 ? VM_PROT_WRITE : VM_PROT_READ, |
frame->err & 0x2 ? VM_PROT_WRITE : VM_PROT_READ, |
116 |
kernel_p); |
kernel_p); |
117 |
if (kr == KERN_SUCCESS) |
if (kr == KERN_SUCCESS) |
118 |
return; |
return; |
119 |
|
|
120 |
|
/* Exception subcode is the address of the fault. */ |
121 |
|
exception = EXCEPTION_BAD_ACCESS; |
122 |
|
code = kr; |
123 |
|
subcode = (frame->cr2 - VM_USER_MIN_ADDRESS); |
124 |
break; |
break; |
125 |
} |
} |
126 |
|
|
127 |
|
/* ??? when things start to get stable we remove this. */ |
128 |
dump_exception_frame (frame); |
dump_exception_frame (frame); |
129 |
|
|
130 |
|
/* If the exception came from the kernel there ain't much we can |
131 |
|
do except panic, so that's what we do here! */ |
132 |
if (kernel_p) |
if (kernel_p) |
133 |
panic ("failed to resolve exception from kernel-mode"); |
panic ("failed to resolve exception from kernel-mode"); |
134 |
|
|
135 |
for (; frame->trapno != 3;) |
/* Raise exception. Send exception raise message to user exception |
136 |
; |
port. If no exception port is defined, terminate task. */ |
137 |
|
exception_raise (THREAD_CURRENT (), TASK_CURRENT (), exception, |
138 |
|
code, subcode); |
139 |
} |
} |
140 |
|
|
141 |
void |
void |