Skip to contentSkip to conversation

x86: Make _syscallX() macros compile in PIC mode on i386

PATCH In review Markus Gutschke 6 participants 11 messages Mar 9, 2006 – Mar 11, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: Make _syscallX() macros compile in PIC mode on i386

From: Markus Gutschke <[email protected]>

Gcc reserves %ebx when compiling position-independent-code on i386. This means, the _syscallX() macros in include/asm-i386/unistd.h will not compile. This patch is against 2.6.15.6 and adds a new set of macros which will be used in PIC mode. These macros take special care to preserve %ebx.

The bug can be tracked at http://bugzilla.kernel.org/show_bug.cgi?id=6204

---

include/asm-i386/unistd.h renamed from include/asm-i386/unistd.h.orig +77 -0
@@ -326,6 +326,7 @@
 __syscall_return(type,__res); \
 }
 
+#ifndef __PIC__
 #define _syscall1(type,name,type1,arg1) \
 type name(type1 arg1) \
 { \
@@ -393,6 +394,82 @@
 __syscall_return(type,__res); \
 }
 
+#else /* __PIC__ */
+
+#define _syscall1(type,name,type1,arg1) \
+type name(type1 arg1) \
+{ \
+long __res; \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
+	: "=a" (__res) \
+	: "0" (__NR_##name),"ri" ((long)(arg1)) : "memory"); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall2(type,name,type1,arg1,type2,arg2) \
+type name(type1 arg1,type2 arg2) \
+{ \
+long __res; \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
+	: "=a" (__res) \
+	: "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)) \
+	: "memory"); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
+type name(type1 arg1,type2 arg2,type3 arg3) \
+{ \
+long __res; \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
+	: "=a" (__res) \
+	: "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
+		  "d" ((long)(arg3)) : "memory"); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
+type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
+{ \
+long __res; \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
+	: "=a" (__res) \
+	: "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
+	  "d" ((long)(arg3)),"S" ((long)(arg4)) : "memory"); \
+__syscall_return(type,__res); \
+} 
+
+#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
+	  type5,arg5) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
+{ \
+long __res; \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; movl %1,%%eax ; " \
+                  "int $0x80 ; pop %%ebx" \
+	: "=a" (__res) \
+	: "i" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
+	  "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
+	: "memory"); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
+	  type5,arg5,type6,arg6) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
+{ \
+long __res; \
+  struct { long __a1; long __a6; } __s = { (long)arg1, (long)arg6 }; \
+__asm__ volatile ("push %%ebp ; push %%ebx ; movl 4(%2),%%ebp ; " \
+                  "movl 0(%2),%%ebx ; movl %1,%%eax ; int $0x80 ; " \
+                  "pop %%ebx ;  pop %%ebp" \
+	: "=a" (__res) \
+	: "i" (__NR_##name),"0" ((long)(&__s)),"c" ((long)(arg2)), \
+	  "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
+	: "memory"); \
+__syscall_return(type,__res); \
+}
+#endif /* __PIC__ */
+
 #ifdef __KERNEL__
 #define __ARCH_WANT_IPC_PARSE_VERSION
 #define __ARCH_WANT_OLD_READDIR
#1 lore raw
Quoted 6 lines · Markus Gutschke

Gcc reserves %ebx when compiling position-independent-code on i386. This
means, the _syscallX() macros in include/asm-i386/unistd.h will not compile. This patch is against 2.6.15.6 and adds a new set of macros which will be used in PIC mode. These macros take special care to preserve %ebx.

But we don't compile the kernel with -fpic... We might want to, for kdump convenience at some stage, perhaps.

If we do, it'd be better to simply replace those _syscallX functions with versions which work in either mode, rather than having two versions.

The syscallX() macros are almost obsolete - it's preferred that code simply include syscalls.h and call sys_foo() directly. But there are a few hard-to-convert places, iirc.

#2 lore raw
Quoted 2 lines · Andrew Morton

But we don't compile the kernel with -fpic... We might want to, for kdump convenience at some stage, perhaps.

Unless I am really confused, there should be no place in the kernel that uses any of the _syscallX() macros. These macros are for the benefit of userspace, and they get picked up by and distributed with glibc. They just happen to be shipped and maintained with the kernel.

Quoted 2 lines

If we do, it'd be better to simply replace those _syscallX functions with versions which work in either mode, rather than having two versions.

That is certainly possible. The new macros work in both modes, but they are slightly less efficient than the old macros, if you have access to %ebx (i.e. in non-PIC code). If you prefer, we could just remove the old macros and unconditionally replace them with the new ones.

Quoted 3 lines

The syscallX() macros are almost obsolete - it's preferred that code simply include syscalls.h and call sys_foo() directly. But there are a few hard-to-convert places, iirc.

Are you thinking of the code that jumps through the vdso entry point? That is not always an easy option for user-space applications which need to remain backwards compatible to older versions of the kernel and of libc.

Markus

#3 lore raw
Quoted 9 lines · Markus Gutschke
Quoted 2 lines · Andrew Morton

But we don't compile the kernel with -fpic... We might want to, for kdump convenience at some stage, perhaps.

Unless I am really confused, there should be no place in the kernel that uses any of the _syscallX() macros. These macros are for the benefit of userspace, and they get picked up by and distributed with glibc. They just happen to be shipped and maintained with the kernel.

Nope, there's an int-80-based execve() implemented in include/asm-i386/unistd.h. It's called from init/do_mounts_initrd.c and kernel/kmod.c (at least).

Quoted 7 lines
Quoted 2 lines

If we do, it'd be better to simply replace those _syscallX functions with versions which work in either mode, rather than having two versions.

That is certainly possible. The new macros work in both modes, but they are slightly less efficient than the old macros, if you have access to %ebx (i.e. in non-PIC code). If you prefer, we could just remove the old macros and unconditionally replace them with the new ones.

I'd be OK with that - the kernel doesn't (shouldn't) care about the performance of __KERNEL_SYSCALLS__ stuff. I doubt if glibc is borrowing the kernel's macros.

Quoted 8 lines
Quoted 3 lines

The syscallX() macros are almost obsolete - it's preferred that code simply include syscalls.h and call sys_foo() directly. But there are a few hard-to-convert places, iirc.

Are you thinking of the code that jumps through the vdso entry point? That is not always an easy option for user-space applications which need to remain backwards compatible to older versions of the kernel and of libc.

afaik, execve() is the only reason for retaining __KERNEL_SYSCALLS__ support on x86.

#4 lore raw
Quoted 1 line · Andrew Morton

I doubt if glibc is borrowing the kernel's macros.

I think it is, though.

When I build gcc/glibc toolchains, I have to use kernel headers. I used to directly use the ones in the kernel.org tree, but those aren't quite intended for use in userspace; fortunately, Mariusz Mazur's sanitized kernel headers work great.

I'd like to see these patches go in to the sanitized kernel headers and/or the kernel.org tree. I imagine that putting them in the kernel.org tree is right, and they'd naturally percolate from there to the various sanitized headers projects.

See also http://lkml.org/lkml/2006/1/7/51
- Dan

#10 lore raw
In reply to Andrew Morton
Quoted 9 lines
Quoted 5 lines

Gcc reserves %ebx when compiling position-independent-code on i386. This
means, the _syscallX() macros in include/asm-i386/unistd.h will not compile. This patch is against 2.6.15.6 and adds a new set of macros which will be used in PIC mode. These macros take special care to preserve %ebx.

But we don't compile the kernel with -fpic... We might want to, for kdump convenience at some stage, perhaps.

UML. Maybe it does not build with -fpic/-fPIC either, but it's one case where it's more likely than with a "true" kernel.

Jan Engelhardt

#7 lore raw

Re: [PATCH 1/1] x86: Make _syscallX() macros compile in PIC mode on i386 (updated patch)

In reply to Andrew Morton

From: Markus Gutschke <[email protected]>

Gcc reserves %ebx when compiling position-independent-code on i386. This means, the _syscallX() macros in include/asm-i386/unistd.h will not compile. This patch is against 2.6.15.6 and changes the existing macros to take special care to preserve %ebx.

The bug can be tracked at http://bugzilla.kernel.org/show_bug.cgi?id=6204

---

Quoted 6 lines · Andrew Morton
Quoted 1 line · Markus Gutschke

That is certainly possible. The new macros work in both modes, but they are slightly less efficient than the old macros, if you have access to %ebx (i.e. in non-PIC code). If you prefer, we could just remove the old macros and unconditionally replace them with the new ones.

I'd be OK with that - the kernel doesn't (shouldn't) care about the performance of __KERNEL_SYSCALLS__ stuff. I doubt if glibc is borrowing the kernel's macros.

Would you like this patch better? It now unconditionally replaces the old macros with a fixed version. This will entail a minor performance penalty in non-PIC mode. But for the vast majority of applications the difference should be entire negligible.

Once this change has made it into the kernel, I will try to get it propagated into libc.

Markus

include/asm-i386/unistd.h renamed from include/asm-i386/unistd.h.orig +22 -16
@@ -330,9 +330,9 @@
 type name(type1 arg1) \
 { \
 long __res; \
-__asm__ volatile ("int $0x80" \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
 	: "=a" (__res) \
-	: "0" (__NR_##name),"b" ((long)(arg1)) : "memory"); \
+	: "0" (__NR_##name),"ri" ((long)(arg1)) : "memory"); \
 __syscall_return(type,__res); \
 }
 
@@ -340,9 +340,10 @@
 type name(type1 arg1,type2 arg2) \
 { \
 long __res; \
-__asm__ volatile ("int $0x80" \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
 	: "=a" (__res) \
-	: "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)) : "memory"); \
+	: "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)) \
+	: "memory"); \
 __syscall_return(type,__res); \
 }
 
@@ -350,9 +351,9 @@
 type name(type1 arg1,type2 arg2,type3 arg3) \
 { \
 long __res; \
-__asm__ volatile ("int $0x80" \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
 	: "=a" (__res) \
-	: "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
+	: "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
 		  "d" ((long)(arg3)) : "memory"); \
 __syscall_return(type,__res); \
 }
@@ -361,9 +362,9 @@
 type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
 { \
 long __res; \
-__asm__ volatile ("int $0x80" \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
 	: "=a" (__res) \
-	: "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
+	: "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
 	  "d" ((long)(arg3)),"S" ((long)(arg4)) : "memory"); \
 __syscall_return(type,__res); \
 } 
@@ -373,10 +374,12 @@
 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
 { \
 long __res; \
-__asm__ volatile ("int $0x80" \
+__asm__ volatile ("push %%ebx ; movl %2,%%ebx ; movl %1,%%eax ; " \
+                  "int $0x80 ; pop %%ebx" \
 	: "=a" (__res) \
-	: "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
-	  "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) : "memory"); \
+	: "i" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
+	  "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
+	: "memory"); \
 __syscall_return(type,__res); \
 }
 
@@ -385,11 +388,14 @@
 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
 { \
 long __res; \
-__asm__ volatile ("push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; int $0x80 ; pop %%ebp" \
-	: "=a" (__res) \
-	: "i" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
-	  "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)), \
-	  "0" ((long)(arg6)) : "memory"); \
+  struct { long __a1; long __a6; } __s = { (long)arg1, (long)arg6 }; \
+__asm__ volatile ("push %%ebp ; push %%ebx ; movl 4(%2),%%ebp ; " \
+                  "movl 0(%2),%%ebx ; movl %1,%%eax ; int $0x80 ; " \
+                  "pop %%ebx ;  pop %%ebp" \
+	: "=a" (__res) \
+	: "i" (__NR_##name),"0" ((long)(&__s)),"c" ((long)(arg2)), \
+	  "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
+	: "memory"); \
 __syscall_return(type,__res); \
 }
 
#8 lore raw

Re: [PATCH 1/1] x86: Make _syscallX() macros compile in PIC mode on i386 (updated patch)

Quoted 2 lines · Markus Gutschke

Once this change has made it into the kernel, I will try to get it propagated into libc.

Cool...

I could be wrong, but I think "propagating into libc" here means "propagating into
the sanitized kernel headers, e.g. the set at
http://ep09.pld-linux.org/~mmazur/linux-libc-headers/

#5 lore raw
In reply to dkegel
Quoted 10 lines · dkegel
Quoted 1 line · Andrew Morton

I doubt if glibc is borrowing the kernel's macros.

I think it is, though.

When I build gcc/glibc toolchains, I have to use kernel headers. I used to directly use the ones in the kernel.org tree, but those aren't quite intended for use in userspace; fortunately, Mariusz Mazur's sanitized kernel headers work great.

Quoted 4 lines

I'd like to see these patches go in to the sanitized kernel headers and/or the kernel.org tree. I imagine that putting them in the kernel.org tree is right, and they'd naturally percolate from there to the various sanitized headers projects.

It uses the headers for many things. It does not use the _syscallX macros.

Signature · 2 lines
Daniel Jacobowitz
CodeSourcery
#6 lore raw
Quoted 7 lines · Daniel Jacobowitz
Quoted 4 lines

I'd like to see these patches go in to the sanitized kernel headers and/or the kernel.org tree. I imagine that putting them in the kernel.org tree is right, and they'd naturally percolate from there to the various sanitized headers projects.

It uses the headers for many things. It does not use the _syscallX macros.

Sorry, I misspoke.
The glibc build process makes the _syscallX macros available to userspace via the <asm/syscalls.h> include.
So even if glibc doesn't use those macros directly, it does seem to pass them along. Which is how Markus got on to this whole topic in the first place.

#9 lore raw
In reply to Andrew Morton

Am Friday 10 March 2006 04:22 schrieb Andrew Morton:

Quoted 2 lines

afaik, execve() is the only reason for retaining __KERNEL_SYSCALLS__ support on x86.

Yes. Actually sh64 seems to be the only architecture left where __KERNEL_SYSCALLS__ is used for something besides execve.

	Arnd <><