Skip to contentSkip to conversation

x86/emul: Adjustments to exception error code handling

PATCH In review Andrew Cooper 2 participants 4 messages Feb 5, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/emul: Adjustments to exception error code handling

The mkec() end result in c/s bac133d43 wasn't really what I intended, and unfortunately hides programmer errors if passing an error code with an exception which doesn't take one.

Rework mkec() completely to simply insert X86_EVENT_NO_EC if no error code parameter was provided. It still is the programmers responsibility to get error codes correct, but an error will now trip an ASSERT() later during event injection. All current generate_exception_if() users appear to be correct.

There is a minor improvement:

add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-301 (-301)
function old new delta
protmode_load_seg 1460 1442 -18
x86_emulate 103975 103692 -283

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +3 -5
index f22f821..0b472b0 100644
@@ -877,14 +877,12 @@ do {                                                    \
     if ( rc ) goto done;                                \
 } while (0)
 
-static inline int mkec(uint8_t e, int32_t ec, ...)
-{
-    return (e < 32 && ((1u << e) & EXC_HAS_EC)) ? ec : X86_EVENT_NO_EC;
-}
+/* CPP magic.  Chooses ec if not empty, otherwise X86_EVENT_NO_EC. */
+#define mkec(ignore, x, ...) x
 
 #define generate_exception_if(p, e, ec...)                                \
 ({  if ( (p) ) {                                                          \
-        x86_emul_hw_exception(e, mkec(e, ##ec, 0), ctxt);                 \
+        x86_emul_hw_exception(e, mkec(X, ##ec, X86_EVENT_NO_EC), ctxt);   \
         rc = X86EMUL_EXCEPTION;                                           \
         goto done;                                                        \
     }                                                                     \
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 05.02.18 at 11:59, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +0 -4
@@ -877,14 +877,12 @@ do {                                                    \
     if ( rc ) goto done;                                \
 } while (0)
 
-static inline int mkec(uint8_t e, int32_t ec, ...)
-{
-    return (e < 32 && ((1u << e) & EXC_HAS_EC)) ? ec : X86_EVENT_NO_EC;
-}

xen/arch/x86/x86_emulate/x86_emulate.c do { \ · line 888

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +3 -1
+/* CPP magic.  Chooses ec if not empty, otherwise X86_EVENT_NO_EC. */
+#define mkec(ignore, x, ...) x
 
 #define generate_exception_if(p, e, ec...)                                \
 ({  if ( (p) ) {                                                          \
-        x86_emul_hw_exception(e, mkec(e, ##ec, 0), ctxt);                 \
+        x86_emul_hw_exception(e, mkec(X, ##ec, X86_EVENT_NO_EC), ctxt);   \
         rc = X86EMUL_EXCEPTION;                                           \
         goto done;                                                        \
     }                                                                     \

This orphans EXC_HAS_EC, which makes me wonder what assertion you're talking about in the description. The way things are before your change means that at least an exception with error code will be delivered properly (the error code will be zero then) if it wasn't specified in the invocation (which, as you may recall, I actually consider useful, but you did object to making this an "officially" allowed mechanism). With your change in place, an assertion will supposedly trigger (wherever that is), killing the host or (in a release build) leading to some other behavior that's likely fatal to a guest. Would the guest perhaps get to see an error code of all ones? If, otoh, we could know at build time that something is wrong, I would be quite a bit more in agreement with doing such a change, most importantly because those exception raising paths are rarely hit, and are mostly (if not entirely) untested by the test harness.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 05.02.18 at 11:59, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +2 -4
@@ -877,14 +877,12 @@ do {                                                    \
     if ( rc ) goto done;                                \
 } while (0)
 
-static inline int mkec(uint8_t e, int32_t ec, ...)
-{
-    return (e < 32 && ((1u << e) & EXC_HAS_EC)) ? ec : X86_EVENT_NO_EC;
-}
+/* CPP magic.  Chooses ec if not empty, otherwise X86_EVENT_NO_EC. */
+#define mkec(ignore, x, ...) x
 
Quoted 9 lines

xen/arch/x86/x86_emulate/x86_emulate.c do { \ · line 888

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
 #define generate_exception_if(p, e, ec...)                                \
 ({  if ( (p) ) {                                                          \
-        x86_emul_hw_exception(e, mkec(e, ##ec, 0), ctxt);                 \
+        x86_emul_hw_exception(e, mkec(X, ##ec, X86_EVENT_NO_EC), ctxt);   \
         rc = X86EMUL_EXCEPTION;                                           \
         goto done;                                                        \
     }                                                                     \

This orphans EXC_HAS_EC, which makes me wonder what assertion you're talking about in the description.

{pv,hvm}_inject_event()

Quoted 6 lines

The way things are before
your change means that at least an exception with error code will be delivered properly (the error code will be zero then) if it wasn't specified in the invocation (which, as you may recall, I actually consider useful, but you did object to making this an "officially" allowed mechanism).

It also meant that programming errors go completely unnoticed, which is worse.

Quoted 5 lines

With your change in place, an assertion will
supposedly trigger (wherever that is), killing the host or (in a release build) leading to some other behavior that's likely fatal to a guest. Would the guest perhaps get to see an error code of all ones?

In a release builds, it depends how vicious the vmentry checks are.

Quoted 4 lines

If, otoh, we could know at build time that something is wrong,
I would be quite a bit more in agreement with doing such a change, most importantly because those exception raising paths are rarely hit, and are mostly (if not entirely) untested by the test harness.

I was originally aiming for a build time check, but the check_fpu_exn() and protmode_load_seg() paths at least have non-constant exceptions.

We could force a constant exception by BUILD_BUG_ON(e >= 32), and opencode the result of check_fpu_exn() (which is the only case which can't be converted to a constant exception) to use x86_emul_hw_exception() directly with suitable auditing.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 05.02.18 at 17:00, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 05.02.18 at 11:59, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +2 -4
@@ -877,14 +877,12 @@ do {                                                    \
     if ( rc ) goto done;                                \
 } while (0)
 
-static inline int mkec(uint8_t e, int32_t ec, ...)
-{
-    return (e < 32 && ((1u << e) & EXC_HAS_EC)) ? ec : X86_EVENT_NO_EC;
-}
+/* CPP magic.  Chooses ec if not empty, otherwise X86_EVENT_NO_EC. */
+#define mkec(ignore, x, ...) x
 
Quoted 9 lines

xen/arch/x86/x86_emulate/x86_emulate.c do { \ · line 888

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
 #define generate_exception_if(p, e, ec...)                                \
 ({  if ( (p) ) {                                                          \
-        x86_emul_hw_exception(e, mkec(e, ##ec, 0), ctxt);                 \
+        x86_emul_hw_exception(e, mkec(X, ##ec, X86_EVENT_NO_EC), ctxt);   \
         rc = X86EMUL_EXCEPTION;                                           \
         goto done;                                                        \
     }                                                                     \

This orphans EXC_HAS_EC, which makes me wonder what assertion you're talking about in the description.

Quoted 1 line

{pv,hvm}_inject_event()

Which means that ...

Quoted 10 lines
Quoted 6 lines

The way things are before
your change means that at least an exception with error code will be delivered properly (the error code will be zero then) if it wasn't specified in the invocation (which, as you may recall, I actually consider useful, but you did object to making this an "officially" allowed mechanism).

It also meant that programming errors go completely unnoticed, which is worse.

Quoted 7 lines
Quoted 5 lines

With your change in place, an assertion will
supposedly trigger (wherever that is), killing the host or (in a release build) leading to some other behavior that's likely fatal to a guest. Would the guest perhaps get to see an error code of all ones?

In a release builds, it depends how vicious the vmentry checks are.

... covers only half of it - there are no such checks at all for PV.

Quoted 5 lines
Quoted 4 lines

If, otoh, we could know at build time that something is wrong,
I would be quite a bit more in agreement with doing such a change, most importantly because those exception raising paths are rarely hit, and are mostly (if not entirely) untested by the test harness.

Quoted 7 lines

I was originally aiming for a build time check, but the check_fpu_exn() and protmode_load_seg() paths at least have non-constant exceptions.

We could force a constant exception by BUILD_BUG_ON(e >= 32), and opencode the result of check_fpu_exn() (which is the only case which can't be converted to a constant exception) to use x86_emul_hw_exception() directly with suitable auditing.

I'd prefer to avoid such open coding. Would the combination of __builtin_constant_p() and a reference to a link-time undefined symbol not do the job?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel