Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: vgic_init kvm selftests fixup

Bring some improvements/rationalization over the first version
of the vgic_init selftests:

- ucall_init is moved in run_cpu()
- vcpu_args_set is not called as not needed
- whenever a helper is supposed to succeed, call the non "_" version
- helpers do not return -errno, instead errno is checked by the caller
- vm_gic struct is used whenever possible, as well as vm_gic_destroy
- _kvm_create_device takes an addition fd parameter

Signed-off-by: Eric Auger <eric.auger@redhat.com>
Suggested-by: Andrew Jones <drjones@redhat.com>
Reviewed-by: Andrew Jones <drjones@redhat.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20210407135937.533141-1-eric.auger@redhat.com

authored by

Eric Auger and committed by
Marc Zyngier
4cffb2df dc0e058e

+136 -171
+121 -154
tools/testing/selftests/kvm/aarch64/vgic_init.c
··· 27 27 int gic_fd; 28 28 }; 29 29 30 - int max_ipa_bits; 30 + static int max_ipa_bits; 31 31 32 32 /* helper to access a redistributor register */ 33 33 static int access_redist_reg(int gicv3_fd, int vcpu, int offset, ··· 51 51 /* we don't want to assert on run execution, hence that helper */ 52 52 static int run_vcpu(struct kvm_vm *vm, uint32_t vcpuid) 53 53 { 54 - int ret; 55 - 56 - vcpu_args_set(vm, vcpuid, 1); 57 - ret = _vcpu_ioctl(vm, vcpuid, KVM_RUN, NULL); 58 - get_ucall(vm, vcpuid, NULL); 59 - 54 + ucall_init(vm, NULL); 55 + int ret = _vcpu_ioctl(vm, vcpuid, KVM_RUN, NULL); 60 56 if (ret) 61 57 return -errno; 62 58 return 0; ··· 64 68 65 69 v.vm = vm_create_default_with_vcpus(NR_VCPUS, 0, 0, guest_code, NULL); 66 70 v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 67 - TEST_ASSERT(v.gic_fd > 0, "GICv3 device created"); 68 71 69 72 return v; 70 73 } ··· 86 91 uint64_t addr; 87 92 88 93 /* Check existing group/attributes */ 89 - ret = _kvm_device_check_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 90 - KVM_VGIC_V3_ADDR_TYPE_DIST); 91 - TEST_ASSERT(!ret, "KVM_DEV_ARM_VGIC_GRP_ADDR/KVM_VGIC_V3_ADDR_TYPE_DIST supported"); 94 + kvm_device_check_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 95 + KVM_VGIC_V3_ADDR_TYPE_DIST); 92 96 93 - ret = _kvm_device_check_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 94 - KVM_VGIC_V3_ADDR_TYPE_REDIST); 95 - TEST_ASSERT(!ret, "KVM_DEV_ARM_VGIC_GRP_ADDR/KVM_VGIC_V3_ADDR_TYPE_REDIST supported"); 97 + kvm_device_check_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 98 + KVM_VGIC_V3_ADDR_TYPE_REDIST); 96 99 97 100 /* check non existing attribute */ 98 101 ret = _kvm_device_check_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 0); 99 - TEST_ASSERT(ret == -ENXIO, "attribute not supported"); 102 + TEST_ASSERT(ret && errno == ENXIO, "attribute not supported"); 100 103 101 104 /* misaligned DIST and REDIST address settings */ 102 105 addr = 0x1000; 103 106 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 104 107 KVM_VGIC_V3_ADDR_TYPE_DIST, &addr, true); 105 - TEST_ASSERT(ret == -EINVAL, "GICv3 dist base not 64kB aligned"); 108 + TEST_ASSERT(ret && errno == EINVAL, "GICv3 dist base not 64kB aligned"); 106 109 107 110 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 108 111 KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr, true); 109 - TEST_ASSERT(ret == -EINVAL, "GICv3 redist base not 64kB aligned"); 112 + TEST_ASSERT(ret && errno == EINVAL, "GICv3 redist base not 64kB aligned"); 110 113 111 114 /* out of range address */ 112 115 if (max_ipa_bits) { 113 116 addr = 1ULL << max_ipa_bits; 114 117 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 115 118 KVM_VGIC_V3_ADDR_TYPE_DIST, &addr, true); 116 - TEST_ASSERT(ret == -E2BIG, "dist address beyond IPA limit"); 119 + TEST_ASSERT(ret && errno == E2BIG, "dist address beyond IPA limit"); 117 120 118 121 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 119 122 KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr, true); 120 - TEST_ASSERT(ret == -E2BIG, "redist address beyond IPA limit"); 123 + TEST_ASSERT(ret && errno == E2BIG, "redist address beyond IPA limit"); 121 124 } 122 125 123 126 /* set REDIST base address @0x0*/ 124 127 addr = 0x00000; 125 - ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 126 - KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr, true); 127 - TEST_ASSERT(!ret, "GICv3 redist base set"); 128 + kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 129 + KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr, true); 128 130 129 131 /* Attempt to create a second legacy redistributor region */ 130 132 addr = 0xE0000; 131 133 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 132 134 KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr, true); 133 - TEST_ASSERT(ret == -EEXIST, "GICv3 redist base set again"); 135 + TEST_ASSERT(ret && errno == EEXIST, "GICv3 redist base set again"); 134 136 135 137 /* Attempt to mix legacy and new redistributor regions */ 136 138 addr = REDIST_REGION_ATTR_ADDR(NR_VCPUS, 0x100000, 0, 0); 137 139 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 138 140 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 139 - TEST_ASSERT(ret == -EINVAL, "attempt to mix GICv3 REDIST and REDIST_REGION"); 141 + TEST_ASSERT(ret && errno == EINVAL, "attempt to mix GICv3 REDIST and REDIST_REGION"); 140 142 141 143 /* 142 144 * Set overlapping DIST / REDIST, cannot be detected here. Will be detected 143 145 * on first vcpu run instead. 144 146 */ 145 147 addr = 3 * 2 * 0x10000; 146 - ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, KVM_VGIC_V3_ADDR_TYPE_DIST, 147 - &addr, true); 148 - TEST_ASSERT(!ret, "dist overlapping rdist"); 148 + kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, KVM_VGIC_V3_ADDR_TYPE_DIST, 149 + &addr, true); 149 150 } 150 151 151 152 /* Test the new REDIST region API */ ··· 157 166 addr = REDIST_REGION_ATTR_ADDR(NR_VCPUS, 0x100000, 2, 0); 158 167 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 159 168 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 160 - TEST_ASSERT(ret == -EINVAL, "redist region attr value with flags != 0"); 169 + TEST_ASSERT(ret && errno == EINVAL, "redist region attr value with flags != 0"); 161 170 162 171 addr = REDIST_REGION_ATTR_ADDR(0, 0x100000, 0, 0); 163 172 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 164 173 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 165 - TEST_ASSERT(ret == -EINVAL, "redist region attr value with count== 0"); 174 + TEST_ASSERT(ret && errno == EINVAL, "redist region attr value with count== 0"); 166 175 167 176 addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 1); 168 177 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 169 178 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 170 - TEST_ASSERT(ret == -EINVAL, "attempt to register the first rdist region with index != 0"); 179 + TEST_ASSERT(ret && errno == EINVAL, 180 + "attempt to register the first rdist region with index != 0"); 171 181 172 182 addr = REDIST_REGION_ATTR_ADDR(2, 0x201000, 0, 1); 173 183 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 174 184 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 175 - TEST_ASSERT(ret == -EINVAL, "rdist region with misaligned address"); 185 + TEST_ASSERT(ret && errno == EINVAL, "rdist region with misaligned address"); 176 186 177 187 addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 0); 178 - ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 179 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 180 - TEST_ASSERT(!ret, "First valid redist region with 2 rdist @ 0x200000, index 0"); 188 + kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 189 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 181 190 182 191 addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 1); 183 192 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 184 193 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 185 - TEST_ASSERT(ret == -EINVAL, "register an rdist region with already used index"); 194 + TEST_ASSERT(ret && errno == EINVAL, "register an rdist region with already used index"); 186 195 187 196 addr = REDIST_REGION_ATTR_ADDR(1, 0x210000, 0, 2); 188 197 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 189 198 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 190 - TEST_ASSERT(ret == -EINVAL, "register an rdist region overlapping with another one"); 199 + TEST_ASSERT(ret && errno == EINVAL, 200 + "register an rdist region overlapping with another one"); 191 201 192 202 addr = REDIST_REGION_ATTR_ADDR(1, 0x240000, 0, 2); 193 203 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 194 204 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 195 - TEST_ASSERT(ret == -EINVAL, "register redist region with index not +1"); 205 + TEST_ASSERT(ret && errno == EINVAL, "register redist region with index not +1"); 196 206 197 207 addr = REDIST_REGION_ATTR_ADDR(1, 0x240000, 0, 1); 198 - ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 199 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 200 - TEST_ASSERT(!ret, "register valid redist region with 1 rdist @ 0x220000, index 1"); 208 + kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 209 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 201 210 202 211 addr = REDIST_REGION_ATTR_ADDR(1, 1ULL << max_ipa_bits, 0, 2); 203 212 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 204 213 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 205 - TEST_ASSERT(ret == -E2BIG, "register redist region with base address beyond IPA range"); 214 + TEST_ASSERT(ret && errno == E2BIG, 215 + "register redist region with base address beyond IPA range"); 206 216 207 217 addr = 0x260000; 208 218 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 209 219 KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr, true); 210 - TEST_ASSERT(ret == -EINVAL, "Mix KVM_VGIC_V3_ADDR_TYPE_REDIST and REDIST_REGION"); 220 + TEST_ASSERT(ret && errno == EINVAL, 221 + "Mix KVM_VGIC_V3_ADDR_TYPE_REDIST and REDIST_REGION"); 211 222 212 223 /* 213 224 * Now there are 2 redist regions: ··· 233 240 addr = REDIST_REGION_ATTR_ADDR(0, 0, 0, 2); 234 241 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 235 242 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, false); 236 - TEST_ASSERT(ret == -ENOENT, "read characteristics of non existing region"); 243 + TEST_ASSERT(ret && errno == ENOENT, "read characteristics of non existing region"); 237 244 238 245 addr = 0x260000; 239 - ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 240 - KVM_VGIC_V3_ADDR_TYPE_DIST, &addr, true); 241 - TEST_ASSERT(!ret, "set dist region"); 246 + kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 247 + KVM_VGIC_V3_ADDR_TYPE_DIST, &addr, true); 242 248 243 249 addr = REDIST_REGION_ATTR_ADDR(1, 0x260000, 0, 2); 244 250 ret = _kvm_device_access(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 245 251 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 246 - TEST_ASSERT(ret == -EINVAL, "register redist region colliding with dist"); 252 + TEST_ASSERT(ret && errno == EINVAL, "register redist region colliding with dist"); 247 253 } 248 254 249 255 /* ··· 256 264 257 265 v.vm = vm_create_default(0, 0, guest_code); 258 266 v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 259 - TEST_ASSERT(v.gic_fd > 0, "GICv3 device created"); 260 267 261 268 subtest_dist_rdist(&v); 262 269 ··· 263 272 for (i = 1; i < NR_VCPUS; ++i) 264 273 vm_vcpu_add_default(v.vm, i, guest_code); 265 274 266 - ucall_init(v.vm, NULL); 267 275 ret = run_vcpu(v.vm, 3); 268 276 TEST_ASSERT(ret == -EINVAL, "dist/rdist overlap detected on 1st vcpu run"); 269 277 ··· 279 289 280 290 subtest_dist_rdist(&v); 281 291 282 - ucall_init(v.vm, NULL); 283 292 ret = run_vcpu(v.vm, 3); 284 293 TEST_ASSERT(ret == -EINVAL, "dist/rdist overlap detected on 1st vcpu run"); 285 294 ··· 294 305 295 306 v = vm_gic_create(); 296 307 subtest_redist_regions(&v); 297 - ret = _kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 298 - KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 299 - TEST_ASSERT(!ret, "init the vgic"); 308 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 309 + KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 300 310 301 - ucall_init(v.vm, NULL); 302 311 ret = run_vcpu(v.vm, 3); 303 312 TEST_ASSERT(ret == -ENXIO, "running without sufficient number of rdists"); 304 313 vm_gic_destroy(&v); ··· 307 320 subtest_redist_regions(&v); 308 321 309 322 addr = REDIST_REGION_ATTR_ADDR(1, 0x280000, 0, 2); 310 - ret = _kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 311 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 312 - TEST_ASSERT(!ret, "register a third region allowing to cover the 4 vcpus"); 323 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 324 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 313 325 314 - ucall_init(v.vm, NULL); 315 326 ret = run_vcpu(v.vm, 3); 316 327 TEST_ASSERT(ret == -EBUSY, "running without vgic explicit init"); 317 328 ··· 320 335 v = vm_gic_create(); 321 336 subtest_redist_regions(&v); 322 337 323 - ret = _kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 324 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, dummy, true); 325 - TEST_ASSERT(ret == -EFAULT, "register a third region allowing to cover the 4 vcpus"); 338 + _kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 339 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, dummy, true); 340 + TEST_ASSERT(ret && errno == EFAULT, 341 + "register a third region allowing to cover the 4 vcpus"); 326 342 327 343 addr = REDIST_REGION_ATTR_ADDR(1, 0x280000, 0, 2); 328 - ret = _kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 329 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 330 - TEST_ASSERT(!ret, "register a third region allowing to cover the 4 vcpus"); 344 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 345 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 331 346 332 - ret = _kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 333 - KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 334 - TEST_ASSERT(!ret, "init the vgic"); 347 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 348 + KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 335 349 336 - ucall_init(v.vm, NULL); 337 350 ret = run_vcpu(v.vm, 3); 338 351 TEST_ASSERT(!ret, "vcpu run"); 339 352 ··· 340 357 341 358 static void test_typer_accesses(void) 342 359 { 343 - int ret, i, gicv3_fd = -1; 360 + struct vm_gic v; 344 361 uint64_t addr; 345 - struct kvm_vm *vm; 346 362 uint32_t val; 363 + int ret, i; 347 364 348 - vm = vm_create_default(0, 0, guest_code); 365 + v.vm = vm_create_default(0, 0, guest_code); 349 366 350 - gicv3_fd = kvm_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 351 - TEST_ASSERT(gicv3_fd >= 0, "VGIC_V3 device created"); 367 + v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 352 368 353 - vm_vcpu_add_default(vm, 3, guest_code); 369 + vm_vcpu_add_default(v.vm, 3, guest_code); 354 370 355 - ret = access_redist_reg(gicv3_fd, 1, GICR_TYPER, &val, false); 356 - TEST_ASSERT(ret == -EINVAL, "attempting to read GICR_TYPER of non created vcpu"); 371 + ret = access_redist_reg(v.gic_fd, 1, GICR_TYPER, &val, false); 372 + TEST_ASSERT(ret && errno == EINVAL, "attempting to read GICR_TYPER of non created vcpu"); 357 373 358 - vm_vcpu_add_default(vm, 1, guest_code); 374 + vm_vcpu_add_default(v.vm, 1, guest_code); 359 375 360 - ret = access_redist_reg(gicv3_fd, 1, GICR_TYPER, &val, false); 361 - TEST_ASSERT(ret == -EBUSY, "read GICR_TYPER before GIC initialized"); 376 + ret = access_redist_reg(v.gic_fd, 1, GICR_TYPER, &val, false); 377 + TEST_ASSERT(ret && errno == EBUSY, "read GICR_TYPER before GIC initialized"); 362 378 363 - vm_vcpu_add_default(vm, 2, guest_code); 379 + vm_vcpu_add_default(v.vm, 2, guest_code); 364 380 365 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 366 - KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 367 - TEST_ASSERT(!ret, "init the vgic after the vcpu creations"); 381 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 382 + KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 368 383 369 384 for (i = 0; i < NR_VCPUS ; i++) { 370 - ret = access_redist_reg(gicv3_fd, 0, GICR_TYPER, &val, false); 385 + ret = access_redist_reg(v.gic_fd, 0, GICR_TYPER, &val, false); 371 386 TEST_ASSERT(!ret && !val, "read GICR_TYPER before rdist region setting"); 372 387 } 373 388 374 389 addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 0); 375 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 376 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 377 - TEST_ASSERT(!ret, "first rdist region with a capacity of 2 rdists"); 390 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 391 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 378 392 379 393 /* The 2 first rdists should be put there (vcpu 0 and 3) */ 380 - ret = access_redist_reg(gicv3_fd, 0, GICR_TYPER, &val, false); 394 + ret = access_redist_reg(v.gic_fd, 0, GICR_TYPER, &val, false); 381 395 TEST_ASSERT(!ret && !val, "read typer of rdist #0"); 382 396 383 - ret = access_redist_reg(gicv3_fd, 3, GICR_TYPER, &val, false); 397 + ret = access_redist_reg(v.gic_fd, 3, GICR_TYPER, &val, false); 384 398 TEST_ASSERT(!ret && val == 0x310, "read typer of rdist #1"); 385 399 386 400 addr = REDIST_REGION_ATTR_ADDR(10, 0x100000, 0, 1); 387 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 401 + ret = _kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 388 402 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 389 - TEST_ASSERT(ret == -EINVAL, "collision with previous rdist region"); 403 + TEST_ASSERT(ret && errno == EINVAL, "collision with previous rdist region"); 390 404 391 - ret = access_redist_reg(gicv3_fd, 1, GICR_TYPER, &val, false); 405 + ret = access_redist_reg(v.gic_fd, 1, GICR_TYPER, &val, false); 392 406 TEST_ASSERT(!ret && val == 0x100, 393 407 "no redist region attached to vcpu #1 yet, last cannot be returned"); 394 408 395 - ret = access_redist_reg(gicv3_fd, 2, GICR_TYPER, &val, false); 409 + ret = access_redist_reg(v.gic_fd, 2, GICR_TYPER, &val, false); 396 410 TEST_ASSERT(!ret && val == 0x200, 397 411 "no redist region attached to vcpu #2, last cannot be returned"); 398 412 399 413 addr = REDIST_REGION_ATTR_ADDR(10, 0x20000, 0, 1); 400 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 401 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 402 - TEST_ASSERT(!ret, "second rdist region"); 414 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 415 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 403 416 404 - ret = access_redist_reg(gicv3_fd, 1, GICR_TYPER, &val, false); 417 + ret = access_redist_reg(v.gic_fd, 1, GICR_TYPER, &val, false); 405 418 TEST_ASSERT(!ret && val == 0x100, "read typer of rdist #1"); 406 419 407 - ret = access_redist_reg(gicv3_fd, 2, GICR_TYPER, &val, false); 420 + ret = access_redist_reg(v.gic_fd, 2, GICR_TYPER, &val, false); 408 421 TEST_ASSERT(!ret && val == 0x210, 409 422 "read typer of rdist #1, last properly returned"); 410 423 411 - close(gicv3_fd); 412 - kvm_vm_free(vm); 424 + vm_gic_destroy(&v); 413 425 } 414 426 415 427 /** ··· 420 442 static void test_last_bit_redist_regions(void) 421 443 { 422 444 uint32_t vcpuids[] = { 0, 3, 5, 4, 1, 2 }; 423 - int ret, gicv3_fd; 445 + struct vm_gic v; 424 446 uint64_t addr; 425 - struct kvm_vm *vm; 426 447 uint32_t val; 448 + int ret; 427 449 428 - vm = vm_create_default_with_vcpus(6, 0, 0, guest_code, vcpuids); 450 + v.vm = vm_create_default_with_vcpus(6, 0, 0, guest_code, vcpuids); 429 451 430 - gicv3_fd = kvm_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 431 - TEST_ASSERT(gicv3_fd >= 0, "VGIC_V3 device created"); 452 + v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 432 453 433 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 434 - KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 435 - TEST_ASSERT(!ret, "init the vgic after the vcpu creations"); 454 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 455 + KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 436 456 437 457 addr = REDIST_REGION_ATTR_ADDR(2, 0x100000, 0, 0); 438 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 439 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 440 - TEST_ASSERT(!ret, "rdist region #0 (2 rdist)"); 458 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 459 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 441 460 442 461 addr = REDIST_REGION_ATTR_ADDR(2, 0x240000, 0, 1); 443 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 444 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 445 - TEST_ASSERT(!ret, "rdist region #1 (1 rdist) contiguous with #2"); 462 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 463 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 446 464 447 465 addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 2); 448 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 449 - KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 450 - TEST_ASSERT(!ret, "rdist region #2 with a capacity of 2 rdists"); 466 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 467 + KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr, true); 451 468 452 - ret = access_redist_reg(gicv3_fd, 0, GICR_TYPER, &val, false); 469 + ret = access_redist_reg(v.gic_fd, 0, GICR_TYPER, &val, false); 453 470 TEST_ASSERT(!ret && val == 0x000, "read typer of rdist #0"); 454 471 455 - ret = access_redist_reg(gicv3_fd, 1, GICR_TYPER, &val, false); 472 + ret = access_redist_reg(v.gic_fd, 1, GICR_TYPER, &val, false); 456 473 TEST_ASSERT(!ret && val == 0x100, "read typer of rdist #1"); 457 474 458 - ret = access_redist_reg(gicv3_fd, 2, GICR_TYPER, &val, false); 475 + ret = access_redist_reg(v.gic_fd, 2, GICR_TYPER, &val, false); 459 476 TEST_ASSERT(!ret && val == 0x200, "read typer of rdist #2"); 460 477 461 - ret = access_redist_reg(gicv3_fd, 3, GICR_TYPER, &val, false); 478 + ret = access_redist_reg(v.gic_fd, 3, GICR_TYPER, &val, false); 462 479 TEST_ASSERT(!ret && val == 0x310, "read typer of rdist #3"); 463 480 464 - ret = access_redist_reg(gicv3_fd, 5, GICR_TYPER, &val, false); 481 + ret = access_redist_reg(v.gic_fd, 5, GICR_TYPER, &val, false); 465 482 TEST_ASSERT(!ret && val == 0x500, "read typer of rdist #5"); 466 483 467 - ret = access_redist_reg(gicv3_fd, 4, GICR_TYPER, &val, false); 484 + ret = access_redist_reg(v.gic_fd, 4, GICR_TYPER, &val, false); 468 485 TEST_ASSERT(!ret && val == 0x410, "read typer of rdist #4"); 469 486 470 - close(gicv3_fd); 471 - kvm_vm_free(vm); 487 + vm_gic_destroy(&v); 472 488 } 473 489 474 490 /* Test last bit with legacy region */ 475 491 static void test_last_bit_single_rdist(void) 476 492 { 477 493 uint32_t vcpuids[] = { 0, 3, 5, 4, 1, 2 }; 478 - int ret, gicv3_fd; 494 + struct vm_gic v; 479 495 uint64_t addr; 480 - struct kvm_vm *vm; 481 496 uint32_t val; 497 + int ret; 482 498 483 - vm = vm_create_default_with_vcpus(6, 0, 0, guest_code, vcpuids); 499 + v.vm = vm_create_default_with_vcpus(6, 0, 0, guest_code, vcpuids); 484 500 485 - gicv3_fd = kvm_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 486 - TEST_ASSERT(gicv3_fd >= 0, "VGIC_V3 device created"); 501 + v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 487 502 488 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 489 - KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 490 - TEST_ASSERT(!ret, "init the vgic after the vcpu creations"); 503 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 504 + KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 491 505 492 506 addr = 0x10000; 493 - ret = _kvm_device_access(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 494 - KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr, true); 507 + kvm_device_access(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 508 + KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr, true); 495 509 496 - ret = access_redist_reg(gicv3_fd, 0, GICR_TYPER, &val, false); 510 + ret = access_redist_reg(v.gic_fd, 0, GICR_TYPER, &val, false); 497 511 TEST_ASSERT(!ret && val == 0x000, "read typer of rdist #0"); 498 512 499 - ret = access_redist_reg(gicv3_fd, 3, GICR_TYPER, &val, false); 513 + ret = access_redist_reg(v.gic_fd, 3, GICR_TYPER, &val, false); 500 514 TEST_ASSERT(!ret && val == 0x300, "read typer of rdist #1"); 501 515 502 - ret = access_redist_reg(gicv3_fd, 5, GICR_TYPER, &val, false); 516 + ret = access_redist_reg(v.gic_fd, 5, GICR_TYPER, &val, false); 503 517 TEST_ASSERT(!ret && val == 0x500, "read typer of rdist #2"); 504 518 505 - ret = access_redist_reg(gicv3_fd, 1, GICR_TYPER, &val, false); 519 + ret = access_redist_reg(v.gic_fd, 1, GICR_TYPER, &val, false); 506 520 TEST_ASSERT(!ret && val == 0x100, "read typer of rdist #3"); 507 521 508 - ret = access_redist_reg(gicv3_fd, 2, GICR_TYPER, &val, false); 522 + ret = access_redist_reg(v.gic_fd, 2, GICR_TYPER, &val, false); 509 523 TEST_ASSERT(!ret && val == 0x210, "read typer of rdist #3"); 510 524 511 - close(gicv3_fd); 512 - kvm_vm_free(vm); 525 + vm_gic_destroy(&v); 513 526 } 514 527 515 528 void test_kvm_device(void) 516 529 { 517 530 struct vm_gic v; 518 - int ret; 531 + int ret, fd; 519 532 520 533 v.vm = vm_create_default_with_vcpus(NR_VCPUS, 0, 0, guest_code, NULL); 521 534 522 535 /* try to create a non existing KVM device */ 523 - ret = _kvm_create_device(v.vm, 0, true); 524 - TEST_ASSERT(ret == -ENODEV, "unsupported device"); 536 + ret = _kvm_create_device(v.vm, 0, true, &fd); 537 + TEST_ASSERT(ret && errno == ENODEV, "unsupported device"); 525 538 526 539 /* trial mode with VGIC_V3 device */ 527 - ret = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, true); 540 + ret = _kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, true, &fd); 528 541 if (ret) { 529 542 print_skip("GICv3 not supported"); 530 543 exit(KSFT_SKIP); 531 544 } 532 545 v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 533 - TEST_ASSERT(v.gic_fd, "create the GICv3 device"); 534 546 535 - ret = _kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, false); 536 - TEST_ASSERT(ret == -EEXIST, "create GICv3 device twice"); 547 + ret = _kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, false, &fd); 548 + TEST_ASSERT(ret && errno == EEXIST, "create GICv3 device twice"); 537 549 538 - ret = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, true); 539 - TEST_ASSERT(!ret, "create GICv3 in test mode while the same already is created"); 550 + kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3, true); 540 551 541 - if (!_kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V2, true)) { 542 - ret = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V2, false); 543 - TEST_ASSERT(ret == -EINVAL, "create GICv2 while v3 exists"); 552 + if (!_kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V2, true, &fd)) { 553 + ret = _kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V2, false, &fd); 554 + TEST_ASSERT(ret && errno == EINVAL, "create GICv2 while v3 exists"); 544 555 } 545 556 546 557 vm_gic_destroy(&v);
+1 -1
tools/testing/selftests/kvm/include/kvm_util.h
··· 225 225 226 226 int _kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr); 227 227 int kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr); 228 - int _kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test); 228 + int _kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test, int *fd); 229 229 int kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test); 230 230 int _kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, 231 231 void *val, bool write);
+14 -16
tools/testing/selftests/kvm/lib/kvm_util.c
··· 1739 1739 .attr = attr, 1740 1740 .flags = 0, 1741 1741 }; 1742 - int ret = ioctl(dev_fd, KVM_HAS_DEVICE_ATTR, &attribute); 1743 1742 1744 - if (ret == -1) 1745 - return -errno; 1746 - return 0; 1743 + return ioctl(dev_fd, KVM_HAS_DEVICE_ATTR, &attribute); 1747 1744 } 1748 1745 1749 1746 int kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr) 1750 1747 { 1751 1748 int ret = _kvm_device_check_attr(dev_fd, group, attr); 1752 1749 1753 - TEST_ASSERT(ret >= 0, "KVM_HAS_DEVICE_ATTR failed, errno: %i", errno); 1750 + TEST_ASSERT(ret >= 0, "KVM_HAS_DEVICE_ATTR failed, rc: %i errno: %i", ret, errno); 1754 1751 return ret; 1755 1752 } 1756 1753 1757 - int _kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test) 1754 + int _kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test, int *fd) 1758 1755 { 1759 1756 struct kvm_create_device create_dev; 1760 1757 int ret; ··· 1760 1763 create_dev.fd = -1; 1761 1764 create_dev.flags = test ? KVM_CREATE_DEVICE_TEST : 0; 1762 1765 ret = ioctl(vm_get_fd(vm), KVM_CREATE_DEVICE, &create_dev); 1763 - if (ret == -1) 1764 - return -errno; 1765 - return test ? 0 : create_dev.fd; 1766 + *fd = create_dev.fd; 1767 + return ret; 1766 1768 } 1767 1769 1768 1770 int kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test) 1769 1771 { 1770 - int ret = _kvm_create_device(vm, type, test); 1772 + int fd, ret; 1771 1773 1772 - TEST_ASSERT(ret >= 0, "KVM_CREATE_DEVICE IOCTL failed,\n" 1773 - " errno: %i", errno); 1774 + ret = _kvm_create_device(vm, type, test, &fd); 1775 + 1776 + if (!test) { 1777 + TEST_ASSERT(ret >= 0, 1778 + "KVM_CREATE_DEVICE IOCTL failed, rc: %i errno: %i", ret, errno); 1779 + return fd; 1780 + } 1774 1781 return ret; 1775 1782 } 1776 1783 ··· 1791 1790 1792 1791 ret = ioctl(dev_fd, write ? KVM_SET_DEVICE_ATTR : KVM_GET_DEVICE_ATTR, 1793 1792 &kvmattr); 1794 - if (ret < 0) 1795 - return -errno; 1796 1793 return ret; 1797 1794 } 1798 1795 ··· 1799 1800 { 1800 1801 int ret = _kvm_device_access(dev_fd, group, attr, val, write); 1801 1802 1802 - TEST_ASSERT(ret >= 0, "KVM_SET|GET_DEVICE_ATTR IOCTL failed,\n" 1803 - " errno: %i", errno); 1803 + TEST_ASSERT(ret >= 0, "KVM_SET|GET_DEVICE_ATTR IOCTL failed, rc: %i errno: %i", ret, errno); 1804 1804 return ret; 1805 1805 } 1806 1806