code:
/* $OpenBSD: subr.S,v 1.24 2014/06/08 13:20:39 miod Exp $	*/
/*
 * Mach Operating System
 * Copyright (c) 1993-1992 Carnegie Mellon University
 * Copyright (c) 1991 OMRON Corporation
 * Copyright (c) 1996 Nivas Madhur
 * Copyright (c) 1998 Steve Murphree, Jr.
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON AND OMRON ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON AND OMRON DISCLAIM ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#include "assym.h"

#include <sys/errno.h>

#include <machine/param.h>
#include <machine/asm.h>
#include <machine/psl.h>
#include <machine/trap.h>

#ifdef	M88100

/*
 * DO_LOAD_ADDRESS
 *
 * 	unsigned int do_load_word(address, supervisor_mode)
 *		vaddr_t address;		\\ in r2
 *		boolean_t supervisor_mode;	\\ in r3
 *
 * Return the word at ADDRESS (from user space if SUPERVISOR_MODE is zero,
 * supervisor space if non-zero).
 *
 */

ENTRY(do_load_word)	/* do_load_word(address, supervisor) */
	bcnd	ne0,%r3,1f
#ifdef ERRATA__XXX_USR
	NOP
	ld.usr	%r2,%r2,%r0
	NOP
	NOP
	NOP
	jmp	%r1
#else
	jmp.n	%r1
	 ld.usr	%r2,%r2,%r0
#endif
1:	jmp.n	%r1
	 ld	%r2,%r2,%r0

ENTRY(do_load_half)	/* do_load_half(address, supervisor) */
	bcnd	ne0,%r3,1f
#ifdef ERRATA__XXX_USR
	NOP
	ld.h.usr	%r2,%r2,%r0
	NOP
	NOP
	NOP
	jmp	%r1
#else
	jmp.n	%r1
	 ld.h.usr	%r2,%r2,%r0
#endif
1:	jmp.n	%r1
	 ld.h	%r2,%r2,%r0

ENTRY(do_load_byte)	/* do_load_byte(address, supervisor) */
	bcnd	ne0,%r3,1f
#ifdef ERRATA__XXX_USR
	NOP
	ld.b.usr	%r2,%r2,%r0
	NOP
	NOP
	NOP
	jmp	%r1
#else
	jmp.n	%r1
	 ld.b.usr	%r2,%r2,%r0
#endif
1:	jmp.n	%r1
	 ld.b	%r2,%r2,%r0

ENTRY(do_store_word)	/* do_store_word(address, data, supervisor) */
	bcnd	ne0,%r4,1f
#ifdef ERRATA__XXX_USR
	NOP
	st.usr	%r3,%r2,%r0
	NOP
	NOP
	NOP
	jmp	%r1
#else
	jmp.n	%r1
	 st.usr	%r3,%r2,%r0
#endif
1:	jmp.n	%r1
	 st	%r3,%r2,%r0

ENTRY(do_store_half)	/* do_store_half(address, data, supervisor) */
	bcnd	ne0,%r4,1f
#ifdef ERRATA__XXX_USR
	NOP
	st.h.usr	%r3,%r2,%r0
	NOP
	NOP
	NOP
	jmp	%r1
#else
	jmp.n	%r1
	 st.h.usr	%r3,%r2,%r0
#endif
1:	jmp.n	%r1
	 st.h	%r3,%r2,%r0

ENTRY(do_store_byte)	/* do_store_byte(address, data, supervisor) */
	bcnd	ne0,%r4,1f
#ifdef ERRATA__XXX_USR
	NOP
	st.b.usr	%r3,%r2,%r0
	NOP
	NOP
	NOP
	jmp	%r1
#else
	jmp.n	%r1
	 st.b.usr	%r3,%r2,%r0
#endif
1:	jmp.n	%r1
	 st.b	%r3,%r2,%r0

ENTRY(do_xmem_word)	/* do_xmem_word(address, data, supervisor) */
	bcnd	ne0,%r4,1f
#ifdef ERRATA__XXX_USR
	NOP
#endif
	xmem.usr	%r3,%r2,%r0
#ifdef ERRATA__XXX_USR
	NOP
	NOP
	NOP
#endif
	jmp.n	%r1
	 or	%r2, %r3, %r0
1:	xmem	%r3,%r2,%r0
	jmp.n	%r1
	 or	%r2, %r3, %r0

ENTRY(do_xmem_byte)	/* do_xmem_byte(address, data, supervisor) */
	bcnd	ne0,%r4,1f
#ifdef ERRATA__XXX_USR
	NOP
#endif
	xmem.bu.usr	%r3,%r2,%r0
#ifdef ERRATA__XXX_USR
	NOP
	NOP
	NOP
#endif
	jmp.n	%r1
	 or	%r2,%r3,%r0
1:	xmem.bu	%r3,%r2,%r0
	jmp.n	%r1
	 or	%r2,%r3,%r0

#endif	/* M88100 */

/*
 * Copy specified amount of data from user space into the kernel
 * copyin(from, to, len)
 *	r2 == from (user source address)
 *	r3 == to (kernel destination address)
 *	r4 == length
 */

#define	SRC	%r2
#define	DEST	%r3
#define	LEN	%r4

ENTRY(copyin)
	/* set up fault handler */
	ldcr	%r5,  CPU
	ld	%r6,  %r5,  CI_CURPCB
	or.u	%r5,  %r0,  %hi16(_ASM_LABEL(Lciflt))
	or	%r5,  %r5,  %lo16(_ASM_LABEL(Lciflt))
	st	%r5,  %r6,  PCB_ONFAULT	/* pcb_onfault = Lciflt */

	/*
	 * If it's a small length (less than 8), then do byte-by-byte.
	 * Despite not being optimal if len is 4, and from and to
	 * are word-aligned, this is still faster than doing more tests
	 * to save an hyperthetical fraction of cycle.
	 */
	cmp	%r9,  LEN,  8
	bb1	lt,   %r9,  _ASM_LABEL(copyin_byte_only)

	/* If they're not aligned similarly, use byte only... */
	xor	%r9,  SRC,  DEST
	mask	%r8,  %r9,  0x3
	bcnd	ne0,  %r8,  _ASM_LABEL(copyin_byte_only)

	/*
	 * At this point, we don't know if they're word aligned or not,
	 * but we know that what needs to be done to one to align
	 * it is what's needed for the other.
	 */
	bb1	0,    SRC,  _ASM_LABEL(copyin_left_align_to_halfword)
ASLOCAL(copyin_left_aligned_to_halfword)
	bb1	1,    SRC,  _ASM_LABEL(copyin_left_align_to_word)
ASLOCAL(copyin_left_aligned_to_word)
	bb1	0,    LEN,  _ASM_LABEL(copyin_right_align_to_halfword)
ASLOCAL(copyin_right_aligned_to_halfword)
	bb1	1,    LEN,  _ASM_LABEL(copyin_right_align_to_word)
ASLOCAL(copyin_right_aligned_to_word)

	/*
	 * At this point, both SRC and DEST are aligned to a word
	 * boundary, and LEN is a multiple of 4. We want it an even
	 * multiple of 4.
	 */
	bb1.n	2,    LEN,  _ASM_LABEL(copyin_right_align_to_doubleword)
	 or	%r7,  %r0,  4

ASLOCAL(copyin_right_aligned_to_doubleword)
#ifdef ERRATA__XXX_USR
	NOP
	ld.usr	%r5,  SRC,  %r0
	NOP
	NOP
	NOP
	ld.usr	%r6,  SRC,  %r7
	NOP
	NOP
	NOP
#else
	ld.usr	%r5,  SRC,  %r0
	ld.usr	%r6,  SRC,  %r7
#endif
	subu	LEN,  LEN,  8
	st	%r5,  DEST, %r0
	addu	SRC,  SRC,  8
	st	%r6,  DEST, %r7
	bcnd.n	ne0,  LEN,  _ASM_LABEL(copyin_right_aligned_to_doubleword)
	 addu	DEST, DEST, 8
	br.n	_ASM_LABEL(Lcidone)
	 or	%r2, %r0, %r0	/* successful return */

ASLOCAL(copyin_left_align_to_halfword)
#ifdef ERRATA__XXX_USR
	NOP
	ld.b.usr	%r5,  SRC, %r0
	NOP
	NOP
	NOP
#else
	ld.b.usr	%r5,  SRC, %r0
#endif
	subu	LEN,  LEN,  1
	st.b	%r5,  DEST, %r0
	addu	SRC,  SRC,  1
	br.n	_ASM_LABEL(copyin_left_aligned_to_halfword)
	 addu	DEST, DEST, 1

ASLOCAL(copyin_left_align_to_word)
#ifdef ERRATA__XXX_USR
	NOP
	ld.h.usr	%r5,   SRC,  %r0
	NOP
	NOP
	NOP
#else
	ld.h.usr	%r5,   SRC,  %r0
#endif
	subu	LEN,  LEN,  2
	st.h	%r5,  DEST, %r0
	addu	SRC,  SRC,  2
	br.n	_ASM_LABEL(copyin_left_aligned_to_word)
	 addu	DEST, DEST, 2

ASLOCAL(copyin_right_align_to_halfword)
	subu	LEN,  LEN,  1
#ifdef ERRATA__XXX_USR
	NOP
	ld.b.usr	%r5,  SRC, LEN
	NOP
	NOP
	NOP
#else
	ld.b.usr	%r5,  SRC, LEN
#endif
	br.n	_ASM_LABEL(copyin_right_aligned_to_halfword)
	 st.b	%r5,  DEST, LEN

ASLOCAL(copyin_right_align_to_word)
	subu	LEN,  LEN,  2
#ifdef ERRATA__XXX_USR
	NOP
	ld.h.usr	%r5,  SRC, LEN
	NOP
	NOP
	NOP
#else
	ld.h.usr	%r5,  SRC, LEN
#endif
	br.n	_ASM_LABEL(copyin_right_aligned_to_word)
	 st.h	%r5,  DEST, LEN

ASLOCAL(copyin_right_align_to_doubleword)
	subu	LEN,  LEN,  4
#ifdef ERRATA__XXX_USR
	NOP
	ld.usr	%r5,  SRC,  LEN
	NOP
	NOP
	NOP
#else
	ld.usr	%r5,  SRC,  LEN
#endif
	bcnd.n	ne0,  LEN, _ASM_LABEL(copyin_right_aligned_to_doubleword)
	 st	%r5,  DEST, LEN
	br.n	_ASM_LABEL(Lcidone)
   	 or	%r2, %r0, %r0	/* successful return */

ASLOCAL(copyin_byte_only)
	bcnd	eq0, LEN, 2f
1:
	subu	LEN, LEN, 1
#ifdef ERRATA__XXX_USR
	NOP
	ld.b.usr	%r5, SRC, LEN
	NOP
	NOP
	NOP
#else
	ld.b.usr	%r5, SRC, LEN
#endif
	bcnd.n	ne0, LEN, 1b
	 st.b	%r5, DEST, LEN
2:
	or	%r2, %r0, %r0	/* successful return */
	/* FALLTHROUGH */

ASLOCAL(Lcidone)
	ldcr	%r5,  CPU
	ld	%r6,  %r5,  CI_CURPCB
	jmp.n	%r1
	 st	%r0,  %r6,  PCB_ONFAULT

ASLOCAL(Lciflt)
	br.n	_ASM_LABEL(Lcidone)
	 or	%r2, %r0, EFAULT	/* return fault */

#undef	SRC
#undef	DEST
#undef	LEN

/*######################################################################*/
/*######################################################################*/

/*
 * Copy a null terminated string from the user space to the kernel
 * address space.
 *
 * copyinstr(from, to, maxlen, &lencopied)
 * r2 == from
 * r3 == to
 * r4 == maxlen
 * r5 == len actually transferred (includes the terminating NUL!!!)
 * r6 & r7 - used as temporaries
 */
#define	SRC	%r2
#define	DEST	%r3
#define	CNT	%r4
#define	LEN	%r5

ENTRY(copyinstr)

	/* setup fault handler */
	ldcr	%r6,  CPU
	ld	%r7,  %r6,   CI_CURPCB
	or.u	%r6,  %r0,   %hi16(_ASM_LABEL(Lcisflt))
	or	%r6,  %r6,   %lo16(_ASM_LABEL(Lcisflt))
	st	%r6,  %r7,   PCB_ONFAULT
	or	%r6,  %r0,   0
	bcnd	lt0,  CNT,  _ASM_LABEL(Lcisflt)
	bcnd	eq0,  CNT,  _ASM_LABEL(Lcistoolong)
1:
#ifdef ERRATA__XXX_USR
	NOP
	ld.bu.usr	%r7,  SRC, %r6
	NOP
	NOP
	NOP
#else
	ld.bu.usr	%r7,  SRC,  %r6
#endif
	st.b	%r7,  DEST, %r6
	bcnd.n	eq0,  %r7,  2f		/* all done */
	 addu	%r6,  %r6,  1
	cmp	%r7,  %r6,  CNT
	bb1	lt,   %r7,  1b

ASLOCAL(Lcistoolong)
	or	%r2,   %r0, ENAMETOOLONG	/* overflow */

ASLOCAL(Lcisnull)
	bcnd	eq0,%r6, _ASM_LABEL(Lcisdone)	/* do not attempt to clear last byte */
					/* if we did not write to the string */
	subu	%r6,  %r6,  1
	st.b	%r0,  DEST, %r6		/* clear last byte */
	br.n	_ASM_LABEL(Lcisdone)
	 addu	%r6,  %r6,  1
2:					/* all done */
	or	%r2,  %r0,  0

ASLOCAL(Lcisdone)
	bcnd	eq0, LEN, 3f
	st	%r6, %r0, LEN
3:
	ldcr	%r5,  CPU
	ld	%r6,  %r5,  CI_CURPCB
	jmp.n	%r1
	 st	%r0,  %r6,  PCB_ONFAULT	/* clear the handler */

ASLOCAL(Lcisflt)
	br.n	_ASM_LABEL(Lcisnull)
	 or	%r2,  %r0,  EFAULT	/* return fault */

#undef	SRC
#undef	DEST
#undef	CNT
#undef	LEN

/*
 * Copy specified amount of data from kernel to the user space
 * Copyout(from, to, len)
 *	r2 == from (kernel source address)
 *	r3 == to (user destination address)
 *	r4 == length
 */

#define	SRC	%r2
#define	DEST	%r3
#define	LEN	%r4

ENTRY(copyout)
	/* setup fault handler */
	ldcr	%r5,  CPU
	ld	%r6,  %r5,  CI_CURPCB
	or.u	%r5,  %r0,  %hi16(_ASM_LABEL(Lcoflt))
	or	%r5,  %r5,  %lo16(_ASM_LABEL(Lcoflt))
	st	%r5,  %r6,  PCB_ONFAULT	/* pcb_onfault = Lcoflt */

	/*
	 * If it's a small length (less than 8), then do byte-by-byte.
	 * Despite not being optimal if len is 4, and from and to
	 * are word-aligned, this is still faster than doing more tests
	 * to save an hyperthetical fraction of cycle.
	 */
	cmp	%r9,  LEN,  8
	bb1	lt,   %r9,   _ASM_LABEL(copyout_byte_only)

	/* If they're not aligned similarly, use byte only... */
	xor	%r9,  SRC,  DEST
	mask	%r8,  %r9,  0x3
	bcnd	ne0,  %r8,  _ASM_LABEL(copyout_byte_only)

	/*
	 * At this point, we don't know if they're word aligned or not,
	 * but we know that what needs to be done to one to align
	 * it is what's needed for the other.
	 */
	bb1	0,    SRC,  _ASM_LABEL(copyout_left_align_to_halfword)
