Annotation of qemu/linux-user/syscall.c, revision 1.1.1.2

1.1       root        1: /*
                      2:  *  Linux syscalls
                      3:  * 
                      4:  *  Copyright (c) 2003 Fabrice Bellard
                      5:  *
                      6:  *  This program is free software; you can redistribute it and/or modify
                      7:  *  it under the terms of the GNU General Public License as published by
                      8:  *  the Free Software Foundation; either version 2 of the License, or
                      9:  *  (at your option) any later version.
                     10:  *
                     11:  *  This program is distributed in the hope that it will be useful,
                     12:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
                     13:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     14:  *  GNU General Public License for more details.
                     15:  *
                     16:  *  You should have received a copy of the GNU General Public License
                     17:  *  along with this program; if not, write to the Free Software
                     18:  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     19:  */
                     20: #include <stdlib.h>
                     21: #include <stdio.h>
                     22: #include <stdarg.h>
                     23: #include <string.h>
                     24: #include <elf.h>
                     25: #include <endian.h>
                     26: #include <errno.h>
                     27: #include <unistd.h>
                     28: #include <fcntl.h>
                     29: #include <time.h>
                     30: #include <sys/types.h>
                     31: #include <sys/wait.h>
                     32: #include <sys/time.h>
                     33: #include <sys/stat.h>
                     34: #include <sys/mount.h>
                     35: #include <sys/resource.h>
                     36: #include <sys/mman.h>
                     37: #include <sys/swap.h>
                     38: #include <signal.h>
                     39: #include <sched.h>
                     40: #include <sys/socket.h>
                     41: #include <sys/uio.h>
                     42: #include <sys/poll.h>
                     43: #include <sys/times.h>
                     44: #include <sys/shm.h>
