libxc: osdep: convert hypercall buffer allocation
PATCH 1/1 libxc: osdep: convert hypercall buffer allocation
# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1299488485 0
# Node ID 72cbc9120370ae9d04ad93a9bf11577a070d8dc2 # Parent f2212579853a585a0cfdb3533a7136b5c2069ea7
libxc: osdep: convert hypercall buffer allocation
tools/libxc/xc_hcall_buf.c +9 -36
@@ -17,7 +17,7 @@*/ #include <stdlib.h> -#include <malloc.h> +#include <string.h> #include <pthread.h> #include "xc_private.h"
@@ -95,15 +95,6 @@ static int hypercall_buffer_cache_free(xreturn rc; } -static void do_hypercall_buffer_free_pages (void *ptr,int nr_pages) -{ -#ifndef __sun__ - (void )munlock (ptr, nr_pages *PAGE_SIZE ); -#endif - -free (ptr); -} -void xc__hypercall_buffer_cache_release (xc_interface *xch) {void *p;
@@ -126,7 +117,7 @@ void xc__hypercall_buffer_cache_release(while ( xch->hypercall_buffer_cache_nr >0 ) { p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr]; -do_hypercall_buffer_free_pages (p,1 ); + xch->ops->u.privcmd.free_hypercall_buffer (xch, xch->ops_handle, p,1 ); }hypercall_buffer_cache_unlock (xch);
@@ -134,36 +125,18 @@ void xc__hypercall_buffer_cache_release(void *xc__hypercall_buffer_alloc_pages (xc_interface *xch,xc_hypercall_buffer_t *b,int nr_pages) { -size_t size = nr_pages *PAGE_SIZE ;void *p =hypercall_buffer_cache_alloc (xch, nr_pages); -if ( !p ) { -#if defined(_POSIX_C_SOURCE) && !defined(__sun__) -int ret; - ret =posix_memalign (&p,PAGE_SIZE , size); -if (ret !=0 ) -return NULL ; -#elif defined(__NetBSD__) || defined(__OpenBSD__) - p =valloc (size); -#else - p =memalign (PAGE_SIZE , size); -#endif +if ( !p ) + p = xch->ops->u.privcmd.alloc_hypercall_buffer (xch, xch->ops_handle, nr_pages); -if (!p) -return NULL ; - -#ifndef __sun__ -if (mlock (p, size) <0 ) - { -free (p); -return NULL ; - } -#endif - } +if (!p) +return NULL ; b->hbuf = p; -memset (p,0 , size); +memset (p,0 , nr_pages *PAGE_SIZE ); +return b->hbuf; }
@@ -173,7 +146,7 @@ void xc__hypercall_buffer_free_pages(xc_return ;if ( !hypercall_buffer_cache_free (xch, b->hbuf, nr_pages) ) -do_hypercall_buffer_free_pages (b->hbuf, nr_pages); + xch->ops->u.privcmd.free_hypercall_buffer (xch, xch->ops_handle, b->hbuf, nr_pages); }struct allocation_header {
tools/libxc/xc_linux_osdep.c +27 -0
@@ -84,6 +84,30 @@ static int linux_privcmd_close(xc_interf {int fd = (int )h;return close (fd); +} + +static void *linux_privcmd_alloc_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,int npages) +{ +size_t size = npages *XC_PAGE_SIZE ; +void *p; +int ret; + + ret =posix_memalign (&p,XC_PAGE_SIZE , size); +if (ret !=0 || !p) +return NULL ; + +if (mlock (p, size) <0 ) + { +free (p); +return NULL ; + } +return p; +} + +static void linux_privcmd_free_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,void *ptr,int npages) +{ + (void )munlock (ptr, npages *XC_PAGE_SIZE ); +free (ptr); }static int linux_privcmd_hypercall (xc_interface *xch,xc_osdep_handle h,privcmd_hypercall_t *hypercall)
@@ -344,6 +368,9 @@ static struct xc_osdep_ops linux_privcmd .close = &linux_privcmd_close, .u.privcmd = { + .alloc_hypercall_buffer = &linux_privcmd_alloc_hypercall_buffer, + .free_hypercall_buffer = &linux_privcmd_free_hypercall_buffer, + .hypercall = &linux_privcmd_hypercall, .map_foreign_batch = &linux_privcmd_map_foreign_batch,
tools/libxc/xc_minios.c +14 -0
@@ -37,6 +37,7 @@#include <assert.h> #include <stdint.h> #include <inttypes.h> +#include <malloc.h> #include "xc_private.h"
@@ -68,6 +69,16 @@ void minios_interface_close_fd(int fd)void minios_interface_close_fd (int fd) { files[fd].type =FTYPE_NONE ; +} + +static void *minios_privcmd_alloc_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,int npages) +{ +return memalign (PAGE_SIZE , npages *PAGE_SIZE ); +} + +static void minios_privcmd_free_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,void *ptr,int npages) +{ +free (ptr); }static int minios_privcmd_hypercall (xc_interface *xch,xc_osdep_handle h,privcmd_hypercall_t *hypercall)
@@ -187,6 +198,9 @@ static struct xc_osdep_ops minios_privcm .close = &minios_privcmd_close, .u.privcmd = { + .alloc_hypercall_buffer = &minios_privcmd_alloc_hypercall_buffer, + .free_hypercall_buffer = &minios_privcmd_free_hypercall_buffer, + .hypercall = &minios_privcmd_hypercall, .map_foreign_batch = &minios_privcmd_map_foreign_batch,
tools/libxc/xc_netbsd.c +27 -0
@@ -23,6 +23,8 @@#include <xen/sys/evtchn.h> #include <unistd.h> #include <fcntl.h> +#include <malloc.h> +#include <sys/mman.h> static xc_osdep_handle netbsd_privcmd_open (xc_interface *xch) {
@@ -64,6 +66,28 @@ static int netbsd_privcmd_close(xc_inter {int fd = (int )h;return close (fd); +} + +static void *netbsd_privcmd_alloc_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,int npages) +{ +size_t size = npages *XC_PAGE_SIZE ; +void *p =valloc (size); + +if (!p) +return NULL ; + +if (mlock (p, size) <0 ) + { +free (p); +return NULL ; + } +return p; +} + +static void netbsd_privcmd_free_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,void *ptr,int npages) +{ + (void )munlock (ptr, npages *XC_PAGE_SIZE ); +free (ptr); }static int netbsd_privcmd_hypercall (xc_interface *xch,xc_osdep_handle h,privcmd_hypercall_t *hypercall)
@@ -181,6 +205,9 @@ static struct xc_osdep_ops netbsd_privcm .close = &netbsd_privcmd_close, .u.privcmd = { + .alloc_hypercall_buffer = &netbsd_privcmd_alloc_hypercall_buffer, + .free_hypercall_buffer = &netbsd_privcmd_free_hypercall_buffer, + .hypercall = &netbsd_privcmd_hypercall, .map_foreign_batch = &netbsd_privcmd_map_foreign_batch,
tools/libxc/xc_solaris.c +14 -0
@@ -24,6 +24,7 @@#include <xen/sys/evtchn.h> #include <unistd.h> #include <fcntl.h> +#include <malloc.h> static xc_osdep_handle solaris_privcmd_open (xc_interface *xch) {
@@ -65,6 +66,16 @@ static int solaris_privcmd_close(xc_inte {int fd = (int )h;return close (fd); +} + +static void *solaris_privcmd_alloc_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,int npages) +{ +return memalign (XC_PAGE_SIZE , npages *XC_PAGE_SIZE ); +} + +static void solaris_privcmd_free_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,void *ptr,int npages) +{ +free (ptr); }static int solaris_privcmd_hypercall (xc_interface *xch,xc_osdep_handle h,privcmd_hypercall_t *hypercall)
@@ -172,6 +183,9 @@ static struct xc_osdep_ops solaris_privc .close = &solaris_privcmd_close, .u.privcmd = { + .alloc_hypercall_buffer = &solaris_privcmd_alloc_hypercall_buffer, + .free_hypercall_buffer = &solaris_privcmd_free_hypercall_buffer, + .hypercall = &solaris_privcmd_hypercall; .map_foreign_batch = &solaris_privcmd_map_foreign_batch,
tools/libxc/xenctrlosdep.h +3 -0
@@ -74,6 +74,9 @@ struct xc_osdep_ops union { struct { + void *(*alloc_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, int npages); + void (*free_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages); + int (*hypercall)(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall); void *(*map_foreign_batch)(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int prot,
Quoted 6 lines · Ian Campbell
# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1299488485 0
# Node ID72cbc9120370ae9d04ad93a9bf11577a070d8dc2# Parentf2212579853a585a0cfdb3533a7136b5c2069ea7
libxc: osdep: convert hypercall buffer allocation
I should have said something like:
This will allow us to use OS specific interfaces to ensure that the allocated memory is safe for use as a hypercall buffer in the future.
Ian.
Quoted 276 lines
Signed-off-by Ian Campbell <[email protected]>diff -r
f2212579853a-r72cbc9120370tools/libxc/xc_hcall_buf.ctools/libxc/xc_hcall_buf.c +9
-36@@ -17,7 +17,7 @@*/ #include <stdlib.h> -#include <malloc.h> +#include <string.h> #include <pthread.h> #include "xc_private.h" @@ -95,15 +95,6 @@ static int hypercall_buffer_cache_free(xreturn rc; } -static void do_hypercall_buffer_free_pages (void *ptr,int nr_pages) -{ -#ifndef __sun__ - (void )munlock (ptr, nr_pages *PAGE_SIZE ); -#endif - -free (ptr); -} -void xc__hypercall_buffer_cache_release (xc_interface *xch) {void *p;@@ -126,7 +117,7 @@ void xc__hypercall_buffer_cache_release(while ( xch->hypercall_buffer_cache_nr >0 ) { p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr]; -do_hypercall_buffer_free_pages (p,1 ); + xch->ops->u.privcmd.free_hypercall_buffer (xch, xch->ops_handle, p,1 ); }hypercall_buffer_cache_unlock (xch);@@ -134,36 +125,18 @@ void xc__hypercall_buffer_cache_release(void *xc__hypercall_buffer_alloc_pages (xc_interface *xch,xc_hypercall_buffer_t *b,int nr_pages) { -size_t size = nr_pages *PAGE_SIZE ;void *p =hypercall_buffer_cache_alloc (xch, nr_pages); -if ( !p ) { -#if defined(_POSIX_C_SOURCE) && !defined(__sun__) -int ret; - ret =posix_memalign (&p,PAGE_SIZE , size); -if (ret !=0 ) -return NULL ; -#elif defined(__NetBSD__) || defined(__OpenBSD__) - p =valloc (size); -#else - p =memalign (PAGE_SIZE , size); -#endif +if ( !p ) + p = xch->ops->u.privcmd.alloc_hypercall_buffer (xch, xch->ops_handle, nr_pages); -if (!p) -return NULL ; - -#ifndef __sun__ -if (mlock (p, size) <0 ) - { -free (p); -return NULL ; - } -#endif - } +if (!p) +return NULL ; b->hbuf = p; -memset (p,0 , size); +memset (p,0 , nr_pages *PAGE_SIZE ); +return b->hbuf; }@@ -173,7 +146,7 @@ void xc__hypercall_buffer_free_pages(xc_return ;if ( !hypercall_buffer_cache_free (xch, b->hbuf, nr_pages) ) -do_hypercall_buffer_free_pages (b->hbuf, nr_pages); + xch->ops->u.privcmd.free_hypercall_buffer (xch, xch->ops_handle, b->hbuf, nr_pages); }struct allocation_header {diff -r
f2212579853a-r72cbc9120370tools/libxc/xc_linux_osdep.ctools/libxc/xc_linux_osdep.c +27
-0@@ -84,6 +84,30 @@ static int linux_privcmd_close(xc_interf {int fd = (int )h;return close (fd); +} + +static void *linux_privcmd_alloc_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,int npages) +{ +size_t size = npages *XC_PAGE_SIZE ; +void *p; +int ret; + + ret =posix_memalign (&p,XC_PAGE_SIZE , size); +if (ret !=0 || !p) +return NULL ; + +if (mlock (p, size) <0 ) + { +free (p); +return NULL ; + } +return p; +} + +static void linux_privcmd_free_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,void *ptr,int npages) +{ + (void )munlock (ptr, npages *XC_PAGE_SIZE ); +free (ptr); }static int linux_privcmd_hypercall (xc_interface *xch,xc_osdep_handle h,privcmd_hypercall_t *hypercall)@@ -344,6 +368,9 @@ static struct xc_osdep_ops linux_privcmd .close = &linux_privcmd_close, .u.privcmd = { + .alloc_hypercall_buffer = &linux_privcmd_alloc_hypercall_buffer, + .free_hypercall_buffer = &linux_privcmd_free_hypercall_buffer, + .hypercall = &linux_privcmd_hypercall, .map_foreign_batch = &linux_privcmd_map_foreign_batch,diff -r
f2212579853a-r72cbc9120370tools/libxc/xc_minios.ctools/libxc/xc_minios.c +14
-0@@ -37,6 +37,7 @@#include <assert.h> #include <stdint.h> #include <inttypes.h> +#include <malloc.h> #include "xc_private.h" @@ -68,6 +69,16 @@ void minios_interface_close_fd(int fd)void minios_interface_close_fd (int fd) { files[fd].type =FTYPE_NONE ; +} + +static void *minios_privcmd_alloc_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,int npages) +{ +return memalign (PAGE_SIZE , npages *PAGE_SIZE ); +} + +static void minios_privcmd_free_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,void *ptr,int npages) +{ +free (ptr); }static int minios_privcmd_hypercall (xc_interface *xch,xc_osdep_handle h,privcmd_hypercall_t *hypercall)@@ -187,6 +198,9 @@ static struct xc_osdep_ops minios_privcm .close = &minios_privcmd_close, .u.privcmd = { + .alloc_hypercall_buffer = &minios_privcmd_alloc_hypercall_buffer, + .free_hypercall_buffer = &minios_privcmd_free_hypercall_buffer, + .hypercall = &minios_privcmd_hypercall, .map_foreign_batch = &minios_privcmd_map_foreign_batch,diff -r
f2212579853a-r72cbc9120370tools/libxc/xc_netbsd.ctools/libxc/xc_netbsd.c +27
-0@@ -23,6 +23,8 @@#include <xen/sys/evtchn.h> #include <unistd.h> #include <fcntl.h> +#include <malloc.h> +#include <sys/mman.h> static xc_osdep_handle netbsd_privcmd_open (xc_interface *xch) {@@ -64,6 +66,28 @@ static int netbsd_privcmd_close(xc_inter {int fd = (int )h;return close (fd); +} + +static void *netbsd_privcmd_alloc_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,int npages) +{ +size_t size = npages *XC_PAGE_SIZE ; +void *p =valloc (size); + +if (!p) +return NULL ; + +if (mlock (p, size) <0 ) + { +free (p); +return NULL ; + } +return p; +} + +static void netbsd_privcmd_free_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,void *ptr,int npages) +{ + (void )munlock (ptr, npages *XC_PAGE_SIZE ); +free (ptr); }static int netbsd_privcmd_hypercall (xc_interface *xch,xc_osdep_handle h,privcmd_hypercall_t *hypercall)@@ -181,6 +205,9 @@ static struct xc_osdep_ops netbsd_privcm .close = &netbsd_privcmd_close, .u.privcmd = { + .alloc_hypercall_buffer = &netbsd_privcmd_alloc_hypercall_buffer, + .free_hypercall_buffer = &netbsd_privcmd_free_hypercall_buffer, + .hypercall = &netbsd_privcmd_hypercall, .map_foreign_batch = &netbsd_privcmd_map_foreign_batch,diff -r
f2212579853a-r72cbc9120370tools/libxc/xc_solaris.ctools/libxc/xc_solaris.c +14
-0@@ -24,6 +24,7 @@#include <xen/sys/evtchn.h> #include <unistd.h> #include <fcntl.h> +#include <malloc.h> static xc_osdep_handle solaris_privcmd_open (xc_interface *xch) {@@ -65,6 +66,16 @@ static int solaris_privcmd_close(xc_inte {int fd = (int )h;return close (fd); +} + +static void *solaris_privcmd_alloc_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,int npages) +{ +return memalign (XC_PAGE_SIZE , npages *XC_PAGE_SIZE ); +} + +static void solaris_privcmd_free_hypercall_buffer (xc_interface *xch,xc_osdep_handle h,void *ptr,int npages) +{ +free (ptr); }static int solaris_privcmd_hypercall (xc_interface *xch,xc_osdep_handle h,privcmd_hypercall_t *hypercall)@@ -172,6 +183,9 @@ static struct xc_osdep_ops solaris_privc .close = &solaris_privcmd_close, .u.privcmd = { + .alloc_hypercall_buffer = &solaris_privcmd_alloc_hypercall_buffer, + .free_hypercall_buffer = &solaris_privcmd_free_hypercall_buffer, + .hypercall = &solaris_privcmd_hypercall; .map_foreign_batch = &solaris_privcmd_map_foreign_batch,diff -r
f2212579853a-r72cbc9120370tools/libxc/xenctrlosdep.htools/libxc/xenctrlosdep.h +3
-0@@ -74,6 +74,9 @@ struct xc_osdep_ops union { struct { + void *(*alloc_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, int npages); + void (*free_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages); + int (*hypercall)(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall); void *(*map_foreign_batch)(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int prot,
Ian Campbell writes ("[Xen-devel] [PATCH] libxc: osdep: convert hypercall buffer allocation"):
Quoted 1 line
libxc: osdep: convert hypercall buffer allocation
Thanks, but:
xc_pagetab.c: In function 'xc_translate_foreign_address':
xc_pagetab.c:64: error: implicit declaration of function 'xen_cr3_to_pfn_x86_64' xc_pagetab.c:68: error: implicit declaration of function 'xen_cr3_to_pfn_x86_32' make[1]: *** [xc_pagetab.o] Error 1
Ian.
Quoted 11 lines · Ian Jackson
Ian Campbell writes ("[Xen-devel] [PATCH] libxc: osdep: convert hypercall buffer allocation"):
Quoted 1 line
libxc: osdep: convert hypercall buffer allocation
Thanks, but:
xc_pagetab.c: In function 'xc_translate_foreign_address':
xc_pagetab.c:64: error: implicit declaration of function 'xen_cr3_to_pfn_x86_64' xc_pagetab.c:68: error: implicit declaration of function 'xen_cr3_to_pfn_x86_32' make[1]: *** [xc_pagetab.o] Error 1From this patch? Are you sure?
$ hg export libxc-osdep-hypercall-buffer.patch | diffstat -p1 tools/libxc/xc_hcall_buf.c | 45 ++++++++----------------------------------- tools/libxc/xc_linux_osdep.c | 27 +++++++++++++++++++++++++ tools/libxc/xc_minios.c | 14 +++++++++++++ tools/libxc/xc_netbsd.c | 27 +++++++++++++++++++++++++ tools/libxc/xc_solaris.c | 14 +++++++++++++ tools/libxc/xenctrlosdep.h | 3 ++ 6 files changed, 94 insertions(+), 36 deletions(-)
The only plausibly relevant change would be the .h which is:
tools/libxc/xenctrlosdep.h Mon Mar 07 09:01:25 2011 +0000 +3 -0
@@ -74,6 +74,9 @@ struct xc_osdep_ops union { struct { + void *(*alloc_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, int npages); + void (*free_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages); + int (*hypercall)(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall); void *(*map_foreign_batch)(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int prot,
Which doesn't seem likely.
Was this a stubdom or a regular tools build failure?
Ian.
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Ian Jackson
Ian Campbell writes ("[Xen-devel] [PATCH] libxc: osdep: convert hypercall buffer allocation"):
Quoted 1 line
libxc: osdep: convert hypercall buffer allocation
Quoted 8 lines
Quoted 8 lines
Thanks, but:
xc_pagetab.c: In function 'xc_translate_foreign_address':
xc_pagetab.c:64: error: implicit declaration of function 'xen_cr3_to_pfn_x86_64' xc_pagetab.c:68: error: implicit declaration of function 'xen_cr3_to_pfn_x86_32' make[1]: *** [xc_pagetab.o] Error 1From this patch? Are you sure?
I'm reasonably sure this must be down to 22966:de49500f344a "86, libxc:
Fix xc_translate_foreign_address() for PV guests of" which added xen_cr3_to_pfn to the foriegn headers and an incomplete rebuild on your end.
The FOO_$(ARCH) symbols come out of tools/include/xen-foreign and are auto-generated.
Ian.
Quoted 37 lines
$ hg export libxc-osdep-hypercall-buffer.patch | diffstat -p1 tools/libxc/xc_hcall_buf.c | 45 ++++++++----------------------------------- tools/libxc/xc_linux_osdep.c | 27 +++++++++++++++++++++++++ tools/libxc/xc_minios.c | 14 +++++++++++++ tools/libxc/xc_netbsd.c | 27 +++++++++++++++++++++++++ tools/libxc/xc_solaris.c | 14 +++++++++++++ tools/libxc/xenctrlosdep.h | 3 ++ 6 files changed, 94 insertions(+), 36 deletions(-)The only plausibly relevant change would be the .h which is:
diff -r
f2212579853a-r72cbc9120370tools/libxc/xenctrlosdep.htools/libxc/xenctrlosdep.h Mon Mar 07 09:01:25 2011 +0000 +3
-0@@ -74,6 +74,9 @@ struct xc_osdep_ops union { struct { + void *(*alloc_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, int npages); + void (*free_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages); + int (*hypercall)(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall); void *(*map_foreign_batch)(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int prot,Which doesn't seem likely.
Was this a stubdom or a regular tools build failure?
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Ian Campbell writes ("Re: [Xen-devel] [PATCH] libxc: osdep: convert hypercall buffer allocation"):
Quoted 4 lines
I'm reasonably sure this must be down to 22966:de49500f344a "86, libxc:
Fixxc_translate_foreign_address()for PV guests of" which addedxen_cr3_to_pfnto the foriegn headers and an incomplete rebuild on your end.
Very strange; I wasn't able to repro the build failure. Sorry. I have applied your patch (with amended description), anyway.
Ian.