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 /*
8 * Instruction data type for POWER
9 */
10
11 struct ppc_inst {
12 u32 val;
13 #ifdef CONFIG_PPC64
14 u32 suffix;
15 #endif
16 } __packed;
17
ppc_inst_val(struct ppc_inst x)18 static inline u32 ppc_inst_val(struct ppc_inst x)
19 {
20 return x.val;
21 }
22
ppc_inst_primary_opcode(struct ppc_inst x)23 static inline int ppc_inst_primary_opcode(struct ppc_inst x)
24 {
25 return ppc_inst_val(x) >> 26;
26 }
27
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
ppc_inst_suffix(struct ppc_inst x)33 static inline u32 ppc_inst_suffix(struct ppc_inst x)
34 {
35 return x.suffix;
36 }
37
ppc_inst_prefixed(struct ppc_inst x)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
ppc_inst_swab(struct ppc_inst x)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
ppc_inst_read(const struct ppc_inst * ptr)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
ppc_inst_equal(struct ppc_inst x,struct ppc_inst y)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
ppc_inst_prefixed(struct ppc_inst x)71 static inline bool ppc_inst_prefixed(struct ppc_inst x)
72 {
73 return false;
74 }
75
ppc_inst_suffix(struct ppc_inst x)76 static inline u32 ppc_inst_suffix(struct ppc_inst x)
77 {
78 return 0;
79 }
80
ppc_inst_swab(struct ppc_inst x)81 static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x)
82 {
83 return ppc_inst(swab32(ppc_inst_val(x)));
84 }
85
ppc_inst_read(const struct ppc_inst * ptr)86 static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr)
87 {
88 return *ptr;
89 }
90
ppc_inst_equal(struct ppc_inst x,struct ppc_inst y)91 static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y)
92 {
93 return ppc_inst_val(x) == ppc_inst_val(y);
94 }
95
96 #endif /* CONFIG_PPC64 */
97
ppc_inst_len(struct ppc_inst x)98 static inline int ppc_inst_len(struct ppc_inst x)
99 {
100 return ppc_inst_prefixed(x) ? 8 : 4;
101 }
102
103 /*
104 * Return the address of the next instruction, if the instruction @value was
105 * located at @location.
106 */
ppc_inst_next(void * location,struct ppc_inst * value)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
ppc_inst_as_u64(struct ppc_inst x)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
125 #define PPC_INST_STR_LEN sizeof("00000000 00000000")
126
__ppc_inst_as_str(char str[PPC_INST_STR_LEN],struct ppc_inst x)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
144 int probe_user_read_inst(struct ppc_inst *inst,
145 struct ppc_inst __user *nip);
146
147 int probe_kernel_read_inst(struct ppc_inst *inst,
148 struct ppc_inst *src);
149
150 #endif /* _ASM_POWERPC_INST_H */
151