Home | History | Annotate | Line # | Download | only in hppa
trap.c revision 1.79
      1 /*	$NetBSD: trap.c,v 1.79 2010/03/16 16:20:19 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matthew Fredette.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*	$OpenBSD: trap.c,v 1.30 2001/09/19 20:50:56 mickey Exp $	*/
     33 
     34 /*
     35  * Copyright (c) 1998-2004 Michael Shalayeff
     36  * All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     51  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     52  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     53  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     55  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     56  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     57  * THE POSSIBILITY OF SUCH DAMAGE.
     58  */
     59 
     60 #include <sys/cdefs.h>
     61 __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.79 2010/03/16 16:20:19 skrll Exp $");
     62 
     63 /* #define INTRDEBUG */
     64 /* #define TRAPDEBUG */
     65 /* #define USERTRACE */
     66 
     67 #include "opt_kgdb.h"
     68 #include "opt_ptrace.h"
     69 #include "opt_sa.h"
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/kernel.h>
     74 #include <sys/syscall.h>
     75 #include <sys/syscallvar.h>
     76 #include <sys/sa.h>
     77 #include <sys/savar.h>
     78 #include <sys/mutex.h>
     79 #include <sys/ktrace.h>
     80 #include <sys/proc.h>
     81 #include <sys/signalvar.h>
     82 #include <sys/acct.h>
     83 #include <sys/signal.h>
     84 #include <sys/device.h>
     85 #include <sys/pool.h>
     86 #include <sys/userret.h>
     87 
     88 #include <net/netisr.h>
     89 
     90 #ifdef KGDB
     91 #include <sys/kgdb.h>
     92 #endif
     93 
     94 #include <uvm/uvm.h>
     95 
     96 #include <machine/iomod.h>
     97 #include <machine/cpufunc.h>
     98 #include <machine/reg.h>
     99 #include <machine/autoconf.h>
    100 
    101 #include <machine/db_machdep.h>
    102 
    103 #include <hppa/hppa/machdep.h>
    104 
    105 #include <ddb/db_output.h>
    106 #include <ddb/db_interface.h>
    107 
    108 #ifdef PTRACE
    109 void ss_clear_breakpoints(struct lwp *l);
    110 int ss_put_value(struct lwp *, vaddr_t, u_int);
    111 int ss_get_value(struct lwp *, vaddr_t, u_int *);
    112 #endif
    113 
    114 /* single-step breakpoint */
    115 #define SSBREAKPOINT   (HPPA_BREAK_KERNEL | (HPPA_BREAK_SS << 13))
    116 
    117 #if defined(DEBUG) || defined(DIAGNOSTIC)
    118 /*
    119  * 0x6fc1000 is a stwm r1, d(sr0, sp), which is the last
    120  * instruction in the function prologue that gcc -O0 uses.
    121  * When we have this instruction we know the relationship
    122  * between the stack pointer and the gcc -O0 frame pointer
    123  * (in r3, loaded with the initial sp) for the body of a
    124  * function.
    125  *
    126  * If the given instruction is a stwm r1, d(sr0, sp) where
    127  * d > 0, we evaluate to d, else we evaluate to zero.
    128  */
    129 #define STWM_R1_D_SR0_SP(inst) \
    130 	(((inst) & 0xffffc001) == 0x6fc10000 ? (((inst) & 0x00003ff) >> 1) : 0)
    131 #endif /* DEBUG || DIAGNOSTIC */
    132 
    133 const char *trap_type[] = {
    134 	"invalid",
    135 	"HPMC",
    136 	"power failure",
    137 	"recovery counter",
    138 	"external interrupt",
    139 	"LPMC",
    140 	"ITLB miss fault",
    141 	"instruction protection",
    142 	"Illegal instruction",
    143 	"break instruction",
    144 	"privileged operation",
    145 	"privileged register",
    146 	"overflow",
    147 	"conditional",
    148 	"assist exception",
    149 	"DTLB miss",
    150 	"ITLB non-access miss",
    151 	"DTLB non-access miss",
    152 	"data protection/rights/alignment",
    153 	"data break",
    154 	"TLB dirty",
    155 	"page reference",
    156 	"assist emulation",
    157 	"higher-priv transfer",
    158 	"lower-priv transfer",
    159 	"taken branch",
    160 	"data access rights",
    161 	"data protection",
    162 	"unaligned data ref",
    163 };
    164 int trap_types = __arraycount(trap_type);
    165 
    166 uint8_t fpopmap[] = {
    167 	0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
    168 	0x00, 0x0c, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00,
    169 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    170 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    171 	0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
    172 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    173 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    174 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    175 };
    176 
    177 volatile int astpending;
    178 
    179 void pmap_hptdump(void);
    180 void syscall(struct trapframe *, int *);
    181 
    182 #if defined(DEBUG)
    183 struct trapframe *sanity_frame;
    184 struct lwp *sanity_lwp;
    185 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, but the offset
    269 		 * queue tail didn't, assume that the user wants to jump to the
    270 		 * head offset, and adjust the tail accordingly.  This should
    271 		 * fix the kgdb `jump' command, and can help DDB users who `set'
    272 		 * the offset head but forget the tail.
    273 		 */
    274 		if (frame->tf_iioq_head != tf_iioq_head_old &&
    275 		    frame->tf_iioq_tail == tf_iioq_tail_old)
    276 			frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    277 
    278 		/*
    279 		 * This is some single-stepping support.  If we're trying to
    280 		 * step through a nullified instruction, just advance by hand
    281 		 * and trap again.  Otherwise, load the recovery counter with
    282 		 * zero.
    283 		 */
    284 		if (frame->tf_ipsw & PSW_R) {
    285 #ifdef TRAPDEBUG
    286 			printf("(single stepping at head 0x%x tail 0x%x)\n",
    287 			    frame->tf_iioq_head, frame->tf_iioq_tail);
    288 #endif
    289 			if (frame->tf_ipsw & PSW_N) {
    290 #ifdef TRAPDEBUG
    291 				printf("(single stepping past nullified)\n");
    292 #endif
    293 
    294 				/* Advance the program counter. */
    295 				frame->tf_iioq_head = frame->tf_iioq_tail;
    296 				frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    297 
    298 				/* Clear flags. */
    299 				frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
    300 
    301 				/* Simulate another trap. */
    302 				type = T_RECOVERY;
    303 				continue;
    304 			}
    305 			frame->tf_rctr = 0;
    306 		}
    307 
    308 		/* We handled this trap. */
    309 		return (1);
    310 	}
    311 	/* NOTREACHED */
    312 }
    313 #else	/* !KGDB && !DDB */
    314 #define trap_kdebug(t, c, f)	(0)
    315 #endif	/* !KGDB && !DDB */
    316 
    317 #if defined(DEBUG) || defined(USERTRACE)
    318 /*
    319  * These functions give a crude usermode backtrace.  They really only work when
    320  * code has been compiled without optimization, as they assume a certain func-
    321  * tion prologue sets up a frame pointer and stores the return pointer and arg-
    322  * uments in it.
    323  */
    324 static void
    325 user_backtrace_raw(u_int pc, u_int fp)
    326 {
    327 	int frame_number;
    328 	int arg_number;
    329 
    330 	for (frame_number = 0;
    331 	     frame_number < 100 && pc > HPPA_PC_PRIV_MASK && fp;
    332 	     frame_number++) {
    333 
    334 		printf("%3d: pc=%08x%s fp=0x%08x", frame_number,
    335 		    pc & ~HPPA_PC_PRIV_MASK, USERMODE(pc) ? "  " : "**", fp);
    336 		for(arg_number = 0; arg_number < 4; arg_number++)
    337 			printf(" arg%d=0x%08x", arg_number,
    338 			    (int) fuword(HPPA_FRAME_CARG(arg_number, fp)));
    339 		printf("\n");
    340                 pc = fuword(((register_t *) fp) - 5);	/* fetch rp */
    341 		if (pc == -1) {
    342 			printf("  fuword for pc failed\n");
    343 			break;
    344 		}
    345                 fp = fuword(((register_t *) fp) + 0);	/* fetch previous fp */
    346 		if (fp == -1) {
    347 			printf("  fuword for fp failed\n");
    348 			break;
    349 		}
    350 	}
    351 	printf("  backtrace stopped with pc %08x fp 0x%08x\n", pc, fp);
    352 }
    353 
    354 static void
    355 user_backtrace(struct trapframe *tf, struct lwp *l, int type)
    356 {
    357 	struct proc *p = l->l_proc;
    358 	u_int pc, fp, inst;
    359 
    360 	/*
    361 	 * Display any trap type that we have.
    362 	 */
    363 	if (type >= 0)
    364 		printf("pid %d (%s) trap #%d\n",
    365 		    p->p_pid, p->p_comm, type & ~T_USER);
    366 
    367 	/*
    368 	 * Assuming that the frame pointer in r3 is valid,
    369 	 * dump out a stack trace.
    370 	 */
    371 	fp = tf->tf_r3;
    372 	printf("pid %d (%s) backtrace, starting with fp 0x%08x\n",
    373 		p->p_pid, p->p_comm, fp);
    374 	user_backtrace_raw(tf->tf_iioq_head, fp);
    375 
    376 	/*
    377 	 * In case the frame pointer in r3 is not valid,
    378 	 * assuming the stack pointer is valid and the
    379 	 * faulting function is a non-leaf, if we can
    380 	 * find its prologue we can recover its frame
    381 	 * pointer.
    382 	 */
    383 	pc = tf->tf_iioq_head;
    384 	fp = tf->tf_sp - HPPA_FRAME_SIZE;
    385 	printf("pid %d (%s) backtrace, starting with sp 0x%08x pc 0x%08x\n",
    386 		p->p_pid, p->p_comm, tf->tf_sp, pc);
    387 	for (pc &= ~HPPA_PC_PRIV_MASK; pc > 0; pc -= sizeof(inst)) {
    388 		inst = fuword((register_t *) pc);
    389 		if (inst == -1) {
    390 			printf("  fuword for inst at pc %08x failed\n", pc);
    391 			break;
    392 		}
    393 		/* Check for the prologue instruction that sets sp. */
    394 		if (STWM_R1_D_SR0_SP(inst)) {
    395 			fp = tf->tf_sp - STWM_R1_D_SR0_SP(inst);
    396 			printf("  sp from fp at pc %08x: %08x\n", pc, inst);
    397 			break;
    398 		}
    399 	}
    400 	user_backtrace_raw(tf->tf_iioq_head, fp);
    401 }
    402 #endif /* DEBUG || USERTRACE */
    403 
    404 #ifdef DEBUG
    405 /*
    406  * This sanity-checks a trapframe.  It is full of various
    407  * assumptions about what a healthy CPU state should be,
    408  * with some documented elsewhere, some not.
    409  */
    410 void
    411 frame_sanity_check(int where, int type, struct trapframe *tf, struct lwp *l)
    412 {
    413 	extern int kernel_text;
    414 	extern int etext;
    415 	extern register_t kpsw;
    416 
    417 #define SANITY(e)					\
    418 do {							\
    419 	if (sanity_frame == NULL && !(e)) {		\
    420 		sanity_frame = tf;			\
    421 		sanity_lwp = l;				\
    422 		sanity_checked = __LINE__;		\
    423 	}						\
    424 } while (/* CONSTCOND */ 0)
    425 
    426 	KASSERT(l != NULL);
    427 	SANITY((tf->tf_ipsw & kpsw) == kpsw);
    428 	SANITY((kpsw & PSW_I) == 0 || tf->tf_eiem != 0);
    429 	if (tf->tf_iisq_head == HPPA_SID_KERNEL) {
    430 		vaddr_t minsp, maxsp, uv;
    431 
    432 		uv = uvm_lwp_getuarea(l);
    433 
    434 		/*
    435 		 * If the trap happened in the gateway
    436 		 * page, we take the easy way out and
    437 		 * assume that the trapframe is okay.
    438 		 */
    439 		if ((tf->tf_iioq_head & ~PAGE_MASK) == SYSCALLGATE)
    440 			goto out;
    441 
    442 		SANITY(!USERMODE(tf->tf_iioq_head));
    443 		SANITY(!USERMODE(tf->tf_iioq_tail));
    444 
    445 		/*
    446 		 * Don't check the instruction queues or stack on interrupts
    447 		 * as we could be be in the sti code (outside normal kernel
    448 		 * text) or switching LWPs (curlwp and sp are not in sync)
    449 		 */
    450 		if ((type & ~T_USER) == T_INTERRUPT)
    451 			goto out;
    452 
    453 		SANITY(tf->tf_iioq_head >= (u_int) &kernel_text);
    454 		SANITY(tf->tf_iioq_head < (u_int) &etext);
    455 		SANITY(tf->tf_iioq_tail >= (u_int) &kernel_text);
    456 		SANITY(tf->tf_iioq_tail < (u_int) &etext);
    457 
    458 		maxsp = uv + USPACE + PAGE_SIZE;
    459 		minsp = uv + PAGE_SIZE;
    460 
    461 		SANITY(tf->tf_sp >= minsp && tf->tf_sp < maxsp);
    462 	} else {
    463 		struct pcb *pcb = lwp_getpcb(l);
    464 
    465 		SANITY(USERMODE(tf->tf_iioq_head));
    466 		SANITY(USERMODE(tf->tf_iioq_tail));
    467 		SANITY(tf->tf_cr30 == (u_int)pcb->pcb_fpregs);
    468 	}
    469 #undef SANITY
    470 out:
    471 	if (sanity_frame == tf) {
    472 		printf("insanity: where 0x%x type 0x%x tf %p lwp %p line %d "
    473 		       "sp 0x%x pc 0x%x\n",
    474 		       where, type, sanity_frame, sanity_lwp, sanity_checked,
    475 		       tf->tf_sp, tf->tf_iioq_head);
    476 		(void) trap_kdebug(T_IBREAK, 0, tf);
    477 		sanity_frame = NULL;
    478 		sanity_lwp = NULL;
    479 		sanity_checked = 0;
    480 	}
    481 }
    482 #endif /* DEBUG */
    483 
    484 void
    485 trap(int type, struct trapframe *frame)
    486 {
    487 	struct lwp *l;
    488 	struct proc *p;
    489 	struct pcb *pcb;
    490 	vaddr_t va;
    491 	struct vm_map *map;
    492 	struct vmspace *vm;
    493 	vm_prot_t vftype;
    494 	pa_space_t space;
    495 	ksiginfo_t ksi;
    496 	u_int opcode, onfault;
    497 	int ret;
    498 	const char *tts = "reserved";
    499 	int trapnum;
    500 #ifdef DIAGNOSTIC
    501 	extern int emergency_stack_start, emergency_stack_end;
    502 	int oldcpl = cpl;
    503 #endif
    504 
    505 	trapnum = type & ~T_USER;
    506 	opcode = frame->tf_iir;
    507 	if (trapnum == T_ITLBMISS || trapnum == T_ITLBMISSNA ||
    508 	    trapnum == T_IBREAK || trapnum == T_TAKENBR) {
    509 		va = frame->tf_iioq_head;
    510 		space = frame->tf_iisq_head;
    511 		vftype = VM_PROT_EXECUTE;
    512 	} else {
    513 		va = frame->tf_ior;
    514 		space = frame->tf_isr;
    515 		vftype = inst_store(opcode) ? VM_PROT_WRITE : VM_PROT_READ;
    516 	}
    517 
    518 	KASSERT(curlwp != NULL);
    519 	l = curlwp;
    520 	p = l->l_proc;
    521 	if ((type & T_USER) != 0)
    522 		LWP_CACHE_CREDS(l, p);
    523 
    524 #ifdef DIAGNOSTIC
    525 	/*
    526 	 * If we are on the emergency stack, then we either got
    527 	 * a fault on the kernel stack, or we're just handling
    528 	 * a trap for the machine check handler (which also
    529 	 * runs on the emergency stack).
    530 	 *
    531 	 * We *very crudely* differentiate between the two cases
    532 	 * by checking the faulting instruction: if it is the
    533 	 * function prologue instruction that stores the old
    534 	 * frame pointer and updates the stack pointer, we assume
    535 	 * that we faulted on the kernel stack.
    536 	 *
    537 	 * In this case, not completing that instruction will
    538 	 * probably confuse backtraces in kgdb/ddb.  Completing
    539 	 * it would be difficult, because we already faulted on
    540 	 * that part of the stack, so instead we fix up the
    541 	 * frame as if the function called has just returned.
    542 	 * This has peculiar knowledge about what values are in
    543 	 * what registers during the "normal gcc -g" prologue.
    544 	 */
    545 	if (&type >= &emergency_stack_start &&
    546 	    &type < &emergency_stack_end &&
    547 	    type != T_IBREAK && STWM_R1_D_SR0_SP(opcode)) {
    548 		/* Restore the caller's frame pointer. */
    549 		frame->tf_r3 = frame->tf_r1;
    550 		/* Restore the caller's instruction offsets. */
    551 		frame->tf_iioq_head = frame->tf_rp;
    552 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    553 		goto dead_end;
    554 	}
    555 #endif /* DIAGNOSTIC */
    556 
    557 #ifdef DEBUG
    558 	frame_sanity_check(0xdead01, type, frame, l);
    559 #endif /* DEBUG */
    560 
    561 	if (frame->tf_flags & TFF_LAST)
    562 		l->l_md.md_regs = frame;
    563 
    564 	if (trapnum <= trap_types)
    565 		tts = trap_type[trapnum];
    566 
    567 #ifdef TRAPDEBUG
    568 	if (trapnum != T_INTERRUPT && trapnum != T_IBREAK)
    569 		printf("trap: %d, %s for %x:%lx at %x:%x, fp=%p, rp=%x\n",
    570 		    type, tts, space, va, frame->tf_iisq_head,
    571 		    frame->tf_iioq_head, frame, frame->tf_rp);
    572 	else if (trapnum == T_IBREAK)
    573 		printf("trap: break instruction %x:%x at %x:%x, fp=%p\n",
    574 		    break5(opcode), break13(opcode),
    575 		    frame->tf_iisq_head, frame->tf_iioq_head, frame);
    576 
    577 	{
    578 		extern int etext;
    579 		if (frame < (struct trapframe *)&etext) {
    580 			printf("trap: bogus frame ptr %p\n", frame);
    581 			goto dead_end;
    582 		}
    583 	}
    584 #endif
    585 
    586 	pcb = lwp_getpcb(l);
    587 
    588 	/* If this is a trap, not an interrupt, reenable interrupts. */
    589 	if (trapnum != T_INTERRUPT) {
    590 		uvmexp.traps++;
    591 		mtctl(frame->tf_eiem, CR_EIEM);
    592 	}
    593 
    594 	switch (type) {
    595 	case T_NONEXIST:
    596 	case T_NONEXIST|T_USER:
    597 #if !defined(DDB) && !defined(KGDB)
    598 		/* we've got screwed up by the central scrutinizer */
    599 		panic ("trap: elvis has just left the building!");
    600 		break;
    601 #else
    602 		goto dead_end;
    603 #endif
    604 	case T_RECOVERY|T_USER:
    605 #ifdef USERTRACE
    606 		for(;;) {
    607 			if (frame->tf_iioq_head != rctr_next_iioq)
    608 				printf("-%08x\nr %08x",
    609 					rctr_next_iioq - 4,
    610 					frame->tf_iioq_head);
    611 			rctr_next_iioq = frame->tf_iioq_head + 4;
    612 			if (frame->tf_ipsw & PSW_N) {
    613 				/* Advance the program counter. */
    614 				frame->tf_iioq_head = frame->tf_iioq_tail;
    615 				frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    616 				/* Clear flags. */
    617 				frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
    618 				/* Simulate another trap. */
    619 				continue;
    620 			}
    621 			break;
    622 		}
    623 		frame->tf_rctr = 0;
    624 		break;
    625 #endif /* USERTRACE */
    626 	case T_RECOVERY:
    627 #if !defined(DDB) && !defined(KGDB)
    628 		/* XXX will implement later */
    629 		printf ("trap: handicapped");
    630 		break;
    631 #else
    632 		goto dead_end;
    633 #endif
    634 
    635 	case T_EMULATION | T_USER:
    636 #ifdef FPEMUL
    637 		hppa_fpu_emulate(frame, l, opcode);
    638 #else  /* !FPEMUL */
    639 		/*
    640 		 * We don't have FPU emulation, so signal the
    641 		 * process with a SIGFPE.
    642 		 */
    643 
    644 		KSI_INIT_TRAP(&ksi);
    645 		ksi.ksi_signo = SIGFPE;
    646 		ksi.ksi_code = SI_NOINFO;
    647 		ksi.ksi_trap = type;
    648 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    649 		trapsignal(l, &ksi);
    650 #endif /* !FPEMUL */
    651 		break;
    652 
    653 	case T_DATALIGN:
    654 		if (pcb->pcb_onfault) {
    655 do_onfault:
    656 			frame->tf_iioq_head = pcb->pcb_onfault;
    657 			frame->tf_iioq_tail = 4 + frame->tf_iioq_head;
    658 			pcb->pcb_onfault = 0;
    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 			if ((l->l_flag & LW_SA)
    854 			    && (~l->l_pflag & LP_SA_NOBLOCK)) {
    855 				l->l_savp->savp_faultaddr = va;
    856 				l->l_pflag |= LP_SA_PAGEFAULT;
    857 			}
    858 		}
    859 
    860 		va = trunc_page(va);
    861 
    862 		if (map->pmap->pm_space != space) {
    863 #ifdef TRAPDEBUG
    864 			printf("trap: space mismatch %d != %d\n",
    865 			    space, map->pmap->pm_space);
    866 #endif
    867 			/* actually dump the user, crap the kernel */
    868 			goto dead_end;
    869 		}
    870 
    871 		/* Never call uvm_fault in interrupt context. */
    872 		KASSERT(hppa_intr_depth == 0);
    873 
    874 		onfault = pcb->pcb_onfault;
    875 		pcb->pcb_onfault = 0;
    876 		ret = uvm_fault(map, va, vftype);
    877 		pcb->pcb_onfault = onfault;
    878 
    879 #ifdef TRAPDEBUG
    880 		printf("uvm_fault(%p, %x, %d)=%d\n",
    881 		    map, (u_int)va, vftype, ret);
    882 #endif
    883 
    884 		if (map != kernel_map)
    885 			l->l_pflag &= ~LP_SA_PAGEFAULT;
    886 
    887 		/*
    888 		 * If this was a stack access we keep track of the maximum
    889 		 * accessed stack size.  Also, if uvm_fault gets a protection
    890 		 * failure it is due to accessing the stack region outside
    891 		 * the current limit and we need to reflect that as an access
    892 		 * error.
    893 		 */
    894 		if (map != kernel_map && va >= (vaddr_t)vm->vm_minsaddr) {
    895 			if (ret == 0)
    896 				uvm_grow(l->l_proc, va);
    897 			else if (ret == EACCES)
    898 				ret = EFAULT;
    899 		}
    900 
    901 		if (ret != 0) {
    902 			if (type & T_USER) {
    903 #ifdef DEBUG
    904 				user_backtrace(frame, l, type);
    905 #endif
    906 				KSI_INIT_TRAP(&ksi);
    907 				ksi.ksi_signo = SIGSEGV;
    908 				ksi.ksi_code = (ret == EACCES ?
    909 						SEGV_ACCERR : SEGV_MAPERR);
    910 				ksi.ksi_trap = type;
    911 				ksi.ksi_addr = (void *)va;
    912 				trapsignal(l, &ksi);
    913 			} else {
    914 				if (pcb->pcb_onfault) {
    915 					goto do_onfault;
    916 				}
    917 				panic("trap: uvm_fault(%p, %lx, %d): %d",
    918 				    map, va, vftype, ret);
    919 			}
    920 		}
    921 		break;
    922 
    923 	case T_DATALIGN | T_USER:
    924 #ifdef DEBUG
    925 		user_backtrace(frame, l, type);
    926 #endif
    927 		KSI_INIT_TRAP(&ksi);
    928 		ksi.ksi_signo = SIGBUS;
    929 		ksi.ksi_code = BUS_ADRALN;
    930 		ksi.ksi_trap = type;
    931 		ksi.ksi_addr = (void *)va;
    932 		trapsignal(l, &ksi);
    933 		break;
    934 
    935 	case T_INTERRUPT:
    936 	case T_INTERRUPT|T_USER:
    937 		hppa_intr(frame);
    938 		mtctl(frame->tf_eiem, CR_EIEM);
    939 		break;
    940 
    941 	case T_LOWERPL:
    942 	case T_DPROT:
    943 	case T_IPROT:
    944 	case T_OVERFLOW:
    945 	case T_CONDITION:
    946 	case T_ILLEGAL:
    947 	case T_HIGHERPL:
    948 	case T_TAKENBR:
    949 	case T_POWERFAIL:
    950 	case T_LPMC:
    951 	case T_PAGEREF:
    952 	case T_DATAPID:  	case T_DATAPID  | T_USER:
    953 		if (0 /* T-chip */) {
    954 			break;
    955 		}
    956 		/* FALLTHROUGH to unimplemented */
    957 	default:
    958 		panic ("trap: unimplemented \'%s\' (%d)", tts, type);
    959 	}
    960 
    961 #ifdef DIAGNOSTIC
    962 	if (cpl != oldcpl)
    963 		printf("WARNING: SPL (%d) NOT LOWERED ON TRAP (%d) EXIT\n",
    964 		    cpl, trapnum);
    965 #endif
    966 
    967 	if (type & T_USER)
    968 		userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    969 
    970 #ifdef DEBUG
    971 	frame_sanity_check(0xdead02, type, frame, l);
    972 	if (frame->tf_flags & TFF_LAST && (curlwp->l_flag & LW_IDLE) == 0)
    973 		frame_sanity_check(0xdead03, type, curlwp->l_md.md_regs,
    974 				   curlwp);
    975 #endif /* DEBUG */
    976 }
    977 
    978 void
    979 child_return(void *arg)
    980 {
    981 	struct lwp *l = arg;
    982 
    983 	/*
    984 	 * Return values in the frame set by cpu_lwp_fork().
    985 	 */
    986 
    987 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    988 	ktrsysret(SYS_fork, 0, 0);
    989 #ifdef DEBUG
    990 	frame_sanity_check(0xdead04, 0, l->l_md.md_regs, l);
    991 #endif /* DEBUG */
    992 }
    993 
    994 #ifdef PTRACE
    995 
    996 #include <sys/ptrace.h>
    997 
    998 int
    999 ss_get_value(struct lwp *l, vaddr_t addr, u_int *value)
   1000 {
   1001 	struct uio uio;
   1002 	struct iovec iov;
   1003 
   1004 	iov.iov_base = (void *)value;
   1005 	iov.iov_len = sizeof(u_int);
   1006 	uio.uio_iov = &iov;
   1007 	uio.uio_iovcnt = 1;
   1008 	uio.uio_offset = (off_t)addr;
   1009 	uio.uio_resid = sizeof(u_int);
   1010 	uio.uio_rw = UIO_READ;
   1011 	UIO_SETUP_SYSSPACE(&uio);
   1012 
   1013 	return (process_domem(curlwp, l, &uio));
   1014 }
   1015 
   1016 int
   1017 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
   1018 {
   1019 	struct uio uio;
   1020 	struct iovec iov;
   1021 
   1022 	iov.iov_base = (void *)&value;
   1023 	iov.iov_len = sizeof(u_int);
   1024 	uio.uio_iov = &iov;
   1025 	uio.uio_iovcnt = 1;
   1026 	uio.uio_offset = (off_t)addr;
   1027 	uio.uio_resid = sizeof(u_int);
   1028 	uio.uio_rw = UIO_WRITE;
   1029 	UIO_SETUP_SYSSPACE(&uio);
   1030 
   1031 	return (process_domem(curlwp, l, &uio));
   1032 }
   1033 
   1034 void
   1035 ss_clear_breakpoints(struct lwp *l)
   1036 {
   1037 	/* Restore origional instructions. */
   1038 	if (l->l_md.md_bpva != 0) {
   1039 		ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
   1040 		ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
   1041 		l->l_md.md_bpva = 0;
   1042 	}
   1043 }
   1044 
   1045 
   1046 int
   1047 process_sstep(struct lwp *l, int sstep)
   1048 {
   1049 	struct trapframe *tf = l->l_md.md_regs;
   1050 	int error;
   1051 
   1052 	ss_clear_breakpoints(l);
   1053 
   1054 	/* We're continuing... */
   1055 	/* Don't touch the syscall gateway page. */
   1056 	/* XXX head */
   1057 	if (sstep == 0 ||
   1058 	    (tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE) {
   1059 		tf->tf_ipsw &= ~PSW_T;
   1060 		return 0;
   1061 	}
   1062 
   1063 	l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
   1064 
   1065 	/*
   1066 	 * Insert two breakpoint instructions; the first one might be
   1067 	 * nullified.  Of course we need to save two instruction
   1068 	 * first.
   1069 	 */
   1070 
   1071 	error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
   1072 	if (error)
   1073 		return (error);
   1074 	error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
   1075 	if (error)
   1076 		return (error);
   1077 
   1078 	error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
   1079 	if (error)
   1080 		return error;
   1081 	error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
   1082 	if (error)
   1083 		return error;
   1084 
   1085 	tf->tf_ipsw |= PSW_T;
   1086 
   1087 	return 0;
   1088 }
   1089 #endif
   1090 
   1091 
   1092 /*
   1093  * call actual syscall routine
   1094  * from the low-level syscall handler:
   1095  * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
   1096  *   our stack, this wins compared to copyin just needed amount anyway
   1097  * - register args are copied onto stack too
   1098  */
   1099 void
   1100 syscall(struct trapframe *frame, int *args)
   1101 {
   1102 	struct lwp *l;
   1103 	struct proc *p;
   1104 	const struct sysent *callp;
   1105 	size_t nargs64;
   1106 	int nsys, code, error;
   1107 	int tmp;
   1108 	int rval[2];
   1109 #ifdef DIAGNOSTIC
   1110 	int oldcpl = cpl;
   1111 #endif
   1112 
   1113 	uvmexp.syscalls++;
   1114 
   1115 #ifdef DEBUG
   1116 	frame_sanity_check(0xdead04, 0, frame, curlwp);
   1117 #endif /* DEBUG */
   1118 
   1119 	if (!USERMODE(frame->tf_iioq_head))
   1120 		panic("syscall");
   1121 
   1122 	KASSERT(curlwp != NULL);
   1123 	l = curlwp;
   1124 	p = l->l_proc;
   1125 	l->l_md.md_regs = frame;
   1126 	nsys = p->p_emul->e_nsysent;
   1127 	callp = p->p_emul->e_sysent;
   1128 	code = frame->tf_t1;
   1129 	LWP_CACHE_CREDS(l, p);
   1130 
   1131 #ifdef KERN_SA
   1132 	if (__predict_false((l->l_savp)
   1133             && (l->l_savp->savp_pflags & SAVP_FLAG_DELIVERING)))
   1134 		l->l_savp->savp_pflags &= ~SAVP_FLAG_DELIVERING;
   1135 #endif
   1136 
   1137 	/*
   1138 	 * Restarting a system call is touchy on the HPPA, because syscall
   1139 	 * arguments are passed in registers and the program counter of the
   1140 	 * syscall "point" isn't easily divined.
   1141 	 *
   1142 	 * We handle the first problem by assuming that we will have to restart
   1143 	 * this system call, so we stuff the first four words of the original
   1144 	 * arguments back into the frame as arg0...arg3, which is where we
   1145 	 * found them in the first place.  Any further arguments are (still) on
   1146 	 * the user's stack and the  syscall code will fetch them from there
   1147 	 * (again).
   1148 	 *
   1149 	 * The program counter problem is addressed below.
   1150 	 */
   1151 	frame->tf_arg0 = args[0];
   1152 	frame->tf_arg1 = args[1];
   1153 	frame->tf_arg2 = args[2];
   1154 	frame->tf_arg3 = args[3];
   1155 
   1156 	/*
   1157 	 * Some special handling for the syscall(2) and
   1158 	 * __syscall(2) system calls.
   1159 	 */
   1160 	switch (code) {
   1161 	case SYS_syscall:
   1162 		code = *args;
   1163 		args += 1;
   1164 		break;
   1165 	case SYS___syscall:
   1166 		if (callp != sysent)
   1167 			break;
   1168 		/*
   1169 		 * NB: even though __syscall(2) takes a quad_t containing the
   1170 		 * system call number, because our argument copying word-swaps
   1171 		 * 64-bit arguments, the least significant word of that quad_t
   1172 		 * is the first word in the argument array.
   1173 		 */
   1174 		code = *args;
   1175 		args += 2;
   1176 	}
   1177 
   1178 	/*
   1179 	 * Stacks growing from lower addresses to higher addresses are not
   1180 	 * really such a good idea, because it makes it impossible to overlay a
   1181 	 * struct on top of C stack arguments (the arguments appear in
   1182 	 * reversed order).
   1183 	 *
   1184 	 * You can do the obvious thing (as locore.S does) and copy argument
   1185 	 * words one by one, laying them out in the "right" order in the dest-
   1186 	 * ination buffer, but this ends up word-swapping multi-word arguments
   1187 	 * (like off_t).
   1188 	 *
   1189 	 * FIXME - this works only on native binaries and
   1190 	 * will probably screw up any and all emulation.
   1191 	 *
   1192 	 */
   1193 
   1194 	if (code < 0 || code >= nsys)
   1195 		callp += p->p_emul->e_nosys;	/* bad syscall # */
   1196 	else
   1197 		callp += code;
   1198 
   1199 	nargs64 = SYCALL_NARGS64(callp);
   1200 	if (nargs64 != 0) {
   1201 		size_t nargs = callp->sy_narg;
   1202 
   1203 		for (size_t i = 0; i < nargs + nargs64;) {
   1204 			if (SYCALL_ARG_64_P(callp, i)) {
   1205 				tmp = args[i];
   1206 				args[i] = args[i + 1];
   1207 				args[i + 1] = tmp;
   1208 				i += 2;
   1209 			} else
   1210 				i++;
   1211 		}
   1212 	}
   1213 
   1214 #ifdef USERTRACE
   1215 	if (0) {
   1216 		user_backtrace(frame, l, -1);
   1217 		frame->tf_ipsw |= PSW_R;
   1218 		frame->tf_rctr = 0;
   1219 		printf("r %08x", frame->tf_iioq_head);
   1220 		rctr_next_iioq = frame->tf_iioq_head + 4;
   1221 	}
   1222 #endif
   1223 
   1224 	error = 0;
   1225 	if (__predict_false(p->p_trace_enabled)) {
   1226 		error = trace_enter(code, args, callp->sy_narg);
   1227 		if (error)
   1228 			goto out;
   1229 	}
   1230 
   1231 	rval[0] = 0;
   1232 	rval[1] = 0;
   1233 	error = sy_call(callp, l, args, rval);
   1234 out:
   1235 	switch (error) {
   1236 	case 0:
   1237 		l = curlwp;			/* changes on exec() */
   1238 		frame = l->l_md.md_regs;
   1239 		frame->tf_ret0 = rval[0];
   1240 		frame->tf_ret1 = rval[1];
   1241 		frame->tf_t1 = 0;
   1242 		break;
   1243 	case ERESTART:
   1244 		/*
   1245 		 * Now we have to wind back the instruction offset queue to the
   1246 		 * point where the system call will be made again.  This is
   1247 		 * inherently tied to the SYSCALL macro.
   1248 		 *
   1249 		 * Currently, the part of the SYSCALL macro that we want to re-
   1250 		 * run reads as:
   1251 		 *
   1252 		 *	ldil	L%SYSCALLGATE, r1
   1253 		 *	ble	4(sr7, r1)
   1254 		 *	ldi	__CONCAT(SYS_,x), t1
   1255 		 *	comb,<>	%r0, %t1, __cerror
   1256 		 *
   1257 		 * And our offset queue head points to the comb instruction.
   1258 		 * So we need to subtract twelve to reach the ldil.
   1259 		 */
   1260 		frame->tf_iioq_head -= 12;
   1261 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
   1262 		break;
   1263 	case EJUSTRETURN:
   1264 		p = curproc;
   1265 		break;
   1266 	default:
   1267 		if (p->p_emul->e_errno)
   1268 			error = p->p_emul->e_errno[error];
   1269 		frame->tf_t1 = error;
   1270 		break;
   1271 	}
   1272 
   1273 	if (__predict_false(p->p_trace_enabled))
   1274 		trace_exit(code, rval, error);
   1275 
   1276 	userret(l, frame->tf_iioq_head, 0);
   1277 
   1278 #ifdef DIAGNOSTIC
   1279 	if (cpl != oldcpl) {
   1280 		printf("WARNING: SPL (0x%x) NOT LOWERED ON "
   1281 		    "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
   1282 		    cpl, code, args[0], args[1], args[2], p->p_pid);
   1283 		cpl = oldcpl;
   1284 	}
   1285 #endif
   1286 
   1287 #ifdef DEBUG
   1288 	frame_sanity_check(0xdead05, 0, frame, l);
   1289 #endif /* DEBUG */
   1290 }
   1291 
   1292 /*
   1293  * Start a new LWP
   1294  */
   1295 void
   1296 startlwp(void *arg)
   1297 {
   1298 	int err;
   1299 	ucontext_t *uc = arg;
   1300 	struct lwp *l = curlwp;
   1301 
   1302 	err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
   1303 #if DIAGNOSTIC
   1304 	if (err) {
   1305 		printf("Error %d from cpu_setmcontext.", err);
   1306 	}
   1307 #endif
   1308 	pool_put(&lwp_uc_pool, uc);
   1309 
   1310 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1311 }
   1312 
   1313 /*
   1314  * XXX This is a terrible name.
   1315  */
   1316 void
   1317 upcallret(struct lwp *l)
   1318 {
   1319 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1320 }
   1321