From 27ac7a95b69d70622f281c1b8d0300d38e5c541d Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Mon, 30 Jan 2023 11:25:17 +0100 Subject: Added mainpfn guest utility to determine rough pfn for userspace code --- Makefile | 25 ++++++++++++++++--------- test/.gitignore | 1 + test/qemu-pagestep | Bin 98504 -> 108168 bytes util/.gitignore | 2 +- util/mainpfn.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 util/mainpfn.c diff --git a/Makefile b/Makefile index b5af6af..8e6a872 100755 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ BINS += test/kvm-pagestep test/kvm-pagestep_guest BINS += test/qemu-pagestep BINS += test/qemu-eviction test/qemu-eviction_guest # BINS += test/qemu-aes_guest test/qemu-aes -BINS += util/debug util/reset +BINS += util/debug util/reset util/mainpfn CFLAGS = -I . -I linux/usr/include CFLAGS += -g -Wunused-variable -Wunknown-pragmas -Wunused-function @@ -22,8 +22,11 @@ GUEST_CFLAGS = $(CFLAGS) -static LDLIBS = -lpthread -TEST_SRCS = test/util.c test/util.h test/kvm.c test/kvm.h -TEST_SRCS += cachepc/uapi.h cachepc/const.h +UTIL_HDRS = cachepc/uapi.h cachepc/const.h +UTIL_SRCS = + +TEST_HDRS = cachepc/uapi.h cachepc/const.h test/util.h test/kvm.h +TEST_SRCS = test/util.c test/kvm.c all: build $(BINS) @@ -62,16 +65,20 @@ prep: sudo cpupower frequency-set -d 3.7GHz -u 3.7GHz sudo bash -c "for f in /proc/irq/*/smp_affinity; do echo 1 > \$$f 2>/dev/null; done" -util/%: util/%.c $(CACHEPC_UAPI) +util/%: util/%.c $(UTIL_SRCS) + $(CC) -o $@ $< $(HOST_CFLAGS) + +util/mainpfn: util/mainpfn.c $(UTIL_SRCS) + $(CC) -o $@ $< $(GUEST_CFLAGS) -test/%.o: test/%.c - $(CC) -c -o $@ $^ $(CFLAGS) +test/%.o: test/%.c $(TEST_HDRS) + $(CC) -c -o $@ $< $(HOST_CFLAGS) -test/%.o: test/%.S - $(CC) -c -o $@ $^ $(CFLAGS) +test/%.o: test/%.S $(TEST_HDRS) + $(CC) -c -o $@ $< $(HOST_CFLAGS) test/%: test/%.c $(TEST_SRCS) - $(CC) -o $@ $(filter %.c,$^) $(filter %.S,$^) $(CFLAGS) $(LDLIBS) + $(CC) -o $@ $(filter %.c,$^) $(HOST_CFLAGS) $(LDLIBS) test/kvm-%_guest: test/kvm-%_guest.o test/kvm-guest.lds $(LD) -Ttest/kvm-guest.lds -o $@ $< diff --git a/test/.gitignore b/test/.gitignore index ad9a84a..cea8b5a 100755 --- a/test/.gitignore +++ b/test/.gitignore @@ -9,6 +9,7 @@ kvm-pagestep kvm-pagestep_guest qemu-eviction qemu-eviction_guest +qemu-pagestep qemu-aes qemu-aes_guest qemu-poc diff --git a/test/qemu-pagestep b/test/qemu-pagestep index c8a6690..0526960 100755 Binary files a/test/qemu-pagestep and b/test/qemu-pagestep differ diff --git a/util/.gitignore b/util/.gitignore index ef5bc67..dba752f 100644 --- a/util/.gitignore +++ b/util/.gitignore @@ -1,3 +1,3 @@ debug -svme reset +mainpfn diff --git a/util/mainpfn.c b/util/mainpfn.c new file mode 100644 index 0000000..17c1b18 --- /dev/null +++ b/util/mainpfn.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include + +struct pageinfo { + uint64_t pfn : 54; + unsigned int soft_dirty : 1; + unsigned int file_page : 1; + unsigned int swapped : 1; + unsigned int present : 1; +}; + +void +pagemap_get_entry(struct pageinfo *entry, int fd, uintptr_t vaddr) +{ + uint64_t data; + size_t offset; + + offset = (vaddr / sysconf(_SC_PAGE_SIZE)) * 8; + if (pread(fd, (void *) &data, 8, offset) != 8) + err(1, "pread"); + + entry->pfn = data & ((1ULL << 54) - 1); + entry->soft_dirty = (data >> 54) & 1; + entry->file_page = (data >> 61) & 1; + entry->swapped = (data >> 62) & 1; + entry->present = (data >> 63) & 1; +} + +int +main(int argc, const char **argv) +{ + char filepath[256]; + struct pageinfo info; + pid_t pid; + int fd; + + pid = getpid(); + snprintf(filepath, sizeof(filepath), "/proc/%u/pagemap", pid); + + fd = open(filepath, O_RDONLY); + if (!fd) err(1, "open"); + + pagemap_get_entry(&info, fd, (uintptr_t) main); + printf("PFN: %08lx\n", info.pfn); + + close(fd); +} -- cgit v1.2.3-71-gd317