diff --git a/u2mfn/u2mfnlib.c b/u2mfn/u2mfnlib.c index 998f47e9..dff06e43 100644 --- a/u2mfn/u2mfnlib.c +++ b/u2mfn/u2mfnlib.c @@ -31,11 +31,24 @@ static int u2mfn_fd = -1; static int get_fd() { - if (u2mfn_fd == -1) { - u2mfn_fd = open("/proc/u2mfn", O_RDWR); - if (u2mfn_fd < 0) - return -1; - } + if (u2mfn_fd == -1) + u2mfn_fd = u2mfn_get_fd(); + if (u2mfn_fd < 0) + return -1; + return 0; +} + +int u2mfn_get_fd() +{ + return open("/proc/u2mfn", O_RDWR); +} + +int u2mfn_get_mfn_for_page_with_fd(int fd, long va, int *mfn) +{ + *mfn = ioctl(fd, U2MFN_GET_MFN_FOR_PAGE, va); + if (*mfn == -1) + return -1; + return 0; } @@ -43,7 +56,12 @@ int u2mfn_get_mfn_for_page(long va, int *mfn) { if (get_fd()) return -1; - *mfn = ioctl(u2mfn_fd, U2MFN_GET_MFN_FOR_PAGE, va); + return u2mfn_get_mfn_for_page_with_fd(u2mfn_fd, va, mfn); +} + +int u2mfn_get_last_mfn_with_fd(int fd, int *mfn) +{ + *mfn = ioctl(fd, U2MFN_GET_LAST_MFN, 0); if (*mfn == -1) return -1; @@ -54,22 +72,20 @@ int u2mfn_get_last_mfn(int *mfn) { if (get_fd()) return -1; - - *mfn = ioctl(u2mfn_fd, U2MFN_GET_LAST_MFN, 0); - if (*mfn == -1) - return -1; - - return 0; + return u2mfn_get_last_mfn_with_fd(u2mfn_fd, mfn); } - +char *u2mfn_alloc_kpage_with_fd(int fd) +{ + char *ret; + ret = + mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + return ret; +} char *u2mfn_alloc_kpage() { - char *ret; if (get_fd()) return MAP_FAILED; - ret = - mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, u2mfn_fd, 0); - return ret; + return u2mfn_alloc_kpage_with_fd(u2mfn_fd); } diff --git a/u2mfn/u2mfnlib.h b/u2mfn/u2mfnlib.h index e64431af..a7c20972 100644 --- a/u2mfn/u2mfnlib.h +++ b/u2mfn/u2mfnlib.h @@ -19,6 +19,10 @@ * */ +int u2mfn_get_fd(); int u2mfn_get_mfn_for_page(long va, int *mfn) ; +int u2mfn_get_mfn_for_page_with_fd(int fd, long va, int *mfn) ; int u2mfn_get_last_mfn(int *mfn) ; -char *u2mfn_alloc_kpage(void) ; +int u2mfn_get_last_mfn_with_fd(int fd, int *mfn) ; +char *u2mfn_alloc_kpage(void); +char *u2mfn_alloc_kpage_with_fd(int fd);