Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ bc51c5c9

History | View | Annotate | Download (93.3 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;
1733
            struct target_utimbuf *target_tbuf = (void *)arg2;
1734
            tbuf.actime = tswapl(target_tbuf->actime);
1735
            tbuf.modtime = tswapl(target_tbuf->modtime);
1736
            ret = get_errno(utime((const char *)arg1, &tbuf));
1737
        }
1738
        break;
1739
#ifdef TARGET_NR_stty
1740
    case TARGET_NR_stty:
1741
        goto unimplemented;
1742
#endif
1743
#ifdef TARGET_NR_gtty
1744
    case TARGET_NR_gtty:
1745
        goto unimplemented;
1746
#endif
1747
    case TARGET_NR_access:
1748
        ret = get_errno(access((const char *)arg1, arg2));
1749
        break;
1750
    case TARGET_NR_nice:
1751
        ret = get_errno(nice(arg1));
1752
        break;
1753
#ifdef TARGET_NR_ftime
1754
    case TARGET_NR_ftime:
1755
        goto unimplemented;
1756
#endif
1757
    case TARGET_NR_sync:
1758
        sync();
1759
        ret = 0;
1760
        break;
1761
    case TARGET_NR_kill:
1762
        ret = get_errno(kill(arg1, arg2));
1763
        break;
1764
    case TARGET_NR_rename:
1765
        ret = get_errno(rename((const char *)arg1, (const char *)arg2));
1766
        break;
1767
    case TARGET_NR_mkdir:
1768
        ret = get_errno(mkdir((const char *)arg1, arg2));
1769
        break;
1770
    case TARGET_NR_rmdir:
1771
        ret = get_errno(rmdir((const char *)arg1));
1772
        break;
1773
    case TARGET_NR_dup:
1774
        ret = get_errno(dup(arg1));
1775
        break;
1776
    case TARGET_NR_pipe:
1777
        {
1778
            int *pipe_ptr = (int *)arg1;
1779
            ret = get_errno(pipe(pipe_ptr));
1780
            if (!is_error(ret)) {
1781
                tswap32s(&pipe_ptr[0]);
1782
                tswap32s(&pipe_ptr[1]);
1783
            }
1784
        }
1785
        break;
1786
    case TARGET_NR_times:
1787
        {
1788
            struct target_tms *tmsp = (void *)arg1;
1789
            struct tms tms;
1790
            ret = get_errno(times(&tms));
1791
            if (tmsp) {
1792
                tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
1793
                tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
1794
                tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
1795
                tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
1796
            }
1797
            if (!is_error(ret))
1798
                ret = host_to_target_clock_t(ret);
1799
        }
1800
        break;
1801
#ifdef TARGET_NR_prof
1802
    case TARGET_NR_prof:
1803
        goto unimplemented;
1804
#endif
1805
    case TARGET_NR_signal:
1806
        goto unimplemented;
1807

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

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

    
2419
            dirp = malloc(count);
2420
            if (!dirp)
2421
                return -ENOMEM;
2422
            
2423
            ret = get_errno(sys_getdents(arg1, dirp, count));
2424
            if (!is_error(ret)) {
2425
                struct dirent *de;
2426
                struct target_dirent *tde;
2427
                int len = ret;
2428
                int reclen, treclen;
2429
                int count1, tnamelen;
2430

    
2431
                count1 = 0;
2432
                de = dirp;
2433
                tde = target_dirp;
2434
                while (len > 0) {
2435
                    reclen = de->d_reclen;
2436
                    treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
2437
                    tde->d_reclen = tswap16(treclen);
2438
                    tde->d_ino = tswapl(de->d_ino);
2439
                    tde->d_off = tswapl(de->d_off);
2440
                    tnamelen = treclen - (2 * sizeof(target_long) + 2);
2441
                    if (tnamelen > 256)
2442
                        tnamelen = 256;
2443
                    strncpy(tde->d_name, de->d_name, tnamelen);
2444
                    de = (struct dirent *)((char *)de + reclen);
2445
                    len -= reclen;
2446
                    tde = (struct dirent *)((char *)tde + treclen);
2447
                    count1 += treclen;
2448
                }
2449
                ret = count1;
2450
            }
2451
            free(dirp);
2452
        }
