Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Bibo Mao and committed by
Huacai Chen
0f90fa6e 4e882409

+39
+9
tools/testing/selftests/kvm/lib/loongarch/processor.c
··· 3 3 #include <assert.h> 4 4 #include <linux/compiler.h> 5 5 6 + #include <asm/kvm.h> 6 7 #include "kvm_util.h" 7 8 #include "processor.h" 8 9 #include "ucall_common.h" ··· 246 245 vcpu_regs_set(vcpu, &regs); 247 246 } 248 247 248 + static void loongarch_set_reg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val) 249 + { 250 + __vcpu_set_reg(vcpu, id, val); 251 + } 252 + 249 253 static void loongarch_get_csr(struct kvm_vcpu *vcpu, uint64_t id, void *addr) 250 254 { 251 255 uint64_t csrid; ··· 291 285 loongarch_set_csr(vcpu, LOONGARCH_CSR_TCFG, 0); 292 286 loongarch_set_csr(vcpu, LOONGARCH_CSR_ASID, 1); 293 287 288 + /* time count start from 0 */ 294 289 val = 0; 290 + loongarch_set_reg(vcpu, KVM_REG_LOONGARCH_COUNTER, val); 291 + 295 292 width = vm->page_shift - 3; 296 293 297 294 switch (vm->pgtable_levels) {
+30
tools/testing/selftests/kvm/loongarch/arch_timer.c
··· 136 136 local_irq_enable(); 137 137 } 138 138 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 + 139 166 static void guest_code(void) 140 167 { 141 168 uint32_t cpu = guest_get_vcpuid(); 169 + 170 + /* must run at first */ 171 + guest_time_count_test(cpu); 142 172 143 173 timer_irq_enable(); 144 174 local_irq_enable();