Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ b92c47c1

History | View | Annotate | Download (155.9 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 void host_to_target_rusage(abi_ulong target_addr,
508
                                         const struct rusage *rusage)
509
{
510
    struct target_rusage *target_rusage;
511

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

    
534
static inline void target_to_host_timeval(struct timeval *tv,
535
                                          abi_ulong target_addr)
536
{
537
    struct target_timeval *target_tv;
538

    
539
    lock_user_struct(target_tv, target_addr, 1);
540
    tv->tv_sec = tswapl(target_tv->tv_sec);
541
    tv->tv_usec = tswapl(target_tv->tv_usec);
542
    unlock_user_struct(target_tv, target_addr, 0);
543
}
544

    
545
static inline void host_to_target_timeval(abi_ulong target_addr,
546
                                          const struct timeval *tv)
547
{
548
    struct target_timeval *target_tv;
549

    
550
    lock_user_struct(target_tv, target_addr, 0);
551
    target_tv->tv_sec = tswapl(tv->tv_sec);
552
    target_tv->tv_usec = tswapl(tv->tv_usec);
553
    unlock_user_struct(target_tv, target_addr, 1);
554
}
555

    
556

    
557
/* do_select() must return target values and target errnos. */
558
static abi_long do_select(int n,
559
                          abi_ulong rfd_p, abi_ulong wfd_p,
560
                          abi_ulong efd_p, abi_ulong target_tv)
561
{
562
    fd_set rfds, wfds, efds;
563
    fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
564
    abi_long *target_rfds, *target_wfds, *target_efds;
565
    struct timeval tv, *tv_ptr;
566
    abi_long ret;
567
    int ok;
568

    
569
    if (rfd_p) {
570
        target_rfds = lock_user(rfd_p, sizeof(abi_long) * n, 1);
571
        rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
572
    } else {
573
        target_rfds = NULL;
574
        rfds_ptr = NULL;
575
    }
576
    if (wfd_p) {
577
        target_wfds = lock_user(wfd_p, sizeof(abi_long) * n, 1);
578
        wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
579
    } else {
580
        target_wfds = NULL;
581
        wfds_ptr = NULL;
582
    }
583
    if (efd_p) {
584
        target_efds = lock_user(efd_p, sizeof(abi_long) * n, 1);
585
        efds_ptr = target_to_host_fds(&efds, target_efds, n);
586
    } else {
587
        target_efds = NULL;
588
        efds_ptr = NULL;
589
    }
590

    
591
    if (target_tv) {
592
        target_to_host_timeval(&tv, target_tv);
593
        tv_ptr = &tv;
594
    } else {
595
        tv_ptr = NULL;
596
    }
597
    ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
598
    ok = !is_error(ret);
599

    
600
    if (ok) {
601
        host_to_target_fds(target_rfds, rfds_ptr, n);
602
        host_to_target_fds(target_wfds, wfds_ptr, n);
603
        host_to_target_fds(target_efds, efds_ptr, n);
604

    
605
        if (target_tv) {
606
            host_to_target_timeval(target_tv, &tv);
607
        }
608
    }
609
    if (target_rfds)
610
        unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0);
611
    if (target_wfds)
612
        unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0);
613
    if (target_efds)
614
        unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0);
615

    
616
    return ret;
617
}
618

    
619
static inline void target_to_host_sockaddr(struct sockaddr *addr,
620
                                           abi_ulong target_addr,
621
                                           socklen_t len)
622
{
623
    struct target_sockaddr *target_saddr;
624

    
625
    target_saddr = lock_user(target_addr, len, 1);
626
    memcpy(addr, target_saddr, len);
627
    addr->sa_family = tswap16(target_saddr->sa_family);
628
    unlock_user(target_saddr, target_addr, 0);
629
}
630

    
631
static inline void host_to_target_sockaddr(abi_ulong target_addr,
632
                                           struct sockaddr *addr,
633
                                           socklen_t len)
634
{
635
    struct target_sockaddr *target_saddr;
636

    
637
    target_saddr = lock_user(target_addr, len, 0);
638
    memcpy(target_saddr, addr, len);
639
    target_saddr->sa_family = tswap16(addr->sa_family);
640
    unlock_user(target_saddr, target_addr, len);
641
}
642

    
643
/* ??? Should this also swap msgh->name?  */
644
static inline void target_to_host_cmsg(struct msghdr *msgh,
645
                                       struct target_msghdr *target_msgh)
646
{
647
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
648
    struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
649
    socklen_t space = 0;
650

    
651
    while (cmsg && target_cmsg) {
652
        void *data = CMSG_DATA(cmsg);
653
        void *target_data = TARGET_CMSG_DATA(target_cmsg);
654

    
655
        int len = tswapl(target_cmsg->cmsg_len)
656
                  - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
657

    
658
        space += CMSG_SPACE(len);
659
        if (space > msgh->msg_controllen) {
660
            space -= CMSG_SPACE(len);
661
            gemu_log("Host cmsg overflow\n");
662
            break;
663
        }
664

    
665
        cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
666
        cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
667
        cmsg->cmsg_len = CMSG_LEN(len);
668

    
669
        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
670
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
671
            memcpy(data, target_data, len);
672
        } else {
673
            int *fd = (int *)data;
674
            int *target_fd = (int *)target_data;
675
            int i, numfds = len / sizeof(int);
676

    
677
            for (i = 0; i < numfds; i++)
678
                fd[i] = tswap32(target_fd[i]);
679
        }
680

    
681
        cmsg = CMSG_NXTHDR(msgh, cmsg);
682
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
683
    }
684

    
685
    msgh->msg_controllen = space;
686
}
687

    
688
/* ??? Should this also swap msgh->name?  */
689
static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
690
                                       struct msghdr *msgh)
691
{
692
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
693
    struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
694
    socklen_t space = 0;
695

    
696
    while (cmsg && target_cmsg) {
697
        void *data = CMSG_DATA(cmsg);
698
        void *target_data = TARGET_CMSG_DATA(target_cmsg);
699

    
700
        int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
701

    
702
        space += TARGET_CMSG_SPACE(len);
703
        if (space > tswapl(target_msgh->msg_controllen)) {
704
            space -= TARGET_CMSG_SPACE(len);
705
            gemu_log("Target cmsg overflow\n");
706
            break;
707
        }
708

    
709
        target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
710
        target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
711
        target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
712

    
713
        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
714
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
715
            memcpy(target_data, data, len);
716
        } else {
717
            int *fd = (int *)data;
718
            int *target_fd = (int *)target_data;
719
            int i, numfds = len / sizeof(int);
720

    
721
            for (i = 0; i < numfds; i++)
722
                target_fd[i] = tswap32(fd[i]);
723
        }
724

    
725
        cmsg = CMSG_NXTHDR(msgh, cmsg);
726
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
727
    }
728

    
729
    msgh->msg_controllen = tswapl(space);
730
}
731

    
732
/* do_setsockopt() Must return target values and target errnos. */
733
static abi_long do_setsockopt(int sockfd, int level, int optname,
734
                              abi_ulong optval, socklen_t optlen)
735
{
736
    abi_long ret;
737
    int val;
738

    
739
    switch(level) {
740
    case SOL_TCP:
741
        /* TCP options all take an 'int' value.  */
742
        if (optlen < sizeof(uint32_t))
743
            return -TARGET_EINVAL;
744

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

    
843
        val = tget32(optval);
844
        ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
845
        break;
846
    default:
847
    unimplemented:
848
        gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
849
        ret = -TARGET_ENOSYS;
850
    }
851
    return ret;
852
}
853

    
854
/* do_getsockopt() Must return target values and target errnos. */
855
static abi_long do_getsockopt(int sockfd, int level, int optname,
856
                              abi_ulong optval, abi_ulong optlen)
857
{
858
    abi_long ret;
859
    int len, lv, val;
860

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

    
944
static void lock_iovec(struct iovec *vec, abi_ulong target_addr,
945
                       int count, int copy)
946
{
947
    struct target_iovec *target_vec;
948
    abi_ulong base;
949
    int i;
950

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

    
960
static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
961
                         int count, int copy)
962
{
963
    struct target_iovec *target_vec;
964
    abi_ulong base;
965
    int i;
966

    
967
    target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
968
    for(i = 0;i < count; i++) {
969
        base = tswapl(target_vec[i].iov_base);
970
        unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
971
    }
972
    unlock_user (target_vec, target_addr, 0);
973
}
974

    
975
/* do_socket() Must return target values and target errnos. */
976
static abi_long do_socket(int domain, int type, int protocol)
977
{
978
#if defined(TARGET_MIPS)
979
    switch(type) {
980
    case TARGET_SOCK_DGRAM:
981
        type = SOCK_DGRAM;
982
        break;
983
    case TARGET_SOCK_STREAM:
984
        type = SOCK_STREAM;
985
        break;
986
    case TARGET_SOCK_RAW:
987
        type = SOCK_RAW;
988
        break;
989
    case TARGET_SOCK_RDM:
990
        type = SOCK_RDM;
991
        break;
992
    case TARGET_SOCK_SEQPACKET:
993
        type = SOCK_SEQPACKET;
994
        break;
995
    case TARGET_SOCK_PACKET:
996
        type = SOCK_PACKET;
997
        break;
998
    }
999
#endif
1000
    if (domain == PF_NETLINK)
1001
        return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1002
    return get_errno(socket(domain, type, protocol));
1003
}
1004

    
1005
/* do_bind() Must return target values and target errnos. */
1006
static abi_long do_bind(int sockfd, abi_ulong target_addr,
1007
                        socklen_t addrlen)
1008
{
1009
    void *addr = alloca(addrlen);
1010

    
1011
    target_to_host_sockaddr(addr, target_addr, addrlen);
1012
    return get_errno(bind(sockfd, addr, addrlen));
1013
}
1014

    
1015
/* do_connect() Must return target values and target errnos. */
1016
static abi_long do_connect(int sockfd, abi_ulong target_addr,
1017
                           socklen_t addrlen)
1018
{
1019
    void *addr = alloca(addrlen);
1020

    
1021
    target_to_host_sockaddr(addr, target_addr, addrlen);
1022
    return get_errno(connect(sockfd, addr, addrlen));
1023
}
1024

    
1025
/* do_sendrecvmsg() Must return target values and target errnos. */
1026
static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1027
                               int flags, int send)
1028
{
1029
    abi_long ret;
1030
    struct target_msghdr *msgp;
1031
    struct msghdr msg;
1032
    int count;
1033
    struct iovec *vec;
1034
    abi_ulong target_vec;
1035

    
1036
    lock_user_struct(msgp, target_msg, 1);
1037
    if (msgp->msg_name) {
1038
        msg.msg_namelen = tswap32(msgp->msg_namelen);
1039
        msg.msg_name = alloca(msg.msg_namelen);
1040
        target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1041
                                msg.msg_namelen);
1042
    } else {
1043
        msg.msg_name = NULL;
1044
        msg.msg_namelen = 0;
1045
    }
1046
    msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1047
    msg.msg_control = alloca(msg.msg_controllen);
1048
    msg.msg_flags = tswap32(msgp->msg_flags);
1049

    
1050
    count = tswapl(msgp->msg_iovlen);
1051
    vec = alloca(count * sizeof(struct iovec));
1052
    target_vec = tswapl(msgp->msg_iov);
1053
    lock_iovec(vec, target_vec, count, send);
1054
    msg.msg_iovlen = count;
1055
    msg.msg_iov = vec;
1056

    
1057
    if (send) {
1058
        target_to_host_cmsg(&msg, msgp);
1059
        ret = get_errno(sendmsg(fd, &msg, flags));
1060
    } else {
1061
        ret = get_errno(recvmsg(fd, &msg, flags));
1062
        if (!is_error(ret))
1063
            host_to_target_cmsg(msgp, &msg);
1064
    }
1065
    unlock_iovec(vec, target_vec, count, !send);
1066
    return ret;
1067
}
1068

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

    
1077
    ret = get_errno(accept(fd, addr, &addrlen));
1078
    if (!is_error(ret)) {
1079
        host_to_target_sockaddr(target_addr, addr, addrlen);
1080
        tput32(target_addrlen, addrlen);
1081
    }
1082
    return ret;
1083
}
1084

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

    
1093
    ret = get_errno(getpeername(fd, addr, &addrlen));
1094
    if (!is_error(ret)) {
1095
        host_to_target_sockaddr(target_addr, addr, addrlen);
1096
        tput32(target_addrlen, addrlen);
1097
    }
1098
    return ret;
1099
}
1100

    
1101
/* do_getsockname() Must return target values and target errnos. */
1102
static abi_long do_getsockname(int fd, abi_ulong target_addr,
1103
                               abi_ulong target_addrlen)
1104
{
1105
    socklen_t addrlen = tget32(target_addrlen);
1106
    void *addr = alloca(addrlen);
1107
    abi_long ret;
1108

    
1109
    ret = get_errno(getsockname(fd, addr, &addrlen));
1110
    if (!is_error(ret)) {
1111
        host_to_target_sockaddr(target_addr, addr, addrlen);
1112
        tput32(target_addrlen, addrlen);
1113
    }
1114
    return ret;
1115
}
1116

    
1117
/* do_socketpair() Must return target values and target errnos. */
1118
static abi_long do_socketpair(int domain, int type, int protocol,
1119
                              abi_ulong target_tab)
