Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ 12bc92ab

History | View | Annotate | Download (155.1 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/ipc.h>
32
#include <sys/msg.h>
33
#include <sys/wait.h>
34
#include <sys/time.h>
35
#include <sys/stat.h>
36
#include <sys/mount.h>
37
#include <sys/prctl.h>
38
#include <sys/resource.h>
39
#include <sys/mman.h>
40
#include <sys/swap.h>
41
#include <signal.h>
42
#include <sched.h>
43
#include <sys/socket.h>
44
#include <sys/uio.h>
45
#include <sys/poll.h>
46
#include <sys/times.h>
47
#include <sys/shm.h>
48
#include <sys/sem.h>
49
#include <sys/statfs.h>
50
#include <utime.h>
51
#include <sys/sysinfo.h>
52
//#include <sys/user.h>
53
#include <netinet/ip.h>
54
#include <netinet/tcp.h>
55

    
56
#define termios host_termios
57
#define winsize host_winsize
58
#define termio host_termio
59
#define sgttyb host_sgttyb /* same as target */
60
#define tchars host_tchars /* same as target */
61
#define ltchars host_ltchars /* same as target */
62

    
63
#include <linux/termios.h>
64
#include <linux/unistd.h>
65
#include <linux/utsname.h>
66
#include <linux/cdrom.h>
67
#include <linux/hdreg.h>
68
#include <linux/soundcard.h>
69
#include <linux/dirent.h>
70
#include <linux/kd.h>
71

    
72
#include "qemu.h"
73

    
74
//#define DEBUG
75

    
76
#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
77
    || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
78
/* 16 bit uid wrappers emulation */
79
#define USE_UID16
80
#endif
81

    
82
//#include <linux/msdos_fs.h>
83
#define        VFAT_IOCTL_READDIR_BOTH                _IOR('r', 1, struct dirent [2])
84
#define        VFAT_IOCTL_READDIR_SHORT        _IOR('r', 2, struct dirent [2])
85

    
86

    
87
#undef _syscall0
88
#undef _syscall1
89
#undef _syscall2
90
#undef _syscall3
91
#undef _syscall4
92
#undef _syscall5
93
#undef _syscall6
94

    
95
#define _syscall0(type,name)                \
96
type name (void)                        \
97
{                                        \
98
        return syscall(__NR_##name);        \
99
}
100

    
101
#define _syscall1(type,name,type1,arg1)                \
102
type name (type1 arg1)                                \
103
{                                                \
104
        return syscall(__NR_##name, arg1);        \
105
}
106

    
107
#define _syscall2(type,name,type1,arg1,type2,arg2)        \
108
type name (type1 arg1,type2 arg2)                        \
109
{                                                        \
110
        return syscall(__NR_##name, arg1, arg2);        \
111
}
112

    
113
#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)        \
114
type name (type1 arg1,type2 arg2,type3 arg3)                        \
115
{                                                                \
116
        return syscall(__NR_##name, arg1, arg2, arg3);                \
117
}
118

    
119
#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)        \
120
type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4)                                \
121
{                                                                                \
122
        return syscall(__NR_##name, arg1, arg2, arg3, arg4);                        \
123
}
124

    
125
#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,        \
126
                  type5,arg5)                                                        \
127
type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5)                \
128
{                                                                                \
129
        return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5);                \
130
}
131

    
132

    
133
#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,        \
134
                  type5,arg5,type6,arg6)                                        \
135
type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6)        \
136
{                                                                                \
137
        return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6);        \
138
}
139

    
140

    
141
#define __NR_sys_uname __NR_uname
142
#define __NR_sys_faccessat __NR_faccessat
143
#define __NR_sys_fchmodat __NR_fchmodat
144
#define __NR_sys_fchownat __NR_fchownat
145
#define __NR_sys_getcwd1 __NR_getcwd
146
#define __NR_sys_getdents __NR_getdents
147
#define __NR_sys_getdents64 __NR_getdents64
148
#define __NR_sys_getpriority __NR_getpriority
149
#define __NR_sys_linkat __NR_linkat
150
#define __NR_sys_mkdirat __NR_mkdirat
151
#define __NR_sys_mknodat __NR_mknodat
152
#define __NR_sys_openat __NR_openat
153
#define __NR_sys_readlinkat __NR_readlinkat
154
#define __NR_sys_renameat __NR_renameat
155
#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
156
#define __NR_sys_symlinkat __NR_symlinkat
157
#define __NR_sys_syslog __NR_syslog
158
#define __NR_sys_tgkill __NR_tgkill
159
#define __NR_sys_tkill __NR_tkill
160
#define __NR_sys_unlinkat __NR_unlinkat
161
#define __NR_sys_utimensat __NR_utimensat
162

    
163
#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
164
#define __NR__llseek __NR_lseek
165
#endif
166

    
167
#ifdef __NR_gettid
168
_syscall0(int, gettid)
169
#else
170
/* This is a replacement for the host gettid() and must return a host
171
   errno. */
172
static int gettid(void) {
173
    return -ENOSYS;
174
}
175
#endif
176
_syscall1(int,sys_uname,struct new_utsname *,buf)
177
#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
178
_syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
179
#endif
180
#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
181
_syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
182
          mode_t,mode,int,flags)
183
#endif
184
#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
185
_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
186
          uid_t,owner,gid_t,group,int,flags)
187
#endif
188
_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
189
_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
190
#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
191
_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
192
#endif
193
_syscall2(int, sys_getpriority, int, which, int, who);
194
_syscall5(int, _llseek,  uint,  fd, ulong, hi, ulong, lo,
195
          loff_t *, res, uint, wh);
196
#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
197
_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
198
          int,newdirfd,const char *,newpath,int,flags)
199
#endif
200
#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
201
_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
202
#endif
203
#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
204
_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
205
          mode_t,mode,dev_t,dev)
206
#endif
207
#if defined(TARGET_NR_openat) && defined(__NR_openat)
208
_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
209
#endif
210
#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
211
_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
212
          char *,buf,size_t,bufsize)
213
#endif
214
#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
215
_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
216
          int,newdirfd,const char *,newpath)
217
#endif
218
_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
219
#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
220
_syscall3(int,sys_symlinkat,const char *,oldpath,
221
          int,newdirfd,const char *,newpath)
222
#endif
223
_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
224
#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
225
_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
226
#endif
227
#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
228
_syscall2(int,sys_tkill,int,tid,int,sig)
229
#endif
230
#ifdef __NR_exit_group
231
_syscall1(int,exit_group,int,error_code)
232
#endif
233
#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234
_syscall1(int,set_tid_address,int *,tidptr)
235
#endif
236
#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
237
_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
238
#endif
239
#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
240
_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
241
          const struct timespec *,tsp,int,flags)
242
#endif
243

    
244
extern int personality(int);
245
extern int flock(int, int);
246
extern int setfsuid(int);
247
extern int setfsgid(int);
248
extern int setresuid(uid_t, uid_t, uid_t);
249
extern int getresuid(uid_t *, uid_t *, uid_t *);
250
extern int setresgid(gid_t, gid_t, gid_t);
251
extern int getresgid(gid_t *, gid_t *, gid_t *);
252
extern int setgroups(int, gid_t *);
253

    
254
/*
255
 * This list is the union of errno values overridden in asm-<arch>/errno.h
256
 * minus the errnos that are not actually generic to all archs.
257
 */
258
static uint16_t host_to_target_errno_table[1200] = {
259
    [EIDRM]                = TARGET_EIDRM,
260
    [ECHRNG]                = TARGET_ECHRNG,
261
    [EL2NSYNC]                = TARGET_EL2NSYNC,
262
    [EL3HLT]                = TARGET_EL3HLT,
263
    [EL3RST]                = TARGET_EL3RST,
264
    [ELNRNG]                = TARGET_ELNRNG,
265
    [EUNATCH]                = TARGET_EUNATCH,
266
    [ENOCSI]                = TARGET_ENOCSI,
267
    [EL2HLT]                = TARGET_EL2HLT,
268
    [EDEADLK]                = TARGET_EDEADLK,
269
    [ENOLCK]                = TARGET_ENOLCK,
270
    [EBADE]                = TARGET_EBADE,
271
    [EBADR]                = TARGET_EBADR,
272
    [EXFULL]                = TARGET_EXFULL,
273
    [ENOANO]                = TARGET_ENOANO,
274
    [EBADRQC]                = TARGET_EBADRQC,
275
    [EBADSLT]                = TARGET_EBADSLT,
276
    [EBFONT]                = TARGET_EBFONT,
277
    [ENOSTR]                = TARGET_ENOSTR,
278
    [ENODATA]                = TARGET_ENODATA,
279
    [ETIME]                = TARGET_ETIME,
280
    [ENOSR]                = TARGET_ENOSR,
281
    [ENONET]                = TARGET_ENONET,
282
    [ENOPKG]                = TARGET_ENOPKG,
283
    [EREMOTE]                = TARGET_EREMOTE,
284
    [ENOLINK]                = TARGET_ENOLINK,
285
    [EADV]                = TARGET_EADV,
286
    [ESRMNT]                = TARGET_ESRMNT,
287
    [ECOMM]                = TARGET_ECOMM,
288
    [EPROTO]                = TARGET_EPROTO,
289
    [EDOTDOT]                = TARGET_EDOTDOT,
290
    [EMULTIHOP]                = TARGET_EMULTIHOP,
291
    [EBADMSG]                = TARGET_EBADMSG,
292
    [ENAMETOOLONG]        = TARGET_ENAMETOOLONG,
293
    [EOVERFLOW]                = TARGET_EOVERFLOW,
294
    [ENOTUNIQ]                = TARGET_ENOTUNIQ,
295
    [EBADFD]                = TARGET_EBADFD,
296
    [EREMCHG]                = TARGET_EREMCHG,
297
    [ELIBACC]                = TARGET_ELIBACC,
298
    [ELIBBAD]                = TARGET_ELIBBAD,
299
    [ELIBSCN]                = TARGET_ELIBSCN,
300
    [ELIBMAX]                = TARGET_ELIBMAX,
301
    [ELIBEXEC]                = TARGET_ELIBEXEC,
302
    [EILSEQ]                = TARGET_EILSEQ,
303
    [ENOSYS]                = TARGET_ENOSYS,
304
    [ELOOP]                = TARGET_ELOOP,
305
    [ERESTART]                = TARGET_ERESTART,
306
    [ESTRPIPE]                = TARGET_ESTRPIPE,
307
    [ENOTEMPTY]                = TARGET_ENOTEMPTY,
308
    [EUSERS]                = TARGET_EUSERS,
309
    [ENOTSOCK]                = TARGET_ENOTSOCK,
310
    [EDESTADDRREQ]        = TARGET_EDESTADDRREQ,
311
    [EMSGSIZE]                = TARGET_EMSGSIZE,
312
    [EPROTOTYPE]        = TARGET_EPROTOTYPE,
313
    [ENOPROTOOPT]        = TARGET_ENOPROTOOPT,
314
    [EPROTONOSUPPORT]        = TARGET_EPROTONOSUPPORT,
315
    [ESOCKTNOSUPPORT]        = TARGET_ESOCKTNOSUPPORT,
316
    [EOPNOTSUPP]        = TARGET_EOPNOTSUPP,
317
    [EPFNOSUPPORT]        = TARGET_EPFNOSUPPORT,
318
    [EAFNOSUPPORT]        = TARGET_EAFNOSUPPORT,
319
    [EADDRINUSE]        = TARGET_EADDRINUSE,
320
    [EADDRNOTAVAIL]        = TARGET_EADDRNOTAVAIL,
321
    [ENETDOWN]                = TARGET_ENETDOWN,
322
    [ENETUNREACH]        = TARGET_ENETUNREACH,
323
    [ENETRESET]                = TARGET_ENETRESET,
324
    [ECONNABORTED]        = TARGET_ECONNABORTED,
325
    [ECONNRESET]        = TARGET_ECONNRESET,
326
    [ENOBUFS]                = TARGET_ENOBUFS,
327
    [EISCONN]                = TARGET_EISCONN,
328
    [ENOTCONN]                = TARGET_ENOTCONN,
329
    [EUCLEAN]                = TARGET_EUCLEAN,
330
    [ENOTNAM]                = TARGET_ENOTNAM,
331
    [ENAVAIL]                = TARGET_ENAVAIL,
332
    [EISNAM]                = TARGET_EISNAM,
333
    [EREMOTEIO]                = TARGET_EREMOTEIO,
334
    [ESHUTDOWN]                = TARGET_ESHUTDOWN,
335
    [ETOOMANYREFS]        = TARGET_ETOOMANYREFS,
336
    [ETIMEDOUT]                = TARGET_ETIMEDOUT,
337
    [ECONNREFUSED]        = TARGET_ECONNREFUSED,
338
    [EHOSTDOWN]                = TARGET_EHOSTDOWN,
339
    [EHOSTUNREACH]        = TARGET_EHOSTUNREACH,
340
    [EALREADY]                = TARGET_EALREADY,
341
    [EINPROGRESS]        = TARGET_EINPROGRESS,
342
    [ESTALE]                = TARGET_ESTALE,
343
    [ECANCELED]                = TARGET_ECANCELED,
344
    [ENOMEDIUM]                = TARGET_ENOMEDIUM,
345
    [EMEDIUMTYPE]        = TARGET_EMEDIUMTYPE,
346
#ifdef ENOKEY
347
    [ENOKEY]                = TARGET_ENOKEY,
348
#endif
349
#ifdef EKEYEXPIRED
350
    [EKEYEXPIRED]        = TARGET_EKEYEXPIRED,
351
#endif
352
#ifdef EKEYREVOKED
353
    [EKEYREVOKED]        = TARGET_EKEYREVOKED,
354
#endif
355
#ifdef EKEYREJECTED
356
    [EKEYREJECTED]        = TARGET_EKEYREJECTED,
357
#endif
358
#ifdef EOWNERDEAD
359
    [EOWNERDEAD]        = TARGET_EOWNERDEAD,
360
#endif
361
#ifdef ENOTRECOVERABLE
362
    [ENOTRECOVERABLE]        = TARGET_ENOTRECOVERABLE,
363
#endif
364
        };
365

    
366
static inline int host_to_target_errno(int err)
367
{
368
    if(host_to_target_errno_table[err])
369
        return host_to_target_errno_table[err];
370
    return err;
371
}
372

    
373
static inline abi_long get_errno(abi_long ret)
374
{
375
    if (ret == -1)
376
        return -host_to_target_errno(errno);
377
    else
378
        return ret;
379
}
380

    
381
static inline int is_error(abi_long ret)
382
{
383
    return (abi_ulong)ret >= (abi_ulong)(-4096);
384
}
385

    
386
static abi_ulong target_brk;
387
static abi_ulong target_original_brk;
388

    
389
void target_set_brk(abi_ulong new_brk)
390
{
391
    target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
392
}
393

    
394
/* do_brk() must return target values and target errnos. */
395
abi_long do_brk(abi_ulong new_brk)
396
{
397
    abi_ulong brk_page;
398
    abi_long mapped_addr;
399
    int        new_alloc_size;
400

    
401
    if (!new_brk)
402
        return target_brk;
403
    if (new_brk < target_original_brk)
404
        return -TARGET_ENOMEM;
405

    
406
    brk_page = HOST_PAGE_ALIGN(target_brk);
407

    
408
    /* If the new brk is less than this, set it and we're done... */
409
    if (new_brk < brk_page) {
410
        target_brk = new_brk;
411
            return target_brk;
412
    }
413

    
414
    /* We need to allocate more memory after the brk... */
415
    new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
416
    mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
417
                                        PROT_READ|PROT_WRITE,
418
                                        MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
419
    if (is_error(mapped_addr)) {
420
        return mapped_addr;
421
    } else {
422
        target_brk = new_brk;
423
            return target_brk;
424
    }
425
}
426

    
427
static inline fd_set *target_to_host_fds(fd_set *fds,
428
                                         abi_long *target_fds, int n)
429
{
430
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
431
    return (fd_set *)target_fds;
432
#else
433
    int i, b;
434
    if (target_fds) {
435
        FD_ZERO(fds);
436
        for(i = 0;i < n; i++) {
437
            b = (tswapl(target_fds[i / TARGET_ABI_BITS]) >>
438
                 (i & (TARGET_ABI_BITS - 1))) & 1;
439
            if (b)
440
                FD_SET(i, fds);
441
        }
442
        return fds;
443
    } else {
444
        return NULL;
445
    }
446
#endif
447
}
448

    
449
static inline void host_to_target_fds(abi_long *target_fds,
450
                                      fd_set *fds, int n)
451
{
452
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
453
    /* nothing to do */
454
#else
455
    int i, nw, j, k;
456
    abi_long v;
457

    
458
    if (target_fds) {
459
        nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
460
        k = 0;
461
        for(i = 0;i < nw; i++) {
462
            v = 0;
463
            for(j = 0; j < TARGET_ABI_BITS; j++) {
464
                v |= ((FD_ISSET(k, fds) != 0) << j);
465
                k++;
466
            }
467
            target_fds[i] = tswapl(v);
468
        }
469
    }
470
#endif
471
}
472

    
473
#if defined(__alpha__)
474
#define HOST_HZ 1024
475
#else
476
#define HOST_HZ 100
477
#endif
478

    
479
static inline abi_long host_to_target_clock_t(long ticks)
480
{
481
#if HOST_HZ == TARGET_HZ
482
    return ticks;
483
#else
484
    return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
485
#endif
486
}
487

    
488
static inline void host_to_target_rusage(abi_ulong target_addr,
489
                                         const struct rusage *rusage)
490
{
491
    struct target_rusage *target_rusage;
492

    
493
    lock_user_struct(target_rusage, target_addr, 0);
494
    target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
495
    target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
496
    target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
497
    target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
498
    target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
499
    target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
500
    target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
501
    target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
502
    target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
503
    target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
504
    target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
505
    target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
506
    target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
507
    target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
508
    target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
509
    target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
510
    target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
511
    target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
512
    unlock_user_struct(target_rusage, target_addr, 1);
513
}
514

    
515
static inline void target_to_host_timeval(struct timeval *tv,
516
                                          abi_ulong target_addr)
517
{
518
    struct target_timeval *target_tv;
519

    
520
    lock_user_struct(target_tv, target_addr, 1);
521
    tv->tv_sec = tswapl(target_tv->tv_sec);
522
    tv->tv_usec = tswapl(target_tv->tv_usec);
523
    unlock_user_struct(target_tv, target_addr, 0);
524
}
525

    
526
static inline void host_to_target_timeval(abi_ulong target_addr,
527
                                          const struct timeval *tv)
528
{
529
    struct target_timeval *target_tv;
530

    
531
    lock_user_struct(target_tv, target_addr, 0);
532
    target_tv->tv_sec = tswapl(tv->tv_sec);
533
    target_tv->tv_usec = tswapl(tv->tv_usec);
534
    unlock_user_struct(target_tv, target_addr, 1);
535
}
536

    
537

    
538
/* do_select() must return target values and target errnos. */
539
static abi_long do_select(int n,
540
                          abi_ulong rfd_p, abi_ulong wfd_p,
541
                          abi_ulong efd_p, abi_ulong target_tv)
542
{
543
    fd_set rfds, wfds, efds;
544
    fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
545
    abi_long *target_rfds, *target_wfds, *target_efds;
546
    struct timeval tv, *tv_ptr;
547
    abi_long ret;
548
    int ok;
549

    
550
    if (rfd_p) {
551
        target_rfds = lock_user(rfd_p, sizeof(abi_long) * n, 1);
552
        rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
553
    } else {
554
        target_rfds = NULL;
555
        rfds_ptr = NULL;
556
    }
557
    if (wfd_p) {
558
        target_wfds = lock_user(wfd_p, sizeof(abi_long) * n, 1);
559
        wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
560
    } else {
561
        target_wfds = NULL;
562
        wfds_ptr = NULL;
563
    }
564
    if (efd_p) {
565
        target_efds = lock_user(efd_p, sizeof(abi_long) * n, 1);
566
        efds_ptr = target_to_host_fds(&efds, target_efds, n);
567
    } else {
568
        target_efds = NULL;
569
        efds_ptr = NULL;
570
    }
571

    
572
    if (target_tv) {
573
        target_to_host_timeval(&tv, target_tv);
574
        tv_ptr = &tv;
575
    } else {
576
        tv_ptr = NULL;
577
    }
578
    ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
579
    ok = !is_error(ret);
580

    
581
    if (ok) {
582
        host_to_target_fds(target_rfds, rfds_ptr, n);
583
        host_to_target_fds(target_wfds, wfds_ptr, n);
584
        host_to_target_fds(target_efds, efds_ptr, n);
585

    
586
        if (target_tv) {
587
            host_to_target_timeval(target_tv, &tv);
588
        }
589
    }
590
    if (target_rfds)
591
        unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0);
592
    if (target_wfds)
593
        unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0);
594
    if (target_efds)
595
        unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0);
596

    
597
    return ret;
598
}
599

    
600
static inline void target_to_host_sockaddr(struct sockaddr *addr,
601
                                           abi_ulong target_addr,
602
                                           socklen_t len)
603
{
604
    struct target_sockaddr *target_saddr;
605

    
606
    target_saddr = lock_user(target_addr, len, 1);
607
    memcpy(addr, target_saddr, len);
608
    addr->sa_family = tswap16(target_saddr->sa_family);
609
    unlock_user(target_saddr, target_addr, 0);
610
}
611

    
612
static inline void host_to_target_sockaddr(abi_ulong target_addr,
613
                                           struct sockaddr *addr,
614
                                           socklen_t len)
615
{
616
    struct target_sockaddr *target_saddr;
617

    
618
    target_saddr = lock_user(target_addr, len, 0);
619
    memcpy(target_saddr, addr, len);
620
    target_saddr->sa_family = tswap16(addr->sa_family);
621
    unlock_user(target_saddr, target_addr, len);
622
}
623

    
624
/* ??? Should this also swap msgh->name?  */
625
static inline void target_to_host_cmsg(struct msghdr *msgh,
626
                                       struct target_msghdr *target_msgh)
627
{
628
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
629
    struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
630
    socklen_t space = 0;
631

    
632
    while (cmsg && target_cmsg) {
633
        void *data = CMSG_DATA(cmsg);
634
        void *target_data = TARGET_CMSG_DATA(target_cmsg);
635

    
636
        int len = tswapl(target_cmsg->cmsg_len)
637
                  - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
638

    
639
        space += CMSG_SPACE(len);
640
        if (space > msgh->msg_controllen) {
641
            space -= CMSG_SPACE(len);
642
            gemu_log("Host cmsg overflow\n");
643
            break;
644
        }
645

    
646
        cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
647
        cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
648
        cmsg->cmsg_len = CMSG_LEN(len);
649

    
650
        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
651
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
652
            memcpy(data, target_data, len);
653
        } else {
654
            int *fd = (int *)data;
655
            int *target_fd = (int *)target_data;
656
            int i, numfds = len / sizeof(int);
657

    
658
            for (i = 0; i < numfds; i++)
659
                fd[i] = tswap32(target_fd[i]);
660
        }
661

    
662
        cmsg = CMSG_NXTHDR(msgh, cmsg);
663
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
664
    }
