trap.c revision 1.21 1 /* $NetBSD: trap.c,v 1.21 2004/07/24 18:59:06 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.21 2004/07/24 18:59:06 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, opcode);
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 uint64_t *fpp;
656 uint32_t *pex;
657 uint32_t ex, inst, stat;
658 int i, flt;
659
660 hppa_fpu_flush(l);
661 fpp = l->l_addr->u_pcb.pcb_fpregs;
662 pex = (uint32_t *)&fpp[0];
663 for (i = 0, pex++; i < 7 && !*pex; i++, pex++)
664 ;
665 flt = 0;
666 if (i < 7) {
667 ex = *pex;
668 stat = HPPA_FPU_OP(ex);
669
670 if (stat & HPPA_FPU_UNMPL)
671 flt = FPE_FLTINV;
672 else if (stat & (HPPA_FPU_V << 1))
673 flt = FPE_FLTINV;
674 else if (stat & (HPPA_FPU_Z << 1))
675 flt = FPE_FLTDIV;
676 else if (stat & (HPPA_FPU_I << 1))
677 flt = FPE_FLTRES;
678 else if (stat & (HPPA_FPU_O << 1))
679 flt = FPE_FLTOVF;
680 else if (stat & (HPPA_FPU_U << 1))
681 flt = FPE_FLTUND;
682 /* still left: under/over-flow w/ inexact */
683 *pex = 0;
684 }
685 /* reset the trap flag, as if there was none */
686 fpp[0] &= ~(((uint64_t)HPPA_FPU_T) << 32);
687
688 /* XXX assume it's opcode 0C */
689 inst = (0x0c << 26) | (ex & 0x03ffffff);
690 hppa_fpu_emulate(frame, l, inst);
691 }
692 break;
693
694 case T_OVERFLOW | T_USER:
695 hppa_trapsignal_hack(l, SIGFPE, va);
696 break;
697
698 case T_CONDITION | T_USER:
699 break;
700
701 case T_ILLEGAL | T_USER:
702 #ifdef DEBUG
703 user_backtrace(frame, l, type);
704 #endif
705 hppa_trapsignal_hack(l, SIGILL, va);
706 break;
707
708 case T_PRIV_OP | T_USER:
709 #ifdef DEBUG
710 user_backtrace(frame, l, type);
711 #endif
712 hppa_trapsignal_hack(l, SIGILL, va);
713 break;
714
715 case T_PRIV_REG | T_USER:
716 #ifdef DEBUG
717 user_backtrace(frame, l, type);
718 #endif
719 hppa_trapsignal_hack(l, SIGILL, va);
720 break;
721
722 /* these should never got here */
723 case T_HIGHERPL | T_USER:
724 case T_LOWERPL | T_USER:
725 hppa_trapsignal_hack(l, SIGSEGV, va);
726 break;
727
728 case T_IPROT | T_USER:
729 case T_DPROT | T_USER:
730 hppa_trapsignal_hack(l, SIGSEGV, va);
731 break;
732
733 case T_DATACC: case T_USER | T_DATACC:
734 case T_ITLBMISS: case T_USER | T_ITLBMISS:
735 case T_DTLBMISS: case T_USER | T_DTLBMISS:
736 case T_ITLBMISSNA: case T_USER | T_ITLBMISSNA:
737 case T_DTLBMISSNA: case T_USER | T_DTLBMISSNA:
738 case T_TLB_DIRTY: case T_USER | T_TLB_DIRTY:
739 vm = p->p_vmspace;
740
741 if (!vm) {
742 #ifdef TRAPDEBUG
743 printf("trap: no vm, p=%p\n", p);
744 #endif
745 goto dead_end;
746 }
747
748 /*
749 * it could be a kernel map for exec_map faults
750 */
751 if (!(type & T_USER) && space == HPPA_SID_KERNEL)
752 map = kernel_map;
753 else {
754 map = &vm->vm_map;
755 if (l->l_flag & L_SA) {
756 l->l_savp->savp_faultaddr = va;
757 l->l_flag |= L_SA_PAGEFAULT;
758 }
759 }
760
761 va = hppa_trunc_page(va);
762
763 if (map->pmap->pmap_space != space) {
764 #ifdef TRAPDEBUG
765 printf("trap: space missmatch %d != %d\n",
766 space, map->pmap->pmap_space);
767 #endif
768 /* actually dump the user, crap the kernel */
769 goto dead_end;
770 }
771
772 /* Never call uvm_fault in interrupt context. */
773 KASSERT(hppa_intr_depth == 0);
774
775 onfault = l->l_addr->u_pcb.pcb_onfault;
776 l->l_addr->u_pcb.pcb_onfault = 0;
777 ret = uvm_fault(map, va, 0, vftype);
778 l->l_addr->u_pcb.pcb_onfault = onfault;
779
780 #ifdef TRAPDEBUG
781 printf("uvm_fault(%p, %x, %d, %d)=%d\n",
782 map, (u_int)va, 0, vftype, ret);
783 #endif
784
785 if (map != kernel_map)
786 l->l_flag &= ~L_SA_PAGEFAULT;
787 /*
788 * If this was a stack access we keep track of the maximum
789 * accessed stack size. Also, if uvm_fault gets a protection
790 * failure it is due to accessing the stack region outside
791 * the current limit and we need to reflect that as an access
792 * error.
793 */
794 if (va >= (vaddr_t)vm->vm_maxsaddr + vm->vm_ssize) {
795 if (ret == 0) {
796 vsize_t nss = btoc(va - USRSTACK + PAGE_SIZE);
797 if (nss > vm->vm_ssize)
798 vm->vm_ssize = nss;
799 } else if (ret == EACCES)
800 ret = EFAULT;
801 }
802
803 if (ret != 0) {
804 if (type & T_USER) {
805 #ifdef DEBUG
806 user_backtrace(frame, l, type);
807 #endif
808 hppa_trapsignal_hack(l, SIGSEGV, frame->tf_ior);
809 } else {
810 if (l->l_addr->u_pcb.pcb_onfault) {
811 #ifdef TRAPDEBUG
812 printf("trap: copyin/out %d\n",ret);
813 #endif
814 pcbp = &l->l_addr->u_pcb;
815 frame->tf_iioq_tail = 4 +
816 (frame->tf_iioq_head =
817 pcbp->pcb_onfault);
818 pcbp->pcb_onfault = 0;
819 break;
820 }
821 panic("trap: uvm_fault(%p, %lx, %d, %d): %d",
822 map, va, 0, vftype, ret);
823 }
824 }
825 break;
826
827 case T_DATALIGN | T_USER:
828 #ifdef DEBUG
829 user_backtrace(frame, l, type);
830 #endif
831 hppa_trapsignal_hack(l, SIGBUS, va);
832 break;
833
834 case T_INTERRUPT:
835 case T_INTERRUPT|T_USER:
836 hppa_intr(frame);
837 mtctl(frame->tf_eiem, CR_EIEM);
838 #if 0
839 if (trap_kdebug (type, va, frame))
840 return;
841 #endif
842 break;
843 case T_LOWERPL:
844 case T_DPROT:
845 case T_IPROT:
846 case T_OVERFLOW:
847 case T_CONDITION:
848 case T_ILLEGAL:
849 case T_HIGHERPL:
850 case T_TAKENBR:
851 case T_POWERFAIL:
852 case T_LPMC:
853 case T_PAGEREF:
854 case T_DATAPID: case T_DATAPID | T_USER:
855 if (0 /* T-chip */) {
856 break;
857 }
858 /* FALLTHROUGH to unimplemented */
859 default:
860 panic ("trap: unimplemented \'%s\' (%d)", tts, type);
861 }
862
863 if (type & T_USER)
864 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
865
866 #ifdef DEBUG
867 frame_sanity_check(frame, l);
868 if (frame->tf_flags & TFF_LAST && curlwp != NULL)
869 frame_sanity_check(curlwp->l_md.md_regs, curlwp);
870 #endif /* DEBUG */
871 }
872
873 void
874 child_return(void *arg)
875 {
876 struct lwp *l = arg;
877 struct proc *p = l->l_proc;
878
879 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
880 #ifdef KTRACE
881 if (KTRPOINT(p, KTR_SYSRET))
882 ktrsysret(p, SYS_fork, 0, 0);
883 #endif
884 #ifdef DEBUG
885 frame_sanity_check(l->l_md.md_regs, l);
886 #endif /* DEBUG */
887 }
888
889 /*
890 * call actual syscall routine
891 * from the low-level syscall handler:
892 * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
893 * our stack, this wins compared to copyin just needed amount anyway
894 * - register args are copied onto stack too
895 */
896 void
897 syscall(struct trapframe *frame, int *args)
898 {
899 struct lwp *l;
900 struct proc *p;
901 const struct sysent *callp;
902 int nsys, code, argsize, error;
903 int tmp;
904 int rval[2];
905
906 uvmexp.syscalls++;
907
908 #ifdef DEBUG
909 frame_sanity_check(frame, curlwp);
910 #endif /* DEBUG */
911
912 if (!USERMODE(frame->tf_iioq_head))
913 panic("syscall");
914
915 l = curlwp;
916 p = l->l_proc;
917 l->l_md.md_regs = frame;
918 nsys = p->p_emul->e_nsysent;
919 callp = p->p_emul->e_sysent;
920 code = frame->tf_t1;
921
922 /*
923 * Restarting a system call is touchy on the HPPA,
924 * because syscall arguments are passed in registers
925 * and the program counter of the syscall "point"
926 * isn't easily divined.
927 *
928 * We handle the first problem by assuming that we
929 * will have to restart this system call, so we
930 * stuff the first four words of the original arguments
931 * back into the frame as arg0...arg3, which is where
932 * we found them in the first place. Any further
933 * arguments are (still) on the user's stack and the
934 * syscall code will fetch them from there (again).
935 *
936 * The program counter problem is addressed below.
937 */
938 frame->tf_arg0 = args[0];
939 frame->tf_arg1 = args[1];
940 frame->tf_arg2 = args[2];
941 frame->tf_arg3 = args[3];
942
943 /*
944 * Some special handling for the syscall(2) and
945 * __syscall(2) system calls.
946 */
947 switch (code) {
948 case SYS_syscall:
949 code = *args;
950 args += 1;
951 break;
952 case SYS___syscall:
953 if (callp != sysent)
954 break;
955 /*
956 * NB: even though __syscall(2) takes a quad_t
957 * containing the system call number, because
958 * our argument copying word-swaps 64-bit arguments,
959 * the least significant word of that quad_t
960 * is the first word in the argument array.
961 */
962 code = *args;
963 args += 2;
964 }
965
966 /*
967 * Stacks growing from lower addresses to higher
968 * addresses are not really such a good idea, because
969 * it makes it impossible to overlay a struct on top
970 * of C stack arguments (the arguments appear in
971 * reversed order).
972 *
973 * You can do the obvious thing (as locore.S does) and
974 * copy argument words one by one, laying them out in
975 * the "right" order in the destination buffer, but this
976 * ends up word-swapping multi-word arguments (like off_t).
977 *
978 * To compensate, we have some automatically-generated
979 * code that word-swaps these multi-word arguments.
980 * Right now the script that generates this code is
981 * in Perl, because I don't know awk.
982 *
983 * FIXME - this works only on native binaries and
984 * will probably screw up any and all emulation.
985 */
986 switch (code) {
987 /*
988 * BEGIN automatically generated
989 * by /home/fredette/project/hppa/makescargfix.pl
990 * do not edit!
991 */
992 case SYS_pread:
993 /*
994 * syscallarg(int) fd;
995 * syscallarg(void *) buf;
996 * syscallarg(size_t) nbyte;
997 * syscallarg(int) pad;
998 * syscallarg(off_t) offset;
999 */
1000 tmp = args[4];
1001 args[4] = args[4 + 1];
1002 args[4 + 1] = tmp;
1003 break;
1004 case SYS_pwrite:
1005 /*
1006 * syscallarg(int) fd;
1007 * syscallarg(const void *) buf;
1008 * syscallarg(size_t) nbyte;
1009 * syscallarg(int) pad;
1010 * syscallarg(off_t) offset;
1011 */
1012 tmp = args[4];
1013 args[4] = args[4 + 1];
1014 args[4 + 1] = tmp;
1015 break;
1016 case SYS_mmap:
1017 /*
1018 * syscallarg(void *) addr;
1019 * syscallarg(size_t) len;
1020 * syscallarg(int) prot;
1021 * syscallarg(int) flags;
1022 * syscallarg(int) fd;
1023 * syscallarg(long) pad;
1024 * syscallarg(off_t) pos;
1025 */
1026 tmp = args[6];
1027 args[6] = args[6 + 1];
1028 args[6 + 1] = tmp;
1029 break;
1030 case SYS_lseek:
1031 /*
1032 * syscallarg(int) fd;
1033 * syscallarg(int) pad;
1034 * syscallarg(off_t) offset;
1035 */
1036 tmp = args[2];
1037 args[2] = args[2 + 1];
1038 args[2 + 1] = tmp;
1039 break;
1040 case SYS_truncate:
1041 /*
1042 * syscallarg(const char *) path;
1043 * syscallarg(int) pad;
1044 * syscallarg(off_t) length;
1045 */
1046 tmp = args[2];
1047 args[2] = args[2 + 1];
1048 args[2 + 1] = tmp;
1049 break;
1050 case SYS_ftruncate:
1051 /*
1052 * syscallarg(int) fd;
1053 * syscallarg(int) pad;
1054 * syscallarg(off_t) length;
1055 */
1056 tmp = args[2];
1057 args[2] = args[2 + 1];
1058 args[2 + 1] = tmp;
1059 break;
1060 case SYS_preadv:
1061 /*
1062 * syscallarg(int) fd;
1063 * syscallarg(const struct iovec *) iovp;
1064 * syscallarg(int) iovcnt;
1065 * syscallarg(int) pad;
1066 * syscallarg(off_t) offset;
1067 */
1068 tmp = args[4];
1069 args[4] = args[4 + 1];
1070 args[4 + 1] = tmp;
1071 break;
1072 case SYS_pwritev:
1073 /*
1074 * syscallarg(int) fd;
1075 * syscallarg(const struct iovec *) iovp;
1076 * syscallarg(int) iovcnt;
1077 * syscallarg(int) pad;
1078 * syscallarg(off_t) offset;
1079 */
1080 tmp = args[4];
1081 args[4] = args[4 + 1];
1082 args[4 + 1] = tmp;
1083 break;
1084 default:
1085 break;
1086 /*
1087 * END automatically generated
1088 * by /home/fredette/project/hppa/makescargfix.pl
1089 * do not edit!
1090 */
1091 }
1092
1093 #ifdef USERTRACE
1094 if (0) {
1095 user_backtrace(frame, p, -1);
1096 frame->tf_ipsw |= PSW_R;
1097 frame->tf_rctr = 0;
1098 printf("r %08x", frame->tf_iioq_head);
1099 rctr_next_iioq = frame->tf_iioq_head + 4;
1100 }
1101 #endif
1102
1103 if (code < 0 || code >= nsys)
1104 callp += p->p_emul->e_nosys; /* bad syscall # */
1105 else
1106 callp += code;
1107 argsize = callp->sy_argsize;
1108
1109 if ((error = trace_enter(l, code, code, NULL, args)) != 0)
1110 goto bad;
1111
1112 rval[0] = 0;
1113 rval[1] = 0;
1114 switch (error = (*callp->sy_call)(l, args, rval)) {
1115 case 0:
1116 l = curlwp; /* changes on exec() */
1117 frame = l->l_md.md_regs;
1118 frame->tf_ret0 = rval[0];
1119 frame->tf_ret1 = rval[1];
1120 frame->tf_t1 = 0;
1121 break;
1122 case ERESTART:
1123 /*
1124 * Now we have to wind back the instruction
1125 * offset queue to the point where the system
1126 * call will be made again. This is inherently
1127 * tied to the SYSCALL macro.
1128 *
1129 * Currently, the part of the SYSCALL macro
1130 * that we want to rerun reads as:
1131 *
1132 * ldil L%SYSCALLGATE, r1
1133 * ble 4(sr7, r1)
1134 * ldi __CONCAT(SYS_,x), t1
1135 * ldw HPPA_FRAME_ERP(sr0,sp), rp
1136 *
1137 * And our offset queue head points to the
1138 * final ldw instruction. So we need to
1139 * subtract twelve to reach the ldil.
1140 */
1141 frame->tf_iioq_head -= 12;
1142 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
1143 break;
1144 case EJUSTRETURN:
1145 p = curproc;
1146 break;
1147 default:
1148 bad:
1149 if (p->p_emul->e_errno)
1150 error = p->p_emul->e_errno[error];
1151 frame->tf_t1 = error;
1152 break;
1153 }
1154
1155 trace_exit(l, code, args, rval, error);
1156
1157 userret(l, frame->tf_iioq_head, 0);
1158 #ifdef DEBUG
1159 frame_sanity_check(frame, l);
1160 #endif /* DEBUG */
1161 }
1162
1163 /*
1164 * Start a new LWP
1165 */
1166 void
1167 startlwp(void *arg)
1168 {
1169 int err;
1170 ucontext_t *uc = arg;
1171 struct lwp *l = curlwp;
1172
1173 err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
1174 #if DIAGNOSTIC
1175 if (err) {
1176 printf("Error %d from cpu_setmcontext.", err);
1177 }
1178 #endif
1179 pool_put(&lwp_uc_pool, uc);
1180
1181 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1182 }
1183
1184 /*
1185 * XXX This is a terrible name.
1186 */
1187 void
1188 upcallret(struct lwp *l)
1189 {
1190 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1191 }
1192
1193 /*
1194 * XXX for transition to SIGINFO
1195 */
1196 void
1197 hppa_trapsignal_hack(struct lwp *l, int signum, u_long code)
1198 {
1199 ksiginfo_t ksi;
1200
1201 KSI_INIT_TRAP(&ksi);
1202 ksi.ksi_signo = signum;
1203 ksi.ksi_trap = (int)code;
1204 trapsignal(l, &ksi);
1205 }
1206