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