Skip to contentSkip to conversation

xc_cpuid_x86.c: No need to mask NX twice

PATCH In review Zhuo Song 2 participants 16 messages Sep 5, 2014 – Sep 9, 2014

lore
Share & tools
lore

Zhuo Song

author
#0 lore raw

PATCH 1/1 xc_cpuid_x86.c: No need to mask NX twice

Since amd_xc_cpuid_policy or intel_xc_cpuid_policy will do it later as well, we may drop an extra action.

---
 tools/libxc/xc_cpuid_x86.c | 1 -
 1 file changed, 1 deletion(-)
tools/libxc/xc_cpuid_x86.c +0 -1
index 6b81641..61af3e6 100644
@@ -392,7 +392,6 @@ static void xc_cpuid_hvm_policy(
 
     case 0x80000001:
         if ( !is_pae ) {
-            clear_bit(X86_FEATURE_NX, regs[3]);
             clear_bit(X86_FEATURE_PSE36, regs[3]);
         }
         break;
Signature · 1 line
1.8.5.2 (Apple Git-48)
#1 lore raw
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 05.09.14 at 12:11, <[email protected]> wrote:

Since amd_xc_cpuid_policy or intel_xc_cpuid_policy will do it later as well, we may drop an extra action.

But then I think it would be better to keep the generic one and drop the vendor specific ones.

Jan

Quoted 25 lines
---
 tools/libxc/xc_cpuid_x86.c | 1 -
 1 file changed, 1 deletion(-)
tools/libxc/xc_cpuid_x86.c +0 -1
index 6b81641..61af3e6 100644
@@ -392,7 +392,6 @@ static void xc_cpuid_hvm_policy(
 
     case 0x80000001:
         if ( !is_pae ) {
-            clear_bit(X86_FEATURE_NX, regs[3]);
             clear_bit(X86_FEATURE_PSE36, regs[3]);
         }
         break;
Signature · 7 lines
1.8.5.2 (Apple Git-48)


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

Hi, Jan

Generally, I agree with you. But it seems that we may not simply remove the ones from the 2 vendor specific functions and keep the generic one, because the bit could be still cleared eventually when is_pae is true. And I find PSE36 will be cleared permanently no matter is_pae is true or not. Actually the work below has been totally overlapped:

        if ( !is_pae )

{

            clear_bit(X86_FEATURE_NX,

regs[3]);

            clear_bit(X86_FEATURE_PSE36,

regs[3]);

        }

How can we just "passthrough to cpu vendor specific functions" like leaf 0x80000000? like this:

    case 0x80000000:
    case

0x80000001:

        /* Passthrough to cpu vendor specific functions

*/

        break;

Correct me if I missed something.

Zhuo

Quoted 30 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 05.09.14 at 12:11, <[email protected]> wrote:

Since amd_xc_cpuid_policy or intel_xc_cpuid_policy will do it later as well, we may drop an extra action.

But then I think it would be better to keep the generic one and drop the vendor specific ones.

Jan

Quoted 21 lines
---
 tools/libxc/xc_cpuid_x86.c | 1 -
 1 file changed, 1 deletion(-)
tools/libxc/xc_cpuid_x86.c +0 -2
index 6b81641..61af3e6 100644
@@ -392,7 +392,6 @@ static void xc_cpuid_hvm_policy(
 
     case 0x80000001:
         if ( !is_pae ) {
-            clear_bit(X86_FEATURE_NX, regs[3]);
             clear_bit(X86_FEATURE_PSE36, regs[3]);
         }
         break;
--

1.8.5.2 (Apple Git-48)

Quoted 8 lines
Quoted 4 lines

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

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

#3 lore raw
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 05.09.14 at 15:54, <[email protected]> wrote:

Hi, Jan

Generally, I agree with you. But it seems that we may not simply remove the ones from the 2 vendor specific functions and keep the generic one, because the bit could be still cleared eventually when is_pae is true. And I find PSE36 will be cleared permanently no matter is_pae is true or not. Actually the work below has been totally overlapped:

        if ( !is_pae )

{

            clear_bit(X86_FEATURE_NX,

regs[3]);

            clear_bit(X86_FEATURE_PSE36,

regs[3]);

        }

How can we just "passthrough to cpu vendor specific functions" like leaf 0x80000000? like this:

    case 0x80000000:
    case

0x80000001:

Quoted 7 lines
        /* Passthrough to cpu vendor specific functions

*/

        break;

Correct me if I missed something.

I'm sorry, but I don't think I understand what you're trying to say or you're asking.

Jan

author
#4 lore raw

Hi, Jan

I am sorry for making you confused.

What I mean is there seems to be some redundant work. For example, to leaf 0x80000001, the generic work (masking NX and PSE36) has been overwritten by the the vendor's functions (amd_xc_cpuid_policy and intel_xc_cpuid_policy) , why couldn't we just drop them and leave the work to the vendor? Of cause, another way is just like you said, keeping the generic ones, and changing the logic in the vendor-based implementation.

What I want to do is to simplify this if possible. :)

Zhuo

Quoted 37 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.09.14 at 15:54, <[email protected]> wrote:

Hi, Jan

Generally, I agree with you. But it seems that we may not simply remove

the

Quoted 1 line

ones from the 2 vendor specific functions and keep the generic one,

because

Quoted 2 lines

the bit could be still cleared eventually when is_pae is true. And I find PSE36 will be cleared permanently no matter is_pae is true or not.

Actually

Quoted 27 lines

the work below has been totally overlapped:

        if ( !is_pae )

{

            clear_bit(X86_FEATURE_NX,

regs[3]);

            clear_bit(X86_FEATURE_PSE36,

regs[3]);

        }

How can we just "passthrough to cpu vendor specific functions" like leaf 0x80000000? like this:

    case 0x80000000:
    case

0x80000001:

        /* Passthrough to cpu vendor specific functions

*/

        break;
Quoted 8 lines
Quoted 1 line

Correct me if I missed something.

I'm sorry, but I don't think I understand what you're trying to say or you're asking.

Jan

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

#5 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 05.09.14 at 17:45, <[email protected]> wrote:

Hi, Jan

I am sorry for making you confused.

Quoted 8 lines

What I mean is there seems to be some redundant work. For example, to leaf 0x80000001, the generic work (masking NX and PSE36) has been overwritten by the the vendor's functions (amd_xc_cpuid_policy and intel_xc_cpuid_policy) , why couldn't we just drop them and leave the work to the vendor? Of cause, another way is just like you said, keeping the generic ones, and changing the logic in the vendor-based implementation.

What I want to do is to simplify this if possible. :)

Right, yet conceptually anything that is defined by the architecture would better be done in the generic routine. Anything that is truly vendor specific should be done in the vendor ones. And the dependency of NX on PAE is (nowadays) an architectural one.

Jan

author
#6 lore raw

Got it. Thanks, Jan.
If so, I think we could remove the condition for masking NX in both vendor specific functions, since the architectural logic has help cover it and the
judgement is unnecessary. For example:

tools/libxc/xc_cpuid_x86.c +2 -2
index 61af3e6..6bd89b0 100644
@@ -116,7 +116,7 @@ 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) |
+                    bitmaskof(X86_FEATURE_NX) |
                     (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
                     bitmaskof(X86_FEATURE_SYSCALL) |
                     bitmaskof(X86_FEATURE_MP) |
@@ -201,7 +201,7 @@ static void intel_xc_cpuid_policy(
         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) |
+        regs[3] &= (bitmaskof(X86_FEATURE_NX) |
                     (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_RDTSCP) : 0));

