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