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> |
| 6 | |
Jordan Niethe | 7534625 | 2020-05-06 13:40:26 +1000 | [diff] [blame] | 7 | /* |
| 8 | * Instruction data type for POWER |
| 9 | */ |
| 10 | |
Jordan Niethe | 94afd06 | 2020-05-06 13:40:31 +1000 | [diff] [blame] | 11 | struct ppc_inst { |
| 12 | u32 val; |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 13 | #ifdef CONFIG_PPC64 |
| 14 | u32 suffix; |
| 15 | #endif |
Jordan Niethe | 94afd06 | 2020-05-06 13:40:31 +1000 | [diff] [blame] | 16 | } __packed; |
Jordan Niethe | 7534625 | 2020-05-06 13:40:26 +1000 | [diff] [blame] | 17 | |
Jordan Niethe | 94afd06 | 2020-05-06 13:40:31 +1000 | [diff] [blame] | 18 | static inline u32 ppc_inst_val(struct ppc_inst x) |
Jordan Niethe | 777e26f | 2020-05-06 13:40:27 +1000 | [diff] [blame] | 19 | { |
Jordan Niethe | 94afd06 | 2020-05-06 13:40:31 +1000 | [diff] [blame] | 20 | return x.val; |
Jordan Niethe | 777e26f | 2020-05-06 13:40:27 +1000 | [diff] [blame] | 21 | } |
| 22 | |
Jordan Niethe | 94afd06 | 2020-05-06 13:40:31 +1000 | [diff] [blame] | 23 | static inline int ppc_inst_primary_opcode(struct ppc_inst x) |
Jordan Niethe | 8094892 | 2020-05-06 13:40:28 +1000 | [diff] [blame] | 24 | { |
| 25 | return ppc_inst_val(x) >> 26; |
| 26 | } |
| 27 | |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 28 | #ifdef CONFIG_PPC64 |
| 29 | #define ppc_inst(x) ((struct ppc_inst){ .val = (x), .suffix = 0xff }) |
| 30 | |
| 31 | #define ppc_inst_prefix(x, y) ((struct ppc_inst){ .val = (x), .suffix = (y) }) |
| 32 | |
| 33 | static inline u32 ppc_inst_suffix(struct ppc_inst x) |
| 34 | { |
| 35 | return x.suffix; |
| 36 | } |
| 37 | |
| 38 | static inline bool ppc_inst_prefixed(struct ppc_inst x) |
| 39 | { |
| 40 | return (ppc_inst_primary_opcode(x) == 1) && ppc_inst_suffix(x) != 0xff; |
| 41 | } |
| 42 | |
| 43 | static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x) |
| 44 | { |
| 45 | return ppc_inst_prefix(swab32(ppc_inst_val(x)), |
| 46 | swab32(ppc_inst_suffix(x))); |
| 47 | } |
| 48 | |
| 49 | static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr) |
| 50 | { |
| 51 | u32 val, suffix; |
| 52 | |
| 53 | val = *(u32 *)ptr; |
| 54 | if ((val >> 26) == OP_PREFIX) { |
| 55 | suffix = *((u32 *)ptr + 1); |
| 56 | return ppc_inst_prefix(val, suffix); |
| 57 | } else { |
| 58 | return ppc_inst(val); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y) |
| 63 | { |
| 64 | return *(u64 *)&x == *(u64 *)&y; |
| 65 | } |
| 66 | |
| 67 | #else |
| 68 | |
| 69 | #define ppc_inst(x) ((struct ppc_inst){ .val = x }) |
| 70 | |
| 71 | static inline bool ppc_inst_prefixed(struct ppc_inst x) |
| 72 | { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | static inline u32 ppc_inst_suffix(struct ppc_inst x) |
| 77 | { |
| 78 | return 0; |
| 79 | } |
| 80 | |
Jordan Niethe | 94afd06 | 2020-05-06 13:40:31 +1000 | [diff] [blame] | 81 | static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x) |
Jordan Niethe | aabd223 | 2020-05-06 13:40:29 +1000 | [diff] [blame] | 82 | { |
| 83 | return ppc_inst(swab32(ppc_inst_val(x))); |
| 84 | } |
| 85 | |
Jordan Niethe | f8faaff | 2020-05-06 13:40:32 +1000 | [diff] [blame] | 86 | static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr) |
| 87 | { |
| 88 | return *ptr; |
| 89 | } |
| 90 | |
Jordan Niethe | 94afd06 | 2020-05-06 13:40:31 +1000 | [diff] [blame] | 91 | static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y) |
Jordan Niethe | 217862d | 2020-05-06 13:40:30 +1000 | [diff] [blame] | 92 | { |
Jordan Niethe | 94afd06 | 2020-05-06 13:40:31 +1000 | [diff] [blame] | 93 | return ppc_inst_val(x) == ppc_inst_val(y); |
Jordan Niethe | 217862d | 2020-05-06 13:40:30 +1000 | [diff] [blame] | 94 | } |
| 95 | |
Jordan Niethe | 650b55b | 2020-05-15 12:12:55 +1000 | [diff] [blame] | 96 | #endif /* CONFIG_PPC64 */ |
| 97 | |
| 98 | static inline int ppc_inst_len(struct ppc_inst x) |
| 99 | { |
| 100 | return ppc_inst_prefixed(x) ? 8 : 4; |
| 101 | } |
| 102 | |
Michael Ellerman | c5ff46d | 2020-05-22 23:33:18 +1000 | [diff] [blame] | 103 | /* |
| 104 | * Return the address of the next instruction, if the instruction @value was |
| 105 | * located at @location. |
| 106 | */ |
| 107 | static inline struct ppc_inst *ppc_inst_next(void *location, struct ppc_inst *value) |
| 108 | { |
| 109 | struct ppc_inst tmp; |
| 110 | |
| 111 | tmp = ppc_inst_read(value); |
| 112 | |
| 113 | return location + ppc_inst_len(tmp); |
| 114 | } |
| 115 | |
Michael Ellerman | 16ef976 | 2020-05-26 17:26:30 +1000 | [diff] [blame] | 116 | static inline u64 ppc_inst_as_u64(struct ppc_inst x) |
| 117 | { |
| 118 | #ifdef CONFIG_CPU_LITTLE_ENDIAN |
| 119 | return (u64)ppc_inst_suffix(x) << 32 | ppc_inst_val(x); |
| 120 | #else |
| 121 | return (u64)ppc_inst_val(x) << 32 | ppc_inst_suffix(x); |
| 122 | #endif |
| 123 | } |
| 124 | |
Jordan Niethe | 50428fd | 2020-06-02 15:27:25 +1000 | [diff] [blame^] | 125 | #define PPC_INST_STR_LEN sizeof("00000000 00000000") |
| 126 | |
| 127 | static inline char *__ppc_inst_as_str(char str[PPC_INST_STR_LEN], struct ppc_inst x) |
| 128 | { |
| 129 | if (ppc_inst_prefixed(x)) |
| 130 | sprintf(str, "%08x %08x", ppc_inst_val(x), ppc_inst_suffix(x)); |
| 131 | else |
| 132 | sprintf(str, "%08x", ppc_inst_val(x)); |
| 133 | |
| 134 | return str; |
| 135 | } |
| 136 | |
| 137 | #define ppc_inst_as_str(x) \ |
| 138 | ({ \ |
| 139 | char __str[PPC_INST_STR_LEN]; \ |
| 140 | __ppc_inst_as_str(__str, x); \ |
| 141 | __str; \ |
| 142 | }) |
| 143 | |
Jordan Niethe | 7ba68b2 | 2020-05-06 13:40:33 +1000 | [diff] [blame] | 144 | int probe_user_read_inst(struct ppc_inst *inst, |
| 145 | struct ppc_inst __user *nip); |
| 146 | |
Jordan Niethe | 95b980a | 2020-05-06 13:40:34 +1000 | [diff] [blame] | 147 | int probe_kernel_read_inst(struct ppc_inst *inst, |
| 148 | struct ppc_inst *src); |
| 149 | |
Jordan Niethe | 7534625 | 2020-05-06 13:40:26 +1000 | [diff] [blame] | 150 | #endif /* _ASM_POWERPC_INST_H */ |