paul@68 | 1 | /* |
paul@68 | 2 | * Handler routines. |
paul@68 | 3 | * |
paul@91 | 4 | * Copyright (C) 2015 Nicholas FitzRoy-Dale <wzdd.code@lardcave.net> |
paul@110 | 5 | * Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@68 | 6 | * |
paul@68 | 7 | * This program is free software: you can redistribute it and/or modify |
paul@68 | 8 | * it under the terms of the GNU General Public License as published by |
paul@68 | 9 | * the Free Software Foundation, either version 3 of the License, or |
paul@68 | 10 | * (at your option) any later version. |
paul@68 | 11 | * |
paul@68 | 12 | * This program is distributed in the hope that it will be useful, |
paul@68 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@68 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@68 | 15 | * GNU General Public License for more details. |
paul@68 | 16 | * |
paul@68 | 17 | * You should have received a copy of the GNU General Public License |
paul@68 | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@68 | 19 | */ |
paul@68 | 20 | |
paul@68 | 21 | .text |
paul@68 | 22 | .extern irq_handle |
paul@145 | 23 | .extern current_registers |
paul@133 | 24 | .extern current_stack_pointer |
paul@133 | 25 | .extern current_task |
paul@86 | 26 | .globl interrupt_handler |
paul@167 | 27 | .globl enter_task |
paul@68 | 28 | .set noreorder |
paul@68 | 29 | .set noat |
paul@68 | 30 | |
paul@86 | 31 | interrupt_handler: |
paul@127 | 32 | /* gp should have been set in the entrypoint. */ |
paul@127 | 33 | |
paul@114 | 34 | jal save_state |
paul@114 | 35 | nop |
paul@114 | 36 | |
paul@114 | 37 | /* Invoke the handler. */ |
paul@114 | 38 | |
paul@114 | 39 | jal irq_handle |
paul@114 | 40 | nop |
paul@114 | 41 | |
paul@133 | 42 | /* Switch the stack pointer. */ |
paul@133 | 43 | |
paul@133 | 44 | la $k0, current_stack_pointer |
paul@145 | 45 | lw $k1, 0($k0) /* &stack_pointers[current_task] */ |
paul@145 | 46 | lw $sp, 0($k1) |
paul@133 | 47 | |
paul@133 | 48 | /* Set the current task ASID. */ |
paul@133 | 49 | |
paul@133 | 50 | la $k0, current_task |
paul@133 | 51 | lw $k1, 0($k0) |
paul@140 | 52 | mtc0 $k1, $10 /* CP0_ENTRYHI */ |
paul@133 | 53 | nop |
paul@133 | 54 | |
paul@145 | 55 | /* Obtain the current task's registers. */ |
paul@145 | 56 | |
paul@114 | 57 | j load_and_return |
paul@114 | 58 | nop |
paul@114 | 59 | |
paul@114 | 60 | save_state: |
paul@145 | 61 | /* Obtain a store of registers for the current task. */ |
paul@145 | 62 | |
paul@145 | 63 | la $k0, current_registers |
paul@145 | 64 | lw $k1, 0($k0) |
paul@145 | 65 | |
paul@145 | 66 | sw $at, 4($k1) |
paul@145 | 67 | sw $v0, 8($k1) |
paul@145 | 68 | sw $v1, 12($k1) |
paul@145 | 69 | sw $a0, 16($k1) |
paul@145 | 70 | sw $a1, 20($k1) |
paul@145 | 71 | sw $a2, 24($k1) |
paul@145 | 72 | sw $a3, 28($k1) |
paul@145 | 73 | sw $t0, 32($k1) |
paul@145 | 74 | sw $t1, 36($k1) |
paul@145 | 75 | sw $t2, 40($k1) |
paul@145 | 76 | sw $t3, 44($k1) |
paul@145 | 77 | sw $t4, 48($k1) |
paul@145 | 78 | sw $t5, 52($k1) |
paul@145 | 79 | sw $t6, 56($k1) |
paul@145 | 80 | sw $t7, 60($k1) |
paul@145 | 81 | sw $s0, 64($k1) |
paul@145 | 82 | sw $s1, 68($k1) |
paul@145 | 83 | sw $s2, 72($k1) |
paul@145 | 84 | sw $s3, 76($k1) |
paul@145 | 85 | sw $s4, 80($k1) |
paul@145 | 86 | sw $s5, 84($k1) |
paul@145 | 87 | sw $s6, 88($k1) |
paul@145 | 88 | sw $s7, 92($k1) |
paul@145 | 89 | sw $t8, 96($k1) |
paul@145 | 90 | |
paul@145 | 91 | /* t9, gp and ra are already saved */ |
paul@145 | 92 | |
paul@145 | 93 | sw $fp, 108($k1) |
paul@68 | 94 | |
paul@140 | 95 | mfc0 $k0, $14 /* CP0_EPC */ |
paul@110 | 96 | nop |
paul@145 | 97 | sw $k0, 116($k1) |
paul@110 | 98 | |
paul@148 | 99 | mflo $k0 |
paul@148 | 100 | sw $k0, 120($k1) |
paul@148 | 101 | mfhi $k0 |
paul@148 | 102 | sw $k0, 124($k1) |
paul@148 | 103 | |
paul@159 | 104 | jr $ra |
paul@114 | 105 | nop |
paul@71 | 106 | |
paul@114 | 107 | load_and_return: |
paul@145 | 108 | /* Obtain a store of registers for the current task. */ |
paul@68 | 109 | |
paul@145 | 110 | la $k0, current_registers |
paul@145 | 111 | lw $k1, 0($k0) |
paul@68 | 112 | |
paul@145 | 113 | lw $at, 4($k1) |
paul@145 | 114 | lw $v0, 8($k1) |
paul@145 | 115 | lw $v1, 12($k1) |
paul@145 | 116 | lw $a0, 16($k1) |
paul@145 | 117 | lw $a1, 20($k1) |
paul@145 | 118 | lw $a2, 24($k1) |
paul@145 | 119 | lw $a3, 28($k1) |
paul@145 | 120 | lw $t0, 32($k1) |
paul@145 | 121 | lw $t1, 36($k1) |
paul@145 | 122 | lw $t2, 40($k1) |
paul@145 | 123 | lw $t3, 44($k1) |
paul@145 | 124 | lw $t4, 48($k1) |
paul@145 | 125 | lw $t5, 52($k1) |
paul@145 | 126 | lw $t6, 56($k1) |
paul@145 | 127 | lw $t7, 60($k1) |
paul@145 | 128 | lw $s0, 64($k1) |
paul@145 | 129 | lw $s1, 68($k1) |
paul@145 | 130 | lw $s2, 72($k1) |
paul@145 | 131 | lw $s3, 76($k1) |
paul@145 | 132 | lw $s4, 80($k1) |
paul@145 | 133 | lw $s5, 84($k1) |
paul@145 | 134 | lw $s6, 88($k1) |
paul@145 | 135 | lw $s7, 92($k1) |
paul@145 | 136 | lw $t8, 96($k1) |
paul@145 | 137 | lw $t9, 100($k1) |
paul@145 | 138 | lw $gp, 104($k1) |
paul@145 | 139 | lw $fp, 108($k1) |
paul@145 | 140 | lw $ra, 112($k1) |
paul@86 | 141 | |
paul@145 | 142 | lw $k0, 116($k1) |
paul@140 | 143 | mtc0 $k0, $14 /* CP0_EPC */ |
paul@110 | 144 | nop |
paul@110 | 145 | |
paul@158 | 146 | lw $k0, 120($k1) |
paul@148 | 147 | mtlo $k0 |
paul@158 | 148 | lw $k0, 124($k1) |
paul@148 | 149 | mthi $k0 |
paul@148 | 150 | |
paul@167 | 151 | lui $k0, %hi(_enter_task) |
paul@167 | 152 | ori $k0, $k0, %lo(_enter_task) |
paul@167 | 153 | lw $k1, 0($k0) |
paul@167 | 154 | jr $k1 |
paul@167 | 155 | nop |
paul@167 | 156 | |
paul@167 | 157 | enter_task: |
paul@86 | 158 | eret |
paul@86 | 159 | nop |
paul@68 | 160 | |
paul@68 | 161 | .set reorder |
paul@68 | 162 | .set at |