|
| 1 | +;/* |
| 2 | +; * File : context_gcc.S |
| 3 | +; * This file is part of RT-Thread RTOS |
| 4 | +; * COPYRIGHT (C) 2018, RT-Thread Development Team |
| 5 | +; * |
| 6 | +; * This program is free software; you can redistribute it and/or modify |
| 7 | +; * it under the terms of the GNU General Public License as published by |
| 8 | +; * the Free Software Foundation; either version 2 of the License, or |
| 9 | +; * (at your option) any later version. |
| 10 | +; * |
| 11 | +; * This program is distributed in the hope that it will be useful, |
| 12 | +; * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +; * GNU General Public License for more details. |
| 15 | +; * |
| 16 | +; * You should have received a copy of the GNU General Public License along |
| 17 | +; * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | +; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | +; * |
| 20 | +; * Change Logs: |
| 21 | +; * Date Author Notes |
| 22 | +; * 2017-07-16 zhangjun for hifive1 |
| 23 | +; * 2018-05-29 tanek optimize rt_hw_interrupt_* |
| 24 | +; * 2018-05-29 tanek add mie register to context |
| 25 | +; */ |
| 26 | + |
| 27 | +/* |
| 28 | + * rt_base_t rt_hw_interrupt_disable(void); |
| 29 | + */ |
| 30 | + .globl rt_hw_interrupt_disable |
| 31 | +rt_hw_interrupt_disable: |
| 32 | + csrrci a0, mstatus, 8 |
| 33 | + ret |
| 34 | + |
| 35 | +/* |
| 36 | + * void rt_hw_interrupt_enable(rt_base_t level); |
| 37 | + */ |
| 38 | + .globl rt_hw_interrupt_enable |
| 39 | +rt_hw_interrupt_enable: |
| 40 | + csrw mstatus, a0 |
| 41 | + ret |
| 42 | + |
| 43 | +/* |
| 44 | + * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to); |
| 45 | + * a0 --> from |
| 46 | + * a1 --> to |
| 47 | + */ |
| 48 | + .globl rt_hw_context_switch |
| 49 | +rt_hw_context_switch: |
| 50 | + |
| 51 | + /* saved from thread context |
| 52 | + * x1/ra -> sp(0) |
| 53 | + * x1/ra -> sp(1) |
| 54 | + * mstatus.mie -> sp(2) |
| 55 | + * x(i) -> sp(i-4) |
| 56 | + */ |
| 57 | + addi sp, sp, -32 * 4 |
| 58 | + sw sp, (a0) |
| 59 | + |
| 60 | + sw x1, 0 * 4(sp) |
| 61 | + sw x1, 1 * 4(sp) |
| 62 | + |
| 63 | + csrr a0, mstatus |
| 64 | + andi a0, a0, 8 |
| 65 | + beqz a0, save_mpie |
| 66 | + li a0, 0x80 |
| 67 | +save_mpie: |
| 68 | + sw a0, 2 * 4(sp) |
| 69 | + |
| 70 | + sw x4, 4 * 4(sp) |
| 71 | + sw x5, 5 * 4(sp) |
| 72 | + sw x6, 6 * 4(sp) |
| 73 | + sw x7, 7 * 4(sp) |
| 74 | + sw x8, 8 * 4(sp) |
| 75 | + sw x9, 9 * 4(sp) |
| 76 | + sw x10, 10 * 4(sp) |
| 77 | + sw x11, 11 * 4(sp) |
| 78 | + sw x12, 12 * 4(sp) |
| 79 | + sw x13, 13 * 4(sp) |
| 80 | + sw x14, 14 * 4(sp) |
| 81 | + sw x15, 15 * 4(sp) |
| 82 | + sw x16, 16 * 4(sp) |
| 83 | + sw x17, 17 * 4(sp) |
| 84 | + sw x18, 18 * 4(sp) |
| 85 | + sw x19, 19 * 4(sp) |
| 86 | + sw x20, 20 * 4(sp) |
| 87 | + sw x21, 21 * 4(sp) |
| 88 | + sw x22, 22 * 4(sp) |
| 89 | + sw x23, 23 * 4(sp) |
| 90 | + sw x24, 24 * 4(sp) |
| 91 | + sw x25, 25 * 4(sp) |
| 92 | + sw x26, 26 * 4(sp) |
| 93 | + sw x27, 27 * 4(sp) |
| 94 | + sw x28, 28 * 4(sp) |
| 95 | + sw x29, 29 * 4(sp) |
| 96 | + sw x30, 30 * 4(sp) |
| 97 | + sw x31, 31 * 4(sp) |
| 98 | + |
| 99 | + /* restore to thread context |
| 100 | + * sp(0) -> epc; |
| 101 | + * sp(1) -> ra; |
| 102 | + * sp(i) -> x(i+2) |
| 103 | + */ |
| 104 | + lw sp, (a1) |
| 105 | + |
| 106 | + /* resw ra to mepc */ |
| 107 | + lw a1, 0 * 4(sp) |
| 108 | + csrw mepc, a1 |
| 109 | + lw x1, 1 * 4(sp) |
| 110 | + |
| 111 | + /* force to machin mode(MPP=11) */ |
| 112 | + li a1, 0x00001800; |
| 113 | + csrs mstatus, a1 |
| 114 | + lw a1, 2 * 4(sp) |
| 115 | + csrs mstatus, a1 |
| 116 | + |
| 117 | + lw x4, 4 * 4(sp) |
| 118 | + lw x5, 5 * 4(sp) |
| 119 | + lw x6, 6 * 4(sp) |
| 120 | + lw x7, 7 * 4(sp) |
| 121 | + lw x8, 8 * 4(sp) |
| 122 | + lw x9, 9 * 4(sp) |
| 123 | + lw x10, 10 * 4(sp) |
| 124 | + lw x11, 11 * 4(sp) |
| 125 | + lw x12, 12 * 4(sp) |
| 126 | + lw x13, 13 * 4(sp) |
| 127 | + lw x14, 14 * 4(sp) |
| 128 | + lw x15, 15 * 4(sp) |
| 129 | + lw x16, 16 * 4(sp) |
| 130 | + lw x17, 17 * 4(sp) |
| 131 | + lw x18, 18 * 4(sp) |
| 132 | + lw x19, 19 * 4(sp) |
| 133 | + lw x20, 20 * 4(sp) |
| 134 | + lw x21, 21 * 4(sp) |
| 135 | + lw x22, 22 * 4(sp) |
| 136 | + lw x23, 23 * 4(sp) |
| 137 | + lw x24, 24 * 4(sp) |
| 138 | + lw x25, 25 * 4(sp) |
| 139 | + lw x26, 26 * 4(sp) |
| 140 | + lw x27, 27 * 4(sp) |
| 141 | + lw x28, 28 * 4(sp) |
| 142 | + lw x29, 29 * 4(sp) |
| 143 | + lw x30, 30 * 4(sp) |
| 144 | + lw x31, 31 * 4(sp) |
| 145 | + |
| 146 | + addi sp, sp, 32 * 4 |
| 147 | + mret |
| 148 | + |
| 149 | +/* |
| 150 | + * void rt_hw_context_switch_to(rt_uint32 to); |
| 151 | + * a0 --> to |
| 152 | + */ |
| 153 | + .globl rt_hw_context_switch_to |
| 154 | +rt_hw_context_switch_to: |
| 155 | + lw sp, (a0) |
| 156 | + |
| 157 | + /* load epc from stack */ |
| 158 | + lw a0, 0 * 4(sp) |
| 159 | + csrw mepc, a0 |
| 160 | + lw x1, 1 * 4(sp) |
| 161 | + /* load mstatus from stack */ |
| 162 | + lw a0, 2 * 4(sp) |
| 163 | + csrw mstatus, a0 |
| 164 | + lw x4, 4 * 4(sp) |
| 165 | + lw x5, 5 * 4(sp) |
| 166 | + lw x6, 6 * 4(sp) |
| 167 | + lw x7, 7 * 4(sp) |
| 168 | + lw x8, 8 * 4(sp) |
| 169 | + lw x9, 9 * 4(sp) |
| 170 | + lw x10, 10 * 4(sp) |
| 171 | + lw x11, 11 * 4(sp) |
| 172 | + lw x12, 12 * 4(sp) |
| 173 | + lw x13, 13 * 4(sp) |
| 174 | + lw x14, 14 * 4(sp) |
| 175 | + lw x15, 15 * 4(sp) |
| 176 | + lw x16, 16 * 4(sp) |
| 177 | + lw x17, 17 * 4(sp) |
| 178 | + lw x18, 18 * 4(sp) |
| 179 | + lw x19, 19 * 4(sp) |
| 180 | + lw x20, 20 * 4(sp) |
| 181 | + lw x21, 21 * 4(sp) |
| 182 | + lw x22, 22 * 4(sp) |
| 183 | + lw x23, 23 * 4(sp) |
| 184 | + lw x24, 24 * 4(sp) |
| 185 | + lw x25, 25 * 4(sp) |
| 186 | + lw x26, 26 * 4(sp) |
| 187 | + lw x27, 27 * 4(sp) |
| 188 | + lw x28, 28 * 4(sp) |
| 189 | + lw x29, 29 * 4(sp) |
| 190 | + lw x30, 30 * 4(sp) |
| 191 | + lw x31, 31 * 4(sp) |
| 192 | + |
| 193 | + addi sp, sp, 32 * 4 |
| 194 | + mret |
| 195 | + |
| 196 | +/* |
| 197 | + * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to); |
| 198 | + */ |
| 199 | + .globl rt_thread_switch_interrupt_flag |
| 200 | + .globl rt_interrupt_from_thread |
| 201 | + .globl rt_interrupt_to_thread |
| 202 | + .globl rt_hw_context_switch_interrupt |
| 203 | +rt_hw_context_switch_interrupt: |
| 204 | + addi sp, sp, -16 |
| 205 | + sw s0, 12(sp) |
| 206 | + sw a0, 8(sp) |
| 207 | + sw a5, 4(sp) |
| 208 | + |
| 209 | + la a0, rt_thread_switch_interrupt_flag |
| 210 | + lw a5, (a0) |
| 211 | + bnez a5, _reswitch |
| 212 | + li a5, 1 |
| 213 | + sw a5, (a0) |
| 214 | + |
| 215 | + la a5, rt_interrupt_from_thread |
| 216 | + lw a0, 8(sp) |
| 217 | + sw a0, (a5) |
| 218 | + |
| 219 | +_reswitch: |
| 220 | + la a5, rt_interrupt_to_thread |
| 221 | + sw a1, (a5) |
| 222 | + |
| 223 | + lw a5, 4(sp) |
| 224 | + lw a0, 8(sp) |
| 225 | + lw s0, 12(sp) |
| 226 | + addi sp, sp, 16 |
| 227 | + ret |
0 commit comments