1// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14#include "riscv/rvruntime-frames.h"
15
16/* The riscv panic handler in components/riscv/vectors.S doesn't allow the panic
17   handler function to return.
18
19   However, for the purposes of this test we want to allow the panic handler to return.
20
21   There is functionality in vectors.S restore the CPU state, but it only
22   restores when CONTEXT_SIZE registers are
23   pushed onto the stack not RV_STK_FRMSZ registers
24
25   Instead of messing with that, implement a full "restore from RvExcFrame"
26   function here which restores the CPU and then
27   returns from exception.
28
29   Called as return_from_panic_handler(RvExcFrame *frame)
30*/
31.global return_from_panic_handler
32return_from_panic_handler:
33    or t0, a0, a0 /* use t0 as the working register */
34
35    /* save general registers */
36    lw ra,  RV_STK_RA(t0)
37    lw sp,  RV_STK_SP(t0)
38    lw gp,  RV_STK_GP(t0)
39    lw tp,  RV_STK_TP(t0)
40    lw s0,  RV_STK_S0(t0)
41    lw s1,  RV_STK_S1(t0)
42    lw a0,  RV_STK_A0(t0)
43    lw a1,  RV_STK_A1(t0)
44    lw a2,  RV_STK_A2(t0)
45    lw a3,  RV_STK_A3(t0)
46    lw a4,  RV_STK_A4(t0)
47    lw a5,  RV_STK_A5(t0)
48    lw a6,  RV_STK_A6(t0)
49    lw a7,  RV_STK_A7(t0)
50    lw s2,  RV_STK_S2(t0)
51    lw s3,  RV_STK_S3(t0)
52    lw s4,  RV_STK_S4(t0)
53    lw s5,  RV_STK_S5(t0)
54    lw s6,  RV_STK_S6(t0)
55    lw s7,  RV_STK_S7(t0)
56    lw s8,  RV_STK_S8(t0)
57    lw s9,  RV_STK_S9(t0)
58    lw s10, RV_STK_S10(t0)
59    lw s11, RV_STK_S11(t0)
60    lw t3,  RV_STK_T3(t0)
61    lw t4,  RV_STK_T4(t0)
62    lw t5,  RV_STK_T5(t0)
63    lw t6,  RV_STK_T6(t0)
64
65    lw t2,  RV_STK_MEPC(t0)
66    csrw mepc, t2
67
68    lw t1,  RV_STK_T1(t0)
69    lw t2,  RV_STK_T2(t0)
70    lw t0,  RV_STK_T0(t0)
71    mret
72