xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: "Jan Beulich" <JBeulich@suse.com>
To: xen-devel <xen-devel@lists.xenproject.org>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>, Keir Fraser <keir@xen.org>
Subject: [PATCH] x86emul: special case far branch validation outside of long mode
Date: Fri, 11 Mar 2016 09:01:21 -0700	[thread overview]
Message-ID: <56E2F9E102000078000DBAD6@prv-mh.provo.novell.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 3475 bytes --]

In that case (with the new value being held in, or now in one case cast
to, a 32-bit variable) there's no need to go through the long mode part
of the checks.

Primarily this was meant to hopefully address Coverity ID 1355278, but
since the change produces smaller code as well I think we should use it
even if it doesn't help that (benign) warning.

Also it's more in line with jmp_rel() for commit_far_branch() to do the
_regs.eip update, so adjust that at once.

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

--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -652,13 +652,20 @@ do {
     _regs.eip = ip;                                                     \
 } while (0)
 
-#define validate_far_branch(cs, ip)                                     \
-    generate_exception_if(in_longmode(ctxt, ops) && (cs)->attr.fields.l \
-                          ? !is_canonical_address(ip)                   \
-                          : (ip) > (cs)->limit, EXC_GP, 0)
+#define validate_far_branch(cs, ip) ({                                  \
+    if ( sizeof(ip) <= 4 ) {                                            \
+        ASSERT(in_longmode(ctxt, ops) <= 0);                            \
+        generate_exception_if((ip) > (cs)->limit, EXC_GP, 0);           \
+    } else                                                              \
+        generate_exception_if(in_longmode(ctxt, ops) &&                 \
+                              (cs)->attr.fields.l                       \
+                              ? !is_canonical_address(ip)               \
+                              : (ip) > (cs)->limit, EXC_GP, 0);         \
+})
 
 #define commit_far_branch(cs, ip) ({                                    \
     validate_far_branch(cs, ip);                                        \
+    _regs.eip = (ip);                                                   \
     ops->write_segment(x86_seg_cs, cs, ctxt);                           \
 })
 
@@ -2787,7 +2794,6 @@ x86_emulate(
              (rc = load_seg(x86_seg_cs, src.val, 1, &cs, ctxt, ops)) ||
              (rc = commit_far_branch(&cs, dst.val)) )
             goto done;
-        _regs.eip = dst.val;
         break;
     }
 
@@ -2830,9 +2836,8 @@ x86_emulate(
         eflags &= 0x257fd5;
         _regs.eflags &= mask;
         _regs.eflags |= (eflags & ~mask) | 0x02;
-        _regs.eip = eip;
         if ( (rc = load_seg(x86_seg_cs, sel, 1, &cs, ctxt, ops)) ||
-             (rc = commit_far_branch(&cs, eip)) )
+             (rc = commit_far_branch(&cs, (uint32_t)eip)) )
             goto done;
         break;
     }
@@ -3465,7 +3470,6 @@ x86_emulate(
         if ( (rc = load_seg(x86_seg_cs, sel, 0, &cs, ctxt, ops)) ||
              (rc = commit_far_branch(&cs, eip)) )
             goto done;
-        _regs.eip = eip;
         break;
     }
 
@@ -3767,11 +3771,11 @@ x86_emulate(
                                       &_regs.eip, op_bytes, ctxt)) ||
                      (rc = ops->write_segment(x86_seg_cs, &cs, ctxt)) )
                     goto done;
+                _regs.eip = src.val;
             }
             else if ( (rc = load_seg(x86_seg_cs, sel, 0, &cs, ctxt, ops)) ||
                       (rc = commit_far_branch(&cs, src.val)) )
                 goto done;
-            _regs.eip = src.val;
 
             dst.type = OP_NONE;
             break;




[-- Attachment #2: x86emul-coverity-warning.patch --]
[-- Type: text/plain, Size: 3537 bytes --]

x86emul: special case far branch validation outside of long mode

In that case (with the new value being held in, or now in one case cast
to, a 32-bit variable) there's no need to go through the long mode part
of the checks.

Primarily this was meant to hopefully address Coverity ID 1355278, but
since the change produces smaller code as well I think we should use it
even if it doesn't help that (benign) warning.

Also it's more in line with jmp_rel() for commit_far_branch() to do the
_regs.eip update, so adjust that at once.

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

--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -652,13 +652,20 @@ do {
     _regs.eip = ip;                                                     \
 } while (0)
 
-#define validate_far_branch(cs, ip)                                     \
-    generate_exception_if(in_longmode(ctxt, ops) && (cs)->attr.fields.l \
-                          ? !is_canonical_address(ip)                   \
-                          : (ip) > (cs)->limit, EXC_GP, 0)
+#define validate_far_branch(cs, ip) ({                                  \
+    if ( sizeof(ip) <= 4 ) {                                            \
+        ASSERT(in_longmode(ctxt, ops) <= 0);                            \
+        generate_exception_if((ip) > (cs)->limit, EXC_GP, 0);           \
+    } else                                                              \
+        generate_exception_if(in_longmode(ctxt, ops) &&                 \
+                              (cs)->attr.fields.l                       \
+                              ? !is_canonical_address(ip)               \
+                              : (ip) > (cs)->limit, EXC_GP, 0);         \
+})
 
 #define commit_far_branch(cs, ip) ({                                    \
     validate_far_branch(cs, ip);                                        \
+    _regs.eip = (ip);                                                   \
     ops->write_segment(x86_seg_cs, cs, ctxt);                           \
 })
 
@@ -2787,7 +2794,6 @@ x86_emulate(
              (rc = load_seg(x86_seg_cs, src.val, 1, &cs, ctxt, ops)) ||
              (rc = commit_far_branch(&cs, dst.val)) )
             goto done;
-        _regs.eip = dst.val;
         break;
     }
 
@@ -2830,9 +2836,8 @@ x86_emulate(
         eflags &= 0x257fd5;
         _regs.eflags &= mask;
         _regs.eflags |= (eflags & ~mask) | 0x02;
-        _regs.eip = eip;
         if ( (rc = load_seg(x86_seg_cs, sel, 1, &cs, ctxt, ops)) ||
-             (rc = commit_far_branch(&cs, eip)) )
+             (rc = commit_far_branch(&cs, (uint32_t)eip)) )
             goto done;
         break;
     }
@@ -3465,7 +3470,6 @@ x86_emulate(
         if ( (rc = load_seg(x86_seg_cs, sel, 0, &cs, ctxt, ops)) ||
              (rc = commit_far_branch(&cs, eip)) )
             goto done;
-        _regs.eip = eip;
         break;
     }
 
@@ -3767,11 +3771,11 @@ x86_emulate(
                                       &_regs.eip, op_bytes, ctxt)) ||
                      (rc = ops->write_segment(x86_seg_cs, &cs, ctxt)) )
                     goto done;
+                _regs.eip = src.val;
             }
             else if ( (rc = load_seg(x86_seg_cs, sel, 0, &cs, ctxt, ops)) ||
                       (rc = commit_far_branch(&cs, src.val)) )
                 goto done;
-            _regs.eip = src.val;
 
             dst.type = OP_NONE;
             break;

[-- Attachment #3: Type: text/plain, Size: 126 bytes --]

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

             reply	other threads:[~2016-03-11 16:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-11 16:01 Jan Beulich [this message]
2016-04-21  8:36 ` [PATCH] x86emul: special case far branch validation outside of long mode Andrew Cooper
2016-04-21  8:55   ` Jan Beulich
2016-04-22 10:07     ` Wei Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56E2F9E102000078000DBAD6@prv-mh.provo.novell.com \
    --to=jbeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=keir@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).