1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for the hypercall interface exposed to protected guests by
7 * Copyright (C) 2024 Google LLC
10 #include <linux/arm-smccc.h>
11 #include <linux/array_size.h>
13 #include <linux/mem_encrypt.h>
15 #include <linux/pgtable.h>
17 #include <asm/hypervisor.h>
19 static size_t pkvm_granule;
21 static int arm_smccc_do_one_page(u32 func_id, phys_addr_t phys)
23 phys_addr_t end = phys + PAGE_SIZE;
26 struct arm_smccc_res res;
28 arm_smccc_1_1_invoke(func_id, phys, 0, 0, &res);
29 if (res.a0 != SMCCC_RET_SUCCESS)
38 static int __set_memory_range(u32 func_id, unsigned long start, int numpages)
40 void *addr = (void *)start, *end = addr + numpages * PAGE_SIZE;
45 err = arm_smccc_do_one_page(func_id, virt_to_phys(addr));
55 static int pkvm_set_memory_encrypted(unsigned long addr, int numpages)
57 return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID,
61 static int pkvm_set_memory_decrypted(unsigned long addr, int numpages)
63 return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID,
67 static const struct arm64_mem_crypt_ops pkvm_crypt_ops = {
68 .encrypt = pkvm_set_memory_encrypted,
69 .decrypt = pkvm_set_memory_decrypted,
72 static int mmio_guard_ioremap_hook(phys_addr_t phys, size_t size,
76 pteval_t protval = pgprot_val(*prot);
79 * We only expect MMIO emulation for regions mapped with device
82 if (protval != PROT_DEVICE_nGnRE && protval != PROT_DEVICE_nGnRnE)
85 phys = PAGE_ALIGN_DOWN(phys);
86 end = phys + PAGE_ALIGN(size);
89 const int func_id = ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_FUNC_ID;
91 WARN_ON_ONCE(arm_smccc_do_one_page(func_id, phys));
98 void pkvm_init_hyp_services(void)
101 struct arm_smccc_res res;
102 const u32 funcs[] = {
103 ARM_SMCCC_KVM_FUNC_HYP_MEMINFO,
104 ARM_SMCCC_KVM_FUNC_MEM_SHARE,
105 ARM_SMCCC_KVM_FUNC_MEM_UNSHARE,
108 for (i = 0; i < ARRAY_SIZE(funcs); ++i) {
109 if (!kvm_arm_hyp_service_available(funcs[i]))
113 arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID,
115 if (res.a0 > PAGE_SIZE) /* Includes error codes */
118 pkvm_granule = res.a0;
119 arm64_mem_crypt_ops_register(&pkvm_crypt_ops);
121 if (kvm_arm_hyp_service_available(ARM_SMCCC_KVM_FUNC_MMIO_GUARD))
122 arm64_ioremap_prot_hook_register(&mmio_guard_ioremap_hook);