]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/exception.S
Merge llvm-project release/17.x llvmorg-17.0.3-0-g888437e1b600
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / exception.S
1 /*-
2  * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * Portions of this software were developed by SRI International and the
6  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * Portions of this software were developed by the University of Cambridge
10  * Computer Laboratory as part of the CTSRD Project, with support from the
11  * UK Higher Education Innovation Fund (HEIF).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <machine/asm.h>
36 #include "assym.inc"
37
38 #include <machine/trap.h>
39 #include <machine/riscvreg.h>
40
41 .macro save_registers mode
42         addi    sp, sp, -(TF_SIZE)
43
44         sd      ra, (TF_RA)(sp)
45         sd      tp, (TF_TP)(sp)
46         sd      gp, (TF_GP)(sp)
47
48 .if \mode == 0  /* We came from userspace. */
49 .option push
50 .option norelax
51         /* Load the kernel's global pointer */
52         la      gp, __global_pointer$
53 .option pop
54
55         /* Load our pcpu */
56         ld      tp, (TF_SIZE)(sp)
57 .endif
58
59         sd      t0, (TF_T + 0 * 8)(sp)
60         sd      t1, (TF_T + 1 * 8)(sp)
61         sd      t2, (TF_T + 2 * 8)(sp)
62         sd      t3, (TF_T + 3 * 8)(sp)
63         sd      t4, (TF_T + 4 * 8)(sp)
64         sd      t5, (TF_T + 5 * 8)(sp)
65         sd      t6, (TF_T + 6 * 8)(sp)
66
67         sd      s0, (TF_S + 0 * 8)(sp)
68         sd      s1, (TF_S + 1 * 8)(sp)
69         sd      s2, (TF_S + 2 * 8)(sp)
70         sd      s3, (TF_S + 3 * 8)(sp)
71         sd      s4, (TF_S + 4 * 8)(sp)
72         sd      s5, (TF_S + 5 * 8)(sp)
73         sd      s6, (TF_S + 6 * 8)(sp)
74         sd      s7, (TF_S + 7 * 8)(sp)
75         sd      s8, (TF_S + 8 * 8)(sp)
76         sd      s9, (TF_S + 9 * 8)(sp)
77         sd      s10, (TF_S + 10 * 8)(sp)
78         sd      s11, (TF_S + 11 * 8)(sp)
79
80         sd      a0, (TF_A + 0 * 8)(sp)
81         sd      a1, (TF_A + 1 * 8)(sp)
82         sd      a2, (TF_A + 2 * 8)(sp)
83         sd      a3, (TF_A + 3 * 8)(sp)
84         sd      a4, (TF_A + 4 * 8)(sp)
85         sd      a5, (TF_A + 5 * 8)(sp)
86         sd      a6, (TF_A + 6 * 8)(sp)
87         sd      a7, (TF_A + 7 * 8)(sp)
88
89 .if \mode == 1
90         /* Store kernel sp */
91         li      t1, TF_SIZE
92         add     t0, sp, t1
93         sd      t0, (TF_SP)(sp)
94 .else
95         /* Store user sp */
96         csrr    t0, sscratch
97         sd      t0, (TF_SP)(sp)
98 .endif
99         li      t0, 0
100         csrw    sscratch, t0
101         csrr    t0, sepc
102         sd      t0, (TF_SEPC)(sp)
103         csrr    t0, sstatus
104         sd      t0, (TF_SSTATUS)(sp)
105 .if \mode == 1
106         /* Disable user address access for supervisor mode exceptions. */
107         li      t0, SSTATUS_SUM
108         csrc    sstatus, t0
109 .endif
110         csrr    t0, stval
111         sd      t0, (TF_STVAL)(sp)
112         csrr    t0, scause
113         sd      t0, (TF_SCAUSE)(sp)
114 .endm
115
116 .macro load_registers mode
117         ld      t0, (TF_SSTATUS)(sp)
118 .if \mode == 0
119         /* Ensure user interrupts will be enabled on eret */
120         li      t1, SSTATUS_SPIE
121         or      t0, t0, t1
122 .else
123         /*
124          * Disable interrupts for supervisor mode exceptions.
125          * For user mode exceptions we have already done this
126          * in do_ast.
127          */
128         li      t1, ~SSTATUS_SIE
129         and     t0, t0, t1
130 .endif
131         csrw    sstatus, t0
132
133         ld      t0, (TF_SEPC)(sp)
134         csrw    sepc, t0
135
136 .if \mode == 0
137         /* We go to userspace. Load user sp */
138         ld      t0, (TF_SP)(sp)
139         csrw    sscratch, t0
140
141         /* Store our pcpu */
142         sd      tp, (TF_SIZE)(sp)
143         ld      tp, (TF_TP)(sp)
144
145         /* And restore the user's global pointer */
146         ld      gp, (TF_GP)(sp)
147 .endif
148
149         ld      ra, (TF_RA)(sp)
150
151         ld      t0, (TF_T + 0 * 8)(sp)
152         ld      t1, (TF_T + 1 * 8)(sp)
153         ld      t2, (TF_T + 2 * 8)(sp)
154         ld      t3, (TF_T + 3 * 8)(sp)
155         ld      t4, (TF_T + 4 * 8)(sp)
156         ld      t5, (TF_T + 5 * 8)(sp)
157         ld      t6, (TF_T + 6 * 8)(sp)
158
159         ld      s0, (TF_S + 0 * 8)(sp)
160         ld      s1, (TF_S + 1 * 8)(sp)
161         ld      s2, (TF_S + 2 * 8)(sp)
162         ld      s3, (TF_S + 3 * 8)(sp)
163         ld      s4, (TF_S + 4 * 8)(sp)
164         ld      s5, (TF_S + 5 * 8)(sp)
165         ld      s6, (TF_S + 6 * 8)(sp)
166         ld      s7, (TF_S + 7 * 8)(sp)
167         ld      s8, (TF_S + 8 * 8)(sp)
168         ld      s9, (TF_S + 9 * 8)(sp)
169         ld      s10, (TF_S + 10 * 8)(sp)
170         ld      s11, (TF_S + 11 * 8)(sp)
171
172         ld      a0, (TF_A + 0 * 8)(sp)
173         ld      a1, (TF_A + 1 * 8)(sp)
174         ld      a2, (TF_A + 2 * 8)(sp)
175         ld      a3, (TF_A + 3 * 8)(sp)
176         ld      a4, (TF_A + 4 * 8)(sp)
177         ld      a5, (TF_A + 5 * 8)(sp)
178         ld      a6, (TF_A + 6 * 8)(sp)
179         ld      a7, (TF_A + 7 * 8)(sp)
180
181         addi    sp, sp, (TF_SIZE)
182 .endm
183
184 .macro  do_ast
185         /* Disable interrupts */
186         csrr    a4, sstatus
187 1:
188         csrci   sstatus, (SSTATUS_SIE)
189
190         ld      a1, PC_CURTHREAD(tp)
191         lw      a2, TD_AST(a1)
192
193         beqz    a2, 2f
194
195         /* Restore interrupts */
196         andi    a4, a4, (SSTATUS_SIE)
197         csrs    sstatus, a4
198
199         /* Handle the ast */
200         mv      a0, sp
201         call    _C_LABEL(ast)
202
203         /* Re-check for new ast scheduled */
204         j       1b
205 2:
206 .endm
207
208 ENTRY(cpu_exception_handler)
209         csrrw   sp, sscratch, sp
210         beqz    sp, 1f
211         /* User mode detected */
212         j       cpu_exception_handler_user
213 1:
214         /* Supervisor mode detected */
215         csrrw   sp, sscratch, sp
216         j       cpu_exception_handler_supervisor
217 END(cpu_exception_handler)
218
219 ENTRY(cpu_exception_handler_supervisor)
220         save_registers 1
221         mv      a0, sp
222         call    _C_LABEL(do_trap_supervisor)
223         load_registers 1
224         sret
225 END(cpu_exception_handler_supervisor)
226
227 ENTRY(cpu_exception_handler_user)
228         save_registers 0
229         mv      a0, sp
230         call    _C_LABEL(do_trap_user)
231         do_ast
232         load_registers 0
233         csrrw   sp, sscratch, sp
234         sret
235 END(cpu_exception_handler_user)