From: "Jan Beulich" Subject: [PATCH v2 RFC 2/4] x86/PV: support data breakpoint extension registers Date: Wed, 26 Mar 2014 14:13:19 +0000 Message-ID: <5332EE8F02000078000026BA@nat28.tlf.novell.com> References: <532880230200007800125450@nat28.tlf.novell.com> <53296E90020000780012593B@nat28.tlf.novell.com> <53296E90020000780012593B@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part86B4EA6F.3__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WSoa5-0001Bn-VG for xen-devel@lists.xenproject.org; Wed, 26 Mar 2014 14:13:26 +0000 In-Reply-To: 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 , Ian Jackson , Ian Campbell , suravee.suthikulpanit@amd.com, Sherry Hurwitz 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. --=__Part86B4EA6F.3__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Introducing an extension to XEN_DOMCTL_[gs]et_ext_vcpucontext similar to the generic MSR save/restore logic recently added for HVM. This also moves some debug register related declarations/definition to the header intended for these. Signed-off-by: Jan Beulich --- v2: libxc adjustment put in place (depending on the separately posted http://lists.xenproject.org/archives/html/xen-devel/2014-03/msg03059.ht= ml) --- a/tools/libxc/xc_domain_restore.c +++ b/tools/libxc/xc_domain_restore.c @@ -590,8 +590,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; + const xen_domctl_ext_vcpucontext_t *evc; + } ptr; =20 /* TODO: handle changing pfntab and vcpu counts */ /* PFN tab */ @@ -634,11 +639,36 @@ 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; + 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) + 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 */ @@ -1996,6 +2026,8 @@ int xc_domain_restore(xc_interface *xch, vcpup =3D tailbuf.u.pv.vcpubuf; for ( i =3D 0; i <=3D max_vcpu_id; i++ ) { + DECLARE_HYPERCALL_BUFFER(xen_domctl_ext_vcpu_msr_t, msrs); + if ( !(vcpumap[i/64] & (1ULL << (i%64))) ) continue; =20 @@ -2130,9 +2162,25 @@ int xc_domain_restore(xc_interface *xch, goto vcpu_ext_state_restore; memcpy(&domctl.u.ext_vcpucontext, vcpup, 128); vcpup +=3D 128; + if ( domctl.u.ext_vcpucontext.msr_count ) + { + size_t sz =3D domctl.u.ext_vcpucontext.msr_count * sizeof(*msr= s); + + msrs =3D xc_hypercall_buffer_alloc(xch, msrs, sz); + if ( !msrs ) + { + PERROR("No memory for vcpu%d MSRs", i); + goto out; + } + memcpy(msrs, vcpup, sz); + vcpup +=3D sz; + 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); + if ( msrs ) + xc_hypercall_buffer_free(xch, msrs); 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,36 @@ 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.m= sr_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; + } + 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 +2157,8 @@ 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))); + if (msrs) + xc_hypercall_buffer_free(xch, msrs); =20 free(pfn_type); free(pfn_batch); --- a/xen/arch/x86/acpi/suspend.c +++ b/xen/arch/x86/acpi/suspend.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -1316,14 +1316,7 @@ static void paravirt_ctxt_switch_to(stru write_cr4(cr4); =20 if ( unlikely(v->arch.debugreg[7] & DR7_ACTIVE_MASK) ) - { - write_debugreg(0, v->arch.debugreg[0]); - write_debugreg(1, v->arch.debugreg[1]); - write_debugreg(2, v->arch.debugreg[2]); - write_debugreg(3, v->arch.debugreg[3]); - write_debugreg(6, v->arch.debugreg[6]); - write_debugreg(7, v->arch.debugreg[7]); - } + activate_debugregs(v); =20 if ( (v->domain->arch.tsc_mode =3D=3D TSC_MODE_PVRDTSCP) && boot_cpu_has(X86_FEATURE_RDTSCP) ) --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -52,6 +52,7 @@ long arch_do_domctl( { long ret =3D 0; bool_t copyback =3D 0; + unsigned long i; =20 switch ( domctl->cmd ) { @@ -319,7 +320,6 @@ long arch_do_domctl( =20 case XEN_DOMCTL_getmemlist: { - int i; unsigned long max_pfns =3D domctl->u.getmemlist.max_pfns; uint64_t mfn; struct page_info *page; @@ -645,7 +645,6 @@ long arch_do_domctl( unsigned long mfn =3D domctl->u.memory_mapping.first_mfn; unsigned long nr_mfns =3D domctl->u.memory_mapping.nr_mfns; int add =3D domctl->u.memory_mapping.add_mapping; - unsigned long i; =20 ret =3D -EINVAL; if ( (mfn + nr_mfns - 1) < mfn || /* wrap? */ @@ -809,6 +808,7 @@ long arch_do_domctl( { struct xen_domctl_ext_vcpucontext *evc; struct vcpu *v; + struct xen_domctl_ext_vcpu_msr msr; =20 evc =3D &domctl->u.ext_vcpucontext; =20 @@ -854,7 +854,42 @@ long arch_do_domctl( evc->vmce.mci_ctl2_bank0 =3D v->arch.vmce.bank[0].mci_ctl2; evc->vmce.mci_ctl2_bank1 =3D v->arch.vmce.bank[1].mci_ctl2; =20 - ret =3D 0; + i =3D ret =3D 0; + if ( boot_cpu_has(X86_FEATURE_DBEXT) ) + { + unsigned int j; + + if ( v->arch.pv_vcpu.dr_mask[0] ) + { + if ( i < evc->msr_count && !ret ) + { + msr.index =3D MSR_AMD64_DR0_ADDRESS_MASK; + msr.reserved =3D 0; + msr.value =3D v->arch.pv_vcpu.dr_mask[0]; + if ( copy_to_guest_offset(evc->msrs, i, &msr, 1) = ) + ret =3D -EFAULT; + } + ++i; + } + for ( j =3D 0; j < 3; ++j ) + { + if ( !v->arch.pv_vcpu.dr_mask[1 + j] ) + continue; + if ( i < evc->msr_count && !ret ) + { + msr.index =3D MSR_AMD64_DR1_ADDRESS_MASK + j; + msr.reserved =3D 0; + msr.value =3D v->arch.pv_vcpu.dr_mask[1 + j]; + if ( copy_to_guest_offset(evc->msrs, i, &msr, 1) = ) + ret =3D -EFAULT; + } + ++i; + } + } + if ( i > evc->msr_count && !ret ) + ret =3D -ENOBUFS; + evc->msr_count =3D i; + vcpu_unpause(v); copyback =3D 1; } @@ -909,9 +944,49 @@ long arch_do_domctl( =20 ret =3D vmce_restore_vcpu(v, &vmce); } + else if ( evc->size > offsetof(typeof(*evc), vmce) ) + ret =3D -EINVAL; else ret =3D 0; =20 + if ( ret || evc->size <=3D offsetof(typeof(*evc), msrs) ) + /* nothing */; + else if ( evc->size < offsetof(typeof(*evc), msrs) + + sizeof(evc->msrs) ) + ret =3D -EINVAL; + else + { + for ( i =3D 0; i < evc->msr_count; ++i ) + { + ret =3D -EFAULT; + if ( copy_from_guest_offset(&msr, evc->msrs, i, 1) ) + break; + ret =3D -EINVAL; + if ( msr.reserved ) + break; + switch ( msr.index ) + { + case MSR_AMD64_DR0_ADDRESS_MASK: + if ( !boot_cpu_has(X86_FEATURE_DBEXT) || + (msr.value >> 32) ) + break; + v->arch.pv_vcpu.dr_mask[0] =3D msr.value; + continue; + case MSR_AMD64_DR1_ADDRESS_MASK ... + MSR_AMD64_DR3_ADDRESS_MASK: + if ( !boot_cpu_has(X86_FEATURE_DBEXT) || + (msr.value >> 32) ) + break; + msr.index -=3D MSR_AMD64_DR1_ADDRESS_MASK - 1; + v->arch.pv_vcpu.dr_mask[msr.index] =3D msr.value; + continue; + } + break; + } + if ( i =3D=3D evc->msr_count ) + ret =3D 0; + } + domain_unpause(d); } } @@ -921,7 +996,6 @@ long arch_do_domctl( { xen_domctl_cpuid_t *ctl =3D &domctl->u.cpuid; cpuid_input_t *cpuid =3D NULL;=20 - int i; =20 for ( i =3D 0; i < MAX_CPUID_INPUT; i++ ) { --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -2498,6 +2498,23 @@ static int emulate_privileged_op(struct=20 if ( wrmsr_safe(regs->ecx, msr_content) !=3D 0 ) goto fail; break; + + case MSR_AMD64_DR0_ADDRESS_MASK: + if ( !boot_cpu_has(X86_FEATURE_DBEXT) || (msr_content >> 32) = ) + goto fail; + v->arch.pv_vcpu.dr_mask[0] =3D msr_content; + if ( v->arch.debugreg[7] & DR7_ACTIVE_MASK ) + wrmsrl(MSR_AMD64_DR0_ADDRESS_MASK, msr_content); + break; + case MSR_AMD64_DR1_ADDRESS_MASK ... MSR_AMD64_DR3_ADDRESS_MASK: + if ( !boot_cpu_has(X86_FEATURE_DBEXT) || (msr_content >> 32) = ) + goto fail; + v->arch.pv_vcpu.dr_mask + [regs->_ecx - MSR_AMD64_DR1_ADDRESS_MASK + 1] =3D = msr_content; + if ( v->arch.debugreg[7] & DR7_ACTIVE_MASK ) + wrmsrl(regs->_ecx, msr_content); + break; + default: if ( wrmsr_hypervisor_regs(regs->ecx, msr_content) =3D=3D 1 ) break; @@ -2585,6 +2602,21 @@ static int emulate_privileged_op(struct=20 regs->eax =3D (uint32_t)msr_content; regs->edx =3D (uint32_t)(msr_content >> 32); break; + + case MSR_AMD64_DR0_ADDRESS_MASK: + if ( !boot_cpu_has(X86_FEATURE_DBEXT) ) + goto fail; + regs->eax =3D v->arch.pv_vcpu.dr_mask[0]; + regs->edx =3D 0; + break; + case MSR_AMD64_DR1_ADDRESS_MASK ... MSR_AMD64_DR3_ADDRESS_MASK: + if ( !boot_cpu_has(X86_FEATURE_DBEXT) ) + goto fail; + regs->eax =3D v->arch.pv_vcpu.dr_mask + [regs->_ecx - MSR_AMD64_DR1_ADDRESS_MASK + = 1]; + regs->edx =3D 0; + break; + default: if ( rdmsr_hypervisor_regs(regs->ecx, &val) ) { @@ -3628,7 +3660,27 @@ long do_set_trap_table(XEN_GUEST_HANDLE_ return rc; } =20 -long set_debugreg(struct vcpu *v, int reg, unsigned long value) +void activate_debugregs(const struct vcpu *curr) +{ + ASSERT(curr =3D=3D current); + + write_debugreg(0, curr->arch.debugreg[0]); + write_debugreg(1, curr->arch.debugreg[1]); + write_debugreg(2, curr->arch.debugreg[2]); + write_debugreg(3, curr->arch.debugreg[3]); + write_debugreg(6, curr->arch.debugreg[6]); + write_debugreg(7, curr->arch.debugreg[7]); + + if ( boot_cpu_has(X86_FEATURE_DBEXT) ) + { + wrmsrl(MSR_AMD64_DR0_ADDRESS_MASK, curr->arch.pv_vcpu.dr_mask[0]);= + wrmsrl(MSR_AMD64_DR1_ADDRESS_MASK, curr->arch.pv_vcpu.dr_mask[1]);= + wrmsrl(MSR_AMD64_DR2_ADDRESS_MASK, curr->arch.pv_vcpu.dr_mask[2]);= + wrmsrl(MSR_AMD64_DR3_ADDRESS_MASK, curr->arch.pv_vcpu.dr_mask[3]);= + } +} + +long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value) { int i; struct vcpu *curr =3D current; @@ -3709,11 +3761,8 @@ long set_debugreg(struct vcpu *v, int re if ( (v =3D=3D curr) && !(v->arch.debugreg[7] & DR7_ACTIVE_MASK) ) { - write_debugreg(0, v->arch.debugreg[0]); - write_debugreg(1, v->arch.debugreg[1]); - write_debugreg(2, v->arch.debugreg[2]); - write_debugreg(3, v->arch.debugreg[3]); - write_debugreg(6, v->arch.debugreg[6]); + activate_debugregs(curr); + break; } } if ( v =3D=3D curr ) --- a/xen/include/asm-x86/debugreg.h +++ b/xen/include/asm-x86/debugreg.h @@ -64,4 +64,16 @@ #define DR_GLOBAL_EXACT_ENABLE (0x00000200ul) /* Global exact enable */ #define DR_GENERAL_DETECT (0x00002000ul) /* General detect enable = */ =20 +#define write_debugreg(reg, val) do { \ + unsigned long __val =3D val; \ + asm volatile ( "mov %0,%%db" #reg : : "r" (__val) ); \ +} while (0) +#define read_debugreg(reg) ({ \ + unsigned long __val; \ + asm volatile ( "mov %%db" #reg ",%0" : "=3Dr" (__val) ); \ + __val; \ +}) +long set_debugreg(struct vcpu *, unsigned int reg, unsigned long value); +void activate_debugregs(const struct vcpu *); + #endif /* _X86_DEBUGREG_H */ --- a/xen/include/asm-x86/domain.h +++ b/xen/include/asm-x86/domain.h @@ -374,6 +374,9 @@ struct pv_vcpu unsigned long shadow_ldt_mapcnt; spinlock_t shadow_ldt_lock; =20 + /* data breakpoint extension MSRs */ + uint32_t dr_mask[4]; + /* Deferred VA-based update state. */ bool_t need_update_runstate_area; struct vcpu_time_info pending_system_time; --- a/xen/include/asm-x86/processor.h +++ b/xen/include/asm-x86/processor.h @@ -462,17 +462,6 @@ long set_gdt(struct vcpu *d,=20 unsigned long *frames,=20 unsigned int entries); =20 -#define write_debugreg(reg, val) do { \ - unsigned long __val =3D val; \ - asm volatile ( "mov %0,%%db" #reg : : "r" (__val) ); \ -} while (0) -#define read_debugreg(reg) ({ \ - unsigned long __val; \ - asm volatile ( "mov %%db" #reg ",%0" : "=3Dr" (__val) ); \ - __val; \ -}) -long set_debugreg(struct vcpu *p, int reg, unsigned long value); - /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */ static always_inline void rep_nop(void) { --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -36,7 +36,7 @@ #include "grant_table.h" #include "hvm/save.h" =20 -#define XEN_DOMCTL_INTERFACE_VERSION 0x00000009 +#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000a =20 /* * NB. xen_domctl.domain is an IN/OUT parameter for this operation. @@ -563,6 +563,16 @@ typedef struct xen_domctl_pin_mem_cachea DEFINE_XEN_GUEST_HANDLE(xen_domctl_pin_mem_cacheattr_t); =20 =20 +#if defined(__i386__) || defined(__x86_64__) +struct xen_domctl_ext_vcpu_msr { + uint32_t index; + uint32_t reserved; + uint64_aligned_t value; +}; +typedef struct xen_domctl_ext_vcpu_msr xen_domctl_ext_vcpu_msr_t; +DEFINE_XEN_GUEST_HANDLE(xen_domctl_ext_vcpu_msr_t); +#endif + /* XEN_DOMCTL_set_ext_vcpucontext */ /* XEN_DOMCTL_get_ext_vcpucontext */ struct xen_domctl_ext_vcpucontext { @@ -582,6 +592,7 @@ struct xen_domctl_ext_vcpucontext { uint16_t sysenter_callback_cs; uint8_t syscall32_disables_events; uint8_t sysenter_disables_events; + uint16_t msr_count; #if defined(__GNUC__) union { uint64_aligned_t mcg_cap; @@ -590,6 +601,7 @@ struct xen_domctl_ext_vcpucontext { #else struct hvm_vmce_vcpu vmce; #endif + XEN_GUEST_HANDLE_64(xen_domctl_ext_vcpu_msr_t) msrs; #endif }; typedef struct xen_domctl_ext_vcpucontext xen_domctl_ext_vcpucontext_t; --=__Part86B4EA6F.3__= Content-Type: text/plain; name="PV-debug-address-mask-MSRs.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="PV-debug-address-mask-MSRs.patch" x86/PV: support data breakpoint extension registers=0A=0AIntroducing an = extension to XEN_DOMCTL_[gs]et_ext_vcpucontext similar=0Ato the generic = MSR save/restore logic recently added for HVM.=0A=0AThis also moves some = debug register related declarations/definition to=0Athe header intended = for these.=0A=0ASigned-off-by: Jan Beulich =0A---=0Av2: = libxc adjustment put in place (depending on the separately posted=0A = http://lists.xenproject.org/archives/html/xen-devel/2014-03/msg03059.html)= =0A=0A--- a/tools/libxc/xc_domain_restore.c=0A+++ b/tools/libxc/xc_domain_r= estore.c=0A@@ -590,8 +590,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+ const xen_domctl_ext_vcpucontext_t *evc;=0A+ } ptr;=0A = =0A /* TODO: handle changing pfntab and vcpu counts */=0A /* PFN = tab */=0A@@ -634,11 +639,36 @@ 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+ = 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+ = break;=0A+ if (alloc) {=0A+ void *nbuf =3D realloc(buf->v= cpubuf, 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@@ -1996,6 +2026,8 @@ int = xc_domain_restore(xc_interface *xch,=0A vcpup =3D tailbuf.u.pv.vcpubuf;= =0A for ( i =3D 0; i <=3D max_vcpu_id; i++ )=0A {=0A+ = DECLARE_HYPERCALL_BUFFER(xen_domctl_ext_vcpu_msr_t, msrs);=0A+=0A = if ( !(vcpumap[i/64] & (1ULL << (i%64))) )=0A continue;=0A = =0A@@ -2130,9 +2162,25 @@ int xc_domain_restore(xc_interface *xch,=0A = goto vcpu_ext_state_restore;=0A memcpy(&domctl.u.ext_vcpucon= text, vcpup, 128);=0A vcpup +=3D 128;=0A+ if ( domctl.u.ext_= vcpucontext.msr_count )=0A+ {=0A+ size_t sz =3D = domctl.u.ext_vcpucontext.msr_count * sizeof(*msrs);=0A+=0A+ = msrs =3D xc_hypercall_buffer_alloc(xch, msrs, sz);=0A+ if ( = !msrs )=0A+ {=0A+ PERROR("No memory for vcpu%d = MSRs", i);=0A+ goto out;=0A+ }=0A+ = memcpy(msrs, vcpup, sz);=0A+ vcpup +=3D sz;=0A+ = set_xen_guest_handle(domctl.u.ext_vcpucontext.msrs, msrs);=0A+ }=0A = domctl.cmd =3D XEN_DOMCTL_set_ext_vcpucontext;=0A = domctl.domain =3D dom;=0A frc =3D xc_domctl(xch, &domctl);=0A+ = if ( msrs )=0A+ xc_hypercall_buffer_free(xch, msrs);=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,36 @@ 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 && domctl.u.ext_v= cpucontext.msr_count )=0A+ {=0A+ msrs =3D xc_hypercall_bu= ffer_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+ }=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_buff= er_free(xch, msrs);=0A+ msrs =3D NULL;=0A+ }=0A =0A = /* Start to fetch CPU eXtended States */=0A /* Get buffer size = first */=0A@@ -2134,6 +2157,8 @@ int xc_domain_save(xc_interface *xch, = in=0A =0A xc_hypercall_buffer_free_pages(xch, to_send, NRPAGES(bitmap_s= ize(dinfo->p2m_size)));=0A xc_hypercall_buffer_free_pages(xch, = to_skip, NRPAGES(bitmap_size(dinfo->p2m_size)));=0A+ if (msrs)=0A+ = xc_hypercall_buffer_free(xch, msrs);=0A =0A free(pfn_type);=0A = free(pfn_batch);=0A--- a/xen/arch/x86/acpi/suspend.c=0A+++ b/xen/arch/x86/a= cpi/suspend.c=0A@@ -9,6 +9,7 @@=0A #include =0A #include = =0A #include =0A+#include =0A = #include =0A #include =0A #include = =0A--- a/xen/arch/x86/domain.c=0A+++ b/xen/arch/x86/doma= in.c=0A@@ -1316,14 +1316,7 @@ static void paravirt_ctxt_switch_to(stru=0A = write_cr4(cr4);=0A =0A if ( unlikely(v->arch.debugreg[7] & = DR7_ACTIVE_MASK) )=0A- {=0A- write_debugreg(0, v->arch.debugreg[0= ]);=0A- write_debugreg(1, v->arch.debugreg[1]);=0A- = write_debugreg(2, v->arch.debugreg[2]);=0A- write_debugreg(3, = v->arch.debugreg[3]);=0A- write_debugreg(6, v->arch.debugreg[6]);=0A= - write_debugreg(7, v->arch.debugreg[7]);=0A- }=0A+ = activate_debugregs(v);=0A =0A if ( (v->domain->arch.tsc_mode =3D=3D = TSC_MODE_PVRDTSCP) &&=0A boot_cpu_has(X86_FEATURE_RDTSCP) )=0A--- = a/xen/arch/x86/domctl.c=0A+++ b/xen/arch/x86/domctl.c=0A@@ -52,6 +52,7 @@ = long arch_do_domctl(=0A {=0A long ret =3D 0;=0A bool_t copyback = =3D 0;=0A+ unsigned long i;=0A =0A switch ( domctl->cmd )=0A = {=0A@@ -319,7 +320,6 @@ long arch_do_domctl(=0A =0A case XEN_DOMCTL_get= memlist:=0A {=0A- int i;=0A unsigned long max_pfns =3D = domctl->u.getmemlist.max_pfns;=0A uint64_t mfn;=0A struct = page_info *page;=0A@@ -645,7 +645,6 @@ long arch_do_domctl(=0A = unsigned long mfn =3D domctl->u.memory_mapping.first_mfn;=0A = unsigned long nr_mfns =3D domctl->u.memory_mapping.nr_mfns;=0A int = add =3D domctl->u.memory_mapping.add_mapping;=0A- unsigned long = i;=0A =0A ret =3D -EINVAL;=0A if ( (mfn + nr_mfns - 1) < = mfn || /* wrap? */=0A@@ -809,6 +808,7 @@ long arch_do_domctl(=0A {=0A = struct xen_domctl_ext_vcpucontext *evc;=0A struct vcpu = *v;=0A+ struct xen_domctl_ext_vcpu_msr msr;=0A =0A evc =3D = &domctl->u.ext_vcpucontext;=0A =0A@@ -854,7 +854,42 @@ long arch_do_domctl(= =0A evc->vmce.mci_ctl2_bank0 =3D v->arch.vmce.bank[0].mci_ctl2;= =0A evc->vmce.mci_ctl2_bank1 =3D v->arch.vmce.bank[1].mci_ctl2;= =0A =0A- ret =3D 0;=0A+ i =3D ret =3D 0;=0A+ = if ( boot_cpu_has(X86_FEATURE_DBEXT) )=0A+ {=0A+ = unsigned int j;=0A+=0A+ if ( v->arch.pv_vcpu.dr_mask[0] = )=0A+ {=0A+ if ( i < evc->msr_count && = !ret )=0A+ {=0A+ msr.index =3D = MSR_AMD64_DR0_ADDRESS_MASK;=0A+ msr.reserved =3D = 0;=0A+ msr.value =3D v->arch.pv_vcpu.dr_mask[0];=0A+= if ( copy_to_guest_offset(evc->msrs, i, &msr, 1) = )=0A+ ret =3D -EFAULT;=0A+ = }=0A+ ++i;=0A+ }=0A+ for = ( j =3D 0; j < 3; ++j )=0A+ {=0A+ if ( = !v->arch.pv_vcpu.dr_mask[1 + j] )=0A+ continue;=0A+ = if ( i < evc->msr_count && !ret )=0A+ = {=0A+ msr.index =3D MSR_AMD64_DR1_ADDRESS_MASK + = j;=0A+ msr.reserved =3D 0;=0A+ = msr.value =3D v->arch.pv_vcpu.dr_mask[1 + j];=0A+ = if ( copy_to_guest_offset(evc->msrs, i, &msr, 1) )=0A+ = ret =3D -EFAULT;=0A+ }=0A+ = ++i;=0A+ }=0A+ }=0A+ if ( i > = evc->msr_count && !ret )=0A+ ret =3D -ENOBUFS;=0A+ = evc->msr_count =3D i;=0A+=0A vcpu_unpause(v);=0A = copyback =3D 1;=0A }=0A@@ -909,9 +944,49 @@ long arch_do_domctl(=0A= =0A ret =3D vmce_restore_vcpu(v, &vmce);=0A = }=0A+ else if ( evc->size > offsetof(typeof(*evc), vmce) )=0A+ = ret =3D -EINVAL;=0A else=0A ret = =3D 0;=0A =0A+ if ( ret || evc->size <=3D offsetof(typeof(*evc),= msrs) )=0A+ /* nothing */;=0A+ else if ( = evc->size < offsetof(typeof(*evc), msrs) +=0A+ = sizeof(evc->msrs) )=0A+ ret =3D -EINVAL;=0A+ = else=0A+ {=0A+ for ( i =3D 0; i < evc->msr_count= ; ++i )=0A+ {=0A+ ret =3D -EFAULT;=0A+ = if ( copy_from_guest_offset(&msr, evc->msrs, i, 1) )=0A+ = break;=0A+ ret =3D -EINVAL;=0A+ = if ( msr.reserved )=0A+ break;=0A+ = switch ( msr.index )=0A+ {=0A+ = case MSR_AMD64_DR0_ADDRESS_MASK:=0A+ = if ( !boot_cpu_has(X86_FEATURE_DBEXT) ||=0A+ = (msr.value >> 32) )=0A+ break;=0A+ = v->arch.pv_vcpu.dr_mask[0] =3D msr.value;=0A+ = continue;=0A+ case MSR_AMD64_DR1_ADDRESS_MASK = ...=0A+ MSR_AMD64_DR3_ADDRESS_MASK:=0A+ = if ( !boot_cpu_has(X86_FEATURE_DBEXT) ||=0A+ = (msr.value >> 32) )=0A+ break;=0A+ = msr.index -=3D MSR_AMD64_DR1_ADDRESS_MASK - 1;=0A+ = v->arch.pv_vcpu.dr_mask[msr.index] =3D msr.value;=0A+ = continue;=0A+ }=0A+ = break;=0A+ }=0A+ if ( i =3D=3D evc->msr_c= ount )=0A+ ret =3D 0;=0A+ }=0A+=0A = domain_unpause(d);=0A }=0A }=0A@@ -921,7 +996,6 @@ long = arch_do_domctl(=0A {=0A xen_domctl_cpuid_t *ctl =3D &domctl->u.= cpuid;=0A cpuid_input_t *cpuid =3D NULL; =0A- int i;=0A =0A = for ( i =3D 0; i < MAX_CPUID_INPUT; i++ )=0A {=0A--- = a/xen/arch/x86/traps.c=0A+++ b/xen/arch/x86/traps.c=0A@@ -2498,6 +2498,23 = @@ static int emulate_privileged_op(struct =0A if ( wrmsr_safe(= regs->ecx, msr_content) !=3D 0 )=0A goto fail;=0A = break;=0A+=0A+ case MSR_AMD64_DR0_ADDRESS_MASK:=0A+ = if ( !boot_cpu_has(X86_FEATURE_DBEXT) || (msr_content >> 32) )=0A+ = goto fail;=0A+ v->arch.pv_vcpu.dr_mask[0] =3D msr_content= ;=0A+ if ( v->arch.debugreg[7] & DR7_ACTIVE_MASK )=0A+ = wrmsrl(MSR_AMD64_DR0_ADDRESS_MASK, msr_content);=0A+ = break;=0A+ case MSR_AMD64_DR1_ADDRESS_MASK ... MSR_AMD64_DR3_ADDRESS= _MASK:=0A+ if ( !boot_cpu_has(X86_FEATURE_DBEXT) || (msr_content= >> 32) )=0A+ goto fail;=0A+ v->arch.pv_vcpu.dr_m= ask=0A+ [regs->_ecx - MSR_AMD64_DR1_ADDRESS_MASK + 1] =3D = msr_content;=0A+ if ( v->arch.debugreg[7] & DR7_ACTIVE_MASK = )=0A+ wrmsrl(regs->_ecx, msr_content);=0A+ = break;=0A+=0A default:=0A if ( wrmsr_hypervisor_regs(re= gs->ecx, msr_content) =3D=3D 1 )=0A break;=0A@@ -2585,6 = +2602,21 @@ static int emulate_privileged_op(struct =0A = regs->eax =3D (uint32_t)msr_content;=0A regs->edx =3D = (uint32_t)(msr_content >> 32);=0A break;=0A+=0A+ case = MSR_AMD64_DR0_ADDRESS_MASK:=0A+ if ( !boot_cpu_has(X86_FEATURE_D= BEXT) )=0A+ goto fail;=0A+ regs->eax =3D = v->arch.pv_vcpu.dr_mask[0];=0A+ regs->edx =3D 0;=0A+ = break;=0A+ case MSR_AMD64_DR1_ADDRESS_MASK ... MSR_AMD64_DR3_ADDRESS= _MASK:=0A+ if ( !boot_cpu_has(X86_FEATURE_DBEXT) )=0A+ = goto fail;=0A+ regs->eax =3D v->arch.pv_vcpu.dr_mask=0A+ = [regs->_ecx - MSR_AMD64_DR1_ADDRESS_MASK + = 1];=0A+ regs->edx =3D 0;=0A+ break;=0A+=0A = default:=0A if ( rdmsr_hypervisor_regs(regs->ecx, &val) )=0A = {=0A@@ -3628,7 +3660,27 @@ long do_set_trap_table(XEN_GUEST_HANDL= E_=0A return rc;=0A }=0A =0A-long set_debugreg(struct vcpu *v, int = reg, unsigned long value)=0A+void activate_debugregs(const struct vcpu = *curr)=0A+{=0A+ ASSERT(curr =3D=3D current);=0A+=0A+ write_debugreg(0= , curr->arch.debugreg[0]);=0A+ write_debugreg(1, curr->arch.debugreg[1])= ;=0A+ write_debugreg(2, curr->arch.debugreg[2]);=0A+ write_debugreg(3= , curr->arch.debugreg[3]);=0A+ write_debugreg(6, curr->arch.debugreg[6])= ;=0A+ write_debugreg(7, curr->arch.debugreg[7]);=0A+=0A+ if ( = boot_cpu_has(X86_FEATURE_DBEXT) )=0A+ {=0A+ wrmsrl(MSR_AMD64_DR0_= ADDRESS_MASK, curr->arch.pv_vcpu.dr_mask[0]);=0A+ wrmsrl(MSR_AMD64_D= R1_ADDRESS_MASK, curr->arch.pv_vcpu.dr_mask[1]);=0A+ wrmsrl(MSR_AMD6= 4_DR2_ADDRESS_MASK, curr->arch.pv_vcpu.dr_mask[2]);=0A+ wrmsrl(MSR_A= MD64_DR3_ADDRESS_MASK, curr->arch.pv_vcpu.dr_mask[3]);=0A+ }=0A+}=0A+=0A= +long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long = value)=0A {=0A int i;=0A struct vcpu *curr =3D current;=0A@@ = -3709,11 +3761,8 @@ long set_debugreg(struct vcpu *v, int re=0A = if ( (v =3D=3D curr) &&=0A !(v->arch.debugreg[7] & = DR7_ACTIVE_MASK) )=0A {=0A- write_debugreg(0, = v->arch.debugreg[0]);=0A- write_debugreg(1, v->arch.debugreg= [1]);=0A- write_debugreg(2, v->arch.debugreg[2]);=0A- = write_debugreg(3, v->arch.debugreg[3]);=0A- = write_debugreg(6, v->arch.debugreg[6]);=0A+ activate_debugre= gs(curr);=0A+ break;=0A }=0A }=0A = if ( v =3D=3D curr )=0A--- a/xen/include/asm-x86/debugreg.h=0A+++ = b/xen/include/asm-x86/debugreg.h=0A@@ -64,4 +64,16 @@=0A #define DR_GLOBAL_= EXACT_ENABLE (0x00000200ul) /* Global exact enable */=0A #define = DR_GENERAL_DETECT (0x00002000ul) /* General detect enable */=0A = =0A+#define write_debugreg(reg, val) do { \=0A+ = unsigned long __val =3D val; \=0A+ asm = volatile ( "mov %0,%%db" #reg : : "r" (__val) ); \=0A+} while (0)=0A+#de= fine read_debugreg(reg) ({ \=0A+ unsigned = long __val; \=0A+ asm volatile ( = "mov %%db" #reg ",%0" : "=3Dr" (__val) ); \=0A+ __val; = \=0A+})=0A+long set_debugreg(struct vcpu = *, unsigned int reg, unsigned long value);=0A+void activate_debugregs(const= struct vcpu *);=0A+=0A #endif /* _X86_DEBUGREG_H */=0A--- a/xen/include/as= m-x86/domain.h=0A+++ b/xen/include/asm-x86/domain.h=0A@@ -374,6 +374,9 @@ = struct pv_vcpu=0A unsigned long shadow_ldt_mapcnt;=0A spinlock_t = shadow_ldt_lock;=0A =0A+ /* data breakpoint extension MSRs */=0A+ = uint32_t dr_mask[4];=0A+=0A /* Deferred VA-based update state. */=0A = bool_t need_update_runstate_area;=0A struct vcpu_time_info pending_sy= stem_time;=0A--- a/xen/include/asm-x86/processor.h=0A+++ b/xen/include/asm-= x86/processor.h=0A@@ -462,17 +462,6 @@ long set_gdt(struct vcpu *d, =0A = unsigned long *frames, =0A unsigned int entries);=0A= =0A-#define write_debugreg(reg, val) do { \=0A- = unsigned long __val =3D val; \=0A- asm = volatile ( "mov %0,%%db" #reg : : "r" (__val) ); \=0A-} while (0)=0A-#de= fine read_debugreg(reg) ({ \=0A- unsigned = long __val; \=0A- asm volatile ( = "mov %%db" #reg ",%0" : "=3Dr" (__val) ); \=0A- __val; = \=0A-})=0A-long set_debugreg(struct vcpu = *p, int reg, unsigned long value);=0A-=0A /* REP NOP (PAUSE) is a good = thing to insert into busy-wait loops. */=0A static always_inline void = rep_nop(void)=0A {=0A--- a/xen/include/public/domctl.h=0A+++ b/xen/include/= public/domctl.h=0A@@ -36,7 +36,7 @@=0A #include "grant_table.h"=0A = #include "hvm/save.h"=0A =0A-#define XEN_DOMCTL_INTERFACE_VERSION = 0x00000009=0A+#define XEN_DOMCTL_INTERFACE_VERSION 0x0000000a=0A =0A /*=0A = * NB. xen_domctl.domain is an IN/OUT parameter for this operation.=0A@@ = -563,6 +563,16 @@ typedef struct xen_domctl_pin_mem_cachea=0A DEFINE_XEN_GU= EST_HANDLE(xen_domctl_pin_mem_cacheattr_t);=0A =0A =0A+#if defined(__i386__= ) || defined(__x86_64__)=0A+struct xen_domctl_ext_vcpu_msr {=0A+ = uint32_t index;=0A+ uint32_t reserved;=0A+ uint64_ali= gned_t value;=0A+};=0A+typedef struct xen_domctl_ext_vcpu_msr xen_domctl_ex= t_vcpu_msr_t;=0A+DEFINE_XEN_GUEST_HANDLE(xen_domctl_ext_vcpu_msr_t);=0A+#en= dif=0A+=0A /* XEN_DOMCTL_set_ext_vcpucontext */=0A /* XEN_DOMCTL_get_ext_vc= pucontext */=0A struct xen_domctl_ext_vcpucontext {=0A@@ -582,6 +592,7 @@ = struct xen_domctl_ext_vcpucontext {=0A uint16_t sysenter_callba= ck_cs;=0A uint8_t syscall32_disables_events;=0A uint8_t = sysenter_disables_events;=0A+ uint16_t msr_count;=0A #if = defined(__GNUC__)=0A union {=0A uint64_aligned_t mcg_cap;=0A@@ = -590,6 +601,7 @@ struct xen_domctl_ext_vcpucontext {=0A #else=0A = struct hvm_vmce_vcpu vmce;=0A #endif=0A+ XEN_GUEST_HANDLE_64(xen_domctl_= ext_vcpu_msr_t) msrs;=0A #endif=0A };=0A typedef struct xen_domctl_ext_vcpu= context xen_domctl_ext_vcpucontext_t;=0A --=__Part86B4EA6F.3__= 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 --=__Part86B4EA6F.3__=--