source: mainline/kernel/arch/mips32/src/start.S@ 4b334fd6

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

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