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