Subject: port-mips/4277: NetBSD/mips endian probrem
To: None <gnats-bugs@gnats.netbsd.org>
From: None <tsubai@iri.co.jp>
List: netbsd-bugs
Date: 10/16/1997 17:42:17
>Number:         4277
>Category:       port-mips
>Synopsis:       NetBSD/mips endian probrem
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    gnats-admin (GNATS administrator)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Thu Oct 16 01:50:02 1997
>Last-Modified:
>Originator:     Tsubai Masanari
>Organization:
Internet Research Institute, Inc.
>Release:        1.2G
>Environment:
SONY NEWS3400
System: NetBSD wapiko 1.2G NetBSD 1.2G (WAPIKO) #1: Mon Aug 18 18:26:51 JST 1997 tsubai@wapiko:/a/0816/src/sys/arch/news/compile/WAPIKO news


>Description:
	I ported NetBSD to SONY NEWS3400 series.  It is based on NetBSD/mips
	and 4.4BSD Lite2.  Because NEWS3400 is BIG-ENDIAN, some modification
	is needed (locore.S, mips_opcode.h).
	Full kernel sources are
	ftp://elilin.hss.titech.ac.jp/pub/NetBSD/news/sys.news-970818.tar.gz
	(This is the newest ;-)
>How-To-Repeat:

>Fix:
	Please see following diff.


*** mips/locore.S.orig	Sat Aug  9 20:17:36 1997
--- mips/locore.S	Sun Aug 17 21:07:41 1997
***************
*** 95,100 ****
--- 95,127 ----
  #include <mips/pte.h>
  #include <machine/param.h>
  
+ 
+ /* #include <machine/endian.h> */
+ /* common endian.h includes function declarations */
+ #define	LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
+ #define	BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
+ #define	PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
+ 
+ #ifdef __MIPSEL__
+ # define BYTE_ORDER LITTLE_ENDIAN
+ #endif
+ #ifdef __MIPSEB__
+ # define BYTE_ORDER BIG_ENDIAN
+ #endif
+ 
+ #if BYTE_ORDER == LITTLE_ENDIAN
+ # define LWHI lwr
+ # define LWLO lwl
+ # define SWHI swr
+ # define SWLO swl
+ #endif
+ #if BYTE_ORDER == BIG_ENDIAN
+ # define LWHI lwl
+ # define LWLO lwr
+ # define SWHI swl
+ # define SWLO swr
+ #endif
+ 
  /*----------------------------------------------------------------------------
   *
   *  Macros used to save and restore registers when entering and
***************
*** 259,264 ****
--- 286,292 ----
   * netorder = htonl(hostorder)
   * hostorder = ntohl(netorder)
   */
+ #if BYTE_ORDER == LITTLE_ENDIAN
  LEAF(htonl)				# a0 = 0x11223344, return 0x44332211
  ALEAF(ntohl)
  	srl	v1, a0, 24		# v1 = 0x00000011
***************
*** 286,291 ****
--- 314,320 ----
  	j	ra
  	or	v0, v0, v1
  END(htons)
+ #endif
  
  /*
   * strlen(str)
***************
*** 335,341 ****
  	and	a3, a3, 3
  	beq	a3, zero, 1f		# skip if word aligned
  	subu	a1, a1, a3		# subtract from remaining count
! 	swr	zero, 0(a0)		# clear 1, 2, or 3 bytes to align
  	addu	a0, a0, a3
  1:
  	and	v0, a1, 3		# compute number of words left
--- 364,370 ----
  	and	a3, a3, 3
  	beq	a3, zero, 1f		# skip if word aligned
  	subu	a1, a1, a3		# subtract from remaining count
! 	SWHI	zero, 0(a0)		# clear 1, 2, or 3 bytes to align
  	addu	a0, a0, a3
  1:
  	and	v0, a1, 3		# compute number of words left
***************
*** 372,379 ****
  	beq	a3, zero, 1f
  	subu	a2, a2, a3		# subtract from remaining count
  	move	v0, v1			# init v0,v1 so unmodified bytes match
! 	lwr	v0, 0(a0)		# read 1, 2, or 3 bytes
! 	lwr	v1, 0(a1)
  	addu	a1, a1, a3
  	bne	v0, v1, nomatch
  	addu	a0, a0, a3
--- 401,408 ----
  	beq	a3, zero, 1f
  	subu	a2, a2, a3		# subtract from remaining count
  	move	v0, v1			# init v0,v1 so unmodified bytes match
! 	LWHI	v0, 0(a0)		# read 1, 2, or 3 bytes
! 	LWHI	v1, 0(a1)
  	addu	a1, a1, a3
  	bne	v0, v1, nomatch
  	addu	a0, a0, a3
***************
*** 408,415 ****
  	subu	a2, a2, a3		#   which has to be >= (16-3) & ~3
  	addu	a3, a3, a0		# compute ending address
  3:
! 	lwr	v0, 0(a0)		# compare words a0 unaligned, a1 aligned
! 	lwl	v0, 3(a0)
  	lw	v1, 0(a1)
  	addu	a0, a0, 4
  	bne	v0, v1, nomatch
--- 437,444 ----
  	subu	a2, a2, a3		#   which has to be >= (16-3) & ~3
  	addu	a3, a3, a0		# compute ending address
  3:
! 	LWHI	v0, 0(a0)		# compare words a0 unaligned, a1 aligned
! 	LWLO	v0, 3(a0)
  	lw	v1, 0(a1)
  	addu	a0, a0, 4
  	bne	v0, v1, nomatch
***************
*** 552,564 ****
  	addu	a3,a0,a3	# stop point
  
  3:
! #if MIPSEL	/* little-endian */
! 	lwr	v0,0(a0)
! 	lwl	v0,3(a0)
! #else		/* big-endian */
! 	lwl	v0,0(a0)
! 	lwr	v0,3(a0)
! #endif
  	addi	a0,4
  	sw	v0,0(a1)
  	bne	a0,a3,3b
--- 581,588 ----
  	addu	a3,a0,a3	# stop point
  
  3:
! 	LWHI	v0,0(a0)
! 	LWLO	v0,3(a0)
  	addi	a0,4
  	sw	v0,0(a1)
  	bne	a0,a3,3b
*** include/mips_opcode.h.orig	Mon Mar 25 21:24:19 1996
--- include/mips_opcode.h	Sun Jul 20 16:45:18 1997
***************
*** 81,86 ****
--- 81,118 ----
  		unsigned op: 6;		/* always '0x11' */
  	} FRType;
  #endif
+ #if BYTE_ORDER == BIG_ENDIAN
+ 	struct {
+ 		unsigned op: 6;
+ 		unsigned rs: 5;
+ 		unsigned rt: 5;
+ 		unsigned imm: 16;
+ 	} IType;
+ 
+ 	struct {
+ 		unsigned op: 6;
+ 		unsigned target: 26;
+ 	} JType;
+ 
+ 	struct {
+ 		unsigned op: 6;
+ 		unsigned rs: 5;
+ 		unsigned rt: 5;
+ 		unsigned rd: 5;
+ 		unsigned shamt: 5;
+ 		unsigned func: 6;
+ 	} RType;
+ 
+ 	struct {
+ 		unsigned op: 6;		/* always '0x11' */
+ 		unsigned : 1;		/* always '1' */
+ 		unsigned fmt: 4;
+ 		unsigned ft: 5;
+ 		unsigned fs: 5;
+ 		unsigned fd: 5;
+ 		unsigned func: 6;
+ 	} FRType;
+ #endif
  } InstFmt;
  
  /*
>Audit-Trail:
>Unformatted: