Skip to contentSkip to conversation

Add arch_timer_edge_cases selftest

PATCH Superseded Colton Lewis 2 participants 10 messages Nov 3, 2023 – Dec 8, 2023

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 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.

v3:
* Rebase to v6.6
* Patch to standardize GIC base addresses across tests
* Patch to guarantee interrupt handling in vgic_irq with context sync, fixing an error there that was caught here
* Expand sync_cmd enum for more logical arguments to waiting functions

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       | 1104 +++++++++++++++++
 .../testing/selftests/kvm/aarch64/vgic_irq.c  |   23 +-
 .../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 +-
 10 files changed, 1159 insertions(+), 30 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c

--
2.42.0.869.gea05f2083d-goog

#1 lore raw

PATCH v3 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 ++---------
 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 ++++++-
 7 files changed, 23 insertions(+), 23 deletions(-)
tools/testing/selftests/kvm/aarch64/arch_timer.c +2 -6
index 274b8465b42a..f5101898c46a 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/dirty_log_perf_test.c +1 -4
index d374dbcf9a53..53c9d7a2611d 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 b5f28d21a947..7cd5be5351c8 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.42.0.869.gea05f2083d-goog
#2 lore raw

Re: [PATCH v3 1/3] KVM: arm64: selftests: Standardize GIC base addresses

Quoted 6 lines · Colton Lewis

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.

<snip>

Quoted 6 lines
tools/testing/selftests/kvm/lib/aarch64/gic.c +1 -1
-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();

tools/testing/selftests/kvm/lib/aarch64/gic.c void gic_init(enum gic_type type, unsigned int nr_cpus, · line 70

Quoted 8 lines
tools/testing/selftests/kvm/lib/aarch64/gic.c +5 -0
@@ -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);
+}
+

</snip>

Quoted 6 lines
tools/testing/selftests/kvm/lib/aarch64/vgic.c +1 -1
-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)
 {
tools/testing/selftests/kvm/lib/aarch64/vgic.c +0 -0
 	int gic_fd;

tools/testing/selftests/kvm/lib/aarch64/vgic.c int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus, uint32_t nr_irqs, · line 86

Quoted 8 lines
tools/testing/selftests/kvm/lib/aarch64/vgic.c +5 -0
@@ -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);
+}
+

What's the point of having the internal implementations of these functions that still take addresses? If we're standardizing GIC placement then there's no need for allowing the caller to provide different addresses.

Signature · 2 lines
Thanks,
Oliver
#3 lore raw

Re: [PATCH v3 1/3] KVM: arm64: selftests: Standardize GIC base addresses

Quoted 40 lines · Oliver Upton
Quoted 6 lines · Colton Lewis

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.

<snip>

Quoted 6 lines
tools/testing/selftests/kvm/lib/aarch64/gic.c +1 -1
-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();

tools/testing/selftests/kvm/lib/aarch64/gic.c void gic_init(enum gic_type type, unsigned int · line 71

Quoted 9 lines
tools/testing/selftests/kvm/lib/aarch64/gic.c +5 -0
@@ -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);
+}
+

</snip>

Quoted 11 lines
(quoted diff) +1 -1
-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;

tools/testing/selftests/kvm/lib/aarch64/vgic.c +0 -0
@@ -79,6 +79,11 @@ int vgic_v3_setup(struct kvm_vm *vm, unsigned int  
 nr_vcpus, uint32_t nr_irqs,
  	return gic_fd;
  }
Quoted 12 lines

Review int vgic_v3_setup(struct kvm_vm *vm, unsigned int · line 88

Quoted 7 lines
(quoted diff) +5 -0
+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);
+}
+

What's the point of having the internal implementations of these functions that still take addresses? If we're standardizing GIC placement then there's no need for allowing the caller to provide different addresses.

I wasn't sure if there might be a reason for that allowance I was unaware of since that's what the original interface was so I erred on the side of flexibility. I'll delete it.

#4 lore raw

PATCH v3 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");
+}

--
2.42.0.869.gea05f2083d-goog

#5 lore raw

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

Hi Colton,

Quoted 7 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.

What's missing from the changelog is that you've spotted an actual bug, and this is really a bugfix.

