Home | History | Annotate | Line # | Download | only in hppa
trap.c revision 1.95
      1 /*	$NetBSD: trap.c,v 1.95 2010/12/20 00:25:34 matt 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.95 2010/12/20 00:25:34 matt Exp $");
     62 
     63 /* #define INTRDEBUG */
     64 /* #define TRAPDEBUG */
     65 /* #define USERTRACE */
     66 
     67 #include "opt_kgdb.h"
     68 #include "opt_ptrace.h"
     69 #include "opt_sa.h"
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/kernel.h>
     74 #include <sys/syscall.h>
     75 #include <sys/syscallvar.h>
     76 #include <sys/sa.h>
     77 #include <sys/savar.h>
     78 #include <sys/mutex.h>
     79 #include <sys/ktrace.h>
     80 #include <sys/proc.h>
     81 #include <sys/signalvar.h>
     82 #include <sys/acct.h>
     83 #include <sys/signal.h>
     84 #include <sys/device.h>
     85 #include <sys/kmem.h>
     86 #include <sys/userret.h>
     87 
     88 #include <net/netisr.h>
     89 
     90 #ifdef KGDB
     91 #include <sys/kgdb.h>
     92 #endif
     93 
     94 #include <uvm/uvm.h>
     95 
     96 #include <machine/iomod.h>
     97 #include <machine/cpufunc.h>
     98 #include <machine/reg.h>
     99 #include <machine/autoconf.h>
    100 
    101 #include <machine/db_machdep.h>
    102 
    103 #include <hppa/hppa/machdep.h>
    104 
    105 #include <ddb/db_output.h>
    106 #include <ddb/db_interface.h>
    107 
    108 #ifdef PTRACE
    109 void ss_clear_breakpoints(struct lwp *l);
    110 int ss_put_value(struct lwp *, vaddr_t, u_int);
    111 int ss_get_value(struct lwp *, vaddr_t, u_int *);
    112 
    113 /* single-step breakpoint */
    114 #define SSBREAKPOINT   (HPPA_BREAK_KERNEL | (HPPA_BREAK_SS << 13))
    115 
    116 #endif
    117 
    118 #if defined(DEBUG) || defined(DIAGNOSTIC)
    119 /*
    120  * 0x6fc1000 is a stwm r1, d(sr0, sp), which is the last
    121  * instruction in the function prologue that gcc -O0 uses.
    122  * When we have this instruction we know the relationship
    123  * between the stack pointer and the gcc -O0 frame pointer
    124  * (in r3, loaded with the initial sp) for the body of a
    125  * function.
    126  *
    127  * If the given instruction is a stwm r1, d(sr0, sp) where
    128  * d > 0, we evaluate to d, else we evaluate to zero.
    129  */
    130 #define STWM_R1_D_SR0_SP(inst) \
    131 	(((inst) & 0xffffc001) == 0x6fc10000 ? (((inst) & 0x00003ff) >> 1) : 0)
    132 #endif /* DEBUG || DIAGNOSTIC */
    133 
    134 const char *trap_type[] = {
    135 	"invalid",
    136 	"HPMC",
    137 	"power failure",
    138 	"recovery counter",
    139 	"external interrupt",
    140 	"LPMC",
    141 	"ITLB miss fault",
    142 	"instruction protection",
    143 	"Illegal instruction",
    144 	"break instruction",
    145 	"privileged operation",
    146 	"privileged register",
    147 	"overflow",
    148 	"conditional",
    149 	"assist exception",
    150 	"DTLB miss",
    151 	"ITLB non-access miss",
    152 	"DTLB non-access miss",
    153 	"data protection/rights/alignment",
    154 	"data break",
    155 	"TLB dirty",
    156 	"page reference",
    157 	"assist emulation",
    158 	"higher-priv transfer",
    159 	"lower-priv transfer",
    160 	"taken branch",
    161 	"data access rights",
    162 	"data protection",
    163 	"unaligned data ref",
    164 };
    165 int trap_types = __arraycount(trap_type);
    166 
    167 uint8_t fpopmap[] = {
    168 	0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
    169 	0x00, 0x0c, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00,
    170 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    171 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    172 	0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00,
    173 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    174 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    175 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    176 };
    177 
    178 void pmap_hptdump(void);
    179 void syscall(struct trapframe *, int *);
    180 
    181 #if defined(DEBUG)
    182 struct trapframe *sanity_frame;
    183 struct lwp *sanity_lwp;
    184 const char *sanity_string;
    185 void frame_sanity_check(const char *, int, int, struct trapframe *,
    186     struct lwp *);
    187 #endif
    188 
    189 
    190 #ifdef USERTRACE
    191 /*
    192  * USERTRACE is a crude facility that traces the PC of a single user process.
    193  * This tracing is normally activated by the dispatching of a certain syscall
    194  * with certain arguments - see the activation code in syscall().
    195  */
    196 static void user_backtrace(struct trapframe *, struct lwp *, int);
    197 static void user_backtrace_raw(u_int, u_int);
    198 
    199 u_int rctr_next_iioq;
    200 #endif
    201 
    202 static inline void
    203 userret(struct lwp *l, register_t pc, u_quad_t oticks)
    204 {
    205 	struct proc *p = l->l_proc;
    206 
    207 	if (l->l_md.md_astpending) {
    208 		l->l_md.md_astpending = 0;
    209 		//curcpu()->ci_data.cpu_nast++;
    210 
    211 		if (curcpu()->ci_want_resched)
    212 			preempt();
    213 	}
    214 
    215 	mi_userret(l);
    216 
    217 	/*
    218 	 * If profiling, charge recent system time to the trapped pc.
    219 	 */
    220 	if (p->p_stflag & PST_PROFIL) {
    221 		extern int psratio;
    222 
    223 		addupc_task(l, pc, (int)(p->p_sticks - oticks) * psratio);
    224 	}
    225 }
    226 
    227 /*
    228  * This handles some messy kernel debugger details.
    229  * It dispatches into either kgdb or DDB, and knows
    230  * about some special things to do, like skipping over
    231  * break instructions and how to really set up for
    232  * a single-step.
    233  */
    234 #if defined(KGDB) || defined(DDB)
    235 static int
    236 trap_kdebug(int type, int code, struct trapframe *frame)
    237 {
    238 	int handled;
    239 	u_int tf_iioq_head_old;
    240 	u_int tf_iioq_tail_old;
    241 
    242 	for (;;) {
    243 
    244 		/* This trap has not been handled. */
    245 		handled = 0;
    246 
    247 		/* Remember the instruction offset queue. */
    248 		tf_iioq_head_old = frame->tf_iioq_head;
    249 		tf_iioq_tail_old = frame->tf_iioq_tail;
    250 
    251 #ifdef	KGDB
    252 		/* Let KGDB handle it (if connected) */
    253 		if (!handled)
    254 			handled = kgdb_trap(type, frame);
    255 #endif
    256 #ifdef	DDB
    257 		/* Let DDB handle it. */
    258 		if (!handled)
    259 			handled = kdb_trap(type, code, frame);
    260 #endif
    261 
    262 		/* If this trap wasn't handled, return now. */
    263 		if (!handled)
    264 			return(0);
    265 
    266 		/*
    267 		 * If the instruction offset queue head changed, but the offset
    268 		 * queue tail didn't, assume that the user wants to jump to the
    269 		 * head offset, and adjust the tail accordingly.  This should
    270 		 * fix the kgdb `jump' command, and can help DDB users who `set'
    271 		 * the offset head but forget the tail.
    272 		 */
    273 		if (frame->tf_iioq_head != tf_iioq_head_old &&
    274 		    frame->tf_iioq_tail == tf_iioq_tail_old)
    275 			frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    276 
    277 		/*
    278 		 * This is some single-stepping support.  If we're trying to
    279 		 * step through a nullified instruction, just advance by hand
    280 		 * and trap again.  Otherwise, load the recovery counter with
    281 		 * zero.
    282 		 */
    283 		if (frame->tf_ipsw & PSW_R) {
    284 #ifdef TRAPDEBUG
    285 			printf("(single stepping at head 0x%x tail 0x%x)\n",
    286 			    frame->tf_iioq_head, frame->tf_iioq_tail);
    287 #endif
    288 			if (frame->tf_ipsw & PSW_N) {
    289 #ifdef TRAPDEBUG
    290 				printf("(single stepping past nullified)\n");
    291 #endif
    292 
    293 				/* Advance the program counter. */
    294 				frame->tf_iioq_head = frame->tf_iioq_tail;
    295 				frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    296 
    297 				/* Clear flags. */
    298 				frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
    299 
    300 				/* Simulate another trap. */
    301 				type = T_RECOVERY;
    302 				continue;
    303 			}
    304 			frame->tf_rctr = 0;
    305 		}
    306 
    307 		/* We handled this trap. */
    308 		return (1);
    309 	}
    310 	/* NOTREACHED */
    311 }
    312 #else	/* !KGDB && !DDB */
    313 #define trap_kdebug(t, c, f)	(0)
    314 #endif	/* !KGDB && !DDB */
    315 
    316 #if defined(DEBUG) || defined(USERTRACE)
    317 /*
    318  * These functions give a crude usermode backtrace.  They really only work when
    319  * code has been compiled without optimization, as they assume a certain func-
    320  * tion prologue sets up a frame pointer and stores the return pointer and arg-
    321  * uments in it.
    322  */
    323 static void
    324 user_backtrace_raw(u_int pc, u_int fp)
    325 {
    326 	int frame_number;
    327 	int arg_number;
    328 
    329 	for (frame_number = 0;
    330 	     frame_number < 100 && pc > HPPA_PC_PRIV_MASK && fp;
    331 	     frame_number++) {
    332 
    333 		printf("%3d: pc=%08x%s fp=0x%08x", frame_number,
    334 		    pc & ~HPPA_PC_PRIV_MASK, USERMODE(pc) ? "  " : "**", fp);
    335 		for (arg_number = 0; arg_number < 4; arg_number++)
    336 			printf(" arg%d=0x%08x", arg_number,
    337 			    (int) fuword(HPPA_FRAME_CARG(arg_number, fp)));
    338 		printf("\n");
    339                 pc = fuword(((register_t *) fp) - 5);	/* fetch rp */
    340 		if (pc == -1) {
    341 			printf("  fuword for pc failed\n");
    342 			break;
    343 		}
    344                 fp = fuword(((register_t *) fp) + 0);	/* fetch previous fp */
    345 		if (fp == -1) {
    346 			printf("  fuword for fp failed\n");
    347 			break;
    348 		}
    349 	}
    350 	printf("  backtrace stopped with pc %08x fp 0x%08x\n", pc, fp);
    351 }
    352 
    353 static void
    354 user_backtrace(struct trapframe *tf, struct lwp *l, int type)
    355 {
    356 	struct proc *p = l->l_proc;
    357 	u_int pc, fp, inst;
    358 
    359 	/*
    360 	 * Display any trap type that we have.
    361 	 */
    362 	if (type >= 0)
    363 		printf("pid %d (%s) trap #%d\n",
    364 		    p->p_pid, p->p_comm, type & ~T_USER);
    365 
    366 	/*
    367 	 * Assuming that the frame pointer in r3 is valid,
    368 	 * dump out a stack trace.
    369 	 */
    370 	fp = tf->tf_r3;
    371 	printf("pid %d (%s) backtrace, starting with fp 0x%08x\n",
    372 		p->p_pid, p->p_comm, fp);
    373 	user_backtrace_raw(tf->tf_iioq_head, fp);
    374 
    375 	/*
    376 	 * In case the frame pointer in r3 is not valid, assuming the stack
    377 	 * pointer is valid and the faulting function is a non-leaf, if we can
    378 	 * find its prologue we can recover its frame pointer.
    379 	 */
    380 	pc = tf->tf_iioq_head;
    381 	fp = tf->tf_sp - HPPA_FRAME_SIZE;
    382 	printf("pid %d (%s) backtrace, starting with sp 0x%08x pc 0x%08x\n",
    383 	    p->p_pid, p->p_comm, tf->tf_sp, pc);
    384 	for (pc &= ~HPPA_PC_PRIV_MASK; pc > 0; pc -= sizeof(inst)) {
    385 		inst = fuword((register_t *) pc);
    386 		if (inst == -1) {
    387 			printf("  fuword for inst at pc %08x failed\n", pc);
    388 			break;
    389 		}
    390 		/* Check for the prologue instruction that sets sp. */
    391 		if (STWM_R1_D_SR0_SP(inst)) {
    392 			fp = tf->tf_sp - STWM_R1_D_SR0_SP(inst);
    393 			printf("  sp from fp at pc %08x: %08x\n", pc, inst);
    394 			break;
    395 		}
    396 	}
    397 	user_backtrace_raw(tf->tf_iioq_head, fp);
    398 }
    399 #endif /* DEBUG || USERTRACE */
    400 
    401 #ifdef DEBUG
    402 /*
    403  * This sanity-checks a trapframe.  It is full of various assumptions about
    404  * what a healthy CPU state should be, with some documented elsewhere, some not.
    405  */
    406 void
    407 frame_sanity_check(const char *func, int line, int type, struct trapframe *tf,
    408     struct lwp *l)
    409 {
    410 #if 0
    411 	extern int kernel_text;
    412 	extern int etext;
    413 #endif
    414 	extern register_t kpsw;
    415 
    416 #define SANITY(e)					\
    417 do {							\
    418 	if (sanity_frame == NULL && !(e)) {		\
    419 		sanity_frame = tf;			\
    420 		sanity_lwp = l;				\
    421 		sanity_string = #e;			\
    422 	}						\
    423 } while (/* CONSTCOND */ 0)
    424 
    425 	KASSERT(l != NULL);
    426 	SANITY((tf->tf_ipsw & kpsw) == kpsw);
    427 	SANITY((kpsw & PSW_I) == 0 || tf->tf_eiem != 0);
    428 	if (tf->tf_iisq_head == HPPA_SID_KERNEL) {
    429 		vaddr_t minsp, maxsp, uv;
    430 
    431 		uv = uvm_lwp_getuarea(l);
    432 
    433 		/*
    434 		 * If the trap happened in the gateway page, we take the easy
    435 		 * way out and assume that the trapframe is okay.
    436 		 */
    437 		if ((tf->tf_iioq_head & ~PAGE_MASK) == SYSCALLGATE)
    438 			goto out;
    439 
    440 		SANITY(!USERMODE(tf->tf_iioq_head));
    441 		SANITY(!USERMODE(tf->tf_iioq_tail));
    442 
    443 		/*
    444 		 * Don't check the instruction queues or stack on interrupts
    445 		 * as we could be be in the sti code (outside normal kernel
    446 		 * text) or switching LWPs (curlwp and sp are not in sync)
    447 		 */
    448 		if ((type & ~T_USER) == T_INTERRUPT)
    449 			goto out;
    450 #if 0
    451 		SANITY(tf->tf_iioq_head >= (u_int) &kernel_text);
    452 		SANITY(tf->tf_iioq_head < (u_int) &etext);
    453 		SANITY(tf->tf_iioq_tail >= (u_int) &kernel_text);
    454 		SANITY(tf->tf_iioq_tail < (u_int) &etext);
    455 #endif
    456 
    457 		maxsp = uv + USPACE + PAGE_SIZE;
    458 		minsp = uv + PAGE_SIZE;
    459 
    460 		SANITY(tf->tf_sp >= minsp && tf->tf_sp < maxsp);
    461 	} else {
    462 		struct pcb *pcb = lwp_getpcb(l);
    463 
    464 		SANITY(USERMODE(tf->tf_iioq_head));
    465 		SANITY(USERMODE(tf->tf_iioq_tail));
    466 		SANITY(tf->tf_cr30 == (u_int)pcb->pcb_fpregs);
    467 	}
    468 #undef SANITY
    469 out:
    470 	if (sanity_frame == tf) {
    471 		printf("insanity: '%s' at %s:%d type 0x%x tf %p lwp %p "
    472 		    "sp 0x%x pc 0x%x\n",
    473 		    sanity_string, func, line, type, sanity_frame, sanity_lwp,
    474 		    tf->tf_sp, tf->tf_iioq_head);
    475 		(void) trap_kdebug(T_IBREAK, 0, tf);
    476 		sanity_frame = NULL;
    477 		sanity_lwp = NULL;
    478 	}
    479 }
    480 #endif /* DEBUG */
    481 
    482 void
    483 trap(int type, struct trapframe *frame)
    484 {
    485 	struct lwp *l;
    486 	struct proc *p;
    487 	struct pcb *pcb;
    488 	vaddr_t va;
    489 	struct vm_map *map;
    490 	struct vmspace *vm;
    491 	vm_prot_t vftype;
    492 	pa_space_t space;
    493 	ksiginfo_t ksi;
    494 	u_int opcode, onfault;
    495 	int ret;
    496 	const char *tts = "reserved";
    497 	int trapnum;
    498 #ifdef DIAGNOSTIC
    499 	extern int emergency_stack_start, emergency_stack_end;
    500 	struct cpu_info *ci = curcpu();
    501 	int oldcpl = ci->ci_cpl;
    502 #endif
    503 
    504 	trapnum = type & ~T_USER;
    505 	opcode = frame->tf_iir;
    506 
    507 	if (trapnum <= T_EXCEPTION || trapnum == T_HIGHERPL ||
    508 	    trapnum == T_LOWERPL || trapnum == T_TAKENBR ||
    509 	    trapnum == T_IDEBUG || trapnum == T_PERFMON) {
    510 		va = frame->tf_iioq_head;
    511 		space = frame->tf_iisq_head;
    512 		vftype = VM_PROT_EXECUTE;
    513 	} else {
    514 		va = frame->tf_ior;
    515 		space = frame->tf_isr;
    516 		vftype = inst_store(opcode) ? VM_PROT_WRITE : VM_PROT_READ;
    517 	}
    518 
    519 	KASSERT(curlwp != NULL);
    520 	l = curlwp;
    521 	p = l->l_proc;
    522 	if ((type & T_USER) != 0)
    523 		LWP_CACHE_CREDS(l, p);
    524 
    525 #ifdef DIAGNOSTIC
    526 	/*
    527 	 * If we are on the emergency stack, then we either got
    528 	 * a fault on the kernel stack, or we're just handling
    529 	 * a trap for the machine check handler (which also
    530 	 * runs on the emergency stack).
    531 	 *
    532 	 * We *very crudely* differentiate between the two cases
    533 	 * by checking the faulting instruction: if it is the
    534 	 * function prologue instruction that stores the old
    535 	 * frame pointer and updates the stack pointer, we assume
    536 	 * that we faulted on the kernel stack.
    537 	 *
    538 	 * In this case, not completing that instruction will
    539 	 * probably confuse backtraces in kgdb/ddb.  Completing
    540 	 * it would be difficult, because we already faulted on
    541 	 * that part of the stack, so instead we fix up the
    542 	 * frame as if the function called has just returned.
    543 	 * This has peculiar knowledge about what values are in
    544 	 * what registers during the "normal gcc -g" prologue.
    545 	 */
    546 	if (&type >= &emergency_stack_start &&
    547 	    &type < &emergency_stack_end &&
    548 	    type != T_IBREAK && STWM_R1_D_SR0_SP(opcode)) {
    549 		/* Restore the caller's frame pointer. */
    550 		frame->tf_r3 = frame->tf_r1;
    551 		/* Restore the caller's instruction offsets. */
    552 		frame->tf_iioq_head = frame->tf_rp;
    553 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    554 		goto dead_end;
    555 	}
    556 #endif /* DIAGNOSTIC */
    557 
    558 #ifdef DEBUG
    559 	frame_sanity_check(__func__, __LINE__, type, frame, l);
    560 #endif /* DEBUG */
    561 
    562 	if (frame->tf_flags & TFF_LAST)
    563 		l->l_md.md_regs = frame;
    564 
    565 	if (trapnum <= trap_types)
    566 		tts = trap_type[trapnum];
    567 
    568 #ifdef TRAPDEBUG
    569 	if (trapnum != T_INTERRUPT && trapnum != T_IBREAK)
    570 		printf("trap: %d, %s for %x:%lx at %x:%x, fp=%p, rp=%x\n",
    571 		    type, tts, space, va, frame->tf_iisq_head,
    572 		    frame->tf_iioq_head, frame, frame->tf_rp);
    573 	else if (trapnum == T_IBREAK)
    574 		printf("trap: break instruction %x:%x at %x:%x, fp=%p\n",
    575 		    break5(opcode), break13(opcode),
    576 		    frame->tf_iisq_head, frame->tf_iioq_head, frame);
    577 
    578 	{
    579 		extern int etext;
    580 		if (frame < (struct trapframe *)&etext) {
    581 			printf("trap: bogus frame ptr %p\n", frame);
    582 			goto dead_end;
    583 		}
    584 	}
    585 #endif
    586 
    587 	pcb = lwp_getpcb(l);
    588 
    589 	/* If this is a trap, not an interrupt, reenable interrupts. */
    590 	if (trapnum != T_INTERRUPT) {
    591 		curcpu()->ci_data.cpu_ntrap++;
    592 		mtctl(frame->tf_eiem, CR_EIEM);
    593 	}
    594 
    595 	switch (type) {
    596 	case T_NONEXIST:
    597 	case T_NONEXIST|T_USER:
    598 #if !defined(DDB) && !defined(KGDB)
    599 		/* we've got screwed up by the central scrutinizer */
    600 		panic ("trap: elvis has just left the building!");
    601 		break;
    602 #else
    603 		goto dead_end;
    604 #endif
    605 	case T_RECOVERY|T_USER:
    606 #ifdef USERTRACE
    607 		for (;;) {
    608 			if (frame->tf_iioq_head != rctr_next_iioq)
    609 				printf("-%08x\nr %08x",
    610 					rctr_next_iioq - 4,
    611 					frame->tf_iioq_head);
    612 			rctr_next_iioq = frame->tf_iioq_head + 4;
    613 			if (frame->tf_ipsw & PSW_N) {
    614 				/* Advance the program counter. */
    615 				frame->tf_iioq_head = frame->tf_iioq_tail;
    616 				frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    617 				/* Clear flags. */
    618 				frame->tf_ipsw &= ~(PSW_N|PSW_X|PSW_Y|PSW_Z|PSW_B|PSW_T|PSW_H|PSW_L);
    619 				/* Simulate another trap. */
    620 				continue;
    621 			}
    622 			break;
    623 		}
    624 		frame->tf_rctr = 0;
    625 		break;
    626 #endif /* USERTRACE */
    627 	case T_RECOVERY:
    628 #if !defined(DDB) && !defined(KGDB)
    629 		/* XXX will implement later */
    630 		printf ("trap: handicapped");
    631 		break;
    632 #else
    633 		goto dead_end;
    634 #endif
    635 
    636 	case T_EMULATION | T_USER:
    637 #ifdef FPEMUL
    638 		hppa_fpu_emulate(frame, l, opcode);
    639 #else  /* !FPEMUL */
    640 		/*
    641 		 * We don't have FPU emulation, so signal the
    642 		 * process with a SIGFPE.
    643 		 */
    644 
    645 		KSI_INIT_TRAP(&ksi);
    646 		ksi.ksi_signo = SIGFPE;
    647 		ksi.ksi_code = SI_NOINFO;
    648 		ksi.ksi_trap = type;
    649 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    650 		trapsignal(l, &ksi);
    651 #endif /* !FPEMUL */
    652 		break;
    653 
    654 	case T_DATALIGN:
    655 		onfault = pcb->pcb_onfault;
    656 		if (onfault) {
    657 			ret = EFAULT;
    658 do_onfault:
    659 			frame->tf_iioq_head = onfault;
    660 			frame->tf_iioq_tail = frame->tf_iioq_head + 4;
    661 			frame->tf_ret0 = ret;
    662 			break;
    663 		}
    664 		/*FALLTHROUGH*/
    665 
    666 #ifdef DIAGNOSTIC
    667 		/* these just can't happen ever */
    668 	case T_PRIV_OP:
    669 	case T_PRIV_REG:
    670 		/* these just can't make it to the trap() ever */
    671 	case T_HPMC:
    672 	case T_HPMC | T_USER:
    673 	case T_EMULATION:
    674 	case T_EXCEPTION:
    675 #endif
    676 	case T_IBREAK:
    677 	case T_DBREAK:
    678 	dead_end:
    679 		if (type & T_USER) {
    680 #ifdef DEBUG
    681 			user_backtrace(frame, l, type);
    682 #endif
    683 			KSI_INIT_TRAP(&ksi);
    684 			ksi.ksi_signo = SIGILL;
    685 			ksi.ksi_code = ILL_ILLTRP;
    686 			ksi.ksi_trap = type;
    687 			ksi.ksi_addr = (void *)frame->tf_iioq_head;
    688 			trapsignal(l, &ksi);
    689 			break;
    690 		}
    691 		if (trap_kdebug(type, va, frame))
    692 			return;
    693 		else if (type == T_DATALIGN)
    694 			panic ("trap: %s at 0x%x", tts, (u_int) va);
    695 		else
    696 			panic ("trap: no debugger for \"%s\" (%d)", tts, type);
    697 		break;
    698 
    699 	case T_IBREAK | T_USER:
    700 	case T_DBREAK | T_USER:
    701 		KSI_INIT_TRAP(&ksi);
    702 		ksi.ksi_signo = SIGTRAP;
    703 		ksi.ksi_code = TRAP_TRACE;
    704 		ksi.ksi_trap = trapnum;
    705 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    706 #ifdef PTRACE
    707 		ss_clear_breakpoints(l);
    708 		if (opcode == SSBREAKPOINT)
    709 			ksi.ksi_code = TRAP_BRKPT;
    710 #endif
    711 		/* pass to user debugger */
    712 		trapsignal(l, &ksi);
    713 
    714 		break;
    715 
    716 #ifdef PTRACE
    717 	case T_TAKENBR | T_USER:
    718 		ss_clear_breakpoints(l);
    719 
    720 		KSI_INIT_TRAP(&ksi);
    721 		ksi.ksi_signo = SIGTRAP;
    722 		ksi.ksi_code = TRAP_TRACE;
    723 		ksi.ksi_trap = trapnum;
    724 		ksi.ksi_addr = (void *)frame->tf_iioq_head;
    725 
    726                 /* pass to user debugger */
    727 		trapsignal(l, &ksi);
    728 		break;
    729 #endif
    730 
    731 	case T_EXCEPTION | T_USER: {	/* co-proc assist trap */
    732 		uint64_t *fpp;
    733 		uint32_t *pex, ex, inst;
    734 		int i;
    735 
    736 		hppa_fpu_flush(l);
    737 		fpp = (uint64_t *)pcb->pcb_fpregs;
    738 
    739 		/* skip the status register */
    740 		pex = (uint32_t *)&fpp[0];
    741 		pex++;
    742 
    743 		/* loop through the exception registers */
    744 		for (i = 1; i < 8 && !*pex; i++, pex++)
    745 			;
    746 		KASSERT(i < 8);
    747 		ex = *pex;
    748 		*pex = 0;
    749 
    750 		/* reset the trap flag, as if there was none */
    751 		fpp[0] &= ~(((uint64_t)HPPA_FPU_T) << 32);
    752 
    753 		/* emulate the instruction */
    754 		inst = ((uint32_t)fpopmap[ex >> 26] << 26) | (ex & 0x03ffffff);
    755 		hppa_fpu_emulate(frame, l, inst);
    756 		}
    757 		break;
    758 
    759 	case T_OVERFLOW | T_USER:
    760 		KSI_INIT_TRAP(&ksi);
    761 		ksi.ksi_signo = SIGFPE;
    762 		ksi.ksi_code = SI_NOINFO;
    763 		ksi.ksi_trap = type;
    764 		ksi.ksi_addr = (void *)va;
    765 		trapsignal(l, &ksi);
    766 		break;
    767 
    768 	case T_CONDITION | T_USER:
    769 		KSI_INIT_TRAP(&ksi);
    770 		ksi.ksi_signo = SIGFPE;
    771 		ksi.ksi_code = FPE_INTDIV;
    772 		ksi.ksi_trap = type;
    773 		ksi.ksi_addr = (void *)va;
    774 		trapsignal(l, &ksi);
    775 		break;
    776 
    777 	case T_ILLEGAL | T_USER:
    778 #ifdef DEBUG
    779 		user_backtrace(frame, l, type);
    780 #endif
    781 		KSI_INIT_TRAP(&ksi);
    782 		ksi.ksi_signo = SIGILL;
    783 		ksi.ksi_code = ILL_ILLOPC;
    784 		ksi.ksi_trap = type;
    785 		ksi.ksi_addr = (void *)va;
    786 		trapsignal(l, &ksi);
    787 		break;
    788 
    789 	case T_PRIV_OP | T_USER:
    790 #ifdef DEBUG
    791 		user_backtrace(frame, l, type);
    792 #endif
    793 		KSI_INIT_TRAP(&ksi);
    794 		ksi.ksi_signo = SIGILL;
    795 		ksi.ksi_code = ILL_PRVOPC;
    796 		ksi.ksi_trap = type;
    797 		ksi.ksi_addr = (void *)va;
    798 		trapsignal(l, &ksi);
    799 		break;
    800 
    801 	case T_PRIV_REG | T_USER:
    802 #ifdef DEBUG
    803 		user_backtrace(frame, l, type);
    804 #endif
    805 		KSI_INIT_TRAP(&ksi);
    806 		ksi.ksi_signo = SIGILL;
    807 		ksi.ksi_code = ILL_PRVREG;
    808 		ksi.ksi_trap = type;
    809 		ksi.ksi_addr = (void *)va;
    810 		trapsignal(l, &ksi);
    811 		break;
    812 
    813 		/* these should never got here */
    814 	case T_HIGHERPL | T_USER:
    815 	case T_LOWERPL | T_USER:
    816 		KSI_INIT_TRAP(&ksi);
    817 		ksi.ksi_signo = SIGSEGV;
    818 		ksi.ksi_code = SEGV_ACCERR;
    819 		ksi.ksi_trap = type;
    820 		ksi.ksi_addr = (void *)va;
    821 		trapsignal(l, &ksi);
    822 		break;
    823 
    824 	case T_IPROT | T_USER:
    825 	case T_DPROT | T_USER:
    826 		KSI_INIT_TRAP(&ksi);
    827 		ksi.ksi_signo = SIGSEGV;
    828 		ksi.ksi_code = SEGV_ACCERR;
    829 		ksi.ksi_trap = type;
    830 		ksi.ksi_addr = (void *)va;
    831 		trapsignal(l, &ksi);
    832 		break;
    833 
    834 	case T_DATACC:   	case T_USER | T_DATACC:
    835 	case T_ITLBMISS:	case T_USER | T_ITLBMISS:
    836 	case T_DTLBMISS:	case T_USER | T_DTLBMISS:
    837 	case T_ITLBMISSNA:	case T_USER | T_ITLBMISSNA:
    838 	case T_DTLBMISSNA:	case T_USER | T_DTLBMISSNA:
    839 	case T_TLB_DIRTY:	case T_USER | T_TLB_DIRTY:
    840 		vm = p->p_vmspace;
    841 
    842 		if (!vm) {
    843 #ifdef TRAPDEBUG
    844 			printf("trap: no vm, p=%p\n", p);
    845 #endif
    846 			goto dead_end;
    847 		}
    848 
    849 		/*
    850 		 * it could be a kernel map for exec_map faults
    851 		 */
    852 		if (!(type & T_USER) && space == HPPA_SID_KERNEL)
    853 			map = kernel_map;
    854 		else {
    855 			map = &vm->vm_map;
    856 			if ((l->l_flag & LW_SA)
    857 			    && (~l->l_pflag & LP_SA_NOBLOCK)) {
    858 				l->l_savp->savp_faultaddr = va;
    859 				l->l_pflag |= LP_SA_PAGEFAULT;
    860 			}
    861 		}
    862 
    863 		va = trunc_page(va);
    864 
    865 		if (map->pmap->pm_space != space) {
    866 #ifdef TRAPDEBUG
    867 			printf("trap: space mismatch %d != %d\n",
    868 			    space, map->pmap->pm_space);
    869 #endif
    870 			/* actually dump the user, crap the kernel */
    871 			goto dead_end;
    872 		}
    873 
    874 		/* Never call uvm_fault in interrupt context. */
    875 		KASSERT(curcpu()->ci_cpl == 0);
    876 
    877 		onfault = pcb->pcb_onfault;
    878 		pcb->pcb_onfault = 0;
    879 		ret = uvm_fault(map, va, vftype);
    880 		pcb->pcb_onfault = onfault;
    881 
    882 #ifdef TRAPDEBUG
    883 		printf("uvm_fault(%p, %x, %d)=%d\n",
    884 		    map, (u_int)va, vftype, ret);
    885 #endif
    886 
    887 		if (map != kernel_map)
    888 			l->l_pflag &= ~LP_SA_PAGEFAULT;
    889 
    890 		/*
    891 		 * If this was a stack access we keep track of the maximum
    892 		 * accessed stack size.  Also, if uvm_fault gets a protection
    893 		 * failure it is due to accessing the stack region outside
    894 		 * the current limit and we need to reflect that as an access
    895 		 * error.
    896 		 */
    897 		if (map != kernel_map && va >= (vaddr_t)vm->vm_minsaddr) {
    898 			if (ret == 0)
    899 				uvm_grow(l->l_proc, va);
    900 			else if (ret == EACCES)
    901 				ret = EFAULT;
    902 		}
    903 
    904 		if (ret != 0) {
    905 			if (type & T_USER) {
    906 #ifdef DEBUG
    907 				user_backtrace(frame, l, type);
    908 #endif
    909 				KSI_INIT_TRAP(&ksi);
    910 				ksi.ksi_signo = SIGSEGV;
    911 				ksi.ksi_code = (ret == EACCES ?
    912 						SEGV_ACCERR : SEGV_MAPERR);
    913 				ksi.ksi_trap = type;
    914 				ksi.ksi_addr = (void *)va;
    915 				trapsignal(l, &ksi);
    916 			} else {
    917 				if (onfault) {
    918 					goto do_onfault;
    919 				}
    920 				panic("trap: uvm_fault(%p, %lx, %d): %d",
    921 				    map, va, vftype, ret);
    922 			}
    923 		} else if ((type & T_USER) == 0) {
    924 			extern char ucas_ras_start[];
    925 			extern char ucas_ras_end[];
    926 
    927 			if (frame->tf_iioq_head > (u_int)ucas_ras_start &&
    928 			    frame->tf_iioq_head < (u_int)ucas_ras_end) {
    929 				frame->tf_iioq_head = (u_int)ucas_ras_start;
    930 				frame->tf_iioq_tail = (u_int)ucas_ras_start + 4;
    931 			}
    932 		}
    933 		break;
    934 
    935 	case T_DATALIGN | T_USER:
    936 #ifdef DEBUG
    937 		user_backtrace(frame, l, type);
    938 #endif
    939 		KSI_INIT_TRAP(&ksi);
    940 		ksi.ksi_signo = SIGBUS;
    941 		ksi.ksi_code = BUS_ADRALN;
    942 		ksi.ksi_trap = type;
    943 		ksi.ksi_addr = (void *)va;
    944 		trapsignal(l, &ksi);
    945 		break;
    946 
    947 	case T_INTERRUPT:
    948 	case T_INTERRUPT|T_USER:
    949 		hppa_intr(frame);
    950 		mtctl(frame->tf_eiem, CR_EIEM);
    951 		break;
    952 
    953 	case T_LOWERPL:
    954 	case T_DPROT:
    955 	case T_IPROT:
    956 	case T_OVERFLOW:
    957 	case T_CONDITION:
    958 	case T_ILLEGAL:
    959 	case T_HIGHERPL:
    960 	case T_TAKENBR:
    961 	case T_POWERFAIL:
    962 	case T_LPMC:
    963 	case T_PAGEREF:
    964 	case T_DATAPID:  	case T_DATAPID  | T_USER:
    965 		if (0 /* T-chip */) {
    966 			break;
    967 		}
    968 		/* FALLTHROUGH to unimplemented */
    969 	default:
    970 		panic ("trap: unimplemented \'%s\' (%d)", tts, type);
    971 	}
    972 
    973 #ifdef DIAGNOSTIC
    974 	if (ci->ci_cpl != oldcpl)
    975 		printf("WARNING: SPL (%d) NOT LOWERED ON TRAP (%d) EXIT\n",
    976 		    ci->ci_cpl, trapnum);
    977 #endif
    978 
    979 	if (type & T_USER)
    980 		userret(l, l->l_md.md_regs->tf_iioq_head, 0);
    981 
    982 #ifdef DEBUG
    983 	frame_sanity_check(__func__, __LINE__, type, frame, l);
    984 	if (frame->tf_flags & TFF_LAST && (curlwp->l_flag & LW_IDLE) == 0)
    985 		frame_sanity_check(__func__, __LINE__, type,
    986 		    curlwp->l_md.md_regs, curlwp);
    987 #endif /* DEBUG */
    988 }
    989 
    990 void
    991 child_return(void *arg)
    992 {
    993 	struct lwp *l = arg;
    994 
    995 	/*
    996 	 * Return values in the frame set by cpu_lwp_fork().
    997 	 */
    998 
    999 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1000 	ktrsysret(SYS_fork, 0, 0);
   1001 #ifdef DEBUG
   1002 	frame_sanity_check(__func__, __LINE__, 0, l->l_md.md_regs, l);
   1003 #endif /* DEBUG */
   1004 }
   1005 
   1006 #ifdef PTRACE
   1007 
   1008 #include <sys/ptrace.h>
   1009 
   1010 int
   1011 ss_get_value(struct lwp *l, vaddr_t addr, u_int *value)
   1012 {
   1013 	struct uio uio;
   1014 	struct iovec iov;
   1015 
   1016 	iov.iov_base = (void *)value;
   1017 	iov.iov_len = sizeof(u_int);
   1018 	uio.uio_iov = &iov;
   1019 	uio.uio_iovcnt = 1;
   1020 	uio.uio_offset = (off_t)addr;
   1021 	uio.uio_resid = sizeof(u_int);
   1022 	uio.uio_rw = UIO_READ;
   1023 	UIO_SETUP_SYSSPACE(&uio);
   1024 
   1025 	return (process_domem(curlwp, l, &uio));
   1026 }
   1027 
   1028 int
   1029 ss_put_value(struct lwp *l, vaddr_t addr, u_int value)
   1030 {
   1031 	struct uio uio;
   1032 	struct iovec iov;
   1033 
   1034 	iov.iov_base = (void *)&value;
   1035 	iov.iov_len = sizeof(u_int);
   1036 	uio.uio_iov = &iov;
   1037 	uio.uio_iovcnt = 1;
   1038 	uio.uio_offset = (off_t)addr;
   1039 	uio.uio_resid = sizeof(u_int);
   1040 	uio.uio_rw = UIO_WRITE;
   1041 	UIO_SETUP_SYSSPACE(&uio);
   1042 
   1043 	return (process_domem(curlwp, l, &uio));
   1044 }
   1045 
   1046 void
   1047 ss_clear_breakpoints(struct lwp *l)
   1048 {
   1049 	/* Restore origional instructions. */
   1050 	if (l->l_md.md_bpva != 0) {
   1051 		ss_put_value(l, l->l_md.md_bpva, l->l_md.md_bpsave[0]);
   1052 		ss_put_value(l, l->l_md.md_bpva + 4, l->l_md.md_bpsave[1]);
   1053 		l->l_md.md_bpva = 0;
   1054 	}
   1055 }
   1056 
   1057 
   1058 int
   1059 process_sstep(struct lwp *l, int sstep)
   1060 {
   1061 	struct trapframe *tf = l->l_md.md_regs;
   1062 	int error;
   1063 
   1064 	ss_clear_breakpoints(l);
   1065 
   1066 	/* We're continuing... */
   1067 	if (sstep == 0) {
   1068 		tf->tf_ipsw &= ~PSW_T;
   1069 		return 0;
   1070 	}
   1071 
   1072 	/*
   1073 	 * Don't touch the syscall gateway page.  Instead, insert a
   1074 	 * breakpoint where we're supposed to return.
   1075 	 */
   1076 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1077 		l->l_md.md_bpva = tf->tf_r31 & ~HPPA_PC_PRIV_MASK;
   1078 	else
   1079 		l->l_md.md_bpva = tf->tf_iioq_tail & ~HPPA_PC_PRIV_MASK;
   1080 
   1081 	error = ss_get_value(l, l->l_md.md_bpva, &l->l_md.md_bpsave[0]);
   1082 	if (error)
   1083 		return error;
   1084 	error = ss_get_value(l, l->l_md.md_bpva + 4, &l->l_md.md_bpsave[1]);
   1085 	if (error)
   1086 		return error;
   1087 
   1088 	error = ss_put_value(l, l->l_md.md_bpva, SSBREAKPOINT);
   1089 	if (error)
   1090 		return error;
   1091 	error = ss_put_value(l, l->l_md.md_bpva + 4, SSBREAKPOINT);
   1092 	if (error)
   1093 		return error;
   1094 
   1095 	if ((tf->tf_iioq_tail & ~PAGE_MASK) == SYSCALLGATE)
   1096 		tf->tf_ipsw &= ~PSW_T;
   1097 	else
   1098 		tf->tf_ipsw |= PSW_T;
   1099 
   1100 	return 0;
   1101 }
   1102 #endif
   1103 
   1104 
   1105 /*
   1106  * call actual syscall routine
   1107  * from the low-level syscall handler:
   1108  * - all HPPA_FRAME_NARGS syscall's arguments supposed to be copied onto
   1109  *   our stack, this wins compared to copyin just needed amount anyway
   1110  * - register args are copied onto stack too
   1111  */
   1112 void
   1113 syscall(struct trapframe *frame, int *args)
   1114 {
   1115 	struct lwp *l;
   1116 	struct proc *p;
   1117 	const struct sysent *callp;
   1118 	size_t nargs64;
   1119 	int nsys, code, error;
   1120 	int tmp;
   1121 	int rval[2];
   1122 #ifdef DIAGNOSTIC
   1123 	struct cpu_info *ci = curcpu();
   1124 	int oldcpl = ci->ci_cpl;
   1125 #endif
   1126 
   1127 	curcpu()->ci_data.cpu_nsyscall++;
   1128 
   1129 #ifdef DEBUG
   1130 	frame_sanity_check(__func__, __LINE__, 0, frame, curlwp);
   1131 #endif /* DEBUG */
   1132 
   1133 	if (!USERMODE(frame->tf_iioq_head))
   1134 		panic("syscall");
   1135 
   1136 	KASSERT(curlwp != NULL);
   1137 	l = curlwp;
   1138 	p = l->l_proc;
   1139 	l->l_md.md_regs = frame;
   1140 	nsys = p->p_emul->e_nsysent;
   1141 	callp = p->p_emul->e_sysent;
   1142 	code = frame->tf_t1;
   1143 	LWP_CACHE_CREDS(l, p);
   1144 
   1145 #ifdef KERN_SA
   1146 	if (__predict_false((l->l_savp)
   1147             && (l->l_savp->savp_pflags & SAVP_FLAG_DELIVERING)))
   1148 		l->l_savp->savp_pflags &= ~SAVP_FLAG_DELIVERING;
   1149 #endif
   1150 
   1151 	/*
   1152 	 * Restarting a system call is touchy on the HPPA, because syscall
   1153 	 * arguments are passed in registers and the program counter of the
   1154 	 * syscall "point" isn't easily divined.
   1155 	 *
   1156 	 * We handle the first problem by assuming that we will have to restart
   1157 	 * this system call, so we stuff the first four words of the original
   1158 	 * arguments back into the frame as arg0...arg3, which is where we
   1159 	 * found them in the first place.  Any further arguments are (still) on
   1160 	 * the user's stack and the  syscall code will fetch them from there
   1161 	 * (again).
   1162 	 *
   1163 	 * The program counter problem is addressed below.
   1164 	 */
   1165 	frame->tf_arg0 = args[0];
   1166 	frame->tf_arg1 = args[1];
   1167 	frame->tf_arg2 = args[2];
   1168 	frame->tf_arg3 = args[3];
   1169 
   1170 	/*
   1171 	 * Some special handling for the syscall(2) and
   1172 	 * __syscall(2) system calls.
   1173 	 */
   1174 	switch (code) {
   1175 	case SYS_syscall:
   1176 		code = *args;
   1177 		args += 1;
   1178 		break;
   1179 	case SYS___syscall:
   1180 		if (callp != sysent)
   1181 			break;
   1182 		/*
   1183 		 * NB: even though __syscall(2) takes a quad_t containing the
   1184 		 * system call number, because our argument copying word-swaps
   1185 		 * 64-bit arguments, the least significant word of that quad_t
   1186 		 * is the first word in the argument array.
   1187 		 */
   1188 		code = *args;
   1189 		args += 2;
   1190 	}
   1191 
   1192 	/*
   1193 	 * Stacks growing from lower addresses to higher addresses are not
   1194 	 * really such a good idea, because it makes it impossible to overlay a
   1195 	 * struct on top of C stack arguments (the arguments appear in
   1196 	 * reversed order).
   1197 	 *
   1198 	 * You can do the obvious thing (as locore.S does) and copy argument
   1199 	 * words one by one, laying them out in the "right" order in the dest-
   1200 	 * ination buffer, but this ends up word-swapping multi-word arguments
   1201 	 * (like off_t).
   1202 	 *
   1203 	 * FIXME - this works only on native binaries and
   1204 	 * will probably screw up any and all emulation.
   1205 	 *
   1206 	 */
   1207 
   1208 	if (code < 0 || code >= nsys)
   1209 		callp += p->p_emul->e_nosys;	/* bad syscall # */
   1210 	else
   1211 		callp += code;
   1212 
   1213 	nargs64 = SYCALL_NARGS64(callp);
   1214 	if (nargs64 != 0) {
   1215 		size_t nargs = callp->sy_narg;
   1216 
   1217 		for (size_t i = 0; i < nargs + nargs64;) {
   1218 			if (SYCALL_ARG_64_P(callp, i)) {
   1219 				tmp = args[i];
   1220 				args[i] = args[i + 1];
   1221 				args[i + 1] = tmp;
   1222 				i += 2;
   1223 			} else
   1224 				i++;
   1225 		}
   1226 	}
   1227 
   1228 #ifdef USERTRACE
   1229 	if (0) {
   1230 		user_backtrace(frame, l, -1);
   1231 		frame->tf_ipsw |= PSW_R;
   1232 		frame->tf_rctr = 0;
   1233 		printf("r %08x", frame->tf_iioq_head);
   1234 		rctr_next_iioq = frame->tf_iioq_head + 4;
   1235 	}
   1236 #endif
   1237 
   1238 	error = 0;
   1239 	if (__predict_false(p->p_trace_enabled)) {
   1240 		error = trace_enter(code, args, callp->sy_narg);
   1241 		if (error)
   1242 			goto out;
   1243 	}
   1244 
   1245 	rval[0] = 0;
   1246 	rval[1] = 0;
   1247 	error = sy_call(callp, l, args, rval);
   1248 out:
   1249 	switch (error) {
   1250 	case 0:
   1251 		l = curlwp;			/* changes on exec() */
   1252 		frame = l->l_md.md_regs;
   1253 		frame->tf_ret0 = rval[0];
   1254 		frame->tf_ret1 = rval[1];
   1255 		frame->tf_t1 = 0;
   1256 		break;
   1257 	case ERESTART:
   1258 		/*
   1259 		 * Now we have to wind back the instruction offset queue to the
   1260 		 * point where the system call will be made again.  This is
   1261 		 * inherently tied to the SYSCALL macro.
   1262 		 *
   1263 		 * Currently, the part of the SYSCALL macro that we want to re-
   1264 		 * run reads as:
   1265 		 *
   1266 		 *	ldil	L%SYSCALLGATE, r1
   1267 		 *	ble	4(sr7, r1)
   1268 		 *	ldi	__CONCAT(SYS_,x), t1
   1269 		 *	comb,<>	%r0, %t1, __cerror
   1270 		 *
   1271 		 * And our offset queue head points to the comb instruction.
   1272 		 * So we need to subtract twelve to reach the ldil.
   1273 		 */
   1274 		frame->tf_iioq_head -= 12;
   1275 		frame->tf_iioq_tail = frame->tf_iioq_head + 4;
   1276 		break;
   1277 	case EJUSTRETURN:
   1278 		p = curproc;
   1279 		break;
   1280 	default:
   1281 		if (p->p_emul->e_errno)
   1282 			error = p->p_emul->e_errno[error];
   1283 		frame->tf_t1 = error;
   1284 		break;
   1285 	}
   1286 
   1287 	if (__predict_false(p->p_trace_enabled))
   1288 		trace_exit(code, rval, error);
   1289 
   1290 	userret(l, frame->tf_iioq_head, 0);
   1291 
   1292 #ifdef DIAGNOSTIC
   1293 	if (ci->ci_cpl != oldcpl) {
   1294 		printf("WARNING: SPL (0x%x) NOT LOWERED ON "
   1295 		    "syscall(0x%x, 0x%x, 0x%x, 0x%x...) EXIT, PID %d\n",
   1296 		    ci->ci_cpl, code, args[0], args[1], args[2], p->p_pid);
   1297 		ci->ci_cpl = oldcpl;
   1298 	}
   1299 #endif
   1300 
   1301 #ifdef DEBUG
   1302 	frame_sanity_check(__func__, __LINE__, 0, frame, l);
   1303 #endif /* DEBUG */
   1304 }
   1305 
   1306 /*
   1307  * Start a new LWP
   1308  */
   1309 void
   1310 startlwp(void *arg)
   1311 {
   1312 	ucontext_t *uc = arg;
   1313 	lwp_t *l = curlwp;
   1314 	int error;
   1315 
   1316 	error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
   1317 	KASSERT(error == 0);
   1318 
   1319 	kmem_free(uc, sizeof(ucontext_t));
   1320 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1321 }
   1322 
   1323 /*
   1324  * XXX This is a terrible name.
   1325  */
   1326 void
   1327 upcallret(struct lwp *l)
   1328 {
   1329 	userret(l, l->l_md.md_regs->tf_iioq_head, 0);
   1330 }
   1331