Skip to contentSkip to conversation

Various patches from Scratchbox

PATCH New Timo Savola 1 participant 1 message Jun 16, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Various patches from Scratchbox

Hello. Here's a bunch of patches that have been developed for Scratchbox's [1] version of qemu. They make it possible to run the ARM version of the maemo [2] platform.

[1] http://scratchbox.org/
[2] http://maemo.org/

The patches have been made against qemu 0.7.0, and most of them are specific to user-mode emulation. Some of them are quite evil, but I've included them for educational reasons...

The following files are attached:

* evil-dlclose.patch
Description: This patch fixes dlopen/dlclose issues with qemu. It
flags every close call and flushes the code gen buffer thus causing some performance hit.
Author: Valtteri Rahkonen

* ipc.patch
Description: Implements SysV IPC message and semaphore IPC calls.
Author: Valtteri Rahkonen

* proc-hack.patch
Description: Implements a /proc/self/exe readlink hack; it returns
the emulated binary instead of qemu.
Author: Valtteri Rahkonen

* sigaction.patch
Description: This patch fixes emulated sigaction behaviour with
invalid signal parameters SIGKILL and SIGSTOP. EINVAL is returned for these
signals.
Author: Valtteri Rahkonen

* sigsilent.patch
Description: Modifies signal handling so that uncaught signal
message is not printed to stderr. (This would screw up some applications that wants to kill their siblings...)
Author: Valtteri Rahkonen

* socketcall.patch
Description: Fixes AF_UNIX sockets' address structure length
handling so that UNIX sockets can be used.
Author: Timo Savola

* socketopt.patch
Description: Implements SO_LINGER, SO_RCVTIMEO, SO_SNDTIMEO,
SO_PEERNAME and SO_PEERCRED getsockoptions.
Author: Valtteri Rahkonen

* solinger-setsockopt.patch
Description: Implements SO_LINGER setsockoption which is needed by
some network applications.
Author: Valtteri Rahkonen

* sysctl.patch
Description: Implements kernel version query using the sysctl
syscall.
Author: Timo Savola

* tcpip-sockopt.patch
Description: Implements the SOL_IP/IP_TOS option for setsockopt and
SOL_TCP/TCP_NODELAY option for getsockopt.
Author: Timo Savola

* truncate64.patch
Description: Implements truncate64 syscall.
Author: Valtteri Rahkonen

We also use the following patches by Ulrich Hecht (posted previously to qemu-devel):

* qemu-arm-sigfpe.patch
* qemu-jobsignals.patch
* qemu-sigaltstackhack.patch
* qemu-syscalls.patch

timo

main.c +11 -0
@@ -25,4 +25,7 @@
 #include <unistd.h>
 
 #include "qemu.h"
+#include "exec-all.h"
+
+int flush_close;
 
@@ -467,6 +470,12 @@
             abort();
         }
         process_pending_signals(env);
+
+	if (flush_close)
+	{
+	    tb_flush(env);
+	    flush_close = 0;
+	}
     }
 }
 
@@ -1150,6 +1159,8 @@
     if (argc <= 1)
         usage();
 
+    flush_close = 0;
+    
     /* init debug */
     cpu_set_log_filename(DEBUG_LOGFILE);
 
qemu.h +2 -0
@@ -15,4 +15,6 @@
 
