xref: /openbmc/linux/arch/x86/kernel/jump_label.c (revision bb0a008d6a2c543efc11313b448d2f26f91dc4f8)
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 
386fffacb3SPavel Tatashin static void __ref __jump_label_transform(struct jump_entry *entry,
39e71a5be1SJeremy Fitzhardinge 					 enum jump_label_type type,
409c85f3bdSSteven Rostedt 					 int init)
41d9f5ab7bSJason Baron {
429fc0f798SArd Biesheuvel 	union jump_code_union jmp;
43a8fab074SHannes Frederic Sowa 	const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
449c85f3bdSSteven Rostedt 	const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
459fc0f798SArd Biesheuvel 	const void *expect, *code;
469fc0f798SArd Biesheuvel 	int line;
479fc0f798SArd Biesheuvel 
489fc0f798SArd Biesheuvel 	jmp.jump = 0xe9;
499fc0f798SArd Biesheuvel 	jmp.offset = jump_entry_target(entry) -
509fc0f798SArd Biesheuvel 		     (jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE);
51d9f5ab7bSJason Baron 
5276b235c6SPeter Zijlstra 	if (type == JUMP_LABEL_JMP) {
53a8fab074SHannes Frederic Sowa 		if (init) {
549fc0f798SArd Biesheuvel 			expect = default_nop; line = __LINE__;
55a8fab074SHannes Frederic Sowa 		} else {
569fc0f798SArd Biesheuvel 			expect = ideal_nop; line = __LINE__;
57a8fab074SHannes Frederic Sowa 		}
589c85f3bdSSteven Rostedt 
599fc0f798SArd Biesheuvel 		code = &jmp.code;
609c85f3bdSSteven Rostedt 	} else {
619c85f3bdSSteven Rostedt 		if (init) {
629fc0f798SArd Biesheuvel 			expect = default_nop; line = __LINE__;
639c85f3bdSSteven Rostedt 		} else {
649fc0f798SArd Biesheuvel 			expect = &jmp.code; line = __LINE__;
659c85f3bdSSteven Rostedt 		}
669fc0f798SArd Biesheuvel 
679fc0f798SArd Biesheuvel 		code = ideal_nop;
689c85f3bdSSteven Rostedt 	}
69e71a5be1SJeremy Fitzhardinge 
709fc0f798SArd Biesheuvel 	if (memcmp((void *)jump_entry_code(entry), expect, JUMP_LABEL_NOP_SIZE))
719fc0f798SArd Biesheuvel 		bug_at((void *)jump_entry_code(entry), line);
729fc0f798SArd Biesheuvel 
7351b2c07bSJiri Kosina 	/*
74*bb0a008dSNadav Amit 	 * As long as only a single processor is running and the code is still
75*bb0a008dSNadav Amit 	 * not marked as RO, text_poke_early() can be used; Checking that
76*bb0a008dSNadav Amit 	 * system_state is SYSTEM_BOOTING guarantees it. It will be set to
77*bb0a008dSNadav Amit 	 * SYSTEM_SCHEDULING before other cores are awaken and before the
78*bb0a008dSNadav Amit 	 * code is write-protected.
7951b2c07bSJiri Kosina 	 *
8051b2c07bSJiri Kosina 	 * At the time the change is being done, just ignore whether we
8151b2c07bSJiri Kosina 	 * are doing nop -> jump or jump -> nop transition, and assume
8251b2c07bSJiri Kosina 	 * always nop being the 'currently valid' instruction
8351b2c07bSJiri Kosina 	 */
84*bb0a008dSNadav Amit 	if (init || system_state == SYSTEM_BOOTING) {
85*bb0a008dSNadav Amit 		text_poke_early((void *)jump_entry_code(entry), code,
869fc0f798SArd Biesheuvel 				JUMP_LABEL_NOP_SIZE);
879fc0f798SArd Biesheuvel 		return;
889fc0f798SArd Biesheuvel 	}
899fc0f798SArd Biesheuvel 
909fc0f798SArd Biesheuvel 	text_poke_bp((void *)jump_entry_code(entry), code, JUMP_LABEL_NOP_SIZE,
919fc0f798SArd Biesheuvel 		     (void *)jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE);
92e71a5be1SJeremy Fitzhardinge }
93e71a5be1SJeremy Fitzhardinge 
94e71a5be1SJeremy Fitzhardinge void arch_jump_label_transform(struct jump_entry *entry,
95e71a5be1SJeremy Fitzhardinge 			       enum jump_label_type type)
96e71a5be1SJeremy Fitzhardinge {
97d9f5ab7bSJason Baron 	mutex_lock(&text_mutex);
98*bb0a008dSNadav Amit 	__jump_label_transform(entry, type, 0);
99d9f5ab7bSJason Baron 	mutex_unlock(&text_mutex);
100d9f5ab7bSJason Baron }
101d9f5ab7bSJason Baron 
10211570da1SSteven Rostedt static enum {
10311570da1SSteven Rostedt 	JL_STATE_START,
10411570da1SSteven Rostedt 	JL_STATE_NO_UPDATE,
10511570da1SSteven Rostedt 	JL_STATE_UPDATE,
10611570da1SSteven Rostedt } jlstate __initdata_or_module = JL_STATE_START;
10711570da1SSteven Rostedt 
1089cdbe1cbSPeter Zijlstra __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry,
109e71a5be1SJeremy Fitzhardinge 				      enum jump_label_type type)
110e71a5be1SJeremy Fitzhardinge {
11111570da1SSteven Rostedt 	/*
11211570da1SSteven Rostedt 	 * This function is called at boot up and when modules are
11311570da1SSteven Rostedt 	 * first loaded. Check if the default nop, the one that is
11411570da1SSteven Rostedt 	 * inserted at compile time, is the ideal nop. If it is, then
11511570da1SSteven Rostedt 	 * we do not need to update the nop, and we can leave it as is.
11611570da1SSteven Rostedt 	 * If it is not, then we need to update the nop to the ideal nop.
11711570da1SSteven Rostedt 	 */
11811570da1SSteven Rostedt 	if (jlstate == JL_STATE_START) {
11911570da1SSteven Rostedt 		const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
12011570da1SSteven Rostedt 		const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
12111570da1SSteven Rostedt 
12211570da1SSteven Rostedt 		if (memcmp(ideal_nop, default_nop, 5) != 0)
12311570da1SSteven Rostedt 			jlstate = JL_STATE_UPDATE;
12411570da1SSteven Rostedt 		else
12511570da1SSteven Rostedt 			jlstate = JL_STATE_NO_UPDATE;
12611570da1SSteven Rostedt 	}
12711570da1SSteven Rostedt 	if (jlstate == JL_STATE_UPDATE)
128*bb0a008dSNadav Amit 		__jump_label_transform(entry, type, 1);
129e71a5be1SJeremy Fitzhardinge }
130