1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 #ifndef _ASM_POWERPC_INST_H 3 #define _ASM_POWERPC_INST_H 4 5 #include <asm/ppc-opcode.h> 6 7 #ifdef CONFIG_PPC64 8 9 #define ___get_user_instr(gu_op, dest, ptr) \ 10 ({ \ 11 long __gui_ret = 0; \ 12 unsigned long __gui_ptr = (unsigned long)ptr; \ 13 struct ppc_inst __gui_inst; \ 14 unsigned int __prefix, __suffix; \ 15 __gui_ret = gu_op(__prefix, (unsigned int __user *)__gui_ptr); \ 16 if (__gui_ret == 0) { \ 17 if ((__prefix >> 26) == OP_PREFIX) { \ 18 __gui_ret = gu_op(__suffix, \ 19 (unsigned int __user *)__gui_ptr + 1); \ 20 __gui_inst = ppc_inst_prefix(__prefix, \ 21 __suffix); \ 22 } else { \ 23 __gui_inst = ppc_inst(__prefix); \ 24 } \ 25 if (__gui_ret == 0) \ 26 (dest) = __gui_inst; \ 27 } \ 28 __gui_ret; \ 29 }) 30 #else /* !CONFIG_PPC64 */ 31 #define ___get_user_instr(gu_op, dest, ptr) \ 32 gu_op((dest).val, (u32 __user *)(ptr)) 33 #endif /* CONFIG_PPC64 */ 34 35 #define get_user_instr(x, ptr) \ 36 ___get_user_instr(get_user, x, ptr) 37 38 #define __get_user_instr(x, ptr) \ 39 ___get_user_instr(__get_user, x, ptr) 40 41 /* 42 * Instruction data type for POWER 43 */ 44 45 struct ppc_inst { 46 u32 val; 47 #ifdef CONFIG_PPC64 48 u32 suffix; 49 #endif 50 } __packed; 51 52 static inline u32 ppc_inst_val(struct ppc_inst x) 53 { 54 return x.val; 55 } 56 57 static inline int ppc_inst_primary_opcode(struct ppc_inst x) 58 { 59 return ppc_inst_val(x) >> 26; 60 } 61 62 #ifdef CONFIG_PPC64 63 #define ppc_inst(x) ((struct ppc_inst){ .val = (x), .suffix = 0xff }) 64 65 #define ppc_inst_prefix(x, y) ((struct ppc_inst){ .val = (x), .suffix = (y) }) 66 67 static inline u32 ppc_inst_suffix(struct ppc_inst x) 68 { 69 return x.suffix; 70 } 71 72 static inline bool ppc_inst_prefixed(struct ppc_inst x) 73 { 74 return (ppc_inst_primary_opcode(x) == 1) && ppc_inst_suffix(x) != 0xff; 75 } 76 77 static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x) 78 { 79 return ppc_inst_prefix(swab32(ppc_inst_val(x)), 80 swab32(ppc_inst_suffix(x))); 81 } 82 83 static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr) 84 { 85 u32 val, suffix; 86 87 val = *(u32 *)ptr; 88 if ((val >> 26) == OP_PREFIX) { 89 suffix = *((u32 *)ptr + 1); 90 return ppc_inst_prefix(val, suffix); 91 } else { 92 return ppc_inst(val); 93 } 94 } 95 96 static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y) 97 { 98 return *(u64 *)&x == *(u64 *)&y; 99 } 100 101 #else 102 103 #define ppc_inst(x) ((struct ppc_inst){ .val = x }) 104 105 #define ppc_inst_prefix(x, y) ppc_inst(x) 106 107 static inline bool ppc_inst_prefixed(struct ppc_inst x) 108 { 109 return false; 110 } 111 112 static inline u32 ppc_inst_suffix(struct ppc_inst x) 113 { 114 return 0; 115 } 116 117 static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x) 118 { 119 return ppc_inst(swab32(ppc_inst_val(x))); 120 } 121 122 static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr) 123 { 124 return *ptr; 125 } 126 127 static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y) 128 { 129 return ppc_inst_val(x) == ppc_inst_val(y); 130 } 131 132 #endif /* CONFIG_PPC64 */ 133 134 static inline int ppc_inst_len(struct ppc_inst x) 135 { 136 return ppc_inst_prefixed(x) ? 8 : 4; 137 } 138 139 /* 140 * Return the address of the next instruction, if the instruction @value was 141 * located at @location. 142 */ 143 static inline struct ppc_inst *ppc_inst_next(void *location, struct ppc_inst *value) 144 { 145 struct ppc_inst tmp; 146 147 tmp = ppc_inst_read(value); 148 149 return location + ppc_inst_len(tmp); 150 } 151 152 static inline unsigned long ppc_inst_as_ulong(struct ppc_inst x) 153 { 154 if (IS_ENABLED(CONFIG_PPC32)) 155 return ppc_inst_val(x); 156 else if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN)) 157 return (u64)ppc_inst_suffix(x) << 32 | ppc_inst_val(x); 158 else 159 return (u64)ppc_inst_val(x) << 32 | ppc_inst_suffix(x); 160 } 161 162 #define PPC_INST_STR_LEN sizeof("00000000 00000000") 163 164 static inline char *__ppc_inst_as_str(char str[PPC_INST_STR_LEN], struct ppc_inst x) 165 { 166 if (ppc_inst_prefixed(x)) 167 sprintf(str, "%08x %08x", ppc_inst_val(x), ppc_inst_suffix(x)); 168 else 169 sprintf(str, "%08x", ppc_inst_val(x)); 170 171 return str; 172 } 173 174 #define ppc_inst_as_str(x) \ 175 ({ \ 176 char __str[PPC_INST_STR_LEN]; \ 177 __ppc_inst_as_str(__str, x); \ 178 __str; \ 179 }) 180 181 int copy_inst_from_kernel_nofault(struct ppc_inst *inst, struct ppc_inst *src); 182 183 #endif /* _ASM_POWERPC_INST_H */ 184