Quoted 13 lines
---
 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);
 

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +4 -5
 	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 */

nit: this comment should appear above the isb()

Quoted 12 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +2 -1
+		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 +0 -0
index 9043eaef1076..f474714e4cb2 100644

tools/testing/selftests/kvm/include/aarch64/gic.h void gic_irq_clear_pending(unsigned int intid); · line 52

Quoted 6 lines
tools/testing/selftests/kvm/include/aarch64/gic.h +3 -0
@@ -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);
+

WFIs have nothing to do with the GIC, they're an instruction supported by the CPU. I'd just merge the definition and declaration into the header while you're at it.

So, could you throw something like this in aarch64/processor.h?

static inline void wfi(void)

{
	asm volatile("wfi");
}
Signature · 2 lines
Thanks,
Oliver
#6 lore raw

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

Quoted 7 lines · Oliver Upton

Hi Colton,

Quoted 3 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.

Quoted 6 lines
Quoted 3 lines

Introduce a function wrapper for the WFI instruction.

What's missing from the changelog is that you've spotted an actual bug, and this is really a bugfix.

I will update the changelog mentioning this is a bugfix.

Quoted 13 lines
Quoted 12 lines
---
  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);
Quoted 12 lines

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +4 -5
  	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 */

nit: this comment should appear above the isb()

I put it there because the manual specifies pending interrupts are handled before the first instruction *after* the context sync, but I can see why to put it above.

Quoted 23 lines
Quoted 12 lines
tools/testing/selftests/kvm/aarch64/vgic_irq.c +2 -1
+		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 +0 -0
index 9043eaef1076..f474714e4cb2 100644

tools/testing/selftests/kvm/include/aarch64/gic.h void gic_irq_clear_pending(unsigned int intid); · line 52

Quoted 6 lines
tools/testing/selftests/kvm/include/aarch64/gic.h +3 -0
@@ -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);
+

WFIs have nothing to do with the GIC, they're an instruction supported by the CPU. I'd just merge the definition and declaration into the header while you're at it.

Quoted 6 lines

So, could you throw something like this in aarch64/processor.h?

static inline void wfi(void)

{
	asm volatile("wfi");
}

Will do.

#7 lore raw

PATCH v3 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       | 1104 +++++++++++++++++
 .../kvm/include/aarch64/arch_timer.h          |   18 +-
 3 files changed, 1122 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c
tools/testing/selftests/kvm/Makefile +1 -0
index a3bb36fb3cfc..e940b7c6b818 100644
@@ -141,6 +141,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 +1104 -0

