Skip to contentSkip to conversation

Improvements to struct semgent_register

PATCH Superseded Andrew Cooper 2 participants 19 messages Jun 30, 2017 – Jul 3, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Improvements to struct semgent_register

No functional change, but the source code is less verbose.

Andrew Cooper (3):
x86/emul: Introduce build time assertions for struct segment_register x86/hvm: Rearange check_segment() to use a switch statement x86/emul: Drop segment_attributes_t

 tools/fuzz/x86_instruction_emulator/fuzz-emul.c |  10 +-
 tools/tests/x86_emulator/test_x86_emulator.c    |   2 +-
 xen/arch/x86/cpu/vpmu.c                         |   2 +-
 xen/arch/x86/hvm/domain.c                       |  78 ++++++------
 xen/arch/x86/hvm/emulate.c                      |  20 +--
 xen/arch/x86/hvm/hvm.c                          | 154 ++++++++++++------------
 xen/arch/x86/hvm/svm/svm.c                      |  10 +-
 xen/arch/x86/hvm/svm/svmdebug.c                 |   4 +-
 xen/arch/x86/hvm/svm/vmcb.c                     |  16 +--
 xen/arch/x86/hvm/vmx/realmode.c                 |  10 +-
 xen/arch/x86/hvm/vmx/vmx.c                      |  41 +++----
 xen/arch/x86/mm/shadow/common.c                 |   6 +-
 xen/arch/x86/pv/emul-priv-op.c                  |  40 +++---
 xen/arch/x86/vm_event.c                         |   2 +-
 xen/arch/x86/x86_emulate/x86_emulate.c          |  61 +++++-----
 xen/arch/x86/x86_emulate/x86_emulate.h          |  37 +++---
 16 files changed, 248 insertions(+), 245 deletions(-)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH 1/3 x86/emul: Introduce build time assertions for struct segment_register

This structure is shared with hardware in the AMD VMCB.

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

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 6 ++++++
 1 file changed, 6 insertions(+)
xen/arch/x86/x86_emulate/x86_emulate.c +6 -0
index 87e84c6..5805d70 100644
@@ -7899,6 +7899,12 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(X86_EVENTTYPE_SW_INTERRUPT != 4);
     BUILD_BUG_ON(X86_EVENTTYPE_PRI_SW_EXCEPTION != 5);
     BUILD_BUG_ON(X86_EVENTTYPE_SW_EXCEPTION != 6);
+
+    /* Check struct segment_register against the VMCB segment layout. */
+    BUILD_BUG_ON(sizeof(struct segment_register) != 16);
+    BUILD_BUG_ON(offsetof(struct segment_register, attr) != 2);
+    BUILD_BUG_ON(offsetof(struct segment_register, limit) != 4);
+    BUILD_BUG_ON(offsetof(struct segment_register, base) != 8);
 }
 
 #ifndef NDEBUG
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#2 lore raw

Re: [PATCH 1/3] x86/emul: Introduce build time assertions for struct segment_register

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:

This structure is shared with hardware in the AMD VMCB.

Indeed, but do we really depend on that in the emulator code?

xen/arch/x86/x86_emulate/x86_emulate.c static void __init __maybe_unused build_assertions(void) · line 7905

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -0
@@ -7899,6 +7899,12 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(X86_EVENTTYPE_SW_INTERRUPT != 4);
     BUILD_BUG_ON(X86_EVENTTYPE_PRI_SW_EXCEPTION != 5);
     BUILD_BUG_ON(X86_EVENTTYPE_SW_EXCEPTION != 6);
+
+    /* Check struct segment_register against the VMCB segment layout. */
+    BUILD_BUG_ON(sizeof(struct segment_register) != 16);
+    BUILD_BUG_ON(offsetof(struct segment_register, attr) != 2);

I.e. for these two I don't think I can see any dependency, and ...

xen/arch/x86/x86_emulate/x86_emulate.c static void __init __maybe_unused build_assertions(void) · line 7907

Quoted 2 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
+    BUILD_BUG_ON(offsetof(struct segment_register, limit) != 4);
+    BUILD_BUG_ON(offsetof(struct segment_register, base) != 8);

... for these two I think all we require is >=. Otoh, if these were put in SVM code, then I could see the point of them being the way they are. I'd then even raise the question whether we wouldn't also want offsetof() checks.

Jan

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

#3 lore raw

Re: [PATCH 1/3] x86/emul: Introduce build time assertions for struct segment_register

Quoted 16 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:

This structure is shared with hardware in the AMD VMCB.

Indeed, but do we really depend on that in the emulator code?

xen/arch/x86/x86_emulate/x86_emulate.c static void __init __maybe_unused build_assertions(void) · line 7905

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -0
@@ -7899,6 +7899,12 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(X86_EVENTTYPE_SW_INTERRUPT != 4);
     BUILD_BUG_ON(X86_EVENTTYPE_PRI_SW_EXCEPTION != 5);
     BUILD_BUG_ON(X86_EVENTTYPE_SW_EXCEPTION != 6);
+
+    /* Check struct segment_register against the VMCB segment layout. */
+    BUILD_BUG_ON(sizeof(struct segment_register) != 16);
+    BUILD_BUG_ON(offsetof(struct segment_register, attr) != 2);

I.e. for these two I don't think I can see any dependency, and ...

Quoted 5 lines

xen/arch/x86/x86_emulate/x86_emulate.c static void __init __maybe_unused build_assertions(void) · line 7907

Quoted 2 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
+    BUILD_BUG_ON(offsetof(struct segment_register, limit) != 4);
+    BUILD_BUG_ON(offsetof(struct segment_register, base) != 8);

... for these two I think all we require is >=. Otoh, if these were put in SVM code, then I could see the point of them being the way they are.

These are indeed only for the SVM code. See the impending 4/3 patch which I'm about to post.

Quoted 2 lines

I'd then even raise the question whether we wouldn't also want
offsetof() checks.

I don't understand; these are offsetof checks.

~Andrew

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

#4 lore raw

Re: [PATCH 1/3] x86/emul: Introduce build time assertions for struct segment_register

Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:05, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:

This structure is shared with hardware in the AMD VMCB.

Indeed, but do we really depend on that in the emulator code?

xen/arch/x86/x86_emulate/x86_emulate.c static void __init __maybe_unused build_assertions(void) · line 7905

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -0
@@ -7899,6 +7899,12 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(X86_EVENTTYPE_SW_INTERRUPT != 4);
     BUILD_BUG_ON(X86_EVENTTYPE_PRI_SW_EXCEPTION != 5);
     BUILD_BUG_ON(X86_EVENTTYPE_SW_EXCEPTION != 6);
+
+    /* Check struct segment_register against the VMCB segment layout. */
+    BUILD_BUG_ON(sizeof(struct segment_register) != 16);
+    BUILD_BUG_ON(offsetof(struct segment_register, attr) != 2);

I.e. for these two I don't think I can see any dependency, and ...

Quoted 8 lines
Quoted 5 lines

xen/arch/x86/x86_emulate/x86_emulate.c static void __init __maybe_unused build_assertions(void) · line 7907

Quoted 2 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
+    BUILD_BUG_ON(offsetof(struct segment_register, limit) != 4);
+    BUILD_BUG_ON(offsetof(struct segment_register, base) != 8);

... for these two I think all we require is >=. Otoh, if these were put in SVM code, then I could see the point of them being the way they are.

These are indeed only for the SVM code. See the impending 4/3 patch which I'm about to post.

Ah, I see. But then I'm still unconvinced we need them here as well. I.e. I'd rather see that new patch to replace the one here.

Quoted 4 lines
Quoted 2 lines

I'd then even raise the question whether we wouldn't also want
offsetof() checks.

I don't understand; these are offsetof checks.

Oops, I must have inferred sizeof() from the first one, and with the literal numbers being applicable to both this didn't cause any inconsistency. In any event - I think we should have both sizeof() and offsetof() ones.

Jan

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

#5 lore raw

Re: [PATCH 1/3] x86/emul: Introduce build time assertions for struct segment_register

Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:05, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:

This structure is shared with hardware in the AMD VMCB.

Indeed, but do we really depend on that in the emulator code?

xen/arch/x86/x86_emulate/x86_emulate.c static void __init __maybe_unused build_assertions(void) · line 7905

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -0
@@ -7899,6 +7899,12 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(X86_EVENTTYPE_SW_INTERRUPT != 4);
     BUILD_BUG_ON(X86_EVENTTYPE_PRI_SW_EXCEPTION != 5);
     BUILD_BUG_ON(X86_EVENTTYPE_SW_EXCEPTION != 6);
+
+    /* Check struct segment_register against the VMCB segment layout. */
+    BUILD_BUG_ON(sizeof(struct segment_register) != 16);
+    BUILD_BUG_ON(offsetof(struct segment_register, attr) != 2);

I.e. for these two I don't think I can see any dependency, and ...

Quoted 2 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
+    BUILD_BUG_ON(offsetof(struct segment_register, limit) != 4);
+    BUILD_BUG_ON(offsetof(struct segment_register, base) != 8);
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

... for these two I think all we require is >=. Otoh, if these were put in SVM code, then I could see the point of them being the way they are.

These are indeed only for the SVM code. See the impending 4/3 patch which I'm about to post.

Ah, I see. But then I'm still unconvinced we need them here as well. I.e. I'd rather see that new patch to replace the one here.

I should have been clearer. I intend the 4/3 patch to replace this patch entirely in the series.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

I'd then even raise the question whether we wouldn't also want
offsetof() checks.

I don't understand; these are offsetof checks.

Oops, I must have inferred sizeof() from the first one, and with the literal numbers being applicable to both this didn't cause any inconsistency. In any event - I think we should have both sizeof() and offsetof() ones.

I don't see what the individual sizeof() checks gains us. The only field where a sizeof check would be useful is the attr union, but offsetof(following field) is the only way to infer the size, as the union is anonymous.

For the other fields, we'd be checking sizeof(uint{16,32,64}_t) being correct, which is overkill.

~Andrew

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

#6 lore raw

PATCH 2/3 x86/hvm: Rearange check_segment() to use a switch statement

In reply to Andrew Cooper

This simplifies the logic by separating the x86_segment check from the type check. No functional change.

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

---
 xen/arch/x86/hvm/domain.c | 41 ++++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 15 deletions(-)
xen/arch/x86/hvm/domain.c +26 -15
index dca7a00..20a5294 100644
@@ -70,24 +70,35 @@ static int check_segment(struct segment_register *reg, enum x86_segment seg)
         return -EINVAL;
     }
 
-    if ( seg == x86_seg_cs && !(reg->attr.fields.type & 0x8) )
+    switch ( seg )
     {
-        gprintk(XENLOG_ERR, "Non-code segment provided for CS\n");
-        return -EINVAL;
-    }
+    case x86_seg_cs:
+        if ( !(reg->attr.fields.type & 0x8) )
+        {
+            gprintk(XENLOG_ERR, "Non-code segment provided for CS\n");
+            return -EINVAL;
+        }
+        break;
 
-    if ( seg == x86_seg_ss &&
-         ((reg->attr.fields.type & 0x8) || !(reg->attr.fields.type & 0x2)) )
-    {
-        gprintk(XENLOG_ERR, "Non-writeable segment provided for SS\n");
-        return -EINVAL;
-    }
+    case x86_seg_ss:
+        if ( (reg->attr.fields.type & 0x8) || !(reg->attr.fields.type & 0x2) )
+        {
+            gprintk(XENLOG_ERR, "Non-writeable segment provided for SS\n");
+            return -EINVAL;
+        }
+        break;
 
-    if ( reg->attr.fields.s && seg != x86_seg_ss && seg != x86_seg_cs &&
-         (reg->attr.fields.type & 0x8) && !(reg->attr.fields.type & 0x2) )
-    {
-        gprintk(XENLOG_ERR, "Non-readable segment provided for DS or ES\n");
-        return -EINVAL;
+    case x86_seg_ds:
+    case x86_seg_es:
+        if ( (reg->attr.fields.type & 0x8) && !(reg->attr.fields.type & 0x2) )
+        {
+            gprintk(XENLOG_ERR, "Non-readable segment provided for DS or ES\n");
+            return -EINVAL;
+        }
+        break;
+
+    default: /* -Werror=switch */
+        break;
     }
 
     return 0;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] x86/hvm: Rearange check_segment() to use a switch statement

xen/arch/x86/hvm/domain.c static int check_segment(struct segment_register *reg, enum x86_segment seg) · line 102

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:
xen/arch/x86/hvm/domain.c +11 -0
+    case x86_seg_ds:
+    case x86_seg_es:
+        if ( (reg->attr.fields.type & 0x8) && !(reg->attr.fields.type & 0x2) )
+        {
+            gprintk(XENLOG_ERR, "Non-readable segment provided for DS or ES\n");
+            return -EINVAL;
+        }
+        break;
+
+    default: /* -Werror=switch */
+        break;
     }

Perhaps better to have

    default:
        ASSERT_UNREACHABLE();
    case x86_seg_tr:
        break;

to make more visible that it is not an oversight that especially FS and GS aren't being handled here? Either way

Jan

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

#8 lore raw

Re: [PATCH 2/3] x86/hvm: Rearange check_segment() to use a switch statement

Quoted 15 lines · Jan Beulich

xen/arch/x86/hvm/domain.c static int check_segment(struct segment_register *reg, enum x86_segment seg) · line 102

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:
xen/arch/x86/hvm/domain.c +11 -0
+    case x86_seg_ds:
+    case x86_seg_es:
+        if ( (reg->attr.fields.type & 0x8) && !(reg->attr.fields.type & 0x2) )
+        {
+            gprintk(XENLOG_ERR, "Non-readable segment provided for DS or ES\n");
+            return -EINVAL;
+        }
+        break;
+
+    default: /* -Werror=switch */
+        break;
     }

Perhaps better to have

Quoted 8 lines
    default:
        ASSERT_UNREACHABLE();
    case x86_seg_tr:
        break;

to make more visible that it is not an oversight that especially FS and GS aren't being handled here? Either way

The x86_seg_tr case exits check_segment() rather earlier. How about

default:

    ASSERT_UNREACHABLE();
    return -EINVAL;

?

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

#9 lore raw

Re: [PATCH 2/3] x86/hvm: Rearange check_segment() to use a switch statement

Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:15, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

xen/arch/x86/hvm/domain.c static int check_segment(struct segment_register *reg, enum x86_segment seg)

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:
xen/arch/x86/hvm/domain.c +5 -0
+    case x86_seg_ds:
+    case x86_seg_es:
+        if ( (reg->attr.fields.type & 0x8) && !(reg->attr.fields.type & 0x2) )
+        {
+            gprintk(XENLOG_ERR, "Non-readable segment provided for DS or 

ES\n");

Quoted 14 lines

xen/arch/x86/hvm/domain.c static int check_segment(struct segment_register *reg, enum x86_segment seg) · line 102

Quoted 7 lines
xen/arch/x86/hvm/domain.c +6 -0
+            return -EINVAL;
+        }
+        break;
+
+    default: /* -Werror=switch */
+        break;
     }

Perhaps better to have

    default:
        ASSERT_UNREACHABLE();
    case x86_seg_tr:
        break;
Quoted 5 lines
Quoted 3 lines

to make more visible that it is not an oversight that especially FS and GS aren't being handled here? Either way

The x86_seg_tr case exits check_segment() rather earlier.

I don't think it does - there are just two specific error paths there.

Quoted 7 lines

How about

default:

    ASSERT_UNREACHABLE();
    return -EINVAL;

?

Indeed I would have suggested this if I had been able to convince myself that x86_seg_tr can't come here.

Jan

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

#10 lore raw

Re: [PATCH 2/3] x86/hvm: Rearange check_segment() to use a switch statement

Quoted 31 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:15, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

xen/arch/x86/hvm/domain.c static int check_segment(struct segment_register *reg, enum x86_segment seg)

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:
xen/arch/x86/hvm/domain.c +5 -0
+    case x86_seg_ds:
+    case x86_seg_es:
+        if ( (reg->attr.fields.type & 0x8) && !(reg->attr.fields.type & 0x2) )
+        {
+            gprintk(XENLOG_ERR, "Non-readable segment provided for DS or 

ES\n");

Quoted 14 lines

xen/arch/x86/hvm/domain.c static int check_segment(struct segment_register *reg, enum x86_segment seg) · line 102

Quoted 7 lines
xen/arch/x86/hvm/domain.c +6 -0
+            return -EINVAL;
+        }
+        break;
+
+    default: /* -Werror=switch */
+        break;
     }

Perhaps better to have

    default:
        ASSERT_UNREACHABLE();
    case x86_seg_tr:
        break;
Quoted 4 lines
Quoted 3 lines

to make more visible that it is not an oversight that especially FS and GS aren't being handled here? Either way

The x86_seg_tr case exits check_segment() rather earlier.

I don't think it does - there are just two specific error paths there.

Quoted 2 lines

How about

Quoted 7 lines
Quoted 5 lines

default:

    ASSERT_UNREACHABLE();
    return -EINVAL;

?

Indeed I would have suggested this if I had been able to convince myself that x86_seg_tr can't come here.

You are quite right. I was mistaken. I will go with this:

    case x86_seg_tr:
        break;

    default:
        ASSERT_UNREACHABLE();
        return -EINVAL;
    }

which fails slightly safer than your suggestion in release builds.

~Andrew

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

#11 lore raw

Re: [PATCH 2/3] x86/hvm: Rearange check_segment() to use a switch statement

Quoted 51 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:58, <[email protected]> wrote:
Quoted 31 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:15, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

xen/arch/x86/hvm/domain.c static int check_segment(struct segment_register *reg, enum x86_segment seg)

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:
xen/arch/x86/hvm/domain.c +5 -0
+    case x86_seg_ds:
+    case x86_seg_es:
+        if ( (reg->attr.fields.type & 0x8) && !(reg->attr.fields.type & 0x2) )
+        {
+            gprintk(XENLOG_ERR, "Non-readable segment provided for DS or 

ES\n");

Quoted 14 lines

xen/arch/x86/hvm/domain.c static int check_segment(struct segment_register *reg, enum x86_segment seg) · line 102

Quoted 7 lines
xen/arch/x86/hvm/domain.c +6 -0
+            return -EINVAL;
+        }
+        break;
+
+    default: /* -Werror=switch */
+        break;
     }

Perhaps better to have

    default:
        ASSERT_UNREACHABLE();
    case x86_seg_tr:
        break;
Quoted 4 lines
Quoted 3 lines

to make more visible that it is not an oversight that especially FS and GS aren't being handled here? Either way

The x86_seg_tr case exits check_segment() rather earlier.

I don't think it does - there are just two specific error paths there.

Quoted 2 lines

How about

Quoted 7 lines
Quoted 5 lines

default:

    ASSERT_UNREACHABLE();
    return -EINVAL;

?

Indeed I would have suggested this if I had been able to convince myself that x86_seg_tr can't come here.

You are quite right. I was mistaken. I will go with this:

    case x86_seg_tr:
        break;

    default:
        ASSERT_UNREACHABLE();
        return -EINVAL;
    }
Quoted 1 line

which fails slightly safer than your suggestion in release builds.

Ah, yes, agreed.

Jan

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

#12 lore raw

PATCH 3/3 x86/emul: Drop segment_attributes_t

In reply to Andrew Cooper

The amount of namespace resolution is unnecessarily large, as all code deals in terms of struct segment_register. This removes the attr.fields part of all references, and alters attr.bytes to just attr.

Three areas of code using initialisers for segment_register are tweaked to compile with older versions of GCC. arch_set_info_hvm_guest() has its SEG() macros altered to use plain comma-based initialisation, while {rm,vm86}_{cs,ds}_attr are simplified to plain numbers which matches their description in the manuals.

No functional change. (For some reason, the old {rm,vm86}_{cs,ds}_attr causes GCC to create variable in .rodata, whereas the new code uses immediate operands. As a result, vmx_{get,set}_segment_register() are slightly shorter.)

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

Since RFC:
* Drop attr sub union entirely
* Compile tested with GCC 4.1

---
 tools/fuzz/x86_instruction_emulator/fuzz-emul.c |  10 +-
 tools/tests/x86_emulator/test_x86_emulator.c    |   2 +-
 xen/arch/x86/cpu/vpmu.c                         |   2 +-
 xen/arch/x86/hvm/domain.c                       |  43 ++++---
 xen/arch/x86/hvm/emulate.c                      |  20 +--
 xen/arch/x86/hvm/hvm.c                          | 154 ++++++++++++------------
 xen/arch/x86/hvm/svm/svm.c                      |  10 +-
 xen/arch/x86/hvm/svm/svmdebug.c                 |   4 +-
 xen/arch/x86/hvm/svm/vmcb.c                     |  16 +--
 xen/arch/x86/hvm/vmx/realmode.c                 |  10 +-
 xen/arch/x86/hvm/vmx/vmx.c                      |  41 +++----
 xen/arch/x86/mm/shadow/common.c                 |   6 +-
 xen/arch/x86/pv/emul-priv-op.c                  |  40 +++---
 xen/arch/x86/vm_event.c                         |   2 +-
 xen/arch/x86/x86_emulate/x86_emulate.c          |  55 +++++----
 xen/arch/x86/x86_emulate/x86_emulate.h          |  37 +++---
 16 files changed, 219 insertions(+), 233 deletions(-)
tools/fuzz/x86_instruction_emulator/fuzz-emul.c +5 -5
index aadbb40..a2329f8 100644
@@ -583,7 +583,7 @@ static bool in_longmode(struct x86_emulate_ctxt *ctxt)
     const struct fuzz_state *s = ctxt->data;
     const struct fuzz_corpus *c = s->corpus;
 
-    return long_mode_active(ctxt) && c->segments[x86_seg_cs].attr.fields.l;
+    return long_mode_active(ctxt) && c->segments[x86_seg_cs].l;
 }
 
 static void set_sizes(struct x86_emulate_ctxt *ctxt)
@@ -597,8 +597,8 @@ static void set_sizes(struct x86_emulate_ctxt *ctxt)
         ctxt->addr_size = ctxt->sp_size = 64;
     else
     {
-        ctxt->addr_size = c->segments[x86_seg_cs].attr.fields.db ? 32 : 16;
-        ctxt->sp_size   = c->segments[x86_seg_ss].attr.fields.db ? 32 : 16;
+        ctxt->addr_size = c->segments[x86_seg_cs].db ? 32 : 16;
+        ctxt->sp_size   = c->segments[x86_seg_ss].db ? 32 : 16;
     }
 }
 
