blob: 1e30fe1f13552395afb77e4a3a03ed3f068eeeef [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. */
Fuad Tabba7c299d82019-09-12 13:05:18 +010094 sub x18, x18, #0x16
95 cbnz x18, slow_sync_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010096
Wedson Almeida Filho87009642018-07-02 10:20:07 +010097 /*
Andrew Walbran3a71c982019-09-12 18:22:11 +010098 * Save x4-x17, x29 and x30, which are not saved by the callee, then jump to
99 * HVC handler.
100 */
101 stp x4, x5, [sp, #-16]!
102 stp x6, x7, [sp, #-16]!
103 stp x8, x9, [sp, #-16]!
104 stp x10, x11, [sp, #-16]!
105 stp x12, x13, [sp, #-16]!
106 stp x14, x15, [sp, #-16]!
107 stp x16, x17, [sp, #-16]!
108 stp x29, x30, [sp, #-16]!
109
110 /*
Andrew Walbranfed412e2019-09-02 18:23:16 +0100111 * Make room for hvc_handler_return on stack, and point x8 (the indirect
112 * result location register in the AAPCS64 standard) to it.
113 * hvc_handler_return is returned this way according to paragraph
114 * 5.4.2.B.3 and section 5.5 because it is larger than 16 bytes.
115 */
116 stp xzr, xzr, [sp, #-16]!
117 stp xzr, xzr, [sp, #-16]!
118 stp xzr, xzr, [sp, #-16]!
119 mov x8, sp
120
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100121 bl hvc_handler
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100122
Andrew Walbranfed412e2019-09-02 18:23:16 +0100123 /* Get the hvc_handler_return back off the stack. */
124 ldp x0, x1, [sp], #16
125 ldp x2, x3, [sp], #16
Andrew Walbran3a71c982019-09-12 18:22:11 +0100126 ldr x18, [sp], #16
Andrew Walbranfed412e2019-09-02 18:23:16 +0100127
Andrew Walbran3a71c982019-09-12 18:22:11 +0100128 ldp x29, x30, [sp], #16
Andrew Walbranfed412e2019-09-02 18:23:16 +0100129 ldp x16, x17, [sp], #16
Andrew Walbran3a71c982019-09-12 18:22:11 +0100130 ldp x14, x15, [sp], #16
131 ldp x12, x13, [sp], #16
132 ldp x10, x11, [sp], #16
133 ldp x8, x9, [sp], #16
134 ldp x6, x7, [sp], #16
135 ldp x4, x5, [sp], #16
Wedson Almeida Filho450ccb82018-08-12 16:25:36 +0100136
Andrew Walbran3a71c982019-09-12 18:22:11 +0100137 cbnz x18, sync_lower_switch
Wedson Almeida Filho450ccb82018-08-12 16:25:36 +0100138 /* Restore x18, which was saved on the stack. */
139 ldr x18, [sp], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100140 eret
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000141.endm
142
143/**
144 * The following is the exception table. A pointer to it will be stored in
145 * register vbar_el2.
146 */
147.section .text.vector_table_el2, "ax"
148.global vector_table_el2
149.balign 0x800
150vector_table_el2:
151sync_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000152 current_exception_sp0 el2 sync_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000153
154.balign 0x80
155irq_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000156 current_exception_sp0 el2 irq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000157
158.balign 0x80
159fiq_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000160 current_exception_sp0 el2 fiq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000161
162.balign 0x80
163serr_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000164 current_exception_sp0 el2 serr_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000165
166.balign 0x80
167sync_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000168 current_exception_spx el2 sync_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000169
170.balign 0x80
171irq_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000172 current_exception_spx el2 irq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000173
174.balign 0x80
175fiq_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000176 current_exception_spx el2 fiq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000177
178.balign 0x80
179serr_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000180 current_exception_spx el2 serr_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000181
182.balign 0x80
183sync_lower_64:
184 lower_sync_exception
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100185
186.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000187irq_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000188 lower_exception irq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100189
190.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000191fiq_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000192 lower_exception fiq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100193
194.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000195serr_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000196 lower_exception serr_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100197
198.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000199sync_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000200 lower_sync_exception
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100201
202.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000203irq_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000204 lower_exception irq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100205
206.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000207fiq_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000208 lower_exception fiq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100209
210.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000211serr_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000212 lower_exception serr_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100213
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000214.balign 0x40
215slow_sync_lower:
216 /* The caller must have saved x18, so we don't save it here. */
217 save_volatile_to_vcpu
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100218
Fuad Tabba7c299d82019-09-12 13:05:18 +0100219 /* Extract the exception class (EC) from exception syndrome register. */
220 mrs x18, esr_el2
221 lsr x18, x18, #26
222
223 /* Take the system register path for EC 0x18. */
224 sub x18, x18, #0x18
225 cbz x18, system_register_access
226
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100227 /* Read syndrome register and call C handler. */
228 mrs x0, esr_el2
229 bl sync_lower_exception
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100230 cbnz x0, vcpu_switch
231
232 /* vcpu is not changing. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000233 mrs x0, tpidr_el2
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100234 b vcpu_restore_volatile_and_run
235
Fuad Tabba7c299d82019-09-12 13:05:18 +0100236/**
237 * Handle accesses to system registers (EC=0x18) and return to original caller.
238 */
239system_register_access:
240 /*
241 * Non-volatile registers are (conservatively) saved because the handler
242 * can clobber non-volatile registers that are used by the msr/mrs,
243 * which results in the wrong value being read or written.
244 */
245 /* Get the current vcpu. */
246 mrs x18, tpidr_el2
247 stp x19, x20, [x18, #VCPU_REGS + 8 * 19]
248 stp x21, x22, [x18, #VCPU_REGS + 8 * 21]
249 stp x23, x24, [x18, #VCPU_REGS + 8 * 23]
250 stp x25, x26, [x18, #VCPU_REGS + 8 * 25]
251 stp x27, x28, [x18, #VCPU_REGS + 8 * 27]
252
253 /* Read syndrome register and call C handler. */
254 mrs x0, esr_el2
255 bl handle_system_register_access
256 cbnz x0, vcpu_switch
257
258 /* vcpu is not changing. */
259 mrs x0, tpidr_el2
260 b vcpu_restore_nonvolatile_and_run
261
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000262sync_lower_switch:
Andrew Walbran3a71c982019-09-12 18:22:11 +0100263 /* Store new vcpu on stack temporarily so we can use x18 for the old one. */
264 str x18, [sp, #-16]!
265
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100266 /* We'll have to switch, so save volatile state before doing so. */
267 mrs x18, tpidr_el2
268
Andrew Walbran3a71c982019-09-12 18:22:11 +0100269 /* Store volatile registers. */
Andrew Walbranfed412e2019-09-02 18:23:16 +0100270 stp x0, x1, [x18, #VCPU_REGS + 8 * 0]
271 stp x2, x3, [x18, #VCPU_REGS + 8 * 2]
Andrew Walbran3a71c982019-09-12 18:22:11 +0100272 stp x4, x5, [x18, #VCPU_REGS + 8 * 4]
273 stp x6, x7, [x18, #VCPU_REGS + 8 * 6]
274 stp x8, x9, [x18, #VCPU_REGS + 8 * 8]
275 stp x10, x11, [x18, #VCPU_REGS + 8 * 10]
276 stp x12, x13, [x18, #VCPU_REGS + 8 * 12]
277 stp x14, x15, [x18, #VCPU_REGS + 8 * 14]
278 stp x16, x17, [x18, #VCPU_REGS + 8 * 16]
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000279 stp x29, x30, [x18, #VCPU_REGS + 8 * 29]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100280
Andrew Walbran3a71c982019-09-12 18:22:11 +0100281 /* Now we can pop the new vcpu to a volatile register that is now available. */
282 ldr x0, [sp], #16
283
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100284 /* x18 was saved on the stack, so we move it to vcpu regs buffer. */
285 ldr x2, [sp], #16
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000286 str x2, [x18, #VCPU_REGS + 8 * 18]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100287
288 /* Save return address & mode. */
289 mrs x2, elr_el2
290 mrs x3, spsr_el2
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000291 stp x2, x3, [x18, #VCPU_REGS + 8 * 31]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100292
293 /* Save lazy state, then switch to new vcpu. */
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100294
295 /* Intentional fallthrough. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100296/**
297 * Switch to a new vcpu.
298 *
299 * All volatile registers from the old vcpu have already been saved. We need
300 * to save only non-volatile ones from the old vcpu, and restore all from the
301 * new one.
302 *
303 * x0 is a pointer to the new vcpu.
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100304 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100305vcpu_switch:
306 /* Save non-volatile registers. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000307 mrs x1, tpidr_el2
308 stp x19, x20, [x1, #VCPU_REGS + 8 * 19]
309 stp x21, x22, [x1, #VCPU_REGS + 8 * 21]
310 stp x23, x24, [x1, #VCPU_REGS + 8 * 23]
311 stp x25, x26, [x1, #VCPU_REGS + 8 * 25]
312 stp x27, x28, [x1, #VCPU_REGS + 8 * 27]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100313
314 /* Save lazy state. */
Fuad Tabba5e147a92019-08-14 15:30:30 +0100315 /* Use x28 as the base */
316 add x28, x1, #VCPU_LAZY
317
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100318 mrs x24, vmpidr_el2
319 mrs x25, csselr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100320 stp x24, x25, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100321
322 mrs x2, sctlr_el1
323 mrs x3, actlr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100324 stp x2, x3, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100325
326 mrs x4, cpacr_el1
327 mrs x5, ttbr0_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100328 stp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100329
330 mrs x6, ttbr1_el1
331 mrs x7, tcr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100332 stp x6, x7, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100333
334 mrs x8, esr_el1
335 mrs x9, afsr0_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100336 stp x8, x9, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100337
338 mrs x10, afsr1_el1
339 mrs x11, far_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100340 stp x10, x11, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100341
342 mrs x12, mair_el1
343 mrs x13, vbar_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100344 stp x12, x13, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100345
346 mrs x14, contextidr_el1
347 mrs x15, tpidr_el0
Fuad Tabba5e147a92019-08-14 15:30:30 +0100348 stp x14, x15, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100349
350 mrs x16, tpidrro_el0
351 mrs x17, tpidr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100352 stp x16, x17, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100353
354 mrs x18, amair_el1
355 mrs x19, cntkctl_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100356 stp x18, x19, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100357
358 mrs x20, sp_el0
359 mrs x21, sp_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100360 stp x20, x21, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100361
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000362 mrs x22, elr_el1
363 mrs x23, spsr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100364 stp x22, x23, [x28], #16
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100365
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000366 mrs x24, par_el1
367 mrs x25, hcr_el2
Fuad Tabba5e147a92019-08-14 15:30:30 +0100368 stp x24, x25, [x28], #16
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100369
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000370 mrs x26, cptr_el2
371 mrs x27, cnthctl_el2
Fuad Tabba5e147a92019-08-14 15:30:30 +0100372 stp x26, x27, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000373
Fuad Tabba5e147a92019-08-14 15:30:30 +0100374 mrs x4, vttbr_el2
375 mrs x5, mdcr_el2
376 stp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100377
Fuad Tabbac76466d2019-09-06 10:42:12 +0100378 mrs x6, mdscr_el1
379 str x6, [x28], #16
380
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100381 /* Save GIC registers. */
382#if GIC_VERSION == 3 || GIC_VERSION == 4
383 /* Offset is too large, so start from a new base. */
384 add x2, x1, #VCPU_GIC
385
386 mrs x3, ich_hcr_el2
Andrew Walbran4b976f42019-06-05 15:00:50 +0100387 mrs x4, icc_sre_el2
388 stp x3, x4, [x2, #16 * 0]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100389#endif
390
Fuad Tabba5e147a92019-08-14 15:30:30 +0100391 /* Save floating point registers. */
392 /* Use x28 as the base. */
393 add x28, x1, #VCPU_FREGS
394 stp q0, q1, [x28], #32
395 stp q2, q3, [x28], #32
396 stp q4, q5, [x28], #32
397 stp q6, q7, [x28], #32
398 stp q8, q9, [x28], #32
399 stp q10, q11, [x28], #32
400 stp q12, q13, [x28], #32
401 stp q14, q15, [x28], #32
402 stp q16, q17, [x28], #32
403 stp q18, q19, [x28], #32
404 stp q20, q21, [x28], #32
405 stp q22, q23, [x28], #32
406 stp q24, q25, [x28], #32
407 stp q26, q27, [x28], #32
408 stp q28, q29, [x28], #32
409 stp q30, q31, [x28], #32
Conrad Groblera824af62019-03-22 17:33:23 +0000410 mrs x3, fpsr
411 mrs x4, fpcr
Fuad Tabba5e147a92019-08-14 15:30:30 +0100412 stp x3, x4, [x28], #32
Conrad Groblera824af62019-03-22 17:33:23 +0000413
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000414 /* Save new vcpu pointer in non-volatile register. */
415 mov x19, x0
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100416
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000417 /*
418 * Save peripheral registers, and inform the arch-independent sections
419 * that registers have been saved.
420 */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000421 mov x0, x1
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000422 bl complete_saving_state
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000423 mov x0, x19
424
425 /* Intentional fallthrough. */
Andrew Walbran375f4532019-07-09 16:54:37 +0100426.global vcpu_restore_all_and_run
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100427vcpu_restore_all_and_run:
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100428 /* Update pointer to current vcpu. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100429 msr tpidr_el2, x0
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100430
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000431 /* Restore peripheral registers. */
432 mov x19, x0
433 bl begin_restoring_state
434 mov x0, x19
435
Conrad Groblera824af62019-03-22 17:33:23 +0000436 /*
437 * Restore floating point registers.
438 *
439 * Offset is too large, so start from a new base.
440 */
441 add x2, x0, #VCPU_FREGS
442 ldp q0, q1, [x2, #32 * 0]
443 ldp q2, q3, [x2, #32 * 1]
444 ldp q4, q5, [x2, #32 * 2]
445 ldp q6, q7, [x2, #32 * 3]
446 ldp q8, q9, [x2, #32 * 4]
447 ldp q10, q11, [x2, #32 * 5]
448 ldp q12, q13, [x2, #32 * 6]
449 ldp q14, q15, [x2, #32 * 7]
450 ldp q16, q17, [x2, #32 * 8]
451 ldp q18, q19, [x2, #32 * 9]
452 ldp q20, q21, [x2, #32 * 10]
453 ldp q22, q23, [x2, #32 * 11]
454 ldp q24, q25, [x2, #32 * 12]
455 ldp q26, q27, [x2, #32 * 13]
456 ldp q28, q29, [x2, #32 * 14]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100457 /* Offset becomes too large, so move the base. */
Conrad Groblera824af62019-03-22 17:33:23 +0000458 ldp q30, q31, [x2, #32 * 15]!
459 ldp x3, x4, [x2, #32 * 1]
460 msr fpsr, x3
Conrad Groblera824af62019-03-22 17:33:23 +0000461
Conrad Grobler02ff6af2019-06-04 09:40:28 +0100462 /*
463 * Only restore FPCR if changed, to avoid expensive
464 * self-synchronising operation where possible.
465 */
466 mrs x5, fpcr
467 cmp x5, x4
468 b.eq vcpu_restore_lazy_and_run
469 msr fpcr, x4
470 /* Intentional fallthrough. */
471
472vcpu_restore_lazy_and_run:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000473 /* Restore lazy registers. */
Fuad Tabba5e147a92019-08-14 15:30:30 +0100474 /* Use x28 as the base. */
475 add x28, x0, #VCPU_LAZY
476
477 ldp x24, x25, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100478 msr vmpidr_el2, x24
479 msr csselr_el1, x25
480
Fuad Tabba5e147a92019-08-14 15:30:30 +0100481 ldp x2, x3, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100482 msr sctlr_el1, x2
483 msr actlr_el1, x3
484
Fuad Tabba5e147a92019-08-14 15:30:30 +0100485 ldp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100486 msr cpacr_el1, x4
487 msr ttbr0_el1, x5
488
Fuad Tabba5e147a92019-08-14 15:30:30 +0100489 ldp x6, x7, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100490 msr ttbr1_el1, x6
491 msr tcr_el1, x7
492
Fuad Tabba5e147a92019-08-14 15:30:30 +0100493 ldp x8, x9, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100494 msr esr_el1, x8
495 msr afsr0_el1, x9
496
Fuad Tabba5e147a92019-08-14 15:30:30 +0100497 ldp x10, x11, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100498 msr afsr1_el1, x10
499 msr far_el1, x11
500
Fuad Tabba5e147a92019-08-14 15:30:30 +0100501 ldp x12, x13, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100502 msr mair_el1, x12
503 msr vbar_el1, x13
504
Fuad Tabba5e147a92019-08-14 15:30:30 +0100505 ldp x14, x15, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100506 msr contextidr_el1, x14
507 msr tpidr_el0, x15
508
Fuad Tabba5e147a92019-08-14 15:30:30 +0100509 ldp x16, x17, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100510 msr tpidrro_el0, x16
511 msr tpidr_el1, x17
512
Fuad Tabba5e147a92019-08-14 15:30:30 +0100513 ldp x18, x19, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100514 msr amair_el1, x18
515 msr cntkctl_el1, x19
516
Fuad Tabba5e147a92019-08-14 15:30:30 +0100517 ldp x20, x21, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100518 msr sp_el0, x20
519 msr sp_el1, x21
520
Fuad Tabba5e147a92019-08-14 15:30:30 +0100521 ldp x22, x23, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000522 msr elr_el1, x22
523 msr spsr_el1, x23
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100524
Fuad Tabba5e147a92019-08-14 15:30:30 +0100525 ldp x24, x25, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000526 msr par_el1, x24
527 msr hcr_el2, x25
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100528
Fuad Tabba5e147a92019-08-14 15:30:30 +0100529 ldp x26, x27, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000530 msr cptr_el2, x26
531 msr cnthctl_el2, x27
532
Fuad Tabba5e147a92019-08-14 15:30:30 +0100533 ldp x4, x5, [x28], #16
534 msr vttbr_el2, x4
535 msr mdcr_el2, x5
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100536
Fuad Tabbac76466d2019-09-06 10:42:12 +0100537 ldr x6, [x28], #16
538 msr mdscr_el1, x6
539
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100540 /* Restore GIC registers. */
541#if GIC_VERSION == 3 || GIC_VERSION == 4
542 /* Offset is too large, so start from a new base. */
543 add x2, x0, #VCPU_GIC
544
Andrew Walbran4b976f42019-06-05 15:00:50 +0100545 ldp x3, x4, [x2, #16 * 0]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100546 msr ich_hcr_el2, x3
Andrew Walbran4b976f42019-06-05 15:00:50 +0100547 msr icc_sre_el2, x4
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100548#endif
549
Andrew Walbran1f32e722019-06-07 17:57:26 +0100550 /*
551 * If a different vCPU is being run on this physical CPU to the last one
552 * which was run for this VM, invalidate the TLB. This must be called
553 * after vttbr_el2 has been updated, so that we have the page table and
554 * VMID of the vCPU to which we are switching.
555 */
556 mov x19, x0
557 bl maybe_invalidate_tlb
558 mov x0, x19
559
Fuad Tabba7c299d82019-09-12 13:05:18 +0100560 /* Intentional fallthrough. */
561
562vcpu_restore_nonvolatile_and_run:
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100563 /* Restore non-volatile registers. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000564 ldp x19, x20, [x0, #VCPU_REGS + 8 * 19]
565 ldp x21, x22, [x0, #VCPU_REGS + 8 * 21]
566 ldp x23, x24, [x0, #VCPU_REGS + 8 * 23]
567 ldp x25, x26, [x0, #VCPU_REGS + 8 * 25]
568 ldp x27, x28, [x0, #VCPU_REGS + 8 * 27]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100569
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100570 /* Intentional fallthrough. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100571/**
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100572 * Restore volatile registers and run the given vcpu.
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100573 *
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000574 * x0 is a pointer to the target vcpu.
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100575 */
576vcpu_restore_volatile_and_run:
Fuad Tabba7c299d82019-09-12 13:05:18 +0100577 ldp x4, x5, [x0, #VCPU_REGS + 8 * 4]
578 ldp x6, x7, [x0, #VCPU_REGS + 8 * 6]
579 ldp x8, x9, [x0, #VCPU_REGS + 8 * 8]
580 ldp x10, x11, [x0, #VCPU_REGS + 8 * 10]
581 ldp x12, x13, [x0, #VCPU_REGS + 8 * 12]
582 ldp x14, x15, [x0, #VCPU_REGS + 8 * 14]
583 ldp x16, x17, [x0, #VCPU_REGS + 8 * 16]
584 ldr x18, [x0, #VCPU_REGS + 8 * 18]
585 ldp x29, x30, [x0, #VCPU_REGS + 8 * 29]
586
587 /* Restore return address & mode. */
588 ldp x1, x2, [x0, #VCPU_REGS + 8 * 31]
589 msr elr_el2, x1
590 msr spsr_el2, x2
591
592 /* Restore x0..x3, which we have used as scratch before. */
593 ldp x2, x3, [x0, #VCPU_REGS + 8 * 2]
594 ldp x0, x1, [x0, #VCPU_REGS + 8 * 0]
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000595 eret
596
597.balign 0x40
598/**
Fuad Tabbac76466d2019-09-06 10:42:12 +0100599 * Restore volatile registers from stack and return to original caller.
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000600 */
601restore_from_stack_and_return:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000602 restore_volatile_from_stack el2
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100603 eret