aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/include/asm/runtime-patch.h
blob: 366444d7eef9a7e6d247c0dba7a0490de42ea911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * arch/arm/include/asm/runtime-patch.h
 * Note: this file should not be included by non-asm/.h files
 *
 * Copyright 2012 Texas Instruments, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __ASM_ARM_RUNTIME_PATCH_H
#define __ASM_ARM_RUNTIME_PATCH_H

#include <linux/stringify.h>

#ifndef __ASSEMBLY__

#ifdef CONFIG_ARM_RUNTIME_PATCH

struct patch_info {
	void		*insn;
	u16		 type;
	u8		 insn_size;
	u8		 data_size;
	u32		 data[0];
};

#define PATCH_IMM8		0x0001
struct patch_info_imm8 {
	u32		*imm;
	u32		 insn;
};

#define patch_next(p)	((void *)(p) + sizeof(*(p)) + (p)->data_size)
#define patch_data(p)	((void *)&(p)->data[0])

#define patch_stub(type, code, patch_data, ...)				\
	__asm__("@ patch stub\n"					\
		"1:\n"							\
		code							\
		"2:\n"							\
		"	.pushsection .runtime.patch.table, \"a\"\n"	\
		"3:\n"							\
		"	.word 1b\n"					\
		"	.hword (" __stringify(type) ")\n"		\
		"	.byte (2b-1b)\n"				\
		"	.byte (5f-4f)\n"				\
		"4:\n"							\
		patch_data						\
		"	.align\n"					\
		"5:\n"							\
		"	.popsection\n"					\
		__VA_ARGS__)

#define early_patch_stub(type, code, pad, patch_data, ...)		\
	__asm__("@ patch stub\n"					\
		"1:\n"							\
		"	b	6f\n"					\
		"	.fill	" __stringify(pad) ", 1, 0\n"		\
		"2:\n"							\
		"	.pushsection .runtime.patch.table, \"a\"\n"	\
		"3:\n"							\
		"	.word 1b\n"					\
		"	.hword (" __stringify(type) ")\n"		\
		"	.byte (2b-1b)\n"				\
		"	.byte (5f-4f)\n"				\
		"4:\n"							\
		patch_data						\
		"	.align\n"					\
		"5:\n"							\
		"	.popsection\n"					\
		"	.pushsection .runtime.patch.code, \"ax\"\n"	\
		"6:\n"							\
		code							\
		"	b 2b\n"						\
		"	.popsection\n"					\
		__VA_ARGS__)

/* constant used to force encoding */
#define __IMM8		(0x81 << 24)

/*
 * patch_imm8() - init-time specialized binary operation (imm8 operand)
 *		  This effectively does: to = from "insn" sym,
 *		  where the value of sym is fixed at init-time, and is patched
 *		  in as an immediate operand.  This value must be
 *		  representible as an 8-bit quantity with an optional
 *		  rotation.
 *
 *		  The stub code produced by this variant is non-functional
 *		  prior to patching.  Use early_patch_imm8() if you need the
 *		  code to be functional early on in the init sequence.
 */
#define patch_imm8(_insn, _to, _from, _sym, _ofs)			\
	patch_stub(							\
		/* type */						\
			PATCH_IMM8,					\
		/* code */						\
			_insn "	%[to], %[from], %[imm]\n",		\
		/* patch_data */					\
			".long " __stringify(_sym + _ofs) "\n"		\
			_insn "	%[to], %[from], %[imm]\n",		\
		/* operands */						\
			: [to]	 "=r" (_to)				\
			: [from] "r"  (_from),				\
			  [imm]	 "I"  (__IMM8),				\
				 "i"  (&(_sym))				\
			: "cc")

/*
 * patch_imm8_mov() - same as patch_imm8(), but for mov/mvn instructions
 */
#define patch_imm8_mov(_insn, _to, _sym, _ofs)				\
	patch_stub(							\
		/* type */						\
			PATCH_IMM8,					\
		/* code */						\
			_insn "	%[to], %[imm]\n",			\
		/* patch_data */					\
			".long " __stringify(_sym + _ofs) "\n"		\
			_insn "	%[to], %[imm]\n",			\
		/* operands */						\
			: [to]	"=r" (_to)				\
			: [imm]	"I"  (__IMM8),				\
				"i"  (&(_sym))				\
			: "cc")

/*
 * early_patch_imm8() - early functional variant of patch_imm8() above.  The
 *			same restrictions on the constant apply here.  This
 *			version emits workable (albeit inefficient) code at
 *			compile-time, and therefore functions even prior to
 *			patch application.
 */
#define early_patch_imm8(_insn, _to, _from, _sym, _ofs)			\
do {									\
	unsigned long __tmp;						\
	early_patch_stub(						\
		/* type */						\
			PATCH_IMM8,					\
		/* code */						\
			"ldr	%[tmp], =" __stringify(_sym + _ofs) "\n"\
			"ldr	%[tmp], [%[tmp]]\n"			\
			_insn "	%[to], %[from], %[tmp]\n",		\
		/* pad */						\
			0,						\
		/* patch_data */					\
			".long " __stringify(_sym + _ofs) "\n"		\
			_insn "	%[to], %[from], %[imm]\n",		\
		/* operands */						\
			: [to]	 "=r"  (_to),				\
			  [tmp]	 "=&r" (__tmp)				\
			: [from] "r"   (_from),				\
			  [imm]	 "I"   (__IMM8),			\
				 "i"   (&(_sym))			\
			: "cc");					\
} while (0)

#define early_patch_imm8_mov(_insn, _to, _sym, _ofs)			\
do {									\
	unsigned long __tmp;						\
	early_patch_stub(						\
		/* type */						\
			PATCH_IMM8					\
		/* code */						\
			"ldr	%[tmp], =" __stringify(_sym + _ofs) "\n"\
			"ldr	%[tmp], [%[tmp]]\n"			\
			_insn "	%[to], %[tmp]\n",			\
		/* pad */						\
			0,						\
		/* patch_data */					\
			".long " __stringify(_sym + _ofs) "\n"		\
			_insn " %[to], %[imm]\n",			\
		/* operands */						\
			: [to]	"=r"  (_to),				\
			  [tmp]	"=&r" (__tmp)				\
			: [imm]	"I"   (__IMM8),				\
				"i"   (&(_sym))				\
			: "cc");					\
} while (0)

int runtime_patch(const void *table, unsigned size);
void runtime_patch_kernel(void);

#else

static inline int runtime_patch(const void *table, unsigned size)
{
	return 0;
}

static inline void runtime_patch_kernel(void)
{
}

#endif /* CONFIG_ARM_RUNTIME_PATCH */

#endif /* __ASSEMBLY__ */

#endif /* __ASM_ARM_RUNTIME_PATCH_H */