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