2020-05-06 11:40:26 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#ifndef _ASM_POWERPC_INST_H
|
|
|
|
#define _ASM_POWERPC_INST_H
|
|
|
|
|
2020-05-15 10:12:55 +08:00
|
|
|
#include <asm/ppc-opcode.h>
|
|
|
|
|
2020-05-06 11:40:26 +08:00
|
|
|
/*
|
|
|
|
* Instruction data type for POWER
|
|
|
|
*/
|
|
|
|
|
2020-05-06 11:40:31 +08:00
|
|
|
struct ppc_inst {
|
|
|
|
u32 val;
|
2020-05-15 10:12:55 +08:00
|
|
|
#ifdef CONFIG_PPC64
|
|
|
|
u32 suffix;
|
|
|
|
#endif
|
2020-05-06 11:40:31 +08:00
|
|
|
} __packed;
|
2020-05-06 11:40:26 +08:00
|
|
|
|
2020-05-06 11:40:31 +08:00
|
|
|
static inline u32 ppc_inst_val(struct ppc_inst x)
|
2020-05-06 11:40:27 +08:00
|
|
|
{
|
2020-05-06 11:40:31 +08:00
|
|
|
return x.val;
|
2020-05-06 11:40:27 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 10:12:55 +08:00
|
|
|
static inline int ppc_inst_primary_opcode(struct ppc_inst x)
|
2020-05-06 11:40:37 +08:00
|
|
|
{
|
2020-05-15 10:12:55 +08:00
|
|
|
return ppc_inst_val(x) >> 26;
|
2020-05-06 11:40:37 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 10:12:55 +08:00
|
|
|
#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)
|
2020-05-06 11:40:28 +08:00
|
|
|
{
|
2020-05-15 10:12:55 +08:00
|
|
|
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;
|
2020-05-06 11:40:28 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 11:40:31 +08:00
|
|
|
static inline struct ppc_inst ppc_inst_swab(struct ppc_inst x)
|
2020-05-06 11:40:29 +08:00
|
|
|
{
|
|
|
|
return ppc_inst(swab32(ppc_inst_val(x)));
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:40:32 +08:00
|
|
|
static inline struct ppc_inst ppc_inst_read(const struct ppc_inst *ptr)
|
|
|
|
{
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:40:31 +08:00
|
|
|
static inline bool ppc_inst_equal(struct ppc_inst x, struct ppc_inst y)
|
2020-05-06 11:40:30 +08:00
|
|
|
{
|
2020-05-06 11:40:31 +08:00
|
|
|
return ppc_inst_val(x) == ppc_inst_val(y);
|
2020-05-06 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 10:12:55 +08:00
|
|
|
#endif /* CONFIG_PPC64 */
|
|
|
|
|
|
|
|
static inline int ppc_inst_len(struct ppc_inst x)
|
|
|
|
{
|
|
|
|
return ppc_inst_prefixed(x) ? 8 : 4;
|
|
|
|
}
|
|
|
|
|
powerpc: Add ppc_inst_next()
In a few places we want to calculate the address of the next
instruction. Previously that was simple, we just added 4 bytes, or if
using a u32 * we incremented that pointer by 1.
But prefixed instructions make it more complicated, we need to advance
by either 4 or 8 bytes depending on the actual instruction. We also
can't do pointer arithmetic using struct ppc_inst, because it is
always 8 bytes in size on 64-bit, even though we might only need to
advance by 4 bytes.
So add a ppc_inst_next() helper which calculates the location of the
next instruction, if the given instruction was located at the given
address. Note the instruction doesn't need to actually be at the
address in memory.
Although it would seem natural for the value to be passed by value,
that makes it too easy to write a loop that will read off the end of a
page, eg:
for (; src < end; src = ppc_inst_next(src, *src),
dest = ppc_inst_next(dest, *dest))
As noticed by Christophe and Jordan, if end is the exact end of a
page, and the next page is not mapped, this will fault, because *dest
will read 8 bytes, 4 bytes into the next page.
So value is passed by reference, so the helper can be careful to use
ppc_inst_read() on it.
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Reviewed-by: Jordan Niethe <jniethe5@gmail.com>
Link: https://lore.kernel.org/r/20200522133318.1681406-1-mpe@ellerman.id.au
2020-05-22 21:33:18 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:26:30 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:27:25 +08:00
|
|
|
#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; \
|
|
|
|
})
|
|
|
|
|
2020-05-06 11:40:33 +08:00
|
|
|
int probe_user_read_inst(struct ppc_inst *inst,
|
|
|
|
struct ppc_inst __user *nip);
|
|
|
|
|
2020-05-06 11:40:34 +08:00
|
|
|
int probe_kernel_read_inst(struct ppc_inst *inst,
|
|
|
|
struct ppc_inst *src);
|
|
|
|
|
2020-05-06 11:40:26 +08:00
|
|
|
#endif /* _ASM_POWERPC_INST_H */
|