Home | History | Annotate | Line # | Download | only in hppa
trap.c revision 1.100
      1 /*	$NetBSD: trap.c,v 1.100 2012/04/06 12:21:59 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.100 2012/04/06 12:21:59 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;
    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 
    697 		break;
    698 
    699 #ifdef PTRACE
    700 	case T_TAKENBR | T_USER:
    701 		ss_clear_breakpoints(l);
    702 
    703 		KSI_INIT_TRAP(&ksi);
    704 		ksi.ksi_signo = SIGTRAP;
    705 		ksi.ksi_code = TRAP_TRACE;
    706 		ksi.ksi_trap = trapnum;
    707 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    708 
    709                 /* pass to user debugger */
    710 		trapsignal(l, &ksi);
    711 		break;
    712 #endif
    713 
    714 	case T_EXCEPTION | T_USER: {	/* co-proc assist trap */
    715 		uint64_t *fpp;
    716 		uint32_t *pex, ex, inst;
    717 		int i;
    718 
    719 		hppa_fpu_flush(l);
    720 		fpp = (uint64_t *)pcb->pcb_fpregs;
    721 
    722 		/* skip the status register */
    723 		pex = (uint32_t *)&fpp[0];
    724 		pex++;
    725 
    726 		/* loop through the exception registers */
    727 		for (i = 1; i < 8 && !*pex; i++, pex++)
    728 			;
    729 		KASSERT(i < 8);
    730 		ex = *pex;
    731 		*pex = 0;
    732 
    733 		/* reset the trap flag, as if there was none */
    734 		fpp[0] &= ~(((uint64_t)HPPA_FPU_T) << 32);
    735 
    736 		/* emulate the instruction */
    737 		inst = ((uint32_t)fpopmap[ex >> 26] << 26) | (ex & 0x03ffffff);
    738 		hppa_fpu_emulate(frame, l, inst);
    739 		}
    740 		break;
    741 
    742 	case T_OVERFLOW | T_USER:
    743 		KSI_INIT_TRAP(&ksi);
    744 		ksi.ksi_signo = SIGFPE;
    745 		ksi.ksi_code = SI_NOINFO;
    746 		ksi.ksi_trap = type;
    747 		ksi.ksi_addr = (void *)va;
    748 		trapsignal(l, &ksi);
    749 		break;
    750 
    751 	case T_CONDITION | T_USER:
    752 		KSI_INIT_TRAP(&ksi);
    753 		ksi.ksi_signo = SIGFPE;
    754 		ksi.ksi_code = FPE_INTDIV;
    755 		ksi.ksi_trap = type;
    756 		ksi.ksi_addr = (void *)va;
    757 		trapsignal(l, &ksi);
    758 		break;
    759 
    760 	case T_ILLEGAL | T_USER:
    761 #ifdef DEBUG
    762 		user_backtrace(frame, l, type);
    763 #endif
    764 		KSI_INIT_TRAP(&ksi);
    765 		ksi.ksi_signo = SIGILL;
    766 		ksi.ksi_code = ILL_ILLOPC;
    767 		ksi.ksi_trap = type;
    768 		ksi.ksi_addr = (void *)va;
    769 		trapsignal(l, &ksi);
    770 		break;
    771 
    772 	case T_PRIV_OP | T_USER:
    773 #ifdef DEBUG
    774 		user_backtrace(frame, l, type);
    775 #endif
    776 		KSI_INIT_TRAP(&ksi);
    777 		ksi.ksi_signo = SIGILL;
    778 		ksi.ksi_code = ILL_PRVOPC;
    779 		ksi.ksi_trap = type;
    780 		ksi.ksi_addr = (void *)va;
    781 		trapsignal(l, &ksi);
    782 		break;
    783 
    784 	case T_PRIV_REG | T_USER:
    785 #ifdef DEBUG
    786 		user_backtrace(frame, l, type);
    787 #endif
    788 		KSI_INIT_TRAP(&ksi);
    789 		ksi.ksi_signo = SIGILL;
    790 		ksi.ksi_code = ILL_PRVREG;
    791 		ksi.ksi_trap = type;
    792 		ksi.ksi_addr = (void *)va;
    793 		trapsignal(l, &ksi);
    794 		break;
    795 
    796 		/* these should never got here */
    797 	case T_HIGHERPL | T_USER:
    798 	case T_LOWERPL | T_USER:
    799 		KSI_INIT_TRAP(&ksi);
    800 		ksi.ksi_signo = SIGSEGV;
    801 		ksi.ksi_code = SEGV_ACCERR;
    802 		ksi.ksi_trap = type;
    803 		ksi.ksi_addr = (void *)va;
    804 		trapsignal(l, &ksi);
    805 		break;
    806 
    807 	case T_IPROT | T_USER:
    808 	case T_DPROT | T_USER:
    809 		KSI_INIT_TRAP(&ksi);
    810 		ksi.ksi_signo = SIGSEGV;
    811 		ksi.ksi_code = SEGV_ACCERR;
    812 		ksi.ksi_trap = type;
    813 		ksi.ksi_addr = (void *)va;
    814 		trapsignal(l, &ksi);
    815 		break;
    816 
    817 	case T_DATACC:   	case T_USER | T_DATACC:
    818 	case T_ITLBMISS:	case T_USER | T_ITLBMISS:
    819 	case T_DTLBMISS:	case T_USER | T_DTLBMISS:
    820 	case T_ITLBMISSNA:	case T_USER | T_ITLBMISSNA:
    821 	case T_DTLBMISSNA:	case T_USER | T_DTLBMISSNA:
    822 	case T_TLB_DIRTY:	case T_USER | T_TLB_DIRTY:
    823 		vm = p->p_vmspace;
    824 
    825 		if (!vm) {
    826 #ifdef TRAPDEBUG
    827 			printf("trap: no vm, p=%p\n", p);
    828 #endif
    829 			goto dead_end;
    830 		}
    831 
    832 		/*
    833 		 * it could be a kernel map for exec_map faults
    834 		 */
    835 		if (!(type & T_USER) && space == HPPA_SID_KERNEL)
    836 			map = kernel_map;
    837 		else {
    838 			map = &vm->vm_map;
    839 		}
    840 
    841 		va = trunc_page(va);
    842 
    843 		if (map->pmap->pm_space != space) {
    844 #ifdef TRAPDEBUG
    845 			printf("trap: space mismatch %d != %d\n",
    846 			    space, map->pmap->pm_space);
    847 #endif
    848 			/* actually dump the user, crap the kernel */
    849 			goto dead_end;
    850 		}
    851 
    852 		/* Never call uvm_fault in interrupt context. */
    853 		KASSERT(curcpu()->ci_cpl == 0);
    854 
    855 		onfault = pcb->pcb_onfault;
    856 		pcb->pcb_onfault = 0;
    857 		ret = uvm_fault(map, va, vftype);
    858 		pcb->pcb_onfault = onfault;
    859 
    860 #ifdef TRAPDEBUG
    861 		printf("uvm_fault(%p, %x, %d)=%d\n",
    862 		    map, (u_int)va, vftype, ret);
    863 #endif
    864 
    865 		/*
    866 		 * If this was a stack access we keep track of the maximum
    867 		 * accessed stack size.  Also, if uvm_fault gets a protection
    868 		 * failure it is due to accessing the stack region outside
    869 		 * the current limit and we need to reflect that as an access
    870 		 * error.
    871 		 */
    872 		if (map != kernel_map && va >= (vaddr_t)vm->vm_minsaddr) {
    873 			if (ret == 0)
    874 				uvm_grow(l->l_proc, va);
    875 			else if (ret == EACCES)
    876 				ret = EFAULT;
    877 		}
    878 
    879 		if (ret != 0) {
    880 			if (type & T_USER) {
    881 #ifdef DEBUG
    882 				user_backtrace(frame, l, type);
    883 #endif
    884 				KSI_INIT_TRAP(&ksi);
    885 				ksi.ksi_signo = SIGSEGV;
    886 				ksi.ksi_code = (ret == EACCES ?
    887 						SEGV_ACCERR : SEGV_MAPERR);
    888 				ksi.ksi_trap = type;
    889 				ksi.ksi_addr = (void *)va;
    890 				trapsignal(l, &ksi);
    891 			} else {
    892 				if (onfault) {
    893 					goto do_onfault;
    894 				}
    895 				panic("trap: uvm_fault(%p, %lx, %d): %d",
    896 				    map, va, vftype, ret);
    897 			}
    898 		} else if ((type & T_USER) == 0) {
    899 			extern char ucas_ras_start[];
    900 			extern char ucas_ras_end[];
    901 
    902 			if (frame->tf_iioq_head > (u_int)ucas_ras_start &&
    903 			    frame->tf_iioq_head < (u_int)ucas_ras_end) {
    904 				frame->tf_iioq_head = (u_int)ucas_ras_start;
    905 				frame->tf_iioq_tail = (u_int)ucas_ras_start + 4;
    906 			}
    907 		}
    908 		break;
    909 
    910 	case T_DATALIGN | T_USER:
    911 #ifdef DEBUG
    912 		user_backtrace(frame, l, type);
    913 #endif
    914 		KSI_INIT_TRAP(&ksi);
    915 		ksi.ksi_signo = SIGBUS;
    916 		ksi.ksi_code = BUS_ADRALN;
    917 		ksi.ksi_trap = type;
    918 		ksi.ksi_addr = (void *)va;
    919 		trapsignal(l, &ksi);
    920 		break;
    921 
    922 	case T_INTERRUPT:
    923 	case T_INTERRUPT|T_USER:
    924 		hppa_intr(frame);
    925 		mtctl(frame->tf_eiem, CR_EIEM);
    926 		break;
    927 
    928 	case T_LOWERPL:
    929 	case T_DPROT:
    930 	case T_IPROT:
    931 	case T_OVERFLOW:
    932 	case T_CONDITION:
    933 	case T_ILLEGAL:
    934 	case T_HIGHERPL:
    935 	case T_TAKENBR:
    936 	case T_POWERFAIL:
    937 	case T_LPMC:
    938 	case T_PAGEREF:
    939 	case T_DATAPID:  	case T_DATAPID  | T_USER:
    940 		if (0 /* T-chip */) {
    941 			break;
    942 		}
    943 		/* FALLTHROUGH to unimplemented */
    944 	default:
    945 		panic ("trap: unimplemented \'%s\' (%d)", tts, type);
    946 	}
    947 
    948 #ifdef DIAGNOSTIC
    949 	if (ci->ci_cpl != oldcpl)
    950 		printf("WARNING: SPL (%d) NOT LOWERED ON TRAP (%d) EXIT\n",
    951 		    ci->ci_cpl, trapnum);
    952 #endif
    953 
    954 	if (type & T_USER)
    955 		userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    956 
    957 #ifdef DEBUG
    958 	frame_sanity_check(__func__, __LINE__, type, frame, l);
    959 	if (frame->tf_flags & TFF_LAST && (curlwp->l_flag & LW_IDLE) == 0)
    960 		frame_sanity_check(__func__, __LINE__, type,
    961 		    curlwp->l_md.md_regs, curlwp);
    962 #endif /* DEBUG */
    963 }
    964 
    965 void
    966 child_return(void *arg)
    967 {
    968 	struct lwp *l = arg;
    969 
    970 	/*
    971 	 * Return values in the frame set by cpu_lwp_fork().
    972 	 */
    973 
    974 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    975 	ktrsysret(SYS_fork, 0, 0);
    976 #ifdef DEBUG
    977 	frame_sanity_check(__func__, __LINE__, 0, l->l_md.md_regs, l);
    978 #endif /* DEBUG */
    979 }
    980 
    981 /*
    982  * Process the tail end of a posix_spawn() for the child.
    983  */
    984 void
    985 cpu_spawn_return(struct lwp *l)
    986 {
    987 	struct proc *p = l->l_proc;
    988 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    989 	pa_space_t space = pmap->pm_space;
    990 	struct trapframe *tf = l->l_md.md_regs;
    991 
    992 	/* Load all of the user's space registers. */
    993 	tf->tf_sr0 = tf->tf_sr1 = tf->tf_sr3 = tf->tf_sr2 =
    994 	tf->tf_sr4 = tf->tf_sr5 = tf->tf_sr6 = space;
    995 	tf->tf_iisq_head = tf->tf_iisq_tail = space;
    996 
    997 	/* Load the protection registers */
    998 	tf->tf_pidr1 = tf->tf_pidr2 = pmap->pm_pid;
    999 
   1000 	/*
   1001 	 * theoretically these could be inherited from the father,
   1002 	 * but just in case.
   1003 	 */
   1004 	tf->tf_sr7 = HPPA_SID_KERNEL;
   1005 	mfctl(CR_EIEM, tf->tf_eiem);
   1006 	tf->tf_ipsw = PSW_C | PSW_Q | PSW_P | PSW_D | PSW_I /* | PSW_L */ |
   1007 	    (curcpu()->ci_psw & PSW_O);
   1008 
   1009 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1010 #ifdef DEBUG
   1011 	frame_sanity_check(__func__, __LINE__, 0, l->l_md.md_regs, l);
   1012 #endif /* DEBUG */
   1013 }
   1014 
   1015 #ifdef PTRACE
   1016 
   1017 #include <sys/ptrace.h>
   1018 
   1019 int
   1020 ss_get_value(struct lwp *l, vaddr_t addr, u_int *value)
   1021 {
   1022 	struct uio uio;
   1023 	struct iovec iov;
   1024 
   1025 	iov.iov_base = (void *)value;
   1026 	iov.iov_len = sizeof(u_int);
   1027 	uio.uio_iov = &iov;
   1028 	uio.uio_iovcnt = 1;
   1029 	uio.uio_offset = (off_t)addr;
   1030 	uio.uio_resid = sizeof(u_int);
   1031 	uio.uio_rw = UIO_READ;
   1032 	UIO_SETUP_SYSSPACE(&uio);
   1033 
   1034 	return (process_domem(curlwp, l, &uio));
   1035 }
   1036 
   1037 int
   1038 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
   1039 {
   1040 	struct uio uio;
   1041 	struct iovec iov;
   1042 
   1043 	iov.iov_base = (void *)&value;
   1044 	iov.iov_len = sizeof(u_int);
   1045 	uio.uio_iov = &iov;
   1046 	uio.uio_iovcnt = 1;
   1047 	uio.uio_offset = (off_t)addr;
   1048 	uio.uio_resid = sizeof(u_int);
   1049 	uio.uio_rw = UIO_WRITE;
   1050 	UIO_SETUP_SYSSPACE(&uio);
   1051 
   1052 	return (process_domem(curlwp, l, &uio));
   1053 }
   1054 
   1055 void
   1056 ss_clear_breakpoints(struct lwp *l)
   1057 {
   1058 	/* Restore origional instructions. */
   1059 	if (l->l_md.md_bpva != 0) {
   1060 		ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
   1061 		ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
   1062 		l->l_md.md_bpva = 0;
   1063 	}
   1064 }
   1065 
   1066 
   1067 int
   1068 process_sstep(struct lwp *l, int sstep)
   1069 {
   1070 	struct trapframe *tf = l->l_md.md_regs;
   1071 	int error;
   1072 
   1073 	ss_clear_breakpoints(l);
   1074 
   1075 	/* We're continuing... */
   1076 	if (sstep == 0) {
   1077 		tf->tf_ipsw &= ~PSW_T;
   1078 		return 0;
   1079 	}
   1080 
   1081 	/*
   1082 	 * Don't touch the syscall gateway page.  Instead, insert a
   1083 	 * breakpoint where we're supposed to return.
   1084 	 */
   1085 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1086 		l->l_md.md_bpva = tf->tf_r31 & ~HPPA_PC_PRIV_MASK;
   1087 	else
   1088 		l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
   1089 
   1090 	error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
   1091 	if (error)
   1092 		return error;
   1093 	error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
   1094 	if (error)
   1095 		return error;
   1096 
   1097 	error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
   1098 	if (error)
   1099 		return error;
   1100 	error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
   1101 	if (error)
   1102 		return error;
   1103 
   1104 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1105 		tf->tf_ipsw &= ~PSW_T;
   1106 	else
   1107 		tf->tf_ipsw |= PSW_T;
   1108 
   1109 	return 0;
   1110 }
   1111 #endif
   1112 
   1113 
   1114 /*
   1115  * call actual syscall routine
   1116  * from the low-level syscall handler:
   1117  * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
   1118  *   our stack, this wins compared to copyin just needed amount anyway
   1119  * - register args are copied onto stack too
   1120  */
   1121 void
   1122 syscall(struct trapframe *frame, int *args)
   1123 {
   1124 	struct lwp *l;
   1125 	struct proc *p;
   1126 	const struct sysent *callp;
   1127 	size_t nargs64;
   1128 	int nsys, code, error;
   1129 	int tmp;
   1130 	int rval[2];
   1131 #ifdef DIAGNOSTIC
   1132 	struct cpu_info *ci = curcpu();
   1133 	int oldcpl = ci->ci_cpl;
   1134 #endif
   1135 
   1136 	curcpu()->ci_data.cpu_nsyscall++;
   1137 
   1138 #ifdef DEBUG
   1139 	frame_sanity_check(__func__, __LINE__, 0, frame, curlwp);
   1140 #endif /* DEBUG */
   1141 
   1142 	if (!USERMODE(frame->tf_iioq_head))
   1143 		panic("syscall");
   1144 
   1145 	KASSERT(curlwp != NULL);
   1146 	l = curlwp;
   1147 	p = l->l_proc;
   1148 	l->l_md.md_regs = frame;
   1149 	nsys = p->p_emul->e_nsysent;
   1150 	callp = p->p_emul->e_sysent;
   1151 	code = frame->tf_t1;
   1152 	LWP_CACHE_CREDS(l, p);
   1153 
   1154 	/*
   1155 	 * Restarting a system call is touchy on the HPPA, because syscall
   1156 	 * arguments are passed in registers and the program counter of the
   1157 	 * syscall "point" isn't easily divined.
   1158 	 *
   1159 	 * We handle the first problem by assuming that we will have to restart
   1160 	 * this system call, so we stuff the first four words of the original
   1161 	 * arguments back into the frame as arg0...arg3, which is where we
   1162 	 * found them in the first place.  Any further arguments are (still) on
   1163 	 * the user's stack and the  syscall code will fetch them from there
   1164 	 * (again).
   1165 	 *
   1166 	 * The program counter problem is addressed below.
   1167 	 */
   1168 	frame->tf_arg0 = args[0];
   1169 	frame->tf_arg1 = args[1];
   1170 	frame->tf_arg2 = args[2];
   1171 	frame->tf_arg3 = args[3];
   1172 
   1173 	/*
   1174 	 * Some special handling for the syscall(2) and
   1175 	 * __syscall(2) system calls.
   1176 	 */
   1177 	switch (code) {
   1178 	case SYS_syscall:
   1179 		code = *args;
   1180 		args += 1;
   1181 		break;
   1182 	case SYS___syscall:
   1183 		if (callp != sysent)
   1184 			break;
   1185 		/*
   1186 		 * NB: even though __syscall(2) takes a quad_t containing the
   1187 		 * system call number, because our argument copying word-swaps
   1188 		 * 64-bit arguments, the least significant word of that quad_t
   1189 		 * is the first word in the argument array.
   1190 		 */
   1191 		code = *args;
   1192 		args += 2;
   1193 	}
   1194 
   1195 	/*
   1196 	 * Stacks growing from lower addresses to higher addresses are not
   1197 	 * really such a good idea, because it makes it impossible to overlay a
   1198 	 * struct on top of C stack arguments (the arguments appear in
   1199 	 * reversed order).
   1200 	 *
   1201 	 * You can do the obvious thing (as locore.S does) and copy argument
   1202 	 * words one by one, laying them out in the "right" order in the dest-
   1203 	 * ination buffer, but this ends up word-swapping multi-word arguments
   1204 	 * (like off_t).
   1205 	 *
   1206 	 * FIXME - this works only on native binaries and
   1207 	 * will probably screw up any and all emulation.
   1208 	 *
   1209 	 */
   1210 
   1211 	if (code < 0 || code >= nsys)
   1212 		callp += p->p_emul->e_nosys;	/* bad syscall # */
   1213 	else
   1214 		callp += code;
   1215 
   1216 	nargs64 = SYCALL_NARGS64(callp);
   1217 	if (nargs64 != 0) {
   1218 		size_t nargs = callp->sy_narg;
   1219 
   1220 		for (size_t i = 0; i < nargs + nargs64;) {
   1221 			if (SYCALL_ARG_64_P(callp, i)) {
   1222 				tmp = args[i];
   1223 				args[i] = args[i + 1];
   1224 				args[i + 1] = tmp;
   1225 				i += 2;
   1226 			} else
   1227 				i++;
   1228 		}
   1229 	}
   1230 
   1231 #ifdef USERTRACE
   1232 	if (0) {
   1233 		user_backtrace(frame, l, -1);
   1234 		frame->tf_ipsw |= PSW_R;
   1235 		frame->tf_rctr = 0;
   1236 		printf("r %08x", frame->tf_iioq_head);
   1237 		rctr_next_iioq = frame->tf_iioq_head + 4;
   1238 	}
   1239 #endif
   1240 
   1241 	error = 0;
   1242 	if (__predict_false(p->p_trace_enabled)) {
   1243 		error = trace_enter(code, args, callp->sy_narg);
   1244 		if (error)
   1245 			goto out;
   1246 	}
   1247 
   1248 	rval[0] = 0;
   1249 	rval[1] = 0;
   1250 	error = sy_call(callp, l, args, rval);
   1251 out:
   1252 	switch (error) {
   1253 	case 0:
   1254 		l = curlwp;			/* changes on exec() */
   1255 		frame = l->l_md.md_regs;
   1256 		frame->tf_ret0 = rval[0];
   1257 		frame->tf_ret1 = rval[1];
   1258 		frame->tf_t1 = 0;
   1259 		break;
   1260 	case ERESTART:
   1261 		/*
   1262 		 * Now we have to wind back the instruction offset queue to the
   1263 		 * point where the system call will be made again.  This is
   1264 		 * inherently tied to the SYSCALL macro.
   1265 		 *
   1266 		 * Currently, the part of the SYSCALL macro that we want to re-
   1267 		 * run reads as:
   1268 		 *
   1269 		 *	ldil	L%SYSCALLGATE, r1
   1270 		 *	ble	4(sr7, r1)
   1271 		 *	ldi	__CONCAT(SYS_,x), t1
   1272 		 *	comb,<>	%r0, %t1, __cerror
   1273 		 *
   1274 		 * And our offset queue head points to the comb instruction.
   1275 		 * So we need to subtract twelve to reach the ldil.
   1276 		 */
   1277 		frame->tf_iioq_head -= 12;
   1278 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
   1279 		break;
   1280 	case EJUSTRETURN:
   1281 		p = curproc;
   1282 		break;
   1283 	default:
   1284 		if (p->p_emul->e_errno)
   1285 			error = p->p_emul->e_errno[error];
   1286 		frame->tf_t1 = error;
   1287 		break;
   1288 	}
   1289 
   1290 	if (__predict_false(p->p_trace_enabled))
   1291 		trace_exit(code, rval, error);
   1292 
   1293 	userret(l, frame->tf_iioq_head, 0);
   1294 
   1295 #ifdef DIAGNOSTIC
   1296 	if (ci->ci_cpl != oldcpl) {
   1297 		printf("WARNING: SPL (0x%x) NOT LOWERED ON "
   1298 		    "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
   1299 		    ci->ci_cpl, code, args[0], args[1], args[2], p->p_pid);
   1300 		ci->ci_cpl = oldcpl;
   1301 	}
   1302 #endif
   1303 
   1304 #ifdef DEBUG
   1305 	frame_sanity_check(__func__, __LINE__, 0, frame, l);
   1306 #endif /* DEBUG */
   1307 }
   1308 
   1309 /*
   1310  * Start a new LWP
   1311  */
   1312 void
   1313 startlwp(void *arg)
   1314 {
   1315 	ucontext_t *uc = arg;
   1316 	lwp_t *l = curlwp;
   1317 	int error;
   1318 
   1319 	error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
   1320 	KASSERT(error == 0);
   1321 
   1322 	kmem_free(uc, sizeof(ucontext_t));
   1323 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1324 }
   1325