cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

memslot_modification_stress_test.c (4770B)


      1// SPDX-License-Identifier: GPL-2.0
      2/*
      3 * KVM memslot modification stress test
      4 * Adapted from demand_paging_test.c
      5 *
      6 * Copyright (C) 2018, Red Hat, Inc.
      7 * Copyright (C) 2020, Google, Inc.
      8 */
      9
     10#define _GNU_SOURCE /* for program_invocation_name */
     11
     12#include <stdio.h>
     13#include <stdlib.h>
     14#include <sys/syscall.h>
     15#include <unistd.h>
     16#include <asm/unistd.h>
     17#include <time.h>
     18#include <poll.h>
     19#include <pthread.h>
     20#include <linux/bitmap.h>
     21#include <linux/bitops.h>
     22#include <linux/userfaultfd.h>
     23
     24#include "perf_test_util.h"
     25#include "processor.h"
     26#include "test_util.h"
     27#include "guest_modes.h"
     28
     29#define DUMMY_MEMSLOT_INDEX 7
     30
     31#define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10
     32
     33
     34static int nr_vcpus = 1;
     35static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
     36
     37static bool run_vcpus = true;
     38
     39static void vcpu_worker(struct perf_test_vcpu_args *vcpu_args)
     40{
     41	int ret;
     42	int vcpu_id = vcpu_args->vcpu_id;
     43	struct kvm_vm *vm = perf_test_args.vm;
     44	struct kvm_run *run;
     45
     46	run = vcpu_state(vm, vcpu_id);
     47
     48	/* Let the guest access its memory until a stop signal is received */
     49	while (READ_ONCE(run_vcpus)) {
     50		ret = _vcpu_run(vm, vcpu_id);
     51		TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
     52
     53		if (get_ucall(vm, vcpu_id, NULL) == UCALL_SYNC)
     54			continue;
     55
     56		TEST_ASSERT(false,
     57			    "Invalid guest sync status: exit_reason=%s\n",
     58			    exit_reason_str(run->exit_reason));
     59	}
     60}
     61
     62struct memslot_antagonist_args {
     63	struct kvm_vm *vm;
     64	useconds_t delay;
     65	uint64_t nr_modifications;
     66};
     67
     68static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
     69			       uint64_t nr_modifications)
     70{
     71	const uint64_t pages = 1;
     72	uint64_t gpa;
     73	int i;
     74
     75	/*
     76	 * Add the dummy memslot just below the perf_test_util memslot, which is
     77	 * at the top of the guest physical address space.
     78	 */
     79	gpa = perf_test_args.gpa - pages * vm_get_page_size(vm);
     80
     81	for (i = 0; i < nr_modifications; i++) {
     82		usleep(delay);
     83		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
     84					    DUMMY_MEMSLOT_INDEX, pages, 0);
     85
     86		vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
     87	}
     88}
     89
     90struct test_params {
     91	useconds_t memslot_modification_delay;
     92	uint64_t nr_memslot_modifications;
     93	bool partition_vcpu_memory_access;
     94};
     95
     96static void run_test(enum vm_guest_mode mode, void *arg)
     97{
     98	struct test_params *p = arg;
     99	struct kvm_vm *vm;
    100
    101	vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
    102				 VM_MEM_SRC_ANONYMOUS,
    103				 p->partition_vcpu_memory_access);
    104
    105	pr_info("Finished creating vCPUs\n");
    106
    107	perf_test_start_vcpu_threads(nr_vcpus, vcpu_worker);
    108
    109	pr_info("Started all vCPUs\n");
    110
    111	add_remove_memslot(vm, p->memslot_modification_delay,
    112			   p->nr_memslot_modifications);
    113
    114	run_vcpus = false;
    115
    116	perf_test_join_vcpu_threads(nr_vcpus);
    117	pr_info("All vCPU threads joined\n");
    118
    119	perf_test_destroy_vm(vm);
    120}
    121
    122static void help(char *name)
    123{
    124	puts("");
    125	printf("usage: %s [-h] [-m mode] [-d delay_usec]\n"
    126	       "          [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
    127	guest_modes_help();
    128	printf(" -d: add a delay between each iteration of adding and\n"
    129	       "     deleting a memslot in usec.\n");
    130	printf(" -b: specify the size of the memory region which should be\n"
    131	       "     accessed by each vCPU. e.g. 10M or 3G.\n"
    132	       "     Default: 1G\n");
    133	printf(" -v: specify the number of vCPUs to run.\n");
    134	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
    135	       "     them into a separate region of memory for each vCPU.\n");
    136	printf(" -i: specify the number of iterations of adding and removing\n"
    137	       "     a memslot.\n"
    138	       "     Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
    139	puts("");
    140	exit(0);
    141}
    142
    143int main(int argc, char *argv[])
    144{
    145	int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
    146	int opt;
    147	struct test_params p = {
    148		.memslot_modification_delay = 0,
    149		.nr_memslot_modifications =
    150			DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
    151		.partition_vcpu_memory_access = true
    152	};
    153
    154	guest_modes_append_default();
    155
    156	while ((opt = getopt(argc, argv, "hm:d:b:v:oi:")) != -1) {
    157		switch (opt) {
    158		case 'm':
    159			guest_modes_cmdline(optarg);
    160			break;
    161		case 'd':
    162			p.memslot_modification_delay = strtoul(optarg, NULL, 0);
    163			TEST_ASSERT(p.memslot_modification_delay >= 0,
    164				    "A negative delay is not supported.");
    165			break;
    166		case 'b':
    167			guest_percpu_mem_size = parse_size(optarg);
    168			break;
    169		case 'v':
    170			nr_vcpus = atoi(optarg);
    171			TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
    172				    "Invalid number of vcpus, must be between 1 and %d",
    173				    max_vcpus);
    174			break;
    175		case 'o':
    176			p.partition_vcpu_memory_access = false;
    177			break;
    178		case 'i':
    179			p.nr_memslot_modifications = atoi(optarg);
    180			break;
    181		case 'h':
    182		default:
    183			help(argv[0]);
    184			break;
    185		}
    186	}
    187
    188	for_each_guest_mode(run_test, &p);
    189
    190	return 0;
    191}