ASLOCAL(copyout_left_aligned_to_halfword)
	bb1	1,    SRC,  _ASM_LABEL(copyout_left_align_to_word)
ASLOCAL(copyout_left_aligned_to_word)
	bb1	0,    LEN,  _ASM_LABEL(copyout_right_align_to_halfword)
ASLOCAL(copyout_right_aligned_to_halfword)
	bb1	1,    LEN,  _ASM_LABEL(copyout_right_align_to_word)
ASLOCAL(copyout_right_aligned_to_word)

	/*
	 * At this point, both SRC and DEST are aligned to a word
	 * boundary, and LEN is a multiple of 4. We want it an even
	 * multiple of 4.
	 */
	bb1.n	2,    LEN,  _ASM_LABEL(copyout_right_align_to_doubleword)
	 or	%r7,  %r0,  4

ASLOCAL(copyout_right_aligned_to_doubleword)
	ld 	%r5,  SRC,  %r0
	ld    	%r6,  SRC,  %r7
	subu	LEN,  LEN,  8
#ifdef ERRATA__XXX_USR
	NOP
	st.usr	%r5,  DEST, %r0
	NOP
	NOP
	NOP
#else
	st.usr	%r5,  DEST, %r0
#endif
	addu	SRC,  SRC,  8
#ifdef ERRATA__XXX_USR
	NOP
	st.usr	%r6,  DEST, %r7
	NOP
	NOP
	NOP
#else
	st.usr	%r6,  DEST, %r7
#endif
	bcnd.n	ne0,  LEN,  _ASM_LABEL(copyout_right_aligned_to_doubleword)
	 addu	DEST, DEST, 8
	or	%r2,  %r0,  %r0	/* successful return */
	br	_ASM_LABEL(Lcodone)

	/***************************************************/
ASLOCAL(copyout_left_align_to_halfword)
	ld.b	%r5,  SRC,  %r0
	subu	LEN,  LEN,  1
#ifdef ERRATA__XXX_USR
	NOP
	st.b.usr	%r5,  DEST, %r0
	NOP
	NOP
	NOP
#else
	st.b.usr	%r5,  DEST, %r0
#endif
	addu	SRC,  SRC,  1
	br.n	_ASM_LABEL(copyout_left_aligned_to_halfword)
	 addu	DEST, DEST, 1

ASLOCAL(copyout_left_align_to_word)
	ld.h	%r5,  SRC,  %r0
	subu	LEN,  LEN,  2
#ifdef ERRATA__XXX_USR
	NOP
	st.h.usr	%r5,  DEST, %r0
	NOP
	NOP
	NOP
#else
	st.h.usr	%r5,  DEST, %r0
#endif
	addu	SRC,  SRC,  2
	br.n	_ASM_LABEL(copyout_left_aligned_to_word)
	 addu	DEST, DEST, 2

ASLOCAL(copyout_right_align_to_halfword)
	subu	LEN,  LEN,  1
	ld.b	%r5,  SRC,  LEN
#ifdef ERRATA__XXX_USR
	NOP
	st.b.usr	%r5,  DEST, LEN
	NOP
	NOP
	NOP
	br	_ASM_LABEL(copyout_right_aligned_to_halfword)
#else
	br.n	_ASM_LABEL(copyout_right_aligned_to_halfword)
	 st.b.usr	%r5,  DEST, LEN
#endif

ASLOCAL(copyout_right_align_to_word)
	subu	LEN,  LEN,  2
	ld.h	%r5,  SRC,  LEN
#ifdef ERRATA__XXX_USR
	NOP
	st.h.usr	%r5,  DEST, LEN
	NOP
	NOP
	NOP
	br	_ASM_LABEL(copyout_right_aligned_to_word)
#else
	br.n	_ASM_LABEL(copyout_right_aligned_to_word)
	 st.h.usr	%r5,  DEST, LEN
#endif

ASLOCAL(copyout_right_align_to_doubleword)
	subu	LEN,  LEN,  4
	ld	%r5,  SRC,  LEN
#ifdef ERRATA__XXX_USR
	NOP
	st.usr	%r5,  DEST, LEN
	NOP
	NOP
	NOP
	bcnd	ne0,  LEN, _ASM_LABEL(copyout_right_aligned_to_doubleword)
#else
	bcnd.n	ne0,  LEN, _ASM_LABEL(copyout_right_aligned_to_doubleword)
	 st.usr	%r5,  DEST, LEN
#endif
	br.n	_ASM_LABEL(Lcodone)
	 or	%r2, %r0, %r0	/* successful return */

ASLOCAL(copyout_byte_only)
	bcnd	eq0, LEN, 2f
1:
	subu	LEN, LEN, 1
	ld.b	%r5, SRC, LEN
#ifdef ERRATA__XXX_USR
	NOP
	st.b.usr	%r5, DEST, LEN
	NOP
	NOP
	NOP
	bcnd	ne0, LEN, 1b
#else
	bcnd.n	ne0, LEN, 1b
	 st.b.usr	%r5, DEST, LEN
#endif

2:
	or	%r2, %r0, %r0	/* successful return */
	/* FALLTHROUGH */

ASLOCAL(Lcodone)
	ldcr	%r5,  CPU
	ld	%r6,  %r5,  CI_CURPCB
	jmp.n	%r1
	 st	%r0,  %r6,  PCB_ONFAULT	/* clear the handler */

ASLOCAL(Lcoflt)
	br.n	_ASM_LABEL(Lcodone)
	 or	%r2, %r0, EFAULT	/* return fault */

#undef	SRC
#undef	DEST
#undef	LEN

/*
 * Copy a null terminated string from the kernel space to the user
 * address space.
 *
 * copyoutstr(from, to, maxlen, &lencopied)
 * r2 == from
 * r3 == to
 * r4 == maxlen that can be copied
 * r5 == len actually copied (including the terminating NUL!!!)
 */

#define	SRC	%r2
#define	DEST	%r3
#define	CNT	%r4
#define	LEN	%r5

ENTRY(copyoutstr)
	/* setup fault handler */
	ldcr	%r6,  CPU
	ld	%r7,  %r6,  CI_CURPCB
	or.u	%r6,  %r0,  %hi16(_ASM_LABEL(Lcosflt))
	or	%r6,  %r6,  %lo16(_ASM_LABEL(Lcosflt))
	st	%r6,  %r7,  PCB_ONFAULT
	bcnd	lt0,  CNT,  _ASM_LABEL(Lcosflt)
	bcnd	eq0,  CNT,  _ASM_LABEL(Lcosdone)
	or	%r6,  %r0,  0
1:
	ld.bu	%r7,  SRC,  %r6
#ifdef ERRATA__XXX_USR
	NOP
	st.b.usr	%r7,  DEST,  %r6
	NOP
	NOP
	NOP
#else
	st.b.usr	%r7,  DEST,  %r6
#endif
	bcnd.n	eq0,  %r7, 2f		/* all done */
	 addu	%r6,  %r6, 1
	cmp	%r7,  %r6, CNT
	bb1	lt,   %r7, 1b
	br.n	_ASM_LABEL(Lcosdone)
	 or	%r2,  %r0, ENAMETOOLONG
2:
	br.n	_ASM_LABEL(Lcosdone)
	 or	%r2,  %r0, 0

ASLOCAL(Lcosflt)
	br.n	_ASM_LABEL(Lcosdone)
	 or	%r2, %r0, EFAULT

ASLOCAL(Lcosdone)
	bcnd	eq0, LEN, 3f
	st	%r6, %r0, LEN
3:
	ldcr	%r5, CPU
	ld	%r6, %r5, CI_CURPCB
	jmp.n	%r1
	 st	%r0, %r6, PCB_ONFAULT	/* clear the handler */

#undef	SRC
#undef	DEST
#undef	CNT
#undef	LEN

/*######################################################################*/

/*
 * kcopy(const void *src, void *dst, size_t len);
 *
 * Copy len bytes from src to dst, aborting if we encounter a page fault.
 */
ENTRY(kcopy)
	subu	%r31, %r31, 16
	ldcr	%r5,  CPU
	ld	%r6,  %r5,  CI_CURPCB
	or.u	%r5,  %r0,  %hi16(_ASM_LABEL(kcopy_fault))
	ld	%r7,  %r6,  PCB_ONFAULT
	or	%r5,  %r5,  %lo16(_ASM_LABEL(kcopy_fault))
	st	%r7,  %r31, 0			/* save old pcb_onfault */
	st	%r5,  %r6,  PCB_ONFAULT		/* pcb_onfault = kcopy_fault */
	bcnd	le0,  %r4,  _ASM_LABEL(kcopy_out) /* nothing to do if <= 0 */
/*
 *	check position of source and destination data
 */
	cmp 	%r9,  %r2,  %r3	/* compare source address to destination */
	bb1	eq,   %r9,  _ASM_LABEL(kcopy_out) /* nothing to do if equal */
	bb1	lo,   %r9,  _ASM_LABEL(kcopy_reverse) /* reverse copy if src < dest */
/*
 *	source address is greater than destination address, copy forward
 */
	cmp 	%r9,  %r4,  16	/* see if we have at least 16 bytes */
	bb1	lt,   %r9,  _ASM_LABEL(kf_byte_copy)	/* copy bytes for small length */
/*
 *	determine copy strategy based on alignment of source and destination
 */
	mask	%r6,  %r2,  3	/* get 2 low order bits of source address */
	mask	%r7,  %r3,  3	/* get 2 low order bits of destination addr */
	mak	%r6,  %r6,  0<4>/* convert source bits to table offset */
	mak	%r7,  %r7,  0<2>/* convert destination bits to table offset */
	or.u	%r12, %r0,  %hi16(_ASM_LABEL(kf_strat))
	or	%r12, %r12, %lo16(_ASM_LABEL(kf_strat))
	addu	%r6,  %r6,  %r7	/* compute final table offset for strategy */
	ld	%r12, %r12, %r6	/* load the strategy routine */
	jmp	%r12		/* branch to strategy routine */

/*
 * Copy three bytes from src to destination then copy words
 */
ASLOCAL(kf_3byte_word_copy)
	ld.bu	%r6,  %r2,  0		/* load byte from source */
	ld.bu	%r7,  %r2,  1		/* load byte from source */
	ld.bu	%r8,  %r2,  2		/* load byte from source */
	st.b	%r6,  %r3,  0		/* store byte to destination */
	st.b	%r7,  %r3,  1		/* store byte to destination */
	st.b	%r8,  %r3,  2		/* store byte to destination */
	addu	%r2,  %r2,  3		/* increment source pointer */
	addu	%r3,  %r3,  3		/* increment destination pointer */
	br.n	_ASM_LABEL(kf_word_copy)/* copy full words */
	 subu	%r4,  %r4,  3		/* decrement length */

/*
 * Copy 1 halfword from src to destination then copy words
 */
ASLOCAL(kf_1half_word_copy)
	ld.hu	%r6,  %r2,  0		/* load half-word from source */
	st.h	%r6,  %r3,  0		/* store half-word to destination */
	addu	%r2,  %r2,  2		/* increment source pointer */
	addu	%r3,  %r3,  2		/* increment destination pointer */
	br.n	_ASM_LABEL(kf_word_copy)/* copy full words */
	 subu	%r4,  %r4,  2		/* decrement remaining length */

/*
 * Copy 1 byte from src to destination then copy words
 */
ASLOCAL(kf_1byte_word_copy)
	ld.bu	%r6,  %r2,  0		/* load 1 byte from source */
	st.b	%r6,  %r3,  0		/* store 1 byte to destination */
	addu	%r2,  %r2,  1		/* increment source pointer */
	addu	%r3,  %r3,  1		/* increment destination pointer */
	subu	%r4,  %r4,  1		/* decrement remaining length */
	/* fall through to word copy */
/*
 * Copy as many full words as possible, 4 words per loop
 */
ASLOCAL(kf_word_copy)
	cmp	%r10, %r4,  16		/* see if we have 16 bytes remaining */
	bb1	lo,   %r10, _ASM_LABEL(kf_byte_copy) 	/* not enough left, copy bytes */
	ld	%r6,  %r2,  0		/* load first word */
	ld	%r7,  %r2,  4		/* load second word */
	ld	%r8,  %r2,  8		/* load third word */
	ld	%r9,  %r2,  12		/* load fourth word */
	st	%r6,  %r3,  0		/* store first word */
	st	%r7,  %r3,  4		/* store second word */
	st 	%r8,  %r3,  8		/* store third word */
	st 	%r9,  %r3,  12		/* store fourth word */
	addu	%r2,  %r2,  16		/* increment source pointer */
	addu	%r3,  %r3,  16		/* increment destination pointer */
	br.n	_ASM_LABEL(kf_word_copy)/* copy another block */
	 subu	%r4,  %r4,  16		/* decrement remaining length */

ASLOCAL(kf_1byte_half_copy)
	ld.bu	%r6,  %r2,  0		/* load 1 byte from source */
	st.b	%r6,  %r3,  0		/* store 1 byte to destination */
	addu	%r2,  %r2,  1		/* increment source pointer */
	addu	%r3,  %r3,  1		/* increment destination pointer */
	subu	%r4,  %r4,  1		/* decrement remaining length */
	/* fall through to half copy */

ASLOCAL(kf_half_copy)
	cmp	%r10, %r4,  16		/* see if we have 16 bytes remaining */
	bb1	lo,   %r10, _ASM_LABEL(kf_byte_copy)	/* not enough left, copy bytes */
	ld.hu	%r6,  %r2,  0		/* load first half-word */
	ld.hu	%r7,  %r2,  2		/* load second half-word */
	ld.hu	%r8,  %r2,  4		/* load third half-word */
	ld.hu	%r9,  %r2,  6		/* load fourth half-word */
	ld.hu	%r10, %r2,  8		/* load fifth half-word */
	ld.hu	%r11, %r2,  10		/* load sixth half-word */
	ld.hu	%r12, %r2,  12		/* load seventh half-word */
	ld.hu	%r13, %r2,  14		/* load eighth half-word */
	st.h	%r6,  %r3,  0		/* store first half-word */
	st.h	%r7,  %r3,  2		/* store second half-word */
	st.h 	%r8,  %r3,  4		/* store third half-word */
	st.h 	%r9,  %r3,  6		/* store fourth half-word */
	st.h	%r10, %r3,  8		/* store fifth half-word */
	st.h	%r11, %r3,  10		/* store sixth half-word */
	st.h 	%r12, %r3,  12		/* store seventh half-word */
	st.h 	%r13, %r3,  14		/* store eighth half-word */
	addu	%r2,  %r2,  16		/* increment source pointer */
	addu	%r3,  %r3,  16		/* increment destination pointer */
	br.n	_ASM_LABEL(kf_half_copy)/* copy another block */
	 subu	%r4,  %r4,  16		/* decrement remaining length */

