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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b0c2075 was 6d123b3, checked in by Martin Decky <martin@…>, 13 years ago

respect the o32 ABI when calling C functions from assembler and creating contexts
(thx Adam Hraska)

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