xref: /linux/arch/powerpc/include/asm/inst.h (revision 74205b3fc2effde821b219d955c70e727dc43cc6)
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 static inline bool ppc_inst_prefixed(struct ppc_inst x)
106 {
107 	return false;
108 }
109 
110 static inline u32 ppc_inst_suffix(struct ppc_inst x)
111 {
112 	return 0;
113 }
114 
115 static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x)
116 {
117 	return ppc_inst(swab32(ppc_inst_val(x)));
118 }
119 
120 static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr)
121 {
122 	return *ptr;
123 }
124 
125 static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y)
126 {
127 	return ppc_inst_val(x) == ppc_inst_val(y);
128 }
129 
130 #endif /* CONFIG_PPC64 */
131 
132 static inline int ppc_inst_len(struct ppc_inst x)
133 {
134 	return ppc_inst_prefixed(x) ? 8 : 4;
135 }
136 
137 /*
138  * Return the address of the next instruction, if the instruction @value was
139  * located at @location.
140  */
141 static inline struct ppc_inst *ppc_inst_next(void *location, struct ppc_inst *value)
142 {
143 	struct ppc_inst tmp;
144 
145 	tmp = ppc_inst_read(value);
146 
147 	return location + ppc_inst_len(tmp);
148 }
149 
150 static inline u64 ppc_inst_as_u64(struct ppc_inst x)
151 {
152 #ifdef CONFIG_CPU_LITTLE_ENDIAN
153 	return (u64)ppc_inst_suffix(x) << 32 | ppc_inst_val(x);
154 #else
155 	return (u64)ppc_inst_val(x) << 32 | ppc_inst_suffix(x);
156 #endif
157 }
158 
159 #define PPC_INST_STR_LEN sizeof("00000000 00000000")
160 
161 static inline char *__ppc_inst_as_str(char str[PPC_INST_STR_LEN], struct ppc_inst x)
162 {
163 	if (ppc_inst_prefixed(x))
164 		sprintf(str, "%08x %08x", ppc_inst_val(x), ppc_inst_suffix(x));
165 	else
166 		sprintf(str, "%08x", ppc_inst_val(x));
167 
168 	return str;
169 }
170 
171 #define ppc_inst_as_str(x)		\
172 ({					\
173 	char __str[PPC_INST_STR_LEN];	\
174 	__ppc_inst_as_str(__str, x);	\
175 	__str;				\
176 })
177 
178 int probe_user_read_inst(struct ppc_inst *inst,
179 			 struct ppc_inst __user *nip);
180 
181 int probe_kernel_read_inst(struct ppc_inst *inst,
182 			   struct ppc_inst *src);
183 
184 #endif /* _ASM_POWERPC_INST_H */
185