Skip to contentSkip to conversation

Add arch_timer_edge_cases selftest

PATCH Superseded Colton Lewis 2 participants 4 messages Aug 9, 2024 – Aug 15, 2024

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 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
* reprogramming timers
* timers fired multiple times
* masking/unmasking using the timer control mask

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

v5:
* Drop previous patch 1 standardizing GIC addresses as this has now been done by another series
* Fix shortlog message for patch on arch_timer test
* Comment ISB use appropriately
* Rename gic_wfi() helper to wfi() as WFI instructions don't imply a GIC

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

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 (2):
KVM: selftests: Ensure pending interrupts are handled in arch_timer
test
KVM: arm64: selftests: Add arch_timer_edge_cases selftest

 tools/testing/selftests/kvm/Makefile          |    1 +
 .../kvm/aarch64/arch_timer_edge_cases.c       | 1099 +++++++++++++++++
 .../testing/selftests/kvm/aarch64/vgic_irq.c  |   11 +-
 .../kvm/include/aarch64/arch_timer.h          |   18 +-
 .../selftests/kvm/include/aarch64/processor.h |    3 +
 .../selftests/kvm/lib/aarch64/processor.c     |    6 +
 6 files changed, 1131 insertions(+), 7 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c

--
2.46.0.76.ge559c4bf1a-goog

#1 lore raw

PATCH v5 1/2 KVM: selftests: Ensure pending interrupts are handled in arch_timer test

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        | 11 +++++------
 .../testing/selftests/kvm/include/aarch64/processor.h |  3 +++
 tools/testing/selftests/kvm/lib/aarch64/processor.c   |  6 ++++++
 3 files changed, 14 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/aarch64/vgic_irq.c +5 -6
index a51dbd2a5f84..f4ac28d53747 100644
@@ -269,13 +269,12 @@ 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");
+		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/processor.h +3 -0
index 9b20a355d81a..de977d131082 100644
@@ -243,4 +243,7 @@ void smccc_smc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
 	       uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
 	       uint64_t arg6, struct arm_smccc_res *res);
 
+/* Execute a Wait For Interrupt instruction. */
+void wfi(void);
+
 #endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/lib/aarch64/processor.c +6 -0
index 0ac7cc89f38c..fe4dc3693112 100644
@@ -639,3 +639,9 @@ void vm_vaddr_populate_bitmap(struct kvm_vm *vm)
 	sparsebit_set_num(vm->vpages_valid, 0,
 			  (1ULL << vm->va_bits) >> vm->page_shift);
 }
+
+/* Helper to call wfi instruction. */
+void wfi(void)
+{
+	asm volatile("wfi");
+}
Signature · 1 line
2.46.0.76.ge559c4bf1a-goog
#2 lore raw

PATCH v5 2/2 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       | 1099 +++++++++++++++++
 .../kvm/include/aarch64/arch_timer.h          |   18 +-
 3 files changed, 1117 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c
tools/testing/selftests/kvm/Makefile +1 -0
index 48d32c5aa3eb..facf1bc6beb5 100644
@@ -152,6 +152,7 @@ TEST_GEN_PROGS_x86_64 += pre_fault_memory_test
 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_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 +1099 -0

1,099 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.46.0.76.ge559c4bf1a-goog
#3 lore raw

Re: [PATCH v5 2/2] KVM: arm64: selftests: Add arch_timer_edge_cases selftest

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

Quoted 3 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +3 -0
+#include "kvm_util.h"
+#include "processor.h"
+#include "delay.h"

nit: Are we using any of the functions from delay.h?

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

Quoted 8 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +8 -0
+#include "arch_timer.h"
+#include "gic.h"
+#include "vgic.h"
+
+const uint64_t CVAL_MAX = ~0ULL;
+/* tval is a signed 32-bit int. */
+const int32_t TVAL_MAX = INT_MAX;
+const int32_t TVAL_MIN = INT_MIN;

nit: To be super clear, use INT32_{MIN|MAX}

Quoted 6 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +6 -0
+
+/* After how much time we say there is no IRQ. */
+const uint32_t TIMEOUT_NO_IRQ_US = 50000;
+
+/* A nice counter value to use as the starting one for most tests. */
+const uint64_t DEF_CNT = (CVAL_MAX / 2);

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+
+/* Number of runs. */
+const uint32_t NR_TEST_ITERS_DEF = 5;
+
+/* Default wait test time in ms. */
+const uint32_t WAIT_TEST_MS = 10;
+
+/* Default "long" wait test time in ms. */
+const uint32_t LONG_WAIT_TEST_MS = 100;
+