+extern int flush_close;
+
 /* This struct is used to hold certain information about the image.
  * Basically, it replicates in user space what would be certain
  * task_struct fields in the kernel
syscall.c +17 -0
@@ -1721,7 +1721,24 @@
                              arg3));
         break;
     case TARGET_NR_close:
+    {
+        struct stat buf;
+
+        ret = fstat(arg1, &buf);
+
+        if ((ret == 0) && (S_ISREG(buf.st_mode)) && (buf.st_mode & S_IRUSR))
+        {
+            char str[EI_PAD];
+
+            if (lseek(arg1, 0, SEEK_SET) != (off_t)-1)
+                if (read(arg1, str, EI_PAD) > 0)
+                    if (str[EI_MAG0] == ELFMAG0 && str[EI_MAG1] == ELFMAG1 && 
+                        str[EI_MAG1] == ELFMAG1 && str[EI_MAG3] == ELFMAG3)
+                        flush_close = 1;
+        }
+	
         ret = get_errno(close(arg1));
+    }
         break;
     case TARGET_NR_brk:
         ret = do_brk((char *)arg1);
syscall.c +63 -3

110 lines not on this page · raw message

main.c +3 -0
@@ -25,6 +25,8 @@
 #include <unistd.h>
 
 #include "qemu.h"
+
+char *exec_path;
 
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
@@ -1139,6 +1167,7 @@
     if (optind >= argc)
         usage();
     filename = argv[optind];
+    exec_path = argv[optind];
 
     modify_env();
 
qemu.h +2 -0
@@ -9,7 +9,9 @@
 
 #include "cpu.h"
 #include "syscall.h"
 #include "gdbstub.h"
+
+extern char *exec_path;
 
 /* This struct is used to hold certain information about the image.
  * Basically, it replicates in user space what would be certain
syscall.c +5 -2
@@ -2282,6 +2279,9 @@
         goto unimplemented;
 #endif
     case TARGET_NR_readlink:
-        ret = get_errno(readlink(path((const char *)arg1), (char *)arg2, arg3));
-        break;
+        if (strncmp((const char *)arg1, "/proc/self/exe", 14) == 0)
+	    ret = get_errno(snprintf((char *)arg2, arg3, "%s", exec_path));
+        else
+            ret = get_errno(readlink(path((const char *)arg1), (char *)arg2, arg3));
+	break;
     case TARGET_NR_uselib:
signal.c +5 -0
@@ -417,6 +417,11 @@
 
     if (sig < 1 || sig > TARGET_NSIG)
         return -EINVAL;
+
+    /* no point doing the stuff as those are not allowed for sigaction */
+    if ((sig == TARGET_SIGKILL) || (sig == TARGET_SIGSTOP))
+        return -EINVAL;
+
     k = &sigact_table[sig - 1];
 #if defined(DEBUG_SIGNAL)
     fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n", 
signal.c +1 -13
@@ -310,20 +310,8 @@
 {
     int host_sig;
     host_sig = target_to_host_signal(sig);
-    fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n", 
-            sig, strsignal(host_sig));
-#if 1
+
     _exit(-host_sig);
-#else
-    {
-        struct sigaction act;
-        sigemptyset(&act.sa_mask);
-        act.sa_flags = SA_SIGINFO;
-        act.sa_sigaction = SIG_DFL;
-        sigaction(SIGABRT, &act, NULL);
-        abort();
-    }
-#endif
 }
 
 /* queue a signal so that it will be send to the virtual CPU as soon
syscall.c +12 -6
@@ -47,6 +47,7 @@
 //#include <sys/user.h>
 #include <netinet/ip.h>
 #include <netinet/tcp.h>
+#include <sys/un.h>
 
 #define termios host_termios
 #define winsize host_winsize
@@ -427,12 +428,17 @@ static long do_select(long n, 
     return ret;
 }
 
-static inline void target_to_host_sockaddr(struct sockaddr *addr,
-                                           struct target_sockaddr *target_addr,
-                                           socklen_t len)
+static inline socklen_t target_to_host_sockaddr(struct sockaddr *addr,
+                                                struct target_sockaddr *target_addr,
+                                                socklen_t len)
 {
     memcpy(addr, target_addr, len);
     addr->sa_family = tswap16(target_addr->sa_family);
+
+    if (addr->sa_family == AF_UNIX)
+        return sizeof (struct sockaddr_un);
+
+    return len;
 }
 
 static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
@@ -668,7 +679,7 @@ static long do_socketcall(int num, int32
             socklen_t addrlen = tswap32(vptr[2]);
             void *addr = alloca(addrlen);
 
-            target_to_host_sockaddr(addr, target_addr, addrlen);
+            addrlen = target_to_host_sockaddr(addr, target_addr, addrlen);
             ret = get_errno(bind(sockfd, addr, addrlen));
         }
         break;
@@ -679,7 +690,7 @@ static long do_socketcall(int num, int32
             socklen_t addrlen = tswap32(vptr[2]);
             void *addr = alloca(addrlen);
 
-            target_to_host_sockaddr(addr, target_addr, addrlen);
+            addrlen = target_to_host_sockaddr(addr, target_addr, addrlen);
             ret = get_errno(connect(sockfd, addr, addrlen));
         }
         break;
@@ -781,7 +792,7 @@ static long do_socketcall(int num, int32
             socklen_t addrlen = tswap32(vptr[5]);
             void *addr = alloca(addrlen);
 
-            target_to_host_sockaddr(addr, target_addr, addrlen);
+            addrlen = target_to_host_sockaddr(addr, target_addr, addrlen);
             ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
         }
         break;
syscall.c +13 -2
@@ -633,10 +630,21 @@
 	case SO_LINGER:
 	case SO_RCVTIMEO:
 	case SO_SNDTIMEO:
-	case SO_PEERCRED:
 	case SO_PEERNAME:
 	    /* These don't just return a single integer */