ASLOCAL(kf_byte_copy)
	bcnd	eq0,  %r4,  _ASM_LABEL(kcopy_out)	/* branch if nothing left to copy */
	ld.bu	%r6,  %r2,  0		/* load byte from source */
	st.b	%r6,  %r3,  0		/* store byte in destination */
	addu	%r2,  %r2,  1		/* increment source pointer */
	addu	%r3,  %r3,  1		/* increment destination pointer */
	br.n	_ASM_LABEL(kf_byte_copy)/* branch for next byte */
	 subu	%r4,  %r4,  1		/* decrement remaining length */

/*
 *	source address is less than destination address, copy in reverse
 */
ASLOCAL(kcopy_reverse)
/*
 * start copy pointers at end of data
 */
	addu	%r2,  %r2,  %r4		/* start source at end of data */
	addu	%r3,  %r3,  %r4		/* start destination at end of data */
/*
 * check for short data
 */
	cmp 	%r9,  %r4,  16		/* see if we have at least 16 bytes */
	bb1	lt,   %r9,  _ASM_LABEL(kr_byte_copy)	/* copy bytes for small data length */
/*
 *	determine copy strategy based on alignment of source and destination
 */
	mask	%r6,  %r2,  3	/* get 2 low order bits of source address */
	mask	%r7,  %r3,  3	/* get 2 low order bits of destination addr */
	mak	%r6,  %r6,  0<4>/* convert source bits to table offset */
	mak	%r7,  %r7,  0<2>/* convert destination bits to table offset */
	or.u	%r12, %r0,  %hi16(_ASM_LABEL(kr_strat))
	or	%r12, %r12, %lo16(_ASM_LABEL(kr_strat))
	addu	%r6,  %r6,  %r7	/* compute final table offset for strategy */
	ld	%r12, %r12, %r6	/* load the strategy routine */
	jmp	%r12		/* branch to strategy routine */

/*
 * Copy three bytes from src to destination then copy words
 */
ASLOCAL(kr_3byte_word_copy)
	subu	%r2,  %r2,  3		/* decrement source pointer */
	subu	%r3,  %r3,  3		/* decrement destination pointer */
	ld.bu	%r6,  %r2,  0		/* load byte from source */
	ld.bu	%r7,  %r2,  1		/* load byte from source */
	ld.bu	%r8,  %r2,  2		/* load byte from source */
	st.b	%r6,  %r3,  0		/* store byte to destination */
	st.b	%r7,  %r3,  1		/* store byte to destination */
	st.b	%r8,  %r3,  2		/* store byte to destination */
	br.n	_ASM_LABEL(kr_word_copy)/* copy full words */
	 subu	%r4,  %r4,  3		/* decrement length */

/*
 * Copy 1 halfword from src to destination then copy words
 */
ASLOCAL(kr_1half_word_copy)
	subu	%r2,  %r2,  2		/* decrement source pointer */
	subu	%r3,  %r3,  2		/* decrement destination pointer */
	ld.hu	%r6,  %r2,  0		/* load half-word from source */
	st.h	%r6,  %r3,  0		/* store half-word to destination */
	br.n	_ASM_LABEL(kr_word_copy)/* copy full words */
	 subu	%r4,  %r4,  2		/* decrement remaining length */

/*
 * Copy 1 byte from src to destination then copy words
 */
ASLOCAL(kr_1byte_word_copy)
	subu	%r2,  %r2,  1		/* decrement source pointer */
	subu	%r3,  %r3,  1		/* decrement destination pointer */
	ld.bu	%r6,  %r2,  0		/* load 1 byte from source */
	st.b	%r6,  %r3,  0		/* store 1 byte to destination */
	subu	%r4,  %r4,  1		/* decrement remaining length */
	/* fall through to word copy */
/*
 * Copy as many full words as possible, 4 words per loop
 */
ASLOCAL(kr_word_copy)
	cmp	%r10, %r4,  16		/* see if we have 16 bytes remaining */
	bb1	lo,   %r10, _ASM_LABEL(kr_byte_copy)	/* not enough left, copy bytes */
	subu	%r2,  %r2,  16		/* decrement source pointer */
	subu	%r3,  %r3,  16		/* decrement destination pointer */
	ld	%r6,  %r2,  0		/* load first word */
	ld	%r7,  %r2,  4		/* load second word */
	ld	%r8,  %r2,  8		/* load third word */
	ld	%r9,  %r2,  12		/* load fourth word */
	st	%r6,  %r3,  0		/* store first word */
	st	%r7,  %r3,  4		/* store second word */
	st 	%r8,  %r3,  8		/* store third word */
	st 	%r9,  %r3,  12		/* store fourth word */
	br.n	_ASM_LABEL(kr_word_copy)/* copy another block */
	 subu	%r4,  %r4,  16	/* decrement remaining length */

ASLOCAL(kr_1byte_half_copy)
	subu	%r2,  %r2,  1		/* decrement source pointer */
	subu	%r3,  %r3,  1		/* decrement destination pointer */
	ld.bu	%r6,  %r2,  0		/* load 1 byte from source */
	st.b	%r6,  %r3,  0		/* store 1 byte to destination */
	subu	%r4,  %r4,  1		/* decrement remaining length */
	/* fall through to half copy */

ASLOCAL(kr_half_copy)
	cmp	%r10, %r4,  16		/* see if we have 16 bytes remaining */
	bb1	lo,   %r10, _ASM_LABEL(kr_byte_copy)	/* not enough left, copy bytes */
	subu	%r2,  %r2,  16		/* decrement source pointer */
	subu	%r3,  %r3,  16		/* decrement destination pointer */
	ld.hu	%r6,  %r2,  0		/* load first half-word */
	ld.hu	%r7,  %r2,  2		/* load second half-word */
	ld.hu	%r8,  %r2,  4		/* load third half-word */
	ld.hu	%r9,  %r2,  6		/* load fourth half-word */
	ld.hu	%r10, %r2,  8		/* load fifth half-word */
	ld.hu	%r11, %r2,  10		/* load sixth half-word */
	ld.hu	%r12, %r2,  12		/* load seventh half-word */
	ld.hu	%r13, %r2,  14		/* load eighth half-word */
	st.h	%r6,  %r3,  0		/* store first half-word */
	st.h	%r7,  %r3,  2		/* store second half-word */
	st.h 	%r8,  %r3,  4		/* store third half-word */
	st.h 	%r9,  %r3,  6		/* store fourth half-word */
	st.h	%r10, %r3,  8		/* store fifth half-word */
	st.h	%r11, %r3,  10		/* store sixth half-word */
	st.h 	%r12, %r3,  12		/* store seventh half-word */
	st.h 	%r13, %r3,  14		/* store eighth half-word */
	br.n	_ASM_LABEL(kr_half_copy)/* copy another block */
	 subu	%r4,  %r4,  16		/* decrement remaining length */

ASLOCAL(kr_byte_copy)
	bcnd	eq0,  %r4,  _ASM_LABEL(kcopy_out)	/* branch if nothing left to copy */
	subu	%r2,  %r2,  1		/* decrement source pointer */
	subu	%r3,  %r3,  1		/* decrement destination pointer */
	ld.bu	%r6,  %r2,  0		/* load byte from source */
	st.b	%r6,  %r3,  0		/* store byte in destination */
	br.n	_ASM_LABEL(kr_byte_copy)/* branch for next byte */
	 subu	%r4,  %r4,  1		/* decrement remaining length */

ASLOCAL(kcopy_out)
	or	%r2,   %r0,  0		/* return success */
ASLOCAL(kcopy_out_fault)
	ldcr	%r5,  CPU
	ld	%r7,  %r31, 0
	ld	%r6,  %r5,  CI_CURPCB
	add	%r31, %r31, 16
	jmp.n	%r1			/* all done, return to caller */
	 st	%r7,  %r6,  PCB_ONFAULT	/* restore previous pcb_onfault */

ASLOCAL(kcopy_fault)
	br.n	_ASM_LABEL(kcopy_out_fault)
	 or	%r2,  %r0,  EFAULT	/* return fault */

	.data
	.align	2
ASLOCAL(kf_strat)
	.word	_ASM_LABEL(kf_word_copy)
	.word	_ASM_LABEL(kf_byte_copy)
	.word	_ASM_LABEL(kf_half_copy)
	.word	_ASM_LABEL(kf_byte_copy)
	.word	_ASM_LABEL(kf_byte_copy)
	.word	_ASM_LABEL(kf_3byte_word_copy)
	.word	_ASM_LABEL(kf_byte_copy)
	.word	_ASM_LABEL(kf_1byte_half_copy)
	.word	_ASM_LABEL(kf_half_copy)
	.word	_ASM_LABEL(kf_byte_copy)
	.word	_ASM_LABEL(kf_1half_word_copy)
	.word	_ASM_LABEL(kf_byte_copy)
	.word	_ASM_LABEL(kf_byte_copy)
	.word	_ASM_LABEL(kf_1byte_half_copy)
	.word	_ASM_LABEL(kf_byte_copy)
	.word	_ASM_LABEL(kf_1byte_word_copy)

ASLOCAL(kr_strat)
	.word	_ASM_LABEL(kr_word_copy)
	.word	_ASM_LABEL(kr_byte_copy)
	.word	_ASM_LABEL(kr_half_copy)
	.word	_ASM_LABEL(kr_byte_copy)
	.word	_ASM_LABEL(kr_byte_copy)
	.word	_ASM_LABEL(kr_1byte_word_copy)
	.word	_ASM_LABEL(kr_byte_copy)
	.word	_ASM_LABEL(kr_1byte_half_copy)
	.word	_ASM_LABEL(kr_half_copy)
	.word	_ASM_LABEL(kr_byte_copy)
	.word	_ASM_LABEL(kr_1half_word_copy)
	.word	_ASM_LABEL(kr_byte_copy)
	.word	_ASM_LABEL(kr_byte_copy)
	.word	_ASM_LABEL(kr_1byte_half_copy)
	.word	_ASM_LABEL(kr_byte_copy)
	.word	_ASM_LABEL(kr_3byte_word_copy)

#ifdef DDB
/*
 * non-local goto
 *	int setjmp(label_t *);
 *	void longjmp(label_t*);
 */
ENTRY(setjmp)
	st	%r1,  %r2, 0
	st	%r14, %r2, 4
	st	%r15, %r2, 2*4
	st	%r16, %r2, 3*4
	st	%r17, %r2, 4*4
	st	%r18, %r2, 5*4
	st	%r19, %r2, 6*4
	st	%r20, %r2, 7*4
	st	%r21, %r2, 8*4
	st	%r22, %r2, 9*4
	st	%r23, %r2, 10*4
	st	%r24, %r2, 11*4
	st	%r25, %r2, 12*4
	st	%r26, %r2, 13*4
	st	%r27, %r2, 14*4
	st	%r28, %r2, 15*4
	st	%r29, %r2, 16*4
	st	%r30, %r2, 17*4
	st	%r31, %r2, 18*4
	jmp.n	%r1
	 or	%r2,  %r0, %r0

ENTRY(longjmp)
	ld	%r1,  %r2, 0
	ld	%r14, %r2, 4
	ld	%r15, %r2, 2*4
	ld	%r16, %r2, 3*4
	ld	%r17, %r2, 4*4
	ld	%r18, %r2, 5*4
	ld	%r19, %r2, 6*4
	ld	%r20, %r2, 7*4
	ld	%r21, %r2, 8*4
	ld	%r22, %r2, 9*4
	ld	%r23, %r2, 10*4
	ld	%r24, %r2, 11*4
	ld	%r25, %r2, 12*4
	ld	%r26, %r2, 13*4
	ld	%r27, %r2, 14*4
	ld	%r28, %r2, 15*4
	ld	%r29, %r2, 16*4
	ld	%r30, %r2, 17*4
	ld	%r31, %r2, 18*4
	jmp.n	%r1
	 or	%r2,  %r0, 1
#endif

/*
 * Signal trampoline code.
 * The kernel arranges for the handler to be invoked directly, and return
 * here.
 */
ENTRY(sigcode)			/* r31 points to sigframe */
	ld	%r2,  %r31, 0	/* pick sigcontext* */
	or	%r13, %r0,  SYS_sigreturn
	tb0	0,    %r0,  450	/* syscall trap, calling sigreturn */
	NOP			| failure return
#ifdef dontbother		/* sigreturn will not return unless it fails */
	NOP			| success return
#endif
	or	%r13, %r0,  SYS_exit
	tb0	0,    %r0,  450	/* syscall trap, exit */
	/*
	 * this never returns, but we need to provide fetchable instructions
	 * for the 88100 pipeline.
	 */
	NOP
	NOP
GLOBAL(esigcode)

/*
 * Helper functions for pmap_copy_page() and pmap_zero_page().
 */

#ifdef M88100

/*
 * void copypage(vaddr_t src, vaddr_t dst);
 *
 * This copies PAGE_SIZE bytes from src to dst in 32 byte chunks.
 */
ENTRY(m8820x_copypage)
	addu	%r12, %r2, PAGE_SIZE
1:
	ld.d	%r4,  %r2, 0x00
	ld.d	%r6,  %r2, 0x08
	st.d	%r4,  %r3, 0x00
	ld.d	%r8,  %r2, 0x10
	st.d	%r6,  %r3, 0x08
	ld.d	%r10, %r2, 0x18
	st.d	%r8,  %r3, 0x10
	addu	%r2,  %r2, 0x20
	st.d	%r10, %r3, 0x18
	cmp	%r4,  %r2, %r12
	bb1.n	ne,   %r4, 1b
	 addu	%r3,  %r3, 0x20
	jmp	%r1

/*
 * void zeropage(vaddr_t dst);
 *
 * This zeroes PAGE_SIZE bytes from src to dst in 64 byte chunks.
 */
ENTRY(m8820x_zeropage)
	addu	%r12, %r2, PAGE_SIZE
	or	%r3,  %r1, %r0
	or	%r1,  %r0, %r0
1:
	st.d	%r0,  %r2, 0x00
	st.d	%r0,  %r2, 0x08
	st.d	%r0,  %r2, 0x10
	st.d	%r0,  %r2, 0x18
	st.d	%r0,  %r2, 0x20
	st.d	%r0,  %r2, 0x28
	st.d	%r0,  %r2, 0x30
	st.d	%r0,  %r2, 0x38
	addu	%r2,  %r2, 0x40
	cmp	%r4,  %r2, %r12
	bb1	ne,   %r4, 1b
	jmp	%r3

#endif	/* M88100 */

#ifdef M88110

/*
 * void copypage(vaddr_t src, vaddr_t dst);
 *
 * This copies PAGE_SIZE bytes from src to dst in 32 byte chunks (one
 * cache line).
 */
