From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D21EE54B for ; Mon, 29 Jun 2026 06:56:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782716220; cv=none; b=Xs+ki2W7OG4d/Gq5+WKjWYTA8QohjUWbZlK3X/+dIJixq9AbZT1xx9BjT3KVad3EOwncSko8ANXq0spaXvceKWGBCP+r1LhiMw+uOaPCH2WaNQ+//U8Ex3KUVDmloXzVusUed8WqXoFzyfU+0EmqX4GrNPiKRzSwNSXujp8UWkY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782716220; c=relaxed/simple; bh=bu5DRfzzUEyuO2Lo+UleBY4Lbk0IQhvPFaRBoiQ4cmo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qq99diWKpeUipUJujjL0AKss6IZrTDbzoxMpSnHqcZGZlRlyX1I6BHd3HPwPELNj1Fc9MCkK/8ElNgVCwBPoRj91n9O6O07Se8YoxqyW4hDOv7jfglDDiIomCvsH/ZI1/WrQ6goM/zlRA9lDeymQ9fqSTeXC0fGzSIcZG4BH0fQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 622D875ADB; Mon, 29 Jun 2026 06:56:50 +0000 (UTC) Authentication-Results: smtp-out2.suse.de; none Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 1A699779A8; Mon, 29 Jun 2026 06:56:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id zy/6BDIXQmpuQgAAD6G6ig (envelope-from ); Mon, 29 Jun 2026 06:56:50 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , "Xin Li (Intel)" Subject: [PATCH v4 11/18] x86/msr: Use the alternatives mechanism for RDMSR Date: Mon, 29 Jun 2026 08:55:37 +0200 Message-ID: <20260629065544.3643253-12-jgross@suse.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260629065544.3643253-1-jgross@suse.com> References: <20260629065544.3643253-1-jgross@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spam-Flag: NO X-Spam-Score: -4.00 X-Spam-Level: X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Rspamd-Queue-Id: 622D875ADB X-Rspamd-Action: no action When available use the immediate variant of RDMSR in __rdmsr(). For the safe/unsafe variants make __rdmsr() to be a common base function instead of duplicating the ALTERNATIVE*() macros. Modify native_rdmsr() and native_read_msr() to use native_rdmsrq(). The paravirt case will be handled later. Originally-by: Xin Li (Intel) Signed-off-by: Juergen Gross --- V2: - new patch, partially taken from "[RFC PATCH v2 22/34] x86/msr: Utilize the alternatives mechanism to read MSR" by Xin Li --- arch/x86/include/asm/msr.h | 106 +++++++++++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 22 deletions(-) diff --git a/arch/x86/include/asm/msr.h b/arch/x86/include/asm/msr.h index 40bb03df162c..6a8af80305d1 100644 --- a/arch/x86/include/asm/msr.h +++ b/arch/x86/include/asm/msr.h @@ -66,6 +66,8 @@ static inline void do_trace_rdpmc(u32 msr, u64 val, int failed) {} /* The GNU Assembler (Gas) with Binutils 2.41 adds the .insn directive support */ #if defined(CONFIG_AS_IS_GNU) && CONFIG_AS_VERSION >= 24100 +#define ASM_RDMSR_IMM \ + " .insn VEX.128.F2.M7.W0 0xf6 /0, %[msr]%{:u32}, %[val]\n\t" #define ASM_WRMSRNS_IMM \ " .insn VEX.128.F3.M7.W0 0xf6 /0, %[val], %[msr]%{:u32}\n\t" #else @@ -75,10 +77,17 @@ static inline void do_trace_rdpmc(u32 msr, u64 val, int failed) {} * The register operand is encoded as %rax because all uses of the immediate * form MSR access instructions reference %rax as the register operand. */ +#define ASM_RDMSR_IMM \ + " .byte 0xc4,0xe7,0x7b,0xf6,0xc0; .long %c[msr]" #define ASM_WRMSRNS_IMM \ " .byte 0xc4,0xe7,0x7a,0xf6,0xc0; .long %c[msr]" #endif +#define RDMSR_AND_SAVE_RESULT \ + "rdmsr\n\t" \ + "shl $0x20, %%rdx\n\t" \ + "or %%rdx, %%rax\n\t" + #define PREPARE_RDX_FOR_WRMSR \ "mov %%rax, %%rdx\n\t" \ "shr $0x20, %%rdx\n\t" @@ -94,16 +103,76 @@ static inline void do_trace_rdpmc(u32 msr, u64 val, int failed) {} * think of extending them - you will be slapped with a stinking trout or a frozen * shark will reach you, wherever you are! You've been warned. */ -static __always_inline u64 __rdmsr(u32 msr) +static __always_inline bool __rdmsrq_variable(u32 msr, u64 *val, int type) + { +#ifdef CONFIG_X86_64 + BUILD_BUG_ON(__builtin_constant_p(msr)); + + asm_inline volatile goto( + "1:\n" + RDMSR_AND_SAVE_RESULT + _ASM_EXTABLE_TYPE(1b, %l[badmsr], %c[type]) /* For RDMSR */ + + : [val] "=a" (*val) + : "c" (msr), [type] "i" (type) + : "rdx" + : badmsr); +#else + asm_inline volatile goto( + "1: rdmsr\n\t" + _ASM_EXTABLE_TYPE(1b, %l[badmsr], %c[type]) /* For RDMSR */ + + : "=A" (*val) + : "c" (msr), [type] "i" (type) + : + : badmsr); +#endif + + return false; + +badmsr: + *val = 0; + + return true; +} + +#ifdef CONFIG_X86_64 +static __always_inline bool __rdmsrq_constant(u32 msr, u64 *val, int type) { - EAX_EDX_DECLARE_ARGS(val, low, high); + BUILD_BUG_ON(!__builtin_constant_p(msr)); - asm volatile("1: rdmsr\n" - "2:\n" - _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_RDMSR) - : EAX_EDX_RET(val, low, high) : "c" (msr)); + asm_inline volatile goto( + "1:\n" + ALTERNATIVE("mov %[msr], %%ecx\n\t" + "2:\n" + RDMSR_AND_SAVE_RESULT, + ASM_RDMSR_IMM, + X86_FEATURE_MSR_IMM) + _ASM_EXTABLE_TYPE(1b, %l[badmsr], %c[type]) /* For RDMSR immediate */ + _ASM_EXTABLE_TYPE(2b, %l[badmsr], %c[type]) /* For RDMSR */ + + : [val] "=a" (*val) + : [msr] "i" (msr), [type] "i" (type) + : "ecx", "rdx" + : badmsr); - return EAX_EDX_VAL(val, low, high); + return false; + +badmsr: + *val = 0; + + return true; +} +#endif + +static __always_inline bool __rdmsr(u32 msr, u64 *val, int type) +{ +#ifdef CONFIG_X86_64 + if (__builtin_constant_p(msr)) + return __rdmsrq_constant(msr, val, type); +#endif + + return __rdmsrq_variable(msr, val, type); } static __always_inline void __wrmsrq(u32 msr, u64 val) @@ -188,7 +257,11 @@ static __always_inline bool __wrmsrns(u32 msr, u64 val, int type) static __always_inline u64 native_rdmsrq(u32 msr) { - return __rdmsr(msr); + u64 val; + + __rdmsr(msr, &val, EX_TYPE_RDMSR); + + return val; } static __always_inline void native_wrmsrq(u32 msr, u64 val) @@ -198,23 +271,12 @@ static __always_inline void native_wrmsrq(u32 msr, u64 val) static inline u64 native_read_msr(u32 msr) { - return __rdmsr(msr); + return native_rdmsrq(msr); } -static inline int native_read_msr_safe(u32 msr, u64 *p) +static inline int native_read_msr_safe(u32 msr, u64 *val) { - int err; - EAX_EDX_DECLARE_ARGS(val, low, high); - - asm volatile("1: rdmsr ; xor %[err],%[err]\n" - "2:\n\t" - _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_RDMSR_SAFE, %[err]) - : [err] "=r" (err), EAX_EDX_RET(val, low, high) - : "c" (msr)); - - *p = EAX_EDX_VAL(val, low, high); - - return err; + return __rdmsr(msr, val, EX_TYPE_RDMSR_SAFE) ? -EIO : 0; } /* Can be uninlined because referenced by paravirt */ -- 2.54.0