Skip to contentSkip to conversation

xc_cpuid_x86.c: Simplify masking conditions and remove redundant work

PATCH In review Zhuo Song 3 participants 6 messages Sep 10, 2014

v2 of 3 In review Latest is v3 →

lore
Share & tools
lore

Zhuo Song

author
#0 lore raw

PATCH v2 1/1 xc_cpuid_x86.c: Simplify masking conditions and remove redundant work

* Since there would not be 32-bit hypervisor, we do not need hypervisor_is_64bit() again.

* Remove xen_64bit from xc_cpuid_pv_policy().

* Move conditionals for LM/NX masking into architectural logic.

* Since RDTSCP could be used for both 64-bit and 32-bit architectures, we do not need the tying to 64-bit in intel_xc_cpuid_policy().

* vmx_cpuid_intercept() has covered SYSCALL masking when vmexit and original is_64bit or is_pae could not cover whether guest OS is really in long mode or not. Here to drop the conditionals and leave it to vmexit handler to do the real work.

---
 tools/libxc/xc_cpuid_x86.c | 40 ++++++++++++++++------------------------
 1 file changed, 16 insertions(+), 24 deletions(-)
tools/libxc/xc_cpuid_x86.c +16 -24
index 6b81641..a022202 100644
@@ -34,13 +34,6 @@
 #define DEF_MAX_INTELEXT  0x80000008u
 #define DEF_MAX_AMDEXT    0x8000001cu
 
-static int hypervisor_is_64bit(xc_interface *xch)
-{
-    xen_capabilities_info_t xen_caps = "";
-    return ((xc_version(xch, XENVER_capabilities, &xen_caps) == 0) &&
-            (strstr(xen_caps, "x86_64") != NULL));
-}
-
 static void cpuid(const unsigned int *input, unsigned int *regs)
 {
     unsigned int count = (input[1] == XEN_CPUID_INPUT_UNUSED) ? 0 : input[1];
@@ -95,13 +88,11 @@ static void amd_xc_cpuid_policy(
         break;
 
     case 0x80000001: {
-        int is_64bit = hypervisor_is_64bit(xch) && is_pae;
-
         if ( !is_pae )
             clear_bit(X86_FEATURE_PAE, regs[3]);
 
         /* Filter all other features according to a whitelist. */
-        regs[2] &= ((is_64bit ? bitmaskof(X86_FEATURE_LAHF_LM) : 0) |
+        regs[2] &= (bitmaskof(X86_FEATURE_LAHF_LM) |
                     bitmaskof(X86_FEATURE_CMP_LEGACY) |
                     (is_nestedhvm ? bitmaskof(X86_FEATURE_SVM) : 0) |
                     bitmaskof(X86_FEATURE_CR8_LEGACY) |
@@ -116,8 +107,8 @@ static void amd_xc_cpuid_policy(
                     bitmaskof(X86_FEATURE_TBM) |
                     bitmaskof(X86_FEATURE_DBEXT));
         regs[3] &= (0x0183f3ff | /* features shared with 0x00000001:EDX */
-                    (is_pae ? bitmaskof(X86_FEATURE_NX) : 0) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
+                    bitmaskof(X86_FEATURE_NX) |
+                    bitmaskof(X86_FEATURE_LM) |
                     bitmaskof(X86_FEATURE_SYSCALL) |
                     bitmaskof(X86_FEATURE_MP) |
                     bitmaskof(X86_FEATURE_MMXEXT) |
@@ -195,16 +186,14 @@ static void intel_xc_cpuid_policy(
         break;
 
     case 0x80000001: {
-        int is_64bit = hypervisor_is_64bit(xch) && is_pae;
-
         /* Only a few features are advertised in Intel's 0x80000001. */
-        regs[2] &= (is_64bit ? bitmaskof(X86_FEATURE_LAHF_LM) : 0) |
-                               bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
-                               bitmaskof(X86_FEATURE_ABM);
-        regs[3] &= ((is_pae ? bitmaskof(X86_FEATURE_NX) : 0) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_RDTSCP) : 0));
+        regs[2] &= (bitmaskof(X86_FEATURE_LAHF_LM) |
+                    bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
+                    bitmaskof(X86_FEATURE_ABM);
+        regs[3] &= (bitmaskof(X86_FEATURE_NX) |
+                    bitmaskof(X86_FEATURE_LM) |
+                    bitmaskof(X86_FEATURE_SYSCALL) |
+                    bitmaskof(X86_FEATURE_RDTSCP));
         break;
     }
 
@@ -391,7 +380,10 @@ static void xc_cpuid_hvm_policy(
         break;
 
     case 0x80000001:
-        if ( !is_pae ) {
+        if ( !is_pae )
+        {
+            clear_bit(X86_FEATURE_LAHF_LM, regs[2]);
+            clear_bit(X86_FEATURE_LM, regs[3]);
             clear_bit(X86_FEATURE_NX, regs[3]);
             clear_bit(X86_FEATURE_PSE36, regs[3]);
         }
@@ -442,7 +434,7 @@ static void xc_cpuid_pv_policy(
 {
     DECLARE_DOMCTL;
     unsigned int guest_width;
-    int guest_64bit, xen_64bit = hypervisor_is_64bit(xch);
+    int guest_64bit;
     char brand[13];
     uint64_t xfeature_mask;
 
@@ -474,7 +466,7 @@ static void xc_cpuid_pv_policy(
     switch ( input[0] )
     {
     case 0x00000001:
-        if ( !xen_64bit || strstr(brand, "AMD") )
+        if ( strstr(brand, "AMD") )
             clear_bit(X86_FEATURE_SEP, regs[3]);
         clear_bit(X86_FEATURE_DS, regs[3]);
         clear_bit(X86_FEATURE_ACC, regs[3]);
Signature · 1 line
1.8.5.2 (Apple Git-48)
#1 lore raw

On Wed, 2014-09-10 at 18:29 +0800, Zhuo Song wrote:

Please try and number your postings on a given subject in some sort of consistent way. Right now my queue folder contains from you:

Subject: [PATCH] xc_cpuid_x86.c: Remove the condition for masking NX in vendor specific functions
Date: Sat, 6 Sep 2014 01:55:22 +0800 (05/09/14 18:55:22)

Subject: [PATCH] xc_cpuid_x86.c: Remove the condition for masking NX in vendor specific functions
Date: Sat, 6 Sep 2014 02:01:21 +0800 (05/09/14 19:01:21)

Subject: [PATCH] xc_cpuid_x86.c: Simplify masking conditions and remove redundant work
Date: Mon, 8 Sep 2014 20:02:27 +0800 (08/09/14 13:02:27)

Subject: [PATCH] xc_cpuid_x86.c: No need to mask NX twice
Date: Fri, 5 Sep 2014 18:11:33 +0800 (05/09/14 11:11:33)

Subject: [PATCH v2 resend 2/2] xc_cpuid_x86.c: Remove limit for RDTSCP
Date: Tue, 9 Sep 2014 15:55:49 +0800 (09/09/14 08:55:49)

Subject: [PATCH v2 1/2] xc_cpuid_x86.c: Simplify masking conditions and remove redundant work
Date: Tue, 9 Sep 2014 12:31:51 +0800 (09/09/14 05:31:51)

Subject: [PATCH v3 1/3] xc_cpuid_x86.c: Simplify masking conditions and remove redundant work
Date: Wed, 10 Sep 2014 10:29:24 +0800 (10/09/14 03:29:24)

Subject: [PATCH v2] xc_cpuid_x86.c: Simplify masking conditions and remove redundant work
Date: Wed, 10 Sep 2014 18:29:00 +0800 (10/09/14 11:29:00)

Which I think are all different iterations of the same thing, but it is very hard for me to figure out what is going on with all of these patches flying around with different versions and titles etc, especially when the version numbers appear to be going backwards in some postings.

In future when you resend a patch then please increment the version, even if you have given it a different subject/title. Don't start again from v1, and certainly never decrement it as you appear to have done in the latest posting. If necessary use a 0/N series introduction with a consistent title to explain the general purpose of the series (which should hopefully be immune to renaming as the patch is reviewed).

And please use the intra-patch changelog (which should be after the "---" explaining what changed in this posting since last time, see [0]) to indicate if something is a renamed version of some previous patch, as well as what has changed.

So, to get back on track please can you let me know which of the above is the latest thing which I should actually pay attention to.

Ian.

[0]
http://wiki.xen.org/wiki/Submitting_Xen_Patches#Review.2C_Rinse_.26_Repeat

author
#2 lore raw
Quoted 53 lines · Ian Campbell

On Wed, 2014-09-10 at 18:29 +0800, Zhuo Song wrote:

Please try and number your postings on a given subject in some sort of consistent way. Right now my queue folder contains from you:

Subject: [PATCH] xc_cpuid_x86.c: Remove the condition for masking
NX in vendor specific functions
Date: Sat, 6 Sep 2014 01:55:22 +0800 (05/09/14 18:55:22)

Subject: [PATCH] xc_cpuid_x86.c: Remove the condition for masking
NX in vendor specific functions
Date: Sat, 6 Sep 2014 02:01:21 +0800 (05/09/14 19:01:21)

Subject: [PATCH] xc_cpuid_x86.c: Simplify masking conditions and
remove redundant work
Date: Mon, 8 Sep 2014 20:02:27 +0800 (08/09/14 13:02:27)

Subject:        [PATCH] xc_cpuid_x86.c: No need to mask NX twice
Date:           Fri, 5 Sep 2014 18:11:33 +0800 (05/09/14 11:11:33)

Subject: [PATCH v2 resend 2/2] xc_cpuid_x86.c: Remove limit for
RDTSCP
Date: Tue, 9 Sep 2014 15:55:49 +0800 (09/09/14 08:55:49)

Subject: [PATCH v2 1/2] xc_cpuid_x86.c: Simplify masking conditions
and remove redundant work
Date: Tue, 9 Sep 2014 12:31:51 +0800 (09/09/14 05:31:51)

Subject: [PATCH v3 1/3] xc_cpuid_x86.c: Simplify masking conditions
and remove redundant work
Date: Wed, 10 Sep 2014 10:29:24 +0800 (10/09/14 03:29:24)

Subject: [PATCH v2] xc_cpuid_x86.c: Simplify masking conditions and
remove redundant work
Date: Wed, 10 Sep 2014 18:29:00 +0800 (10/09/14 11:29:00)

Which I think are all different iterations of the same thing, but it is very hard for me to figure out what is going on with all of these patches flying around with different versions and titles etc, especially when the version numbers appear to be going backwards in some postings.

In future when you resend a patch then please increment the version, even if you have given it a different subject/title. Don't start again from v1, and certainly never decrement it as you appear to have done in the latest posting. If necessary use a 0/N series introduction with a consistent title to explain the general purpose of the series (which should hopefully be immune to renaming as the patch is reviewed).

And please use the intra-patch changelog (which should be after the "---" explaining what changed in this posting since last time, see [0]) to indicate if something is a renamed version of some previous patch, as well as what has changed.

Quoted 9 lines

So, to get back on track please can you let me know which of the above is the latest thing which I should actually pay attention to.

Ian.

[0]
http://wiki.xen.org/wiki/Submitting_Xen_Patches#Review.2C_Rinse_.26_Repeat

Hi, Ian

I am sorry for confusing. The latest one is my all-in-one version:

Subject: [PATCH v2] xc_cpuid_x86.c: Simplify masking conditions and
remove redundant work
Date: Wed, 10 Sep 2014 18:29:00 +0800 (10/09/14 11:29:00)

Thank you for your suggestions and the rules. I left the community for a couple of years to work for some more closed projects.
And I am glad to be here again. I will follow the advices you mentioned.

Thanks again.
Zhuo

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

#3 lore raw
Quoted 5 lines · On Wed, 2014-09-10 at 21:02 +0800, z wrote:

I am sorry for confusing. The latest one is my all-in-one version:

Subject: [PATCH v2] xc_cpuid_x86.c: Simplify masking conditions
and remove redundant work
Date: Wed, 10 Sep 2014 18:29:00 +0800 (10/09/14 11:29:00)

Thanks, since Jan has already indicated he's happy with it I went to apply it, however a build test resulted in:
xc_cpuid_x86.c: In function 'intel_xc_cpuid_policy':
xc_cpuid_x86.c:192:32: error: expected ')' before ';' token xc_cpuid_x86.c:198:5: error: expected ';' before '}' token

I've folded in this change to fix it:

        @@ -189,7 +189,7 @@ static void intel_xc_cpuid_policy(
                 /* Only a few features are advertised in Intel's 0x80000001. */
                 regs[2] &= (bitmaskof(X86_FEATURE_LAHF_LM) |
                             bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
        -                    bitmaskof(X86_FEATURE_ABM);
        +                    bitmaskof(X86_FEATURE_ABM));
                 regs[3] &= (bitmaskof(X86_FEATURE_NX) |
                             bitmaskof(X86_FEATURE_LM) |
                             bitmaskof(X86_FEATURE_SYSCALL) |

Please try and remember to compile test your incremental changes next time ;-)

Quoted 5 lines

Thank you for your suggestions and the rules. I left the community for a couple of years to work for some more closed projects.

And I am glad to be here again. I will follow the advices you mentioned.

Welcome back!

Ian.

author
#4 lore raw
Quoted 27 lines · Ian Campbell
Quoted 5 lines · On Wed, 2014-09-10 at 21:02 +0800, z wrote:

I am sorry for confusing. The latest one is my all-in-one version:

Subject: [PATCH v2] xc_cpuid_x86.c: Simplify masking conditions
and remove redundant work
Date: Wed, 10 Sep 2014 18:29:00 +0800 (10/09/14 11:29:00)

Thanks, since Jan has already indicated he's happy with it I went to apply it, however a build test resulted in:
xc_cpuid_x86.c: In function 'intel_xc_cpuid_policy':
xc_cpuid_x86.c:192:32: error: expected ')' before ';' token xc_cpuid_x86.c:198:5: error: expected ';' before '}' token

I've folded in this change to fix it:

        @@ -189,7 +189,7 @@ static void intel_xc_cpuid_policy(
                 /* Only a few features are advertised in Intel's
0x80000001. */
                 regs[2] &= (bitmaskof(X86_FEATURE_LAHF_LM) |
                             bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
        -                    bitmaskof(X86_FEATURE_ABM);
        +                    bitmaskof(X86_FEATURE_ABM));
                 regs[3] &= (bitmaskof(X86_FEATURE_NX) |
                             bitmaskof(X86_FEATURE_LM) |
                             bitmaskof(X86_FEATURE_SYSCALL) |
Quoted 3 lines

Please try and remember to compile test your incremental changes next time ;-)

Ohh, I am sorry. Thank you for your nice work. Also thanks for Jan's suggestions.
I got a couple of mistakes for this submission. And thanks for your patience.

It would not be expected to happen again, to me.

Zhuo

Quoted 4 lines
Quoted 4 lines

Thank you for your suggestions and the rules. I left the community for a couple of years to work for some more closed projects.

Quoted 11 lines
Quoted 2 lines

And I am glad to be here again. I will follow the advices you mentioned.

Welcome back!

Ian.

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

Reviewed-by on v2 1/1
#5 lore raw
In reply to Zhuo Song
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 10.09.14 at 12:29, <[email protected]> wrote:

* Since there would not be 32-bit hypervisor, we do not need hypervisor_is_64bit() again.

* Remove xen_64bit from xc_cpuid_pv_policy().

* Move conditionals for LM/NX masking into architectural logic.

* Since RDTSCP could be used for both 64-bit and 32-bit architectures, we do not need the tying to 64-bit in intel_xc_cpuid_policy().

Quoted 6 lines

* vmx_cpuid_intercept() has covered SYSCALL masking when vmexit and original is_64bit or is_pae could not cover whether guest OS is really in long mode or not. Here to drop the conditionals and leave it to vmexit handler to do the real work.

Quoted 105 lines
---
 tools/libxc/xc_cpuid_x86.c | 40 ++++++++++++++++------------------------
 1 file changed, 16 insertions(+), 24 deletions(-)
tools/libxc/xc_cpuid_x86.c +0 -7
index 6b81641..a022202 100644
@@ -34,13 +34,6 @@
 #define DEF_MAX_INTELEXT  0x80000008u
 #define DEF_MAX_AMDEXT    0x8000001cu
 
-static int hypervisor_is_64bit(xc_interface *xch)
-{
-    xen_capabilities_info_t xen_caps = "";
-    return ((xc_version(xch, XENVER_capabilities, &xen_caps) == 0) &&
-            (strstr(xen_caps, "x86_64") != NULL));
-}
-
 static void cpuid(const unsigned int *input, unsigned int *regs)
 {
     unsigned int count = (input[1] == XEN_CPUID_INPUT_UNUSED) ? 0 : 

input[1];

tools/libxc/xc_cpuid_x86.c +16 -17
@@ -95,13 +88,11 @@ static void amd_xc_cpuid_policy(
         break;
 
     case 0x80000001: {
-        int is_64bit = hypervisor_is_64bit(xch) && is_pae;
-
         if ( !is_pae )
             clear_bit(X86_FEATURE_PAE, regs[3]);
 
         /* Filter all other features according to a whitelist. */
-        regs[2] &= ((is_64bit ? bitmaskof(X86_FEATURE_LAHF_LM) : 0) |
+        regs[2] &= (bitmaskof(X86_FEATURE_LAHF_LM) |
                     bitmaskof(X86_FEATURE_CMP_LEGACY) |
                     (is_nestedhvm ? bitmaskof(X86_FEATURE_SVM) : 0) |
                     bitmaskof(X86_FEATURE_CR8_LEGACY) |
@@ -116,8 +107,8 @@ static void amd_xc_cpuid_policy(
                     bitmaskof(X86_FEATURE_TBM) |
                     bitmaskof(X86_FEATURE_DBEXT));
         regs[3] &= (0x0183f3ff | /* features shared with 0x00000001:EDX */
-                    (is_pae ? bitmaskof(X86_FEATURE_NX) : 0) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
+                    bitmaskof(X86_FEATURE_NX) |
+                    bitmaskof(X86_FEATURE_LM) |
                     bitmaskof(X86_FEATURE_SYSCALL) |
                     bitmaskof(X86_FEATURE_MP) |
                     bitmaskof(X86_FEATURE_MMXEXT) |
@@ -195,16 +186,14 @@ static void intel_xc_cpuid_policy(
         break;
 
     case 0x80000001: {
-        int is_64bit = hypervisor_is_64bit(xch) && is_pae;
-
         /* Only a few features are advertised in Intel's 0x80000001. */
-        regs[2] &= (is_64bit ? bitmaskof(X86_FEATURE_LAHF_LM) : 0) |
-                               bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
-                               bitmaskof(X86_FEATURE_ABM);
-        regs[3] &= ((is_pae ? bitmaskof(X86_FEATURE_NX) : 0) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_RDTSCP) : 0));
+        regs[2] &= (bitmaskof(X86_FEATURE_LAHF_LM) |
+                    bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
+                    bitmaskof(X86_FEATURE_ABM);
+        regs[3] &= (bitmaskof(X86_FEATURE_NX) |
+                    bitmaskof(X86_FEATURE_LM) |
+                    bitmaskof(X86_FEATURE_SYSCALL) |
+                    bitmaskof(X86_FEATURE_RDTSCP));
         break;
     }
 
@@ -391,7 +380,10 @@ static void xc_cpuid_hvm_policy(
         break;
 
     case 0x80000001:
-        if ( !is_pae ) {
+        if ( !is_pae )
+        {
+            clear_bit(X86_FEATURE_LAHF_LM, regs[2]);
+            clear_bit(X86_FEATURE_LM, regs[3]);
             clear_bit(X86_FEATURE_NX, regs[3]);
             clear_bit(X86_FEATURE_PSE36, regs[3]);
         }
@@ -442,7 +434,7 @@ static void xc_cpuid_pv_policy(
 {
     DECLARE_DOMCTL;
     unsigned int guest_width;
-    int guest_64bit, xen_64bit = hypervisor_is_64bit(xch);
+    int guest_64bit;
     char brand[13];
     uint64_t xfeature_mask;
 
@@ -474,7 +466,7 @@ static void xc_cpuid_pv_policy(
     switch ( input[0] )
     {
     case 0x00000001:
-        if ( !xen_64bit || strstr(brand, "AMD") )
+        if ( strstr(brand, "AMD") )
             clear_bit(X86_FEATURE_SEP, regs[3]);
         clear_bit(X86_FEATURE_DS, regs[3]);
         clear_bit(X86_FEATURE_ACC, regs[3]);
Signature · 1 line
1.8.5.2 (Apple Git-48)