665

    
666
    msgh->msg_controllen = space;
667
}
668

    
669
/* ??? Should this also swap msgh->name?  */
670
static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
671
                                       struct msghdr *msgh)
672
{
673
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
674
    struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
675
    socklen_t space = 0;
676

    
677
    while (cmsg && target_cmsg) {
678
        void *data = CMSG_DATA(cmsg);
679
        void *target_data = TARGET_CMSG_DATA(target_cmsg);
680

    
681
        int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
682

    
683
        space += TARGET_CMSG_SPACE(len);
684
        if (space > tswapl(target_msgh->msg_controllen)) {
685
            space -= TARGET_CMSG_SPACE(len);
686
            gemu_log("Target cmsg overflow\n");
687
            break;
688
        }
689

    
690
        target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
691
        target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
692
        target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
693

    
694
        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
695
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
696
            memcpy(target_data, data, len);
697
        } else {
698
            int *fd = (int *)data;
699
            int *target_fd = (int *)target_data;
700
            int i, numfds = len / sizeof(int);
701

    
702
            for (i = 0; i < numfds; i++)
703
                target_fd[i] = tswap32(fd[i]);
704
        }
705

    
706
        cmsg = CMSG_NXTHDR(msgh, cmsg);
707
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
708
    }
709

    
710
    msgh->msg_controllen = tswapl(space);
711
}
712

    
713
/* do_setsockopt() Must return target values and target errnos. */
714
static abi_long do_setsockopt(int sockfd, int level, int optname,
715
                              abi_ulong optval, socklen_t optlen)
716
{
717
    abi_long ret;
718
    int val;
719

    
720
    switch(level) {
721
    case SOL_TCP:
722
        /* TCP options all take an 'int' value.  */
723
        if (optlen < sizeof(uint32_t))
724
            return -TARGET_EINVAL;
725

    
726
        val = tget32(optval);
727
        ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
728
        break;
729
    case SOL_IP:
730
        switch(optname) {
731
        case IP_TOS:
732
        case IP_TTL:
733
        case IP_HDRINCL:
734
        case IP_ROUTER_ALERT:
735
        case IP_RECVOPTS:
736
        case IP_RETOPTS:
737
        case IP_PKTINFO:
738
        case IP_MTU_DISCOVER:
739
        case IP_RECVERR:
740
        case IP_RECVTOS:
741
#ifdef IP_FREEBIND
742
        case IP_FREEBIND:
743
#endif
744
        case IP_MULTICAST_TTL:
745
        case IP_MULTICAST_LOOP:
746
            val = 0;
747
            if (optlen >= sizeof(uint32_t)) {
748
                val = tget32(optval);
749
            } else if (optlen >= 1) {
750
                val = tget8(optval);
751
            }
752
            ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
753
            break;
754
        default:
755
            goto unimplemented;
756
        }
757
        break;
758
    case TARGET_SOL_SOCKET:
759
        switch (optname) {
760
            /* Options with 'int' argument.  */
761
        case TARGET_SO_DEBUG:
762
                optname = SO_DEBUG;
763
                break;
764
        case TARGET_SO_REUSEADDR:
765
                optname = SO_REUSEADDR;
766
                break;
767
        case TARGET_SO_TYPE:
768
                optname = SO_TYPE;
769
                break;
770
        case TARGET_SO_ERROR:
771
                optname = SO_ERROR;
772
                break;
773
        case TARGET_SO_DONTROUTE:
774
                optname = SO_DONTROUTE;
775
                break;
776
        case TARGET_SO_BROADCAST:
777
                optname = SO_BROADCAST;
778
                break;
779
        case TARGET_SO_SNDBUF:
780
                optname = SO_SNDBUF;
781
                break;
782
        case TARGET_SO_RCVBUF:
783
                optname = SO_RCVBUF;
784
                break;
785
        case TARGET_SO_KEEPALIVE:
786
                optname = SO_KEEPALIVE;
787
                break;
788
        case TARGET_SO_OOBINLINE:
789
                optname = SO_OOBINLINE;
790
                break;
791
        case TARGET_SO_NO_CHECK:
792
                optname = SO_NO_CHECK;
793
                break;
794
        case TARGET_SO_PRIORITY:
795
                optname = SO_PRIORITY;
796
                break;
797
#ifdef SO_BSDCOMPAT
798
        case TARGET_SO_BSDCOMPAT:
799
                optname = SO_BSDCOMPAT;
800
                break;
801
#endif
802
        case TARGET_SO_PASSCRED:
803
                optname = SO_PASSCRED;
804
                break;
805
        case TARGET_SO_TIMESTAMP:
806
                optname = SO_TIMESTAMP;
807
                break;
808
        case TARGET_SO_RCVLOWAT:
809
                optname = SO_RCVLOWAT;
810
                break;
811
        case TARGET_SO_RCVTIMEO:
812
                optname = SO_RCVTIMEO;
813
                break;
814
        case TARGET_SO_SNDTIMEO:
815
                optname = SO_SNDTIMEO;
816
                break;
817
            break;
818
        default:
819
            goto unimplemented;
820
        }
821
        if (optlen < sizeof(uint32_t))
822
        return -TARGET_EINVAL;
823

    
824
        val = tget32(optval);
825
        ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
826
        break;
827
    default:
828
    unimplemented:
829
        gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
830
        ret = -TARGET_ENOSYS;
831
    }
832
    return ret;
833
}
834

    
835
/* do_getsockopt() Must return target values and target errnos. */
836
static abi_long do_getsockopt(int sockfd, int level, int optname,
837
                              abi_ulong optval, abi_ulong optlen)
838
{
839
    abi_long ret;
840
    int len, lv, val;
841

    
842
    switch(level) {
843
    case TARGET_SOL_SOCKET:
844
            level = SOL_SOCKET;
845
        switch (optname) {
846
        case TARGET_SO_LINGER:
847
        case TARGET_SO_RCVTIMEO:
848
        case TARGET_SO_SNDTIMEO:
849
        case TARGET_SO_PEERCRED:
850
        case TARGET_SO_PEERNAME:
851
            /* These don't just return a single integer */
852
            goto unimplemented;
853
        default:
854
            goto int_case;
855
        }
856
        break;
857
    case SOL_TCP:
858
        /* TCP options all take an 'int' value.  */
859
    int_case:
860
        len = tget32(optlen);
861
        if (len < 0)
862
            return -TARGET_EINVAL;
863
        lv = sizeof(int);
864
        ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
865
        if (ret < 0)
866
            return ret;
867
        val = tswap32(val);
868
        if (len > lv)
869
            len = lv;
870
        if (len == 4)
871
            tput32(optval, val);
872
        else
873
            tput8(optval, val);
874
        tput32(optlen, len);
875
        break;
876
    case SOL_IP:
877
        switch(optname) {
878
        case IP_TOS:
879
        case IP_TTL:
880
        case IP_HDRINCL:
881
        case IP_ROUTER_ALERT:
882
        case IP_RECVOPTS:
883
        case IP_RETOPTS:
884
        case IP_PKTINFO:
885
        case IP_MTU_DISCOVER:
886
        case IP_RECVERR:
887
        case IP_RECVTOS:
888
#ifdef IP_FREEBIND
889
        case IP_FREEBIND:
890
#endif
891
        case IP_MULTICAST_TTL:
892
        case IP_MULTICAST_LOOP:
893
            len = tget32(optlen);
894
            if (len < 0)
895
                return -TARGET_EINVAL;
896
            lv = sizeof(int);
897
            ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
898
            if (ret < 0)
899
                return ret;
900
            if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
901
                len = 1;
902
                tput32(optlen, len);
903
                tput8(optval, val);
904
            } else {
905
                if (len > sizeof(int))
906
                    len = sizeof(int);
907
                tput32(optlen, len);
908
                tput32(optval, val);
909
            }
910
            break;
911
        default:
912
            goto unimplemented;
913
        }
914
        break;
915
    default:
916
    unimplemented:
917
        gemu_log("getsockopt level=%d optname=%d not yet supported\n",
918
                 level, optname);
919
        ret = -TARGET_ENOSYS;
920
        break;
921
    }
922
    return ret;
923
}
924

    
925
static void lock_iovec(struct iovec *vec, abi_ulong target_addr,
926
                       int count, int copy)
927
{
928
    struct target_iovec *target_vec;
929
    abi_ulong base;
930
    int i;
931

    
932
    target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
933
    for(i = 0;i < count; i++) {
934
        base = tswapl(target_vec[i].iov_base);
935
        vec[i].iov_len = tswapl(target_vec[i].iov_len);
936
        vec[i].iov_base = lock_user(base, vec[i].iov_len, copy);
937
    }
938
    unlock_user (target_vec, target_addr, 0);
939
}
940

    
941
static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
942
                         int count, int copy)
943
{
944
    struct target_iovec *target_vec;
945
    abi_ulong base;
946
    int i;
947

    
948
    target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
949
    for(i = 0;i < count; i++) {
950
        base = tswapl(target_vec[i].iov_base);
951
        unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
952
    }
953
    unlock_user (target_vec, target_addr, 0);
954
}
955

    
956
/* do_socket() Must return target values and target errnos. */
957
static abi_long do_socket(int domain, int type, int protocol)
958
{
959
#if defined(TARGET_MIPS)
960
    switch(type) {
961
    case TARGET_SOCK_DGRAM:
962
        type = SOCK_DGRAM;
963
        break;
964
    case TARGET_SOCK_STREAM:
965
        type = SOCK_STREAM;
966
        break;
967
    case TARGET_SOCK_RAW:
968
        type = SOCK_RAW;
969
        break;
970
    case TARGET_SOCK_RDM:
971
        type = SOCK_RDM;
972
        break;
973
    case TARGET_SOCK_SEQPACKET:
974
        type = SOCK_SEQPACKET;
975
        break;
976
    case TARGET_SOCK_PACKET:
977
        type = SOCK_PACKET;
978
        break;
979
    }
980
#endif
981
    if (domain == PF_NETLINK)
982
        return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
983
    return get_errno(socket(domain, type, protocol));
984
}
985

    
986
/* do_bind() Must return target values and target errnos. */
987
static abi_long do_bind(int sockfd, abi_ulong target_addr,
988
                        socklen_t addrlen)
989
{
990
    void *addr = alloca(addrlen);
991

    
992
    target_to_host_sockaddr(addr, target_addr, addrlen);
993
    return get_errno(bind(sockfd, addr, addrlen));
994
}
995

    
996
/* do_connect() Must return target values and target errnos. */
997
static abi_long do_connect(int sockfd, abi_ulong target_addr,
998
                           socklen_t addrlen)
999
{
1000
    void *addr = alloca(addrlen);
1001

    
1002
    target_to_host_sockaddr(addr, target_addr, addrlen);
1003
    return get_errno(connect(sockfd, addr, addrlen));
1004
}
1005

    
1006
/* do_sendrecvmsg() Must return target values and target errnos. */
1007
static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1008
                               int flags, int send)
1009
{
1010
    abi_long ret;
1011
    struct target_msghdr *msgp;
1012
    struct msghdr msg;
1013
    int count;
1014
    struct iovec *vec;
1015
    abi_ulong target_vec;
1016

    
1017
    lock_user_struct(msgp, target_msg, 1);
1018
    if (msgp->msg_name) {
1019
        msg.msg_namelen = tswap32(msgp->msg_namelen);
1020
        msg.msg_name = alloca(msg.msg_namelen);
1021
        target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1022
                                msg.msg_namelen);
1023
    } else {
1024
        msg.msg_name = NULL;
1025
        msg.msg_namelen = 0;
1026
    }
1027
    msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1028
    msg.msg_control = alloca(msg.msg_controllen);
1029
    msg.msg_flags = tswap32(msgp->msg_flags);
1030

    
1031
    count = tswapl(msgp->msg_iovlen);
1032
    vec = alloca(count * sizeof(struct iovec));
1033
    target_vec = tswapl(msgp->msg_iov);
1034
    lock_iovec(vec, target_vec, count, send);
1035
    msg.msg_iovlen = count;
1036
    msg.msg_iov = vec;
1037

    
1038
    if (send) {
1039
        target_to_host_cmsg(&msg, msgp);
1040
        ret = get_errno(sendmsg(fd, &msg, flags));
1041
    } else {
1042
        ret = get_errno(recvmsg(fd, &msg, flags));
1043
        if (!is_error(ret))
1044
            host_to_target_cmsg(msgp, &msg);
1045
    }
1046
    unlock_iovec(vec, target_vec, count, !send);
1047
    return ret;
1048
}
1049

    
1050
/* do_accept() Must return target values and target errnos. */
1051
static abi_long do_accept(int fd, abi_ulong target_addr,
1052
                          abi_ulong target_addrlen)
1053
{
1054
    socklen_t addrlen = tget32(target_addrlen);
1055
    void *addr = alloca(addrlen);
1056
    abi_long ret;
1057

    
1058
    ret = get_errno(accept(fd, addr, &addrlen));
1059
    if (!is_error(ret)) {
1060
        host_to_target_sockaddr(target_addr, addr, addrlen);
1061
        tput32(target_addrlen, addrlen);
1062
    }
1063
    return ret;
1064
}
1065

    
1066
/* do_getpeername() Must return target values and target errnos. */
1067
static abi_long do_getpeername(int fd, abi_ulong target_addr,
1068
                               abi_ulong target_addrlen)
1069
{
1070
    socklen_t addrlen = tget32(target_addrlen);
1071
    void *addr = alloca(addrlen);
1072
    abi_long ret;
1073

    
1074
    ret = get_errno(getpeername(fd, addr, &addrlen));
1075
    if (!is_error(ret)) {
1076
        host_to_target_sockaddr(target_addr, addr, addrlen);
1077
        tput32(target_addrlen, addrlen);
1078
    }
1079
    return ret;
1080
}
1081

    
1082
/* do_getsockname() Must return target values and target errnos. */
1083
static abi_long do_getsockname(int fd, abi_ulong target_addr,
1084
                               abi_ulong target_addrlen)
1085
{
1086
    socklen_t addrlen = tget32(target_addrlen);
1087
    void *addr = alloca(addrlen);
1088
    abi_long ret;
1089

    
1090
    ret = get_errno(getsockname(fd, addr, &addrlen));
1091
    if (!is_error(ret)) {
1092
        host_to_target_sockaddr(target_addr, addr, addrlen);
1093
        tput32(target_addrlen, addrlen);
1094
    }
1095
    return ret;
1096
}
1097

    
1098
/* do_socketpair() Must return target values and target errnos. */
1099
static abi_long do_socketpair(int domain, int type, int protocol,
1100
                              abi_ulong target_tab)
1101
{
1102
    int tab[2];
1103
    abi_long ret;
1104

    
1105
    ret = get_errno(socketpair(domain, type, protocol, tab));
1106
    if (!is_error(ret)) {
1107
        tput32(target_tab, tab[0]);
1108
        tput32(target_tab + 4, tab[1]);
1109
    }
1110
    return ret;
1111
}
1112

    
1113
/* do_sendto() Must return target values and target errnos. */
1114
static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1115
                          abi_ulong target_addr, socklen_t addrlen)
1116
{
1117
    void *addr;
1118
    void *host_msg;
1119
    abi_long ret;
1120

    
1121
    host_msg = lock_user(msg, len, 1);
1122
    if (target_addr) {
1123
        addr = alloca(addrlen);
1124
        target_to_host_sockaddr(addr, target_addr, addrlen);
1125
        ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1126
    } else {
1127
        ret = get_errno(send(fd, host_msg, len, flags));
1128
    }
1129
    unlock_user(host_msg, msg, 0);
1130
    return ret;
1131
}
1132

    
1133
/* do_recvfrom() Must return target values and target errnos. */
1134
static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1135
                            abi_ulong target_addr,
1136
                            abi_ulong target_addrlen)
1137
{
1138
    socklen_t addrlen;
1139
    void *addr;
1140
    void *host_msg;
1141
    abi_long ret;
1142

    
1143
    host_msg = lock_user(msg, len, 0);
1144
    if (target_addr) {
1145
        addrlen = tget32(target_addrlen);
1146
        addr = alloca(addrlen);
1147
        ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1148
    } else {
1149
        addr = NULL; /* To keep compiler quiet.  */
1150
        ret = get_errno(recv(fd, host_msg, len, flags));
1151
    }
1152
    if (!is_error(ret)) {
1153
        if (target_addr) {
1154
            host_to_target_sockaddr(target_addr, addr, addrlen);
1155
            tput32(target_addrlen, addrlen);
1156
        }
1157
        unlock_user(host_msg, msg, len);
1158
    } else {
1159
        unlock_user(host_msg, msg, 0);
1160
    }
1161
    return ret;
1162
}
1163

    
1164
#ifdef TARGET_NR_socketcall
1165
/* do_socketcall() Must return target values and target errnos. */
1166
static abi_long do_socketcall(int num, abi_ulong vptr)
1167
{
1168
    abi_long ret;
1169
    const int n = sizeof(abi_ulong);
1170

    
1171
    switch(num) {
1172
    case SOCKOP_socket:
1173
        {
1174
            int domain = tgetl(vptr);
1175
            int type = tgetl(vptr + n);
1176
            int protocol = tgetl(vptr + 2 * n);
1177
            ret = do_socket(domain, type, protocol);
1178
        }
1179
        break;
1180
    case SOCKOP_bind:
1181
        {
1182
            int sockfd = tgetl(vptr);
1183
            abi_ulong target_addr = tgetl(vptr + n);
1184
            socklen_t addrlen = tgetl(vptr + 2 * n);
1185
            ret = do_bind(sockfd, target_addr, addrlen);
1186
        }
1187
        break;
1188
    case SOCKOP_connect:
1189
        {
1190
            int sockfd = tgetl(vptr);
1191
            abi_ulong target_addr = tgetl(vptr + n);
1192
            socklen_t addrlen = tgetl(vptr + 2 * n);
1193
            ret = do_connect(sockfd, target_addr, addrlen);
1194
        }
1195
        break;
1196
    case SOCKOP_listen:
1197
        {
1198
            int sockfd = tgetl(vptr);
1199
            int backlog = tgetl(vptr + n);
1200
            ret = get_errno(listen(sockfd, backlog));
1201
        }
1202
        break;
1203
    case SOCKOP_accept:
1204
        {
1205
            int sockfd = tgetl(vptr);
1206
            abi_ulong target_addr = tgetl(vptr + n);
1207
            abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1208
            ret = do_accept(sockfd, target_addr, target_addrlen);
1209
        }
1210
        break;
1211
    case SOCKOP_getsockname:
1212
        {
1213
            int sockfd = tgetl(vptr);
1214
            abi_ulong target_addr = tgetl(vptr + n);
1215
            abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1216
            ret = do_getsockname(sockfd, target_addr, target_addrlen);
1217
        }
1218
        break;
1219
    case SOCKOP_getpeername:
1220
        {
1221
            int sockfd = tgetl(vptr);
1222
            abi_ulong target_addr = tgetl(vptr + n);
1223
            abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1224
            ret = do_getpeername(sockfd, target_addr, target_addrlen);
1225
        }
1226
        break;
1227
    case SOCKOP_socketpair:
1228
        {
1229
            int domain = tgetl(vptr);
1230
            int type = tgetl(vptr + n);
1231
            int protocol = tgetl(vptr + 2 * n);
1232
            abi_ulong tab = tgetl(vptr + 3 * n);
1233
            ret = do_socketpair(domain, type, protocol, tab);
1234
        }
1235
        break;
1236
    case SOCKOP_send:
1237
        {
1238
            int sockfd = tgetl(vptr);
1239
            abi_ulong msg = tgetl(vptr + n);
1240
            size_t len = tgetl(vptr + 2 * n);
1241
            int flags = tgetl(vptr + 3 * n);
1242
            ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1243
        }
1244
        break;
1245
    case SOCKOP_recv:
1246
        {
1247
            int sockfd = tgetl(vptr);
1248
            abi_ulong msg = tgetl(vptr + n);
1249
            size_t len = tgetl(vptr + 2 * n);
1250
            int flags = tgetl(vptr + 3 * n);
1251
            ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1252
        }
1253
        break;
1254
    case SOCKOP_sendto:
1255
        {
1256
            int sockfd = tgetl(vptr);
1257
            abi_ulong msg = tgetl(vptr + n);
1258
            size_t len = tgetl(vptr + 2 * n);
1259
            int flags = tgetl(vptr + 3 * n);
1260
            abi_ulong addr = tgetl(vptr + 4 * n);
1261
            socklen_t addrlen = tgetl(vptr + 5 * n);
1262
            ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1263
        }
1264
        break;
1265
    case SOCKOP_recvfrom:
1266
        {
1267
            int sockfd = tgetl(vptr);
1268
            abi_ulong msg = tgetl(vptr + n);
1269
            size_t len = tgetl(vptr + 2 * n);
1270
            int flags = tgetl(vptr + 3 * n);
1271
            abi_ulong addr = tgetl(vptr + 4 * n);
1272
            abi_ulong addrlen = tgetl(vptr + 5 * n);
1273
            ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1274
        }
1275
        break;
1276
    case SOCKOP_shutdown:
1277
        {
1278
            int sockfd = tgetl(vptr);
1279
            int how = tgetl(vptr + n);
1280

    
1281
            ret = get_errno(shutdown(sockfd, how));
1282
        }
1283
        break;
1284
    case SOCKOP_sendmsg:
1285
    case SOCKOP_recvmsg:
1286
        {
1287
            int fd;
1288
            abi_ulong target_msg;
1289
            int flags;
1290

    
1291
            fd = tgetl(vptr);
1292
            target_msg = tgetl(vptr + n);
1293
            flags = tgetl(vptr + 2 * n);
1294

    
1295
            ret = do_sendrecvmsg(fd, target_msg, flags,
1296
                                 (num == SOCKOP_sendmsg));
1297
        }
1298
        break;
1299
    case SOCKOP_setsockopt:
1300
        {
1301
            int sockfd = tgetl(vptr);
1302
            int level = tgetl(vptr + n);
1303
            int optname = tgetl(vptr + 2 * n);
1304
            abi_ulong optval = tgetl(vptr + 3 * n);
1305
            socklen_t optlen = tgetl(vptr + 4 * n);
1306

    
1307
            ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1308
        }
1309
        break;
1310
    case SOCKOP_getsockopt:
1311
        {
1312
            int sockfd = tgetl(vptr);
1313
            int level = tgetl(vptr + n);
1314
            int optname = tgetl(vptr + 2 * n);
1315
            abi_ulong optval = tgetl(vptr + 3 * n);
1316
            abi_ulong poptlen = tgetl(vptr + 4 * n);
1317

    
1318
            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
1319
        }
1320
        break;
1321
    default:
1322
        gemu_log("Unsupported socketcall: %d\n", num);
1323
        ret = -TARGET_ENOSYS;
1324
        break;
1325
    }
1326
    return ret;
1327
}
1328
#endif
1329

    
1330
#ifdef TARGET_NR_ipc
1331
#define N_SHM_REGIONS        32
1332

    
1333
static struct shm_region {
1334
    uint32_t        start;
1335
    uint32_t        size;
1336
} shm_regions[N_SHM_REGIONS];
1337

    
1338
struct target_ipc_perm
1339
{
1340
    abi_long __key;
1341
    abi_ulong uid;
1342
    abi_ulong gid;
1343
    abi_ulong cuid;
1344
    abi_ulong cgid;
1345
    unsigned short int mode;
1346
    unsigned short int __pad1;
1347
    unsigned short int __seq;
1348
    unsigned short int __pad2;
1349
    abi_ulong __unused1;
1350
    abi_ulong __unused2;
1351
};
1352

    
1353
struct target_semid_ds
1354
{
1355
  struct target_ipc_perm sem_perm;
1356
  abi_ulong sem_otime;
1357
  abi_ulong __unused1;
1358
  abi_ulong sem_ctime;
1359
  abi_ulong __unused2;
1360
  abi_ulong sem_nsems;
1361
  abi_ulong __unused3;
1362
  abi_ulong __unused4;
1363
};
1364

    
1365
static inline void target_to_host_ipc_perm(struct ipc_perm *host_ip,
1366
                                           abi_ulong target_addr)
1367
{
1368
    struct target_ipc_perm *target_ip;
1369
    struct target_semid_ds *target_sd;
1370

    
1371
    lock_user_struct(target_sd, target_addr, 1);
1372
    target_ip=&(target_sd->sem_perm);
1373
    host_ip->__key = tswapl(target_ip->__key);
1374
    host_ip->uid = tswapl(target_ip->uid);
1375
    host_ip->gid = tswapl(target_ip->gid);
1376
    host_ip->cuid = tswapl(target_ip->cuid);
1377
    host_ip->cgid = tswapl(target_ip->cgid);
1378
    host_ip->mode = tswapl(target_ip->mode);
1379
    unlock_user_struct(target_sd, target_addr, 0);
1380
}
1381

    
1382
static inline void host_to_target_ipc_perm(abi_ulong target_addr,
1383
                                           struct ipc_perm *host_ip)
1384
{
1385
    struct target_ipc_perm *target_ip;
1386
    struct target_semid_ds *target_sd;
1387

    
1388
    lock_user_struct(target_sd, target_addr, 0);
1389
    target_ip = &(target_sd->sem_perm);
1390
    target_ip->__key = tswapl(host_ip->__key);
1391
    target_ip->uid = tswapl(host_ip->uid);
1392
    target_ip->gid = tswapl(host_ip->gid);
1393
    target_ip->cuid = tswapl(host_ip->cuid);
1394
    target_ip->cgid = tswapl(host_ip->cgid);
1395
    target_ip->mode = tswapl(host_ip->mode);
1396
    unlock_user_struct(target_sd, target_addr, 1);
1397
}
1398

    
1399
static inline void target_to_host_semid_ds(struct semid_ds *host_sd,
1400
                                          abi_ulong target_addr)
1401
{
1402
    struct target_semid_ds *target_sd;
1403

    
1404
    lock_user_struct(target_sd, target_addr, 1);
1405
    target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1406
    host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1407
    host_sd->sem_otime = tswapl(target_sd->sem_otime);
1408
    host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1409
    unlock_user_struct(target_sd, target_addr, 0);
1410
}
1411

    
1412
static inline void host_to_target_semid_ds(abi_ulong target_addr,
1413
                                           struct semid_ds *host_sd)
1414
{
1415
    struct target_semid_ds *target_sd;
1416

    
1417
    lock_user_struct(target_sd, target_addr, 0);
1418
    host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1419
    target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1420
    target_sd->sem_otime = tswapl(host_sd->sem_otime);
1421
    target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1422
    unlock_user_struct(target_sd, target_addr, 1);
1423
}
1424

    
1425
union semun {
1426
        int val;
1427
        struct semid_ds *buf;
1428
        unsigned short *array;
1429
};
1430

    
1431
union target_semun {
1432
        int val;
1433
        abi_long buf;
1434
        unsigned short int *array;
1435
};
1436

    
1437
static inline void target_to_host_semun(int cmd,
1438
                                        union semun *host_su,
1439
                                        abi_ulong target_addr,
1440
                                        struct semid_ds *ds)
1441
{
1442
    union target_semun *target_su;
1443

    
1444
    switch( cmd ) {
1445
        case IPC_STAT:
1446
        case IPC_SET:
1447
           lock_user_struct(target_su, target_addr, 1);
1448
           target_to_host_semid_ds(ds,target_su->buf);
1449
           host_su->buf = ds;
1450
           unlock_user_struct(target_su, target_addr, 0);
1451
           break;
1452
        case GETVAL:
1453
        case SETVAL:
1454
           lock_user_struct(target_su, target_addr, 1);
1455
           host_su->val = tswapl(target_su->val);
1456
           unlock_user_struct(target_su, target_addr, 0);
1457
           break;
1458
        case GETALL:
1459
        case SETALL:
1460
           lock_user_struct(target_su, target_addr, 1);
1461
           *host_su->array = tswap16(*target_su->array);
1462
           unlock_user_struct(target_su, target_addr, 0);
1463
           break;
1464
        default:
1465
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1466
    }
1467
}
1468

    
1469
static inline void host_to_target_semun(int cmd,
1470
                                        abi_ulong target_addr,
1471
                                        union semun *host_su,
1472
                                        struct semid_ds *ds)
1473
{
1474
    union target_semun *target_su;
1475

    
1476
    switch( cmd ) {
1477
        case IPC_STAT:
1478
        case IPC_SET:
1479
           lock_user_struct(target_su, target_addr, 0);
1480
           host_to_target_semid_ds(target_su->buf,ds);
1481
           unlock_user_struct(target_su, target_addr, 1);
1482
           break;
1483
        case GETVAL:
1484
        case SETVAL:
1485
           lock_user_struct(target_su, target_addr, 0);
1486
           target_su->val = tswapl(host_su->val);
1487
           unlock_user_struct(target_su, target_addr, 1);
1488
           break;
1489
        case GETALL:
1490
        case SETALL:
1491
           lock_user_struct(target_su, target_addr, 0);
1492
           *target_su->array = tswap16(*host_su->array);
1493
           unlock_user_struct(target_su, target_addr, 1);
1494
           break;
1495
        default:
1496
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1497
    }
1498
}
1499

    
1500
static inline abi_long do_semctl(int first, int second, int third,
1501
                                 abi_long ptr)
1502
{
1503
    union semun arg;
1504
    struct semid_ds dsarg;
1505
    int cmd = third&0xff;
1506
    abi_long ret = 0;
1507

    
1508
    switch( cmd ) {
1509
        case GETVAL:
1510
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1511
            ret = get_errno(semctl(first, second, cmd, arg));
1512
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1513
            break;
1514
        case SETVAL:
1515
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1516
            ret = get_errno(semctl(first, second, cmd, arg));
1517
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1518
            break;
1519
        case GETALL:
1520
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1521
            ret = get_errno(semctl(first, second, cmd, arg));
1522
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1523
            break;
1524
        case SETALL:
1525
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1526
            ret = get_errno(semctl(first, second, cmd, arg));
1527
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1528
            break;
1529
        case IPC_STAT:
1530
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1531
            ret = get_errno(semctl(first, second, cmd, arg));
1532
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1533
            break;
1534
        case IPC_SET:
1535
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1536
            ret = get_errno(semctl(first, second, cmd, arg));
1537
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1538
            break;
1539
    default:
1540
            ret = get_errno(semctl(first, second, cmd, arg));
1541
    }
1542

    
1543
    return ret;
1544
}
1545

    
1546
struct target_msqid_ds
1547
{
1548
  struct target_ipc_perm msg_perm;
1549
  abi_ulong msg_stime;
1550
  abi_ulong __unused1;
1551
  abi_ulong msg_rtime;
1552
  abi_ulong __unused2;
1553
  abi_ulong msg_ctime;
1554
  abi_ulong __unused3;
1555
  abi_ulong __msg_cbytes;
1556
  abi_ulong msg_qnum;
1557
  abi_ulong msg_qbytes;
1558
  abi_ulong msg_lspid;
1559
  abi_ulong msg_lrpid;
1560
  abi_ulong __unused4;
1561
  abi_ulong __unused5;
1562
};
1563

    
1564
static inline void target_to_host_msqid_ds(struct msqid_ds *host_md,
1565
                                           abi_ulong target_addr)
1566
{
1567
    struct target_msqid_ds *target_md;
1568

    
1569
    lock_user_struct(target_md, target_addr, 1);
1570
    target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1571
    host_md->msg_stime = tswapl(target_md->msg_stime);
1572
    host_md->msg_rtime = tswapl(target_md->msg_rtime);
1573
    host_md->msg_ctime = tswapl(target_md->msg_ctime);
1574
    host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1575
    host_md->msg_qnum = tswapl(target_md->msg_qnum);
1576
    host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1577
    host_md->msg_lspid = tswapl(target_md->msg_lspid);
1578
    host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1579
    unlock_user_struct(target_md, target_addr, 0);
1580
}
1581

    
1582
static inline void host_to_target_msqid_ds(abi_ulong target_addr,
1583
                                           struct msqid_ds *host_md)
1584
{
1585
    struct target_msqid_ds *target_md;
1586

    
1587
    lock_user_struct(target_md, target_addr, 0);
1588
    host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1589
    target_md->msg_stime = tswapl(host_md->msg_stime);
1590
    target_md->msg_rtime = tswapl(host_md->msg_rtime);
1591
    target_md->msg_ctime = tswapl(host_md->msg_ctime);
1592
    target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1593
    target_md->msg_qnum = tswapl(host_md->msg_qnum);
1594
    target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1595
    target_md->msg_lspid = tswapl(host_md->msg_lspid);
1596
    target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1597
    unlock_user_struct(target_md, target_addr, 1);
1598
}
1599

    
1600
static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1601
{
1602
    struct msqid_ds dsarg;
1603
    int cmd = second&0xff;
1604
    abi_long ret = 0;
1605
    switch( cmd ) {
1606
    case IPC_STAT:
1607
    case IPC_SET:
1608
        target_to_host_msqid_ds(&dsarg,ptr);
1609
        ret = get_errno(msgctl(first, cmd, &dsarg));
1610
        host_to_target_msqid_ds(ptr,&dsarg);
1611
    default:
1612
        ret = get_errno(msgctl(first, cmd, &dsarg));
1613
    }
1614
    return ret;
1615
}
1616

    
1617
struct target_msgbuf {
1618
        abi_ulong mtype;
1619
        char        mtext[1];
1620
};
1621

    
1622
static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1623
                                 unsigned int msgsz, int msgflg)
1624
{
1625
    struct target_msgbuf *target_mb;
1626
    struct msgbuf *host_mb;
1627
    abi_long ret = 0;
1628

    
1629
    lock_user_struct(target_mb,msgp,0);
1630
    host_mb = malloc(msgsz+sizeof(long));
1631
    host_mb->mtype = tswapl(target_mb->mtype);
1632
    memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1633
    ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1634
    free(host_mb);
1635
    unlock_user_struct(target_mb, msgp, 0);
1636

    
1637
    return ret;
1638
}
1639

    
1640
static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1641
                                 unsigned int msgsz, int msgtype,
1642
                                 int msgflg)
1643
{
1644
    struct target_msgbuf *target_mb;
1645
    struct msgbuf *host_mb;
1646
    abi_long ret = 0;
1647

    
1648
    lock_user_struct(target_mb, msgp, 0);
1649
    host_mb = malloc(msgsz+sizeof(long));
1650
    ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1651
    if (ret > 0)
1652
            memcpy(target_mb->mtext, host_mb->mtext, ret);
1653
    target_mb->mtype = tswapl(host_mb->mtype);
1654
    free(host_mb);
1655
    unlock_user_struct(target_mb, msgp, 0);
1656

    
1657
    return ret;
1658
}
1659

    
1660
/* ??? This only works with linear mappings.  */
1661
/* do_ipc() must return target values and target errnos. */
1662
static abi_long do_ipc(unsigned int call, int first,
1663
                       int second, int third,
1664
                       abi_long ptr, abi_long fifth)
1665
{
1666
    int version;
1667
    abi_long ret = 0;
1668
    unsigned long raddr;
1669
    struct shmid_ds shm_info;
1670
    int i;
1671

    
1672
    version = call >> 16;
1673
    call &= 0xffff;
1674

    
1675
    switch (call) {
1676
    case IPCOP_semop:
1677
        ret = get_errno(semop(first,(struct sembuf *) ptr, second));
1678
        break;
1679

    
1680
    case IPCOP_semget:
1681
        ret = get_errno(semget(first, second, third));
1682
        break;
1683

    
1684
    case IPCOP_semctl:
1685
        ret = do_semctl(first, second, third, ptr);
1686
        break;
1687

    
1688
    case IPCOP_semtimedop:
1689
        gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1690
        ret = -TARGET_ENOSYS;
1691
        break;
1692

    
1693
        case IPCOP_msgget:
1694
                ret = get_errno(msgget(first, second));
1695
                break;
1696

    
1697
        case IPCOP_msgsnd:
1698
                ret = do_msgsnd(first, ptr, second, third);
1699
                break;
1700

    
1701
        case IPCOP_msgctl:
1702
                ret = do_msgctl(first, second, ptr);
1703
                break;
1704

    
1705
        case IPCOP_msgrcv:
1706
                {
1707
                      struct ipc_kludge
1708
                      {
1709
                              void *__unbounded msgp;
1710
                              long int msgtyp;
1711
                      };
1712

    
1713
                      struct ipc_kludge *foo = (struct ipc_kludge *) ptr;
1714
                      struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
1715

    
1716
                      ret = do_msgrcv(first, (long)msgp, second, 0, third);
1717

    
1718
                }
1719
                break;
1720

    
1721
    case IPCOP_shmat:
1722
        /* SHM_* flags are the same on all linux platforms */
1723
        ret = get_errno((long) shmat(first, (void *) ptr, second));
1724
        if (is_error(ret))
1725
            break;
1726
        raddr = ret;
1727
        /* find out the length of the shared memory segment */
1728

    
1729
        ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1730
        if (is_error(ret)) {
1731
            /* can't get length, bail out */
1732
            shmdt((void *) raddr);
1733
            break;
1734
        }
1735
        page_set_flags(raddr, raddr + shm_info.shm_segsz,
1736
                       PAGE_VALID | PAGE_READ |
1737
                       ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1738
        for (i = 0; i < N_SHM_REGIONS; ++i) {
1739
            if (shm_regions[i].start == 0) {
1740
                shm_regions[i].start = raddr;
1741
                shm_regions[i].size = shm_info.shm_segsz;
1742
                break;
1743
            }
1744
        }
1745
        if (put_user(raddr, (abi_ulong *)third))
1746
            return -TARGET_EFAULT;
1747
        ret = 0;
1748
        break;
1749
    case IPCOP_shmdt:
1750
        for (i = 0; i < N_SHM_REGIONS; ++i) {
1751
            if (shm_regions[i].start == ptr) {
1752
                shm_regions[i].start = 0;
1753
                page_set_flags(ptr, shm_regions[i].size, 0);
1754
                break;
1755
            }
1756
        }
1757
        ret = get_errno(shmdt((void *) ptr));
1758
        break;
1759

    
1760
    case IPCOP_shmget:
1761
        /* IPC_* flag values are the same on all linux platforms */
1762
        ret = get_errno(shmget(first, second, third));
1763
        break;
1764

    
1765
        /* IPC_* and SHM_* command values are the same on all linux platforms */
1766
    case IPCOP_shmctl:
1767
        switch(second) {
1768
        case IPC_RMID:
1769
        case SHM_LOCK:
1770
        case SHM_UNLOCK:
1771
            ret = get_errno(shmctl(first, second, NULL));
1772
            break;
1773
        default:
1774
            goto unimplemented;
1775
        }
1776
        break;
1777
    default:
1778
    unimplemented:
1779
        gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1780
        ret = -TARGET_ENOSYS;
1781
        break;
1782
    }
1783
    return ret;
1784
}
1785
#endif
1786

    
1787
/* kernel structure types definitions */
1788
#define IFNAMSIZ        16
1789

    
1790
#define STRUCT(name, list...) STRUCT_ ## name,
1791
#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1792
enum {
1793
#include "syscall_types.h"
1794
};
1795
#undef STRUCT
1796
#undef STRUCT_SPECIAL
1797

    
1798
#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1799
#define STRUCT_SPECIAL(name)
1800
#include "syscall_types.h"
1801
#undef STRUCT
1802
#undef STRUCT_SPECIAL
1803

    
1804
typedef struct IOCTLEntry {
1805
    unsigned int target_cmd;
1806
    unsigned int host_cmd;
1807
    const char *name;
1808
    int access;
1809
    const argtype arg_type[5];
1810
} IOCTLEntry;
1811

    
1812
#define IOC_R 0x0001
1813
#define IOC_W 0x0002
1814
#define IOC_RW (IOC_R | IOC_W)
1815

    
1816
#define MAX_STRUCT_SIZE 4096
1817

    
1818
IOCTLEntry ioctl_entries[] = {
1819
#define IOCTL(cmd, access, types...) \
1820
    { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1821
#include "ioctls.h"
1822
    { 0, 0, },
1823
};
1824

    
1825
/* ??? Implement proper locking for ioctls.  */
1826
/* do_ioctl() Must return target values and target errnos. */
1827
static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
1828
{
1829
    const IOCTLEntry *ie;
1830
    const argtype *arg_type;
1831
    abi_long ret;
1832
    uint8_t buf_temp[MAX_STRUCT_SIZE];
1833
    int target_size;
1834
    void *argptr;
1835

    
1836
    ie = ioctl_entries;
1837
    for(;;) {
1838
        if (ie->target_cmd == 0) {
1839
            gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
1840
            return -TARGET_ENOSYS;
1841
        }
1842
        if (ie->target_cmd == cmd)
1843
            break;
1844
        ie++;
1845
    }
1846
    arg_type = ie->arg_type;
1847
#if defined(DEBUG)
1848
    gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
1849
#endif
1850
    switch(arg_type[0]) {
1851
    case TYPE_NULL:
1852
        /* no argument */
1853
        ret = get_errno(ioctl(fd, ie->host_cmd));
1854
        break;
1855
    case TYPE_PTRVOID:
1856
    case TYPE_INT:
1857
        /* int argment */
1858
        ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1859
        break;
1860
    case TYPE_PTR:
1861
        arg_type++;
1862
        target_size = thunk_type_size(arg_type, 0);
1863
        switch(ie->access) {
1864
        case IOC_R:
1865
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1866
            if (!is_error(ret)) {
1867
                argptr = lock_user(arg, target_size, 0);
1868
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1869
                unlock_user(argptr, arg, target_size);
1870
            }
1871
            break;
1872
        case IOC_W:
1873
            argptr = lock_user(arg, target_size, 1);
1874
            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1875
            unlock_user(argptr, arg, 0);
1876
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1877
            break;
1878
        default:
1879
        case IOC_RW:
1880
            argptr = lock_user(arg, target_size, 1);
1881
            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1882
            unlock_user(argptr, arg, 0);
1883
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1884
            if (!is_error(ret)) {
1885
                argptr = lock_user(arg, target_size, 0);
1886
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1887
                unlock_user(argptr, arg, target_size);
1888
            }
1889
            break;
1890
        }
1891
        break;
1892
    default:
1893
        gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
1894
                 (long)cmd, arg_type[0]);
1895
        ret = -TARGET_ENOSYS;
1896
        break;
1897
    }
1898
    return ret;
1899
}
1900

    
1901
bitmask_transtbl iflag_tbl[] = {
1902
        { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1903
        { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1904
        { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1905
        { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1906
        { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1907
        { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1908
        { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1909
        { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1910
        { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1911
        { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1912
        { TARGET_IXON, TARGET_IXON, IXON, IXON },
1913
        { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1914
        { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1915
        { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1916
        { 0, 0, 0, 0 }
1917
};
1918

    
1919
bitmask_transtbl oflag_tbl[] = {
1920
        { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1921
        { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1922
        { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1923
        { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1924
        { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1925
        { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1926
        { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1927
        { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1928
        { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1929
        { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1930
        { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1931
        { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1932
        { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1933
        { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1934
        { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1935
        { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1936
        { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1937
        { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1938
        { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1939
        { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1940
        { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1941
        { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1942
        { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1943
        { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1944
        { 0, 0, 0, 0 }
1945
};
1946

    
1947
bitmask_transtbl cflag_tbl[] = {
1948
        { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1949
        { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1950
        { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1951
        { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1952
        { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1953
        { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1954
        { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1955
        { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1956
        { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1957
        { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1958
        { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1959
        { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1960
        { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1961
        { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1962
        { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1963
        { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1964
        { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1965
        { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1966
        { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1967
        { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1968
        { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1969
        { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1970
        { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1971
        { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1972
        { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1973
        { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1974
        { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1975
        { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1976
        { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1977
        { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1978
        { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1979
        { 0, 0, 0, 0 }
1980
};
1981

    
1982
bitmask_transtbl lflag_tbl[] = {
1983
        { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1984
        { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1985
        { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1986
        { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1987
        { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1988
        { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1989
        { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1990
        { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1991
        { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1992
        { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1993
        { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1994
        { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1995
        { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1996
        { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1997
        { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1998
        { 0, 0, 0, 0 }
1999
};
2000

    
2001
static void target_to_host_termios (void *dst, const void *src)
2002
{
2003
    struct host_termios *host = dst;
2004
    const struct target_termios *target = src;
2005

    
2006
    host->c_iflag =
2007
        target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2008
    host->c_oflag =
2009
        target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2010
    host->c_cflag =
2011
        target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2012
    host->c_lflag =
2013
        target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2014
    host->c_line = target->c_line;
2015

    
2016
    host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2017
    host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2018
    host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2019
    host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2020
    host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2021
    host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2022
    host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2023
    host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2024
    host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2025
    host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2026
    host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2027
    host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2028
    host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2029
    host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2030
    host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2031
    host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2032
    host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2033
}
2034

    
2035
static void host_to_target_termios (void *dst, const void *src)
2036
{
2037
    struct target_termios *target = dst;
2038
    const struct host_termios *host = src;
2039

    
2040
    target->c_iflag =
2041
        tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2042
    target->c_oflag =
2043
        tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2044
    target->c_cflag =
2045
        tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2046
    target->c_lflag =
2047
        tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2048
    target->c_line = host->c_line;
2049

    
2050
    target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2051
    target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2052
    target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2053
    target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2054
    target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2055
    target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2056
    target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2057
    target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2058
    target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2059
    target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2060
    target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2061
    target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2062
    target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2063
    target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2064
    target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2065
    target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2066
    target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2067
}
2068

    
2069
StructEntry struct_termios_def = {
2070
    .convert = { host_to_target_termios, target_to_host_termios },
2071
    .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2072
    .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2073
};
2074

    
2075
static bitmask_transtbl mmap_flags_tbl[] = {
2076
        { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2077
        { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2078
        { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2079
        { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2080
        { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2081
        { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2082
        { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2083
        { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2084
        { 0, 0, 0, 0 }
2085
};
2086

    
2087
static bitmask_transtbl fcntl_flags_tbl[] = {
2088
        { TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
2089
        { TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
2090
        { TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
2091
        { TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
2092
        { TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
2093
        { TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
2094
        { TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
2095
        { TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
2096
        { TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
2097
        { TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
2098
        { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2099
        { TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
2100
        { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2101
#if defined(O_DIRECT)
2102
        { TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
2103
#endif
2104
        { 0, 0, 0, 0 }
2105
};
2106

    
2107
#if defined(TARGET_I386)
2108

    
2109
/* NOTE: there is really one LDT for all the threads */
2110
uint8_t *ldt_table;
2111

    
2112
static int read_ldt(abi_ulong ptr, unsigned long bytecount)
2113
{
2114
    int size;
2115
    void *p;
2116

    
2117
    if (!ldt_table)
2118
        return 0;
2119
    size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2120
    if (size > bytecount)
2121
        size = bytecount;
2122
    p = lock_user(ptr, size, 0);
2123
    /* ??? Shoudl this by byteswapped?  */
2124
    memcpy(p, ldt_table, size);
2125
    unlock_user(p, ptr, size);
2126
    return size;
2127
}
2128

    
2129
/* XXX: add locking support */
2130
/* write_ldt() returns host errnos */
2131
static int write_ldt(CPUX86State *env,
2132
                     abi_ulong ptr, unsigned long bytecount, int oldmode)
2133
{
2134
    struct target_modify_ldt_ldt_s ldt_info;
2135
    struct target_modify_ldt_ldt_s *target_ldt_info;
2136
    int seg_32bit, contents, read_exec_only, limit_in_pages;
2137
    int seg_not_present, useable;
2138
    uint32_t *lp, entry_1, entry_2;
2139

    
2140
    if (bytecount != sizeof(ldt_info))
2141
        return -EINVAL;
2142
    lock_user_struct(target_ldt_info, ptr, 1);
2143
    ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2144
    ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2145
    ldt_info.limit = tswap32(target_ldt_info->limit);
2146
    ldt_info.flags = tswap32(target_ldt_info->flags);
2147
    unlock_user_struct(target_ldt_info, ptr, 0);
2148

    
2149
    if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2150
        return -EINVAL;
2151
    seg_32bit = ldt_info.flags & 1;
2152
    contents = (ldt_info.flags >> 1) & 3;
2153
    read_exec_only = (ldt_info.flags >> 3) & 1;
2154
    limit_in_pages = (ldt_info.flags >> 4) & 1;
2155
    seg_not_present = (ldt_info.flags >> 5) & 1;
2156
    useable = (ldt_info.flags >> 6) & 1;
2157

    
2158
    if (contents == 3) {
2159
        if (oldmode)
2160
            return -EINVAL;
2161
        if (seg_not_present == 0)
2162
            return -EINVAL;
2163
    }
2164
    /* allocate the LDT */
2165
    if (!ldt_table) {
2166
        ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2167
        if (!ldt_table)
2168
            return -ENOMEM;
2169
        memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2170
        env->ldt.base = h2g(ldt_table);
2171
        env->ldt.limit = 0xffff;
2172
    }
2173

    
2174
    /* NOTE: same code as Linux kernel */
2175
    /* Allow LDTs to be cleared by the user. */
2176
    if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2177
        if (oldmode ||
2178
            (contents == 0                &&
2179
             read_exec_only == 1        &&
2180
             seg_32bit == 0                &&
2181
             limit_in_pages == 0        &&
2182
             seg_not_present == 1        &&
2183
             useable == 0 )) {
2184
            entry_1 = 0;
2185
            entry_2 = 0;
2186
            goto install;
2187
        }
2188
    }
2189

    
2190
    entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2191
        (ldt_info.limit & 0x0ffff);
2192
    entry_2 = (ldt_info.base_addr & 0xff000000) |
2193
        ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2194
        (ldt_info.limit & 0xf0000) |
2195
        ((read_exec_only ^ 1) << 9) |
2196
        (contents << 10) |
2197
        ((seg_not_present ^ 1) << 15) |
2198
        (seg_32bit << 22) |
2199
        (limit_in_pages << 23) |
2200
        0x7000;
2201
    if (!oldmode)
2202
        entry_2 |= (useable << 20);
2203

    
2204
    /* Install the new entry ...  */
2205
install:
2206
    lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2207
    lp[0] = tswap32(entry_1);
2208
    lp[1] = tswap32(entry_2);
2209
    return 0;
2210
}
2211

    
2212
/* specific and weird i386 syscalls */
2213
/* do_modify_ldt() returns host errnos (it is inconsistent with the
2214
   other do_*() functions which return target errnos). */
2215
int do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, unsigned long bytecount)
2216
{
2217
    int ret = -ENOSYS;
2218

    
2219
    switch (func) {
2220
    case 0:
2221
        ret = read_ldt(ptr, bytecount);
2222
        break;
2223
    case 1:
2224
        ret = write_ldt(env, ptr, bytecount, 1);
2225
        break;
2226
    case 0x11:
2227
        ret = write_ldt(env, ptr, bytecount, 0);
2228
        break;
2229
    }
2230
    return ret;
2231
}
2232

    
2233
#endif /* defined(TARGET_I386) */
2234

    
2235
/* this stack is the equivalent of the kernel stack associated with a
2236
   thread/process */
2237
#define NEW_STACK_SIZE 8192
2238

    
2239
static int clone_func(void *arg)
2240
{
2241
    CPUState *env = arg;
2242
    cpu_loop(env);
2243
    /* never exits */
2244
    return 0;
2245
}
2246

    
2247
/* do_fork() Must return host values and target errnos (unlike most
2248
   do_*() functions). */
2249
int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
2250
{
2251
    int ret;
2252
    TaskState *ts;
2253
    uint8_t *new_stack;
2254
    CPUState *new_env;
2255

    
2256
    if (flags & CLONE_VM) {
2257
        ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2258
        memset(ts, 0, sizeof(TaskState));
2259
        new_stack = ts->stack;
2260
        ts->used = 1;
2261
        /* add in task state list */
2262
        ts->next = first_task_state;
2263
        first_task_state = ts;
2264
        /* we create a new CPU instance. */
2265
        new_env = cpu_copy(env);
2266
#if defined(TARGET_I386)
2267
        if (!newsp)
2268
            newsp = env->regs[R_ESP];
2269
        new_env->regs[R_ESP] = newsp;
2270
        new_env->regs[R_EAX] = 0;
2271
#elif defined(TARGET_ARM)
2272
        if (!newsp)
2273
            newsp = env->regs[13];
2274
        new_env->regs[13] = newsp;
2275
        new_env->regs[0] = 0;
2276
#elif defined(TARGET_SPARC)
2277
        if (!newsp)
2278
            newsp = env->regwptr[22];
2279
        new_env->regwptr[22] = newsp;
2280
        new_env->regwptr[0] = 0;
2281
        /* XXXXX */
2282
        printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
2283
#elif defined(TARGET_M68K)
2284
        if (!newsp)
2285
            newsp = env->aregs[7];
2286
        new_env->aregs[7] = newsp;
2287
        new_env->dregs[0] = 0;
2288
        /* ??? is this sufficient?  */
2289
#elif defined(TARGET_MIPS)
2290
        if (!newsp)
2291
            newsp = env->gpr[29][env->current_tc];
2292
        new_env->gpr[29][env->current_tc] = newsp;
2293
#elif defined(TARGET_PPC)
2294
        if (!newsp)
2295
            newsp = env->gpr[1];
2296
        new_env->gpr[1] = newsp;
2297
        {
2298
            int i;
2299
            for (i = 7; i < 32; i++)
2300
                new_env->gpr[i] = 0;
2301
        }
2302
#elif defined(TARGET_SH4)
2303
        if (!newsp)
2304
          newsp = env->gregs[15];
2305
        new_env->gregs[15] = newsp;
2306
        /* XXXXX */
2307
#elif defined(TARGET_ALPHA)
2308
       if (!newsp)
2309
         newsp = env->ir[30];
2310
       new_env->ir[30] = newsp;
2311
        /* ? */
2312
        {
2313
            int i;
2314
            for (i = 7; i < 30; i++)
2315
                new_env->ir[i] = 0;
2316
        }
2317
#elif defined(TARGET_CRIS)
2318
        if (!newsp)
2319
          newsp = env->regs[14];
2320
        new_env->regs[14] = newsp;
2321
#else
2322
#error unsupported target CPU
2323
#endif
2324
        new_env->opaque = ts;
2325
#ifdef __ia64__
2326
        ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2327
#else
2328
        ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2329
#endif
2330
    } else {
2331
        /* if no CLONE_VM, we consider it is a fork */
2332
        if ((flags & ~CSIGNAL) != 0)
2333
            return -EINVAL;
2334
        ret = fork();
2335
    }
2336
    return ret;
2337
}
2338

    
2339
static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2340
{
2341
    struct flock fl;
2342
    struct target_flock *target_fl;
2343
    struct flock64 fl64;
2344
    struct target_flock64 *target_fl64;
2345
    abi_long ret;
2346

    
2347
    switch(cmd) {
2348
    case TARGET_F_GETLK:
2349
        lock_user_struct(target_fl, arg, 1);
2350
        fl.l_type = tswap16(target_fl->l_type);
2351
        fl.l_whence = tswap16(target_fl->l_whence);
2352
        fl.l_start = tswapl(target_fl->l_start);
2353
        fl.l_len = tswapl(target_fl->l_len);
2354
        fl.l_pid = tswapl(target_fl->l_pid);
2355
        unlock_user_struct(target_fl, arg, 0);
2356
        ret = fcntl(fd, cmd, &fl);
2357
        if (ret == 0) {
2358
            lock_user_struct(target_fl, arg, 0);
2359
            target_fl->l_type = tswap16(fl.l_type);
2360
            target_fl->l_whence = tswap16(fl.l_whence);
2361
            target_fl->l_start = tswapl(fl.l_start);
2362
            target_fl->l_len = tswapl(fl.l_len);
2363
            target_fl->l_pid = tswapl(fl.l_pid);
2364
            unlock_user_struct(target_fl, arg, 1);
2365
        }
2366
        break;
2367

    
2368
    case TARGET_F_SETLK:
2369
    case TARGET_F_SETLKW:
2370
        lock_user_struct(target_fl, arg, 1);
2371
        fl.l_type = tswap16(target_fl->l_type);
2372
        fl.l_whence = tswap16(target_fl->l_whence);
2373
        fl.l_start = tswapl(target_fl->l_start);
2374
        fl.l_len = tswapl(target_fl->l_len);
2375
        fl.l_pid = tswapl(target_fl->l_pid);
2376
        unlock_user_struct(target_fl, arg, 0);
2377
        ret = fcntl(fd, cmd, &fl);
2378
        break;
2379

    
2380
    case TARGET_F_GETLK64:
2381
        lock_user_struct(target_fl64, arg, 1);
2382
        fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2383
        fl64.l_whence = tswap16(target_fl64->l_whence);
2384
        fl64.l_start = tswapl(target_fl64->l_start);
2385
        fl64.l_len = tswapl(target_fl64->l_len);
2386
        fl64.l_pid = tswap16(target_fl64->l_pid);
2387
        unlock_user_struct(target_fl64, arg, 0);
2388
        ret = fcntl(fd, cmd >> 1, &fl64);
2389
        if (ret == 0) {
2390
            lock_user_struct(target_fl64, arg, 0);
2391
            target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2392
            target_fl64->l_whence = tswap16(fl64.l_whence);
2393
            target_fl64->l_start = tswapl(fl64.l_start);
2394
            target_fl64->l_len = tswapl(fl64.l_len);
2395
            target_fl64->l_pid = tswapl(fl64.l_pid);
2396
            unlock_user_struct(target_fl64, arg, 1);
2397
        }
2398
                break;
2399
    case TARGET_F_SETLK64:
2400
    case TARGET_F_SETLKW64:
2401
        lock_user_struct(target_fl64, arg, 1);
2402
        fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2403
        fl64.l_whence = tswap16(target_fl64->l_whence);
2404
        fl64.l_start = tswapl(target_fl64->l_start);
2405
        fl64.l_len = tswapl(target_fl64->l_len);
2406
        fl64.l_pid = tswap16(target_fl64->l_pid);
2407
        unlock_user_struct(target_fl64, arg, 0);
2408
                ret = fcntl(fd, cmd >> 1, &fl64);
2409
        break;
2410

    
2411
    case F_GETFL:
2412
        ret = fcntl(fd, cmd, arg);
2413
        ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2414
        break;
2415

    
2416
    case F_SETFL:
2417
        ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2418
        break;
2419

    
2420
    default:
2421
        ret = fcntl(fd, cmd, arg);
2422
        break;
2423
    }
2424
    return ret;
2425
}
2426

    
2427
#ifdef USE_UID16
2428

    
2429
static inline int high2lowuid(int uid)
2430
{
2431
    if (uid > 65535)
2432
        return 65534;
2433
    else
2434
        return uid;
2435
}
2436

    
2437
static inline int high2lowgid(int gid)
2438
{
2439
    if (gid > 65535)
2440
        return 65534;
2441
    else
2442
        return gid;
2443
}
2444

    
2445
static inline int low2highuid(int uid)
2446
{
2447
    if ((int16_t)uid == -1)
2448
        return -1;
2449
    else
2450
        return uid;
2451
}
2452

    
2453
static inline int low2highgid(int gid)
2454
{
2455
    if ((int16_t)gid == -1)
2456
        return -1;
2457
    else
2458
        return gid;
2459
}
2460

    
2461
#endif /* USE_UID16 */
2462

    
2463
void syscall_init(void)
2464
{
2465
    IOCTLEntry *ie;
2466
    const argtype *arg_type;
2467
    int size;
2468

    
2469
#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2470
#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
2471
#include "syscall_types.h"
2472
#undef STRUCT
2473
#undef STRUCT_SPECIAL
2474

    
2475
    /* we patch the ioctl size if necessary. We rely on the fact that
2476
       no ioctl has all the bits at '1' in the size field */
2477
    ie = ioctl_entries;
2478
    while (ie->target_cmd != 0) {
2479
        if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2480
            TARGET_IOC_SIZEMASK) {
2481
            arg_type = ie->arg_type;
2482
            if (arg_type[0] != TYPE_PTR) {
2483
                fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2484
                        ie->target_cmd);
2485
                exit(1);
2486
            }
2487
            arg_type++;
2488
            size = thunk_type_size(arg_type, 0);
2489
            ie->target_cmd = (ie->target_cmd &
2490
                              ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2491
                (size << TARGET_IOC_SIZESHIFT);
2492
        }
2493
        /* automatic consistency check if same arch */
2494
#if defined(__i386__) && defined(TARGET_I386)
2495
        if (ie->target_cmd != ie->host_cmd) {
2496
            fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2497
                    ie->target_cmd, ie->host_cmd);
2498
        }
2499
#endif
2500
        ie++;
2501
    }
2502
}
2503

    
2504
#if TARGET_ABI_BITS == 32
2505
static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2506
{
2507
#ifdef TARGET_WORDS_BIG_ENDIAN
2508
    return ((uint64_t)word0 << 32) | word1;
2509
#else
2510
    return ((uint64_t)word1 << 32) | word0;
2511
#endif
2512
}
2513
#else /* TARGET_ABI_BITS == 32 */
2514
static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2515
{
2516
    return word0;
2517
}
2518
#endif /* TARGET_ABI_BITS != 32 */
2519

    
2520
#ifdef TARGET_NR_truncate64
2521
static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2522
                                         abi_long arg2,
2523
                                         abi_long arg3,
2524
                                         abi_long arg4)
2525
{
2526
#ifdef TARGET_ARM
2527
    if (((CPUARMState *)cpu_env)->eabi)
2528
      {
2529
        arg2 = arg3;
2530
        arg3 = arg4;
2531
      }
2532
#endif
2533
    return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2534
}
2535
#endif
2536

    
2537
#ifdef TARGET_NR_ftruncate64
2538
static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2539
                                          abi_long arg2,
2540
                                          abi_long arg3,
2541
                                          abi_long arg4)
2542
{
2543
#ifdef TARGET_ARM
2544
    if (((CPUARMState *)cpu_env)->eabi)
2545
      {
2546
        arg2 = arg3;
2547
        arg3 = arg4;
2548
      }
2549
#endif
2550
    return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2551
}
2552
#endif
2553

    
2554
static inline void target_to_host_timespec(struct timespec *host_ts,
2555
                                           abi_ulong target_addr)
2556
{
2557
    struct target_timespec *target_ts;
2558

    
2559
    lock_user_struct(target_ts, target_addr, 1);
2560
    host_ts->tv_sec = tswapl(target_ts->tv_sec);
2561
    host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2562
    unlock_user_struct(target_ts, target_addr, 0);
2563
}
2564

    
2565
static inline void host_to_target_timespec(abi_ulong target_addr,
2566
                                           struct timespec *host_ts)
2567
{
2568
    struct target_timespec *target_ts;
2569

    
2570
    lock_user_struct(target_ts, target_addr, 0);
2571
    target_ts->tv_sec = tswapl(host_ts->tv_sec);
2572
    target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2573
    unlock_user_struct(target_ts, target_addr, 1);
2574
}
2575

    
2576
/* do_syscall() should always have a single exit point at the end so
2577
   that actions, such as logging of syscall results, can be performed.
2578
   All errnos that do_syscall() returns must be -TARGET_<errcode>. */
2579
abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2580
                    abi_long arg2, abi_long arg3, abi_long arg4,
2581
                    abi_long arg5, abi_long arg6)
2582
{
2583
    abi_long ret;
2584
    struct stat st;
2585
    struct statfs stfs;
2586
    void *p;
2587

    
2588
#ifdef DEBUG
2589
    gemu_log("syscall %d", num);
2590
#endif
2591
    switch(num) {
2592
    case TARGET_NR_exit:
2593
#ifdef HAVE_GPROF
2594
        _mcleanup();
2595
#endif
2596
        gdb_exit(cpu_env, arg1);
2597
        /* XXX: should free thread stack and CPU env */
2598
        _exit(arg1);
2599
        ret = 0; /* avoid warning */
2600
        break;
2601
    case TARGET_NR_read:
2602
        page_unprotect_range(arg2, arg3);
2603
        p = lock_user(arg2, arg3, 0);
2604
        ret = get_errno(read(arg1, p, arg3));
2605
        unlock_user(p, arg2, ret);
2606
        break;
2607
    case TARGET_NR_write:
2608
        p = lock_user(arg2, arg3, 1);
2609
        ret = get_errno(write(arg1, p, arg3));
2610
        unlock_user(p, arg2, 0);
2611
        break;
2612
    case TARGET_NR_open:
2613
        p = lock_user_string(arg1);
2614
        ret = get_errno(open(path(p),
2615
                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
2616
                             arg3));
2617
        unlock_user(p, arg1, 0);
2618
        break;
2619
#if defined(TARGET_NR_openat) && defined(__NR_openat)
2620
    case TARGET_NR_openat:
2621
        if (!arg2) {
2622
            ret = -TARGET_EFAULT;
2623
            goto fail;
2624
        }
2625
        p = lock_user_string(arg2);
2626
        if (!access_ok(VERIFY_READ, p, 1))
2627
            /* Don't "goto fail" so that cleanup can happen. */
2628
            ret = -TARGET_EFAULT;
2629
        else
2630
            ret = get_errno(sys_openat(arg1,
2631
                                       path(p),
2632
                                       target_to_host_bitmask(arg3, fcntl_flags_tbl),
2633
                                       arg4));
2634
        if (p)
2635
            unlock_user(p, arg2, 0);
2636
        break;
2637
#endif
2638
    case TARGET_NR_close:
2639
        ret = get_errno(close(arg1));
2640
        break;
2641
    case TARGET_NR_brk:
2642
        ret = do_brk(arg1);
2643
        break;
2644
    case TARGET_NR_fork:
2645
        ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
2646
        break;
2647
#ifdef TARGET_NR_waitpid
2648
    case TARGET_NR_waitpid:
2649
        {
2650
            int status;
2651
            ret = get_errno(waitpid(arg1, &status, arg3));
2652
            if (!is_error(ret) && arg2)
2653
                tput32(arg2, status);
2654
        }
2655
        break;
2656
#endif
2657
#ifdef TARGET_NR_creat /* not on alpha */
2658
    case TARGET_NR_creat:
2659
        p = lock_user_string(arg1);
2660
        ret = get_errno(creat(p, arg2));
2661
        unlock_user(p, arg1, 0);
2662
        break;
2663
#endif
2664
    case TARGET_NR_link:
2665
        {
2666
            void * p2;
2667
            p = lock_user_string(arg1);
2668
            p2 = lock_user_string(arg2);
2669
            ret = get_errno(link(p, p2));
2670
            unlock_user(p2, arg2, 0);
2671
            unlock_user(p, arg1, 0);
2672
        }
2673
        break;
2674
#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2675
    case TARGET_NR_linkat:
2676
        if (!arg2 || !arg4) {
2677
            ret = -TARGET_EFAULT;
2678
            goto fail;
2679
        }
2680
        {
2681
            void * p2 = NULL;
2682
            p  = lock_user_string(arg2);
2683
            p2 = lock_user_string(arg4);
2684
            if (!access_ok(VERIFY_READ, p, 1)
2685
                || !access_ok(VERIFY_READ, p2, 1))
2686
                /* Don't "goto fail" so that cleanup can happen. */
2687
                ret = -TARGET_EFAULT;
2688
            else
2689
                ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
2690
            if (p2)
2691
                unlock_user(p, arg2, 0);
2692
            if (p)
2693
                unlock_user(p2, arg4, 0);
2694
        }
2695
        break;
2696
#endif
2697
    case TARGET_NR_unlink:
2698
        p = lock_user_string(arg1);
2699
        ret = get_errno(unlink(p));
2700
        unlock_user(p, arg1, 0);
2701
        break;
2702
#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2703
    case TARGET_NR_unlinkat:
2704
        if (!arg2) {
2705
            ret = -TARGET_EFAULT;
2706
            goto fail;
2707
        }
2708
        p = lock_user_string(arg2);
2709
        if (!access_ok(VERIFY_READ, p, 1))
2710
            /* Don't "goto fail" so that cleanup can happen. */
2711
            ret = -TARGET_EFAULT;
2712
        else
2713
            ret = get_errno(sys_unlinkat(arg1, p, arg3));
2714
        if (p)
2715
            unlock_user(p, arg2, 0);
2716
        break;
2717
#endif
2718
    case TARGET_NR_execve:
2719
        {
2720
            char **argp, **envp;
2721
            int argc, envc;
2722
            abi_ulong gp;
2723
            abi_ulong guest_argp;
2724
            abi_ulong guest_envp;
2725
            abi_ulong addr;
2726
            char **q;
2727

    
2728
            argc = 0;
2729
            guest_argp = arg2;
2730
            for (gp = guest_argp; tgetl(gp); gp++)
2731
                argc++;
2732
            envc = 0;
2733
            guest_envp = arg3;
2734
            for (gp = guest_envp; tgetl(gp); gp++)
2735
                envc++;
2736

    
2737
            argp = alloca((argc + 1) * sizeof(void *));
2738
            envp = alloca((envc + 1) * sizeof(void *));
2739

    
2740
            for (gp = guest_argp, q = argp; ;
2741
                  gp += sizeof(abi_ulong), q++) {
2742
                addr = tgetl(gp);
2743
                if (!addr)
2744
                    break;
2745
                *q = lock_user_string(addr);
2746
            }
2747
            *q = NULL;
2748

    
2749
            for (gp = guest_envp, q = envp; ;
2750
                  gp += sizeof(abi_ulong), q++) {
2751
                addr = tgetl(gp);
2752
                if (!addr)
2753
                    break;
2754
                *q = lock_user_string(addr);
2755
            }
2756
            *q = NULL;
2757

    
2758
            p = lock_user_string(arg1);
2759
            ret = get_errno(execve(p, argp, envp));
2760
            unlock_user(p, arg1, 0);
2761

    
2762
            for (gp = guest_argp, q = argp; *q;
2763
                  gp += sizeof(abi_ulong), q++) {
2764
                addr = tgetl(gp);
2765
                unlock_user(*q, addr, 0);
2766
            }
2767
            for (gp = guest_envp, q = envp; *q;
2768
                  gp += sizeof(abi_ulong), q++) {
2769
                addr = tgetl(gp);
2770
                unlock_user(*q, addr, 0);
2771
            }
2772
        }
2773
        break;
2774
    case TARGET_NR_chdir:
2775
        p = lock_user_string(arg1);
2776
        ret = get_errno(chdir(p));
2777
        unlock_user(p, arg1, 0);
2778
        break;
2779
#ifdef TARGET_NR_time
2780
    case TARGET_NR_time:
2781
        {
2782
            time_t host_time;
2783
            ret = get_errno(time(&host_time));
2784
            if (!is_error(ret) && arg1)
2785
                tputl(arg1, host_time);
2786
        }
2787
        break;
2788
#endif
2789
    case TARGET_NR_mknod:
2790
        p = lock_user_string(arg1);
2791
        ret = get_errno(mknod(p, arg2, arg3));
2792
        unlock_user(p, arg1, 0);
2793
        break;
2794
#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2795
    case TARGET_NR_mknodat:
2796
        if (!arg2) {
2797
            ret = -TARGET_EFAULT;
2798
            goto fail;
2799
        }
2800
        p = lock_user_string(arg2);
2801
        if (!access_ok(VERIFY_READ, p, 1))
2802
            /* Don't "goto fail" so that cleanup can happen. */
2803
            ret = -TARGET_EFAULT;
2804
        else
2805
            ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2806
        if (p)
2807
            unlock_user(p, arg2, 0);
2808
        break;
2809
#endif
2810
    case TARGET_NR_chmod:
2811
        p = lock_user_string(arg1);
2812
        ret = get_errno(chmod(p, arg2));
2813
        unlock_user(p, arg1, 0);
2814
        break;
2815
#ifdef TARGET_NR_break
2816
    case TARGET_NR_break:
2817
        goto unimplemented;
2818
#endif
2819
#ifdef TARGET_NR_oldstat
2820
    case TARGET_NR_oldstat:
2821
        goto unimplemented;
2822
#endif
2823
    case TARGET_NR_lseek:
2824
        ret = get_errno(lseek(arg1, arg2, arg3));
2825
        break;
2826
#ifdef TARGET_NR_getxpid
2827
    case TARGET_NR_getxpid:
2828
#else
2829
    case TARGET_NR_getpid:
2830
#endif
2831
        ret = get_errno(getpid());
2832
        break;
2833
    case TARGET_NR_mount:
2834
                {
2835
                        /* need to look at the data field */
2836
                        void *p2, *p3;
2837
                        p = lock_user_string(arg1);
2838
                        p2 = lock_user_string(arg2);
2839
                        p3 = lock_user_string(arg3);
2840
                        ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, (const void *)arg5));
2841
                        unlock_user(p, arg1, 0);
2842
                        unlock_user(p2, arg2, 0);
2843
                        unlock_user(p3, arg3, 0);
2844
                        break;
2845
                }
2846
#ifdef TARGET_NR_umount
2847
    case TARGET_NR_umount:
2848
        p = lock_user_string(arg1);
2849
        ret = get_errno(umount(p));
2850
        unlock_user(p, arg1, 0);
2851
        break;
2852
#endif
2853
#ifdef TARGET_NR_stime /* not on alpha */
2854
    case TARGET_NR_stime:
2855
        {
2856
            time_t host_time;
2857
            host_time = tgetl(arg1);
2858
            ret = get_errno(stime(&host_time));
2859
        }
2860
        break;
2861
#endif
2862
    case TARGET_NR_ptrace:
2863
        goto unimplemented;
2864
#ifdef TARGET_NR_alarm /* not on alpha */
2865
    case TARGET_NR_alarm:
2866
        ret = alarm(arg1);
2867
        break;
2868
#endif
2869
#ifdef TARGET_NR_oldfstat
2870
    case TARGET_NR_oldfstat:
2871
        goto unimplemented;
2872
#endif
2873
#ifdef TARGET_NR_pause /* not on alpha */
2874
    case TARGET_NR_pause:
2875
        ret = get_errno(pause());
2876
        break;
2877
#endif
2878
#ifdef TARGET_NR_utime
2879
    case TARGET_NR_utime:
2880
        {
2881
            struct utimbuf tbuf, *host_tbuf;
2882
            struct target_utimbuf *target_tbuf;
2883
            if (arg2) {
2884
                lock_user_struct(target_tbuf, arg2, 1);
2885
                tbuf.actime = tswapl(target_tbuf->actime);
2886
                tbuf.modtime = tswapl(target_tbuf->modtime);
2887
                unlock_user_struct(target_tbuf, arg2, 0);
2888
                host_tbuf = &tbuf;
2889
            } else {
2890
                host_tbuf = NULL;
2891
            }
2892
            p = lock_user_string(arg1);
2893
            ret = get_errno(utime(p, host_tbuf));
2894
            unlock_user(p, arg1, 0);
2895
        }
2896
        break;
2897
#endif
2898
    case TARGET_NR_utimes:
2899
        {
2900
            struct timeval *tvp, tv[2];
2901
            if (arg2) {
2902
                target_to_host_timeval(&tv[0], arg2);
2903
                target_to_host_timeval(&tv[1],
2904
                    arg2 + sizeof (struct target_timeval));
2905
                tvp = tv;
2906
            } else {
2907
                tvp = NULL;
2908
            }
2909
            p = lock_user_string(arg1);
2910
            ret = get_errno(utimes(p, tvp));
2911
            unlock_user(p, arg1, 0);
2912
        }
2913
        break;
2914
#ifdef TARGET_NR_stty
2915
    case TARGET_NR_stty:
2916
        goto unimplemented;
2917
#endif
2918
#ifdef TARGET_NR_gtty
2919
    case TARGET_NR_gtty:
2920
        goto unimplemented;
2921
#endif
2922
    case TARGET_NR_access:
2923
        p = lock_user_string(arg1);
2924
        ret = get_errno(access(p, arg2));
2925
        unlock_user(p, arg1, 0);
2926
        break;
2927
#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
2928
    case TARGET_NR_faccessat:
2929
        if (!arg2) {
2930
            ret = -TARGET_EFAULT;
2931
            goto fail;
2932
        }
2933
        p = lock_user_string(arg2);
2934
        if (!access_ok(VERIFY_READ, p, 1))
2935
            /* Don't "goto fail" so that cleanup can happen. */
2936
                ret = -TARGET_EFAULT;
2937
        else
2938
            ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
2939
        if (p)
2940
            unlock_user(p, arg2, 0);
2941
        break;
2942
#endif
2943
#ifdef TARGET_NR_nice /* not on alpha */
2944
    case TARGET_NR_nice:
2945
        ret = get_errno(nice(arg1));
2946
        break;
2947
#endif
2948
#ifdef TARGET_NR_ftime
2949
    case TARGET_NR_ftime:
2950
        goto unimplemented;
2951
#endif
2952
    case TARGET_NR_sync:
2953
        sync();
2954
        ret = 0;
2955
        break;
2956
    case TARGET_NR_kill:
2957
        ret = get_errno(kill(arg1, arg2));
2958
        break;
2959
    case TARGET_NR_rename:
2960
        {
2961
            void *p2;
2962
            p = lock_user_string(arg1);
2963
            p2 = lock_user_string(arg2);
2964
            ret = get_errno(rename(p, p2));
2965
            unlock_user(p2, arg2, 0);
2966
            unlock_user(p, arg1, 0);
2967
        }
2968
        break;
2969
#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
2970
    case TARGET_NR_renameat:
2971
        if (!arg2 || !arg4) {
2972
            ret = -TARGET_EFAULT;
2973
            goto fail;
2974
        }
2975
        {
2976
            void *p2 = NULL;
2977
            p  = lock_user_string(arg2);
2978
            p2 = lock_user_string(arg4);
2979
            if (!access_ok(VERIFY_READ, p, 1)
2980
                || !access_ok(VERIFY_READ, p2, 1))
2981
                /* Don't "goto fail" so that cleanup can happen. */
2982
                ret = -TARGET_EFAULT;
2983
            else
2984
                ret = get_errno(sys_renameat(arg1, p, arg3, p2));
2985
            if (p2)
2986
                unlock_user(p2, arg4, 0);
2987
            if (p)
2988
                unlock_user(p, arg2, 0);
2989
        }
2990
        break;
2991
#endif
2992
    case TARGET_NR_mkdir:
2993
        p = lock_user_string(arg1);
2994
        ret = get_errno(mkdir(p, arg2));
2995
        unlock_user(p, arg1, 0);
2996
        break;
2997
#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
2998
    case TARGET_NR_mkdirat:
2999
        if (!arg2) {
3000
            ret = -TARGET_EFAULT;
3001
            goto fail;
3002
        }
3003
        p = lock_user_string(arg2);
3004
        if (!access_ok(VERIFY_READ, p, 1))
3005
            /* Don't "goto fail" so that cleanup can happen. */
3006
            ret = -TARGET_EFAULT;
3007
        else
3008
            ret = get_errno(sys_mkdirat(arg1, p, arg3));
3009
        if (p)
3010
            unlock_user(p, arg2, 0);
3011
        break;
3012
#endif
3013
    case TARGET_NR_rmdir:
3014
        p = lock_user_string(arg1);
3015
        ret = get_errno(rmdir(p));
3016
        unlock_user(p, arg1, 0);
3017
        break;
3018
    case TARGET_NR_dup:
3019
        ret = get_errno(dup(arg1));
3020
        break;
3021
    case TARGET_NR_pipe:
3022
        {
3023
            int host_pipe[2];
3024
            ret = get_errno(pipe(host_pipe));
3025
            if (!is_error(ret)) {
3026
#if defined(TARGET_MIPS)
3027
                CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3028
                env->gpr[3][env->current_tc] = host_pipe[1];
3029
                ret = host_pipe[0];
3030
#else
3031
                tput32(arg1, host_pipe[0]);
3032
                tput32(arg1 + 4, host_pipe[1]);
3033
#endif
3034
            }
3035
        }
3036
        break;
3037
    case TARGET_NR_times:
3038
        {
3039
            struct target_tms *tmsp;
3040
            struct tms tms;
3041
            ret = get_errno(times(&tms));
3042
            if (arg1) {
3043
                tmsp = lock_user(arg1, sizeof(struct target_tms), 0);
3044
                tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3045
                tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3046
                tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3047
                tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
3048
            }
3049
            if (!is_error(ret))
3050
                ret = host_to_target_clock_t(ret);
3051
        }
3052
        break;
3053
#ifdef TARGET_NR_prof
3054
    case TARGET_NR_prof:
3055
        goto unimplemented;
3056
#endif
3057
#ifdef TARGET_NR_signal
3058
    case TARGET_NR_signal:
3059
        goto unimplemented;
3060
#endif
3061
    case TARGET_NR_acct:
3062
        p = lock_user_string(arg1);
3063
        ret = get_errno(acct(path(p)));
3064
        unlock_user(p, arg1, 0);
3065
        break;
3066
#ifdef TARGET_NR_umount2 /* not on alpha */
3067
    case TARGET_NR_umount2:
3068
        p = lock_user_string(arg1);
3069
        ret = get_errno(umount2(p, arg2));
3070
        unlock_user(p, arg1, 0);
3071
        break;
3072
#endif
3073
#ifdef TARGET_NR_lock
3074
    case TARGET_NR_lock:
3075
        goto unimplemented;
3076
#endif
3077
    case TARGET_NR_ioctl:
3078
        ret = do_ioctl(arg1, arg2, arg3);
3079
        break;
3080
    case TARGET_NR_fcntl:
3081
        ret = get_errno(do_fcntl(arg1, arg2, arg3));
3082
        break;
3083
#ifdef TARGET_NR_mpx
3084
    case TARGET_NR_mpx:
3085
        goto unimplemented;
3086
#endif
3087
    case TARGET_NR_setpgid:
3088
        ret = get_errno(setpgid(arg1, arg2));
3089
        break;
3090
#ifdef TARGET_NR_ulimit
3091
    case TARGET_NR_ulimit:
3092
        goto unimplemented;
3093
#endif
3094
#ifdef TARGET_NR_oldolduname
3095
    case TARGET_NR_oldolduname:
3096
        goto unimplemented;
3097
#endif
3098
    case TARGET_NR_umask:
3099
        ret = get_errno(umask(arg1));
3100
        break;
3101
    case TARGET_NR_chroot:
3102
        p = lock_user_string(arg1);
3103
        ret = get_errno(chroot(p));
3104
        unlock_user(p, arg1, 0);
3105
        break;
3106
    case TARGET_NR_ustat:
3107
        goto unimplemented;
3108
    case TARGET_NR_dup2:
3109
        ret = get_errno(dup2(arg1, arg2));
3110
        break;
3111
#ifdef TARGET_NR_getppid /* not on alpha */
3112
    case TARGET_NR_getppid:
3113
        ret = get_errno(getppid());
3114
        break;
3115
#endif
3116
    case TARGET_NR_getpgrp:
3117
        ret = get_errno(getpgrp());
3118
        break;
3119
    case TARGET_NR_setsid:
3120
        ret = get_errno(setsid());
3121
        break;
3122
#ifdef TARGET_NR_sigaction
3123
    case TARGET_NR_sigaction:
3124
        {
3125
#if !defined(TARGET_MIPS)
3126
            struct target_old_sigaction *old_act;
3127
            struct target_sigaction act, oact, *pact;
3128
            if (arg2) {
3129
                lock_user_struct(old_act, arg2, 1);
3130
                act._sa_handler = old_act->_sa_handler;
3131
                target_siginitset(&act.sa_mask, old_act->sa_mask);
3132
                act.sa_flags = old_act->sa_flags;
3133
                act.sa_restorer = old_act->sa_restorer;
3134
                unlock_user_struct(old_act, arg2, 0);
3135
                pact = &act;
3136
            } else {
3137
                pact = NULL;
3138
            }
3139
            ret = get_errno(do_sigaction(arg1, pact, &oact));
3140
            if (!is_error(ret) && arg3) {
3141
                lock_user_struct(old_act, arg3, 0);
3142
                old_act->_sa_handler = oact._sa_handler;
3143
                old_act->sa_mask = oact.sa_mask.sig[0];
3144
                old_act->sa_flags = oact.sa_flags;
3145
                old_act->sa_restorer = oact.sa_restorer;
3146
                unlock_user_struct(old_act, arg3, 1);
3147
            }
3148
#else
3149
            struct target_sigaction act, oact, *pact, *old_act;
3150

    
3151
            if (arg2) {
3152
                lock_user_struct(old_act, arg2, 1);
3153
                act._sa_handler = old_act->_sa_handler;
3154
                target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3155
                act.sa_flags = old_act->sa_flags;
3156
                unlock_user_struct(old_act, arg2, 0);
3157
                pact = &act;
3158
            } else {
3159
                pact = NULL;
3160
            }
3161

    
3162
            ret = get_errno(do_sigaction(arg1, pact, &oact));
3163

    
3164
            if (!is_error(ret) && arg3) {
3165
                lock_user_struct(old_act, arg3, 0);
3166
                old_act->_sa_handler = oact._sa_handler;
3167
                old_act->sa_flags = oact.sa_flags;
3168
                old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3169
                old_act->sa_mask.sig[1] = 0;
3170
                old_act->sa_mask.sig[2] = 0;
3171
                old_act->sa_mask.sig[3] = 0;
3172
                unlock_user_struct(old_act, arg3, 1);
3173
            }
3174
#endif
3175
        }
3176
        break;
3177
#endif
3178
    case TARGET_NR_rt_sigaction:
3179
        {
3180
            struct target_sigaction *act;
3181
            struct target_sigaction *oact;
3182

    
3183
            if (arg2)
3184
                lock_user_struct(act, arg2, 1);
3185
            else
3186
                act = NULL;
3187
            if (arg3)
3188
                lock_user_struct(oact, arg3, 0);
3189
            else
3190
                oact = NULL;
3191
            ret = get_errno(do_sigaction(arg1, act, oact));
3192
            if (arg2)
3193
                unlock_user_struct(act, arg2, 0);
3194
            if (arg3)
3195
                unlock_user_struct(oact, arg3, 1);
3196
        }
3197
        break;
3198
#ifdef TARGET_NR_sgetmask /* not on alpha */
3199
    case TARGET_NR_sgetmask:
3200
        {
3201
            sigset_t cur_set;
3202
            abi_ulong target_set;
3203
            sigprocmask(0, NULL, &cur_set);
3204
            host_to_target_old_sigset(&target_set, &cur_set);
3205
            ret = target_set;
3206
        }
3207
        break;
3208
#endif
3209
#ifdef TARGET_NR_ssetmask /* not on alpha */
3210
    case TARGET_NR_ssetmask:
3211
        {
3212
            sigset_t set, oset, cur_set;
3213
            abi_ulong target_set = arg1;
3214
            sigprocmask(0, NULL, &cur_set);
3215
            target_to_host_old_sigset(&set, &target_set);
3216
            sigorset(&set, &set, &cur_set);
3217
            sigprocmask(SIG_SETMASK, &set, &oset);
3218
            host_to_target_old_sigset(&target_set, &oset);
3219
            ret = target_set;
3220
        }
3221
        break;
3222
#endif
3223
#ifdef TARGET_NR_sigprocmask
3224
    case TARGET_NR_sigprocmask:
3225
        {
3226
            int how = arg1;
3227
            sigset_t set, oldset, *set_ptr;
3228

    
3229
            if (arg2) {
3230
                switch(how) {
3231
                case TARGET_SIG_BLOCK:
3232
                    how = SIG_BLOCK;
3233
                    break;
3234
                case TARGET_SIG_UNBLOCK:
3235
                    how = SIG_UNBLOCK;
3236
                    break;
3237
                case TARGET_SIG_SETMASK:
3238
                    how = SIG_SETMASK;
3239
                    break;
3240
                default:
3241
                    ret = -TARGET_EINVAL;
3242
                    goto fail;
3243
                }
3244
                p = lock_user(arg2, sizeof(target_sigset_t), 1);
3245
                target_to_host_old_sigset(&set, p);
3246
                unlock_user(p, arg2, 0);
3247
                set_ptr = &set;
3248
            } else {
3249
                how = 0;
3250
                set_ptr = NULL;
3251
            }
3252
            ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
3253
            if (!is_error(ret) && arg3) {
3254
                p = lock_user(arg3, sizeof(target_sigset_t), 0);
3255
                host_to_target_old_sigset(p, &oldset);
3256
                unlock_user(p, arg3, sizeof(target_sigset_t));
3257
            }
3258
        }
3259
        break;
3260
#endif
3261
    case TARGET_NR_rt_sigprocmask:
3262
        {
3263
            int how = arg1;
3264
            sigset_t set, oldset, *set_ptr;
3265

    
3266
            if (arg2) {
3267
                switch(how) {
3268
                case TARGET_SIG_BLOCK:
3269
                    how = SIG_BLOCK;
3270
                    break;
3271
                case TARGET_SIG_UNBLOCK:
3272
                    how = SIG_UNBLOCK;
3273
                    break;
3274
                case TARGET_SIG_SETMASK:
3275
                    how = SIG_SETMASK;
3276
                    break;
3277
                default:
3278
                    ret = -TARGET_EINVAL;
3279
                    goto fail;
3280
                }
3281
                p = lock_user(arg2, sizeof(target_sigset_t), 1);
3282
                target_to_host_sigset(&set, p);
3283
                unlock_user(p, arg2, 0);
3284
                set_ptr = &set;
3285
            } else {
3286
                how = 0;
3287
                set_ptr = NULL;
3288
            }
3289
            ret = get_errno(sigprocmask(how, set_ptr, &oldset));
3290
            if (!is_error(ret) && arg3) {
3291
                p = lock_user(arg3, sizeof(target_sigset_t), 0);
3292
                host_to_target_sigset(p, &oldset);
3293
                unlock_user(p, arg3, sizeof(target_sigset_t));
3294
            }
3295
        }
3296
        break;
3297
#ifdef TARGET_NR_sigpending
3298
    case TARGET_NR_sigpending:
3299
        {
3300
            sigset_t set;
3301
            ret = get_errno(sigpending(&set));
3302
            if (!is_error(ret)) {
3303
                p = lock_user(arg1, sizeof(target_sigset_t), 0);
3304
                host_to_target_old_sigset(p, &set);
3305
                unlock_user(p, arg1, sizeof(target_sigset_t));
3306
            }
3307
        }
3308
        break;
3309
#endif
3310
    case TARGET_NR_rt_sigpending:
3311
        {
3312
            sigset_t set;
3313
            ret = get_errno(sigpending(&set));
3314
            if (!is_error(ret)) {
3315
                p = lock_user(arg1, sizeof(target_sigset_t), 0);
3316
                host_to_target_sigset(p, &set);
3317
                unlock_user(p, arg1, sizeof(target_sigset_t));
3318
            }
3319
        }
3320
        break;
3321
#ifdef TARGET_NR_sigsuspend
3322
    case TARGET_NR_sigsuspend:
3323
        {
3324
            sigset_t set;
3325
            p = lock_user(arg1, sizeof(target_sigset_t), 1);
3326
            target_to_host_old_sigset(&set, p);
3327
            unlock_user(p, arg1, 0);
3328
            ret = get_errno(sigsuspend(&set));
3329
        }
3330
        break;
3331
#endif
3332
    case TARGET_NR_rt_sigsuspend:
3333
        {
3334
            sigset_t set;
3335
            p = lock_user(arg1, sizeof(target_sigset_t), 1);
3336
            target_to_host_sigset(&set, p);
3337
            unlock_user(p, arg1, 0);
3338
            ret = get_errno(sigsuspend(&set));
3339
        }
3340
        break;
3341
    case TARGET_NR_rt_sigtimedwait:
3342
        {
3343
            sigset_t set;
3344
            struct timespec uts, *puts;
3345
            siginfo_t uinfo;
3346

    
3347
            p = lock_user(arg1, sizeof(target_sigset_t), 1);
3348
            target_to_host_sigset(&set, p);
3349
            unlock_user(p, arg1, 0);
3350
            if (arg3) {
3351
                puts = &uts;
3352
                target_to_host_timespec(puts, arg3);
3353
            } else {
3354
                puts = NULL;
3355
            }
3356
            ret = get_errno(sigtimedwait(&set, &uinfo, puts));
3357
            if (!is_error(ret) && arg2) {
3358
                p = lock_user(arg2, sizeof(target_sigset_t), 0);
3359
                host_to_target_siginfo(p, &uinfo);
3360
                unlock_user(p, arg2, sizeof(target_sigset_t));
3361
            }
3362
        }
3363
        break;
3364
    case TARGET_NR_rt_sigqueueinfo:
3365
        {
3366
            siginfo_t uinfo;
3367
            p = lock_user(arg3, sizeof(target_sigset_t), 1);
3368
            target_to_host_siginfo(&uinfo, p);
3369
            unlock_user(p, arg1, 0);
3370
            ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3371
        }
3372
        break;
3373
#ifdef TARGET_NR_sigreturn
3374
    case TARGET_NR_sigreturn:
3375
        /* NOTE: ret is eax, so not transcoding must be done */
3376
        ret = do_sigreturn(cpu_env);
3377
        break;
3378
#endif
3379
    case TARGET_NR_rt_sigreturn:
3380
        /* NOTE: ret is eax, so not transcoding must be done */
3381
        ret = do_rt_sigreturn(cpu_env);
3382
        break;
3383
    case TARGET_NR_sethostname:
3384
        p = lock_user_string(arg1);
3385
        ret = get_errno(sethostname(p, arg2));
3386
        unlock_user(p, arg1, 0);
3387
        break;
3388
    case TARGET_NR_setrlimit:
3389
        {
3390
            /* XXX: convert resource ? */
3391
            int resource = arg1;
3392
            struct target_rlimit *target_rlim;
3393
            struct rlimit rlim;
3394
            lock_user_struct(target_rlim, arg2, 1);
3395
            rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3396
            rlim.rlim_max = tswapl(target_rlim->rlim_max);
3397
            unlock_user_struct(target_rlim, arg2, 0);
3398
            ret = get_errno(setrlimit(resource, &rlim));
3399
        }
3400
        break;
3401
    case TARGET_NR_getrlimit:
3402
        {
3403
            /* XXX: convert resource ? */
3404
            int resource = arg1;
3405
            struct target_rlimit *target_rlim;
3406
            struct rlimit rlim;
3407

    
3408
            ret = get_errno(getrlimit(resource, &rlim));
3409
            if (!is_error(ret)) {
3410
                lock_user_struct(target_rlim, arg2, 0);
3411
                rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3412
                rlim.rlim_max = tswapl(target_rlim->rlim_max);
3413
                unlock_user_struct(target_rlim, arg2, 1);
3414
            }
3415
        }
3416
        break;
3417
    case TARGET_NR_getrusage:
3418
        {
3419
            struct rusage rusage;
3420
            ret = get_errno(getrusage(arg1, &rusage));
3421
            if (!is_error(ret)) {
3422
                host_to_target_rusage(arg2, &rusage);
3423
            }
3424
        }
3425
        break;
3426
    case TARGET_NR_gettimeofday:
3427
        {
3428
            struct timeval tv;
3429
            ret = get_errno(gettimeofday(&tv, NULL));
3430
            if (!is_error(ret)) {
3431
                host_to_target_timeval(arg1, &tv);
3432
            }
3433
        }
3434
        break;
3435
    case TARGET_NR_settimeofday:
3436
        {
3437
            struct timeval tv;
3438
            target_to_host_timeval(&tv, arg1);
3439
            ret = get_errno(settimeofday(&tv, NULL));
3440
        }
3441
        break;
3442
#ifdef TARGET_NR_select
3443
    case TARGET_NR_select:
3444
        {
3445
            struct target_sel_arg_struct *sel;
3446
            abi_ulong inp, outp, exp, tvp;
3447
            long nsel;
3448

    
3449
            lock_user_struct(sel, arg1, 1);
3450
            nsel = tswapl(sel->n);
3451
            inp = tswapl(sel->inp);
3452
            outp = tswapl(sel->outp);
3453
            exp = tswapl(sel->exp);
3454
            tvp = tswapl(sel->tvp);
3455
            unlock_user_struct(sel, arg1, 0);
3456
            ret = do_select(nsel, inp, outp, exp, tvp);
3457
        }
3458
        break;
3459
#endif
3460
    case TARGET_NR_symlink:
3461
        {
3462
            void *p2;
3463
            p = lock_user_string(arg1);
3464
            p2 = lock_user_string(arg2);
3465
            ret = get_errno(symlink(p, p2));
3466
            unlock_user(p2, arg2, 0);
3467
            unlock_user(p, arg1, 0);
3468
        }
3469
        break;
3470
#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3471
    case TARGET_NR_symlinkat:
3472
        if (!arg1 || !arg3) {
3473
            ret = -TARGET_EFAULT;
3474
            goto fail;
3475
        }
3476
        {
3477
            void *p2 = NULL;
3478
            p  = lock_user_string(arg1);
3479
            p2 = lock_user_string(arg3);
3480
            if (!access_ok(VERIFY_READ, p, 1)
3481
                || !access_ok(VERIFY_READ, p2, 1))
3482
                /* Don't "goto fail" so that cleanup can happen. */
3483
                ret = -TARGET_EFAULT;
3484
            else
3485
                ret = get_errno(sys_symlinkat(p, arg2, p2));
3486
            if (p2)
3487
                unlock_user(p2, arg3, 0);
3488
            if (p)
3489
                unlock_user(p, arg1, 0);
3490
        }
3491
        break;
3492
#endif
3493
#ifdef TARGET_NR_oldlstat
3494
    case TARGET_NR_oldlstat:
3495
        goto unimplemented;
3496
#endif
3497
    case TARGET_NR_readlink:
3498
        {
3499
            void *p2;
3500
            p = lock_user_string(arg1);
3501
            p2 = lock_user(arg2, arg3, 0);
3502
            ret = get_errno(readlink(path(p), p2, arg3));
3503
            unlock_user(p2, arg2, ret);
3504
            unlock_user(p, arg1, 0);
3505
        }
3506
        break;
3507
#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3508
    case TARGET_NR_readlinkat:
3509
        if (!arg2 || !arg3) {
3510
            ret = -TARGET_EFAULT;
3511
            goto fail;
3512
        }
3513
        {
3514
            void *p2 = NULL;
3515
            p  = lock_user_string(arg2);
3516
            p2 = lock_user(arg3, arg4, 0);
3517
            if (!access_ok(VERIFY_READ, p, 1)
3518
                || !access_ok(VERIFY_READ, p2, 1))
3519
                /* Don't "goto fail" so that cleanup can happen. */
3520
                ret = -TARGET_EFAULT;
3521
            else
3522
                ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
3523
            if (p2)
3524
                unlock_user(p2, arg3, ret);
3525
            if (p)
3526
                unlock_user(p, arg2, 0);
3527
        }
3528
        break;
3529
#endif
3530
#ifdef TARGET_NR_uselib
3531
    case TARGET_NR_uselib:
3532
        goto unimplemented;
3533
#endif
3534
#ifdef TARGET_NR_swapon
3535
    case TARGET_NR_swapon:
3536
        p = lock_user_string(arg1);
3537
        ret = get_errno(swapon(p, arg2));
3538
        unlock_user(p, arg1, 0);
3539
        break;
3540
#endif
3541
    case TARGET_NR_reboot:
3542
        goto unimplemented;
3543
#ifdef TARGET_NR_readdir
3544
    case TARGET_NR_readdir:
3545
        goto unimplemented;
3546
#endif
3547
#ifdef TARGET_NR_mmap
3548
    case TARGET_NR_mmap:
3549
#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
3550
        {
3551
            abi_ulong *v;
3552
            abi_ulong v1, v2, v3, v4, v5, v6;
3553
            v = lock_user(arg1, 6 * sizeof(abi_ulong), 1);
3554
            v1 = tswapl(v[0]);
3555
            v2 = tswapl(v[1]);
3556
            v3 = tswapl(v[2]);
3557
            v4 = tswapl(v[3]);
3558
            v5 = tswapl(v[4]);
3559
            v6 = tswapl(v[5]);
3560
            unlock_user(v, arg1, 0);
3561
            ret = get_errno(target_mmap(v1, v2, v3,
3562
                                        target_to_host_bitmask(v4, mmap_flags_tbl),
3563
                                        v5, v6));
3564
        }
3565
#else
3566
        ret = get_errno(target_mmap(arg1, arg2, arg3,
3567
                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
3568
                                    arg5,
3569
                                    arg6));
3570
#endif
3571
        break;
3572
#endif
3573
#ifdef TARGET_NR_mmap2
3574
    case TARGET_NR_mmap2:
3575
#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
3576
#define MMAP_SHIFT 12
3577
#else
3578
#define MMAP_SHIFT TARGET_PAGE_BITS
3579
#endif
3580
        ret = get_errno(target_mmap(arg1, arg2, arg3,
3581
                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
3582
                                    arg5,
3583
                                    arg6 << MMAP_SHIFT));
3584
        break;
3585
#endif
3586
    case TARGET_NR_munmap:
3587
        ret = get_errno(target_munmap(arg1, arg2));
3588
        break;
3589
    case TARGET_NR_mprotect:
3590
        ret = get_errno(target_mprotect(arg1, arg2, arg3));
3591
        break;
3592
#ifdef TARGET_NR_mremap
3593
    case TARGET_NR_mremap:
3594
        ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
3595
        break;
3596
#endif
3597
        /* ??? msync/mlock/munlock are broken for softmmu.  */
3598
#ifdef TARGET_NR_msync
3599
    case TARGET_NR_msync:
3600
        ret = get_errno(msync(g2h(arg1), arg2, arg3));
3601
        break;
3602
#endif
3603
#ifdef TARGET_NR_mlock
3604
    case TARGET_NR_mlock:
3605
        ret = get_errno(mlock(g2h(arg1), arg2));
3606
        break;
3607
#endif
3608
#ifdef TARGET_NR_munlock
3609
    case TARGET_NR_munlock:
3610
        ret = get_errno(munlock(g2h(arg1), arg2));
3611
        break;
3612
#endif
3613
#ifdef TARGET_NR_mlockall
3614
    case TARGET_NR_mlockall:
3615
        ret = get_errno(mlockall(arg1));
3616
        break;
3617
#endif
3618
#ifdef TARGET_NR_munlockall
3619
    case TARGET_NR_munlockall:
3620
        ret = get_errno(munlockall());
3621
        break;
3622
#endif
3623
    case TARGET_NR_truncate:
3624
        p = lock_user_string(arg1);
3625
        ret = get_errno(truncate(p, arg2));
3626
        unlock_user(p, arg1, 0);
3627
        break;
3628
    case TARGET_NR_ftruncate:
3629
        ret = get_errno(ftruncate(arg1, arg2));
3630
        break;
3631
    case TARGET_NR_fchmod:
3632
        ret = get_errno(fchmod(arg1, arg2));
3633
        break;
3634
#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3635
    case TARGET_NR_fchmodat:
3636
        if (!arg2) {
3637
            ret = -TARGET_EFAULT;
3638
            goto fail;
3639
        }
3640
        p = lock_user_string(arg2);
3641
        if (!access_ok(VERIFY_READ, p, 1))
3642
            /* Don't "goto fail" so that cleanup can happen. */
3643
            ret = -TARGET_EFAULT;
3644
        else
3645
            ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3646
        if (p)
3647
            unlock_user(p, arg2, 0);
3648
        break;
3649
#endif
3650
    case TARGET_NR_getpriority:
3651
        /* libc does special remapping of the return value of
3652
         * sys_getpriority() so it's just easiest to call
3653
         * sys_getpriority() directly rather than through libc. */
3654
        ret = sys_getpriority(arg1, arg2);
3655
        break;
3656
    case TARGET_NR_setpriority:
3657
        ret = get_errno(setpriority(arg1, arg2, arg3));
3658
        break;
3659
#ifdef TARGET_NR_profil
3660
    case TARGET_NR_profil:
3661
        goto unimplemented;
3662
#endif
3663
    case TARGET_NR_statfs:
3664
        p = lock_user_string(arg1);
3665
        ret = get_errno(statfs(path(p), &stfs));
3666
        unlock_user(p, arg1, 0);
3667
    convert_statfs:
3668
        if (!is_error(ret)) {
3669
            struct target_statfs *target_stfs;
3670

    
3671
            lock_user_struct(target_stfs, arg2, 0);
3672
            /* ??? put_user is probably wrong.  */
3673
            put_user(stfs.f_type, &target_stfs->f_type);
3674
            put_user(stfs.f_bsize, &target_stfs->f_bsize);
3675
            put_user(stfs.f_blocks, &target_stfs->f_blocks);
3676
            put_user(stfs.f_bfree, &target_stfs->f_bfree);
3677
            put_user(stfs.f_bavail, &target_stfs->f_bavail);
3678
            put_user(stfs.f_files, &target_stfs->f_files);
3679
            put_user(stfs.f_ffree, &target_stfs->f_ffree);
3680
            put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3681
            put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3682
            put_user(stfs.f_namelen, &target_stfs->f_namelen);
3683
            unlock_user_struct(target_stfs, arg2, 1);
3684
        }
3685
        break;
3686
    case TARGET_NR_fstatfs:
3687
        ret = get_errno(fstatfs(arg1, &stfs));
3688
        goto convert_statfs;
3689
#ifdef TARGET_NR_statfs64
3690
    case TARGET_NR_statfs64:
3691
        p = lock_user_string(arg1);
3692
        ret = get_errno(statfs(path(p), &stfs));
3693
        unlock_user(p, arg1, 0);
3694
    convert_statfs64:
3695
        if (!is_error(ret)) {
3696
            struct target_statfs64 *target_stfs;
3697

    
3698
            lock_user_struct(target_stfs, arg3, 0);
3699
            /* ??? put_user is probably wrong.  */
3700
            put_user(stfs.f_type, &target_stfs->f_type);
3701
            put_user(stfs.f_bsize, &target_stfs->f_bsize);
3702
            put_user(stfs.f_blocks, &target_stfs->f_blocks);
3703
            put_user(stfs.f_bfree, &target_stfs->f_bfree);
3704
            put_user(stfs.f_bavail, &target_stfs->f_bavail);
3705
            put_user(stfs.f_files, &target_stfs->f_files);
3706
            put_user(stfs.f_ffree, &target_stfs->f_ffree);
3707
            put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3708
            put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3709
            put_user(stfs.f_namelen, &target_stfs->f_namelen);
3710
            unlock_user_struct(target_stfs, arg3, 0);
3711
        }
3712
        break;
3713
    case TARGET_NR_fstatfs64:
3714
        ret = get_errno(fstatfs(arg1, &stfs));
3715
        goto convert_statfs64;
3716
#endif
3717
#ifdef TARGET_NR_ioperm
3718
    case TARGET_NR_ioperm:
3719
        goto unimplemented;
3720
#endif
3721
#ifdef TARGET_NR_socketcall
3722
    case TARGET_NR_socketcall:
3723
        ret = do_socketcall(arg1, arg2);
3724
        break;
3725
#endif
3726
#ifdef TARGET_NR_accept
3727
    case TARGET_NR_accept:
3728
        ret = do_accept(arg1, arg2, arg3);
3729
        break;
3730
#endif
3731
#ifdef TARGET_NR_bind
3732
    case TARGET_NR_bind:
3733
        ret = do_bind(arg1, arg2, arg3);
3734
        break;
3735
#endif
3736
#ifdef TARGET_NR_connect
3737
    case TARGET_NR_connect:
3738
        ret = do_connect(arg1, arg2, arg3);
3739
        break;
3740
#endif
3741
#ifdef TARGET_NR_getpeername
3742
    case TARGET_NR_getpeername:
3743
        ret = do_getpeername(arg1, arg2, arg3);
3744
        break;
3745
#endif
3746
#ifdef TARGET_NR_getsockname
3747
    case TARGET_NR_getsockname:
3748
        ret = do_getsockname(arg1, arg2, arg3);
3749
        break;
3750
#endif
3751
#ifdef TARGET_NR_getsockopt
3752
    case TARGET_NR_getsockopt:
3753
        ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3754
        break;
3755
#endif
3756
#ifdef TARGET_NR_listen
3757
    case TARGET_NR_listen:
3758
        ret = get_errno(listen(arg1, arg2));
3759
        break;
3760
#endif
3761
#ifdef TARGET_NR_recv
3762
    case TARGET_NR_recv:
3763
        ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3764
        break;
3765
#endif
3766
#ifdef TARGET_NR_recvfrom
3767
    case TARGET_NR_recvfrom:
3768
        ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3769
        break;
3770
#endif
3771
#ifdef TARGET_NR_recvmsg
3772
    case TARGET_NR_recvmsg:
3773
        ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3774
        break;
3775
#endif
3776
#ifdef TARGET_NR_send
3777
    case TARGET_NR_send:
3778
        ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3779
        break;
3780
#endif
3781
#ifdef TARGET_NR_sendmsg
3782
    case TARGET_NR_sendmsg:
3783
        ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3784
        break;
3785
#endif
3786
#ifdef TARGET_NR_sendto
3787
    case TARGET_NR_sendto:
3788
        ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3789
        break;
3790
#endif
3791
#ifdef TARGET_NR_shutdown
3792
    case TARGET_NR_shutdown:
3793
        ret = get_errno(shutdown(arg1, arg2));
3794
        break;
3795
#endif
3796
#ifdef TARGET_NR_socket
3797
    case TARGET_NR_socket:
3798
        ret = do_socket(arg1, arg2, arg3);
3799
        break;
3800
#endif
3801
#ifdef TARGET_NR_socketpair
3802
    case TARGET_NR_socketpair:
3803
        ret = do_socketpair(arg1, arg2, arg3, arg4);
3804
        break;
3805
#endif
3806
#ifdef TARGET_NR_setsockopt
3807
    case TARGET_NR_setsockopt:
3808
        ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3809
        break;
3810
#endif
3811

    
3812
    case TARGET_NR_syslog:
3813
        p = lock_user_string(arg2);
3814
        ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3815
        unlock_user(p, arg2, 0);
3816
        break;
3817

    
3818
    case TARGET_NR_setitimer:
3819
        {
3820
            struct itimerval value, ovalue, *pvalue;
3821

    
3822
            if (arg2) {
3823
                pvalue = &value;
3824
                target_to_host_timeval(&pvalue->it_interval,
3825
                                       arg2);
3826
                target_to_host_timeval(&pvalue->it_value,
3827
                                       arg2 + sizeof(struct target_timeval));
3828
            } else {
3829
                pvalue = NULL;
3830
            }
3831
            ret = get_errno(setitimer(arg1, pvalue, &ovalue));
3832
            if (!is_error(ret) && arg3) {
3833
                host_to_target_timeval(arg3,
3834
                                       &ovalue.it_interval);
3835
                host_to_target_timeval(arg3 + sizeof(struct target_timeval),
3836
                                       &ovalue.it_value);
3837
            }
3838
        }
3839
        break;
3840
    case TARGET_NR_getitimer:
3841
        {
3842
            struct itimerval value;
3843

    
3844
            ret = get_errno(getitimer(arg1, &value));
3845
            if (!is_error(ret) && arg2) {
3846
                host_to_target_timeval(arg2,
3847
                                       &value.it_interval);
3848
                host_to_target_timeval(arg2 + sizeof(struct target_timeval),
3849
                                       &value.it_value);
3850
            }
3851
        }
3852
        break;
3853
    case TARGET_NR_stat:
3854
        p = lock_user_string(arg1);
3855
        ret = get_errno(stat(path(p), &st));
3856
        unlock_user(p, arg1, 0);
3857
        goto do_stat;
3858
    case TARGET_NR_lstat:
3859
        p = lock_user_string(arg1);
3860
        ret = get_errno(lstat(path(p), &st));
3861
        unlock_user(p, arg1, 0);
3862
        goto do_stat;
3863
    case TARGET_NR_fstat:
3864
        {
3865
            ret = get_errno(fstat(arg1, &st));
3866
        do_stat:
3867
            if (!is_error(ret)) {
3868
                struct target_stat *target_st;
3869

    
3870
                lock_user_struct(target_st, arg2, 0);
3871
#if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32))
3872
                target_st->st_dev = tswap32(st.st_dev);
3873
#else
3874
                target_st->st_dev = tswap16(st.st_dev);
3875
#endif
3876
                target_st->st_ino = tswapl(st.st_ino);
3877
#if defined(TARGET_PPC) || defined(TARGET_MIPS)
3878
                target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
3879
                target_st->st_uid = tswap32(st.st_uid);
3880
                target_st->st_gid = tswap32(st.st_gid);
3881
#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
3882
                target_st->st_mode = tswap32(st.st_mode);
3883
                target_st->st_uid = tswap32(st.st_uid);
3884
                target_st->st_gid = tswap32(st.st_gid);
3885
#else
3886
                target_st->st_mode = tswap16(st.st_mode);
3887
                target_st->st_uid = tswap16(st.st_uid);
3888
                target_st->st_gid = tswap16(st.st_gid);
3889
#endif
3890
#if defined(TARGET_MIPS)
3891
                /* If this is the same on PPC, then just merge w/ the above ifdef */
3892
                target_st->st_nlink = tswapl(st.st_nlink);
3893
                target_st->st_rdev = tswapl(st.st_rdev);
3894
#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
3895
                target_st->st_nlink = tswap32(st.st_nlink);
3896
                target_st->st_rdev = tswap32(st.st_rdev);
3897
#else
3898
                target_st->st_nlink = tswap16(st.st_nlink);
3899
                target_st->st_rdev = tswap16(st.st_rdev);
3900
#endif
3901
                target_st->st_size = tswapl(st.st_size);
3902
                target_st->st_blksize = tswapl(st.st_blksize);
3903
                target_st->st_blocks = tswapl(st.st_blocks);
3904
                target_st->target_st_atime = tswapl(st.st_atime);
3905
                target_st->target_st_mtime = tswapl(st.st_mtime);
3906
                target_st->target_st_ctime = tswapl(st.st_ctime);
3907
                unlock_user_struct(target_st, arg2, 1);
3908
            }
3909
        }
3910
        break;
3911
#ifdef TARGET_NR_olduname
3912
    case TARGET_NR_olduname:
3913
        goto unimplemented;
3914
#endif
3915
#ifdef TARGET_NR_iopl
3916
    case TARGET_NR_iopl:
3917
        goto unimplemented;
3918
#endif
3919
    case TARGET_NR_vhangup:
3920
        ret = get_errno(vhangup());
3921
        break;
3922
#ifdef TARGET_NR_idle
3923
    case TARGET_NR_idle:
3924
        goto unimplemented;
3925
#endif
3926
#ifdef TARGET_NR_syscall
3927
    case TARGET_NR_syscall:
3928
            ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
3929
            break;
3930
#endif
3931
    case TARGET_NR_wait4:
3932
        {
3933
            int status;
3934
            abi_long status_ptr = arg2;
3935
            struct rusage rusage, *rusage_ptr;
3936
            abi_ulong target_rusage = arg4;
3937
            if (target_rusage)
3938
                rusage_ptr = &rusage;
3939
            else
3940
                rusage_ptr = NULL;
3941
            ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
3942
            if (!is_error(ret)) {
3943
                if (status_ptr)
3944
                    tputl(status_ptr, status);
3945
                if (target_rusage) {
3946
                    host_to_target_rusage(target_rusage, &rusage);
3947
                }
3948
            }
3949
        }
3950
        break;
3951
#ifdef TARGET_NR_swapoff
3952
    case TARGET_NR_swapoff:
3953
        p = lock_user_string(arg1);
3954
        ret = get_errno(swapoff(p));
3955
        unlock_user(p, arg1, 0);
3956
        break;
3957
#endif
3958
    case TARGET_NR_sysinfo:
3959
        {
3960
            struct target_sysinfo *target_value;
3961
            struct sysinfo value;
3962
            ret = get_errno(sysinfo(&value));
3963
            if (!is_error(ret) && arg1)
3964
            {
3965
                /* ??? __put_user is probably wrong.  */
3966
                lock_user_struct(target_value, arg1, 0);
3967
                __put_user(value.uptime, &target_value->uptime);
3968
                __put_user(value.loads[0], &target_value->loads[0]);
3969
                __put_user(value.loads[1], &target_value->loads[1]);
3970
                __put_user(value.loads[2], &target_value->loads[2]);
3971
                __put_user(value.totalram, &target_value->totalram);
3972
                __put_user(value.freeram, &target_value->freeram);
3973
                __put_user(value.sharedram, &target_value->sharedram);
3974
                __put_user(value.bufferram, &target_value->bufferram);
3975
                __put_user(value.totalswap, &target_value->totalswap);
3976
                __put_user(value.freeswap, &target_value->freeswap);
3977
                __put_user(value.procs, &target_value->procs);
3978
                __put_user(value.totalhigh, &target_value->totalhigh);
3979
                __put_user(value.freehigh, &target_value->freehigh);
3980
                __put_user(value.mem_unit, &target_value->mem_unit);
3981
                unlock_user_struct(target_value, arg1, 1);
3982
            }
3983
        }
3984
        break;
3985
#ifdef TARGET_NR_ipc
3986
    case TARGET_NR_ipc:
3987
        ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
3988
        break;
3989
#endif
3990
    case TARGET_NR_fsync:
3991
        ret = get_errno(fsync(arg1));
3992
        break;
3993
    case TARGET_NR_clone:
3994
        ret = get_errno(do_fork(cpu_env, arg1, arg2));
3995
        break;
3996
#ifdef __NR_exit_group
3997
        /* new thread calls */
3998
    case TARGET_NR_exit_group:
3999
        gdb_exit(cpu_env, arg1);
4000
        ret = get_errno(exit_group(arg1));
4001
        break;
4002
#endif
4003
    case TARGET_NR_setdomainname:
4004
        p = lock_user_string(arg1);
4005
        ret = get_errno(setdomainname(p, arg2));
4006
        unlock_user(p, arg1, 0);
4007
        break;
4008
    case TARGET_NR_uname:
4009
        /* no need to transcode because we use the linux syscall */
4010
        {
4011
            struct new_utsname * buf;
4012

    
4013
            lock_user_struct(buf, arg1, 0);
4014
            ret = get_errno(sys_uname(buf));
4015
            if (!is_error(ret)) {
4016
                /* Overrite the native machine name with whatever is being
4017
                   emulated. */
4018
                strcpy (buf->machine, UNAME_MACHINE);
4019
                /* Allow the user to override the reported release.  */
4020
                if (qemu_uname_release && *qemu_uname_release)
4021
                  strcpy (buf->release, qemu_uname_release);
4022
            }
4023
            unlock_user_struct(buf, arg1, 1);
4024
        }
4025
        break;
4026
#ifdef TARGET_I386
4027
    case TARGET_NR_modify_ldt:
4028
        ret = get_errno(do_modify_ldt(cpu_env, arg1, arg2, arg3));
4029
        break;
4030
#if !defined(TARGET_X86_64)
4031
    case TARGET_NR_vm86old:
4032
        goto unimplemented;
4033
    case TARGET_NR_vm86:
4034
        ret = do_vm86(cpu_env, arg1, arg2);
4035
        break;
4036
#endif
4037
#endif
4038
    case TARGET_NR_adjtimex:
4039
        goto unimplemented;
4040
#ifdef TARGET_NR_create_module
4041
    case TARGET_NR_create_module:
4042
#endif
4043
    case TARGET_NR_init_module:
4044
    case TARGET_NR_delete_module:
4045
#ifdef TARGET_NR_get_kernel_syms
4046
    case TARGET_NR_get_kernel_syms:
4047
#endif
4048
        goto unimplemented;
4049
    case TARGET_NR_quotactl:
4050
        goto unimplemented;
4051
    case TARGET_NR_getpgid:
4052
        ret = get_errno(getpgid(arg1));
4053
        break;
4054
    case TARGET_NR_fchdir:
4055
        ret = get_errno(fchdir(arg1));
4056
        break;
4057
#ifdef TARGET_NR_bdflush /* not on x86_64 */
4058
    case TARGET_NR_bdflush:
4059
        goto unimplemented;
4060
#endif
4061
#ifdef TARGET_NR_sysfs
4062
    case TARGET_NR_sysfs:
4063
        goto unimplemented;
4064
#endif
4065
    case TARGET_NR_personality:
4066
        ret = get_errno(personality(arg1));
4067
        break;
4068
#ifdef TARGET_NR_afs_syscall
4069
    case TARGET_NR_afs_syscall:
4070
        goto unimplemented;
4071
#endif
4072
#ifdef TARGET_NR__llseek /* Not on alpha */
4073
    case TARGET_NR__llseek:
4074
        {
4075
#if defined (__x86_64__)
4076
            ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
4077
            tput64(arg4, ret);
4078
#else
4079
            int64_t res;
4080
            ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4081
            tput64(arg4, res);
4082
#endif
4083
        }
4084
        break;
4085
#endif
4086
    case TARGET_NR_getdents:
4087
#if TARGET_ABI_BITS != 32
4088
        goto unimplemented;
4089
#warning not supported
4090
#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4091
        {
4092
            struct target_dirent *target_dirp;
4093
            struct dirent *dirp;
4094
            abi_long count = arg3;
4095

    
4096
            dirp = malloc(count);
4097
            if (!dirp) {
4098
                ret = -TARGET_EFAULT;
4099
                goto fail;
4100
            }
4101

    
4102
            ret = get_errno(sys_getdents(arg1, dirp, count));
4103
            if (!is_error(ret)) {
4104
                struct dirent *de;
4105
                struct target_dirent *tde;
4106
                int len = ret;
4107
                int reclen, treclen;
4108
                int count1, tnamelen;
4109

    
4110
                count1 = 0;
4111
                de = dirp;
4112
                target_dirp = lock_user(arg2, count, 0);
4113
                tde = target_dirp;
4114
                while (len > 0) {
4115
                    reclen = de->d_reclen;
4116
                    treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4117
                    tde->d_reclen = tswap16(treclen);
4118
                    tde->d_ino = tswapl(de->d_ino);
4119
                    tde->d_off = tswapl(de->d_off);
4120
                    tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4121
                    if (tnamelen > 256)
4122
                        tnamelen = 256;
4123
                    /* XXX: may not be correct */
4124
                    strncpy(tde->d_name, de->d_name, tnamelen);
4125
                    de = (struct dirent *)((char *)de + reclen);
4126
                    len -= reclen;
4127
                    tde = (struct target_dirent *)((char *)tde + treclen);
4128
                    count1 += treclen;
4129
                }
4130
                ret = count1;
4131
            }
4132
            unlock_user(target_dirp, arg2, ret);
4133
            free(dirp);
4134
        }
4135
#else
4136
        {
4137
            struct dirent *dirp;
4138
            abi_long count = arg3;
4139

    
4140
            dirp = lock_user(arg2, count, 0);
4141
            ret = get_errno(sys_getdents(arg1, dirp, count));
4142
            if (!is_error(ret)) {
4143
                struct dirent *de;
4144
                int len = ret;
4145
                int reclen;
4146
                de = dirp;
4147
                while (len > 0) {
4148
                    reclen = de->d_reclen;
4149
                    if (reclen > len)
4150
                        break;
4151
                    de->d_reclen = tswap16(reclen);
4152
                    tswapls(&de->d_ino);
4153
                    tswapls(&de->d_off);
4154
                    de = (struct dirent *)((char *)de + reclen);
4155
                    len -= reclen;
4156
                }
4157
            }
4158
            unlock_user(dirp, arg2, ret);
4159
        }
4160
#endif
4161
        break;
4162
#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
4163
    case TARGET_NR_getdents64:
4164
        {
4165
            struct dirent64 *dirp;
4166
            abi_long count = arg3;
4167
            dirp = lock_user(arg2, count, 0);
4168
            ret = get_errno(sys_getdents64(arg1, dirp, count));
4169
            if (!is_error(ret)) {
4170
                struct dirent64 *de;
4171
                int len = ret;
4172
                int reclen;
4173
                de = dirp;
4174
                while (len > 0) {
4175
                    reclen = de->d_reclen;
4176
                    if (reclen > len)
4177
                        break;
4178
                    de->d_reclen = tswap16(reclen);
4179
                    tswap64s(&de->d_ino);
4180
                    tswap64s(&de->d_off);
4181
                    de = (struct dirent64 *)((char *)de + reclen);
4182
                    len -= reclen;
4183
                }
4184
            }
4185
            unlock_user(dirp, arg2, ret);
4186
        }
4187
        break;
4188
#endif /* TARGET_NR_getdents64 */
4189
#ifdef TARGET_NR__newselect
4190
    case TARGET_NR__newselect:
4191
        ret = do_select(arg1, arg2, arg3, arg4, arg5);
4192
        break;
4193
#endif
4194
#ifdef TARGET_NR_poll
4195
    case TARGET_NR_poll:
4196
        {
4197
            struct target_pollfd *target_pfd;
4198
            unsigned int nfds = arg2;
4199
            int timeout = arg3;
4200
            struct pollfd *pfd;
4201
            unsigned int i;
4202

    
4203
            target_pfd = lock_user(arg1, sizeof(struct target_pollfd) * nfds, 1);
4204
            pfd = alloca(sizeof(struct pollfd) * nfds);
4205
            for(i = 0; i < nfds; i++) {
4206
                pfd[i].fd = tswap32(target_pfd[i].fd);
4207
                pfd[i].events = tswap16(target_pfd[i].events);
4208
            }
4209
            ret = get_errno(poll(pfd, nfds, timeout));
4210
            if (!is_error(ret)) {
4211
                for(i = 0; i < nfds; i++) {
4212
                    target_pfd[i].revents = tswap16(pfd[i].revents);
4213
                }
4214
                ret += nfds * (sizeof(struct target_pollfd)
4215
                               - sizeof(struct pollfd));
4216
            }
4217
            unlock_user(target_pfd, arg1, ret);
4218
        }
4219
        break;
4220
#endif
4221
    case TARGET_NR_flock:
4222
        /* NOTE: the flock constant seems to be the same for every
4223
           Linux platform */
4224
        ret = get_errno(flock(arg1, arg2));
4225
        break;
4226
    case TARGET_NR_readv:
4227
        {
4228
            int count = arg3;
4229
            struct iovec *vec;
4230

    
4231
            vec = alloca(count * sizeof(struct iovec));
4232
            lock_iovec(vec, arg2, count, 0);
4233
            ret = get_errno(readv(arg1, vec, count));
4234
            unlock_iovec(vec, arg2, count, 1);
4235
        }
4236
        break;
4237
    case TARGET_NR_writev:
4238
        {
4239
            int count = arg3;
4240
            struct iovec *vec;
4241

    
4242
            vec = alloca(count * sizeof(struct iovec));
4243
            lock_iovec(vec, arg2, count, 1);
4244
            ret = get_errno(writev(arg1, vec, count));
4245
            unlock_iovec(vec, arg2, count, 0);
4246
        }
4247
        break;
4248
    case TARGET_NR_getsid:
4249
        ret = get_errno(getsid(arg1));
4250
        break;
4251
#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
4252
    case TARGET_NR_fdatasync:
4253
        ret = get_errno(fdatasync(arg1));
4254
        break;
4255
#endif
4256
    case TARGET_NR__sysctl:
4257
        /* We don't implement this, but ENOTDIR is always a safe
4258
           return value. */
4259
        ret = -TARGET_ENOTDIR;
4260
        break;
4261
    case TARGET_NR_sched_setparam:
4262
        {
4263
            struct sched_param *target_schp;
4264
            struct sched_param schp;
4265

    
4266
            lock_user_struct(target_schp, arg2, 1);
4267
            schp.sched_priority = tswap32(target_schp->sched_priority);
4268
            unlock_user_struct(target_schp, arg2, 0);
4269
            ret = get_errno(sched_setparam(arg1, &schp));
4270
        }
4271
        break;
4272
    case TARGET_NR_sched_getparam:
4273
        {
4274
            struct sched_param *target_schp;
4275
            struct sched_param schp;
4276
            ret = get_errno(sched_getparam(arg1, &schp));
4277
            if (!is_error(ret)) {
4278
                lock_user_struct(target_schp, arg2, 0);
4279
                target_schp->sched_priority = tswap32(schp.sched_priority);
4280
                unlock_user_struct(target_schp, arg2, 1);
4281
            }
4282
        }
4283
        break;
4284
    case TARGET_NR_sched_setscheduler:
4285
        {
4286
            struct sched_param *target_schp;
4287
            struct sched_param schp;
4288
            lock_user_struct(target_schp, arg3, 1);
4289
            schp.sched_priority = tswap32(target_schp->sched_priority);
4290
            unlock_user_struct(target_schp, arg3, 0);
4291
            ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4292
        }
4293
        break;
4294
    case TARGET_NR_sched_getscheduler:
4295
        ret = get_errno(sched_getscheduler(arg1));
4296
        break;
4297
    case TARGET_NR_sched_yield:
4298
        ret = get_errno(sched_yield());
4299
        break;
4300
    case TARGET_NR_sched_get_priority_max:
4301
        ret = get_errno(sched_get_priority_max(arg1));
4302
        break;
4303
    case TARGET_NR_sched_get_priority_min:
4304
        ret = get_errno(sched_get_priority_min(arg1));
4305
        break;
4306
    case TARGET_NR_sched_rr_get_interval:
4307
        {
4308
            struct timespec ts;
4309
            ret = get_errno(sched_rr_get_interval(arg1, &ts));
4310
            if (!is_error(ret)) {
4311
                host_to_target_timespec(arg2, &ts);
4312
            }
4313
        }
4314
        break;
4315
    case TARGET_NR_nanosleep:
4316
        {
4317
            struct timespec req, rem;
4318
            target_to_host_timespec(&req, arg1);
4319
            ret = get_errno(nanosleep(&req, &rem));
4320
            if (is_error(ret) && arg2) {
4321
                host_to_target_timespec(arg2, &rem);
4322
            }
4323
        }
4324
        break;
4325
#ifdef TARGET_NR_query_module
4326
    case TARGET_NR_query_module:
4327
        goto unimplemented;
4328
#endif
4329
#ifdef TARGET_NR_nfsservctl
4330
    case TARGET_NR_nfsservctl:
4331
        goto unimplemented;
4332
#endif
4333
    case TARGET_NR_prctl:
4334
        switch (arg1)
4335
            {
4336
            case PR_GET_PDEATHSIG:
4337
                {
4338
                    int deathsig;
4339
                    ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4340
                    if (!is_error(ret) && arg2)
4341
                        tput32(arg2, deathsig);
4342
                }
4343
                break;
4344
            default:
4345
                ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4346
                break;
4347
            }
4348
        break;
4349
#ifdef TARGET_NR_pread
4350
    case TARGET_NR_pread:
4351
        page_unprotect_range(arg2, arg3);
4352
        p = lock_user(arg2, arg3, 0);
4353
        ret = get_errno(pread(arg1, p, arg3, arg4));
4354
        unlock_user(p, arg2, ret);
4355
        break;
4356
    case TARGET_NR_pwrite:
4357
        p = lock_user(arg2, arg3, 1);
4358
        ret = get_errno(pwrite(arg1, p, arg3, arg4));
4359
        unlock_user(p, arg2, 0);
4360
        break;
4361
#endif
4362
    case TARGET_NR_getcwd:
4363
        p = lock_user(arg1, arg2, 0);
4364
        ret = get_errno(sys_getcwd1(p, arg2));
4365
        unlock_user(p, arg1, ret);
4366
        break;
4367
    case TARGET_NR_capget:
4368
        goto unimplemented;
4369
    case TARGET_NR_capset:
4370
        goto unimplemented;
4371
    case TARGET_NR_sigaltstack:
4372
#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4373
    defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
4374
        ret = do_sigaltstack((struct target_sigaltstack *)arg1,
4375
                             (struct target_sigaltstack *)arg2,
4376
                             get_sp_from_cpustate((CPUState *)cpu_env));
4377
        break;
4378
#else
4379
        goto unimplemented;
4380
#endif
4381
    case TARGET_NR_sendfile:
4382
        goto unimplemented;
4383
#ifdef TARGET_NR_getpmsg
4384
    case TARGET_NR_getpmsg:
4385
        goto unimplemented;
4386
#endif
4387
#ifdef TARGET_NR_putpmsg
4388
    case TARGET_NR_putpmsg:
4389
        goto unimplemented;
4390
#endif
4391
#ifdef TARGET_NR_vfork
4392
    case TARGET_NR_vfork:
4393
        ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
4394
        break;
4395
#endif
4396
#ifdef TARGET_NR_ugetrlimit
4397
    case TARGET_NR_ugetrlimit:
4398
    {
4399
        struct rlimit rlim;
4400
        ret = get_errno(getrlimit(arg1, &rlim));
4401
        if (!is_error(ret)) {
4402
            struct target_rlimit *target_rlim;
4403
            lock_user_struct(target_rlim, arg2, 0);
4404
            target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4405
            target_rlim->rlim_max = tswapl(rlim.rlim_max);
4406
            unlock_user_struct(target_rlim, arg2, 1);
4407
        }
4408
        break;
4409
    }
4410
#endif
4411
#ifdef TARGET_NR_truncate64
4412
    case TARGET_NR_truncate64:
4413
        p = lock_user_string(arg1);
4414
        ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4415
        unlock_user(p, arg1, 0);
4416
        break;
4417
#endif
4418
#ifdef TARGET_NR_ftruncate64
4419
    case TARGET_NR_ftruncate64:
4420
        ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
4421
        break;
4422
#endif
4423
#ifdef TARGET_NR_stat64
4424
    case TARGET_NR_stat64:
4425
        p = lock_user_string(arg1);
4426
        ret = get_errno(stat(path(p), &st));
4427
        unlock_user(p, arg1, 0);
4428
        goto do_stat64;
4429
#endif
4430
#ifdef TARGET_NR_lstat64
4431
    case TARGET_NR_lstat64:
4432
        p = lock_user_string(arg1);
4433
        ret = get_errno(lstat(path(p), &st));
4434
        unlock_user(p, arg1, 0);
4435
        goto do_stat64;
4436
#endif
4437
#ifdef TARGET_NR_fstat64
4438
    case TARGET_NR_fstat64:
4439
        {
4440
            ret = get_errno(fstat(arg1, &st));
4441
        do_stat64:
4442
            if (!is_error(ret)) {
4443
#ifdef TARGET_ARM
4444
                if (((CPUARMState *)cpu_env)->eabi) {
4445
                    struct target_eabi_stat64 *target_st;
4446
                    lock_user_struct(target_st, arg2, 1);
4447
                    memset(target_st, 0, sizeof(struct target_eabi_stat64));
4448
                    /* put_user is probably wrong.  */
4449
                    put_user(st.st_dev, &target_st->st_dev);
4450
                    put_user(st.st_ino, &target_st->st_ino);
4451
#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4452
                    put_user(st.st_ino, &target_st->__st_ino);
4453
#endif
4454
                    put_user(st.st_mode, &target_st->st_mode);
4455
                    put_user(st.st_nlink, &target_st->st_nlink);
4456
                    put_user(st.st_uid, &target_st->st_uid);
4457
                    put_user(st.st_gid, &target_st->st_gid);
4458
                    put_user(st.st_rdev, &target_st->st_rdev);
4459
                    /* XXX: better use of kernel struct */
4460
                    put_user(st.st_size, &target_st->st_size);
4461
                    put_user(st.st_blksize, &target_st->st_blksize);
4462
                    put_user(st.st_blocks, &target_st->st_blocks);
4463
                    put_user(st.st_atime, &target_st->target_st_atime);
4464
                    put_user(st.st_mtime, &target_st->target_st_mtime);
4465
                    put_user(st.st_ctime, &target_st->target_st_ctime);
4466
                    unlock_user_struct(target_st, arg2, 0);
4467
                } else
4468
#endif
4469
                {
4470
                    struct target_stat64 *target_st;
4471
                    lock_user_struct(target_st, arg2, 1);
4472
                    memset(target_st, 0, sizeof(struct target_stat64));
4473
                    /* ??? put_user is probably wrong.  */
4474
                    put_user(st.st_dev, &target_st->st_dev);
4475
                    put_user(st.st_ino, &target_st->st_ino);
4476
#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4477
                    put_user(st.st_ino, &target_st->__st_ino);
4478
#endif
4479
                    put_user(st.st_mode, &target_st->st_mode);
4480
                    put_user(st.st_nlink, &target_st->st_nlink);
4481
                    put_user(st.st_uid, &target_st->st_uid);
4482
                    put_user(st.st_gid, &target_st->st_gid);
4483
                    put_user(st.st_rdev, &target_st->st_rdev);
4484
                    /* XXX: better use of kernel struct */
4485
                    put_user(st.st_size, &target_st->st_size);
4486
                    put_user(st.st_blksize, &target_st->st_blksize);
4487
                    put_user(st.st_blocks, &target_st->st_blocks);
4488
                    put_user(st.st_atime, &target_st->target_st_atime);
4489
                    put_user(st.st_mtime, &target_st->target_st_mtime);
4490
                    put_user(st.st_ctime, &target_st->target_st_ctime);
4491
                    unlock_user_struct(target_st, arg2, 0);
4492
                }
4493
            }
4494
        }
4495
        break;
4496
#endif
4497
#ifdef USE_UID16
4498
    case TARGET_NR_lchown:
4499
        p = lock_user_string(arg1);
4500
        ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4501
        unlock_user(p, arg1, 0);
4502
        break;
4503
    case TARGET_NR_getuid:
4504
        ret = get_errno(high2lowuid(getuid()));
4505
        break;
4506
    case TARGET_NR_getgid:
4507
        ret = get_errno(high2lowgid(getgid()));
4508
        break;
4509
    case TARGET_NR_geteuid:
4510
        ret = get_errno(high2lowuid(geteuid()));
4511
        break;
4512
    case TARGET_NR_getegid:
4513
        ret = get_errno(high2lowgid(getegid()));
4514
        break;
4515
    case TARGET_NR_setreuid:
4516
        ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4517
        break;
4518
    case TARGET_NR_setregid:
4519
        ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4520
        break;
4521
    case TARGET_NR_getgroups:
4522
        {
4523
            int gidsetsize = arg1;
4524
            uint16_t *target_grouplist;
4525
            gid_t *grouplist;
4526
            int i;
4527

    
4528
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4529
            ret = get_errno(getgroups(gidsetsize, grouplist));
4530
            if (!is_error(ret)) {
4531
                target_grouplist = lock_user(arg2, gidsetsize * 2, 0);
4532
                for(i = 0;i < gidsetsize; i++)
4533
                    target_grouplist[i] = tswap16(grouplist[i]);
4534
                unlock_user(target_grouplist, arg2, gidsetsize * 2);
4535
            }
4536
        }
4537
        break;
4538
    case TARGET_NR_setgroups:
4539
        {
4540
            int gidsetsize = arg1;
4541
            uint16_t *target_grouplist;
4542
            gid_t *grouplist;
4543
            int i;
4544

    
4545
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4546
            target_grouplist = lock_user(arg2, gidsetsize * 2, 1);
4547
            for(i = 0;i < gidsetsize; i++)
4548
                grouplist[i] = tswap16(target_grouplist[i]);
4549
            unlock_user(target_grouplist, arg2, 0);
4550
            ret = get_errno(setgroups(gidsetsize, grouplist));
4551
        }
4552
        break;
4553
    case TARGET_NR_fchown:
4554
        ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4555
        break;
4556
#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4557
    case TARGET_NR_fchownat:
4558
        if (!arg2) {
4559
            ret = -TARGET_EFAULT;
4560
            goto fail;
4561
        }
4562
        p = lock_user_string(arg2);
4563
        if (!access_ok(VERIFY_READ, p, 1))
4564
            /* Don't "goto fail" so that cleanup can happen. */
4565
            ret = -TARGET_EFAULT;
4566
        else
4567
            ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4568
        if (p)
4569
            unlock_user(p, arg2, 0);
4570
        break;
4571
#endif
4572
#ifdef TARGET_NR_setresuid
4573
    case TARGET_NR_setresuid:
4574
        ret = get_errno(setresuid(low2highuid(arg1),
4575
                                  low2highuid(arg2),
4576
                                  low2highuid(arg3)));
4577
        break;
4578
#endif
4579
#ifdef TARGET_NR_getresuid
4580
    case TARGET_NR_getresuid:
4581
        {
4582
            uid_t ruid, euid, suid;
4583
            ret = get_errno(getresuid(&ruid, &euid, &suid));
4584
            if (!is_error(ret)) {
4585
                tput16(arg1, tswap16(high2lowuid(ruid)));
4586
                tput16(arg2, tswap16(high2lowuid(euid)));
4587
                tput16(arg3, tswap16(high2lowuid(suid)));
4588
            }
4589
        }
4590
        break;
4591
#endif
4592
#ifdef TARGET_NR_getresgid
4593
    case TARGET_NR_setresgid:
4594
        ret = get_errno(setresgid(low2highgid(arg1),
4595
                                  low2highgid(arg2),
4596
                                  low2highgid(arg3)));
4597
        break;
4598
#endif
4599
#ifdef TARGET_NR_getresgid
4600
    case TARGET_NR_getresgid:
4601
        {
4602
            gid_t rgid, egid, sgid;
4603
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
4604
            if (!is_error(ret)) {
4605
                tput16(arg1, tswap16(high2lowgid(rgid)));
4606
                tput16(arg2, tswap16(high2lowgid(egid)));
4607
                tput16(arg3, tswap16(high2lowgid(sgid)));
4608
            }
4609
        }
4610
        break;
4611
#endif
4612
    case TARGET_NR_chown:
4613
        p = lock_user_string(arg1);
4614
        ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4615
        unlock_user(p, arg1, 0);
4616
        break;
4617
    case TARGET_NR_setuid:
4618
        ret = get_errno(setuid(low2highuid(arg1)));
4619
        break;
4620
    case TARGET_NR_setgid:
4621
        ret = get_errno(setgid(low2highgid(arg1)));
4622
        break;
4623
    case TARGET_NR_setfsuid:
4624
        ret = get_errno(setfsuid(arg1));
4625
        break;
4626
    case TARGET_NR_setfsgid:
4627
        ret = get_errno(setfsgid(arg1));
4628
        break;
4629
#endif /* USE_UID16 */
4630

    
4631
#ifdef TARGET_NR_lchown32
4632
    case TARGET_NR_lchown32:
4633
        p = lock_user_string(arg1);
4634
        ret = get_errno(lchown(p, arg2, arg3));
4635
        unlock_user(p, arg1, 0);
4636
        break;
4637
#endif
4638
#ifdef TARGET_NR_getuid32
4639
    case TARGET_NR_getuid32:
4640
        ret = get_errno(getuid());
4641
        break;
4642
#endif
4643
#ifdef TARGET_NR_getgid32
4644
    case TARGET_NR_getgid32:
4645
        ret = get_errno(getgid());
4646
        break;
4647
#endif
4648
#ifdef TARGET_NR_geteuid32
4649
    case TARGET_NR_geteuid32:
4650
        ret = get_errno(geteuid());
4651
        break;
4652
#endif
4653
#ifdef TARGET_NR_getegid32
4654
    case TARGET_NR_getegid32:
4655
        ret = get_errno(getegid());
4656
        break;
4657
#endif
4658
#ifdef TARGET_NR_setreuid32
4659
    case TARGET_NR_setreuid32:
4660
        ret = get_errno(setreuid(arg1, arg2));
4661
        break;
4662
#endif
4663
#ifdef TARGET_NR_setregid32
4664
    case TARGET_NR_setregid32:
4665
        ret = get_errno(setregid(arg1, arg2));
4666
        break;
4667
#endif
4668
#ifdef TARGET_NR_getgroups32
4669
    case TARGET_NR_getgroups32:
4670
        {
4671
            int gidsetsize = arg1;
4672
            uint32_t *target_grouplist;
4673
            gid_t *grouplist;
4674
            int i;
4675

    
4676
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4677
            ret = get_errno(getgroups(gidsetsize, grouplist));
4678
            if (!is_error(ret)) {
4679
                target_grouplist = lock_user(arg2, gidsetsize * 4, 0);
4680
                for(i = 0;i < gidsetsize; i++)
4681
                    target_grouplist[i] = tswap32(grouplist[i]);
4682
                unlock_user(target_grouplist, arg2, gidsetsize * 4);
4683
            }
4684
        }
4685
        break;
4686
#endif
4687
#ifdef TARGET_NR_setgroups32
4688
    case TARGET_NR_setgroups32:
4689
        {
4690
            int gidsetsize = arg1;
4691
            uint32_t *target_grouplist;
4692
            gid_t *grouplist;
4693
            int i;
4694

    
4695
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4696
            target_grouplist = lock_user(arg2, gidsetsize * 4, 1);
4697
            for(i = 0;i < gidsetsize; i++)
4698
                grouplist[i] = tswap32(target_grouplist[i]);
4699
            unlock_user(target_grouplist, arg2, 0);
4700
            ret = get_errno(setgroups(gidsetsize, grouplist));
4701
        }
4702
        break;
4703
#endif
4704
#ifdef TARGET_NR_fchown32
4705
    case TARGET_NR_fchown32:
4706
        ret = get_errno(fchown(arg1, arg2, arg3));
4707
        break;
4708
#endif
4709
#ifdef TARGET_NR_setresuid32
4710
    case TARGET_NR_setresuid32:
4711
        ret = get_errno(setresuid(arg1, arg2, arg3));
4712
        break;
4713
#endif
4714
#ifdef TARGET_NR_getresuid32
4715
    case TARGET_NR_getresuid32:
4716
        {
4717
            uid_t ruid, euid, suid;
4718
            ret = get_errno(getresuid(&ruid, &euid, &suid));
4719
            if (!is_error(ret)) {
4720
                tput32(arg1, tswap32(ruid));
4721
                tput32(arg2, tswap32(euid));
4722
                tput32(arg3, tswap32(suid));
4723
            }
4724
        }
4725
        break;
4726
#endif
4727
#ifdef TARGET_NR_setresgid32
4728
    case TARGET_NR_setresgid32:
4729
        ret = get_errno(setresgid(arg1, arg2, arg3));
4730
        break;
4731
#endif
4732
#ifdef TARGET_NR_getresgid32
4733
    case TARGET_NR_getresgid32:
4734
        {
4735
            gid_t rgid, egid, sgid;
4736
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
4737
            if (!is_error(ret)) {
4738
                tput32(arg1, tswap32(rgid));
4739
                tput32(arg2, tswap32(egid));
4740
                tput32(arg3, tswap32(sgid));
4741
            }
4742
        }
4743
        break;
4744
#endif
4745
#ifdef TARGET_NR_chown32
4746
    case TARGET_NR_chown32:
4747
        p = lock_user_string(arg1);
4748
        ret = get_errno(chown(p, arg2, arg3));
4749
        unlock_user(p, arg1, 0);
4750
        break;
4751
#endif
4752
#ifdef TARGET_NR_setuid32
4753
    case TARGET_NR_setuid32:
4754
        ret = get_errno(setuid(arg1));
4755
        break;
4756
#endif
4757
#ifdef TARGET_NR_setgid32
4758
    case TARGET_NR_setgid32:
4759
        ret = get_errno(setgid(arg1));
4760
        break;
4761
#endif
4762
#ifdef TARGET_NR_setfsuid32
4763
    case TARGET_NR_setfsuid32:
4764
        ret = get_errno(setfsuid(arg1));
4765
        break;
4766
#endif
4767
#ifdef TARGET_NR_setfsgid32
4768
    case TARGET_NR_setfsgid32:
4769
        ret = get_errno(setfsgid(arg1));
4770
        break;
4771
#endif
4772

    
4773
    case TARGET_NR_pivot_root:
4774
        goto unimplemented;
4775
#ifdef TARGET_NR_mincore
4776
    case TARGET_NR_mincore:
4777
        goto unimplemented;
4778
#endif
4779
#ifdef TARGET_NR_madvise
4780
    case TARGET_NR_madvise:
4781
        /* A straight passthrough may not be safe because qemu sometimes
4782
           turns private flie-backed mappings into anonymous mappings.
4783
           This will break MADV_DONTNEED.
4784
           This is a hint, so ignoring and returning success is ok.  */
4785
        ret = get_errno(0);
4786
        break;
4787
#endif
4788
#if TARGET_ABI_BITS == 32
4789
    case TARGET_NR_fcntl64:
4790
    {
4791
        int cmd;
4792
        struct flock64 fl;
4793
        struct target_flock64 *target_fl;
4794
#ifdef TARGET_ARM
4795
        struct target_eabi_flock64 *target_efl;
4796
#endif
4797

    
4798
        switch(arg2){
4799
        case TARGET_F_GETLK64:
4800
            cmd = F_GETLK64;
4801
            break;
4802
        case TARGET_F_SETLK64:
4803
            cmd = F_SETLK64;
4804
            break;
4805
        case TARGET_F_SETLKW64:
4806
            cmd = F_SETLK64;
4807
            break;
4808
        default:
4809
            cmd = arg2;
4810
            break;
4811
        }
4812

    
4813
        switch(arg2) {
4814
        case TARGET_F_GETLK64:
4815
#ifdef TARGET_ARM
4816
            if (((CPUARMState *)cpu_env)->eabi) {
4817
                lock_user_struct(target_efl, arg3, 1);
4818
                fl.l_type = tswap16(target_efl->l_type);
4819
                fl.l_whence = tswap16(target_efl->l_whence);
4820
                fl.l_start = tswap64(target_efl->l_start);
4821
                fl.l_len = tswap64(target_efl->l_len);
4822
                fl.l_pid = tswapl(target_efl->l_pid);
4823
                unlock_user_struct(target_efl, arg3, 0);
4824
            } else
4825
#endif
4826
            {
4827
                lock_user_struct(target_fl, arg3, 1);
4828
                fl.l_type = tswap16(target_fl->l_type);
4829
                fl.l_whence = tswap16(target_fl->l_whence);
4830
                fl.l_start = tswap64(target_fl->l_start);
4831
                fl.l_len = tswap64(target_fl->l_len);
4832
                fl.l_pid = tswapl(target_fl->l_pid);
4833
                unlock_user_struct(target_fl, arg3, 0);
4834
            }
4835
            ret = get_errno(fcntl(arg1, cmd, &fl));
4836
            if (ret == 0) {
4837
#ifdef TARGET_ARM
4838
                if (((CPUARMState *)cpu_env)->eabi) {
4839
                    lock_user_struct(target_efl, arg3, 0);
4840
                    target_efl->l_type = tswap16(fl.l_type);
4841
                    target_efl->l_whence = tswap16(fl.l_whence);
4842
                    target_efl->l_start = tswap64(fl.l_start);
4843
                    target_efl->l_len = tswap64(fl.l_len);
4844
                    target_efl->l_pid = tswapl(fl.l_pid);
4845
                    unlock_user_struct(target_efl, arg3, 1);
4846
                } else
4847
#endif
4848
                {
4849
                    lock_user_struct(target_fl, arg3, 0);
4850
                    target_fl->l_type = tswap16(fl.l_type);
4851
                    target_fl->l_whence = tswap16(fl.l_whence);
4852
                    target_fl->l_start = tswap64(fl.l_start);
4853
                    target_fl->l_len = tswap64(fl.l_len);
4854
                    target_fl->l_pid = tswapl(fl.l_pid);
4855
                    unlock_user_struct(target_fl, arg3, 1);
4856
                }
4857
            }
4858
            break;
4859

    
4860
        case TARGET_F_SETLK64:
4861
        case TARGET_F_SETLKW64:
4862
#ifdef TARGET_ARM
4863
            if (((CPUARMState *)cpu_env)->eabi) {
4864
                lock_user_struct(target_efl, arg3, 1);
4865
                fl.l_type = tswap16(target_efl->l_type);
4866
                fl.l_whence = tswap16(target_efl->l_whence);
4867
                fl.l_start = tswap64(target_efl->l_start);
4868
                fl.l_len = tswap64(target_efl->l_len);
4869
                fl.l_pid = tswapl(target_efl->l_pid);
4870
                unlock_user_struct(target_efl, arg3, 0);
4871
            } else
4872
#endif
4873
            {
4874
                lock_user_struct(target_fl, arg3, 1);
4875
                fl.l_type = tswap16(target_fl->l_type);
4876
                fl.l_whence = tswap16(target_fl->l_whence);
4877
                fl.l_start = tswap64(target_fl->l_start);
4878
                fl.l_len = tswap64(target_fl->l_len);
4879
                fl.l_pid = tswapl(target_fl->l_pid);
4880
                unlock_user_struct(target_fl, arg3, 0);
4881
            }
4882
            ret = get_errno(fcntl(arg1, cmd, &fl));
4883
            break;
4884
        default:
4885
            ret = get_errno(do_fcntl(arg1, cmd, arg3));
4886
            break;
4887
        }
4888
        break;
4889
    }
4890
#endif
4891
#ifdef TARGET_NR_cacheflush
4892
    case TARGET_NR_cacheflush:
4893
        /* self-modifying code is handled automatically, so nothing needed */
4894
        ret = 0;
4895
        break;
4896
#endif
4897
#ifdef TARGET_NR_security
4898
    case TARGET_NR_security:
4899
        goto unimplemented;
4900
#endif
4901
#ifdef TARGET_NR_getpagesize
4902
    case TARGET_NR_getpagesize:
4903
        ret = TARGET_PAGE_SIZE;
4904
        break;
4905
#endif
4906
    case TARGET_NR_gettid:
4907
        ret = get_errno(gettid());
4908
        break;
4909
#ifdef TARGET_NR_readahead
4910
    case TARGET_NR_readahead:
4911
        goto unimplemented;
4912
#endif
4913
#ifdef TARGET_NR_setxattr
4914
    case TARGET_NR_setxattr:
4915
    case TARGET_NR_lsetxattr:
4916
    case TARGET_NR_fsetxattr:
4917
    case TARGET_NR_getxattr:
4918
    case TARGET_NR_lgetxattr:
4919
    case TARGET_NR_fgetxattr:
4920
    case TARGET_NR_listxattr:
4921
    case TARGET_NR_llistxattr:
4922
    case TARGET_NR_flistxattr:
4923
    case TARGET_NR_removexattr:
4924
    case TARGET_NR_lremovexattr:
4925
    case TARGET_NR_fremovexattr:
4926
        goto unimplemented_nowarn;
4927
#endif
4928
#ifdef TARGET_NR_set_thread_area
4929
    case TARGET_NR_set_thread_area:
4930
#ifdef TARGET_MIPS
4931
      ((CPUMIPSState *) cpu_env)->tls_value = arg1;
4932
      ret = 0;
4933
      break;
4934
#else
4935
      goto unimplemented_nowarn;
4936
#endif
4937
#endif
4938
#ifdef TARGET_NR_get_thread_area
4939
    case TARGET_NR_get_thread_area:
4940
        goto unimplemented_nowarn;
4941
#endif
4942
#ifdef TARGET_NR_getdomainname
4943
    case TARGET_NR_getdomainname:
4944
        goto unimplemented_nowarn;
4945
#endif
4946

    
4947
#ifdef TARGET_NR_clock_gettime
4948
    case TARGET_NR_clock_gettime:
4949
    {
4950
        struct timespec ts;
4951
        ret = get_errno(clock_gettime(arg1, &ts));
4952
        if (!is_error(ret)) {
4953
            host_to_target_timespec(arg2, &ts);
4954
        }
4955
        break;
4956
    }
4957
#endif
4958
#ifdef TARGET_NR_clock_getres
4959
    case TARGET_NR_clock_getres:
4960
    {
4961
        struct timespec ts;
4962
        ret = get_errno(clock_getres(arg1, &ts));
4963
        if (!is_error(ret)) {
4964
            host_to_target_timespec(arg2, &ts);
4965
        }
4966
        break;
4967
    }
4968
#endif
4969

    
4970
#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
4971
    case TARGET_NR_set_tid_address:
4972
      ret = get_errno(set_tid_address((int *) arg1));
4973
      break;
4974
#endif
4975

    
4976
#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
4977
    case TARGET_NR_tkill:
4978
        ret = get_errno(sys_tkill((int)arg1, (int)arg2));
4979
        break;
4980
#endif
4981

    
4982
#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
4983
    case TARGET_NR_tgkill:
4984
        ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
4985
        break;
4986
#endif
4987

    
4988
#ifdef TARGET_NR_set_robust_list
4989
    case TARGET_NR_set_robust_list:
4990
        goto unimplemented_nowarn;
4991
#endif
4992

    
4993
#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
4994
    case TARGET_NR_utimensat:
4995
        {
4996
            struct timespec ts[2];
4997
            target_to_host_timespec(ts, arg3);
4998
            target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
4999
            if (!arg2)
5000
                ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5001
            else {
5002
                p = lock_user_string(arg2);
5003
                if (!access_ok(VERIFY_READ, p, 1))
5004
                    /* Don't "goto fail" so that cleanup can happen. */
5005
                    ret = -TARGET_EFAULT;
5006
                else
5007
                    ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5008
                if (p)
5009
                    unlock_user(p, arg2, 0);
5010
            }
5011
        }
5012
        break;
5013
#endif
5014

    
5015
    default:
5016
    unimplemented:
5017
        gemu_log("qemu: Unsupported syscall: %d\n", num);
<