ENTRY(m88110_copypage)
	addu	%r12, %r2, PAGE_SIZE
1:
	ld.h	%r0,  %r2, 0x00	| load allocate
	ld.d	%r4,  %r2, 0x00
	ld.d	%r6,  %r2, 0x08
	st.d	%r4,  %r3, 0x00
	ld.d	%r8,  %r2, 0x10
	st.d	%r6,  %r3, 0x08
	ld.d	%r10, %r2, 0x18
	st.d	%r8,  %r3, 0x10
	addu	%r2,  %r2, 0x20
	st.d	%r10, %r3, 0x18
	cmp	%r4,  %r2, %r12
	addu	%r3,  %r3, 0x20
	bb1	ne,   %r4, 1b
	jmp	%r1

/*
 * void zeropage(vaddr_t dst);
 *
 * This zeroes PAGE_SIZE bytes from src to dst in 32 byte chunks.
 */
ENTRY(m88110_zeropage)
	addu	%r12, %r2, PAGE_SIZE
	or	%r3,  %r1, %r0
	or	%r1,  %r0, %r0
1:
	ld.h	%r0,  %r2, 0x00	| load allocate
	st.d	%r0,  %r2, 0x00
	st.d	%r0,  %r2, 0x08
	st.d	%r0,  %r2, 0x10
	st.d	%r0,  %r2, 0x18
	addu	%r2,  %r2, 0x20
	cmp	%r4,  %r2, %r12
	bb1	ne,   %r4, 1b
	jmp	%r3

#endif	/* M88110 */

/*
 * PSR initialization code, invoked from locore on every processor startup.
 */
ASENTRY(setup_psr)
	ldcr	%r2,  PID
	extu	%r3,  %r2, 8<8>

	/*
	 * Ensure that the PSR is as we like:
	 *	supervisor mode
	 *	big-endian byte ordering
	 *	concurrent operation allowed
	 *	carry bit clear (I don't think we really care about this)
	 *	FPU enabled
	 *	misaligned access raises an exception
	 *	interrupts disabled
	 *	shadow registers frozen
	 *
	 * The manual says not to disable interrupts and freeze shadowing
	 * at the same time because interrupts are not actually disabled
	 * until after the next instruction. Well, if an interrupt
	 * occurs now, we're in deep trouble anyway, so I'm going to do
	 * the two together.
	 *
	 * Upon a reset (or poweron, I guess), the PSR indicates:
	 *   supervisor mode
	 *   interrupts, shadowing, FPU, misaligned exception: all disabled
	 *
	 * We'll just construct our own turning on what we want.
	 *
	 *	jfriedl@omron.co.jp
	 */

	cmp	%r4, %r3, CPU_88110
	bb1	eq,  %r4, 1f	/* if it's a mc88110, skip SSBR */
	stcr	%r0, SSBR	/* clear this for later */
1:
	stcr	%r0, SR1	/* clear the CPU flags */

	or.u	%r2, %r0, %hi16(KERNEL_PSR)
	or	%r2, %r2, %lo16(KERNEL_PSR)
	stcr	%r2, PSR
	FLUSH_PIPELINE

	jmp	%r1

/*
 * Update the VBR value.
 * This needs to be done with interrupts and shadowing disabled.
 */
GLOBAL(set_vbr)
	ldcr	%r3, PSR
	set	%r4, %r3, 1<PSR_INTERRUPT_DISABLE_BIT>
	set	%r4, %r4, 1<PSR_SHADOW_FREEZE_BIT>
	stcr	%r4, PSR
	FLUSH_PIPELINE

	stcr	%r2, VBR
	FLUSH_PIPELINE

#if defined(M88100) && defined(M88110)
	ldcr	%r2, PID
	extu	%r5, %r2, 8<8>
	cmp	%r4, %r5, CPU_88110
	bb1	eq,  %r4, 1f
#endif
#ifdef M88100
	/* 88100 */
	stcr	%r3, PSR
	FLUSH_PIPELINE
	jmp	%r1
#endif
#ifdef M88110
1:
	/* 88110 */
	stcr	%r1, EXIP
	stcr	%r3, EPSR
	RTE
#endif


code:
/*	$OpenBSD: trap.c,v 1.102 2015/02/10 10:15:30 miod Exp $	*/
/*
 * Copyright (c) 2004, Miodrag Vallat.
 * Copyright (c) 1998 Steve Murphree, Jr.
 * Copyright (c) 1996 Nivas Madhur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Nivas Madhur.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/*
 * Mach Operating System
 * Copyright (c) 1991 Carnegie Mellon University
 * Copyright (c) 1991 OMRON Corporation
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/user.h>
#include <sys/syscall.h>
#include <sys/syscall_mi.h>
#include <sys/systm.h>

#include <uvm/uvm_extern.h>

#include <machine/asm_macro.h>
#include <machine/cmmu.h>
#include <machine/cpu.h>
#ifdef M88100
#include <machine/m88100.h>
#include <machine/m8820x.h>
#endif
#ifdef M88110
#include <machine/m88110.h>
#endif
#include <machine/fpu.h>
#include <machine/pcb.h>
#include <machine/psl.h>
#include <machine/trap.h>

#include <machine/db_machdep.h>

#define SSBREAKPOINT (0xF000D1F8U) /* Single Step Breakpoint */

#define USERMODE(PSR)   (((PSR) & PSR_MODE) == 0)
#define SYSTEMMODE(PSR) (((PSR) & PSR_MODE) != 0)

void printtrap(int, struct trapframe *);
__dead void panictrap(int, struct trapframe *);
__dead void error_fatal(struct trapframe *);
int double_reg_fixup(struct trapframe *, int);
int ss_put_value(struct proc *, vaddr_t, u_int);

extern void regdump(struct trapframe *f);

const char *trap_type[] = {
	"Reset",
	"Interrupt Exception",
	"Instruction Access",
	"Data Access Exception",
	"Misaligned Access",
	"Unimplemented Opcode",
	"Privilege Violation"
	"Bounds Check Violation",
	"Illegal Integer Divide",
	"Integer Overflow",
	"Error Exception",
	"Non-Maskable Exception",
};

const int trap_types = sizeof trap_type / sizeof trap_type[0];

#ifdef M88100
const char *pbus_exception_type[] = {
	"Success (No Fault)",
	"unknown 1",
	"unknown 2",
	"Bus Error",
	"Segment Fault",
	"Page Fault",
	"Supervisor Violation",
	"Write Violation",
};
#endif

void
printtrap(int type, struct trapframe *frame)
{
#ifdef M88100
	if (CPU_IS88100) {
		if (type == 2) {
			/* instruction exception */
			printf("\nInstr access fault (%s) v = %lx, frame %p\n",
			    pbus_exception_type[
			      CMMU_PFSR_FAULT(frame->tf_ipfsr)],
			    frame->tf_sxip & XIP_ADDR, frame);
		} else if (type == 3) {
			/* data access exception */
			printf("\nData access fault (%s) v = %lx, frame %p\n",
			    pbus_exception_type[
			      CMMU_PFSR_FAULT(frame->tf_dpfsr)],
			    frame->tf_sxip & XIP_ADDR, frame);
		} else
			printf("\nTrap type %d, v = %lx, frame %p\n",
			    type, frame->tf_sxip & XIP_ADDR, frame);
	}
#endif
#ifdef M88110
	if (CPU_IS88110) {
		printf("\nTrap type %d, v = %lx, frame %p\n",
		    type, frame->tf_exip, frame);
	}
#endif
#ifdef DDB
	regdump(frame);
#endif
}

__dead void
panictrap(int type, struct trapframe *frame)
{
	static int panicing = 0;

	if (panicing++ == 0)
		printtrap(type, frame);
	if ((u_int)type < trap_types)
		panic(trap_type[type]);
	else
		panic("trap %d", type);
	/*NOTREACHED*/
}

/*
 * Handle external interrupts.
 */
void
interrupt(struct trapframe *frame)
{
	struct cpu_info *ci = curcpu();

	ci->ci_intrdepth++;
	md_interrupt_func(frame);
	ci->ci_intrdepth--;
}

#ifdef M88110
/*
 * Handle non-maskable interrupts.
 */
int
nmi(struct trapframe *frame)
{
	return md_nmi_func(frame);
}

/*
 * Reenable non-maskable interrupts.
 */
void
nmi_wrapup(struct trapframe *frame)
{
	md_nmi_wrapup_func(frame);
}
#endif

/*
 * Handle asynchronous software traps.
 */
void
ast(struct trapframe *frame)
{
	struct cpu_info *ci = curcpu();
	struct proc *p = ci->ci_curproc;

	p->p_md.md_astpending = 0;

	uvmexp.softs++;
	mi_ast(p, ci->ci_want_resched);
	userret(p);
}