Define all the above variables as 'static const'.

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

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +4 -0
+static int vtimer_irq, ptimer_irq;
+
+enum sync_cmd {
+       SET_COUNTER_VALUE = 100001,

nit: Any specific reason to start with this value?

Quoted 6 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +5 -0
+       USERSPACE_USLEEP,
+       USERSPACE_SCHED_YIELD,
+       USERSPACE_MIGRATE_SELF,
+       NO_USERSPACE_CMD,
+};
 

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+static void guest_irq_handler(struct ex_regs *regs)
+{
+       unsigned int intid = gic_get_and_ack_irq();
+       enum arch_timer timer;
+       uint64_t cnt, cval;
+       uint32_t ctl;
+       bool timer_condition, istatus;
+
+       if (intid == IAR_SPURIOUS) {
+               atomic_inc(&shared_data.spurious);

Any reason why we are accounting for the spurious interrupts? I don't see the test reading this anywhere.

Quoted 6 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +6 -0
+               goto out;
+       }
+
+       if (intid == ptimer_irq)
+               timer = PHYSICAL;
+       else if (intid == vtimer_irq)

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+               timer = VIRTUAL;
+       else
+               goto out;
+
+       ctl = timer_get_ctl(timer);
+       cval = timer_get_cval(timer);
+       cnt = timer_get_cntct(timer);
+       timer_condition = cnt >= cval;
+       istatus = (ctl & CTL_ISTATUS) && (ctl & CTL_ENABLE);
+       GUEST_ASSERT_EQ(timer_condition, istatus);

What if both are false? I think checking these independently would be a better way to go, even in terms of readability.

Quoted 16 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +16 -0
+
+       /* Disable and mask the timer. */
+       timer_set_ctl(timer, CTL_IMASK);
+
+       atomic_inc(&shared_data.handled);
+
+out:
+       gic_set_eoi(intid);
+}
+
+static void set_cval_irq(enum arch_timer timer, uint64_t cval_cycles,
+                        uint32_t ctl)
+{
+       atomic_set(&shared_data.handled, 0);
+       atomic_set(&shared_data.spurious, 0);
+       timer_set_cval(timer, cval_cycles);

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+       timer_set_ctl(timer, ctl);
+}
+
+static void set_tval_irq(enum arch_timer timer, uint64_t tval_cycles,
+                        uint32_t ctl)
+{
+       atomic_set(&shared_data.handled, 0);
+       atomic_set(&shared_data.spurious, 0);
+       timer_set_tval(timer, tval_cycles);
+       timer_set_ctl(timer, ctl);

Maybe it's a good idea to set the ctl before the tval/cval. This is just to avoid narrow races such as, when an IRQ is not needed but, programming the tval/cval could generate an IRQ before we get a chance to set ctl. The comment applies to the order in set_cval_irq() as well.

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

Quoted 3 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +3 -0
+
+/*
+ * Should be called with IRQs masked.

nit: It might be easy to miss this. Can we add a safety check? Same goes for other wait_* functions.

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +4 -0
+static void test_xval_check_no_irq(enum arch_timer timer, uint64_t xval,
+                                  uint64_t usec, enum timer_view timer_view,
+                                  sleep_method_t guest_sleep)
+{

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+       local_irq_disable();
+
+       set_xval_irq(timer, xval, CTL_ENABLE | CTL_IMASK, timer_view);
+       guest_sleep(timer, usec);
+
+       local_irq_enable();
+       isb();
+
+       /* Assume success (no IRQ) after waiting usec microseconds */
+       assert_irqs_handled(0);

The test cases calling into this expect no IRQ for the given conditions. However, since you set CTL_IMASK above, we wouldn't be getting an IRQ regardless of the fact that the timer condition is met (erroneously) or not. Should we leave it unmasked and/or check CTL_ISTATUS?

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -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);
+
+       /* Unmask the timer, and then get an IRQ. */
+       local_irq_disable();
+       timer_set_ctl(timer, CTL_ENABLE);
+       wait_for_non_spurious_irq();

Do you want to loop through @irq_wait_method array like other test routines or is there a specific reason to call only wait_for_non_spurious_irq()?

Quoted 6 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +6 -0
+
+       assert_irqs_handled(1);
+       local_irq_enable();
+}
+
+/* Check that timer control masks actually mask a timer being fired. */

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+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);

Again, do we want to loop through @sleep_method array like other routines?

Quoted 11 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +11 -0
+static void test_reprogram_timers(enum arch_timer timer)
+{
+       int i;
+       uint64_t base_wait = test_args.wait_ms;
+
+       for (i = 0; i < ARRAY_SIZE(irq_wait_method); i++) {
+               /*
+                * Ensure reprogramming works whether going from a
+                * longer time to a shorter or vice versa.
+                */
+               test_reprogramming_timer(timer, irq_wait_method[i], 2 * base_wait,

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+                                        base_wait);
+               test_reprogramming_timer(timer, irq_wait_method[i], base_wait,
+                                        2 * base_wait);
+       }
+
+       for (i = 0; i < ARRAY_SIZE(sleep_method); i++) {
+               test_reprogramming_timer_with_timeout(timer, sleep_method[i],
+                                                     2 * base_wait, base_wait);
+               test_reprogramming_timer_with_timeout(timer, sleep_method[i],
+                                                     base_wait, 2 * base_wait);

Apart from the timeout part, is there anything else that's different from the above (non-timeout) ones that I might be missing? If not, would just having the timeout one give the coverage that we are looking for?

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +4 -0
+       }
+}
+
+static void test_basic_functionality(enum arch_timer timer)

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+{
+       int32_t tval = (int32_t) msec_to_cycles(test_args.wait_ms);
+       uint64_t cval;
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(irq_wait_method); i++) {
+               irq_wait_method_t wm = irq_wait_method[i];
+
+               cval = DEF_CNT + msec_to_cycles(test_args.wait_ms);
+

nit: Similar to @tval, since the @cval variable doesn't change over the course of the loop, may be set it outside?

Quoted 17 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +16 -0
+               test_timer_cval(timer, cval, wm, true, DEF_CNT);
+               test_timer_tval(timer, tval, wm, true, DEF_CNT);
+       }
+}
+
 
+
+static void test_timers_sanity_checks(enum arch_timer timer)
+{
+       timers_sanity_checks(timer, false);
+       /* Check how KVM saves/restores these edge-case values. */
+       timers_sanity_checks(timer, true);
+}
+
+static void test_set_cnt_after_tval_max(enum arch_timer timer, irq_wait_method_t wm)
+{
+       local_irq_disable();

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+       reset_timer_state(timer, DEF_CNT);
+
+       set_cval_irq(timer,
+                    (uint64_t) TVAL_MAX +
+                    msec_to_cycles(test_args.wait_ms) / 2, CTL_ENABLE);
+
+       set_counter(timer, TVAL_MAX);
+       wm();
+
+       assert_irqs_handled(1);

When I first read this, I was thinking, "how will this assert pass if the IRQs are disabled?". But then, I looked into wm() and saw that we enable the IRQs there. Can we make this part easily readable or at least add appropriate comments? The comment applies to test_timer_xval(), test_set_cnt_after_xval(), and any other places where this approach is taken,

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

Quoted 13 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +13 -0
+       local_irq_enable();
+}
+
+/* Test timers set for: cval = now + TVAL_MAX + wait_ms / 2 */
+static void test_timers_above_tval_max(enum arch_timer timer)
+{
+       uint64_t cval;
+       int i;
+
+       /*
+        * Test that the system is not implementing cval in terms of
+        * tval.  If that was the case, setting a cval to "cval = now
+        * + TVAL_MAX + wait_ms" would wrap to "cval = now +

wait_ms / 2? Also, why /2? Is there a significance?

Quoted 22 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +22 -0
+        * wait_ms / 2", and the timer would fire immediately. Test that it
+        * doesn't.
+        */
+       for (i = 0; i < ARRAY_SIZE(sleep_method); i++) {
+               reset_timer_state(timer, DEF_CNT);
+               cval =
+                   timer_get_cntct(timer) + TVAL_MAX +
+                   msec_to_cycles(test_args.wait_ms) / 2;
+               test_cval_no_irq(timer, cval,
+                                msecs_to_usecs(test_args.wait_ms) / 2 +
+                                TIMEOUT_NO_IRQ_US, sleep_method[i]);
+       }
+
+       for (i = 0; i < ARRAY_SIZE(irq_wait_method); i++) {
+               /* Get the IRQ by moving the counter forward. */
+               test_set_cnt_after_tval_max(timer, irq_wait_method[i]);
+       }
+}
+
+/*
+ * Template function to be used by the test_move_counter_ahead_* tests.  It
+ * sets the counter to cnt_1, the [c|t]val, the counter to cnt_2, and

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+ * then waits for an IRQ.
+ */
+static void test_set_cnt_after_xval(enum arch_timer timer, uint64_t cnt_1,
+                                   uint64_t xval, uint64_t cnt_2,
+                                   irq_wait_method_t wm, enum timer_view tv)
+{
+       local_irq_disable();
+
+       set_counter(timer, cnt_1);
+       timer_set_ctl(timer, CTL_IMASK);

Is the expectation that with cnt_1 programmed, we should not be seeing an IRQ? If that's the case, should we unmask the interrupts and assert_irqs_handled(0) and/or check CTL_ISTATUS?

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

Quoted 13 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +13 -0
+
+       set_xval_irq(timer, xval, CTL_ENABLE, tv);
+       set_counter(timer, cnt_2);
+       wm();
+
+       assert_irqs_handled(1);
+       local_irq_enable();
+}
+
+/*
+ * Template function to be used by the test_move_counter_ahead_* tests.  It
+ * sets the counter to cnt_1, the [c|t]val, the counter to cnt_2, and
+ * then waits for an IRQ.

"waits for an IRQ to not show up"?

Quoted 37 lines

+ */

tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +35 -0
+static void test_set_cnt_after_xval_no_irq(enum arch_timer timer,
+                                          uint64_t cnt_1, uint64_t xval,
+                                          uint64_t cnt_2,
+                                          sleep_method_t guest_sleep,
+                                          enum timer_view tv)
+{
+       local_irq_disable();
+
+       set_counter(timer, cnt_1);
+       timer_set_ctl(timer, CTL_IMASK);
+
+       set_xval_irq(timer, xval, CTL_ENABLE, tv);
+       set_counter(timer, cnt_2);
+       guest_sleep(timer, TIMEOUT_NO_IRQ_US);
+
+       local_irq_enable();
+       isb();
+
+       /* Assume no IRQ after waiting TIMEOUT_NO_IRQ_US microseconds */
+       assert_irqs_handled(0);
+       timer_set_ctl(timer, CTL_IMASK);
+}
+
 
+static void test_timers_in_the_past(enum arch_timer timer)
+{
+       int32_t tval = -1 * (int32_t) msec_to_cycles(test_args.wait_ms);
+       uint64_t cval;
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(irq_wait_method); i++) {
+               irq_wait_method_t wm = irq_wait_method[i];
+
+               /* set a timer wait_ms the past. */
+               cval = DEF_CNT - msec_to_cycles(test_args.wait_ms);
+               test_timer_cval(timer, cval, wm, true, DEF_CNT);

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

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +10 -0
+               test_timer_tval(timer, tval, wm, true, DEF_CNT);
+
+               /* Set a timer to counter=0 (in the past) */
+               test_timer_cval(timer, 0, wm, true, DEF_CNT);
+
+               /* Set a time for tval=0 (now) */
+               test_timer_tval(timer, 0, wm, true, DEF_CNT);
+
+               /* Set a timer to as far in the past as possible */
+               test_timer_tval(timer, TVAL_MIN, wm, true, DEF_CNT);

Is this part of the test any different from the above call, test_timer_tval(timer, tval, wm, true, DEF_CNT)? I'm guessing both would wrap around and meet the timer condition, with the only difference of 'when'.

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

Quoted 12 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +12 -0
+       }
+
+       /*
+        * Set the counter to wait_ms, and a tval to -wait_ms. There should be no
+        * timer as that tval means cval=CVAL_MAX-wait_ms.
+        */
+       for (i = 0; i < ARRAY_SIZE(sleep_method); i++) {
+               sleep_method_t sm = sleep_method[i];
+
+               set_counter(timer, msec_to_cycles(test_args.wait_ms));
+               test_tval_no_irq(timer, tval, TIMEOUT_NO_IRQ_US, sm);
+       }

I'm not sure if I understand what we are trying to test here. Do you mind re-framing the comment for better clarity?

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

Quoted 12 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +12 -0
+}
+
+static void test_long_timer_delays(enum arch_timer timer)
+{
+       int32_t tval = (int32_t) msec_to_cycles(test_args.long_wait_ms);
+       uint64_t cval;
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(irq_wait_method); i++) {
+               irq_wait_method_t wm = irq_wait_method[i];
+
+               cval = DEF_CNT + msec_to_cycles(test_args.long_wait_ms);

nit: Maybe set this outside the loop?

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

Quoted 5 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +5 -0
+               test_timer_cval(timer, cval, wm, true, DEF_CNT);
+               test_timer_tval(timer, tval, wm, true, DEF_CNT);
+       }
+}
+

Thank you.
Raghavendra

Quoted 142 lines
tools/testing/selftests/kvm/aarch64/arch_timer_edge_cases.c new file +102 -0
+
+static void test_vm_create(struct kvm_vm **vm, struct kvm_vcpu **vcpu,
+                          enum arch_timer timer)
+{
+       *vm = vm_create_with_one_vcpu(vcpu, guest_code);
+       TEST_ASSERT(*vm, "Failed to create the test VM\n");
+
+       vm_init_descriptor_tables(*vm);
+       vm_install_exception_handler(*vm, VECTOR_IRQ_CURRENT,
+                                    guest_irq_handler);
+
+       vcpu_init_descriptor_tables(*vcpu);
+       vcpu_args_set(*vcpu, 1, timer);
+
+       test_init_timer_irq(*vm, *vcpu);
+       vgic_v3_setup(*vm, 1, 64);
+       sync_global_to_guest(*vm, test_args);
+}
+
+static void test_print_help(char *name)
+{
+       pr_info("Usage: %s [-h] [-b] [-i iterations] [-l long_wait_ms] [-p] [-v]\n"
+               , name);
+       pr_info("\t-i: Number of iterations (default: %u)\n",
+               NR_TEST_ITERS_DEF);
+       pr_info("\t-b: Test both physical and virtual timers (default: true)\n");
+       pr_info("\t-l: Delta (in ms) used for long wait time test (default: %u)\n",
+            LONG_WAIT_TEST_MS);
+       pr_info("\t-l: Delta (in ms) used for wait times (default: %u)\n",
+               WAIT_TEST_MS);
+       pr_info("\t-p: Test physical timer (default: true)\n");
+       pr_info("\t-v: Test virtual timer (default: true)\n");
+       pr_info("\t-h: Print this help message\n");
+}
+
+static bool parse_args(int argc, char *argv[])
+{
+       int opt;
+
+       while ((opt = getopt(argc, argv, "bhi:l:pvw:")) != -1) {
+               switch (opt) {
+               case 'b':
+                       test_args.test_physical = true;
+                       test_args.test_virtual = true;
+                       break;
+               case 'i':
+                       test_args.iterations =
+                           atoi_positive("Number of iterations", optarg);
+                       break;
+               case 'l':
+                       test_args.long_wait_ms =
+                           atoi_positive("Long wait time", optarg);
+                       break;
+               case 'p':
+                       test_args.test_physical = true;
+                       test_args.test_virtual = false;
+                       break;
+               case 'v':
+                       test_args.test_virtual = true;
+                       test_args.test_physical = false;
+                       break;
+               case 'w':
+                       test_args.wait_ms = atoi_positive("Wait time", optarg);
+                       break;
+               case 'h':
+               default:
+                       goto err;
+               }
+       }
+
+       return true;
+
+ err:
+       test_print_help(argv[0]);
+       return false;
+}
+
+int main(int argc, char *argv[])
+{
+       struct kvm_vcpu *vcpu;
+       struct kvm_vm *vm;
+
+       /* Tell stdout not to buffer its content */
+       setbuf(stdout, NULL);
+
+       if (!parse_args(argc, argv))
+               exit(KSFT_SKIP);
+
+       if (test_args.test_virtual) {
+               test_vm_create(&vm, &vcpu, VIRTUAL);
+               test_run(vm, vcpu);
+               kvm_vm_free(vm);
+       }
+
+       if (test_args.test_physical) {
+               test_vm_create(&vm, &vcpu, PHYSICAL);
+               test_run(vm, vcpu);
+               kvm_vm_free(vm);
+       }
+
+       return 0;
+}
tools/testing/selftests/kvm/include/aarch64/arch_timer.h +17 -2
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.46.0.76.ge559c4bf1a-goog