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