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