Home | History | Annotate | Line # | Download | only in libsa
srt0.S revision 1.1.126.1
      1 /*	$NetBSD: srt0.S,v 1.1.126.1 2008/05/16 02:22:56 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Steve C. Woodford.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <machine/asm.h>
     33 #include <machine/psl.h>
     34 #include <powerpc/spr.h>
     35 
     36 #define	STACK_SIZE	8192
     37 
     38 /*
     39  * The main entry point when loaded by PPC-Bug.
     40  *
     41  * There are two possible entry conditions here:
     42  *
     43  *   1) We were booted in `PReP' mode, either from disk or the network.
     44  *      In this case, we have no control over the load address so we
     45  *      have to relocate ourselves to the appropriate place.
     46  *      The firmware passes us the following registers:
     47  *
     48  *        r1  -> Temporary stack
     49  *        r3  -> Residual Data
     50  *        r4  -> The address we were loaded to
     51  *        r5  -> Zero
     52  *
     53  *   2) We were booted over the network in Non-PReP mode. In this case,
     54  *      the load address is usually set using PPC-Bug's "niot" command,
     55  *      but we won't depend on it so relocation may be required. The
     56  *      firmware passes us the following registers:
     57  *
     58  *        r1  -> Temporary stack
     59  *        r3  -> CLUN of the network device we booted from
     60  *        r4  -> DLUN of the network device we booted from
     61  *        r5  -> Non-zero
     62  *        r6  -> Base address of network device
     63  *        r7  -> Execution address of loaded program
     64  *        r8  -> Address of IP-address data structure
     65  *        r9  -> Pointer to start of filename string
     66  *        r10 -> Pointer to end+1 of filename string
     67  *        r11 -> Pointer to start of argument string
     68  *        r12 -> Pointer to end+1 of argument string
     69  *
     70  * The obvious way to distinguish between the two boot modes is by
     71  * checking the value of r5.
     72  */
     73 ENTRY(_start)
     74 	bl	1f
     75 1:	xor	r0,r0,r0
     76 
     77 	/* First, switch off Instruction and Data caches. */
     78 	mfspr	r13,SPR_HID0
     79 	LDCONST(r14, HID0_DCE|HID0_ICE)
     80 	andc	r13,r13,r14
     81 	sync
     82 	mtspr	SPR_HID0,r13
     83 
     84 
     85 	/*
     86 	 * All registers now available. Let's see if we need to relocate
     87 	 */
     88 	LDCONST(r13,_C_LABEL(_start))	/* Where we'd like to be */
     89 	LDCONST(r14,_C_LABEL(edata))	/* End of data section */
     90 	LDCONST(r15,0x3)
     91 	add	r14,r14,r15
     92 	andc	r14,r14,r15		/* Rounded up to the nearest 32-bits */
     93 	sub	r15,r14,r13		/* Our size, in bytes */
     94 	mflr	r16			/* Get address we were loaded to */
     95 	subi	r16,r16,0x4		/* Correct for branch */
     96 	cmp	cr0,r13,r16		/* Do we need to relocate? */
     97 	beq	_ASM_LABEL(clrbss)	/* No relocation necessary */
     98 	li	r17,0x4
     99 	bgt	1f			/* Relocate using forward copy? */
    100 
    101 	/* Nope. Need to copy in reverse in case of overlap */
    102 	mr	r13,r14			/* dest -> end */
    103 	add	r16,r16,r15		/* src + size */
    104 	subi	r17,r17,0x8		/* Increment is -4 */
    105 
    106 	/*
    107 	 * Do the relocation
    108 	 *  r13  -> dest
    109 	 *  r15  -> number of bytes
    110 	 *  r16  -> src
    111 	 *  r17  -> Increment (+4 or -4)
    112 	 */
    113 1:	srwi	r15,r15,0x2		/* Convert length to 32-bit words */
    114 	mtctr	r15			/* Save in counter register */
    115 
    116 2:	lwz	r15,0(r16)
    117 	stw	r15,0(r13)
    118 	add	r16,r16,r17
    119 	add	r13,r13,r17
    120 	bdnz	2b
    121 
    122 	/* Now do an absolute jump to the relocated code */
    123 	LDCONST(r13,_ASM_LABEL(clrbss))
    124 	mtlr	r13
    125 	blr
    126 
    127 ASENTRY(clrbss)
    128 	LDCONST(r13,_C_LABEL(edata))	/* End of the data section */
    129 	LDCONST(r14,_C_LABEL(end))	/* End of BSS */
    130 	LDCONST(r15,0x3)
    131 	add	r14,r14,r15
    132 	andc	r14,r14,r15		/* Round-up end of BSS to 32-bits */
    133 	sub	r15,r14,r13		/* r15 == length of BSS */
    134 	srwi	r15,r15,0x2
    135 	mtctr	r15			/* CTR == # of 32-bit words in BSS */
    136 1:	stw	r0,0(r13)		/* Clear BSS */
    137 	addi	r13,r13,4
    138 	bdnz	1b
    139 
    140 	/* Fix up our own stack */
    141 	LDCONST(r1,stack)
    142 	addi	r1,r1,STACK_SIZE-0x10
    143 	LDCONST(r13,0x0f)
    144 	andc	r1,r1,r13
    145 
    146 	/*
    147 	 * Copy the arguments passed in from Bug into bug_bootinfo
    148 	 *
    149 	 * See bugsyscalls.h for details.
    150 	 */
    151 	LDCONST(r13,_C_LABEL(bug_bootinfo))
    152 	stw	r5,0x00(r13)
    153 	stw	r3,0x04(r13)
    154 	stw	r4,0x08(r13)
    155 	stw	r6,0x0c(r13)
    156 	stw	r7,0x10(r13)
    157 	stw	r8,0x14(r13)
    158 	stw	r9,0x18(r13)
    159 	stw	r10,0x1c(r13)
    160 	stw	r11,0x20(r13)
    161 	stw	r12,0x24(r13)
    162 
    163 	mr	r3,r13
    164 	bl	_C_LABEL(main)		/* void main(void) */
    165 	/* FALLTHROUGH */
    166 
    167 /*
    168  * Return to the debugger, either because main() returned or via panic().
    169  */
    170 ENTRY(_rtt)
    171 	addi	r10,0,0x0063
    172 	sc
    173 1:	nop
    174 	b	1b
    175 
    176 	/*
    177 	 * C code runs on this stack.
    178 	 */
    179 	.comm	stack,STACK_SIZE,4
    180 	.comm	errno,4,4
    181 	.comm	debug,4,4
    182