Skip to content

Commit e7e80b6

Browse files
committed
x86/cpu: KVM: Add common defines for architectural memory types (PAT, MTRRs, etc.)
Add defines for the architectural memory types that can be shoved into various MSRs and registers, e.g. MTRRs, PAT, VMX capabilities MSRs, EPTPs, etc. While most MSRs/registers support only a subset of all memory types, the values themselves are architectural and identical across all users. Leave the goofy MTRR_TYPE_* definitions as-is since they are in a uapi header, but add compile-time assertions to connect the dots (and sanity check that the msr-index.h values didn't get fat-fingered). Keep the VMX_EPTP_MT_* defines so that it's slightly more obvious that the EPTP holds a single memory type in 3 of its 64 bits; those bits just happen to be 2:0, i.e. don't need to be shifted. Opportunistically use X86_MEMTYPE_WB instead of an open coded '6' in setup_vmcs_config(). No functional change intended. Reviewed-by: Thomas Gleixner <[email protected]> Acked-by: Kai Huang <[email protected]> Reviewed-by: Xiaoyao Li <[email protected]> Reviewed-by: Kai Huang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 47ac09b commit e7e80b6

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed

arch/x86/include/asm/msr-index.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@
3636
#define EFER_FFXSR (1<<_EFER_FFXSR)
3737
#define EFER_AUTOIBRS (1<<_EFER_AUTOIBRS)
3838

39+
/*
40+
* Architectural memory types that are common to MTRRs, PAT, VMX MSRs, etc.
41+
* Most MSRs support/allow only a subset of memory types, but the values
42+
* themselves are common across all relevant MSRs.
43+
*/
44+
#define X86_MEMTYPE_UC 0ull /* Uncacheable, a.k.a. Strong Uncacheable */
45+
#define X86_MEMTYPE_WC 1ull /* Write Combining */
46+
/* RESERVED 2 */
47+
/* RESERVED 3 */
48+
#define X86_MEMTYPE_WT 4ull /* Write Through */
49+
#define X86_MEMTYPE_WP 5ull /* Write Protected */
50+
#define X86_MEMTYPE_WB 6ull /* Write Back */
51+
#define X86_MEMTYPE_UC_MINUS 7ull /* Weak Uncacheabled (PAT only) */
52+
3953
/* FRED MSRs */
4054
#define MSR_IA32_FRED_RSP0 0x1cc /* Level 0 stack pointer */
4155
#define MSR_IA32_FRED_RSP1 0x1cd /* Level 1 stack pointer */
@@ -1163,7 +1177,6 @@
11631177
#define VMX_BASIC_64 0x0001000000000000LLU
11641178
#define VMX_BASIC_MEM_TYPE_SHIFT 50
11651179
#define VMX_BASIC_MEM_TYPE_MASK 0x003c000000000000LLU
1166-
#define VMX_BASIC_MEM_TYPE_WB 6LLU
11671180
#define VMX_BASIC_INOUT 0x0040000000000000LLU
11681181

11691182
/* Resctrl MSRs: */

arch/x86/include/asm/vmx.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,10 @@ enum vmcs_field {
508508
#define VMX_EPTP_PWL_4 0x18ull
509509
#define VMX_EPTP_PWL_5 0x20ull
510510
#define VMX_EPTP_AD_ENABLE_BIT (1ull << 6)
511+
/* The EPTP memtype is encoded in bits 2:0, i.e. doesn't need to be shifted. */
511512
#define VMX_EPTP_MT_MASK 0x7ull
512-
#define VMX_EPTP_MT_WB 0x6ull
513-
#define VMX_EPTP_MT_UC 0x0ull
513+
#define VMX_EPTP_MT_WB X86_MEMTYPE_WB
514+
#define VMX_EPTP_MT_UC X86_MEMTYPE_UC
514515
#define VMX_EPT_READABLE_MASK 0x1ull
515516
#define VMX_EPT_WRITABLE_MASK 0x2ull
516517
#define VMX_EPT_EXECUTABLE_MASK 0x4ull

arch/x86/kernel/cpu/mtrr/mtrr.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555

5656
#include "mtrr.h"
5757

58+
static_assert(X86_MEMTYPE_UC == MTRR_TYPE_UNCACHABLE);
59+
static_assert(X86_MEMTYPE_WC == MTRR_TYPE_WRCOMB);
60+
static_assert(X86_MEMTYPE_WT == MTRR_TYPE_WRTHROUGH);
61+
static_assert(X86_MEMTYPE_WP == MTRR_TYPE_WRPROT);
62+
static_assert(X86_MEMTYPE_WB == MTRR_TYPE_WRBACK);
63+
5864
/* arch_phys_wc_add returns an MTRR register index plus this offset. */
5965
#define MTRR_TO_PHYS_WC_OFFSET 1000
6066

arch/x86/kvm/vmx/nested.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7070,7 +7070,7 @@ static void nested_vmx_setup_basic(struct nested_vmx_msrs *msrs)
70707070
VMCS12_REVISION |
70717071
VMX_BASIC_TRUE_CTLS |
70727072
((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
7073-
(VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
7073+
(X86_MEMTYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
70747074

70757075
if (cpu_has_vmx_basic_inout())
70767076
msrs->basic |= VMX_BASIC_INOUT;

arch/x86/kvm/vmx/vmx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2747,7 +2747,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,
27472747
#endif
27482748

27492749
/* Require Write-Back (WB) memory type for VMCS accesses. */
2750-
if (((vmx_msr_high >> 18) & 15) != 6)
2750+
if (((vmx_msr_high >> 18) & 15) != X86_MEMTYPE_WB)
27512751
return -EIO;
27522752

27532753
rdmsrl(MSR_IA32_VMX_MISC, misc_msr);

arch/x86/mm/pat/memtype.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,6 @@ static inline void set_page_memtype(struct page *pg,
176176
}
177177
#endif
178178

179-
enum {
180-
PAT_UC = 0, /* uncached */
181-
PAT_WC = 1, /* Write combining */
182-
PAT_WT = 4, /* Write Through */
183-
PAT_WP = 5, /* Write Protected */
184-
PAT_WB = 6, /* Write Back (default) */
185-
PAT_UC_MINUS = 7, /* UC, but can be overridden by MTRR */
186-
};
187-
188179
#define CM(c) (_PAGE_CACHE_MODE_ ## c)
189180

190181
static enum page_cache_mode __init pat_get_cache_mode(unsigned int pat_val,
@@ -194,13 +185,13 @@ static enum page_cache_mode __init pat_get_cache_mode(unsigned int pat_val,
194185
char *cache_mode;
195186

196187
switch (pat_val) {
197-
case PAT_UC: cache = CM(UC); cache_mode = "UC "; break;
198-
case PAT_WC: cache = CM(WC); cache_mode = "WC "; break;
199-
case PAT_WT: cache = CM(WT); cache_mode = "WT "; break;
200-
case PAT_WP: cache = CM(WP); cache_mode = "WP "; break;
201-
case PAT_WB: cache = CM(WB); cache_mode = "WB "; break;
202-
case PAT_UC_MINUS: cache = CM(UC_MINUS); cache_mode = "UC- "; break;
203-
default: cache = CM(WB); cache_mode = "WB "; break;
188+
case X86_MEMTYPE_UC: cache = CM(UC); cache_mode = "UC "; break;
189+
case X86_MEMTYPE_WC: cache = CM(WC); cache_mode = "WC "; break;
190+
case X86_MEMTYPE_WT: cache = CM(WT); cache_mode = "WT "; break;
191+
case X86_MEMTYPE_WP: cache = CM(WP); cache_mode = "WP "; break;
192+
case X86_MEMTYPE_WB: cache = CM(WB); cache_mode = "WB "; break;
193+
case X86_MEMTYPE_UC_MINUS: cache = CM(UC_MINUS); cache_mode = "UC- "; break;
194+
default: cache = CM(WB); cache_mode = "WB "; break;
204195
}
205196

206197
memcpy(msg, cache_mode, 4);
@@ -257,11 +248,11 @@ void pat_cpu_init(void)
257248
void __init pat_bp_init(void)
258249
{
259250
struct cpuinfo_x86 *c = &boot_cpu_data;
260-
#define PAT(p0, p1, p2, p3, p4, p5, p6, p7) \
261-
(((u64)PAT_ ## p0) | ((u64)PAT_ ## p1 << 8) | \
262-
((u64)PAT_ ## p2 << 16) | ((u64)PAT_ ## p3 << 24) | \
263-
((u64)PAT_ ## p4 << 32) | ((u64)PAT_ ## p5 << 40) | \
264-
((u64)PAT_ ## p6 << 48) | ((u64)PAT_ ## p7 << 56))
251+
#define PAT(p0, p1, p2, p3, p4, p5, p6, p7) \
252+
((X86_MEMTYPE_ ## p0) | (X86_MEMTYPE_ ## p1 << 8) | \
253+
(X86_MEMTYPE_ ## p2 << 16) | (X86_MEMTYPE_ ## p3 << 24) | \
254+
(X86_MEMTYPE_ ## p4 << 32) | (X86_MEMTYPE_ ## p5 << 40) | \
255+
(X86_MEMTYPE_ ## p6 << 48) | (X86_MEMTYPE_ ## p7 << 56))
265256

266257

267258
if (!IS_ENABLED(CONFIG_X86_PAT))

0 commit comments

Comments
 (0)