summaryrefslogtreecommitdiff
path: root/libcpu/risc-v/common64/interrupt_gcc.S
blob: 5b1f28666881d903b931b47b97f0be54a19614bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018/10/02     Bernard      The first version
 * 2018/12/27     Jesven       Add SMP schedule
 * 2021/02/02     lizhirui     Add userspace support
 * 2021/12/24     JasonHu      Add user setting save/restore
 * 2022/10/22     Shell        Support kernel mode RVV;
 *                             Rewrite trap handling routine
 */

#include "cpuport.h"
#include "encoding.h"
#include "stackframe.h"

    .align 2
    .global trap_entry
    .global debug_check_sp
trap_entry:
    // distingush exception from kernel or user
    csrrw   sp, sscratch, sp
    bnez    sp, _save_context

    // BE REALLY careful with sscratch,
    // if it's wrong, we could looping here forever
    // or accessing random memory and seeing things totally
    // messy after a long time and don't even know why
_from_kernel:
    csrr    sp, sscratch
    j _save_context

_save_context:
    SAVE_ALL
    // clear sscratch to say 'now in kernel mode'
    csrw    sscratch, zero

    RESTORE_SYS_GP

    // now we are ready to enter interrupt / excepiton handler
_distinguish_syscall:
    csrr    t0, scause
#ifdef RT_USING_SMART
    // TODO swap 8 with config macro name
    li      t1, 8
    bne     t0, t1, _handle_interrupt_and_exception
    call syscall_entry
    // syscall never return here
#endif

_handle_interrupt_and_exception:
    mv      a0, t0
    csrrc   a1, stval, zero
    csrr    a2, sepc
    // sp as exception frame pointer
    mv      a3, sp
    call    handle_trap

_interrupt_exit:
#ifndef RT_USING_SMP
    la      s0, rt_thread_switch_interrupt_flag
    lw      s2, 0(s0)
    beqz    s2, _resume_execution
    sw      zero, 0(s0)
#else
    mv      a0, sp
    call    rt_scheduler_do_irq_switch
    // if failed, jump to __resume_execution
    j       _resume_execution
#endif /* RT_USING_SMP */

_context_switch:
    la      t0, rt_interrupt_from_thread
    LOAD    a0, 0(t0)
    la      t0, rt_interrupt_to_thread
    LOAD    a1, 0(t0)
    csrr    t0, sstatus
    andi    t0, t0, ~SSTATUS_SPIE
    csrw    sstatus, t0
    jal     rt_hw_context_switch

_resume_execution:
#ifdef RT_USING_SMART
    LOAD    t0, FRAME_OFF_SSTATUS(sp)
    andi    t0, t0, SSTATUS_SPP
    bnez    t0, _resume_kernel
    call arch_ret_to_user
#endif

_resume_kernel:
    RESTORE_ALL
    csrw    sscratch, zero
    sret

#ifndef RT_USING_SMP
.global rt_hw_interrupt_enable
rt_hw_interrupt_enable:
    csrs sstatus, a0    /* restore to old csr */
    jr ra

.global rt_hw_interrupt_disable
rt_hw_interrupt_disable:
    csrrci a0, sstatus, 2   /* clear SIE */
    jr ra
#else
.global rt_hw_local_irq_disable
rt_hw_local_irq_disable:
    csrrci a0, sstatus, 2
    jr ra

.global rt_hw_local_irq_enable
rt_hw_local_irq_enable:
    csrs sstatus, a0
    jr ra

.global rt_hw_secondary_cpu_idle_exec
rt_hw_secondary_cpu_idle_exec:
    jr ra
#endif  /* RT_USING_SMP */