From: Paul Mackerras <paulus@ozlabs.org>
To: linuxppc-dev@ozlabs.org
Subject: [PATCH] powerpc: Implement cross-endian emulation of larx and stcx instructions
Date: Mon, 4 Sep 2017 15:34:02 +1000 [thread overview]
Message-ID: <20170904053402.GB16532@fergus.ozlabs.ibm.com> (raw)
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
next reply other threads:[~2017-09-04 5:34 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-04 5:34 Paul Mackerras [this message]
2018-03-26 9:22 ` [PATCH] powerpc: Implement cross-endian emulation of larx and stcx instructions Michael Ellerman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170904053402.GB16532@fergus.ozlabs.ibm.com \
--to=paulus@ozlabs.org \
--cc=linuxppc-dev@ozlabs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).