Skip to contentSkip to conversation

Add arch_timer_edge_cases selftest

PATCH Superseded Colton Lewis 2 participants 7 messages Mar 7, 2024 – Mar 11, 2024

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add arch_timer_edge_cases selftest

Add arch_timer_edge_cases selftest to test various corner cases of the ARM timers such as:

* timers above the max TVAL value
* timers in the past
* moving counters ahead and behind pending timers
* reprograming timers
* timers fired multiple times
* masking/unmasking using the timer control mask

These are intentionally unusual scenarios to stress compliance with the arm architecture.

v4:
* Convert macros to functions where possible
* Remove magic numbers
* Improve naming of variables and types

v3:
https://lore.kernel.org/kvmarm/[email protected]/

v2:
https://lore.kernel.org/kvmarm/[email protected]/

v1:
https://lore.kernel.org/kvm/[email protected]/

Colton Lewis (3):
KVM: arm64: selftests: Standardize GIC base addresses
KVM: arm64: selftests: Guarantee interrupts are handled
KVM: arm64: selftests: Add arch_timer_edge_cases selftest

 tools/testing/selftests/kvm/Makefile          |    1 +
 .../selftests/kvm/aarch64/arch_timer.c        |    8 +-
 .../kvm/aarch64/arch_timer_edge_cases.c       | 1102 +++++++++++++++++
 .../testing/selftests/kvm/aarch64/vgic_irq.c  |   23 +-
 .../kvm/aarch64/vpmu_counter_access.c         |    3 +-
 .../selftests/kvm/dirty_log_perf_test.c       |    5 +-
 .../kvm/include/aarch64/arch_timer.h          |   18 +-
 .../selftests/kvm/include/aarch64/gic.h       |    8 +-
 .../selftests/kvm/include/aarch64/vgic.h      |    3 +-
 tools/testing/selftests/kvm/lib/aarch64/gic.c |   12 +-
 .../testing/selftests/kvm/lib/aarch64/vgic.c  |    7 +-
 11 files changed, 1158 insertions(+), 32 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c

--
2.44.0.278.ge034bb2e1d-goog

#1 lore raw

PATCH v4 1/3 KVM: arm64: selftests: Standardize GIC base addresses

Use default values during GIC initialization and setup to avoid multiple tests needing to decide and declare base addresses for GICD and GICR. Remove the repeated definitions of these addresses across tests.

---
 tools/testing/selftests/kvm/aarch64/arch_timer.c      |  8 ++------
 tools/testing/selftests/kvm/aarch64/vgic_irq.c        | 11 ++---------
 .../selftests/kvm/aarch64/vpmu_counter_access.c       |  3 +--
 tools/testing/selftests/kvm/dirty_log_perf_test.c     |  5 +----
 tools/testing/selftests/kvm/include/aarch64/gic.h     |  5 ++++-
 tools/testing/selftests/kvm/include/aarch64/vgic.h    |  3 ++-
 tools/testing/selftests/kvm/lib/aarch64/gic.c         |  7 ++++++-
 tools/testing/selftests/kvm/lib/aarch64/vgic.c        |  7 ++++++-
 8 files changed, 24 insertions(+), 25 deletions(-)
