GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — fgetpriority_NEXT/kernel/arm64_branch_patch.h at master · CKAndroidProject/fgetpriority_NEXT Expand file tree/ Copy patharm64_branch_patch.hMore file actions 162 lines (136 loc) · 5.34 KB / Copy patharm64_branch_patch.hFile metadata and controls162 lines (136 loc) · 5.34 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2026 \xx * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * */ #ifndef __H_ARM64_BRANCH_PATCH #define __H_ARM64_BRANCH_PATCH #include <linux/version.h> #include <linux/uaccess.h> #include <asm/insn.h> #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 13, 0) #include <asm/text-patching.h> #elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0) #include <asm/patching.h> #endif #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0) || defined(MODULE) __weak s32 aarch64_get_branch_offset(u32 insn) { // 26-bit signed relative jump offset // FC, D, E, F, so 3 s32 imm26 = insn & 0x03FFFFFF; // in case of backward jumps if (imm26 & 0x02000000) imm26 |= 0xFC000000; // 4-byte word count to byte delta return imm26 * 4; } #endif /** * arm64_branch_patch(): hunt and patch all b or bl insn found within target * * @target_callsite: callsite on where to start scanning for (ptr / fn_ptr) * @target_width: how far to scan for (bytes / ptrdiff) * @symbol_addr: symbol being called to look for at the site (fn_ptr) * @hook_addr: symbol to redirect "b symbol_addr" to (fn_ptr) * @type: branch type to look for. 1: b, 2: bl, 3: b or bl * * CONTEXT: * - do NOT call inside atomic context! (aarch64_insn_patch_text calls stop_machine) * - safe to run early or within kernel threads. * * NOTES: * - both symbol_addr and hook_addr must be inside +/-128MB ptrdiff to callsite! else ret -ERANGE * - if target bl not found within target_width, ret -ENOENT * - returns 0 on success. * - preserves instruction type (e.g., 'BL' becomes a 'BL' to the hook, and 'B' becomes a 'B' to the hook). * */ #define BRANCH_TYPE_B 1 #define BRANCH_TYPE_BL 2 #define BRANCH_TYPE_B_OR_BL 3 nodiscard static noinline int arm64_branch_patch(uintptr_t target_callsite, ptrdiff_t target_width, uintptr_t symbol_addr, uintptr_t hook_addr, uint8_t type) { if (!target_callsite || !symbol_addr) { pr_info("%s: no callsite or symbol addr specified!\n", __func__); return -EINVAL; } might_sleep(); constexpr ptrdiff_t bl_max_delta = (1L << 25) * sizeof(uint32_t); // 26 bits signed * insn size uintptr_t start_addr = (uintptr_t)target_callsite; uintptr_t end_addr = start_addr + target_width; ptrdiff_t hook_to_start = (ptrdiff_t)hook_addr - (ptrdiff_t)start_addr; ptrdiff_t hook_to_end = (ptrdiff_t)hook_addr - (ptrdiff_t)end_addr; if (__builtin_labs(hook_to_start) >= bl_max_delta || __builtin_labs(hook_to_end) >= bl_max_delta) { pr_info("%s: scan window start: 0x%lx end: 0x%lx out of range to hook: 0x%lx!\n", __func__, start_addr, end_addr, hook_addr); return -ERANGE; } // pr_info("%s: scan window start: 0x%lx end: 0x%lx in range to hook: 0x%lx deltas: %ld / %ld \n", __func__, start_addr, end_addr, hook_addr, hook_to_start, hook_to_end); uintptr_t curr_addr = start_addr; uint32_t raw_instruction; // arm64 wordsize unsigned int patched = 0; start_scan: if (curr_addr >= end_addr) goto bail; if (!!copy_from_kernel_nofault(&raw_instruction, (void *)curr_addr, sizeof(uint32_t))) goto step_up; // aarch64_insn_is_##abbr, arch/arm64/include/asm/insn.h switch (type) { case BRANCH_TYPE_BL: if (!aarch64_insn_is_bl(raw_instruction)) goto step_up; break; case BRANCH_TYPE_B: if (!aarch64_insn_is_b(raw_instruction)) goto step_up; break; case BRANCH_TYPE_B_OR_BL: if (!aarch64_insn_is_bl(raw_instruction) && !aarch64_insn_is_b(raw_instruction)) goto step_up; break; default: return -EINVAL; } // signed long offset = aarch64_get_branch_offset(raw_instruction); uintptr_t calculated_destination = curr_addr + offset; if (calculated_destination != symbol_addr) goto step_up; pr_info("%s: found call site at 0x%lx w/ .inst 0x%x \n", __func__, curr_addr, raw_instruction); ptrdiff_t delta = hook_addr - curr_addr; if (__builtin_labs(delta) >= bl_max_delta) { __builtin_unreachable(); // see range check above pr_info("%s: callsite 0x%lx to hook 0x%lx out of range! (delta: %ld bytes)\n", __func__, curr_addr, hook_addr, delta); return -ERANGE; } pr_info("%s: callsite 0x%lx to hook 0x%lx inside range! (delta: %ld bytes)\n", __func__, curr_addr, hook_addr, delta); uint32_t insn; if (aarch64_insn_is_bl(raw_instruction)) insn = aarch64_insn_gen_branch_imm(curr_addr, hook_addr, AARCH64_INSN_BRANCH_LINK); else insn = aarch64_insn_gen_branch_imm(curr_addr, hook_addr, AARCH64_INSN_BRANCH_NOLINK); void *arr_addr[] = { (void*)curr_addr }; uint32_t arr_insn[] = { insn }; int res = aarch64_insn_patch_text(arr_addr, arr_insn, 1); pr_info("%s: patched callsite: 0x%lx ret: %d\n", __func__, curr_addr, res); patched++; step_up: curr_addr = curr_addr + sizeof(uint32_t); goto start_scan; bail: if (!patched) { pr_info("%s: callsite instruction not found!\n", __func__); return -ENOENT; } if (patched > 1) pr_info("%s: patch count: %d\n", __func__, patched); return 0; } #define arm64_b_patch(tgt, width, symaddr, hookaddr) arm64_branch_patch(tgt, width, symaddr, hookaddr, BRANCH_TYPE_B) #define arm64_bl_patch(tgt, width, symaddr, hookaddr) arm64_branch_patch(tgt, width, symaddr, hookaddr, BRANCH_TYPE_BL) #define arm64_b_or_bl_patch(tgt, width, symaddr, hookaddr) arm64_branch_patch(tgt, width, symaddr, hookaddr, BRANCH_TYPE_B_OR_BL) #endif // __H_ARM64_BRANCH_PATCH |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |