Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ 274da6b2

History | View | Annotate | Download (93.6 kB)

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>
45
#include <utime.h>
46
//#include <sys/user.h>
47
#include <netinet/ip.h>
48
#include <netinet/tcp.h>
49

    
50
#define termios host_termios
51
#define winsize host_winsize
52
#define termio host_termio
53
#define sgttyb host_sgttyb /* same as target */
54
#define tchars host_tchars /* same as target */
55
#define ltchars host_ltchars /* same as target */
56

    
57
#include <linux/termios.h>
58
#include <linux/unistd.h>
59
#include <linux/utsname.h>
60
#include <linux/cdrom.h>
61
#include <linux/hdreg.h>
62
#include <linux/soundcard.h>
63
#include <linux/dirent.h>
64
#include <linux/kd.h>
65

    
66
#include "qemu.h"
67

    
68
//#define DEBUG
69

    
70
#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC)
71
/* 16 bit uid wrappers emulation */
72
#define USE_UID16
73
#endif
74

    
75
//#include <linux/msdos_fs.h>
76
#define        VFAT_IOCTL_READDIR_BOTH                _IOR('r', 1, struct dirent [2])
77
#define        VFAT_IOCTL_READDIR_SHORT        _IOR('r', 2, struct dirent [2])
78

    
79

    
80
#if defined(__powerpc__)
81
#undef __syscall_nr
82
#undef __sc_loadargs_0
83
#undef __sc_loadargs_1
84
#undef __sc_loadargs_2
85
#undef __sc_loadargs_3
86
#undef __sc_loadargs_4
87
#undef __sc_loadargs_5
88
#undef __sc_asm_input_0
89
#undef __sc_asm_input_1
90
#undef __sc_asm_input_2
91
#undef __sc_asm_input_3
92
#undef __sc_asm_input_4
93
#undef __sc_asm_input_5
94
#undef _syscall0
95
#undef _syscall1
96
#undef _syscall2
97
#undef _syscall3
98
#undef _syscall4
99
#undef _syscall5
100

    
101
/* need to redefine syscalls as Linux kernel defines are incorrect for
102
   the clobber list */
103
/* On powerpc a system call basically clobbers the same registers like a
104
 * function call, with the exception of LR (which is needed for the
105
 * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
106
 * an error return status).
107
 */
108

    
109
#define __syscall_nr(nr, type, name, args...)                                \
110
        unsigned long __sc_ret, __sc_err;                                \
111
        {                                                                \
112
                register unsigned long __sc_0  __asm__ ("r0");                \
113
                register unsigned long __sc_3  __asm__ ("r3");                \
114
                register unsigned long __sc_4  __asm__ ("r4");                \
115
                register unsigned long __sc_5  __asm__ ("r5");                \
116
                register unsigned long __sc_6  __asm__ ("r6");                \
117
                register unsigned long __sc_7  __asm__ ("r7");                \
118
                                                                        \
119
                __sc_loadargs_##nr(name, args);                                \
120
                __asm__ __volatile__                                        \
121
                        ("sc           \n\t"                                \
122
                         "mfcr %0      "                                \
123
                        : "=&r" (__sc_0),                                \
124
                          "=&r" (__sc_3),  "=&r" (__sc_4),                \
125
                          "=&r" (__sc_5),  "=&r" (__sc_6),                \
126
                          "=&r" (__sc_7)                                \
127
                        : __sc_asm_input_##nr                                \
128
                        : "cr0", "ctr", "memory",                        \
129
                          "r8", "r9", "r10","r11", "r12");                \
130
                __sc_ret = __sc_3;                                        \
131
                __sc_err = __sc_0;                                        \
132
        }                                                                \
133
        if (__sc_err & 0x10000000)                                        \
134
        {                                                                \
135
                errno = __sc_ret;                                        \
136
                __sc_ret = -1;                                                \
137
        }                                                                \
138
        return (type) __sc_ret
139

    
140
#define __sc_loadargs_0(name, dummy...)                                        \
141
        __sc_0 = __NR_##name
142
#define __sc_loadargs_1(name, arg1)                                        \
143
        __sc_loadargs_0(name);                                                \
144
        __sc_3 = (unsigned long) (arg1)
145
#define __sc_loadargs_2(name, arg1, arg2)                                \
146
        __sc_loadargs_1(name, arg1);                                        \
147
        __sc_4 = (unsigned long) (arg2)
148
#define __sc_loadargs_3(name, arg1, arg2, arg3)                                \
149
        __sc_loadargs_2(name, arg1, arg2);                                \
150
        __sc_5 = (unsigned long) (arg3)
151
#define __sc_loadargs_4(name, arg1, arg2, arg3, arg4)                        \
152
        __sc_loadargs_3(name, arg1, arg2, arg3);                        \
153
        __sc_6 = (unsigned long) (arg4)
154
#define __sc_loadargs_5(name, arg1, arg2, arg3, arg4, arg5)                \
155
        __sc_loadargs_4(name, arg1, arg2, arg3, arg4);                        \
156
        __sc_7 = (unsigned long) (arg5)
157

    
158
#define __sc_asm_input_0 "0" (__sc_0)
159
#define __sc_asm_input_1 __sc_asm_input_0, "1" (__sc_3)
160
#define __sc_asm_input_2 __sc_asm_input_1, "2" (__sc_4)
161
#define __sc_asm_input_3 __sc_asm_input_2, "3" (__sc_5)
162
#define __sc_asm_input_4 __sc_asm_input_3, "4" (__sc_6)
163
#define __sc_asm_input_5 __sc_asm_input_4, "5" (__sc_7)
164

    
165
#define _syscall0(type,name)                                                \
166
type name(void)                                                                \
167
{                                                                        \
168
        __syscall_nr(0, type, name);                                        \
169
}
170

    
171
#define _syscall1(type,name,type1,arg1)                                        \
172
type name(type1 arg1)                                                        \
173
{                                                                        \
174
        __syscall_nr(1, type, name, arg1);                                \
175
}
176

    
177
#define _syscall2(type,name,type1,arg1,type2,arg2)                        \
178
type name(type1 arg1, type2 arg2)                                        \
179
{                                                                        \
180
        __syscall_nr(2, type, name, arg1, arg2);                        \
181
}
182

    
183
#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)                \
184
type name(type1 arg1, type2 arg2, type3 arg3)                                \
185
{                                                                        \
186
        __syscall_nr(3, type, name, arg1, arg2, arg3);                        \
187
}
188

    
189
#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
190
type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4)                \
191
{                                                                        \
192
        __syscall_nr(4, type, name, arg1, arg2, arg3, arg4);                \
193
}
194

    
195
#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
196
type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)        \
197
{                                                                        \
198
        __syscall_nr(5, type, name, arg1, arg2, arg3, arg4, arg5);        \
199
}
200
#endif
201

    
202
#define __NR_sys_uname __NR_uname
203
#define __NR_sys_getcwd1 __NR_getcwd
204
#define __NR_sys_statfs __NR_statfs
205
#define __NR_sys_fstatfs __NR_fstatfs
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
_syscall2(int,sys_statfs,const char *,path,struct kernel_statfs *,buf)
228
_syscall2(int,sys_fstatfs,int,fd,struct kernel_statfs *,buf)
229
_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
230
#ifdef __NR_exit_group
231
_syscall1(int,exit_group,int,error_code)
232
#endif
233

    
234
extern int personality(int);
235
extern int flock(int, int);
236
extern int setfsuid(int);
237
extern int setfsgid(int);
238
extern int setresuid(uid_t, uid_t, uid_t);
239
extern int getresuid(uid_t *, uid_t *, uid_t *);
240
extern int setresgid(gid_t, gid_t, gid_t);
241
extern int getresgid(gid_t *, gid_t *, gid_t *);
242
extern int setgroups(int, gid_t *);
243

    
244
static inline long get_errno(long ret)
245
{
246
    if (ret == -1)
247
        return -errno;
248
    else
249
        return ret;
250
}
251

    
252
static inline int is_error(long ret)
253
{
254
    return (unsigned long)ret >= (unsigned long)(-4096);
255
}
256

    
257
static char *target_brk;
258
static char *target_original_brk;
259

    
260
void target_set_brk(char *new_brk)
261
{
262
    target_brk = new_brk;
263
    target_original_brk = new_brk;
264
}
265

    
266
static long do_brk(char *new_brk)
267
{
268
    char *brk_page;
269
    long mapped_addr;
270
    int        new_alloc_size;
271

    
272
    if (!new_brk)
273
        return (long)target_brk;
274
    if (new_brk < target_original_brk)
275
        return -ENOMEM;
276
    
277
    brk_page = (char *)HOST_PAGE_ALIGN((unsigned long)target_brk);
278

    
279
    /* If the new brk is less than this, set it and we're done... */
280
    if (new_brk < brk_page) {
281
        target_brk = new_brk;
282
            return (long)target_brk;
283
    }
284

    
285
    /* We need to allocate more memory after the brk... */
286
    new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
287
    mapped_addr = get_errno(target_mmap((unsigned long)brk_page, new_alloc_size, 
288
                                        PROT_READ|PROT_WRITE,
289
                                        MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
290
    if (is_error(mapped_addr)) {
291
        return mapped_addr;
292
    } else {
293
        target_brk = new_brk;
294
            return (long)target_brk;
295
    }
296
}
297

    
298
static inline fd_set *target_to_host_fds(fd_set *fds, 
299
                                         target_long *target_fds, int n)
300
{
301
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
302
    return (fd_set *)target_fds;
303
#else
304
    int i, b;
305
    if (target_fds) {
306
        FD_ZERO(fds);
307
        for(i = 0;i < n; i++) {
308
            b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
309
                 (i & (TARGET_LONG_BITS - 1))) & 1;
310
            if (b)
311
                FD_SET(i, fds);
312
        }
313
        return fds;
314
    } else {
315
        return NULL;
316
    }
317
#endif
318
}
319

    
320
static inline void host_to_target_fds(target_long *target_fds, 
321
                                      fd_set *fds, int n)
322
{
323
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
324
    /* nothing to do */
325
#else
326
    int i, nw, j, k;
327
    target_long v;
328

    
329
    if (target_fds) {
330
        nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
331
        k = 0;
332
        for(i = 0;i < nw; i++) {
333
            v = 0;
334
            for(j = 0; j < TARGET_LONG_BITS; j++) {
335
                v |= ((FD_ISSET(k, fds) != 0) << j);
336
                k++;
337
            }
338
            target_fds[i] = tswapl(v);
339
        }
340
    }
341
#endif
342
}
343

    
344
#if defined(__alpha__)
345
#define HOST_HZ 1024
346
#else
347
#define HOST_HZ 100
348
#endif
349

    
350
static inline long host_to_target_clock_t(long ticks)
351
{
352
#if HOST_HZ == TARGET_HZ
353
    return ticks;
354
#else
355
    return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
356
#endif
357
}
358

    
359
static inline void host_to_target_rusage(struct target_rusage *target_rusage, 
360
                                         const struct rusage *rusage)
361
{
362
    target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
363
    target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
364
    target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
365
    target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
366
    target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
367
    target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
368
    target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
369
    target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
370
    target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
371
    target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
372
    target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
373
    target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
374
    target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
375
    target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
376
    target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
377
    target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
378
    target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
379
    target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
380
}
381

    
382
static inline void target_to_host_timeval(struct timeval *tv, 
383
                                          const struct target_timeval *target_tv)
384
{
385
    tv->tv_sec = tswapl(target_tv->tv_sec);
386
    tv->tv_usec = tswapl(target_tv->tv_usec);
387
}
388

    
389
static inline void host_to_target_timeval(struct target_timeval *target_tv, 
390
                                          const struct timeval *tv)
391
{
392
    target_tv->tv_sec = tswapl(tv->tv_sec);
393
    target_tv->tv_usec = tswapl(tv->tv_usec);
394
}
395

    
396

    
397
static long do_select(long n, 
398
                      target_long *target_rfds, target_long *target_wfds, 
399
                      target_long *target_efds, struct target_timeval *target_tv)
400
{
401
    fd_set rfds, wfds, efds;
402
    fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
403
    struct timeval tv, *tv_ptr;
404
    long ret;
405

    
406
    rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
407
    wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
408
    efds_ptr = target_to_host_fds(&efds, target_efds, n);
409
            
410
    if (target_tv) {
411
        target_to_host_timeval(&tv, target_tv);
412
        tv_ptr = &tv;
413
    } else {
414
        tv_ptr = NULL;
415
    }
416
    ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
417
    if (!is_error(ret)) {
418
        host_to_target_fds(target_rfds, rfds_ptr, n);
419
        host_to_target_fds(target_wfds, wfds_ptr, n);
420
        host_to_target_fds(target_efds, efds_ptr, n);
421

    
422
        if (target_tv) {
423
            host_to_target_timeval(target_tv, &tv);
424
        }
425
    }
426
    return ret;
427
}
428

    
429
static inline void target_to_host_sockaddr(struct sockaddr *addr,
430
                                           struct target_sockaddr *target_addr,
431
                                           socklen_t len)
432
{
433
    memcpy(addr, target_addr, len);
434
    addr->sa_family = tswap16(target_addr->sa_family);
435
}
436

    
437
static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
438
                                           struct sockaddr *addr,
439
                                           socklen_t len)
