* [PATCH] powerpc: Implement cross-endian emulation of larx and stcx instructions
@ 2017-09-04 5:34 Paul Mackerras
2018-03-26 9:22 ` Michael Ellerman
0 siblings, 1 reply; 2+ messages in thread
From: Paul Mackerras @ 2017-09-04 5:34 UTC (permalink / raw)
To: linuxppc-dev
This adds byte-swapping of the values loaded or stored by the
load with reservation (larx) and store conditional (stcx)
instructions when the execution environment being emulated has
the opposite endianness to the kernel. This should have been done
in commit d955189ae427 ("powerpc: Handle opposite-endian processes
in emulation code", 2017-08-30) but was missed then.
Since op->reg is used quite frequently in emulate_loadstore(),
this puts op->reg into rd at the beginning of the function and
replaces subsequent uses of op->reg with rd.
This does not affect alignment interrupt handling, since these
instructions cannot be emulated when the address is not aligned,
because we have no way to do atomic unaligned accesses, in
general.
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
---
arch/powerpc/lib/sstep.c | 47 ++++++++++++++++++++++++++++++-----------------
1 file changed, 30 insertions(+), 17 deletions(-)
diff --git a/arch/powerpc/lib/sstep.c b/arch/powerpc/lib/sstep.c
index fb9f58b..0590417 100644
--- a/arch/powerpc/lib/sstep.c
+++ b/arch/powerpc/lib/sstep.c
@@ -2719,6 +2719,7 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
type = op->type & INSTR_TYPE_MASK;
cross_endian = (regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
ea = truncate_if_32bit(regs->msr, op->ea);
+ rd = op->reg;
switch (type) {
case LARX:
@@ -2745,7 +2746,12 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
__get_user_asmx(val, ea, err, "ldarx");
break;
case 16:
- err = do_lqarx(ea, ®s->gpr[op->reg]);
+ err = do_lqarx(ea, ®s->gpr[rd]);
+ if (unlikely(cross_endian)) {
+ val = byterev_8(regs->gpr[rd]);
+ regs->gpr[rd] = byterev_8(regs->gpr[rd + 1]);
+ regs->gpr[rd + 1] = val;
+ }
break;
#endif
default:
@@ -2755,8 +2761,11 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
regs->dar = ea;
break;
}
- if (size < 16)
- regs->gpr[op->reg] = val;
+ if (size < 16) {
+ if (unlikely(cross_endian))
+ val = byterev_8(val);
+ regs->gpr[rd] = val;
+ }
break;
case STCX:
@@ -2764,6 +2773,8 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
return -EACCES; /* can't handle misaligned */
if (!address_ok(regs, ea, size))
return -EFAULT;
+ if (unlikely(cross_endian))
+ do_byterev(&op->val, size);
err = 0;
switch (size) {
#ifdef __powerpc64__
@@ -2782,8 +2793,12 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
__put_user_asmx(op->val, ea, err, "stdcx.", cr);
break;
case 16:
- err = do_stqcx(ea, regs->gpr[op->reg],
- regs->gpr[op->reg + 1], &cr);
+ if (unlikely(cross_endian))
+ err = do_stqcx(ea, byterev_8(regs->gpr[rd + 1]),
+ op->val, &cr);
+ else
+ err = do_stqcx(ea, op->val, regs->gpr[rd + 1],
+ &cr);
break;
#endif
default:
@@ -2800,16 +2815,16 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
case LOAD:
#ifdef __powerpc64__
if (size == 16) {
- err = emulate_lq(regs, ea, op->reg, cross_endian);
+ err = emulate_lq(regs, ea, rd, cross_endian);
break;
}
#endif
- err = read_mem(®s->gpr[op->reg], ea, size, regs);
+ err = read_mem(®s->gpr[rd], ea, size, regs);
if (!err) {
if (op->type & SIGNEXT)
- do_signext(®s->gpr[op->reg], size);
+ do_signext(®s->gpr[rd], size);
if ((op->type & BYTEREV) == (cross_endian ? 0 : BYTEREV))
- do_byterev(®s->gpr[op->reg], size);
+ do_byterev(®s->gpr[rd], size);
}
break;
@@ -2830,7 +2845,7 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
case LOAD_VMX:
if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_VEC))
return 0;
- err = do_vec_load(op->reg, ea, size, regs, cross_endian);
+ err = do_vec_load(rd, ea, size, regs, cross_endian);
break;
#endif
#ifdef CONFIG_VSX
@@ -2841,7 +2856,7 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
* Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
* when the target of the instruction is a vector register.
*/
- if (op->reg >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
+ if (rd >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
msrbit = MSR_VEC;
if (!(regs->msr & MSR_PR) && !(regs->msr & msrbit))
return 0;
@@ -2852,7 +2867,6 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
case LOAD_MULTI:
if (!address_ok(regs, ea, size))
return -EFAULT;
- rd = op->reg;
for (i = 0; i < size; i += 4) {
unsigned int v32 = 0;
@@ -2874,12 +2888,12 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
case STORE:
#ifdef __powerpc64__
if (size == 16) {
- err = emulate_stq(regs, ea, op->reg, cross_endian);
+ err = emulate_stq(regs, ea, rd, cross_endian);
break;
}
#endif
if ((op->type & UPDATE) && size == sizeof(long) &&
- op->reg == 1 && op->update_reg == 1 &&
+ rd == 1 && op->update_reg == 1 &&
!(regs->msr & MSR_PR) &&
ea >= regs->gpr[1] - STACK_INT_FRAME_SIZE) {
err = handle_stack_update(ea, regs);
@@ -2901,7 +2915,7 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
case STORE_VMX:
if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_VEC))
return 0;
- err = do_vec_store(op->reg, ea, size, regs, cross_endian);
+ err = do_vec_store(rd, ea, size, regs, cross_endian);
break;
#endif
#ifdef CONFIG_VSX
@@ -2912,7 +2926,7 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
* Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
* when the target of the instruction is a vector register.
*/
- if (op->reg >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
+ if (rd >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
msrbit = MSR_VEC;
if (!(regs->msr & MSR_PR) && !(regs->msr & msrbit))
return 0;
@@ -2923,7 +2937,6 @@ int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
case STORE_MULTI:
if (!address_ok(regs, ea, size))
return -EFAULT;
- rd = op->reg;
for (i = 0; i < size; i += 4) {
unsigned int v32 = regs->gpr[rd];
--
2.7.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] powerpc: Implement cross-endian emulation of larx and stcx instructions
2017-09-04 5:34 [PATCH] powerpc: Implement cross-endian emulation of larx and stcx instructions Paul Mackerras
@ 2018-03-26 9:22 ` Michael Ellerman
0 siblings, 0 replies; 2+ messages in thread
From: Michael Ellerman @ 2018-03-26 9:22 UTC (permalink / raw)
To: Paul Mackerras, linuxppc-dev
Paul Mackerras <paulus@ozlabs.org> writes:
> diff --git a/arch/powerpc/lib/sstep.c b/arch/powerpc/lib/sstep.c
> index fb9f58b..0590417 100644
> --- a/arch/powerpc/lib/sstep.c
> +++ b/arch/powerpc/lib/sstep.c
> @@ -2755,8 +2761,11 @@ int emulate_loadstore(struct pt_regs *regs, struct=
instruction_op *op)
> regs->dar =3D ea;
> break;
> }
> - if (size < 16)
> - regs->gpr[op->reg] =3D val;
> + if (size < 16) {
> + if (unlikely(cross_endian))
> + val =3D byterev_8(val);
This doesn't build on 32-bit (pmac32_defconfig):
arch/powerpc/lib/sstep.c: In function =E2=80=98emulate_loadstore=E2=80=99:
arch/powerpc/lib/sstep.c:2789:11: error: implicit declaration of function=
=E2=80=98byterev_8=E2=80=99;did you mean =E2=80=98byterev_4=E2=80=99? [-We=
rror=3Dimplicit-function-declaration]
val =3D byterev_8(val);
^~~~~~~~~
byterev_4
cheers
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2018-03-26 9:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-04 5:34 [PATCH] powerpc: Implement cross-endian emulation of larx and stcx instructions Paul Mackerras
2018-03-26 9:22 ` Michael Ellerman
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.