Skip to content

Instantly share code, notes, and snippets.

@tklengyel
Created August 25, 2017 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tklengyel/a0615c69aa6e377efc28a7f63c34a47a to your computer and use it in GitHub Desktop.
Save tklengyel/a0615c69aa6e377efc28a7f63c34a47a to your computer and use it in GitHub Desktop.
Xen foreign memory mapping
/*
* gcc -o xen_memmap -lxenctrl xen_memmap.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#define XC_WANT_COMPAT_MAP_FOREIGN_API 1
#include <xenctrl.h>
int main(int argc, char **argv) {
xc_interface *xc = xc_interface_open(0, 0, 0);
if (!xc) {
fprintf(stderr, "xc_interface_open() failed!\n");
return 0;
}
unsigned long pfn = strtoul(argv[1], NULL, 0);
domid_t domainid = 1;
void *memory = xc_map_foreign_range(xc, domainid, XC_PAGE_SIZE, PROT_READ, (unsigned long) pfn);
if ( memory ) {
/* Page is now mapped */
munmap(memory, XC_PAGE_SIZE);
}
xc_interface_close(xc);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment