xref: /openbmc/linux/arch/x86/kernel/jump_label.c (revision f9510fa9caaf8229381d5f86ba0774bf1a6ca39b)
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 
1918cbc8beSPeter Zijlstra static const void *
20a89dfde3SPeter Zijlstra __jump_label_set_jump_code(struct jump_entry *entry, enum jump_label_type type)
21d9f5ab7bSJason Baron {
2263f62addSPeter Zijlstra 	const void *expect, *code;
2363f62addSPeter Zijlstra 	const void *addr, *dest;
249fc0f798SArd Biesheuvel 
2563f62addSPeter Zijlstra 	addr = (void *)jump_entry_code(entry);
2663f62addSPeter Zijlstra 	dest = (void *)jump_entry_target(entry);
2718cbc8beSPeter Zijlstra 
2863f62addSPeter Zijlstra 	code = text_gen_insn(JMP32_INSN_OPCODE, addr, dest);
29d9f5ab7bSJason Baron 
30*f9510fa9SPeter Zijlstra 	if (type == JUMP_LABEL_JMP)
31*f9510fa9SPeter Zijlstra 		expect = x86_nops[5];
32*f9510fa9SPeter Zijlstra 	else
33*f9510fa9SPeter Zijlstra 		expect = code;
34e71a5be1SJeremy Fitzhardinge 
35*f9510fa9SPeter Zijlstra 	if (memcmp(addr, expect, JUMP_LABEL_NOP_SIZE)) {
36*f9510fa9SPeter Zijlstra 		/*
37*f9510fa9SPeter Zijlstra 		 * The location is not an op that we were expecting.
38*f9510fa9SPeter Zijlstra 		 * Something went wrong. Crash the box, as something could be
39*f9510fa9SPeter Zijlstra 		 * corrupting the kernel.
40*f9510fa9SPeter Zijlstra 		 */
41*f9510fa9SPeter Zijlstra 		pr_crit("jump_label: Fatal kernel bug, unexpected op at %pS [%p] (%5ph != %5ph)) type:%d\n",
42*f9510fa9SPeter Zijlstra 				addr, addr, addr, expect, type);
43*f9510fa9SPeter Zijlstra 		BUG();
44*f9510fa9SPeter Zijlstra 	}
459fc0f798SArd Biesheuvel 
464cc6620bSDaniel Bristot de Oliveira 	if (type == JUMP_LABEL_NOP)
47a89dfde3SPeter Zijlstra 		code = x86_nops[5];
4818cbc8beSPeter Zijlstra 
4963f62addSPeter Zijlstra 	return code;
504cc6620bSDaniel Bristot de Oliveira }
514cc6620bSDaniel Bristot de Oliveira 
524de4952cSRandy Dunlap static inline void __jump_label_transform(struct jump_entry *entry,
534cc6620bSDaniel Bristot de Oliveira 					  enum jump_label_type type,
544cc6620bSDaniel Bristot de Oliveira 					  int init)
554cc6620bSDaniel Bristot de Oliveira {
56a89dfde3SPeter Zijlstra 	const void *opcode = __jump_label_set_jump_code(entry, type);
574cc6620bSDaniel Bristot de Oliveira 
5851b2c07bSJiri Kosina 	/*
59bb0a008dSNadav Amit 	 * As long as only a single processor is running and the code is still
60bb0a008dSNadav Amit 	 * not marked as RO, text_poke_early() can be used; Checking that
61bb0a008dSNadav Amit 	 * system_state is SYSTEM_BOOTING guarantees it. It will be set to
62bb0a008dSNadav Amit 	 * SYSTEM_SCHEDULING before other cores are awaken and before the
63bb0a008dSNadav Amit 	 * code is write-protected.
6451b2c07bSJiri Kosina 	 *
6551b2c07bSJiri Kosina 	 * At the time the change is being done, just ignore whether we
6651b2c07bSJiri Kosina 	 * are doing nop -> jump or jump -> nop transition, and assume
6751b2c07bSJiri Kosina 	 * always nop being the 'currently valid' instruction
6851b2c07bSJiri Kosina 	 */
69bb0a008dSNadav Amit 	if (init || system_state == SYSTEM_BOOTING) {
7018cbc8beSPeter Zijlstra 		text_poke_early((void *)jump_entry_code(entry), opcode,
719fc0f798SArd Biesheuvel 				JUMP_LABEL_NOP_SIZE);
729fc0f798SArd Biesheuvel 		return;
739fc0f798SArd Biesheuvel 	}
749fc0f798SArd Biesheuvel 
7518cbc8beSPeter Zijlstra 	text_poke_bp((void *)jump_entry_code(entry), opcode, JUMP_LABEL_NOP_SIZE, NULL);
7618cbc8beSPeter Zijlstra }
7718cbc8beSPeter Zijlstra 
7818cbc8beSPeter Zijlstra static void __ref jump_label_transform(struct jump_entry *entry,
7918cbc8beSPeter Zijlstra 				       enum jump_label_type type,
8018cbc8beSPeter Zijlstra 				       int init)
8118cbc8beSPeter Zijlstra {
8218cbc8beSPeter Zijlstra 	mutex_lock(&text_mutex);
8318cbc8beSPeter Zijlstra 	__jump_label_transform(entry, type, init);
8418cbc8beSPeter Zijlstra 	mutex_unlock(&text_mutex);
85e71a5be1SJeremy Fitzhardinge }
86e71a5be1SJeremy Fitzhardinge 
87e71a5be1SJeremy Fitzhardinge void arch_jump_label_transform(struct jump_entry *entry,
88e71a5be1SJeremy Fitzhardinge 			       enum jump_label_type type)
89e71a5be1SJeremy Fitzhardinge {
9018cbc8beSPeter Zijlstra 	jump_label_transform(entry, type, 0);
91d9f5ab7bSJason Baron }
92d9f5ab7bSJason Baron 
93ba54f0c3SDaniel Bristot de Oliveira bool arch_jump_label_transform_queue(struct jump_entry *entry,
94ba54f0c3SDaniel Bristot de Oliveira 				     enum jump_label_type type)
95ba54f0c3SDaniel Bristot de Oliveira {
9618cbc8beSPeter Zijlstra 	const void *opcode;
97ba54f0c3SDaniel Bristot de Oliveira 
98ba54f0c3SDaniel Bristot de Oliveira 	if (system_state == SYSTEM_BOOTING) {
99ba54f0c3SDaniel Bristot de Oliveira 		/*
100ba54f0c3SDaniel Bristot de Oliveira 		 * Fallback to the non-batching mode.
101ba54f0c3SDaniel Bristot de Oliveira 		 */
102ba54f0c3SDaniel Bristot de Oliveira 		arch_jump_label_transform(entry, type);
103ba54f0c3SDaniel Bristot de Oliveira 		return true;
104ba54f0c3SDaniel Bristot de Oliveira 	}
105ba54f0c3SDaniel Bristot de Oliveira 
10618cbc8beSPeter Zijlstra 	mutex_lock(&text_mutex);
107a89dfde3SPeter Zijlstra 	opcode = __jump_label_set_jump_code(entry, type);
10818cbc8beSPeter Zijlstra 	text_poke_queue((void *)jump_entry_code(entry),
10918cbc8beSPeter Zijlstra 			opcode, JUMP_LABEL_NOP_SIZE, NULL);
11018cbc8beSPeter Zijlstra 	mutex_unlock(&text_mutex);
111ba54f0c3SDaniel Bristot de Oliveira 	return true;
112ba54f0c3SDaniel Bristot de Oliveira }
113ba54f0c3SDaniel Bristot de Oliveira 
114ba54f0c3SDaniel Bristot de Oliveira void arch_jump_label_transform_apply(void)
115ba54f0c3SDaniel Bristot de Oliveira {
116ba54f0c3SDaniel Bristot de Oliveira 	mutex_lock(&text_mutex);
11718cbc8beSPeter Zijlstra 	text_poke_finish();
118ba54f0c3SDaniel Bristot de Oliveira 	mutex_unlock(&text_mutex);
119ba54f0c3SDaniel Bristot de Oliveira }
120ba54f0c3SDaniel Bristot de Oliveira 
12111570da1SSteven Rostedt static enum {
12211570da1SSteven Rostedt 	JL_STATE_START,
12311570da1SSteven Rostedt 	JL_STATE_NO_UPDATE,
12411570da1SSteven Rostedt 	JL_STATE_UPDATE,
12511570da1SSteven Rostedt } jlstate __initdata_or_module = JL_STATE_START;
12611570da1SSteven Rostedt 
1279cdbe1cbSPeter Zijlstra __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry,
128e71a5be1SJeremy Fitzhardinge 				      enum jump_label_type type)
129e71a5be1SJeremy Fitzhardinge {
13011570da1SSteven Rostedt 	if (jlstate == JL_STATE_UPDATE)
13118cbc8beSPeter Zijlstra 		jump_label_transform(entry, type, 1);
132e71a5be1SJeremy Fitzhardinge }
133