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