1fff8dcd7SAndrew Jones // SPDX-License-Identifier: GPL-2.0
2fff8dcd7SAndrew Jones /*
3fff8dcd7SAndrew Jones * KVM dirty page logging test
4fff8dcd7SAndrew Jones *
5fff8dcd7SAndrew Jones * Copyright (C) 2018, Red Hat, Inc.
6fff8dcd7SAndrew Jones */
7fff8dcd7SAndrew Jones
8e1b376f1SAndrew Jones #define _GNU_SOURCE /* for program_invocation_name */
9e1b376f1SAndrew Jones
10fff8dcd7SAndrew Jones #include <stdio.h>
11fff8dcd7SAndrew Jones #include <stdlib.h>
12fff8dcd7SAndrew Jones #include <pthread.h>
1384292e56SPeter Xu #include <semaphore.h>
1484292e56SPeter Xu #include <sys/types.h>
1584292e56SPeter Xu #include <signal.h>
1684292e56SPeter Xu #include <errno.h>
17fff8dcd7SAndrew Jones #include <linux/bitmap.h>
18fff8dcd7SAndrew Jones #include <linux/bitops.h>
19016ff1a4SPeter Xu #include <linux/atomic.h>
204eb6486cSMarc Zyngier #include <asm/barrier.h>
21fff8dcd7SAndrew Jones
22fff8dcd7SAndrew Jones #include "kvm_util.h"
23e42ac777SAndrew Jones #include "test_util.h"
24e42ac777SAndrew Jones #include "guest_modes.h"
25fff8dcd7SAndrew Jones #include "processor.h"
26fff8dcd7SAndrew Jones
27dc6df7d4SGavin Shan #define DIRTY_MEM_BITS 30 /* 1G */
28dc6df7d4SGavin Shan #define PAGE_SHIFT_4K 12
29dc6df7d4SGavin Shan
30fff8dcd7SAndrew Jones /* The memory slot index to track dirty pages */
31fff8dcd7SAndrew Jones #define TEST_MEM_SLOT_INDEX 1
32fff8dcd7SAndrew Jones
33a049a377SThomas Huth /* Default guest test virtual memory offset */
34a049a377SThomas Huth #define DEFAULT_GUEST_TEST_MEM 0xc0000000
35fff8dcd7SAndrew Jones
36fff8dcd7SAndrew Jones /* How many pages to dirty for each guest loop */
37fff8dcd7SAndrew Jones #define TEST_PAGES_PER_LOOP 1024
38fff8dcd7SAndrew Jones
39fff8dcd7SAndrew Jones /* How many host loops to run (one KVM_GET_DIRTY_LOG for each loop) */
40fb363e2dSAndrea Parri #define TEST_HOST_LOOP_N 32UL
41fff8dcd7SAndrew Jones
42fff8dcd7SAndrew Jones /* Interval for each host loop (ms) */
43fb363e2dSAndrea Parri #define TEST_HOST_LOOP_INTERVAL 10UL
44fff8dcd7SAndrew Jones
45a049a377SThomas Huth /* Dirty bitmaps are always little endian, so we need to swap on big endian */
46a049a377SThomas Huth #if defined(__s390x__)
47a049a377SThomas Huth # define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7)
48a049a377SThomas Huth # define test_bit_le(nr, addr) \
49a049a377SThomas Huth test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
5003a0c819SSean Christopherson # define __set_bit_le(nr, addr) \
5103a0c819SSean Christopherson __set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
5203a0c819SSean Christopherson # define __clear_bit_le(nr, addr) \
5303a0c819SSean Christopherson __clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
5403a0c819SSean Christopherson # define __test_and_set_bit_le(nr, addr) \
5503a0c819SSean Christopherson __test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
5603a0c819SSean Christopherson # define __test_and_clear_bit_le(nr, addr) \
5703a0c819SSean Christopherson __test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
58a049a377SThomas Huth #else
59a049a377SThomas Huth # define test_bit_le test_bit
6003a0c819SSean Christopherson # define __set_bit_le __set_bit
6103a0c819SSean Christopherson # define __clear_bit_le __clear_bit
6203a0c819SSean Christopherson # define __test_and_set_bit_le __test_and_set_bit
6303a0c819SSean Christopherson # define __test_and_clear_bit_le __test_and_clear_bit
64a049a377SThomas Huth #endif
65a049a377SThomas Huth
66019d321aSPeter Xu #define TEST_DIRTY_RING_COUNT 65536
67019d321aSPeter Xu
68019d321aSPeter Xu #define SIG_IPI SIGUSR1
6984292e56SPeter Xu
70fff8dcd7SAndrew Jones /*
71fff8dcd7SAndrew Jones * Guest/Host shared variables. Ensure addr_gva2hva() and/or
72fff8dcd7SAndrew Jones * sync_global_to/from_guest() are used when accessing from
73fff8dcd7SAndrew Jones * the host. READ/WRITE_ONCE() should also be used with anything
74fff8dcd7SAndrew Jones * that may change.
75fff8dcd7SAndrew Jones */
76fff8dcd7SAndrew Jones static uint64_t host_page_size;
77fff8dcd7SAndrew Jones static uint64_t guest_page_size;
78e1b376f1SAndrew Jones static uint64_t guest_num_pages;
79fff8dcd7SAndrew Jones static uint64_t random_array[TEST_PAGES_PER_LOOP];
80fff8dcd7SAndrew Jones static uint64_t iteration;
81fff8dcd7SAndrew Jones
82fff8dcd7SAndrew Jones /*
836498e1daSAndrew Jones * Guest physical memory offset of the testing memory slot.
846498e1daSAndrew Jones * This will be set to the topmost valid physical address minus
856498e1daSAndrew Jones * the test memory size.
865b8ee879SAndrew Jones */
876498e1daSAndrew Jones static uint64_t guest_test_phys_mem;
886498e1daSAndrew Jones
896498e1daSAndrew Jones /*
906498e1daSAndrew Jones * Guest virtual memory offset of the testing memory slot.
916498e1daSAndrew Jones * Must not conflict with identity mapped test code.
926498e1daSAndrew Jones */
93d4df5a15SAndrew Jones static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
945b8ee879SAndrew Jones
955b8ee879SAndrew Jones /*
96fff8dcd7SAndrew Jones * Continuously write to the first 8 bytes of a random pages within
97fff8dcd7SAndrew Jones * the testing memory region.
98fff8dcd7SAndrew Jones */
guest_code(void)99fff8dcd7SAndrew Jones static void guest_code(void)
100fff8dcd7SAndrew Jones {
101a049a377SThomas Huth uint64_t addr;
102fff8dcd7SAndrew Jones int i;
103fff8dcd7SAndrew Jones
104a049a377SThomas Huth /*
105a049a377SThomas Huth * On s390x, all pages of a 1M segment are initially marked as dirty
106a049a377SThomas Huth * when a page of the segment is written to for the very first time.
107a049a377SThomas Huth * To compensate this specialty in this test, we need to touch all
108a049a377SThomas Huth * pages during the first iteration.
109a049a377SThomas Huth */
110a049a377SThomas Huth for (i = 0; i < guest_num_pages; i++) {
111a049a377SThomas Huth addr = guest_test_virt_mem + i * guest_page_size;
112a049a377SThomas Huth *(uint64_t *)addr = READ_ONCE(iteration);
113a049a377SThomas Huth }
114a049a377SThomas Huth
115fff8dcd7SAndrew Jones while (true) {
116fff8dcd7SAndrew Jones for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
117a049a377SThomas Huth addr = guest_test_virt_mem;
118e1b376f1SAndrew Jones addr += (READ_ONCE(random_array[i]) % guest_num_pages)
119fff8dcd7SAndrew Jones * guest_page_size;
120c071ff41SSean Christopherson addr = align_down(addr, host_page_size);
121fff8dcd7SAndrew Jones *(uint64_t *)addr = READ_ONCE(iteration);
122fff8dcd7SAndrew Jones }
123fff8dcd7SAndrew Jones
124fff8dcd7SAndrew Jones /* Tell the host that we need more random numbers */
125fff8dcd7SAndrew Jones GUEST_SYNC(1);
126fff8dcd7SAndrew Jones }
127fff8dcd7SAndrew Jones }
128fff8dcd7SAndrew Jones
129fff8dcd7SAndrew Jones /* Host variables */
130fff8dcd7SAndrew Jones static bool host_quit;
131fff8dcd7SAndrew Jones
132fff8dcd7SAndrew Jones /* Points to the test VM memory region on which we track dirty logs */
133fff8dcd7SAndrew Jones static void *host_test_mem;
134fff8dcd7SAndrew Jones static uint64_t host_num_pages;
135fff8dcd7SAndrew Jones
136fff8dcd7SAndrew Jones /* For statistics only */
137fff8dcd7SAndrew Jones static uint64_t host_dirty_count;
138fff8dcd7SAndrew Jones static uint64_t host_clear_count;
139fff8dcd7SAndrew Jones static uint64_t host_track_next_count;
140fff8dcd7SAndrew Jones
14184292e56SPeter Xu /* Whether dirty ring reset is requested, or finished */
142016ff1a4SPeter Xu static sem_t sem_vcpu_stop;
143016ff1a4SPeter Xu static sem_t sem_vcpu_cont;
144016ff1a4SPeter Xu /*
145016ff1a4SPeter Xu * This is only set by main thread, and only cleared by vcpu thread. It is
146016ff1a4SPeter Xu * used to request vcpu thread to stop at the next GUEST_SYNC, since GUEST_SYNC
147016ff1a4SPeter Xu * is the only place that we'll guarantee both "dirty bit" and "dirty data"
148016ff1a4SPeter Xu * will match. E.g., SIG_IPI won't guarantee that if the vcpu is interrupted
149016ff1a4SPeter Xu * after setting dirty bit but before the data is written.
150016ff1a4SPeter Xu */
151016ff1a4SPeter Xu static atomic_t vcpu_sync_stop_requested;
15284292e56SPeter Xu /*
153019d321aSPeter Xu * This is updated by the vcpu thread to tell the host whether it's a
154019d321aSPeter Xu * ring-full event. It should only be read until a sem_wait() of
155016ff1a4SPeter Xu * sem_vcpu_stop and before vcpu continues to run.
156019d321aSPeter Xu */
157019d321aSPeter Xu static bool dirty_ring_vcpu_ring_full;
158019d321aSPeter Xu /*
15984292e56SPeter Xu * This is only used for verifying the dirty pages. Dirty ring has a very
16084292e56SPeter Xu * tricky case when the ring just got full, kvm will do userspace exit due to
16184292e56SPeter Xu * ring full. When that happens, the very last PFN is set but actually the
16284292e56SPeter Xu * data is not changed (the guest WRITE is not really applied yet), because
16384292e56SPeter Xu * we found that the dirty ring is full, refused to continue the vcpu, and
16484292e56SPeter Xu * recorded the dirty gfn with the old contents.
16584292e56SPeter Xu *
16684292e56SPeter Xu * For this specific case, it's safe to skip checking this pfn for this
16784292e56SPeter Xu * bit, because it's a redundant bit, and when the write happens later the bit
16884292e56SPeter Xu * will be set again. We use this variable to always keep track of the latest
16984292e56SPeter Xu * dirty gfn we've collected, so that if a mismatch of data found later in the
17084292e56SPeter Xu * verifying process, we let it pass.
17184292e56SPeter Xu */
17284292e56SPeter Xu static uint64_t dirty_ring_last_page;
17384292e56SPeter Xu
174afdb1960SPeter Xu enum log_mode_t {
175afdb1960SPeter Xu /* Only use KVM_GET_DIRTY_LOG for logging */
176afdb1960SPeter Xu LOG_MODE_DIRTY_LOG = 0,
177afdb1960SPeter Xu
178afdb1960SPeter Xu /* Use both KVM_[GET|CLEAR]_DIRTY_LOG for logging */
179afdb1960SPeter Xu LOG_MODE_CLEAR_LOG = 1,
180afdb1960SPeter Xu
18184292e56SPeter Xu /* Use dirty ring for logging */
18284292e56SPeter Xu LOG_MODE_DIRTY_RING = 2,
18384292e56SPeter Xu
184afdb1960SPeter Xu LOG_MODE_NUM,
185afdb1960SPeter Xu
186afdb1960SPeter Xu /* Run all supported modes */
187afdb1960SPeter Xu LOG_MODE_ALL = LOG_MODE_NUM,
188afdb1960SPeter Xu };
189afdb1960SPeter Xu
190afdb1960SPeter Xu /* Mode of logging to test. Default is to run all supported modes */
191afdb1960SPeter Xu static enum log_mode_t host_log_mode_option = LOG_MODE_ALL;
192afdb1960SPeter Xu /* Logging mode for current run */
193afdb1960SPeter Xu static enum log_mode_t host_log_mode;
19484292e56SPeter Xu static pthread_t vcpu_thread;
195edd3de6fSPeter Xu static uint32_t test_dirty_ring_count = TEST_DIRTY_RING_COUNT;
19684292e56SPeter Xu
vcpu_kick(void)197019d321aSPeter Xu static void vcpu_kick(void)
198019d321aSPeter Xu {
199019d321aSPeter Xu pthread_kill(vcpu_thread, SIG_IPI);
200019d321aSPeter Xu }
201019d321aSPeter Xu
20284292e56SPeter Xu /*
20384292e56SPeter Xu * In our test we do signal tricks, let's use a better version of
20484292e56SPeter Xu * sem_wait to avoid signal interrupts
20584292e56SPeter Xu */
sem_wait_until(sem_t * sem)20684292e56SPeter Xu static void sem_wait_until(sem_t *sem)
20784292e56SPeter Xu {
20884292e56SPeter Xu int ret;
20984292e56SPeter Xu
21084292e56SPeter Xu do
21184292e56SPeter Xu ret = sem_wait(sem);
21284292e56SPeter Xu while (ret == -1 && errno == EINTR);
21384292e56SPeter Xu }
214afdb1960SPeter Xu
clear_log_supported(void)215afdb1960SPeter Xu static bool clear_log_supported(void)
216afdb1960SPeter Xu {
2173ea9b809SSean Christopherson return kvm_has_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
218afdb1960SPeter Xu }
219afdb1960SPeter Xu
clear_log_create_vm_done(struct kvm_vm * vm)220afdb1960SPeter Xu static void clear_log_create_vm_done(struct kvm_vm *vm)
221afdb1960SPeter Xu {
222afdb1960SPeter Xu u64 manual_caps;
223afdb1960SPeter Xu
224afdb1960SPeter Xu manual_caps = kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
225afdb1960SPeter Xu TEST_ASSERT(manual_caps, "MANUAL_CAPS is zero!");
226afdb1960SPeter Xu manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
227afdb1960SPeter Xu KVM_DIRTY_LOG_INITIALLY_SET);
228a12c86c4SSean Christopherson vm_enable_cap(vm, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2, manual_caps);
229afdb1960SPeter Xu }
230afdb1960SPeter Xu
dirty_log_collect_dirty_pages(struct kvm_vcpu * vcpu,int slot,void * bitmap,uint32_t num_pages,uint32_t * unused)231c09aee34SSean Christopherson static void dirty_log_collect_dirty_pages(struct kvm_vcpu *vcpu, int slot,
2327167190dSGavin Shan void *bitmap, uint32_t num_pages,
2337167190dSGavin Shan uint32_t *unused)
234afdb1960SPeter Xu {
235c09aee34SSean Christopherson kvm_vm_get_dirty_log(vcpu->vm, slot, bitmap);
236afdb1960SPeter Xu }
237afdb1960SPeter Xu
clear_log_collect_dirty_pages(struct kvm_vcpu * vcpu,int slot,void * bitmap,uint32_t num_pages,uint32_t * unused)238c09aee34SSean Christopherson static void clear_log_collect_dirty_pages(struct kvm_vcpu *vcpu, int slot,
2397167190dSGavin Shan void *bitmap, uint32_t num_pages,
2407167190dSGavin Shan uint32_t *unused)
241afdb1960SPeter Xu {
242c09aee34SSean Christopherson kvm_vm_get_dirty_log(vcpu->vm, slot, bitmap);
243c09aee34SSean Christopherson kvm_vm_clear_dirty_log(vcpu->vm, slot, bitmap, 0, num_pages);
244afdb1960SPeter Xu }
245afdb1960SPeter Xu
246016ff1a4SPeter Xu /* Should only be called after a GUEST_SYNC */
vcpu_handle_sync_stop(void)247016ff1a4SPeter Xu static void vcpu_handle_sync_stop(void)
248016ff1a4SPeter Xu {
249016ff1a4SPeter Xu if (atomic_read(&vcpu_sync_stop_requested)) {
250016ff1a4SPeter Xu /* It means main thread is sleeping waiting */
251016ff1a4SPeter Xu atomic_set(&vcpu_sync_stop_requested, false);
252016ff1a4SPeter Xu sem_post(&sem_vcpu_stop);
253016ff1a4SPeter Xu sem_wait_until(&sem_vcpu_cont);
254016ff1a4SPeter Xu }
255016ff1a4SPeter Xu }
256016ff1a4SPeter Xu
default_after_vcpu_run(struct kvm_vcpu * vcpu,int ret,int err)257c09aee34SSean Christopherson static void default_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err)
25860f644fbSPeter Xu {
259c09aee34SSean Christopherson struct kvm_run *run = vcpu->run;
26060f644fbSPeter Xu
26184292e56SPeter Xu TEST_ASSERT(ret == 0 || (ret == -1 && err == EINTR),
26284292e56SPeter Xu "vcpu run failed: errno=%d", err);
26384292e56SPeter Xu
264768e9a61SSean Christopherson TEST_ASSERT(get_ucall(vcpu, NULL) == UCALL_SYNC,
26560f644fbSPeter Xu "Invalid guest sync status: exit_reason=%s\n",
26660f644fbSPeter Xu exit_reason_str(run->exit_reason));
267016ff1a4SPeter Xu
268016ff1a4SPeter Xu vcpu_handle_sync_stop();
26960f644fbSPeter Xu }
27060f644fbSPeter Xu
dirty_ring_supported(void)27184292e56SPeter Xu static bool dirty_ring_supported(void)
27284292e56SPeter Xu {
2734b3402f1SMarc Zyngier return (kvm_has_cap(KVM_CAP_DIRTY_LOG_RING) ||
2744b3402f1SMarc Zyngier kvm_has_cap(KVM_CAP_DIRTY_LOG_RING_ACQ_REL));
27584292e56SPeter Xu }
27684292e56SPeter Xu
dirty_ring_create_vm_done(struct kvm_vm * vm)27784292e56SPeter Xu static void dirty_ring_create_vm_done(struct kvm_vm *vm)
27884292e56SPeter Xu {
279dc6df7d4SGavin Shan uint64_t pages;
280dc6df7d4SGavin Shan uint32_t limit;
281dc6df7d4SGavin Shan
282dc6df7d4SGavin Shan /*
283dc6df7d4SGavin Shan * We rely on vcpu exit due to full dirty ring state. Adjust
284dc6df7d4SGavin Shan * the ring buffer size to ensure we're able to reach the
285dc6df7d4SGavin Shan * full dirty ring state.
286dc6df7d4SGavin Shan */
287dc6df7d4SGavin Shan pages = (1ul << (DIRTY_MEM_BITS - vm->page_shift)) + 3;
288dc6df7d4SGavin Shan pages = vm_adjust_num_guest_pages(vm->mode, pages);
289dc6df7d4SGavin Shan if (vm->page_size < getpagesize())
290dc6df7d4SGavin Shan pages = vm_num_host_pages(vm->mode, pages);
291dc6df7d4SGavin Shan
292dc6df7d4SGavin Shan limit = 1 << (31 - __builtin_clz(pages));
293dc6df7d4SGavin Shan test_dirty_ring_count = 1 << (31 - __builtin_clz(test_dirty_ring_count));
294dc6df7d4SGavin Shan test_dirty_ring_count = min(limit, test_dirty_ring_count);
295dc6df7d4SGavin Shan pr_info("dirty ring count: 0x%x\n", test_dirty_ring_count);
296dc6df7d4SGavin Shan
29784292e56SPeter Xu /*
29884292e56SPeter Xu * Switch to dirty ring mode after VM creation but before any
29984292e56SPeter Xu * of the vcpu creation.
30084292e56SPeter Xu */
301edd3de6fSPeter Xu vm_enable_dirty_ring(vm, test_dirty_ring_count *
30284292e56SPeter Xu sizeof(struct kvm_dirty_gfn));
30384292e56SPeter Xu }
30484292e56SPeter Xu
dirty_gfn_is_dirtied(struct kvm_dirty_gfn * gfn)30584292e56SPeter Xu static inline bool dirty_gfn_is_dirtied(struct kvm_dirty_gfn *gfn)
30684292e56SPeter Xu {
3074eb6486cSMarc Zyngier return smp_load_acquire(&gfn->flags) == KVM_DIRTY_GFN_F_DIRTY;
30884292e56SPeter Xu }
30984292e56SPeter Xu
dirty_gfn_set_collected(struct kvm_dirty_gfn * gfn)31084292e56SPeter Xu static inline void dirty_gfn_set_collected(struct kvm_dirty_gfn *gfn)
31184292e56SPeter Xu {
3124eb6486cSMarc Zyngier smp_store_release(&gfn->flags, KVM_DIRTY_GFN_F_RESET);
31384292e56SPeter Xu }
31484292e56SPeter Xu
dirty_ring_collect_one(struct kvm_dirty_gfn * dirty_gfns,int slot,void * bitmap,uint32_t num_pages,uint32_t * fetch_index)31584292e56SPeter Xu static uint32_t dirty_ring_collect_one(struct kvm_dirty_gfn *dirty_gfns,
31684292e56SPeter Xu int slot, void *bitmap,
31784292e56SPeter Xu uint32_t num_pages, uint32_t *fetch_index)
31884292e56SPeter Xu {
31984292e56SPeter Xu struct kvm_dirty_gfn *cur;
32084292e56SPeter Xu uint32_t count = 0;
32184292e56SPeter Xu
32284292e56SPeter Xu while (true) {
323edd3de6fSPeter Xu cur = &dirty_gfns[*fetch_index % test_dirty_ring_count];
32484292e56SPeter Xu if (!dirty_gfn_is_dirtied(cur))
32584292e56SPeter Xu break;
32684292e56SPeter Xu TEST_ASSERT(cur->slot == slot, "Slot number didn't match: "
32784292e56SPeter Xu "%u != %u", cur->slot, slot);
32884292e56SPeter Xu TEST_ASSERT(cur->offset < num_pages, "Offset overflow: "
32984292e56SPeter Xu "0x%llx >= 0x%x", cur->offset, num_pages);
33084292e56SPeter Xu //pr_info("fetch 0x%x page %llu\n", *fetch_index, cur->offset);
33103a0c819SSean Christopherson __set_bit_le(cur->offset, bitmap);
33284292e56SPeter Xu dirty_ring_last_page = cur->offset;
33384292e56SPeter Xu dirty_gfn_set_collected(cur);
33484292e56SPeter Xu (*fetch_index)++;
33584292e56SPeter Xu count++;
33684292e56SPeter Xu }
33784292e56SPeter Xu
33884292e56SPeter Xu return count;
33984292e56SPeter Xu }
34084292e56SPeter Xu
dirty_ring_wait_vcpu(void)34184292e56SPeter Xu static void dirty_ring_wait_vcpu(void)
34284292e56SPeter Xu {
343019d321aSPeter Xu /* This makes sure that hardware PML cache flushed */
344019d321aSPeter Xu vcpu_kick();
345016ff1a4SPeter Xu sem_wait_until(&sem_vcpu_stop);
34684292e56SPeter Xu }
34784292e56SPeter Xu
dirty_ring_continue_vcpu(void)34884292e56SPeter Xu static void dirty_ring_continue_vcpu(void)
34984292e56SPeter Xu {
35084292e56SPeter Xu pr_info("Notifying vcpu to continue\n");
351016ff1a4SPeter Xu sem_post(&sem_vcpu_cont);
35284292e56SPeter Xu }
35384292e56SPeter Xu
dirty_ring_collect_dirty_pages(struct kvm_vcpu * vcpu,int slot,void * bitmap,uint32_t num_pages,uint32_t * ring_buf_idx)354c09aee34SSean Christopherson static void dirty_ring_collect_dirty_pages(struct kvm_vcpu *vcpu, int slot,
3557167190dSGavin Shan void *bitmap, uint32_t num_pages,
3567167190dSGavin Shan uint32_t *ring_buf_idx)
35784292e56SPeter Xu {
35884292e56SPeter Xu uint32_t count = 0, cleared;
359019d321aSPeter Xu bool continued_vcpu = false;
36084292e56SPeter Xu
36184292e56SPeter Xu dirty_ring_wait_vcpu();
36284292e56SPeter Xu
363019d321aSPeter Xu if (!dirty_ring_vcpu_ring_full) {
364019d321aSPeter Xu /*
365019d321aSPeter Xu * This is not a ring-full event, it's safe to allow
366019d321aSPeter Xu * vcpu to continue
367019d321aSPeter Xu */
368019d321aSPeter Xu dirty_ring_continue_vcpu();
369019d321aSPeter Xu continued_vcpu = true;
370019d321aSPeter Xu }
371019d321aSPeter Xu
37284292e56SPeter Xu /* Only have one vcpu */
373768e9a61SSean Christopherson count = dirty_ring_collect_one(vcpu_map_dirty_ring(vcpu),
3747167190dSGavin Shan slot, bitmap, num_pages,
3757167190dSGavin Shan ring_buf_idx);
37684292e56SPeter Xu
377c09aee34SSean Christopherson cleared = kvm_vm_reset_dirty_ring(vcpu->vm);
37884292e56SPeter Xu
379*9f9aff00SSean Christopherson /*
380*9f9aff00SSean Christopherson * Cleared pages should be the same as collected, as KVM is supposed to
381*9f9aff00SSean Christopherson * clear only the entries that have been harvested.
382*9f9aff00SSean Christopherson */
38384292e56SPeter Xu TEST_ASSERT(cleared == count, "Reset dirty pages (%u) mismatch "
38484292e56SPeter Xu "with collected (%u)", cleared, count);
38584292e56SPeter Xu
386019d321aSPeter Xu if (!continued_vcpu) {
387019d321aSPeter Xu TEST_ASSERT(dirty_ring_vcpu_ring_full,
388019d321aSPeter Xu "Didn't continue vcpu even without ring full");
38984292e56SPeter Xu dirty_ring_continue_vcpu();
390019d321aSPeter Xu }
39184292e56SPeter Xu
39284292e56SPeter Xu pr_info("Iteration %ld collected %u pages\n", iteration, count);
39384292e56SPeter Xu }
39484292e56SPeter Xu
dirty_ring_after_vcpu_run(struct kvm_vcpu * vcpu,int ret,int err)395c09aee34SSean Christopherson static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err)
39684292e56SPeter Xu {
397c09aee34SSean Christopherson struct kvm_run *run = vcpu->run;
39884292e56SPeter Xu
39984292e56SPeter Xu /* A ucall-sync or ring-full event is allowed */
400768e9a61SSean Christopherson if (get_ucall(vcpu, NULL) == UCALL_SYNC) {
40184292e56SPeter Xu /* We should allow this to continue */
40284292e56SPeter Xu ;
403019d321aSPeter Xu } else if (run->exit_reason == KVM_EXIT_DIRTY_RING_FULL ||
404019d321aSPeter Xu (ret == -1 && err == EINTR)) {
40584292e56SPeter Xu /* Update the flag first before pause */
406019d321aSPeter Xu WRITE_ONCE(dirty_ring_vcpu_ring_full,
407019d321aSPeter Xu run->exit_reason == KVM_EXIT_DIRTY_RING_FULL);
408016ff1a4SPeter Xu sem_post(&sem_vcpu_stop);
409019d321aSPeter Xu pr_info("vcpu stops because %s...\n",
410019d321aSPeter Xu dirty_ring_vcpu_ring_full ?
411019d321aSPeter Xu "dirty ring is full" : "vcpu is kicked out");
412016ff1a4SPeter Xu sem_wait_until(&sem_vcpu_cont);
41384292e56SPeter Xu pr_info("vcpu continues now.\n");
41484292e56SPeter Xu } else {
41584292e56SPeter Xu TEST_ASSERT(false, "Invalid guest sync status: "
41684292e56SPeter Xu "exit_reason=%s\n",
41784292e56SPeter Xu exit_reason_str(run->exit_reason));
41884292e56SPeter Xu }
41984292e56SPeter Xu }
42084292e56SPeter Xu
421afdb1960SPeter Xu struct log_mode {
422afdb1960SPeter Xu const char *name;
423afdb1960SPeter Xu /* Return true if this mode is supported, otherwise false */
424afdb1960SPeter Xu bool (*supported)(void);
425afdb1960SPeter Xu /* Hook when the vm creation is done (before vcpu creation) */
426afdb1960SPeter Xu void (*create_vm_done)(struct kvm_vm *vm);
427afdb1960SPeter Xu /* Hook to collect the dirty pages into the bitmap provided */
428c09aee34SSean Christopherson void (*collect_dirty_pages) (struct kvm_vcpu *vcpu, int slot,
4297167190dSGavin Shan void *bitmap, uint32_t num_pages,
4307167190dSGavin Shan uint32_t *ring_buf_idx);
43160f644fbSPeter Xu /* Hook to call when after each vcpu run */
432c09aee34SSean Christopherson void (*after_vcpu_run)(struct kvm_vcpu *vcpu, int ret, int err);
433afdb1960SPeter Xu } log_modes[LOG_MODE_NUM] = {
434afdb1960SPeter Xu {
435afdb1960SPeter Xu .name = "dirty-log",
436afdb1960SPeter Xu .collect_dirty_pages = dirty_log_collect_dirty_pages,
43760f644fbSPeter Xu .after_vcpu_run = default_after_vcpu_run,
438afdb1960SPeter Xu },
439afdb1960SPeter Xu {
440afdb1960SPeter Xu .name = "clear-log",
441afdb1960SPeter Xu .supported = clear_log_supported,
442afdb1960SPeter Xu .create_vm_done = clear_log_create_vm_done,
443afdb1960SPeter Xu .collect_dirty_pages = clear_log_collect_dirty_pages,
44460f644fbSPeter Xu .after_vcpu_run = default_after_vcpu_run,
445afdb1960SPeter Xu },
44684292e56SPeter Xu {
44784292e56SPeter Xu .name = "dirty-ring",
44884292e56SPeter Xu .supported = dirty_ring_supported,
44984292e56SPeter Xu .create_vm_done = dirty_ring_create_vm_done,
45084292e56SPeter Xu .collect_dirty_pages = dirty_ring_collect_dirty_pages,
45184292e56SPeter Xu .after_vcpu_run = dirty_ring_after_vcpu_run,
45284292e56SPeter Xu },
453afdb1960SPeter Xu };
454afdb1960SPeter Xu
455fff8dcd7SAndrew Jones /*
456fff8dcd7SAndrew Jones * We use this bitmap to track some pages that should have its dirty
457fff8dcd7SAndrew Jones * bit set in the _next_ iteration. For example, if we detected the
458fff8dcd7SAndrew Jones * page value changed to current iteration but at the same time the
459fff8dcd7SAndrew Jones * page bit is cleared in the latest bitmap, then the system must
460fff8dcd7SAndrew Jones * report that write in the next get dirty log call.
461fff8dcd7SAndrew Jones */
462fff8dcd7SAndrew Jones static unsigned long *host_bmap_track;
463fff8dcd7SAndrew Jones
log_modes_dump(void)464afdb1960SPeter Xu static void log_modes_dump(void)
465afdb1960SPeter Xu {
466afdb1960SPeter Xu int i;
467afdb1960SPeter Xu
468afdb1960SPeter Xu printf("all");
469afdb1960SPeter Xu for (i = 0; i < LOG_MODE_NUM; i++)
470afdb1960SPeter Xu printf(", %s", log_modes[i].name);
471afdb1960SPeter Xu printf("\n");
472afdb1960SPeter Xu }
473afdb1960SPeter Xu
log_mode_supported(void)474afdb1960SPeter Xu static bool log_mode_supported(void)
475afdb1960SPeter Xu {
476afdb1960SPeter Xu struct log_mode *mode = &log_modes[host_log_mode];
477afdb1960SPeter Xu
478afdb1960SPeter Xu if (mode->supported)
479afdb1960SPeter Xu return mode->supported();
480afdb1960SPeter Xu
481afdb1960SPeter Xu return true;
482afdb1960SPeter Xu }
483afdb1960SPeter Xu
log_mode_create_vm_done(struct kvm_vm * vm)484afdb1960SPeter Xu static void log_mode_create_vm_done(struct kvm_vm *vm)
485afdb1960SPeter Xu {
486afdb1960SPeter Xu struct log_mode *mode = &log_modes[host_log_mode];
487afdb1960SPeter Xu
488afdb1960SPeter Xu if (mode->create_vm_done)
489afdb1960SPeter Xu mode->create_vm_done(vm);
490afdb1960SPeter Xu }
491afdb1960SPeter Xu
log_mode_collect_dirty_pages(struct kvm_vcpu * vcpu,int slot,void * bitmap,uint32_t num_pages,uint32_t * ring_buf_idx)492c09aee34SSean Christopherson static void log_mode_collect_dirty_pages(struct kvm_vcpu *vcpu, int slot,
4937167190dSGavin Shan void *bitmap, uint32_t num_pages,
4947167190dSGavin Shan uint32_t *ring_buf_idx)
495afdb1960SPeter Xu {
496afdb1960SPeter Xu struct log_mode *mode = &log_modes[host_log_mode];
497afdb1960SPeter Xu
498afdb1960SPeter Xu TEST_ASSERT(mode->collect_dirty_pages != NULL,
499afdb1960SPeter Xu "collect_dirty_pages() is required for any log mode!");
5007167190dSGavin Shan mode->collect_dirty_pages(vcpu, slot, bitmap, num_pages, ring_buf_idx);
501afdb1960SPeter Xu }
502afdb1960SPeter Xu
log_mode_after_vcpu_run(struct kvm_vcpu * vcpu,int ret,int err)503c09aee34SSean Christopherson static void log_mode_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err)
50460f644fbSPeter Xu {
50560f644fbSPeter Xu struct log_mode *mode = &log_modes[host_log_mode];
50660f644fbSPeter Xu
50760f644fbSPeter Xu if (mode->after_vcpu_run)
508c09aee34SSean Christopherson mode->after_vcpu_run(vcpu, ret, err);
50984292e56SPeter Xu }
51084292e56SPeter Xu
generate_random_array(uint64_t * guest_array,uint64_t size)511fff8dcd7SAndrew Jones static void generate_random_array(uint64_t *guest_array, uint64_t size)
512fff8dcd7SAndrew Jones {
513fff8dcd7SAndrew Jones uint64_t i;
514fff8dcd7SAndrew Jones
515fff8dcd7SAndrew Jones for (i = 0; i < size; i++)
516fff8dcd7SAndrew Jones guest_array[i] = random();
517fff8dcd7SAndrew Jones }
518fff8dcd7SAndrew Jones
vcpu_worker(void * data)519fff8dcd7SAndrew Jones static void *vcpu_worker(void *data)
520fff8dcd7SAndrew Jones {
52138d4a385SSean Christopherson int ret;
522c09aee34SSean Christopherson struct kvm_vcpu *vcpu = data;
523c09aee34SSean Christopherson struct kvm_vm *vm = vcpu->vm;
524fff8dcd7SAndrew Jones uint64_t *guest_array;
525fff8dcd7SAndrew Jones uint64_t pages_count = 0;
526019d321aSPeter Xu struct kvm_signal_mask *sigmask = alloca(offsetof(struct kvm_signal_mask, sigset)
527019d321aSPeter Xu + sizeof(sigset_t));
528019d321aSPeter Xu sigset_t *sigset = (sigset_t *) &sigmask->sigset;
529fff8dcd7SAndrew Jones
530019d321aSPeter Xu /*
531019d321aSPeter Xu * SIG_IPI is unblocked atomically while in KVM_RUN. It causes the
532019d321aSPeter Xu * ioctl to return with -EINTR, but it is still pending and we need
533019d321aSPeter Xu * to accept it with the sigwait.
534019d321aSPeter Xu */
535019d321aSPeter Xu sigmask->len = 8;
536019d321aSPeter Xu pthread_sigmask(0, NULL, sigset);
537bf1e15a8SPaolo Bonzini sigdelset(sigset, SIG_IPI);
538768e9a61SSean Christopherson vcpu_ioctl(vcpu, KVM_SET_SIGNAL_MASK, sigmask);
539019d321aSPeter Xu
540019d321aSPeter Xu sigemptyset(sigset);
541019d321aSPeter Xu sigaddset(sigset, SIG_IPI);
542019d321aSPeter Xu
543fff8dcd7SAndrew Jones guest_array = addr_gva2hva(vm, (vm_vaddr_t)random_array);
544fff8dcd7SAndrew Jones
545fff8dcd7SAndrew Jones while (!READ_ONCE(host_quit)) {
54684292e56SPeter Xu /* Clear any existing kick signals */
54760f644fbSPeter Xu generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
54860f644fbSPeter Xu pages_count += TEST_PAGES_PER_LOOP;
549fff8dcd7SAndrew Jones /* Let the guest dirty the random pages */
550768e9a61SSean Christopherson ret = __vcpu_run(vcpu);
551019d321aSPeter Xu if (ret == -1 && errno == EINTR) {
552019d321aSPeter Xu int sig = -1;
553019d321aSPeter Xu sigwait(sigset, &sig);
554019d321aSPeter Xu assert(sig == SIG_IPI);
555019d321aSPeter Xu }
556c09aee34SSean Christopherson log_mode_after_vcpu_run(vcpu, ret, errno);
557fff8dcd7SAndrew Jones }
558fff8dcd7SAndrew Jones
5593439d886SAndrew Jones pr_info("Dirtied %"PRIu64" pages\n", pages_count);
560fff8dcd7SAndrew Jones
561fff8dcd7SAndrew Jones return NULL;
562fff8dcd7SAndrew Jones }
563fff8dcd7SAndrew Jones
vm_dirty_log_verify(enum vm_guest_mode mode,unsigned long * bmap)56487a802d9SAndrew Jones static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap)
565fff8dcd7SAndrew Jones {
56687a802d9SAndrew Jones uint64_t step = vm_num_host_pages(mode, 1);
567fff8dcd7SAndrew Jones uint64_t page;
568fff8dcd7SAndrew Jones uint64_t *value_ptr;
56984292e56SPeter Xu uint64_t min_iter = 0;
570fff8dcd7SAndrew Jones
571e1b376f1SAndrew Jones for (page = 0; page < host_num_pages; page += step) {
572fff8dcd7SAndrew Jones value_ptr = host_test_mem + page * host_page_size;
573fff8dcd7SAndrew Jones
574fff8dcd7SAndrew Jones /* If this is a special page that we were tracking... */
57503a0c819SSean Christopherson if (__test_and_clear_bit_le(page, host_bmap_track)) {
576fff8dcd7SAndrew Jones host_track_next_count++;
577a049a377SThomas Huth TEST_ASSERT(test_bit_le(page, bmap),
578fff8dcd7SAndrew Jones "Page %"PRIu64" should have its dirty bit "
579fff8dcd7SAndrew Jones "set in this iteration but it is missing",
580fff8dcd7SAndrew Jones page);
581fff8dcd7SAndrew Jones }
582fff8dcd7SAndrew Jones
58303a0c819SSean Christopherson if (__test_and_clear_bit_le(page, bmap)) {
58484292e56SPeter Xu bool matched;
58584292e56SPeter Xu
586fff8dcd7SAndrew Jones host_dirty_count++;
58784292e56SPeter Xu
588fff8dcd7SAndrew Jones /*
589fff8dcd7SAndrew Jones * If the bit is set, the value written onto
590fff8dcd7SAndrew Jones * the corresponding page should be either the
591fff8dcd7SAndrew Jones * previous iteration number or the current one.
592fff8dcd7SAndrew Jones */
59384292e56SPeter Xu matched = (*value_ptr == iteration ||
59484292e56SPeter Xu *value_ptr == iteration - 1);
59584292e56SPeter Xu
59684292e56SPeter Xu if (host_log_mode == LOG_MODE_DIRTY_RING && !matched) {
59784292e56SPeter Xu if (*value_ptr == iteration - 2 && min_iter <= iteration - 2) {
59884292e56SPeter Xu /*
59984292e56SPeter Xu * Short answer: this case is special
60084292e56SPeter Xu * only for dirty ring test where the
60184292e56SPeter Xu * page is the last page before a kvm
60284292e56SPeter Xu * dirty ring full in iteration N-2.
60384292e56SPeter Xu *
60484292e56SPeter Xu * Long answer: Assuming ring size R,
60584292e56SPeter Xu * one possible condition is:
60684292e56SPeter Xu *
60784292e56SPeter Xu * main thr vcpu thr
60884292e56SPeter Xu * -------- --------
60984292e56SPeter Xu * iter=1
61084292e56SPeter Xu * write 1 to page 0~(R-1)
61184292e56SPeter Xu * full, vmexit
61284292e56SPeter Xu * collect 0~(R-1)
61384292e56SPeter Xu * kick vcpu
61484292e56SPeter Xu * write 1 to (R-1)~(2R-2)
61584292e56SPeter Xu * full, vmexit
61684292e56SPeter Xu * iter=2
61784292e56SPeter Xu * collect (R-1)~(2R-2)
61884292e56SPeter Xu * kick vcpu
61984292e56SPeter Xu * write 1 to (2R-2)
62084292e56SPeter Xu * (NOTE!!! "1" cached in cpu reg)
62184292e56SPeter Xu * write 2 to (2R-1)~(3R-3)
62284292e56SPeter Xu * full, vmexit
62384292e56SPeter Xu * iter=3
62484292e56SPeter Xu * collect (2R-2)~(3R-3)
62584292e56SPeter Xu * (here if we read value on page
62684292e56SPeter Xu * "2R-2" is 1, while iter=3!!!)
62784292e56SPeter Xu *
62884292e56SPeter Xu * This however can only happen once per iteration.
62984292e56SPeter Xu */
63084292e56SPeter Xu min_iter = iteration - 1;
63184292e56SPeter Xu continue;
63284292e56SPeter Xu } else if (page == dirty_ring_last_page) {
63384292e56SPeter Xu /*
63484292e56SPeter Xu * Please refer to comments in
63584292e56SPeter Xu * dirty_ring_last_page.
63684292e56SPeter Xu */
63784292e56SPeter Xu continue;
63884292e56SPeter Xu }
63984292e56SPeter Xu }
64084292e56SPeter Xu
64184292e56SPeter Xu TEST_ASSERT(matched,
642fff8dcd7SAndrew Jones "Set page %"PRIu64" value %"PRIu64
643fff8dcd7SAndrew Jones " incorrect (iteration=%"PRIu64")",
644fff8dcd7SAndrew Jones page, *value_ptr, iteration);
645fff8dcd7SAndrew Jones } else {
646fff8dcd7SAndrew Jones host_clear_count++;
647fff8dcd7SAndrew Jones /*
648fff8dcd7SAndrew Jones * If cleared, the value written can be any
649fff8dcd7SAndrew Jones * value smaller or equals to the iteration
650fff8dcd7SAndrew Jones * number. Note that the value can be exactly
651fff8dcd7SAndrew Jones * (iteration-1) if that write can happen
652fff8dcd7SAndrew Jones * like this:
653fff8dcd7SAndrew Jones *
654fff8dcd7SAndrew Jones * (1) increase loop count to "iteration-1"
655fff8dcd7SAndrew Jones * (2) write to page P happens (with value
656fff8dcd7SAndrew Jones * "iteration-1")
657fff8dcd7SAndrew Jones * (3) get dirty log for "iteration-1"; we'll
658fff8dcd7SAndrew Jones * see that page P bit is set (dirtied),
659fff8dcd7SAndrew Jones * and not set the bit in host_bmap_track
660fff8dcd7SAndrew Jones * (4) increase loop count to "iteration"
661fff8dcd7SAndrew Jones * (which is current iteration)
662fff8dcd7SAndrew Jones * (5) get dirty log for current iteration,
663fff8dcd7SAndrew Jones * we'll see that page P is cleared, with
664fff8dcd7SAndrew Jones * value "iteration-1".
665fff8dcd7SAndrew Jones */
666fff8dcd7SAndrew Jones TEST_ASSERT(*value_ptr <= iteration,
667fff8dcd7SAndrew Jones "Clear page %"PRIu64" value %"PRIu64
668fff8dcd7SAndrew Jones " incorrect (iteration=%"PRIu64")",
669fff8dcd7SAndrew Jones page, *value_ptr, iteration);
670fff8dcd7SAndrew Jones if (*value_ptr == iteration) {
671fff8dcd7SAndrew Jones /*
672fff8dcd7SAndrew Jones * This page is _just_ modified; it
673fff8dcd7SAndrew Jones * should report its dirtyness in the
674fff8dcd7SAndrew Jones * next run
675fff8dcd7SAndrew Jones */
67603a0c819SSean Christopherson __set_bit_le(page, host_bmap_track);
677fff8dcd7SAndrew Jones }
678fff8dcd7SAndrew Jones }
679fff8dcd7SAndrew Jones }
680fff8dcd7SAndrew Jones }
681fff8dcd7SAndrew Jones
create_vm(enum vm_guest_mode mode,struct kvm_vcpu ** vcpu,uint64_t extra_mem_pages,void * guest_code)68264a1aaccSSean Christopherson static struct kvm_vm *create_vm(enum vm_guest_mode mode, struct kvm_vcpu **vcpu,
68312c386b2SPeter Xu uint64_t extra_mem_pages, void *guest_code)
684fff8dcd7SAndrew Jones {
685e1b376f1SAndrew Jones struct kvm_vm *vm;
686e1b376f1SAndrew Jones
6873439d886SAndrew Jones pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
6883439d886SAndrew Jones
6896e1d13bfSSean Christopherson vm = __vm_create(mode, 1, extra_mem_pages);
6904acefa38SSean Christopherson
691afdb1960SPeter Xu log_mode_create_vm_done(vm);
69264a1aaccSSean Christopherson *vcpu = vm_vcpu_add(vm, 0, guest_code);
693e1b376f1SAndrew Jones return vm;
694fff8dcd7SAndrew Jones }
695fff8dcd7SAndrew Jones
696e42ac777SAndrew Jones struct test_params {
697e42ac777SAndrew Jones unsigned long iterations;
698e42ac777SAndrew Jones unsigned long interval;
699e42ac777SAndrew Jones uint64_t phys_offset;
700e42ac777SAndrew Jones };
701e42ac777SAndrew Jones
run_test(enum vm_guest_mode mode,void * arg)702e42ac777SAndrew Jones static void run_test(enum vm_guest_mode mode, void *arg)
703fff8dcd7SAndrew Jones {
704e42ac777SAndrew Jones struct test_params *p = arg;
705c09aee34SSean Christopherson struct kvm_vcpu *vcpu;
706fff8dcd7SAndrew Jones struct kvm_vm *vm;
707fff8dcd7SAndrew Jones unsigned long *bmap;
7087167190dSGavin Shan uint32_t ring_buf_idx = 0;
709*9f9aff00SSean Christopherson int sem_val;
710fff8dcd7SAndrew Jones
711afdb1960SPeter Xu if (!log_mode_supported()) {
712afdb1960SPeter Xu print_skip("Log mode '%s' not supported",
713afdb1960SPeter Xu log_modes[host_log_mode].name);
714afdb1960SPeter Xu return;
715afdb1960SPeter Xu }
716afdb1960SPeter Xu
717338eb298SPeter Xu /*
718338eb298SPeter Xu * We reserve page table for 2 times of extra dirty mem which
719338eb298SPeter Xu * will definitely cover the original (1G+) test range. Here
720338eb298SPeter Xu * we do the calculation with 4K page size which is the
721338eb298SPeter Xu * smallest so the page number will be enough for all archs
722338eb298SPeter Xu * (e.g., 64K page size guest will need even less memory for
723338eb298SPeter Xu * page tables).
724338eb298SPeter Xu */
72564a1aaccSSean Christopherson vm = create_vm(mode, &vcpu,
72664a1aaccSSean Christopherson 2ul << (DIRTY_MEM_BITS - PAGE_SHIFT_4K), guest_code);
727338eb298SPeter Xu
72868c1b3e9SSean Christopherson guest_page_size = vm->page_size;
72976d58e0fSPaolo Bonzini /*
73076d58e0fSPaolo Bonzini * A little more than 1G of guest page sized pages. Cover the
73176d58e0fSPaolo Bonzini * case where the size is not aligned to 64 pages.
73276d58e0fSPaolo Bonzini */
73368c1b3e9SSean Christopherson guest_num_pages = (1ul << (DIRTY_MEM_BITS - vm->page_shift)) + 3;
73487a802d9SAndrew Jones guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
735331b4de9SAndrew Jones
736fff8dcd7SAndrew Jones host_page_size = getpagesize();
73787a802d9SAndrew Jones host_num_pages = vm_num_host_pages(mode, guest_num_pages);
738fff8dcd7SAndrew Jones
739e42ac777SAndrew Jones if (!p->phys_offset) {
74068c1b3e9SSean Christopherson guest_test_phys_mem = (vm->max_gfn - guest_num_pages) *
74168c1b3e9SSean Christopherson guest_page_size;
742c071ff41SSean Christopherson guest_test_phys_mem = align_down(guest_test_phys_mem, host_page_size);
743fd3f6f81SAndrew Jones } else {
744e42ac777SAndrew Jones guest_test_phys_mem = p->phys_offset;
7455b8ee879SAndrew Jones }
7465b8ee879SAndrew Jones
747a049a377SThomas Huth #ifdef __s390x__
748a049a377SThomas Huth /* Align to 1M (segment size) */
749c071ff41SSean Christopherson guest_test_phys_mem = align_down(guest_test_phys_mem, 1 << 20);
750a049a377SThomas Huth #endif
751a049a377SThomas Huth
7523439d886SAndrew Jones pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
7535b8ee879SAndrew Jones
7547fc5b571SAndy Shevchenko bmap = bitmap_zalloc(host_num_pages);
7557fc5b571SAndy Shevchenko host_bmap_track = bitmap_zalloc(host_num_pages);
756fff8dcd7SAndrew Jones
757fff8dcd7SAndrew Jones /* Add an extra memory slot for testing dirty logging */
758fff8dcd7SAndrew Jones vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
759d4df5a15SAndrew Jones guest_test_phys_mem,
760fff8dcd7SAndrew Jones TEST_MEM_SLOT_INDEX,
761e1b376f1SAndrew Jones guest_num_pages,
762fff8dcd7SAndrew Jones KVM_MEM_LOG_DIRTY_PAGES);
763fff8dcd7SAndrew Jones
764d4df5a15SAndrew Jones /* Do mapping for the dirty track memory slot */
7654307af73SSean Christopherson virt_map(vm, guest_test_virt_mem, guest_test_phys_mem, guest_num_pages);
766fff8dcd7SAndrew Jones
767fff8dcd7SAndrew Jones /* Cache the HVA pointer of the region */
768d4df5a15SAndrew Jones host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)guest_test_phys_mem);
769fff8dcd7SAndrew Jones
770e1b376f1SAndrew Jones /* Export the shared variables to the guest */
771fff8dcd7SAndrew Jones sync_global_to_guest(vm, host_page_size);
772fff8dcd7SAndrew Jones sync_global_to_guest(vm, guest_page_size);
773d4df5a15SAndrew Jones sync_global_to_guest(vm, guest_test_virt_mem);
774e1b376f1SAndrew Jones sync_global_to_guest(vm, guest_num_pages);
775fff8dcd7SAndrew Jones
776fff8dcd7SAndrew Jones /* Start the iterations */
777fff8dcd7SAndrew Jones iteration = 1;
778fff8dcd7SAndrew Jones sync_global_to_guest(vm, iteration);
779*9f9aff00SSean Christopherson WRITE_ONCE(host_quit, false);
780e1b376f1SAndrew Jones host_dirty_count = 0;
781e1b376f1SAndrew Jones host_clear_count = 0;
782e1b376f1SAndrew Jones host_track_next_count = 0;
7837167190dSGavin Shan WRITE_ONCE(dirty_ring_vcpu_ring_full, false);
784fff8dcd7SAndrew Jones
785*9f9aff00SSean Christopherson /*
786*9f9aff00SSean Christopherson * Ensure the previous iteration didn't leave a dangling semaphore, i.e.
787*9f9aff00SSean Christopherson * that the main task and vCPU worker were synchronized and completed
788*9f9aff00SSean Christopherson * verification of all iterations.
789*9f9aff00SSean Christopherson */
790*9f9aff00SSean Christopherson sem_getvalue(&sem_vcpu_stop, &sem_val);
791*9f9aff00SSean Christopherson TEST_ASSERT_EQ(sem_val, 0);
792*9f9aff00SSean Christopherson sem_getvalue(&sem_vcpu_cont, &sem_val);
793*9f9aff00SSean Christopherson TEST_ASSERT_EQ(sem_val, 0);
794*9f9aff00SSean Christopherson
795c09aee34SSean Christopherson pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu);
796fff8dcd7SAndrew Jones
797e42ac777SAndrew Jones while (iteration < p->iterations) {
798fff8dcd7SAndrew Jones /* Give the vcpu thread some time to dirty some pages */
799e42ac777SAndrew Jones usleep(p->interval * 1000);
800c09aee34SSean Christopherson log_mode_collect_dirty_pages(vcpu, TEST_MEM_SLOT_INDEX,
8017167190dSGavin Shan bmap, host_num_pages,
8027167190dSGavin Shan &ring_buf_idx);
803016ff1a4SPeter Xu
804016ff1a4SPeter Xu /*
805016ff1a4SPeter Xu * See vcpu_sync_stop_requested definition for details on why
806016ff1a4SPeter Xu * we need to stop vcpu when verify data.
807016ff1a4SPeter Xu */
808016ff1a4SPeter Xu atomic_set(&vcpu_sync_stop_requested, true);
809016ff1a4SPeter Xu sem_wait_until(&sem_vcpu_stop);
810016ff1a4SPeter Xu /*
811016ff1a4SPeter Xu * NOTE: for dirty ring, it's possible that we didn't stop at
812016ff1a4SPeter Xu * GUEST_SYNC but instead we stopped because ring is full;
813016ff1a4SPeter Xu * that's okay too because ring full means we're only missing
814016ff1a4SPeter Xu * the flush of the last page, and since we handle the last
815016ff1a4SPeter Xu * page specially verification will succeed anyway.
816016ff1a4SPeter Xu */
817016ff1a4SPeter Xu assert(host_log_mode == LOG_MODE_DIRTY_RING ||
818016ff1a4SPeter Xu atomic_read(&vcpu_sync_stop_requested) == false);
81987a802d9SAndrew Jones vm_dirty_log_verify(mode, bmap);
820016ff1a4SPeter Xu
821*9f9aff00SSean Christopherson /*
822*9f9aff00SSean Christopherson * Set host_quit before sem_vcpu_cont in the final iteration to
823*9f9aff00SSean Christopherson * ensure that the vCPU worker doesn't resume the guest. As
824*9f9aff00SSean Christopherson * above, the dirty ring test may stop and wait even when not
825*9f9aff00SSean Christopherson * explicitly request to do so, i.e. would hang waiting for a
826*9f9aff00SSean Christopherson * "continue" if it's allowed to resume the guest.
827*9f9aff00SSean Christopherson */
828*9f9aff00SSean Christopherson if (++iteration == p->iterations)
829*9f9aff00SSean Christopherson WRITE_ONCE(host_quit, true);
830*9f9aff00SSean Christopherson
831*9f9aff00SSean Christopherson sem_post(&sem_vcpu_cont);
832fff8dcd7SAndrew Jones sync_global_to_guest(vm, iteration);
833fff8dcd7SAndrew Jones }
834fff8dcd7SAndrew Jones
835fff8dcd7SAndrew Jones pthread_join(vcpu_thread, NULL);
836fff8dcd7SAndrew Jones
8373439d886SAndrew Jones pr_info("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "
838fff8dcd7SAndrew Jones "track_next (%"PRIu64")\n", host_dirty_count, host_clear_count,
839fff8dcd7SAndrew Jones host_track_next_count);
840fff8dcd7SAndrew Jones
841fff8dcd7SAndrew Jones free(bmap);
842fff8dcd7SAndrew Jones free(host_bmap_track);
843fff8dcd7SAndrew Jones kvm_vm_free(vm);
844e1b376f1SAndrew Jones }
845e1b376f1SAndrew Jones
help(char * name)846e1b376f1SAndrew Jones static void help(char *name)
847e1b376f1SAndrew Jones {
848e1b376f1SAndrew Jones puts("");
8495b8ee879SAndrew Jones printf("usage: %s [-h] [-i iterations] [-I interval] "
8506498e1daSAndrew Jones "[-p offset] [-m mode]\n", name);
851e1b376f1SAndrew Jones puts("");
852dc6df7d4SGavin Shan printf(" -c: hint to dirty ring size, in number of entries\n");
853edd3de6fSPeter Xu printf(" (only useful for dirty-ring test; default: %"PRIu32")\n",
854edd3de6fSPeter Xu TEST_DIRTY_RING_COUNT);
855e1b376f1SAndrew Jones printf(" -i: specify iteration counts (default: %"PRIu64")\n",
856e1b376f1SAndrew Jones TEST_HOST_LOOP_N);
857e1b376f1SAndrew Jones printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n",
858e1b376f1SAndrew Jones TEST_HOST_LOOP_INTERVAL);
8596498e1daSAndrew Jones printf(" -p: specify guest physical test memory offset\n"
8606498e1daSAndrew Jones " Warning: a low offset can conflict with the loaded test code.\n");
861afdb1960SPeter Xu printf(" -M: specify the host logging mode "
862afdb1960SPeter Xu "(default: run all log modes). Supported modes: \n\t");
863afdb1960SPeter Xu log_modes_dump();
864e42ac777SAndrew Jones guest_modes_help();
865e1b376f1SAndrew Jones puts("");
866e1b376f1SAndrew Jones exit(0);
867e1b376f1SAndrew Jones }
868e1b376f1SAndrew Jones
main(int argc,char * argv[])869e1b376f1SAndrew Jones int main(int argc, char *argv[])
870e1b376f1SAndrew Jones {
871e42ac777SAndrew Jones struct test_params p = {
872e42ac777SAndrew Jones .iterations = TEST_HOST_LOOP_N,
873e42ac777SAndrew Jones .interval = TEST_HOST_LOOP_INTERVAL,
874e42ac777SAndrew Jones };
875e42ac777SAndrew Jones int opt, i;
876bf1e15a8SPaolo Bonzini sigset_t sigset;
8772a31b9dbSPaolo Bonzini
878016ff1a4SPeter Xu sem_init(&sem_vcpu_stop, 0, 0);
879016ff1a4SPeter Xu sem_init(&sem_vcpu_cont, 0, 0);
88084292e56SPeter Xu
881e42ac777SAndrew Jones guest_modes_append_default();
882696ade77SAndrew Jones
883edd3de6fSPeter Xu while ((opt = getopt(argc, argv, "c:hi:I:p:m:M:")) != -1) {
884e1b376f1SAndrew Jones switch (opt) {
885edd3de6fSPeter Xu case 'c':
886edd3de6fSPeter Xu test_dirty_ring_count = strtol(optarg, NULL, 10);
887edd3de6fSPeter Xu break;
888e1b376f1SAndrew Jones case 'i':
889e42ac777SAndrew Jones p.iterations = strtol(optarg, NULL, 10);
890e1b376f1SAndrew Jones break;
891e1b376f1SAndrew Jones case 'I':
892e42ac777SAndrew Jones p.interval = strtol(optarg, NULL, 10);
893e1b376f1SAndrew Jones break;
8946498e1daSAndrew Jones case 'p':
895e42ac777SAndrew Jones p.phys_offset = strtoull(optarg, NULL, 0);
8965b8ee879SAndrew Jones break;
897e1b376f1SAndrew Jones case 'm':
898e42ac777SAndrew Jones guest_modes_cmdline(optarg);
899e1b376f1SAndrew Jones break;
900afdb1960SPeter Xu case 'M':
901afdb1960SPeter Xu if (!strcmp(optarg, "all")) {
902afdb1960SPeter Xu host_log_mode_option = LOG_MODE_ALL;
903afdb1960SPeter Xu break;
904afdb1960SPeter Xu }
905afdb1960SPeter Xu for (i = 0; i < LOG_MODE_NUM; i++) {
906afdb1960SPeter Xu if (!strcmp(optarg, log_modes[i].name)) {
907afdb1960SPeter Xu pr_info("Setting log mode to: '%s'\n",
908afdb1960SPeter Xu optarg);
909afdb1960SPeter Xu host_log_mode_option = i;
910afdb1960SPeter Xu break;
911afdb1960SPeter Xu }
912afdb1960SPeter Xu }
913afdb1960SPeter Xu if (i == LOG_MODE_NUM) {
914afdb1960SPeter Xu printf("Log mode '%s' invalid. Please choose "
915afdb1960SPeter Xu "from: ", optarg);
916afdb1960SPeter Xu log_modes_dump();
917afdb1960SPeter Xu exit(1);
918afdb1960SPeter Xu }
919afdb1960SPeter Xu break;
920e1b376f1SAndrew Jones case 'h':
921e1b376f1SAndrew Jones default:
922e1b376f1SAndrew Jones help(argv[0]);
923e1b376f1SAndrew Jones break;
924e1b376f1SAndrew Jones }
925e1b376f1SAndrew Jones }
926e1b376f1SAndrew Jones
927e42ac777SAndrew Jones TEST_ASSERT(p.iterations > 2, "Iterations must be greater than two");
928e42ac777SAndrew Jones TEST_ASSERT(p.interval > 0, "Interval must be greater than zero");
929e1b376f1SAndrew Jones
9303439d886SAndrew Jones pr_info("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n",
931e42ac777SAndrew Jones p.iterations, p.interval);
932e1b376f1SAndrew Jones
933e1b376f1SAndrew Jones srandom(time(0));
934e1b376f1SAndrew Jones
935bf1e15a8SPaolo Bonzini /* Ensure that vCPU threads start with SIG_IPI blocked. */
936bf1e15a8SPaolo Bonzini sigemptyset(&sigset);
937bf1e15a8SPaolo Bonzini sigaddset(&sigset, SIG_IPI);
938bf1e15a8SPaolo Bonzini pthread_sigmask(SIG_BLOCK, &sigset, NULL);
939bf1e15a8SPaolo Bonzini
940afdb1960SPeter Xu if (host_log_mode_option == LOG_MODE_ALL) {
941afdb1960SPeter Xu /* Run each log mode */
942e42ac777SAndrew Jones for (i = 0; i < LOG_MODE_NUM; i++) {
943e42ac777SAndrew Jones pr_info("Testing Log Mode '%s'\n", log_modes[i].name);
944e42ac777SAndrew Jones host_log_mode = i;
945e42ac777SAndrew Jones for_each_guest_mode(run_test, &p);
946e1b376f1SAndrew Jones }
947afdb1960SPeter Xu } else {
948afdb1960SPeter Xu host_log_mode = host_log_mode_option;
949e42ac777SAndrew Jones for_each_guest_mode(run_test, &p);
950afdb1960SPeter Xu }
951fff8dcd7SAndrew Jones
952fff8dcd7SAndrew Jones return 0;
953fff8dcd7SAndrew Jones }
954