19fda6753SDavid Matlack // SPDX-License-Identifier: GPL-2.0
29fda6753SDavid Matlack /*
39fda6753SDavid Matlack  * Copyright (C) 2020, Google LLC.
49fda6753SDavid Matlack  */
59fda6753SDavid Matlack #define _GNU_SOURCE
69fda6753SDavid Matlack 
79fda6753SDavid Matlack #include <inttypes.h>
8*de10b798SBen Gardon #include <linux/bitmap.h>
99fda6753SDavid Matlack 
109fda6753SDavid Matlack #include "kvm_util.h"
119fda6753SDavid Matlack #include "memstress.h"
129fda6753SDavid Matlack #include "processor.h"
139fda6753SDavid Matlack 
147812d80cSDavid Matlack struct memstress_args memstress_args;
159fda6753SDavid Matlack 
169fda6753SDavid Matlack /*
179fda6753SDavid Matlack  * Guest virtual memory offset of the testing memory slot.
189fda6753SDavid Matlack  * Must not conflict with identity mapped test code.
199fda6753SDavid Matlack  */
209fda6753SDavid Matlack static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
219fda6753SDavid Matlack 
229fda6753SDavid Matlack struct vcpu_thread {
239fda6753SDavid Matlack 	/* The index of the vCPU. */
249fda6753SDavid Matlack 	int vcpu_idx;
259fda6753SDavid Matlack 
269fda6753SDavid Matlack 	/* The pthread backing the vCPU. */
279fda6753SDavid Matlack 	pthread_t thread;
289fda6753SDavid Matlack 
299fda6753SDavid Matlack 	/* Set to true once the vCPU thread is up and running. */
309fda6753SDavid Matlack 	bool running;
319fda6753SDavid Matlack };
329fda6753SDavid Matlack 
339fda6753SDavid Matlack /* The vCPU threads involved in this test. */
349fda6753SDavid Matlack static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS];
359fda6753SDavid Matlack 
369fda6753SDavid Matlack /* The function run by each vCPU thread, as provided by the test. */
377812d80cSDavid Matlack static void (*vcpu_thread_fn)(struct memstress_vcpu_args *);
389fda6753SDavid Matlack 
399fda6753SDavid Matlack /* Set to true once all vCPU threads are up and running. */
409fda6753SDavid Matlack static bool all_vcpu_threads_running;
419fda6753SDavid Matlack 
429fda6753SDavid Matlack static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
439fda6753SDavid Matlack 
449fda6753SDavid Matlack /*
459fda6753SDavid Matlack  * Continuously write to the first 8 bytes of each page in the
469fda6753SDavid Matlack  * specified region.
479fda6753SDavid Matlack  */
memstress_guest_code(uint32_t vcpu_idx)487812d80cSDavid Matlack void memstress_guest_code(uint32_t vcpu_idx)
499fda6753SDavid Matlack {
507812d80cSDavid Matlack 	struct memstress_args *args = &memstress_args;
517812d80cSDavid Matlack 	struct memstress_vcpu_args *vcpu_args = &args->vcpu_args[vcpu_idx];
529fda6753SDavid Matlack 	struct guest_random_state rand_state;
539fda6753SDavid Matlack 	uint64_t gva;
549fda6753SDavid Matlack 	uint64_t pages;
559fda6753SDavid Matlack 	uint64_t addr;
569fda6753SDavid Matlack 	uint64_t page;
579fda6753SDavid Matlack 	int i;
589fda6753SDavid Matlack 
59a008a335SDavid Matlack 	rand_state = new_guest_random_state(args->random_seed + vcpu_idx);
609fda6753SDavid Matlack 
619fda6753SDavid Matlack 	gva = vcpu_args->gva;
629fda6753SDavid Matlack 	pages = vcpu_args->pages;
639fda6753SDavid Matlack 
649fda6753SDavid Matlack 	/* Make sure vCPU args data structure is not corrupt. */
659fda6753SDavid Matlack 	GUEST_ASSERT(vcpu_args->vcpu_idx == vcpu_idx);
669fda6753SDavid Matlack 
679fda6753SDavid Matlack 	while (true) {
6807b4b2f4SPaolo Bonzini 		for (i = 0; i < sizeof(memstress_args); i += args->guest_page_size)
6907b4b2f4SPaolo Bonzini 			(void) *((volatile char *)args + i);
7007b4b2f4SPaolo Bonzini 
719fda6753SDavid Matlack 		for (i = 0; i < pages; i++) {
72a008a335SDavid Matlack 			if (args->random_access)
739fda6753SDavid Matlack 				page = guest_random_u32(&rand_state) % pages;
749fda6753SDavid Matlack 			else
759fda6753SDavid Matlack 				page = i;
769fda6753SDavid Matlack 
77a008a335SDavid Matlack 			addr = gva + (page * args->guest_page_size);
789fda6753SDavid Matlack 
79a008a335SDavid Matlack 			if (guest_random_u32(&rand_state) % 100 < args->write_percent)
809fda6753SDavid Matlack 				*(uint64_t *)addr = 0x0123456789ABCDEF;
819fda6753SDavid Matlack 			else
829fda6753SDavid Matlack 				READ_ONCE(*(uint64_t *)addr);
839fda6753SDavid Matlack 		}
849fda6753SDavid Matlack 
859fda6753SDavid Matlack 		GUEST_SYNC(1);
869fda6753SDavid Matlack 	}
879fda6753SDavid Matlack }
889fda6753SDavid Matlack 
memstress_setup_vcpus(struct kvm_vm * vm,int nr_vcpus,struct kvm_vcpu * vcpus[],uint64_t vcpu_memory_bytes,bool partition_vcpu_memory_access)897812d80cSDavid Matlack void memstress_setup_vcpus(struct kvm_vm *vm, int nr_vcpus,
909fda6753SDavid Matlack 			   struct kvm_vcpu *vcpus[],
919fda6753SDavid Matlack 			   uint64_t vcpu_memory_bytes,
929fda6753SDavid Matlack 			   bool partition_vcpu_memory_access)
939fda6753SDavid Matlack {
947812d80cSDavid Matlack 	struct memstress_args *args = &memstress_args;
957812d80cSDavid Matlack 	struct memstress_vcpu_args *vcpu_args;
969fda6753SDavid Matlack 	int i;
979fda6753SDavid Matlack 
989fda6753SDavid Matlack 	for (i = 0; i < nr_vcpus; i++) {
99a008a335SDavid Matlack 		vcpu_args = &args->vcpu_args[i];
1009fda6753SDavid Matlack 
1019fda6753SDavid Matlack 		vcpu_args->vcpu = vcpus[i];
1029fda6753SDavid Matlack 		vcpu_args->vcpu_idx = i;
1039fda6753SDavid Matlack 
1049fda6753SDavid Matlack 		if (partition_vcpu_memory_access) {
1059fda6753SDavid Matlack 			vcpu_args->gva = guest_test_virt_mem +
1069fda6753SDavid Matlack 					 (i * vcpu_memory_bytes);
1079fda6753SDavid Matlack 			vcpu_args->pages = vcpu_memory_bytes /
108a008a335SDavid Matlack 					   args->guest_page_size;
109a008a335SDavid Matlack 			vcpu_args->gpa = args->gpa + (i * vcpu_memory_bytes);
1109fda6753SDavid Matlack 		} else {
1119fda6753SDavid Matlack 			vcpu_args->gva = guest_test_virt_mem;
1129fda6753SDavid Matlack 			vcpu_args->pages = (nr_vcpus * vcpu_memory_bytes) /
113a008a335SDavid Matlack 					   args->guest_page_size;
114a008a335SDavid Matlack 			vcpu_args->gpa = args->gpa;
1159fda6753SDavid Matlack 		}
1169fda6753SDavid Matlack 
1179fda6753SDavid Matlack 		vcpu_args_set(vcpus[i], 1, i);
1189fda6753SDavid Matlack 
1199fda6753SDavid Matlack 		pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
1209fda6753SDavid Matlack 			 i, vcpu_args->gpa, vcpu_args->gpa +
121a008a335SDavid Matlack 			 (vcpu_args->pages * args->guest_page_size));
1229fda6753SDavid Matlack 	}
1239fda6753SDavid Matlack }
1249fda6753SDavid Matlack 
memstress_create_vm(enum vm_guest_mode mode,int nr_vcpus,uint64_t vcpu_memory_bytes,int slots,enum vm_mem_backing_src_type backing_src,bool partition_vcpu_memory_access)1257812d80cSDavid Matlack struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus,
1269fda6753SDavid Matlack 				   uint64_t vcpu_memory_bytes, int slots,
1279fda6753SDavid Matlack 				   enum vm_mem_backing_src_type backing_src,
1289fda6753SDavid Matlack 				   bool partition_vcpu_memory_access)
1299fda6753SDavid Matlack {
1307812d80cSDavid Matlack 	struct memstress_args *args = &memstress_args;
1319fda6753SDavid Matlack 	struct kvm_vm *vm;
1329fda6753SDavid Matlack 	uint64_t guest_num_pages, slot0_pages = 0;
1339fda6753SDavid Matlack 	uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);
1349fda6753SDavid Matlack 	uint64_t region_end_gfn;
1359fda6753SDavid Matlack 	int i;
1369fda6753SDavid Matlack 
1379fda6753SDavid Matlack 	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
1389fda6753SDavid Matlack 
1399fda6753SDavid Matlack 	/* By default vCPUs will write to memory. */
140a008a335SDavid Matlack 	args->write_percent = 100;
1419fda6753SDavid Matlack 
1429fda6753SDavid Matlack 	/*
1439fda6753SDavid Matlack 	 * Snapshot the non-huge page size.  This is used by the guest code to
1449fda6753SDavid Matlack 	 * access/dirty pages at the logging granularity.
1459fda6753SDavid Matlack 	 */
146a008a335SDavid Matlack 	args->guest_page_size = vm_guest_mode_params[mode].page_size;
1479fda6753SDavid Matlack 
1489fda6753SDavid Matlack 	guest_num_pages = vm_adjust_num_guest_pages(mode,
149a008a335SDavid Matlack 				(nr_vcpus * vcpu_memory_bytes) / args->guest_page_size);
1509fda6753SDavid Matlack 
1519fda6753SDavid Matlack 	TEST_ASSERT(vcpu_memory_bytes % getpagesize() == 0,
1529fda6753SDavid Matlack 		    "Guest memory size is not host page size aligned.");
153a008a335SDavid Matlack 	TEST_ASSERT(vcpu_memory_bytes % args->guest_page_size == 0,
1549fda6753SDavid Matlack 		    "Guest memory size is not guest page size aligned.");
1559fda6753SDavid Matlack 	TEST_ASSERT(guest_num_pages % slots == 0,
1569fda6753SDavid Matlack 		    "Guest memory cannot be evenly divided into %d slots.",
1579fda6753SDavid Matlack 		    slots);
1589fda6753SDavid Matlack 
1599fda6753SDavid Matlack 	/*
1609fda6753SDavid Matlack 	 * If using nested, allocate extra pages for the nested page tables and
1619fda6753SDavid Matlack 	 * in-memory data structures.
1629fda6753SDavid Matlack 	 */
163a008a335SDavid Matlack 	if (args->nested)
1647812d80cSDavid Matlack 		slot0_pages += memstress_nested_pages(nr_vcpus);
1659fda6753SDavid Matlack 
1669fda6753SDavid Matlack 	/*
1679fda6753SDavid Matlack 	 * Pass guest_num_pages to populate the page tables for test memory.
1689fda6753SDavid Matlack 	 * The memory is also added to memslot 0, but that's a benign side
1699fda6753SDavid Matlack 	 * effect as KVM allows aliasing HVAs in meslots.
1709fda6753SDavid Matlack 	 */
1719fda6753SDavid Matlack 	vm = __vm_create_with_vcpus(mode, nr_vcpus, slot0_pages + guest_num_pages,
1727812d80cSDavid Matlack 				    memstress_guest_code, vcpus);
1739fda6753SDavid Matlack 
174a008a335SDavid Matlack 	args->vm = vm;
1759fda6753SDavid Matlack 
1769fda6753SDavid Matlack 	/* Put the test region at the top guest physical memory. */
1779fda6753SDavid Matlack 	region_end_gfn = vm->max_gfn + 1;
1789fda6753SDavid Matlack 
1799fda6753SDavid Matlack #ifdef __x86_64__
1809fda6753SDavid Matlack 	/*
1819fda6753SDavid Matlack 	 * When running vCPUs in L2, restrict the test region to 48 bits to
1829fda6753SDavid Matlack 	 * avoid needing 5-level page tables to identity map L2.
1839fda6753SDavid Matlack 	 */
184a008a335SDavid Matlack 	if (args->nested)
185a008a335SDavid Matlack 		region_end_gfn = min(region_end_gfn, (1UL << 48) / args->guest_page_size);
1869fda6753SDavid Matlack #endif
1879fda6753SDavid Matlack 	/*
1889fda6753SDavid Matlack 	 * If there should be more memory in the guest test region than there
1899fda6753SDavid Matlack 	 * can be pages in the guest, it will definitely cause problems.
1909fda6753SDavid Matlack 	 */
1919fda6753SDavid Matlack 	TEST_ASSERT(guest_num_pages < region_end_gfn,
1929fda6753SDavid Matlack 		    "Requested more guest memory than address space allows.\n"
1939fda6753SDavid Matlack 		    "    guest pages: %" PRIx64 " max gfn: %" PRIx64
1949fda6753SDavid Matlack 		    " nr_vcpus: %d wss: %" PRIx64 "]\n",
1959fda6753SDavid Matlack 		    guest_num_pages, region_end_gfn - 1, nr_vcpus, vcpu_memory_bytes);
1969fda6753SDavid Matlack 
197a008a335SDavid Matlack 	args->gpa = (region_end_gfn - guest_num_pages - 1) * args->guest_page_size;
198a008a335SDavid Matlack 	args->gpa = align_down(args->gpa, backing_src_pagesz);
1999fda6753SDavid Matlack #ifdef __s390x__
2009fda6753SDavid Matlack 	/* Align to 1M (segment size) */
201a008a335SDavid Matlack 	args->gpa = align_down(args->gpa, 1 << 20);
2029fda6753SDavid Matlack #endif
203a008a335SDavid Matlack 	args->size = guest_num_pages * args->guest_page_size;
2049fda6753SDavid Matlack 	pr_info("guest physical test memory: [0x%lx, 0x%lx)\n",
205a008a335SDavid Matlack 		args->gpa, args->gpa + args->size);
2069fda6753SDavid Matlack 
2079fda6753SDavid Matlack 	/* Add extra memory slots for testing */
2089fda6753SDavid Matlack 	for (i = 0; i < slots; i++) {
2099fda6753SDavid Matlack 		uint64_t region_pages = guest_num_pages / slots;
210a008a335SDavid Matlack 		vm_paddr_t region_start = args->gpa + region_pages * args->guest_page_size * i;
2119fda6753SDavid Matlack 
2129fda6753SDavid Matlack 		vm_userspace_mem_region_add(vm, backing_src, region_start,
2137812d80cSDavid Matlack 					    MEMSTRESS_MEM_SLOT_INDEX + i,
2149fda6753SDavid Matlack 					    region_pages, 0);
2159fda6753SDavid Matlack 	}
2169fda6753SDavid Matlack 
2179fda6753SDavid Matlack 	/* Do mapping for the demand paging memory slot */
218a008a335SDavid Matlack 	virt_map(vm, guest_test_virt_mem, args->gpa, guest_num_pages);
2199fda6753SDavid Matlack 
2207812d80cSDavid Matlack 	memstress_setup_vcpus(vm, nr_vcpus, vcpus, vcpu_memory_bytes,
2219fda6753SDavid Matlack 			      partition_vcpu_memory_access);
2229fda6753SDavid Matlack 
223a008a335SDavid Matlack 	if (args->nested) {
2249fda6753SDavid Matlack 		pr_info("Configuring vCPUs to run in L2 (nested).\n");
2257812d80cSDavid Matlack 		memstress_setup_nested(vm, nr_vcpus, vcpus);
2269fda6753SDavid Matlack 	}
2279fda6753SDavid Matlack 
2289fda6753SDavid Matlack 	/* Export the shared variables to the guest. */
2297812d80cSDavid Matlack 	sync_global_to_guest(vm, memstress_args);
2309fda6753SDavid Matlack 
2319fda6753SDavid Matlack 	return vm;
2329fda6753SDavid Matlack }
2339fda6753SDavid Matlack 
memstress_destroy_vm(struct kvm_vm * vm)2347812d80cSDavid Matlack void memstress_destroy_vm(struct kvm_vm *vm)
2359fda6753SDavid Matlack {
2369fda6753SDavid Matlack 	kvm_vm_free(vm);
2379fda6753SDavid Matlack }
2389fda6753SDavid Matlack 
memstress_set_write_percent(struct kvm_vm * vm,uint32_t write_percent)2397812d80cSDavid Matlack void memstress_set_write_percent(struct kvm_vm *vm, uint32_t write_percent)
2409fda6753SDavid Matlack {
2417812d80cSDavid Matlack 	memstress_args.write_percent = write_percent;
2427812d80cSDavid Matlack 	sync_global_to_guest(vm, memstress_args.write_percent);
2439fda6753SDavid Matlack }
2449fda6753SDavid Matlack 
memstress_set_random_seed(struct kvm_vm * vm,uint32_t random_seed)2457812d80cSDavid Matlack void memstress_set_random_seed(struct kvm_vm *vm, uint32_t random_seed)
2469fda6753SDavid Matlack {
2477812d80cSDavid Matlack 	memstress_args.random_seed = random_seed;
2487812d80cSDavid Matlack 	sync_global_to_guest(vm, memstress_args.random_seed);
2499fda6753SDavid Matlack }
2509fda6753SDavid Matlack 
memstress_set_random_access(struct kvm_vm * vm,bool random_access)2517812d80cSDavid Matlack void memstress_set_random_access(struct kvm_vm *vm, bool random_access)
2529fda6753SDavid Matlack {
2537812d80cSDavid Matlack 	memstress_args.random_access = random_access;
2547812d80cSDavid Matlack 	sync_global_to_guest(vm, memstress_args.random_access);
2559fda6753SDavid Matlack }
2569fda6753SDavid Matlack 
memstress_nested_pages(int nr_vcpus)2577812d80cSDavid Matlack uint64_t __weak memstress_nested_pages(int nr_vcpus)
2589fda6753SDavid Matlack {
2599fda6753SDavid Matlack 	return 0;
2609fda6753SDavid Matlack }
2619fda6753SDavid Matlack 
memstress_setup_nested(struct kvm_vm * vm,int nr_vcpus,struct kvm_vcpu ** vcpus)2627812d80cSDavid Matlack void __weak memstress_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu **vcpus)
2639fda6753SDavid Matlack {
2649fda6753SDavid Matlack 	pr_info("%s() not support on this architecture, skipping.\n", __func__);
2659fda6753SDavid Matlack 	exit(KSFT_SKIP);
2669fda6753SDavid Matlack }
2679fda6753SDavid Matlack 
vcpu_thread_main(void * data)2689fda6753SDavid Matlack static void *vcpu_thread_main(void *data)
2699fda6753SDavid Matlack {
2709fda6753SDavid Matlack 	struct vcpu_thread *vcpu = data;
2719fda6753SDavid Matlack 	int vcpu_idx = vcpu->vcpu_idx;
2729fda6753SDavid Matlack 
2737812d80cSDavid Matlack 	if (memstress_args.pin_vcpus)
2747812d80cSDavid Matlack 		kvm_pin_this_task_to_pcpu(memstress_args.vcpu_to_pcpu[vcpu_idx]);
2759fda6753SDavid Matlack 
2769fda6753SDavid Matlack 	WRITE_ONCE(vcpu->running, true);
2779fda6753SDavid Matlack 
2789fda6753SDavid Matlack 	/*
2799fda6753SDavid Matlack 	 * Wait for all vCPU threads to be up and running before calling the test-
2809fda6753SDavid Matlack 	 * provided vCPU thread function. This prevents thread creation (which
2819fda6753SDavid Matlack 	 * requires taking the mmap_sem in write mode) from interfering with the
2829fda6753SDavid Matlack 	 * guest faulting in its memory.
2839fda6753SDavid Matlack 	 */
2849fda6753SDavid Matlack 	while (!READ_ONCE(all_vcpu_threads_running))
2859fda6753SDavid Matlack 		;
2869fda6753SDavid Matlack 
2877812d80cSDavid Matlack 	vcpu_thread_fn(&memstress_args.vcpu_args[vcpu_idx]);
2889fda6753SDavid Matlack 
2899fda6753SDavid Matlack 	return NULL;
2909fda6753SDavid Matlack }
2919fda6753SDavid Matlack 
memstress_start_vcpu_threads(int nr_vcpus,void (* vcpu_fn)(struct memstress_vcpu_args *))2927812d80cSDavid Matlack void memstress_start_vcpu_threads(int nr_vcpus,
2937812d80cSDavid Matlack 				  void (*vcpu_fn)(struct memstress_vcpu_args *))
2949fda6753SDavid Matlack {
2959fda6753SDavid Matlack 	int i;
2969fda6753SDavid Matlack 
2979fda6753SDavid Matlack 	vcpu_thread_fn = vcpu_fn;
2989fda6753SDavid Matlack 	WRITE_ONCE(all_vcpu_threads_running, false);
299eb561891SPaolo Bonzini 	WRITE_ONCE(memstress_args.stop_vcpus, false);
3009fda6753SDavid Matlack 
3019fda6753SDavid Matlack 	for (i = 0; i < nr_vcpus; i++) {
3029fda6753SDavid Matlack 		struct vcpu_thread *vcpu = &vcpu_threads[i];
3039fda6753SDavid Matlack 
3049fda6753SDavid Matlack 		vcpu->vcpu_idx = i;
3059fda6753SDavid Matlack 		WRITE_ONCE(vcpu->running, false);
3069fda6753SDavid Matlack 
3079fda6753SDavid Matlack 		pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu);
3089fda6753SDavid Matlack 	}
3099fda6753SDavid Matlack 
3109fda6753SDavid Matlack 	for (i = 0; i < nr_vcpus; i++) {
3119fda6753SDavid Matlack 		while (!READ_ONCE(vcpu_threads[i].running))
3129fda6753SDavid Matlack 			;
3139fda6753SDavid Matlack 	}
3149fda6753SDavid Matlack 
3159fda6753SDavid Matlack 	WRITE_ONCE(all_vcpu_threads_running, true);
3169fda6753SDavid Matlack }
3179fda6753SDavid Matlack 
memstress_join_vcpu_threads(int nr_vcpus)3187812d80cSDavid Matlack void memstress_join_vcpu_threads(int nr_vcpus)
3199fda6753SDavid Matlack {
3209fda6753SDavid Matlack 	int i;
3219fda6753SDavid Matlack 
322eb561891SPaolo Bonzini 	WRITE_ONCE(memstress_args.stop_vcpus, true);
323eb561891SPaolo Bonzini 
3249fda6753SDavid Matlack 	for (i = 0; i < nr_vcpus; i++)
3259fda6753SDavid Matlack 		pthread_join(vcpu_threads[i].thread, NULL);
3269fda6753SDavid Matlack }
327*de10b798SBen Gardon 
toggle_dirty_logging(struct kvm_vm * vm,int slots,bool enable)328*de10b798SBen Gardon static void toggle_dirty_logging(struct kvm_vm *vm, int slots, bool enable)
329*de10b798SBen Gardon {
330*de10b798SBen Gardon 	int i;
331*de10b798SBen Gardon 
332*de10b798SBen Gardon 	for (i = 0; i < slots; i++) {
333*de10b798SBen Gardon 		int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
334*de10b798SBen Gardon 		int flags = enable ? KVM_MEM_LOG_DIRTY_PAGES : 0;
335*de10b798SBen Gardon 
336*de10b798SBen Gardon 		vm_mem_region_set_flags(vm, slot, flags);
337*de10b798SBen Gardon 	}
338*de10b798SBen Gardon }
339*de10b798SBen Gardon 
memstress_enable_dirty_logging(struct kvm_vm * vm,int slots)340*de10b798SBen Gardon void memstress_enable_dirty_logging(struct kvm_vm *vm, int slots)
341*de10b798SBen Gardon {
342*de10b798SBen Gardon 	toggle_dirty_logging(vm, slots, true);
343*de10b798SBen Gardon }
344*de10b798SBen Gardon 
memstress_disable_dirty_logging(struct kvm_vm * vm,int slots)345*de10b798SBen Gardon void memstress_disable_dirty_logging(struct kvm_vm *vm, int slots)
346*de10b798SBen Gardon {
347*de10b798SBen Gardon 	toggle_dirty_logging(vm, slots, false);
348*de10b798SBen Gardon }
349*de10b798SBen Gardon 
memstress_get_dirty_log(struct kvm_vm * vm,unsigned long * bitmaps[],int slots)350*de10b798SBen Gardon void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots)
351*de10b798SBen Gardon {
352*de10b798SBen Gardon 	int i;
353*de10b798SBen Gardon 
354*de10b798SBen Gardon 	for (i = 0; i < slots; i++) {
355*de10b798SBen Gardon 		int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
356*de10b798SBen Gardon 
357*de10b798SBen Gardon 		kvm_vm_get_dirty_log(vm, slot, bitmaps[i]);
358*de10b798SBen Gardon 	}
359*de10b798SBen Gardon }
360*de10b798SBen Gardon 
memstress_clear_dirty_log(struct kvm_vm * vm,unsigned long * bitmaps[],int slots,uint64_t pages_per_slot)361*de10b798SBen Gardon void memstress_clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[],
362*de10b798SBen Gardon 			       int slots, uint64_t pages_per_slot)
363*de10b798SBen Gardon {
364*de10b798SBen Gardon 	int i;
365*de10b798SBen Gardon 
366*de10b798SBen Gardon 	for (i = 0; i < slots; i++) {
367*de10b798SBen Gardon 		int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
368*de10b798SBen Gardon 
369*de10b798SBen Gardon 		kvm_vm_clear_dirty_log(vm, slot, bitmaps[i], 0, pages_per_slot);
370*de10b798SBen Gardon 	}
371*de10b798SBen Gardon }
372*de10b798SBen Gardon 
memstress_alloc_bitmaps(int slots,uint64_t pages_per_slot)373*de10b798SBen Gardon unsigned long **memstress_alloc_bitmaps(int slots, uint64_t pages_per_slot)
374*de10b798SBen Gardon {
375*de10b798SBen Gardon 	unsigned long **bitmaps;
376*de10b798SBen Gardon 	int i;
377*de10b798SBen Gardon 
378*de10b798SBen Gardon 	bitmaps = malloc(slots * sizeof(bitmaps[0]));
379*de10b798SBen Gardon 	TEST_ASSERT(bitmaps, "Failed to allocate bitmaps array.");
380*de10b798SBen Gardon 
381*de10b798SBen Gardon 	for (i = 0; i < slots; i++) {
382*de10b798SBen Gardon 		bitmaps[i] = bitmap_zalloc(pages_per_slot);
383*de10b798SBen Gardon 		TEST_ASSERT(bitmaps[i], "Failed to allocate slot bitmap.");
384*de10b798SBen Gardon 	}
385*de10b798SBen Gardon 
386*de10b798SBen Gardon 	return bitmaps;
387*de10b798SBen Gardon }
388*de10b798SBen Gardon 
memstress_free_bitmaps(unsigned long * bitmaps[],int slots)389*de10b798SBen Gardon void memstress_free_bitmaps(unsigned long *bitmaps[], int slots)
390*de10b798SBen Gardon {
391*de10b798SBen Gardon 	int i;
392*de10b798SBen Gardon 
393*de10b798SBen Gardon 	for (i = 0; i < slots; i++)
394*de10b798SBen Gardon 		free(bitmaps[i]);
395*de10b798SBen Gardon 
396*de10b798SBen Gardon 	free(bitmaps);
397*de10b798SBen Gardon }
398