Zhuo

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

Hi, Jan

I am sorry for making you confused.

What I mean is there seems to be some redundant work. For example, to

leaf

Quoted 1 line

0x80000001, the generic work (masking NX and PSE36) has been overwritten

by

Quoted 1 line

the the vendor's functions (amd_xc_cpuid_policy and

intel_xc_cpuid_policy)

Quoted 4 lines

, why couldn't we just drop them and leave the work to the vendor? Of cause, another way is just like you said, keeping the generic ones, and changing the logic in the vendor-based implementation.

Quoted 10 lines
Quoted 1 line

What I want to do is to simplify this if possible. :)

Right, yet conceptually anything that is defined by the architecture would better be done in the generic routine. Anything that is truly vendor specific should be done in the vendor ones. And the dependency of NX on PAE is (nowadays) an architectural one.

Jan

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

#7 lore raw
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 05.09.14 at 18:35, <[email protected]> wrote:

Got it. Thanks, Jan.
If so, I think we could remove the condition for masking NX in both vendor specific functions, since the architectural logic has help cover it and the
judgement is unnecessary. For example:

tools/libxc/xc_cpuid_x86.c +1 -1
index 61af3e6..6bd89b0 100644
@@ -116,7 +116,7 @@ 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) |
+                    bitmaskof(X86_FEATURE_NX) |
                     (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
                     bitmaskof(X86_FEATURE_SYSCALL) |
                     bitmaskof(X86_FEATURE_MP) |

tools/libxc/xc_cpuid_x86.c static void intel_xc_cpuid_policy( · line 207

Quoted 8 lines
tools/libxc/xc_cpuid_x86.c +1 -1
@@ -201,7 +201,7 @@ static void intel_xc_cpuid_policy(
         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) |
+        regs[3] &= (bitmaskof(X86_FEATURE_NX) |
                     (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_RDTSCP) : 0));

Right, and I think you could go further and move at least the LM check into vendor-independent code too.

Looking at the context above I also wonder whether tying RDTSCP to 64-bit guests is really correct in (at least) the Intel case.

Jan

author
#8 lore raw
Quoted 26 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 05.09.14 at 18:35, <[email protected]> wrote:

Got it. Thanks, Jan.
If so, I think we could remove the condition for masking NX in both

vendor

Quoted 1 line

specific functions, since the architectural logic has help cover it and

the

tools/libxc/xc_cpuid_x86.c static void amd_xc_cpuid_policy( · line 118

Quoted 10 lines

judgement is unnecessary. For example:

tools/libxc/xc_cpuid_x86.c +0 -0
index 61af3e6..6bd89b0 100644
@@ -116,7 +116,7 @@ static void amd_xc_cpuid_policy(
                     bitmaskof(X86_FEATURE_TBM) |
                     bitmaskof(X86_FEATURE_DBEXT));
         regs[3] &= (0x0183f3ff | /* features shared with 0x00000001:EDX

*/

Quoted 9 lines
tools/libxc/xc_cpuid_x86.c +1 -1
-                    (is_pae ? bitmaskof(X86_FEATURE_NX) : 0) |
+                    bitmaskof(X86_FEATURE_NX) |
                     (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
                     bitmaskof(X86_FEATURE_SYSCALL) |
                     bitmaskof(X86_FEATURE_MP) |
@@ -201,7 +201,7 @@ static void intel_xc_cpuid_policy(
         regs[2] &= (is_64bit ? bitmaskof(X86_FEATURE_LAHF_LM) : 0) |
                                bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
                                bitmaskof(X86_FEATURE_ABM);
Quoted 9 lines

tools/libxc/xc_cpuid_x86.c static void intel_xc_cpuid_policy( · line 207

Quoted 5 lines
tools/libxc/xc_cpuid_x86.c +1 -1
-        regs[3] &= ((is_pae ? bitmaskof(X86_FEATURE_NX) : 0) |
+        regs[3] &= (bitmaskof(X86_FEATURE_NX) |
                     (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_RDTSCP) : 0));

Right, and I think you could go further and move at least the LM check into vendor-independent code too.

Hi, Jan

Thanks. Yes, and I did some changes as below.
Please correct me if I did some improper ones.

tools/libxc/xc_cpuid_x86.c +23 -17
index 9add109..7d79dc2 100644
@@ -80,7 +80,7 @@ static void xc_cpuid_brand_get(char *str)
 static void amd_xc_cpuid_policy(
     xc_interface *xch, domid_t domid,
     const unsigned int *input, unsigned int *regs,
-    int is_pae, int is_nestedhvm)
+    int is_64bit, int is_pae, int is_nestedhvm)
 {
     switch ( input[0] )
     {
@@ -95,13 +95,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) |
@@ -117,7 +115,7 @@ static void amd_xc_cpuid_policy(
                     bitmaskof(X86_FEATURE_DBEXT));
         regs[3] &= (0x0183f3ff | /* features shared with 0x00000001:EDX */
                     bitmaskof(X86_FEATURE_NX) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
+                    bitmaskof(X86_FEATURE_LM) |
                     bitmaskof(X86_FEATURE_SYSCALL) |
                     bitmaskof(X86_FEATURE_MP) |
                     bitmaskof(X86_FEATURE_MMXEXT) |
@@ -169,7 +167,7 @@ static void amd_xc_cpuid_policy(
 static void intel_xc_cpuid_policy(
     xc_interface *xch, domid_t domid,
     const unsigned int *input, unsigned int *regs,
-    int is_pae, int is_nestedhvm)
+    int is_64bit, int is_pae, int is_nestedhvm)
 {
     switch ( input[0] )
     {
@@ -195,14 +193,12 @@ 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[2] &= (bitmaskof(X86_FEATURE_LAHF_LM) |
+                    bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
+                    bitmaskof(X86_FEATURE_ABM);
         regs[3] &= (bitmaskof(X86_FEATURE_NX) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
+                    bitmaskof(X86_FEATURE_LM) |
                     (is_64bit ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_RDTSCP) : 0));
         break;
@@ -278,12 +274,14 @@ static void xc_cpuid_hvm_policy(
     DECLARE_DOMCTL;
     char brand[13];
     uint64_t val;
-    int is_pae, is_nestedhvm;
+    int is_64bit, is_pae, is_nestedhvm;
     uint64_t xfeature_mask;
 
     xc_hvm_param_get(xch, domid, HVM_PARAM_PAE_ENABLED, &val);
     is_pae = !!val;
-
+
+    is_64bit = hypervisor_is_64bit(xch) && is_pae;
+
     /* Detecting Xen's atitude towards XSAVE */
     memset(&domctl, 0, sizeof(domctl));
     domctl.cmd = XEN_DOMCTL_getvcpuextstate;
@@ -391,10 +389,18 @@ static void xc_cpuid_hvm_policy(
         break;
 
     case 0x80000001:
-        if ( !is_pae ) {
+        if ( !is_64bit ) {
+            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]);
+        } else if ( !is_pae ) {
             clear_bit(X86_FEATURE_NX, regs[3]);
             clear_bit(X86_FEATURE_PSE36, regs[3]);
+        } else {
+            /* Do nothing for 32-bit guest */
         }
+
         break;
 
     case 0x80000007:
@@ -430,10 +436,11 @@ static void xc_cpuid_hvm_policy(
 
     xc_cpuid_brand_get(brand);
     if ( strstr(brand, "AMD") )
-        amd_xc_cpuid_policy(xch, domid, input, regs, is_pae, is_nestedhvm);
+        amd_xc_cpuid_policy(xch, domid, input, regs,
+                            is_64bit, is_pae, is_nestedhvm);
     else
-        intel_xc_cpuid_policy(xch, domid, input, regs, is_pae,

is_nestedhvm);
-
+ intel_xc_cpuid_policy(xch, domid, input, regs,
+ is_64bit, is_pae, is_nestedhvm);

Quoted 4 lines

Looking at the context above I also wonder whether tying RDTSCP to 64-bit guests is really correct in (at least) the Intel case.

I am not so sure for now, but I will check it later as well:)

Quoted 4 lines

Jan

Zhuo

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

#9 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 08.09.14 at 10:48, <[email protected]> wrote:

Things look fine from a general pov, but

tools/libxc/xc_cpuid_x86.c static void xc_cpuid_hvm_policy( · line 283

Quoted 13 lines
tools/libxc/xc_cpuid_x86.c +3 -2
@@ -278,12 +274,14 @@ static void xc_cpuid_hvm_policy(
     DECLARE_DOMCTL;
     char brand[13];
     uint64_t val;
-    int is_pae, is_nestedhvm;
+    int is_64bit, is_pae, is_nestedhvm;
     uint64_t xfeature_mask;
 
     xc_hvm_param_get(xch, domid, HVM_PARAM_PAE_ENABLED, &val);
     is_pae = !!val;
-
+
+    is_64bit = hypervisor_is_64bit(xch) && is_pae;

... with this using hypervisor_is_64bit() and there not being a 32-bit hypervisor anymore, there's clearly room for more cleanup (and in particular no need to pass around an "is_64bit" variable that's always going to be set to true).

Quoted 6 lines
tools/libxc/xc_cpuid_x86.c +1 -1
@@ -391,10 +389,18 @@ static void xc_cpuid_hvm_policy(
         break;
 
     case 0x80000001:
-        if ( !is_pae ) {
+        if ( !is_64bit ) {

tools/libxc/xc_cpuid_x86.c static void xc_cpuid_hvm_policy( · line 402

Quoted 10 lines
tools/libxc/xc_cpuid_x86.c +7 -0
+            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]);
+        } else if ( !is_pae ) {
             clear_bit(X86_FEATURE_NX, regs[3]);
             clear_bit(X86_FEATURE_PSE36, regs[3]);
+        } else {
+            /* Do nothing for 32-bit guest */
         }

The ordering of the if/else-if above seems wrong to me, but this would become moot anyway if "is_64bit" got dropped.

Jan

author
#10 lore raw
Quoted 12 lines · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 08.09.14 at 10:48, <[email protected]> wrote:

Things look fine from a general pov, but

Quoted 8 lines
(quoted diff) +1 -1
@@ -278,12 +274,14 @@ static void xc_cpuid_hvm_policy(
     DECLARE_DOMCTL;
     char brand[13];
     uint64_t val;
-    int is_pae, is_nestedhvm;
+    int is_64bit, is_pae, is_nestedhvm;
     uint64_t xfeature_mask;
 
Quoted 11 lines

Review static void xc_cpuid_hvm_policy( · line 283

Quoted 5 lines
(quoted diff) +2 -1
     xc_hvm_param_get(xch, domid, HVM_PARAM_PAE_ENABLED, &val);
     is_pae = !!val;
-
+
+    is_64bit = hypervisor_is_64bit(xch) && is_pae;

... with this using hypervisor_is_64bit() and there not being a 32-bit hypervisor anymore, there's clearly room for more cleanup (and in particular no need to pass around an "is_64bit" variable that's always going to be set to true).

Do your mean hypervisor_is_64bit() will always return true? Then it means that is_64bit will only depend on is_pae here, so we could simply use is_pae instead of is_64bit in both vendor specific functions. If so, I think xen_64bit in xc_cpuid_pv_policy could also be dropped and function hypervisor_is_64bit()
is also redundant now. Right?

Maybe something like this?

tools/libxc/xc_cpuid_x86.c +0 -7
index 6b81641..710fd61 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) |
+                    (is_pae ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
+                    (is_pae ? bitmaskof(X86_FEATURE_RDTSCP) : 0));
         break;
     }

@@ -392,6 +381,8 @@ static void xc_cpuid_hvm_policy(

     case 0x80000001:
         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 +433,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 +465,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]);

Zhuo

Quoted 20 lines
Quoted 6 lines
(quoted diff) +1 -1
@@ -391,10 +389,18 @@ static void xc_cpuid_hvm_policy(
         break;
 
     case 0x80000001:
-        if ( !is_pae ) {
+        if ( !is_64bit ) {

Review static void xc_cpuid_hvm_policy( · line 402

Quoted 10 lines
(quoted diff) +7 -0
+            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]);
+        } else if ( !is_pae ) {
             clear_bit(X86_FEATURE_NX, regs[3]);
             clear_bit(X86_FEATURE_PSE36, regs[3]);
+        } else {
+            /* Do nothing for 32-bit guest */
         }

The ordering of the if/else-if above seems wrong to me, but this would become moot anyway if "is_64bit" got dropped.

Quoted 3 lines

Jan

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

#11 lore raw
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 08.09.14 at 11:56, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 08.09.14 at 10:48, <[email protected]> wrote:

Things look fine from a general pov, but

Quoted 8 lines
(quoted diff) +1 -1
@@ -278,12 +274,14 @@ static void xc_cpuid_hvm_policy(
     DECLARE_DOMCTL;
     char brand[13];
     uint64_t val;
-    int is_pae, is_nestedhvm;
+    int is_64bit, is_pae, is_nestedhvm;
     uint64_t xfeature_mask;
 
Quoted 11 lines

Review static void xc_cpuid_hvm_policy( · line 283

Quoted 5 lines
(quoted diff) +2 -1
     xc_hvm_param_get(xch, domid, HVM_PARAM_PAE_ENABLED, &val);
     is_pae = !!val;
-
+
+    is_64bit = hypervisor_is_64bit(xch) && is_pae;

... with this using hypervisor_is_64bit() and there not being a 32-bit hypervisor anymore, there's clearly room for more cleanup (and in particular no need to pass around an "is_64bit" variable that's always going to be set to true).

Quoted 6 lines

Do your mean hypervisor_is_64bit() will always return true? Then it means that is_64bit will only depend on is_pae here, so we could simply use is_pae instead of is_64bit in both vendor specific functions. If so, I think xen_64bit in xc_cpuid_pv_policy could also be dropped and function hypervisor_is_64bit()
is also redundant now. Right?

Right.

Quoted 1 line

Maybe something like this?

Yes.

Thanks, Jan

author
#12 lore raw
Quoted 34 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 08.09.14 at 11:56, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 08.09.14 at 10:48, <[email protected]> wrote:

Things look fine from a general pov, but

Quoted 8 lines
(quoted diff) +1 -1
@@ -278,12 +274,14 @@ static void xc_cpuid_hvm_policy(
     DECLARE_DOMCTL;
     char brand[13];
     uint64_t val;
-    int is_pae, is_nestedhvm;
+    int is_64bit, is_pae, is_nestedhvm;
     uint64_t xfeature_mask;
 
Quoted 11 lines

Review static void xc_cpuid_hvm_policy( · line 283

Quoted 5 lines
(quoted diff) +2 -1
     xc_hvm_param_get(xch, domid, HVM_PARAM_PAE_ENABLED, &val);
     is_pae = !!val;
-
+
+    is_64bit = hypervisor_is_64bit(xch) && is_pae;

... with this using hypervisor_is_64bit() and there not being a 32-bit hypervisor anymore, there's clearly room for more cleanup (and in particular no need to pass around an "is_64bit" variable that's always going to be set to true).

Quoted 6 lines

Do your mean hypervisor_is_64bit() will always return true? Then it means that is_64bit will only depend on is_pae here, so we could simply use is_pae instead of is_64bit in both vendor specific functions. If so, I think xen_64bit in xc_cpuid_pv_policy could also be dropped and function hypervisor_is_64bit()
is also redundant now. Right?

Quoted 8 lines

Right.

Quoted 1 line

Maybe something like this?

Yes.

Thanks, Jan

You are welcome and thanks for your comments and suggestions :)

Zhuo

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

author
#13 lore raw
In reply to z
Quoted 166 lines · On Mon, Sep 8, 2014 at 4:48 PM, z <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 05.09.14 at 18:35, <[email protected]> wrote:

Got it. Thanks, Jan.
If so, I think we could remove the condition for masking NX in both

vendor

Quoted 1 line

specific functions, since the architectural logic has help cover it and

the

tools/libxc/xc_cpuid_x86.c static void amd_xc_cpuid_policy( · line 118

Quoted 10 lines

judgement is unnecessary. For example:

tools/libxc/xc_cpuid_x86.c +0 -0
index 61af3e6..6bd89b0 100644
@@ -116,7 +116,7 @@ static void amd_xc_cpuid_policy(
                     bitmaskof(X86_FEATURE_TBM) |
                     bitmaskof(X86_FEATURE_DBEXT));
         regs[3] &= (0x0183f3ff | /* features shared with

0x00000001:EDX */

Quoted 9 lines
tools/libxc/xc_cpuid_x86.c +1 -1
-                    (is_pae ? bitmaskof(X86_FEATURE_NX) : 0) |
+                    bitmaskof(X86_FEATURE_NX) |
                     (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
                     bitmaskof(X86_FEATURE_SYSCALL) |
                     bitmaskof(X86_FEATURE_MP) |
@@ -201,7 +201,7 @@ static void intel_xc_cpuid_policy(
         regs[2] &= (is_64bit ? bitmaskof(X86_FEATURE_LAHF_LM) : 0) |
                                bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
                                bitmaskof(X86_FEATURE_ABM);
Quoted 9 lines

tools/libxc/xc_cpuid_x86.c static void intel_xc_cpuid_policy( · line 207

Quoted 5 lines
tools/libxc/xc_cpuid_x86.c +1 -1
-        regs[3] &= ((is_pae ? bitmaskof(X86_FEATURE_NX) : 0) |
+        regs[3] &= (bitmaskof(X86_FEATURE_NX) |
                     (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_RDTSCP) : 0));

Right, and I think you could go further and move at least the LM check into vendor-independent code too.

Hi, Jan

Thanks. Yes, and I did some changes as below.
Please correct me if I did some improper ones.

tools/libxc/xc_cpuid_x86.c +25 -18
index 9add109..7d79dc2 100644
@@ -80,7 +80,7 @@ static void xc_cpuid_brand_get(char *str)
 static void amd_xc_cpuid_policy(
     xc_interface *xch, domid_t domid,
     const unsigned int *input, unsigned int *regs,
-    int is_pae, int is_nestedhvm)
+    int is_64bit, int is_pae, int is_nestedhvm)
 {
     switch ( input[0] )
     {
@@ -95,13 +95,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) |
@@ -117,7 +115,7 @@ static void amd_xc_cpuid_policy(
                     bitmaskof(X86_FEATURE_DBEXT));
         regs[3] &= (0x0183f3ff | /* features shared with 0x00000001:EDX */
                     bitmaskof(X86_FEATURE_NX) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
+                    bitmaskof(X86_FEATURE_LM) |
                     bitmaskof(X86_FEATURE_SYSCALL) |
                     bitmaskof(X86_FEATURE_MP) |
                     bitmaskof(X86_FEATURE_MMXEXT) |
@@ -169,7 +167,7 @@ static void amd_xc_cpuid_policy(
 static void intel_xc_cpuid_policy(
     xc_interface *xch, domid_t domid,
     const unsigned int *input, unsigned int *regs,
-    int is_pae, int is_nestedhvm)
+    int is_64bit, int is_pae, int is_nestedhvm)
 {
     switch ( input[0] )
     {
@@ -195,14 +193,12 @@ 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[2] &= (bitmaskof(X86_FEATURE_LAHF_LM) |
+                    bitmaskof(X86_FEATURE_3DNOWPREFETCH) |
+                    bitmaskof(X86_FEATURE_ABM);
         regs[3] &= (bitmaskof(X86_FEATURE_NX) |
-                    (is_64bit ? bitmaskof(X86_FEATURE_LM) : 0) |
+                    bitmaskof(X86_FEATURE_LM) |
                     (is_64bit ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
                     (is_64bit ? bitmaskof(X86_FEATURE_RDTSCP) : 0));
         break;
@@ -278,12 +274,14 @@ static void xc_cpuid_hvm_policy(
     DECLARE_DOMCTL;
     char brand[13];
     uint64_t val;
-    int is_pae, is_nestedhvm;
+    int is_64bit, is_pae, is_nestedhvm;
     uint64_t xfeature_mask;
 
     xc_hvm_param_get(xch, domid, HVM_PARAM_PAE_ENABLED, &val);
     is_pae = !!val;
-
+
+    is_64bit = hypervisor_is_64bit(xch) && is_pae;
+
     /* Detecting Xen's atitude towards XSAVE */
     memset(&domctl, 0, sizeof(domctl));
     domctl.cmd = XEN_DOMCTL_getvcpuextstate;
@@ -391,10 +389,18 @@ static void xc_cpuid_hvm_policy(
         break;
 
     case 0x80000001:
-        if ( !is_pae ) {
+        if ( !is_64bit ) {
+            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]);
+        } else if ( !is_pae ) {
             clear_bit(X86_FEATURE_NX, regs[3]);
             clear_bit(X86_FEATURE_PSE36, regs[3]);
+        } else {
+            /* Do nothing for 32-bit guest */
         }
+
         break;
 
     case 0x80000007:
@@ -430,10 +436,11 @@ static void xc_cpuid_hvm_policy(
 
     xc_cpuid_brand_get(brand);
     if ( strstr(brand, "AMD") )
-        amd_xc_cpuid_policy(xch, domid, input, regs, is_pae,
 is_nestedhvm);
+        amd_xc_cpuid_policy(xch, domid, input, regs,
+                            is_64bit, is_pae, is_nestedhvm);
     else
-        intel_xc_cpuid_policy(xch, domid, input, regs, is_pae,
 is_nestedhvm);
-
+        intel_xc_cpuid_policy(xch, domid, input, regs,
+                              is_64bit, is_pae, is_nestedhvm);
 
Quoted 3 lines
Quoted 6 lines
Quoted 3 lines

Looking at the context above I also wonder whether tying RDTSCP to 64-bit guests is really correct in (at least) the Intel case.

I am not so sure for now, but I will check it later as well:)

Hi, Jan

I just checked Intel SDM. It seems you are right. RDTSCP could be used for both 64-bit and 32-bit architectures. Maybe the limit for 64-bit should be removed too.

tools/libxc/xc_cpuid_x86.c +1 -1
index 710fd61..e7b50b1 100644
@@ -193,7 +193,7 @@ static void intel_xc_cpuid_policy(
         regs[3] &= (bitmaskof(X86_FEATURE_NX) |
                     bitmaskof(X86_FEATURE_LM) |
                     (is_pae ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
-                    (is_pae ? bitmaskof(X86_FEATURE_RDTSCP) : 0));
+                    (bitmaskof(X86_FEATURE_RDTSCP));
         break;
     }
 

Zhuo

Quoted 9 lines
Quoted 6 lines

Jan

Zhuo

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

#14 lore raw
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 08.09.14 at 16:43, <[email protected]> wrote:
Quoted 6 lines · On Mon, Sep 8, 2014 at 4:48 PM, z <[email protected]> wrote:
Quoted 3 lines · Jan Beulich

Looking at the context above I also wonder whether tying RDTSCP to 64-bit guests is really correct in (at least) the Intel case.

I am not so sure for now, but I will check it later as well:)

I just checked Intel SDM. It seems you are right. RDTSCP could be used for both 64-bit and 32-bit architectures. Maybe the limit for 64-bit should be removed too.

tools/libxc/xc_cpuid_x86.c +0 -0
index 710fd61..e7b50b1 100644

tools/libxc/xc_cpuid_x86.c static void intel_xc_cpuid_policy( · line 198

Quoted 7 lines
tools/libxc/xc_cpuid_x86.c +1 -1
@@ -193,7 +193,7 @@ static void intel_xc_cpuid_policy(
         regs[3] &= (bitmaskof(X86_FEATURE_NX) |
                     bitmaskof(X86_FEATURE_LM) |
                     (is_pae ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
-                    (is_pae ? bitmaskof(X86_FEATURE_RDTSCP) : 0));
+                    (bitmaskof(X86_FEATURE_RDTSCP));
         break;
     }

That's what I though it should look like; the tying of various features to "is_pae" with your most recent patch is a little strange anyway, i.e. I'd hope that many of those could get cleaned up just like the RDTSCP one.

In any even, please submit a format patch for the change above and feel free to extend the cleanup.

Thanks again, Jan

author
#15 lore raw
Quoted 33 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 08.09.14 at 16:43, <[email protected]> wrote:
Quoted 6 lines · On Mon, Sep 8, 2014 at 4:48 PM, z <[email protected]> wrote:
Quoted 3 lines · Jan Beulich

Looking at the context above I also wonder whether tying RDTSCP to 64-bit guests is really correct in (at least) the Intel case.

I am not so sure for now, but I will check it later as well:)

I just checked Intel SDM. It seems you are right. RDTSCP could be used

for

Quoted 1 line

both 64-bit and 32-bit architectures. Maybe the limit for 64-bit should

be

Quoted 7 lines

removed too.

tools/libxc/xc_cpuid_x86.c +0 -0
index 710fd61..e7b50b1 100644

tools/libxc/xc_cpuid_x86.c static void intel_xc_cpuid_policy( · line 198

Quoted 7 lines
tools/libxc/xc_cpuid_x86.c +1 -1
@@ -193,7 +193,7 @@ static void intel_xc_cpuid_policy(
         regs[3] &= (bitmaskof(X86_FEATURE_NX) |
                     bitmaskof(X86_FEATURE_LM) |
                     (is_pae ? bitmaskof(X86_FEATURE_SYSCALL) : 0) |
-                    (is_pae ? bitmaskof(X86_FEATURE_RDTSCP) : 0));
+                    (bitmaskof(X86_FEATURE_RDTSCP));
         break;
     }

That's what I though it should look like; the tying of various features to "is_pae" with your most recent patch is a little strange anyway, i.e. I'd hope that many of those could get cleaned up just like the RDTSCP one.

Quoted 5 lines

In any even, please submit a format patch for the change above and feel free to extend the cleanup.

Thanks again, Jan

Hi, Jan

Okay, thanks.

Zhuo

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