xref: /openbmc/linux/arch/x86/kernel/jump_label.c (revision ba54f0c3f7c400a392c413d8ca21d3ada35f2584)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d9f5ab7bSJason Baron /*
3d9f5ab7bSJason Baron  * jump label x86 support
4d9f5ab7bSJason Baron  *
5d9f5ab7bSJason Baron  * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
6d9f5ab7bSJason Baron  *
7d9f5ab7bSJason Baron  */
8d9f5ab7bSJason Baron #include <linux/jump_label.h>
9d9f5ab7bSJason Baron #include <linux/memory.h>
10d9f5ab7bSJason Baron #include <linux/uaccess.h>
11d9f5ab7bSJason Baron #include <linux/module.h>
12d9f5ab7bSJason Baron #include <linux/list.h>
13d9f5ab7bSJason Baron #include <linux/jhash.h>
14d9f5ab7bSJason Baron #include <linux/cpu.h>
15d9f5ab7bSJason Baron #include <asm/kprobes.h>
16d9f5ab7bSJason Baron #include <asm/alternative.h>
1735de5b06SAndy Lutomirski #include <asm/text-patching.h>
18d9f5ab7bSJason Baron 
19d9f5ab7bSJason Baron union jump_code_union {
20d9f5ab7bSJason Baron 	char code[JUMP_LABEL_NOP_SIZE];
21d9f5ab7bSJason Baron 	struct {
22d9f5ab7bSJason Baron 		char jump;
23d9f5ab7bSJason Baron 		int offset;
24d9f5ab7bSJason Baron 	} __attribute__((packed));
25d9f5ab7bSJason Baron };
26d9f5ab7bSJason Baron 
27fb40d7a8SSteven Rostedt static void bug_at(unsigned char *ip, int line)
28fb40d7a8SSteven Rostedt {
29fb40d7a8SSteven Rostedt 	/*
30fb40d7a8SSteven Rostedt 	 * The location is not an op that we were expecting.
31fb40d7a8SSteven Rostedt 	 * Something went wrong. Crash the box, as something could be
32fb40d7a8SSteven Rostedt 	 * corrupting the kernel.
33fb40d7a8SSteven Rostedt 	 */
346e03f66cSAndy Shevchenko 	pr_crit("jump_label: Fatal kernel bug, unexpected op at %pS [%p] (%5ph) %d\n", ip, ip, ip, line);
35fb40d7a8SSteven Rostedt 	BUG();
36fb40d7a8SSteven Rostedt }
37fb40d7a8SSteven Rostedt 
384cc6620bSDaniel Bristot de Oliveira static void __jump_label_set_jump_code(struct jump_entry *entry,
39e71a5be1SJeremy Fitzhardinge 				       enum jump_label_type type,
404cc6620bSDaniel Bristot de Oliveira 				       union jump_code_union *code,
419c85f3bdSSteven Rostedt 				       int init)
42d9f5ab7bSJason Baron {
43a8fab074SHannes Frederic Sowa 	const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
449c85f3bdSSteven Rostedt 	const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
454cc6620bSDaniel Bristot de Oliveira 	const void *expect;
469fc0f798SArd Biesheuvel 	int line;
479fc0f798SArd Biesheuvel 
484cc6620bSDaniel Bristot de Oliveira 	code->jump = 0xe9;
494cc6620bSDaniel Bristot de Oliveira 	code->offset = jump_entry_target(entry) -
509fc0f798SArd Biesheuvel 		       (jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE);
51d9f5ab7bSJason Baron 
52a8fab074SHannes Frederic Sowa 	if (init) {
539fc0f798SArd Biesheuvel 		expect = default_nop; line = __LINE__;
544cc6620bSDaniel Bristot de Oliveira 	} else if (type == JUMP_LABEL_JMP) {
559fc0f798SArd Biesheuvel 		expect = ideal_nop; line = __LINE__;
569c85f3bdSSteven Rostedt 	} else {
574cc6620bSDaniel Bristot de Oliveira 		expect = code->code; line = __LINE__;
589c85f3bdSSteven Rostedt 	}
59e71a5be1SJeremy Fitzhardinge 
609fc0f798SArd Biesheuvel 	if (memcmp((void *)jump_entry_code(entry), expect, JUMP_LABEL_NOP_SIZE))
619fc0f798SArd Biesheuvel 		bug_at((void *)jump_entry_code(entry), line);
629fc0f798SArd Biesheuvel 
634cc6620bSDaniel Bristot de Oliveira 	if (type == JUMP_LABEL_NOP)
644cc6620bSDaniel Bristot de Oliveira 		memcpy(code, ideal_nop, JUMP_LABEL_NOP_SIZE);
654cc6620bSDaniel Bristot de Oliveira }
664cc6620bSDaniel Bristot de Oliveira 
674cc6620bSDaniel Bristot de Oliveira static void __ref __jump_label_transform(struct jump_entry *entry,
684cc6620bSDaniel Bristot de Oliveira 					 enum jump_label_type type,
694cc6620bSDaniel Bristot de Oliveira 					 int init)
704cc6620bSDaniel Bristot de Oliveira {
714cc6620bSDaniel Bristot de Oliveira 	union jump_code_union code;
724cc6620bSDaniel Bristot de Oliveira 
734cc6620bSDaniel Bristot de Oliveira 	__jump_label_set_jump_code(entry, type, &code, init);
744cc6620bSDaniel Bristot de Oliveira 
7551b2c07bSJiri Kosina 	/*
76bb0a008dSNadav Amit 	 * As long as only a single processor is running and the code is still
77bb0a008dSNadav Amit 	 * not marked as RO, text_poke_early() can be used; Checking that
78bb0a008dSNadav Amit 	 * system_state is SYSTEM_BOOTING guarantees it. It will be set to
79bb0a008dSNadav Amit 	 * SYSTEM_SCHEDULING before other cores are awaken and before the
80bb0a008dSNadav Amit 	 * code is write-protected.
8151b2c07bSJiri Kosina 	 *
8251b2c07bSJiri Kosina 	 * At the time the change is being done, just ignore whether we
8351b2c07bSJiri Kosina 	 * are doing nop -> jump or jump -> nop transition, and assume
8451b2c07bSJiri Kosina 	 * always nop being the 'currently valid' instruction
8551b2c07bSJiri Kosina 	 */
86bb0a008dSNadav Amit 	if (init || system_state == SYSTEM_BOOTING) {
874cc6620bSDaniel Bristot de Oliveira 		text_poke_early((void *)jump_entry_code(entry), &code,
889fc0f798SArd Biesheuvel 				JUMP_LABEL_NOP_SIZE);
899fc0f798SArd Biesheuvel 		return;
909fc0f798SArd Biesheuvel 	}
919fc0f798SArd Biesheuvel 
924cc6620bSDaniel Bristot de Oliveira 	text_poke_bp((void *)jump_entry_code(entry), &code, JUMP_LABEL_NOP_SIZE,
939fc0f798SArd Biesheuvel 		     (void *)jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE);
94e71a5be1SJeremy Fitzhardinge }
95e71a5be1SJeremy Fitzhardinge 
96e71a5be1SJeremy Fitzhardinge void arch_jump_label_transform(struct jump_entry *entry,
97e71a5be1SJeremy Fitzhardinge 			       enum jump_label_type type)
98e71a5be1SJeremy Fitzhardinge {
99d9f5ab7bSJason Baron 	mutex_lock(&text_mutex);
100bb0a008dSNadav Amit 	__jump_label_transform(entry, type, 0);
101d9f5ab7bSJason Baron 	mutex_unlock(&text_mutex);
102d9f5ab7bSJason Baron }
103d9f5ab7bSJason Baron 
104*ba54f0c3SDaniel Bristot de Oliveira #define TP_VEC_MAX (PAGE_SIZE / sizeof(struct text_poke_loc))
105*ba54f0c3SDaniel Bristot de Oliveira static struct text_poke_loc tp_vec[TP_VEC_MAX];
106*ba54f0c3SDaniel Bristot de Oliveira int tp_vec_nr = 0;
107*ba54f0c3SDaniel Bristot de Oliveira 
108*ba54f0c3SDaniel Bristot de Oliveira bool arch_jump_label_transform_queue(struct jump_entry *entry,
109*ba54f0c3SDaniel Bristot de Oliveira 				     enum jump_label_type type)
110*ba54f0c3SDaniel Bristot de Oliveira {
111*ba54f0c3SDaniel Bristot de Oliveira 	struct text_poke_loc *tp;
112*ba54f0c3SDaniel Bristot de Oliveira 	void *entry_code;
113*ba54f0c3SDaniel Bristot de Oliveira 
114*ba54f0c3SDaniel Bristot de Oliveira 	if (system_state == SYSTEM_BOOTING) {
115*ba54f0c3SDaniel Bristot de Oliveira 		/*
116*ba54f0c3SDaniel Bristot de Oliveira 		 * Fallback to the non-batching mode.
117*ba54f0c3SDaniel Bristot de Oliveira 		 */
118*ba54f0c3SDaniel Bristot de Oliveira 		arch_jump_label_transform(entry, type);
119*ba54f0c3SDaniel Bristot de Oliveira 		return true;
120*ba54f0c3SDaniel Bristot de Oliveira 	}
121*ba54f0c3SDaniel Bristot de Oliveira 
122*ba54f0c3SDaniel Bristot de Oliveira 	/*
123*ba54f0c3SDaniel Bristot de Oliveira 	 * No more space in the vector, tell upper layer to apply
124*ba54f0c3SDaniel Bristot de Oliveira 	 * the queue before continuing.
125*ba54f0c3SDaniel Bristot de Oliveira 	 */
126*ba54f0c3SDaniel Bristot de Oliveira 	if (tp_vec_nr == TP_VEC_MAX)
127*ba54f0c3SDaniel Bristot de Oliveira 		return false;
128*ba54f0c3SDaniel Bristot de Oliveira 
129*ba54f0c3SDaniel Bristot de Oliveira 	tp = &tp_vec[tp_vec_nr];
130*ba54f0c3SDaniel Bristot de Oliveira 
131*ba54f0c3SDaniel Bristot de Oliveira 	entry_code = (void *)jump_entry_code(entry);
132*ba54f0c3SDaniel Bristot de Oliveira 
133*ba54f0c3SDaniel Bristot de Oliveira 	/*
134*ba54f0c3SDaniel Bristot de Oliveira 	 * The INT3 handler will do a bsearch in the queue, so we need entries
135*ba54f0c3SDaniel Bristot de Oliveira 	 * to be sorted. We can survive an unsorted list by rejecting the entry,
136*ba54f0c3SDaniel Bristot de Oliveira 	 * forcing the generic jump_label code to apply the queue. Warning once,
137*ba54f0c3SDaniel Bristot de Oliveira 	 * to raise the attention to the case of an unsorted entry that is
138*ba54f0c3SDaniel Bristot de Oliveira 	 * better not happen, because, in the worst case we will perform in the
139*ba54f0c3SDaniel Bristot de Oliveira 	 * same way as we do without batching - with some more overhead.
140*ba54f0c3SDaniel Bristot de Oliveira 	 */
141*ba54f0c3SDaniel Bristot de Oliveira 	if (tp_vec_nr > 0) {
142*ba54f0c3SDaniel Bristot de Oliveira 		int prev = tp_vec_nr - 1;
143*ba54f0c3SDaniel Bristot de Oliveira 		struct text_poke_loc *prev_tp = &tp_vec[prev];
144*ba54f0c3SDaniel Bristot de Oliveira 
145*ba54f0c3SDaniel Bristot de Oliveira 		if (WARN_ON_ONCE(prev_tp->addr > entry_code))
146*ba54f0c3SDaniel Bristot de Oliveira 			return false;
147*ba54f0c3SDaniel Bristot de Oliveira 	}
148*ba54f0c3SDaniel Bristot de Oliveira 
149*ba54f0c3SDaniel Bristot de Oliveira 	__jump_label_set_jump_code(entry, type,
150*ba54f0c3SDaniel Bristot de Oliveira 				   (union jump_code_union *) &tp->opcode, 0);
151*ba54f0c3SDaniel Bristot de Oliveira 
152*ba54f0c3SDaniel Bristot de Oliveira 	tp->addr = entry_code;
153*ba54f0c3SDaniel Bristot de Oliveira 	tp->detour = entry_code + JUMP_LABEL_NOP_SIZE;
154*ba54f0c3SDaniel Bristot de Oliveira 	tp->len = JUMP_LABEL_NOP_SIZE;
155*ba54f0c3SDaniel Bristot de Oliveira 
156*ba54f0c3SDaniel Bristot de Oliveira 	tp_vec_nr++;
157*ba54f0c3SDaniel Bristot de Oliveira 
158*ba54f0c3SDaniel Bristot de Oliveira 	return true;
159*ba54f0c3SDaniel Bristot de Oliveira }
160*ba54f0c3SDaniel Bristot de Oliveira 
161*ba54f0c3SDaniel Bristot de Oliveira void arch_jump_label_transform_apply(void)
162*ba54f0c3SDaniel Bristot de Oliveira {
163*ba54f0c3SDaniel Bristot de Oliveira 	if (!tp_vec_nr)
164*ba54f0c3SDaniel Bristot de Oliveira 		return;
165*ba54f0c3SDaniel Bristot de Oliveira 
166*ba54f0c3SDaniel Bristot de Oliveira 	mutex_lock(&text_mutex);
167*ba54f0c3SDaniel Bristot de Oliveira 	text_poke_bp_batch(tp_vec, tp_vec_nr);
168*ba54f0c3SDaniel Bristot de Oliveira 	mutex_unlock(&text_mutex);
169*ba54f0c3SDaniel Bristot de Oliveira 
170*ba54f0c3SDaniel Bristot de Oliveira 	tp_vec_nr = 0;
171*ba54f0c3SDaniel Bristot de Oliveira }
172*ba54f0c3SDaniel Bristot de Oliveira 
17311570da1SSteven Rostedt static enum {
17411570da1SSteven Rostedt 	JL_STATE_START,
17511570da1SSteven Rostedt 	JL_STATE_NO_UPDATE,
17611570da1SSteven Rostedt 	JL_STATE_UPDATE,
17711570da1SSteven Rostedt } jlstate __initdata_or_module = JL_STATE_START;
17811570da1SSteven Rostedt 
1799cdbe1cbSPeter Zijlstra __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry,
180e71a5be1SJeremy Fitzhardinge 				      enum jump_label_type type)
181e71a5be1SJeremy Fitzhardinge {
18211570da1SSteven Rostedt 	/*
18311570da1SSteven Rostedt 	 * This function is called at boot up and when modules are
18411570da1SSteven Rostedt 	 * first loaded. Check if the default nop, the one that is
18511570da1SSteven Rostedt 	 * inserted at compile time, is the ideal nop. If it is, then
18611570da1SSteven Rostedt 	 * we do not need to update the nop, and we can leave it as is.
18711570da1SSteven Rostedt 	 * If it is not, then we need to update the nop to the ideal nop.
18811570da1SSteven Rostedt 	 */
18911570da1SSteven Rostedt 	if (jlstate == JL_STATE_START) {
19011570da1SSteven Rostedt 		const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
19111570da1SSteven Rostedt 		const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
19211570da1SSteven Rostedt 
19311570da1SSteven Rostedt 		if (memcmp(ideal_nop, default_nop, 5) != 0)
19411570da1SSteven Rostedt 			jlstate = JL_STATE_UPDATE;
19511570da1SSteven Rostedt 		else
19611570da1SSteven Rostedt 			jlstate = JL_STATE_NO_UPDATE;
19711570da1SSteven Rostedt 	}
19811570da1SSteven Rostedt 	if (jlstate == JL_STATE_UPDATE)
199bb0a008dSNadav Amit 		__jump_label_transform(entry, type, 1);
200e71a5be1SJeremy Fitzhardinge }
201