-	    goto unimplemented;
+        if (get_user(len, optlen))
+            return -EFAULT;
+        if (len < 0)
+            return -EINVAL;
+        ret = get_errno(getsockopt(sockfd, level, optname, optval, &len));
+        if (put_user(len, optlen))
+            return -EFAULT;
+        break;
+
+	case SO_PEERCRED:
+        ret = get_errno(getsockopt(sockfd, level, optname, optval, optlen));
+        break;
+        
         default:
             if (get_user(len, optlen))
                 return -EFAULT;
syscall.c +14 -0
@@ -603,12 +603,26 @@
         case SO_RCVLOWAT:
         case SO_RCVTIMEO:
         case SO_SNDTIMEO:
+
             if (optlen < sizeof(uint32_t))
                 return -EINVAL;
             if (get_user(val, (uint32_t *)optval))
                 return -EFAULT;
             ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
             break;
+
+        case SO_LINGER:
+        {
+            struct linger tmp;
+            struct linger *target = (struct linger *) optval;
+            
+            tmp.l_onoff = tswapl(target->l_onoff);
+            tmp.l_linger = tswapl(target->l_linger);
+
+            ret = get_errno(setsockopt(sockfd, level, optname, &tmp, sizeof(struct linger)));
+        }
+        break;
+
         default:
             goto unimplemented;
         }
syscall.c renamed from syscall.c.orig +26 -3
@@ -51,6 +51,7 @@
 #include <netinet/ip.h>
 #include <netinet/tcp.h>
 #include <sys/un.h>
+#include <sys/sysctl.h>
 
 #define termios host_termios
 #define winsize host_winsize
@@ -2572,9 +2573,31 @@
         ret = get_errno(fdatasync(arg1));
         break;
     case TARGET_NR__sysctl:
-        /* We don't implement this, but ENODIR is always a safe
-           return value. */
-        return -ENOTDIR;
+        {
+            struct __sysctl_args *args = (struct __sysctl_args *) arg1;
+            int *name, nlen, *oldlenp, oldlen, newlen;
+            void *oldval, *newval;
+
+            name = (int *) tswapl((long) args->name);
+            nlen = tswapl(args->nlen);
+            oldval = (void *) tswapl((long) args->oldval);
+            oldlenp = (int *) tswapl((long) args->oldlenp);
+            oldlen = tswapl(*oldlenp);
+            newval = (void *) tswapl((long) args->newval);
+            newlen = tswapl(args->newlen);
+
+            if (nlen == 2 && name[0] == CTL_KERN && name[1] == KERN_VERSION) {
+                ret = get_errno(
+                        sysctl(name, nlen, oldval, &oldlen, newval, newlen));
+                if (!is_error(ret)) {
+                    *oldlenp = tswapl(oldlen);
+                }
+            } else {
+                gemu_log("qemu: Unsupported sysctl name\n");
+                ret = -ENOSYS;
+            }
+        }
+        break;
     case TARGET_NR_sched_setparam:
         {
             struct sched_param *target_schp = (void *)arg2;
syscall.c +27 -0
@@ -571,6 +571,17 @@
             }
             ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
             break;
+        case IP_TOS:
+            val = 0;
+            if (optlen >= sizeof(uint32_t)) {
+                if (get_user(val, (uint32_t *)optval))
+                    return -EFAULT;
+            } else if (optlen >= 1) {
+                if (get_user(val, (uint8_t *)optval))
+                    return -EFAULT;
+            }
+            ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
+            break;
         default:
             goto unimplemented;
         }
@@ -675,6 +686,22 @@
             break;
         }
         break;
+    case SOL_TCP:
+	switch (optname) {
+	case TCP_NODELAY:
+            if (get_user(len, (uint32_t *)optlen))
+                return -EFAULT;
+            if (len < 0)
+                return -EINVAL;
+            ret = get_errno(getsockopt(sockfd, level, optname, optval, &len));
+            if (put_user(len, optlen))
+                return -EFAULT;
+            break;
+
+        default:
+            goto unimplemented;
+        }
+        break;
     default:
     unimplemented:
         gemu_log("getsockopt level=%d optname=%d not yet supported\n",
syscall.c +2 -1
diff -Nru linux-user/syscall.c linux-user/syscall.c
@@ -2894,7 +2894,8 @@
 #endif
 #ifdef TARGET_NR_truncate64
     case TARGET_NR_truncate64:
-        goto unimplemented;
+	ret = get_errno(truncate64((const char *)arg1, arg2));
+	break;
 #endif
 #ifdef TARGET_NR_ftruncate64
     case TARGET_NR_ftruncate64: