powerpc: asm/ppc-opcode.h: introduce __PPC_SH64()
Subsystems (3)
PATCH 1/3 powerpc: asm/ppc-opcode.h: introduce __PPC_SH64()
From: "Naveen N. Rao" <[email protected]>
Introduce __PPC_SH64() as a 64-bit variant to encode shift field in some of the shift and rotate instructions operating on double-words. Convert some of the BPF instruction macros to use the same.
--- arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/net/bpf_jit.h | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +1 -0
@@ -306,6 +306,7 @@#define __PPC_WC (w) (((w) & 0x3) << 21)#define __PPC_WS (w) (((w) & 0x1f) << 11)#define __PPC_SH (s) __PPC_WS(s) +#define __PPC_SH64 (s) (__PPC_SH(s) | (((s) & 0x20) >> 4))#define __PPC_MB (s) (((s) & 0x1f) << 6)#define __PPC_ME (s) (((s) & 0x1f) << 1)#define __PPC_MB64 (s) (__PPC_MB(s) | ((s) & 0x20))
arch/powerpc/net/bpf_jit.h +5 -6
@@ -157,8 +157,7 @@#define PPC_SRAD (d, a, s) EMIT(PPC_INST_SRAD | ___PPC_RA(d) | \ ___PPC_RS(a) | ___PPC_RB(s))#define PPC_SRADI (d, a, i) EMIT(PPC_INST_SRADI | ___PPC_RA(d) | \ - ___PPC_RS(a) | __PPC_SH(i) | \ - (((i) & 0x20) >> 4)) + ___PPC_RS(a) | __PPC_SH64(i))#define PPC_RLWINM (d, a, i, mb, me) EMIT(PPC_INST_RLWINM | ___PPC_RA(d) | \ ___PPC_RS(a) | __PPC_SH(i) | \ __PPC_MB(mb) | __PPC_ME(me))
@@ -166,11 +165,11 @@ ___PPC_RS(a) | __PPC_SH(i) | \ __PPC_MB(mb) | __PPC_ME(me))#define PPC_RLDICL (d, a, i, mb) EMIT(PPC_INST_RLDICL | ___PPC_RA(d) | \ - ___PPC_RS(a) | __PPC_SH(i) | \ - __PPC_MB64(mb) | (((i) & 0x20) >> 4)) + ___PPC_RS(a) | __PPC_SH64(i) | \ + __PPC_MB64(mb))#define PPC_RLDICR (d, a, i, me) EMIT(PPC_INST_RLDICR | ___PPC_RA(d) | \ - ___PPC_RS(a) | __PPC_SH(i) | \ - __PPC_ME64(me) | (((i) & 0x20) >> 4)) + ___PPC_RS(a) | __PPC_SH64(i) | \ + __PPC_ME64(me))/* slwi = rlwinm Rx, Ry, n, 0, 31-n */ #define PPC_SLWI (d, a, i) PPC_RLWINM(d, a, i, 0, 31-(i))
Signature · 1 line
2.7.4
PATCH 2/3 powerpc: add helper to check if offset is within rel branch range
To permit the use of relative branch instruction in powerpc, the target address has to be relatively nearby, since the address is specified in an immediate field (24 bit filed) in the instruction opcode itself. Here nearby refers to 32MB on either side of the current instruction.
This patch verifies whether the target address is within +/- 32MB range or not.
--- arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/lib/code-patching.c | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/code-patching.h +1 -0
@@ -22,6 +22,7 @@#define BRANCH_SET_LINK 0x1#define BRANCH_ABSOLUTE 0x2 +bool is_offset_in_branch_range (long offset);unsigned int create_branch (const unsigned int *addr,unsigned long target,int flags); unsigned int create_cond_branch(const unsigned int *addr,
arch/powerpc/lib/code-patching.c +23 -1
@@ -32,6 +32,28 @@ int patch_branch(unsigned int *addr, unsigned long target, int flags)return patch_instruction (addr,create_branch (addr, target, flags)); } +bool is_offset_in_branch_range (long offset) +{ +/* +* Powerpc branch instruction is : +* +* 0 6 30 31 +* +---------+----------------+---+---+ +* | opcode | LI |AA |LK | +* +---------+----------------+---+---+ +* Where AA = 0 and LK = 0 +* +* LI is a signed 24 bits integer. The real branch offset is computed +* by: imm32 = SignExtend(LI:'0b00', 32); +* +* So the maximum forward branch should be: +* (0x007fffff << 2) = 0x01fffffc = 0x1fffffc +* The maximum backward branch should be: +* (0xff800000 << 2) = 0xfe000000 = -0x2000000 +*/ +return (offset >=-0x2000000 && offset <=0x1fffffc && !(offset &0x3 )); +} +unsigned int create_branch (const unsigned int *addr,unsigned long target,int flags) {
@@ -43,7 +65,7 @@ unsigned int create_branch(const unsigned int *addr, offset = offset - (unsigned long )addr;/* Check we can represent the target in the instruction format */ -if (offset <-0x2000000 || offset >0x1fffffc || offset &0x3 ) +if (!is_offset_in_branch_range (offset))return 0 ;/* Mask out the flags and target, so they don't step on each other. */
Signature · 1 line
2.7.4
PATCH 3/3 powerpc: kprobes: fixes for kprobe_lookup_name on BE
In reply to Anju T SudhakarFrom: "Naveen N. Rao" <[email protected]>
Fix two issues with kprobes.h on BE which were exposed with the optprobes work:
- one, having to do with a missing include for linux/module.h for MODULE_NAME_LEN -- this didn't show up previously since the only users of kprobe_lookup_name were in kprobes.c, which included linux/module.h through other headers, and
- two, with a missing const qualifier for a local variable which ends up referring a string literal. Again, this is unique to how kprobe_lookup_name is being invoked in optprobes.c
--- arch/powerpc/include/asm/kprobes.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kprobes.h +2 -1
@@ -29,6 +29,7 @@#include <linux/types.h> #include <linux/ptrace.h> #include <linux/percpu.h> +#include <linux/module.h> #include <asm/probes.h> #include <asm/code-patching.h>
@@ -61,7 +62,7 @@ typedef ppc_opcode_t kprobe_opcode_t;#define kprobe_lookup_name (name, addr) \ { \ char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN]; \ - char *modsym; \ + const char *modsym; \ bool dot_appended = false; \ if ((modsym = strchr(name, ':')) != NULL) { \ modsym++; \
Signature · 1 line
2.7.4
Quoted 7 lines · Anju T
From: "Naveen N. Rao" <[email protected]>
Introduce
__PPC_SH64()as a 64-bit variant to encode shift field in some of the shift and rotate instructions operating on double-words. Convert some of the BPF instruction macros to use the same.Signed-off-by Naveen N. Rao <[email protected]>
Series applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/c233f5979b3dbb39a5b2473b5fcaf5
cheers