trap.c revision 1.69 1 /* $NetBSD: trap.c,v 1.69 2010/01/16 07:56:16 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.69 2010/01/16 07:56:16 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 trapnum;
498 #ifdef DIAGNOSTIC
499 extern int emergency_stack_start, emergency_stack_end;
500 int oldcpl = cpl;
501 #endif
502
503 trapnum = type & ~T_USER;
504 opcode = frame->tf_iir;
505 if (trapnum == T_ITLBMISS || trapnum == T_ITLBMISSNA ||
506 trapnum == T_IBREAK || trapnum == T_TAKENBR) {
507 va = frame->tf_iioq_head;
508 space = frame->tf_iisq_head;
509 vftype = VM_PROT_EXECUTE;
510 } else {
511 va = frame->tf_ior;
512 space = frame->tf_isr;
513 vftype = inst_store(opcode) ? VM_PROT_WRITE : VM_PROT_READ;
514 }
515
516 KASSERT(curlwp != NULL);
517 l = curlwp;
518 p = l->l_proc;
519 if ((type & T_USER) != 0)
520 LWP_CACHE_CREDS(l, p);
521
522 tts = (trapnum > trap_types) ? "reserved" : trap_type[trapnum];
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 (trapnum != T_INTERRUPT && trapnum != 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 (trapnum == 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 (trapnum != 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 = trapnum;
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 = trapnum;
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 #ifdef DIAGNOSTIC
953 if (cpl != oldcpl)
954 printf("WARNING: SPL (%d) NOT LOWERED ON TRAP (%d) EXIT\n",
955 cpl, trapnum);
956 #endif
957
958 if (type & T_USER)
959 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
960
961 #ifdef DEBUG
962 frame_sanity_check(0xdead02, type, frame, l);
963 if (frame->tf_flags & TFF_LAST && (curlwp->l_flag & LW_IDLE) == 0)
964 frame_sanity_check(0xdead03, type, curlwp->l_md.md_regs,
965 curlwp);
966 #endif /* DEBUG */
967 }
968
969 void
970 child_return(void *arg)
971 {
972 struct lwp *l = arg;
973
974 /*
975 * Return values in the frame set by cpu_lwp_fork().
976 */
977
978 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
979 ktrsysret(SYS_fork, 0, 0);
980 #ifdef DEBUG
981 frame_sanity_check(0xdead04, 0, l->l_md.md_regs, l);
982 #endif /* DEBUG */
983 }
984
985 #ifdef PTRACE
986
987 #include <sys/ptrace.h>
988
989 int
990 ss_get_value(struct lwp *l, vaddr_t addr, u_int *value)
991 {
992 struct uio uio;
993 struct iovec iov;
994
995 iov.iov_base = (void *)value;
996 iov.iov_len = sizeof(u_int);
997 uio.uio_iov = &iov;
998 uio.uio_iovcnt = 1;
999 uio.uio_offset = (off_t)addr;
1000 uio.uio_resid = sizeof(u_int);
1001 uio.uio_rw = UIO_READ;
1002 UIO_SETUP_SYSSPACE(&uio);
1003
1004 return (process_domem(curlwp, l, &uio));
1005 }
1006
1007 int
1008 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
1009 {
1010 struct uio uio;
1011 struct iovec iov;
1012
1013 iov.iov_base = (void *)&value;
1014 iov.iov_len = sizeof(u_int);
1015 uio.uio_iov = &iov;
1016 uio.uio_iovcnt = 1;
1017 uio.uio_offset = (off_t)addr;
1018 uio.uio_resid = sizeof(u_int);
1019 uio.uio_rw = UIO_WRITE;
1020 UIO_SETUP_SYSSPACE(&uio);
1021
1022 return (process_domem(curlwp, l, &uio));
1023 }
1024
1025 void
1026 ss_clear_breakpoints(struct lwp *l)
1027 {
1028 /* Restore origional instructions. */
1029 if (l->l_md.md_bpva != 0) {
1030 ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
1031 ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
1032 l->l_md.md_bpva = 0;
1033 }
1034 }
1035
1036
1037 int
1038 process_sstep(struct lwp *l, int sstep)
1039 {
1040 struct trapframe *tf = l->l_md.md_regs;
1041 int error;
1042
1043 ss_clear_breakpoints(l);
1044
1045 /* We're continuing... */
1046 /* Don't touch the syscall gateway page. */
1047 /* XXX head */
1048 if (sstep == 0 ||
1049 (tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE) {
1050 tf->tf_ipsw &= ~PSW_T;
1051 return 0;
1052 }
1053
1054 l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
1055
1056 /*
1057 * Insert two breakpoint instructions; the first one might be
1058 * nullified. Of course we need to save two instruction
1059 * first.
1060 */
1061
1062 error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
1063 if (error)
1064 return (error);
1065 error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
1066 if (error)
1067 return (error);
1068
1069 error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
1070 if (error)
1071 return error;
1072 error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
1073 if (error)
1074 return error;
1075
1076 tf->tf_ipsw |= PSW_T;
1077
1078 return 0;
1079 }
1080 #endif
1081
1082
1083 /*
1084 * call actual syscall routine
1085 * from the low-level syscall handler:
1086 * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
1087 * our stack, this wins compared to copyin just needed amount anyway
1088 * - register args are copied onto stack too
1089 */
1090 void
1091 syscall(struct trapframe *frame, int *args)
1092 {
1093 struct lwp *l;
1094 struct proc *p;
1095 const struct sysent *callp;
1096 size_t nargs64;
1097 int nsys, code, error;
1098 int tmp;
1099 int rval[2];
1100 #ifdef DIAGNOSTIC
1101 int oldcpl = cpl;
1102 #endif
1103
1104 uvmexp.syscalls++;
1105
1106 #ifdef DEBUG
1107 frame_sanity_check(0xdead04, 0, frame, curlwp);
1108 #endif /* DEBUG */
1109
1110 if (!USERMODE(frame->tf_iioq_head))
1111 panic("syscall");
1112
1113 KASSERT(curlwp != NULL);
1114 l = curlwp;
1115 p = l->l_proc;
1116 l->l_md.md_regs = frame;
1117 nsys = p->p_emul->e_nsysent;
1118 callp = p->p_emul->e_sysent;
1119 code = frame->tf_t1;
1120 LWP_CACHE_CREDS(l, p);
1121
1122 #ifdef KERN_SA
1123 if (__predict_false((l->l_savp)
1124 && (l->l_savp->savp_pflags & SAVP_FLAG_DELIVERING)))
1125 l->l_savp->savp_pflags &= ~SAVP_FLAG_DELIVERING;
1126 #endif
1127
1128 /*
1129 * Restarting a system call is touchy on the HPPA, because syscall
1130 * arguments are passed in registers and the program counter of the
1131 * syscall "point" isn't easily divined.
1132 *
1133 * We handle the first problem by assuming that we will have to restart
1134 * this system call, so we stuff the first four words of the original
1135 * arguments back into the frame as arg0...arg3, which is where we
1136 * found them in the first place. Any further arguments are (still) on
1137 * the user's stack and the syscall code will fetch them from there
1138 * (again).
1139 *
1140 * The program counter problem is addressed below.
1141 */
1142 frame->tf_arg0 = args[0];
1143 frame->tf_arg1 = args[1];
1144 frame->tf_arg2 = args[2];
1145 frame->tf_arg3 = args[3];
1146
1147 /*
1148 * Some special handling for the syscall(2) and
1149 * __syscall(2) system calls.
1150 */
1151 switch (code) {
1152 case SYS_syscall:
1153 code = *args;
1154 args += 1;
1155 break;
1156 case SYS___syscall:
1157 if (callp != sysent)
1158 break;
1159 /*
1160 * NB: even though __syscall(2) takes a quad_t containing the
1161 * system call number, because our argument copying word-swaps
1162 * 64-bit arguments, the least significant word of that quad_t
1163 * is the first word in the argument array.
1164 */
1165 code = *args;
1166 args += 2;
1167 }
1168
1169 /*
1170 * Stacks growing from lower addresses to higher addresses are not
1171 * really such a good idea, because it makes it impossible to overlay a
1172 * struct on top of C stack arguments (the arguments appear in
1173 * reversed order).
1174 *
1175 * You can do the obvious thing (as locore.S does) and copy argument
1176 * words one by one, laying them out in the "right" order in the dest-
1177 * ination buffer, but this ends up word-swapping multi-word arguments
1178 * (like off_t).
1179 *
1180 * FIXME - this works only on native binaries and
1181 * will probably screw up any and all emulation.
1182 *
1183 */
1184
1185 if (code < 0 || code >= nsys)
1186 callp += p->p_emul->e_nosys; /* bad syscall # */
1187 else
1188 callp += code;
1189
1190 nargs64 = SYCALL_NARGS64(callp);
1191 if (nargs64 != 0) {
1192 size_t nargs = callp->sy_narg;
1193
1194 for (size_t i = 0; i < nargs + nargs64;) {
1195 if (SYCALL_ARG_64_P(callp, i)) {
1196 tmp = args[i];
1197 args[i] = args[i + 1];
1198 args[i + 1] = tmp;
1199 i += 2;
1200 } else
1201 i++;
1202 }
1203 }
1204
1205 #ifdef USERTRACE
1206 if (0) {
1207 user_backtrace(frame, l, -1);
1208 frame->tf_ipsw |= PSW_R;
1209 frame->tf_rctr = 0;
1210 printf("r %08x", frame->tf_iioq_head);
1211 rctr_next_iioq = frame->tf_iioq_head + 4;
1212 }
1213 #endif
1214
1215 error = 0;
1216 if (__predict_false(p->p_trace_enabled)) {
1217 error = trace_enter(code, args, callp->sy_narg);
1218 if (error)
1219 goto out;
1220 }
1221
1222 rval[0] = 0;
1223 rval[1] = 0;
1224 error = sy_call(callp, l, args, rval);
1225 out:
1226 switch (error) {
1227 case 0:
1228 l = curlwp; /* changes on exec() */
1229 frame = l->l_md.md_regs;
1230 frame->tf_ret0 = rval[0];
1231 frame->tf_ret1 = rval[1];
1232 frame->tf_t1 = 0;
1233 break;
1234 case ERESTART:
1235 /*
1236 * Now we have to wind back the instruction
1237 * offset queue to the point where the system
1238 * call will be made again. This is inherently
1239 * tied to the SYSCALL macro.
1240 *
1241 * Currently, the part of the SYSCALL macro
1242 * that we want to rerun reads as:
1243 *
1244 * ldil L%SYSCALLGATE, r1
1245 * ble 4(sr7, r1)
1246 * ldi __CONCAT(SYS_,x), t1
1247 * comb,<> %r0, %t1, __cerror
1248 *
1249 * And our offset queue head points to the
1250 * comb instruction. So we need to
1251 * subtract twelve to reach the ldil.
1252 */
1253 frame->tf_iioq_head -= 12;
1254 frame->tf_iioq_tail = frame->tf_iioq_head + 4;
1255 break;
1256 case EJUSTRETURN:
1257 p = curproc;
1258 break;
1259 default:
1260 if (p->p_emul->e_errno)
1261 error = p->p_emul->e_errno[error];
1262 frame->tf_t1 = error;
1263 break;
1264 }
1265
1266 if (__predict_false(p->p_trace_enabled))
1267 trace_exit(code, rval, error);
1268
1269 userret(l, frame->tf_iioq_head, 0);
1270
1271 #ifdef DIAGNOSTIC
1272 if (cpl != oldcpl) {
1273 printf("WARNING: SPL (0x%x) NOT LOWERED ON "
1274 "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
1275 cpl, code, args[0], args[1], args[2], p->p_pid);
1276 cpl = oldcpl;
1277 }
1278 #endif
1279
1280 #ifdef DEBUG
1281 frame_sanity_check(0xdead05, 0, frame, l);
1282 #endif /* DEBUG */
1283 }
1284
1285 /*
1286 * Start a new LWP
1287 */
1288 void
1289 startlwp(void *arg)
1290 {
1291 int err;
1292 ucontext_t *uc = arg;
1293 struct lwp *l = curlwp;
1294
1295 err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
1296 #if DIAGNOSTIC
1297 if (err) {
1298 printf("Error %d from cpu_setmcontext.", err);
1299 }
1300 #endif
1301 pool_put(&lwp_uc_pool, uc);
1302
1303 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1304 }
1305
1306 /*
1307 * XXX This is a terrible name.
1308 */
1309 void
1310 upcallret(struct lwp *l)
1311 {
1312 userret(l, l->l_md.md_regs->tf_iioq_head, 0);
1313 }
1314