440
{
441
    memcpy(target_addr, addr, len);
442
    target_addr->sa_family = tswap16(addr->sa_family);
443
}
444

    
445
static inline void target_to_host_cmsg(struct msghdr *msgh,
446
                                       struct target_msghdr *target_msgh)
447
{
448
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
449
    struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
450
    socklen_t space = 0;
451

    
452
    while (cmsg && target_cmsg) {
453
        void *data = CMSG_DATA(cmsg);
454
        void *target_data = TARGET_CMSG_DATA(target_cmsg);
455

    
456
        int len = tswapl(target_cmsg->cmsg_len) 
457
                  - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
458

    
459
        space += CMSG_SPACE(len);
460
        if (space > msgh->msg_controllen) {
461
            space -= CMSG_SPACE(len);
462
            gemu_log("Host cmsg overflow");
463
            break;
464
        }
465

    
466
        cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
467
        cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
468
        cmsg->cmsg_len = CMSG_LEN(len);
469

    
470
        if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
471
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
472
            memcpy(data, target_data, len);
473
        } else {
474
            int *fd = (int *)data;
475
            int *target_fd = (int *)target_data;
476
            int i, numfds = len / sizeof(int);
477

    
478
            for (i = 0; i < numfds; i++)
479
                fd[i] = tswap32(target_fd[i]);
480
        }
481

    
482
        cmsg = CMSG_NXTHDR(msgh, cmsg);
483
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
484
    }
485

    
486
    msgh->msg_controllen = space;
487
}
488

    
489
static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
490
                                       struct msghdr *msgh)
491
{
492
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
493
    struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
494
    socklen_t space = 0;
495

    
496
    while (cmsg && target_cmsg) {
497
        void *data = CMSG_DATA(cmsg);
498
        void *target_data = TARGET_CMSG_DATA(target_cmsg);
499

    
500
        int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
501

    
502
        space += TARGET_CMSG_SPACE(len);
503
        if (space > tswapl(target_msgh->msg_controllen)) {
504
            space -= TARGET_CMSG_SPACE(len);
505
            gemu_log("Target cmsg overflow");
506
            break;
507
        }
508

    
509
        target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
510
        target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
511
        target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
512

    
513
        if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
514
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
515
            memcpy(target_data, data, len);
516
        } else {
517
            int *fd = (int *)data;
518
            int *target_fd = (int *)target_data;
519
            int i, numfds = len / sizeof(int);
520

    
521
            for (i = 0; i < numfds; i++)
522
                target_fd[i] = tswap32(fd[i]);
523
        }
524

    
525
        cmsg = CMSG_NXTHDR(msgh, cmsg);
526
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
527
    }
528

    
529
    msgh->msg_controllen = tswapl(space);
530
}
531

    
532
static long do_setsockopt(int sockfd, int level, int optname, 
533
                          void *optval, socklen_t optlen)
534
{
535
    int val, ret;
536
            
537
    switch(level) {
538
    case SOL_TCP:
539
        /* TCP options all take an 'int' value.  */
540
        if (optlen < sizeof(uint32_t))
541
            return -EINVAL;
542
        
543
        if (get_user(val, (uint32_t *)optval))
544
            return -EFAULT;
545
        ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
546
        break;
547
    case SOL_IP:
548
        switch(optname) {
549
        case IP_HDRINCL:
550
            val = 0;
551
            if (optlen >= sizeof(uint32_t)) {
552
                if (get_user(val, (uint32_t *)optval))
553
                    return -EFAULT;
554
            } else if (optlen >= 1) {
555
                if (get_user(val, (uint8_t *)optval))
556
                    return -EFAULT;
557
            }
558
            ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
559
            break;
560
        default:
561
            goto unimplemented;
562
        }
563
        break;
564
    case SOL_SOCKET:
565
        switch (optname) {
566
            /* Options with 'int' argument.  */
567
        case SO_DEBUG:
568
        case SO_REUSEADDR:
569
        case SO_TYPE:
570
        case SO_ERROR:
571
        case SO_DONTROUTE:
572
        case SO_BROADCAST:
573
        case SO_SNDBUF:
574
        case SO_RCVBUF:
575
        case SO_KEEPALIVE:
576
        case SO_OOBINLINE:
577
        case SO_NO_CHECK:
578
        case SO_PRIORITY:
579
        case SO_BSDCOMPAT:
580
        case SO_PASSCRED:
581
        case SO_TIMESTAMP:
582
        case SO_RCVLOWAT:
583
        case SO_RCVTIMEO:
584
        case SO_SNDTIMEO:
585
            if (optlen < sizeof(uint32_t))
586
                return -EINVAL;
587
            if (get_user(val, (uint32_t *)optval))
588
                return -EFAULT;
589
            ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
590
            break;
591
        default:
592
            goto unimplemented;
593
        }
594
        break;
595
    default:
596
    unimplemented:
597
        gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
598
        ret = -ENOSYS;
599
    }
600
    return ret;
601
}
602

    
603
static long do_getsockopt(int sockfd, int level, int optname, 
604
                          void *optval, socklen_t *optlen)
605
{
606
    int len, lv, val, ret;
607

    
608
    switch(level) {
609
    case SOL_SOCKET:
610
        switch (optname) {
611
        case SO_LINGER:
612
        case SO_RCVTIMEO:
613
        case SO_SNDTIMEO:
614
        case SO_PEERCRED:
615
        case SO_PEERNAME:
616
            /* These don't just return a single integer */
617
            goto unimplemented;
618
        default:
619
            if (get_user(len, optlen))
620
                return -EFAULT;
621
            if (len < 0)
622
                return -EINVAL;
623
            lv = sizeof(int);
624
            ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
625
            if (ret < 0)
626
                return ret;
627
            val = tswap32(val);
628
            if (len > lv)
629
                len = lv;
630
            if (copy_to_user(optval, &val, len))
631
                return -EFAULT;
632
            if (put_user(len, optlen))
633
                return -EFAULT;
634
            break;
635
        }
636
        break;
637
    default:
638
    unimplemented:
639
        gemu_log("getsockopt level=%d optname=%d not yet supported\n",
640
                 level, optname);
641
        ret = -ENOSYS;
642
        break;
643
    }
644
    return ret;
645
}
646

    
647
static long do_socketcall(int num, int32_t *vptr)
648
{
649
    long ret;
650

    
651
    switch(num) {
652
    case SOCKOP_socket:
653
        {
654
            int domain = tswap32(vptr[0]);
655
            int type = tswap32(vptr[1]);
656
            int protocol = tswap32(vptr[2]);
657

    
658
            ret = get_errno(socket(domain, type, protocol));
659
        }
660
        break;
661
    case SOCKOP_bind:
662
        {
663
            int sockfd = tswap32(vptr[0]);
664
            void *target_addr = (void *)tswap32(vptr[1]);
665
            socklen_t addrlen = tswap32(vptr[2]);
666
            void *addr = alloca(addrlen);
667

    
668
            target_to_host_sockaddr(addr, target_addr, addrlen);
669
            ret = get_errno(bind(sockfd, addr, addrlen));
670
        }
671
        break;
672
    case SOCKOP_connect:
673
        {
674
            int sockfd = tswap32(vptr[0]);
675
            void *target_addr = (void *)tswap32(vptr[1]);
676
            socklen_t addrlen = tswap32(vptr[2]);
677
            void *addr = alloca(addrlen);
678

    
679
            target_to_host_sockaddr(addr, target_addr, addrlen);
680
            ret = get_errno(connect(sockfd, addr, addrlen));
681
        }
682
        break;
683
    case SOCKOP_listen:
684
        {
685
            int sockfd = tswap32(vptr[0]);
686
            int backlog = tswap32(vptr[1]);
687

    
688
            ret = get_errno(listen(sockfd, backlog));
689
        }
690
        break;
691
    case SOCKOP_accept:
692
        {
693
            int sockfd = tswap32(vptr[0]);
694
            void *target_addr = (void *)tswap32(vptr[1]);
695
            uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
696
            socklen_t addrlen = tswap32(*target_addrlen);
697
            void *addr = alloca(addrlen);
698

    
699
            ret = get_errno(accept(sockfd, addr, &addrlen));
700
            if (!is_error(ret)) {
701
                host_to_target_sockaddr(target_addr, addr, addrlen);
702
                *target_addrlen = tswap32(addrlen);
703
            }
704
        }
705
        break;
706
    case SOCKOP_getsockname:
707
        {
708
            int sockfd = tswap32(vptr[0]);
709
            void *target_addr = (void *)tswap32(vptr[1]);
710
            uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
711
            socklen_t addrlen = tswap32(*target_addrlen);
712
            void *addr = alloca(addrlen);
713

    
714
            ret = get_errno(getsockname(sockfd, addr, &addrlen));
715
            if (!is_error(ret)) {
716
                host_to_target_sockaddr(target_addr, addr, addrlen);
717
                *target_addrlen = tswap32(addrlen);
718
            }
719
        }
720
        break;
721
    case SOCKOP_getpeername:
722
        {
723
            int sockfd = tswap32(vptr[0]);
724
            void *target_addr = (void *)tswap32(vptr[1]);
725
            uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
726
            socklen_t addrlen = tswap32(*target_addrlen);
727
            void *addr = alloca(addrlen);
728

    
729
            ret = get_errno(getpeername(sockfd, addr, &addrlen));
730
            if (!is_error(ret)) {
731
                host_to_target_sockaddr(target_addr, addr, addrlen);
732
                *target_addrlen = tswap32(addrlen);
733
            }
734
        }
735
        break;
736
    case SOCKOP_socketpair:
737
        {
738
            int domain = tswap32(vptr[0]);
739
            int type = tswap32(vptr[1]);
740
            int protocol = tswap32(vptr[2]);
741
            int32_t *target_tab = (void *)tswap32(vptr[3]);
742
            int tab[2];
743

    
744
            ret = get_errno(socketpair(domain, type, protocol, tab));
745
            if (!is_error(ret)) {
746
                target_tab[0] = tswap32(tab[0]);
747
                target_tab[1] = tswap32(tab[1]);
748
            }
749
        }
750
        break;
751
    case SOCKOP_send:
752
        {
753
            int sockfd = tswap32(vptr[0]);
754
            void *msg = (void *)tswap32(vptr[1]);
755
            size_t len = tswap32(vptr[2]);
756
            int flags = tswap32(vptr[3]);
757

    
758
            ret = get_errno(send(sockfd, msg, len, flags));
759
        }
760
        break;
761
    case SOCKOP_recv:
762
        {
763
            int sockfd = tswap32(vptr[0]);
764
            void *msg = (void *)tswap32(vptr[1]);
765
            size_t len = tswap32(vptr[2]);
766
            int flags = tswap32(vptr[3]);
767

    
768
            ret = get_errno(recv(sockfd, msg, len, flags));
769
        }
770
        break;
771
    case SOCKOP_sendto:
772
        {
773
            int sockfd = tswap32(vptr[0]);
774
            void *msg = (void *)tswap32(vptr[1]);
775
            size_t len = tswap32(vptr[2]);
776
            int flags = tswap32(vptr[3]);
777
            void *target_addr = (void *)tswap32(vptr[4]);
778
            socklen_t addrlen = tswap32(vptr[5]);
779
            void *addr = alloca(addrlen);
780

    
781
            target_to_host_sockaddr(addr, target_addr, addrlen);
782
            ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
783
        }
784
        break;
785
    case SOCKOP_recvfrom:
786
        {
787
            int sockfd = tswap32(vptr[0]);
788
            void *msg = (void *)tswap32(vptr[1]);
789
            size_t len = tswap32(vptr[2]);
790
            int flags = tswap32(vptr[3]);
791
            void *target_addr = (void *)tswap32(vptr[4]);
792
            uint32_t *target_addrlen = (void *)tswap32(vptr[5]);
793
            socklen_t addrlen = tswap32(*target_addrlen);
794
            void *addr = alloca(addrlen);
795

    
796
            ret = get_errno(recvfrom(sockfd, msg, len, flags, addr, &addrlen));
797
            if (!is_error(ret)) {
798
                host_to_target_sockaddr(target_addr, addr, addrlen);
799
                *target_addrlen = tswap32(addrlen);
800
            }
801
        }
802
        break;
803
    case SOCKOP_shutdown:
804
        {
805
            int sockfd = tswap32(vptr[0]);
806
            int how = tswap32(vptr[1]);
807

    
808
            ret = get_errno(shutdown(sockfd, how));
809
        }
810
        break;
811
    case SOCKOP_sendmsg:
812
    case SOCKOP_recvmsg:
813
        {
814
            int fd;
815
            struct target_msghdr *msgp;
816
            struct msghdr msg;
817
            int flags, count, i;
818
            struct iovec *vec;
819
            struct target_iovec *target_vec;
820

    
821
            msgp = (void *)tswap32(vptr[1]);
822
            msg.msg_name = (void *)tswapl(msgp->msg_name);
823
            msg.msg_namelen = tswapl(msgp->msg_namelen);
824
            msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
825
            msg.msg_control = alloca(msg.msg_controllen);
826
            msg.msg_flags = tswap32(msgp->msg_flags);
827

    
828
            count = tswapl(msgp->msg_iovlen);
829
            vec = alloca(count * sizeof(struct iovec));
830
            target_vec = (void *)tswapl(msgp->msg_iov);
831
            for(i = 0;i < count; i++) {
832
                vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
833
                vec[i].iov_len = tswapl(target_vec[i].iov_len);
834
            }
835
            msg.msg_iovlen = count;
836
            msg.msg_iov = vec;
837

    
838
            fd = tswap32(vptr[0]);
839
            flags = tswap32(vptr[2]);
840
            if (num == SOCKOP_sendmsg) {
841
                target_to_host_cmsg(&msg, msgp);
842
                ret = get_errno(sendmsg(fd, &msg, flags));
843
            } else {
844
                ret = get_errno(recvmsg(fd, &msg, flags));
845
                if (!is_error(ret))
846
                  host_to_target_cmsg(msgp, &msg);
847
            }
848
        }
849
        break;
850
    case SOCKOP_setsockopt:
851
        {
852
            int sockfd = tswap32(vptr[0]);
853
            int level = tswap32(vptr[1]);
854
            int optname = tswap32(vptr[2]);
855
            void *optval = (void *)tswap32(vptr[3]);
856
            socklen_t optlen = tswap32(vptr[4]);
857

    
858
            ret = do_setsockopt(sockfd, level, optname, optval, optlen);
859
        }
860
        break;
861
    case SOCKOP_getsockopt:
862
        {
863
            int sockfd = tswap32(vptr[0]);
864
            int level = tswap32(vptr[1]);
865
            int optname = tswap32(vptr[2]);
866
            void *optval = (void *)tswap32(vptr[3]);
867
            uint32_t *poptlen = (void *)tswap32(vptr[4]);
868

    
869
            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
870
        }
871
        break;
872
    default:
873
        gemu_log("Unsupported socketcall: %d\n", num);
874
        ret = -ENOSYS;
875
        break;
876
    }
877
    return ret;
878
}
879

    
880

    
881
#define N_SHM_REGIONS        32
882

    
883
static struct shm_region {
884
    uint32_t        start;
885
    uint32_t        size;
886
} shm_regions[N_SHM_REGIONS];
887

    
888
static long do_ipc(long call, long first, long second, long third,
889
                   long ptr, long fifth)
