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