Home | History | Annotate | Line # | Download | only in hppa
trap.c revision 1.61
      1 /*	$NetBSD: trap.c,v 1.61 2009/11/21 15:36:34 rmind 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.61 2009/11/21 15:36:34 rmind 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 = sizeof(trap_type)/sizeof(trap_type[0]);
    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 (curcpu()->ci_want_resched) {
    210 		preempt();
    211 	}
    212 
    213 	mi_userret(l);
    214 
    215 	/*
    216 	 * If profiling, charge recent system time to the trapped pc.
    217 	 */
    218 	if (p->p_stflag & PST_PROFIL) {
    219 		extern int psratio;
    220 
    221 		addupc_task(l, pc, (int)(p->p_sticks - oticks) * psratio);
    222 	}
    223 }
    224 
    225 /*
    226  * This handles some messy kernel debugger details.
    227  * It dispatches into either kgdb or DDB, and knows
    228  * about some special things to do, like skipping over
    229  * break instructions and how to really set up for
    230  * a single-step.
    231  */
    232 #if defined(KGDB) || defined(DDB)
    233 static int
    234 trap_kdebug(int type, int code, struct trapframe *frame)
    235 {
    236 	int handled;
    237 	u_int tf_iioq_head_old;
    238 	u_int tf_iioq_tail_old;
    239 
    240 	for(;;) {
    241 
    242 		/* This trap has not been handled. */
    243 		handled = 0;
    244 
    245 		/* Remember the instruction offset queue. */
    246 		tf_iioq_head_old = frame->tf_iioq_head;
    247 		tf_iioq_tail_old = frame->tf_iioq_tail;
    248 
    249 #ifdef	KGDB
    250 		/* Let KGDB handle it (if connected) */
    251 		if (!handled)
    252 			handled = kgdb_trap(type, frame);
    253 #endif
    254 #ifdef	DDB
    255 		/* Let DDB handle it. */
    256 		if (!handled)
    257 			handled = kdb_trap(type, code, frame);
    258 #endif
    259 
    260 		/* If this trap wasn't handled, return now. */
    261 		if (!handled)
    262 			return(0);
    263 
    264 		/*
    265 		 * If the instruction offset queue head changed,
    266 		 * but the offset queue tail didn't, assume that
    267 		 * the user wants to jump to the head offset, and
    268 		 * adjust the tail accordingly.  This should fix
    269 		 * the kgdb `jump' command, and can help DDB users
    270 		 * who `set' the offset head but forget the tail.
    271 		 */
    272 		if (frame->tf_iioq_head != tf_iioq_head_old &&
    273 		    frame->tf_iioq_tail == tf_iioq_tail_old)
    274 			frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    275 
    276 		/*
    277 		 * This is some single-stepping support.
    278 		 * If we're trying to step through a nullified
    279 		 * instruction, just advance by hand and trap
    280 		 * again.  Otherwise, load the recovery counter
    281 		 * with zero.
    282 		 */
    283 		if (frame->tf_ipsw & PSW_R) {
    284 #ifdef TRAPDEBUG
    285 			printf("(single stepping at head 0x%x tail 0x%x)\n",
    286 			    frame->tf_iioq_head, frame->tf_iioq_tail);
    287 #endif
    288 			if (frame->tf_ipsw & PSW_N) {
    289 #ifdef TRAPDEBUG
    290 				printf("(single stepping past nullified)\n");
    291 #endif
    292 
    293 				/* Advance the program counter. */
    294 				frame->tf_iioq_head = frame->tf_iioq_tail;
    295 				frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    296 
    297 				/* Clear flags. */
    298 				frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
    299 
    300 				/* Simulate another trap. */
    301 				type = T_RECOVERY;
    302 				continue;
    303 			}
    304 			frame->tf_rctr = 0;
    305 		}
    306 
    307 		/* We handled this trap. */
    308 		return (1);
    309 	}
    310 	/* NOTREACHED */
    311 }
    312 #else	/* !KGDB && !DDB */
    313 #define trap_kdebug(t, c, f)	(0)
    314 #endif	/* !KGDB && !DDB */
    315 
    316 #if defined(DEBUG) || defined(USERTRACE)
    317 /*
    318  * These functions give a crude usermode backtrace.  They
    319  * really only work when code has been compiled without
    320  * optimization, as they assume a certain function prologue
    321  * sets up a frame pointer and stores the return pointer
    322  * and arguments 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 #define SANITY(e)					\
    417 do {							\
    418 	if (sanity_frame == NULL && !(e)) {		\
    419 		sanity_frame = tf;			\
    420 		sanity_lwp = l;				\
    421 		sanity_checked = __LINE__;		\
    422 	}						\
    423 } while (/* CONSTCOND */ 0)
    424 
    425 	SANITY((tf->tf_ipsw & kpsw) == kpsw);
    426 	SANITY((kpsw & PSW_I) == 0 || tf->tf_eiem != 0);
    427 	if (tf->tf_iisq_head == HPPA_SID_KERNEL) {
    428 		vaddr_t minsp, maxsp;
    429 
    430 		/*
    431 		 * If the trap happened in the gateway
    432 		 * page, we take the easy way out and
    433 		 * assume that the trapframe is okay.
    434 		 */
    435 		if ((tf->tf_iioq_head & ~PAGE_MASK) == SYSCALLGATE)
    436 			goto out;
    437 
    438 		SANITY(!USERMODE(tf->tf_iioq_head));
    439 		SANITY(!USERMODE(tf->tf_iioq_tail));
    440 
    441 		/*
    442 		 * Don't check the instruction queues or stack on interrupts
    443 		 * as we could be be in the sti code (outside normal kernel
    444 		 * text) or switching LWPs (curlwp and sp are not in sync)
    445 		 */
    446 		if ((type & ~T_USER) == T_INTERRUPT)
    447 			goto out;
    448 
    449 		SANITY(tf->tf_iioq_head >= (u_int) &kernel_text);
    450 		SANITY(tf->tf_iioq_head < (u_int) &etext);
    451 		SANITY(tf->tf_iioq_tail >= (u_int) &kernel_text);
    452 		SANITY(tf->tf_iioq_tail < (u_int) &etext);
    453 
    454 		maxsp = (u_int)(l->l_addr) + USPACE + PAGE_SIZE;
    455 		minsp = (u_int)(l->l_addr) + PAGE_SIZE;
    456 
    457 		SANITY(l != NULL || (tf->tf_sp >= minsp && tf->tf_sp < maxsp));
    458 	} else {
    459 		SANITY(USERMODE(tf->tf_iioq_head));
    460 		SANITY(USERMODE(tf->tf_iioq_tail));
    461 		SANITY(l != NULL && tf->tf_cr30 == kvtop((void *)l->l_addr));
    462 	}
    463 #undef SANITY
    464 out:
    465 	if (sanity_frame == tf) {
    466 		printf("insanity: where 0x%x type 0x%x tf %p lwp %p line %d "
    467 		       "sp 0x%x pc 0x%x\n",
    468 		       where, type, sanity_frame, sanity_lwp, sanity_checked,
    469 		       tf->tf_sp, tf->tf_iioq_head);
    470 		(void) trap_kdebug(T_IBREAK, 0, tf);
    471 		sanity_frame = NULL;
    472 		sanity_lwp = NULL;
    473 		sanity_checked = 0;
    474 	}
    475 }
    476 #endif /* DEBUG */
    477 
    478 void
    479 trap(int type, struct trapframe *frame)
    480 {
    481 	struct lwp *l;
    482 	struct proc *p;
    483 	struct pcb *pcb;
    484 	vaddr_t va;
    485 	struct vm_map *map;
    486 	struct vmspace *vm;
    487 	vm_prot_t vftype;
    488 	pa_space_t space;
    489 	ksiginfo_t ksi;
    490 	u_int opcode, onfault;
    491 	int ret;
    492 	const char *tts;
    493 	int type_raw;
    494 #ifdef DIAGNOSTIC
    495 	extern int emergency_stack_start, emergency_stack_end;
    496 #endif
    497 
    498 	type_raw = type & ~T_USER;
    499 	opcode = frame->tf_iir;
    500 	if (type_raw == T_ITLBMISS || type_raw == T_ITLBMISSNA ||
    501 	    type_raw == T_IBREAK || type_raw == T_TAKENBR) {
    502 		va = frame->tf_iioq_head;
    503 		space = frame->tf_iisq_head;
    504 		vftype = VM_PROT_EXECUTE;
    505 	} else {
    506 		va = frame->tf_ior;
    507 		space = frame->tf_isr;
    508 		vftype = inst_store(opcode) ? VM_PROT_WRITE : VM_PROT_READ;
    509 	}
    510 
    511 	l = curlwp;
    512 	p = l ? l->l_proc : NULL;
    513 	if ((type & T_USER) != 0)
    514 		LWP_CACHE_CREDS(l, p);
    515 
    516 	tts = (type & ~T_USER) > trap_types ? "reserved" :
    517 		trap_type[type & ~T_USER];
    518 
    519 #ifdef DIAGNOSTIC
    520 	/*
    521 	 * If we are on the emergency stack, then we either got
    522 	 * a fault on the kernel stack, or we're just handling
    523 	 * a trap for the machine check handler (which also
    524 	 * runs on the emergency stack).
    525 	 *
    526 	 * We *very crudely* differentiate between the two cases
    527 	 * by checking the faulting instruction: if it is the
    528 	 * function prologue instruction that stores the old
    529 	 * frame pointer and updates the stack pointer, we assume
    530 	 * that we faulted on the kernel stack.
    531 	 *
    532 	 * In this case, not completing that instruction will
    533 	 * probably confuse backtraces in kgdb/ddb.  Completing
    534 	 * it would be difficult, because we already faulted on
    535 	 * that part of the stack, so instead we fix up the
    536 	 * frame as if the function called has just returned.
    537 	 * This has peculiar knowledge about what values are in
    538 	 * what registers during the "normal gcc -g" prologue.
    539 	 */
    540 	if (&type >= &emergency_stack_start &&
    541 	    &type < &emergency_stack_end &&
    542 	    type != T_IBREAK && STWM_R1_D_SR0_SP(opcode)) {
    543 		/* Restore the caller's frame pointer. */
    544 		frame->tf_r3 = frame->tf_r1;
    545 		/* Restore the caller's instruction offsets. */
    546 		frame->tf_iioq_head = frame->tf_rp;
    547 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    548 		goto dead_end;
    549 	}
    550 #endif /* DIAGNOSTIC */
    551 
    552 #ifdef DEBUG
    553 	frame_sanity_check(0xdead01, type, frame, l);
    554 #endif /* DEBUG */
    555 
    556 	/* If this is a trap, not an interrupt, reenable interrupts. */
    557 	if (type_raw != T_INTERRUPT)
    558 		mtctl(frame->tf_eiem, CR_EIEM);
    559 
    560 	if (frame->tf_flags & TFF_LAST)
    561 		l->l_md.md_regs = frame;
    562 
    563 #ifdef TRAPDEBUG
    564 	if (type_raw != T_INTERRUPT && type_raw != T_IBREAK)
    565 		printf("trap: %d, %s for %x:%x at %x:%x, fp=%p, rp=%x\n",
    566 		    type, tts, space, (u_int)va, frame->tf_iisq_head,
    567 		    frame->tf_iioq_head, frame, frame->tf_rp);
    568 	else if (type_raw == T_IBREAK)
    569 		printf("trap: break instruction %x:%x at %x:%x, fp=%p\n",
    570 		    break5(opcode), break13(opcode),
    571 		    frame->tf_iisq_head, frame->tf_iioq_head, frame);
    572 
    573 	{
    574 		extern int etext;
    575 		if (frame < (struct trapframe *)&etext) {
    576 			printf("trap: bogus frame ptr %p\n", frame);
    577 			goto dead_end;
    578 		}
    579 	}
    580 #endif
    581 	pcb = lwp_getpcb(l);
    582 
    583 	switch (type) {
    584 	case T_NONEXIST:
    585 	case T_NONEXIST|T_USER:
    586 #if !defined(DDB) && !defined(KGDB)
    587 		/* we've got screwed up by the central scrutinizer */
    588 		panic ("trap: elvis has just left the building!");
    589 		break;
    590 #else
    591 		goto dead_end;
    592 #endif
    593 	case T_RECOVERY|T_USER:
    594 #ifdef USERTRACE
    595 		for(;;) {
    596 			if (frame->tf_iioq_head != rctr_next_iioq)
    597 				printf("-%08x\nr %08x",
    598 					rctr_next_iioq - 4,
    599 					frame->tf_iioq_head);
    600 			rctr_next_iioq = frame->tf_iioq_head + 4;
    601 			if (frame->tf_ipsw & PSW_N) {
    602 				/* Advance the program counter. */
    603 				frame->tf_iioq_head = frame->tf_iioq_tail;
    604 				frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    605 				/* Clear flags. */
    606 				frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
    607 				/* Simulate another trap. */
    608 				continue;
    609 			}
    610 			break;
    611 		}
    612 		frame->tf_rctr = 0;
    613 		break;
    614 #endif /* USERTRACE */
    615 	case T_RECOVERY:
    616 #if !defined(DDB) && !defined(KGDB)
    617 		/* XXX will implement later */
    618 		printf ("trap: handicapped");
    619 		break;
    620 #else
    621 		goto dead_end;
    622 #endif
    623 
    624 	case T_EMULATION | T_USER:
    625 #ifdef FPEMUL
    626 		hppa_fpu_emulate(frame, l, opcode);
    627 #else  /* !FPEMUL */
    628 		/*
    629 		 * We don't have FPU emulation, so signal the
    630 		 * process with a SIGFPE.
    631 		 */
    632 
    633 		KSI_INIT_TRAP(&ksi);
    634 		ksi.ksi_signo = SIGFPE;
    635 		ksi.ksi_code = SI_NOINFO;
    636 		ksi.ksi_trap = type;
    637 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    638 		trapsignal(l, &ksi);
    639 #endif /* !FPEMUL */
    640 		break;
    641 
    642 	case T_DATALIGN:
    643 		if (pcb->pcb_onfault) {
    644 do_onfault:
    645 			frame->tf_iioq_head = pcb->pcb_onfault;
    646 			frame->tf_iioq_tail = 4 + frame->tf_iioq_head;
    647 			pcb->pcb_onfault = 0;
    648 			break;
    649 		}
    650 		/*FALLTHROUGH*/
    651 
    652 #ifdef DIAGNOSTIC
    653 		/* these just can't happen ever */
    654 	case T_PRIV_OP:
    655 	case T_PRIV_REG:
    656 		/* these just can't make it to the trap() ever */
    657 	case T_HPMC:
    658 	case T_HPMC | T_USER:
    659 	case T_EMULATION:
    660 	case T_EXCEPTION:
    661 #endif
    662 	case T_IBREAK:
    663 	case T_DBREAK:
    664 	dead_end:
    665 		if (type & T_USER) {
    666 #ifdef DEBUG
    667 			user_backtrace(frame, l, type);
    668 #endif
    669 			KSI_INIT_TRAP(&ksi);
    670 			ksi.ksi_signo = SIGILL;
    671 			ksi.ksi_code = ILL_ILLTRP;
    672 			ksi.ksi_trap = type;
    673 			ksi.ksi_addr = (void *)frame->tf_iioq_head;
    674 			trapsignal(l, &ksi);
    675 			break;
    676 		}
    677 		if (trap_kdebug(type, va, frame))
    678 			return;
    679 		else if (type == T_DATALIGN)
    680 			panic ("trap: %s at 0x%x", tts, (u_int) va);
    681 		else
    682 			panic ("trap: no debugger for \"%s\" (%d)", tts, type);
    683 		break;
    684 
    685 	case T_IBREAK | T_USER:
    686 	case T_DBREAK | T_USER:
    687 		KSI_INIT_TRAP(&ksi);
    688 		ksi.ksi_signo = SIGTRAP;
    689 		ksi.ksi_code = TRAP_TRACE;
    690 		ksi.ksi_trap = type_raw;
    691 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    692 #ifdef PTRACE
    693 		ss_clear_breakpoints(l);
    694 		if (opcode == SSBREAKPOINT)
    695 			ksi.ksi_code = TRAP_BRKPT;
    696 #endif
    697 		/* pass to user debugger */
    698 		trapsignal(l, &ksi);
    699 
    700 		break;
    701 
    702 #ifdef PTRACE
    703 	case T_TAKENBR | T_USER:
    704 		ss_clear_breakpoints(l);
    705 
    706 		KSI_INIT_TRAP(&ksi);
    707 		ksi.ksi_signo = SIGTRAP;
    708 		ksi.ksi_code = TRAP_TRACE;
    709 		ksi.ksi_trap = type_raw;
    710 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    711 
    712                 /* pass to user debugger */
    713 		trapsignal(l, &ksi);
    714 		break;
    715 #endif
    716 
    717 	case T_EXCEPTION | T_USER: {	/* co-proc assist trap */
    718 		uint64_t *fpp;
    719 		uint32_t *pex, ex, inst;
    720 		int i;
    721 
    722 		hppa_fpu_flush(l);
    723 		fpp = pcb->pcb_fpregs;
    724 		pex = (uint32_t *)&fpp[1];
    725 		for (i = 1; i < 8 && !*pex; i++, pex++)
    726 			;
    727 		KASSERT(i < 8);
    728 		ex = *pex;
    729 		*pex = 0;
    730 
    731 		/* reset the trap flag, as if there was none */
    732 		fpp[0] &= ~(((uint64_t)HPPA_FPU_T) << 32);
    733 
    734 		/* emulate the instruction */
    735 		inst = ((uint32_t)fpopmap[ex >> 26] << 26) | (ex & 0x03ffffff);
    736 		hppa_fpu_emulate(frame, l, inst);
    737 		}
    738 		break;
    739 
    740 	case T_OVERFLOW | T_USER:
    741 		KSI_INIT_TRAP(&ksi);
    742 		ksi.ksi_signo = SIGFPE;
    743 		ksi.ksi_code = SI_NOINFO;
    744 		ksi.ksi_trap = type;
    745 		ksi.ksi_addr = (void *)va;
    746 		trapsignal(l, &ksi);
    747 		break;
    748 
    749 	case T_CONDITION | T_USER:
    750 		KSI_INIT_TRAP(&ksi);
    751 		ksi.ksi_signo = SIGFPE;
    752 		ksi.ksi_code = FPE_INTDIV;
    753 		ksi.ksi_trap = type;
    754 		ksi.ksi_addr = (void *)va;
    755 		trapsignal(l, &ksi);
    756 		break;
    757 
    758 	case T_ILLEGAL | T_USER:
    759 #ifdef DEBUG
    760 		user_backtrace(frame, l, type);
    761 #endif
    762 		KSI_INIT_TRAP(&ksi);
    763 		ksi.ksi_signo = SIGILL;
    764 		ksi.ksi_code = ILL_ILLOPC;
    765 		ksi.ksi_trap = type;
    766 		ksi.ksi_addr = (void *)va;
    767 		trapsignal(l, &ksi);
    768 		break;
    769 
    770 	case T_PRIV_OP | T_USER:
    771 #ifdef DEBUG
    772 		user_backtrace(frame, l, type);
    773 #endif
    774 		KSI_INIT_TRAP(&ksi);
    775 		ksi.ksi_signo = SIGILL;
    776 		ksi.ksi_code = ILL_PRVOPC;
    777 		ksi.ksi_trap = type;
    778 		ksi.ksi_addr = (void *)va;
    779 		trapsignal(l, &ksi);
    780 		break;
    781 
    782 	case T_PRIV_REG | T_USER:
    783 #ifdef DEBUG
    784 		user_backtrace(frame, l, type);
    785 #endif
    786 		KSI_INIT_TRAP(&ksi);
    787 		ksi.ksi_signo = SIGILL;
    788 		ksi.ksi_code = ILL_PRVREG;
    789 		ksi.ksi_trap = type;
    790 		ksi.ksi_addr = (void *)va;
    791 		trapsignal(l, &ksi);
    792 		break;
    793 
    794 		/* these should never got here */
    795 	case T_HIGHERPL | T_USER:
    796 	case T_LOWERPL | T_USER:
    797 		KSI_INIT_TRAP(&ksi);
    798 		ksi.ksi_signo = SIGSEGV;
    799 		ksi.ksi_code = SEGV_ACCERR;
    800 		ksi.ksi_trap = type;
    801 		ksi.ksi_addr = (void *)va;
    802 		trapsignal(l, &ksi);
    803 		break;
    804 
    805 	case T_IPROT | T_USER:
    806 	case T_DPROT | T_USER:
    807 		KSI_INIT_TRAP(&ksi);
    808 		ksi.ksi_signo = SIGSEGV;
    809 		ksi.ksi_code = SEGV_ACCERR;
    810 		ksi.ksi_trap = type;
    811 		ksi.ksi_addr = (void *)va;
    812 		trapsignal(l, &ksi);
    813 		break;
    814 
    815 	case T_DATACC:   	case T_USER | T_DATACC:
    816 	case T_ITLBMISS:	case T_USER | T_ITLBMISS:
    817 	case T_DTLBMISS:	case T_USER | T_DTLBMISS:
    818 	case T_ITLBMISSNA:	case T_USER | T_ITLBMISSNA:
    819 	case T_DTLBMISSNA:	case T_USER | T_DTLBMISSNA:
    820 	case T_TLB_DIRTY:	case T_USER | T_TLB_DIRTY:
    821 		vm = p->p_vmspace;
    822 
    823 		if (!vm) {
    824 #ifdef TRAPDEBUG
    825 			printf("trap: no vm, p=%p\n", p);
    826 #endif
    827 			goto dead_end;
    828 		}
    829 
    830 		/*
    831 		 * it could be a kernel map for exec_map faults
    832 		 */
    833 		if (!(type & T_USER) && space == HPPA_SID_KERNEL)
    834 			map = kernel_map;
    835 		else {
    836 			map = &vm->vm_map;
    837 			if ((l->l_flag & LW_SA)
    838 			    && (~l->l_pflag & LP_SA_NOBLOCK)) {
    839 				l->l_savp->savp_faultaddr = va;
    840 				l->l_pflag |= LP_SA_PAGEFAULT;
    841 			}
    842 		}
    843 
    844 		va = trunc_page(va);
    845 
    846 		if (map->pmap->pm_space != space) {
    847 #ifdef TRAPDEBUG
    848 			printf("trap: space mismatch %d != %d\n",
    849 			    space, map->pmap->pm_space);
    850 #endif
    851 			/* actually dump the user, crap the kernel */
    852 			goto dead_end;
    853 		}
    854 
    855 		/* Never call uvm_fault in interrupt context. */
    856 		KASSERT(hppa_intr_depth == 0);
    857 
    858 		onfault = pcb->pcb_onfault;
    859 		pcb->pcb_onfault = 0;
    860 		ret = uvm_fault(map, va, vftype);
    861 		pcb->pcb_onfault = onfault;
    862 
    863 #ifdef TRAPDEBUG
    864 		printf("uvm_fault(%p, %x, %d)=%d\n",
    865 		    map, (u_int)va, vftype, ret);
    866 #endif
    867 
    868 		if (map != kernel_map)
    869 			l->l_pflag &= ~LP_SA_PAGEFAULT;
    870 
    871 		/*
    872 		 * If this was a stack access we keep track of the maximum
    873 		 * accessed stack size.  Also, if uvm_fault gets a protection
    874 		 * failure it is due to accessing the stack region outside
    875 		 * the current limit and we need to reflect that as an access
    876 		 * error.
    877 		 */
    878 		if (map != kernel_map && va >= (vaddr_t)vm->vm_minsaddr) {
    879 			if (ret == 0)
    880 				uvm_grow(l->l_proc, va);
    881 			else if (ret == EACCES)
    882 				ret = EFAULT;
    883 		}
    884 
    885 		if (ret != 0) {
    886 			if (type & T_USER) {
    887 #ifdef DEBUG
    888 				user_backtrace(frame, l, type);
    889 #endif
    890 				KSI_INIT_TRAP(&ksi);
    891 				ksi.ksi_signo = SIGSEGV;
    892 				ksi.ksi_code = (ret == EACCES ?
    893 						SEGV_ACCERR : SEGV_MAPERR);
    894 				ksi.ksi_trap = type;
    895 				ksi.ksi_addr = (void *)va;
    896 				trapsignal(l, &ksi);
    897 			} else {
    898 				if (pcb->pcb_onfault) {
    899 					goto do_onfault;
    900 				}
    901 				panic("trap: uvm_fault(%p, %lx, %d): %d",
    902 				    map, va, vftype, ret);
    903 			}
    904 		}
    905 		break;
    906 
    907 	case T_DATALIGN | T_USER:
    908 #ifdef DEBUG
    909 		user_backtrace(frame, l, type);
    910 #endif
    911 		KSI_INIT_TRAP(&ksi);
    912 		ksi.ksi_signo = SIGBUS;
    913 		ksi.ksi_code = BUS_ADRALN;
    914 		ksi.ksi_trap = type;
    915 		ksi.ksi_addr = (void *)va;
    916 		trapsignal(l, &ksi);
    917 		break;
    918 
    919 	case T_INTERRUPT:
    920 	case T_INTERRUPT|T_USER:
    921 		hppa_intr(frame);
    922 		mtctl(frame->tf_eiem, CR_EIEM);
    923 		break;
    924 
    925 	case T_LOWERPL:
    926 	case T_DPROT:
    927 	case T_IPROT:
    928 	case T_OVERFLOW:
    929 	case T_CONDITION:
    930 	case T_ILLEGAL:
    931 	case T_HIGHERPL:
    932 	case T_TAKENBR:
    933 	case T_POWERFAIL:
    934 	case T_LPMC:
    935 	case T_PAGEREF:
    936 	case T_DATAPID:  	case T_DATAPID  | T_USER:
    937 		if (0 /* T-chip */) {
    938 			break;
    939 		}
    940 		/* FALLTHROUGH to unimplemented */
    941 	default:
    942 		panic ("trap: unimplemented \'%s\' (%d)", tts, type);
    943 	}
    944 
    945 	if (type & T_USER)
    946 		userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    947 
    948 #ifdef DEBUG
    949 	frame_sanity_check(0xdead02, type, frame, l);
    950 	if (frame->tf_flags & TFF_LAST && (curlwp->l_flag & LW_IDLE) == 0)
    951 		frame_sanity_check(0xdead03, type, curlwp->l_md.md_regs,
    952 				   curlwp);
    953 #endif /* DEBUG */
    954 }
    955 
    956 void
    957 child_return(void *arg)
    958 {
    959 	struct lwp *l = arg;
    960 
    961 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    962 	ktrsysret(SYS_fork, 0, 0);
    963 #ifdef DEBUG
    964 	frame_sanity_check(0xdead04, 0, l->l_md.md_regs, l);
    965 #endif /* DEBUG */
    966 }
    967 
    968 #ifdef PTRACE
    969 
    970 #include <sys/ptrace.h>
    971 
    972 int
    973 ss_get_value(struct lwp *l, vaddr_t addr, u_int *value)
    974 {
    975 	struct uio uio;
    976 	struct iovec iov;
    977 
    978 	iov.iov_base = (void *)value;
    979 	iov.iov_len = sizeof(u_int);
    980 	uio.uio_iov = &iov;
    981 	uio.uio_iovcnt = 1;
    982 	uio.uio_offset = (off_t)addr;
    983 	uio.uio_resid = sizeof(u_int);
    984 	uio.uio_rw = UIO_READ;
    985 	UIO_SETUP_SYSSPACE(&uio);
    986 
    987 	return (process_domem(curlwp, l, &uio));
    988 }
    989 
    990 int
    991 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
    992 {
    993 	struct uio uio;
    994 	struct iovec iov;
    995 
    996 	iov.iov_base = (void *)&value;
    997 	iov.iov_len = sizeof(u_int);
    998 	uio.uio_iov = &iov;
    999 	uio.uio_iovcnt = 1;
   1000 	uio.uio_offset = (off_t)addr;
   1001 	uio.uio_resid = sizeof(u_int);
   1002 	uio.uio_rw = UIO_WRITE;
   1003 	UIO_SETUP_SYSSPACE(&uio);
   1004 
   1005 	return (process_domem(curlwp, l, &uio));
   1006 }
   1007 
   1008 void
   1009 ss_clear_breakpoints(struct lwp *l)
   1010 {
   1011 	/* Restore origional instructions. */
   1012 	if (l->l_md.md_bpva != 0) {
   1013 		ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
   1014 		ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
   1015 		l->l_md.md_bpva = 0;
   1016 	}
   1017 }
   1018 
   1019 
   1020 int
   1021 process_sstep(struct lwp *l, int sstep)
   1022 {
   1023 	struct trapframe *tf = l->l_md.md_regs;
   1024 	int error;
   1025 
   1026 	ss_clear_breakpoints(l);
   1027 
   1028 	/* We're continuing... */
   1029 	/* Don't touch the syscall gateway page. */
   1030 	/* XXX head */
   1031 	if (sstep == 0 ||
   1032 	    (tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE) {
   1033 		tf->tf_ipsw &= ~PSW_T;
   1034 		return 0;
   1035 	}
   1036 
   1037 	l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
   1038 
   1039 	/*
   1040 	 * Insert two breakpoint instructions; the first one might be
   1041 	 * nullified.  Of course we need to save two instruction
   1042 	 * first.
   1043 	 */
   1044 
   1045 	error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
   1046 	if (error)
   1047 		return (error);
   1048 	error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
   1049 	if (error)
   1050 		return (error);
   1051 
   1052 	error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
   1053 	if (error)
   1054 		return error;
   1055 	error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
   1056 	if (error)
   1057 		return error;
   1058 
   1059 	tf->tf_ipsw |= PSW_T;
   1060 
   1061 	return 0;
   1062 }
   1063 #endif
   1064 
   1065 
   1066 /*
   1067  * call actual syscall routine
   1068  * from the low-level syscall handler:
   1069  * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
   1070  *   our stack, this wins compared to copyin just needed amount anyway
   1071  * - register args are copied onto stack too
   1072  */
   1073 void
   1074 syscall(struct trapframe *frame, int *args)
   1075 {
   1076 	struct lwp *l;
   1077 	struct proc *p;
   1078 	const struct sysent *callp;
   1079 	int nsys, code, error;
   1080 	int tmp;
   1081 	int rval[2];
   1082 
   1083 	uvmexp.syscalls++;
   1084 
   1085 #ifdef DEBUG
   1086 	frame_sanity_check(0xdead04, 0, frame, curlwp);
   1087 #endif /* DEBUG */
   1088 
   1089 	if (!USERMODE(frame->tf_iioq_head))
   1090 		panic("syscall");
   1091 
   1092 	l = curlwp;
   1093 	p = l->l_proc;
   1094 	l->l_md.md_regs = frame;
   1095 	nsys = p->p_emul->e_nsysent;
   1096 	callp = p->p_emul->e_sysent;
   1097 	code = frame->tf_t1;
   1098 	LWP_CACHE_CREDS(l, p);
   1099 
   1100 #ifdef KERN_SA
   1101 	if (__predict_false((l->l_savp)
   1102             && (l->l_savp->savp_pflags & SAVP_FLAG_DELIVERING)))
   1103 		l->l_savp->savp_pflags &= ~SAVP_FLAG_DELIVERING;
   1104 #endif
   1105 
   1106 	/*
   1107 	 * Restarting a system call is touchy on the HPPA,
   1108 	 * because syscall arguments are passed in registers
   1109 	 * and the program counter of the syscall "point"
   1110 	 * isn't easily divined.
   1111 	 *
   1112 	 * We handle the first problem by assuming that we
   1113 	 * will have to restart this system call, so we
   1114 	 * stuff the first four words of the original arguments
   1115 	 * back into the frame as arg0...arg3, which is where
   1116 	 * we found them in the first place.  Any further
   1117 	 * arguments are (still) on the user's stack and the
   1118 	 * syscall code will fetch them from there (again).
   1119 	 *
   1120 	 * The program counter problem is addressed below.
   1121 	 */
   1122 	frame->tf_arg0 = args[0];
   1123 	frame->tf_arg1 = args[1];
   1124 	frame->tf_arg2 = args[2];
   1125 	frame->tf_arg3 = args[3];
   1126 
   1127 	/*
   1128 	 * Some special handling for the syscall(2) and
   1129 	 * __syscall(2) system calls.
   1130 	 */
   1131 	switch (code) {
   1132 	case SYS_syscall:
   1133 		code = *args;
   1134 		args += 1;
   1135 		break;
   1136 	case SYS___syscall:
   1137 		if (callp != sysent)
   1138 			break;
   1139 		/*
   1140 		 * NB: even though __syscall(2) takes a quad_t
   1141 		 * containing the system call number, because
   1142 		 * our argument copying word-swaps 64-bit arguments,
   1143 		 * the least significant word of that quad_t
   1144 		 * is the first word in the argument array.
   1145 		 */
   1146 		code = *args;
   1147 		args += 2;
   1148 	}
   1149 
   1150 	/*
   1151 	 * Stacks growing from lower addresses to higher
   1152 	 * addresses are not really such a good idea, because
   1153 	 * it makes it impossible to overlay a struct on top
   1154 	 * of C stack arguments (the arguments appear in
   1155 	 * reversed order).
   1156 	 *
   1157 	 * You can do the obvious thing (as locore.S does) and
   1158 	 * copy argument words one by one, laying them out in
   1159 	 * the "right" order in the destination buffer, but this
   1160 	 * ends up word-swapping multi-word arguments (like off_t).
   1161 	 *
   1162 	 * To compensate, we have some automatically-generated
   1163 	 * code that word-swaps these multi-word arguments.
   1164 	 * Right now the script that generates this code is
   1165 	 * in Perl, because I don't know awk.
   1166 	 *
   1167 	 * FIXME - this works only on native binaries and
   1168 	 * will probably screw up any and all emulation.
   1169 	 */
   1170 	switch (code) {
   1171 	case SYS_pread:
   1172 		/*
   1173 		 * 	syscallarg(int) fd;
   1174 		 * 	syscallarg(void *) buf;
   1175 		 * 	syscallarg(size_t) nbyte;
   1176 		 * 	syscallarg(int) pad;
   1177 		 * 	syscallarg(off_t) offset;
   1178 		 */
   1179 		tmp = args[4];
   1180 		args[4] = args[4 + 1];
   1181 		args[4 + 1] = tmp;
   1182 		break;
   1183 	case SYS_pwrite:
   1184 		/*
   1185 		 * 	syscallarg(int) fd;
   1186 		 * 	syscallarg(const void *) buf;
   1187 		 * 	syscallarg(size_t) nbyte;
   1188 		 * 	syscallarg(int) pad;
   1189 		 * 	syscallarg(off_t) offset;
   1190 		 */
   1191 		tmp = args[4];
   1192 		args[4] = args[4 + 1];
   1193 		args[4 + 1] = tmp;
   1194 		break;
   1195 	case SYS_mmap:
   1196 		/*
   1197 		 * 	syscallarg(void *) addr;
   1198 		 * 	syscallarg(size_t) len;
   1199 		 * 	syscallarg(int) prot;
   1200 		 * 	syscallarg(int) flags;
   1201 		 * 	syscallarg(int) fd;
   1202 		 * 	syscallarg(long) pad;
   1203 		 * 	syscallarg(off_t) pos;
   1204 		 */
   1205 		tmp = args[6];
   1206 		args[6] = args[6 + 1];
   1207 		args[6 + 1] = tmp;
   1208 		break;
   1209 	case SYS_lseek:
   1210 		/*
   1211 		 * 	syscallarg(int) fd;
   1212 		 * 	syscallarg(int) pad;
   1213 		 * 	syscallarg(off_t) offset;
   1214 		 */
   1215 		tmp = args[2];
   1216 		args[2] = args[2 + 1];
   1217 		args[2 + 1] = tmp;
   1218 		break;
   1219 	case SYS_truncate:
   1220 		/*
   1221 		 * 	syscallarg(const char *) path;
   1222 		 * 	syscallarg(int) pad;
   1223 		 * 	syscallarg(off_t) length;
   1224 		 */
   1225 		tmp = args[2];
   1226 		args[2] = args[2 + 1];
   1227 		args[2 + 1] = tmp;
   1228 		break;
   1229 	case SYS_ftruncate:
   1230 		/*
   1231 		 * 	syscallarg(int) fd;
   1232 		 * 	syscallarg(int) pad;
   1233 		 * 	syscallarg(off_t) length;
   1234 		 */
   1235 		tmp = args[2];
   1236 		args[2] = args[2 + 1];
   1237 		args[2 + 1] = tmp;
   1238 		break;
   1239 	case SYS_preadv:
   1240 		/*
   1241 		 * 	syscallarg(int) fd;
   1242 		 * 	syscallarg(const struct iovec *) iovp;
   1243 		 * 	syscallarg(int) iovcnt;
   1244 		 * 	syscallarg(int) pad;
   1245 		 * 	syscallarg(off_t) offset;
   1246 		 */
   1247 		tmp = args[4];
   1248 		args[4] = args[4 + 1];
   1249 		args[4 + 1] = tmp;
   1250 		break;
   1251 	case SYS_pwritev:
   1252 		/*
   1253 		 * 	syscallarg(int) fd;
   1254 		 * 	syscallarg(const struct iovec *) iovp;
   1255 		 * 	syscallarg(int) iovcnt;
   1256 		 * 	syscallarg(int) pad;
   1257 		 * 	syscallarg(off_t) offset;
   1258 		 */
   1259 		tmp = args[4];
   1260 		args[4] = args[4 + 1];
   1261 		args[4 + 1] = tmp;
   1262 		break;
   1263 	case SYS___posix_fadvise50:
   1264 		/*
   1265 		 *	syscallarg(int) fd;
   1266 		 *	syscallarg(int) pad;
   1267 		 *	syscallarg(off_t) offset;
   1268 		 *	syscallarg(off_t) len;
   1269 		 *	syscallarg(int) advice;
   1270 		 */
   1271 		tmp = args[2];
   1272 		args[2] = args[2 + 1];
   1273 		args[2 + 1] = tmp;
   1274 		tmp = args[4];
   1275 		args[4] = args[4 + 1];
   1276 		args[4 + 1] = tmp;
   1277 	case SYS___mknod50:
   1278 		/*
   1279 		 *	syscallarg(const char *) path;
   1280 		 *	syscallarg(mode_t) mode;
   1281 		 *	syscallarg(dev_t) dev;
   1282 		 */
   1283 		tmp = args[2];
   1284 		args[2] = args[2 + 1];
   1285 		args[2 + 1] = tmp;
   1286 	default:
   1287 		break;
   1288 	}
   1289 
   1290 #ifdef USERTRACE
   1291 	if (0) {
   1292 		user_backtrace(frame, l, -1);
   1293 		frame->tf_ipsw |= PSW_R;
   1294 		frame->tf_rctr = 0;
   1295 		printf("r %08x", frame->tf_iioq_head);
   1296 		rctr_next_iioq = frame->tf_iioq_head + 4;
   1297 	}
   1298 #endif
   1299 
   1300 	if (code < 0 || code >= nsys)
   1301 		callp += p->p_emul->e_nosys;	/* bad syscall # */
   1302 	else
   1303 		callp += code;
   1304 
   1305 	if ((error = trace_enter(code, args, callp->sy_narg)) != 0)
   1306 		goto out;
   1307 
   1308 	rval[0] = 0;
   1309 	rval[1] = 0;
   1310 	error = sy_call(callp, l, args, rval);
   1311 out:
   1312 	switch (error) {
   1313 	case 0:
   1314 		l = curlwp;			/* changes on exec() */
   1315 		frame = l->l_md.md_regs;
   1316 		frame->tf_ret0 = rval[0];
   1317 		frame->tf_ret1 = rval[1];
   1318 		frame->tf_t1 = 0;
   1319 		break;
   1320 	case ERESTART:
   1321 		/*
   1322 		 * Now we have to wind back the instruction
   1323 		 * offset queue to the point where the system
   1324 		 * call will be made again.  This is inherently
   1325 		 * tied to the SYSCALL macro.
   1326 		 *
   1327 		 * Currently, the part of the SYSCALL macro
   1328 		 * that we want to rerun reads as:
   1329 		 *
   1330 		 *	ldil	L%SYSCALLGATE, r1
   1331 		 *	ble	4(sr7, r1)
   1332 		 *	ldi	__CONCAT(SYS_,x), t1
   1333 		 *	comb,<>	%r0, %t1, __cerror
   1334 		 *
   1335 		 * And our offset queue head points to the
   1336 		 * comb instruction.  So we need to
   1337 		 * subtract twelve to reach the ldil.
   1338 		 */
   1339 		frame->tf_iioq_head -= 12;
   1340 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
   1341 		break;
   1342 	case EJUSTRETURN:
   1343 		p = curproc;
   1344 		break;
   1345 	default:
   1346 		if (p->p_emul->e_errno)
   1347 			error = p->p_emul->e_errno[error];
   1348 		frame->tf_t1 = error;
   1349 		break;
   1350 	}
   1351 
   1352 	trace_exit(code, rval, error);
   1353 
   1354 	userret(l, frame->tf_iioq_head, 0);
   1355 #ifdef DEBUG
   1356 	frame_sanity_check(0xdead05, 0, frame, l);
   1357 #endif /* DEBUG */
   1358 }
   1359 
   1360 /*
   1361  * Start a new LWP
   1362  */
   1363 void
   1364 startlwp(void *arg)
   1365 {
   1366 	int err;
   1367 	ucontext_t *uc = arg;
   1368 	struct lwp *l = curlwp;
   1369 
   1370 	err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
   1371 #if DIAGNOSTIC
   1372 	if (err) {
   1373 		printf("Error %d from cpu_setmcontext.", err);
   1374 	}
   1375 #endif
   1376 	pool_put(&lwp_uc_pool, uc);
   1377 
   1378 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1379 }
   1380 
   1381 /*
   1382  * XXX This is a terrible name.
   1383  */
   1384 void
   1385 upcallret(struct lwp *l)
   1386 {
   1387 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1388 }
   1389