Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ 03acab66

History | View | Annotate | Download (162.3 kB)

1
/*
2
 *  Linux syscalls
3
 *
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 */
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <stdarg.h>
23
#include <string.h>
24
#include <elf.h>
25
#include <endian.h>
26
#include <errno.h>
27
#include <unistd.h>
28
#include <fcntl.h>
29
#include <time.h>
30
#include <sys/types.h>
31
#include <sys/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
#define ERRNO_TABLE_SIZE 1200
255

    
256
/* target_to_host_errno_table[] is initialized from
257
 * host_to_target_errno_table[] in syscall_init(). */
258
static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
259
};
260

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

    
373
static inline int host_to_target_errno(int err)
374
{
375
    if(host_to_target_errno_table[err])
376
        return host_to_target_errno_table[err];
377
    return err;
378
}
379

    
380
static inline int target_to_host_errno(int err)
381
{
382
    if (target_to_host_errno_table[err])
383
        return target_to_host_errno_table[err];
384
    return err;
385
}
386

    
387
static inline abi_long get_errno(abi_long ret)
388
{
389
    if (ret == -1)
390
        return -host_to_target_errno(errno);
391
    else
392
        return ret;
393
}
394

    
395
static inline int is_error(abi_long ret)
396
{
397
    return (abi_ulong)ret >= (abi_ulong)(-4096);
398
}
399

    
400
char *target_strerror(int err)
401
{
402
    return strerror(target_to_host_errno(err));
403
}
404

    
405
static abi_ulong target_brk;
406
static abi_ulong target_original_brk;
407

    
408
void target_set_brk(abi_ulong new_brk)
409
{
410
    target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
411
}
412

    
413
/* do_brk() must return target values and target errnos. */
414
abi_long do_brk(abi_ulong new_brk)
415
{
416
    abi_ulong brk_page;
417
    abi_long mapped_addr;
418
    int        new_alloc_size;
419

    
420
    if (!new_brk)
421
        return target_brk;
422
    if (new_brk < target_original_brk)
423
        return -TARGET_ENOMEM;
424

    
425
    brk_page = HOST_PAGE_ALIGN(target_brk);
426

    
427
    /* If the new brk is less than this, set it and we're done... */
428
    if (new_brk < brk_page) {
429
        target_brk = new_brk;
430
            return target_brk;
431
    }
432

    
433
    /* We need to allocate more memory after the brk... */
434
    new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
435
    mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
436
                                        PROT_READ|PROT_WRITE,
437
                                        MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
438
    if (is_error(mapped_addr)) {
439
        return mapped_addr;
440
    } else {
441
        target_brk = new_brk;
442
            return target_brk;
443
    }
444
}
445

    
446
static inline fd_set *target_to_host_fds(fd_set *fds,
447
                                         abi_long *target_fds, int n)
448
{
449
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
450
    return (fd_set *)target_fds;
451
#else
452
    int i, b;
453
    if (target_fds) {
454
        FD_ZERO(fds);
455
        for(i = 0;i < n; i++) {
456
            b = (tswapl(target_fds[i / TARGET_ABI_BITS]) >>
457
                 (i & (TARGET_ABI_BITS - 1))) & 1;
458
            if (b)
459
                FD_SET(i, fds);
460
        }
461
        return fds;
462
    } else {
463
        return NULL;
464
    }
465
#endif
466
}
467

    
468
static inline void host_to_target_fds(abi_long *target_fds,
469
                                      fd_set *fds, int n)
470
{
471
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
472
    /* nothing to do */
473
#else
474
    int i, nw, j, k;
475
    abi_long v;
476

    
477
    if (target_fds) {
478
        nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
479
        k = 0;
480
        for(i = 0;i < nw; i++) {
481
            v = 0;
482
            for(j = 0; j < TARGET_ABI_BITS; j++) {
483
                v |= ((FD_ISSET(k, fds) != 0) << j);
484
                k++;
485
            }
486
            target_fds[i] = tswapl(v);
487
        }
488
    }
489
#endif
490
}
491

    
492
#if defined(__alpha__)
493
#define HOST_HZ 1024
494
#else
495
#define HOST_HZ 100
496
#endif
497

    
498
static inline abi_long host_to_target_clock_t(long ticks)
499
{
500
#if HOST_HZ == TARGET_HZ
501
    return ticks;
502
#else
503
    return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
504
#endif
505
}
506

    
507
static inline abi_long host_to_target_rusage(abi_ulong target_addr,
508
                                             const struct rusage *rusage)
509
{
510
    struct target_rusage *target_rusage;
511

    
512
    if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
513
        return -TARGET_EFAULT;
514
    target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
515
    target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
516
    target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
517
    target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
518
    target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
519
    target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
520
    target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
521
    target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
522
    target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
523
    target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
524
    target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
525
    target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
526
    target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
527
    target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
528
    target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
529
    target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
530
    target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
531
    target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
532
    unlock_user_struct(target_rusage, target_addr, 1);
533

    
534
    return 0;
535
}
536

    
537
static inline abi_long target_to_host_timeval(struct timeval *tv,
538
                                              abi_ulong target_addr)
539
{
540
    struct target_timeval *target_tv;
541

    
542
    if (!lock_user_struct(VERIFY_READ, target_tv, target_addr, 1))
543
        return -TARGET_EFAULT;
544
    tv->tv_sec = tswapl(target_tv->tv_sec);
545
    tv->tv_usec = tswapl(target_tv->tv_usec);
546
    unlock_user_struct(target_tv, target_addr, 0);
547

    
548
    return 0;
549
}
550

    
551
static inline abi_long host_to_target_timeval(abi_ulong target_addr,
552
                                              const struct timeval *tv)
553
{
554
    struct target_timeval *target_tv;
555

    
556
    if (!lock_user_struct(VERIFY_WRITE, target_tv, target_addr, 0))
557
        return -TARGET_EFAULT;
558
    target_tv->tv_sec = tswapl(tv->tv_sec);
559
    target_tv->tv_usec = tswapl(tv->tv_usec);
560
    unlock_user_struct(target_tv, target_addr, 1);
561

    
562
    return 0;
563
}
564

    
565

    
566
/* do_select() must return target values and target errnos. */
567
static abi_long do_select(int n,
568
                          abi_ulong rfd_p, abi_ulong wfd_p,
569
                          abi_ulong efd_p, abi_ulong target_tv)
570
{
571
    fd_set rfds, wfds, efds;
572
    fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
573
    abi_long *target_rfds, *target_wfds, *target_efds;
574
    struct timeval tv, *tv_ptr;
575
    abi_long ret;
576
    int ok;
577

    
578
    if (rfd_p) {
579
        target_rfds = lock_user(VERIFY_WRITE, rfd_p, sizeof(abi_long) * n, 1);
580
        if (!target_rfds) {
581
            ret = -TARGET_EFAULT;
582
            goto end;
583
        }
584
        rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
585
    } else {
586
        target_rfds = NULL;
587
        rfds_ptr = NULL;
588
    }
589
    if (wfd_p) {
590
        target_wfds = lock_user(VERIFY_WRITE, wfd_p, sizeof(abi_long) * n, 1);
591
        if (!target_wfds) {
592
            ret = -TARGET_EFAULT;
593
            goto end;
594
        }
595
        wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
596
    } else {
597
        target_wfds = NULL;
598
        wfds_ptr = NULL;
599
    }
600
    if (efd_p) {
601
        target_efds = lock_user(VERIFY_WRITE, efd_p, sizeof(abi_long) * n, 1);
602
        if (!target_efds) {
603
            ret = -TARGET_EFAULT;
604
            goto end;
605
        }
606
        efds_ptr = target_to_host_fds(&efds, target_efds, n);
607
    } else {
608
        target_efds = NULL;
609
        efds_ptr = NULL;
610
    }
611

    
612
    if (target_tv) {
613
        target_to_host_timeval(&tv, target_tv);
614
        tv_ptr = &tv;
615
    } else {
616
        tv_ptr = NULL;
617
    }
618
    ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
619
    ok = !is_error(ret);
620

    
621
    if (ok) {
622
        host_to_target_fds(target_rfds, rfds_ptr, n);
623
        host_to_target_fds(target_wfds, wfds_ptr, n);
624
        host_to_target_fds(target_efds, efds_ptr, n);
625

    
626
        if (target_tv) {
627
            host_to_target_timeval(target_tv, &tv);
628
        }
629
    }
630

    
631
end:
632
    unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0);
633
    unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0);
634
    unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0);
635

    
636
    return ret;
637
}
638

    
639
static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
640
                                               abi_ulong target_addr,
641
                                               socklen_t len)
642
{
643
    struct target_sockaddr *target_saddr;
644

    
645
    target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
646
    if (!target_saddr)
647
        return -TARGET_EFAULT;
648
    memcpy(addr, target_saddr, len);
649
    addr->sa_family = tswap16(target_saddr->sa_family);
650
    unlock_user(target_saddr, target_addr, 0);
651

    
652
    return 0;
653
}
654

    
655
static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
656
                                               struct sockaddr *addr,
657
                                               socklen_t len)
658
{
659
    struct target_sockaddr *target_saddr;
660

    
661
    target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
662
    if (!target_saddr)
663
        return -TARGET_EFAULT;
664
    memcpy(target_saddr, addr, len);
665
    target_saddr->sa_family = tswap16(addr->sa_family);
666
    unlock_user(target_saddr, target_addr, len);
667

    
668
    return 0;
669
}
670

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

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

    
683
        int len = tswapl(target_cmsg->cmsg_len)
684
                  - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
685

    
686
        space += CMSG_SPACE(len);
687
        if (space > msgh->msg_controllen) {
688
            space -= CMSG_SPACE(len);
689
            gemu_log("Host cmsg overflow\n");
690
            break;
691
        }
692

    
693
        cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
694
        cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
695
        cmsg->cmsg_len = CMSG_LEN(len);
696

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

    
705
            for (i = 0; i < numfds; i++)
706
                fd[i] = tswap32(target_fd[i]);
707
        }
708

    
709
        cmsg = CMSG_NXTHDR(msgh, cmsg);
710
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
711
    }
712

    
713
    msgh->msg_controllen = space;
714
}
715

    
716
/* ??? Should this also swap msgh->name?  */
717
static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
718
                                       struct msghdr *msgh)
719
{
720
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
721
    struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
722
    socklen_t space = 0;
723

    
724
    while (cmsg && target_cmsg) {
725
        void *data = CMSG_DATA(cmsg);
726
        void *target_data = TARGET_CMSG_DATA(target_cmsg);
727

    
728
        int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
729

    
730
        space += TARGET_CMSG_SPACE(len);
731
        if (space > tswapl(target_msgh->msg_controllen)) {
732
            space -= TARGET_CMSG_SPACE(len);
733
            gemu_log("Target cmsg overflow\n");
734
            break;
735
        }
736

    
737
        target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
738
        target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
739
        target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
740

    
741
        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
742
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
743
            memcpy(target_data, data, len);
744
        } else {
745
            int *fd = (int *)data;
746
            int *target_fd = (int *)target_data;
747
            int i, numfds = len / sizeof(int);
748

    
749
            for (i = 0; i < numfds; i++)
750
                target_fd[i] = tswap32(fd[i]);
751
        }
752

    
753
        cmsg = CMSG_NXTHDR(msgh, cmsg);
754
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
755
    }
756

    
757
    msgh->msg_controllen = tswapl(space);
758
}
759

    
760
/* do_setsockopt() Must return target values and target errnos. */
761
static abi_long do_setsockopt(int sockfd, int level, int optname,
762
                              abi_ulong optval, socklen_t optlen)
763
{
764
    abi_long ret;
765
    int val;
766

    
767
    switch(level) {
768
    case SOL_TCP:
769
        /* TCP options all take an 'int' value.  */
770
        if (optlen < sizeof(uint32_t))
771
            return -TARGET_EINVAL;
772

    
773
        val = tget32(optval);
774
        ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
775
        break;
776
    case SOL_IP:
777
        switch(optname) {
778
        case IP_TOS:
779
        case IP_TTL:
780
        case IP_HDRINCL:
781
        case IP_ROUTER_ALERT:
782
        case IP_RECVOPTS:
783
        case IP_RETOPTS:
784
        case IP_PKTINFO:
785
        case IP_MTU_DISCOVER:
786
        case IP_RECVERR:
787
        case IP_RECVTOS:
788
#ifdef IP_FREEBIND
789
        case IP_FREEBIND:
790
#endif
791
        case IP_MULTICAST_TTL:
792
        case IP_MULTICAST_LOOP:
793
            val = 0;
794
            if (optlen >= sizeof(uint32_t)) {
795
                val = tget32(optval);
796
            } else if (optlen >= 1) {
797
                val = tget8(optval);
798
            }
799
            ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
800
            break;
801
        default:
802
            goto unimplemented;
803
        }
804
        break;
805
    case TARGET_SOL_SOCKET:
806
        switch (optname) {
807
            /* Options with 'int' argument.  */
808
        case TARGET_SO_DEBUG:
809
                optname = SO_DEBUG;
810
                break;
811
        case TARGET_SO_REUSEADDR:
812
                optname = SO_REUSEADDR;
813
                break;
814
        case TARGET_SO_TYPE:
815
                optname = SO_TYPE;
816
                break;
817
        case TARGET_SO_ERROR:
818
                optname = SO_ERROR;
819
                break;
820
        case TARGET_SO_DONTROUTE:
821
                optname = SO_DONTROUTE;
822
                break;
823
        case TARGET_SO_BROADCAST:
824
                optname = SO_BROADCAST;
825
                break;
826
        case TARGET_SO_SNDBUF:
827
                optname = SO_SNDBUF;
828
                break;
829
        case TARGET_SO_RCVBUF:
830
                optname = SO_RCVBUF;
831
                break;
832
        case TARGET_SO_KEEPALIVE:
833
                optname = SO_KEEPALIVE;
834
                break;
835
        case TARGET_SO_OOBINLINE:
836
                optname = SO_OOBINLINE;
837
                break;
838
        case TARGET_SO_NO_CHECK:
839
                optname = SO_NO_CHECK;
840
                break;
841
        case TARGET_SO_PRIORITY:
842
                optname = SO_PRIORITY;
843
                break;
844
#ifdef SO_BSDCOMPAT
845
        case TARGET_SO_BSDCOMPAT:
846
                optname = SO_BSDCOMPAT;
847
                break;
848
#endif
849
        case TARGET_SO_PASSCRED:
850
                optname = SO_PASSCRED;
851
                break;
852
        case TARGET_SO_TIMESTAMP:
853
                optname = SO_TIMESTAMP;
854
                break;
855
        case TARGET_SO_RCVLOWAT:
856
                optname = SO_RCVLOWAT;
857
                break;
858
        case TARGET_SO_RCVTIMEO:
859
                optname = SO_RCVTIMEO;
860
                break;
861
        case TARGET_SO_SNDTIMEO:
862
                optname = SO_SNDTIMEO;
863
                break;
864
            break;
865
        default:
866
            goto unimplemented;
867
        }
868
        if (optlen < sizeof(uint32_t))
869
        return -TARGET_EINVAL;
870

    
871
        val = tget32(optval);
872
        ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
873
        break;
874
    default:
875
    unimplemented:
876
        gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
877
        ret = -TARGET_ENOSYS;
878
    }
879
    return ret;
880
}
881

    
882
/* do_getsockopt() Must return target values and target errnos. */
883
static abi_long do_getsockopt(int sockfd, int level, int optname,
884
                              abi_ulong optval, abi_ulong optlen)
885
{
886
    abi_long ret;
887
    int len, lv, val;
888

    
889
    switch(level) {
890
    case TARGET_SOL_SOCKET:
891
            level = SOL_SOCKET;
892
        switch (optname) {
893
        case TARGET_SO_LINGER:
894
        case TARGET_SO_RCVTIMEO:
895
        case TARGET_SO_SNDTIMEO:
896
        case TARGET_SO_PEERCRED:
897
        case TARGET_SO_PEERNAME:
898
            /* These don't just return a single integer */
899
            goto unimplemented;
900
        default:
901
            goto int_case;
902
        }
903
        break;
904
    case SOL_TCP:
905
        /* TCP options all take an 'int' value.  */
906
    int_case:
907
        len = tget32(optlen);
908
        if (len < 0)
909
            return -TARGET_EINVAL;
910
        lv = sizeof(int);
911
        ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
912
        if (ret < 0)
913
            return ret;
914
        val = tswap32(val);
915
        if (len > lv)
916
            len = lv;
917
        if (len == 4)
918
            tput32(optval, val);
919
        else
920
            tput8(optval, val);
921
        tput32(optlen, len);
922
        break;
923
    case SOL_IP:
924
        switch(optname) {
925
        case IP_TOS:
926
        case IP_TTL:
927
        case IP_HDRINCL:
928
        case IP_ROUTER_ALERT:
929
        case IP_RECVOPTS:
930
        case IP_RETOPTS:
931
        case IP_PKTINFO:
932
        case IP_MTU_DISCOVER:
933
        case IP_RECVERR:
934
        case IP_RECVTOS:
935
#ifdef IP_FREEBIND
936
        case IP_FREEBIND:
937
#endif
938
        case IP_MULTICAST_TTL:
939
        case IP_MULTICAST_LOOP:
940
            len = tget32(optlen);
941
            if (len < 0)
942
                return -TARGET_EINVAL;
943
            lv = sizeof(int);
944
            ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
945
            if (ret < 0)
946
                return ret;
947
            if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
948
                len = 1;
949
                tput32(optlen, len);
950
                tput8(optval, val);
951
            } else {
952
                if (len > sizeof(int))
953
                    len = sizeof(int);
954
                tput32(optlen, len);
955
                tput32(optval, val);
956
            }
957
            break;
958
        default:
959
            goto unimplemented;
960
        }
961
        break;
962
    default:
963
    unimplemented:
964
        gemu_log("getsockopt level=%d optname=%d not yet supported\n",
965
                 level, optname);
966
        ret = -TARGET_ENOSYS;
967
        break;
968
    }
969
    return ret;
970
}
971

    
972
/* FIXME
973
 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
974
 * other lock functions have a return code of 0 for failure.
975
 */
976
static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
977
                           int count, int copy)
978
{
979
    struct target_iovec *target_vec;
980
    abi_ulong base;
981
    int i, j;
982

    
983
    target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
984
    if (!target_vec)
985
        return -TARGET_EFAULT;
986
    for(i = 0;i < count; i++) {
987
        base = tswapl(target_vec[i].iov_base);
988
        vec[i].iov_len = tswapl(target_vec[i].iov_len);
989
        vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
990
        if (!vec[i].iov_base) 
991
            goto fail;
992
    }
993
    unlock_user (target_vec, target_addr, 0);
994
    return 0;
995
 fail:
996
    /* failure - unwind locks */
997
    for (j = 0; j < i; j++) {
998
        base = tswapl(target_vec[j].iov_base);
999
        unlock_user(vec[j].iov_base, base, 0);
1000
    }
1001
    unlock_user (target_vec, target_addr, 0);
1002
    return -TARGET_EFAULT;
1003
}
1004

    
1005
static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1006
                             int count, int copy)
1007
{
1008
    struct target_iovec *target_vec;
1009
    abi_ulong base;
1010
    int i;
1011

    
1012
    target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1013
    if (!target_vec)
1014
        return -TARGET_EFAULT;
1015
    for(i = 0;i < count; i++) {
1016
        base = tswapl(target_vec[i].iov_base);
1017
        unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1018
    }
1019
    unlock_user (target_vec, target_addr, 0);
1020

    
1021
    return 0;
1022
}
1023

    
1024
/* do_socket() Must return target values and target errnos. */
1025
static abi_long do_socket(int domain, int type, int protocol)
1026
{
1027
#if defined(TARGET_MIPS)
1028
    switch(type) {
1029
    case TARGET_SOCK_DGRAM:
1030
        type = SOCK_DGRAM;
1031
        break;
1032
    case TARGET_SOCK_STREAM:
1033
        type = SOCK_STREAM;
1034
        break;
1035
    case TARGET_SOCK_RAW:
1036
        type = SOCK_RAW;
1037
        break;
1038
    case TARGET_SOCK_RDM:
1039
        type = SOCK_RDM;
1040
        break;
1041
    case TARGET_SOCK_SEQPACKET:
1042
        type = SOCK_SEQPACKET;
1043
        break;
1044
    case TARGET_SOCK_PACKET:
1045
        type = SOCK_PACKET;
1046
        break;
1047
    }
1048
#endif
1049
    if (domain == PF_NETLINK)
1050
        return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1051
    return get_errno(socket(domain, type, protocol));
1052
}
1053

    
1054
/* do_bind() Must return target values and target errnos. */
1055
static abi_long do_bind(int sockfd, abi_ulong target_addr,
1056
                        socklen_t addrlen)
1057
{
1058
    void *addr = alloca(addrlen);
1059

    
1060
    target_to_host_sockaddr(addr, target_addr, addrlen);
1061
    return get_errno(bind(sockfd, addr, addrlen));
1062
}
1063

    
1064
/* do_connect() Must return target values and target errnos. */
1065
static abi_long do_connect(int sockfd, abi_ulong target_addr,
1066
                           socklen_t addrlen)
1067
{
1068
    void *addr = alloca(addrlen);
1069

    
1070
    target_to_host_sockaddr(addr, target_addr, addrlen);
1071
    return get_errno(connect(sockfd, addr, addrlen));
1072
}
1073

    
1074
/* do_sendrecvmsg() Must return target values and target errnos. */
1075
static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1076
                               int flags, int send)
1077
{
1078
    abi_long ret;
1079
    struct target_msghdr *msgp;
1080
    struct msghdr msg;
1081
    int count;
1082
    struct iovec *vec;
1083
    abi_ulong target_vec;
1084

    
1085
    /* FIXME */
1086
    if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1087
                          msgp,
1088
                          target_msg,
1089
                          send ? 1 : 0))
1090
        return -TARGET_EFAULT;
1091
    if (msgp->msg_name) {
1092
        msg.msg_namelen = tswap32(msgp->msg_namelen);
1093
        msg.msg_name = alloca(msg.msg_namelen);
1094
        target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1095
                                msg.msg_namelen);
1096
    } else {
1097
        msg.msg_name = NULL;
1098
        msg.msg_namelen = 0;
1099
    }
1100
    msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1101
    msg.msg_control = alloca(msg.msg_controllen);
1102
    msg.msg_flags = tswap32(msgp->msg_flags);
1103

    
1104
    count = tswapl(msgp->msg_iovlen);
1105
    vec = alloca(count * sizeof(struct iovec));
1106
    target_vec = tswapl(msgp->msg_iov);
1107
    lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1108
    msg.msg_iovlen = count;
1109
    msg.msg_iov = vec;
1110

    
1111
    if (send) {
1112
        target_to_host_cmsg(&msg, msgp);
1113
        ret = get_errno(sendmsg(fd, &msg, flags));
1114
    } else {
1115
        ret = get_errno(recvmsg(fd, &msg, flags));
1116
        if (!is_error(ret))
1117
            host_to_target_cmsg(msgp, &msg);
1118
    }
1119
    unlock_iovec(vec, target_vec, count, !send);
1120
    unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1121
    return ret;
1122
}
1123

    
1124
/* do_accept() Must return target values and target errnos. */
1125
static abi_long do_accept(int fd, abi_ulong target_addr,
1126
                          abi_ulong target_addrlen)
1127
{
1128
    socklen_t addrlen = tget32(target_addrlen);
1129
    void *addr = alloca(addrlen);
1130
    abi_long ret;
1131

    
1132
    ret = get_errno(accept(fd, addr, &addrlen));
1133
    if (!is_error(ret)) {
1134
        host_to_target_sockaddr(target_addr, addr, addrlen);
1135
        tput32(target_addrlen, addrlen);
1136
    }
1137
    return ret;
1138
}
1139

    
1140
/* do_getpeername() Must return target values and target errnos. */
1141
static abi_long do_getpeername(int fd, abi_ulong target_addr,
1142
                               abi_ulong target_addrlen)
1143
{
1144
    socklen_t addrlen = tget32(target_addrlen);
1145
    void *addr = alloca(addrlen);
1146
    abi_long ret;
1147

    
1148
    ret = get_errno(getpeername(fd, addr, &addrlen));
1149
    if (!is_error(ret)) {
1150
        host_to_target_sockaddr(target_addr, addr, addrlen);
1151
        tput32(target_addrlen, addrlen);
1152
    }
1153
    return ret;
1154
}
1155

    
1156
/* do_getsockname() Must return target values and target errnos. */
1157
static abi_long do_getsockname(int fd, abi_ulong target_addr,
1158
                               abi_ulong target_addrlen)
1159
{
1160
    socklen_t addrlen = tget32(target_addrlen);
1161
    void *addr = alloca(addrlen);
1162
    abi_long ret;
1163

    
1164
    ret = get_errno(getsockname(fd, addr, &addrlen));
1165
    if (!is_error(ret)) {
1166
        host_to_target_sockaddr(target_addr, addr, addrlen);
1167
        tput32(target_addrlen, addrlen);
1168
    }
1169
    return ret;
1170
}
1171

    
1172
/* do_socketpair() Must return target values and target errnos. */
1173
static abi_long do_socketpair(int domain, int type, int protocol,
1174
                              abi_ulong target_tab)
1175
{
1176
    int tab[2];
1177
    abi_long ret;
1178

    
1179
    ret = get_errno(socketpair(domain, type, protocol, tab));
1180
    if (!is_error(ret)) {
1181
        tput32(target_tab, tab[0]);
1182
        tput32(target_tab + 4, tab[1]);
1183
    }
1184
    return ret;
1185
}
1186

    
1187
/* do_sendto() Must return target values and target errnos. */
1188
static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1189
                          abi_ulong target_addr, socklen_t addrlen)
1190
{
1191
    void *addr;
1192
    void *host_msg;
1193
    abi_long ret;
1194

    
1195
    host_msg = lock_user(VERIFY_READ, msg, len, 1);
1196
    if (!host_msg)
1197
        return -TARGET_EFAULT;
1198
    if (target_addr) {
1199
        addr = alloca(addrlen);
1200
        target_to_host_sockaddr(addr, target_addr, addrlen);
1201
        ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1202
    } else {
1203
        ret = get_errno(send(fd, host_msg, len, flags));
1204
    }
1205
    unlock_user(host_msg, msg, 0);
1206
    return ret;
1207
}
1208

    
1209
/* do_recvfrom() Must return target values and target errnos. */
1210
static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1211
                            abi_ulong target_addr,
1212
                            abi_ulong target_addrlen)
1213
{
1214
    socklen_t addrlen;
1215
    void *addr;
1216
    void *host_msg;
1217
    abi_long ret;
1218

    
1219
    host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1220
    if (!host_msg)
1221
        return -TARGET_EFAULT;
1222
    if (target_addr) {
1223
        addrlen = tget32(target_addrlen);
1224
        addr = alloca(addrlen);
1225
        ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1226
    } else {
1227
        addr = NULL; /* To keep compiler quiet.  */
1228
        ret = get_errno(recv(fd, host_msg, len, flags));
1229
    }
1230
    if (!is_error(ret)) {
1231
        if (target_addr) {
1232
            host_to_target_sockaddr(target_addr, addr, addrlen);
1233
            tput32(target_addrlen, addrlen);
1234
        }
1235
        unlock_user(host_msg, msg, len);
1236
    } else {
1237
        unlock_user(host_msg, msg, 0);
1238
    }
1239
    return ret;
1240
}
1241

    
1242
#ifdef TARGET_NR_socketcall
1243
/* do_socketcall() Must return target values and target errnos. */
1244
static abi_long do_socketcall(int num, abi_ulong vptr)
1245
{
1246
    abi_long ret;
1247
    const int n = sizeof(abi_ulong);
1248

    
1249
    switch(num) {
1250
    case SOCKOP_socket:
1251
        {
1252
            int domain = tgetl(vptr);
1253
            int type = tgetl(vptr + n);
1254
            int protocol = tgetl(vptr + 2 * n);
1255
            ret = do_socket(domain, type, protocol);
1256
        }
1257
        break;
1258
    case SOCKOP_bind:
1259
        {
1260
            int sockfd = tgetl(vptr);
1261
            abi_ulong target_addr = tgetl(vptr + n);
1262
            socklen_t addrlen = tgetl(vptr + 2 * n);
1263
            ret = do_bind(sockfd, target_addr, addrlen);
1264
        }
1265
        break;
1266
    case SOCKOP_connect:
1267
        {
1268
            int sockfd = tgetl(vptr);
1269
            abi_ulong target_addr = tgetl(vptr + n);
1270
            socklen_t addrlen = tgetl(vptr + 2 * n);
1271
            ret = do_connect(sockfd, target_addr, addrlen);
1272
        }
1273
        break;
1274
    case SOCKOP_listen:
1275
        {
1276
            int sockfd = tgetl(vptr);
1277
            int backlog = tgetl(vptr + n);
1278
            ret = get_errno(listen(sockfd, backlog));
1279
        }
1280
        break;
1281
    case SOCKOP_accept:
1282
        {
1283
            int sockfd = tgetl(vptr);
1284
            abi_ulong target_addr = tgetl(vptr + n);
1285
            abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1286
            ret = do_accept(sockfd, target_addr, target_addrlen);
1287
        }
1288
        break;
1289
    case SOCKOP_getsockname:
1290
        {
1291
            int sockfd = tgetl(vptr);
1292
            abi_ulong target_addr = tgetl(vptr + n);
1293
            abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1294
            ret = do_getsockname(sockfd, target_addr, target_addrlen);
1295
        }
1296
        break;
1297
    case SOCKOP_getpeername:
1298
        {
1299
            int sockfd = tgetl(vptr);
1300
            abi_ulong target_addr = tgetl(vptr + n);
1301
            abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1302
            ret = do_getpeername(sockfd, target_addr, target_addrlen);
1303
        }
1304
        break;
1305
    case SOCKOP_socketpair:
1306
        {
1307
            int domain = tgetl(vptr);
1308
            int type = tgetl(vptr + n);
1309
            int protocol = tgetl(vptr + 2 * n);
1310
            abi_ulong tab = tgetl(vptr + 3 * n);
1311
            ret = do_socketpair(domain, type, protocol, tab);
1312
        }
1313
        break;
1314
    case SOCKOP_send:
1315
        {
1316
            int sockfd = tgetl(vptr);
1317
            abi_ulong msg = tgetl(vptr + n);
1318
            size_t len = tgetl(vptr + 2 * n);
1319
            int flags = tgetl(vptr + 3 * n);
1320
            ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1321
        }
1322
        break;
1323
    case SOCKOP_recv:
1324
        {
1325
            int sockfd = tgetl(vptr);
1326
            abi_ulong msg = tgetl(vptr + n);
1327
            size_t len = tgetl(vptr + 2 * n);
1328
            int flags = tgetl(vptr + 3 * n);
1329
            ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1330
        }
1331
        break;
1332
    case SOCKOP_sendto:
1333
        {
1334
            int sockfd = tgetl(vptr);
1335
            abi_ulong msg = tgetl(vptr + n);
1336
            size_t len = tgetl(vptr + 2 * n);
1337
            int flags = tgetl(vptr + 3 * n);
1338
            abi_ulong addr = tgetl(vptr + 4 * n);
1339
            socklen_t addrlen = tgetl(vptr + 5 * n);
1340
            ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1341
        }
1342
        break;
1343
    case SOCKOP_recvfrom:
1344
        {
1345
            int sockfd = tgetl(vptr);
1346
            abi_ulong msg = tgetl(vptr + n);
1347
            size_t len = tgetl(vptr + 2 * n);
1348
            int flags = tgetl(vptr + 3 * n);
1349
            abi_ulong addr = tgetl(vptr + 4 * n);
1350
            abi_ulong addrlen = tgetl(vptr + 5 * n);
1351
            ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1352
        }
1353
        break;
1354
    case SOCKOP_shutdown:
1355
        {
1356
            int sockfd = tgetl(vptr);
1357
            int how = tgetl(vptr + n);
1358

    
1359
            ret = get_errno(shutdown(sockfd, how));
1360
        }
1361
        break;
1362
    case SOCKOP_sendmsg:
1363
    case SOCKOP_recvmsg:
1364
        {
1365
            int fd;
1366
            abi_ulong target_msg;
1367
            int flags;
1368

    
1369
            fd = tgetl(vptr);
1370
            target_msg = tgetl(vptr + n);
1371
            flags = tgetl(vptr + 2 * n);
1372

    
1373
            ret = do_sendrecvmsg(fd, target_msg, flags,
1374
                                 (num == SOCKOP_sendmsg));
1375
        }
1376
        break;
1377
    case SOCKOP_setsockopt:
1378
        {
1379
            int sockfd = tgetl(vptr);
1380
            int level = tgetl(vptr + n);
1381
            int optname = tgetl(vptr + 2 * n);
1382
            abi_ulong optval = tgetl(vptr + 3 * n);
1383
            socklen_t optlen = tgetl(vptr + 4 * n);
1384

    
1385
            ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1386
        }
1387
        break;
1388
    case SOCKOP_getsockopt:
1389
        {
1390
            int sockfd = tgetl(vptr);
1391
            int level = tgetl(vptr + n);
1392
            int optname = tgetl(vptr + 2 * n);
1393
            abi_ulong optval = tgetl(vptr + 3 * n);
1394
            abi_ulong poptlen = tgetl(vptr + 4 * n);
1395

    
1396
            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
1397
        }
1398
        break;
1399
    default:
1400
        gemu_log("Unsupported socketcall: %d\n", num);
1401
        ret = -TARGET_ENOSYS;
1402
        break;
1403
    }
1404
    return ret;
1405
}
1406
#endif
1407

    
1408
#ifdef TARGET_NR_ipc
1409
#define N_SHM_REGIONS        32
1410

    
1411
static struct shm_region {
1412
    uint32_t        start;
1413
    uint32_t        size;
1414
} shm_regions[N_SHM_REGIONS];
1415

    
1416
struct target_ipc_perm
1417
{
1418
    abi_long __key;
1419
    abi_ulong uid;
1420
    abi_ulong gid;
1421
    abi_ulong cuid;
1422
    abi_ulong cgid;
1423
    unsigned short int mode;
1424
    unsigned short int __pad1;
1425
    unsigned short int __seq;
1426
    unsigned short int __pad2;
1427
    abi_ulong __unused1;
1428
    abi_ulong __unused2;
1429
};
1430

    
1431
struct target_semid_ds
1432
{
1433
  struct target_ipc_perm sem_perm;
1434
  abi_ulong sem_otime;
1435
  abi_ulong __unused1;
1436
  abi_ulong sem_ctime;
1437
  abi_ulong __unused2;
1438
  abi_ulong sem_nsems;
1439
  abi_ulong __unused3;
1440
  abi_ulong __unused4;
1441
};
1442

    
1443
static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1444
                                               abi_ulong target_addr)
1445
{
1446
    struct target_ipc_perm *target_ip;
1447
    struct target_semid_ds *target_sd;
1448

    
1449
    if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1450
        return -TARGET_EFAULT;
1451
    target_ip=&(target_sd->sem_perm);
1452
    host_ip->__key = tswapl(target_ip->__key);
1453
    host_ip->uid = tswapl(target_ip->uid);
1454
    host_ip->gid = tswapl(target_ip->gid);
1455
    host_ip->cuid = tswapl(target_ip->cuid);
1456
    host_ip->cgid = tswapl(target_ip->cgid);
1457
    host_ip->mode = tswapl(target_ip->mode);
1458
    unlock_user_struct(target_sd, target_addr, 0);
1459
    return 0;
1460
}
1461

    
1462
static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1463
                                               struct ipc_perm *host_ip)
1464
{
1465
    struct target_ipc_perm *target_ip;
1466
    struct target_semid_ds *target_sd;
1467

    
1468
    if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1469
        return -TARGET_EFAULT;
1470
    target_ip = &(target_sd->sem_perm);
1471
    target_ip->__key = tswapl(host_ip->__key);
1472
    target_ip->uid = tswapl(host_ip->uid);
1473
    target_ip->gid = tswapl(host_ip->gid);
1474
    target_ip->cuid = tswapl(host_ip->cuid);
1475
    target_ip->cgid = tswapl(host_ip->cgid);
1476
    target_ip->mode = tswapl(host_ip->mode);
1477
    unlock_user_struct(target_sd, target_addr, 1);
1478
    return 0;
1479
}
1480

    
1481
static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1482
                                               abi_ulong target_addr)
1483
{
1484
    struct target_semid_ds *target_sd;
1485

    
1486
    if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1487
        return -TARGET_EFAULT;
1488
    target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1489
    host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1490
    host_sd->sem_otime = tswapl(target_sd->sem_otime);
1491
    host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1492
    unlock_user_struct(target_sd, target_addr, 0);
1493
    return 0;
1494
}
1495

    
1496
static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1497
                                               struct semid_ds *host_sd)
1498
{
1499
    struct target_semid_ds *target_sd;
1500

    
1501
    if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1502
        return -TARGET_EFAULT;
1503
    host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1504
    target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1505
    target_sd->sem_otime = tswapl(host_sd->sem_otime);
1506
    target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1507
    unlock_user_struct(target_sd, target_addr, 1);
1508
    return 0;
1509
}
1510

    
1511
union semun {
1512
        int val;
1513
        struct semid_ds *buf;
1514
        unsigned short *array;
1515
};
1516

    
1517
union target_semun {
1518
        int val;
1519
        abi_long buf;
1520
        unsigned short int *array;
1521
};
1522

    
1523
static inline abi_long target_to_host_semun(int cmd,
1524
                                            union semun *host_su,
1525
                                            abi_ulong target_addr,
1526
                                            struct semid_ds *ds)
1527
{
1528
    union target_semun *target_su;
1529

    
1530
    switch( cmd ) {
1531
        case IPC_STAT:
1532
        case IPC_SET:
1533
           if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1534
               return -TARGET_EFAULT;
1535
           target_to_host_semid_ds(ds,target_su->buf);
1536
           host_su->buf = ds;
1537
           unlock_user_struct(target_su, target_addr, 0);
1538
           break;
1539
        case GETVAL:
1540
        case SETVAL:
1541
           if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1542
               return -TARGET_EFAULT;
1543
           host_su->val = tswapl(target_su->val);
1544
           unlock_user_struct(target_su, target_addr, 0);
1545
           break;
1546
        case GETALL:
1547
        case SETALL:
1548
           if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1549
               return -TARGET_EFAULT;
1550
           *host_su->array = tswap16(*target_su->array);
1551
           unlock_user_struct(target_su, target_addr, 0);
1552
           break;
1553
        default:
1554
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1555
    }
1556
    return 0;
1557
}
1558

    
1559
static inline abi_long host_to_target_semun(int cmd,
1560
                                            abi_ulong target_addr,
1561
                                            union semun *host_su,
1562
                                            struct semid_ds *ds)
1563
{
1564
    union target_semun *target_su;
1565

    
1566
    switch( cmd ) {
1567
        case IPC_STAT:
1568
        case IPC_SET:
1569
           if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1570
               return -TARGET_EFAULT;
1571
           host_to_target_semid_ds(target_su->buf,ds);
1572
           unlock_user_struct(target_su, target_addr, 1);
1573
           break;
1574
        case GETVAL:
1575
        case SETVAL:
1576
           if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1577
               return -TARGET_EFAULT;
1578
           target_su->val = tswapl(host_su->val);
1579
           unlock_user_struct(target_su, target_addr, 1);
1580
           break;
1581
        case GETALL:
1582
        case SETALL:
1583
           if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1584
               return -TARGET_EFAULT;
1585
           *target_su->array = tswap16(*host_su->array);
1586
           unlock_user_struct(target_su, target_addr, 1);
1587
           break;
1588
        default:
1589
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1590
    }
1591
    return 0;
1592
}
1593

    
1594
static inline abi_long do_semctl(int first, int second, int third,
1595
                                 abi_long ptr)
