[PATCH] uml: properly invoke x86_64 system calls

This patch makes stub_segv use the stub_syscall macros. This was needed
anyway, but the bug that prompted this was the discovery that gcc was storing
stuff in RCX, which is trashed across a system call. This is exactly the sort
of problem that the new macros fix.

There is a stub_syscall0 for getpid. stub_segv was changed to be a libc file,
and that caused some include changes.

Signed-off-by: Jeff Dike <jdike@addtoit.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by Jeff Dike and committed by Linus Torvalds 17d46971 e23181de

+39 -17
+9
arch/um/include/sysdep-i386/stub.h
··· 16 16 #define STUB_MMAP_NR __NR_mmap2 17 17 #define MMAP_OFFSET(o) ((o) >> PAGE_SHIFT) 18 18 19 + static inline long stub_syscall0(long syscall) 20 + { 21 + long ret; 22 + 23 + __asm__ volatile ("int $0x80" : "=a" (ret) : "0" (syscall)); 24 + 25 + return ret; 26 + } 27 + 19 28 static inline long stub_syscall1(long syscall, long arg1) 20 29 { 21 30 long ret;
+11 -1
arch/um/include/sysdep-x86_64/stub.h
··· 6 6 #ifndef __SYSDEP_STUB_H 7 7 #define __SYSDEP_STUB_H 8 8 9 - #include <asm/ptrace.h> 10 9 #include <asm/unistd.h> 11 10 #include <sysdep/ptrace_user.h> 12 11 ··· 18 19 19 20 #define __syscall_clobber "r11","rcx","memory" 20 21 #define __syscall "syscall" 22 + 23 + static inline long stub_syscall0(long syscall) 24 + { 25 + long ret; 26 + 27 + __asm__ volatile (__syscall 28 + : "=a" (ret) 29 + : "0" (syscall) : __syscall_clobber ); 30 + 31 + return ret; 32 + } 21 33 22 34 static inline long stub_syscall2(long syscall, long arg1, long arg2) 23 35 {
+1 -1
arch/um/sys-i386/Makefile
··· 5 5 obj-$(CONFIG_HIGHMEM) += highmem.o 6 6 obj-$(CONFIG_MODULES) += module.o 7 7 8 - USER_OBJS := bugs.o ptrace_user.o sigcontext.o fault.o 8 + USER_OBJS := bugs.o ptrace_user.o sigcontext.o fault.o stub_segv.o 9 9 10 10 SYMLINKS = bitops.c semaphore.c highmem.c module.c 11 11
+7 -4
arch/um/sys-i386/stub_segv.c
··· 3 3 * Licensed under the GPL 4 4 */ 5 5 6 - #include <asm/signal.h> 6 + #include <signal.h> 7 + #include <sys/select.h> /* The only way I can see to get sigset_t */ 7 8 #include <asm/unistd.h> 8 9 #include "uml-config.h" 10 + #include "sysdep/stub.h" 9 11 #include "sysdep/sigcontext.h" 10 12 #include "sysdep/faultinfo.h" 11 13 ··· 15 13 stub_segv_handler(int sig) 16 14 { 17 15 struct sigcontext *sc = (struct sigcontext *) (&sig + 1); 16 + int pid; 18 17 19 18 GET_FAULTINFO_FROM_SC(*((struct faultinfo *) UML_CONFIG_STUB_DATA), 20 19 sc); 21 20 22 - __asm__("movl %0, %%eax ; int $0x80": : "g" (__NR_getpid)); 23 - __asm__("movl %%eax, %%ebx ; movl %0, %%eax ; movl %1, %%ecx ;" 24 - "int $0x80": : "g" (__NR_kill), "g" (SIGUSR1)); 21 + pid = stub_syscall0(__NR_getpid); 22 + stub_syscall2(__NR_kill, pid, SIGUSR1); 23 + 25 24 /* Load pointer to sigcontext into esp, since we need to leave 26 25 * the stack in its original form when we do the sigreturn here, by 27 26 * hand.
+1 -1
arch/um/sys-x86_64/Makefile
··· 12 12 obj-y := ksyms.o 13 13 obj-$(CONFIG_MODULES) += module.o um_module.o 14 14 15 - USER_OBJS := ptrace_user.o sigcontext.o 15 + USER_OBJS := ptrace_user.o sigcontext.o stub_segv.o 16 16 17 17 SYMLINKS = bitops.c csum-copy.S csum-partial.c csum-wrappers.c ldt.c memcpy.S \ 18 18 thunk.S module.c
+10 -10
arch/um/sys-x86_64/stub_segv.c
··· 3 3 * Licensed under the GPL 4 4 */ 5 5 6 - #include <asm/signal.h> 6 + #include <stddef.h> 7 + #include <signal.h> 7 8 #include <linux/compiler.h> 8 9 #include <asm/unistd.h> 9 - #include <asm/ucontext.h> 10 10 #include "uml-config.h" 11 11 #include "sysdep/sigcontext.h" 12 12 #include "sysdep/faultinfo.h" 13 - #include <stddef.h> 13 + #include "sysdep/stub.h" 14 14 15 15 /* Copied from sys-x86_64/signal.c - Can't find an equivalent definition 16 16 * in the libc headers anywhere. ··· 31 31 stub_segv_handler(int sig) 32 32 { 33 33 struct ucontext *uc; 34 + int pid; 34 35 35 36 __asm__("movq %%rdx, %0" : "=g" (uc) :); 36 37 GET_FAULTINFO_FROM_SC(*((struct faultinfo *) UML_CONFIG_STUB_DATA), 37 38 &uc->uc_mcontext); 38 39 39 - __asm__("movq %0, %%rax ; syscall": : "g" (__NR_getpid)); 40 - __asm__("movq %%rax, %%rdi ; movq %0, %%rax ; movq %1, %%rsi ;" 41 - "syscall": : "g" (__NR_kill), "g" (SIGUSR1) : 42 - "%rdi", "%rax", "%rsi"); 40 + pid = stub_syscall0(__NR_getpid); 41 + stub_syscall2(__NR_kill, pid, SIGUSR1); 42 + 43 43 /* sys_sigreturn expects that the stack pointer will be 8 bytes into 44 44 * the signal frame. So, we use the ucontext pointer, which we know 45 45 * already, to get the signal frame pointer, and add 8 to that. 46 46 */ 47 - __asm__("movq %0, %%rsp": : 47 + __asm__("movq %0, %%rsp; movq %1, %%rax ; syscall": : 48 48 "g" ((unsigned long) container_of(uc, struct rt_sigframe, 49 - uc) + 8)); 50 - __asm__("movq %0, %%rax ; syscall" : : "g" (__NR_rt_sigreturn)); 49 + uc) + 8), 50 + "g" (__NR_rt_sigreturn)); 51 51 }