trap.c revision 1.20 1 /* $NetBSD: trap.c,v 1.20 2004/07/18 20:57:34 chs Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthew Fredette.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /* $OpenBSD: trap.c,v 1.30 2001/09/19 20:50:56 mickey Exp $ */
40
41 /*
42 * Copyright (c) 1998-2000 Michael Shalayeff
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by Michael Shalayeff.
56 * 4. The name of the author may not be used to endorse or promote products
57 * derived from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.20 2004/07/18 20:57:34 chs Exp $");
73
74 /* #define INTRDEBUG */
75 /* #define TRAPDEBUG */
76 /* #define USERTRACE */
77
78 #include "opt_kgdb.h"
79 #include "opt_syscall_debug.h"
80 #include "opt_ktrace.h"
81 #include "opt_systrace.h"
82
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/syscall.h>
87 #include <sys/sa.h>
88 #include <sys/savar.h>
89 #ifdef KTRACE
90 #include <sys/ktrace.h>
91 #endif
92 #ifdef SYSTRACE
93 #include <sys/systrace.h>
94 #endif
95 #include <sys/proc.h>
96 #include <sys/signalvar.h>
97 #include <sys/user.h>
98 #include <sys/acct.h>
99 #include <sys/signal.h>
100 #include <sys/device.h>
101 #include <sys/pool.h>
102 #include <sys/userret.h>
103
104 #include <net/netisr.h>
105
106 #ifdef KGDB
107 #include <sys/kgdb.h>
108 #endif
109
110 #include <uvm/uvm.h>
111
112 #include <machine/iomod.h>
113 #include <machine/cpufunc.h>
114 #include <machine/reg.h>
115 #include <machine/autoconf.h>
116
117 #include <machine/db_machdep.h>
118
119 #include <hppa/hppa/machdep.h>
120
121 #include <ddb/db_output.h>
122 #include <ddb/db_interface.h>
123
124 #if defined(DEBUG) || defined(DIAGNOSTIC)
125 /*
126 * 0x6fc1000 is a stwm r1, d(sr0, sp), which is the last
127 * instruction in the function prologue that gcc -O0 uses.
128 * When we have this instruction we know the relationship
129 * between the stack pointer and the gcc -O0 frame pointer
130 * (in r3, loaded with the initial sp) for the body of a
131 * function.
132 *
133 * If the given instruction is a stwm r1, d(sr0, sp) where
134 * d > 0, we evaluate to d, else we evaluate to zero.
135 */
136 #define STWM_R1_D_SR0_SP(inst) \
137 (((inst) & 0xffffc001) == 0x6fc10000 ? (((inst) & 0x00003ff) >> 1) : 0)
138 #endif /* DEBUG || DIAGNOSTIC */
139
140 const char *trap_type[] = {
141 "invalid",
142 "HPMC",
143 "power failure",
144 "recovery counter",
145 "external interrupt",
146 "LPMC",
147 "ITLB miss fault",
148 "instruction protection",
149 "Illegal instruction",
150 "break instruction",
151 "privileged operation",
152 "privileged register",
153 "overflow",
154 "conditional",
155 "assist exception",
156 "DTLB miss",
157 "ITLB non-access miss",
158 "DTLB non-access miss",
159 "data protection/rights/alignment",
160 "data break",
161 "TLB dirty",
162 "page reference",
163 "assist emulation",
164 "higher-priv transfer",
165 "lower-priv transfer",
166 "taken branch",
167 "data access rights",
168 "data protection",
169 "unaligned data ref",
170 };
171 int trap_types = sizeof(trap_type)/sizeof(trap_type[0]);
172
173 int want_resched;
174 volatile int astpending;
175
176 void pmap_hptdump(void);
177 void syscall(struct trapframe *, int *);
178
179 /* XXX */
180 void hppa_trapsignal_hack(struct lwp *, int, u_long);
181
182 #ifdef USERTRACE
183 /*
184 * USERTRACE is a crude facility that traces the PC of
185 * a single user process. This tracing is normally
186 * activated by the dispatching of a certain syscall
187 * with certain arguments - see the activation code in
188 * syscall().
189 */
190 u_int rctr_next_iioq;
191 #endif
192
193 static __inline void
194 userret(struct lwp *l, register_t pc, u_quad_t oticks)
195 {
196 struct proc *p = l->l_proc;
197
198 l->l_priority = l->l_usrpri;
199 if (want_resched) {
200 preempt(0);
201 }
202
203 mi_userret(l);
204
205 /*
206 * If profiling, charge recent system time to the trapped pc.
207 */
208 if (l->l_flag & P_PROFIL) {
209 extern int psratio;
210
211 addupc_task(p, pc, (int)(p->p_sticks - oticks) * psratio);
212 }
213
214 curcpu()->ci_schedstate.spc_curpriority = l->l_priority;
215 }
216
217 /*
218 * This handles some messy kernel debugger details.
219 * It dispatches into either kgdb or DDB, and knows
220 * about some special things to do, like skipping over
221 * break instructions and how to really set up for
222 * a single-step.
223 */
224 #if defined(KGDB) || defined(DDB)
225 static int
226 trap_kdebug(int type, int code, struct trapframe *frame)
227 {
228 int handled;
229 u_int tf_iioq_head_old;
230 u_int tf_iioq_tail_old;
231
232 for(;;) {
233
234 /* This trap has not been handled. */
235 handled = 0;
236
237 /* Remember the instruction offset queue. */
238 tf_iioq_head_old = frame->tf_iioq_head;
239 tf_iioq_tail_old = frame->tf_iioq_tail;
240
241 #ifdef KGDB
242 /* Let KGDB handle it (if connected) */
243 if (!handled)
244 handled = kgdb_trap(type, frame);
245 #endif
246 #ifdef DDB
247 /* Let DDB handle it. */
248 if (!handled)
249 handled = kdb_trap(type, code, frame);
250 #endif
251
252 /* If this trap wasn't handled, return now. */
253 if (!handled)
254 return(0);
255
256 /*
257 * If the instruction offset queue head changed,
258 * but the offset queue tail didn't, assume that
259 * the user wants to jump to the head offset, and
260 * adjust the tail accordingly. This should fix
261 * the kgdb `jump' command, and can help DDB users
262 * who `set' the offset head but forget the tail.
263 */
264 if (frame->tf_iioq_head != tf_iioq_head_old &&
265 frame->tf_iioq_tail == tf_iioq_tail_old)
266 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
267
268 /*
269 * This is some single-stepping support.
270 * If we're trying to step through a nullified
271 * instruction, just advance by hand and trap
272 * again. Otherwise, load the recovery counter
273 * with zero.
274 */
275 if (frame->tf_ipsw & PSW_R) {
276 #ifdef TRAPDEBUG
277 printf("(single stepping at head 0x%x tail 0x%x)\n", frame->tf_iioq_head, frame->tf_iioq_tail);
278 #endif
279 if (frame->tf_ipsw & PSW_N) {
280 #ifdef TRAPDEBUG
281 printf("(single stepping past nullified)\n");
282 #endif
283
284 /* Advance the program counter. */
285 frame->tf_iioq_head = frame->tf_iioq_tail;
286 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
287
288 /* Clear flags. */
289 frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
290
291 /* Simulate another trap. */
292 type = T_RECOVERY;
293 continue;
294 }
295 frame->tf_rctr = 0;
296 }
297
298 /* We handled this trap. */
299 return (1);
300 }
301 /* NOTREACHED */
302 }
303 #else /* !KGDB && !DDB */
304 #define trap_kdebug(t, c, f) (0)
305 #endif /* !KGDB && !DDB */
306
307 #ifdef DIAGNOSTIC
308 /*
309 * These functions give a crude usermode backtrace. They
310 * really only work when code has been compiled without
311 * optimization, as they assume a certain function prologue
312 * sets up a frame pointer and stores the return pointer
313 * and arguments in it.
314 */
315 static void user_backtrace_raw(u_int, u_int);
316 static void
317 user_backtrace_raw(u_int pc, u_int fp)
318 {
319 int frame_number;
320 int arg_number;
321
322 for (frame_number = 0;
323 frame_number < 100 && pc > HPPA_PC_PRIV_MASK && fp;
324 frame_number++) {
325
326 printf("%3d: pc=%08x%s fp=0x%08x", frame_number,
327 pc & ~HPPA_PC_PRIV_MASK, USERMODE(pc) ? "" : "**", fp);
328 for(arg_number = 0; arg_number < 4; arg_number++)
329 printf(" arg%d=0x%08x", arg_number,
330 (int) fuword(HPPA_FRAME_CARG(arg_number, fp)));
331 printf("\n");
332 pc = fuword(((register_t *) fp) - 5); /* fetch rp */
333 if (pc == -1) {
334 printf(" fuword for pc failed\n");
335 break;
336 }
337 fp = fuword(((register_t *) fp) + 0); /* fetch previous fp */
338 if (fp == -1) {
339 printf(" fuword for fp failed\n");
340 break;
341 }
342 }
343 printf(" backtrace stopped with pc %08x fp 0x%08x\n", pc, fp);
344 }
345
346 static void user_backtrace(struct trapframe *, struct lwp *, int);
347 static void
348 user_backtrace(struct trapframe *tf, struct lwp *l, int type)
349 {
350 struct proc *p = l->l_proc;
351 u_int pc, fp, inst;
352
353 /*
354 * Display any trap type that we have.
355 */
356 if (type >= 0)
357 printf("pid %d (%s) trap #%d\n",
358 p->p_pid, p->p_comm, type & ~T_USER);
359
360 /*
361 * Assuming that the frame pointer in r3 is valid,
362 * dump out a stack trace.
363 */
364 fp = tf->tf_r3;
365 printf("pid %d (%s) backtrace, starting with fp 0x%08x\n",
366 p->p_pid, p->p_comm, fp);
367 user_backtrace_raw(tf->tf_iioq_head, fp);
368
369 /*
370 * In case the frame pointer in r3 is not valid,
371 * assuming the stack pointer is valid and the
372 * faulting function is a non-leaf, if we can
373 * find its prologue we can recover its frame
374 * pointer.
375 */
376 pc = tf->tf_iioq_head;
377 fp = tf->tf_sp - HPPA_FRAME_SIZE;
378 printf("pid %d (%s) backtrace, starting with sp 0x%08x pc 0x%08x\n",
379 p->p_pid, p->p_comm, tf->tf_sp, pc);
380 for(pc &= ~HPPA_PC_PRIV_MASK; pc > 0; pc -= sizeof(inst)) {
381 inst = fuword((register_t *) pc);
382 if (inst == -1) {
383 printf(" fuword for inst at pc %08x failed\n", pc);
384 break;
385 }
386 /* Check for the prologue instruction that sets sp. */
387 if (STWM_R1_D_SR0_SP(inst)) {
388 fp = tf->tf_sp - STWM_R1_D_SR0_SP(inst);
389 printf(" sp from fp at pc %08x: %08x\n", pc, inst);
390 break;
391 }
392 }
393 user_backtrace_raw(tf->tf_iioq_head, fp);
394 }
395 #endif /* DIAGNOSTIC */
396
397 #ifdef DEBUG
398 /*
399 * This sanity-checks a trapframe. It is full of various
400 * assumptions about what a healthy CPU state should be,
401 * with some documented elsewhere, some not.
402 */
403 struct trapframe *sanity_frame;
404 struct lwp *sanity_lwp;
405 int sanity_checked = 0;
406 void frame_sanity_check(struct trapframe *, struct lwp *);
407 void
408 frame_sanity_check(struct trapframe *tf, struct lwp *l)
409 {
410 extern int kernel_text;
411 extern int etext;
412 extern register_t kpsw;
413 extern vaddr_t hpt_base;
414 extern vsize_t hpt_mask;
415 vsize_t uspace_size;
416 #define SANITY(e) \
417 do { \
418 if (sanity_frame == NULL && !(e)) { \
419 sanity_frame = tf; \
420 sanity_lwp = l; \
421 sanity_checked = __LINE__; \
422 } \
423 } while (/* CONSTCOND */ 0)
424
425 SANITY((tf->tf_ipsw & kpsw) == kpsw);
426 SANITY(tf->tf_hptm == hpt_mask && tf->tf_vtop == hpt_base);
427 SANITY((kpsw & PSW_I) == 0 || tf->tf_eiem != 0);
428 if (tf->tf_iisq_head == HPPA_SID_KERNEL) {
429 /*
430 * If the trap happened in the gateway
431 * page, we take the easy way out and
432 * assume that the trapframe is okay.
433 */
434 if ((tf->tf_iioq_head & ~PAGE_MASK) != SYSCALLGATE) {
435 SANITY(!USERMODE(tf->tf_iioq_head));
436 SANITY(!USERMODE(tf->tf_iioq_tail));
437 SANITY(tf->tf_iioq_head >= (u_int) &kernel_text);
438 SANITY(tf->tf_iioq_head < (u_int) &etext);
439 SANITY(tf->tf_iioq_tail >= (u_int) &kernel_text);
440 SANITY(tf->tf_iioq_tail < (u_int) &etext);
441 #ifdef HPPA_REDZONE
442 uspace_size = HPPA_REDZONE;
443 #else
444 uspace_size = USPACE;
445 #endif
446 SANITY(l == NULL ||
447 ((tf->tf_sp >= (u_int)(l->l_addr) + PAGE_SIZE &&
448 tf->tf_sp < (u_int)(l->l_addr) + uspace_size)));
449 }
450 } else {
451 SANITY(USERMODE(tf->tf_iioq_head));
452 SANITY(USERMODE(tf->tf_iioq_tail));
453 SANITY(l != NULL && tf->tf_cr30 == kvtop((caddr_t)l->l_addr));
454 }
455 #undef SANITY
456 if (sanity_frame == tf) {
457 (void) trap_kdebug(T_IBREAK, 0, tf);
458 sanity_frame = NULL;
459 sanity_lwp = NULL;
460 sanity_checked = 0;
461 }
462 }
463 #endif /* DEBUG */
464
465 void
466 trap(int type, struct trapframe *frame)
467 {
468 struct lwp *l;
469 struct proc *p;
470 struct pcb *pcbp;
471 vaddr_t va;
472 struct vm_map *map;
473 struct vmspace *vm;
474 vm_prot_t vftype;
475 pa_space_t space;
476 u_int opcode, onfault;
477 int ret;
478 const char *tts;
479 int type_raw;
480 #ifdef DIAGNOSTIC
481 extern int emergency_stack_start, emergency_stack_end;
482 #endif
483
484 type_raw = type & ~T_USER;
485 opcode = frame->tf_iir;
486 if (type_raw == T_ITLBMISS || type_raw == T_ITLBMISSNA) {
487 va = frame->tf_iioq_head;
488 space = frame->tf_iisq_head;
489 vftype = VM_PROT_EXECUTE;
490 } else {
491 va = frame->tf_ior;
492 space = frame->tf_isr;
493 vftype = inst_store(opcode) ? VM_PROT_WRITE : VM_PROT_READ;
494 }
495
496 l = curlwp;
497 p = l ? l->l_proc : NULL;
498
499 #ifdef DIAGNOSTIC
500 /*
501 * If we are on the emergency stack, then we either got
502 * a fault on the kernel stack, or we're just handling
503 * a trap for the machine check handler (which also
504 * runs on the emergency stack).
505 *
506 * We *very crudely* differentiate between the two cases
507 * by checking the faulting instruction: if it is the
508 * function prologue instruction that stores the old
509 * frame pointer and updates the stack pointer, we assume
510 * that we faulted on the kernel stack.
511 *
512 * In this case, not completing that instruction will
513 * probably confuse backtraces in kgdb/ddb. Completing
514 * it would be difficult, because we already faulted on
515 * that part of the stack, so instead we fix up the
516 * frame as if the function called has just returned.
517 * This has peculiar knowledge about what values are in
518 * what registers during the "normal gcc -g" prologue.
519 */
520 if (&type >= &emergency_stack_start &&
521 &type < &emergency_stack_end &&
522 type != T_IBREAK && STWM_R1_D_SR0_SP(opcode)) {
523 /* Restore the caller's frame pointer. */
524 frame->tf_r3 = frame->tf_r1;
525 /* Restore the caller's instruction offsets. */
526 frame->tf_iioq_head = frame->tf_rp;
527 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
528 goto dead_end;
529 }
530 #endif /* DIAGNOSTIC */
531
532 #ifdef DEBUG
533 frame_sanity_check(frame, l);
534 #endif /* DEBUG */
535
536 /* If this is a trap, not an interrupt, reenable interrupts. */
537 if (type_raw != T_INTERRUPT)
538 mtctl(frame->tf_eiem, CR_EIEM);
539
540 if (frame->tf_flags & TFF_LAST)
541 l->l_md.md_regs = frame;
542
543 if ((type & ~T_USER) > trap_types)
544 tts = "reserved";
545 else
546 tts = trap_type[type & ~T_USER];
547
548 #ifdef TRAPDEBUG
549 if (type_raw != T_INTERRUPT && type_raw != T_IBREAK)
550 printf("trap: %d, %s for %x:%x at %x:%x, fp=%p, rp=%x\n",
551 type, tts, space, (u_int)va, frame->tf_iisq_head,
552 frame->tf_iioq_head, frame, frame->tf_rp);
553 else if (type_raw == T_IBREAK)
554 printf("trap: break instruction %x:%x at %x:%x, fp=%p\n",
555 break5(opcode), break13(opcode),
556 frame->tf_iisq_head, frame->tf_iioq_head, frame);
557
558 {
559 extern int etext;
560 if (frame < (struct trapframe *)&etext) {
561 printf("trap: bogus frame ptr %p\n", frame);
562 goto dead_end;
563 }
564 }
565 #endif
566 switch (type) {
567 case T_NONEXIST:
568 case T_NONEXIST|T_USER:
569 #if !defined(DDB) && !defined(KGDB)
570 /* we've got screwed up by the central scrutinizer */
571 panic ("trap: elvis has just left the building!");
572 break;
573 #else
574 goto dead_end;
575 #endif
576 case T_RECOVERY|T_USER:
577 #ifdef USERTRACE
578 for(;;) {
579 if (frame->tf_iioq_head != rctr_next_iioq)
580 printf("-%08x\nr %08x",
581 rctr_next_iioq - 4,
582 frame->tf_iioq_head);
583 rctr_next_iioq = frame->tf_iioq_head + 4;
584 if (frame->tf_ipsw & PSW_N) {
585 /* Advance the program counter. */
586 frame->tf_iioq_head = frame->tf_iioq_tail;
587 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
588 /* Clear flags. */
589 frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
590 /* Simulate another trap. */
591 continue;
592 }
593 break;
594 }
595 frame->tf_rctr = 0;
596 break;
597 #endif /* USERTRACE */
598 case T_RECOVERY:
599 #if !defined(DDB) && !defined(KGDB)
600 /* XXX will implement later */
601 printf ("trap: handicapped");
602 break;
603 #else
604 goto dead_end;
605 #endif
606
607 case T_EMULATION | T_USER:
608 #ifdef FPEMUL
609 hppa_fpu_emulate(frame, l);
610 #else /* !FPEMUL */
611 /*
612 * We don't have FPU emulation, so signal the
613 * process with a SIGFPE.
614 */
615 hppa_trapsignal_hack(l, SIGFPE, frame->tf_iioq_head);
616 #endif /* !FPEMUL */
617 break;
618
619 #ifdef DIAGNOSTIC
620 case T_EXCEPTION:
621 panic("FPU/SFU emulation botch");
622
623 /* these just can't happen ever */
624 case T_PRIV_OP:
625 case T_PRIV_REG:
626 /* these just can't make it to the trap() ever */
627 case T_HPMC: case T_HPMC | T_USER:
628 case T_EMULATION:
629 #endif
630 case T_IBREAK:
631 case T_DATALIGN:
632 case T_DBREAK:
633 dead_end:
634 if (type & T_USER) {
635 #ifdef DEBUG
636 user_backtrace(frame, l, type);
637 #endif
638 hppa_trapsignal_hack(l, SIGILL, frame->tf_iioq_head);
639 break;
640 }
641 if (trap_kdebug(type, va, frame))
642 return;
643 else if (type == T_DATALIGN)
644 panic ("trap: %s at 0x%x", tts, (u_int) va);
645 else
646 panic ("trap: no debugger for \"%s\" (%d)", tts, type);
647 break;
648
649 case T_IBREAK | T_USER:
650 case T_DBREAK | T_USER:
651 /* pass to user debugger */
652 break;
653
654 case T_EXCEPTION | T_USER: /* co-proc assist trap */
655 hppa_trapsignal_hack(l, SIGFPE, va);
656 break;
657
658 case T_OVERFLOW | T_USER:
659 hppa_trapsignal_hack(l, SIGFPE, va);
660 break;
661
662 case T_CONDITION | T_USER:
663 break;
664
665 case T_ILLEGAL | T_USER:
666 #ifdef DEBUG
667 user_backtrace(frame, l, type);
668 #endif
669 hppa_trapsignal_hack(l, SIGILL, va);
670 break;
671
672 case T_PRIV_OP | T_USER:
673 #ifdef DEBUG
674 user_backtrace(frame, l, type);
675 #endif
676 hppa_trapsignal_hack(l, SIGILL, va);
677 break;
678
679 case T_PRIV_REG | T_USER:
680 #ifdef DEBUG
681 user_backtrace(frame, l, type);
682 #endif
683 hppa_trapsignal_hack(l, SIGILL, va);
684 break;
685
686 /* these should never got here */
687 case T_HIGHERPL | T_USER:
688 case T_LOWERPL | T_USER:
689 hppa_trapsignal_hack(l, SIGSEGV, va);
690 break;
691
692 case T_IPROT | T_USER:
693 case T_DPROT | T_USER:
694 hppa_trapsignal_hack(l, SIGSEGV, va);
695 break;
696
697 case T_DATACC: case T_USER | T_DATACC:
698 case T_ITLBMISS: case T_USER | T_ITLBMISS:
699 case T_DTLBMISS: case T_USER | T_DTLBMISS:
700 case T_ITLBMISSNA: case T_USER | T_ITLBMISSNA:
701 case T_DTLBMISSNA: case T_USER | T_DTLBMISSNA:
702 case T_TLB_DIRTY: case T_USER | T_TLB_DIRTY:
703 vm = p->p_vmspace;
704
705 if (!vm) {
706 #ifdef TRAPDEBUG
707 printf("trap: no vm, p=%p\n", p);
708 #endif
709 goto dead_end;
710 }
711
712 /*
713 * it could be a kernel map for exec_map faults
714 */
715 if (!(type & T_USER) && space == HPPA_SID_KERNEL)
716 map = kernel_map;
717 else {
718 map = &vm->vm_map;
719 if (l->l_flag & L_SA) {
720 l->l_savp->savp_faultaddr = va;
721 l->l_flag |= L_SA_PAGEFAULT;
722 }
723 }
724
725 va = hppa_trunc_page(va);
726
727 if (map->pmap->pmap_space != space) {
728 #ifdef TRAPDEBUG
729 printf("trap: space missmatch %d != %d\n",
730 space, map->pmap->pmap_space);
731 #endif
732 /* actually dump the user, crap the kernel */
733 goto dead_end;
734 }
735
736 /* Never call uvm_fault in interrupt context. */
737 KASSERT(hppa_intr_depth == 0);
738
739 onfault = l->l_addr->u_pcb.pcb_onfault;
740 l->l_addr->u_pcb.pcb_onfault = 0;
741 ret = uvm_fault(map, va, 0, vftype);
742 l->l_addr->u_pcb.pcb_onfault = onfault;
743
744 #ifdef TRAPDEBUG
745 printf("uvm_fault(%p, %x, %d, %d)=%d\n",
746 map, (u_int)va, 0, vftype, ret);
747 #endif
748
749 if (map != kernel_map)
750 l->l_flag &= ~L_SA_PAGEFAULT;
751 /*
752 * If this was a stack access we keep track of the maximum
753 * accessed stack size. Also, if uvm_fault gets a protection
754 * failure it is due to accessing the stack region outside
755 * the current limit and we need to reflect that as an access
756 * error.
757 */
758 if (va >= (vaddr_t)vm->vm_maxsaddr + vm->vm_ssize) {
759 if (ret == 0) {
760 vsize_t nss = btoc(va - USRSTACK + PAGE_SIZE);
761 if (nss > vm->vm_ssize)
762 vm->vm_ssize = nss;
763 } else if (ret == EACCES)
764 ret = EFAULT;
765 }
766
767 if (ret != 0) {
768 if (type & T_USER) {
769 #ifdef DEBUG
770 user_backtrace(frame, l, type);
771 #endif
772 hppa_trapsignal_hack(l, SIGSEGV, frame->tf_ior);
773 } else {
774 if (l->l_addr->u_pcb.pcb_onfault) {
775 #ifdef TRAPDEBUG
776 printf("trap: copyin/out %d\n",ret);
777 #endif
778 pcbp = &l->l_addr->u_pcb;
779 frame->tf_iioq_tail = 4 +
780 (frame->tf_iioq_head =
781 pcbp->pcb_onfault);
782 pcbp->pcb_onfault = 0;
783 break;
784 }
785 panic("trap: uvm_fault(%p, %lx, %d, %d): %d",
786 map, va, 0, vftype, ret);
787 }
788 }
789 break;
790
791 case T_DATALIGN | T_USER:
792 #ifdef DEBUG
793 user_backtrace(frame, l, type);
794 #endif
795 hppa_trapsignal_hack(l, SIGBUS, va);
796 break;
797
798 case T_INTERRUPT:
799 case T_INTERRUPT|T_USER:
800 hppa_intr(frame);
801 mtctl(frame->tf_eiem, CR_EIEM);
802 #if 0
803 if (trap_kdebug (type, va, frame))
804 return;
805 #endif
806 break;
807 case T_LOWERPL:
808 case T_DPROT:
809 case T_IPROT:
810 case T_OVERFLOW:
811 case T_CONDITION:
812 case T_ILLEGAL:
813 case T_HIGHERPL:
814 case T_TAKENBR:
815 case T_POWERFAIL:
816 case T_LPMC:
817 case T_PAGEREF:
818 case T_DATAPID: case T_DATAPID | T_USER:
819 if (0 /* T-chip */) {
820 break;
821 }
822 /* FALLTHROUGH to unimplemented */
823 default:
824 panic ("trap: unimplemented \'%s\' (%d)", tts, type);
825 }
826
827 if (type & T_USER)
828 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
829
830 #ifdef DEBUG
831 frame_sanity_check(frame, l);
832 if (frame->tf_flags & TFF_LAST && curlwp != NULL)
833 frame_sanity_check(curlwp->l_md.md_regs, curlwp);
834 #endif /* DEBUG */
835 }
836
837 void
838 child_return(void *arg)
839 {
840 struct lwp *l = arg;
841 struct proc *p = l->l_proc;
842
843 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
844 #ifdef KTRACE
845 if (KTRPOINT(p, KTR_SYSRET))
846 ktrsysret(p, SYS_fork, 0, 0);
847 #endif
848 #ifdef DEBUG
849 frame_sanity_check(l->l_md.md_regs, l);
850 #endif /* DEBUG */
851 }
852
853 /*
854 * call actual syscall routine
855 * from the low-level syscall handler:
856 * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
857 * our stack, this wins compared to copyin just needed amount anyway
858 * - register args are copied onto stack too
859 */
860 void
861 syscall(struct trapframe *frame, int *args)
862 {
863 struct lwp *l;
864 struct proc *p;
865 const struct sysent *callp;
866 int nsys, code, argsize, error;
867 int tmp;
868 int rval[2];
869
870 uvmexp.syscalls++;
871
872 #ifdef DEBUG
873 frame_sanity_check(frame, curlwp);
874 #endif /* DEBUG */
875
876 if (!USERMODE(frame->tf_iioq_head))
877 panic("syscall");
878
879 l = curlwp;
880 p = l->l_proc;
881 l->l_md.md_regs = frame;
882 nsys = p->p_emul->e_nsysent;
883 callp = p->p_emul->e_sysent;
884 code = frame->tf_t1;
885
886 /*
887 * Restarting a system call is touchy on the HPPA,
888 * because syscall arguments are passed in registers
889 * and the program counter of the syscall "point"
890 * isn't easily divined.
891 *
892 * We handle the first problem by assuming that we
893 * will have to restart this system call, so we
894 * stuff the first four words of the original arguments
895 * back into the frame as arg0...arg3, which is where
896 * we found them in the first place. Any further
897 * arguments are (still) on the user's stack and the
898 * syscall code will fetch them from there (again).
899 *
900 * The program counter problem is addressed below.
901 */
902 frame->tf_arg0 = args[0];
903 frame->tf_arg1 = args[1];
904 frame->tf_arg2 = args[2];
905 frame->tf_arg3 = args[3];
906
907 /*
908 * Some special handling for the syscall(2) and
909 * __syscall(2) system calls.
910 */
911 switch (code) {
912 case SYS_syscall:
913 code = *args;
914 args += 1;
915 break;
916 case SYS___syscall:
917 if (callp != sysent)
918 break;
919 /*
920 * NB: even though __syscall(2) takes a quad_t
921 * containing the system call number, because
922 * our argument copying word-swaps 64-bit arguments,
923 * the least significant word of that quad_t
924 * is the first word in the argument array.
925 */
926 code = *args;
927 args += 2;
928 }
929
930 /*
931 * Stacks growing from lower addresses to higher
932 * addresses are not really such a good idea, because
933 * it makes it impossible to overlay a struct on top
934 * of C stack arguments (the arguments appear in
935 * reversed order).
936 *
937 * You can do the obvious thing (as locore.S does) and
938 * copy argument words one by one, laying them out in
939 * the "right" order in the destination buffer, but this
940 * ends up word-swapping multi-word arguments (like off_t).
941 *
942 * To compensate, we have some automatically-generated
943 * code that word-swaps these multi-word arguments.
944 * Right now the script that generates this code is
945 * in Perl, because I don't know awk.
946 *
947 * FIXME - this works only on native binaries and
948 * will probably screw up any and all emulation.
949 */
950 switch (code) {
951 /*
952 * BEGIN automatically generated
953 * by /home/fredette/project/hppa/makescargfix.pl
954 * do not edit!
955 */
956 case SYS_pread:
957 /*
958 * syscallarg(int) fd;
959 * syscallarg(void *) buf;
960 * syscallarg(size_t) nbyte;
961 * syscallarg(int) pad;
962 * syscallarg(off_t) offset;
963 */
964 tmp = args[4];
965 args[4] = args[4 + 1];
966 args[4 + 1] = tmp;
967 break;
968 case SYS_pwrite:
969 /*
970 * syscallarg(int) fd;
971 * syscallarg(const void *) buf;
972 * syscallarg(size_t) nbyte;
973 * syscallarg(int) pad;
974 * syscallarg(off_t) offset;
975 */
976 tmp = args[4];
977 args[4] = args[4 + 1];
978 args[4 + 1] = tmp;
979 break;
980 case SYS_mmap:
981 /*
982 * syscallarg(void *) addr;
983 * syscallarg(size_t) len;
984 * syscallarg(int) prot;
985 * syscallarg(int) flags;
986 * syscallarg(int) fd;
987 * syscallarg(long) pad;
988 * syscallarg(off_t) pos;
989 */
990 tmp = args[6];
991 args[6] = args[6 + 1];
992 args[6 + 1] = tmp;
993 break;
994 case SYS_lseek:
995 /*
996 * syscallarg(int) fd;
997 * syscallarg(int) pad;
998 * syscallarg(off_t) offset;
999 */
1000 tmp = args[2];
1001 args[2] = args[2 + 1];
1002 args[2 + 1] = tmp;
1003 break;
1004 case SYS_truncate:
1005 /*
1006 * syscallarg(const char *) path;
1007 * syscallarg(int) pad;
1008 * syscallarg(off_t) length;
1009 */
1010 tmp = args[2];
1011 args[2] = args[2 + 1];
1012 args[2 + 1] = tmp;
1013 break;
1014 case SYS_ftruncate:
1015 /*
1016 * syscallarg(int) fd;
1017 * syscallarg(int) pad;
1018 * syscallarg(off_t) length;
1019 */
1020 tmp = args[2];
1021 args[2] = args[2 + 1];
1022 args[2 + 1] = tmp;
1023 break;
1024 case SYS_preadv:
1025 /*
1026 * syscallarg(int) fd;
1027 * syscallarg(const struct iovec *) iovp;
1028 * syscallarg(int) iovcnt;
1029 * syscallarg(int) pad;
1030 * syscallarg(off_t) offset;
1031 */
1032 tmp = args[4];
1033 args[4] = args[4 + 1];
1034 args[4 + 1] = tmp;
1035 break;
1036 case SYS_pwritev:
1037 /*
1038 * syscallarg(int) fd;
1039 * syscallarg(const struct iovec *) iovp;
1040 * syscallarg(int) iovcnt;
1041 * syscallarg(int) pad;
1042 * syscallarg(off_t) offset;
1043 */
1044 tmp = args[4];
1045 args[4] = args[4 + 1];
1046 args[4 + 1] = tmp;
1047 break;
1048 default:
1049 break;
1050 /*
1051 * END automatically generated
1052 * by /home/fredette/project/hppa/makescargfix.pl
1053 * do not edit!
1054 */
1055 }
1056
1057 #ifdef USERTRACE
1058 if (0) {
1059 user_backtrace(frame, p, -1);
1060 frame->tf_ipsw |= PSW_R;
1061 frame->tf_rctr = 0;
1062 printf("r %08x", frame->tf_iioq_head);
1063 rctr_next_iioq = frame->tf_iioq_head + 4;
1064 }
1065 #endif
1066
1067 if (code < 0 || code >= nsys)
1068 callp += p->p_emul->e_nosys; /* bad syscall # */
1069 else
1070 callp += code;
1071 argsize = callp->sy_argsize;
1072
1073 if ((error = trace_enter(l, code, code, NULL, args)) != 0)
1074 goto bad;
1075
1076 rval[0] = 0;
1077 rval[1] = 0;
1078 switch (error = (*callp->sy_call)(l, args, rval)) {
1079 case 0:
1080 l = curlwp; /* changes on exec() */
1081 frame = l->l_md.md_regs;
1082 frame->tf_ret0 = rval[0];
1083 frame->tf_ret1 = rval[1];
1084 frame->tf_t1 = 0;
1085 break;
1086 case ERESTART:
1087 /*
1088 * Now we have to wind back the instruction
1089 * offset queue to the point where the system
1090 * call will be made again. This is inherently
1091 * tied to the SYSCALL macro.
1092 *
1093 * Currently, the part of the SYSCALL macro
1094 * that we want to rerun reads as:
1095 *
1096 * ldil L%SYSCALLGATE, r1
1097 * ble 4(sr7, r1)
1098 * ldi __CONCAT(SYS_,x), t1
1099 * ldw HPPA_FRAME_ERP(sr0,sp), rp
1100 *
1101 * And our offset queue head points to the
1102 * final ldw instruction. So we need to
1103 * subtract twelve to reach the ldil.
1104 */
1105 frame->tf_iioq_head -= 12;
1106 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
1107 break;
1108 case EJUSTRETURN:
1109 p = curproc;
1110 break;
1111 default:
1112 bad:
1113 if (p->p_emul->e_errno)
1114 error = p->p_emul->e_errno[error];
1115 frame->tf_t1 = error;
1116 break;
1117 }
1118
1119 trace_exit(l, code, args, rval, error);
1120
1121 userret(l, frame->tf_iioq_head, 0);
1122 #ifdef DEBUG
1123 frame_sanity_check(frame, l);
1124 #endif /* DEBUG */
1125 }
1126
1127 /*
1128 * Start a new LWP
1129 */
1130 void
1131 startlwp(void *arg)
1132 {
1133 int err;
1134 ucontext_t *uc = arg;
1135 struct lwp *l = curlwp;
1136
1137 err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
1138 #if DIAGNOSTIC
1139 if (err) {
1140 printf("Error %d from cpu_setmcontext.", err);
1141 }
1142 #endif
1143 pool_put(&lwp_uc_pool, uc);
1144
1145 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1146 }
1147
1148 /*
1149 * XXX This is a terrible name.
1150 */
1151 void
1152 upcallret(struct lwp *l)
1153 {
1154 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1155 }
1156
1157 /*
1158 * XXX for transition to SIGINFO
1159 */
1160 void
1161 hppa_trapsignal_hack(struct lwp *l, int signum, u_long code)
1162 {
1163 ksiginfo_t ksi;
1164
1165 KSI_INIT_TRAP(&ksi);
1166 ksi.ksi_signo = signum;
1167 ksi.ksi_trap = (int)code;
1168 trapsignal(l, &ksi);
1169 }
1170