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