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