xref: /linux/arch/powerpc/lib/code-patching.c (revision e90a21ea801d1776d9a786ad02354fd3fe23ce09)
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 
2194afd069SJordan Niethe static int __patch_instruction(struct ppc_inst *exec_addr, struct ppc_inst instr,
2294afd069SJordan Niethe 			       struct ppc_inst *patch_addr)
23aaddd3eaSMichael Ellerman {
24e63ceebdSChristophe Leroy 	if (!ppc_inst_prefixed(instr)) {
25e63ceebdSChristophe Leroy 		u32 val = ppc_inst_val(instr);
26e63ceebdSChristophe Leroy 
27e63ceebdSChristophe Leroy 		__put_kernel_nofault(patch_addr, &val, u32, failed);
28e63ceebdSChristophe Leroy 	} else {
29693557ebSChristophe Leroy 		u64 val = ppc_inst_as_ulong(instr);
30e63ceebdSChristophe Leroy 
31e63ceebdSChristophe Leroy 		__put_kernel_nofault(patch_addr, &val, u64, failed);
32e63ceebdSChristophe Leroy 	}
3337bc3e5fSBalbir Singh 
348cf4c057SChristophe Leroy 	asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
358cf4c057SChristophe Leroy 							    "r" (exec_addr));
3637bc3e5fSBalbir Singh 
37b6e37968SSteven Rostedt 	return 0;
38e64ac41aSChristophe Leroy 
39e64ac41aSChristophe Leroy failed:
40e64ac41aSChristophe Leroy 	return -EFAULT;
41aaddd3eaSMichael Ellerman }
42aaddd3eaSMichael Ellerman 
4394afd069SJordan Niethe int raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
448cf4c057SChristophe Leroy {
458cf4c057SChristophe Leroy 	return __patch_instruction(addr, instr, addr);
468cf4c057SChristophe Leroy }
478cf4c057SChristophe Leroy 
4837bc3e5fSBalbir Singh #ifdef CONFIG_STRICT_KERNEL_RWX
4937bc3e5fSBalbir Singh static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
5037bc3e5fSBalbir Singh 
5137bc3e5fSBalbir Singh static int text_area_cpu_up(unsigned int cpu)
5237bc3e5fSBalbir Singh {
5337bc3e5fSBalbir Singh 	struct vm_struct *area;
5437bc3e5fSBalbir Singh 
5537bc3e5fSBalbir Singh 	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
5637bc3e5fSBalbir Singh 	if (!area) {
5737bc3e5fSBalbir Singh 		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
5837bc3e5fSBalbir Singh 			cpu);
5937bc3e5fSBalbir Singh 		return -1;
6037bc3e5fSBalbir Singh 	}
6137bc3e5fSBalbir Singh 	this_cpu_write(text_poke_area, area);
6237bc3e5fSBalbir Singh 
6337bc3e5fSBalbir Singh 	return 0;
6437bc3e5fSBalbir Singh }
6537bc3e5fSBalbir Singh 
6637bc3e5fSBalbir Singh static int text_area_cpu_down(unsigned int cpu)
6737bc3e5fSBalbir Singh {
6837bc3e5fSBalbir Singh 	free_vm_area(this_cpu_read(text_poke_area));
6937bc3e5fSBalbir Singh 	return 0;
7037bc3e5fSBalbir Singh }
7137bc3e5fSBalbir Singh 
7237bc3e5fSBalbir Singh /*
7337bc3e5fSBalbir Singh  * Run as a late init call. This allows all the boot time patching to be done
7437bc3e5fSBalbir Singh  * simply by patching the code, and then we're called here prior to
7537bc3e5fSBalbir Singh  * mark_rodata_ro(), which happens after all init calls are run. Although
7637bc3e5fSBalbir Singh  * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
7737bc3e5fSBalbir Singh  * it as being preferable to a kernel that will crash later when someone tries
7837bc3e5fSBalbir Singh  * to use patch_instruction().
7937bc3e5fSBalbir Singh  */
8037bc3e5fSBalbir Singh static int __init setup_text_poke_area(void)
8137bc3e5fSBalbir Singh {
8237bc3e5fSBalbir Singh 	BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
8337bc3e5fSBalbir Singh 		"powerpc/text_poke:online", text_area_cpu_up,
8437bc3e5fSBalbir Singh 		text_area_cpu_down));
8537bc3e5fSBalbir Singh 
8637bc3e5fSBalbir Singh 	return 0;
8737bc3e5fSBalbir Singh }
8837bc3e5fSBalbir Singh late_initcall(setup_text_poke_area);
8937bc3e5fSBalbir Singh 
9037bc3e5fSBalbir Singh /*
9137bc3e5fSBalbir Singh  * This can be called for kernel text or a module.
9237bc3e5fSBalbir Singh  */
9337bc3e5fSBalbir Singh static int map_patch_area(void *addr, unsigned long text_poke_addr)
9437bc3e5fSBalbir Singh {
9537bc3e5fSBalbir Singh 	unsigned long pfn;
9637bc3e5fSBalbir Singh 	int err;
9737bc3e5fSBalbir Singh 
98ccc8fcf7SChristophe Leroy 	if (is_vmalloc_or_module_addr(addr))
9937bc3e5fSBalbir Singh 		pfn = vmalloc_to_pfn(addr);
10037bc3e5fSBalbir Singh 	else
10137bc3e5fSBalbir Singh 		pfn = __pa_symbol(addr) >> PAGE_SHIFT;
10237bc3e5fSBalbir Singh 
103c766ee72SChristophe Leroy 	err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
10437bc3e5fSBalbir Singh 
10537bc3e5fSBalbir Singh 	pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
10637bc3e5fSBalbir Singh 	if (err)
10737bc3e5fSBalbir Singh 		return -1;
10837bc3e5fSBalbir Singh 
10937bc3e5fSBalbir Singh 	return 0;
11037bc3e5fSBalbir Singh }
11137bc3e5fSBalbir Singh 
11237bc3e5fSBalbir Singh static inline int unmap_patch_area(unsigned long addr)
11337bc3e5fSBalbir Singh {
11437bc3e5fSBalbir Singh 	pte_t *ptep;
11537bc3e5fSBalbir Singh 	pmd_t *pmdp;
11637bc3e5fSBalbir Singh 	pud_t *pudp;
1172fb47060SMike Rapoport 	p4d_t *p4dp;
11837bc3e5fSBalbir Singh 	pgd_t *pgdp;
11937bc3e5fSBalbir Singh 
12037bc3e5fSBalbir Singh 	pgdp = pgd_offset_k(addr);
12137bc3e5fSBalbir Singh 	if (unlikely(!pgdp))
12237bc3e5fSBalbir Singh 		return -EINVAL;
12337bc3e5fSBalbir Singh 
1242fb47060SMike Rapoport 	p4dp = p4d_offset(pgdp, addr);
1252fb47060SMike Rapoport 	if (unlikely(!p4dp))
1262fb47060SMike Rapoport 		return -EINVAL;
1272fb47060SMike Rapoport 
1282fb47060SMike Rapoport 	pudp = pud_offset(p4dp, addr);
12937bc3e5fSBalbir Singh 	if (unlikely(!pudp))
13037bc3e5fSBalbir Singh 		return -EINVAL;
13137bc3e5fSBalbir Singh 
13237bc3e5fSBalbir Singh 	pmdp = pmd_offset(pudp, addr);
13337bc3e5fSBalbir Singh 	if (unlikely(!pmdp))
13437bc3e5fSBalbir Singh 		return -EINVAL;
13537bc3e5fSBalbir Singh 
13637bc3e5fSBalbir Singh 	ptep = pte_offset_kernel(pmdp, addr);
13737bc3e5fSBalbir Singh 	if (unlikely(!ptep))
13837bc3e5fSBalbir Singh 		return -EINVAL;
13937bc3e5fSBalbir Singh 
14037bc3e5fSBalbir Singh 	pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
14137bc3e5fSBalbir Singh 
14237bc3e5fSBalbir Singh 	/*
14337bc3e5fSBalbir Singh 	 * In hash, pte_clear flushes the tlb, in radix, we have to
14437bc3e5fSBalbir Singh 	 */
14537bc3e5fSBalbir Singh 	pte_clear(&init_mm, addr, ptep);
14637bc3e5fSBalbir Singh 	flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
14737bc3e5fSBalbir Singh 
14837bc3e5fSBalbir Singh 	return 0;
14937bc3e5fSBalbir Singh }
15037bc3e5fSBalbir Singh 
15194afd069SJordan Niethe static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
15237bc3e5fSBalbir Singh {
15337bc3e5fSBalbir Singh 	int err;
15494afd069SJordan Niethe 	struct ppc_inst *patch_addr = NULL;
15537bc3e5fSBalbir Singh 	unsigned long flags;
15637bc3e5fSBalbir Singh 	unsigned long text_poke_addr;
15737bc3e5fSBalbir Singh 	unsigned long kaddr = (unsigned long)addr;
15837bc3e5fSBalbir Singh 
15937bc3e5fSBalbir Singh 	/*
16037bc3e5fSBalbir Singh 	 * During early early boot patch_instruction is called
16137bc3e5fSBalbir Singh 	 * when text_poke_area is not ready, but we still need
16237bc3e5fSBalbir Singh 	 * to allow patching. We just do the plain old patching
16337bc3e5fSBalbir Singh 	 */
1648183d99fSChristophe Leroy 	if (!this_cpu_read(text_poke_area))
1658cf4c057SChristophe Leroy 		return raw_patch_instruction(addr, instr);
16637bc3e5fSBalbir Singh 
16737bc3e5fSBalbir Singh 	local_irq_save(flags);
16837bc3e5fSBalbir Singh 
16937bc3e5fSBalbir Singh 	text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
17037bc3e5fSBalbir Singh 	if (map_patch_area(addr, text_poke_addr)) {
17137bc3e5fSBalbir Singh 		err = -1;
17237bc3e5fSBalbir Singh 		goto out;
17337bc3e5fSBalbir Singh 	}
17437bc3e5fSBalbir Singh 
17594afd069SJordan Niethe 	patch_addr = (struct ppc_inst *)(text_poke_addr + (kaddr & ~PAGE_MASK));
17637bc3e5fSBalbir Singh 
1778cf4c057SChristophe Leroy 	__patch_instruction(addr, instr, patch_addr);
17837bc3e5fSBalbir Singh 
17937bc3e5fSBalbir Singh 	err = unmap_patch_area(text_poke_addr);
18037bc3e5fSBalbir Singh 	if (err)
18137bc3e5fSBalbir Singh 		pr_warn("failed to unmap %lx\n", text_poke_addr);
18237bc3e5fSBalbir Singh 
18337bc3e5fSBalbir Singh out:
18437bc3e5fSBalbir Singh 	local_irq_restore(flags);
18537bc3e5fSBalbir Singh 
18637bc3e5fSBalbir Singh 	return err;
18737bc3e5fSBalbir Singh }
18837bc3e5fSBalbir Singh #else /* !CONFIG_STRICT_KERNEL_RWX */
18937bc3e5fSBalbir Singh 
19094afd069SJordan Niethe static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
19137bc3e5fSBalbir Singh {
1928cf4c057SChristophe Leroy 	return raw_patch_instruction(addr, instr);
19337bc3e5fSBalbir Singh }
19437bc3e5fSBalbir Singh 
19537bc3e5fSBalbir Singh #endif /* CONFIG_STRICT_KERNEL_RWX */
196b45ba4a5SChristophe Leroy 
19794afd069SJordan Niethe int patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
198b45ba4a5SChristophe Leroy {
199b45ba4a5SChristophe Leroy 	/* Make sure we aren't patching a freed init section */
200b45ba4a5SChristophe Leroy 	if (init_mem_is_free && init_section_contains(addr, 4)) {
201b45ba4a5SChristophe Leroy 		pr_debug("Skipping init section patching addr: 0x%px\n", addr);
202b45ba4a5SChristophe Leroy 		return 0;
203b45ba4a5SChristophe Leroy 	}
204b45ba4a5SChristophe Leroy 	return do_patch_instruction(addr, instr);
205b45ba4a5SChristophe Leroy }
20637bc3e5fSBalbir Singh NOKPROBE_SYMBOL(patch_instruction);
20737bc3e5fSBalbir Singh 
20894afd069SJordan Niethe int patch_branch(struct ppc_inst *addr, unsigned long target, int flags)
209e7a57273SMichael Ellerman {
21094afd069SJordan Niethe 	struct ppc_inst instr;
2117c95d889SJordan Niethe 
2127c95d889SJordan Niethe 	create_branch(&instr, addr, target, flags);
2137c95d889SJordan Niethe 	return patch_instruction(addr, instr);
214e7a57273SMichael Ellerman }
215e7a57273SMichael Ellerman 
216ebfa50dfSAnju T bool is_offset_in_branch_range(long offset)
217ebfa50dfSAnju T {
218ebfa50dfSAnju T 	/*
219ebfa50dfSAnju T 	 * Powerpc branch instruction is :
220ebfa50dfSAnju T 	 *
221ebfa50dfSAnju T 	 *  0         6                 30   31
222ebfa50dfSAnju T 	 *  +---------+----------------+---+---+
223ebfa50dfSAnju T 	 *  | opcode  |     LI         |AA |LK |
224ebfa50dfSAnju T 	 *  +---------+----------------+---+---+
225ebfa50dfSAnju T 	 *  Where AA = 0 and LK = 0
226ebfa50dfSAnju T 	 *
227ebfa50dfSAnju T 	 * LI is a signed 24 bits integer. The real branch offset is computed
228ebfa50dfSAnju T 	 * by: imm32 = SignExtend(LI:'0b00', 32);
229ebfa50dfSAnju T 	 *
230ebfa50dfSAnju T 	 * So the maximum forward branch should be:
231ebfa50dfSAnju T 	 *   (0x007fffff << 2) = 0x01fffffc =  0x1fffffc
232ebfa50dfSAnju T 	 * The maximum backward branch should be:
233ebfa50dfSAnju T 	 *   (0xff800000 << 2) = 0xfe000000 = -0x2000000
234ebfa50dfSAnju T 	 */
235ebfa50dfSAnju T 	return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
236ebfa50dfSAnju T }
237ebfa50dfSAnju T 
23851c9c084SAnju T /*
23951c9c084SAnju T  * Helper to check if a given instruction is a conditional branch
24051c9c084SAnju T  * Derived from the conditional checks in analyse_instr()
24151c9c084SAnju T  */
24294afd069SJordan Niethe bool is_conditional_branch(struct ppc_inst instr)
24351c9c084SAnju T {
2448094892dSJordan Niethe 	unsigned int opcode = ppc_inst_primary_opcode(instr);
24551c9c084SAnju T 
24651c9c084SAnju T 	if (opcode == 16)       /* bc, bca, bcl, bcla */
24751c9c084SAnju T 		return true;
24851c9c084SAnju T 	if (opcode == 19) {
249777e26f0SJordan Niethe 		switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
25051c9c084SAnju T 		case 16:        /* bclr, bclrl */
25151c9c084SAnju T 		case 528:       /* bcctr, bcctrl */
25251c9c084SAnju T 		case 560:       /* bctar, bctarl */
25351c9c084SAnju T 			return true;
25451c9c084SAnju T 		}
25551c9c084SAnju T 	}
25651c9c084SAnju T 	return false;
25751c9c084SAnju T }
25871f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(is_conditional_branch);
25951c9c084SAnju T 
26094afd069SJordan Niethe int create_branch(struct ppc_inst *instr,
26194afd069SJordan Niethe 		  const struct ppc_inst *addr,
262e7a57273SMichael Ellerman 		  unsigned long target, int flags)
263aaddd3eaSMichael Ellerman {
264053a858eSMichael Ellerman 	long offset;
265aaddd3eaSMichael Ellerman 
26694afd069SJordan Niethe 	*instr = ppc_inst(0);
267053a858eSMichael Ellerman 	offset = target;
268aaddd3eaSMichael Ellerman 	if (! (flags & BRANCH_ABSOLUTE))
269053a858eSMichael Ellerman 		offset = offset - (unsigned long)addr;
270053a858eSMichael Ellerman 
271053a858eSMichael Ellerman 	/* Check we can represent the target in the instruction format */
272ebfa50dfSAnju T 	if (!is_offset_in_branch_range(offset))
2737c95d889SJordan Niethe 		return 1;
274aaddd3eaSMichael Ellerman 
275aaddd3eaSMichael Ellerman 	/* Mask out the flags and target, so they don't step on each other. */
27694afd069SJordan Niethe 	*instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
277aaddd3eaSMichael Ellerman 
2787c95d889SJordan Niethe 	return 0;
279aaddd3eaSMichael Ellerman }
280411781a2SMichael Ellerman 
28194afd069SJordan Niethe int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
282411781a2SMichael Ellerman 		       unsigned long target, int flags)
283411781a2SMichael Ellerman {
284411781a2SMichael Ellerman 	long offset;
285411781a2SMichael Ellerman 
286411781a2SMichael Ellerman 	offset = target;
287411781a2SMichael Ellerman 	if (! (flags & BRANCH_ABSOLUTE))
288411781a2SMichael Ellerman 		offset = offset - (unsigned long)addr;
289411781a2SMichael Ellerman 
290411781a2SMichael Ellerman 	/* Check we can represent the target in the instruction format */
291411781a2SMichael Ellerman 	if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
2927c95d889SJordan Niethe 		return 1;
293411781a2SMichael Ellerman 
294411781a2SMichael Ellerman 	/* Mask out the flags and target, so they don't step on each other. */
29594afd069SJordan Niethe 	*instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
296411781a2SMichael Ellerman 
2977c95d889SJordan Niethe 	return 0;
298411781a2SMichael Ellerman }
299411781a2SMichael Ellerman 
30094afd069SJordan Niethe static unsigned int branch_opcode(struct ppc_inst instr)
301411781a2SMichael Ellerman {
3028094892dSJordan Niethe 	return ppc_inst_primary_opcode(instr) & 0x3F;
303411781a2SMichael Ellerman }
304411781a2SMichael Ellerman 
30594afd069SJordan Niethe static int instr_is_branch_iform(struct ppc_inst instr)
306411781a2SMichael Ellerman {
307411781a2SMichael Ellerman 	return branch_opcode(instr) == 18;
308411781a2SMichael Ellerman }
309411781a2SMichael Ellerman 
31094afd069SJordan Niethe static int instr_is_branch_bform(struct ppc_inst instr)
311411781a2SMichael Ellerman {
312411781a2SMichael Ellerman 	return branch_opcode(instr) == 16;
313411781a2SMichael Ellerman }
314411781a2SMichael Ellerman 
31594afd069SJordan Niethe int instr_is_relative_branch(struct ppc_inst instr)
316411781a2SMichael Ellerman {
317777e26f0SJordan Niethe 	if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
318411781a2SMichael Ellerman 		return 0;
319411781a2SMichael Ellerman 
320411781a2SMichael Ellerman 	return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
321411781a2SMichael Ellerman }
322411781a2SMichael Ellerman 
32394afd069SJordan Niethe int instr_is_relative_link_branch(struct ppc_inst instr)
324b9eab08dSJosh Poimboeuf {
325777e26f0SJordan Niethe 	return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
326b9eab08dSJosh Poimboeuf }
327b9eab08dSJosh Poimboeuf 
32894afd069SJordan Niethe static unsigned long branch_iform_target(const struct ppc_inst *instr)
329411781a2SMichael Ellerman {
330411781a2SMichael Ellerman 	signed long imm;
331411781a2SMichael Ellerman 
33218c85964SChristophe Leroy 	imm = ppc_inst_val(ppc_inst_read(instr)) & 0x3FFFFFC;
333411781a2SMichael Ellerman 
334411781a2SMichael Ellerman 	/* If the top bit of the immediate value is set this is negative */
335411781a2SMichael Ellerman 	if (imm & 0x2000000)
336411781a2SMichael Ellerman 		imm -= 0x4000000;
337411781a2SMichael Ellerman 
33818c85964SChristophe Leroy 	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
339411781a2SMichael Ellerman 		imm += (unsigned long)instr;
340411781a2SMichael Ellerman 
341411781a2SMichael Ellerman 	return (unsigned long)imm;
342411781a2SMichael Ellerman }
343411781a2SMichael Ellerman 
34494afd069SJordan Niethe static unsigned long branch_bform_target(const struct ppc_inst *instr)
345411781a2SMichael Ellerman {
346411781a2SMichael Ellerman 	signed long imm;
347411781a2SMichael Ellerman 
34818c85964SChristophe Leroy 	imm = ppc_inst_val(ppc_inst_read(instr)) & 0xFFFC;
349411781a2SMichael Ellerman 
350411781a2SMichael Ellerman 	/* If the top bit of the immediate value is set this is negative */
351411781a2SMichael Ellerman 	if (imm & 0x8000)
352411781a2SMichael Ellerman 		imm -= 0x10000;
353411781a2SMichael Ellerman 
35418c85964SChristophe Leroy 	if ((ppc_inst_val(ppc_inst_read(instr)) & BRANCH_ABSOLUTE) == 0)
355411781a2SMichael Ellerman 		imm += (unsigned long)instr;
356411781a2SMichael Ellerman 
357411781a2SMichael Ellerman 	return (unsigned long)imm;
358411781a2SMichael Ellerman }
359411781a2SMichael Ellerman 
36094afd069SJordan Niethe unsigned long branch_target(const struct ppc_inst *instr)
361411781a2SMichael Ellerman {
362f8faaffaSJordan Niethe 	if (instr_is_branch_iform(ppc_inst_read(instr)))
363411781a2SMichael Ellerman 		return branch_iform_target(instr);
364f8faaffaSJordan Niethe 	else if (instr_is_branch_bform(ppc_inst_read(instr)))
365411781a2SMichael Ellerman 		return branch_bform_target(instr);
366411781a2SMichael Ellerman 
367411781a2SMichael Ellerman 	return 0;
368411781a2SMichael Ellerman }
369411781a2SMichael Ellerman 
37094afd069SJordan Niethe int translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest,
37194afd069SJordan Niethe 		     const struct ppc_inst *src)
372411781a2SMichael Ellerman {
373411781a2SMichael Ellerman 	unsigned long target;
374411781a2SMichael Ellerman 	target = branch_target(src);
375411781a2SMichael Ellerman 
376f8faaffaSJordan Niethe 	if (instr_is_branch_iform(ppc_inst_read(src)))
377f8faaffaSJordan Niethe 		return create_branch(instr, dest, target,
378f8faaffaSJordan Niethe 				     ppc_inst_val(ppc_inst_read(src)));
379f8faaffaSJordan Niethe 	else if (instr_is_branch_bform(ppc_inst_read(src)))
380f8faaffaSJordan Niethe 		return create_cond_branch(instr, dest, target,
381f8faaffaSJordan Niethe 					  ppc_inst_val(ppc_inst_read(src)));
382411781a2SMichael Ellerman 
3837c95d889SJordan Niethe 	return 1;
384411781a2SMichael Ellerman }
385ae0dc736SMichael Ellerman 
3861e8341aeSKevin Hao #ifdef CONFIG_PPC_BOOK3E_64
3871e8341aeSKevin Hao void __patch_exception(int exc, unsigned long addr)
3881e8341aeSKevin Hao {
3891e8341aeSKevin Hao 	extern unsigned int interrupt_base_book3e;
3901e8341aeSKevin Hao 	unsigned int *ibase = &interrupt_base_book3e;
3911e8341aeSKevin Hao 
3921e8341aeSKevin Hao 	/* Our exceptions vectors start with a NOP and -then- a branch
3931e8341aeSKevin Hao 	 * to deal with single stepping from userspace which stops on
3941e8341aeSKevin Hao 	 * the second instruction. Thus we need to patch the second
3951e8341aeSKevin Hao 	 * instruction of the exception, not the first one
3961e8341aeSKevin Hao 	 */
3971e8341aeSKevin Hao 
39894afd069SJordan Niethe 	patch_branch((struct ppc_inst *)(ibase + (exc / 4) + 1), addr, 0);
3991e8341aeSKevin Hao }
4001e8341aeSKevin Hao #endif
401ae0dc736SMichael Ellerman 
402ae0dc736SMichael Ellerman #ifdef CONFIG_CODE_PATCHING_SELFTEST
403ae0dc736SMichael Ellerman 
4046c0d181dSChristophe Leroy static int instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr)
4056c0d181dSChristophe Leroy {
4066c0d181dSChristophe Leroy 	if (instr_is_branch_iform(ppc_inst_read(instr)) ||
4076c0d181dSChristophe Leroy 	    instr_is_branch_bform(ppc_inst_read(instr)))
4086c0d181dSChristophe Leroy 		return branch_target(instr) == addr;
4096c0d181dSChristophe Leroy 
4106c0d181dSChristophe Leroy 	return 0;
4116c0d181dSChristophe Leroy }
4126c0d181dSChristophe Leroy 
413ae0dc736SMichael Ellerman static void __init test_trampoline(void)
414ae0dc736SMichael Ellerman {
415ae0dc736SMichael Ellerman 	asm ("nop;\n");
416ae0dc736SMichael Ellerman }
417ae0dc736SMichael Ellerman 
418ae0dc736SMichael Ellerman #define check(x)	\
419ae0dc736SMichael Ellerman 	if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
420ae0dc736SMichael Ellerman 
421ae0dc736SMichael Ellerman static void __init test_branch_iform(void)
422ae0dc736SMichael Ellerman {
4237c95d889SJordan Niethe 	int err;
42494afd069SJordan Niethe 	struct ppc_inst instr;
425*e90a21eaSChristophe Leroy 	u32 tmp[2];
426*e90a21eaSChristophe Leroy 	struct ppc_inst *iptr = (struct ppc_inst *)tmp;
427*e90a21eaSChristophe Leroy 	unsigned long addr = (unsigned long)tmp;
428ae0dc736SMichael Ellerman 
429ae0dc736SMichael Ellerman 	/* The simplest case, branch to self, no flags */
43075346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x48000000)));
431ae0dc736SMichael Ellerman 	/* All bits of target set, and flags */
43275346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
433ae0dc736SMichael Ellerman 	/* High bit of opcode set, which is wrong */
43475346251SJordan Niethe 	check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
435ae0dc736SMichael Ellerman 	/* Middle bits of opcode set, which is wrong */
43675346251SJordan Niethe 	check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
437ae0dc736SMichael Ellerman 
438ae0dc736SMichael Ellerman 	/* Simplest case, branch to self with link */
43975346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x48000001)));
440ae0dc736SMichael Ellerman 	/* All bits of targets set */
44175346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
442ae0dc736SMichael Ellerman 	/* Some bits of targets set */
44375346251SJordan Niethe 	check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
444ae0dc736SMichael Ellerman 	/* Must be a valid branch to start with */
44575346251SJordan Niethe 	check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
446ae0dc736SMichael Ellerman 
447ae0dc736SMichael Ellerman 	/* Absolute branch to 0x100 */
448*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x48000103));
449*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, 0x100));
450ae0dc736SMichael Ellerman 	/* Absolute branch to 0x420fc */
451*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x480420ff));
452*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, 0x420fc));
453ae0dc736SMichael Ellerman 	/* Maximum positive relative branch, + 20MB - 4B */
454*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x49fffffc));
455*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr + 0x1FFFFFC));
456ae0dc736SMichael Ellerman 	/* Smallest negative relative branch, - 4B */
457*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x4bfffffc));
458*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 4));
459ae0dc736SMichael Ellerman 	/* Largest negative relative branch, - 32 MB */
460*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x4a000000));
461*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
462ae0dc736SMichael Ellerman 
463ae0dc736SMichael Ellerman 	/* Branch to self, with link */
464*e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr, BRANCH_SET_LINK);
465*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
466*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr));
467ae0dc736SMichael Ellerman 
468ae0dc736SMichael Ellerman 	/* Branch to self - 0x100, with link */
469*e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr - 0x100, BRANCH_SET_LINK);
470*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
471*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x100));
472ae0dc736SMichael Ellerman 
473ae0dc736SMichael Ellerman 	/* Branch to self + 0x100, no link */
474*e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr + 0x100, 0);
475*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
476*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr + 0x100));
477ae0dc736SMichael Ellerman 
478ae0dc736SMichael Ellerman 	/* Maximum relative negative offset, - 32 MB */
479*e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr - 0x2000000, BRANCH_SET_LINK);
480*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
481*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
482ae0dc736SMichael Ellerman 
483ae0dc736SMichael Ellerman 	/* Out of range relative negative offset, - 32 MB + 4*/
484*e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr - 0x2000004, BRANCH_SET_LINK);
4857c95d889SJordan Niethe 	check(err);
486ae0dc736SMichael Ellerman 
487ae0dc736SMichael Ellerman 	/* Out of range relative positive offset, + 32 MB */
488*e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr + 0x2000000, BRANCH_SET_LINK);
4897c95d889SJordan Niethe 	check(err);
490ae0dc736SMichael Ellerman 
491ae0dc736SMichael Ellerman 	/* Unaligned target */
492*e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr + 3, BRANCH_SET_LINK);
4937c95d889SJordan Niethe 	check(err);
494ae0dc736SMichael Ellerman 
495ae0dc736SMichael Ellerman 	/* Check flags are masked correctly */
496*e90a21eaSChristophe Leroy 	err = create_branch(&instr, iptr, addr, 0xFFFFFFFC);
497*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
498*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr));
499217862d9SJordan Niethe 	check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
500ae0dc736SMichael Ellerman }
501ae0dc736SMichael Ellerman 
502ae0dc736SMichael Ellerman static void __init test_create_function_call(void)
503ae0dc736SMichael Ellerman {
50494afd069SJordan Niethe 	struct ppc_inst *iptr;
505ae0dc736SMichael Ellerman 	unsigned long dest;
50694afd069SJordan Niethe 	struct ppc_inst instr;
507ae0dc736SMichael Ellerman 
508ae0dc736SMichael Ellerman 	/* Check we can create a function call */
50994afd069SJordan Niethe 	iptr = (struct ppc_inst *)ppc_function_entry(test_trampoline);
510ae0dc736SMichael Ellerman 	dest = ppc_function_entry(test_create_function_call);
5117c95d889SJordan Niethe 	create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
5127c95d889SJordan Niethe 	patch_instruction(iptr, instr);
513ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(iptr, dest));
514ae0dc736SMichael Ellerman }
515ae0dc736SMichael Ellerman 
516ae0dc736SMichael Ellerman static void __init test_branch_bform(void)
517ae0dc736SMichael Ellerman {
5187c95d889SJordan Niethe 	int err;
519ae0dc736SMichael Ellerman 	unsigned long addr;
52094afd069SJordan Niethe 	struct ppc_inst *iptr, instr;
521*e90a21eaSChristophe Leroy 	u32 tmp[2];
52294afd069SJordan Niethe 	unsigned int flags;
523ae0dc736SMichael Ellerman 
524*e90a21eaSChristophe Leroy 	iptr = (struct ppc_inst *)tmp;
525ae0dc736SMichael Ellerman 	addr = (unsigned long)iptr;
526ae0dc736SMichael Ellerman 
527ae0dc736SMichael Ellerman 	/* The simplest case, branch to self, no flags */
52875346251SJordan Niethe 	check(instr_is_branch_bform(ppc_inst(0x40000000)));
529ae0dc736SMichael Ellerman 	/* All bits of target set, and flags */
53075346251SJordan Niethe 	check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
531ae0dc736SMichael Ellerman 	/* High bit of opcode set, which is wrong */
53275346251SJordan Niethe 	check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
533ae0dc736SMichael Ellerman 	/* Middle bits of opcode set, which is wrong */
53475346251SJordan Niethe 	check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
535ae0dc736SMichael Ellerman 
536ae0dc736SMichael Ellerman 	/* Absolute conditional branch to 0x100 */
537*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43ff0103));
538*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, 0x100));
539ae0dc736SMichael Ellerman 	/* Absolute conditional branch to 0x20fc */
540*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43ff20ff));
541*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, 0x20fc));
542ae0dc736SMichael Ellerman 	/* Maximum positive relative conditional branch, + 32 KB - 4B */
543*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43ff7ffc));
544*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr + 0x7FFC));
545ae0dc736SMichael Ellerman 	/* Smallest negative relative conditional branch, - 4B */
546*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43fffffc));
547*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 4));
548ae0dc736SMichael Ellerman 	/* Largest negative relative conditional branch, - 32 KB */
549*e90a21eaSChristophe Leroy 	patch_instruction(iptr, ppc_inst(0x43ff8000));
550*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x8000));
551ae0dc736SMichael Ellerman 
552ae0dc736SMichael Ellerman 	/* All condition code bits set & link */
553ae0dc736SMichael Ellerman 	flags = 0x3ff000 | BRANCH_SET_LINK;
554ae0dc736SMichael Ellerman 
555ae0dc736SMichael Ellerman 	/* Branch to self */
5567c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr, flags);
557*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
558*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr));
559ae0dc736SMichael Ellerman 
560ae0dc736SMichael Ellerman 	/* Branch to self - 0x100 */
5617c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
562*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
563*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x100));
564ae0dc736SMichael Ellerman 
565ae0dc736SMichael Ellerman 	/* Branch to self + 0x100 */
5667c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
567*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
568*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr + 0x100));
569ae0dc736SMichael Ellerman 
570ae0dc736SMichael Ellerman 	/* Maximum relative negative offset, - 32 KB */
5717c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
572*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
573*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr - 0x8000));
574ae0dc736SMichael Ellerman 
575ae0dc736SMichael Ellerman 	/* Out of range relative negative offset, - 32 KB + 4*/
5767c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
5777c95d889SJordan Niethe 	check(err);
578ae0dc736SMichael Ellerman 
579ae0dc736SMichael Ellerman 	/* Out of range relative positive offset, + 32 KB */
5807c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
5817c95d889SJordan Niethe 	check(err);
582ae0dc736SMichael Ellerman 
583ae0dc736SMichael Ellerman 	/* Unaligned target */
5847c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr + 3, flags);
5857c95d889SJordan Niethe 	check(err);
586ae0dc736SMichael Ellerman 
587ae0dc736SMichael Ellerman 	/* Check flags are masked correctly */
5887c95d889SJordan Niethe 	err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
589*e90a21eaSChristophe Leroy 	patch_instruction(iptr, instr);
590*e90a21eaSChristophe Leroy 	check(instr_is_branch_to_addr(iptr, addr));
591217862d9SJordan Niethe 	check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
592ae0dc736SMichael Ellerman }
593ae0dc736SMichael Ellerman 
594ae0dc736SMichael Ellerman static void __init test_translate_branch(void)
595ae0dc736SMichael Ellerman {
596ae0dc736SMichael Ellerman 	unsigned long addr;
5970b582db5SJordan Niethe 	void *p, *q;
59894afd069SJordan Niethe 	struct ppc_inst instr;
599ae0dc736SMichael Ellerman 	void *buf;
600ae0dc736SMichael Ellerman 
601ae0dc736SMichael Ellerman 	buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
602ae0dc736SMichael Ellerman 	check(buf);
603ae0dc736SMichael Ellerman 	if (!buf)
604ae0dc736SMichael Ellerman 		return;
605ae0dc736SMichael Ellerman 
606ae0dc736SMichael Ellerman 	/* Simple case, branch to self moved a little */
607ae0dc736SMichael Ellerman 	p = buf;
608ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
609ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
610ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
6110b582db5SJordan Niethe 	q = p + 4;
6127c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6137c95d889SJordan Niethe 	patch_instruction(q, instr);
614ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
615ae0dc736SMichael Ellerman 
616ae0dc736SMichael Ellerman 	/* Maximum negative case, move b . to addr + 32 MB */
617ae0dc736SMichael Ellerman 	p = buf;
618ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
619ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
620ae0dc736SMichael Ellerman 	q = buf + 0x2000000;
6217c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6227c95d889SJordan Niethe 	patch_instruction(q, instr);
623ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
624ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
625f8faaffaSJordan Niethe 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
626ae0dc736SMichael Ellerman 
627ae0dc736SMichael Ellerman 	/* Maximum positive case, move x to x - 32 MB + 4 */
628ae0dc736SMichael Ellerman 	p = buf + 0x2000000;
629ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
630ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
631ae0dc736SMichael Ellerman 	q = buf + 4;
6327c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6337c95d889SJordan Niethe 	patch_instruction(q, instr);
634ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
635ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
636f8faaffaSJordan Niethe 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
637ae0dc736SMichael Ellerman 
638ae0dc736SMichael Ellerman 	/* Jump to x + 16 MB moved to x + 20 MB */
639ae0dc736SMichael Ellerman 	p = buf;
640ae0dc736SMichael Ellerman 	addr = 0x1000000 + (unsigned long)buf;
641ae0dc736SMichael Ellerman 	patch_branch(p, addr, BRANCH_SET_LINK);
642ae0dc736SMichael Ellerman 	q = buf + 0x1400000;
6437c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6447c95d889SJordan Niethe 	patch_instruction(q, instr);
645ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
646ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
647ae0dc736SMichael Ellerman 
648ae0dc736SMichael Ellerman 	/* Jump to x + 16 MB moved to x - 16 MB + 4 */
649ae0dc736SMichael Ellerman 	p = buf + 0x1000000;
650ae0dc736SMichael Ellerman 	addr = 0x2000000 + (unsigned long)buf;
651ae0dc736SMichael Ellerman 	patch_branch(p, addr, 0);
652ae0dc736SMichael Ellerman 	q = buf + 4;
6537c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6547c95d889SJordan Niethe 	patch_instruction(q, instr);
655ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
656ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
657ae0dc736SMichael Ellerman 
658ae0dc736SMichael Ellerman 
659ae0dc736SMichael Ellerman 	/* Conditional branch tests */
660ae0dc736SMichael Ellerman 
661ae0dc736SMichael Ellerman 	/* Simple case, branch to self moved a little */
662ae0dc736SMichael Ellerman 	p = buf;
663ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
6647c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0);
6657c95d889SJordan Niethe 	patch_instruction(p, instr);
666ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
6670b582db5SJordan Niethe 	q = buf + 4;
6687c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6697c95d889SJordan Niethe 	patch_instruction(q, instr);
670ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
671ae0dc736SMichael Ellerman 
672ae0dc736SMichael Ellerman 	/* Maximum negative case, move b . to addr + 32 KB */
673ae0dc736SMichael Ellerman 	p = buf;
674ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
6757c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
6767c95d889SJordan Niethe 	patch_instruction(p, instr);
677ae0dc736SMichael Ellerman 	q = buf + 0x8000;
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));
682f8faaffaSJordan Niethe 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
683ae0dc736SMichael Ellerman 
684ae0dc736SMichael Ellerman 	/* Maximum positive case, move x to x - 32 KB + 4 */
685ae0dc736SMichael Ellerman 	p = buf + 0x8000;
686ae0dc736SMichael Ellerman 	addr = (unsigned long)p;
6877c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
6887c95d889SJordan Niethe 	patch_instruction(p, instr);
689ae0dc736SMichael Ellerman 	q = buf + 4;
6907c95d889SJordan Niethe 	translate_branch(&instr, q, p);
6917c95d889SJordan Niethe 	patch_instruction(q, instr);
692ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
693ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
694f8faaffaSJordan Niethe 	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
695ae0dc736SMichael Ellerman 
696ae0dc736SMichael Ellerman 	/* Jump to x + 12 KB moved to x + 20 KB */
697ae0dc736SMichael Ellerman 	p = buf;
698ae0dc736SMichael Ellerman 	addr = 0x3000 + (unsigned long)buf;
6997c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
7007c95d889SJordan Niethe 	patch_instruction(p, instr);
701ae0dc736SMichael Ellerman 	q = buf + 0x5000;
7027c95d889SJordan Niethe 	translate_branch(&instr, q, p);
7037c95d889SJordan Niethe 	patch_instruction(q, instr);
704ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
705ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
706ae0dc736SMichael Ellerman 
707ae0dc736SMichael Ellerman 	/* Jump to x + 8 KB moved to x - 8 KB + 4 */
708ae0dc736SMichael Ellerman 	p = buf + 0x2000;
709ae0dc736SMichael Ellerman 	addr = 0x4000 + (unsigned long)buf;
7107c95d889SJordan Niethe 	create_cond_branch(&instr, p, addr, 0);
7117c95d889SJordan Niethe 	patch_instruction(p, instr);
712ae0dc736SMichael Ellerman 	q = buf + 4;
7137c95d889SJordan Niethe 	translate_branch(&instr, q, p);
7147c95d889SJordan Niethe 	patch_instruction(q, instr);
715ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(p, addr));
716ae0dc736SMichael Ellerman 	check(instr_is_branch_to_addr(q, addr));
717ae0dc736SMichael Ellerman 
718ae0dc736SMichael Ellerman 	/* Free the buffer we were using */
719ae0dc736SMichael Ellerman 	vfree(buf);
720ae0dc736SMichael Ellerman }
721ae0dc736SMichael Ellerman 
722f77f8ff7SJordan Niethe #ifdef CONFIG_PPC64
723f77f8ff7SJordan Niethe static void __init test_prefixed_patching(void)
724f77f8ff7SJordan Niethe {
725f77f8ff7SJordan Niethe 	extern unsigned int code_patching_test1[];
726f77f8ff7SJordan Niethe 	extern unsigned int code_patching_test1_expected[];
727f77f8ff7SJordan Niethe 	extern unsigned int end_code_patching_test1[];
728f77f8ff7SJordan Niethe 
729f77f8ff7SJordan Niethe 	__patch_instruction((struct ppc_inst *)code_patching_test1,
730f77f8ff7SJordan Niethe 			    ppc_inst_prefix(OP_PREFIX << 26, 0x00000000),
731f77f8ff7SJordan Niethe 			    (struct ppc_inst *)code_patching_test1);
732f77f8ff7SJordan Niethe 
733f77f8ff7SJordan Niethe 	check(!memcmp(code_patching_test1,
734f77f8ff7SJordan Niethe 		      code_patching_test1_expected,
735f77f8ff7SJordan Niethe 		      sizeof(unsigned int) *
736f77f8ff7SJordan Niethe 		      (end_code_patching_test1 - code_patching_test1)));
737f77f8ff7SJordan Niethe }
738f77f8ff7SJordan Niethe #else
739f77f8ff7SJordan Niethe static inline void test_prefixed_patching(void) {}
740f77f8ff7SJordan Niethe #endif
741f77f8ff7SJordan Niethe 
742ae0dc736SMichael Ellerman static int __init test_code_patching(void)
743ae0dc736SMichael Ellerman {
744ae0dc736SMichael Ellerman 	printk(KERN_DEBUG "Running code patching self-tests ...\n");
745ae0dc736SMichael Ellerman 
746ae0dc736SMichael Ellerman 	test_branch_iform();
747ae0dc736SMichael Ellerman 	test_branch_bform();
748ae0dc736SMichael Ellerman 	test_create_function_call();
749ae0dc736SMichael Ellerman 	test_translate_branch();
750f77f8ff7SJordan Niethe 	test_prefixed_patching();
751ae0dc736SMichael Ellerman 
752ae0dc736SMichael Ellerman 	return 0;
753ae0dc736SMichael Ellerman }
754ae0dc736SMichael Ellerman late_initcall(test_code_patching);
755ae0dc736SMichael Ellerman 
756ae0dc736SMichael Ellerman #endif /* CONFIG_CODE_PATCHING_SELFTEST */
757