source: mainline/kernel/arch/mips32/src/start.S@ 07c913b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 07c913b was 33add3a8, checked in by Jakub Jermar <jakub@…>, 11 years ago

Autogenerate mips32 kernel context_t and its offsets.

  • Property mode set to 100644
File size: 8.5 KB
Line 
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/stack.h>
33#include <arch/istate_struct.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 are thread-local:
49 * KSU(UM), EXL, ERL, IE
50 */
51#define REG_SAVE_MASK 0x1f
52
53/*
54 * The fake ABI prologue is never executed and may not be part of the
55 * procedure's body. Instead, it should be immediately preceding the procedure's
56 * body. Its only purpose is to trick the stack trace walker into thinking that
57 * the exception is more or less just a normal function call.
58 */
59.macro FAKE_ABI_PROLOGUE
60 sub $sp, ISTATE_SIZE
61 sw $ra, ISTATE_OFFSET_EPC($sp)
62.endm
63
64/*
65 * Save registers to space defined by \r
66 * We will change status: Disable ERL, EXL, UM, IE
67 * These changes will be automatically reversed in REGISTER_LOAD
68 * %sp is NOT saved as part of these registers
69 */
70.macro REGISTERS_STORE_AND_EXC_RESET r
71 sw $at, ISTATE_OFFSET_AT(\r)
72 sw $v0, ISTATE_OFFSET_V0(\r)
73 sw $v1, ISTATE_OFFSET_V1(\r)
74 sw $a0, ISTATE_OFFSET_A0(\r)
75 sw $a1, ISTATE_OFFSET_A1(\r)
76 sw $a2, ISTATE_OFFSET_A2(\r)
77 sw $a3, ISTATE_OFFSET_A3(\r)
78 sw $t0, ISTATE_OFFSET_T0(\r)
79 sw $t1, ISTATE_OFFSET_T1(\r)
80 sw $t2, ISTATE_OFFSET_T2(\r)
81 sw $t3, ISTATE_OFFSET_T3(\r)
82 sw $t4, ISTATE_OFFSET_T4(\r)
83 sw $t5, ISTATE_OFFSET_T5(\r)
84 sw $t6, ISTATE_OFFSET_T6(\r)
85 sw $t7, ISTATE_OFFSET_T7(\r)
86 sw $t8, ISTATE_OFFSET_T8(\r)
87 sw $t9, ISTATE_OFFSET_T9(\r)
88 sw $s0, ISTATE_OFFSET_S0(\r)
89 sw $s1, ISTATE_OFFSET_S1(\r)
90 sw $s2, ISTATE_OFFSET_S2(\r)
91 sw $s3, ISTATE_OFFSET_S3(\r)
92 sw $s4, ISTATE_OFFSET_S4(\r)
93 sw $s5, ISTATE_OFFSET_S5(\r)
94 sw $s6, ISTATE_OFFSET_S6(\r)
95 sw $s7, ISTATE_OFFSET_S7(\r)
96 sw $s8, ISTATE_OFFSET_S8(\r)
97
98 mflo $at
99 sw $at, ISTATE_OFFSET_LO(\r)
100 mfhi $at
101 sw $at, ISTATE_OFFSET_HI(\r)
102
103 sw $gp, ISTATE_OFFSET_GP(\r)
104 sw $ra, ISTATE_OFFSET_RA(\r)
105 sw $k0, ISTATE_OFFSET_KT0(\r)
106 sw $k1, ISTATE_OFFSET_KT1(\r)
107
108 mfc0 $t0, $status
109 mfc0 $t1, $epc
110
111 /* save only KSU, EXL, ERL, IE */
112 and $t2, $t0, REG_SAVE_MASK
113
114 /* clear KSU, EXL, ERL, IE */
115 li $t3, ~(REG_SAVE_MASK)
116 and $t0, $t0, $t3
117
118 sw $t2, ISTATE_OFFSET_STATUS(\r)
119 sw $t1, ISTATE_OFFSET_EPC(\r)
120 mtc0 $t0, $status
121.endm
122
123.macro REGISTERS_LOAD r
124 /*
125 * Update only UM, EXR, IE from status, the rest
126 * is controlled by OS and not bound to task.
127 */
128 mfc0 $t0, $status
129 lw $t1, ISTATE_OFFSET_STATUS(\r)
130
131 /* mask UM, EXL, ERL, IE */
132 li $t2, ~REG_SAVE_MASK
133 and $t0, $t0, $t2
134
135 /* copy UM, EXL, ERL, IE from saved status */
136 or $t0, $t0, $t1
137 mtc0 $t0, $status
138
139 lw $v0, ISTATE_OFFSET_V0(\r)
140 lw $v1, ISTATE_OFFSET_V1(\r)
141 lw $a0, ISTATE_OFFSET_A0(\r)
142 lw $a1, ISTATE_OFFSET_A1(\r)
143 lw $a2, ISTATE_OFFSET_A2(\r)
144 lw $a3, ISTATE_OFFSET_A3(\r)
145 lw $t0, ISTATE_OFFSET_T0(\r)
146 lw $t1, ISTATE_OFFSET_T1(\r)
147 lw $t2, ISTATE_OFFSET_T2(\r)
148 lw $t3, ISTATE_OFFSET_T3(\r)
149 lw $t4, ISTATE_OFFSET_T4(\r)
150 lw $t5, ISTATE_OFFSET_T5(\r)
151 lw $t6, ISTATE_OFFSET_T6(\r)
152 lw $t7, ISTATE_OFFSET_T7(\r)
153 lw $t8, ISTATE_OFFSET_T8(\r)
154 lw $t9, ISTATE_OFFSET_T9(\r)
155
156 lw $gp, ISTATE_OFFSET_GP(\r)
157 lw $ra, ISTATE_OFFSET_RA(\r)
158 lw $k1, ISTATE_OFFSET_KT1(\r)
159
160 lw $at, ISTATE_OFFSET_LO(\r)
161 mtlo $at
162 lw $at, ISTATE_OFFSET_HI(\r)
163 mthi $at
164
165 lw $at, ISTATE_OFFSET_EPC(\r)
166 mtc0 $at, $epc
167
168 lw $at, ISTATE_OFFSET_AT(\r)
169 lw $sp, ISTATE_OFFSET_SP(\r)
170.endm
171
172/*
173 * Move kernel stack pointer address to register $k0.
174 * If we are in user mode, load the appropriate stack address.
175 */
176.macro KERNEL_STACK_TO_K0
177 /* if we are in user mode */
178 mfc0 $k0, $status
179 andi $k0, 0x10
180
181 beq $k0, $0, 1f
182 move $k0, $sp
183
184 /* move $k0 pointer to kernel stack */
185 la $k0, supervisor_sp
186
187 /* move $k0 (supervisor_sp) */
188 lw $k0, ($k0)
189
190 1:
191.endm
192
193.org 0x0
194kernel_image_start:
195 /* load temporary stack */
196 lui $sp, %hi(end_stack)
197 ori $sp, $sp, %lo(end_stack)
198
199 /* not sure about this, but might be needed for PIC code */
200 lui $gp, 0x8000
201
202 /* $a1 contains physical address of bootinfo_t */
203 jal arch_pre_main
204 addiu $sp, -ABI_STACK_FRAME
205
206 j main_bsp
207 nop
208
209.space TEMP_STACK_SIZE
210end_stack:
211
212tlb_refill_entry:
213 j tlb_refill_handler
214 nop
215
216cache_error_entry:
217 j cache_error_handler
218 nop
219
220exception_entry:
221 j exception_handler
222 nop
223
224 FAKE_ABI_PROLOGUE
225exception_handler:
226 KERNEL_STACK_TO_K0
227
228 sub $k0, ISTATE_SIZE
229 sw $sp, ISTATE_OFFSET_SP($k0)
230 move $sp, $k0
231
232 mfc0 $k0, $cause
233
234 sra $k0, $k0, 0x2 /* cp0_exc_cause() part 1 */
235 andi $k0, $k0, 0x1f /* cp0_exc_cause() part 2 */
236 sub $k0, 8 /* 8 = SYSCALL */
237
238 beqz $k0, syscall_shortcut
239 add $k0, 8 /* revert $k0 back to correct exc number */
240
241 REGISTERS_STORE_AND_EXC_RESET $sp
242
243 move $a1, $sp
244 move $a0, $k0
245 jal exc_dispatch /* exc_dispatch(excno, register_space) */
246 addiu $sp, -ABI_STACK_FRAME
247 addiu $sp, ABI_STACK_FRAME
248
249 REGISTERS_LOAD $sp
250 /* the $sp is automatically restored to former value */
251 eret
252
253/** Syscall entry
254 *
255 * Registers:
256 *
257 * @param $v0 Syscall number.
258 * @param $a0 1st argument.
259 * @param $a1 2nd argument.
260 * @param $a2 3rd argument.
261 * @param $a3 4th argument.
262 * @param $t0 5th argument.
263 * @param $t1 6th argument.
264 *
265 * @return The return value will be stored in $v0.
266 *
267 */
268syscall_shortcut:
269 mfc0 $t3, $epc
270 mfc0 $t2, $status
271 sw $t3, ISTATE_OFFSET_EPC($sp) /* save EPC */
272 sw $k1, ISTATE_OFFSET_KT1($sp) /* save $k1 not saved on context switch */
273
274 and $t4, $t2, REG_SAVE_MASK /* save only KSU, EXL, ERL, IE */
275 li $t5, ~(0x1f)
276 and $t2, $t2, $t5 /* clear KSU, EXL, ERL */
277 ori $t2, $t2, 0x1 /* set IE */
278
279 sw $t4, ISTATE_OFFSET_STATUS($sp)
280 mtc0 $t2, $status
281
282 /*
283 * Call the higher level system call handler.
284 *
285 */
286 sw $t0, ISTATE_OFFSET_T0($sp) /* save the 5th argument on the stack */
287 sw $t1, ISTATE_OFFSET_T1($sp) /* save the 6th argument on the stack */
288
289 jal syscall_handler
290 sw $v0, ISTATE_OFFSET_V0($sp) /* save the syscall number on the stack */
291
292 /* restore status */
293 mfc0 $t2, $status
294 lw $t3, ISTATE_OFFSET_STATUS($sp)
295
296 /*
297 * Change back to EXL = 1 (from last exception), otherwise
298 * an interrupt could rewrite the CP0 - EPC.
299 *
300 */
301 li $t4, ~REG_SAVE_MASK /* mask UM, EXL, ERL, IE */
302 and $t2, $t2, $t4
303 or $t2, $t2, $t3 /* copy saved UM, EXL, ERL, IE */
304 mtc0 $t2, $status
305
306 /* restore epc + 4 */
307 lw $t2, ISTATE_OFFSET_EPC($sp)
308 lw $k1, ISTATE_OFFSET_KT1($sp)
309 addi $t2, $t2, 4
310 mtc0 $t2, $epc
311
312 lw $sp, ISTATE_OFFSET_SP($sp) /* restore $sp */
313 eret
314
315 FAKE_ABI_PROLOGUE
316tlb_refill_handler:
317 KERNEL_STACK_TO_K0
318 sub $k0, ISTATE_SIZE
319 REGISTERS_STORE_AND_EXC_RESET $k0
320 sw $sp, ISTATE_OFFSET_SP($k0)
321 move $sp, $k0
322
323 move $a0, $sp
324 jal tlb_refill
325 addiu $sp, -ABI_STACK_FRAME
326 addiu $sp, ABI_STACK_FRAME
327
328 REGISTERS_LOAD $sp
329 eret
330
331 FAKE_ABI_PROLOGUE
332cache_error_handler:
333 KERNEL_STACK_TO_K0
334 sub $k0, ISTATE_SIZE
335 REGISTERS_STORE_AND_EXC_RESET $k0
336 sw $sp, ISTATE_OFFSET_SP($k0)
337 move $sp, $k0
338
339 move $a0, $sp
340 jal cache_error
341 addiu $sp, -ABI_STACK_FRAME
342 addiu $sp, ABI_STACK_FRAME
343
344 REGISTERS_LOAD $sp
345 eret
346
347userspace_asm:
348 move $sp, $a0
349 move $v0, $a1
350 move $t9, $a2 /* set up correct entry into PIC code */
351 xor $a0, $a0, $a0 /* $a0 is defined to hold pcb_ptr */
352 /* set it to 0 */
353 eret
Note: See TracBrowser for help on using the repository browser.