tools/testing/selftests/kvm/aarch64/arch_timer.c +2 -6
index 2cb8dd1f8275..01c359eea1d6 100644
@@ -59,9 +59,6 @@ static struct test_args test_args = {
 
 #define msecs_to_usecs(msec)		((msec) * 1000LL)
 
-#define GICD_BASE_GPA			0x8000000ULL
-#define GICR_BASE_GPA			0x80A0000ULL
-
 enum guest_stage {
 	GUEST_STAGE_VTIMER_CVAL = 1,
 	GUEST_STAGE_VTIMER_TVAL,
@@ -204,8 +201,7 @@ static void guest_code(void)
 
 	local_irq_disable();
 
-	gic_init(GIC_V3, test_args.nr_vcpus,
-		(void *)GICD_BASE_GPA, (void *)GICR_BASE_GPA);
+	gic_init(GIC_V3, test_args.nr_vcpus);
 
 	timer_set_ctl(VIRTUAL, CTL_IMASK);
 	timer_set_ctl(PHYSICAL, CTL_IMASK);
@@ -391,7 +387,7 @@ static struct kvm_vm *test_vm_create(void)
 		vcpu_init_descriptor_tables(vcpus[i]);
 
 	test_init_timer_irq(vm);
-	gic_fd = vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA);
+	gic_fd = vgic_v3_setup(vm, nr_vcpus, 64);
 	__TEST_REQUIRE(gic_fd >= 0, "Failed to create vgic-v3");
 
 	/* Make all the test's cmdline args visible to the guest */
tools/testing/selftests/kvm/aarch64/vgic_irq.c +2 -9
index 2e64b4856e38..d3bf584d2cc1 100644
@@ -19,9 +19,6 @@
 #include "gic_v3.h"
 #include "vgic.h"
 
-#define GICD_BASE_GPA		0x08000000ULL
-#define GICR_BASE_GPA		0x080A0000ULL
-
 /*
  * Stores the user specified args; it's passed to the guest and to every test
  * function.
@@ -49,9 +46,6 @@ struct test_args {
 #define IRQ_DEFAULT_PRIO	(LOWEST_PRIO - 1)
 #define IRQ_DEFAULT_PRIO_REG	(IRQ_DEFAULT_PRIO << KVM_PRIO_SHIFT) /* 0xf0 */
 
-static void *dist = (void *)GICD_BASE_GPA;
-static void *redist = (void *)GICR_BASE_GPA;
-
 /*
  * The kvm_inject_* utilities are used by the guest to ask the host to inject
  * interrupts (e.g., using the KVM_IRQ_LINE ioctl).
@@ -478,7 +472,7 @@ static void guest_code(struct test_args *args)
 	bool level_sensitive = args->level_sensitive;
 	struct kvm_inject_desc *f, *inject_fns;
 
-	gic_init(GIC_V3, 1, dist, redist);
+	gic_init(GIC_V3, 1);
 
 	for (i = 0; i < nr_irqs; i++)
 		gic_irq_enable(i);
@@ -764,8 +758,7 @@ static void test_vgic(uint32_t nr_irqs, bool level_sensitive, bool eoi_split)
 	memcpy(addr_gva2hva(vm, args_gva), &args, sizeof(args));
 	vcpu_args_set(vcpu, 1, args_gva);
 
-	gic_fd = vgic_v3_setup(vm, 1, nr_irqs,
-			GICD_BASE_GPA, GICR_BASE_GPA);
+	gic_fd = vgic_v3_setup(vm, 1, nr_irqs);
 	__TEST_REQUIRE(gic_fd >= 0, "Failed to create vgic-v3, skipping");
 
 	vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT,
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c +1 -2
index 5f9713364693..7007cc424bf0 100644
@@ -454,8 +454,7 @@ static void create_vpmu_vm(void *guest_code)
 	init.features[0] |= (1 << KVM_ARM_VCPU_PMU_V3);
 	vpmu_vm.vcpu = aarch64_vcpu_add(vpmu_vm.vm, 0, &init, guest_code);
 	vcpu_init_descriptor_tables(vpmu_vm.vcpu);
-	vpmu_vm.gic_fd = vgic_v3_setup(vpmu_vm.vm, 1, 64,
-					GICD_BASE_GPA, GICR_BASE_GPA);
+	vpmu_vm.gic_fd = vgic_v3_setup(vpmu_vm.vm, 1, 64);
 	__TEST_REQUIRE(vpmu_vm.gic_fd >= 0,
 		       "Failed to create vgic-v3, skipping");
 
tools/testing/selftests/kvm/dirty_log_perf_test.c +1 -4
index 504f6fe980e8..61535c4f3405 100644
@@ -22,9 +22,6 @@
 #ifdef __aarch64__
 #include "aarch64/vgic.h"
 
-#define GICD_BASE_GPA			0x8000000ULL
-#define GICR_BASE_GPA			0x80A0000ULL
-
 static int gic_fd;
 
 static void arch_setup_vm(struct kvm_vm *vm, unsigned int nr_vcpus)
@@ -33,7 +30,7 @@ static void arch_setup_vm(struct kvm_vm *vm, unsigned int nr_vcpus)
 	 * The test can still run even if hardware does not support GICv3, as it
 	 * is only an optimization to reduce guest exits.
 	 */
-	gic_fd = vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA);
+	gic_fd = vgic_v3_setup(vm, nr_vcpus, 64);
 }
 
 static void arch_cleanup_vm(struct kvm_vm *vm)
