Home | History | Annotate | Line # | Download | only in hppa
trap.c revision 1.106
      1 /*	$NetBSD: trap.c,v 1.106 2014/03/06 19:02:58 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.106 2014/03/06 19:02:58 skrll Exp $");
     62 
     63 /* #define INTRDEBUG */
     64 /* #define TRAPDEBUG */
     65 /* #define USERTRACE */
     66 
     67 #include "opt_kgdb.h"
     68 #include "opt_ptrace.h"
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/kernel.h>
     73 #include <sys/syscall.h>
     74 #include <sys/syscallvar.h>
     75 #include <sys/mutex.h>
     76 #include <sys/ktrace.h>
     77 #include <sys/proc.h>
     78 #include <sys/signalvar.h>
     79 #include <sys/acct.h>
     80 #include <sys/signal.h>
     81 #include <sys/device.h>
     82 #include <sys/kmem.h>
     83 #include <sys/userret.h>
     84 
     85 #include <net/netisr.h>
     86 
     87 #ifdef KGDB
     88 #include <sys/kgdb.h>
     89 #endif
     90 
     91 #include <uvm/uvm.h>
     92 
     93 #include <machine/iomod.h>
     94 #include <machine/cpufunc.h>
     95 #include <machine/reg.h>
     96 #include <machine/autoconf.h>
     97 
     98 #include <machine/db_machdep.h>
     99 
    100 #include <hppa/hppa/machdep.h>
    101 
    102 #include <ddb/db_output.h>
    103 #include <ddb/db_interface.h>
    104 
    105 #ifdef PTRACE
    106 void ss_clear_breakpoints(struct lwp *l);
    107 int ss_put_value(struct lwp *, vaddr_t, u_int);
    108 int ss_get_value(struct lwp *, vaddr_t, u_int *);
    109 
    110 /* single-step breakpoint */
    111 #define SSBREAKPOINT   (HPPA_BREAK_KERNEL | (HPPA_BREAK_SS << 13))
    112 
    113 #endif
    114 
    115 #if defined(DEBUG) || defined(DIAGNOSTIC)
    116 /*
    117  * 0x6fc1000 is a stwm r1, d(sr0, sp), which is the last
    118  * instruction in the function prologue that gcc -O0 uses.
    119  * When we have this instruction we know the relationship
    120  * between the stack pointer and the gcc -O0 frame pointer
    121  * (in r3, loaded with the initial sp) for the body of a
    122  * function.
    123  *
    124  * If the given instruction is a stwm r1, d(sr0, sp) where
    125  * d > 0, we evaluate to d, else we evaluate to zero.
    126  */
    127 #define STWM_R1_D_SR0_SP(inst) \
    128 	(((inst) & 0xffffc001) == 0x6fc10000 ? (((inst) & 0x00003ff) >> 1) : 0)
    129 #endif /* DEBUG || DIAGNOSTIC */
    130 
    131 const char *trap_type[] = {
    132 	"invalid",
    133 	"HPMC",
    134 	"power failure",
    135 	"recovery counter",
    136 	"external interrupt",
    137 	"LPMC",
    138 	"ITLB miss fault",
    139 	"instruction protection",
    140 	"Illegal instruction",
    141 	"break instruction",
    142 	"privileged operation",
    143 	"privileged register",
    144 	"overflow",
    145 	"conditional",
    146 	"assist exception",
    147 	"DTLB miss",
    148 	"ITLB non-access miss",
    149 	"DTLB non-access miss",
    150 	"data protection/rights/alignment",
    151 	"data break",
    152 	"TLB dirty",
    153 	"page reference",
    154 	"assist emulation",
    155 	"higher-priv transfer",
    156 	"lower-priv transfer",
    157 	"taken branch",
    158 	"data access rights",
    159 	"data protection",
    160 	"unaligned data ref",
    161 };
    162 int trap_types = __arraycount(trap_type);
    163 
    164 uint8_t fpopmap[] = {
    165 	0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
    166 	0x00, 0x0c, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00,
    167 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    168 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    169 	0x00, 0x00, 0x00, 0x26, 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, 0x00, 0x00, 0x00, 0x00, 0x00,
    173 };
    174 
    175 void pmap_hptdump(void);
    176 void syscall(struct trapframe *, int *);
    177 
    178 #if defined(DEBUG)
    179 struct trapframe *sanity_frame;
    180 struct lwp *sanity_lwp;
    181 const char *sanity_string;
    182 void frame_sanity_check(const char *, int, int, struct trapframe *,
    183     struct lwp *);
    184 #endif
    185 
    186 
    187 #ifdef USERTRACE
    188 /*
    189  * USERTRACE is a crude facility that traces the PC of a single user process.
    190  * This tracing is normally activated by the dispatching of a certain syscall
    191  * with certain arguments - see the activation code in syscall().
    192  */
    193 static void user_backtrace(struct trapframe *, struct lwp *, int);
    194 static void user_backtrace_raw(u_int, u_int);
    195 
    196 u_int rctr_next_iioq;
    197 #endif
    198 
    199 static inline void
    200 userret(struct lwp *l, register_t pc, u_quad_t oticks)
    201 {
    202 	struct proc *p = l->l_proc;
    203 
    204 	if (l->l_md.md_astpending) {
    205 		l->l_md.md_astpending = 0;
    206 		//curcpu()->ci_data.cpu_nast++;
    207 
    208 		if (curcpu()->ci_want_resched)
    209 			preempt();
    210 	}
    211 
    212 	mi_userret(l);
    213 
    214 	/*
    215 	 * If profiling, charge recent system time to the trapped pc.
    216 	 */
    217 	if (p->p_stflag & PST_PROFIL) {
    218 		extern int psratio;
    219 
    220 		addupc_task(l, pc, (int)(p->p_sticks - oticks) * psratio);
    221 	}
    222 }
    223 
    224 /*
    225  * This handles some messy kernel debugger details.
    226  * It dispatches into either kgdb or DDB, and knows
    227  * about some special things to do, like skipping over
    228  * break instructions and how to really set up for
    229  * a single-step.
    230  */
    231 #if defined(KGDB) || defined(DDB)
    232 static int
    233 trap_kdebug(int type, int code, struct trapframe *frame)
    234 {
    235 	int handled;
    236 	u_int tf_iioq_head_old;
    237 	u_int tf_iioq_tail_old;
    238 
    239 	for (;;) {
    240 
    241 		/* This trap has not been handled. */
    242 		handled = 0;
    243 
    244 		/* Remember the instruction offset queue. */
    245 		tf_iioq_head_old = frame->tf_iioq_head;
    246 		tf_iioq_tail_old = frame->tf_iioq_tail;
    247 
    248 #ifdef	KGDB
    249 		/* Let KGDB handle it (if connected) */
    250 		if (!handled)
    251 			handled = kgdb_trap(type, frame);
    252 #endif
    253 #ifdef	DDB
    254 		/* Let DDB handle it. */
    255 		if (!handled)
    256 			handled = kdb_trap(type, code, frame);
    257 #endif
    258 
    259 		/* If this trap wasn't handled, return now. */
    260 		if (!handled)
    261 			return(0);
    262 
    263 		/*
    264 		 * If the instruction offset queue head changed, but the offset
    265 		 * queue tail didn't, assume that the user wants to jump to the
    266 		 * head offset, and adjust the tail accordingly.  This should
    267 		 * fix the kgdb `jump' command, and can help DDB users who `set'
    268 		 * the offset head but forget the tail.
    269 		 */
    270 		if (frame->tf_iioq_head != tf_iioq_head_old &&
    271 		    frame->tf_iioq_tail == tf_iioq_tail_old)
    272 			frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    273 
    274 		/*
    275 		 * This is some single-stepping support.  If we're trying to
    276 		 * step through a nullified instruction, just advance by hand
    277 		 * and trap again.  Otherwise, load the recovery counter with
    278 		 * zero.
    279 		 */
    280 		if (frame->tf_ipsw & PSW_R) {
    281 #ifdef TRAPDEBUG
    282 			printf("(single stepping at head 0x%x tail 0x%x)\n",
    283 			    frame->tf_iioq_head, frame->tf_iioq_tail);
    284 #endif
    285 			if (frame->tf_ipsw & PSW_N) {
    286 #ifdef TRAPDEBUG
    287 				printf("(single stepping past nullified)\n");
    288 #endif
    289 
    290 				/* Advance the program counter. */
    291 				frame->tf_iioq_head = frame->tf_iioq_tail;
    292 				frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    293 
    294 				/* Clear flags. */
    295 				frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
    296 
    297 				/* Simulate another trap. */
    298 				type = T_RECOVERY;
    299 				continue;
    300 			}
    301 			frame->tf_rctr = 0;
    302 		}
    303 
    304 		/* We handled this trap. */
    305 		return (1);
    306 	}
    307 	/* NOTREACHED */
    308 }
    309 #else	/* !KGDB && !DDB */
    310 #define trap_kdebug(t, c, f)	(0)
    311 #endif	/* !KGDB && !DDB */
    312 
    313 #if defined(DEBUG) || defined(USERTRACE)
    314 /*
    315  * These functions give a crude usermode backtrace.  They really only work when
    316  * code has been compiled without optimization, as they assume a certain func-
    317  * tion prologue sets up a frame pointer and stores the return pointer and arg-
    318  * uments in it.
    319  */
    320 static void
    321 user_backtrace_raw(u_int pc, u_int fp)
    322 {
    323 	int frame_number;
    324 	int arg_number;
    325 
    326 	for (frame_number = 0;
    327 	     frame_number < 100 && pc > HPPA_PC_PRIV_MASK && fp;
    328 	     frame_number++) {
    329 
    330 		printf("%3d: pc=%08x%s fp=0x%08x", frame_number,
    331 		    pc & ~HPPA_PC_PRIV_MASK, USERMODE(pc) ? "  " : "**", fp);
    332 		for (arg_number = 0; arg_number < 4; arg_number++)
    333 			printf(" arg%d=0x%08x", arg_number,
    334 			    (int) fuword(HPPA_FRAME_CARG(arg_number, fp)));
    335 		printf("\n");
    336                 pc = fuword(((register_t *) fp) - 5);	/* fetch rp */
    337 		if (pc == -1) {
    338 			printf("  fuword for pc failed\n");
    339 			break;
    340 		}
    341                 fp = fuword(((register_t *) fp) + 0);	/* fetch previous fp */
    342 		if (fp == -1) {
    343 			printf("  fuword for fp failed\n");
    344 			break;
    345 		}
    346 	}
    347 	printf("  backtrace stopped with pc %08x fp 0x%08x\n", pc, fp);
    348 }
    349 
    350 static void
    351 user_backtrace(struct trapframe *tf, struct lwp *l, int type)
    352 {
    353 	struct proc *p = l->l_proc;
    354 	u_int pc, fp, inst;
    355 
    356 	/*
    357 	 * Display any trap type that we have.
    358 	 */
    359 	if (type >= 0)
    360 		printf("pid %d (%s) trap #%d\n",
    361 		    p->p_pid, p->p_comm, type & ~T_USER);
    362 
    363 	/*
    364 	 * Assuming that the frame pointer in r3 is valid,
    365 	 * dump out a stack trace.
    366 	 */
    367 	fp = tf->tf_r3;
    368 	printf("pid %d (%s) backtrace, starting with fp 0x%08x\n",
    369 		p->p_pid, p->p_comm, fp);
    370 	user_backtrace_raw(tf->tf_iioq_head, fp);
    371 
    372 	/*
    373 	 * In case the frame pointer in r3 is not valid, assuming the stack
    374 	 * pointer is valid and the faulting function is a non-leaf, if we can
    375 	 * find its prologue we can recover its frame pointer.
    376 	 */
    377 	pc = tf->tf_iioq_head;
    378 	fp = tf->tf_sp - HPPA_FRAME_SIZE;
    379 	printf("pid %d (%s) backtrace, starting with sp 0x%08x pc 0x%08x\n",
    380 	    p->p_pid, p->p_comm, tf->tf_sp, pc);
    381 	for (pc &= ~HPPA_PC_PRIV_MASK; pc > 0; pc -= sizeof(inst)) {
    382 		inst = fuword((register_t *) pc);
    383 		if (inst == -1) {
    384 			printf("  fuword for inst at pc %08x failed\n", pc);
    385 			break;
    386 		}
    387 		/* Check for the prologue instruction that sets sp. */
    388 		if (STWM_R1_D_SR0_SP(inst)) {
    389 			fp = tf->tf_sp - STWM_R1_D_SR0_SP(inst);
    390 			printf("  sp from fp at pc %08x: %08x\n", pc, inst);
    391 			break;
    392 		}
    393 	}
    394 	user_backtrace_raw(tf->tf_iioq_head, fp);
    395 }
    396 #endif /* DEBUG || USERTRACE */
    397 
    398 #ifdef DEBUG
    399 /*
    400  * This sanity-checks a trapframe.  It is full of various assumptions about
    401  * what a healthy CPU state should be, with some documented elsewhere, some not.
    402  */
    403 void
    404 frame_sanity_check(const char *func, int line, int type, struct trapframe *tf,
    405     struct lwp *l)
    406 {
    407 #if 0
    408 	extern int kernel_text;
    409 	extern int etext;
    410 #endif
    411 	struct cpu_info *ci = curcpu();
    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 & ci->ci_psw) == ci->ci_psw);
    424 	SANITY((ci->ci_psw & 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 #if 0
    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 #endif
    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 		curcpu()->ci_data.cpu_ntrap++;
    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 		hppa_fpu_emulate(frame, l, opcode);
    635 		break;
    636 
    637 	case T_DATALIGN:
    638 		onfault = pcb->pcb_onfault;
    639 		if (onfault) {
    640 			ret = EFAULT;
    641 do_onfault:
    642 			frame->tf_iioq_head = onfault;
    643 			frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    644 			frame->tf_ret0 = ret;
    645 			break;
    646 		}
    647 		/*FALLTHROUGH*/
    648 
    649 #ifdef DIAGNOSTIC
    650 		/* these just can't happen ever */
    651 	case T_PRIV_OP:
    652 	case T_PRIV_REG:
    653 		/* these just can't make it to the trap() ever */
    654 	case T_HPMC:
    655 	case T_HPMC | T_USER:
    656 	case T_EMULATION:
    657 	case T_EXCEPTION:
    658 #endif
    659 	case T_IBREAK:
    660 	case T_DBREAK:
    661 	dead_end:
    662 		if (type & T_USER) {
    663 #ifdef DEBUG
    664 			user_backtrace(frame, l, type);
    665 #endif
    666 			KSI_INIT_TRAP(&ksi);
    667 			ksi.ksi_signo = SIGILL;
    668 			ksi.ksi_code = ILL_ILLTRP;
    669 			ksi.ksi_trap = type;
    670 			ksi.ksi_addr = (void *)frame->tf_iioq_head;
    671 			trapsignal(l, &ksi);
    672 			break;
    673 		}
    674 		if (trap_kdebug(type, va, frame))
    675 			return;
    676 		else if (type == T_DATALIGN)
    677 			panic ("trap: %s at 0x%x", tts, (u_int) va);
    678 		else
    679 			panic ("trap: no debugger for \"%s\" (%d)", tts, type);
    680 		break;
    681 
    682 	case T_IBREAK | T_USER:
    683 	case T_DBREAK | T_USER:
    684 		KSI_INIT_TRAP(&ksi);
    685 		ksi.ksi_signo = SIGTRAP;
    686 		ksi.ksi_code = TRAP_TRACE;
    687 		ksi.ksi_trap = trapnum;
    688 		ksi.ksi_addr = (void *)(frame->tf_iioq_head & ~HPPA_PC_PRIV_MASK);
    689 #ifdef PTRACE
    690 		ss_clear_breakpoints(l);
    691 		if (opcode == SSBREAKPOINT)
    692 			ksi.ksi_code = TRAP_BRKPT;
    693 #endif
    694 		/* pass to user debugger */
    695 		trapsignal(l, &ksi);
    696 		break;
    697 
    698 #ifdef PTRACE
    699 	case T_TAKENBR | T_USER:
    700 		ss_clear_breakpoints(l);
    701 
    702 		KSI_INIT_TRAP(&ksi);
    703 		ksi.ksi_signo = SIGTRAP;
    704 		ksi.ksi_code = TRAP_TRACE;
    705 		ksi.ksi_trap = trapnum;
    706 		ksi.ksi_addr = (void *)(frame->tf_iioq_head & ~HPPA_PC_PRIV_MASK);
    707 
    708                 /* pass to user debugger */
    709 		trapsignal(l, &ksi);
    710 		break;
    711 #endif
    712 
    713 	case T_EXCEPTION | T_USER: {	/* co-proc assist trap */
    714 		uint64_t *fpp;
    715 		uint32_t *pex, ex, inst;
    716 		int i;
    717 
    718 		hppa_fpu_flush(l);
    719 		fpp = (uint64_t *)pcb->pcb_fpregs;
    720 
    721 		/* skip the status register */
    722 		pex = (uint32_t *)&fpp[0];
    723 		pex++;
    724 
    725 		/* loop through the exception registers */
    726 		for (i = 1; i < 8 && !*pex; i++, pex++)
    727 			;
    728 		KASSERT(i < 8);
    729 		ex = *pex;
    730 		*pex = 0;
    731 
    732 		/* reset the trap flag, as if there was none */
    733 		fpp[0] &= ~(((uint64_t)HPPA_FPU_T) << 32);
    734 
    735 		/* emulate the instruction */
    736 		inst = ((uint32_t)fpopmap[ex >> 26] << 26) | (ex & 0x03ffffff);
    737 		hppa_fpu_emulate(frame, l, inst);
    738 		}
    739 		break;
    740 
    741 	case T_OVERFLOW | T_USER:
    742 		KSI_INIT_TRAP(&ksi);
    743 		ksi.ksi_signo = SIGFPE;
    744 		ksi.ksi_code = SI_NOINFO;
    745 		ksi.ksi_trap = type;
    746 		ksi.ksi_addr = (void *)va;
    747 		trapsignal(l, &ksi);
    748 		break;
    749 
    750 	case T_CONDITION | T_USER:
    751 		KSI_INIT_TRAP(&ksi);
    752 		ksi.ksi_signo = SIGFPE;
    753 		ksi.ksi_code = FPE_INTDIV;
    754 		ksi.ksi_trap = type;
    755 		ksi.ksi_addr = (void *)va;
    756 		trapsignal(l, &ksi);
    757 		break;
    758 
    759 	case T_ILLEGAL | T_USER:
    760 #ifdef DEBUG
    761 		user_backtrace(frame, l, type);
    762 #endif
    763 		KSI_INIT_TRAP(&ksi);
    764 		ksi.ksi_signo = SIGILL;
    765 		ksi.ksi_code = ILL_ILLOPC;
    766 		ksi.ksi_trap = type;
    767 		ksi.ksi_addr = (void *)va;
    768 		trapsignal(l, &ksi);
    769 		break;
    770 
    771 	case T_PRIV_OP | T_USER:
    772 #ifdef DEBUG
    773 		user_backtrace(frame, l, type);
    774 #endif
    775 		KSI_INIT_TRAP(&ksi);
    776 		ksi.ksi_signo = SIGILL;
    777 		ksi.ksi_code = ILL_PRVOPC;
    778 		ksi.ksi_trap = type;
    779 		ksi.ksi_addr = (void *)va;
    780 		trapsignal(l, &ksi);
    781 		break;
    782 
    783 	case T_PRIV_REG | T_USER:
    784 #ifdef DEBUG
    785 		user_backtrace(frame, l, type);
    786 #endif
    787 		KSI_INIT_TRAP(&ksi);
    788 		ksi.ksi_signo = SIGILL;
    789 		ksi.ksi_code = ILL_PRVREG;
    790 		ksi.ksi_trap = type;
    791 		ksi.ksi_addr = (void *)va;
    792 		trapsignal(l, &ksi);
    793 		break;
    794 
    795 		/* these should never got here */
    796 	case T_HIGHERPL | T_USER:
    797 	case T_LOWERPL | T_USER:
    798 		KSI_INIT_TRAP(&ksi);
    799 		ksi.ksi_signo = SIGSEGV;
    800 		ksi.ksi_code = SEGV_ACCERR;
    801 		ksi.ksi_trap = type;
    802 		ksi.ksi_addr = (void *)va;
    803 		trapsignal(l, &ksi);
    804 		break;
    805 
    806 	case T_IPROT | T_USER:
    807 	case T_DPROT | T_USER:
    808 		KSI_INIT_TRAP(&ksi);
    809 		ksi.ksi_signo = SIGSEGV;
    810 		ksi.ksi_code = SEGV_ACCERR;
    811 		ksi.ksi_trap = type;
    812 		ksi.ksi_addr = (void *)va;
    813 		trapsignal(l, &ksi);
    814 		break;
    815 
    816 	case T_DATACC:   	case T_USER | T_DATACC:
    817 	case T_ITLBMISS:	case T_USER | T_ITLBMISS:
    818 	case T_DTLBMISS:	case T_USER | T_DTLBMISS:
    819 	case T_ITLBMISSNA:	case T_USER | T_ITLBMISSNA:
    820 	case T_DTLBMISSNA:	case T_USER | T_DTLBMISSNA:
    821 	case T_TLB_DIRTY:	case T_USER | T_TLB_DIRTY:
    822 		vm = p->p_vmspace;
    823 
    824 		if (!vm) {
    825 #ifdef TRAPDEBUG
    826 			printf("trap: no vm, p=%p\n", p);
    827 #endif
    828 			goto dead_end;
    829 		}
    830 
    831 		/*
    832 		 * it could be a kernel map for exec_map faults
    833 		 */
    834 		if (!(type & T_USER) && space == HPPA_SID_KERNEL)
    835 			map = kernel_map;
    836 		else {
    837 			map = &vm->vm_map;
    838 		}
    839 
    840 		va = trunc_page(va);
    841 
    842 		if (map->pmap->pm_space != space) {
    843 #ifdef TRAPDEBUG
    844 			printf("trap: space mismatch %d != %d\n",
    845 			    space, map->pmap->pm_space);
    846 #endif
    847 			/* actually dump the user, crap the kernel */
    848 			goto dead_end;
    849 		}
    850 
    851 		/* Never call uvm_fault in interrupt context. */
    852 		KASSERT(curcpu()->ci_cpl == 0);
    853 
    854 		onfault = pcb->pcb_onfault;
    855 		pcb->pcb_onfault = 0;
    856 		ret = uvm_fault(map, va, vftype);
    857 		pcb->pcb_onfault = onfault;
    858 
    859 #ifdef TRAPDEBUG
    860 		printf("uvm_fault(%p, %x, %d)=%d\n",
    861 		    map, (u_int)va, vftype, ret);
    862 #endif
    863 
    864 		/*
    865 		 * If this was a stack access we keep track of the maximum
    866 		 * accessed stack size.  Also, if uvm_fault gets a protection
    867 		 * failure it is due to accessing the stack region outside
    868 		 * the current limit and we need to reflect that as an access
    869 		 * error.
    870 		 */
    871 		if (map != kernel_map && va >= (vaddr_t)vm->vm_minsaddr) {
    872 			if (ret == 0)
    873 				uvm_grow(l->l_proc, va);
    874 			else if (ret == EACCES)
    875 				ret = EFAULT;
    876 		}
    877 
    878 		if (ret != 0) {
    879 			if (type & T_USER) {
    880 #ifdef DEBUG
    881 				user_backtrace(frame, l, type);
    882 #endif
    883 				KSI_INIT_TRAP(&ksi);
    884 				ksi.ksi_signo = SIGSEGV;
    885 				ksi.ksi_code = (ret == EACCES ?
    886 						SEGV_ACCERR : SEGV_MAPERR);
    887 				ksi.ksi_trap = type;
    888 				ksi.ksi_addr = (void *)va;
    889 				trapsignal(l, &ksi);
    890 			} else {
    891 				if (onfault) {
    892 					goto do_onfault;
    893 				}
    894 				panic("trap: uvm_fault(%p, %lx, %d): %d",
    895 				    map, va, vftype, ret);
    896 			}
    897 		} else if ((type & T_USER) == 0) {
    898 			extern char ucas_ras_start[];
    899 			extern char ucas_ras_end[];
    900 
    901 			if (frame->tf_iioq_head > (u_int)ucas_ras_start &&
    902 			    frame->tf_iioq_head < (u_int)ucas_ras_end) {
    903 				frame->tf_iioq_head = (u_int)ucas_ras_start;
    904 				frame->tf_iioq_tail = (u_int)ucas_ras_start + 4;
    905 			}
    906 		}
    907 		break;
    908 
    909 	case T_DATALIGN | T_USER:
    910 #ifdef DEBUG
    911 		user_backtrace(frame, l, type);
    912 #endif
    913 		KSI_INIT_TRAP(&ksi);
    914 		ksi.ksi_signo = SIGBUS;
    915 		ksi.ksi_code = BUS_ADRALN;
    916 		ksi.ksi_trap = type;
    917 		ksi.ksi_addr = (void *)va;
    918 		trapsignal(l, &ksi);
    919 		break;
    920 
    921 	case T_INTERRUPT:
    922 	case T_INTERRUPT|T_USER:
    923 		hppa_intr(frame);
    924 		mtctl(frame->tf_eiem, CR_EIEM);
    925 		break;
    926 
    927 	case T_LOWERPL:
    928 	case T_DPROT:
    929 	case T_IPROT:
    930 	case T_OVERFLOW:
    931 	case T_CONDITION:
    932 	case T_ILLEGAL:
    933 	case T_HIGHERPL:
    934 	case T_TAKENBR:
    935 	case T_POWERFAIL:
    936 	case T_LPMC:
    937 	case T_PAGEREF:
    938 	case T_DATAPID:  	case T_DATAPID  | T_USER:
    939 		if (0 /* T-chip */) {
    940 			break;
    941 		}
    942 		/* FALLTHROUGH to unimplemented */
    943 	default:
    944 		panic ("trap: unimplemented \'%s\' (%d)", tts, type);
    945 	}
    946 
    947 #ifdef DIAGNOSTIC
    948 	if (ci->ci_cpl != oldcpl)
    949 		printf("WARNING: SPL (%d) NOT LOWERED ON TRAP (%d) EXIT\n",
    950 		    ci->ci_cpl, trapnum);
    951 #endif
    952 
    953 	if (type & T_USER)
    954 		userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    955 
    956 #ifdef DEBUG
    957 	frame_sanity_check(__func__, __LINE__, type, frame, l);
    958 	if (frame->tf_flags & TFF_LAST && (curlwp->l_flag & LW_IDLE) == 0)
    959 		frame_sanity_check(__func__, __LINE__, type,
    960 		    curlwp->l_md.md_regs, curlwp);
    961 #endif /* DEBUG */
    962 }
    963 
    964 void
    965 child_return(void *arg)
    966 {
    967 	struct lwp *l = arg;
    968 
    969 	/*
    970 	 * Return values in the frame set by cpu_lwp_fork().
    971 	 */
    972 
    973 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    974 	ktrsysret(SYS_fork, 0, 0);
    975 #ifdef DEBUG
    976 	frame_sanity_check(__func__, __LINE__, 0, l->l_md.md_regs, l);
    977 #endif /* DEBUG */
    978 }
    979 
    980 /*
    981  * Process the tail end of a posix_spawn() for the child.
    982  */
    983 void
    984 cpu_spawn_return(struct lwp *l)
    985 {
    986 
    987 	userret(l, l->l_md.md_regs->tf_iioq_head, 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 	if (sstep == 0) {
   1055 		tf->tf_ipsw &= ~PSW_T;
   1056 		return 0;
   1057 	}
   1058 
   1059 	/*
   1060 	 * Don't touch the syscall gateway page.  Instead, insert a
   1061 	 * breakpoint where we're supposed to return.
   1062 	 */
   1063 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1064 		l->l_md.md_bpva = tf->tf_r31 & ~HPPA_PC_PRIV_MASK;
   1065 	else
   1066 		l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
   1067 
   1068 	error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
   1069 	if (error)
   1070 		return error;
   1071 	error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
   1072 	if (error)
   1073 		return error;
   1074 
   1075 	error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
   1076 	if (error)
   1077 		return error;
   1078 	error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
   1079 	if (error)
   1080 		return error;
   1081 
   1082 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1083 		tf->tf_ipsw &= ~PSW_T;
   1084 	else
   1085 		tf->tf_ipsw |= PSW_T;
   1086 
   1087 	return 0;
   1088 }
   1089 #endif
   1090 
   1091 
   1092 /*
   1093  * call actual syscall routine
   1094  * from the low-level syscall handler:
   1095  * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
   1096  *   our stack, this wins compared to copyin just needed amount anyway
   1097  * - register args are copied onto stack too
   1098  */
   1099 void
   1100 syscall(struct trapframe *frame, int *args)
   1101 {
   1102 	struct lwp *l;
   1103 	struct proc *p;
   1104 	const struct sysent *callp;
   1105 	size_t nargs64;
   1106 	int nsys, code, error;
   1107 	int tmp;
   1108 	int rval[2];
   1109 #ifdef DIAGNOSTIC
   1110 	struct cpu_info *ci = curcpu();
   1111 	int oldcpl = ci->ci_cpl;
   1112 #endif
   1113 
   1114 	curcpu()->ci_data.cpu_nsyscall++;
   1115 
   1116 #ifdef DEBUG
   1117 	frame_sanity_check(__func__, __LINE__, 0, frame, curlwp);
   1118 #endif /* DEBUG */
   1119 
   1120 	if (!USERMODE(frame->tf_iioq_head))
   1121 		panic("syscall");
   1122 
   1123 	KASSERT(curlwp != NULL);
   1124 	l = curlwp;
   1125 	p = l->l_proc;
   1126 	l->l_md.md_regs = frame;
   1127 	nsys = p->p_emul->e_nsysent;
   1128 	callp = p->p_emul->e_sysent;
   1129 	code = frame->tf_t1;
   1130 	LWP_CACHE_CREDS(l, p);
   1131 
   1132 	/*
   1133 	 * Restarting a system call is touchy on the HPPA, because syscall
   1134 	 * arguments are passed in registers and the program counter of the
   1135 	 * syscall "point" isn't easily divined.
   1136 	 *
   1137 	 * We handle the first problem by assuming that we will have to restart
   1138 	 * this system call, so we stuff the first four words of the original
   1139 	 * arguments back into the frame as arg0...arg3, which is where we
   1140 	 * found them in the first place.  Any further arguments are (still) on
   1141 	 * the user's stack and the  syscall code will fetch them from there
   1142 	 * (again).
   1143 	 *
   1144 	 * The program counter problem is addressed below.
   1145 	 */
   1146 	frame->tf_arg0 = args[0];
   1147 	frame->tf_arg1 = args[1];
   1148 	frame->tf_arg2 = args[2];
   1149 	frame->tf_arg3 = args[3];
   1150 
   1151 	/*
   1152 	 * Some special handling for the syscall(2) and
   1153 	 * __syscall(2) system calls.
   1154 	 */
   1155 	switch (code) {
   1156 	case SYS_syscall:
   1157 		code = *args;
   1158 		args += 1;
   1159 		break;
   1160 	case SYS___syscall:
   1161 		if (callp != sysent)
   1162 			break;
   1163 		/*
   1164 		 * NB: even though __syscall(2) takes a quad_t containing the
   1165 		 * system call number, because our argument copying word-swaps
   1166 		 * 64-bit arguments, the least significant word of that quad_t
   1167 		 * is the first word in the argument array.
   1168 		 */
   1169 		code = *args;
   1170 		args += 2;
   1171 	}
   1172 
   1173 	/*
   1174 	 * Stacks growing from lower addresses to higher addresses are not
   1175 	 * really such a good idea, because it makes it impossible to overlay a
   1176 	 * struct on top of C stack arguments (the arguments appear in
   1177 	 * reversed order).
   1178 	 *
   1179 	 * You can do the obvious thing (as locore.S does) and copy argument
   1180 	 * words one by one, laying them out in the "right" order in the dest-
   1181 	 * ination buffer, but this ends up word-swapping multi-word arguments
   1182 	 * (like off_t).
   1183 	 *
   1184 	 * FIXME - this works only on native binaries and
   1185 	 * will probably screw up any and all emulation.
   1186 	 *
   1187 	 */
   1188 
   1189 	if (code < 0 || code >= nsys)
   1190 		callp += p->p_emul->e_nosys;	/* bad syscall # */
   1191 	else
   1192 		callp += code;
   1193 
   1194 	nargs64 = SYCALL_NARGS64(callp);
   1195 	if (nargs64 != 0) {
   1196 		size_t nargs = callp->sy_narg;
   1197 
   1198 		for (size_t i = 0; i < nargs + nargs64;) {
   1199 			if (SYCALL_ARG_64_P(callp, i)) {
   1200 				tmp = args[i];
   1201 				args[i] = args[i + 1];
   1202 				args[i + 1] = tmp;
   1203 				i += 2;
   1204 			} else
   1205 				i++;
   1206 		}
   1207 	}
   1208 
   1209 #ifdef USERTRACE
   1210 	if (0) {
   1211 		user_backtrace(frame, l, -1);
   1212 		frame->tf_ipsw |= PSW_R;
   1213 		frame->tf_rctr = 0;
   1214 		printf("r %08x", frame->tf_iioq_head);
   1215 		rctr_next_iioq = frame->tf_iioq_head + 4;
   1216 	}
   1217 #endif
   1218 
   1219 	error = sy_invoke(callp, l, args, rval, code);
   1220 
   1221 	switch (error) {
   1222 	case 0:
   1223 		l = curlwp;			/* changes on exec() */
   1224 		frame = l->l_md.md_regs;
   1225 		frame->tf_ret0 = rval[0];
   1226 		frame->tf_ret1 = rval[1];
   1227 		frame->tf_t1 = 0;
   1228 		break;
   1229 	case ERESTART:
   1230 		/*
   1231 		 * Now we have to wind back the instruction offset queue to the
   1232 		 * point where the system call will be made again.  This is
   1233 		 * inherently tied to the SYSCALL macro.
   1234 		 *
   1235 		 * Currently, the part of the SYSCALL macro that we want to re-
   1236 		 * run reads as:
   1237 		 *
   1238 		 *	ldil	L%SYSCALLGATE, r1
   1239 		 *	ble	4(srX, r1)
   1240 		 *	ldi	__CONCAT(SYS_,x), t1
   1241 		 *	comb,<>	%r0, %t1, __cerror
   1242 		 *
   1243 		 * And our offset queue head points to the comb instruction.
   1244 		 * So we need to subtract twelve to reach the ldil.
   1245 		 */
   1246 		frame->tf_iioq_head -= 12;
   1247 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
   1248 		break;
   1249 	case EJUSTRETURN:
   1250 		p = curproc;
   1251 		break;
   1252 	default:
   1253 		if (p->p_emul->e_errno)
   1254 			error = p->p_emul->e_errno[error];
   1255 		frame->tf_t1 = error;
   1256 		break;
   1257 	}
   1258 
   1259 	userret(l, frame->tf_iioq_head, 0);
   1260 
   1261 #ifdef DIAGNOSTIC
   1262 	if (ci->ci_cpl != oldcpl) {
   1263 		printf("WARNING: SPL (0x%x) NOT LOWERED ON "
   1264 		    "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
   1265 		    ci->ci_cpl, code, args[0], args[1], args[2], p->p_pid);
   1266 		ci->ci_cpl = oldcpl;
   1267 	}
   1268 #endif
   1269 
   1270 #ifdef DEBUG
   1271 	frame_sanity_check(__func__, __LINE__, 0, frame, l);
   1272 #endif /* DEBUG */
   1273 }
   1274 
   1275 /*
   1276  * Start a new LWP
   1277  */
   1278 void
   1279 startlwp(void *arg)
   1280 {
   1281 	ucontext_t *uc = arg;
   1282 	lwp_t *l = curlwp;
   1283 	int error __diagused;
   1284 
   1285 	error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
   1286 	KASSERT(error == 0);
   1287 
   1288 	kmem_free(uc, sizeof(ucontext_t));
   1289 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1290 }
   1291