Skip to contentSkip to conversation

x86/boot: Further simplify CR4 handling in dom0_construct_pv()

PATCH In review Andrew Cooper 3 participants 5 messages Sep 24, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/boot: Further simplify CR4 handling in dom0_construct_pv()

The logic would be more robust disabling SMAP based on its precense in CR4, rather than on certain features.

A forthcoming feature, LASS, needs the same treatment here. Introduce minimum enumeration information, although it will take a bit more work to get LASS fully usable in guests.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

I know LASS can't be used with traditional PV guests, but I have some PV-lite plans. The problem is the PV kernel, in CPL3, accessing addresses in the high canonincal half.

---
 xen/arch/x86/include/asm/x86-defns.h        |  1 +
 xen/arch/x86/pv/dom0_build.c                | 18 ++++++++++--------
 xen/include/public/arch-x86/cpufeatureset.h |  1 +
 3 files changed, 12 insertions(+), 8 deletions(-)
xen/arch/x86/include/asm/x86-defns.h +1 -0
index caa92829eaa9..8f97fb1e6a12 100644
@@ -75,6 +75,7 @@
 #define X86_CR4_PKE        0x00400000 /* enable PKE */
 #define X86_CR4_CET        0x00800000 /* Control-flow Enforcement Technology */
 #define X86_CR4_PKS        0x01000000 /* Protection Key Supervisor */
+#define X86_CR4_LASS       0x08000000 /* Linear Address Space Separation */
 
 /*
  * XSTATE component flags in XCR0 | MSR_XSS
xen/arch/x86/pv/dom0_build.c +10 -8
index 262edb6bf2f0..f5c868df384f 100644
@@ -1057,29 +1057,31 @@ int __init dom0_construct_pv(struct domain *d,
                              module_t *initrd,
                              const char *cmdline)
 {
+    unsigned long cr4 = read_cr4();
+    unsigned long mask = X86_CR4_SMAP | X86_CR4_LASS;
     int rc;
 
     /*
-     * Clear SMAP in CR4 to allow user-accesses in construct_dom0().  This
-     * prevents us needing to write construct_dom0() in terms of
+     * Clear SMAP/LASS in CR4 to allow user-accesses in construct_dom0().
+     * This prevents us needing to write construct_dom0() in terms of
      * copy_{to,from}_user().
      */
-    if ( boot_cpu_has(X86_FEATURE_XEN_SMAP) )
+    if ( cr4 & mask )
     {
         if ( IS_ENABLED(CONFIG_PV32) )
-            cr4_pv32_mask &= ~X86_CR4_SMAP;
+            cr4_pv32_mask &= ~mask;
 
-        write_cr4(read_cr4() & ~X86_CR4_SMAP);
+        write_cr4(cr4 & ~mask);
     }
 
     rc = dom0_construct(d, image, image_headroom, initrd, cmdline);
 
-    if ( boot_cpu_has(X86_FEATURE_XEN_SMAP) )
+    if ( cr4 & mask )
     {
-        write_cr4(read_cr4() | X86_CR4_SMAP);
+        write_cr4(cr4);
 
         if ( IS_ENABLED(CONFIG_PV32) )
-            cr4_pv32_mask |= X86_CR4_SMAP;
+            cr4_pv32_mask |= mask;
     }
 
     return rc;
xen/include/public/arch-x86/cpufeatureset.h +1 -0
index 8fa3fb711a8d..cbc0a3a8aa2b 100644
@@ -303,6 +303,7 @@ XEN_CPUFEATURE(SM3,          10*32+ 1) /*A  SM3 Instructions */
 XEN_CPUFEATURE(SM4,          10*32+ 2) /*A  SM4 Instructions */
 XEN_CPUFEATURE(AVX_VNNI,     10*32+ 4) /*A  AVX-VNNI Instructions */
 XEN_CPUFEATURE(AVX512_BF16,  10*32+ 5) /*A  AVX512 BFloat16 Instructions */
