1 // SPDX-License-Identifier: GPL-2.0
3 * jump label x86 support
5 * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
8 #include <linux/jump_label.h>
9 #include <linux/memory.h>
10 #include <linux/uaccess.h>
11 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <linux/jhash.h>
14 #include <linux/cpu.h>
15 #include <asm/kprobes.h>
16 #include <asm/alternative.h>
17 #include <asm/text-patching.h>
20 __jump_label_set_jump_code(struct jump_entry *entry, enum jump_label_type type)
22 const void *expect, *code;
23 const void *addr, *dest;
25 addr = (void *)jump_entry_code(entry);
26 dest = (void *)jump_entry_target(entry);
28 code = text_gen_insn(JMP32_INSN_OPCODE, addr, dest);
30 if (type == JUMP_LABEL_JMP)
35 if (memcmp(addr, expect, JUMP_LABEL_NOP_SIZE)) {
37 * The location is not an op that we were expecting.
38 * Something went wrong. Crash the box, as something could be
39 * corrupting the kernel.
41 pr_crit("jump_label: Fatal kernel bug, unexpected op at %pS [%p] (%5ph != %5ph)) type:%d\n",
42 addr, addr, addr, expect, type);
46 if (type == JUMP_LABEL_NOP)
52 static inline void __jump_label_transform(struct jump_entry *entry,
53 enum jump_label_type type,
56 const void *opcode = __jump_label_set_jump_code(entry, type);
59 * As long as only a single processor is running and the code is still
60 * not marked as RO, text_poke_early() can be used; Checking that
61 * system_state is SYSTEM_BOOTING guarantees it. It will be set to
62 * SYSTEM_SCHEDULING before other cores are awaken and before the
63 * code is write-protected.
65 * At the time the change is being done, just ignore whether we
66 * are doing nop -> jump or jump -> nop transition, and assume
67 * always nop being the 'currently valid' instruction
69 if (init || system_state == SYSTEM_BOOTING) {
70 text_poke_early((void *)jump_entry_code(entry), opcode,
75 text_poke_bp((void *)jump_entry_code(entry), opcode, JUMP_LABEL_NOP_SIZE, NULL);
78 static void __ref jump_label_transform(struct jump_entry *entry,
79 enum jump_label_type type,
82 mutex_lock(&text_mutex);
83 __jump_label_transform(entry, type, init);
84 mutex_unlock(&text_mutex);
87 void arch_jump_label_transform(struct jump_entry *entry,
88 enum jump_label_type type)
90 jump_label_transform(entry, type, 0);
93 bool arch_jump_label_transform_queue(struct jump_entry *entry,
94 enum jump_label_type type)
98 if (system_state == SYSTEM_BOOTING) {
100 * Fallback to the non-batching mode.
102 arch_jump_label_transform(entry, type);
106 mutex_lock(&text_mutex);
107 opcode = __jump_label_set_jump_code(entry, type);
108 text_poke_queue((void *)jump_entry_code(entry),
109 opcode, JUMP_LABEL_NOP_SIZE, NULL);
110 mutex_unlock(&text_mutex);
114 void arch_jump_label_transform_apply(void)
116 mutex_lock(&text_mutex);
118 mutex_unlock(&text_mutex);
125 } jlstate __initdata_or_module = JL_STATE_START;
127 __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry,
128 enum jump_label_type type)
130 if (jlstate == JL_STATE_UPDATE)
131 jump_label_transform(entry, type, 1);