| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: trap.c,v 1.42 2011/11/27 21:38:17 reinoud Exp $ */ | | 1 | /* $NetBSD: trap.c,v 1.43 2011/12/13 17:54:01 reinoud Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2011 Reinoud Zandijk <reinoud@netbsd.org> | | 4 | * Copyright (c) 2011 Reinoud Zandijk <reinoud@netbsd.org> |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -17,27 +17,27 @@ | | | @@ -17,27 +17,27 @@ |
17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. | | 26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | #include <sys/cdefs.h> | | 29 | #include <sys/cdefs.h> |
30 | __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.42 2011/11/27 21:38:17 reinoud Exp $"); | | 30 | __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.43 2011/12/13 17:54:01 reinoud Exp $"); |
31 | | | 31 | |
32 | #include <sys/types.h> | | 32 | #include <sys/types.h> |
33 | #include <sys/param.h> | | 33 | #include <sys/param.h> |
34 | #include <sys/systm.h> | | 34 | #include <sys/systm.h> |
35 | #include <sys/proc.h> | | 35 | #include <sys/proc.h> |
36 | #include <sys/systm.h> | | 36 | #include <sys/systm.h> |
37 | #include <sys/userret.h> | | 37 | #include <sys/userret.h> |
38 | #include <sys/errno.h> | | 38 | #include <sys/errno.h> |
39 | | | 39 | |
40 | #include <uvm/uvm_extern.h> | | 40 | #include <uvm/uvm_extern.h> |
41 | #include <machine/cpu.h> | | 41 | #include <machine/cpu.h> |
42 | #include <machine/pcb.h> | | 42 | #include <machine/pcb.h> |
43 | #include <machine/pmap.h> | | 43 | #include <machine/pmap.h> |
| @@ -55,27 +55,26 @@ void setup_signal_handlers(void); | | | @@ -55,27 +55,26 @@ void setup_signal_handlers(void); |
55 | static void mem_access_handler(int sig, siginfo_t *info, void *ctx); | | 55 | static void mem_access_handler(int sig, siginfo_t *info, void *ctx); |
56 | static void illegal_instruction_handler(int sig, siginfo_t *info, void *ctx); | | 56 | static void illegal_instruction_handler(int sig, siginfo_t *info, void *ctx); |
57 | extern int errno; | | 57 | extern int errno; |
58 | | | 58 | |
59 | bool pmap_fault(pmap_t pmap, vaddr_t va, vm_prot_t *atype); | | 59 | bool pmap_fault(pmap_t pmap, vaddr_t va, vm_prot_t *atype); |
60 | | | 60 | |
61 | static stack_t sigstk; | | 61 | static stack_t sigstk; |
62 | | | 62 | |
63 | void | | 63 | void |
64 | startlwp(void *arg) | | 64 | startlwp(void *arg) |
65 | { | | 65 | { |
66 | } | | 66 | } |
67 | | | 67 | |
68 | //static int debug_fh; | | | |
69 | void | | 68 | void |
70 | setup_signal_handlers(void) | | 69 | setup_signal_handlers(void) |
71 | { | | 70 | { |
72 | static struct sigaction sa; | | 71 | static struct sigaction sa; |
73 | | | 72 | |
74 | if ((sigstk.ss_sp = thunk_malloc(SIGSTKSZ)) == NULL) | | 73 | if ((sigstk.ss_sp = thunk_malloc(SIGSTKSZ)) == NULL) |
75 | panic("can't allocate signal stack space\n"); | | 74 | panic("can't allocate signal stack space\n"); |
76 | sigstk.ss_size = SIGSTKSZ; | | 75 | sigstk.ss_size = SIGSTKSZ; |
77 | sigstk.ss_flags = 0; | | 76 | sigstk.ss_flags = 0; |
78 | if (thunk_sigaltstack(&sigstk, 0) < 0) | | 77 | if (thunk_sigaltstack(&sigstk, 0) < 0) |
79 | panic("can't set alternate stacksize : %d", | | 78 | panic("can't set alternate stacksize : %d", |
80 | thunk_geterrno()); | | 79 | thunk_geterrno()); |
81 | | | 80 | |
| @@ -85,28 +84,26 @@ setup_signal_handlers(void) | | | @@ -85,28 +84,26 @@ setup_signal_handlers(void) |
85 | // thunk_sigaddset(&sa.sa_mask, SIGALRM); | | 84 | // thunk_sigaddset(&sa.sa_mask, SIGALRM); |
86 | if (thunk_sigaction(SIGSEGV, &sa, NULL) == -1) | | 85 | if (thunk_sigaction(SIGSEGV, &sa, NULL) == -1) |
87 | panic("couldn't register SIGSEGV handler : %d", | | 86 | panic("couldn't register SIGSEGV handler : %d", |
88 | thunk_geterrno()); | | 87 | thunk_geterrno()); |
89 | if (thunk_sigaction(SIGBUS, &sa, NULL) == -1) | | 88 | if (thunk_sigaction(SIGBUS, &sa, NULL) == -1) |
90 | panic("couldn't register SIGBUS handler : %d", thunk_geterrno()); | | 89 | panic("couldn't register SIGBUS handler : %d", thunk_geterrno()); |
91 | | | 90 | |
92 | sa.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; | | 91 | sa.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
93 | sa.sa_sigaction = illegal_instruction_handler; | | 92 | sa.sa_sigaction = illegal_instruction_handler; |
94 | thunk_sigemptyset(&sa.sa_mask); | | 93 | thunk_sigemptyset(&sa.sa_mask); |
95 | // thunk_sigaddset(&sa.sa_mask, SIGALRM); | | 94 | // thunk_sigaddset(&sa.sa_mask, SIGALRM); |
96 | if (thunk_sigaction(SIGILL, &sa, NULL) == -1) | | 95 | if (thunk_sigaction(SIGILL, &sa, NULL) == -1) |
97 | panic("couldn't register SIGILL handler : %d", thunk_geterrno()); | | 96 | panic("couldn't register SIGILL handler : %d", thunk_geterrno()); |
98 | | | | |
99 | // debug_fh = thunk_open("/usr/sources/debug", O_RDWR | O_TRUNC | O_CREAT, 0666); | | | |
100 | } | | 97 | } |
101 | | | 98 | |
102 | | | 99 | |
103 | static void | | 100 | static void |
104 | mem_access_handler(int sig, siginfo_t *info, void *ctx) | | 101 | mem_access_handler(int sig, siginfo_t *info, void *ctx) |
105 | { | | 102 | { |
106 | ucontext_t *uct = ctx; | | 103 | ucontext_t *uct = ctx; |
107 | struct lwp *l; | | 104 | struct lwp *l; |
108 | struct pcb *pcb; | | 105 | struct pcb *pcb; |
109 | vaddr_t va; | | 106 | vaddr_t va; |
110 | | | 107 | |
111 | assert((info->si_signo == SIGSEGV) || (info->si_signo == SIGBUS)); | | 108 | assert((info->si_signo == SIGSEGV) || (info->si_signo == SIGBUS)); |
112 | | | 109 | |
| @@ -248,70 +245,60 @@ illegal_instruction_handler(int sig, sig | | | @@ -248,70 +245,60 @@ illegal_instruction_handler(int sig, sig |
248 | */ | | 245 | */ |
249 | void | | 246 | void |
250 | pagefault(void) | | 247 | pagefault(void) |
251 | { | | 248 | { |
252 | struct proc *p; | | 249 | struct proc *p; |
253 | struct lwp *l; | | 250 | struct lwp *l; |
254 | struct pcb *pcb; | | 251 | struct pcb *pcb; |
255 | struct vmspace *vm; | | 252 | struct vmspace *vm; |
256 | struct vm_map *vm_map; | | 253 | struct vm_map *vm_map; |
257 | vm_prot_t atype; | | 254 | vm_prot_t atype; |
258 | vaddr_t va; | | 255 | vaddr_t va; |
259 | void *onfault; | | 256 | void *onfault; |
260 | int kmem, lwp_errno, rv; | | 257 | int kmem, lwp_errno, rv; |
261 | static vaddr_t old_va, old_old_va = 0; | | | |
262 | | | 258 | |
263 | l = curlwp; | | 259 | l = curlwp; |
264 | pcb = lwp_getpcb(l); | | 260 | pcb = lwp_getpcb(l); |
265 | p = l->l_proc; | | 261 | p = l->l_proc; |
266 | vm = p->p_vmspace; | | 262 | vm = p->p_vmspace; |
267 | va = pcb->pcb_fault_addr; | | 263 | va = pcb->pcb_fault_addr; |
268 | | | 264 | |
269 | lwp_errno = thunk_geterrno(); | | 265 | lwp_errno = thunk_geterrno(); |
270 | | | 266 | |
271 | kmem = 1; | | 267 | kmem = 1; |
272 | vm_map = kernel_map; | | 268 | vm_map = kernel_map; |
273 | if ((va >= VM_MIN_ADDRESS) && (va < VM_MAXUSER_ADDRESS)) { | | 269 | if ((va >= VM_MIN_ADDRESS) && (va < VM_MAXUSER_ADDRESS)) { |
274 | kmem = 0; | | 270 | kmem = 0; |
275 | vm_map = &vm->vm_map; | | 271 | vm_map = &vm->vm_map; |
276 | } | | 272 | } |
277 | | | 273 | |
278 | dprintf_debug("pagefault : va = %p\n", (void *) va); | | 274 | dprintf_debug("pagefault : va = %p\n", (void *) va); |
| | | 275 | |
279 | /* can pmap handle it? on its own? (r/m) */ | | 276 | /* can pmap handle it? on its own? (r/m) */ |
280 | onfault = pcb->pcb_onfault; | | 277 | onfault = pcb->pcb_onfault; |
281 | rv = 0; | | 278 | rv = 0; |
282 | if (!pmap_fault(vm_map->pmap, va, &atype)) { | | 279 | if (!pmap_fault(vm_map->pmap, va, &atype)) { |
283 | dprintf_debug("pmap fault couldn't handle it! : " | | 280 | dprintf_debug("pmap fault couldn't handle it! : " |
284 | "derived atype %d\n", atype); | | 281 | "derived atype %d\n", atype); |
285 | | | 282 | |
286 | /* extra debug for now */ | | 283 | /* extra debug for now */ |
287 | if (pcb == 0) | | 284 | if (pcb == 0) |
288 | panic("NULL pcb!\n"); | | 285 | panic("NULL pcb!\n"); |
289 | | | 286 | |
290 | pcb->pcb_onfault = NULL; | | 287 | pcb->pcb_onfault = NULL; |
291 | rv = uvm_fault(vm_map, va, atype); | | 288 | rv = uvm_fault(vm_map, va, atype); |
292 | pcb->pcb_onfault = onfault; | | 289 | pcb->pcb_onfault = onfault; |
293 | } | | 290 | } |
294 | | | 291 | |
295 | #if 0 | | | |
296 | if (old_old_va) | | | |
297 | thunk_pwrite(debug_fh, (void *) old_old_va, PAGE_SIZE, old_old_va); | | | |
298 | if (old_va) | | | |
299 | thunk_pwrite(debug_fh, (void *) old_va, PAGE_SIZE, old_va); | | | |
300 | thunk_pwrite(debug_fh, (void *) va, PAGE_SIZE, va); | | | |
301 | #endif | | | |
302 | old_old_va = old_va; | | | |
303 | old_va = va; | | | |
304 | | | | |
305 | if (rv) { | | 292 | if (rv) { |
306 | dprintf_debug("uvm_fault returned error %d\n", rv); | | 293 | dprintf_debug("uvm_fault returned error %d\n", rv); |
307 | | | 294 | |
308 | /* something got wrong */ | | 295 | /* something got wrong */ |
309 | if (kmem) { | | 296 | if (kmem) { |
310 | /* copyin / copyout */ | | 297 | /* copyin / copyout */ |
311 | if (!onfault) | | 298 | if (!onfault) |
312 | panic("kernel fault"); | | 299 | panic("kernel fault"); |
313 | panic("%s: can't call onfault yet\n", __func__); | | 300 | panic("%s: can't call onfault yet\n", __func__); |
314 | /* jump to given onfault */ | | 301 | /* jump to given onfault */ |
315 | // tf = &kernel_tf; | | 302 | // tf = &kernel_tf; |
316 | // memset(tf, 0, sizeof(struct trapframe)); | | 303 | // memset(tf, 0, sizeof(struct trapframe)); |
317 | // tf->tf_pc = onfault; | | 304 | // tf->tf_pc = onfault; |