jump_label.h 1.7 KB
Newer Older
J
Jason Baron 已提交
1 2 3
#ifndef _ASM_X86_JUMP_LABEL_H
#define _ASM_X86_JUMP_LABEL_H

4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef HAVE_JUMP_LABEL
/*
 * For better or for worse, if jump labels (the gcc extension) are missing,
 * then the entire static branch patching infrastructure is compiled out.
 * If that happens, the code in here will malfunction.  Raise a compiler
 * error instead.
 *
 * In theory, jump labels and the static branch patching infrastructure
 * could be decoupled to fix this.
 */
#error asm/jump_label.h included on a non-jump-label kernel
#endif

17
#ifndef __ASSEMBLY__
J
Jason Baron 已提交
18

19
#include <linux/stringify.h>
J
Jason Baron 已提交
20 21
#include <linux/types.h>
#include <asm/nops.h>
22
#include <asm/asm.h>
J
Jason Baron 已提交
23 24 25

#define JUMP_LABEL_NOP_SIZE 5

26 27 28 29 30
#ifdef CONFIG_X86_64
# define STATIC_KEY_INIT_NOP P6_NOP5_ATOMIC
#else
# define STATIC_KEY_INIT_NOP GENERIC_NOP5_ATOMIC
#endif
31

32
static __always_inline bool arch_static_branch(struct static_key *key, bool branch)
33
{
34
	asm_volatile_goto("1:"
35
		".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t"
36
		".pushsection __jump_table,  \"aw\" \n\t"
37
		_ASM_ALIGN "\n\t"
38
		_ASM_PTR "1b, %l[l_yes], %c0 + %c1 \n\t"
39
		".popsection \n\t"
40
		: :  "i" (key), "i" (branch) : : l_yes);
41 42 43 44 45 46 47 48 49 50 51 52 53

	return false;
l_yes:
	return true;
}

static __always_inline bool arch_static_branch_jump(struct static_key *key, bool branch)
{
	asm_volatile_goto("1:"
		".byte 0xe9\n\t .long %l[l_yes] - 2f\n\t"
		"2:\n\t"
		".pushsection __jump_table,  \"aw\" \n\t"
		_ASM_ALIGN "\n\t"
54
		_ASM_PTR "1b, %l[l_yes], %c0 + %c1 \n\t"
55
		".popsection \n\t"
56
		: :  "i" (key), "i" (branch) : : l_yes);
57

58 59 60 61
	return false;
l_yes:
	return true;
}
J
Jason Baron 已提交
62 63 64 65 66

#ifdef CONFIG_X86_64
typedef u64 jump_label_t;
#else
typedef u32 jump_label_t;
67
#endif
J
Jason Baron 已提交
68 69 70 71 72 73 74

struct jump_entry {
	jump_label_t code;
	jump_label_t target;
	jump_label_t key;
};

75
#endif  /* __ASSEMBLY__ */
J
Jason Baron 已提交
76
#endif