From: Andrew Cooper Subject: Re: [PATCH] x86: fold code in load_segments() Date: Wed, 14 Sep 2016 18:12:01 +0100 Message-ID: <90d1fbec-8292-c1a2-ac5f-6527acb19e55@citrix.com> References: <57D987E6020000780010EE8F@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============8740676082404559673==" Return-path: Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bkDjC-0007lW-SS for xen-devel@lists.xenproject.org; Wed, 14 Sep 2016 17:12:06 +0000 In-Reply-To: <57D987E6020000780010EE8F@prv-mh.provo.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: Jan Beulich , xen-devel List-Id: xen-devel@lists.xenproject.org --===============8740676082404559673== Content-Type: multipart/alternative; boundary="------------88C64A6AB3E427A3F57F2166" --------------88C64A6AB3E427A3F57F2166 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit On 14/09/16 16:24, Jan Beulich wrote: > No need to have the same logic twice. > > Signed-off-by: Jan Beulich > > --- a/xen/arch/x86/domain.c > +++ b/xen/arch/x86/domain.c > @@ -1745,22 +1745,22 @@ static void load_segments(struct vcpu *n > (unsigned long *)pv->kernel_sp; > unsigned long cs_and_mask, rflags; > > + /* Fold upcall mask and architectural IOPL into RFLAGS.IF. */ > + rflags = regs->rflags & ~(X86_EFLAGS_IF|X86_EFLAGS_IOPL); > + rflags |= !vcpu_info(n, evtchn_upcall_mask) << 9; > + if ( VM_ASSIST(n->domain, architectural_iopl) ) > + rflags |= n->arch.pv_vcpu.iopl; > + > if ( is_pv_32bit_vcpu(n) ) > { > unsigned int *esp = ring_1(regs) ? > (unsigned int *)regs->rsp : > (unsigned int *)pv->kernel_sp; > - unsigned int cs_and_mask, eflags; The unshadowed cs_and_mask is unsigned long, not int, which means the put_user() below will clobber a 32bit PV guests stack frame. Other than that, Reviewed-by: Andrew Cooper for the intended change. ~Andrew > int ret = 0; > > /* CS longword also contains full evtchn_upcall_mask. */ > cs_and_mask = (unsigned short)regs->cs | > ((unsigned int)vcpu_info(n, evtchn_upcall_mask) << 16); > - /* Fold upcall mask into RFLAGS.IF. */ > - eflags = regs->_eflags & ~(X86_EFLAGS_IF|X86_EFLAGS_IOPL); > - eflags |= !vcpu_info(n, evtchn_upcall_mask) << 9; > - if ( VM_ASSIST(n->domain, architectural_iopl) ) > - eflags |= n->arch.pv_vcpu.iopl; > > if ( !ring_1(regs) ) > { > @@ -1770,7 +1770,7 @@ static void load_segments(struct vcpu *n > } > > if ( ret | > - put_user(eflags, esp-1) | > + put_user(rflags, esp-1) | > put_user(cs_and_mask, esp-2) | > put_user(regs->_eip, esp-3) | > put_user(uregs->gs, esp-4) | > @@ -1805,12 +1805,6 @@ static void load_segments(struct vcpu *n > cs_and_mask = (unsigned long)regs->cs | > ((unsigned long)vcpu_info(n, evtchn_upcall_mask) << 32); > > - /* Fold upcall mask into RFLAGS.IF. */ > - rflags = regs->rflags & ~(X86_EFLAGS_IF|X86_EFLAGS_IOPL); > - rflags |= !vcpu_info(n, evtchn_upcall_mask) << 9; > - if ( VM_ASSIST(n->domain, architectural_iopl) ) > - rflags |= n->arch.pv_vcpu.iopl; > - > if ( put_user(regs->ss, rsp- 1) | > put_user(regs->rsp, rsp- 2) | > put_user(rflags, rsp- 3) | > > > > > > _______________________________________________ > Xen-devel mailing list > Xen-devel@lists.xen.org > https://lists.xen.org/xen-devel --------------88C64A6AB3E427A3F57F2166 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 7bit
On 14/09/16 16:24, Jan Beulich wrote:
No need to have the same logic twice.

Signed-off-by: Jan Beulich <jbeulich@suse.com>

--- a/xen/arch/x86/domain.c
+++ b/xen/arch/x86/domain.c
@@ -1745,22 +1745,22 @@ static void load_segments(struct vcpu *n
             (unsigned long *)pv->kernel_sp;
         unsigned long cs_and_mask, rflags;
 
+        /* Fold upcall mask and architectural IOPL into RFLAGS.IF. */
+        rflags  = regs->rflags & ~(X86_EFLAGS_IF|X86_EFLAGS_IOPL);
+        rflags |= !vcpu_info(n, evtchn_upcall_mask) << 9;
+        if ( VM_ASSIST(n->domain, architectural_iopl) )
+            rflags |= n->arch.pv_vcpu.iopl;
+
         if ( is_pv_32bit_vcpu(n) )
         {
             unsigned int *esp = ring_1(regs) ?
                                 (unsigned int *)regs->rsp :
                                 (unsigned int *)pv->kernel_sp;
-            unsigned int cs_and_mask, eflags;

The unshadowed cs_and_mask is unsigned long, not int, which means the put_user() below will clobber a 32bit PV guests stack frame.

Other than that, Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com> for the intended change.

~Andrew

             int ret = 0;
 
             /* CS longword also contains full evtchn_upcall_mask. */
             cs_and_mask = (unsigned short)regs->cs |
                 ((unsigned int)vcpu_info(n, evtchn_upcall_mask) << 16);
-            /* Fold upcall mask into RFLAGS.IF. */
-            eflags  = regs->_eflags & ~(X86_EFLAGS_IF|X86_EFLAGS_IOPL);
-            eflags |= !vcpu_info(n, evtchn_upcall_mask) << 9;
-            if ( VM_ASSIST(n->domain, architectural_iopl) )
-                eflags |= n->arch.pv_vcpu.iopl;
 
             if ( !ring_1(regs) )
             {
@@ -1770,7 +1770,7 @@ static void load_segments(struct vcpu *n
             }
 
             if ( ret |
-                 put_user(eflags,              esp-1) |
+                 put_user(rflags,              esp-1) |
                  put_user(cs_and_mask,         esp-2) |
                  put_user(regs->_eip,          esp-3) |
                  put_user(uregs->gs,           esp-4) |
@@ -1805,12 +1805,6 @@ static void load_segments(struct vcpu *n
         cs_and_mask = (unsigned long)regs->cs |
             ((unsigned long)vcpu_info(n, evtchn_upcall_mask) << 32);
 
-        /* Fold upcall mask into RFLAGS.IF. */
-        rflags  = regs->rflags & ~(X86_EFLAGS_IF|X86_EFLAGS_IOPL);
-        rflags |= !vcpu_info(n, evtchn_upcall_mask) << 9;
-        if ( VM_ASSIST(n->domain, architectural_iopl) )
-            rflags |= n->arch.pv_vcpu.iopl;
-
         if ( put_user(regs->ss,            rsp- 1) |
              put_user(regs->rsp,           rsp- 2) |
              put_user(rflags,              rsp- 3) |





_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

--------------88C64A6AB3E427A3F57F2166-- --===============8740676082404559673== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --===============8740676082404559673==--