aboutsummaryrefslogtreecommitdiffstats
path: root/arch/powerpc/include/asm/inst.h
blob: 268d3bd073c8ac0b93221992fcc1377d79eccd1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* SPDX-License-Identifier: GPL-2.0-or-later */
#ifndef _ASM_POWERPC_INST_H
#define _ASM_POWERPC_INST_H

#include <asm/ppc-opcode.h>

#ifdef CONFIG_PPC64

#define ___get_user_instr(gu_op, dest, ptr)				\
({									\
	long __gui_ret = 0;						\
	unsigned long __gui_ptr = (unsigned long)ptr;			\
	struct ppc_inst __gui_inst;					\
	unsigned int __prefix, __suffix;				\
	__gui_ret = gu_op(__prefix, (unsigned int __user *)__gui_ptr);	\
	if (__gui_ret == 0) {						\
		if ((__prefix >> 26) == OP_PREFIX) {			\
			__gui_ret = gu_op(__suffix,			\
				(unsigned int __user *)__gui_ptr + 1);	\
			__gui_inst = ppc_inst_prefix(__prefix,		\
						     __suffix);		\
		} else {						\
			__gui_inst = ppc_inst(__prefix);		\
		}							\
		if (__gui_ret == 0)					\
			(dest) = __gui_inst;				\
	}								\
	__gui_ret;							\
})
#else /* !CONFIG_PPC64 */
#define ___get_user_instr(gu_op, dest, ptr)				\
	gu_op((dest).val, (u32 __user *)(ptr))
#endif /* CONFIG_PPC64 */

#define get_user_instr(x, ptr) \
	___get_user_instr(get_user, x, ptr)

#define __get_user_instr(x, ptr) \
	___get_user_instr(__get_user, x, ptr)

/*
 * Instruction data type for POWER
 */

struct ppc_inst {
	u32 val;
#ifdef CONFIG_PPC64
	u32 suffix;
#endif
} __packed;

static inline u32 ppc_inst_val(struct ppc_inst x)
{
	return x.val;
}

static inline int ppc_inst_primary_opcode(struct ppc_inst x)
{
	return ppc_inst_val(x) >> 26;
}

#ifdef CONFIG_PPC64
#define ppc_inst(x) ((struct ppc_inst){ .val = (x), .suffix = 0xff })

#define ppc_inst_prefix(x, y) ((struct ppc_inst){ .val = (x), .suffix = (y) })

static inline u32 ppc_inst_suffix(struct ppc_inst x)
{
	return x.suffix;
}

static inline bool ppc_inst_prefixed(struct ppc_inst x)
{
	return (ppc_inst_primary_opcode(x) == 1) && ppc_inst_suffix(x) != 0xff;
}

static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x)
{
	return ppc_inst_prefix(swab32(ppc_inst_val(x)),
			       swab32(ppc_inst_suffix(x)));
}

static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr)
{
	u32 val, suffix;

	val = *(u32 *)ptr;
	if ((val >> 26) == OP_PREFIX) {
		suffix = *((u32 *)ptr + 1);
		return ppc_inst_prefix(val, suffix);
	} else {
		return ppc_inst(val);
	}
}

static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y)
{
	return *(u64 *)&x == *(u64 *)&y;
}

#else

#define ppc_inst(x) ((struct ppc_inst){ .val = x })

#define ppc_inst_prefix(x, y) ppc_inst(x)

static inline bool ppc_inst_prefixed(struct ppc_inst x)
{
	return false;
}

static inline u32 ppc_inst_suffix(struct ppc_inst x)
{
	return 0;
}

static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x)
{
	return ppc_inst(swab32(ppc_inst_val(x)));
}

static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr)
{
	return *ptr;
}

static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y)
{
	return ppc_inst_val(x) == ppc_inst_val(y);
}

#endif /* CONFIG_PPC64 */

static inline int ppc_inst_len(struct ppc_inst x)
{
	return ppc_inst_prefixed(x) ? 8 : 4;
}

/*
 * Return the address of the next instruction, if the instruction @value was
 * located at @location.
 */
static inline struct ppc_inst *ppc_inst_next(void *location, struct ppc_inst *value)
{
	struct ppc_inst tmp;

	tmp = ppc_inst_read(value);

	return location + ppc_inst_len(tmp);
}

static inline unsigned long ppc_inst_as_ulong(struct ppc_inst x)
{
	if (IS_ENABLED(CONFIG_PPC32))
		return ppc_inst_val(x);
	else if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
		return (u64)ppc_inst_suffix(x) << 32 | ppc_inst_val(x);
	else
		return (u64)ppc_inst_val(x) << 32 | ppc_inst_suffix(x);
}

#define PPC_INST_STR_LEN sizeof("00000000 00000000")

static inline char *__ppc_inst_as_str(char str[PPC_INST_STR_LEN], struct ppc_inst x)
{
	if (ppc_inst_prefixed(x))
		sprintf(str, "%08x %08x", ppc_inst_val(x), ppc_inst_suffix(x));
	else
		sprintf(str, "%08x", ppc_inst_val(x));

	return str;
}

#define ppc_inst_as_str(x)		\
({					\
	char __str[PPC_INST_STR_LEN];	\
	__ppc_inst_as_str(__str, x);	\
	__str;				\
})

int copy_inst_from_kernel_nofault(struct ppc_inst *inst, struct ppc_inst *src);

#endif /* _ASM_POWERPC_INST_H */