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