blob: 24cf2b25ce737d1a4529f1e4ce32f9228bca5227 [file] [log] [blame]
Jason Barond9f5ab72010-09-17 11:09:22 -04001/*
2 * jump label x86 support
3 *
4 * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
5 *
6 */
7#include <linux/jump_label.h>
8#include <linux/memory.h>
9#include <linux/uaccess.h>
10#include <linux/module.h>
11#include <linux/list.h>
12#include <linux/jhash.h>
13#include <linux/cpu.h>
14#include <asm/kprobes.h>
15#include <asm/alternative.h>
16
17#ifdef HAVE_JUMP_LABEL
18
19union jump_code_union {
20 char code[JUMP_LABEL_NOP_SIZE];
21 struct {
22 char jump;
23 int offset;
24 } __attribute__((packed));
25};
26
Jeremy Fitzhardingee71a5be2011-09-29 11:11:09 -070027static void __jump_label_transform(struct jump_entry *entry,
28 enum jump_label_type type,
Steven Rostedt9c85f3b2012-01-26 18:38:07 -050029 void *(*poker)(void *, const void *, size_t),
30 int init)
Jason Barond9f5ab72010-09-17 11:09:22 -040031{
32 union jump_code_union code;
Steven Rostedt9c85f3b2012-01-26 18:38:07 -050033 const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
Jason Barond9f5ab72010-09-17 11:09:22 -040034
35 if (type == JUMP_LABEL_ENABLE) {
Steven Rostedt9c85f3b2012-01-26 18:38:07 -050036 /*
37 * We are enabling this jump label. If it is not a nop
38 * then something must have gone wrong.
39 */
40 BUG_ON(memcmp((void *)entry->code, ideal_nop, 5) != 0);
41
Jason Barond9f5ab72010-09-17 11:09:22 -040042 code.jump = 0xe9;
43 code.offset = entry->target -
44 (entry->code + JUMP_LABEL_NOP_SIZE);
Steven Rostedt9c85f3b2012-01-26 18:38:07 -050045 } else {
46 /*
47 * We are disabling this jump label. If it is not what
48 * we think it is, then something must have gone wrong.
49 * If this is the first initialization call, then we
50 * are converting the default nop to the ideal nop.
51 */
52 if (init) {
53 const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
54 BUG_ON(memcmp((void *)entry->code, default_nop, 5) != 0);
55 } else {
56 code.jump = 0xe9;
57 code.offset = entry->target -
58 (entry->code + JUMP_LABEL_NOP_SIZE);
59 BUG_ON(memcmp((void *)entry->code, &code, 5) != 0);
60 }
H. Peter Anvindc326fc2011-04-18 15:19:51 -070061 memcpy(&code, ideal_nops[NOP_ATOMIC5], JUMP_LABEL_NOP_SIZE);
Steven Rostedt9c85f3b2012-01-26 18:38:07 -050062 }
Jeremy Fitzhardingee71a5be2011-09-29 11:11:09 -070063
64 (*poker)((void *)entry->code, &code, JUMP_LABEL_NOP_SIZE);
65}
66
67void arch_jump_label_transform(struct jump_entry *entry,
68 enum jump_label_type type)
69{
Jason Barond9f5ab72010-09-17 11:09:22 -040070 get_online_cpus();
71 mutex_lock(&text_mutex);
Steven Rostedt9c85f3b2012-01-26 18:38:07 -050072 __jump_label_transform(entry, type, text_poke_smp, 0);
Jason Barond9f5ab72010-09-17 11:09:22 -040073 mutex_unlock(&text_mutex);
74 put_online_cpus();
75}
76
Steven Rostedt11570da2012-01-26 18:16:15 -050077static enum {
78 JL_STATE_START,
79 JL_STATE_NO_UPDATE,
80 JL_STATE_UPDATE,
81} jlstate __initdata_or_module = JL_STATE_START;
82
Peter Zijlstra9cdbe1c2011-12-06 17:27:29 +010083__init_or_module void arch_jump_label_transform_static(struct jump_entry *entry,
Jeremy Fitzhardingee71a5be2011-09-29 11:11:09 -070084 enum jump_label_type type)
85{
Steven Rostedt11570da2012-01-26 18:16:15 -050086 /*
87 * This function is called at boot up and when modules are
88 * first loaded. Check if the default nop, the one that is
89 * inserted at compile time, is the ideal nop. If it is, then
90 * we do not need to update the nop, and we can leave it as is.
91 * If it is not, then we need to update the nop to the ideal nop.
92 */
93 if (jlstate == JL_STATE_START) {
94 const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP };
95 const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5];
96
97 if (memcmp(ideal_nop, default_nop, 5) != 0)
98 jlstate = JL_STATE_UPDATE;
99 else
100 jlstate = JL_STATE_NO_UPDATE;
101 }
102 if (jlstate == JL_STATE_UPDATE)
Steven Rostedt9c85f3b2012-01-26 18:38:07 -0500103 __jump_label_transform(entry, type, text_poke_early, 1);
Jeremy Fitzhardingee71a5be2011-09-29 11:11:09 -0700104}
105
Jason Barond9f5ab72010-09-17 11:09:22 -0400106#endif