Home | History | Annotate | Line # | Download | only in hppa
trap.c revision 1.101
      1 /*	$NetBSD: trap.c,v 1.101 2012/04/23 11:25:03 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.101 2012/04/23 11:25:03 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 	struct proc *p = l->l_proc;
    987 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    988 	pa_space_t space = pmap->pm_space;
    989 	struct trapframe *tf = l->l_md.md_regs;
    990 
    991 	/* Load all of the user's space registers. */
    992 	tf->tf_sr0 = tf->tf_sr1 = tf->tf_sr3 = tf->tf_sr2 =
    993 	tf->tf_sr4 = tf->tf_sr5 = tf->tf_sr6 = space;
    994 	tf->tf_iisq_head = tf->tf_iisq_tail = space;
    995 
    996 	/* Load the protection registers */
    997 	tf->tf_pidr1 = tf->tf_pidr2 = pmap->pm_pid;
    998 
    999 	/*
   1000 	 * theoretically these could be inherited from the father,
   1001 	 * but just in case.
   1002 	 */
   1003 	tf->tf_sr7 = HPPA_SID_KERNEL;
   1004 	mfctl(CR_EIEM, tf->tf_eiem);
   1005 	tf->tf_ipsw = PSW_C | PSW_Q | PSW_P | PSW_D | PSW_I /* | PSW_L */ |
   1006 	    (curcpu()->ci_psw & PSW_O);
   1007 
   1008 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1009 #ifdef DEBUG
   1010 	frame_sanity_check(__func__, __LINE__, 0, l->l_md.md_regs, l);
   1011 #endif /* DEBUG */
   1012 }
   1013 
   1014 #ifdef PTRACE
   1015 
   1016 #include <sys/ptrace.h>
   1017 
   1018 int
   1019 ss_get_value(struct lwp *l, vaddr_t addr, u_int *value)
   1020 {
   1021 	struct uio uio;
   1022 	struct iovec iov;
   1023 
   1024 	iov.iov_base = (void *)value;
   1025 	iov.iov_len = sizeof(u_int);
   1026 	uio.uio_iov = &iov;
   1027 	uio.uio_iovcnt = 1;
   1028 	uio.uio_offset = (off_t)addr;
   1029 	uio.uio_resid = sizeof(u_int);
   1030 	uio.uio_rw = UIO_READ;
   1031 	UIO_SETUP_SYSSPACE(&uio);
   1032 
   1033 	return (process_domem(curlwp, l, &uio));
   1034 }
   1035 
   1036 int
   1037 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
   1038 {
   1039 	struct uio uio;
   1040 	struct iovec iov;
   1041 
   1042 	iov.iov_base = (void *)&value;
   1043 	iov.iov_len = sizeof(u_int);
   1044 	uio.uio_iov = &iov;
   1045 	uio.uio_iovcnt = 1;
   1046 	uio.uio_offset = (off_t)addr;
   1047 	uio.uio_resid = sizeof(u_int);
   1048 	uio.uio_rw = UIO_WRITE;
   1049 	UIO_SETUP_SYSSPACE(&uio);
   1050 
   1051 	return (process_domem(curlwp, l, &uio));
   1052 }
   1053 
   1054 void
   1055 ss_clear_breakpoints(struct lwp *l)
   1056 {
   1057 	/* Restore origional instructions. */
   1058 	if (l->l_md.md_bpva != 0) {
   1059 		ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
   1060 		ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
   1061 		l->l_md.md_bpva = 0;
   1062 	}
   1063 }
   1064 
   1065 
   1066 int
   1067 process_sstep(struct lwp *l, int sstep)
   1068 {
   1069 	struct trapframe *tf = l->l_md.md_regs;
   1070 	int error;
   1071 
   1072 	ss_clear_breakpoints(l);
   1073 
   1074 	/* We're continuing... */
   1075 	if (sstep == 0) {
   1076 		tf->tf_ipsw &= ~PSW_T;
   1077 		return 0;
   1078 	}
   1079 
   1080 	/*
   1081 	 * Don't touch the syscall gateway page.  Instead, insert a
   1082 	 * breakpoint where we're supposed to return.
   1083 	 */
   1084 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1085 		l->l_md.md_bpva = tf->tf_r31 & ~HPPA_PC_PRIV_MASK;
   1086 	else
   1087 		l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
   1088 
   1089 	error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
   1090 	if (error)
   1091 		return error;
   1092 	error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
   1093 	if (error)
   1094 		return error;
   1095 
   1096 	error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
   1097 	if (error)
   1098 		return error;
   1099 	error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
   1100 	if (error)
   1101 		return error;
   1102 
   1103 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1104 		tf->tf_ipsw &= ~PSW_T;
   1105 	else
   1106 		tf->tf_ipsw |= PSW_T;
   1107 
   1108 	return 0;
   1109 }
   1110 #endif
   1111 
   1112 
   1113 /*
   1114  * call actual syscall routine
   1115  * from the low-level syscall handler:
   1116  * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
   1117  *   our stack, this wins compared to copyin just needed amount anyway
   1118  * - register args are copied onto stack too
   1119  */
   1120 void
   1121 syscall(struct trapframe *frame, int *args)
   1122 {
   1123 	struct lwp *l;
   1124 	struct proc *p;
   1125 	const struct sysent *callp;
   1126 	size_t nargs64;
   1127 	int nsys, code, error;
   1128 	int tmp;
   1129 	int rval[2];
   1130 #ifdef DIAGNOSTIC
   1131 	struct cpu_info *ci = curcpu();
   1132 	int oldcpl = ci->ci_cpl;
   1133 #endif
   1134 
   1135 	curcpu()->ci_data.cpu_nsyscall++;
   1136 
   1137 #ifdef DEBUG
   1138 	frame_sanity_check(__func__, __LINE__, 0, frame, curlwp);
   1139 #endif /* DEBUG */
   1140 
   1141 	if (!USERMODE(frame->tf_iioq_head))
   1142 		panic("syscall");
   1143 
   1144 	KASSERT(curlwp != NULL);
   1145 	l = curlwp;
   1146 	p = l->l_proc;
   1147 	l->l_md.md_regs = frame;
   1148 	nsys = p->p_emul->e_nsysent;
   1149 	callp = p->p_emul->e_sysent;
   1150 	code = frame->tf_t1;
   1151 	LWP_CACHE_CREDS(l, p);
   1152 
   1153 	/*
   1154 	 * Restarting a system call is touchy on the HPPA, because syscall
   1155 	 * arguments are passed in registers and the program counter of the
   1156 	 * syscall "point" isn't easily divined.
   1157 	 *
   1158 	 * We handle the first problem by assuming that we will have to restart
   1159 	 * this system call, so we stuff the first four words of the original
   1160 	 * arguments back into the frame as arg0...arg3, which is where we
   1161 	 * found them in the first place.  Any further arguments are (still) on
   1162 	 * the user's stack and the  syscall code will fetch them from there
   1163 	 * (again).
   1164 	 *
   1165 	 * The program counter problem is addressed below.
   1166 	 */
   1167 	frame->tf_arg0 = args[0];
   1168 	frame->tf_arg1 = args[1];
   1169 	frame->tf_arg2 = args[2];
   1170 	frame->tf_arg3 = args[3];
   1171 
   1172 	/*
   1173 	 * Some special handling for the syscall(2) and
   1174 	 * __syscall(2) system calls.
   1175 	 */
   1176 	switch (code) {
   1177 	case SYS_syscall:
   1178 		code = *args;
   1179 		args += 1;
   1180 		break;
   1181 	case SYS___syscall:
   1182 		if (callp != sysent)
   1183 			break;
   1184 		/*
   1185 		 * NB: even though __syscall(2) takes a quad_t containing the
   1186 		 * system call number, because our argument copying word-swaps
   1187 		 * 64-bit arguments, the least significant word of that quad_t
   1188 		 * is the first word in the argument array.
   1189 		 */
   1190 		code = *args;
   1191 		args += 2;
   1192 	}
   1193 
   1194 	/*
   1195 	 * Stacks growing from lower addresses to higher addresses are not
   1196 	 * really such a good idea, because it makes it impossible to overlay a
   1197 	 * struct on top of C stack arguments (the arguments appear in
   1198 	 * reversed order).
   1199 	 *
   1200 	 * You can do the obvious thing (as locore.S does) and copy argument
   1201 	 * words one by one, laying them out in the "right" order in the dest-
   1202 	 * ination buffer, but this ends up word-swapping multi-word arguments
   1203 	 * (like off_t).
   1204 	 *
   1205 	 * FIXME - this works only on native binaries and
   1206 	 * will probably screw up any and all emulation.
   1207 	 *
   1208 	 */
   1209 
   1210 	if (code < 0 || code >= nsys)
   1211 		callp += p->p_emul->e_nosys;	/* bad syscall # */
   1212 	else
   1213 		callp += code;
   1214 
   1215 	nargs64 = SYCALL_NARGS64(callp);
   1216 	if (nargs64 != 0) {
   1217 		size_t nargs = callp->sy_narg;
   1218 
   1219 		for (size_t i = 0; i < nargs + nargs64;) {
   1220 			if (SYCALL_ARG_64_P(callp, i)) {
   1221 				tmp = args[i];
   1222 				args[i] = args[i + 1];
   1223 				args[i + 1] = tmp;
   1224 				i += 2;
   1225 			} else
   1226 				i++;
   1227 		}
   1228 	}
   1229 
   1230 #ifdef USERTRACE
   1231 	if (0) {
   1232 		user_backtrace(frame, l, -1);
   1233 		frame->tf_ipsw |= PSW_R;
   1234 		frame->tf_rctr = 0;
   1235 		printf("r %08x", frame->tf_iioq_head);
   1236 		rctr_next_iioq = frame->tf_iioq_head + 4;
   1237 	}
   1238 #endif
   1239 
   1240 	error = 0;
   1241 	if (__predict_false(p->p_trace_enabled)) {
   1242 		error = trace_enter(code, args, callp->sy_narg);
   1243 		if (error)
   1244 			goto out;
   1245 	}
   1246 
   1247 	rval[0] = 0;
   1248 	rval[1] = 0;
   1249 	error = sy_call(callp, l, args, rval);
   1250 out:
   1251 	switch (error) {
   1252 	case 0:
   1253 		l = curlwp;			/* changes on exec() */
   1254 		frame = l->l_md.md_regs;
   1255 		frame->tf_ret0 = rval[0];
   1256 		frame->tf_ret1 = rval[1];
   1257 		frame->tf_t1 = 0;
   1258 		break;
   1259 	case ERESTART:
   1260 		/*
   1261 		 * Now we have to wind back the instruction offset queue to the
   1262 		 * point where the system call will be made again.  This is
   1263 		 * inherently tied to the SYSCALL macro.
   1264 		 *
   1265 		 * Currently, the part of the SYSCALL macro that we want to re-
   1266 		 * run reads as:
   1267 		 *
   1268 		 *	ldil	L%SYSCALLGATE, r1
   1269 		 *	ble	4(sr7, r1)
   1270 		 *	ldi	__CONCAT(SYS_,x), t1
   1271 		 *	comb,<>	%r0, %t1, __cerror
   1272 		 *
   1273 		 * And our offset queue head points to the comb instruction.
   1274 		 * So we need to subtract twelve to reach the ldil.
   1275 		 */
   1276 		frame->tf_iioq_head -= 12;
   1277 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
   1278 		break;
   1279 	case EJUSTRETURN:
   1280 		p = curproc;
   1281 		break;
   1282 	default:
   1283 		if (p->p_emul->e_errno)
   1284 			error = p->p_emul->e_errno[error];
   1285 		frame->tf_t1 = error;
   1286 		break;
   1287 	}
   1288 
   1289 	if (__predict_false(p->p_trace_enabled))
   1290 		trace_exit(code, rval, error);
   1291 
   1292 	userret(l, frame->tf_iioq_head, 0);
   1293 
   1294 #ifdef DIAGNOSTIC
   1295 	if (ci->ci_cpl != oldcpl) {
   1296 		printf("WARNING: SPL (0x%x) NOT LOWERED ON "
   1297 		    "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
   1298 		    ci->ci_cpl, code, args[0], args[1], args[2], p->p_pid);
   1299 		ci->ci_cpl = oldcpl;
   1300 	}
   1301 #endif
   1302 
   1303 #ifdef DEBUG
   1304 	frame_sanity_check(__func__, __LINE__, 0, frame, l);
   1305 #endif /* DEBUG */
   1306 }
   1307 
   1308 /*
   1309  * Start a new LWP
   1310  */
   1311 void
   1312 startlwp(void *arg)
   1313 {
   1314 	ucontext_t *uc = arg;
   1315 	lwp_t *l = curlwp;
   1316 	int error;
   1317 
   1318 	error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
   1319 	KASSERT(error == 0);
   1320 
   1321 	kmem_free(uc, sizeof(ucontext_t));
   1322 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1323 }
   1324