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

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

coding style changes, no change in functionality

  • Property mode set to 100644
File size: 7.2 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# Which status bits should are thread-local
48#define REG_SAVE_MASK 0x1f # KSU(UM), EXL, ERL, IE
49
50# Save registers to space defined by \r
51# We will change status: Disable ERL, EXL, UM, IE
52# These changes will be automatically reversed in REGISTER_LOAD
53# %sp is NOT saved as part of these registers
54.macro REGISTERS_STORE_AND_EXC_RESET r
55 sw $at, EOFFSET_AT(\r)
56 sw $v0, EOFFSET_V0(\r)
57 sw $v1, EOFFSET_V1(\r)
58 sw $a0, EOFFSET_A0(\r)
59 sw $a1, EOFFSET_A1(\r)
60 sw $a2, EOFFSET_A2(\r)
61 sw $a3, EOFFSET_A3(\r)
62 sw $t0, EOFFSET_T0(\r)
63 sw $t1, EOFFSET_T1(\r)
64 sw $t2, EOFFSET_T2(\r)
65 sw $t3, EOFFSET_T3(\r)
66 sw $t4, EOFFSET_T4(\r)
67 sw $t5, EOFFSET_T5(\r)
68 sw $t6, EOFFSET_T6(\r)
69 sw $t7, EOFFSET_T7(\r)
70 sw $t8, EOFFSET_T8(\r)
71 sw $t9, EOFFSET_T9(\r)
72
73 mflo $at
74 sw $at, EOFFSET_LO(\r)
75 mfhi $at
76 sw $at, EOFFSET_HI(\r)
77
78 sw $gp, EOFFSET_GP(\r)
79 sw $ra, EOFFSET_RA(\r)
80 sw $k1, EOFFSET_K1(\r)
81
82 mfc0 $t0, $status
83 mfc0 $t1, $epc
84
85 # save only KSU, EXL, ERL, IE
86 and $t2, $t0, REG_SAVE_MASK
87
88 # clear KSU, EXL, ERL, IE
89 li $t3, ~(REG_SAVE_MASK)
90 and $t0, $t0, $t3
91
92 sw $t2, EOFFSET_STATUS(\r)
93 sw $t1, EOFFSET_EPC(\r)
94 mtc0 $t0, $status
95.endm
96
97.macro REGISTERS_LOAD r
98 # Update only UM, EXR, IE from status, the rest
99 # is controlled by OS and not bound to task
100 mfc0 $t0, $status
101 lw $t1,EOFFSET_STATUS(\r)
102
103 # Mask UM, EXL, ERL, IE
104 li $t2, ~REG_SAVE_MASK
105 and $t0, $t0, $t2
106
107 # Copy UM, EXL, ERL, IE from saved status
108 or $t0, $t0, $t1
109 mtc0 $t0, $status
110
111 lw $v0, EOFFSET_V0(\r)
112 lw $v1, EOFFSET_V1(\r)
113 lw $a0, EOFFSET_A0(\r)
114 lw $a1, EOFFSET_A1(\r)
115 lw $a2, EOFFSET_A2(\r)
116 lw $a3, EOFFSET_A3(\r)
117 lw $t0, EOFFSET_T0(\r)
118 lw $t1, EOFFSET_T1(\r)
119 lw $t2, EOFFSET_T2(\r)
120 lw $t3, EOFFSET_T3(\r)
121 lw $t4, EOFFSET_T4(\r)
122 lw $t5, EOFFSET_T5(\r)
123 lw $t6, EOFFSET_T6(\r)
124 lw $t7, EOFFSET_T7(\r)
125 lw $t8, EOFFSET_T8(\r)
126 lw $t9, EOFFSET_T9(\r)
127
128 lw $gp, EOFFSET_GP(\r)
129 lw $ra, EOFFSET_RA(\r)
130 lw $k1, EOFFSET_K1(\r)
131
132 lw $at, EOFFSET_LO(\r)
133 mtlo $at
134 lw $at, EOFFSET_HI(\r)
135 mthi $at
136
137 lw $at, EOFFSET_EPC(\r)
138 mtc0 $at, $epc
139
140 lw $at, EOFFSET_AT(\r)
141 lw $sp, EOFFSET_SP(\r)
142.endm
143
144# Move kernel stack pointer address to register K0
145# - if we are in user mode, load the appropriate stack address
146.macro KERNEL_STACK_TO_K0
147 # if we are in user mode
148 mfc0 $k0, $status
149 andi $k0, 0x10
150
151 beq $k0, $0, 1f
152 add $k0, $sp, 0
153
154 # move $k0 pointer to kernel stack
155 lui $k0, %hi(supervisor_sp)
156 ori $k0, $k0, %lo(supervisor_sp)
157
158 # move $k0 (supervisor_sp)
159 lw $k0, 0($k0)
160
161 1:
162.endm
163
164.org 0x0
165kernel_image_start:
166 # load temporary stack
167 lui $sp, %hi(end_stack)
168 ori $sp, $sp, %lo(end_stack)
169
170 # Not sure about this, but might
171 # be needed for PIC code
172 lui $gp, 0x8000
173
174 # $a1 contains physical address of bootinfo_t
175 jal arch_pre_main
176 nop
177
178 j main_bsp
179 nop
180
181.space TEMP_STACK_SIZE
182end_stack:
183
184tlb_refill_entry:
185 j tlb_refill_handler
186 nop
187
188cache_error_entry:
189 j cache_error_handler
190 nop
191
192exception_entry:
193 j exception_handler
194 nop
195
196exception_handler:
197 KERNEL_STACK_TO_K0
198
199 sub $k0, REGISTER_SPACE
200 sw $sp, EOFFSET_SP($k0)
201 move $sp, $k0
202
203 mfc0 $k0, $cause
204
205 sra $k0, $k0, 0x2 # cp0_exc_cause() part 1
206 andi $k0, $k0, 0x1f # cp0_exc_cause() part 2
207 sub $k0, 8 # 8 = SYSCALL
208
209 beqz $k0, syscall_shortcut
210 add $k0, 8 # Revert $k0 back to correct exc number
211
212 REGISTERS_STORE_AND_EXC_RESET $sp
213
214 move $a1, $sp
215 jal exc_dispatch # exc_dispatch(excno, register_space)
216 move $a0, $k0
217
218 REGISTERS_LOAD $sp
219 # The $sp is automatically restored to former value
220 eret
221
222## Syscall entry
223#
224# Registers:
225#
226# @param v0 Syscall number.
227# @param a0 1st argument.
228# @param a1 2nd argument.
229# @param a2 3rd argument.
230# @param a3 4th argument.
231# @param t0 5th argument.
232# @param t1 6th argument.
233#
234# @return The return value will be stored in v0.
235#
236#define SS_SP EOFFSET_SP
237#define SS_STATUS EOFFSET_STATUS
238#define SS_EPC EOFFSET_EPC
239#define SS_K1 EOFFSET_K1
240syscall_shortcut:
241 # We have a lot of space on the stack, with free use
242 mfc0 $t3, $epc
243 mfc0 $t2, $status
244 sw $t3, SS_EPC($sp) # Save EPC
245 sw $k1, SS_K1($sp) # Save k1 not saved on context switch
246
247 and $t4, $t2, REG_SAVE_MASK # Save only KSU, EXL, ERL, IE
248 li $t5, ~(0x1f)
249 and $t2, $t2, $t5 # Clear KSU, EXL, ERL
250 ori $t2, $t2, 0x1 # Set IE
251
252 sw $t4, SS_STATUS($sp)
253 mtc0 $t2, $status
254
255 #
256 # Call the higher level system call handler
257 # We are going to reuse part of the unused exception stack frame
258 #
259 sw $t0, STACK_ARG4($sp) # save the 5th argument on the stack
260 sw $t1, STACK_ARG5($sp) # save the 6th argument on the stack
261 jal syscall_handler
262 sw $v0, STACK_ARG6($sp) # save the syscall number on the stack
263
264 # restore status
265 mfc0 $t2, $status
266 lw $t3, SS_STATUS($sp)
267
268 # Change back to EXL = 1 (from last exception), otherwise
269 # an interrupt could rewrite the CP0 - EPC
270 li $t4, ~REG_SAVE_MASK # Mask UM, EXL, ERL, IE
271 and $t2, $t2, $t4
272 or $t2, $t2, $t3 # Copy saved UM, EXL, ERL, IE
273 mtc0 $t2, $status
274
275 # restore epc + 4
276 lw $t2, SS_EPC($sp)
277 lw $k1, SS_K1($sp)
278 addi $t2, $t2, 4
279 mtc0 $t2, $epc
280
281 lw $sp, SS_SP($sp) # restore sp
282
283 eret
284
285tlb_refill_handler:
286 KERNEL_STACK_TO_K0
287 sub $k0, REGISTER_SPACE
288 REGISTERS_STORE_AND_EXC_RESET $k0
289 sw $sp,EOFFSET_SP($k0)
290 add $sp, $k0, 0
291
292 jal tlb_refill
293 add $a0, $sp, 0
294
295 REGISTERS_LOAD $sp
296
297 eret
298
299cache_error_handler:
300 KERNEL_STACK_TO_K0
301 sub $k0, REGISTER_SPACE
302 REGISTERS_STORE_AND_EXC_RESET $k0
303 sw $sp,EOFFSET_SP($k0)
304 add $sp, $k0, 0
305
306 jal cache_error
307 add $a0, $sp, 0
308
309 REGISTERS_LOAD $sp
310
311 eret
312
313userspace_asm:
314 add $sp, $a0, 0
315 add $v0, $a1, 0
316 add $t9, $a2, 0 # Set up correct entry into PIC code
317 xor $a0, $a0, $a0 # $a0 is defined to hold pcb_ptr
318 # set it to 0
319 eret
Note: See TracBrowser for help on using the repository browser.