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