From: "Jan Beulich" Subject: [PATCH v4 3/2] libxc/PV: save/restore data breakpoint extension registers Date: Wed, 16 Apr 2014 15:35:18 +0100 Message-ID: <534EB1460200007800009971@nat28.tlf.novell.com> References: <534EAE6402000078000098FB@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartD6E4C636.2__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WaQvq-0003bt-8y for xen-devel@lists.xenproject.org; Wed, 16 Apr 2014 14:35:22 +0000 In-Reply-To: <534EAE6402000078000098FB@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Keir Fraser , Tim Deegan , Ian Jackson , Ian Campbell , Aravind Gopalakrishnan , suravee.suthikulpanit@amd.com List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartD6E4C636.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Requiring the handling of the generic MSR extension to XEN_DOMCTL_[gs]et_ext_vcpucontext. Signed-off-by: Jan Beulich --- a/tools/libxc/xc_domain_restore.c +++ b/tools/libxc/xc_domain_restore.c @@ -38,6 +38,7 @@ =20 #include #include +#include =20 #include "xg_private.h" #include "xg_save_restore.h" @@ -590,8 +591,13 @@ static int buffer_tail_pv(xc_interface * uint32_t vcpuextstate_size) { unsigned int i; - size_t pfnlen, vcpulen; + size_t pfnlen, vcpulen, total; + int alloc =3D 0; struct domain_info_context *dinfo =3D &ctx->dinfo; + union { + const unsigned char *raw; + xen_domctl_ext_vcpucontext_t *evc; + } ptr; =20 /* TODO: handle changing pfntab and vcpu counts */ /* PFN tab */ @@ -634,11 +640,42 @@ static int buffer_tail_pv(xc_interface * ERROR("Error allocating VCPU ctxt tail buffer"); goto free_pfntab; } + alloc =3D 1; } // DPRINTF("Reading VCPUS: %d bytes\n", vcpulen); - if ( RDEXACT(fd, buf->vcpubuf, vcpulen) ) { - PERROR("Error when reading ctxt"); - goto free_vcpus; + for ( total =3D i =3D 0, ptr.raw =3D buf->vcpubuf; ext_vcpucontext; ) = { + if ( RDEXACT(fd, buf->vcpubuf + total, vcpulen) ) { + PERROR("Error when reading ctxt"); + goto free_vcpus; + } + total +=3D vcpulen; + for ( vcpulen =3D 0; i < buf->vcpucount; ++i ) { + size_t msrlen; + + if ( (const unsigned char *)(ptr.evc + 1) > buf->vcpubuf + = total ) + break; + if ( ptr.evc->size < + (offsetof(xen_domctl_ext_vcpucontext_t, msrs) + + sizeof(ptr.evc->msrs)) ) + ptr.evc->msr_count =3D 0; + msrlen =3D ptr.evc->msr_count * sizeof(xen_domctl_ext_vcpu_msr= _t); + vcpulen +=3D msrlen; + ptr.raw +=3D 128 + msrlen + vcpuextstate_size; + } + if ( !vcpulen ) { + assert(i =3D=3D buf->vcpucount); + break; + } + if ( alloc ) { + void *nbuf =3D realloc(buf->vcpubuf, total + vcpulen); + + if ( !nbuf ) { + ERROR("Error growing VCPU ctxt tail buffer"); + goto free_vcpus; + } + ptr.raw =3D nbuf + (ptr.raw - buf->vcpubuf); + buf->vcpubuf =3D nbuf; + } } =20 /* load shared_info_page */ @@ -2130,9 +2167,28 @@ int xc_domain_restore(xc_interface *xch, goto vcpu_ext_state_restore; memcpy(&domctl.u.ext_vcpucontext, vcpup, 128); vcpup +=3D 128; - domctl.cmd =3D XEN_DOMCTL_set_ext_vcpucontext; - domctl.domain =3D dom; - frc =3D xc_domctl(xch, &domctl); + if ( domctl.u.ext_vcpucontext.msr_count ) + { + xen_domctl_ext_vcpu_msr_t *msrs =3D (void *)vcpup; + size_t sz =3D domctl.u.ext_vcpucontext.msr_count * sizeof(*msr= s); + DECLARE_HYPERCALL_BOUNCE(msrs, sz, XC_HYPERCALL_BUFFER_BOUNCE_= IN); + + if ( xc_hypercall_bounce_pre(xch, msrs) ) + { + PERROR("Can't bounce in vcpu%d MSRs", i); + goto out; + } + set_xen_guest_handle(domctl.u.ext_vcpucontext.msrs, msrs); + domctl.cmd =3D XEN_DOMCTL_set_ext_vcpucontext; + domctl.domain =3D dom; + frc =3D xc_domctl(xch, &domctl); + xc_hypercall_bounce_post(xch, msrs); + vcpup +=3D sz; + } else { + domctl.cmd =3D XEN_DOMCTL_set_ext_vcpucontext; + domctl.domain =3D dom; + frc =3D xc_domctl(xch, &domctl); + } if ( frc !=3D 0 ) { PERROR("Couldn't set extended vcpu%d info", i); --- a/tools/libxc/xc_domain_save.c +++ b/tools/libxc/xc_domain_save.c @@ -836,6 +836,9 @@ int xc_domain_save(xc_interface *xch, in /* base of the region in which domain memory is mapped */ unsigned char *region_base =3D NULL; =20 + /* MSR extensions to xen_domctl_ext_vcpucontext_t */ + DECLARE_HYPERCALL_BUFFER(xen_domctl_ext_vcpu_msr_t, msrs); + /* A copy of the CPU eXtended States of the guest. */ DECLARE_HYPERCALL_BUFFER(void, buffer); =20 @@ -1960,16 +1963,42 @@ int xc_domain_save(xc_interface *xch, in domctl.domain =3D dom; memset(&domctl.u, 0, sizeof(domctl.u)); domctl.u.ext_vcpucontext.vcpu =3D i; - if ( xc_domctl(xch, &domctl) < 0 ) + frc =3D xc_domctl(xch, &domctl); + if ( frc < 0 && errno =3D=3D ENOBUFS && + domctl.u.ext_vcpucontext.size >=3D + (offsetof(xen_domctl_ext_vcpucontext_t, msrs) + + sizeof(domctl.u.ext_vcpucontext.msrs)) && + domctl.u.ext_vcpucontext.msr_count ) + { + msrs =3D xc_hypercall_buffer_alloc(xch, msrs, + domctl.u.ext_vcpucontext.msr_= count * + sizeof(*msrs)); + set_xen_guest_handle(domctl.u.ext_vcpucontext.msrs, msrs); + frc =3D msrs ? xc_domctl(xch, &domctl) : -1; + /* Don't save the actual pointer. */ + set_xen_guest_handle_raw(domctl.u.ext_vcpucontext.msrs, = NULL); + } + if ( frc < 0 ) { PERROR("No extended context for VCPU%d", i); goto out; } if ( wrexact(io_fd, &domctl.u.ext_vcpucontext, 128) ) { - PERROR("Error when writing to state file (2)"); + PERROR("Error when writing to state file (ext ctxt)"); goto out; } + if ( msrs ) + { + if ( wrexact(io_fd, msrs, + domctl.u.ext_vcpucontext.msr_count * sizeof(*msrs= )) ) + { + PERROR("Error when writing to state file (MSRs)"); + goto out; + } + xc_hypercall_buffer_free(xch, msrs); + msrs =3D NULL; + } =20 /* Start to fetch CPU eXtended States */ /* Get buffer size first */ @@ -2134,6 +2163,7 @@ int xc_domain_save(xc_interface *xch, in =20 xc_hypercall_buffer_free_pages(xch, to_send, NRPAGES(bitmap_size(dinfo= ->p2m_size))); xc_hypercall_buffer_free_pages(xch, to_skip, NRPAGES(bitmap_size(dinfo= ->p2m_size))); + xc_hypercall_buffer_free(xch, msrs); =20 free(pfn_type); free(pfn_batch); --- a/tools/libxc/xg_save_restore.h +++ b/tools/libxc/xg_save_restore.h @@ -62,6 +62,9 @@ * the block size. * "extv" : Presence indicates use of extended VCPU context = in * tail, data size is 0. + * This block may be followed by an array of + * xen_domctl_ext_vcpu_msr_t as indicated by + * xen_domctl_ext_vcpucontext_t's msr_count field. * * p2m (PV-only): * --=__PartD6E4C636.2__= Content-Type: text/plain; name="PV-debug-address-mask-MSRs-sr.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="PV-debug-address-mask-MSRs-sr.patch" libxc/PV: save/restore data breakpoint extension registers=0A=0ARequiring = the handling of the generic MSR extension to=0AXEN_DOMCTL_[gs]et_ext_vcpuco= ntext.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/tools/libxc/xc_domain_restore.c=0A+++ b/tools/libxc/xc_domain_restore.c= =0A@@ -38,6 +38,7 @@=0A =0A #include =0A #include =0A+#= include =0A =0A #include "xg_private.h"=0A #include "xg_save_rest= ore.h"=0A@@ -590,8 +591,13 @@ static int buffer_tail_pv(xc_interface *=0A = uint32_t vcpuextstate_size)=0A {=0A unsigned = int i;=0A- size_t pfnlen, vcpulen;=0A+ size_t pfnlen, vcpulen, = total;=0A+ int alloc =3D 0;=0A struct domain_info_context *dinfo = =3D &ctx->dinfo;=0A+ union {=0A+ const unsigned char *raw;=0A+ = xen_domctl_ext_vcpucontext_t *evc;=0A+ } ptr;=0A =0A /* TODO: = handle changing pfntab and vcpu counts */=0A /* PFN tab */=0A@@ = -634,11 +640,42 @@ static int buffer_tail_pv(xc_interface *=0A = ERROR("Error allocating VCPU ctxt tail buffer");=0A goto = free_pfntab;=0A }=0A+ alloc =3D 1;=0A }=0A // = DPRINTF("Reading VCPUS: %d bytes\n", vcpulen);=0A- if ( RDEXACT(fd, = buf->vcpubuf, vcpulen) ) {=0A- PERROR("Error when reading ctxt");=0A= - goto free_vcpus;=0A+ for ( total =3D i =3D 0, ptr.raw =3D = buf->vcpubuf; ext_vcpucontext; ) {=0A+ if ( RDEXACT(fd, buf->vcpubuf= + total, vcpulen) ) {=0A+ PERROR("Error when reading ctxt");=0A= + goto free_vcpus;=0A+ }=0A+ total +=3D = vcpulen;=0A+ for ( vcpulen =3D 0; i < buf->vcpucount; ++i ) {=0A+ = size_t msrlen;=0A+=0A+ if ( (const unsigned char = *)(ptr.evc + 1) > buf->vcpubuf + total )=0A+ break;=0A+ = if ( ptr.evc->size <=0A+ (offsetof(xen_domctl_ext_vc= pucontext_t, msrs) +=0A+ sizeof(ptr.evc->msrs)) )=0A+ = ptr.evc->msr_count =3D 0;=0A+ msrlen =3D ptr.evc->msr= _count * sizeof(xen_domctl_ext_vcpu_msr_t);=0A+ vcpulen +=3D = msrlen;=0A+ ptr.raw +=3D 128 + msrlen + vcpuextstate_size;=0A+ = }=0A+ if ( !vcpulen ) {=0A+ assert(i =3D=3D = buf->vcpucount);=0A+ break;=0A+ }=0A+ if ( alloc = ) {=0A+ void *nbuf =3D realloc(buf->vcpubuf, total + = vcpulen);=0A+=0A+ if ( !nbuf ) {=0A+ ERROR("Error= growing VCPU ctxt tail buffer");=0A+ goto free_vcpus;=0A+ = }=0A+ ptr.raw =3D nbuf + (ptr.raw - buf->vcpubuf);=0A+= buf->vcpubuf =3D nbuf;=0A+ }=0A }=0A =0A /* = load shared_info_page */=0A@@ -2130,9 +2167,28 @@ int xc_domain_restore(xc_= interface *xch,=0A goto vcpu_ext_state_restore;=0A = memcpy(&domctl.u.ext_vcpucontext, vcpup, 128);=0A vcpup +=3D = 128;=0A- domctl.cmd =3D XEN_DOMCTL_set_ext_vcpucontext;=0A- = domctl.domain =3D dom;=0A- frc =3D xc_domctl(xch, &domctl);=0A+ = if ( domctl.u.ext_vcpucontext.msr_count )=0A+ {=0A+ = xen_domctl_ext_vcpu_msr_t *msrs =3D (void *)vcpup;=0A+ size_t = sz =3D domctl.u.ext_vcpucontext.msr_count * sizeof(*msrs);=0A+ = DECLARE_HYPERCALL_BOUNCE(msrs, sz, XC_HYPERCALL_BUFFER_BOUNCE_IN);=0A+=0A+ = if ( xc_hypercall_bounce_pre(xch, msrs) )=0A+ {=0A+ = PERROR("Can't bounce in vcpu%d MSRs", i);=0A+ = goto out;=0A+ }=0A+ set_xen_guest_handle(domctl.u.ext= _vcpucontext.msrs, msrs);=0A+ domctl.cmd =3D XEN_DOMCTL_set_ext_= vcpucontext;=0A+ domctl.domain =3D dom;=0A+ frc =3D = xc_domctl(xch, &domctl);=0A+ xc_hypercall_bounce_post(xch, = msrs);=0A+ vcpup +=3D sz;=0A+ } else {=0A+ = domctl.cmd =3D XEN_DOMCTL_set_ext_vcpucontext;=0A+ domctl.domain= =3D dom;=0A+ frc =3D xc_domctl(xch, &domctl);=0A+ }=0A = if ( frc !=3D 0 )=0A {=0A PERROR("Couldn't set = extended vcpu%d info", i);=0A--- a/tools/libxc/xc_domain_save.c=0A+++ = b/tools/libxc/xc_domain_save.c=0A@@ -836,6 +836,9 @@ int xc_domain_save(xc_= interface *xch, in=0A /* base of the region in which domain memory is = mapped */=0A unsigned char *region_base =3D NULL;=0A =0A+ /* MSR = extensions to xen_domctl_ext_vcpucontext_t */=0A+ DECLARE_HYPERCALL_BUFF= ER(xen_domctl_ext_vcpu_msr_t, msrs);=0A+=0A /* A copy of the CPU = eXtended States of the guest. */=0A DECLARE_HYPERCALL_BUFFER(void, = buffer);=0A =0A@@ -1960,16 +1963,42 @@ int xc_domain_save(xc_interface = *xch, in=0A domctl.domain =3D dom;=0A memset(&domctl.u, 0, = sizeof(domctl.u));=0A domctl.u.ext_vcpucontext.vcpu =3D i;=0A- = if ( xc_domctl(xch, &domctl) < 0 )=0A+ frc =3D xc_domctl(xch, = &domctl);=0A+ if ( frc < 0 && errno =3D=3D ENOBUFS &&=0A+ = domctl.u.ext_vcpucontext.size >=3D=0A+ (offsetof(xen_domctl_e= xt_vcpucontext_t, msrs) +=0A+ sizeof(domctl.u.ext_vcpucontext.= msrs)) &&=0A+ domctl.u.ext_vcpucontext.msr_count )=0A+ = {=0A+ msrs =3D xc_hypercall_buffer_alloc(xch, msrs,=0A+ = domctl.u.ext_vcpucontext.msr_count = *=0A+ sizeof(*msrs));=0A+ = set_xen_guest_handle(domctl.u.ext_vcpucontext.msrs, msrs);=0A+ = frc =3D msrs ? xc_domctl(xch, &domctl) : -1;=0A+ /* Don't = save the actual pointer. */=0A+ set_xen_guest_handle_raw(domctl.= u.ext_vcpucontext.msrs, NULL);=0A+ }=0A+ if ( frc < 0 )=0A = {=0A PERROR("No extended context for VCPU%d", i);=0A = goto out;=0A }=0A if ( wrexact(io_fd, &domctl.u.ext= _vcpucontext, 128) )=0A {=0A- PERROR("Error when = writing to state file (2)");=0A+ PERROR("Error when writing to = state file (ext ctxt)");=0A goto out;=0A }=0A+ = if ( msrs )=0A+ {=0A+ if ( wrexact(io_fd, msrs,=0A+ = domctl.u.ext_vcpucontext.msr_count * sizeof(*msrs)) = )=0A+ {=0A+ PERROR("Error when writing to state = file (MSRs)");=0A+ goto out;=0A+ }=0A+ = xc_hypercall_buffer_free(xch, msrs);=0A+ msrs =3D NULL;=0A+ = }=0A =0A /* Start to fetch CPU eXtended States */=0A = /* Get buffer size first */=0A@@ -2134,6 +2163,7 @@ int xc_domain_save(xc_i= nterface *xch, in=0A =0A xc_hypercall_buffer_free_pages(xch, to_send, = NRPAGES(bitmap_size(dinfo->p2m_size)));=0A xc_hypercall_buffer_free_pag= es(xch, to_skip, NRPAGES(bitmap_size(dinfo->p2m_size)));=0A+ xc_hypercal= l_buffer_free(xch, msrs);=0A =0A free(pfn_type);=0A free(pfn_batch)= ;=0A--- a/tools/libxc/xg_save_restore.h=0A+++ b/tools/libxc/xg_save_restore= .h=0A@@ -62,6 +62,9 @@=0A * the block size.=0A * = "extv" : Presence indicates use of extended VCPU context = in=0A * tail, data size is 0.=0A+ * = This block may be followed by an array of=0A+ * = xen_domctl_ext_vcpu_msr_t as indicated by=0A+ * = xen_domctl_ext_vcpucontext_t's msr_count field.=0A *=0A * p2m (PV-only):= =0A *=0A --=__PartD6E4C636.2__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__PartD6E4C636.2__=--