+XEN_CPUFEATURE(LASS,         10*32+ 6) /*   Linear Address Space Separation */
 XEN_CPUFEATURE(CMPCCXADD,    10*32+ 7) /*a  CMPccXADD Instructions */
 XEN_CPUFEATURE(FZRM,         10*32+10) /*A  Fast Zero-length REP MOVSB */
 XEN_CPUFEATURE(FSRS,         10*32+11) /*A  Fast Short REP STOSB */
Signature · 1 line
2.39.5
#1 lore raw
Quoted 6 lines · Andrew Cooper

The logic would be more robust disabling SMAP based on its precense in CR4, rather than on certain features.

A forthcoming feature, LASS, needs the same treatment here. Introduce minimum enumeration information, although it will take a bit more work to get LASS fully usable in guests.

Reading the ISA, doesn't LASS require SMAP to be enabled in %cr4, and hence disabling SMAP already disables LASS? (without having to toggle the LASS %cr4 bit)

"A supervisor-mode data access causes a LASS violation only if supervisor-mode access protection is enabled (because CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure."

We can consider also disabling it, but I think it would need to be noted that such disabling is not strictly necessary, as disabling SMAP already disables LASS.

Quoted 32 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

I know LASS can't be used with traditional PV guests, but I have some PV-lite plans. The problem is the PV kernel, in CPL3, accessing addresses in the high canonincal half.

---
 xen/arch/x86/include/asm/x86-defns.h        |  1 +
 xen/arch/x86/pv/dom0_build.c                | 18 ++++++++++--------
 xen/include/public/arch-x86/cpufeatureset.h |  1 +
 3 files changed, 12 insertions(+), 8 deletions(-)
xen/arch/x86/include/asm/x86-defns.h +1 -0
index caa92829eaa9..8f97fb1e6a12 100644
@@ -75,6 +75,7 @@
 #define X86_CR4_PKE        0x00400000 /* enable PKE */
 #define X86_CR4_CET        0x00800000 /* Control-flow Enforcement Technology */
 #define X86_CR4_PKS        0x01000000 /* Protection Key Supervisor */
+#define X86_CR4_LASS       0x08000000 /* Linear Address Space Separation */
 
 /*
  * XSTATE component flags in XCR0 | MSR_XSS
xen/arch/x86/pv/dom0_build.c +0 -0
index 262edb6bf2f0..f5c868df384f 100644

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 1061

Quoted 5 lines
xen/arch/x86/pv/dom0_build.c +2 -0
@@ -1057,29 +1057,31 @@ int __init dom0_construct_pv(struct domain *d,
                              module_t *initrd,
                              const char *cmdline)
 {
+    unsigned long cr4 = read_cr4();
+    unsigned long mask = X86_CR4_SMAP | X86_CR4_LASS;

const maybe? Seeing as it is read-only.

Thanks, Roger.

#2 lore raw
Quoted 11 lines · Roger Pau Monné
Quoted 6 lines · Andrew Cooper

The logic would be more robust disabling SMAP based on its precense in CR4, rather than on certain features.

A forthcoming feature, LASS, needs the same treatment here. Introduce minimum enumeration information, although it will take a bit more work to get LASS fully usable in guests.

Reading the ISA, doesn't LASS require SMAP to be enabled in %cr4, and hence disabling SMAP already disables LASS? (without having to toggle the LASS %cr4 bit)

Quoted 8 lines

"A supervisor-mode data access causes a LASS violation only if supervisor-mode access protection is enabled (because CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure."

We can consider also disabling it, but I think it would need to be noted that such disabling is not strictly necessary, as disabling SMAP already disables LASS.

Hmm.  LASS looks to have no CR4 dependencies on SMAP or SMEP, and the ISE does suggest they can be used independently.

However, I see no connection to paging (beyond LMA), and that is going to become a problem in due course.

Anyway - I'll drop the LASS aspect for now.  It can be left to whomever gets some working real hardware first.

~Andrew

#3 lore raw
In reply to Andrew Cooper
Quoted 29 lines · Andrew Cooper
xen/arch/x86/pv/dom0_build.c +7 -5
@@ -1057,29 +1057,31 @@ int __init dom0_construct_pv(struct domain *d,
                              module_t *initrd,
                              const char *cmdline)
 {
+    unsigned long cr4 = read_cr4();
+    unsigned long mask = X86_CR4_SMAP | X86_CR4_LASS;
     int rc;
 
     /*
-     * Clear SMAP in CR4 to allow user-accesses in construct_dom0().  This
-     * prevents us needing to write construct_dom0() in terms of
+     * Clear SMAP/LASS in CR4 to allow user-accesses in construct_dom0().
+     * This prevents us needing to write construct_dom0() in terms of
      * copy_{to,from}_user().
      */
