source: mainline/kernel/arch/mips32/src/start.S@ 20235a3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 20235a3 was 3fb3c1fc, checked in by Jakub Jermar <jakub@…>, 15 years ago

On a non-syscall exception, save all GPRs into istate_t.

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