]> git.proxmox.com Git - qemu.git/blobdiff - linux-user/syscall.c
mips user emulation
[qemu.git] / linux-user / syscall.c
index 95806454d7fedde10cc3e8bd3b2ced6c08f2e701..f5b7f8e68dda8825a3fdba544e88e8a5fb63b4e6 100644 (file)
@@ -43,6 +43,7 @@
 #include <sys/times.h>
 #include <sys/shm.h>
 #include <utime.h>
+#include <sys/sysinfo.h>
 //#include <sys/user.h>
 #include <netinet/ip.h>
 #include <netinet/tcp.h>
@@ -263,7 +264,7 @@ void target_set_brk(char *new_brk)
     target_original_brk = new_brk;
 }
 
-static long do_brk(char *new_brk)
+long do_brk(char *new_brk)
 {
     char *brk_page;
     long mapped_addr;
@@ -546,7 +547,21 @@ static long do_setsockopt(int sockfd, int level, int optname,
         break;
     case SOL_IP:
         switch(optname) {
+        case IP_TOS:
+        case IP_TTL:
         case IP_HDRINCL:
+        case IP_ROUTER_ALERT:
+        case IP_RECVOPTS:
+        case IP_RETOPTS:
+        case IP_PKTINFO:
+        case IP_MTU_DISCOVER:
+        case IP_RECVERR:
+        case IP_RECVTOS:
+#ifdef IP_FREEBIND
+        case IP_FREEBIND:
+#endif
+        case IP_MULTICAST_TTL:
+        case IP_MULTICAST_LOOP:
             val = 0;
             if (optlen >= sizeof(uint32_t)) {
                 if (get_user(val, (uint32_t *)optval))
@@ -576,7 +591,9 @@ static long do_setsockopt(int sockfd, int level, int optname,
         case SO_OOBINLINE:
         case SO_NO_CHECK:
         case SO_PRIORITY:
+#ifdef SO_BSDCOMPAT
         case SO_BSDCOMPAT:
+#endif
         case SO_PASSCRED:
         case SO_TIMESTAMP:
         case SO_RCVLOWAT:
@@ -616,6 +633,45 @@ static long do_getsockopt(int sockfd, int level, int optname,
            /* These don't just return a single integer */
            goto unimplemented;
         default:
+            goto int_case;
+        }
+        break;
+    case SOL_TCP:
+        /* TCP options all take an 'int' value.  */
+    int_case:
+        if (get_user(len, optlen))
+            return -EFAULT;
+        if (len < 0)
+            return -EINVAL;
+        lv = sizeof(int);
+        ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
+        if (ret < 0)
+            return ret;
+        val = tswap32(val);
+        if (len > lv)
+            len = lv;
+        if (copy_to_user(optval, &val, len))
+            return -EFAULT;
+        if (put_user(len, optlen))
+            return -EFAULT;
+        break;
+    case SOL_IP:
+        switch(optname) {
+        case IP_TOS:
+        case IP_TTL:
+        case IP_HDRINCL:
+        case IP_ROUTER_ALERT:
+        case IP_RECVOPTS:
+        case IP_RETOPTS:
+        case IP_PKTINFO:
+        case IP_MTU_DISCOVER:
+        case IP_RECVERR:
+        case IP_RECVTOS:
+#ifdef IP_FREEBIND
+        case IP_FREEBIND:
+#endif
+        case IP_MULTICAST_TTL:
+        case IP_MULTICAST_LOOP:
             if (get_user(len, optlen))
                 return -EFAULT;
             if (len < 0)
@@ -624,14 +680,25 @@ static long do_getsockopt(int sockfd, int level, int optname,
             ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
             if (ret < 0)
                 return ret;
-            val = tswap32(val);
-            if (len > lv)
-                len = lv;
-            if (copy_to_user(optval, &val, len))
-                return -EFAULT;
-            if (put_user(len, optlen))
-                return -EFAULT;
+            if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
+                unsigned char ucval = val;
+                len = 1;
+               if (put_user(len, optlen))
+                    return -EFAULT;
+               if (copy_to_user(optval,&ucval,1))
+                    return -EFAULT;
+            } else {
+                val = tswap32(val);
+                if (len > sizeof(int))
+                    len = sizeof(int);
+                if (put_user(len, optlen))
+                    return -EFAULT;
+                if (copy_to_user(optval, &val, len))
+                    return -EFAULT;
+            }
             break;
+        default:
+            goto unimplemented;
         }
         break;
     default:
@@ -1325,7 +1392,7 @@ static int write_ldt(CPUX86State *env,
         if (!ldt_table)
             return -ENOMEM;
         memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
-        env->ldt.base = ldt_table;
+        env->ldt.base = (long)ldt_table;
         env->ldt.limit = 0xffff;
     }
 
@@ -1430,6 +1497,8 @@ int do_fork(CPUState *env, unsigned int flags, unsigned long newsp)
         new_env->regs[0] = 0;
 #elif defined(TARGET_SPARC)
         printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
+#elif defined(TARGET_MIPS)
+        printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
 #elif defined(TARGET_PPC)
         if (!newsp)
             newsp = env->gpr[1];
@@ -1600,6 +1669,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
 #ifdef HAVE_GPROF
         _mcleanup();
 #endif
+        gdb_exit(cpu_env, arg1);
         /* XXX: should free thread stack and CPU env */
         _exit(arg1);
         ret = 0; /* avoid warning */
@@ -1673,6 +1743,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
     case TARGET_NR_chdir:
         ret = get_errno(chdir((const char *)arg1));
         break;
+#ifdef TARGET_NR_time
     case TARGET_NR_time:
         {
             int *time_ptr = (int *)arg1;
@@ -1681,6 +1752,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
                 tswap32s(time_ptr);
         }
         break;
+#endif
     case TARGET_NR_mknod:
         ret = get_errno(mknod((const char *)arg1, arg2, arg3));
         break;
@@ -1729,11 +1801,30 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
         break;
     case TARGET_NR_utime:
         {
-            struct utimbuf tbuf;
+            struct utimbuf tbuf, *tbuf1;
             struct target_utimbuf *target_tbuf = (void *)arg2;
-            tbuf.actime = tswapl(target_tbuf->actime);
-            tbuf.modtime = tswapl(target_tbuf->modtime);
-            ret = get_errno(utime((const char *)arg1, &tbuf));
+            if (target_tbuf) {
+                get_user(tbuf.actime, &target_tbuf->actime);
+                get_user(tbuf.modtime, &target_tbuf->modtime);
+                tbuf1 = &tbuf;
+            } else {
+                tbuf1 = NULL;
+            }
+            ret = get_errno(utime((const char *)arg1, tbuf1));
+        }
+        break;
+    case TARGET_NR_utimes:
+        {
+            struct target_timeval *target_tvp = (struct target_timeval *)arg2;
+            struct timeval *tvp, tv[2];
+            if (target_tvp) {
+                target_to_host_timeval(&tv[0], &target_tvp[0]);
+                target_to_host_timeval(&tv[1], &target_tvp[1]);
+                tvp = tv;
+            } else {
+                tvp = NULL;
+            }
+            ret = get_errno(utimes((const char *)arg1, tvp));
         }
         break;
 #ifdef TARGET_NR_stty
@@ -2095,6 +2186,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
             ret = get_errno(settimeofday(&tv, NULL));
         }
         break;
+#ifdef TARGET_NR_select
     case TARGET_NR_select:
         {
             struct target_sel_arg_struct *sel = (void *)arg1;
@@ -2107,6 +2199,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
                             (void *)sel->exp, (void *)sel->tvp);
         }
         break;
+#endif
     case TARGET_NR_symlink:
         ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
         break;
@@ -2148,6 +2241,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
                                     arg6));
 #endif
         break;
+#ifdef TARGET_NR_mmap2
     case TARGET_NR_mmap2:
 #if defined(TARGET_SPARC)
 #define MMAP_SHIFT 12
@@ -2159,6 +2253,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
                                     arg5,
                                     arg6 << MMAP_SHIFT));
         break;
+#endif
     case TARGET_NR_munmap:
         ret = get_errno(target_munmap(arg1, arg2));
         break;
@@ -2318,6 +2413,11 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
 #ifdef TARGET_NR_idle
     case TARGET_NR_idle:
         goto unimplemented;
+#endif
+#ifdef TARGET_NR_syscall
+    case TARGET_NR_syscall:
+       ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
+       break;
 #endif
     case TARGET_NR_wait4:
         {
@@ -2343,7 +2443,29 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
         ret = get_errno(swapoff((const char *)arg1));
         break;
     case TARGET_NR_sysinfo:
-        goto unimplemented;
+        {
+            struct target_sysinfo *target_value = (void *)arg1;
+            struct sysinfo value;
+            ret = get_errno(sysinfo(&value));
+            if (!is_error(ret) && target_value)
+            {
+                __put_user(value.uptime, &target_value->uptime);
+                __put_user(value.loads[0], &target_value->loads[0]);
+                __put_user(value.loads[1], &target_value->loads[1]);
+                __put_user(value.loads[2], &target_value->loads[2]);
+                __put_user(value.totalram, &target_value->totalram);
+                __put_user(value.freeram, &target_value->freeram);
+                __put_user(value.sharedram, &target_value->sharedram);
+                __put_user(value.bufferram, &target_value->bufferram);
+                __put_user(value.totalswap, &target_value->totalswap);
+                __put_user(value.freeswap, &target_value->freeswap);
+                __put_user(value.procs, &target_value->procs);
+                __put_user(value.totalhigh, &target_value->totalhigh);
+                __put_user(value.freehigh, &target_value->freehigh);
+                __put_user(value.mem_unit, &target_value->mem_unit);
+            }
+        }
+        break;
     case TARGET_NR_ipc:
        ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
        break;
@@ -2356,6 +2478,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
 #ifdef __NR_exit_group
         /* new thread calls */
     case TARGET_NR_exit_group:
+        gdb_exit(cpu_env, arg1);
         ret = get_errno(exit_group(arg1));
         break;
 #endif
@@ -2364,7 +2487,17 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
         break;
     case TARGET_NR_uname:
         /* no need to transcode because we use the linux syscall */
-        ret = get_errno(sys_uname((struct new_utsname *)arg1));
+        {
+            struct new_utsname * buf;
+    
+            buf = (struct new_utsname *)arg1;
+            ret = get_errno(sys_uname(buf));
+            if (!is_error(ret)) {
+                /* Overrite the native machine name with whatever is being
+                   emulated. */
+                strcpy (buf->machine, UNAME_MACHINE);
+            }
+        }
         break;
 #ifdef TARGET_I386
     case TARGET_NR_modify_ldt:
@@ -2402,14 +2535,19 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
         goto unimplemented;
     case TARGET_NR__llseek:
         {
+#if defined (__x86_64__)
+            ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
+            *(int64_t *)arg4 = ret;
+#else
             int64_t res;
             ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
             *(int64_t *)arg4 = tswap64(res);
+#endif
         }
         break;
     case TARGET_NR_getdents:
 #if TARGET_LONG_SIZE != 4
-#error not supported
+#warning not supported
 #elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
         {
             struct target_dirent *target_dirp = (void *)arg2;
@@ -2440,6 +2578,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
                    tnamelen = treclen - (2 * sizeof(target_long) + 2);
                    if (tnamelen > 256)
                         tnamelen = 256;
+                    /* XXX: may not be correct */
                    strncpy(tde->d_name, de->d_name, tnamelen);
                     de = (struct dirent *)((char *)de + reclen);
                     len -= reclen;
@@ -2567,7 +2706,9 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
         ret = get_errno(fdatasync(arg1));
         break;
     case TARGET_NR__sysctl:
-        goto unimplemented;
+        /* We don't implement this, but ENODIR is always a safe
+           return value. */
+        return -ENOTDIR;
     case TARGET_NR_sched_setparam:
         {
             struct sched_param *target_schp = (void *)arg2;
@@ -2665,9 +2806,11 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
     case TARGET_NR_putpmsg:
         goto unimplemented;
 #endif
+#ifdef TARGET_NR_vfork
     case TARGET_NR_vfork:
         ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
         break;
+#endif
 #ifdef TARGET_NR_ugetrlimit
     case TARGET_NR_ugetrlimit:
     {
@@ -2681,16 +2824,27 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
        break;
     }
 #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:
-        goto unimplemented;
+       ret = get_errno(ftruncate64(arg1, arg2));
+       break;
+#endif
+#ifdef TARGET_NR_stat64
     case TARGET_NR_stat64:
         ret = get_errno(stat(path((const char *)arg1), &st));
         goto do_stat64;
+#endif
+#ifdef TARGET_NR_lstat64
     case TARGET_NR_lstat64:
         ret = get_errno(lstat(path((const char *)arg1), &st));
         goto do_stat64;
+#endif
+#ifdef TARGET_NR_fstat64
     case TARGET_NR_fstat64:
         {
             ret = get_errno(fstat(arg1, &st));
@@ -2718,7 +2872,7 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
             }
         }
         break;
-
+#endif
 #ifdef USE_UID16
     case TARGET_NR_lchown:
         ret = get_errno(lchown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
@@ -2829,37 +2983,84 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
         break;
 #endif /* USE_UID16 */
 
+#ifdef TARGET_NR_lchown32
     case TARGET_NR_lchown32:
         ret = get_errno(lchown((const char *)arg1, arg2, arg3));
         break;
+#endif
+#ifdef TARGET_NR_getuid32
     case TARGET_NR_getuid32:
         ret = get_errno(getuid());
         break;
+#endif
+#ifdef TARGET_NR_getgid32
     case TARGET_NR_getgid32:
         ret = get_errno(getgid());
         break;
+#endif
+#ifdef TARGET_NR_geteuid32
     case TARGET_NR_geteuid32:
         ret = get_errno(geteuid());
         break;
+#endif
+#ifdef TARGET_NR_getegid32
     case TARGET_NR_getegid32:
         ret = get_errno(getegid());
         break;
+#endif
+#ifdef TARGET_NR_setreuid32
     case TARGET_NR_setreuid32:
         ret = get_errno(setreuid(arg1, arg2));
         break;
+#endif
+#ifdef TARGET_NR_setregid32
     case TARGET_NR_setregid32:
         ret = get_errno(setregid(arg1, arg2));
         break;
+#endif
+#ifdef TARGET_NR_getgroups32
     case TARGET_NR_getgroups32:
-        goto unimplemented;
+        {
+            int gidsetsize = arg1;
+            uint32_t *target_grouplist = (void *)arg2;
+            gid_t *grouplist;
+            int i;
+
+            grouplist = alloca(gidsetsize * sizeof(gid_t));
+            ret = get_errno(getgroups(gidsetsize, grouplist));
+            if (!is_error(ret)) {
+                for(i = 0;i < gidsetsize; i++)
+                    put_user(grouplist[i], &target_grouplist[i]);
+            }
+        }
+        break;
+#endif
+#ifdef TARGET_NR_setgroups32
     case TARGET_NR_setgroups32:
-        goto unimplemented;
+        {
+            int gidsetsize = arg1;
+            uint32_t *target_grouplist = (void *)arg2;
+            gid_t *grouplist;
+            int i;
+            
+            grouplist = alloca(gidsetsize * sizeof(gid_t));
+            for(i = 0;i < gidsetsize; i++)
+                get_user(grouplist[i], &target_grouplist[i]);
+            ret = get_errno(setgroups(gidsetsize, grouplist));
+        }
+        break;
+#endif
+#ifdef TARGET_NR_fchown32
     case TARGET_NR_fchown32:
         ret = get_errno(fchown(arg1, arg2, arg3));
         break;
+#endif
+#ifdef TARGET_NR_setresuid32
     case TARGET_NR_setresuid32:
         ret = get_errno(setresuid(arg1, arg2, arg3));
         break;
+#endif
+#ifdef TARGET_NR_getresuid32
     case TARGET_NR_getresuid32:
         {
             int ruid, euid, suid;
@@ -2871,9 +3072,13 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
             }
         }
         break;
+#endif
+#ifdef TARGET_NR_setresgid32
     case TARGET_NR_setresgid32:
         ret = get_errno(setresgid(arg1, arg2, arg3));
         break;
+#endif
+#ifdef TARGET_NR_getresgid32
     case TARGET_NR_getresgid32:
         {
             int rgid, egid, sgid;
@@ -2885,21 +3090,32 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
             }
         }
         break;
+#endif
+#ifdef TARGET_NR_chown32
     case TARGET_NR_chown32:
         ret = get_errno(chown((const char *)arg1, arg2, arg3));
         break;
+#endif
+#ifdef TARGET_NR_setuid32
     case TARGET_NR_setuid32:
         ret = get_errno(setuid(arg1));
         break;
+#endif
+#ifdef TARGET_NR_setgid32
     case TARGET_NR_setgid32:
         ret = get_errno(setgid(arg1));
         break;
+#endif
+#ifdef TARGET_NR_setfsuid32
     case TARGET_NR_setfsuid32:
         ret = get_errno(setfsuid(arg1));
         break;
+#endif
+#ifdef TARGET_NR_setfsgid32
     case TARGET_NR_setfsgid32:
         ret = get_errno(setfsgid(arg1));
         break;
+#endif
 
     case TARGET_NR_pivot_root:
         goto unimplemented;
@@ -2982,7 +3198,9 @@ long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
     default:
     unimplemented:
         gemu_log("qemu: Unsupported syscall: %d\n", num);
+#if defined(TARGET_NR_setxattr) || defined(TARGET_NR_set_thread_area)
     unimplemented_nowarn:
+#endif
         ret = -ENOSYS;
         break;
     }