-    if ( boot_cpu_has(X86_FEATURE_XEN_SMAP) )
+    if ( cr4 & mask )
     {
         if ( IS_ENABLED(CONFIG_PV32) )
-            cr4_pv32_mask &= ~X86_CR4_SMAP;
+            cr4_pv32_mask &= ~mask;
 
-        write_cr4(read_cr4() & ~X86_CR4_SMAP);
+        write_cr4(cr4 & ~mask);
     }
 
     rc = dom0_construct(d, image, image_headroom, initrd, cmdline);

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 1084

Quoted 10 lines
xen/arch/x86/pv/dom0_build.c +3 -3
 
-    if ( boot_cpu_has(X86_FEATURE_XEN_SMAP) )
+    if ( cr4 & mask )
     {
-        write_cr4(read_cr4() | X86_CR4_SMAP);
+        write_cr4(cr4);
 
         if ( IS_ENABLED(CONFIG_PV32) )
-            cr4_pv32_mask |= X86_CR4_SMAP;
+            cr4_pv32_mask |= mask;

You may end up setting a bit here which wasn't previously set, and which might then fault when cr4_pv32_restore tries to OR this into %cr4. Aiui you must have tested this on LASS-capable hardware, for it to have worked.

Jan

#4 lore raw
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Andrew Cooper
xen/arch/x86/pv/dom0_build.c +9 -7
@@ -1057,29 +1057,31 @@ int __init dom0_construct_pv(struct domain *d,
                              module_t *initrd,
                              const char *cmdline)
 {
+    unsigned long cr4 = read_cr4();
+    unsigned long mask = X86_CR4_SMAP | X86_CR4_LASS;
     int rc;
 
     /*
-     * Clear SMAP in CR4 to allow user-accesses in construct_dom0().  This
-     * prevents us needing to write construct_dom0() in terms of
+     * Clear SMAP/LASS in CR4 to allow user-accesses in construct_dom0().
+     * This prevents us needing to write construct_dom0() in terms of
      * copy_{to,from}_user().
      */
-    if ( boot_cpu_has(X86_FEATURE_XEN_SMAP) )
+    if ( cr4 & mask )
     {
         if ( IS_ENABLED(CONFIG_PV32) )
-            cr4_pv32_mask &= ~X86_CR4_SMAP;
+            cr4_pv32_mask &= ~mask;
 
-        write_cr4(read_cr4() & ~X86_CR4_SMAP);
+        write_cr4(cr4 & ~mask);
     }
 
     rc = dom0_construct(d, image, image_headroom, initrd, cmdline);
 
-    if ( boot_cpu_has(X86_FEATURE_XEN_SMAP) )
+    if ( cr4 & mask )
     {
-        write_cr4(read_cr4() | X86_CR4_SMAP);
+        write_cr4(cr4);
 
Quoted 7 lines

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 1084

Quoted 3 lines
xen/arch/x86/pv/dom0_build.c +1 -1
         if ( IS_ENABLED(CONFIG_PV32) )
-            cr4_pv32_mask |= X86_CR4_SMAP;
+            cr4_pv32_mask |= mask;

You may end up setting a bit here which wasn't previously set, and which might then fault when cr4_pv32_restore tries to OR this into %cr4. Aiui you must have tested this on LASS-capable hardware, for it to have worked.

Possibly also needs X86_CR4_LASS adding to the XEN_CR4_PV32_BITS define, as otherwise it won't end up in cr4_pv32_mask in the first place AFAICT.

Thanks, Roger.