^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2008 Michael Ellerman, IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/cpuhotplug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/code-patching.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/inst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int __patch_instruction(struct ppc_inst *exec_addr, struct ppc_inst instr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct ppc_inst *patch_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (!ppc_inst_prefixed(instr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) __put_user_asm_goto(ppc_inst_val(instr), patch_addr, failed, "stw");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) __put_user_asm_goto(ppc_inst_as_u64(instr), patch_addr, failed, "std");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) "r" (exec_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return __patch_instruction(addr, instr, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #ifdef CONFIG_STRICT_KERNEL_RWX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static int text_area_cpu_up(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct vm_struct *area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) area = get_vm_area(PAGE_SIZE, VM_ALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (!area) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) WARN_ONCE(1, "Failed to create text area for cpu %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) this_cpu_write(text_poke_area, area);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int text_area_cpu_down(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) free_vm_area(this_cpu_read(text_poke_area));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Run as a late init call. This allows all the boot time patching to be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * simply by patching the code, and then we're called here prior to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * mark_rodata_ro(), which happens after all init calls are run. Although
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * it as being preferable to a kernel that will crash later when someone tries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * to use patch_instruction().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static int __init setup_text_poke_area(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) "powerpc/text_poke:online", text_area_cpu_up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) text_area_cpu_down));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) late_initcall(setup_text_poke_area);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * This can be called for kernel text or a module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int map_patch_area(void *addr, unsigned long text_poke_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (is_vmalloc_or_module_addr(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pfn = vmalloc_to_pfn(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pfn = __pa_symbol(addr) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static inline int unmap_patch_area(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pte_t *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) pmd_t *pmdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) pud_t *pudp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) p4d_t *p4dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) pgd_t *pgdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pgdp = pgd_offset_k(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (unlikely(!pgdp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) p4dp = p4d_offset(pgdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (unlikely(!p4dp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) pudp = pud_offset(p4dp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (unlikely(!pudp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pmdp = pmd_offset(pudp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (unlikely(!pmdp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ptep = pte_offset_kernel(pmdp, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (unlikely(!ptep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * In hash, pte_clear flushes the tlb, in radix, we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pte_clear(&init_mm, addr, ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct ppc_inst *patch_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) unsigned long text_poke_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) unsigned long kaddr = (unsigned long)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * During early early boot patch_instruction is called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * when text_poke_area is not ready, but we still need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * to allow patching. We just do the plain old patching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!this_cpu_read(text_poke_area))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return raw_patch_instruction(addr, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (map_patch_area(addr, text_poke_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) patch_addr = (struct ppc_inst *)(text_poke_addr + (kaddr & ~PAGE_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) __patch_instruction(addr, instr, patch_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) err = unmap_patch_area(text_poke_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) pr_warn("failed to unmap %lx\n", text_poke_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #else /* !CONFIG_STRICT_KERNEL_RWX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return raw_patch_instruction(addr, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) #endif /* CONFIG_STRICT_KERNEL_RWX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* Make sure we aren't patching a freed init section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (init_mem_is_free && init_section_contains(addr, 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) pr_debug("Skipping init section patching addr: 0x%px\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return do_patch_instruction(addr, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) NOKPROBE_SYMBOL(patch_instruction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int patch_branch(struct ppc_inst *addr, unsigned long target, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct ppc_inst instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) create_branch(&instr, addr, target, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return patch_instruction(addr, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) bool is_offset_in_branch_range(long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * Powerpc branch instruction is :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * 0 6 30 31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * +---------+----------------+---+---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * | opcode | LI |AA |LK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * +---------+----------------+---+---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * Where AA = 0 and LK = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * LI is a signed 24 bits integer. The real branch offset is computed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * by: imm32 = SignExtend(LI:'0b00', 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * So the maximum forward branch should be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * The maximum backward branch should be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * (0xff800000 << 2) = 0xfe000000 = -0x2000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) bool is_offset_in_cond_branch_range(long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * Helper to check if a given instruction is a conditional branch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * Derived from the conditional checks in analyse_instr()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) bool is_conditional_branch(struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) unsigned int opcode = ppc_inst_primary_opcode(instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (opcode == 16) /* bc, bca, bcl, bcla */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (opcode == 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) case 16: /* bclr, bclrl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) case 528: /* bcctr, bcctrl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) case 560: /* bctar, bctarl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) NOKPROBE_SYMBOL(is_conditional_branch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int create_branch(struct ppc_inst *instr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) const struct ppc_inst *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) unsigned long target, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) *instr = ppc_inst(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) offset = target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (! (flags & BRANCH_ABSOLUTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) offset = offset - (unsigned long)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /* Check we can represent the target in the instruction format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!is_offset_in_branch_range(offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /* Mask out the flags and target, so they don't step on each other. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) *instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) unsigned long target, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) offset = target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (! (flags & BRANCH_ABSOLUTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) offset = offset - (unsigned long)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /* Check we can represent the target in the instruction format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (!is_offset_in_cond_branch_range(offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* Mask out the flags and target, so they don't step on each other. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) *instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static unsigned int branch_opcode(struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return ppc_inst_primary_opcode(instr) & 0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int instr_is_branch_iform(struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return branch_opcode(instr) == 18;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int instr_is_branch_bform(struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return branch_opcode(instr) == 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int instr_is_relative_branch(struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int instr_is_relative_link_branch(struct ppc_inst instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static unsigned long branch_iform_target(const struct ppc_inst *instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) signed long imm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) imm = ppc_inst_val(*instr) & 0x3FFFFFC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* If the top bit of the immediate value is set this is negative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (imm & 0x2000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) imm -= 0x4000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) imm += (unsigned long)instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return (unsigned long)imm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static unsigned long branch_bform_target(const struct ppc_inst *instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) signed long imm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) imm = ppc_inst_val(*instr) & 0xFFFC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* If the top bit of the immediate value is set this is negative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (imm & 0x8000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) imm -= 0x10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) imm += (unsigned long)instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return (unsigned long)imm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) unsigned long branch_target(const struct ppc_inst *instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (instr_is_branch_iform(ppc_inst_read(instr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return branch_iform_target(instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) else if (instr_is_branch_bform(ppc_inst_read(instr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return branch_bform_target(instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (instr_is_branch_iform(ppc_inst_read(instr)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) instr_is_branch_bform(ppc_inst_read(instr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return branch_target(instr) == addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) int translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) const struct ppc_inst *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) unsigned long target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) target = branch_target(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (instr_is_branch_iform(ppc_inst_read(src)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return create_branch(instr, dest, target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ppc_inst_val(ppc_inst_read(src)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) else if (instr_is_branch_bform(ppc_inst_read(src)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return create_cond_branch(instr, dest, target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) ppc_inst_val(ppc_inst_read(src)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) #ifdef CONFIG_PPC_BOOK3E_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) void __patch_exception(int exc, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) extern unsigned int interrupt_base_book3e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) unsigned int *ibase = &interrupt_base_book3e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /* Our exceptions vectors start with a NOP and -then- a branch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * to deal with single stepping from userspace which stops on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * the second instruction. Thus we need to patch the second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * instruction of the exception, not the first one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) patch_branch((struct ppc_inst *)(ibase + (exc / 4) + 1), addr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) #ifdef CONFIG_CODE_PATCHING_SELFTEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static void __init test_trampoline(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) asm ("nop;\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) #define check(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static void __init test_branch_iform(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct ppc_inst instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) addr = (unsigned long)&instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /* The simplest case, branch to self, no flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) check(instr_is_branch_iform(ppc_inst(0x48000000)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /* All bits of target set, and flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /* High bit of opcode set, which is wrong */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /* Middle bits of opcode set, which is wrong */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /* Simplest case, branch to self with link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) check(instr_is_branch_iform(ppc_inst(0x48000001)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /* All bits of targets set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /* Some bits of targets set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /* Must be a valid branch to start with */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) /* Absolute branch to 0x100 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) instr = ppc_inst(0x48000103);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) check(instr_is_branch_to_addr(&instr, 0x100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /* Absolute branch to 0x420fc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) instr = ppc_inst(0x480420ff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) check(instr_is_branch_to_addr(&instr, 0x420fc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /* Maximum positive relative branch, + 20MB - 4B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) instr = ppc_inst(0x49fffffc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /* Smallest negative relative branch, - 4B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) instr = ppc_inst(0x4bfffffc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) check(instr_is_branch_to_addr(&instr, addr - 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /* Largest negative relative branch, - 32 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) instr = ppc_inst(0x4a000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* Branch to self, with link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) check(instr_is_branch_to_addr(&instr, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /* Branch to self - 0x100, with link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) check(instr_is_branch_to_addr(&instr, addr - 0x100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /* Branch to self + 0x100, no link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) err = create_branch(&instr, &instr, addr + 0x100, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) check(instr_is_branch_to_addr(&instr, addr + 0x100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /* Maximum relative negative offset, - 32 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /* Out of range relative negative offset, - 32 MB + 4*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) check(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) /* Out of range relative positive offset, + 32 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) check(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* Unaligned target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) check(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /* Check flags are masked correctly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) err = create_branch(&instr, &instr, addr, 0xFFFFFFFC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) check(instr_is_branch_to_addr(&instr, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static void __init test_create_function_call(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct ppc_inst *iptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) unsigned long dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct ppc_inst instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /* Check we can create a function call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) iptr = (struct ppc_inst *)ppc_function_entry(test_trampoline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) dest = ppc_function_entry(test_create_function_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) patch_instruction(iptr, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) check(instr_is_branch_to_addr(iptr, dest));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static void __init test_branch_bform(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct ppc_inst *iptr, instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) iptr = &instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) addr = (unsigned long)iptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) /* The simplest case, branch to self, no flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) check(instr_is_branch_bform(ppc_inst(0x40000000)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /* All bits of target set, and flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) /* High bit of opcode set, which is wrong */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /* Middle bits of opcode set, which is wrong */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /* Absolute conditional branch to 0x100 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) instr = ppc_inst(0x43ff0103);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) check(instr_is_branch_to_addr(&instr, 0x100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /* Absolute conditional branch to 0x20fc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) instr = ppc_inst(0x43ff20ff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) check(instr_is_branch_to_addr(&instr, 0x20fc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /* Maximum positive relative conditional branch, + 32 KB - 4B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) instr = ppc_inst(0x43ff7ffc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /* Smallest negative relative conditional branch, - 4B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) instr = ppc_inst(0x43fffffc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) check(instr_is_branch_to_addr(&instr, addr - 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /* Largest negative relative conditional branch, - 32 KB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) instr = ppc_inst(0x43ff8000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) check(instr_is_branch_to_addr(&instr, addr - 0x8000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /* All condition code bits set & link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) flags = 0x3ff000 | BRANCH_SET_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /* Branch to self */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) err = create_cond_branch(&instr, iptr, addr, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) check(instr_is_branch_to_addr(&instr, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /* Branch to self - 0x100 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) check(instr_is_branch_to_addr(&instr, addr - 0x100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) /* Branch to self + 0x100 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) check(instr_is_branch_to_addr(&instr, addr + 0x100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* Maximum relative negative offset, - 32 KB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) check(instr_is_branch_to_addr(&instr, addr - 0x8000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) /* Out of range relative negative offset, - 32 KB + 4*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) check(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /* Out of range relative positive offset, + 32 KB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) check(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) /* Unaligned target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) err = create_cond_branch(&instr, iptr, addr + 3, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) check(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /* Check flags are masked correctly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) check(instr_is_branch_to_addr(&instr, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) static void __init test_translate_branch(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) void *p, *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) struct ppc_inst instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) check(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) /* Simple case, branch to self moved a little */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) addr = (unsigned long)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) patch_branch(p, addr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) q = p + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) /* Maximum negative case, move b . to addr + 32 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) addr = (unsigned long)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) patch_branch(p, addr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) q = buf + 0x2000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) /* Maximum positive case, move x to x - 32 MB + 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) p = buf + 0x2000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) addr = (unsigned long)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) patch_branch(p, addr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) q = buf + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) /* Jump to x + 16 MB moved to x + 20 MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) addr = 0x1000000 + (unsigned long)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) patch_branch(p, addr, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) q = buf + 0x1400000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) /* Jump to x + 16 MB moved to x - 16 MB + 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) p = buf + 0x1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) addr = 0x2000000 + (unsigned long)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) patch_branch(p, addr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) q = buf + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) /* Conditional branch tests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) /* Simple case, branch to self moved a little */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) addr = (unsigned long)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) create_cond_branch(&instr, p, addr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) patch_instruction(p, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) q = buf + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) /* Maximum negative case, move b . to addr + 32 KB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) addr = (unsigned long)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) patch_instruction(p, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) q = buf + 0x8000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) /* Maximum positive case, move x to x - 32 KB + 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) p = buf + 0x8000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) addr = (unsigned long)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) patch_instruction(p, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) q = buf + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) /* Jump to x + 12 KB moved to x + 20 KB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) addr = 0x3000 + (unsigned long)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) patch_instruction(p, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) q = buf + 0x5000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) /* Jump to x + 8 KB moved to x - 8 KB + 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) p = buf + 0x2000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) addr = 0x4000 + (unsigned long)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) create_cond_branch(&instr, p, addr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) patch_instruction(p, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) q = buf + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) translate_branch(&instr, q, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) patch_instruction(q, instr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) check(instr_is_branch_to_addr(p, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) check(instr_is_branch_to_addr(q, addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) /* Free the buffer we were using */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) vfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) #ifdef CONFIG_PPC64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static void __init test_prefixed_patching(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) extern unsigned int code_patching_test1[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) extern unsigned int code_patching_test1_expected[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) extern unsigned int end_code_patching_test1[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) __patch_instruction((struct ppc_inst *)code_patching_test1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) ppc_inst_prefix(OP_PREFIX << 26, 0x00000000),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) (struct ppc_inst *)code_patching_test1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) check(!memcmp(code_patching_test1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) code_patching_test1_expected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) sizeof(unsigned int) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) (end_code_patching_test1 - code_patching_test1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) static inline void test_prefixed_patching(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) static int __init test_code_patching(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) printk(KERN_DEBUG "Running code patching self-tests ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) test_branch_iform();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) test_branch_bform();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) test_create_function_call();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) test_translate_branch();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) test_prefixed_patching();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) late_initcall(test_code_patching);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) #endif /* CONFIG_CODE_PATCHING_SELFTEST */