2453
#else
2454
        {
2455
            struct dirent *dirp = (void *)arg2;
2456
            long count = arg3;
2457

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

    
2513
            pfd = alloca(sizeof(struct pollfd) * nfds);
2514
            for(i = 0; i < nfds; i++) {
2515
                pfd[i].fd = tswap32(target_pfd[i].fd);
2516
                pfd[i].events = tswap16(target_pfd[i].events);
2517
            }
2518
            ret = get_errno(poll(pfd, nfds, timeout));
2519
            if (!is_error(ret)) {
2520
                for(i = 0; i < nfds; i++) {
2521
                    target_pfd[i].revents = tswap16(pfd[i].revents);
2522
                }
2523
            }
2524
        }
2525
        break;
2526
    case TARGET_NR_flock:
2527
        /* NOTE: the flock constant seems to be the same for every
2528
           Linux platform */
2529
        ret = get_errno(flock(arg1, arg2));
2530
        break;
2531
    case TARGET_NR_readv:
2532
        {
2533
            int count = arg3;
2534
            int i;
2535
            struct iovec *vec;
2536
            struct target_iovec *target_vec = (void *)arg2;
2537

    
2538
            vec = alloca(count * sizeof(struct iovec));
2539
            for(i = 0;i < count; i++) {
2540
                vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2541
                vec[i].iov_len = tswapl(target_vec[i].iov_len);
2542
            }
2543
            ret = get_errno(readv(arg1, vec, count));
2544
        }
2545
        break;
2546
    case TARGET_NR_writev:
2547
        {
2548
            int count = arg3;
2549
            int i;
2550
            struct iovec *vec;
2551
            struct target_iovec *target_vec = (void *)arg2;
2552

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

    
2720
#ifdef USE_UID16
2721
    case TARGET_NR_lchown:
2722
        ret = get_errno(lchown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
2723
        break;
2724
    case TARGET_NR_getuid:
2725
        ret = get_errno(high2lowuid(getuid()));
2726
        break;
2727
    case TARGET_NR_getgid:
2728
        ret = get_errno(high2lowgid(getgid()));
2729
        break;
2730
    case TARGET_NR_geteuid:
2731
        ret = get_errno(high2lowuid(geteuid()));
2732
        break;
2733
    case TARGET_NR_getegid:
2734
        ret = get_errno(high2lowgid(getegid()));
2735
        break;
2736
    case TARGET_NR_setreuid:
2737
        ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
2738
        break;
2739
    case TARGET_NR_setregid:
2740
        ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
2741
        break;
2742
    case TARGET_NR_getgroups:
2743
        {
2744
            int gidsetsize = arg1;
2745
            uint16_t *target_grouplist = (void *)arg2;
2746
            gid_t *grouplist;
2747
            int i;
2748

    
2749
            grouplist = alloca(gidsetsize * sizeof(gid_t));
2750
            ret = get_errno(getgroups(gidsetsize, grouplist));
2751
            if (!is_error(ret)) {
2752
                for(i = 0;i < gidsetsize; i++)
2753
                    target_grouplist[i] = tswap16(grouplist[i]);
2754
            }
2755
        }
2756
        break;
2757
    case TARGET_NR_setgroups:
2758
        {
2759
            int gidsetsize = arg1;
2760
            uint16_t *target_grouplist = (void *)arg2;
2761
            gid_t *grouplist;
2762
            int i;
2763

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

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

    
2902
    case TARGET_NR_pivot_root:
2903
        goto unimplemented;
2904
#ifdef TARGET_NR_mincore
2905
    case TARGET_NR_mincore:
2906
        goto unimplemented;
2907
#endif
2908
#ifdef TARGET_NR_madvise
2909
    case TARGET_NR_madvise:
2910
        goto unimplemented;
2911
#endif
2912
#if TARGET_LONG_BITS == 32
2913
    case TARGET_NR_fcntl64:
2914
    {
2915
        struct flock64 fl;
2916
        struct target_flock64 *target_fl = (void *)arg3;
2917

    
2918
        switch(arg2) {
2919
        case F_GETLK64:
2920
            ret = get_errno(fcntl(arg1, arg2, &fl));
2921
            if (ret == 0) {
2922
                target_fl->l_type = tswap16(fl.l_type);
2923
                target_fl->l_whence = tswap16(fl.l_whence);
2924
                target_fl->l_start = tswap64(fl.l_start);
2925
                target_fl->l_len = tswap64(fl.l_len);
2926
                target_fl->l_pid = tswapl(fl.l_pid);
2927
            }
2928
            break;
2929

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