[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH 4/6] tools/libxc: x86 pv common code
Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx> Signed-off-by: Frediano Ziglio <frediano.ziglio@xxxxxxxxxx> --- tools/libxc/saverestore/common.h | 79 ++++++++++++ tools/libxc/saverestore/common_x86_pv.c | 208 +++++++++++++++++++++++++++++++ tools/libxc/saverestore/common_x86_pv.h | 105 ++++++++++++++++ 3 files changed, 392 insertions(+) create mode 100644 tools/libxc/saverestore/common_x86_pv.c create mode 100644 tools/libxc/saverestore/common_x86_pv.h diff --git a/tools/libxc/saverestore/common.h b/tools/libxc/saverestore/common.h index fff0a39..4220c18 100644 --- a/tools/libxc/saverestore/common.h +++ b/tools/libxc/saverestore/common.h @@ -1,8 +1,16 @@ #ifndef __COMMON__H #define __COMMON__H +// Hack out junk from the namespace +#define mfn_to_pfn __UNUSED_mfn_to_pfn +#define pfn_to_mfn __UNUSED_pfn_to_mfn + #include "../xg_private.h" +#undef mfn_to_pfn +#undef pfn_to_mfn + + #include "stream_format.h" // TODO: Find a better place to put this... @@ -11,6 +19,77 @@ const char *dhdr_type_to_str(uint32_t type); const char *rec_type_to_str(uint32_t type); +struct context +{ + xc_interface *xch; + uint32_t domid; + int fd; + + xc_dominfo_t dominfo; + + union + { + struct + { + /* From Image Header */ + uint32_t format_version; + + /* From Domain Header */ + uint32_t guest_type; + uint32_t guest_page_size; + + unsigned long xenstore_mfn, console_mfn; + unsigned int xenstore_evtchn, console_evtchn; + domid_t xenstore_domid, console_domid; + } restore; + + struct + { + struct save_callbacks *callbacks; + } save; + }; + + union + { + struct + { + /* 4 or 8; 32 or 64 bit domain */ + unsigned int width; + /* 3 or 4 pagetable levels */ + unsigned int levels; + + + /* Maximum Xen frame */ + unsigned long max_mfn; + /* Read-only machine to phys map */ + xen_pfn_t *m2p; + /* firtst m2p mfn. Incorrectly based on toolstack bitness - FIXME */ + xen_pfn_t m2p_mfn0; + /* Number of m2p frames mapped */ + unsigned long nr_m2p_frames; + + + /* Maximum guest frame */ + unsigned long max_pfn; + /* Frames per page in guest p2m */ + unsigned int fpp; + + /* Number of frames making up the p2m */ + unsigned int p2m_frames; + /* Guest's phys to machine map. Mapped read-only (save) or + * allocated locally (restore). Uses guest unsigned longs. */ + void *p2m; + /* The guest pfns containing the p2m leaves */ + xen_pfn_t *p2m_pfns; + /* Types for each page */ + uint32_t *pfn_types; + + /* Read-only mapping of guests shared info page */ + shared_info_any_t *shinfo; + } x86_pv; + }; +}; + #endif /* * Local variables: diff --git a/tools/libxc/saverestore/common_x86_pv.c b/tools/libxc/saverestore/common_x86_pv.c new file mode 100644 index 0000000..71fb13d --- /dev/null +++ b/tools/libxc/saverestore/common_x86_pv.c @@ -0,0 +1,208 @@ +#include <assert.h> + +#include "common_x86_pv.h" + +xen_pfn_t mfn_to_pfn(struct context *ctx, xen_pfn_t mfn) +{ + assert(mfn <= ctx->x86_pv.max_mfn); + return ctx->x86_pv.m2p[mfn]; +} + +xen_pfn_t pfn_to_mfn(struct context *ctx, xen_pfn_t pfn) +{ + assert(pfn <= ctx->x86_pv.max_pfn); + + if ( ctx->x86_pv.width == sizeof (uint64_t) ) + /* 64 bit guest. Need to truncate their pfns for 32 bit toolstacks */ + return ((uint64_t *)ctx->x86_pv.p2m)[pfn]; + else + { + /* 32 bit guest. Need to expand INVALID_MFN fot 64 bit toolstacks */ + uint32_t mfn = ((uint32_t *)ctx->x86_pv.p2m)[pfn]; + + return mfn == ~0U ? INVALID_MFN : mfn; + } +} + +void set_p2m(struct context *ctx, xen_pfn_t pfn, xen_pfn_t mfn) +{ + assert(pfn <= ctx->x86_pv.max_pfn); + + if ( ctx->x86_pv.width == sizeof (uint64_t) ) + /* 64 bit guest. Need to expand INVALID_MFN for 32 bit toolstacks */ + ((uint64_t *)ctx->x86_pv.p2m)[pfn] = mfn == INVALID_MFN ? ~0ULL : mfn; + else + /* 32 bit guest. Can safely truncate INVALID_MFN fot 64 bit toolstacks */ + ((uint32_t *)ctx->x86_pv.p2m)[pfn] = mfn; +} + +bool mfn_in_pseudophysmap(struct context *ctx, xen_pfn_t mfn) +{ + return ( (mfn <= ctx->x86_pv.max_mfn) && + (mfn_to_pfn(ctx, mfn) <= ctx->x86_pv.max_pfn) && + (pfn_to_mfn(ctx, mfn_to_pfn(ctx, mfn) == mfn)) ); +} + +void pseudophysmap_walk(struct context *ctx, xen_pfn_t mfn) +{ + xc_interface *xch = ctx->xch; + xen_pfn_t pfn = ~0UL; + + ERROR("mfn %#lx, max %#lx", mfn, ctx->x86_pv.max_mfn); + + if ( (mfn != ~0UL) && (mfn <= ctx->x86_pv.max_mfn) ) + { + pfn = ctx->x86_pv.m2p[mfn]; + ERROR(" m2p[%#lx] = %#lx, max_pfn %#lx", + mfn, pfn, ctx->x86_pv.max_pfn); + } + + if ( (pfn != ~0UL) && (pfn <= ctx->x86_pv.max_pfn) ) + ERROR(" p2m[%#lx] = %#lx", + pfn, pfn_to_mfn(ctx, pfn)); +} + +xen_pfn_t cr3_to_mfn(struct context *ctx, uint64_t cr3) +{ + if ( ctx->x86_pv.width == 8 ) + return cr3 >> 12; + else + return (((uint32_t)cr3 >> 12) | ((uint32_t)cr3 << 20)); +} + +uint64_t mfn_to_cr3(struct context *ctx, xen_pfn_t mfn) +{ + if ( ctx->x86_pv.width == 8 ) + return ((uint64_t)mfn) << 12; + else + return (((uint32_t)mfn << 12) | ((uint32_t)mfn >> 20)); +} + +int x86_pv_domain_info(struct context *ctx) +{ + xc_interface *xch = ctx->xch; + unsigned int guest_width, guest_levels, fpp; + int max_pfn; + + /* Get the domain width */ + if ( xc_domain_get_guest_width(xch, ctx->domid, &guest_width) ) + { + PERROR("Unable to determine dom%d's width", ctx->domid); + return -1; + } + else if ( guest_width == 4 ) + guest_levels = 3; + else if ( guest_width == 8 ) + guest_levels = 4; + else + { + ERROR("Invalid guest width %d. Expected 32 or 64", guest_width); + return -1; + } + ctx->x86_pv.width = guest_width; + ctx->x86_pv.levels = guest_levels; + ctx->x86_pv.fpp = fpp = PAGE_SIZE / ctx->x86_pv.width; + + DPRINTF("%d bits, %d levels", guest_width * 8, guest_levels); + + /* Get the domains maximum pfn */ + max_pfn = xc_domain_maximum_gpfn(xch, ctx->domid); + if ( max_pfn < 0 ) + { + PERROR("Unable to obtain guests max pfn"); + return -1; + } + else if ( max_pfn >= ~XEN_DOMCTL_PFINFO_LTAB_MASK ) + { + errno = E2BIG; + PERROR("Cannot save a guest this large %#x"); + return -1; + } + else if ( max_pfn > 0 ) + { + ctx->x86_pv.max_pfn = max_pfn; + ctx->x86_pv.p2m_frames = (ctx->x86_pv.max_pfn + fpp) / fpp; + + DPRINTF("max_pfn %#x, p2m_frames %d", max_pfn, ctx->x86_pv.p2m_frames); + } + + return 0; +} + +int x86_pv_map_m2p(struct context *ctx) +{ + xc_interface *xch = ctx->xch; + long max_page = xc_maximum_ram_page(xch); + unsigned long m2p_chunks, m2p_size; + privcmd_mmap_entry_t *entries = NULL; + xen_pfn_t *extents_start = NULL; + int rc = -1, i; + + if ( max_page < 0 ) + { + PERROR("Failed to get maximum ram page"); + goto err; + } + + ctx->x86_pv.max_mfn = max_page; + m2p_size = M2P_SIZE(ctx->x86_pv.max_mfn); + m2p_chunks = M2P_CHUNKS(ctx->x86_pv.max_mfn); + + extents_start = malloc(m2p_chunks * sizeof(xen_pfn_t)); + if ( !extents_start ) + { + ERROR("Unable to allocate %zu bytes for m2p mfns", + m2p_chunks * sizeof(xen_pfn_t)); + goto err; + } + + if ( xc_machphys_mfn_list(xch, m2p_chunks, extents_start) ) + { + PERROR("Failed to get m2p mfn list"); + goto err; + } + + entries = malloc(m2p_chunks * sizeof(privcmd_mmap_entry_t)); + if ( !entries ) + { + ERROR("Unable to allocate %zu bytes for m2p mapping mfns", + m2p_chunks * sizeof(privcmd_mmap_entry_t)); + goto err; + } + + for ( i = 0; i < m2p_chunks; ++i ) + entries[i].mfn = extents_start[i]; + + ctx->x86_pv.m2p = xc_map_foreign_ranges( + xch, DOMID_XEN, m2p_size, PROT_READ, + M2P_CHUNK_SIZE, entries, m2p_chunks); + + if ( !ctx->x86_pv.m2p ) + { + PERROR("Failed to mmap m2p ranges"); + goto err; + } + + ctx->x86_pv.nr_m2p_frames = (M2P_CHUNK_SIZE >> PAGE_SHIFT) * m2p_chunks; + ctx->x86_pv.m2p_mfn0 = entries[0].mfn; + + /* All Done */ + rc = 0; + DPRINTF("max_mfn %#lx", ctx->x86_pv.max_mfn); + +err: + free(entries); + free(extents_start); + + return rc; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/tools/libxc/saverestore/common_x86_pv.h b/tools/libxc/saverestore/common_x86_pv.h new file mode 100644 index 0000000..ba1b60f --- /dev/null +++ b/tools/libxc/saverestore/common_x86_pv.h @@ -0,0 +1,105 @@ +#ifndef __COMMON_X86_PV_H +#define __COMMON_X86_PV_H + +#include <stdbool.h> +#include "common.h" + +/* + * Convert an mfn to a pfn, given Xens m2p table. + * + * Caller must ensure that the requested mfn is in range. + */ +xen_pfn_t mfn_to_pfn(struct context *ctx, xen_pfn_t mfn); + +/* + * Convert a pfn to an mfn, given the guests p2m table. + * + * Caller must ensure that the requested pfn is in range. + */ +xen_pfn_t pfn_to_mfn(struct context *ctx, xen_pfn_t pfn); + +/* + * Set a mapping in the p2m table. + * + * Caller must ensure that the requested pfn is in range. + */ +void set_p2m(struct context *ctx, xen_pfn_t pfn, xen_pfn_t mfn); + +/* + * Query whether a particular mfn is valid in the physmap of a guest. + */ +bool mfn_in_pseudophysmap(struct context *ctx, xen_pfn_t mfn); + +/* + * Debug a particular mfn by walking the p2m and m2p. + */ +void pseudophysmap_walk(struct context *ctx, xen_pfn_t mfn); + +/* + * Convert a PV cr3 field to an mfn. + */ +xen_pfn_t cr3_to_mfn(struct context *ctx, uint64_t cr3); + +/* + * Convert an mfn to a PV cr3 field. + */ +uint64_t mfn_to_cr3(struct context *ctx, xen_pfn_t mfn); + +/* + * Extract an MFN from a Pagetable Entry. + */ +static inline xen_pfn_t pte_to_frame(struct context *ctx, uint64_t pte) +{ + if ( ctx->x86_pv.width == 8 ) + return (pte >> PAGE_SHIFT) & ((1ULL << (52 - PAGE_SHIFT)) - 1); + else + return (pte >> PAGE_SHIFT) & ((1ULL << (44 - PAGE_SHIFT)) - 1); +} + +static inline void update_pte(struct context *ctx, uint64_t *pte, xen_pfn_t pfn) +{ + if ( ctx->x86_pv.width == 8 ) + *pte &= ~(((1ULL << (52 - PAGE_SHIFT)) - 1) << PAGE_SHIFT); + else + *pte &= ~(((1ULL << (44 - PAGE_SHIFT)) - 1) << PAGE_SHIFT); + + *pte |= (uint64_t)pfn << PAGE_SHIFT; +} + +/* + * Get current domain information. + * + * Fills ctx->x86_pv + * - .width + * - .levels + * - .fpp + * - .p2m_frames + * + * Used by the save side to create the X86_PV_INFO record, and by the restore + * side to verify the incoming stream. + * + * Returns 0 on success and non-zero on error. + */ +int x86_pv_domain_info(struct context *ctx); + +/* + * Maps the Xen M2P. + * + * Fills ctx->x86_pv. + * - .max_mfn + * - .m2p + * + * Returns 0 on success and non-zero on error. + */ +int x86_pv_map_m2p(struct context *ctx); + +#endif +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ -- 1.7.10.4 _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |