Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/boot: Introduce helpers for MSR reads/writes

The current set of helpers used throughout the run-time kernel have
dependencies on code/facilities outside of the boot kernel, so there
are a number of call-sites throughout the boot kernel where inline
assembly is used instead. More will be added with subsequent patches
that add support for SEV-SNP, so take the opportunity to provide a basic
set of helpers that can be used by the boot kernel to reduce reliance on
inline assembly.

Use boot_* prefix so that it's clear these are helpers specific to the
boot kernel to avoid any confusion with the various other MSR read/write
helpers.

[ bp: Disambiguate parameter names and trim comment. ]

Suggested-by: Borislav Petkov <bp@alien8.de>
Signed-off-by: Michael Roth <michael.roth@amd.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lore.kernel.org/r/20220307213356.2797205-6-brijesh.singh@amd.com

authored by

Michael Roth and committed by
Borislav Petkov
176db622 6d3b3d34

+42 -10
+26
arch/x86/boot/msr.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * Helpers/definitions related to MSR access. 4 + */ 5 + 6 + #ifndef BOOT_MSR_H 7 + #define BOOT_MSR_H 8 + 9 + #include <asm/shared/msr.h> 10 + 11 + /* 12 + * The kernel proper already defines rdmsr()/wrmsr(), but they are not for the 13 + * boot kernel since they rely on tracepoint/exception handling infrastructure 14 + * that's not available here. 15 + */ 16 + static inline void boot_rdmsr(unsigned int reg, struct msr *m) 17 + { 18 + asm volatile("rdmsr" : "=a" (m->l), "=d" (m->h) : "c" (reg)); 19 + } 20 + 21 + static inline void boot_wrmsr(unsigned int reg, const struct msr *m) 22 + { 23 + asm volatile("wrmsr" : : "c" (reg), "a"(m->l), "d" (m->h) : "memory"); 24 + } 25 + 26 + #endif /* BOOT_MSR_H */
+1 -10
arch/x86/include/asm/msr.h
··· 10 10 #include <asm/errno.h> 11 11 #include <asm/cpumask.h> 12 12 #include <uapi/asm/msr.h> 13 - 14 - struct msr { 15 - union { 16 - struct { 17 - u32 l; 18 - u32 h; 19 - }; 20 - u64 q; 21 - }; 22 - }; 13 + #include <asm/shared/msr.h> 23 14 24 15 struct msr_info { 25 16 u32 msr_no;
+15
arch/x86/include/asm/shared/msr.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef _ASM_X86_SHARED_MSR_H 3 + #define _ASM_X86_SHARED_MSR_H 4 + 5 + struct msr { 6 + union { 7 + struct { 8 + u32 l; 9 + u32 h; 10 + }; 11 + u64 q; 12 + }; 13 + }; 14 + 15 + #endif /* _ASM_X86_SHARED_MSR_H */