mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [PATCH] Fix atomic_arch.h for MIPS32 R6
Date: Mon, 28 Mar 2016 22:19:27 -0400	[thread overview]
Message-ID: <20160329021927.GK21636@brightrain.aerifal.cx> (raw)
In-Reply-To: <20160328130451.GH21636@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 2473 bytes --]

On Mon, Mar 28, 2016 at 09:04:51AM -0400, Rich Felker wrote:
> On Mon, Mar 28, 2016 at 05:07:39AM +0000, Jaydeep Patil wrote:
> > >> >I was just saying it makes the code less cluttered to use them
> > >> >spuriously even though we don't need to:
> > >> >
> > >> >		".set push ; "
> > >> >#if __mips_isa_rev < 6
> > >> >		".set mips2 ; "
> > >> >#endif
> > >> >		"ll %0, %1 ; .set pop"
> > >> >
> > >> >or similar.
> > >> >
> > >> >It's also not clear to me whether the "m" constraint is valid anymore
> > >> >for the R6 ll/sc instructions since they take a 9-bit offset now instead of a
> > >16-bit offset.
> > >> >The compiler could generate an address expression whose offset part
> > >> >does not fit in 9 bits. In that case we may need to #if the whole
> > >> >function (or at least the __asm__ statement) separately rather than just
> > >skipping the .set mips2....
> > >> >
> > >>
> > >> The "m" constrain is still valid here, as the offset will be 0 in this case..
> > >
> > >How can you assume the offset will be 0? It's the compiler's choice what to
> > >use. For instance, a_cas(&foo->bar, t, s) is likely to have an offset equal to
> > >offsetof(__typeof__(foo),bar). AFAIK this happens in practice with small
> > >offsets in mutex structures, etc. so the bug may be unlikely to be hit, but I
> > >think it's still an incorrect-constraint bug.
> > 
> > Compiler generates appropriate LL/SC based on the offset. 
> > Compiler adds the offset to the base register if it does not fit 9bits.
> 
> The compiler has no way of knowing that the operand will be used with
> ll with the 9-bit offset restriction; as far as it knows, it will be
> used in a normal context where a 16-bit offset is valid. I don't have
> a toolchain that will target r6, but you can try the following program
> which produces an offset of 4096 for loading p[1024]:
> 
> unsigned ll1k(volatile unsigned *p)
> {
> 	unsigned val;
> 	__asm__ __volatile__ ("ll %0, %1" : "=r"(val) : "m"(p[1024]) : "memory" );
> 	return val;
> }
> 
> I would expect this to produce errors at assembly time on r6.

Indeed, the ZC constraint seems to have been added to address this;
see:

https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html

It's not available on old gcc versions (pre-mipsr6) so I think the
attached patch would be appropriate. Let me know if it works for you.

We still need to add r6 detection in configure and the dynamic linker
name in reloc.h, and fix the pthread_arch.h issue.

Rich

[-- Attachment #2: mipsr6-atomic.diff --]
[-- Type: text/plain, Size: 1279 bytes --]

diff --git a/arch/mips/atomic_arch.h b/arch/mips/atomic_arch.h
index ce2823b..ad534f2 100644
--- a/arch/mips/atomic_arch.h
+++ b/arch/mips/atomic_arch.h
@@ -2,11 +2,17 @@
 static inline int a_ll(volatile int *p)
 {
 	int v;
+#if __mips_isa_rev < 6
 	__asm__ __volatile__ (
 		".set push ; .set mips2\n\t"
 		"ll %0, %1"
 		"\n\t.set pop"
 		: "=r"(v) : "m"(*p));
+#else
+	__asm__ __volatile__ (
+		"ll %0, %1"
+		: "=r"(v) : "ZC"(*p));
+#endif
 	return v;
 }
 
@@ -14,24 +20,29 @@ static inline int a_ll(volatile int *p)
 static inline int a_sc(volatile int *p, int v)
 {
 	int r;
+#if __mips_isa_rev < 6
 	__asm__ __volatile__ (
 		".set push ; .set mips2\n\t"
 		"sc %0, %1"
 		"\n\t.set pop"
 		: "=r"(r), "=m"(*p) : "0"(v) : "memory");
+#else
+	__asm__ __volatile__ (
+		"sc %0, %1"
+		: "=r"(r), "=ZC"(*p) : "0"(v) : "memory");
+#endif
 	return r;
 }
 
 #define a_barrier a_barrier
 static inline void a_barrier()
 {
+#if __mips_isa_rev < 2
 	/* mips2 sync, but using too many directives causes
 	 * gcc not to inline it, so encode with .long instead. */
 	__asm__ __volatile__ (".long 0xf" : : : "memory");
-#if 0
-	__asm__ __volatile__ (
-		".set push ; .set mips2 ; sync ; .set pop"
-		: : : "memory");
+#else
+	__asm__ __volatile__ ("sync" : : : "memory");
 #endif
 }
 

  reply	other threads:[~2016-03-29  2:19 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-21  6:03 Jaydeep Patil
2016-03-21 17:37 ` dalias
2016-03-22  4:58   ` Jaydeep Patil
2016-03-22 21:22     ` Rich Felker
2016-03-23  6:37       ` Jaydeep Patil
2016-03-23 15:03         ` Rich Felker
2016-03-28  5:07           ` Jaydeep Patil
2016-03-28 13:04             ` Rich Felker
2016-03-29  2:19               ` Rich Felker [this message]
2016-03-29  3:54               ` Jaydeep Patil
2016-03-29  4:10                 ` Rich Felker
2016-03-29  7:16                   ` Jaydeep Patil
2016-03-29 13:32                     ` Rich Felker
2016-03-30  9:45                       ` Jaydeep Patil
2016-03-30 14:29                         ` Rich Felker
2016-03-30 15:28                           ` Rich Felker
2016-03-31  5:20                             ` Jaydeep Patil
2016-03-29  3:55               ` Jaydeep Patil

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=20160329021927.GK21636@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).