1 // SPDX-License-Identifier: GPL-2.0-only 2 3 /* hypercalls: Check the ARM64's psuedo-firmware bitmap register interface. 4 * 5 * The test validates the basic hypercall functionalities that are exposed 6 * via the psuedo-firmware bitmap register. This includes the registers' 7 * read/write behavior before and after the VM has started, and if the 8 * hypercalls are properly masked or unmasked to the guest when disabled or 9 * enabled from the KVM userspace, respectively. 10 */ 11 12 #include <errno.h> 13 #include <linux/arm-smccc.h> 14 #include <asm/kvm.h> 15 #include <kvm_util.h> 16 17 #include "processor.h" 18 19 #define FW_REG_ULIMIT_VAL(max_feat_bit) (GENMASK(max_feat_bit, 0)) 20 21 /* Last valid bits of the bitmapped firmware registers */ 22 #define KVM_REG_ARM_STD_BMAP_BIT_MAX 0 23 #define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0 24 #define KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX 1 25 26 struct kvm_fw_reg_info { 27 uint64_t reg; /* Register definition */ 28 uint64_t max_feat_bit; /* Bit that represents the upper limit of the feature-map */ 29 }; 30 31 #define FW_REG_INFO(r) \ 32 { \ 33 .reg = r, \ 34 .max_feat_bit = r##_BIT_MAX, \ 35 } 36 37 static const struct kvm_fw_reg_info fw_reg_info[] = { 38 FW_REG_INFO(KVM_REG_ARM_STD_BMAP), 39 FW_REG_INFO(KVM_REG_ARM_STD_HYP_BMAP), 40 FW_REG_INFO(KVM_REG_ARM_VENDOR_HYP_BMAP), 41 }; 42 43 enum test_stage { 44 TEST_STAGE_REG_IFACE, 45 TEST_STAGE_HVC_IFACE_FEAT_DISABLED, 46 TEST_STAGE_HVC_IFACE_FEAT_ENABLED, 47 TEST_STAGE_HVC_IFACE_FALSE_INFO, 48 TEST_STAGE_END, 49 }; 50 51 static int stage = TEST_STAGE_REG_IFACE; 52 53 struct test_hvc_info { 54 uint32_t func_id; 55 uint64_t arg1; 56 }; 57 58 #define TEST_HVC_INFO(f, a1) \ 59 { \ 60 .func_id = f, \ 61 .arg1 = a1, \ 62 } 63 64 static const struct test_hvc_info hvc_info[] = { 65 /* KVM_REG_ARM_STD_BMAP */ 66 TEST_HVC_INFO(ARM_SMCCC_TRNG_VERSION, 0), 67 TEST_HVC_INFO(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND64), 68 TEST_HVC_INFO(ARM_SMCCC_TRNG_GET_UUID, 0), 69 TEST_HVC_INFO(ARM_SMCCC_TRNG_RND32, 0), 70 TEST_HVC_INFO(ARM_SMCCC_TRNG_RND64, 0), 71 72 /* KVM_REG_ARM_STD_HYP_BMAP */ 73 TEST_HVC_INFO(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, ARM_SMCCC_HV_PV_TIME_FEATURES), 74 TEST_HVC_INFO(ARM_SMCCC_HV_PV_TIME_FEATURES, ARM_SMCCC_HV_PV_TIME_ST), 75 TEST_HVC_INFO(ARM_SMCCC_HV_PV_TIME_ST, 0), 76 77 /* KVM_REG_ARM_VENDOR_HYP_BMAP */ 78 TEST_HVC_INFO(ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID, 79 ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID), 80 TEST_HVC_INFO(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, 0), 81 TEST_HVC_INFO(ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID, KVM_PTP_VIRT_COUNTER), 82 }; 83 84 /* Feed false hypercall info to test the KVM behavior */ 85 static const struct test_hvc_info false_hvc_info[] = { 86 /* Feature support check against a different family of hypercalls */ 87 TEST_HVC_INFO(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID), 88 TEST_HVC_INFO(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, ARM_SMCCC_TRNG_RND64), 89 TEST_HVC_INFO(ARM_SMCCC_HV_PV_TIME_FEATURES, ARM_SMCCC_TRNG_RND64), 90 }; 91 92 static void guest_test_hvc(const struct test_hvc_info *hc_info) 93 { 94 unsigned int i; 95 struct arm_smccc_res res; 96 unsigned int hvc_info_arr_sz; 97 98 hvc_info_arr_sz = 99 hc_info == hvc_info ? ARRAY_SIZE(hvc_info) : ARRAY_SIZE(false_hvc_info); 100 101 for (i = 0; i < hvc_info_arr_sz; i++, hc_info++) { 102 memset(&res, 0, sizeof(res)); 103 smccc_hvc(hc_info->func_id, hc_info->arg1, 0, 0, 0, 0, 0, 0, &res); 104 105 switch (stage) { 106 case TEST_STAGE_HVC_IFACE_FEAT_DISABLED: 107 case TEST_STAGE_HVC_IFACE_FALSE_INFO: 108 GUEST_ASSERT_3(res.a0 == SMCCC_RET_NOT_SUPPORTED, 109 res.a0, hc_info->func_id, hc_info->arg1); 110 break; 111 case TEST_STAGE_HVC_IFACE_FEAT_ENABLED: 112 GUEST_ASSERT_3(res.a0 != SMCCC_RET_NOT_SUPPORTED, 113 res.a0, hc_info->func_id, hc_info->arg1); 114 break; 115 default: 116 GUEST_ASSERT_1(0, stage); 117 } 118 } 119 } 120 121 static void guest_code(void) 122 { 123 while (stage != TEST_STAGE_END) { 124 switch (stage) { 125 case TEST_STAGE_REG_IFACE: 126 break; 127 case TEST_STAGE_HVC_IFACE_FEAT_DISABLED: 128 case TEST_STAGE_HVC_IFACE_FEAT_ENABLED: 129 guest_test_hvc(hvc_info); 130 break; 131 case TEST_STAGE_HVC_IFACE_FALSE_INFO: 132 guest_test_hvc(false_hvc_info); 133 break; 134 default: 135 GUEST_ASSERT_1(0, stage); 136 } 137 138 GUEST_SYNC(stage); 139 } 140 141 GUEST_DONE(); 142 } 143 144 struct st_time { 145 uint32_t rev; 146 uint32_t attr; 147 uint64_t st_time; 148 }; 149 150 #define STEAL_TIME_SIZE ((sizeof(struct st_time) + 63) & ~63) 151 #define ST_GPA_BASE (1 << 30) 152 153 static void steal_time_init(struct kvm_vm *vm) 154 { 155 uint64_t st_ipa = (ulong)ST_GPA_BASE; 156 unsigned int gpages; 157 struct kvm_device_attr dev = { 158 .group = KVM_ARM_VCPU_PVTIME_CTRL, 159 .attr = KVM_ARM_VCPU_PVTIME_IPA, 160 .addr = (uint64_t)&st_ipa, 161 }; 162 163 gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE); 164 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0); 165 166 vcpu_ioctl(vm, 0, KVM_SET_DEVICE_ATTR, &dev); 167 } 168 169 static void test_fw_regs_before_vm_start(struct kvm_vm *vm) 170 { 171 uint64_t val; 172 unsigned int i; 173 int ret; 174 175 for (i = 0; i < ARRAY_SIZE(fw_reg_info); i++) { 176 const struct kvm_fw_reg_info *reg_info = &fw_reg_info[i]; 177 178 /* First 'read' should be an upper limit of the features supported */ 179 vcpu_get_reg(vm, 0, reg_info->reg, &val); 180 TEST_ASSERT(val == FW_REG_ULIMIT_VAL(reg_info->max_feat_bit), 181 "Expected all the features to be set for reg: 0x%lx; expected: 0x%lx; read: 0x%lx\n", 182 reg_info->reg, FW_REG_ULIMIT_VAL(reg_info->max_feat_bit), val); 183 184 /* Test a 'write' by disabling all the features of the register map */ 185 ret = __vcpu_set_reg(vm, 0, reg_info->reg, 0); 186 TEST_ASSERT(ret == 0, 187 "Failed to clear all the features of reg: 0x%lx; ret: %d\n", 188 reg_info->reg, errno); 189 190 vcpu_get_reg(vm, 0, reg_info->reg, &val); 191 TEST_ASSERT(val == 0, 192 "Expected all the features to be cleared for reg: 0x%lx\n", reg_info->reg); 193 194 /* 195 * Test enabling a feature that's not supported. 196 * Avoid this check if all the bits are occupied. 197 */ 198 if (reg_info->max_feat_bit < 63) { 199 ret = __vcpu_set_reg(vm, 0, reg_info->reg, BIT(reg_info->max_feat_bit + 1)); 200 TEST_ASSERT(ret != 0 && errno == EINVAL, 201 "Unexpected behavior or return value (%d) while setting an unsupported feature for reg: 0x%lx\n", 202 errno, reg_info->reg); 203 } 204 } 205 } 206 207 static void test_fw_regs_after_vm_start(struct kvm_vm *vm) 208 { 209 uint64_t val; 210 unsigned int i; 211 int ret; 212 213 for (i = 0; i < ARRAY_SIZE(fw_reg_info); i++) { 214 const struct kvm_fw_reg_info *reg_info = &fw_reg_info[i]; 215 216 /* 217 * Before starting the VM, the test clears all the bits. 218 * Check if that's still the case. 219 */ 220 vcpu_get_reg(vm, 0, reg_info->reg, &val); 221 TEST_ASSERT(val == 0, 222 "Expected all the features to be cleared for reg: 0x%lx\n", 223 reg_info->reg); 224 225 /* 226 * Since the VM has run at least once, KVM shouldn't allow modification of 227 * the registers and should return EBUSY. Set the registers and check for 228 * the expected errno. 229 */ 230 ret = __vcpu_set_reg(vm, 0, reg_info->reg, FW_REG_ULIMIT_VAL(reg_info->max_feat_bit)); 231 TEST_ASSERT(ret != 0 && errno == EBUSY, 232 "Unexpected behavior or return value (%d) while setting a feature while VM is running for reg: 0x%lx\n", 233 errno, reg_info->reg); 234 } 235 } 236 237 static struct kvm_vm *test_vm_create(void) 238 { 239 struct kvm_vm *vm; 240 241 vm = vm_create_default(0, 0, guest_code); 242 243 ucall_init(vm, NULL); 244 steal_time_init(vm); 245 246 return vm; 247 } 248 249 static struct kvm_vm *test_guest_stage(struct kvm_vm *vm) 250 { 251 struct kvm_vm *ret_vm = vm; 252 253 pr_debug("Stage: %d\n", stage); 254 255 switch (stage) { 256 case TEST_STAGE_REG_IFACE: 257 test_fw_regs_after_vm_start(vm); 258 break; 259 case TEST_STAGE_HVC_IFACE_FEAT_DISABLED: 260 /* Start a new VM so that all the features are now enabled by default */ 261 kvm_vm_free(vm); 262 ret_vm = test_vm_create(); 263 break; 264 case TEST_STAGE_HVC_IFACE_FEAT_ENABLED: 265 case TEST_STAGE_HVC_IFACE_FALSE_INFO: 266 break; 267 default: 268 TEST_FAIL("Unknown test stage: %d\n", stage); 269 } 270 271 stage++; 272 sync_global_to_guest(vm, stage); 273 274 return ret_vm; 275 } 276 277 static void test_run(void) 278 { 279 struct kvm_vm *vm; 280 struct ucall uc; 281 bool guest_done = false; 282 283 vm = test_vm_create(); 284 285 test_fw_regs_before_vm_start(vm); 286 287 while (!guest_done) { 288 vcpu_run(vm, 0); 289 290 switch (get_ucall(vm, 0, &uc)) { 291 case UCALL_SYNC: 292 vm = test_guest_stage(vm); 293 break; 294 case UCALL_DONE: 295 guest_done = true; 296 break; 297 case UCALL_ABORT: 298 TEST_FAIL("%s at %s:%ld\n\tvalues: 0x%lx, 0x%lx; 0x%lx, stage: %u", 299 (const char *)uc.args[0], __FILE__, uc.args[1], 300 uc.args[2], uc.args[3], uc.args[4], stage); 301 break; 302 default: 303 TEST_FAIL("Unexpected guest exit\n"); 304 } 305 } 306 307 kvm_vm_free(vm); 308 } 309 310 int main(void) 311 { 312 setbuf(stdout, NULL); 313 314 test_run(); 315 return 0; 316 } 317