1,104 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) {

--
2.42.0.869.gea05f2083d-goog

#8 lore raw

Re: [PATCH v3 3/3] KVM: arm64: selftests: Add arch_timer_edge_cases selftest

Quoted 17 lines · Colton Lewis
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +17 -0
+struct test_args {
+	/* Virtual or physical timer and counter tests. */
+	enum arch_timer timer;
+	/* Delay used for most timer tests. */
+	uint64_t wait_ms;
+	/* Delay used in the test_long_timer_delays test. */
+	uint64_t long_wait_ms;
+	/* Number of iterations. */
+	int iterations;
+	/* Whether to test the physical timer. */
+	bool test_physical;
+	/* Whether to test the virtual timer. */
+	bool test_virtual;
+};
+
+struct test_args test_args = {
+	.wait_ms = WAIT_TEST_MS,

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 90

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+	.long_wait_ms = LONG_WAIT_TEST_MS,
+	.iterations = NR_TEST_ITERS_DEF,
+	.test_physical = true,
+	.test_virtual = true,
+};
+
+static int vtimer_irq, ptimer_irq;
+
+enum sync_cmd {
+	SET_REG_KVM_REG_ARM_TIMER_CNT = 100001,

nit: Why not call it SET_COUNTER_VALUE? Also, what's the reason for the magic starting value here?

Quoted 29 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +29 -0
+	USERSPACE_USLEEP,
+	USERSPACE_SCHED_YIELD,
+	USERSPACE_MIGRATE_SELF,
+	NO_USERSPACE_CMD,
+};
+
+typedef void (*sleep_method_t)(enum arch_timer timer, uint64_t usec);
+
+static void sleep_poll(enum arch_timer timer, uint64_t usec);
+static void sleep_sched_poll(enum arch_timer timer, uint64_t usec);
+static void sleep_in_userspace(enum arch_timer timer, uint64_t usec);
+static void sleep_migrate(enum arch_timer timer, uint64_t usec);
+
+sleep_method_t sleep_method[] = {
+	sleep_poll,
+	sleep_sched_poll,
+	sleep_migrate,
+	sleep_in_userspace,
+};
+
+typedef void (*wfi_method_t)(void);
+
+static void wait_for_non_spurious_irq(void);
+static void wait_poll_for_irq(void);
+static void wait_sched_poll_for_irq(void);
+static void wait_migrate_poll_for_irq(void);
+
+wfi_method_t wfi_method[] = {
+	wait_for_non_spurious_irq,

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 129

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+	wait_poll_for_irq,
+	wait_sched_poll_for_irq,
+	wait_migrate_poll_for_irq,
+};
+
+#define for_each_wfi_method(i)							\
+	for ((i) = 0; (i) < ARRAY_SIZE(wfi_method); (i)++)
+
+#define for_each_sleep_method(i)						\
+	for ((i) = 0; (i) < ARRAY_SIZE(sleep_method); (i)++)

I don't see a tremendous amount of value in using iterators for these arrays, especially since the caller is still directly referencing the underlying arrays to get at the element anyway.

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 132

Quoted 2 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +2 -0
+enum timer_view {
+	TIMER_CVAL = 1,

Again, when I read an enumeration with an explicit starting value, I assume that there is a functional reason for it.

Quoted 15 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +15 -0
+	TIMER_TVAL,
+};
+
+#define ASSERT_IRQS_HANDLED(_nr, _args...) do {				\
+		int _h = atomic_read(&shared_data.handled);		\
+		__GUEST_ASSERT(_h == (_nr), "Handled %d IRQS but expected %d", _h, _nr, ##_args); \
+	} while (0)
+
+#define GUEST_SYNC_CLOCK(__cmd, __val)						\
+	GUEST_SYNC_ARGS(__cmd, __val, 0, 0, 0)
+
+#define USERSPACE_CMD(__cmd)							\
+	GUEST_SYNC_ARGS(__cmd, 0, 0, 0, 0)
+
+#define USERSPACE_SCHEDULE()							\

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 157

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+	USERSPACE_CMD(USERSPACE_SCHED_YIELD)
+
+#define USERSPACE_MIGRATE_VCPU()						\
+	USERSPACE_CMD(USERSPACE_MIGRATE_SELF)
+
+#define SLEEP_IN_USERSPACE(__usecs)						\
+	GUEST_SYNC_ARGS(USERSPACE_USLEEP, (__usecs), 0, 0, 0)
+
+#define IAR_SPURIOUS		1023
+

Isn't this already defined in gic.h?

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 161

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +4 -0
+static void set_counter(enum arch_timer timer, uint64_t counter)
+{
+	GUEST_SYNC_ARGS(SET_REG_KVM_REG_ARM_TIMER_CNT, counter, timer, 0, 0);
+}

Why do some of the ucall helpers use macros and this one is done as a function? You should avoid using macros unless you're actually doing something valuable with the preprocessor.

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 172

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+static uint32_t next_pcpu(void)
+{
+	uint32_t max = get_nprocs();
+	uint32_t cur = sched_getcpu();
+	uint32_t next = cur;
+	cpu_set_t cpuset;
+
+	TEST_ASSERT(max > 1, "Need at least two physical cpus");
+
+	sched_getaffinity(getpid(), sizeof(cpuset), &cpuset);

Can you just pass 0 here for the pid? Forgive me if I'm getting my wires crossed, but isn't this going to return the last cpuset you've written in migrate_self()? In that case it would seem you'll always select the same CPU.

Also, it would seem that the test isn't pinning to a particular CPU in the beginning. In that case CPU migrations can happen _at any time_ and are not being precisely controlled by the test. Is this intentional?

Quoted 7 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +7 -0
+/*
+ * Should be called with IRQs masked.
+ *
+ * Note that this can theoretically hang forever, so we rely on having
+ * a timeout mechanism in the "runner", like:
+ * tools/testing/selftests/kselftest/runner.sh.
+ */

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 266

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+static void wait_for_non_spurious_irq(void)
+{
+	int h;
+
+	for (h = atomic_read(&shared_data.handled); h == atomic_read(&shared_data.handled);) {
+		gic_wfi();
+		local_irq_enable();
+		isb();
+		/* handle IRQ */
+		local_irq_disable();

Same nitpick about comment placement here. The isb *is* the context synchronization event that precipitates the imaginary window where pending interrupts are taken.

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 272

Quoted 6 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +6 -0
+	}
+}
+
+/*
+ * Wait for an non-spurious IRQ by polling in the guest (userspace=0) or in
+ * userspace (e.g., userspace=1 and userspace_cmd=USERSPACE_SCHED_YIELD).
		       ^~~~~~~~~~~

More magic values. What is this?

Quoted 11 lines

+ * Should be called with IRQs masked. Not really needed like the wfi above, but
+ * it should match the others.
+ *
+ * Note that this can theoretically hang forever, so we rely on having
+ * a timeout mechanism in the "runner", like:
+ * tools/testing/selftests/kselftest/runner.sh.
+ */

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +4 -0
+static void poll_for_non_spurious_irq(enum sync_cmd userspace_cmd)
+{
+	int h;
+

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 294

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+	h = atomic_read(&shared_data.handled);
+
+	local_irq_enable();
+	while (h == atomic_read(&shared_data.handled)) {
+		if (userspace_cmd == NO_USERSPACE_CMD)
+			cpu_relax();
+		else
+			USERSPACE_CMD(userspace_cmd);
+	}
+	local_irq_disable();

cpu_relax(), or rather the yield instruction, is not a context synchronization event.

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 438

Quoted 8 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +8 -0
+/* Test masking/unmasking a timer using the timer mask (not the IRQ mask). */
+static void test_timer_control_mask_then_unmask(enum arch_timer timer)
+{
+	reset_timer_state(timer, DEF_CNT);
+	set_tval_irq(timer, -1, CTL_ENABLE | CTL_IMASK);
+
+	/* No IRQs because the timer is still masked. */
+	ASSERT_IRQS_HANDLED(0);

This seems to assume both the timer hardware and GIC are capable of getting the interrupt to the CPU in just a few cycles.

Oh wait, that's exactly what test_timer_control_masks() is doing... What's the point of this then?

Quoted 27 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +27 -0
+	/* Unmask the timer, and then get an IRQ. */
+	local_irq_disable();
+	timer_set_ctl(timer, CTL_ENABLE);
+	wait_for_non_spurious_irq();
+
+	ASSERT_IRQS_HANDLED(1);
+	local_irq_enable();
+}
+
+/* Check that timer control masks actually mask a timer being fired. */
+static void test_timer_control_masks(enum arch_timer timer)
+{
+	reset_timer_state(timer, DEF_CNT);
+
+	/* Local IRQs are not masked at this point. */
+
+	set_tval_irq(timer, -1, CTL_ENABLE | CTL_IMASK);
+
+	/* Assume no IRQ after waiting TIMEOUT_NO_IRQ_US microseconds */
+	sleep_poll(timer, TIMEOUT_NO_IRQ_US);
+
+	ASSERT_IRQS_HANDLED(0);
+	timer_set_ctl(timer, CTL_IMASK);
+}
+
+static void test_fire_a_timer_multiple_times(enum arch_timer timer,
+					     wfi_method_t wm, int num)

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 476

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+{
+	int i;
+
+	local_irq_disable();
+	reset_timer_state(timer, DEF_CNT);
+
+	set_tval_irq(timer, 0, CTL_ENABLE);
+
+	for (i = 1; i <= num; i++) {
+		wm();

wfi_method_t is such a misnomer. Critically, it masks/unmasks IRQs which is rather hard to remember once you're this deep into the test code. At least having some comments on what wfi_method_t is expected to do would help a bit.

Quoted 22 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +22 -0
+		/* The IRQ handler masked and disabled the timer.
+		 * Enable and unmmask it again.
+		 */
+		timer_set_ctl(timer, CTL_ENABLE);
+
+		ASSERT_IRQS_HANDLED(i);
+	}
+
+	local_irq_enable();
+}
+
+static void test_timers_fired_multiple_times(enum arch_timer timer)
+{
+	int i;
+
+	for_each_wfi_method(i)
+		test_fire_a_timer_multiple_times(timer, wfi_method[i], 10);
+}
+
+/*
+ * Set a timer for tval=d_1_ms then reprogram it to tval=d_2_ms. Check that we
+ * get the timer fired. There is no timeout for the wait: we use the wfi

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 509

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+ * instruction.
+ */
+static void test_reprogramming_timer(enum arch_timer timer, wfi_method_t wm,
+				     int32_t d_1_ms, int32_t d_2_ms)
+{
+	local_irq_disable();
+	reset_timer_state(timer, DEF_CNT);
+
+	/* Program the timer to DEF_CNT + d_1_ms. */
+	set_tval_irq(timer, msec_to_cycles(d_1_ms), CTL_ENABLE);

This assumes that the program doesn't get preempted for @d_1_ms here, right?

Quoted 21 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +21 -0
+	/* Reprogram the timer to DEF_CNT + d_2_ms. */
+	timer_set_tval(timer, msec_to_cycles(d_2_ms));
+
+	wm();
+
+	/* The IRQ should arrive at DEF_CNT + d_2_ms (or after). */
+	GUEST_ASSERT(timer_get_cntct(timer) >=
+		     DEF_CNT + msec_to_cycles(d_2_ms));
+
+	local_irq_enable();
+	ASSERT_IRQS_HANDLED(1, wm);
+};
+
+/*
+ * Set a timer for tval=d_1_ms then reprogram it to tval=d_2_ms. Check
+ * that we get the timer fired in d_2_ms.
+ */
+static void test_reprogramming_timer_with_timeout(enum arch_timer timer,
+						  sleep_method_t guest_sleep,
+						  int32_t d_1_ms,
+						  int32_t d_2_ms)

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 541

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+{
+	local_irq_disable();
+	reset_timer_state(timer, DEF_CNT);
+
+	set_tval_irq(timer, msec_to_cycles(d_1_ms), CTL_ENABLE);
+
+	/* Reprogram the timer. */
+	timer_set_tval(timer, msec_to_cycles(d_2_ms));
+
+	guest_sleep(timer, msecs_to_usecs(d_2_ms) + TEST_MARGIN_US);

Even more magic values. What is the difference between TEST_MARGIN_US and TIMEOUT_NO_IRQ_US?

Quoted 5 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +5 -0
+	local_irq_enable();
+	isb();
+	ASSERT_IRQS_HANDLED(1);
+};
+

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 557

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+static void test_reprogram_timers(enum arch_timer timer)
+{
+	int i;
+	uint64_t base_wait = test_args.wait_ms;
+
+	for_each_wfi_method(i) {
+		test_reprogramming_timer(timer, wfi_method[i], 2 * base_wait,
+					 base_wait);
+		test_reprogramming_timer(timer, wfi_method[i], base_wait,
+					 2 * base_wait);

What is the value of changing around the two timer deltas? It is entirely unclear from reading this what potential edge case it is detecting.

Quoted 15 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +15 -0
+/*
+ * This test checks basic timer behavior without actually firing timers, things
+ * like: the relationship between cval and tval, tval down-counting.
+ */
+static void timers_sanity_checks(enum arch_timer timer, bool use_sched)
+{
+	reset_timer_state(timer, DEF_CNT);
+
+	local_irq_disable();
+
+	/* cval in the past */
+	timer_set_cval(timer,
+		       timer_get_cntct(timer) -
+		       msec_to_cycles(test_args.wait_ms));
+	if (use_sched)

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 608

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+		USERSPACE_MIGRATE_VCPU();
+	GUEST_ASSERT(timer_get_tval(timer) < 0);
+
+	/* tval in the past */
+	timer_set_tval(timer, -1);
+	if (use_sched)
+		USERSPACE_MIGRATE_VCPU();
+	GUEST_ASSERT(timer_get_cval(timer) < timer_get_cntct(timer));
+
+	/* tval larger than TVAL_MAX. */

Isn't this programming CVAL with a delta larger than what can be expressed in TVAL?

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 619

Quoted 11 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +11 -0
+	timer_set_cval(timer,
+		       timer_get_cntct(timer) + TVAL_MAX +
+		       msec_to_cycles(test_args.wait_ms));
+	if (use_sched)
+		USERSPACE_MIGRATE_VCPU();
+	GUEST_ASSERT(timer_get_tval(timer) <= 0);
+
+	/*
+	 * tval larger than 2 * TVAL_MAX.
+	 * Twice the TVAL_MAX completely loops around the TVAL.
+	 */

Same here. The comment calls it TVAL, but the test is programming CVAL.

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +4 -0
+	timer_set_cval(timer,
+		       timer_get_cntct(timer) + 2ULL * TVAL_MAX +
+		       msec_to_cycles(test_args.wait_ms));
+	if (use_sched)

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 633

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+		USERSPACE_MIGRATE_VCPU();
+	GUEST_ASSERT(timer_get_tval(timer) <=
+		       msec_to_cycles(test_args.wait_ms));
+
+	/* negative tval that rollovers from 0. */
+	set_counter(timer, msec_to_cycles(1));
+	timer_set_tval(timer, -1 * msec_to_cycles(test_args.wait_ms));
+	if (use_sched)
+		USERSPACE_MIGRATE_VCPU();
+	GUEST_ASSERT(timer_get_cval(timer) >= (CVAL_MAX - msec_to_cycles(9)));
					      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm lost. What is the significance of this expression?

I'm pretty sure the architecture allows implementers to size the CVAL register according to the number of implemented bits in the counter. I don't see how the case of hardware truncating the MSBs of the register is handled here.

Looks like there's quite a lot more of this code I haven't gotten to, but I've reached a stopping point and need to work on some other things.

Signature · 2 lines
Thanks,
Oliver
#9 lore raw

Re: [PATCH v3 3/3] KVM: arm64: selftests: Add arch_timer_edge_cases selftest

Quoted 6 lines · Oliver Upton

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 90

Quoted 2 lines · Colton Lewis
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +2 -0
+enum sync_cmd {
+	SET_REG_KVM_REG_ARM_TIMER_CNT = 100001,

nit: Why not call it SET_COUNTER_VALUE? Also, what's the reason for the magic starting value here?

No reason I'm aware of other than it was that way before I took over.

Quoted 9 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 129

Quoted 5 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +5 -0
+#define for_each_wfi_method(i)							\
+	for ((i) = 0; (i) < ARRAY_SIZE(wfi_method); (i)++)
+
+#define for_each_sleep_method(i)						\
+	for ((i) = 0; (i) < ARRAY_SIZE(sleep_method); (i)++)

I don't see a tremendous amount of value in using iterators for these arrays, especially since the caller is still directly referencing the underlying arrays to get at the element anyway.

Easy enough to change.

Quoted 5 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 132

Quoted 2 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +2 -0
+enum timer_view {
+	TIMER_CVAL = 1,

Again, when I read an enumeration with an explicit starting value, I assume that there is a functional reason for it.

Not this time. I'll change it.

Quoted 4 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 157

Quoted 2 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +2 -0
+#define IAR_SPURIOUS		1023
+

Isn't this already defined in gic.h?

It is.

Quoted 8 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 161

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +4 -0
+static void set_counter(enum arch_timer timer, uint64_t counter)
+{
+	GUEST_SYNC_ARGS(SET_REG_KVM_REG_ARM_TIMER_CNT, counter, timer, 0, 0);
+}

Why do some of the ucall helpers use macros and this one is done as a function? You should avoid using macros unless you're actually doing something valuable with the preprocessor.

I agree that's an inconsistency and it would be better to make them all functions.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +7 -0
+static uint32_t next_pcpu(void)
+{
+	uint32_t max = get_nprocs();
+	uint32_t cur = sched_getcpu();
+	uint32_t next = cur;
+	cpu_set_t cpuset;
+
Quoted 8 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 172

Quoted 3 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +3 -0
+	TEST_ASSERT(max > 1, "Need at least two physical cpus");
+
+	sched_getaffinity(getpid(), sizeof(cpuset), &cpuset);

Can you just pass 0 here for the pid? Forgive me if I'm getting my wires crossed, but isn't this going to return the last cpuset you've written in migrate_self()? In that case it would seem you'll always select the same CPU.

I checked and I can pass 0. You are right that it will select the last cpuset written in migrate_self, but that is not always the same. next_pcpu() uses the current cpuset to calculate the next cpu to migrate to (current scheme just rotates through all cpus in ascending order). To migrate, the test calls migrate_self(next_pcpu()), so it should advance the cpuset to the next cpu every time migrate_self is called.

Quoted 3 lines

Also, it would seem that the test isn't pinning to a particular CPU in the beginning. In that case CPU migrations can happen _at any time_ and are not being precisely controlled by the test. Is this intentional?

This appears to be an oversight on my part. I did have a pin at the beginning before but it must have been deleted when I shuffled some code around.

Quoted 9 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 266

Quoted 5 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +5 -0
+		gic_wfi();
+		local_irq_enable();
+		isb();
+		/* handle IRQ */
+		local_irq_disable();

Same nitpick about comment placement here. The isb *is* the context synchronization event that precipitates the imaginary window where pending interrupts are taken.

Strictly speaking by ARM ref manual (DDI 0487J.a D1.3.6 table entry for R_RBZYL), "interrupt is taken before the first instruction **after** context synchronization event".

But I don't mind just putting the comment to the right of the isb.

Quoted 7 lines
Quoted 4 lines

+ * Wait for an non-spurious IRQ by polling in the guest (userspace=0) or in
+ * userspace (e.g., userspace=1 and userspace_cmd=USERSPACE_SCHED_YIELD).

		       ^~~~~~~~~~~

More magic values. What is this?

In this case it's 1 and 0 representing true and false. Seems like a standard Cism to me but I can make it more clear.

Quoted 11 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 294

Quoted 8 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +8 -0
+	local_irq_enable();
+	while (h == atomic_read(&shared_data.handled)) {
+		if (userspace_cmd == NO_USERSPACE_CMD)
+			cpu_relax();
+		else
+			USERSPACE_CMD(userspace_cmd);
+	}
+	local_irq_disable();

cpu_relax(), or rather the yield instruction, is not a context synchronization event.

I agree it's not, but I don't think it matters. We are just waiting for an interrupt. Context will catch up eventually.

Quoted 13 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 438

Quoted 9 lines

+/* Test masking/unmasking a timer using the timer mask (not the IRQ mask). */

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +7 -0
+static void test_timer_control_mask_then_unmask(enum arch_timer timer)
+{
+	reset_timer_state(timer, DEF_CNT);
+	set_tval_irq(timer, -1, CTL_ENABLE | CTL_IMASK);
+
+	/* No IRQs because the timer is still masked. */
+	ASSERT_IRQS_HANDLED(0);

This seems to assume both the timer hardware and GIC are capable of getting the interrupt to the CPU in just a few cycles.

Quoted 2 lines

Oh wait, that's exactly what test_timer_control_masks() is doing... What's the point of this then?

I agree the point of this test is something else so the ASSERT_IRQS_HANDLED(0) is duplicating test_timer_control_masks

Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +9 -0
+static void test_fire_a_timer_multiple_times(enum arch_timer timer,
+					     wfi_method_t wm, int num)
+{
+	int i;
+
+	local_irq_disable();
+	reset_timer_state(timer, DEF_CNT);
+
+	set_tval_irq(timer, 0, CTL_ENABLE);
Quoted 8 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 476

Quoted 3 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +3 -0
+
+	for (i = 1; i <= num; i++) {
+		wm();

wfi_method_t is such a misnomer. Critically, it masks/unmasks IRQs which is rather hard to remember once you're this deep into the test code. At least having some comments on what wfi_method_t is expected to do would help a bit.

I agree a better name is called for there. How about irq_wait_method_t? That better hints what it is doing with irqs.

Quoted 10 lines
Quoted 10 lines

+/*
+ * Set a timer for tval=d_1_ms then reprogram it to tval=d_2_ms. Check that we
+ * get the timer fired. There is no timeout for the wait: we use the wfi
+ * instruction.
+ */
+static void test_reprogramming_timer(enum arch_timer timer, wfi_method_t wm,

(quoted diff) +2 -0
+				     int32_t d_1_ms, int32_t d_2_ms)
+{
Quoted 8 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 509

Quoted 5 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +5 -0
+	local_irq_disable();
+	reset_timer_state(timer, DEF_CNT);
+
+	/* Program the timer to DEF_CNT + d_1_ms. */
+	set_tval_irq(timer, msec_to_cycles(d_1_ms), CTL_ENABLE);

This assumes that the program doesn't get preempted for @d_1_ms here, right?

No? Where are you getting that?

Quoted 4 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 541

Quoted 1 line
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +1 -0
+	guest_sleep(timer, msecs_to_usecs(d_2_ms) + TEST_MARGIN_US);

Even more magic values. What is the difference between TEST_MARGIN_US and TIMEOUT_NO_IRQ_US?

Seems like they serve the same purpose and should be unified.

Quoted 6 lines
Quoted 6 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +6 -0
+static void test_reprogram_timers(enum arch_timer timer)
+{
+	int i;
+	uint64_t base_wait = test_args.wait_ms;
+
+	for_each_wfi_method(i) {
Quoted 8 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 557

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +4 -0
+		test_reprogramming_timer(timer, wfi_method[i], 2 * base_wait,
+					 base_wait);
+		test_reprogramming_timer(timer, wfi_method[i], base_wait,
+					 2 * base_wait);

What is the value of changing around the two timer deltas? It is entirely unclear from reading this what potential edge case it is detecting.

It's ensuring the timer is reprogrammed correctly whether going from a longer to a shorter delta or shorter to longer. I can comment on that.

Quoted 21 lines
Quoted 21 lines

+/*
+ * This test checks basic timer behavior without actually firing timers, things
+ * like: the relationship between cval and tval, tval down-counting.
+ */

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +16 -0
+static void timers_sanity_checks(enum arch_timer timer, bool use_sched)
+{
+	reset_timer_state(timer, DEF_CNT);
+
+	local_irq_disable();
+
+	/* cval in the past */
+	timer_set_cval(timer,
+		       timer_get_cntct(timer) -
+		       msec_to_cycles(test_args.wait_ms));
+	if (use_sched)
+		USERSPACE_MIGRATE_VCPU();
+	GUEST_ASSERT(timer_get_tval(timer) < 0);
+
+	/* tval in the past */
+	timer_set_tval(timer, -1);
Quoted 8 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 608

Quoted 5 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +5 -0
+	if (use_sched)
+		USERSPACE_MIGRATE_VCPU();
+	GUEST_ASSERT(timer_get_cval(timer) < timer_get_cntct(timer));
+
+	/* tval larger than TVAL_MAX. */

Isn't this programming CVAL with a delta larger than what can be expressed in TVAL?

Yes. If the value is not expressible by TVAL (greater than INT_MAX), then it won't work with the timer_set_tval function and has to be programmed with timer_set_cval.

Quoted 6 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 619

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +4 -0
+	/*
+	 * tval larger than 2 * TVAL_MAX.
+	 * Twice the TVAL_MAX completely loops around the TVAL.
+	 */

Same here. The comment calls it TVAL, but the test is programming CVAL.

Same response.

Quoted 9 lines

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c line 633

Quoted 6 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +6 -0
+	/* negative tval that rollovers from 0. */
+	set_counter(timer, msec_to_cycles(1));
+	timer_set_tval(timer, -1 * msec_to_cycles(test_args.wait_ms));
+	if (use_sched)
+		USERSPACE_MIGRATE_VCPU();
+	GUEST_ASSERT(timer_get_cval(timer) >= (CVAL_MAX - msec_to_cycles(9)));
					      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm lost. What is the significance of this expression?

Good question. I'll have to think about that one some more.

Quoted 4 lines

I'm pretty sure the architecture allows implementers to size the CVAL register according to the number of implemented bits in the counter. I don't see how the case of hardware truncating the MSBs of the register is handled here.

I'm pretty sure the CVAL register is always 64 bits regardless of the number of implemented counter bits.

Quoted 2 lines

Looks like there's quite a lot more of this code I haven't gotten to, but I've reached a stopping point and need to work on some other things.

Thanks. I can see you put a lot of time and effort into a thoughtful critique and I realize there are some frustrating aspects of this code I have not fully removed yet.