]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/exception.S
Remove block of dead code
[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 __FBSDID("$FreeBSD$");
37
38 #include "assym.inc"
39
40 #include <machine/trap.h>
41 #include <machine/riscvreg.h>
42
43 .macro save_registers el
44         addi    sp, sp, -(TF_SIZE)
45
46         sd      ra, (TF_RA)(sp)
47         sd      tp, (TF_TP)(sp)
48
49 .if \el == 0    /* We came from userspace. Load our pcpu */
50         sd      gp, (TF_GP)(sp)
51         ld      gp, (TF_SIZE)(sp)
52 .endif
53
54         sd      t0, (TF_T + 0 * 8)(sp)
55         sd      t1, (TF_T + 1 * 8)(sp)
56         sd      t2, (TF_T + 2 * 8)(sp)
57         sd      t3, (TF_T + 3 * 8)(sp)
58         sd      t4, (TF_T + 4 * 8)(sp)
59         sd      t5, (TF_T + 5 * 8)(sp)
60         sd      t6, (TF_T + 6 * 8)(sp)
61
62         sd      s0, (TF_S + 0 * 8)(sp)
63         sd      s1, (TF_S + 1 * 8)(sp)
64         sd      s2, (TF_S + 2 * 8)(sp)
65         sd      s3, (TF_S + 3 * 8)(sp)
66         sd      s4, (TF_S + 4 * 8)(sp)
67         sd      s5, (TF_S + 5 * 8)(sp)
68         sd      s6, (TF_S + 6 * 8)(sp)
69         sd      s7, (TF_S + 7 * 8)(sp)
70         sd      s8, (TF_S + 8 * 8)(sp)
71         sd      s9, (TF_S + 9 * 8)(sp)
72         sd      s10, (TF_S + 10 * 8)(sp)
73         sd      s11, (TF_S + 11 * 8)(sp)
74
75         sd      a0, (TF_A + 0 * 8)(sp)
76         sd      a1, (TF_A + 1 * 8)(sp)
77         sd      a2, (TF_A + 2 * 8)(sp)
78         sd      a3, (TF_A + 3 * 8)(sp)
79         sd      a4, (TF_A + 4 * 8)(sp)
80         sd      a5, (TF_A + 5 * 8)(sp)
81         sd      a6, (TF_A + 6 * 8)(sp)
82         sd      a7, (TF_A + 7 * 8)(sp)
83
84 .if \el == 1
85         /* Store kernel sp */
86         li      t1, TF_SIZE
87         add     t0, sp, t1
88         sd      t0, (TF_SP)(sp)
89 .else
90         /* Store user sp */
91         csrr    t0, sscratch
92         sd      t0, (TF_SP)(sp)
93 .endif
94         li      t0, 0
95         csrw    sscratch, t0
96         csrr    t0, sepc
97         sd      t0, (TF_SEPC)(sp)
98         csrr    t0, sstatus
99         sd      t0, (TF_SSTATUS)(sp)
100         csrr    t0, stval
101         sd      t0, (TF_STVAL)(sp)
102         csrr    t0, scause
103         sd      t0, (TF_SCAUSE)(sp)
104 .endm
105
106 .macro load_registers el
107         ld      t0, (TF_SSTATUS)(sp)
108 .if \el == 0
109         /* Ensure user interrupts will be enabled on eret */
110         li      t1, SSTATUS_SPIE
111         or      t0, t0, t1
112 .else
113         /*
114          * Disable interrupts for supervisor mode exceptions.
115          * For user mode exceptions we have already done this
116          * in do_ast.
117          */
118         li      t1, ~SSTATUS_SIE
119         and     t0, t0, t1
120 .endif
121         csrw    sstatus, t0
122
123         ld      t0, (TF_SEPC)(sp)
124         csrw    sepc, t0
125
126 .if \el == 0
127         /* We go to userspace. Load user sp */
128         ld      t0, (TF_SP)(sp)
129         csrw    sscratch, t0
130
131         /* And store our pcpu */
132         sd      gp, (TF_SIZE)(sp)
133         ld      gp, (TF_GP)(sp)
134 .endif
135
136         ld      ra, (TF_RA)(sp)
137         ld      tp, (TF_TP)(sp)
138
139         ld      t0, (TF_T + 0 * 8)(sp)
140         ld      t1, (TF_T + 1 * 8)(sp)
141         ld      t2, (TF_T + 2 * 8)(sp)
142         ld      t3, (TF_T + 3 * 8)(sp)
143         ld      t4, (TF_T + 4 * 8)(sp)
144         ld      t5, (TF_T + 5 * 8)(sp)
145         ld      t6, (TF_T + 6 * 8)(sp)
146
147         ld      s0, (TF_S + 0 * 8)(sp)
148         ld      s1, (TF_S + 1 * 8)(sp)
149         ld      s2, (TF_S + 2 * 8)(sp)
150         ld      s3, (TF_S + 3 * 8)(sp)
151         ld      s4, (TF_S + 4 * 8)(sp)
152         ld      s5, (TF_S + 5 * 8)(sp)
153         ld      s6, (TF_S + 6 * 8)(sp)
154         ld      s7, (TF_S + 7 * 8)(sp)
155         ld      s8, (TF_S + 8 * 8)(sp)
156         ld      s9, (TF_S + 9 * 8)(sp)
157         ld      s10, (TF_S + 10 * 8)(sp)
158         ld      s11, (TF_S + 11 * 8)(sp)
159
160         ld      a0, (TF_A + 0 * 8)(sp)
161         ld      a1, (TF_A + 1 * 8)(sp)
162         ld      a2, (TF_A + 2 * 8)(sp)
163         ld      a3, (TF_A + 3 * 8)(sp)
164         ld      a4, (TF_A + 4 * 8)(sp)
165         ld      a5, (TF_A + 5 * 8)(sp)
166         ld      a6, (TF_A + 6 * 8)(sp)
167         ld      a7, (TF_A + 7 * 8)(sp)
168
169         addi    sp, sp, (TF_SIZE)
170 .endm
171
172 .macro  do_ast
173         /* Disable interrupts */
174         csrr    a4, sstatus
175 1:
176         csrci   sstatus, (SSTATUS_SIE)
177
178         ld      a1, PC_CURTHREAD(gp)
179         lw      a2, TD_FLAGS(a1)
180
181         li      a3, (TDF_ASTPENDING|TDF_NEEDRESCHED)
182         and     a2, a2, a3
183         beqz    a2, 2f
184
185         /* Restore interrupts */
186         andi    a4, a4, (SSTATUS_SIE)
187         csrs    sstatus, a4
188
189         /* Handle the ast */
190         mv      a0, sp
191         call    _C_LABEL(ast)
192
193         /* Re-check for new ast scheduled */
194         j       1b
195 2:
196 .endm
197
198 ENTRY(cpu_exception_handler)
199         csrrw   sp, sscratch, sp
200         beqz    sp, 1f
201         /* User mode detected */
202         csrrw   sp, sscratch, sp
203         j       cpu_exception_handler_user
204 1:
205         /* Supervisor mode detected */
206         csrrw   sp, sscratch, sp
207         j       cpu_exception_handler_supervisor
208 END(cpu_exception_handler)
209
210 ENTRY(cpu_exception_handler_supervisor)
211         save_registers 1
212         mv      a0, sp
213         call    _C_LABEL(do_trap_supervisor)
214         load_registers 1
215         sret
216 END(cpu_exception_handler_supervisor)
217
218 ENTRY(cpu_exception_handler_user)
219         csrrw   sp, sscratch, sp
220         save_registers 0
221         mv      a0, sp
222         call    _C_LABEL(do_trap_user)
223         do_ast
224         load_registers 0
225         csrrw   sp, sscratch, sp
226         sret
227 END(cpu_exception_handler_user)