Skip to content

Commit da0fe6e

Browse files
jgross1bp3tk0v
authored andcommitted
x86/alternative: Add indirect call patching
In order to prepare replacing of paravirt patching with alternative patching, add the capability to replace an indirect call with a direct one. 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 so 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. [ bp: Massage commit message, fixup the debug output and clarify flow more. ] Co-developed-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Juergen Gross <jgross@suse.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/r/20231210062138.2417-4-jgross@suse.com
1 parent 9824b00 commit da0fe6e

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

arch/x86/include/asm/alternative.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#define ALT_FLAG_NOT (1 << 0)
1212
#define ALT_NOT(feature) ((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature))
13+
#define ALT_FLAG_DIRECT_CALL (1 << 1)
14+
#define ALT_DIRECT_CALL(feature) ((ALT_FLAG_DIRECT_CALL << ALT_FLAGS_SHIFT) | (feature))
15+
#define ALT_CALL_ALWAYS ALT_DIRECT_CALL(X86_FEATURE_ALWAYS)
1316

1417
#ifndef __ASSEMBLY__
1518

@@ -150,6 +153,8 @@ static inline int alternatives_text_reserved(void *start, void *end)
150153
}
151154
#endif /* CONFIG_SMP */
152155

156+
#define ALT_CALL_INSTR "call BUG_func"
157+
153158
#define b_replacement(num) "664"#num
154159
#define e_replacement(num) "665"#num
155160

@@ -386,6 +391,10 @@ void nop_func(void);
386391
.byte \alt_len
387392
.endm
388393

394+
.macro ALT_CALL_INSTR
395+
call BUG_func
396+
.endm
397+
389398
/*
390399
* Define an alternative between two instructions. If @feature is
391400
* present, early code in apply_alternatives() replaces @oldinstr with

arch/x86/kernel/alternative.c

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,53 @@ noinstr void BUG_func(void)
395395
}
396396
EXPORT_SYMBOL_GPL(BUG_func);
397397

398+
#define CALL_RIP_REL_OPCODE 0xff
399+
#define CALL_RIP_REL_MODRM 0x15
400+
401+
/*
402+
* Rewrite the "call BUG_func" replacement to point to the target of the
403+
* indirect pv_ops call "call *disp(%ip)".
404+
*/
405+
static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a)
406+
{
407+
void *target, *bug = &BUG_func;
408+
s32 disp;
409+
410+
if (a->replacementlen != 5 || insn_buff[0] != CALL_INSN_OPCODE) {
411+
pr_err("ALT_FLAG_DIRECT_CALL set for a non-call replacement instruction\n");
412+
BUG();
413+
}
414+
415+
if (a->instrlen != 6 ||
416+
instr[0] != CALL_RIP_REL_OPCODE ||
417+
instr[1] != CALL_RIP_REL_MODRM) {
418+
pr_err("ALT_FLAG_DIRECT_CALL set for unrecognized indirect call\n");
419+
BUG();
420+
}
421+
422+
/* Skip CALL_RIP_REL_OPCODE and CALL_RIP_REL_MODRM */
423+
disp = *(s32 *)(instr + 2);
424+
#ifdef CONFIG_X86_64
425+
/* ff 15 00 00 00 00 call *0x0(%rip) */
426+
/* target address is stored at "next instruction + disp". */
427+
target = *(void **)(instr + a->instrlen + disp);
428+
#else
429+
/* ff 15 00 00 00 00 call *0x0 */
430+
/* target address is stored at disp. */
431+
target = *(void **)disp;
432+
#endif
433+
if (!target)
434+
target = bug;
435+
436+
/* (BUG_func - .) + (target - BUG_func) := target - . */
437+
*(s32 *)(insn_buff + 1) += target - bug;
438+
439+
if (target == &nop_func)
440+
return 0;
441+
442+
return 5;
443+
}
444+
398445
/*
399446
* Replace instructions with better alternatives for this CPU type. This runs
400447
* before SMP is initialized to avoid SMP problems with self modifying code.
@@ -452,16 +499,21 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
452499
continue;
453500
}
454501

455-
DPRINTK(ALT, "feat: %s%d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d)",
456-
(a->flags & ALT_FLAG_NOT) ? "!" : "",
502+
DPRINTK(ALT, "feat: %d32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x",
457503
a->cpuid >> 5,
458504
a->cpuid & 0x1f,
459505
instr, instr, a->instrlen,
460-
replacement, a->replacementlen);
506+
replacement, a->replacementlen, a->flags);
461507

462508
memcpy(insn_buff, replacement, a->replacementlen);
463509
insn_buff_sz = a->replacementlen;
464510

511+
if (a->flags & ALT_FLAG_DIRECT_CALL) {
512+
insn_buff_sz = alt_replace_call(instr, insn_buff, a);
513+
if (insn_buff_sz < 0)
514+
continue;
515+
}
516+
465517
for (; insn_buff_sz < a->instrlen; insn_buff_sz++)
466518
insn_buff[insn_buff_sz] = 0x90;
467519

0 commit comments

Comments
 (0)