]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/exception.S
zfs: merge openzfs/zfs@6c3c5fcfb (zfs-2.1-release) into stable/13
[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 /*
37  * This is limited to 28 instructions as it's placed in the exception vector
38  * slot that is 32 instructions long. We need one for the branch, and three
39  * for the prologue.
40  */
41 .macro  save_registers_head el
42 .if \el == 1
43         mov     x18, sp
44         sub     sp, sp, #128
45 .endif
46         sub     sp, sp, #(TF_SIZE + 16)
47         stp     x29, lr, [sp, #(TF_SIZE)]
48         stp     x28, x29, [sp, #(TF_X + 28 * 8)]
49         stp     x26, x27, [sp, #(TF_X + 26 * 8)]
50         stp     x24, x25, [sp, #(TF_X + 24 * 8)]
51         stp     x22, x23, [sp, #(TF_X + 22 * 8)]
52         stp     x20, x21, [sp, #(TF_X + 20 * 8)]
53         stp     x18, x19, [sp, #(TF_X + 18 * 8)]
54         stp     x16, x17, [sp, #(TF_X + 16 * 8)]
55         stp     x14, x15, [sp, #(TF_X + 14 * 8)]
56         stp     x12, x13, [sp, #(TF_X + 12 * 8)]
57         stp     x10, x11, [sp, #(TF_X + 10 * 8)]
58         stp     x8,  x9,  [sp, #(TF_X + 8  * 8)]
59         stp     x6,  x7,  [sp, #(TF_X + 6  * 8)]
60         stp     x4,  x5,  [sp, #(TF_X + 4  * 8)]
61         stp     x2,  x3,  [sp, #(TF_X + 2  * 8)]
62         stp     x0,  x1,  [sp, #(TF_X + 0  * 8)]
63         mrs     x10, elr_el1
64         mrs     x11, spsr_el1
65         mrs     x12, esr_el1
66 .if \el == 0
67         mrs     x18, sp_el0
68 .endif
69         str     x10, [sp, #(TF_ELR)]
70         stp     w11, w12, [sp, #(TF_SPSR)]
71         stp     x18,  lr, [sp, #(TF_SP)]
72         mrs     x18, tpidr_el1
73         add     x29, sp, #(TF_SIZE)
74 .endm
75
76 .macro  save_registers el
77 .if \el == 0
78 #if defined(PERTHREAD_SSP)
79         /* Load the SSP canary to sp_el0 */
80         ldr     x1, [x18, #(PC_CURTHREAD)]
81         add     x1, x1, #(TD_MD_CANARY)
82         msr     sp_el0, x1
83 #endif
84
85         /* Apply the SSBD (CVE-2018-3639) workaround if needed */
86         ldr     x1, [x18, #PC_SSBD]
87         cbz     x1, 1f
88         mov     w0, #1
89         blr     x1
90 1:
91
92         ldr     x0, [x18, #(PC_CURTHREAD)]
93         bl      dbg_monitor_enter
94
95         /* Unmask debug and SError exceptions */
96         msr     daifclr, #(DAIF_D | DAIF_A)
97 .else
98         /*
99          * Unmask debug and SError exceptions.
100          * For EL1, debug exceptions are conditionally unmasked in
101          * do_el1h_sync().
102          */
103         msr     daifclr, #(DAIF_A)
104 .endif
105 .endm
106
107 .macro  restore_registers el
108         /*
109          * Mask all exceptions, x18 may change in the interrupt exception
110          * handler.
111          */
112         msr     daifset, #(DAIF_ALL)
113 .if \el == 0
114         ldr     x0, [x18, #PC_CURTHREAD]
115         mov     x1, sp
116         bl      dbg_monitor_exit
117
118         /* Remove the SSBD (CVE-2018-3639) workaround if needed */
119         ldr     x1, [x18, #PC_SSBD]
120         cbz     x1, 1f
121         mov     w0, #0
122         blr     x1
123 1:
124 .endif
125         ldp     x18,  lr, [sp, #(TF_SP)]
126         ldp     x10, x11, [sp, #(TF_ELR)]
127 .if \el == 0
128         msr     sp_el0, x18
129 .endif
130         msr     spsr_el1, x11
131         msr     elr_el1, x10
132         ldp     x0,  x1,  [sp, #(TF_X + 0  * 8)]
133         ldp     x2,  x3,  [sp, #(TF_X + 2  * 8)]
134         ldp     x4,  x5,  [sp, #(TF_X + 4  * 8)]
135         ldp     x6,  x7,  [sp, #(TF_X + 6  * 8)]
136         ldp     x8,  x9,  [sp, #(TF_X + 8  * 8)]
137         ldp     x10, x11, [sp, #(TF_X + 10 * 8)]
138         ldp     x12, x13, [sp, #(TF_X + 12 * 8)]
139         ldp     x14, x15, [sp, #(TF_X + 14 * 8)]
140         ldp     x16, x17, [sp, #(TF_X + 16 * 8)]
141 .if \el == 0
142         /*
143          * We only restore the callee saved registers when returning to
144          * userland as they may have been updated by a system call or signal.
145          */
146         ldp     x18, x19, [sp, #(TF_X + 18 * 8)]
147         ldp     x20, x21, [sp, #(TF_X + 20 * 8)]
148         ldp     x22, x23, [sp, #(TF_X + 22 * 8)]
149         ldp     x24, x25, [sp, #(TF_X + 24 * 8)]
150         ldp     x26, x27, [sp, #(TF_X + 26 * 8)]
151         ldp     x28, x29, [sp, #(TF_X + 28 * 8)]
152 .else
153         ldr          x29, [sp, #(TF_X + 29 * 8)]
154 .endif
155 .if \el == 0
156         add     sp, sp, #(TF_SIZE + 16)
157 .else
158         mov     sp, x18
159         mrs     x18, tpidr_el1
160 .endif
161 .endm
162
163 .macro  do_ast
164         mrs     x19, daif
165         /* Make sure the IRQs are enabled before calling ast() */
166         bic     x19, x19, #PSR_I
167 1:
168         /*
169          * Mask interrupts while checking the ast pending flag
170          */
171         msr     daifset, #(DAIF_INTR)
172
173         /* Read the current thread flags */
174         ldr     x1, [x18, #PC_CURTHREAD]        /* Load curthread */
175         ldr     x2, [x1, #TD_FLAGS]
176
177         /* Check if we have either bits set */
178         mov     x3, #((TDF_ASTPENDING|TDF_NEEDRESCHED) >> 8)
179         lsl     x3, x3, #8
180         and     x2, x2, x3
181         cbz     x2, 2f
182
183         /* Restore interrupts */
184         msr     daif, x19
185
186         /* handle the ast */
187         mov     x0, sp
188         bl      _C_LABEL(ast)
189
190         /* Re-check for new ast scheduled */
191         b       1b
192 2:
193 .endm
194
195 ENTRY(handle_el1h_sync)
196         save_registers 1
197         ldr     x0, [x18, #PC_CURTHREAD]
198         mov     x1, sp
199         bl      do_el1h_sync
200         restore_registers 1
201         ERET
202 END(handle_el1h_sync)
203
204 ENTRY(handle_el1h_irq)
205         save_registers 1
206         mov     x0, sp
207         bl      intr_irq_handler
208         restore_registers 1
209         ERET
210 END(handle_el1h_irq)
211
212 ENTRY(handle_el0_sync)
213         save_registers 0
214         ldr     x0, [x18, #PC_CURTHREAD]
215         mov     x1, sp
216         str     x1, [x0, #TD_FRAME]
217         bl      do_el0_sync
218         do_ast
219         restore_registers 0
220         ERET
221 END(handle_el0_sync)
222
223 ENTRY(handle_el0_irq)
224         save_registers 0
225         mov     x0, sp
226         bl      intr_irq_handler
227         do_ast
228         restore_registers 0
229         ERET
230 END(handle_el0_irq)
231
232 ENTRY(handle_serror)
233         save_registers 0
234         mov     x0, sp
235 1:      bl      do_serror
236         b       1b
237 END(handle_serror)
238
239 ENTRY(handle_empty_exception)
240         save_registers 0
241         mov     x0, sp
242 1:      bl      unhandled_exception
243         b       1b
244 END(handle_empty_exception)
245
246 .macro  vector  name, el
247         .align 7
248         save_registers_head \el
249         b       handle_\name
250         dsb     sy
251         isb
252         /* Break instruction to ensure we aren't executing code here. */
253         brk     0x42
254 .endm
255
256 .macro  vempty el
257         vector  empty_exception \el
258 .endm
259
260         .align 11
261         .globl exception_vectors
262 exception_vectors:
263         vempty 1                /* Synchronous EL1t */
264         vempty 1                /* IRQ EL1t */
265         vempty 1                /* FIQ EL1t */
266         vempty 1                /* Error EL1t */
267
268         vector el1h_sync 1      /* Synchronous EL1h */
269         vector el1h_irq 1       /* IRQ EL1h */
270         vempty 1                /* FIQ EL1h */
271         vector serror 1         /* Error EL1h */
272
273         vector el0_sync 0       /* Synchronous 64-bit EL0 */
274         vector el0_irq 0        /* IRQ 64-bit EL0 */
275         vempty 0                /* FIQ 64-bit EL0 */
276         vector serror 0         /* Error 64-bit EL0 */
277
278         vector el0_sync 0       /* Synchronous 32-bit EL0 */
279         vector el0_irq 0        /* IRQ 32-bit EL0 */
280         vempty 0                /* FIQ 32-bit EL0 */
281         vector serror 0         /* Error 32-bit EL0 */
282