From: "Maciej W. Rozycki" <macro@mips.com> To: Fredrik Noring <noring@nocrew.org> Cc: <linux-mips@linux-mips.org> Subject: Re: [PATCH v2] MIPS: Add basic R5900 support Date: Fri, 10 Nov 2017 23:34:26 +0000 [thread overview] Message-ID: <alpine.DEB.2.00.1711102209440.10088@tp.orcam.me.uk> (raw) In-Reply-To: <20171029172016.GA2600@localhost.localdomain> Hi Fredrik, > However, it turns out that the R5900 has a grave hardware error that > appears to rule out most if not all generic MIPS distributions: > > The short loop bug under certain conditions causes loops to execute only > once or twice. GCC 2.95 that shipped with Sony PS2 Linux had a patch with > the following note: > > On the R5900, we must ensure that the compiler never generates > loops that satisfy all of the following conditions: > > - a loop consists of less than equal to six instructions > (including the branch delay slot). > - a loop contains only one conditional branch instruction at > the end of the loop. > - a loop does not contain any other branch or jump instructions. > - a branch delay slot of the loop is not NOP (EE 2.9 or later). > > We need to do this because of a bug in the chip. You'll need a `-mfix-r5900' workaround in the compiler then. One for GAS for handcoded assembly might be doable as well, fixing the `reorder' mode only and possibly bailing out if the conditions are met in the `noreorder' mode. > > originating from the IDT R4650 and the NEC Vr5500 processors. It has > > nothing to do with the DSP ASE (though it may have been claimed originally > > to be a DSP enhancement). > > The R5900 has three-operand multiply and multiply-accumulate instructions > as part of its multimedia set. Sadly, the MULT instruction format > > SPECIAL MULT > +--------+----+----+----+-------+--------+ > | 000000 | rs | rt | rd | 00000 | 011000 | > +--------+----+----+----+-------+--------+ > 6 5 5 5 5 6 > > is incompatible with the corresponding MIPS32 MUL format > > SPECIAL2 MUL > +--------+----+----+----+-------+--------+ > | 011100 | rs | rt | rd | 00000 | 000010 | > +--------+----+----+----+-------+--------+. > 6 5 5 5 5 6 Still R5900-specific code may use it. > > > > Also make sure you have RDHWR instruction emulation in place for CP0 > > > > UserLocal register access. > > > > > > Right. Debian's BusyBox has 857 of those. Jürgen Urban observed in the > > > conversation with you in > > > > > > https://gcc.gnu.org/ml/gcc-patches/2013-01/msg00658.html > > > > > > that RDHWR has the same encoding as "sq v1,-6085(zero)" for the R5900, > > > which luckily always gives an alignment exception so that the kernel is > > > able to emulate RDHWR properly. I haven't verified this though. > > > > That instruction encoding (actually implemented by some MIPS32r2/MIPS64r2 > > and newer hardware) is used under Linux for Thread Local Storage (TLS) > > access. For hardware that does not have it the instruction is emulated in > > the Reserved Instruction (RI) exception handler, but obviously not the > > Address Error Store (AdES) exception. So code to handle it as a special > > case with the R5900 has to be provided among the patches (and included > > with the initial series). > > > > Note that `rdhwr $3,$29' is the usual encoding, handled by a fastpath in > > arch/mips/kernel/genex.S (see `handle_ri_rdhwr'), however all `rt' > > encodings (covered in `simulate_rdhwr' in arch/mips/kernel/traps.c) have > > to be handled for completeness. Fortunately RDHWR and SQ both use the > > same bits for `rt', and the `-6085(zero)' encoding of the memory reference > > makes no sense, so we can safely rely on the AdES exception. > > This patch traps the RDHWR instruction as an unaligned SQ: > > diff --git a/arch/mips/include/asm/traps.h b/arch/mips/include/asm/traps.h > index f41cf3ee82a7..d4987e2d9695 100644 > --- a/arch/mips/include/asm/traps.h > +++ b/arch/mips/include/asm/traps.h > @@ -39,4 +39,6 @@ extern int register_nmi_notifier(struct notifier_block *nb); > register_nmi_notifier(&fn##_nb); \ > }) > > +asmlinkage void do_ri(struct pt_regs *regs); > + > #endif /* _ASM_TRAPS_H */ > diff --git a/arch/mips/kernel/unaligned.c b/arch/mips/kernel/unaligned.c > index f806ee56e639..7303d5d5cac8 100644 > --- a/arch/mips/kernel/unaligned.c > +++ b/arch/mips/kernel/unaligned.c > @@ -89,6 +89,7 @@ > #include <asm/fpu.h> > #include <asm/fpu_emulator.h> > #include <asm/inst.h> > +#include <asm/traps.h> > #include <linux/uaccess.h> > > #define STR(x) __STR(x) > @@ -1309,6 +1310,35 @@ static void emulate_load_store_insn(struct pt_regs *regs, > cu2_notifier_call_chain(CU2_SDC2_OP, regs); > break; > #endif > + > +#ifdef CONFIG_CPU_R5900 It might be preferable to use: if (IS_ENABLED(CONFIG_CPU_R5900)) instead. > + case spec3_op: There is already a `spec3_op' case in this `switch' statement, so you need to fold your code into it (have you actually successfully built this piece before posting?). > + /* > + * On the R5900 the RDHWR instruction > + * > + * +--------+-------+----+----+-------+--------+ > + * | 011111 | 00000 | rt | rd | 00000 | 111011 | > + * +--------+-------+----+----+-------+--------+ > + * 6 5 5 5 5 6 > + * > + * is interpreted as the R5900 specific SQ instruction > + * > + * +--------+-------+----+---------------------+ > + * | 011111 | base | rt | offset | > + * +--------+-------+----+---------------------+ > + * 6 5 5 16 > + * > + * with an odd offset based on $0 that always yields an > + * address error exception. Hence RDHWR can be trapped > + * and emulated here. > + */ > + if (insn.spec3_format.func == rdhwr_op) { I think `r_format' is more appropriate for RDHWR (`spec3_format' really matches EVA instructions only; we might invent a distinct new format for the BSHFL, DBSHFL and RDHWR minor opcodes, but I think this would be an overkill) and you need to qualify the other instruction fields, i.e. `rs' and `re', because of the overlap with SQ. We only want to give the special exception for what looks like a real RDHWR instruction and not just any faulting SQ whose least significant bits of the offset happen to match the RDHWR minor opcode. > + do_ri(regs); Or rather `simulate_rdhwr(regs, insn.r_format.rd, insn.r_format.rt)' as we've already qualified it. > + return; > + } > + goto sigill; This I think should be `sigbus' as the SQ opcode is valid on the R5900. Maciej
WARNING: multiple messages have this Message-ID (diff)
From: "Maciej W. Rozycki" <macro@mips.com> To: Fredrik Noring <noring@nocrew.org> Cc: linux-mips@linux-mips.org Subject: Re: [PATCH v2] MIPS: Add basic R5900 support Date: Fri, 10 Nov 2017 23:34:26 +0000 [thread overview] Message-ID: <alpine.DEB.2.00.1711102209440.10088@tp.orcam.me.uk> (raw) Message-ID: <20171110233426.ka8rS1MKeWdoH6LXpZ19zRBLMbpkLEW93dfMCEP6oMY@z> (raw) In-Reply-To: <20171029172016.GA2600@localhost.localdomain> Hi Fredrik, > However, it turns out that the R5900 has a grave hardware error that > appears to rule out most if not all generic MIPS distributions: > > The short loop bug under certain conditions causes loops to execute only > once or twice. GCC 2.95 that shipped with Sony PS2 Linux had a patch with > the following note: > > On the R5900, we must ensure that the compiler never generates > loops that satisfy all of the following conditions: > > - a loop consists of less than equal to six instructions > (including the branch delay slot). > - a loop contains only one conditional branch instruction at > the end of the loop. > - a loop does not contain any other branch or jump instructions. > - a branch delay slot of the loop is not NOP (EE 2.9 or later). > > We need to do this because of a bug in the chip. You'll need a `-mfix-r5900' workaround in the compiler then. One for GAS for handcoded assembly might be doable as well, fixing the `reorder' mode only and possibly bailing out if the conditions are met in the `noreorder' mode. > > originating from the IDT R4650 and the NEC Vr5500 processors. It has > > nothing to do with the DSP ASE (though it may have been claimed originally > > to be a DSP enhancement). > > The R5900 has three-operand multiply and multiply-accumulate instructions > as part of its multimedia set. Sadly, the MULT instruction format > > SPECIAL MULT > +--------+----+----+----+-------+--------+ > | 000000 | rs | rt | rd | 00000 | 011000 | > +--------+----+----+----+-------+--------+ > 6 5 5 5 5 6 > > is incompatible with the corresponding MIPS32 MUL format > > SPECIAL2 MUL > +--------+----+----+----+-------+--------+ > | 011100 | rs | rt | rd | 00000 | 000010 | > +--------+----+----+----+-------+--------+. > 6 5 5 5 5 6 Still R5900-specific code may use it. > > > > Also make sure you have RDHWR instruction emulation in place for CP0 > > > > UserLocal register access. > > > > > > Right. Debian's BusyBox has 857 of those. Jürgen Urban observed in the > > > conversation with you in > > > > > > https://gcc.gnu.org/ml/gcc-patches/2013-01/msg00658.html > > > > > > that RDHWR has the same encoding as "sq v1,-6085(zero)" for the R5900, > > > which luckily always gives an alignment exception so that the kernel is > > > able to emulate RDHWR properly. I haven't verified this though. > > > > That instruction encoding (actually implemented by some MIPS32r2/MIPS64r2 > > and newer hardware) is used under Linux for Thread Local Storage (TLS) > > access. For hardware that does not have it the instruction is emulated in > > the Reserved Instruction (RI) exception handler, but obviously not the > > Address Error Store (AdES) exception. So code to handle it as a special > > case with the R5900 has to be provided among the patches (and included > > with the initial series). > > > > Note that `rdhwr $3,$29' is the usual encoding, handled by a fastpath in > > arch/mips/kernel/genex.S (see `handle_ri_rdhwr'), however all `rt' > > encodings (covered in `simulate_rdhwr' in arch/mips/kernel/traps.c) have > > to be handled for completeness. Fortunately RDHWR and SQ both use the > > same bits for `rt', and the `-6085(zero)' encoding of the memory reference > > makes no sense, so we can safely rely on the AdES exception. > > This patch traps the RDHWR instruction as an unaligned SQ: > > diff --git a/arch/mips/include/asm/traps.h b/arch/mips/include/asm/traps.h > index f41cf3ee82a7..d4987e2d9695 100644 > --- a/arch/mips/include/asm/traps.h > +++ b/arch/mips/include/asm/traps.h > @@ -39,4 +39,6 @@ extern int register_nmi_notifier(struct notifier_block *nb); > register_nmi_notifier(&fn##_nb); \ > }) > > +asmlinkage void do_ri(struct pt_regs *regs); > + > #endif /* _ASM_TRAPS_H */ > diff --git a/arch/mips/kernel/unaligned.c b/arch/mips/kernel/unaligned.c > index f806ee56e639..7303d5d5cac8 100644 > --- a/arch/mips/kernel/unaligned.c > +++ b/arch/mips/kernel/unaligned.c > @@ -89,6 +89,7 @@ > #include <asm/fpu.h> > #include <asm/fpu_emulator.h> > #include <asm/inst.h> > +#include <asm/traps.h> > #include <linux/uaccess.h> > > #define STR(x) __STR(x) > @@ -1309,6 +1310,35 @@ static void emulate_load_store_insn(struct pt_regs *regs, > cu2_notifier_call_chain(CU2_SDC2_OP, regs); > break; > #endif > + > +#ifdef CONFIG_CPU_R5900 It might be preferable to use: if (IS_ENABLED(CONFIG_CPU_R5900)) instead. > + case spec3_op: There is already a `spec3_op' case in this `switch' statement, so you need to fold your code into it (have you actually successfully built this piece before posting?). > + /* > + * On the R5900 the RDHWR instruction > + * > + * +--------+-------+----+----+-------+--------+ > + * | 011111 | 00000 | rt | rd | 00000 | 111011 | > + * +--------+-------+----+----+-------+--------+ > + * 6 5 5 5 5 6 > + * > + * is interpreted as the R5900 specific SQ instruction > + * > + * +--------+-------+----+---------------------+ > + * | 011111 | base | rt | offset | > + * +--------+-------+----+---------------------+ > + * 6 5 5 16 > + * > + * with an odd offset based on $0 that always yields an > + * address error exception. Hence RDHWR can be trapped > + * and emulated here. > + */ > + if (insn.spec3_format.func == rdhwr_op) { I think `r_format' is more appropriate for RDHWR (`spec3_format' really matches EVA instructions only; we might invent a distinct new format for the BSHFL, DBSHFL and RDHWR minor opcodes, but I think this would be an overkill) and you need to qualify the other instruction fields, i.e. `rs' and `re', because of the overlap with SQ. We only want to give the special exception for what looks like a real RDHWR instruction and not just any faulting SQ whose least significant bits of the offset happen to match the RDHWR minor opcode. > + do_ri(regs); Or rather `simulate_rdhwr(regs, insn.r_format.rd, insn.r_format.rt)' as we've already qualified it. > + return; > + } > + goto sigill; This I think should be `sigbus' as the SQ opcode is valid on the R5900. Maciej
next prev parent reply other threads:[~2017-11-10 23:34 UTC|newest] Thread overview: 117+ messages / expand[flat|nested] mbox.gz Atom feed top 2017-08-27 13:23 [PATCH] MIPS: Add basic R5900 support Fredrik Noring 2017-08-28 13:53 ` Ralf Baechle 2017-08-28 17:11 ` Maciej W. Rozycki 2017-08-29 17:33 ` Fredrik Noring 2017-08-29 17:24 ` Maciej W. Rozycki 2017-08-29 17:24 ` Maciej W. Rozycki 2017-08-30 13:23 ` Fredrik Noring 2017-08-31 15:11 ` Maciej W. Rozycki 2017-08-31 15:11 ` Maciej W. Rozycki 2017-09-02 10:28 ` Fredrik Noring 2017-09-09 10:13 ` Maciej W. Rozycki 2017-09-09 10:13 ` Maciej W. Rozycki 2017-09-11 5:21 ` Maciej W. Rozycki 2017-09-11 5:21 ` Maciej W. Rozycki 2017-09-12 17:59 ` Fredrik Noring 2017-09-15 11:12 ` Maciej W. Rozycki 2017-09-15 11:12 ` Maciej W. Rozycki 2017-09-15 13:19 ` Fredrik Noring 2017-09-15 18:28 ` Maciej W. Rozycki 2017-09-15 18:28 ` Maciej W. Rozycki 2017-09-02 14:10 ` [PATCH v2] " Fredrik Noring 2017-09-11 5:18 ` Maciej W. Rozycki 2017-09-11 5:18 ` Maciej W. Rozycki 2017-09-11 15:17 ` Fredrik Noring 2017-09-14 13:50 ` Maciej W. Rozycki 2017-09-14 13:50 ` Maciej W. Rozycki 2017-09-16 13:34 ` Fredrik Noring 2017-09-18 17:05 ` Maciej W. Rozycki 2017-09-18 17:05 ` Maciej W. Rozycki 2017-09-18 19:24 ` Fredrik Noring 2017-09-19 12:44 ` Maciej W. Rozycki 2017-09-19 12:44 ` Maciej W. Rozycki 2017-09-20 14:54 ` Fredrik Noring 2017-09-26 11:50 ` Maciej W. Rozycki 2017-09-26 11:50 ` Maciej W. Rozycki 2017-09-27 17:21 ` Fredrik Noring 2017-09-28 12:13 ` Maciej W. Rozycki 2017-09-28 12:13 ` Maciej W. Rozycki 2017-09-30 6:56 ` Fredrik Noring 2017-10-02 9:05 ` Maciej W. Rozycki 2017-10-02 9:05 ` Maciej W. Rozycki 2017-10-02 16:33 ` Fredrik Noring 2017-10-29 17:20 ` Fredrik Noring 2017-11-10 23:34 ` Maciej W. Rozycki [this message] 2017-11-10 23:34 ` Maciej W. Rozycki 2017-11-11 16:04 ` Fredrik Noring 2018-01-29 20:27 ` Fredrik Noring 2018-01-31 23:01 ` Maciej W. Rozycki 2018-02-11 7:29 ` [RFC] MIPS: R5900: Workaround for the short loop bug Fredrik Noring 2018-02-12 9:25 ` Maciej W. Rozycki 2018-02-12 15:22 ` Fredrik Noring 2018-02-11 7:46 ` [RFC] MIPS: R5900: Use SYNC.L for data cache and SYNC.P for instruction cache Fredrik Noring 2018-02-11 7:56 ` [RFC] MIPS: R5900: Workaround exception NOP execution bug (FLX05) Fredrik Noring 2018-02-12 9:28 ` Maciej W. Rozycki 2018-02-15 19:15 ` [RFC v2] " Fredrik Noring 2018-02-15 20:49 ` Maciej W. Rozycki 2018-02-17 11:16 ` Fredrik Noring 2018-02-17 11:57 ` Maciej W. Rozycki 2018-02-17 13:38 ` Fredrik Noring 2018-02-17 15:03 ` Maciej W. Rozycki 2018-02-17 20:04 ` Fredrik Noring 2018-02-20 14:09 ` Maciej W. Rozycki 2018-02-22 17:04 ` Fredrik Noring 2018-02-18 8:47 ` Fredrik Noring 2018-02-20 14:41 ` Maciej W. Rozycki 2018-02-22 17:27 ` Fredrik Noring 2018-02-11 8:01 ` [RFC] MIPS: R5900: Workaround for CACHE instruction near branch delay slot Fredrik Noring 2018-02-11 11:16 ` Aw: " "Jürgen Urban" 2018-02-11 8:09 ` [RFC] MIPS: R5900: The ERET instruction has issues with delay slot and CACHE Fredrik Noring 2018-02-11 11:07 ` Aw: " "Jürgen Urban" 2018-02-11 8:29 ` [RFC] MIPS: R5900: Use mandatory SYNC.L in exception handlers Fredrik Noring 2018-02-11 10:33 ` Aw: " "Jürgen Urban" 2018-02-12 9:22 ` Maciej W. Rozycki 2018-02-12 9:22 ` Maciej W. Rozycki 2018-02-18 10:30 ` Fredrik Noring 2018-02-17 14:43 ` [RFC] MIPS: R5900: Workaround for saving and restoring FPU registers Fredrik Noring 2018-02-17 15:18 ` Maciej W. Rozycki 2018-02-17 17:47 ` Fredrik Noring 2018-02-17 19:33 ` Maciej W. Rozycki 2018-02-18 9:26 ` [RFC] MIPS: R5900: Workaround where MSB must be 0 for the instruction cache Fredrik Noring 2018-02-18 11:08 ` [RFC] MIPS: R5900: Add mandatory SYNC.P to all M[FT]C0 instructions Fredrik Noring 2018-03-03 12:26 ` [RFC] MIPS: PS2: Interrupt request (IRQ) support Fredrik Noring 2018-03-03 13:09 ` Maciej W. Rozycki 2018-03-03 14:14 ` Fredrik Noring 2018-04-09 15:51 ` Fredrik Noring 2018-03-18 10:45 ` Fredrik Noring 2018-03-19 19:15 ` Thomas Gleixner 2018-06-18 18:52 ` [RFC v2] " Fredrik Noring 2017-10-30 17:55 ` [PATCH v2] MIPS: Add basic R5900 support Fredrik Noring 2017-11-24 10:26 ` Maciej W. Rozycki 2017-11-24 10:26 ` Maciej W. Rozycki 2017-11-24 10:39 ` Maciej W. Rozycki 2017-11-24 10:39 ` Maciej W. Rozycki 2017-09-20 14:07 ` Fredrik Noring 2017-09-21 21:07 ` Maciej W. Rozycki 2017-09-21 21:07 ` Maciej W. Rozycki 2017-09-22 16:37 ` Fredrik Noring 2017-09-22 16:37 ` Fredrik Noring 2017-09-29 23:55 ` Maciej W. Rozycki 2017-09-29 23:55 ` Maciej W. Rozycki 2017-09-30 18:26 ` Fredrik Noring 2017-10-02 9:11 ` Maciej W. Rozycki 2017-10-02 9:11 ` Maciej W. Rozycki 2017-10-03 19:49 ` Fredrik Noring 2017-10-05 19:04 ` Fredrik Noring 2017-10-06 20:28 ` Fredrik Noring 2017-10-15 16:39 ` Fredrik Noring 2017-10-17 12:23 ` Maciej W. Rozycki 2017-10-17 12:23 ` Maciej W. Rozycki 2017-10-21 18:00 ` Fredrik Noring 2017-10-23 16:10 ` Maciej W. Rozycki 2017-10-23 16:10 ` Maciej W. Rozycki 2017-09-21 18:11 ` Paul Burton 2017-09-21 18:11 ` Paul Burton 2017-09-21 19:48 ` Maciej W. Rozycki 2017-09-21 19:48 ` Maciej W. Rozycki 2017-10-29 18:42 ` Fredrik Noring
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=alpine.DEB.2.00.1711102209440.10088@tp.orcam.me.uk \ --to=macro@mips.com \ --cc=linux-mips@linux-mips.org \ --cc=noring@nocrew.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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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.