Skip to content

Commit 9249bcd

Browse files
jallen-amdbp3tk0v
authored andcommitted
x86/boot: Move boot_*msr helpers to asm/shared/msr.h
The boot_{rdmsr,wrmsr}() helpers are *just* the barebones MSR access functionality, without any tracing or exception handling glue as it is done in kernel proper. Move these helpers to asm/shared/msr.h and rename to raw_{rdmsr,wrmsr}() to indicate what they are. [ bp: Correct the reason why those helpers exist. I should've caught that in the original patch that added them: 176db62 ("x86/boot: Introduce helpers for MSR reads/writes" but oh well... - fixup include path delimiters to <> ] Signed-off-by: John Allen <john.allen@amd.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> Link: https://patch.msgid.link/all/20250924200852.4452-2-john.allen@amd.com
1 parent dcb6fa3 commit 9249bcd

5 files changed

Lines changed: 30 additions & 40 deletions

File tree

arch/x86/boot/compressed/sev.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <asm/bootparam.h>
1616
#include <asm/pgtable_types.h>
17+
#include <asm/shared/msr.h>
1718
#include <asm/sev.h>
1819
#include <asm/trapnr.h>
1920
#include <asm/trap_pf.h>
@@ -397,7 +398,7 @@ void sev_enable(struct boot_params *bp)
397398
}
398399

399400
/* Set the SME mask if this is an SEV guest. */
400-
boot_rdmsr(MSR_AMD64_SEV, &m);
401+
raw_rdmsr(MSR_AMD64_SEV, &m);
401402
sev_status = m.q;
402403
if (!(sev_status & MSR_AMD64_SEV_ENABLED))
403404
return;
@@ -446,7 +447,7 @@ u64 sev_get_status(void)
446447
if (sev_check_cpu_support() < 0)
447448
return 0;
448449

449-
boot_rdmsr(MSR_AMD64_SEV, &m);
450+
raw_rdmsr(MSR_AMD64_SEV, &m);
450451
return m.q;
451452
}
452453

@@ -496,7 +497,7 @@ bool early_is_sevsnp_guest(void)
496497
struct msr m;
497498

498499
/* Obtain the address of the calling area to use */
499-
boot_rdmsr(MSR_SVSM_CAA, &m);
500+
raw_rdmsr(MSR_SVSM_CAA, &m);
500501
boot_svsm_caa_pa = m.q;
501502

502503
/*

arch/x86/boot/compressed/sev.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#ifdef CONFIG_AMD_MEM_ENCRYPT
1212

13-
#include "../msr.h"
13+
#include <asm/shared/msr.h>
1414

1515
void snp_accept_memory(phys_addr_t start, phys_addr_t end);
1616
u64 sev_get_status(void);
@@ -20,7 +20,7 @@ static inline u64 sev_es_rd_ghcb_msr(void)
2020
{
2121
struct msr m;
2222

23-
boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m);
23+
raw_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m);
2424

2525
return m.q;
2626
}
@@ -30,7 +30,7 @@ static inline void sev_es_wr_ghcb_msr(u64 val)
3030
struct msr m;
3131

3232
m.q = val;
33-
boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m);
33+
raw_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m);
3434
}
3535

3636
#else

arch/x86/boot/cpucheck.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
#include <asm/intel-family.h>
2727
#include <asm/processor-flags.h>
2828
#include <asm/msr-index.h>
29+
#include <asm/shared/msr.h>
2930

3031
#include "string.h"
31-
#include "msr.h"
3232

3333
static u32 err_flags[NCAPINTS];
3434

@@ -134,9 +134,9 @@ int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr)
134134

135135
struct msr m;
136136

137-
boot_rdmsr(MSR_K7_HWCR, &m);
137+
raw_rdmsr(MSR_K7_HWCR, &m);
138138
m.l &= ~(1 << 15);
139-
boot_wrmsr(MSR_K7_HWCR, &m);
139+
raw_wrmsr(MSR_K7_HWCR, &m);
140140

141141
get_cpuflags(); /* Make sure it really did something */
142142
err = check_cpuflags();
@@ -148,9 +148,9 @@ int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr)
148148

149149
struct msr m;
150150

151-
boot_rdmsr(MSR_VIA_FCR, &m);
151+
raw_rdmsr(MSR_VIA_FCR, &m);
152152
m.l |= (1 << 1) | (1 << 7);
153-
boot_wrmsr(MSR_VIA_FCR, &m);
153+
raw_wrmsr(MSR_VIA_FCR, &m);
154154

155155
set_bit(X86_FEATURE_CX8, cpu.flags);
156156
err = check_cpuflags();
@@ -160,14 +160,14 @@ int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr)
160160
struct msr m, m_tmp;
161161
u32 level = 1;
162162

163-
boot_rdmsr(0x80860004, &m);
163+
raw_rdmsr(0x80860004, &m);
164164
m_tmp = m;
165165
m_tmp.l = ~0;
166-
boot_wrmsr(0x80860004, &m_tmp);
166+
raw_wrmsr(0x80860004, &m_tmp);
167167
asm("cpuid"
168168
: "+a" (level), "=d" (cpu.flags[0])
169169
: : "ecx", "ebx");
170-
boot_wrmsr(0x80860004, &m);
170+
raw_wrmsr(0x80860004, &m);
171171

172172
err = check_cpuflags();
173173
} else if (err == 0x01 &&

arch/x86/boot/msr.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

arch/x86/include/asm/shared/msr.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@ struct msr {
1212
};
1313
};
1414

15+
/*
16+
* The kernel proper already defines rdmsr()/wrmsr(), but they are not for the
17+
* boot kernel since they rely on tracepoint/exception handling infrastructure
18+
* that's not available here.
19+
*/
20+
static inline void raw_rdmsr(unsigned int reg, struct msr *m)
21+
{
22+
asm volatile("rdmsr" : "=a" (m->l), "=d" (m->h) : "c" (reg));
23+
}
24+
25+
static inline void raw_wrmsr(unsigned int reg, const struct msr *m)
26+
{
27+
asm volatile("wrmsr" : : "c" (reg), "a"(m->l), "d" (m->h) : "memory");
28+
}
29+
1530
#endif /* _ASM_X86_SHARED_MSR_H */

0 commit comments

Comments
 (0)