1596
{
1597
    union semun arg;
1598
    struct semid_ds dsarg;
1599
    int cmd = third&0xff;
1600
    abi_long ret = 0;
1601

    
1602
    switch( cmd ) {
1603
        case GETVAL:
1604
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1605
            ret = get_errno(semctl(first, second, cmd, arg));
1606
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1607
            break;
1608
        case SETVAL:
1609
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1610
            ret = get_errno(semctl(first, second, cmd, arg));
1611
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1612
            break;
1613
        case GETALL:
1614
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1615
            ret = get_errno(semctl(first, second, cmd, arg));
1616
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1617
            break;
1618
        case SETALL:
1619
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1620
            ret = get_errno(semctl(first, second, cmd, arg));
1621
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1622
            break;
1623
        case IPC_STAT:
1624
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1625
            ret = get_errno(semctl(first, second, cmd, arg));
1626
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1627
            break;
1628
        case IPC_SET:
1629
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1630
            ret = get_errno(semctl(first, second, cmd, arg));
1631
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1632
            break;
1633
    default:
1634
            ret = get_errno(semctl(first, second, cmd, arg));
1635
    }
1636

    
1637
    return ret;
1638
}
1639

    
1640
struct target_msqid_ds
1641
{
1642
  struct target_ipc_perm msg_perm;
1643
  abi_ulong msg_stime;
1644
  abi_ulong __unused1;
1645
  abi_ulong msg_rtime;
1646
  abi_ulong __unused2;
1647
  abi_ulong msg_ctime;
1648
  abi_ulong __unused3;
1649
  abi_ulong __msg_cbytes;
1650
  abi_ulong msg_qnum;
1651
  abi_ulong msg_qbytes;
1652
  abi_ulong msg_lspid;
1653
  abi_ulong msg_lrpid;
1654
  abi_ulong __unused4;
1655
  abi_ulong __unused5;
1656
};
1657

    
1658
static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1659
                                               abi_ulong target_addr)
1660
{
1661
    struct target_msqid_ds *target_md;
1662

    
1663
    if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1664
        return -TARGET_EFAULT;
1665
    target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1666
    host_md->msg_stime = tswapl(target_md->msg_stime);
1667
    host_md->msg_rtime = tswapl(target_md->msg_rtime);
1668
    host_md->msg_ctime = tswapl(target_md->msg_ctime);
1669
    host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1670
    host_md->msg_qnum = tswapl(target_md->msg_qnum);
1671
    host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1672
    host_md->msg_lspid = tswapl(target_md->msg_lspid);
1673
    host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1674
    unlock_user_struct(target_md, target_addr, 0);
1675
    return 0;
1676
}
1677

    
1678
static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1679
                                               struct msqid_ds *host_md)
1680
{
1681
    struct target_msqid_ds *target_md;
1682

    
1683
    if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1684
        return -TARGET_EFAULT;
1685
    host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1686
    target_md->msg_stime = tswapl(host_md->msg_stime);
1687
    target_md->msg_rtime = tswapl(host_md->msg_rtime);
1688
    target_md->msg_ctime = tswapl(host_md->msg_ctime);
1689
    target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1690
    target_md->msg_qnum = tswapl(host_md->msg_qnum);
1691
    target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1692
    target_md->msg_lspid = tswapl(host_md->msg_lspid);
1693
    target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1694
    unlock_user_struct(target_md, target_addr, 1);
1695
    return 0;
1696
}
1697

    
1698
static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1699
{
1700
    struct msqid_ds dsarg;
1701
    int cmd = second&0xff;
1702
    abi_long ret = 0;
1703
    switch( cmd ) {
1704
    case IPC_STAT:
1705
    case IPC_SET:
1706
        target_to_host_msqid_ds(&dsarg,ptr);
1707
        ret = get_errno(msgctl(first, cmd, &dsarg));
1708
        host_to_target_msqid_ds(ptr,&dsarg);
1709
    default:
1710
        ret = get_errno(msgctl(first, cmd, &dsarg));
1711
    }
1712
    return ret;
1713
}
1714

    
1715
struct target_msgbuf {
1716
        abi_ulong mtype;
1717
        char        mtext[1];
1718
};
1719

    
1720
static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1721
                                 unsigned int msgsz, int msgflg)
1722
{
1723
    struct target_msgbuf *target_mb;
1724
    struct msgbuf *host_mb;
1725
    abi_long ret = 0;
1726

    
1727
    if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1728
        return -TARGET_EFAULT;
1729
    host_mb = malloc(msgsz+sizeof(long));
1730
    host_mb->mtype = tswapl(target_mb->mtype);
1731
    memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1732
    ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1733
    free(host_mb);
1734
    unlock_user_struct(target_mb, msgp, 0);
1735

    
1736
    return ret;
1737
}
1738

    
1739
static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1740
                                 unsigned int msgsz, int msgtype,
1741
                                 int msgflg)
1742
{
1743
    struct target_msgbuf *target_mb;
1744
    char *target_mtext;
1745
    struct msgbuf *host_mb;
1746
    abi_long ret = 0;
1747

    
1748
    if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1749
        return -TARGET_EFAULT;
1750
    host_mb = malloc(msgsz+sizeof(long));
1751
    ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1752
    if (ret > 0) {
1753
        abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1754
        target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1755
        if (!target_mtext) {
1756
            ret = -TARGET_EFAULT;
1757
            goto end;
1758
        }
1759
            memcpy(target_mb->mtext, host_mb->mtext, ret);
1760
        unlock_user(target_mtext, target_mtext_addr, ret);
1761
    }
1762
    target_mb->mtype = tswapl(host_mb->mtype);
1763
    free(host_mb);
1764

    
1765
end:
1766
    if (target_mb)
1767
        unlock_user_struct(target_mb, msgp, 1);
1768
    return ret;
1769
}
1770

    
1771
/* ??? This only works with linear mappings.  */
1772
/* do_ipc() must return target values and target errnos. */
1773
static abi_long do_ipc(unsigned int call, int first,
1774
                       int second, int third,
1775
                       abi_long ptr, abi_long fifth)
