xref: /linux/arch/powerpc/lib/code-patching.c (revision edecd2d6d6f4a122dd62bce654b4f63301e8ad9a)
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/tlbflush.h>
1637bc3e5fSBalbir Singh #include <asm/page.h>
1737bc3e5fSBalbir Singh #include <asm/code-patching.h>
18252eb558SChristophe Leroy #include <asm/setup.h>
1975346251SJordan Niethe #include <asm/inst.h>
20aaddd3eaSMichael Ellerman 
21c545b9f0SChristophe Leroy static int __patch_instruction(u32 *exec_addr, ppc_inst_t instr, u32 *patch_addr)
22aaddd3eaSMichael Ellerman {
23e63ceebdSChristophe Leroy 	if (!ppc_inst_prefixed(instr)) {
24e63ceebdSChristophe Leroy 		u32 val = ppc_inst_val(instr);
25e63ceebdSChristophe Leroy 
26e63ceebdSChristophe Leroy 		__put_kernel_nofault(patch_addr, &val, u32, failed);
27e63ceebdSChristophe Leroy 	} else {
28693557ebSChristophe Leroy 		u64 val = ppc_inst_as_ulong(instr);
29e63ceebdSChristophe Leroy 
30e63ceebdSChristophe Leroy 		__put_kernel_nofault(patch_addr, &val, u64, failed);
31e63ceebdSChristophe Leroy 	}
3237bc3e5fSBalbir Singh 
338cf4c057SChristophe Leroy 	asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
348cf4c057SChristophe Leroy 							    "r" (exec_addr));
3537bc3e5fSBalbir Singh 
36b6e37968SSteven Rostedt 	return 0;
37e64ac41aSChristophe Leroy 
38e64ac41aSChristophe Leroy failed:
39e64ac41aSChristophe Leroy 	return -EFAULT;
40aaddd3eaSMichael Ellerman }
41aaddd3eaSMichael Ellerman 
42c545b9f0SChristophe Leroy int raw_patch_instruction(u32 *addr, ppc_inst_t instr)
438cf4c057SChristophe Leroy {
448cf4c057SChristophe Leroy 	return __patch_instruction(addr, instr, addr);
458cf4c057SChristophe Leroy }
468cf4c057SChristophe Leroy 
4737bc3e5fSBalbir Singh #ifdef CONFIG_STRICT_KERNEL_RWX
4837bc3e5fSBalbir Singh static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
4937bc3e5fSBalbir Singh 
5037bc3e5fSBalbir Singh static int text_area_cpu_up(unsigned int cpu)
5137bc3e5fSBalbir Singh {
5237bc3e5fSBalbir Singh 	struct vm_struct *area;
5337bc3e5fSBalbir Singh 
5437bc3e5fSBalbir Singh 	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
5537bc3e5fSBalbir Singh 	if (!area) {
5637bc3e5fSBalbir Singh 		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
5737bc3e5fSBalbir Singh 			cpu);
5837bc3e5fSBalbir Singh 		return -1;
5937bc3e5fSBalbir Singh 	}
6037bc3e5fSBalbir Singh 	this_cpu_write(text_poke_area, area);
6137bc3e5fSBalbir Singh 
6237bc3e5fSBalbir Singh 	return 0;
6337bc3e5fSBalbir Singh }
6437bc3e5fSBalbir Singh 
6537bc3e5fSBalbir Singh static int text_area_cpu_down(unsigned int cpu)
6637bc3e5fSBalbir Singh {
6737bc3e5fSBalbir Singh 	free_vm_area(this_cpu_read(text_poke_area));
6837bc3e5fSBalbir Singh 	return 0;
6937bc3e5fSBalbir Singh }
7037bc3e5fSBalbir Singh 
7137bc3e5fSBalbir Singh /*
7271a5b3dbSJordan Niethe  * Although BUG_ON() is rude, in this case it should only happen if ENOMEM, and
7371a5b3dbSJordan Niethe  * we judge it as being preferable to a kernel that will crash later when
7471a5b3dbSJordan Niethe  * someone tries to use patch_instruction().
7537bc3e5fSBalbir Singh  */
7671a5b3dbSJordan Niethe void __init poking_init(void)
7737bc3e5fSBalbir Singh {
7837bc3e5fSBalbir Singh 	BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
7937bc3e5fSBalbir Singh 		"powerpc/text_poke:online", text_area_cpu_up,
8037bc3e5fSBalbir Singh 		text_area_cpu_down));
8137bc3e5fSBalbir Singh }
8237bc3e5fSBalbir Singh 
8337bc3e5fSBalbir Singh /*
8437bc3e5fSBalbir Singh  * This can be called for kernel text or a module.
8537bc3e5fSBalbir Singh  */
8637bc3e5fSBalbir Singh static int map_patch_area(void *addr, unsigned long text_poke_addr)
8737bc3e5fSBalbir Singh {
8837bc3e5fSBalbir Singh 	unsigned long pfn;
8937bc3e5fSBalbir Singh 	int err;
9037bc3e5fSBalbir Singh 
91ccc8fcf7SChristophe Leroy 	if (is_vmalloc_or_module_addr(addr))
9237bc3e5fSBalbir Singh 		pfn = vmalloc_to_pfn(addr);
9337bc3e5fSBalbir Singh 	else
9437bc3e5fSBalbir Singh 		pfn = __pa_symbol(addr) >> PAGE_SHIFT;
9537bc3e5fSBalbir Singh 
96c766ee72SChristophe Leroy 	err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
9737bc3e5fSBalbir Singh 
9837bc3e5fSBalbir Singh 	if (err)
9937bc3e5fSBalbir Singh 		return -1;
10037bc3e5fSBalbir Singh 
10137bc3e5fSBalbir Singh 	return 0;
10237bc3e5fSBalbir Singh }
10337bc3e5fSBalbir Singh 
10437bc3e5fSBalbir Singh static inline int unmap_patch_area(unsigned long addr)
10537bc3e5fSBalbir Singh {
10637bc3e5fSBalbir Singh 	pte_t *ptep;
10737bc3e5fSBalbir Singh 	pmd_t *pmdp;
10837bc3e5fSBalbir Singh 	pud_t *pudp;
1092fb47060SMike Rapoport 	p4d_t *p4dp;
11037bc3e5fSBalbir Singh 	pgd_t *pgdp;
11137bc3e5fSBalbir Singh 
11237bc3e5fSBalbir Singh 	pgdp = pgd_offset_k(addr);
11337bc3e5fSBalbir Singh 	if (unlikely(!pgdp))
11437bc3e5fSBalbir Singh 		return -EINVAL;
11537bc3e5fSBalbir Singh 
1162fb47060SMike Rapoport 	p4dp = p4d_offset(pgdp, addr);
1172fb47060SMike Rapoport 	if (unlikely(!p4dp))
1182fb47060SMike Rapoport 		return -EINVAL;
1192fb47060SMike Rapoport 
1202fb47060SMike Rapoport 	pudp = pud_offset(p4dp, addr);
12137bc3e5fSBalbir Singh 	if (unlikely(!pudp))
12237bc3e5fSBalbir Singh 		return -EINVAL;
12337bc3e5fSBalbir Singh 
12437bc3e5fSBalbir Singh 	pmdp = pmd_offset(pudp, addr);
12537bc3e5fSBalbir Singh 	if (unlikely(!pmdp))
12637bc3e5fSBalbir Singh 		return -EINVAL;
12737bc3e5fSBalbir Singh 
12837bc3e5fSBalbir Singh 	ptep = pte_offset_kernel(pmdp, addr);
12937bc3e5fSBalbir Singh 	if (unlikely(!ptep))
13037bc3e5fSBalbir Singh 		return -EINVAL;
13137bc3e5fSBalbir Singh 
13237bc3e5fSBalbir Singh 	/*
13337bc3e5fSBalbir Singh 	 * In hash, pte_clear flushes the tlb, in radix, we have to
13437bc3e5fSBalbir Singh 	 */
13537bc3e5fSBalbir Singh 	pte_clear(&init_mm, addr, ptep);
13637bc3e5fSBalbir Singh 	flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
13737bc3e5fSBalbir Singh 
13837bc3e5fSBalbir Singh 	return 0;
13937bc3e5fSBalbir Singh }
14037bc3e5fSBalbir Singh 
141c545b9f0SChristophe Leroy static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
14237bc3e5fSBalbir Singh {
14337bc3e5fSBalbir Singh 	int err;
14469d4d6e5SChristophe Leroy 	u32 *patch_addr = NULL;
14537bc3e5fSBalbir Singh 	unsigned long flags;
14637bc3e5fSBalbir Singh 	unsigned long text_poke_addr;
14737bc3e5fSBalbir Singh 	unsigned long kaddr = (unsigned long)addr;
14837bc3e5fSBalbir Singh 
14937bc3e5fSBalbir Singh 	/*
15037bc3e5fSBalbir Singh 	 * During early early boot patch_instruction is called
15137bc3e5fSBalbir Singh 	 * when text_poke_area is not ready, but we still need
15237bc3e5fSBalbir Singh 	 * to allow patching. We just do the plain old patching
15337bc3e5fSBalbir Singh 	 */
1548183d99fSChristophe Leroy 	if (!this_cpu_read(text_poke_area))
1558cf4c057SChristophe Leroy 		return raw_patch_instruction(addr, instr);
15637bc3e5fSBalbir Singh 
15737bc3e5fSBalbir Singh 	local_irq_save(flags);
15837bc3e5fSBalbir Singh 
15937bc3e5fSBalbir Singh 	text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
16037bc3e5fSBalbir Singh 	if (map_patch_area(addr, text_poke_addr)) {
16137bc3e5fSBalbir Singh 		err = -1;
16237bc3e5fSBalbir Singh 		goto out;
16337bc3e5fSBalbir Singh 	}
16437bc3e5fSBalbir Singh 
16569d4d6e5SChristophe Leroy 	patch_addr = (u32 *)(text_poke_addr + (kaddr & ~PAGE_MASK));
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 
180c545b9f0SChristophe Leroy static int do_patch_instruction(u32 *addr, ppc_inst_t instr)
18137bc3e5fSBalbir Singh {
1828cf4c057SChristophe Leroy 	return raw_patch_instruction(addr, instr);
18337bc3e5fSBalbir Singh }
18437bc3e5fSBalbir Singh 
18537bc3e5fSBalbir Singh #endif /* CONFIG_STRICT_KERNEL_RWX */
186b45ba4a5SChristophe Leroy 
187c545b9f0SChristophe Leroy int patch_instruction(u32 *addr, ppc_inst_t instr)
188b45ba4a5SChristophe Leroy {
189b45ba4a5SChristophe Leroy 	/* Make sure we aren't patching a freed init section */
190*edecd2d6SChristophe Leroy 	if (init_mem_is_free && init_section_contains(addr, 4))
191b45ba4a5SChristophe Leroy 		return 0;
192*edecd2d6SChristophe Leroy 
193b45ba4a5SChristophe Leroy 	return do_patch_instruction(addr, instr);
194b45ba4a5SChristophe Leroy }
19537bc3e5fSBalbir Singh NOKPROBE_SYMBOL(patch_instruction);
19637bc3e5fSBalbir Singh 
19769d4d6e5SChristophe Leroy int patch_branch(u32 *addr, unsigned long target, int flags)
198e7a57273SMichael Ellerman {
199c545b9f0SChristophe Leroy 	ppc_inst_t instr;
2007c95d889SJordan Niethe 
2017c95d889SJordan Niethe 	create_branch(&instr, addr, target, flags);
2027c95d889SJordan Niethe 	return patch_instruction(addr, instr);
203e7a57273SMichael Ellerman }
204e7a57273SMichael Ellerman 
205ebfa50dfSAnju T bool is_offset_in_branch_range(long offset)
206ebfa50dfSAnju T {
207ebfa50dfSAnju T 	/*
208ebfa50dfSAnju T 	 * Powerpc branch instruction is :
209ebfa50dfSAnju T 	 *
210ebfa50dfSAnju T 	 *  0         6                 30   31
211ebfa50dfSAnju T 	 *  +---------+----------------+---+---+
212ebfa50dfSAnju T 	 *  | opcode  |     LI         |AA |LK |
213ebfa50dfSAnju T 	 *  +---------+----------------+---+---+
214ebfa50dfSAnju T 	 *  Where AA = 0 and LK = 0
215ebfa50dfSAnju T 	 *
216ebfa50dfSAnju T 	 * LI is a signed 24 bits integer. The real branch offset is computed
217ebfa50dfSAnju T 	 * by: imm32 = SignExtend(LI:'0b00', 32);
218ebfa50dfSAnju T 	 *
219ebfa50dfSAnju T 	 * So the maximum forward branch should be:
220ebfa50dfSAnju T 	 *   (0x007fffff << 2) = 0x01fffffc =  0x1fffffc
221ebfa50dfSAnju T 	 * The maximum backward branch should be:
222ebfa50dfSAnju T 	 *   (0xff800000 << 2) = 0xfe000000 = -0x2000000
223ebfa50dfSAnju T 	 */
224ebfa50dfSAnju T 	return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
225ebfa50dfSAnju T }
226ebfa50dfSAnju T 
2274549c3eaSNaveen N. Rao bool is_offset_in_cond_branch_range(long offset)
2284549c3eaSNaveen N. Rao {
2294549c3eaSNaveen N. Rao 	return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
2304549c3eaSNaveen N. Rao }
2314549c3eaSNaveen N. Rao 
23251c9c084SAnju T /*
23351c9c084SAnju T  * Helper to check if a given instruction is a conditional branch
23451c9c084SAnju T  * Derived from the conditional checks in analyse_instr()
23551c9c084SAnju T  */
236c545b9f0SChristophe Leroy bool is_conditional_branch(ppc_inst_t instr)
23751c9c084SAnju T {
2388094892dSJordan Niethe 	unsigned int opcode = ppc_inst_primary_opcode(instr);
23951c9c084SAnju T 
24051c9c084SAnju T 	if (opcode == 16)       /* bc, bca, bcl, bcla */
24151c9c084SAnju T 		return true;
24251c9c084SAnju T 	if (opcode == 19) {
243777e26f0SJordan Niethe 		switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
24451c9c084SAnju T 		case 16:        /* bclr, bclrl */
24551c9c084SAnju T 		case 528:       /* bcctr, bcctrl */
24651c9c084SAnju T 		case 560:       /* bctar, bctarl */
24751c9c084SAnju T 			return true;
24851c9c084SAnju T 		}
24951c9c084SAnju T 	}
25051c9c084SAnju T 	return false;
25151c9c084SAnju T }
25271f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(is_conditional_branch);
25351c9c084SAnju T 
254c545b9f0SChristophe Leroy int create_branch(ppc_inst_t *instr, const u32 *addr,
255e7a57273SMichael Ellerman 		  unsigned long target, int flags)
256aaddd3eaSMichael Ellerman {
257053a858eSMichael Ellerman 	long offset;
258aaddd3eaSMichael Ellerman 
25994afd069SJordan Niethe 	*instr = ppc_inst(0);
260053a858eSMichael Ellerman 	offset = target;
261aaddd3eaSMichael Ellerman 	if (! (flags & BRANCH_ABSOLUTE))
262053a858eSMichael Ellerman 		offset = offset - (unsigned long)addr;
263053a858eSMichael Ellerman 
264053a858eSMichael Ellerman 	/* Check we can represent the target in the instruction format */
265ebfa50dfSAnju T 	if (!is_offset_in_branch_range(offset))
2667c95d889SJordan Niethe 		return 1;
267aaddd3eaSMichael Ellerman 
268aaddd3eaSMichael Ellerman 	/* Mask out the flags and target, so they don't step on each other. */
26994afd069SJordan Niethe 	*instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
270aaddd3eaSMichael Ellerman 
2717c95d889SJordan Niethe 	return 0;
272aaddd3eaSMichael Ellerman }
273411781a2SMichael Ellerman 
274c545b9f0SChristophe Leroy int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
275411781a2SMichael Ellerman 		       unsigned long target, int flags)
276411781a2SMichael Ellerman {
277411781a2SMichael Ellerman 	long offset;
278411781a2SMichael Ellerman 
279411781a2SMichael Ellerman 	offset = target;
280411781a2SMichael Ellerman 	if (! (flags & BRANCH_ABSOLUTE))
281411781a2SMichael Ellerman 		offset = offset - (unsigned long)addr;
282411781a2SMichael Ellerman 
283411781a2SMichael Ellerman 	/* Check we can represent the target in the instruction format */
2844549c3eaSNaveen N. Rao 	if (!is_offset_in_cond_branch_range(offset))
2857c95d889SJordan Niethe 		return 1;
286411781a2SMichael Ellerman 
287411781a2SMichael Ellerman 	/* Mask out the flags and target, so they don't step on each other. */
28894afd069SJordan Niethe 	*instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
289411781a2SMichael Ellerman 
2907c95d889SJordan Niethe 	return 0;
291411781a2SMichael Ellerman }
292411781a2SMichael Ellerman 
293c545b9f0SChristophe Leroy static unsigned int branch_opcode(ppc_inst_t instr)
294411781a2SMichael Ellerman {
2958094892dSJordan Niethe 	return ppc_inst_primary_opcode(instr) & 0x3F;
296411781a2SMichael Ellerman }
297411781a2SMichael Ellerman 
298c545b9f0SChristophe Leroy static int instr_is_branch_iform(ppc_inst_t instr)
299411781a2SMichael Ellerman {
300411781a2SMichael Ellerman 	return branch_opcode(instr) == 18;
301411781a2SMichael Ellerman }
302411781a2SMichael Ellerman 
303c545b9f0SChristophe Leroy static int instr_is_branch_bform(ppc_inst_t instr)
304411781a2SMichael Ellerman {
305411781a2SMichael Ellerman 	return branch_opcode(instr) == 16;
306411781a2SMichael Ellerman }
307411781a2SMichael Ellerman 
308c545b9f0SChristophe Leroy int instr_is_relative_branch(ppc_inst_t instr)
309411781a2SMichael Ellerman {
310777e26f0SJordan Niethe 	if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
311411781a2SMichael Ellerman 		return 0;
312411781a2SMichael Ellerman 
313411781a2SMichael Ellerman 	return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
314411781a2SMichael Ellerman }
315411781a2SMichael Ellerman 
316c545b9f0SChristophe Leroy int instr_is_relative_link_branch(ppc_inst_t instr)
317b9eab08dSJosh Poimboeuf {
318777e26f0SJordan Niethe 	return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
319b9eab08dSJosh Poimboeuf }
320b9eab08dSJosh Poimboeuf 
32169d4d6e5SChristophe Leroy static unsigned long branch_iform_target(const u32 *instr)
322411781a2SMichael Ellerman {
323411781a2SMichael Ellerman 	signed long imm;
324411781a2SMichael Ellerman 
32518c85964SChristophe Leroy 	imm = ppc_inst_val(ppc_inst_read(instr)) & 0x3FFFFFC;
326411781a2SMichael Ellerman 
327411781a2SMichael Ellerman 	/* If the top bit of the immediate value is set this is negative */
328411781a2SMichael Ellerman 	if (imm & 0x2000000)
329411781a2SMichael Ellerman 		imm -= 0x4000000;
330411781a2SMichael Ellerman 
33118c85964SChristophe Leroy 	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
332411781a2SMichael Ellerman 		imm += (unsigned long)instr;
333411781a2SMichael Ellerman 
334411781a2SMichael Ellerman 	return (unsigned long)imm;
335411781a2SMichael Ellerman }
336411781a2SMichael Ellerman 
33769d4d6e5SChristophe Leroy static unsigned long branch_bform_target(const u32 *instr)
338411781a2SMichael Ellerman {
339411781a2SMichael Ellerman 	signed long imm;
340411781a2SMichael Ellerman 
34118c85964SChristophe Leroy 	imm = ppc_inst_val(ppc_inst_read(instr)) & 0xFFFC;
342411781a2SMichael Ellerman 
343411781a2SMichael Ellerman 	/* If the top bit of the immediate value is set this is negative */
344411781a2SMichael Ellerman 	if (imm & 0x8000)
345411781a2SMichael Ellerman 		imm -= 0x10000;
346411781a2SMichael Ellerman 
34718c85964SChristophe Leroy 	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
348411781a2SMichael Ellerman 		imm += (unsigned long)instr;
349411781a2SMichael Ellerman 
350411781a2SMichael Ellerman 	return (unsigned long)imm;
351411781a2SMichael Ellerman }
352411781a2SMichael Ellerman 
35369d4d6e5SChristophe Leroy unsigned long branch_target(const u32 *instr)
354411781a2SMichael Ellerman {
355f8faaffaSJordan Niethe 	if (instr_is_branch_iform(ppc_inst_read(instr)))
356411781a2SMichael Ellerman 		return branch_iform_target(instr);
357f8faaffaSJordan Niethe 	else if (instr_is_branch_bform(ppc_inst_read(instr)))
358411781a2SMichael Ellerman 		return branch_bform_target(instr);
359411781a2SMichael Ellerman 
360411781a2SMichael Ellerman 	return 0;
361411781a2SMichael Ellerman }
362411781a2SMichael Ellerman 
363c545b9f0SChristophe Leroy int translate_branch(ppc_inst_t *instr, const u32 *dest, const u32 *src)
364411781a2SMichael Ellerman {
365411781a2SMichael Ellerman 	unsigned long target;
366411781a2SMichael Ellerman 	target = branch_target(src);
367411781a2SMichael Ellerman 
368f8faaffaSJordan Niethe 	if (instr_is_branch_iform(ppc_inst_read(src)))
369f8faaffaSJordan Niethe 		return create_branch(instr, dest, target,
370f8faaffaSJordan Niethe 				     ppc_inst_val(ppc_inst_read(src)));
371f8faaffaSJordan Niethe 	else if (instr_is_branch_bform(ppc_inst_read(src)))
372f8faaffaSJordan Niethe 		return create_cond_branch(instr, dest, target,
373f8faaffaSJordan Niethe 					  ppc_inst_val(ppc_inst_read(src)));
374411781a2SMichael Ellerman 
3757c95d889SJordan Niethe 	return 1;
376411781a2SMichael Ellerman }
377ae0dc736SMichael Ellerman 
3781e8341aeSKevin Hao #ifdef CONFIG_PPC_BOOK3E_64
3791e8341aeSKevin Hao void __patch_exception(int exc, unsigned long addr)
3801e8341aeSKevin Hao {
3811e8341aeSKevin Hao 	extern unsigned int interrupt_base_book3e;
3821e8341aeSKevin Hao 	unsigned int *ibase = &interrupt_base_book3e;
3831e8341aeSKevin Hao 
3841e8341aeSKevin Hao 	/* Our exceptions vectors start with a NOP and -then- a branch
3851e8341aeSKevin Hao 	 * to deal with single stepping from userspace which stops on
3861e8341aeSKevin Hao 	 * the second instruction. Thus we need to patch the second
3871e8341aeSKevin Hao 	 * instruction of the exception, not the first one
3881e8341aeSKevin Hao 	 */
3891e8341aeSKevin Hao 
39069d4d6e5SChristophe Leroy 	patch_branch(ibase + (exc / 4) + 1, addr, 0);
3911e8341aeSKevin Hao }
3921e8341aeSKevin Hao #endif
393ae0dc736SMichael Ellerman 
394ae0dc736SMichael Ellerman #ifdef CONFIG_CODE_PATCHING_SELFTEST
395ae0dc736SMichael Ellerman 
396ce0c6be9SNick Child static int __init instr_is_branch_to_addr(const u32 *instr, unsigned long addr)
3976c0d181dSChristophe Leroy {
3986c0d181dSChristophe Leroy 	if (instr_is_branch_iform(ppc_inst_read(instr)) ||
3996c0d181dSChristophe Leroy 	    instr_is_branch_bform(ppc_inst_read(instr)))
4006c0d181dSChristophe Leroy 		return branch_target(instr) == addr;
4016c0d181dSChristophe Leroy 
4026c0d181dSChristophe Leroy 	return 0;
4036c0d181dSChristophe Leroy }
4046c0d181dSChristophe Leroy 
405ae0dc736SMichael Ellerman static void __init test_trampoline(void)
406ae0dc736SMichael Ellerman {
407ae0dc736SMichael Ellerman 	asm ("nop;\n");
408ae0dc736SMichael Ellerman }
409ae0dc736SMichael Ellerman 
410*edecd2d6SChristophe Leroy #define check(x)	do {	\
411*edecd2d6SChristophe Leroy 	if (!(x))		\
412*edecd2d6SChristophe Leroy 		pr_err("code-patching: test failed at line %d\n", __LINE__); \
413*edecd2d6SChristophe Leroy } while (0)
414ae0dc736SMichael Ellerman 
415ae0dc736SMichael Ellerman static void __init test_branch_iform(void)
416ae0dc736SMichael Ellerman {
4177c95d889SJordan Niethe 	int err;
418c545b9f0SChristophe Leroy 	ppc_inst_t instr;
419e90a21eaSChristophe Leroy 	u32 tmp[2];
42069d4d6e5SChristophe Leroy 	u32 *iptr = tmp;
421e90a21eaSChristophe Leroy 	unsigned long addr = (unsigned long)tmp;
422ae0dc736SMichael Ellerman 
423ae0dc736SMichael Ellerman 	/* The simplest case, branch to self, no flags */
42475346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x48000000)));
425ae0dc736SMichael Ellerman 	/* All bits of target set, and flags */
42675346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
427ae0dc736SMichael Ellerman 	/* High bit of opcode set, which is wrong */
42875346251SJordan Niethe 	check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
429ae0dc736SMichael Ellerman 	/* Middle bits of opcode set, which is wrong */
43075346251SJordan Niethe 	check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
431ae0dc736SMichael Ellerman 
432ae0dc736SMichael Ellerman 	/* Simplest case, branch to self with link */
43375346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x48000001)));
434ae0dc736SMichael Ellerman 	/* All bits of targets set */
43575346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
436ae0dc736SMichael Ellerman 	/* Some bits of targets set */
43775346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
438ae0dc736SMichael Ellerman 	/* Must be a valid branch to start with */
43975346251SJordan Niethe 	check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
440ae0dc736SMichael Ellerman 
441ae0dc736SMichael Ellerman 	/* Absolute branch to 0x100 */
442e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x48000103));
443e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, 0x100));
444ae0dc736SMichael Ellerman 	/* Absolute branch to 0x420fc */
445e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x480420ff));
446e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, 0x420fc));
447ae0dc736SMichael Ellerman 	/* Maximum positive relative branch, + 20MB - 4B */
448e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x49fffffc));
449e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr + 0x1FFFFFC));
450ae0dc736SMichael Ellerman 	/* Smallest negative relative branch, - 4B */
451e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x4bfffffc));
452e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 4));
453ae0dc736SMichael Ellerman 	/* Largest negative relative branch, - 32 MB */
454e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x4a000000));
455e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
456ae0dc736SMichael Ellerman 
457ae0dc736SMichael Ellerman 	/* Branch to self, with link */
458e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr, BRANCH_SET_LINK);
459e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
460e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr));
461ae0dc736SMichael Ellerman 
462ae0dc736SMichael Ellerman 	/* Branch to self - 0x100, with link */
463e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr - 0x100, BRANCH_SET_LINK);
464e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
465e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x100));
466ae0dc736SMichael Ellerman 
467ae0dc736SMichael Ellerman 	/* Branch to self + 0x100, no link */
468e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr + 0x100, 0);
469e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
470e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr + 0x100));
471ae0dc736SMichael Ellerman 
472ae0dc736SMichael Ellerman 	/* Maximum relative negative offset, - 32 MB */
473e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr - 0x2000000, BRANCH_SET_LINK);
474e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
475e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
476ae0dc736SMichael Ellerman 
477ae0dc736SMichael Ellerman 	/* Out of range relative negative offset, - 32 MB + 4*/
478e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr - 0x2000004, BRANCH_SET_LINK);
4797c95d889SJordan Niethe 	check(err);
480ae0dc736SMichael Ellerman 
481ae0dc736SMichael Ellerman 	/* Out of range relative positive offset, + 32 MB */
482e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr + 0x2000000, BRANCH_SET_LINK);
4837c95d889SJordan Niethe 	check(err);
484ae0dc736SMichael Ellerman 
485ae0dc736SMichael Ellerman 	/* Unaligned target */
486e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr + 3, BRANCH_SET_LINK);
4877c95d889SJordan Niethe 	check(err);
488ae0dc736SMichael Ellerman 
489ae0dc736SMichael Ellerman 	/* Check flags are masked correctly */
490e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr, 0xFFFFFFFC);
491e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
492e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr));
493217862d9SJordan Niethe 	check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
494ae0dc736SMichael Ellerman }
495ae0dc736SMichael Ellerman 
496ae0dc736SMichael Ellerman static void __init test_create_function_call(void)
497ae0dc736SMichael Ellerman {
49869d4d6e5SChristophe Leroy 	u32 *iptr;
499ae0dc736SMichael Ellerman 	unsigned long dest;
500c545b9f0SChristophe Leroy 	ppc_inst_t instr;
501ae0dc736SMichael Ellerman 
502ae0dc736SMichael Ellerman 	/* Check we can create a function call */
50369d4d6e5SChristophe Leroy 	iptr = (u32 *)ppc_function_entry(test_trampoline);
504ae0dc736SMichael Ellerman 	dest = ppc_function_entry(test_create_function_call);
5057c95d889SJordan Niethe 	create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
5067c95d889SJordan Niethe 	patch_instruction(iptr, instr);
507ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(iptr, dest));
508ae0dc736SMichael Ellerman }
509ae0dc736SMichael Ellerman 
510ae0dc736SMichael Ellerman static void __init test_branch_bform(void)
511ae0dc736SMichael Ellerman {
5127c95d889SJordan Niethe 	int err;
513ae0dc736SMichael Ellerman 	unsigned long addr;
514c545b9f0SChristophe Leroy 	ppc_inst_t instr;
515e90a21eaSChristophe Leroy 	u32 tmp[2];
51669d4d6e5SChristophe Leroy 	u32 *iptr = tmp;
51794afd069SJordan Niethe 	unsigned int flags;
518ae0dc736SMichael Ellerman 
519ae0dc736SMichael Ellerman 	addr = (unsigned long)iptr;
520ae0dc736SMichael Ellerman 
521ae0dc736SMichael Ellerman 	/* The simplest case, branch to self, no flags */
52275346251SJordan Niethe 	check(instr_is_branch_bform(ppc_inst(0x40000000)));
523ae0dc736SMichael Ellerman 	/* All bits of target set, and flags */
52475346251SJordan Niethe 	check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
525ae0dc736SMichael Ellerman 	/* High bit of opcode set, which is wrong */
52675346251SJordan Niethe 	check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
527ae0dc736SMichael Ellerman 	/* Middle bits of opcode set, which is wrong */
52875346251SJordan Niethe 	check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
529ae0dc736SMichael Ellerman 
530ae0dc736SMichael Ellerman 	/* Absolute conditional branch to 0x100 */
531e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43ff0103));
532e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, 0x100));
533ae0dc736SMichael Ellerman 	/* Absolute conditional branch to 0x20fc */
534e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43ff20ff));
535e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, 0x20fc));
536ae0dc736SMichael Ellerman 	/* Maximum positive relative conditional branch, + 32 KB - 4B */
537e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43ff7ffc));
538e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr + 0x7FFC));
539ae0dc736SMichael Ellerman 	/* Smallest negative relative conditional branch, - 4B */
540e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43fffffc));
541e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 4));
542ae0dc736SMichael Ellerman 	/* Largest negative relative conditional branch, - 32 KB */
543e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43ff8000));
544e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x8000));
545ae0dc736SMichael Ellerman 
546ae0dc736SMichael Ellerman 	/* All condition code bits set & link */
547ae0dc736SMichael Ellerman 	flags = 0x3ff000 | BRANCH_SET_LINK;
548ae0dc736SMichael Ellerman 
549ae0dc736SMichael Ellerman 	/* Branch to self */
5507c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr, flags);
551e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
552e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr));
553ae0dc736SMichael Ellerman 
554ae0dc736SMichael Ellerman 	/* Branch to self - 0x100 */
5557c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
556e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
557e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x100));
558ae0dc736SMichael Ellerman 
559ae0dc736SMichael Ellerman 	/* Branch to self + 0x100 */
5607c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
561e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
562e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr + 0x100));
563ae0dc736SMichael Ellerman 
564ae0dc736SMichael Ellerman 	/* Maximum relative negative offset, - 32 KB */
5657c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
566e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
567e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x8000));
568ae0dc736SMichael Ellerman 
569ae0dc736SMichael Ellerman 	/* Out of range relative negative offset, - 32 KB + 4*/
5707c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
5717c95d889SJordan Niethe 	check(err);
572ae0dc736SMichael Ellerman 
573ae0dc736SMichael Ellerman 	/* Out of range relative positive offset, + 32 KB */
5747c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
5757c95d889SJordan Niethe 	check(err);
576ae0dc736SMichael Ellerman 
577ae0dc736SMichael Ellerman 	/* Unaligned target */
5787c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr + 3, flags);
5797c95d889SJordan Niethe 	check(err);
580ae0dc736SMichael Ellerman 
581ae0dc736SMichael Ellerman 	/* Check flags are masked correctly */
5827c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
583e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
584e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr));
585217862d9SJordan Niethe 	check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
586ae0dc736SMichael Ellerman }
587ae0dc736SMichael Ellerman 
588ae0dc736SMichael Ellerman static void __init test_translate_branch(void)
589ae0dc736SMichael Ellerman {
590ae0dc736SMichael Ellerman 	unsigned long addr;
5910b582db5SJordan Niethe 	void *p, *q;
592c545b9f0SChristophe Leroy 	ppc_inst_t instr;
593ae0dc736SMichael Ellerman 	void *buf;
594ae0dc736SMichael Ellerman 
595ae0dc736SMichael Ellerman 	buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
596ae0dc736SMichael Ellerman 	check(buf);
597ae0dc736SMichael Ellerman 	if (!buf)
598ae0dc736SMichael Ellerman 		return;
599ae0dc736SMichael Ellerman 
600ae0dc736SMichael Ellerman 	/* Simple case, branch to self moved a little */
601ae0dc736SMichael Ellerman 	p = buf;
602ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
603ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
604ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
6050b582db5SJordan Niethe 	q = p + 4;
6067c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6077c95d889SJordan Niethe 	patch_instruction(q, instr);
608ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
609ae0dc736SMichael Ellerman 
610ae0dc736SMichael Ellerman 	/* Maximum negative case, move b . to addr + 32 MB */
611ae0dc736SMichael Ellerman 	p = buf;
612ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
613ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
614ae0dc736SMichael Ellerman 	q = buf + 0x2000000;
6157c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6167c95d889SJordan Niethe 	patch_instruction(q, instr);
617ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
618ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
619f8faaffaSJordan Niethe 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
620ae0dc736SMichael Ellerman 
621ae0dc736SMichael Ellerman 	/* Maximum positive case, move x to x - 32 MB + 4 */
622ae0dc736SMichael Ellerman 	p = buf + 0x2000000;
623ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
624ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
625ae0dc736SMichael Ellerman 	q = buf + 4;
6267c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6277c95d889SJordan Niethe 	patch_instruction(q, instr);
628ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
629ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
630f8faaffaSJordan Niethe 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
631ae0dc736SMichael Ellerman 
632ae0dc736SMichael Ellerman 	/* Jump to x + 16 MB moved to x + 20 MB */
633ae0dc736SMichael Ellerman 	p = buf;
634ae0dc736SMichael Ellerman 	addr = 0x1000000 + (unsigned long)buf;
635ae0dc736SMichael Ellerman 	patch_branch(p, addr, BRANCH_SET_LINK);
636ae0dc736SMichael Ellerman 	q = buf + 0x1400000;
6377c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6387c95d889SJordan Niethe 	patch_instruction(q, instr);
639ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
640ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
641ae0dc736SMichael Ellerman 
642ae0dc736SMichael Ellerman 	/* Jump to x + 16 MB moved to x - 16 MB + 4 */
643ae0dc736SMichael Ellerman 	p = buf + 0x1000000;
644ae0dc736SMichael Ellerman 	addr = 0x2000000 + (unsigned long)buf;
645ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
646ae0dc736SMichael Ellerman 	q = buf + 4;
6477c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6487c95d889SJordan Niethe 	patch_instruction(q, instr);
649ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
650ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
651ae0dc736SMichael Ellerman 
652ae0dc736SMichael Ellerman 
653ae0dc736SMichael Ellerman 	/* Conditional branch tests */
654ae0dc736SMichael Ellerman 
655ae0dc736SMichael Ellerman 	/* Simple case, branch to self moved a little */
656ae0dc736SMichael Ellerman 	p = buf;
657ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
6587c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0);
6597c95d889SJordan Niethe 	patch_instruction(p, instr);
660ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
6610b582db5SJordan Niethe 	q = buf + 4;
6627c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6637c95d889SJordan Niethe 	patch_instruction(q, instr);
664ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
665ae0dc736SMichael Ellerman 
666ae0dc736SMichael Ellerman 	/* Maximum negative case, move b . to addr + 32 KB */
667ae0dc736SMichael Ellerman 	p = buf;
668ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
6697c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
6707c95d889SJordan Niethe 	patch_instruction(p, instr);
671ae0dc736SMichael Ellerman 	q = buf + 0x8000;
6727c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6737c95d889SJordan Niethe 	patch_instruction(q, instr);
674ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
675ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
676f8faaffaSJordan Niethe 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
677ae0dc736SMichael Ellerman 
678ae0dc736SMichael Ellerman 	/* Maximum positive case, move x to x - 32 KB + 4 */
679ae0dc736SMichael Ellerman 	p = buf + 0x8000;
680ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
6817c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
6827c95d889SJordan Niethe 	patch_instruction(p, instr);
683ae0dc736SMichael Ellerman 	q = buf + 4;
6847c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6857c95d889SJordan Niethe 	patch_instruction(q, instr);
686ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
687ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
688f8faaffaSJordan Niethe 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
689ae0dc736SMichael Ellerman 
690ae0dc736SMichael Ellerman 	/* Jump to x + 12 KB moved to x + 20 KB */
691ae0dc736SMichael Ellerman 	p = buf;
692ae0dc736SMichael Ellerman 	addr = 0x3000 + (unsigned long)buf;
6937c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
6947c95d889SJordan Niethe 	patch_instruction(p, instr);
695ae0dc736SMichael Ellerman 	q = buf + 0x5000;
6967c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6977c95d889SJordan Niethe 	patch_instruction(q, instr);
698ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
699ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
700ae0dc736SMichael Ellerman 
701ae0dc736SMichael Ellerman 	/* Jump to x + 8 KB moved to x - 8 KB + 4 */
702ae0dc736SMichael Ellerman 	p = buf + 0x2000;
703ae0dc736SMichael Ellerman 	addr = 0x4000 + (unsigned long)buf;
7047c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0);
7057c95d889SJordan Niethe 	patch_instruction(p, instr);
706ae0dc736SMichael Ellerman 	q = buf + 4;
7077c95d889SJordan Niethe 	translate_branch(&instr, q, p);
7087c95d889SJordan Niethe 	patch_instruction(q, instr);
709ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
710ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
711ae0dc736SMichael Ellerman 
712ae0dc736SMichael Ellerman 	/* Free the buffer we were using */
713ae0dc736SMichael Ellerman 	vfree(buf);
714ae0dc736SMichael Ellerman }
715ae0dc736SMichael Ellerman 
716f77f8ff7SJordan Niethe #ifdef CONFIG_PPC64
717f77f8ff7SJordan Niethe static void __init test_prefixed_patching(void)
718f77f8ff7SJordan Niethe {
719f77f8ff7SJordan Niethe 	extern unsigned int code_patching_test1[];
720f77f8ff7SJordan Niethe 	extern unsigned int code_patching_test1_expected[];
721f77f8ff7SJordan Niethe 	extern unsigned int end_code_patching_test1[];
722f77f8ff7SJordan Niethe 
72369d4d6e5SChristophe Leroy 	__patch_instruction(code_patching_test1,
724f77f8ff7SJordan Niethe 			    ppc_inst_prefix(OP_PREFIX << 26, 0x00000000),
72569d4d6e5SChristophe Leroy 			    code_patching_test1);
726f77f8ff7SJordan Niethe 
727f77f8ff7SJordan Niethe 	check(!memcmp(code_patching_test1,
728f77f8ff7SJordan Niethe 		      code_patching_test1_expected,
729f77f8ff7SJordan Niethe 		      sizeof(unsigned int) *
730f77f8ff7SJordan Niethe 		      (end_code_patching_test1 - code_patching_test1)));
731f77f8ff7SJordan Niethe }
732f77f8ff7SJordan Niethe #else
733f77f8ff7SJordan Niethe static inline void test_prefixed_patching(void) {}
734f77f8ff7SJordan Niethe #endif
735f77f8ff7SJordan Niethe 
736ae0dc736SMichael Ellerman static int __init test_code_patching(void)
737ae0dc736SMichael Ellerman {
738*edecd2d6SChristophe Leroy 	pr_info("Running code patching self-tests ...\n");
739ae0dc736SMichael Ellerman 
740ae0dc736SMichael Ellerman 	test_branch_iform();
741ae0dc736SMichael Ellerman 	test_branch_bform();
742ae0dc736SMichael Ellerman 	test_create_function_call();
743ae0dc736SMichael Ellerman 	test_translate_branch();
744f77f8ff7SJordan Niethe 	test_prefixed_patching();
745ae0dc736SMichael Ellerman 
746ae0dc736SMichael Ellerman 	return 0;
747ae0dc736SMichael Ellerman }
748ae0dc736SMichael Ellerman late_initcall(test_code_patching);
749ae0dc736SMichael Ellerman 
750ae0dc736SMichael Ellerman #endif /* CONFIG_CODE_PATCHING_SELFTEST */
751