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