Rev 1787 | Rev 3222 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
308 | jermar | 1 | # |
2071 | jermar | 2 | # Copyright (c) 2003-2004 Jakub Jermar |
1 | jermar | 3 | # All rights reserved. |
4 | # |
||
5 | # Redistribution and use in source and binary forms, with or without |
||
6 | # modification, are permitted provided that the following conditions |
||
7 | # are met: |
||
8 | # |
||
9 | # - Redistributions of source code must retain the above copyright |
||
10 | # notice, this list of conditions and the following disclaimer. |
||
11 | # - Redistributions in binary form must reproduce the above copyright |
||
12 | # notice, this list of conditions and the following disclaimer in the |
||
13 | # documentation and/or other materials provided with the distribution. |
||
14 | # - The name of the author may not be used to endorse or promote products |
||
15 | # derived from this software without specific prior written permission. |
||
16 | # |
||
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | # |
||
28 | |||
306 | palkovsky | 29 | #include <arch/asm/regname.h> |
30 | #include <arch/mm/page.h> |
||
31 | #include <arch/asm/boot.h> |
||
317 | palkovsky | 32 | #include <arch/context_offset.h> |
306 | palkovsky | 33 | |
1 | jermar | 34 | .text |
35 | |||
36 | .set noat |
||
37 | .set noreorder |
||
38 | .set nomacro |
||
39 | |||
40 | .global kernel_image_start |
||
41 | .global tlb_refill_entry |
||
42 | .global cache_error_entry |
||
43 | .global exception_entry |
||
313 | palkovsky | 44 | .global userspace_asm |
1 | jermar | 45 | |
1096 | palkovsky | 46 | # Which status bits should are thread-local |
47 | #define REG_SAVE_MASK 0x1f # KSU(UM), EXL, ERL, IE |
||
48 | |||
313 | palkovsky | 49 | # Save registers to space defined by \r |
1096 | palkovsky | 50 | # We will change status: Disable ERL,EXL,UM,IE |
51 | # These changes will be automatically reversed in REGISTER_LOAD |
||
1102 | palkovsky | 52 | # SP is NOT saved as part of these registers |
1096 | palkovsky | 53 | .macro REGISTERS_STORE_AND_EXC_RESET r |
306 | palkovsky | 54 | sw $at,EOFFSET_AT(\r) |
55 | sw $v0,EOFFSET_V0(\r) |
||
56 | sw $v1,EOFFSET_V1(\r) |
||
57 | sw $a0,EOFFSET_A0(\r) |
||
58 | sw $a1,EOFFSET_A1(\r) |
||
59 | sw $a2,EOFFSET_A2(\r) |
||
60 | sw $a3,EOFFSET_A3(\r) |
||
317 | palkovsky | 61 | sw $t0,EOFFSET_T0(\r) |
306 | palkovsky | 62 | sw $t1,EOFFSET_T1(\r) |
63 | sw $t2,EOFFSET_T2(\r) |
||
64 | sw $t3,EOFFSET_T3(\r) |
||
65 | sw $t4,EOFFSET_T4(\r) |
||
66 | sw $t5,EOFFSET_T5(\r) |
||
67 | sw $t6,EOFFSET_T6(\r) |
||
68 | sw $t7,EOFFSET_T7(\r) |
||
69 | sw $t8,EOFFSET_T8(\r) |
||
70 | sw $t9,EOFFSET_T9(\r) |
||
313 | palkovsky | 71 | |
72 | mflo $at |
||
73 | sw $at, EOFFSET_LO(\r) |
||
74 | mfhi $at |
||
75 | sw $at, EOFFSET_HI(\r) |
||
76 | |||
1094 | palkovsky | 77 | #ifdef CONFIG_DEBUG_ALLREGS |
306 | palkovsky | 78 | sw $s0,EOFFSET_S0(\r) |
79 | sw $s1,EOFFSET_S1(\r) |
||
80 | sw $s2,EOFFSET_S2(\r) |
||
81 | sw $s3,EOFFSET_S3(\r) |
||
82 | sw $s4,EOFFSET_S4(\r) |
||
83 | sw $s5,EOFFSET_S5(\r) |
||
84 | sw $s6,EOFFSET_S6(\r) |
||
85 | sw $s7,EOFFSET_S7(\r) |
||
1094 | palkovsky | 86 | sw $s8,EOFFSET_S8(\r) |
87 | #endif |
||
88 | |||
306 | palkovsky | 89 | sw $gp,EOFFSET_GP(\r) |
90 | sw $ra,EOFFSET_RA(\r) |
||
1102 | palkovsky | 91 | sw $k1,EOFFSET_K1(\r) |
313 | palkovsky | 92 | |
1096 | palkovsky | 93 | mfc0 $t0, $status |
94 | mfc0 $t1, $epc |
||
95 | |||
96 | and $t2, $t0, REG_SAVE_MASK # Save only KSU,EXL,ERL,IE |
||
97 | li $t3, ~(0x1f) |
||
98 | and $t0, $t0, $t3 # Clear KSU,EXL,ERL,IE |
||
99 | |||
100 | sw $t2,EOFFSET_STATUS(\r) |
||
101 | sw $t1,EOFFSET_EPC(\r) |
||
102 | mtc0 $t0, $status |
||
306 | palkovsky | 103 | .endm |
104 | |||
105 | .macro REGISTERS_LOAD r |
||
1096 | palkovsky | 106 | # Update only UM,EXR,IE from status, the rest |
107 | # is controlled by OS and not bound to task |
||
108 | mfc0 $t0, $status |
||
109 | lw $t1,EOFFSET_STATUS(\r) |
||
110 | |||
111 | li $t2, ~REG_SAVE_MASK # Mask UM,EXL,ERL,IE |
||
112 | and $t0, $t0, $t2 |
||
113 | |||
114 | or $t0, $t0, $t1 # Copy UM,EXL,ERL,IE from saved status |
||
115 | mtc0 $t0, $status |
||
116 | |||
306 | palkovsky | 117 | lw $v0,EOFFSET_V0(\r) |
118 | lw $v1,EOFFSET_V1(\r) |
||
119 | lw $a0,EOFFSET_A0(\r) |
||
120 | lw $a1,EOFFSET_A1(\r) |
||
121 | lw $a2,EOFFSET_A2(\r) |
||
122 | lw $a3,EOFFSET_A3(\r) |
||
317 | palkovsky | 123 | lw $t0,EOFFSET_T0(\r) |
306 | palkovsky | 124 | lw $t1,EOFFSET_T1(\r) |
125 | lw $t2,EOFFSET_T2(\r) |
||
126 | lw $t3,EOFFSET_T3(\r) |
||
127 | lw $t4,EOFFSET_T4(\r) |
||
128 | lw $t5,EOFFSET_T5(\r) |
||
129 | lw $t6,EOFFSET_T6(\r) |
||
130 | lw $t7,EOFFSET_T7(\r) |
||
131 | lw $t8,EOFFSET_T8(\r) |
||
132 | lw $t9,EOFFSET_T9(\r) |
||
1094 | palkovsky | 133 | |
134 | #ifdef CONFIG_DEBUG_ALLREGS |
||
306 | palkovsky | 135 | lw $s0,EOFFSET_S0(\r) |
136 | lw $s1,EOFFSET_S1(\r) |
||
137 | lw $s2,EOFFSET_S2(\r) |
||
138 | lw $s3,EOFFSET_S3(\r) |
||
139 | lw $s4,EOFFSET_S4(\r) |
||
140 | lw $s5,EOFFSET_S5(\r) |
||
141 | lw $s6,EOFFSET_S6(\r) |
||
142 | lw $s7,EOFFSET_S7(\r) |
||
143 | lw $s8,EOFFSET_S8(\r) |
||
1094 | palkovsky | 144 | #endif |
306 | palkovsky | 145 | lw $gp,EOFFSET_GP(\r) |
146 | lw $ra,EOFFSET_RA(\r) |
||
1102 | palkovsky | 147 | lw $k1,EOFFSET_K1(\r) |
306 | palkovsky | 148 | |
313 | palkovsky | 149 | lw $at,EOFFSET_LO(\r) |
150 | mtlo $at |
||
151 | lw $at,EOFFSET_HI(\r) |
||
152 | mthi $at |
||
153 | |||
317 | palkovsky | 154 | lw $at,EOFFSET_EPC(\r) |
155 | mtc0 $at, $epc |
||
313 | palkovsky | 156 | |
157 | lw $at,EOFFSET_AT(\r) |
||
158 | lw $sp,EOFFSET_SP(\r) |
||
306 | palkovsky | 159 | .endm |
160 | |||
313 | palkovsky | 161 | # Move kernel stack pointer address to register K0 |
162 | # - if we are in user mode, load the appropriate stack |
||
163 | # address |
||
164 | .macro KERNEL_STACK_TO_K0 |
||
165 | # If we are in user mode |
||
166 | mfc0 $k0, $status |
||
167 | andi $k0, 0x10 |
||
306 | palkovsky | 168 | |
313 | palkovsky | 169 | beq $k0, $0, 1f |
170 | add $k0, $sp, 0 |
||
171 | |||
172 | # Move $k0 pointer to kernel stack |
||
173 | lui $k0, %hi(supervisor_sp) |
||
320 | palkovsky | 174 | ori $k0, $k0, %lo(supervisor_sp) |
313 | palkovsky | 175 | # Move $k0 (superveisor_sp) |
176 | lw $k0, 0($k0) |
||
177 | 1: |
||
178 | .endm |
||
179 | |||
1 | jermar | 180 | .org 0x0 |
306 | palkovsky | 181 | kernel_image_start: |
182 | /* Load temporary stack */ |
||
313 | palkovsky | 183 | lui $sp, %hi(end_stack) |
320 | palkovsky | 184 | ori $sp, $sp, %lo(end_stack) |
1686 | decky | 185 | |
186 | /* $a1 contains physical address of bootinfo_t */ |
||
187 | /* $a2 contains size of bootinfo_t */ |
||
188 | |||
189 | beq $a2, $0, bootinfo_end |
||
190 | |||
306 | palkovsky | 191 | /* Not sure about this, but might be needed for PIC code???? */ |
192 | lui $gp, 0x8000 |
||
193 | |||
1686 | decky | 194 | lui $a3, %hi(bootinfo) |
195 | ori $a3, $a3, %lo(bootinfo) |
||
196 | |||
197 | bootinfo_loop: |
||
198 | |||
199 | lw $v0, 0($a1) |
||
200 | sw $v0, 0($a3) |
||
201 | |||
202 | addi $a1, $a1, 4 |
||
203 | addi $a3, $a3, 4 |
||
204 | addi $a2, $a2, -4 |
||
205 | |||
206 | bgtz $a2, bootinfo_loop |
||
207 | nop |
||
208 | |||
209 | bootinfo_end: |
||
210 | |||
1222 | decky | 211 | jal arch_pre_main |
306 | palkovsky | 212 | nop |
1222 | decky | 213 | |
214 | j main_bsp |
||
215 | nop |
||
306 | palkovsky | 216 | |
217 | .space TEMP_STACK_SIZE |
||
326 | palkovsky | 218 | end_stack: |
219 | |||
220 | tlb_refill_entry: |
||
221 | j tlb_refill_handler |
||
222 | nop |
||
223 | |||
224 | cache_error_entry: |
||
225 | j cache_error_handler |
||
226 | nop |
||
227 | |||
228 | exception_entry: |
||
229 | j exception_handler |
||
230 | nop |
||
231 | |||
306 | palkovsky | 232 | |
326 | palkovsky | 233 | |
313 | palkovsky | 234 | exception_handler: |
235 | KERNEL_STACK_TO_K0 |
||
236 | sub $k0, REGISTER_SPACE |
||
1102 | palkovsky | 237 | sw $sp,EOFFSET_SP($k0) |
238 | move $sp, $k0 |
||
1096 | palkovsky | 239 | |
1102 | palkovsky | 240 | mfc0 $k0, $cause |
1096 | palkovsky | 241 | |
1102 | palkovsky | 242 | sra $k0, $k0, 0x2 # cp0_exc_cause() part 1 |
243 | andi $k0, $k0, 0x1f # cp0_exc_cause() part 2 |
||
244 | sub $k0, 8 # 8=SYSCALL |
||
1096 | palkovsky | 245 | |
1102 | palkovsky | 246 | beqz $k0, syscall_shortcut |
1386 | palkovsky | 247 | add $k0, 8 # Revert $k0 back to correct exc number |
1096 | palkovsky | 248 | |
1102 | palkovsky | 249 | REGISTERS_STORE_AND_EXC_RESET $sp |
250 | |||
1096 | palkovsky | 251 | move $a1, $sp |
252 | jal exc_dispatch # exc_dispatch(excno, register_space) |
||
1102 | palkovsky | 253 | move $a0, $k0 |
313 | palkovsky | 254 | |
255 | REGISTERS_LOAD $sp |
||
256 | # The $sp is automatically restored to former value |
||
257 | eret |
||
1096 | palkovsky | 258 | |
259 | # it seems that mips reserves some space on stack for varfuncs??? |
||
260 | #define SS_ARG4 16 |
||
1102 | palkovsky | 261 | #define SS_SP EOFFSET_SP |
262 | #define SS_STATUS EOFFSET_STATUS |
||
263 | #define SS_EPC EOFFSET_EPC |
||
1386 | palkovsky | 264 | #define SS_K1 EOFFSET_K1 |
1100 | palkovsky | 265 | syscall_shortcut: |
1096 | palkovsky | 266 | # We have a lot of space on the stack, with free use |
267 | mfc0 $t1, $epc |
||
268 | mfc0 $t0, $status |
||
269 | sw $t1,SS_EPC($sp) # Save EPC |
||
1386 | palkovsky | 270 | sw $k1,SS_K1($sp) # Save k1, which is not saved during context switch |
313 | palkovsky | 271 | |
1096 | palkovsky | 272 | and $t2, $t0, REG_SAVE_MASK # Save only KSU,EXL,ERL,IE |
273 | li $t3, ~(0x1f) |
||
274 | and $t0, $t0, $t3 # Clear KSU,EXL,ERL |
||
275 | ori $t0, $t0, 0x1 # Set IE |
||
276 | |||
277 | sw $t2,SS_STATUS($sp) |
||
278 | mtc0 $t0, $status |
||
279 | |||
1126 | palkovsky | 280 | # CALL Syscall handler |
1096 | palkovsky | 281 | jal syscall_handler |
282 | sw $v0, SS_ARG4($sp) # save v0 - arg4 to stack |
||
283 | |||
284 | # restore status |
||
285 | mfc0 $t0, $status |
||
286 | lw $t1,SS_STATUS($sp) |
||
287 | |||
1126 | palkovsky | 288 | # Change back to EXL=1(from last exception), otherwise |
289 | # an interrupt could rewrite the CP0-EPC |
||
1096 | palkovsky | 290 | li $t2, ~REG_SAVE_MASK # Mask UM,EXL,ERL,IE |
291 | and $t0, $t0, $t2 |
||
292 | or $t0, $t0, $t1 # Copy UM,EXL,ERL,IE from saved status |
||
293 | mtc0 $t0, $status |
||
294 | |||
1126 | palkovsky | 295 | # restore epc+4 |
296 | lw $t0,SS_EPC($sp) |
||
1386 | palkovsky | 297 | lw $k1,SS_K1($sp) |
1126 | palkovsky | 298 | addi $t0, $t0, 4 |
299 | mtc0 $t0, $epc |
||
300 | |||
1096 | palkovsky | 301 | lw $sp,SS_SP($sp) # restore sp |
302 | |||
303 | eret |
||
1278 | palkovsky | 304 | |
1 | jermar | 305 | tlb_refill_handler: |
313 | palkovsky | 306 | KERNEL_STACK_TO_K0 |
307 | sub $k0, REGISTER_SPACE |
||
1096 | palkovsky | 308 | REGISTERS_STORE_AND_EXC_RESET $k0 |
1102 | palkovsky | 309 | sw $sp,EOFFSET_SP($k0) |
313 | palkovsky | 310 | add $sp, $k0, 0 |
125 | jermar | 311 | |
1769 | jermar | 312 | jal tlb_refill |
317 | palkovsky | 313 | add $a0, $sp, 0 |
125 | jermar | 314 | |
306 | palkovsky | 315 | REGISTERS_LOAD $sp |
125 | jermar | 316 | |
50 | jermar | 317 | eret |
1 | jermar | 318 | |
319 | cache_error_handler: |
||
313 | palkovsky | 320 | KERNEL_STACK_TO_K0 |
1102 | palkovsky | 321 | sub $k0, REGISTER_SPACE |
322 | REGISTERS_STORE_AND_EXC_RESET $k0 |
||
323 | sw $sp,EOFFSET_SP($k0) |
||
313 | palkovsky | 324 | add $sp, $k0, 0 |
1 | jermar | 325 | |
50 | jermar | 326 | jal cache_error |
1769 | jermar | 327 | add $a0, $sp, 0 |
125 | jermar | 328 | |
306 | palkovsky | 329 | REGISTERS_LOAD $sp |
1 | jermar | 330 | |
50 | jermar | 331 | eret |
313 | palkovsky | 332 | |
333 | userspace_asm: |
||
334 | add $sp, $a0, 0 |
||
1122 | palkovsky | 335 | add $v0, $a1, 0 |
336 | add $t9, $a2, 0 # Set up correct entry into PIC code |
||
313 | palkovsky | 337 | eret |