1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _ASM_POWERPC_INST_H
3 #define _ASM_POWERPC_INST_H
5 #include <asm/ppc-opcode.h>
9 #define ___get_user_instr(gu_op, dest, ptr) \
12 u32 __user *__gui_ptr = (u32 __user *)ptr; \
13 struct ppc_inst __gui_inst; \
14 unsigned int __prefix, __suffix; \
16 __chk_user_ptr(ptr); \
17 __gui_ret = gu_op(__prefix, __gui_ptr); \
18 if (__gui_ret == 0) { \
19 if ((__prefix >> 26) == OP_PREFIX) { \
20 __gui_ret = gu_op(__suffix, __gui_ptr + 1); \
21 __gui_inst = ppc_inst_prefix(__prefix, \
24 __gui_inst = ppc_inst(__prefix); \
27 (dest) = __gui_inst; \
31 #else /* !CONFIG_PPC64 */
32 #define ___get_user_instr(gu_op, dest, ptr) \
34 __chk_user_ptr(ptr); \
35 gu_op((dest).val, (u32 __user *)(ptr)); \
37 #endif /* CONFIG_PPC64 */
39 #define get_user_instr(x, ptr) \
40 ___get_user_instr(get_user, x, ptr)
42 #define __get_user_instr(x, ptr) \
43 ___get_user_instr(__get_user, x, ptr)
46 * Instruction data type for POWER
56 static inline u32 ppc_inst_val(struct ppc_inst x)
61 static inline int ppc_inst_primary_opcode(struct ppc_inst x)
63 return ppc_inst_val(x) >> 26;
67 #define ppc_inst(x) ((struct ppc_inst){ .val = (x), .suffix = 0xff })
69 #define ppc_inst_prefix(x, y) ((struct ppc_inst){ .val = (x), .suffix = (y) })
71 static inline u32 ppc_inst_suffix(struct ppc_inst x)
76 static inline bool ppc_inst_prefixed(struct ppc_inst x)
78 return (ppc_inst_primary_opcode(x) == 1) && ppc_inst_suffix(x) != 0xff;
81 static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x)
83 return ppc_inst_prefix(swab32(ppc_inst_val(x)),
84 swab32(ppc_inst_suffix(x)));
87 static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr)
92 if ((val >> 26) == OP_PREFIX) {
93 suffix = *((u32 *)ptr + 1);
94 return ppc_inst_prefix(val, suffix);
100 static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y)
102 return *(u64 *)&x == *(u64 *)&y;
107 #define ppc_inst(x) ((struct ppc_inst){ .val = x })
109 #define ppc_inst_prefix(x, y) ppc_inst(x)
111 static inline bool ppc_inst_prefixed(struct ppc_inst x)
116 static inline u32 ppc_inst_suffix(struct ppc_inst x)
121 static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x)
123 return ppc_inst(swab32(ppc_inst_val(x)));
126 static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr)
131 static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y)
133 return ppc_inst_val(x) == ppc_inst_val(y);
136 #endif /* CONFIG_PPC64 */
138 static inline int ppc_inst_len(struct ppc_inst x)
140 return ppc_inst_prefixed(x) ? 8 : 4;
144 * Return the address of the next instruction, if the instruction @value was
145 * located at @location.
147 static inline struct ppc_inst *ppc_inst_next(void *location, struct ppc_inst *value)
151 tmp = ppc_inst_read(value);
153 return location + ppc_inst_len(tmp);
156 static inline unsigned long ppc_inst_as_ulong(struct ppc_inst x)
158 if (IS_ENABLED(CONFIG_PPC32))
159 return ppc_inst_val(x);
160 else if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
161 return (u64)ppc_inst_suffix(x) << 32 | ppc_inst_val(x);
163 return (u64)ppc_inst_val(x) << 32 | ppc_inst_suffix(x);
166 #define PPC_INST_STR_LEN sizeof("00000000 00000000")
168 static inline char *__ppc_inst_as_str(char str[PPC_INST_STR_LEN], struct ppc_inst x)
170 if (ppc_inst_prefixed(x))
171 sprintf(str, "%08x %08x", ppc_inst_val(x), ppc_inst_suffix(x));
173 sprintf(str, "%08x", ppc_inst_val(x));
178 #define ppc_inst_as_str(x) \
180 char __str[PPC_INST_STR_LEN]; \
181 __ppc_inst_as_str(__str, x); \
185 int copy_inst_from_kernel_nofault(struct ppc_inst *inst, struct ppc_inst *src);
187 #endif /* _ASM_POWERPC_INST_H */