xref: /linux/arch/powerpc/kernel/optprobes.c (revision 472b440fd26822c645befe459172dafdc2d225de)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Code for Kernel probes Jump optimization.
4  *
5  * Copyright 2017, Anju T, IBM Corp.
6  */
7 
8 #include <linux/kprobes.h>
9 #include <linux/jump_label.h>
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
13 #include <asm/kprobes.h>
14 #include <asm/ptrace.h>
15 #include <asm/cacheflush.h>
16 #include <asm/code-patching.h>
17 #include <asm/sstep.h>
18 #include <asm/ppc-opcode.h>
19 #include <asm/inst.h>
20 
21 #define TMPL_CALL_HDLR_IDX	\
22 	(optprobe_template_call_handler - optprobe_template_entry)
23 #define TMPL_EMULATE_IDX	\
24 	(optprobe_template_call_emulate - optprobe_template_entry)
25 #define TMPL_RET_IDX		\
26 	(optprobe_template_ret - optprobe_template_entry)
27 #define TMPL_OP_IDX		\
28 	(optprobe_template_op_address - optprobe_template_entry)
29 #define TMPL_INSN_IDX		\
30 	(optprobe_template_insn - optprobe_template_entry)
31 #define TMPL_END_IDX		\
32 	(optprobe_template_end - optprobe_template_entry)
33 
34 static bool insn_page_in_use;
35 
36 void *alloc_optinsn_page(void)
37 {
38 	if (insn_page_in_use)
39 		return NULL;
40 	insn_page_in_use = true;
41 	return &optinsn_slot;
42 }
43 
44 void free_optinsn_page(void *page)
45 {
46 	insn_page_in_use = false;
47 }
48 
49 /*
50  * Check if we can optimize this probe. Returns NIP post-emulation if this can
51  * be optimized and 0 otherwise.
52  */
53 static unsigned long can_optimize(struct kprobe *p)
54 {
55 	struct pt_regs regs;
56 	struct instruction_op op;
57 	unsigned long nip = 0;
58 
59 	/*
60 	 * kprobe placed for kretprobe during boot time
61 	 * has a 'nop' instruction, which can be emulated.
62 	 * So further checks can be skipped.
63 	 */
64 	if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
65 		return (unsigned long)p->addr + sizeof(kprobe_opcode_t);
66 
67 	/*
68 	 * We only support optimizing kernel addresses, but not
69 	 * module addresses.
70 	 *
71 	 * FIXME: Optimize kprobes placed in module addresses.
72 	 */
73 	if (!is_kernel_addr((unsigned long)p->addr))
74 		return 0;
75 
76 	memset(&regs, 0, sizeof(struct pt_regs));
77 	regs.nip = (unsigned long)p->addr;
78 	regs.trap = 0x0;
79 	regs.msr = MSR_KERNEL;
80 
81 	/*
82 	 * Kprobe placed in conditional branch instructions are
83 	 * not optimized, as we can't predict the nip prior with
84 	 * dummy pt_regs and can not ensure that the return branch
85 	 * from detour buffer falls in the range of address (i.e 32MB).
86 	 * A branch back from trampoline is set up in the detour buffer
87 	 * to the nip returned by the analyse_instr() here.
88 	 *
89 	 * Ensure that the instruction is not a conditional branch,
90 	 * and that can be emulated.
91 	 */
92 	if (!is_conditional_branch(ppc_inst_read((struct ppc_inst *)p->ainsn.insn)) &&
93 	    analyse_instr(&op, &regs,
94 			  ppc_inst_read((struct ppc_inst *)p->ainsn.insn)) == 1) {
95 		emulate_update_regs(&regs, &op);
96 		nip = regs.nip;
97 	}
98 
99 	return nip;
100 }
101 
102 static void optimized_callback(struct optimized_kprobe *op,
103 			       struct pt_regs *regs)
104 {
105 	/* This is possible if op is under delayed unoptimizing */
106 	if (kprobe_disabled(&op->kp))
107 		return;
108 
109 	preempt_disable();
110 
111 	if (kprobe_running()) {
112 		kprobes_inc_nmissed_count(&op->kp);
113 	} else {
114 		__this_cpu_write(current_kprobe, &op->kp);
115 		regs->nip = (unsigned long)op->kp.addr;
116 		get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
117 		opt_pre_handler(&op->kp, regs);
118 		__this_cpu_write(current_kprobe, NULL);
119 	}
120 
121 	preempt_enable_no_resched();
122 }
123 NOKPROBE_SYMBOL(optimized_callback);
124 
125 void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
126 {
127 	if (op->optinsn.insn) {
128 		free_optinsn_slot(op->optinsn.insn, 1);
129 		op->optinsn.insn = NULL;
130 	}
131 }
132 
133 static void patch_imm32_load_insns(unsigned long val, int reg, kprobe_opcode_t *addr)
134 {
135 	patch_instruction((struct ppc_inst *)addr,
136 			  ppc_inst(PPC_RAW_LIS(reg, IMM_H(val))));
137 	addr++;
138 
139 	patch_instruction((struct ppc_inst *)addr,
140 			  ppc_inst(PPC_RAW_ORI(reg, reg, IMM_L(val))));
141 }
142 
143 /*
144  * Generate instructions to load provided immediate 64-bit value
145  * to register 'reg' and patch these instructions at 'addr'.
146  */
147 static void patch_imm64_load_insns(unsigned long long val, int reg, kprobe_opcode_t *addr)
148 {
149 	/* lis reg,(op)@highest */
150 	patch_instruction((struct ppc_inst *)addr,
151 			  ppc_inst(PPC_INST_ADDIS | ___PPC_RT(reg) |
152 				   ((val >> 48) & 0xffff)));
153 	addr++;
154 
155 	/* ori reg,reg,(op)@higher */
156 	patch_instruction((struct ppc_inst *)addr,
157 			  ppc_inst(PPC_INST_ORI | ___PPC_RA(reg) |
158 				   ___PPC_RS(reg) | ((val >> 32) & 0xffff)));
159 	addr++;
160 
161 	/* rldicr reg,reg,32,31 */
162 	patch_instruction((struct ppc_inst *)addr,
163 			  ppc_inst(PPC_INST_RLDICR | ___PPC_RA(reg) |
164 				   ___PPC_RS(reg) | __PPC_SH64(32) | __PPC_ME64(31)));
165 	addr++;
166 
167 	/* oris reg,reg,(op)@h */
168 	patch_instruction((struct ppc_inst *)addr,
169 			  ppc_inst(PPC_INST_ORIS | ___PPC_RA(reg) |
170 				   ___PPC_RS(reg) | ((val >> 16) & 0xffff)));
171 	addr++;
172 
173 	/* ori reg,reg,(op)@l */
174 	patch_instruction((struct ppc_inst *)addr,
175 			  ppc_inst(PPC_INST_ORI | ___PPC_RA(reg) |
176 				   ___PPC_RS(reg) | (val & 0xffff)));
177 }
178 
179 static void patch_imm_load_insns(unsigned long val, int reg, kprobe_opcode_t *addr)
180 {
181 	if (IS_ENABLED(CONFIG_PPC64))
182 		patch_imm64_load_insns(val, reg, addr);
183 	else
184 		patch_imm32_load_insns(val, reg, addr);
185 }
186 
187 int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
188 {
189 	struct ppc_inst branch_op_callback, branch_emulate_step, temp;
190 	kprobe_opcode_t *op_callback_addr, *emulate_step_addr, *buff;
191 	long b_offset;
192 	unsigned long nip, size;
193 	int rc, i;
194 
195 	nip = can_optimize(p);
196 	if (!nip)
197 		return -EILSEQ;
198 
199 	/* Allocate instruction slot for detour buffer */
200 	buff = get_optinsn_slot();
201 	if (!buff)
202 		return -ENOMEM;
203 
204 	/*
205 	 * OPTPROBE uses 'b' instruction to branch to optinsn.insn.
206 	 *
207 	 * The target address has to be relatively nearby, to permit use
208 	 * of branch instruction in powerpc, because the address is specified
209 	 * in an immediate field in the instruction opcode itself, ie 24 bits
210 	 * in the opcode specify the address. Therefore the address should
211 	 * be within 32MB on either side of the current instruction.
212 	 */
213 	b_offset = (unsigned long)buff - (unsigned long)p->addr;
214 	if (!is_offset_in_branch_range(b_offset))
215 		goto error;
216 
217 	/* Check if the return address is also within 32MB range */
218 	b_offset = (unsigned long)(buff + TMPL_RET_IDX) -
219 			(unsigned long)nip;
220 	if (!is_offset_in_branch_range(b_offset))
221 		goto error;
222 
223 	/* Setup template */
224 	/* We can optimize this via patch_instruction_window later */
225 	size = (TMPL_END_IDX * sizeof(kprobe_opcode_t)) / sizeof(int);
226 	pr_devel("Copying template to %p, size %lu\n", buff, size);
227 	for (i = 0; i < size; i++) {
228 		rc = patch_instruction((struct ppc_inst *)(buff + i),
229 				       ppc_inst(*(optprobe_template_entry + i)));
230 		if (rc < 0)
231 			goto error;
232 	}
233 
234 	/*
235 	 * Fixup the template with instructions to:
236 	 * 1. load the address of the actual probepoint
237 	 */
238 	patch_imm_load_insns((unsigned long)op, 3, buff + TMPL_OP_IDX);
239 
240 	/*
241 	 * 2. branch to optimized_callback() and emulate_step()
242 	 */
243 	op_callback_addr = (kprobe_opcode_t *)ppc_kallsyms_lookup_name("optimized_callback");
244 	emulate_step_addr = (kprobe_opcode_t *)ppc_kallsyms_lookup_name("emulate_step");
245 	if (!op_callback_addr || !emulate_step_addr) {
246 		WARN(1, "Unable to lookup optimized_callback()/emulate_step()\n");
247 		goto error;
248 	}
249 
250 	rc = create_branch(&branch_op_callback,
251 			   (struct ppc_inst *)(buff + TMPL_CALL_HDLR_IDX),
252 			   (unsigned long)op_callback_addr,
253 			   BRANCH_SET_LINK);
254 
255 	rc |= create_branch(&branch_emulate_step,
256 			    (struct ppc_inst *)(buff + TMPL_EMULATE_IDX),
257 			    (unsigned long)emulate_step_addr,
258 			    BRANCH_SET_LINK);
259 
260 	if (rc)
261 		goto error;
262 
263 	patch_instruction((struct ppc_inst *)(buff + TMPL_CALL_HDLR_IDX),
264 			  branch_op_callback);
265 	patch_instruction((struct ppc_inst *)(buff + TMPL_EMULATE_IDX),
266 			  branch_emulate_step);
267 
268 	/*
269 	 * 3. load instruction to be emulated into relevant register, and
270 	 */
271 	temp = ppc_inst_read((struct ppc_inst *)p->ainsn.insn);
272 	patch_imm_load_insns(ppc_inst_as_ulong(temp), 4, buff + TMPL_INSN_IDX);
273 
274 	/*
275 	 * 4. branch back from trampoline
276 	 */
277 	patch_branch((struct ppc_inst *)(buff + TMPL_RET_IDX), (unsigned long)nip, 0);
278 
279 	flush_icache_range((unsigned long)buff,
280 			   (unsigned long)(&buff[TMPL_END_IDX]));
281 
282 	op->optinsn.insn = buff;
283 
284 	return 0;
285 
286 error:
287 	free_optinsn_slot(buff, 0);
288 	return -ERANGE;
289 
290 }
291 
292 int arch_prepared_optinsn(struct arch_optimized_insn *optinsn)
293 {
294 	return optinsn->insn != NULL;
295 }
296 
297 /*
298  * On powerpc, Optprobes always replaces one instruction (4 bytes
299  * aligned and 4 bytes long). It is impossible to encounter another
300  * kprobe in this address range. So always return 0.
301  */
302 int arch_check_optimized_kprobe(struct optimized_kprobe *op)
303 {
304 	return 0;
305 }
306 
307 void arch_optimize_kprobes(struct list_head *oplist)
308 {
309 	struct ppc_inst instr;
310 	struct optimized_kprobe *op;
311 	struct optimized_kprobe *tmp;
312 
313 	list_for_each_entry_safe(op, tmp, oplist, list) {
314 		/*
315 		 * Backup instructions which will be replaced
316 		 * by jump address
317 		 */
318 		memcpy(op->optinsn.copied_insn, op->kp.addr,
319 					       RELATIVEJUMP_SIZE);
320 		create_branch(&instr,
321 			      (struct ppc_inst *)op->kp.addr,
322 			      (unsigned long)op->optinsn.insn, 0);
323 		patch_instruction((struct ppc_inst *)op->kp.addr, instr);
324 		list_del_init(&op->list);
325 	}
326 }
327 
328 void arch_unoptimize_kprobe(struct optimized_kprobe *op)
329 {
330 	arch_arm_kprobe(&op->kp);
331 }
332 
333 void arch_unoptimize_kprobes(struct list_head *oplist,
334 			     struct list_head *done_list)
335 {
336 	struct optimized_kprobe *op;
337 	struct optimized_kprobe *tmp;
338 
339 	list_for_each_entry_safe(op, tmp, oplist, list) {
340 		arch_unoptimize_kprobe(op);
341 		list_move(&op->list, done_list);
342 	}
343 }
344 
345 int arch_within_optimized_kprobe(struct optimized_kprobe *op,
346 				 unsigned long addr)
347 {
348 	return ((unsigned long)op->kp.addr <= addr &&
349 		(unsigned long)op->kp.addr + RELATIVEJUMP_SIZE > addr);
350 }
351