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