powerpc: Send SIGBUS on unaligned copy and paste
PATCH v2 1/2 powerpc: Send SIGBUS on unaligned copy and paste
Calling ISA 3.0 instructions copy, copy_first, paste and paste_last generates an alignment fault when copying or pasting unaligned data (128 byte). We catch this and send SIGBUS to the userspace process that caused it.
We do not emulate these because paste may contain additional metadata when pasting to a co-processor and paste_last is the synchronisation point for preceding copy/paste sequences.
Thanks to Michael Neuling <[email protected]> for his help.
---
Changes since v1:
- define and use instruction for mask test
arch/powerpc/include/asm/ppc-opcode.h | 4 ++++arch/powerpc/kernel/align.c | 14 ++++++++++++++
2 files changed, 18 insertions(+)
arch/powerpc/include/asm/ppc-opcode.h +0 -0
@@ -131,6 +131,8 @@
/* sorted alphabetically */
#define PPC_INST_BHRBE 0x7c00025c
#define PPC_INST_CLRBHRB 0x7c00035c
+#define PPC_INST_COPY 0x7c00060c
+#define PPC_INST_COPY_FIRST 0x7c20060c
#define PPC_INST_CP_ABORT 0x7c00068c
#define PPC_INST_DCBA 0x7c0005ec
#define PPC_INST_DCBA_MASK 0xfc0007fe
@@ -159,6 +161,8 @@
#define PPC_INST_MSGSNDP 0x7c00011c
#define PPC_INST_MTTMR 0x7c0003dc
#define PPC_INST_NOP 0x60000000
+#define PPC_INST_PASTE 0x7c00070c
+#define PPC_INST_PASTE_LAST 0x7c20070d
#define PPC_INST_POPCNTB 0x7c0000f4
#define PPC_INST_POPCNTB_MASK 0xfc0007fe
#define PPC_INST_POPCNTD 0x7c0003f4
arch/powerpc/kernel/align.c +0 -0
@@ -875,6 +875,20 @@ int fix_alignment(struct pt_regs *regs)
return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize); }
#endif
+
+ /*
+ * ISA 3.0 (such as P9) copy, copy_first, paste and paste_last alignment
+ * check.
+ *
+ * Send a SIGBUS to the process that caused the fault.
+ *
+ * We do not emulate these because paste may contain additional metadata
+ * when pasting to a co-processor. Furthermore, paste_last is the
+ * synchronisation point for preceding copy/paste sequences.
+ */
+ if ((instruction & 0xfc0006fe) == PPC_INST_COPY)
+ return -EIO;
+
/* A size of 0 indicates an instruction we don't support, with * the exception of DCBZ which is handled as a special case here */
Signature · 1 line
2.7.4
Review
Quoted 5 lines · Chris Smart
(quoted diff) +4
-0+#define PPC_INST_COPY 0x7c00060c +#define PPC_INST_COPY_FIRST 0x7c20060c +#define PPC_INST_PASTE 0x7c00070c +#define PPC_INST_PASTE_LAST 0x7c20070d
That's not quite right I think.
copy is 7c00060c mask fc2007fe (or ffe007fe)copy_first is 7c20060c mask fc2007fe
paste is 7c00070c mask fc2007fepaste_last is 7c20070c mask fc2007fe
(this includes record form for paste; the low bit).
Segher
Quoted 54 lines · Chris Smart
Calling ISA 3.0 instructions copy,
copy_first, paste andpaste_lastgenerates an alignment fault when copying or pasting unaligned data (128 byte). We catch this and send SIGBUS to the userspace process that caused it.We do not emulate these because paste may contain additional metadata when pasting to a co-processor and
paste_lastis the synchronisation point for preceding copy/paste sequences.Thanks to Michael Neuling <[email protected]> for his help.
Signed-off-by Chris Smart <[email protected]>---
Changes since v1:
- define and use instruction for mask test
arch/powerpc/include/asm/ppc-opcode.h| 4 ++++arch/powerpc/kernel/align.c| 14 ++++++++++++++2 files changed, 18 insertions(+)arch/powerpc/include/asm/ppc-opcode.h +4
-0@@ -131,6 +131,8 @@/* sorted alphabetically */ #define PPC_INST_BHRBE 0x7c00025c#define PPC_INST_CLRBHRB 0x7c00035c +#define PPC_INST_COPY 0x7c00060c +#define PPC_INST_COPY_FIRST 0x7c20060c#define PPC_INST_CP_ABORT 0x7c00068c#define PPC_INST_DCBA 0x7c0005ec#define PPC_INST_DCBA_MASK 0xfc0007fe@@ -159,6 +161,8 @@#define PPC_INST_MSGSNDP 0x7c00011c#define PPC_INST_MTTMR 0x7c0003dc#define PPC_INST_NOP 0x60000000 +#define PPC_INST_PASTE 0x7c00070c +#define PPC_INST_PASTE_LAST 0x7c20070d#define PPC_INST_POPCNTB 0x7c0000f4#define PPC_INST_POPCNTB_MASK 0xfc0007fe#define PPC_INST_POPCNTD 0x7c0003f4arch/powerpc/kernel/align.c +3
-0@@ -875,6 +875,20 @@ int fix_alignment(struct pt_regs *regs) return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize); }#endif + +/* +* ISA 3.0 (such as P9) copy, copy_first, paste and paste_last alignment
arch/powerpc/kernel/align.c int fix_alignment(struct pt_regs *regs) · line 890
Quoted 10 lines
arch/powerpc/kernel/align.c +10
-0+* check. +* +* Send a SIGBUS to the process that caused the fault. +* +* We do not emulate these because paste may contain additional metadata +* when pasting to a co-processor. Furthermore, paste_last is the +* synchronisation point for preceding copy/paste sequences. +*/ +if ((instruction &0xfc0006fe ) ==PPC_INST_COPY ) +return -EIO ;
Should this all be under cpu_has_feature(CPU_FTR_ARCH_300)?
Balbir Singh.
Quoted 3 lines · Balbir Singh
On 17/06/16 09:33, Chris Smart wrote:
[snip]
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +13
-0+ + /* + * ISA 3.0 (such as P9) copy, copy_first, paste and paste_last alignment + * check. + * + * Send a SIGBUS to the process that caused the fault. + * + * We do not emulate these because paste may contain additional metadata + * when pasting to a co-processor. Furthermore, paste_last is the + * synchronisation point for preceding copy/paste sequences. + */ + if ((instruction & 0xfc0006fe) == PPC_INST_COPY) + return -EIO;
Quoted 2 lines
Should this all be under
cpu_has_feature(CPU_FTR_ARCH_300)?
I'm not sure we should or not. That instruction only exists on those machines so is it worth adding an additional check when the next check will fail anyway?
I guess it would reduce non ISA 3.0 machines to just a single check rather than two, but increases ISO 3.0 machines to three.
I defer to the wisdom of others.
-c
Quoted 24 lines · Chris Smart
Quoted 3 lines · Balbir Singh
On 17/06/16 09:33, Chris Smart wrote:
[snip]
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +13
-0+ + /* + * ISA 3.0 (such as P9) copy, copy_first, paste and paste_last alignment + * check. + * + * Send a SIGBUS to the process that caused the fault. + * + * We do not emulate these because paste may contain additional metadata + * when pasting to a co-processor. Furthermore, paste_last is the + * synchronisation point for preceding copy/paste sequences. + */ + if ((instruction & 0xfc0006fe) == PPC_INST_COPY) + return -EIO;Quoted 2 lines
Should this all be under
cpu_has_feature(CPU_FTR_ARCH_300)?
Quoted 7 lines
I'm not sure we should or not. That instruction only exists on those machines so is it worth adding an additional check when the next check will fail anyway?
I guess it would reduce non ISA 3.0 machines to just a single check rather than two, but increases ISO 3.0 machines to three.
Ideally we can do features and fixups, but that today works mostly for the assembly code we have. In this case we might get away by doing the if and "&", I would recommend adding a branch hint
if (unlikely(...))
Balbir Singh
Quoted 9 lines · Segher Boessenkool
Review
Quoted 5 lines · Chris Smart
(quoted diff) +4
-0+#define PPC_INST_COPY 0x7c00060c +#define PPC_INST_COPY_FIRST 0x7c20060c +#define PPC_INST_PASTE 0x7c00070c +#define PPC_INST_PASTE_LAST 0x7c20070dThat's not quite right I think.
Hi Segher,
Thanks for checking that for me, it's good to make sure it's correct.
Just to be sure, I've gone back and compared them all with the ISA. I think that the only one that differs is the paste_last. Am I missing something?
Quoted 1 line
copy is
7c00060cmaskfc2007fe(orffe007fe)
COPY = copy RA,RB,L (L=0)
31 //// L RA RB 774 /
011111 0000 0 00000 00000 1100000110 0 = 0x7c00060c (instruction) 111111 0000 1 00000 00000 1111111111 0 = 0xfc2007fe (specific mask)
Quoted 1 line
copy_firstis7c20060cmaskfc2007fe
COPY_FIRST = copy RA,RB,L (L=1)
If L=1, the instruction identifies the beginning of a move group.
31 //// L RA RB 774 /
011111 0000 1 00000 00000 1100000110 0 = 0x7c20060c (instruction) 111111 0000 1 00000 00000 1111111111 0 = 0xfc2007fe (specific mask)
Quoted 1 line
paste is
7c00070cmaskfc2007fe
PASTE = paste RA,RB,L (L=0 Rc=0)
31 //// L RA RB 902 Rc
011111 0000 0 00000 00000 1110000110 0 = 0x7c00070c (instruction) 111111 0000 1 00000 00000 1111111111 1 = 0xfc2007ff (specific mask)
Quoted 2 lines
paste_lastis7c20070cmaskfc2007fe
PASTE_LAST = paste. RA,RB,L (L=1 Rc=1)
If L=1, the instruction identifies the end of a move group. If L≠Rc, the instruction form is invalid.
31 //// L RA RB 902 Rc
011111 0000 1 00000 00000 1110000110 1 = 0x7c20070d (instruction) 111111 0000 1 00000 00000 1111111111 1 = 0xfc2007ff (specific mask)
Quoted 2 lines
(this includes record form for paste; the low bit).
To make the test simple I use a combined copy, copy_first, paste and paste_last mask to compare just against copy. So that excluded:
- L
- bit 24 of 32
- Rc
111111 0000 0 00000 00000 1101111111 0 = 0xfc0006fe
Would it be better and more clear to check each instruction with its mask? Something like:
#define PPC_INST_COPY_MASK 0xfc2007fe
#define PPC_INST_PASTE_MASK 0xfc2007ff
if (cpu_has_feature(CPU_FTR_ARCH_300)) {
unsigned int masked_instruction = instruction & PPC_INST_COPY_MASK;
if (masked_instruction == PPC_INST_COPY || \
masked_instruction == PPC_INST_COPY_FIRST)
return -EIO;
masked_instruction = instruction & PPC_INST_PASTE_MASK;
if (masked_instruction == PPC_INST_PASTE || \
masked_instruction == PPC_INST_PASTE_LAST)
return -EIO;
}Thanks!
-c
Quoted 5 lines · Chris Smart
Calling ISA 3.0 instructions copy,
copy_first, paste andpaste_lastgenerates an alignment fault when copying or pasting unaligned data (128 byte). We catch this and send SIGBUS to the userspace process that caused it.
Quoted 7 lines
We do not emulate these because paste may contain additional metadata when pasting to a co-processor and
paste_lastis the synchronisation point for preceding copy/paste sequences.Thanks to Michael Neuling <[email protected]> for his help.
Signed-off-by Chris Smart <[email protected]>
Applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/ae26b36f8098c793a754549662
cheers