2 * Copyright (c) 2014 Andrew Turner
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <machine/asm.h>
29 #include <machine/armreg.h>
35 * This is limited to 28 instructions as it's placed in the exception vector
36 * slot that is 32 instructions long. We need one for the branch, and three
39 .macro save_registers_head el
42 stp x0, x1, [sp, #(TF_X - TF_SIZE - 128)]!
44 stp x0, x1, [sp, #(TF_X - TF_SIZE)]!
46 stp x2, x3, [sp, #(2 * 8)]
47 stp x4, x5, [sp, #(4 * 8)]
48 stp x6, x7, [sp, #(6 * 8)]
49 stp x8, x9, [sp, #(8 * 8)]
50 stp x10, x11, [sp, #(10 * 8)]
51 stp x12, x13, [sp, #(12 * 8)]
52 stp x14, x15, [sp, #(14 * 8)]
53 stp x16, x17, [sp, #(16 * 8)]
54 stp x18, x19, [sp, #(18 * 8)]
55 stp x20, x21, [sp, #(20 * 8)]
56 stp x22, x23, [sp, #(22 * 8)]
57 stp x24, x25, [sp, #(24 * 8)]
58 stp x26, x27, [sp, #(26 * 8)]
59 stp x28, x29, [sp, #(28 * 8)]
67 stp x18, lr, [sp, #(TF_SP - TF_X)]!
68 stp x10, x11, [sp, #(TF_ELR)]
69 stp x12, x13, [sp, #(TF_ESR)]
73 .macro save_registers el
74 add x29, sp, #(TF_SIZE)
76 #if defined(PERTHREAD_SSP)
77 /* Load the SSP canary to sp_el0 */
78 ldr x1, [x18, #(PC_CURTHREAD)]
79 add x1, x1, #(TD_MD_CANARY)
83 /* Apply the SSBD (CVE-2018-3639) workaround if needed */
84 ldr x1, [x18, #PC_SSBD]
90 ldr x0, [x18, #PC_CURTHREAD]
93 ldr x0, [x18, #(PC_CURTHREAD)]
96 /* Unmask debug and SError exceptions */
97 msr daifclr, #(DAIF_D | DAIF_A)
100 * Unmask debug and SError exceptions.
101 * For EL1, debug exceptions are conditionally unmasked in
104 msr daifclr, #(DAIF_A)
108 .macro restore_registers el
110 * Mask all exceptions, x18 may change in the interrupt exception
113 msr daifset, #(DAIF_ALL)
115 ldr x0, [x18, #PC_CURTHREAD]
119 ldr x0, [x18, #PC_CURTHREAD]
122 /* Remove the SSBD (CVE-2018-3639) workaround if needed */
123 ldr x1, [x18, #PC_SSBD]
129 ldp x18, lr, [sp, #(TF_SP)]
130 ldp x10, x11, [sp, #(TF_ELR)]
136 ldp x0, x1, [sp, #(TF_X + 0 * 8)]
137 ldp x2, x3, [sp, #(TF_X + 2 * 8)]
138 ldp x4, x5, [sp, #(TF_X + 4 * 8)]
139 ldp x6, x7, [sp, #(TF_X + 6 * 8)]
140 ldp x8, x9, [sp, #(TF_X + 8 * 8)]
141 ldp x10, x11, [sp, #(TF_X + 10 * 8)]
142 ldp x12, x13, [sp, #(TF_X + 12 * 8)]
143 ldp x14, x15, [sp, #(TF_X + 14 * 8)]
144 ldp x16, x17, [sp, #(TF_X + 16 * 8)]
147 * We only restore the callee saved registers when returning to
148 * userland as they may have been updated by a system call or signal.
150 ldp x18, x19, [sp, #(TF_X + 18 * 8)]
151 ldp x20, x21, [sp, #(TF_X + 20 * 8)]
152 ldp x22, x23, [sp, #(TF_X + 22 * 8)]
153 ldp x24, x25, [sp, #(TF_X + 24 * 8)]
154 ldp x26, x27, [sp, #(TF_X + 26 * 8)]
155 ldp x28, x29, [sp, #(TF_X + 28 * 8)]
157 ldr x29, [sp, #(TF_X + 29 * 8)]
160 add sp, sp, #(TF_SIZE)
169 /* Make sure the IRQs are enabled before calling ast() */
173 * Mask interrupts while checking the ast pending flag
175 msr daifset, #(DAIF_INTR)
177 /* Read the current thread AST mask */
178 ldr x1, [x18, #PC_CURTHREAD] /* Load curthread */
179 ldr w1, [x1, #(TD_AST)]
181 /* Check if we have a non-zero AST mask */
184 /* Restore interrupts */
191 /* Re-check for new ast scheduled */
196 ENTRY(handle_el1h_sync)
198 ldr x0, [x18, #PC_CURTHREAD]
203 END(handle_el1h_sync)
205 ENTRY(handle_el1h_irq)
213 ENTRY(handle_el0_sync)
215 ldr x0, [x18, #PC_CURTHREAD]
217 str x1, [x0, #TD_FRAME]
224 ENTRY(handle_el0_irq)
240 ENTRY(handle_empty_exception)
243 1: bl unhandled_exception
245 END(handle_empty_exception)
247 .macro vector name, el
249 save_registers_head \el
253 /* Break instruction to ensure we aren't executing code here. */
258 vector empty_exception \el
262 .globl exception_vectors
264 vempty 1 /* Synchronous EL1t */
265 vempty 1 /* IRQ EL1t */
266 vempty 1 /* FIQ EL1t */
267 vempty 1 /* Error EL1t */
269 vector el1h_sync 1 /* Synchronous EL1h */
270 vector el1h_irq 1 /* IRQ EL1h */
271 vempty 1 /* FIQ EL1h */
272 vector serror 1 /* Error EL1h */
274 vector el0_sync 0 /* Synchronous 64-bit EL0 */
275 vector el0_irq 0 /* IRQ 64-bit EL0 */
276 vempty 0 /* FIQ 64-bit EL0 */
277 vector serror 0 /* Error 64-bit EL0 */
279 vector el0_sync 0 /* Synchronous 32-bit EL0 */
280 vector el0_irq 0 /* IRQ 32-bit EL0 */
281 vempty 0 /* FIQ 32-bit EL0 */
282 vector serror 0 /* Error 32-bit EL0 */