Home | History | Annotate | Line # | Download | only in hppa
trap.c revision 1.96.8.3
      1 /*	$NetBSD: trap.c,v 1.96.8.3 2012/03/11 01:52:21 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.3 2012/03/11 01:52:21 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 	struct proc *p = l->l_proc;
   1002 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
   1003 	pa_space_t space = pmap->pm_space;
   1004 	struct trapframe *tf = l->l_md.md_regs;
   1005 
   1006 	/* Load all of the user's space registers. */
   1007 	tf->tf_sr0 = tf->tf_sr1 = tf->tf_sr3 = tf->tf_sr2 =
   1008 	tf->tf_sr4 = tf->tf_sr5 = tf->tf_sr6 = space;
   1009 	tf->tf_iisq_head = tf->tf_iisq_tail = space;
   1010 
   1011 	/* Load the protection registers */
   1012 	tf->tf_pidr1 = tf->tf_pidr2 = pmap->pm_pid;
   1013 
   1014 	/*
   1015 	 * theoretically these could be inherited from the father,
   1016 	 * but just in case.
   1017 	 */
   1018 	tf->tf_sr7 = HPPA_SID_KERNEL;
   1019 	mfctl(CR_EIEM, tf->tf_eiem);
   1020 	tf->tf_ipsw = PSW_C | PSW_Q | PSW_P | PSW_D | PSW_I /* | PSW_L */ |
   1021 	    (curcpu()->ci_psw & PSW_O);
   1022 
   1023 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1024 #ifdef DEBUG
   1025 	frame_sanity_check(__func__, __LINE__, 0, l->l_md.md_regs, l);
   1026 #endif /* DEBUG */
   1027 }
   1028 
   1029 #ifdef PTRACE
   1030 
   1031 #include <sys/ptrace.h>
   1032 
   1033 int
   1034 ss_get_value(struct lwp *l, vaddr_t addr, u_int *value)
   1035 {
   1036 	struct uio uio;
   1037 	struct iovec iov;
   1038 
   1039 	iov.iov_base = (void *)value;
   1040 	iov.iov_len = sizeof(u_int);
   1041 	uio.uio_iov = &iov;
   1042 	uio.uio_iovcnt = 1;
   1043 	uio.uio_offset = (off_t)addr;
   1044 	uio.uio_resid = sizeof(u_int);
   1045 	uio.uio_rw = UIO_READ;
   1046 	UIO_SETUP_SYSSPACE(&uio);
   1047 
   1048 	return (process_domem(curlwp, l, &uio));
   1049 }
   1050 
   1051 int
   1052 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
   1053 {
   1054 	struct uio uio;
   1055 	struct iovec iov;
   1056 
   1057 	iov.iov_base = (void *)&value;
   1058 	iov.iov_len = sizeof(u_int);
   1059 	uio.uio_iov = &iov;
   1060 	uio.uio_iovcnt = 1;
   1061 	uio.uio_offset = (off_t)addr;
   1062 	uio.uio_resid = sizeof(u_int);
   1063 	uio.uio_rw = UIO_WRITE;
   1064 	UIO_SETUP_SYSSPACE(&uio);
   1065 
   1066 	return (process_domem(curlwp, l, &uio));
   1067 }
   1068 
   1069 void
   1070 ss_clear_breakpoints(struct lwp *l)
   1071 {
   1072 	/* Restore origional instructions. */
   1073 	if (l->l_md.md_bpva != 0) {
   1074 		ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
   1075 		ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
   1076 		l->l_md.md_bpva = 0;
   1077 	}
   1078 }
   1079 
   1080 
   1081 int
   1082 process_sstep(struct lwp *l, int sstep)
   1083 {
   1084 	struct trapframe *tf = l->l_md.md_regs;
   1085 	int error;
   1086 
   1087 	ss_clear_breakpoints(l);
   1088 
   1089 	/* We're continuing... */
   1090 	if (sstep == 0) {
   1091 		tf->tf_ipsw &= ~PSW_T;
   1092 		return 0;
   1093 	}
   1094 
   1095 	/*
   1096 	 * Don't touch the syscall gateway page.  Instead, insert a
   1097 	 * breakpoint where we're supposed to return.
   1098 	 */
   1099 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1100 		l->l_md.md_bpva = tf->tf_r31 & ~HPPA_PC_PRIV_MASK;
   1101 	else
   1102 		l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
   1103 
   1104 	error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
   1105 	if (error)
   1106 		return error;
   1107 	error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
   1108 	if (error)
   1109 		return error;
   1110 
   1111 	error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
   1112 	if (error)
   1113 		return error;
   1114 	error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
   1115 	if (error)
   1116 		return error;
   1117 
   1118 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1119 		tf->tf_ipsw &= ~PSW_T;
   1120 	else
   1121 		tf->tf_ipsw |= PSW_T;
   1122 
   1123 	return 0;
   1124 }
   1125 #endif
   1126 
   1127 
   1128 /*
   1129  * call actual syscall routine
   1130  * from the low-level syscall handler:
   1131  * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
   1132  *   our stack, this wins compared to copyin just needed amount anyway
   1133  * - register args are copied onto stack too
   1134  */
   1135 void
   1136 syscall(struct trapframe *frame, int *args)
   1137 {
   1138 	struct lwp *l;
   1139 	struct proc *p;
   1140 	const struct sysent *callp;
   1141 	size_t nargs64;
   1142 	int nsys, code, error;
   1143 	int tmp;
   1144 	int rval[2];
   1145 #ifdef DIAGNOSTIC
   1146 	struct cpu_info *ci = curcpu();
   1147 	int oldcpl = ci->ci_cpl;
   1148 #endif
   1149 
   1150 	curcpu()->ci_data.cpu_nsyscall++;
   1151 
   1152 #ifdef DEBUG
   1153 	frame_sanity_check(__func__, __LINE__, 0, frame, curlwp);
   1154 #endif /* DEBUG */
   1155 
   1156 	if (!USERMODE(frame->tf_iioq_head))
   1157 		panic("syscall");
   1158 
   1159 	KASSERT(curlwp != NULL);
   1160 	l = curlwp;
   1161 	p = l->l_proc;
   1162 	l->l_md.md_regs = frame;
   1163 	nsys = p->p_emul->e_nsysent;
   1164 	callp = p->p_emul->e_sysent;
   1165 	code = frame->tf_t1;
   1166 	LWP_CACHE_CREDS(l, p);
   1167 
   1168 	/*
   1169 	 * Restarting a system call is touchy on the HPPA, because syscall
   1170 	 * arguments are passed in registers and the program counter of the
   1171 	 * syscall "point" isn't easily divined.
   1172 	 *
   1173 	 * We handle the first problem by assuming that we will have to restart
   1174 	 * this system call, so we stuff the first four words of the original
   1175 	 * arguments back into the frame as arg0...arg3, which is where we
   1176 	 * found them in the first place.  Any further arguments are (still) on
   1177 	 * the user's stack and the  syscall code will fetch them from there
   1178 	 * (again).
   1179 	 *
   1180 	 * The program counter problem is addressed below.
   1181 	 */
   1182 	frame->tf_arg0 = args[0];
   1183 	frame->tf_arg1 = args[1];
   1184 	frame->tf_arg2 = args[2];
   1185 	frame->tf_arg3 = args[3];
   1186 
   1187 	/*
   1188 	 * Some special handling for the syscall(2) and
   1189 	 * __syscall(2) system calls.
   1190 	 */
   1191 	switch (code) {
   1192 	case SYS_syscall:
   1193 		code = *args;
   1194 		args += 1;
   1195 		break;
   1196 	case SYS___syscall:
   1197 		if (callp != sysent)
   1198 			break;
   1199 		/*
   1200 		 * NB: even though __syscall(2) takes a quad_t containing the
   1201 		 * system call number, because our argument copying word-swaps
   1202 		 * 64-bit arguments, the least significant word of that quad_t
   1203 		 * is the first word in the argument array.
   1204 		 */
   1205 		code = *args;
   1206 		args += 2;
   1207 	}
   1208 
   1209 	/*
   1210 	 * Stacks growing from lower addresses to higher addresses are not
   1211 	 * really such a good idea, because it makes it impossible to overlay a
   1212 	 * struct on top of C stack arguments (the arguments appear in
   1213 	 * reversed order).
   1214 	 *
   1215 	 * You can do the obvious thing (as locore.S does) and copy argument
   1216 	 * words one by one, laying them out in the "right" order in the dest-
   1217 	 * ination buffer, but this ends up word-swapping multi-word arguments
   1218 	 * (like off_t).
   1219 	 *
   1220 	 * FIXME - this works only on native binaries and
   1221 	 * will probably screw up any and all emulation.
   1222 	 *
   1223 	 */
   1224 
   1225 	if (code < 0 || code >= nsys)
   1226 		callp += p->p_emul->e_nosys;	/* bad syscall # */
   1227 	else
   1228 		callp += code;
   1229 
   1230 	nargs64 = SYCALL_NARGS64(callp);
   1231 	if (nargs64 != 0) {
   1232 		size_t nargs = callp->sy_narg;
   1233 
   1234 		for (size_t i = 0; i < nargs + nargs64;) {
   1235 			if (SYCALL_ARG_64_P(callp, i)) {
   1236 				tmp = args[i];
   1237 				args[i] = args[i + 1];
   1238 				args[i + 1] = tmp;
   1239 				i += 2;
   1240 			} else
   1241 				i++;
   1242 		}
   1243 	}
   1244 
   1245 #ifdef USERTRACE
   1246 	if (0) {
   1247 		user_backtrace(frame, l, -1);
   1248 		frame->tf_ipsw |= PSW_R;
   1249 		frame->tf_rctr = 0;
   1250 		printf("r %08x", frame->tf_iioq_head);
   1251 		rctr_next_iioq = frame->tf_iioq_head + 4;
   1252 	}
   1253 #endif
   1254 
   1255 	error = 0;
   1256 	if (__predict_false(p->p_trace_enabled)) {
   1257 		error = trace_enter(code, args, callp->sy_narg);
   1258 		if (error)
   1259 			goto out;
   1260 	}
   1261 
   1262 	rval[0] = 0;
   1263 	rval[1] = 0;
   1264 	error = sy_call(callp, l, args, rval);
   1265 out:
   1266 	switch (error) {
   1267 	case 0:
   1268 		l = curlwp;			/* changes on exec() */
   1269 		frame = l->l_md.md_regs;
   1270 		frame->tf_ret0 = rval[0];
   1271 		frame->tf_ret1 = rval[1];
   1272 		frame->tf_t1 = 0;
   1273 		break;
   1274 	case ERESTART:
   1275 		/*
   1276 		 * Now we have to wind back the instruction offset queue to the
   1277 		 * point where the system call will be made again.  This is
   1278 		 * inherently tied to the SYSCALL macro.
   1279 		 *
   1280 		 * Currently, the part of the SYSCALL macro that we want to re-
   1281 		 * run reads as:
   1282 		 *
   1283 		 *	ldil	L%SYSCALLGATE, r1
   1284 		 *	ble	4(sr7, r1)
   1285 		 *	ldi	__CONCAT(SYS_,x), t1
   1286 		 *	comb,<>	%r0, %t1, __cerror
   1287 		 *
   1288 		 * And our offset queue head points to the comb instruction.
   1289 		 * So we need to subtract twelve to reach the ldil.
   1290 		 */
   1291 		frame->tf_iioq_head -= 12;
   1292 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
   1293 		break;
   1294 	case EJUSTRETURN:
   1295 		p = curproc;
   1296 		break;
   1297 	default:
   1298 		if (p->p_emul->e_errno)
   1299 			error = p->p_emul->e_errno[error];
   1300 		frame->tf_t1 = error;
   1301 		break;
   1302 	}
   1303 
   1304 	if (__predict_false(p->p_trace_enabled))
   1305 		trace_exit(code, rval, error);
   1306 
   1307 	userret(l, frame->tf_iioq_head, 0);
   1308 
   1309 #ifdef DIAGNOSTIC
   1310 	if (ci->ci_cpl != oldcpl) {
   1311 		printf("WARNING: SPL (0x%x) NOT LOWERED ON "
   1312 		    "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
   1313 		    ci->ci_cpl, code, args[0], args[1], args[2], p->p_pid);
   1314 		ci->ci_cpl = oldcpl;
   1315 	}
   1316 #endif
   1317 
   1318 #ifdef DEBUG
   1319 	frame_sanity_check(__func__, __LINE__, 0, frame, l);
   1320 #endif /* DEBUG */
   1321 }
   1322 
   1323 /*
   1324  * Start a new LWP
   1325  */
   1326 void
   1327 startlwp(void *arg)
   1328 {
   1329 	ucontext_t *uc = arg;
   1330 	lwp_t *l = curlwp;
   1331 	int error;
   1332 
   1333 	error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
   1334 	KASSERT(error == 0);
   1335 
   1336 	kmem_free(uc, sizeof(ucontext_t));
   1337 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1338 }
   1339