Skip to content

Commit 250b8d6

Browse files
rananta468Marc Zyngier
authored andcommitted
KVM: arm64: selftests: Add host support for vGIC
Implement a simple library to perform vGIC-v3 setup from a host point of view. This includes creating a vGIC device, setting up distributor and redistributor attributes, and mapping the guest physical addresses. The definition of REDIST_REGION_ATTR_ADDR is taken from aarch64/vgic_init test. Hence, replace the definition by including vgic.h in the test file. Signed-off-by: Raghavendra Rao Ananta <[email protected]> Reviewed-by: Ricardo Koller <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 2828165 commit 250b8d6

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

tools/testing/selftests/kvm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ endif
3535

3636
LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/rbtree.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c
3737
LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S
38-
LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c
38+
LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c lib/aarch64/vgic.c
3939
LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c
4040

4141
TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test

tools/testing/selftests/kvm/aarch64/vgic_init.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
#include "test_util.h"
1414
#include "kvm_util.h"
1515
#include "processor.h"
16+
#include "vgic.h"
1617

1718
#define NR_VCPUS 4
1819

19-
#define REDIST_REGION_ATTR_ADDR(count, base, flags, index) (((uint64_t)(count) << 52) | \
20-
((uint64_t)((base) >> 16) << 16) | ((uint64_t)(flags) << 12) | index)
2120
#define REG_OFFSET(vcpu, offset) (((uint64_t)vcpu << 32) | offset)
2221

2322
#define GICR_TYPER 0x8
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* ARM Generic Interrupt Controller (GIC) host specific defines
4+
*/
5+
6+
#ifndef SELFTEST_KVM_VGIC_H
7+
#define SELFTEST_KVM_VGIC_H
8+
9+
#include <linux/kvm.h>
10+
11+
#define REDIST_REGION_ATTR_ADDR(count, base, flags, index) \
12+
(((uint64_t)(count) << 52) | \
13+
((uint64_t)((base) >> 16) << 16) | \
14+
((uint64_t)(flags) << 12) | \
15+
index)
16+
17+
int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus,
18+
uint64_t gicd_base_gpa, uint64_t gicr_base_gpa);
19+
20+
#endif /* SELFTEST_KVM_VGIC_H */
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* ARM Generic Interrupt Controller (GIC) v3 host support
4+
*/
5+
6+
#include <linux/kvm.h>
7+
#include <linux/sizes.h>
8+
#include <asm/kvm.h>
9+
10+
#include "kvm_util.h"
11+
#include "../kvm_util_internal.h"
12+
#include "vgic.h"
13+
14+
/*
15+
* vGIC-v3 default host setup
16+
*
17+
* Input args:
18+
* vm - KVM VM
19+
* nr_vcpus - Number of vCPUs supported by this VM
20+
* gicd_base_gpa - Guest Physical Address of the Distributor region
21+
* gicr_base_gpa - Guest Physical Address of the Redistributor region
22+
*
23+
* Output args: None
24+
*
25+
* Return: GIC file-descriptor or negative error code upon failure
26+
*
27+
* The function creates a vGIC-v3 device and maps the distributor and
28+
* redistributor regions of the guest. Since it depends on the number of
29+
* vCPUs for the VM, it must be called after all the vCPUs have been created.
30+
*/
31+
int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus,
32+
uint64_t gicd_base_gpa, uint64_t gicr_base_gpa)
33+
{
34+
int gic_fd;
35+
uint64_t redist_attr;
36+
struct list_head *iter;
37+
unsigned int nr_gic_pages, nr_vcpus_created = 0;
38+
39+
TEST_ASSERT(nr_vcpus, "Number of vCPUs cannot be empty\n");
40+
41+
/*
42+
* Make sure that the caller is infact calling this
43+
* function after all the vCPUs are added.
44+
*/
45+
list_for_each(iter, &vm->vcpus)
46+
nr_vcpus_created++;
47+
TEST_ASSERT(nr_vcpus == nr_vcpus_created,
48+
"Number of vCPUs requested (%u) doesn't match with the ones created for the VM (%u)\n",
49+
nr_vcpus, nr_vcpus_created);
50+
51+
/* Distributor setup */
52+
gic_fd = kvm_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_V3, false);
53+
kvm_device_access(gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
54+
KVM_VGIC_V3_ADDR_TYPE_DIST, &gicd_base_gpa, true);
55+
nr_gic_pages = vm_calc_num_guest_pages(vm->mode, KVM_VGIC_V3_DIST_SIZE);
56+
virt_map(vm, gicd_base_gpa, gicd_base_gpa, nr_gic_pages);
57+
58+
/* Redistributor setup */
59+
redist_attr = REDIST_REGION_ATTR_ADDR(nr_vcpus, gicr_base_gpa, 0, 0);
60+
kvm_device_access(gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
61+
KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &redist_attr, true);
62+
nr_gic_pages = vm_calc_num_guest_pages(vm->mode,
63+
KVM_VGIC_V3_REDIST_SIZE * nr_vcpus);
64+
virt_map(vm, gicr_base_gpa, gicr_base_gpa, nr_gic_pages);
65+
66+
kvm_device_access(gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
67+
KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true);
68+
69+
return gic_fd;
70+
}

0 commit comments

Comments
 (0)