[PATCH v3 09/12] x86: Derive XEN_MSR_PAT from its individual entries

Demi Marie Obenour posted 12 patches 3 years, 1 month ago
There is a newer version of this series
[PATCH v3 09/12] x86: Derive XEN_MSR_PAT from its individual entries
Posted by Demi Marie Obenour 3 years, 1 month ago
This avoids it being a magic constant that is difficult for humans to
decode.  Use BUILD_BUG_ON to check that the old and new values are
identical.

Signed-off-by: Demi Marie Obenour <demi@invisiblethingslab.com>
---
 xen/arch/x86/include/asm/processor.h | 10 +++++++++-
 xen/arch/x86/mm.c                    |  6 ++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/xen/arch/x86/include/asm/processor.h b/xen/arch/x86/include/asm/processor.h
index 8e2816fae9b97bd4e153a30cc3802971fe0355af..9535b1f7f49d75e6853365e6109a33359c740f4f 100644
--- a/xen/arch/x86/include/asm/processor.h
+++ b/xen/arch/x86/include/asm/processor.h
@@ -96,7 +96,15 @@
  * Host IA32_CR_PAT value to cover all memory types.  This is not the default
  * MSR_PAT value, and is an ABI with PV guests.
  */
-#define XEN_MSR_PAT _AC(0x050100070406, ULL)
+#define XEN_MSR_PAT (X86_MT_WB  << 0x00 | \
+                     X86_MT_WT  << 0x08 | \
+                     X86_MT_UCM << 0x10 | \
+                     X86_MT_UC  << 0x18 | \
+                     X86_MT_WC  << 0x20 | \
+                     X86_MT_WP  << 0x28 | \
+                     X86_MT_UC  << 0x30 | \
+                     X86_MT_UC  << 0x38 | \
+                     0)
 
 #ifndef __ASSEMBLY__
 
diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index b73cb29327ba49703673886d09d79f2f8928a6c0..a8def47aa3bf9770576c62a190032d45d63dd86e 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -6355,6 +6355,12 @@ unsigned long get_upper_mfn_bound(void)
     return min(max_mfn, 1UL << (paddr_bits - PAGE_SHIFT)) - 1;
 }
 
+static void __init __maybe_unused build_assertions(void)
+{
+    BUILD_BUG_ON(XEN_MSR_PAT != 0x050100070406ULL &&
+                 "wrong XEN_MSR_PAT breaks PV guests");
+}
+
 /*
  * Local variables:
  * mode: C
-- 
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
Re: [PATCH v3 09/12] x86: Derive XEN_MSR_PAT from its individual entries
Posted by Jan Beulich 3 years, 1 month ago
On 15.12.2022 00:11, Demi Marie Obenour wrote:
> --- a/xen/arch/x86/include/asm/processor.h
> +++ b/xen/arch/x86/include/asm/processor.h
> @@ -96,7 +96,15 @@
>   * Host IA32_CR_PAT value to cover all memory types.  This is not the default
>   * MSR_PAT value, and is an ABI with PV guests.
>   */
> -#define XEN_MSR_PAT _AC(0x050100070406, ULL)
> +#define XEN_MSR_PAT (X86_MT_WB  << 0x00 | \
> +                     X86_MT_WT  << 0x08 | \
> +                     X86_MT_UCM << 0x10 | \
> +                     X86_MT_UC  << 0x18 | \
> +                     X86_MT_WC  << 0x20 | \
> +                     X86_MT_WP  << 0x28 | \
> +                     X86_MT_UC  << 0x30 | \
> +                     X86_MT_UC  << 0x38 | \

This wants properly parenthesizing (the shifts against the ors), and
at least the last four constants also look to need casting to uint64_t;
in fact I'm surprised that ...

> +                     0)

(interposed: I don't think this really does any good.)

> --- a/xen/arch/x86/mm.c
> +++ b/xen/arch/x86/mm.c
> @@ -6355,6 +6355,12 @@ unsigned long get_upper_mfn_bound(void)
>      return min(max_mfn, 1UL << (paddr_bits - PAGE_SHIFT)) - 1;
>  }
>  
> +static void __init __maybe_unused build_assertions(void)
> +{
> +    BUILD_BUG_ON(XEN_MSR_PAT != 0x050100070406ULL &&
> +                 "wrong XEN_MSR_PAT breaks PV guests");

... this didn't trigger for you. (We also don't normally add such
constructs with a string literal to BUILD_BUG_ON() expressions, not
the least because compilers may take issue with such. I'd like to
suggest to convert this to a comment instead.)

Jan