1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2015 Mihai Carabas <mihai.carabas@gmail.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/smp.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/mman.h>
36 #include <sys/pcpu.h>
37 #include <sys/proc.h>
38 #include <sys/sysctl.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/vmem.h>
42
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <vm/vm_extern.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_param.h>
49
50 #include <machine/vm.h>
51 #include <machine/cpufunc.h>
52 #include <machine/cpu.h>
53 #include <machine/machdep.h>
54 #include <machine/vmm.h>
55 #include <machine/atomic.h>
56 #include <machine/hypervisor.h>
57 #include <machine/pmap.h>
58
59 #include <dev/vmm/vmm_mem.h>
60 #include <dev/vmm/vmm_vm.h>
61
62 #include "mmu.h"
63 #include "arm64.h"
64 #include "hyp.h"
65 #include "reset.h"
66 #include "io/vgic.h"
67 #include "io/vgic_v3.h"
68 #include "io/vtimer.h"
69 #include "vmm_handlers.h"
70 #include "vmm_stat.h"
71
72 #define HANDLED 1
73 #define UNHANDLED 0
74
75 /* Number of bits in an EL2 virtual address */
76 #define EL2_VIRT_BITS 48
77 CTASSERT((1ul << EL2_VIRT_BITS) >= HYP_VM_MAX_ADDRESS);
78
79 /* TODO: Move the host hypctx off the stack */
80 #define VMM_STACK_PAGES 4
81 #define VMM_STACK_SIZE (VMM_STACK_PAGES * PAGE_SIZE)
82
83 static int vmm_pmap_levels, vmm_virt_bits, vmm_max_ipa_bits;
84
85 /* Register values passed to arm_setup_vectors to set in the hypervisor */
86 struct vmm_init_regs {
87 uint64_t tcr_el2;
88 uint64_t vtcr_el2;
89 };
90
91 MALLOC_DEFINE(M_HYP, "ARM VMM HYP", "ARM VMM HYP");
92
93 extern char hyp_init_vectors[];
94 extern char hyp_vectors[];
95 extern char hyp_stub_vectors[];
96
97 static vm_paddr_t hyp_code_base;
98 static size_t hyp_code_len;
99
100 static char *stack[MAXCPU];
101 static vm_offset_t stack_hyp_va[MAXCPU];
102
103 static vmem_t *el2_mem_alloc;
104
105 static void arm_setup_vectors(void *arg);
106
107 DPCPU_DEFINE_STATIC(struct hypctx *, vcpu);
108
109 static inline void
arm64_set_active_vcpu(struct hypctx * hypctx)110 arm64_set_active_vcpu(struct hypctx *hypctx)
111 {
112 DPCPU_SET(vcpu, hypctx);
113 }
114
115 struct hypctx *
arm64_get_active_vcpu(void)116 arm64_get_active_vcpu(void)
117 {
118 return (DPCPU_GET(vcpu));
119 }
120
121 static void
arm_setup_vectors(void * arg)122 arm_setup_vectors(void *arg)
123 {
124 struct vmm_init_regs *el2_regs;
125 uintptr_t stack_top;
126 uint32_t sctlr_el2;
127 register_t daif;
128
129 el2_regs = arg;
130 arm64_set_active_vcpu(NULL);
131
132 /*
133 * Configure the system control register for EL2:
134 *
135 * SCTLR_EL2_M: MMU on
136 * SCTLR_EL2_C: Data cacheability not affected
137 * SCTLR_EL2_I: Instruction cacheability not affected
138 * SCTLR_EL2_A: Instruction alignment check
139 * SCTLR_EL2_SA: Stack pointer alignment check
140 * SCTLR_EL2_WXN: Treat writable memory as execute never
141 * ~SCTLR_EL2_EE: Data accesses are little-endian
142 */
143 sctlr_el2 = SCTLR_EL2_RES1;
144 sctlr_el2 |= SCTLR_EL2_M | SCTLR_EL2_C | SCTLR_EL2_I;
145 sctlr_el2 |= SCTLR_EL2_A | SCTLR_EL2_SA;
146 sctlr_el2 |= SCTLR_EL2_WXN;
147 sctlr_el2 &= ~SCTLR_EL2_EE;
148
149 daif = intr_disable();
150
151 if (in_vhe()) {
152 WRITE_SPECIALREG(vtcr_el2, el2_regs->vtcr_el2);
153 } else {
154 /*
155 * Install the temporary vectors which will be responsible for
156 * initializing the VMM when we next trap into EL2.
157 *
158 * x0: the exception vector table responsible for hypervisor
159 * initialization on the next call.
160 */
161 vmm_call_hyp(vtophys(&vmm_hyp_code));
162
163 /* Create and map the hypervisor stack */
164 stack_top = stack_hyp_va[PCPU_GET(cpuid)] + VMM_STACK_SIZE;
165
166 /* Special call to initialize EL2 */
167 vmm_call_hyp(vmmpmap_to_ttbr0(), stack_top, el2_regs->tcr_el2,
168 sctlr_el2, el2_regs->vtcr_el2);
169 }
170
171 intr_restore(daif);
172 }
173
174 static void
arm_teardown_vectors(void * arg)175 arm_teardown_vectors(void *arg)
176 {
177 register_t daif;
178
179 /*
180 * vmm_cleanup() will disable the MMU. For the next few instructions,
181 * before the hardware disables the MMU, one of the following is
182 * possible:
183 *
184 * a. The instruction addresses are fetched with the MMU disabled,
185 * and they must represent the actual physical addresses. This will work
186 * because we call the vmm_cleanup() function by its physical address.
187 *
188 * b. The instruction addresses are fetched using the old translation
189 * tables. This will work because we have an identity mapping in place
190 * in the translation tables and vmm_cleanup() is called by its physical
191 * address.
192 */
193 daif = intr_disable();
194 /* TODO: Invalidate the cache */
195 vmm_call_hyp(HYP_CLEANUP, vtophys(hyp_stub_vectors));
196 intr_restore(daif);
197
198 arm64_set_active_vcpu(NULL);
199 }
200
201 static uint64_t
vmm_vtcr_el2_sl(u_int levels)202 vmm_vtcr_el2_sl(u_int levels)
203 {
204 #if PAGE_SIZE == PAGE_SIZE_4K
205 switch (levels) {
206 case 2:
207 return (VTCR_EL2_SL0_4K_LVL2);
208 case 3:
209 return (VTCR_EL2_SL0_4K_LVL1);
210 case 4:
211 return (VTCR_EL2_SL0_4K_LVL0);
212 default:
213 panic("%s: Invalid number of page table levels %u", __func__,
214 levels);
215 }
216 #elif PAGE_SIZE == PAGE_SIZE_16K
217 switch (levels) {
218 case 2:
219 return (VTCR_EL2_SL0_16K_LVL2);
220 case 3:
221 return (VTCR_EL2_SL0_16K_LVL1);
222 case 4:
223 return (VTCR_EL2_SL0_16K_LVL0);
224 default:
225 panic("%s: Invalid number of page table levels %u", __func__,
226 levels);
227 }
228 #else
229 #error Unsupported page size
230 #endif
231 }
232
233 int
vmmops_modinit(int ipinum)234 vmmops_modinit(int ipinum)
235 {
236 struct vmm_init_regs el2_regs;
237 vm_offset_t next_hyp_va;
238 vm_paddr_t vmm_base;
239 uint64_t id_aa64mmfr0_el1, pa_range_bits, pa_range_field;
240 int cpu, i;
241 bool rv __diagused;
242
243 if (!has_hyp()) {
244 printf(
245 "vmm: Processor doesn't have support for virtualization\n");
246 return (ENXIO);
247 }
248
249 if (!vgic_present()) {
250 printf("vmm: No vgic found\n");
251 return (ENODEV);
252 }
253
254 if (!get_kernel_reg(ID_AA64MMFR0_EL1, &id_aa64mmfr0_el1)) {
255 printf("vmm: Unable to read ID_AA64MMFR0_EL1\n");
256 return (ENXIO);
257 }
258 pa_range_field = ID_AA64MMFR0_PARange_VAL(id_aa64mmfr0_el1);
259 /*
260 * Use 3 levels to give us up to 39 bits with 4k pages, or
261 * 47 bits with 16k pages.
262 */
263 /* TODO: Check the number of levels for 64k pages */
264 vmm_pmap_levels = 3;
265 switch (pa_range_field) {
266 case ID_AA64MMFR0_PARange_4G:
267 printf("vmm: Not enough physical address bits\n");
268 return (ENXIO);
269 case ID_AA64MMFR0_PARange_64G:
270 vmm_virt_bits = 36;
271 #if PAGE_SIZE == PAGE_SIZE_16K
272 vmm_pmap_levels = 2;
273 #endif
274 break;
275 default:
276 vmm_virt_bits = 39;
277 break;
278 }
279 pa_range_bits = pa_range_field >> ID_AA64MMFR0_PARange_SHIFT;
280
281 if (!in_vhe()) {
282 /* Initialise the EL2 MMU */
283 if (!vmmpmap_init()) {
284 printf("vmm: Failed to init the EL2 MMU\n");
285 return (ENOMEM);
286 }
287 }
288
289 /* Set up the stage 2 pmap callbacks */
290 MPASS(pmap_clean_stage2_tlbi == NULL);
291 pmap_clean_stage2_tlbi = vmm_clean_s2_tlbi;
292 pmap_stage2_invalidate_range = vmm_s2_tlbi_range;
293 pmap_stage2_invalidate_all = vmm_s2_tlbi_all;
294
295 if (!in_vhe()) {
296 /*
297 * Create an allocator for the virtual address space used by
298 * EL2. EL2 code is identity-mapped; the allocator is used to
299 * find space for VM structures.
300 */
301 el2_mem_alloc = vmem_create("VMM EL2", 0, 0, PAGE_SIZE, 0,
302 M_WAITOK);
303
304 /* Create the mappings for the hypervisor translation table. */
305 hyp_code_len = round_page(&vmm_hyp_code_end - &vmm_hyp_code);
306
307 /* We need an physical identity mapping for when we activate the MMU */
308 hyp_code_base = vmm_base = vtophys(&vmm_hyp_code);
309 rv = vmmpmap_enter(vmm_base, hyp_code_len, vmm_base,
310 VM_PROT_READ | VM_PROT_EXECUTE);
311 MPASS(rv);
312
313 next_hyp_va = roundup2(vmm_base + hyp_code_len, L2_SIZE);
314
315 /* Create a per-CPU hypervisor stack */
316 CPU_FOREACH(cpu) {
317 stack[cpu] = malloc(VMM_STACK_SIZE, M_HYP, M_WAITOK | M_ZERO);
318 stack_hyp_va[cpu] = next_hyp_va;
319
320 for (i = 0; i < VMM_STACK_PAGES; i++) {
321 rv = vmmpmap_enter(stack_hyp_va[cpu] + ptoa(i),
322 PAGE_SIZE, vtophys(stack[cpu] + ptoa(i)),
323 VM_PROT_READ | VM_PROT_WRITE);
324 MPASS(rv);
325 }
326 next_hyp_va += L2_SIZE;
327 }
328
329 el2_regs.tcr_el2 = TCR_EL2_RES1;
330 el2_regs.tcr_el2 |= min(pa_range_bits << TCR_EL2_PS_SHIFT,
331 TCR_EL2_PS_52BITS);
332 el2_regs.tcr_el2 |= TCR_EL2_T0SZ(64 - EL2_VIRT_BITS);
333 el2_regs.tcr_el2 |= TCR_EL2_IRGN0_WBWA | TCR_EL2_ORGN0_WBWA;
334 #if PAGE_SIZE == PAGE_SIZE_4K
335 el2_regs.tcr_el2 |= TCR_EL2_TG0_4K;
336 #elif PAGE_SIZE == PAGE_SIZE_16K
337 el2_regs.tcr_el2 |= TCR_EL2_TG0_16K;
338 #else
339 #error Unsupported page size
340 #endif
341 #ifdef SMP
342 el2_regs.tcr_el2 |= TCR_EL2_SH0_IS;
343 #endif
344 }
345
346 switch (pa_range_bits << TCR_EL2_PS_SHIFT) {
347 case TCR_EL2_PS_32BITS:
348 vmm_max_ipa_bits = 32;
349 break;
350 case TCR_EL2_PS_36BITS:
351 vmm_max_ipa_bits = 36;
352 break;
353 case TCR_EL2_PS_40BITS:
354 vmm_max_ipa_bits = 40;
355 break;
356 case TCR_EL2_PS_42BITS:
357 vmm_max_ipa_bits = 42;
358 break;
359 case TCR_EL2_PS_44BITS:
360 vmm_max_ipa_bits = 44;
361 break;
362 case TCR_EL2_PS_48BITS:
363 vmm_max_ipa_bits = 48;
364 break;
365 case TCR_EL2_PS_52BITS:
366 default:
367 vmm_max_ipa_bits = 52;
368 break;
369 }
370
371 /*
372 * Configure the Stage 2 translation control register:
373 *
374 * VTCR_IRGN0_WBWA: Translation table walks access inner cacheable
375 * normal memory
376 * VTCR_ORGN0_WBWA: Translation table walks access outer cacheable
377 * normal memory
378 * VTCR_EL2_TG0_4K/16K: Stage 2 uses the same page size as the kernel
379 * VTCR_EL2_SL0_4K_LVL1: Stage 2 uses concatenated level 1 tables
380 * VTCR_EL2_SH0_IS: Memory associated with Stage 2 walks is inner
381 * shareable
382 */
383 el2_regs.vtcr_el2 = VTCR_EL2_RES1;
384 el2_regs.vtcr_el2 |= VTCR_EL2_IRGN0_WBWA | VTCR_EL2_ORGN0_WBWA;
385 el2_regs.vtcr_el2 |= VTCR_EL2_T0SZ(64 - vmm_virt_bits);
386 el2_regs.vtcr_el2 |= vmm_vtcr_el2_sl(vmm_pmap_levels);
387 #if PAGE_SIZE == PAGE_SIZE_4K
388 el2_regs.vtcr_el2 |= VTCR_EL2_TG0_4K;
389 #elif PAGE_SIZE == PAGE_SIZE_16K
390 el2_regs.vtcr_el2 |= VTCR_EL2_TG0_16K;
391 #else
392 #error Unsupported page size
393 #endif
394 #ifdef SMP
395 el2_regs.vtcr_el2 |= VTCR_EL2_SH0_IS;
396 #endif
397 /*
398 * If FEAT_LPA2 is enabled in the host then we need to enable it here
399 * so the page tables created by pmap.c are correct. The meaning of
400 * the shareability field changes to become address bits when this
401 * is set.
402 */
403 if ((READ_SPECIALREG(tcr_el1) & TCR_DS) != 0) {
404 el2_regs.vtcr_el2 |= VTCR_EL2_DS;
405 el2_regs.vtcr_el2 |=
406 min(pa_range_bits << VTCR_EL2_PS_SHIFT, VTCR_EL2_PS_52BIT);
407 } else {
408 el2_regs.vtcr_el2 |=
409 min(pa_range_bits << VTCR_EL2_PS_SHIFT, VTCR_EL2_PS_48BIT);
410 }
411
412 smp_rendezvous(NULL, arm_setup_vectors, NULL, &el2_regs);
413
414 if (!in_vhe()) {
415 /* Add memory to the vmem allocator (checking there is space) */
416 if (vmm_base > (L2_SIZE + PAGE_SIZE)) {
417 /*
418 * Ensure there is an L2 block before the vmm code to check
419 * for buffer overflows on earlier data. Include the PAGE_SIZE
420 * of the minimum we can allocate.
421 */
422 vmm_base -= L2_SIZE + PAGE_SIZE;
423 vmm_base = rounddown2(vmm_base, L2_SIZE);
424
425 /*
426 * Check there is memory before the vmm code to add.
427 *
428 * Reserve the L2 block at address 0 so NULL dereference will
429 * raise an exception.
430 */
431 if (vmm_base > L2_SIZE)
432 vmem_add(el2_mem_alloc, L2_SIZE, vmm_base - L2_SIZE,
433 M_WAITOK);
434 }
435
436 /*
437 * Add the memory after the stacks. There is most of an L2 block
438 * between the last stack and the first allocation so this should
439 * be safe without adding more padding.
440 */
441 if (next_hyp_va < HYP_VM_MAX_ADDRESS - PAGE_SIZE)
442 vmem_add(el2_mem_alloc, next_hyp_va,
443 HYP_VM_MAX_ADDRESS - next_hyp_va, M_WAITOK);
444 }
445
446 vgic_init();
447 vtimer_init();
448
449 return (0);
450 }
451
452 int
vmmops_modcleanup(void)453 vmmops_modcleanup(void)
454 {
455 int cpu;
456
457 if (!in_vhe()) {
458 smp_rendezvous(NULL, arm_teardown_vectors, NULL, NULL);
459
460 CPU_FOREACH(cpu) {
461 vmmpmap_remove(stack_hyp_va[cpu],
462 VMM_STACK_PAGES * PAGE_SIZE, false);
463 }
464
465 vmmpmap_remove(hyp_code_base, hyp_code_len, false);
466 }
467
468 vtimer_cleanup();
469
470 if (!in_vhe()) {
471 vmmpmap_fini();
472
473 CPU_FOREACH(cpu)
474 free(stack[cpu], M_HYP);
475 }
476
477 pmap_clean_stage2_tlbi = NULL;
478 pmap_stage2_invalidate_range = NULL;
479 pmap_stage2_invalidate_all = NULL;
480
481 return (0);
482 }
483
484 static vm_size_t
el2_hyp_size(struct vm * vm)485 el2_hyp_size(struct vm *vm)
486 {
487 return (round_page(sizeof(struct hyp) +
488 sizeof(struct hypctx *) * vm_get_maxcpus(vm)));
489 }
490
491 static vm_size_t
el2_hypctx_size(void)492 el2_hypctx_size(void)
493 {
494 return (round_page(sizeof(struct hypctx)));
495 }
496
497 static vm_offset_t
el2_map_enter(vm_offset_t data,vm_size_t size,vm_prot_t prot)498 el2_map_enter(vm_offset_t data, vm_size_t size, vm_prot_t prot)
499 {
500 vmem_addr_t addr;
501 int err __diagused;
502 bool rv __diagused;
503
504 err = vmem_alloc(el2_mem_alloc, size, M_NEXTFIT | M_WAITOK, &addr);
505 MPASS(err == 0);
506 rv = vmmpmap_enter(addr, size, vtophys(data), prot);
507 MPASS(rv);
508
509 return (addr);
510 }
511
512 void *
vmmops_init(struct vm * vm,pmap_t pmap)513 vmmops_init(struct vm *vm, pmap_t pmap)
514 {
515 struct hyp *hyp;
516 vm_size_t size;
517 uint64_t idreg;
518
519 size = el2_hyp_size(vm);
520 hyp = malloc_aligned(size, PAGE_SIZE, M_HYP, M_WAITOK | M_ZERO);
521
522 hyp->vm = vm;
523 hyp->vgic_attached = false;
524
525 if (get_kernel_reg(ID_AA64MMFR0_EL1, &idreg)) {
526 if (ID_AA64MMFR0_ECV_VAL(idreg) >= ID_AA64MMFR0_ECV_POFF)
527 hyp->feats |= HYP_FEAT_ECV_POFF;
528
529 switch (ID_AA64MMFR0_FGT_VAL(idreg)) {
530 case ID_AA64MMFR0_FGT_NONE:
531 break;
532 default:
533 case ID_AA64MMFR0_FGT_8_9:
534 hyp->feats |= HYP_FEAT_FGT2;
535 /* FALLTHROUGH */
536 case ID_AA64MMFR0_FGT_8_6:
537 hyp->feats |= HYP_FEAT_FGT;
538 break;
539 }
540 }
541
542 if (get_kernel_reg(ID_AA64MMFR1_EL1, &idreg)) {
543 if (ID_AA64MMFR1_HCX_VAL(idreg) >= ID_AA64MMFR1_HCX_IMPL)
544 hyp->feats |= HYP_FEAT_HCX;
545 }
546
547 vtimer_vminit(hyp);
548 vgic_vminit(hyp);
549
550 if (!in_vhe())
551 hyp->el2_addr = el2_map_enter((vm_offset_t)hyp, size,
552 VM_PROT_READ | VM_PROT_WRITE);
553
554 return (hyp);
555 }
556
557 void *
vmmops_vcpu_init(void * vmi,struct vcpu * vcpu1,int vcpuid)558 vmmops_vcpu_init(void *vmi, struct vcpu *vcpu1, int vcpuid)
559 {
560 struct hyp *hyp = vmi;
561 struct hypctx *hypctx;
562 vm_size_t size;
563
564 size = el2_hypctx_size();
565 hypctx = malloc_aligned(size, PAGE_SIZE, M_HYP, M_WAITOK | M_ZERO);
566
567 KASSERT(vcpuid >= 0 && vcpuid < vm_get_maxcpus(hyp->vm),
568 ("%s: Invalid vcpuid %d", __func__, vcpuid));
569 hyp->ctx[vcpuid] = hypctx;
570
571 hypctx->hyp = hyp;
572 hypctx->vcpu = vcpu1;
573
574 reset_vm_el01_regs(hypctx);
575 reset_vm_el2_regs(hypctx);
576
577 vtimer_cpuinit(hypctx);
578 vgic_cpuinit(hypctx);
579
580 if (!in_vhe())
581 hypctx->el2_addr = el2_map_enter((vm_offset_t)hypctx, size,
582 VM_PROT_READ | VM_PROT_WRITE);
583
584 return (hypctx);
585 }
586
587 static int
arm_vmm_pinit(pmap_t pmap)588 arm_vmm_pinit(pmap_t pmap)
589 {
590
591 pmap_pinit_stage(pmap, PM_STAGE2, vmm_pmap_levels);
592 return (1);
593 }
594
595 struct vmspace *
vmmops_vmspace_alloc(vm_offset_t min,vm_offset_t max)596 vmmops_vmspace_alloc(vm_offset_t min, vm_offset_t max)
597 {
598 return (vmspace_alloc(min, max, arm_vmm_pinit));
599 }
600
601 void
vmmops_vmspace_free(struct vmspace * vmspace)602 vmmops_vmspace_free(struct vmspace *vmspace)
603 {
604
605 pmap_remove_pages(vmspace_pmap(vmspace));
606 vmspace_free(vmspace);
607 }
608
609 static inline void
arm64_print_hyp_regs(struct vm_exit * vme)610 arm64_print_hyp_regs(struct vm_exit *vme)
611 {
612 printf("esr_el2: 0x%016lx\n", vme->u.hyp.esr_el2);
613 printf("far_el2: 0x%016lx\n", vme->u.hyp.far_el2);
614 printf("hpfar_el2: 0x%016lx\n", vme->u.hyp.hpfar_el2);
615 printf("elr_el2: 0x%016lx\n", vme->pc);
616 }
617
618 static void
arm64_gen_inst_emul_data(struct hypctx * hypctx,uint32_t esr_iss,struct vm_exit * vme_ret)619 arm64_gen_inst_emul_data(struct hypctx *hypctx, uint32_t esr_iss,
620 struct vm_exit *vme_ret)
621 {
622 struct vm_guest_paging *paging;
623 struct vie *vie;
624 uint32_t esr_sas, reg_num;
625
626 /*
627 * Get the page address from HPFAR_EL2.
628 */
629 vme_ret->u.inst_emul.gpa =
630 HPFAR_EL2_FIPA_ADDR(hypctx->exit_info.hpfar_el2);
631 /* Bits [11:0] are the same as bits [11:0] from the virtual address. */
632 vme_ret->u.inst_emul.gpa += hypctx->exit_info.far_el2 &
633 FAR_EL2_HPFAR_PAGE_MASK;
634
635 esr_sas = (esr_iss & ISS_DATA_SAS_MASK) >> ISS_DATA_SAS_SHIFT;
636 reg_num = (esr_iss & ISS_DATA_SRT_MASK) >> ISS_DATA_SRT_SHIFT;
637
638 vie = &vme_ret->u.inst_emul.vie;
639 vie->access_size = 1 << esr_sas;
640 vie->sign_extend = (esr_iss & ISS_DATA_SSE) ? 1 : 0;
641 vie->dir = (esr_iss & ISS_DATA_WnR) ? VM_DIR_WRITE : VM_DIR_READ;
642 vie->reg = reg_num;
643
644 paging = &vme_ret->u.inst_emul.paging;
645 paging->ttbr0_addr = hypctx->ttbr0_el1 & ~(TTBR_ASID_MASK | TTBR_CnP);
646 paging->ttbr1_addr = hypctx->ttbr1_el1 & ~(TTBR_ASID_MASK | TTBR_CnP);
647 paging->tcr_el1 = hypctx->tcr_el1;
648 paging->tcr2_el1 = hypctx->tcr2_el1;
649 paging->flags = hypctx->tf.tf_spsr & (PSR_M_MASK | PSR_M_32);
650 if ((hypctx->sctlr_el1 & SCTLR_M) != 0)
651 paging->flags |= VM_GP_MMU_ENABLED;
652 }
653
654 static void
arm64_gen_reg_emul_data(uint32_t esr_iss,struct vm_exit * vme_ret)655 arm64_gen_reg_emul_data(uint32_t esr_iss, struct vm_exit *vme_ret)
656 {
657 uint32_t reg_num;
658 struct vre *vre;
659
660 /* u.hyp member will be replaced by u.reg_emul */
661 vre = &vme_ret->u.reg_emul.vre;
662
663 vre->inst_syndrome = esr_iss;
664 /* ARMv8 Architecture Manual, p. D7-2273: 1 means read */
665 vre->dir = (esr_iss & ISS_MSR_DIR) ? VM_DIR_READ : VM_DIR_WRITE;
666 reg_num = ISS_MSR_Rt(esr_iss);
667 vre->reg = reg_num;
668 }
669
670 void
raise_data_insn_abort(struct hypctx * hypctx,uint64_t far,bool dabort,int fsc)671 raise_data_insn_abort(struct hypctx *hypctx, uint64_t far, bool dabort, int fsc)
672 {
673 uint64_t esr;
674
675 if ((hypctx->tf.tf_spsr & PSR_M_MASK) == PSR_M_EL0t)
676 esr = EXCP_INSN_ABORT_L << ESR_ELx_EC_SHIFT;
677 else
678 esr = EXCP_INSN_ABORT << ESR_ELx_EC_SHIFT;
679 /* Set the bit that changes from insn -> data abort */
680 if (dabort)
681 esr |= EXCP_DATA_ABORT_L << ESR_ELx_EC_SHIFT;
682 /* Set the IL bit if set by hardware */
683 esr |= hypctx->tf.tf_esr & ESR_ELx_IL;
684
685 vmmops_exception(hypctx, esr | fsc, far);
686 }
687
688 static int
handle_el1_sync_excp(struct hypctx * hypctx,struct vm_exit * vme_ret,pmap_t pmap)689 handle_el1_sync_excp(struct hypctx *hypctx, struct vm_exit *vme_ret,
690 pmap_t pmap)
691 {
692 uint64_t gpa;
693 uint32_t esr_ec, esr_iss;
694
695 esr_ec = ESR_ELx_EXCEPTION(hypctx->tf.tf_esr);
696 esr_iss = hypctx->tf.tf_esr & ESR_ELx_ISS_MASK;
697
698 switch (esr_ec) {
699 case EXCP_UNKNOWN:
700 vmm_stat_incr(hypctx->vcpu, VMEXIT_UNKNOWN, 1);
701 arm64_print_hyp_regs(vme_ret);
702 vme_ret->exitcode = VM_EXITCODE_HYP;
703 break;
704 case EXCP_TRAP_WFI_WFE:
705 if ((hypctx->tf.tf_esr & 0x3) == 0) { /* WFI */
706 vmm_stat_incr(hypctx->vcpu, VMEXIT_WFI, 1);
707 vme_ret->exitcode = VM_EXITCODE_WFI;
708 } else {
709 vmm_stat_incr(hypctx->vcpu, VMEXIT_WFE, 1);
710 vme_ret->exitcode = VM_EXITCODE_HYP;
711 }
712 break;
713 case EXCP_HVC:
714 vmm_stat_incr(hypctx->vcpu, VMEXIT_HVC, 1);
715 vme_ret->exitcode = VM_EXITCODE_HVC;
716 break;
717 case EXCP_MSR:
718 vmm_stat_incr(hypctx->vcpu, VMEXIT_MSR, 1);
719 arm64_gen_reg_emul_data(esr_iss, vme_ret);
720 vme_ret->exitcode = VM_EXITCODE_REG_EMUL;
721 break;
722 case EXCP_BRK:
723 vmm_stat_incr(hypctx->vcpu, VMEXIT_BRK, 1);
724 vme_ret->exitcode = VM_EXITCODE_BRK;
725 break;
726 case EXCP_SOFTSTP_EL0:
727 vmm_stat_incr(hypctx->vcpu, VMEXIT_SS, 1);
728 vme_ret->exitcode = VM_EXITCODE_SS;
729 break;
730 case EXCP_INSN_ABORT_L:
731 case EXCP_DATA_ABORT_L:
732 vmm_stat_incr(hypctx->vcpu, esr_ec == EXCP_DATA_ABORT_L ?
733 VMEXIT_DATA_ABORT : VMEXIT_INSN_ABORT, 1);
734 switch (hypctx->tf.tf_esr & ISS_DATA_DFSC_MASK) {
735 case ISS_DATA_DFSC_TF_L0:
736 case ISS_DATA_DFSC_TF_L1:
737 case ISS_DATA_DFSC_TF_L2:
738 case ISS_DATA_DFSC_TF_L3:
739 case ISS_DATA_DFSC_AFF_L1:
740 case ISS_DATA_DFSC_AFF_L2:
741 case ISS_DATA_DFSC_AFF_L3:
742 case ISS_DATA_DFSC_PF_L1:
743 case ISS_DATA_DFSC_PF_L2:
744 case ISS_DATA_DFSC_PF_L3:
745 gpa = HPFAR_EL2_FIPA_ADDR(hypctx->exit_info.hpfar_el2);
746 /* Check the IPA is valid */
747 if (gpa >= (1ul << vmm_max_ipa_bits)) {
748 raise_data_insn_abort(hypctx,
749 hypctx->exit_info.far_el2,
750 esr_ec == EXCP_DATA_ABORT_L,
751 ISS_DATA_DFSC_ASF_L0);
752 vme_ret->inst_length = 0;
753 return (HANDLED);
754 }
755
756 if (vm_mem_allocated(hypctx->vcpu, gpa)) {
757 vme_ret->exitcode = VM_EXITCODE_PAGING;
758 vme_ret->inst_length = 0;
759 vme_ret->u.paging.esr = hypctx->tf.tf_esr;
760 vme_ret->u.paging.gpa = gpa;
761 } else if (esr_ec == EXCP_INSN_ABORT_L) {
762 /*
763 * Raise an external abort. Device memory is
764 * not executable
765 */
766 raise_data_insn_abort(hypctx,
767 hypctx->exit_info.far_el2, false,
768 ISS_DATA_DFSC_EXT);
769 vme_ret->inst_length = 0;
770 return (HANDLED);
771 } else {
772 arm64_gen_inst_emul_data(hypctx, esr_iss,
773 vme_ret);
774 vme_ret->exitcode = VM_EXITCODE_INST_EMUL;
775 }
776 break;
777 default:
778 arm64_print_hyp_regs(vme_ret);
779 vme_ret->exitcode = VM_EXITCODE_HYP;
780 break;
781 }
782
783 break;
784
785 default:
786 vmm_stat_incr(hypctx->vcpu, VMEXIT_UNHANDLED_SYNC, 1);
787 arm64_print_hyp_regs(vme_ret);
788 vme_ret->exitcode = VM_EXITCODE_HYP;
789 break;
790 }
791
792 /* We don't don't do any instruction emulation here */
793 return (UNHANDLED);
794 }
795
796 static int
arm64_handle_world_switch(struct hypctx * hypctx,int excp_type,struct vm_exit * vme,pmap_t pmap)797 arm64_handle_world_switch(struct hypctx *hypctx, int excp_type,
798 struct vm_exit *vme, pmap_t pmap)
799 {
800 int handled;
801
802 switch (excp_type) {
803 case EXCP_TYPE_EL1_SYNC:
804 /* The exit code will be set by handle_el1_sync_excp(). */
805 handled = handle_el1_sync_excp(hypctx, vme, pmap);
806 break;
807
808 case EXCP_TYPE_EL1_IRQ:
809 case EXCP_TYPE_EL1_FIQ:
810 /* The host kernel will handle IRQs and FIQs. */
811 vmm_stat_incr(hypctx->vcpu,
812 excp_type == EXCP_TYPE_EL1_IRQ ? VMEXIT_IRQ : VMEXIT_FIQ,1);
813 vme->exitcode = VM_EXITCODE_BOGUS;
814 handled = UNHANDLED;
815 break;
816
817 case EXCP_TYPE_EL1_ERROR:
818 case EXCP_TYPE_EL2_SYNC:
819 case EXCP_TYPE_EL2_IRQ:
820 case EXCP_TYPE_EL2_FIQ:
821 case EXCP_TYPE_EL2_ERROR:
822 vmm_stat_incr(hypctx->vcpu, VMEXIT_UNHANDLED_EL2, 1);
823 vme->exitcode = VM_EXITCODE_BOGUS;
824 handled = UNHANDLED;
825 break;
826
827 default:
828 vmm_stat_incr(hypctx->vcpu, VMEXIT_UNHANDLED, 1);
829 vme->exitcode = VM_EXITCODE_BOGUS;
830 handled = UNHANDLED;
831 break;
832 }
833
834 return (handled);
835 }
836
837 static void
ptp_release(void ** cookie)838 ptp_release(void **cookie)
839 {
840 if (*cookie != NULL) {
841 vm_gpa_release(*cookie);
842 *cookie = NULL;
843 }
844 }
845
846 static void *
ptp_hold(struct vcpu * vcpu,vm_paddr_t ptpphys,size_t len,void ** cookie)847 ptp_hold(struct vcpu *vcpu, vm_paddr_t ptpphys, size_t len, void **cookie)
848 {
849 void *ptr;
850
851 ptp_release(cookie);
852 ptr = vm_gpa_hold(vcpu, ptpphys, len, VM_PROT_RW, cookie);
853 return (ptr);
854 }
855
856 /* log2 of the number of bytes in a page table entry */
857 #define PTE_SHIFT 3
858 int
vmmops_gla2gpa(void * vcpui,struct vm_guest_paging * paging,uint64_t gla,int prot,uint64_t * gpa,int * is_fault)859 vmmops_gla2gpa(void *vcpui, struct vm_guest_paging *paging, uint64_t gla,
860 int prot, uint64_t *gpa, int *is_fault)
861 {
862 struct hypctx *hypctx;
863 void *cookie;
864 uint64_t mask, *ptep, pte, pte_addr;
865 int address_bits, granule_shift, ia_bits, levels, pte_shift, tsz;
866 bool is_el0;
867
868 /* Check if the MMU is off */
869 if ((paging->flags & VM_GP_MMU_ENABLED) == 0) {
870 *is_fault = 0;
871 *gpa = gla;
872 return (0);
873 }
874
875 is_el0 = (paging->flags & PSR_M_MASK) == PSR_M_EL0t;
876
877 if (ADDR_IS_KERNEL(gla)) {
878 /* If address translation is disabled raise an exception */
879 if ((paging->tcr_el1 & TCR_EPD1) != 0) {
880 *is_fault = 1;
881 return (0);
882 }
883 if (is_el0 && (paging->tcr_el1 & TCR_E0PD1) != 0) {
884 *is_fault = 1;
885 return (0);
886 }
887 pte_addr = paging->ttbr1_addr;
888 tsz = (paging->tcr_el1 & TCR_T1SZ_MASK) >> TCR_T1SZ_SHIFT;
889 /* Clear the top byte if TBI is on */
890 if ((paging->tcr_el1 & TCR_TBI1) != 0)
891 gla |= (0xfful << 56);
892 switch (paging->tcr_el1 & TCR_TG1_MASK) {
893 case TCR_TG1_4K:
894 granule_shift = PAGE_SHIFT_4K;
895 break;
896 case TCR_TG1_16K:
897 granule_shift = PAGE_SHIFT_16K;
898 break;
899 case TCR_TG1_64K:
900 granule_shift = PAGE_SHIFT_64K;
901 break;
902 default:
903 *is_fault = 1;
904 return (EINVAL);
905 }
906 } else {
907 /* If address translation is disabled raise an exception */
908 if ((paging->tcr_el1 & TCR_EPD0) != 0) {
909 *is_fault = 1;
910 return (0);
911 }
912 if (is_el0 && (paging->tcr_el1 & TCR_E0PD0) != 0) {
913 *is_fault = 1;
914 return (0);
915 }
916 pte_addr = paging->ttbr0_addr;
917 tsz = (paging->tcr_el1 & TCR_T0SZ_MASK) >> TCR_T0SZ_SHIFT;
918 /* Clear the top byte if TBI is on */
919 if ((paging->tcr_el1 & TCR_TBI0) != 0)
920 gla &= ~(0xfful << 56);
921 switch (paging->tcr_el1 & TCR_TG0_MASK) {
922 case TCR_TG0_4K:
923 granule_shift = PAGE_SHIFT_4K;
924 break;
925 case TCR_TG0_16K:
926 granule_shift = PAGE_SHIFT_16K;
927 break;
928 case TCR_TG0_64K:
929 granule_shift = PAGE_SHIFT_64K;
930 break;
931 default:
932 *is_fault = 1;
933 return (EINVAL);
934 }
935 }
936
937 /*
938 * TODO: Support FEAT_TTST for smaller tsz values and FEAT_LPA2
939 * for larger values.
940 */
941 switch (granule_shift) {
942 case PAGE_SHIFT_4K:
943 case PAGE_SHIFT_16K:
944 /*
945 * See "Table D8-11 4KB granule, determining stage 1 initial
946 * lookup level" and "Table D8-21 16KB granule, determining
947 * stage 1 initial lookup level" from the "Arm Architecture
948 * Reference Manual for A-Profile architecture" revision I.a
949 * for the minimum and maximum values.
950 *
951 * TODO: Support less than 16 when FEAT_LPA2 is implemented
952 * and TCR_EL1.DS == 1
953 * TODO: Support more than 39 when FEAT_TTST is implemented
954 */
955 if (tsz < 16 || tsz > 39) {
956 *is_fault = 1;
957 return (EINVAL);
958 }
959 break;
960 case PAGE_SHIFT_64K:
961 /* TODO: Support 64k granule. It will probably work, but is untested */
962 default:
963 *is_fault = 1;
964 return (EINVAL);
965 }
966
967 /*
968 * Calculate the input address bits. These are 64 bit in an address
969 * with the top tsz bits being all 0 or all 1.
970 */
971 ia_bits = 64 - tsz;
972
973 /*
974 * Calculate the number of address bits used in the page table
975 * calculation. This is ia_bits minus the bottom granule_shift
976 * bits that are passed to the output address.
977 */
978 address_bits = ia_bits - granule_shift;
979
980 /*
981 * Calculate the number of levels. Each level uses
982 * granule_shift - PTE_SHIFT bits of the input address.
983 * This is because the table is 1 << granule_shift and each
984 * entry is 1 << PTE_SHIFT bytes.
985 */
986 levels = howmany(address_bits, granule_shift - PTE_SHIFT);
987
988 /* Mask of the upper unused bits in the virtual address */
989 gla &= (1ul << ia_bits) - 1;
990 hypctx = (struct hypctx *)vcpui;
991 cookie = NULL;
992 /* TODO: Check if the level supports block descriptors */
993 for (;levels > 0; levels--) {
994 int idx;
995
996 pte_shift = (levels - 1) * (granule_shift - PTE_SHIFT) +
997 granule_shift;
998 idx = (gla >> pte_shift) &
999 ((1ul << (granule_shift - PTE_SHIFT)) - 1);
1000 while (idx > PAGE_SIZE / sizeof(pte)) {
1001 idx -= PAGE_SIZE / sizeof(pte);
1002 pte_addr += PAGE_SIZE;
1003 }
1004
1005 ptep = ptp_hold(hypctx->vcpu, pte_addr, PAGE_SIZE, &cookie);
1006 if (ptep == NULL)
1007 goto error;
1008 pte = ptep[idx];
1009
1010 /* Calculate the level we are looking at */
1011 switch (levels) {
1012 default:
1013 goto fault;
1014 /* TODO: Level -1 when FEAT_LPA2 is implemented */
1015 case 4: /* Level 0 */
1016 if ((pte & ATTR_DESCR_MASK) != L0_TABLE)
1017 goto fault;
1018 /* FALLTHROUGH */
1019 case 3: /* Level 1 */
1020 case 2: /* Level 2 */
1021 switch (pte & ATTR_DESCR_MASK) {
1022 /* Use L1 macro as all levels are the same */
1023 case L1_TABLE:
1024 /* Check if EL0 can access this address space */
1025 if (is_el0 &&
1026 (pte & TATTR_AP_TABLE_NO_EL0) != 0)
1027 goto fault;
1028 /* Check if the address space is writable */
1029 if ((prot & PROT_WRITE) != 0 &&
1030 (pte & TATTR_AP_TABLE_RO) != 0)
1031 goto fault;
1032 if ((prot & PROT_EXEC) != 0) {
1033 /* Check the table exec attribute */
1034 if ((is_el0 &&
1035 (pte & TATTR_UXN_TABLE) != 0) ||
1036 (!is_el0 &&
1037 (pte & TATTR_PXN_TABLE) != 0))
1038 goto fault;
1039 }
1040 pte_addr = pte & ~ATTR_MASK;
1041 break;
1042 case L1_BLOCK:
1043 goto done;
1044 default:
1045 goto fault;
1046 }
1047 break;
1048 case 1: /* Level 3 */
1049 if ((pte & ATTR_DESCR_MASK) == L3_PAGE)
1050 goto done;
1051 goto fault;
1052 }
1053 }
1054
1055 done:
1056 /* Check if EL0 has access to the block/page */
1057 if (is_el0 && (pte & ATTR_S1_AP(ATTR_S1_AP_USER)) == 0)
1058 goto fault;
1059 if ((prot & PROT_WRITE) != 0 && (pte & ATTR_S1_AP_RW_BIT) != 0)
1060 goto fault;
1061 if ((prot & PROT_EXEC) != 0) {
1062 if ((is_el0 && (pte & ATTR_S1_UXN) != 0) ||
1063 (!is_el0 && (pte & ATTR_S1_PXN) != 0))
1064 goto fault;
1065 }
1066 mask = (1ul << pte_shift) - 1;
1067 *gpa = (pte & ~ATTR_MASK) | (gla & mask);
1068 *is_fault = 0;
1069 ptp_release(&cookie);
1070 return (0);
1071
1072 error:
1073 ptp_release(&cookie);
1074 return (EFAULT);
1075 fault:
1076 *is_fault = 1;
1077 ptp_release(&cookie);
1078 return (0);
1079 }
1080
1081 int
vmmops_run(void * vcpui,register_t pc,pmap_t pmap,struct vm_eventinfo * evinfo)1082 vmmops_run(void *vcpui, register_t pc, pmap_t pmap, struct vm_eventinfo *evinfo)
1083 {
1084 uint64_t excp_type;
1085 int handled;
1086 register_t daif;
1087 struct hyp *hyp;
1088 struct hypctx *hypctx;
1089 struct vcpu *vcpu;
1090 struct vm_exit *vme;
1091 int mode;
1092
1093 hypctx = (struct hypctx *)vcpui;
1094 hyp = hypctx->hyp;
1095 vcpu = hypctx->vcpu;
1096 vme = vm_exitinfo(vcpu);
1097
1098 hypctx->tf.tf_elr = (uint64_t)pc;
1099
1100 for (;;) {
1101 if (hypctx->has_exception) {
1102 hypctx->has_exception = false;
1103 hypctx->elr_el1 = hypctx->tf.tf_elr;
1104
1105 mode = hypctx->tf.tf_spsr & (PSR_M_MASK | PSR_M_32);
1106
1107 if (mode == PSR_M_EL1t) {
1108 hypctx->tf.tf_elr = hypctx->vbar_el1 + 0x0;
1109 } else if (mode == PSR_M_EL1h) {
1110 hypctx->tf.tf_elr = hypctx->vbar_el1 + 0x200;
1111 } else if ((mode & PSR_M_32) == PSR_M_64) {
1112 /* 64-bit EL0 */
1113 hypctx->tf.tf_elr = hypctx->vbar_el1 + 0x400;
1114 } else {
1115 /* 32-bit EL0 */
1116 hypctx->tf.tf_elr = hypctx->vbar_el1 + 0x600;
1117 }
1118
1119 /* Set the new spsr */
1120 hypctx->spsr_el1 = hypctx->tf.tf_spsr;
1121
1122 /* Set the new cpsr */
1123 hypctx->tf.tf_spsr = hypctx->spsr_el1 & PSR_FLAGS;
1124 hypctx->tf.tf_spsr |= PSR_DAIF | PSR_M_EL1h;
1125
1126 /*
1127 * Update fields that may change on exeption entry
1128 * based on how sctlr_el1 is configured.
1129 */
1130 if ((hypctx->sctlr_el1 & SCTLR_SPAN) == 0)
1131 hypctx->tf.tf_spsr |= PSR_PAN;
1132 if ((hypctx->sctlr_el1 & SCTLR_DSSBS) == 0)
1133 hypctx->tf.tf_spsr &= ~PSR_SSBS;
1134 else
1135 hypctx->tf.tf_spsr |= PSR_SSBS;
1136 }
1137
1138 daif = intr_disable();
1139
1140 /* Check if the vcpu is suspended */
1141 if (vcpu_suspended(evinfo)) {
1142 intr_restore(daif);
1143 vm_exit_suspended(vcpu, pc);
1144 break;
1145 }
1146
1147 if (vcpu_debugged(vcpu)) {
1148 intr_restore(daif);
1149 vm_exit_debug(vcpu, pc);
1150 break;
1151 }
1152
1153 /* Activate the stage2 pmap so the vmid is valid */
1154 pmap_activate_vm(pmap);
1155 hyp->vttbr_el2 = pmap_to_ttbr0(pmap);
1156
1157 /*
1158 * TODO: What happens if a timer interrupt is asserted exactly
1159 * here, but for the previous VM?
1160 */
1161 arm64_set_active_vcpu(hypctx);
1162 vgic_flush_hwstate(hypctx);
1163
1164 /* Call into EL2 to switch to the guest */
1165 excp_type = vmm_enter_guest(hyp, hypctx);
1166
1167 vgic_sync_hwstate(hypctx);
1168 vtimer_sync_hwstate(hypctx);
1169
1170 /*
1171 * Deactivate the stage2 pmap.
1172 */
1173 PCPU_SET(curvmpmap, NULL);
1174 intr_restore(daif);
1175
1176 vmm_stat_incr(vcpu, VMEXIT_COUNT, 1);
1177 if (excp_type == EXCP_TYPE_MAINT_IRQ)
1178 continue;
1179
1180 vme->pc = hypctx->tf.tf_elr;
1181 vme->inst_length = INSN_SIZE;
1182 vme->u.hyp.exception_nr = excp_type;
1183 vme->u.hyp.esr_el2 = hypctx->tf.tf_esr;
1184 vme->u.hyp.far_el2 = hypctx->exit_info.far_el2;
1185 vme->u.hyp.hpfar_el2 = hypctx->exit_info.hpfar_el2;
1186
1187 handled = arm64_handle_world_switch(hypctx, excp_type, vme,
1188 pmap);
1189 if (handled == UNHANDLED)
1190 /* Exit loop to emulate instruction. */
1191 break;
1192 else
1193 /* Resume guest execution from the next instruction. */
1194 hypctx->tf.tf_elr += vme->inst_length;
1195 }
1196
1197 return (0);
1198 }
1199
1200 static void
arm_pcpu_vmcleanup(void * arg)1201 arm_pcpu_vmcleanup(void *arg)
1202 {
1203 struct hyp *hyp;
1204 int i, maxcpus;
1205
1206 hyp = arg;
1207 maxcpus = vm_get_maxcpus(hyp->vm);
1208 for (i = 0; i < maxcpus; i++) {
1209 if (arm64_get_active_vcpu() == hyp->ctx[i]) {
1210 arm64_set_active_vcpu(NULL);
1211 break;
1212 }
1213 }
1214 }
1215
1216 void
vmmops_vcpu_cleanup(void * vcpui)1217 vmmops_vcpu_cleanup(void *vcpui)
1218 {
1219 struct hypctx *hypctx = vcpui;
1220
1221 vtimer_cpucleanup(hypctx);
1222 vgic_cpucleanup(hypctx);
1223
1224 if (!in_vhe())
1225 vmmpmap_remove(hypctx->el2_addr, el2_hypctx_size(), true);
1226
1227 free(hypctx, M_HYP);
1228 }
1229
1230 void
vmmops_cleanup(void * vmi)1231 vmmops_cleanup(void *vmi)
1232 {
1233 struct hyp *hyp = vmi;
1234
1235 vtimer_vmcleanup(hyp);
1236 vgic_vmcleanup(hyp);
1237
1238 smp_rendezvous(NULL, arm_pcpu_vmcleanup, NULL, hyp);
1239
1240 if (!in_vhe())
1241 vmmpmap_remove(hyp->el2_addr, el2_hyp_size(hyp->vm), true);
1242
1243 free(hyp, M_HYP);
1244 }
1245
1246 /*
1247 * Return register value. Registers have different sizes and an explicit cast
1248 * must be made to ensure proper conversion.
1249 */
1250 static uint64_t *
hypctx_regptr(struct hypctx * hypctx,int reg)1251 hypctx_regptr(struct hypctx *hypctx, int reg)
1252 {
1253 switch (reg) {
1254 case VM_REG_GUEST_X0 ... VM_REG_GUEST_X29:
1255 return (&hypctx->tf.tf_x[reg]);
1256 case VM_REG_GUEST_LR:
1257 return (&hypctx->tf.tf_lr);
1258 case VM_REG_GUEST_SP:
1259 return (&hypctx->tf.tf_sp);
1260 case VM_REG_GUEST_CPSR:
1261 return (&hypctx->tf.tf_spsr);
1262 case VM_REG_GUEST_PC:
1263 return (&hypctx->tf.tf_elr);
1264 case VM_REG_GUEST_SCTLR_EL1:
1265 return (&hypctx->sctlr_el1);
1266 case VM_REG_GUEST_TTBR0_EL1:
1267 return (&hypctx->ttbr0_el1);
1268 case VM_REG_GUEST_TTBR1_EL1:
1269 return (&hypctx->ttbr1_el1);
1270 case VM_REG_GUEST_TCR_EL1:
1271 return (&hypctx->tcr_el1);
1272 case VM_REG_GUEST_TCR2_EL1:
1273 return (&hypctx->tcr2_el1);
1274 case VM_REG_GUEST_MPIDR_EL1:
1275 return (&hypctx->vmpidr_el2);
1276 default:
1277 break;
1278 }
1279 return (NULL);
1280 }
1281
1282 int
vmmops_getreg(void * vcpui,int reg,uint64_t * retval)1283 vmmops_getreg(void *vcpui, int reg, uint64_t *retval)
1284 {
1285 uint64_t *regp;
1286 int running, hostcpu;
1287 struct hypctx *hypctx = vcpui;
1288
1289 running = vcpu_is_running(hypctx->vcpu, &hostcpu);
1290 if (running && hostcpu != curcpu)
1291 panic("arm_getreg: %s%d is running", vm_name(hypctx->hyp->vm),
1292 vcpu_vcpuid(hypctx->vcpu));
1293
1294 regp = hypctx_regptr(hypctx, reg);
1295 if (regp == NULL)
1296 return (EINVAL);
1297
1298 *retval = *regp;
1299 return (0);
1300 }
1301
1302 int
vmmops_setreg(void * vcpui,int reg,uint64_t val)1303 vmmops_setreg(void *vcpui, int reg, uint64_t val)
1304 {
1305 uint64_t *regp;
1306 struct hypctx *hypctx = vcpui;
1307 int running, hostcpu;
1308
1309 running = vcpu_is_running(hypctx->vcpu, &hostcpu);
1310 if (running && hostcpu != curcpu)
1311 panic("arm_setreg: %s%d is running", vm_name(hypctx->hyp->vm),
1312 vcpu_vcpuid(hypctx->vcpu));
1313
1314 regp = hypctx_regptr(hypctx, reg);
1315 if (regp == NULL)
1316 return (EINVAL);
1317
1318 *regp = val;
1319 return (0);
1320 }
1321
1322 int
vmmops_exception(void * vcpui,uint64_t esr,uint64_t far)1323 vmmops_exception(void *vcpui, uint64_t esr, uint64_t far)
1324 {
1325 struct hypctx *hypctx = vcpui;
1326 int running, hostcpu;
1327
1328 running = vcpu_is_running(hypctx->vcpu, &hostcpu);
1329 if (running && hostcpu != curcpu)
1330 panic("%s: %s%d is running", __func__, vm_name(hypctx->hyp->vm),
1331 vcpu_vcpuid(hypctx->vcpu));
1332
1333 hypctx->far_el1 = far;
1334 hypctx->esr_el1 = esr;
1335 hypctx->has_exception = true;
1336
1337 return (0);
1338 }
1339
1340 int
vmmops_getcap(void * vcpui,int num,int * retval)1341 vmmops_getcap(void *vcpui, int num, int *retval)
1342 {
1343 struct hypctx *hypctx = vcpui;
1344 int ret;
1345
1346 ret = ENOENT;
1347
1348 switch (num) {
1349 case VM_CAP_UNRESTRICTED_GUEST:
1350 *retval = 1;
1351 ret = 0;
1352 break;
1353 case VM_CAP_BRK_EXIT:
1354 case VM_CAP_SS_EXIT:
1355 case VM_CAP_MASK_HWINTR:
1356 *retval = (hypctx->setcaps & (1ul << num)) != 0;
1357 break;
1358 default:
1359 break;
1360 }
1361
1362 return (ret);
1363 }
1364
1365 int
vmmops_setcap(void * vcpui,int num,int val)1366 vmmops_setcap(void *vcpui, int num, int val)
1367 {
1368 struct hypctx *hypctx = vcpui;
1369 int ret;
1370
1371 ret = 0;
1372
1373 switch (num) {
1374 case VM_CAP_BRK_EXIT:
1375 if ((val != 0) == ((hypctx->setcaps & (1ul << num)) != 0))
1376 break;
1377 if (val != 0)
1378 hypctx->mdcr_el2 |= MDCR_EL2_TDE;
1379 else if ((hypctx->setcaps & (1ul << VM_CAP_SS_EXIT)) == 0)
1380 hypctx->mdcr_el2 &= ~MDCR_EL2_TDE;
1381 break;
1382 case VM_CAP_SS_EXIT:
1383 if ((val != 0) == ((hypctx->setcaps & (1ul << num)) != 0))
1384 break;
1385
1386 if (val != 0) {
1387 hypctx->debug_spsr |= (hypctx->tf.tf_spsr & PSR_SS);
1388 hypctx->debug_mdscr |= (hypctx->mdscr_el1 & MDSCR_SS);
1389
1390 hypctx->tf.tf_spsr |= PSR_SS;
1391 hypctx->mdscr_el1 |= MDSCR_SS;
1392 hypctx->mdcr_el2 |= MDCR_EL2_TDE;
1393 } else {
1394 hypctx->tf.tf_spsr &= ~PSR_SS;
1395 hypctx->tf.tf_spsr |= hypctx->debug_spsr;
1396 hypctx->debug_spsr &= ~PSR_SS;
1397 hypctx->mdscr_el1 &= ~MDSCR_SS;
1398 hypctx->mdscr_el1 |= hypctx->debug_mdscr;
1399 hypctx->debug_mdscr &= ~MDSCR_SS;
1400 if ((hypctx->setcaps & (1ul << VM_CAP_BRK_EXIT)) == 0)
1401 hypctx->mdcr_el2 &= ~MDCR_EL2_TDE;
1402 }
1403 break;
1404 case VM_CAP_MASK_HWINTR:
1405 if ((val != 0) == ((hypctx->setcaps & (1ul << num)) != 0))
1406 break;
1407
1408 if (val != 0) {
1409 hypctx->debug_spsr |= (hypctx->tf.tf_spsr &
1410 (PSR_I | PSR_F));
1411 hypctx->tf.tf_spsr |= PSR_I | PSR_F;
1412 } else {
1413 hypctx->tf.tf_spsr &= ~(PSR_I | PSR_F);
1414 hypctx->tf.tf_spsr |= (hypctx->debug_spsr &
1415 (PSR_I | PSR_F));
1416 hypctx->debug_spsr &= ~(PSR_I | PSR_F);
1417 }
1418 break;
1419 default:
1420 ret = ENOENT;
1421 break;
1422 }
1423
1424 if (ret == 0) {
1425 if (val == 0)
1426 hypctx->setcaps &= ~(1ul << num);
1427 else
1428 hypctx->setcaps |= (1ul << num);
1429 }
1430
1431 return (ret);
1432 }
1433