xref: /linux/tools/testing/selftests/kvm/lib/aarch64/processor.c (revision 64dd3b6a79f0907d36de481b0f15fab323a53e5a)
17a6629efSAndrew Jones // SPDX-License-Identifier: GPL-2.0
27a6629efSAndrew Jones /*
37a6629efSAndrew Jones  * AArch64 code
47a6629efSAndrew Jones  *
57a6629efSAndrew Jones  * Copyright (C) 2018, Red Hat, Inc.
67a6629efSAndrew Jones  */
77a6629efSAndrew Jones 
898e68344SAndrew Jones #include <linux/compiler.h>
9e3db7579SRicardo Koller #include <assert.h>
1098e68344SAndrew Jones 
110303ffdbSMarc Zyngier #include "guest_modes.h"
127a6629efSAndrew Jones #include "kvm_util.h"
130bec140fSAndrew Jones #include "processor.h"
142b7deea3SSean Christopherson #include "ucall_common.h"
152b7deea3SSean Christopherson 
161a618203SReiji Watanabe #include <linux/bitfield.h>
1710a0cc3bSRyan Roberts #include <linux/sizes.h>
187a6629efSAndrew Jones 
190bec140fSAndrew Jones #define DEFAULT_ARM64_GUEST_STACK_VADDR_MIN	0xac0000
207a6629efSAndrew Jones 
21e3db7579SRicardo Koller static vm_vaddr_t exception_handlers;
22e3db7579SRicardo Koller 
page_align(struct kvm_vm * vm,uint64_t v)237a6629efSAndrew Jones static uint64_t page_align(struct kvm_vm *vm, uint64_t v)
247a6629efSAndrew Jones {
257a6629efSAndrew Jones 	return (v + vm->page_size) & ~(vm->page_size - 1);
267a6629efSAndrew Jones }
277a6629efSAndrew Jones 
pgd_index(struct kvm_vm * vm,vm_vaddr_t gva)287a6629efSAndrew Jones static uint64_t pgd_index(struct kvm_vm *vm, vm_vaddr_t gva)
297a6629efSAndrew Jones {
307a6629efSAndrew Jones 	unsigned int shift = (vm->pgtable_levels - 1) * (vm->page_shift - 3) + vm->page_shift;
317a6629efSAndrew Jones 	uint64_t mask = (1UL << (vm->va_bits - shift)) - 1;
327a6629efSAndrew Jones 
337a6629efSAndrew Jones 	return (gva >> shift) & mask;
347a6629efSAndrew Jones }
357a6629efSAndrew Jones 
pud_index(struct kvm_vm * vm,vm_vaddr_t gva)367a6629efSAndrew Jones static uint64_t pud_index(struct kvm_vm *vm, vm_vaddr_t gva)
377a6629efSAndrew Jones {
387a6629efSAndrew Jones 	unsigned int shift = 2 * (vm->page_shift - 3) + vm->page_shift;
397a6629efSAndrew Jones 	uint64_t mask = (1UL << (vm->page_shift - 3)) - 1;
407a6629efSAndrew Jones 
417a6629efSAndrew Jones 	TEST_ASSERT(vm->pgtable_levels == 4,
427a6629efSAndrew Jones 		"Mode %d does not have 4 page table levels", vm->mode);
437a6629efSAndrew Jones 
447a6629efSAndrew Jones 	return (gva >> shift) & mask;
457a6629efSAndrew Jones }
467a6629efSAndrew Jones 
pmd_index(struct kvm_vm * vm,vm_vaddr_t gva)477a6629efSAndrew Jones static uint64_t pmd_index(struct kvm_vm *vm, vm_vaddr_t gva)
487a6629efSAndrew Jones {
497a6629efSAndrew Jones 	unsigned int shift = (vm->page_shift - 3) + vm->page_shift;
507a6629efSAndrew Jones 	uint64_t mask = (1UL << (vm->page_shift - 3)) - 1;
517a6629efSAndrew Jones 
527a6629efSAndrew Jones 	TEST_ASSERT(vm->pgtable_levels >= 3,
537a6629efSAndrew Jones 		"Mode %d does not have >= 3 page table levels", vm->mode);
547a6629efSAndrew Jones 
557a6629efSAndrew Jones 	return (gva >> shift) & mask;
567a6629efSAndrew Jones }
577a6629efSAndrew Jones 
pte_index(struct kvm_vm * vm,vm_vaddr_t gva)587a6629efSAndrew Jones static uint64_t pte_index(struct kvm_vm *vm, vm_vaddr_t gva)
597a6629efSAndrew Jones {
607a6629efSAndrew Jones 	uint64_t mask = (1UL << (vm->page_shift - 3)) - 1;
617a6629efSAndrew Jones 	return (gva >> vm->page_shift) & mask;
627a6629efSAndrew Jones }
637a6629efSAndrew Jones 
use_lpa2_pte_format(struct kvm_vm * vm)6410a0cc3bSRyan Roberts static inline bool use_lpa2_pte_format(struct kvm_vm *vm)
6510a0cc3bSRyan Roberts {
6610a0cc3bSRyan Roberts 	return (vm->page_size == SZ_4K || vm->page_size == SZ_16K) &&
6710a0cc3bSRyan Roberts 	    (vm->pa_bits > 48 || vm->va_bits > 48);
6810a0cc3bSRyan Roberts }
6910a0cc3bSRyan Roberts 
addr_pte(struct kvm_vm * vm,uint64_t pa,uint64_t attrs)70e659babfSRyan Roberts static uint64_t addr_pte(struct kvm_vm *vm, uint64_t pa, uint64_t attrs)
717a6629efSAndrew Jones {
72e659babfSRyan Roberts 	uint64_t pte;
73e659babfSRyan Roberts 
7410a0cc3bSRyan Roberts 	if (use_lpa2_pte_format(vm)) {
7510a0cc3bSRyan Roberts 		pte = pa & GENMASK(49, vm->page_shift);
7610a0cc3bSRyan Roberts 		pte |= FIELD_GET(GENMASK(51, 50), pa) << 8;
7710a0cc3bSRyan Roberts 		attrs &= ~GENMASK(9, 8);
7810a0cc3bSRyan Roberts 	} else {
79e659babfSRyan Roberts 		pte = pa & GENMASK(47, vm->page_shift);
80e659babfSRyan Roberts 		if (vm->page_shift == 16)
81e659babfSRyan Roberts 			pte |= FIELD_GET(GENMASK(51, 48), pa) << 12;
8210a0cc3bSRyan Roberts 	}
83e659babfSRyan Roberts 	pte |= attrs;
84e659babfSRyan Roberts 
85e659babfSRyan Roberts 	return pte;
86e659babfSRyan Roberts }
87e659babfSRyan Roberts 
pte_addr(struct kvm_vm * vm,uint64_t pte)88e659babfSRyan Roberts static uint64_t pte_addr(struct kvm_vm *vm, uint64_t pte)
89e659babfSRyan Roberts {
90e659babfSRyan Roberts 	uint64_t pa;
91e659babfSRyan Roberts 
9210a0cc3bSRyan Roberts 	if (use_lpa2_pte_format(vm)) {
9310a0cc3bSRyan Roberts 		pa = pte & GENMASK(49, vm->page_shift);
9410a0cc3bSRyan Roberts 		pa |= FIELD_GET(GENMASK(9, 8), pte) << 50;
9510a0cc3bSRyan Roberts 	} else {
96e659babfSRyan Roberts 		pa = pte & GENMASK(47, vm->page_shift);
97e659babfSRyan Roberts 		if (vm->page_shift == 16)
98e659babfSRyan Roberts 			pa |= FIELD_GET(GENMASK(15, 12), pte) << 48;
9910a0cc3bSRyan Roberts 	}
100e659babfSRyan Roberts 
101e659babfSRyan Roberts 	return pa;
1027a6629efSAndrew Jones }
1037a6629efSAndrew Jones 
ptrs_per_pgd(struct kvm_vm * vm)1047a6629efSAndrew Jones static uint64_t ptrs_per_pgd(struct kvm_vm *vm)
1057a6629efSAndrew Jones {
1067a6629efSAndrew Jones 	unsigned int shift = (vm->pgtable_levels - 1) * (vm->page_shift - 3) + vm->page_shift;
1077a6629efSAndrew Jones 	return 1 << (vm->va_bits - shift);
1087a6629efSAndrew Jones }
1097a6629efSAndrew Jones 
ptrs_per_pte(struct kvm_vm * vm)11098e68344SAndrew Jones static uint64_t __maybe_unused ptrs_per_pte(struct kvm_vm *vm)
1117a6629efSAndrew Jones {
1127a6629efSAndrew Jones 	return 1 << (vm->page_shift - 3);
1137a6629efSAndrew Jones }
1147a6629efSAndrew Jones 
virt_arch_pgd_alloc(struct kvm_vm * vm)1159931be3fSSean Christopherson void virt_arch_pgd_alloc(struct kvm_vm *vm)
1167a6629efSAndrew Jones {
1175485e822SRicardo Koller 	size_t nr_pages = page_align(vm, ptrs_per_pgd(vm) * 8) / vm->page_size;
1185485e822SRicardo Koller 
1195485e822SRicardo Koller 	if (vm->pgd_created)
1205485e822SRicardo Koller 		return;
1215485e822SRicardo Koller 
1225485e822SRicardo Koller 	vm->pgd = vm_phy_pages_alloc(vm, nr_pages,
1231446e331SRicardo Koller 				     KVM_GUEST_PAGE_TABLE_MIN_PADDR,
1241446e331SRicardo Koller 				     vm->memslots[MEM_REGION_PT]);
1257a6629efSAndrew Jones 	vm->pgd_created = true;
1267a6629efSAndrew Jones }
1277a6629efSAndrew Jones 
_virt_pg_map(struct kvm_vm * vm,uint64_t vaddr,uint64_t paddr,uint64_t flags)1284307af73SSean Christopherson static void _virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
1294307af73SSean Christopherson 			 uint64_t flags)
1307a6629efSAndrew Jones {
1317a6629efSAndrew Jones 	uint8_t attr_idx = flags & 7;
1327a6629efSAndrew Jones 	uint64_t *ptep;
1337a6629efSAndrew Jones 
1347a6629efSAndrew Jones 	TEST_ASSERT((vaddr % vm->page_size) == 0,
1357a6629efSAndrew Jones 		"Virtual address not on page boundary,\n"
1367a6629efSAndrew Jones 		"  vaddr: 0x%lx vm->page_size: 0x%x", vaddr, vm->page_size);
1377a6629efSAndrew Jones 	TEST_ASSERT(sparsebit_is_set(vm->vpages_valid,
1387a6629efSAndrew Jones 		(vaddr >> vm->page_shift)),
1397a6629efSAndrew Jones 		"Invalid virtual address, vaddr: 0x%lx", vaddr);
1407a6629efSAndrew Jones 	TEST_ASSERT((paddr % vm->page_size) == 0,
1417a6629efSAndrew Jones 		"Physical address not on page boundary,\n"
1427a6629efSAndrew Jones 		"  paddr: 0x%lx vm->page_size: 0x%x", paddr, vm->page_size);
1437a6629efSAndrew Jones 	TEST_ASSERT((paddr >> vm->page_shift) <= vm->max_gfn,
1447a6629efSAndrew Jones 		"Physical address beyond beyond maximum supported,\n"
1457a6629efSAndrew Jones 		"  paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x",
1467a6629efSAndrew Jones 		paddr, vm->max_gfn, vm->page_size);
1477a6629efSAndrew Jones 
1487a6629efSAndrew Jones 	ptep = addr_gpa2hva(vm, vm->pgd) + pgd_index(vm, vaddr) * 8;
149cce0c23dSSean Christopherson 	if (!*ptep)
150e659babfSRyan Roberts 		*ptep = addr_pte(vm, vm_alloc_page_table(vm), 3);
1517a6629efSAndrew Jones 
1527a6629efSAndrew Jones 	switch (vm->pgtable_levels) {
1537a6629efSAndrew Jones 	case 4:
1547a6629efSAndrew Jones 		ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pud_index(vm, vaddr) * 8;
155cce0c23dSSean Christopherson 		if (!*ptep)
156e659babfSRyan Roberts 			*ptep = addr_pte(vm, vm_alloc_page_table(vm), 3);
1577a6629efSAndrew Jones 		/* fall through */
1587a6629efSAndrew Jones 	case 3:
1597a6629efSAndrew Jones 		ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pmd_index(vm, vaddr) * 8;
160cce0c23dSSean Christopherson 		if (!*ptep)
161e659babfSRyan Roberts 			*ptep = addr_pte(vm, vm_alloc_page_table(vm), 3);
1627a6629efSAndrew Jones 		/* fall through */
1637a6629efSAndrew Jones 	case 2:
1647a6629efSAndrew Jones 		ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pte_index(vm, vaddr) * 8;
1657a6629efSAndrew Jones 		break;
1667a6629efSAndrew Jones 	default:
167352be2c5SWainer dos Santos Moschetta 		TEST_FAIL("Page table levels must be 2, 3, or 4");
1687a6629efSAndrew Jones 	}
1697a6629efSAndrew Jones 
170e659babfSRyan Roberts 	*ptep = addr_pte(vm, paddr, (attr_idx << 2) | (1 << 10) | 3);  /* AF */
1717a6629efSAndrew Jones }
1727a6629efSAndrew Jones 
virt_arch_pg_map(struct kvm_vm * vm,uint64_t vaddr,uint64_t paddr)1739931be3fSSean Christopherson void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
1747a6629efSAndrew Jones {
17541f5189eSRicardo Koller 	uint64_t attr_idx = MT_NORMAL;
1767a6629efSAndrew Jones 
1774307af73SSean Christopherson 	_virt_pg_map(vm, vaddr, paddr, attr_idx);
1787a6629efSAndrew Jones }
1797a6629efSAndrew Jones 
virt_get_pte_hva(struct kvm_vm * vm,vm_vaddr_t gva)180228f324dSRicardo Koller uint64_t *virt_get_pte_hva(struct kvm_vm *vm, vm_vaddr_t gva)
1817a6629efSAndrew Jones {
1827a6629efSAndrew Jones 	uint64_t *ptep;
1837a6629efSAndrew Jones 
1847a6629efSAndrew Jones 	if (!vm->pgd_created)
1857a6629efSAndrew Jones 		goto unmapped_gva;
1867a6629efSAndrew Jones 
1877a6629efSAndrew Jones 	ptep = addr_gpa2hva(vm, vm->pgd) + pgd_index(vm, gva) * 8;
1887a6629efSAndrew Jones 	if (!ptep)
1897a6629efSAndrew Jones 		goto unmapped_gva;
1907a6629efSAndrew Jones 
1917a6629efSAndrew Jones 	switch (vm->pgtable_levels) {
1927a6629efSAndrew Jones 	case 4:
1937a6629efSAndrew Jones 		ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pud_index(vm, gva) * 8;
1947a6629efSAndrew Jones 		if (!ptep)
1957a6629efSAndrew Jones 			goto unmapped_gva;
1967a6629efSAndrew Jones 		/* fall through */
1977a6629efSAndrew Jones 	case 3:
1987a6629efSAndrew Jones 		ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pmd_index(vm, gva) * 8;
1997a6629efSAndrew Jones 		if (!ptep)
2007a6629efSAndrew Jones 			goto unmapped_gva;
2017a6629efSAndrew Jones 		/* fall through */
2027a6629efSAndrew Jones 	case 2:
2037a6629efSAndrew Jones 		ptep = addr_gpa2hva(vm, pte_addr(vm, *ptep)) + pte_index(vm, gva) * 8;
2047a6629efSAndrew Jones 		if (!ptep)
2057a6629efSAndrew Jones 			goto unmapped_gva;
2067a6629efSAndrew Jones 		break;
2077a6629efSAndrew Jones 	default:
208352be2c5SWainer dos Santos Moschetta 		TEST_FAIL("Page table levels must be 2, 3, or 4");
2097a6629efSAndrew Jones 	}
2107a6629efSAndrew Jones 
211228f324dSRicardo Koller 	return ptep;
2127a6629efSAndrew Jones 
2137a6629efSAndrew Jones unmapped_gva:
214352be2c5SWainer dos Santos Moschetta 	TEST_FAIL("No mapping for vm virtual address, gva: 0x%lx", gva);
215228f324dSRicardo Koller 	exit(EXIT_FAILURE);
216228f324dSRicardo Koller }
217228f324dSRicardo Koller 
addr_arch_gva2gpa(struct kvm_vm * vm,vm_vaddr_t gva)218228f324dSRicardo Koller vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)
219228f324dSRicardo Koller {
220228f324dSRicardo Koller 	uint64_t *ptep = virt_get_pte_hva(vm, gva);
221228f324dSRicardo Koller 
222228f324dSRicardo Koller 	return pte_addr(vm, *ptep) + (gva & (vm->page_size - 1));
2237a6629efSAndrew Jones }
2247a6629efSAndrew Jones 
pte_dump(FILE * stream,struct kvm_vm * vm,uint8_t indent,uint64_t page,int level)2257a6629efSAndrew Jones static void pte_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent, uint64_t page, int level)
2267a6629efSAndrew Jones {
2273439d886SAndrew Jones #ifdef DEBUG
2287a6629efSAndrew Jones 	static const char * const type[] = { "", "pud", "pmd", "pte" };
2297a6629efSAndrew Jones 	uint64_t pte, *ptep;
2307a6629efSAndrew Jones 
2317a6629efSAndrew Jones 	if (level == 4)
2327a6629efSAndrew Jones 		return;
2337a6629efSAndrew Jones 
2347a6629efSAndrew Jones 	for (pte = page; pte < page + ptrs_per_pte(vm) * 8; pte += 8) {
2357a6629efSAndrew Jones 		ptep = addr_gpa2hva(vm, pte);
2367a6629efSAndrew Jones 		if (!*ptep)
2377a6629efSAndrew Jones 			continue;
238f09ab268SAndrew Jones 		fprintf(stream, "%*s%s: %lx: %lx at %p\n", indent, "", type[level], pte, *ptep, ptep);
2397a6629efSAndrew Jones 		pte_dump(stream, vm, indent + 1, pte_addr(vm, *ptep), level + 1);
2407a6629efSAndrew Jones 	}
2417a6629efSAndrew Jones #endif
2427a6629efSAndrew Jones }
2437a6629efSAndrew Jones 
virt_arch_dump(FILE * stream,struct kvm_vm * vm,uint8_t indent)2449931be3fSSean Christopherson void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
2457a6629efSAndrew Jones {
2467a6629efSAndrew Jones 	int level = 4 - (vm->pgtable_levels - 1);
2477a6629efSAndrew Jones 	uint64_t pgd, *ptep;
2487a6629efSAndrew Jones 
2497a6629efSAndrew Jones 	if (!vm->pgd_created)
2507a6629efSAndrew Jones 		return;
2517a6629efSAndrew Jones 
2527a6629efSAndrew Jones 	for (pgd = vm->pgd; pgd < vm->pgd + ptrs_per_pgd(vm) * 8; pgd += 8) {
2537a6629efSAndrew Jones 		ptep = addr_gpa2hva(vm, pgd);
2547a6629efSAndrew Jones 		if (!*ptep)
2557a6629efSAndrew Jones 			continue;
256f09ab268SAndrew Jones 		fprintf(stream, "%*spgd: %lx: %lx at %p\n", indent, "", pgd, *ptep, ptep);
2577a6629efSAndrew Jones 		pte_dump(stream, vm, indent + 1, pte_addr(vm, *ptep), level);
2587a6629efSAndrew Jones 	}
2597a6629efSAndrew Jones }
2600bec140fSAndrew Jones 
aarch64_vcpu_setup(struct kvm_vcpu * vcpu,struct kvm_vcpu_init * init)261768e9a61SSean Christopherson void aarch64_vcpu_setup(struct kvm_vcpu *vcpu, struct kvm_vcpu_init *init)
2620bec140fSAndrew Jones {
263f5dd4ccfSAndrew Jones 	struct kvm_vcpu_init default_init = { .target = -1, };
264768e9a61SSean Christopherson 	struct kvm_vm *vm = vcpu->vm;
265e1707175SRyan Roberts 	uint64_t sctlr_el1, tcr_el1, ttbr0_el1;
2660bec140fSAndrew Jones 
267f5dd4ccfSAndrew Jones 	if (!init)
268f5dd4ccfSAndrew Jones 		init = &default_init;
269f5dd4ccfSAndrew Jones 
270f5dd4ccfSAndrew Jones 	if (init->target == -1) {
271f5dd4ccfSAndrew Jones 		struct kvm_vcpu_init preferred;
272f5dd4ccfSAndrew Jones 		vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &preferred);
273f5dd4ccfSAndrew Jones 		init->target = preferred.target;
274f5dd4ccfSAndrew Jones 	}
275f5dd4ccfSAndrew Jones 
276768e9a61SSean Christopherson 	vcpu_ioctl(vcpu, KVM_ARM_VCPU_INIT, init);
2770bec140fSAndrew Jones 
2780bec140fSAndrew Jones 	/*
2790bec140fSAndrew Jones 	 * Enable FP/ASIMD to avoid trapping when accessing Q0-Q15
2800bec140fSAndrew Jones 	 * registers, which the variable argument list macros do.
2810bec140fSAndrew Jones 	 */
282768e9a61SSean Christopherson 	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_CPACR_EL1), 3 << 20);
2830bec140fSAndrew Jones 
284768e9a61SSean Christopherson 	vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_SCTLR_EL1), &sctlr_el1);
285768e9a61SSean Christopherson 	vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TCR_EL1), &tcr_el1);
2860bec140fSAndrew Jones 
2872f41a61cSMarc Zyngier 	/* Configure base granule size */
2880bec140fSAndrew Jones 	switch (vm->mode) {
289567a9f1eSPeter Xu 	case VM_MODE_PXXV48_4K:
290352be2c5SWainer dos Santos Moschetta 		TEST_FAIL("AArch64 does not support 4K sized pages "
291567a9f1eSPeter Xu 			  "with ANY-bit physical address ranges");
29281d1cca0SAndrew Jones 	case VM_MODE_P52V48_64K:
2932f41a61cSMarc Zyngier 	case VM_MODE_P48V48_64K:
2942f41a61cSMarc Zyngier 	case VM_MODE_P40V48_64K:
295e7f58a6bSMarc Zyngier 	case VM_MODE_P36V48_64K:
29681d1cca0SAndrew Jones 		tcr_el1 |= 1ul << 14; /* TG0 = 64KB */
2972f41a61cSMarc Zyngier 		break;
29810a0cc3bSRyan Roberts 	case VM_MODE_P52V48_16K:
299aa674de1SMarc Zyngier 	case VM_MODE_P48V48_16K:
300aa674de1SMarc Zyngier 	case VM_MODE_P40V48_16K:
301aa674de1SMarc Zyngier 	case VM_MODE_P36V48_16K:
302aa674de1SMarc Zyngier 	case VM_MODE_P36V47_16K:
303aa674de1SMarc Zyngier 		tcr_el1 |= 2ul << 14; /* TG0 = 16KB */
304aa674de1SMarc Zyngier 		break;
30510a0cc3bSRyan Roberts 	case VM_MODE_P52V48_4K:
3062f41a61cSMarc Zyngier 	case VM_MODE_P48V48_4K:
3072f41a61cSMarc Zyngier 	case VM_MODE_P40V48_4K:
308e7f58a6bSMarc Zyngier 	case VM_MODE_P36V48_4K:
3092f41a61cSMarc Zyngier 		tcr_el1 |= 0ul << 14; /* TG0 = 4KB */
3102f41a61cSMarc Zyngier 		break;
3112f41a61cSMarc Zyngier 	default:
3122f41a61cSMarc Zyngier 		TEST_FAIL("Unknown guest mode, mode: 0x%x", vm->mode);
3132f41a61cSMarc Zyngier 	}
3142f41a61cSMarc Zyngier 
315e1707175SRyan Roberts 	ttbr0_el1 = vm->pgd & GENMASK(47, vm->page_shift);
316e1707175SRyan Roberts 
3172f41a61cSMarc Zyngier 	/* Configure output size */
3182f41a61cSMarc Zyngier 	switch (vm->mode) {
31910a0cc3bSRyan Roberts 	case VM_MODE_P52V48_4K:
32010a0cc3bSRyan Roberts 	case VM_MODE_P52V48_16K:
3212f41a61cSMarc Zyngier 	case VM_MODE_P52V48_64K:
32281d1cca0SAndrew Jones 		tcr_el1 |= 6ul << 32; /* IPS = 52 bits */
323e1707175SRyan Roberts 		ttbr0_el1 |= FIELD_GET(GENMASK(51, 48), vm->pgd) << 2;
32481d1cca0SAndrew Jones 		break;
325cdbd2428SAndrew Jones 	case VM_MODE_P48V48_4K:
326aa674de1SMarc Zyngier 	case VM_MODE_P48V48_16K:
327cdbd2428SAndrew Jones 	case VM_MODE_P48V48_64K:
328cdbd2428SAndrew Jones 		tcr_el1 |= 5ul << 32; /* IPS = 48 bits */
329cdbd2428SAndrew Jones 		break;
330e28934e6SAndrew Jones 	case VM_MODE_P40V48_4K:
331aa674de1SMarc Zyngier 	case VM_MODE_P40V48_16K:
332e28934e6SAndrew Jones 	case VM_MODE_P40V48_64K:
333e28934e6SAndrew Jones 		tcr_el1 |= 2ul << 32; /* IPS = 40 bits */
334e28934e6SAndrew Jones 		break;
335e7f58a6bSMarc Zyngier 	case VM_MODE_P36V48_4K:
336aa674de1SMarc Zyngier 	case VM_MODE_P36V48_16K:
337e7f58a6bSMarc Zyngier 	case VM_MODE_P36V48_64K:
338aa674de1SMarc Zyngier 	case VM_MODE_P36V47_16K:
339e7f58a6bSMarc Zyngier 		tcr_el1 |= 1ul << 32; /* IPS = 36 bits */
340e7f58a6bSMarc Zyngier 		break;
3410bec140fSAndrew Jones 	default:
342352be2c5SWainer dos Santos Moschetta 		TEST_FAIL("Unknown guest mode, mode: 0x%x", vm->mode);
3430bec140fSAndrew Jones 	}
3440bec140fSAndrew Jones 
3450bec140fSAndrew Jones 	sctlr_el1 |= (1 << 0) | (1 << 2) | (1 << 12) /* M | C | I */;
3460bec140fSAndrew Jones 	/* TCR_EL1 |= IRGN0:WBWA | ORGN0:WBWA | SH0:Inner-Shareable */;
3470bec140fSAndrew Jones 	tcr_el1 |= (1 << 8) | (1 << 10) | (3 << 12);
3480bec140fSAndrew Jones 	tcr_el1 |= (64 - vm->va_bits) /* T0SZ */;
34910a0cc3bSRyan Roberts 	if (use_lpa2_pte_format(vm))
35010a0cc3bSRyan Roberts 		tcr_el1 |= (1ul << 59) /* DS */;
3510bec140fSAndrew Jones 
352768e9a61SSean Christopherson 	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_SCTLR_EL1), sctlr_el1);
353768e9a61SSean Christopherson 	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TCR_EL1), tcr_el1);
354768e9a61SSean Christopherson 	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MAIR_EL1), DEFAULT_MAIR_EL1);
355e1707175SRyan Roberts 	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TTBR0_EL1), ttbr0_el1);
356768e9a61SSean Christopherson 	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TPIDR_EL1), vcpu->id);
3570bec140fSAndrew Jones }
3580bec140fSAndrew Jones 
vcpu_arch_dump(FILE * stream,struct kvm_vcpu * vcpu,uint8_t indent)359768e9a61SSean Christopherson void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu, uint8_t indent)
3600bec140fSAndrew Jones {
3610bec140fSAndrew Jones 	uint64_t pstate, pc;
3620bec140fSAndrew Jones 
363768e9a61SSean Christopherson 	vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pstate), &pstate);
364768e9a61SSean Christopherson 	vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc), &pc);
3650bec140fSAndrew Jones 
36698e68344SAndrew Jones 	fprintf(stream, "%*spstate: 0x%.16lx pc: 0x%.16lx\n",
3670bec140fSAndrew Jones 		indent, "", pstate, pc);
3680bec140fSAndrew Jones }
369837ec79bSPaolo Bonzini 
vcpu_arch_set_entry_point(struct kvm_vcpu * vcpu,void * guest_code)37053a43dd4SSean Christopherson void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)
37153a43dd4SSean Christopherson {
37253a43dd4SSean Christopherson 	vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pc), (uint64_t)guest_code);
37353a43dd4SSean Christopherson }
37453a43dd4SSean Christopherson 
__aarch64_vcpu_add(struct kvm_vm * vm,uint32_t vcpu_id,struct kvm_vcpu_init * init)37553a43dd4SSean Christopherson static struct kvm_vcpu *__aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
37653a43dd4SSean Christopherson 					   struct kvm_vcpu_init *init)
377837ec79bSPaolo Bonzini {
3785485e822SRicardo Koller 	size_t stack_size;
3795485e822SRicardo Koller 	uint64_t stack_vaddr;
380f742d94fSSean Christopherson 	struct kvm_vcpu *vcpu = __vm_vcpu_add(vm, vcpu_id);
381837ec79bSPaolo Bonzini 
3825485e822SRicardo Koller 	stack_size = vm->page_size == 4096 ? DEFAULT_STACK_PGS * vm->page_size :
3835485e822SRicardo Koller 					     vm->page_size;
3841446e331SRicardo Koller 	stack_vaddr = __vm_vaddr_alloc(vm, stack_size,
3851446e331SRicardo Koller 				       DEFAULT_ARM64_GUEST_STACK_VADDR_MIN,
3861446e331SRicardo Koller 				       MEM_REGION_DATA);
3875485e822SRicardo Koller 
388768e9a61SSean Christopherson 	aarch64_vcpu_setup(vcpu, init);
389837ec79bSPaolo Bonzini 
390768e9a61SSean Christopherson 	vcpu_set_reg(vcpu, ARM64_CORE_REG(sp_el1), stack_vaddr + stack_size);
39153a43dd4SSean Christopherson 	return vcpu;
39253a43dd4SSean Christopherson }
39353a43dd4SSean Christopherson 
aarch64_vcpu_add(struct kvm_vm * vm,uint32_t vcpu_id,struct kvm_vcpu_init * init,void * guest_code)39453a43dd4SSean Christopherson struct kvm_vcpu *aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
39553a43dd4SSean Christopherson 				  struct kvm_vcpu_init *init, void *guest_code)
39653a43dd4SSean Christopherson {
39753a43dd4SSean Christopherson 	struct kvm_vcpu *vcpu = __aarch64_vcpu_add(vm, vcpu_id, init);
39853a43dd4SSean Christopherson 
39953a43dd4SSean Christopherson 	vcpu_arch_set_entry_point(vcpu, guest_code);
4001422efd6SSean Christopherson 
4011422efd6SSean Christopherson 	return vcpu;
402837ec79bSPaolo Bonzini }
403837ec79bSPaolo Bonzini 
vm_arch_vcpu_add(struct kvm_vm * vm,uint32_t vcpu_id)40453a43dd4SSean Christopherson struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
405fb89f4eaSAndrew Jones {
40653a43dd4SSean Christopherson 	return __aarch64_vcpu_add(vm, vcpu_id, NULL);
407fb89f4eaSAndrew Jones }
4089bbf2474SBen Gardon 
vcpu_args_set(struct kvm_vcpu * vcpu,unsigned int num,...)409768e9a61SSean Christopherson void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...)
4109bbf2474SBen Gardon {
4119bbf2474SBen Gardon 	va_list ap;
4129bbf2474SBen Gardon 	int i;
4139bbf2474SBen Gardon 
4149bbf2474SBen Gardon 	TEST_ASSERT(num >= 1 && num <= 8, "Unsupported number of args,\n"
41595be17e4SAndrew Jones 		    "  num: %u", num);
4169bbf2474SBen Gardon 
4179bbf2474SBen Gardon 	va_start(ap, num);
4189bbf2474SBen Gardon 
4199bbf2474SBen Gardon 	for (i = 0; i < num; i++) {
420768e9a61SSean Christopherson 		vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.regs[i]),
4219bbf2474SBen Gardon 			     va_arg(ap, uint64_t));
4229bbf2474SBen Gardon 	}
4239bbf2474SBen Gardon 
4249bbf2474SBen Gardon 	va_end(ap);
4259bbf2474SBen Gardon }
42629faeb96SAaron Lewis 
kvm_exit_unexpected_exception(int vector,uint64_t ec,bool valid_ec)427e3db7579SRicardo Koller void kvm_exit_unexpected_exception(int vector, uint64_t ec, bool valid_ec)
428e3db7579SRicardo Koller {
429e3db7579SRicardo Koller 	ucall(UCALL_UNHANDLED, 3, vector, ec, valid_ec);
430e3db7579SRicardo Koller 	while (1)
431e3db7579SRicardo Koller 		;
432e3db7579SRicardo Koller }
433e3db7579SRicardo Koller 
assert_on_unhandled_exception(struct kvm_vcpu * vcpu)434768e9a61SSean Christopherson void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
43529faeb96SAaron Lewis {
436e3db7579SRicardo Koller 	struct ucall uc;
437e3db7579SRicardo Koller 
438768e9a61SSean Christopherson 	if (get_ucall(vcpu, &uc) != UCALL_UNHANDLED)
439e3db7579SRicardo Koller 		return;
440e3db7579SRicardo Koller 
441e3db7579SRicardo Koller 	if (uc.args[2]) /* valid_ec */ {
442e3db7579SRicardo Koller 		assert(VECTOR_IS_SYNC(uc.args[0]));
443e3db7579SRicardo Koller 		TEST_FAIL("Unexpected exception (vector:0x%lx, ec:0x%lx)",
444e3db7579SRicardo Koller 			  uc.args[0], uc.args[1]);
445e3db7579SRicardo Koller 	} else {
446e3db7579SRicardo Koller 		assert(!VECTOR_IS_SYNC(uc.args[0]));
447e3db7579SRicardo Koller 		TEST_FAIL("Unexpected exception (vector:0x%lx)",
448e3db7579SRicardo Koller 			  uc.args[0]);
449e3db7579SRicardo Koller 	}
450e3db7579SRicardo Koller }
451e3db7579SRicardo Koller 
452e3db7579SRicardo Koller struct handlers {
453e3db7579SRicardo Koller 	handler_fn exception_handlers[VECTOR_NUM][ESR_EC_NUM];
454e3db7579SRicardo Koller };
455e3db7579SRicardo Koller 
vcpu_init_descriptor_tables(struct kvm_vcpu * vcpu)456768e9a61SSean Christopherson void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu)
457e3db7579SRicardo Koller {
458e3db7579SRicardo Koller 	extern char vectors;
459e3db7579SRicardo Koller 
460768e9a61SSean Christopherson 	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_VBAR_EL1), (uint64_t)&vectors);
461e3db7579SRicardo Koller }
462e3db7579SRicardo Koller 
route_exception(struct ex_regs * regs,int vector)463e3db7579SRicardo Koller void route_exception(struct ex_regs *regs, int vector)
464e3db7579SRicardo Koller {
465e3db7579SRicardo Koller 	struct handlers *handlers = (struct handlers *)exception_handlers;
466e3db7579SRicardo Koller 	bool valid_ec;
467e3db7579SRicardo Koller 	int ec = 0;
468e3db7579SRicardo Koller 
469e3db7579SRicardo Koller 	switch (vector) {
470e3db7579SRicardo Koller 	case VECTOR_SYNC_CURRENT:
471e3db7579SRicardo Koller 	case VECTOR_SYNC_LOWER_64:
472e3db7579SRicardo Koller 		ec = (read_sysreg(esr_el1) >> ESR_EC_SHIFT) & ESR_EC_MASK;
473e3db7579SRicardo Koller 		valid_ec = true;
474e3db7579SRicardo Koller 		break;
475e3db7579SRicardo Koller 	case VECTOR_IRQ_CURRENT:
476e3db7579SRicardo Koller 	case VECTOR_IRQ_LOWER_64:
477e3db7579SRicardo Koller 	case VECTOR_FIQ_CURRENT:
478e3db7579SRicardo Koller 	case VECTOR_FIQ_LOWER_64:
479e3db7579SRicardo Koller 	case VECTOR_ERROR_CURRENT:
480e3db7579SRicardo Koller 	case VECTOR_ERROR_LOWER_64:
481e3db7579SRicardo Koller 		ec = 0;
482e3db7579SRicardo Koller 		valid_ec = false;
483e3db7579SRicardo Koller 		break;
484e3db7579SRicardo Koller 	default:
485e3db7579SRicardo Koller 		valid_ec = false;
486e3db7579SRicardo Koller 		goto unexpected_exception;
487e3db7579SRicardo Koller 	}
488e3db7579SRicardo Koller 
489e3db7579SRicardo Koller 	if (handlers && handlers->exception_handlers[vector][ec])
490e3db7579SRicardo Koller 		return handlers->exception_handlers[vector][ec](regs);
491e3db7579SRicardo Koller 
492e3db7579SRicardo Koller unexpected_exception:
493e3db7579SRicardo Koller 	kvm_exit_unexpected_exception(vector, ec, valid_ec);
494e3db7579SRicardo Koller }
495e3db7579SRicardo Koller 
vm_init_descriptor_tables(struct kvm_vm * vm)496e3db7579SRicardo Koller void vm_init_descriptor_tables(struct kvm_vm *vm)
497e3db7579SRicardo Koller {
4981446e331SRicardo Koller 	vm->handlers = __vm_vaddr_alloc(vm, sizeof(struct handlers),
4991446e331SRicardo Koller 					vm->page_size, MEM_REGION_DATA);
500e3db7579SRicardo Koller 
501e3db7579SRicardo Koller 	*(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers;
502e3db7579SRicardo Koller }
503e3db7579SRicardo Koller 
vm_install_sync_handler(struct kvm_vm * vm,int vector,int ec,void (* handler)(struct ex_regs *))504e3db7579SRicardo Koller void vm_install_sync_handler(struct kvm_vm *vm, int vector, int ec,
505e3db7579SRicardo Koller 			 void (*handler)(struct ex_regs *))
506e3db7579SRicardo Koller {
507e3db7579SRicardo Koller 	struct handlers *handlers = addr_gva2hva(vm, vm->handlers);
508e3db7579SRicardo Koller 
509e3db7579SRicardo Koller 	assert(VECTOR_IS_SYNC(vector));
510e3db7579SRicardo Koller 	assert(vector < VECTOR_NUM);
511e3db7579SRicardo Koller 	assert(ec < ESR_EC_NUM);
512e3db7579SRicardo Koller 	handlers->exception_handlers[vector][ec] = handler;
513e3db7579SRicardo Koller }
514e3db7579SRicardo Koller 
vm_install_exception_handler(struct kvm_vm * vm,int vector,void (* handler)(struct ex_regs *))515e3db7579SRicardo Koller void vm_install_exception_handler(struct kvm_vm *vm, int vector,
516e3db7579SRicardo Koller 			 void (*handler)(struct ex_regs *))
517e3db7579SRicardo Koller {
518e3db7579SRicardo Koller 	struct handlers *handlers = addr_gva2hva(vm, vm->handlers);
519e3db7579SRicardo Koller 
520e3db7579SRicardo Koller 	assert(!VECTOR_IS_SYNC(vector));
521e3db7579SRicardo Koller 	assert(vector < VECTOR_NUM);
522e3db7579SRicardo Koller 	handlers->exception_handlers[vector][0] = handler;
52329faeb96SAaron Lewis }
52417229bdcSRaghavendra Rao Ananta 
guest_get_vcpuid(void)52517229bdcSRaghavendra Rao Ananta uint32_t guest_get_vcpuid(void)
52617229bdcSRaghavendra Rao Ananta {
52717229bdcSRaghavendra Rao Ananta 	return read_sysreg(tpidr_el1);
52817229bdcSRaghavendra Rao Ananta }
529cb7c4f36SMarc Zyngier 
max_ipa_for_page_size(uint32_t vm_ipa,uint32_t gran,uint32_t not_sup_val,uint32_t ipa52_min_val)53072324ac5SRyan Roberts static uint32_t max_ipa_for_page_size(uint32_t vm_ipa, uint32_t gran,
53172324ac5SRyan Roberts 				uint32_t not_sup_val, uint32_t ipa52_min_val)
53272324ac5SRyan Roberts {
53372324ac5SRyan Roberts 	if (gran == not_sup_val)
53472324ac5SRyan Roberts 		return 0;
53572324ac5SRyan Roberts 	else if (gran >= ipa52_min_val && vm_ipa >= 52)
53672324ac5SRyan Roberts 		return 52;
53772324ac5SRyan Roberts 	else
53872324ac5SRyan Roberts 		return min(vm_ipa, 48U);
53972324ac5SRyan Roberts }
54072324ac5SRyan Roberts 
aarch64_get_supported_page_sizes(uint32_t ipa,uint32_t * ipa4k,uint32_t * ipa16k,uint32_t * ipa64k)54172324ac5SRyan Roberts void aarch64_get_supported_page_sizes(uint32_t ipa, uint32_t *ipa4k,
54272324ac5SRyan Roberts 					uint32_t *ipa16k, uint32_t *ipa64k)
5430303ffdbSMarc Zyngier {
5440303ffdbSMarc Zyngier 	struct kvm_vcpu_init preferred_init;
5450303ffdbSMarc Zyngier 	int kvm_fd, vm_fd, vcpu_fd, err;
5460303ffdbSMarc Zyngier 	uint64_t val;
54772324ac5SRyan Roberts 	uint32_t gran;
5480303ffdbSMarc Zyngier 	struct kvm_one_reg reg = {
5490303ffdbSMarc Zyngier 		.id	= KVM_ARM64_SYS_REG(SYS_ID_AA64MMFR0_EL1),
5500303ffdbSMarc Zyngier 		.addr	= (uint64_t)&val,
5510303ffdbSMarc Zyngier 	};
5520303ffdbSMarc Zyngier 
5530303ffdbSMarc Zyngier 	kvm_fd = open_kvm_dev_path_or_exit();
554fcba483eSSean Christopherson 	vm_fd = __kvm_ioctl(kvm_fd, KVM_CREATE_VM, (void *)(unsigned long)ipa);
555f9725f89SSean Christopherson 	TEST_ASSERT(vm_fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm_fd));
5560303ffdbSMarc Zyngier 
5570303ffdbSMarc Zyngier 	vcpu_fd = ioctl(vm_fd, KVM_CREATE_VCPU, 0);
558a78593fdSSean Christopherson 	TEST_ASSERT(vcpu_fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VCPU, vcpu_fd));
5590303ffdbSMarc Zyngier 
5600303ffdbSMarc Zyngier 	err = ioctl(vm_fd, KVM_ARM_PREFERRED_TARGET, &preferred_init);
561a78593fdSSean Christopherson 	TEST_ASSERT(err == 0, KVM_IOCTL_ERROR(KVM_ARM_PREFERRED_TARGET, err));
5620303ffdbSMarc Zyngier 	err = ioctl(vcpu_fd, KVM_ARM_VCPU_INIT, &preferred_init);
563a78593fdSSean Christopherson 	TEST_ASSERT(err == 0, KVM_IOCTL_ERROR(KVM_ARM_VCPU_INIT, err));
5640303ffdbSMarc Zyngier 
5650303ffdbSMarc Zyngier 	err = ioctl(vcpu_fd, KVM_GET_ONE_REG, &reg);
566a78593fdSSean Christopherson 	TEST_ASSERT(err == 0, KVM_IOCTL_ERROR(KVM_GET_ONE_REG, vcpu_fd));
5670303ffdbSMarc Zyngier 
56872324ac5SRyan Roberts 	gran = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64MMFR0_EL1_TGRAN4), val);
56972324ac5SRyan Roberts 	*ipa4k = max_ipa_for_page_size(ipa, gran, ID_AA64MMFR0_EL1_TGRAN4_NI,
57072324ac5SRyan Roberts 					ID_AA64MMFR0_EL1_TGRAN4_52_BIT);
57172324ac5SRyan Roberts 
57272324ac5SRyan Roberts 	gran = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64MMFR0_EL1_TGRAN64), val);
57372324ac5SRyan Roberts 	*ipa64k = max_ipa_for_page_size(ipa, gran, ID_AA64MMFR0_EL1_TGRAN64_NI,
57472324ac5SRyan Roberts 					ID_AA64MMFR0_EL1_TGRAN64_IMP);
57572324ac5SRyan Roberts 
57672324ac5SRyan Roberts 	gran = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64MMFR0_EL1_TGRAN16), val);
57772324ac5SRyan Roberts 	*ipa16k = max_ipa_for_page_size(ipa, gran, ID_AA64MMFR0_EL1_TGRAN16_NI,
57872324ac5SRyan Roberts 					ID_AA64MMFR0_EL1_TGRAN16_52_BIT);
5790303ffdbSMarc Zyngier 
5800303ffdbSMarc Zyngier 	close(vcpu_fd);
5810303ffdbSMarc Zyngier 	close(vm_fd);
5820303ffdbSMarc Zyngier 	close(kvm_fd);
5830303ffdbSMarc Zyngier }
5840303ffdbSMarc Zyngier 
585fab19915SOliver Upton #define __smccc_call(insn, function_id, arg0, arg1, arg2, arg3, arg4, arg5,	\
586fab19915SOliver Upton 		     arg6, res)							\
587fab19915SOliver Upton 	asm volatile("mov   w0, %w[function_id]\n"				\
588fab19915SOliver Upton 		     "mov   x1, %[arg0]\n"					\
589fab19915SOliver Upton 		     "mov   x2, %[arg1]\n"					\
590fab19915SOliver Upton 		     "mov   x3, %[arg2]\n"					\
591fab19915SOliver Upton 		     "mov   x4, %[arg3]\n"					\
592fab19915SOliver Upton 		     "mov   x5, %[arg4]\n"					\
593fab19915SOliver Upton 		     "mov   x6, %[arg5]\n"					\
594fab19915SOliver Upton 		     "mov   x7, %[arg6]\n"					\
595fab19915SOliver Upton 		     #insn  "#0\n"						\
596fab19915SOliver Upton 		     "mov   %[res0], x0\n"					\
597fab19915SOliver Upton 		     "mov   %[res1], x1\n"					\
598fab19915SOliver Upton 		     "mov   %[res2], x2\n"					\
599fab19915SOliver Upton 		     "mov   %[res3], x3\n"					\
600fab19915SOliver Upton 		     : [res0] "=r"(res->a0), [res1] "=r"(res->a1),		\
601fab19915SOliver Upton 		       [res2] "=r"(res->a2), [res3] "=r"(res->a3)		\
602fab19915SOliver Upton 		     : [function_id] "r"(function_id), [arg0] "r"(arg0),	\
603fab19915SOliver Upton 		       [arg1] "r"(arg1), [arg2] "r"(arg2), [arg3] "r"(arg3),	\
604fab19915SOliver Upton 		       [arg4] "r"(arg4), [arg5] "r"(arg5), [arg6] "r"(arg6)	\
605fab19915SOliver Upton 		     : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7")
606fab19915SOliver Upton 
607fab19915SOliver Upton 
smccc_hvc(uint32_t function_id,uint64_t arg0,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4,uint64_t arg5,uint64_t arg6,struct arm_smccc_res * res)608e918e2bcSOliver Upton void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
609e918e2bcSOliver Upton 	       uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
610e918e2bcSOliver Upton 	       uint64_t arg6, struct arm_smccc_res *res)
611e918e2bcSOliver Upton {
612fab19915SOliver Upton 	__smccc_call(hvc, function_id, arg0, arg1, arg2, arg3, arg4, arg5,
613fab19915SOliver Upton 		     arg6, res);
614fab19915SOliver Upton }
615fab19915SOliver Upton 
smccc_smc(uint32_t function_id,uint64_t arg0,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4,uint64_t arg5,uint64_t arg6,struct arm_smccc_res * res)616fab19915SOliver Upton void smccc_smc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
617fab19915SOliver Upton 	       uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
618fab19915SOliver Upton 	       uint64_t arg6, struct arm_smccc_res *res)
619fab19915SOliver Upton {
620fab19915SOliver Upton 	__smccc_call(smc, function_id, arg0, arg1, arg2, arg3, arg4, arg5,
621fab19915SOliver Upton 		     arg6, res);
622e918e2bcSOliver Upton }
623e1ab3124SVishal Annapurve 
kvm_selftest_arch_init(void)624e1ab3124SVishal Annapurve void kvm_selftest_arch_init(void)
625e1ab3124SVishal Annapurve {
626e1ab3124SVishal Annapurve 	/*
627e1ab3124SVishal Annapurve 	 * arm64 doesn't have a true default mode, so start by computing the
628e1ab3124SVishal Annapurve 	 * available IPA space and page sizes early.
629e1ab3124SVishal Annapurve 	 */
630e1ab3124SVishal Annapurve 	guest_modes_append_default();
631e1ab3124SVishal Annapurve }
632e8b9a055SOliver Upton 
vm_vaddr_populate_bitmap(struct kvm_vm * vm)633e8b9a055SOliver Upton void vm_vaddr_populate_bitmap(struct kvm_vm *vm)
634e8b9a055SOliver Upton {
635e8b9a055SOliver Upton 	/*
636e8b9a055SOliver Upton 	 * arm64 selftests use only TTBR0_EL1, meaning that the valid VA space
637e8b9a055SOliver Upton 	 * is [0, 2^(64 - TCR_EL1.T0SZ)).
638e8b9a055SOliver Upton 	 */
639e8b9a055SOliver Upton 	sparsebit_set_num(vm->vpages_valid, 0,
640e8b9a055SOliver Upton 			  (1ULL << vm->va_bits) >> vm->page_shift);
641e8b9a055SOliver Upton }
642*ca1a1836SColton Lewis 
643*ca1a1836SColton Lewis /* Helper to call wfi instruction. */
wfi(void)644*ca1a1836SColton Lewis void wfi(void)
645*ca1a1836SColton Lewis {
646*ca1a1836SColton Lewis 	asm volatile("wfi");
647*ca1a1836SColton Lewis }
648