trap.c revision 1.71 1 /* $NetBSD: trap.c,v 1.71 2010/01/17 08:50:04 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.71 2010/01/17 08:50:04 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 (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;
505 int trapnum;
506 #ifdef DIAGNOSTIC
507 extern int emergency_stack_start, emergency_stack_end;
508 int oldcpl = cpl;
509 #endif
510
511 trapnum = type & ~T_USER;
512 opcode = frame->tf_iir;
513 if (trapnum == T_ITLBMISS || trapnum == T_ITLBMISSNA ||
514 trapnum == T_IBREAK || trapnum == T_TAKENBR) {
515 va = frame->tf_iioq_head;
516 space = frame->tf_iisq_head;
517 vftype = VM_PROT_EXECUTE;
518 } else {
519 va = frame->tf_ior;
520 space = frame->tf_isr;
521 vftype = inst_store(opcode) ? VM_PROT_WRITE : VM_PROT_READ;
522 }
523
524 KASSERT(curlwp != NULL);
525 l = curlwp;
526 p = l->l_proc;
527 if ((type & T_USER) != 0)
528 LWP_CACHE_CREDS(l, p);
529
530 tts = (trapnum > trap_types) ? "reserved" : trap_type[trapnum];
531
532 #ifdef DIAGNOSTIC
533 /*
534 * If we are on the emergency stack, then we either got
535 * a fault on the kernel stack, or we're just handling
536 * a trap for the machine check handler (which also
537 * runs on the emergency stack).
538 *
539 * We *very crudely* differentiate between the two cases
540 * by checking the faulting instruction: if it is the
541 * function prologue instruction that stores the old
542 * frame pointer and updates the stack pointer, we assume
543 * that we faulted on the kernel stack.
544 *
545 * In this case, not completing that instruction will
546 * probably confuse backtraces in kgdb/ddb. Completing
547 * it would be difficult, because we already faulted on
548 * that part of the stack, so instead we fix up the
549 * frame as if the function called has just returned.
550 * This has peculiar knowledge about what values are in
551 * what registers during the "normal gcc -g" prologue.
552 */
553 if (&type >= &emergency_stack_start &&
554 &type < &emergency_stack_end &&
555 type != T_IBREAK && STWM_R1_D_SR0_SP(opcode)) {
556 /* Restore the caller's frame pointer. */
557 frame->tf_r3 = frame->tf_r1;
558 /* Restore the caller's instruction offsets. */
559 frame->tf_iioq_head = frame->tf_rp;
560 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
561 goto dead_end;
562 }
563 #endif /* DIAGNOSTIC */
564
565 #ifdef DEBUG
566 frame_sanity_check(0xdead01, type, frame, l);
567 #endif /* DEBUG */
568
569 if (frame->tf_flags & TFF_LAST)
570 l->l_md.md_regs = frame;
571
572 #ifdef TRAPDEBUG
573 if (trapnum != T_INTERRUPT && trapnum != T_IBREAK)
574 printf("trap: %d, %s for %x:%x at %x:%x, fp=%p, rp=%x\n",
575 type, tts, space, (u_int)va, frame->tf_iisq_head,
576 frame->tf_iioq_head, frame, frame->tf_rp);
577 else if (trapnum == T_IBREAK)
578 printf("trap: break instruction %x:%x at %x:%x, fp=%p\n",
579 break5(opcode), break13(opcode),
580 frame->tf_iisq_head, frame->tf_iioq_head, frame);
581
582 {
583 extern int etext;
584 if (frame < (struct trapframe *)&etext) {
585 printf("trap: bogus frame ptr %p\n", frame);
586 goto dead_end;
587 }
588 }
589 #endif
590 pcb = lwp_getpcb(l);
591
592 /* If this is a trap, not an interrupt, reenable interrupts. */
593 if (trapnum != T_INTERRUPT) {
594 uvmexp.traps++;
595 mtctl(frame->tf_eiem, CR_EIEM);
596 }
597
598 switch (type) {
599 case T_NONEXIST:
600 case T_NONEXIST|T_USER:
601 #if !defined(DDB) && !defined(KGDB)
602 /* we've got screwed up by the central scrutinizer */
603 panic ("trap: elvis has just left the building!");
604 break;
605 #else
606 goto dead_end;
607 #endif
608 case T_RECOVERY|T_USER:
609 #ifdef USERTRACE
610 for(;;) {
611 if (frame->tf_iioq_head != rctr_next_iioq)
612 printf("-%08x\nr %08x",
613 rctr_next_iioq - 4,
614 frame->tf_iioq_head);
615 rctr_next_iioq = frame->tf_iioq_head + 4;
616 if (frame->tf_ipsw & PSW_N) {
617 /* Advance the program counter. */
618 frame->tf_iioq_head = frame->tf_iioq_tail;
619 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
620 /* Clear flags. */
621 frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
622 /* Simulate another trap. */
623 continue;
624 }
625 break;
626 }
627 frame->tf_rctr = 0;
628 break;
629 #endif /* USERTRACE */
630 case T_RECOVERY:
631 #if !defined(DDB) && !defined(KGDB)
632 /* XXX will implement later */
633 printf ("trap: handicapped");
634 break;
635 #else
636 goto dead_end;
637 #endif
638
639 case T_EMULATION | T_USER:
640 #ifdef FPEMUL
641 hppa_fpu_emulate(frame, l, opcode);
642 #else /* !FPEMUL */
643 /*
644 * We don't have FPU emulation, so signal the
645 * process with a SIGFPE.
646 */
647
648 KSI_INIT_TRAP(&ksi);
649 ksi.ksi_signo = SIGFPE;
650 ksi.ksi_code = SI_NOINFO;
651 ksi.ksi_trap = type;
652 ksi.ksi_addr = (void *)frame->tf_iioq_head;
653 trapsignal(l, &ksi);
654 #endif /* !FPEMUL */
655 break;
656
657 case T_DATALIGN:
658 if (pcb->pcb_onfault) {
659 do_onfault:
660 frame->tf_iioq_head = pcb->pcb_onfault;
661 frame->tf_iioq_tail = 4 + frame->tf_iioq_head;
662 pcb->pcb_onfault = 0;
663 break;
664 }
665 /*FALLTHROUGH*/
666
667 #ifdef DIAGNOSTIC
668 /* these just can't happen ever */
669 case T_PRIV_OP:
670 case T_PRIV_REG:
671 /* these just can't make it to the trap() ever */
672 case T_HPMC:
673 case T_HPMC | T_USER:
674 case T_EMULATION:
675 case T_EXCEPTION:
676 #endif
677 case T_IBREAK:
678 case T_DBREAK:
679 dead_end:
680 if (type & T_USER) {
681 #ifdef DEBUG
682 user_backtrace(frame, l, type);
683 #endif
684 KSI_INIT_TRAP(&ksi);
685 ksi.ksi_signo = SIGILL;
686 ksi.ksi_code = ILL_ILLTRP;
687 ksi.ksi_trap = type;
688 ksi.ksi_addr = (void *)frame->tf_iioq_head;
689 trapsignal(l, &ksi);
690 break;
691 }
692 if (trap_kdebug(type, va, frame))
693 return;
694 else if (type == T_DATALIGN)
695 panic ("trap: %s at 0x%x", tts, (u_int) va);
696 else
697 panic ("trap: no debugger for \"%s\" (%d)", tts, type);
698 break;
699
700 case T_IBREAK | T_USER:
701 case T_DBREAK | T_USER:
702 KSI_INIT_TRAP(&ksi);
703 ksi.ksi_signo = SIGTRAP;
704 ksi.ksi_code = TRAP_TRACE;
705 ksi.ksi_trap = trapnum;
706 ksi.ksi_addr = (void *)frame->tf_iioq_head;
707 #ifdef PTRACE
708 ss_clear_breakpoints(l);
709 if (opcode == SSBREAKPOINT)
710 ksi.ksi_code = TRAP_BRKPT;
711 #endif
712 /* pass to user debugger */
713 trapsignal(l, &ksi);
714
715 break;
716
717 #ifdef PTRACE
718 case T_TAKENBR | T_USER:
719 ss_clear_breakpoints(l);
720
721 KSI_INIT_TRAP(&ksi);
722 ksi.ksi_signo = SIGTRAP;
723 ksi.ksi_code = TRAP_TRACE;
724 ksi.ksi_trap = trapnum;
725 ksi.ksi_addr = (void *)frame->tf_iioq_head;
726
727 /* pass to user debugger */
728 trapsignal(l, &ksi);
729 break;
730 #endif
731
732 case T_EXCEPTION | T_USER: { /* co-proc assist trap */
733 uint64_t *fpp;
734 uint32_t *pex, ex, inst;
735 int i;
736
737 hppa_fpu_flush(l);
738 fpp = pcb->pcb_fpregs;
739 pex = (uint32_t *)&fpp[1];
740 for (i = 1; i < 8 && !*pex; i++, pex++)
741 ;
742 KASSERT(i < 8);
743 ex = *pex;
744 *pex = 0;
745
746 /* reset the trap flag, as if there was none */
747 fpp[0] &= ~(((uint64_t)HPPA_FPU_T) << 32);
748
749 /* emulate the instruction */
750 inst = ((uint32_t)fpopmap[ex >> 26] << 26) | (ex & 0x03ffffff);
751 hppa_fpu_emulate(frame, l, inst);
752 }
753 break;
754
755 case T_OVERFLOW | T_USER:
756 KSI_INIT_TRAP(&ksi);
757 ksi.ksi_signo = SIGFPE;
758 ksi.ksi_code = SI_NOINFO;
759 ksi.ksi_trap = type;
760 ksi.ksi_addr = (void *)va;
761 trapsignal(l, &ksi);
762 break;
763
764 case T_CONDITION | T_USER:
765 KSI_INIT_TRAP(&ksi);
766 ksi.ksi_signo = SIGFPE;
767 ksi.ksi_code = FPE_INTDIV;
768 ksi.ksi_trap = type;
769 ksi.ksi_addr = (void *)va;
770 trapsignal(l, &ksi);
771 break;
772
773 case T_ILLEGAL | T_USER:
774 #ifdef DEBUG
775 user_backtrace(frame, l, type);
776 #endif
777 KSI_INIT_TRAP(&ksi);
778 ksi.ksi_signo = SIGILL;
779 ksi.ksi_code = ILL_ILLOPC;
780 ksi.ksi_trap = type;
781 ksi.ksi_addr = (void *)va;
782 trapsignal(l, &ksi);
783 break;
784
785 case T_PRIV_OP | T_USER:
786 #ifdef DEBUG
787 user_backtrace(frame, l, type);
788 #endif
789 KSI_INIT_TRAP(&ksi);
790 ksi.ksi_signo = SIGILL;
791 ksi.ksi_code = ILL_PRVOPC;
792 ksi.ksi_trap = type;
793 ksi.ksi_addr = (void *)va;
794 trapsignal(l, &ksi);
795 break;
796
797 case T_PRIV_REG | T_USER:
798 #ifdef DEBUG
799 user_backtrace(frame, l, type);
800 #endif
801 KSI_INIT_TRAP(&ksi);
802 ksi.ksi_signo = SIGILL;
803 ksi.ksi_code = ILL_PRVREG;
804 ksi.ksi_trap = type;
805 ksi.ksi_addr = (void *)va;
806 trapsignal(l, &ksi);
807 break;
808
809 /* these should never got here */
810 case T_HIGHERPL | T_USER:
811 case T_LOWERPL | T_USER:
812 KSI_INIT_TRAP(&ksi);
813 ksi.ksi_signo = SIGSEGV;
814 ksi.ksi_code = SEGV_ACCERR;
815 ksi.ksi_trap = type;
816 ksi.ksi_addr = (void *)va;
817 trapsignal(l, &ksi);
818 break;
819
820 case T_IPROT | T_USER:
821 case T_DPROT | T_USER:
822 KSI_INIT_TRAP(&ksi);
823 ksi.ksi_signo = SIGSEGV;
824 ksi.ksi_code = SEGV_ACCERR;
825 ksi.ksi_trap = type;
826 ksi.ksi_addr = (void *)va;
827 trapsignal(l, &ksi);
828 break;
829
830 case T_DATACC: case T_USER | T_DATACC:
831 case T_ITLBMISS: case T_USER | T_ITLBMISS:
832 case T_DTLBMISS: case T_USER | T_DTLBMISS:
833 case T_ITLBMISSNA: case T_USER | T_ITLBMISSNA:
834 case T_DTLBMISSNA: case T_USER | T_DTLBMISSNA:
835 case T_TLB_DIRTY: case T_USER | T_TLB_DIRTY:
836 vm = p->p_vmspace;
837
838 if (!vm) {
839 #ifdef TRAPDEBUG
840 printf("trap: no vm, p=%p\n", p);
841 #endif
842 goto dead_end;
843 }
844
845 /*
846 * it could be a kernel map for exec_map faults
847 */
848 if (!(type & T_USER) && space == HPPA_SID_KERNEL)
849 map = kernel_map;
850 else {
851 map = &vm->vm_map;
852 if ((l->l_flag & LW_SA)
853 && (~l->l_pflag & LP_SA_NOBLOCK)) {
854 l->l_savp->savp_faultaddr = va;
855 l->l_pflag |= LP_SA_PAGEFAULT;
856 }
857 }
858
859 va = trunc_page(va);
860
861 if (map->pmap->pm_space != space) {
862 #ifdef TRAPDEBUG
863 printf("trap: space mismatch %d != %d\n",
864 space, map->pmap->pm_space);
865 #endif
866 /* actually dump the user, crap the kernel */
867 goto dead_end;
868 }
869
870 /* Never call uvm_fault in interrupt context. */
871 KASSERT(hppa_intr_depth == 0);
872
873 onfault = pcb->pcb_onfault;
874 pcb->pcb_onfault = 0;
875 ret = uvm_fault(map, va, vftype);
876 pcb->pcb_onfault = onfault;
877
878 #ifdef TRAPDEBUG
879 printf("uvm_fault(%p, %x, %d)=%d\n",
880 map, (u_int)va, vftype, ret);
881 #endif
882
883 if (map != kernel_map)
884 l->l_pflag &= ~LP_SA_PAGEFAULT;
885
886 /*
887 * If this was a stack access we keep track of the maximum
888 * accessed stack size. Also, if uvm_fault gets a protection
889 * failure it is due to accessing the stack region outside
890 * the current limit and we need to reflect that as an access
891 * error.
892 */
893 if (map != kernel_map && va >= (vaddr_t)vm->vm_minsaddr) {
894 if (ret == 0)
895 uvm_grow(l->l_proc, va);
896 else if (ret == EACCES)
897 ret = EFAULT;
898 }
899
900 if (ret != 0) {
901 if (type & T_USER) {
902 #ifdef DEBUG
903 user_backtrace(frame, l, type);
904 #endif
905 KSI_INIT_TRAP(&ksi);
906 ksi.ksi_signo = SIGSEGV;
907 ksi.ksi_code = (ret == EACCES ?
908 SEGV_ACCERR : SEGV_MAPERR);
909 ksi.ksi_trap = type;
910 ksi.ksi_addr = (void *)va;
911 trapsignal(l, &ksi);
912 } else {
913 if (pcb->pcb_onfault) {
914 goto do_onfault;
915 }
916 panic("trap: uvm_fault(%p, %lx, %d): %d",
917 map, va, vftype, ret);
918 }
919 }
920 break;
921
922 case T_DATALIGN | T_USER:
923 #ifdef DEBUG
924 user_backtrace(frame, l, type);
925 #endif
926 KSI_INIT_TRAP(&ksi);
927 ksi.ksi_signo = SIGBUS;
928 ksi.ksi_code = BUS_ADRALN;
929 ksi.ksi_trap = type;
930 ksi.ksi_addr = (void *)va;
931 trapsignal(l, &ksi);
932 break;
933
934 case T_INTERRUPT:
935 case T_INTERRUPT|T_USER:
936 hppa_intr(frame);
937 mtctl(frame->tf_eiem, CR_EIEM);
938 break;
939
940 case T_LOWERPL:
941 case T_DPROT:
942 case T_IPROT:
943 case T_OVERFLOW:
944 case T_CONDITION:
945 case T_ILLEGAL:
946 case T_HIGHERPL:
947 case T_TAKENBR:
948 case T_POWERFAIL:
949 case T_LPMC:
950 case T_PAGEREF:
951 case T_DATAPID: case T_DATAPID | T_USER:
952 if (0 /* T-chip */) {
953 break;
954 }
955 /* FALLTHROUGH to unimplemented */
956 default:
957 panic ("trap: unimplemented \'%s\' (%d)", tts, type);
958 }
959
960 #ifdef DIAGNOSTIC
961 if (cpl != oldcpl)
962 printf("WARNING: SPL (%d) NOT LOWERED ON TRAP (%d) EXIT\n",
963 cpl, trapnum);
964 #endif
965
966 if (type & T_USER)
967 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
968
969 #ifdef DEBUG
970 frame_sanity_check(0xdead02, type, frame, l);
971 if (frame->tf_flags & TFF_LAST && (curlwp->l_flag & LW_IDLE) == 0)
972 frame_sanity_check(0xdead03, type, curlwp->l_md.md_regs,
973 curlwp);
974 #endif /* DEBUG */
975 }
976
977 void
978 child_return(void *arg)
979 {
980 struct lwp *l = arg;
981
982 /*
983 * Return values in the frame set by cpu_lwp_fork().
984 */
985
986 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
987 ktrsysret(SYS_fork, 0, 0);
988 #ifdef DEBUG
989 frame_sanity_check(0xdead04, 0, l->l_md.md_regs, l);
990 #endif /* DEBUG */
991 }
992
993 #ifdef PTRACE
994
995 #include <sys/ptrace.h>
996
997 int
998 ss_get_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_READ;
1010 UIO_SETUP_SYSSPACE(&uio);
1011
1012 return (process_domem(curlwp, l, &uio));
1013 }
1014
1015 int
1016 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
1017 {
1018 struct uio uio;
1019 struct iovec iov;
1020
1021 iov.iov_base = (void *)&value;
1022 iov.iov_len = sizeof(u_int);
1023 uio.uio_iov = &iov;
1024 uio.uio_iovcnt = 1;
1025 uio.uio_offset = (off_t)addr;
1026 uio.uio_resid = sizeof(u_int);
1027 uio.uio_rw = UIO_WRITE;
1028 UIO_SETUP_SYSSPACE(&uio);
1029
1030 return (process_domem(curlwp, l, &uio));
1031 }
1032
1033 void
1034 ss_clear_breakpoints(struct lwp *l)
1035 {
1036 /* Restore origional instructions. */
1037 if (l->l_md.md_bpva != 0) {
1038 ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
1039 ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
1040 l->l_md.md_bpva = 0;
1041 }
1042 }
1043
1044
1045 int
1046 process_sstep(struct lwp *l, int sstep)
1047 {
1048 struct trapframe *tf = l->l_md.md_regs;
1049 int error;
1050
1051 ss_clear_breakpoints(l);
1052
1053 /* We're continuing... */
1054 /* Don't touch the syscall gateway page. */
1055 /* XXX head */
1056 if (sstep == 0 ||
1057 (tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE) {
1058 tf->tf_ipsw &= ~PSW_T;
1059 return 0;
1060 }
1061
1062 l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
1063
1064 /*
1065 * Insert two breakpoint instructions; the first one might be
1066 * nullified. Of course we need to save two instruction
1067 * first.
1068 */
1069
1070 error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
1071 if (error)
1072 return (error);
1073 error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
1074 if (error)
1075 return (error);
1076
1077 error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
1078 if (error)
1079 return error;
1080 error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
1081 if (error)
1082 return error;
1083
1084 tf->tf_ipsw |= PSW_T;
1085
1086 return 0;
1087 }
1088 #endif
1089
1090
1091 /*
1092 * call actual syscall routine
1093 * from the low-level syscall handler:
1094 * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
1095 * our stack, this wins compared to copyin just needed amount anyway
1096 * - register args are copied onto stack too
1097 */
1098 void
1099 syscall(struct trapframe *frame, int *args)
1100 {
1101 struct lwp *l;
1102 struct proc *p;
1103 const struct sysent *callp;
1104 size_t nargs64;
1105 int nsys, code, error;
1106 int tmp;
1107 int rval[2];
1108 #ifdef DIAGNOSTIC
1109 int oldcpl = cpl;
1110 #endif
1111
1112 uvmexp.syscalls++;
1113
1114 #ifdef DEBUG
1115 frame_sanity_check(0xdead04, 0, frame, curlwp);
1116 #endif /* DEBUG */
1117
1118 if (!USERMODE(frame->tf_iioq_head))
1119 panic("syscall");
1120
1121 KASSERT(curlwp != NULL);
1122 l = curlwp;
1123 p = l->l_proc;
1124 l->l_md.md_regs = frame;
1125 nsys = p->p_emul->e_nsysent;
1126 callp = p->p_emul->e_sysent;
1127 code = frame->tf_t1;
1128 LWP_CACHE_CREDS(l, p);
1129
1130 #ifdef KERN_SA
1131 if (__predict_false((l->l_savp)
1132 && (l->l_savp->savp_pflags & SAVP_FLAG_DELIVERING)))
1133 l->l_savp->savp_pflags &= ~SAVP_FLAG_DELIVERING;
1134 #endif
1135
1136 /*
1137 * Restarting a system call is touchy on the HPPA, because syscall
1138 * arguments are passed in registers and the program counter of the
1139 * syscall "point" isn't easily divined.
1140 *
1141 * We handle the first problem by assuming that we will have to restart
1142 * this system call, so we stuff the first four words of the original
1143 * arguments back into the frame as arg0...arg3, which is where we
1144 * found them in the first place. Any further arguments are (still) on
1145 * the user's stack and the syscall code will fetch them from there
1146 * (again).
1147 *
1148 * The program counter problem is addressed below.
1149 */
1150 frame->tf_arg0 = args[0];
1151 frame->tf_arg1 = args[1];
1152 frame->tf_arg2 = args[2];
1153 frame->tf_arg3 = args[3];
1154
1155 /*
1156 * Some special handling for the syscall(2) and
1157 * __syscall(2) system calls.
1158 */
1159 switch (code) {
1160 case SYS_syscall:
1161 code = *args;
1162 args += 1;
1163 break;
1164 case SYS___syscall:
1165 if (callp != sysent)
1166 break;
1167 /*
1168 * NB: even though __syscall(2) takes a quad_t containing the
1169 * system call number, because our argument copying word-swaps
1170 * 64-bit arguments, the least significant word of that quad_t
1171 * is the first word in the argument array.
1172 */
1173 code = *args;
1174 args += 2;
1175 }
1176
1177 /*
1178 * Stacks growing from lower addresses to higher addresses are not
1179 * really such a good idea, because it makes it impossible to overlay a
1180 * struct on top of C stack arguments (the arguments appear in
1181 * reversed order).
1182 *
1183 * You can do the obvious thing (as locore.S does) and copy argument
1184 * words one by one, laying them out in the "right" order in the dest-
1185 * ination buffer, but this ends up word-swapping multi-word arguments
1186 * (like off_t).
1187 *
1188 * FIXME - this works only on native binaries and
1189 * will probably screw up any and all emulation.
1190 *
1191 */
1192
1193 if (code < 0 || code >= nsys)
1194 callp += p->p_emul->e_nosys; /* bad syscall # */
1195 else
1196 callp += code;
1197
1198 nargs64 = SYCALL_NARGS64(callp);
1199 if (nargs64 != 0) {
1200 size_t nargs = callp->sy_narg;
1201
1202 for (size_t i = 0; i < nargs + nargs64;) {
1203 if (SYCALL_ARG_64_P(callp, i)) {
1204 tmp = args[i];
1205 args[i] = args[i + 1];
1206 args[i + 1] = tmp;
1207 i += 2;
1208 } else
1209 i++;
1210 }
1211 }
1212
1213 #ifdef USERTRACE
1214 if (0) {
1215 user_backtrace(frame, l, -1);
1216 frame->tf_ipsw |= PSW_R;
1217 frame->tf_rctr = 0;
1218 printf("r %08x", frame->tf_iioq_head);
1219 rctr_next_iioq = frame->tf_iioq_head + 4;
1220 }
1221 #endif
1222
1223 error = 0;
1224 if (__predict_false(p->p_trace_enabled)) {
1225 error = trace_enter(code, args, callp->sy_narg);
1226 if (error)
1227 goto out;
1228 }
1229
1230 rval[0] = 0;
1231 rval[1] = 0;
1232 error = sy_call(callp, l, args, rval);
1233 out:
1234 switch (error) {
1235 case 0:
1236 l = curlwp; /* changes on exec() */
1237 frame = l->l_md.md_regs;
1238 frame->tf_ret0 = rval[0];
1239 frame->tf_ret1 = rval[1];
1240 frame->tf_t1 = 0;
1241 break;
1242 case ERESTART:
1243 /*
1244 * Now we have to wind back the instruction
1245 * offset queue to the point where the system
1246 * call will be made again. This is inherently
1247 * tied to the SYSCALL macro.
1248 *
1249 * Currently, the part of the SYSCALL macro
1250 * that we want to rerun reads as:
1251 *
1252 * ldil L%SYSCALLGATE, r1
1253 * ble 4(sr7, r1)
1254 * ldi __CONCAT(SYS_,x), t1
1255 * comb,<> %r0, %t1, __cerror
1256 *
1257 * And our offset queue head points to the
1258 * comb instruction. So we need to
1259 * subtract twelve to reach the ldil.
1260 */
1261 frame->tf_iioq_head -= 12;
1262 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
1263 break;
1264 case EJUSTRETURN:
1265 p = curproc;
1266 break;
1267 default:
1268 if (p->p_emul->e_errno)
1269 error = p->p_emul->e_errno[error];
1270 frame->tf_t1 = error;
1271 break;
1272 }
1273
1274 if (__predict_false(p->p_trace_enabled))
1275 trace_exit(code, rval, error);
1276
1277 userret(l, frame->tf_iioq_head, 0);
1278
1279 #ifdef DIAGNOSTIC
1280 if (cpl != oldcpl) {
1281 printf("WARNING: SPL (0x%x) NOT LOWERED ON "
1282 "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
1283 cpl, code, args[0], args[1], args[2], p->p_pid);
1284 cpl = oldcpl;
1285 }
1286 #endif
1287
1288 #ifdef DEBUG
1289 frame_sanity_check(0xdead05, 0, frame, l);
1290 #endif /* DEBUG */
1291 }
1292
1293 /*
1294 * Start a new LWP
1295 */
1296 void
1297 startlwp(void *arg)
1298 {
1299 int err;
1300 ucontext_t *uc = arg;
1301 struct lwp *l = curlwp;
1302
1303 err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
1304 #if DIAGNOSTIC
1305 if (err) {
1306 printf("Error %d from cpu_setmcontext.", err);
1307 }
1308 #endif
1309 pool_put(&lwp_uc_pool, uc);
1310
1311 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1312 }
1313
1314 /*
1315 * XXX This is a terrible name.
1316 */
1317 void
1318 upcallret(struct lwp *l)
1319 {
1320 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1321 }
1322