trap.c revision 1.74 1 /* $NetBSD: trap.c,v 1.74 2010/02/10 20:51:23 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.74 2010/02/10 20:51:23 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 = __arraycount(trap_type);
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 (astpending) {
210 astpending = 0;
211 if (curcpu()->ci_want_resched) {
212 preempt();
213 }
214 }
215
216 mi_userret(l);
217
218 /*
219 * If profiling, charge recent system time to the trapped pc.
220 */
221 if (p->p_stflag & PST_PROFIL) {
222 extern int psratio;
223
224 addupc_task(l, pc, (int)(p->p_sticks - oticks) * psratio);
225 }
226 }
227
228 /*
229 * This handles some messy kernel debugger details.
230 * It dispatches into either kgdb or DDB, and knows
231 * about some special things to do, like skipping over
232 * break instructions and how to really set up for
233 * a single-step.
234 */
235 #if defined(KGDB) || defined(DDB)
236 static int
237 trap_kdebug(int type, int code, struct trapframe *frame)
238 {
239 int handled;
240 u_int tf_iioq_head_old;
241 u_int tf_iioq_tail_old;
242
243 for(;;) {
244
245 /* This trap has not been handled. */
246 handled = 0;
247
248 /* Remember the instruction offset queue. */
249 tf_iioq_head_old = frame->tf_iioq_head;
250 tf_iioq_tail_old = frame->tf_iioq_tail;
251
252 #ifdef KGDB
253 /* Let KGDB handle it (if connected) */
254 if (!handled)
255 handled = kgdb_trap(type, frame);
256 #endif
257 #ifdef DDB
258 /* Let DDB handle it. */
259 if (!handled)
260 handled = kdb_trap(type, code, frame);
261 #endif
262
263 /* If this trap wasn't handled, return now. */
264 if (!handled)
265 return(0);
266
267 /*
268 * If the instruction offset queue head changed,
269 * but the offset queue tail didn't, assume that
270 * the user wants to jump to the head offset, and
271 * adjust the tail accordingly. This should fix
272 * the kgdb `jump' command, and can help DDB users
273 * who `set' the offset head but forget the tail.
274 */
275 if (frame->tf_iioq_head != tf_iioq_head_old &&
276 frame->tf_iioq_tail == tf_iioq_tail_old)
277 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
278
279 /*
280 * This is some single-stepping support.
281 * If we're trying to step through a nullified
282 * instruction, just advance by hand and trap
283 * again. Otherwise, load the recovery counter
284 * with zero.
285 */
286 if (frame->tf_ipsw & PSW_R) {
287 #ifdef TRAPDEBUG
288 printf("(single stepping at head 0x%x tail 0x%x)\n",
289 frame->tf_iioq_head, frame->tf_iioq_tail);
290 #endif
291 if (frame->tf_ipsw & PSW_N) {
292 #ifdef TRAPDEBUG
293 printf("(single stepping past nullified)\n");
294 #endif
295
296 /* Advance the program counter. */
297 frame->tf_iioq_head = frame->tf_iioq_tail;
298 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
299
300 /* Clear flags. */
301 frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
302
303 /* Simulate another trap. */
304 type = T_RECOVERY;
305 continue;
306 }
307 frame->tf_rctr = 0;
308 }
309
310 /* We handled this trap. */
311 return (1);
312 }
313 /* NOTREACHED */
314 }
315 #else /* !KGDB && !DDB */
316 #define trap_kdebug(t, c, f) (0)
317 #endif /* !KGDB && !DDB */
318
319 #if defined(DEBUG) || defined(USERTRACE)
320 /*
321 * These functions give a crude usermode backtrace. They
322 * really only work when code has been compiled without
323 * optimization, as they assume a certain function prologue
324 * sets up a frame pointer and stores the return pointer
325 * and arguments in it.
326 */
327 static void
328 user_backtrace_raw(u_int pc, u_int fp)
329 {
330 int frame_number;
331 int arg_number;
332
333 for (frame_number = 0;
334 frame_number < 100 && pc > HPPA_PC_PRIV_MASK && fp;
335 frame_number++) {
336
337 printf("%3d: pc=%08x%s fp=0x%08x", frame_number,
338 pc & ~HPPA_PC_PRIV_MASK, USERMODE(pc) ? " " : "**", fp);
339 for(arg_number = 0; arg_number < 4; arg_number++)
340 printf(" arg%d=0x%08x", arg_number,
341 (int) fuword(HPPA_FRAME_CARG(arg_number, fp)));
342 printf("\n");
343 pc = fuword(((register_t *) fp) - 5); /* fetch rp */
344 if (pc == -1) {
345 printf(" fuword for pc failed\n");
346 break;
347 }
348 fp = fuword(((register_t *) fp) + 0); /* fetch previous fp */
349 if (fp == -1) {
350 printf(" fuword for fp failed\n");
351 break;
352 }
353 }
354 printf(" backtrace stopped with pc %08x fp 0x%08x\n", pc, fp);
355 }
356
357 static void
358 user_backtrace(struct trapframe *tf, struct lwp *l, int type)
359 {
360 struct proc *p = l->l_proc;
361 u_int pc, fp, inst;
362
363 /*
364 * Display any trap type that we have.
365 */
366 if (type >= 0)
367 printf("pid %d (%s) trap #%d\n",
368 p->p_pid, p->p_comm, type & ~T_USER);
369
370 /*
371 * Assuming that the frame pointer in r3 is valid,
372 * dump out a stack trace.
373 */
374 fp = tf->tf_r3;
375 printf("pid %d (%s) backtrace, starting with fp 0x%08x\n",
376 p->p_pid, p->p_comm, fp);
377 user_backtrace_raw(tf->tf_iioq_head, fp);
378
379 /*
380 * In case the frame pointer in r3 is not valid,
381 * assuming the stack pointer is valid and the
382 * faulting function is a non-leaf, if we can
383 * find its prologue we can recover its frame
384 * pointer.
385 */
386 pc = tf->tf_iioq_head;
387 fp = tf->tf_sp - HPPA_FRAME_SIZE;
388 printf("pid %d (%s) backtrace, starting with sp 0x%08x pc 0x%08x\n",
389 p->p_pid, p->p_comm, tf->tf_sp, pc);
390 for (pc &= ~HPPA_PC_PRIV_MASK; pc > 0; pc -= sizeof(inst)) {
391 inst = fuword((register_t *) pc);
392 if (inst == -1) {
393 printf(" fuword for inst at pc %08x failed\n", pc);
394 break;
395 }
396 /* Check for the prologue instruction that sets sp. */
397 if (STWM_R1_D_SR0_SP(inst)) {
398 fp = tf->tf_sp - STWM_R1_D_SR0_SP(inst);
399 printf(" sp from fp at pc %08x: %08x\n", pc, inst);
400 break;
401 }
402 }
403 user_backtrace_raw(tf->tf_iioq_head, fp);
404 }
405 #endif /* DEBUG || USERTRACE */
406
407 #ifdef DEBUG
408 /*
409 * This sanity-checks a trapframe. It is full of various
410 * assumptions about what a healthy CPU state should be,
411 * with some documented elsewhere, some not.
412 */
413 void
414 frame_sanity_check(int where, int type, struct trapframe *tf, struct lwp *l)
415 {
416 extern int kernel_text;
417 extern int etext;
418 extern register_t kpsw;
419
420 #define SANITY(e) \
421 do { \
422 if (sanity_frame == NULL && !(e)) { \
423 sanity_frame = tf; \
424 sanity_lwp = l; \
425 sanity_checked = __LINE__; \
426 } \
427 } while (/* CONSTCOND */ 0)
428
429 SANITY((tf->tf_ipsw & kpsw) == kpsw);
430 SANITY((kpsw & PSW_I) == 0 || tf->tf_eiem != 0);
431 if (tf->tf_iisq_head == HPPA_SID_KERNEL) {
432 vaddr_t minsp, maxsp, uv;
433
434 uv = uvm_lwp_getuarea(l);
435
436 /*
437 * If the trap happened in the gateway
438 * page, we take the easy way out and
439 * assume that the trapframe is okay.
440 */
441 if ((tf->tf_iioq_head & ~PAGE_MASK) == SYSCALLGATE)
442 goto out;
443
444 SANITY(!USERMODE(tf->tf_iioq_head));
445 SANITY(!USERMODE(tf->tf_iioq_tail));
446
447 /*
448 * Don't check the instruction queues or stack on interrupts
449 * as we could be be in the sti code (outside normal kernel
450 * text) or switching LWPs (curlwp and sp are not in sync)
451 */
452 if ((type & ~T_USER) == T_INTERRUPT)
453 goto out;
454
455 SANITY(tf->tf_iioq_head >= (u_int) &kernel_text);
456 SANITY(tf->tf_iioq_head < (u_int) &etext);
457 SANITY(tf->tf_iioq_tail >= (u_int) &kernel_text);
458 SANITY(tf->tf_iioq_tail < (u_int) &etext);
459
460 maxsp = uv + USPACE + PAGE_SIZE;
461 minsp = uv + PAGE_SIZE;
462
463 SANITY(l != NULL || (tf->tf_sp >= minsp && tf->tf_sp < maxsp));
464 } else {
465 bool ok;
466 paddr_t pa;
467
468 ok = pmap_extract(pmap_kernel(), uvm_lwp_getuarea(l), &pa);
469 KASSERT(ok);
470
471 SANITY(USERMODE(tf->tf_iioq_head));
472 SANITY(USERMODE(tf->tf_iioq_tail));
473 SANITY(l != NULL && tf->tf_cr30 == pa);
474 }
475 #undef SANITY
476 out:
477 if (sanity_frame == tf) {
478 printf("insanity: where 0x%x type 0x%x tf %p lwp %p line %d "
479 "sp 0x%x pc 0x%x\n",
480 where, type, sanity_frame, sanity_lwp, sanity_checked,
481 tf->tf_sp, tf->tf_iioq_head);
482 (void) trap_kdebug(T_IBREAK, 0, tf);
483 sanity_frame = NULL;
484 sanity_lwp = NULL;
485 sanity_checked = 0;
486 }
487 }
488 #endif /* DEBUG */
489
490 void
491 trap(int type, struct trapframe *frame)
492 {
493 struct lwp *l;
494 struct proc *p;
495 struct pcb *pcb;
496 vaddr_t va;
497 struct vm_map *map;
498 struct vmspace *vm;
499 vm_prot_t vftype;
500 pa_space_t space;
501 ksiginfo_t ksi;
502 u_int opcode, onfault;
503 int ret;
504 const char *tts = "reserved";
505 int trapnum;
506 #ifdef DIAGNOSTIC
507 extern int emergency_stack_start, emergency_stack_end;
508
509 int oldcpl = cpl;
510 #endif
511
512 trapnum = type & ~T_USER;
513 opcode = frame->tf_iir;
514 if (trapnum == T_ITLBMISS || trapnum == T_ITLBMISSNA ||
515 trapnum == T_IBREAK || trapnum == T_TAKENBR) {
516 va = frame->tf_iioq_head;
517 space = frame->tf_iisq_head;
518 vftype = VM_PROT_EXECUTE;
519 } else {
520 va = frame->tf_ior;
521 space = frame->tf_isr;
522 vftype = inst_store(opcode) ? VM_PROT_WRITE : VM_PROT_READ;
523 }
524
525 KASSERT(curlwp != NULL);
526 l = curlwp;
527 p = l->l_proc;
528 if ((type & T_USER) != 0)
529 LWP_CACHE_CREDS(l, p);
530
531 #ifdef DIAGNOSTIC
532 /*
533 * If we are on the emergency stack, then we either got
534 * a fault on the kernel stack, or we're just handling
535 * a trap for the machine check handler (which also
536 * runs on the emergency stack).
537 *
538 * We *very crudely* differentiate between the two cases
539 * by checking the faulting instruction: if it is the
540 * function prologue instruction that stores the old
541 * frame pointer and updates the stack pointer, we assume
542 * that we faulted on the kernel stack.
543 *
544 * In this case, not completing that instruction will
545 * probably confuse backtraces in kgdb/ddb. Completing
546 * it would be difficult, because we already faulted on
547 * that part of the stack, so instead we fix up the
548 * frame as if the function called has just returned.
549 * This has peculiar knowledge about what values are in
550 * what registers during the "normal gcc -g" prologue.
551 */
552 if (&type >= &emergency_stack_start &&
553 &type < &emergency_stack_end &&
554 type != T_IBREAK && STWM_R1_D_SR0_SP(opcode)) {
555 /* Restore the caller's frame pointer. */
556 frame->tf_r3 = frame->tf_r1;
557 /* Restore the caller's instruction offsets. */
558 frame->tf_iioq_head = frame->tf_rp;
559 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
560 goto dead_end;
561 }
562 #endif /* DIAGNOSTIC */
563
564 #ifdef DEBUG
565 frame_sanity_check(0xdead01, type, frame, l);
566 #endif /* DEBUG */
567
568 if (frame->tf_flags & TFF_LAST)
569 l->l_md.md_regs = frame;
570
571 if (trapnum <= trap_types)
572 tts = trap_type[trapnum];
573
574 #ifdef TRAPDEBUG
575 if (trapnum != T_INTERRUPT && trapnum != T_IBREAK)
576 printf("trap: %d, %s for %x:%x at %x:%x, fp=%p, rp=%x\n",
577 type, tts, space, (u_int)va, frame->tf_iisq_head,
578 frame->tf_iioq_head, frame, frame->tf_rp);
579 else if (trapnum == T_IBREAK)
580 printf("trap: break instruction %x:%x at %x:%x, fp=%p\n",
581 break5(opcode), break13(opcode),
582 frame->tf_iisq_head, frame->tf_iioq_head, frame);
583
584 {
585 extern int etext;
586 if (frame < (struct trapframe *)&etext) {
587 printf("trap: bogus frame ptr %p\n", frame);
588 goto dead_end;
589 }
590 }
591 #endif
592
593 pcb = lwp_getpcb(l);
594
595 /* If this is a trap, not an interrupt, reenable interrupts. */
596 if (trapnum != T_INTERRUPT) {
597 uvmexp.traps++;
598 mtctl(frame->tf_eiem, CR_EIEM);
599 }
600
601 switch (type) {
602 case T_NONEXIST:
603 case T_NONEXIST|T_USER:
604 #if !defined(DDB) && !defined(KGDB)
605 /* we've got screwed up by the central scrutinizer */
606 panic ("trap: elvis has just left the building!");
607 break;
608 #else
609 goto dead_end;
610 #endif
611 case T_RECOVERY|T_USER:
612 #ifdef USERTRACE
613 for(;;) {
614 if (frame->tf_iioq_head != rctr_next_iioq)
615 printf("-%08x\nr %08x",
616 rctr_next_iioq - 4,
617 frame->tf_iioq_head);
618 rctr_next_iioq = frame->tf_iioq_head + 4;
619 if (frame->tf_ipsw & PSW_N) {
620 /* Advance the program counter. */
621 frame->tf_iioq_head = frame->tf_iioq_tail;
622 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
623 /* Clear flags. */
624 frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
625 /* Simulate another trap. */
626 continue;
627 }
628 break;
629 }
630 frame->tf_rctr = 0;
631 break;
632 #endif /* USERTRACE */
633 case T_RECOVERY:
634 #if !defined(DDB) && !defined(KGDB)
635 /* XXX will implement later */
636 printf ("trap: handicapped");
637 break;
638 #else
639 goto dead_end;
640 #endif
641
642 case T_EMULATION | T_USER:
643 #ifdef FPEMUL
644 hppa_fpu_emulate(frame, l, opcode);
645 #else /* !FPEMUL */
646 /*
647 * We don't have FPU emulation, so signal the
648 * process with a SIGFPE.
649 */
650
651 KSI_INIT_TRAP(&ksi);
652 ksi.ksi_signo = SIGFPE;
653 ksi.ksi_code = SI_NOINFO;
654 ksi.ksi_trap = type;
655 ksi.ksi_addr = (void *)frame->tf_iioq_head;
656 trapsignal(l, &ksi);
657 #endif /* !FPEMUL */
658 break;
659
660 case T_DATALIGN:
661 if (pcb->pcb_onfault) {
662 do_onfault:
663 frame->tf_iioq_head = pcb->pcb_onfault;
664 frame->tf_iioq_tail = 4 + frame->tf_iioq_head;
665 pcb->pcb_onfault = 0;
666 break;
667 }
668 /*FALLTHROUGH*/
669
670 #ifdef DIAGNOSTIC
671 /* these just can't happen ever */
672 case T_PRIV_OP:
673 case T_PRIV_REG:
674 /* these just can't make it to the trap() ever */
675 case T_HPMC:
676 case T_HPMC | T_USER:
677 case T_EMULATION:
678 case T_EXCEPTION:
679 #endif
680 case T_IBREAK:
681 case T_DBREAK:
682 dead_end:
683 if (type & T_USER) {
684 #ifdef DEBUG
685 user_backtrace(frame, l, type);
686 #endif
687 KSI_INIT_TRAP(&ksi);
688 ksi.ksi_signo = SIGILL;
689 ksi.ksi_code = ILL_ILLTRP;
690 ksi.ksi_trap = type;
691 ksi.ksi_addr = (void *)frame->tf_iioq_head;
692 trapsignal(l, &ksi);
693 break;
694 }
695 if (trap_kdebug(type, va, frame))
696 return;
697 else if (type == T_DATALIGN)
698 panic ("trap: %s at 0x%x", tts, (u_int) va);
699 else
700 panic ("trap: no debugger for \"%s\" (%d)", tts, type);
701 break;
702
703 case T_IBREAK | T_USER:
704 case T_DBREAK | T_USER:
705 KSI_INIT_TRAP(&ksi);
706 ksi.ksi_signo = SIGTRAP;
707 ksi.ksi_code = TRAP_TRACE;
708 ksi.ksi_trap = trapnum;
709 ksi.ksi_addr = (void *)frame->tf_iioq_head;
710 #ifdef PTRACE
711 ss_clear_breakpoints(l);
712 if (opcode == SSBREAKPOINT)
713 ksi.ksi_code = TRAP_BRKPT;
714 #endif
715 /* pass to user debugger */
716 trapsignal(l, &ksi);
717
718 break;
719
720 #ifdef PTRACE
721 case T_TAKENBR | T_USER:
722 ss_clear_breakpoints(l);
723
724 KSI_INIT_TRAP(&ksi);
725 ksi.ksi_signo = SIGTRAP;
726 ksi.ksi_code = TRAP_TRACE;
727 ksi.ksi_trap = trapnum;
728 ksi.ksi_addr = (void *)frame->tf_iioq_head;
729
730 /* pass to user debugger */
731 trapsignal(l, &ksi);
732 break;
733 #endif
734
735 case T_EXCEPTION | T_USER: { /* co-proc assist trap */
736 uint64_t *fpp;
737 uint32_t *pex, ex, inst;
738 int i;
739
740 hppa_fpu_flush(l);
741 fpp = pcb->pcb_fpregs;
742 pex = (uint32_t *)&fpp[1];
743 for (i = 1; i < 8 && !*pex; i++, pex++)
744 ;
745 KASSERT(i < 8);
746 ex = *pex;
747 *pex = 0;
748
749 /* reset the trap flag, as if there was none */
750 fpp[0] &= ~(((uint64_t)HPPA_FPU_T) << 32);
751
752 /* emulate the instruction */
753 inst = ((uint32_t)fpopmap[ex >> 26] << 26) | (ex & 0x03ffffff);
754 hppa_fpu_emulate(frame, l, inst);
755 }
756 break;
757
758 case T_OVERFLOW | T_USER:
759 KSI_INIT_TRAP(&ksi);
760 ksi.ksi_signo = SIGFPE;
761 ksi.ksi_code = SI_NOINFO;
762 ksi.ksi_trap = type;
763 ksi.ksi_addr = (void *)va;
764 trapsignal(l, &ksi);
765 break;
766
767 case T_CONDITION | T_USER:
768 KSI_INIT_TRAP(&ksi);
769 ksi.ksi_signo = SIGFPE;
770 ksi.ksi_code = FPE_INTDIV;
771 ksi.ksi_trap = type;
772 ksi.ksi_addr = (void *)va;
773 trapsignal(l, &ksi);
774 break;
775
776 case T_ILLEGAL | T_USER:
777 #ifdef DEBUG
778 user_backtrace(frame, l, type);
779 #endif
780 KSI_INIT_TRAP(&ksi);
781 ksi.ksi_signo = SIGILL;
782 ksi.ksi_code = ILL_ILLOPC;
783 ksi.ksi_trap = type;
784 ksi.ksi_addr = (void *)va;
785 trapsignal(l, &ksi);
786 break;
787
788 case T_PRIV_OP | T_USER:
789 #ifdef DEBUG
790 user_backtrace(frame, l, type);
791 #endif
792 KSI_INIT_TRAP(&ksi);
793 ksi.ksi_signo = SIGILL;
794 ksi.ksi_code = ILL_PRVOPC;
795 ksi.ksi_trap = type;
796 ksi.ksi_addr = (void *)va;
797 trapsignal(l, &ksi);
798 break;
799
800 case T_PRIV_REG | T_USER:
801 #ifdef DEBUG
802 user_backtrace(frame, l, type);
803 #endif
804 KSI_INIT_TRAP(&ksi);
805 ksi.ksi_signo = SIGILL;
806 ksi.ksi_code = ILL_PRVREG;
807 ksi.ksi_trap = type;
808 ksi.ksi_addr = (void *)va;
809 trapsignal(l, &ksi);
810 break;
811
812 /* these should never got here */
813 case T_HIGHERPL | T_USER:
814 case T_LOWERPL | T_USER:
815 KSI_INIT_TRAP(&ksi);
816 ksi.ksi_signo = SIGSEGV;
817 ksi.ksi_code = SEGV_ACCERR;
818 ksi.ksi_trap = type;
819 ksi.ksi_addr = (void *)va;
820 trapsignal(l, &ksi);
821 break;
822
823 case T_IPROT | T_USER:
824 case T_DPROT | T_USER:
825 KSI_INIT_TRAP(&ksi);
826 ksi.ksi_signo = SIGSEGV;
827 ksi.ksi_code = SEGV_ACCERR;
828 ksi.ksi_trap = type;
829 ksi.ksi_addr = (void *)va;
830 trapsignal(l, &ksi);
831 break;
832
833 case T_DATACC: case T_USER | T_DATACC:
834 case T_ITLBMISS: case T_USER | T_ITLBMISS:
835 case T_DTLBMISS: case T_USER | T_DTLBMISS:
836 case T_ITLBMISSNA: case T_USER | T_ITLBMISSNA:
837 case T_DTLBMISSNA: case T_USER | T_DTLBMISSNA:
838 case T_TLB_DIRTY: case T_USER | T_TLB_DIRTY:
839 vm = p->p_vmspace;
840
841 if (!vm) {
842 #ifdef TRAPDEBUG
843 printf("trap: no vm, p=%p\n", p);
844 #endif
845 goto dead_end;
846 }
847
848 /*
849 * it could be a kernel map for exec_map faults
850 */
851 if (!(type & T_USER) && space == HPPA_SID_KERNEL)
852 map = kernel_map;
853 else {
854 map = &vm->vm_map;
855 if ((l->l_flag & LW_SA)
856 && (~l->l_pflag & LP_SA_NOBLOCK)) {
857 l->l_savp->savp_faultaddr = va;
858 l->l_pflag |= LP_SA_PAGEFAULT;
859 }
860 }
861
862 va = trunc_page(va);
863
864 if (map->pmap->pm_space != space) {
865 #ifdef TRAPDEBUG
866 printf("trap: space mismatch %d != %d\n",
867 space, map->pmap->pm_space);
868 #endif
869 /* actually dump the user, crap the kernel */
870 goto dead_end;
871 }
872
873 /* Never call uvm_fault in interrupt context. */
874 KASSERT(hppa_intr_depth == 0);
875
876 onfault = pcb->pcb_onfault;
877 pcb->pcb_onfault = 0;
878 ret = uvm_fault(map, va, vftype);
879 pcb->pcb_onfault = onfault;
880
881 #ifdef TRAPDEBUG
882 printf("uvm_fault(%p, %x, %d)=%d\n",
883 map, (u_int)va, vftype, ret);
884 #endif
885
886 if (map != kernel_map)
887 l->l_pflag &= ~LP_SA_PAGEFAULT;
888
889 /*
890 * If this was a stack access we keep track of the maximum
891 * accessed stack size. Also, if uvm_fault gets a protection
892 * failure it is due to accessing the stack region outside
893 * the current limit and we need to reflect that as an access
894 * error.
895 */
896 if (map != kernel_map && va >= (vaddr_t)vm->vm_minsaddr) {
897 if (ret == 0)
898 uvm_grow(l->l_proc, va);
899 else if (ret == EACCES)
900 ret = EFAULT;
901 }
902
903 if (ret != 0) {
904 if (type & T_USER) {
905 #ifdef DEBUG
906 user_backtrace(frame, l, type);
907 #endif
908 KSI_INIT_TRAP(&ksi);
909 ksi.ksi_signo = SIGSEGV;
910 ksi.ksi_code = (ret == EACCES ?
911 SEGV_ACCERR : SEGV_MAPERR);
912 ksi.ksi_trap = type;
913 ksi.ksi_addr = (void *)va;
914 trapsignal(l, &ksi);
915 } else {
916 if (pcb->pcb_onfault) {
917 goto do_onfault;
918 }
919 panic("trap: uvm_fault(%p, %lx, %d): %d",
920 map, va, vftype, ret);
921 }
922 }
923 break;
924
925 case T_DATALIGN | T_USER:
926 #ifdef DEBUG
927 user_backtrace(frame, l, type);
928 #endif
929 KSI_INIT_TRAP(&ksi);
930 ksi.ksi_signo = SIGBUS;
931 ksi.ksi_code = BUS_ADRALN;
932 ksi.ksi_trap = type;
933 ksi.ksi_addr = (void *)va;
934 trapsignal(l, &ksi);
935 break;
936
937 case T_INTERRUPT:
938 case T_INTERRUPT|T_USER:
939 hppa_intr(frame);
940 mtctl(frame->tf_eiem, CR_EIEM);
941 break;
942
943 case T_LOWERPL:
944 case T_DPROT:
945 case T_IPROT:
946 case T_OVERFLOW:
947 case T_CONDITION:
948 case T_ILLEGAL:
949 case T_HIGHERPL:
950 case T_TAKENBR:
951 case T_POWERFAIL:
952 case T_LPMC:
953 case T_PAGEREF:
954 case T_DATAPID: case T_DATAPID | T_USER:
955 if (0 /* T-chip */) {
956 break;
957 }
958 /* FALLTHROUGH to unimplemented */
959 default:
960 panic ("trap: unimplemented \'%s\' (%d)", tts, type);
961 }
962
963 #ifdef DIAGNOSTIC
964 if (cpl != oldcpl)
965 printf("WARNING: SPL (%d) NOT LOWERED ON TRAP (%d) EXIT\n",
966 cpl, trapnum);
967 #endif
968
969 if (type & T_USER)
970 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
971
972 #ifdef DEBUG
973 frame_sanity_check(0xdead02, type, frame, l);
974 if (frame->tf_flags & TFF_LAST && (curlwp->l_flag & LW_IDLE) == 0)
975 frame_sanity_check(0xdead03, type, curlwp->l_md.md_regs,
976 curlwp);
977 #endif /* DEBUG */
978 }
979
980 void
981 child_return(void *arg)
982 {
983 struct lwp *l = arg;
984
985 /*
986 * Return values in the frame set by cpu_lwp_fork().
987 */
988
989 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
990 ktrsysret(SYS_fork, 0, 0);
991 #ifdef DEBUG
992 frame_sanity_check(0xdead04, 0, l->l_md.md_regs, l);
993 #endif /* DEBUG */
994 }
995
996 #ifdef PTRACE
997
998 #include <sys/ptrace.h>
999
1000 int
1001 ss_get_value(struct lwp *l, vaddr_t addr, u_int *value)
1002 {
1003 struct uio uio;
1004 struct iovec iov;
1005
1006 iov.iov_base = (void *)value;
1007 iov.iov_len = sizeof(u_int);
1008 uio.uio_iov = &iov;
1009 uio.uio_iovcnt = 1;
1010 uio.uio_offset = (off_t)addr;
1011 uio.uio_resid = sizeof(u_int);
1012 uio.uio_rw = UIO_READ;
1013 UIO_SETUP_SYSSPACE(&uio);
1014
1015 return (process_domem(curlwp, l, &uio));
1016 }
1017
1018 int
1019 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
1020 {
1021 struct uio uio;
1022 struct iovec iov;
1023
1024 iov.iov_base = (void *)&value;
1025 iov.iov_len = sizeof(u_int);
1026 uio.uio_iov = &iov;
1027 uio.uio_iovcnt = 1;
1028 uio.uio_offset = (off_t)addr;
1029 uio.uio_resid = sizeof(u_int);
1030 uio.uio_rw = UIO_WRITE;
1031 UIO_SETUP_SYSSPACE(&uio);
1032
1033 return (process_domem(curlwp, l, &uio));
1034 }
1035
1036 void
1037 ss_clear_breakpoints(struct lwp *l)
1038 {
1039 /* Restore origional instructions. */
1040 if (l->l_md.md_bpva != 0) {
1041 ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
1042 ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
1043 l->l_md.md_bpva = 0;
1044 }
1045 }
1046
1047
1048 int
1049 process_sstep(struct lwp *l, int sstep)
1050 {
1051 struct trapframe *tf = l->l_md.md_regs;
1052 int error;
1053
1054 ss_clear_breakpoints(l);
1055
1056 /* We're continuing... */
1057 /* Don't touch the syscall gateway page. */
1058 /* XXX head */
1059 if (sstep == 0 ||
1060 (tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE) {
1061 tf->tf_ipsw &= ~PSW_T;
1062 return 0;
1063 }
1064
1065 l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
1066
1067 /*
1068 * Insert two breakpoint instructions; the first one might be
1069 * nullified. Of course we need to save two instruction
1070 * first.
1071 */
1072
1073 error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
1074 if (error)
1075 return (error);
1076 error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
1077 if (error)
1078 return (error);
1079
1080 error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
1081 if (error)
1082 return error;
1083 error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
1084 if (error)
1085 return error;
1086
1087 tf->tf_ipsw |= PSW_T;
1088
1089 return 0;
1090 }
1091 #endif
1092
1093
1094 /*
1095 * call actual syscall routine
1096 * from the low-level syscall handler:
1097 * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
1098 * our stack, this wins compared to copyin just needed amount anyway
1099 * - register args are copied onto stack too
1100 */
1101 void
1102 syscall(struct trapframe *frame, int *args)
1103 {
1104 struct lwp *l;
1105 struct proc *p;
1106 const struct sysent *callp;
1107 size_t nargs64;
1108 int nsys, code, error;
1109 int tmp;
1110 int rval[2];
1111 #ifdef DIAGNOSTIC
1112 int oldcpl = cpl;
1113 #endif
1114
1115 uvmexp.syscalls++;
1116
1117 #ifdef DEBUG
1118 frame_sanity_check(0xdead04, 0, frame, curlwp);
1119 #endif /* DEBUG */
1120
1121 if (!USERMODE(frame->tf_iioq_head))
1122 panic("syscall");
1123
1124 KASSERT(curlwp != NULL);
1125 l = curlwp;
1126 p = l->l_proc;
1127 l->l_md.md_regs = frame;
1128 nsys = p->p_emul->e_nsysent;
1129 callp = p->p_emul->e_sysent;
1130 code = frame->tf_t1;
1131 LWP_CACHE_CREDS(l, p);
1132
1133 #ifdef KERN_SA
1134 if (__predict_false((l->l_savp)
1135 && (l->l_savp->savp_pflags & SAVP_FLAG_DELIVERING)))
1136 l->l_savp->savp_pflags &= ~SAVP_FLAG_DELIVERING;
1137 #endif
1138
1139 /*
1140 * Restarting a system call is touchy on the HPPA, because syscall
1141 * arguments are passed in registers and the program counter of the
1142 * syscall "point" isn't easily divined.
1143 *
1144 * We handle the first problem by assuming that we will have to restart
1145 * this system call, so we stuff the first four words of the original
1146 * arguments back into the frame as arg0...arg3, which is where we
1147 * found them in the first place. Any further arguments are (still) on
1148 * the user's stack and the syscall code will fetch them from there
1149 * (again).
1150 *
1151 * The program counter problem is addressed below.
1152 */
1153 frame->tf_arg0 = args[0];
1154 frame->tf_arg1 = args[1];
1155 frame->tf_arg2 = args[2];
1156 frame->tf_arg3 = args[3];
1157
1158 /*
1159 * Some special handling for the syscall(2) and
1160 * __syscall(2) system calls.
1161 */
1162 switch (code) {
1163 case SYS_syscall:
1164 code = *args;
1165 args += 1;
1166 break;
1167 case SYS___syscall:
1168 if (callp != sysent)
1169 break;
1170 /*
1171 * NB: even though __syscall(2) takes a quad_t containing the
1172 * system call number, because our argument copying word-swaps
1173 * 64-bit arguments, the least significant word of that quad_t
1174 * is the first word in the argument array.
1175 */
1176 code = *args;
1177 args += 2;
1178 }
1179
1180 /*
1181 * Stacks growing from lower addresses to higher addresses are not
1182 * really such a good idea, because it makes it impossible to overlay a
1183 * struct on top of C stack arguments (the arguments appear in
1184 * reversed order).
1185 *
1186 * You can do the obvious thing (as locore.S does) and copy argument
1187 * words one by one, laying them out in the "right" order in the dest-
1188 * ination buffer, but this ends up word-swapping multi-word arguments
1189 * (like off_t).
1190 *
1191 * FIXME - this works only on native binaries and
1192 * will probably screw up any and all emulation.
1193 *
1194 */
1195
1196 if (code < 0 || code >= nsys)
1197 callp += p->p_emul->e_nosys; /* bad syscall # */
1198 else
1199 callp += code;
1200
1201 nargs64 = SYCALL_NARGS64(callp);
1202 if (nargs64 != 0) {
1203 size_t nargs = callp->sy_narg;
1204
1205 for (size_t i = 0; i < nargs + nargs64;) {
1206 if (SYCALL_ARG_64_P(callp, i)) {
1207 tmp = args[i];
1208 args[i] = args[i + 1];
1209 args[i + 1] = tmp;
1210 i += 2;
1211 } else
1212 i++;
1213 }
1214 }
1215
1216 #ifdef USERTRACE
1217 if (0) {
1218 user_backtrace(frame, l, -1);
1219 frame->tf_ipsw |= PSW_R;
1220 frame->tf_rctr = 0;
1221 printf("r %08x", frame->tf_iioq_head);
1222 rctr_next_iioq = frame->tf_iioq_head + 4;
1223 }
1224 #endif
1225
1226 error = 0;
1227 if (__predict_false(p->p_trace_enabled)) {
1228 error = trace_enter(code, args, callp->sy_narg);
1229 if (error)
1230 goto out;
1231 }
1232
1233 rval[0] = 0;
1234 rval[1] = 0;
1235 error = sy_call(callp, l, args, rval);
1236 out:
1237 switch (error) {
1238 case 0:
1239 l = curlwp; /* changes on exec() */
1240 frame = l->l_md.md_regs;
1241 frame->tf_ret0 = rval[0];
1242 frame->tf_ret1 = rval[1];
1243 frame->tf_t1 = 0;
1244 break;
1245 case ERESTART:
1246 /*
1247 * Now we have to wind back the instruction offset queue to the
1248 * point where the system call will be made again. This is
1249 * inherently tied to the SYSCALL macro.
1250 *
1251 * Currently, the part of the SYSCALL macro that we want to re-
1252 * run reads as:
1253 *
1254 * ldil L%SYSCALLGATE, r1
1255 * ble 4(sr7, r1)
1256 * ldi __CONCAT(SYS_,x), t1
1257 * comb,<> %r0, %t1, __cerror
1258 *
1259 * And our offset queue head points to the comb instruction.
1260 * So we need to subtract twelve to reach the ldil.
1261 */
1262 frame->tf_iioq_head -= 12;
1263 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
1264 break;
1265 case EJUSTRETURN:
1266 p = curproc;
1267 break;
1268 default:
1269 if (p->p_emul->e_errno)
1270 error = p->p_emul->e_errno[error];
1271 frame->tf_t1 = error;
1272 break;
1273 }
1274
1275 if (__predict_false(p->p_trace_enabled))
1276 trace_exit(code, rval, error);
1277
1278 userret(l, frame->tf_iioq_head, 0);
1279
1280 #ifdef DIAGNOSTIC
1281 if (cpl != oldcpl) {
1282 printf("WARNING: SPL (0x%x) NOT LOWERED ON "
1283 "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
1284 cpl, code, args[0], args[1], args[2], p->p_pid);
1285 cpl = oldcpl;
1286 }
1287 #endif
1288
1289 #ifdef DEBUG
1290 frame_sanity_check(0xdead05, 0, frame, l);
1291 #endif /* DEBUG */
1292 }
1293
1294 /*
1295 * Start a new LWP
1296 */
1297 void
1298 startlwp(void *arg)
1299 {
1300 int err;
1301 ucontext_t *uc = arg;
1302 struct lwp *l = curlwp;
1303
1304 err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
1305 #if DIAGNOSTIC
1306 if (err) {
1307 printf("Error %d from cpu_setmcontext.", err);
1308 }
1309 #endif
1310 pool_put(&lwp_uc_pool, uc);
1311
1312 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1313 }
1314
1315 /*
1316 * XXX This is a terrible name.
1317 */
1318 void
1319 upcallret(struct lwp *l)
1320 {
1321 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1322 }
1323