All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Implement prctl(PR_GET_ENDIAN) for all architectures
@ 2009-10-22 19:35 Helge Deller
  2009-11-09 20:46 ` Andrew Morton
  0 siblings, 1 reply; 5+ messages in thread
From: Helge Deller @ 2009-10-22 19:35 UTC (permalink / raw)
  To: linux-kernel, Anton Blanchard, Paul Mackerras, Andrew Morton

The PR_GET_ENDIAN and PR_SET_ENDIAN prctl() calls have been implemented
to allow to switch processes at runtime from big-endian to little-endian
mode (and vice versa) on PowerPC processors. Since the other architectures
don't support this feature, they currently will just fail and return -EINVAL.

This patch adds just minimal overhead and implements the PR_GET_ENDIAN
call for all other architectures by returning the native endianess of
the architecture. Furthermore, calling prctl(PR_SET_ENDIAN) with the
native endianess of the architecture will succeed, while trying to
set another (not-supported) endianess, will fail.

The patch can be tested with the following program:

#include <stdio.h>
#include <linux/prctl.h>

int main(int argc, char **argv)
{
	int endian, ret;

	ret = prctl(PR_GET_ENDIAN, &endian);
	if (ret)
		perror("prctl(PR_GET_ENDIAN) not implemented");
	printf("current process/machine is running in %s endian mode (%d)\n",
		endian == PR_ENDIAN_LITTLE ? "little":"big", endian);

	/* setting native endianess should succeed */
	ret = prctl(PR_SET_ENDIAN, endian);
	printf("prctl(PR_SET_ENDIAN,%d) should succeed: %s\n",
		endian, ret == 0 ? "OK":"FAIL");

	/* setting foreign endianess should fail */
	endian = (endian == PR_ENDIAN_LITTLE) ?
		PR_ENDIAN_BIG : PR_ENDIAN_LITTLE;
	ret = prctl(PR_SET_ENDIAN, endian);
	printf("prctl(PR_SET_ENDIAN,%d) should fail: %s\n",
		endian, ret == 0 ? "OK":"FAIL");
}

Signed-off-by: Helge Deller <deller@gmx.de>



diff --git a/kernel/sys.c b/kernel/sys.c
index 255475d..bd71dcb 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -46,6 +46,15 @@
 #include <asm/io.h>
 #include <asm/unistd.h>
 
+#if defined(__BIG_ENDIAN)
+# define PRCTL_ENDIAN_DEFAULT	(PR_ENDIAN_BIG)
+#elif defined(__LITTLE_ENDIAN)
+# define PRCTL_ENDIAN_DEFAULT	(PR_ENDIAN_LITTLE)
+#else
+# error "No endianess?"
+#endif
+
+
 #ifndef SET_UNALIGN_CTL
 # define SET_UNALIGN_CTL(a,b)	(-EINVAL)
 #endif
@@ -65,10 +74,12 @@
 # define GET_FPEXC_CTL(a,b)	(-EINVAL)
 #endif
 #ifndef GET_ENDIAN
-# define GET_ENDIAN(a,b)	(-EINVAL)
+# define GET_ENDIAN(task,addr)	\
+	put_user(PRCTL_ENDIAN_DEFAULT, (int __user *) (addr))
 #endif
 #ifndef SET_ENDIAN
-# define SET_ENDIAN(a,b)	(-EINVAL)
+# define SET_ENDIAN(task,value)	\
+	( (value) == PRCTL_ENDIAN_DEFAULT ? 0 : -EINVAL )
 #endif
 #ifndef GET_TSC_CTL
 # define GET_TSC_CTL(a)		(-EINVAL)

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] Implement prctl(PR_GET_ENDIAN) for all architectures
  2009-10-22 19:35 [PATCH] Implement prctl(PR_GET_ENDIAN) for all architectures Helge Deller
@ 2009-11-09 20:46 ` Andrew Morton
  2009-11-09 21:32   ` Mikael Pettersson
  2009-11-10 22:04   ` Helge Deller
  0 siblings, 2 replies; 5+ messages in thread
From: Andrew Morton @ 2009-11-09 20:46 UTC (permalink / raw)
  To: Helge Deller; +Cc: linux-kernel, Anton Blanchard, Paul Mackerras

On Thu, 22 Oct 2009 21:35:33 +0200
Helge Deller <deller@gmx.de> wrote:

> The PR_GET_ENDIAN and PR_SET_ENDIAN prctl() calls have been implemented
> to allow to switch processes at runtime from big-endian to little-endian
> mode (and vice versa) on PowerPC processors. Since the other architectures
> don't support this feature, they currently will just fail and return -EINVAL.
> 
> This patch adds just minimal overhead and implements the PR_GET_ENDIAN
> call for all other architectures by returning the native endianess of
> the architecture. Furthermore, calling prctl(PR_SET_ENDIAN) with the
> native endianess of the architecture will succeed, while trying to
> set another (not-supported) endianess, will fail.
> 
> The patch can be tested with the following program:
> 
> #include <stdio.h>
> #include <linux/prctl.h>
> 
> int main(int argc, char **argv)
> {
> 	int endian, ret;
> 
> 	ret = prctl(PR_GET_ENDIAN, &endian);
> 	if (ret)
> 		perror("prctl(PR_GET_ENDIAN) not implemented");
> 	printf("current process/machine is running in %s endian mode (%d)\n",
> 		endian == PR_ENDIAN_LITTLE ? "little":"big", endian);
> 
> 	/* setting native endianess should succeed */
> 	ret = prctl(PR_SET_ENDIAN, endian);
> 	printf("prctl(PR_SET_ENDIAN,%d) should succeed: %s\n",
> 		endian, ret == 0 ? "OK":"FAIL");
> 
> 	/* setting foreign endianess should fail */
> 	endian = (endian == PR_ENDIAN_LITTLE) ?
> 		PR_ENDIAN_BIG : PR_ENDIAN_LITTLE;
> 	ret = prctl(PR_SET_ENDIAN, endian);
> 	printf("prctl(PR_SET_ENDIAN,%d) should fail: %s\n",
> 		endian, ret == 0 ? "OK":"FAIL");
> }
> 

The changelog forgot to provide any reason for making this change to
the kernel.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Implement prctl(PR_GET_ENDIAN) for all architectures
  2009-11-09 20:46 ` Andrew Morton
@ 2009-11-09 21:32   ` Mikael Pettersson
  2009-11-10 22:20     ` Helge Deller
  2009-11-10 22:04   ` Helge Deller
  1 sibling, 1 reply; 5+ messages in thread
From: Mikael Pettersson @ 2009-11-09 21:32 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Helge Deller, linux-kernel, Anton Blanchard, Paul Mackerras

Andrew Morton writes:
 > On Thu, 22 Oct 2009 21:35:33 +0200
 > Helge Deller <deller@gmx.de> wrote:
 > 
 > > The PR_GET_ENDIAN and PR_SET_ENDIAN prctl() calls have been implemented
 > > to allow to switch processes at runtime from big-endian to little-endian
 > > mode (and vice versa) on PowerPC processors. Since the other architectures
 > > don't support this feature, they currently will just fail and return -EINVAL.
 > > 
 > > This patch adds just minimal overhead and implements the PR_GET_ENDIAN
 > > call for all other architectures by returning the native endianess of
 > > the architecture. Furthermore, calling prctl(PR_SET_ENDIAN) with the
 > > native endianess of the architecture will succeed, while trying to
 > > set another (not-supported) endianess, will fail.
 > > 
 > > The patch can be tested with the following program:
 > > 
 > > #include <stdio.h>
 > > #include <linux/prctl.h>
 > > 
 > > int main(int argc, char **argv)
 > > {
 > > 	int endian, ret;
 > > 
 > > 	ret = prctl(PR_GET_ENDIAN, &endian);
 > > 	if (ret)
 > > 		perror("prctl(PR_GET_ENDIAN) not implemented");
 > > 	printf("current process/machine is running in %s endian mode (%d)\n",
 > > 		endian == PR_ENDIAN_LITTLE ? "little":"big", endian);
 > > 
 > > 	/* setting native endianess should succeed */
 > > 	ret = prctl(PR_SET_ENDIAN, endian);
 > > 	printf("prctl(PR_SET_ENDIAN,%d) should succeed: %s\n",
 > > 		endian, ret == 0 ? "OK":"FAIL");
 > > 
 > > 	/* setting foreign endianess should fail */
 > > 	endian = (endian == PR_ENDIAN_LITTLE) ?
 > > 		PR_ENDIAN_BIG : PR_ENDIAN_LITTLE;
 > > 	ret = prctl(PR_SET_ENDIAN, endian);
 > > 	printf("prctl(PR_SET_ENDIAN,%d) should fail: %s\n",
 > > 		endian, ret == 0 ? "OK":"FAIL");
 > > }
 > > 
 > 
 > The changelog forgot to provide any reason for making this change to
 > the kernel.

Why is PR_GET_ENDIAN needed? Surely user-space can detect the current
endianess for itself?

Also, which endianess does this refer to? ARM has had and continues to
invent bizarre endianess rules, where the endianess of some datum depends
not only on the endianess flag in a core control register but also the
functional unit operating on the data and the ARM ISA version. Some of
these endianess properties are unchangeable, some may be changeable in
specific implementations.

What about archs that always run in one endianess but support other-endian
data accesses via special instruction operands? (I believe SPARC falls into
this category.) PR_SET_ENDIAN on the other-endian value must fail, but what
does that mean to the program? That it can't do other-endian accesses? No.

I think the very concept of flipping a thread-global endianess mode flag
is too architecture specific to be treated as a generic thing.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Implement prctl(PR_GET_ENDIAN) for all architectures
  2009-11-09 20:46 ` Andrew Morton
  2009-11-09 21:32   ` Mikael Pettersson
@ 2009-11-10 22:04   ` Helge Deller
  1 sibling, 0 replies; 5+ messages in thread
From: Helge Deller @ 2009-11-10 22:04 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, Anton Blanchard, Paul Mackerras

On 11/09/2009 09:46 PM, Andrew Morton wrote:
> On Thu, 22 Oct 2009 21:35:33 +0200
> Helge Deller<deller@gmx.de>  wrote:
>
>> The PR_GET_ENDIAN and PR_SET_ENDIAN prctl() calls have been implemented
>> to allow to switch processes at runtime from big-endian to little-endian
>> mode (and vice versa) on PowerPC processors. Since the other architectures
>> don't support this feature, they currently will just fail and return -EINVAL.
>>
>> This patch adds just minimal overhead and implements the PR_GET_ENDIAN
>> call for all other architectures by returning the native endianess of
>> the architecture. Furthermore, calling prctl(PR_SET_ENDIAN) with the
>> native endianess of the architecture will succeed, while trying to
>> set another (not-supported) endianess, will fail.
>>
>> The patch can be tested with the following program:
>>
>> #include<stdio.h>
>> #include<linux/prctl.h>
>>
>> int main(int argc, char **argv)
>> {
>> 	int endian, ret;
>>
>> 	ret = prctl(PR_GET_ENDIAN,&endian);
>> 	if (ret)
>> 		perror("prctl(PR_GET_ENDIAN) not implemented");
>> 	printf("current process/machine is running in %s endian mode (%d)\n",
>> 		endian == PR_ENDIAN_LITTLE ? "little":"big", endian);
>>
>> 	/* setting native endianess should succeed */
>> 	ret = prctl(PR_SET_ENDIAN, endian);
>> 	printf("prctl(PR_SET_ENDIAN,%d) should succeed: %s\n",
>> 		endian, ret == 0 ? "OK":"FAIL");
>>
>> 	/* setting foreign endianess should fail */
>> 	endian = (endian == PR_ENDIAN_LITTLE) ?
>> 		PR_ENDIAN_BIG : PR_ENDIAN_LITTLE;
>> 	ret = prctl(PR_SET_ENDIAN, endian);
>> 	printf("prctl(PR_SET_ENDIAN,%d) should fail: %s\n",
>> 		endian, ret == 0 ? "OK":"FAIL");
>> }
>>
>
> The changelog forgot to provide any reason for making this change to
> the kernel.

The reason - and the patch - is pretty trivial.
The kernel provides an interface (for all architectures) which is  currently
only useable on one single architecture (ppc).
So, either we could just remove the interface alltogether for all architectures
beside ppc, or implement the functionality for all architectures so that they
return at least some kind of useful values back to userspace.
I hope this qualifies for kernel inclusion?

Helge

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Implement prctl(PR_GET_ENDIAN) for all architectures
  2009-11-09 21:32   ` Mikael Pettersson
@ 2009-11-10 22:20     ` Helge Deller
  0 siblings, 0 replies; 5+ messages in thread
From: Helge Deller @ 2009-11-10 22:20 UTC (permalink / raw)
  To: Mikael Pettersson
  Cc: Andrew Morton, linux-kernel, Anton Blanchard, Paul Mackerras

On 11/09/2009 10:32 PM, Mikael Pettersson wrote:
> Andrew Morton writes:
>   >  On Thu, 22 Oct 2009 21:35:33 +0200
>   >  Helge Deller<deller@gmx.de>  wrote:
>   >
>   >  >  The PR_GET_ENDIAN and PR_SET_ENDIAN prctl() calls have been implemented
>   >  >  to allow to switch processes at runtime from big-endian to little-endian
>   >  >  mode (and vice versa) on PowerPC processors. Since the other architectures
>   >  >  don't support this feature, they currently will just fail and return -EINVAL.
>   >  >
>   >  >  This patch adds just minimal overhead and implements the PR_GET_ENDIAN
>   >  >  call for all other architectures by returning the native endianess of
>   >  >  the architecture. Furthermore, calling prctl(PR_SET_ENDIAN) with the
>   >  >  native endianess of the architecture will succeed, while trying to
>   >  >  set another (not-supported) endianess, will fail.
>   >  >
>   >  >  The patch can be tested with the following program:
>   >  >
>   >  >  #include<stdio.h>
>   >  >  #include<linux/prctl.h>
>   >  >
>   >  >  int main(int argc, char **argv)
>   >  >  {
>   >  >  	int endian, ret;
>   >  >
>   >  >  	ret = prctl(PR_GET_ENDIAN,&endian);
>   >  >  	if (ret)
>   >  >  		perror("prctl(PR_GET_ENDIAN) not implemented");
>   >  >  	printf("current process/machine is running in %s endian mode (%d)\n",
>   >  >  		endian == PR_ENDIAN_LITTLE ? "little":"big", endian);
>   >  >
>   >  >  	/* setting native endianess should succeed */
>   >  >  	ret = prctl(PR_SET_ENDIAN, endian);
>   >  >  	printf("prctl(PR_SET_ENDIAN,%d) should succeed: %s\n",
>   >  >  		endian, ret == 0 ? "OK":"FAIL");
>   >  >
>   >  >  	/* setting foreign endianess should fail */
>   >  >  	endian = (endian == PR_ENDIAN_LITTLE) ?
>   >  >  		PR_ENDIAN_BIG : PR_ENDIAN_LITTLE;
>   >  >  	ret = prctl(PR_SET_ENDIAN, endian);
>   >  >  	printf("prctl(PR_SET_ENDIAN,%d) should fail: %s\n",
>   >  >  		endian, ret == 0 ? "OK":"FAIL");
>   >  >  }
>   >  >
>   >
>   >  The changelog forgot to provide any reason for making this change to
>   >  the kernel.
>
> Why is PR_GET_ENDIAN needed? Surely user-space can detect the current
> endianess for itself?

PR_GET_ENDIAN/PR_SET_ENDIAN was introduced with:
commit 651d765d0b2c72d33430487c8b6ef64c60cd2134
Author: Anton Blanchard <anton@samba.org>
Date:   Wed Jun 7 16:10:19 2006 +1000

     [PATCH] Add a prctl to change the endianness of a process.

     This new prctl is intended for changing the execution mode of the
     processor, on processors that support both a little-endian mode and a
     big-endian mode.  It is intended for use by programs such as
     instruction set emulators (for example an x86 emulator on PowerPC),
     which may find it convenient to use the processor in an alternate
     endianness mode when executing translated instructions.

     Note that this does not imply the existence of a fully-fledged ABI for
     both endiannesses, or of compatibility code for converting system
     calls done in the non-native endianness mode.  The program is expected
     to arrange for all of its system call arguments to be presented in the
     native endianness.

     Switching between big and little-endian mode will require some care in
     constructing the instruction sequence for the switch.  Generally the
     instructions up to the instruction that invokes the prctl system call
     will have to be in the old endianness, and subsequent instructions
     will have to be in the new endianness.

     Signed-off-by: Anton Blanchard <anton@samba.org>
     Signed-off-by: Paul Mackerras <paulus@samba.org>

> Also, which endianess does this refer to? ARM has had and continues to
> invent bizarre endianess rules, where the endianess of some datum depends
> not only on the endianess flag in a core control register but also the
> functional unit operating on the data and the ARM ISA version. Some of
> these endianess properties are unchangeable, some may be changeable in
> specific implementations.
>
> What about archs that always run in one endianess but support other-endian
> data accesses via special instruction operands? (I believe SPARC falls into
> this category.) PR_SET_ENDIAN on the other-endian value must fail, but what
> does that mean to the program? That it can't do other-endian accesses? No.

Right. I think no one objects on what you are writing here.
  
> I think the very concept of flipping a thread-global endianess mode flag
> is too architecture specific to be treated as a generic thing.

Please read my patch again.
It's not trying to flip any endianess settings in any generic way at all.
For PR_GET_ENDIAN it just returns the real kernels endianess instead of -EINVAL,
and it does not allow you to change the endianess with PR_SET_ENDIAN.

Helge

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-11-10 22:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-22 19:35 [PATCH] Implement prctl(PR_GET_ENDIAN) for all architectures Helge Deller
2009-11-09 20:46 ` Andrew Morton
2009-11-09 21:32   ` Mikael Pettersson
2009-11-10 22:20     ` Helge Deller
2009-11-10 22:04   ` Helge Deller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.