18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright 2008 Michael Ellerman, IBM Corporation.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/kprobes.h>
88c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/mm.h>
118c2ecf20Sopenharmony_ci#include <linux/cpuhotplug.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
168c2ecf20Sopenharmony_ci#include <asm/page.h>
178c2ecf20Sopenharmony_ci#include <asm/code-patching.h>
188c2ecf20Sopenharmony_ci#include <asm/setup.h>
198c2ecf20Sopenharmony_ci#include <asm/inst.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic int __patch_instruction(struct ppc_inst *exec_addr, struct ppc_inst instr,
228c2ecf20Sopenharmony_ci			       struct ppc_inst *patch_addr)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	if (!ppc_inst_prefixed(instr))
258c2ecf20Sopenharmony_ci		__put_user_asm_goto(ppc_inst_val(instr), patch_addr, failed, "stw");
268c2ecf20Sopenharmony_ci	else
278c2ecf20Sopenharmony_ci		__put_user_asm_goto(ppc_inst_as_u64(instr), patch_addr, failed, "std");
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
308c2ecf20Sopenharmony_ci							    "r" (exec_addr));
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	return 0;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cifailed:
358c2ecf20Sopenharmony_ci	return -EFAULT;
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciint raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	return __patch_instruction(addr, instr, addr);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#ifdef CONFIG_STRICT_KERNEL_RWX
448c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic int text_area_cpu_up(unsigned int cpu)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct vm_struct *area;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	area = get_vm_area(PAGE_SIZE, VM_ALLOC);
518c2ecf20Sopenharmony_ci	if (!area) {
528c2ecf20Sopenharmony_ci		WARN_ONCE(1, "Failed to create text area for cpu %d\n",
538c2ecf20Sopenharmony_ci			cpu);
548c2ecf20Sopenharmony_ci		return -1;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci	this_cpu_write(text_poke_area, area);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return 0;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int text_area_cpu_down(unsigned int cpu)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	free_vm_area(this_cpu_read(text_poke_area));
648c2ecf20Sopenharmony_ci	return 0;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/*
688c2ecf20Sopenharmony_ci * Run as a late init call. This allows all the boot time patching to be done
698c2ecf20Sopenharmony_ci * simply by patching the code, and then we're called here prior to
708c2ecf20Sopenharmony_ci * mark_rodata_ro(), which happens after all init calls are run. Although
718c2ecf20Sopenharmony_ci * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
728c2ecf20Sopenharmony_ci * it as being preferable to a kernel that will crash later when someone tries
738c2ecf20Sopenharmony_ci * to use patch_instruction().
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistatic int __init setup_text_poke_area(void)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
788c2ecf20Sopenharmony_ci		"powerpc/text_poke:online", text_area_cpu_up,
798c2ecf20Sopenharmony_ci		text_area_cpu_down));
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return 0;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_cilate_initcall(setup_text_poke_area);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * This can be called for kernel text or a module.
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_cistatic int map_patch_area(void *addr, unsigned long text_poke_addr)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	unsigned long pfn;
918c2ecf20Sopenharmony_ci	int err;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (is_vmalloc_or_module_addr(addr))
948c2ecf20Sopenharmony_ci		pfn = vmalloc_to_pfn(addr);
958c2ecf20Sopenharmony_ci	else
968c2ecf20Sopenharmony_ci		pfn = __pa_symbol(addr) >> PAGE_SHIFT;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
1018c2ecf20Sopenharmony_ci	if (err)
1028c2ecf20Sopenharmony_ci		return -1;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return 0;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic inline int unmap_patch_area(unsigned long addr)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	pte_t *ptep;
1108c2ecf20Sopenharmony_ci	pmd_t *pmdp;
1118c2ecf20Sopenharmony_ci	pud_t *pudp;
1128c2ecf20Sopenharmony_ci	p4d_t *p4dp;
1138c2ecf20Sopenharmony_ci	pgd_t *pgdp;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	pgdp = pgd_offset_k(addr);
1168c2ecf20Sopenharmony_ci	if (unlikely(!pgdp))
1178c2ecf20Sopenharmony_ci		return -EINVAL;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	p4dp = p4d_offset(pgdp, addr);
1208c2ecf20Sopenharmony_ci	if (unlikely(!p4dp))
1218c2ecf20Sopenharmony_ci		return -EINVAL;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	pudp = pud_offset(p4dp, addr);
1248c2ecf20Sopenharmony_ci	if (unlikely(!pudp))
1258c2ecf20Sopenharmony_ci		return -EINVAL;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	pmdp = pmd_offset(pudp, addr);
1288c2ecf20Sopenharmony_ci	if (unlikely(!pmdp))
1298c2ecf20Sopenharmony_ci		return -EINVAL;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	ptep = pte_offset_kernel(pmdp, addr);
1328c2ecf20Sopenharmony_ci	if (unlikely(!ptep))
1338c2ecf20Sopenharmony_ci		return -EINVAL;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	/*
1388c2ecf20Sopenharmony_ci	 * In hash, pte_clear flushes the tlb, in radix, we have to
1398c2ecf20Sopenharmony_ci	 */
1408c2ecf20Sopenharmony_ci	pte_clear(&init_mm, addr, ptep);
1418c2ecf20Sopenharmony_ci	flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return 0;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	int err;
1498c2ecf20Sopenharmony_ci	struct ppc_inst *patch_addr = NULL;
1508c2ecf20Sopenharmony_ci	unsigned long flags;
1518c2ecf20Sopenharmony_ci	unsigned long text_poke_addr;
1528c2ecf20Sopenharmony_ci	unsigned long kaddr = (unsigned long)addr;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/*
1558c2ecf20Sopenharmony_ci	 * During early early boot patch_instruction is called
1568c2ecf20Sopenharmony_ci	 * when text_poke_area is not ready, but we still need
1578c2ecf20Sopenharmony_ci	 * to allow patching. We just do the plain old patching
1588c2ecf20Sopenharmony_ci	 */
1598c2ecf20Sopenharmony_ci	if (!this_cpu_read(text_poke_area))
1608c2ecf20Sopenharmony_ci		return raw_patch_instruction(addr, instr);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	local_irq_save(flags);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
1658c2ecf20Sopenharmony_ci	if (map_patch_area(addr, text_poke_addr)) {
1668c2ecf20Sopenharmony_ci		err = -1;
1678c2ecf20Sopenharmony_ci		goto out;
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	patch_addr = (struct ppc_inst *)(text_poke_addr + (kaddr & ~PAGE_MASK));
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	__patch_instruction(addr, instr, patch_addr);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	err = unmap_patch_area(text_poke_addr);
1758c2ecf20Sopenharmony_ci	if (err)
1768c2ecf20Sopenharmony_ci		pr_warn("failed to unmap %lx\n", text_poke_addr);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ciout:
1798c2ecf20Sopenharmony_ci	local_irq_restore(flags);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return err;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci#else /* !CONFIG_STRICT_KERNEL_RWX */
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	return raw_patch_instruction(addr, instr);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci#endif /* CONFIG_STRICT_KERNEL_RWX */
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciint patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	/* Make sure we aren't patching a freed init section */
1958c2ecf20Sopenharmony_ci	if (init_mem_is_free && init_section_contains(addr, 4)) {
1968c2ecf20Sopenharmony_ci		pr_debug("Skipping init section patching addr: 0x%px\n", addr);
1978c2ecf20Sopenharmony_ci		return 0;
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci	return do_patch_instruction(addr, instr);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ciNOKPROBE_SYMBOL(patch_instruction);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ciint patch_branch(struct ppc_inst *addr, unsigned long target, int flags)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	struct ppc_inst instr;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	create_branch(&instr, addr, target, flags);
2088c2ecf20Sopenharmony_ci	return patch_instruction(addr, instr);
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cibool is_offset_in_branch_range(long offset)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	/*
2148c2ecf20Sopenharmony_ci	 * Powerpc branch instruction is :
2158c2ecf20Sopenharmony_ci	 *
2168c2ecf20Sopenharmony_ci	 *  0         6                 30   31
2178c2ecf20Sopenharmony_ci	 *  +---------+----------------+---+---+
2188c2ecf20Sopenharmony_ci	 *  | opcode  |     LI         |AA |LK |
2198c2ecf20Sopenharmony_ci	 *  +---------+----------------+---+---+
2208c2ecf20Sopenharmony_ci	 *  Where AA = 0 and LK = 0
2218c2ecf20Sopenharmony_ci	 *
2228c2ecf20Sopenharmony_ci	 * LI is a signed 24 bits integer. The real branch offset is computed
2238c2ecf20Sopenharmony_ci	 * by: imm32 = SignExtend(LI:'0b00', 32);
2248c2ecf20Sopenharmony_ci	 *
2258c2ecf20Sopenharmony_ci	 * So the maximum forward branch should be:
2268c2ecf20Sopenharmony_ci	 *   (0x007fffff << 2) = 0x01fffffc =  0x1fffffc
2278c2ecf20Sopenharmony_ci	 * The maximum backward branch should be:
2288c2ecf20Sopenharmony_ci	 *   (0xff800000 << 2) = 0xfe000000 = -0x2000000
2298c2ecf20Sopenharmony_ci	 */
2308c2ecf20Sopenharmony_ci	return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cibool is_offset_in_cond_branch_range(long offset)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci/*
2398c2ecf20Sopenharmony_ci * Helper to check if a given instruction is a conditional branch
2408c2ecf20Sopenharmony_ci * Derived from the conditional checks in analyse_instr()
2418c2ecf20Sopenharmony_ci */
2428c2ecf20Sopenharmony_cibool is_conditional_branch(struct ppc_inst instr)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	unsigned int opcode = ppc_inst_primary_opcode(instr);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	if (opcode == 16)       /* bc, bca, bcl, bcla */
2478c2ecf20Sopenharmony_ci		return true;
2488c2ecf20Sopenharmony_ci	if (opcode == 19) {
2498c2ecf20Sopenharmony_ci		switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
2508c2ecf20Sopenharmony_ci		case 16:        /* bclr, bclrl */
2518c2ecf20Sopenharmony_ci		case 528:       /* bcctr, bcctrl */
2528c2ecf20Sopenharmony_ci		case 560:       /* bctar, bctarl */
2538c2ecf20Sopenharmony_ci			return true;
2548c2ecf20Sopenharmony_ci		}
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci	return false;
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ciNOKPROBE_SYMBOL(is_conditional_branch);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ciint create_branch(struct ppc_inst *instr,
2618c2ecf20Sopenharmony_ci		  const struct ppc_inst *addr,
2628c2ecf20Sopenharmony_ci		  unsigned long target, int flags)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	long offset;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	*instr = ppc_inst(0);
2678c2ecf20Sopenharmony_ci	offset = target;
2688c2ecf20Sopenharmony_ci	if (! (flags & BRANCH_ABSOLUTE))
2698c2ecf20Sopenharmony_ci		offset = offset - (unsigned long)addr;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	/* Check we can represent the target in the instruction format */
2728c2ecf20Sopenharmony_ci	if (!is_offset_in_branch_range(offset))
2738c2ecf20Sopenharmony_ci		return 1;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/* Mask out the flags and target, so they don't step on each other. */
2768c2ecf20Sopenharmony_ci	*instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	return 0;
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ciint create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
2828c2ecf20Sopenharmony_ci		       unsigned long target, int flags)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	long offset;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	offset = target;
2878c2ecf20Sopenharmony_ci	if (! (flags & BRANCH_ABSOLUTE))
2888c2ecf20Sopenharmony_ci		offset = offset - (unsigned long)addr;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	/* Check we can represent the target in the instruction format */
2918c2ecf20Sopenharmony_ci	if (!is_offset_in_cond_branch_range(offset))
2928c2ecf20Sopenharmony_ci		return 1;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	/* Mask out the flags and target, so they don't step on each other. */
2958c2ecf20Sopenharmony_ci	*instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return 0;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic unsigned int branch_opcode(struct ppc_inst instr)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	return ppc_inst_primary_opcode(instr) & 0x3F;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic int instr_is_branch_iform(struct ppc_inst instr)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	return branch_opcode(instr) == 18;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic int instr_is_branch_bform(struct ppc_inst instr)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	return branch_opcode(instr) == 16;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ciint instr_is_relative_branch(struct ppc_inst instr)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
3188c2ecf20Sopenharmony_ci		return 0;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ciint instr_is_relative_link_branch(struct ppc_inst instr)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic unsigned long branch_iform_target(const struct ppc_inst *instr)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	signed long imm;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	imm = ppc_inst_val(*instr) & 0x3FFFFFC;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	/* If the top bit of the immediate value is set this is negative */
3358c2ecf20Sopenharmony_ci	if (imm & 0x2000000)
3368c2ecf20Sopenharmony_ci		imm -= 0x4000000;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
3398c2ecf20Sopenharmony_ci		imm += (unsigned long)instr;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	return (unsigned long)imm;
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic unsigned long branch_bform_target(const struct ppc_inst *instr)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	signed long imm;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	imm = ppc_inst_val(*instr) & 0xFFFC;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	/* If the top bit of the immediate value is set this is negative */
3518c2ecf20Sopenharmony_ci	if (imm & 0x8000)
3528c2ecf20Sopenharmony_ci		imm -= 0x10000;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
3558c2ecf20Sopenharmony_ci		imm += (unsigned long)instr;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	return (unsigned long)imm;
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ciunsigned long branch_target(const struct ppc_inst *instr)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	if (instr_is_branch_iform(ppc_inst_read(instr)))
3638c2ecf20Sopenharmony_ci		return branch_iform_target(instr);
3648c2ecf20Sopenharmony_ci	else if (instr_is_branch_bform(ppc_inst_read(instr)))
3658c2ecf20Sopenharmony_ci		return branch_bform_target(instr);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	return 0;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ciint instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	if (instr_is_branch_iform(ppc_inst_read(instr)) ||
3738c2ecf20Sopenharmony_ci	    instr_is_branch_bform(ppc_inst_read(instr)))
3748c2ecf20Sopenharmony_ci		return branch_target(instr) == addr;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	return 0;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ciint translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest,
3808c2ecf20Sopenharmony_ci		     const struct ppc_inst *src)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	unsigned long target;
3838c2ecf20Sopenharmony_ci	target = branch_target(src);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	if (instr_is_branch_iform(ppc_inst_read(src)))
3868c2ecf20Sopenharmony_ci		return create_branch(instr, dest, target,
3878c2ecf20Sopenharmony_ci				     ppc_inst_val(ppc_inst_read(src)));
3888c2ecf20Sopenharmony_ci	else if (instr_is_branch_bform(ppc_inst_read(src)))
3898c2ecf20Sopenharmony_ci		return create_cond_branch(instr, dest, target,
3908c2ecf20Sopenharmony_ci					  ppc_inst_val(ppc_inst_read(src)));
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	return 1;
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3E_64
3968c2ecf20Sopenharmony_civoid __patch_exception(int exc, unsigned long addr)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	extern unsigned int interrupt_base_book3e;
3998c2ecf20Sopenharmony_ci	unsigned int *ibase = &interrupt_base_book3e;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	/* Our exceptions vectors start with a NOP and -then- a branch
4028c2ecf20Sopenharmony_ci	 * to deal with single stepping from userspace which stops on
4038c2ecf20Sopenharmony_ci	 * the second instruction. Thus we need to patch the second
4048c2ecf20Sopenharmony_ci	 * instruction of the exception, not the first one
4058c2ecf20Sopenharmony_ci	 */
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	patch_branch((struct ppc_inst *)(ibase + (exc / 4) + 1), addr, 0);
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ci#endif
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci#ifdef CONFIG_CODE_PATCHING_SELFTEST
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic void __init test_trampoline(void)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	asm ("nop;\n");
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci#define check(x)	\
4198c2ecf20Sopenharmony_ci	if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic void __init test_branch_iform(void)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	int err;
4248c2ecf20Sopenharmony_ci	struct ppc_inst instr;
4258c2ecf20Sopenharmony_ci	unsigned long addr;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	addr = (unsigned long)&instr;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	/* The simplest case, branch to self, no flags */
4308c2ecf20Sopenharmony_ci	check(instr_is_branch_iform(ppc_inst(0x48000000)));
4318c2ecf20Sopenharmony_ci	/* All bits of target set, and flags */
4328c2ecf20Sopenharmony_ci	check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
4338c2ecf20Sopenharmony_ci	/* High bit of opcode set, which is wrong */
4348c2ecf20Sopenharmony_ci	check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
4358c2ecf20Sopenharmony_ci	/* Middle bits of opcode set, which is wrong */
4368c2ecf20Sopenharmony_ci	check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	/* Simplest case, branch to self with link */
4398c2ecf20Sopenharmony_ci	check(instr_is_branch_iform(ppc_inst(0x48000001)));
4408c2ecf20Sopenharmony_ci	/* All bits of targets set */
4418c2ecf20Sopenharmony_ci	check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
4428c2ecf20Sopenharmony_ci	/* Some bits of targets set */
4438c2ecf20Sopenharmony_ci	check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
4448c2ecf20Sopenharmony_ci	/* Must be a valid branch to start with */
4458c2ecf20Sopenharmony_ci	check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	/* Absolute branch to 0x100 */
4488c2ecf20Sopenharmony_ci	instr = ppc_inst(0x48000103);
4498c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, 0x100));
4508c2ecf20Sopenharmony_ci	/* Absolute branch to 0x420fc */
4518c2ecf20Sopenharmony_ci	instr = ppc_inst(0x480420ff);
4528c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, 0x420fc));
4538c2ecf20Sopenharmony_ci	/* Maximum positive relative branch, + 20MB - 4B */
4548c2ecf20Sopenharmony_ci	instr = ppc_inst(0x49fffffc);
4558c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
4568c2ecf20Sopenharmony_ci	/* Smallest negative relative branch, - 4B */
4578c2ecf20Sopenharmony_ci	instr = ppc_inst(0x4bfffffc);
4588c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr - 4));
4598c2ecf20Sopenharmony_ci	/* Largest negative relative branch, - 32 MB */
4608c2ecf20Sopenharmony_ci	instr = ppc_inst(0x4a000000);
4618c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	/* Branch to self, with link */
4648c2ecf20Sopenharmony_ci	err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK);
4658c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr));
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	/* Branch to self - 0x100, with link */
4688c2ecf20Sopenharmony_ci	err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK);
4698c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr - 0x100));
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	/* Branch to self + 0x100, no link */
4728c2ecf20Sopenharmony_ci	err = create_branch(&instr, &instr, addr + 0x100, 0);
4738c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr + 0x100));
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	/* Maximum relative negative offset, - 32 MB */
4768c2ecf20Sopenharmony_ci	err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK);
4778c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	/* Out of range relative negative offset, - 32 MB + 4*/
4808c2ecf20Sopenharmony_ci	err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK);
4818c2ecf20Sopenharmony_ci	check(err);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	/* Out of range relative positive offset, + 32 MB */
4848c2ecf20Sopenharmony_ci	err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK);
4858c2ecf20Sopenharmony_ci	check(err);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	/* Unaligned target */
4888c2ecf20Sopenharmony_ci	err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK);
4898c2ecf20Sopenharmony_ci	check(err);
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	/* Check flags are masked correctly */
4928c2ecf20Sopenharmony_ci	err = create_branch(&instr, &instr, addr, 0xFFFFFFFC);
4938c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr));
4948c2ecf20Sopenharmony_ci	check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
4958c2ecf20Sopenharmony_ci}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic void __init test_create_function_call(void)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	struct ppc_inst *iptr;
5008c2ecf20Sopenharmony_ci	unsigned long dest;
5018c2ecf20Sopenharmony_ci	struct ppc_inst instr;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	/* Check we can create a function call */
5048c2ecf20Sopenharmony_ci	iptr = (struct ppc_inst *)ppc_function_entry(test_trampoline);
5058c2ecf20Sopenharmony_ci	dest = ppc_function_entry(test_create_function_call);
5068c2ecf20Sopenharmony_ci	create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
5078c2ecf20Sopenharmony_ci	patch_instruction(iptr, instr);
5088c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(iptr, dest));
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic void __init test_branch_bform(void)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	int err;
5148c2ecf20Sopenharmony_ci	unsigned long addr;
5158c2ecf20Sopenharmony_ci	struct ppc_inst *iptr, instr;
5168c2ecf20Sopenharmony_ci	unsigned int flags;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	iptr = &instr;
5198c2ecf20Sopenharmony_ci	addr = (unsigned long)iptr;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	/* The simplest case, branch to self, no flags */
5228c2ecf20Sopenharmony_ci	check(instr_is_branch_bform(ppc_inst(0x40000000)));
5238c2ecf20Sopenharmony_ci	/* All bits of target set, and flags */
5248c2ecf20Sopenharmony_ci	check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
5258c2ecf20Sopenharmony_ci	/* High bit of opcode set, which is wrong */
5268c2ecf20Sopenharmony_ci	check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
5278c2ecf20Sopenharmony_ci	/* Middle bits of opcode set, which is wrong */
5288c2ecf20Sopenharmony_ci	check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	/* Absolute conditional branch to 0x100 */
5318c2ecf20Sopenharmony_ci	instr = ppc_inst(0x43ff0103);
5328c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, 0x100));
5338c2ecf20Sopenharmony_ci	/* Absolute conditional branch to 0x20fc */
5348c2ecf20Sopenharmony_ci	instr = ppc_inst(0x43ff20ff);
5358c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, 0x20fc));
5368c2ecf20Sopenharmony_ci	/* Maximum positive relative conditional branch, + 32 KB - 4B */
5378c2ecf20Sopenharmony_ci	instr = ppc_inst(0x43ff7ffc);
5388c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
5398c2ecf20Sopenharmony_ci	/* Smallest negative relative conditional branch, - 4B */
5408c2ecf20Sopenharmony_ci	instr = ppc_inst(0x43fffffc);
5418c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr - 4));
5428c2ecf20Sopenharmony_ci	/* Largest negative relative conditional branch, - 32 KB */
5438c2ecf20Sopenharmony_ci	instr = ppc_inst(0x43ff8000);
5448c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr - 0x8000));
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	/* All condition code bits set & link */
5478c2ecf20Sopenharmony_ci	flags = 0x3ff000 | BRANCH_SET_LINK;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	/* Branch to self */
5508c2ecf20Sopenharmony_ci	err = create_cond_branch(&instr, iptr, addr, flags);
5518c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr));
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	/* Branch to self - 0x100 */
5548c2ecf20Sopenharmony_ci	err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
5558c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr - 0x100));
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	/* Branch to self + 0x100 */
5588c2ecf20Sopenharmony_ci	err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
5598c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr + 0x100));
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	/* Maximum relative negative offset, - 32 KB */
5628c2ecf20Sopenharmony_ci	err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
5638c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr - 0x8000));
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	/* Out of range relative negative offset, - 32 KB + 4*/
5668c2ecf20Sopenharmony_ci	err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
5678c2ecf20Sopenharmony_ci	check(err);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	/* Out of range relative positive offset, + 32 KB */
5708c2ecf20Sopenharmony_ci	err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
5718c2ecf20Sopenharmony_ci	check(err);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	/* Unaligned target */
5748c2ecf20Sopenharmony_ci	err = create_cond_branch(&instr, iptr, addr + 3, flags);
5758c2ecf20Sopenharmony_ci	check(err);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	/* Check flags are masked correctly */
5788c2ecf20Sopenharmony_ci	err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
5798c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(&instr, addr));
5808c2ecf20Sopenharmony_ci	check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_cistatic void __init test_translate_branch(void)
5848c2ecf20Sopenharmony_ci{
5858c2ecf20Sopenharmony_ci	unsigned long addr;
5868c2ecf20Sopenharmony_ci	void *p, *q;
5878c2ecf20Sopenharmony_ci	struct ppc_inst instr;
5888c2ecf20Sopenharmony_ci	void *buf;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
5918c2ecf20Sopenharmony_ci	check(buf);
5928c2ecf20Sopenharmony_ci	if (!buf)
5938c2ecf20Sopenharmony_ci		return;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	/* Simple case, branch to self moved a little */
5968c2ecf20Sopenharmony_ci	p = buf;
5978c2ecf20Sopenharmony_ci	addr = (unsigned long)p;
5988c2ecf20Sopenharmony_ci	patch_branch(p, addr, 0);
5998c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
6008c2ecf20Sopenharmony_ci	q = p + 4;
6018c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
6028c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
6038c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	/* Maximum negative case, move b . to addr + 32 MB */
6068c2ecf20Sopenharmony_ci	p = buf;
6078c2ecf20Sopenharmony_ci	addr = (unsigned long)p;
6088c2ecf20Sopenharmony_ci	patch_branch(p, addr, 0);
6098c2ecf20Sopenharmony_ci	q = buf + 0x2000000;
6108c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
6118c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
6128c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
6138c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
6148c2ecf20Sopenharmony_ci	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	/* Maximum positive case, move x to x - 32 MB + 4 */
6178c2ecf20Sopenharmony_ci	p = buf + 0x2000000;
6188c2ecf20Sopenharmony_ci	addr = (unsigned long)p;
6198c2ecf20Sopenharmony_ci	patch_branch(p, addr, 0);
6208c2ecf20Sopenharmony_ci	q = buf + 4;
6218c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
6228c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
6238c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
6248c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
6258c2ecf20Sopenharmony_ci	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	/* Jump to x + 16 MB moved to x + 20 MB */
6288c2ecf20Sopenharmony_ci	p = buf;
6298c2ecf20Sopenharmony_ci	addr = 0x1000000 + (unsigned long)buf;
6308c2ecf20Sopenharmony_ci	patch_branch(p, addr, BRANCH_SET_LINK);
6318c2ecf20Sopenharmony_ci	q = buf + 0x1400000;
6328c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
6338c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
6348c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
6358c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	/* Jump to x + 16 MB moved to x - 16 MB + 4 */
6388c2ecf20Sopenharmony_ci	p = buf + 0x1000000;
6398c2ecf20Sopenharmony_ci	addr = 0x2000000 + (unsigned long)buf;
6408c2ecf20Sopenharmony_ci	patch_branch(p, addr, 0);
6418c2ecf20Sopenharmony_ci	q = buf + 4;
6428c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
6438c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
6448c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
6458c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	/* Conditional branch tests */
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	/* Simple case, branch to self moved a little */
6518c2ecf20Sopenharmony_ci	p = buf;
6528c2ecf20Sopenharmony_ci	addr = (unsigned long)p;
6538c2ecf20Sopenharmony_ci	create_cond_branch(&instr, p, addr, 0);
6548c2ecf20Sopenharmony_ci	patch_instruction(p, instr);
6558c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
6568c2ecf20Sopenharmony_ci	q = buf + 4;
6578c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
6588c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
6598c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	/* Maximum negative case, move b . to addr + 32 KB */
6628c2ecf20Sopenharmony_ci	p = buf;
6638c2ecf20Sopenharmony_ci	addr = (unsigned long)p;
6648c2ecf20Sopenharmony_ci	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
6658c2ecf20Sopenharmony_ci	patch_instruction(p, instr);
6668c2ecf20Sopenharmony_ci	q = buf + 0x8000;
6678c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
6688c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
6698c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
6708c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
6718c2ecf20Sopenharmony_ci	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	/* Maximum positive case, move x to x - 32 KB + 4 */
6748c2ecf20Sopenharmony_ci	p = buf + 0x8000;
6758c2ecf20Sopenharmony_ci	addr = (unsigned long)p;
6768c2ecf20Sopenharmony_ci	create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
6778c2ecf20Sopenharmony_ci	patch_instruction(p, instr);
6788c2ecf20Sopenharmony_ci	q = buf + 4;
6798c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
6808c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
6818c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
6828c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
6838c2ecf20Sopenharmony_ci	check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	/* Jump to x + 12 KB moved to x + 20 KB */
6868c2ecf20Sopenharmony_ci	p = buf;
6878c2ecf20Sopenharmony_ci	addr = 0x3000 + (unsigned long)buf;
6888c2ecf20Sopenharmony_ci	create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
6898c2ecf20Sopenharmony_ci	patch_instruction(p, instr);
6908c2ecf20Sopenharmony_ci	q = buf + 0x5000;
6918c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
6928c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
6938c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
6948c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	/* Jump to x + 8 KB moved to x - 8 KB + 4 */
6978c2ecf20Sopenharmony_ci	p = buf + 0x2000;
6988c2ecf20Sopenharmony_ci	addr = 0x4000 + (unsigned long)buf;
6998c2ecf20Sopenharmony_ci	create_cond_branch(&instr, p, addr, 0);
7008c2ecf20Sopenharmony_ci	patch_instruction(p, instr);
7018c2ecf20Sopenharmony_ci	q = buf + 4;
7028c2ecf20Sopenharmony_ci	translate_branch(&instr, q, p);
7038c2ecf20Sopenharmony_ci	patch_instruction(q, instr);
7048c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(p, addr));
7058c2ecf20Sopenharmony_ci	check(instr_is_branch_to_addr(q, addr));
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	/* Free the buffer we were using */
7088c2ecf20Sopenharmony_ci	vfree(buf);
7098c2ecf20Sopenharmony_ci}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
7128c2ecf20Sopenharmony_cistatic void __init test_prefixed_patching(void)
7138c2ecf20Sopenharmony_ci{
7148c2ecf20Sopenharmony_ci	extern unsigned int code_patching_test1[];
7158c2ecf20Sopenharmony_ci	extern unsigned int code_patching_test1_expected[];
7168c2ecf20Sopenharmony_ci	extern unsigned int end_code_patching_test1[];
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	__patch_instruction((struct ppc_inst *)code_patching_test1,
7198c2ecf20Sopenharmony_ci			    ppc_inst_prefix(OP_PREFIX << 26, 0x00000000),
7208c2ecf20Sopenharmony_ci			    (struct ppc_inst *)code_patching_test1);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	check(!memcmp(code_patching_test1,
7238c2ecf20Sopenharmony_ci		      code_patching_test1_expected,
7248c2ecf20Sopenharmony_ci		      sizeof(unsigned int) *
7258c2ecf20Sopenharmony_ci		      (end_code_patching_test1 - code_patching_test1)));
7268c2ecf20Sopenharmony_ci}
7278c2ecf20Sopenharmony_ci#else
7288c2ecf20Sopenharmony_cistatic inline void test_prefixed_patching(void) {}
7298c2ecf20Sopenharmony_ci#endif
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_cistatic int __init test_code_patching(void)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "Running code patching self-tests ...\n");
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	test_branch_iform();
7368c2ecf20Sopenharmony_ci	test_branch_bform();
7378c2ecf20Sopenharmony_ci	test_create_function_call();
7388c2ecf20Sopenharmony_ci	test_translate_branch();
7398c2ecf20Sopenharmony_ci	test_prefixed_patching();
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	return 0;
7428c2ecf20Sopenharmony_ci}
7438c2ecf20Sopenharmony_cilate_initcall(test_code_patching);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci#endif /* CONFIG_CODE_PATCHING_SELFTEST */
746