Home | History | Annotate | Line # | Download | only in hppa
trap.c revision 1.88
      1 /*	$NetBSD: trap.c,v 1.88 2010/04/23 19:18:09 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.88 2010/04/23 19:18:09 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/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' in func %s at line %d type 0x%x tf %p "
    469 		    "lwp %p sp 0x%x pc 0x%x\n", sanity_string, func, line, type,
    470 		    sanity_frame, sanity_lwp, tf->tf_sp, tf->tf_iioq_head);
    471 		(void) trap_kdebug(T_IBREAK, 0, tf);
    472 		sanity_frame = NULL;
    473 		sanity_lwp = NULL;
    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 = "reserved";
    493 	int trapnum;
    494 #ifdef DIAGNOSTIC
    495 	extern int emergency_stack_start, emergency_stack_end;
    496 	struct cpu_info *ci = curcpu();
    497 	int oldcpl = ci->ci_cpl;
    498 #endif
    499 
    500 	trapnum = type & ~T_USER;
    501 	opcode = frame->tf_iir;
    502 
    503 	if (trapnum <= T_EXCEPTION || trapnum == T_HIGHERPL ||
    504 	    trapnum == T_LOWERPL || trapnum == T_TAKENBR ||
    505 	    trapnum == T_IDEBUG || trapnum == T_PERFMON) {
    506 		va = frame->tf_iioq_head;
    507 		space = frame->tf_iisq_head;
    508 		vftype = VM_PROT_EXECUTE;
    509 	} else {
    510 		va = frame->tf_ior;
    511 		space = frame->tf_isr;
    512 		vftype = inst_store(opcode) ? VM_PROT_WRITE : VM_PROT_READ;
    513 	}
    514 
    515 	KASSERT(curlwp != NULL);
    516 	l = curlwp;
    517 	p = l->l_proc;
    518 	if ((type & T_USER) != 0)
    519 		LWP_CACHE_CREDS(l, p);
    520 
    521 #ifdef DIAGNOSTIC
    522 	/*
    523 	 * If we are on the emergency stack, then we either got
    524 	 * a fault on the kernel stack, or we're just handling
    525 	 * a trap for the machine check handler (which also
    526 	 * runs on the emergency stack).
    527 	 *
    528 	 * We *very crudely* differentiate between the two cases
    529 	 * by checking the faulting instruction: if it is the
    530 	 * function prologue instruction that stores the old
    531 	 * frame pointer and updates the stack pointer, we assume
    532 	 * that we faulted on the kernel stack.
    533 	 *
    534 	 * In this case, not completing that instruction will
    535 	 * probably confuse backtraces in kgdb/ddb.  Completing
    536 	 * it would be difficult, because we already faulted on
    537 	 * that part of the stack, so instead we fix up the
    538 	 * frame as if the function called has just returned.
    539 	 * This has peculiar knowledge about what values are in
    540 	 * what registers during the "normal gcc -g" prologue.
    541 	 */
    542 	if (&type >= &emergency_stack_start &&
    543 	    &type < &emergency_stack_end &&
    544 	    type != T_IBREAK && STWM_R1_D_SR0_SP(opcode)) {
    545 		/* Restore the caller's frame pointer. */
    546 		frame->tf_r3 = frame->tf_r1;
    547 		/* Restore the caller's instruction offsets. */
    548 		frame->tf_iioq_head = frame->tf_rp;
    549 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    550 		goto dead_end;
    551 	}
    552 #endif /* DIAGNOSTIC */
    553 
    554 #ifdef DEBUG
    555 	frame_sanity_check(__func__, __LINE__, type, frame, l);
    556 #endif /* DEBUG */
    557 
    558 	if (frame->tf_flags & TFF_LAST)
    559 		l->l_md.md_regs = frame;
    560 
    561 	if (trapnum <= trap_types)
    562 		tts = trap_type[trapnum];
    563 
    564 #ifdef TRAPDEBUG
    565 	if (trapnum != T_INTERRUPT && trapnum != T_IBREAK)
    566 		printf("trap: %d, %s for %x:%lx at %x:%x, fp=%p, rp=%x\n",
    567 		    type, tts, space, va, frame->tf_iisq_head,
    568 		    frame->tf_iioq_head, frame, frame->tf_rp);
    569 	else if (trapnum == T_IBREAK)
    570 		printf("trap: break instruction %x:%x at %x:%x, fp=%p\n",
    571 		    break5(opcode), break13(opcode),
    572 		    frame->tf_iisq_head, frame->tf_iioq_head, frame);
    573 
    574 	{
    575 		extern int etext;
    576 		if (frame < (struct trapframe *)&etext) {
    577 			printf("trap: bogus frame ptr %p\n", frame);
    578 			goto dead_end;
    579 		}
    580 	}
    581 #endif
    582 
    583 	pcb = lwp_getpcb(l);
    584 
    585 	/* If this is a trap, not an interrupt, reenable interrupts. */
    586 	if (trapnum != T_INTERRUPT) {
    587 		uvmexp.traps++;
    588 		mtctl(frame->tf_eiem, CR_EIEM);
    589 	}
    590 
    591 	switch (type) {
    592 	case T_NONEXIST:
    593 	case T_NONEXIST|T_USER:
    594 #if !defined(DDB) && !defined(KGDB)
    595 		/* we've got screwed up by the central scrutinizer */
    596 		panic ("trap: elvis has just left the building!");
    597 		break;
    598 #else
    599 		goto dead_end;
    600 #endif
    601 	case T_RECOVERY|T_USER:
    602 #ifdef USERTRACE
    603 		for(;;) {
    604 			if (frame->tf_iioq_head != rctr_next_iioq)
    605 				printf("-%08x\nr %08x",
    606 					rctr_next_iioq - 4,
    607 					frame->tf_iioq_head);
    608 			rctr_next_iioq = frame->tf_iioq_head + 4;
    609 			if (frame->tf_ipsw & PSW_N) {
    610 				/* Advance the program counter. */
    611 				frame->tf_iioq_head = frame->tf_iioq_tail;
    612 				frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    613 				/* Clear flags. */
    614 				frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
    615 				/* Simulate another trap. */
    616 				continue;
    617 			}
    618 			break;
    619 		}
    620 		frame->tf_rctr = 0;
    621 		break;
    622 #endif /* USERTRACE */
    623 	case T_RECOVERY:
    624 #if !defined(DDB) && !defined(KGDB)
    625 		/* XXX will implement later */
    626 		printf ("trap: handicapped");
    627 		break;
    628 #else
    629 		goto dead_end;
    630 #endif
    631 
    632 	case T_EMULATION | T_USER:
    633 #ifdef FPEMUL
    634 		hppa_fpu_emulate(frame, l, opcode);
    635 #else  /* !FPEMUL */
    636 		/*
    637 		 * We don't have FPU emulation, so signal the
    638 		 * process with a SIGFPE.
    639 		 */
    640 
    641 		KSI_INIT_TRAP(&ksi);
    642 		ksi.ksi_signo = SIGFPE;
    643 		ksi.ksi_code = SI_NOINFO;
    644 		ksi.ksi_trap = type;
    645 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    646 		trapsignal(l, &ksi);
    647 #endif /* !FPEMUL */
    648 		break;
    649 
    650 	case T_DATALIGN:
    651 		onfault = pcb->pcb_onfault;
    652 		if (onfault) {
    653 			ret = EFAULT;
    654 do_onfault:
    655 			frame->tf_iioq_head = onfault;
    656 			frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    657 			frame->tf_ret0 = ret;
    658 			break;
    659 		}
    660 		/*FALLTHROUGH*/
    661 
    662 #ifdef DIAGNOSTIC
    663 		/* these just can't happen ever */
    664 	case T_PRIV_OP:
    665 	case T_PRIV_REG:
    666 		/* these just can't make it to the trap() ever */
    667 	case T_HPMC:
    668 	case T_HPMC | T_USER:
    669 	case T_EMULATION:
    670 	case T_EXCEPTION:
    671 #endif
    672 	case T_IBREAK:
    673 	case T_DBREAK:
    674 	dead_end:
    675 		if (type & T_USER) {
    676 #ifdef DEBUG
    677 			user_backtrace(frame, l, type);
    678 #endif
    679 			KSI_INIT_TRAP(&ksi);
    680 			ksi.ksi_signo = SIGILL;
    681 			ksi.ksi_code = ILL_ILLTRP;
    682 			ksi.ksi_trap = type;
    683 			ksi.ksi_addr = (void *)frame->tf_iioq_head;
    684 			trapsignal(l, &ksi);
    685 			break;
    686 		}
    687 		if (trap_kdebug(type, va, frame))
    688 			return;
    689 		else if (type == T_DATALIGN)
    690 			panic ("trap: %s at 0x%x", tts, (u_int) va);
    691 		else
    692 			panic ("trap: no debugger for \"%s\" (%d)", tts, type);
    693 		break;
    694 
    695 	case T_IBREAK | T_USER:
    696 	case T_DBREAK | T_USER:
    697 		KSI_INIT_TRAP(&ksi);
    698 		ksi.ksi_signo = SIGTRAP;
    699 		ksi.ksi_code = TRAP_TRACE;
    700 		ksi.ksi_trap = trapnum;
    701 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    702 #ifdef PTRACE
    703 		ss_clear_breakpoints(l);
    704 		if (opcode == SSBREAKPOINT)
    705 			ksi.ksi_code = TRAP_BRKPT;
    706 #endif
    707 		/* pass to user debugger */
    708 		trapsignal(l, &ksi);
    709 
    710 		break;
    711 
    712 #ifdef PTRACE
    713 	case T_TAKENBR | T_USER:
    714 		ss_clear_breakpoints(l);
    715 
    716 		KSI_INIT_TRAP(&ksi);
    717 		ksi.ksi_signo = SIGTRAP;
    718 		ksi.ksi_code = TRAP_TRACE;
    719 		ksi.ksi_trap = trapnum;
    720 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    721 
    722                 /* pass to user debugger */
    723 		trapsignal(l, &ksi);
    724 		break;
    725 #endif
    726 
    727 	case T_EXCEPTION | T_USER: {	/* co-proc assist trap */
    728 		uint64_t *fpp;
    729 		uint32_t *pex, ex, inst;
    730 		int i;
    731 
    732 		hppa_fpu_flush(l);
    733 		fpp = (uint64_t *)pcb->pcb_fpregs;
    734 
    735 		/* skip the status register */
    736 		pex = (uint32_t *)&fpp[0];
    737 		pex++;
    738 
    739 		/* loop through the exception registers */
    740 		for (i = 1; i < 8 && !*pex; i++, pex++)
    741 			;
    742 		KASSERT(i < 8);
    743 		ex = *pex;
    744 		*pex = 0;
    745 
    746 		/* reset the trap flag, as if there was none */
    747 		fpp[0] &= ~(((uint64_t)HPPA_FPU_T) << 32);
    748 
    749 		/* emulate the instruction */
    750 		inst = ((uint32_t)fpopmap[ex >> 26] << 26) | (ex & 0x03ffffff);
    751 		hppa_fpu_emulate(frame, l, inst);
    752 		}
    753 		break;
    754 
    755 	case T_OVERFLOW | T_USER:
    756 		KSI_INIT_TRAP(&ksi);
    757 		ksi.ksi_signo = SIGFPE;
    758 		ksi.ksi_code = SI_NOINFO;
    759 		ksi.ksi_trap = type;
    760 		ksi.ksi_addr = (void *)va;
    761 		trapsignal(l, &ksi);
    762 		break;
    763 
    764 	case T_CONDITION | T_USER:
    765 		KSI_INIT_TRAP(&ksi);
    766 		ksi.ksi_signo = SIGFPE;
    767 		ksi.ksi_code = FPE_INTDIV;
    768 		ksi.ksi_trap = type;
    769 		ksi.ksi_addr = (void *)va;
    770 		trapsignal(l, &ksi);
    771 		break;
    772 
    773 	case T_ILLEGAL | T_USER:
    774 #ifdef DEBUG
    775 		user_backtrace(frame, l, type);
    776 #endif
    777 		KSI_INIT_TRAP(&ksi);
    778 		ksi.ksi_signo = SIGILL;
    779 		ksi.ksi_code = ILL_ILLOPC;
    780 		ksi.ksi_trap = type;
    781 		ksi.ksi_addr = (void *)va;
    782 		trapsignal(l, &ksi);
    783 		break;
    784 
    785 	case T_PRIV_OP | T_USER:
    786 #ifdef DEBUG
    787 		user_backtrace(frame, l, type);
    788 #endif
    789 		KSI_INIT_TRAP(&ksi);
    790 		ksi.ksi_signo = SIGILL;
    791 		ksi.ksi_code = ILL_PRVOPC;
    792 		ksi.ksi_trap = type;
    793 		ksi.ksi_addr = (void *)va;
    794 		trapsignal(l, &ksi);
    795 		break;
    796 
    797 	case T_PRIV_REG | T_USER:
    798 #ifdef DEBUG
    799 		user_backtrace(frame, l, type);
    800 #endif
    801 		KSI_INIT_TRAP(&ksi);
    802 		ksi.ksi_signo = SIGILL;
    803 		ksi.ksi_code = ILL_PRVREG;
    804 		ksi.ksi_trap = type;
    805 		ksi.ksi_addr = (void *)va;
    806 		trapsignal(l, &ksi);
    807 		break;
    808 
    809 		/* these should never got here */
    810 	case T_HIGHERPL | T_USER:
    811 	case T_LOWERPL | T_USER:
    812 		KSI_INIT_TRAP(&ksi);
    813 		ksi.ksi_signo = SIGSEGV;
    814 		ksi.ksi_code = SEGV_ACCERR;
    815 		ksi.ksi_trap = type;
    816 		ksi.ksi_addr = (void *)va;
    817 		trapsignal(l, &ksi);
    818 		break;
    819 
    820 	case T_IPROT | T_USER:
    821 	case T_DPROT | T_USER:
    822 		KSI_INIT_TRAP(&ksi);
    823 		ksi.ksi_signo = SIGSEGV;
    824 		ksi.ksi_code = SEGV_ACCERR;
    825 		ksi.ksi_trap = type;
    826 		ksi.ksi_addr = (void *)va;
    827 		trapsignal(l, &ksi);
    828 		break;
    829 
    830 	case T_DATACC:   	case T_USER | T_DATACC:
    831 	case T_ITLBMISS:	case T_USER | T_ITLBMISS:
    832 	case T_DTLBMISS:	case T_USER | T_DTLBMISS:
    833 	case T_ITLBMISSNA:	case T_USER | T_ITLBMISSNA:
    834 	case T_DTLBMISSNA:	case T_USER | T_DTLBMISSNA:
    835 	case T_TLB_DIRTY:	case T_USER | T_TLB_DIRTY:
    836 		vm = p->p_vmspace;
    837 
    838 		if (!vm) {
    839 #ifdef TRAPDEBUG
    840 			printf("trap: no vm, p=%p\n", p);
    841 #endif
    842 			goto dead_end;
    843 		}
    844 
    845 		/*
    846 		 * it could be a kernel map for exec_map faults
    847 		 */
    848 		if (!(type & T_USER) && space == HPPA_SID_KERNEL)
    849 			map = kernel_map;
    850 		else {
    851 			map = &vm->vm_map;
    852 			if ((l->l_flag & LW_SA)
    853 			    && (~l->l_pflag & LP_SA_NOBLOCK)) {
    854 				l->l_savp->savp_faultaddr = va;
    855 				l->l_pflag |= LP_SA_PAGEFAULT;
    856 			}
    857 		}
    858 
    859 		va = trunc_page(va);
    860 
    861 		if (map->pmap->pm_space != space) {
    862 #ifdef TRAPDEBUG
    863 			printf("trap: space mismatch %d != %d\n",
    864 			    space, map->pmap->pm_space);
    865 #endif
    866 			/* actually dump the user, crap the kernel */
    867 			goto dead_end;
    868 		}
    869 
    870 		/* Never call uvm_fault in interrupt context. */
    871 		KASSERT(curcpu()->ci_cpl == 0);
    872 
    873 		onfault = pcb->pcb_onfault;
    874 		pcb->pcb_onfault = 0;
    875 		ret = uvm_fault(map, va, vftype);
    876 		pcb->pcb_onfault = onfault;
    877 
    878 #ifdef TRAPDEBUG
    879 		printf("uvm_fault(%p, %x, %d)=%d\n",
    880 		    map, (u_int)va, vftype, ret);
    881 #endif
    882 
    883 		if (map != kernel_map)
    884 			l->l_pflag &= ~LP_SA_PAGEFAULT;
    885 
    886 		/*
    887 		 * If this was a stack access we keep track of the maximum
    888 		 * accessed stack size.  Also, if uvm_fault gets a protection
    889 		 * failure it is due to accessing the stack region outside
    890 		 * the current limit and we need to reflect that as an access
    891 		 * error.
    892 		 */
    893 		if (map != kernel_map && va >= (vaddr_t)vm->vm_minsaddr) {
    894 			if (ret == 0)
    895 				uvm_grow(l->l_proc, va);
    896 			else if (ret == EACCES)
    897 				ret = EFAULT;
    898 		}
    899 
    900 		if (ret != 0) {
    901 			if (type & T_USER) {
    902 #ifdef DEBUG
    903 				user_backtrace(frame, l, type);
    904 #endif
    905 				KSI_INIT_TRAP(&ksi);
    906 				ksi.ksi_signo = SIGSEGV;
    907 				ksi.ksi_code = (ret == EACCES ?
    908 						SEGV_ACCERR : SEGV_MAPERR);
    909 				ksi.ksi_trap = type;
    910 				ksi.ksi_addr = (void *)va;
    911 				trapsignal(l, &ksi);
    912 			} else {
    913 				if (onfault) {
    914 					goto do_onfault;
    915 				}
    916 				panic("trap: uvm_fault(%p, %lx, %d): %d",
    917 				    map, va, vftype, ret);
    918 			}
    919 		}
    920 		break;
    921 
    922 	case T_DATALIGN | T_USER:
    923 #ifdef DEBUG
    924 		user_backtrace(frame, l, type);
    925 #endif
    926 		KSI_INIT_TRAP(&ksi);
    927 		ksi.ksi_signo = SIGBUS;
    928 		ksi.ksi_code = BUS_ADRALN;
    929 		ksi.ksi_trap = type;
    930 		ksi.ksi_addr = (void *)va;
    931 		trapsignal(l, &ksi);
    932 		break;
    933 
    934 	case T_INTERRUPT:
    935 	case T_INTERRUPT|T_USER:
    936 		hppa_intr(frame);
    937 		mtctl(frame->tf_eiem, CR_EIEM);
    938 		break;
    939 
    940 	case T_LOWERPL:
    941 	case T_DPROT:
    942 	case T_IPROT:
    943 	case T_OVERFLOW:
    944 	case T_CONDITION:
    945 	case T_ILLEGAL:
    946 	case T_HIGHERPL:
    947 	case T_TAKENBR:
    948 	case T_POWERFAIL:
    949 	case T_LPMC:
    950 	case T_PAGEREF:
    951 	case T_DATAPID:  	case T_DATAPID  | T_USER:
    952 		if (0 /* T-chip */) {
    953 			break;
    954 		}
    955 		/* FALLTHROUGH to unimplemented */
    956 	default:
    957 		panic ("trap: unimplemented \'%s\' (%d)", tts, type);
    958 	}
    959 
    960 #ifdef DIAGNOSTIC
    961 	if (ci->ci_cpl != oldcpl)
    962 		printf("WARNING: SPL (%d) NOT LOWERED ON TRAP (%d) EXIT\n",
    963 		    ci->ci_cpl, trapnum);
    964 #endif
    965 
    966 	if (type & T_USER)
    967 		userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    968 
    969 #ifdef DEBUG
    970 	frame_sanity_check(__func__, __LINE__, type, frame, l);
    971 	if (frame->tf_flags & TFF_LAST && (curlwp->l_flag & LW_IDLE) == 0)
    972 		frame_sanity_check(__func__, __LINE__, type,
    973 		    curlwp->l_md.md_regs, curlwp);
    974 #endif /* DEBUG */
    975 }
    976 
    977 void
    978 child_return(void *arg)
    979 {
    980 	struct lwp *l = arg;
    981 
    982 	/*
    983 	 * Return values in the frame set by cpu_lwp_fork().
    984 	 */
    985 
    986 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    987 	ktrsysret(SYS_fork, 0, 0);
    988 #ifdef DEBUG
    989 	frame_sanity_check(__func__, __LINE__, 0, l->l_md.md_regs, l);
    990 #endif /* DEBUG */
    991 }
    992 
    993 #ifdef PTRACE
    994 
    995 #include <sys/ptrace.h>
    996 
    997 int
    998 ss_get_value(struct lwp *l, vaddr_t addr, u_int *value)
    999 {
   1000 	struct uio uio;
   1001 	struct iovec iov;
   1002 
   1003 	iov.iov_base = (void *)value;
   1004 	iov.iov_len = sizeof(u_int);
   1005 	uio.uio_iov = &iov;
   1006 	uio.uio_iovcnt = 1;
   1007 	uio.uio_offset = (off_t)addr;
   1008 	uio.uio_resid = sizeof(u_int);
   1009 	uio.uio_rw = UIO_READ;
   1010 	UIO_SETUP_SYSSPACE(&uio);
   1011 
   1012 	return (process_domem(curlwp, l, &uio));
   1013 }
   1014 
   1015 int
   1016 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
   1017 {
   1018 	struct uio uio;
   1019 	struct iovec iov;
   1020 
   1021 	iov.iov_base = (void *)&value;
   1022 	iov.iov_len = sizeof(u_int);
   1023 	uio.uio_iov = &iov;
   1024 	uio.uio_iovcnt = 1;
   1025 	uio.uio_offset = (off_t)addr;
   1026 	uio.uio_resid = sizeof(u_int);
   1027 	uio.uio_rw = UIO_WRITE;
   1028 	UIO_SETUP_SYSSPACE(&uio);
   1029 
   1030 	return (process_domem(curlwp, l, &uio));
   1031 }
   1032 
   1033 void
   1034 ss_clear_breakpoints(struct lwp *l)
   1035 {
   1036 	/* Restore origional instructions. */
   1037 	if (l->l_md.md_bpva != 0) {
   1038 		ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
   1039 		ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
   1040 		l->l_md.md_bpva = 0;
   1041 	}
   1042 }
   1043 
   1044 
   1045 int
   1046 process_sstep(struct lwp *l, int sstep)
   1047 {
   1048 	struct trapframe *tf = l->l_md.md_regs;
   1049 	int error;
   1050 
   1051 	ss_clear_breakpoints(l);
   1052 
   1053 	/* We're continuing... */
   1054 	/* Don't touch the syscall gateway page. */
   1055 	/* XXX head */
   1056 	if (sstep == 0 ||
   1057 	    (tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE) {
   1058 		tf->tf_ipsw &= ~PSW_T;
   1059 		return 0;
   1060 	}
   1061 
   1062 	l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
   1063 
   1064 	/*
   1065 	 * Insert two breakpoint instructions; the first one might be
   1066 	 * nullified.  Of course we need to save two instruction
   1067 	 * first.
   1068 	 */
   1069 
   1070 	error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
   1071 	if (error)
   1072 		return (error);
   1073 	error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
   1074 	if (error)
   1075 		return (error);
   1076 
   1077 	error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
   1078 	if (error)
   1079 		return error;
   1080 	error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
   1081 	if (error)
   1082 		return error;
   1083 
   1084 	tf->tf_ipsw |= PSW_T;
   1085 
   1086 	return 0;
   1087 }
   1088 #endif
   1089 
   1090 
   1091 /*
   1092  * call actual syscall routine
   1093  * from the low-level syscall handler:
   1094  * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
   1095  *   our stack, this wins compared to copyin just needed amount anyway
   1096  * - register args are copied onto stack too
   1097  */
   1098 void
   1099 syscall(struct trapframe *frame, int *args)
   1100 {
   1101 	struct lwp *l;
   1102 	struct proc *p;
   1103 	const struct sysent *callp;
   1104 	size_t nargs64;
   1105 	int nsys, code, error;
   1106 	int tmp;
   1107 	int rval[2];
   1108 #ifdef DIAGNOSTIC
   1109 	struct cpu_info *ci = curcpu();
   1110 	int oldcpl = ci->ci_cpl;
   1111 #endif
   1112 
   1113 	uvmexp.syscalls++;
   1114 
   1115 #ifdef DEBUG
   1116 	frame_sanity_check(__func__, __LINE__, 0, frame, curlwp);
   1117 #endif /* DEBUG */
   1118 
   1119 	if (!USERMODE(frame->tf_iioq_head))
   1120 		panic("syscall");
   1121 
   1122 	KASSERT(curlwp != NULL);
   1123 	l = curlwp;
   1124 	p = l->l_proc;
   1125 	l->l_md.md_regs = frame;
   1126 	nsys = p->p_emul->e_nsysent;
   1127 	callp = p->p_emul->e_sysent;
   1128 	code = frame->tf_t1;
   1129 	LWP_CACHE_CREDS(l, p);
   1130 
   1131 #ifdef KERN_SA
   1132 	if (__predict_false((l->l_savp)
   1133             && (l->l_savp->savp_pflags & SAVP_FLAG_DELIVERING)))
   1134 		l->l_savp->savp_pflags &= ~SAVP_FLAG_DELIVERING;
   1135 #endif
   1136 
   1137 	/*
   1138 	 * Restarting a system call is touchy on the HPPA, because syscall
   1139 	 * arguments are passed in registers and the program counter of the
   1140 	 * syscall "point" isn't easily divined.
   1141 	 *
   1142 	 * We handle the first problem by assuming that we will have to restart
   1143 	 * this system call, so we stuff the first four words of the original
   1144 	 * arguments back into the frame as arg0...arg3, which is where we
   1145 	 * found them in the first place.  Any further arguments are (still) on
   1146 	 * the user's stack and the  syscall code will fetch them from there
   1147 	 * (again).
   1148 	 *
   1149 	 * The program counter problem is addressed below.
   1150 	 */
   1151 	frame->tf_arg0 = args[0];
   1152 	frame->tf_arg1 = args[1];
   1153 	frame->tf_arg2 = args[2];
   1154 	frame->tf_arg3 = args[3];
   1155 
   1156 	/*
   1157 	 * Some special handling for the syscall(2) and
   1158 	 * __syscall(2) system calls.
   1159 	 */
   1160 	switch (code) {
   1161 	case SYS_syscall:
   1162 		code = *args;
   1163 		args += 1;
   1164 		break;
   1165 	case SYS___syscall:
   1166 		if (callp != sysent)
   1167 			break;
   1168 		/*
   1169 		 * NB: even though __syscall(2) takes a quad_t containing the
   1170 		 * system call number, because our argument copying word-swaps
   1171 		 * 64-bit arguments, the least significant word of that quad_t
   1172 		 * is the first word in the argument array.
   1173 		 */
   1174 		code = *args;
   1175 		args += 2;
   1176 	}
   1177 
   1178 	/*
   1179 	 * Stacks growing from lower addresses to higher addresses are not
   1180 	 * really such a good idea, because it makes it impossible to overlay a
   1181 	 * struct on top of C stack arguments (the arguments appear in
   1182 	 * reversed order).
   1183 	 *
   1184 	 * You can do the obvious thing (as locore.S does) and copy argument
   1185 	 * words one by one, laying them out in the "right" order in the dest-
   1186 	 * ination buffer, but this ends up word-swapping multi-word arguments
   1187 	 * (like off_t).
   1188 	 *
   1189 	 * FIXME - this works only on native binaries and
   1190 	 * will probably screw up any and all emulation.
   1191 	 *
   1192 	 */
   1193 
   1194 	if (code < 0 || code >= nsys)
   1195 		callp += p->p_emul->e_nosys;	/* bad syscall # */
   1196 	else
   1197 		callp += code;
   1198 
   1199 	nargs64 = SYCALL_NARGS64(callp);
   1200 	if (nargs64 != 0) {
   1201 		size_t nargs = callp->sy_narg;
   1202 
   1203 		for (size_t i = 0; i < nargs + nargs64;) {
   1204 			if (SYCALL_ARG_64_P(callp, i)) {
   1205 				tmp = args[i];
   1206 				args[i] = args[i + 1];
   1207 				args[i + 1] = tmp;
   1208 				i += 2;
   1209 			} else
   1210 				i++;
   1211 		}
   1212 	}
   1213 
   1214 #ifdef USERTRACE
   1215 	if (0) {
   1216 		user_backtrace(frame, l, -1);
   1217 		frame->tf_ipsw |= PSW_R;
   1218 		frame->tf_rctr = 0;
   1219 		printf("r %08x", frame->tf_iioq_head);
   1220 		rctr_next_iioq = frame->tf_iioq_head + 4;
   1221 	}
   1222 #endif
   1223 
   1224 	error = 0;
   1225 	if (__predict_false(p->p_trace_enabled)) {
   1226 		error = trace_enter(code, args, callp->sy_narg);
   1227 		if (error)
   1228 			goto out;
   1229 	}
   1230 
   1231 	rval[0] = 0;
   1232 	rval[1] = 0;
   1233 	error = sy_call(callp, l, args, rval);
   1234 out:
   1235 	switch (error) {
   1236 	case 0:
   1237 		l = curlwp;			/* changes on exec() */
   1238 		frame = l->l_md.md_regs;
   1239 		frame->tf_ret0 = rval[0];
   1240 		frame->tf_ret1 = rval[1];
   1241 		frame->tf_t1 = 0;
   1242 		break;
   1243 	case ERESTART:
   1244 		/*
   1245 		 * Now we have to wind back the instruction offset queue to the
   1246 		 * point where the system call will be made again.  This is
   1247 		 * inherently tied to the SYSCALL macro.
   1248 		 *
   1249 		 * Currently, the part of the SYSCALL macro that we want to re-
   1250 		 * run reads as:
   1251 		 *
   1252 		 *	ldil	L%SYSCALLGATE, r1
   1253 		 *	ble	4(sr7, r1)
   1254 		 *	ldi	__CONCAT(SYS_,x), t1
   1255 		 *	comb,<>	%r0, %t1, __cerror
   1256 		 *
   1257 		 * And our offset queue head points to the comb instruction.
   1258 		 * So we need to subtract twelve to reach the ldil.
   1259 		 */
   1260 		frame->tf_iioq_head -= 12;
   1261 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
   1262 		break;
   1263 	case EJUSTRETURN:
   1264 		p = curproc;
   1265 		break;
   1266 	default:
   1267 		if (p->p_emul->e_errno)
   1268 			error = p->p_emul->e_errno[error];
   1269 		frame->tf_t1 = error;
   1270 		break;
   1271 	}
   1272 
   1273 	if (__predict_false(p->p_trace_enabled))
   1274 		trace_exit(code, rval, error);
   1275 
   1276 	userret(l, frame->tf_iioq_head, 0);
   1277 
   1278 #ifdef DIAGNOSTIC
   1279 	if (ci->ci_cpl != oldcpl) {
   1280 		printf("WARNING: SPL (0x%x) NOT LOWERED ON "
   1281 		    "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
   1282 		    ci->ci_cpl, code, args[0], args[1], args[2], p->p_pid);
   1283 		ci->ci_cpl = oldcpl;
   1284 	}
   1285 #endif
   1286 
   1287 #ifdef DEBUG
   1288 	frame_sanity_check(__func__, __LINE__, 0, frame, l);
   1289 #endif /* DEBUG */
   1290 }
   1291 
   1292 /*
   1293  * Start a new LWP
   1294  */
   1295 void
   1296 startlwp(void *arg)
   1297 {
   1298 	ucontext_t *uc = arg;
   1299 	lwp_t *l = curlwp;
   1300 	int error;
   1301 
   1302 	error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
   1303 	KASSERT(error == 0);
   1304 
   1305 	kmem_free(uc, sizeof(ucontext_t));
   1306 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1307 }
   1308 
   1309 /*
   1310  * XXX This is a terrible name.
   1311  */
   1312 void
   1313 upcallret(struct lwp *l)
   1314 {
   1315 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1316 }
   1317