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