0001
0002 #ifndef _ASM_POWERPC_INST_H
0003 #define _ASM_POWERPC_INST_H
0004
0005 #include <asm/ppc-opcode.h>
0006 #include <asm/reg.h>
0007 #include <asm/disassemble.h>
0008 #include <asm/uaccess.h>
0009
0010 #define ___get_user_instr(gu_op, dest, ptr) \
0011 ({ \
0012 long __gui_ret; \
0013 u32 __user *__gui_ptr = (u32 __user *)ptr; \
0014 ppc_inst_t __gui_inst; \
0015 unsigned int __prefix, __suffix; \
0016 \
0017 __chk_user_ptr(ptr); \
0018 __gui_ret = gu_op(__prefix, __gui_ptr); \
0019 if (__gui_ret == 0) { \
0020 if (IS_ENABLED(CONFIG_PPC64) && (__prefix >> 26) == OP_PREFIX) { \
0021 __gui_ret = gu_op(__suffix, __gui_ptr + 1); \
0022 __gui_inst = ppc_inst_prefix(__prefix, __suffix); \
0023 } else { \
0024 __gui_inst = ppc_inst(__prefix); \
0025 } \
0026 if (__gui_ret == 0) \
0027 (dest) = __gui_inst; \
0028 } \
0029 __gui_ret; \
0030 })
0031
0032 #define get_user_instr(x, ptr) ___get_user_instr(get_user, x, ptr)
0033
0034 #define __get_user_instr(x, ptr) ___get_user_instr(__get_user, x, ptr)
0035
0036
0037
0038
0039
0040 #if defined(CONFIG_PPC64) || defined(__CHECKER__)
0041 static inline u32 ppc_inst_val(ppc_inst_t x)
0042 {
0043 return x.val;
0044 }
0045
0046 #define ppc_inst(x) ((ppc_inst_t){ .val = (x) })
0047
0048 #else
0049 static inline u32 ppc_inst_val(ppc_inst_t x)
0050 {
0051 return x;
0052 }
0053 #define ppc_inst(x) (x)
0054 #endif
0055
0056 static inline int ppc_inst_primary_opcode(ppc_inst_t x)
0057 {
0058 return ppc_inst_val(x) >> 26;
0059 }
0060
0061 #ifdef CONFIG_PPC64
0062 #define ppc_inst_prefix(x, y) ((ppc_inst_t){ .val = (x), .suffix = (y) })
0063
0064 static inline u32 ppc_inst_suffix(ppc_inst_t x)
0065 {
0066 return x.suffix;
0067 }
0068
0069 #else
0070 #define ppc_inst_prefix(x, y) ((void)y, ppc_inst(x))
0071
0072 static inline u32 ppc_inst_suffix(ppc_inst_t x)
0073 {
0074 return 0;
0075 }
0076
0077 #endif
0078
0079 static inline ppc_inst_t ppc_inst_read(const u32 *ptr)
0080 {
0081 if (IS_ENABLED(CONFIG_PPC64) && (*ptr >> 26) == OP_PREFIX)
0082 return ppc_inst_prefix(*ptr, *(ptr + 1));
0083 else
0084 return ppc_inst(*ptr);
0085 }
0086
0087 static inline bool ppc_inst_prefixed(ppc_inst_t x)
0088 {
0089 return IS_ENABLED(CONFIG_PPC64) && ppc_inst_primary_opcode(x) == OP_PREFIX;
0090 }
0091
0092 static inline ppc_inst_t ppc_inst_swab(ppc_inst_t x)
0093 {
0094 return ppc_inst_prefix(swab32(ppc_inst_val(x)), swab32(ppc_inst_suffix(x)));
0095 }
0096
0097 static inline bool ppc_inst_equal(ppc_inst_t x, ppc_inst_t y)
0098 {
0099 if (ppc_inst_val(x) != ppc_inst_val(y))
0100 return false;
0101 if (!ppc_inst_prefixed(x))
0102 return true;
0103 return ppc_inst_suffix(x) == ppc_inst_suffix(y);
0104 }
0105
0106 static inline int ppc_inst_len(ppc_inst_t x)
0107 {
0108 return ppc_inst_prefixed(x) ? 8 : 4;
0109 }
0110
0111
0112
0113
0114
0115 static inline u32 *ppc_inst_next(u32 *location, u32 *value)
0116 {
0117 ppc_inst_t tmp;
0118
0119 tmp = ppc_inst_read(value);
0120
0121 return (void *)location + ppc_inst_len(tmp);
0122 }
0123
0124 static inline unsigned long ppc_inst_as_ulong(ppc_inst_t x)
0125 {
0126 if (IS_ENABLED(CONFIG_PPC32))
0127 return ppc_inst_val(x);
0128 else if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
0129 return (u64)ppc_inst_suffix(x) << 32 | ppc_inst_val(x);
0130 else
0131 return (u64)ppc_inst_val(x) << 32 | ppc_inst_suffix(x);
0132 }
0133
0134 static inline void ppc_inst_write(u32 *ptr, ppc_inst_t x)
0135 {
0136 if (!ppc_inst_prefixed(x))
0137 *ptr = ppc_inst_val(x);
0138 else
0139 *(u64 *)ptr = ppc_inst_as_ulong(x);
0140 }
0141
0142 static inline int __copy_inst_from_kernel_nofault(ppc_inst_t *inst, u32 *src)
0143 {
0144 unsigned int val, suffix;
0145
0146
0147 #if defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION < 140000
0148 val = suffix = 0;
0149 #endif
0150 __get_kernel_nofault(&val, src, u32, Efault);
0151 if (IS_ENABLED(CONFIG_PPC64) && get_op(val) == OP_PREFIX) {
0152 __get_kernel_nofault(&suffix, src + 1, u32, Efault);
0153 *inst = ppc_inst_prefix(val, suffix);
0154 } else {
0155 *inst = ppc_inst(val);
0156 }
0157 return 0;
0158 Efault:
0159 return -EFAULT;
0160 }
0161
0162 static inline int copy_inst_from_kernel_nofault(ppc_inst_t *inst, u32 *src)
0163 {
0164 if (unlikely(!is_kernel_addr((unsigned long)src)))
0165 return -ERANGE;
0166
0167 return __copy_inst_from_kernel_nofault(inst, src);
0168 }
0169
0170 #endif