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