diff mbox series

MIPS: Optional SYNC emulation

Message ID 20200821031228.31231-1-r@hev.cc (mailing list archive)
State New
Headers show
Series MIPS: Optional SYNC emulation | expand

Commit Message

hev Aug. 21, 2020, 3:12 a.m. UTC
MIPS ISA defines several types of memory barrier, of which Type-0 (full barrier)
is required, and the others are optional. In some vendor implementation (such as
Loongson), all optional parts are implemented to emit an illegal instruction
exception. Here, emulate to full barrier to ensure the functional semantics.

If an implementation does not support SYNC 0, it should also not support SMP, so
the `smp_mb()` is only a compilation barrier.

Signed-off-by: Heiher <r@hev.cc>
---
 arch/mips/kernel/traps.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Thomas Bogendoerfer Sept. 30, 2020, 11:16 a.m. UTC | #1
On Fri, Aug 21, 2020 at 11:12:28AM +0800, Heiher wrote:
> MIPS ISA defines several types of memory barrier, of which Type-0 (full barrier)
> is required, and the others are optional. In some vendor implementation (such as
> Loongson), all optional parts are implemented to emit an illegal instruction
> exception. Here, emulate to full barrier to ensure the functional semantics.
> 
> If an implementation does not support SYNC 0, it should also not support SMP, so
> the `smp_mb()` is only a compilation barrier.

I see your point, but isn't taking an exception already more than a
compiler barrier ? Does the missing sync hurt in real life ?

Thomas.
hev Oct. 1, 2020, 7:45 a.m. UTC | #2
Hello Thomas,

On Wed, Sep 30, 2020 at 8:48 PM Thomas Bogendoerfer
<tsbogend@alpha.franken.de> wrote:
>
> On Fri, Aug 21, 2020 at 11:12:28AM +0800, Heiher wrote:
> > MIPS ISA defines several types of memory barrier, of which Type-0 (full barrier)
> > is required, and the others are optional. In some vendor implementation (such as
> > Loongson), all optional parts are implemented to emit an illegal instruction
> > exception. Here, emulate to full barrier to ensure the functional semantics.
> >
> > If an implementation does not support SYNC 0, it should also not support SMP, so
> > the `smp_mb()` is only a compilation barrier.
>
> I see your point, but isn't taking an exception already more than a
> compiler barrier ? Does the missing sync hurt in real life ?

As far as I known, the optional sync instruction has been used in user
space programs (such as firefox), and the illegal instruction
exception does not include the semantics of the memory barrier, so if
the optional sync instruction is not simulated, the memory access
order of these programs it may be different from the order in the
code.

About the compiler barrier, What if the hardware does not support SYNC
0? I think it does not support SMP, so smp_mb() is only a compiler
barrier and will not cause infinite recursion in the simulation.

Thank you

>
> Thomas.
>
> --
> Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
> good idea.                                                [ RFC1925, 2.3 ]
Maciej W. Rozycki Nov. 2, 2020, 12:33 p.m. UTC | #3
On Thu, 1 Oct 2020, Hev wrote:

> > I see your point, but isn't taking an exception already more than a
> > compiler barrier ? Does the missing sync hurt in real life ?
> 
> As far as I known, the optional sync instruction has been used in user
> space programs (such as firefox), and the illegal instruction
> exception does not include the semantics of the memory barrier, so if
> the optional sync instruction is not simulated, the memory access
> order of these programs it may be different from the order in the
> code.

 Your concerns as to ERET not implying any of the SYNC semantics seem 
valid to me.

> About the compiler barrier, What if the hardware does not support SYNC
> 0? I think it does not support SMP, so smp_mb() is only a compiler
> barrier and will not cause infinite recursion in the simulation.

 However both SYNC 0 is mandatory (save for ancient MIPS I ISA processors) 
and other SYNC operations have to fall back to SYNC 0 if not specifically 
implemented.  Even MIPSr1 had this[1][2]:

"SYNC does not guarantee the order in which instruction fetches are 
performed.  The stype values 1-31 are reserved for future extensions to 
the architecture.  A value of zero will always be defined such that it 
performs all defined synchronization operations.  Non-zero values may be 
defined to remove some synchronization operations.  As such, software 
should never use a non-zero value of the stype field, as this may 
inadvertently cause future failures if non-zero values remove 
synchronization operations."

and MIPSr0.95 was even stricter[3][4]:

"SYNC does not guarantee the order in which instruction fetches are 
performed.  The stype values 1-31 are reserved; they produce the same 
result as the value zero."

so I suggest a board/CPU-specific workaround.

References:

[1]  "MIPS32 Architecture For Programmers, Volume II: The MIPS32
     Instruction Set", MIPS Technologies, Inc., Document Number: MD00086,
     Revision 1.00, August 29, 2002, p. 209

[2]  "MIPS64 Architecture For Programmers, Volume II: The MIPS64
     Instruction Set", MIPS Technologies, Inc., Document Number: MD00087,
     Revision 1.00, August 29, 2002, p. 295

[3]  "MIPS32 Architecture For Programmers, Volume II: The MIPS32
     Instruction Set", MIPS Technologies, Inc., Document Number: MD00086,
     Revision 0.95, March 12, 2001, p. 215

[4]  "MIPS64 Architecture For Programmers, Volume II: The MIPS64
     Instruction Set", MIPS Technologies, Inc., Document Number: MD00087,
     Revision 0.95, March 12, 2001, p. 300

 HTH,

  Maciej
diff mbox series

Patch

diff --git a/arch/mips/kernel/traps.c b/arch/mips/kernel/traps.c
index 38aa07ccdbcc..d63e8671e9d2 100644
--- a/arch/mips/kernel/traps.c
+++ b/arch/mips/kernel/traps.c
@@ -501,6 +501,7 @@  asmlinkage void do_be(struct pt_regs *regs)
 #define RD     0x0000f800
 #define FUNC   0x0000003f
 #define SYNC   0x0000000f
+#define STYPE  0x000007c0
 #define RDHWR  0x0000003b
 
 /*  microMIPS definitions   */
@@ -688,6 +689,8 @@  static int simulate_rdhwr_mm(struct pt_regs *regs, unsigned int opcode)
 static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
 {
 	if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC) {
+		if ((opcode & STYPE) != 0)
+			smp_mb();
 		perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
 				1, regs, 0);
 		return 0;