tools/testing/selftests/kvm/include/aarch64/gic.h +4 -1
index b217ea17cac5..9043eaef1076 100644
@@ -16,13 +16,16 @@ enum gic_type {
 #define MIN_SPI			32
 #define MAX_SPI			1019
 #define IAR_SPURIOUS		1023
+#define GICD_BASE_GPA			0x8000000ULL
+#define GICR_BASE_GPA			0x80A0000ULL
 
 #define INTID_IS_SGI(intid)	(0       <= (intid) && (intid) < MIN_PPI)
 #define INTID_IS_PPI(intid)	(MIN_PPI <= (intid) && (intid) < MIN_SPI)
 #define INTID_IS_SPI(intid)	(MIN_SPI <= (intid) && (intid) <= MAX_SPI)
 
-void gic_init(enum gic_type type, unsigned int nr_cpus,
+void _gic_init(enum gic_type type, unsigned int nr_cpus,
 		void *dist_base, void *redist_base);
+void gic_init(enum gic_type type, unsigned int nr_cpus);
 void gic_irq_enable(unsigned int intid);
 void gic_irq_disable(unsigned int intid);
 unsigned int gic_get_and_ack_irq(void);
tools/testing/selftests/kvm/include/aarch64/vgic.h +2 -1
index 0ac6f05c63f9..e116a815964a 100644
@@ -16,8 +16,9 @@
 	((uint64_t)(flags) << 12) | \
 	index)
 
-int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus, uint32_t nr_irqs,
+int _vgic_v3_setup(struct kvm_vm *vm, uint32_t nr_vcpus, uint32_t nr_irqs,
 		uint64_t gicd_base_gpa, uint64_t gicr_base_gpa);
+int vgic_v3_setup(struct kvm_vm *vm, uint32_t nr_vcpus, uint32_t nr_irqs);
 
 #define VGIC_MAX_RESERVED	1023
 
tools/testing/selftests/kvm/lib/aarch64/gic.c +6 -1
index 55668631d546..9d15598d4e34 100644
@@ -49,7 +49,7 @@ gic_dist_init(enum gic_type type, unsigned int nr_cpus, void *dist_base)
 	spin_unlock(&gic_lock);
 }
 