1120
{
1121
    int tab[2];
1122
    abi_long ret;
1123

    
1124
    ret = get_errno(socketpair(domain, type, protocol, tab));
1125
    if (!is_error(ret)) {
1126
        tput32(target_tab, tab[0]);
1127
        tput32(target_tab + 4, tab[1]);
1128
    }
1129
    return ret;
1130
}
1131

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

    
1140
    host_msg = lock_user(msg, len, 1);
1141
    if (target_addr) {
1142
        addr = alloca(addrlen);
1143
        target_to_host_sockaddr(addr, target_addr, addrlen);
1144
        ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1145
    } else {
1146
        ret = get_errno(send(fd, host_msg, len, flags));
1147
    }
1148
    unlock_user(host_msg, msg, 0);
1149
    return ret;
1150
}
1151

    
1152
/* do_recvfrom() Must return target values and target errnos. */
1153
static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1154
                            abi_ulong target_addr,
1155
                            abi_ulong target_addrlen)
1156
{
1157
    socklen_t addrlen;
1158
    void *addr;
1159
    void *host_msg;
1160
    abi_long ret;
1161

    
1162
    host_msg = lock_user(msg, len, 0);
1163
    if (target_addr) {
1164
        addrlen = tget32(target_addrlen);
1165
        addr = alloca(addrlen);
1166
        ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1167
    } else {
1168
        addr = NULL; /* To keep compiler quiet.  */
1169
        ret = get_errno(recv(fd, host_msg, len, flags));
1170
    }
1171
    if (!is_error(ret)) {
1172
        if (target_addr) {
1173
            host_to_target_sockaddr(target_addr, addr, addrlen);
1174
            tput32(target_addrlen, addrlen);
1175
        }
1176
        unlock_user(host_msg, msg, len);
1177
    } else {
1178
        unlock_user(host_msg, msg, 0);
1179
    }
1180
    return ret;
1181
}
1182

    
1183
#ifdef TARGET_NR_socketcall
1184
/* do_socketcall() Must return target values and target errnos. */
1185
static abi_long do_socketcall(int num, abi_ulong vptr)
1186
{
1187
    abi_long ret;
1188
    const int n = sizeof(abi_ulong);
1189

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

    
1300
            ret = get_errno(shutdown(sockfd, how));
1301
        }
1302
        break;
1303
    case SOCKOP_sendmsg:
1304
    case SOCKOP_recvmsg:
1305
        {
1306
            int fd;
1307
            abi_ulong target_msg;
1308
            int flags;
1309

    
1310
            fd = tgetl(vptr);
1311
            target_msg = tgetl(vptr + n);
1312
            flags = tgetl(vptr + 2 * n);
1313

    
1314
            ret = do_sendrecvmsg(fd, target_msg, flags,
1315
                                 (num == SOCKOP_sendmsg));
1316
        }
1317
        break;
1318
    case SOCKOP_setsockopt:
1319
        {
1320
            int sockfd = tgetl(vptr);
1321
            int level = tgetl(vptr + n);
1322
            int optname = tgetl(vptr + 2 * n);
1323
            abi_ulong optval = tgetl(vptr + 3 * n);
1324
            socklen_t optlen = tgetl(vptr + 4 * n);
1325

    
1326
            ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1327
        }
1328
        break;
1329
    case SOCKOP_getsockopt:
1330
        {
1331
            int sockfd = tgetl(vptr);
1332
            int level = tgetl(vptr + n);
1333
            int optname = tgetl(vptr + 2 * n);
1334
            abi_ulong optval = tgetl(vptr + 3 * n);
1335
            abi_ulong poptlen = tgetl(vptr + 4 * n);
1336

    
1337
            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
1338
        }
1339
        break;
1340
    default:
1341
        gemu_log("Unsupported socketcall: %d\n", num);
1342
        ret = -TARGET_ENOSYS;
1343
        break;
1344
    }
1345
    return ret;
1346
}
1347
#endif
1348

    
1349
#ifdef TARGET_NR_ipc
1350
#define N_SHM_REGIONS        32
1351

    
1352
static struct shm_region {
1353
    uint32_t        start;
1354
    uint32_t        size;
1355
} shm_regions[N_SHM_REGIONS];
1356

    
1357
struct target_ipc_perm
1358
{
1359
    abi_long __key;
1360
    abi_ulong uid;
1361
    abi_ulong gid;
1362
    abi_ulong cuid;
1363
    abi_ulong cgid;
1364
    unsigned short int mode;
1365
    unsigned short int __pad1;
1366
    unsigned short int __seq;
1367
    unsigned short int __pad2;
1368
    abi_ulong __unused1;
1369
    abi_ulong __unused2;
1370
};
1371

    
1372
struct target_semid_ds
1373
{
1374
  struct target_ipc_perm sem_perm;
1375
  abi_ulong sem_otime;
1376
  abi_ulong __unused1;
1377
  abi_ulong sem_ctime;
1378
  abi_ulong __unused2;
1379
  abi_ulong sem_nsems;
1380
  abi_ulong __unused3;
1381
  abi_ulong __unused4;
1382
};
1383

    
1384
static inline void target_to_host_ipc_perm(struct ipc_perm *host_ip,
1385
                                           abi_ulong target_addr)
1386
{
1387
    struct target_ipc_perm *target_ip;
1388
    struct target_semid_ds *target_sd;
1389

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

    
1401
static inline void host_to_target_ipc_perm(abi_ulong target_addr,
1402
                                           struct ipc_perm *host_ip)
1403
{
1404
    struct target_ipc_perm *target_ip;
1405
    struct target_semid_ds *target_sd;
1406

    
1407
    lock_user_struct(target_sd, target_addr, 0);
1408
    target_ip = &(target_sd->sem_perm);
1409
    target_ip->__key = tswapl(host_ip->__key);
1410
    target_ip->uid = tswapl(host_ip->uid);
1411
    target_ip->gid = tswapl(host_ip->gid);
1412
    target_ip->cuid = tswapl(host_ip->cuid);
1413
    target_ip->cgid = tswapl(host_ip->cgid);
1414
    target_ip->mode = tswapl(host_ip->mode);
1415
    unlock_user_struct(target_sd, target_addr, 1);
1416
}
1417

    
1418
static inline void target_to_host_semid_ds(struct semid_ds *host_sd,
1419
                                          abi_ulong target_addr)
1420
{
1421
    struct target_semid_ds *target_sd;
1422

    
1423
    lock_user_struct(target_sd, target_addr, 1);
1424
    target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1425
    host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1426
    host_sd->sem_otime = tswapl(target_sd->sem_otime);
1427
    host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1428
    unlock_user_struct(target_sd, target_addr, 0);
1429
}
1430

    
1431
static inline void host_to_target_semid_ds(abi_ulong target_addr,
1432
                                           struct semid_ds *host_sd)
1433
{
1434
    struct target_semid_ds *target_sd;
1435

    
1436
    lock_user_struct(target_sd, target_addr, 0);
1437
    host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1438
    target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1439
    target_sd->sem_otime = tswapl(host_sd->sem_otime);
1440
    target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1441
    unlock_user_struct(target_sd, target_addr, 1);
1442
}
1443

    
1444
union semun {
1445
        int val;
1446
        struct semid_ds *buf;
1447
        unsigned short *array;
1448
};
1449

    
1450
union target_semun {
1451
        int val;
1452
        abi_long buf;
1453
        unsigned short int *array;
1454
};
1455

    
1456
static inline void target_to_host_semun(int cmd,
1457
                                        union semun *host_su,
1458
                                        abi_ulong target_addr,
1459
                                        struct semid_ds *ds)
1460
{
1461
    union target_semun *target_su;
1462

    
1463
    switch( cmd ) {
1464
        case IPC_STAT:
1465
        case IPC_SET:
1466
           lock_user_struct(target_su, target_addr, 1);
1467
           target_to_host_semid_ds(ds,target_su->buf);
1468
           host_su->buf = ds;
1469
           unlock_user_struct(target_su, target_addr, 0);
1470
           break;
1471
        case GETVAL:
1472
        case SETVAL:
1473
           lock_user_struct(target_su, target_addr, 1);
1474
           host_su->val = tswapl(target_su->val);
1475
           unlock_user_struct(target_su, target_addr, 0);
1476
           break;
1477
        case GETALL:
1478
        case SETALL:
1479
           lock_user_struct(target_su, target_addr, 1);
1480
           *host_su->array = tswap16(*target_su->array);
1481
           unlock_user_struct(target_su, target_addr, 0);
1482
           break;
1483
        default:
1484
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1485
    }
1486
}
1487

    
1488
static inline void host_to_target_semun(int cmd,
1489
                                        abi_ulong target_addr,
1490
                                        union semun *host_su,
1491
                                        struct semid_ds *ds)
1492
{
1493
    union target_semun *target_su;
1494

    
1495
    switch( cmd ) {
1496
        case IPC_STAT:
1497
        case IPC_SET:
1498
           lock_user_struct(target_su, target_addr, 0);
1499
           host_to_target_semid_ds(target_su->buf,ds);
1500
           unlock_user_struct(target_su, target_addr, 1);
1501
           break;
1502
        case GETVAL:
1503
        case SETVAL:
1504
           lock_user_struct(target_su, target_addr, 0);
1505
           target_su->val = tswapl(host_su->val);
1506
           unlock_user_struct(target_su, target_addr, 1);
1507
           break;
1508
        case GETALL:
1509
        case SETALL:
1510
           lock_user_struct(target_su, target_addr, 0);
1511
           *target_su->array = tswap16(*host_su->array);
1512
           unlock_user_struct(target_su, target_addr, 1);
1513
           break;
1514
        default:
1515
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1516
    }
1517
}
1518

    
1519
static inline abi_long do_semctl(int first, int second, int third,
1520
                                 abi_long ptr)
1521
{
1522
    union semun arg;
1523
    struct semid_ds dsarg;
1524
    int cmd = third&0xff;
1525
    abi_long ret = 0;
1526

    
1527
    switch( cmd ) {
1528
        case GETVAL:
1529
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1530
            ret = get_errno(semctl(first, second, cmd, arg));
1531
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1532
            break;
1533
        case SETVAL:
1534
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1535
            ret = get_errno(semctl(first, second, cmd, arg));
1536
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1537
            break;
1538
        case GETALL:
1539
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1540
            ret = get_errno(semctl(first, second, cmd, arg));
1541
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1542
            break;
1543
        case SETALL:
1544
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1545
            ret = get_errno(semctl(first, second, cmd, arg));
1546
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1547
            break;
1548
        case IPC_STAT:
1549
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1550
            ret = get_errno(semctl(first, second, cmd, arg));
1551
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1552
            break;
1553
        case IPC_SET:
1554
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1555
            ret = get_errno(semctl(first, second, cmd, arg));
1556
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1557
            break;
1558
    default:
1559
            ret = get_errno(semctl(first, second, cmd, arg));
1560
    }
1561

    
1562
    return ret;
1563
}
1564

    
1565
struct target_msqid_ds
1566
{
1567
  struct target_ipc_perm msg_perm;
1568
  abi_ulong msg_stime;
1569
  abi_ulong __unused1;
1570
  abi_ulong msg_rtime;
1571
  abi_ulong __unused2;
1572
  abi_ulong msg_ctime;
1573
  abi_ulong __unused3;
1574
  abi_ulong __msg_cbytes;
1575
  abi_ulong msg_qnum;
1576
  abi_ulong msg_qbytes;
1577
  abi_ulong msg_lspid;
1578
  abi_ulong msg_lrpid;
1579
  abi_ulong __unused4;
1580
  abi_ulong __unused5;
1581
};
1582

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

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

    
1601
static inline void host_to_target_msqid_ds(abi_ulong target_addr,
1602
                                           struct msqid_ds *host_md)
1603
{
1604
    struct target_msqid_ds *target_md;
1605

    
1606
    lock_user_struct(target_md, target_addr, 0);
1607
    host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1608
    target_md->msg_stime = tswapl(host_md->msg_stime);
1609
    target_md->msg_rtime = tswapl(host_md->msg_rtime);
1610
    target_md->msg_ctime = tswapl(host_md->msg_ctime);
1611
    target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1612
    target_md->msg_qnum = tswapl(host_md->msg_qnum);
1613
    target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1614
    target_md->msg_lspid = tswapl(host_md->msg_lspid);
1615
    target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1616
    unlock_user_struct(target_md, target_addr, 1);
1617
}
1618

    
1619
static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1620
{
1621
    struct msqid_ds dsarg;
1622
    int cmd = second&0xff;
1623
    abi_long ret = 0;
1624
    switch( cmd ) {
1625
    case IPC_STAT:
1626
    case IPC_SET:
1627
        target_to_host_msqid_ds(&dsarg,ptr);
1628
        ret = get_errno(msgctl(first, cmd, &dsarg));
1629
        host_to_target_msqid_ds(ptr,&dsarg);
1630
    default:
1631
        ret = get_errno(msgctl(first, cmd, &dsarg));
1632
    }
1633
    return ret;
1634
}
1635

    
1636
struct target_msgbuf {
1637
        abi_ulong mtype;
1638
        char        mtext[1];
1639
};
1640

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

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

    
1656
    return ret;
1657
}
1658

    
1659
static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1660
                                 unsigned int msgsz, int msgtype,
1661
                                 int msgflg)