890
{
891
    int version;
892
    long ret = 0;
893
    unsigned long raddr;
894
    struct shmid_ds shm_info;
895
    int i;
896

    
897
    version = call >> 16;
898
    call &= 0xffff;
899

    
900
    switch (call) {
901
    case IPCOP_shmat:
902
        /* SHM_* flags are the same on all linux platforms */
903
        ret = get_errno((long) shmat(first, (void *) ptr, second));
904
        if (is_error(ret))
905
            break;
906
        raddr = ret;
907
        /* find out the length of the shared memory segment */
908
        
909
        ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
910
        if (is_error(ret)) {
911
            /* can't get length, bail out */
912
            shmdt((void *) raddr);
913
            break;
914
        }
915
        page_set_flags(raddr, raddr + shm_info.shm_segsz,
916
                       PAGE_VALID | PAGE_READ |
917
                       ((second & SHM_RDONLY)? 0: PAGE_WRITE));
918
        for (i = 0; i < N_SHM_REGIONS; ++i) {
919
            if (shm_regions[i].start == 0) {
920
                shm_regions[i].start = raddr;
921
                shm_regions[i].size = shm_info.shm_segsz;
922
                break;
923
            }
924
        }
925
        if (put_user(raddr, (uint32_t *)third))
926
            return -EFAULT;
927
        ret = 0;
928
        break;
929
    case IPCOP_shmdt:
930
        for (i = 0; i < N_SHM_REGIONS; ++i) {
931
            if (shm_regions[i].start == ptr) {
932
                shm_regions[i].start = 0;
933
                page_set_flags(ptr, shm_regions[i].size, 0);
934
                break;
935
            }
936
        }
937
        ret = get_errno(shmdt((void *) ptr));
938
        break;
939

    
940
    case IPCOP_shmget:
941
        /* IPC_* flag values are the same on all linux platforms */
942
        ret = get_errno(shmget(first, second, third));
943
        break;
944

    
945
        /* IPC_* and SHM_* command values are the same on all linux platforms */
946
    case IPCOP_shmctl:
947
        switch(second) {
948
        case IPC_RMID:
949
        case SHM_LOCK:
950
        case SHM_UNLOCK:
951
            ret = get_errno(shmctl(first, second, NULL));
952
            break;
953
        default:
954
            goto unimplemented;
955
        }
956
        break;
957
    default:
958
    unimplemented:
959
        gemu_log("Unsupported ipc call: %ld (version %d)\n", call, version);
960
        ret = -ENOSYS;
961
        break;
962
    }
963
    return ret;
964
}
965

    
966
/* kernel structure types definitions */
967
#define IFNAMSIZ        16
968

    
969
#define STRUCT(name, list...) STRUCT_ ## name,
970
#define STRUCT_SPECIAL(name) STRUCT_ ## name,
971
enum {
972
#include "syscall_types.h"
973
};
974
#undef STRUCT
975
#undef STRUCT_SPECIAL
976

    
977
#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
978
#define STRUCT_SPECIAL(name)
979
#include "syscall_types.h"
980
#undef STRUCT
981
#undef STRUCT_SPECIAL
982

    
983
typedef struct IOCTLEntry {
984
    unsigned int target_cmd;
985
    unsigned int host_cmd;
986
    const char *name;
987
    int access;
988
    const argtype arg_type[5];
989
} IOCTLEntry;
990

    
991
#define IOC_R 0x0001
992
#define IOC_W 0x0002
993
#define IOC_RW (IOC_R | IOC_W)
994

    
995
#define MAX_STRUCT_SIZE 4096
996

    
997
IOCTLEntry ioctl_entries[] = {
998
#define IOCTL(cmd, access, types...) \
999
    { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1000
#include "ioctls.h"
1001
    { 0, 0, },
1002
};
1003

    
1004
static long do_ioctl(long fd, long cmd, long arg)
1005
{
1006
    const IOCTLEntry *ie;
1007
    const argtype *arg_type;
1008
    long ret;
1009
    uint8_t buf_temp[MAX_STRUCT_SIZE];
1010

    
1011
    ie = ioctl_entries;
1012
    for(;;) {
1013
        if (ie->target_cmd == 0) {
1014
            gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
1015
            return -ENOSYS;
1016
        }
1017
        if (ie->target_cmd == cmd)
1018
            break;
1019
        ie++;
1020
    }
1021
    arg_type = ie->arg_type;
1022
#if defined(DEBUG)
1023
    gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
1024
#endif
1025
    switch(arg_type[0]) {
1026
    case TYPE_NULL:
1027
        /* no argument */
1028
        ret = get_errno(ioctl(fd, ie->host_cmd));
1029
        break;
1030
    case TYPE_PTRVOID:
1031
    case TYPE_INT:
1032
        /* int argment */
1033
        ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1034
        break;
1035
    case TYPE_PTR:
1036
        arg_type++;
1037
        switch(ie->access) {
1038
        case IOC_R:
1039
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1040
            if (!is_error(ret)) {
1041
                thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
1042
            }
1043
            break;
1044
        case IOC_W:
1045
            thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
1046
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1047
            break;
1048
        default:
1049
        case IOC_RW:
1050
            thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
1051
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1052
            if (!is_error(ret)) {
1053
                thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
1054
            }
1055
            break;
1056
        }
1057
        break;
1058
    default:
1059
        gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
1060
        ret = -ENOSYS;
1061
        break;
1062
    }
1063
    return ret;
1064
}
1065

    
1066
bitmask_transtbl iflag_tbl[] = {
1067
        { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1068
        { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1069
        { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1070
        { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1071
        { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1072
        { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1073
        { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1074
        { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1075
        { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1076
        { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1077
        { TARGET_IXON, TARGET_IXON, IXON, IXON },
1078
        { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1079
        { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1080
        { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1081
        { 0, 0, 0, 0 }
1082
};
1083

    
1084
bitmask_transtbl oflag_tbl[] = {
1085
        { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1086
        { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1087
        { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1088
        { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1089
        { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1090
        { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1091
        { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1092
        { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1093
        { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1094
        { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1095
        { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1096
        { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1097
        { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1098
        { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1099
        { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1100
        { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1101
        { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1102
        { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1103
        { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1104
        { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1105
        { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1106
        { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1107
        { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1108
        { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1109
        { 0, 0, 0, 0 }
1110
};
1111

    
1112
bitmask_transtbl cflag_tbl[] = {
1113
        { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1114
        { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1115
        { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1116
        { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1117
        { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1118
        { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1119
        { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1120
        { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1121
        { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1122
        { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1123
        { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1124
        { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1125
        { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1126
        { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1127
        { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1128
        { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1129
        { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1130
        { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1131
        { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1132
        { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1133
        { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1134
        { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1135
        { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1136
        { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1137
        { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1138
        { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1139
        { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1140
        { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1141
        { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1142
        { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1143
        { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1144
        { 0, 0, 0, 0 }
1145
};
1146

    
1147
bitmask_transtbl lflag_tbl[] = {
1148
        { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1149
        { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1150
        { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1151
        { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1152
        { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1153
        { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1154
        { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1155
        { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1156
        { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1157
        { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1158
        { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1159
        { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1160
        { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1161
        { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1162
        { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1163
        { 0, 0, 0, 0 }
1164
};
1165

    
1166
static void target_to_host_termios (void *dst, const void *src)
1167
{
1168
    struct host_termios *host = dst;
1169
    const struct target_termios *target = src;
1170
    
1171
    host->c_iflag = 
1172
        target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
1173
    host->c_oflag = 
1174
        target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
1175
    host->c_cflag = 
1176
        target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
1177
    host->c_lflag = 
1178
        target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
1179
    host->c_line = target->c_line;
1180
    
1181
    host->c_cc[VINTR] = target->c_cc[TARGET_VINTR]; 
1182
    host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT]; 
1183
    host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];       
1184
    host->c_cc[VKILL] = target->c_cc[TARGET_VKILL]; 
1185
    host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];   
1186
    host->c_cc[VTIME] = target->c_cc[TARGET_VTIME]; 
1187
    host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];   
1188
    host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC]; 
1189
    host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];       
1190
    host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP]; 
1191
    host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP]; 
1192
    host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];   
1193
    host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];   
1194
    host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];   
1195
    host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];     
1196
    host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];       
1197
    host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2]; 
1198
}
1199
  
1200
static void host_to_target_termios (void *dst, const void *src)
1201
{
1202
    struct target_termios *target = dst;
1203
    const struct host_termios *host = src;
1204

    
1205
    target->c_iflag = 
1206
        tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
1207
    target->c_oflag = 
1208
        tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
1209
    target->c_cflag = 
1210
        tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
1211
    target->c_lflag = 
1212
        tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
1213
    target->c_line = host->c_line;
1214
  
1215
    target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
1216
    target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
1217
    target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
1218
    target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
1219
    target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
1220
    target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
1221
    target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
1222
    target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
1223
    target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
1224
    target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
1225
    target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
1226
    target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
1227
    target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
1228
    target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
1229
    target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
1230
    target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
1231
    target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
1232
}
1233

    
1234
StructEntry struct_termios_def = {
1235
    .convert = { host_to_target_termios, target_to_host_termios },
1236
    .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
1237
    .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
1238
};
1239

    
1240
static bitmask_transtbl mmap_flags_tbl[] = {
1241
        { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
1242
        { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
1243
        { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
1244
        { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
1245
        { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
1246
        { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
1247
        { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
1248
        { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
1249
        { 0, 0, 0, 0 }
1250
};
1251

    
1252
static bitmask_transtbl fcntl_flags_tbl[] = {
1253
        { TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
1254
        { TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
1255
        { TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
1256
        { TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
1257
        { TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
1258
        { TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
1259
        { TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
1260
        { TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
1261
        { TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
1262
        { TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
1263
        { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
1264
        { TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
1265
        { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
1266
#if defined(O_DIRECT)
1267
        { TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
1268
#endif
1269
        { 0, 0, 0, 0 }
1270
};
1271

    
1272
#if defined(TARGET_I386)
1273

    
1274
/* NOTE: there is really one LDT for all the threads */
1275
uint8_t *ldt_table;
1276

    
1277
static int read_ldt(void *ptr, unsigned long bytecount)
1278
{
1279
    int size;
1280

    
1281
    if (!ldt_table)
1282
        return 0;
1283
    size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
1284
    if (size > bytecount)
1285
        size = bytecount;
1286
    memcpy(ptr, ldt_table, size);
1287
    return size;
1288
}
1289

    
1290
/* XXX: add locking support */
1291
static int write_ldt(CPUX86State *env, 
1292
                     void *ptr, unsigned long bytecount, int oldmode)
1293
{
1294
    struct target_modify_ldt_ldt_s ldt_info;
1295
    int seg_32bit, contents, read_exec_only, limit_in_pages;
1296
    int seg_not_present, useable;
1297
    uint32_t *lp, entry_1, entry_2;
1298

    
1299
    if (bytecount != sizeof(ldt_info))
1300
        return -EINVAL;
1301
    memcpy(&ldt_info, ptr, sizeof(ldt_info));
1302
    tswap32s(&ldt_info.entry_number);
1303
    tswapls((long *)&ldt_info.base_addr);
1304
    tswap32s(&ldt_info.limit);
1305
    tswap32s(&ldt_info.flags);
1306
    
1307
    if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
1308
        return -EINVAL;
1309
    seg_32bit = ldt_info.flags & 1;
1310
    contents = (ldt_info.flags >> 1) & 3;
1311
    read_exec_only = (ldt_info.flags >> 3) & 1;
1312
    limit_in_pages = (ldt_info.flags >> 4) & 1;
1313
    seg_not_present = (ldt_info.flags >> 5) & 1;
1314
    useable = (ldt_info.flags >> 6) & 1;
1315

    
1316
    if (contents == 3) {
1317
        if (oldmode)
1318
            return -EINVAL;
1319
        if (seg_not_present == 0)
1320
            return -EINVAL;
1321
    }
1322
    /* allocate the LDT */
1323
    if (!ldt_table) {
1324
        ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1325
        if (!ldt_table)
1326
            return -ENOMEM;
1327
        memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1328
        env->ldt.base = ldt_table;
1329
        env->ldt.limit = 0xffff;
1330
    }
1331

    
1332
    /* NOTE: same code as Linux kernel */
1333
    /* Allow LDTs to be cleared by the user. */
1334
    if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
1335
        if (oldmode ||
1336
            (contents == 0                &&
1337
             read_exec_only == 1        &&
1338
             seg_32bit == 0                &&
1339
             limit_in_pages == 0        &&
1340
             seg_not_present == 1        &&
1341
             useable == 0 )) {
1342
            entry_1 = 0;
1343
            entry_2 = 0;
1344
            goto install;
1345
        }
1346
    }
1347
    
1348
    entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
1349
        (ldt_info.limit & 0x0ffff);
1350
    entry_2 = (ldt_info.base_addr & 0xff000000) |
1351
        ((ldt_info.base_addr & 0x00ff0000) >> 16) |
1352
        (ldt_info.limit & 0xf0000) |
1353
        ((read_exec_only ^ 1) << 9) |
1354
        (contents << 10) |
1355
        ((seg_not_present ^ 1) << 15) |
1356
        (seg_32bit << 22) |
1357
        (limit_in_pages << 23) |
1358
        0x7000;
1359
    if (!oldmode)
1360
        entry_2 |= (useable << 20);
1361

    
1362
    /* Install the new entry ...  */
1363
install:
1364
    lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
1365
    lp[0] = tswap32(entry_1);
1366
    lp[1] = tswap32(entry_2);
1367
    return 0;
1368
}
1369

    
1370
/* specific and weird i386 syscalls */
1371
int do_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
1372
{
1373
    int ret = -ENOSYS;
1374
    
1375
    switch (func) {
1376
    case 0:
1377
        ret = read_ldt(ptr, bytecount);
1378
        break;
1379
    case 1:
1380
        ret = write_ldt(env, ptr, bytecount, 1);
1381
        break;
1382
    case 0x11:
1383
        ret = write_ldt(env, ptr, bytecount, 0);
1384
        break;
1385
    }
1386
    return ret;
1387
}
1388

    
1389
#endif /* defined(TARGET_I386) */
1390

    
1391
/* this stack is the equivalent of the kernel stack associated with a
1392
   thread/process */
1393
#define NEW_STACK_SIZE 8192
1394

    
1395
static int clone_func(void *arg)
1396
{
1397
    CPUState *env = arg;
1398
    cpu_loop(env);
1399
    /* never exits */
1400
    return 0;
1401
}
1402

    
1403
int do_fork(CPUState *env, unsigned int flags, unsigned long newsp)
1404
{
1405
    int ret;
1406
    TaskState *ts;
1407
    uint8_t *new_stack;
1408
    CPUState *new_env;
1409
    
1410
    if (flags & CLONE_VM) {
1411
        ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
1412
        memset(ts, 0, sizeof(TaskState));
1413
        new_stack = ts->stack;
1414
        ts->used = 1;
1415
        /* add in task state list */
1416
        ts->next = first_task_state;
1417
        first_task_state = ts;
1418
        /* we create a new CPU instance. */
1419
        new_env = cpu_init();
1420
        memcpy(new_env, env, sizeof(CPUState));
1421
#if defined(TARGET_I386)
1422
        if (!newsp)
1423
            newsp = env->regs[R_ESP];
1424
        new_env->regs[R_ESP] = newsp;
1425
        new_env->regs[R_EAX] = 0;
1426
#elif defined(TARGET_ARM)
1427
        if (!newsp)
1428
            newsp = env->regs[13];
1429
        new_env->regs[13] = newsp;
1430
        new_env->regs[0] = 0;
1431
#elif defined(TARGET_SPARC)
1432
        printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
1433
#elif defined(TARGET_PPC)
1434
        if (!newsp)
1435
            newsp = env->gpr[1];
1436
        new_env->gpr[1] = newsp;
1437
        { 
1438
            int i;
1439
            for (i = 7; i < 32; i++)
1440
                new_env->gpr[i] = 0;
1441
        }
1442
#else
1443
#error unsupported target CPU
1444
#endif
1445
        new_env->opaque = ts;
1446
#ifdef __ia64__
1447
        ret = clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1448
#else
1449
        ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1450
#endif
1451
    } else {
1452
        /* if no CLONE_VM, we consider it is a fork */
1453
        if ((flags & ~CSIGNAL) != 0)
1454
            return -EINVAL;
1455
        ret = fork();
1456
    }
1457
    return ret;
1458
}
1459

    
1460
static long do_fcntl(int fd, int cmd, unsigned long arg)
1461
{
1462
    struct flock fl;
1463
    struct target_flock *target_fl = (void *)arg;
1464
    long ret;
1465
    
1466
    switch(cmd) {
1467
    case TARGET_F_GETLK:
1468
        ret = fcntl(fd, cmd, &fl);
1469
        if (ret == 0) {
1470
            target_fl->l_type = tswap16(fl.l_type);
1471
            target_fl->l_whence = tswap16(fl.l_whence);
1472
            target_fl->l_start = tswapl(fl.l_start);
1473
            target_fl->l_len = tswapl(fl.l_len);
1474
            target_fl->l_pid = tswapl(fl.l_pid);
1475
        }
1476
        break;
1477
        
1478
    case TARGET_F_SETLK:
1479
    case TARGET_F_SETLKW:
1480
        fl.l_type = tswap16(target_fl->l_type);
1481
        fl.l_whence = tswap16(target_fl->l_whence);
1482
        fl.l_start = tswapl(target_fl->l_start);
1483
        fl.l_len = tswapl(target_fl->l_len);
1484
        fl.l_pid = tswapl(target_fl->l_pid);
1485
        ret = fcntl(fd, cmd, &fl);
1486
        break;
1487
        
1488
    case TARGET_F_GETLK64:
1489
    case TARGET_F_SETLK64:
1490
    case TARGET_F_SETLKW64:
1491
        ret = -1;
1492
        errno = EINVAL;
1493
        break;
1494

    
1495
    case F_GETFL:
1496
        ret = fcntl(fd, cmd, arg);
1497
        ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
1498
        break;
1499

    
1500
    case F_SETFL:
1501
        ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
1502
        break;
1503

    
1504
    default:
1505
        ret = fcntl(fd, cmd, arg);
1506
        break;
1507
    }
1508
    return ret;
1509
}
1510

    
1511
#ifdef USE_UID16
1512

    
1513
static inline int high2lowuid(int uid)
1514
{
1515
    if (uid > 65535)
1516
        return 65534;
1517
    else
1518
        return uid;
1519
}
1520

    
1521
static inline int high2lowgid(int gid)
1522
{
1523
    if (gid > 65535)
1524
        return 65534;
1525
    else
1526
        return gid;
1527
}
1528

    
1529
static inline int low2highuid(int uid)
1530
{
1531
    if ((int16_t)uid == -1)
1532
        return -1;
1533
    else
1534
        return uid;
1535
}
1536

    
1537
static inline int low2highgid(int gid)
1538
{
1539
    if ((int16_t)gid == -1)
1540
        return -1;
1541
    else
1542
        return gid;
1543
}
1544

    
1545
#endif /* USE_UID16 */
1546

    
1547
void syscall_init(void)
1548
{
1549
    IOCTLEntry *ie;
1550
    const argtype *arg_type;
1551
    int size;
1552

    
1553
#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def); 
1554
#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def); 
1555
#include "syscall_types.h"
1556
#undef STRUCT
1557
#undef STRUCT_SPECIAL
1558

    
1559
    /* we patch the ioctl size if necessary. We rely on the fact that
1560
       no ioctl has all the bits at '1' in the size field */
1561
    ie = ioctl_entries;
1562
    while (ie->target_cmd != 0) {
1563
        if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
1564
            TARGET_IOC_SIZEMASK) {
1565
            arg_type = ie->arg_type;
1566
            if (arg_type[0] != TYPE_PTR) {
1567
                fprintf(stderr, "cannot patch size for ioctl 0x%x\n", 
1568
                        ie->target_cmd);
1569
                exit(1);
1570
            }
1571
            arg_type++;
1572
            size = thunk_type_size(arg_type, 0);
1573
            ie->target_cmd = (ie->target_cmd & 
1574
                              ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
1575
                (size << TARGET_IOC_SIZESHIFT);
1576
        }
1577
        /* automatic consistency check if same arch */
1578
#if defined(__i386__) && defined(TARGET_I386)
1579
        if (ie->target_cmd != ie->host_cmd) {
1580
            fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n", 
1581
                    ie->target_cmd, ie->host_cmd);
1582
        }
1583
#endif
1584
        ie++;
1585
    }
1586
}
1587

    
1588
long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3, 
1589
                long arg4, long arg5, long arg6)
1590
{
1591
    long ret;
1592
    struct stat st;
1593
    struct kernel_statfs *stfs;
1594
    
1595
#ifdef DEBUG
1596
    gemu_log("syscall %d", num);
1597
#endif
1598
    switch(num) {
1599
    case TARGET_NR_exit:
1600
#ifdef HAVE_GPROF
1601
        _mcleanup();
1602
#endif
1603
        /* XXX: should free thread stack and CPU env */
1604
        _exit(arg1);
1605
        ret = 0; /* avoid warning */
1606
        break;
1607
    case TARGET_NR_read:
1608
        page_unprotect_range((void *)arg2, arg3);
1609
        ret = get_errno(read(arg1, (void *)arg2, arg3));
1610
        break;
1611
    case TARGET_NR_write:
1612
        ret = get_errno(write(arg1, (void *)arg2, arg3));
1613
        break;
1614
    case TARGET_NR_open:
1615
        ret = get_errno(open(path((const char *)arg1),
1616
                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
1617
                             arg3));
1618
        break;
1619
    case TARGET_NR_close:
1620
        ret = get_errno(close(arg1));
1621
        break;
1622
    case TARGET_NR_brk:
1623
        ret = do_brk((char *)arg1);
1624
        break;
1625
    case TARGET_NR_fork:
1626
        ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
1627
        break;
1628
    case TARGET_NR_waitpid:
1629
        {
1630
            int *status = (int *)arg2;
1631
            ret = get_errno(waitpid(arg1, status, arg3));
1632
            if (!is_error(ret) && status)
1633
                tswapls((long *)&status);
1634
        }
1635
        break;
1636
    case TARGET_NR_creat:
1637
        ret = get_errno(creat((const char *)arg1, arg2));
1638
        break;
1639
    case TARGET_NR_link:
1640
        ret = get_errno(link((const char *)arg1, (const char *)arg2));
1641
        break;
1642
    case TARGET_NR_unlink:
1643
        ret = get_errno(unlink((const char *)arg1));
1644
        break;
1645
    case TARGET_NR_execve:
1646
        {
1647
            char **argp, **envp;
1648
            int argc, envc;
1649
            uint32_t *p;
1650
            char **q;
1651

    
1652
            argc = 0;
1653
            for (p = (void *)arg2; *p; p++)
1654
                argc++;
1655
            envc = 0;
1656
            for (p = (void *)arg3; *p; p++)
1657
                envc++;
1658

    
1659
            argp = alloca((argc + 1) * sizeof(void *));
1660
            envp = alloca((envc + 1) * sizeof(void *));
1661

    
1662
            for (p = (void *)arg2, q = argp; *p; p++, q++)
1663
                *q = (void *)tswap32(*p);
1664
            *q = NULL;
1665

    
1666
            for (p = (void *)arg3, q = envp; *p; p++, q++)
1667
                *q = (void *)tswap32(*p);
1668
            *q = NULL;
1669

    
1670
            ret = get_errno(execve((const char *)arg1, argp, envp));
1671
        }
1672
        break;
1673
    case TARGET_NR_chdir:
1674
        ret = get_errno(chdir((const char *)arg1));
1675
        break;
1676
    case TARGET_NR_time:
1677
        {
1678
            int *time_ptr = (int *)arg1;
1679
            ret = get_errno(time((time_t *)time_ptr));
1680
            if (!is_error(ret) && time_ptr)
1681
                tswap32s(time_ptr);
1682
        }
1683
        break;
1684
    case TARGET_NR_mknod:
1685
        ret = get_errno(mknod((const char *)arg1, arg2, arg3));
1686
        break;
1687
    case TARGET_NR_chmod:
1688
        ret = get_errno(chmod((const char *)arg1, arg2));
1689
        break;
1690
#ifdef TARGET_NR_break
1691
    case TARGET_NR_break:
1692
        goto unimplemented;
1693
#endif
1694
#ifdef TARGET_NR_oldstat
1695
    case TARGET_NR_oldstat:
1696
        goto unimplemented;
1697
#endif
1698
    case TARGET_NR_lseek:
1699
        ret = get_errno(lseek(arg1, arg2, arg3));
1700
        break;
1701
    case TARGET_NR_getpid:
1702
        ret = get_errno(getpid());
1703
        break;
1704
    case TARGET_NR_mount:
1705
        /* need to look at the data field */
1706
        goto unimplemented;
1707
    case TARGET_NR_umount:
1708
        ret = get_errno(umount((const char *)arg1));
1709
        break;
1710
    case TARGET_NR_stime:
1711
        {
1712
            int *time_ptr = (int *)arg1;
1713
            if (time_ptr)
1714
                tswap32s(time_ptr);
1715
            ret = get_errno(stime((time_t *)time_ptr));
1716
        }
1717
        break;
1718
    case TARGET_NR_ptrace:
1719
        goto unimplemented;
1720
    case TARGET_NR_alarm:
1721
        ret = alarm(arg1);
1722
        break;
1723
#ifdef TARGET_NR_oldfstat
1724
    case TARGET_NR_oldfstat:
1725
        goto unimplemented;
1726
#endif
1727
    case TARGET_NR_pause:
1728
        ret = get_errno(pause());
1729
        break;
1730
    case TARGET_NR_utime:
1731
        {
1732
            struct utimbuf tbuf, *tbuf1;
1733
            struct target_utimbuf *target_tbuf = (void *)arg2;
1734
            if (target_tbuf) {
1735
                get_user(tbuf.actime, &target_tbuf->actime);
1736
                get_user(tbuf.modtime, &target_tbuf->modtime);
1737
                tbuf1 = &tbuf;
1738
            } else {
1739
                tbuf1 = NULL;
1740
            }
1741
            ret = get_errno(utime((const char *)arg1, tbuf1));
1742
        }
1743
        break;
1744
#ifdef TARGET_NR_stty
1745
    case TARGET_NR_stty:
1746
        goto unimplemented;
1747
#endif
1748
#ifdef TARGET_NR_gtty
1749
    case TARGET_NR_gtty:
1750
        goto unimplemented;
1751
#endif
1752
    case TARGET_NR_access:
1753
        ret = get_errno(access((const char *)arg1, arg2));
1754
        break;
1755
    case TARGET_NR_nice:
1756
        ret = get_errno(nice(arg1));
1757
        break;
1758
#ifdef TARGET_NR_ftime
1759
    case TARGET_NR_ftime:
1760
        goto unimplemented;
1761
#endif
1762
    case TARGET_NR_sync:
1763
        sync();
1764
        ret = 0;
1765
        break;
1766
    case TARGET_NR_kill:
1767
        ret = get_errno(kill(arg1, arg2));
1768
        break;
1769
    case TARGET_NR_rename:
1770
        ret = get_errno(rename((const char *)arg1, (const char *)arg2));
1771
        break;
1772
    case TARGET_NR_mkdir:
1773
        ret = get_errno(mkdir((const char *)arg1, arg2));
1774
        break;
1775
    case TARGET_NR_rmdir:
1776
        ret = get_errno(rmdir((const char *)arg1));
1777
        break;
1778
    case TARGET_NR_dup:
1779
        ret = get_errno(dup(arg1));
1780
        break;
1781
    case TARGET_NR_pipe:
1782
        {
1783
            int *pipe_ptr = (int *)arg1;
1784
            ret = get_errno(pipe(pipe_ptr));
1785
            if (!is_error(ret)) {
1786
                tswap32s(&pipe_ptr[0]);
1787
                tswap32s(&pipe_ptr[1]);
1788
            }
1789
        }
1790
        break;
1791
    case TARGET_NR_times:
1792
        {
1793
            struct target_tms *tmsp = (void *)arg1;
1794
            struct tms tms;
1795
            ret = get_errno(times(&tms));
1796
            if (tmsp) {
1797
                tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
1798
                tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
1799
                tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
1800
                tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
1801
            }
1802
            if (!is_error(ret))
1803
                ret = host_to_target_clock_t(ret);
1804
        }
1805
        break;
1806
#ifdef TARGET_NR_prof
1807
    case TARGET_NR_prof:
1808
        goto unimplemented;
1809
#endif
1810
    case TARGET_NR_signal:
1811
        goto unimplemented;
1812

    
1813
    case TARGET_NR_acct:
1814
        goto unimplemented;
1815
    case TARGET_NR_umount2:
1816
        ret = get_errno(umount2((const char *)arg1, arg2));
1817
        break;
1818
#ifdef TARGET_NR_lock
1819
    case TARGET_NR_lock:
1820
        goto unimplemented;
1821
#endif
1822
    case TARGET_NR_ioctl:
1823
        ret = do_ioctl(arg1, arg2, arg3);
1824
        break;
1825
    case TARGET_NR_fcntl:
1826
        ret = get_errno(do_fcntl(arg1, arg2, arg3));
1827
        break;
1828
#ifdef TARGET_NR_mpx
1829
    case TARGET_NR_mpx:
1830
        goto unimplemented;
1831
#endif
1832
    case TARGET_NR_setpgid:
1833
        ret = get_errno(setpgid(arg1, arg2));
1834
        break;
1835
#ifdef TARGET_NR_ulimit
1836
    case TARGET_NR_ulimit:
1837
        goto unimplemented;
1838
#endif
1839
#ifdef TARGET_NR_oldolduname
1840
    case TARGET_NR_oldolduname:
1841
        goto unimplemented;
1842
#endif
1843
    case TARGET_NR_umask:
1844
        ret = get_errno(umask(arg1));
1845
        break;
1846
    case TARGET_NR_chroot:
1847
        ret = get_errno(chroot((const char *)arg1));
1848
        break;
1849
    case TARGET_NR_ustat:
1850
        goto unimplemented;
1851
    case TARGET_NR_dup2:
1852
        ret = get_errno(dup2(arg1, arg2));
1853
        break;
1854
    case TARGET_NR_getppid:
1855
        ret = get_errno(getppid());
1856
        break;
1857
    case TARGET_NR_getpgrp:
1858
        ret = get_errno(getpgrp());
1859
        break;
1860
    case TARGET_NR_setsid:
1861
        ret = get_errno(setsid());
1862
        break;
1863
    case TARGET_NR_sigaction:
1864
        {
1865
            struct target_old_sigaction *old_act = (void *)arg2;
1866
            struct target_old_sigaction *old_oact = (void *)arg3;
1867
            struct target_sigaction act, oact, *pact;
1868
            if (old_act) {
1869
                act._sa_handler = old_act->_sa_handler;
1870
                target_siginitset(&act.sa_mask, old_act->sa_mask);
1871
                act.sa_flags = old_act->sa_flags;
1872
                act.sa_restorer = old_act->sa_restorer;
1873
                pact = &act;
1874
            } else {
1875
                pact = NULL;
1876
            }
1877
            ret = get_errno(do_sigaction(arg1, pact, &oact));
1878
            if (!is_error(ret) && old_oact) {
1879
                old_oact->_sa_handler = oact._sa_handler;
1880
                old_oact->sa_mask = oact.sa_mask.sig[0];
1881
                old_oact->sa_flags = oact.sa_flags;
1882
                old_oact->sa_restorer = oact.sa_restorer;
1883
            }
1884
        }
1885
        break;
1886
    case TARGET_NR_rt_sigaction:
1887
        ret = get_errno(do_sigaction(arg1, (void *)arg2, (void *)arg3));
1888
        break;
1889
    case TARGET_NR_sgetmask:
1890
        {
1891
            sigset_t cur_set;
1892
            target_ulong target_set;
1893
            sigprocmask(0, NULL, &cur_set);
1894
            host_to_target_old_sigset(&target_set, &cur_set);
1895
            ret = target_set;
1896
        }
1897
        break;
1898
    case TARGET_NR_ssetmask:
1899
        {
1900
            sigset_t set, oset, cur_set;
1901
            target_ulong target_set = arg1;
1902
            sigprocmask(0, NULL, &cur_set);
1903
            target_to_host_old_sigset(&set, &target_set);
1904
            sigorset(&set, &set, &cur_set);
1905
            sigprocmask(SIG_SETMASK, &set, &oset);
1906
            host_to_target_old_sigset(&target_set, &oset);
1907
            ret = target_set;
1908
        }
1909
        break;
1910
    case TARGET_NR_sigprocmask:
1911
        {
1912
            int how = arg1;
1913
            sigset_t set, oldset, *set_ptr;
1914
            target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
1915
            
1916
            if (pset) {
1917
                switch(how) {
1918
                case TARGET_SIG_BLOCK:
1919
                    how = SIG_BLOCK;
1920
                    break;
1921
                case TARGET_SIG_UNBLOCK:
1922
                    how = SIG_UNBLOCK;
1923
                    break;
1924
                case TARGET_SIG_SETMASK:
1925
                    how = SIG_SETMASK;
1926
                    break;
1927
                default:
1928
                    ret = -EINVAL;
1929
                    goto fail;
1930
                }
1931
                target_to_host_old_sigset(&set, pset);
1932
                set_ptr = &set;
1933
            } else {
1934
                how = 0;
1935
                set_ptr = NULL;
1936
            }
1937
            ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
1938
            if (!is_error(ret) && poldset) {
1939
                host_to_target_old_sigset(poldset, &oldset);
1940
            }
1941
        }
1942
        break;
1943
    case TARGET_NR_rt_sigprocmask:
1944
        {
1945
            int how = arg1;
1946
            sigset_t set, oldset, *set_ptr;
1947
            target_sigset_t *pset = (void *)arg2;
1948
            target_sigset_t *poldset = (void *)arg3;
1949
            
1950
            if (pset) {
1951
                switch(how) {
1952
                case TARGET_SIG_BLOCK:
1953
                    how = SIG_BLOCK;
1954
                    break;
1955
                case TARGET_SIG_UNBLOCK:
1956
                    how = SIG_UNBLOCK;
1957
                    break;
1958
                case TARGET_SIG_SETMASK:
1959
                    how = SIG_SETMASK;
1960
                    break;
1961
                default:
1962
                    ret = -EINVAL;
1963
                    goto fail;
1964
                }
1965
                target_to_host_sigset(&set, pset);
1966
                set_ptr = &set;
1967
            } else {
1968
                how = 0;
1969
                set_ptr = NULL;
1970
            }
1971
            ret = get_errno(sigprocmask(how, set_ptr, &oldset));
1972
            if (!is_error(ret) && poldset) {
1973
                host_to_target_sigset(poldset, &oldset);
1974
            }
1975
        }
1976
        break;
1977
    case TARGET_NR_sigpending:
1978
        {
1979
            sigset_t set;
1980
            ret = get_errno(sigpending(&set));
1981
            if (!is_error(ret)) {
1982
                host_to_target_old_sigset((target_ulong *)arg1, &set);
1983
            }
1984
        }
1985
        break;
1986
    case TARGET_NR_rt_sigpending:
1987
        {
1988
            sigset_t set;
1989
            ret = get_errno(sigpending(&set));
1990
            if (!is_error(ret)) {
1991
                host_to_target_sigset((target_sigset_t *)arg1, &set);
1992
            }
1993
        }
1994
        break;
1995
    case TARGET_NR_sigsuspend:
1996
        {
1997
            sigset_t set;
1998
            target_to_host_old_sigset(&set, (target_ulong *)arg1);
1999
            ret = get_errno(sigsuspend(&set));
2000
        }
2001
        break;
2002
    case TARGET_NR_rt_sigsuspend:
2003
        {
2004
            sigset_t set;
2005
            target_to_host_sigset(&set, (target_sigset_t *)arg1);
2006
            ret = get_errno(sigsuspend(&set));
2007
        }
2008
        break;
2009
    case TARGET_NR_rt_sigtimedwait:
2010
        {
2011
            target_sigset_t *target_set = (void *)arg1;
2012
            target_siginfo_t *target_uinfo = (void *)arg2;
2013
            struct target_timespec *target_uts = (void *)arg3;
2014
            sigset_t set;
2015
            struct timespec uts, *puts;
2016
            siginfo_t uinfo;
2017
            
2018
            target_to_host_sigset(&set, target_set);
2019
            if (target_uts) {
2020
                puts = &uts;
2021
                puts->tv_sec = tswapl(target_uts->tv_sec);
2022
                puts->tv_nsec = tswapl(target_uts->tv_nsec);
2023
            } else {
2024
                puts = NULL;
2025
            }
2026
            ret = get_errno(sigtimedwait(&set, &uinfo, puts));
2027
            if (!is_error(ret) && target_uinfo) {
2028
                host_to_target_siginfo(target_uinfo, &uinfo);
2029
            }
2030
        }
2031
        break;
2032
    case TARGET_NR_rt_sigqueueinfo:
2033
        {
2034
            siginfo_t uinfo;
2035
            target_to_host_siginfo(&uinfo, (target_siginfo_t *)arg3);
2036
            ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
2037
        }
2038
        break;
2039
    case TARGET_NR_sigreturn:
2040
        /* NOTE: ret is eax, so not transcoding must be done */
2041
        ret = do_sigreturn(cpu_env);
2042
        break;
2043
    case TARGET_NR_rt_sigreturn:
2044
        /* NOTE: ret is eax, so not transcoding must be done */
2045
        ret = do_rt_sigreturn(cpu_env);
2046
        break;
2047
    case TARGET_NR_sethostname:
2048
        ret = get_errno(sethostname((const char *)arg1, arg2));
2049
        break;
2050
    case TARGET_NR_setrlimit:
2051
        {
2052
            /* XXX: convert resource ? */
2053
            int resource = arg1;
2054
            struct target_rlimit *target_rlim = (void *)arg2;
2055
            struct rlimit rlim;
2056
            rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
2057
            rlim.rlim_max = tswapl(target_rlim->rlim_max);
2058
            ret = get_errno(setrlimit(resource, &rlim));
2059
        }
2060
        break;
2061
    case TARGET_NR_getrlimit:
2062
        {
2063
            /* XXX: convert resource ? */
2064
            int resource = arg1;
2065
            struct target_rlimit *target_rlim = (void *)arg2;
2066
            struct rlimit rlim;
2067
            
2068
            ret = get_errno(getrlimit(resource, &rlim));
2069
            if (!is_error(ret)) {
2070
                target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
2071
                target_rlim->rlim_max = tswapl(rlim.rlim_max);
2072
            }
2073
        }
2074
        break;
2075
    case TARGET_NR_getrusage:
2076
        {
2077
            struct rusage rusage;
2078
            struct target_rusage *target_rusage = (void *)arg2;
2079
            ret = get_errno(getrusage(arg1, &rusage));
2080
            if (!is_error(ret)) {
2081
                host_to_target_rusage(target_rusage, &rusage);
2082
            }
2083
        }
2084
        break;
2085
    case TARGET_NR_gettimeofday:
2086
        {
2087
            struct target_timeval *target_tv = (void *)arg1;
2088
            struct timeval tv;
2089
            ret = get_errno(gettimeofday(&tv, NULL));
2090
            if (!is_error(ret)) {
2091
                host_to_target_timeval(target_tv, &tv);
2092
            }
2093
        }
2094
        break;
2095
    case TARGET_NR_settimeofday:
2096
        {
2097
            struct target_timeval *target_tv = (void *)arg1;
2098
            struct timeval tv;
2099
            target_to_host_timeval(&tv, target_tv);
2100
            ret = get_errno(settimeofday(&tv, NULL));
2101
        }
2102
        break;
2103
    case TARGET_NR_select:
2104
        {
2105
            struct target_sel_arg_struct *sel = (void *)arg1;
2106
            sel->n = tswapl(sel->n);
2107
            sel->inp = tswapl(sel->inp);
2108
            sel->outp = tswapl(sel->outp);
2109
            sel->exp = tswapl(sel->exp);
2110
            sel->tvp = tswapl(sel->tvp);
2111
            ret = do_select(sel->n, (void *)sel->inp, (void *)sel->outp,
2112
                            (void *)sel->exp, (void *)sel->tvp);
2113
        }
2114
        break;
2115
    case TARGET_NR_symlink:
2116
        ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
2117
        break;
2118
#ifdef TARGET_NR_oldlstat
2119
    case TARGET_NR_oldlstat:
2120
        goto unimplemented;
2121
#endif
2122
    case TARGET_NR_readlink:
2123
        ret = get_errno(readlink(path((const char *)arg1), (char *)arg2, arg3));
2124
        break;
2125
    case TARGET_NR_uselib:
2126
        goto unimplemented;
2127
    case TARGET_NR_swapon:
2128
        ret = get_errno(swapon((const char *)arg1, arg2));
2129
        break;
2130
    case TARGET_NR_reboot:
2131
        goto unimplemented;
2132
    case TARGET_NR_readdir:
2133
        goto unimplemented;
2134
    case TARGET_NR_mmap:
2135
#if defined(TARGET_I386) || defined(TARGET_ARM)
2136
        {
2137
            uint32_t v1, v2, v3, v4, v5, v6, *vptr;
2138
            vptr = (uint32_t *)arg1;
2139
            v1 = tswap32(vptr[0]);
2140
            v2 = tswap32(vptr[1]);
2141
            v3 = tswap32(vptr[2]);
2142
            v4 = tswap32(vptr[3]);
2143
            v5 = tswap32(vptr[4]);
2144
            v6 = tswap32(vptr[5]);
2145
            ret = get_errno(target_mmap(v1, v2, v3, 
2146
                                        target_to_host_bitmask(v4, mmap_flags_tbl),
2147
                                        v5, v6));
2148
        }
2149
#else
2150
        ret = get_errno(target_mmap(arg1, arg2, arg3, 
2151
                                    target_to_host_bitmask(arg4, mmap_flags_tbl), 
2152
                                    arg5,
2153
                                    arg6));
2154
#endif
2155
        break;
2156
    case TARGET_NR_mmap2:
2157
#if defined(TARGET_SPARC)
2158
#define MMAP_SHIFT 12
2159
#else
2160
#define MMAP_SHIFT TARGET_PAGE_BITS
2161
#endif
2162
        ret = get_errno(target_mmap(arg1, arg2, arg3, 
2163
                                    target_to_host_bitmask(arg4, mmap_flags_tbl), 
2164
                                    arg5,
2165
                                    arg6 << MMAP_SHIFT));
2166
        break;
2167
    case TARGET_NR_munmap:
2168
        ret = get_errno(target_munmap(arg1, arg2));
2169
        break;
2170
    case TARGET_NR_mprotect:
2171
        ret = get_errno(target_mprotect(arg1, arg2, arg3));
2172
        break;
2173
    case TARGET_NR_mremap:
2174
        ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
2175
        break;
2176
    case TARGET_NR_msync:
2177
        ret = get_errno(msync((void *)arg1, arg2, arg3));
2178
        break;
2179
    case TARGET_NR_mlock:
2180
        ret = get_errno(mlock((void *)arg1, arg2));
2181
        break;
2182
    case TARGET_NR_munlock:
2183
        ret = get_errno(munlock((void *)arg1, arg2));
2184
        break;
2185
    case TARGET_NR_mlockall:
2186
        ret = get_errno(mlockall(arg1));
2187
        break;
2188
    case TARGET_NR_munlockall:
2189
        ret = get_errno(munlockall());
2190
        break;
2191
    case TARGET_NR_truncate:
2192
        ret = get_errno(truncate((const char *)arg1, arg2));
2193
        break;
2194
    case TARGET_NR_ftruncate:
2195
        ret = get_errno(ftruncate(arg1, arg2));
2196
        break;
2197
    case TARGET_NR_fchmod:
2198
        ret = get_errno(fchmod(arg1, arg2));
2199
        break;
2200
    case TARGET_NR_getpriority:
2201
        ret = get_errno(getpriority(arg1, arg2));
2202
        break;
2203
    case TARGET_NR_setpriority:
2204
        ret = get_errno(setpriority(arg1, arg2, arg3));
2205
        break;
2206
#ifdef TARGET_NR_profil
2207
    case TARGET_NR_profil:
2208
        goto unimplemented;
2209
#endif
2210
    case TARGET_NR_statfs:
2211
        stfs = (void *)arg2;
2212
        ret = get_errno(sys_statfs(path((const char *)arg1), stfs));
2213
    convert_statfs:
2214
        if (!is_error(ret)) {
2215
            tswap32s(&stfs->f_type);
2216
            tswap32s(&stfs->f_bsize);
2217
            tswap32s(&stfs->f_blocks);
2218
            tswap32s(&stfs->f_bfree);
2219
            tswap32s(&stfs->f_bavail);
2220
            tswap32s(&stfs->f_files);
2221
            tswap32s(&stfs->f_ffree);
2222
            tswap32s(&stfs->f_fsid.val[0]);
2223
            tswap32s(&stfs->f_fsid.val[1]);
2224
            tswap32s(&stfs->f_namelen);
2225
        }
2226
        break;
2227
    case TARGET_NR_fstatfs:
2228
        stfs = (void *)arg2;
2229
        ret = get_errno(sys_fstatfs(arg1, stfs));
2230
        goto convert_statfs;
2231
#ifdef TARGET_NR_ioperm
2232
    case TARGET_NR_ioperm:
2233
        goto unimplemented;
2234
#endif
2235
    case TARGET_NR_socketcall:
2236
        ret = do_socketcall(arg1, (int32_t *)arg2);
2237
        break;
2238
    case TARGET_NR_syslog:
2239
        goto unimplemented;
2240
    case TARGET_NR_setitimer:
2241
        {
2242
            struct target_itimerval *target_value = (void *)arg2;
2243
            struct target_itimerval *target_ovalue = (void *)arg3;
2244
            struct itimerval value, ovalue, *pvalue;
2245

    
2246
            if (target_value) {
2247
                pvalue = &value;
2248
                target_to_host_timeval(&pvalue->it_interval, 
2249
                                       &target_value->it_interval);
2250
                target_to_host_timeval(&pvalue->it_value, 
2251
                                       &target_value->it_value);
2252
            } else {
2253
                pvalue = NULL;
2254
            }
2255
            ret = get_errno(setitimer(arg1, pvalue, &ovalue));
2256
            if (!is_error(ret) && target_ovalue) {
2257
                host_to_target_timeval(&target_ovalue->it_interval, 
2258
                                       &ovalue.it_interval);
2259
                host_to_target_timeval(&target_ovalue->it_value, 
2260
                                       &ovalue.it_value);
2261
            }
2262
        }
2263
        break;
2264
    case TARGET_NR_getitimer:
2265
        {
2266
            struct target_itimerval *target_value = (void *)arg2;
2267
            struct itimerval value;
2268
            
2269
            ret = get_errno(getitimer(arg1, &value));
2270
            if (!is_error(ret) && target_value) {
2271
                host_to_target_timeval(&target_value->it_interval, 
2272
                                       &value.it_interval);
2273
                host_to_target_timeval(&target_value->it_value, 
2274
                                       &value.it_value);
2275
            }
2276
        }
2277
        break;
2278
    case TARGET_NR_stat:
2279
        ret = get_errno(stat(path((const char *)arg1), &st));
2280
        goto do_stat;
2281
    case TARGET_NR_lstat:
2282
        ret = get_errno(lstat(path((const char *)arg1), &st));
2283
        goto do_stat;
2284
    case TARGET_NR_fstat:
2285
        {
2286
            ret = get_errno(fstat(arg1, &st));
2287
        do_stat:
2288
            if (!is_error(ret)) {
2289
                struct target_stat *target_st = (void *)arg2;
2290
                target_st->st_dev = tswap16(st.st_dev);
2291
                target_st->st_ino = tswapl(st.st_ino);
2292
#if defined(TARGET_PPC)
2293
                target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
2294
                target_st->st_uid = tswap32(st.st_uid);
2295
                target_st->st_gid = tswap32(st.st_gid);
2296
#else
2297
                target_st->st_mode = tswap16(st.st_mode);
2298
                target_st->st_uid = tswap16(st.st_uid);
2299
                target_st->st_gid = tswap16(st.st_gid);
2300
#endif
2301
                target_st->st_nlink = tswap16(st.st_nlink);
2302
                target_st->st_rdev = tswap16(st.st_rdev);
2303
                target_st->st_size = tswapl(st.st_size);
2304
                target_st->st_blksize = tswapl(st.st_blksize);
2305
                target_st->st_blocks = tswapl(st.st_blocks);
2306
                target_st->target_st_atime = tswapl(st.st_atime);
2307
                target_st->target_st_mtime = tswapl(st.st_mtime);
2308
                target_st->target_st_ctime = tswapl(st.st_ctime);
2309
            }
2310
        }
2311
        break;
2312
#ifdef TARGET_NR_olduname
2313
    case TARGET_NR_olduname:
2314
        goto unimplemented;
2315
#endif
2316
#ifdef TARGET_NR_iopl
2317
    case TARGET_NR_iopl:
2318
        goto unimplemented;
2319
#endif
2320
    case TARGET_NR_vhangup:
2321
        ret = get_errno(vhangup());
2322
        break;
2323
#ifdef TARGET_NR_idle
2324
    case TARGET_NR_idle:
2325
        goto unimplemented;
2326
#endif
2327
    case TARGET_NR_wait4:
2328
        {
2329
            int status;
2330
            target_long *status_ptr = (void *)arg2;
2331
            struct rusage rusage, *rusage_ptr;
2332
            struct target_rusage *target_rusage = (void *)arg4;
2333
            if (target_rusage)
2334
                rusage_ptr = &rusage;
2335
            else
2336
                rusage_ptr = NULL;
2337
            ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
2338
            if (!is_error(ret)) {
2339
                if (status_ptr)
2340
                    *status_ptr = tswap32(status);
2341
                if (target_rusage) {
2342
                    host_to_target_rusage(target_rusage, &rusage);
2343
                }
2344
            }
2345
        }
2346
        break;
2347
    case TARGET_NR_swapoff:
2348
        ret = get_errno(swapoff((const char *)arg1));
2349
        break;
2350
    case TARGET_NR_sysinfo:
2351
        goto unimplemented;
2352
    case TARGET_NR_ipc:
2353
        ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
2354
        break;
2355
    case TARGET_NR_fsync:
2356
        ret = get_errno(fsync(arg1));
2357
        break;
2358
    case TARGET_NR_clone:
2359
        ret = get_errno(do_fork(cpu_env, arg1, arg2));
2360
        break;
2361
#ifdef __NR_exit_group
2362
        /* new thread calls */
2363
    case TARGET_NR_exit_group:
2364
        ret = get_errno(exit_group(arg1));
2365
        break;
2366
#endif
2367
    case TARGET_NR_setdomainname:
2368
        ret = get_errno(setdomainname((const char *)arg1, arg2));
2369
        break;
2370
    case TARGET_NR_uname:
2371
        /* no need to transcode because we use the linux syscall */
2372
        ret = get_errno(sys_uname((struct new_utsname *)arg1));
2373
        break;
2374
#ifdef TARGET_I386
2375
    case TARGET_NR_modify_ldt:
2376
        ret = get_errno(do_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
2377
        break;
2378
    case TARGET_NR_vm86old:
2379
        goto unimplemented;
2380
    case TARGET_NR_vm86:
2381
        ret = do_vm86(cpu_env, arg1, (void *)arg2);
2382
        break;
2383
#endif
2384
    case TARGET_NR_adjtimex:
2385
        goto unimplemented;
2386
    case TARGET_NR_create_module:
2387
    case TARGET_NR_init_module:
2388
    case TARGET_NR_delete_module:
2389
    case TARGET_NR_get_kernel_syms:
2390
        goto unimplemented;
2391
    case TARGET_NR_quotactl:
2392
        goto unimplemented;
2393
    case TARGET_NR_getpgid:
2394
        ret = get_errno(getpgid(arg1));
2395
        break;
2396
    case TARGET_NR_fchdir:
2397
        ret = get_errno(fchdir(arg1));
2398
        break;
2399
    case TARGET_NR_bdflush:
2400
        goto unimplemented;
2401
    case TARGET_NR_sysfs:
2402
        goto unimplemented;
2403
    case TARGET_NR_personality:
2404
        ret = get_errno(personality(arg1));
2405
        break;
2406
    case TARGET_NR_afs_syscall:
2407
        goto unimplemented;
2408
    case TARGET_NR__llseek:
2409
        {
2410
#if defined (__x86_64__)
2411
            ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
2412
            *(int64_t *)arg4 = ret;
2413
#else
2414
            int64_t res;
2415
            ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
2416
            *(int64_t *)arg4 = tswap64(res);
2417
#endif
2418
        }
2419
        break;
2420
    case TARGET_NR_getdents:
2421
#if TARGET_LONG_SIZE != 4
2422
#error not supported
2423
#elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
2424
        {
2425
            struct target_dirent *target_dirp = (void *)arg2;
2426
            struct dirent *dirp;
2427
            long count = arg3;
2428

    
2429
            dirp = malloc(count);
2430
            if (!dirp)
2431
                return -ENOMEM;
2432
            
2433
            ret = get_errno(sys_getdents(arg1, dirp, count));
2434
            if (!is_error(ret)) {
2435
                struct dirent *de;
2436
                struct target_dirent *tde;
2437
                int len = ret;
2438
                int reclen, treclen;
2439
                int count1, tnamelen;
2440

    
2441
                count1 = 0;
2442
                de = dirp;
2443
                tde = target_dirp;
2444
                while (len > 0) {
2445
                    reclen = de->d_reclen;
2446
                    treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
2447
                    tde->d_reclen = tswap16(treclen);
2448
                    tde->d_ino = tswapl(de->d_ino);
2449
                    tde->d_off = tswapl(de->d_off);
2450
                    tnamelen = treclen - (2 * sizeof(target_long) + 2);
2451
                    if (tnamelen > 256)
2452
                        tnamelen = 256;
2453
                    strncpy(tde->d_name, de->d_name, tnamelen);
2454
                    de = (struct dirent *)((char *)de + reclen);
2455
                    len -= reclen;
2456
                    tde = (struct dirent *)((char *)tde + treclen);
2457
                    count1 += treclen;
2458
                }
2459
                ret = count1;
2460
            }
2461
            free(dirp);
2462
        }
2463
#else
2464
        {
2465
            struct dirent *dirp = (void *)arg2;
2466
            long count = arg3;
2467

    
2468
            ret = get_errno(sys_getdents(arg1, dirp, count));
2469
            if (!is_error(ret)) {
2470
                struct dirent *de;
2471
                int len = ret;
2472
                int reclen;
2473
                de = dirp;
2474
                while (len > 0) {
2475
                    reclen = de->d_reclen;
2476
                    if (reclen > len)
2477
                        break;
2478
                    de->d_reclen = tswap16(reclen);
2479
                    tswapls(&de->d_ino);
2480
                    tswapls(&de->d_off);
2481
                    de = (struct dirent *)((char *)de + reclen);
2482
                    len -= reclen;
2483
                }
2484
            }
2485
        }
2486
#endif
2487
        break;
2488
#ifdef TARGET_NR_getdents64
2489
    case TARGET_NR_getdents64:
2490
        {
2491
            struct dirent64 *dirp = (void *)arg2;
2492
            long count = arg3;
2493
            ret = get_errno(sys_getdents64(arg1, dirp, count));
2494
            if (!is_error(ret)) {
2495
                struct dirent64 *de;
2496
                int len = ret;
2497
                int reclen;
2498
                de = dirp;
2499
                while (len > 0) {
2500
                    reclen = de->d_reclen;
2501
                    if (reclen > len)
2502
                        break;
2503
                    de->d_reclen = tswap16(reclen);
2504
                    tswap64s(&de->d_ino);
2505
                    tswap64s(&de->d_off);
2506
                    de = (struct dirent64 *)((char *)de + reclen);
2507
                    len -= reclen;
2508
                }
2509
            }
2510
        }
2511
        break;
2512
#endif /* TARGET_NR_getdents64 */
2513
    case TARGET_NR__newselect:
2514
        ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4, 
2515
                        (void *)arg5);
2516
        break;
2517
    case TARGET_NR_poll:
2518
        {
2519
            struct target_pollfd *target_pfd = (void *)arg1;
2520
            unsigned int nfds = arg2;
2521
            int timeout = arg3;
2522
            struct pollfd *pfd;
2523
            unsigned int i;
2524

    
2525
            pfd = alloca(sizeof(struct pollfd) * nfds);
2526
            for(i = 0; i < nfds; i++) {
2527
                pfd[i].fd = tswap32(target_pfd[i].fd);
2528
                pfd[i].events = tswap16(target_pfd[i].events);
2529
            }
2530
            ret = get_errno(poll(pfd, nfds, timeout));
2531
            if (!is_error(ret)) {
2532
                for(i = 0; i < nfds; i++) {
2533
                    target_pfd[i].revents = tswap16(pfd[i].revents);
2534
                }
2535
            }
2536
        }
2537
        break;
2538
    case TARGET_NR_flock:
2539
        /* NOTE: the flock constant seems to be the same for every
2540
           Linux platform */
2541
        ret = get_errno(flock(arg1, arg2));
2542
        break;
2543
    case TARGET_NR_readv:
2544
        {
2545
            int count = arg3;
2546
            int i;
2547
            struct iovec *vec;
2548
            struct target_iovec *target_vec = (void *)arg2;
2549

    
2550
            vec = alloca(count * sizeof(struct iovec));
2551
            for(i = 0;i < count; i++) {
2552
                vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2553
                vec[i].iov_len = tswapl(target_vec[i].iov_len);
2554
            }
2555
            ret = get_errno(readv(arg1, vec, count));
2556
        }
2557
        break;
2558
    case TARGET_NR_writev:
2559
        {
2560
            int count = arg3;
2561
            int i;
2562
            struct iovec *vec;
2563
            struct target_iovec *target_vec = (void *)arg2;
2564

    
2565
            vec = alloca(count * sizeof(struct iovec));
2566
            for(i = 0;i < count; i++) {
2567
                vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2568
                vec[i].iov_len = tswapl(target_vec[i].iov_len);
2569
            }
2570
            ret = get_errno(writev(arg1, vec, count));
2571
        }
2572
        break;
2573
    case TARGET_NR_getsid:
2574
        ret = get_errno(getsid(arg1));
2575
        break;
2576
    case TARGET_NR_fdatasync:
2577
        ret = get_errno(fdatasync(arg1));
2578
        break;
2579
    case TARGET_NR__sysctl:
2580
        goto unimplemented;
2581
    case TARGET_NR_sched_setparam:
2582
        {
2583
            struct sched_param *target_schp = (void *)arg2;
2584
            struct sched_param schp;
2585
            schp.sched_priority = tswap32(target_schp->sched_priority);
2586
            ret = get_errno(sched_setparam(arg1, &schp));
2587
        }
2588
        break;
2589
    case TARGET_NR_sched_getparam:
2590
        {
2591
            struct sched_param *target_schp = (void *)arg2;
2592
            struct sched_param schp;
2593
            ret = get_errno(sched_getparam(arg1, &schp));
2594
            if (!is_error(ret)) {
2595
                target_schp->sched_priority = tswap32(schp.sched_priority);
2596
            }
2597
        }
2598
        break;
2599
    case TARGET_NR_sched_setscheduler:
2600
        {
2601
            struct sched_param *target_schp = (void *)arg3;
2602
            struct sched_param schp;
2603
            schp.sched_priority = tswap32(target_schp->sched_priority);
2604
            ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
2605
        }
2606
        break;
2607
    case TARGET_NR_sched_getscheduler:
2608
        ret = get_errno(sched_getscheduler(arg1));
2609
        break;
2610
    case TARGET_NR_sched_yield:
2611
        ret = get_errno(sched_yield());
2612
        break;
2613
    case TARGET_NR_sched_get_priority_max:
2614
        ret = get_errno(sched_get_priority_max(arg1));
2615
        break;
2616
    case TARGET_NR_sched_get_priority_min:
2617
        ret = get_errno(sched_get_priority_min(arg1));
2618
        break;
2619
    case TARGET_NR_sched_rr_get_interval:
2620
        {
2621
            struct target_timespec *target_ts = (void *)arg2;
2622
            struct timespec ts;
2623
            ret = get_errno(sched_rr_get_interval(arg1, &ts));
2624
            if (!is_error(ret)) {
2625
                target_ts->tv_sec = tswapl(ts.tv_sec);
2626
                target_ts->tv_nsec = tswapl(ts.tv_nsec);
2627
            }
2628
        }
2629
        break;
2630
    case TARGET_NR_nanosleep:
2631
        {
2632
            struct target_timespec *target_req = (void *)arg1;
2633
            struct target_timespec *target_rem = (void *)arg2;
2634
            struct timespec req, rem;
2635
            req.tv_sec = tswapl(target_req->tv_sec);
2636
            req.tv_nsec = tswapl(target_req->tv_nsec);
2637
            ret = get_errno(nanosleep(&req, &rem));
2638
            if (is_error(ret) && target_rem) {
2639
                target_rem->tv_sec = tswapl(rem.tv_sec);
2640
                target_rem->tv_nsec = tswapl(rem.tv_nsec);
2641
            }
2642
        }
2643
        break;
2644
    case TARGET_NR_query_module:
2645
        goto unimplemented;
2646
    case TARGET_NR_nfsservctl:
2647
        goto unimplemented;
2648
    case TARGET_NR_prctl:
2649
        goto unimplemented;
2650
#ifdef TARGET_NR_pread
2651
    case TARGET_NR_pread:
2652
        page_unprotect_range((void *)arg2, arg3);
2653
        ret = get_errno(pread(arg1, (void *)arg2, arg3, arg4));
2654
        break;
2655
    case TARGET_NR_pwrite:
2656
        ret = get_errno(pwrite(arg1, (void *)arg2, arg3, arg4));
2657
        break;
2658
#endif
2659
    case TARGET_NR_getcwd:
2660
        ret = get_errno(sys_getcwd1((char *)arg1, arg2));
2661
        break;
2662
    case TARGET_NR_capget:
2663
        goto unimplemented;
2664
    case TARGET_NR_capset:
2665
        goto unimplemented;
2666
    case TARGET_NR_sigaltstack:
2667
        goto unimplemented;
2668
    case TARGET_NR_sendfile:
2669
        goto unimplemented;
2670
#ifdef TARGET_NR_getpmsg
2671
    case TARGET_NR_getpmsg:
2672
        goto unimplemented;
2673
#endif
2674
#ifdef TARGET_NR_putpmsg
2675
    case TARGET_NR_putpmsg:
2676
        goto unimplemented;
2677
#endif
2678
    case TARGET_NR_vfork:
2679
        ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
2680
        break;
2681
#ifdef TARGET_NR_ugetrlimit
2682
    case TARGET_NR_ugetrlimit:
2683
    {
2684
        struct rlimit rlim;
2685
        ret = get_errno(getrlimit(arg1, &rlim));
2686
        if (!is_error(ret)) {
2687
            struct target_rlimit *target_rlim = (void *)arg2;
2688
            target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
2689
            target_rlim->rlim_max = tswapl(rlim.rlim_max);
2690
        }
2691
        break;
2692
    }
2693
#endif
2694
    case TARGET_NR_truncate64:
2695
        goto unimplemented;
2696
    case TARGET_NR_ftruncate64:
2697
        goto unimplemented;
2698
    case TARGET_NR_stat64:
2699
        ret = get_errno(stat(path((const char *)arg1), &st));
2700
        goto do_stat64;
2701
    case TARGET_NR_lstat64:
2702
        ret = get_errno(lstat(path((const char *)arg1), &st));
2703
        goto do_stat64;
2704
    case TARGET_NR_fstat64:
2705
        {
2706
            ret = get_errno(fstat(arg1, &st));
2707
        do_stat64:
2708
            if (!is_error(ret)) {
2709
                struct target_stat64 *target_st = (void *)arg2;
2710
                memset(target_st, 0, sizeof(struct target_stat64));
2711
                put_user(st.st_dev, &target_st->st_dev);
2712
                put_user(st.st_ino, &target_st->st_ino);
2713
#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
2714
                put_user(st.st_ino, &target_st->__st_ino);
2715
#endif
2716
                put_user(st.st_mode, &target_st->st_mode);
2717
                put_user(st.st_nlink, &target_st->st_nlink);
2718
                put_user(st.st_uid, &target_st->st_uid);
2719
                put_user(st.st_gid, &target_st->st_gid);
2720
                put_user(st.st_rdev, &target_st->st_rdev);
2721
                /* XXX: better use of kernel struct */
2722
                put_user(st.st_size, &target_st->st_size);
2723
                put_user(st.st_blksize, &target_st->st_blksize);
2724
                put_user(st.st_blocks, &target_st->st_blocks);
2725
                put_user(st.st_atime, &target_st->target_st_atime);
2726
                put_user(st.st_mtime, &target_st->target_st_mtime);
2727
                put_user(st.st_ctime, &target_st->target_st_ctime);
2728
            }
2729
        }
2730
        break;
2731

    
2732
#ifdef USE_UID16
2733
    case TARGET_NR_lchown:
2734
        ret = get_errno(lchown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
2735
        break;
2736
    case TARGET_NR_getuid:
2737
        ret = get_errno(high2lowuid(getuid()));
2738
        break;
2739
    case TARGET_NR_getgid:
2740
        ret = get_errno(high2lowgid(getgid()));
2741
        break;
2742
    case TARGET_NR_geteuid:
2743
        ret = get_errno(high2lowuid(geteuid()));
2744
        break;
2745
    case TARGET_NR_getegid:
2746
        ret = get_errno(high2lowgid(getegid()));
2747
        break;
2748
    case TARGET_NR_setreuid:
2749
        ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
2750
        break;
2751
    case TARGET_NR_setregid:
2752
        ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
2753
        break;
2754
    case TARGET_NR_getgroups:
2755
        {
2756
            int gidsetsize = arg1;
2757
            uint16_t *target_grouplist = (void *)arg2;
2758
            gid_t *grouplist;
2759
            int i;
2760

    
2761
            grouplist = alloca(gidsetsize * sizeof(gid_t));
2762
            ret = get_errno(getgroups(gidsetsize, grouplist));
2763
            if (!is_error(ret)) {
2764
                for(i = 0;i < gidsetsize; i++)
2765
                    target_grouplist[i] = tswap16(grouplist[i]);
2766
            }
2767
        }
2768
        break;
2769
    case TARGET_NR_setgroups:
2770
        {
2771
            int gidsetsize = arg1;
2772
            uint16_t *target_grouplist = (void *)arg2;
2773
            gid_t *grouplist;
2774
            int i;
2775

    
2776
            grouplist = alloca(gidsetsize * sizeof(gid_t));
2777
            for(i = 0;i < gidsetsize; i++)
2778
                grouplist[i] = tswap16(target_grouplist[i]);
2779
            ret = get_errno(setgroups(gidsetsize, grouplist));
2780
        }
2781
        break;
2782
    case TARGET_NR_fchown:
2783
        ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
2784
        break;
2785
#ifdef TARGET_NR_setresuid
2786
    case TARGET_NR_setresuid:
2787
        ret = get_errno(setresuid(low2highuid(arg1), 
2788
                                  low2highuid(arg2), 
2789
                                  low2highuid(arg3)));
2790
        break;
2791
#endif
2792
#ifdef TARGET_NR_getresuid
2793
    case TARGET_NR_getresuid:
2794
        {
2795
            int ruid, euid, suid;
2796
            ret = get_errno(getresuid(&ruid, &euid, &suid));
2797
            if (!is_error(ret)) {
2798
                *(uint16_t *)arg1 = tswap16(high2lowuid(ruid));
2799
                *(uint16_t *)arg2 = tswap16(high2lowuid(euid));
2800
                *(uint16_t *)arg3 = tswap16(high2lowuid(suid));
2801
            }
2802
        }
2803
        break;
2804
#endif
2805
#ifdef TARGET_NR_getresgid
2806
    case TARGET_NR_setresgid:
2807
        ret = get_errno(setresgid(low2highgid(arg1), 
2808
                                  low2highgid(arg2), 
2809
                                  low2highgid(arg3)));
2810
        break;
2811
#endif
2812
#ifdef TARGET_NR_getresgid
2813
    case TARGET_NR_getresgid:
2814
        {
2815
            int rgid, egid, sgid;
2816
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
2817
            if (!is_error(ret)) {
2818
                *(uint16_t *)arg1 = tswap16(high2lowgid(rgid));
2819
                *(uint16_t *)arg2 = tswap16(high2lowgid(egid));
2820
                *(uint16_t *)arg3 = tswap16(high2lowgid(sgid));
2821
            }
2822
        }
2823
        break;
2824
#endif
2825
    case TARGET_NR_chown:
2826
        ret = get_errno(chown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
2827
        break;
2828
    case TARGET_NR_setuid:
2829
        ret = get_errno(setuid(low2highuid(arg1)));
2830
        break;
2831
    case TARGET_NR_setgid:
2832
        ret = get_errno(setgid(low2highgid(arg1)));
2833
        break;
2834
    case TARGET_NR_setfsuid:
2835
        ret = get_errno(setfsuid(arg1));
2836
        break;
2837
    case TARGET_NR_setfsgid:
2838
        ret = get_errno(setfsgid(arg1));
2839
        break;
2840
#endif /* USE_UID16 */
2841

    
2842
    case TARGET_NR_lchown32:
2843
        ret = get_errno(lchown((const char *)arg1, arg2, arg3));
2844
        break;
2845
    case TARGET_NR_getuid32:
2846
        ret = get_errno(getuid());
2847
        break;
2848
    case TARGET_NR_getgid32:
2849
        ret = get_errno(getgid());
2850
        break;
2851
    case TARGET_NR_geteuid32:
2852
        ret = get_errno(geteuid());
2853
        break;
2854
    case TARGET_NR_getegid32:
2855
        ret = get_errno(getegid());
2856
        break;
2857
    case TARGET_NR_setreuid32:
2858
        ret = get_errno(setreuid(arg1, arg2));
2859
        break;
2860
    case TARGET_NR_setregid32:
2861
        ret = get_errno(setregid(arg1, arg2));
2862
        break;
2863
    case TARGET_NR_getgroups32:
2864
        goto unimplemented;
2865
    case TARGET_NR_setgroups32:
2866
        goto unimplemented;
2867
    case TARGET_NR_fchown32:
2868
        ret = get_errno(fchown(arg1, arg2, arg3));
2869
        break;
2870
    case TARGET_NR_setresuid32:
2871
        ret = get_errno(setresuid(arg1, arg2, arg3));
2872
        break;
2873
    case TARGET_NR_getresuid32:
2874
        {
2875
            int ruid, euid, suid;
2876
            ret = get_errno(getresuid(&ruid, &euid, &suid));
2877
            if (!is_error(ret)) {
2878
                *(uint32_t *)arg1 = tswap32(ruid);
2879
                *(uint32_t *)arg2 = tswap32(euid);
2880
                *(uint32_t *)arg3 = tswap32(suid);
2881
            }
2882
        }
2883
        break;
2884
    case TARGET_NR_setresgid32:
2885
        ret = get_errno(setresgid(arg1, arg2, arg3));
2886
        break;
2887
    case TARGET_NR_getresgid32:
2888
        {
2889
            int rgid, egid, sgid;
2890
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
2891
            if (!is_error(ret)) {
2892
                *(uint32_t *)arg1 = tswap32(rgid);
2893
                *(uint32_t *)arg2 = tswap32(egid);
2894
                *(uint32_t *)arg3 = tswap32(sgid);
2895
            }
2896
        }
2897
        break;
2898
    case TARGET_NR_chown32:
2899
        ret = get_errno(chown((const char *)arg1, arg2, arg3));
2900
        break;
2901
    case TARGET_NR_setuid32:
2902
        ret = get_errno(setuid(arg1));
2903
        break;
2904
    case TARGET_NR_setgid32:
2905
        ret = get_errno(setgid(arg1));
2906
        break;
2907
    case TARGET_NR_setfsuid32:
2908
        ret = get_errno(setfsuid(arg1));
2909
        break;
2910
    case TARGET_NR_setfsgid32:
2911
        ret = get_errno(setfsgid(arg1));
2912
        break;
2913

    
2914
    case TARGET_NR_pivot_root:
2915
        goto unimplemented;
2916
#ifdef TARGET_NR_mincore
2917
    case TARGET_NR_mincore:
2918
        goto unimplemented;
2919
#endif
2920
#ifdef TARGET_NR_madvise
2921
    case TARGET_NR_madvise:
2922
        goto unimplemented;
2923
#endif
2924
#if TARGET_LONG_BITS == 32
2925
    case TARGET_NR_fcntl64:
2926
    {
2927
        struct flock64 fl;
2928
        struct target_flock64 *target_fl = (void *)arg3;
2929

    
2930
        switch(arg2) {
2931
        case F_GETLK64:
2932
            ret = get_errno(fcntl(arg1, arg2, &fl));
2933
            if (ret == 0) {
2934
                target_fl->l_type = tswap16(fl.l_type);
2935
                target_fl->l_whence = tswap16(fl.l_whence);
2936
                target_fl->l_start = tswap64(fl.l_start);
2937
                target_fl->l_len = tswap64(fl.l_len);
2938
                target_fl->l_pid = tswapl(fl.l_pid);
2939
            }
2940
            break;
2941

    
2942
        case F_SETLK64:
2943
        case F_SETLKW64:
2944
            fl.l_type = tswap16(target_fl->l_type);
2945
            fl.l_whence = tswap16(target_fl->l_whence);
2946
            fl.l_start = tswap64(target_fl->l_start);
2947
            fl.l_len = tswap64(target_fl->l_len);
2948
            fl.l_pid = tswapl(target_fl->l_pid);
2949
            ret = get_errno(fcntl(arg1, arg2, &fl));
2950
            break;
2951
        default:
2952
            ret = get_errno(do_fcntl(arg1, arg2, arg3));
2953
            break;
2954
        }
2955
        break;
2956
    }
2957
#endif
2958
#ifdef TARGET_NR_security
2959
    case TARGET_NR_security:
2960
        goto unimplemented;
2961
#endif
2962
#ifdef TARGET_NR_getpagesize
2963
    case TARGET_NR_getpagesize:
2964
        ret = TARGET_PAGE_SIZE;
2965
        break;
2966
#endif
2967
    case TARGET_NR_gettid:
2968
        ret = get_errno(gettid());
2969
        break;
2970
    case TARGET_NR_readahead:
2971
        goto unimplemented;
2972
#ifdef TARGET_NR_setxattr
2973
    case TARGET_NR_setxattr:
2974
    case TARGET_NR_lsetxattr:
2975
    case TARGET_NR_fsetxattr:
2976
    case TARGET_NR_getxattr:
2977
    case TARGET_NR_lgetxattr:
2978
    case TARGET_NR_fgetxattr:
2979
    case TARGET_NR_listxattr:
2980
    case TARGET_NR_llistxattr:
2981
    case TARGET_NR_flistxattr:
2982
    case TARGET_NR_removexattr:
2983
    case TARGET_NR_lremovexattr:
2984
    case TARGET_NR_fremovexattr:
2985
        goto unimplemented_nowarn;
2986
#endif
2987
#ifdef TARGET_NR_set_thread_area
2988
    case TARGET_NR_set_thread_area:
2989
    case TARGET_NR_get_thread_area:
2990
        goto unimplemented_nowarn;
2991
#endif
2992
    default:
2993
    unimplemented:
2994
        gemu_log("qemu: Unsupported syscall: %d\n", num);
2995
    unimplemented_nowarn:
2996
        ret = -ENOSYS;
2997
        break;
2998
    }
2999
 fail:
3000
#ifdef DEBUG
3001
    gemu_log(" = %ld\n", ret);
3002
#endif
3003
    return ret;
3004
}
3005