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