#ifdef M88100
void
m88100_trap(u_int type, struct trapframe *frame)
{
	struct proc *p;
	struct vm_map *map;
	vaddr_t va, pcb_onfault;
	vm_prot_t ftype;
	int fault_type, pbus_type;
	u_long fault_code;
	vaddr_t fault_addr;
	struct vmspace *vm;
	union sigval sv;
	int result;
#ifdef DDB
	int s;
	u_int psr;
#endif
	int sig = 0;

	uvmexp.traps++;
	if ((p = curproc) == NULL)
		p = &proc0;

	if (USERMODE(frame->tf_epsr)) {
		type += T_USER;
		p->p_md.md_tf = frame;	/* for ptrace/signals */
		refreshcreds(p);
	}
	fault_type = SI_NOINFO;
	fault_code = 0;
	fault_addr = frame->tf_sxip & XIP_ADDR;

	switch (type) {
	default:
	case T_ILLFLT:
lose:
		panictrap(frame->tf_vector, frame);
		break;
		/*NOTREACHED*/

#if defined(DDB)
	case T_KDB_BREAK:
		s = splhigh();
		set_psr((psr = get_psr()) & ~PSR_IND);
		ddb_break_trap(T_KDB_BREAK, (db_regs_t*)frame);
		set_psr(psr);
		splx(s);
		return;
	case T_KDB_ENTRY:
		s = splhigh();
		set_psr((psr = get_psr()) & ~PSR_IND);
		ddb_entry_trap(T_KDB_ENTRY, (db_regs_t*)frame);
		set_psr(psr);
		splx(s);
		return;
#endif /* DDB */
	case T_MISALGNFLT:
		printf("kernel misaligned access exception @0x%08lx\n",
		    frame->tf_sxip);
		goto lose;
	case T_INSTFLT:
		/* kernel mode instruction access fault.
		 * Should never, never happen for a non-paged kernel.
		 */
#ifdef TRAPDEBUG
		pbus_type = CMMU_PFSR_FAULT(frame->tf_ipfsr);
		printf("Kernel Instruction fault #%d (%s) v = 0x%x, frame 0x%x cpu %p\n",
		    pbus_type, pbus_exception_type[pbus_type],
		    fault_addr, frame, frame->tf_cpu);
#endif
		goto lose;
	case T_DATAFLT:
		/* kernel mode data fault */

		/* data fault on the user address? */
		if ((frame->tf_dmt0 & DMT_DAS) == 0) {
			KERNEL_LOCK();
			goto user_fault;
		}

		fault_addr = frame->tf_dma0;
		if (frame->tf_dmt0 & (DMT_WRITE|DMT_LOCKBAR)) {
			ftype = PROT_READ | PROT_WRITE;
			fault_code = PROT_WRITE;
		} else {
			ftype = PROT_READ;
			fault_code = PROT_READ;
		}

		va = trunc_page((vaddr_t)fault_addr);

		KERNEL_LOCK();
		vm = p->p_vmspace;
		map = kernel_map;

		pbus_type = CMMU_PFSR_FAULT(frame->tf_dpfsr);
#ifdef TRAPDEBUG
		printf("Kernel Data access fault #%d (%s) v = 0x%x, frame 0x%x cpu %p\n",
		    pbus_type, pbus_exception_type[pbus_type],
		    fault_addr, frame, frame->tf_cpu);
#endif

		pcb_onfault = p->p_addr->u_pcb.pcb_onfault;
		switch (pbus_type) {
		case CMMU_PFSR_SUCCESS:
			/*
			 * The fault was resolved. Call data_access_emulation
			 * to drain the data unit pipe line and reset dmt0
			 * so that trap won't get called again.
			 */
			p->p_addr->u_pcb.pcb_onfault = 0;
			data_access_emulation((u_int *)frame);
			p->p_addr->u_pcb.pcb_onfault = pcb_onfault;
			frame->tf_dmt0 = 0;
			frame->tf_dpfsr = 0;
			KERNEL_UNLOCK();
			return;
		case CMMU_PFSR_SFAULT:
		case CMMU_PFSR_PFAULT:
			p->p_addr->u_pcb.pcb_onfault = 0;
			result = uvm_fault(map, va, VM_FAULT_INVALID, ftype);
			p->p_addr->u_pcb.pcb_onfault = pcb_onfault;
			if (result == 0) {
				/*
				 * We could resolve the fault. Call
				 * data_access_emulation to drain the data
				 * unit pipe line and reset dmt0 so that trap
				 * won't get called again.
				 */
				p->p_addr->u_pcb.pcb_onfault = 0;
				data_access_emulation((u_int *)frame);
				p->p_addr->u_pcb.pcb_onfault = pcb_onfault;
				frame->tf_dmt0 = 0;
				frame->tf_dpfsr = 0;
				KERNEL_UNLOCK();
				return;
			} else if (pcb_onfault != 0) {
				/*
				 * This could be a fault caused in copyout*()
				 * while accessing kernel space.
				 */
				frame->tf_snip = pcb_onfault | NIP_V;
				frame->tf_sfip = (pcb_onfault + 4) | FIP_V;
				/*
				 * Continue as if the fault had been resolved,
				 * but do not try to complete the faulting
				 * access.
				 */
				frame->tf_dmt0 = 0;
				frame->tf_dpfsr = 0;
				KERNEL_UNLOCK();
				return;
			}
			break;
		}
#ifdef TRAPDEBUG
		printf("PBUS Fault %d (%s) va = 0x%x\n", pbus_type,
		    pbus_exception_type[pbus_type], va);
#endif
		KERNEL_UNLOCK();
		goto lose;
		/* NOTREACHED */
	case T_INSTFLT+T_USER:
		/* User mode instruction access fault */
		/* FALLTHROUGH */
	case T_DATAFLT+T_USER:
		KERNEL_LOCK();
user_fault:
		if (type == T_INSTFLT + T_USER) {
			pbus_type = CMMU_PFSR_FAULT(frame->tf_ipfsr);
#ifdef TRAPDEBUG
			printf("User Instruction fault #%d (%s) v = 0x%x, frame 0x%x cpu %p\n",
			    pbus_type, pbus_exception_type[pbus_type],
			    fault_addr, frame, frame->tf_cpu);
#endif
		} else {
			fault_addr = frame->tf_dma0;
			pbus_type = CMMU_PFSR_FAULT(frame->tf_dpfsr);
#ifdef TRAPDEBUG
			printf("User Data access fault #%d (%s) v = 0x%x, frame 0x%x cpu %p\n",
			    pbus_type, pbus_exception_type[pbus_type],
			    fault_addr, frame, frame->tf_cpu);
#endif
		}

		if (frame->tf_dmt0 & (DMT_WRITE | DMT_LOCKBAR)) {
			ftype = PROT_READ | PROT_WRITE;
			fault_code = PROT_WRITE;
		} else {
			ftype = PROT_READ;
			fault_code = PROT_READ;
		}

		va = trunc_page((vaddr_t)fault_addr);

		vm = p->p_vmspace;
		map = &vm->vm_map;
		if ((pcb_onfault = p->p_addr->u_pcb.pcb_onfault) != 0)
			p->p_addr->u_pcb.pcb_onfault = 0;

		/* Call uvm_fault() to resolve non-bus error faults */
		switch (pbus_type) {
		case CMMU_PFSR_SUCCESS:
			result = 0;
			break;
		case CMMU_PFSR_BERROR:
			result = EACCES;
			break;
		default:
			result = uvm_fault(map, va, VM_FAULT_INVALID, ftype);
			if (result == EACCES)
				result = EFAULT;
			break;
		}

		p->p_addr->u_pcb.pcb_onfault = pcb_onfault;

		if (result == 0 && (caddr_t)va >= vm->vm_maxsaddr)
			uvm_grow(p, va);

		if (result == 0) {
			if (type == T_INSTFLT + T_USER) {
				m88100_rewind_insn(&(frame->tf_regs));
				/* clear the error bit */
				frame->tf_sfip &= ~FIP_E;
				frame->tf_snip &= ~NIP_E;
				frame->tf_ipfsr = 0;
			} else {
				/*
			 	 * We could resolve the fault. Call
			 	 * data_access_emulation to drain the data unit
			 	 * pipe line and reset dmt0 so that trap won't
			 	 * get called again.
			 	 */
				p->p_addr->u_pcb.pcb_onfault = 0;
				data_access_emulation((u_int *)frame);
				p->p_addr->u_pcb.pcb_onfault = pcb_onfault;
				frame->tf_dmt0 = 0;
				frame->tf_dpfsr = 0;
			}
		} else {
			/*
			 * This could be a fault caused in copyin*()
			 * while accessing user space.
			 */
			if (pcb_onfault != 0) {
				frame->tf_snip = pcb_onfault | NIP_V;
				frame->tf_sfip = (pcb_onfault + 4) | FIP_V;
				/*
				 * Continue as if the fault had been resolved,
				 * but do not try to complete the faulting
				 * access.
				 */
				frame->tf_dmt0 = 0;
				frame->tf_dpfsr = 0;
			} else {
				sig = result == EACCES ? SIGBUS : SIGSEGV;
				fault_type = result == EACCES ?
				    BUS_ADRERR : SEGV_MAPERR;
			}
		}
		KERNEL_UNLOCK();
		break;
	case T_MISALGNFLT+T_USER:
		/* Fix any misaligned ld.d or st.d instructions */
		sig = double_reg_fixup(frame, T_MISALGNFLT);
		fault_type = BUS_ADRALN;
		break;
	case T_PRIVINFLT+T_USER:
	case T_ILLFLT+T_USER:
#ifndef DDB
	case T_KDB_BREAK:
	case T_KDB_ENTRY:
#endif
	case T_KDB_BREAK+T_USER:
	case T_KDB_ENTRY+T_USER:
	case T_KDB_TRACE:
	case T_KDB_TRACE+T_USER:
		sig = SIGILL;
		break;
	case T_BNDFLT+T_USER:
		sig = SIGFPE;
		break;
	case T_ZERODIV+T_USER:
		sig = SIGFPE;
		fault_type = FPE_INTDIV;
		break;
	case T_OVFFLT+T_USER:
		sig = SIGFPE;
		fault_type = FPE_INTOVF;
		break;
	case T_FPEPFLT+T_USER:
		m88100_fpu_precise_exception(frame);
		goto userexit;
	case T_FPEIFLT+T_USER:
		m88100_fpu_imprecise_exception(frame);
		goto userexit;
	case T_SIGSYS+T_USER:
		sig = SIGSYS;
		break;
	case T_STEPBPT+T_USER:
#ifdef PTRACE
		/*
		 * This trap is used by the kernel to support single-step
		 * debugging (although any user could generate this trap
		 * which should probably be handled differently). When a
		 * process is continued by a debugger with the PT_STEP
		 * function of ptrace (single step), the kernel inserts
		 * one or two breakpoints in the user process so that only
		 * one instruction (or two in the case of a delayed branch)
		 * is executed.  When this breakpoint is hit, we get the
		 * T_STEPBPT trap.
		 */
		{
			u_int instr;
			vaddr_t pc = PC_REGS(&frame->tf_regs);

			/* read break instruction */
			copyin((caddr_t)pc, &instr, sizeof(u_int));

			/* check and see if we got here by accident */
			if ((p->p_md.md_bp0va != pc &&
			     p->p_md.md_bp1va != pc) ||
			    instr != SSBREAKPOINT) {
				sig = SIGTRAP;
				fault_type = TRAP_TRACE;
				break;
			}

			/* restore original instruction and clear breakpoint */
			KERNEL_LOCK();
			if (p->p_md.md_bp0va == pc) {
				ss_put_value(p, pc, p->p_md.md_bp0save);
				p->p_md.md_bp0va = 0;
			}
			if (p->p_md.md_bp1va == pc) {
				ss_put_value(p, pc, p->p_md.md_bp1save);
				p->p_md.md_bp1va = 0;
			}
			KERNEL_UNLOCK();

			frame->tf_sxip = pc | NIP_V;
			sig = SIGTRAP;
			fault_type = TRAP_BRKPT;
		}
#else
		sig = SIGTRAP;
		fault_type = TRAP_TRACE;
#endif
		break;

	case T_USERBPT+T_USER:
		/*
		 * This trap is meant to be used by debuggers to implement
		 * breakpoint debugging.  When we get this trap, we just
		 * return a signal which gets caught by the debugger.
		 */
		sig = SIGTRAP;
		fault_type = TRAP_BRKPT;
		break;

	}

	/*
	 * If trap from supervisor mode, just return
	 */
	if (type < T_USER)
		return;

	if (sig) {
		sv.sival_ptr = (void *)fault_addr;
		KERNEL_LOCK();
		trapsignal(p, sig, fault_code, fault_type, sv);
		KERNEL_UNLOCK();
		/*
		 * don't want multiple faults - we are going to
		 * deliver signal.
		 */
		frame->tf_dmt0 = 0;
		frame->tf_ipfsr = frame->tf_dpfsr = 0;
	}

userexit:
	userret(p);
}
#endif /* M88100 */

