xref: /openbmc/linux/arch/x86/kernel/jump_label.c (revision 4de4952c0abcb490039cdc946e49ed7f237285a2)
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 
1963f62addSPeter Zijlstra static void bug_at(const void *ip, int line)
20fb40d7a8SSteven Rostedt {
21fb40d7a8SSteven Rostedt 	/*
22fb40d7a8SSteven Rostedt 	 * The location is not an op that we were expecting.
23fb40d7a8SSteven Rostedt 	 * Something went wrong. Crash the box, as something could be
24fb40d7a8SSteven Rostedt 	 * corrupting the kernel.
25fb40d7a8SSteven Rostedt 	 */
266e03f66cSAndy Shevchenko 	pr_crit("jump_label: Fatal kernel bug, unexpected op at %pS [%p] (%5ph) %d\n", ip, ip, ip, line);
27fb40d7a8SSteven Rostedt 	BUG();
28fb40d7a8SSteven Rostedt }
29fb40d7a8SSteven Rostedt 
3018cbc8beSPeter Zijlstra static const void *
3118cbc8beSPeter Zijlstra __jump_label_set_jump_code(struct jump_entry *entry, enum jump_label_type type, int init)
32d9f5ab7bSJason Baron {
33a8fab074SHannes Frederic Sowa 	const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
349c85f3bdSSteven Rostedt 	const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
3563f62addSPeter Zijlstra 	const void *expect, *code;
3663f62addSPeter Zijlstra 	const void *addr, *dest;
379fc0f798SArd Biesheuvel 	int line;
389fc0f798SArd Biesheuvel 
3963f62addSPeter Zijlstra 	addr = (void *)jump_entry_code(entry);
4063f62addSPeter Zijlstra 	dest = (void *)jump_entry_target(entry);
4118cbc8beSPeter Zijlstra 
4263f62addSPeter Zijlstra 	code = text_gen_insn(JMP32_INSN_OPCODE, addr, dest);
43d9f5ab7bSJason Baron 
44a8fab074SHannes Frederic Sowa 	if (init) {
459fc0f798SArd Biesheuvel 		expect = default_nop; line = __LINE__;
464cc6620bSDaniel Bristot de Oliveira 	} else if (type == JUMP_LABEL_JMP) {
479fc0f798SArd Biesheuvel 		expect = ideal_nop; line = __LINE__;
489c85f3bdSSteven Rostedt 	} else {
4963f62addSPeter Zijlstra 		expect = code; line = __LINE__;
509c85f3bdSSteven Rostedt 	}
51e71a5be1SJeremy Fitzhardinge 
5263f62addSPeter Zijlstra 	if (memcmp(addr, expect, JUMP_LABEL_NOP_SIZE))
5363f62addSPeter Zijlstra 		bug_at(addr, line);
549fc0f798SArd Biesheuvel 
554cc6620bSDaniel Bristot de Oliveira 	if (type == JUMP_LABEL_NOP)
5663f62addSPeter Zijlstra 		code = ideal_nop;
5718cbc8beSPeter Zijlstra 
5863f62addSPeter Zijlstra 	return code;
594cc6620bSDaniel Bristot de Oliveira }
604cc6620bSDaniel Bristot de Oliveira 
61*4de4952cSRandy Dunlap static inline void __jump_label_transform(struct jump_entry *entry,
624cc6620bSDaniel Bristot de Oliveira 					  enum jump_label_type type,
634cc6620bSDaniel Bristot de Oliveira 					  int init)
644cc6620bSDaniel Bristot de Oliveira {
6518cbc8beSPeter Zijlstra 	const void *opcode = __jump_label_set_jump_code(entry, type, init);
664cc6620bSDaniel Bristot de Oliveira 
6751b2c07bSJiri Kosina 	/*
68bb0a008dSNadav Amit 	 * As long as only a single processor is running and the code is still
69bb0a008dSNadav Amit 	 * not marked as RO, text_poke_early() can be used; Checking that
70bb0a008dSNadav Amit 	 * system_state is SYSTEM_BOOTING guarantees it. It will be set to
71bb0a008dSNadav Amit 	 * SYSTEM_SCHEDULING before other cores are awaken and before the
72bb0a008dSNadav Amit 	 * code is write-protected.
7351b2c07bSJiri Kosina 	 *
7451b2c07bSJiri Kosina 	 * At the time the change is being done, just ignore whether we
7551b2c07bSJiri Kosina 	 * are doing nop -> jump or jump -> nop transition, and assume
7651b2c07bSJiri Kosina 	 * always nop being the 'currently valid' instruction
7751b2c07bSJiri Kosina 	 */
78bb0a008dSNadav Amit 	if (init || system_state == SYSTEM_BOOTING) {
7918cbc8beSPeter Zijlstra 		text_poke_early((void *)jump_entry_code(entry), opcode,
809fc0f798SArd Biesheuvel 				JUMP_LABEL_NOP_SIZE);
819fc0f798SArd Biesheuvel 		return;
829fc0f798SArd Biesheuvel 	}
839fc0f798SArd Biesheuvel 
8418cbc8beSPeter Zijlstra 	text_poke_bp((void *)jump_entry_code(entry), opcode, JUMP_LABEL_NOP_SIZE, NULL);
8518cbc8beSPeter Zijlstra }
8618cbc8beSPeter Zijlstra 
8718cbc8beSPeter Zijlstra static void __ref jump_label_transform(struct jump_entry *entry,
8818cbc8beSPeter Zijlstra 				       enum jump_label_type type,
8918cbc8beSPeter Zijlstra 				       int init)
9018cbc8beSPeter Zijlstra {
9118cbc8beSPeter Zijlstra 	mutex_lock(&text_mutex);
9218cbc8beSPeter Zijlstra 	__jump_label_transform(entry, type, init);
9318cbc8beSPeter Zijlstra 	mutex_unlock(&text_mutex);
94e71a5be1SJeremy Fitzhardinge }
95e71a5be1SJeremy Fitzhardinge 
96e71a5be1SJeremy Fitzhardinge void arch_jump_label_transform(struct jump_entry *entry,
97e71a5be1SJeremy Fitzhardinge 			       enum jump_label_type type)
98e71a5be1SJeremy Fitzhardinge {
9918cbc8beSPeter Zijlstra 	jump_label_transform(entry, type, 0);
100d9f5ab7bSJason Baron }
101d9f5ab7bSJason Baron 
102ba54f0c3SDaniel Bristot de Oliveira bool arch_jump_label_transform_queue(struct jump_entry *entry,
103ba54f0c3SDaniel Bristot de Oliveira 				     enum jump_label_type type)
104ba54f0c3SDaniel Bristot de Oliveira {
10518cbc8beSPeter Zijlstra 	const void *opcode;
106ba54f0c3SDaniel Bristot de Oliveira 
107ba54f0c3SDaniel Bristot de Oliveira 	if (system_state == SYSTEM_BOOTING) {
108ba54f0c3SDaniel Bristot de Oliveira 		/*
109ba54f0c3SDaniel Bristot de Oliveira 		 * Fallback to the non-batching mode.
110ba54f0c3SDaniel Bristot de Oliveira 		 */
111ba54f0c3SDaniel Bristot de Oliveira 		arch_jump_label_transform(entry, type);
112ba54f0c3SDaniel Bristot de Oliveira 		return true;
113ba54f0c3SDaniel Bristot de Oliveira 	}
114ba54f0c3SDaniel Bristot de Oliveira 
11518cbc8beSPeter Zijlstra 	mutex_lock(&text_mutex);
11618cbc8beSPeter Zijlstra 	opcode = __jump_label_set_jump_code(entry, type, 0);
11718cbc8beSPeter Zijlstra 	text_poke_queue((void *)jump_entry_code(entry),
11818cbc8beSPeter Zijlstra 			opcode, JUMP_LABEL_NOP_SIZE, NULL);
11918cbc8beSPeter Zijlstra 	mutex_unlock(&text_mutex);
120ba54f0c3SDaniel Bristot de Oliveira 	return true;
121ba54f0c3SDaniel Bristot de Oliveira }
122ba54f0c3SDaniel Bristot de Oliveira 
123ba54f0c3SDaniel Bristot de Oliveira void arch_jump_label_transform_apply(void)
124ba54f0c3SDaniel Bristot de Oliveira {
125ba54f0c3SDaniel Bristot de Oliveira 	mutex_lock(&text_mutex);
12618cbc8beSPeter Zijlstra 	text_poke_finish();
127ba54f0c3SDaniel Bristot de Oliveira 	mutex_unlock(&text_mutex);
128ba54f0c3SDaniel Bristot de Oliveira }
129ba54f0c3SDaniel Bristot de Oliveira 
13011570da1SSteven Rostedt static enum {
13111570da1SSteven Rostedt 	JL_STATE_START,
13211570da1SSteven Rostedt 	JL_STATE_NO_UPDATE,
13311570da1SSteven Rostedt 	JL_STATE_UPDATE,
13411570da1SSteven Rostedt } jlstate __initdata_or_module = JL_STATE_START;
13511570da1SSteven Rostedt 
1369cdbe1cbSPeter Zijlstra __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry,
137e71a5be1SJeremy Fitzhardinge 				      enum jump_label_type type)
138e71a5be1SJeremy Fitzhardinge {
13911570da1SSteven Rostedt 	/*
14011570da1SSteven Rostedt 	 * This function is called at boot up and when modules are
14111570da1SSteven Rostedt 	 * first loaded. Check if the default nop, the one that is
14211570da1SSteven Rostedt 	 * inserted at compile time, is the ideal nop. If it is, then
14311570da1SSteven Rostedt 	 * we do not need to update the nop, and we can leave it as is.
14411570da1SSteven Rostedt 	 * If it is not, then we need to update the nop to the ideal nop.
14511570da1SSteven Rostedt 	 */
14611570da1SSteven Rostedt 	if (jlstate == JL_STATE_START) {
14711570da1SSteven Rostedt 		const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
14811570da1SSteven Rostedt 		const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
14911570da1SSteven Rostedt 
15011570da1SSteven Rostedt 		if (memcmp(ideal_nop, default_nop, 5) != 0)
15111570da1SSteven Rostedt 			jlstate = JL_STATE_UPDATE;
15211570da1SSteven Rostedt 		else
15311570da1SSteven Rostedt 			jlstate = JL_STATE_NO_UPDATE;
15411570da1SSteven Rostedt 	}
15511570da1SSteven Rostedt 	if (jlstate == JL_STATE_UPDATE)
15618cbc8beSPeter Zijlstra 		jump_label_transform(entry, type, 1);
157e71a5be1SJeremy Fitzhardinge }
158