blob: fa8746c320067a51b50696207228a9140ed690a6 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002#ifndef _ASM_POWERPC_CODE_PATCHING_H
3#define _ASM_POWERPC_CODE_PATCHING_H
4
5/*
6 * Copyright 2008, Michael Ellerman, IBM Corporation.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#include <asm/types.h>
10#include <asm/ppc-opcode.h>
11#include <linux/string.h>
12#include <linux/kallsyms.h>
13#include <asm/asm-compat.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020014#include <asm/inst.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015
16/* Flags for create_branch:
17 * "b" == create_branch(addr, target, 0);
18 * "ba" == create_branch(addr, target, BRANCH_ABSOLUTE);
19 * "bl" == create_branch(addr, target, BRANCH_SET_LINK);
20 * "bla" == create_branch(addr, target, BRANCH_ABSOLUTE | BRANCH_SET_LINK);
21 */
22#define BRANCH_SET_LINK 0x1
23#define BRANCH_ABSOLUTE 0x2
24
25bool is_offset_in_branch_range(long offset);
Olivier Deprez157378f2022-04-04 15:47:50 +020026bool is_offset_in_cond_branch_range(long offset);
27int create_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
28 unsigned long target, int flags);
29int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
30 unsigned long target, int flags);
31int patch_branch(struct ppc_inst *addr, unsigned long target, int flags);
32int patch_instruction(struct ppc_inst *addr, struct ppc_inst instr);
33int raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr);
David Brazdil0f672f62019-12-10 10:32:29 +000034
35static inline unsigned long patch_site_addr(s32 *site)
36{
37 return (unsigned long)site + *site;
38}
39
Olivier Deprez157378f2022-04-04 15:47:50 +020040static inline int patch_instruction_site(s32 *site, struct ppc_inst instr)
David Brazdil0f672f62019-12-10 10:32:29 +000041{
Olivier Deprez157378f2022-04-04 15:47:50 +020042 return patch_instruction((struct ppc_inst *)patch_site_addr(site), instr);
David Brazdil0f672f62019-12-10 10:32:29 +000043}
44
45static inline int patch_branch_site(s32 *site, unsigned long target, int flags)
46{
Olivier Deprez157378f2022-04-04 15:47:50 +020047 return patch_branch((struct ppc_inst *)patch_site_addr(site), target, flags);
David Brazdil0f672f62019-12-10 10:32:29 +000048}
49
50static inline int modify_instruction(unsigned int *addr, unsigned int clr,
51 unsigned int set)
52{
Olivier Deprez157378f2022-04-04 15:47:50 +020053 return patch_instruction((struct ppc_inst *)addr, ppc_inst((*addr & ~clr) | set));
David Brazdil0f672f62019-12-10 10:32:29 +000054}
55
56static inline int modify_instruction_site(s32 *site, unsigned int clr, unsigned int set)
57{
58 return modify_instruction((unsigned int *)patch_site_addr(site), clr, set);
59}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060
Olivier Deprez157378f2022-04-04 15:47:50 +020061int instr_is_relative_branch(struct ppc_inst instr);
62int instr_is_relative_link_branch(struct ppc_inst instr);
63int instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr);
64unsigned long branch_target(const struct ppc_inst *instr);
65int translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest,
66 const struct ppc_inst *src);
67extern bool is_conditional_branch(struct ppc_inst instr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068#ifdef CONFIG_PPC_BOOK3E_64
69void __patch_exception(int exc, unsigned long addr);
70#define patch_exception(exc, name) do { \
71 extern unsigned int name; \
72 __patch_exception((exc), (unsigned long)&name); \
73} while (0)
74#endif
75
76#define OP_RT_RA_MASK 0xffff0000UL
Olivier Deprez0e641232021-09-23 10:07:05 +020077#define LIS_R2 0x3c400000UL
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078#define ADDIS_R2_R12 0x3c4c0000UL
79#define ADDI_R2_R2 0x38420000UL
80
81static inline unsigned long ppc_function_entry(void *func)
82{
83#ifdef PPC64_ELF_ABI_v2
84 u32 *insn = func;
85
86 /*
87 * A PPC64 ABIv2 function may have a local and a global entry
88 * point. We need to use the local entry point when patching
89 * functions, so identify and step over the global entry point
90 * sequence.
91 *
92 * The global entry point sequence is always of the form:
93 *
94 * addis r2,r12,XXXX
95 * addi r2,r2,XXXX
96 *
97 * A linker optimisation may convert the addis to lis:
98 *
99 * lis r2,XXXX
100 * addi r2,r2,XXXX
101 */
102 if ((((*insn & OP_RT_RA_MASK) == ADDIS_R2_R12) ||
103 ((*insn & OP_RT_RA_MASK) == LIS_R2)) &&
104 ((*(insn+1) & OP_RT_RA_MASK) == ADDI_R2_R2))
105 return (unsigned long)(insn + 2);
106 else
107 return (unsigned long)func;
108#elif defined(PPC64_ELF_ABI_v1)
109 /*
110 * On PPC64 ABIv1 the function pointer actually points to the
111 * function's descriptor. The first entry in the descriptor is the
112 * address of the function text.
113 */
114 return ((func_descr_t *)func)->entry;
115#else
116 return (unsigned long)func;
117#endif
118}
119
120static inline unsigned long ppc_global_function_entry(void *func)
121{
122#ifdef PPC64_ELF_ABI_v2
123 /* PPC64 ABIv2 the global entry point is at the address */
124 return (unsigned long)func;
125#else
126 /* All other cases there is no change vs ppc_function_entry() */
127 return ppc_function_entry(func);
128#endif
129}
130
131/*
132 * Wrapper around kallsyms_lookup() to return function entry address:
133 * - For ABIv1, we lookup the dot variant.
134 * - For ABIv2, we return the local entry point.
135 */
136static inline unsigned long ppc_kallsyms_lookup_name(const char *name)
137{
138 unsigned long addr;
139#ifdef PPC64_ELF_ABI_v1
140 /* check for dot variant */
141 char dot_name[1 + KSYM_NAME_LEN];
142 bool dot_appended = false;
143
144 if (strnlen(name, KSYM_NAME_LEN) >= KSYM_NAME_LEN)
145 return 0;
146
147 if (name[0] != '.') {
148 dot_name[0] = '.';
149 dot_name[1] = '\0';
150 strlcat(dot_name, name, sizeof(dot_name));
151 dot_appended = true;
152 } else {
153 dot_name[0] = '\0';
154 strlcat(dot_name, name, sizeof(dot_name));
155 }
156 addr = kallsyms_lookup_name(dot_name);
157 if (!addr && dot_appended)
158 /* Let's try the original non-dot symbol lookup */
159 addr = kallsyms_lookup_name(name);
160#elif defined(PPC64_ELF_ABI_v2)
161 addr = kallsyms_lookup_name(name);
162 if (addr)
163 addr = ppc_function_entry((void *)addr);
164#else
165 addr = kallsyms_lookup_name(name);
166#endif
167 return addr;
168}
169
170#ifdef CONFIG_PPC64
171/*
172 * Some instruction encodings commonly used in dynamic ftracing
173 * and function live patching.
174 */
175
176/* This must match the definition of STK_GOT in <asm/ppc_asm.h> */
177#ifdef PPC64_ELF_ABI_v2
178#define R2_STACK_OFFSET 24
179#else
180#define R2_STACK_OFFSET 40
181#endif
182
183#define PPC_INST_LD_TOC (PPC_INST_LD | ___PPC_RT(__REG_R2) | \
184 ___PPC_RA(__REG_R1) | R2_STACK_OFFSET)
185
186/* usually preceded by a mflr r0 */
187#define PPC_INST_STD_LR (PPC_INST_STD | ___PPC_RS(__REG_R0) | \
188 ___PPC_RA(__REG_R1) | PPC_LR_STKOFF)
189#endif /* CONFIG_PPC64 */
190
191#endif /* _ASM_POWERPC_CODE_PATCHING_H */