blob: 4a89f47dfba3df2eda9e73fceba2b107c0aae023 [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/**
Andrew Walbran59182d52019-09-23 17:55:39 +010021 * Saves the volatile registers into the register buffer of the current vcpu.
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000022 */
Andrew Walbran59182d52019-09-23 17:55:39 +010023.macro save_volatile_to_vcpu
Wedson Almeida Filho5bc0b4c2018-07-30 15:31:44 +010024 /*
25 * Save x18 since we're about to clobber it. We subtract 16 instead of
26 * 8 from the stack pointer to keep it 16-byte aligned.
27 */
28 str x18, [sp, #-16]!
Andrew Walbran59182d52019-09-23 17:55:39 +010029
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000030 /* Get the current vcpu. */
31 mrs x18, tpidr_el2
32 stp x0, x1, [x18, #VCPU_REGS + 8 * 0]
33 stp x2, x3, [x18, #VCPU_REGS + 8 * 2]
34 stp x4, x5, [x18, #VCPU_REGS + 8 * 4]
35 stp x6, x7, [x18, #VCPU_REGS + 8 * 6]
36 stp x8, x9, [x18, #VCPU_REGS + 8 * 8]
37 stp x10, x11, [x18, #VCPU_REGS + 8 * 10]
38 stp x12, x13, [x18, #VCPU_REGS + 8 * 12]
39 stp x14, x15, [x18, #VCPU_REGS + 8 * 14]
40 stp x16, x17, [x18, #VCPU_REGS + 8 * 16]
41 stp x29, x30, [x18, #VCPU_REGS + 8 * 29]
42
43 /* x18 was saved on the stack, so we move it to vcpu regs buffer. */
44 ldr x0, [sp], #16
45 str x0, [x18, #VCPU_REGS + 8 * 18]
46
47 /* Save return address & mode. */
48 mrs x1, elr_el2
49 mrs x2, spsr_el2
50 stp x1, x2, [x18, #VCPU_REGS + 8 * 31]
51.endm
52
53/**
54 * This is a generic handler for exceptions taken at a lower EL. It saves the
55 * volatile registers to the current vcpu and calls the C handler, which can
56 * select one of two paths: (a) restore volatile registers and return, or
57 * (b) switch to a different vcpu. In the latter case, the handler needs to save
58 * all non-volatile registers (they haven't been saved yet), then restore all
59 * registers from the new vcpu.
60 */
61.macro lower_exception handler:req
Andrew Walbran59182d52019-09-23 17:55:39 +010062 save_volatile_to_vcpu
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000063
64 /* Call C handler. */
65 bl \handler
66
67 /* Switch vcpu if requested by handler. */
68 cbnz x0, vcpu_switch
69
70 /* vcpu is not changing. */
71 mrs x0, tpidr_el2
72 b vcpu_restore_volatile_and_run
73.endm
74
75/**
Andrew Walbran59182d52019-09-23 17:55:39 +010076 * This is the handler for a sync exception taken at a lower EL.
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000077 */
78.macro lower_sync_exception
Andrew Walbran59182d52019-09-23 17:55:39 +010079 save_volatile_to_vcpu
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010080
81 /* Extract the exception class (EC) from exception syndrome register. */
82 mrs x18, esr_el2
83 lsr x18, x18, #26
84
Andrew Walbran59182d52019-09-23 17:55:39 +010085 /* Take the system register path for EC 0x18. */
86 sub x18, x18, #0x18
87 cbz x18, system_register_access
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010088
Andrew Walbran59182d52019-09-23 17:55:39 +010089 /* Read syndrome register and call C handler. */
90 mrs x0, esr_el2
91 bl sync_lower_exception
Andrew Walbran3a71c982019-09-12 18:22:11 +010092
Andrew Walbran59182d52019-09-23 17:55:39 +010093 /* Switch vcpu if requested by handler. */
94 cbnz x0, vcpu_switch
Andrew Walbranfed412e2019-09-02 18:23:16 +010095
Andrew Walbran59182d52019-09-23 17:55:39 +010096 /* vcpu is not changing. */
97 mrs x0, tpidr_el2
98 b vcpu_restore_volatile_and_run
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +000099.endm
100
101/**
102 * The following is the exception table. A pointer to it will be stored in
103 * register vbar_el2.
104 */
105.section .text.vector_table_el2, "ax"
106.global vector_table_el2
107.balign 0x800
108vector_table_el2:
109sync_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000110 current_exception_sp0 el2 sync_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000111
112.balign 0x80
113irq_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000114 current_exception_sp0 el2 irq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000115
116.balign 0x80
117fiq_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000118 current_exception_sp0 el2 fiq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000119
120.balign 0x80
121serr_cur_sp0:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000122 current_exception_sp0 el2 serr_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000123
124.balign 0x80
125sync_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000126 current_exception_spx el2 sync_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000127
128.balign 0x80
129irq_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000130 current_exception_spx el2 irq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000131
132.balign 0x80
133fiq_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000134 current_exception_spx el2 fiq_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000135
136.balign 0x80
137serr_cur_spx:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000138 current_exception_spx el2 serr_current_exception
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000139
140.balign 0x80
141sync_lower_64:
142 lower_sync_exception
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100143
144.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000145irq_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000146 lower_exception irq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100147
148.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000149fiq_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000150 lower_exception fiq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100151
152.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000153serr_lower_64:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000154 lower_exception serr_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100155
156.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000157sync_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000158 lower_sync_exception
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100159
160.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000161irq_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000162 lower_exception irq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100163
164.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000165fiq_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000166 lower_exception fiq_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100167
168.balign 0x80
Andrew Walbran83f61322018-11-12 13:29:30 +0000169serr_lower_32:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000170 lower_exception serr_lower
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100171
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000172.balign 0x40
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100173
Fuad Tabba7c299d82019-09-12 13:05:18 +0100174/**
175 * Handle accesses to system registers (EC=0x18) and return to original caller.
176 */
177system_register_access:
178 /*
179 * Non-volatile registers are (conservatively) saved because the handler
180 * can clobber non-volatile registers that are used by the msr/mrs,
181 * which results in the wrong value being read or written.
182 */
183 /* Get the current vcpu. */
184 mrs x18, tpidr_el2
185 stp x19, x20, [x18, #VCPU_REGS + 8 * 19]
186 stp x21, x22, [x18, #VCPU_REGS + 8 * 21]
187 stp x23, x24, [x18, #VCPU_REGS + 8 * 23]
188 stp x25, x26, [x18, #VCPU_REGS + 8 * 25]
189 stp x27, x28, [x18, #VCPU_REGS + 8 * 27]
190
191 /* Read syndrome register and call C handler. */
192 mrs x0, esr_el2
193 bl handle_system_register_access
194 cbnz x0, vcpu_switch
195
196 /* vcpu is not changing. */
197 mrs x0, tpidr_el2
198 b vcpu_restore_nonvolatile_and_run
199
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000200sync_lower_switch:
Andrew Walbran3a71c982019-09-12 18:22:11 +0100201 /* Store new vcpu on stack temporarily so we can use x18 for the old one. */
202 str x18, [sp, #-16]!
203
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100204 /* We'll have to switch, so save volatile state before doing so. */
205 mrs x18, tpidr_el2
206
Andrew Walbran3a71c982019-09-12 18:22:11 +0100207 /* Store volatile registers. */
Andrew Walbranfed412e2019-09-02 18:23:16 +0100208 stp x0, x1, [x18, #VCPU_REGS + 8 * 0]
209 stp x2, x3, [x18, #VCPU_REGS + 8 * 2]
Andrew Walbran3a71c982019-09-12 18:22:11 +0100210 stp x4, x5, [x18, #VCPU_REGS + 8 * 4]
211 stp x6, x7, [x18, #VCPU_REGS + 8 * 6]
212 stp x8, x9, [x18, #VCPU_REGS + 8 * 8]
213 stp x10, x11, [x18, #VCPU_REGS + 8 * 10]
214 stp x12, x13, [x18, #VCPU_REGS + 8 * 12]
215 stp x14, x15, [x18, #VCPU_REGS + 8 * 14]
216 stp x16, x17, [x18, #VCPU_REGS + 8 * 16]
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000217 stp x29, x30, [x18, #VCPU_REGS + 8 * 29]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100218
Andrew Walbran3a71c982019-09-12 18:22:11 +0100219 /* Now we can pop the new vcpu to a volatile register that is now available. */
220 ldr x0, [sp], #16
221
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100222 /* x18 was saved on the stack, so we move it to vcpu regs buffer. */
223 ldr x2, [sp], #16
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000224 str x2, [x18, #VCPU_REGS + 8 * 18]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100225
226 /* Save return address & mode. */
227 mrs x2, elr_el2
228 mrs x3, spsr_el2
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000229 stp x2, x3, [x18, #VCPU_REGS + 8 * 31]
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100230
231 /* Save lazy state, then switch to new vcpu. */
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100232
233 /* Intentional fallthrough. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100234/**
235 * Switch to a new vcpu.
236 *
237 * All volatile registers from the old vcpu have already been saved. We need
238 * to save only non-volatile ones from the old vcpu, and restore all from the
239 * new one.
240 *
241 * x0 is a pointer to the new vcpu.
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100242 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100243vcpu_switch:
244 /* Save non-volatile registers. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000245 mrs x1, tpidr_el2
246 stp x19, x20, [x1, #VCPU_REGS + 8 * 19]
247 stp x21, x22, [x1, #VCPU_REGS + 8 * 21]
248 stp x23, x24, [x1, #VCPU_REGS + 8 * 23]
249 stp x25, x26, [x1, #VCPU_REGS + 8 * 25]
250 stp x27, x28, [x1, #VCPU_REGS + 8 * 27]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100251
252 /* Save lazy state. */
Fuad Tabba5e147a92019-08-14 15:30:30 +0100253 /* Use x28 as the base */
254 add x28, x1, #VCPU_LAZY
255
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100256 mrs x24, vmpidr_el2
257 mrs x25, csselr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100258 stp x24, x25, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100259
260 mrs x2, sctlr_el1
261 mrs x3, actlr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100262 stp x2, x3, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100263
264 mrs x4, cpacr_el1
265 mrs x5, ttbr0_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100266 stp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100267
268 mrs x6, ttbr1_el1
269 mrs x7, tcr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100270 stp x6, x7, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100271
272 mrs x8, esr_el1
273 mrs x9, afsr0_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100274 stp x8, x9, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100275
276 mrs x10, afsr1_el1
277 mrs x11, far_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100278 stp x10, x11, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100279
280 mrs x12, mair_el1
281 mrs x13, vbar_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100282 stp x12, x13, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100283
284 mrs x14, contextidr_el1
285 mrs x15, tpidr_el0
Fuad Tabba5e147a92019-08-14 15:30:30 +0100286 stp x14, x15, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100287
288 mrs x16, tpidrro_el0
289 mrs x17, tpidr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100290 stp x16, x17, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100291
292 mrs x18, amair_el1
293 mrs x19, cntkctl_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100294 stp x18, x19, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100295
296 mrs x20, sp_el0
297 mrs x21, sp_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100298 stp x20, x21, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100299
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000300 mrs x22, elr_el1
301 mrs x23, spsr_el1
Fuad Tabba5e147a92019-08-14 15:30:30 +0100302 stp x22, x23, [x28], #16
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100303
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000304 mrs x24, par_el1
305 mrs x25, hcr_el2
Fuad Tabba5e147a92019-08-14 15:30:30 +0100306 stp x24, x25, [x28], #16
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100307
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000308 mrs x26, cptr_el2
309 mrs x27, cnthctl_el2
Fuad Tabba5e147a92019-08-14 15:30:30 +0100310 stp x26, x27, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000311
Fuad Tabba5e147a92019-08-14 15:30:30 +0100312 mrs x4, vttbr_el2
313 mrs x5, mdcr_el2
314 stp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100315
Fuad Tabbac76466d2019-09-06 10:42:12 +0100316 mrs x6, mdscr_el1
317 str x6, [x28], #16
318
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
Andrew Walbran4b976f42019-06-05 15:00:50 +0100325 mrs x4, icc_sre_el2
326 stp x3, x4, [x2, #16 * 0]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100327#endif
328
Fuad Tabba5e147a92019-08-14 15:30:30 +0100329 /* Save floating point registers. */
330 /* Use x28 as the base. */
331 add x28, x1, #VCPU_FREGS
332 stp q0, q1, [x28], #32
333 stp q2, q3, [x28], #32
334 stp q4, q5, [x28], #32
335 stp q6, q7, [x28], #32
336 stp q8, q9, [x28], #32
337 stp q10, q11, [x28], #32
338 stp q12, q13, [x28], #32
339 stp q14, q15, [x28], #32
340 stp q16, q17, [x28], #32
341 stp q18, q19, [x28], #32
342 stp q20, q21, [x28], #32
343 stp q22, q23, [x28], #32
344 stp q24, q25, [x28], #32
345 stp q26, q27, [x28], #32
346 stp q28, q29, [x28], #32
347 stp q30, q31, [x28], #32
Conrad Groblera824af62019-03-22 17:33:23 +0000348 mrs x3, fpsr
349 mrs x4, fpcr
Fuad Tabba5e147a92019-08-14 15:30:30 +0100350 stp x3, x4, [x28], #32
Conrad Groblera824af62019-03-22 17:33:23 +0000351
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000352 /* Save new vcpu pointer in non-volatile register. */
353 mov x19, x0
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100354
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000355 /*
356 * Save peripheral registers, and inform the arch-independent sections
357 * that registers have been saved.
358 */
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000359 mov x0, x1
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000360 bl complete_saving_state
Wedson Almeida Filho03306112018-11-26 00:08:03 +0000361 mov x0, x19
362
363 /* Intentional fallthrough. */
Andrew Walbran375f4532019-07-09 16:54:37 +0100364.global vcpu_restore_all_and_run
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100365vcpu_restore_all_and_run:
Wedson Almeida Filho59978322018-10-24 15:13:33 +0100366 /* Update pointer to current vcpu. */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100367 msr tpidr_el2, x0
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100368
Andrew Walbran1f8d4872018-12-20 11:21:32 +0000369 /* Restore peripheral registers. */
370 mov x19, x0
371 bl begin_restoring_state
372 mov x0, x19
373
Conrad Groblera824af62019-03-22 17:33:23 +0000374 /*
375 * Restore floating point registers.
376 *
377 * Offset is too large, so start from a new base.
378 */
379 add x2, x0, #VCPU_FREGS
380 ldp q0, q1, [x2, #32 * 0]
381 ldp q2, q3, [x2, #32 * 1]
382 ldp q4, q5, [x2, #32 * 2]
383 ldp q6, q7, [x2, #32 * 3]
384 ldp q8, q9, [x2, #32 * 4]
385 ldp q10, q11, [x2, #32 * 5]
386 ldp q12, q13, [x2, #32 * 6]
387 ldp q14, q15, [x2, #32 * 7]
388 ldp q16, q17, [x2, #32 * 8]
389 ldp q18, q19, [x2, #32 * 9]
390 ldp q20, q21, [x2, #32 * 10]
391 ldp q22, q23, [x2, #32 * 11]
392 ldp q24, q25, [x2, #32 * 12]
393 ldp q26, q27, [x2, #32 * 13]
394 ldp q28, q29, [x2, #32 * 14]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100395 /* Offset becomes too large, so move the base. */
Conrad Groblera824af62019-03-22 17:33:23 +0000396 ldp q30, q31, [x2, #32 * 15]!
397 ldp x3, x4, [x2, #32 * 1]
398 msr fpsr, x3
Conrad Groblera824af62019-03-22 17:33:23 +0000399
Conrad Grobler02ff6af2019-06-04 09:40:28 +0100400 /*
401 * Only restore FPCR if changed, to avoid expensive
402 * self-synchronising operation where possible.
403 */
404 mrs x5, fpcr
405 cmp x5, x4
406 b.eq vcpu_restore_lazy_and_run
407 msr fpcr, x4
408 /* Intentional fallthrough. */
409
410vcpu_restore_lazy_and_run:
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000411 /* Restore lazy registers. */
Fuad Tabba5e147a92019-08-14 15:30:30 +0100412 /* Use x28 as the base. */
413 add x28, x0, #VCPU_LAZY
414
415 ldp x24, x25, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100416 msr vmpidr_el2, x24
417 msr csselr_el1, x25
418
Fuad Tabba5e147a92019-08-14 15:30:30 +0100419 ldp x2, x3, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100420 msr sctlr_el1, x2
421 msr actlr_el1, x3
422
Fuad Tabba5e147a92019-08-14 15:30:30 +0100423 ldp x4, x5, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100424 msr cpacr_el1, x4
425 msr ttbr0_el1, x5
426
Fuad Tabba5e147a92019-08-14 15:30:30 +0100427 ldp x6, x7, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100428 msr ttbr1_el1, x6
429 msr tcr_el1, x7
430
Fuad Tabba5e147a92019-08-14 15:30:30 +0100431 ldp x8, x9, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100432 msr esr_el1, x8
433 msr afsr0_el1, x9
434
Fuad Tabba5e147a92019-08-14 15:30:30 +0100435 ldp x10, x11, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100436 msr afsr1_el1, x10
437 msr far_el1, x11
438
Fuad Tabba5e147a92019-08-14 15:30:30 +0100439 ldp x12, x13, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100440 msr mair_el1, x12
441 msr vbar_el1, x13
442
Fuad Tabba5e147a92019-08-14 15:30:30 +0100443 ldp x14, x15, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100444 msr contextidr_el1, x14
445 msr tpidr_el0, x15
446
Fuad Tabba5e147a92019-08-14 15:30:30 +0100447 ldp x16, x17, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100448 msr tpidrro_el0, x16
449 msr tpidr_el1, x17
450
Fuad Tabba5e147a92019-08-14 15:30:30 +0100451 ldp x18, x19, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100452 msr amair_el1, x18
453 msr cntkctl_el1, x19
454
Fuad Tabba5e147a92019-08-14 15:30:30 +0100455 ldp x20, x21, [x28], #16
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100456 msr sp_el0, x20
457 msr sp_el1, x21
458
Fuad Tabba5e147a92019-08-14 15:30:30 +0100459 ldp x22, x23, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000460 msr elr_el1, x22
461 msr spsr_el1, x23
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100462
Fuad Tabba5e147a92019-08-14 15:30:30 +0100463 ldp x24, x25, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000464 msr par_el1, x24
465 msr hcr_el2, x25
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100466
Fuad Tabba5e147a92019-08-14 15:30:30 +0100467 ldp x26, x27, [x28], #16
Andrew Walbranbc82f2d2019-02-21 14:50:29 +0000468 msr cptr_el2, x26
469 msr cnthctl_el2, x27
470
Fuad Tabba5e147a92019-08-14 15:30:30 +0100471 ldp x4, x5, [x28], #16
472 msr vttbr_el2, x4
473 msr mdcr_el2, x5
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100474
Fuad Tabbac76466d2019-09-06 10:42:12 +0100475 ldr x6, [x28], #16
476 msr mdscr_el1, x6
477
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100478 /* Restore GIC registers. */
479#if GIC_VERSION == 3 || GIC_VERSION == 4
480 /* Offset is too large, so start from a new base. */
481 add x2, x0, #VCPU_GIC
482
Andrew Walbran4b976f42019-06-05 15:00:50 +0100483 ldp x3, x4, [x2, #16 * 0]
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100484 msr ich_hcr_el2, x3
Andrew Walbran4b976f42019-06-05 15:00:50 +0100485 msr icc_sre_el2, x4
Andrew Walbranb208b4a2019-05-20 12:42:22 +0100486#endif
487
Andrew Walbran1f32e722019-06-07 17:57:26 +0100488 /*
489 * If a different vCPU is being run on this physical CPU to the last one
490 * which was run for this VM, invalidate the TLB. This must be called
491 * after vttbr_el2 has been updated, so that we have the page table and
492 * VMID of the vCPU to which we are switching.
493 */
494 mov x19, x0
495 bl maybe_invalidate_tlb
496 mov x0, x19
497
Fuad Tabba7c299d82019-09-12 13:05:18 +0100498 /* Intentional fallthrough. */
499
500vcpu_restore_nonvolatile_and_run:
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100501 /* Restore non-volatile registers. */
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000502 ldp x19, x20, [x0, #VCPU_REGS + 8 * 19]
503 ldp x21, x22, [x0, #VCPU_REGS + 8 * 21]
504 ldp x23, x24, [x0, #VCPU_REGS + 8 * 23]
505 ldp x25, x26, [x0, #VCPU_REGS + 8 * 25]
506 ldp x27, x28, [x0, #VCPU_REGS + 8 * 27]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100507
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100508 /* Intentional fallthrough. */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100509/**
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100510 * Restore volatile registers and run the given vcpu.
Wedson Almeida Filhod615cdb2018-10-09 13:00:21 +0100511 *
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000512 * x0 is a pointer to the target vcpu.
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100513 */
514vcpu_restore_volatile_and_run:
Fuad Tabba7c299d82019-09-12 13:05:18 +0100515 ldp x4, x5, [x0, #VCPU_REGS + 8 * 4]
516 ldp x6, x7, [x0, #VCPU_REGS + 8 * 6]
517 ldp x8, x9, [x0, #VCPU_REGS + 8 * 8]
518 ldp x10, x11, [x0, #VCPU_REGS + 8 * 10]
519 ldp x12, x13, [x0, #VCPU_REGS + 8 * 12]
520 ldp x14, x15, [x0, #VCPU_REGS + 8 * 14]
521 ldp x16, x17, [x0, #VCPU_REGS + 8 * 16]
522 ldr x18, [x0, #VCPU_REGS + 8 * 18]
523 ldp x29, x30, [x0, #VCPU_REGS + 8 * 29]
524
525 /* Restore return address & mode. */
526 ldp x1, x2, [x0, #VCPU_REGS + 8 * 31]
527 msr elr_el2, x1
528 msr spsr_el2, x2
529
530 /* Restore x0..x3, which we have used as scratch before. */
531 ldp x2, x3, [x0, #VCPU_REGS + 8 * 2]
532 ldp x0, x1, [x0, #VCPU_REGS + 8 * 0]
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000533 eret
534
535.balign 0x40
536/**
Fuad Tabbac76466d2019-09-06 10:42:12 +0100537 * Restore volatile registers from stack and return to original caller.
Wedson Almeida Filho9d5040f2018-10-29 08:41:27 +0000538 */
539restore_from_stack_and_return:
Andrew Walbranc55365d2018-12-06 15:45:11 +0000540 restore_volatile_from_stack el2
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100541 eret