xref: /linux/arch/powerpc/lib/code-patching.c (revision 217862d9b98bf08958d57fd7b31b9de0f1a9477d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2aaddd3eaSMichael Ellerman /*
3aaddd3eaSMichael Ellerman  *  Copyright 2008 Michael Ellerman, IBM Corporation.
4aaddd3eaSMichael Ellerman  */
5aaddd3eaSMichael Ellerman 
6aaddd3eaSMichael Ellerman #include <linux/kernel.h>
771f6e58eSNaveen N. Rao #include <linux/kprobes.h>
8ae0dc736SMichael Ellerman #include <linux/vmalloc.h>
9ae0dc736SMichael Ellerman #include <linux/init.h>
1027ac792cSAndrea Righi #include <linux/mm.h>
1137bc3e5fSBalbir Singh #include <linux/cpuhotplug.h>
1237bc3e5fSBalbir Singh #include <linux/slab.h>
137c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
14aaddd3eaSMichael Ellerman 
1537bc3e5fSBalbir Singh #include <asm/pgtable.h>
1637bc3e5fSBalbir Singh #include <asm/tlbflush.h>
1737bc3e5fSBalbir Singh #include <asm/page.h>
1837bc3e5fSBalbir Singh #include <asm/code-patching.h>
19252eb558SChristophe Leroy #include <asm/setup.h>
2075346251SJordan Niethe #include <asm/inst.h>
21aaddd3eaSMichael Ellerman 
228cf4c057SChristophe Leroy static int __patch_instruction(unsigned int *exec_addr, unsigned int instr,
238cf4c057SChristophe Leroy 			       unsigned int *patch_addr)
24aaddd3eaSMichael Ellerman {
25ef296729SRussell Currey 	int err = 0;
26b6e37968SSteven Rostedt 
27ef296729SRussell Currey 	__put_user_asm(instr, patch_addr, err, "stw");
28b6e37968SSteven Rostedt 	if (err)
29b6e37968SSteven Rostedt 		return err;
3037bc3e5fSBalbir Singh 
318cf4c057SChristophe Leroy 	asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
328cf4c057SChristophe Leroy 							    "r" (exec_addr));
3337bc3e5fSBalbir Singh 
34b6e37968SSteven Rostedt 	return 0;
35aaddd3eaSMichael Ellerman }
36aaddd3eaSMichael Ellerman 
378183d99fSChristophe Leroy int raw_patch_instruction(unsigned int *addr, unsigned int instr)
388cf4c057SChristophe Leroy {
398cf4c057SChristophe Leroy 	return __patch_instruction(addr, instr, addr);
408cf4c057SChristophe Leroy }
418cf4c057SChristophe Leroy 
4237bc3e5fSBalbir Singh #ifdef CONFIG_STRICT_KERNEL_RWX
4337bc3e5fSBalbir Singh static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
4437bc3e5fSBalbir Singh 
4537bc3e5fSBalbir Singh static int text_area_cpu_up(unsigned int cpu)
4637bc3e5fSBalbir Singh {
4737bc3e5fSBalbir Singh 	struct vm_struct *area;
4837bc3e5fSBalbir Singh 
4937bc3e5fSBalbir Singh 	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
5037bc3e5fSBalbir Singh 	if (!area) {
5137bc3e5fSBalbir Singh 		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
5237bc3e5fSBalbir Singh 			cpu);
5337bc3e5fSBalbir Singh 		return -1;
5437bc3e5fSBalbir Singh 	}
5537bc3e5fSBalbir Singh 	this_cpu_write(text_poke_area, area);
5637bc3e5fSBalbir Singh 
5737bc3e5fSBalbir Singh 	return 0;
5837bc3e5fSBalbir Singh }
5937bc3e5fSBalbir Singh 
6037bc3e5fSBalbir Singh static int text_area_cpu_down(unsigned int cpu)
6137bc3e5fSBalbir Singh {
6237bc3e5fSBalbir Singh 	free_vm_area(this_cpu_read(text_poke_area));
6337bc3e5fSBalbir Singh 	return 0;
6437bc3e5fSBalbir Singh }
6537bc3e5fSBalbir Singh 
6637bc3e5fSBalbir Singh /*
6737bc3e5fSBalbir Singh  * Run as a late init call. This allows all the boot time patching to be done
6837bc3e5fSBalbir Singh  * simply by patching the code, and then we're called here prior to
6937bc3e5fSBalbir Singh  * mark_rodata_ro(), which happens after all init calls are run. Although
7037bc3e5fSBalbir Singh  * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
7137bc3e5fSBalbir Singh  * it as being preferable to a kernel that will crash later when someone tries
7237bc3e5fSBalbir Singh  * to use patch_instruction().
7337bc3e5fSBalbir Singh  */
7437bc3e5fSBalbir Singh static int __init setup_text_poke_area(void)
7537bc3e5fSBalbir Singh {
7637bc3e5fSBalbir Singh 	BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
7737bc3e5fSBalbir Singh 		"powerpc/text_poke:online", text_area_cpu_up,
7837bc3e5fSBalbir Singh 		text_area_cpu_down));
7937bc3e5fSBalbir Singh 
8037bc3e5fSBalbir Singh 	return 0;
8137bc3e5fSBalbir Singh }
8237bc3e5fSBalbir Singh late_initcall(setup_text_poke_area);
8337bc3e5fSBalbir Singh 
8437bc3e5fSBalbir Singh /*
8537bc3e5fSBalbir Singh  * This can be called for kernel text or a module.
8637bc3e5fSBalbir Singh  */
8737bc3e5fSBalbir Singh static int map_patch_area(void *addr, unsigned long text_poke_addr)
8837bc3e5fSBalbir Singh {
8937bc3e5fSBalbir Singh 	unsigned long pfn;
9037bc3e5fSBalbir Singh 	int err;
9137bc3e5fSBalbir Singh 
9237bc3e5fSBalbir Singh 	if (is_vmalloc_addr(addr))
9337bc3e5fSBalbir Singh 		pfn = vmalloc_to_pfn(addr);
9437bc3e5fSBalbir Singh 	else
9537bc3e5fSBalbir Singh 		pfn = __pa_symbol(addr) >> PAGE_SHIFT;
9637bc3e5fSBalbir Singh 
97c766ee72SChristophe Leroy 	err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
9837bc3e5fSBalbir Singh 
9937bc3e5fSBalbir Singh 	pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
10037bc3e5fSBalbir Singh 	if (err)
10137bc3e5fSBalbir Singh 		return -1;
10237bc3e5fSBalbir Singh 
10337bc3e5fSBalbir Singh 	return 0;
10437bc3e5fSBalbir Singh }
10537bc3e5fSBalbir Singh 
10637bc3e5fSBalbir Singh static inline int unmap_patch_area(unsigned long addr)
10737bc3e5fSBalbir Singh {
10837bc3e5fSBalbir Singh 	pte_t *ptep;
10937bc3e5fSBalbir Singh 	pmd_t *pmdp;
11037bc3e5fSBalbir Singh 	pud_t *pudp;
11137bc3e5fSBalbir Singh 	pgd_t *pgdp;
11237bc3e5fSBalbir Singh 
11337bc3e5fSBalbir Singh 	pgdp = pgd_offset_k(addr);
11437bc3e5fSBalbir Singh 	if (unlikely(!pgdp))
11537bc3e5fSBalbir Singh 		return -EINVAL;
11637bc3e5fSBalbir Singh 
11737bc3e5fSBalbir Singh 	pudp = pud_offset(pgdp, addr);
11837bc3e5fSBalbir Singh 	if (unlikely(!pudp))
11937bc3e5fSBalbir Singh 		return -EINVAL;
12037bc3e5fSBalbir Singh 
12137bc3e5fSBalbir Singh 	pmdp = pmd_offset(pudp, addr);
12237bc3e5fSBalbir Singh 	if (unlikely(!pmdp))
12337bc3e5fSBalbir Singh 		return -EINVAL;
12437bc3e5fSBalbir Singh 
12537bc3e5fSBalbir Singh 	ptep = pte_offset_kernel(pmdp, addr);
12637bc3e5fSBalbir Singh 	if (unlikely(!ptep))
12737bc3e5fSBalbir Singh 		return -EINVAL;
12837bc3e5fSBalbir Singh 
12937bc3e5fSBalbir Singh 	pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
13037bc3e5fSBalbir Singh 
13137bc3e5fSBalbir Singh 	/*
13237bc3e5fSBalbir Singh 	 * In hash, pte_clear flushes the tlb, in radix, we have to
13337bc3e5fSBalbir Singh 	 */
13437bc3e5fSBalbir Singh 	pte_clear(&init_mm, addr, ptep);
13537bc3e5fSBalbir Singh 	flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
13637bc3e5fSBalbir Singh 
13737bc3e5fSBalbir Singh 	return 0;
13837bc3e5fSBalbir Singh }
13937bc3e5fSBalbir Singh 
140b45ba4a5SChristophe Leroy static int do_patch_instruction(unsigned int *addr, unsigned int instr)
14137bc3e5fSBalbir Singh {
14237bc3e5fSBalbir Singh 	int err;
1438cf4c057SChristophe Leroy 	unsigned int *patch_addr = NULL;
14437bc3e5fSBalbir Singh 	unsigned long flags;
14537bc3e5fSBalbir Singh 	unsigned long text_poke_addr;
14637bc3e5fSBalbir Singh 	unsigned long kaddr = (unsigned long)addr;
14737bc3e5fSBalbir Singh 
14837bc3e5fSBalbir Singh 	/*
14937bc3e5fSBalbir Singh 	 * During early early boot patch_instruction is called
15037bc3e5fSBalbir Singh 	 * when text_poke_area is not ready, but we still need
15137bc3e5fSBalbir Singh 	 * to allow patching. We just do the plain old patching
15237bc3e5fSBalbir Singh 	 */
1538183d99fSChristophe Leroy 	if (!this_cpu_read(text_poke_area))
1548cf4c057SChristophe Leroy 		return raw_patch_instruction(addr, instr);
15537bc3e5fSBalbir Singh 
15637bc3e5fSBalbir Singh 	local_irq_save(flags);
15737bc3e5fSBalbir Singh 
15837bc3e5fSBalbir Singh 	text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
15937bc3e5fSBalbir Singh 	if (map_patch_area(addr, text_poke_addr)) {
16037bc3e5fSBalbir Singh 		err = -1;
16137bc3e5fSBalbir Singh 		goto out;
16237bc3e5fSBalbir Singh 	}
16337bc3e5fSBalbir Singh 
1648cf4c057SChristophe Leroy 	patch_addr = (unsigned int *)(text_poke_addr) +
16537bc3e5fSBalbir Singh 			((kaddr & ~PAGE_MASK) / sizeof(unsigned int));
16637bc3e5fSBalbir Singh 
1678cf4c057SChristophe Leroy 	__patch_instruction(addr, instr, patch_addr);
16837bc3e5fSBalbir Singh 
16937bc3e5fSBalbir Singh 	err = unmap_patch_area(text_poke_addr);
17037bc3e5fSBalbir Singh 	if (err)
17137bc3e5fSBalbir Singh 		pr_warn("failed to unmap %lx\n", text_poke_addr);
17237bc3e5fSBalbir Singh 
17337bc3e5fSBalbir Singh out:
17437bc3e5fSBalbir Singh 	local_irq_restore(flags);
17537bc3e5fSBalbir Singh 
17637bc3e5fSBalbir Singh 	return err;
17737bc3e5fSBalbir Singh }
17837bc3e5fSBalbir Singh #else /* !CONFIG_STRICT_KERNEL_RWX */
17937bc3e5fSBalbir Singh 
180b45ba4a5SChristophe Leroy static int do_patch_instruction(unsigned int *addr, unsigned int instr)
18137bc3e5fSBalbir Singh {
1828cf4c057SChristophe Leroy 	return raw_patch_instruction(addr, instr);
18337bc3e5fSBalbir Singh }
18437bc3e5fSBalbir Singh 
18537bc3e5fSBalbir Singh #endif /* CONFIG_STRICT_KERNEL_RWX */
186b45ba4a5SChristophe Leroy 
187b45ba4a5SChristophe Leroy int patch_instruction(unsigned int *addr, unsigned int instr)
188b45ba4a5SChristophe Leroy {
189b45ba4a5SChristophe Leroy 	/* Make sure we aren't patching a freed init section */
190b45ba4a5SChristophe Leroy 	if (init_mem_is_free && init_section_contains(addr, 4)) {
191b45ba4a5SChristophe Leroy 		pr_debug("Skipping init section patching addr: 0x%px\n", addr);
192b45ba4a5SChristophe Leroy 		return 0;
193b45ba4a5SChristophe Leroy 	}
194b45ba4a5SChristophe Leroy 	return do_patch_instruction(addr, instr);
195b45ba4a5SChristophe Leroy }
19637bc3e5fSBalbir Singh NOKPROBE_SYMBOL(patch_instruction);
19737bc3e5fSBalbir Singh 
198b6e37968SSteven Rostedt int patch_branch(unsigned int *addr, unsigned long target, int flags)
199e7a57273SMichael Ellerman {
2007c95d889SJordan Niethe 	unsigned int instr;
2017c95d889SJordan Niethe 
2027c95d889SJordan Niethe 	create_branch(&instr, addr, target, flags);
2037c95d889SJordan Niethe 	return patch_instruction(addr, instr);
204e7a57273SMichael Ellerman }
205e7a57273SMichael Ellerman 
206ebfa50dfSAnju T bool is_offset_in_branch_range(long offset)
207ebfa50dfSAnju T {
208ebfa50dfSAnju T 	/*
209ebfa50dfSAnju T 	 * Powerpc branch instruction is :
210ebfa50dfSAnju T 	 *
211ebfa50dfSAnju T 	 *  0         6                 30   31
212ebfa50dfSAnju T 	 *  +---------+----------------+---+---+
213ebfa50dfSAnju T 	 *  | opcode  |     LI         |AA |LK |
214ebfa50dfSAnju T 	 *  +---------+----------------+---+---+
215ebfa50dfSAnju T 	 *  Where AA = 0 and LK = 0
216ebfa50dfSAnju T 	 *
217ebfa50dfSAnju T 	 * LI is a signed 24 bits integer. The real branch offset is computed
218ebfa50dfSAnju T 	 * by: imm32 = SignExtend(LI:'0b00', 32);
219ebfa50dfSAnju T 	 *
220ebfa50dfSAnju T 	 * So the maximum forward branch should be:
221ebfa50dfSAnju T 	 *   (0x007fffff << 2) = 0x01fffffc =  0x1fffffc
222ebfa50dfSAnju T 	 * The maximum backward branch should be:
223ebfa50dfSAnju T 	 *   (0xff800000 << 2) = 0xfe000000 = -0x2000000
224ebfa50dfSAnju T 	 */
225ebfa50dfSAnju T 	return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
226ebfa50dfSAnju T }
227ebfa50dfSAnju T 
22851c9c084SAnju T /*
22951c9c084SAnju T  * Helper to check if a given instruction is a conditional branch
23051c9c084SAnju T  * Derived from the conditional checks in analyse_instr()
23151c9c084SAnju T  */
23271f6e58eSNaveen N. Rao bool is_conditional_branch(unsigned int instr)
23351c9c084SAnju T {
2348094892dSJordan Niethe 	unsigned int opcode = ppc_inst_primary_opcode(instr);
23551c9c084SAnju T 
23651c9c084SAnju T 	if (opcode == 16)       /* bc, bca, bcl, bcla */
23751c9c084SAnju T 		return true;
23851c9c084SAnju T 	if (opcode == 19) {
239777e26f0SJordan Niethe 		switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
24051c9c084SAnju T 		case 16:        /* bclr, bclrl */
24151c9c084SAnju T 		case 528:       /* bcctr, bcctrl */
24251c9c084SAnju T 		case 560:       /* bctar, bctarl */
24351c9c084SAnju T 			return true;
24451c9c084SAnju T 		}
24551c9c084SAnju T 	}
24651c9c084SAnju T 	return false;
24751c9c084SAnju T }
24871f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(is_conditional_branch);
24951c9c084SAnju T 
2507c95d889SJordan Niethe int create_branch(unsigned int *instr,
2517c95d889SJordan Niethe 		  const unsigned int *addr,
252e7a57273SMichael Ellerman 		  unsigned long target, int flags)
253aaddd3eaSMichael Ellerman {
254053a858eSMichael Ellerman 	long offset;
255aaddd3eaSMichael Ellerman 
2567c95d889SJordan Niethe 	*instr = 0;
257053a858eSMichael Ellerman 	offset = target;
258aaddd3eaSMichael Ellerman 	if (! (flags & BRANCH_ABSOLUTE))
259053a858eSMichael Ellerman 		offset = offset - (unsigned long)addr;
260053a858eSMichael Ellerman 
261053a858eSMichael Ellerman 	/* Check we can represent the target in the instruction format */
262ebfa50dfSAnju T 	if (!is_offset_in_branch_range(offset))
2637c95d889SJordan Niethe 		return 1;
264aaddd3eaSMichael Ellerman 
265aaddd3eaSMichael Ellerman 	/* Mask out the flags and target, so they don't step on each other. */
2667c95d889SJordan Niethe 	*instr = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
267aaddd3eaSMichael Ellerman 
2687c95d889SJordan Niethe 	return 0;
269aaddd3eaSMichael Ellerman }
270411781a2SMichael Ellerman 
2717c95d889SJordan Niethe int create_cond_branch(unsigned int *instr, const unsigned int *addr,
272411781a2SMichael Ellerman 		       unsigned long target, int flags)
273411781a2SMichael Ellerman {
274411781a2SMichael Ellerman 	long offset;
275411781a2SMichael Ellerman 
276411781a2SMichael Ellerman 	offset = target;
277411781a2SMichael Ellerman 	if (! (flags & BRANCH_ABSOLUTE))
278411781a2SMichael Ellerman 		offset = offset - (unsigned long)addr;
279411781a2SMichael Ellerman 
280411781a2SMichael Ellerman 	/* Check we can represent the target in the instruction format */
281411781a2SMichael Ellerman 	if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
2827c95d889SJordan Niethe 		return 1;
283411781a2SMichael Ellerman 
284411781a2SMichael Ellerman 	/* Mask out the flags and target, so they don't step on each other. */
2857c95d889SJordan Niethe 	*instr = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
286411781a2SMichael Ellerman 
2877c95d889SJordan Niethe 	return 0;
288411781a2SMichael Ellerman }
289411781a2SMichael Ellerman 
290411781a2SMichael Ellerman static unsigned int branch_opcode(unsigned int instr)
291411781a2SMichael Ellerman {
2928094892dSJordan Niethe 	return ppc_inst_primary_opcode(instr) & 0x3F;
293411781a2SMichael Ellerman }
294411781a2SMichael Ellerman 
295411781a2SMichael Ellerman static int instr_is_branch_iform(unsigned int instr)
296411781a2SMichael Ellerman {
297411781a2SMichael Ellerman 	return branch_opcode(instr) == 18;
298411781a2SMichael Ellerman }
299411781a2SMichael Ellerman 
300411781a2SMichael Ellerman static int instr_is_branch_bform(unsigned int instr)
301411781a2SMichael Ellerman {
302411781a2SMichael Ellerman 	return branch_opcode(instr) == 16;
303411781a2SMichael Ellerman }
304411781a2SMichael Ellerman 
305411781a2SMichael Ellerman int instr_is_relative_branch(unsigned int instr)
306411781a2SMichael Ellerman {
307777e26f0SJordan Niethe 	if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
308411781a2SMichael Ellerman 		return 0;
309411781a2SMichael Ellerman 
310411781a2SMichael Ellerman 	return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
311411781a2SMichael Ellerman }
312411781a2SMichael Ellerman 
313b9eab08dSJosh Poimboeuf int instr_is_relative_link_branch(unsigned int instr)
314b9eab08dSJosh Poimboeuf {
315777e26f0SJordan Niethe 	return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
316b9eab08dSJosh Poimboeuf }
317b9eab08dSJosh Poimboeuf 
318411781a2SMichael Ellerman static unsigned long branch_iform_target(const unsigned int *instr)
319411781a2SMichael Ellerman {
320411781a2SMichael Ellerman 	signed long imm;
321411781a2SMichael Ellerman 
322777e26f0SJordan Niethe 	imm = ppc_inst_val(*instr) & 0x3FFFFFC;
323411781a2SMichael Ellerman 
324411781a2SMichael Ellerman 	/* If the top bit of the immediate value is set this is negative */
325411781a2SMichael Ellerman 	if (imm & 0x2000000)
326411781a2SMichael Ellerman 		imm -= 0x4000000;
327411781a2SMichael Ellerman 
328777e26f0SJordan Niethe 	if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
329411781a2SMichael Ellerman 		imm += (unsigned long)instr;
330411781a2SMichael Ellerman 
331411781a2SMichael Ellerman 	return (unsigned long)imm;
332411781a2SMichael Ellerman }
333411781a2SMichael Ellerman 
334411781a2SMichael Ellerman static unsigned long branch_bform_target(const unsigned int *instr)
335411781a2SMichael Ellerman {
336411781a2SMichael Ellerman 	signed long imm;
337411781a2SMichael Ellerman 
338777e26f0SJordan Niethe 	imm = ppc_inst_val(*instr) & 0xFFFC;
339411781a2SMichael Ellerman 
340411781a2SMichael Ellerman 	/* If the top bit of the immediate value is set this is negative */
341411781a2SMichael Ellerman 	if (imm & 0x8000)
342411781a2SMichael Ellerman 		imm -= 0x10000;
343411781a2SMichael Ellerman 
344777e26f0SJordan Niethe 	if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
345411781a2SMichael Ellerman 		imm += (unsigned long)instr;
346411781a2SMichael Ellerman 
347411781a2SMichael Ellerman 	return (unsigned long)imm;
348411781a2SMichael Ellerman }
349411781a2SMichael Ellerman 
350411781a2SMichael Ellerman unsigned long branch_target(const unsigned int *instr)
351411781a2SMichael Ellerman {
352411781a2SMichael Ellerman 	if (instr_is_branch_iform(*instr))
353411781a2SMichael Ellerman 		return branch_iform_target(instr);
354411781a2SMichael Ellerman 	else if (instr_is_branch_bform(*instr))
355411781a2SMichael Ellerman 		return branch_bform_target(instr);
356411781a2SMichael Ellerman 
357411781a2SMichael Ellerman 	return 0;
358411781a2SMichael Ellerman }
359411781a2SMichael Ellerman 
360411781a2SMichael Ellerman int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
361411781a2SMichael Ellerman {
362411781a2SMichael Ellerman 	if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
363411781a2SMichael Ellerman 		return branch_target(instr) == addr;
364411781a2SMichael Ellerman 
365411781a2SMichael Ellerman 	return 0;
366411781a2SMichael Ellerman }
367411781a2SMichael Ellerman 
3687c95d889SJordan Niethe int translate_branch(unsigned int *instr, const unsigned int *dest,
3697c95d889SJordan Niethe 		     const unsigned int *src)
370411781a2SMichael Ellerman {
371411781a2SMichael Ellerman 	unsigned long target;
372411781a2SMichael Ellerman 
373411781a2SMichael Ellerman 	target = branch_target(src);
374411781a2SMichael Ellerman 
375411781a2SMichael Ellerman 	if (instr_is_branch_iform(*src))
376777e26f0SJordan Niethe 		return create_branch(instr, dest, target, ppc_inst_val(*src));
377411781a2SMichael Ellerman 	else if (instr_is_branch_bform(*src))
378777e26f0SJordan Niethe 		return create_cond_branch(instr, dest, target, ppc_inst_val(*src));
379411781a2SMichael Ellerman 
3807c95d889SJordan Niethe 	return 1;
381411781a2SMichael Ellerman }
382ae0dc736SMichael Ellerman 
3831e8341aeSKevin Hao #ifdef CONFIG_PPC_BOOK3E_64
3841e8341aeSKevin Hao void __patch_exception(int exc, unsigned long addr)
3851e8341aeSKevin Hao {
3861e8341aeSKevin Hao 	extern unsigned int interrupt_base_book3e;
3871e8341aeSKevin Hao 	unsigned int *ibase = &interrupt_base_book3e;
3881e8341aeSKevin Hao 
3891e8341aeSKevin Hao 	/* Our exceptions vectors start with a NOP and -then- a branch
3901e8341aeSKevin Hao 	 * to deal with single stepping from userspace which stops on
3911e8341aeSKevin Hao 	 * the second instruction. Thus we need to patch the second
3921e8341aeSKevin Hao 	 * instruction of the exception, not the first one
3931e8341aeSKevin Hao 	 */
3941e8341aeSKevin Hao 
3951e8341aeSKevin Hao 	patch_branch(ibase + (exc / 4) + 1, addr, 0);
3961e8341aeSKevin Hao }
3971e8341aeSKevin Hao #endif
398ae0dc736SMichael Ellerman 
399ae0dc736SMichael Ellerman #ifdef CONFIG_CODE_PATCHING_SELFTEST
400ae0dc736SMichael Ellerman 
401ae0dc736SMichael Ellerman static void __init test_trampoline(void)
402ae0dc736SMichael Ellerman {
403ae0dc736SMichael Ellerman 	asm ("nop;\n");
404ae0dc736SMichael Ellerman }
405ae0dc736SMichael Ellerman 
406ae0dc736SMichael Ellerman #define check(x)	\
407ae0dc736SMichael Ellerman 	if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
408ae0dc736SMichael Ellerman 
409ae0dc736SMichael Ellerman static void __init test_branch_iform(void)
410ae0dc736SMichael Ellerman {
4117c95d889SJordan Niethe 	int err;
412ae0dc736SMichael Ellerman 	unsigned int instr;
413ae0dc736SMichael Ellerman 	unsigned long addr;
414ae0dc736SMichael Ellerman 
415ae0dc736SMichael Ellerman 	addr = (unsigned long)&instr;
416ae0dc736SMichael Ellerman 
417ae0dc736SMichael Ellerman 	/* The simplest case, branch to self, no flags */
41875346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x48000000)));
419ae0dc736SMichael Ellerman 	/* All bits of target set, and flags */
42075346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
421ae0dc736SMichael Ellerman 	/* High bit of opcode set, which is wrong */
42275346251SJordan Niethe 	check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
423ae0dc736SMichael Ellerman 	/* Middle bits of opcode set, which is wrong */
42475346251SJordan Niethe 	check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
425ae0dc736SMichael Ellerman 
426ae0dc736SMichael Ellerman 	/* Simplest case, branch to self with link */
42775346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x48000001)));
428ae0dc736SMichael Ellerman 	/* All bits of targets set */
42975346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
430ae0dc736SMichael Ellerman 	/* Some bits of targets set */
43175346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
432ae0dc736SMichael Ellerman 	/* Must be a valid branch to start with */
43375346251SJordan Niethe 	check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
434ae0dc736SMichael Ellerman 
435ae0dc736SMichael Ellerman 	/* Absolute branch to 0x100 */
43675346251SJordan Niethe 	instr = ppc_inst(0x48000103);
437ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, 0x100));
438ae0dc736SMichael Ellerman 	/* Absolute branch to 0x420fc */
43975346251SJordan Niethe 	instr = ppc_inst(0x480420ff);
440ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, 0x420fc));
441ae0dc736SMichael Ellerman 	/* Maximum positive relative branch, + 20MB - 4B */
44275346251SJordan Niethe 	instr = ppc_inst(0x49fffffc);
443ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
444ae0dc736SMichael Ellerman 	/* Smallest negative relative branch, - 4B */
44575346251SJordan Niethe 	instr = ppc_inst(0x4bfffffc);
446ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 4));
447ae0dc736SMichael Ellerman 	/* Largest negative relative branch, - 32 MB */
44875346251SJordan Niethe 	instr = ppc_inst(0x4a000000);
449ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
450ae0dc736SMichael Ellerman 
451ae0dc736SMichael Ellerman 	/* Branch to self, with link */
4527c95d889SJordan Niethe 	err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK);
453ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr));
454ae0dc736SMichael Ellerman 
455ae0dc736SMichael Ellerman 	/* Branch to self - 0x100, with link */
4567c95d889SJordan Niethe 	err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK);
457ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x100));
458ae0dc736SMichael Ellerman 
459ae0dc736SMichael Ellerman 	/* Branch to self + 0x100, no link */
4607c95d889SJordan Niethe 	err = create_branch(&instr, &instr, addr + 0x100, 0);
461ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr + 0x100));
462ae0dc736SMichael Ellerman 
463ae0dc736SMichael Ellerman 	/* Maximum relative negative offset, - 32 MB */
4647c95d889SJordan Niethe 	err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK);
465ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
466ae0dc736SMichael Ellerman 
467ae0dc736SMichael Ellerman 	/* Out of range relative negative offset, - 32 MB + 4*/
4687c95d889SJordan Niethe 	err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK);
4697c95d889SJordan Niethe 	check(err);
470ae0dc736SMichael Ellerman 
471ae0dc736SMichael Ellerman 	/* Out of range relative positive offset, + 32 MB */
4727c95d889SJordan Niethe 	err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK);
4737c95d889SJordan Niethe 	check(err);
474ae0dc736SMichael Ellerman 
475ae0dc736SMichael Ellerman 	/* Unaligned target */
4767c95d889SJordan Niethe 	err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK);
4777c95d889SJordan Niethe 	check(err);
478ae0dc736SMichael Ellerman 
479ae0dc736SMichael Ellerman 	/* Check flags are masked correctly */
4807c95d889SJordan Niethe 	err = create_branch(&instr, &instr, addr, 0xFFFFFFFC);
481ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr));
482*217862d9SJordan Niethe 	check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
483ae0dc736SMichael Ellerman }
484ae0dc736SMichael Ellerman 
485ae0dc736SMichael Ellerman static void __init test_create_function_call(void)
486ae0dc736SMichael Ellerman {
487ae0dc736SMichael Ellerman 	unsigned int *iptr;
488ae0dc736SMichael Ellerman 	unsigned long dest;
4897c95d889SJordan Niethe 	unsigned int instr;
490ae0dc736SMichael Ellerman 
491ae0dc736SMichael Ellerman 	/* Check we can create a function call */
492ae0dc736SMichael Ellerman 	iptr = (unsigned int *)ppc_function_entry(test_trampoline);
493ae0dc736SMichael Ellerman 	dest = ppc_function_entry(test_create_function_call);
4947c95d889SJordan Niethe 	create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
4957c95d889SJordan Niethe 	patch_instruction(iptr, instr);
496ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(iptr, dest));
497ae0dc736SMichael Ellerman }
498ae0dc736SMichael Ellerman 
499ae0dc736SMichael Ellerman static void __init test_branch_bform(void)
500ae0dc736SMichael Ellerman {
5017c95d889SJordan Niethe 	int err;
502ae0dc736SMichael Ellerman 	unsigned long addr;
503ae0dc736SMichael Ellerman 	unsigned int *iptr, instr, flags;
504ae0dc736SMichael Ellerman 
505ae0dc736SMichael Ellerman 	iptr = &instr;
506ae0dc736SMichael Ellerman 	addr = (unsigned long)iptr;
507ae0dc736SMichael Ellerman 
508ae0dc736SMichael Ellerman 	/* The simplest case, branch to self, no flags */
50975346251SJordan Niethe 	check(instr_is_branch_bform(ppc_inst(0x40000000)));
510ae0dc736SMichael Ellerman 	/* All bits of target set, and flags */
51175346251SJordan Niethe 	check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
512ae0dc736SMichael Ellerman 	/* High bit of opcode set, which is wrong */
51375346251SJordan Niethe 	check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
514ae0dc736SMichael Ellerman 	/* Middle bits of opcode set, which is wrong */
51575346251SJordan Niethe 	check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
516ae0dc736SMichael Ellerman 
517ae0dc736SMichael Ellerman 	/* Absolute conditional branch to 0x100 */
51875346251SJordan Niethe 	instr = ppc_inst(0x43ff0103);
519ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, 0x100));
520ae0dc736SMichael Ellerman 	/* Absolute conditional branch to 0x20fc */
52175346251SJordan Niethe 	instr = ppc_inst(0x43ff20ff);
522ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, 0x20fc));
523ae0dc736SMichael Ellerman 	/* Maximum positive relative conditional branch, + 32 KB - 4B */
52475346251SJordan Niethe 	instr = ppc_inst(0x43ff7ffc);
525ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
526ae0dc736SMichael Ellerman 	/* Smallest negative relative conditional branch, - 4B */
52775346251SJordan Niethe 	instr = ppc_inst(0x43fffffc);
528ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 4));
529ae0dc736SMichael Ellerman 	/* Largest negative relative conditional branch, - 32 KB */
53075346251SJordan Niethe 	instr = ppc_inst(0x43ff8000);
531ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x8000));
532ae0dc736SMichael Ellerman 
533ae0dc736SMichael Ellerman 	/* All condition code bits set & link */
534ae0dc736SMichael Ellerman 	flags = 0x3ff000 | BRANCH_SET_LINK;
535ae0dc736SMichael Ellerman 
536ae0dc736SMichael Ellerman 	/* Branch to self */
5377c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr, flags);
538ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr));
539ae0dc736SMichael Ellerman 
540ae0dc736SMichael Ellerman 	/* Branch to self - 0x100 */
5417c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
542ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x100));
543ae0dc736SMichael Ellerman 
544ae0dc736SMichael Ellerman 	/* Branch to self + 0x100 */
5457c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
546ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr + 0x100));
547ae0dc736SMichael Ellerman 
548ae0dc736SMichael Ellerman 	/* Maximum relative negative offset, - 32 KB */
5497c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
550ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr - 0x8000));
551ae0dc736SMichael Ellerman 
552ae0dc736SMichael Ellerman 	/* Out of range relative negative offset, - 32 KB + 4*/
5537c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
5547c95d889SJordan Niethe 	check(err);
555ae0dc736SMichael Ellerman 
556ae0dc736SMichael Ellerman 	/* Out of range relative positive offset, + 32 KB */
5577c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
5587c95d889SJordan Niethe 	check(err);
559ae0dc736SMichael Ellerman 
560ae0dc736SMichael Ellerman 	/* Unaligned target */
5617c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr + 3, flags);
5627c95d889SJordan Niethe 	check(err);
563ae0dc736SMichael Ellerman 
564ae0dc736SMichael Ellerman 	/* Check flags are masked correctly */
5657c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
566ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(&instr, addr));
567*217862d9SJordan Niethe 	check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
568ae0dc736SMichael Ellerman }
569ae0dc736SMichael Ellerman 
570ae0dc736SMichael Ellerman static void __init test_translate_branch(void)
571ae0dc736SMichael Ellerman {
572ae0dc736SMichael Ellerman 	unsigned long addr;
573ae0dc736SMichael Ellerman 	unsigned int *p, *q;
5747c95d889SJordan Niethe 	unsigned int instr;
575ae0dc736SMichael Ellerman 	void *buf;
576ae0dc736SMichael Ellerman 
577ae0dc736SMichael Ellerman 	buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
578ae0dc736SMichael Ellerman 	check(buf);
579ae0dc736SMichael Ellerman 	if (!buf)
580ae0dc736SMichael Ellerman 		return;
581ae0dc736SMichael Ellerman 
582ae0dc736SMichael Ellerman 	/* Simple case, branch to self moved a little */
583ae0dc736SMichael Ellerman 	p = buf;
584ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
585ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
586ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
587ae0dc736SMichael Ellerman 	q = p + 1;
5887c95d889SJordan Niethe 	translate_branch(&instr, q, p);
5897c95d889SJordan Niethe 	patch_instruction(q, instr);
590ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
591ae0dc736SMichael Ellerman 
592ae0dc736SMichael Ellerman 	/* Maximum negative case, move b . to addr + 32 MB */
593ae0dc736SMichael Ellerman 	p = buf;
594ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
595ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
596ae0dc736SMichael Ellerman 	q = buf + 0x2000000;
5977c95d889SJordan Niethe 	translate_branch(&instr, q, p);
5987c95d889SJordan Niethe 	patch_instruction(q, instr);
599ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
600ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
601*217862d9SJordan Niethe 	check(ppc_inst_equal(*q, ppc_inst(0x4a000000)));
602ae0dc736SMichael Ellerman 
603ae0dc736SMichael Ellerman 	/* Maximum positive case, move x to x - 32 MB + 4 */
604ae0dc736SMichael Ellerman 	p = buf + 0x2000000;
605ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
606ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
607ae0dc736SMichael Ellerman 	q = buf + 4;
6087c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6097c95d889SJordan Niethe 	patch_instruction(q, instr);
610ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
611ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
612*217862d9SJordan Niethe 	check(ppc_inst_equal(*q, ppc_inst(0x49fffffc)));
613ae0dc736SMichael Ellerman 
614ae0dc736SMichael Ellerman 	/* Jump to x + 16 MB moved to x + 20 MB */
615ae0dc736SMichael Ellerman 	p = buf;
616ae0dc736SMichael Ellerman 	addr = 0x1000000 + (unsigned long)buf;
617ae0dc736SMichael Ellerman 	patch_branch(p, addr, BRANCH_SET_LINK);
618ae0dc736SMichael Ellerman 	q = buf + 0x1400000;
6197c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6207c95d889SJordan Niethe 	patch_instruction(q, instr);
621ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
622ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
623ae0dc736SMichael Ellerman 
624ae0dc736SMichael Ellerman 	/* Jump to x + 16 MB moved to x - 16 MB + 4 */
625ae0dc736SMichael Ellerman 	p = buf + 0x1000000;
626ae0dc736SMichael Ellerman 	addr = 0x2000000 + (unsigned long)buf;
627ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
628ae0dc736SMichael Ellerman 	q = buf + 4;
6297c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6307c95d889SJordan Niethe 	patch_instruction(q, instr);
631ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
632ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
633ae0dc736SMichael Ellerman 
634ae0dc736SMichael Ellerman 
635ae0dc736SMichael Ellerman 	/* Conditional branch tests */
636ae0dc736SMichael Ellerman 
637ae0dc736SMichael Ellerman 	/* Simple case, branch to self moved a little */
638ae0dc736SMichael Ellerman 	p = buf;
639ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
6407c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0);
6417c95d889SJordan Niethe 	patch_instruction(p, instr);
642ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
643ae0dc736SMichael Ellerman 	q = p + 1;
6447c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6457c95d889SJordan Niethe 	patch_instruction(q, instr);
646ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
647ae0dc736SMichael Ellerman 
648ae0dc736SMichael Ellerman 	/* Maximum negative case, move b . to addr + 32 KB */
649ae0dc736SMichael Ellerman 	p = buf;
650ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
6517c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
6527c95d889SJordan Niethe 	patch_instruction(p, instr);
653ae0dc736SMichael Ellerman 	q = buf + 0x8000;
6547c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6557c95d889SJordan Niethe 	patch_instruction(q, instr);
656ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
657ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
658*217862d9SJordan Niethe 	check(ppc_inst_equal(*q, ppc_inst(0x43ff8000)));
659ae0dc736SMichael Ellerman 
660ae0dc736SMichael Ellerman 	/* Maximum positive case, move x to x - 32 KB + 4 */
661ae0dc736SMichael Ellerman 	p = buf + 0x8000;
662ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
6637c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
6647c95d889SJordan Niethe 	patch_instruction(p, instr);
665ae0dc736SMichael Ellerman 	q = buf + 4;
6667c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6677c95d889SJordan Niethe 	patch_instruction(q, instr);
668ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
669ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
670*217862d9SJordan Niethe 	check(ppc_inst_equal(*q, ppc_inst(0x43ff7ffc)));
671ae0dc736SMichael Ellerman 
672ae0dc736SMichael Ellerman 	/* Jump to x + 12 KB moved to x + 20 KB */
673ae0dc736SMichael Ellerman 	p = buf;
674ae0dc736SMichael Ellerman 	addr = 0x3000 + (unsigned long)buf;
6757c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
6767c95d889SJordan Niethe 	patch_instruction(p, instr);
677ae0dc736SMichael Ellerman 	q = buf + 0x5000;
6787c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6797c95d889SJordan Niethe 	patch_instruction(q, instr);
680ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
681ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
682ae0dc736SMichael Ellerman 
683ae0dc736SMichael Ellerman 	/* Jump to x + 8 KB moved to x - 8 KB + 4 */
684ae0dc736SMichael Ellerman 	p = buf + 0x2000;
685ae0dc736SMichael Ellerman 	addr = 0x4000 + (unsigned long)buf;
6867c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0);
6877c95d889SJordan Niethe 	patch_instruction(p, instr);
688ae0dc736SMichael Ellerman 	q = buf + 4;
6897c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6907c95d889SJordan Niethe 	patch_instruction(q, instr);
691ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
692ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
693ae0dc736SMichael Ellerman 
694ae0dc736SMichael Ellerman 	/* Free the buffer we were using */
695ae0dc736SMichael Ellerman 	vfree(buf);
696ae0dc736SMichael Ellerman }
697ae0dc736SMichael Ellerman 
698ae0dc736SMichael Ellerman static int __init test_code_patching(void)
699ae0dc736SMichael Ellerman {
700ae0dc736SMichael Ellerman 	printk(KERN_DEBUG "Running code patching self-tests ...\n");
701ae0dc736SMichael Ellerman 
702ae0dc736SMichael Ellerman 	test_branch_iform();
703ae0dc736SMichael Ellerman 	test_branch_bform();
704ae0dc736SMichael Ellerman 	test_create_function_call();
705ae0dc736SMichael Ellerman 	test_translate_branch();
706ae0dc736SMichael Ellerman 
707ae0dc736SMichael Ellerman 	return 0;
708ae0dc736SMichael Ellerman }
709ae0dc736SMichael Ellerman late_initcall(test_code_patching);
710ae0dc736SMichael Ellerman 
711ae0dc736SMichael Ellerman #endif /* CONFIG_CODE_PATCHING_SELFTEST */
712