cachepc

Prime+Probe cache-based side-channel attack on AMD SEV-SNP protected virtual machines
git clone https://git.sinitax.com/sinitax/cachepc
Log | Files | Refs | Submodules | README | sfeed.txt

mainpfn.c (1033B)


      1#include <unistd.h>
      2#include <fcntl.h>
      3#include <err.h>
      4#include <stdio.h>
      5#include <stdint.h>
      6#include <stdlib.h>
      7
      8struct pageinfo {
      9	uint64_t pfn : 54;
     10	unsigned int soft_dirty : 1;
     11	unsigned int file_page : 1;
     12	unsigned int swapped : 1;
     13	unsigned int present : 1;
     14};
     15
     16void
     17pagemap_get_entry(struct pageinfo *entry, int fd, uintptr_t vaddr)
     18{
     19	uint64_t data;
     20	size_t offset;
     21
     22	offset = (vaddr / sysconf(_SC_PAGE_SIZE)) * 8;
     23	if (pread(fd, (void *) &data, 8, offset) != 8)
     24		err(1, "pread");
     25
     26	entry->pfn = data & ((1ULL << 54) - 1);
     27	entry->soft_dirty = (data >> 54) & 1;
     28	entry->file_page = (data >> 61) & 1;
     29	entry->swapped = (data >> 62) & 1;
     30	entry->present = (data >> 63) & 1;
     31}
     32
     33int
     34main(int argc, const char **argv)
     35{
     36	char filepath[256];
     37	struct pageinfo info;
     38	pid_t pid;
     39	int fd;
     40
     41	pid = getpid();
     42	snprintf(filepath, sizeof(filepath), "/proc/%u/pagemap", pid);
     43
     44	fd = open(filepath, O_RDONLY);
     45	if (!fd) err(1, "open");
     46
     47	pagemap_get_entry(&info, fd, (uintptr_t) main);
     48	printf("PFN: %08lx\n", info.pfn);
     49
     50	close(fd);
     51}