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