1 | #
|
---|
2 | # Copyright (c) 2003-2004 Jakub 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 |
|
---|
29 | #include <arch/asm/regname.h>
|
---|
30 | #include <arch/mm/page.h>
|
---|
31 | #include <arch/asm/boot.h>
|
---|
32 | #include <arch/context_offset.h>
|
---|
33 | #include <arch/stack.h>
|
---|
34 |
|
---|
35 | .text
|
---|
36 |
|
---|
37 | .set noat
|
---|
38 | .set noreorder
|
---|
39 | .set nomacro
|
---|
40 |
|
---|
41 | .global kernel_image_start
|
---|
42 | .global tlb_refill_entry
|
---|
43 | .global cache_error_entry
|
---|
44 | .global exception_entry
|
---|
45 | .global userspace_asm
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Which status bits should are thread-local:
|
---|
49 | * KSU(UM), EXL, ERL, IE
|
---|
50 | */
|
---|
51 | #define REG_SAVE_MASK 0x1f
|
---|
52 |
|
---|
53 | #define ISTATE_OFFSET_A0 0
|
---|
54 | #define ISTATE_OFFSET_A1 4
|
---|
55 | #define ISTATE_OFFSET_A2 8
|
---|
56 | #define ISTATE_OFFSET_A3 12
|
---|
57 | #define ISTATE_OFFSET_T0 16
|
---|
58 | #define ISTATE_OFFSET_T1 20
|
---|
59 | #define ISTATE_OFFSET_V0 24
|
---|
60 | #define ISTATE_OFFSET_V1 28
|
---|
61 | #define ISTATE_OFFSET_AT 32
|
---|
62 | #define ISTATE_OFFSET_T2 36
|
---|
63 | #define ISTATE_OFFSET_T3 40
|
---|
64 | #define ISTATE_OFFSET_T4 44
|
---|
65 | #define ISTATE_OFFSET_T5 48
|
---|
66 | #define ISTATE_OFFSET_T6 52
|
---|
67 | #define ISTATE_OFFSET_T7 56
|
---|
68 | #define ISTATE_OFFSET_S0 60
|
---|
69 | #define ISTATE_OFFSET_S1 64
|
---|
70 | #define ISTATE_OFFSET_S2 68
|
---|
71 | #define ISTATE_OFFSET_S3 72
|
---|
72 | #define ISTATE_OFFSET_S4 76
|
---|
73 | #define ISTATE_OFFSET_S5 80
|
---|
74 | #define ISTATE_OFFSET_S6 84
|
---|
75 | #define ISTATE_OFFSET_S7 88
|
---|
76 | #define ISTATE_OFFSET_T8 92
|
---|
77 | #define ISTATE_OFFSET_T9 96
|
---|
78 | #define ISTATE_OFFSET_KT0 100
|
---|
79 | #define ISTATE_OFFSET_KT1 104
|
---|
80 | #define ISTATE_OFFSET_GP 108
|
---|
81 | #define ISTATE_OFFSET_SP 112
|
---|
82 | #define ISTATE_OFFSET_S8 116
|
---|
83 | #define ISTATE_OFFSET_RA 120
|
---|
84 | #define ISTATE_OFFSET_LO 124
|
---|
85 | #define ISTATE_OFFSET_HI 128
|
---|
86 | #define ISTATE_OFFSET_STATUS 132
|
---|
87 | #define ISTATE_OFFSET_EPC 136
|
---|
88 | #define ISTATE_OFFSET_ALIGNMENT 140
|
---|
89 |
|
---|
90 | #define ISTATE_SOFT_SIZE 144
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Save registers to space defined by \r
|
---|
94 | * We will change status: Disable ERL, EXL, UM, IE
|
---|
95 | * These changes will be automatically reversed in REGISTER_LOAD
|
---|
96 | * %sp is NOT saved as part of these registers
|
---|
97 | */
|
---|
98 | .macro REGISTERS_STORE_AND_EXC_RESET r
|
---|
99 | sw $at, EOFFSET_AT(\r)
|
---|
100 | sw $v0, EOFFSET_V0(\r)
|
---|
101 | sw $v1, EOFFSET_V1(\r)
|
---|
102 | sw $a0, EOFFSET_A0(\r)
|
---|
103 | sw $a1, EOFFSET_A1(\r)
|
---|
104 | sw $a2, EOFFSET_A2(\r)
|
---|
105 | sw $a3, EOFFSET_A3(\r)
|
---|
106 | sw $t0, EOFFSET_T0(\r)
|
---|
107 | sw $t1, EOFFSET_T1(\r)
|
---|
108 | sw $t2, EOFFSET_T2(\r)
|
---|
109 | sw $t3, EOFFSET_T3(\r)
|
---|
110 | sw $t4, EOFFSET_T4(\r)
|
---|
111 | sw $t5, EOFFSET_T5(\r)
|
---|
112 | sw $t6, EOFFSET_T6(\r)
|
---|
113 | sw $t7, EOFFSET_T7(\r)
|
---|
114 | sw $t8, EOFFSET_T8(\r)
|
---|
115 | sw $t9, EOFFSET_T9(\r)
|
---|
116 |
|
---|
117 | mflo $at
|
---|
118 | sw $at, EOFFSET_LO(\r)
|
---|
119 | mfhi $at
|
---|
120 | sw $at, EOFFSET_HI(\r)
|
---|
121 |
|
---|
122 | sw $gp, EOFFSET_GP(\r)
|
---|
123 | sw $ra, EOFFSET_RA(\r)
|
---|
124 | sw $k1, EOFFSET_K1(\r)
|
---|
125 |
|
---|
126 | mfc0 $t0, $status
|
---|
127 | mfc0 $t1, $epc
|
---|
128 |
|
---|
129 | /* save only KSU, EXL, ERL, IE */
|
---|
130 | and $t2, $t0, REG_SAVE_MASK
|
---|
131 |
|
---|
132 | /* clear KSU, EXL, ERL, IE */
|
---|
133 | li $t3, ~(REG_SAVE_MASK)
|
---|
134 | and $t0, $t0, $t3
|
---|
135 |
|
---|
136 | sw $t2, EOFFSET_STATUS(\r)
|
---|
137 | sw $t1, EOFFSET_EPC(\r)
|
---|
138 | mtc0 $t0, $status
|
---|
139 | .endm
|
---|
140 |
|
---|
141 | .macro REGISTERS_LOAD r
|
---|
142 | /*
|
---|
143 | * Update only UM, EXR, IE from status, the rest
|
---|
144 | * is controlled by OS and not bound to task.
|
---|
145 | */
|
---|
146 | mfc0 $t0, $status
|
---|
147 | lw $t1,EOFFSET_STATUS(\r)
|
---|
148 |
|
---|
149 | /* mask UM, EXL, ERL, IE */
|
---|
150 | li $t2, ~REG_SAVE_MASK
|
---|
151 | and $t0, $t0, $t2
|
---|
152 |
|
---|
153 | /* copy UM, EXL, ERL, IE from saved status */
|
---|
154 | or $t0, $t0, $t1
|
---|
155 | mtc0 $t0, $status
|
---|
156 |
|
---|
157 | lw $v0, EOFFSET_V0(\r)
|
---|
158 | lw $v1, EOFFSET_V1(\r)
|
---|
159 | lw $a0, EOFFSET_A0(\r)
|
---|
160 | lw $a1, EOFFSET_A1(\r)
|
---|
161 | lw $a2, EOFFSET_A2(\r)
|
---|
162 | lw $a3, EOFFSET_A3(\r)
|
---|
163 | lw $t0, EOFFSET_T0(\r)
|
---|
164 | lw $t1, EOFFSET_T1(\r)
|
---|
165 | lw $t2, EOFFSET_T2(\r)
|
---|
166 | lw $t3, EOFFSET_T3(\r)
|
---|
167 | lw $t4, EOFFSET_T4(\r)
|
---|
168 | lw $t5, EOFFSET_T5(\r)
|
---|
169 | lw $t6, EOFFSET_T6(\r)
|
---|
170 | lw $t7, EOFFSET_T7(\r)
|
---|
171 | lw $t8, EOFFSET_T8(\r)
|
---|
172 | lw $t9, EOFFSET_T9(\r)
|
---|
173 |
|
---|
174 | lw $gp, EOFFSET_GP(\r)
|
---|
175 | lw $ra, EOFFSET_RA(\r)
|
---|
176 | lw $k1, EOFFSET_K1(\r)
|
---|
177 |
|
---|
178 | lw $at, EOFFSET_LO(\r)
|
---|
179 | mtlo $at
|
---|
180 | lw $at, EOFFSET_HI(\r)
|
---|
181 | mthi $at
|
---|
182 |
|
---|
183 | lw $at, EOFFSET_EPC(\r)
|
---|
184 | mtc0 $at, $epc
|
---|
185 |
|
---|
186 | lw $at, EOFFSET_AT(\r)
|
---|
187 | lw $sp, EOFFSET_SP(\r)
|
---|
188 | .endm
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Move kernel stack pointer address to register $k0.
|
---|
192 | * If we are in user mode, load the appropriate stack address.
|
---|
193 | */
|
---|
194 | .macro KERNEL_STACK_TO_K0
|
---|
195 | /* if we are in user mode */
|
---|
196 | mfc0 $k0, $status
|
---|
197 | andi $k0, 0x10
|
---|
198 |
|
---|
199 | beq $k0, $0, 1f
|
---|
200 | add $k0, $sp, 0
|
---|
201 |
|
---|
202 | /* move $k0 pointer to kernel stack */
|
---|
203 | lui $k0, %hi(supervisor_sp)
|
---|
204 | ori $k0, $k0, %lo(supervisor_sp)
|
---|
205 |
|
---|
206 | /* move $k0 (supervisor_sp) */
|
---|
207 | lw $k0, 0($k0)
|
---|
208 |
|
---|
209 | 1:
|
---|
210 | .endm
|
---|
211 |
|
---|
212 | .org 0x0
|
---|
213 | kernel_image_start:
|
---|
214 | /* load temporary stack */
|
---|
215 | lui $sp, %hi(end_stack)
|
---|
216 | ori $sp, $sp, %lo(end_stack)
|
---|
217 |
|
---|
218 | /* not sure about this, but might be needed for PIC code */
|
---|
219 | lui $gp, 0x8000
|
---|
220 |
|
---|
221 | /* $a1 contains physical address of bootinfo_t */
|
---|
222 | jal arch_pre_main
|
---|
223 | nop
|
---|
224 |
|
---|
225 | j main_bsp
|
---|
226 | nop
|
---|
227 |
|
---|
228 | .space TEMP_STACK_SIZE
|
---|
229 | end_stack:
|
---|
230 |
|
---|
231 | tlb_refill_entry:
|
---|
232 | j tlb_refill_handler
|
---|
233 | nop
|
---|
234 |
|
---|
235 | cache_error_entry:
|
---|
236 | j cache_error_handler
|
---|
237 | nop
|
---|
238 |
|
---|
239 | exception_entry:
|
---|
240 | j exception_handler
|
---|
241 | nop
|
---|
242 |
|
---|
243 | exception_handler:
|
---|
244 | KERNEL_STACK_TO_K0
|
---|
245 |
|
---|
246 | sub $k0, ISTATE_SOFT_SIZE
|
---|
247 | sw $sp, ISTATE_OFFSET_SP($k0)
|
---|
248 | move $sp, $k0
|
---|
249 |
|
---|
250 | mfc0 $k0, $cause
|
---|
251 |
|
---|
252 | sra $k0, $k0, 0x2 /* cp0_exc_cause() part 1 */
|
---|
253 | andi $k0, $k0, 0x1f /* cp0_exc_cause() part 2 */
|
---|
254 | sub $k0, 8 /* 8 = SYSCALL */
|
---|
255 |
|
---|
256 | beqz $k0, syscall_shortcut
|
---|
257 | add $k0, 8 /* revert $k0 back to correct exc number */
|
---|
258 |
|
---|
259 | REGISTERS_STORE_AND_EXC_RESET $sp
|
---|
260 |
|
---|
261 | move $a1, $sp
|
---|
262 | jal exc_dispatch /* exc_dispatch(excno, register_space) */
|
---|
263 | move $a0, $k0
|
---|
264 |
|
---|
265 | REGISTERS_LOAD $sp
|
---|
266 | /* the $sp is automatically restored to former value */
|
---|
267 | eret
|
---|
268 |
|
---|
269 | #define SS_SP EOFFSET_SP
|
---|
270 | #define SS_STATUS EOFFSET_STATUS
|
---|
271 | #define SS_EPC EOFFSET_EPC
|
---|
272 | #define SS_K1 EOFFSET_K1
|
---|
273 |
|
---|
274 | /** Syscall entry
|
---|
275 | *
|
---|
276 | * Registers:
|
---|
277 | *
|
---|
278 | * @param $v0 Syscall number.
|
---|
279 | * @param $a0 1st argument.
|
---|
280 | * @param $a1 2nd argument.
|
---|
281 | * @param $a2 3rd argument.
|
---|
282 | * @param $a3 4th argument.
|
---|
283 | * @param $t0 5th argument.
|
---|
284 | * @param $t1 6th argument.
|
---|
285 | *
|
---|
286 | * @return The return value will be stored in $v0.
|
---|
287 | *
|
---|
288 | */
|
---|
289 | syscall_shortcut:
|
---|
290 | /* we have a lot of space on the stack, with free use */
|
---|
291 | mfc0 $t3, $epc
|
---|
292 | mfc0 $t2, $status
|
---|
293 | sw $t3, SS_EPC($sp) /* save EPC */
|
---|
294 | sw $k1, SS_K1($sp) /* save $k1 not saved on context switch */
|
---|
295 |
|
---|
296 | and $t4, $t2, REG_SAVE_MASK /* save only KSU, EXL, ERL, IE */
|
---|
297 | li $t5, ~(0x1f)
|
---|
298 | and $t2, $t2, $t5 /* clear KSU, EXL, ERL */
|
---|
299 | ori $t2, $t2, 0x1 /* set IE */
|
---|
300 |
|
---|
301 | sw $t4, SS_STATUS($sp)
|
---|
302 | mtc0 $t2, $status
|
---|
303 |
|
---|
304 | /*
|
---|
305 | * Call the higher level system call handler.
|
---|
306 | * We are going to reuse part of the unused exception stack frame.
|
---|
307 | *
|
---|
308 | */
|
---|
309 | sw $t0, STACK_ARG4($sp) /* save the 5th argument on the stack */
|
---|
310 | sw $t1, STACK_ARG5($sp) /* save the 6th argument on the stack */
|
---|
311 | jal syscall_handler
|
---|
312 | sw $v0, STACK_ARG6($sp) /* save the syscall number on the stack */
|
---|
313 |
|
---|
314 | /* restore status */
|
---|
315 | mfc0 $t2, $status
|
---|
316 | lw $t3, SS_STATUS($sp)
|
---|
317 |
|
---|
318 | /*
|
---|
319 | * Change back to EXL = 1 (from last exception), otherwise
|
---|
320 | * an interrupt could rewrite the CP0 - EPC.
|
---|
321 | *
|
---|
322 | */
|
---|
323 | li $t4, ~REG_SAVE_MASK /* mask UM, EXL, ERL, IE */
|
---|
324 | and $t2, $t2, $t4
|
---|
325 | or $t2, $t2, $t3 /* copy saved UM, EXL, ERL, IE */
|
---|
326 | mtc0 $t2, $status
|
---|
327 |
|
---|
328 | /* restore epc + 4 */
|
---|
329 | lw $t2, SS_EPC($sp)
|
---|
330 | lw $k1, SS_K1($sp)
|
---|
331 | addi $t2, $t2, 4
|
---|
332 | mtc0 $t2, $epc
|
---|
333 |
|
---|
334 | lw $sp, SS_SP($sp) /* restore $sp */
|
---|
335 | eret
|
---|
336 |
|
---|
337 | tlb_refill_handler:
|
---|
338 | KERNEL_STACK_TO_K0
|
---|
339 | sub $k0, REGISTER_SPACE
|
---|
340 | REGISTERS_STORE_AND_EXC_RESET $k0
|
---|
341 | sw $sp,EOFFSET_SP($k0)
|
---|
342 | add $sp, $k0, 0
|
---|
343 |
|
---|
344 | jal tlb_refill
|
---|
345 | add $a0, $sp, 0
|
---|
346 |
|
---|
347 | REGISTERS_LOAD $sp
|
---|
348 | eret
|
---|
349 |
|
---|
350 | cache_error_handler:
|
---|
351 | KERNEL_STACK_TO_K0
|
---|
352 | sub $k0, REGISTER_SPACE
|
---|
353 | REGISTERS_STORE_AND_EXC_RESET $k0
|
---|
354 | sw $sp,EOFFSET_SP($k0)
|
---|
355 | add $sp, $k0, 0
|
---|
356 |
|
---|
357 | jal cache_error
|
---|
358 | add $a0, $sp, 0
|
---|
359 |
|
---|
360 | REGISTERS_LOAD $sp
|
---|
361 | eret
|
---|
362 |
|
---|
363 | userspace_asm:
|
---|
364 | add $sp, $a0, 0
|
---|
365 | add $v0, $a1, 0
|
---|
366 | add $t9, $a2, 0 /* set up correct entry into PIC code */
|
---|
367 | xor $a0, $a0, $a0 /* $a0 is defined to hold pcb_ptr */
|
---|
368 | /* set it to 0 */
|
---|
369 | eret
|
---|