Jordan Niethe | 7534625 | 2020-05-06 13:40:26 +1000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | #ifndef _ASM_POWERPC_INST_H |
| 3 | #define _ASM_POWERPC_INST_H |
| 4 | |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 5 | #include <asm/ppc-opcode.h> |
Christophe Leroy | 9b30757 | 2021-11-29 18:49:40 +0100 | [diff] [blame] | 6 | #include <asm/reg.h> |
Christophe Leroy | 0d76914 | 2021-11-29 18:49:41 +0100 | [diff] [blame] | 7 | #include <asm/disassemble.h> |
| 8 | #include <asm/uaccess.h> |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 9 | |
Christophe Leroy | 35506a3 | 2021-03-10 17:46:44 +0000 | [diff] [blame] | 10 | #define ___get_user_instr(gu_op, dest, ptr) \ |
| 11 | ({ \ |
Christophe Leroy | 042e086 | 2021-05-20 13:50:40 +0000 | [diff] [blame] | 12 | long __gui_ret; \ |
Christophe Leroy | 9134806 | 2021-05-20 13:50:39 +0000 | [diff] [blame] | 13 | u32 __user *__gui_ptr = (u32 __user *)ptr; \ |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 14 | ppc_inst_t __gui_inst; \ |
Christophe Leroy | 35506a3 | 2021-03-10 17:46:44 +0000 | [diff] [blame] | 15 | unsigned int __prefix, __suffix; \ |
Christophe Leroy | b3a9e52 | 2021-05-20 13:50:38 +0000 | [diff] [blame] | 16 | \ |
| 17 | __chk_user_ptr(ptr); \ |
Christophe Leroy | 9134806 | 2021-05-20 13:50:39 +0000 | [diff] [blame] | 18 | __gui_ret = gu_op(__prefix, __gui_ptr); \ |
Christophe Leroy | 35506a3 | 2021-03-10 17:46:44 +0000 | [diff] [blame] | 19 | if (__gui_ret == 0) { \ |
Christophe Leroy | 3261d99 | 2021-11-29 18:49:37 +0100 | [diff] [blame] | 20 | if (IS_ENABLED(CONFIG_PPC64) && (__prefix >> 26) == OP_PREFIX) { \ |
Christophe Leroy | 9134806 | 2021-05-20 13:50:39 +0000 | [diff] [blame] | 21 | __gui_ret = gu_op(__suffix, __gui_ptr + 1); \ |
Christophe Leroy | 042e086 | 2021-05-20 13:50:40 +0000 | [diff] [blame] | 22 | __gui_inst = ppc_inst_prefix(__prefix, __suffix); \ |
Christophe Leroy | 35506a3 | 2021-03-10 17:46:44 +0000 | [diff] [blame] | 23 | } else { \ |
| 24 | __gui_inst = ppc_inst(__prefix); \ |
| 25 | } \ |
| 26 | if (__gui_ret == 0) \ |
| 27 | (dest) = __gui_inst; \ |
| 28 | } \ |
| 29 | __gui_ret; \ |
| 30 | }) |
Christophe Leroy | 35506a3 | 2021-03-10 17:46:44 +0000 | [diff] [blame] | 31 | |
Christophe Leroy | 042e086 | 2021-05-20 13:50:40 +0000 | [diff] [blame] | 32 | #define get_user_instr(x, ptr) ___get_user_instr(get_user, x, ptr) |
Christophe Leroy | 35506a3 | 2021-03-10 17:46:44 +0000 | [diff] [blame] | 33 | |
Christophe Leroy | 042e086 | 2021-05-20 13:50:40 +0000 | [diff] [blame] | 34 | #define __get_user_instr(x, ptr) ___get_user_instr(__get_user, x, ptr) |
Christophe Leroy | 35506a3 | 2021-03-10 17:46:44 +0000 | [diff] [blame] | 35 | |
Jordan Niethe | 7534625 | 2020-05-06 13:40:26 +1000 | [diff] [blame] | 36 | /* |
| 37 | * Instruction data type for POWER |
| 38 | */ |
| 39 | |
Christophe Leroy | 07b863a | 2021-11-29 18:49:39 +0100 | [diff] [blame] | 40 | #if defined(CONFIG_PPC64) || defined(__CHECKER__) |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 41 | static inline u32 ppc_inst_val(ppc_inst_t x) |
Jordan Niethe | 777e26f | 2020-05-06 13:40:27 +1000 | [diff] [blame] | 42 | { |
Jordan Niethe | 94afd06 | 2020-05-06 13:40:31 +1000 | [diff] [blame] | 43 | return x.val; |
Jordan Niethe | 777e26f | 2020-05-06 13:40:27 +1000 | [diff] [blame] | 44 | } |
| 45 | |
Christophe Leroy | 07b863a | 2021-11-29 18:49:39 +0100 | [diff] [blame] | 46 | #define ppc_inst(x) ((ppc_inst_t){ .val = (x) }) |
| 47 | |
| 48 | #else |
Christophe Leroy | 07b863a | 2021-11-29 18:49:39 +0100 | [diff] [blame] | 49 | static inline u32 ppc_inst_val(ppc_inst_t x) |
| 50 | { |
| 51 | return x; |
| 52 | } |
| 53 | #define ppc_inst(x) (x) |
| 54 | #endif |
| 55 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 56 | static inline int ppc_inst_primary_opcode(ppc_inst_t x) |
Jordan Niethe | 8094892 | 2020-05-06 13:40:28 +1000 | [diff] [blame] | 57 | { |
| 58 | return ppc_inst_val(x) >> 26; |
| 59 | } |
| 60 | |
Christophe Leroy | 077c4de | 2021-05-20 13:50:46 +0000 | [diff] [blame] | 61 | #ifdef CONFIG_PPC64 |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 62 | #define ppc_inst_prefix(x, y) ((ppc_inst_t){ .val = (x), .suffix = (y) }) |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 63 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 64 | static inline u32 ppc_inst_suffix(ppc_inst_t x) |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 65 | { |
| 66 | return x.suffix; |
| 67 | } |
| 68 | |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 69 | #else |
Christophe Leroy | 3261d99 | 2021-11-29 18:49:37 +0100 | [diff] [blame] | 70 | #define ppc_inst_prefix(x, y) ((void)y, ppc_inst(x)) |
Christophe Leroy | 6449078 | 2021-04-14 13:08:41 +0000 | [diff] [blame] | 71 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 72 | static inline u32 ppc_inst_suffix(ppc_inst_t x) |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 73 | { |
| 74 | return 0; |
| 75 | } |
| 76 | |
Christophe Leroy | 077c4de | 2021-05-20 13:50:46 +0000 | [diff] [blame] | 77 | #endif /* CONFIG_PPC64 */ |
Jordan Niethe | aabd223 | 2020-05-06 13:40:29 +1000 | [diff] [blame] | 78 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 79 | static inline ppc_inst_t ppc_inst_read(const u32 *ptr) |
Jordan Niethe | f8faaff | 2020-05-06 13:40:32 +1000 | [diff] [blame] | 80 | { |
Christophe Leroy | 077c4de | 2021-05-20 13:50:46 +0000 | [diff] [blame] | 81 | if (IS_ENABLED(CONFIG_PPC64) && (*ptr >> 26) == OP_PREFIX) |
| 82 | return ppc_inst_prefix(*ptr, *(ptr + 1)); |
| 83 | else |
| 84 | return ppc_inst(*ptr); |
Jordan Niethe | f8faaff | 2020-05-06 13:40:32 +1000 | [diff] [blame] | 85 | } |
| 86 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 87 | static inline bool ppc_inst_prefixed(ppc_inst_t x) |
Christophe Leroy | 077c4de | 2021-05-20 13:50:46 +0000 | [diff] [blame] | 88 | { |
| 89 | return IS_ENABLED(CONFIG_PPC64) && ppc_inst_primary_opcode(x) == OP_PREFIX; |
| 90 | } |
| 91 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 92 | static inline ppc_inst_t ppc_inst_swab(ppc_inst_t x) |
Christophe Leroy | 077c4de | 2021-05-20 13:50:46 +0000 | [diff] [blame] | 93 | { |
| 94 | return ppc_inst_prefix(swab32(ppc_inst_val(x)), swab32(ppc_inst_suffix(x))); |
| 95 | } |
Christophe Leroy | 036b556 | 2021-05-20 13:50:41 +0000 | [diff] [blame] | 96 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 97 | static inline bool ppc_inst_equal(ppc_inst_t x, ppc_inst_t y) |
Jordan Niethe | 217862d | 2020-05-06 13:40:30 +1000 | [diff] [blame] | 98 | { |
Christophe Leroy | 036b556 | 2021-05-20 13:50:41 +0000 | [diff] [blame] | 99 | if (ppc_inst_val(x) != ppc_inst_val(y)) |
| 100 | return false; |
| 101 | if (!ppc_inst_prefixed(x)) |
| 102 | return true; |
| 103 | return ppc_inst_suffix(x) == ppc_inst_suffix(y); |
Jordan Niethe | 217862d | 2020-05-06 13:40:30 +1000 | [diff] [blame] | 104 | } |
| 105 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 106 | static inline int ppc_inst_len(ppc_inst_t x) |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 107 | { |
| 108 | return ppc_inst_prefixed(x) ? 8 : 4; |
| 109 | } |
| 110 | |
Michael Ellerman | c5ff46d | 2020-05-22 23:33:18 +1000 | [diff] [blame] | 111 | /* |
| 112 | * Return the address of the next instruction, if the instruction @value was |
| 113 | * located at @location. |
| 114 | */ |
Christophe Leroy | 69d4d6e | 2021-05-20 13:50:45 +0000 | [diff] [blame] | 115 | static inline u32 *ppc_inst_next(u32 *location, u32 *value) |
Michael Ellerman | c5ff46d | 2020-05-22 23:33:18 +1000 | [diff] [blame] | 116 | { |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 117 | ppc_inst_t tmp; |
Michael Ellerman | c5ff46d | 2020-05-22 23:33:18 +1000 | [diff] [blame] | 118 | |
| 119 | tmp = ppc_inst_read(value); |
| 120 | |
Christophe Leroy | 69d4d6e | 2021-05-20 13:50:45 +0000 | [diff] [blame] | 121 | return (void *)location + ppc_inst_len(tmp); |
Michael Ellerman | c5ff46d | 2020-05-22 23:33:18 +1000 | [diff] [blame] | 122 | } |
| 123 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 124 | static inline unsigned long ppc_inst_as_ulong(ppc_inst_t x) |
Michael Ellerman | 16ef976 | 2020-05-26 17:26:30 +1000 | [diff] [blame] | 125 | { |
Christophe Leroy | 693557e | 2021-04-20 14:02:06 +0000 | [diff] [blame] | 126 | if (IS_ENABLED(CONFIG_PPC32)) |
| 127 | return ppc_inst_val(x); |
| 128 | else if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN)) |
| 129 | return (u64)ppc_inst_suffix(x) << 32 | ppc_inst_val(x); |
| 130 | else |
| 131 | return (u64)ppc_inst_val(x) << 32 | ppc_inst_suffix(x); |
Michael Ellerman | 16ef976 | 2020-05-26 17:26:30 +1000 | [diff] [blame] | 132 | } |
| 133 | |
Christophe Leroy | 309a0a6 | 2021-12-02 13:00:27 +0100 | [diff] [blame] | 134 | static inline void ppc_inst_write(u32 *ptr, ppc_inst_t x) |
| 135 | { |
| 136 | if (!ppc_inst_prefixed(x)) |
| 137 | *ptr = ppc_inst_val(x); |
| 138 | else |
| 139 | *(u64 *)ptr = ppc_inst_as_ulong(x); |
| 140 | } |
| 141 | |
Jordan Niethe | 50428fd | 2020-06-02 15:27:25 +1000 | [diff] [blame] | 142 | #define PPC_INST_STR_LEN sizeof("00000000 00000000") |
| 143 | |
Christophe Leroy | c545b9f | 2021-11-29 18:49:38 +0100 | [diff] [blame] | 144 | static inline char *__ppc_inst_as_str(char str[PPC_INST_STR_LEN], ppc_inst_t x) |
Jordan Niethe | 50428fd | 2020-06-02 15:27:25 +1000 | [diff] [blame] | 145 | { |
| 146 | if (ppc_inst_prefixed(x)) |
| 147 | sprintf(str, "%08x %08x", ppc_inst_val(x), ppc_inst_suffix(x)); |
| 148 | else |
| 149 | sprintf(str, "%08x", ppc_inst_val(x)); |
| 150 | |
| 151 | return str; |
| 152 | } |
| 153 | |
| 154 | #define ppc_inst_as_str(x) \ |
| 155 | ({ \ |
| 156 | char __str[PPC_INST_STR_LEN]; \ |
| 157 | __ppc_inst_as_str(__str, x); \ |
| 158 | __str; \ |
| 159 | }) |
| 160 | |
Christophe Leroy | 0d76914 | 2021-11-29 18:49:41 +0100 | [diff] [blame] | 161 | static inline int copy_inst_from_kernel_nofault(ppc_inst_t *inst, u32 *src) |
| 162 | { |
| 163 | unsigned int val, suffix; |
| 164 | |
| 165 | if (unlikely(!is_kernel_addr((unsigned long)src))) |
| 166 | return -ERANGE; |
| 167 | |
| 168 | /* See https://github.com/ClangBuiltLinux/linux/issues/1521 */ |
| 169 | #if defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION < 140000 |
| 170 | val = suffix = 0; |
| 171 | #endif |
| 172 | __get_kernel_nofault(&val, src, u32, Efault); |
| 173 | if (IS_ENABLED(CONFIG_PPC64) && get_op(val) == OP_PREFIX) { |
| 174 | __get_kernel_nofault(&suffix, src + 1, u32, Efault); |
| 175 | *inst = ppc_inst_prefix(val, suffix); |
| 176 | } else { |
| 177 | *inst = ppc_inst(val); |
| 178 | } |
| 179 | return 0; |
| 180 | Efault: |
| 181 | return -EFAULT; |
| 182 | } |
Jordan Niethe | 95b980a | 2020-05-06 13:40:34 +1000 | [diff] [blame] | 183 | |
Jordan Niethe | 7534625 | 2020-05-06 13:40:26 +1000 | [diff] [blame] | 184 | #endif /* _ASM_POWERPC_INST_H */ |