blob: cc73c12675721c3e699dcc4056db785a5a04d6eb [file] [log] [blame]
Jordan Niethe75346252020-05-06 13:40:26 +10001/* SPDX-License-Identifier: GPL-2.0-or-later */
2#ifndef _ASM_POWERPC_INST_H
3#define _ASM_POWERPC_INST_H
4
Jordan Niethe650b55b2020-05-15 12:12:55 +10005#include <asm/ppc-opcode.h>
6
Jordan Niethe75346252020-05-06 13:40:26 +10007/*
8 * Instruction data type for POWER
9 */
10
Jordan Niethe94afd062020-05-06 13:40:31 +100011struct ppc_inst {
12 u32 val;
Jordan Niethe650b55b2020-05-15 12:12:55 +100013#ifdef CONFIG_PPC64
14 u32 suffix;
15#endif
Jordan Niethe94afd062020-05-06 13:40:31 +100016} __packed;
Jordan Niethe75346252020-05-06 13:40:26 +100017
Jordan Niethe94afd062020-05-06 13:40:31 +100018static inline u32 ppc_inst_val(struct ppc_inst x)
Jordan Niethe777e26f2020-05-06 13:40:27 +100019{
Jordan Niethe94afd062020-05-06 13:40:31 +100020 return x.val;
Jordan Niethe777e26f2020-05-06 13:40:27 +100021}
22
Jordan Niethe94afd062020-05-06 13:40:31 +100023static inline int ppc_inst_primary_opcode(struct ppc_inst x)
Jordan Niethe80948922020-05-06 13:40:28 +100024{
25 return ppc_inst_val(x) >> 26;
26}
27
Jordan Niethe650b55b2020-05-15 12:12:55 +100028#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
33static inline u32 ppc_inst_suffix(struct ppc_inst x)
34{
35 return x.suffix;
36}
37
38static 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
43static 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
49static 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
62static 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
71static inline bool ppc_inst_prefixed(struct ppc_inst x)
72{
73 return false;
74}
75
76static inline u32 ppc_inst_suffix(struct ppc_inst x)
77{
78 return 0;
79}
80
Jordan Niethe94afd062020-05-06 13:40:31 +100081static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x)
Jordan Nietheaabd2232020-05-06 13:40:29 +100082{
83 return ppc_inst(swab32(ppc_inst_val(x)));
84}
85
Jordan Niethef8faaff2020-05-06 13:40:32 +100086static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr)
87{
88 return *ptr;
89}
90
Jordan Niethe94afd062020-05-06 13:40:31 +100091static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y)
Jordan Niethe217862d2020-05-06 13:40:30 +100092{
Jordan Niethe94afd062020-05-06 13:40:31 +100093 return ppc_inst_val(x) == ppc_inst_val(y);
Jordan Niethe217862d2020-05-06 13:40:30 +100094}
95
Jordan Niethe650b55b2020-05-15 12:12:55 +100096#endif /* CONFIG_PPC64 */
97
98static inline int ppc_inst_len(struct ppc_inst x)
99{
100 return ppc_inst_prefixed(x) ? 8 : 4;
101}
102
Michael Ellermanc5ff46d2020-05-22 23:33:18 +1000103/*
104 * Return the address of the next instruction, if the instruction @value was
105 * located at @location.
106 */
107static 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 Ellerman16ef9762020-05-26 17:26:30 +1000116static 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 Niethe50428fd2020-06-02 15:27:25 +1000125#define PPC_INST_STR_LEN sizeof("00000000 00000000")
126
127static 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 Niethe7ba68b22020-05-06 13:40:33 +1000144int probe_user_read_inst(struct ppc_inst *inst,
145 struct ppc_inst __user *nip);
146
Jordan Niethe95b980a2020-05-06 13:40:34 +1000147int probe_kernel_read_inst(struct ppc_inst *inst,
148 struct ppc_inst *src);
149
Jordan Niethe75346252020-05-06 13:40:26 +1000150#endif /* _ASM_POWERPC_INST_H */