Commit 5b7b15af authored by Jeff Dike's avatar Jeff Dike Committed by Linus Torvalds

[PATCH] uml skas0: stop gcc's insanity

With Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>

UML skas0 stub has been miscompiling for many people (incidentally not
the authors), depending on the used GCC versions.

I think (and testing on some GCC versions shows) this patch avoids the
fundamental issue which is behind this, namely gcc using the stack when
we have just replaced it, behind gcc's back.  The remapping and storage
of the return value is hidden in a blob of asm, hopefully giving gcc no
room for creativity.
Signed-off-by: default avatarPaolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 76c842d8
...@@ -6,8 +6,12 @@ ...@@ -6,8 +6,12 @@
#ifndef __SYSDEP_STUB_H #ifndef __SYSDEP_STUB_H
#define __SYSDEP_STUB_H #define __SYSDEP_STUB_H
#include <sys/mman.h>
#include <asm/ptrace.h> #include <asm/ptrace.h>
#include <asm/unistd.h> #include <asm/unistd.h>
#include "stub-data.h"
#include "kern_constants.h"
#include "uml-config.h"
extern void stub_segv_handler(int sig); extern void stub_segv_handler(int sig);
extern void stub_clone_handler(void); extern void stub_clone_handler(void);
...@@ -76,23 +80,22 @@ static inline long stub_syscall5(long syscall, long arg1, long arg2, long arg3, ...@@ -76,23 +80,22 @@ static inline long stub_syscall5(long syscall, long arg1, long arg2, long arg3,
return ret; return ret;
} }
static inline long stub_syscall6(long syscall, long arg1, long arg2, long arg3, static inline void trap_myself(void)
long arg4, long arg5, long arg6)
{ {
long ret; __asm("int3");
__asm__ volatile ("push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; "
"int $0x80 ; pop %%ebp"
: "=a" (ret)
: "g" (syscall), "b" (arg1), "c" (arg2), "d" (arg3),
"S" (arg4), "D" (arg5), "0" (arg6));
return ret;
} }
static inline void trap_myself(void) static inline void remap_stack(int fd, unsigned long offset)
{ {
__asm("int3"); __asm__ volatile ("movl %%eax,%%ebp ; movl %0,%%eax ; int $0x80 ;"
"movl %7, %%ebx ; movl %%eax, (%%ebx)"
: : "g" (STUB_MMAP_NR), "b" (UML_CONFIG_STUB_DATA),
"c" (UM_KERN_PAGE_SIZE),
"d" (PROT_READ | PROT_WRITE),
"S" (MAP_FIXED | MAP_SHARED), "D" (fd),
"a" (offset),
"i" (&((struct stub_data *) UML_CONFIG_STUB_DATA)->err)
: "memory");
} }
#endif #endif
...@@ -6,8 +6,12 @@ ...@@ -6,8 +6,12 @@
#ifndef __SYSDEP_STUB_H #ifndef __SYSDEP_STUB_H
#define __SYSDEP_STUB_H #define __SYSDEP_STUB_H
#include <sys/mman.h>
#include <asm/unistd.h> #include <asm/unistd.h>
#include <sysdep/ptrace_user.h> #include <sysdep/ptrace_user.h>
#include "stub-data.h"
#include "kern_constants.h"
#include "uml-config.h"
extern void stub_segv_handler(int sig); extern void stub_segv_handler(int sig);
extern void stub_clone_handler(void); extern void stub_clone_handler(void);
...@@ -81,23 +85,23 @@ static inline long stub_syscall5(long syscall, long arg1, long arg2, long arg3, ...@@ -81,23 +85,23 @@ static inline long stub_syscall5(long syscall, long arg1, long arg2, long arg3,
return ret; return ret;
} }
static inline long stub_syscall6(long syscall, long arg1, long arg2, long arg3, static inline void trap_myself(void)
long arg4, long arg5, long arg6)
{ {
long ret; __asm("int3");
__asm__ volatile ("movq %5,%%r10 ; movq %6,%%r8 ; "
"movq %7, %%r9; " __syscall : "=a" (ret)
: "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3),
"g" (arg4), "g" (arg5), "g" (arg6)
: __syscall_clobber, "r10", "r8", "r9" );
return ret;
} }
static inline void trap_myself(void) static inline void remap_stack(long fd, unsigned long offset)
{ {
__asm("int3"); __asm__ volatile ("movq %4,%%r10 ; movq %5,%%r8 ; "
"movq %6, %%r9; " __syscall "; movq %7, %%rbx ; "
"movq %%rax, (%%rbx)":
: "a" (STUB_MMAP_NR), "D" (UML_CONFIG_STUB_DATA),
"S" (UM_KERN_PAGE_SIZE),
"d" (PROT_READ | PROT_WRITE),
"g" (MAP_FIXED | MAP_SHARED), "g" (fd),
"g" (offset),
"i" (&((struct stub_data *) UML_CONFIG_STUB_DATA)->err)
: __syscall_clobber, "r10", "r8", "r9" );
} }
#endif #endif
...@@ -18,11 +18,10 @@ ...@@ -18,11 +18,10 @@
* on some systems. * on some systems.
*/ */
#define STUB_DATA(field) (((struct stub_data *) UML_CONFIG_STUB_DATA)->field)
void __attribute__ ((__section__ (".__syscall_stub"))) void __attribute__ ((__section__ (".__syscall_stub")))
stub_clone_handler(void) stub_clone_handler(void)
{ {
struct stub_data *data = (struct stub_data *) UML_CONFIG_STUB_DATA;
long err; long err;
err = stub_syscall2(__NR_clone, CLONE_PARENT | CLONE_FILES | SIGCHLD, err = stub_syscall2(__NR_clone, CLONE_PARENT | CLONE_FILES | SIGCHLD,
...@@ -36,16 +35,20 @@ stub_clone_handler(void) ...@@ -36,16 +35,20 @@ stub_clone_handler(void)
goto out; goto out;
err = stub_syscall3(__NR_setitimer, ITIMER_VIRTUAL, err = stub_syscall3(__NR_setitimer, ITIMER_VIRTUAL,
(long) &STUB_DATA(timer), 0); (long) &data->timer, 0);
if(err) if(err)
goto out; goto out;
err = stub_syscall6(STUB_MMAP_NR, UML_CONFIG_STUB_DATA, remap_stack(data->fd, data->offset);
UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE, goto done;
MAP_FIXED | MAP_SHARED, STUB_DATA(fd),
STUB_DATA(offset));
out: out:
/* save current result. Parent: pid; child: retcode of mmap */ /* save current result.
STUB_DATA(err) = err; * Parent: pid;
* child: retcode of mmap already saved and it jumps around this
* assignment
*/
data->err = err;
done:
trap_myself(); trap_myself();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment