blob: 55de60140133773a5ed740dbc9b3f693894a8860 [file] [log] [blame]
Wedson Almeida Filho22c973a2018-10-27 16:25:42 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Wedson Almeida Filho22c973a2018-10-27 16:25:42 +01003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010017#include "offsets.h"
Andrew Walbranc55365d2018-12-06 15:45:11 +000018#include "exception_macros.S"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010019
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000020/**
21 * Saves the volatile registers into the register buffer of the current vcpu. It
22 * allocates space on the stack for x18 and saves it if "also_save_x18" is
23 * specified; otherwise the caller is expected to have saved x18 in a similar
24 * fashion.
25 */
26.macro save_volatile_to_vcpu also_save_x18
27.ifnb \also_save_x18
Wedson Almeida Filho5bc0b4c2018-07-30 15:31:44 +010028 /*
29 * Save x18 since we're about to clobber it. We subtract 16 instead of
30 * 8 from the stack pointer to keep it 16-byte aligned.
31 */
32 str x18, [sp, #-16]!
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000033.endif
34 /* Get the current vcpu. */
35 mrs x18, tpidr_el2
36 stp x0, x1, [x18, #VCPU_REGS + 8 * 0]
37 stp x2, x3, [x18, #VCPU_REGS + 8 * 2]
38 stp x4, x5, [x18, #VCPU_REGS + 8 * 4]
39 stp x6, x7, [x18, #VCPU_REGS + 8 * 6]
40 stp x8, x9, [x18, #VCPU_REGS + 8 * 8]
41 stp x10, x11, [x18, #VCPU_REGS + 8 * 10]
42 stp x12, x13, [x18, #VCPU_REGS + 8 * 12]
43 stp x14, x15, [x18, #VCPU_REGS + 8 * 14]
44 stp x16, x17, [x18, #VCPU_REGS + 8 * 16]
45 stp x29, x30, [x18, #VCPU_REGS + 8 * 29]
46
47 /* x18 was saved on the stack, so we move it to vcpu regs buffer. */
48 ldr x0, [sp], #16
49 str x0, [x18, #VCPU_REGS + 8 * 18]
50
51 /* Save return address & mode. */
52 mrs x1, elr_el2
53 mrs x2, spsr_el2
54 stp x1, x2, [x18, #VCPU_REGS + 8 * 31]
55.endm
56
57/**
Fuad Tabbac76466d2019-09-06 10:42:12 +010058 * Save all general purpose registers into register buffer of current vcpu.
59 */
60.macro save_registers_to_vcpu
61 save_volatile_to_vcpu also_save_x18
62 stp x19, x20, [x18, #VCPU_REGS + 8 * 19]
63 stp x21, x22, [x18, #VCPU_REGS + 8 * 21]
64 stp x23, x24, [x18, #VCPU_REGS + 8 * 23]
65 stp x25, x26, [x18, #VCPU_REGS + 8 * 25]
66 stp x27, x28, [x18, #VCPU_REGS + 8 * 27]
67.endm
68
69/**
70 * Restore the volatile registers from the register buffer of the current vcpu.
71 */
72.macro restore_volatile_from_vcpu vcpu_ptr:req
73 ldp x4, x5, [\vcpu_ptr, #VCPU_REGS + 8 * 4]
74 ldp x6, x7, [\vcpu_ptr, #VCPU_REGS + 8 * 6]
75 ldp x8, x9, [\vcpu_ptr, #VCPU_REGS + 8 * 8]
76 ldp x10, x11, [\vcpu_ptr, #VCPU_REGS + 8 * 10]
77 ldp x12, x13, [\vcpu_ptr, #VCPU_REGS + 8 * 12]
78 ldp x14, x15, [\vcpu_ptr, #VCPU_REGS + 8 * 14]
79 ldp x16, x17, [\vcpu_ptr, #VCPU_REGS + 8 * 16]
80 ldr x18, [\vcpu_ptr, #VCPU_REGS + 8 * 18]
81 ldp x29, x30, [\vcpu_ptr, #VCPU_REGS + 8 * 29]
82
83 /* Restore return address & mode. */
84 ldp x1, x2, [\vcpu_ptr, #VCPU_REGS + 8 * 31]
85 msr elr_el2, x1
86 msr spsr_el2, x2
87
88 /* Restore x0..x3, which we have used as scratch before. */
89 ldp x2, x3, [\vcpu_ptr, #VCPU_REGS + 8 * 2]
90 ldp x0, x1, [\vcpu_ptr, #VCPU_REGS + 8 * 0]
91.endm
92
93/**
94 * Restore all general purpose registers from register buffer of current vcpu.
95 */
96.macro restore_registers_from_vcpu vcpu_ptr:req
97 ldp x19, x20, [\vcpu_ptr, #VCPU_REGS + 8 * 19]
98 ldp x21, x22, [\vcpu_ptr, #VCPU_REGS + 8 * 21]
99 ldp x23, x24, [\vcpu_ptr, #VCPU_REGS + 8 * 23]
100 ldp x25, x26, [\vcpu_ptr, #VCPU_REGS + 8 * 25]
101 ldp x27, x28, [\vcpu_ptr, #VCPU_REGS + 8 * 27]
102 restore_volatile_from_vcpu \vcpu_ptr
103.endm
104
105/**
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000106 * This is a generic handler for exceptions taken at a lower EL. It saves the
107 * volatile registers to the current vcpu and calls the C handler, which can
108 * select one of two paths: (a) restore volatile registers and return, or
109 * (b) switch to a different vcpu. In the latter case, the handler needs to save
110 * all non-volatile registers (they haven't been saved yet), then restore all
111 * registers from the new vcpu.
112 */
113.macro lower_exception handler:req
114 save_volatile_to_vcpu also_save_x18
115
116 /* Call C handler. */
117 bl \handler
118
119 /* Switch vcpu if requested by handler. */
120 cbnz x0, vcpu_switch
121
122 /* vcpu is not changing. */
123 mrs x0, tpidr_el2
124 b vcpu_restore_volatile_and_run
125.endm
126
127/**
128 * This is the handler for a sync exception taken at a lower EL. If the reason
129 * for the exception is an HVC call, it calls the faster hvc_handler without
130 * saving a lot of the registers, otherwise it goes to slow_sync_lower, which is
131 * the slow path where all registers needs to be saved/restored.
132 */
133.macro lower_sync_exception
134 /* Save x18 as save_volatile_to_vcpu would have. */
135 str x18, [sp, #-16]!
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100136
137 /* Extract the exception class (EC) from exception syndrome register. */
138 mrs x18, esr_el2
139 lsr x18, x18, #26
140
141 /* Take the slow path if exception is not due to an HVC instruction. */
Fuad Tabbac76466d2019-09-06 10:42:12 +0100142 cmp x18, #0x16
143 b.ne slow_sync_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100144
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100145 /*
Andrew Walbranfed412e2019-09-02 18:23:16 +0100146 * Make room for hvc_handler_return on stack, and point x8 (the indirect
147 * result location register in the AAPCS64 standard) to it.
148 * hvc_handler_return is returned this way according to paragraph
149 * 5.4.2.B.3 and section 5.5 because it is larger than 16 bytes.
150 */
151 stp xzr, xzr, [sp, #-16]!
152 stp xzr, xzr, [sp, #-16]!
153 stp xzr, xzr, [sp, #-16]!
154 mov x8, sp
155
156 /*
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100157 * Save x29 and x30, which are not saved by the callee, then jump to
158 * HVC handler.
159 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100160 stp x29, x30, [sp, #-16]!
161 bl hvc_handler
162 ldp x29, x30, [sp], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100163
Andrew Walbranfed412e2019-09-02 18:23:16 +0100164 /* Get the hvc_handler_return back off the stack. */
165 ldp x0, x1, [sp], #16
166 ldp x2, x3, [sp], #16
167 ldr x4, [sp], #16
168
169 cbnz x4, sync_lower_switch
170
171 /*
172 * Zero out volatile registers (except x0-x3, which contain results) and
173 * return.
174 */
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100175 stp xzr, xzr, [sp, #-16]!
Andrew Walbranfed412e2019-09-02 18:23:16 +0100176 ldp x4, x5, [sp]
177 ldp x6, x7, [sp]
178 ldp x8, x9, [sp]
179 ldp x10, x11, [sp]
180 ldp x12, x13, [sp]
181 ldp x14, x15, [sp]
182 ldp x16, x17, [sp], #16
Wedson Almeida Filho450ccb82018-08-12 16:25:36 +0100183
184 /* Restore x18, which was saved on the stack. */
185 ldr x18, [sp], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100186 eret
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000187.endm
188
189/**
190 * The following is the exception table. A pointer to it will be stored in
191 * register vbar_el2.
192 */
193.section .text.vector_table_el2, "ax"
194.global vector_table_el2
195.balign 0x800
196vector_table_el2:
197sync_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000198 current_exception_sp0 el2 sync_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000199
200.balign 0x80
201irq_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000202 current_exception_sp0 el2 irq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000203
204.balign 0x80
205fiq_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000206 current_exception_sp0 el2 fiq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000207
208.balign 0x80
209serr_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000210 current_exception_sp0 el2 serr_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000211
212.balign 0x80
213sync_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000214 current_exception_spx el2 sync_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000215
216.balign 0x80
217irq_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000218 current_exception_spx el2 irq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000219
220.balign 0x80
221fiq_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000222 current_exception_spx el2 fiq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000223
224.balign 0x80
225serr_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000226 current_exception_spx el2 serr_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000227
228.balign 0x80
229sync_lower_64:
230 lower_sync_exception
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100231
232.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000233irq_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000234 lower_exception irq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100235
236.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000237fiq_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000238 lower_exception fiq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100239
240.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000241serr_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000242 lower_exception serr_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100243
244.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000245sync_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000246 lower_sync_exception
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100247
248.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000249irq_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000250 lower_exception irq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100251
252.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000253fiq_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000254 lower_exception fiq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100255
256.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000257serr_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000258 lower_exception serr_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100259
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000260.balign 0x40
261slow_sync_lower:
Fuad Tabbac76466d2019-09-06 10:42:12 +0100262 /* Take the system register path for EC 0x18 */
263 cmp x18, #0x18
264 b.eq handle_system_register_access_s
265
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000266 /* The caller must have saved x18, so we don't save it here. */
267 save_volatile_to_vcpu
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100268
269 /* Read syndrome register and call C handler. */
270 mrs x0, esr_el2
271 bl sync_lower_exception
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100272 cbnz x0, vcpu_switch
273
274 /* vcpu is not changing. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000275 mrs x0, tpidr_el2
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100276 b vcpu_restore_volatile_and_run
277
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000278sync_lower_switch:
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100279 /* We'll have to switch, so save volatile state before doing so. */
280 mrs x18, tpidr_el2
281
Andrew Walbranfed412e2019-09-02 18:23:16 +0100282 /* Store zeroes in volatile register storage, except x0-x3. */
283 stp x0, x1, [x18, #VCPU_REGS + 8 * 0]
284 stp x2, x3, [x18, #VCPU_REGS + 8 * 2]
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000285 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 4]
286 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 6]
287 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 8]
288 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 10]
289 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 12]
290 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 14]
291 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 16]
292 stp x29, x30, [x18, #VCPU_REGS + 8 * 29]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100293
294 /* x18 was saved on the stack, so we move it to vcpu regs buffer. */
295 ldr x2, [sp], #16
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000296 str x2, [x18, #VCPU_REGS + 8 * 18]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100297
298 /* Save return address & mode. */
299 mrs x2, elr_el2
300 mrs x3, spsr_el2
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000301 stp x2, x3, [x18, #VCPU_REGS + 8 * 31]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100302
303 /* Save lazy state, then switch to new vcpu. */
Andrew Walbranfed412e2019-09-02 18:23:16 +0100304 mov x0, x4
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100305
306 /* Intentional fallthrough. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100307/**
308 * Switch to a new vcpu.
309 *
310 * All volatile registers from the old vcpu have already been saved. We need
311 * to save only non-volatile ones from the old vcpu, and restore all from the
312 * new one.
313 *
314 * x0 is a pointer to the new vcpu.
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100315 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100316vcpu_switch:
317 /* Save non-volatile registers. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000318 mrs x1, tpidr_el2
319 stp x19, x20, [x1, #VCPU_REGS + 8 * 19]
320 stp x21, x22, [x1, #VCPU_REGS + 8 * 21]
321 stp x23, x24, [x1, #VCPU_REGS + 8 * 23]
322 stp x25, x26, [x1, #VCPU_REGS + 8 * 25]
323 stp x27, x28, [x1, #VCPU_REGS + 8 * 27]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100324
325 /* Save lazy state. */
Fuad Tabba5e147a92019-08-14 15:30:30 +0100326 /* Use x28 as the base */
327 add x28, x1, #VCPU_LAZY
328
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100329 mrs x24, vmpidr_el2
330 mrs x25, csselr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100331 stp x24, x25, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100332
333 mrs x2, sctlr_el1
334 mrs x3, actlr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100335 stp x2, x3, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100336
337 mrs x4, cpacr_el1
338 mrs x5, ttbr0_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100339 stp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100340
341 mrs x6, ttbr1_el1
342 mrs x7, tcr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100343 stp x6, x7, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100344
345 mrs x8, esr_el1
346 mrs x9, afsr0_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100347 stp x8, x9, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100348
349 mrs x10, afsr1_el1
350 mrs x11, far_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100351 stp x10, x11, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100352
353 mrs x12, mair_el1
354 mrs x13, vbar_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100355 stp x12, x13, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100356
357 mrs x14, contextidr_el1
358 mrs x15, tpidr_el0
Fuad Tabba5e147a92019-08-14 15:30:30 +0100359 stp x14, x15, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100360
361 mrs x16, tpidrro_el0
362 mrs x17, tpidr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100363 stp x16, x17, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100364
365 mrs x18, amair_el1
366 mrs x19, cntkctl_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100367 stp x18, x19, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100368
369 mrs x20, sp_el0
370 mrs x21, sp_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100371 stp x20, x21, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100372
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000373 mrs x22, elr_el1
374 mrs x23, spsr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100375 stp x22, x23, [x28], #16
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100376
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000377 mrs x24, par_el1
378 mrs x25, hcr_el2
Fuad Tabba5e147a92019-08-14 15:30:30 +0100379 stp x24, x25, [x28], #16
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100380
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000381 mrs x26, cptr_el2
382 mrs x27, cnthctl_el2
Fuad Tabba5e147a92019-08-14 15:30:30 +0100383 stp x26, x27, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000384
Fuad Tabba5e147a92019-08-14 15:30:30 +0100385 mrs x4, vttbr_el2
386 mrs x5, mdcr_el2
387 stp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100388
Fuad Tabbac76466d2019-09-06 10:42:12 +0100389 mrs x6, mdscr_el1
390 str x6, [x28], #16
391
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100392 /* Save GIC registers. */
393#if GIC_VERSION == 3 || GIC_VERSION == 4
394 /* Offset is too large, so start from a new base. */
395 add x2, x1, #VCPU_GIC
396
397 mrs x3, ich_hcr_el2
Andrew Walbran4b976f42019-06-05 15:00:50 +0100398 mrs x4, icc_sre_el2
399 stp x3, x4, [x2, #16 * 0]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100400#endif
401
Fuad Tabba5e147a92019-08-14 15:30:30 +0100402 /* Save floating point registers. */
403 /* Use x28 as the base. */
404 add x28, x1, #VCPU_FREGS
405 stp q0, q1, [x28], #32
406 stp q2, q3, [x28], #32
407 stp q4, q5, [x28], #32
408 stp q6, q7, [x28], #32
409 stp q8, q9, [x28], #32
410 stp q10, q11, [x28], #32
411 stp q12, q13, [x28], #32
412 stp q14, q15, [x28], #32
413 stp q16, q17, [x28], #32
414 stp q18, q19, [x28], #32
415 stp q20, q21, [x28], #32
416 stp q22, q23, [x28], #32
417 stp q24, q25, [x28], #32
418 stp q26, q27, [x28], #32
419 stp q28, q29, [x28], #32
420 stp q30, q31, [x28], #32
Conrad Groblera824af62019-03-22 17:33:23 +0000421 mrs x3, fpsr
422 mrs x4, fpcr
Fuad Tabba5e147a92019-08-14 15:30:30 +0100423 stp x3, x4, [x28], #32
Conrad Groblera824af62019-03-22 17:33:23 +0000424
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000425 /* Save new vcpu pointer in non-volatile register. */
426 mov x19, x0
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100427
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000428 /*
429 * Save peripheral registers, and inform the arch-independent sections
430 * that registers have been saved.
431 */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000432 mov x0, x1
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000433 bl complete_saving_state
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000434 mov x0, x19
435
436 /* Intentional fallthrough. */
Andrew Walbran375f4532019-07-09 16:54:37 +0100437.global vcpu_restore_all_and_run
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100438vcpu_restore_all_and_run:
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100439 /* Update pointer to current vcpu. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100440 msr tpidr_el2, x0
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100441
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000442 /* Restore peripheral registers. */
443 mov x19, x0
444 bl begin_restoring_state
445 mov x0, x19
446
Conrad Groblera824af62019-03-22 17:33:23 +0000447 /*
448 * Restore floating point registers.
449 *
450 * Offset is too large, so start from a new base.
451 */
452 add x2, x0, #VCPU_FREGS
453 ldp q0, q1, [x2, #32 * 0]
454 ldp q2, q3, [x2, #32 * 1]
455 ldp q4, q5, [x2, #32 * 2]
456 ldp q6, q7, [x2, #32 * 3]
457 ldp q8, q9, [x2, #32 * 4]
458 ldp q10, q11, [x2, #32 * 5]
459 ldp q12, q13, [x2, #32 * 6]
460 ldp q14, q15, [x2, #32 * 7]
461 ldp q16, q17, [x2, #32 * 8]
462 ldp q18, q19, [x2, #32 * 9]
463 ldp q20, q21, [x2, #32 * 10]
464 ldp q22, q23, [x2, #32 * 11]
465 ldp q24, q25, [x2, #32 * 12]
466 ldp q26, q27, [x2, #32 * 13]
467 ldp q28, q29, [x2, #32 * 14]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100468 /* Offset becomes too large, so move the base. */
Conrad Groblera824af62019-03-22 17:33:23 +0000469 ldp q30, q31, [x2, #32 * 15]!
470 ldp x3, x4, [x2, #32 * 1]
471 msr fpsr, x3
Conrad Groblera824af62019-03-22 17:33:23 +0000472
Conrad Grobler02ff6af2019-06-04 09:40:28 +0100473 /*
474 * Only restore FPCR if changed, to avoid expensive
475 * self-synchronising operation where possible.
476 */
477 mrs x5, fpcr
478 cmp x5, x4
479 b.eq vcpu_restore_lazy_and_run
480 msr fpcr, x4
481 /* Intentional fallthrough. */
482
483vcpu_restore_lazy_and_run:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000484 /* Restore lazy registers. */
Fuad Tabba5e147a92019-08-14 15:30:30 +0100485 /* Use x28 as the base. */
486 add x28, x0, #VCPU_LAZY
487
488 ldp x24, x25, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100489 msr vmpidr_el2, x24
490 msr csselr_el1, x25
491
Fuad Tabba5e147a92019-08-14 15:30:30 +0100492 ldp x2, x3, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100493 msr sctlr_el1, x2
494 msr actlr_el1, x3
495
Fuad Tabba5e147a92019-08-14 15:30:30 +0100496 ldp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100497 msr cpacr_el1, x4
498 msr ttbr0_el1, x5
499
Fuad Tabba5e147a92019-08-14 15:30:30 +0100500 ldp x6, x7, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100501 msr ttbr1_el1, x6
502 msr tcr_el1, x7
503
Fuad Tabba5e147a92019-08-14 15:30:30 +0100504 ldp x8, x9, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100505 msr esr_el1, x8
506 msr afsr0_el1, x9
507
Fuad Tabba5e147a92019-08-14 15:30:30 +0100508 ldp x10, x11, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100509 msr afsr1_el1, x10
510 msr far_el1, x11
511
Fuad Tabba5e147a92019-08-14 15:30:30 +0100512 ldp x12, x13, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100513 msr mair_el1, x12
514 msr vbar_el1, x13
515
Fuad Tabba5e147a92019-08-14 15:30:30 +0100516 ldp x14, x15, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100517 msr contextidr_el1, x14
518 msr tpidr_el0, x15
519
Fuad Tabba5e147a92019-08-14 15:30:30 +0100520 ldp x16, x17, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100521 msr tpidrro_el0, x16
522 msr tpidr_el1, x17
523
Fuad Tabba5e147a92019-08-14 15:30:30 +0100524 ldp x18, x19, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100525 msr amair_el1, x18
526 msr cntkctl_el1, x19
527
Fuad Tabba5e147a92019-08-14 15:30:30 +0100528 ldp x20, x21, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100529 msr sp_el0, x20
530 msr sp_el1, x21
531
Fuad Tabba5e147a92019-08-14 15:30:30 +0100532 ldp x22, x23, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000533 msr elr_el1, x22
534 msr spsr_el1, x23
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100535
Fuad Tabba5e147a92019-08-14 15:30:30 +0100536 ldp x24, x25, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000537 msr par_el1, x24
538 msr hcr_el2, x25
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100539
Fuad Tabba5e147a92019-08-14 15:30:30 +0100540 ldp x26, x27, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000541 msr cptr_el2, x26
542 msr cnthctl_el2, x27
543
Fuad Tabba5e147a92019-08-14 15:30:30 +0100544 ldp x4, x5, [x28], #16
545 msr vttbr_el2, x4
546 msr mdcr_el2, x5
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100547
Fuad Tabbac76466d2019-09-06 10:42:12 +0100548 ldr x6, [x28], #16
549 msr mdscr_el1, x6
550
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100551 /* Restore GIC registers. */
552#if GIC_VERSION == 3 || GIC_VERSION == 4
553 /* Offset is too large, so start from a new base. */
554 add x2, x0, #VCPU_GIC
555
Andrew Walbran4b976f42019-06-05 15:00:50 +0100556 ldp x3, x4, [x2, #16 * 0]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100557 msr ich_hcr_el2, x3
Andrew Walbran4b976f42019-06-05 15:00:50 +0100558 msr icc_sre_el2, x4
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100559#endif
560
Andrew Walbran1f32e722019-06-07 17:57:26 +0100561 /*
562 * If a different vCPU is being run on this physical CPU to the last one
563 * which was run for this VM, invalidate the TLB. This must be called
564 * after vttbr_el2 has been updated, so that we have the page table and
565 * VMID of the vCPU to which we are switching.
566 */
567 mov x19, x0
568 bl maybe_invalidate_tlb
569 mov x0, x19
570
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100571 /* Restore non-volatile registers. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000572 ldp x19, x20, [x0, #VCPU_REGS + 8 * 19]
573 ldp x21, x22, [x0, #VCPU_REGS + 8 * 21]
574 ldp x23, x24, [x0, #VCPU_REGS + 8 * 23]
575 ldp x25, x26, [x0, #VCPU_REGS + 8 * 25]
576 ldp x27, x28, [x0, #VCPU_REGS + 8 * 27]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100577
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100578 /* Intentional fallthrough. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100579/**
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100580 * Restore volatile registers and run the given vcpu.
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100581 *
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000582 * x0 is a pointer to the target vcpu.
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100583 */
584vcpu_restore_volatile_and_run:
Fuad Tabbac76466d2019-09-06 10:42:12 +0100585 restore_volatile_from_vcpu x0
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000586 eret
587
588.balign 0x40
589/**
Fuad Tabbac76466d2019-09-06 10:42:12 +0100590 * Restore volatile registers from stack and return to original caller.
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000591 */
592restore_from_stack_and_return:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000593 restore_volatile_from_stack el2
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100594 eret
Fuad Tabbac76466d2019-09-06 10:42:12 +0100595
596.balign 0x40
597/**
598 * Handle accesses to system registers (EC=0x18) and return to original caller.
599 */
600handle_system_register_access_s:
601 /*
602 * All registers are (conservatively) saved because the handler can
603 * clobber non-volatile registers that are used by the msr/mrs, which
604 * results in the wrong value being read or written.
605 */
606 save_registers_to_vcpu
607
608 /* Read syndrome register and call C handler. */
609 mrs x0, esr_el2
610 bl handle_system_register_access
611 cbnz x0, vcpu_switch
612
613 /* vcpu is not changing. */
614 mrs x0, tpidr_el2
615 restore_registers_from_vcpu x0
616 eret