1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * jump label x86 support
4 *
5 * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
6 *
7 */
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>
18
19 union jump_code_union {
20 char code[JUMP_LABEL_NOP_SIZE];
21 struct {
22 char jump;
23 int offset;
24 } __attribute__((packed));
25 };
26
bug_at(unsigned char * ip,int line)27 static void bug_at(unsigned char *ip, int line)
28 {
29 /*
30 * The location is not an op that we were expecting.
31 * Something went wrong. Crash the box, as something could be
32 * corrupting the kernel.
33 */
34 pr_crit("jump_label: Fatal kernel bug, unexpected op at %pS [%p] (%5ph) %d\n", ip, ip, ip, line);
35 BUG();
36 }
37
__jump_label_transform(struct jump_entry * entry,enum jump_label_type type,void * (* poker)(void *,const void *,size_t),int init)38 static void __ref __jump_label_transform(struct jump_entry *entry,
39 enum jump_label_type type,
40 void *(*poker)(void *, const void *, size_t),
41 int init)
42 {
43 union jump_code_union code;
44 const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
45 const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
46
47 if (early_boot_irqs_disabled)
48 poker = text_poke_early;
49
50 if (type == JUMP_LABEL_JMP) {
51 if (init) {
52 /*
53 * Jump label is enabled for the first time.
54 * So we expect a default_nop...
55 */
56 if (unlikely(memcmp((void *)entry->code, default_nop, 5)
57 != 0))
58 bug_at((void *)entry->code, __LINE__);
59 } else {
60 /*
61 * ...otherwise expect an ideal_nop. Otherwise
62 * something went horribly wrong.
63 */
64 if (unlikely(memcmp((void *)entry->code, ideal_nop, 5)
65 != 0))
66 bug_at((void *)entry->code, __LINE__);
67 }
68
69 code.jump = 0xe9;
70 code.offset = entry->target -
71 (entry->code + JUMP_LABEL_NOP_SIZE);
72 } else {
73 /*
74 * We are disabling this jump label. If it is not what
75 * we think it is, then something must have gone wrong.
76 * If this is the first initialization call, then we
77 * are converting the default nop to the ideal nop.
78 */
79 if (init) {
80 if (unlikely(memcmp((void *)entry->code, default_nop, 5) != 0))
81 bug_at((void *)entry->code, __LINE__);
82 } else {
83 code.jump = 0xe9;
84 code.offset = entry->target -
85 (entry->code + JUMP_LABEL_NOP_SIZE);
86 if (unlikely(memcmp((void *)entry->code, &code, 5) != 0))
87 bug_at((void *)entry->code, __LINE__);
88 }
89 memcpy(&code, ideal_nops[NOP_ATOMIC5], JUMP_LABEL_NOP_SIZE);
90 }
91
92 /*
93 * Make text_poke_bp() a default fallback poker.
94 *
95 * At the time the change is being done, just ignore whether we
96 * are doing nop -> jump or jump -> nop transition, and assume
97 * always nop being the 'currently valid' instruction
98 *
99 */
100 if (poker)
101 (*poker)((void *)entry->code, &code, JUMP_LABEL_NOP_SIZE);
102 else
103 text_poke_bp((void *)entry->code, &code, JUMP_LABEL_NOP_SIZE,
104 (void *)entry->code + JUMP_LABEL_NOP_SIZE);
105 }
106
arch_jump_label_transform(struct jump_entry * entry,enum jump_label_type type)107 void arch_jump_label_transform(struct jump_entry *entry,
108 enum jump_label_type type)
109 {
110 mutex_lock(&text_mutex);
111 __jump_label_transform(entry, type, NULL, 0);
112 mutex_unlock(&text_mutex);
113 }
114
115 static enum {
116 JL_STATE_START,
117 JL_STATE_NO_UPDATE,
118 JL_STATE_UPDATE,
119 } jlstate __initdata_or_module = JL_STATE_START;
120
arch_jump_label_transform_static(struct jump_entry * entry,enum jump_label_type type)121 __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry,
122 enum jump_label_type type)
123 {
124 /*
125 * This function is called at boot up and when modules are
126 * first loaded. Check if the default nop, the one that is
127 * inserted at compile time, is the ideal nop. If it is, then
128 * we do not need to update the nop, and we can leave it as is.
129 * If it is not, then we need to update the nop to the ideal nop.
130 */
131 if (jlstate == JL_STATE_START) {
132 const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
133 const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
134
135 if (memcmp(ideal_nop, default_nop, 5) != 0)
136 jlstate = JL_STATE_UPDATE;
137 else
138 jlstate = JL_STATE_NO_UPDATE;
139 }
140 if (jlstate == JL_STATE_UPDATE)
141 __jump_label_transform(entry, type, text_poke_early, 1);
142 }
143