-void gic_init(enum gic_type type, unsigned int nr_cpus,
+void _gic_init(enum gic_type type, unsigned int nr_cpus,
 		void *dist_base, void *redist_base)
 {
 	uint32_t cpu = guest_get_vcpuid();
@@ -63,6 +63,11 @@ void gic_init(enum gic_type type, unsigned int nr_cpus,
 	gic_cpu_init(cpu, redist_base);
 }
 
+void gic_init(enum gic_type type, unsigned int nr_cpus)
+{
+	_gic_init(type, nr_cpus, (void *)GICD_BASE_GPA, (void *)GICR_BASE_GPA);
+}
+
 void gic_irq_enable(unsigned int intid)
 {
 	GUEST_ASSERT(gic_common_ops);
tools/testing/selftests/kvm/lib/aarch64/vgic.c +6 -1
index 184378d593e9..3fdb568a47df 100644
@@ -30,7 +30,7 @@
  * redistributor regions of the guest. Since it depends on the number of
  * vCPUs for the VM, it must be called after all the vCPUs have been created.
  */
-int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus, uint32_t nr_irqs,
+int _vgic_v3_setup(struct kvm_vm *vm, uint32_t nr_vcpus, uint32_t nr_irqs,
 		uint64_t gicd_base_gpa, uint64_t gicr_base_gpa)
 {
 	int gic_fd;
@@ -79,6 +79,11 @@ int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus, uint32_t nr_irqs,
 	return gic_fd;
 }
 
+int vgic_v3_setup(struct kvm_vm *vm, uint32_t nr_vcpus, uint32_t nr_irqs)
+{
+	return _vgic_v3_setup(vm, nr_vcpus, nr_irqs, GICD_BASE_GPA, GICR_BASE_GPA);
+}
+
 /* should only work for level sensitive interrupts */
 int _kvm_irq_set_level_info(int gic_fd, uint32_t intid, int level)
 {
Signature · 1 line
2.44.0.278.ge034bb2e1d-goog
#2 lore raw

PATCH v4 2/3 KVM: arm64: selftests: Guarantee interrupts are handled

In reply to Colton Lewis

Break up the asm instructions poking daifclr and daifset to handle interrupts. R_RBZYL specifies pending interrupts will be handle after context synchronization events such as an ISB.

Introduce a function wrapper for the WFI instruction.

---
 tools/testing/selftests/kvm/aarch64/vgic_irq.c    | 12 ++++++------
 tools/testing/selftests/kvm/include/aarch64/gic.h |  3 +++
 tools/testing/selftests/kvm/lib/aarch64/gic.c     |  5 +++++
 3 files changed, 14 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/aarch64/vgic_irq.c +6 -6
index d3bf584d2cc1..85f182704d79 100644
@@ -269,13 +269,13 @@ static void guest_inject(struct test_args *args,
 	KVM_INJECT_MULTI(cmd, first_intid, num);
 
 	while (irq_handled < num) {
-		asm volatile("wfi\n"
-			     "msr daifclr, #2\n"
-			     /* handle IRQ */
-			     "msr daifset, #2\n"
-			     : : : "memory");
+		gic_wfi();
+		local_irq_enable();
+		isb();
+		/* handle IRQ */
+		local_irq_disable();
 	}
-	asm volatile("msr daifclr, #2" : : : "memory");
+	local_irq_enable();
 
 	GUEST_ASSERT_EQ(irq_handled, num);
 	for (i = first_intid; i < num + first_intid; i++)
tools/testing/selftests/kvm/include/aarch64/gic.h +3 -0
index 9043eaef1076..f474714e4cb2 100644
@@ -47,4 +47,7 @@ void gic_irq_clear_pending(unsigned int intid);
 bool gic_irq_get_pending(unsigned int intid);
 void gic_irq_set_config(unsigned int intid, bool is_edge);
 
+/* Execute a Wait For Interrupt instruction. */
+void gic_wfi(void);
+
 #endif /* SELFTEST_KVM_GIC_H */
tools/testing/selftests/kvm/lib/aarch64/gic.c +5 -0
index 9d15598d4e34..392e3f581ae0 100644
@@ -164,3 +164,8 @@ void gic_irq_set_config(unsigned int intid, bool is_edge)
 	GUEST_ASSERT(gic_common_ops);
 	gic_common_ops->gic_irq_set_config(intid, is_edge);
 }
+
+void gic_wfi(void)
+{
+	asm volatile("wfi");
+}
Signature · 1 line
2.44.0.278.ge034bb2e1d-goog
#3 lore raw

Re: [PATCH v4 2/3] KVM: arm64: selftests: Guarantee interrupts are handled

nitpick: Shortlog should read

KVM: selftests: Ensure pending interrupts are handled in arch_timer test

The fact that we're dealing with pending interrupts is critical here; the ISB has no interaction with the GIC in terms of interrupt timing as it gets to the PE.

Quoted 21 lines · Colton Lewis

Break up the asm instructions poking daifclr and daifset to handle interrupts. R_RBZYL specifies pending interrupts will be handle after context synchronization events such as an ISB.

Introduce a function wrapper for the WFI instruction.

---
 tools/testing/selftests/kvm/aarch64/vgic_irq.c    | 12 ++++++------
 tools/testing/selftests/kvm/include/aarch64/gic.h |  3 +++
 tools/testing/selftests/kvm/lib/aarch64/gic.c     |  5 +++++
 3 files changed, 14 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/aarch64/vgic_irq.c +0 -0
index d3bf584d2cc1..85f182704d79 100644
@@ -269,13 +269,13 @@ static void guest_inject(struct test_args *args,
 	KVM_INJECT_MULTI(cmd, first_intid, num);
 
 	while (irq_handled < num) {

tools/testing/selftests/kvm/aarch64/vgic_irq.c static void guest_inject(struct test_args *args, · line 276

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +5 -5
-		asm volatile("wfi\n"
-			     "msr daifclr, #2\n"
-			     /* handle IRQ */
-			     "msr daifset, #2\n"
-			     : : : "memory");
+		gic_wfi();
+		local_irq_enable();
+		isb();
+		/* handle IRQ */
+		local_irq_disable();

Sorry, this *still* annoys me. Please move the comment above the ISB, you're documenting a behavior that is implied by the instruction, not anything else.

Quoted 23 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +1 -1
 	}
-	asm volatile("msr daifclr, #2" : : : "memory");
+	local_irq_enable();
 
 	GUEST_ASSERT_EQ(irq_handled, num);
 	for (i = first_intid; i < num + first_intid; i++)
tools/testing/selftests/kvm/include/aarch64/gic.h +3 -0
index 9043eaef1076..f474714e4cb2 100644
@@ -47,4 +47,7 @@ void gic_irq_clear_pending(unsigned int intid);
 bool gic_irq_get_pending(unsigned int intid);
 void gic_irq_set_config(unsigned int intid, bool is_edge);
 
+/* Execute a Wait For Interrupt instruction. */
+void gic_wfi(void);
+
 #endif /* SELFTEST_KVM_GIC_H */
tools/testing/selftests/kvm/lib/aarch64/gic.c +0 -0
index 9d15598d4e34..392e3f581ae0 100644

tools/testing/selftests/kvm/lib/aarch64/gic.c void gic_irq_set_config(unsigned int intid, bool is_edge) · line 171

Quoted 8 lines
tools/testing/selftests/kvm/lib/aarch64/gic.c +5 -0
@@ -164,3 +164,8 @@ void gic_irq_set_config(unsigned int intid, bool is_edge)
 	GUEST_ASSERT(gic_common_ops);
 	gic_common_ops->gic_irq_set_config(intid, is_edge);
 }
+
+void gic_wfi(void)
+{
+	asm volatile("wfi");
+}

Ok, I left a comment about this last time...

WFI instructions are only relevant in the context of a PE, so it would be natural to add such a helper to aarch64/processor.h. There are definitely implementations out there that do not use a GIC and still have WFI instructions.

Signature · 2 lines
Thanks,
Oliver
#4 lore raw

Re: [PATCH v4 2/3] KVM: arm64: selftests: Guarantee interrupts are handled

Quoted 7 lines · Oliver Upton

nitpick: Shortlog should read

KVM: selftests: Ensure pending interrupts are handled in arch_timer test

The fact that we're dealing with pending interrupts is critical here; the ISB has no interaction with the GIC in terms of interrupt timing as it gets to the PE.

I'll change that.

Quoted 72 lines · Colton Lewis
Quoted 21 lines

Break up the asm instructions poking daifclr and daifset to handle interrupts. R_RBZYL specifies pending interrupts will be handle after context synchronization events such as an ISB.

Introduce a function wrapper for the WFI instruction.

---
  tools/testing/selftests/kvm/aarch64/vgic_irq.c    | 12 ++++++------
  tools/testing/selftests/kvm/include/aarch64/gic.h |  3 +++
  tools/testing/selftests/kvm/lib/aarch64/gic.c     |  5 +++++
  3 files changed, 14 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/aarch64/vgic_irq.c +0 -0
index d3bf584d2cc1..85f182704d79 100644
@@ -269,13 +269,13 @@ static void guest_inject(struct test_args *args,
  	KVM_INJECT_MULTI(cmd, first_intid, num);
 
  	while (irq_handled < num) {

tools/testing/selftests/kvm/aarch64/vgic_irq.c static void guest_inject(struct test_args *args, · line 276

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +5 -5
-		asm volatile("wfi\n"
-			     "msr daifclr, #2\n"
-			     /* handle IRQ */
-			     "msr daifset, #2\n"
-			     : : : "memory");
+		gic_wfi();
+		local_irq_enable();
+		isb();
+		/* handle IRQ */
+		local_irq_disable();

Sorry, this *still* annoys me. Please move the comment above the ISB, you're documenting a behavior that is implied by the instruction, not anything else.

Quoted 23 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +1 -1
  	}
-	asm volatile("msr daifclr, #2" : : : "memory");
+	local_irq_enable();
 
  	GUEST_ASSERT_EQ(irq_handled, num);
  	for (i = first_intid; i < num + first_intid; i++)
tools/testing/selftests/kvm/include/aarch64/gic.h +3 -0
index 9043eaef1076..f474714e4cb2 100644
@@ -47,4 +47,7 @@ void gic_irq_clear_pending(unsigned int intid);
  bool gic_irq_get_pending(unsigned int intid);
  void gic_irq_set_config(unsigned int intid, bool is_edge);
 
+/* Execute a Wait For Interrupt instruction. */
+void gic_wfi(void);
+
  #endif /* SELFTEST_KVM_GIC_H */
tools/testing/selftests/kvm/lib/aarch64/gic.c +0 -0
index 9d15598d4e34..392e3f581ae0 100644

tools/testing/selftests/kvm/lib/aarch64/gic.c void gic_irq_set_config(unsigned int intid, bool · line 171

Quoted 9 lines
tools/testing/selftests/kvm/lib/aarch64/gic.c +5 -0
@@ -164,3 +164,8 @@ void gic_irq_set_config(unsigned int intid, bool  
 is_edge)
  	GUEST_ASSERT(gic_common_ops);
  	gic_common_ops->gic_irq_set_config(intid, is_edge);
  }
+
+void gic_wfi(void)
+{
+	asm volatile("wfi");
+}

Ok, I left a comment about this last time...

Quoted 4 lines

WFI instructions are only relevant in the context of a PE, so it would be natural to add such a helper to aarch64/processor.h. There are definitely implementations out there that do not use a GIC and still have WFI instructions.

Apologies if I missed it. I'll remove gic_from the name.

#5 lore raw

Re: [PATCH v4 2/3] KVM: arm64: selftests: Guarantee interrupts are handled

In reply to Oliver Upton
Quoted 6 lines · Oliver Upton
Quoted 6 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +1 -5
-		asm volatile("wfi\n"
-			     "msr daifclr, #2\n"
-			     /* handle IRQ */
-			     "msr daifset, #2\n"
-			     : : : "memory");
+		gic_wfi();
Quoted 8 lines

tools/testing/selftests/kvm/aarch64/vgic_irq.c static void guest_inject(struct test_args *args, · line 276

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +4 -0
+		local_irq_enable();
+		isb();
+		/* handle IRQ */
+		local_irq_disable();

Sorry, this *still* annoys me. Please move the comment above the ISB, you're documenting a behavior that is implied by the instruction, not anything else.

Fixing this too. I changed this in the third patch but forgot there was also an instance in the second.

#6 lore raw

PATCH v4 3/3 KVM: arm64: selftests: Add arch_timer_edge_cases selftest

In reply to Colton Lewis

Add a new arch_timer_edge_cases selftests that validates:

- timers above the max TVAL value
- timers in the past
- moving counters ahead and behind pending timers
- reprograming timers
- timers fired multiple times
- masking/unmasking using the timer control mask

These are intentionally unusual scenarios to stress compliance with the arm architecture.

---
 tools/testing/selftests/kvm/Makefile          |    1 +
 .../kvm/aarch64/arch_timer_edge_cases.c       | 1102 +++++++++++++++++
 .../kvm/include/aarch64/arch_timer.h          |   18 +-
 3 files changed, 1120 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c
tools/testing/selftests/kvm/Makefile +1 -0
index 492e937fab00..fd9468d19ba4 100644
@@ -144,6 +144,7 @@ TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test
 
 TEST_GEN_PROGS_aarch64 += aarch64/aarch32_id_regs
 TEST_GEN_PROGS_aarch64 += aarch64/arch_timer
+TEST_GEN_PROGS_aarch64 += aarch64/arch_timer_edge_cases
 TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions
 TEST_GEN_PROGS_aarch64 += aarch64/hypercalls
 TEST_GEN_PROGS_aarch64 += aarch64/page_fault_test
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +1102 -0

1,102 lines not on this page · raw message

tools/testing/selftests/kvm/include/aarch64/arch_timer.h +17 -1
index b3e97525cb55..bf461de34785 100644
@@ -79,7 +79,7 @@ static inline uint64_t timer_get_cval(enum arch_timer timer)
 	return 0;
 }
 
-static inline void timer_set_tval(enum arch_timer timer, uint32_t tval)
+static inline void timer_set_tval(enum arch_timer timer, int32_t tval)
 {
 	switch (timer) {
 	case VIRTUAL:
@@ -95,6 +95,22 @@ static inline void timer_set_tval(enum arch_timer timer, uint32_t tval)
 	isb();
 }
 
+static inline int32_t timer_get_tval(enum arch_timer timer)
+{
+	isb();
+	switch (timer) {
+	case VIRTUAL:
+		return read_sysreg(cntv_tval_el0);
+	case PHYSICAL:
+		return read_sysreg(cntp_tval_el0);
+	default:
+		GUEST_FAIL("Could not get timer %d\n", timer);
+	}
+
+	/* We should not reach here */
+	return 0;
+}
+
 static inline void timer_set_ctl(enum arch_timer timer, uint32_t ctl)
 {
 	switch (timer) {
Signature · 1 line
2.44.0.278.ge034bb2e1d-goog