1662
{
1663
    struct target_msgbuf *target_mb;
1664
    struct msgbuf *host_mb;
1665
    abi_long ret = 0;
1666

    
1667
    lock_user_struct(target_mb, msgp, 0);
1668
    host_mb = malloc(msgsz+sizeof(long));
1669
    ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1670
    if (ret > 0)
1671
            memcpy(target_mb->mtext, host_mb->mtext, ret);
1672
    target_mb->mtype = tswapl(host_mb->mtype);
1673
    free(host_mb);
1674
    unlock_user_struct(target_mb, msgp, 0);
1675

    
1676
    return ret;
1677
}
1678

    
1679
/* ??? This only works with linear mappings.  */
1680
/* do_ipc() must return target values and target errnos. */
1681
static abi_long do_ipc(unsigned int call, int first,
1682
                       int second, int third,
1683
                       abi_long ptr, abi_long fifth)
1684
{
1685
    int version;
1686
    abi_long ret = 0;
1687
    unsigned long raddr;
1688
    struct shmid_ds shm_info;
1689
    int i;
1690

    
1691
    version = call >> 16;
1692
    call &= 0xffff;
1693

    
1694
    switch (call) {
1695
    case IPCOP_semop:
1696
        ret = get_errno(semop(first,(struct sembuf *) ptr, second));
1697
        break;
1698

    
1699
    case IPCOP_semget:
1700
        ret = get_errno(semget(first, second, third));
1701
        break;
1702

    
1703
    case IPCOP_semctl:
1704
        ret = do_semctl(first, second, third, ptr);
1705
        break;
1706

    
1707
    case IPCOP_semtimedop:
1708
        gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1709
        ret = -TARGET_ENOSYS;
1710
        break;
1711

    
1712
        case IPCOP_msgget:
1713
                ret = get_errno(msgget(first, second));
1714
                break;
1715

    
1716
        case IPCOP_msgsnd:
1717
                ret = do_msgsnd(first, ptr, second, third);
1718
                break;
1719

    
1720
        case IPCOP_msgctl:
1721
                ret = do_msgctl(first, second, ptr);
1722
                break;
1723

    
1724
        case IPCOP_msgrcv:
1725
                {
1726
                      struct ipc_kludge
1727
                      {
1728
                              void *__unbounded msgp;
1729
                              long int msgtyp;
1730
                      };
1731

    
1732
                      struct ipc_kludge *foo = (struct ipc_kludge *) ptr;
1733
                      struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
1734

    
1735
                      ret = do_msgrcv(first, (long)msgp, second, 0, third);
1736

    
1737
                }
1738
                break;
1739

    
1740
    case IPCOP_shmat:
1741
        /* SHM_* flags are the same on all linux platforms */
1742
        ret = get_errno((long) shmat(first, (void *) ptr, second));
1743
        if (is_error(ret))
1744
            break;
1745
        raddr = ret;
1746
        /* find out the length of the shared memory segment */
1747

    
1748
        ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1749
        if (is_error(ret)) {
1750
            /* can't get length, bail out */
1751
            shmdt((void *) raddr);
1752
            break;
1753
        }
1754
        page_set_flags(raddr, raddr + shm_info.shm_segsz,
1755
                       PAGE_VALID | PAGE_READ |
1756
                       ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1757
        for (i = 0; i < N_SHM_REGIONS; ++i) {
1758
            if (shm_regions[i].start == 0) {
1759
                shm_regions[i].start = raddr;
1760
                shm_regions[i].size = shm_info.shm_segsz;
1761
                break;
1762
            }
1763
        }
1764
        if (put_user(raddr, (abi_ulong *)third))
1765
            return -TARGET_EFAULT;
1766
        ret = 0;
1767
        break;
1768
    case IPCOP_shmdt:
1769
        for (i = 0; i < N_SHM_REGIONS; ++i) {
1770
            if (shm_regions[i].start == ptr) {
1771
                shm_regions[i].start = 0;
1772
                page_set_flags(ptr, shm_regions[i].size, 0);
1773
                break;
1774
            }
1775
        }
1776
        ret = get_errno(shmdt((void *) ptr));
1777
        break;
1778

    
1779
    case IPCOP_shmget:
1780
        /* IPC_* flag values are the same on all linux platforms */
1781
        ret = get_errno(shmget(first, second, third));
1782
        break;
1783

    
1784
        /* IPC_* and SHM_* command values are the same on all linux platforms */
1785
    case IPCOP_shmctl:
1786
        switch(second) {
1787
        case IPC_RMID:
1788
        case SHM_LOCK:
1789
        case SHM_UNLOCK:
1790
            ret = get_errno(shmctl(first, second, NULL));
1791
            break;
1792
        default:
1793
            goto unimplemented;
1794
        }
1795
        break;
1796
    default:
1797
    unimplemented:
1798
        gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1799
        ret = -TARGET_ENOSYS;
1800
        break;
1801
    }
1802
    return ret;
1803
}
1804
#endif
1805

    
1806
/* kernel structure types definitions */
1807
#define IFNAMSIZ        16
1808

    
1809
#define STRUCT(name, list...) STRUCT_ ## name,
1810
#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1811
enum {
1812
#include "syscall_types.h"
1813
};
1814
#undef STRUCT
1815
#undef STRUCT_SPECIAL
1816

    
1817
#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1818
#define STRUCT_SPECIAL(name)
1819
#include "syscall_types.h"
1820
#undef STRUCT
1821
#undef STRUCT_SPECIAL
1822

    
1823
typedef struct IOCTLEntry {
1824
    unsigned int target_cmd;
1825
    unsigned int host_cmd;
1826
    const char *name;
1827
    int access;
1828
    const argtype arg_type[5];
1829
} IOCTLEntry;
1830

    
1831
#define IOC_R 0x0001
1832
#define IOC_W 0x0002
1833
#define IOC_RW (IOC_R | IOC_W)
1834

    
1835
#define MAX_STRUCT_SIZE 4096
1836

    
1837
IOCTLEntry ioctl_entries[] = {
1838
#define IOCTL(cmd, access, types...) \
1839
    { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1840
#include "ioctls.h"
1841
    { 0, 0, },
1842
};
1843

    
1844
/* ??? Implement proper locking for ioctls.  */
1845
/* do_ioctl() Must return target values and target errnos. */
1846
static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
1847
{
1848
    const IOCTLEntry *ie;
1849
    const argtype *arg_type;
1850
    abi_long ret;
1851
    uint8_t buf_temp[MAX_STRUCT_SIZE];
1852
    int target_size;
1853
    void *argptr;
1854

    
1855
    ie = ioctl_entries;
1856
    for(;;) {
1857
        if (ie->target_cmd == 0) {
1858
            gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
1859
            return -TARGET_ENOSYS;
1860
        }
1861
        if (ie->target_cmd == cmd)
1862
            break;
1863
        ie++;
1864
    }
1865
    arg_type = ie->arg_type;
1866
#if defined(DEBUG)
1867
    gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
1868
#endif
1869
    switch(arg_type[0]) {
1870
    case TYPE_NULL:
1871
        /* no argument */
1872
        ret = get_errno(ioctl(fd, ie->host_cmd));
1873
        break;
1874
    case TYPE_PTRVOID:
1875
    case TYPE_INT:
1876
        /* int argment */
1877
        ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1878
        break;
1879
    case TYPE_PTR:
1880
        arg_type++;
1881
        target_size = thunk_type_size(arg_type, 0);
1882
        switch(ie->access) {
1883
        case IOC_R:
1884
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1885
            if (!is_error(ret)) {
1886
                argptr = lock_user(arg, target_size, 0);
1887
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1888
                unlock_user(argptr, arg, target_size);
1889
            }
1890
            break;
1891
        case IOC_W:
1892
            argptr = lock_user(arg, target_size, 1);
1893
            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1894
            unlock_user(argptr, arg, 0);
1895
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1896
            break;
1897
        default:
1898
        case IOC_RW:
1899
            argptr = lock_user(arg, target_size, 1);
1900
            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1901
            unlock_user(argptr, arg, 0);
1902
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1903
            if (!is_error(ret)) {
1904
                argptr = lock_user(arg, target_size, 0);
1905
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1906
                unlock_user(argptr, arg, target_size);
1907
            }
1908
            break;
1909
        }
1910
        break;
1911
    default:
1912
        gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
1913
                 (long)cmd, arg_type[0]);
1914
        ret = -TARGET_ENOSYS;
1915
        break;
1916
    }
1917
    return ret;
1918
}
1919

    
1920
bitmask_transtbl iflag_tbl[] = {
1921
        { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1922
        { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1923
        { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1924
        { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1925
        { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1926
        { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1927
        { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1928
        { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1929
        { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1930
        { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1931
        { TARGET_IXON, TARGET_IXON, IXON, IXON },
1932
        { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1933
        { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1934
        { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1935
        { 0, 0, 0, 0 }
1936
};
1937

    
1938
bitmask_transtbl oflag_tbl[] = {
1939
        { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1940
        { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1941
        { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1942
        { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1943
        { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1944
        { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1945
        { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1946
        { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1947
        { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1948
        { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1949
        { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1950
        { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1951
        { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1952
        { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1953
        { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1954
        { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1955
        { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1956
        { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1957
        { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1958
        { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1959
        { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1960
        { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1961
        { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1962
        { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1963
        { 0, 0, 0, 0 }
1964
};
1965

    
1966
bitmask_transtbl cflag_tbl[] = {
1967
        { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1968
        { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1969
        { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1970
        { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1971
        { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1972
        { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1973
        { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1974
        { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1975
        { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1976
        { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1977
        { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1978
        { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1979
        { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1980
        { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1981
        { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1982
        { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1983
        { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1984
        { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1985
        { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1986
        { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1987
        { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1988
        { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1989
        { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1990
        { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1991
        { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1992
        { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1993
        { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1994
        { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1995
        { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1996
        { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1997
        { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1998
        { 0, 0, 0, 0 }
1999
};
2000

    
2001
bitmask_transtbl lflag_tbl[] = {
2002
        { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2003
        { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2004
        { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2005
        { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2006
        { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2007
        { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2008
        { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2009
        { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2010
        { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2011
        { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2012
        { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2013
        { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2014
        { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2015
        { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2016
        { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2017
        { 0, 0, 0, 0 }
2018
};
2019

    
2020
static void target_to_host_termios (void *dst, const void *src)
2021
{
2022
    struct host_termios *host = dst;
2023
    const struct target_termios *target = src;
2024

    
2025
    host->c_iflag =
2026
        target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2027
    host->c_oflag =
2028
        target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2029
    host->c_cflag =
2030
        target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2031
    host->c_lflag =
2032
        target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2033
    host->c_line = target->c_line;
2034

    
2035
    host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2036
    host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2037
    host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2038
    host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2039
    host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2040
    host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2041
    host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2042
    host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2043
    host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2044
    host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2045
    host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2046
    host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2047
    host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2048
    host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2049
    host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2050
    host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2051
    host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2052
}
2053

    
2054
static void host_to_target_termios (void *dst, const void *src)
2055
{
2056
    struct target_termios *target = dst;
2057
    const struct host_termios *host = src;
2058

    
2059
    target->c_iflag =
2060
        tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2061
    target->c_oflag =
2062
        tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2063
    target->c_cflag =
2064
        tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2065
    target->c_lflag =
2066
        tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2067
    target->c_line = host->c_line;
2068

    
2069
    target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2070
    target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2071
    target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2072
    target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2073
    target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2074
    target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2075
    target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2076
    target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2077
    target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2078
    target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2079
    target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2080
    target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2081
    target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2082
    target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2083
    target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2084
    target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2085
    target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2086
}
2087

    
2088
StructEntry struct_termios_def = {
2089
    .convert = { host_to_target_termios, target_to_host_termios },
2090
    .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2091
    .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2092
};
2093

    
2094
static bitmask_transtbl mmap_flags_tbl[] = {
2095
        { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2096
        { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2097
        { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2098
        { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2099
        { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2100
        { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2101
        { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2102
        { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2103
        { 0, 0, 0, 0 }
2104
};
2105

    
2106
static bitmask_transtbl fcntl_flags_tbl[] = {
2107
        { TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
2108
        { TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
2109
        { TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
2110
        { TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
2111
        { TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
2112
        { TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
2113
        { TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
2114
        { TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
2115
        { TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
2116
        { TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
2117
        { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2118
        { TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
2119
        { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2120
#if defined(O_DIRECT)
2121
        { TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
2122
#endif
2123
        { 0, 0, 0, 0 }
2124
};
2125

    
2126
#if defined(TARGET_I386)
2127

    
2128
/* NOTE: there is really one LDT for all the threads */
2129
uint8_t *ldt_table;
2130

    
2131
static int read_ldt(abi_ulong ptr, unsigned long bytecount)
2132
{
2133
    int size;
2134
    void *p;
2135

    
2136
    if (!ldt_table)
2137
        return 0;
2138
    size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2139
    if (size > bytecount)
2140
        size = bytecount;
2141
    p = lock_user(ptr, size, 0);
2142
    /* ??? Shoudl this by byteswapped?  */
2143
    memcpy(p, ldt_table, size);
2144
    unlock_user(p, ptr, size);
2145
    return size;
2146
}
2147

    
2148
/* XXX: add locking support */
2149
/* write_ldt() returns host errnos */
2150
static int write_ldt(CPUX86State *env,
2151
                     abi_ulong ptr, unsigned long bytecount, int oldmode)
2152
{
2153
    struct target_modify_ldt_ldt_s ldt_info;
2154
    struct target_modify_ldt_ldt_s *target_ldt_info;
2155
    int seg_32bit, contents, read_exec_only, limit_in_pages;
2156
    int seg_not_present, useable;
2157
    uint32_t *lp, entry_1, entry_2;
2158

    
2159
    if (bytecount != sizeof(ldt_info))
2160
        return -EINVAL;
2161
    lock_user_struct(target_ldt_info, ptr, 1);
2162
    ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2163
    ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2164
    ldt_info.limit = tswap32(target_ldt_info->limit);
2165
    ldt_info.flags = tswap32(target_ldt_info->flags);
2166
    unlock_user_struct(target_ldt_info, ptr, 0);
2167

    
2168
    if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2169
        return -EINVAL;
2170
    seg_32bit = ldt_info.flags & 1;
2171
    contents = (ldt_info.flags >> 1) & 3;
2172
    read_exec_only = (ldt_info.flags >> 3) & 1;
2173
    limit_in_pages = (ldt_info.flags >> 4) & 1;
2174
    seg_not_present = (ldt_info.flags >> 5) & 1;
2175
    useable = (ldt_info.flags >> 6) & 1;
2176

    
2177
    if (contents == 3) {
2178
        if (oldmode)
2179
            return -EINVAL;
2180
        if (seg_not_present == 0)
2181
            return -EINVAL;
2182
    }
2183
    /* allocate the LDT */
2184
    if (!ldt_table) {
2185
        ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2186
        if (!ldt_table)
2187
            return -ENOMEM;
2188
        memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2189
        env->ldt.base = h2g(ldt_table);
2190
        env->ldt.limit = 0xffff;
2191
    }
2192

    
2193
    /* NOTE: same code as Linux kernel */
2194
    /* Allow LDTs to be cleared by the user. */
2195
    if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2196
        if (oldmode ||
2197
            (contents == 0                &&
2198
             read_exec_only == 1        &&
2199
             seg_32bit == 0                &&
2200
             limit_in_pages == 0        &&
2201
             seg_not_present == 1        &&
2202
             useable == 0 )) {
2203
            entry_1 = 0;
2204
            entry_2 = 0;
2205
            goto install;
2206
        }
2207
    }
2208

    
2209
    entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2210
        (ldt_info.limit & 0x0ffff);
2211
    entry_2 = (ldt_info.base_addr & 0xff000000) |
2212
        ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2213
        (ldt_info.limit & 0xf0000) |
2214
        ((read_exec_only ^ 1) << 9) |
2215
        (contents << 10) |
2216
        ((seg_not_present ^ 1) << 15) |
2217
        (seg_32bit << 22) |
2218
        (limit_in_pages << 23) |
2219
        0x7000;
2220
    if (!oldmode)
2221
        entry_2 |= (useable << 20);
2222

    
2223
    /* Install the new entry ...  */
2224
install:
2225
    lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2226
    lp[0] = tswap32(entry_1);
2227
    lp[1] = tswap32(entry_2);
2228
    return 0;
2229
}
2230

    
2231
/* specific and weird i386 syscalls */
2232
/* do_modify_ldt() returns host errnos (it is inconsistent with the
2233
   other do_*() functions which return target errnos). */
2234
int do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, unsigned long bytecount)
2235
{
2236
    int ret = -ENOSYS;
2237

    
2238
    switch (func) {
2239
    case 0:
2240
        ret = read_ldt(ptr, bytecount);
2241
        break;
2242
    case 1:
2243
        ret = write_ldt(env, ptr, bytecount, 1);
2244
        break;
2245
    case 0x11:
2246
        ret = write_ldt(env, ptr, bytecount, 0);
2247
        break;
2248
    }
2249
    return ret;
2250
}
2251

    
2252
#endif /* defined(TARGET_I386) */
2253

    
2254
/* this stack is the equivalent of the kernel stack associated with a
2255
   thread/process */
2256
#define NEW_STACK_SIZE 8192
2257

    
2258
static int clone_func(void *arg)
2259
{
2260
    CPUState *env = arg;
2261
    cpu_loop(env);
2262
    /* never exits */
2263
    return 0;
2264
}
2265

    
2266
/* do_fork() Must return host values and target errnos (unlike most
2267
   do_*() functions). */
2268
int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
2269
{
2270
    int ret;
2271
    TaskState *ts;
2272
    uint8_t *new_stack;
2273
    CPUState *new_env;
2274

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

    
2358
static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2359
{
2360
    struct flock fl;
2361
    struct target_flock *target_fl;
2362
    struct flock64 fl64;
2363
    struct target_flock64 *target_fl64;
2364
    abi_long ret;
2365

    
2366
    switch(cmd) {
2367
    case TARGET_F_GETLK:
2368
        lock_user_struct(target_fl, arg, 1);
2369
        fl.l_type = tswap16(target_fl->l_type);
2370
        fl.l_whence = tswap16(target_fl->l_whence);
2371
        fl.l_start = tswapl(target_fl->l_start);
2372
        fl.l_len = tswapl(target_fl->l_len);
2373
        fl.l_pid = tswapl(target_fl->l_pid);
2374
        unlock_user_struct(target_fl, arg, 0);
2375
        ret = fcntl(fd, cmd, &fl);
2376
        if (ret == 0) {
2377
            lock_user_struct(target_fl, arg, 0);
2378
            target_fl->l_type = tswap16(fl.l_type);
2379
            target_fl->l_whence = tswap16(fl.l_whence);
2380
            target_fl->l_start = tswapl(fl.l_start);
2381
            target_fl->l_len = tswapl(fl.l_len);
2382
            target_fl->l_pid = tswapl(fl.l_pid);
2383
            unlock_user_struct(target_fl, arg, 1);
2384
        }
2385
        break;
2386

    
2387
    case TARGET_F_SETLK:
2388
    case TARGET_F_SETLKW:
2389
        lock_user_struct(target_fl, arg, 1);
2390
        fl.l_type = tswap16(target_fl->l_type);
2391
        fl.l_whence = tswap16(target_fl->l_whence);
2392
        fl.l_start = tswapl(target_fl->l_start);
2393
        fl.l_len = tswapl(target_fl->l_len);
2394
        fl.l_pid = tswapl(target_fl->l_pid);
2395
        unlock_user_struct(target_fl, arg, 0);
2396
        ret = fcntl(fd, cmd, &fl);
2397
        break;
2398

    
2399
    case TARGET_F_GETLK64:
2400
        lock_user_struct(target_fl64, arg, 1);
2401
        fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2402
        fl64.l_whence = tswap16(target_fl64->l_whence);
2403
        fl64.l_start = tswapl(target_fl64->l_start);
2404
        fl64.l_len = tswapl(target_fl64->l_len);
2405
        fl64.l_pid = tswap16(target_fl64->l_pid);
2406
        unlock_user_struct(target_fl64, arg, 0);
2407
        ret = fcntl(fd, cmd >> 1, &fl64);
2408
        if (ret == 0) {
2409
            lock_user_struct(target_fl64, arg, 0);
2410
            target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2411
            target_fl64->l_whence = tswap16(fl64.l_whence);
2412
            target_fl64->l_start = tswapl(fl64.l_start);
2413
            target_fl64->l_len = tswapl(fl64.l_len);
2414
            target_fl64->l_pid = tswapl(fl64.l_pid);
2415
            unlock_user_struct(target_fl64, arg, 1);
2416
        }
2417
                break;
2418
    case TARGET_F_SETLK64:
2419
    case TARGET_F_SETLKW64:
2420
        lock_user_struct(target_fl64, arg, 1);
2421
        fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2422
        fl64.l_whence = tswap16(target_fl64->l_whence);
2423
        fl64.l_start = tswapl(target_fl64->l_start);
2424
        fl64.l_len = tswapl(target_fl64->l_len);
2425
        fl64.l_pid = tswap16(target_fl64->l_pid);
2426
        unlock_user_struct(target_fl64, arg, 0);
2427
                ret = fcntl(fd, cmd >> 1, &fl64);
2428
        break;
2429

    
2430
    case F_GETFL:
2431
        ret = fcntl(fd, cmd, arg);
2432
        ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2433
        break;
2434

    
2435
    case F_SETFL:
2436
        ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2437
        break;
2438

    
2439
    default:
2440
        ret = fcntl(fd, cmd, arg);
2441
        break;
2442
    }
2443
    return ret;
2444
}
2445

    
2446
#ifdef USE_UID16
2447

    
2448
static inline int high2lowuid(int uid)
2449
{
2450
    if (uid > 65535)
2451
        return 65534;
2452
    else
2453
        return uid;
2454
}
2455

    
2456
static inline int high2lowgid(int gid)
2457
{
2458
    if (gid > 65535)
2459
        return 65534;
2460
    else
2461
        return gid;
2462
}
2463

    
2464
static inline int low2highuid(int uid)
2465
{
2466
    if ((int16_t)uid == -1)
2467
        return -1;
2468
    else
2469
        return uid;
2470
}
2471

    
2472
static inline int low2highgid(int gid)
2473
{
2474
    if ((int16_t)gid == -1)
2475
        return -1;
2476
    else
2477
        return gid;
2478
}
2479

    
2480
#endif /* USE_UID16 */
2481

    
2482
void syscall_init(void)
2483
{
2484
    IOCTLEntry *ie;
2485
    const argtype *arg_type;
2486
    int size;
2487
    int i;
2488

    
2489
#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2490
#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
2491
#include "syscall_types.h"
2492
#undef STRUCT
2493
#undef STRUCT_SPECIAL
2494

    
2495
    /* we patch the ioctl size if necessary. We rely on the fact that
2496
       no ioctl has all the bits at '1' in the size field */
2497
    ie = ioctl_entries;
2498
    while (ie->target_cmd != 0) {
2499
        if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2500
            TARGET_IOC_SIZEMASK) {
2501
            arg_type = ie->arg_type;
2502
            if (arg_type[0] != TYPE_PTR) {
2503
                fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2504
                        ie->target_cmd);
2505
                exit(1);
2506
            }
2507
            arg_type++;
2508
            size = thunk_type_size(arg_type, 0);
2509
            ie->target_cmd = (ie->target_cmd &
2510
                              ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2511
                (size << TARGET_IOC_SIZESHIFT);
2512
        }
2513

    
2514
        /* Build target_to_host_errno_table[] table from
2515
         * host_to_target_errno_table[]. */
2516
        for (i=0; i < ERRNO_TABLE_SIZE; i++)
2517
                target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2518

    
2519
        /* automatic consistency check if same arch */
2520
#if defined(__i386__) && defined(TARGET_I386)
2521
        if (ie->target_cmd != ie->host_cmd) {
2522
            fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2523
                    ie->target_cmd, ie->host_cmd);
2524
        }
2525
#endif
2526
        ie++;
2527
    }
2528
}
2529

    
2530
#if TARGET_ABI_BITS == 32
2531
static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2532
{
2533
#ifdef TARGET_WORDS_BIG_ENDIAN
2534
    return ((uint64_t)word0 << 32) | word1;
2535
#else
2536
    return ((uint64_t)word1 << 32) | word0;
2537
#endif
2538
}
2539
#else /* TARGET_ABI_BITS == 32 */
2540
static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2541
{
2542
    return word0;
2543
}
2544
#endif /* TARGET_ABI_BITS != 32 */
2545

    
2546
#ifdef TARGET_NR_truncate64
2547
static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2548
                                         abi_long arg2,
2549
                                         abi_long arg3,
2550
                                         abi_long arg4)
2551
{
2552
#ifdef TARGET_ARM
2553
    if (((CPUARMState *)cpu_env)->eabi)
2554
      {
2555
        arg2 = arg3;
2556
        arg3 = arg4;
2557
      }
2558
#endif
2559
    return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2560
}
2561
#endif
2562

    
2563
#ifdef TARGET_NR_ftruncate64
2564
static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2565
                                          abi_long arg2,
2566
                                          abi_long arg3,
2567
                                          abi_long arg4)
2568
{
2569
#ifdef TARGET_ARM
2570
    if (((CPUARMState *)cpu_env)->eabi)
2571
      {
2572
        arg2 = arg3;
2573
        arg3 = arg4;
2574
      }
2575
#endif
2576
    return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2577
}
2578
#endif
2579

    
2580
static inline void target_to_host_timespec(struct timespec *host_ts,
2581
                                           abi_ulong target_addr)
2582
{
2583
    struct target_timespec *target_ts;
2584

    
2585
    lock_user_struct(target_ts, target_addr, 1);
2586
    host_ts->tv_sec = tswapl(target_ts->tv_sec);
2587
    host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2588
    unlock_user_struct(target_ts, target_addr, 0);
2589
}
2590

    
2591
static inline void host_to_target_timespec(abi_ulong target_addr,
2592
                                           struct timespec *host_ts)
2593
{
2594
    struct target_timespec *target_ts;
2595

    
2596
    lock_user_struct(target_ts, target_addr, 0);
2597
    target_ts->tv_sec = tswapl(host_ts->tv_sec);
2598
    target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2599
    unlock_user_struct(target_ts, target_addr, 1);
2600
}
2601

    
2602
/* do_syscall() should always have a single exit point at the end so
2603
   that actions, such as logging of syscall results, can be performed.
2604
   All errnos that do_syscall() returns must be -TARGET_<errcode>. */
2605
abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2606
                    abi_long arg2, abi_long arg3, abi_long arg4,
2607
                    abi_long arg5, abi_long arg6)
2608
{
2609
    abi_long ret;
2610
    struct stat st;
2611
    struct statfs stfs;
2612
    void *p;
2613

    
2614
#ifdef DEBUG
2615
    gemu_log("syscall %d", num);
2616
#endif
2617
    if(do_strace)
2618
        print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
2619

    
2620
    switch(num) {
2621
    case TARGET_NR_exit:
2622
#ifdef HAVE_GPROF
2623
        _mcleanup();
2624
#endif
2625
        gdb_exit(cpu_env, arg1);
2626
        /* XXX: should free thread stack and CPU env */
2627
        _exit(arg1);
2628
        ret = 0; /* avoid warning */
2629
        break;
2630
    case TARGET_NR_read:
2631
        page_unprotect_range(arg2, arg3);
2632
        p = lock_user(arg2, arg3, 0);
2633
        ret = get_errno(read(arg1, p, arg3));
2634
        unlock_user(p, arg2, ret);
2635
        break;
2636
    case TARGET_NR_write:
2637
        p = lock_user(arg2, arg3, 1);
2638
        ret = get_errno(write(arg1, p, arg3));
2639
        unlock_user(p, arg2, 0);
2640
        break;
2641
    case TARGET_NR_open:
2642
        p = lock_user_string(arg1);
2643
        ret = get_errno(open(path(p),
2644
                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
2645
                             arg3));
2646
        unlock_user(p, arg1, 0);
2647
        break;
2648
#if defined(TARGET_NR_openat) && defined(__NR_openat)
2649
    case TARGET_NR_openat:
2650
        if (!arg2) {
2651
            ret = -TARGET_EFAULT;
2652
            goto fail;
2653
        }
2654
        p = lock_user_string(arg2);
2655
        if (!access_ok(VERIFY_READ, p, 1))
2656
            /* Don't "goto fail" so that cleanup can happen. */
2657
            ret = -TARGET_EFAULT;
2658
        else
2659
            ret = get_errno(sys_openat(arg1,
2660
                                       path(p),
2661
                                       target_to_host_bitmask(arg3, fcntl_flags_tbl),
2662
                                       arg4));
2663
        if (p)
2664
            unlock_user(p, arg2, 0);
2665
        break;
2666
#endif
2667
    case TARGET_NR_close:
2668
        ret = get_errno(close(arg1));
2669
        break;
2670
    case TARGET_NR_brk:
2671
        ret = do_brk(arg1);
2672
        break;
2673
    case TARGET_NR_fork:
2674
        ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
2675
        break;
2676
#ifdef TARGET_NR_waitpid
2677
    case TARGET_NR_waitpid:
2678
        {
2679
            int status;
2680
            ret = get_errno(waitpid(arg1, &status, arg3));
2681
            if (!is_error(ret) && arg2)
2682
                tput32(arg2, status);
2683
        }
2684
        break;
2685
#endif
2686
#ifdef TARGET_NR_creat /* not on alpha */
2687
    case TARGET_NR_creat:
2688
        p = lock_user_string(arg1);
2689
        ret = get_errno(creat(p, arg2));
2690
        unlock_user(p, arg1, 0);
2691
        break;
2692
#endif
2693
    case TARGET_NR_link:
2694
        {
2695
            void * p2;
2696
            p = lock_user_string(arg1);
2697
            p2 = lock_user_string(arg2);
2698
            ret = get_errno(link(p, p2));
2699
            unlock_user(p2, arg2, 0);
2700
            unlock_user(p, arg1, 0);
2701
        }
2702
        break;
2703
#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2704
    case TARGET_NR_linkat:
2705
        if (!arg2 || !arg4) {
2706
            ret = -TARGET_EFAULT;
2707
            goto fail;
2708
        }
2709
        {
2710
            void * p2 = NULL;
2711
            p  = lock_user_string(arg2);
2712
            p2 = lock_user_string(arg4);
2713
            if (!access_ok(VERIFY_READ, p, 1)
2714
                || !access_ok(VERIFY_READ, p2, 1))
2715
                /* Don't "goto fail" so that cleanup can happen. */
2716
                ret = -TARGET_EFAULT;
2717
            else
2718
                ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
2719
            if (p2)
2720
                unlock_user(p, arg2, 0);
2721
            if (p)
2722
                unlock_user(p2, arg4, 0);
2723
        }
2724
        break;
2725
#endif
2726
    case TARGET_NR_unlink:
2727
        p = lock_user_string(arg1);
2728
        ret = get_errno(unlink(p));
2729
        unlock_user(p, arg1, 0);
2730
        break;
2731
#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2732
    case TARGET_NR_unlinkat:
2733
        if (!arg2) {
2734
            ret = -TARGET_EFAULT;
2735
            goto fail;
2736
        }
2737
        p = lock_user_string(arg2);
2738
        if (!access_ok(VERIFY_READ, p, 1))
2739
            /* Don't "goto fail" so that cleanup can happen. */
2740
            ret = -TARGET_EFAULT;
2741
        else
2742
            ret = get_errno(sys_unlinkat(arg1, p, arg3));
2743
        if (p)
2744
            unlock_user(p, arg2, 0);
2745
        break;
2746
#endif
2747
    case TARGET_NR_execve:
2748
        {
2749
            char **argp, **envp;
2750
            int argc, envc;
2751
            abi_ulong gp;
2752
            abi_ulong guest_argp;
2753
            abi_ulong guest_envp;
2754
            abi_ulong addr;
2755
            char **q;
2756

    
2757
            argc = 0;
2758
            guest_argp = arg2;
2759
            for (gp = guest_argp; tgetl(gp); gp++)
2760
                argc++;
2761
            envc = 0;
2762
            guest_envp = arg3;
2763
            for (gp = guest_envp; tgetl(gp); gp++)
2764
                envc++;
2765

    
2766
            argp = alloca((argc + 1) * sizeof(void *));
2767
            envp = alloca((envc + 1) * sizeof(void *));
2768

    
2769
            for (gp = guest_argp, q = argp; ;
2770
                  gp += sizeof(abi_ulong), q++) {
2771
                addr = tgetl(gp);
2772
                if (!addr)
2773
                    break;
2774
                *q = lock_user_string(addr);
2775
            }
2776
            *q = NULL;
2777

    
2778
            for (gp = guest_envp, q = envp; ;
2779
                  gp += sizeof(abi_ulong), q++) {
2780
                addr = tgetl(gp);
2781
                if (!addr)
2782
                    break;
2783
                *q = lock_user_string(addr);
2784
            }
2785
            *q = NULL;
2786

    
2787
            p = lock_user_string(arg1);
2788
            ret = get_errno(execve(p, argp, envp));
2789
            unlock_user(p, arg1, 0);
2790

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

    
3180
            if (arg2) {
3181
                lock_user_struct(old_act, arg2, 1);
3182
                act._sa_handler = old_act->_sa_handler;
3183
                target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3184
                act.sa_flags = old_act->sa_flags;
3185
                unlock_user_struct(old_act, arg2, 0);
3186
                pact = &act;
3187
            } else {
3188
                pact = NULL;
3189
            }
3190

    
3191
            ret = get_errno(do_sigaction(arg1, pact, &oact));
3192

    
3193
            if (!is_error(ret) && arg3) {
3194
                lock_user_struct(old_act, arg3, 0);
3195
                old_act->_sa_handler = oact._sa_handler;
3196
                old_act->sa_flags = oact.sa_flags;
3197
                old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3198
                old_act->sa_mask.sig[1] = 0;
3199
                old_act->sa_mask.sig[2] = 0;
3200
                old_act->sa_mask.sig[3] = 0;
3201
                unlock_user_struct(old_act, arg3, 1);
3202
            }
3203
#endif
3204
        }
3205
        break;
3206
#endif
3207
    case TARGET_NR_rt_sigaction:
3208
        {
3209
            struct target_sigaction *act;
3210
            struct target_sigaction *oact;
3211

    
3212
            if (arg2)
3213
                lock_user_struct(act, arg2, 1);
3214
            else
3215
                act = NULL;
3216
            if (arg3)
3217
                lock_user_struct(oact, arg3, 0);
3218
            else
3219
                oact = NULL;
3220
            ret = get_errno(do_sigaction(arg1, act, oact));
3221
            if (arg2)
3222
                unlock_user_struct(act, arg2, 0);
3223
            if (arg3)
3224
                unlock_user_struct(oact, arg3, 1);
3225
        }
3226
        break;
3227
#ifdef TARGET_NR_sgetmask /* not on alpha */
3228
    case TARGET_NR_sgetmask:
3229
        {
3230
            sigset_t cur_set;
3231
            abi_ulong target_set;
3232
            sigprocmask(0, NULL, &cur_set);
3233
            host_to_target_old_sigset(&target_set, &cur_set);
3234
            ret = target_set;
3235
        }
3236
        break;
3237
#endif
3238
#ifdef TARGET_NR_ssetmask /* not on alpha */
3239
    case TARGET_NR_ssetmask:
3240
        {
3241
            sigset_t set, oset, cur_set;
3242
            abi_ulong target_set = arg1;
3243
            sigprocmask(0, NULL, &cur_set);
3244
            target_to_host_old_sigset(&set, &target_set);
3245
            sigorset(&set, &set, &cur_set);
3246
            sigprocmask(SIG_SETMASK, &set, &oset);
3247
            host_to_target_old_sigset(&target_set, &oset);
3248
            ret = target_set;
3249
        }
3250
        break;
3251
#endif
3252
#ifdef TARGET_NR_sigprocmask
3253
    case TARGET_NR_sigprocmask:
3254
        {
3255
            int how = arg1;
3256
            sigset_t set, oldset, *set_ptr;
3257

    
3258
            if (arg2) {
3259
                switch(how) {
3260
                case TARGET_SIG_BLOCK:
3261
                    how = SIG_BLOCK;
3262
                    break;
3263
                case TARGET_SIG_UNBLOCK:
3264
                    how = SIG_UNBLOCK;
3265
                    break;
3266
                case TARGET_SIG_SETMASK:
3267
                    how = SIG_SETMASK;
3268
                    break;
3269
                default:
3270
                    ret = -TARGET_EINVAL;
3271
                    goto fail;
3272
                }
3273
                p = lock_user(arg2, sizeof(target_sigset_t), 1);
3274
                target_to_host_old_sigset(&set, p);
3275
                unlock_user(p, arg2, 0);
3276
                set_ptr = &set;
3277
            } else {
3278
                how = 0;
3279
                set_ptr = NULL;
3280
            }
3281
            ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
3282
            if (!is_error(ret) && arg3) {
3283
                p = lock_user(arg3, sizeof(target_sigset_t), 0);
3284
                host_to_target_old_sigset(p, &oldset);
3285
                unlock_user(p, arg3, sizeof(target_sigset_t));
3286
            }
3287
        }
3288
        break;
3289
#endif
3290
    case TARGET_NR_rt_sigprocmask:
3291
        {
3292
            int how = arg1;
3293
            sigset_t set, oldset, *set_ptr;
3294

    
3295
            if (arg2) {
3296
                switch(how) {
3297
                case TARGET_SIG_BLOCK:
3298
                    how = SIG_BLOCK;
3299
                    break;
3300
                case TARGET_SIG_UNBLOCK:
3301
                    how = SIG_UNBLOCK;
3302
                    break;
3303
                case TARGET_SIG_SETMASK:
3304
                    how = SIG_SETMASK;
3305
                    break;
3306
                default:
3307
                    ret = -TARGET_EINVAL;
3308
                    goto fail;
3309
                }
3310
                p = lock_user(arg2, sizeof(target_sigset_t), 1);
3311
                target_to_host_sigset(&set, p);
3312
                unlock_user(p, arg2, 0);
3313
                set_ptr = &set;
3314
            } else {
3315
                how = 0;
3316
                set_ptr = NULL;
3317
            }
3318
            ret = get_errno(sigprocmask(how, set_ptr, &oldset));
3319
            if (!is_error(ret) && arg3) {
3320
                p = lock_user(arg3, sizeof(target_sigset_t), 0);
3321
                host_to_target_sigset(p, &oldset);
3322
                unlock_user(p, arg3, sizeof(target_sigset_t));
3323
            }
3324
        }
3325
        break;
3326
#ifdef TARGET_NR_sigpending
3327
    case TARGET_NR_sigpending:
3328
        {
3329
            sigset_t set;
3330
            ret = get_errno(sigpending(&set));
3331
            if (!is_error(ret)) {
3332
                p = lock_user(arg1, sizeof(target_sigset_t), 0);
3333
                host_to_target_old_sigset(p, &set);
3334
                unlock_user(p, arg1, sizeof(target_sigset_t));
3335
            }
3336
        }
3337
        break;
3338
#endif
3339
    case TARGET_NR_rt_sigpending:
3340
        {
3341
            sigset_t set;
3342
            ret = get_errno(sigpending(&set));
3343
            if (!is_error(ret)) {
3344
                p = lock_user(arg1, sizeof(target_sigset_t), 0);
3345
                host_to_target_sigset(p, &set);
3346
                unlock_user(p, arg1, sizeof(target_sigset_t));
3347
            }
3348
        }
3349
        break;
3350
#ifdef TARGET_NR_sigsuspend
3351
    case TARGET_NR_sigsuspend:
3352
        {
3353
            sigset_t set;
3354
            p = lock_user(arg1, sizeof(target_sigset_t), 1);
3355
            target_to_host_old_sigset(&set, p);
3356
            unlock_user(p, arg1, 0);
3357
            ret = get_errno(sigsuspend(&set));
3358
        }
3359
        break;
3360
#endif
3361
    case TARGET_NR_rt_sigsuspend:
3362
        {
3363
            sigset_t set;
3364
            p = lock_user(arg1, sizeof(target_sigset_t), 1);
3365
            target_to_host_sigset(&set, p);
3366
            unlock_user(p, arg1, 0);
3367
            ret = get_errno(sigsuspend(&set));
3368
        }
3369
        break;
3370
    case TARGET_NR_rt_sigtimedwait:
3371
        {
3372
            sigset_t set;
3373
            struct timespec uts, *puts;
3374
            siginfo_t uinfo;
3375

    
3376
            p = lock_user(arg1, sizeof(target_sigset_t), 1);
3377
            target_to_host_sigset(&set, p);
3378
            unlock_user(p, arg1, 0);
3379
            if (arg3) {
3380
                puts = &uts;
3381
                target_to_host_timespec(puts, arg3);
3382
            } else {
3383
                puts = NULL;
3384
            }
3385
            ret = get_errno(sigtimedwait(&set, &uinfo, puts));
3386
            if (!is_error(ret) && arg2) {
3387
                p = lock_user(arg2, sizeof(target_sigset_t), 0);
3388
                host_to_target_siginfo(p, &uinfo);
3389
                unlock_user(p, arg2, sizeof(target_sigset_t));
3390
            }
3391
        }
3392
        break;
3393
    case TARGET_NR_rt_sigqueueinfo:
3394
        {
3395
            siginfo_t uinfo;
3396
            p = lock_user(arg3, sizeof(target_sigset_t), 1);
3397
            target_to_host_siginfo(&uinfo, p);
3398
            unlock_user(p, arg1, 0);
3399
            ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3400
        }
3401
        break;
3402
#ifdef TARGET_NR_sigreturn
3403
    case TARGET_NR_sigreturn:
3404
        /* NOTE: ret is eax, so not transcoding must be done */
3405
        ret = do_sigreturn(cpu_env);
3406
        break;
3407
#endif
3408
    case TARGET_NR_rt_sigreturn:
3409
        /* NOTE: ret is eax, so not transcoding must be done */
3410
        ret = do_rt_sigreturn(cpu_env);
3411
        break;
3412
    case TARGET_NR_sethostname:
3413
        p = lock_user_string(arg1);
3414
        ret = get_errno(sethostname(p, arg2));
3415
        unlock_user(p, arg1, 0);
3416
        break;
3417
    case TARGET_NR_setrlimit:
3418
        {
3419
            /* XXX: convert resource ? */
3420
            int resource = arg1;
3421
            struct target_rlimit *target_rlim;
3422
            struct rlimit rlim;
3423
            lock_user_struct(target_rlim, arg2, 1);
3424
            rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3425
            rlim.rlim_max = tswapl(target_rlim->rlim_max);
3426
            unlock_user_struct(target_rlim, arg2, 0);
3427
            ret = get_errno(setrlimit(resource, &rlim));
3428
        }
3429
        break;
3430
    case TARGET_NR_getrlimit:
3431
        {
3432
            /* XXX: convert resource ? */
3433
            int resource = arg1;
3434
            struct target_rlimit *target_rlim;
3435
            struct rlimit rlim;
3436

    
3437
            ret = get_errno(getrlimit(resource, &rlim));
3438
            if (!is_error(ret)) {
3439
                lock_user_struct(target_rlim, arg2, 0);
3440
                rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3441
                rlim.rlim_max = tswapl(target_rlim->rlim_max);
3442
                unlock_user_struct(target_rlim, arg2, 1);
3443
            }
3444
        }
3445
        break;
3446
    case TARGET_NR_getrusage:
3447
        {
3448
            struct rusage rusage;
3449
            ret = get_errno(getrusage(arg1, &rusage));
3450
            if (!is_error(ret)) {
3451
                host_to_target_rusage(arg2, &rusage);
3452
            }
3453
        }
3454
        break;
3455
    case TARGET_NR_gettimeofday:
3456
        {
3457
            struct timeval tv;
3458
            ret = get_errno(gettimeofday(&tv, NULL));
3459
            if (!is_error(ret)) {
3460
                host_to_target_timeval(arg1, &tv);
3461
            }
3462
        }
3463
        break;
3464
    case TARGET_NR_settimeofday:
3465
        {
3466
            struct timeval tv;
3467
            target_to_host_timeval(&tv, arg1);
3468
            ret = get_errno(settimeofday(&tv, NULL));
3469
        }
3470
        break;
3471
#ifdef TARGET_NR_select
3472
    case TARGET_NR_select:
3473
        {
3474
            struct target_sel_arg_struct *sel;
3475
            abi_ulong inp, outp, exp, tvp;
3476
            long nsel;
3477

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

    
3700
            lock_user_struct(target_stfs, arg2, 0);
3701
            /* ??? put_user is probably wrong.  */
3702
            put_user(stfs.f_type, &target_stfs->f_type);
3703
            put_user(stfs.f_bsize, &target_stfs->f_bsize);
3704
            put_user(stfs.f_blocks, &target_stfs->f_blocks);
3705
            put_user(stfs.f_bfree, &target_stfs->f_bfree);
3706
            put_user(stfs.f_bavail, &target_stfs->f_bavail);
3707
            put_user(stfs.f_files, &target_stfs->f_files);
3708
            put_user(stfs.f_ffree, &target_stfs->f_ffree);
3709
            put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3710
            put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3711
            put_user(stfs.f_namelen, &target_stfs->f_namelen);
3712
            unlock_user_struct(target_stfs, arg2, 1);
3713
        }
3714
        break;
3715
    case TARGET_NR_fstatfs:
3716
        ret = get_errno(fstatfs(arg1, &stfs));
3717
        goto convert_statfs;
3718
#ifdef TARGET_NR_statfs64
3719
    case TARGET_NR_statfs64:
3720
        p = lock_user_string(arg1);
3721
        ret = get_errno(statfs(path(p), &stfs));
3722
        unlock_user(p, arg1, 0);
3723
    convert_statfs64:
3724
        if (!is_error(ret)) {
3725
            struct target_statfs64 *target_stfs;
3726

    
3727
            lock_user_struct(target_stfs, arg3, 0);
3728
            /* ??? put_user is probably wrong.  */
3729
            put_user(stfs.f_type, &target_stfs->f_type);
3730
            put_user(stfs.f_bsize, &target_stfs->f_bsize);
3731
            put_user(stfs.f_blocks, &target_stfs->f_blocks);
3732
            put_user(stfs.f_bfree, &target_stfs->f_bfree);
3733
            put_user(stfs.f_bavail, &target_stfs->f_bavail);
3734
            put_user(stfs.f_files, &target_stfs->f_files);
3735
            put_user(stfs.f_ffree, &target_stfs->f_ffree);
3736
            put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3737
            put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3738
            put_user(stfs.f_namelen, &target_stfs->f_namelen);
3739
            unlock_user_struct(target_stfs, arg3, 0);
3740
        }
3741
        break;
3742
    case TARGET_NR_fstatfs64:
3743
        ret = get_errno(fstatfs(arg1, &stfs));
3744
        goto convert_statfs64;
3745
#endif
3746
#ifdef TARGET_NR_ioperm
3747
    case TARGET_NR_ioperm:
3748
        goto unimplemented;
3749
#endif
3750
#ifdef TARGET_NR_socketcall
3751
    case TARGET_NR_socketcall:
3752
        ret = do_socketcall(arg1, arg2);
3753
        break;
3754
#endif
3755
#ifdef TARGET_NR_accept
3756
    case TARGET_NR_accept:
3757
        ret = do_accept(arg1, arg2, arg3);
3758
        break;
3759
#endif
3760
#ifdef TARGET_NR_bind
3761
    case TARGET_NR_bind:
3762
        ret = do_bind(arg1, arg2, arg3);
3763
        break;
3764
#endif
3765
#ifdef TARGET_NR_connect
3766
    case TARGET_NR_connect:
3767
        ret = do_connect(arg1, arg2, arg3);
3768
        break;
3769
#endif
3770
#ifdef TARGET_NR_getpeername
3771
    case TARGET_NR_getpeername:
3772
        ret = do_getpeername(arg1, arg2, arg3);
3773
        break;
3774
#endif
3775
#ifdef TARGET_NR_getsockname
3776
    case TARGET_NR_getsockname:
3777
        ret = do_getsockname(arg1, arg2, arg3);
3778
        break;
3779
#endif
3780
#ifdef TARGET_NR_getsockopt
3781
    case TARGET_NR_getsockopt:
3782
        ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3783
        break;
3784
#endif
3785
#ifdef TARGET_NR_listen
3786
    case TARGET_NR_listen:
3787
        ret = get_errno(listen(arg1, arg2));
3788
        break;
3789
#endif
3790
#ifdef TARGET_NR_recv
3791
    case TARGET_NR_recv:
3792
        ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3793
        break;
3794
#endif
3795
#ifdef TARGET_NR_recvfrom
3796
    case TARGET_NR_recvfrom:
3797
        ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3798
        break;
3799
#endif
3800
#ifdef TARGET_NR_recvmsg
3801
    case TARGET_NR_recvmsg:
3802
        ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3803
        break;
3804
#endif
3805
#ifdef TARGET_NR_send
3806
    case TARGET_NR_send:
3807
        ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3808
        break;
3809
#endif
3810
#ifdef TARGET_NR_sendmsg
3811
    case TARGET_NR_sendmsg:
3812
        ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3813
        break;
3814
#endif
3815
#ifdef TARGET_NR_sendto
3816
    case TARGET_NR_sendto:
3817
        ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3818
        break;
3819
#endif
3820
#ifdef TARGET_NR_shutdown
3821
    case TARGET_NR_shutdown:
3822
        ret = get_errno(shutdown(arg1, arg2));
3823
        break;
3824
#endif
3825
#ifdef TARGET_NR_socket
3826
    case TARGET_NR_socket:
3827
        ret = do_socket(arg1, arg2, arg3);
3828
        break;
3829
#endif
3830
#ifdef TARGET_NR_socketpair
3831
    case TARGET_NR_socketpair:
3832
        ret = do_socketpair(arg1, arg2, arg3, arg4);
3833
        break;
3834
#endif
3835
#ifdef TARGET_NR_setsockopt
3836
    case TARGET_NR_setsockopt:
3837
        ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3838
        break;
3839
#endif
3840

    
3841
    case TARGET_NR_syslog:
3842
        p = lock_user_string(arg2);
3843
        ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3844
        unlock_user(p, arg2, 0);
3845
        break;
3846

    
3847
    case TARGET_NR_setitimer:
3848
        {
3849
            struct itimerval value, ovalue, *pvalue;
3850

    
3851
            if (arg2) {
3852
                pvalue = &value;
3853
                target_to_host_timeval(&pvalue->it_interval,
3854
                                       arg2);
3855
                target_to_host_timeval(&pvalue->it_value,
3856
                                       arg2 + sizeof(struct target_timeval));
3857
            } else {
3858
                pvalue = NULL;
3859
            }
3860
            ret = get_errno(setitimer(arg1, pvalue, &ovalue));
3861
            if (!is_error(ret) && arg3) {
3862
                host_to_target_timeval(arg3,
3863
                                       &ovalue.it_interval);
3864
                host_to_target_timeval(arg3 + sizeof(struct target_timeval),
3865
                                       &ovalue.it_value);
3866
            }
3867
        }
3868
        break;
3869
    case TARGET_NR_getitimer:
3870
        {
3871
            struct itimerval value;
3872

    
3873
            ret = get_errno(getitimer(arg1, &value));
3874
            if (!is_error(ret) && arg2) {
3875
                host_to_target_timeval(arg2,
3876
                                       &value.it_interval);
3877
                host_to_target_timeval(arg2 + sizeof(struct target_timeval),
3878
                                       &value.it_value);
3879
            }
3880
        }
3881
        break;
3882
    case TARGET_NR_stat:
3883
        p = lock_user_string(arg1);
3884
        ret = get_errno(stat(path(p), &st));
3885
        unlock_user(p, arg1, 0);
3886
        goto do_stat;
3887
    case TARGET_NR_lstat:
3888
        p = lock_user_string(arg1);
3889
        ret = get_errno(lstat(path(p), &st));
3890
        unlock_user(p, arg1, 0);
3891
        goto do_stat;
3892
    case TARGET_NR_fstat:
3893
        {
3894
            ret = get_errno(fstat(arg1, &st));
3895
        do_stat:
3896
            if (!is_error(ret)) {
3897
                struct target_stat *target_st;
3898

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

    
4042
            lock_user_struct(buf, arg1, 0);
4043
            ret = get_errno(sys_uname(buf));
4044
            if (!is_error(ret)) {
4045
                /* Overrite the native machine name with whatever is being
4046
                   emulated. */
4047
                strcpy (buf->machine, UNAME_MACHINE);
4048
                /* Allow the user to override the reported release.  */
4049
                if (qemu_uname_release && *qemu_uname_release)
4050
                  strcpy (buf->release, qemu_uname_release);
4051
            }
4052
            unlock_user_struct(buf, arg1, 1);
4053
        }
4054
        break;
4055
#ifdef TARGET_I386
4056
    case TARGET_NR_modify_ldt:
4057
        ret = get_errno(do_modify_ldt(cpu_env, arg1, arg2, arg3));
4058
        break;
4059
#if !defined(TARGET_X86_64)
4060
    case TARGET_NR_vm86old:
4061
        goto unimplemented;
4062
    case TARGET_NR_vm86:
4063
        ret = do_vm86(cpu_env, arg1, arg2);
4064
        break;
4065
#endif
4066
#endif
4067
    case TARGET_NR_adjtimex:
4068
        goto unimplemented;
4069
#ifdef TARGET_NR_create_module
4070
    case TARGET_NR_create_module:
4071
#endif
4072
    case TARGET_NR_init_module:
4073
    case TARGET_NR_delete_module:
4074
#ifdef TARGET_NR_get_kernel_syms
4075
    case TARGET_NR_get_kernel_syms:
4076
#endif
4077
        goto unimplemented;
4078
    case TARGET_NR_quotactl:
4079
        goto unimplemented;
4080
    case TARGET_NR_getpgid:
4081
        ret = get_errno(getpgid(arg1));
4082
        break;
4083
    case TARGET_NR_fchdir:
4084
        ret = get_errno(fchdir(arg1));
4085
        break;
4086
#ifdef TARGET_NR_bdflush /* not on x86_64 */
4087
    case TARGET_NR_bdflush:
4088
        goto unimplemented;
4089
#endif
4090
#ifdef TARGET_NR_sysfs
4091
    case TARGET_NR_sysfs:
4092
        goto unimplemented;
4093
#endif
4094
    case TARGET_NR_personality:
4095
        ret = get_errno(personality(arg1));
4096
        break;
4097
#ifdef TARGET_NR_afs_syscall
4098
    case TARGET_NR_afs_syscall:
4099
        goto unimplemented;
4100
#endif
4101
#ifdef TARGET_NR__llseek /* Not on alpha */
4102
    case TARGET_NR__llseek:
4103
        {
4104
#if defined (__x86_64__)
4105
            ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
4106
            tput64(arg4, ret);
4107
#else
4108
            int64_t res;
4109
            ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4110
            tput64(arg4, res);
4111
#endif
4112
        }
4113
        break;
4114
#endif
4115
    case TARGET_NR_getdents:
4116
#if TARGET_ABI_BITS != 32
4117
        goto unimplemented;
4118
#warning not supported
4119
#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4120
        {
4121
            struct target_dirent *target_dirp;
4122
            struct dirent *dirp;
4123
            abi_long count = arg3;
4124

    
4125
            dirp = malloc(count);
4126
            if (!dirp) {
4127
                ret = -TARGET_EFAULT;
4128
                goto fail;
4129
            }
4130

    
4131
            ret = get_errno(sys_getdents(arg1, dirp, count));
4132
            if (!is_error(ret)) {
4133
                struct dirent *de;
4134
                struct target_dirent *tde;
4135
                int len = ret;
4136
                int reclen, treclen;
4137
                int count1, tnamelen;
4138

    
4139
                count1 = 0;
4140
                de = dirp;
4141
                target_dirp = lock_user(arg2, count, 0);
4142
                tde = target_dirp;
4143
                while (len > 0) {
4144
                    reclen = de->d_reclen;
4145
                    treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4146
                    tde->d_reclen = tswap16(treclen);
4147
                    tde->d_ino = tswapl(de->d_ino);
4148
                    tde->d_off = tswapl(de->d_off);
4149
                    tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4150
                    if (tnamelen > 256)
4151
                        tnamelen = 256;
4152
                    /* XXX: may not be correct */
4153
                    strncpy(tde->d_name, de->d_name, tnamelen);
4154
                    de = (struct dirent *)((char *)de + reclen);
4155
                    len -= reclen;
4156
                    tde = (struct target_dirent *)((char *)tde + treclen);
4157
                    count1 += treclen;
4158
                }
4159
                ret = count1;
4160
            }
4161
            unlock_user(target_dirp, arg2, ret);
4162
            free(dirp);
4163
        }
4164
#else
4165
        {
4166
            struct dirent *dirp;
4167
            abi_long count = arg3;
4168

    
4169
            dirp = lock_user(arg2, count, 0);
4170
            ret = get_errno(sys_getdents(arg1, dirp, count));
4171
            if (!is_error(ret)) {
4172
                struct dirent *de;
4173
                int len = ret;
4174
                int reclen;
4175
                de = dirp;
4176
                while (len > 0) {
4177
                    reclen = de->d_reclen;
4178
                    if (reclen > len)
4179
                        break;
4180
                    de->d_reclen = tswap16(reclen);
4181
                    tswapls(&de->d_ino);
4182
                    tswapls(&de->d_off);
4183
                    de = (struct dirent *)((char *)de + reclen);
4184
                    len -= reclen;
4185
                }
4186
            }
4187
            unlock_user(dirp, arg2, ret);
4188
        }
4189
#endif
4190
        break;
4191
#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
4192
    case TARGET_NR_getdents64:
4193
        {
4194
            struct dirent64 *dirp;
4195
            abi_long count = arg3;
4196
            dirp = lock_user(arg2, count, 0);
4197
            ret = get_errno(sys_getdents64(arg1, dirp, count));
4198
            if (!is_error(ret)) {
4199
                struct dirent64 *de;
4200
                int len = ret;
4201
                int reclen;
4202
                de = dirp;
4203
                while (len > 0) {
4204
                    reclen = de->d_reclen;
4205
                    if (reclen > len)
4206
                        break;
4207
                    de->d_reclen = tswap16(reclen);
4208
                    tswap64s(&de->d_ino);
4209
                    tswap64s(&de->d_off);
4210
                    de = (struct dirent64 *)((char *)de + reclen);
4211
                    len -= reclen;
4212
                }
4213
            }
4214
            unlock_user(dirp, arg2, ret);
4215
        }
4216
        break;
4217
#endif /* TARGET_NR_getdents64 */
4218
#ifdef TARGET_NR__newselect
4219
    case TARGET_NR__newselect:
4220
        ret = do_select(arg1, arg2, arg3, arg4, arg5);
4221
        break;
4222
#endif
4223
#ifdef TARGET_NR_poll
4224
    case TARGET_NR_poll:
4225
        {
4226
            struct target_pollfd *target_pfd;
4227
            unsigned int nfds = arg2;
4228
            int timeout = arg3;
4229
            struct pollfd *pfd;
4230
            unsigned int i;
4231

    
4232
            target_pfd = lock_user(arg1, sizeof(struct target_pollfd) * nfds, 1);
4233
            pfd = alloca(sizeof(struct pollfd) * nfds);
4234
            for(i = 0; i < nfds; i++) {
4235
                pfd[i].fd = tswap32(target_pfd[i].fd);
4236
                pfd[i].events = tswap16(target_pfd[i].events);
4237
            }
4238
            ret = get_errno(poll(pfd, nfds, timeout));
4239
            if (!is_error(ret)) {
4240
                for(i = 0; i < nfds; i++) {
4241
                    target_pfd[i].revents = tswap16(pfd[i].revents);
4242
                }
4243
                ret += nfds * (sizeof(struct target_pollfd)
4244
                               - sizeof(struct pollfd));
4245
            }
4246
            unlock_user(target_pfd, arg1, ret);
4247
        }
4248
        break;
4249
#endif
4250
    case TARGET_NR_flock:
4251
        /* NOTE: the flock constant seems to be the same for every
4252
           Linux platform */
4253
        ret = get_errno(flock(arg1, arg2));
4254
        break;
4255
    case TARGET_NR_readv:
4256
        {
4257
            int count = arg3;
4258
            struct iovec *vec;
4259

    
4260
            vec = alloca(count * sizeof(struct iovec));
4261
            lock_iovec(vec, arg2, count, 0);
4262
            ret = get_errno(readv(arg1, vec, count));
4263
            unlock_iovec(vec, arg2, count, 1);
4264
        }
4265
        break;
4266
    case TARGET_NR_writev:
4267
        {
4268
            int count = arg3;
4269
            struct iovec *vec;
4270

    
4271
            vec = alloca(count * sizeof(struct iovec));
4272
            lock_iovec(vec, arg2, count, 1);
4273
            ret = get_errno(writev(arg1, vec, count));
4274
            unlock_iovec(vec, arg2, count, 0);
4275
        }
4276
        break;
4277
    case TARGET_NR_getsid:
4278
        ret = get_errno(getsid(arg1));
4279
        break;
4280
#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
4281
    case TARGET_NR_fdatasync:
4282
        ret = get_errno(fdatasync(arg1));
4283
        break;
4284
#endif
4285
    case TARGET_NR__sysctl:
4286
        /* We don't implement this, but ENOTDIR is always a safe
4287
           return value. */
4288
        ret = -TARGET_ENOTDIR;
4289
        break;
4290
    case TARGET_NR_sched_setparam:
4291
        {
4292
            struct sched_param *target_schp;
4293
            struct sched_param schp;
4294

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

    
4557
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4558
            ret = get_errno(getgroups(gidsetsize, grouplist));
4559
            if (!is_error(ret)) {
4560
                target_grouplist = lock_user(arg2, gidsetsize * 2, 0);
4561
                for(i = 0;i < gidsetsize; i++)
4562
                    target_grouplist[i] = tswap16(grouplist[i]);
4563
                unlock_user(target_grouplist, arg2, gidsetsize * 2);
4564
            }
4565
        }
4566
        break;
4567
    case TARGET_NR_setgroups:
4568
        {
4569
            int gidsetsize = arg1;
4570
            uint16_t *target_grouplist;
4571
            gid_t *grouplist;
4572
            int i;
4573

    
4574
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4575
            target_grouplist = lock_user(arg2, gidsetsize * 2, 1);
4576
            for(i = 0;i < gidsetsize; i++)
4577
                grouplist[i] = tswap16(target_grouplist[i]);
4578
            unlock_user(target_grouplist, arg2, 0);
4579
            ret = get_errno(setgroups(gidsetsize, grouplist));
4580
        }
4581
        break;
4582
    case TARGET_NR_fchown:
4583
        ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4584
        break;
4585
#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4586
    case TARGET_NR_fchownat:
4587
        if (!arg2) {
4588
            ret = -TARGET_EFAULT;
4589
            goto fail;
4590
        }
4591
        p = lock_user_string(arg2);
4592
        if (!access_ok(VERIFY_READ, p, 1))
4593
            /* Don't "goto fail" so that cleanup can happen. */
4594
            ret = -TARGET_EFAULT;
4595
        else
4596
            ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4597
        if (p)
4598
            unlock_user(p, arg2, 0);
4599
        break;
4600
#endif
4601
#ifdef TARGET_NR_setresuid
4602
    case TARGET_NR_setresuid:
4603
        ret = get_errno(setresuid(low2highuid(arg1),
4604
                                  low2highuid(arg2),
4605
                                  low2highuid(arg3)));
4606
        break;
4607
#endif
4608
#ifdef TARGET_NR_getresuid
4609
    case TARGET_NR_getresuid:
4610
        {
4611
            uid_t ruid, euid, suid;
4612
            ret = get_errno(getresuid(&ruid, &euid, &suid));
4613
            if (!is_error(ret)) {
4614
                tput16(arg1, tswap16(high2lowuid(ruid)));
4615
                tput16(arg2, tswap16(high2lowuid(euid)));
4616
                tput16(arg3, tswap16(high2lowuid(suid)));
4617
            }
4618
        }
4619
        break;
4620
#endif
4621
#ifdef TARGET_NR_getresgid
4622
    case TARGET_NR_setresgid:
4623
        ret = get_errno(setresgid(low2highgid(arg1),
4624
                                  low2highgid(arg2),
4625
                                  low2highgid(arg3)));
4626
        break;
4627
#endif
4628
#ifdef TARGET_NR_getresgid
4629
    case TARGET_NR_getresgid:
4630
        {
4631
            gid_t rgid, egid, sgid;
4632
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
4633
            if (!is_error(ret)) {
4634
                tput16(arg1, tswap16(high2lowgid(rgid)));
4635
                tput16(arg2, tswap16(high2lowgid(egid)));
4636
                tput16(arg3, tswap16(high2lowgid(sgid)));
4637
            }
4638
        }
4639
        break;
4640
#endif
4641
    case TARGET_NR_chown:
4642
        p = lock_user_string(arg1);
4643
        ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4644
        unlock_user(p, arg1, 0);
4645
        break;
4646
    case TARGET_NR_setuid:
4647
        ret = get_errno(setuid(low2highuid(arg1)));
4648
        break;
4649
    case TARGET_NR_setgid:
4650
        ret = get_errno(setgid(low2highgid(arg1)));
4651
        break;
4652
    case TARGET_NR_setfsuid:
4653
        ret = get_errno(setfsuid(arg1));
4654
        break;
4655
    case TARGET_NR_setfsgid:
4656
        ret = get_errno(setfsgid(arg1));
4657
        break;
4658
#endif /* USE_UID16 */
4659

    
4660
#ifdef TARGET_NR_lchown32
4661
    case TARGET_NR_lchown32:
4662
        p = lock_user_string(arg1);
4663
        ret = get_errno(lchown(p, arg2, arg3));
4664
        unlock_user(p, arg1, 0);
4665
        break;
4666
#endif
4667
#ifdef TARGET_NR_getuid32
4668
    case TARGET_NR_getuid32:
4669
        ret = get_errno(getuid());
4670
        break;
4671
#endif
4672
#ifdef TARGET_NR_getgid32
4673
    case TARGET_NR_getgid32:
4674
        ret = get_errno(getgid());
4675
        break;
4676
#endif
4677
#ifdef TARGET_NR_geteuid32
4678
    case TARGET_NR_geteuid32:
4679
        ret = get_errno(geteuid());
4680
        break;
4681
#endif
4682
#ifdef TARGET_NR_getegid32
4683
    case TARGET_NR_getegid32:
4684
        ret = get_errno(getegid());
4685
        break;
4686
#endif
4687
#ifdef TARGET_NR_setreuid32
4688
    case TARGET_NR_setreuid32:
4689
        ret = get_errno(setreuid(arg1, arg2));
4690
        break;
4691
#endif
4692
#ifdef TARGET_NR_setregid32
4693
    case TARGET_NR_setregid32:
4694
        ret = get_errno(setregid(arg1, arg2));
4695
        break;
4696
#endif
4697
#ifdef TARGET_NR_getgroups32
4698
    case TARGET_NR_getgroups32:
4699
        {
4700
            int gidsetsize = arg1;
4701
            uint32_t *target_grouplist;
4702
            gid_t *grouplist;
4703
            int i;
4704

    
4705
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4706
            ret = get_errno(getgroups(gidsetsize, grouplist));
4707
            if (!is_error(ret)) {
4708
                target_grouplist = lock_user(arg2, gidsetsize * 4, 0);
4709
                for(i = 0;i < gidsetsize; i++)
4710
                    target_grouplist[i] = tswap32(grouplist[i]);
4711
                unlock_user(target_grouplist, arg2, gidsetsize * 4);
4712
            }
4713
        }
4714
        break;
4715
#endif
4716
#ifdef TARGET_NR_setgroups32
4717
    case TARGET_NR_setgroups32:
4718
        {
4719
            int gidsetsize = arg1;
4720
            uint32_t *target_grouplist;
4721
            gid_t *grouplist;
4722
            int i;
4723

    
4724
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4725
            target_grouplist = lock_user(arg2, gidsetsize * 4, 1);
4726
            for(i = 0;i < gidsetsize; i++)
4727
                grouplist[i] = tswap32(target_grouplist[i]);
4728
            unlock_user(target_grouplist, arg2, 0);
4729
            ret = get_errno(setgroups(gidsetsize, grouplist));
4730
        }
4731
        break;
4732
#endif
4733
#ifdef TARGET_NR_fchown32
4734
    case TARGET_NR_fchown32:
4735
        ret = get_errno(fchown(arg1, arg2, arg3));
4736
        break;
4737
#endif
4738
#ifdef TARGET_NR_setresuid32
4739
    case TARGET_NR_setresuid32:
4740
        ret = get_errno(setresuid(arg1, arg2, arg3));
4741
        break;
4742
#endif
4743
#ifdef TARGET_NR_getresuid32
4744
    case TARGET_NR_getresuid32:
4745
        {
4746
            uid_t ruid, euid, suid;
4747
            ret = get_errno(getresuid(&ruid, &euid, &suid));
4748
            if (!is_error(ret)) {
4749
                tput32(arg1, tswap32(ruid));
4750
                tput32(arg2, tswap32(euid));
4751
                tput32(arg3, tswap32(suid));
4752
            }
4753
        }
4754
        break;
4755
#endif
4756
#ifdef TARGET_NR_setresgid32
4757
    case TARGET_NR_setresgid32:
4758
        ret = get_errno(setresgid(arg1, arg2, arg3));
4759
        break;
4760
#endif
4761
#ifdef TARGET_NR_getresgid32
4762
    case TARGET_NR_getresgid32:
4763
        {
4764
            gid_t rgid, egid, sgid;
4765
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
4766
            if (!is_error(ret)) {
4767
                tput32(arg1, tswap32(rgid));
4768
                tput32(arg2, tswap32(egid));
4769
                tput32(arg3, tswap32(sgid));
4770
            }
4771
        }
4772
        break;
4773
#endif
4774
#ifdef TARGET_NR_chown32
4775
    case TARGET_NR_chown32:
4776
        p = lock_user_string(arg1);
4777
        ret = get_errno(chown(p, arg2, arg3));
4778
        unlock_user(p, arg1, 0);
4779
        break;
4780
#endif
4781
#ifdef TARGET_NR_setuid32
4782
    case TARGET_NR_setuid32:
4783
        ret = get_errno(setuid(arg1));
4784
        break;
4785
#endif
4786
#ifdef TARGET_NR_setgid32
4787
    case TARGET_NR_setgid32:
4788
        ret = get_errno(setgid(arg1));
4789
        break;
4790
#endif
4791
#ifdef TARGET_NR_setfsuid32
4792
    case TARGET_NR_setfsuid32:
4793
        ret = get_errno(setfsuid(arg1));
4794
        break;
4795
#endif
4796
#ifdef TARGET_NR_setfsgid32
4797
    case TARGET_NR_setfsgid32:
4798
        ret = get_errno(setfsgid(arg1));
4799
        break;
4800
#endif
4801

    
4802
    case TARGET_NR_pivot_root:
4803
        goto unimplemented;
4804
#ifdef TARGET_NR_mincore
4805
    case TARGET_NR_mincore:
4806
        goto unimplemented;
4807
#endif
4808
#ifdef TARGET_NR_madvise
4809
    case TARGET_NR_madvise:
4810
        /* A straight passthrough may not be safe because qemu sometimes
4811
           turns private flie-backed mappings into anonymous mappings.
4812
           This will break MADV_DONTNEED.
4813
           This is a hint, so ignoring and returning success is ok.  */
4814
        ret = get_errno(0);
4815
        break;
4816
#endif
4817
#if TARGET_ABI_BITS == 32
4818
    case TARGET_NR_fcntl64:
4819
    {
4820
        int cmd;
4821
        struct flock64 fl;
4822
        struct target_flock64 *target_fl;
4823
#ifdef TARGET_ARM
4824
        struct target_eabi_flock64 *target_efl;
4825
#endif
4826

    
4827
        switch(arg2){
4828
        case TARGET_F_GETLK64:
4829
            cmd = F_GETLK64;
4830
            break;
4831
        case TARGET_F_SETLK64:
4832
            cmd = F_SETLK64;
4833
            break;
4834
        case TARGET_F_SETLKW64:
4835
            cmd = F_SETLK64;
4836
            break;
4837
        default:
4838
            cmd = arg2;
4839
            break;
4840
        }
4841

    
4842
        switch(arg2) {
4843
        case TARGET_F_GETLK64:
4844
#ifdef TARGET_ARM
4845
            if (((CPUARMState *)cpu_env)->eabi) {
4846
                lock_user_struct(target_efl, arg3, 1);
4847
                fl.l_type = tswap16(target_efl->l_type);
4848
                fl.l_whence = tswap16(target_efl->l_whence);
4849
                fl.l_start = tswap64(target_efl->l_start);
4850
                fl.l_len = tswap64(target_efl->l_len);
4851
                fl.l_pid = tswapl(target_efl->l_pid);
4852
                unlock_user_struct(target_efl, arg3, 0);
4853
            } else
4854
#endif
4855
            {
4856
                lock_user_struct(target_fl, arg3, 1);
4857
                fl.l_type = tswap16(target_fl->l_type);
4858
                fl.l_whence = tswap16(target_fl->l_whence);
4859
                fl.l_start = tswap64(target_fl->l_start);
4860
                fl.l_len = tswap64(target_fl->l_len);
4861
                fl.l_pid = tswapl(target_fl->l_pid);
4862
                unlock_user_struct(target_fl, arg3, 0);
4863
            }
4864
            ret = get_errno(fcntl(arg1, cmd, &fl));
4865
            if (ret == 0) {
4866
#ifdef TARGET_ARM
4867
                if (((CPUARMState *)cpu_env)->eabi) {
4868
                    lock_user_struct(target_efl, arg3, 0);
4869
                    target_efl->l_type = tswap16(fl.l_type);
4870
                    target_efl->l_whence = tswap16(fl.l_whence);
4871
                    target_efl->l_start = tswap64(fl.l_start);
4872
                    target_efl->l_len = tswap64(fl.l_len);
4873
                    target_efl->l_pid = tswapl(fl.l_pid);
4874
                    unlock_user_struct(target_efl, arg3, 1);
4875
                } else
4876
#endif
4877
                {
4878
                    lock_user_struct(target_fl, arg3, 0);
4879
                    target_fl->l_type = tswap16(fl.l_type);
4880
                    target_fl->l_whence = tswap16(fl.l_whence);
4881
                    target_fl->l_start = tswap64(fl.l_start);
4882
                    target_fl->l_len = tswap64(fl.l_len);
4883
                    target_fl->l_pid = tswapl(fl.l_pid);
4884
                    unlock_user_struct(target_fl, arg3, 1);
4885
                }
4886
            }
4887
            break;
4888

    
4889
        case TARGET_F_SETLK64:
4890
        case TARGET_F_SETLKW64:
4891
#ifdef TARGET_ARM
4892
            if (((CPUARMState *)cpu_env)->eabi) {
4893
                lock_user_struct(target_efl, arg3, 1);
4894
                fl.l_type = tswap16(target_efl->l_type);
4895
                fl.l_whence = tswap16(target_efl->l_whence);
4896
                fl.l_start = tswap64(target_efl->l_start);
4897
                fl.l_len = tswap64(target_efl->l_len);
4898
                fl.l_pid = tswapl(target_efl->l_pid);
4899
                unlock_user_struct(target_efl, arg3, 0);
4900
            } else
4901
#endif
4902
            {
4903
                lock_user_struct(target_fl, arg3, 1);
4904
                fl.l_type = tswap16(target_fl->l_type);
4905
                fl.l_whence = tswap16(target_fl->l_whence);
4906
                fl.l_start = tswap64(target_fl->l_start);
4907
                fl.l_len = tswap64(target_fl->l_len);
4908
                fl.l_pid = tswapl(target_fl->l_pid);
4909
                unlock_user_struct(target_fl, arg3, 0);
4910
            }
4911
            ret = get_errno(fcntl(arg1, cmd, &fl));
4912
            break;
4913
        default:
4914
            ret = get_errno(do_fcntl(arg1, cmd, arg3));
4915
            break;
4916
        }
4917
        break;
4918
    }
4919
#endif
4920
#ifdef TARGET_NR_cacheflush
4921
    case TARGET_NR_cacheflush:
4922
        /* self-modifying code is handled automatically, so nothing needed */
4923
        ret = 0;
4924
        break;
4925
#endif
4926
#ifdef TARGET_NR_security
4927
    case TARGET_NR_security:
4928
        goto unimplemented;
4929
#endif
4930
#ifdef TARGET_NR_getpagesize
4931
    case TARGET_NR_getpagesize:
4932
        ret = TARGET_PAGE_SIZE;
4933
        break;
4934
#endif
4935
    case TARGET_NR_gettid:
4936
        ret = get_errno(gettid());
4937
        break;
4938
#ifdef TARGET_NR_readahead
4939
    case TARGET_NR_readahead:
4940
        goto unimplemented;
4941
#endif
4942
#ifdef TARGET_NR_setxattr
4943
    case TARGET_NR_setxattr:
4944
    case TARGET_NR_lsetxattr:
4945
    case TARGET_NR_fsetxattr:
4946
    case TARGET_NR_getxattr:
4947
    case TARGET_NR_lgetxattr:
4948
    case TARGET_NR_fgetxattr:
4949
    case TARGET_NR_listxattr:
4950
    case TARGET_NR_llistxattr:
4951
    case TARGET_NR_flistxattr:
4952
    case TARGET_NR_removexattr:
4953
    case TARGET_NR_lremovexattr:
4954
    case TARGET_NR_fremovexattr:
4955
        goto unimplemented_nowarn;
4956
#endif
4957
#ifdef TARGET_NR_set_thread_area
4958
    case TARGET_NR_set_thread_area:
4959
#ifdef TARGET_MIPS
4960
      ((CPUMIPSState *) cpu_env)->tls_value = arg1;
4961
      ret = 0;
4962
      break;
4963
#else
4964
      goto unimplemented_nowarn;
4965
#endif
4966
#endif
4967
#ifdef TARGET_NR_get_thread_area
4968
    case TARGET_NR_get_thread_area:
4969
        goto unimplemented_nowarn;
4970
#endif
4971
#ifdef TARGET_NR_getdomainname
4972
    case TARGET_NR_getdomainname:
4973
        goto unimplemented_nowarn;
4974
#endif
4975

    
4976
#ifdef TARGET_NR_clock_gettime
4977
    case TARGET_NR_clock_gettime:
4978
    {
4979
        struct timespec ts;
4980
        ret = get_errno(clock_gettime(arg1, &ts));
4981
        if (!is_error(ret)) {
4982
            host_to_target_timespec(arg2, &ts);
4983
        }
4984
        break;
4985
    }
4986
#endif
4987
#ifdef TARGET_NR_clock_getres
4988
    case TARGET_NR_clock_getres:
4989
    {
4990
        struct timespec ts;
4991
        ret = get_errno(clock_getres(arg1, &ts));
4992
        if (!is_error(ret)) {
4993
            host_to_target_timespec(arg2, &ts);
4994
        }
4995
        break;
4996
    }
4997
#endif
4998

    
4999
#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5000
    case TARGET_NR_set_tid_address:
5001
      ret = get_errno(set_tid_address((int *) arg1));
5002
      break;
5003
#endif
5004

    
5005
#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
5006
    case TARGET_NR_tkill:
5007
        ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5008
        break;
5009
#endif
5010

    
5011
#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
5012
    case TARGET_NR_tgkill:
5013
        ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5014
        break;
5015
#endif
5016

    
5017
#ifdef TARGET_NR_set_robust_list
5018
    case TARGET_NR_set_robust_list:
5019
        goto unimplemented_nowarn;