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