make xenguest save and restore reentrant
cover make xenguest save and restore reentrant
The following patchset make xenguest save and restore functions reentrant by moving the global variables into a structure pass around functions.
Vincent Hanquez (7):
create a domain_info_context structure to store guest_width and
p2m_size for macros.
create a global context structure to record global variables in save move restore global variable to a global static context move domain_info_context into the save_context
move the domain_info_context into the restore_context
pass the suspend_context through function and allocate the context on
the save function stack.
pass the restore_context through function and allocate the context on
the restore function stack.
tools/libxc/xc_core.c | 13 +- tools/libxc/xc_core_x86.c | 30 +++-- tools/libxc/xc_core_x86.h | 2 +- tools/libxc/xc_domain_restore.c | 278 +++++++++++++++++++------------------- tools/libxc/xc_domain_save.c | 180 +++++++++++++------------- tools/libxc/xc_offline_page.c | 10 +- tools/libxc/xc_resume.c | 21 ++-- tools/libxc/xg_private.h | 18 ++- tools/libxc/xg_save_restore.h | 12 +- 9 files changed, 287 insertions(+), 277 deletions(-)
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 1/7 create a domain_info_context structure to store guest_width and p2m_size for macros.
macro now refers to guest_width and p2m_size through a dinfo pointer.
--- tools/libxc/xc_core.c | 13 +++--- tools/libxc/xc_core_x86.c | 30 ++++++++------ tools/libxc/xc_core_x86.h | 2 +- tools/libxc/xc_domain_restore.c | 82 +++++++++++++++++++-------------------- tools/libxc/xc_domain_save.c | 74 ++++++++++++++++------------------ tools/libxc/xc_offline_page.c | 10 ++-- tools/libxc/xc_resume.c | 21 ++++++---- tools/libxc/xg_private.h | 18 +++++--- tools/libxc/xg_save_restore.h | 12 +++--- 9 files changed, 134 insertions(+), 128 deletions(-)
tools/libxc/xc_core.c +7 -6
@@ -427,7 +427,8 @@ xc_domain_dumpcore_via_callback(int xc_handle, {xc_dominfo_t info;shared_info_any_t *live_shinfo =NULL ; -unsigned int guest_width; +struct domain_info_context _dinfo = {}; +struct domain_info_context *dinfo = &_dinfo;int nr_vcpus =0 ;char *dump_mem, *dump_mem_start =NULL ;
@@ -462,7 +463,7 @@ xc_domain_dumpcore_via_callback(int xc_handle,struct xc_core_section_headers *sheaders =NULL ;Elf64_Shdr *shdr; -if (get_guest_width (xc_handle, domid, &guest_width) !=0 ) +if (get_guest_width (xc_handle, domid, &dinfo->guest_width) !=0 ) {PERROR ("Could not get address size for domain" );return sts;
@@ -547,7 +548,7 @@ xc_domain_dumpcore_via_callback(int xc_handle, goto out; } - sts =xc_core_arch_map_p2m (xc_handle, guest_width, &info, live_shinfo, + sts =xc_core_arch_map_p2m (xc_handle, dinfo->guest_width, &info, live_shinfo, &p2m, &p2m_size);if ( sts !=0 ) goto out;
@@ -745,7 +746,7 @@ xc_domain_dumpcore_via_callback(int xc_handle, goto out;/* elf note section: xen version */ - sts =elfnote_dump_xen_version (args, dump_rtn, xc_handle, guest_width); + sts =elfnote_dump_xen_version (args, dump_rtn, xc_handle, dinfo->guest_width);if ( sts !=0 ) goto out;
@@ -805,9 +806,9 @@ xc_domain_dumpcore_via_callback(int xc_handle,if ( !auto_translated_physmap ) { -if ( guest_width >=sizeof (unsigned long ) ) +if ( dinfo->guest_width >=sizeof (unsigned long ) ) { -if ( guest_width ==sizeof (unsigned long ) ) +if ( dinfo->guest_width ==sizeof (unsigned long ) ) gmfn = p2m[i];else gmfn = ((uint64_t *)p2m)[i];
tools/libxc/xc_core_x86.c +17 -13
@@ -22,7 +22,7 @@#include "xc_core.h" #include "xc_e820.h" -#define GET_FIELD (_p, _f) ((guest_width==8) ? ((_p)->x64._f) : ((_p)->x32._f)) +#define GET_FIELD (_p, _f) ((dinfo->guest_width==8) ? ((_p)->x64._f) : ((_p)->x32._f))#ifndef MAX #define MAX (_a, _b) ((_a) >= (_b) ? (_a) : (_b))
@@ -76,7 +76,7 @@ xc_core_arch_memory_map_get(int xc_handle, struct xc_core_arch_context *unused, }static int -xc_core_arch_map_p2m_rw (int xc_handle,unsigned int guest_width,xc_dominfo_t *info, +xc_core_arch_map_p2m_rw (int xc_handle,struct domain_info_context *dinfo,xc_dominfo_t *info,shared_info_any_t *live_shinfo,xen_pfn_t **live_p2m,unsigned long *pfnp,int rw) {
@@ -88,14 +88,14 @@ xc_core_arch_map_p2m_rw(int xc_handle, unsigned int guest_width, xc_dominfo_t *ixen_pfn_t *p2m_frame_list =NULL ;uint32_t dom = info->domid; -unsigned long p2m_size =nr_gpfns (xc_handle, info->domid);int ret =-1 ;int err;int i; -if ( p2m_size < info->nr_pages ) + dinfo->p2m_size =nr_gpfns (xc_handle, info->domid); +if ( dinfo->p2m_size < info->nr_pages ) { -ERROR ("p2m_size < nr_pages -1 (%lx < %lx" , p2m_size, info->nr_pages -1 ); +ERROR ("p2m_size < nr_pages -1 (%lx < %lx" , dinfo->p2m_size, info->nr_pages -1 ); goto out; }
@@ -118,13 +118,13 @@ xc_core_arch_map_p2m_rw(int xc_handle, unsigned int guest_width, xc_dominfo_t *imemcpy (p2m_frame_list_list, live_p2m_frame_list_list,PAGE_SIZE );/* Canonicalize guest's unsigned long vs ours */ -if ( guest_width >sizeof (unsigned long ) ) +if ( dinfo->guest_width >sizeof (unsigned long ) )for ( i =0 ; i <PAGE_SIZE /sizeof (unsigned long ); i++ ) -if ( i <PAGE_SIZE /guest_width ) +if ( i <PAGE_SIZE /dinfo->guest_width ) p2m_frame_list_list[i] = ((uint64_t *)p2m_frame_list_list)[i];else p2m_frame_list_list[i] =0 ; -else if ( guest_width <sizeof (unsigned long ) ) +else if ( dinfo->guest_width <sizeof (unsigned long ) )for ( i =PAGE_SIZE /sizeof (unsigned long ) -1 ; i >=0 ; i-- ) p2m_frame_list_list[i] = ((uint32_t *)p2m_frame_list_list)[i];
@@ -149,10 +149,10 @@ xc_core_arch_map_p2m_rw(int xc_handle, unsigned int guest_width, xc_dominfo_t *imemcpy (p2m_frame_list, live_p2m_frame_list,P2M_GUEST_FL_SIZE );/* Canonicalize guest's unsigned long vs ours */ -if ( guest_width >sizeof (unsigned long ) ) +if ( dinfo->guest_width >sizeof (unsigned long ) )for ( i =0 ; i <P2M_FL_ENTRIES ; i++ ) p2m_frame_list[i] = ((uint64_t *)p2m_frame_list)[i]; -else if ( guest_width <sizeof (unsigned long ) ) +else if ( dinfo->guest_width <sizeof (unsigned long ) )for ( i =P2M_FL_ENTRIES -1 ; i >=0 ; i-- ) p2m_frame_list[i] = ((uint32_t *)p2m_frame_list)[i];
@@ -167,7 +167,7 @@ xc_core_arch_map_p2m_rw(int xc_handle, unsigned int guest_width, xc_dominfo_t *i goto out; } - *pfnp = p2m_size; + *pfnp = dinfo->p2m_size; ret =0 ;
@@ -195,7 +195,9 @@ xc_core_arch_map_p2m(int xc_handle, unsigned int guest_width, xc_dominfo_t *info shared_info_any_t *live_shinfo, xen_pfn_t **live_p2m, unsigned long *pfnp) { -return xc_core_arch_map_p2m_rw (xc_handle, guest_width, info, +struct domain_info_context _dinfo = { .guest_width = guest_width }; +struct domain_info_context *dinfo = &_dinfo; +return xc_core_arch_map_p2m_rw (xc_handle, dinfo, info, live_shinfo, live_p2m, pfnp,0 ); }
@@ -204,7 +206,9 @@ xc_core_arch_map_p2m_writable(int xc_handle, unsigned int guest_width, xc_dominf shared_info_any_t *live_shinfo, xen_pfn_t **live_p2m, unsigned long *pfnp) { -return xc_core_arch_map_p2m_rw (xc_handle, guest_width, info, +struct domain_info_context _dinfo = { .guest_width = guest_width }; +struct domain_info_context *dinfo = &_dinfo; +return xc_core_arch_map_p2m_rw (xc_handle, dinfo, info, live_shinfo, live_p2m, pfnp,1 ); }/*
tools/libxc/xc_core_x86.h +1 -1
@@ -22,7 +22,7 @@#define XC_CORE_X86_H #define ELF_ARCH_DATA ELFDATA2LSB -#define ELF_ARCH_MACHINE (guest_width == 8 ? EM_X86_64 : EM_386) +#define ELF_ARCH_MACHINE (dinfo->guest_width == 8 ? EM_X86_64 : EM_386)struct xc_core_arch_context {/* nothing */
tools/libxc/xc_domain_restore.c +40 -42
266 lines not on this page · raw message
tools/libxc/xc_domain_save.c +35 -39
266 lines not on this page · raw message
tools/libxc/xc_offline_page.c +5 -5
@@ -45,8 +45,8 @@ struct pte_backupint cur; }; -/* Global definition for some MACRO */ -int guest_width, p2m_size; +static struct domain_info_context _dinfo; +static struct domain_info_context *dinfo = &_dinfo;int xc_mark_page_online (int xc,unsigned long start,unsigned long end,uint32_t *status)
@@ -234,7 +234,7 @@ static int init_mem_info(int xc_handle, int domid,ERROR ("Unable to get PT level info." );return -EFAULT ; } - guest_width = minfo->guest_width; + dinfo->guest_width = minfo->guest_width; shared_info_frame = info->shared_info_frame;
@@ -255,7 +255,7 @@ static int init_mem_info(int xc_handle, int domid,munmap (live_shinfo,PAGE_SIZE ); live_shinfo =NULL ; - p2m_size = minfo->p2m_size; + dinfo->p2m_size = minfo->p2m_size; minfo->max_mfn =xc_memory_op (xc_handle, XENMEM_maximum_ram_page,NULL ); if ( !(minfo->m2p_table =
@@ -286,7 +286,7 @@ static int init_mem_info(int xc_handle, int domid,for (i =0 ; i < minfo->p2m_size ; i+=1024 ) { -int count = ((p2m_size - i ) >1024 ) ?1024 : (p2m_size - i); +int count = ((dinfo->p2m_size - i ) >1024 ) ?1024 : (dinfo->p2m_size - i);if ( ( rc =xc_get_pfn_type_batch (xc_handle, domid, count, minfo->pfn_type + i)) ) {
tools/libxc/xc_resume.c +12 -9
@@ -26,7 +26,9 @@ static int modify_returncode(int xc_handle, uint32_t domid)vcpu_guest_context_any_t ctxt;xc_dominfo_t info;xen_capabilities_info_t caps; -int rc, guest_width; +struct domain_info_context _dinfo = {}; +struct domain_info_context *dinfo = &_dinfo; +int rc;if (xc_domain_getinfo (xc_handle, domid,1 , &info) !=1 ) {
@@ -48,13 +50,13 @@ static int modify_returncode(int xc_handle, uint32_t domid)PERROR ("Could not get Xen capabilities\n" );return -1 ; } - guest_width =strstr (caps,"x86_64" ) ?8 :4 ; + dinfo->guest_width =strstr (caps,"x86_64" ) ?8 :4 ; }else {/* Probe PV guest address width. */ - guest_width =pv_guest_width (xc_handle, domid); -if ( guest_width <0 ) + dinfo->guest_width =pv_guest_width (xc_handle, domid); +if ( dinfo->guest_width <0 )return -1 ; }
@@ -102,8 +104,9 @@ static int xc_domain_resume_any(int xc_handle, uint32_t domid)xc_dominfo_t info;int i, rc =-1 ;#if defined(__i386__) || defined(__x86_64__) -int guest_width; -unsigned long mfn, p2m_size =0 ; +struct domain_info_context _dinfo = { .p2m_size =0 }; +struct domain_info_context *dinfo = &_dinfo; +unsigned long mfn;vcpu_guest_context_any_t ctxt;start_info_t *start_info;shared_info_t *shinfo =NULL ;
@@ -128,8 +131,8 @@ static int xc_domain_resume_any(int xc_handle, uint32_t domid)return rc; } - guest_width =pv_guest_width (xc_handle, domid); -if ( guest_width !=sizeof (long ) ) + dinfo->guest_width =pv_guest_width (xc_handle, domid); +if ( dinfo->guest_width !=sizeof (long ) ) {ERROR ("Cannot resume uncooperative cross-address-size guests" );return rc;
@@ -144,7 +147,7 @@ static int xc_domain_resume_any(int xc_handle, uint32_t domid) goto out; } - p2m_size = shinfo->arch.max_pfn; + dinfo->p2m_size = shinfo->arch.max_pfn; p2m_frame_list_list = xc_map_foreign_range(xc_handle, domid, PAGE_SIZE, PROT_READ,
tools/libxc/xg_private.h +11 -7
@@ -144,23 +144,27 @@ typedef l4_pgentry_64_t l4_pgentry_t;/* XXX SMH: following skanky macros rely on variable p2m_size being set */ /* XXX TJD: also, "guest_width" should be the guest's sizeof(unsigned long) */ -/* Number of xen_pfn_t in a page */ +struct domain_info_context { +unsigned int guest_width; +unsigned long p2m_size; +}; -#define FPP (PAGE_SIZE/(guest_width)) +/* Number of xen_pfn_t in a page */ +#define FPP (PAGE_SIZE/(dinfo->guest_width))/* Number of entries in the pfn_to_mfn_frame_list_list */ -#define P2M_FLL_ENTRIES (((p2m_size)+(FPP*FPP)-1)/(FPP*FPP)) +#define P2M_FLL_ENTRIES (((dinfo->p2m_size)+(FPP*FPP)-1)/(FPP*FPP))/* Number of entries in the pfn_to_mfn_frame_list */ -#define P2M_FL_ENTRIES (((p2m_size)+FPP-1)/FPP) +#define P2M_FL_ENTRIES (((dinfo->p2m_size)+FPP-1)/FPP)/* Size in bytes of the pfn_to_mfn_frame_list */ -#define P2M_GUEST_FL_SIZE ((P2M_FL_ENTRIES) * (guest_width)) +#define P2M_GUEST_FL_SIZE ((P2M_FL_ENTRIES) * (dinfo->guest_width))#define P2M_TOOLS_FL_SIZE ((P2M_FL_ENTRIES) * \ - MAX((sizeof (xen_pfn_t)), guest_width)) + MAX((sizeof (xen_pfn_t)), dinfo->guest_width))/* Masks for PTE<->PFN conversions */ -#define MADDR_BITS_X86 ((guest_width == 8) ? 52 : 44) +#define MADDR_BITS_X86 ((dinfo->guest_width == 8) ? 52 : 44)#define MFN_MASK_X86 ((1ULL << (MADDR_BITS_X86 - PAGE_SHIFT_X86)) - 1)#define MADDR_MASK_X86 (MFN_MASK_X86 << PAGE_SHIFT_X86)
tools/libxc/xg_save_restore.h +6 -6
@@ -112,34 +112,34 @@ static inline int get_platform_info(int xc_handle, uint32_t dom,#define is_mapped (pfn_type) (!((pfn_type) & 0x80000000UL)) -#define GET_FIELD (_p, _f) ((guest_width==8) ? ((_p)->x64._f) : ((_p)->x32._f)) +#define GET_FIELD (_p, _f) ((dinfo->guest_width==8) ? ((_p)->x64._f) : ((_p)->x32._f))#define SET_FIELD (_p, _f, _v) do { \ - if (guest_width == 8) \ + if (dinfo->guest_width == 8) \ (_p)->x64._f = (_v); \ else \ (_p)->x32._f = (_v); \ } while (0)#define UNFOLD_CR3 (_c) \ - ((uint64_t)((guest_width == 8) \ + ((uint64_t)((dinfo->guest_width == 8) \ ? ((_c) >> 12) \ : (((uint32_t)(_c) >> 12) | ((uint32_t)(_c) << 20))))#define FOLD_CR3 (_c) \ - ((uint64_t)((guest_width == 8) \ + ((uint64_t)((dinfo->guest_width == 8) \ ? ((uint64_t)(_c)) << 12 \ : (((uint32_t)(_c) << 12) | ((uint32_t)(_c) >> 20))))#define MEMCPY_FIELD (_d, _s, _f) do { \ - if (guest_width == 8) \ + if (dinfo->guest_width == 8) \ memcpy(&(_d)->x64._f, &(_s)->x64._f,sizeof((_d)->x64._f)); \ else \ memcpy(&(_d)->x32._f, &(_s)->x32._f,sizeof((_d)->x32._f)); \ } while (0)#define MEMSET_ARRAY_FIELD (_p, _f, _v) do { \ - if (guest_width == 8) \ + if (dinfo->guest_width == 8) \ memset(&(_p)->x64._f[0], (_v), sizeof((_p)->x64._f)); \ else \ memset(&(_p)->x32._f[0], (_v), sizeof((_p)->x32._f)); \
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 2/7 create a global context structure to record global variables in save
In reply to Vincent Hanquez--- tools/libxc/xc_domain_save.c | 96 ++++++++++++++++++++--------------------- 1 files changed, 47 insertions(+), 49 deletions(-)
tools/libxc/xc_domain_save.c +47 -49
@@ -30,26 +30,24 @@#define DEF_MAX_ITERS 29/* limit us to 30 times round loop */ #define DEF_MAX_FACTOR 3/* never send more than 3x p2m_size */ -/* max mfn of the whole machine */ -static unsigned long max_mfn; - -/* virtual starting address of the hypervisor */ -static unsigned long hvirt_start; - -/* #levels of page tables used by the current guest */ -static unsigned int pt_levels; - - -/* Live mapping of the table mapping each PFN to its current MFN. */ -static xen_pfn_t *live_p2m =NULL ; - -/* Live mapping of system MFN to PFN table. */ -static xen_pfn_t *live_m2p =NULL ; -static unsigned long m2p_mfn0; +struct save_context { +unsigned long hvirt_start;/* virtual starting address of the hypervisor */ +unsigned int pt_levels;/* #levels of page tables used by the current guest */ +unsigned long max_mfn;/* max mfn of the whole machine */ +xen_pfn_t *live_p2m;/* Live mapping of the table mapping each PFN to its current MFN. */ +xen_pfn_t *live_m2p;/* Live mapping of system MFN to PFN table. */ +unsigned long m2p_mfn0; +};static struct domain_info_context _dinfo;static struct domain_info_context *dinfo = &_dinfo; +static struct save_context _ctx = { + .live_p2m =NULL , + .live_m2p =NULL , +}; +static struct save_context *ctx = &_ctx; +/* buffer for output */ struct outbuf {void * buf;
@@ -61,20 +59,20 @@ struct outbuf {/* grep fodder: machine_to_phys */ -#define mfn_to_pfn (_mfn) (live_m2p[(_mfn)]) +#define mfn_to_pfn (_mfn) (ctx->live_m2p[(_mfn)])#define pfn_to_mfn (_pfn) \ ((xen_pfn_t) ((dinfo->guest_width==8) \ - ? (((uint64_t *)live_p2m)[(_pfn)]) \ - : ((((uint32_t *)live_p2m)[(_pfn)]) == 0xffffffffU \ - ? (-1UL) : (((uint32_t *)live_p2m)[(_pfn)])))) + ? (((uint64_t *)ctx->live_p2m)[(_pfn)]) \ + : ((((uint32_t *)ctx->live_p2m)[(_pfn)]) == 0xffffffffU \ + ? (-1UL) : (((uint32_t *)ctx->live_p2m)[(_pfn)]))))/* * Returns TRUE if the given machine frame number has a unique mapping * in the guest's pseudophysical map. */ #define MFN_IS_IN_PSEUDOPHYS_MAP (_mfn) \ - (((_mfn) < (max_mfn)) && \ + (((_mfn) < (ctx->max_mfn)) && \ ((mfn_to_pfn(_mfn) < (dinfo->p2m_size)) && \ (pfn_to_mfn(mfn_to_pfn(_mfn)) == (_mfn))))
@@ -502,12 +500,12 @@ static int canonicalize_pagetable(unsigned long type, unsigned long pfn,** reserved hypervisor mappings. This depends on the current ** page table type as well as the number of paging levels. */ - xen_start = xen_end = pte_last =PAGE_SIZE / ((pt_levels ==2 ) ?4 :8 ); + xen_start = xen_end = pte_last =PAGE_SIZE / ((ctx->pt_levels ==2 ) ?4 :8 ); -if ( (pt_levels ==2 ) && (type ==XEN_DOMCTL_PFINFO_L2TAB ) ) - xen_start = (hvirt_start >>L2_PAGETABLE_SHIFT ); +if ( (ctx->pt_levels ==2 ) && (type ==XEN_DOMCTL_PFINFO_L2TAB ) ) + xen_start = (ctx->hvirt_start >>L2_PAGETABLE_SHIFT ); -if ( (pt_levels ==3 ) && (type ==XEN_DOMCTL_PFINFO_L3TAB ) ) +if ( (ctx->pt_levels ==3 ) && (type ==XEN_DOMCTL_PFINFO_L3TAB ) ) xen_start =L3_PAGETABLE_ENTRIES_PAE ;/*
@@ -515,30 +513,30 @@ static int canonicalize_pagetable(unsigned long type, unsigned long pfn,** We can spot this by looking for the guest's mappingof the m2p. ** Guests must ensure that this check will fail for other L2s. */ -if ( (pt_levels ==3 ) && (type ==XEN_DOMCTL_PFINFO_L2TAB ) ) +if ( (ctx->pt_levels ==3 ) && (type ==XEN_DOMCTL_PFINFO_L2TAB ) ) {int hstart;uint64_t he; - hstart = (hvirt_start >>L2_PAGETABLE_SHIFT_PAE ) &0x1ff ; + hstart = (ctx->hvirt_start >>L2_PAGETABLE_SHIFT_PAE ) &0x1ff ; he = ((const uint64_t *) spage)[hstart]; -if ( ((he >>PAGE_SHIFT ) &MFN_MASK_X86 ) == m2p_mfn0 ) +if ( ((he >>PAGE_SHIFT ) &MFN_MASK_X86 ) == ctx->m2p_mfn0 ) {/* hvirt starts with xen stuff... */ xen_start = hstart; } -else if ( hvirt_start !=0xf5800000 ) +else if ( ctx->hvirt_start !=0xf5800000 ) {/* old L2s from before hole was shrunk... */ hstart = (0xf5800000 >>L2_PAGETABLE_SHIFT_PAE ) &0x1ff ; he = ((const uint64_t *) spage)[hstart]; -if ( ((he >>PAGE_SHIFT ) &MFN_MASK_X86 ) == m2p_mfn0 ) +if ( ((he >>PAGE_SHIFT ) &MFN_MASK_X86 ) == ctx->m2p_mfn0 ) xen_start = hstart; } } -if ( (pt_levels ==4 ) && (type ==XEN_DOMCTL_PFINFO_L4TAB ) ) +if ( (ctx->pt_levels ==4 ) && (type ==XEN_DOMCTL_PFINFO_L4TAB ) ) {/* ** XXX SMH: should compute these from hvirt_start (which we have)
@@ -553,7 +551,7 @@ static int canonicalize_pagetable(unsigned long type, unsigned long pfn, {unsigned long pfn, mfn; -if ( pt_levels ==2 ) +if ( ctx->pt_levels ==2 ) pte = ((const uint32_t *)spage)[i];else pte = ((const uint64_t *)spage)[i];
@@ -588,13 +586,13 @@ static int canonicalize_pagetable(unsigned long type, unsigned long pfn,* a 64bit hypervisor. We zap these here to avoid any * surprise at restore time... */ -if ( (pt_levels ==3 ) && +if ( (ctx->pt_levels ==3 ) && (type ==XEN_DOMCTL_PFINFO_L3TAB ) && (pte & (_PAGE_USER|_PAGE_RW|_PAGE_ACCESSED)) ) pte &= ~(_PAGE_USER|_PAGE_RW|_PAGE_ACCESSED); } -if ( pt_levels ==2 ) +if ( ctx->pt_levels ==2 ) ((uint32_t *)dpage)[i] = pte;else ((uint64_t *)dpage)[i] = pte;
@@ -753,7 +751,7 @@ static xen_pfn_t *map_and_save_p2m_table(int xc_handle,ERROR ("Couldn't map p2m table" ); goto out; } - live_p2m = p2m;/* So that translation macros will work */ + ctx->live_p2m = p2m;/* So that translation macros will work */ /* Canonicalise the pfn-to-mfn table frame-number list. */ for ( i =0 ; i < dinfo->p2m_size; i +=FPP )
@@ -762,15 +760,15 @@ static xen_pfn_t *map_and_save_p2m_table(int xc_handle, {ERROR ("Frame# in pfn-to-mfn frame list is not in pseudophys" );ERROR ("entry %d: p2m_frame_list[%ld] is 0x%" PRIx64", max 0x%lx" , - i, i/FPP , (uint64_t )p2m_frame_list[i/FPP ], max_mfn); -if ( p2m_frame_list[i/FPP ] < max_mfn ) + i, i/FPP , (uint64_t )p2m_frame_list[i/FPP ], ctx->max_mfn); +if ( p2m_frame_list[i/FPP ] < ctx->max_mfn ) {ERROR ("m2p[0x%" PRIx64"] = 0x%" PRIx64, (uint64_t )p2m_frame_list[i/FPP ], - (uint64_t )live_m2p[p2m_frame_list[i/FPP ]]); + (uint64_t )ctx->live_m2p[p2m_frame_list[i/FPP ]]);ERROR ("p2m[0x%" PRIx64"] = 0x%" PRIx64, - (uint64_t )live_m2p[p2m_frame_list[i/FPP ]], - (uint64_t )p2m[live_m2p[p2m_frame_list[i/FPP ]]]); + (uint64_t )ctx->live_m2p[p2m_frame_list[i/FPP ]], + (uint64_t )p2m[ctx->live_m2p[p2m_frame_list[i/FPP ]]]); } goto out;
@@ -924,7 +922,7 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters,initialize_mbit_rate ();if ( !get_platform_info (xc_handle, dom, - &max_mfn, &hvirt_start, &pt_levels, &dinfo->guest_width) ) + &ctx->max_mfn, &ctx->hvirt_start, &ctx->pt_levels, &dinfo->guest_width) ) {ERROR ("Unable to get platform info." );return 1 ;
@@ -1063,7 +1061,7 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters, }/* Setup the mfn_to_pfn table mapping */ -if ( !(live_m2p =xc_map_m2p (xc_handle, max_mfn,PROT_READ , &m2p_mfn0)) ) +if ( !(ctx->live_m2p =xc_map_m2p (xc_handle, ctx->max_mfn,PROT_READ , &ctx->m2p_mfn0)) ) {ERROR ("Failed to map live M2P table" ); goto out;
@@ -1081,8 +1079,8 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters,int err =0 ;/* Map the P2M table, and write the list of P2M frames */ - live_p2m =map_and_save_p2m_table (xc_handle, io_fd, dom, live_shinfo); -if ( live_p2m ==NULL ) + ctx->live_p2m =map_and_save_p2m_table (xc_handle, io_fd, dom, live_shinfo); +if ( ctx->live_p2m ==NULL ) {ERROR ("Failed to map/save the p2m frame list" ); goto out;
@@ -1701,7 +1699,7 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters, FOLD_CR3(mfn_to_pfn(UNFOLD_CR3(GET_FIELD(&ctxt, ctrlreg[3])))));/* Guest pagetable (x86/64) stored in otherwise-unused CR1. */ -if ( (pt_levels ==4 ) && ctxt.x64.ctrlreg[1 ] ) +if ( (ctx->pt_levels ==4 ) && ctxt.x64.ctrlreg[1 ] ) {if ( !MFN_IS_IN_PSEUDOPHYS_MAP (UNFOLD_CR3 (ctxt.x64.ctrlreg[1 ])) ) {
@@ -1809,11 +1807,11 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters,if ( live_shinfo )munmap (live_shinfo,PAGE_SIZE ); -if ( live_p2m ) -munmap (live_p2m,P2M_FLL_ENTRIES *PAGE_SIZE ); +if ( ctx->live_p2m ) +munmap (ctx->live_p2m,P2M_FLL_ENTRIES *PAGE_SIZE ); -if ( live_m2p ) -munmap (live_m2p,M2P_SIZE (max_mfn)); +if ( ctx->live_m2p ) +munmap (ctx->live_m2p,M2P_SIZE (ctx->max_mfn));free (pfn_type);free (pfn_batch);
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 3/7 move restore global variable to a global static context
In reply to Vincent Hanquez--- tools/libxc/xc_domain_restore.c | 151 +++++++++++++++++++-------------------- 1 files changed, 73 insertions(+), 78 deletions(-)
tools/libxc/xc_domain_restore.c +73 -78
@@ -32,27 +32,22 @@#include <xen/hvm/ioreq.h> #include <xen/hvm/params.h> -/* max mfn of the current host machine */ -static unsigned long max_mfn; - -/* virtual starting address of the hypervisor */ -static unsigned long hvirt_start; - -/* #levels of page tables used by the current guest */ -static unsigned int pt_levels; - - -/* number of 'in use' pfns in the guest (i.e. #P2M entries with a valid mfn) */ -static unsigned long nr_pfns; - -/* Live mapping of the table mapping each PFN to its current MFN. */ -static xen_pfn_t *live_p2m =NULL ; - -/* A table mapping each PFN to its new MFN. */ -static xen_pfn_t *p2m =NULL ; - -/* If have enough continuous memory for super page allocation */ -static unsigned no_superpage_mem =0 ; +struct restore_ctx { +unsigned long max_mfn;/* max mfn of the current host machine */ +unsigned long hvirt_start;/* virtual starting address of the hypervisor */ +unsigned int pt_levels;/* #levels of page tables used by the current guest */ +unsigned long nr_pfns;/* number of 'in use' pfns in the guest (i.e. #P2M entries with a valid mfn) */ +xen_pfn_t *live_p2m;/* Live mapping of the table mapping each PFN to its current MFN. */ +xen_pfn_t *p2m;/* A table mapping each PFN to its new MFN. */ +unsigned no_superpage_mem;/* If have enough continuous memory for super page allocation */ +}; + +static struct restore_ctx _ctx = { + .live_p2m =NULL , + .p2m =NULL , + .no_superpage_mem =0 , +}; +static struct restore_ctx *ctx = &_ctx;static struct domain_info_context _dinfo;static struct domain_info_context *dinfo = &_dinfo;
@@ -82,7 +77,7 @@ static int super_page_populated(unsigned long pfn) pfn &= ~(SUPERPAGE_NR_PFNS -1 );for ( i = pfn; i < pfn +SUPERPAGE_NR_PFNS ; i++ ) { -if ( p2m[i] !=INVALID_P2M_ENTRY ) +if ( ctx->p2m[i] !=INVALID_P2M_ENTRY )return 1 ; }return 0 ;
@@ -107,7 +102,7 @@ static int break_super_page(int xc_handle,for ( i = start_pfn; i < start_pfn +SUPERPAGE_NR_PFNS ; i++ ) {/* check the 2M page are populated */ -if ( p2m[i] ==INVALID_P2M_ENTRY ) { +if ( ctx->p2m[i] ==INVALID_P2M_ENTRY ) {DPRINTF ("Previous super page was populated wrongly!\n" );return 1 ; }
@@ -156,7 +151,7 @@ static int break_super_page(int xc_handle, start_pfn = next_pfn & ~(SUPERPAGE_NR_PFNS -1 );for ( i = start_pfn; i < start_pfn +SUPERPAGE_NR_PFNS ; i++ ) { - p2m[i] =INVALID_P2M_ENTRY ; + ctx->p2m[i] =INVALID_P2M_ENTRY ; }for ( i = start_pfn; i < start_pfn + tot_pfns; i++ )
@@ -170,7 +165,7 @@ static int break_super_page(int xc_handle, rc =1 ; goto out; } - p2m[i] = mfn; + ctx->p2m[i] = mfn; }/* restore contents */
@@ -222,7 +217,7 @@ static int allocate_mfn_list(int xc_handle, sp_pfn = *next_pfn;if ( !superpages || - no_superpage_mem || + ctx->no_superpage_mem || !SUPER_PAGE_TRACKING (sp_pfn) ) goto normal_page;
@@ -267,13 +262,13 @@ static int allocate_mfn_list(int xc_handle, {for ( i = pfn; i < pfn +SUPERPAGE_NR_PFNS ; i++, mfn++ ) { - p2m[i] = mfn; + ctx->p2m[i] = mfn; }return 0 ; }DPRINTF ("No 2M page available for pfn 0x%lx, fall back to 4K page.\n" , pfn); - no_superpage_mem =1 ; + ctx->no_superpage_mem =1 ; normal_page:if ( !batch_buf )
@@ -289,7 +284,7 @@ normal_page:continue ; pfn = mfn = batch_buf[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK ; -if ( p2m[pfn] ==INVALID_P2M_ENTRY ) +if ( ctx->p2m[pfn] ==INVALID_P2M_ENTRY ) {if (xc_domain_memory_populate_physmap (xc_handle, dom,1 ,0 ,0 , &mfn) !=0 )
@@ -299,7 +294,7 @@ normal_page: errno =ENOMEM ;return 1 ; } - p2m[pfn] = mfn; + ctx->p2m[pfn] = mfn; } }
@@ -436,7 +431,7 @@ alloc_page: }else { -if (p2m[pfn] ==INVALID_P2M_ENTRY ) +if (ctx->p2m[pfn] ==INVALID_P2M_ENTRY ) {DPRINTF ("Warning: pfn 0x%lx are not allocated!\n" , pfn);/*XXX:allocate this page?*/
@@ -444,7 +439,7 @@ alloc_page:/* setup region_mfn[] for batch map. * For HVM guests, this interface takes PFNs, not MFNs */ - region_mfn[i] = hvm ? pfn : p2m[pfn]; + region_mfn[i] = hvm ? pfn : ctx->p2m[pfn]; } }return 0 ;
@@ -510,11 +505,11 @@ static int uncanonicalize_pagetable(int xc_handle, uint32_t dom,unsigned long pfn;uint64_t pte; - pte_last =PAGE_SIZE / ((pt_levels ==2 )?4 :8 ); + pte_last =PAGE_SIZE / ((ctx->pt_levels ==2 )?4 :8 );for ( i =0 ; i < pte_last; i++ ) { -if ( pt_levels ==2 ) +if ( ctx->pt_levels ==2 ) pte = ((uint32_t *)page)[i];else pte = ((uint64_t *)page)[i];
@@ -526,7 +521,7 @@ static int uncanonicalize_pagetable(int xc_handle, uint32_t dom, pfn = (pte >>PAGE_SHIFT ) &MFN_MASK_X86 ;/* Allocate mfn if necessary */ -if ( p2m[pfn] ==INVALID_P2M_ENTRY ) +if ( ctx->p2m[pfn] ==INVALID_P2M_ENTRY ) {unsigned long force_pfn = superpages ?FORCE_SP_MASK : pfn; if (allocate_mfn_list(xc_handle, dom,
@@ -534,9 +529,9 @@ static int uncanonicalize_pagetable(int xc_handle, uint32_t dom,return 0 ; } pte &= ~MADDR_MASK_X86 ; - pte |= (uint64_t )p2m[pfn] <<PAGE_SHIFT ; + pte |= (uint64_t )ctx->p2m[pfn] <<PAGE_SHIFT ; -if ( pt_levels ==2 ) +if ( ctx->pt_levels ==2 ) ((uint32_t *)page)[i] = (uint32_t )pte;else ((uint64_t *)page)[i] = (uint64_t )pte;
@@ -594,13 +589,13 @@ static xen_pfn_t *load_p2m_frame_list(if ( chunk_bytes ==sizeof (ctxt.x32) ) { dinfo->guest_width =4 ; -if ( pt_levels >2 ) - pt_levels =3 ; +if ( ctx->pt_levels >2 ) + ctx->pt_levels =3 ; }else if ( chunk_bytes ==sizeof (ctxt.x64) ) { dinfo->guest_width =8 ; - pt_levels =4 ; + ctx->pt_levels =4 ; }else {
@@ -1220,7 +1215,7 @@ static int apply_batch(int xc_handle, uint32_t dom, xen_pfn_t* region_mfn, pfn_type[pfn] = pagetype; - mfn = p2m[pfn]; + mfn = ctx->p2m[pfn];/* In verify mode, we use a copy; otherwise we work in place */ page = pagebuf->verify ? (void *)buf : (region_base + i*PAGE_SIZE );
@@ -1241,7 +1236,7 @@ static int apply_batch(int xc_handle, uint32_t dom, xen_pfn_t* region_mfn,** so we may need to update the p2m after the main loop. ** Hence we defer canonicalization of L1s until then. */ -if ((pt_levels !=3 ) || +if ((ctx->pt_levels !=3 ) || pae_extended_cr3 || (pagetype !=XEN_DOMCTL_PFINFO_L1TAB )) {
@@ -1357,7 +1352,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, tailbuf.ishvm = hvm;/* For info only */ - nr_pfns =0 ; + ctx->nr_pfns =0 ;/* Always try to allocate 2M pages for HVM */ if ( hvm )
@@ -1371,7 +1366,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,DPRINTF ("xc_domain_restore start: p2m_size = %lx\n" , dinfo->p2m_size);if ( !get_platform_info (xc_handle, dom, - &max_mfn, &hvirt_start, &pt_levels, &dinfo->guest_width) ) + &ctx->max_mfn, &ctx->hvirt_start, &ctx->pt_levels, &dinfo->guest_width) ) {ERROR ("Unable to get platform info." );return 1 ;
@@ -1381,7 +1376,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,* assume the guest will be the same as we are. We'll fix that later * if we discover otherwise. */ dinfo->guest_width =sizeof (unsigned long ); - pt_levels = (dinfo->guest_width ==8 ) ?4 : (pt_levels ==2 ) ?2 :3 ; + ctx->pt_levels = (dinfo->guest_width ==8 ) ?4 : (ctx->pt_levels ==2 ) ?2 :3 ;if ( !hvm ) {
@@ -1405,13 +1400,13 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, }/* We want zeroed memory so use calloc rather than malloc. */ - p2m =calloc (dinfo->p2m_size,sizeof (xen_pfn_t)); + ctx->p2m =calloc (dinfo->p2m_size,sizeof (xen_pfn_t)); pfn_type =calloc (dinfo->p2m_size,sizeof (unsigned long )); region_mfn =xg_memalign (PAGE_SIZE ,ROUNDUP (MAX_BATCH_SIZE *sizeof (xen_pfn_t),PAGE_SHIFT )); -if ( (p2m ==NULL ) || (pfn_type ==NULL ) || +if ( (ctx->p2m ==NULL ) || (pfn_type ==NULL ) || (region_mfn ==NULL ) ) {ERROR ("memory alloc failed" );
@@ -1440,7 +1435,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,/* Mark all PFNs as invalid; we allocate on demand */ for ( pfn =0 ; pfn < dinfo->p2m_size; pfn++ ) - p2m[pfn] =INVALID_P2M_ENTRY ; + ctx->p2m[pfn] =INVALID_P2M_ENTRY ; mmu =xc_alloc_mmu_updates (xc_handle, dom);if ( mmu ==NULL )
@@ -1575,7 +1570,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,if ( hvm ) goto finish_hvm; -if ( (pt_levels ==3 ) && !pae_extended_cr3 ) +if ( (ctx->pt_levels ==3 ) && !pae_extended_cr3 ) {/* ** XXX SMH on PAE we need to ensure PGDs are in MFNs < 4G. This
@@ -1596,7 +1591,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, {if ( ((pfn_type[i] &XEN_DOMCTL_PFINFO_LTABTYPE_MASK ) ==XEN_DOMCTL_PFINFO_L3TAB ) && - (p2m[i] >0xfffffUL ) ) + (ctx->p2m[i] >0xfffffUL ) ) {unsigned long new_mfn;uint64_t l3ptes[4 ];
@@ -1604,21 +1599,21 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, l3tab = (uint64_t *)xc_map_foreign_range (xc_handle, dom,PAGE_SIZE , -PROT_READ , p2m[i]); +PROT_READ , ctx->p2m[i]);for ( j =0 ; j <4 ; j++ ) l3ptes[j] = l3tab[j];munmap (l3tab,PAGE_SIZE ); - new_mfn =xc_make_page_below_4G (xc_handle, dom, p2m[i]); + new_mfn =xc_make_page_below_4G (xc_handle, dom, ctx->p2m[i]);if ( !new_mfn ) {ERROR ("Couldn't get a page below 4GB :-(" ); goto out; } - p2m[i] = new_mfn; + ctx->p2m[i] = new_mfn; if ( xc_add_mmu_update(xc_handle, mmu, (((unsigned long long)new_mfn) << PAGE_SHIFT) |
@@ -1630,7 +1625,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, l3tab = (uint64_t *)xc_map_foreign_range (xc_handle, dom,PAGE_SIZE , -PROT_READ |PROT_WRITE , p2m[i]); +PROT_READ |PROT_WRITE , ctx->p2m[i]);for ( j =0 ; j <4 ; j++ ) l3tab[j] = l3ptes[j];
@@ -1647,7 +1642,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,if ( ((pfn_type[i] &XEN_DOMCTL_PFINFO_LTABTYPE_MASK ) ==XEN_DOMCTL_PFINFO_L1TAB ) ) { - region_mfn[j] = p2m[i]; + region_mfn[j] = ctx->p2m[i]; j++; }
@@ -1716,7 +1711,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,continue ; } - pin[nr_pins].arg1.mfn = p2m[i]; + pin[nr_pins].arg1.mfn = ctx->p2m[i]; nr_pins++;/* Batch full? Then flush. */
@@ -1739,7 +1734,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, }DPRINTF ("\b\b\b\b100%%\n" ); -DPRINTF ("Memory reloaded (%ld pages)\n" , nr_pfns); +DPRINTF ("Memory reloaded (%ld pages)\n" , ctx->nr_pfns);/* Get the list of PFNs that are not in the psuedo-phys map */ {
@@ -1749,12 +1744,12 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, {unsigned long pfn = tailbuf.u.pv.pfntab[i]; -if ( p2m[pfn] !=INVALID_P2M_ENTRY ) +if ( ctx->p2m[pfn] !=INVALID_P2M_ENTRY ) {/* pfn is not in physmap now, but was at some point during the save/migration process - need to free it */ - tailbuf.u.pv.pfntab[nr_frees++] = p2m[pfn]; - p2m[pfn] =INVALID_P2M_ENTRY ;/* not in pseudo-physical map */ + tailbuf.u.pv.pfntab[nr_frees++] = ctx->p2m[pfn]; + ctx->p2m[pfn] =INVALID_P2M_ENTRY ;/* not in pseudo-physical map */ } }
@@ -1812,17 +1807,17 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,ERROR ("Suspend record frame number is bad" ); goto out; } - mfn = p2m[pfn]; + mfn = ctx->p2m[pfn];SET_FIELD (&ctxt, user_regs.edx, mfn); start_info =xc_map_foreign_range ( xc_handle, dom,PAGE_SIZE ,PROT_READ |PROT_WRITE , mfn);SET_FIELD (start_info, nr_pages, dinfo->p2m_size);SET_FIELD (start_info, shared_info, shared_info_frame<<PAGE_SHIFT );SET_FIELD (start_info, flags,0 ); - *store_mfn = p2m[GET_FIELD (start_info, store_mfn)]; + *store_mfn = ctx->p2m[GET_FIELD (start_info, store_mfn)];SET_FIELD (start_info, store_mfn, *store_mfn);SET_FIELD (start_info, store_evtchn, store_evtchn); - *console_mfn = p2m[GET_FIELD (start_info, console.domU.mfn)]; + *console_mfn = ctx->p2m[GET_FIELD (start_info, console.domU.mfn)];SET_FIELD (start_info, console.domU.mfn, *console_mfn);SET_FIELD (start_info, console.domU.evtchn, console_evtchn);munmap (start_info,PAGE_SIZE );
@@ -1844,7 +1839,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, j, (unsigned long)pfn); goto out; } -SET_FIELD (&ctxt, gdt_frames[j], p2m[pfn]); +SET_FIELD (&ctxt, gdt_frames[j], ctx->p2m[pfn]); }/* Uncanonicalise the page table base pointer. */ pfn =UNFOLD_CR3 (GET_FIELD (&ctxt, ctrlreg[3 ]));
@@ -1857,17 +1852,17 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, }if ( (pfn_type[pfn] &XEN_DOMCTL_PFINFO_LTABTYPE_MASK ) != - ((unsigned long )pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT ) ) + ((unsigned long )ctx->pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT ) ) {ERROR ("PT base is bad. pfn=%lu nr=%lu type=%08lx %08lx" , pfn, dinfo->p2m_size, pfn_type[pfn], - (unsigned long )pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT ); + (unsigned long )ctx->pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT ); goto out; } -SET_FIELD (&ctxt, ctrlreg[3 ],FOLD_CR3 (p2m[pfn])); +SET_FIELD (&ctxt, ctrlreg[3 ],FOLD_CR3 (ctx->p2m[pfn]));/* Guest pagetable (x86/64) stored in otherwise-unused CR1. */ -if ( (pt_levels ==4 ) && (ctxt.x64.ctrlreg[1 ] &1 ) ) +if ( (ctx->pt_levels ==4 ) && (ctxt.x64.ctrlreg[1 ] &1 ) ) { pfn =UNFOLD_CR3 (ctxt.x64.ctrlreg[1 ] & ~1 );if ( pfn >= dinfo->p2m_size )
@@ -1877,14 +1872,14 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, goto out; }if ( (pfn_type[pfn] &XEN_DOMCTL_PFINFO_LTABTYPE_MASK ) != - ((unsigned long )pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT ) ) + ((unsigned long )ctx->pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT ) ) {ERROR ("User PT base is bad. pfn=%lu nr=%lu type=%08lx %08lx" , pfn, dinfo->p2m_size, pfn_type[pfn], - (unsigned long )pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT ); + (unsigned long )ctx->pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT ); goto out; } - ctxt.x64.ctrlreg[1 ] =FOLD_CR3 (p2m[pfn]); + ctxt.x64.ctrlreg[1 ] =FOLD_CR3 (ctx->p2m[pfn]); } domctl.cmd = XEN_DOMCTL_setvcpucontext; domctl.domain = (domid_t )dom;
@@ -1943,11 +1938,11 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,ERROR ("PFN-to-MFN frame number %i (%#lx) is bad" , i, pfn); goto out; } - p2m_frame_list[i] = p2m[pfn]; + p2m_frame_list[i] = ctx->p2m[pfn]; }/* Copy the P2M we've constructed to the 'live' P2M */ -if ( !(live_p2m =xc_map_foreign_batch (xc_handle, dom,PROT_WRITE , +if ( !(ctx->live_p2m =xc_map_foreign_batch (xc_handle, dom,PROT_WRITE , p2m_frame_list,P2M_FL_ENTRIES )) ) {ERROR ("Couldn't map p2m table" );
@@ -1958,13 +1953,13 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,* we need to adjust the live_p2m assignment appropriately */ if ( dinfo->guest_width >sizeof (xen_pfn_t) )for ( i = dinfo->p2m_size -1 ; i >=0 ; i-- ) - ((int64_t *)live_p2m)[i] = (long )p2m[i]; + ((int64_t *)ctx->live_p2m)[i] = (long )ctx->p2m[i];else if ( dinfo->guest_width <sizeof (xen_pfn_t) )for ( i =0 ; i < dinfo->p2m_size; i++ ) - ((uint32_t *)live_p2m)[i] = p2m[i]; + ((uint32_t *)ctx->live_p2m)[i] = ctx->p2m[i];else -memcpy (live_p2m, p2m, dinfo->p2m_size *sizeof (xen_pfn_t)); -munmap (live_p2m,P2M_FL_ENTRIES *PAGE_SIZE ); +memcpy (ctx->live_p2m, ctx->p2m, dinfo->p2m_size *sizeof (xen_pfn_t)); +munmap (ctx->live_p2m,P2M_FL_ENTRIES *PAGE_SIZE );DPRINTF ("Domain ready to be built.\n" ); rc =0 ;
@@ -2018,7 +2013,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,if ( (rc !=0 ) && (dom !=0 ) )xc_domain_destroy (xc_handle, dom);free (mmu); -free (p2m); +free (ctx->p2m);free (pfn_type);tailbuf_free (&tailbuf);
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 4/7 move domain_info_context into the save_context
In reply to Vincent Hanquez--- tools/libxc/xc_domain_save.c | 17 ++++++++++------- 1 files changed, 10 insertions(+), 7 deletions(-)
tools/libxc/xc_domain_save.c +10 -7
@@ -30,23 +30,22 @@#define DEF_MAX_ITERS 29/* limit us to 30 times round loop */ #define DEF_MAX_FACTOR 3/* never send more than 3x p2m_size */ -struct save_context { +struct save_ctx {unsigned long hvirt_start;/* virtual starting address of the hypervisor */ unsigned int pt_levels;/* #levels of page tables used by the current guest */ unsigned long max_mfn;/* max mfn of the whole machine */ xen_pfn_t *live_p2m;/* Live mapping of the table mapping each PFN to its current MFN. */ xen_pfn_t *live_m2p;/* Live mapping of system MFN to PFN table. */ unsigned long m2p_mfn0; +struct domain_info_context dinfo; }; -static struct domain_info_context _dinfo; -static struct domain_info_context *dinfo = &_dinfo; - -static struct save_context _ctx = { +static struct save_ctx _ctx = { .live_p2m =NULL , .live_m2p =NULL , }; -static struct save_context *ctx = &_ctx; + +static struct save_ctx *ctx = &_ctx;/* buffer for output */ struct outbuf {
@@ -403,6 +402,7 @@ static int analysis_phase(int xc_handle, uint32_t domid,long long start, now;xc_shadow_op_stats_t stats;int j; +struct domain_info_context *dinfo = &ctx->dinfo; start =llgettimeofday ();
@@ -458,6 +458,7 @@ static void *map_frame_list_list(int xc_handle, uint32_t dom, {int count =100 ;void *p; +struct domain_info_context *dinfo = &ctx->dinfo;uint64_t fll =GET_FIELD (shinfo, arch.pfn_to_mfn_frame_list_list);while ( count-- && (fll ==0 ) )
@@ -491,7 +492,7 @@ static void *map_frame_list_list(int xc_handle, uint32_t dom,static int canonicalize_pagetable (unsigned long type,unsigned long pfn,const void *spage,void *dpage) { - +struct domain_info_context *dinfo = &ctx->dinfo;int i, pte_last, xen_start, xen_end, race =0 ;uint64_t pte;
@@ -672,6 +673,7 @@ static xen_pfn_t *map_and_save_p2m_table(int xc_handle, shared_info_any_t *live_shinfo) {vcpu_guest_context_any_t ctxt; +struct domain_info_context *dinfo = &ctx->dinfo;/* Double and single indirect references to the live P2M table */ void *live_p2m_frame_list_list =NULL ;
@@ -910,6 +912,7 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters,unsigned long mfn;struct outbuf ob; +struct domain_info_context *dinfo = &ctx->dinfo;int completed =0 ;
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 5/7 move the domain_info_context into the restore_context
In reply to Vincent Hanquez--- tools/libxc/xc_domain_restore.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)
tools/libxc/xc_domain_restore.c +8 -3
@@ -40,6 +40,7 @@ struct restore_ctx {xen_pfn_t *live_p2m;/* Live mapping of the table mapping each PFN to its current MFN. */ xen_pfn_t *p2m;/* A table mapping each PFN to its new MFN. */ unsigned no_superpage_mem;/* If have enough continuous memory for super page allocation */ +struct domain_info_context dinfo; };static struct restore_ctx _ctx = {
@@ -49,9 +50,6 @@ static struct restore_ctx _ctx = { };static struct restore_ctx *ctx = &_ctx; -static struct domain_info_context _dinfo; -static struct domain_info_context *dinfo = &_dinfo; -/* ** **
@@ -315,6 +313,7 @@ static int allocate_physmem(int xc_handle, uint32_t dom,/* Buffer of pfn list for 2M page, or series of 4K pages */ xen_pfn_t *batch_buf;unsigned int batch_buf_len; +struct domain_info_context *dinfo = &ctx->dinfo;if ( !superpages ) {
@@ -504,6 +503,7 @@ static int uncanonicalize_pagetable(int xc_handle, uint32_t dom,int i, pte_last;unsigned long pfn;uint64_t pte; +struct domain_info_context *dinfo = &ctx->dinfo; pte_last =PAGE_SIZE / ((ctx->pt_levels ==2 )?4 :8 );
@@ -548,6 +548,7 @@ static xen_pfn_t *load_p2m_frame_list(xen_pfn_t *p2m_frame_list;vcpu_guest_context_any_t ctxt;xen_pfn_t p2m_fl_zero; +struct domain_info_context *dinfo = &ctx->dinfo;/* Read first entry of P2M list, or extended-info signature (~0UL). */ if (read_exact (io_fd, &p2m_fl_zero,sizeof (long )) )
@@ -864,6 +865,7 @@ static int buffer_tail_pv(struct tailbuf_pv *buf, int fd, {unsigned int i;size_t pfnlen, vcpulen; +struct domain_info_context *dinfo = &ctx->dinfo;/* TODO: handle changing pfntab and vcpu counts */ /* PFN tab */
@@ -1172,6 +1174,7 @@ static int apply_batch(int xc_handle, uint32_t dom, xen_pfn_t* region_mfn,/* A temporary mapping, and a copy, of one frame of guest memory. */ unsigned long *page =NULL ;int nraces =0 ; +struct domain_info_context *dinfo = &ctx->dinfo;unsigned long mfn, pfn, pagetype;
@@ -1347,6 +1350,8 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,tailbuf_t tailbuf, tmptail;void * vcpup; +struct domain_info_context *dinfo = &ctx->dinfo; +pagebuf_init (&pagebuf);memset (&tailbuf,0 ,sizeof (tailbuf)); tailbuf.ishvm = hvm;
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 6/7 pass the suspend_context through function and allocate the context on the save function stack.
In reply to Vincent Hanquez--- tools/libxc/xc_domain_save.c | 27 ++++++++++++++------------- 1 files changed, 14 insertions(+), 13 deletions(-)
tools/libxc/xc_domain_save.c +14 -13
@@ -40,13 +40,6 @@ struct save_ctx {struct domain_info_context dinfo; }; -static struct save_ctx _ctx = { - .live_p2m =NULL , - .live_m2p =NULL , -}; - -static struct save_ctx *ctx = &_ctx; -/* buffer for output */ struct outbuf {void * buf;
@@ -396,7 +389,7 @@ static int print_stats(int xc_handle, uint32_t domid, int pages_sent, } -static int analysis_phase (int xc_handle,uint32_t domid, +static int analysis_phase (int xc_handle,uint32_t domid,struct save_ctx *ctx,unsigned long *arr,int runs) {long long start, now;
@@ -454,6 +447,7 @@ static int suspend_and_state(int (*suspend)(void*), void* data,** it to update the MFN to a reasonable value. */ static void *map_frame_list_list (int xc_handle,uint32_t dom, +struct save_ctx *ctx,shared_info_any_t *shinfo) {int count =100 ;
@@ -489,7 +483,8 @@ static void *map_frame_list_list(int xc_handle, uint32_t dom,** which entries do not require canonicalization (in particular, those ** entries which map the virtual address reserved for the hypervisor). */ -static int canonicalize_pagetable (unsigned long type,unsigned long pfn, +static int canonicalize_pagetable (struct save_ctx *ctx, +unsigned long type,unsigned long pfn,const void *spage,void *dpage) {struct domain_info_context *dinfo = &ctx->dinfo;
@@ -670,6 +665,7 @@ err0:static xen_pfn_t *map_and_save_p2m_table (int xc_handle,int io_fd,uint32_t dom, +struct save_ctx *ctx,shared_info_any_t *live_shinfo) {vcpu_guest_context_any_t ctxt;
@@ -688,7 +684,7 @@ static xen_pfn_t *map_and_save_p2m_table(int xc_handle,int i, success =0 ; - live_p2m_frame_list_list =map_frame_list_list (xc_handle, dom, + live_p2m_frame_list_list =map_frame_list_list (xc_handle, dom, ctx, live_shinfo);if ( !live_p2m_frame_list_list ) goto out;
@@ -912,6 +908,11 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters,unsigned long mfn;struct outbuf ob; +static struct save_ctx _ctx = { + .live_p2m =NULL , + .live_m2p =NULL , + }; +static struct save_ctx *ctx = &_ctx;struct domain_info_context *dinfo = &ctx->dinfo;int completed =0 ;
@@ -1043,7 +1044,7 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters, } } -analysis_phase (xc_handle, dom, to_skip,0 ); +analysis_phase (xc_handle, dom, ctx, to_skip,0 ); pfn_type =xg_memalign (PAGE_SIZE ,ROUNDUP (MAX_BATCH_SIZE *sizeof (*pfn_type),PAGE_SHIFT ));
@@ -1082,7 +1083,7 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters,int err =0 ;/* Map the P2M table, and write the list of P2M frames */ - ctx->live_p2m =map_and_save_p2m_table (xc_handle, io_fd, dom, live_shinfo); + ctx->live_p2m =map_and_save_p2m_table (xc_handle, io_fd, dom, ctx, live_shinfo);if ( ctx->live_p2m ==NULL ) {ERROR ("Failed to map/save the p2m frame list" );
@@ -1376,7 +1377,7 @@ int xc_domain_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters, {/* We have a pagetable page: need to rewrite it. */ race = -canonicalize_pagetable (pagetype, pfn, spage, page); +canonicalize_pagetable (ctx, pagetype, pfn, spage, page);if ( race && !live ) {
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 7/7 pass the restore_context through function and allocate the context on the restore function stack.
In reply to Vincent Hanquez--- tools/libxc/xc_domain_restore.c | 62 ++++++++++++++++++++------------------- 1 files changed, 32 insertions(+), 30 deletions(-)
tools/libxc/xc_domain_restore.c +32 -30
@@ -43,13 +43,6 @@ struct restore_ctx {struct domain_info_context dinfo; }; -static struct restore_ctx _ctx = { - .live_p2m =NULL , - .p2m =NULL , - .no_superpage_mem =0 , -}; -static struct restore_ctx *ctx = &_ctx; -/* ** **
@@ -69,7 +62,7 @@ static struct restore_ctx *ctx = &_ctx;#define SUPER_PAGE_TRACKING (pfn) ( (pfn) != INVALID_SUPER_PAGE )#define SUPER_PAGE_DONE (pfn) ( SUPER_PAGE_START(pfn) ) -static int super_page_populated (unsigned long pfn) +static int super_page_populated (struct restore_ctx *ctx,unsigned long pfn) {int i; pfn &= ~(SUPERPAGE_NR_PFNS -1 );
@@ -87,6 +80,7 @@ static int super_page_populated(unsigned long pfn)*/ static int break_super_page (int xc_handle,uint32_t dom, +struct restore_ctx *ctx,xen_pfn_t next_pfn) {xen_pfn_t *page_array, start_pfn, mfn;
@@ -200,6 +194,7 @@ out:*/ static int allocate_mfn_list(int xc_handle, uint32_t dom, + struct restore_ctx *ctx, unsigned long nr_extents, xen_pfn_t *batch_buf, xen_pfn_t *next_pfn,
@@ -226,7 +221,7 @@ static int allocate_mfn_list(int xc_handle, !SUPER_PAGE_DONE(sp_pfn)) {/* break previously allocated super page*/ -if (break_super_page (xc_handle, dom, sp_pfn) !=0 ) +if (break_super_page (xc_handle, dom, ctx, sp_pfn) !=0 ) {ERROR ("Break previous super page fail!\n" );return 1 ;
@@ -249,7 +244,7 @@ static int allocate_mfn_list(int xc_handle, goto normal_page; pfn = batch_buf[0 ] & ~XEN_DOMCTL_PFINFO_LTAB_MASK ; -if (super_page_populated (pfn) ) +if (super_page_populated (ctx, pfn) ) goto normal_page; pfn &= ~(SUPERPAGE_NR_PFNS -1 );
@@ -300,6 +295,7 @@ normal_page: }static int allocate_physmem (int xc_handle,uint32_t dom, +struct restore_ctx *ctx,unsigned long *region_pfn_type,int region_size,unsigned int hvm,xen_pfn_t *region_mfn,int superpages) {
@@ -341,7 +337,7 @@ static int allocate_physmem(int xc_handle, uint32_t dom,if (SUPER_PAGE_START (pfn) ) {/* Start of a 2M extent, populate previsous buf */ -if (allocate_mfn_list (xc_handle, dom, +if (allocate_mfn_list (xc_handle, dom, ctx, batch_buf_len, batch_buf, &required_pfn, superpages) !=0 ) {
@@ -363,7 +359,7 @@ static int allocate_physmem(int xc_handle, uint32_t dom,else if (SUPER_PAGE_TRACKING (required_pfn) ) {/* break of a 2M extent, populate previous buf */ -if (allocate_mfn_list (xc_handle, dom, +if (allocate_mfn_list (xc_handle, dom, ctx, batch_buf_len, batch_buf, &required_pfn, superpages) !=0 ) {
@@ -404,7 +400,7 @@ static int allocate_physmem(int xc_handle, uint32_t dom, alloc_page:if ( batch_buf ) { -if (allocate_mfn_list (xc_handle, dom, +if (allocate_mfn_list (xc_handle, dom, ctx, batch_buf_len, batch_buf, &required_pfn, superpages) !=0 )
@@ -497,7 +493,7 @@ static ssize_t read_exact_timed(int fd, void* buf, size_t size)** This function inverts that operation, replacing the pfn values with ** the (now known) appropriate mfn values. */ -static int uncanonicalize_pagetable (int xc_handle,uint32_t dom, +static int uncanonicalize_pagetable (int xc_handle,uint32_t dom,struct restore_ctx *ctx,unsigned long type,void *page,int superpages) {int i, pte_last;
@@ -524,7 +520,7 @@ static int uncanonicalize_pagetable(int xc_handle, uint32_t dom,if ( ctx->p2m[pfn] ==INVALID_P2M_ENTRY ) {unsigned long force_pfn = superpages ?FORCE_SP_MASK : pfn; -if (allocate_mfn_list (xc_handle, dom, +if (allocate_mfn_list (xc_handle, dom, ctx,1 , &pfn, &force_pfn, superpages) !=0 )return 0 ; }
@@ -542,7 +538,7 @@ static int uncanonicalize_pagetable(int xc_handle, uint32_t dom,/* Load the p2m frame list, plus potential extended info chunk */ -static xen_pfn_t *load_p2m_frame_list ( +static xen_pfn_t *load_p2m_frame_list (struct restore_ctx *ctx,int io_fd,int *pae_extended_cr3,int *ext_vcpucontext) {xen_pfn_t *p2m_frame_list;
@@ -798,7 +794,7 @@ static int dump_qemu(uint32_t dom, struct tailbuf_hvm *buf)return 0 ; } -static int buffer_tail_hvm (struct tailbuf_hvm *buf,int fd, +static int buffer_tail_hvm (struct restore_ctx *ctx,struct tailbuf_hvm *buf,int fd,unsigned int max_vcpu_id,uint64_t vcpumap,int ext_vcpucontext) {
@@ -859,7 +855,7 @@ static int buffer_tail_hvm(struct tailbuf_hvm *buf, int fd,return -1 ; } -static int buffer_tail_pv (struct tailbuf_pv *buf,int fd, +static int buffer_tail_pv (struct restore_ctx *ctx,struct tailbuf_pv *buf,int fd,unsigned int max_vcpu_id,uint64_t vcpumap,int ext_vcpucontext) {
@@ -937,14 +933,14 @@ static int buffer_tail_pv(struct tailbuf_pv *buf, int fd,return -1 ; } -static int buffer_tail (tailbuf_t *buf,int fd,unsigned int max_vcpu_id, +static int buffer_tail (struct restore_ctx *ctx,tailbuf_t *buf,int fd,unsigned int max_vcpu_id,uint64_t vcpumap,int ext_vcpucontext) {if ( buf->ishvm ) -return buffer_tail_hvm (&buf->u.hvm, fd, max_vcpu_id, vcpumap, +return buffer_tail_hvm (ctx, &buf->u.hvm, fd, max_vcpu_id, vcpumap, ext_vcpucontext);else -return buffer_tail_pv (&buf->u.pv, fd, max_vcpu_id, vcpumap, +return buffer_tail_pv (ctx, &buf->u.pv, fd, max_vcpu_id, vcpumap, ext_vcpucontext); }
@@ -1161,8 +1157,8 @@ static int pagebuf_get(pagebuf_t* buf, int fd, int xch, uint32_t dom)return rc; } -static int apply_batch (int xc_handle,uint32_t dom,xen_pfn_t * region_mfn, -unsigned long * pfn_type,int pae_extended_cr3, +static int apply_batch (int xc_handle,uint32_t dom,struct restore_ctx *ctx, +xen_pfn_t * region_mfn,unsigned long * pfn_type,int pae_extended_cr3,unsigned int hvm,struct xc_mmu * mmu,pagebuf_t * pagebuf,int curbatch,int superpages) {
@@ -1182,7 +1178,7 @@ static int apply_batch(int xc_handle, uint32_t dom, xen_pfn_t* region_mfn,if (j >MAX_BATCH_SIZE ) j =MAX_BATCH_SIZE ; -if (allocate_physmem (xc_handle, dom, &pagebuf->pfn_types[curbatch], +if (allocate_physmem (xc_handle, dom, ctx, &pagebuf->pfn_types[curbatch], j, hvm, region_mfn, superpages) !=0 ) {ERROR ("allocate_physmem() failed\n" );
@@ -1243,7 +1239,7 @@ static int apply_batch(int xc_handle, uint32_t dom, xen_pfn_t* region_mfn, pae_extended_cr3 || (pagetype != XEN_DOMCTL_PFINFO_L1TAB)) { -if (!uncanonicalize_pagetable (xc_handle, dom, +if (!uncanonicalize_pagetable (xc_handle, dom, ctx, pagetype, page, superpages)) {/* ** Failing to uncanonicalize a page table can be ok
@@ -1350,6 +1346,12 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,tailbuf_t tailbuf, tmptail;void * vcpup; +static struct restore_ctx _ctx = { + .live_p2m =NULL , + .p2m =NULL , + .no_superpage_mem =0 , + }; +static struct restore_ctx *ctx = &_ctx;struct domain_info_context *dinfo = &ctx->dinfo;pagebuf_init (&pagebuf);
@@ -1386,7 +1388,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,if ( !hvm ) {/* Load the p2m frame list, plus potential extended info chunk */ - p2m_frame_list =load_p2m_frame_list ( + p2m_frame_list =load_p2m_frame_list (ctx, io_fd, &pae_extended_cr3, &ext_vcpucontext);if ( !p2m_frame_list ) goto out;
@@ -1500,7 +1502,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,while ( curbatch < j ) {int brc; - brc =apply_batch (xc_handle, dom, region_mfn, pfn_type, + brc =apply_batch (xc_handle, dom, ctx, region_mfn, pfn_type, pae_extended_cr3, hvm, mmu, &pagebuf, curbatch, superpages);if ( brc <0 ) goto out;
@@ -1541,7 +1543,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,if ( !completed ) {int flags =0 ; -if (buffer_tail (&tailbuf, io_fd, max_vcpu_id, vcpumap, +if (buffer_tail (ctx, &tailbuf, io_fd, max_vcpu_id, vcpumap, ext_vcpucontext) <0 ) {ERROR ("error buffering image tail" ); goto out;
@@ -1561,7 +1563,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom, }memset (&tmptail,0 ,sizeof (tmptail)); tmptail.ishvm = hvm; -if (buffer_tail (&tmptail, io_fd, max_vcpu_id, vcpumap, +if (buffer_tail (ctx, &tmptail, io_fd, max_vcpu_id, vcpumap, ext_vcpucontext) <0 ) {ERROR ("error buffering image tail, finishing" ); goto finish;
@@ -1664,7 +1666,7 @@ int xc_domain_restore(int xc_handle, int io_fd, uint32_t dom,for ( k =0 ; k < j; k++ ) {if ( !uncanonicalize_pagetable ( - xc_handle, dom,XEN_DOMCTL_PFINFO_L1TAB , + xc_handle, dom, ctx,XEN_DOMCTL_PFINFO_L1TAB , region_base + k*PAGE_SIZE , superpages) ) {ERROR ("failed uncanonicalize pt!" );
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel