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