From: Juergen Gross <jgross@suse.com>
To: linux-kernel@vger.kernel.org, x86@kernel.org
Cc: Juergen Gross <jgross@suse.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>
Subject: [RFC PATCH 2/3] x86/alternative: add indirect call patching
Date: Thu, 8 Jun 2023 16:03:32 +0200 [thread overview]
Message-ID: <20230608140333.4083-3-jgross@suse.com> (raw)
In-Reply-To: <20230608140333.4083-1-jgross@suse.com>
In order to prepare replacing of paravirt patching with alternative
patching, add the capability to replace an indirect call with a direct
one to alternative patching.
This is done via a new flag ALT_FLAG_CALL as the target of the call
instruction needs to be evaluated using the value of the location
addressed by the indirect call. For convenience add a macro for a
default call instruction. In case it is being used without the new
flag being set, it will result in a BUG() when being executed. As in
most cases the feature used will be X86_FEATURE_ALWAYS add another
macro ALT_CALL_ALWAYS usable for the flags parameter of the ALTERNATIVE
macros.
For a complete replacement handle the special cases of calling a nop
function and an indirect call of NULL the same way as paravirt does.
Signed-off-by: Juergen Gross <jgross@suse.com>
---
arch/x86/include/asm/alternative.h | 5 ++++
arch/x86/kernel/alternative.c | 37 +++++++++++++++++++++++++++---
2 files changed, 39 insertions(+), 3 deletions(-)
diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h
index a5a4944ce5d1..8e0871c1631a 100644
--- a/arch/x86/include/asm/alternative.h
+++ b/arch/x86/include/asm/alternative.h
@@ -10,6 +10,9 @@
#define ALT_FLAG_NOT (1 << 0)
#define ALT_NOT(feature) ((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature))
+#define ALT_FLAG_CALL (1 << 1)
+#define ALT_CALL(feature) ((ALT_FLAG_CALL << ALT_FLAGS_SHIFT) | (feature))
+#define ALT_CALL_ALWAYS ALT_CALL(X86_FEATURE_ALWAYS)
#ifndef __ASSEMBLY__
@@ -155,6 +158,8 @@ static inline int alternatives_text_reserved(void *start, void *end)
}
#endif /* CONFIG_SMP */
+#define ALT_CALL_INSTR "call x86_BUG"
+
#define b_replacement(num) "664"#num
#define e_replacement(num) "665"#num
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index b7c70479fe58..f0371304c1b5 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -321,19 +321,50 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
insn_buff_sz = a->replacementlen;
/*
- * 0xe8 is a relative jump; fix the offset.
+ * 0xe8 is a relative call; fix the offset.
*
* Instruction length is checked before the opcode to avoid
* accessing uninitialized bytes for zero-length replacements.
*/
if (a->replacementlen == 5 && *insn_buff == 0xe8) {
- *(s32 *)(insn_buff + 1) += replacement - instr;
+ s32 *displ = (s32 *)(insn_buff + 1);
+
+ if (a->flags & ALT_FLAG_CALL) {
+ u8 *f_ptr;
+
+ if (a->instrlen != 6 ||
+ (instr[0] != 0xff && instr[1] != 0x15)) {
+ pr_err("Alternative: ALT_FLAG_CALL set for unrecognized indirect call\n");
+ pr_err(" Not replacing instruction at %pS (%px)\n",
+ instr, instr);
+ continue;
+ }
+
+ f_ptr = *(u8 **)(instr + a->instrlen +
+ *(s32 *)(instr + 2));
+
+ /* Replace calls of NULL with explicit BUG(). */
+ if (!f_ptr)
+ f_ptr = (u8 *)x86_BUG;
+
+ /* Replace a nop call with nops. */
+ if (f_ptr == (u8 *)x86_nop)
+ insn_buff_sz = 0;
+ else
+ *displ = f_ptr - (instr + insn_buff_sz);
+ } else {
+ *displ += replacement - instr;
+ }
DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
*(s32 *)(insn_buff + 1),
(unsigned long)instr + *(s32 *)(insn_buff + 1) + 5);
+ } else if (a->flags & ALT_FLAG_CALL) {
+ pr_err("Alternative: ALT_FLAG_CALL set for a non-call replacement instruction\n");
+ pr_err(" Ignoring the flag for instruction at %pS (%px)\n",
+ instr, instr);
}
- if (a->replacementlen && is_jmp(replacement[0]))
+ if (insn_buff_sz && is_jmp(replacement[0]))
recompute_jump(a, instr, replacement, insn_buff);
for (; insn_buff_sz < a->instrlen; insn_buff_sz++)
--
2.35.3
next prev parent reply other threads:[~2023-06-08 14:03 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-08 14:03 [RFC PATCH 0/3] x86/paravirt: Get rid of paravirt patching Juergen Gross
2023-06-08 14:03 ` [RFC PATCH 1/3] x86/paravirt: move some functions and defines to alternative Juergen Gross
2023-09-20 14:52 ` Peter Zijlstra
2023-06-08 14:03 ` Juergen Gross [this message]
2023-09-20 14:44 ` [RFC PATCH 2/3] x86/alternative: add indirect call patching Peter Zijlstra
2023-06-08 14:03 ` [RFC PATCH 3/3] x86/paravirt: switch mixed paravirt/alternative calls to alternative_2 Juergen Gross
2023-09-20 14:52 ` Peter Zijlstra
2023-09-20 15:49 ` Juergen Gross
2023-07-10 12:29 ` [RFC PATCH 0/3] x86/paravirt: Get rid of paravirt patching Juergen Gross
2023-08-24 9:01 ` Juergen Gross
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230608140333.4083-3-jgross@suse.com \
--to=jgross@suse.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®