Skip to content

Commit c5ff46d

Browse files
committed
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
1 parent baddc87 commit c5ff46d

4 files changed

Lines changed: 23 additions & 9 deletions

File tree

‎arch/powerpc/include/asm/inst.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ static inline int ppc_inst_len(struct ppc_inst x)
100100
return ppc_inst_prefixed(x) ? 8 : 4;
101101
}
102102

103+
/*
104+
* Return the address of the next instruction, if the instruction @value was
105+
* located at @location.
106+
*/
107+
static 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+
103116
int probe_user_read_inst(struct ppc_inst *inst,
104117
struct ppc_inst __user *nip);
105118

‎arch/powerpc/kernel/uprobes.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
112112
* support doesn't exist and have to fix-up the next instruction
113113
* to be executed.
114114
*/
115-
regs->nip = utask->vaddr + ppc_inst_len(ppc_inst_read(&auprobe->insn));
115+
regs->nip = (unsigned long)ppc_inst_next((void *)utask->vaddr, &auprobe->insn);
116116

117117
user_disable_single_step(current);
118118
return 0;

‎arch/powerpc/lib/feature-fixups.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static int patch_alt_instruction(struct ppc_inst *src, struct ppc_inst *dest,
6868

6969
static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
7070
{
71-
struct ppc_inst *start, *end, *alt_start, *alt_end, *src, *dest;
71+
struct ppc_inst *start, *end, *alt_start, *alt_end, *src, *dest, nop;
7272

7373
start = calc_addr(fcur, fcur->start_off);
7474
end = calc_addr(fcur, fcur->end_off);
@@ -84,14 +84,15 @@ static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
8484
src = alt_start;
8585
dest = start;
8686

87-
for (; src < alt_end; src = (void *)src + ppc_inst_len(ppc_inst_read(src)),
88-
(dest = (void *)dest + ppc_inst_len(ppc_inst_read(dest)))) {
87+
for (; src < alt_end; src = ppc_inst_next(src, src),
88+
dest = ppc_inst_next(dest, dest)) {
8989
if (patch_alt_instruction(src, dest, alt_start, alt_end))
9090
return 1;
9191
}
9292

93-
for (; dest < end; dest = (void *)dest + ppc_inst_len(ppc_inst(PPC_INST_NOP)))
94-
raw_patch_instruction(dest, ppc_inst(PPC_INST_NOP));
93+
nop = ppc_inst(PPC_INST_NOP);
94+
for (; dest < end; dest = ppc_inst_next(dest, &nop))
95+
raw_patch_instruction(dest, nop);
9596

9697
return 0;
9798
}
@@ -405,8 +406,8 @@ static void do_final_fixups(void)
405406
while (src < end) {
406407
inst = ppc_inst_read(src);
407408
raw_patch_instruction(dest, inst);
408-
src = (void *)src + ppc_inst_len(inst);
409-
dest = (void *)dest + ppc_inst_len(inst);
409+
src = ppc_inst_next(src, src);
410+
dest = ppc_inst_next(dest, dest);
410411
}
411412
#endif
412413
}

‎arch/powerpc/xmon/xmon.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ static void insert_bpts(void)
939939
}
940940

941941
patch_instruction(bp->instr, instr);
942-
patch_instruction((void *)bp->instr + ppc_inst_len(instr),
942+
patch_instruction(ppc_inst_next(bp->instr, &instr),
943943
ppc_inst(bpinstr));
944944
if (bp->enabled & BP_CIABR)
945945
continue;

0 commit comments

Comments
 (0)