]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/exception.S
Enable arm64 SError exceptions in the kernel
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / exception.S
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <machine/asm.h>
29 #include <machine/armreg.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "assym.inc"
33
34         .text
35
36 .macro  save_registers el
37 .if \el == 1
38         mov     x18, sp
39         sub     sp, sp, #128
40 .endif
41         sub     sp, sp, #(TF_SIZE)
42         stp     x28, x29, [sp, #(TF_X + 28 * 8)]
43         stp     x26, x27, [sp, #(TF_X + 26 * 8)]
44         stp     x24, x25, [sp, #(TF_X + 24 * 8)]
45         stp     x22, x23, [sp, #(TF_X + 22 * 8)]
46         stp     x20, x21, [sp, #(TF_X + 20 * 8)]
47         stp     x18, x19, [sp, #(TF_X + 18 * 8)]
48         stp     x16, x17, [sp, #(TF_X + 16 * 8)]
49         stp     x14, x15, [sp, #(TF_X + 14 * 8)]
50         stp     x12, x13, [sp, #(TF_X + 12 * 8)]
51         stp     x10, x11, [sp, #(TF_X + 10 * 8)]
52         stp     x8,  x9,  [sp, #(TF_X + 8  * 8)]
53         stp     x6,  x7,  [sp, #(TF_X + 6  * 8)]
54         stp     x4,  x5,  [sp, #(TF_X + 4  * 8)]
55         stp     x2,  x3,  [sp, #(TF_X + 2  * 8)]
56         stp     x0,  x1,  [sp, #(TF_X + 0  * 8)]
57         mrs     x10, elr_el1
58         mrs     x11, spsr_el1
59         mrs     x12, esr_el1
60 .if \el == 0
61         mrs     x18, sp_el0
62 .endif
63         str     x10, [sp, #(TF_ELR)]
64         stp     w11, w12, [sp, #(TF_SPSR)]
65         stp     x18,  lr, [sp, #(TF_SP)]
66         mrs     x18, tpidr_el1
67         add     x29, sp, #(TF_SIZE)
68 .if \el == 0
69         /* Apply the SSBD (CVE-2018-3639) workaround if needed */
70         ldr     x1, [x18, #PC_SSBD]
71         cbz     x1, 1f
72         mov     w0, #1
73         blr     x1
74 1:
75
76         ldr     x0, [x18, #(PC_CURTHREAD)]
77         bl      dbg_monitor_enter
78
79         /* Unmask debug and SError exceptions */
80         msr     daifclr, #(DAIF_D | DAIF_A)
81 .else
82         /*
83          * Unmask debug and SError exceptions.
84          * For EL1, debug exceptions are conditionally unmasked in
85          * do_el1h_sync().
86          */
87         msr     daifclr, #(DAIF_A)
88 .endif
89 .endm
90
91 .macro  restore_registers el
92         /*
93          * Mask all exceptions, x18 may change in the interrupt exception
94          * handler.
95          */
96         msr     daifset, #(DAIF_ALL)
97 .if \el == 0
98         ldr     x0, [x18, #PC_CURTHREAD]
99         mov     x1, sp
100         bl      dbg_monitor_exit
101
102         /* Remove the SSBD (CVE-2018-3639) workaround if needed */
103         ldr     x1, [x18, #PC_SSBD]
104         cbz     x1, 1f
105         mov     w0, #0
106         blr     x1
107 1:
108 .endif
109         ldp     x18,  lr, [sp, #(TF_SP)]
110         ldp     x10, x11, [sp, #(TF_ELR)]
111 .if \el == 0
112         msr     sp_el0, x18
113 .endif
114         msr     spsr_el1, x11
115         msr     elr_el1, x10
116         ldp     x0,  x1,  [sp, #(TF_X + 0  * 8)]
117         ldp     x2,  x3,  [sp, #(TF_X + 2  * 8)]
118         ldp     x4,  x5,  [sp, #(TF_X + 4  * 8)]
119         ldp     x6,  x7,  [sp, #(TF_X + 6  * 8)]
120         ldp     x8,  x9,  [sp, #(TF_X + 8  * 8)]
121         ldp     x10, x11, [sp, #(TF_X + 10 * 8)]
122         ldp     x12, x13, [sp, #(TF_X + 12 * 8)]
123         ldp     x14, x15, [sp, #(TF_X + 14 * 8)]
124         ldp     x16, x17, [sp, #(TF_X + 16 * 8)]
125 .if \el == 0
126         /*
127          * We only restore the callee saved registers when returning to
128          * userland as they may have been updated by a system call or signal.
129          */
130         ldp     x18, x19, [sp, #(TF_X + 18 * 8)]
131         ldp     x20, x21, [sp, #(TF_X + 20 * 8)]
132         ldp     x22, x23, [sp, #(TF_X + 22 * 8)]
133         ldp     x24, x25, [sp, #(TF_X + 24 * 8)]
134         ldp     x26, x27, [sp, #(TF_X + 26 * 8)]
135         ldp     x28, x29, [sp, #(TF_X + 28 * 8)]
136 .else
137         ldr          x29, [sp, #(TF_X + 29 * 8)]
138 .endif
139 .if \el == 0
140         add     sp, sp, #(TF_SIZE)
141 .else
142         mov     sp, x18
143         mrs     x18, tpidr_el1
144 .endif
145 .endm
146
147 .macro  do_ast
148         mrs     x19, daif
149         /* Make sure the IRQs are enabled before calling ast() */
150         bic     x19, x19, #PSR_I
151 1:
152         /*
153          * Mask interrupts while checking the ast pending flag
154          */
155         msr     daifset, #(DAIF_INTR)
156
157         /* Read the current thread flags */
158         ldr     x1, [x18, #PC_CURTHREAD]        /* Load curthread */
159         ldr     x2, [x1, #TD_FLAGS]
160
161         /* Check if we have either bits set */
162         mov     x3, #((TDF_ASTPENDING|TDF_NEEDRESCHED) >> 8)
163         lsl     x3, x3, #8
164         and     x2, x2, x3
165         cbz     x2, 2f
166
167         /* Restore interrupts */
168         msr     daif, x19
169
170         /* handle the ast */
171         mov     x0, sp
172         bl      _C_LABEL(ast)
173
174         /* Re-check for new ast scheduled */
175         b       1b
176 2:
177 .endm
178
179 ENTRY(handle_el1h_sync)
180         save_registers 1
181         ldr     x0, [x18, #PC_CURTHREAD]
182         mov     x1, sp
183         bl      do_el1h_sync
184         restore_registers 1
185         ERET
186 END(handle_el1h_sync)
187
188 ENTRY(handle_el1h_irq)
189         save_registers 1
190         mov     x0, sp
191         bl      intr_irq_handler
192         restore_registers 1
193         ERET
194 END(handle_el1h_irq)
195
196 ENTRY(handle_el0_sync)
197         save_registers 0
198         ldr     x0, [x18, #PC_CURTHREAD]
199         mov     x1, sp
200         str     x1, [x0, #TD_FRAME]
201         bl      do_el0_sync
202         do_ast
203         restore_registers 0
204         ERET
205 END(handle_el0_sync)
206
207 ENTRY(handle_el0_irq)
208         save_registers 0
209         mov     x0, sp
210         bl      intr_irq_handler
211         do_ast
212         restore_registers 0
213         ERET
214 END(handle_el0_irq)
215
216 ENTRY(handle_serror)
217         save_registers 0
218         mov     x0, sp
219 1:      bl      do_serror
220         b       1b
221 END(handle_serror)
222
223 ENTRY(handle_empty_exception)
224         save_registers 0
225         mov     x0, sp
226 1:      bl      unhandled_exception
227         b       1b
228 END(handle_unhandled_exception)
229
230 .macro  vempty
231         .align 7
232         b       handle_empty_exception
233 .endm
234
235 .macro  vector  name
236         .align 7
237         b       handle_\name
238 .endm
239
240         .align 11
241         .globl exception_vectors
242 exception_vectors:
243         vempty                  /* Synchronous EL1t */
244         vempty                  /* IRQ EL1t */
245         vempty                  /* FIQ EL1t */
246         vempty                  /* Error EL1t */
247
248         vector el1h_sync        /* Synchronous EL1h */
249         vector el1h_irq         /* IRQ EL1h */
250         vempty                  /* FIQ EL1h */
251         vector serror           /* Error EL1h */
252
253         vector el0_sync         /* Synchronous 64-bit EL0 */
254         vector el0_irq          /* IRQ 64-bit EL0 */
255         vempty                  /* FIQ 64-bit EL0 */
256         vector serror           /* Error 64-bit EL0 */
257
258         vector el0_sync         /* Synchronous 32-bit EL0 */
259         vector el0_irq          /* IRQ 32-bit EL0 */
260         vempty                  /* FIQ 32-bit EL0 */
261         vector serror           /* Error 32-bit EL0 */
262