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