forked from QubesOS/qubes-vmm-xen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0622-x86-mm-make-code-robust-to-future-PAT-changes.patch
107 lines (101 loc) · 3.95 KB
/
0622-x86-mm-make-code-robust-to-future-PAT-changes.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
From deecde46e68bffdcfe51e8ccd0cadd91e178db50 Mon Sep 17 00:00:00 2001
From: Demi Marie Obenour <[email protected]>
Date: Mon, 5 Dec 2022 18:19:05 -0500
Subject: [PATCH] x86/mm: make code robust to future PAT changes
It may be desirable to change Xen's PAT for various reasons. This
requires changes to several _PAGE_* macros as well. Add static
assertions to check that XEN_MSR_PAT is consistent with the _PAGE_*
macros, and that _PAGE_WB is 0 as required by Linux.
Signed-off-by: Demi Marie Obenour <[email protected]>
---
xen/arch/x86/mm.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 71 insertions(+)
diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index 648d6dd475ba..702c76a879e5 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -6358,6 +6358,11 @@ unsigned long get_upper_mfn_bound(void)
return min(max_mfn, 1UL << (paddr_bits - PAGE_SHIFT)) - 1;
}
+
+/*
+ * A bunch of static assertions to check that the XEN_MSR_PAT is valid
+ * and consistent with the _PAGE_* macros, and that _PAGE_WB is zero.
+ */
static void __init __maybe_unused build_assertions(void)
{
/*
@@ -6367,6 +6372,72 @@ static void __init __maybe_unused build_assertions(void)
* using different PATs will not work.
*/
BUILD_BUG_ON(XEN_MSR_PAT != 0x050100070406ULL);
+
+ /*
+ * _PAGE_WB must be zero for several reasons, not least because Linux
+ * assumes it.
+ */
+ BUILD_BUG_ON(_PAGE_WB);
+
+ /* A macro to convert from cache attributes to actual cacheability */
+#define PAT_ENTRY(v) (0xFF & (XEN_MSR_PAT >> (8 * (v))))
+
+ /* Validate at compile-time that v is a valid value for a PAT entry */
+#define CHECK_PAT_ENTRY_VALUE(v) \
+ BUILD_BUG_ON((v) < 0 || (v) > 7 || \
+ (v) == X86_MT_RSVD_2 || (v) == X86_MT_RSVD_3)
+
+ /* Validate at compile-time that PAT entry v is valid */
+#define CHECK_PAT_ENTRY(v) do { \
+ BUILD_BUG_ON((v) < 0 || (v) > 7); \
+ CHECK_PAT_ENTRY_VALUE(PAT_ENTRY(v)); \
+} while (0);
+
+ /*
+ * If one of these trips, the corresponding entry in XEN_MSR_PAT is invalid.
+ * This would cause Xen to crash (with #GP) at startup.
+ */
+ CHECK_PAT_ENTRY(0);
+ CHECK_PAT_ENTRY(1);
+ CHECK_PAT_ENTRY(2);
+ CHECK_PAT_ENTRY(3);
+ CHECK_PAT_ENTRY(4);
+ CHECK_PAT_ENTRY(5);
+ CHECK_PAT_ENTRY(6);
+ CHECK_PAT_ENTRY(7);
+
+#undef CHECK_PAT_ENTRY
+#undef CHECK_PAT_ENTRY_VALUE
+
+ /* Macro version of page_flags_to_cacheattr(), for use in BUILD_BUG_ON()s */
+#define PAGE_FLAGS_TO_CACHEATTR(page_value) \
+ ((((page_value) >> 5) & 4) | (((page_value) >> 3) & 3))
+
+ /* Check that a PAT-related _PAGE_* macro is correct */
+#define CHECK_PAGE_VALUE(page_value) do { \
+ /* Check that the _PAGE_* macros only use bits from PAGE_CACHE_ATTRS */ \
+ BUILD_BUG_ON(((_PAGE_##page_value) & PAGE_CACHE_ATTRS) != \
+ (_PAGE_##page_value)); \
+ /* Check that the _PAGE_* are consistent with XEN_MSR_PAT */ \
+ BUILD_BUG_ON(PAT_ENTRY(PAGE_FLAGS_TO_CACHEATTR(_PAGE_##page_value)) != \
+ (X86_MT_##page_value)); \
+} while (0)
+
+ /*
+ * If one of these trips, the corresponding _PAGE_* macro is inconsistent
+ * with XEN_MSR_PAT. This would cause Xen to use incorrect cacheability
+ * flags, with results that are undefined and probably harmful.
+ */
+ CHECK_PAGE_VALUE(WT);
+ CHECK_PAGE_VALUE(WB);
+ CHECK_PAGE_VALUE(WC);
+ CHECK_PAGE_VALUE(UC);
+ CHECK_PAGE_VALUE(UCM);
+ CHECK_PAGE_VALUE(WP);
+
+#undef CHECK_PAGE_VALUE
+#undef PAGE_FLAGS_TO_CACHEATTR
+#undef PAT_ENTRY
}
/*
--
2.44.0