1.1.1.2 ! root       45: #include <sys/statfs.h>
1.1       root       46: #include <utime.h>
                     47: #include <sys/sysinfo.h>
                     48: //#include <sys/user.h>
                     49: #include <netinet/ip.h>
                     50: #include <netinet/tcp.h>
                     51: 
                     52: #define termios host_termios
                     53: #define winsize host_winsize
                     54: #define termio host_termio
                     55: #define sgttyb host_sgttyb /* same as target */
                     56: #define tchars host_tchars /* same as target */
                     57: #define ltchars host_ltchars /* same as target */
                     58: 
                     59: #include <linux/termios.h>
                     60: #include <linux/unistd.h>
                     61: #include <linux/utsname.h>
                     62: #include <linux/cdrom.h>
                     63: #include <linux/hdreg.h>
                     64: #include <linux/soundcard.h>
                     65: #include <linux/dirent.h>
                     66: #include <linux/kd.h>
                     67: 
                     68: #include "qemu.h"
                     69: 
                     70: //#define DEBUG
                     71: 
                     72: #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC)
                     73: /* 16 bit uid wrappers emulation */
                     74: #define USE_UID16
                     75: #endif
                     76: 
                     77: //#include <linux/msdos_fs.h>
                     78: #define        VFAT_IOCTL_READDIR_BOTH         _IOR('r', 1, struct dirent [2])
                     79: #define        VFAT_IOCTL_READDIR_SHORT        _IOR('r', 2, struct dirent [2])
                     80: 
                     81: 
                     82: #if defined(__powerpc__)
                     83: #undef __syscall_nr
                     84: #undef __sc_loadargs_0
                     85: #undef __sc_loadargs_1
                     86: #undef __sc_loadargs_2
                     87: #undef __sc_loadargs_3
                     88: #undef __sc_loadargs_4
                     89: #undef __sc_loadargs_5
                     90: #undef __sc_asm_input_0
                     91: #undef __sc_asm_input_1
                     92: #undef __sc_asm_input_2
                     93: #undef __sc_asm_input_3
                     94: #undef __sc_asm_input_4
                     95: #undef __sc_asm_input_5
                     96: #undef _syscall0
                     97: #undef _syscall1
                     98: #undef _syscall2
                     99: #undef _syscall3
                    100: #undef _syscall4
                    101: #undef _syscall5
                    102: 
                    103: /* need to redefine syscalls as Linux kernel defines are incorrect for
                    104:    the clobber list */
                    105: /* On powerpc a system call basically clobbers the same registers like a
                    106:  * function call, with the exception of LR (which is needed for the
                    107:  * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
                    108:  * an error return status).
                    109:  */
                    110: 
                    111: #define __syscall_nr(nr, type, name, args...)                          \
                    112:        unsigned long __sc_ret, __sc_err;                               \
                    113:        {                                                               \
                    114:                register unsigned long __sc_0  __asm__ ("r0");          \
                    115:                register unsigned long __sc_3  __asm__ ("r3");          \
                    116:                register unsigned long __sc_4  __asm__ ("r4");          \
                    117:                register unsigned long __sc_5  __asm__ ("r5");          \
                    118:                register unsigned long __sc_6  __asm__ ("r6");          \
                    119:                register unsigned long __sc_7  __asm__ ("r7");          \
                    120:                                                                        \
                    121:                __sc_loadargs_##nr(name, args);                         \
                    122:                __asm__ __volatile__                                    \
                    123:                        ("sc           \n\t"                            \
                    124:                         "mfcr %0      "                                \
                    125:                        : "=&r" (__sc_0),                               \
                    126:                          "=&r" (__sc_3),  "=&r" (__sc_4),              \
                    127:                          "=&r" (__sc_5),  "=&r" (__sc_6),              \
                    128:                          "=&r" (__sc_7)                                \
                    129:                        : __sc_asm_input_##nr                           \
                    130:                        : "cr0", "ctr", "memory",                       \
                    131:                          "r8", "r9", "r10","r11", "r12");              \
                    132:                __sc_ret = __sc_3;                                      \
                    133:                __sc_err = __sc_0;                                      \
                    134:        }                                                               \
                    135:        if (__sc_err & 0x10000000)                                      \
                    136:        {                                                               \
                    137:                errno = __sc_ret;                                       \
                    138:                __sc_ret = -1;                                          \
                    139:        }                                                               \
                    140:        return (type) __sc_ret
                    141: 
                    142: #define __sc_loadargs_0(name, dummy...)                                        \
                    143:        __sc_0 = __NR_##name
                    144: #define __sc_loadargs_1(name, arg1)                                    \
                    145:        __sc_loadargs_0(name);                                          \
                    146:        __sc_3 = (unsigned long) (arg1)
                    147: #define __sc_loadargs_2(name, arg1, arg2)                              \
                    148:        __sc_loadargs_1(name, arg1);                                    \
                    149:        __sc_4 = (unsigned long) (arg2)
                    150: #define __sc_loadargs_3(name, arg1, arg2, arg3)                                \
                    151:        __sc_loadargs_2(name, arg1, arg2);                              \
                    152:        __sc_5 = (unsigned long) (arg3)
                    153: #define __sc_loadargs_4(name, arg1, arg2, arg3, arg4)                  \
                    154:        __sc_loadargs_3(name, arg1, arg2, arg3);                        \
                    155:        __sc_6 = (unsigned long) (arg4)
                    156: #define __sc_loadargs_5(name, arg1, arg2, arg3, arg4, arg5)            \
                    157:        __sc_loadargs_4(name, arg1, arg2, arg3, arg4);                  \
                    158:        __sc_7 = (unsigned long) (arg5)
                    159: 
                    160: #define __sc_asm_input_0 "0" (__sc_0)
                    161: #define __sc_asm_input_1 __sc_asm_input_0, "1" (__sc_3)
                    162: #define __sc_asm_input_2 __sc_asm_input_1, "2" (__sc_4)
                    163: #define __sc_asm_input_3 __sc_asm_input_2, "3" (__sc_5)
                    164: #define __sc_asm_input_4 __sc_asm_input_3, "4" (__sc_6)
                    165: #define __sc_asm_input_5 __sc_asm_input_4, "5" (__sc_7)
                    166: 
                    167: #define _syscall0(type,name)                                           \
                    168: type name(void)                                                                \
                    169: {                                                                      \
                    170:        __syscall_nr(0, type, name);                                    \
                    171: }
                    172: 
                    173: #define _syscall1(type,name,type1,arg1)                                        \
                    174: type name(type1 arg1)                                                  \
                    175: {                                                                      \
                    176:        __syscall_nr(1, type, name, arg1);                              \
                    177: }
                    178: 
                    179: #define _syscall2(type,name,type1,arg1,type2,arg2)                     \
                    180: type name(type1 arg1, type2 arg2)                                      \
                    181: {                                                                      \
                    182:        __syscall_nr(2, type, name, arg1, arg2);                        \
                    183: }
                    184: 
                    185: #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)          \
                    186: type name(type1 arg1, type2 arg2, type3 arg3)                          \
                    187: {                                                                      \
                    188:        __syscall_nr(3, type, name, arg1, arg2, arg3);                  \
                    189: }
                    190: 
                    191: #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
                    192: type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4)              \
                    193: {                                                                      \
                    194:        __syscall_nr(4, type, name, arg1, arg2, arg3, arg4);            \
                    195: }
                    196: 
                    197: #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
                    198: type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)  \
                    199: {                                                                      \
                    200:        __syscall_nr(5, type, name, arg1, arg2, arg3, arg4, arg5);      \
                    201: }
                    202: #endif
                    203: 
                    204: #define __NR_sys_uname __NR_uname
                    205: #define __NR_sys_getcwd1 __NR_getcwd
                    206: #define __NR_sys_getdents __NR_getdents
                    207: #define __NR_sys_getdents64 __NR_getdents64
                    208: #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
                    209: 
                    210: #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
                    211: #define __NR__llseek __NR_lseek
                    212: #endif
                    213: 
                    214: #ifdef __NR_gettid
                    215: _syscall0(int, gettid)
                    216: #else
                    217: static int gettid(void) {
                    218:     return -ENOSYS;
                    219: }
                    220: #endif
                    221: _syscall1(int,sys_uname,struct new_utsname *,buf)
                    222: _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
                    223: _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
                    224: _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
                    225: _syscall5(int, _llseek,  uint,  fd, ulong, hi, ulong, lo,
                    226:           loff_t *, res, uint, wh);
                    227: _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
                    228: #ifdef __NR_exit_group
                    229: _syscall1(int,exit_group,int,error_code)
                    230: #endif
                    231: 
                    232: extern int personality(int);
                    233: extern int flock(int, int);
                    234: extern int setfsuid(int);
                    235: extern int setfsgid(int);
                    236: extern int setresuid(uid_t, uid_t, uid_t);
                    237: extern int getresuid(uid_t *, uid_t *, uid_t *);
                    238: extern int setresgid(gid_t, gid_t, gid_t);
                    239: extern int getresgid(gid_t *, gid_t *, gid_t *);
                    240: extern int setgroups(int, gid_t *);
                    241: 
                    242: static inline long get_errno(long ret)
                    243: {
                    244:     if (ret == -1)
                    245:         return -errno;
                    246:     else
                    247:         return ret;
                    248: }
                    249: 
                    250: static inline int is_error(long ret)
                    251: {
                    252:     return (unsigned long)ret >= (unsigned long)(-4096);
                    253: }
                    254: 
                    255: static char *target_brk;
                    256: static char *target_original_brk;
                    257: 
                    258: void target_set_brk(char *new_brk)
                    259: {
                    260:     target_brk = new_brk;
                    261:     target_original_brk = new_brk;
                    262: }
                    263: 
                    264: long do_brk(char *new_brk)
                    265: {
                    266:     char *brk_page;
                    267:     long mapped_addr;
                    268:     int        new_alloc_size;
                    269: 
                    270:     if (!new_brk)
                    271:         return (long)target_brk;
                    272:     if (new_brk < target_original_brk)
                    273:         return -ENOMEM;
                    274:     
                    275:     brk_page = (char *)HOST_PAGE_ALIGN((unsigned long)target_brk);
                    276: 
                    277:     /* If the new brk is less than this, set it and we're done... */
                    278:     if (new_brk < brk_page) {
                    279:        target_brk = new_brk;
                    280:        return (long)target_brk;
                    281:     }
                    282: 
                    283:     /* We need to allocate more memory after the brk... */
                    284:     new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
                    285:     mapped_addr = get_errno(target_mmap((unsigned long)brk_page, new_alloc_size, 
                    286:                                         PROT_READ|PROT_WRITE,
                    287:                                         MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
                    288:     if (is_error(mapped_addr)) {
                    289:        return mapped_addr;
                    290:     } else {
                    291:        target_brk = new_brk;
                    292:        return (long)target_brk;
                    293:     }
                    294: }
                    295: 
                    296: static inline fd_set *target_to_host_fds(fd_set *fds, 
                    297:                                          target_long *target_fds, int n)
                    298: {
                    299: #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
                    300:     return (fd_set *)target_fds;
                    301: #else
                    302:     int i, b;
                    303:     if (target_fds) {
                    304:         FD_ZERO(fds);
                    305:         for(i = 0;i < n; i++) {
                    306:             b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
                    307:                  (i & (TARGET_LONG_BITS - 1))) & 1;
                    308:             if (b)
                    309:                 FD_SET(i, fds);
                    310:         }
                    311:         return fds;
                    312:     } else {
                    313:         return NULL;
                    314:     }
                    315: #endif
                    316: }
                    317: 
                    318: static inline void host_to_target_fds(target_long *target_fds, 
                    319:                                       fd_set *fds, int n)
                    320: {
                    321: #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
                    322:     /* nothing to do */
                    323: #else
                    324:     int i, nw, j, k;
                    325:     target_long v;
                    326: 
                    327:     if (target_fds) {
                    328:         nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
                    329:         k = 0;
                    330:         for(i = 0;i < nw; i++) {
                    331:             v = 0;
                    332:             for(j = 0; j < TARGET_LONG_BITS; j++) {
                    333:                 v |= ((FD_ISSET(k, fds) != 0) << j);
                    334:                 k++;
                    335:             }
                    336:             target_fds[i] = tswapl(v);
                    337:         }
                    338:     }
                    339: #endif
                    340: }
                    341: 
                    342: #if defined(__alpha__)
                    343: #define HOST_HZ 1024
                    344: #else
                    345: #define HOST_HZ 100
                    346: #endif
                    347: 
                    348: static inline long host_to_target_clock_t(long ticks)
                    349: {
                    350: #if HOST_HZ == TARGET_HZ
                    351:     return ticks;
                    352: #else
                    353:     return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
                    354: #endif
                    355: }
                    356: 
                    357: static inline void host_to_target_rusage(struct target_rusage *target_rusage, 
                    358:                                          const struct rusage *rusage)
                    359: {
                    360:     target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
                    361:     target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
                    362:     target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
                    363:     target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
                    364:     target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
                    365:     target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
                    366:     target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
                    367:     target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
                    368:     target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
                    369:     target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
                    370:     target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
                    371:     target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
                    372:     target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
                    373:     target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
                    374:     target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
                    375:     target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
                    376:     target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
                    377:     target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
                    378: }
                    379: 
                    380: static inline void target_to_host_timeval(struct timeval *tv, 
                    381:                                           const struct target_timeval *target_tv)
                    382: {
                    383:     tv->tv_sec = tswapl(target_tv->tv_sec);
                    384:     tv->tv_usec = tswapl(target_tv->tv_usec);
                    385: }
                    386: 
                    387: static inline void host_to_target_timeval(struct target_timeval *target_tv, 
                    388:                                           const struct timeval *tv)
                    389: {
                    390:     target_tv->tv_sec = tswapl(tv->tv_sec);
                    391:     target_tv->tv_usec = tswapl(tv->tv_usec);
                    392: }
                    393: 
                    394: 
                    395: static long do_select(long n, 
                    396:                       target_long *target_rfds, target_long *target_wfds, 
                    397:                       target_long *target_efds, struct target_timeval *target_tv)
                    398: {
                    399:     fd_set rfds, wfds, efds;
                    400:     fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
                    401:     struct timeval tv, *tv_ptr;
                    402:     long ret;
                    403: 
                    404:     rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
                    405:     wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
                    406:     efds_ptr = target_to_host_fds(&efds, target_efds, n);
                    407:             
                    408:     if (target_tv) {
                    409:         target_to_host_timeval(&tv, target_tv);
                    410:         tv_ptr = &tv;
                    411:     } else {
                    412:         tv_ptr = NULL;
                    413:     }
                    414:     ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
                    415:     if (!is_error(ret)) {
                    416:         host_to_target_fds(target_rfds, rfds_ptr, n);
                    417:         host_to_target_fds(target_wfds, wfds_ptr, n);
                    418:         host_to_target_fds(target_efds, efds_ptr, n);
                    419: 
                    420:         if (target_tv) {
                    421:             host_to_target_timeval(target_tv, &tv);
                    422:         }
                    423:     }
                    424:     return ret;
                    425: }
                    426: 
                    427: static inline void target_to_host_sockaddr(struct sockaddr *addr,
                    428:                                            struct target_sockaddr *target_addr,
                    429:                                            socklen_t len)
                    430: {
                    431:     memcpy(addr, target_addr, len);
                    432:     addr->sa_family = tswap16(target_addr->sa_family);
                    433: }
                    434: 
                    435: static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
                    436:                                            struct sockaddr *addr,
                    437:                                            socklen_t len)
                    438: {
                    439:     memcpy(target_addr, addr, len);
                    440:     target_addr->sa_family = tswap16(addr->sa_family);
                    441: }
                    442: 
                    443: static inline void target_to_host_cmsg(struct msghdr *msgh,
                    444:                                        struct target_msghdr *target_msgh)
                    445: {
                    446:     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
                    447:     struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
                    448:     socklen_t space = 0;
                    449: 
                    450:     while (cmsg && target_cmsg) {
                    451:         void *data = CMSG_DATA(cmsg);
                    452:         void *target_data = TARGET_CMSG_DATA(target_cmsg);
                    453: 
                    454:         int len = tswapl(target_cmsg->cmsg_len) 
                    455:                   - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
                    456: 
                    457:         space += CMSG_SPACE(len);
                    458:         if (space > msgh->msg_controllen) {
                    459:             space -= CMSG_SPACE(len);
1.1.1.2 ! root      460:             gemu_log("Host cmsg overflow\n");
1.1       root      461:             break;
                    462:         }
                    463: 
                    464:         cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
                    465:         cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
                    466:         cmsg->cmsg_len = CMSG_LEN(len);
                    467: 
                    468:         if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
                    469:             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
                    470:             memcpy(data, target_data, len);
                    471:         } else {
                    472:             int *fd = (int *)data;
                    473:             int *target_fd = (int *)target_data;
                    474:             int i, numfds = len / sizeof(int);
                    475: 
                    476:             for (i = 0; i < numfds; i++)
                    477:                 fd[i] = tswap32(target_fd[i]);
                    478:         }
                    479: 
                    480:         cmsg = CMSG_NXTHDR(msgh, cmsg);
                    481:         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
                    482:     }
                    483: 
                    484:     msgh->msg_controllen = space;
                    485: }
                    486: 
                    487: static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
                    488:                                        struct msghdr *msgh)
                    489: {
                    490:     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
                    491:     struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
                    492:     socklen_t space = 0;
                    493: 
                    494:     while (cmsg && target_cmsg) {
                    495:         void *data = CMSG_DATA(cmsg);
                    496:         void *target_data = TARGET_CMSG_DATA(target_cmsg);
                    497: 
                    498:         int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
                    499: 
                    500:         space += TARGET_CMSG_SPACE(len);
                    501:         if (space > tswapl(target_msgh->msg_controllen)) {
                    502:             space -= TARGET_CMSG_SPACE(len);
1.1.1.2 ! root      503:             gemu_log("Target cmsg overflow\n");
1.1       root      504:             break;
                    505:         }
                    506: 
                    507:         target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
                    508:         target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
                    509:         target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
                    510: 
                    511:         if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
                    512:             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
                    513:             memcpy(target_data, data, len);
                    514:         } else {
                    515:             int *fd = (int *)data;
                    516:             int *target_fd = (int *)target_data;
                    517:             int i, numfds = len / sizeof(int);
                    518: 
                    519:             for (i = 0; i < numfds; i++)
                    520:                 target_fd[i] = tswap32(fd[i]);
                    521:         }
                    522: 
                    523:         cmsg = CMSG_NXTHDR(msgh, cmsg);
                    524:         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
                    525:     }
                    526: 
                    527:     msgh->msg_controllen = tswapl(space);
                    528: }
                    529: 
                    530: static long do_setsockopt(int sockfd, int level, int optname, 
                    531:                           void *optval, socklen_t optlen)
                    532: {
                    533:     int val, ret;
                    534:             
                    535:     switch(level) {
                    536:     case SOL_TCP:
                    537:         /* TCP options all take an 'int' value.  */
                    538:         if (optlen < sizeof(uint32_t))
                    539:             return -EINVAL;
                    540:         
                    541:         if (get_user(val, (uint32_t *)optval))
                    542:             return -EFAULT;
                    543:         ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
                    544:         break;
                    545:     case SOL_IP:
                    546:         switch(optname) {
                    547:         case IP_TOS:
                    548:         case IP_TTL:
                    549:         case IP_HDRINCL:
                    550:         case IP_ROUTER_ALERT:
                    551:         case IP_RECVOPTS:
                    552:         case IP_RETOPTS:
                    553:         case IP_PKTINFO:
                    554:         case IP_MTU_DISCOVER:
                    555:         case IP_RECVERR:
                    556:         case IP_RECVTOS:
                    557: #ifdef IP_FREEBIND
                    558:         case IP_FREEBIND:
                    559: #endif
                    560:         case IP_MULTICAST_TTL:
                    561:         case IP_MULTICAST_LOOP:
                    562:             val = 0;
                    563:             if (optlen >= sizeof(uint32_t)) {
                    564:                 if (get_user(val, (uint32_t *)optval))
                    565:                     return -EFAULT;
                    566:             } else if (optlen >= 1) {
                    567:                 if (get_user(val, (uint8_t *)optval))
                    568:                     return -EFAULT;
                    569:             }
                    570:             ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
                    571:             break;
                    572:         default:
                    573:             goto unimplemented;
                    574:         }
                    575:         break;
                    576:     case SOL_SOCKET:
                    577:         switch (optname) {
                    578:             /* Options with 'int' argument.  */
                    579:         case SO_DEBUG:
                    580:         case SO_REUSEADDR:
                    581:         case SO_TYPE:
                    582:         case SO_ERROR:
                    583:         case SO_DONTROUTE:
                    584:         case SO_BROADCAST:
                    585:         case SO_SNDBUF:
                    586:         case SO_RCVBUF:
                    587:         case SO_KEEPALIVE:
                    588:         case SO_OOBINLINE:
                    589:         case SO_NO_CHECK:
                    590:         case SO_PRIORITY:
                    591: #ifdef SO_BSDCOMPAT
                    592:         case SO_BSDCOMPAT:
                    593: #endif
                    594:         case SO_PASSCRED:
                    595:         case SO_TIMESTAMP:
                    596:         case SO_RCVLOWAT:
                    597:         case SO_RCVTIMEO:
                    598:         case SO_SNDTIMEO:
                    599:             if (optlen < sizeof(uint32_t))
                    600:                 return -EINVAL;
                    601:             if (get_user(val, (uint32_t *)optval))
                    602:                 return -EFAULT;
                    603:             ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
                    604:             break;
                    605:         default:
                    606:             goto unimplemented;
                    607:         }
                    608:         break;
                    609:     default:
                    610:     unimplemented:
                    611:         gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
                    612:         ret = -ENOSYS;
                    613:     }
                    614:     return ret;
                    615: }
                    616: 
                    617: static long do_getsockopt(int sockfd, int level, int optname, 
                    618:                           void *optval, socklen_t *optlen)
                    619: {
                    620:     int len, lv, val, ret;
                    621: 
                    622:     switch(level) {
                    623:     case SOL_SOCKET:
                    624:        switch (optname) {
                    625:        case SO_LINGER:
                    626:        case SO_RCVTIMEO:
                    627:        case SO_SNDTIMEO:
                    628:        case SO_PEERCRED:
                    629:        case SO_PEERNAME:
                    630:            /* These don't just return a single integer */
                    631:            goto unimplemented;
                    632:         default:
                    633:             goto int_case;
                    634:         }
                    635:         break;
                    636:     case SOL_TCP:
                    637:         /* TCP options all take an 'int' value.  */
                    638:     int_case:
                    639:         if (get_user(len, optlen))
                    640:             return -EFAULT;
                    641:         if (len < 0)
                    642:             return -EINVAL;
                    643:         lv = sizeof(int);
                    644:         ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
                    645:         if (ret < 0)
                    646:             return ret;
                    647:         val = tswap32(val);
                    648:         if (len > lv)
                    649:             len = lv;
                    650:         if (copy_to_user(optval, &val, len))
                    651:             return -EFAULT;
                    652:         if (put_user(len, optlen))
                    653:             return -EFAULT;
                    654:         break;
                    655:     case SOL_IP:
                    656:         switch(optname) {
                    657:         case IP_TOS:
                    658:         case IP_TTL:
                    659:         case IP_HDRINCL:
                    660:         case IP_ROUTER_ALERT:
                    661:         case IP_RECVOPTS:
                    662:         case IP_RETOPTS:
                    663:         case IP_PKTINFO:
                    664:         case IP_MTU_DISCOVER:
                    665:         case IP_RECVERR:
                    666:         case IP_RECVTOS:
                    667: #ifdef IP_FREEBIND
                    668:         case IP_FREEBIND:
                    669: #endif
                    670:         case IP_MULTICAST_TTL:
                    671:         case IP_MULTICAST_LOOP:
                    672:             if (get_user(len, optlen))
                    673:                 return -EFAULT;
                    674:             if (len < 0)
                    675:                 return -EINVAL;
                    676:             lv = sizeof(int);
                    677:             ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
                    678:             if (ret < 0)
                    679:                 return ret;
                    680:             if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
                    681:                 unsigned char ucval = val;
                    682:                 len = 1;
                    683:                if (put_user(len, optlen))
                    684:                     return -EFAULT;
                    685:                if (copy_to_user(optval,&ucval,1))
                    686:                     return -EFAULT;
                    687:             } else {
                    688:                 val = tswap32(val);
                    689:                 if (len > sizeof(int))
                    690:                     len = sizeof(int);
                    691:                 if (put_user(len, optlen))
                    692:                     return -EFAULT;
                    693:                 if (copy_to_user(optval, &val, len))
                    694:                     return -EFAULT;
                    695:             }
                    696:             break;
                    697:         default:
                    698:             goto unimplemented;
                    699:         }
                    700:         break;
                    701:     default:
                    702:     unimplemented:
                    703:         gemu_log("getsockopt level=%d optname=%d not yet supported\n",
                    704:                  level, optname);
                    705:         ret = -ENOSYS;
                    706:         break;
                    707:     }
                    708:     return ret;
                    709: }
                    710: 
                    711: static long do_socketcall(int num, int32_t *vptr)
                    712: {
                    713:     long ret;
                    714: 
                    715:     switch(num) {
                    716:     case SOCKOP_socket:
                    717:        {
                    718:             int domain = tswap32(vptr[0]);
                    719:             int type = tswap32(vptr[1]);
                    720:             int protocol = tswap32(vptr[2]);
                    721: 
                    722:             ret = get_errno(socket(domain, type, protocol));
                    723:        }
                    724:         break;
                    725:     case SOCKOP_bind:
                    726:        {
                    727:             int sockfd = tswap32(vptr[0]);
                    728:             void *target_addr = (void *)tswap32(vptr[1]);
                    729:             socklen_t addrlen = tswap32(vptr[2]);
                    730:             void *addr = alloca(addrlen);
                    731: 
                    732:             target_to_host_sockaddr(addr, target_addr, addrlen);
                    733:             ret = get_errno(bind(sockfd, addr, addrlen));
                    734:         }
                    735:         break;
                    736:     case SOCKOP_connect:
                    737:         {
                    738:             int sockfd = tswap32(vptr[0]);
                    739:             void *target_addr = (void *)tswap32(vptr[1]);
                    740:             socklen_t addrlen = tswap32(vptr[2]);
                    741:             void *addr = alloca(addrlen);
                    742: 
                    743:             target_to_host_sockaddr(addr, target_addr, addrlen);
                    744:             ret = get_errno(connect(sockfd, addr, addrlen));
                    745:         }
                    746:         break;
                    747:     case SOCKOP_listen:
                    748:         {
                    749:             int sockfd = tswap32(vptr[0]);
                    750:             int backlog = tswap32(vptr[1]);
                    751: 
                    752:             ret = get_errno(listen(sockfd, backlog));
                    753:         }
                    754:         break;
                    755:     case SOCKOP_accept:
                    756:         {
                    757:             int sockfd = tswap32(vptr[0]);
                    758:             void *target_addr = (void *)tswap32(vptr[1]);
                    759:             uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
                    760:             socklen_t addrlen = tswap32(*target_addrlen);
                    761:             void *addr = alloca(addrlen);
                    762: 
                    763:             ret = get_errno(accept(sockfd, addr, &addrlen));
                    764:             if (!is_error(ret)) {
                    765:                 host_to_target_sockaddr(target_addr, addr, addrlen);
                    766:                 *target_addrlen = tswap32(addrlen);
                    767:             }
                    768:         }
                    769:         break;
                    770:     case SOCKOP_getsockname:
                    771:         {
                    772:             int sockfd = tswap32(vptr[0]);
                    773:             void *target_addr = (void *)tswap32(vptr[1]);
                    774:             uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
                    775:             socklen_t addrlen = tswap32(*target_addrlen);
                    776:             void *addr = alloca(addrlen);
                    777: 
                    778:             ret = get_errno(getsockname(sockfd, addr, &addrlen));
                    779:             if (!is_error(ret)) {
                    780:                 host_to_target_sockaddr(target_addr, addr, addrlen);
                    781:                 *target_addrlen = tswap32(addrlen);
                    782:             }
                    783:         }
                    784:         break;
                    785:     case SOCKOP_getpeername:
                    786:         {
                    787:             int sockfd = tswap32(vptr[0]);
                    788:             void *target_addr = (void *)tswap32(vptr[1]);
                    789:             uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
                    790:             socklen_t addrlen = tswap32(*target_addrlen);
                    791:             void *addr = alloca(addrlen);
                    792: 
                    793:             ret = get_errno(getpeername(sockfd, addr, &addrlen));
                    794:             if (!is_error(ret)) {
                    795:                 host_to_target_sockaddr(target_addr, addr, addrlen);
                    796:                 *target_addrlen = tswap32(addrlen);
                    797:             }
                    798:         }
                    799:         break;
                    800:     case SOCKOP_socketpair:
                    801:         {
                    802:             int domain = tswap32(vptr[0]);
                    803:             int type = tswap32(vptr[1]);
                    804:             int protocol = tswap32(vptr[2]);
                    805:             int32_t *target_tab = (void *)tswap32(vptr[3]);
                    806:             int tab[2];
                    807: 
                    808:             ret = get_errno(socketpair(domain, type, protocol, tab));
                    809:             if (!is_error(ret)) {
                    810:                 target_tab[0] = tswap32(tab[0]);
                    811:                 target_tab[1] = tswap32(tab[1]);
                    812:             }
                    813:         }
                    814:         break;
                    815:     case SOCKOP_send:
                    816:         {
                    817:             int sockfd = tswap32(vptr[0]);
                    818:             void *msg = (void *)tswap32(vptr[1]);
                    819:             size_t len = tswap32(vptr[2]);
                    820:             int flags = tswap32(vptr[3]);
                    821: 
                    822:             ret = get_errno(send(sockfd, msg, len, flags));
                    823:         }
                    824:         break;
                    825:     case SOCKOP_recv:
                    826:         {
                    827:             int sockfd = tswap32(vptr[0]);
                    828:             void *msg = (void *)tswap32(vptr[1]);
                    829:             size_t len = tswap32(vptr[2]);
                    830:             int flags = tswap32(vptr[3]);
                    831: 
                    832:             ret = get_errno(recv(sockfd, msg, len, flags));
                    833:         }
                    834:         break;
                    835:     case SOCKOP_sendto:
                    836:         {
                    837:             int sockfd = tswap32(vptr[0]);
                    838:             void *msg = (void *)tswap32(vptr[1]);
                    839:             size_t len = tswap32(vptr[2]);
                    840:             int flags = tswap32(vptr[3]);
                    841:             void *target_addr = (void *)tswap32(vptr[4]);
                    842:             socklen_t addrlen = tswap32(vptr[5]);
                    843:             void *addr = alloca(addrlen);
                    844: 
                    845:             target_to_host_sockaddr(addr, target_addr, addrlen);
                    846:             ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
                    847:         }
                    848:         break;
                    849:     case SOCKOP_recvfrom:
                    850:         {
                    851:             int sockfd = tswap32(vptr[0]);
                    852:             void *msg = (void *)tswap32(vptr[1]);
                    853:             size_t len = tswap32(vptr[2]);
                    854:             int flags = tswap32(vptr[3]);
                    855:             void *target_addr = (void *)tswap32(vptr[4]);
                    856:             uint32_t *target_addrlen = (void *)tswap32(vptr[5]);
                    857:             socklen_t addrlen = tswap32(*target_addrlen);
                    858:             void *addr = alloca(addrlen);
                    859: 
                    860:             ret = get_errno(recvfrom(sockfd, msg, len, flags, addr, &addrlen));
                    861:             if (!is_error(ret)) {
                    862:                 host_to_target_sockaddr(target_addr, addr, addrlen);
                    863:                 *target_addrlen = tswap32(addrlen);
                    864:             }
                    865:         }
                    866:         break;
                    867:     case SOCKOP_shutdown:
                    868:         {
                    869:             int sockfd = tswap32(vptr[0]);
                    870:             int how = tswap32(vptr[1]);
                    871: 
                    872:             ret = get_errno(shutdown(sockfd, how));
                    873:         }
                    874:         break;
                    875:     case SOCKOP_sendmsg:
                    876:     case SOCKOP_recvmsg:
                    877:         {
                    878:             int fd;
                    879:             struct target_msghdr *msgp;
                    880:             struct msghdr msg;
                    881:             int flags, count, i;
                    882:             struct iovec *vec;
                    883:             struct target_iovec *target_vec;
                    884: 
                    885:             msgp = (void *)tswap32(vptr[1]);
                    886:             msg.msg_name = (void *)tswapl(msgp->msg_name);
                    887:             msg.msg_namelen = tswapl(msgp->msg_namelen);
                    888:             msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
                    889:             msg.msg_control = alloca(msg.msg_controllen);
                    890:             msg.msg_flags = tswap32(msgp->msg_flags);
                    891: 
                    892:             count = tswapl(msgp->msg_iovlen);
                    893:             vec = alloca(count * sizeof(struct iovec));
                    894:             target_vec = (void *)tswapl(msgp->msg_iov);
                    895:             for(i = 0;i < count; i++) {
                    896:                 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
                    897:                 vec[i].iov_len = tswapl(target_vec[i].iov_len);
                    898:             }
                    899:             msg.msg_iovlen = count;
                    900:             msg.msg_iov = vec;
                    901: 
                    902:             fd = tswap32(vptr[0]);
                    903:             flags = tswap32(vptr[2]);
                    904:             if (num == SOCKOP_sendmsg) {
                    905:                 target_to_host_cmsg(&msg, msgp);
                    906:                 ret = get_errno(sendmsg(fd, &msg, flags));
                    907:             } else {
                    908:                 ret = get_errno(recvmsg(fd, &msg, flags));
                    909:                 if (!is_error(ret))
                    910:                   host_to_target_cmsg(msgp, &msg);
                    911:             }
                    912:         }
                    913:         break;
                    914:     case SOCKOP_setsockopt:
                    915:         {
                    916:             int sockfd = tswap32(vptr[0]);
                    917:             int level = tswap32(vptr[1]);
                    918:             int optname = tswap32(vptr[2]);
                    919:             void *optval = (void *)tswap32(vptr[3]);
                    920:             socklen_t optlen = tswap32(vptr[4]);
                    921: 
                    922:             ret = do_setsockopt(sockfd, level, optname, optval, optlen);
                    923:         }
                    924:         break;
                    925:     case SOCKOP_getsockopt:
                    926:         {
                    927:             int sockfd = tswap32(vptr[0]);
                    928:             int level = tswap32(vptr[1]);
                    929:             int optname = tswap32(vptr[2]);
                    930:             void *optval = (void *)tswap32(vptr[3]);
                    931:             uint32_t *poptlen = (void *)tswap32(vptr[4]);
                    932: 
                    933:             ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
                    934:         }
                    935:         break;
                    936:     default:
                    937:         gemu_log("Unsupported socketcall: %d\n", num);
                    938:         ret = -ENOSYS;
                    939:         break;
                    940:     }
                    941:     return ret;
                    942: }
                    943: 
                    944: 
                    945: #define N_SHM_REGIONS  32
                    946: 
                    947: static struct shm_region {
                    948:     uint32_t   start;
                    949:     uint32_t   size;
                    950: } shm_regions[N_SHM_REGIONS];
                    951: 
                    952: static long do_ipc(long call, long first, long second, long third,
                    953:                   long ptr, long fifth)
                    954: {
                    955:     int version;
                    956:     long ret = 0;
                    957:     unsigned long raddr;
                    958:     struct shmid_ds shm_info;
                    959:     int i;
                    960: 
                    961:     version = call >> 16;
                    962:     call &= 0xffff;
                    963: 
                    964:     switch (call) {
                    965:     case IPCOP_shmat:
                    966:        /* SHM_* flags are the same on all linux platforms */
                    967:        ret = get_errno((long) shmat(first, (void *) ptr, second));
                    968:         if (is_error(ret))
                    969:             break;
                    970:         raddr = ret;
                    971:        /* find out the length of the shared memory segment */
                    972:         
                    973:         ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
                    974:         if (is_error(ret)) {
                    975:             /* can't get length, bail out */
                    976:             shmdt((void *) raddr);
                    977:            break;
                    978:        }
                    979:        page_set_flags(raddr, raddr + shm_info.shm_segsz,
                    980:                       PAGE_VALID | PAGE_READ |
                    981:                       ((second & SHM_RDONLY)? 0: PAGE_WRITE));
                    982:        for (i = 0; i < N_SHM_REGIONS; ++i) {
                    983:            if (shm_regions[i].start == 0) {
                    984:                shm_regions[i].start = raddr;
                    985:                shm_regions[i].size = shm_info.shm_segsz;
                    986:                 break;
                    987:            }
                    988:        }
                    989:        if (put_user(raddr, (uint32_t *)third))
                    990:             return -EFAULT;
                    991:         ret = 0;
                    992:        break;
                    993:     case IPCOP_shmdt:
                    994:        for (i = 0; i < N_SHM_REGIONS; ++i) {
                    995:            if (shm_regions[i].start == ptr) {
                    996:                shm_regions[i].start = 0;
                    997:                page_set_flags(ptr, shm_regions[i].size, 0);
                    998:                break;
                    999:            }
                   1000:        }
                   1001:        ret = get_errno(shmdt((void *) ptr));
                   1002:        break;
                   1003: 
                   1004:     case IPCOP_shmget:
                   1005:        /* IPC_* flag values are the same on all linux platforms */
                   1006:        ret = get_errno(shmget(first, second, third));
                   1007:        break;
                   1008: 
                   1009:        /* IPC_* and SHM_* command values are the same on all linux platforms */
                   1010:     case IPCOP_shmctl:
                   1011:         switch(second) {
                   1012:         case IPC_RMID:
                   1013:         case SHM_LOCK:
                   1014:         case SHM_UNLOCK:
                   1015:             ret = get_errno(shmctl(first, second, NULL));
                   1016:             break;
                   1017:         default:
                   1018:             goto unimplemented;
                   1019:         }
                   1020:         break;
                   1021:     default:
                   1022:     unimplemented:
                   1023:        gemu_log("Unsupported ipc call: %ld (version %d)\n", call, version);
                   1024:        ret = -ENOSYS;
                   1025:        break;
                   1026:     }
                   1027:     return ret;
                   1028: }
                   1029: 
                   1030: /* kernel structure types definitions */
                   1031: #define IFNAMSIZ        16
                   1032: 
                   1033: #define STRUCT(name, list...) STRUCT_ ## name,
                   1034: #define STRUCT_SPECIAL(name) STRUCT_ ## name,
                   1035: enum {
                   1036: #include "syscall_types.h"
                   1037: };
                   1038: #undef STRUCT
                   1039: #undef STRUCT_SPECIAL
                   1040: 
                   1041: #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
                   1042: #define STRUCT_SPECIAL(name)
                   1043: #include "syscall_types.h"
                   1044: #undef STRUCT
                   1045: #undef STRUCT_SPECIAL
                   1046: 
                   1047: typedef struct IOCTLEntry {
                   1048:     unsigned int target_cmd;
                   1049:     unsigned int host_cmd;
                   1050:     const char *name;
                   1051:     int access;
                   1052:     const argtype arg_type[5];
                   1053: } IOCTLEntry;
                   1054: 
                   1055: #define IOC_R 0x0001
                   1056: #define IOC_W 0x0002
                   1057: #define IOC_RW (IOC_R | IOC_W)
                   1058: 
                   1059: #define MAX_STRUCT_SIZE 4096
                   1060: 
                   1061: IOCTLEntry ioctl_entries[] = {
                   1062: #define IOCTL(cmd, access, types...) \
                   1063:     { TARGET_ ## cmd, cmd, #cmd, access, { types } },
                   1064: #include "ioctls.h"
                   1065:     { 0, 0, },
                   1066: };
                   1067: 
                   1068: static long do_ioctl(long fd, long cmd, long arg)
                   1069: {
                   1070:     const IOCTLEntry *ie;
                   1071:     const argtype *arg_type;
                   1072:     long ret;
                   1073:     uint8_t buf_temp[MAX_STRUCT_SIZE];
                   1074: 
                   1075:     ie = ioctl_entries;
                   1076:     for(;;) {
                   1077:         if (ie->target_cmd == 0) {
                   1078:             gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
                   1079:             return -ENOSYS;
                   1080:         }
                   1081:         if (ie->target_cmd == cmd)
                   1082:             break;
                   1083:         ie++;
                   1084:     }
                   1085:     arg_type = ie->arg_type;
                   1086: #if defined(DEBUG)
                   1087:     gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
                   1088: #endif
                   1089:     switch(arg_type[0]) {
                   1090:     case TYPE_NULL:
                   1091:         /* no argument */
                   1092:         ret = get_errno(ioctl(fd, ie->host_cmd));
                   1093:         break;
                   1094:     case TYPE_PTRVOID:
                   1095:     case TYPE_INT:
                   1096:         /* int argment */
                   1097:         ret = get_errno(ioctl(fd, ie->host_cmd, arg));
                   1098:         break;
                   1099:     case TYPE_PTR:
                   1100:         arg_type++;
                   1101:         switch(ie->access) {
                   1102:         case IOC_R:
                   1103:             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
                   1104:             if (!is_error(ret)) {
                   1105:                 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
                   1106:             }
                   1107:             break;
                   1108:         case IOC_W:
                   1109:             thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
                   1110:             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
                   1111:             break;
                   1112:         default:
                   1113:         case IOC_RW:
                   1114:             thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
                   1115:             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
                   1116:             if (!is_error(ret)) {
                   1117:                 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
                   1118:             }
                   1119:             break;
                   1120:         }
                   1121:         break;
                   1122:     default:
                   1123:         gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
                   1124:         ret = -ENOSYS;
                   1125:         break;
                   1126:     }
                   1127:     return ret;
                   1128: }
                   1129: 
                   1130: bitmask_transtbl iflag_tbl[] = {
                   1131:         { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
                   1132:         { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
                   1133:         { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
                   1134:         { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
                   1135:         { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
                   1136:         { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
                   1137:         { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
                   1138:         { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
                   1139:         { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
                   1140:         { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
                   1141:         { TARGET_IXON, TARGET_IXON, IXON, IXON },
                   1142:         { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
                   1143:         { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
                   1144:         { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
                   1145:         { 0, 0, 0, 0 }
                   1146: };
                   1147: 
                   1148: bitmask_transtbl oflag_tbl[] = {
                   1149:        { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
                   1150:        { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
                   1151:        { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
                   1152:        { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
                   1153:        { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
                   1154:        { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
                   1155:        { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
                   1156:        { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
                   1157:        { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
                   1158:        { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
                   1159:        { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
                   1160:        { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
                   1161:        { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
                   1162:        { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
                   1163:        { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
                   1164:        { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
                   1165:        { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
                   1166:        { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
                   1167:        { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
                   1168:        { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
                   1169:        { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
                   1170:        { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
                   1171:        { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
                   1172:        { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
                   1173:        { 0, 0, 0, 0 }
                   1174: };
                   1175: 
                   1176: bitmask_transtbl cflag_tbl[] = {
                   1177:        { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
                   1178:        { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
                   1179:        { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
                   1180:        { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
                   1181:        { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
                   1182:        { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
                   1183:        { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
                   1184:        { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
                   1185:        { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
                   1186:        { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
                   1187:        { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
                   1188:        { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
                   1189:        { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
                   1190:        { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
                   1191:        { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
                   1192:        { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
                   1193:        { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
                   1194:        { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
                   1195:        { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
                   1196:        { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
                   1197:        { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
                   1198:        { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
                   1199:        { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
                   1200:        { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
                   1201:        { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
                   1202:        { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
                   1203:        { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
                   1204:        { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
                   1205:        { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
                   1206:        { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
                   1207:        { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
                   1208:        { 0, 0, 0, 0 }
                   1209: };
                   1210: 
                   1211: bitmask_transtbl lflag_tbl[] = {
                   1212:        { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
                   1213:        { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
                   1214:        { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
                   1215:        { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
                   1216:        { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
                   1217:        { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
                   1218:        { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
                   1219:        { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
                   1220:        { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
                   1221:        { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
                   1222:        { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
                   1223:        { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
                   1224:        { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
                   1225:        { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
                   1226:        { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
                   1227:        { 0, 0, 0, 0 }
                   1228: };
                   1229: 
                   1230: static void target_to_host_termios (void *dst, const void *src)
                   1231: {
                   1232:     struct host_termios *host = dst;
                   1233:     const struct target_termios *target = src;
                   1234:     
                   1235:     host->c_iflag = 
                   1236:         target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
                   1237:     host->c_oflag = 
                   1238:         target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
                   1239:     host->c_cflag = 
                   1240:         target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
                   1241:     host->c_lflag = 
                   1242:         target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
                   1243:     host->c_line = target->c_line;
                   1244:     
                   1245:     host->c_cc[VINTR] = target->c_cc[TARGET_VINTR]; 
                   1246:     host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT]; 
                   1247:     host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];       
                   1248:     host->c_cc[VKILL] = target->c_cc[TARGET_VKILL]; 
                   1249:     host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];   
                   1250:     host->c_cc[VTIME] = target->c_cc[TARGET_VTIME]; 
                   1251:     host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];   
                   1252:     host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC]; 
                   1253:     host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];       
                   1254:     host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP]; 
                   1255:     host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP]; 
                   1256:     host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];   
                   1257:     host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];   
                   1258:     host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];   
                   1259:     host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];     
                   1260:     host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];       
                   1261:     host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2]; 
                   1262: }
                   1263:   
                   1264: static void host_to_target_termios (void *dst, const void *src)
                   1265: {
                   1266:     struct target_termios *target = dst;
                   1267:     const struct host_termios *host = src;
                   1268: 
                   1269:     target->c_iflag = 
                   1270:         tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
                   1271:     target->c_oflag = 
                   1272:         tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
                   1273:     target->c_cflag = 
                   1274:         tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
                   1275:     target->c_lflag = 
                   1276:         tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
                   1277:     target->c_line = host->c_line;
                   1278:   
                   1279:     target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
                   1280:     target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
                   1281:     target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
                   1282:     target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
                   1283:     target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
                   1284:     target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
                   1285:     target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
                   1286:     target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
                   1287:     target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
                   1288:     target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
                   1289:     target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
                   1290:     target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
                   1291:     target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
                   1292:     target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
                   1293:     target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
                   1294:     target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
                   1295:     target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
                   1296: }
                   1297: 
                   1298: StructEntry struct_termios_def = {
                   1299:     .convert = { host_to_target_termios, target_to_host_termios },
                   1300:     .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
                   1301:     .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
                   1302: };
                   1303: 
                   1304: static bitmask_transtbl mmap_flags_tbl[] = {
                   1305:        { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
                   1306:        { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
                   1307:        { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
                   1308:        { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
                   1309:        { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
                   1310:        { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
                   1311:        { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
                   1312:        { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
                   1313:        { 0, 0, 0, 0 }
                   1314: };
                   1315: 
                   1316: static bitmask_transtbl fcntl_flags_tbl[] = {
                   1317:        { TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
                   1318:        { TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
                   1319:        { TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
                   1320:        { TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
                   1321:        { TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
                   1322:        { TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
                   1323:        { TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
                   1324:        { TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
                   1325:        { TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
                   1326:        { TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
                   1327:        { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
                   1328:        { TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
                   1329:        { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
                   1330: #if defined(O_DIRECT)
                   1331:        { TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
                   1332: #endif
                   1333:        { 0, 0, 0, 0 }
                   1334: };
                   1335: 
                   1336: #if defined(TARGET_I386)
                   1337: 
                   1338: /* NOTE: there is really one LDT for all the threads */
                   1339: uint8_t *ldt_table;
                   1340: 
                   1341: static int read_ldt(void *ptr, unsigned long bytecount)
                   1342: {
                   1343:     int size;
                   1344: 
                   1345:     if (!ldt_table)
                   1346:         return 0;
                   1347:     size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
                   1348:     if (size > bytecount)
                   1349:         size = bytecount;
                   1350:     memcpy(ptr, ldt_table, size);
                   1351:     return size;
                   1352: }
                   1353: 
                   1354: /* XXX: add locking support */
                   1355: static int write_ldt(CPUX86State *env, 
                   1356:                      void *ptr, unsigned long bytecount, int oldmode)
                   1357: {
                   1358:     struct target_modify_ldt_ldt_s ldt_info;
                   1359:     int seg_32bit, contents, read_exec_only, limit_in_pages;
                   1360:     int seg_not_present, useable;
                   1361:     uint32_t *lp, entry_1, entry_2;
                   1362: 
                   1363:     if (bytecount != sizeof(ldt_info))
                   1364:         return -EINVAL;
                   1365:     memcpy(&ldt_info, ptr, sizeof(ldt_info));
                   1366:     tswap32s(&ldt_info.entry_number);
                   1367:     tswapls((long *)&ldt_info.base_addr);
                   1368:     tswap32s(&ldt_info.limit);
                   1369:     tswap32s(&ldt_info.flags);
                   1370:     
                   1371:     if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
                   1372:         return -EINVAL;
                   1373:     seg_32bit = ldt_info.flags & 1;
                   1374:     contents = (ldt_info.flags >> 1) & 3;
                   1375:     read_exec_only = (ldt_info.flags >> 3) & 1;
                   1376:     limit_in_pages = (ldt_info.flags >> 4) & 1;
                   1377:     seg_not_present = (ldt_info.flags >> 5) & 1;
                   1378:     useable = (ldt_info.flags >> 6) & 1;
                   1379: 
                   1380:     if (contents == 3) {
                   1381:         if (oldmode)
                   1382:             return -EINVAL;
                   1383:         if (seg_not_present == 0)
                   1384:             return -EINVAL;
                   1385:     }
                   1386:     /* allocate the LDT */
                   1387:     if (!ldt_table) {
                   1388:         ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
                   1389:         if (!ldt_table)
                   1390:             return -ENOMEM;
                   1391:         memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
                   1392:         env->ldt.base = (long)ldt_table;
                   1393:         env->ldt.limit = 0xffff;
                   1394:     }
                   1395: 
                   1396:     /* NOTE: same code as Linux kernel */
                   1397:     /* Allow LDTs to be cleared by the user. */
                   1398:     if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
                   1399:         if (oldmode ||
                   1400:             (contents == 0             &&
                   1401:              read_exec_only == 1       &&
                   1402:              seg_32bit == 0            &&
                   1403:              limit_in_pages == 0       &&
                   1404:              seg_not_present == 1      &&
                   1405:              useable == 0 )) {
                   1406:             entry_1 = 0;
                   1407:             entry_2 = 0;
                   1408:             goto install;
                   1409:         }
                   1410:     }
                   1411:     
                   1412:     entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
                   1413:         (ldt_info.limit & 0x0ffff);
                   1414:     entry_2 = (ldt_info.base_addr & 0xff000000) |
                   1415:         ((ldt_info.base_addr & 0x00ff0000) >> 16) |
                   1416:         (ldt_info.limit & 0xf0000) |
                   1417:         ((read_exec_only ^ 1) << 9) |
                   1418:         (contents << 10) |
                   1419:         ((seg_not_present ^ 1) << 15) |
                   1420:         (seg_32bit << 22) |
                   1421:         (limit_in_pages << 23) |
                   1422:         0x7000;
                   1423:     if (!oldmode)
                   1424:         entry_2 |= (useable << 20);
                   1425: 
                   1426:     /* Install the new entry ...  */
                   1427: install:
                   1428:     lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
                   1429:     lp[0] = tswap32(entry_1);
                   1430:     lp[1] = tswap32(entry_2);
                   1431:     return 0;
                   1432: }
                   1433: 
                   1434: /* specific and weird i386 syscalls */
                   1435: int do_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
                   1436: {
                   1437:     int ret = -ENOSYS;
                   1438:     
                   1439:     switch (func) {
                   1440:     case 0:
                   1441:         ret = read_ldt(ptr, bytecount);
                   1442:         break;
                   1443:     case 1:
                   1444:         ret = write_ldt(env, ptr, bytecount, 1);
                   1445:         break;
                   1446:     case 0x11:
                   1447:         ret = write_ldt(env, ptr, bytecount, 0);
                   1448:         break;
                   1449:     }
                   1450:     return ret;
                   1451: }
                   1452: 
                   1453: #endif /* defined(TARGET_I386) */
                   1454: 
                   1455: /* this stack is the equivalent of the kernel stack associated with a
                   1456:    thread/process */
                   1457: #define NEW_STACK_SIZE 8192
                   1458: 
                   1459: static int clone_func(void *arg)
                   1460: {
                   1461:     CPUState *env = arg;
                   1462:     cpu_loop(env);
                   1463:     /* never exits */
                   1464:     return 0;
                   1465: }
                   1466: 
                   1467: int do_fork(CPUState *env, unsigned int flags, unsigned long newsp)
                   1468: {
                   1469:     int ret;
                   1470:     TaskState *ts;
                   1471:     uint8_t *new_stack;
                   1472:     CPUState *new_env;
                   1473:     
                   1474:     if (flags & CLONE_VM) {
                   1475:         ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
                   1476:         memset(ts, 0, sizeof(TaskState));
                   1477:         new_stack = ts->stack;
                   1478:         ts->used = 1;
                   1479:         /* add in task state list */
                   1480:         ts->next = first_task_state;
                   1481:         first_task_state = ts;
                   1482:         /* we create a new CPU instance. */
                   1483:         new_env = cpu_init();
                   1484:         memcpy(new_env, env, sizeof(CPUState));
                   1485: #if defined(TARGET_I386)
                   1486:         if (!newsp)
                   1487:             newsp = env->regs[R_ESP];
                   1488:         new_env->regs[R_ESP] = newsp;
                   1489:         new_env->regs[R_EAX] = 0;
                   1490: #elif defined(TARGET_ARM)
                   1491:         if (!newsp)
                   1492:             newsp = env->regs[13];
                   1493:         new_env->regs[13] = newsp;
                   1494:         new_env->regs[0] = 0;
                   1495: #elif defined(TARGET_SPARC)
                   1496:         printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
1.1.1.2 ! root     1497: #elif defined(TARGET_MIPS)
        !          1498:         printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
1.1       root     1499: #elif defined(TARGET_PPC)
                   1500:         if (!newsp)
                   1501:             newsp = env->gpr[1];
                   1502:         new_env->gpr[1] = newsp;
                   1503:         { 
                   1504:             int i;
                   1505:             for (i = 7; i < 32; i++)
                   1506:                 new_env->gpr[i] = 0;
                   1507:         }
                   1508: #else
                   1509: #error unsupported target CPU
                   1510: #endif
                   1511:         new_env->opaque = ts;
                   1512: #ifdef __ia64__
                   1513:         ret = clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
                   1514: #else
                   1515:        ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
                   1516: #endif
                   1517:     } else {
                   1518:         /* if no CLONE_VM, we consider it is a fork */
                   1519:         if ((flags & ~CSIGNAL) != 0)
                   1520:             return -EINVAL;
                   1521:         ret = fork();
                   1522:     }
                   1523:     return ret;
                   1524: }
                   1525: 
                   1526: static long do_fcntl(int fd, int cmd, unsigned long arg)
                   1527: {
                   1528:     struct flock fl;
                   1529:     struct target_flock *target_fl = (void *)arg;
                   1530:     long ret;
                   1531:     
                   1532:     switch(cmd) {
                   1533:     case TARGET_F_GETLK:
                   1534:         ret = fcntl(fd, cmd, &fl);
                   1535:         if (ret == 0) {
                   1536:             target_fl->l_type = tswap16(fl.l_type);
                   1537:             target_fl->l_whence = tswap16(fl.l_whence);
                   1538:             target_fl->l_start = tswapl(fl.l_start);
                   1539:             target_fl->l_len = tswapl(fl.l_len);
                   1540:             target_fl->l_pid = tswapl(fl.l_pid);
                   1541:         }
                   1542:         break;
                   1543:         
                   1544:     case TARGET_F_SETLK:
                   1545:     case TARGET_F_SETLKW:
                   1546:         fl.l_type = tswap16(target_fl->l_type);
                   1547:         fl.l_whence = tswap16(target_fl->l_whence);
                   1548:         fl.l_start = tswapl(target_fl->l_start);
                   1549:         fl.l_len = tswapl(target_fl->l_len);
                   1550:         fl.l_pid = tswapl(target_fl->l_pid);
                   1551:         ret = fcntl(fd, cmd, &fl);
                   1552:         break;
                   1553:         
                   1554:     case TARGET_F_GETLK64:
                   1555:     case TARGET_F_SETLK64:
                   1556:     case TARGET_F_SETLKW64:
                   1557:         ret = -1;
                   1558:         errno = EINVAL;
                   1559:         break;
                   1560: 
                   1561:     case F_GETFL:
                   1562:         ret = fcntl(fd, cmd, arg);
                   1563:         ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
                   1564:         break;
                   1565: 
                   1566:     case F_SETFL:
                   1567:         ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
                   1568:         break;
                   1569: 
                   1570:     default:
                   1571:         ret = fcntl(fd, cmd, arg);
                   1572:         break;
                   1573:     }
                   1574:     return ret;
                   1575: }
                   1576: 
                   1577: #ifdef USE_UID16
                   1578: 
                   1579: static inline int high2lowuid(int uid)
                   1580: {
                   1581:     if (uid > 65535)
                   1582:         return 65534;
                   1583:     else
                   1584:         return uid;
                   1585: }
                   1586: 
                   1587: static inline int high2lowgid(int gid)
                   1588: {
                   1589:     if (gid > 65535)
                   1590:         return 65534;
                   1591:     else
                   1592:         return gid;
                   1593: }
                   1594: 
                   1595: static inline int low2highuid(int uid)
                   1596: {
                   1597:     if ((int16_t)uid == -1)
                   1598:         return -1;
                   1599:     else
                   1600:         return uid;
                   1601: }
                   1602: 
                   1603: static inline int low2highgid(int gid)
                   1604: {
                   1605:     if ((int16_t)gid == -1)
                   1606:         return -1;
                   1607:     else
                   1608:         return gid;
                   1609: }
                   1610: 
                   1611: #endif /* USE_UID16 */
                   1612: 
                   1613: void syscall_init(void)
                   1614: {
                   1615:     IOCTLEntry *ie;
                   1616:     const argtype *arg_type;
                   1617:     int size;
                   1618: 
                   1619: #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def); 
                   1620: #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def); 
                   1621: #include "syscall_types.h"
                   1622: #undef STRUCT
                   1623: #undef STRUCT_SPECIAL
                   1624: 
                   1625:     /* we patch the ioctl size if necessary. We rely on the fact that
                   1626:        no ioctl has all the bits at '1' in the size field */
                   1627:     ie = ioctl_entries;
                   1628:     while (ie->target_cmd != 0) {
                   1629:         if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
                   1630:             TARGET_IOC_SIZEMASK) {
                   1631:             arg_type = ie->arg_type;
                   1632:             if (arg_type[0] != TYPE_PTR) {
                   1633:                 fprintf(stderr, "cannot patch size for ioctl 0x%x\n", 
                   1634:                         ie->target_cmd);
                   1635:                 exit(1);
                   1636:             }
                   1637:             arg_type++;
                   1638:             size = thunk_type_size(arg_type, 0);
                   1639:             ie->target_cmd = (ie->target_cmd & 
                   1640:                               ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
                   1641:                 (size << TARGET_IOC_SIZESHIFT);
                   1642:         }
                   1643:         /* automatic consistency check if same arch */
                   1644: #if defined(__i386__) && defined(TARGET_I386)
                   1645:         if (ie->target_cmd != ie->host_cmd) {
                   1646:             fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n", 
                   1647:                     ie->target_cmd, ie->host_cmd);
                   1648:         }
                   1649: #endif
                   1650:         ie++;
                   1651:     }
                   1652: }
                   1653: 
                   1654: long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3, 
                   1655:                 long arg4, long arg5, long arg6)
                   1656: {
                   1657:     long ret;
                   1658:     struct stat st;
1.1.1.2 ! root     1659:     struct statfs stfs;
1.1       root     1660:     
                   1661: #ifdef DEBUG
                   1662:     gemu_log("syscall %d", num);
                   1663: #endif
                   1664:     switch(num) {
                   1665:     case TARGET_NR_exit:
                   1666: #ifdef HAVE_GPROF
                   1667:         _mcleanup();
                   1668: #endif
                   1669:         gdb_exit(cpu_env, arg1);
                   1670:         /* XXX: should free thread stack and CPU env */
                   1671:         _exit(arg1);
                   1672:         ret = 0; /* avoid warning */
                   1673:         break;
                   1674:     case TARGET_NR_read:
                   1675:         page_unprotect_range((void *)arg2, arg3);
                   1676:         ret = get_errno(read(arg1, (void *)arg2, arg3));
                   1677:         break;
                   1678:     case TARGET_NR_write:
                   1679:         ret = get_errno(write(arg1, (void *)arg2, arg3));
                   1680:         break;
                   1681:     case TARGET_NR_open:
                   1682:         ret = get_errno(open(path((const char *)arg1),
                   1683:                              target_to_host_bitmask(arg2, fcntl_flags_tbl),
                   1684:                              arg3));
                   1685:         break;
                   1686:     case TARGET_NR_close:
                   1687:         ret = get_errno(close(arg1));
                   1688:         break;
                   1689:     case TARGET_NR_brk:
                   1690:         ret = do_brk((char *)arg1);
                   1691:         break;
                   1692:     case TARGET_NR_fork:
                   1693:         ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
                   1694:         break;
                   1695:     case TARGET_NR_waitpid:
                   1696:         {
                   1697:             int *status = (int *)arg2;
                   1698:             ret = get_errno(waitpid(arg1, status, arg3));
                   1699:             if (!is_error(ret) && status)
                   1700:                 tswapls((long *)&status);
                   1701:         }
                   1702:         break;
                   1703:     case TARGET_NR_creat:
                   1704:         ret = get_errno(creat((const char *)arg1, arg2));
                   1705:         break;
                   1706:     case TARGET_NR_link:
                   1707:         ret = get_errno(link((const char *)arg1, (const char *)arg2));
                   1708:         break;
                   1709:     case TARGET_NR_unlink:
                   1710:         ret = get_errno(unlink((const char *)arg1));
                   1711:         break;
                   1712:     case TARGET_NR_execve:
                   1713:         {
                   1714:             char **argp, **envp;
                   1715:             int argc, envc;
                   1716:             uint32_t *p;
                   1717:             char **q;
                   1718: 
                   1719:             argc = 0;
                   1720:             for (p = (void *)arg2; *p; p++)
                   1721:                 argc++;
                   1722:             envc = 0;
                   1723:             for (p = (void *)arg3; *p; p++)
                   1724:                 envc++;
                   1725: 
                   1726:             argp = alloca((argc + 1) * sizeof(void *));
                   1727:             envp = alloca((envc + 1) * sizeof(void *));
                   1728: 
                   1729:             for (p = (void *)arg2, q = argp; *p; p++, q++)
                   1730:                 *q = (void *)tswap32(*p);
                   1731:             *q = NULL;
                   1732: 
                   1733:             for (p = (void *)arg3, q = envp; *p; p++, q++)
                   1734:                 *q = (void *)tswap32(*p);
                   1735:             *q = NULL;
                   1736: 
                   1737:             ret = get_errno(execve((const char *)arg1, argp, envp));
                   1738:         }
                   1739:         break;
                   1740:     case TARGET_NR_chdir:
                   1741:         ret = get_errno(chdir((const char *)arg1));
                   1742:         break;
                   1743: #ifdef TARGET_NR_time
                   1744:     case TARGET_NR_time:
                   1745:         {
                   1746:             int *time_ptr = (int *)arg1;
                   1747:             ret = get_errno(time((time_t *)time_ptr));
                   1748:             if (!is_error(ret) && time_ptr)
                   1749:                 tswap32s(time_ptr);
                   1750:         }
                   1751:         break;
                   1752: #endif
                   1753:     case TARGET_NR_mknod:
                   1754:         ret = get_errno(mknod((const char *)arg1, arg2, arg3));
                   1755:         break;
                   1756:     case TARGET_NR_chmod:
                   1757:         ret = get_errno(chmod((const char *)arg1, arg2));
                   1758:         break;
                   1759: #ifdef TARGET_NR_break
                   1760:     case TARGET_NR_break:
                   1761:         goto unimplemented;
                   1762: #endif
                   1763: #ifdef TARGET_NR_oldstat
                   1764:     case TARGET_NR_oldstat:
                   1765:         goto unimplemented;
                   1766: #endif
                   1767:     case TARGET_NR_lseek:
                   1768:         ret = get_errno(lseek(arg1, arg2, arg3));
                   1769:         break;
                   1770:     case TARGET_NR_getpid:
                   1771:         ret = get_errno(getpid());
                   1772:         break;
                   1773:     case TARGET_NR_mount:
                   1774:         /* need to look at the data field */
                   1775:         goto unimplemented;
                   1776:     case TARGET_NR_umount:
                   1777:         ret = get_errno(umount((const char *)arg1));
                   1778:         break;
                   1779:     case TARGET_NR_stime:
                   1780:         {
                   1781:             int *time_ptr = (int *)arg1;
                   1782:             if (time_ptr)
                   1783:                 tswap32s(time_ptr);
                   1784:             ret = get_errno(stime((time_t *)time_ptr));
                   1785:         }
                   1786:         break;
                   1787:     case TARGET_NR_ptrace:
                   1788:         goto unimplemented;
                   1789:     case TARGET_NR_alarm:
                   1790:         ret = alarm(arg1);
                   1791:         break;
                   1792: #ifdef TARGET_NR_oldfstat
                   1793:     case TARGET_NR_oldfstat:
                   1794:         goto unimplemented;
                   1795: #endif
                   1796:     case TARGET_NR_pause:
                   1797:         ret = get_errno(pause());
                   1798:         break;
                   1799:     case TARGET_NR_utime:
                   1800:         {
                   1801:             struct utimbuf tbuf, *tbuf1;
                   1802:             struct target_utimbuf *target_tbuf = (void *)arg2;
                   1803:             if (target_tbuf) {
                   1804:                 get_user(tbuf.actime, &target_tbuf->actime);
                   1805:                 get_user(tbuf.modtime, &target_tbuf->modtime);
                   1806:                 tbuf1 = &tbuf;
                   1807:             } else {
                   1808:                 tbuf1 = NULL;
                   1809:             }
                   1810:             ret = get_errno(utime((const char *)arg1, tbuf1));
                   1811:         }
                   1812:         break;
                   1813:     case TARGET_NR_utimes:
                   1814:         {
                   1815:             struct target_timeval *target_tvp = (struct target_timeval *)arg2;
                   1816:             struct timeval *tvp, tv[2];
                   1817:             if (target_tvp) {
                   1818:                 target_to_host_timeval(&tv[0], &target_tvp[0]);
                   1819:                 target_to_host_timeval(&tv[1], &target_tvp[1]);
                   1820:                 tvp = tv;
                   1821:             } else {
                   1822:                 tvp = NULL;
                   1823:             }
                   1824:             ret = get_errno(utimes((const char *)arg1, tvp));
                   1825:         }
                   1826:         break;
                   1827: #ifdef TARGET_NR_stty
                   1828:     case TARGET_NR_stty:
                   1829:         goto unimplemented;
                   1830: #endif
                   1831: #ifdef TARGET_NR_gtty
                   1832:     case TARGET_NR_gtty:
                   1833:         goto unimplemented;
                   1834: #endif
                   1835:     case TARGET_NR_access:
                   1836:         ret = get_errno(access((const char *)arg1, arg2));
                   1837:         break;
                   1838:     case TARGET_NR_nice:
                   1839:         ret = get_errno(nice(arg1));
                   1840:         break;
                   1841: #ifdef TARGET_NR_ftime
                   1842:     case TARGET_NR_ftime:
                   1843:         goto unimplemented;
                   1844: #endif
                   1845:     case TARGET_NR_sync:
                   1846:         sync();
                   1847:         ret = 0;
                   1848:         break;
                   1849:     case TARGET_NR_kill:
                   1850:         ret = get_errno(kill(arg1, arg2));
                   1851:         break;
                   1852:     case TARGET_NR_rename:
                   1853:         ret = get_errno(rename((const char *)arg1, (const char *)arg2));
                   1854:         break;
                   1855:     case TARGET_NR_mkdir:
                   1856:         ret = get_errno(mkdir((const char *)arg1, arg2));
                   1857:         break;
                   1858:     case TARGET_NR_rmdir:
                   1859:         ret = get_errno(rmdir((const char *)arg1));
                   1860:         break;
                   1861:     case TARGET_NR_dup:
                   1862:         ret = get_errno(dup(arg1));
                   1863:         break;
                   1864:     case TARGET_NR_pipe:
                   1865:         {
                   1866:             int *pipe_ptr = (int *)arg1;
                   1867:             ret = get_errno(pipe(pipe_ptr));
                   1868:             if (!is_error(ret)) {
                   1869:                 tswap32s(&pipe_ptr[0]);
                   1870:                 tswap32s(&pipe_ptr[1]);
                   1871:             }
                   1872:         }
                   1873:         break;
                   1874:     case TARGET_NR_times:
                   1875:         {
                   1876:             struct target_tms *tmsp = (void *)arg1;
                   1877:             struct tms tms;
                   1878:             ret = get_errno(times(&tms));
                   1879:             if (tmsp) {
                   1880:                 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
                   1881:                 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
                   1882:                 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
                   1883:                 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
                   1884:             }
                   1885:             if (!is_error(ret))
                   1886:                 ret = host_to_target_clock_t(ret);
                   1887:         }
                   1888:         break;
                   1889: #ifdef TARGET_NR_prof
                   1890:     case TARGET_NR_prof:
                   1891:         goto unimplemented;
                   1892: #endif
                   1893:     case TARGET_NR_signal:
                   1894:         goto unimplemented;
                   1895: 
                   1896:     case TARGET_NR_acct:
                   1897:         goto unimplemented;
                   1898:     case TARGET_NR_umount2:
                   1899:         ret = get_errno(umount2((const char *)arg1, arg2));
                   1900:         break;
                   1901: #ifdef TARGET_NR_lock
                   1902:     case TARGET_NR_lock:
                   1903:         goto unimplemented;
                   1904: #endif
                   1905:     case TARGET_NR_ioctl:
                   1906:         ret = do_ioctl(arg1, arg2, arg3);
                   1907:         break;
                   1908:     case TARGET_NR_fcntl:
                   1909:         ret = get_errno(do_fcntl(arg1, arg2, arg3));
                   1910:         break;
                   1911: #ifdef TARGET_NR_mpx
                   1912:     case TARGET_NR_mpx:
                   1913:         goto unimplemented;
                   1914: #endif
                   1915:     case TARGET_NR_setpgid:
                   1916:         ret = get_errno(setpgid(arg1, arg2));
                   1917:         break;
                   1918: #ifdef TARGET_NR_ulimit
                   1919:     case TARGET_NR_ulimit:
                   1920:         goto unimplemented;
                   1921: #endif
                   1922: #ifdef TARGET_NR_oldolduname
                   1923:     case TARGET_NR_oldolduname:
                   1924:         goto unimplemented;
                   1925: #endif
                   1926:     case TARGET_NR_umask:
                   1927:         ret = get_errno(umask(arg1));
                   1928:         break;
                   1929:     case TARGET_NR_chroot:
                   1930:         ret = get_errno(chroot((const char *)arg1));
                   1931:         break;
                   1932:     case TARGET_NR_ustat:
                   1933:         goto unimplemented;
                   1934:     case TARGET_NR_dup2:
                   1935:         ret = get_errno(dup2(arg1, arg2));
                   1936:         break;
                   1937:     case TARGET_NR_getppid:
                   1938:         ret = get_errno(getppid());
                   1939:         break;
                   1940:     case TARGET_NR_getpgrp:
                   1941:         ret = get_errno(getpgrp());
                   1942:         break;
                   1943:     case TARGET_NR_setsid:
                   1944:         ret = get_errno(setsid());
                   1945:         break;
                   1946:     case TARGET_NR_sigaction:
                   1947:         {
                   1948:             struct target_old_sigaction *old_act = (void *)arg2;
                   1949:             struct target_old_sigaction *old_oact = (void *)arg3;
                   1950:             struct target_sigaction act, oact, *pact;
                   1951:             if (old_act) {
                   1952:                 act._sa_handler = old_act->_sa_handler;
                   1953:                 target_siginitset(&act.sa_mask, old_act->sa_mask);
                   1954:                 act.sa_flags = old_act->sa_flags;
                   1955:                 act.sa_restorer = old_act->sa_restorer;
                   1956:                 pact = &act;
                   1957:             } else {
                   1958:                 pact = NULL;
                   1959:             }
                   1960:             ret = get_errno(do_sigaction(arg1, pact, &oact));
                   1961:             if (!is_error(ret) && old_oact) {
                   1962:                 old_oact->_sa_handler = oact._sa_handler;
                   1963:                 old_oact->sa_mask = oact.sa_mask.sig[0];
                   1964:                 old_oact->sa_flags = oact.sa_flags;
                   1965:                 old_oact->sa_restorer = oact.sa_restorer;
                   1966:             }
                   1967:         }
                   1968:         break;
                   1969:     case TARGET_NR_rt_sigaction:
                   1970:         ret = get_errno(do_sigaction(arg1, (void *)arg2, (void *)arg3));
                   1971:         break;
                   1972:     case TARGET_NR_sgetmask:
                   1973:         {
                   1974:             sigset_t cur_set;
                   1975:             target_ulong target_set;
                   1976:             sigprocmask(0, NULL, &cur_set);
                   1977:             host_to_target_old_sigset(&target_set, &cur_set);
                   1978:             ret = target_set;
                   1979:         }
                   1980:         break;
                   1981:     case TARGET_NR_ssetmask:
                   1982:         {
                   1983:             sigset_t set, oset, cur_set;
                   1984:             target_ulong target_set = arg1;
                   1985:             sigprocmask(0, NULL, &cur_set);
                   1986:             target_to_host_old_sigset(&set, &target_set);
                   1987:             sigorset(&set, &set, &cur_set);
                   1988:             sigprocmask(SIG_SETMASK, &set, &oset);
                   1989:             host_to_target_old_sigset(&target_set, &oset);
                   1990:             ret = target_set;
                   1991:         }
                   1992:         break;
                   1993:     case TARGET_NR_sigprocmask:
                   1994:         {
                   1995:             int how = arg1;
                   1996:             sigset_t set, oldset, *set_ptr;
                   1997:             target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
                   1998:             
                   1999:             if (pset) {
                   2000:                 switch(how) {
                   2001:                 case TARGET_SIG_BLOCK:
                   2002:                     how = SIG_BLOCK;
                   2003:                     break;
                   2004:                 case TARGET_SIG_UNBLOCK:
                   2005:                     how = SIG_UNBLOCK;
                   2006:                     break;
                   2007:                 case TARGET_SIG_SETMASK:
                   2008:                     how = SIG_SETMASK;
                   2009:                     break;
                   2010:                 default:
                   2011:                     ret = -EINVAL;
                   2012:                     goto fail;
                   2013:                 }
                   2014:                 target_to_host_old_sigset(&set, pset);
                   2015:                 set_ptr = &set;
                   2016:             } else {
                   2017:                 how = 0;
                   2018:                 set_ptr = NULL;
                   2019:             }
                   2020:             ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
                   2021:             if (!is_error(ret) && poldset) {
                   2022:                 host_to_target_old_sigset(poldset, &oldset);
                   2023:             }
                   2024:         }
                   2025:         break;
                   2026:     case TARGET_NR_rt_sigprocmask:
                   2027:         {
                   2028:             int how = arg1;
                   2029:             sigset_t set, oldset, *set_ptr;
                   2030:             target_sigset_t *pset = (void *)arg2;
                   2031:             target_sigset_t *poldset = (void *)arg3;
                   2032:             
                   2033:             if (pset) {
                   2034:                 switch(how) {
                   2035:                 case TARGET_SIG_BLOCK:
                   2036:                     how = SIG_BLOCK;
                   2037:                     break;
                   2038:                 case TARGET_SIG_UNBLOCK:
                   2039:                     how = SIG_UNBLOCK;
                   2040:                     break;
                   2041:                 case TARGET_SIG_SETMASK:
                   2042:                     how = SIG_SETMASK;
                   2043:                     break;
                   2044:                 default:
                   2045:                     ret = -EINVAL;
                   2046:                     goto fail;
                   2047:                 }
                   2048:                 target_to_host_sigset(&set, pset);
                   2049:                 set_ptr = &set;
                   2050:             } else {
                   2051:                 how = 0;
                   2052:                 set_ptr = NULL;
                   2053:             }
                   2054:             ret = get_errno(sigprocmask(how, set_ptr, &oldset));
                   2055:             if (!is_error(ret) && poldset) {
                   2056:                 host_to_target_sigset(poldset, &oldset);
                   2057:             }
                   2058:         }
                   2059:         break;
                   2060:     case TARGET_NR_sigpending:
                   2061:         {
                   2062:             sigset_t set;
                   2063:             ret = get_errno(sigpending(&set));
                   2064:             if (!is_error(ret)) {
                   2065:                 host_to_target_old_sigset((target_ulong *)arg1, &set);
                   2066:             }
                   2067:         }
                   2068:         break;
                   2069:     case TARGET_NR_rt_sigpending:
                   2070:         {
                   2071:             sigset_t set;
                   2072:             ret = get_errno(sigpending(&set));
                   2073:             if (!is_error(ret)) {
                   2074:                 host_to_target_sigset((target_sigset_t *)arg1, &set);
                   2075:             }
                   2076:         }
                   2077:         break;
                   2078:     case TARGET_NR_sigsuspend:
                   2079:         {
                   2080:             sigset_t set;
                   2081:             target_to_host_old_sigset(&set, (target_ulong *)arg1);
                   2082:             ret = get_errno(sigsuspend(&set));
                   2083:         }
                   2084:         break;
                   2085:     case TARGET_NR_rt_sigsuspend:
                   2086:         {
                   2087:             sigset_t set;
                   2088:             target_to_host_sigset(&set, (target_sigset_t *)arg1);
                   2089:             ret = get_errno(sigsuspend(&set));
                   2090:         }
                   2091:         break;
                   2092:     case TARGET_NR_rt_sigtimedwait:
                   2093:         {
                   2094:             target_sigset_t *target_set = (void *)arg1;
                   2095:             target_siginfo_t *target_uinfo = (void *)arg2;
                   2096:             struct target_timespec *target_uts = (void *)arg3;
                   2097:             sigset_t set;
                   2098:             struct timespec uts, *puts;
                   2099:             siginfo_t uinfo;
                   2100:             
                   2101:             target_to_host_sigset(&set, target_set);
                   2102:             if (target_uts) {
                   2103:                 puts = &uts;
                   2104:                 puts->tv_sec = tswapl(target_uts->tv_sec);
                   2105:                 puts->tv_nsec = tswapl(target_uts->tv_nsec);
                   2106:             } else {
                   2107:                 puts = NULL;
                   2108:             }
                   2109:             ret = get_errno(sigtimedwait(&set, &uinfo, puts));
                   2110:             if (!is_error(ret) && target_uinfo) {
                   2111:                 host_to_target_siginfo(target_uinfo, &uinfo);
                   2112:             }
                   2113:         }
                   2114:         break;
                   2115:     case TARGET_NR_rt_sigqueueinfo:
                   2116:         {
                   2117:             siginfo_t uinfo;
                   2118:             target_to_host_siginfo(&uinfo, (target_siginfo_t *)arg3);
                   2119:             ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
                   2120:         }
                   2121:         break;
                   2122:     case TARGET_NR_sigreturn:
                   2123:         /* NOTE: ret is eax, so not transcoding must be done */
                   2124:         ret = do_sigreturn(cpu_env);
                   2125:         break;
                   2126:     case TARGET_NR_rt_sigreturn:
                   2127:         /* NOTE: ret is eax, so not transcoding must be done */
                   2128:         ret = do_rt_sigreturn(cpu_env);
                   2129:         break;
                   2130:     case TARGET_NR_sethostname:
                   2131:         ret = get_errno(sethostname((const char *)arg1, arg2));
                   2132:         break;
                   2133:     case TARGET_NR_setrlimit:
                   2134:         {
                   2135:             /* XXX: convert resource ? */
                   2136:             int resource = arg1;
                   2137:             struct target_rlimit *target_rlim = (void *)arg2;
                   2138:             struct rlimit rlim;
                   2139:             rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
                   2140:             rlim.rlim_max = tswapl(target_rlim->rlim_max);
                   2141:             ret = get_errno(setrlimit(resource, &rlim));
                   2142:         }
                   2143:         break;
                   2144:     case TARGET_NR_getrlimit:
                   2145:         {
                   2146:             /* XXX: convert resource ? */
                   2147:             int resource = arg1;
                   2148:             struct target_rlimit *target_rlim = (void *)arg2;
                   2149:             struct rlimit rlim;
                   2150:             
                   2151:             ret = get_errno(getrlimit(resource, &rlim));
                   2152:             if (!is_error(ret)) {
                   2153:                 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
                   2154:                 target_rlim->rlim_max = tswapl(rlim.rlim_max);
                   2155:             }
                   2156:         }
                   2157:         break;
                   2158:     case TARGET_NR_getrusage:
                   2159:         {
                   2160:             struct rusage rusage;
                   2161:             struct target_rusage *target_rusage = (void *)arg2;
                   2162:             ret = get_errno(getrusage(arg1, &rusage));
                   2163:             if (!is_error(ret)) {
                   2164:                 host_to_target_rusage(target_rusage, &rusage);
                   2165:             }
                   2166:         }
                   2167:         break;
                   2168:     case TARGET_NR_gettimeofday:
                   2169:         {
                   2170:             struct target_timeval *target_tv = (void *)arg1;
                   2171:             struct timeval tv;
                   2172:             ret = get_errno(gettimeofday(&tv, NULL));
                   2173:             if (!is_error(ret)) {
                   2174:                 host_to_target_timeval(target_tv, &tv);
                   2175:             }
                   2176:         }
                   2177:         break;
                   2178:     case TARGET_NR_settimeofday:
                   2179:         {
                   2180:             struct target_timeval *target_tv = (void *)arg1;
                   2181:             struct timeval tv;
                   2182:             target_to_host_timeval(&tv, target_tv);
                   2183:             ret = get_errno(settimeofday(&tv, NULL));
                   2184:         }
                   2185:         break;
1.1.1.2 ! root     2186: #ifdef TARGET_NR_select
1.1       root     2187:     case TARGET_NR_select:
                   2188:         {
                   2189:             struct target_sel_arg_struct *sel = (void *)arg1;
                   2190:             sel->n = tswapl(sel->n);
                   2191:             sel->inp = tswapl(sel->inp);
                   2192:             sel->outp = tswapl(sel->outp);
                   2193:             sel->exp = tswapl(sel->exp);
                   2194:             sel->tvp = tswapl(sel->tvp);
                   2195:             ret = do_select(sel->n, (void *)sel->inp, (void *)sel->outp,
                   2196:                             (void *)sel->exp, (void *)sel->tvp);
                   2197:         }
                   2198:         break;
1.1.1.2 ! root     2199: #endif
1.1       root     2200:     case TARGET_NR_symlink:
                   2201:         ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
                   2202:         break;
                   2203: #ifdef TARGET_NR_oldlstat
                   2204:     case TARGET_NR_oldlstat:
                   2205:         goto unimplemented;
                   2206: #endif
                   2207:     case TARGET_NR_readlink:
                   2208:         ret = get_errno(readlink(path((const char *)arg1), (char *)arg2, arg3));
                   2209:         break;
                   2210:     case TARGET_NR_uselib:
                   2211:         goto unimplemented;
                   2212:     case TARGET_NR_swapon:
                   2213:         ret = get_errno(swapon((const char *)arg1, arg2));
                   2214:         break;
                   2215:     case TARGET_NR_reboot:
                   2216:         goto unimplemented;
                   2217:     case TARGET_NR_readdir:
                   2218:         goto unimplemented;
                   2219:     case TARGET_NR_mmap:
                   2220: #if defined(TARGET_I386) || defined(TARGET_ARM)
                   2221:         {
                   2222:             uint32_t v1, v2, v3, v4, v5, v6, *vptr;
                   2223:             vptr = (uint32_t *)arg1;
                   2224:             v1 = tswap32(vptr[0]);
                   2225:             v2 = tswap32(vptr[1]);
                   2226:             v3 = tswap32(vptr[2]);
                   2227:             v4 = tswap32(vptr[3]);
                   2228:             v5 = tswap32(vptr[4]);
                   2229:             v6 = tswap32(vptr[5]);
                   2230:             ret = get_errno(target_mmap(v1, v2, v3, 
                   2231:                                         target_to_host_bitmask(v4, mmap_flags_tbl),
                   2232:                                         v5, v6));
                   2233:         }
                   2234: #else
                   2235:         ret = get_errno(target_mmap(arg1, arg2, arg3, 
                   2236:                                     target_to_host_bitmask(arg4, mmap_flags_tbl), 
                   2237:                                     arg5,
                   2238:                                     arg6));
                   2239: #endif
                   2240:         break;
                   2241: #ifdef TARGET_NR_mmap2
                   2242:     case TARGET_NR_mmap2:
                   2243: #if defined(TARGET_SPARC)
                   2244: #define MMAP_SHIFT 12
                   2245: #else
                   2246: #define MMAP_SHIFT TARGET_PAGE_BITS
                   2247: #endif
                   2248:         ret = get_errno(target_mmap(arg1, arg2, arg3, 
                   2249:                                     target_to_host_bitmask(arg4, mmap_flags_tbl), 
                   2250:                                     arg5,
                   2251:                                     arg6 << MMAP_SHIFT));
                   2252:         break;
                   2253: #endif
                   2254:     case TARGET_NR_munmap:
                   2255:         ret = get_errno(target_munmap(arg1, arg2));
                   2256:         break;
                   2257:     case TARGET_NR_mprotect:
                   2258:         ret = get_errno(target_mprotect(arg1, arg2, arg3));
                   2259:         break;
                   2260:     case TARGET_NR_mremap:
                   2261:         ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
                   2262:         break;
                   2263:     case TARGET_NR_msync:
                   2264:         ret = get_errno(msync((void *)arg1, arg2, arg3));
                   2265:         break;
                   2266:     case TARGET_NR_mlock:
                   2267:         ret = get_errno(mlock((void *)arg1, arg2));
                   2268:         break;
                   2269:     case TARGET_NR_munlock:
                   2270:         ret = get_errno(munlock((void *)arg1, arg2));
                   2271:         break;
                   2272:     case TARGET_NR_mlockall:
                   2273:         ret = get_errno(mlockall(arg1));
                   2274:         break;
                   2275:     case TARGET_NR_munlockall:
                   2276:         ret = get_errno(munlockall());
                   2277:         break;
                   2278:     case TARGET_NR_truncate:
                   2279:         ret = get_errno(truncate((const char *)arg1, arg2));
                   2280:         break;
                   2281:     case TARGET_NR_ftruncate:
                   2282:         ret = get_errno(ftruncate(arg1, arg2));
                   2283:         break;
                   2284:     case TARGET_NR_fchmod:
                   2285:         ret = get_errno(fchmod(arg1, arg2));
                   2286:         break;
                   2287:     case TARGET_NR_getpriority:
                   2288:         ret = get_errno(getpriority(arg1, arg2));
                   2289:         break;
                   2290:     case TARGET_NR_setpriority:
                   2291:         ret = get_errno(setpriority(arg1, arg2, arg3));
                   2292:         break;
                   2293: #ifdef TARGET_NR_profil
                   2294:     case TARGET_NR_profil:
                   2295:         goto unimplemented;
                   2296: #endif
                   2297:     case TARGET_NR_statfs:
1.1.1.2 ! root     2298:         ret = get_errno(statfs(path((const char *)arg1), &stfs));
1.1       root     2299:     convert_statfs:
                   2300:         if (!is_error(ret)) {
1.1.1.2 ! root     2301:             struct target_statfs *target_stfs = (void *)arg2;
        !          2302:             
        !          2303:             put_user(stfs.f_type, &target_stfs->f_type);
        !          2304:             put_user(stfs.f_bsize, &target_stfs->f_bsize);
        !          2305:             put_user(stfs.f_blocks, &target_stfs->f_blocks);
        !          2306:             put_user(stfs.f_bfree, &target_stfs->f_bfree);
        !          2307:             put_user(stfs.f_bavail, &target_stfs->f_bavail);
        !          2308:             put_user(stfs.f_files, &target_stfs->f_files);
        !          2309:             put_user(stfs.f_ffree, &target_stfs->f_ffree);
        !          2310:             put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid);
        !          2311:             put_user(stfs.f_namelen, &target_stfs->f_namelen);
1.1       root     2312:         }
                   2313:         break;
                   2314:     case TARGET_NR_fstatfs:
1.1.1.2 ! root     2315:         ret = get_errno(fstatfs(arg1, &stfs));
1.1       root     2316:         goto convert_statfs;
1.1.1.2 ! root     2317: #ifdef TARGET_NR_statfs64
        !          2318:     case TARGET_NR_statfs64:
        !          2319:         ret = get_errno(statfs(path((const char *)arg1), &stfs));
        !          2320:     convert_statfs64:
        !          2321:         if (!is_error(ret)) {
        !          2322:             struct target_statfs64 *target_stfs = (void *)arg3;
        !          2323: 
        !          2324:             put_user(stfs.f_type, &target_stfs->f_type);
        !          2325:             put_user(stfs.f_bsize, &target_stfs->f_bsize);
        !          2326:             put_user(stfs.f_blocks, &target_stfs->f_blocks);
        !          2327:             put_user(stfs.f_bfree, &target_stfs->f_bfree);
        !          2328:             put_user(stfs.f_bavail, &target_stfs->f_bavail);
        !          2329:             put_user(stfs.f_files, &target_stfs->f_files);
        !          2330:             put_user(stfs.f_ffree, &target_stfs->f_ffree);
        !          2331:             put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid);
        !          2332:             put_user(stfs.f_namelen, &target_stfs->f_namelen);
        !          2333:         }
        !          2334:         break;
        !          2335:     case TARGET_NR_fstatfs64:
        !          2336:         ret = get_errno(fstatfs(arg1, &stfs));
        !          2337:         goto convert_statfs64;
        !          2338: #endif
1.1       root     2339: #ifdef TARGET_NR_ioperm
                   2340:     case TARGET_NR_ioperm:
                   2341:         goto unimplemented;
                   2342: #endif
                   2343:     case TARGET_NR_socketcall:
                   2344:         ret = do_socketcall(arg1, (int32_t *)arg2);
                   2345:         break;
                   2346:     case TARGET_NR_syslog:
                   2347:         goto unimplemented;
                   2348:     case TARGET_NR_setitimer:
                   2349:         {
                   2350:             struct target_itimerval *target_value = (void *)arg2;
                   2351:             struct target_itimerval *target_ovalue = (void *)arg3;
                   2352:             struct itimerval value, ovalue, *pvalue;
                   2353: 
                   2354:             if (target_value) {
                   2355:                 pvalue = &value;
                   2356:                 target_to_host_timeval(&pvalue->it_interval, 
                   2357:                                        &target_value->it_interval);
                   2358:                 target_to_host_timeval(&pvalue->it_value, 
                   2359:                                        &target_value->it_value);
                   2360:             } else {
                   2361:                 pvalue = NULL;
                   2362:             }
                   2363:             ret = get_errno(setitimer(arg1, pvalue, &ovalue));
                   2364:             if (!is_error(ret) && target_ovalue) {
                   2365:                 host_to_target_timeval(&target_ovalue->it_interval, 
                   2366:                                        &ovalue.it_interval);
                   2367:                 host_to_target_timeval(&target_ovalue->it_value, 
                   2368:                                        &ovalue.it_value);
                   2369:             }
                   2370:         }
                   2371:         break;
                   2372:     case TARGET_NR_getitimer:
                   2373:         {
                   2374:             struct target_itimerval *target_value = (void *)arg2;
                   2375:             struct itimerval value;
                   2376:             
                   2377:             ret = get_errno(getitimer(arg1, &value));
                   2378:             if (!is_error(ret) && target_value) {
                   2379:                 host_to_target_timeval(&target_value->it_interval, 
                   2380:                                        &value.it_interval);
                   2381:                 host_to_target_timeval(&target_value->it_value, 
                   2382:                                        &value.it_value);
                   2383:             }
                   2384:         }
                   2385:         break;
                   2386:     case TARGET_NR_stat:
                   2387:         ret = get_errno(stat(path((const char *)arg1), &st));
                   2388:         goto do_stat;
                   2389:     case TARGET_NR_lstat:
                   2390:         ret = get_errno(lstat(path((const char *)arg1), &st));
                   2391:         goto do_stat;
                   2392:     case TARGET_NR_fstat:
                   2393:         {
                   2394:             ret = get_errno(fstat(arg1, &st));
                   2395:         do_stat:
                   2396:             if (!is_error(ret)) {
                   2397:                 struct target_stat *target_st = (void *)arg2;
                   2398:                 target_st->st_dev = tswap16(st.st_dev);
                   2399:                 target_st->st_ino = tswapl(st.st_ino);
                   2400: #if defined(TARGET_PPC)
                   2401:                 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
                   2402:                 target_st->st_uid = tswap32(st.st_uid);
                   2403:                 target_st->st_gid = tswap32(st.st_gid);
                   2404: #else
                   2405:                 target_st->st_mode = tswap16(st.st_mode);
                   2406:                 target_st->st_uid = tswap16(st.st_uid);
                   2407:                 target_st->st_gid = tswap16(st.st_gid);
                   2408: #endif
                   2409:                 target_st->st_nlink = tswap16(st.st_nlink);
                   2410:                 target_st->st_rdev = tswap16(st.st_rdev);
                   2411:                 target_st->st_size = tswapl(st.st_size);
                   2412:                 target_st->st_blksize = tswapl(st.st_blksize);
                   2413:                 target_st->st_blocks = tswapl(st.st_blocks);
                   2414:                 target_st->target_st_atime = tswapl(st.st_atime);
                   2415:                 target_st->target_st_mtime = tswapl(st.st_mtime);
                   2416:                 target_st->target_st_ctime = tswapl(st.st_ctime);
                   2417:             }
                   2418:         }
                   2419:         break;
                   2420: #ifdef TARGET_NR_olduname
                   2421:     case TARGET_NR_olduname:
                   2422:         goto unimplemented;
                   2423: #endif
                   2424: #ifdef TARGET_NR_iopl
                   2425:     case TARGET_NR_iopl:
                   2426:         goto unimplemented;
                   2427: #endif
                   2428:     case TARGET_NR_vhangup:
                   2429:         ret = get_errno(vhangup());
                   2430:         break;
                   2431: #ifdef TARGET_NR_idle
                   2432:     case TARGET_NR_idle:
                   2433:         goto unimplemented;
                   2434: #endif
                   2435: #ifdef TARGET_NR_syscall
                   2436:     case TARGET_NR_syscall:
                   2437:        ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
                   2438:        break;
                   2439: #endif
                   2440:     case TARGET_NR_wait4:
                   2441:         {
                   2442:             int status;
                   2443:             target_long *status_ptr = (void *)arg2;
                   2444:             struct rusage rusage, *rusage_ptr;
                   2445:             struct target_rusage *target_rusage = (void *)arg4;
                   2446:             if (target_rusage)
                   2447:                 rusage_ptr = &rusage;
                   2448:             else
                   2449:                 rusage_ptr = NULL;
                   2450:             ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
                   2451:             if (!is_error(ret)) {
                   2452:                 if (status_ptr)
                   2453:                     *status_ptr = tswap32(status);
                   2454:                 if (target_rusage) {
                   2455:                     host_to_target_rusage(target_rusage, &rusage);
                   2456:                 }
                   2457:             }
                   2458:         }
                   2459:         break;
                   2460:     case TARGET_NR_swapoff:
                   2461:         ret = get_errno(swapoff((const char *)arg1));
                   2462:         break;
                   2463:     case TARGET_NR_sysinfo:
                   2464:         {
                   2465:             struct target_sysinfo *target_value = (void *)arg1;
                   2466:             struct sysinfo value;
                   2467:             ret = get_errno(sysinfo(&value));
                   2468:             if (!is_error(ret) && target_value)
                   2469:             {
                   2470:                 __put_user(value.uptime, &target_value->uptime);
                   2471:                 __put_user(value.loads[0], &target_value->loads[0]);
                   2472:                 __put_user(value.loads[1], &target_value->loads[1]);
                   2473:                 __put_user(value.loads[2], &target_value->loads[2]);
                   2474:                 __put_user(value.totalram, &target_value->totalram);
                   2475:                 __put_user(value.freeram, &target_value->freeram);
                   2476:                 __put_user(value.sharedram, &target_value->sharedram);
                   2477:                 __put_user(value.bufferram, &target_value->bufferram);
                   2478:                 __put_user(value.totalswap, &target_value->totalswap);
                   2479:                 __put_user(value.freeswap, &target_value->freeswap);
                   2480:                 __put_user(value.procs, &target_value->procs);
                   2481:                 __put_user(value.totalhigh, &target_value->totalhigh);
                   2482:                 __put_user(value.freehigh, &target_value->freehigh);
                   2483:                 __put_user(value.mem_unit, &target_value->mem_unit);
                   2484:             }
                   2485:         }
                   2486:         break;
                   2487:     case TARGET_NR_ipc:
                   2488:        ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
                   2489:        break;
                   2490:     case TARGET_NR_fsync:
                   2491:         ret = get_errno(fsync(arg1));
                   2492:         break;
                   2493:     case TARGET_NR_clone:
                   2494:         ret = get_errno(do_fork(cpu_env, arg1, arg2));
                   2495:         break;
                   2496: #ifdef __NR_exit_group
                   2497:         /* new thread calls */
                   2498:     case TARGET_NR_exit_group:
                   2499:         gdb_exit(cpu_env, arg1);
                   2500:         ret = get_errno(exit_group(arg1));
                   2501:         break;
                   2502: #endif
                   2503:     case TARGET_NR_setdomainname:
                   2504:         ret = get_errno(setdomainname((const char *)arg1, arg2));
                   2505:         break;
                   2506:     case TARGET_NR_uname:
                   2507:         /* no need to transcode because we use the linux syscall */
                   2508:         {
                   2509:             struct new_utsname * buf;
                   2510:     
                   2511:             buf = (struct new_utsname *)arg1;
                   2512:             ret = get_errno(sys_uname(buf));
                   2513:             if (!is_error(ret)) {
                   2514:                 /* Overrite the native machine name with whatever is being
                   2515:                    emulated. */
                   2516:                 strcpy (buf->machine, UNAME_MACHINE);
                   2517:             }
                   2518:         }
                   2519:         break;
                   2520: #ifdef TARGET_I386
                   2521:     case TARGET_NR_modify_ldt:
                   2522:         ret = get_errno(do_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
                   2523:         break;
                   2524:     case TARGET_NR_vm86old:
                   2525:         goto unimplemented;
                   2526:     case TARGET_NR_vm86:
                   2527:         ret = do_vm86(cpu_env, arg1, (void *)arg2);
                   2528:         break;
                   2529: #endif
                   2530:     case TARGET_NR_adjtimex:
                   2531:         goto unimplemented;
                   2532:     case TARGET_NR_create_module:
                   2533:     case TARGET_NR_init_module:
                   2534:     case TARGET_NR_delete_module:
                   2535:     case TARGET_NR_get_kernel_syms:
                   2536:         goto unimplemented;
                   2537:     case TARGET_NR_quotactl:
                   2538:         goto unimplemented;
                   2539:     case TARGET_NR_getpgid:
                   2540:         ret = get_errno(getpgid(arg1));
                   2541:         break;
                   2542:     case TARGET_NR_fchdir:
                   2543:         ret = get_errno(fchdir(arg1));
                   2544:         break;
                   2545:     case TARGET_NR_bdflush:
                   2546:         goto unimplemented;
                   2547:     case TARGET_NR_sysfs:
                   2548:         goto unimplemented;
                   2549:     case TARGET_NR_personality:
                   2550:         ret = get_errno(personality(arg1));
                   2551:         break;
                   2552:     case TARGET_NR_afs_syscall:
                   2553:         goto unimplemented;
                   2554:     case TARGET_NR__llseek:
                   2555:         {
                   2556: #if defined (__x86_64__)
                   2557:             ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
                   2558:             *(int64_t *)arg4 = ret;
                   2559: #else
                   2560:             int64_t res;
                   2561:             ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
                   2562:             *(int64_t *)arg4 = tswap64(res);
                   2563: #endif
                   2564:         }
                   2565:         break;
                   2566:     case TARGET_NR_getdents:
                   2567: #if TARGET_LONG_SIZE != 4
                   2568: #warning not supported
                   2569: #elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
                   2570:         {
                   2571:             struct target_dirent *target_dirp = (void *)arg2;
                   2572:             struct dirent *dirp;
                   2573:             long count = arg3;
                   2574: 
                   2575:            dirp = malloc(count);
                   2576:            if (!dirp)
                   2577:                 return -ENOMEM;
                   2578:             
                   2579:             ret = get_errno(sys_getdents(arg1, dirp, count));
                   2580:             if (!is_error(ret)) {
                   2581:                 struct dirent *de;
                   2582:                struct target_dirent *tde;
                   2583:                 int len = ret;
                   2584:                 int reclen, treclen;
                   2585:                int count1, tnamelen;
                   2586: 
                   2587:                count1 = 0;
                   2588:                 de = dirp;
                   2589:                tde = target_dirp;
                   2590:                 while (len > 0) {
                   2591:                     reclen = de->d_reclen;
                   2592:                    treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
                   2593:                     tde->d_reclen = tswap16(treclen);
                   2594:                     tde->d_ino = tswapl(de->d_ino);
                   2595:                     tde->d_off = tswapl(de->d_off);
                   2596:                    tnamelen = treclen - (2 * sizeof(target_long) + 2);
                   2597:                    if (tnamelen > 256)
                   2598:                         tnamelen = 256;
                   2599:                     /* XXX: may not be correct */
                   2600:                    strncpy(tde->d_name, de->d_name, tnamelen);
                   2601:                     de = (struct dirent *)((char *)de + reclen);
                   2602:                     len -= reclen;
                   2603:                     tde = (struct dirent *)((char *)tde + treclen);
                   2604:                    count1 += treclen;
                   2605:                 }
                   2606:                ret = count1;
                   2607:             }
                   2608:            free(dirp);
                   2609:         }
                   2610: #else
                   2611:         {
                   2612:             struct dirent *dirp = (void *)arg2;
                   2613:             long count = arg3;
                   2614: 
                   2615:             ret = get_errno(sys_getdents(arg1, dirp, count));
                   2616:             if (!is_error(ret)) {
                   2617:                 struct dirent *de;
                   2618:                 int len = ret;
                   2619:                 int reclen;
                   2620:                 de = dirp;
                   2621:                 while (len > 0) {
                   2622:                     reclen = de->d_reclen;
                   2623:                     if (reclen > len)
                   2624:                         break;
                   2625:                     de->d_reclen = tswap16(reclen);
                   2626:                     tswapls(&de->d_ino);
                   2627:                     tswapls(&de->d_off);
                   2628:                     de = (struct dirent *)((char *)de + reclen);
                   2629:                     len -= reclen;
                   2630:                 }
                   2631:             }
                   2632:         }
                   2633: #endif
                   2634:         break;
                   2635: #ifdef TARGET_NR_getdents64
                   2636:     case TARGET_NR_getdents64:
                   2637:         {
                   2638:             struct dirent64 *dirp = (void *)arg2;
                   2639:             long count = arg3;
                   2640:             ret = get_errno(sys_getdents64(arg1, dirp, count));
                   2641:             if (!is_error(ret)) {
                   2642:                 struct dirent64 *de;
                   2643:                 int len = ret;
                   2644:                 int reclen;
                   2645:                 de = dirp;
                   2646:                 while (len > 0) {
                   2647:                     reclen = de->d_reclen;
                   2648:                     if (reclen > len)
                   2649:                         break;
                   2650:                     de->d_reclen = tswap16(reclen);
                   2651:                     tswap64s(&de->d_ino);
                   2652:                     tswap64s(&de->d_off);
                   2653:                     de = (struct dirent64 *)((char *)de + reclen);
                   2654:                     len -= reclen;
                   2655:                 }
                   2656:             }
                   2657:         }
                   2658:         break;
                   2659: #endif /* TARGET_NR_getdents64 */
                   2660:     case TARGET_NR__newselect:
                   2661:         ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4, 
                   2662:                         (void *)arg5);
                   2663:         break;
                   2664:     case TARGET_NR_poll:
                   2665:         {
                   2666:             struct target_pollfd *target_pfd = (void *)arg1;
                   2667:             unsigned int nfds = arg2;
                   2668:             int timeout = arg3;
                   2669:             struct pollfd *pfd;
                   2670:             unsigned int i;
                   2671: 
                   2672:             pfd = alloca(sizeof(struct pollfd) * nfds);
                   2673:             for(i = 0; i < nfds; i++) {
                   2674:                 pfd[i].fd = tswap32(target_pfd[i].fd);
                   2675:                 pfd[i].events = tswap16(target_pfd[i].events);
                   2676:             }
                   2677:             ret = get_errno(poll(pfd, nfds, timeout));
                   2678:             if (!is_error(ret)) {
                   2679:                 for(i = 0; i < nfds; i++) {
                   2680:                     target_pfd[i].revents = tswap16(pfd[i].revents);
                   2681:                 }
                   2682:             }
                   2683:         }
                   2684:         break;
                   2685:     case TARGET_NR_flock:
                   2686:         /* NOTE: the flock constant seems to be the same for every
                   2687:            Linux platform */
                   2688:         ret = get_errno(flock(arg1, arg2));
                   2689:         break;
                   2690:     case TARGET_NR_readv:
                   2691:         {
                   2692:             int count = arg3;
                   2693:             int i;
                   2694:             struct iovec *vec;
                   2695:             struct target_iovec *target_vec = (void *)arg2;
                   2696: 
                   2697:             vec = alloca(count * sizeof(struct iovec));
                   2698:             for(i = 0;i < count; i++) {
                   2699:                 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
                   2700:                 vec[i].iov_len = tswapl(target_vec[i].iov_len);
                   2701:             }
                   2702:             ret = get_errno(readv(arg1, vec, count));
                   2703:         }
                   2704:         break;
                   2705:     case TARGET_NR_writev:
                   2706:         {
                   2707:             int count = arg3;
                   2708:             int i;
                   2709:             struct iovec *vec;
                   2710:             struct target_iovec *target_vec = (void *)arg2;
                   2711: 
                   2712:             vec = alloca(count * sizeof(struct iovec));
                   2713:             for(i = 0;i < count; i++) {
                   2714:                 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
                   2715:                 vec[i].iov_len = tswapl(target_vec[i].iov_len);
                   2716:             }
                   2717:             ret = get_errno(writev(arg1, vec, count));
                   2718:         }
                   2719:         break;
                   2720:     case TARGET_NR_getsid:
                   2721:         ret = get_errno(getsid(arg1));
                   2722:         break;
                   2723:     case TARGET_NR_fdatasync:
                   2724:         ret = get_errno(fdatasync(arg1));
                   2725:         break;
                   2726:     case TARGET_NR__sysctl:
                   2727:         /* We don't implement this, but ENODIR is always a safe
                   2728:            return value. */
                   2729:         return -ENOTDIR;
                   2730:     case TARGET_NR_sched_setparam:
                   2731:         {
                   2732:             struct sched_param *target_schp = (void *)arg2;
                   2733:             struct sched_param schp;
                   2734:             schp.sched_priority = tswap32(target_schp->sched_priority);
                   2735:             ret = get_errno(sched_setparam(arg1, &schp));
                   2736:         }
                   2737:         break;
                   2738:     case TARGET_NR_sched_getparam:
                   2739:         {
                   2740:             struct sched_param *target_schp = (void *)arg2;
                   2741:             struct sched_param schp;
                   2742:             ret = get_errno(sched_getparam(arg1, &schp));
                   2743:             if (!is_error(ret)) {
                   2744:                 target_schp->sched_priority = tswap32(schp.sched_priority);
                   2745:             }
                   2746:         }
                   2747:         break;
                   2748:     case TARGET_NR_sched_setscheduler:
                   2749:         {
                   2750:             struct sched_param *target_schp = (void *)arg3;
                   2751:             struct sched_param schp;
                   2752:             schp.sched_priority = tswap32(target_schp->sched_priority);
                   2753:             ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
                   2754:         }
                   2755:         break;
                   2756:     case TARGET_NR_sched_getscheduler:
                   2757:         ret = get_errno(sched_getscheduler(arg1));
                   2758:         break;
                   2759:     case TARGET_NR_sched_yield:
                   2760:         ret = get_errno(sched_yield());
                   2761:         break;
                   2762:     case TARGET_NR_sched_get_priority_max:
                   2763:         ret = get_errno(sched_get_priority_max(arg1));
                   2764:         break;
                   2765:     case TARGET_NR_sched_get_priority_min:
                   2766:         ret = get_errno(sched_get_priority_min(arg1));
                   2767:         break;
                   2768:     case TARGET_NR_sched_rr_get_interval:
                   2769:         {
                   2770:             struct target_timespec *target_ts = (void *)arg2;
                   2771:             struct timespec ts;
                   2772:             ret = get_errno(sched_rr_get_interval(arg1, &ts));
                   2773:             if (!is_error(ret)) {
                   2774:                 target_ts->tv_sec = tswapl(ts.tv_sec);
                   2775:                 target_ts->tv_nsec = tswapl(ts.tv_nsec);
                   2776:             }
                   2777:         }
                   2778:         break;
                   2779:     case TARGET_NR_nanosleep:
                   2780:         {
                   2781:             struct target_timespec *target_req = (void *)arg1;
                   2782:             struct target_timespec *target_rem = (void *)arg2;
                   2783:             struct timespec req, rem;
                   2784:             req.tv_sec = tswapl(target_req->tv_sec);
                   2785:             req.tv_nsec = tswapl(target_req->tv_nsec);
                   2786:             ret = get_errno(nanosleep(&req, &rem));
                   2787:             if (is_error(ret) && target_rem) {
                   2788:                 target_rem->tv_sec = tswapl(rem.tv_sec);
                   2789:                 target_rem->tv_nsec = tswapl(rem.tv_nsec);
                   2790:             }
                   2791:         }
                   2792:         break;
                   2793:     case TARGET_NR_query_module:
                   2794:         goto unimplemented;
                   2795:     case TARGET_NR_nfsservctl:
                   2796:         goto unimplemented;
                   2797:     case TARGET_NR_prctl:
                   2798:         goto unimplemented;
                   2799: #ifdef TARGET_NR_pread
                   2800:     case TARGET_NR_pread:
                   2801:         page_unprotect_range((void *)arg2, arg3);
                   2802:         ret = get_errno(pread(arg1, (void *)arg2, arg3, arg4));
                   2803:         break;
                   2804:     case TARGET_NR_pwrite:
                   2805:         ret = get_errno(pwrite(arg1, (void *)arg2, arg3, arg4));
                   2806:         break;
                   2807: #endif
                   2808:     case TARGET_NR_getcwd:
                   2809:         ret = get_errno(sys_getcwd1((char *)arg1, arg2));
                   2810:         break;
                   2811:     case TARGET_NR_capget:
                   2812:         goto unimplemented;
                   2813:     case TARGET_NR_capset:
                   2814:         goto unimplemented;
                   2815:     case TARGET_NR_sigaltstack:
                   2816:         goto unimplemented;
                   2817:     case TARGET_NR_sendfile:
                   2818:         goto unimplemented;
                   2819: #ifdef TARGET_NR_getpmsg
                   2820:     case TARGET_NR_getpmsg:
                   2821:         goto unimplemented;
                   2822: #endif
                   2823: #ifdef TARGET_NR_putpmsg
                   2824:     case TARGET_NR_putpmsg:
                   2825:         goto unimplemented;
                   2826: #endif
1.1.1.2 ! root     2827: #ifdef TARGET_NR_vfork
1.1       root     2828:     case TARGET_NR_vfork:
                   2829:         ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
                   2830:         break;
1.1.1.2 ! root     2831: #endif
1.1       root     2832: #ifdef TARGET_NR_ugetrlimit
                   2833:     case TARGET_NR_ugetrlimit:
                   2834:     {
                   2835:        struct rlimit rlim;
                   2836:        ret = get_errno(getrlimit(arg1, &rlim));
                   2837:        if (!is_error(ret)) {
                   2838:            struct target_rlimit *target_rlim = (void *)arg2;
                   2839:            target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
                   2840:            target_rlim->rlim_max = tswapl(rlim.rlim_max);
                   2841:        }
                   2842:        break;
                   2843:     }
                   2844: #endif
                   2845: #ifdef TARGET_NR_truncate64
                   2846:     case TARGET_NR_truncate64:
                   2847:        ret = get_errno(truncate64((const char *)arg1, arg2));
                   2848:        break;
                   2849: #endif
                   2850: #ifdef TARGET_NR_ftruncate64
                   2851:     case TARGET_NR_ftruncate64:
                   2852:        ret = get_errno(ftruncate64(arg1, arg2));
                   2853:        break;
                   2854: #endif
                   2855: #ifdef TARGET_NR_stat64
                   2856:     case TARGET_NR_stat64:
                   2857:         ret = get_errno(stat(path((const char *)arg1), &st));
                   2858:         goto do_stat64;
                   2859: #endif
                   2860: #ifdef TARGET_NR_lstat64
                   2861:     case TARGET_NR_lstat64:
                   2862:         ret = get_errno(lstat(path((const char *)arg1), &st));
                   2863:         goto do_stat64;
                   2864: #endif
                   2865: #ifdef TARGET_NR_fstat64
                   2866:     case TARGET_NR_fstat64:
                   2867:         {
                   2868:             ret = get_errno(fstat(arg1, &st));
                   2869:         do_stat64:
                   2870:             if (!is_error(ret)) {
                   2871:                 struct target_stat64 *target_st = (void *)arg2;
                   2872:                 memset(target_st, 0, sizeof(struct target_stat64));
                   2873:                 put_user(st.st_dev, &target_st->st_dev);
                   2874:                 put_user(st.st_ino, &target_st->st_ino);
                   2875: #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
                   2876:                 put_user(st.st_ino, &target_st->__st_ino);
                   2877: #endif
                   2878:                 put_user(st.st_mode, &target_st->st_mode);
                   2879:                 put_user(st.st_nlink, &target_st->st_nlink);
                   2880:                 put_user(st.st_uid, &target_st->st_uid);
                   2881:                 put_user(st.st_gid, &target_st->st_gid);
                   2882:                 put_user(st.st_rdev, &target_st->st_rdev);
                   2883:                 /* XXX: better use of kernel struct */
                   2884:                 put_user(st.st_size, &target_st->st_size);
                   2885:                 put_user(st.st_blksize, &target_st->st_blksize);
                   2886:                 put_user(st.st_blocks, &target_st->st_blocks);
                   2887:                 put_user(st.st_atime, &target_st->target_st_atime);
                   2888:                 put_user(st.st_mtime, &target_st->target_st_mtime);
                   2889:                 put_user(st.st_ctime, &target_st->target_st_ctime);
                   2890:             }
                   2891:         }
                   2892:         break;
                   2893: #endif
                   2894: #ifdef USE_UID16
                   2895:     case TARGET_NR_lchown:
                   2896:         ret = get_errno(lchown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
                   2897:         break;
                   2898:     case TARGET_NR_getuid:
                   2899:         ret = get_errno(high2lowuid(getuid()));
                   2900:         break;
                   2901:     case TARGET_NR_getgid:
                   2902:         ret = get_errno(high2lowgid(getgid()));
                   2903:         break;
                   2904:     case TARGET_NR_geteuid:
                   2905:         ret = get_errno(high2lowuid(geteuid()));
                   2906:         break;
                   2907:     case TARGET_NR_getegid:
                   2908:         ret = get_errno(high2lowgid(getegid()));
                   2909:         break;
                   2910:     case TARGET_NR_setreuid:
                   2911:         ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
                   2912:         break;
                   2913:     case TARGET_NR_setregid:
                   2914:         ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
                   2915:         break;
                   2916:     case TARGET_NR_getgroups:
                   2917:         {
                   2918:             int gidsetsize = arg1;
                   2919:             uint16_t *target_grouplist = (void *)arg2;
                   2920:             gid_t *grouplist;
                   2921:             int i;
                   2922: 
                   2923:             grouplist = alloca(gidsetsize * sizeof(gid_t));
                   2924:             ret = get_errno(getgroups(gidsetsize, grouplist));
                   2925:             if (!is_error(ret)) {
                   2926:                 for(i = 0;i < gidsetsize; i++)
                   2927:                     target_grouplist[i] = tswap16(grouplist[i]);
                   2928:             }
                   2929:         }
                   2930:         break;
                   2931:     case TARGET_NR_setgroups:
                   2932:         {
                   2933:             int gidsetsize = arg1;
                   2934:             uint16_t *target_grouplist = (void *)arg2;
                   2935:             gid_t *grouplist;
                   2936:             int i;
                   2937: 
                   2938:             grouplist = alloca(gidsetsize * sizeof(gid_t));
                   2939:             for(i = 0;i < gidsetsize; i++)
                   2940:                 grouplist[i] = tswap16(target_grouplist[i]);
                   2941:             ret = get_errno(setgroups(gidsetsize, grouplist));
                   2942:         }
                   2943:         break;
                   2944:     case TARGET_NR_fchown:
                   2945:         ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
                   2946:         break;
                   2947: #ifdef TARGET_NR_setresuid
                   2948:     case TARGET_NR_setresuid:
                   2949:         ret = get_errno(setresuid(low2highuid(arg1), 
                   2950:                                   low2highuid(arg2), 
                   2951:                                   low2highuid(arg3)));
                   2952:         break;
                   2953: #endif
                   2954: #ifdef TARGET_NR_getresuid
                   2955:     case TARGET_NR_getresuid:
                   2956:         {
                   2957:             int ruid, euid, suid;
                   2958:             ret = get_errno(getresuid(&ruid, &euid, &suid));
                   2959:             if (!is_error(ret)) {
                   2960:                 *(uint16_t *)arg1 = tswap16(high2lowuid(ruid));
                   2961:                 *(uint16_t *)arg2 = tswap16(high2lowuid(euid));
                   2962:                 *(uint16_t *)arg3 = tswap16(high2lowuid(suid));
                   2963:             }
                   2964:         }
                   2965:         break;
                   2966: #endif
                   2967: #ifdef TARGET_NR_getresgid
                   2968:     case TARGET_NR_setresgid:
                   2969:         ret = get_errno(setresgid(low2highgid(arg1), 
                   2970:                                   low2highgid(arg2), 
                   2971:                                   low2highgid(arg3)));
                   2972:         break;
                   2973: #endif
                   2974: #ifdef TARGET_NR_getresgid
                   2975:     case TARGET_NR_getresgid:
                   2976:         {
                   2977:             int rgid, egid, sgid;
                   2978:             ret = get_errno(getresgid(&rgid, &egid, &sgid));
                   2979:             if (!is_error(ret)) {
                   2980:                 *(uint16_t *)arg1 = tswap16(high2lowgid(rgid));
                   2981:                 *(uint16_t *)arg2 = tswap16(high2lowgid(egid));
                   2982:                 *(uint16_t *)arg3 = tswap16(high2lowgid(sgid));
                   2983:             }
                   2984:         }
                   2985:         break;
                   2986: #endif
                   2987:     case TARGET_NR_chown:
                   2988:         ret = get_errno(chown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
                   2989:         break;
                   2990:     case TARGET_NR_setuid:
                   2991:         ret = get_errno(setuid(low2highuid(arg1)));
                   2992:         break;
                   2993:     case TARGET_NR_setgid:
                   2994:         ret = get_errno(setgid(low2highgid(arg1)));
                   2995:         break;
                   2996:     case TARGET_NR_setfsuid:
                   2997:         ret = get_errno(setfsuid(arg1));
                   2998:         break;
                   2999:     case TARGET_NR_setfsgid:
                   3000:         ret = get_errno(setfsgid(arg1));
                   3001:         break;
                   3002: #endif /* USE_UID16 */
                   3003: 
                   3004: #ifdef TARGET_NR_lchown32
                   3005:     case TARGET_NR_lchown32:
                   3006:         ret = get_errno(lchown((const char *)arg1, arg2, arg3));
                   3007:         break;
                   3008: #endif
                   3009: #ifdef TARGET_NR_getuid32
                   3010:     case TARGET_NR_getuid32:
                   3011:         ret = get_errno(getuid());
                   3012:         break;
                   3013: #endif
                   3014: #ifdef TARGET_NR_getgid32
                   3015:     case TARGET_NR_getgid32:
                   3016:         ret = get_errno(getgid());
                   3017:         break;
                   3018: #endif
                   3019: #ifdef TARGET_NR_geteuid32
                   3020:     case TARGET_NR_geteuid32:
                   3021:         ret = get_errno(geteuid());
                   3022:         break;
                   3023: #endif
                   3024: #ifdef TARGET_NR_getegid32
                   3025:     case TARGET_NR_getegid32:
                   3026:         ret = get_errno(getegid());
                   3027:         break;
                   3028: #endif
                   3029: #ifdef TARGET_NR_setreuid32
                   3030:     case TARGET_NR_setreuid32:
                   3031:         ret = get_errno(setreuid(arg1, arg2));
                   3032:         break;
                   3033: #endif
                   3034: #ifdef TARGET_NR_setregid32
                   3035:     case TARGET_NR_setregid32:
                   3036:         ret = get_errno(setregid(arg1, arg2));
                   3037:         break;
                   3038: #endif
                   3039: #ifdef TARGET_NR_getgroups32
                   3040:     case TARGET_NR_getgroups32:
                   3041:         {
                   3042:             int gidsetsize = arg1;
                   3043:             uint32_t *target_grouplist = (void *)arg2;
                   3044:             gid_t *grouplist;
                   3045:             int i;
                   3046: 
                   3047:             grouplist = alloca(gidsetsize * sizeof(gid_t));
                   3048:             ret = get_errno(getgroups(gidsetsize, grouplist));
                   3049:             if (!is_error(ret)) {
                   3050:                 for(i = 0;i < gidsetsize; i++)
                   3051:                     put_user(grouplist[i], &target_grouplist[i]);
                   3052:             }
                   3053:         }
                   3054:         break;
                   3055: #endif
                   3056: #ifdef TARGET_NR_setgroups32
                   3057:     case TARGET_NR_setgroups32:
                   3058:         {
                   3059:             int gidsetsize = arg1;
                   3060:             uint32_t *target_grouplist = (void *)arg2;
                   3061:             gid_t *grouplist;
                   3062:             int i;
                   3063:             
                   3064:             grouplist = alloca(gidsetsize * sizeof(gid_t));
                   3065:             for(i = 0;i < gidsetsize; i++)
                   3066:                 get_user(grouplist[i], &target_grouplist[i]);
                   3067:             ret = get_errno(setgroups(gidsetsize, grouplist));
                   3068:         }
                   3069:         break;
                   3070: #endif
                   3071: #ifdef TARGET_NR_fchown32
                   3072:     case TARGET_NR_fchown32:
                   3073:         ret = get_errno(fchown(arg1, arg2, arg3));
                   3074:         break;
                   3075: #endif
                   3076: #ifdef TARGET_NR_setresuid32
                   3077:     case TARGET_NR_setresuid32:
                   3078:         ret = get_errno(setresuid(arg1, arg2, arg3));
                   3079:         break;
                   3080: #endif
                   3081: #ifdef TARGET_NR_getresuid32
                   3082:     case TARGET_NR_getresuid32:
                   3083:         {
                   3084:             int ruid, euid, suid;
                   3085:             ret = get_errno(getresuid(&ruid, &euid, &suid));
                   3086:             if (!is_error(ret)) {
                   3087:                 *(uint32_t *)arg1 = tswap32(ruid);
                   3088:                 *(uint32_t *)arg2 = tswap32(euid);
                   3089:                 *(uint32_t *)arg3 = tswap32(suid);
                   3090:             }
                   3091:         }
                   3092:         break;
                   3093: #endif
                   3094: #ifdef TARGET_NR_setresgid32
                   3095:     case TARGET_NR_setresgid32:
                   3096:         ret = get_errno(setresgid(arg1, arg2, arg3));
                   3097:         break;
                   3098: #endif
                   3099: #ifdef TARGET_NR_getresgid32
                   3100:     case TARGET_NR_getresgid32:
                   3101:         {
                   3102:             int rgid, egid, sgid;
                   3103:             ret = get_errno(getresgid(&rgid, &egid, &sgid));
                   3104:             if (!is_error(ret)) {
                   3105:                 *(uint32_t *)arg1 = tswap32(rgid);
                   3106:                 *(uint32_t *)arg2 = tswap32(egid);
                   3107:                 *(uint32_t *)arg3 = tswap32(sgid);
                   3108:             }
                   3109:         }
                   3110:         break;
                   3111: #endif
                   3112: #ifdef TARGET_NR_chown32
                   3113:     case TARGET_NR_chown32:
                   3114:         ret = get_errno(chown((const char *)arg1, arg2, arg3));
                   3115:         break;
                   3116: #endif
                   3117: #ifdef TARGET_NR_setuid32
                   3118:     case TARGET_NR_setuid32:
                   3119:         ret = get_errno(setuid(arg1));
                   3120:         break;
                   3121: #endif
                   3122: #ifdef TARGET_NR_setgid32
                   3123:     case TARGET_NR_setgid32:
                   3124:         ret = get_errno(setgid(arg1));
                   3125:         break;
                   3126: #endif
                   3127: #ifdef TARGET_NR_setfsuid32
                   3128:     case TARGET_NR_setfsuid32:
                   3129:         ret = get_errno(setfsuid(arg1));
                   3130:         break;
                   3131: #endif
                   3132: #ifdef TARGET_NR_setfsgid32
                   3133:     case TARGET_NR_setfsgid32:
                   3134:         ret = get_errno(setfsgid(arg1));
                   3135:         break;
                   3136: #endif
                   3137: 
                   3138:     case TARGET_NR_pivot_root:
                   3139:         goto unimplemented;
                   3140: #ifdef TARGET_NR_mincore
                   3141:     case TARGET_NR_mincore:
                   3142:         goto unimplemented;
                   3143: #endif
                   3144: #ifdef TARGET_NR_madvise
                   3145:     case TARGET_NR_madvise:
                   3146:         goto unimplemented;
                   3147: #endif
                   3148: #if TARGET_LONG_BITS == 32
                   3149:     case TARGET_NR_fcntl64:
                   3150:     {
                   3151:        struct flock64 fl;
                   3152:        struct target_flock64 *target_fl = (void *)arg3;
                   3153: 
                   3154:         switch(arg2) {
                   3155:         case F_GETLK64:
                   3156:             ret = get_errno(fcntl(arg1, arg2, &fl));
                   3157:            if (ret == 0) {
                   3158:                target_fl->l_type = tswap16(fl.l_type);
                   3159:                target_fl->l_whence = tswap16(fl.l_whence);
                   3160:                target_fl->l_start = tswap64(fl.l_start);
                   3161:                target_fl->l_len = tswap64(fl.l_len);
                   3162:                target_fl->l_pid = tswapl(fl.l_pid);
                   3163:            }
                   3164:            break;
                   3165: 
                   3166:         case F_SETLK64:
                   3167:         case F_SETLKW64:
                   3168:            fl.l_type = tswap16(target_fl->l_type);
                   3169:            fl.l_whence = tswap16(target_fl->l_whence);
                   3170:            fl.l_start = tswap64(target_fl->l_start);
                   3171:            fl.l_len = tswap64(target_fl->l_len);
                   3172:            fl.l_pid = tswapl(target_fl->l_pid);
                   3173:             ret = get_errno(fcntl(arg1, arg2, &fl));
                   3174:            break;
                   3175:         default:
                   3176:             ret = get_errno(do_fcntl(arg1, arg2, arg3));
                   3177:             break;
                   3178:         }
                   3179:        break;
                   3180:     }
                   3181: #endif
                   3182: #ifdef TARGET_NR_security
                   3183:     case TARGET_NR_security:
                   3184:         goto unimplemented;
                   3185: #endif
                   3186: #ifdef TARGET_NR_getpagesize
                   3187:     case TARGET_NR_getpagesize:
                   3188:         ret = TARGET_PAGE_SIZE;
                   3189:         break;
                   3190: #endif
                   3191:     case TARGET_NR_gettid:
                   3192:         ret = get_errno(gettid());
                   3193:         break;
                   3194:     case TARGET_NR_readahead:
                   3195:         goto unimplemented;
                   3196: #ifdef TARGET_NR_setxattr
                   3197:     case TARGET_NR_setxattr:
                   3198:     case TARGET_NR_lsetxattr:
                   3199:     case TARGET_NR_fsetxattr:
                   3200:     case TARGET_NR_getxattr:
                   3201:     case TARGET_NR_lgetxattr:
                   3202:     case TARGET_NR_fgetxattr:
                   3203:     case TARGET_NR_listxattr:
                   3204:     case TARGET_NR_llistxattr:
                   3205:     case TARGET_NR_flistxattr:
                   3206:     case TARGET_NR_removexattr:
                   3207:     case TARGET_NR_lremovexattr:
                   3208:     case TARGET_NR_fremovexattr:
                   3209:         goto unimplemented_nowarn;
                   3210: #endif
                   3211: #ifdef TARGET_NR_set_thread_area
                   3212:     case TARGET_NR_set_thread_area:
                   3213:     case TARGET_NR_get_thread_area:
                   3214:         goto unimplemented_nowarn;
                   3215: #endif
                   3216:     default:
                   3217:     unimplemented:
                   3218:         gemu_log("qemu: Unsupported syscall: %d\n", num);
                   3219: #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_set_thread_area)
                   3220:     unimplemented_nowarn:
                   3221: #endif
                   3222:         ret = -ENOSYS;
                   3223:         break;
                   3224:     }
                   3225:  fail:
                   3226: #ifdef DEBUG
                   3227:     gemu_log(" = %ld\n", ret);
                   3228: #endif
                   3229:     return ret;
                   3230: }
                   3231: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.