From 572985d705e4575d53ec5d312a484a96a01bce9f Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Thu, 6 Oct 2022 12:02:32 +0200 Subject: Move cachepc ioctl into kvm device and remove old procfs endpoint --- test/.gitignore | 1 + test/access.c | 12 ++++++------ test/eviction.c | 16 +++++++--------- test/kvm.c | 29 +++++++++++++++-------------- test/sev-es.c | 24 ++++++++++-------------- test/sev.c | 24 ++++++++++-------------- test/sevstep | Bin 1287616 -> 0 bytes test/sevstep.c | 2 +- 8 files changed, 50 insertions(+), 58 deletions(-) delete mode 100755 test/sevstep (limited to 'test') diff --git a/test/.gitignore b/test/.gitignore index 9815ec9..c2e8d30 100755 --- a/test/.gitignore +++ b/test/.gitignore @@ -4,3 +4,4 @@ kvm sev sev-es sev-snp +sevstep diff --git a/test/access.c b/test/access.c index 1e38e1e..dc704fb 100644 --- a/test/access.c +++ b/test/access.c @@ -16,15 +16,15 @@ main(int argc, const char **argv) int fd, ret; size_t i; - fd = open("/proc/cachepc", O_RDONLY); + fd = open("/dev/kvm", O_RDONLY); if (fd < 0) err(1, "open"); - for (i = 0; i < 50; i++) { + for (i = 0; i < 100; i++) { arg = 48; /* target set */ - ret = ioctl(fd, CACHEPC_IOCTL_TEST_ACCESS, &arg); - if (ret == -1) err(1, "ioctl fail"); - printf("%i\n", arg); + ret = ioctl(fd, KVM_CPC_TEST_ACCESS, &arg); + if (ret == -1) err(1, "ioctl TEST_ACCESS"); + if (arg != 1) errx(1, "access result (%i) != 1", arg); } - + close(fd); } diff --git a/test/eviction.c b/test/eviction.c index 9fb57b5..9ad6e56 100644 --- a/test/eviction.c +++ b/test/eviction.c @@ -14,28 +14,26 @@ main(int argc, const char **argv) { uint16_t counts[64]; uint32_t arg; - size_t i, len; - int fd, ret; + int i, fd, ret; - fd = open("/proc/cachepc", O_RDONLY); + fd = open("/dev/kvm", O_RDONLY); if (fd < 0) err(1, "open"); arg = 48; if (argc == 2) arg = atoi(argv[1]); - ret = ioctl(fd, CACHEPC_IOCTL_TEST_EVICTION, &arg); - if (ret == -1) err(1, "ioctl"); + ret = ioctl(fd, KVM_CPC_TEST_EVICTION, &arg); + if (ret == -1) err(1, "ioctl TEST_EVICTION"); - len = read(fd, counts, sizeof(counts)); - if (len != sizeof(counts)) - errx(1, "invalid count read"); + ret = ioctl(fd, KVM_CPC_READ_COUNTS, counts); + if (ret == -1) err(1, "ioctl READ_COUNTS"); for (i = 0; i < 64; i++) { if (i % 16 == 0 && i) printf("\n"); if (counts[i] > 0) printf("\x1b[91m"); - printf("%2lu ", i); + printf("%2i ", i); if (counts[i] > 0) printf("\x1b[0m"); } diff --git a/test/kvm.c b/test/kvm.c index 917ed5e..9632fec 100644 --- a/test/kvm.c +++ b/test/kvm.c @@ -49,7 +49,7 @@ extern uint8_t __stop_guest_without[]; static struct kvm kvm; static struct kvm_run *kvm_run; -static int cachepc_fd; +static int kvm_fd; #define TARGET_CACHE_LINESIZE 64 #define TARGET_SET 15 @@ -189,14 +189,15 @@ kvm_init(size_t ramsize, void *code_start, void *code_stop) } uint16_t * -read_counts() +read_counts() { - uint16_t *counts = (uint16_t *)malloc(64*sizeof(uint16_t)); - size_t len; + uint16_t *counts; + int ret; - lseek(cachepc_fd, 0, SEEK_SET); - len = read(cachepc_fd, counts, 64 * sizeof(uint16_t)); - assert(len == 64 * sizeof(uint16_t)); + counts = malloc(64 * sizeof(uint16_t)); + if (!counts) err(1, "malloc"); + ret = ioctl(kvm_fd, KVM_CPC_READ_COUNTS, counts); + if (ret == -1) err(1, "ioctl READ_COUNTS"); return counts; } @@ -272,16 +273,16 @@ main(int argc, const char **argv) pin_process(0, TARGET_CORE, true); - cachepc_fd = open("/proc/cachepc", O_RDONLY); - if (cachepc_fd < 0) err(1, "open"); + kvm_fd = open("/dev/kvm", O_RDONLY); + if (kvm_fd < 0) err(1, "open"); - /* init L1 miss counter */ + /* init L1 miss counter for host kernel */ arg = 0x002264D8; - ret = ioctl(cachepc_fd, CACHEPC_IOCTL_INIT_PMC, &arg); - if (ret == -1) err(1, "ioctl fail"); + ret = ioctl(kvm_fd, KVM_CPC_INIT_PMC, &arg); + if (ret == -1) err(1, "ioctl INIT_PMC"); baseline = calloc(sizeof(uint16_t), 64); - if (!baseline) err(1, "counts"); + if (!baseline) err(1, "calloc"); for (k = 0; k < 64; k++) baseline[k] = UINT16_MAX; @@ -319,6 +320,6 @@ main(int argc, const char **argv) } free(baseline); - close(cachepc_fd); + close(kvm_fd); } diff --git a/test/sev-es.c b/test/sev-es.c index cd67649..bf534d9 100644 --- a/test/sev-es.c +++ b/test/sev-es.c @@ -58,7 +58,7 @@ ssize_t sysret; pid_t victim_pid; /* ioctl dev fds */ -int kvm_dev, sev_dev, cachepc_dev; +int kvm_dev, sev_dev, kvm_dev; enum { GSTATE_UNINIT, @@ -400,15 +400,15 @@ sev_kvm_deinit(struct kvm *kvm) } uint16_t * -read_counts() +read_counts() { uint16_t *counts; - size_t len; + int ret; counts = malloc(64 * sizeof(uint16_t)); - lseek(cachepc_dev, 0, SEEK_SET); - len = read(cachepc_dev, counts, 64 * sizeof(uint16_t)); - assert(len == 64 * sizeof(uint16_t)); + if (!counts) err(1, "malloc"); + ret = ioctl(kvm_dev, KVM_CPC_READ_COUNTS, counts); + if (ret == -1) err(1, "ioctl READ_COUNTS"); return counts; } @@ -470,9 +470,6 @@ main(int argc, const char **argv) pin_process(0, TARGET_CORE, true); - cachepc_dev = open("/proc/cachepc", O_RDONLY); - if (cachepc_dev < 0) err(1, "open /proc/cachepc"); - sev_dev = open("/dev/sev", O_RDWR | O_CLOEXEC); if (sev_dev < 0) err(1, "open /dev/sev"); @@ -484,13 +481,13 @@ main(int argc, const char **argv) if (ret < 0) err(1, "KVM_GET_API_VERSION"); if (ret != 12) errx(1, "KVM_GET_API_VERSION %d, expected 12", ret); - // Init L1 miss counter + /* init L1 miss counter for host kernel */ arg = 0x002264D8; - ret = ioctl(cachepc_dev, CACHEPC_IOCTL_INIT_PMC, &arg); - if (ret < 0) err(1, "ioctl fail"); + ret = ioctl(kvm_dev, KVM_CPC_INIT_PMC, &arg); + if (ret < 0) err(1, "ioctl INIT_PMC"); baseline = calloc(sizeof(uint16_t), 64); - if (!baseline) err(1, "counts"); + if (!baseline) err(1, "calloc"); for (k = 0; k < 64; k++) baseline[k] = UINT16_MAX; @@ -541,7 +538,6 @@ main(int argc, const char **argv) free(baseline); - close(cachepc_dev); close(kvm_dev); close(sev_dev); } diff --git a/test/sev.c b/test/sev.c index 61f1ec3..7d99fd1 100644 --- a/test/sev.c +++ b/test/sev.c @@ -58,7 +58,7 @@ ssize_t sysret; pid_t victim_pid; /* ioctl dev fds */ -int kvm_dev, sev_dev, cachepc_dev; +int kvm_dev, sev_dev, kvm_dev; enum { GSTATE_UNINIT, @@ -399,15 +399,15 @@ sev_kvm_deinit(struct kvm *kvm) } uint16_t * -read_counts() +read_counts() { uint16_t *counts; - size_t len; + int ret; counts = malloc(64 * sizeof(uint16_t)); - lseek(cachepc_dev, 0, SEEK_SET); - len = read(cachepc_dev, counts, 64 * sizeof(uint16_t)); - assert(len == 64 * sizeof(uint16_t)); + if (!counts) err(1, "malloc"); + ret = ioctl(kvm_dev, KVM_CPC_READ_COUNTS, counts); + if (ret == -1) err(1, "ioctl READ_COUNTS"); return counts; } @@ -478,9 +478,6 @@ main(int argc, const char **argv) pin_process(0, TARGET_CORE, true); - cachepc_dev = open("/proc/cachepc", O_RDONLY); - if (cachepc_dev < 0) err(1, "open /proc/cachepc"); - sev_dev = open("/dev/sev", O_RDWR | O_CLOEXEC); if (sev_dev < 0) err(1, "open /dev/sev"); @@ -492,13 +489,13 @@ main(int argc, const char **argv) if (ret < 0) err(1, "KVM_GET_API_VERSION"); if (ret != 12) errx(1, "KVM_GET_API_VERSION %d, expected 12", ret); - // Init L1 miss counter + /* init L1 miss counter for host kernel */ arg = 0x002264D8; - ret = ioctl(cachepc_dev, CACHEPC_IOCTL_INIT_PMC, &arg); - if (ret < 0) err(1, "ioctl fail"); + ret = ioctl(kvm_dev, KVM_CPC_INIT_PMC, &arg); + if (ret < 0) err(1, "ioctl INIT_PMC"); baseline = calloc(sizeof(uint16_t), 64); - if (!baseline) err(1, "counts"); + if (!baseline) err(1, "calloc"); for (k = 0; k < 64; k++) baseline[k] = UINT16_MAX; @@ -537,7 +534,6 @@ main(int argc, const char **argv) free(baseline); - close(cachepc_dev); close(kvm_dev); close(sev_dev); } diff --git a/test/sevstep b/test/sevstep deleted file mode 100755 index b9fe9ea..0000000 Binary files a/test/sevstep and /dev/null differ diff --git a/test/sevstep.c b/test/sevstep.c index 907a773..259010e 100644 --- a/test/sevstep.c +++ b/test/sevstep.c @@ -15,7 +15,7 @@ main(int argc, const char **argv) track_all_pages_t tracking; int ret, fd; - fd = open("/proc/cachepc", O_RDONLY); + fd = open("/dev/kvm", O_RDONLY); if (!fd) err(1, "open"); tracking.track_mode = KVM_PAGE_TRACK_ACCESS; -- cgit v1.2.3-71-gd317