1776
{
1777
    int version;
1778
    abi_long ret = 0;
1779
    unsigned long raddr;
1780
    struct shmid_ds shm_info;
1781
    int i;
1782

    
1783
    version = call >> 16;
1784
    call &= 0xffff;
1785

    
1786
    switch (call) {
1787
    case IPCOP_semop:
1788
        ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
1789
        break;
1790

    
1791
    case IPCOP_semget:
1792
        ret = get_errno(semget(first, second, third));
1793
        break;
1794

    
1795
    case IPCOP_semctl:
1796
        ret = do_semctl(first, second, third, ptr);
1797
        break;
1798

    
1799
    case IPCOP_semtimedop:
1800
        gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1801
        ret = -TARGET_ENOSYS;
1802
        break;
1803

    
1804
        case IPCOP_msgget:
1805
                ret = get_errno(msgget(first, second));
1806
                break;
1807

    
1808
        case IPCOP_msgsnd:
1809
                ret = do_msgsnd(first, ptr, second, third);
1810
                break;
1811

    
1812
        case IPCOP_msgctl:
1813
                ret = do_msgctl(first, second, ptr);
1814
                break;
1815

    
1816
        case IPCOP_msgrcv:
1817
                {
1818
                      /* XXX: this code is not correct */
1819
                      struct ipc_kludge
1820
                      {
1821
                              void *__unbounded msgp;
1822
                              long int msgtyp;
1823
                      };
1824

    
1825
                      struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
1826
                      struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
1827

    
1828
                      ret = do_msgrcv(first, (long)msgp, second, 0, third);
1829

    
1830
                }
1831
                break;
1832

    
1833
    case IPCOP_shmat:
1834
        /* SHM_* flags are the same on all linux platforms */
1835
        ret = get_errno((long) shmat(first, (void *) ptr, second));
1836
        if (is_error(ret))
1837
            break;
1838
        raddr = ret;
1839
        /* find out the length of the shared memory segment */
1840

    
1841
        ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1842
        if (is_error(ret)) {
1843
            /* can't get length, bail out */
1844
            shmdt((void *) raddr);
1845
            break;
1846
        }
1847
        page_set_flags(raddr, raddr + shm_info.shm_segsz,
1848
                       PAGE_VALID | PAGE_READ |
1849
                       ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1850
        for (i = 0; i < N_SHM_REGIONS; ++i) {
1851
            if (shm_regions[i].start == 0) {
1852
                shm_regions[i].start = raddr;
1853
                shm_regions[i].size = shm_info.shm_segsz;
1854
                break;
1855
            }
1856
        }
1857
        if (put_user(raddr, third, abi_ulong))
1858
            return -TARGET_EFAULT;
1859
        ret = 0;
1860
        break;
1861
    case IPCOP_shmdt:
1862
        for (i = 0; i < N_SHM_REGIONS; ++i) {
1863
            if (shm_regions[i].start == ptr) {
1864
                shm_regions[i].start = 0;
1865
                page_set_flags(ptr, shm_regions[i].size, 0);
1866
                break;
1867
            }
1868
        }
1869
        ret = get_errno(shmdt((void *) ptr));
1870
        break;
1871

    
1872
    case IPCOP_shmget:
1873
        /* IPC_* flag values are the same on all linux platforms */
1874
        ret = get_errno(shmget(first, second, third));
1875
        break;
1876

    
1877
        /* IPC_* and SHM_* command values are the same on all linux platforms */
1878
    case IPCOP_shmctl:
1879
        switch(second) {
1880
        case IPC_RMID:
1881
        case SHM_LOCK:
1882
        case SHM_UNLOCK:
1883
            ret = get_errno(shmctl(first, second, NULL));
1884
            break;
1885
        default:
1886
            goto unimplemented;
1887
        }
1888
        break;
1889
    default:
1890
    unimplemented:
1891
        gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1892
        ret = -TARGET_ENOSYS;
1893
        break;
1894
    }
1895
    return ret;
1896
}
1897
#endif
1898

    
1899
/* kernel structure types definitions */
1900
#define IFNAMSIZ        16
1901

    
1902
#define STRUCT(name, list...) STRUCT_ ## name,
1903
#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1904
enum {
1905
#include "syscall_types.h"
1906
};
1907
#undef STRUCT
1908
#undef STRUCT_SPECIAL
1909

    
1910
#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1911
#define STRUCT_SPECIAL(name)
1912
#include "syscall_types.h"
1913
#undef STRUCT
1914
#undef STRUCT_SPECIAL
1915

    
1916
typedef struct IOCTLEntry {
1917
    unsigned int target_cmd;
1918
    unsigned int host_cmd;
1919
    const char *name;
1920
    int access;
1921
    const argtype arg_type[5];
1922
} IOCTLEntry;
1923

    
1924
#define IOC_R 0x0001
1925
#define IOC_W 0x0002
1926
#define IOC_RW (IOC_R | IOC_W)
1927

    
1928
#define MAX_STRUCT_SIZE 4096
1929

    
1930
IOCTLEntry ioctl_entries[] = {
1931
#define IOCTL(cmd, access, types...) \
1932
    { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1933
#include "ioctls.h"
1934
    { 0, 0, },
1935
};
1936

    
1937
/* ??? Implement proper locking for ioctls.  */
1938
/* do_ioctl() Must return target values and target errnos. */
1939
static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
1940
{
1941
    const IOCTLEntry *ie;
1942
    const argtype *arg_type;
1943
    abi_long ret;
1944
    uint8_t buf_temp[MAX_STRUCT_SIZE];
1945
    int target_size;
1946
    void *argptr;
1947

    
1948
    ie = ioctl_entries;
1949
    for(;;) {
1950
        if (ie->target_cmd == 0) {
1951
            gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
1952
            return -TARGET_ENOSYS;
1953
        }
1954
        if (ie->target_cmd == cmd)
1955
            break;
1956
        ie++;
1957
    }
1958
    arg_type = ie->arg_type;
1959
#if defined(DEBUG)
1960
    gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
1961
#endif
1962
    switch(arg_type[0]) {
1963
    case TYPE_NULL:
1964
        /* no argument */
1965
        ret = get_errno(ioctl(fd, ie->host_cmd));
1966
        break;
1967
    case TYPE_PTRVOID:
1968
    case TYPE_INT:
1969
        /* int argment */
1970
        ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1971
        break;
1972
    case TYPE_PTR:
1973
        arg_type++;
1974
        target_size = thunk_type_size(arg_type, 0);
1975
        switch(ie->access) {
1976
        case IOC_R:
1977
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1978
            if (!is_error(ret)) {
1979
                argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
1980
                if (!argptr)
1981
                    return -TARGET_EFAULT;
1982
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1983
                unlock_user(argptr, arg, target_size);
1984
            }
1985
            break;
1986
        case IOC_W:
1987
            argptr = lock_user(VERIFY_READ, arg, target_size, 1);
1988
            if (!argptr)
1989
                return -TARGET_EFAULT;
1990
            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1991
            unlock_user(argptr, arg, 0);
1992
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1993
            break;
1994
        default:
1995
        case IOC_RW:
1996
            argptr = lock_user(VERIFY_READ, arg, target_size, 1);
1997
            if (!argptr)
1998
                return -TARGET_EFAULT;
1999
            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2000
            unlock_user(argptr, arg, 0);
2001
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2002
            if (!is_error(ret)) {
2003
                argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2004
                if (!argptr)
2005
                    return -TARGET_EFAULT;
2006
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2007
                unlock_user(argptr, arg, target_size);
2008
            }
2009
            break;
2010
        }
2011
        break;
2012
    default:
2013
        gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2014
                 (long)cmd, arg_type[0]);
2015
        ret = -TARGET_ENOSYS;
2016
        break;
2017
    }
2018
    return ret;
2019
}
2020

    
2021
bitmask_transtbl iflag_tbl[] = {
2022
        { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2023
        { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2024
        { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2025
        { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2026
        { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2027
        { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2028
        { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2029
        { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2030
        { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2031
        { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2032
        { TARGET_IXON, TARGET_IXON, IXON, IXON },
2033
        { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2034
        { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2035
        { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2036
        { 0, 0, 0, 0 }
2037
};
2038

    
2039
bitmask_transtbl oflag_tbl[] = {
2040
        { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2041
        { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2042
        { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2043
        { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2044
        { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2045
        { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2046
        { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2047
        { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2048
        { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2049
        { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2050
        { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2051
        { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2052
        { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2053
        { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2054
        { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2055
        { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2056
        { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2057
        { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2058
        { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2059
        { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2060
        { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2061
        { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2062
        { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2063
        { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2064
        { 0, 0, 0, 0 }
2065
};
2066

    
2067
bitmask_transtbl cflag_tbl[] = {
2068
        { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2069
        { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2070
        { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2071
        { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2072
        { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2073
        { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2074
        { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2075
        { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2076
        { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2077
        { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2078
        { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2079
        { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2080
        { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2081
        { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2082
        { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2083
        { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2084
        { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2085
        { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2086
        { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2087
        { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2088
        { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2089
        { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2090
        { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2091
        { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2092
        { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2093
        { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2094
        { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2095
        { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2096
        { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2097
        { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2098
        { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2099
        { 0, 0, 0, 0 }
2100
};
2101

    
2102
bitmask_transtbl lflag_tbl[] = {
2103
        { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2104
        { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2105
        { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2106
        { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2107
        { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2108
        { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2109
        { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2110
        { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2111
        { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2112
        { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2113
        { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2114
        { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2115
        { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2116
        { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2117
        { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2118
        { 0, 0, 0, 0 }
2119
};
2120

    
2121
static void target_to_host_termios (void *dst, const void *src)
2122
{
2123
    struct host_termios *host = dst;
2124
    const struct target_termios *target = src;
2125

    
2126
    host->c_iflag =
2127
        target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2128
    host->c_oflag =
2129
        target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2130
    host->c_cflag =
2131
        target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2132
    host->c_lflag =
2133
        target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2134
    host->c_line = target->c_line;
2135

    
2136
    host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2137
    host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2138
    host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2139
    host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2140
    host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2141
    host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2142
    host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2143
    host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2144
    host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2145
    host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2146
    host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2147
    host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2148
    host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2149
    host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2150
    host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2151
    host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2152
    host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2153
}
2154

    
2155
static void host_to_target_termios (void *dst, const void *src)
2156
{
2157
    struct target_termios *target = dst;
2158
    const struct host_termios *host = src;
2159

    
2160
    target->c_iflag =
2161
        tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2162
    target->c_oflag =
2163
        tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2164
    target->c_cflag =
2165
        tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2166
    target->c_lflag =
2167
        tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2168
    target->c_line = host->c_line;
2169

    
2170
    target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2171
    target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2172
    target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2173
    target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2174
    target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2175
    target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2176
    target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2177
    target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2178
    target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2179
    target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2180
    target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2181
    target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2182
    target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2183
    target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2184
    target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2185
    target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2186
    target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2187
}
2188

    
2189
StructEntry struct_termios_def = {
2190
    .convert = { host_to_target_termios, target_to_host_termios },
2191
    .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2192
    .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2193
};
2194

    
2195
static bitmask_transtbl mmap_flags_tbl[] = {
2196
        { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2197
        { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2198
        { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2199
        { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2200
        { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2201
        { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2202
        { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2203
        { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2204
        { 0, 0, 0, 0 }
2205
};
2206

    
2207
static bitmask_transtbl fcntl_flags_tbl[] = {
2208
        { TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
2209
        { TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
2210
        { TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
2211
        { TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
2212
        { TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
2213
        { TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
2214
        { TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
2215
        { TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
2216
        { TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
2217
        { TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
2218
        { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2219
        { TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
2220
        { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2221
#if defined(O_DIRECT)
2222
        { TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
2223
#endif
2224
        { 0, 0, 0, 0 }
2225
};
2226

    
2227
#if defined(TARGET_I386)
2228

    
2229
/* NOTE: there is really one LDT for all the threads */
2230
uint8_t *ldt_table;
2231

    
2232
static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
2233
{
2234
    int size;
2235
    void *p;
2236

    
2237
    if (!ldt_table)
2238
        return 0;
2239
    size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2240
    if (size > bytecount)
2241
        size = bytecount;
2242
    p = lock_user(VERIFY_WRITE, ptr, size, 0);
2243
    if (!p)
2244
        return -TARGET_EFAULT;
2245
    /* ??? Should this by byteswapped?  */
2246
    memcpy(p, ldt_table, size);
2247
    unlock_user(p, ptr, size);
2248
    return size;
2249
}
2250

    
2251
/* XXX: add locking support */
2252
static abi_long write_ldt(CPUX86State *env,
2253
                          abi_ulong ptr, unsigned long bytecount, int oldmode)
2254
{
2255
    struct target_modify_ldt_ldt_s ldt_info;
2256
    struct target_modify_ldt_ldt_s *target_ldt_info;
2257
    int seg_32bit, contents, read_exec_only, limit_in_pages;
2258
    int seg_not_present, useable;
2259
    uint32_t *lp, entry_1, entry_2;
2260

    
2261
    if (bytecount != sizeof(ldt_info))
2262
        return -TARGET_EINVAL;
2263
    if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
2264
        return -TARGET_EFAULT;
2265
    ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2266
    ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2267
    ldt_info.limit = tswap32(target_ldt_info->limit);
2268
    ldt_info.flags = tswap32(target_ldt_info->flags);
2269
    unlock_user_struct(target_ldt_info, ptr, 0);
2270

    
2271
    if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2272
        return -TARGET_EINVAL;
2273
    seg_32bit = ldt_info.flags & 1;
2274
    contents = (ldt_info.flags >> 1) & 3;
2275
    read_exec_only = (ldt_info.flags >> 3) & 1;
2276
    limit_in_pages = (ldt_info.flags >> 4) & 1;
2277
    seg_not_present = (ldt_info.flags >> 5) & 1;
2278
    useable = (ldt_info.flags >> 6) & 1;
2279

    
2280
    if (contents == 3) {
2281
        if (oldmode)
2282
            return -TARGET_EINVAL;
2283
        if (seg_not_present == 0)
2284
            return -TARGET_EINVAL;
2285
    }
2286
    /* allocate the LDT */
2287
    if (!ldt_table) {
2288
        ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2289
        if (!ldt_table)
2290
            return -TARGET_ENOMEM;
2291
        memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2292
        env->ldt.base = h2g(ldt_table);
2293
        env->ldt.limit = 0xffff;
2294
    }
2295

    
2296
    /* NOTE: same code as Linux kernel */
2297
    /* Allow LDTs to be cleared by the user. */
2298
    if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2299
        if (oldmode ||
2300
            (contents == 0                &&
2301
             read_exec_only == 1        &&
2302
             seg_32bit == 0                &&
2303
             limit_in_pages == 0        &&
2304
             seg_not_present == 1        &&
2305
             useable == 0 )) {
2306
            entry_1 = 0;
2307
            entry_2 = 0;
2308
            goto install;
2309
        }
2310
    }
2311

    
2312
    entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2313
        (ldt_info.limit & 0x0ffff);
2314
    entry_2 = (ldt_info.base_addr & 0xff000000) |
2315
        ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2316
        (ldt_info.limit & 0xf0000) |
2317
        ((read_exec_only ^ 1) << 9) |
2318
        (contents << 10) |
2319
        ((seg_not_present ^ 1) << 15) |
2320
        (seg_32bit << 22) |
2321
        (limit_in_pages << 23) |
2322
        0x7000;
2323
    if (!oldmode)
2324
        entry_2 |= (useable << 20);
2325

    
2326
    /* Install the new entry ...  */
2327
install:
2328
    lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2329
    lp[0] = tswap32(entry_1);
2330
    lp[1] = tswap32(entry_2);
2331
    return 0;
2332
}
2333

    
2334
/* specific and weird i386 syscalls */
2335
abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, 
2336
                       unsigned long bytecount)
2337
{
2338
    abi_long ret;
2339

    
2340
    switch (func) {
2341
    case 0:
2342
        ret = read_ldt(ptr, bytecount);
2343
        break;
2344
    case 1:
2345
        ret = write_ldt(env, ptr, bytecount, 1);
2346
        break;
2347
    case 0x11:
2348
        ret = write_ldt(env, ptr, bytecount, 0);
2349
        break;
2350
    default:
2351
        ret = -TARGET_ENOSYS;
2352
        break;
2353
    }
2354
    return ret;
2355
}
2356

    
2357
#endif /* defined(TARGET_I386) */
2358

    
2359
/* this stack is the equivalent of the kernel stack associated with a
2360
   thread/process */
2361
#define NEW_STACK_SIZE 8192
2362

    
2363
static int clone_func(void *arg)
2364
{
2365
    CPUState *env = arg;
2366
    cpu_loop(env);
2367
    /* never exits */
2368
    return 0;
2369
}
2370

    
2371
/* do_fork() Must return host values and target errnos (unlike most
2372
   do_*() functions). */
2373
int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
2374
{
2375
    int ret;
2376
    TaskState *ts;
2377
    uint8_t *new_stack;
2378
    CPUState *new_env;
2379

    
2380
    if (flags & CLONE_VM) {
2381
        ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2382
        memset(ts, 0, sizeof(TaskState));
2383
        new_stack = ts->stack;
2384
        ts->used = 1;
2385
        /* add in task state list */
2386
        ts->next = first_task_state;
2387
        first_task_state = ts;
2388
        /* we create a new CPU instance. */
2389
        new_env = cpu_copy(env);
2390
#if defined(TARGET_I386)
2391
        if (!newsp)
2392
            newsp = env->regs[R_ESP];
2393
        new_env->regs[R_ESP] = newsp;
2394
        new_env->regs[R_EAX] = 0;
2395
#elif defined(TARGET_ARM)
2396
        if (!newsp)
2397
            newsp = env->regs[13];
2398
        new_env->regs[13] = newsp;
2399
        new_env->regs[0] = 0;
2400
#elif defined(TARGET_SPARC)
2401
        if (!newsp)
2402
            newsp = env->regwptr[22];
2403
        new_env->regwptr[22] = newsp;
2404
        new_env->regwptr[0] = 0;
2405
        /* XXXXX */
2406
        printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
2407
#elif defined(TARGET_M68K)
2408
        if (!newsp)
2409
            newsp = env->aregs[7];
2410
        new_env->aregs[7] = newsp;
2411
        new_env->dregs[0] = 0;
2412
        /* ??? is this sufficient?  */
2413
#elif defined(TARGET_MIPS)
2414
        if (!newsp)
2415
            newsp = env->gpr[29][env->current_tc];
2416
        new_env->gpr[29][env->current_tc] = newsp;
2417
#elif defined(TARGET_PPC)
2418
        if (!newsp)
2419
            newsp = env->gpr[1];
2420
        new_env->gpr[1] = newsp;
2421
        {
2422
            int i;
2423
            for (i = 7; i < 32; i++)
2424
                new_env->gpr[i] = 0;
2425
        }
2426
#elif defined(TARGET_SH4)
2427
        if (!newsp)
2428
          newsp = env->gregs[15];
2429
        new_env->gregs[15] = newsp;
2430
        /* XXXXX */
2431
#elif defined(TARGET_ALPHA)
2432
       if (!newsp)
2433
         newsp = env->ir[30];
2434
       new_env->ir[30] = newsp;
2435
        /* ? */
2436
        {
2437
            int i;
2438
            for (i = 7; i < 30; i++)
2439
                new_env->ir[i] = 0;
2440
        }
2441
#elif defined(TARGET_CRIS)
2442
        if (!newsp)
2443
          newsp = env->regs[14];
2444
        new_env->regs[14] = newsp;
2445
#else
2446
#error unsupported target CPU
2447
#endif
2448
        new_env->opaque = ts;
2449
#ifdef __ia64__
2450
        ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2451
#else
2452
        ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2453
#endif
2454
    } else {
2455
        /* if no CLONE_VM, we consider it is a fork */
2456
        if ((flags & ~CSIGNAL) != 0)
2457
            return -EINVAL;
2458
        ret = fork();
2459
    }
2460
    return ret;
2461
}
2462

    
2463
static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2464
{
2465
    struct flock fl;
2466
    struct target_flock *target_fl;
2467
    struct flock64 fl64;
2468
    struct target_flock64 *target_fl64;
2469
    abi_long ret;
2470

    
2471
    switch(cmd) {
2472
    case TARGET_F_GETLK:
2473
        if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2474
            return -TARGET_EFAULT;
2475
        fl.l_type = tswap16(target_fl->l_type);
2476
        fl.l_whence = tswap16(target_fl->l_whence);
2477
        fl.l_start = tswapl(target_fl->l_start);
2478
        fl.l_len = tswapl(target_fl->l_len);
2479
        fl.l_pid = tswapl(target_fl->l_pid);
2480
        unlock_user_struct(target_fl, arg, 0);
2481
        ret = fcntl(fd, cmd, &fl);
2482
        if (ret == 0) {
2483
            if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2484
                return -TARGET_EFAULT;
2485
            target_fl->l_type = tswap16(fl.l_type);
2486
            target_fl->l_whence = tswap16(fl.l_whence);
2487
            target_fl->l_start = tswapl(fl.l_start);
2488
            target_fl->l_len = tswapl(fl.l_len);
2489
            target_fl->l_pid = tswapl(fl.l_pid);
2490
            unlock_user_struct(target_fl, arg, 1);
2491
        }
2492
        break;
2493

    
2494
    case TARGET_F_SETLK:
2495
    case TARGET_F_SETLKW:
2496
        if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2497
            return -TARGET_EFAULT;
2498
        fl.l_type = tswap16(target_fl->l_type);
2499
        fl.l_whence = tswap16(target_fl->l_whence);
2500
        fl.l_start = tswapl(target_fl->l_start);
2501
        fl.l_len = tswapl(target_fl->l_len);
2502
        fl.l_pid = tswapl(target_fl->l_pid);
2503
        unlock_user_struct(target_fl, arg, 0);
2504
        ret = fcntl(fd, cmd, &fl);
2505
        break;
2506

    
2507
    case TARGET_F_GETLK64:
2508
        if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2509
            return -TARGET_EFAULT;
2510
        fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2511
        fl64.l_whence = tswap16(target_fl64->l_whence);
2512
        fl64.l_start = tswapl(target_fl64->l_start);
2513
        fl64.l_len = tswapl(target_fl64->l_len);
2514
        fl64.l_pid = tswap16(target_fl64->l_pid);
2515
        unlock_user_struct(target_fl64, arg, 0);
2516
        ret = fcntl(fd, cmd >> 1, &fl64);
2517
        if (ret == 0) {
2518
            if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2519
                return -TARGET_EFAULT;
2520
            target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2521
            target_fl64->l_whence = tswap16(fl64.l_whence);
2522
            target_fl64->l_start = tswapl(fl64.l_start);
2523
            target_fl64->l_len = tswapl(fl64.l_len);
2524
            target_fl64->l_pid = tswapl(fl64.l_pid);
2525
            unlock_user_struct(target_fl64, arg, 1);
2526
        }
2527
                break;
2528
    case TARGET_F_SETLK64:
2529
    case TARGET_F_SETLKW64:
2530
        if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2531
            return -TARGET_EFAULT;
2532
        fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2533
        fl64.l_whence = tswap16(target_fl64->l_whence);
2534
        fl64.l_start = tswapl(target_fl64->l_start);
2535
        fl64.l_len = tswapl(target_fl64->l_len);
2536
        fl64.l_pid = tswap16(target_fl64->l_pid);
2537
        unlock_user_struct(target_fl64, arg, 0);
2538
        ret = fcntl(fd, cmd >> 1, &fl64);
2539
        break;
2540

    
2541
    case F_GETFL:
2542
        ret = fcntl(fd, cmd, arg);
2543
        ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2544
        break;
2545

    
2546
    case F_SETFL:
2547
        ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2548
        break;
2549

    
2550
    default:
2551
        ret = fcntl(fd, cmd, arg);
2552
        break;
2553
    }
2554
    return ret;
2555
}
2556

    
2557
#ifdef USE_UID16
2558

    
2559
static inline int high2lowuid(int uid)
2560
{
2561
    if (uid > 65535)
2562
        return 65534;
2563
    else
2564
        return uid;
2565
}
2566

    
2567
static inline int high2lowgid(int gid)
2568
{
2569
    if (gid > 65535)
2570
        return 65534;
2571
    else
2572
        return gid;
2573
}
2574

    
2575
static inline int low2highuid(int uid)
2576
{
2577
    if ((int16_t)uid == -1)
2578
        return -1;
2579
    else
2580
        return uid;
2581
}
2582

    
2583
static inline int low2highgid(int gid)
2584
{
2585
    if ((int16_t)gid == -1)
2586
        return -1;
2587
    else
2588
        return gid;
2589
}
2590

    
2591
#endif /* USE_UID16 */
2592

    
2593
void syscall_init(void)
2594
{
2595
    IOCTLEntry *ie;
2596
    const argtype *arg_type;
2597
    int size;
2598
    int i;
2599

    
2600
#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2601
#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
2602
#include "syscall_types.h"
2603
#undef STRUCT
2604
#undef STRUCT_SPECIAL
2605

    
2606
    /* we patch the ioctl size if necessary. We rely on the fact that
2607
       no ioctl has all the bits at '1' in the size field */
2608
    ie = ioctl_entries;
2609
    while (ie->target_cmd != 0) {
2610
        if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2611
            TARGET_IOC_SIZEMASK) {
2612
            arg_type = ie->arg_type;
2613
            if (arg_type[0] != TYPE_PTR) {
2614
                fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2615
                        ie->target_cmd);
2616
                exit(1);
2617
            }
2618
            arg_type++;
2619
            size = thunk_type_size(arg_type, 0);
2620
            ie->target_cmd = (ie->target_cmd &
2621
                              ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2622
                (size << TARGET_IOC_SIZESHIFT);
2623
        }
2624

    
2625
        /* Build target_to_host_errno_table[] table from
2626
         * host_to_target_errno_table[]. */
2627
        for (i=0; i < ERRNO_TABLE_SIZE; i++)
2628
                target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2629

    
2630
        /* automatic consistency check if same arch */
2631
#if defined(__i386__) && defined(TARGET_I386)
2632
        if (ie->target_cmd != ie->host_cmd) {
2633
            fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2634
                    ie->target_cmd, ie->host_cmd);
2635
        }
2636
#endif
2637
        ie++;
2638
    }
2639
}
2640

    
2641
#if TARGET_ABI_BITS == 32
2642
static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2643
{
2644
#ifdef TARGET_WORDS_BIG_ENDIAN
2645
    return ((uint64_t)word0 << 32) | word1;
2646
#else
2647
    return ((uint64_t)word1 << 32) | word0;
2648
#endif
2649
}
2650
#else /* TARGET_ABI_BITS == 32 */
2651
static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2652
{
2653
    return word0;
2654
}
2655
#endif /* TARGET_ABI_BITS != 32 */
2656

    
2657
#ifdef TARGET_NR_truncate64
2658
static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2659
                                         abi_long arg2,
2660
                                         abi_long arg3,
2661
                                         abi_long arg4)
2662
{
2663
#ifdef TARGET_ARM
2664
    if (((CPUARMState *)cpu_env)->eabi)
2665
      {
2666
        arg2 = arg3;
2667
        arg3 = arg4;
2668
      }
2669
#endif
2670
    return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2671
}
2672
#endif
2673

    
2674
#ifdef TARGET_NR_ftruncate64
2675
static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2676
                                          abi_long arg2,
2677
                                          abi_long arg3,
2678
                                          abi_long arg4)
2679
{
2680
#ifdef TARGET_ARM
2681
    if (((CPUARMState *)cpu_env)->eabi)
2682
      {
2683
        arg2 = arg3;
2684
        arg3 = arg4;
2685
      }
2686
#endif
2687
    return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2688
}
2689
#endif
2690

    
2691
static inline abi_long target_to_host_timespec(struct timespec *host_ts,
2692
                                               abi_ulong target_addr)
2693
{
2694
    struct target_timespec *target_ts;
2695

    
2696
    if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
2697
        return -TARGET_EFAULT;
2698
    host_ts->tv_sec = tswapl(target_ts->tv_sec);
2699
    host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2700
    unlock_user_struct(target_ts, target_addr, 0);
2701
}
2702

    
2703
static inline abi_long host_to_target_timespec(abi_ulong target_addr,
2704
                                               struct timespec *host_ts)
2705
{
2706
    struct target_timespec *target_ts;
2707

    
2708
    if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
2709
        return -TARGET_EFAULT;
2710
    target_ts->tv_sec = tswapl(host_ts->tv_sec);
2711
    target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2712
    unlock_user_struct(target_ts, target_addr, 1);
2713
}
2714

    
2715
/* do_syscall() should always have a single exit point at the end so
2716
   that actions, such as logging of syscall results, can be performed.
2717
   All errnos that do_syscall() returns must be -TARGET_<errcode>. */
2718
abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2719
                    abi_long arg2, abi_long arg3, abi_long arg4,
2720
                    abi_long arg5, abi_long arg6)
2721
{
2722
    abi_long ret;
2723
    struct stat st;
2724
    struct statfs stfs;
2725
    void *p;
2726

    
2727
#ifdef DEBUG
2728
    gemu_log("syscall %d", num);
2729
#endif
2730
    if(do_strace)
2731
        print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
2732

    
2733
    switch(num) {
2734
    case TARGET_NR_exit:
2735
#ifdef HAVE_GPROF
2736
        _mcleanup();
2737
#endif
2738
        gdb_exit(cpu_env, arg1);
2739
        /* XXX: should free thread stack and CPU env */
2740
        _exit(arg1);
2741
        ret = 0; /* avoid warning */
2742
        break;
2743
    case TARGET_NR_read:
2744
        page_unprotect_range(arg2, arg3);
2745
        if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
2746
            goto efault;
2747
        ret = get_errno(read(arg1, p, arg3));
2748
        unlock_user(p, arg2, ret);
2749
        break;
2750
    case TARGET_NR_write:
2751
        if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
2752
            goto efault;
2753
        ret = get_errno(write(arg1, p, arg3));
2754
        unlock_user(p, arg2, 0);
2755
        break;
2756
    case TARGET_NR_open:
2757
        if (!(p = lock_user_string(arg1))) {
2758
            return -TARGET_EFAULT;
2759
            goto fail;
2760
        }
2761
        ret = get_errno(open(path(p),
2762
                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
2763
                             arg3));
2764
        unlock_user(p, arg1, 0);
2765
        break;
2766
#if defined(TARGET_NR_openat) && defined(__NR_openat)
2767
    case TARGET_NR_openat:
2768
        if (!(p = lock_user_string(arg2)))
2769
            goto efault;
2770
        ret = get_errno(sys_openat(arg1,
2771
                                   path(p),
2772
                                   target_to_host_bitmask(arg3, fcntl_flags_tbl),
2773
                                   arg4));
2774
        unlock_user(p, arg2, 0);
2775
        break;
2776
#endif
2777
    case TARGET_NR_close:
2778
        ret = get_errno(close(arg1));
2779
        break;
2780
    case TARGET_NR_brk:
2781
        ret = do_brk(arg1);
2782
        break;
2783
    case TARGET_NR_fork:
2784
        ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
2785
        break;
2786
#ifdef TARGET_NR_waitpid
2787
    case TARGET_NR_waitpid:
2788
        {
2789
            int status;
2790
            ret = get_errno(waitpid(arg1, &status, arg3));
2791
            if (!is_error(ret) && arg2)
2792
                tput32(arg2, status);
2793
        }
2794
        break;
2795
#endif
2796
#ifdef TARGET_NR_creat /* not on alpha */
2797
    case TARGET_NR_creat:
2798
        if (!(p = lock_user_string(arg1)))
2799
            goto efault;
2800
        ret = get_errno(creat(p, arg2));
2801
        unlock_user(p, arg1, 0);
2802
        break;
2803
#endif
2804
    case TARGET_NR_link:
2805
        {
2806
            void * p2;
2807
            p = lock_user_string(arg1);
2808
            p2 = lock_user_string(arg2);
2809
            if (!p || !p2)
2810
                ret = -TARGET_EFAULT;
2811
            else
2812
                ret = get_errno(link(p, p2));
2813
            unlock_user(p2, arg2, 0);
2814
            unlock_user(p, arg1, 0);
2815
        }
2816
        break;
2817
#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2818
    case TARGET_NR_linkat:
2819
        {
2820
            void * p2 = NULL;
2821
            if (!arg2 || !arg4)
2822
                goto efault;
2823
            p  = lock_user_string(arg2);
2824
            p2 = lock_user_string(arg4);
2825
            if (!p || !p2)
2826
                ret = -TARGET_EFAULT;
2827
            else
2828
                ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
2829
            unlock_user(p, arg2, 0);
2830
            unlock_user(p2, arg4, 0);
2831
        }
2832
        break;
2833
#endif
2834
    case TARGET_NR_unlink:
2835
        if (!(p = lock_user_string(arg1)))
2836
            goto efault;
2837
        ret = get_errno(unlink(p));
2838
        unlock_user(p, arg1, 0);
2839
        break;
2840
#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2841
    case TARGET_NR_unlinkat:
2842
        if (!(p = lock_user_string(arg2)))
2843
            goto efault;
2844
        ret = get_errno(sys_unlinkat(arg1, p, arg3));
2845
        unlock_user(p, arg2, 0);
2846
#endif
2847
    case TARGET_NR_execve:
2848
        {
2849
            char **argp, **envp;
2850
            int argc, envc;
2851
            abi_ulong gp;
2852
            abi_ulong guest_argp;
2853
            abi_ulong guest_envp;
2854
            abi_ulong addr;
2855
            char **q;
2856

    
2857
            argc = 0;
2858
            guest_argp = arg2;
2859
            for (gp = guest_argp; tgetl(gp); gp++)
2860
                argc++;
2861
            envc = 0;
2862
            guest_envp = arg3;
2863
            for (gp = guest_envp; tgetl(gp); gp++)
2864
                envc++;
2865

    
2866
            argp = alloca((argc + 1) * sizeof(void *));
2867
            envp = alloca((envc + 1) * sizeof(void *));
2868

    
2869
            for (gp = guest_argp, q = argp; ;
2870
                  gp += sizeof(abi_ulong), q++) {
2871
                addr = tgetl(gp);
2872
                if (!addr)
2873
                    break;
2874
                if (!(*q = lock_user_string(addr))) {
2875
                    ret = -TARGET_EFAULT;
2876
                    goto execve_fail;
2877
                }
2878
            }
2879
            *q = NULL;
2880

    
2881
            for (gp = guest_envp, q = envp; ;
2882
                  gp += sizeof(abi_ulong), q++) {
2883
                addr = tgetl(gp);
2884
                if (!addr)
2885
                    break;
2886
                if (!(*q = lock_user_string(addr))) {
2887
                    ret = -TARGET_EFAULT;
2888
                    goto execve_fail;
2889
                }
2890
            }
2891
            *q = NULL;
2892

    
2893
            if (!(p = lock_user_string(arg1))) {
2894
                ret = -TARGET_EFAULT;
2895
                goto execve_fail;
2896
            }
2897
            ret = get_errno(execve(p, argp, envp));
2898
            unlock_user(p, arg1, 0);
2899

    
2900
        execve_fail:
2901
            for (gp = guest_argp, q = argp; *q;
2902
                  gp += sizeof(abi_ulong), q++) {
2903
                addr = tgetl(gp);
2904
                unlock_user(*q, addr, 0);
2905
            }
2906
            for (gp = guest_envp, q = envp; *q;
2907
                  gp += sizeof(abi_ulong), q++) {
2908
                addr = tgetl(gp);
2909
                unlock_user(*q, addr, 0);
2910
            }
2911
        }
2912
        break;
2913
    case TARGET_NR_chdir:
2914
        if (!(p = lock_user_string(arg1)))
2915
            goto efault;
2916
        ret = get_errno(chdir(p));
2917
        unlock_user(p, arg1, 0);
2918
        break;
2919
#ifdef TARGET_NR_time
2920
    case TARGET_NR_time:
2921
        {
2922
            time_t host_time;
2923
            ret = get_errno(time(&host_time));
2924
            if (!is_error(ret) && arg1)
2925
                tputl(arg1, host_time);
2926
        }
2927
        break;
2928
#endif
2929
    case TARGET_NR_mknod:
2930
        if (!(p = lock_user_string(arg1)))
2931
            goto efault;
2932
        ret = get_errno(mknod(p, arg2, arg3));
2933
        unlock_user(p, arg1, 0);
2934
        break;
2935
#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2936
    case TARGET_NR_mknodat:
2937
        if (!(p = lock_user_string(arg2)))
2938
            goto efault;
2939
        ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2940
        unlock_user(p, arg2, 0);
2941
        break;
2942
#endif
2943
    case TARGET_NR_chmod:
2944
        if (!(p = lock_user_string(arg1)))
2945
            goto efault;
2946
        ret = get_errno(chmod(p, arg2));
2947
        unlock_user(p, arg1, 0);
2948
        break;
2949
#ifdef TARGET_NR_break
2950
    case TARGET_NR_break:
2951
        goto unimplemented;
2952
#endif
2953
#ifdef TARGET_NR_oldstat
2954
    case TARGET_NR_oldstat:
2955
        goto unimplemented;
2956
#endif
2957
    case TARGET_NR_lseek:
2958
        ret = get_errno(lseek(arg1, arg2, arg3));
2959
        break;
2960
#ifdef TARGET_NR_getxpid
2961
    case TARGET_NR_getxpid:
2962
#else
2963
    case TARGET_NR_getpid:
2964
#endif
2965
        ret = get_errno(getpid());
2966
        break;
2967
    case TARGET_NR_mount:
2968
                {
2969
                        /* need to look at the data field */
2970
                        void *p2, *p3;
2971
                        p = lock_user_string(arg1);
2972
                        p2 = lock_user_string(arg2);
2973
                        p3 = lock_user_string(arg3);
2974
                        if (!p || !p2 || !p3)
2975
                            ret = -TARGET_EFAULT;
2976
                        else
2977
                            /* FIXME - arg5 should be locked, but it isn't clear how to
2978
                             * do that since it's not guaranteed to be a NULL-terminated
2979
                             * string.
2980
                             */
2981
                            ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
2982
                        unlock_user(p, arg1, 0);
2983
                        unlock_user(p2, arg2, 0);
2984
                        unlock_user(p3, arg3, 0);
2985
                        break;
2986
                }
2987
#ifdef TARGET_NR_umount
2988
    case TARGET_NR_umount:
2989
        if (!(p = lock_user_string(arg1)))
2990
            goto efault;
2991
        ret = get_errno(umount(p));
2992
        unlock_user(p, arg1, 0);
2993
        break;
2994
#endif
2995
#ifdef TARGET_NR_stime /* not on alpha */
2996
    case TARGET_NR_stime:
2997
        {
2998
            time_t host_time;
2999
            host_time = tgetl(arg1);
3000
            ret = get_errno(stime(&host_time));
3001
        }
3002
        break;
3003
#endif
3004
    case TARGET_NR_ptrace:
3005
        goto unimplemented;
3006
#ifdef TARGET_NR_alarm /* not on alpha */
3007
    case TARGET_NR_alarm:
3008
        ret = alarm(arg1);
3009
        break;
3010
#endif
3011
#ifdef TARGET_NR_oldfstat
3012
    case TARGET_NR_oldfstat:
3013
        goto unimplemented;
3014
#endif
3015
#ifdef TARGET_NR_pause /* not on alpha */
3016
    case TARGET_NR_pause:
3017
        ret = get_errno(pause());
3018
        break;
3019
#endif
3020
#ifdef TARGET_NR_utime
3021
    case TARGET_NR_utime:
3022
        {
3023
            struct utimbuf tbuf, *host_tbuf;
3024
            struct target_utimbuf *target_tbuf;
3025
            if (arg2) {
3026
                if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3027
                    goto efault;
3028
                tbuf.actime = tswapl(target_tbuf->actime);
3029
                tbuf.modtime = tswapl(target_tbuf->modtime);
3030
                unlock_user_struct(target_tbuf, arg2, 0);
3031
                host_tbuf = &tbuf;
3032
            } else {
3033
                host_tbuf = NULL;
3034
            }
3035
            if (!(p = lock_user_string(arg1)))
3036
                goto efault;
3037
            ret = get_errno(utime(p, host_tbuf));
3038
            unlock_user(p, arg1, 0);
3039
        }
3040
        break;
3041
#endif
3042
    case TARGET_NR_utimes:
3043
        {
3044
            struct timeval *tvp, tv[2];
3045
            if (arg2) {
3046
                target_to_host_timeval(&tv[0], arg2);
3047
                target_to_host_timeval(&tv[1],
3048
                    arg2 + sizeof (struct target_timeval));
3049
                tvp = tv;
3050
            } else {
3051
                tvp = NULL;
3052
            }
3053
            if (!(p = lock_user_string(arg1)))
3054
                goto efault;
3055
            ret = get_errno(utimes(p, tvp));
3056
            unlock_user(p, arg1, 0);
3057
        }
3058
        break;
3059
#ifdef TARGET_NR_stty
3060
    case TARGET_NR_stty:
3061
        goto unimplemented;
3062
#endif
3063
#ifdef TARGET_NR_gtty
3064
    case TARGET_NR_gtty:
3065
        goto unimplemented;
3066
#endif
3067
    case TARGET_NR_access:
3068
        if (!(p = lock_user_string(arg1)))
3069
            goto efault;
3070
        ret = get_errno(access(p, arg2));
3071
        unlock_user(p, arg1, 0);
3072
        break;
3073
#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3074
    case TARGET_NR_faccessat:
3075
        if (!(p = lock_user_string(arg2)))
3076
            goto efault;
3077
        ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3078
        unlock_user(p, arg2, 0);
3079
        break;
3080
#endif
3081
#ifdef TARGET_NR_nice /* not on alpha */
3082
    case TARGET_NR_nice:
3083
        ret = get_errno(nice(arg1));
3084
        break;
3085
#endif
3086
#ifdef TARGET_NR_ftime
3087
    case TARGET_NR_ftime:
3088
        goto unimplemented;
3089
#endif
3090
    case TARGET_NR_sync:
3091
        sync();
3092
        ret = 0;
3093
        break;
3094
    case TARGET_NR_kill:
3095
        ret = get_errno(kill(arg1, arg2));
3096
        break;
3097
    case TARGET_NR_rename:
3098
        {
3099
            void *p2;
3100
            p = lock_user_string(arg1);
3101
            p2 = lock_user_string(arg2);
3102
            if (!p || !p2)
3103
                ret = -TARGET_EFAULT;
3104
            else
3105
                ret = get_errno(rename(p, p2));
3106
            unlock_user(p2, arg2, 0);
3107
            unlock_user(p, arg1, 0);
3108
        }
3109
        break;
3110
#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3111
    case TARGET_NR_renameat:
3112
        {
3113
            void *p2;
3114
            p  = lock_user_string(arg2);
3115
            p2 = lock_user_string(arg4);
3116
            if (!p || !p2)
3117
                ret = -TARGET_EFAULT;
3118
            else
3119
                ret = get_errno(sys_renameat(arg1, p, arg3, p2));
3120
            unlock_user(p2, arg4, 0);
3121
            unlock_user(p, arg2, 0);
3122
        }
3123
        break;
3124
#endif
3125
    case TARGET_NR_mkdir:
3126
        if (!(p = lock_user_string(arg1)))
3127
            goto efault;
3128
        ret = get_errno(mkdir(p, arg2));
3129
        unlock_user(p, arg1, 0);
3130
        break;
3131
#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3132
    case TARGET_NR_mkdirat:
3133
        if (!(p = lock_user_string(arg2)))
3134
            goto efault;
3135
        ret = get_errno(sys_mkdirat(arg1, p, arg3));
3136
        unlock_user(p, arg2, 0);
3137
        break;
3138
#endif
3139
    case TARGET_NR_rmdir:
3140
        if (!(p = lock_user_string(arg1)))
3141
            goto efault;
3142
        ret = get_errno(rmdir(p));
3143
        unlock_user(p, arg1, 0);
3144
        break;
3145
    case TARGET_NR_dup:
3146
        ret = get_errno(dup(arg1));
3147
        break;
3148
    case TARGET_NR_pipe:
3149
        {
3150
            int host_pipe[2];
3151
            ret = get_errno(pipe(host_pipe));
3152
            if (!is_error(ret)) {
3153
#if defined(TARGET_MIPS)
3154
                CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3155
                env->gpr[3][env->current_tc] = host_pipe[1];
3156
                ret = host_pipe[0];
3157
#else
3158
                tput32(arg1, host_pipe[0]);
3159
                tput32(arg1 + 4, host_pipe[1]);
3160
#endif
3161
            }
3162
        }
3163
        break;
3164
    case TARGET_NR_times:
3165
        {
3166
            struct target_tms *tmsp;
3167
            struct tms tms;
3168
            ret = get_errno(times(&tms));
3169
            if (arg1) {
3170
                tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3171
                if (!tmsp)
3172
                    goto efault;
3173
                tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3174
                tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3175
                tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3176
                tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
3177
            }
3178
            if (!is_error(ret))
3179
                ret = host_to_target_clock_t(ret);
3180
        }
3181
        break;
3182
#ifdef TARGET_NR_prof
3183
    case TARGET_NR_prof:
3184
        goto unimplemented;
3185
#endif
3186
#ifdef TARGET_NR_signal
3187
    case TARGET_NR_signal:
3188
        goto unimplemented;
3189
#endif
3190
    case TARGET_NR_acct:
3191
        if (!(p = lock_user_string(arg1)))
3192
            goto efault;
3193
        ret = get_errno(acct(path(p)));
3194
        unlock_user(p, arg1, 0);
3195
        break;
3196
#ifdef TARGET_NR_umount2 /* not on alpha */
3197
    case TARGET_NR_umount2:
3198
        if (!(p = lock_user_string(arg1)))
3199
            goto efault;
3200
        ret = get_errno(umount2(p, arg2));
3201
        unlock_user(p, arg1, 0);
3202
        break;
3203
#endif
3204
#ifdef TARGET_NR_lock
3205
    case TARGET_NR_lock:
3206
        goto unimplemented;
3207
#endif
3208
    case TARGET_NR_ioctl:
3209
        ret = do_ioctl(arg1, arg2, arg3);
3210
        break;
3211
    case TARGET_NR_fcntl:
3212
        ret = get_errno(do_fcntl(arg1, arg2, arg3));
3213
        break;
3214
#ifdef TARGET_NR_mpx
3215
    case TARGET_NR_mpx:
3216
        goto unimplemented;
3217
#endif
3218
    case TARGET_NR_setpgid:
3219
        ret = get_errno(setpgid(arg1, arg2));
3220
        break;
3221
#ifdef TARGET_NR_ulimit
3222
    case TARGET_NR_ulimit:
3223
        goto unimplemented;
3224
#endif
3225
#ifdef TARGET_NR_oldolduname
3226
    case TARGET_NR_oldolduname:
3227
        goto unimplemented;
3228
#endif
3229
    case TARGET_NR_umask:
3230
        ret = get_errno(umask(arg1));
3231
        break;
3232
    case TARGET_NR_chroot:
3233
        if (!(p = lock_user_string(arg1)))
3234
            goto efault;
3235
        ret = get_errno(chroot(p));
3236
        unlock_user(p, arg1, 0);
3237
        break;
3238
    case TARGET_NR_ustat:
3239
        goto unimplemented;
3240
    case TARGET_NR_dup2:
3241
        ret = get_errno(dup2(arg1, arg2));
3242
        break;
3243
#ifdef TARGET_NR_getppid /* not on alpha */
3244
    case TARGET_NR_getppid:
3245
        ret = get_errno(getppid());
3246
        break;
3247
#endif
3248
    case TARGET_NR_getpgrp:
3249
        ret = get_errno(getpgrp());
3250
        break;
3251
    case TARGET_NR_setsid:
3252
        ret = get_errno(setsid());
3253
        break;
3254
#ifdef TARGET_NR_sigaction
3255
    case TARGET_NR_sigaction:
3256
        {
3257
#if !defined(TARGET_MIPS)
3258
            struct target_old_sigaction *old_act;
3259
            struct target_sigaction act, oact, *pact;
3260
            if (arg2) {
3261
                if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3262
                    goto efault;
3263
                act._sa_handler = old_act->_sa_handler;
3264
                target_siginitset(&act.sa_mask, old_act->sa_mask);
3265
                act.sa_flags = old_act->sa_flags;
3266
                act.sa_restorer = old_act->sa_restorer;
3267
                unlock_user_struct(old_act, arg2, 0);
3268
                pact = &act;
3269
            } else {
3270
                pact = NULL;
3271
            }
3272
            ret = get_errno(do_sigaction(arg1, pact, &oact));
3273
            if (!is_error(ret) && arg3) {
3274
                if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3275
                    goto efault;
3276
                old_act->_sa_handler = oact._sa_handler;
3277
                old_act->sa_mask = oact.sa_mask.sig[0];
3278
                old_act->sa_flags = oact.sa_flags;
3279
                old_act->sa_restorer = oact.sa_restorer;
3280
                unlock_user_struct(old_act, arg3, 1);
3281
            }
3282
#else
3283
            struct target_sigaction act, oact, *pact, *old_act;
3284

    
3285
            if (arg2) {
3286
                if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3287
                    goto efault;
3288
                act._sa_handler = old_act->_sa_handler;
3289
                target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3290
                act.sa_flags = old_act->sa_flags;
3291
                unlock_user_struct(old_act, arg2, 0);
3292
                pact = &act;
3293
            } else {
3294
                pact = NULL;
3295
            }
3296

    
3297
            ret = get_errno(do_sigaction(arg1, pact, &oact));
3298

    
3299
            if (!is_error(ret) && arg3) {
3300
                if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3301
                    goto efault;
3302
                old_act->_sa_handler = oact._sa_handler;
3303
                old_act->sa_flags = oact.sa_flags;
3304
                old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3305
                old_act->sa_mask.sig[1] = 0;
3306
                old_act->sa_mask.sig[2] = 0;
3307
                old_act->sa_mask.sig[3] = 0;
3308
                unlock_user_struct(old_act, arg3, 1);
3309
            }
3310
#endif
3311
        }
3312
        break;
3313
#endif
3314
    case TARGET_NR_rt_sigaction:
3315
        {
3316
            struct target_sigaction *act;
3317
            struct target_sigaction *oact;
3318

    
3319
            if (arg2) {
3320
                if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3321
                    goto efault;
3322
            } else
3323
                act = NULL;
3324
            if (arg3) {
3325
                if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3326
                    ret = -TARGET_EFAULT;
3327
                    goto rt_sigaction_fail;
3328
                }
3329
            } else
3330
                oact = NULL;
3331
            ret = get_errno(do_sigaction(arg1, act, oact));
3332
        rt_sigaction_fail:
3333
            if (act)
3334
                unlock_user_struct(act, arg2, 0);
3335
            if (oact)
3336
                unlock_user_struct(oact, arg3, 1);
3337
        }
3338
        break;
3339
#ifdef TARGET_NR_sgetmask /* not on alpha */
3340
    case TARGET_NR_sgetmask:
3341
        {
3342
            sigset_t cur_set;
3343
            abi_ulong target_set;
3344
            sigprocmask(0, NULL, &cur_set);
3345
            host_to_target_old_sigset(&target_set, &cur_set);
3346
            ret = target_set;
3347
        }
3348
        break;
3349
#endif
3350
#ifdef TARGET_NR_ssetmask /* not on alpha */
3351
    case TARGET_NR_ssetmask:
3352
        {
3353
            sigset_t set, oset, cur_set;
3354
            abi_ulong target_set = arg1;
3355
            sigprocmask(0, NULL, &cur_set);
3356
            target_to_host_old_sigset(&set, &target_set);
3357
            sigorset(&set, &set, &cur_set);
3358
            sigprocmask(SIG_SETMASK, &set, &oset);
3359
            host_to_target_old_sigset(&target_set, &oset);
3360
            ret = target_set;
3361
        }
3362
        break;
3363
#endif
3364
#ifdef TARGET_NR_sigprocmask
3365
    case TARGET_NR_sigprocmask:
3366
        {
3367
            int how = arg1;
3368
            sigset_t set, oldset, *set_ptr;
3369

    
3370
            if (arg2) {
3371
                switch(how) {
3372
                case TARGET_SIG_BLOCK:
3373
                    how = SIG_BLOCK;
3374
                    break;
3375
                case TARGET_SIG_UNBLOCK:
3376
                    how = SIG_UNBLOCK;
3377
                    break;
3378
                case TARGET_SIG_SETMASK:
3379
                    how = SIG_SETMASK;
3380
                    break;
3381
                default:
3382
                    ret = -TARGET_EINVAL;
3383
                    goto fail;
3384
                }
3385
                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3386
                    goto efault;
3387
                target_to_host_old_sigset(&set, p);
3388
                unlock_user(p, arg2, 0);
3389
                set_ptr = &set;
3390
            } else {
3391
                how = 0;
3392
                set_ptr = NULL;
3393
            }
3394
            ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
3395
            if (!is_error(ret) && arg3) {
3396
                if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3397
                    goto efault;
3398
                host_to_target_old_sigset(p, &oldset);
3399
                unlock_user(p, arg3, sizeof(target_sigset_t));
3400
            }
3401
        }
3402
        break;
3403
#endif
3404
    case TARGET_NR_rt_sigprocmask:
3405
        {
3406
            int how = arg1;
3407
            sigset_t set, oldset, *set_ptr;
3408

    
3409
            if (arg2) {
3410
                switch(how) {
3411
                case TARGET_SIG_BLOCK:
3412
                    how = SIG_BLOCK;
3413
                    break;
3414
                case TARGET_SIG_UNBLOCK:
3415
                    how = SIG_UNBLOCK;
3416
                    break;
3417
                case TARGET_SIG_SETMASK:
3418
                    how = SIG_SETMASK;
3419
                    break;
3420
                default:
3421
                    ret = -TARGET_EINVAL;
3422
                    goto fail;
3423
                }
3424
                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3425
                    goto efault;
3426
                target_to_host_sigset(&set, p);
3427
                unlock_user(p, arg2, 0);
3428
                set_ptr = &set;
3429
            } else {
3430
                how = 0;
3431
                set_ptr = NULL;
3432
            }
3433
            ret = get_errno(sigprocmask(how, set_ptr, &oldset));
3434
            if (!is_error(ret) && arg3) {
3435
                if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3436
                    goto efault;
3437
                host_to_target_sigset(p, &oldset);
3438
                unlock_user(p, arg3, sizeof(target_sigset_t));
3439
            }
3440
        }
3441
        break;
3442
#ifdef TARGET_NR_sigpending
3443
    case TARGET_NR_sigpending:
3444
        {
3445
            sigset_t set;
3446
            ret = get_errno(sigpending(&set));
3447
            if (!is_error(ret)) {
3448
                if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3449
                    goto efault;
3450
                host_to_target_old_sigset(p, &set);
3451
                unlock_user(p, arg1, sizeof(target_sigset_t));
3452
            }
3453
        }
3454
        break;
3455
#endif
3456
    case TARGET_NR_rt_sigpending:
3457
        {
3458
            sigset_t set;
3459
            ret = get_errno(sigpending(&set));
3460
            if (!is_error(ret)) {
3461
                if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3462
                    goto efault;
3463
                host_to_target_sigset(p, &set);
3464
                unlock_user(p, arg1, sizeof(target_sigset_t));
3465
            }
3466
        }
3467
        break;
3468
#ifdef TARGET_NR_sigsuspend
3469
    case TARGET_NR_sigsuspend:
3470
        {
3471
            sigset_t set;
3472
            if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3473
                goto efault;
3474
            target_to_host_old_sigset(&set, p);
3475
            unlock_user(p, arg1, 0);
3476
            ret = get_errno(sigsuspend(&set));
3477
        }
3478
        break;
3479
#endif
3480
    case TARGET_NR_rt_sigsuspend:
3481
        {
3482
            sigset_t set;
3483
            if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3484
                goto efault;
3485
            target_to_host_sigset(&set, p);
3486
            unlock_user(p, arg1, 0);
3487
            ret = get_errno(sigsuspend(&set));
3488
        }
3489
        break;
3490
    case TARGET_NR_rt_sigtimedwait:
3491
        {
3492
            sigset_t set;
3493
            struct timespec uts, *puts;
3494
            siginfo_t uinfo;
3495

    
3496
            if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3497
                goto efault;
3498
            target_to_host_sigset(&set, p);
3499
            unlock_user(p, arg1, 0);
3500
            if (arg3) {
3501
                puts = &uts;
3502
                target_to_host_timespec(puts, arg3);
3503
            } else {
3504
                puts = NULL;
3505
            }
3506
            ret = get_errno(sigtimedwait(&set, &uinfo, puts));
3507
            if (!is_error(ret) && arg2) {
3508
                if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3509
                    goto efault;
3510
                host_to_target_siginfo(p, &uinfo);
3511
                unlock_user(p, arg2, sizeof(target_sigset_t));
3512
            }
3513
        }
3514
        break;
3515
    case TARGET_NR_rt_sigqueueinfo:
3516
        {
3517
            siginfo_t uinfo;
3518
            if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3519
                goto efault;
3520
            target_to_host_siginfo(&uinfo, p);
3521
            unlock_user(p, arg1, 0);
3522
            ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3523
        }
3524
        break;
3525
#ifdef TARGET_NR_sigreturn
3526
    case TARGET_NR_sigreturn:
3527
        /* NOTE: ret is eax, so not transcoding must be done */
3528
        ret = do_sigreturn(cpu_env);
3529
        break;
3530
#endif
3531
    case TARGET_NR_rt_sigreturn:
3532
        /* NOTE: ret is eax, so not transcoding must be done */
3533
        ret = do_rt_sigreturn(cpu_env);
3534
        break;
3535
    case TARGET_NR_sethostname:
3536
        if (!(p = lock_user_string(arg1)))
3537
            goto efault;
3538
        ret = get_errno(sethostname(p, arg2));
3539
        unlock_user(p, arg1, 0);
3540
        break;
3541
    case TARGET_NR_setrlimit:
3542
        {
3543
            /* XXX: convert resource ? */
3544
            int resource = arg1;
3545
            struct target_rlimit *target_rlim;
3546
            struct rlimit rlim;
3547
            if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3548
                goto efault;
3549
            rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3550
            rlim.rlim_max = tswapl(target_rlim->rlim_max);
3551
            unlock_user_struct(target_rlim, arg2, 0);
3552
            ret = get_errno(setrlimit(resource, &rlim));
3553
        }
3554
        break;
3555
    case TARGET_NR_getrlimit:
3556
        {
3557
            /* XXX: convert resource ? */
3558
            int resource = arg1;
3559
            struct target_rlimit *target_rlim;
3560
            struct rlimit rlim;
3561

    
3562
            ret = get_errno(getrlimit(resource, &rlim));
3563
            if (!is_error(ret)) {
3564
                if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3565
                    goto efault;
3566
                rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3567
                rlim.rlim_max = tswapl(target_rlim->rlim_max);
3568
                unlock_user_struct(target_rlim, arg2, 1);
3569
            }
3570
        }
3571
        break;
3572
    case TARGET_NR_getrusage:
3573
        {
3574
            struct rusage rusage;
3575
            ret = get_errno(getrusage(arg1, &rusage));
3576
            if (!is_error(ret)) {
3577
                host_to_target_rusage(arg2, &rusage);
3578
            }
3579
        }
3580
        break;
3581
    case TARGET_NR_gettimeofday:
3582
        {
3583
            struct timeval tv;
3584
            ret = get_errno(gettimeofday(&tv, NULL));
3585
            if (!is_error(ret)) {
3586
                host_to_target_timeval(arg1, &tv);
3587
            }
3588
        }
3589
        break;
3590
    case TARGET_NR_settimeofday:
3591
        {
3592
            struct timeval tv;
3593
            target_to_host_timeval(&tv, arg1);
3594
            ret = get_errno(settimeofday(&tv, NULL));
3595
        }
3596
        break;
3597
#ifdef TARGET_NR_select
3598
    case TARGET_NR_select:
3599
        {
3600
            struct target_sel_arg_struct *sel;
3601
            abi_ulong inp, outp, exp, tvp;
3602
            long nsel;
3603

    
3604
            if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3605
                goto efault;
3606
            nsel = tswapl(sel->n);
3607
            inp = tswapl(sel->inp);
3608
            outp = tswapl(sel->outp);
3609
            exp = tswapl(sel->exp);
3610
            tvp = tswapl(sel->tvp);
3611
            unlock_user_struct(sel, arg1, 0);
3612
            ret = do_select(nsel, inp, outp, exp, tvp);
3613
        }
3614
        break;
3615
#endif
3616
    case TARGET_NR_symlink:
3617
        {
3618
            void *p2;
3619
            p = lock_user_string(arg1);
3620
            p2 = lock_user_string(arg2);
3621
            if (!p || !p2)
3622
                ret = -TARGET_EFAULT;
3623
            else
3624
                ret = get_errno(symlink(p, p2));
3625
            unlock_user(p2, arg2, 0);
3626
            unlock_user(p, arg1, 0);
3627
        }
3628
        break;
3629
#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3630
    case TARGET_NR_symlinkat:
3631
        {
3632
            void *p2;
3633
            p  = lock_user_string(arg1);
3634
            p2 = lock_user_string(arg3);
3635
            if (!p || !p2)
3636
                ret = -TARGET_EFAULT;
3637
            else
3638
                ret = get_errno(sys_symlinkat(p, arg2, p2));
3639
            unlock_user(p2, arg3, 0);
3640
            unlock_user(p, arg1, 0);
3641
        }
3642
        break;
3643
#endif
3644
#ifdef TARGET_NR_oldlstat
3645
    case TARGET_NR_oldlstat:
3646
        goto unimplemented;
3647
#endif
3648
    case TARGET_NR_readlink:
3649
        {
3650
            void *p2;
3651
            p = lock_user_string(arg1);
3652
            p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
3653
            if (!p || !p2)
3654
                ret = -TARGET_EFAULT;
3655
            else
3656
                ret = get_errno(readlink(path(p), p2, arg3));
3657
            unlock_user(p2, arg2, ret);
3658
            unlock_user(p, arg1, 0);
3659
        }
3660
        break;
3661
#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3662
    case TARGET_NR_readlinkat:
3663
        {
3664
            void *p2;
3665
            p  = lock_user_string(arg2);
3666
            p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
3667
            if (!p || !p2)
3668
                ret = -TARGET_EFAULT;
3669
            else
3670
                ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
3671
            unlock_user(p2, arg3, ret);
3672
            unlock_user(p, arg2, 0);
3673
        }
3674
        break;
3675
#endif
3676
#ifdef TARGET_NR_uselib
3677
    case TARGET_NR_uselib:
3678
        goto unimplemented;
3679
#endif
3680
#ifdef TARGET_NR_swapon
3681
    case TARGET_NR_swapon:
3682
        if (!(p = lock_user_string(arg1)))
3683
            goto efault;
3684
        ret = get_errno(swapon(p, arg2));
3685
        unlock_user(p, arg1, 0);
3686
        break;
3687
#endif
3688
    case TARGET_NR_reboot:
3689
        goto unimplemented;
3690
#ifdef TARGET_NR_readdir
3691
    case TARGET_NR_readdir:
3692
        goto unimplemented;
3693
#endif
3694
#ifdef TARGET_NR_mmap
3695
    case TARGET_NR_mmap:
3696
#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
3697
        {
3698
            abi_ulong *v;
3699
            abi_ulong v1, v2, v3, v4, v5, v6;
3700
            if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
3701
                goto efault;
3702
            v1 = tswapl(v[0]);
3703
            v2 = tswapl(v[1]);
3704
            v3 = tswapl(v[2]);
3705
            v4 = tswapl(v[3]);
3706
            v5 = tswapl(v[4]);
3707
            v6 = tswapl(v[5]);
3708
            unlock_user(v, arg1, 0);
3709
            ret = get_errno(target_mmap(v1, v2, v3,
3710
                                        target_to_host_bitmask(v4, mmap_flags_tbl),
3711
                                        v5, v6));
3712
        }
3713
#else
3714
        ret = get_errno(target_mmap(arg1, arg2, arg3,
3715
                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
3716
                                    arg5,
3717
                                    arg6));
3718
#endif
3719
        break;
3720
#endif
3721
#ifdef TARGET_NR_mmap2
3722
    case TARGET_NR_mmap2:
3723
#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
3724
#define MMAP_SHIFT 12
3725
#else
3726
#define MMAP_SHIFT TARGET_PAGE_BITS
3727
#endif
3728
        ret = get_errno(target_mmap(arg1, arg2, arg3,
3729
                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
3730
                                    arg5,
3731
                                    arg6 << MMAP_SHIFT));
3732
        break;
3733
#endif
3734
    case TARGET_NR_munmap:
3735
        ret = get_errno(target_munmap(arg1, arg2));
3736
        break;
3737
    case TARGET_NR_mprotect:
3738
        ret = get_errno(target_mprotect(arg1, arg2, arg3));
3739
        break;
3740
#ifdef TARGET_NR_mremap
3741
    case TARGET_NR_mremap:
3742
        ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
3743
        break;
3744
#endif
3745
        /* ??? msync/mlock/munlock are broken for softmmu.  */
3746
#ifdef TARGET_NR_msync
3747
    case TARGET_NR_msync:
3748
        ret = get_errno(msync(g2h(arg1), arg2, arg3));
3749
        break;
3750
#endif
3751
#ifdef TARGET_NR_mlock
3752
    case TARGET_NR_mlock:
3753
        ret = get_errno(mlock(g2h(arg1), arg2));
3754
        break;
3755
#endif
3756
#ifdef TARGET_NR_munlock
3757
    case TARGET_NR_munlock:
3758
        ret = get_errno(munlock(g2h(arg1), arg2));
3759
        break;
3760
#endif
3761
#ifdef TARGET_NR_mlockall
3762
    case TARGET_NR_mlockall:
3763
        ret = get_errno(mlockall(arg1));
3764
        break;
3765
#endif
3766
#ifdef TARGET_NR_munlockall
3767
    case TARGET_NR_munlockall:
3768
        ret = get_errno(munlockall());
3769
        break;
3770
#endif
3771
    case TARGET_NR_truncate:
3772
        if (!(p = lock_user_string(arg1)))
3773
            goto efault;
3774
        ret = get_errno(truncate(p, arg2));
3775
        unlock_user(p, arg1, 0);
3776
        break;
3777
    case TARGET_NR_ftruncate:
3778
        ret = get_errno(ftruncate(arg1, arg2));
3779
        break;
3780
    case TARGET_NR_fchmod:
3781
        ret = get_errno(fchmod(arg1, arg2));
3782
        break;
3783
#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3784
    case TARGET_NR_fchmodat:
3785
        if (!(p = lock_user_string(arg2)))
3786
            goto efault;
3787
        ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3788
        unlock_user(p, arg2, 0);
3789
        break;
3790
#endif
3791
    case TARGET_NR_getpriority:
3792
        /* libc does special remapping of the return value of
3793
         * sys_getpriority() so it's just easiest to call
3794
         * sys_getpriority() directly rather than through libc. */
3795
        ret = sys_getpriority(arg1, arg2);
3796
        break;
3797
    case TARGET_NR_setpriority:
3798
        ret = get_errno(setpriority(arg1, arg2, arg3));
3799
        break;
3800
#ifdef TARGET_NR_profil
3801
    case TARGET_NR_profil:
3802
        goto unimplemented;
3803
#endif
3804
    case TARGET_NR_statfs:
3805
        if (!(p = lock_user_string(arg1)))
3806
            goto efault;
3807
        ret = get_errno(statfs(path(p), &stfs));
3808
        unlock_user(p, arg1, 0);
3809
    convert_statfs:
3810
        if (!is_error(ret)) {
3811
            struct target_statfs *target_stfs;
3812

    
3813
            if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
3814
                goto efault;
3815
            __put_user(stfs.f_type, &target_stfs->f_type);
3816
            __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3817
            __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3818
            __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3819
            __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3820
            __put_user(stfs.f_files, &target_stfs->f_files);
3821
            __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3822
            __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3823
            __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3824
            __put_user(stfs.f_namelen, &target_stfs->f_namelen);
3825
            unlock_user_struct(target_stfs, arg2, 1);
3826
        }
3827
        break;
3828
    case TARGET_NR_fstatfs:
3829
        ret = get_errno(fstatfs(arg1, &stfs));
3830
        goto convert_statfs;
3831
#ifdef TARGET_NR_statfs64
3832
    case TARGET_NR_statfs64:
3833
        if (!(p = lock_user_string(arg1)))
3834
            goto efault;
3835
        ret = get_errno(statfs(path(p), &stfs));
3836
        unlock_user(p, arg1, 0);
3837
    convert_statfs64:
3838
        if (!is_error(ret)) {
3839
            struct target_statfs64 *target_stfs;
3840

    
3841
            if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
3842
                goto efault;
3843
            __put_user(stfs.f_type, &target_stfs->f_type);
3844
            __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3845
            __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3846
            __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3847
            __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3848
            __put_user(stfs.f_files, &target_stfs->f_files);
3849
            __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3850
            __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3851
            __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3852
            __put_user(stfs.f_namelen, &target_stfs->f_namelen);
3853
            unlock_user_struct(target_stfs, arg3, 1);
3854
        }
3855
        break;
3856
    case TARGET_NR_fstatfs64:
3857
        ret = get_errno(fstatfs(arg1, &stfs));
3858
        goto convert_statfs64;
3859
#endif
3860
#ifdef TARGET_NR_ioperm
3861
    case TARGET_NR_ioperm:
3862
        goto unimplemented;
3863
#endif
3864
#ifdef TARGET_NR_socketcall
3865
    case TARGET_NR_socketcall:
3866
        ret = do_socketcall(arg1, arg2);
3867
        break;
3868
#endif
3869
#ifdef TARGET_NR_accept
3870
    case TARGET_NR_accept:
3871
        ret = do_accept(arg1, arg2, arg3);
3872
        break;
3873
#endif
3874
#ifdef TARGET_NR_bind
3875
    case TARGET_NR_bind:
3876
        ret = do_bind(arg1, arg2, arg3);
3877
        break;
3878
#endif
3879
#ifdef TARGET_NR_connect
3880
    case TARGET_NR_connect:
3881
        ret = do_connect(arg1, arg2, arg3);
3882
        break;
3883
#endif
3884
#ifdef TARGET_NR_getpeername
3885
    case TARGET_NR_getpeername:
3886
        ret = do_getpeername(arg1, arg2, arg3);
3887
        break;
3888
#endif
3889
#ifdef TARGET_NR_getsockname
3890
    case TARGET_NR_getsockname:
3891
        ret = do_getsockname(arg1, arg2, arg3);
3892
        break;
3893
#endif
3894
#ifdef TARGET_NR_getsockopt
3895
    case TARGET_NR_getsockopt:
3896
        ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3897
        break;
3898
#endif
3899
#ifdef TARGET_NR_listen
3900
    case TARGET_NR_listen:
3901
        ret = get_errno(listen(arg1, arg2));
3902
        break;
3903
#endif
3904
#ifdef TARGET_NR_recv
3905
    case TARGET_NR_recv:
3906
        ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3907
        break;
3908
#endif
3909
#ifdef TARGET_NR_recvfrom
3910
    case TARGET_NR_recvfrom:
3911
        ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3912
        break;
3913
#endif
3914
#ifdef TARGET_NR_recvmsg
3915
    case TARGET_NR_recvmsg:
3916
        ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3917
        break;
3918
#endif
3919
#ifdef TARGET_NR_send
3920
    case TARGET_NR_send:
3921
        ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3922
        break;
3923
#endif
3924
#ifdef TARGET_NR_sendmsg
3925
    case TARGET_NR_sendmsg:
3926
        ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3927
        break;
3928
#endif
3929
#ifdef TARGET_NR_sendto
3930
    case TARGET_NR_sendto:
3931
        ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3932
        break;
3933
#endif
3934
#ifdef TARGET_NR_shutdown
3935
    case TARGET_NR_shutdown:
3936
        ret = get_errno(shutdown(arg1, arg2));
3937
        break;
3938
#endif
3939
#ifdef TARGET_NR_socket
3940
    case TARGET_NR_socket:
3941
        ret = do_socket(arg1, arg2, arg3);
3942
        break;
3943
#endif
3944
#ifdef TARGET_NR_socketpair
3945
    case TARGET_NR_socketpair:
3946
        ret = do_socketpair(arg1, arg2, arg3, arg4);
3947
        break;
3948
#endif
3949
#ifdef TARGET_NR_setsockopt
3950
    case TARGET_NR_setsockopt:
3951
        ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3952
        break;
3953
#endif
3954

    
3955
    case TARGET_NR_syslog:
3956
        if (!(p = lock_user_string(arg2)))
3957
            goto efault;
3958
        ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3959
        unlock_user(p, arg2, 0);
3960
        break;
3961

    
3962
    case TARGET_NR_setitimer:
3963
        {
3964
            struct itimerval value, ovalue, *pvalue;
3965

    
3966
            if (arg2) {
3967
                pvalue = &value;
3968
                target_to_host_timeval(&pvalue->it_interval,
3969
                                       arg2);
3970
                target_to_host_timeval(&pvalue->it_value,
3971
                                       arg2 + sizeof(struct target_timeval));
3972
            } else {
3973
                pvalue = NULL;
3974
            }
3975
            ret = get_errno(setitimer(arg1, pvalue, &ovalue));
3976
            if (!is_error(ret) && arg3) {
3977
                host_to_target_timeval(arg3,
3978
                                       &ovalue.it_interval);
3979
                host_to_target_timeval(arg3 + sizeof(struct target_timeval),
3980
                                       &ovalue.it_value);
3981
            }
3982
        }
3983
        break;
3984
    case TARGET_NR_getitimer:
3985
        {
3986
            struct itimerval value;
3987

    
3988
            ret = get_errno(getitimer(arg1, &value));
3989
            if (!is_error(ret) && arg2) {
3990
                host_to_target_timeval(arg2,
3991
                                       &value.it_interval);
3992
                host_to_target_timeval(arg2 + sizeof(struct target_timeval),
3993
                                       &value.it_value);
3994
            }
3995
        }
3996
        break;
3997
    case TARGET_NR_stat:
3998
        if (!(p = lock_user_string(arg1)))
3999
            goto efault;
4000
        ret = get_errno(stat(path(p), &st));
4001
        unlock_user(p, arg1, 0);
4002
        goto do_stat;
4003
    case TARGET_NR_lstat:
4004
        if (!(p = lock_user_string(arg1)))
4005
            goto efault;
4006
        ret = get_errno(lstat(path(p), &st));
4007
        unlock_user(p, arg1, 0);
4008
        goto do_stat;
4009
    case TARGET_NR_fstat:
4010
        {
4011
            ret = get_errno(fstat(arg1, &st));
4012
        do_stat:
4013
            if (!is_error(ret)) {
4014
                struct target_stat *target_st;
4015

    
4016
                if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4017
                    goto efault;
4018
#if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32))
4019
                target_st->st_dev = tswap32(st.st_dev);
4020
#else
4021
                target_st->st_dev = tswap16(st.st_dev);
4022
#endif
4023
                target_st->st_ino = tswapl(st.st_ino);
4024
#if defined(TARGET_PPC) || defined(TARGET_MIPS)
4025
                target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
4026
                target_st->st_uid = tswap32(st.st_uid);
4027
                target_st->st_gid = tswap32(st.st_gid);
4028
#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
4029
                target_st->st_mode = tswap32(st.st_mode);
4030
                target_st->st_uid = tswap32(st.st_uid);
4031
                target_st->st_gid = tswap32(st.st_gid);
4032
#else
4033
                target_st->st_mode = tswap16(st.st_mode);
4034
                target_st->st_uid = tswap16(st.st_uid);
4035
                target_st->st_gid = tswap16(st.st_gid);
4036
#endif
4037
#if defined(TARGET_MIPS)
4038
                /* If this is the same on PPC, then just merge w/ the above ifdef */
4039
                target_st->st_nlink = tswapl(st.st_nlink);
4040
                target_st->st_rdev = tswapl(st.st_rdev);
4041
#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
4042
                target_st->st_nlink = tswap32(st.st_nlink);
4043
                target_st->st_rdev = tswap32(st.st_rdev);
4044
#else
4045
                target_st->st_nlink = tswap16(st.st_nlink);
4046
                target_st->st_rdev = tswap16(st.st_rdev);
4047
#endif
4048
                target_st->st_size = tswapl(st.st_size);
4049
                target_st->st_blksize = tswapl(st.st_blksize);
4050
                target_st->st_blocks = tswapl(st.st_blocks);
4051
                target_st->target_st_atime = tswapl(st.st_atime);
4052
                target_st->target_st_mtime = tswapl(st.st_mtime);
4053
                target_st->target_st_ctime = tswapl(st.st_ctime);
4054
                unlock_user_struct(target_st, arg2, 1);
4055
            }
4056
        }
4057
        break;
4058
#ifdef TARGET_NR_olduname
4059
    case TARGET_NR_olduname:
4060
        goto unimplemented;
4061
#endif
4062
#ifdef TARGET_NR_iopl
4063
    case TARGET_NR_iopl:
4064
        goto unimplemented;
4065
#endif
4066
    case TARGET_NR_vhangup:
4067
        ret = get_errno(vhangup());
4068
        break;
4069
#ifdef TARGET_NR_idle
4070
    case TARGET_NR_idle:
4071
        goto unimplemented;
4072
#endif
4073
#ifdef TARGET_NR_syscall
4074
    case TARGET_NR_syscall:
4075
            ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4076
            break;
4077
#endif
4078
    case TARGET_NR_wait4:
4079
        {
4080
            int status;
4081
            abi_long status_ptr = arg2;
4082
            struct rusage rusage, *rusage_ptr;
4083
            abi_ulong target_rusage = arg4;
4084
            if (target_rusage)
4085
                rusage_ptr = &rusage;
4086
            else
4087
                rusage_ptr = NULL;
4088
            ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4089
            if (!is_error(ret)) {
4090
                if (status_ptr)
4091
                    tputl(status_ptr, status);
4092
                if (target_rusage) {
4093
                    host_to_target_rusage(target_rusage, &rusage);
4094
                }
4095
            }
4096
        }
4097
        break;
4098
#ifdef TARGET_NR_swapoff
4099
    case TARGET_NR_swapoff:
4100
        if (!(p = lock_user_string(arg1)))
4101
            goto efault;
4102
        ret = get_errno(swapoff(p));
4103
        unlock_user(p, arg1, 0);
4104
        break;
4105
#endif
4106
    case TARGET_NR_sysinfo:
4107
        {
4108
            struct target_sysinfo *target_value;
4109
            struct sysinfo value;
4110
            ret = get_errno(sysinfo(&value));
4111
            if (!is_error(ret) && arg1)
4112
            {
4113
                if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4114
                    goto efault;
4115
                __put_user(value.uptime, &target_value->uptime);
4116
                __put_user(value.loads[0], &target_value->loads[0]);
4117
                __put_user(value.loads[1], &target_value->loads[1]);
4118
                __put_user(value.loads[2], &target_value->loads[2]);
4119
                __put_user(value.totalram, &target_value->totalram);
4120
                __put_user(value.freeram, &target_value->freeram);
4121
                __put_user(value.sharedram, &target_value->sharedram);
4122
                __put_user(value.bufferram, &target_value->bufferram);
4123
                __put_user(value.totalswap, &target_value->totalswap);
4124
                __put_user(value.freeswap, &target_value->freeswap);
4125
                __put_user(value.procs, &target_value->procs);
4126
                __put_user(value.totalhigh, &target_value->totalhigh);
4127
                __put_user(value.freehigh, &target_value->freehigh);
4128
                __put_user(value.mem_unit, &target_value->mem_unit);
4129
                unlock_user_struct(target_value, arg1, 1);
4130
            }
4131
        }
4132
        break;
4133
#ifdef TARGET_NR_ipc
4134
    case TARGET_NR_ipc:
4135
        ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4136
        break;
4137
#endif
4138
    case TARGET_NR_fsync:
4139
        ret = get_errno(fsync(arg1));
4140
        break;
4141
    case TARGET_NR_clone:
4142
        ret = get_errno(do_fork(cpu_env, arg1, arg2));
4143
        break;
4144
#ifdef __NR_exit_group
4145
        /* new thread calls */
4146
    case TARGET_NR_exit_group:
4147
        gdb_exit(cpu_env, arg1);
4148
        ret = get_errno(exit_group(arg1));
4149
        break;
4150
#endif
4151
    case TARGET_NR_setdomainname:
4152
        if (!(p = lock_user_string(arg1)))
4153
            goto efault;
4154
        ret = get_errno(setdomainname(p, arg2));
4155
        unlock_user(p, arg1, 0);
4156
        break;
4157
    case TARGET_NR_uname:
4158
        /* no need to transcode because we use the linux syscall */
4159
        {
4160
            struct new_utsname * buf;
4161

    
4162
            if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4163
                goto efault;
4164
            ret = get_errno(sys_uname(buf));
4165
            if (!is_error(ret)) {
4166
                /* Overrite the native machine name with whatever is being
4167
                   emulated. */
4168
                strcpy (buf->machine, UNAME_MACHINE);
4169
                /* Allow the user to override the reported release.  */
4170
                if (qemu_uname_release && *qemu_uname_release)
4171
                  strcpy (buf->release, qemu_uname_release);
4172
            }
4173
            unlock_user_struct(buf, arg1, 1);
4174
        }
4175
        break;
4176
#ifdef TARGET_I386
4177
    case TARGET_NR_modify_ldt:
4178
        ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
4179
        break;
4180
#if !defined(TARGET_X86_64)
4181
    case TARGET_NR_vm86old:
4182
        goto unimplemented;
4183
    case TARGET_NR_vm86:
4184
        ret = do_vm86(cpu_env, arg1, arg2);
4185
        break;
4186
#endif
4187
#endif
4188
    case TARGET_NR_adjtimex:
4189
        goto unimplemented;
4190
#ifdef TARGET_NR_create_module
4191
    case TARGET_NR_create_module:
4192
#endif
4193
    case TARGET_NR_init_module:
4194
    case TARGET_NR_delete_module:
4195
#ifdef TARGET_NR_get_kernel_syms
4196
    case TARGET_NR_get_kernel_syms:
4197
#endif
4198
        goto unimplemented;
4199
    case TARGET_NR_quotactl:
4200
        goto unimplemented;
4201
    case TARGET_NR_getpgid:
4202
        ret = get_errno(getpgid(arg1));
4203
        break;
4204
    case TARGET_NR_fchdir:
4205
        ret = get_errno(fchdir(arg1));
4206
        break;
4207
#ifdef TARGET_NR_bdflush /* not on x86_64 */
4208
    case TARGET_NR_bdflush:
4209
        goto unimplemented;
4210
#endif
4211
#ifdef TARGET_NR_sysfs
4212
    case TARGET_NR_sysfs:
4213
        goto unimplemented;
4214
#endif
4215
    case TARGET_NR_personality:
4216
        ret = get_errno(personality(arg1));
4217
        break;
4218
#ifdef TARGET_NR_afs_syscall
4219
    case TARGET_NR_afs_syscall:
4220
        goto unimplemented;
4221
#endif
4222
#ifdef TARGET_NR__llseek /* Not on alpha */
4223
    case TARGET_NR__llseek:
4224
        {
4225
#if defined (__x86_64__)
4226
            ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
4227
            tput64(arg4, ret);
4228
#else
4229
            int64_t res;
4230
            ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4231
            tput64(arg4, res);
4232
#endif
4233
        }
4234
        break;
4235
#endif
4236
    case TARGET_NR_getdents:
4237
#if TARGET_ABI_BITS != 32
4238
        goto unimplemented;
4239
#warning not supported
4240
#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4241
        {
4242
            struct target_dirent *target_dirp;
4243
            struct dirent *dirp;
4244
            abi_long count = arg3;
4245

    
4246
            dirp = malloc(count);
4247
            if (!dirp) {
4248
                ret = -TARGET_ENOMEM;
4249
                goto fail;
4250
            }
4251

    
4252
            ret = get_errno(sys_getdents(arg1, dirp, count));
4253
            if (!is_error(ret)) {
4254
                struct dirent *de;
4255
                struct target_dirent *tde;
4256
                int len = ret;
4257
                int reclen, treclen;
4258
                int count1, tnamelen;
4259

    
4260
                count1 = 0;
4261
                de = dirp;
4262
                if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4263
                    goto efault;
4264
                tde = target_dirp;
4265
                while (len > 0) {
4266
                    reclen = de->d_reclen;
4267
                    treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4268
                    tde->d_reclen = tswap16(treclen);
4269
                    tde->d_ino = tswapl(de->d_ino);
4270
                    tde->d_off = tswapl(de->d_off);
4271
                    tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4272
                    if (tnamelen > 256)
4273
                        tnamelen = 256;
4274
                    /* XXX: may not be correct */
4275
                    strncpy(tde->d_name, de->d_name, tnamelen);
4276
                    de = (struct dirent *)((char *)de + reclen);
4277
                    len -= reclen;
4278
                    tde = (struct target_dirent *)((char *)tde + treclen);
4279
                    count1 += treclen;
4280
                }
4281
                ret = count1;
4282
                unlock_user(target_dirp, arg2, ret);
4283
            }
4284
            free(dirp);
4285
        }
4286
#else
4287
        {
4288
            struct dirent *dirp;
4289
            abi_long count = arg3;
4290

    
4291
            if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4292
                goto efault;
4293
            ret = get_errno(sys_getdents(arg1, dirp, count));
4294
            if (!is_error(ret)) {
4295
                struct dirent *de;
4296
                int len = ret;
4297
                int reclen;
4298
                de = dirp;
4299
                while (len > 0) {
4300
                    reclen = de->d_reclen;
4301
                    if (reclen > len)
4302
                        break;
4303
                    de->d_reclen = tswap16(reclen);
4304
                    tswapls(&de->d_ino);
4305
                    tswapls(&de->d_off);
4306
                    de = (struct dirent *)((char *)de + reclen);
4307
                    len -= reclen;
4308
                }
4309
            }
4310
            unlock_user(dirp, arg2, ret);
4311
        }
4312
#endif
4313
        break;
4314
#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
4315
    case TARGET_NR_getdents64:
4316
        {
4317
            struct dirent64 *dirp;
4318
            abi_long count = arg3;
4319
            if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4320
                goto efault;
4321
            ret = get_errno(sys_getdents64(arg1, dirp, count));
4322
            if (!is_error(ret)) {
4323
                struct dirent64 *de;
4324
                int len = ret;
4325
                int reclen;
4326
                de = dirp;
4327
                while (len > 0) {
4328
                    reclen = de->d_reclen;
4329
                    if (reclen > len)
4330
                        break;
4331
                    de->d_reclen = tswap16(reclen);
4332
                    tswap64s(&de->d_ino);
4333
                    tswap64s(&de->d_off);
4334
                    de = (struct dirent64 *)((char *)de + reclen);
4335
                    len -= reclen;
4336
                }
4337
            }
4338
            unlock_user(dirp, arg2, ret);
4339
        }
4340
        break;
4341
#endif /* TARGET_NR_getdents64 */
4342
#ifdef TARGET_NR__newselect
4343
    case TARGET_NR__newselect:
4344
        ret = do_select(arg1, arg2, arg3, arg4, arg5);
4345
        break;
4346
#endif
4347
#ifdef TARGET_NR_poll
4348
    case TARGET_NR_poll:
4349
        {
4350
            struct target_pollfd *target_pfd;
4351
            unsigned int nfds = arg2;
4352
            int timeout = arg3;
4353
            struct pollfd *pfd;
4354
            unsigned int i;
4355

    
4356
            target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4357
            if (!target_pfd)
4358
                goto efault;
4359
            pfd = alloca(sizeof(struct pollfd) * nfds);
4360
            for(i = 0; i < nfds; i++) {
4361
                pfd[i].fd = tswap32(target_pfd[i].fd);
4362
                pfd[i].events = tswap16(target_pfd[i].events);
4363
            }
4364
            ret = get_errno(poll(pfd, nfds, timeout));
4365
            if (!is_error(ret)) {
4366
                for(i = 0; i < nfds; i++) {
4367
                    target_pfd[i].revents = tswap16(pfd[i].revents);
4368
                }
4369
                ret += nfds * (sizeof(struct target_pollfd)
4370
                               - sizeof(struct pollfd));
4371
            }
4372
            unlock_user(target_pfd, arg1, ret);
4373
        }
4374
        break;
4375
#endif
4376
    case TARGET_NR_flock:
4377
        /* NOTE: the flock constant seems to be the same for every
4378
           Linux platform */
4379
        ret = get_errno(flock(arg1, arg2));
4380
        break;
4381
    case TARGET_NR_readv:
4382
        {
4383
            int count = arg3;
4384
            struct iovec *vec;
4385

    
4386
            vec = alloca(count * sizeof(struct iovec));
4387
            lock_iovec(VERIFY_WRITE, vec, arg2, count, 0);
4388
            ret = get_errno(readv(arg1, vec, count));
4389
            unlock_iovec(vec, arg2, count, 1);
4390
        }
4391
        break;
4392
    case TARGET_NR_writev:
4393
        {
4394
            int count = arg3;
4395
            struct iovec *vec;
4396

    
4397
            vec = alloca(count * sizeof(struct iovec));
4398
            lock_iovec(VERIFY_READ, vec, arg2, count, 1);
4399
            ret = get_errno(writev(arg1, vec, count));
4400
            unlock_iovec(vec, arg2, count, 0);
4401
        }
4402
        break;
4403
    case TARGET_NR_getsid:
4404
        ret = get_errno(getsid(arg1));
4405
        break;
4406
#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
4407
    case TARGET_NR_fdatasync:
4408
        ret = get_errno(fdatasync(arg1));
4409
        break;
4410
#endif
4411
    case TARGET_NR__sysctl:
4412
        /* We don't implement this, but ENOTDIR is always a safe
4413
           return value. */
4414
        ret = -TARGET_ENOTDIR;
4415
        break;
4416
    case TARGET_NR_sched_setparam:
4417
        {
4418
            struct sched_param *target_schp;
4419
            struct sched_param schp;
4420

    
4421
            if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4422
                goto efault;
4423
            schp.sched_priority = tswap32(target_schp->sched_priority);
4424
            unlock_user_struct(target_schp, arg2, 0);
4425
            ret = get_errno(sched_setparam(arg1, &schp));
4426
        }
4427
        break;
4428
    case TARGET_NR_sched_getparam:
4429
        {
4430
            struct sched_param *target_schp;
4431
            struct sched_param schp;
4432
            ret = get_errno(sched_getparam(arg1, &schp));
4433
            if (!is_error(ret)) {
4434
                if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4435
                    goto efault;
4436
                target_schp->sched_priority = tswap32(schp.sched_priority);
4437
                unlock_user_struct(target_schp, arg2, 1);
4438
            }
4439
        }
4440
        break;
4441
    case TARGET_NR_sched_setscheduler:
4442
        {
4443
            struct sched_param *target_schp;
4444
            struct sched_param schp;
4445
            if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4446
                goto efault;
4447
            schp.sched_priority = tswap32(target_schp->sched_priority);
4448
            unlock_user_struct(target_schp, arg3, 0);
4449
            ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4450
        }
4451
        break;
4452
    case TARGET_NR_sched_getscheduler:
4453
        ret = get_errno(sched_getscheduler(arg1));
4454
        break;
4455
    case TARGET_NR_sched_yield:
4456
        ret = get_errno(sched_yield());
4457
        break;
4458
    case TARGET_NR_sched_get_priority_max:
4459
        ret = get_errno(sched_get_priority_max(arg1));
4460
        break;
4461
    case TARGET_NR_sched_get_priority_min:
4462
        ret = get_errno(sched_get_priority_min(arg1));
4463
        break;
4464
    case TARGET_NR_sched_rr_get_interval:
4465
        {
4466
            struct timespec ts;
4467
            ret = get_errno(sched_rr_get_interval(arg1, &ts));
4468
            if (!is_error(ret)) {
4469
                host_to_target_timespec(arg2, &ts);
4470
            }
4471
        }
4472
        break;
4473
    case TARGET_NR_nanosleep:
4474
        {
4475
            struct timespec req, rem;
4476
            target_to_host_timespec(&req, arg1);
4477
            ret = get_errno(nanosleep(&req, &rem));
4478
            if (is_error(ret) && arg2) {
4479
                host_to_target_timespec(arg2, &rem);
4480
            }
4481
        }
4482
        break;
4483
#ifdef TARGET_NR_query_module
4484
    case TARGET_NR_query_module:
4485
        goto unimplemented;
4486
#endif
4487
#ifdef TARGET_NR_nfsservctl
4488
    case TARGET_NR_nfsservctl:
4489
        goto unimplemented;
4490
#endif
4491
    case TARGET_NR_prctl:
4492
        switch (arg1)
4493
            {
4494
            case PR_GET_PDEATHSIG:
4495
                {
4496
                    int deathsig;
4497
                    ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4498
                    if (!is_error(ret) && arg2)
4499
                        tput32(arg2, deathsig);
4500
                }
4501
                break;
4502
            default:
4503
                ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4504
                break;
4505
            }
4506
        break;
4507
#ifdef TARGET_NR_pread
4508
    case TARGET_NR_pread:
4509
        page_unprotect_range(arg2, arg3);
4510
        if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4511
            goto efault;
4512
        ret = get_errno(pread(arg1, p, arg3, arg4));
4513
        unlock_user(p, arg2, ret);
4514
        break;
4515
    case TARGET_NR_pwrite:
4516
        if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4517
            goto efault;
4518
        ret = get_errno(pwrite(arg1, p, arg3, arg4));
4519
        unlock_user(p, arg2, 0);
4520
        break;
4521
#endif
4522
    case TARGET_NR_getcwd:
4523
        if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4524
            goto efault;
4525
        ret = get_errno(sys_getcwd1(p, arg2));
4526
        unlock_user(p, arg1, ret);
4527
        break;
4528
    case TARGET_NR_capget:
4529
        goto unimplemented;
4530
    case TARGET_NR_capset:
4531
        goto unimplemented;
4532
    case TARGET_NR_sigaltstack:
4533
#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4534
    defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
4535
        ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
4536
        break;
4537
#else
4538
        goto unimplemented;
4539
#endif
4540
    case TARGET_NR_sendfile:
4541
        goto unimplemented;
4542
#ifdef TARGET_NR_getpmsg
4543
    case TARGET_NR_getpmsg:
4544
        goto unimplemented;
4545
#endif
4546
#ifdef TARGET_NR_putpmsg
4547
    case TARGET_NR_putpmsg:
4548
        goto unimplemented;
4549
#endif
4550
#ifdef TARGET_NR_vfork
4551
    case TARGET_NR_vfork:
4552
        ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
4553
        break;
4554
#endif
4555
#ifdef TARGET_NR_ugetrlimit
4556
    case TARGET_NR_ugetrlimit:
4557
    {
4558
        struct rlimit rlim;
4559
        ret = get_errno(getrlimit(arg1, &rlim));
4560
        if (!is_error(ret)) {
4561
            struct target_rlimit *target_rlim;
4562
            if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4563
                goto efault;
4564
            target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4565
            target_rlim->rlim_max = tswapl(rlim.rlim_max);
4566
            unlock_user_struct(target_rlim, arg2, 1);
4567
        }
4568
        break;
4569
    }
4570
#endif
4571
#ifdef TARGET_NR_truncate64
4572
    case TARGET_NR_truncate64:
4573
        if (!(p = lock_user_string(arg1)))
4574
            goto efault;
4575
        ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4576
        unlock_user(p, arg1, 0);
4577
        break;
4578
#endif
4579
#ifdef TARGET_NR_ftruncate64
4580
    case TARGET_NR_ftruncate64:
4581
        ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
4582
        break;
4583
#endif
4584
#ifdef TARGET_NR_stat64
4585
    case TARGET_NR_stat64:
4586
        if (!(p = lock_user_string(arg1)))
4587
            goto efault;
4588
        ret = get_errno(stat(path(p), &st));
4589
        unlock_user(p, arg1, 0);
4590
        goto do_stat64;
4591
#endif
4592
#ifdef TARGET_NR_lstat64
4593
    case TARGET_NR_lstat64:
4594
        if (!(p = lock_user_string(arg1)))
4595
            goto efault;
4596
        ret = get_errno(lstat(path(p), &st));
4597
        unlock_user(p, arg1, 0);
4598
        goto do_stat64;
4599
#endif
4600
#ifdef TARGET_NR_fstat64
4601
    case TARGET_NR_fstat64:
4602
        {
4603
            ret = get_errno(fstat(arg1, &st));
4604
        do_stat64:
4605
            if (!is_error(ret)) {
4606
#ifdef TARGET_ARM
4607
                if (((CPUARMState *)cpu_env)->eabi) {
4608
                    struct target_eabi_stat64 *target_st;
4609

    
4610
                    if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4611
                        goto efault;
4612
                    memset(target_st, 0, sizeof(struct target_eabi_stat64));
4613
                    __put_user(st.st_dev, &target_st->st_dev);
4614
                    __put_user(st.st_ino, &target_st->st_ino);
4615
#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4616
                    __put_user(st.st_ino, &target_st->__st_ino);
4617
#endif
4618
                    __put_user(st.st_mode, &target_st->st_mode);
4619
                    __put_user(st.st_nlink, &target_st->st_nlink);
4620
                    __put_user(st.st_uid, &target_st->st_uid);
4621
                    __put_user(st.st_gid, &target_st->st_gid);
4622
                    __put_user(st.st_rdev, &target_st->st_rdev);
4623
                    __put_user(st.st_size, &target_st->st_size);
4624
                    __put_user(st.st_blksize, &target_st->st_blksize);
4625
                    __put_user(st.st_blocks, &target_st->st_blocks);
4626
                    __put_user(st.st_atime, &target_st->target_st_atime);
4627
                    __put_user(st.st_mtime, &target_st->target_st_mtime);
4628
                    __put_user(st.st_ctime, &target_st->target_st_ctime);
4629
                    unlock_user_struct(target_st, arg2, 1);
4630
                } else
4631
#endif
4632
                {
4633
                    struct target_stat64 *target_st;
4634

    
4635
                    if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4636
                        goto efault;
4637
                    memset(target_st, 0, sizeof(struct target_stat64));
4638
                    __put_user(st.st_dev, &target_st->st_dev);
4639
                    __put_user(st.st_ino, &target_st->st_ino);
4640
#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4641
                    __put_user(st.st_ino, &target_st->__st_ino);
4642
#endif
4643
                    __put_user(st.st_mode, &target_st->st_mode);
4644
                    __put_user(st.st_nlink, &target_st->st_nlink);
4645
                    __put_user(st.st_uid, &target_st->st_uid);
4646
                    __put_user(st.st_gid, &target_st->st_gid);
4647
                    __put_user(st.st_rdev, &target_st->st_rdev);
4648
                    /* XXX: better use of kernel struct */
4649
                    __put_user(st.st_size, &target_st->st_size);
4650
                    __put_user(st.st_blksize, &target_st->st_blksize);
4651
                    __put_user(st.st_blocks, &target_st->st_blocks);
4652
                    __put_user(st.st_atime, &target_st->target_st_atime);
4653
                    __put_user(st.st_mtime, &target_st->target_st_mtime);
4654
                    __put_user(st.st_ctime, &target_st->target_st_ctime);
4655
                    unlock_user_struct(target_st, arg2, 1);
4656
                }
4657
            }
4658
        }
4659
        break;
4660
#endif
4661
#ifdef USE_UID16
4662
    case TARGET_NR_lchown:
4663
        if (!(p = lock_user_string(arg1)))
4664
            goto efault;
4665
        ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4666
        unlock_user(p, arg1, 0);
4667
        break;
4668
    case TARGET_NR_getuid:
4669
        ret = get_errno(high2lowuid(getuid()));
4670
        break;
4671
    case TARGET_NR_getgid:
4672
        ret = get_errno(high2lowgid(getgid()));
4673
        break;
4674
    case TARGET_NR_geteuid:
4675
        ret = get_errno(high2lowuid(geteuid()));
4676
        break;
4677
    case TARGET_NR_getegid:
4678
        ret = get_errno(high2lowgid(getegid()));
4679
        break;
4680
    case TARGET_NR_setreuid:
4681
        ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4682
        break;
4683
    case TARGET_NR_setregid:
4684
        ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4685
        break;
4686
    case TARGET_NR_getgroups:
4687
        {
4688
            int gidsetsize = arg1;
4689
            uint16_t *target_grouplist;
4690
            gid_t *grouplist;
4691
            int i;
4692

    
4693
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4694
            ret = get_errno(getgroups(gidsetsize, grouplist));
4695
            if (!is_error(ret)) {
4696
                target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
4697
                if (!target_grouplist)
4698
                    goto efault;
4699
                for(i = 0;i < gidsetsize; i++)
4700
                    target_grouplist[i] = tswap16(grouplist[i]);
4701
                unlock_user(target_grouplist, arg2, gidsetsize * 2);
4702
            }
4703
        }
4704
        break;
4705
    case TARGET_NR_setgroups:
4706
        {
4707
            int gidsetsize = arg1;
4708
            uint16_t *target_grouplist;
4709
            gid_t *grouplist;
4710
            int i;
4711

    
4712
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4713
            target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
4714
            if (!target_grouplist) {
4715
                ret = -TARGET_EFAULT;
4716
                goto fail;
4717
            }
4718
            for(i = 0;i < gidsetsize; i++)
4719
                grouplist[i] = tswap16(target_grouplist[i]);
4720
            unlock_user(target_grouplist, arg2, 0);
4721
            ret = get_errno(setgroups(gidsetsize, grouplist));
4722
        }
4723
        break;
4724
    case TARGET_NR_fchown:
4725
        ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4726
        break;
4727
#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4728
    case TARGET_NR_fchownat:
4729
        if (!(p = lock_user_string(arg2))) 
4730
            goto efault;
4731
        ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4732
        unlock_user(p, arg2, 0);
4733
        break;
4734
#endif
4735
#ifdef TARGET_NR_setresuid
4736
    case TARGET_NR_setresuid:
4737
        ret = get_errno(setresuid(low2highuid(arg1),
4738
                                  low2highuid(arg2),
4739
                                  low2highuid(arg3)));
4740
        break;
4741
#endif
4742
#ifdef TARGET_NR_getresuid
4743
    case TARGET_NR_getresuid:
4744
        {
4745
            uid_t ruid, euid, suid;
4746
            ret = get_errno(getresuid(&ruid, &euid, &suid));
4747
            if (!is_error(ret)) {
4748
                tput16(arg1, tswap16(high2lowuid(ruid)));
4749
                tput16(arg2, tswap16(high2lowuid(euid)));
4750
                tput16(arg3, tswap16(high2lowuid(suid)));
4751
            }
4752
        }
4753
        break;
4754
#endif
4755
#ifdef TARGET_NR_getresgid
4756
    case TARGET_NR_setresgid:
4757
        ret = get_errno(setresgid(low2highgid(arg1),
4758
                                  low2highgid(arg2),
4759
                                  low2highgid(arg3)));
4760
        break;
4761
#endif
4762
#ifdef TARGET_NR_getresgid
4763
    case TARGET_NR_getresgid:
4764
        {
4765
            gid_t rgid, egid, sgid;
4766
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
4767
            if (!is_error(ret)) {
4768
                tput16(arg1, tswap16(high2lowgid(rgid)));
4769
                tput16(arg2, tswap16(high2lowgid(egid)));
4770
                tput16(arg3, tswap16(high2lowgid(sgid)));
4771
            }
4772
        }
4773
        break;
4774
#endif
4775
    case TARGET_NR_chown:
4776
        if (!(p = lock_user_string(arg1)))
4777
            goto efault;
4778
        ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4779
        unlock_user(p, arg1, 0);
4780
        break;
4781
    case TARGET_NR_setuid:
4782
        ret = get_errno(setuid(low2highuid(arg1)));
4783
        break;
4784
    case TARGET_NR_setgid:
4785
        ret = get_errno(setgid(low2highgid(arg1)));
4786
        break;
4787
    case TARGET_NR_setfsuid:
4788
        ret = get_errno(setfsuid(arg1));
4789
        break;
4790
    case TARGET_NR_setfsgid:
4791
        ret = get_errno(setfsgid(arg1));
4792
        break;
4793
#endif /* USE_UID16 */
4794

    
4795
#ifdef TARGET_NR_lchown32
4796
    case TARGET_NR_lchown32:
4797
        if (!(p = lock_user_string(arg1)))
4798
            goto efault;
4799
        ret = get_errno(lchown(p, arg2, arg3));
4800
        unlock_user(p, arg1, 0);
4801
        break;
4802
#endif
4803
#ifdef TARGET_NR_getuid32
4804
    case TARGET_NR_getuid32:
4805
        ret = get_errno(getuid());
4806
        break;
4807
#endif
4808
#ifdef TARGET_NR_getgid32
4809
    case TARGET_NR_getgid32:
4810
        ret = get_errno(getgid());
4811
        break;
4812
#endif
4813
#ifdef TARGET_NR_geteuid32
4814
    case TARGET_NR_geteuid32:
4815
        ret = get_errno(geteuid());
4816
        break;
4817
#endif
4818
#ifdef TARGET_NR_getegid32
4819
    case TARGET_NR_getegid32:
4820
        ret = get_errno(getegid());
4821
        break;
4822
#endif
4823
#ifdef TARGET_NR_setreuid32
4824
    case TARGET_NR_setreuid32:
4825
        ret = get_errno(setreuid(arg1, arg2));
4826
        break;
4827
#endif
4828
#ifdef TARGET_NR_setregid32
4829
    case TARGET_NR_setregid32:
4830
        ret = get_errno(setregid(arg1, arg2));
4831
        break;
4832
#endif
4833
#ifdef TARGET_NR_getgroups32
4834
    case TARGET_NR_getgroups32:
4835
        {
4836
            int gidsetsize = arg1;
4837
            uint32_t *target_grouplist;
4838
            gid_t *grouplist;
4839
            int i;
4840

    
4841
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4842
            ret = get_errno(getgroups(gidsetsize, grouplist));
4843
            if (!is_error(ret)) {
4844
                target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
4845
                if (!target_grouplist) {
4846
                    ret = -TARGET_EFAULT;
4847
                    goto fail;
4848
                }
4849
                for(i = 0;i < gidsetsize; i++)
4850
                    target_grouplist[i] = tswap32(grouplist[i]);
4851
                unlock_user(target_grouplist, arg2, gidsetsize * 4);
4852
            }
4853
        }
4854
        break;
4855
#endif
4856
#ifdef TARGET_NR_setgroups32
4857
    case TARGET_NR_setgroups32:
4858
        {
4859
            int gidsetsize = arg1;
4860
            uint32_t *target_grouplist;
4861
            gid_t *grouplist;
4862
            int i;
4863

    
4864
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4865
            target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
4866
            if (!target_grouplist) {
4867
                ret = -TARGET_EFAULT;
4868
                goto fail;
4869
            }
4870
            for(i = 0;i < gidsetsize; i++)
4871
                grouplist[i] = tswap32(target_grouplist[i]);
4872
            unlock_user(target_grouplist, arg2, 0);
4873
            ret = get_errno(setgroups(gidsetsize, grouplist));
4874
        }
4875
        break;
4876
#endif
4877
#ifdef TARGET_NR_fchown32
4878
    case TARGET_NR_fchown32:
4879
        ret = get_errno(fchown(arg1, arg2, arg3));
4880
        break;
4881
#endif
4882
#ifdef TARGET_NR_setresuid32
4883
    case TARGET_NR_setresuid32:
4884
        ret = get_errno(setresuid(arg1, arg2, arg3));
4885
        break;
4886
#endif
4887
#ifdef TARGET_NR_getresuid32
4888
    case TARGET_NR_getresuid32:
4889
        {
4890
            uid_t ruid, euid, suid;
4891
            ret = get_errno(getresuid(&ruid, &euid, &suid));
4892
            if (!is_error(ret)) {
4893
                tput32(arg1, tswap32(ruid));
4894
                tput32(arg2, tswap32(euid));
4895
                tput32(arg3, tswap32(suid));
4896
            }
4897
        }
4898
        break;
4899
#endif
4900
#ifdef TARGET_NR_setresgid32
4901
    case TARGET_NR_setresgid32:
4902
        ret = get_errno(setresgid(arg1, arg2, arg3));
4903
        break;
4904
#endif
4905
#ifdef TARGET_NR_getresgid32
4906
    case TARGET_NR_getresgid32:
4907
        {
4908
            gid_t rgid, egid, sgid;
4909
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
4910
            if (!is_error(ret)) {
4911
                tput32(arg1, tswap32(rgid));
4912
                tput32(arg2, tswap32(egid));
4913
                tput32(arg3, tswap32(sgid));
4914
            }
4915
        }
4916
        break;
4917
#endif
4918
#ifdef TARGET_NR_chown32
4919
    case TARGET_NR_chown32:
4920
        if (!(p = lock_user_string(arg1)))
4921
            goto efault;
4922
        ret = get_errno(chown(p, arg2, arg3));
4923
        unlock_user(p, arg1, 0);
4924
        break;
4925
#endif
4926
#ifdef TARGET_NR_setuid32
4927
    case TARGET_NR_setuid32:
4928
        ret = get_errno(setuid(arg1));
4929
        break;
4930
#endif
4931
#ifdef TARGET_NR_setgid32
4932
    case TARGET_NR_setgid32:
4933
        ret = get_errno(setgid(arg1));
4934
        break;
4935
#endif
4936
#ifdef TARGET_NR_setfsuid32
4937
    case TARGET_NR_setfsuid32:
4938
        ret = get_errno(setfsuid(arg1));
4939
        break;
4940
#endif
4941
#ifdef TARGET_NR_setfsgid32
4942
    case TARGET_NR_setfsgid32:
4943
        ret = get_errno(setfsgid(arg1));
4944
        break;
4945
#endif
4946

    
4947
    case TARGET_NR_pivot_root:
4948
        goto unimplemented;
4949
#ifdef TARGET_NR_mincore
4950
    case TARGET_NR_mincore:
4951
        goto unimplemented;
4952
#endif
4953
#ifdef TARGET_NR_madvise
4954
    case TARGET_NR_madvise:
4955
        /* A straight passthrough may not be safe because qemu sometimes
4956
           turns private flie-backed mappings into anonymous mappings.
4957
           This will break MADV_DONTNEED.
4958
           This is a hint, so ignoring and returning success is ok.  */
4959
        ret = get_errno(0);
4960
        break;
4961
#endif
4962
#if TARGET_ABI_BITS == 32
4963
    case TARGET_NR_fcntl64:
4964
    {
4965
        int cmd;
4966
        struct flock64 fl;
4967
        struct target_flock64 *target_fl;
4968
#ifdef TARGET_ARM
4969
        struct target_eabi_flock64 *target_efl;
4970
#endif
4971

    
4972
        switch(arg2){
4973
        case TARGET_F_GETLK64:
4974
            cmd = F_GETLK64;
4975
            break;
4976
        case TARGET_F_SETLK64:
4977
            cmd = F_SETLK64;
4978
            break;
4979
        case TARGET_F_SETLKW64:
4980
            cmd = F_SETLK64;
4981
            break;
4982
        default:
4983
            cmd = arg2;
4984
            break;
4985
        }
4986

    
4987
        switch(arg2) {
4988
        case TARGET_F_GETLK64:
4989
#ifdef TARGET_ARM
4990
            if (((CPUARMState *)cpu_env)->eabi) {
4991
                if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1)) {
4992
                    ret = -TARGET_EFAULT;
4993
                    goto fail;
4994
                }
4995
                fl.l_type = tswap16(target_efl->l_type);
4996
                fl.l_whence = tswap16(target_efl->l_whence);
4997
                fl.l_start = tswap64(target_efl->l_start);
4998
                fl.l_len = tswap64(target_efl->l_len);
4999
                fl.l_pid = tswapl(target_efl->l_pid);