#ifdef M88110
void
m88110_trap(u_int type, struct trapframe *frame)
{
	struct proc *p;
	struct vm_map *map;
	vaddr_t va, pcb_onfault;
	vm_prot_t ftype;
	int fault_type;
	u_long fault_code;
	vaddr_t fault_addr;
	struct vmspace *vm;
	union sigval sv;
	int result;
#ifdef DDB
        int s;
	u_int psr;
#endif
	int sig = 0;

	uvmexp.traps++;
	if ((p = curproc) == NULL)
		p = &proc0;

	fault_type = SI_NOINFO;
	fault_code = 0;
	fault_addr = frame->tf_exip & XIP_ADDR;

	/*
	 * 88110 errata #16 (4.2) or #3 (5.1.1):
	 * ``bsr, br, bcnd, jsr and jmp instructions with the .n extension
	 *   can cause the enip value to be incremented by 4 incorrectly
	 *   if the instruction in the delay slot is the first word of a
	 *   page which misses in the mmu and results in a hardware
	 *   tablewalk which encounters an exception or an invalid
	 *   descriptor.  The exip value in this case will point to the
	 *   first word of the page, and the D bit will be set.
	 *
	 *   Note: if the instruction is a jsr.n r1, r1 will be overwritten
	 *   with erroneous data.  Therefore, no recovery is possible. Do
	 *   not allow this instruction to occupy the last word of a page.
	 *
	 *   Suggested fix: recover in general by backing up the exip by 4
	 *   and clearing the delay bit before an rte when the lower 3 hex
	 *   digits of the exip are 001.''
	 */
	if ((frame->tf_exip & PAGE_MASK) == 0x00000001 && type == T_INSTFLT) {
		u_int instr;

		/*
		 * Note that we have initialized fault_addr above, so that
		 * signals provide the correct address if necessary.
		 */
		frame->tf_exip = (frame->tf_exip & ~1) - 4;

		/*
		 * Check the instruction at the (backed up) exip.
		 * If it is a jsr.n, abort.
		 */
		if (!USERMODE(frame->tf_epsr)) {
			instr = *(u_int *)fault_addr;
			if (instr == 0xf400cc01)
				panic("mc88110 errata #16, exip 0x%lx enip 0x%lx",
				    (frame->tf_exip + 4) | 1, frame->tf_enip);
		} else {
			/* copyin here should not fail */
			if (copyin((const void *)frame->tf_exip, &instr,
			    sizeof instr) == 0 &&
			    instr == 0xf400cc01) {
				uprintf("mc88110 errata #16, exip 0x%lx enip 0x%lx",
				    (frame->tf_exip + 4) | 1, frame->tf_enip);
				sig = SIGILL;
			}
		}
	}

	if (USERMODE(frame->tf_epsr)) {
		type += T_USER;
		p->p_md.md_tf = frame;	/* for ptrace/signals */
		refreshcreds(p);
	}

	if (sig != 0)
		goto deliver;

	switch (type) {
	default:
lose:
		panictrap(frame->tf_vector, frame);
		break;
		/*NOTREACHED*/

#ifdef DEBUG
	case T_110_DRM+T_USER:
	case T_110_DRM:
		printf("DMMU read miss: Hardware Table Searches should be enabled!\n");
		goto lose;
	case T_110_DWM+T_USER:
	case T_110_DWM:
		printf("DMMU write miss: Hardware Table Searches should be enabled!\n");
		goto lose;
	case T_110_IAM+T_USER:
	case T_110_IAM:
		printf("IMMU miss: Hardware Table Searches should be enabled!\n");
		goto lose;
#endif

#ifdef DDB
	case T_KDB_TRACE:
		s = splhigh();
		set_psr((psr = get_psr()) & ~PSR_IND);
		ddb_break_trap(T_KDB_TRACE, (db_regs_t*)frame);
		set_psr(psr);
		splx(s);
		return;
	case T_KDB_BREAK:
		s = splhigh();
		set_psr((psr = get_psr()) & ~PSR_IND);
		ddb_break_trap(T_KDB_BREAK, (db_regs_t*)frame);
		set_psr(psr);
		splx(s);
		return;
	case T_KDB_ENTRY:
		s = splhigh();
		set_psr((psr = get_psr()) & ~PSR_IND);
		ddb_entry_trap(T_KDB_ENTRY, (db_regs_t*)frame);
		set_psr(psr);
		/* skip trap instruction */
		m88110_skip_insn(frame);
		splx(s);
		return;
#endif /* DDB */
	case T_ILLFLT:
		/*
		 * The 88110 seems to trigger an instruction fault in
		 * supervisor mode when running the following sequence:
		 *
		 *	bcnd.n cond, reg, 1f
		 *	arithmetic insn
		 *	...
		 *  	the same exact arithmetic insn
		 *  1:	another arithmetic insn stalled by the previous one
		 *	...
		 *
		 * The exception is reported with exip pointing to the
		 * branch address. I don't know, at this point, if there
		 * is any better workaround than the aggressive one
		 * implemented below; I don't see how this could relate to
		 * any of the 88110 errata (although it might be related to
		 * branch prediction).
		 *
		 * For the record, the exact sequence triggering the
		 * spurious exception is:
		 *
		 *	bcnd.n	eq0, r2,  1f
		 *	 or	r25, r0,  r22
		 *	bsr	somewhere
		 *	or	r25, r0,  r22
		 *  1:	cmp	r13, r25, r20
		 *
		 * within the same cache line.
		 *
		 * Simply ignoring the exception and returning does not
		 * cause the exception to disappear. Clearing the
		 * instruction cache works, but on 88110+88410 systems,
		 * the 88410 needs to be invalidated as well. (note that
		 * the size passed to the flush routines does not matter
		 * since there is no way to flush a subset of the 88110
		 * I$ anyway)
		 */
	    {
		extern void *kernel_text, *etext;

		if (fault_addr >= (vaddr_t)&kernel_text &&
		    fault_addr < (vaddr_t)&etext) {
			cmmu_icache_inv(curcpu()->ci_cpuid,
			    trunc_page(fault_addr), PAGE_SIZE);
			cmmu_cache_wbinv(curcpu()->ci_cpuid,
			    trunc_page(fault_addr), PAGE_SIZE);
			return;
		}
	    }
		goto lose;
	case T_MISALGNFLT:
		printf("kernel misaligned access exception @%p\n",
		    (void *)frame->tf_exip);
		goto lose;
	case T_INSTFLT:
		/* kernel mode instruction access fault.
		 * Should never, never happen for a non-paged kernel.
		 */
#ifdef TRAPDEBUG
		printf("Kernel Instruction fault exip %x isr %x ilar %x\n",
		    frame->tf_exip, frame->tf_isr, frame->tf_ilar);
#endif
		goto lose;

	case T_DATAFLT:
		/* kernel mode data fault */

		/* data fault on the user address? */
		if ((frame->tf_dsr & CMMU_DSR_SU) == 0) {
			KERNEL_LOCK();
			goto m88110_user_fault;
		}

#ifdef TRAPDEBUG
		printf("Kernel Data access fault exip %x dsr %x dlar %x\n",
		    frame->tf_exip, frame->tf_dsr, frame->tf_dlar);
#endif

		fault_addr = frame->tf_dlar;
		if (frame->tf_dsr & CMMU_DSR_RW) {
			ftype = PROT_READ;
			fault_code = PROT_READ;
		} else {
			ftype = PROT_READ | PROT_WRITE;
			fault_code = PROT_WRITE;
		}

		va = trunc_page((vaddr_t)fault_addr);

		KERNEL_LOCK();
		vm = p->p_vmspace;
		map = kernel_map;

		if (frame->tf_dsr & (CMMU_DSR_SI | CMMU_DSR_PI)) {
			/*
			 * On a segment or a page fault, call uvm_fault() to
			 * resolve the fault.
			 */
			if ((pcb_onfault = p->p_addr->u_pcb.pcb_onfault) != 0)
				p->p_addr->u_pcb.pcb_onfault = 0;
			result = uvm_fault(map, va, VM_FAULT_INVALID, ftype);
			p->p_addr->u_pcb.pcb_onfault = pcb_onfault;
			/*
			 * This could be a fault caused in copyout*()
			 * while accessing kernel space.
			 */
			if (result != 0 && pcb_onfault != 0) {
				frame->tf_exip = pcb_onfault;
				/*
				 * Continue as if the fault had been resolved.
				 */
				result = 0;
			}
			if (result == 0) {
				KERNEL_UNLOCK();
				return;
			}
		}
		KERNEL_UNLOCK();
		goto lose;
	case T_INSTFLT+T_USER:
		/* User mode instruction access fault */
		/* FALLTHROUGH */
	case T_DATAFLT+T_USER:
		KERNEL_LOCK();
m88110_user_fault:
		if (type == T_INSTFLT+T_USER) {
			ftype = PROT_READ;
			fault_code = PROT_READ;
#ifdef TRAPDEBUG
			printf("User Instruction fault exip %x isr %x ilar %x\n",
			    frame->tf_exip, frame->tf_isr, frame->tf_ilar);
#endif
		} else {
			fault_addr = frame->tf_dlar;
			if (frame->tf_dsr & CMMU_DSR_RW) {
				ftype = PROT_READ;
				fault_code = PROT_READ;
			} else {
				ftype = PROT_READ | PROT_WRITE;
				fault_code = PROT_WRITE;
			}
#ifdef TRAPDEBUG
			printf("User Data access fault exip %x dsr %x dlar %x\n",
			    frame->tf_exip, frame->tf_dsr, frame->tf_dlar);
#endif
		}

		va = trunc_page((vaddr_t)fault_addr);

		vm = p->p_vmspace;
		map = &vm->vm_map;
		if ((pcb_onfault = p->p_addr->u_pcb.pcb_onfault) != 0)
			p->p_addr->u_pcb.pcb_onfault = 0;

		/*
		 * Call uvm_fault() to resolve non-bus error faults
		 * whenever possible.
		 */
		if (type == T_INSTFLT+T_USER) {
			/* instruction faults */
			if (frame->tf_isr &
			    (CMMU_ISR_BE | CMMU_ISR_SP | CMMU_ISR_TBE)) {
				/* bus error, supervisor protection */
				result = EACCES;
			} else
			if (frame->tf_isr & (CMMU_ISR_SI | CMMU_ISR_PI)) {
				/* segment or page fault */
				result = uvm_fault(map, va, VM_FAULT_INVALID, ftype);
				if (result == EACCES)
					result = EFAULT;
			} else {
#ifdef TRAPDEBUG
				printf("Unexpected Instruction fault isr %x\n",
				    frame->tf_isr);
#endif
				KERNEL_UNLOCK();
				goto lose;
			}
		} else {
			/* data faults */
			if (frame->tf_dsr & CMMU_DSR_BE) {
				/* bus error */
				result = EACCES;
			} else
			if (frame->tf_dsr & (CMMU_DSR_SI | CMMU_DSR_PI)) {
				/* segment or page fault */
				result = uvm_fault(map, va, VM_FAULT_INVALID, ftype);
				if (result == EACCES)
					result = EFAULT;
			} else
			if (frame->tf_dsr & (CMMU_DSR_CP | CMMU_DSR_WA)) {
				/* copyback or write allocate error */
				result = EACCES;
			} else
			if (frame->tf_dsr & CMMU_DSR_WE) {
				/* write fault  */
				/* This could be a write protection fault or an
				 * exception to set the used and modified bits
				 * in the pte. Basically, if we got a write
				 * error, then we already have a pte entry that
				 * faulted in from a previous seg fault or page
				 * fault.
				 * Get the pte and check the status of the
				 * modified and valid bits to determine if this
				 * indeed a real write fault.  XXX smurph
				 */
				if (pmap_set_modify(map->pmap, va)) {
#ifdef TRAPDEBUG
					printf("Corrected userland write fault, pmap %p va %p\n",
					    map->pmap, va);
#endif
					result = 0;
				} else {
					/* must be a real wp fault */
#ifdef TRAPDEBUG
					printf("Uncorrected userland write fault, pmap %p va %p\n",
					    map->pmap, va);
#endif
					result = uvm_fault(map, va, VM_FAULT_INVALID, ftype);
					if (result == EACCES)
						result = EFAULT;
				}
			} else {
#ifdef TRAPDEBUG
				printf("Unexpected Data access fault dsr %x\n",
				    frame->tf_dsr);
#endif
				KERNEL_UNLOCK();
				goto lose;
			}
		}
		p->p_addr->u_pcb.pcb_onfault = pcb_onfault;

		if (result == 0 && (caddr_t)va >= vm->vm_maxsaddr)
			uvm_grow(p, va);
		KERNEL_UNLOCK();

		/*
		 * This could be a fault caused in copyin*()
		 * while accessing user space.
		 */
		if (result != 0 && pcb_onfault != 0) {
			frame->tf_exip = pcb_onfault;
			/*
			 * Continue as if the fault had been resolved.
			 */
			result = 0;
		}

		if (result != 0) {
			sig = result == EACCES ? SIGBUS : SIGSEGV;
			fault_type = result == EACCES ?
			    BUS_ADRERR : SEGV_MAPERR;
		}
		break;
	case T_MISALGNFLT+T_USER:
		/* Fix any misaligned ld.d or st.d instructions */
		sig = double_reg_fixup(frame, T_MISALGNFLT);
		fault_type = BUS_ADRALN;
		if (sig == 0) {
			/* skip recovered instruction */
			m88110_skip_insn(frame);
			goto userexit;
		}
		break;
	case T_ILLFLT+T_USER:
		/* Fix any ld.d or st.d instruction with an odd register */
		sig = double_reg_fixup(frame, T_ILLFLT);
		fault_type = ILL_PRVREG;
		if (sig == 0) {
			/* skip recovered instruction */
			m88110_skip_insn(frame);
			goto userexit;
		}
		break;
	case T_PRIVINFLT+T_USER:
		fault_type = ILL_PRVREG;
		/* FALLTHROUGH */
#ifndef DDB
	case T_KDB_BREAK:
	case T_KDB_ENTRY:
	case T_KDB_TRACE:
#endif
	case T_KDB_BREAK+T_USER:
	case T_KDB_ENTRY+T_USER:
	case T_KDB_TRACE+T_USER:
		sig = SIGILL;
		break;
	case T_BNDFLT+T_USER:
		sig = SIGFPE;
		/* skip trap instruction */
		m88110_skip_insn(frame);
		break;
	case T_ZERODIV+T_USER:
		sig = SIGFPE;
		fault_type = FPE_INTDIV;
		/* skip trap instruction */
		m88110_skip_insn(frame);
		break;
	case T_OVFFLT+T_USER:
		sig = SIGFPE;
		fault_type = FPE_INTOVF;
		/* skip trap instruction */
		m88110_skip_insn(frame);
		break;
	case T_FPEPFLT+T_USER:
		m88110_fpu_exception(frame);
		goto userexit;
	case T_SIGSYS+T_USER:
		sig = SIGSYS;
		break;
	case T_STEPBPT+T_USER:
#ifdef PTRACE
		/*
		 * This trap is used by the kernel to support single-step
		 * debugging (although any user could generate this trap
		 * which should probably be handled differently). When a
		 * process is continued by a debugger with the PT_STEP
		 * function of ptrace (single step), the kernel inserts
		 * one or two breakpoints in the user process so that only
		 * one instruction (or two in the case of a delayed branch)
		 * is executed.  When this breakpoint is hit, we get the
		 * T_STEPBPT trap.
		 */
		{
			u_int instr;
			vaddr_t pc = PC_REGS(&frame->tf_regs);

			/* read break instruction */
			copyin((caddr_t)pc, &instr, sizeof(u_int));

			/* check and see if we got here by accident */
			if ((p->p_md.md_bp0va != pc &&
			     p->p_md.md_bp1va != pc) ||
			    instr != SSBREAKPOINT) {
				sig = SIGTRAP;
				fault_type = TRAP_TRACE;
				break;
			}

			/* restore original instruction and clear breakpoint */
			KERNEL_LOCK();
			if (p->p_md.md_bp0va == pc) {
				ss_put_value(p, pc, p->p_md.md_bp0save);
				p->p_md.md_bp0va = 0;
			}
			if (p->p_md.md_bp1va == pc) {
				ss_put_value(p, pc, p->p_md.md_bp1save);
				p->p_md.md_bp1va = 0;
			}
			KERNEL_UNLOCK();

			sig = SIGTRAP;
			fault_type = TRAP_BRKPT;
		}
#else
		sig = SIGTRAP;
		fault_type = TRAP_TRACE;
#endif
		break;
	case T_USERBPT+T_USER:
		/*
		 * This trap is meant to be used by debuggers to implement
		 * breakpoint debugging.  When we get this trap, we just
		 * return a signal which gets caught by the debugger.
		 */
		sig = SIGTRAP;
		fault_type = TRAP_BRKPT;
		break;
	}

	/*
	 * If trap from supervisor mode, just return
	 */
	if (type < T_USER)
		return;

	if (sig) {
deliver:
		sv.sival_ptr = (void *)fault_addr;
		KERNEL_LOCK();
		trapsignal(p, sig, fault_code, fault_type, sv);
		KERNEL_UNLOCK();
	}

userexit:
	userret(p);
}
#endif /* M88110 */

__dead void
error_fatal(struct trapframe *frame)
{
	if (frame->tf_vector == 0)
		printf("\nCPU %d Reset Exception\n", cpu_number());
	else
		printf("\nCPU %d Error Exception\n", cpu_number());

#ifdef DDB
	regdump((struct trapframe*)frame);
#endif
	panic("unrecoverable exception %ld", frame->tf_vector);
}

#ifdef M88100
void
m88100_syscall(register_t code, struct trapframe *tf)
{
	int i, nsys, nap;
	struct sysent *callp;
	struct proc *p = curproc;
	int error;
	register_t args[8], rval[2], *ap;

	uvmexp.syscalls++;

	callp = p->p_p->ps_emul->e_sysent;
	nsys  = p->p_p->ps_emul->e_nsysent;

	p->p_md.md_tf = tf;

	/*
	 * For 88k, all the arguments are passed in the registers (r2-r9),
	 * and further arguments (if any) on stack.
	 * For syscall (and __syscall), r2 (and r3) has the actual code.
	 * __syscall  takes a quad syscall number, so that other
	 * arguments are at their natural alignments.
	 */
	ap = &tf->tf_r[2];
	nap = 8; /* r2-r9 */

	switch (code) {
	case SYS_syscall:
		code = *ap++;
		nap--;
		break;
	case SYS___syscall:
		if (callp != sysent)
			break;
		code = ap[_QUAD_LOWWORD];
		ap += 2;
		nap -= 2;
		break;
	}

	if (code < 0 || code >= nsys)
		callp += p->p_p->ps_emul->e_nosys;
	else
		callp += code;

	i = callp->sy_argsize / sizeof(register_t);
	if (i > sizeof(args) / sizeof(register_t))
		panic("syscall nargs");
	if (i > nap) {
		bcopy((caddr_t)ap, (caddr_t)args, nap * sizeof(register_t));
		if ((error = copyin((caddr_t)tf->tf_r[31], args + nap,
		    (i - nap) * sizeof(register_t))))
			goto bad;
	} else
		bcopy((caddr_t)ap, (caddr_t)args, i * sizeof(register_t));

	rval[0] = 0;
	rval[1] = tf->tf_r[3];

	error = mi_syscall(p, code, callp, args, rval);

	/*
	 * system call will look like:
	 *	 or r13, r0, <code>
	 *       tb0 0, r0, <128> <- sxip
	 *	 br err 	  <- snip
	 *       jmp r1 	  <- sfip
	 *  err: or.u r3, r0, hi16(errno)
	 *	 st r2, r3, lo16(errno)
	 *	 subu r2, r0, 1
	 *	 jmp r1
	 *
	 * So, when we take syscall trap, sxip/snip/sfip will be as
	 * shown above.
	 * Given this,
	 * 1. If the system call returned 0, need to skip nip.
	 *	nip = fip, fip += 4
	 *    (doesn't matter what fip + 4 will be but we will never
	 *    execute this since jmp r1 at nip will change the execution flow.)
	 * 2. If the system call returned an errno > 0, plug the value
	 *    in r2, and leave nip and fip unchanged. This will have us
	 *    executing "br err" on return to user space.
	 * 3. If the system call code returned ERESTART,
	 *    we need to rexecute the trap instruction. Back up the pipe
	 *    line.
	 *     fip = nip, nip = xip
	 * 4. If the system call returned EJUSTRETURN, don't need to adjust
	 *    any pointers.
	 */

	switch (error) {
	case 0:
		tf->tf_r[2] = rval[0];
		tf->tf_r[3] = rval[1];
		tf->tf_epsr &= ~PSR_C;
		tf->tf_snip = tf->tf_sfip & ~NIP_E;
		tf->tf_sfip = tf->tf_snip + 4;
		break;
	case ERESTART:
		m88100_rewind_insn(&(tf->tf_regs));
		/* clear the error bit */
		tf->tf_sfip &= ~FIP_E;
		tf->tf_snip &= ~NIP_E;
		break;
	case EJUSTRETURN:
		break;
	default:
	bad:
		tf->tf_r[2] = error;
		tf->tf_epsr |= PSR_C;   /* fail */
		tf->tf_snip = tf->tf_snip & ~NIP_E;
		tf->tf_sfip = tf->tf_sfip & ~FIP_E;
		break;
	}

	mi_syscall_return(p, code, error, rval);
}
#endif /* M88100 */

#ifdef M88110
/* Instruction pointers operate differently on mc88110 */
void
m88110_syscall(register_t code, struct trapframe *tf)
{
	int i, nsys, nap;
	struct sysent *callp;
	struct proc *p = curproc;
	int error;
	register_t args[8], rval[2], *ap;

	uvmexp.syscalls++;

	callp = p->p_p->ps_emul->e_sysent;
	nsys  = p->p_p->ps_emul->e_nsysent;

	p->p_md.md_tf = tf;

	/*
	 * For 88k, all the arguments are passed in the registers (r2-r9),
	 * and further arguments (if any) on stack.
	 * For syscall (and __syscall), r2 (and r3) has the actual code.
	 * __syscall  takes a quad syscall number, so that other
	 * arguments are at their natural alignments.
	 */
	ap = &tf->tf_r[2];
	nap = 8;	/* r2-r9 */

	switch (code) {
	case SYS_syscall:
		code = *ap++;
		nap--;
		break;
	case SYS___syscall:
		if (callp != sysent)
			break;
		code = ap[_QUAD_LOWWORD];
		ap += 2;
		nap -= 2;
		break;
	}

	if (code < 0 || code >= nsys)
		callp += p->p_p->ps_emul->e_nosys;
	else
		callp += code;

	i = callp->sy_argsize / sizeof(register_t);
	if (i > sizeof(args) / sizeof(register_t))
		panic("syscall nargs");
	if (i > nap) {
		bcopy((caddr_t)ap, (caddr_t)args, nap * sizeof(register_t));
		if ((error = copyin((caddr_t)tf->tf_r[31], args + nap,
		    (i - nap) * sizeof(register_t))))
			goto bad;
	} else
		bcopy((caddr_t)ap, (caddr_t)args, i * sizeof(register_t));

	rval[0] = 0;
	rval[1] = tf->tf_r[3];

	error = mi_syscall(p, code, callp, args, rval);

	/*
	 * system call will look like:
	 *	 or r13, r0, <code>
	 *       tb0 0, r0, <128> <- exip
	 *	 br err 	  <- enip
	 *       jmp r1
	 *  err: or.u r3, r0, hi16(errno)
	 *	 st r2, r3, lo16(errno)
	 *	 subu r2, r0, 1
	 *	 jmp r1
	 *
	 * So, when we take syscall trap, exip/enip will be as
	 * shown above.
	 * Given this,
	 * 1. If the system call returned 0, need to jmp r1.
	 *    exip += 8
	 * 2. If the system call returned an errno > 0, increment
	 *    exip += 4 and plug the value in r2. This will have us
	 *    executing "br err" on return to user space.
	 * 3. If the system call code returned ERESTART,
	 *    we need to rexecute the trap instruction. leave exip as is.
	 * 4. If the system call returned EJUSTRETURN, just return.
	 *    exip += 4
	 */

	switch (error) {
	case 0:
		tf->tf_r[2] = rval[0];
		tf->tf_r[3] = rval[1];
		tf->tf_epsr &= ~PSR_C;
		/* skip two instructions */
		m88110_skip_insn(tf);
		m88110_skip_insn(tf);
		break;
	case ERESTART:
		/*
		 * Reexecute the trap.
		 * exip is already at the trap instruction, so
		 * there is nothing to do.
		 */
		break;
	case EJUSTRETURN:
		/* skip one instruction */
		m88110_skip_insn(tf);
		break;
	default:
	bad:
		tf->tf_r[2] = error;
		tf->tf_epsr |= PSR_C;   /* fail */
		/* skip one instruction */
		m88110_skip_insn(tf);
		break;
	}

	mi_syscall_return(p, code, error, rval);
}
#endif	/* M88110 */

