| 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 <abi/asmtool.h>
|
|---|
| 30 | #include <arch/asm/regname.h>
|
|---|
| 31 | #include <arch/mm/page.h>
|
|---|
| 32 | #include <arch/asm/boot.h>
|
|---|
| 33 | #include <arch/stack.h>
|
|---|
| 34 | #include <arch/istate_struct.h>
|
|---|
| 35 |
|
|---|
| 36 | .text
|
|---|
| 37 |
|
|---|
| 38 | .set noat
|
|---|
| 39 | .set noreorder
|
|---|
| 40 | .set nomacro
|
|---|
| 41 |
|
|---|
| 42 | /*
|
|---|
| 43 | * Which status bits are thread-local:
|
|---|
| 44 | * KSU(UM), EXL, ERL, IE
|
|---|
| 45 | */
|
|---|
| 46 | #define REG_SAVE_MASK 0x1f
|
|---|
| 47 |
|
|---|
| 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
|
|---|
| 55 | sub $sp, ISTATE_SIZE
|
|---|
| 56 | sw $ra, ISTATE_OFFSET_EPC($sp)
|
|---|
| 57 | .endm
|
|---|
| 58 |
|
|---|
| 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 | */
|
|---|
| 65 | .macro REGISTERS_STORE_AND_EXC_RESET r
|
|---|
| 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)
|
|---|
| 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)
|
|---|
| 92 |
|
|---|
| 93 | mflo $at
|
|---|
| 94 | sw $at, ISTATE_OFFSET_LO(\r)
|
|---|
| 95 | mfhi $at
|
|---|
| 96 | sw $at, ISTATE_OFFSET_HI(\r)
|
|---|
| 97 |
|
|---|
| 98 | sw $gp, ISTATE_OFFSET_GP(\r)
|
|---|
| 99 | sw $ra, ISTATE_OFFSET_RA(\r)
|
|---|
| 100 | sw $k0, ISTATE_OFFSET_KT0(\r)
|
|---|
| 101 | sw $k1, ISTATE_OFFSET_KT1(\r)
|
|---|
| 102 |
|
|---|
| 103 | mfc0 $t0, $status
|
|---|
| 104 | mfc0 $t1, $epc
|
|---|
| 105 |
|
|---|
| 106 | /* save only KSU, EXL, ERL, IE */
|
|---|
| 107 | and $t2, $t0, REG_SAVE_MASK
|
|---|
| 108 |
|
|---|
| 109 | /* clear KSU, EXL, ERL, IE */
|
|---|
| 110 | li $t3, ~(REG_SAVE_MASK)
|
|---|
| 111 | and $t0, $t0, $t3
|
|---|
| 112 |
|
|---|
| 113 | sw $t2, ISTATE_OFFSET_STATUS(\r)
|
|---|
| 114 | sw $t1, ISTATE_OFFSET_EPC(\r)
|
|---|
| 115 | mtc0 $t0, $status
|
|---|
| 116 | .endm
|
|---|
| 117 |
|
|---|
| 118 | .macro REGISTERS_LOAD r
|
|---|
| 119 | /*
|
|---|
| 120 | * Update only UM, EXR, IE from status, the rest
|
|---|
| 121 | * is controlled by OS and not bound to task.
|
|---|
| 122 | */
|
|---|
| 123 | mfc0 $t0, $status
|
|---|
| 124 | lw $t1, ISTATE_OFFSET_STATUS(\r)
|
|---|
| 125 |
|
|---|
| 126 | /* mask UM, EXL, ERL, IE */
|
|---|
| 127 | li $t2, ~REG_SAVE_MASK
|
|---|
| 128 | and $t0, $t0, $t2
|
|---|
| 129 |
|
|---|
| 130 | /* copy UM, EXL, ERL, IE from saved status */
|
|---|
| 131 | or $t0, $t0, $t1
|
|---|
| 132 | mtc0 $t0, $status
|
|---|
| 133 |
|
|---|
| 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)
|
|---|
| 156 | mtlo $at
|
|---|
| 157 | lw $at, ISTATE_OFFSET_HI(\r)
|
|---|
| 158 | mthi $at
|
|---|
| 159 |
|
|---|
| 160 | lw $at, ISTATE_OFFSET_EPC(\r)
|
|---|
| 161 | mtc0 $at, $epc
|
|---|
| 162 |
|
|---|
| 163 | lw $at, ISTATE_OFFSET_AT(\r)
|
|---|
| 164 | lw $sp, ISTATE_OFFSET_SP(\r)
|
|---|
| 165 | .endm
|
|---|
| 166 |
|
|---|
| 167 | /*
|
|---|
| 168 | * Move kernel stack pointer address to register $k0.
|
|---|
| 169 | * If we are in user mode, load the appropriate stack address.
|
|---|
| 170 | */
|
|---|
| 171 | .macro KERNEL_STACK_TO_K0
|
|---|
| 172 | /* if we are in user mode */
|
|---|
| 173 | mfc0 $k0, $status
|
|---|
| 174 | andi $k0, 0x10
|
|---|
| 175 |
|
|---|
| 176 | beq $k0, $0, 1f
|
|---|
| 177 | move $k0, $sp
|
|---|
| 178 |
|
|---|
| 179 | /* move $k0 pointer to kernel stack */
|
|---|
| 180 | la $k0, supervisor_sp
|
|---|
| 181 |
|
|---|
| 182 | /* move $k0 (supervisor_sp) */
|
|---|
| 183 | lw $k0, ($k0)
|
|---|
| 184 |
|
|---|
| 185 | 1:
|
|---|
| 186 | .endm
|
|---|
| 187 |
|
|---|
| 188 | .org 0x0
|
|---|
| 189 | SYMBOL(kernel_image_start)
|
|---|
| 190 | /* load temporary stack */
|
|---|
| 191 | lui $sp, %hi(end_stack)
|
|---|
| 192 | ori $sp, $sp, %lo(end_stack)
|
|---|
| 193 |
|
|---|
| 194 | /* not sure about this, but might be needed for PIC code */
|
|---|
| 195 | lui $gp, 0x8000
|
|---|
| 196 |
|
|---|
| 197 | /* $a1 contains physical address of bootinfo_t */
|
|---|
| 198 | jal mips32_pre_main
|
|---|
| 199 | addiu $sp, -ABI_STACK_FRAME
|
|---|
| 200 |
|
|---|
| 201 | j main_bsp
|
|---|
| 202 | nop
|
|---|
| 203 |
|
|---|
| 204 | .space TEMP_STACK_SIZE
|
|---|
| 205 | end_stack:
|
|---|
| 206 |
|
|---|
| 207 | SYMBOL(tlb_refill_entry)
|
|---|
| 208 | j tlb_refill_handler
|
|---|
| 209 | nop
|
|---|
| 210 |
|
|---|
| 211 | SYMBOL(cache_error_entry)
|
|---|
| 212 | j cache_error_handler
|
|---|
| 213 | nop
|
|---|
| 214 |
|
|---|
| 215 | SYMBOL(exception_entry)
|
|---|
| 216 | j exception_handler
|
|---|
| 217 | nop
|
|---|
| 218 |
|
|---|
| 219 | FAKE_ABI_PROLOGUE
|
|---|
| 220 | exception_handler:
|
|---|
| 221 | KERNEL_STACK_TO_K0
|
|---|
| 222 |
|
|---|
| 223 | sub $k0, ISTATE_SIZE
|
|---|
| 224 | sw $sp, ISTATE_OFFSET_SP($k0)
|
|---|
| 225 | move $sp, $k0
|
|---|
| 226 |
|
|---|
| 227 | mfc0 $k0, $cause
|
|---|
| 228 |
|
|---|
| 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 */
|
|---|
| 232 |
|
|---|
| 233 | beqz $k0, syscall_shortcut
|
|---|
| 234 | add $k0, 8 /* revert $k0 back to correct exc number */
|
|---|
| 235 |
|
|---|
| 236 | REGISTERS_STORE_AND_EXC_RESET $sp
|
|---|
| 237 |
|
|---|
| 238 | move $a1, $sp
|
|---|
| 239 | move $a0, $k0
|
|---|
| 240 | jal exc_dispatch /* exc_dispatch(excno, register_space) */
|
|---|
| 241 | addiu $sp, -ABI_STACK_FRAME
|
|---|
| 242 | addiu $sp, ABI_STACK_FRAME
|
|---|
| 243 |
|
|---|
| 244 | REGISTERS_LOAD $sp
|
|---|
| 245 | /* the $sp is automatically restored to former value */
|
|---|
| 246 | eret
|
|---|
| 247 |
|
|---|
| 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 | */
|
|---|
| 263 | syscall_shortcut:
|
|---|
| 264 | mfc0 $t3, $epc
|
|---|
| 265 | mfc0 $t2, $status
|
|---|
| 266 | sw $t3, ISTATE_OFFSET_EPC($sp) /* save EPC */
|
|---|
| 267 | sw $k1, ISTATE_OFFSET_KT1($sp) /* save $k1 not saved on context switch */
|
|---|
| 268 |
|
|---|
| 269 | and $t4, $t2, REG_SAVE_MASK /* save only KSU, EXL, ERL, IE */
|
|---|
| 270 | li $t5, ~(0x1f)
|
|---|
| 271 | and $t2, $t2, $t5 /* clear KSU, EXL, ERL */
|
|---|
| 272 | ori $t2, $t2, 0x1 /* set IE */
|
|---|
| 273 |
|
|---|
| 274 | sw $t4, ISTATE_OFFSET_STATUS($sp)
|
|---|
| 275 | mtc0 $t2, $status
|
|---|
| 276 |
|
|---|
| 277 | /*
|
|---|
| 278 | * Call the higher level system call handler.
|
|---|
| 279 | *
|
|---|
| 280 | */
|
|---|
| 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 */
|
|---|
| 283 |
|
|---|
| 284 | jal syscall_handler
|
|---|
| 285 | sw $v0, ISTATE_OFFSET_V0($sp) /* save the syscall number on the stack */
|
|---|
| 286 |
|
|---|
| 287 | /* restore status */
|
|---|
| 288 | mfc0 $t2, $status
|
|---|
| 289 | lw $t3, ISTATE_OFFSET_STATUS($sp)
|
|---|
| 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 */
|
|---|
| 297 | and $t2, $t2, $t4
|
|---|
| 298 | or $t2, $t2, $t3 /* copy saved UM, EXL, ERL, IE */
|
|---|
| 299 | mtc0 $t2, $status
|
|---|
| 300 |
|
|---|
| 301 | /* restore epc + 4 */
|
|---|
| 302 | lw $t2, ISTATE_OFFSET_EPC($sp)
|
|---|
| 303 | lw $k1, ISTATE_OFFSET_KT1($sp)
|
|---|
| 304 | addi $t2, $t2, 4
|
|---|
| 305 | mtc0 $t2, $epc
|
|---|
| 306 |
|
|---|
| 307 | lw $sp, ISTATE_OFFSET_SP($sp) /* restore $sp */
|
|---|
| 308 | eret
|
|---|
| 309 |
|
|---|
| 310 | FAKE_ABI_PROLOGUE
|
|---|
| 311 | tlb_refill_handler:
|
|---|
| 312 | KERNEL_STACK_TO_K0
|
|---|
| 313 | sub $k0, ISTATE_SIZE
|
|---|
| 314 | REGISTERS_STORE_AND_EXC_RESET $k0
|
|---|
| 315 | sw $sp, ISTATE_OFFSET_SP($k0)
|
|---|
| 316 | move $sp, $k0
|
|---|
| 317 |
|
|---|
| 318 | move $a0, $sp
|
|---|
| 319 | jal tlb_refill
|
|---|
| 320 | addiu $sp, -ABI_STACK_FRAME
|
|---|
| 321 | addiu $sp, ABI_STACK_FRAME
|
|---|
| 322 |
|
|---|
| 323 | REGISTERS_LOAD $sp
|
|---|
| 324 | eret
|
|---|
| 325 |
|
|---|
| 326 | FAKE_ABI_PROLOGUE
|
|---|
| 327 | cache_error_handler:
|
|---|
| 328 | KERNEL_STACK_TO_K0
|
|---|
| 329 | sub $k0, ISTATE_SIZE
|
|---|
| 330 | REGISTERS_STORE_AND_EXC_RESET $k0
|
|---|
| 331 | sw $sp, ISTATE_OFFSET_SP($k0)
|
|---|
| 332 | move $sp, $k0
|
|---|
| 333 |
|
|---|
| 334 | move $a0, $sp
|
|---|
| 335 | jal cache_error
|
|---|
| 336 | addiu $sp, -ABI_STACK_FRAME
|
|---|
| 337 | addiu $sp, ABI_STACK_FRAME
|
|---|
| 338 |
|
|---|
| 339 | REGISTERS_LOAD $sp
|
|---|
| 340 | eret
|
|---|
| 341 |
|
|---|
| 342 | FUNCTION_BEGIN(userspace_asm)
|
|---|
| 343 | move $sp, $a0
|
|---|
| 344 | move $v0, $a1
|
|---|
| 345 | move $t9, $a2 /* set up correct entry into PIC code */
|
|---|
| 346 | xor $a0, $a0, $a0 /* $a0 is defined to hold pcb_ptr */
|
|---|
| 347 | /* set it to 0 */
|
|---|
| 348 | eret
|
|---|
| 349 | FUNCTION_END(userspace_asm)
|
|---|