@@ -741,8 +741,8 @@ static void sanitize_input(struct x86_emulate_ctxt *ctxt)
     /* EFLAGS.VM implies 16-bit mode */
     if ( regs->rflags & X86_EFLAGS_VM )
     {
-        c->segments[x86_seg_cs].attr.fields.db = 0;
-        c->segments[x86_seg_ss].attr.fields.db = 0;
+        c->segments[x86_seg_cs].db = 0;
+        c->segments[x86_seg_ss].db = 0;
     }
 }
 
tools/tests/x86_emulator/test_x86_emulator.c +1 -1
index 1955332..76665ab 100644
@@ -264,7 +264,7 @@ static int read_segment(
     if ( !is_x86_user_segment(seg) )
         return X86EMUL_UNHANDLEABLE;
     memset(reg, 0, sizeof(*reg));
-    reg->attr.fields.p = 1;
+    reg->p = 1;
     return X86EMUL_OKAY;
 }
 
xen/arch/x86/cpu/vpmu.c +1 -1
index 21383d3..90954ca 100644
@@ -304,7 +304,7 @@ void vpmu_do_interrupt(struct cpu_user_regs *regs)
                 r->cs = seg.sel;
                 hvm_get_segment_register(sampled, x86_seg_ss, &seg);
                 r->ss = seg.sel;
-                r->cpl = seg.attr.fields.dpl;
+                r->cpl = seg.dpl;
                 if ( !(sampled->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PE) )
                     *flags |= PMU_SAMPLE_REAL;
             }
xen/arch/x86/hvm/domain.c +21 -22

131 lines not on this page · raw message