/*
 * Set up return-value registers as fork() libc stub expects,
 * and do normal return-to-user-mode stuff.
 */
void
child_return(arg)
	void *arg;
{
	struct proc *p = arg;
	struct trapframe *tf;

	tf = (struct trapframe *)USER_REGS(p);
	tf->tf_r[2] = 0;
	tf->tf_r[3] = 0;
	tf->tf_epsr &= ~PSR_C;
	/* reset r26 (used by the threads library) if __tfork */
	if (p->p_flag & P_THREAD)
		tf->tf_r[26] = 0;
	/* skip br instruction as in syscall() */
#ifdef M88100
	if (CPU_IS88100) {
		tf->tf_snip = (tf->tf_sfip & XIP_ADDR) | XIP_V;
		tf->tf_sfip = tf->tf_snip + 4;
	}
#endif
#ifdef M88110
	if (CPU_IS88110) {
		/* skip two instructions */
		m88110_skip_insn(tf);
		m88110_skip_insn(tf);
	}
#endif

	KERNEL_UNLOCK();

	mi_child_return(p);
}

#ifdef PTRACE

/*
 * User Single Step Debugging Support
 */

#include <sys/ptrace.h>

vaddr_t	ss_branch_taken(u_int, vaddr_t, struct reg *);
int	ss_get_value(struct proc *, vaddr_t, u_int *);
int	ss_inst_branch_or_call(u_int);
int	ss_put_breakpoint(struct proc *, vaddr_t, vaddr_t *, u_int *);

#define	SYSCALL_INSTR	0xf000d080	/* tb0 0,r0,128 */

int
ss_get_value(struct proc *p, vaddr_t addr, u_int *value)
{
	struct uio uio;
	struct iovec iov;

	iov.iov_base = (caddr_t)value;
	iov.iov_len = sizeof(u_int);
	uio.uio_iov = &iov;
	uio.uio_iovcnt = 1;
	uio.uio_offset = (off_t)addr;
	uio.uio_resid = sizeof(u_int);
	uio.uio_segflg = UIO_SYSSPACE;
	uio.uio_rw = UIO_READ;
	uio.uio_procp = curproc;
	return (process_domem(curproc, p, &uio, PT_READ_I));
}

int
ss_put_value(struct proc *p, vaddr_t addr, u_int value)
{
	struct uio uio;
	struct iovec iov;

	iov.iov_base = (caddr_t)&value;
	iov.iov_len = sizeof(u_int);
	uio.uio_iov = &iov;
	uio.uio_iovcnt = 1;
	uio.uio_offset = (off_t)addr;
	uio.uio_resid = sizeof(u_int);
	uio.uio_segflg = UIO_SYSSPACE;
	uio.uio_rw = UIO_WRITE;
	uio.uio_procp = curproc;
	return (process_domem(curproc, p, &uio, PT_WRITE_I));
}

/*
 * ss_branch_taken(instruction, pc, regs)
 *
 * instruction will be a control flow instruction location at address pc.
 * Branch taken is supposed to return the address to which the instruction
 * would jump if the branch is taken.
 *
 * This is different from branch_taken() in ddb, as we also need to process
 * system calls.
 */
vaddr_t
ss_branch_taken(u_int inst, vaddr_t pc, struct reg *regs)
{
	u_int regno;

	/*
	 * Quick check of the instruction. Note that we know we are only
	 * invoked if ss_inst_branch_or_call() returns TRUE, so we do not
	 * need to repeat the jpm, jsr and syscall stricter checks here.
	 */
	switch (inst >> (32 - 5)) {
	case 0x18:	/* br */
	case 0x19:	/* bsr */
		/* signed 26 bit pc relative displacement, shift left 2 bits */
		inst = (inst & 0x03ffffff) << 2;
		/* check if sign extension is needed */
		if (inst & 0x08000000)
			inst |= 0xf0000000;
		return (pc + inst);

	case 0x1a:	/* bb0 */
	case 0x1b:	/* bb1 */
	case 0x1d:	/* bcnd */
		/* signed 16 bit pc relative displacement, shift left 2 bits */
		inst = (inst & 0x0000ffff) << 2;
		/* check if sign extension is needed */
		if (inst & 0x00020000)
			inst |= 0xfffc0000;
		return (pc + inst);

	case 0x1e:	/* jmp or jsr */
		regno = inst & 0x1f;	/* get the register value */
		return (regno == 0 ? 0 : regs->r[regno]);

	default:	/* system call */
		/*
		 * The regular (pc + 4) breakpoint will match the error
		 * return. Successful system calls return at (pc + 8),
		 * so we'll set up a branch breakpoint there.
		 */
		return (pc + 8);
	}
}

int
ss_inst_branch_or_call(u_int ins)
{
	/* check high five bits */
	switch (ins >> (32 - 5)) {
	case 0x18: /* br */
	case 0x19: /* bsr */
	case 0x1a: /* bb0 */
	case 0x1b: /* bb1 */
	case 0x1d: /* bcnd */
		return (TRUE);
	case 0x1e: /* could be jmp or jsr */
		if ((ins & 0xfffff3e0) == 0xf400c000)
			return (TRUE);
	}

	return (FALSE);
}

int
ss_put_breakpoint(struct proc *p, vaddr_t va, vaddr_t *bpva, u_int *bpsave)
{
	int rc;

	/* Restore previous breakpoint if we did not trigger it. */
	if (*bpva != 0) {
		ss_put_value(p, *bpva, *bpsave);
		*bpva = 0;
	}

	/* Save instruction. */
	if ((rc = ss_get_value(p, va, bpsave)) != 0)
		return (rc);

	/* Store breakpoint instruction at the location now. */
	*bpva = va;
	return (ss_put_value(p, va, SSBREAKPOINT));
}

int
process_sstep(struct proc *p, int sstep)
{
	struct reg *sstf = USER_REGS(p);
	vaddr_t pc, brpc;
	u_int32_t instr;
	int rc;

	if (sstep == 0) {
		/* Restore previous breakpoints if any. */
		if (p->p_md.md_bp0va != 0) {
			ss_put_value(p, p->p_md.md_bp0va, p->p_md.md_bp0save);
			p->p_md.md_bp0va = 0;
		}
		if (p->p_md.md_bp1va != 0) {
			ss_put_value(p, p->p_md.md_bp1va, p->p_md.md_bp1save);
			p->p_md.md_bp1va = 0;
		}

		return (0);
	}

	/*
	 * User was stopped at pc, e.g. the instruction at pc was not executed.
	 * Fetch what's at the current location.
	 */
	pc = PC_REGS(sstf);
	if ((rc = ss_get_value(p, pc, &instr)) != 0)
		return (rc);

	/*
	 * Find if this instruction may cause a branch, and set up a breakpoint
	 * at the branch location.
	 */
	if (ss_inst_branch_or_call(instr) || instr == SYSCALL_INSTR) {
		brpc = ss_branch_taken(instr, pc, sstf);

		/* self-branches are hopeless */
		if (brpc != pc && brpc != 0) {
			if ((rc = ss_put_breakpoint(p, brpc,
			    &p->p_md.md_bp1va, &p->p_md.md_bp1save)) != 0)
				return (rc);
		}
	}

	if ((rc = ss_put_breakpoint(p, pc + 4,
	    &p->p_md.md_bp0va, &p->p_md.md_bp0save)) != 0)
		return (rc);

	return (0);
}

#endif	/* PTRACE */

#ifdef DIAGNOSTIC
void
splassert_check(int wantipl, const char *func)
{
	int oldipl;

	oldipl = getipl();

	if (oldipl < wantipl) {
		splassert_fail(wantipl, oldipl, func);
		/*
		 * This will raise the spl,
		 * in a feeble attempt to reduce further damage.
		 */
		(void)raiseipl(wantipl);
	}
}
#endif

/*
 * ld.d and st.d instructions referencing long aligned but not long long
 * aligned addresses will trigger a misaligned address exception.
 *
 * This routine attempts to recover these (valid) statements, by simulating
 * the split form of the instruction. If it fails, it returns the appropriate
 * signal number to deliver.
 *
 * Note that we do not attempt to do anything for .d.usr instructions - the
 * kernel never issues such instructions, and they cause a privileged
 * instruction exception from userland.
 */
int
double_reg_fixup(struct trapframe *frame, int fault)
{
	u_int32_t pc, instr, value;
	int regno, store;
	vaddr_t addr;

	/*
	 * Decode the faulting instruction.
	 */

	pc = PC_REGS(&frame->tf_regs);
	if (copyin((void *)pc, &instr, sizeof(u_int32_t)) != 0)
		return SIGSEGV;

	switch (instr & 0xfc00ff00) {
	case 0xf4001000:	/* ld.d rD, rS1, rS2 */
		addr = frame->tf_r[(instr >> 16) & 0x1f]
		    + frame->tf_r[(instr & 0x1f)];
		store = 0;
		break;
	case 0xf4002000:	/* st.d rD, rS1, rS2 */
		addr = frame->tf_r[(instr >> 16) & 0x1f]
		    + frame->tf_r[(instr & 0x1f)];
		store = 1;
		break;
	default:
		switch (instr >> 26) {
		case 0x10000000 >> 26:	/* ld.d rD, rS, imm16 */
			addr = (instr & 0x0000ffff) +
			    frame->tf_r[(instr >> 16) & 0x1f];
			store = 0;
			break;
		case 0x20000000 >> 26:	/* st.d rD, rS, imm16 */
			addr = (instr & 0x0000ffff) +
			    frame->tf_r[(instr >> 16) & 0x1f];
			store = 1;
			break;
		default:
			return SIGBUS;
		}
		break;
	}

	regno = (instr >> 21) & 0x1f;

	switch (fault) {
	case T_MISALGNFLT:
		/* We only handle long but not long long aligned access here */
		if ((addr & 0x07) != 4)
			return SIGBUS;
		break;
	case T_ILLFLT:
		/* We only handle odd register pair number here */
		if ((regno & 0x01) == 0)
			return SIGILL;
		/* We only handle long aligned access here */
		if ((addr & 0x03) != 0)
			return SIGBUS;
		break;
	}

	if (store) {
		/*
		 * Two word stores.
		 */
		if (regno == 0)
			value = 0;
		else
			value = frame->tf_r[regno];
		if (copyout(&value, (void *)addr, sizeof(u_int32_t)) != 0)
			return SIGSEGV;
		if (regno == 31)
			value = 0;
		else
			value = frame->tf_r[regno + 1];
		if (copyout(&value, (void *)(addr + 4), sizeof(u_int32_t)) != 0)
			return SIGSEGV;
	} else {
		/*
		 * Two word loads. r0 should be left unaltered, but the
		 * value should still be fetched even if it is discarded.
		 */
		if (copyin((void *)addr, &value, sizeof(u_int32_t)) != 0)
			return SIGSEGV;
		if (regno != 0)
			frame->tf_r[regno] = value;
		if (copyin((void *)(addr + 4), &value, sizeof(u_int32_t)) != 0)
			return SIGSEGV;
		if (regno != 31)
			frame->tf_r[regno + 1] = value;
	}

	return 0;
}

void
cache_flush(struct trapframe *tf)
{
	struct proc *p = curproc;
	struct pmap *pmap;
	paddr_t pa;
	vaddr_t va;
	vsize_t len, count;

	p->p_md.md_tf = tf;

	pmap = vm_map_pmap(&p->p_vmspace->vm_map);
	va = tf->tf_r[2];
	len = tf->tf_r[3];

	if (va < VM_MIN_ADDRESS || va >= VM_MAXUSER_ADDRESS ||
	    va + len <= va || va + len >= VM_MAXUSER_ADDRESS)
		len = 0;

	while (len != 0) {
		count = min(len, PAGE_SIZE - (va & PAGE_MASK));
		if (pmap_extract(pmap, va, &pa) != FALSE)
			dma_cachectl(pa, count, DMA_CACHE_SYNC);
		va += count;
		len -= count;
	}

#ifdef M88100
	if (CPU_IS88100) {
		/* clear the error bit */
		tf->tf_sfip &= ~FIP_E;
		tf->tf_snip &= ~NIP_E;
	}
#endif
#ifdef M88110
	if (CPU_IS88110) {
		/* skip instruction */
		m88110_skip_insn(tf);
	}
#endif

	userret(p);
}

license: unknown


code:
/*	$OpenBSD: vectors_88100.S,v 1.7 2014/06/09 15:00:57 miod Exp $	*/
/*
 * Mach Operating System
 * Copyright (c) 1991, 1992 Carnegie Mellon University
 * Copyright (c) 1991 OMRON Corporation
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON AND OMRON ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON AND OMRON DISCLAIM ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#include <machine/asm.h>

#define	UNKNOWN16	\
	VECTOR(unknown_handler); VECTOR(unknown_handler); \
	VECTOR(unknown_handler); VECTOR(unknown_handler); \
	VECTOR(unknown_handler); VECTOR(unknown_handler); \
	VECTOR(unknown_handler); VECTOR(unknown_handler); \
	VECTOR(unknown_handler); VECTOR(unknown_handler); \
	VECTOR(unknown_handler); VECTOR(unknown_handler); \
	VECTOR(unknown_handler); VECTOR(unknown_handler); \
	VECTOR(unknown_handler); VECTOR(unknown_handler);

	.data
GLOBAL(vector_list)
	VECTOR(reset_handler)			/* 00 */
	VECTOR(interrupt_handler)		/* 01 */
	VECTOR(instruction_access_handler)	/* 02 */
	VECTOR(data_exception_handler)		/* 03 */
	VECTOR(misaligned_handler)		/* 04 */
	VECTOR(unimplemented_handler)		/* 05 */
	VECTOR(privilege_handler)		/* 06 */
	VECTOR(bounds_handler)			/* 07 */
	VECTOR(divide_handler)			/* 08 */
	VECTOR(overflow_handler)		/* 09 */
	VECTOR(error_handler)			/* 0a */
	VECTOR(unknown_handler)			/* 0b */
	VECTOR(unknown_handler)			/* 0c */
	VECTOR(unknown_handler)			/* 0d */
	VECTOR(unknown_handler)			/* 0e */
	VECTOR(unknown_handler)			/* 0f */
	UNKNOWN16				/* 1x */
	UNKNOWN16				/* 2x */
	UNKNOWN16				/* 3x */
	UNKNOWN16				/* 4x */
	UNKNOWN16				/* 5x */
	UNKNOWN16				/* 6x */
	VECTOR(unknown_handler)			/* 70 */
	VECTOR(unknown_handler)			/* 71 */
	VECTOR(fp_precise_handler)		/* 72 */
	VECTOR(fp_imprecise_handler)		/* 73 */
	VECTOR(unimplemented_handler)		/* 74 */
	VECTOR(unknown_handler)			/* 75 */
	VECTOR(unimplemented_handler)		/* 76 */
	VECTOR(unknown_handler)			/* 77 */
	VECTOR(unimplemented_handler)		/* 78 */
	VECTOR(unknown_handler)			/* 79 */
	VECTOR(unimplemented_handler)		/* 7a */
	VECTOR(unknown_handler)			/* 7b */
	VECTOR(unimplemented_handler)		/* 7c */
	VECTOR(unknown_handler)			/* 7d */
	VECTOR(unimplemented_handler)		/* 7e */
	VECTOR(unknown_handler)			/* 7f */
	VECTOR(sigsys)				/* 80 */
	VECTOR(sigsys)				/* 81 */
#ifdef DDB
	VECTOR(break)				/* 82 */
	VECTOR(trace)				/* 83 */
	VECTOR(entry)				/* 84 */
#endif
	.word	0
	VECTOR(data_exception_handler_bootstrap)


code:
/*	$OpenBSD: vectors_88110.S,v 1.9 2014/06/09 15:00:57 miod Exp $	*/
/*
 * Mach Operating System
 * Copyright (c) 1991, 1992 Carnegie Mellon University
 * Copyright (c) 1991 OMRON Corporation
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON AND OMRON ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON AND OMRON DISCLAIM ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#include <machine/asm.h>

#define	UNKNOWN16	\
	VECTOR(m88110_unknown_handler); VECTOR(m88110_unknown_handler); \
	VECTOR(m88110_unknown_handler); VECTOR(m88110_unknown_handler); \
	VECTOR(m88110_unknown_handler); VECTOR(m88110_unknown_handler); \
	VECTOR(m88110_unknown_handler); VECTOR(m88110_unknown_handler); \
	VECTOR(m88110_unknown_handler); VECTOR(m88110_unknown_handler); \
	VECTOR(m88110_unknown_handler); VECTOR(m88110_unknown_handler); \
	VECTOR(m88110_unknown_handler); VECTOR(m88110_unknown_handler); \
	VECTOR(m88110_unknown_handler); VECTOR(m88110_unknown_handler);

	.data
GLOBAL(m88110_vector_list)
	VECTOR(m88110_reset_handler)		/* 00 */
	VECTOR(m88110_interrupt_handler)	/* 01 */
	VECTOR(m88110_instruction_access_handler)	/* 02 */
	VECTOR(m88110_data_exception_handler)	/* 03 */
	VECTOR(m88110_misaligned_handler)	/* 04 */
	VECTOR(m88110_unimplemented_handler)	/* 05 */
	VECTOR(m88110_privilege_handler)	/* 06 */
	VECTOR(m88110_bounds_handler)		/* 07 */
	VECTOR(m88110_divide_handler)		/* 08 */
	VECTOR(m88110_overflow_handler)		/* 09 */
	VECTOR(m88110_error_handler)		/* 0a */
	VECTOR(m88110_nonmaskable)		/* 0b */
	VECTOR(m88110_data_read_miss)		/* 0c */
	VECTOR(m88110_data_write_miss)		/* 0d */
	VECTOR(m88110_inst_atc_miss)		/* 0e */
	VECTOR(m88110_unknown_handler)		/* 0f */
	UNKNOWN16				/* 1x */
	UNKNOWN16				/* 2x */
	UNKNOWN16				/* 3x */
	UNKNOWN16				/* 4x */
	UNKNOWN16				/* 5x */
	UNKNOWN16				/* 6x */
	VECTOR(m88110_unknown_handler)		/* 70 */
	VECTOR(m88110_unknown_handler)		/* 71 */
	VECTOR(m88110_fpu_handler)		/* 72 */
	VECTOR(m88110_unknown_handler)		/* 73 */
	VECTOR(m88110_unimplemented_handler)	/* 74 */
	VECTOR(m88110_unknown_handler)		/* 75 */
	VECTOR(m88110_unimplemented_handler)	/* 76 */
	VECTOR(m88110_unknown_handler)		/* 77 */
	VECTOR(m88110_unimplemented_handler)	/* 78 */
	VECTOR(m88110_unknown_handler)		/* 79 */
	VECTOR(m88110_unimplemented_handler)	/* 7a */
	VECTOR(m88110_unknown_handler)		/* 7b */
	VECTOR(m88110_unimplemented_handler)	/* 7c */
	VECTOR(m88110_unknown_handler)		/* 7d */
	VECTOR(m88110_unimplemented_handler)	/* 7e */
	VECTOR(m88110_unknown_handler)		/* 7f */
	VECTOR(m88110_sigsys)			/* 80 */
	VECTOR(m88110_sigsys)			/* 81 */
#ifdef DDB
	VECTOR(m88110_break)			/* 82 */
	VECTOR(m88110_trace)			/* 83 */
	VECTOR(m88110_entry)			/* 84 */
#endif
	.word	0
	VECTOR(m88110_data_exception_handler_bootstrap)


code:
/*	$OpenBSD: vm_machdep.c,v 1.24 2015/05/05 02:13:47 guenther Exp $	*/

/*
 * Copyright (c) 1998 Steve Murphree, Jr.
 * Copyright (c) 1996 Nivas Madhur
 * Copyright (c) 1993 Adam Glass
 * Copyright (c) 1988 University of Utah.
 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * the Systems Programming Group of the University of Utah Computer
 * Science Department.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$
 *	from: @(#)vm_machdep.c	7.10 (Berkeley) 5/7/91
 *	vm_machdep.c,v 1.3 1993/07/07 07:09:32 cgd Exp
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/user.h>
#include <sys/vnode.h>
#include <sys/exec.h>
#include <sys/ptrace.h>

#include <uvm/uvm_extern.h>

#include <machine/mmu.h>
#include <machine/cmmu.h>
#include <machine/cpu.h>
#include <machine/trap.h>

extern void savectx(struct pcb *);
extern void switch_exit(struct proc *);

/*
 * Finish a fork operation, with process p2 nearly set up.
 * Copy and update the kernel stack and pcb, making the child
 * ready to run, and marking it so that it can return differently
 * than the parent.  Returns 1 in the child process, 0 in the parent.
 * We currently double-map the user area so that the stack is at the same
 * address in each process; in the future we will probably relocate
 * the frame pointers on the stack after copying.
 */

void
cpu_fork(p1, p2, stack, stacksize, func, arg)
	struct proc *p1, *p2;
	void *stack;
	size_t stacksize;
	void (*func)(void *);
	void *arg;
{
	struct ksigframe {
		void (*func)(void *);
		void *arg;
	} *ksfp;
	extern void proc_trampoline(void);

	/* Copy pcb from p1 to p2. */
	if (p1 == curproc) {
		/* Sync the PCB before we copy it. */
		savectx(curpcb);
	}
#ifdef DIAGNOSTIC
	else if (p1 != &proc0)
		panic("cpu_fork: curproc");
#endif

	bcopy(&p1->p_addr->u_pcb, &p2->p_addr->u_pcb, sizeof(struct pcb));
	p2->p_md.md_tf = (struct trapframe *)USER_REGS(p2);

	/*
	 * If specified, give the child a different stack.
	 */
	if (stack != NULL)
		USER_REGS(p2)->r[31] = (u_int)stack + stacksize;

	ksfp = (struct ksigframe *)((char *)p2->p_addr + USPACE) - 1;
	ksfp->func = func;
	ksfp->arg = arg;

	/*
	 * When this process resumes, r31 will be ksfp and
	 * the process will be at the beginning of proc_trampoline().
	 * proc_trampoline will execute the function func, pop off
	 * ksfp frame, and resume to userland.
	 */

	p2->p_addr->u_pcb.kernel_state.pcb_sp = (u_int)ksfp;
	p2->p_addr->u_pcb.kernel_state.pcb_pc = (u_int)proc_trampoline;
}

/*
 * cpu_exit is called as the last action during exit.
 */
void
cpu_exit(struct proc *p)
{
	pmap_deactivate(p);
	sched_exit(p);
}

/*
 * Map an IO request into kernel virtual address space via phys_map.
 */
void
vmapbuf(bp, len)
	struct buf *bp;
	vsize_t len;
{
	caddr_t addr;
	vaddr_t ova, kva, off;
	paddr_t pa;
	struct pmap *pmap;
	u_int pg;

#ifdef DIAGNOSTIC
	if ((bp->b_flags & B_PHYS) == 0)
		panic("vmapbuf");
#endif

	addr = (caddr_t)trunc_page((vaddr_t)(bp->b_saveaddr = bp->b_data));
	off = (vaddr_t)bp->b_saveaddr & PGOFSET;
	len = round_page(off + len);
	pmap = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);

	ova = kva = uvm_km_valloc_wait(phys_map, len);

	bp->b_data = (caddr_t)(kva + off);
	for (pg = atop(len); pg != 0; pg--) {
		if (pmap_extract(pmap, (vaddr_t)addr, &pa) == FALSE)
			panic("vmapbuf: null page frame");
		pmap_enter(vm_map_pmap(phys_map), kva, pa,
		    PROT_READ | PROT_WRITE,
		    PROT_READ | PROT_WRITE | PMAP_WIRED);
		addr += PAGE_SIZE;
		kva += PAGE_SIZE;
	}
	pmap_update(pmap_kernel());
}

/*
 * Free the io map PTEs associated with this IO operation.
 * We also restore the original b_addr.
 */
void
vunmapbuf(bp, len)
	struct buf *bp;
	vsize_t len;
{
	vaddr_t addr, off;

#ifdef DIAGNOSTIC
	if ((bp->b_flags & B_PHYS) == 0)
		panic("vunmapbuf");
#endif

	addr = trunc_page((vaddr_t)bp->b_data);
	off = (vaddr_t)bp->b_data & PGOFSET;
	len = round_page(off + len);
	pmap_remove(vm_map_pmap(phys_map), addr, addr + len);
	pmap_update(vm_map_pmap(phys_map));
	uvm_km_free_wakeup(phys_map, addr, len);
	bp->b_data = bp->b_saveaddr;
	bp->b_saveaddr = 0;
}


code:
/Makefile/1.10/Sun Dec  8 14:46:39 2013//TOPENBSD_5_8_BASE
/README/1.3/Sun Sep 15 00:52:54 2002//TOPENBSD_5_8_BASE
D/compile////
D/conf////
D/dev////
D/include////
D/macppc////
D/pci////
D/stand////

path: sys/arch/macppc/CVS/Entries


code: src/sys/arch/macppc

path: sys/arch/macppc/CVS/Repository


code:
#	$OpenBSD: Makefile,v 1.10 2013/12/08 14:46:39 espie Exp $

S=	${.CURDIR}/../..
KFILE=	GENERIC
.if exists(conf/GENERIC.MP)
KFILE=	GENERIC.MP
.endif
TDIRS=	${_arch} include pci
TAGS=	${.CURDIR}/tags

NOPROG=
NOMAN=
NOOBJ=
SUBDIR=	stand

# config the fattest kernel we can find into a temporary dir
# to create a Makefile.  Then use make to pull some variables
# out and push them into the sub-shell to expand the paths,
# and finally run ctags.
tags::
	TDIR=`mktemp -d /tmp/_tagXXXXXXXXXX` || exit 1; \
	eval "S=${S}" && \
	config -s ${S} -b $${TDIR} ${.CURDIR}/conf/${KFILE} && \
	eval "_arch=\"`make -V _arch -f $${TDIR}/Makefile`\"" && \
	eval "_mach=\"`make -V _mach -f $${TDIR}/Makefile`\"" && \
	eval "_machdir=\$S/arch/$${_mach}" && \
	eval "_archdir=\$S/arch/$${_arch}" && \
	eval "HFILES=\"`find $S \( -path $S/'arch' -o -path $S/stand -o -path $S/lib/libsa -o -path $S'/lib/libkern/arch' \) -prune -o -name '*.h'; find $${_machdir} $${_archdir} $S/lib/libkern/arch/$${_mach} \( -name boot -o -name stand \) -prune -o -name '*.h'`\"" && \
	eval "SFILES=\"`make -V SFILES -f $${TDIR}/Makefile`\"" && \
	eval "CFILES=\"`make -V CFILES -f $${TDIR}/Makefile`\"" && \
	eval "AFILES=\"`make -V AFILES -f $${TDIR}/Makefile`\"" && \
	ctags -wd -f ${TAGS} $${CFILES} $${HFILES} && \
	egrep "^[_A-Z]*ENTRY[_A-Z]*\(.*\)" $${SFILES} $${AFILES} | \
	    sed "s;\\([^:]*\\):\\([^(]*\\)(\\([^, )]*\\)\\(.*\\);\\3	\\1	/^\\2(\\3\\4$$/;" \
	    >> ${TAGS} && \
	sort -o ${TAGS} ${TAGS} && \
	rm -rf $${TDIR}

links:
	-for i in conf ${TDIRS}; do \
	    (cd $$i && rm -f tags; ln -s tags tags); done

obj:	_SUBDIRUSE

.include <bsd.prog.mk>

path: sys/arch/macppc/Makefile


code:
This is the macppc port, it supports most of the "New World" Apple Macintosh
machines.

TODO:
	* Sleep mode support
	  lower power while running.
	  suspend/hibernate support.
	* Legacy machine support
	  esp ethernet
	  - other legacy support.
	  Openfirmware 1.x (xcoff bootloader)

Dale
drahn@cvs.openbsd.org

path: sys/arch/macppc/README


code:
/.cvsignore/1.2/Wed Oct 10 15:57:07 2007//TOPENBSD_5_8_BASE
D

path: sys/arch/macppc/compile/CVS/Entries


code: src/sys/arch/macppc/compile

path: sys/arch/macppc/compile/CVS/Repository


code: src/sys/arch/macppc/conf

path: sys/arch/macppc/conf/CVS/Repository