Added boot process information to help someone find out what really happens.
[bootos.git] / stage2 / start_native.S
blob2f65a61a970571b110ff91fdaaba8eda5d5d3535
1 /*  start.S - AsbestOS stage2 startup code
3 Copyright (C) 2010-2011  Hector Martin "marcan" <hector@marcansoft.com>
5 This code is licensed to you under the terms of the GNU GPL, version 2;
6 see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
7 */
9 #include "../common/assembly.h"
10 #define LV1_PANIC 255
12         .text
13         .section .vectors,"ax",@progbits
14         .align 3
16         .global _entry
18 _zero:
19         .space 0x60
21 _thread1_entry:
22         b _start
23         .space 0x100 - (.-_zero)
25 _entry:
26         /* both threads end up running here */
28         /* calculate our base */
29         bl 1f
31         mflr r30
32         addi r30, r30, (_zero - 1b)
33         clrldi r30, r30, 1
35         /* if this is thread 1, go to _thread1_start */
36         mfspr r3, 0x88
37         cntlzw. r3, r3
38         bne _thread1_start
40         b _start
42         .space 0x1000 - (.-_zero)
44         .text
45         .section .start,"ax",@progbits
46         .align 3
48 _start:
50         /* relocate to our real address */
51         mr r4, r30
52         lis r5, __self_start@h
53         ori r5, r5, __self_start@l
54         lis r6, __self_end@h
55         ori r6, r6, __self_end@l
57         cmpld r4, r5
58         beq _no_reloc
60 _reloc_loop:
61         ld r8, 0(r4)
62         std r8, 0(r5)
63         ld r8, 8(r4)
64         std r8, 8(r5)
65         ld r8, 16(r4)
66         std r8, 16(r5)
67         ld r8, 24(r4)
68         std r8, 24(r5)
69         dcbst 0, r5
70         sync
71         icbi 0, r5
72         addi r4, r4, 0x20
73         addi r5, r5, 0x20
74         cmpld r5, r6
75         blt _reloc_loop
77         /* continue running at the relocated location */
78         lis r4, reloced_start@h
79         ori r4, r4, reloced_start@l
80         mtctr r4
81         bctr
83 reloced_start:
85         /* make sure _thread1_active is cleared on the new copy */
86         lis r3, _thread1_active@h
87         ori r3, r3, _thread1_active@l
88         li r0, 0
89         std r0, 0(r3)
91         /* now jump thread1 over to the new vector */
92         lis r4, __base@h
93         ori r4, r4, __base@l
94         sub r5, r3, r4
95         add r5, r5, r30
96         lis r3, _thread1_start@h
97         ori r3, r3, _thread1_start@l
98         std r3, 16(r5)
99         li r3, 1
100         std r3, 8(r5)
102 _no_reloc:
103         /* set up the TOC register */
104         lis r2, (__toc_start+0x8000)@h
105         ori r2, r2, (__toc_start+0x8000)@l
106         /* set up the stack */
107         lis r1, _stack_bot@h
108         ori r1, r1, _stack_bot@l
109         /* make space for the first stack frame */
110         addi r1, r1, 64
111         li r0, 0
112         std r0, 0(r1)
114         /* clear BSS */
115         lis r3, __bss_start@h
116         ori r3, r3, __bss_start@l
117         lis r4, __bss_end@h
118         ori r4, r4, __bss_end@l
119         li r5, 0
120 _bss_loop:
121         std r5, 0(r3)
122         addi r3, r3, 8
123         cmpld r3, r4
124         blt _bss_loop
126         /* jump to main (panic if it returns) */
127         bl main
128         b panic
130         /* thread 1 runs this code - spin until we have a vector */
131 _thread1_start:
132         lis r3, _thread1_active@h
133         ori r3, r3, _thread1_active@l
134         lis r4, __base@h
135         ori r4, r4, __base@l
136         sub r5, r3, r4
137         add r5, r5, r30
139         /* next time make sure we use the real base (if applicable) */
140         lis r30, __base@h
141         ori r30, r30, __base@l
143         li r4, 1
144         std r4, 0(r5)
146         ld r4, 8(r5)
147         cmpldi r4, 0
148         beq 1b
149         ld r4, 16(r5)
150         mtctr r4
151         li r3, 1 /* CPU number for Linux */
152         bctr
154         .align 3
155         .global _thread1_active, _thread1_release, _thread1_vector
156 _thread1_active:        .quad 0
157 _thread1_release:       .quad 0
158 _thread1_vector:        .quad 0
160 /* shutdown */
161         .global panic
162 panic:
163         li r3, 0
164         li r11, LV1_PANIC
165         lv1call
167 /* reboot */
168         .global reboot
169 reboot:
170         li r3, 1
171         li r11, LV1_PANIC
172         lv1call