Skip to contentSkip to conversation

PATCH: Fix ll/sc for mips

PATCH In review H . J . Lu 20 participants 87 messages Jan 31, 2002 – Jul 30, 2002 6 messages not in the archive MIPS

lore
Share & tools
lore

H . J . Lu

author
#0 lore raw

I believe this patch fixes all the ll/sc bugs I found in glibc. Any comments?

Thanks.

H.J.
----
2002-01-31 H.J. Lu <[email protected]>

* ysdeps/mips/pspinlock.c (__pthread_spin_lock): Use a different register in the delayed slot.

* sysdeps/mips/pt-machine.h (testandset): Call _test_and_set. (__compare_and_swap): Return 0 when failed to compare or swap.

2002-01-31 H.J. Lu <[email protected]>

* sysdeps/mips/atomicity.h (compare_and_swap): Return 0 when failed to compare or swap.

* sysdeps/unix/sysv/linux/mips/sys/tas.h (_test_and_set): Fill the delay slot.

linuxthreads/sysdeps/mips/pspinlock.c renamed from linuxthreads/sysdeps/mips/pspinlock.c.llsc +10 -9
@@ -29,20 +29,21 @@
 int
 __pthread_spin_lock (pthread_spinlock_t *lock)
 {
-  unsigned int tmp;
+  unsigned int tmp1, tmp2;
 
   asm volatile
     ("\t\t\t# spin_lock\n\t"
-     "1:\n\t"
-     "ll	%1,%2\n\t"
      ".set	push\n\t"
-     ".set	noreorder\n\t"
+     ".set	noreorder\n"
+     "1:\n\t"
+     "ll	%1,%3\n\t"
      "bnez	%1,1b\n\t"
-     " li	%1,1\n\t"
-     ".set	pop\n\t"
-     "sc	%1,%0\n\t"
-     "beqz	%1,1b"
-     : "=m" (*lock), "=&r" (tmp)
+     "li	%2,1\n\t"
+     "sc	%2,%0\n\t"
+     "beqz	%2,1b\n\t"
+     "nop\n\t"
+     ".set	pop"
+     : "=m" (*lock), "=&r" (tmp1), "=&r" (tmp2)
      : "m" (*lock)
      : "memory");
 
linuxthreads/sysdeps/mips/pt-machine.h renamed from linuxthreads/sysdeps/mips/pt-machine.h.llsc +9 -40

72 lines not on this page · raw message

sysdeps/mips/atomicity.h renamed from sysdeps/mips/atomicity.h.llsc +19 -12
@@ -32,12 +32,16 @@ exchange_and_add (volatile uint32_t *mem
   int result, tmp;
 
   __asm__ __volatile__
-    ("/* Inline exchange & add */\n\t"
+    ("/* Inline exchange & add */\n"
      "1:\n\t"
      "ll	%0,%3\n\t"
      "addu	%1,%4,%0\n\t"
      "sc	%1,%2\n\t"
+     ".set	push\n\t"
+     ".set	noreorder\n\t"
      "beqz	%1,1b\n\t"
+     "nop\n\t"
+     ".set	pop\n\t"
      "/* End exchange & add */"
      : "=&r"(result), "=&r"(tmp), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -53,12 +57,16 @@ atomic_add (volatile uint32_t *mem, int 
   int result;
 
   __asm__ __volatile__
-    ("/* Inline atomic add */\n\t"
+    ("/* Inline atomic add */\n"
      "1:\n\t"
      "ll	%0,%2\n\t"
      "addu	%0,%3,%0\n\t"
      "sc	%0,%1\n\t"
+     ".set	push\n\t"
+     ".set	noreorder\n\t"
      "beqz	%0,1b\n\t"
+     "nop\n\t"
+     ".set	pop\n\t"
      "/* End atomic add */"
      : "=&r"(result), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -69,22 +77,21 @@ static inline int
 __attribute__ ((unused))
 compare_and_swap (volatile long int *p, long int oldval, long int newval)
 {
-  long int ret;
+  long int ret, temp;
 
   __asm__ __volatile__
     ("/* Inline compare & swap */\n\t"
-     "1:\n\t"
-     "ll	%0,%4\n\t"
-     ".set	push\n"
+     "ll	%1,%5\n\t"
+     ".set	push\n\t"
      ".set	noreorder\n\t"
-     "bne	%0,%2,2f\n\t"
-     "move	%0,%3\n\t"
+     "bne	%1,%3,1f\n\t"
+     "move	%0,$0\n\t"
      ".set	pop\n\t"
-     "sc	%0,%1\n\t"
-     "beqz	%0,1b\n"
-     "2:\n\t"
+     "move	%0,%4\n\t"
+     "sc	%0,%2\n"
+     "1:\n\t"
      "/* End compare & swap */"
-     : "=&r" (ret), "=m" (*p)
+     : "=&r" (ret), "=&r" (temp), "=m" (*p)
      : "r" (oldval), "r" (newval), "m" (*p)
      : "memory");
 
sysdeps/unix/sysv/linux/mips/sys/tas.h renamed from sysdeps/unix/sysv/linux/mips/sys/tas.h.llsc +8 -7
@@ -42,16 +42,17 @@ _test_and_set (int *p, int v) __THROW
   int r, t;
 
   __asm__ __volatile__
-    ("1:\n\t"
+    (".set	push\n\t"
+     ".set	noreorder\n"
+     "1:\n\t"
      "ll	%0,%3\n\t"
-     ".set	push\n\t"
-     ".set	noreorder\n\t"
      "beq	%0,%4,2f\n\t"
-     " move	%1,%4\n\t"
-     ".set	pop\n\t"
+     "move	%1,%4\n\t"
      "sc	%1,%2\n\t"
-     "beqz	%1,1b\n"
-     "2:\n"
+     "beqz	%1,1b\n\t"
+     "nop\n"
+     "2:\n\t"
+     ".set	pop"
      : "=&r" (r), "=&r" (t), "=m" (*p)
      : "m" (*p), "r" (v)
      : "memory");
#1 lore raw
Quoted 1 line · H . J . Lu

(__compare_and_swap): Return 0 when failed to compare or swap.

[...]

Quoted 2 lines

* sysdeps/mips/atomicity.h (compare_and_swap): Return 0 when failed to compare or swap.

Looking at the i486 implementation these are not expected to fail.
Unless I am missing something...

Quoted 2 lines

* sysdeps/unix/sysv/linux/mips/sys/tas.h (_test_and_set): Fill the delay slot.

What's the difference? The code looks the same after changes. Also you
forgot to indent instructions in delay slots, which worsens readability.

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#2 lore raw
Quoted 8 lines · Maciej W. Rozycki

Date: Thu, 31 Jan 2002 23:17:21 +0100 (MET)
From: Maciej W. Rozycki <[email protected]>
To: H . J . Lu <[email protected]>
Cc: GNU C Library <[email protected]>, [email protected]
Subject: [libc-alpha] Re: PATCH: Fix ll/sc for mips

On Thu, 31 Jan 2002, H . J . Lu wrote:

Quoted 7 lines
Quoted 1 line

(__compare_and_swap): Return 0 when failed to compare or swap.

[...]

Quoted 2 lines

* sysdeps/mips/atomicity.h (compare_and_swap): Return 0 when failed to compare or swap.

Looking at the i486 implementation these are not expected to fail.
Unless I am missing something...

That's what ``compare'' means in ``compare and swap''. You lock the memory location at some hardware level and then compare the location to the specified value. If there is a match, you change the memory location to the new value. Otherwise you don't, and indicate that you didn't. That's what ``fail'' means. The comparison failed, and the operation failed to install the new value. The caller must detect this and handle that. Algorithms based on atomic compare-and-swap take failure into account.

author
#3 lore raw
In reply to Maciej W. Rozycki
Quoted 9 lines · Maciej W. Rozycki
Quoted 1 line · H . J . Lu

(__compare_and_swap): Return 0 when failed to compare or swap.

[...]

Quoted 2 lines

* sysdeps/mips/atomicity.h (compare_and_swap): Return 0 when failed to compare or swap.

Looking at the i486 implementation these are not expected to fail.
Unless I am missing something...

Why can't it fail?

static inline char
__attribute__ ((unused))
compare_and_swap (volatile long int *p, long int oldval, long int newval) {
char ret;
long int readval;

__asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"

                        : "=q" (ret), "=m" (*p), "=a" (readval)
                        : "r" (newval), "1" (*p), "a" (oldval));

return ret;
}

It will fail if *p is not same as oldval.

Quoted 6 lines
Quoted 3 lines

* sysdeps/unix/sysv/linux/mips/sys/tas.h (_test_and_set): Fill the delay slot.

What's the difference? The code looks the same after changes. Also you
forgot to indent instructions in delay slots, which worsens readability.

Are we looking at the same file? Here is the patched version:

_EXTERN_INLINE int
_test_and_set (int *p, int v) __THROW
{
int r, t;

__asm__ __volatile__

    (".set      push\n\t"
     ".set      noreorder\n"
     "1:\n\t"
     "ll        %0,%3\n\t"
     "beq       %0,%4,2f\n\t"
     "move      %1,%4\n\t"
     "sc        %1,%2\n\t"
     "beqz      %1,1b\n\t"
     "nop\n" 
     "2:\n\t"
     ".set      pop"    
     : "=&r" (r), "=&r" (t), "=m" (*p)
     : "m" (*p), "r" (v) 
     : "memory");

return r;
}

H.J.

#5 lore raw
In reply to Hiroyuki Machida
Quoted 5 lines · Hiroyuki Machida

Please note that "sc" may fail even if nobody write the variable. (See P.211 "8.4.2 Load-Linked/Sotre-Conditional" of "See MIPS RUN" for more detail.)
So, after your patch applied, compare_and_swap() may fail, even if *p is equal to oldval.

I can't think of anything that will break because of this, as long as the compare_and_swap eventually succeeds on some subsequent trial. If the atomic operation has to abort for some reason other than *p being unequal to oldval, that should be cool.

#4 lore raw
In reply to H . J . Lu

From: "H . J . Lu" <[email protected]>
Subject: Re: PATCH: Fix ll/sc for mips
Date: Thu, 31 Jan 2002 14:41:00 -0800

Quoted 20 lines · Maciej W. Rozycki
Quoted 9 lines · H . J . Lu
Quoted 2 lines

(__compare_and_swap): Return 0 when failed to compare or swap.

[...]

Quoted 2 lines

* sysdeps/mips/atomicity.h (compare_and_swap): Return 0 when failed to compare or swap.

Looking at the i486 implementation these are not expected to fail.
Unless I am missing something...

Why can't it fail?

static inline char
__attribute__ ((unused))
compare_and_swap (volatile long int *p, long int oldval, long int newval) {
char ret;
long int readval;

Quoted 7 lines

__asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"

                        : "=q" (ret), "=m" (*p), "=a" (readval)
                        : "r" (newval), "1" (*p), "a" (oldval));

return ret;
}

It will fail if *p is not same as oldval.

Please note that "sc" may fail even if nobody write the variable. (See P.211 "8.4.2 Load-Linked/Sotre-Conditional" of "See MIPS RUN" for more detail.)
So, after your patch applied, compare_and_swap() may fail, even if *p is equal to oldval.

Quoted 30 lines
Quoted 6 lines
Quoted 3 lines

* sysdeps/unix/sysv/linux/mips/sys/tas.h (_test_and_set): Fill the delay slot.

What's the difference? The code looks the same after changes. Also you
forgot to indent instructions in delay slots, which worsens readability.

Are we looking at the same file? Here is the patched version:

_EXTERN_INLINE int
_test_and_set (int *p, int v) __THROW
{
int r, t;

__asm__ __volatile__

    (".set      push\n\t"
     ".set      noreorder\n"
     "1:\n\t"
     "ll        %0,%3\n\t"
     "beq       %0,%4,2f\n\t"
     "move      %1,%4\n\t"
     "sc        %1,%2\n\t"
     "beqz      %1,1b\n\t"
     "nop\n" 
     "2:\n\t"
     ".set      pop"    
     : "=&r" (r), "=&r" (t), "=m" (*p)
     : "m" (*p), "r" (v) 
     : "memory");
Quoted 2 lines

return r;
}

Gas will fill delay slots. Same object codes will be produced, so I think you don't have to do that by hand.

---
Hiroyuki Machida
Sony Corp.

#6 lore raw
In reply to Kaz Kylheku

From: Kaz Kylheku <[email protected]>
Subject: Re: [libc-alpha] Re: PATCH: Fix ll/sc for mips
Date: Thu, 31 Jan 2002 20:02:25 -0800 (PST)

Quoted 11 lines · Hiroyuki Machida
Quoted 5 lines

Please note that "sc" may fail even if nobody write the variable. (See P.211 "8.4.2 Load-Linked/Sotre-Conditional" of "See MIPS RUN" for more detail.)
So, after your patch applied, compare_and_swap() may fail, even if *p is equal to oldval.

I can't think of anything that will break because of this, as long as the compare_and_swap eventually succeeds on some subsequent trial. If the atomic operation has to abort for some reason other than *p being unequal to oldval, that should be cool.

I mean that this patch breaks the spec of compare_and_swap().

In most case, this patch may works as Kaz said. If this patch have no side-effect to any application, it's ok to apply the patch. But we can't know how to use compare_and_swap() in all aplications in a whole world. So we have to follow the spec.

---
Hiroyuki Machida
Sony Corp.

author
#7 lore raw
Quoted 17 lines · Hiroyuki Machida

From: Kaz Kylheku <[email protected]>
Subject: Re: [libc-alpha] Re: PATCH: Fix ll/sc for mips
Date: Thu, 31 Jan 2002 20:02:25 -0800 (PST)

Quoted 11 lines · Hiroyuki Machida
Quoted 5 lines

Please note that "sc" may fail even if nobody write the variable. (See P.211 "8.4.2 Load-Linked/Sotre-Conditional" of "See MIPS RUN" for more detail.)
So, after your patch applied, compare_and_swap() may fail, even if *p is equal to oldval.

I can't think of anything that will break because of this, as long as the compare_and_swap eventually succeeds on some subsequent trial. If the atomic operation has to abort for some reason other than *p being unequal to oldval, that should be cool.

Quoted 6 lines

I mean that this patch breaks the spec of compare_and_swap(). In most case, this patch may works as Kaz said. If this patch have no side-effect to any application, it's ok to apply the patch. But we can't know how to use compare_and_swap() in all aplications in a whole world. So we have to follow the spec.

Please note that the old compare_and_swap is broken. If you use compare_and_swap to check if *p == oldval, my patch doesn't help you. But if you use it to swap old/new, my patch works fine. But I don't think you can use it check if *p == oldval since *p can change at any time. It is the same as simply using "*p == oldval". I don't see my patch should break any sane applications.

H.J.

author
#11 lore raw
In reply to Kaz Kylheku
Quoted 11 lines · Kaz Kylheku
Quoted 5 lines · Hiroyuki Machida

Please note that "sc" may fail even if nobody write the variable. (See P.211 "8.4.2 Load-Linked/Sotre-Conditional" of "See MIPS RUN" for more detail.)
So, after your patch applied, compare_and_swap() may fail, even if *p is equal to oldval.

I can't think of anything that will break because of this, as long as the compare_and_swap eventually succeeds on some subsequent trial. If the atomic operation has to abort for some reason other than *p being unequal to oldval, that should be cool.

Maybe we should document it in glibc, something like

compare_and_swap compares the contents of a variable with an old value. If the values are equal and a new value is stored in the variable atomically, 1 is returned; otherwise, 1 is returned.

H.J.

author
#12 lore raw
In reply to Hiroyuki Machida
Quoted 20 lines · Hiroyuki Machida
Quoted 20 lines

_test_and_set (int *p, int v) __THROW
{
int r, t;

__asm__ __volatile__

    (".set      push\n\t"
     ".set      noreorder\n"
     "1:\n\t"
     "ll        %0,%3\n\t"
     "beq       %0,%4,2f\n\t"
     "move      %1,%4\n\t"
     "sc        %1,%2\n\t"
     "beqz      %1,1b\n\t"
     "nop\n" 
     "2:\n\t"
     ".set      pop"    
     : "=&r" (r), "=&r" (t), "=m" (*p)
     : "m" (*p), "r" (v) 
     : "memory");
Quoted 5 lines
Quoted 2 lines

return r;
}

Gas will fill delay slots. Same object codes will be produced, so I think you don't have to do that by hand.

It will make the code more readable. We don't have to guess what the assembler will do.

H.J.

#9 lore raw
In reply to Hiroyuki Machida

Hiroyuki Machida <[email protected]> writes:

|> From: Kaz Kylheku <[email protected]>
|> Subject: Re: [libc-alpha] Re: PATCH: Fix ll/sc for mips
|> Date: Thu, 31 Jan 2002 20:02:25 -0800 (PST)
|> 
|> > On Fri, 1 Feb 2002, Hiroyuki Machida wrote:
|> > > Please note that "sc" may fail even if nobody write the
|> > > variable. (See P.211 "8.4.2 Load-Linked/Sotre-Conditional" of "See 
|> > > MIPS RUN" for more detail.) 
|> > > So, after your patch applied, compare_and_swap() may fail, even if
|> > > *p is equal to oldval.
|> > 
|> > I can't think of anything that will break because of this, as long
|> > as the compare_and_swap eventually succeeds on some subsequent trial.
|> > If the atomic operation has to abort for some reason other than *p being
|> > unequal to oldval, that should be cool.
|> 
|> I mean that this patch breaks the spec of compare_and_swap().
|> 
|> In most case, this patch may works as Kaz said. If this patch have
|> no side-effect to any application, it's ok to apply the patch. But
|> we can't know how to use compare_and_swap() in all aplications in a
|> whole world. So we have to follow the spec.  

There is no way to find out anything about intermediate values of *p when compare_and_swap returns zero. The value of *p can change anytime, even if it only was different from oldval just at the time compare_and_swap did the comparison. So there is zero chance that a spurious failure of compare_and_swap breaks anything.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE GmbH, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#8 lore raw
In reply to H . J . Lu

From: "H . J . Lu" <[email protected]>
Subject: Re: [libc-alpha] Re: PATCH: Fix ll/sc for mips
Date: Thu, 31 Jan 2002 23:00:50 -0800

Quoted 33 lines · Hiroyuki Machida
Quoted 17 lines

From: Kaz Kylheku <[email protected]>
Subject: Re: [libc-alpha] Re: PATCH: Fix ll/sc for mips
Date: Thu, 31 Jan 2002 20:02:25 -0800 (PST)

Quoted 11 lines · Hiroyuki Machida
Quoted 5 lines

Please note that "sc" may fail even if nobody write the variable. (See P.211 "8.4.2 Load-Linked/Sotre-Conditional" of "See MIPS RUN" for more detail.)
So, after your patch applied, compare_and_swap() may fail, even if *p is equal to oldval.

I can't think of anything that will break because of this, as long as the compare_and_swap eventually succeeds on some subsequent trial. If the atomic operation has to abort for some reason other than *p being unequal to oldval, that should be cool.

Quoted 6 lines

I mean that this patch breaks the spec of compare_and_swap(). In most case, this patch may works as Kaz said. If this patch have no side-effect to any application, it's ok to apply the patch. But we can't know how to use compare_and_swap() in all aplications in a whole world. So we have to follow the spec.

Please note that the old compare_and_swap is broken. If you use compare_and_swap to check if *p == oldval, my patch doesn't help you. But if you use it to swap old/new, my patch works fine. But I don't think you can use it check if *p == oldval since *p can change at any time. It is the same as simply using "*p == oldval". I don't see my patch should break any sane applications.

Quoted 2 lines

H.J.

I know the orinal compare_and_swap() is bad, and I believe the spec of compare_and_swap() as below;

compare_and_swap(p, oldval, newval)

{
	retval = 0;
	begin_atomic
	if (*p==oldval) {
	   *p = newval;
	   retval = 1;
	}
	end_atomic
	return retval;
}

So, compare_and_swap() should be ...

__compare_and_swap (a0 long int oldval, long int newval)

a0: *p
a1: oldval
a2: newval
v0: return value

.set noreorder
retry:

     ll		v0, (a0)
     bne	v0, a1
      move	v0, zero
     move	v0, a2
     sc		v0, (a0)
     beqz	v0, retry
      nop
    j		ra

But, with your patch ...

     .set	noreorder

     ll		t0, (a0)
     bne	t0, a1
      move	v0, zero
     move	v0, a
     sc		v0, (a0)
    j		ra

In this way, compare_and_swap() was changed as

compare_and_swap(p, oldval, newval)

{
	retval = 0;
	begin_atomic
	if (*p==oldval) {

	   if "sc" was failed goto out;
	   
	   *p = newval;
	   retval = 1;
	}
out:
	end_atomic
	return retval;
}

---
Hiroyuki Machida
Sony Corp.

#10 lore raw
In reply to Andreas Schwab
Quoted 8 lines

From: Andreas Schwab <[email protected]>
Date: Fri, 01 Feb 2002 11:49:22 +0100

There is no way to find out anything about intermediate values of *p when compare_and_swap returns zero. The value of *p can change anytime, even if it only was different from oldval just at the time compare_and_swap did the comparison. So there is zero chance that a spurious failure of compare_and_swap breaks anything.

Something to watch out for, though, is livelock. Consider the situation in which two processors are competing for a cache line, and both only win at the 'wrong' time: when computing a new value to be passed to compare_and_swap rather than when actually trying to perform the compare_and_swap. This is why on powerpc the loop is coded in the asm statement.

Signature · 1 line
- Geoffrey Keating <[email protected]> <[email protected]>
#13 lore raw
In reply to H . J . Lu
Quoted 5 lines · H . J . Lu
Quoted 2 lines

Gas will fill delay slots. Same object codes will be produced, so I think you don't have to do that by hand.

It will make the code more readable. We don't have to guess what the assembler will do.

But you lose a chance for something useful being reordered to the slot.
That might not necessarily be a "nop". Please don't forget of indents anyway.

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#77 lore raw
In reply to Hiroyuki Machida
Quoted 7 lines · Hiroyuki Machida
Quoted 1 line

It will fail if *p is not same as oldval.

Please note that "sc" may fail even if nobody write the variable. (See P.211 "8.4.2 Load-Linked/Sotre-Conditional" of "See MIPS RUN" for more detail.)
So, after your patch applied, compare_and_swap() may fail, even if *p is equal to oldval.

That's exactly what I meant -- "sc" may fail to execute the store, while
"cmpxchgl" may not.

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
author
#78 lore raw
Quoted 4 lines · Maciej W. Rozycki
Quoted 2 lines · Hiroyuki Machida
Quoted 1 line

It will fail if *p is not same as oldval.

Quoted 9 lines
Quoted 5 lines

Please note that "sc" may fail even if nobody write the variable. (See P.211 "8.4.2 Load-Linked/Sotre-Conditional" of "See MIPS RUN" for more detail.)
So, after your patch applied, compare_and_swap() may fail, even if *p is equal to oldval.

That's exactly what I meant -- "sc" may fail to execute the store, while
"cmpxchgl" may not.

Here is the updated patch.

H.J.
---
2002-01-31 H.J. Lu <[email protected]>

* sysdeps/mips/pspinlock.c (__pthread_spin_lock): Use a different register in the delayed slot.

* sysdeps/mips/pt-machine.h (testandset): Call _test_and_set. (__compare_and_swap): Return 0 only when failed to compare.

2002-01-31 H.J. Lu <[email protected]>

* sysdeps/mips/atomicity.h (compare_and_swap): Return 0 only when failed to compare.

* sysdeps/unix/sysv/linux/mips/sys/tas.h (_test_and_set): Fill the delay slot.

linuxthreads/sysdeps/mips/pspinlock.c renamed from linuxthreads/sysdeps/mips/pspinlock.c.llsc +10 -9
@@ -29,20 +29,21 @@
 int
 __pthread_spin_lock (pthread_spinlock_t *lock)
 {
-  unsigned int tmp;
+  unsigned int tmp1, tmp2;
 
   asm volatile
     ("\t\t\t# spin_lock\n\t"
-     "1:\n\t"
-     "ll	%1,%2\n\t"
      ".set	push\n\t"
-     ".set	noreorder\n\t"
+     ".set	noreorder\n"
+     "1:\n\t"
+     "ll	%1,%3\n\t"
      "bnez	%1,1b\n\t"
-     " li	%1,1\n\t"
-     ".set	pop\n\t"
-     "sc	%1,%0\n\t"
-     "beqz	%1,1b"
-     : "=m" (*lock), "=&r" (tmp)
+     "li	%2,1\n\t"
+     "sc	%2,%0\n\t"
+     "beqz	%2,1b\n\t"
+     "nop\n\t"
+     ".set	pop"
+     : "=m" (*lock), "=&r" (tmp1), "=&r" (tmp2)
      : "m" (*lock)
      : "memory");
 
linuxthreads/sysdeps/mips/pt-machine.h renamed from linuxthreads/sysdeps/mips/pt-machine.h.llsc +12 -40

75 lines not on this page · raw message

sysdeps/mips/atomicity.h renamed from sysdeps/mips/atomicity.h.llsc +22 -12
@@ -32,12 +32,16 @@ exchange_and_add (volatile uint32_t *mem
   int result, tmp;
 
   __asm__ __volatile__
-    ("/* Inline exchange & add */\n\t"
+    ("/* Inline exchange & add */\n"
      "1:\n\t"
      "ll	%0,%3\n\t"
      "addu	%1,%4,%0\n\t"
      "sc	%1,%2\n\t"
+     ".set	push\n\t"
+     ".set	noreorder\n\t"
      "beqz	%1,1b\n\t"
+     "nop\n\t"
+     ".set	pop\n\t"
      "/* End exchange & add */"
      : "=&r"(result), "=&r"(tmp), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -53,12 +57,16 @@ atomic_add (volatile uint32_t *mem, int 
   int result;
 
   __asm__ __volatile__
-    ("/* Inline atomic add */\n\t"
+    ("/* Inline atomic add */\n"
      "1:\n\t"
      "ll	%0,%2\n\t"
      "addu	%0,%3,%0\n\t"
      "sc	%0,%1\n\t"
+     ".set	push\n\t"
+     ".set	noreorder\n\t"
      "beqz	%0,1b\n\t"
+     "nop\n\t"
+     ".set	pop\n\t"
      "/* End atomic add */"
      : "=&r"(result), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -69,22 +77,24 @@ static inline int
 __attribute__ ((unused))
 compare_and_swap (volatile long int *p, long int oldval, long int newval)
 {
-  long int ret;
+  long int ret, temp;
 
   __asm__ __volatile__
-    ("/* Inline compare & swap */\n\t"
+    ("/* Inline compare & swap */\n"
      "1:\n\t"
-     "ll	%0,%4\n\t"
-     ".set	push\n"
+     "ll	%1,%5\n\t"
+     ".set	push\n\t"
      ".set	noreorder\n\t"
-     "bne	%0,%2,2f\n\t"
-     "move	%0,%3\n\t"
-     ".set	pop\n\t"
-     "sc	%0,%1\n\t"
-     "beqz	%0,1b\n"
+     "bne	%1,%3,2f\n\t"
+     "move	%0,$0\n\t"
+     "move	%0,%4\n\t"
+     "sc	%0,%2\n\t"
+     "beqz	%0,1b\n\t"
+     "nop\n\t"
+     ".set	pop\n"
      "2:\n\t"
      "/* End compare & swap */"
-     : "=&r" (ret), "=m" (*p)
+     : "=&r" (ret), "=&r" (temp), "=m" (*p)
      : "r" (oldval), "r" (newval), "m" (*p)
      : "memory");
 
sysdeps/unix/sysv/linux/mips/sys/tas.h renamed from sysdeps/unix/sysv/linux/mips/sys/tas.h.llsc +8 -7
@@ -42,16 +42,17 @@ _test_and_set (int *p, int v) __THROW
   int r, t;
 
   __asm__ __volatile__
-    ("1:\n\t"
+    (".set	push\n\t"
+     ".set	noreorder\n"
+     "1:\n\t"
      "ll	%0,%3\n\t"
-     ".set	push\n\t"
-     ".set	noreorder\n\t"
      "beq	%0,%4,2f\n\t"
-     " move	%1,%4\n\t"
-     ".set	pop\n\t"
+     "move	%1,%4\n\t"
      "sc	%1,%2\n\t"
-     "beqz	%1,1b\n"
-     "2:\n"
+     "beqz	%1,1b\n\t"
+     "nop\n"
+     "2:\n\t"
+     ".set	pop"
      : "=&r" (r), "=&r" (t), "=m" (*p)
      : "m" (*p), "r" (v)
      : "memory");
author
#14 lore raw

PATCH: Fix ll/sc for mips (take 3)

In reply to Maciej W. Rozycki
Quoted 5 lines · Maciej W. Rozycki
Quoted 3 lines · H . J . Lu
Quoted 2 lines

Gas will fill delay slots. Same object codes will be produced, so I think you don't have to do that by hand.

Quoted 7 lines
Quoted 2 lines

It will make the code more readable. We don't have to guess what the assembler will do.

But you lose a chance for something useful being reordered to the slot.
That might not necessarily be a "nop". Please don't forget of indents anyway.

Here is a new patch. I use branch likely to get rid of nops. Please tell me which indents I may have missed.

Thanks.

H.J.
----
2002-02-01 H.J. Lu <[email protected]>

* sysdeps/mips/pspinlock.c (__pthread_spin_lock): Use a different register in the delayed slot. Use branch likely.

* sysdeps/mips/pt-machine.h (testandset): Call _test_and_set. (__compare_and_swap): Return 0 only when failed to compare. Use branch likely.

2002-02-01 H.J. Lu <[email protected]>

* sysdeps/mips/atomicity.h (exchange_and_add): Use branch likely.
(atomic_add): Likewise.
(compare_and_swap): Return 0 only when failed to compare. Use branch likely.

* sysdeps/unix/sysv/linux/mips/sys/tas.h (_test_and_set): Use branch likely.

linuxthreads/sysdeps/mips/pspinlock.c renamed from linuxthreads/sysdeps/mips/pspinlock.c.llsc +8 -7
@@ -29,20 +29,21 @@
 int
 __pthread_spin_lock (pthread_spinlock_t *lock)
 {
-  unsigned int tmp;
+  unsigned int tmp1, tmp2;
 
   asm volatile
     ("\t\t\t# spin_lock\n\t"
+     "ll	%1,%3\n"
      "1:\n\t"
-     "ll	%1,%2\n\t"
      ".set	push\n\t"
      ".set	noreorder\n\t"
      "bnez	%1,1b\n\t"
-     " li	%1,1\n\t"
-     ".set	pop\n\t"
-     "sc	%1,%0\n\t"
-     "beqz	%1,1b"
-     : "=m" (*lock), "=&r" (tmp)
+     "li	%2,1\n\t"
+     "sc	%2,%0\n\t"
+     "beqzl	%2,1b\n\t"
+     "ll	%1,%3\n\t"
+     ".set	pop"
+     : "=m" (*lock), "=&r" (tmp1), "=&r" (tmp2)
      : "m" (*lock)
      : "memory");
 
linuxthreads/sysdeps/mips/pt-machine.h renamed from linuxthreads/sysdeps/mips/pt-machine.h.llsc +11 -39

74 lines not on this page · raw message

sysdeps/mips/atomicity.h renamed from sysdeps/mips/atomicity.h.llsc +23 -13
@@ -33,11 +33,15 @@ exchange_and_add (volatile uint32_t *mem
 
   __asm__ __volatile__
     ("/* Inline exchange & add */\n\t"
+     "ll	%0,%3\n"
      "1:\n\t"
-     "ll	%0,%3\n\t"
      "addu	%1,%4,%0\n\t"
      "sc	%1,%2\n\t"
-     "beqz	%1,1b\n\t"
+     ".set	push\n\t"
+     ".set	noreorder\n\t"
+     "beqzl	%1,1b\n\t"
+     "ll	%0,%3\n\t"
+     ".set	pop\n\t"
      "/* End exchange & add */"
      : "=&r"(result), "=&r"(tmp), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -54,11 +58,15 @@ atomic_add (volatile uint32_t *mem, int 
 
   __asm__ __volatile__
     ("/* Inline atomic add */\n\t"
+     "ll	%0,%2\n"
      "1:\n\t"
-     "ll	%0,%2\n\t"
      "addu	%0,%3,%0\n\t"
      "sc	%0,%1\n\t"
-     "beqz	%0,1b\n\t"
+     ".set	push\n\t"
+     ".set	noreorder\n\t"
+     "beqzl	%0,1b\n\t"
+     "ll	%0,%2\n\t"
+     ".set	pop\n\t"
      "/* End atomic add */"
      : "=&r"(result), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -69,22 +77,24 @@ static inline int
 __attribute__ ((unused))
 compare_and_swap (volatile long int *p, long int oldval, long int newval)
 {
-  long int ret;
+  long int ret, temp;
 
   __asm__ __volatile__
     ("/* Inline compare & swap */\n\t"
+     "ll	%1,%5\n"
      "1:\n\t"
-     "ll	%0,%4\n\t"
-     ".set	push\n"
+     ".set	push\n\t"
      ".set	noreorder\n\t"
-     "bne	%0,%2,2f\n\t"
-     "move	%0,%3\n\t"
-     ".set	pop\n\t"
-     "sc	%0,%1\n\t"
-     "beqz	%0,1b\n"
+     "bne	%1,%3,2f\n\t"
+     "move	%0,$0\n\t"
+     "move	%0,%4\n\t"
+     "sc	%0,%2\n\t"
+     "beqzl	%0,1b\n\t"
+     "ll	%1,%5\n\t"
+     ".set	pop\n"
      "2:\n\t"
      "/* End compare & swap */"
-     : "=&r" (ret), "=m" (*p)
+     : "=&r" (ret), "=&r" (temp), "=m" (*p)
      : "r" (oldval), "r" (newval), "m" (*p)
      : "memory");
 
sysdeps/unix/sysv/linux/mips/sys/tas.h renamed from sysdeps/unix/sysv/linux/mips/sys/tas.h.llsc +9 -6
@@ -42,16 +42,19 @@ _test_and_set (int *p, int v) __THROW
   int r, t;
 
   __asm__ __volatile__
-    ("1:\n\t"
-     "ll	%0,%3\n\t"
+    ("/* Inline test and set */\n\t"
+     "ll	%0,%3\n"
+     "1:\n\t"
      ".set	push\n\t"
      ".set	noreorder\n\t"
      "beq	%0,%4,2f\n\t"
-     " move	%1,%4\n\t"
-     ".set	pop\n\t"
+     "move	%1,%4\n\t"
      "sc	%1,%2\n\t"
-     "beqz	%1,1b\n"
-     "2:\n"
+     "beqzl	%1,1b\n\t"
+     "ll	%0,%3\n\t"
+     ".set	pop\n"
+     "2:\n\t"
+     "/* End test and set */"
      : "=&r" (r), "=&r" (t), "=m" (*p)
      : "m" (*p), "r" (v)
      : "memory");
#79 lore raw
In reply to H . J . Lu
Quoted 1 line · H . J . Lu

Here is the updated patch.

Please do indent instructions in branch delay slots like it's done in
other code (and here originally as well). It much improves the perception of what exactly is going on.

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
author
#80 lore raw
Quoted 8 lines · Maciej W. Rozycki
Quoted 1 line · H . J . Lu

Here is the updated patch.

Please do indent instructions in branch delay slots like it's done in
other code (and here originally as well). It much improves the perception of what exactly is going on.

Like this?

H.J.
----
2002-02-01 H.J. Lu <[email protected]>

* sysdeps/mips/pspinlock.c (__pthread_spin_lock): Use a different register in the delayed slot. Use branch likely.

* sysdeps/mips/pt-machine.h (testandset): Call _test_and_set. (__compare_and_swap): Return 0 only when failed to compare. Use branch likely.

2002-02-01 H.J. Lu <[email protected]>

* sysdeps/mips/atomicity.h (exchange_and_add): Use branch likely.
(atomic_add): Likewise.
(compare_and_swap): Return 0 only when failed to compare. Use branch likely.

* sysdeps/unix/sysv/linux/mips/sys/tas.h (_test_and_set): Use branch likely.

linuxthreads/sysdeps/mips/pspinlock.c renamed from linuxthreads/sysdeps/mips/pspinlock.c.llsc +8 -7
@@ -29,20 +29,21 @@
 int
 __pthread_spin_lock (pthread_spinlock_t *lock)
 {
-  unsigned int tmp;
+  unsigned int tmp1, tmp2;
 
   asm volatile
     ("\t\t\t# spin_lock\n\t"
+     "ll	%1,%3\n"
      "1:\n\t"
-     "ll	%1,%2\n\t"
      ".set	push\n\t"
      ".set	noreorder\n\t"
      "bnez	%1,1b\n\t"
-     " li	%1,1\n\t"
-     ".set	pop\n\t"
-     "sc	%1,%0\n\t"
-     "beqz	%1,1b"
-     : "=m" (*lock), "=&r" (tmp)
+     " li	%2,1\n\t"
+     "sc	%2,%0\n\t"
+     "beqzl	%2,1b\n\t"
+     " ll	%1,%3\n\t"
+     ".set	pop"
+     : "=m" (*lock), "=&r" (tmp1), "=&r" (tmp2)
      : "m" (*lock)
      : "memory");
 
linuxthreads/sysdeps/mips/pt-machine.h renamed from linuxthreads/sysdeps/mips/pt-machine.h.llsc +11 -39

74 lines not on this page · raw message

sysdeps/mips/atomicity.h renamed from sysdeps/mips/atomicity.h.llsc +23 -13
@@ -33,11 +33,15 @@ exchange_and_add (volatile uint32_t *mem
 
   __asm__ __volatile__
     ("/* Inline exchange & add */\n\t"
+     "ll	%0,%3\n"
      "1:\n\t"
-     "ll	%0,%3\n\t"
      "addu	%1,%4,%0\n\t"
      "sc	%1,%2\n\t"
-     "beqz	%1,1b\n\t"
+     ".set	push\n\t"
+     ".set	noreorder\n\t"
+     "beqzl	%1,1b\n\t"
+     " ll	%0,%3\n\t"
+     ".set	pop\n\t"
      "/* End exchange & add */"
      : "=&r"(result), "=&r"(tmp), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -54,11 +58,15 @@ atomic_add (volatile uint32_t *mem, int 
 
   __asm__ __volatile__
     ("/* Inline atomic add */\n\t"
+     "ll	%0,%2\n"
      "1:\n\t"
-     "ll	%0,%2\n\t"
      "addu	%0,%3,%0\n\t"
      "sc	%0,%1\n\t"
-     "beqz	%0,1b\n\t"
+     ".set	push\n\t"
+     ".set	noreorder\n\t"
+     "beqzl	%0,1b\n\t"
+     " ll	%0,%2\n\t"
+     ".set	pop\n\t"
      "/* End atomic add */"
      : "=&r"(result), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -69,22 +77,24 @@ static inline int
 __attribute__ ((unused))
 compare_and_swap (volatile long int *p, long int oldval, long int newval)
 {
-  long int ret;
+  long int ret, temp;
 
   __asm__ __volatile__
     ("/* Inline compare & swap */\n\t"
+     "ll	%1,%5\n"
      "1:\n\t"
-     "ll	%0,%4\n\t"
-     ".set	push\n"
+     ".set	push\n\t"
      ".set	noreorder\n\t"
-     "bne	%0,%2,2f\n\t"
-     "move	%0,%3\n\t"
-     ".set	pop\n\t"
-     "sc	%0,%1\n\t"
-     "beqz	%0,1b\n"
+     "bne	%1,%3,2f\n\t"
+     " move	%0,$0\n\t"
+     "move	%0,%4\n\t"
+     "sc	%0,%2\n\t"
+     "beqzl	%0,1b\n\t"
+     " ll	%1,%5\n\t"
+     ".set	pop\n"
      "2:\n\t"
      "/* End compare & swap */"
-     : "=&r" (ret), "=m" (*p)
+     : "=&r" (ret), "=&r" (temp), "=m" (*p)
      : "r" (oldval), "r" (newval), "m" (*p)
      : "memory");
 
sysdeps/unix/sysv/linux/mips/sys/tas.h renamed from sysdeps/unix/sysv/linux/mips/sys/tas.h.llsc +8 -5
@@ -42,16 +42,19 @@ _test_and_set (int *p, int v) __THROW
   int r, t;
 
   __asm__ __volatile__
-    ("1:\n\t"
-     "ll	%0,%3\n\t"
+    ("/* Inline test and set */\n\t"
+     "ll	%0,%3\n"
+     "1:\n\t"
      ".set	push\n\t"
      ".set	noreorder\n\t"
      "beq	%0,%4,2f\n\t"
      " move	%1,%4\n\t"
-     ".set	pop\n\t"
      "sc	%1,%2\n\t"
-     "beqz	%1,1b\n"
-     "2:\n"
+     "beqzl	%1,1b\n\t"
+     " ll	%0,%3\n\t"
+     ".set	pop\n"
+     "2:\n\t"
+     "/* End test and set */"
      : "=&r" (r), "=&r" (t), "=m" (*p)
      : "m" (*p), "r" (v)
      : "memory");
#15 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to H . J . Lu
Quoted 9 lines · H . J . Lu
Quoted 8 lines · Maciej W. Rozycki
Quoted 5 lines · H . J . Lu
Quoted 2 lines

Gas will fill delay slots. Same object codes will be produced, so I think you don't have to do that by hand.

It will make the code more readable. We don't have to guess what the assembler will do.

Quoted 7 lines
Quoted 4 lines

But you lose a chance for something useful being reordered to the slot.
That might not necessarily be a "nop". Please don't forget of indents anyway.

Here is a new patch. I use branch likely to get rid of nops. Please tell me which indents I may have missed.

Can you really assume presence of the branch-likely instruction? I don't think so.

Signature · 2 lines
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer
author
#16 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 15 lines · Daniel Jacobowitz
Quoted 14 lines · H . J . Lu
Quoted 12 lines · Maciej W. Rozycki
Quoted 5 lines · H . J . Lu
Quoted 2 lines

Gas will fill delay slots. Same object codes will be produced, so I think you don't have to do that by hand.

It will make the code more readable. We don't have to guess what the assembler will do.

But you lose a chance for something useful being reordered to the slot.
That might not necessarily be a "nop". Please don't forget of indents anyway.

Quoted 5 lines
Quoted 2 lines

Here is a new patch. I use branch likely to get rid of nops. Please tell me which indents I may have missed.

Can you really assume presence of the branch-likely instruction? I don't think so.

Why not? Can you show me a MIPS II or above CPU which doesn't have branch-likely instruction? From gcc,

/* ISA has branch likely instructions (eg. mips2). */
/* Disable branchlikely for tx39 until compare rewrite. They haven't
been generated up to this point. */

#define ISA_HAS_BRANCHLIKELY    (mips_isa != 1                          \
                                 /* || TARGET_MIPS3900 */)                      

Did I miss something?

H.J.

#17 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

From: "H . J . Lu" <[email protected]>
Subject: Re: PATCH: Fix ll/sc for mips (take 3)
Date: Fri, 1 Feb 2002 15:15:13 -0800

Quoted 10 lines

Why not? Can you show me a MIPS II or above CPU which doesn't have branch-likely instruction? From gcc,

/* ISA has branch likely instructions (eg. mips2). */
/* Disable branchlikely for tx39 until compare rewrite. They haven't
been generated up to this point. */

#define ISA_HAS_BRANCHLIKELY    (mips_isa != 1                          \
                                 /* || TARGET_MIPS3900 */)                      

Did I miss something?

I think we can assume CPU has branch-likely insns, if CPU has MIPS ISA 2 or greater ISA, as H.J said. I don't know any exception of this. If anyone know exceptions, please let us know.

(FYI: we can't assume CPU has LL/SC even if CPU has branch-likely insns. )

HL's patch looks good for me.

---
Hiroyuki Machida
Sony Corp.

#23 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to H . J . Lu
Quoted 25 lines · H . J . Lu
Quoted 15 lines · Daniel Jacobowitz
Quoted 14 lines · H . J . Lu
Quoted 12 lines · Maciej W. Rozycki
Quoted 5 lines · H . J . Lu
Quoted 2 lines

Gas will fill delay slots. Same object codes will be produced, so I think you don't have to do that by hand.

It will make the code more readable. We don't have to guess what the assembler will do.

But you lose a chance for something useful being reordered to the slot.
That might not necessarily be a "nop". Please don't forget of indents anyway.

Quoted 5 lines
Quoted 2 lines

Here is a new patch. I use branch likely to get rid of nops. Please tell me which indents I may have missed.

Can you really assume presence of the branch-likely instruction? I don't think so.

Why not? Can you show me a MIPS II or above CPU which doesn't have branch-likely instruction? From gcc,

Quoted 7 lines

/* ISA has branch likely instructions (eg. mips2). */
/* Disable branchlikely for tx39 until compare rewrite. They haven't
been generated up to this point. */

#define ISA_HAS_BRANCHLIKELY    (mips_isa != 1                          \
                                 /* || TARGET_MIPS3900 */)                      

Did I miss something?

My fault. I was indeed thinking of the tx39, which is not normally MIPS2.

Signature · 2 lines
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer
#81 lore raw
In reply to H . J . Lu
Quoted 5 lines · H . J . Lu
Quoted 3 lines

Please do indent instructions in branch delay slots like it's done in
other code (and here originally as well). It much improves the perception of what exactly is going on.

Like this?

Exactly. Thanks.

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#24 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Daniel Jacobowitz
Quoted 17 lines · Daniel Jacobowitz
Quoted 16 lines · H . J . Lu
Quoted 15 lines · Daniel Jacobowitz
Quoted 14 lines · H . J . Lu
Quoted 12 lines · Maciej W. Rozycki
Quoted 5 lines · H . J . Lu
Quoted 2 lines

Gas will fill delay slots. Same object codes will be produced, so I think you don't have to do that by hand.

It will make the code more readable. We don't have to guess what the assembler will do.

But you lose a chance for something useful being reordered to the slot.
That might not necessarily be a "nop". Please don't forget of indents anyway.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Here is a new patch. I use branch likely to get rid of nops. Please tell me which indents I may have missed.

Can you really assume presence of the branch-likely instruction? I don't think so.

Actually, regardless of whether modern cpus implement it, I'd argue that avoiding the branch likely is a good idea for 2 reasons:

1) In the latest MIPS specs (mips32 and mips64) branch likelies have officially been deprecated as probable removals from the architecture in the not-too-distant future.

2) More importantly, most implementations don't use any sort of dynamic branch prediction on branch likelies. They predict taken, always, since that's the specified intent (it's a branch *likely* to be taken). For most spin locks, the normal behaviour is a fall through, not taking that branch, so you're inflicting a branch mispredict penalty on every lock grabbed without contention. Even for locks which the general case is contention, giving the processor branch predictor a chance to learn that is a Good Idea.

-Justin

#26 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to H . J . Lu
Quoted 4 lines · H . J . Lu

Does everyone agree with this? If yes, I can make a patch not to use branch likely. But on the other hand, "gcc -mips2" will generate code using branch likely. If branch likely doesn't buy you anything, shouldn't we change gcc not to generate branch likely instructions?

I would say it's still too early to actively remove them. There are many processors out there (certainly in the embedded world most of them) without branch prediction, and for all of those branch-likelys actually help performance.

/Hartvig

author
#25 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Justin Carlson
Quoted 8 lines · Justin Carlson

Actually, regardless of whether modern cpus implement it, I'd argue that avoiding the branch likely is a good idea for 2 reasons:

1) In the latest MIPS specs (mips32 and mips64) branch likelies have officially been deprecated as probable removals from the architecture in the not-too-distant future.

Quoted 9 lines

2) More importantly, most implementations don't use any sort of dynamic branch prediction on branch likelies. They predict taken, always, since that's the specified intent (it's a branch *likely* to be taken). For most spin locks, the normal behaviour is a fall through, not taking that branch, so you're inflicting a branch mispredict penalty on every lock grabbed without contention. Even for locks which the general case is contention, giving the processor branch predictor a chance to learn that is a Good Idea.

Does everyone agree with this? If yes, I can make a patch not to use branch likely. But on the other hand, "gcc -mips2" will generate code using branch likely. If branch likely doesn't buy you anything, shouldn't we change gcc not to generate branch likely instructions?

H.J.

#82 lore raw
In reply to H . J . Lu
Quoted 2 lines · H . J . Lu

Like this?
[...]

From what I've seen this patch was generally agreed on. I've checked it in.

Signature · 3 lines
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------
#27 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to H . J . Lu
Quoted 19 lines · H . J . Lu
Quoted 8 lines · Justin Carlson

Actually, regardless of whether modern cpus implement it, I'd argue that avoiding the branch likely is a good idea for 2 reasons:

1) In the latest MIPS specs (mips32 and mips64) branch likelies have officially been deprecated as probable removals from the architecture in the not-too-distant future.

Quoted 9 lines

2) More importantly, most implementations don't use any sort of dynamic branch prediction on branch likelies. They predict taken, always, since that's the specified intent (it's a branch *likely* to be taken). For most spin locks, the normal behaviour is a fall through, not taking that branch, so you're inflicting a branch mispredict penalty on every lock grabbed without contention. Even for locks which the general case is contention, giving the processor branch predictor a chance to learn that is a Good Idea.

Quoted 5 lines

Does everyone agree with this? If yes, I can make a patch not to use branch likely. But on the other hand, "gcc -mips2" will generate code using branch likely. If branch likely doesn't buy you anything, shouldn't we change gcc not to generate branch likely instructions?

I know of at least one internal version of gcc which already has been hacked to remove generation of branch likely instructions. It actually helped performance a bit because gcc has (possibly had, this was 6-9 months ago) the bad habit of generating code like this:

beqzl $1, next
daddiu $2, $3, 1
next:
...

This looks like a nice, compact way to do a conditional move. In practice, it's a horrendous idea due to the lack of consideration of the branch prediction. It's easier to tell the compiler not to generate branch likelies than to try to fix the code generation for this case. :)

Also, I didn't say branch likely doesn't buy you anything; there are situations where it works well. Looking at the spin_lock code, though, this isn't one of them. The cases where it is a win are far enough between that my personal practice is to generally avoid them.

-Justin

#31 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to a message not in the archive

[ Hi Justin, enjoying winter in pgh? 8-]

Quoted 4 lines · H . J . Lu

Does everyone agree with this? If yes, I can make a patch not to use branch likely. But on the other hand, "gcc -mips2" will generate code using branch likely. If branch likely doesn't buy you anything, shouldn't we change gcc not to generate branch likely instructions?

Branch-likely instructions probably _do_ buy you something (at least, slightly less code size) on some CPUs, probably even some CPUs which are still being produced.

FYI, somebody, i think it was Mike Stump, submitted a patch to add a flag to disable branch-likely instructions N months ago. It was discussed a little bit, not AFAIK nothing was ever done with it.

To quote the MIPS32 instruction set document from the MIPS web site:

Quoted 8 lines

Programming Notes:

[ ... ]

Software is strongly encouraged to avoid the use of the Branch Likely instructions, as they will be removed from a future revision of the MIPS Architecture.

Quoted 7 lines

Some implementations always predict the branch will be taken, so there is a significant penalty if the branch is not taken. Software should only use this instruction when there is a very high probability (98% or more) that the branch will be taken. If the branch is not likely to be taken or if the probability of a taken branch is unknown, software is encouraged to use the BGEZAL instruction instead.

If you have a sufficiently high static likelyhood of branch-taken, it may well be worthwhile to use branch-likely even on MIPS32/MIPS64 if it'll save you code space, or if you can determine that not clogging up your branch history / predictor buffers makes your code run faster. However, it should definitely not be the default.

Anyway, from where I sit, this should be resolved like:

* -mips1 - -mips5 should generate them by default (for strongly-predicted branches), -mips32 & -mips64 should not. (modify that for your favorite flag names, -march or whatever. 8-)

* There should be flags to override the defaults either way, so you could say -mips2 -mno-branch-likely, or -mips32 -mbranch-likely.

cgd

#18 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Hiroyuki Machida
Quoted 2 lines · Hiroyuki Machida

I think we can assume CPU has branch-likely insns, if CPU has MIPS ISA 2 or greater ISA..

"MIPS II" is officially the instruction set introduced for the long lost R6000 CPU.

But "MIPS II" is now used to mean "the 32-bit subset of MIPS III" (which is extremely close to the same thing, but I'm never quite sure about the last details of the R6000 - Kevin would remember better, probably).

OK: branch-likely is definitely part of MIPS II and MIPS32. There are still MIPS CPUs in regular use which are based on MIPS I and don't provide them. Generally the advantages of MIPS II are slight, so if you want to build a kernel which will not require instruction-set variants, it's no big deal to restrict it to MIPS I.

Quoted 2 lines

(FYI: we can't assume CPU has LL/SC even if CPU has branch-likely insns. )

LL/SC is also part of MIPS III (and the 32-bit variants are thus taken to be in MIPS II). Unfortunately, the documentation of LL/SC gave the impression that they were useful only in multiprocessor systems, so they were omitted by NEC building the Vr41xx and Toshiba's R59xx. In both cases it's a bug - but since it isn't about to be fixed, you need workarounds.

In these more enlightened days, CPU vendors are more likely to ask an operating system person before they leave out bits of the instruction set, so we hope it won't happen again!

Dominic Sweetman
Algorithmics Ltd
The Fruit Farm, Ely Road, Chittering, CAMBS CB5 9PH, ENGLAND
phone: +44 1223 706200 / fax: +44 1223 706250 / direct: +44 1223 706205
http://www.algor.co.uk

#32 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to [email protected]
Quoted 3 lines · [email protected] ([email protected]) writes:

Branch-likely instructions probably _do_ buy you something (at least, slightly less code size) on some CPUs, probably even some CPUs which are still being produced.

Here's how branch likely is used to improve performance in a simple MIPS CPU, and why it has no effect on code size.

You start off with this:

loopstart: insn 1
insn 2
....
insn last
branch to loopstart
nop

In small loops, the last instruction in the loop might well calculate the branch condition, so it can't be moved into the delay slot of the loop-closing branch. That puts a no-op into every loop iteration. With branch-likely, you can transform the loop to

loopstart: insn 1
loop2: insn 2
....
insn last
branch-likely loop2
insn 1 (copy)

The nop is replaced by a duplicate instruction from the top of the loop. Good for performance, no effect on code size.

Builders of clever modern CPUs full of branch prediction hardware, multiple instruction issue and instruction re-ordering find the coupling of the branch-likely to the following instruction makes their CPUs more complicated. That's why MIPS have warned that the instructions will be removed from some future version of the MIPS instruction set.

Signature · 5 lines
Dominic Sweetman
Algorithmics Ltd
The Fruit Farm, Ely Road, Chittering, CAMBS CB5 9PH, ENGLAND
phone: +44 1223 706200 / fax: +44 1223 706250 / direct: +44 1223 706205
http://www.algor.co.uk
#75 lore raw
In reply to H . J . Lu
Quoted 5 lines · H . J . Lu
Quoted 2 lines

Gas will fill delay slots. Same object codes will be produced, so I think you don't have to do that by hand.

It will make the code more readable. We don't have to guess what the assembler will do.

Generally speaking a MIPS assembler is free to do arbitrary reordering. In the past there have been non-GNU assembler that were doing more massive reordering than gcc does ... Using .set noreorder means you dump the assembler's intelligence and take full responsibility for dealing with all interlocks (or the lack thereof) and other performance issues yourself.

Ralf

#74 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Justin Carlson
Quoted 3 lines · Justin Carlson

2) More importantly, most implementations don't use any sort of dynamic branch prediction on branch likelies. They predict taken, always, since that's the specified intent (it's a branch *likely* to be taken).

CPU guys hate branch likely and would probably love if whoever invented them hires at Intel ;-)

Quoted 5 lines

For most spin locks, the normal behaviour is a fall through, not taking that branch, so you're inflicting a branch mispredict penalty on every lock grabbed without contention. Even for locks which the general case is contention, giving the processor branch predictor a chance to learn that is a Good Idea.

I was thinking about spinlocks like

retry: la addrreg, retry

	ll	reg, lockvar
	...
	sc	reg, lockvar
	teq	$0, reg

That would depend on the price of a trap instruction when it's not taken and the probability of the lock being congested.

Ralf

#60 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to [email protected]
Quoted 9 lines · On Sun, Feb 03, 2002 at 03:29:28PM -0800, [email protected] wrote:
Quoted 4 lines · H . J . Lu

Does everyone agree with this? If yes, I can make a patch not to use branch likely. But on the other hand, "gcc -mips2" will generate code using branch likely. If branch likely doesn't buy you anything, shouldn't we change gcc not to generate branch likely instructions?

Branch-likely instructions probably _do_ buy you something (at least, slightly less code size) on some CPUs, probably even some CPUs which are still being produced.

I benchmarked the performance improvment on R4000/R4400 by using branch likely instructions to be in the range of 1-2% in a piece of pretty "branchy" code, so we don't want to disable branch likely right entirely. Newer CPU types, in particular those featuring branch prediction tend to perform differently.

I suggest to enable branch likely in gcc for those > MIPS II CPUs where they're known to improve performance or when optimizing for code size. Unfortunately gcc's knowledge about such architecture details is rather limited.

Ralf

#76 lore raw
In reply to Ralf Baechle
Quoted 9 lines · Ralf Baechle
Quoted 2 lines

It will make the code more readable. We don't have to guess what the assembler will do.

Generally speaking a MIPS assembler is free to do arbitrary reordering. In the past there have been non-GNU assembler that were doing more massive reordering than gcc does ... Using .set noreorder means you dump the assembler's intelligence and take full responsibility for dealing with all interlocks (or the lack thereof) and other performance issues yourself.

That's why I'm still uneasy about the patch or, specifically, its
_test_and_set hunk. It's best to avoid pretending we have the knowledge beyond what an assembler has, as it's often not the case -- consider all the options an assembler can accept for code variations.

Using "noreorder" is really justified if you need to generate an exact
opcode stream for some reason (perhaps for a trampoline with a fixed format) or you know you have the knowledege beyond what an assembler has, e.g. you know an instruction can (or must) really be placed in a delay slot even though a dependency analysis performed by an assembler shows otherwise.

Also beware of implicit macros which may expand into multiple
instructions -- their semantics when placed in a delay slot may differ from what one may expect!

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
author
#33 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Dominic Sweetman
Quoted 34 lines · Dominic Sweetman
Quoted 3 lines · [email protected] ([email protected]) writes:

Branch-likely instructions probably _do_ buy you something (at least, slightly less code size) on some CPUs, probably even some CPUs which are still being produced.

Here's how branch likely is used to improve performance in a simple MIPS CPU, and why it has no effect on code size.

You start off with this:

loopstart: insn 1
insn 2
....
insn last
branch to loopstart
nop

In small loops, the last instruction in the loop might well calculate the branch condition, so it can't be moved into the delay slot of the loop-closing branch. That puts a no-op into every loop iteration. With branch-likely, you can transform the loop to

loopstart: insn 1
loop2: insn 2
....
insn last
branch-likely loop2
insn 1 (copy)

The nop is replaced by a duplicate instruction from the top of the loop. Good for performance, no effect on code size.

Quoted 6 lines

Builders of clever modern CPUs full of branch prediction hardware, multiple instruction issue and instruction re-ordering find the coupling of the branch-likely to the following instruction makes their CPUs more complicated. That's why MIPS have warned that the instructions will be removed from some future version of the MIPS instruction set.

I can change glibc not to use branch-likely without using nop. But it may require one or two instructions outside of the loop. Should I do it given what we know now?

H.J.

#34 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 3 lines · H . J . Lu

I can change glibc not to use branch-likely without using nop. But it may require one or two instructions outside of the loop. Should I do it given what we know now?

I would not recommend using "branch likely" in assembler coding, if that's what you're asking.

Dominic

#59 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Dominic Sweetman

thanks for the explanation.

just goes to show that i should probably sit down and read your book from cover to cover one day, rather than just recommending it to the newbies. 8-)

chris

#28 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Justin Carlson
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Justin" == Justin Carlson <[email protected]> writes:

Quoted 6 lines · H . J . Lu
Quoted 6 lines

Does everyone agree with this? If yes, I can make a patch not to use branch likely. But on the other hand, "gcc -mips2" will generate code using branch likely. If branch likely doesn't buy you anything, shouldn't we change gcc not to generate branch likely instructions?

Justin> I know of at least one internal version of gcc which already Justin> has been hacked to remove generation of branch likely Justin> instructions.

More precisely (if you're looking at the same one I was) -- it has a target processor type check that disables it for those CPUs where it is known to be not a good idea.

Justin> Also, I didn't say branch likely doesn't buy you anything; Justin> there are situations where it works well. Looking at the Justin> spin_lock code, though, this isn't one of them.

I agree, and I think that point was missed. Independent of whether a particular processor (MIPS or otherwise) has the concept of "branch likely", the design rule of spinlocks is that you try to avoid spinning (i.e., avoid lock contention) in the system design. So for that particular construct, the right answer is "branch not likely".

paul

author
#35 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Dominic Sweetman
Quoted 10 lines · Dominic Sweetman
Quoted 3 lines · H . J . Lu

I can change glibc not to use branch-likely without using nop. But it may require one or two instructions outside of the loop. Should I do it given what we know now?

I would not recommend using "branch likely" in assembler coding, if that's what you're asking.

Here is a patch to remove branch likely. But I couldn't find a way not to fill the delay slot with nop. BTW, is that safe to remove ".set noreorder"?

H.J.
---
2002-02-04 H.J. Lu <[email protected]>

* sysdeps/mips/pspinlock.c (__pthread_spin_lock): Not use branch likely.
* sysdeps/mips/pt-machine.h (testandset): Liekwise. (__compare_and_swap): Liekwise.

2002-02-04 H.J. Lu <[email protected]>

* sysdeps/mips/atomicity.h (exchange_and_add): Not use branch likely.
(atomic_add): Likewise.
(compare_and_swap): Likewise.
* sysdeps/unix/sysv/linux/mips/sys/tas.h (_test_and_set):
Likewise.

linuxthreads/sysdeps/mips/pspinlock.c renamed from linuxthreads/sysdeps/mips/pspinlock.c.llsc +1 -1
@@ -40,7 +40,7 @@ __pthread_spin_lock (pthread_spinlock_t 
      "bnez	%1,1b\n\t"
      " li	%2,1\n\t"
      "sc	%2,%0\n\t"
-     "beqzl	%2,1b\n\t"
+     "beqz	%2,1b\n\t"
      " ll	%1,%3\n\t"
      ".set	pop"
      : "=m" (*lock), "=&r" (tmp1), "=&r" (tmp2)
linuxthreads/sysdeps/mips/pt-machine.h renamed from linuxthreads/sysdeps/mips/pt-machine.h.llsc +1 -1
@@ -66,7 +66,7 @@ __compare_and_swap (long int *p, long in
      " move	%0,$0\n\t"
      "move	%0,%4\n\t"
      "sc	%0,%2\n\t"
-     "beqzl	%0,1b\n\t"
+     "beqz	%0,1b\n\t"
      " ll	%1,%5\n\t"
      ".set	pop\n"
      "2:\n\t"
sysdeps/mips/atomicity.h renamed from sysdeps/mips/atomicity.h.llsc +9 -9
@@ -32,15 +32,15 @@ exchange_and_add (volatile uint32_t *mem
   int result, tmp;
 
   __asm__ __volatile__
-    ("/* Inline exchange & add */\n\t"
+    ("/* Inline exchange & add */\n"
+     "1:\n"
      "ll	%0,%3\n"
-     "1:\n\t"
      "addu	%1,%4,%0\n\t"
      "sc	%1,%2\n\t"
      ".set	push\n\t"
      ".set	noreorder\n\t"
-     "beqzl	%1,1b\n\t"
-     " ll	%0,%3\n\t"
+     "beqz	%1,1b\n\t"
+     " nop\n\t"
      ".set	pop\n\t"
      "/* End exchange & add */"
      : "=&r"(result), "=&r"(tmp), "=m"(*mem)
@@ -57,15 +57,15 @@ atomic_add (volatile uint32_t *mem, int 
   int result;
 
   __asm__ __volatile__
-    ("/* Inline atomic add */\n\t"
-     "ll	%0,%2\n"
+    ("/* Inline atomic add */\n"
      "1:\n\t"
+     "ll	%0,%2\n"
      "addu	%0,%3,%0\n\t"
      "sc	%0,%1\n\t"
      ".set	push\n\t"
      ".set	noreorder\n\t"
-     "beqzl	%0,1b\n\t"
-     " ll	%0,%2\n\t"
+     "beqz	%0,1b\n\t"
+     " nop\n\t"
      ".set	pop\n\t"
      "/* End atomic add */"
      : "=&r"(result), "=m"(*mem)
@@ -89,7 +89,7 @@ compare_and_swap (volatile long int *p, 
      " move	%0,$0\n\t"
      "move	%0,%4\n\t"
      "sc	%0,%2\n\t"
-     "beqzl	%0,1b\n\t"
+     "beqz	%0,1b\n\t"
      " ll	%1,%5\n\t"
      ".set	pop\n"
      "2:\n\t"
sysdeps/unix/sysv/linux/mips/sys/tas.h renamed from sysdeps/unix/sysv/linux/mips/sys/tas.h.llsc +4 -4
@@ -42,16 +42,16 @@ _test_and_set (int *p, int v) __THROW
   int r, t;
 
   __asm__ __volatile__
-    ("/* Inline test and set */\n\t"
-     "ll	%0,%3\n"
+    ("/* Inline test and set */\n"
      "1:\n\t"
+     "ll	%0,%3\n"
      ".set	push\n\t"
      ".set	noreorder\n\t"
      "beq	%0,%4,2f\n\t"
      " move	%1,%4\n\t"
      "sc	%1,%2\n\t"
-     "beqzl	%1,1b\n\t"
-     " ll	%0,%3\n\t"
+     "beqz	%1,1b\n\t"
+     " nop\n\t"
      ".set	pop\n"
      "2:\n\t"
      "/* End test and set */"
#36 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 8 lines · H . J . Lu
Quoted 7 lines · Dominic Sweetman
Quoted 3 lines · H . J . Lu

I can change glibc not to use branch-likely without using nop. But it may require one or two instructions outside of the loop. Should I do it given what we know now?

Quoted 7 lines
Quoted 3 lines

I would not recommend using "branch likely" in assembler coding, if that's what you're asking.

Here is a patch to remove branch likely. But I couldn't find a way not to fill the delay slot with nop. BTW, is that safe to remove ".set noreorder"?

You mean, if there is nothing which can be put there? Yes, it's safe.

linuxthreads/sysdeps/mips/pspinlock.c __pthread_spin_lock (pthread_spinlock_t · line 46

Quoted 11 lines
linuxthreads/sysdeps/mips/pspinlock.c renamed from linuxthreads/sysdeps/mips/pspinlock.c.llsc +1 -1
@@ -40,7 +40,7 @@ __pthread_spin_lock (pthread_spinlock_t 
      "bnez	%1,1b\n\t"
      " li	%2,1\n\t"
      "sc	%2,%0\n\t"
-     "beqzl	%2,1b\n\t"
+     "beqz	%2,1b\n\t"
      " ll	%1,%3\n\t"
      ".set	pop"
      : "=m" (*lock), "=&r" (tmp1), "=&r" (tmp2)

Is that really what you meant to do? The ll is now in the delay slot of the beqz.

linuxthreads/sysdeps/mips/pt-machine.h __compare_and_swap (long int *p, long in · line 72

Quoted 11 lines
linuxthreads/sysdeps/mips/pt-machine.h renamed from linuxthreads/sysdeps/mips/pt-machine.h.llsc +1 -1
@@ -66,7 +66,7 @@ __compare_and_swap (long int *p, long in
      " move	%0,$0\n\t"
      "move	%0,%4\n\t"
      "sc	%0,%2\n\t"
-     "beqzl	%0,1b\n\t"
+     "beqz	%0,1b\n\t"
      " ll	%1,%5\n\t"
      ".set	pop\n"
      "2:\n\t"

Ditto.

linuxthreads/sysdeps/mips/pt-machine.h compare_and_swap (volatile long int *p, · line 95

Quoted 9 lines
linuxthreads/sysdeps/mips/pt-machine.h renamed from linuxthreads/sysdeps/mips/pt-machine.h.llsc +1 -1
@@ -89,7 +89,7 @@ compare_and_swap (volatile long int *p, 
      " move	%0,$0\n\t"
      "move	%0,%4\n\t"
      "sc	%0,%2\n\t"
-     "beqzl	%0,1b\n\t"
+     "beqz	%0,1b\n\t"
      " ll	%1,%5\n\t"
      ".set	pop\n"
      "2:\n\t"

Ditto.

Signature · 2 lines
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer
author
#37 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 15 lines · Daniel Jacobowitz

linuxthreads/sysdeps/mips/pspinlock.c __pthread_spin_lock (pthread_spinlock_t · line 46

Quoted 12 lines
linuxthreads/sysdeps/mips/pspinlock.c renamed from linuxthreads/sysdeps/mips/pspinlock.c.llsc +1 -1
@@ -40,7 +40,7 @@ __pthread_spin_lock (pthread_spinlock_t 
      "bnez	%1,1b\n\t"
      " li	%2,1\n\t"
      "sc	%2,%0\n\t"
-     "beqzl	%2,1b\n\t"
+     "beqz	%2,1b\n\t"
      " ll	%1,%3\n\t"
      ".set	pop"
      : "=m" (*lock), "=&r" (tmp1), "=&r" (tmp2)

Is that really what you meant to do? The ll is now in the delay slot of the beqz.

Yes, it is ok since we don't care what ll does if the branch is not taken.

H.J.

#38 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 17 lines · H . J . Lu
Quoted 15 lines · Daniel Jacobowitz

linuxthreads/sysdeps/mips/pspinlock.c __pthread_spin_lock (pthread_spinlock_t · line 46

Quoted 12 lines
linuxthreads/sysdeps/mips/pspinlock.c renamed from linuxthreads/sysdeps/mips/pspinlock.c.llsc +1 -1
@@ -40,7 +40,7 @@ __pthread_spin_lock (pthread_spinlock_t 
      "bnez	%1,1b\n\t"
      " li	%2,1\n\t"
      "sc	%2,%0\n\t"
-     "beqzl	%2,1b\n\t"
+     "beqz	%2,1b\n\t"
      " ll	%1,%3\n\t"
      ".set	pop"
      : "=m" (*lock), "=&r" (tmp1), "=&r" (tmp2)

Is that really what you meant to do? The ll is now in the delay slot of the beqz.

Quoted 2 lines

Yes, it is ok since we don't care what ll does if the branch is not taken.

Won't this cause some gratuitous thrashing if someone else is trying to get the spinlock at the same time?

Signature · 2 lines
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer
#39 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 4 lines · Daniel Jacobowitz

Won't this cause some gratuitous thrashing if someone else is trying to get the spinlock at the same time?

Actually, if you look at the required semantics of ll, no. A ll by itself can never cause a sc from another cpu to fail. It's part of the semantic definition to avoid potential livelock cases, e.g.

A does ll
B does ll, foiling A's lock attempt
A does sc, which fails
A does ll, foiling B's lock attempt
B does sc, which fails
B does ll, foiling A's lock attempt
...

Instead, this case becomes:
A does ll
B does ll
A does sc, which succeeds, even though B has done a ll B does sc which fails
A does critical section work
B spins on ll until A releases the lock

-Justin

#19 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Dominic Sweetman
Quoted 14 lines · Dominic Sweetman
Quoted 2 lines · Hiroyuki Machida

I think we can assume CPU has branch-likely insns, if CPU has MIPS ISA 2 or greater ISA..

"MIPS II" is officially the instruction set introduced for the long lost R6000 CPU.

But "MIPS II" is now used to mean "the 32-bit subset of MIPS III" (which is extremely close to the same thing, but I'm never quite sure about the last details of the R6000 - Kevin would remember better, probably).

Quoted 5 lines

OK: branch-likely is definitely part of MIPS II and MIPS32. There are still MIPS CPUs in regular use which are based on MIPS I and don't provide them. Generally the advantages of MIPS II are slight, so if you want to build a kernel which will not require instruction-set variants, it's no big deal to restrict it to MIPS I.

There's load interlocks, which you can depend on starting with MIPS II. They're also present on the TX39 and VR41xx.

For non-PIC code, I see around a 5-7% reduction in userland code size by compiling with -Wa,-mips2, which afaict just eliminates the generation of nops after loads. The compiler is still generating code anticipating the delays, which is good.

For PIC code, I remember the benefits being in the same range. And you're fighting gas, which will generate load-delay nops in the middle of la/lw/sw macro expansions in PIC mode regardless of what mips architecture you're building for. I made some patches against an older binutils to eliminate those nops for mips2 and up, and got ANOTHER 5-7%, of course depending on actual instruction mix.

Given that I tossed out the SVR4 ABI in search of code density in snow, I'm probably a little abnormal in these concerns. But other people on small platforms may care.

Jay

#20 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 3 lines · Jay Carlson

Given that I tossed out the SVR4 ABI in search of code density in snow, I'm probably a little abnormal in these concerns. But other people on small platforms may care.

SNOW certainly was a nice invention and the definition of small is changing. Are you planning to keep up the support for SNOW?

Ralf

#40 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Justin Carlson

Note that the issue of a "LL" will trigger bus watching activity in the system logic (and maybe delays?) I would personally try to avoid issuing "dangling" ll's in the normal case, even though the functionality would be ok, and in some cases they are inavoidable.

/Hartvig

Quoted 31 lines · Justin Carlson
Quoted 4 lines · Daniel Jacobowitz

Won't this cause some gratuitous thrashing if someone else is trying to get the spinlock at the same time?

Actually, if you look at the required semantics of ll, no. A ll by itself can never cause a sc from another cpu to fail. It's part of the semantic definition to avoid potential livelock cases, e.g.

A does ll
B does ll, foiling A's lock attempt
A does sc, which fails
A does ll, foiling B's lock attempt
B does sc, which fails
B does ll, foiling A's lock attempt
...

Instead, this case becomes:
A does ll
B does ll
A does sc, which succeeds, even though B has done a ll B does sc which fails
A does critical section work
B spins on ll until A releases the lock

-Justin

Signature · 4 lines
 _    _   _____  ____     Hartvig Ekner        Mailto:[email protected]
 |\  /| | |____)(____                          Direct: +45 4486 5503
 | \/ | | |     _____)    MIPS Denmark         Switch: +45 4486 5555
T E C H N O L O G I E S   http://www.mips.com  Fax...: +45 4486 5556
#41 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 4 lines · Hartvig Ekner

Note that the issue of a "LL" will trigger bus watching activity in the system logic (and maybe delays?) I would personally try to avoid issuing "dangling" ll's in the normal case, even though the functionality would be ok, and in some cases they are inavoidable.

I'd suppose the address comparator to be active all the time, as it's the
simpler approach and involves trivial asynchronous hardware and certainly no performance hit.

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#42 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Hartvig Ekner
Quoted 4 lines · Hartvig Ekner

Note that the issue of a "LL" will trigger bus watching activity in the system logic (and maybe delays?) I would personally try to avoid issuing "dangling" ll's in the normal case, even though the functionality would be ok, and in some cases they are inavoidable.

Some of the processor manuals explicitly note that the execution of a ll instruction may not be visible at all externally. That's the case if the address is already in a primary cache line in exclusive (ll) or dirty (sc) state. That makes even if a processor supports full coherency since there is no reason to indicate the update to any other external agent. Or am I missing something?

Ralf

#43 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 6 lines · Ralf Baechle

Some of the processor manuals explicitly note that the execution of a ll instruction may not be visible at all externally. That's the case if the address is already in a primary cache line in exclusive (ll) or dirty (sc) state. That makes even if a processor supports full coherency since there is no reason to indicate the update to any other external agent. Or am I missing something?

By definition, apart from an ordinary load, an "ll" does only the
following two additional actions:

1. Loads the LLAddr register (it's visible in CP0, at least in certain implementations) to set up the ll comparator.

2. Sets the LLbit flip-flop to set the ll state to valid initially.

None of the actions should normally involve externally visible activity.

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#44 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Some of MIPS's cores do externalize the event of a "LL" and make it visible on the bus interface. Similarly, the SC is externalized and requires a go/nogo response from the system logic. Think of it as putting a shared LLAddr & LLBit outside the processor. The SC will only succeed if the internal LLBit is ok *and* the external logic gives the go-ahead as well.

The reasoning behind all this is that one can then utilize LL/SC in multi CPU systems without full coherency support being required.

But then again, this might not be relevant for MIPS/Linux as it will not run without full HW coherency on multiple CPUs?

/Hartvig

Quoted 26 lines · Maciej W. Rozycki
Quoted 6 lines · Ralf Baechle

Some of the processor manuals explicitly note that the execution of a ll instruction may not be visible at all externally. That's the case if the address is already in a primary cache line in exclusive (ll) or dirty (sc) state. That makes even if a processor supports full coherency since there is no reason to indicate the update to any other external agent. Or am I missing something?

By definition, apart from an ordinary load, an "ll" does only the
following two additional actions:

1. Loads the LLAddr register (it's visible in CP0, at least in certain implementations) to set up the ll comparator.

2. Sets the LLbit flip-flop to set the ll state to valid initially.

None of the actions should normally involve externally visible activity.

(quoted diff) +3 -0
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
Signature · 4 lines
 _    _   _____  ____     Hartvig Ekner        Mailto:[email protected]
 |\  /| | |____)(____                          Direct: +45 4486 5503
 | \/ | | |     _____)    MIPS Denmark         Switch: +45 4486 5555
T E C H N O L O G I E S   http://www.mips.com  Fax...: +45 4486 5556
#45 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 6 lines · Hartvig Ekner

Some of MIPS's cores do externalize the event of a "LL" and make it visible on the bus interface. Similarly, the SC is externalized and requires a go/nogo response from the system logic. Think of it as putting a shared LLAddr & LLBit outside the processor. The SC will only succeed if the internal LLBit is ok *and* the external logic gives the go-ahead as well.

OK, but an external register shouldn't need any additional CPU time to
get its contents latched.

Quoted 2 lines

The reasoning behind all this is that one can then utilize LL/SC in multi CPU systems without full coherency support being required.

Nor should an external comparator.

Quoted 2 lines

But then again, this might not be relevant for MIPS/Linux as it will not run without full HW coherency on multiple CPUs?

How do you maintain coherency on such a system? To support such a model
all shared area write accesses should be followed by explicit synchronization requests. It should be trivial but tedious to do for Linux, but it might not be that easy for threads.

One bit I've forgotten about "ll" -- it also implies a "sync".

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#21 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Ralf Baechle
Quoted 8 lines · Ralf Baechle
Quoted 3 lines · Jay Carlson

Given that I tossed out the SVR4 ABI in search of code density in snow, I'm probably a little abnormal in these concerns. But other people on small platforms may care.

SNOW certainly was a nice invention and the definition of small is changing. Are you planning to keep up the support for SNOW?

Yeah, although it's slow going. As usual, "which toolchain" plays a major part in the delay :-)

(Quick background for the list: Because there's such a large code size penalty to PIC/abicalls, I resurrected the bad old Linux/SVR3 statically linked, dynamically loaded libraries, which are linked at absolute locations. Shane Nay took this from a cute demo to a working distribution for the Agenda VR3; Brian Webb helped. Typical code reduction is ~25-40%, eg 391k->272k.)

I think I finally have a working jumptable implementation, which should allow (careful!) upgrades of libraries without triggering app rebuilds. The pain was not actually jumptables themselves; it was getting all exported data in a library shuffled around so it could live at fixed addresses. -fdata-sections let me do most of this with linker scripts. Unfortunately, g++ was still emitting "int foo;" in ".bss", so I had to fix it to generate the proper ".bss.foo".

I created a toolchain builder based on the RH71 SRPMs. About halfway through this, I remembered why I hadn't upgraded from glibc 2.0 to 2.2 before---the library size doubled. So after getting a few huge statically linked executables tested to make sure the toolchain was sound, I backtracked to uclibc and the old Agenda glibc "2.0.7". I have plausible-looking builds of both, but I haven't actually run them on real hardware.....

(Is there any hope of patching glibc 2.2.4 with the sglibc patches? If so, how?)

I started writing a /lib/ld-snow.so.1 to get the library loading code out of the main body of executables.

I'm hoping to have a beta of a full toolchain/library build tool shipped this weekend or next, and from there hook into the community efforts to automate rebuilding the Agenda VR3 from source. I figure X and a pile of C++ fltk apps will expose any lingering bugs in the toolchain and new bugs in the shared library mechanism.

I'd like to be less Agenda-centric. I think this work would benefit the Helio as well as other small platforms. But as you say, the definition of "small" is changing; hopefully in few years, machines will be big enough to support the SVR4 ABI without second thoughts. I don't know; it's possible that the really low end machines will just get cheaper instead of bigger.

Speaking of small, anybody have a Linux box with a CPU with working MIPS16 support? (Other than Vr41xx; I have plenty of those to test on.) I have a small test case I'd like someone to run.

Jay

#22 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 1 line · Jay Carlson

(Quick background for the list: Because there's such a large code size penalty to PIC/abicalls, I resurrected the bad old Linux/SVR3 statically linked, dynamically loaded libraries, which are linked at absolute locations. Shane Nay took this from a cute demo to a working distribution for the Agenda VR3; Brian Webb helped. Typical code reduction is ~25-40%, eg 391k->272k.)

Oh yes, performance. Apps on the Agenda VR3 built in the snow ABI are dramatically faster/more responsive. If you don't believe me, go search the agenda-dev list and read the testimonials :-)

I don't fully understand why, though. Here are my speculations; bear in mind that the VR3 and some of the other small boxes have 16-bit memory interfaces with small i/d caches.

1) Better icache efficiency.

2) Fewer loads (and stalls) to get typical work done. In PIC, you need a load per symbol reference, and that's every function call.

3) Better dcache efficiency. The GOT no longer needs to be hit for those symbol references.

4) Reduced TLB usage. The GOT pages for each module are quite hot, so now that we're no longer touching them, their 4k (ouch) TLB entries can point somewhere more useful.

5) No symbol resolution at load time. For C++ apps, this can help startup a lot. (prelinking fixes this too)

6) Better scheduling from gcc. egcs seemed to do a better job of arranging loads ahead of use when building non-pic; on the TX39, this helps even more due to non-blocking loads.

I dunno.

Jay

#46 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Maciej W. Rozycki
Quoted 4 lines · Maciej W. Rozycki

How do you maintain coherency on such a system? To support such a model
all shared area write accesses should be followed by explicit synchronization requests. It should be trivial but tedious to do for Linux, but it might not be that easy for threads.

This would have to be a loosely coupled system - something using either software coherency or uncached accesses for the shared areas. And then LL/SC for synchronization primitives.

There are a fair number of SOC designs like this, even with more than two processors.

/Hartvig

author
#48 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Daniel Jacobowitz
Quoted 13 lines · Daniel Jacobowitz
Quoted 12 lines · H . J . Lu
Quoted 10 lines · Dominic Sweetman
Quoted 3 lines · H . J . Lu

I can change glibc not to use branch-likely without using nop. But it may require one or two instructions outside of the loop. Should I do it given what we know now?

I would not recommend using "branch likely" in assembler coding, if that's what you're asking.

Quoted 6 lines
Quoted 3 lines

Here is a patch to remove branch likely. But I couldn't find a way not to fill the delay slot with nop. BTW, is that safe to remove ".set noreorder"?

You mean, if there is nothing which can be put there? Yes, it's safe.

Here is a new patch. I removed the extra "ll" in the delay slot.

H.J.
---
2002-02-05 H.J. Lu <[email protected]>

* sysdeps/mips/pspinlock.c (__pthread_spin_lock): Not use branch likely. Remove ".set noreorder".
* sysdeps/mips/pt-machine.h (testandset): Liekwise. (__compare_and_swap): Liekwise.

2002-02-05 H.J. Lu <[email protected]>

* sysdeps/mips/atomicity.h (exchange_and_add): Not use branch likely. Remove ".set noreorder".
(atomic_add): Likewise.
(compare_and_swap): Likewise.
* sysdeps/unix/sysv/linux/mips/sys/tas.h (_test_and_set):
Likewise.

linuxthreads/sysdeps/mips/pspinlock.c renamed from linuxthreads/sysdeps/mips/pspinlock.c.llsc +3 -7
@@ -32,17 +32,13 @@ __pthread_spin_lock (pthread_spinlock_t 
   unsigned int tmp1, tmp2;
 
   asm volatile
-    ("\t\t\t# spin_lock\n\t"
-     "ll	%1,%3\n"
+    ("\t\t\t# spin_lock\n"
      "1:\n\t"
-     ".set	push\n\t"
-     ".set	noreorder\n\t"
+     "ll	%1,%3\n"
      "bnez	%1,1b\n\t"
      " li	%2,1\n\t"
      "sc	%2,%0\n\t"
-     "beqzl	%2,1b\n\t"
-     " ll	%1,%3\n\t"
-     ".set	pop"
+     "beqz	%2,1b\n\t"
      : "=m" (*lock), "=&r" (tmp1), "=&r" (tmp2)
      : "m" (*lock)
      : "memory");
linuxthreads/sysdeps/mips/pt-machine.h renamed from linuxthreads/sysdeps/mips/pt-machine.h.llsc +3 -7
@@ -57,18 +57,14 @@ __compare_and_swap (long int *p, long in
   long int ret, temp;
 
   __asm__ __volatile__
-    ("/* Inline compare & swap */\n\t"
-     "ll	%1,%5\n"
+    ("/* Inline compare & swap */\n"
      "1:\n\t"
-     ".set	push\n\t"
-     ".set	noreorder\n\t"
+     "ll	%1,%5\n"
      "bne	%1,%3,2f\n\t"
      " move	%0,$0\n\t"
      "move	%0,%4\n\t"
      "sc	%0,%2\n\t"
-     "beqzl	%0,1b\n\t"
-     " ll	%1,%5\n\t"
-     ".set	pop\n"
+     "beqz	%0,1b\n\t"
      "2:\n\t"
      "/* End compare & swap */"
      : "=&r" (ret), "=&r" (temp), "=m" (*p)
sysdeps/mips/atomicity.h renamed from sysdeps/mips/atomicity.h.llsc +9 -21
@@ -32,16 +32,12 @@ exchange_and_add (volatile uint32_t *mem
   int result, tmp;
 
   __asm__ __volatile__
-    ("/* Inline exchange & add */\n\t"
+    ("/* Inline exchange & add */\n"
+     "1:\n"
      "ll	%0,%3\n"
-     "1:\n\t"
      "addu	%1,%4,%0\n\t"
      "sc	%1,%2\n\t"
-     ".set	push\n\t"
-     ".set	noreorder\n\t"
-     "beqzl	%1,1b\n\t"
-     " ll	%0,%3\n\t"
-     ".set	pop\n\t"
+     "beqz	%1,1b\n\t"
      "/* End exchange & add */"
      : "=&r"(result), "=&r"(tmp), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -57,16 +53,12 @@ atomic_add (volatile uint32_t *mem, int 
   int result;
 
   __asm__ __volatile__
-    ("/* Inline atomic add */\n\t"
-     "ll	%0,%2\n"
+    ("/* Inline atomic add */\n"
      "1:\n\t"
+     "ll	%0,%2\n"
      "addu	%0,%3,%0\n\t"
      "sc	%0,%1\n\t"
-     ".set	push\n\t"
-     ".set	noreorder\n\t"
-     "beqzl	%0,1b\n\t"
-     " ll	%0,%2\n\t"
-     ".set	pop\n\t"
+     "beqz	%0,1b\n\t"
      "/* End atomic add */"
      : "=&r"(result), "=m"(*mem)
      : "m" (*mem), "r"(val)
@@ -80,18 +72,14 @@ compare_and_swap (volatile long int *p, 
   long int ret, temp;
 
   __asm__ __volatile__
-    ("/* Inline compare & swap */\n\t"
-     "ll	%1,%5\n"
+    ("/* Inline compare & swap */\n"
      "1:\n\t"
-     ".set	push\n\t"
-     ".set	noreorder\n\t"
+     "ll	%1,%5\n"
      "bne	%1,%3,2f\n\t"
      " move	%0,$0\n\t"
      "move	%0,%4\n\t"
      "sc	%0,%2\n\t"
-     "beqzl	%0,1b\n\t"
-     " ll	%1,%5\n\t"
-     ".set	pop\n"
+     "beqz	%0,1b\n\t"
      "2:\n\t"
      "/* End compare & swap */"
      : "=&r" (ret), "=&r" (temp), "=m" (*p)
sysdeps/unix/sysv/linux/mips/sys/tas.h renamed from sysdeps/unix/sysv/linux/mips/sys/tas.h.llsc +3 -7
@@ -42,17 +42,13 @@ _test_and_set (int *p, int v) __THROW
   int r, t;
 
   __asm__ __volatile__
-    ("/* Inline test and set */\n\t"
-     "ll	%0,%3\n"
+    ("/* Inline test and set */\n"
      "1:\n\t"
-     ".set	push\n\t"
-     ".set	noreorder\n\t"
+     "ll	%0,%3\n"
      "beq	%0,%4,2f\n\t"
      " move	%1,%4\n\t"
      "sc	%1,%2\n\t"
-     "beqzl	%1,1b\n\t"
-     " ll	%0,%3\n\t"
-     ".set	pop\n"
+     "beqz	%1,1b\n\t"
      "2:\n\t"
      "/* End test and set */"
      : "=&r" (r), "=&r" (t), "=m" (*p)
#47 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to Hartvig Ekner
Quoted 7 lines · Hartvig Ekner

Some of MIPS's cores do externalize the event of a "LL" and make it visible on the bus interface. Similarly, the SC is externalized and requires a go/nogo response from the system logic. Think of it as putting a shared LLAddr & LLBit outside the processor. The SC will only succeed if the internal LLBit is ok *and* the external logic gives the go-ahead as well.

Quoted 5 lines

The reasoning behind all this is that one can then utilize LL/SC in multi CPU systems without full coherency support being required.

But then again, this might not be relevant for MIPS/Linux as it will not run without full HW coherency on multiple CPUs?

Linux could easily be hacked into handle such a configuration as a cluster. Anything else would be a pretty large job.

Ralf

author
#49 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to H . J . Lu
Quoted 14 lines · H . J . Lu
Quoted 13 lines · Daniel Jacobowitz
Quoted 12 lines · H . J . Lu
Quoted 10 lines · Dominic Sweetman
Quoted 3 lines · H . J . Lu

I can change glibc not to use branch-likely without using nop. But it may require one or two instructions outside of the loop. Should I do it given what we know now?

I would not recommend using "branch likely" in assembler coding, if that's what you're asking.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

Here is a patch to remove branch likely. But I couldn't find a way not to fill the delay slot with nop. BTW, is that safe to remove ".set noreorder"?

You mean, if there is nothing which can be put there? Yes, it's safe.

Here is a new patch. I removed the extra "ll" in the delay slot.

This patch is wrong. The assmebler turns that into

0x2ab0f724 <__pthread_alt_lock+212>: ll v1,0(s1)
0x2ab0f728 <__pthread_alt_lock+216>: bne v1,s0,0x2ab0f744 <__pthread_alt_lock+244>

0x2ab0f72c <__pthread_alt_lock+220>:    nop
0x2ab0f730 <__pthread_alt_lock+224>:    move    a1,zero
0x2ab0f734 <__pthread_alt_lock+228>:    move    a1,v0
0x2ab0f738 <__pthread_alt_lock+232>:    sc      a1,0(s1)

0x2ab0f73c <__pthread_alt_lock+236>: beqz a1,0x2ab0f724 <__pthread_alt_lock+212>
0x2ab0f740 <__pthread_alt_lock+240>: nop

If I do

__asm__ __volatile__

    ("/* Inline compare & swap */\n"
     "1:\n\t"
     "ll        %1,%5\n\t"
     "move      %0,$0\n\t"
     "bne       %1,%3,2f\n\t"
     "move      %0,%4\n\t"
     "sc        %0,%2\n\t"
     "beqz      %0,1b\n\t"
     "2:\n\t"
     "/* End compare & swap */"
     : "=&r" (ret), "=&r" (temp), "=m" (*p)
     : "r" (oldval), "r" (newval), "m" (*p)
     : "memory");

The assembler will do

0xd724 <__pthread_alt_lock+212>:        ll      v1,0(s1)
0xd728 <__pthread_alt_lock+216>:        move    a1,zero
0xd72c <__pthread_alt_lock+220>:	bne v1,s0,0xd744 <__pthread_alt_lock+244>
0xd730 <__pthread_alt_lock+224>:        nop
0xd734 <__pthread_alt_lock+228>:        move    a1,v0
0xd738 <__pthread_alt_lock+232>:        sc      a1,0(s1)

0xd73c <__pthread_alt_lock+236>: beqz a1,0xd724 <__pthread_alt_lock+212>
0xd740 <__pthread_alt_lock+240>: nop

There is an extra "nop" in the delay slot. I don't think gas is smart enough to fill the delay slot. I will put back those ".set noredor".

H.J.

#50 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

Quoted 11 lines · H . J . Lu
0xd724 <__pthread_alt_lock+212>:        ll      v1,0(s1)
0xd728 <__pthread_alt_lock+216>:        move    a1,zero
0xd72c <__pthread_alt_lock+220>:	bne v1,s0,0xd744 <__pthread_alt_lock+244>
0xd730 <__pthread_alt_lock+224>:        nop
0xd734 <__pthread_alt_lock+228>:        move    a1,v0
0xd738 <__pthread_alt_lock+232>:        sc      a1,0(s1)

0xd73c <__pthread_alt_lock+236>: beqz a1,0xd724 <__pthread_alt_lock+212>
0xd740 <__pthread_alt_lock+240>: nop

There is an extra "nop" in the delay slot. I don't think gas is smart enough to fill the delay slot. I will put back those ".set noredor".

The code uses the same count of cycles either way. As you may see above,
gas was smart enough to fill the "ll" load delay slot. Maybe it should prefer filling the branch delay slot instead due to smaller code...

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#51 lore raw

Re: PATCH: Fix ll/sc for mips (take 3)

In reply to H . J . Lu
Quoted 23 lines · H . J . Lu

__asm__ __volatile__

    ("/* Inline compare & swap */\n"
     "1:\n\t"
     "ll        %1,%5\n\t"
     "move      %0,$0\n\t"
     "bne       %1,%3,2f\n\t"
     "move      %0,%4\n\t"
     "sc        %0,%2\n\t"
     "beqz      %0,1b\n\t"
     "2:\n\t"
     "/* End compare & swap */"
     : "=&r" (ret), "=&r" (temp), "=m" (*p)
     : "r" (oldval), "r" (newval), "m" (*p)
     : "memory");

The assembler will do

0xd724 <__pthread_alt_lock+212>:        ll      v1,0(s1)
0xd728 <__pthread_alt_lock+216>:        move    a1,zero
0xd72c <__pthread_alt_lock+220>:	bne v1,s0,0xd744 <__pthread_alt_lock+244>
0xd730 <__pthread_alt_lock+224>:        nop
0xd734 <__pthread_alt_lock+228>:        move    a1,v0
0xd738 <__pthread_alt_lock+232>:        sc      a1,0(s1)
Quoted 5 lines

0xd73c <__pthread_alt_lock+236>: beqz a1,0xd724 <__pthread_alt_lock+212>
0xd740 <__pthread_alt_lock+240>: nop

There is an extra "nop" in the delay slot. I don't think gas is smart enough to fill the delay slot. I will put back those ".set noredor".

The solution is to move the move instruction in front of the branch instruction. The assembler will then move it into the delay slot:

__asm__ __volatile__

     ("/* Inline compare & swap */\n"
      "1:\n\t"
      "ll        %1,%5\n\t"
      "move      %0,$0\n\t"
      "move      %0,%4\n\t"
      "bne       %1,%3,2f\n\t"
      "sc        %0,%2\n\t"
      "beqz      %0,1b\n\t"
      "2:\n\t"
      "/* End compare & swap */"
      : "=&r" (ret), "=&r" (temp), "=m" (*p)
      : "r" (oldval), "r" (newval), "m" (*p)
      : "memory");

Also this function looks like a good candidate for inlining (Is it actually inlined? Haven't checked ...) where depending on it's use the address of *p is loaded twice from the GOT, so changing the code to:

__asm__ __volatile__

     ("/* Inline compare & swap */\n"
      "1:\n\t"
      "ll        %1,(%5)\n\t"
      "move      %0,$0\n\t"
      "move      %0,%4\n\t"
      "bne       %1,%3,2f\n\t"
      "sc        %0,(%2)\n\t"
      "beqz      %0,1b\n\t"
      "2:\n\t"
      "/* End compare & swap */"
      : "=&r" (ret), "=&r" (temp), "=r" (p)
      : "r" (oldval), "r" (newval), "r" (p)
      : "memory");

will avoid having to pay that PIC bloat twice and get you around the gas inefficiency of putting in too many nops into PIC code.

Ralf

author
#52 lore raw

Why does -g turn off filling the delat slot?

Quoted 7 lines · Ralf Baechle
Quoted 3 lines

There is an extra "nop" in the delay slot. I don't think gas is smart enough to fill the delay slot. I will put back those ".set noredor".

The solution is to move the move instruction in front of the branch instruction. The assembler will then move it into the delay slot:

I found out why it didn't work for me. The problem is -g turns off filling the delay slot. The mips as has

    case 'g':
      if (arg == NULL)
        mips_debug = 2;
      else    
        mips_debug = atoi (arg);
      /* When the MIPS assembler sees -g or -g2, it does not do
         optimizations which limit full symbolic debugging.  We take 
         that to be equivalent to -O0.  */
      if (mips_debug == 2)
        mips_optimize = 1;
      break;  

It doesn't matter of you pass -O to as or not. I'd like to override it if -O is seen.

H.J.

author
#53 lore raw

PATCH: Modify the mips gas behavior for -g -O

Quoted 24 lines · H . J . Lu
Quoted 7 lines · Ralf Baechle
Quoted 3 lines

There is an extra "nop" in the delay slot. I don't think gas is smart enough to fill the delay slot. I will put back those ".set noredor".

The solution is to move the move instruction in front of the branch instruction. The assembler will then move it into the delay slot:

I found out why it didn't work for me. The problem is -g turns off filling the delay slot. The mips as has

    case 'g':
      if (arg == NULL)
        mips_debug = 2;
      else    
        mips_debug = atoi (arg);
      /* When the MIPS assembler sees -g or -g2, it does not do
         optimizations which limit full symbolic debugging.  We take 
         that to be equivalent to -O0.  */
      if (mips_debug == 2)
        mips_optimize = 1;
      break;  
Quoted 4 lines

It doesn't matter of you pass -O to as or not. I'd like to override it if -O is seen.

Here is a patch which does what I want. Any comments?

Eric, can you approve

http://sources.redhat.com/ml/binutils/2002-02/msg00028.html

Thanks.

H.J.
----
2002-02-06 H.J. Lu ([email protected])

* config/tc-mips.c (mips_optimize): Initialize to -2. (md_begin): Set mips_optimize to -mips_optimize if it is less than 0.
(md_parse_option): Set mips_optimize to 1 for -g only if it is less than 0.

config/tc-mips.c renamed from config/tc-mips.c.opt +5 -2
@@ -431,7 +431,7 @@ static int mips_frame_reg_valid = 0;
    unneeded NOPs and swap branch instructions when possible.  A value
    of 1 means to not swap branches.  A value of 0 means to always
    insert NOPs.  */
-static int mips_optimize = 2;
+static int mips_optimize = -2;
 
 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
    equivalent to seeing no -g option at all.  */
@@ -1020,6 +1020,9 @@ md_begin ()
   int target_cpu_had_mips16 = 0;
   const struct mips_cpu_info *ci;
 
+  if (mips_optimize < 0)
+    mips_optimize = -mips_optimize;
+
   /* GP relative stuff not working for PE */
   if (strncmp (TARGET_OS, "pe", 2) == 0
       && g_switch_value != 0)
@@ -9794,7 +9797,7 @@ md_parse_option (c, arg)
       /* When the MIPS assembler sees -g or -g2, it does not do
          optimizations which limit full symbolic debugging.  We take
          that to be equivalent to -O0.  */
-      if (mips_debug == 2)
+      if (mips_debug == 2 && mips_optimize < 0)
 	mips_optimize = 1;
       break;
 
#54 lore raw

Re: PATCH: Modify the mips gas behavior for -g -O

Quoted 3 lines

Here is a patch which does what I want. Any comments?

Does anyone care if we have MIPS compatibility? I remember seeing this a few years ago and wondering why we were doing it this way. I remember at one time debuggers had problems with optimized code, but gdb has come a long way since then. Is there any reason to have this code in there at all now, i.e. should we just go off of optimization level and not debug level at all?

Quoted 4 lines

Eric, can you approve

http://sources.redhat.com/ml/binutils/2002-02/msg00028.html

If this is the one for header file information, then go ahead. We need to come up with a better way of doing this though.

-eric

Signature · 1 line
I will not use abbrev.
#55 lore raw

Re: PATCH: Modify the mips gas behavior for -g -O

Quoted 9 lines · Eric Christopher
Quoted 2 lines

Here is a patch which does what I want. Any comments?

Does anyone care if we have MIPS compatibility? I remember seeing this a few years ago and wondering why we were doing it this way. I remember at one time debuggers had problems with optimized code, but gdb has come a long way since then. Is there any reason to have this code in there at all now, i.e. should we just go off of optimization level and not debug level at all?

In general the reason for this sort of compatibility is for easier gcc support. It's normally more convenient if the GNU assembler and the native assembler present the same interface, so that you don't have to use --with-gnu-assembler to get the right result when configuring gcc.

It's far more common to invoke the assembler via the compiler than to invoke it directly, so making it work correctly when invoked via the compiler is normally best.

H.J., my question on this patch would be: why is -g being passed to the assembler?

Ian

#56 lore raw

Re: PATCH: Modify the mips gas behavior for -g -O

Quoted 6 lines

In general the reason for this sort of compatibility is for easier gcc support. It's normally more convenient if the GNU assembler and the native assembler present the same interface, so that you don't have to use --with-gnu-assembler to get the right result when configuring gcc.

Ok. That's fair enough. I was hoping you'd pipe up here. :)

-eric

Signature · 1 line
I will not use abbrev.
author
#57 lore raw

PATCH: Define SUBTARGET_ASM_DEBUGGING_SPEC for Linux/mips

In reply to Ian Lance Taylor
Quoted 4 lines · Ian Lance Taylor

H.J., my question on this patch would be: why is -g being passed to the assembler?

Here is a patch similar to Irix 6.

H.J.
----
2002-02-06 H.J. Lu <[email protected]>

* config/mips/linux.h (SUBTARGET_ASM_DEBUGGING_SPEC): Defined.

config/mips/linux.h renamed from config/mips/linux.h.gas +3 -0
@@ -170,6 +170,9 @@ Boston, MA 02111-1307, USA.  */
 %{!fno-PIC:%{!fno-pic:-KPIC}} \
 %{fno-PIC:-non_shared} %{fno-pic:-non_shared}"
 
+#undef SUBTARGET_ASM_DEBUGGING_SPEC
+#define SUBTARGET_ASM_DEBUGGING_SPEC "-g0"
+
 /* We don't need those nonsenses.  */
 #undef INVOKE__main
 #undef CTOR_LIST_BEGIN
#58 lore raw

Re: PATCH: Define SUBTARGET_ASM_DEBUGGING_SPEC for Linux/mips

Quoted 10 lines

Here is a patch similar to Irix 6.

H.J.
----
2002-02-06 H.J. Lu <[email protected]>

* config/mips/linux.h (SUBTARGET_ASM_DEBUGGING_SPEC): Defined.

Have at. :)

-eric

Signature · 1 line
I will not use abbrev.
#83 lore raw

PATCH 1/1 linux: RFC: elf_check_arch() rework

Hello,

After noticing I cannot run a trivial ELF64 program because of the
EF_MIPS_ARCH_3 flag (quite a reasonable setting for a 64-bit executable, isn't it?), I concluded a considerable rework of elf_check_arch() is needed as what we currently have is inadequate.

Here is my proposal. I think binfmt_elf.c for mips and binfmt_elf32.c
for mips64 should accept all ELF32 binaries either with no ABI mark (as produced by most versions of binutils) or with a 32 (aka o32) ABI one and binfmt_elf.c for mips64 should accept all ELF32 binaries with an n32 ABI mark and all ELF64 ones (which essentially means the 64 aka n64 ABI). Everything else (i.e. o64 and EABIs) is rejected. The patch adds necessary ELF file header flag definitions and synchronizes a few wrong ones to the binutils' definitions as well.

It removes the bogus check of architecture flags as they are really
irrelevant -- the code is intended to handle executable formats and not execution of code. If a user incorporates unsupported opcodes, he'll just get SIGILL at one moment. We may actually check if an architecture is supported even no other Linux port seems to care, but then the comparison should be against mips_cpu.isa_level and not against hardcoded constants.

Note, this is an RFC at this stage -- I haven't tested the code
adequately for an immediate inclusion, even if it looks obvious. There should be no problems with code made with old binutils as unset flags are treated as (o)32.

Maciej

Signature · 141 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +

patch-mips-2.4.19-rc1-20020719-mips64-elf-2
diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/arch/mips64/kernel/binfmt_elf32.c linux-mips-2.4.19-rc1-20020719/arch/mips64/kernel/binfmt_elf32.c
--- linux-mips-2.4.19-rc1-20020719.macro/arch/mips64/kernel/binfmt_elf32.c	2002-06-29 03:01:44.000000000 +0000
+++ linux-mips-2.4.19-rc1-20020719/arch/mips64/kernel/binfmt_elf32.c	2002-07-23 23:15:27.000000000 +0000
@@ -27,8 +27,26 @@ typedef elf_greg_t elf_gregset_t[ELF_NGR
 typedef double elf_fpreg_t;
 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
 
-#define elf_check_arch(x)	\
-	((x)->e_machine == EM_MIPS)
+/*
+ * This is used to ensure we don't load something for the wrong architecture.
+ */
+#define elf_check_arch(hdr)						\
+({									\
+	int __res = 1;							\
+	struct elfhdr *__h = (hdr);					\
+									\
+	if (__h->e_machine != EM_MIPS)					\
+		__res = 0;						\
+	if (__h->e_ident[EI_CLASS] != ELFCLASS32)			\
+		__res = 0;						\
+	if ((__h->e_flags & EF_MIPS_ABI2) != 0)				\
+		__res = 0;						\
+	if (((__h->e_flags & EF_MIPS_ABI) != 0) &&			\
+	    ((__h->e_flags & EF_MIPS_ABI) != EF_MIPS_ABI_O32))		\
+		__res = 0;						\
+									\
+	__res;								\
+})
 
 #define TASK32_SIZE		0x80000000UL
 #undef ELF_ET_DYN_BASE
diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/include/asm-mips/elf.h linux-mips-2.4.19-rc1-20020719/include/asm-mips/elf.h
--- linux-mips-2.4.19-rc1-20020719.macro/include/asm-mips/elf.h	2002-04-25 02:57:43.000000000 +0000
+++ linux-mips-2.4.19-rc1-20020719/include/asm-mips/elf.h	2002-07-23 22:50:48.000000000 +0000
@@ -16,10 +16,11 @@
 #define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
 #define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
 #define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
-#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
-#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */
+#define EF_MIPS_ARCH_32     0x50000000  /* MIPS32 code.  */
+#define EF_MIPS_ARCH_64     0x60000000  /* MIPS64 code.  */
+/* The ABI of the file. */
+#define EF_MIPS_ABI_O32     0x00001000  /* O32 ABI.  */
+#define EF_MIPS_ABI_O64     0x00002000  /* O32 extended for 64 bit.  */
 
 typedef unsigned long elf_greg_t;
 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
@@ -37,10 +38,12 @@ typedef elf_fpreg_t elf_fpregset_t[ELF_N
 									\
 	if (__h->e_machine != EM_MIPS)					\
 		__res = 0;						\
-	if (((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) &&       	\
-	    ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_2) &&       	\
-	    ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32) &&      	\
-	    ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32R2))      	\
+	if (__h->e_ident[EI_CLASS] != ELFCLASS32)			\
+		__res = 0;						\
+	if ((__h->e_flags & EF_MIPS_ABI2) != 0)				\
+		__res = 0;						\
+	if (((__h->e_flags & EF_MIPS_ABI) != 0) &&			\
+	    ((__h->e_flags & EF_MIPS_ABI) != EF_MIPS_ABI_O32))		\
 		__res = 0;						\
 									\
 	__res;								\
diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/include/asm-mips64/elf.h linux-mips-2.4.19-rc1-20020719/include/asm-mips64/elf.h
--- linux-mips-2.4.19-rc1-20020719.macro/include/asm-mips64/elf.h	2002-07-22 08:49:30.000000000 +0000
+++ linux-mips-2.4.19-rc1-20020719/include/asm-mips64/elf.h	2002-07-23 23:12:28.000000000 +0000
@@ -9,21 +9,23 @@
 #include <asm/ptrace.h>
 #include <asm/user.h>
 
-#ifndef ELF_ARCH
-/* ELF register definitions */
-#define ELF_NGREG	45
-#define ELF_NFPREG	33
-
-/* ELF header e_flags defines. MIPS architecture level. */
+/* ELF header e_flags defines. */
+/* MIPS architecture level. */
 #define EF_MIPS_ARCH_1      0x00000000  /* -mips1 code.  */
 #define EF_MIPS_ARCH_2      0x10000000  /* -mips2 code.  */
 #define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
 #define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
 #define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
-#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
-#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */
+#define EF_MIPS_ARCH_32     0x50000000  /* MIPS32 code.  */
+#define EF_MIPS_ARCH_64     0x60000000  /* MIPS64 code.  */
+/* The ABI of the file. */
+#define EF_MIPS_ABI_O32     0x00001000  /* O32 ABI.  */
+#define EF_MIPS_ABI_O64     0x00002000  /* O32 extended for 64 bit.  */
+
+#ifndef ELF_ARCH
+/* ELF register definitions */
+#define ELF_NGREG	45
+#define ELF_NFPREG	33
 
 typedef unsigned long elf_greg_t;
 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
@@ -41,14 +43,9 @@ typedef elf_fpreg_t elf_fpregset_t[ELF_N
 									\
 	if (__h->e_machine != EM_MIPS)					\
 		__res = 0;						\
-	if (sizeof(elf_caddr_t) == 8 &&					\
-	    __h->e_ident[EI_CLASS] == ELFCLASS32)			\
-	        __res = 0;						\
-	if (((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) &&	\
-	    ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_2) &&	\
-	    ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32) &&	\
-	    ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32R2))	\
-                __res = 0;						\
+	if ((__h->e_ident[EI_CLASS] == ELFCLASS32) &&			\
+	    ((__h->e_flags & EF_MIPS_ABI2) == 0))			\
+		__res = 0;						\
 									\
 	__res;								\
 })
diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/include/linux/elf.h linux-mips-2.4.19-rc1-20020719/include/linux/elf.h
--- linux-mips-2.4.19-rc1-20020719.macro/include/linux/elf.h	2002-07-22 08:49:30.000000000 +0000
+++ linux-mips-2.4.19-rc1-20020719/include/linux/elf.h	2002-07-23 22:18:54.000000000 +0000
@@ -37,6 +37,9 @@ typedef __s64	Elf64_Sxword;
 #define EF_MIPS_NOREORDER 0x00000001
 #define EF_MIPS_PIC       0x00000002
 #define EF_MIPS_CPIC      0x00000004
+#define EF_MIPS_ABI2      0x00000020
+#define EF_MIPS_32BITMODE 0x00000100
+#define EF_MIPS_ABI       0x0000f000
 #define EF_MIPS_ARCH      0xf0000000
 
 /* These constants define the different elf file types */
#84 lore raw

Re: [patch] linux: RFC: elf_check_arch() rework

Quoted 23 lines · Maciej W. Rozycki

Hello,

After noticing I cannot run a trivial ELF64 program because of the
EF_MIPS_ARCH_3 flag (quite a reasonable setting for a 64-bit executable, isn't it?), I concluded a considerable rework of elf_check_arch() is needed as what we currently have is inadequate.

Here is my proposal. I think binfmt_elf.c for mips and binfmt_elf32.c
for mips64 should accept all ELF32 binaries either with no ABI mark (as produced by most versions of binutils) or with a 32 (aka o32) ABI one and binfmt_elf.c for mips64 should accept all ELF32 binaries with an n32 ABI mark and all ELF64 ones (which essentially means the 64 aka n64 ABI). Everything else (i.e. o64 and EABIs) is rejected. The patch adds necessary ELF file header flag definitions and synchronizes a few wrong ones to the binutils' definitions as well.

It removes the bogus check of architecture flags as they are really
irrelevant -- the code is intended to handle executable formats and not execution of code. If a user incorporates unsupported opcodes, he'll just get SIGILL at one moment. We may actually check if an architecture is supported even no other Linux port seems to care, but then the comparison should be against mips_cpu.isa_level and not against hardcoded constants.

Quoted 4 lines

Note, this is an RFC at this stage -- I haven't tested the code
adequately for an immediate inclusion, even if it looks obvious. There should be no problems with code made with old binutils as unset flags are treated as (o)32.

Well, that sounds like the right approach to me. It sounds like there's some real progress on the 64-bit tools now...

Signature · 2 lines
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer
#85 lore raw

Re: [patch] linux: RFC: elf_check_arch() rework

In reply to Maciej W. Rozycki

We at MIPS are in the process of making an ABI spec for all this, which is the intention that should be used by the tool-vendors.
So please don't change the ELF header defines.
I don't see that is wrong with checking the ISA level, I rather have an error telling me that I can't execute a certain ISA level than eventually getting a reserved instruction or something worse like something unpredictable.
You are obviously right about the elf_check_arch in the 64-bit part of the kernel is broken.
It's probably just be copied from the 32-bit part without changes, like a lot of the code in the 64-bit kernel is.

Hope that makes some sense.
/Carsten

Quoted 172 lines · Maciej W. Rozycki

Hello,

After noticing I cannot run a trivial ELF64 program because of the
EF_MIPS_ARCH_3 flag (quite a reasonable setting for a 64-bit executable, isn't it?), I concluded a considerable rework of elf_check_arch() is needed as what we currently have is inadequate.

Here is my proposal. I think binfmt_elf.c for mips and binfmt_elf32.c
for mips64 should accept all ELF32 binaries either with no ABI mark (as produced by most versions of binutils) or with a 32 (aka o32) ABI one and binfmt_elf.c for mips64 should accept all ELF32 binaries with an n32 ABI mark and all ELF64 ones (which essentially means the 64 aka n64 ABI). Everything else (i.e. o64 and EABIs) is rejected. The patch adds necessary ELF file header flag definitions and synchronizes a few wrong ones to the binutils' definitions as well.

It removes the bogus check of architecture flags as they are really
irrelevant -- the code is intended to handle executable formats and not execution of code. If a user incorporates unsupported opcodes, he'll just get SIGILL at one moment. We may actually check if an architecture is supported even no other Linux port seems to care, but then the comparison should be against mips_cpu.isa_level and not against hardcoded constants.

Note, this is an RFC at this stage -- I haven't tested the code
adequately for an immediate inclusion, even if it looks obvious. There should be no problems with code made with old binutils as unset flags are treated as (o)32.

Maciej

--

(quoted diff) +3 -0
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +

patch-mips-2.4.19-rc1-20020719-mips64-elf-2
diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/arch/mips64/kernel/binfmt_elf32.c linux-mips-2.4.19-rc1-20020719/arch/mips64/kernel/binfmt_elf32.c

arch/mips64/kernel/binfmt_elf32.c 2002-07-23 23:15:27.000000000 +0000 renamed from arch/mips64/kernel/binfmt_elf32.c 2002-06-29 03:01:44.000000000 +0000 +20 -2
@@ -27,8 +27,26 @@ typedef elf_greg_t elf_gregset_t[ELF_NGR
 typedef double elf_fpreg_t;
 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
 
-#define elf_check_arch(x)      \
-       ((x)->e_machine == EM_MIPS)
+/*
+ * This is used to ensure we don't load something for the wrong architecture.
+ */
+#define elf_check_arch(hdr)                                            \
+({                                                                     \
+       int __res = 1;                                                  \
+       struct elfhdr *__h = (hdr);                                     \
+                                                                       \
+       if (__h->e_machine != EM_MIPS)                                  \
+               __res = 0;                                              \
+       if (__h->e_ident[EI_CLASS] != ELFCLASS32)                       \
+               __res = 0;                                              \
+       if ((__h->e_flags & EF_MIPS_ABI2) != 0)                         \
+               __res = 0;                                              \
+       if (((__h->e_flags & EF_MIPS_ABI) != 0) &&                      \
+           ((__h->e_flags & EF_MIPS_ABI) != EF_MIPS_ABI_O32))          \
+               __res = 0;                                              \
+                                                                       \
+       __res;                                                          \
+})
 
 #define TASK32_SIZE            0x80000000UL
 #undef ELF_ET_DYN_BASE

diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/include/asm-mips/elf.h linux-mips-2.4.19-rc1-20020719/include/asm-mips/elf.h

include/asm-mips/elf.h 2002-07-23 22:50:48.000000000 +0000 renamed from include/asm-mips/elf.h 2002-04-25 02:57:43.000000000 +0000 +11 -8
@@ -16,10 +16,11 @@
 #define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
 #define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
 #define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
-#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
-#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */
+#define EF_MIPS_ARCH_32     0x50000000  /* MIPS32 code.  */
+#define EF_MIPS_ARCH_64     0x60000000  /* MIPS64 code.  */
+/* The ABI of the file. */
+#define EF_MIPS_ABI_O32     0x00001000  /* O32 ABI.  */
+#define EF_MIPS_ABI_O64     0x00002000  /* O32 extended for 64 bit.  */
 
 typedef unsigned long elf_greg_t;
 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
@@ -37,10 +38,12 @@ typedef elf_fpreg_t elf_fpregset_t[ELF_N
                                                                        \
        if (__h->e_machine != EM_MIPS)                                  \
                __res = 0;                                              \
-       if (((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) &&        \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_2) &&        \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32) &&       \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32R2))       \
+       if (__h->e_ident[EI_CLASS] != ELFCLASS32)                       \
+               __res = 0;                                              \
+       if ((__h->e_flags & EF_MIPS_ABI2) != 0)                         \
+               __res = 0;                                              \
+       if (((__h->e_flags & EF_MIPS_ABI) != 0) &&                      \
+           ((__h->e_flags & EF_MIPS_ABI) != EF_MIPS_ABI_O32))          \
                __res = 0;                                              \
                                                                        \
        __res;                                                          \

diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/include/asm-mips64/elf.h linux-mips-2.4.19-rc1-20020719/include/asm-mips64/elf.h

include/asm-mips64/elf.h 2002-07-23 23:12:28.000000000 +0000 renamed from include/asm-mips64/elf.h 2002-07-22 08:49:30.000000000 +0000 +15 -18
@@ -9,21 +9,23 @@
 #include <asm/ptrace.h>
 #include <asm/user.h>
 
-#ifndef ELF_ARCH
-/* ELF register definitions */
-#define ELF_NGREG      45
-#define ELF_NFPREG     33
-
-/* ELF header e_flags defines. MIPS architecture level. */
+/* ELF header e_flags defines. */
+/* MIPS architecture level. */
 #define EF_MIPS_ARCH_1      0x00000000  /* -mips1 code.  */
 #define EF_MIPS_ARCH_2      0x10000000  /* -mips2 code.  */
 #define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
 #define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
 #define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
-#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
-#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */
+#define EF_MIPS_ARCH_32     0x50000000  /* MIPS32 code.  */
+#define EF_MIPS_ARCH_64     0x60000000  /* MIPS64 code.  */
+/* The ABI of the file. */
+#define EF_MIPS_ABI_O32     0x00001000  /* O32 ABI.  */
+#define EF_MIPS_ABI_O64     0x00002000  /* O32 extended for 64 bit.  */
+
+#ifndef ELF_ARCH
+/* ELF register definitions */
+#define ELF_NGREG      45
+#define ELF_NFPREG     33
 
 typedef unsigned long elf_greg_t;
 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
@@ -41,14 +43,9 @@ typedef elf_fpreg_t elf_fpregset_t[ELF_N
                                                                        \
        if (__h->e_machine != EM_MIPS)                                  \
                __res = 0;                                              \
-       if (sizeof(elf_caddr_t) == 8 &&                                 \
-           __h->e_ident[EI_CLASS] == ELFCLASS32)                       \
-               __res = 0;                                              \
-       if (((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) &&        \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_2) &&        \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32) &&       \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32R2))       \
-                __res = 0;                                             \
+       if ((__h->e_ident[EI_CLASS] == ELFCLASS32) &&                   \
+           ((__h->e_flags & EF_MIPS_ABI2) == 0))                       \
+               __res = 0;                                              \
                                                                        \
        __res;                                                          \
 })

diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/include/linux/elf.h linux-mips-2.4.19-rc1-20020719/include/linux/elf.h

include/linux/elf.h 2002-07-23 22:18:54.000000000 +0000 renamed from include/linux/elf.h 2002-07-22 08:49:30.000000000 +0000 +3 -0
@@ -37,6 +37,9 @@ typedef __s64 Elf64_Sxword;
 #define EF_MIPS_NOREORDER 0x00000001
 #define EF_MIPS_PIC       0x00000002
 #define EF_MIPS_CPIC      0x00000004
+#define EF_MIPS_ABI2      0x00000020
+#define EF_MIPS_32BITMODE 0x00000100
+#define EF_MIPS_ABI       0x0000f000
 #define EF_MIPS_ARCH      0xf0000000
 
 /* These constants define the different elf file types */
#86 lore raw

Re: [patch] linux: RFC: elf_check_arch() rework

Quoted 3 lines · Carsten Langgaard

We at MIPS are in the process of making an ABI spec for all this, which is the intention that should be used by the tool-vendors. So please don't change the ELF header defines. I don't see that is wrong

Well, if so you'd better be using the ones Maciej changed them to, not the ones they used to be. Check binutils/include/elf/mips.h for the current definitions of these flags; the kernel was just wrong.

Quoted 3 lines

with checking the ISA level, I rather have an error telling me that I can't execute a certain ISA level than eventually getting a reserved instruction or something worse like something unpredictable. You are

I agree with Maciej on this. It is perfectly reasonable to build a binary which can operate safely at a different ISA level (either above or below) that which it is linked at; and rather than arguing about the semantics of that, as he also pointed out, this routine is specifically supposed to be checking architecture and not ISA level; consistency across platforms has high advantages.

Quoted 184 lines

obviously right about the elf_check_arch in the 64-bit part of the kernel is broken. It's probably just be copied from the 32-bit part without changes, like a lot of the code in the 64-bit kernel is.

Hope that makes some sense.
/Carsten

Quoted 172 lines · Maciej W. Rozycki

Hello,

After noticing I cannot run a trivial ELF64 program because of the
EF_MIPS_ARCH_3 flag (quite a reasonable setting for a 64-bit executable, isn't it?), I concluded a considerable rework of elf_check_arch() is needed as what we currently have is inadequate.

Here is my proposal. I think binfmt_elf.c for mips and binfmt_elf32.c
for mips64 should accept all ELF32 binaries either with no ABI mark (as produced by most versions of binutils) or with a 32 (aka o32) ABI one and binfmt_elf.c for mips64 should accept all ELF32 binaries with an n32 ABI mark and all ELF64 ones (which essentially means the 64 aka n64 ABI). Everything else (i.e. o64 and EABIs) is rejected. The patch adds necessary ELF file header flag definitions and synchronizes a few wrong ones to the binutils' definitions as well.

It removes the bogus check of architecture flags as they are really
irrelevant -- the code is intended to handle executable formats and not execution of code. If a user incorporates unsupported opcodes, he'll just get SIGILL at one moment. We may actually check if an architecture is supported even no other Linux port seems to care, but then the comparison should be against mips_cpu.isa_level and not against hardcoded constants.

Note, this is an RFC at this stage -- I haven't tested the code
adequately for an immediate inclusion, even if it looks obvious. There should be no problems with code made with old binutils as unset flags are treated as (o)32.

Maciej

--

(quoted diff) +3 -0
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +

patch-mips-2.4.19-rc1-20020719-mips64-elf-2
diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/arch/mips64/kernel/binfmt_elf32.c linux-mips-2.4.19-rc1-20020719/arch/mips64/kernel/binfmt_elf32.c

arch/mips64/kernel/binfmt_elf32.c 2002-07-23 23:15:27.000000000 +0000 renamed from arch/mips64/kernel/binfmt_elf32.c 2002-06-29 03:01:44.000000000 +0000 +20 -2
@@ -27,8 +27,26 @@ typedef elf_greg_t elf_gregset_t[ELF_NGR
 typedef double elf_fpreg_t;
 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
 
-#define elf_check_arch(x)      \
-       ((x)->e_machine == EM_MIPS)
+/*
+ * This is used to ensure we don't load something for the wrong architecture.
+ */
+#define elf_check_arch(hdr)                                            \
+({                                                                     \
+       int __res = 1;                                                  \
+       struct elfhdr *__h = (hdr);                                     \
+                                                                       \
+       if (__h->e_machine != EM_MIPS)                                  \
+               __res = 0;                                              \
+       if (__h->e_ident[EI_CLASS] != ELFCLASS32)                       \
+               __res = 0;                                              \
+       if ((__h->e_flags & EF_MIPS_ABI2) != 0)                         \
+               __res = 0;                                              \
+       if (((__h->e_flags & EF_MIPS_ABI) != 0) &&                      \
+           ((__h->e_flags & EF_MIPS_ABI) != EF_MIPS_ABI_O32))          \
+               __res = 0;                                              \
+                                                                       \
+       __res;                                                          \
+})
 
 #define TASK32_SIZE            0x80000000UL
 #undef ELF_ET_DYN_BASE

diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/include/asm-mips/elf.h linux-mips-2.4.19-rc1-20020719/include/asm-mips/elf.h

include/asm-mips/elf.h 2002-07-23 22:50:48.000000000 +0000 renamed from include/asm-mips/elf.h 2002-04-25 02:57:43.000000000 +0000 +11 -8
@@ -16,10 +16,11 @@
 #define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
 #define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
 #define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
-#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
-#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */
+#define EF_MIPS_ARCH_32     0x50000000  /* MIPS32 code.  */
+#define EF_MIPS_ARCH_64     0x60000000  /* MIPS64 code.  */
+/* The ABI of the file. */
+#define EF_MIPS_ABI_O32     0x00001000  /* O32 ABI.  */
+#define EF_MIPS_ABI_O64     0x00002000  /* O32 extended for 64 bit.  */
 
 typedef unsigned long elf_greg_t;
 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
@@ -37,10 +38,12 @@ typedef elf_fpreg_t elf_fpregset_t[ELF_N
                                                                        \
        if (__h->e_machine != EM_MIPS)                                  \
                __res = 0;                                              \
-       if (((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) &&        \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_2) &&        \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32) &&       \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32R2))       \
+       if (__h->e_ident[EI_CLASS] != ELFCLASS32)                       \
+               __res = 0;                                              \
+       if ((__h->e_flags & EF_MIPS_ABI2) != 0)                         \
+               __res = 0;                                              \
+       if (((__h->e_flags & EF_MIPS_ABI) != 0) &&                      \
+           ((__h->e_flags & EF_MIPS_ABI) != EF_MIPS_ABI_O32))          \
                __res = 0;                                              \
                                                                        \
        __res;                                                          \

diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/include/asm-mips64/elf.h linux-mips-2.4.19-rc1-20020719/include/asm-mips64/elf.h

include/asm-mips64/elf.h 2002-07-23 23:12:28.000000000 +0000 renamed from include/asm-mips64/elf.h 2002-07-22 08:49:30.000000000 +0000 +15 -18
@@ -9,21 +9,23 @@
 #include <asm/ptrace.h>
 #include <asm/user.h>
 
-#ifndef ELF_ARCH
-/* ELF register definitions */
-#define ELF_NGREG      45
-#define ELF_NFPREG     33
-
-/* ELF header e_flags defines. MIPS architecture level. */
+/* ELF header e_flags defines. */
+/* MIPS architecture level. */
 #define EF_MIPS_ARCH_1      0x00000000  /* -mips1 code.  */
 #define EF_MIPS_ARCH_2      0x10000000  /* -mips2 code.  */
 #define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
 #define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
 #define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
-#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
-#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
-#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */
+#define EF_MIPS_ARCH_32     0x50000000  /* MIPS32 code.  */
+#define EF_MIPS_ARCH_64     0x60000000  /* MIPS64 code.  */
+/* The ABI of the file. */
+#define EF_MIPS_ABI_O32     0x00001000  /* O32 ABI.  */
+#define EF_MIPS_ABI_O64     0x00002000  /* O32 extended for 64 bit.  */
+
+#ifndef ELF_ARCH
+/* ELF register definitions */
+#define ELF_NGREG      45
+#define ELF_NFPREG     33
 
 typedef unsigned long elf_greg_t;
 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
@@ -41,14 +43,9 @@ typedef elf_fpreg_t elf_fpregset_t[ELF_N
                                                                        \
        if (__h->e_machine != EM_MIPS)                                  \
                __res = 0;                                              \
-       if (sizeof(elf_caddr_t) == 8 &&                                 \
-           __h->e_ident[EI_CLASS] == ELFCLASS32)                       \
-               __res = 0;                                              \
-       if (((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) &&        \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_2) &&        \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32) &&       \
-           ((__h->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_32R2))       \
-                __res = 0;                                             \
+       if ((__h->e_ident[EI_CLASS] == ELFCLASS32) &&                   \
+           ((__h->e_flags & EF_MIPS_ABI2) == 0))                       \
+               __res = 0;                                              \
                                                                        \
        __res;                                                          \
 })

diff -up --recursive --new-file linux-mips-2.4.19-rc1-20020719.macro/include/linux/elf.h linux-mips-2.4.19-rc1-20020719/include/linux/elf.h

include/linux/elf.h 2002-07-23 22:18:54.000000000 +0000 renamed from include/linux/elf.h 2002-07-22 08:49:30.000000000 +0000 +3 -0
@@ -37,6 +37,9 @@ typedef __s64 Elf64_Sxword;
 #define EF_MIPS_NOREORDER 0x00000001
 #define EF_MIPS_PIC       0x00000002
 #define EF_MIPS_CPIC      0x00000004
+#define EF_MIPS_ABI2      0x00000020
+#define EF_MIPS_32BITMODE 0x00000100
+#define EF_MIPS_ABI       0x0000f000
 #define EF_MIPS_ARCH      0xf0000000
 
 /* These constants define the different elf file types */
Signature · 2 lines
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer
#87 lore raw

Re: [patch] linux: RFC: elf_check_arch() rework

In reply to Carsten Langgaard
Quoted 3 lines · Carsten Langgaard

We at MIPS are in the process of making an ABI spec for all this, which is the intention that should be used by the tool-vendors. So please don't change the ELF header defines.

It'd be better the spec matched the real world...

Quoted 4 lines

I don't see that is wrong with checking the ISA level, I rather have an error telling me that I can't execute a certain ISA level than eventually getting a reserved instruction or something worse like something unpredictable.

Well, -ENOEXEC in not any more useful than SIGILL -- with the latter you
have at least an idea what happened. The ISA check is not implemented for any Linux port, so there no suitable hook in binfmt_*.c files. You might propose an implementation if that's particularly important for you.

Quoted 3 lines

You are obviously right about the elf_check_arch in the 64-bit part of the kernel is broken. It's probably just be copied from the 32-bit part without changes, like a lot of the code in the 64-bit kernel is.

Possibly, but it still makes me wonder why it wasn't adjusted at the time
binfmt_elf32.c was created...

Maciej

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#88 lore raw

Re: [patch] linux: RFC: elf_check_arch() rework

Quoted 7 lines · Maciej W. Rozycki
Quoted 3 lines · Carsten Langgaard

We at MIPS are in the process of making an ABI spec for all this, which is the intention that should be used by the tool-vendors. So please don't change the ELF header defines.

It'd be better the spec matched the real world...

Shouldn't it be the other way around, the real world should follow the spec ;-)
The whole ELF header definition is just one big mess, because we are lacking a proper ABI spec.
That's what has motivated us, to begin making this ABI spec.

We have defined the e_flags this way:

/* ELF header e_flags defines. MIPS architecture level. */

#define EF_MIPS_ARCH_1      0x00000000  /* -mips1 code.  */
#define EF_MIPS_ARCH_2      0x10000000  /* -mips2 code.  */
#define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
#define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
#define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */

The missing value 0x50000000, is because IRIX has defined a EF_MIPS_ARCH_6 and Algorithmics has a E_MIPS_ARCH_ALGOR_32, which has this value. If you look at the elf.h file in glibc, the you will see, it has the same values as the kernel.

So I would prefer we fix that in binutils, I guess it not a problem as long as you don't have a toolchain that can generate MIPS32 or MIPS64 code.

Quoted 11 lines
Quoted 5 lines

I don't see that is wrong with checking the ISA level, I rather have an error telling me that I can't execute a certain ISA level than eventually getting a reserved instruction or something worse like something unpredictable.

Well, -ENOEXEC in not any more useful than SIGILL -- with the latter you
have at least an idea what happened. The ISA check is not implemented for any Linux port, so there no suitable hook in binfmt_*.c files. You might propose an implementation if that's particularly important for you.

I would like a message telling me that I can't run this ISA level on the system.
Imagined what would happen, if you execute mips3 code and execute ld/sd instructions on a mips32 kernel (but on a 64-bit processor), the kernel only save half the register and then everything could happen.

Quoted 11 lines
Quoted 4 lines

You are obviously right about the elf_check_arch in the 64-bit part of the kernel is broken. It's probably just be copied from the 32-bit part without changes, like a lot of the code in the 64-bit kernel is.

Possibly, but it still makes me wonder why it wasn't adjusted at the time
binfmt_elf32.c was created...

Maciej

--

Review

Quoted 3 lines
(quoted diff) +3 -0
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +

--

_    _ ____  ___   Carsten Langgaard   Mailto:[email protected]
|\  /|||___)(___   MIPS Denmark        Direct: +45 4486 5527
| \/ |||    ____)  Lautrupvang 4B      Switch: +45 4486 5555
  TECHNOLOGIES     2750 Ballerup       Fax...: +45 4486 5556
                   Denmark             http://www.mips.com
#89 lore raw

Re: [patch] linux: RFC: elf_check_arch() rework

Quoted 2 lines · Carsten Langgaard

Shouldn't it be the other way around, the real world should follow the spec ;-)

Where is the spec? If one were clearly available, the world would
follow. Otherwise, having no other definite reference BFD is *the* spec, as usual (see Alpha/ELF for another example).

Quoted 17 lines

The whole ELF header definition is just one big mess, because we are lacking a proper ABI spec.
That's what has motivated us, to begin making this ABI spec.

We have defined the e_flags this way:

/* ELF header e_flags defines. MIPS architecture level. */

#define EF_MIPS_ARCH_1      0x00000000  /* -mips1 code.  */
#define EF_MIPS_ARCH_2      0x10000000  /* -mips2 code.  */
#define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
#define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
#define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */
Quoted 2 lines

The missing value 0x50000000, is because IRIX has defined a EF_MIPS_ARCH_6 and Algorithmics has a E_MIPS_ARCH_ALGOR_32, which has this value.

OK, but please show me a document. I only have a vague definition of
values in the 0 - 3 range in the SGI's (n)64 ABI draft. There is no definition provided at the master SysV site (i.e. currently 'http://stage.caldera.com/developer/devspecs/') and the mipsabi.org site no longer exists.

BTW, what are the two last entries meant to define, specifically, how do
they differ from the preceding two?

Quoted 2 lines

If you look at the elf.h file in glibc, the you will see, it has the same values as the kernel.

I've seen it and currently it's broken, since real binaries (as created
by binutils) define the values differently.

Quoted 2 lines

So I would prefer we fix that in binutils, I guess it not a problem as long as you don't have a toolchain that can generate MIPS32 or MIPS64 code.

Then please send a proposal to the binutils list ASAP, as code marked as
MIPS32/64 can be already generated by binutils for quite some time now. I don't care personally, at least not yet, but others may do.

Quoted 7 lines
Quoted 4 lines

Well, -ENOEXEC in not any more useful than SIGILL -- with the latter you
have at least an idea what happened. The ISA check is not implemented for any Linux port, so there no suitable hook in binfmt_*.c files. You might propose an implementation if that's particularly important for you.

I would like a message telling me that I can't run this ISA level on the system.

You need to add an error code to <errno.h>, then, and a suitable error
message to be emitted by perror() and friends. Currently I see none that fits. I'm not sure if the various *nix standards provide any support for such functionality, but it might be worthwhile to add.

Quoted 3 lines

Imagined what would happen, if you execute mips3 code and execute ld/sd instructions on a mips32 kernel (but on a 64-bit processor), the kernel only save half the register and then everything could happen.

The code would be rejected by elf_check_arch() as it would have to be
marked as "n32" or "64" (or "o64", or a kind of EABI, but we don't support these) to make use of 64-bit registers. Gcc and gas won't emit 64-bit operations for any ISA (but they may make use of additional instructions defined by the ISA, as long as they operate on 32-bit data) if the selected ABI doesn't permit them (modulo possible bugs, certainly, as the 64-bit support bits are not tested sufficiently, yet, but that's the intent). If you handcode 64-bit operations in assembly, then you are fully responsible for the results and that won't be reflected in the ELF header anyway, as ".set" directives do not affect it.

Maciej

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
author
#90 lore raw

PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC: elf_check_arch() rework)

In reply to Carsten Langgaard

I'd like to fix binutils ASAP. Here is a patch.

Quoted 72 lines · Carsten Langgaard
Quoted 7 lines · Maciej W. Rozycki
Quoted 3 lines · Carsten Langgaard

We at MIPS are in the process of making an ABI spec for all this, which is the intention that should be used by the tool-vendors. So please don't change the ELF header defines.

It'd be better the spec matched the real world...

Shouldn't it be the other way around, the real world should follow the spec ;-)
The whole ELF header definition is just one big mess, because we are lacking a proper ABI spec.
That's what has motivated us, to begin making this ABI spec.

We have defined the e_flags this way:

/* ELF header e_flags defines. MIPS architecture level. */

#define EF_MIPS_ARCH_1      0x00000000  /* -mips1 code.  */
#define EF_MIPS_ARCH_2      0x10000000  /* -mips2 code.  */
#define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
#define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
#define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */

The missing value 0x50000000, is because IRIX has defined a EF_MIPS_ARCH_6 and Algorithmics has a E_MIPS_ARCH_ALGOR_32, which has this value. If you look at the elf.h file in glibc, the you will see, it has the same values as the kernel.

So I would prefer we fix that in binutils, I guess it not a problem as long as you don't have a toolchain that can generate MIPS32 or MIPS64 code.

Quoted 11 lines
Quoted 5 lines

I don't see that is wrong with checking the ISA level, I rather have an error telling me that I can't execute a certain ISA level than eventually getting a reserved instruction or something worse like something unpredictable.

Well, -ENOEXEC in not any more useful than SIGILL -- with the latter you
have at least an idea what happened. The ISA check is not implemented for any Linux port, so there no suitable hook in binfmt_*.c files. You might propose an implementation if that's particularly important for you.

I would like a message telling me that I can't run this ISA level on the system.
Imagined what would happen, if you execute mips3 code and execute ld/sd instructions on a mips32 kernel (but on a 64-bit processor), the kernel only save half the register and then everything could happen.

Quoted 11 lines
Quoted 4 lines

You are obviously right about the elf_check_arch in the 64-bit part of the kernel is broken. It's probably just be copied from the 32-bit part without changes, like a lot of the code in the 64-bit kernel is.

Possibly, but it still makes me wonder why it wasn't adjusted at the time
binfmt_elf32.c was created...

Maciej

--

Review

Quoted 3 lines
(quoted diff) +3 -0
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
Quoted 9 lines

--

_    _ ____  ___   Carsten Langgaard   Mailto:[email protected]
|\  /|||___)(___   MIPS Denmark        Direct: +45 4486 5527
| \/ |||    ____)  Lautrupvang 4B      Switch: +45 4486 5555
  TECHNOLOGIES     2750 Ballerup       Fax...: +45 4486 5556
                   Denmark             http://www.mips.com

2002-07-25 H.J. Lu <[email protected]>

	* mips.h (E_MIPS_ARCH_6): New.
	(E_MIPS_ARCH_ALGOR_32): New.
	(E_MIPS_ARCH_32): Changed.
	(E_MIPS_ARCH_64): Changed.
	(E_MIPS_ARCH_32R2): New.
	(E_MIPS_ARCH_64R2): New.
elf/mips.h renamed from elf/mips.h.mips +14 -2
@@ -139,11 +139,23 @@ END_RELOC_NUMBERS (R_MIPS_maxext)
 /* -mips5 code.  */
 #define E_MIPS_ARCH_5           0x40000000
 
+/* Used by IRIX.  */
+#define E_MIPS_ARCH_6           0x50000000
+
+/* Used by Algorithmics.  */
+#define E_MIPS_ARCH_ALGOR_32    0x50000000
+
 /* -mips32 code.  */
-#define E_MIPS_ARCH_32          0x50000000
+#define E_MIPS_ARCH_32          0x60000000
 
 /* -mips64 code.  */
-#define E_MIPS_ARCH_64          0x60000000
+#define E_MIPS_ARCH_64          0x70000000
+
+/* MIPS32 code.  */
+#define E_MIPS_ARCH_32R2        0x80000000
+
+/* MIPS64 code.  */
+#define E_MIPS_ARCH_64R2        0x90000000
 
 /* The ABI of the file.  Also see EF_MIPS_ABI2 above. */
 #define EF_MIPS_ABI		0x0000F000
#61 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC: elf_check_arch() rework)

In reply to a message not in the archive

[ Added David Anderson (hopefully still 8-) @ SGI to the CC list, since he's been helpful with sorting out questions like this in the past.

Quoted 1 line · H. J. Lu

I'd like to fix binutils ASAP. Here is a patch.

People using stock binutils have been using the current binutils MIPS arch values for a While now. They were in 2.11.1 and later binutils releases, as well.

Quoted 10 lines
Quoted 10 lines

/* ELF header e_flags defines. MIPS architecture level. */

#define EF_MIPS_ARCH_1      0x00000000  /* -mips1 code.  */
#define EF_MIPS_ARCH_2      0x10000000  /* -mips2 code.  */
#define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
#define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
#define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */

Why are 2 definitions of MIPS32 and MIPS64 needed?

Certainly, if you do need different definitions, they need much better comments than that.

Quoted 2 lines
Quoted 2 lines

The missing value 0x50000000, is because IRIX has defined a EF_MIPS_ARCH_6 and Algorithmics has a E_MIPS_ARCH_ALGOR_32, which has this value.

It's unfortunate that MIPS is only at this late stage getting into the business of defining these fields.

Has IRIX actually _used_ EF_MIPS_ARCH_6 for anything (shipped)? That i'm a bit concerned about, since interoperability with IRIX would be a good thing given that SGI has been setting the only ABI example to follow for MIPS.

Algorithmics may have done something different, but they have also been capable of contributing their binutils-related changes back to the binutils projects, yet they have not. Why muck things up for people who _have_, or who've been using the tools, on their account?

cgd

Signature · 3 lines
Chris Demetriou                                            Broadcom Corporation
Senior Staff Design Engineer                  Broadband Processor Business Unit
  Any opinions expressed in this message are mine, not necessarily Broadcom's.
#92 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC: elf_check_arch() rework)

In reply to a message not in the archive
Quoted 24 lines · Chris Demetriou [email protected] writes:

[ Added David Anderson (hopefully still 8-) @ SGI to the CC list, since he's been helpful with sorting out questions like this in the past.

Quoted 1 line · H. J. Lu

I'd like to fix binutils ASAP. Here is a patch.

People using stock binutils have been using the current binutils MIPS arch values for a While now. They were in 2.11.1 and later binutils releases, as well.

Quoted 10 lines
Quoted 10 lines

/* ELF header e_flags defines. MIPS architecture level. */

#define EF_MIPS_ARCH_1      0x00000000  /* -mips1 code.  */
#define EF_MIPS_ARCH_2      0x10000000  /* -mips2 code.  */
#define EF_MIPS_ARCH_3      0x20000000  /* -mips3 code.  */
#define EF_MIPS_ARCH_4      0x30000000  /* -mips4 code.  */
#define EF_MIPS_ARCH_5      0x40000000  /* -mips5 code.  */
#define EF_MIPS_ARCH_32     0x60000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64     0x70000000  /* MIPS64 code.  */
#define EF_MIPS_ARCH_32R2   0x80000000  /* MIPS32 code.  */
#define EF_MIPS_ARCH_64R2   0x90000000  /* MIPS64 code.  */
Quoted 8 lines

Why are 2 definitions of MIPS32 and MIPS64 needed?

Certainly, if you do need different definitions, they need much better comments than that.

Quoted 2 lines
Quoted 2 lines

The missing value 0x50000000, is because IRIX has defined a EF_MIPS_ARCH_6 and Algorithmics has a E_MIPS_ARCH_ALGOR_32, which has this value.

The latest IRIX def (not yet released header fragment):

elf.h:#define EF_MIPS_ARCH              0xf0000000      /* mask: 4 bit field */
elf.h:#define EF_MIPS_ARCH_1            0x00000000      /* MIPS I ISA */
elf.h:#define EF_MIPS_ARCH_2            0x10000000      /* MIPS II ISA */
elf.h:#define EF_MIPS_ARCH_3            0x20000000      /* MIPS III ISA */
elf.h:#define EF_MIPS_ARCH_4            0x30000000      /* MIPS IV ISA */
elf.h:#define EF_MIPS_ARCH_5            0x40000000      /* MIPS V ISA */
elf.h:#define EF_MIPS_ARCH_6            0x50000000
elf.h:#define EF_MIPS_ARCH_32           0x50000000      /* MIPS32 ISA */
elf.h:#define EF_MIPS_ARCH_64           0x60000000      /* MIPS64 ISA */

EF_MIPS_ARCH_32 duplicates EF_MIPS_ARCH_6 for basically historical reasons I think : we did not want some compile to fail for no good reason if EF_MIPS_ARCH_6 was referred to.
We never have used EF_MIPS_ARCH_6 for anything.

The EF_MIPS_ARCH_64 0x60000000
we got from binutils(!) around Nov 2001, according to my email records. Without much certainty it was the right value.

Quoted 7 lines

It's unfortunate that MIPS is only at this late stage getting into the business of defining these fields.

Has IRIX actually _used_ EF_MIPS_ARCH_6 for anything (shipped)? That i'm a bit concerned about, since interoperability with IRIX would be a good thing given that SGI has been setting the only ABI example to follow for MIPS.

No, IRIX never used EF_MIPS_ARCH_6 0x50000000 for anything
shipped.

Quoted 4 lines

Algorithmics may have done something different, but they have also been capable of contributing their binutils-related changes back to the binutils projects, yet they have not. Why muck things up for people who _have_, or who've been using the tools, on their account?

Nor have we shipped EF_MIPS_ARCH_32 or EF_MIPS_ARCH_64 (from the IRIX set above).

While I cannot speak for everyone here, I'm pretty sure that we can follow what is eventually adopted, since the values in question represent nothing shipped.

We added EF_MIPS_ARCH_5 0x40000000
and EF_MIPS_ARCH_6 0x50000000 to elf.h
long long ago.

I don't *think* we've shipped EF_MIPS_ARCH_5 0x40000000
either, but I could well be wrong on that, I forget what was said/done on EF_MIPS_ARCH_5. If it matters I could check (let me know).

I've added a couple key interested sgi folks (via bcc, I don't know they want email addresses exposed) to this response. So if I've made mistakes above such can get corrrected.

Regards,
David B. Anderson [email protected] http://reality.sgiweb.org/davea

#62 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC: elf_check_arch() rework)

In reply to a message not in the archive
Quoted 1 line · H. J. Lu

I'd like to fix binutils ASAP. Here is a patch.

OK, so, I've seen no response so far that indicates that binutils should actually be changed.

to recap:

* Binutils has deployed these values in several releases now, and I know for a fact that people are using binaries with these values.

* SGI has followed binutils' lead in selecting values.

* Algorithmics did something else, though it's not clear what the difference between "ARCH_ALGOR_32" and "ARCH_32" really is.

It seems obvious that the simplest solution that causes the least pain all around is to go with the numbering binutils currently uses. This will probably cause a little bit of pain for Algorithmics, but, well, they could have sent something to binutils to indicate use of that number, and i'm quite sure that most of the rest of us have had to put temporary backward-compat hacks in our own codebases for incompatible changes made by others in the past. It's not that hard and doesn't cause long-term pain.

I could understand that MIPS or Algorithmics might like that, but I think there're a bunch of morals to this story: if you want to lead on ABI issues, get out in front of them (you can't lead from the back 8-); interact with the tool development and use communities about such issues _before_ solutions are needed and agreed upon in those communities; and, you're hacking open source code like binutils, contribute your changes back as soon as you possibly can.

I'd also like to point out that saying "mips will be defining this ABI, so you should all change your code to work with it" without, AFAIK, even providing a draft of said ABI... is unlikely to produce positive results even _if_ there's no precedent that would go against the requested change. (if somebody has a ptr, i'd be glad to be corrected 8-)

(I wonder what other incompatibilities may exist between this new ABI and the current binutils MIPS ELF headers...)

cgd

Signature · 3 lines
Chris Demetriou                                            Broadcom Corporation
Senior Staff Design Engineer                  Broadband Processor Business Unit
  Any opinions expressed in this message are mine, not necessarily Broadcom's.
#63 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC: elf_check_arch() rework)

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"cgd" == cgd <[email protected]> writes:

Quoted 1 line · H. J. Lu
Quoted 1 line

I'd like to fix binutils ASAP. Here is a patch.

cgd> OK, so, I've seen no response so far that indicates that cgd> binutils should actually be changed.

Agreed, for all the reasons you so clearly stated.

paul

#64 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC:elf_check_arch() rework)

In reply to [email protected]

First of all I will like to thank you for all the replies, we really appreciate it, because it helps us in our process of making a proper ABI spec.
I known it's a relative late state we are trying to provide the community with an ABI document, but I guess it's better to be late than to never show up. I believe we all could benefit from such a document, it's only in a internal draft version right now, but it might be a good idea to send it out to the community for comments, before the finally version.
But we are still in an early state and on the learning curve.

Regarding the ELF header arch values, we did try to select whose in order not to break things for anyone.
I'm also a little surprised to see these value out in binutils, it really surprise me, if we got a linux toolchain that can generate mips32/mips64 code.

MIPS32R2 and MIPS64R2 is a new enhanced version of MIPS32 and MIPS64 architecture, which among other things include some new instructions.

Regarding Algorithmics, I don't know if everybody are aware of it, but we have just acquired Algorithmics.
That among other things, is done in order to play a stronger part in the development of the toolchain. And their work will be pushed back to the community.

Algorithmics have done a MIPS32 compiler for us, which is very close to be released.

I hope this clarify a little bit, what our motivation are. /Carsten

Quoted 40 lines · [email protected] wrote:
Quoted 1 line · H. J. Lu

I'd like to fix binutils ASAP. Here is a patch.

OK, so, I've seen no response so far that indicates that binutils should actually be changed.

to recap:

* Binutils has deployed these values in several releases now, and I know for a fact that people are using binaries with these values.

* SGI has followed binutils' lead in selecting values.

* Algorithmics did something else, though it's not clear what the difference between "ARCH_ALGOR_32" and "ARCH_32" really is.

It seems obvious that the simplest solution that causes the least pain all around is to go with the numbering binutils currently uses. This will probably cause a little bit of pain for Algorithmics, but, well, they could have sent something to binutils to indicate use of that number, and i'm quite sure that most of the rest of us have had to put temporary backward-compat hacks in our own codebases for incompatible changes made by others in the past. It's not that hard and doesn't cause long-term pain.

I could understand that MIPS or Algorithmics might like that, but I think there're a bunch of morals to this story: if you want to lead on ABI issues, get out in front of them (you can't lead from the back 8-); interact with the tool development and use communities about such issues _before_ solutions are needed and agreed upon in those communities; and, you're hacking open source code like binutils, contribute your changes back as soon as you possibly can.

I'd also like to point out that saying "mips will be defining this ABI, so you should all change your code to work with it" without, AFAIK, even providing a draft of said ABI... is unlikely to produce positive results even _if_ there's no precedent that would go against the requested change. (if somebody has a ptr, i'd be glad to be corrected 8-)

Quoted 8 lines

(I wonder what other incompatibilities may exist between this new ABI and the current binutils MIPS ELF headers...)

cgd
--
Chris Demetriou Broadcom Corporation
Senior Staff Design Engineer Broadband Processor Business Unit
Any opinions expressed in this message are mine, not necessarily Broadcom's.

--

_    _ ____  ___   Carsten Langgaard   Mailto:[email protected]
|\  /|||___)(___   MIPS Denmark        Direct: +45 4486 5527
| \/ |||    ____)  Lautrupvang 4B      Switch: +45 4486 5555
  TECHNOLOGIES     2750 Ballerup       Fax...: +45 4486 5556
                   Denmark             http://www.mips.com
#65 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC:elf_check_arch() rework)

I've now heard a bit of the history from Nigel at Algorithmics, and to summarize, they chose the 6 and 7 values for MIPS32/64 after Cygnus, who were also producing a MIPS32/64 toolchain, had chosen these. (Algor had originally used the value of 5 for MIPS32, but had to changed when both SGI (who assigned it to something else) and Cygnus chose otherwise). Hence ARCH_ALGOR_32.

A little research also reveals that the value of 5 for ARCH_32 was first checked into CVS in Dec 2000 by Nick Clifton at Redhat.

Anyway, that's why things are the way they are, but may not help much as to a resolution :(

My personal take on this is that the two main commercial providers (Cygnus and Algor) of MIPS32/64-capable GNU toolchains have been using 6 & 7 for quite a while, SGI "agree", and that this is quite a precedent. Hopefully the ABI process will soon shake any other issues out too.

/Dan

Quoted 90 lines · Carsten Langgaard

First of all I will like to thank you for all the replies, we really appreciate it, because it helps us in our process of making a proper ABI spec.
I known it's a relative late state we are trying to provide the community with an ABI document, but I guess it's better to be late than to never show up. I believe we all could benefit from such a document, it's only in a internal draft version right now, but it might be a good idea to send it out to the community for comments, before the finally version.
But we are still in an early state and on the learning curve.

Regarding the ELF header arch values, we did try to select whose in order not to break things for anyone.
I'm also a little surprised to see these value out in binutils, it really surprise me, if we got a linux toolchain that can generate mips32/mips64 code.

MIPS32R2 and MIPS64R2 is a new enhanced version of MIPS32 and MIPS64 architecture, which among other things include some new instructions.

Regarding Algorithmics, I don't know if everybody are aware of it, but we have just acquired Algorithmics.
That among other things, is done in order to play a stronger part in the development of the toolchain. And their work will be pushed back to the community.

Algorithmics have done a MIPS32 compiler for us, which is very close to be released.

I hope this clarify a little bit, what our motivation are. /Carsten

Quoted 40 lines · [email protected] wrote:
Quoted 1 line · H. J. Lu

I'd like to fix binutils ASAP. Here is a patch.

OK, so, I've seen no response so far that indicates that binutils should actually be changed.

to recap:

* Binutils has deployed these values in several releases now, and I know for a fact that people are using binaries with these values.

* SGI has followed binutils' lead in selecting values.

* Algorithmics did something else, though it's not clear what the difference between "ARCH_ALGOR_32" and "ARCH_32" really is.

It seems obvious that the simplest solution that causes the least pain all around is to go with the numbering binutils currently uses. This will probably cause a little bit of pain for Algorithmics, but, well, they could have sent something to binutils to indicate use of that number, and i'm quite sure that most of the rest of us have had to put temporary backward-compat hacks in our own codebases for incompatible changes made by others in the past. It's not that hard and doesn't cause long-term pain.

I could understand that MIPS or Algorithmics might like that, but I think there're a bunch of morals to this story: if you want to lead on ABI issues, get out in front of them (you can't lead from the back 8-); interact with the tool development and use communities about such issues _before_ solutions are needed and agreed upon in those communities; and, you're hacking open source code like binutils, contribute your changes back as soon as you possibly can.

I'd also like to point out that saying "mips will be defining this ABI, so you should all change your code to work with it" without, AFAIK, even providing a draft of said ABI... is unlikely to produce positive results even _if_ there's no precedent that would go against the requested change. (if somebody has a ptr, i'd be glad to be corrected 8-)

Quoted 8 lines

(I wonder what other incompatibilities may exist between this new ABI and the current binutils MIPS ELF headers...)

cgd
--
Chris Demetriou Broadcom Corporation
Senior Staff Design Engineer Broadband Processor Business Unit
Any opinions expressed in this message are mine, not necessarily Broadcom's.

--

_    _ ____  ___   Carsten Langgaard   Mailto:[email protected]
|\  /|||___)(___   MIPS Denmark        Direct: +45 4486 5527
| \/ |||    ____)  Lautrupvang 4B      Switch: +45 4486 5555
  TECHNOLOGIES     2750 Ballerup       Fax...: +45 4486 5556
                   Denmark             http://www.mips.com
Signature · 5 lines
Dan Temple, Eng. Mgr.     Tel: +45-44865512     
  MIPS Denmark              Fax: +45-44865556     
  Lautrupvang 4B            mailto:[email protected]  
  DK-2750 Ballerup          http://www.mips.com
  Denmark                  
#66 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC:elf_check_arch() rework)

Quoted 9 lines · Dan Temple

I've now heard a bit of the history from Nigel at Algorithmics, and to summarize, they chose the 6 and 7 values for MIPS32/64 after Cygnus, who were also producing a MIPS32/64 toolchain, had chosen these. (Algor had originally used the value of 5 for MIPS32, but had to changed when both SGI (who assigned it to something else) and Cygnus chose otherwise). Hence ARCH_ALGOR_32.

A little research also reveals that the value of 5 for ARCH_32 was first checked into CVS in Dec 2000 by Nick Clifton at Redhat.

Hmm, the relevant ChangeLog entry is:

2000-10-16 Chris Demetriou <[email protected]>

* mips.h (E_MIPS_ARCH_32): New constant. (E_MIPS_MACH_MIPS32, E_MIPS_MACH_MIPS32_4K): Replace the former with the latter.

* mips.h (E_MIPS_ARCH_5, E_MIPS_ARCH_64): New definitions.

* mips.h (E_MIPS_MACH_SB1): New constant.

Patches went in with two commits on Dec 1st and 2nd, 2000:

http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/include/elf/mips.h.diff?r1=1.8&r2=1.9&cvsroot=src

http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/include/elf/mips.h.diff?r1=1.9&r2=1.10&cvsroot=src

Maciej

Signature · 3 lines
+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+--------------------------------------------------------------+
+        e-mail: [email protected], PGP key available        +
#69 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC: elf_check_arch() rework)

In reply to a message not in the archive
Quoted 6 lines · Dan Temple

I've now heard a bit of the history from Nigel at Algorithmics, and to summarize, they chose the 6 and 7 values for MIPS32/64 after Cygnus, who were also producing a MIPS32/64 toolchain, had chosen these. (Algor had originally used the value of 5 for MIPS32, but had to changed when both SGI (who assigned it to something else) and Cygnus chose otherwise). Hence ARCH_ALGOR_32.

Uh, i believe i've seen a snapshot of the Cygnus MIPS32/64 toolchain you describe. It was described to me as an "alpha" snapshot. I have no reason to believe it was ever released as a commercial-quality product.

I've made an inquiry, and my understanding is that Cygnus/RedHat internally use the same values as the public tools (i.e. EF_MIPS_ARCH_MIPS32 == 5, ..._MIPS64 == 6).

Quoted 3 lines

My personal take on this is that the two main commercial providers (Cygnus and Algor) of MIPS32/64-capable GNU toolchains have been using 6 & 7 for quite a while,

As noted above, at least for Cygnus, I believe this assertion is not correct.

Quoted 1 line

SGI "agree",

with the current master binutils sources. 8-)

As noted in the message:

	http://sources.redhat.com/ml/binutils/2002-07/msg00681.html

SGI is using the same values as the master public GNU binutils, i.e. MIPS32 == 5, MIPS64 == 6. It sounds like it wouldn't hurt them much to switch, but following your suggestion _would_ involve them switching.

Quoted 2 lines

and that this is quite a
precedent.

The precedent that I see here is:

* the public master GNU tool sources use EF_MIPS_ARCH_32 == 5, EF_MIPS_ARCH_64 == 6.

* Cygnus/RedHat uses those same values.

* SGI also uses those same values.

* A fair number of other GNU tool or OS support houses have also picked up those values and use them.

* Algorithmics uses different values. The meaning of their 'ALGOR_32' arch value hasn't been mentioned here, I don't know what it is. But, given that everybody else uses the values that the current GNU tools do, well, they should probably provide some backward compatibility for their users if they want it, and switch. (Heck, I don't even know that 'ALGOR_32' should be different than '32' to begin with.)

Quoted 2 lines

Hopefully the ABI process will soon shake any other issues out too.

Indeed. I look forward to seeing a published ABI proposal.

I strongly encourage you to announce the proposal to other groups than just the Linux MIPS folks, by the way. For instance, NetBSD is likely to want to adopt the same ABI for MIPS platforms, and it would be good if they were able to participate in the process as well. I'd recommend contacting <[email protected]> (the NetBSD/mips platform-independent development list), and specifically Simon Burge <[email protected]> who I believe personally has an interest in this stuff.

cgd
--
Chris Demetriou Broadcom Corporation
Senior Staff Design Engineer Broadband Processor Business Unit
Any opinions expressed in this message are mine, not necessarily Broadcom's.

#70 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC: elf_check_arch() rework)

Quoted 7 lines

From: [email protected]
Date: 30 Jul 2002 12:20:57 -0700

Uh, i believe i've seen a snapshot of the Cygnus MIPS32/64 toolchain you describe. It was described to me as an "alpha" snapshot. I have no reason to believe it was ever released as a commercial-quality product.

It was.

Signature · 1 line
- Geoffrey Keating <[email protected]> <[email protected]>
#72 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC: elf_check_arch() rework)

In reply to a message not in the archive
Quoted 3 lines · Chris G. Demetriou

I've made an inquiry, and my understanding is that Cygnus/RedHat internally use the same values as the public tools (i.e. EF_MIPS_ARCH_MIPS32 == 5, ..._MIPS64 == 6).

Of course, i typo'd most of the names of the constants in my msg. I meant E_MIPS_ARCH_32, etc., obviously. 8-)

chris

#73 lore raw

Re: PATCH: Update E_MIP_ARCH_XXX (Re: [patch] linux: RFC: elf_check_arch() rework)

Quoted 7 lines · On Tue, 2002-07-30 at 12:27, [email protected] wrote:
Quoted 3 lines · Chris G. Demetriou

I've made an inquiry, and my understanding is that Cygnus/RedHat internally use the same values as the public tools (i.e. EF_MIPS_ARCH_MIPS32 == 5, ..._MIPS64 == 6).

Of course, i typo'd most of the names of the constants in my msg. I meant E_MIPS_ARCH_32, etc., obviously. 8-)

We have nothing different from what the sources on the net use.

-eric

Signature · 2 lines
I don't want a pony, I want a rocket
powered jetpack!