1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) "SMP alternatives: " fmt
4 #include <linux/module.h>
5 #include <linux/sched.h>
6 #include <linux/perf_event.h>
7 #include <linux/mutex.h>
8 #include <linux/list.h>
9 #include <linux/stringify.h>
10 #include <linux/highmem.h>
12 #include <linux/vmalloc.h>
13 #include <linux/memory.h>
14 #include <linux/stop_machine.h>
15 #include <linux/slab.h>
16 #include <linux/kdebug.h>
17 #include <linux/kprobes.h>
18 #include <linux/mmu_context.h>
19 #include <linux/bsearch.h>
20 #include <linux/sync_core.h>
21 #include <asm/text-patching.h>
22 #include <asm/alternative.h>
23 #include <asm/sections.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
30 #include <asm/fixmap.h>
31 #include <asm/paravirt.h>
32 #include <asm/asm-prototypes.h>
35 int __read_mostly alternatives_patched
;
37 EXPORT_SYMBOL_GPL(alternatives_patched
);
39 #define MAX_PATCH_LEN (255-1)
44 #define DA_RETPOLINE 0x04
48 static unsigned int debug_alternative
;
50 static int __init
debug_alt(char *str
)
52 if (str
&& *str
== '=')
55 if (!str
|| kstrtouint(str
, 0, &debug_alternative
))
56 debug_alternative
= DA_ALL
;
60 __setup("debug-alternative", debug_alt
);
62 static int noreplace_smp
;
64 static int __init
setup_noreplace_smp(char *str
)
69 __setup("noreplace-smp", setup_noreplace_smp
);
71 #define DPRINTK(type, fmt, args...) \
73 if (debug_alternative & DA_##type) \
74 printk(KERN_DEBUG pr_fmt(fmt) "\n", ##args); \
77 #define DUMP_BYTES(type, buf, len, fmt, args...) \
79 if (unlikely(debug_alternative & DA_##type)) { \
85 printk(KERN_DEBUG pr_fmt(fmt), ##args); \
86 for (j = 0; j < (len) - 1; j++) \
87 printk(KERN_CONT "%02hhx ", buf[j]); \
88 printk(KERN_CONT "%02hhx\n", buf[j]); \
92 static const unsigned char x86nops
[] =
109 const unsigned char * const x86_nops
[ASM_NOP_MAX
+1] =
116 x86nops
+ 1 + 2 + 3 + 4,
117 x86nops
+ 1 + 2 + 3 + 4 + 5,
118 x86nops
+ 1 + 2 + 3 + 4 + 5 + 6,
119 x86nops
+ 1 + 2 + 3 + 4 + 5 + 6 + 7,
121 x86nops
+ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
122 x86nops
+ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
123 x86nops
+ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
128 * Nomenclature for variable names to simplify and clarify this code and ease
129 * any potential staring at it:
131 * @instr: source address of the original instructions in the kernel text as
132 * generated by the compiler.
134 * @buf: temporary buffer on which the patching operates. This buffer is
135 * eventually text-poked into the kernel image.
137 * @replacement/@repl: pointer to the opcodes which are replacing @instr, located
138 * in the .altinstr_replacement section.
142 * Fill the buffer with a single effective instruction of size @len.
144 * In order not to issue an ORC stack depth tracking CFI entry (Call Frame Info)
145 * for every single-byte NOP, try to generate the maximally available NOP of
146 * size <= ASM_NOP_MAX such that only a single CFI entry is generated (vs one for
147 * each single-byte NOPs). If @len to fill out is > ASM_NOP_MAX, pad with INT3 and
148 * *jump* over instead of executing long and daft NOPs.
150 static void add_nop(u8
*buf
, unsigned int len
)
152 u8
*target
= buf
+ len
;
157 if (len
<= ASM_NOP_MAX
) {
158 memcpy(buf
, x86_nops
[len
], len
);
163 __text_gen_insn(buf
, JMP8_INSN_OPCODE
, buf
, target
, JMP8_INSN_SIZE
);
164 buf
+= JMP8_INSN_SIZE
;
166 __text_gen_insn(buf
, JMP32_INSN_OPCODE
, buf
, target
, JMP32_INSN_SIZE
);
167 buf
+= JMP32_INSN_SIZE
;
170 for (;buf
< target
; buf
++)
171 *buf
= INT3_INSN_OPCODE
;
174 extern s32 __retpoline_sites
[], __retpoline_sites_end
[];
175 extern s32 __return_sites
[], __return_sites_end
[];
176 extern s32 __cfi_sites
[], __cfi_sites_end
[];
177 extern s32 __ibt_endbr_seal
[], __ibt_endbr_seal_end
[];
178 extern s32 __smp_locks
[], __smp_locks_end
[];
179 void text_poke_early(void *addr
, const void *opcode
, size_t len
);
182 * Matches NOP and NOPL, not any of the other possible NOPs.
184 static bool insn_is_nop(struct insn
*insn
)
186 /* Anything NOP, but no REP NOP */
187 if (insn
->opcode
.bytes
[0] == 0x90 &&
188 (!insn
->prefixes
.nbytes
|| insn
->prefixes
.bytes
[0] != 0xF3))
192 if (insn
->opcode
.bytes
[0] == 0x0F && insn
->opcode
.bytes
[1] == 0x1F)
195 /* TODO: more nops */
201 * Find the offset of the first non-NOP instruction starting at @offset
202 * but no further than @len.
204 static int skip_nops(u8
*buf
, int offset
, int len
)
208 for (; offset
< len
; offset
+= insn
.length
) {
209 if (insn_decode_kernel(&insn
, &buf
[offset
]))
212 if (!insn_is_nop(&insn
))
220 * "noinline" to cause control flow change and thus invalidate I$ and
221 * cause refetch after modification.
223 static void noinline
optimize_nops(const u8
* const instr
, u8
*buf
, size_t len
)
225 for (int next
, i
= 0; i
< len
; i
= next
) {
228 if (insn_decode_kernel(&insn
, &buf
[i
]))
231 next
= i
+ insn
.length
;
233 if (insn_is_nop(&insn
)) {
236 /* Has the NOP already been optimized? */
237 if (i
+ insn
.length
== len
)
240 next
= skip_nops(buf
, next
, len
);
242 add_nop(buf
+ nop
, next
- nop
);
243 DUMP_BYTES(ALT
, buf
, len
, "%px: [%d:%d) optimized NOPs: ", instr
, nop
, next
);
249 * In this context, "source" is where the instructions are placed in the
250 * section .altinstr_replacement, for example during kernel build by the
252 * "Destination" is where the instructions are being patched in by this
255 * The source offset is:
257 * src_imm = target - src_next_ip (1)
259 * and the target offset is:
261 * dst_imm = target - dst_next_ip (2)
263 * so rework (1) as an expression for target like:
265 * target = src_imm + src_next_ip (1a)
267 * and substitute in (2) to get:
269 * dst_imm = (src_imm + src_next_ip) - dst_next_ip (3)
271 * Now, since the instruction stream is 'identical' at src and dst (it
272 * is being copied after all) it can be stated that:
274 * src_next_ip = src + ip_offset
275 * dst_next_ip = dst + ip_offset (4)
277 * Substitute (4) in (3) and observe ip_offset being cancelled out to
280 * dst_imm = src_imm + (src + ip_offset) - (dst + ip_offset)
281 * = src_imm + src - dst + ip_offset - ip_offset
282 * = src_imm + src - dst (5)
284 * IOW, only the relative displacement of the code block matters.
287 #define apply_reloc_n(n_, p_, d_) \
289 s32 v = *(s##n_ *)(p_); \
291 BUG_ON((v >> 31) != (v >> (n_-1))); \
292 *(s##n_ *)(p_) = (s##n_)v; \
296 static __always_inline
297 void apply_reloc(int n
, void *ptr
, uintptr_t diff
)
300 case 1: apply_reloc_n(8, ptr
, diff
); break;
301 case 2: apply_reloc_n(16, ptr
, diff
); break;
302 case 4: apply_reloc_n(32, ptr
, diff
); break;
307 static __always_inline
308 bool need_reloc(unsigned long offset
, u8
*src
, size_t src_len
)
310 u8
*target
= src
+ offset
;
312 * If the target is inside the patched block, it's relative to the
313 * block itself and does not need relocation.
315 return (target
< src
|| target
> src
+ src_len
);
318 static void __apply_relocation(u8
*buf
, const u8
* const instr
, size_t instrlen
, u8
*repl
, size_t repl_len
)
320 for (int next
, i
= 0; i
< instrlen
; i
= next
) {
323 if (WARN_ON_ONCE(insn_decode_kernel(&insn
, &buf
[i
])))
326 next
= i
+ insn
.length
;
328 switch (insn
.opcode
.bytes
[0]) {
330 if (insn
.opcode
.bytes
[1] < 0x80 ||
331 insn
.opcode
.bytes
[1] > 0x8f)
334 fallthrough
; /* Jcc.d32 */
335 case 0x70 ... 0x7f: /* Jcc.d8 */
336 case JMP8_INSN_OPCODE
:
337 case JMP32_INSN_OPCODE
:
338 case CALL_INSN_OPCODE
:
339 if (need_reloc(next
+ insn
.immediate
.value
, repl
, repl_len
)) {
340 apply_reloc(insn
.immediate
.nbytes
,
341 buf
+ i
+ insn_offset_immediate(&insn
),
346 * Where possible, convert JMP.d32 into JMP.d8.
348 if (insn
.opcode
.bytes
[0] == JMP32_INSN_OPCODE
) {
349 s32 imm
= insn
.immediate
.value
;
351 imm
+= JMP32_INSN_SIZE
- JMP8_INSN_SIZE
;
352 if ((imm
>> 31) == (imm
>> 7)) {
353 buf
[i
+0] = JMP8_INSN_OPCODE
;
356 memset(&buf
[i
+2], INT3_INSN_OPCODE
, insn
.length
- 2);
362 if (insn_rip_relative(&insn
)) {
363 if (need_reloc(next
+ insn
.displacement
.value
, repl
, repl_len
)) {
364 apply_reloc(insn
.displacement
.nbytes
,
365 buf
+ i
+ insn_offset_displacement(&insn
),
372 void apply_relocation(u8
*buf
, const u8
* const instr
, size_t instrlen
, u8
*repl
, size_t repl_len
)
374 __apply_relocation(buf
, instr
, instrlen
, repl
, repl_len
);
375 optimize_nops(instr
, buf
, instrlen
);
378 /* Low-level backend functions usable from alternative code replacements. */
379 DEFINE_ASM_FUNC(nop_func
, "", .entry
.text
);
380 EXPORT_SYMBOL_GPL(nop_func
);
382 noinstr
void BUG_func(void)
386 EXPORT_SYMBOL(BUG_func
);
388 #define CALL_RIP_REL_OPCODE 0xff
389 #define CALL_RIP_REL_MODRM 0x15
392 * Rewrite the "call BUG_func" replacement to point to the target of the
393 * indirect pv_ops call "call *disp(%ip)".
395 static int alt_replace_call(u8
*instr
, u8
*insn_buff
, struct alt_instr
*a
)
397 void *target
, *bug
= &BUG_func
;
400 if (a
->replacementlen
!= 5 || insn_buff
[0] != CALL_INSN_OPCODE
) {
401 pr_err("ALT_FLAG_DIRECT_CALL set for a non-call replacement instruction\n");
405 if (a
->instrlen
!= 6 ||
406 instr
[0] != CALL_RIP_REL_OPCODE
||
407 instr
[1] != CALL_RIP_REL_MODRM
) {
408 pr_err("ALT_FLAG_DIRECT_CALL set for unrecognized indirect call\n");
412 /* Skip CALL_RIP_REL_OPCODE and CALL_RIP_REL_MODRM */
413 disp
= *(s32
*)(instr
+ 2);
415 /* ff 15 00 00 00 00 call *0x0(%rip) */
416 /* target address is stored at "next instruction + disp". */
417 target
= *(void **)(instr
+ a
->instrlen
+ disp
);
419 /* ff 15 00 00 00 00 call *0x0 */
420 /* target address is stored at disp. */
421 target
= *(void **)disp
;
426 /* (BUG_func - .) + (target - BUG_func) := target - . */
427 *(s32
*)(insn_buff
+ 1) += target
- bug
;
429 if (target
== &nop_func
)
435 static inline u8
* instr_va(struct alt_instr
*i
)
437 return (u8
*)&i
->instr_offset
+ i
->instr_offset
;
441 * Replace instructions with better alternatives for this CPU type. This runs
442 * before SMP is initialized to avoid SMP problems with self modifying code.
443 * This implies that asymmetric systems where APs have less capabilities than
444 * the boot processor are not handled. Tough. Make sure you disable such
447 * Marked "noinline" to cause control flow change and thus insn cache
448 * to refetch changed I$ lines.
450 void __init_or_module noinline
apply_alternatives(struct alt_instr
*start
,
451 struct alt_instr
*end
)
453 u8 insn_buff
[MAX_PATCH_LEN
];
454 u8
*instr
, *replacement
;
455 struct alt_instr
*a
, *b
;
457 DPRINTK(ALT
, "alt table %px, -> %px", start
, end
);
460 * In the case CONFIG_X86_5LEVEL=y, KASAN_SHADOW_START is defined using
461 * cpu_feature_enabled(X86_FEATURE_LA57) and is therefore patched here.
462 * During the process, KASAN becomes confused seeing partial LA57
463 * conversion and triggers a false-positive out-of-bound report.
465 * Disable KASAN until the patching is complete.
467 kasan_disable_current();
470 * The scan order should be from start to end. A later scanned
471 * alternative code can overwrite previously scanned alternative code.
472 * Some kernel functions (e.g. memcpy, memset, etc) use this order to
475 * So be careful if you want to change the scan order to any other
478 for (a
= start
; a
< end
; a
++) {
479 int insn_buff_sz
= 0;
482 * In case of nested ALTERNATIVE()s the outer alternative might
483 * add more padding. To ensure consistent patching find the max
484 * padding for all alt_instr entries for this site (nested
485 * alternatives result in consecutive entries).
487 for (b
= a
+1; b
< end
&& instr_va(b
) == instr_va(a
); b
++) {
488 u8 len
= max(a
->instrlen
, b
->instrlen
);
489 a
->instrlen
= b
->instrlen
= len
;
493 replacement
= (u8
*)&a
->repl_offset
+ a
->repl_offset
;
494 BUG_ON(a
->instrlen
> sizeof(insn_buff
));
495 BUG_ON(a
->cpuid
>= (NCAPINTS
+ NBUGINTS
) * 32);
499 * - feature is present
500 * - feature not present but ALT_FLAG_NOT is set to mean,
501 * patch if feature is *NOT* present.
503 if (!boot_cpu_has(a
->cpuid
) == !(a
->flags
& ALT_FLAG_NOT
)) {
504 memcpy(insn_buff
, instr
, a
->instrlen
);
505 optimize_nops(instr
, insn_buff
, a
->instrlen
);
506 text_poke_early(instr
, insn_buff
, a
->instrlen
);
510 DPRINTK(ALT
, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x",
513 instr
, instr
, a
->instrlen
,
514 replacement
, a
->replacementlen
, a
->flags
);
516 memcpy(insn_buff
, replacement
, a
->replacementlen
);
517 insn_buff_sz
= a
->replacementlen
;
519 if (a
->flags
& ALT_FLAG_DIRECT_CALL
) {
520 insn_buff_sz
= alt_replace_call(instr
, insn_buff
, a
);
521 if (insn_buff_sz
< 0)
525 for (; insn_buff_sz
< a
->instrlen
; insn_buff_sz
++)
526 insn_buff
[insn_buff_sz
] = 0x90;
528 apply_relocation(insn_buff
, instr
, a
->instrlen
, replacement
, a
->replacementlen
);
530 DUMP_BYTES(ALT
, instr
, a
->instrlen
, "%px: old_insn: ", instr
);
531 DUMP_BYTES(ALT
, replacement
, a
->replacementlen
, "%px: rpl_insn: ", replacement
);
532 DUMP_BYTES(ALT
, insn_buff
, insn_buff_sz
, "%px: final_insn: ", instr
);
534 text_poke_early(instr
, insn_buff
, insn_buff_sz
);
537 kasan_enable_current();
540 static inline bool is_jcc32(struct insn
*insn
)
542 /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */
543 return insn
->opcode
.bytes
[0] == 0x0f && (insn
->opcode
.bytes
[1] & 0xf0) == 0x80;
546 #if defined(CONFIG_MITIGATION_RETPOLINE) && defined(CONFIG_OBJTOOL)
551 static int emit_indirect(int op
, int reg
, u8
*bytes
)
557 case CALL_INSN_OPCODE
:
558 modrm
= 0x10; /* Reg = 2; CALL r/m */
561 case JMP32_INSN_OPCODE
:
562 modrm
= 0x20; /* Reg = 4; JMP r/m */
571 bytes
[i
++] = 0x41; /* REX.B prefix */
575 modrm
|= 0xc0; /* Mod = 3 */
578 bytes
[i
++] = 0xff; /* opcode */
584 static int emit_call_track_retpoline(void *addr
, struct insn
*insn
, int reg
, u8
*bytes
)
586 u8 op
= insn
->opcode
.bytes
[0];
590 * Clang does 'weird' Jcc __x86_indirect_thunk_r11 conditional
591 * tail-calls. Deal with them.
593 if (is_jcc32(insn
)) {
595 op
= insn
->opcode
.bytes
[1];
599 if (insn
->length
== 6)
600 bytes
[i
++] = 0x2e; /* CS-prefix */
603 case CALL_INSN_OPCODE
:
604 __text_gen_insn(bytes
+i
, op
, addr
+i
,
605 __x86_indirect_call_thunk_array
[reg
],
610 case JMP32_INSN_OPCODE
:
612 __text_gen_insn(bytes
+i
, op
, addr
+i
,
613 __x86_indirect_jump_thunk_array
[reg
],
615 i
+= JMP32_INSN_SIZE
;
619 WARN(1, "%pS %px %*ph\n", addr
, addr
, 6, addr
);
623 WARN_ON_ONCE(i
!= insn
->length
);
629 * Rewrite the compiler generated retpoline thunk calls.
631 * For spectre_v2=off (!X86_FEATURE_RETPOLINE), rewrite them into immediate
632 * indirect instructions, avoiding the extra indirection.
634 * For example, convert:
636 * CALL __x86_indirect_thunk_\reg
642 * It also tries to inline spectre_v2=retpoline,lfence when size permits.
644 static int patch_retpoline(void *addr
, struct insn
*insn
, u8
*bytes
)
646 retpoline_thunk_t
*target
;
650 target
= addr
+ insn
->length
+ insn
->immediate
.value
;
651 reg
= target
- __x86_indirect_thunk_array
;
653 if (WARN_ON_ONCE(reg
& ~0xf))
656 /* If anyone ever does: CALL/JMP *%rsp, we're in deep trouble. */
659 if (cpu_feature_enabled(X86_FEATURE_RETPOLINE
) &&
660 !cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE
)) {
661 if (cpu_feature_enabled(X86_FEATURE_CALL_DEPTH
))
662 return emit_call_track_retpoline(addr
, insn
, reg
, bytes
);
667 op
= insn
->opcode
.bytes
[0];
672 * Jcc.d32 __x86_indirect_thunk_\reg
682 if (is_jcc32(insn
)) {
683 cc
= insn
->opcode
.bytes
[1] & 0xf;
684 cc
^= 1; /* invert condition */
686 bytes
[i
++] = 0x70 + cc
; /* Jcc.d8 */
687 bytes
[i
++] = insn
->length
- 2; /* sizeof(Jcc.d8) == 2 */
689 /* Continue as if: JMP.d32 __x86_indirect_thunk_\reg */
690 op
= JMP32_INSN_OPCODE
;
694 * For RETPOLINE_LFENCE: prepend the indirect CALL/JMP with an LFENCE.
696 if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE
)) {
699 bytes
[i
++] = 0xe8; /* LFENCE */
702 ret
= emit_indirect(op
, reg
, bytes
+ i
);
708 * The compiler is supposed to EMIT an INT3 after every unconditional
709 * JMP instruction due to AMD BTC. However, if the compiler is too old
710 * or MITIGATION_SLS isn't enabled, we still need an INT3 after
711 * indirect JMPs even on Intel.
713 if (op
== JMP32_INSN_OPCODE
&& i
< insn
->length
)
714 bytes
[i
++] = INT3_INSN_OPCODE
;
716 for (; i
< insn
->length
;)
717 bytes
[i
++] = BYTES_NOP1
;
723 * Generated by 'objtool --retpoline'.
725 void __init_or_module noinline
apply_retpolines(s32
*start
, s32
*end
)
729 for (s
= start
; s
< end
; s
++) {
730 void *addr
= (void *)s
+ *s
;
736 ret
= insn_decode_kernel(&insn
, addr
);
737 if (WARN_ON_ONCE(ret
< 0))
740 op1
= insn
.opcode
.bytes
[0];
741 op2
= insn
.opcode
.bytes
[1];
744 case CALL_INSN_OPCODE
:
745 case JMP32_INSN_OPCODE
:
748 case 0x0f: /* escape */
749 if (op2
>= 0x80 && op2
<= 0x8f)
757 DPRINTK(RETPOLINE
, "retpoline at: %pS (%px) len: %d to: %pS",
758 addr
, addr
, insn
.length
,
759 addr
+ insn
.length
+ insn
.immediate
.value
);
761 len
= patch_retpoline(addr
, &insn
, bytes
);
762 if (len
== insn
.length
) {
763 optimize_nops(addr
, bytes
, len
);
764 DUMP_BYTES(RETPOLINE
, ((u8
*)addr
), len
, "%px: orig: ", addr
);
765 DUMP_BYTES(RETPOLINE
, ((u8
*)bytes
), len
, "%px: repl: ", addr
);
766 text_poke_early(addr
, bytes
, len
);
771 #ifdef CONFIG_MITIGATION_RETHUNK
774 * Rewrite the compiler generated return thunk tail-calls.
776 * For example, convert:
778 * JMP __x86_return_thunk
784 static int patch_return(void *addr
, struct insn
*insn
, u8
*bytes
)
788 /* Patch the custom return thunks... */
789 if (cpu_feature_enabled(X86_FEATURE_RETHUNK
)) {
791 __text_gen_insn(bytes
, JMP32_INSN_OPCODE
, addr
, x86_return_thunk
, i
);
793 /* ... or patch them out if not needed. */
794 bytes
[i
++] = RET_INSN_OPCODE
;
797 for (; i
< insn
->length
;)
798 bytes
[i
++] = INT3_INSN_OPCODE
;
802 void __init_or_module noinline
apply_returns(s32
*start
, s32
*end
)
806 if (cpu_feature_enabled(X86_FEATURE_RETHUNK
))
807 static_call_force_reinit();
809 for (s
= start
; s
< end
; s
++) {
810 void *dest
= NULL
, *addr
= (void *)s
+ *s
;
816 ret
= insn_decode_kernel(&insn
, addr
);
817 if (WARN_ON_ONCE(ret
< 0))
820 op
= insn
.opcode
.bytes
[0];
821 if (op
== JMP32_INSN_OPCODE
)
822 dest
= addr
+ insn
.length
+ insn
.immediate
.value
;
824 if (__static_call_fixup(addr
, op
, dest
) ||
825 WARN_ONCE(dest
!= &__x86_return_thunk
,
826 "missing return thunk: %pS-%pS: %*ph",
827 addr
, dest
, 5, addr
))
830 DPRINTK(RET
, "return thunk at: %pS (%px) len: %d to: %pS",
831 addr
, addr
, insn
.length
,
832 addr
+ insn
.length
+ insn
.immediate
.value
);
834 len
= patch_return(addr
, &insn
, bytes
);
835 if (len
== insn
.length
) {
836 DUMP_BYTES(RET
, ((u8
*)addr
), len
, "%px: orig: ", addr
);
837 DUMP_BYTES(RET
, ((u8
*)bytes
), len
, "%px: repl: ", addr
);
838 text_poke_early(addr
, bytes
, len
);
843 void __init_or_module noinline
apply_returns(s32
*start
, s32
*end
) { }
844 #endif /* CONFIG_MITIGATION_RETHUNK */
846 #else /* !CONFIG_MITIGATION_RETPOLINE || !CONFIG_OBJTOOL */
848 void __init_or_module noinline
apply_retpolines(s32
*start
, s32
*end
) { }
849 void __init_or_module noinline
apply_returns(s32
*start
, s32
*end
) { }
851 #endif /* CONFIG_MITIGATION_RETPOLINE && CONFIG_OBJTOOL */
853 #ifdef CONFIG_X86_KERNEL_IBT
855 static void poison_cfi(void *addr
);
857 static void __init_or_module
poison_endbr(void *addr
, bool warn
)
859 u32 endbr
, poison
= gen_endbr_poison();
861 if (WARN_ON_ONCE(get_kernel_nofault(endbr
, addr
)))
864 if (!is_endbr(endbr
)) {
869 DPRINTK(ENDBR
, "ENDBR at: %pS (%px)", addr
, addr
);
872 * When we have IBT, the lack of ENDBR will trigger #CP
874 DUMP_BYTES(ENDBR
, ((u8
*)addr
), 4, "%px: orig: ", addr
);
875 DUMP_BYTES(ENDBR
, ((u8
*)&poison
), 4, "%px: repl: ", addr
);
876 text_poke_early(addr
, &poison
, 4);
880 * Generated by: objtool --ibt
882 * Seal the functions for indirect calls by clobbering the ENDBR instructions
883 * and the kCFI hash value.
885 void __init_or_module noinline
apply_seal_endbr(s32
*start
, s32
*end
)
889 for (s
= start
; s
< end
; s
++) {
890 void *addr
= (void *)s
+ *s
;
892 poison_endbr(addr
, true);
893 if (IS_ENABLED(CONFIG_FINEIBT
))
894 poison_cfi(addr
- 16);
900 void __init_or_module
apply_seal_endbr(s32
*start
, s32
*end
) { }
902 #endif /* CONFIG_X86_KERNEL_IBT */
904 #ifdef CONFIG_CFI_AUTO_DEFAULT
905 #define __CFI_DEFAULT CFI_AUTO
906 #elif defined(CONFIG_CFI_CLANG)
907 #define __CFI_DEFAULT CFI_KCFI
909 #define __CFI_DEFAULT CFI_OFF
912 enum cfi_mode cfi_mode __ro_after_init
= __CFI_DEFAULT
;
914 #ifdef CONFIG_CFI_CLANG
917 /* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
918 extern unsigned int __bpf_prog_runX(const void *ctx
,
919 const struct bpf_insn
*insn
);
922 * Force a reference to the external symbol so the compiler generates
925 __ADDRESSABLE(__bpf_prog_runX
);
927 /* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
929 " .pushsection .data..ro_after_init,\"aw\",@progbits \n"
930 " .type cfi_bpf_hash,@object \n"
931 " .globl cfi_bpf_hash \n"
932 " .p2align 2, 0x0 \n"
934 " .long __kcfi_typeid___bpf_prog_runX \n"
935 " .size cfi_bpf_hash, 4 \n"
939 /* Must match bpf_callback_t */
940 extern u64
__bpf_callback_fn(u64
, u64
, u64
, u64
, u64
);
942 __ADDRESSABLE(__bpf_callback_fn
);
944 /* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
946 " .pushsection .data..ro_after_init,\"aw\",@progbits \n"
947 " .type cfi_bpf_subprog_hash,@object \n"
948 " .globl cfi_bpf_subprog_hash \n"
949 " .p2align 2, 0x0 \n"
950 "cfi_bpf_subprog_hash: \n"
951 " .long __kcfi_typeid___bpf_callback_fn \n"
952 " .size cfi_bpf_subprog_hash, 4 \n"
956 u32
cfi_get_func_hash(void *func
)
960 func
-= cfi_get_offset();
972 if (get_kernel_nofault(hash
, func
))
979 #ifdef CONFIG_FINEIBT
981 static bool cfi_rand __ro_after_init
= true;
982 static u32 cfi_seed __ro_after_init
;
985 * Re-hash the CFI hash with a boot-time seed while making sure the result is
986 * not a valid ENDBR instruction.
988 static u32
cfi_rehash(u32 hash
)
991 while (unlikely(is_endbr(hash
) || is_endbr(-hash
))) {
1000 static __init
int cfi_parse_cmdline(char *str
)
1006 char *next
= strchr(str
, ',');
1012 if (!strcmp(str
, "auto")) {
1013 cfi_mode
= CFI_AUTO
;
1014 } else if (!strcmp(str
, "off")) {
1017 } else if (!strcmp(str
, "kcfi")) {
1018 cfi_mode
= CFI_KCFI
;
1019 } else if (!strcmp(str
, "fineibt")) {
1020 cfi_mode
= CFI_FINEIBT
;
1021 } else if (!strcmp(str
, "norand")) {
1024 pr_err("Ignoring unknown cfi option (%s).", str
);
1032 early_param("cfi", cfi_parse_cmdline
);
1037 * __cfi_\func: __cfi_\func:
1038 * movl $0x12345678,%eax // 5 endbr64 // 4
1039 * nop subl $0x12345678,%r10d // 7
1053 * movl $(-0x12345678),%r10d // 6 movl $0x12345678,%r10d // 6
1054 * addl $-15(%r11),%r10d // 4 sub $16,%r11 // 4
1055 * je 1f // 2 nop4 // 4
1057 * 1: call __x86_indirect_thunk_r11 // 5 call *%r11; nop2; // 5
1061 asm( ".pushsection .rodata \n"
1062 "fineibt_preamble_start: \n"
1064 " subl $0x12345678, %r10d \n"
1065 " je fineibt_preamble_end \n"
1068 "fineibt_preamble_end: \n"
1072 extern u8 fineibt_preamble_start
[];
1073 extern u8 fineibt_preamble_end
[];
1075 #define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
1076 #define fineibt_preamble_hash 7
1078 asm( ".pushsection .rodata \n"
1079 "fineibt_caller_start: \n"
1080 " movl $0x12345678, %r10d \n"
1083 "fineibt_caller_end: \n"
1087 extern u8 fineibt_caller_start
[];
1088 extern u8 fineibt_caller_end
[];
1090 #define fineibt_caller_size (fineibt_caller_end - fineibt_caller_start)
1091 #define fineibt_caller_hash 2
1093 #define fineibt_caller_jmp (fineibt_caller_size - 2)
1095 static u32
decode_preamble_hash(void *addr
)
1099 /* b8 78 56 34 12 mov $0x12345678,%eax */
1101 return *(u32
*)(addr
+ 1);
1103 return 0; /* invalid hash value */
1106 static u32
decode_caller_hash(void *addr
)
1110 /* 41 ba 78 56 34 12 mov $0x12345678,%r10d */
1111 if (p
[0] == 0x41 && p
[1] == 0xba)
1112 return -*(u32
*)(addr
+ 2);
1114 /* e8 0c 78 56 34 12 jmp.d8 +12 */
1115 if (p
[0] == JMP8_INSN_OPCODE
&& p
[1] == fineibt_caller_jmp
)
1116 return -*(u32
*)(addr
+ 2);
1118 return 0; /* invalid hash value */
1121 /* .retpoline_sites */
1122 static int cfi_disable_callers(s32
*start
, s32
*end
)
1125 * Disable kCFI by patching in a JMP.d8, this leaves the hash immediate
1126 * in tact for later usage. Also see decode_caller_hash() and
1127 * cfi_rewrite_callers().
1129 const u8 jmp
[] = { JMP8_INSN_OPCODE
, fineibt_caller_jmp
};
1132 for (s
= start
; s
< end
; s
++) {
1133 void *addr
= (void *)s
+ *s
;
1136 addr
-= fineibt_caller_size
;
1137 hash
= decode_caller_hash(addr
);
1138 if (!hash
) /* nocfi callers */
1141 text_poke_early(addr
, jmp
, 2);
1147 static int cfi_enable_callers(s32
*start
, s32
*end
)
1150 * Re-enable kCFI, undo what cfi_disable_callers() did.
1152 const u8 mov
[] = { 0x41, 0xba };
1155 for (s
= start
; s
< end
; s
++) {
1156 void *addr
= (void *)s
+ *s
;
1159 addr
-= fineibt_caller_size
;
1160 hash
= decode_caller_hash(addr
);
1161 if (!hash
) /* nocfi callers */
1164 text_poke_early(addr
, mov
, 2);
1171 static int cfi_rand_preamble(s32
*start
, s32
*end
)
1175 for (s
= start
; s
< end
; s
++) {
1176 void *addr
= (void *)s
+ *s
;
1179 hash
= decode_preamble_hash(addr
);
1180 if (WARN(!hash
, "no CFI hash found at: %pS %px %*ph\n",
1181 addr
, addr
, 5, addr
))
1184 hash
= cfi_rehash(hash
);
1185 text_poke_early(addr
+ 1, &hash
, 4);
1191 static int cfi_rewrite_preamble(s32
*start
, s32
*end
)
1195 for (s
= start
; s
< end
; s
++) {
1196 void *addr
= (void *)s
+ *s
;
1199 hash
= decode_preamble_hash(addr
);
1200 if (WARN(!hash
, "no CFI hash found at: %pS %px %*ph\n",
1201 addr
, addr
, 5, addr
))
1204 text_poke_early(addr
, fineibt_preamble_start
, fineibt_preamble_size
);
1205 WARN_ON(*(u32
*)(addr
+ fineibt_preamble_hash
) != 0x12345678);
1206 text_poke_early(addr
+ fineibt_preamble_hash
, &hash
, 4);
1212 static void cfi_rewrite_endbr(s32
*start
, s32
*end
)
1216 for (s
= start
; s
< end
; s
++) {
1217 void *addr
= (void *)s
+ *s
;
1219 poison_endbr(addr
+16, false);
1223 /* .retpoline_sites */
1224 static int cfi_rand_callers(s32
*start
, s32
*end
)
1228 for (s
= start
; s
< end
; s
++) {
1229 void *addr
= (void *)s
+ *s
;
1232 addr
-= fineibt_caller_size
;
1233 hash
= decode_caller_hash(addr
);
1235 hash
= -cfi_rehash(hash
);
1236 text_poke_early(addr
+ 2, &hash
, 4);
1243 static int cfi_rewrite_callers(s32
*start
, s32
*end
)
1247 for (s
= start
; s
< end
; s
++) {
1248 void *addr
= (void *)s
+ *s
;
1251 addr
-= fineibt_caller_size
;
1252 hash
= decode_caller_hash(addr
);
1254 text_poke_early(addr
, fineibt_caller_start
, fineibt_caller_size
);
1255 WARN_ON(*(u32
*)(addr
+ fineibt_caller_hash
) != 0x12345678);
1256 text_poke_early(addr
+ fineibt_caller_hash
, &hash
, 4);
1258 /* rely on apply_retpolines() */
1264 static void __apply_fineibt(s32
*start_retpoline
, s32
*end_retpoline
,
1265 s32
*start_cfi
, s32
*end_cfi
, bool builtin
)
1269 if (WARN_ONCE(fineibt_preamble_size
!= 16,
1270 "FineIBT preamble wrong size: %ld", fineibt_preamble_size
))
1273 if (cfi_mode
== CFI_AUTO
) {
1274 cfi_mode
= CFI_KCFI
;
1275 if (HAS_KERNEL_IBT
&& cpu_feature_enabled(X86_FEATURE_IBT
))
1276 cfi_mode
= CFI_FINEIBT
;
1280 * Rewrite the callers to not use the __cfi_ stubs, such that we might
1281 * rewrite them. This disables all CFI. If this succeeds but any of the
1282 * later stages fails, we're without CFI.
1284 ret
= cfi_disable_callers(start_retpoline
, end_retpoline
);
1290 cfi_seed
= get_random_u32();
1291 cfi_bpf_hash
= cfi_rehash(cfi_bpf_hash
);
1292 cfi_bpf_subprog_hash
= cfi_rehash(cfi_bpf_subprog_hash
);
1295 ret
= cfi_rand_preamble(start_cfi
, end_cfi
);
1299 ret
= cfi_rand_callers(start_retpoline
, end_retpoline
);
1307 pr_info("Disabling CFI\n");
1311 ret
= cfi_enable_callers(start_retpoline
, end_retpoline
);
1316 pr_info("Using kCFI\n");
1320 /* place the FineIBT preamble at func()-16 */
1321 ret
= cfi_rewrite_preamble(start_cfi
, end_cfi
);
1325 /* rewrite the callers to target func()-16 */
1326 ret
= cfi_rewrite_callers(start_retpoline
, end_retpoline
);
1330 /* now that nobody targets func()+0, remove ENDBR there */
1331 cfi_rewrite_endbr(start_cfi
, end_cfi
);
1334 pr_info("Using FineIBT CFI\n");
1342 pr_err("Something went horribly wrong trying to rewrite the CFI implementation.\n");
1345 static inline void poison_hash(void *addr
)
1350 static void poison_cfi(void *addr
)
1362 poison_endbr(addr
, false);
1363 poison_hash(addr
+ fineibt_preamble_hash
);
1372 poison_hash(addr
+ 1);
1382 static void __apply_fineibt(s32
*start_retpoline
, s32
*end_retpoline
,
1383 s32
*start_cfi
, s32
*end_cfi
, bool builtin
)
1387 #ifdef CONFIG_X86_KERNEL_IBT
1388 static void poison_cfi(void *addr
) { }
1393 void apply_fineibt(s32
*start_retpoline
, s32
*end_retpoline
,
1394 s32
*start_cfi
, s32
*end_cfi
)
1396 return __apply_fineibt(start_retpoline
, end_retpoline
,
1398 /* .builtin = */ false);
1402 static void alternatives_smp_lock(const s32
*start
, const s32
*end
,
1403 u8
*text
, u8
*text_end
)
1407 for (poff
= start
; poff
< end
; poff
++) {
1408 u8
*ptr
= (u8
*)poff
+ *poff
;
1410 if (!*poff
|| ptr
< text
|| ptr
>= text_end
)
1412 /* turn DS segment override prefix into lock prefix */
1414 text_poke(ptr
, ((unsigned char []){0xf0}), 1);
1418 static void alternatives_smp_unlock(const s32
*start
, const s32
*end
,
1419 u8
*text
, u8
*text_end
)
1423 for (poff
= start
; poff
< end
; poff
++) {
1424 u8
*ptr
= (u8
*)poff
+ *poff
;
1426 if (!*poff
|| ptr
< text
|| ptr
>= text_end
)
1428 /* turn lock prefix into DS segment override prefix */
1430 text_poke(ptr
, ((unsigned char []){0x3E}), 1);
1434 struct smp_alt_module
{
1435 /* what is this ??? */
1439 /* ptrs to lock prefixes */
1441 const s32
*locks_end
;
1443 /* .text segment, needed to avoid patching init code ;) */
1447 struct list_head next
;
1449 static LIST_HEAD(smp_alt_modules
);
1450 static bool uniproc_patched
= false; /* protected by text_mutex */
1452 void __init_or_module
alternatives_smp_module_add(struct module
*mod
,
1454 void *locks
, void *locks_end
,
1455 void *text
, void *text_end
)
1457 struct smp_alt_module
*smp
;
1459 mutex_lock(&text_mutex
);
1460 if (!uniproc_patched
)
1463 if (num_possible_cpus() == 1)
1464 /* Don't bother remembering, we'll never have to undo it. */
1467 smp
= kzalloc(sizeof(*smp
), GFP_KERNEL
);
1469 /* we'll run the (safe but slow) SMP code then ... */
1475 smp
->locks_end
= locks_end
;
1477 smp
->text_end
= text_end
;
1478 DPRINTK(SMP
, "locks %p -> %p, text %p -> %p, name %s\n",
1479 smp
->locks
, smp
->locks_end
,
1480 smp
->text
, smp
->text_end
, smp
->name
);
1482 list_add_tail(&smp
->next
, &smp_alt_modules
);
1484 alternatives_smp_unlock(locks
, locks_end
, text
, text_end
);
1486 mutex_unlock(&text_mutex
);
1489 void __init_or_module
alternatives_smp_module_del(struct module
*mod
)
1491 struct smp_alt_module
*item
;
1493 mutex_lock(&text_mutex
);
1494 list_for_each_entry(item
, &smp_alt_modules
, next
) {
1495 if (mod
!= item
->mod
)
1497 list_del(&item
->next
);
1501 mutex_unlock(&text_mutex
);
1504 void alternatives_enable_smp(void)
1506 struct smp_alt_module
*mod
;
1508 /* Why bother if there are no other CPUs? */
1509 BUG_ON(num_possible_cpus() == 1);
1511 mutex_lock(&text_mutex
);
1513 if (uniproc_patched
) {
1514 pr_info("switching to SMP code\n");
1515 BUG_ON(num_online_cpus() != 1);
1516 clear_cpu_cap(&boot_cpu_data
, X86_FEATURE_UP
);
1517 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP
);
1518 list_for_each_entry(mod
, &smp_alt_modules
, next
)
1519 alternatives_smp_lock(mod
->locks
, mod
->locks_end
,
1520 mod
->text
, mod
->text_end
);
1521 uniproc_patched
= false;
1523 mutex_unlock(&text_mutex
);
1527 * Return 1 if the address range is reserved for SMP-alternatives.
1528 * Must hold text_mutex.
1530 int alternatives_text_reserved(void *start
, void *end
)
1532 struct smp_alt_module
*mod
;
1534 u8
*text_start
= start
;
1537 lockdep_assert_held(&text_mutex
);
1539 list_for_each_entry(mod
, &smp_alt_modules
, next
) {
1540 if (mod
->text
> text_end
|| mod
->text_end
< text_start
)
1542 for (poff
= mod
->locks
; poff
< mod
->locks_end
; poff
++) {
1543 const u8
*ptr
= (const u8
*)poff
+ *poff
;
1545 if (text_start
<= ptr
&& text_end
> ptr
)
1552 #endif /* CONFIG_SMP */
1555 * Self-test for the INT3 based CALL emulation code.
1557 * This exercises int3_emulate_call() to make sure INT3 pt_regs are set up
1558 * properly and that there is a stack gap between the INT3 frame and the
1559 * previous context. Without this gap doing a virtual PUSH on the interrupted
1560 * stack would corrupt the INT3 IRET frame.
1562 * See entry_{32,64}.S for more details.
1566 * We define the int3_magic() function in assembly to control the calling
1567 * convention such that we can 'call' it from assembly.
1570 extern void int3_magic(unsigned int *ptr
); /* defined in asm */
1573 " .pushsection .init.text, \"ax\", @progbits\n"
1574 " .type int3_magic, @function\n"
1577 " movl $1, (%" _ASM_ARG1
")\n"
1579 " .size int3_magic, .-int3_magic\n"
1583 extern void int3_selftest_ip(void); /* defined in asm below */
1586 int3_exception_notify(struct notifier_block
*self
, unsigned long val
, void *data
)
1588 unsigned long selftest
= (unsigned long)&int3_selftest_ip
;
1589 struct die_args
*args
= data
;
1590 struct pt_regs
*regs
= args
->regs
;
1592 OPTIMIZER_HIDE_VAR(selftest
);
1594 if (!regs
|| user_mode(regs
))
1597 if (val
!= DIE_INT3
)
1600 if (regs
->ip
- INT3_INSN_SIZE
!= selftest
)
1603 int3_emulate_call(regs
, (unsigned long)&int3_magic
);
1607 /* Must be noinline to ensure uniqueness of int3_selftest_ip. */
1608 static noinline
void __init
int3_selftest(void)
1610 static __initdata
struct notifier_block int3_exception_nb
= {
1611 .notifier_call
= int3_exception_notify
,
1612 .priority
= INT_MAX
-1, /* last */
1614 unsigned int val
= 0;
1616 BUG_ON(register_die_notifier(&int3_exception_nb
));
1619 * Basically: int3_magic(&val); but really complicated :-)
1621 * INT3 padded with NOP to CALL_INSN_SIZE. The int3_exception_nb
1622 * notifier above will emulate CALL for us.
1624 asm volatile ("int3_selftest_ip:\n\t"
1626 " int3; nop; nop; nop; nop\n\t"
1627 : ASM_CALL_CONSTRAINT
1628 : __ASM_SEL_RAW(a
, D
) (&val
)
1633 unregister_die_notifier(&int3_exception_nb
);
1636 static __initdata
int __alt_reloc_selftest_addr
;
1638 extern void __init
__alt_reloc_selftest(void *arg
);
1639 __visible noinline
void __init
__alt_reloc_selftest(void *arg
)
1641 WARN_ON(arg
!= &__alt_reloc_selftest_addr
);
1644 static noinline
void __init
alt_reloc_selftest(void)
1647 * Tests apply_relocation().
1649 * This has a relative immediate (CALL) in a place other than the first
1650 * instruction and additionally on x86_64 we get a RIP-relative LEA:
1652 * lea 0x0(%rip),%rdi # 5d0: R_X86_64_PC32 .init.data+0x5566c
1653 * call +0 # 5d5: R_X86_64_PLT32 __alt_reloc_selftest-0x4
1655 * Getting this wrong will either crash and burn or tickle the WARN
1658 asm_inline
volatile (
1659 ALTERNATIVE("", "lea %[mem], %%" _ASM_ARG1
"; call __alt_reloc_selftest;", X86_FEATURE_ALWAYS
)
1660 : ASM_CALL_CONSTRAINT
1661 : [mem
] "m" (__alt_reloc_selftest_addr
)
1666 void __init
alternative_instructions(void)
1671 * The patching is not fully atomic, so try to avoid local
1672 * interruptions that might execute the to be patched code.
1673 * Other CPUs are not running.
1678 * Don't stop machine check exceptions while patching.
1679 * MCEs only happen when something got corrupted and in this
1680 * case we must do something about the corruption.
1681 * Ignoring it is worse than an unlikely patching race.
1682 * Also machine checks tend to be broadcast and if one CPU
1683 * goes into machine check the others follow quickly, so we don't
1684 * expect a machine check to cause undue problems during to code
1689 * Make sure to set (artificial) features depending on used paravirt
1690 * functions which can later influence alternative patching.
1694 __apply_fineibt(__retpoline_sites
, __retpoline_sites_end
,
1695 __cfi_sites
, __cfi_sites_end
, true);
1698 * Rewrite the retpolines, must be done before alternatives since
1699 * those can rewrite the retpoline thunks.
1701 apply_retpolines(__retpoline_sites
, __retpoline_sites_end
);
1702 apply_returns(__return_sites
, __return_sites_end
);
1704 apply_alternatives(__alt_instructions
, __alt_instructions_end
);
1707 * Now all calls are established. Apply the call thunks if
1710 callthunks_patch_builtin_calls();
1713 * Seal all functions that do not have their address taken.
1715 apply_seal_endbr(__ibt_endbr_seal
, __ibt_endbr_seal_end
);
1718 /* Patch to UP if other cpus not imminent. */
1719 if (!noreplace_smp
&& (num_present_cpus() == 1 || setup_max_cpus
<= 1)) {
1720 uniproc_patched
= true;
1721 alternatives_smp_module_add(NULL
, "core kernel",
1722 __smp_locks
, __smp_locks_end
,
1726 if (!uniproc_patched
|| num_possible_cpus() == 1) {
1727 free_init_pages("SMP alternatives",
1728 (unsigned long)__smp_locks
,
1729 (unsigned long)__smp_locks_end
);
1734 alternatives_patched
= 1;
1736 alt_reloc_selftest();
1740 * text_poke_early - Update instructions on a live kernel at boot time
1741 * @addr: address to modify
1742 * @opcode: source of the copy
1743 * @len: length to copy
1745 * When you use this code to patch more than one byte of an instruction
1746 * you need to make sure that other CPUs cannot execute this code in parallel.
1747 * Also no thread must be currently preempted in the middle of these
1748 * instructions. And on the local CPU you need to be protected against NMI or
1749 * MCE handlers seeing an inconsistent instruction while you patch.
1751 void __init_or_module
text_poke_early(void *addr
, const void *opcode
,
1754 unsigned long flags
;
1756 if (boot_cpu_has(X86_FEATURE_NX
) &&
1757 is_module_text_address((unsigned long)addr
)) {
1759 * Modules text is marked initially as non-executable, so the
1760 * code cannot be running and speculative code-fetches are
1761 * prevented. Just change the code.
1763 memcpy(addr
, opcode
, len
);
1765 local_irq_save(flags
);
1766 memcpy(addr
, opcode
, len
);
1768 local_irq_restore(flags
);
1771 * Could also do a CLFLUSH here to speed up CPU recovery; but
1772 * that causes hangs on some VIA CPUs.
1778 struct mm_struct
*mm
;
1782 * Using a temporary mm allows to set temporary mappings that are not accessible
1783 * by other CPUs. Such mappings are needed to perform sensitive memory writes
1784 * that override the kernel memory protections (e.g., W^X), without exposing the
1785 * temporary page-table mappings that are required for these write operations to
1786 * other CPUs. Using a temporary mm also allows to avoid TLB shootdowns when the
1787 * mapping is torn down.
1789 * Context: The temporary mm needs to be used exclusively by a single core. To
1790 * harden security IRQs must be disabled while the temporary mm is
1791 * loaded, thereby preventing interrupt handler bugs from overriding
1792 * the kernel memory protection.
1794 static inline temp_mm_state_t
use_temporary_mm(struct mm_struct
*mm
)
1796 temp_mm_state_t temp_state
;
1798 lockdep_assert_irqs_disabled();
1801 * Make sure not to be in TLB lazy mode, as otherwise we'll end up
1802 * with a stale address space WITHOUT being in lazy mode after
1803 * restoring the previous mm.
1805 if (this_cpu_read(cpu_tlbstate_shared
.is_lazy
))
1808 temp_state
.mm
= this_cpu_read(cpu_tlbstate
.loaded_mm
);
1809 switch_mm_irqs_off(NULL
, mm
, current
);
1812 * If breakpoints are enabled, disable them while the temporary mm is
1813 * used. Userspace might set up watchpoints on addresses that are used
1814 * in the temporary mm, which would lead to wrong signals being sent or
1817 * Note that breakpoints are not disabled selectively, which also causes
1818 * kernel breakpoints (e.g., perf's) to be disabled. This might be
1819 * undesirable, but still seems reasonable as the code that runs in the
1820 * temporary mm should be short.
1822 if (hw_breakpoint_active())
1823 hw_breakpoint_disable();
1828 static inline void unuse_temporary_mm(temp_mm_state_t prev_state
)
1830 lockdep_assert_irqs_disabled();
1831 switch_mm_irqs_off(NULL
, prev_state
.mm
, current
);
1834 * Restore the breakpoints if they were disabled before the temporary mm
1837 if (hw_breakpoint_active())
1838 hw_breakpoint_restore();
1841 __ro_after_init
struct mm_struct
*poking_mm
;
1842 __ro_after_init
unsigned long poking_addr
;
1844 static void text_poke_memcpy(void *dst
, const void *src
, size_t len
)
1846 memcpy(dst
, src
, len
);
1849 static void text_poke_memset(void *dst
, const void *src
, size_t len
)
1851 int c
= *(const int *)src
;
1853 memset(dst
, c
, len
);
1856 typedef void text_poke_f(void *dst
, const void *src
, size_t len
);
1858 static void *__text_poke(text_poke_f func
, void *addr
, const void *src
, size_t len
)
1860 bool cross_page_boundary
= offset_in_page(addr
) + len
> PAGE_SIZE
;
1861 struct page
*pages
[2] = {NULL
};
1862 temp_mm_state_t prev
;
1863 unsigned long flags
;
1869 * While boot memory allocator is running we cannot use struct pages as
1870 * they are not yet initialized. There is no way to recover.
1872 BUG_ON(!after_bootmem
);
1874 if (!core_kernel_text((unsigned long)addr
)) {
1875 pages
[0] = vmalloc_to_page(addr
);
1876 if (cross_page_boundary
)
1877 pages
[1] = vmalloc_to_page(addr
+ PAGE_SIZE
);
1879 pages
[0] = virt_to_page(addr
);
1880 WARN_ON(!PageReserved(pages
[0]));
1881 if (cross_page_boundary
)
1882 pages
[1] = virt_to_page(addr
+ PAGE_SIZE
);
1885 * If something went wrong, crash and burn since recovery paths are not
1888 BUG_ON(!pages
[0] || (cross_page_boundary
&& !pages
[1]));
1891 * Map the page without the global bit, as TLB flushing is done with
1892 * flush_tlb_mm_range(), which is intended for non-global PTEs.
1894 pgprot
= __pgprot(pgprot_val(PAGE_KERNEL
) & ~_PAGE_GLOBAL
);
1897 * The lock is not really needed, but this allows to avoid open-coding.
1899 ptep
= get_locked_pte(poking_mm
, poking_addr
, &ptl
);
1902 * This must not fail; preallocated in poking_init().
1906 local_irq_save(flags
);
1908 pte
= mk_pte(pages
[0], pgprot
);
1909 set_pte_at(poking_mm
, poking_addr
, ptep
, pte
);
1911 if (cross_page_boundary
) {
1912 pte
= mk_pte(pages
[1], pgprot
);
1913 set_pte_at(poking_mm
, poking_addr
+ PAGE_SIZE
, ptep
+ 1, pte
);
1917 * Loading the temporary mm behaves as a compiler barrier, which
1918 * guarantees that the PTE will be set at the time memcpy() is done.
1920 prev
= use_temporary_mm(poking_mm
);
1922 kasan_disable_current();
1923 func((u8
*)poking_addr
+ offset_in_page(addr
), src
, len
);
1924 kasan_enable_current();
1927 * Ensure that the PTE is only cleared after the instructions of memcpy
1928 * were issued by using a compiler barrier.
1932 pte_clear(poking_mm
, poking_addr
, ptep
);
1933 if (cross_page_boundary
)
1934 pte_clear(poking_mm
, poking_addr
+ PAGE_SIZE
, ptep
+ 1);
1937 * Loading the previous page-table hierarchy requires a serializing
1938 * instruction that already allows the core to see the updated version.
1939 * Xen-PV is assumed to serialize execution in a similar manner.
1941 unuse_temporary_mm(prev
);
1944 * Flushing the TLB might involve IPIs, which would require enabled
1945 * IRQs, but not if the mm is not used, as it is in this point.
1947 flush_tlb_mm_range(poking_mm
, poking_addr
, poking_addr
+
1948 (cross_page_boundary
? 2 : 1) * PAGE_SIZE
,
1951 if (func
== text_poke_memcpy
) {
1953 * If the text does not match what we just wrote then something is
1954 * fundamentally screwy; there's nothing we can really do about that.
1956 BUG_ON(memcmp(addr
, src
, len
));
1959 local_irq_restore(flags
);
1960 pte_unmap_unlock(ptep
, ptl
);
1965 * text_poke - Update instructions on a live kernel
1966 * @addr: address to modify
1967 * @opcode: source of the copy
1968 * @len: length to copy
1970 * Only atomic text poke/set should be allowed when not doing early patching.
1971 * It means the size must be writable atomically and the address must be aligned
1972 * in a way that permits an atomic write. It also makes sure we fit on a single
1975 * Note that the caller must ensure that if the modified code is part of a
1976 * module, the module would not be removed during poking. This can be achieved
1977 * by registering a module notifier, and ordering module removal and patching
1980 void *text_poke(void *addr
, const void *opcode
, size_t len
)
1982 lockdep_assert_held(&text_mutex
);
1984 return __text_poke(text_poke_memcpy
, addr
, opcode
, len
);
1988 * text_poke_kgdb - Update instructions on a live kernel by kgdb
1989 * @addr: address to modify
1990 * @opcode: source of the copy
1991 * @len: length to copy
1993 * Only atomic text poke/set should be allowed when not doing early patching.
1994 * It means the size must be writable atomically and the address must be aligned
1995 * in a way that permits an atomic write. It also makes sure we fit on a single
1998 * Context: should only be used by kgdb, which ensures no other core is running,
1999 * despite the fact it does not hold the text_mutex.
2001 void *text_poke_kgdb(void *addr
, const void *opcode
, size_t len
)
2003 return __text_poke(text_poke_memcpy
, addr
, opcode
, len
);
2006 void *text_poke_copy_locked(void *addr
, const void *opcode
, size_t len
,
2009 unsigned long start
= (unsigned long)addr
;
2012 if (WARN_ON_ONCE(!core_ok
&& core_kernel_text(start
)))
2015 while (patched
< len
) {
2016 unsigned long ptr
= start
+ patched
;
2019 s
= min_t(size_t, PAGE_SIZE
* 2 - offset_in_page(ptr
), len
- patched
);
2021 __text_poke(text_poke_memcpy
, (void *)ptr
, opcode
+ patched
, s
);
2028 * text_poke_copy - Copy instructions into (an unused part of) RX memory
2029 * @addr: address to modify
2030 * @opcode: source of the copy
2031 * @len: length to copy, could be more than 2x PAGE_SIZE
2033 * Not safe against concurrent execution; useful for JITs to dump
2034 * new code blocks into unused regions of RX memory. Can be used in
2035 * conjunction with synchronize_rcu_tasks() to wait for existing
2036 * execution to quiesce after having made sure no existing functions
2037 * pointers are live.
2039 void *text_poke_copy(void *addr
, const void *opcode
, size_t len
)
2041 mutex_lock(&text_mutex
);
2042 addr
= text_poke_copy_locked(addr
, opcode
, len
, false);
2043 mutex_unlock(&text_mutex
);
2048 * text_poke_set - memset into (an unused part of) RX memory
2049 * @addr: address to modify
2050 * @c: the byte to fill the area with
2051 * @len: length to copy, could be more than 2x PAGE_SIZE
2053 * This is useful to overwrite unused regions of RX memory with illegal
2056 void *text_poke_set(void *addr
, int c
, size_t len
)
2058 unsigned long start
= (unsigned long)addr
;
2061 if (WARN_ON_ONCE(core_kernel_text(start
)))
2064 mutex_lock(&text_mutex
);
2065 while (patched
< len
) {
2066 unsigned long ptr
= start
+ patched
;
2069 s
= min_t(size_t, PAGE_SIZE
* 2 - offset_in_page(ptr
), len
- patched
);
2071 __text_poke(text_poke_memset
, (void *)ptr
, (void *)&c
, s
);
2074 mutex_unlock(&text_mutex
);
2078 static void do_sync_core(void *info
)
2083 void text_poke_sync(void)
2085 on_each_cpu(do_sync_core
, NULL
, 1);
2089 * NOTE: crazy scheme to allow patching Jcc.d32 but not increase the size of
2090 * this thing. When len == 6 everything is prefixed with 0x0f and we map
2091 * opcode to Jcc.d8, using len to distinguish.
2093 struct text_poke_loc
{
2094 /* addr := _stext + rel_addr */
2099 const u8 text
[POKE_MAX_OPCODE_SIZE
];
2100 /* see text_poke_bp_batch() */
2104 struct bp_patching_desc
{
2105 struct text_poke_loc
*vec
;
2110 static struct bp_patching_desc bp_desc
;
2112 static __always_inline
2113 struct bp_patching_desc
*try_get_desc(void)
2115 struct bp_patching_desc
*desc
= &bp_desc
;
2117 if (!raw_atomic_inc_not_zero(&desc
->refs
))
2123 static __always_inline
void put_desc(void)
2125 struct bp_patching_desc
*desc
= &bp_desc
;
2127 smp_mb__before_atomic();
2128 raw_atomic_dec(&desc
->refs
);
2131 static __always_inline
void *text_poke_addr(struct text_poke_loc
*tp
)
2133 return _stext
+ tp
->rel_addr
;
2136 static __always_inline
int patch_cmp(const void *key
, const void *elt
)
2138 struct text_poke_loc
*tp
= (struct text_poke_loc
*) elt
;
2140 if (key
< text_poke_addr(tp
))
2142 if (key
> text_poke_addr(tp
))
2147 noinstr
int poke_int3_handler(struct pt_regs
*regs
)
2149 struct bp_patching_desc
*desc
;
2150 struct text_poke_loc
*tp
;
2154 if (user_mode(regs
))
2158 * Having observed our INT3 instruction, we now must observe
2159 * bp_desc with non-zero refcount:
2161 * bp_desc.refs = 1 INT3
2163 * write INT3 if (bp_desc.refs != 0)
2167 desc
= try_get_desc();
2172 * Discount the INT3. See text_poke_bp_batch().
2174 ip
= (void *) regs
->ip
- INT3_INSN_SIZE
;
2177 * Skip the binary search if there is a single member in the vector.
2179 if (unlikely(desc
->nr_entries
> 1)) {
2180 tp
= __inline_bsearch(ip
, desc
->vec
, desc
->nr_entries
,
2181 sizeof(struct text_poke_loc
),
2187 if (text_poke_addr(tp
) != ip
)
2193 switch (tp
->opcode
) {
2194 case INT3_INSN_OPCODE
:
2196 * Someone poked an explicit INT3, they'll want to handle it,
2201 case RET_INSN_OPCODE
:
2202 int3_emulate_ret(regs
);
2205 case CALL_INSN_OPCODE
:
2206 int3_emulate_call(regs
, (long)ip
+ tp
->disp
);
2209 case JMP32_INSN_OPCODE
:
2210 case JMP8_INSN_OPCODE
:
2211 int3_emulate_jmp(regs
, (long)ip
+ tp
->disp
);
2214 case 0x70 ... 0x7f: /* Jcc */
2215 int3_emulate_jcc(regs
, tp
->opcode
& 0xf, (long)ip
, tp
->disp
);
2229 #define TP_VEC_MAX (PAGE_SIZE / sizeof(struct text_poke_loc))
2230 static struct text_poke_loc tp_vec
[TP_VEC_MAX
];
2231 static int tp_vec_nr
;
2234 * text_poke_bp_batch() -- update instructions on live kernel on SMP
2235 * @tp: vector of instructions to patch
2236 * @nr_entries: number of entries in the vector
2238 * Modify multi-byte instruction by using int3 breakpoint on SMP.
2239 * We completely avoid stop_machine() here, and achieve the
2240 * synchronization using int3 breakpoint.
2242 * The way it is done:
2243 * - For each entry in the vector:
2244 * - add a int3 trap to the address that will be patched
2246 * - For each entry in the vector:
2247 * - update all but the first byte of the patched range
2249 * - For each entry in the vector:
2250 * - replace the first byte (int3) by the first byte of
2254 static void text_poke_bp_batch(struct text_poke_loc
*tp
, unsigned int nr_entries
)
2256 unsigned char int3
= INT3_INSN_OPCODE
;
2260 lockdep_assert_held(&text_mutex
);
2263 bp_desc
.nr_entries
= nr_entries
;
2266 * Corresponds to the implicit memory barrier in try_get_desc() to
2267 * ensure reading a non-zero refcount provides up to date bp_desc data.
2269 atomic_set_release(&bp_desc
.refs
, 1);
2272 * Function tracing can enable thousands of places that need to be
2273 * updated. This can take quite some time, and with full kernel debugging
2274 * enabled, this could cause the softlockup watchdog to trigger.
2275 * This function gets called every 256 entries added to be patched.
2276 * Call cond_resched() here to make sure that other tasks can get scheduled
2277 * while processing all the functions being patched.
2282 * Corresponding read barrier in int3 notifier for making sure the
2283 * nr_entries and handler are correctly ordered wrt. patching.
2288 * First step: add a int3 trap to the address that will be patched.
2290 for (i
= 0; i
< nr_entries
; i
++) {
2291 tp
[i
].old
= *(u8
*)text_poke_addr(&tp
[i
]);
2292 text_poke(text_poke_addr(&tp
[i
]), &int3
, INT3_INSN_SIZE
);
2298 * Second step: update all but the first byte of the patched range.
2300 for (do_sync
= 0, i
= 0; i
< nr_entries
; i
++) {
2301 u8 old
[POKE_MAX_OPCODE_SIZE
+1] = { tp
[i
].old
, };
2302 u8 _new
[POKE_MAX_OPCODE_SIZE
+1];
2303 const u8
*new = tp
[i
].text
;
2304 int len
= tp
[i
].len
;
2306 if (len
- INT3_INSN_SIZE
> 0) {
2307 memcpy(old
+ INT3_INSN_SIZE
,
2308 text_poke_addr(&tp
[i
]) + INT3_INSN_SIZE
,
2309 len
- INT3_INSN_SIZE
);
2313 memcpy(_new
+ 1, new, 5);
2317 text_poke(text_poke_addr(&tp
[i
]) + INT3_INSN_SIZE
,
2318 new + INT3_INSN_SIZE
,
2319 len
- INT3_INSN_SIZE
);
2325 * Emit a perf event to record the text poke, primarily to
2326 * support Intel PT decoding which must walk the executable code
2327 * to reconstruct the trace. The flow up to here is:
2330 * - write instruction tail
2331 * At this point the actual control flow will be through the
2332 * INT3 and handler and not hit the old or new instruction.
2333 * Intel PT outputs FUP/TIP packets for the INT3, so the flow
2334 * can still be decoded. Subsequently:
2335 * - emit RECORD_TEXT_POKE with the new instruction
2337 * - write first byte
2339 * So before the text poke event timestamp, the decoder will see
2340 * either the old instruction flow or FUP/TIP of INT3. After the
2341 * text poke event timestamp, the decoder will see either the
2342 * new instruction flow or FUP/TIP of INT3. Thus decoders can
2343 * use the timestamp as the point at which to modify the
2345 * The old instruction is recorded so that the event can be
2346 * processed forwards or backwards.
2348 perf_event_text_poke(text_poke_addr(&tp
[i
]), old
, len
, new, len
);
2353 * According to Intel, this core syncing is very likely
2354 * not necessary and we'd be safe even without it. But
2355 * better safe than sorry (plus there's not only Intel).
2361 * Third step: replace the first byte (int3) by the first byte of
2364 for (do_sync
= 0, i
= 0; i
< nr_entries
; i
++) {
2365 u8 byte
= tp
[i
].text
[0];
2370 if (byte
== INT3_INSN_OPCODE
)
2373 text_poke(text_poke_addr(&tp
[i
]), &byte
, INT3_INSN_SIZE
);
2381 * Remove and wait for refs to be zero.
2383 if (!atomic_dec_and_test(&bp_desc
.refs
))
2384 atomic_cond_read_acquire(&bp_desc
.refs
, !VAL
);
2387 static void text_poke_loc_init(struct text_poke_loc
*tp
, void *addr
,
2388 const void *opcode
, size_t len
, const void *emulate
)
2395 memcpy((void *)tp
->text
, opcode
+i
, len
-i
);
2399 ret
= insn_decode_kernel(&insn
, emulate
);
2402 tp
->rel_addr
= addr
- (void *)_stext
;
2404 tp
->opcode
= insn
.opcode
.bytes
[0];
2406 if (is_jcc32(&insn
)) {
2408 * Map Jcc.d32 onto Jcc.d8 and use len to distinguish.
2410 tp
->opcode
= insn
.opcode
.bytes
[1] - 0x10;
2413 switch (tp
->opcode
) {
2414 case RET_INSN_OPCODE
:
2415 case JMP32_INSN_OPCODE
:
2416 case JMP8_INSN_OPCODE
:
2418 * Control flow instructions without implied execution of the
2419 * next instruction can be padded with INT3.
2421 for (i
= insn
.length
; i
< len
; i
++)
2422 BUG_ON(tp
->text
[i
] != INT3_INSN_OPCODE
);
2426 BUG_ON(len
!= insn
.length
);
2429 switch (tp
->opcode
) {
2430 case INT3_INSN_OPCODE
:
2431 case RET_INSN_OPCODE
:
2434 case CALL_INSN_OPCODE
:
2435 case JMP32_INSN_OPCODE
:
2436 case JMP8_INSN_OPCODE
:
2437 case 0x70 ... 0x7f: /* Jcc */
2438 tp
->disp
= insn
.immediate
.value
;
2441 default: /* assume NOP */
2443 case 2: /* NOP2 -- emulate as JMP8+0 */
2444 BUG_ON(memcmp(emulate
, x86_nops
[len
], len
));
2445 tp
->opcode
= JMP8_INSN_OPCODE
;
2449 case 5: /* NOP5 -- emulate as JMP32+0 */
2450 BUG_ON(memcmp(emulate
, x86_nops
[len
], len
));
2451 tp
->opcode
= JMP32_INSN_OPCODE
;
2455 default: /* unknown instruction */
2463 * We hard rely on the tp_vec being ordered; ensure this is so by flushing
2466 static bool tp_order_fail(void *addr
)
2468 struct text_poke_loc
*tp
;
2473 if (!addr
) /* force */
2476 tp
= &tp_vec
[tp_vec_nr
- 1];
2477 if ((unsigned long)text_poke_addr(tp
) > (unsigned long)addr
)
2483 static void text_poke_flush(void *addr
)
2485 if (tp_vec_nr
== TP_VEC_MAX
|| tp_order_fail(addr
)) {
2486 text_poke_bp_batch(tp_vec
, tp_vec_nr
);
2491 void text_poke_finish(void)
2493 text_poke_flush(NULL
);
2496 void __ref
text_poke_queue(void *addr
, const void *opcode
, size_t len
, const void *emulate
)
2498 struct text_poke_loc
*tp
;
2500 text_poke_flush(addr
);
2502 tp
= &tp_vec
[tp_vec_nr
++];
2503 text_poke_loc_init(tp
, addr
, opcode
, len
, emulate
);
2507 * text_poke_bp() -- update instructions on live kernel on SMP
2508 * @addr: address to patch
2509 * @opcode: opcode of new instruction
2510 * @len: length to copy
2511 * @emulate: instruction to be emulated
2513 * Update a single instruction with the vector in the stack, avoiding
2514 * dynamically allocated memory. This function should be used when it is
2515 * not possible to allocate memory.
2517 void __ref
text_poke_bp(void *addr
, const void *opcode
, size_t len
, const void *emulate
)
2519 struct text_poke_loc tp
;
2521 text_poke_loc_init(&tp
, addr
, opcode
, len
, emulate
);
2522 text_poke_bp_batch(&tp
, 1);