From 13c871aec2e9b1976369656d40f92a2ff3296a13 Mon Sep 17 00:00:00 2001 From: Sebastian Ene Date: Wed, 18 Dec 2024 15:39:29 +0000 Subject: [PATCH] ANDROID: KVM: arm64: Allow the pVM guest to boot with different granule Remove the limitation of having the same page size when we probe the hypervisor services from a protected guest VM and allow 4K protected guest VMs to boot on 16Kb kernels. Test: Build Microdroid Kernel with this patch. tools/bazel run --config=fast --lto=thin //common:kernel_aarch64_microdroid_dist -- --destdir=out/dist Replace the kernel with the one already built in the aosp repo: $(aosp)/package/modules/Virtualization/guest/kernel/android15-6.6/arm64/kernel-6.6 Rebuild virtualization service and reinstall the apex Reboot the device and make sure that protected VMs start on a 16Kb host. Bug: 381400679 Bug: 357781595 Change-Id: I8ac96833c3fa031a40f02ca18d786febce8aa3b8 Signed-off-by: Sebastian Ene --- drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c index e90e347ed457..9f8ada6dbbbd 100644 --- a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +++ b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c @@ -63,6 +63,9 @@ static int arm_smccc_do_range(u32 func_id, phys_addr_t phys, int numpages, size_t size = numpages * PAGE_SIZE; int numgranules; + if (!IS_ALIGNED(phys, pkvm_granule)) + return -EINVAL; + numgranules = DIV_ROUND_UP(size, pkvm_granule); phys = ALIGN_DOWN(phys, pkvm_granule); @@ -74,12 +77,18 @@ static int arm_smccc_do_range(u32 func_id, phys_addr_t phys, int numpages, static int pkvm_set_memory_encrypted(unsigned long addr, int numpages) { + if (!IS_ALIGNED(numpages * PAGE_SIZE, pkvm_granule)) + return -EINVAL; + return arm_smccc_do_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID, virt_to_phys((void *)addr), numpages, pkvm_func_range); } static int pkvm_set_memory_decrypted(unsigned long addr, int numpages) { + if (!IS_ALIGNED(numpages * PAGE_SIZE, pkvm_granule)) + return -EINVAL; + return arm_smccc_do_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID, virt_to_phys((void *)addr), numpages, pkvm_func_range); } @@ -153,7 +162,7 @@ void pkvm_init_hyp_services(void) arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID, 0, 0, 0, &res); - if (res.a0 > PAGE_SIZE) /* Includes error codes */ + if ((long)res.a0 < 0) return; pkvm_granule = res.a0;