blob: 009dc839e22dfc575058481fcdc5fabd59b8bf63 [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/**
58 * This is a generic handler for exceptions taken at a lower EL. It saves the
59 * volatile registers to the current vcpu and calls the C handler, which can
60 * select one of two paths: (a) restore volatile registers and return, or
61 * (b) switch to a different vcpu. In the latter case, the handler needs to save
62 * all non-volatile registers (they haven't been saved yet), then restore all
63 * registers from the new vcpu.
64 */
65.macro lower_exception handler:req
66 save_volatile_to_vcpu also_save_x18
67
68 /* Call C handler. */
69 bl \handler
70
71 /* Switch vcpu if requested by handler. */
72 cbnz x0, vcpu_switch
73
74 /* vcpu is not changing. */
75 mrs x0, tpidr_el2
76 b vcpu_restore_volatile_and_run
77.endm
78
79/**
80 * This is the handler for a sync exception taken at a lower EL. If the reason
81 * for the exception is an HVC call, it calls the faster hvc_handler without
82 * saving a lot of the registers, otherwise it goes to slow_sync_lower, which is
83 * the slow path where all registers needs to be saved/restored.
84 */
85.macro lower_sync_exception
86 /* Save x18 as save_volatile_to_vcpu would have. */
87 str x18, [sp, #-16]!
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010088
89 /* Extract the exception class (EC) from exception syndrome register. */
90 mrs x18, esr_el2
91 lsr x18, x18, #26
92
93 /* Take the slow path if exception is not due to an HVC instruction. */
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +010094 sub x18, x18, #0x16
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000095 cbnz x18, slow_sync_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010096
Wedson Almeida Filho87009642018-07-02 10:20:07 +010097 /*
98 * Save x29 and x30, which are not saved by the callee, then jump to
99 * HVC handler.
100 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100101 stp x29, x30, [sp, #-16]!
102 bl hvc_handler
103 ldp x29, x30, [sp], #16
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000104 cbnz x1, sync_lower_switch
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100105
106 /* Zero out all volatile registers (except x0) and return. */
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100107 stp xzr, xzr, [sp, #-16]!
108 ldp x1, x2, [sp]
109 ldp x3, x4, [sp]
110 ldp x5, x6, [sp]
111 ldp x7, x8, [sp]
112 ldp x9, x10, [sp]
113 ldp x11, x12, [sp]
114 ldp x13, x14, [sp]
115 ldp x15, x16, [sp], #16
Wedson Almeida Filho5bc0b4c2018-07-30 15:31:44 +0100116 mov x17, xzr
Wedson Almeida Filho450ccb82018-08-12 16:25:36 +0100117
118 /* Restore x18, which was saved on the stack. */
119 ldr x18, [sp], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100120 eret
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000121.endm
122
123/**
124 * The following is the exception table. A pointer to it will be stored in
125 * register vbar_el2.
126 */
127.section .text.vector_table_el2, "ax"
128.global vector_table_el2
129.balign 0x800
130vector_table_el2:
131sync_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000132 current_exception_sp0 el2 sync_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000133
134.balign 0x80
135irq_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000136 current_exception_sp0 el2 irq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000137
138.balign 0x80
139fiq_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000140 current_exception_sp0 el2 fiq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000141
142.balign 0x80
143serr_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000144 current_exception_sp0 el2 serr_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000145
146.balign 0x80
147sync_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000148 current_exception_spx el2 sync_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000149
150.balign 0x80
151irq_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000152 current_exception_spx el2 irq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000153
154.balign 0x80
155fiq_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000156 current_exception_spx el2 fiq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000157
158.balign 0x80
159serr_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000160 current_exception_spx el2 serr_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000161
162.balign 0x80
163sync_lower_64:
164 lower_sync_exception
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100165
166.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000167irq_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000168 lower_exception irq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100169
170.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000171fiq_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000172 lower_exception fiq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100173
174.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000175serr_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000176 lower_exception serr_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100177
178.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000179sync_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000180 lower_sync_exception
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100181
182.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000183irq_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000184 lower_exception irq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100185
186.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000187fiq_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000188 lower_exception fiq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100189
190.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000191serr_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000192 lower_exception serr_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100193
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000194.balign 0x40
195slow_sync_lower:
196 /* The caller must have saved x18, so we don't save it here. */
197 save_volatile_to_vcpu
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100198
199 /* Read syndrome register and call C handler. */
200 mrs x0, esr_el2
201 bl sync_lower_exception
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100202 cbnz x0, vcpu_switch
203
204 /* vcpu is not changing. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000205 mrs x0, tpidr_el2
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100206 b vcpu_restore_volatile_and_run
207
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000208sync_lower_switch:
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100209 /* We'll have to switch, so save volatile state before doing so. */
210 mrs x18, tpidr_el2
211
212 /* Store zeroes in volatile register storage, except x0. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000213 stp x0, xzr, [x18, #VCPU_REGS + 8 * 0]
214 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 2]
215 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 4]
216 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 6]
217 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 8]
218 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 10]
219 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 12]
220 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 14]
221 stp xzr, xzr, [x18, #VCPU_REGS + 8 * 16]
222 stp x29, x30, [x18, #VCPU_REGS + 8 * 29]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100223
224 /* x18 was saved on the stack, so we move it to vcpu regs buffer. */
225 ldr x2, [sp], #16
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000226 str x2, [x18, #VCPU_REGS + 8 * 18]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100227
228 /* Save return address & mode. */
229 mrs x2, elr_el2
230 mrs x3, spsr_el2
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000231 stp x2, x3, [x18, #VCPU_REGS + 8 * 31]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100232
233 /* Save lazy state, then switch to new vcpu. */
234 mov x0, x1
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100235
236 /* Intentional fallthrough. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100237/**
238 * Switch to a new vcpu.
239 *
240 * All volatile registers from the old vcpu have already been saved. We need
241 * to save only non-volatile ones from the old vcpu, and restore all from the
242 * new one.
243 *
244 * x0 is a pointer to the new vcpu.
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100245 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100246vcpu_switch:
247 /* Save non-volatile registers. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000248 mrs x1, tpidr_el2
249 stp x19, x20, [x1, #VCPU_REGS + 8 * 19]
250 stp x21, x22, [x1, #VCPU_REGS + 8 * 21]
251 stp x23, x24, [x1, #VCPU_REGS + 8 * 23]
252 stp x25, x26, [x1, #VCPU_REGS + 8 * 25]
253 stp x27, x28, [x1, #VCPU_REGS + 8 * 27]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100254
255 /* Save lazy state. */
Fuad Tabba5e147a92019-08-14 15:30:30 +0100256 /* Use x28 as the base */
257 add x28, x1, #VCPU_LAZY
258
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100259 mrs x24, vmpidr_el2
260 mrs x25, csselr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100261 stp x24, x25, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100262
263 mrs x2, sctlr_el1
264 mrs x3, actlr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100265 stp x2, x3, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100266
267 mrs x4, cpacr_el1
268 mrs x5, ttbr0_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100269 stp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100270
271 mrs x6, ttbr1_el1
272 mrs x7, tcr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100273 stp x6, x7, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100274
275 mrs x8, esr_el1
276 mrs x9, afsr0_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100277 stp x8, x9, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100278
279 mrs x10, afsr1_el1
280 mrs x11, far_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100281 stp x10, x11, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100282
283 mrs x12, mair_el1
284 mrs x13, vbar_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100285 stp x12, x13, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100286
287 mrs x14, contextidr_el1
288 mrs x15, tpidr_el0
Fuad Tabba5e147a92019-08-14 15:30:30 +0100289 stp x14, x15, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100290
291 mrs x16, tpidrro_el0
292 mrs x17, tpidr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100293 stp x16, x17, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100294
295 mrs x18, amair_el1
296 mrs x19, cntkctl_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100297 stp x18, x19, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100298
299 mrs x20, sp_el0
300 mrs x21, sp_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100301 stp x20, x21, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100302
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000303 mrs x22, elr_el1
304 mrs x23, spsr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100305 stp x22, x23, [x28], #16
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100306
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000307 mrs x24, par_el1
308 mrs x25, hcr_el2
Fuad Tabba5e147a92019-08-14 15:30:30 +0100309 stp x24, x25, [x28], #16
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100310
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000311 mrs x26, cptr_el2
312 mrs x27, cnthctl_el2
Fuad Tabba5e147a92019-08-14 15:30:30 +0100313 stp x26, x27, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000314
Fuad Tabba5e147a92019-08-14 15:30:30 +0100315 mrs x4, vttbr_el2
316 mrs x5, mdcr_el2
317 stp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100318
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100319 /* Save GIC registers. */
320#if GIC_VERSION == 3 || GIC_VERSION == 4
321 /* Offset is too large, so start from a new base. */
322 add x2, x1, #VCPU_GIC
323
324 mrs x3, ich_hcr_el2
325 str x3, [x2, #16 * 0]
326#endif
327
Fuad Tabba5e147a92019-08-14 15:30:30 +0100328 /* Save floating point registers. */
329 /* Use x28 as the base. */
330 add x28, x1, #VCPU_FREGS
331 stp q0, q1, [x28], #32
332 stp q2, q3, [x28], #32
333 stp q4, q5, [x28], #32
334 stp q6, q7, [x28], #32
335 stp q8, q9, [x28], #32
336 stp q10, q11, [x28], #32
337 stp q12, q13, [x28], #32
338 stp q14, q15, [x28], #32
339 stp q16, q17, [x28], #32
340 stp q18, q19, [x28], #32
341 stp q20, q21, [x28], #32
342 stp q22, q23, [x28], #32
343 stp q24, q25, [x28], #32
344 stp q26, q27, [x28], #32
345 stp q28, q29, [x28], #32
346 stp q30, q31, [x28], #32
Conrad Groblera824af62019-03-22 17:33:23 +0000347 mrs x3, fpsr
348 mrs x4, fpcr
Fuad Tabba5e147a92019-08-14 15:30:30 +0100349 stp x3, x4, [x28], #32
Conrad Groblera824af62019-03-22 17:33:23 +0000350
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000351 /* Save new vcpu pointer in non-volatile register. */
352 mov x19, x0
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100353
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000354 /*
355 * Save peripheral registers, and inform the arch-independent sections
356 * that registers have been saved.
357 */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000358 mov x0, x1
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000359 bl complete_saving_state
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000360 mov x0, x19
361
362 /* Intentional fallthrough. */
Andrew Walbran375f4532019-07-09 16:54:37 +0100363.global vcpu_restore_all_and_run
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100364vcpu_restore_all_and_run:
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100365 /* Update pointer to current vcpu. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100366 msr tpidr_el2, x0
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100367
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000368 /* Restore peripheral registers. */
369 mov x19, x0
370 bl begin_restoring_state
371 mov x0, x19
372
Conrad Groblera824af62019-03-22 17:33:23 +0000373 /*
374 * Restore floating point registers.
375 *
376 * Offset is too large, so start from a new base.
377 */
378 add x2, x0, #VCPU_FREGS
379 ldp q0, q1, [x2, #32 * 0]
380 ldp q2, q3, [x2, #32 * 1]
381 ldp q4, q5, [x2, #32 * 2]
382 ldp q6, q7, [x2, #32 * 3]
383 ldp q8, q9, [x2, #32 * 4]
384 ldp q10, q11, [x2, #32 * 5]
385 ldp q12, q13, [x2, #32 * 6]
386 ldp q14, q15, [x2, #32 * 7]
387 ldp q16, q17, [x2, #32 * 8]
388 ldp q18, q19, [x2, #32 * 9]
389 ldp q20, q21, [x2, #32 * 10]
390 ldp q22, q23, [x2, #32 * 11]
391 ldp q24, q25, [x2, #32 * 12]
392 ldp q26, q27, [x2, #32 * 13]
393 ldp q28, q29, [x2, #32 * 14]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100394 /* Offset becomes too large, so move the base. */
Conrad Groblera824af62019-03-22 17:33:23 +0000395 ldp q30, q31, [x2, #32 * 15]!
396 ldp x3, x4, [x2, #32 * 1]
397 msr fpsr, x3
Conrad Groblera824af62019-03-22 17:33:23 +0000398
Conrad Grobler02ff6af2019-06-04 09:40:28 +0100399 /*
400 * Only restore FPCR if changed, to avoid expensive
401 * self-synchronising operation where possible.
402 */
403 mrs x5, fpcr
404 cmp x5, x4
405 b.eq vcpu_restore_lazy_and_run
406 msr fpcr, x4
407 /* Intentional fallthrough. */
408
409vcpu_restore_lazy_and_run:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000410 /* Restore lazy registers. */
Fuad Tabba5e147a92019-08-14 15:30:30 +0100411 /* Use x28 as the base. */
412 add x28, x0, #VCPU_LAZY
413
414 ldp x24, x25, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100415 msr vmpidr_el2, x24
416 msr csselr_el1, x25
417
Fuad Tabba5e147a92019-08-14 15:30:30 +0100418 ldp x2, x3, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100419 msr sctlr_el1, x2
420 msr actlr_el1, x3
421
Fuad Tabba5e147a92019-08-14 15:30:30 +0100422 ldp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100423 msr cpacr_el1, x4
424 msr ttbr0_el1, x5
425
Fuad Tabba5e147a92019-08-14 15:30:30 +0100426 ldp x6, x7, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100427 msr ttbr1_el1, x6
428 msr tcr_el1, x7
429
Fuad Tabba5e147a92019-08-14 15:30:30 +0100430 ldp x8, x9, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100431 msr esr_el1, x8
432 msr afsr0_el1, x9
433
Fuad Tabba5e147a92019-08-14 15:30:30 +0100434 ldp x10, x11, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100435 msr afsr1_el1, x10
436 msr far_el1, x11
437
Fuad Tabba5e147a92019-08-14 15:30:30 +0100438 ldp x12, x13, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100439 msr mair_el1, x12
440 msr vbar_el1, x13
441
Fuad Tabba5e147a92019-08-14 15:30:30 +0100442 ldp x14, x15, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100443 msr contextidr_el1, x14
444 msr tpidr_el0, x15
445
Fuad Tabba5e147a92019-08-14 15:30:30 +0100446 ldp x16, x17, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100447 msr tpidrro_el0, x16
448 msr tpidr_el1, x17
449
Fuad Tabba5e147a92019-08-14 15:30:30 +0100450 ldp x18, x19, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100451 msr amair_el1, x18
452 msr cntkctl_el1, x19
453
Fuad Tabba5e147a92019-08-14 15:30:30 +0100454 ldp x20, x21, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100455 msr sp_el0, x20
456 msr sp_el1, x21
457
Fuad Tabba5e147a92019-08-14 15:30:30 +0100458 ldp x22, x23, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000459 msr elr_el1, x22
460 msr spsr_el1, x23
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100461
Fuad Tabba5e147a92019-08-14 15:30:30 +0100462 ldp x24, x25, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000463 msr par_el1, x24
464 msr hcr_el2, x25
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100465
Fuad Tabba5e147a92019-08-14 15:30:30 +0100466 ldp x26, x27, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000467 msr cptr_el2, x26
468 msr cnthctl_el2, x27
469
Fuad Tabba5e147a92019-08-14 15:30:30 +0100470 ldp x4, x5, [x28], #16
471 msr vttbr_el2, x4
472 msr mdcr_el2, x5
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100473
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100474 /* Restore GIC registers. */
475#if GIC_VERSION == 3 || GIC_VERSION == 4
476 /* Offset is too large, so start from a new base. */
477 add x2, x0, #VCPU_GIC
478
479 ldr x3, [x2, #16 * 0]
480 msr ich_hcr_el2, x3
481#endif
482
Andrew Walbran1f32e722019-06-07 17:57:26 +0100483 /*
484 * If a different vCPU is being run on this physical CPU to the last one
485 * which was run for this VM, invalidate the TLB. This must be called
486 * after vttbr_el2 has been updated, so that we have the page table and
487 * VMID of the vCPU to which we are switching.
488 */
489 mov x19, x0
490 bl maybe_invalidate_tlb
491 mov x0, x19
492
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100493 /* Restore non-volatile registers. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000494 ldp x19, x20, [x0, #VCPU_REGS + 8 * 19]
495 ldp x21, x22, [x0, #VCPU_REGS + 8 * 21]
496 ldp x23, x24, [x0, #VCPU_REGS + 8 * 23]
497 ldp x25, x26, [x0, #VCPU_REGS + 8 * 25]
498 ldp x27, x28, [x0, #VCPU_REGS + 8 * 27]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100499
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100500 /* Intentional fallthrough. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100501/**
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100502 * Restore volatile registers and run the given vcpu.
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100503 *
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000504 * x0 is a pointer to the target vcpu.
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100505 */
506vcpu_restore_volatile_and_run:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000507 ldp x4, x5, [x0, #VCPU_REGS + 8 * 4]
508 ldp x6, x7, [x0, #VCPU_REGS + 8 * 6]
509 ldp x8, x9, [x0, #VCPU_REGS + 8 * 8]
510 ldp x10, x11, [x0, #VCPU_REGS + 8 * 10]
511 ldp x12, x13, [x0, #VCPU_REGS + 8 * 12]
512 ldp x14, x15, [x0, #VCPU_REGS + 8 * 14]
513 ldp x16, x17, [x0, #VCPU_REGS + 8 * 16]
514 ldr x18, [x0, #VCPU_REGS + 8 * 18]
515 ldp x29, x30, [x0, #VCPU_REGS + 8 * 29]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100516
517 /* Restore return address & mode. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000518 ldp x1, x2, [x0, #VCPU_REGS + 8 * 31]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100519 msr elr_el2, x1
520 msr spsr_el2, x2
521
522 /* Restore x0..x3, which we have used as scratch before. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000523 ldp x2, x3, [x0, #VCPU_REGS + 8 * 2]
524 ldp x0, x1, [x0, #VCPU_REGS + 8 * 0]
525 eret
526
527.balign 0x40
528/**
529 * Restores volatile registers from stack and returns.
530 */
531restore_from_stack_and_return:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000532 restore_volatile_from_stack el2
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100533 eret