inst.h 2.6 KB
Newer Older
1 2 3 4
/* SPDX-License-Identifier: GPL-2.0-or-later */
#ifndef _ASM_POWERPC_INST_H
#define _ASM_POWERPC_INST_H

5 6
#include <asm/ppc-opcode.h>

7 8 9 10
/*
 * Instruction data type for POWER
 */

11 12
struct ppc_inst {
	u32 val;
13 14 15
#ifdef CONFIG_PPC64
	u32 suffix;
#endif
16
} __packed;
17

18
static inline u32 ppc_inst_val(struct ppc_inst x)
19
{
20
	return x.val;
21 22
}

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

28 29 30 31 32 33
#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)
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
	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 })

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

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

81
static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x)
82 83 84 85
{
	return ppc_inst(swab32(ppc_inst_val(x)));
}

86 87 88 89 90
static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr)
{
	return *ptr;
}

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 97 98 99 100 101 102
#endif /* CONFIG_PPC64 */

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

M
Michael Ellerman 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
/*
 * 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);
}

116 117 118 119 120 121 122 123 124
static inline u64 ppc_inst_as_u64(struct ppc_inst x)
{
#ifdef 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);
#endif
}

125 126 127
int probe_user_read_inst(struct ppc_inst *inst,
			 struct ppc_inst __user *nip);

128 129 130
int probe_kernel_read_inst(struct ppc_inst *inst,
			   struct ppc_inst *src);

131
#endif /* _ASM_POWERPC_INST_H */