xen/arch/x86/hvm/emulate.c +10 -10
index 11e4aba..f249add 100644
@@ -873,7 +873,7 @@ static int __hvmemul_read(
 
     if ( is_x86_system_segment(seg) )
         pfec |= PFEC_implicit;
-    else if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
+    else if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
         pfec |= PFEC_user_mode;
 
     rc = hvmemul_virtual_to_linear(
@@ -984,7 +984,7 @@ static int hvmemul_write(
 
     if ( is_x86_system_segment(seg) )
         pfec |= PFEC_implicit;
-    else if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
+    else if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
         pfec |= PFEC_user_mode;
 
     rc = hvmemul_virtual_to_linear(
@@ -1161,7 +1161,7 @@ static int hvmemul_rep_ins(
     if ( rc != X86EMUL_OKAY )
         return rc;
 
-    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
+    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
         pfec |= PFEC_user_mode;
 
     rc = hvmemul_linear_to_phys(
@@ -1230,7 +1230,7 @@ static int hvmemul_rep_outs(
     if ( rc != X86EMUL_OKAY )
         return rc;
 
-    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
+    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
         pfec |= PFEC_user_mode;
 
     rc = hvmemul_linear_to_phys(
@@ -1277,7 +1277,7 @@ static int hvmemul_rep_movs(
     if ( rc != X86EMUL_OKAY )
         return rc;
 
-    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
+    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
         pfec |= PFEC_user_mode;
 
     if ( vio->mmio_access.read_access &&
@@ -1435,7 +1435,7 @@ static int hvmemul_rep_stos(
     {
         uint32_t pfec = PFEC_page_present | PFEC_write_access;
 
-        if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
+        if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
             pfec |= PFEC_user_mode;
 
         rc = hvmemul_linear_to_phys(addr, &gpa, bytes_per_rep, reps, pfec,
@@ -2133,17 +2133,17 @@ void hvm_emulate_init_per_insn(
     hvmemul_ctxt->ctxt.lma = hvm_long_mode_active(curr);
 
     if ( hvmemul_ctxt->ctxt.lma &&
-         hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.l )
+         hvmemul_ctxt->seg_reg[x86_seg_cs].l )
         hvmemul_ctxt->ctxt.addr_size = hvmemul_ctxt->ctxt.sp_size = 64;
     else
     {
         hvmemul_ctxt->ctxt.addr_size =
-            hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.db ? 32 : 16;
+            hvmemul_ctxt->seg_reg[x86_seg_cs].db ? 32 : 16;
         hvmemul_ctxt->ctxt.sp_size =
-            hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.db ? 32 : 16;
+            hvmemul_ctxt->seg_reg[x86_seg_ss].db ? 32 : 16;
     }
 
-    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
+    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
         pfec |= PFEC_user_mode;
 
     hvmemul_ctxt->insn_buf_eip = hvmemul_ctxt->ctxt.regs->rip;
xen/arch/x86/hvm/hvm.c +77 -77

446 lines not on this page · raw message

xen/arch/x86/hvm/svm/svm.c +5 -5
index 402e815..244da12 100644
@@ -526,9 +526,9 @@ static int svm_guest_x86_mode(struct vcpu *v)
         return 0;
     if ( unlikely(guest_cpu_user_regs()->eflags & X86_EFLAGS_VM) )
         return 1;
-    if ( hvm_long_mode_active(v) && likely(vmcb->cs.attr.fields.l) )
+    if ( hvm_long_mode_active(v) && likely(vmcb->cs.l) )
         return 8;
-    return (likely(vmcb->cs.attr.fields.db) ? 4 : 2);
+    return likely(vmcb->cs.db) ? 4 : 2;
 }
 
 void svm_update_guest_cr(struct vcpu *v, unsigned int cr)
@@ -653,7 +653,7 @@ static void svm_get_segment_register(struct vcpu *v, enum x86_segment seg,
         break;
     case x86_seg_ss:
         *reg = vmcb->ss;
-        reg->attr.fields.dpl = vmcb_get_cpl(vmcb);
+        reg->dpl = vmcb_get_cpl(vmcb);
         break;
     case x86_seg_tr:
         svm_sync_vmcb(v);
@@ -726,7 +726,7 @@ static void svm_set_segment_register(struct vcpu *v, enum x86_segment seg,
         break;
     case x86_seg_ss:
         vmcb->ss = *reg;
-        vmcb_set_cpl(vmcb, reg->attr.fields.dpl);
+        vmcb_set_cpl(vmcb, reg->dpl);
         break;
     case x86_seg_tr:
         vmcb->tr = *reg;
@@ -1442,7 +1442,7 @@ static void svm_inject_event(const struct x86_event *event)
      * If injecting an event outside of 64bit mode, zero the upper bits of the
      * %eip and nextrip after the adjustments above.
      */
-    if ( !((vmcb_get_efer(vmcb) & EFER_LMA) && vmcb->cs.attr.fields.l) )
+    if ( !((vmcb_get_efer(vmcb) & EFER_LMA) && vmcb->cs.l) )
     {
         regs->rip = regs->eip;
         vmcb->nextrip = (uint32_t)vmcb->nextrip;
xen/arch/x86/hvm/svm/svmdebug.c +2 -2
index a3f8685..ce788e4 100644
@@ -24,7 +24,7 @@
 static void svm_dump_sel(const char *name, const svm_segment_register_t *s)
 {
     printk("%s: %04x %04x %08x %016"PRIx64"\n",
-           name, s->sel, s->attr.bytes, s->limit, s->base);
+           name, s->sel, s->attr, s->limit, s->base);
 }
 
 void svm_vmcb_dump(const char *from, const struct vmcb_struct *vmcb)
@@ -147,7 +147,7 @@ bool svm_vmcb_isvalid(const char *from, const struct vmcb_struct *vmcb,
     }
 
     if ( (efer & EFER_LME) && (cr0 & X86_CR0_PG) && (cr4 & X86_CR4_PAE) &&
-         vmcb->cs.attr.fields.l && vmcb->cs.attr.fields.db )
+         vmcb->cs.l && vmcb->cs.db )
         PRINTF("EFER_LME, CR0.PG, CR4.PAE, CS.L and CS.D are all non-zero\n");
 
     if ( !(vmcb_get_general2_intercepts(vmcb) & GENERAL2_INTERCEPT_VMRUN) )
xen/arch/x86/hvm/svm/vmcb.c +8 -8
index 96abf8d..82fe9d2 100644
@@ -158,12 +158,12 @@ static int construct_vmcb(struct vcpu *v)
     vmcb->gs.base = 0;
 
     /* Guest segment AR bytes. */
-    vmcb->es.attr.bytes = 0xc93; /* read/write, accessed */
-    vmcb->ss.attr.bytes = 0xc93;
-    vmcb->ds.attr.bytes = 0xc93;
-    vmcb->fs.attr.bytes = 0xc93;
-    vmcb->gs.attr.bytes = 0xc93;
-    vmcb->cs.attr.bytes = 0xc9b; /* exec/read, accessed */
+    vmcb->es.attr = 0xc93; /* read/write, accessed */
+    vmcb->ss.attr = 0xc93;
+    vmcb->ds.attr = 0xc93;
+    vmcb->fs.attr = 0xc93;
+    vmcb->gs.attr = 0xc93;
+    vmcb->cs.attr = 0xc9b; /* exec/read, accessed */
 
     /* Guest IDT. */
     vmcb->idtr.base = 0;
@@ -177,10 +177,10 @@ static int construct_vmcb(struct vcpu *v)
     vmcb->ldtr.sel = 0;
     vmcb->ldtr.base = 0;
     vmcb->ldtr.limit = 0;
-    vmcb->ldtr.attr.bytes = 0;
+    vmcb->ldtr.attr = 0;
 
     /* Guest TSS. */
-    vmcb->tr.attr.bytes = 0x08b; /* 32-bit TSS (busy) */
+    vmcb->tr.attr = 0x08b; /* 32-bit TSS (busy) */
     vmcb->tr.base = 0;
     vmcb->tr.limit = 0xff;
 
xen/arch/x86/hvm/vmx/realmode.c +5 -5
index 1996b1f..11bde58 100644
@@ -70,7 +70,7 @@ static void realmode_deliver_exception(
     frame[2] = regs->flags & ~X86_EFLAGS_RF;
 
     /* We can't test hvmemul_ctxt->ctxt.sp_size: it may not be initialised. */
-    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.db )
+    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].db )
         pstk = regs->esp -= 6;
     else
         pstk = regs->sp -= 6;
@@ -207,13 +207,13 @@ void vmx_realmode(struct cpu_user_regs *regs)
          * DS, ES, FS and GS the most uninvasive trick is to set DPL == RPL.
          */
         sreg = hvmemul_get_seg_reg(x86_seg_ds, &hvmemul_ctxt);
-        sreg->attr.fields.dpl = sreg->sel & 3;
+        sreg->dpl = sreg->sel & 3;
         sreg = hvmemul_get_seg_reg(x86_seg_es, &hvmemul_ctxt);
-        sreg->attr.fields.dpl = sreg->sel & 3;
+        sreg->dpl = sreg->sel & 3;
         sreg = hvmemul_get_seg_reg(x86_seg_fs, &hvmemul_ctxt);
-        sreg->attr.fields.dpl = sreg->sel & 3;
+        sreg->dpl = sreg->sel & 3;
         sreg = hvmemul_get_seg_reg(x86_seg_gs, &hvmemul_ctxt);
-        sreg->attr.fields.dpl = sreg->sel & 3;
+        sreg->dpl = sreg->sel & 3;
         hvmemul_ctxt.seg_reg_dirty |=
             (1ul << x86_seg_ds) | (1ul << x86_seg_es) |
             (1ul << x86_seg_fs) | (1ul << x86_seg_gs);
xen/arch/x86/hvm/vmx/vmx.c +18 -23

71 lines not on this page · raw message

xen/arch/x86/mm/shadow/common.c +3 -3
index 2e64a77..5662cac 100644
@@ -332,13 +332,13 @@ const struct x86_emulate_ops *shadow_init_emulation(
     creg = hvm_get_seg_reg(x86_seg_cs, sh_ctxt);
 
     /* Work out the emulation mode. */
-    if ( sh_ctxt->ctxt.lma && creg->attr.fields.l )
+    if ( sh_ctxt->ctxt.lma && creg->l )
         sh_ctxt->ctxt.addr_size = sh_ctxt->ctxt.sp_size = 64;
     else
     {
         sreg = hvm_get_seg_reg(x86_seg_ss, sh_ctxt);
-        sh_ctxt->ctxt.addr_size = creg->attr.fields.db ? 32 : 16;
-        sh_ctxt->ctxt.sp_size   = sreg->attr.fields.db ? 32 : 16;
+        sh_ctxt->ctxt.addr_size = creg->db ? 32 : 16;
+        sh_ctxt->ctxt.sp_size   = sreg->db ? 32 : 16;
     }
 
     /* Attempt to prefetch whole instruction. */
xen/arch/x86/pv/emul-priv-op.c +20 -20
index 85185b6..d50f519 100644
@@ -479,7 +479,7 @@ static int priv_op_read_segment(enum x86_segment seg,
             return X86EMUL_UNHANDLEABLE;
 
         reg->limit = limit;
-        reg->attr.bytes = ar >> 8;
+        reg->attr = ar >> 8;
     }
     else
     {
@@ -500,19 +500,19 @@ static int priv_op_read_segment(enum x86_segment seg,
 
         reg->limit = ~0U;
 
-        reg->attr.bytes = 0;
-        reg->attr.fields.type = _SEGMENT_WR >> 8;
+        reg->attr = 0;
+        reg->type = _SEGMENT_WR >> 8;
         if ( seg == x86_seg_cs )
         {
-            reg->attr.fields.type |= _SEGMENT_CODE >> 8;
-            reg->attr.fields.l = 1;
+            reg->type |= _SEGMENT_CODE >> 8;
+            reg->l = 1;
         }
         else
-            reg->attr.fields.db = 1;
-        reg->attr.fields.s   = 1;
-        reg->attr.fields.dpl = 3;
-        reg->attr.fields.p   = 1;
-        reg->attr.fields.g   = 1;
+            reg->db = 1;
+        reg->s   = 1;
+        reg->dpl = 3;
+        reg->p   = 1;
+        reg->g   = 1;
     }
 
     /*
@@ -521,9 +521,9 @@ static int priv_op_read_segment(enum x86_segment seg,
      */
     if ( (seg == x86_seg_ss ||
           (seg == x86_seg_cs &&
-           !(reg->attr.fields.type & (_SEGMENT_EC >> 8)))) &&
+           !(reg->type & (_SEGMENT_EC >> 8)))) &&
          guest_kernel_mode(current, ctxt->regs) )
-        reg->attr.fields.dpl = 0;
+        reg->dpl = 0;
 
     return X86EMUL_OKAY;
 }
@@ -578,11 +578,11 @@ static int priv_op_rep_ins(uint16_t port,
     if ( rc != X86EMUL_OKAY )
         return rc;
 
-    if ( !sreg.attr.fields.p )
+    if ( !sreg.p )
         return X86EMUL_UNHANDLEABLE;
-    if ( !sreg.attr.fields.s ||
-         (sreg.attr.fields.type & (_SEGMENT_CODE >> 8)) ||
-         !(sreg.attr.fields.type & (_SEGMENT_WR >> 8)) )
+    if ( !sreg.s ||
+         (sreg.type & (_SEGMENT_CODE >> 8)) ||
+         !(sreg.type & (_SEGMENT_WR >> 8)) )
     {
         x86_emul_hw_exception(TRAP_gp_fault, 0, ctxt);
         return X86EMUL_EXCEPTION;
@@ -643,11 +643,11 @@ static int priv_op_rep_outs(enum x86_segment seg, unsigned long offset,
     if ( rc != X86EMUL_OKAY )
         return rc;
 
-    if ( !sreg.attr.fields.p )
+    if ( !sreg.p )
         return X86EMUL_UNHANDLEABLE;
-    if ( !sreg.attr.fields.s ||
-         ((sreg.attr.fields.type & (_SEGMENT_CODE >> 8)) &&
-          !(sreg.attr.fields.type & (_SEGMENT_WR >> 8))) )
+    if ( !sreg.s ||
+         ((sreg.type & (_SEGMENT_CODE >> 8)) &&
+          !(sreg.type & (_SEGMENT_WR >> 8))) )
     {
         x86_emul_hw_exception(seg != x86_seg_ss ? TRAP_gp_fault
                                                 : TRAP_stack_error,
xen/arch/x86/vm_event.c +1 -1
index a6ea42c..f91aade 100644
@@ -176,7 +176,7 @@ void vm_event_fill_regs(vm_event_request_t *req)
     req->data.regs.x86.gs_base = seg.base;
 
     hvm_get_segment_register(curr, x86_seg_cs, &seg);
-    req->data.regs.x86.cs_arbytes = seg.attr.bytes;
+    req->data.regs.x86.cs_arbytes = seg.attr;
 }
 
 void vm_event_emulate_check(struct vcpu *v, vm_event_response_t *rsp)
xen/arch/x86/x86_emulate/x86_emulate.c +27 -28

152 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +15 -22
index e5ec8a6..4627136 100644
@@ -83,33 +83,26 @@ struct x86_event {
     unsigned long cr2;          /* Only for TRAP_page_fault h/w exception */
 };
 
-/* 
- * Attribute for segment selector. This is a copy of bit 40:47 & 52:55 of the
- * segment descriptor. It happens to match the format of an AMD SVM VMCB.
- */
-typedef union segment_attributes {
-    uint16_t bytes;
-    struct
-    {
-        uint16_t type:4;    /* 0;  Bit 40-43 */
-        uint16_t s:   1;    /* 4;  Bit 44 */
-        uint16_t dpl: 2;    /* 5;  Bit 45-46 */
-        uint16_t p:   1;    /* 7;  Bit 47 */
-        uint16_t avl: 1;    /* 8;  Bit 52 */
-        uint16_t l:   1;    /* 9;  Bit 53 */
-        uint16_t db:  1;    /* 10; Bit 54 */
-        uint16_t g:   1;    /* 11; Bit 55 */
-        uint16_t pad: 4;
-    } fields;
-} segment_attributes_t;
-
 /*
  * Full state of a segment register (visible and hidden portions).
- * Again, this happens to match the format of an AMD SVM VMCB.
+ * Chosen to match the format of an AMD SVM VMCB.
  */
 struct segment_register {
     uint16_t   sel;
-    segment_attributes_t attr;
+    union {
+        uint16_t attr;
+        struct {
+            uint16_t type:4;    /* 0;  Bit 40-43 */
+            uint16_t s:   1;    /* 4;  Bit 44 */
+            uint16_t dpl: 2;    /* 5;  Bit 45-46 */
+            uint16_t p:   1;    /* 7;  Bit 47 */
+            uint16_t avl: 1;    /* 8;  Bit 52 */
+            uint16_t l:   1;    /* 9;  Bit 53 */
+            uint16_t db:  1;    /* 10; Bit 54 */
+            uint16_t g:   1;    /* 11; Bit 55 */
+            uint16_t pad: 4;
+        };
+    };
     uint32_t   limit;
     uint64_t   base;
 };
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 3/3
#13 lore raw

Re: [PATCH 3/3] x86/emul: Drop segment_attributes_t

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:

The amount of namespace resolution is unnecessarily large, as all code deals in terms of struct segment_register. This removes the attr.fields part of all references, and alters attr.bytes to just attr.

Yeah, quite a bit easier to read and type this way.

xen/arch/x86/hvm/domain.c int arch_set_info_hvm_guest(struct vcpu *v, const vcpu_hvm_context_t *ctx) · line 258

Quoted 6 lines
xen/arch/x86/hvm/domain.c +1 -1
@@ -256,7 +255,7 @@ int arch_set_info_hvm_guest(struct vcpu *v, const vcpu_hvm_context_t *ctx)
         v->arch.hvm_vcpu.guest_cr[4] = regs->cr4;
         v->arch.hvm_vcpu.guest_efer  = regs->efer;
 
-#define SEG(l, a) (struct segment_register){ .limit = (l), .attr.bytes = (a) }
+#define SEG(l, a) (struct segment_register){ 0, { (a) }, (l), 0 }

The parens around a and l are pointless here.

xen/arch/x86/hvm/hvm.c void hvm_vcpu_reset_state(struct vcpu *v, uint16_t cs, uint16_t ip) · line 3840

Quoted 12 lines
xen/arch/x86/hvm/hvm.c +2 -2
@@ -3832,25 +3832,25 @@ void hvm_vcpu_reset_state(struct vcpu *v, uint16_t cs, uint16_t ip)
     reg.sel = cs;
     reg.base = (uint32_t)reg.sel << 4;
     reg.limit = 0xffff;
-    reg.attr.bytes = 0x09b;
+    reg.attr = 0x09b;
     hvm_set_segment_register(v, x86_seg_cs, &reg);
 
     reg.sel = reg.base = 0;
     reg.limit = 0xffff;
-    reg.attr.bytes = 0x093;
+    reg.attr = 0x093;

Could I talk you into at once dropping the stray inner zeros in both cases?

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_event { · line 90

Quoted 11 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -4
@@ -83,33 +83,26 @@ struct x86_event {
     unsigned long cr2;          /* Only for TRAP_page_fault h/w exception 
 */
 };
 
-/* 
- * Attribute for segment selector. This is a copy of bit 40:47 & 52:55 of the
- * segment descriptor. It happens to match the format of an AMD SVM VMCB.
- */

The reference to segment descriptors here helped understand what ...

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_event { · line 101

Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -12
-typedef union segment_attributes {
-    uint16_t bytes;
-    struct
-    {
-        uint16_t type:4;    /* 0;  Bit 40-43 */
-        uint16_t s:   1;    /* 4;  Bit 44 */
-        uint16_t dpl: 2;    /* 5;  Bit 45-46 */
-        uint16_t p:   1;    /* 7;  Bit 47 */
-        uint16_t avl: 1;    /* 8;  Bit 52 */
-        uint16_t l:   1;    /* 9;  Bit 53 */
-        uint16_t db:  1;    /* 10; Bit 54 */
-        uint16_t g:   1;    /* 11; Bit 55 */

... the bit numbers here refer to. Hence I like to ask to either restore that reference or ...

Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.h +2 -2
 /*
  * Full state of a segment register (visible and hidden portions).
- * Again, this happens to match the format of an AMD SVM VMCB.
+ * Chosen to match the format of an AMD SVM VMCB.
  */
 struct segment_register {
     uint16_t   sel;
-    segment_attributes_t attr;
+    union {

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_event { · line 102

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +10 -0
+        uint16_t attr;
+        struct {
+            uint16_t type:4;    /* 0;  Bit 40-43 */
+            uint16_t s:   1;    /* 4;  Bit 44 */
+            uint16_t dpl: 2;    /* 5;  Bit 45-46 */
+            uint16_t p:   1;    /* 7;  Bit 47 */
+            uint16_t avl: 1;    /* 8;  Bit 52 */
+            uint16_t l:   1;    /* 9;  Bit 53 */
+            uint16_t db:  1;    /* 10; Bit 54 */
+            uint16_t g:   1;    /* 11; Bit 55 */

... drop the Bit NN comments here, as these aren't bit numbers inside the VMCB field layout afaict. With at least this last aspect taken care of

Jan

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

#14 lore raw

Re: [PATCH 3/3] x86/emul: Drop segment_attributes_t

Quoted 6 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.06.17 at 17:04, <[email protected]> wrote:

The amount of namespace resolution is unnecessarily large, as all code deals in terms of struct segment_register. This removes the attr.fields part of all references, and alters attr.bytes to just attr.

Yeah, quite a bit easier to read and type this way.

Quoted 7 lines

xen/arch/x86/hvm/domain.c int arch_set_info_hvm_guest(struct vcpu *v, const vcpu_hvm_context_t *ctx) · line 258

Quoted 6 lines
xen/arch/x86/hvm/domain.c +1 -1
@@ -256,7 +255,7 @@ int arch_set_info_hvm_guest(struct vcpu *v, const vcpu_hvm_context_t *ctx)
         v->arch.hvm_vcpu.guest_cr[4] = regs->cr4;
         v->arch.hvm_vcpu.guest_efer  = regs->efer;
 
-#define SEG(l, a) (struct segment_register){ .limit = (l), .attr.bytes = (a) }
+#define SEG(l, a) (struct segment_register){ 0, { (a) }, (l), 0 }

The parens around a and l are pointless here.

Will do.

Quoted 9 lines
Quoted 9 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -3832,25 +3832,25 @@ void hvm_vcpu_reset_state(struct vcpu *v, uint16_t cs, uint16_t ip)
     reg.sel = cs;
     reg.base = (uint32_t)reg.sel << 4;
     reg.limit = 0xffff;
-    reg.attr.bytes = 0x09b;
+    reg.attr = 0x09b;
     hvm_set_segment_register(v, x86_seg_cs, &reg);
 
Quoted 6 lines

xen/arch/x86/hvm/hvm.c void hvm_vcpu_reset_state(struct vcpu *v, uint16_t cs, uint16_t ip) · line 3840

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +1 -1
     reg.sel = reg.base = 0;
     reg.limit = 0xffff;
-    reg.attr.bytes = 0x093;
+    reg.attr = 0x093;

Could I talk you into at once dropping the stray inner zeros in both cases?

Will do.

Quoted 48 lines

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_event { · line 90

Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -4
@@ -83,33 +83,26 @@ struct x86_event {
     unsigned long cr2;          /* Only for TRAP_page_fault h/w exception 
 */
 };
 
-/* 
- * Attribute for segment selector. This is a copy of bit 40:47 & 52:55 of the
- * segment descriptor. It happens to match the format of an AMD SVM VMCB.
- */

The reference to segment descriptors here helped understand what ...

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_event { · line 101

Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -12
-typedef union segment_attributes {
-    uint16_t bytes;
-    struct
-    {
-        uint16_t type:4;    /* 0;  Bit 40-43 */
-        uint16_t s:   1;    /* 4;  Bit 44 */
-        uint16_t dpl: 2;    /* 5;  Bit 45-46 */
-        uint16_t p:   1;    /* 7;  Bit 47 */
-        uint16_t avl: 1;    /* 8;  Bit 52 */
-        uint16_t l:   1;    /* 9;  Bit 53 */
-        uint16_t db:  1;    /* 10; Bit 54 */
-        uint16_t g:   1;    /* 11; Bit 55 */

... the bit numbers here refer to. Hence I like to ask to either restore that reference or ...

Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.h +2 -2
 /*
  * Full state of a segment register (visible and hidden portions).
- * Again, this happens to match the format of an AMD SVM VMCB.
+ * Chosen to match the format of an AMD SVM VMCB.
  */
 struct segment_register {
     uint16_t   sel;
-    segment_attributes_t attr;
+    union {

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_event { · line 102

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +10 -0
+        uint16_t attr;
+        struct {
+            uint16_t type:4;    /* 0;  Bit 40-43 */
+            uint16_t s:   1;    /* 4;  Bit 44 */
+            uint16_t dpl: 2;    /* 5;  Bit 45-46 */
+            uint16_t p:   1;    /* 7;  Bit 47 */
+            uint16_t avl: 1;    /* 8;  Bit 52 */
+            uint16_t l:   1;    /* 9;  Bit 53 */
+            uint16_t db:  1;    /* 10; Bit 54 */
+            uint16_t g:   1;    /* 11; Bit 55 */
Quoted 4 lines

... drop the Bit NN comments here, as these aren't bit numbers inside the VMCB field layout afaict. With at least this last aspect taken care of

I think I'd prefer to omit the comments entirely. The main structure comment identifies which layout we've chosen to use, and its not like these bitfield names are obscure in any segment register/descriptor context.

~Andrew

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

#15 lore raw

[PATCH 4/3] x86/svm: Drop svm_segment_register_t

In reply to Andrew Cooper

Most SVM code already uses struct segment_register. Drop the typedef and adjust the definitions in struct vmcb_struct, and svm_dump_sel(). Introduce some build-time assertions that struct segment_register from the common emulation code is usable in struct vmcb_struct.

While making these adjustments, fix some comments to not mix decimal and hexidecimal offsets, and drop all trailing whitespace in vmcb.h

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

---
 xen/arch/x86/hvm/svm/svmdebug.c    |  2 +-
 xen/arch/x86/hvm/svm/vmcb.c        |  9 +++++++++
 xen/include/asm-x86/hvm/svm/vmcb.h | 37 +++++++++++++++++--------------------
 3 files changed, 27 insertions(+), 21 deletions(-)
xen/arch/x86/hvm/svm/svmdebug.c +1 -1
index ce788e4..89ef2db 100644
@@ -21,7 +21,7 @@
 #include <asm/msr-index.h>
 #include <asm/hvm/svm/svmdebug.h>
 
-static void svm_dump_sel(const char *name, const svm_segment_register_t *s)
+static void svm_dump_sel(const char *name, const struct segment_register *s)
 {
     printk("%s: %04x %04x %08x %016"PRIx64"\n",
            name, s->sel, s->attr, s->limit, s->base);
xen/arch/x86/hvm/svm/vmcb.c +9 -0
index 82fe9d2..efe9657 100644
@@ -310,6 +310,15 @@ void __init setup_vmcb_dump(void)
     register_keyhandler('v', vmcb_dump, "dump AMD-V VMCBs", 1);
 }
 
+static void __init __maybe_unused build_assertions(void)
+{
+    /* Check struct segment_register against the VMCB segment layout. */
+    BUILD_BUG_ON(sizeof(struct segment_register) != 16);
+    BUILD_BUG_ON(offsetof(struct segment_register, attr) != 2);
+    BUILD_BUG_ON(offsetof(struct segment_register, limit) != 4);
+    BUILD_BUG_ON(offsetof(struct segment_register, base) != 8);
+}
+
 /*
  * Local variables:
  * mode: C
xen/include/asm-x86/hvm/svm/vmcb.h +17 -20
index 30a228b..fa0d3e2 100644
@@ -31,7 +31,7 @@ enum GenericIntercept1bits
     GENERAL1_INTERCEPT_SMI           = 1 << 2,
     GENERAL1_INTERCEPT_INIT          = 1 << 3,
     GENERAL1_INTERCEPT_VINTR         = 1 << 4,
-    GENERAL1_INTERCEPT_CR0_SEL_WRITE = 1 << 5, 
+    GENERAL1_INTERCEPT_CR0_SEL_WRITE = 1 << 5,
     GENERAL1_INTERCEPT_IDTR_READ     = 1 << 6,
     GENERAL1_INTERCEPT_GDTR_READ     = 1 << 7,
     GENERAL1_INTERCEPT_LDTR_READ     = 1 << 8,
@@ -304,13 +304,10 @@ enum VMEXIT_EXITCODE
     VMEXIT_INVALID          =  -1
 };
 
-/* Definition of segment state is borrowed by the generic HVM code. */
-typedef struct segment_register svm_segment_register_t;
-
 typedef union
 {
     u64 bytes;
-    struct 
+    struct
     {
         u64 vector:    8;
         u64 type:      3;
@@ -324,7 +321,7 @@ typedef union
 typedef union
 {
     u64 bytes;
-    struct 
+    struct
     {
         u64 tpr:          8;
         u64 irq:          1;
@@ -342,7 +339,7 @@ typedef union
 typedef union
 {
     u64 bytes;
-    struct 
+    struct
     {
         u64 type: 1;
         u64 rsv0: 1;
@@ -438,23 +435,23 @@ struct vmcb_struct {
     u8  guest_ins[15];          /* offset 0xD1 */
     u64 res10a[100];            /* offset 0xE0 pad to save area */
 
-    svm_segment_register_t es;  /* offset 1024 - cleanbit 8 */
-    svm_segment_register_t cs;  /* cleanbit 8 */
-    svm_segment_register_t ss;  /* cleanbit 8 */
-    svm_segment_register_t ds;  /* cleanbit 8 */
-    svm_segment_register_t fs;
-    svm_segment_register_t gs;
-    svm_segment_register_t gdtr; /* cleanbit 7 */
-    svm_segment_register_t ldtr;
-    svm_segment_register_t idtr; /* cleanbit 7 */
-    svm_segment_register_t tr;
+    struct segment_register es;  /* offset 0x400 - cleanbit 8 */
+    struct segment_register cs;  /* cleanbit 8 */
+    struct segment_register ss;  /* cleanbit 8 */
+    struct segment_register ds;  /* cleanbit 8 */
+    struct segment_register fs;
+    struct segment_register gs;
+    struct segment_register gdtr; /* cleanbit 7 */
+    struct segment_register ldtr;
+    struct segment_register idtr; /* cleanbit 7 */
+    struct segment_register tr;
     u64 res10[5];
     u8 res11[3];
     u8 _cpl;                    /* cleanbit 8 */
     u32 res12;
-    u64 _efer;                  /* offset 1024 + 0xD0  - cleanbit 5 */
+    u64 _efer;                  /* offset 0x400 + 0xD0 - cleanbit 5 */
     u64 res13[14];
-    u64 _cr4;                   /* offset 1024 + 0x148 - cleanbit 5 */
+    u64 _cr4;                   /* offset 0x400 + 0x148 - cleanbit 5 */
     u64 _cr3;                   /* cleanbit 5 */
     u64 _cr0;                   /* cleanbit 5 */
     u64 _dr7;                   /* cleanbit 6 */
@@ -508,7 +505,7 @@ struct arch_svm_struct {
     uint64_t guest_sysenter_cs;
     uint64_t guest_sysenter_esp;
     uint64_t guest_sysenter_eip;
-    
+
     /* AMD lightweight profiling MSR */
     uint64_t guest_lwp_cfg;      /* guest version */
     uint64_t cpu_lwp_cfg;        /* CPU version */
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by
#16 lore raw

Re: [PATCH 4/3] x86/svm: Drop svm_segment_register_t

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:10, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vmcb.c +0 -0
@@ -310,6 +310,15 @@ void __init setup_vmcb_dump(void)
     register_keyhandler('v', vmcb_dump, "dump AMD-V VMCBs", 1);

xen/arch/x86/hvm/svm/vmcb.c void __init setup_vmcb_dump(void) · line 320

Quoted 10 lines
xen/arch/x86/hvm/svm/vmcb.c +8 -0
 }
 
+static void __init __maybe_unused build_assertions(void)
+{
+    /* Check struct segment_register against the VMCB segment layout. */
+    BUILD_BUG_ON(sizeof(struct segment_register) != 16);
+    BUILD_BUG_ON(offsetof(struct segment_register, attr) != 2);
+    BUILD_BUG_ON(offsetof(struct segment_register, limit) != 4);
+    BUILD_BUG_ON(offsetof(struct segment_register, base) != 8);
+}

As said in reply to patch 1, I think we want to check both position and size here. With respective sizeof() checks added (and both for the so far missing sel field)

Jan

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

#17 lore raw

Re: [PATCH 4/3] x86/svm: Drop svm_segment_register_t

Quoted 18 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:10, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vmcb.c +0 -0
@@ -310,6 +310,15 @@ void __init setup_vmcb_dump(void)
     register_keyhandler('v', vmcb_dump, "dump AMD-V VMCBs", 1);

xen/arch/x86/hvm/svm/vmcb.c void __init setup_vmcb_dump(void) · line 320

Quoted 10 lines
xen/arch/x86/hvm/svm/vmcb.c +8 -0
 }
 
+static void __init __maybe_unused build_assertions(void)
+{
+    /* Check struct segment_register against the VMCB segment layout. */
+    BUILD_BUG_ON(sizeof(struct segment_register) != 16);
+    BUILD_BUG_ON(offsetof(struct segment_register, attr) != 2);
+    BUILD_BUG_ON(offsetof(struct segment_register, limit) != 4);
+    BUILD_BUG_ON(offsetof(struct segment_register, base) != 8);
+}

As said in reply to patch 1, I think we want to check both position and size here. With respective sizeof() checks added (and both for the so far missing sel field)

Quoted 1 line

Lets merge that part of the thread in here:

I don't see what the individual sizeof() checks gains us. The only field where a sizeof check would be useful is the attr union, but offsetof(following field) is the only way to infer the size, as the union is anonymous.

For the other fields, we'd be checking sizeof(uint{16,32,64}_t) being correct, which is overkill.

~Andrew

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

#18 lore raw

Re: [PATCH 4/3] x86/svm: Drop svm_segment_register_t

Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:39, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 03.07.17 at 15:10, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vmcb.c +0 -0
@@ -310,6 +310,15 @@ void __init setup_vmcb_dump(void)
     register_keyhandler('v', vmcb_dump, "dump AMD-V VMCBs", 1);

xen/arch/x86/hvm/svm/vmcb.c void __init setup_vmcb_dump(void) · line 320

Quoted 10 lines
xen/arch/x86/hvm/svm/vmcb.c +8 -0
 }
 
+static void __init __maybe_unused build_assertions(void)
+{
+    /* Check struct segment_register against the VMCB segment layout. */
+    BUILD_BUG_ON(sizeof(struct segment_register) != 16);
+    BUILD_BUG_ON(offsetof(struct segment_register, attr) != 2);
+    BUILD_BUG_ON(offsetof(struct segment_register, limit) != 4);
+    BUILD_BUG_ON(offsetof(struct segment_register, base) != 8);
+}

As said in reply to patch 1, I think we want to check both position and size here. With respective sizeof() checks added (and both for the so far missing sel field)

Quoted 1 line

Lets merge that part of the thread in here:

Quoted 7 lines

I don't see what the individual sizeof() checks gains us. The only field where a sizeof check would be useful is the attr union, but offsetof(following field) is the only way to infer the size, as the union is anonymous.

For the other fields, we'd be checking sizeof(uint{16,32,64}_t) being correct, which is overkill.

Well, my line of thinking is that if someone changed the structure definition, despite the comment not realizing it's tied to something hardware determines, they should see a build failure, no matter whether this affects field offsets, sizes, or both. In a way we really mean to check here that what is being used in the structure definition are uint{16,32,64}_t; ideally we'd even be able to check these are all unsigned types.

Jan

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