Skip to content

Commit 0f90fa6

Browse files
bibo-maochenhuacai
authored andcommitted
KVM: LoongArch: selftests: Add time counter test case
With time counter test, it is to verify that time count starts from 0 and always grows up then. Signed-off-by: Bibo Mao <maobibo@loongson.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent 4e88240 commit 0f90fa6

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

tools/testing/selftests/kvm/lib/loongarch/processor.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <assert.h>
44
#include <linux/compiler.h>
55

6+
#include <asm/kvm.h>
67
#include "kvm_util.h"
78
#include "processor.h"
89
#include "ucall_common.h"
@@ -245,6 +246,11 @@ void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...)
245246
vcpu_regs_set(vcpu, &regs);
246247
}
247248

249+
static void loongarch_set_reg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val)
250+
{
251+
__vcpu_set_reg(vcpu, id, val);
252+
}
253+
248254
static void loongarch_get_csr(struct kvm_vcpu *vcpu, uint64_t id, void *addr)
249255
{
250256
uint64_t csrid;
@@ -285,7 +291,10 @@ static void loongarch_vcpu_setup(struct kvm_vcpu *vcpu)
285291
loongarch_set_csr(vcpu, LOONGARCH_CSR_TCFG, 0);
286292
loongarch_set_csr(vcpu, LOONGARCH_CSR_ASID, 1);
287293

294+
/* time count start from 0 */
288295
val = 0;
296+
loongarch_set_reg(vcpu, KVM_REG_LOONGARCH_COUNTER, val);
297+
289298
width = vm->page_shift - 3;
290299

291300
switch (vm->pgtable_levels) {

tools/testing/selftests/kvm/loongarch/arch_timer.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,40 @@ static void guest_test_emulate_timer(uint32_t cpu)
136136
local_irq_enable();
137137
}
138138

139+
static void guest_time_count_test(uint32_t cpu)
140+
{
141+
uint32_t config_iter;
142+
unsigned long start, end, prev, us;
143+
144+
/* Assuming that test case starts to run in 1 second */
145+
start = timer_get_cycles();
146+
us = msec_to_cycles(1000);
147+
__GUEST_ASSERT(start <= us,
148+
"start = 0x%lx, us = 0x%lx.\n",
149+
start, us);
150+
151+
us = msec_to_cycles(test_args.timer_period_ms);
152+
for (config_iter = 0; config_iter < test_args.nr_iter; config_iter++) {
153+
start = timer_get_cycles();
154+
end = start + us;
155+
/* test time count growing up always */
156+
while (start < end) {
157+
prev = start;
158+
start = timer_get_cycles();
159+
__GUEST_ASSERT(prev <= start,
160+
"prev = 0x%lx, start = 0x%lx.\n",
161+
prev, start);
162+
}
163+
}
164+
}
165+
139166
static void guest_code(void)
140167
{
141168
uint32_t cpu = guest_get_vcpuid();
142169

170+
/* must run at first */
171+
guest_time_count_test(cpu);
172+
143173
timer_irq_enable();
144174
local_irq_enable();
145175
guest_test_period_timer(cpu);

0 commit comments

Comments
 (0)