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