Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ eeeac3f3

History | View | Annotate | Download (163.3 kB)

1
/*
2
 *  Linux syscalls
3
 *
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 */
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <stdarg.h>
23
#include <string.h>
24
#include <elf.h>
25
#include <endian.h>
26
#include <errno.h>
27
#include <unistd.h>
28
#include <fcntl.h>
29
#include <time.h>
30
#include <sys/types.h>
31
#include <sys/ipc.h>
32
#include <sys/msg.h>
33
#include <sys/wait.h>
34
#include <sys/time.h>
35
#include <sys/stat.h>
36
#include <sys/mount.h>
37
#include <sys/prctl.h>
38
#include <sys/resource.h>
39
#include <sys/mman.h>
40
#include <sys/swap.h>
41
#include <signal.h>
42
#include <sched.h>
43
#include <sys/socket.h>
44
#include <sys/uio.h>
45
#include <sys/poll.h>
46
#include <sys/times.h>
47
#include <sys/shm.h>
48
#include <sys/sem.h>
49
#include <sys/statfs.h>
50
#include <utime.h>
51
#include <sys/sysinfo.h>
52
//#include <sys/user.h>
53
#include <netinet/ip.h>
54
#include <netinet/tcp.h>
55

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

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

    
72
#include "qemu.h"
73

    
74
//#define DEBUG
75

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

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

    
86

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

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

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

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

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

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

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

    
132

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

    
140

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

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

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

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

    
254
#define ERRNO_TABLE_SIZE 1200
255

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

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

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

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

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

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

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

    
405
static abi_ulong target_brk;
406
static abi_ulong target_original_brk;
407

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

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

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

    
425
    brk_page = HOST_PAGE_ALIGN(target_brk);
426

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

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

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

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

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

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

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

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

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

    
534
    return 0;
535
}
536

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

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

    
548
    return 0;
549
}
550

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

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

    
562
    return 0;
563
}
564

    
565

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

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

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

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

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

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

    
636
    return ret;
637
}
638

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

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

    
652
    return 0;
653
}
654

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

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

    
668
    return 0;
669
}
670

    
671
/* ??? Should this also swap msgh->name?  */
672
static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
673
                                           struct target_msghdr *target_msgh)
674
{
675
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
676
    abi_long msg_controllen;
677
    abi_ulong target_cmsg_addr;
678
    struct target_cmsghdr *target_cmsg;
679
    socklen_t space = 0;
680
    
681
    msg_controllen = tswapl(target_msgh->msg_controllen);
682
    if (msg_controllen < sizeof (struct target_cmsghdr)) 
683
        goto the_end;
684
    target_cmsg_addr = tswapl(target_msgh->msg_control);
685
    target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
686
    if (!target_cmsg)
687
        return -TARGET_EFAULT;
688

    
689
    while (cmsg && target_cmsg) {
690
        void *data = CMSG_DATA(cmsg);
691
        void *target_data = TARGET_CMSG_DATA(target_cmsg);
692

    
693
        int len = tswapl(target_cmsg->cmsg_len)
694
                  - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
695

    
696
        space += CMSG_SPACE(len);
697
        if (space > msgh->msg_controllen) {
698
            space -= CMSG_SPACE(len);
699
            gemu_log("Host cmsg overflow\n");
700
            break;
701
        }
702

    
703
        cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
704
        cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
705
        cmsg->cmsg_len = CMSG_LEN(len);
706

    
707
        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
708
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
709
            memcpy(data, target_data, len);
710
        } else {
711
            int *fd = (int *)data;
712
            int *target_fd = (int *)target_data;
713
            int i, numfds = len / sizeof(int);
714

    
715
            for (i = 0; i < numfds; i++)
716
                fd[i] = tswap32(target_fd[i]);
717
        }
718

    
719
        cmsg = CMSG_NXTHDR(msgh, cmsg);
720
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
721
    }
722
    unlock_user(target_cmsg, target_cmsg_addr, 0);
723
 the_end:
724
    msgh->msg_controllen = space;
725
    return 0;
726
}
727

    
728
/* ??? Should this also swap msgh->name?  */
729
static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
730
                                           struct msghdr *msgh)
731
{
732
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
733
    abi_long msg_controllen;
734
    abi_ulong target_cmsg_addr;
735
    struct target_cmsghdr *target_cmsg;
736
    socklen_t space = 0;
737

    
738
    msg_controllen = tswapl(target_msgh->msg_controllen);
739
    if (msg_controllen < sizeof (struct target_cmsghdr)) 
740
        goto the_end;
741
    target_cmsg_addr = tswapl(target_msgh->msg_control);
742
    target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
743
    if (!target_cmsg)
744
        return -TARGET_EFAULT;
745

    
746
    while (cmsg && target_cmsg) {
747
        void *data = CMSG_DATA(cmsg);
748
        void *target_data = TARGET_CMSG_DATA(target_cmsg);
749

    
750
        int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
751

    
752
        space += TARGET_CMSG_SPACE(len);
753
        if (space > msg_controllen) {
754
            space -= TARGET_CMSG_SPACE(len);
755
            gemu_log("Target cmsg overflow\n");
756
            break;
757
        }
758

    
759
        target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
760
        target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
761
        target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
762

    
763
        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
764
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
765
            memcpy(target_data, data, len);
766
        } else {
767
            int *fd = (int *)data;
768
            int *target_fd = (int *)target_data;
769
            int i, numfds = len / sizeof(int);
770

    
771
            for (i = 0; i < numfds; i++)
772
                target_fd[i] = tswap32(fd[i]);
773
        }
774

    
775
        cmsg = CMSG_NXTHDR(msgh, cmsg);
776
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
777
    }
778
    unlock_user(target_cmsg, target_cmsg_addr, space);
779
 the_end:
780
    target_msgh->msg_controllen = tswapl(space);
781
    return 0;
782
}
783

    
784
/* do_setsockopt() Must return target values and target errnos. */
785
static abi_long do_setsockopt(int sockfd, int level, int optname,
786
                              abi_ulong optval, socklen_t optlen)
787
{
788
    abi_long ret;
789
    int val;
790

    
791
    switch(level) {
792
    case SOL_TCP:
793
        /* TCP options all take an 'int' value.  */
794
        if (optlen < sizeof(uint32_t))
795
            return -TARGET_EINVAL;
796

    
797
        val = tget32(optval);
798
        ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
799
        break;
800
    case SOL_IP:
801
        switch(optname) {
802
        case IP_TOS:
803
        case IP_TTL:
804
        case IP_HDRINCL:
805
        case IP_ROUTER_ALERT:
806
        case IP_RECVOPTS:
807
        case IP_RETOPTS:
808
        case IP_PKTINFO:
809
        case IP_MTU_DISCOVER:
810
        case IP_RECVERR:
811
        case IP_RECVTOS:
812
#ifdef IP_FREEBIND
813
        case IP_FREEBIND:
814
#endif
815
        case IP_MULTICAST_TTL:
816
        case IP_MULTICAST_LOOP:
817
            val = 0;
818
            if (optlen >= sizeof(uint32_t)) {
819
                val = tget32(optval);
820
            } else if (optlen >= 1) {
821
                val = tget8(optval);
822
            }
823
            ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
824
            break;
825
        default:
826
            goto unimplemented;
827
        }
828
        break;
829
    case TARGET_SOL_SOCKET:
830
        switch (optname) {
831
            /* Options with 'int' argument.  */
832
        case TARGET_SO_DEBUG:
833
                optname = SO_DEBUG;
834
                break;
835
        case TARGET_SO_REUSEADDR:
836
                optname = SO_REUSEADDR;
837
                break;
838
        case TARGET_SO_TYPE:
839
                optname = SO_TYPE;
840
                break;
841
        case TARGET_SO_ERROR:
842
                optname = SO_ERROR;
843
                break;
844
        case TARGET_SO_DONTROUTE:
845
                optname = SO_DONTROUTE;
846
                break;
847
        case TARGET_SO_BROADCAST:
848
                optname = SO_BROADCAST;
849
                break;
850
        case TARGET_SO_SNDBUF:
851
                optname = SO_SNDBUF;
852
                break;
853
        case TARGET_SO_RCVBUF:
854
                optname = SO_RCVBUF;
855
                break;
856
        case TARGET_SO_KEEPALIVE:
857
                optname = SO_KEEPALIVE;
858
                break;
859
        case TARGET_SO_OOBINLINE:
860
                optname = SO_OOBINLINE;
861
                break;
862
        case TARGET_SO_NO_CHECK:
863
                optname = SO_NO_CHECK;
864
                break;
865
        case TARGET_SO_PRIORITY:
866
                optname = SO_PRIORITY;
867
                break;
868
#ifdef SO_BSDCOMPAT
869
        case TARGET_SO_BSDCOMPAT:
870
                optname = SO_BSDCOMPAT;
871
                break;
872
#endif
873
        case TARGET_SO_PASSCRED:
874
                optname = SO_PASSCRED;
875
                break;
876
        case TARGET_SO_TIMESTAMP:
877
                optname = SO_TIMESTAMP;
878
                break;
879
        case TARGET_SO_RCVLOWAT:
880
                optname = SO_RCVLOWAT;
881
                break;
882
        case TARGET_SO_RCVTIMEO:
883
                optname = SO_RCVTIMEO;
884
                break;
885
        case TARGET_SO_SNDTIMEO:
886
                optname = SO_SNDTIMEO;
887
                break;
888
            break;
889
        default:
890
            goto unimplemented;
891
        }
892
        if (optlen < sizeof(uint32_t))
893
        return -TARGET_EINVAL;
894

    
895
        val = tget32(optval);
896
        ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
897
        break;
898
    default:
899
    unimplemented:
900
        gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
901
        ret = -TARGET_ENOSYS;
902
    }
903
    return ret;
904
}
905

    
906
/* do_getsockopt() Must return target values and target errnos. */
907
static abi_long do_getsockopt(int sockfd, int level, int optname,
908
                              abi_ulong optval, abi_ulong optlen)
909
{
910
    abi_long ret;
911
    int len, lv, val;
912

    
913
    switch(level) {
914
    case TARGET_SOL_SOCKET:
915
            level = SOL_SOCKET;
916
        switch (optname) {
917
        case TARGET_SO_LINGER:
918
        case TARGET_SO_RCVTIMEO:
919
        case TARGET_SO_SNDTIMEO:
920
        case TARGET_SO_PEERCRED:
921
        case TARGET_SO_PEERNAME:
922
            /* These don't just return a single integer */
923
            goto unimplemented;
924
        default:
925
            goto int_case;
926
        }
927
        break;
928
    case SOL_TCP:
929
        /* TCP options all take an 'int' value.  */
930
    int_case:
931
        len = tget32(optlen);
932
        if (len < 0)
933
            return -TARGET_EINVAL;
934
        lv = sizeof(int);
935
        ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
936
        if (ret < 0)
937
            return ret;
938
        val = tswap32(val);
939
        if (len > lv)
940
            len = lv;
941
        if (len == 4)
942
            tput32(optval, val);
943
        else
944
            tput8(optval, val);
945
        tput32(optlen, len);
946
        break;
947
    case SOL_IP:
948
        switch(optname) {
949
        case IP_TOS:
950
        case IP_TTL:
951
        case IP_HDRINCL:
952
        case IP_ROUTER_ALERT:
953
        case IP_RECVOPTS:
954
        case IP_RETOPTS:
955
        case IP_PKTINFO:
956
        case IP_MTU_DISCOVER:
957
        case IP_RECVERR:
958
        case IP_RECVTOS:
959
#ifdef IP_FREEBIND
960
        case IP_FREEBIND:
961
#endif
962
        case IP_MULTICAST_TTL:
963
        case IP_MULTICAST_LOOP:
964
            len = tget32(optlen);
965
            if (len < 0)
966
                return -TARGET_EINVAL;
967
            lv = sizeof(int);
968
            ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
969
            if (ret < 0)
970
                return ret;
971
            if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
972
                len = 1;
973
                tput32(optlen, len);
974
                tput8(optval, val);
975
            } else {
976
                if (len > sizeof(int))
977
                    len = sizeof(int);
978
                tput32(optlen, len);
979
                tput32(optval, val);
980
            }
981
            break;
982
        default:
983
            goto unimplemented;
984
        }
985
        break;
986
    default:
987
    unimplemented:
988
        gemu_log("getsockopt level=%d optname=%d not yet supported\n",
989
                 level, optname);
990
        ret = -TARGET_ENOSYS;
991
        break;
992
    }
993
    return ret;
994
}
995

    
996
/* FIXME
997
 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
998
 * other lock functions have a return code of 0 for failure.
999
 */
1000
static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1001
                           int count, int copy)
1002
{
1003
    struct target_iovec *target_vec;
1004
    abi_ulong base;
1005
    int i, j;
1006

    
1007
    target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1008
    if (!target_vec)
1009
        return -TARGET_EFAULT;
1010
    for(i = 0;i < count; i++) {
1011
        base = tswapl(target_vec[i].iov_base);
1012
        vec[i].iov_len = tswapl(target_vec[i].iov_len);
1013
        vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1014
        if (!vec[i].iov_base) 
1015
            goto fail;
1016
    }
1017
    unlock_user (target_vec, target_addr, 0);
1018
    return 0;
1019
 fail:
1020
    /* failure - unwind locks */
1021
    for (j = 0; j < i; j++) {
1022
        base = tswapl(target_vec[j].iov_base);
1023
        unlock_user(vec[j].iov_base, base, 0);
1024
    }
1025
    unlock_user (target_vec, target_addr, 0);
1026
    return -TARGET_EFAULT;
1027
}
1028

    
1029
static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1030
                             int count, int copy)
1031
{
1032
    struct target_iovec *target_vec;
1033
    abi_ulong base;
1034
    int i;
1035

    
1036
    target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1037
    if (!target_vec)
1038
        return -TARGET_EFAULT;
1039
    for(i = 0;i < count; i++) {
1040
        base = tswapl(target_vec[i].iov_base);
1041
        unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1042
    }
1043
    unlock_user (target_vec, target_addr, 0);
1044

    
1045
    return 0;
1046
}
1047

    
1048
/* do_socket() Must return target values and target errnos. */
1049
static abi_long do_socket(int domain, int type, int protocol)
1050
{
1051
#if defined(TARGET_MIPS)
1052
    switch(type) {
1053
    case TARGET_SOCK_DGRAM:
1054
        type = SOCK_DGRAM;
1055
        break;
1056
    case TARGET_SOCK_STREAM:
1057
        type = SOCK_STREAM;
1058
        break;
1059
    case TARGET_SOCK_RAW:
1060
        type = SOCK_RAW;
1061
        break;
1062
    case TARGET_SOCK_RDM:
1063
        type = SOCK_RDM;
1064
        break;
1065
    case TARGET_SOCK_SEQPACKET:
1066
        type = SOCK_SEQPACKET;
1067
        break;
1068
    case TARGET_SOCK_PACKET:
1069
        type = SOCK_PACKET;
1070
        break;
1071
    }
1072
#endif
1073
    if (domain == PF_NETLINK)
1074
        return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1075
    return get_errno(socket(domain, type, protocol));
1076
}
1077

    
1078
/* do_bind() Must return target values and target errnos. */
1079
static abi_long do_bind(int sockfd, abi_ulong target_addr,
1080
                        socklen_t addrlen)
1081
{
1082
    void *addr = alloca(addrlen);
1083

    
1084
    target_to_host_sockaddr(addr, target_addr, addrlen);
1085
    return get_errno(bind(sockfd, addr, addrlen));
1086
}
1087

    
1088
/* do_connect() Must return target values and target errnos. */
1089
static abi_long do_connect(int sockfd, abi_ulong target_addr,
1090
                           socklen_t addrlen)
1091
{
1092
    void *addr = alloca(addrlen);
1093

    
1094
    target_to_host_sockaddr(addr, target_addr, addrlen);
1095
    return get_errno(connect(sockfd, addr, addrlen));
1096
}
1097

    
1098
/* do_sendrecvmsg() Must return target values and target errnos. */
1099
static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1100
                               int flags, int send)
1101
{
1102
    abi_long ret;
1103
    struct target_msghdr *msgp;
1104
    struct msghdr msg;
1105
    int count;
1106
    struct iovec *vec;
1107
    abi_ulong target_vec;
1108

    
1109
    /* FIXME */
1110
    if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1111
                          msgp,
1112
                          target_msg,
1113
                          send ? 1 : 0))
1114
        return -TARGET_EFAULT;
1115
    if (msgp->msg_name) {
1116
        msg.msg_namelen = tswap32(msgp->msg_namelen);
1117
        msg.msg_name = alloca(msg.msg_namelen);
1118
        target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1119
                                msg.msg_namelen);
1120
    } else {
1121
        msg.msg_name = NULL;
1122
        msg.msg_namelen = 0;
1123
    }
1124
    msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1125
    msg.msg_control = alloca(msg.msg_controllen);
1126
    msg.msg_flags = tswap32(msgp->msg_flags);
1127

    
1128
    count = tswapl(msgp->msg_iovlen);
1129
    vec = alloca(count * sizeof(struct iovec));
1130
    target_vec = tswapl(msgp->msg_iov);
1131
    lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1132
    msg.msg_iovlen = count;
1133
    msg.msg_iov = vec;
1134

    
1135
    if (send) {
1136
        ret = target_to_host_cmsg(&msg, msgp);
1137
        if (ret == 0)
1138
            ret = get_errno(sendmsg(fd, &msg, flags));
1139
    } else {
1140
        ret = get_errno(recvmsg(fd, &msg, flags));
1141
        if (!is_error(ret))
1142
            ret = host_to_target_cmsg(msgp, &msg);
1143
    }
1144
    unlock_iovec(vec, target_vec, count, !send);
1145
    unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1146
    return ret;
1147
}
1148

    
1149
/* do_accept() Must return target values and target errnos. */
1150
static abi_long do_accept(int fd, abi_ulong target_addr,
1151
                          abi_ulong target_addrlen)
1152
{
1153
    socklen_t addrlen = tget32(target_addrlen);
1154
    void *addr = alloca(addrlen);
1155
    abi_long ret;
1156

    
1157
    ret = get_errno(accept(fd, addr, &addrlen));
1158
    if (!is_error(ret)) {
1159
        host_to_target_sockaddr(target_addr, addr, addrlen);
1160
        tput32(target_addrlen, addrlen);
1161
    }
1162
    return ret;
1163
}
1164

    
1165
/* do_getpeername() Must return target values and target errnos. */
1166
static abi_long do_getpeername(int fd, abi_ulong target_addr,
1167
                               abi_ulong target_addrlen)
1168
{
1169
    socklen_t addrlen = tget32(target_addrlen);
1170
    void *addr = alloca(addrlen);
1171
    abi_long ret;
1172

    
1173
    ret = get_errno(getpeername(fd, addr, &addrlen));
1174
    if (!is_error(ret)) {
1175
        host_to_target_sockaddr(target_addr, addr, addrlen);
1176
        tput32(target_addrlen, addrlen);
1177
    }
1178
    return ret;
1179
}
1180

    
1181
/* do_getsockname() Must return target values and target errnos. */
1182
static abi_long do_getsockname(int fd, abi_ulong target_addr,
1183
                               abi_ulong target_addrlen)
1184
{
1185
    socklen_t addrlen = tget32(target_addrlen);
1186
    void *addr = alloca(addrlen);
1187
    abi_long ret;
1188

    
1189
    ret = get_errno(getsockname(fd, addr, &addrlen));
1190
    if (!is_error(ret)) {
1191
        host_to_target_sockaddr(target_addr, addr, addrlen);
1192
        tput32(target_addrlen, addrlen);
1193
    }
1194
    return ret;
1195
}
1196

    
1197
/* do_socketpair() Must return target values and target errnos. */
1198
static abi_long do_socketpair(int domain, int type, int protocol,
1199
                              abi_ulong target_tab)
1200
{
1201
    int tab[2];
1202
    abi_long ret;
1203

    
1204
    ret = get_errno(socketpair(domain, type, protocol, tab));
1205
    if (!is_error(ret)) {
1206
        tput32(target_tab, tab[0]);
1207
        tput32(target_tab + 4, tab[1]);
1208
    }
1209
    return ret;
1210
}
1211

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

    
1220
    host_msg = lock_user(VERIFY_READ, msg, len, 1);
1221
    if (!host_msg)
1222
        return -TARGET_EFAULT;
1223
    if (target_addr) {
1224
        addr = alloca(addrlen);
1225
        target_to_host_sockaddr(addr, target_addr, addrlen);
1226
        ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1227
    } else {
1228
        ret = get_errno(send(fd, host_msg, len, flags));
1229
    }
1230
    unlock_user(host_msg, msg, 0);
1231
    return ret;
1232
}
1233

    
1234
/* do_recvfrom() Must return target values and target errnos. */
1235
static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1236
                            abi_ulong target_addr,
1237
                            abi_ulong target_addrlen)
1238
{
1239
    socklen_t addrlen;
1240
    void *addr;
1241
    void *host_msg;
1242
    abi_long ret;
1243

    
1244
    host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1245
    if (!host_msg)
1246
        return -TARGET_EFAULT;
1247
    if (target_addr) {
1248
        addrlen = tget32(target_addrlen);
1249
        addr = alloca(addrlen);
1250
        ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1251
    } else {
1252
        addr = NULL; /* To keep compiler quiet.  */
1253
        ret = get_errno(recv(fd, host_msg, len, flags));
1254
    }
1255
    if (!is_error(ret)) {
1256
        if (target_addr) {
1257
            host_to_target_sockaddr(target_addr, addr, addrlen);
1258
            tput32(target_addrlen, addrlen);
1259
        }
1260
        unlock_user(host_msg, msg, len);
1261
    } else {
1262
        unlock_user(host_msg, msg, 0);
1263
    }
1264
    return ret;
1265
}
1266

    
1267
#ifdef TARGET_NR_socketcall
1268
/* do_socketcall() Must return target values and target errnos. */
1269
static abi_long do_socketcall(int num, abi_ulong vptr)
1270
{
1271
    abi_long ret;
1272
    const int n = sizeof(abi_ulong);
1273

    
1274
    switch(num) {
1275
    case SOCKOP_socket:
1276
        {
1277
            int domain = tgetl(vptr);
1278
            int type = tgetl(vptr + n);
1279
            int protocol = tgetl(vptr + 2 * n);
1280
            ret = do_socket(domain, type, protocol);
1281
        }
1282
        break;
1283
    case SOCKOP_bind:
1284
        {
1285
            int sockfd = tgetl(vptr);
1286
            abi_ulong target_addr = tgetl(vptr + n);
1287
            socklen_t addrlen = tgetl(vptr + 2 * n);
1288
            ret = do_bind(sockfd, target_addr, addrlen);
1289
        }
1290
        break;
1291
    case SOCKOP_connect:
1292
        {
1293
            int sockfd = tgetl(vptr);
1294
            abi_ulong target_addr = tgetl(vptr + n);
1295
            socklen_t addrlen = tgetl(vptr + 2 * n);
1296
            ret = do_connect(sockfd, target_addr, addrlen);
1297
        }
1298
        break;
1299
    case SOCKOP_listen:
1300
        {
1301
            int sockfd = tgetl(vptr);
1302
            int backlog = tgetl(vptr + n);
1303
            ret = get_errno(listen(sockfd, backlog));
1304
        }
1305
        break;
1306
    case SOCKOP_accept:
1307
        {
1308
            int sockfd = tgetl(vptr);
1309
            abi_ulong target_addr = tgetl(vptr + n);
1310
            abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1311
            ret = do_accept(sockfd, target_addr, target_addrlen);
1312
        }
1313
        break;
1314
    case SOCKOP_getsockname:
1315
        {
1316
            int sockfd = tgetl(vptr);
1317
            abi_ulong target_addr = tgetl(vptr + n);
1318
            abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1319
            ret = do_getsockname(sockfd, target_addr, target_addrlen);
1320
        }
1321
        break;
1322
    case SOCKOP_getpeername:
1323
        {
1324
            int sockfd = tgetl(vptr);
1325
            abi_ulong target_addr = tgetl(vptr + n);
1326
            abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1327
            ret = do_getpeername(sockfd, target_addr, target_addrlen);
1328
        }
1329
        break;
1330
    case SOCKOP_socketpair:
1331
        {
1332
            int domain = tgetl(vptr);
1333
            int type = tgetl(vptr + n);
1334
            int protocol = tgetl(vptr + 2 * n);
1335
            abi_ulong tab = tgetl(vptr + 3 * n);
1336
            ret = do_socketpair(domain, type, protocol, tab);
1337
        }
1338
        break;
1339
    case SOCKOP_send:
1340
        {
1341
            int sockfd = tgetl(vptr);
1342
            abi_ulong msg = tgetl(vptr + n);
1343
            size_t len = tgetl(vptr + 2 * n);
1344
            int flags = tgetl(vptr + 3 * n);
1345
            ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1346
        }
1347
        break;
1348
    case SOCKOP_recv:
1349
        {
1350
            int sockfd = tgetl(vptr);
1351
            abi_ulong msg = tgetl(vptr + n);
1352
            size_t len = tgetl(vptr + 2 * n);
1353
            int flags = tgetl(vptr + 3 * n);
1354
            ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1355
        }
1356
        break;
1357
    case SOCKOP_sendto:
1358
        {
1359
            int sockfd = tgetl(vptr);
1360
            abi_ulong msg = tgetl(vptr + n);
1361
            size_t len = tgetl(vptr + 2 * n);
1362
            int flags = tgetl(vptr + 3 * n);
1363
            abi_ulong addr = tgetl(vptr + 4 * n);
1364
            socklen_t addrlen = tgetl(vptr + 5 * n);
1365
            ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1366
        }
1367
        break;
1368
    case SOCKOP_recvfrom:
1369
        {
1370
            int sockfd = tgetl(vptr);
1371
            abi_ulong msg = tgetl(vptr + n);
1372
            size_t len = tgetl(vptr + 2 * n);
1373
            int flags = tgetl(vptr + 3 * n);
1374
            abi_ulong addr = tgetl(vptr + 4 * n);
1375
            abi_ulong addrlen = tgetl(vptr + 5 * n);
1376
            ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1377
        }
1378
        break;
1379
    case SOCKOP_shutdown:
1380
        {
1381
            int sockfd = tgetl(vptr);
1382
            int how = tgetl(vptr + n);
1383

    
1384
            ret = get_errno(shutdown(sockfd, how));
1385
        }
1386
        break;
1387
    case SOCKOP_sendmsg:
1388
    case SOCKOP_recvmsg:
1389
        {
1390
            int fd;
1391
            abi_ulong target_msg;
1392
            int flags;
1393

    
1394
            fd = tgetl(vptr);
1395
            target_msg = tgetl(vptr + n);
1396
            flags = tgetl(vptr + 2 * n);
1397

    
1398
            ret = do_sendrecvmsg(fd, target_msg, flags,
1399
                                 (num == SOCKOP_sendmsg));
1400
        }
1401
        break;
1402
    case SOCKOP_setsockopt:
1403
        {
1404
            int sockfd = tgetl(vptr);
1405
            int level = tgetl(vptr + n);
1406
            int optname = tgetl(vptr + 2 * n);
1407
            abi_ulong optval = tgetl(vptr + 3 * n);
1408
            socklen_t optlen = tgetl(vptr + 4 * n);
1409

    
1410
            ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1411
        }
1412
        break;
1413
    case SOCKOP_getsockopt:
1414
        {
1415
            int sockfd = tgetl(vptr);
1416
            int level = tgetl(vptr + n);
1417
            int optname = tgetl(vptr + 2 * n);
1418
            abi_ulong optval = tgetl(vptr + 3 * n);
1419
            abi_ulong poptlen = tgetl(vptr + 4 * n);
1420

    
1421
            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
1422
        }
1423
        break;
1424
    default:
1425
        gemu_log("Unsupported socketcall: %d\n", num);
1426
        ret = -TARGET_ENOSYS;
1427
        break;
1428
    }
1429
    return ret;
1430
}
1431
#endif
1432

    
1433
#ifdef TARGET_NR_ipc
1434
#define N_SHM_REGIONS        32
1435

    
1436
static struct shm_region {
1437
    abi_ulong        start;
1438
    abi_ulong        size;
1439
} shm_regions[N_SHM_REGIONS];
1440

    
1441
struct target_ipc_perm
1442
{
1443
    abi_long __key;
1444
    abi_ulong uid;
1445
    abi_ulong gid;
1446
    abi_ulong cuid;
1447
    abi_ulong cgid;
1448
    unsigned short int mode;
1449
    unsigned short int __pad1;
1450
    unsigned short int __seq;
1451
    unsigned short int __pad2;
1452
    abi_ulong __unused1;
1453
    abi_ulong __unused2;
1454
};
1455

    
1456
struct target_semid_ds
1457
{
1458
  struct target_ipc_perm sem_perm;
1459
  abi_ulong sem_otime;
1460
  abi_ulong __unused1;
1461
  abi_ulong sem_ctime;
1462
  abi_ulong __unused2;
1463
  abi_ulong sem_nsems;
1464
  abi_ulong __unused3;
1465
  abi_ulong __unused4;
1466
};
1467

    
1468
static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1469
                                               abi_ulong target_addr)
1470
{
1471
    struct target_ipc_perm *target_ip;
1472
    struct target_semid_ds *target_sd;
1473

    
1474
    if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1475
        return -TARGET_EFAULT;
1476
    target_ip=&(target_sd->sem_perm);
1477
    host_ip->__key = tswapl(target_ip->__key);
1478
    host_ip->uid = tswapl(target_ip->uid);
1479
    host_ip->gid = tswapl(target_ip->gid);
1480
    host_ip->cuid = tswapl(target_ip->cuid);
1481
    host_ip->cgid = tswapl(target_ip->cgid);
1482
    host_ip->mode = tswapl(target_ip->mode);
1483
    unlock_user_struct(target_sd, target_addr, 0);
1484
    return 0;
1485
}
1486

    
1487
static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1488
                                               struct ipc_perm *host_ip)
1489
{
1490
    struct target_ipc_perm *target_ip;
1491
    struct target_semid_ds *target_sd;
1492

    
1493
    if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1494
        return -TARGET_EFAULT;
1495
    target_ip = &(target_sd->sem_perm);
1496
    target_ip->__key = tswapl(host_ip->__key);
1497
    target_ip->uid = tswapl(host_ip->uid);
1498
    target_ip->gid = tswapl(host_ip->gid);
1499
    target_ip->cuid = tswapl(host_ip->cuid);
1500
    target_ip->cgid = tswapl(host_ip->cgid);
1501
    target_ip->mode = tswapl(host_ip->mode);
1502
    unlock_user_struct(target_sd, target_addr, 1);
1503
    return 0;
1504
}
1505

    
1506
static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1507
                                               abi_ulong target_addr)
1508
{
1509
    struct target_semid_ds *target_sd;
1510

    
1511
    if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1512
        return -TARGET_EFAULT;
1513
    target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1514
    host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1515
    host_sd->sem_otime = tswapl(target_sd->sem_otime);
1516
    host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1517
    unlock_user_struct(target_sd, target_addr, 0);
1518
    return 0;
1519
}
1520

    
1521
static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1522
                                               struct semid_ds *host_sd)
1523
{
1524
    struct target_semid_ds *target_sd;
1525

    
1526
    if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1527
        return -TARGET_EFAULT;
1528
    host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1529
    target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1530
    target_sd->sem_otime = tswapl(host_sd->sem_otime);
1531
    target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1532
    unlock_user_struct(target_sd, target_addr, 1);
1533
    return 0;
1534
}
1535

    
1536
union semun {
1537
        int val;
1538
        struct semid_ds *buf;
1539
        unsigned short *array;
1540
};
1541

    
1542
union target_semun {
1543
        int val;
1544
        abi_long buf;
1545
        unsigned short int *array;
1546
};
1547

    
1548
static inline abi_long target_to_host_semun(int cmd,
1549
                                            union semun *host_su,
1550
                                            abi_ulong target_addr,
1551
                                            struct semid_ds *ds)
1552
{
1553
    union target_semun *target_su;
1554

    
1555
    switch( cmd ) {
1556
        case IPC_STAT:
1557
        case IPC_SET:
1558
           if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1559
               return -TARGET_EFAULT;
1560
           target_to_host_semid_ds(ds,target_su->buf);
1561
           host_su->buf = ds;
1562
           unlock_user_struct(target_su, target_addr, 0);
1563
           break;
1564
        case GETVAL:
1565
        case SETVAL:
1566
           if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1567
               return -TARGET_EFAULT;
1568
           host_su->val = tswapl(target_su->val);
1569
           unlock_user_struct(target_su, target_addr, 0);
1570
           break;
1571
        case GETALL:
1572
        case SETALL:
1573
           if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1574
               return -TARGET_EFAULT;
1575
           *host_su->array = tswap16(*target_su->array);
1576
           unlock_user_struct(target_su, target_addr, 0);
1577
           break;
1578
        default:
1579
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1580
    }
1581
    return 0;
1582
}
1583

    
1584
static inline abi_long host_to_target_semun(int cmd,
1585
                                            abi_ulong target_addr,
1586
                                            union semun *host_su,
1587
                                            struct semid_ds *ds)
1588
{
1589
    union target_semun *target_su;
1590

    
1591
    switch( cmd ) {
1592
        case IPC_STAT:
1593
        case IPC_SET:
1594
           if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1595
               return -TARGET_EFAULT;
1596
           host_to_target_semid_ds(target_su->buf,ds);
1597
           unlock_user_struct(target_su, target_addr, 1);
1598
           break;
1599
        case GETVAL:
1600
        case SETVAL:
1601
           if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1602
               return -TARGET_EFAULT;
1603
           target_su->val = tswapl(host_su->val);
1604
           unlock_user_struct(target_su, target_addr, 1);
1605
           break;
1606
        case GETALL:
1607
        case SETALL:
1608
           if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1609
               return -TARGET_EFAULT;
1610
           *target_su->array = tswap16(*host_su->array);
1611
           unlock_user_struct(target_su, target_addr, 1);
1612
           break;
1613
        default:
1614
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1615
    }
1616
    return 0;
1617
}
1618

    
1619
static inline abi_long do_semctl(int first, int second, int third,
1620
                                 abi_long ptr)
1621
{
1622
    union semun arg;
1623
    struct semid_ds dsarg;
1624
    int cmd = third&0xff;
1625
    abi_long ret = 0;
1626

    
1627
    switch( cmd ) {
1628
        case GETVAL:
1629
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1630
            ret = get_errno(semctl(first, second, cmd, arg));
1631
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1632
            break;
1633
        case SETVAL:
1634
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1635
            ret = get_errno(semctl(first, second, cmd, arg));
1636
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1637
            break;
1638
        case GETALL:
1639
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1640
            ret = get_errno(semctl(first, second, cmd, arg));
1641
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1642
            break;
1643
        case SETALL:
1644
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1645
            ret = get_errno(semctl(first, second, cmd, arg));
1646
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1647
            break;
1648
        case IPC_STAT:
1649
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1650
            ret = get_errno(semctl(first, second, cmd, arg));
1651
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1652
            break;
1653
        case IPC_SET:
1654
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
1655
            ret = get_errno(semctl(first, second, cmd, arg));
1656
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
1657
            break;
1658
    default:
1659
            ret = get_errno(semctl(first, second, cmd, arg));
1660
    }
1661

    
1662
    return ret;
1663
}
1664

    
1665
struct target_msqid_ds
1666
{
1667
  struct target_ipc_perm msg_perm;
1668
  abi_ulong msg_stime;
1669
  abi_ulong __unused1;
1670
  abi_ulong msg_rtime;
1671
  abi_ulong __unused2;
1672
  abi_ulong msg_ctime;
1673
  abi_ulong __unused3;
1674
  abi_ulong __msg_cbytes;
1675
  abi_ulong msg_qnum;
1676
  abi_ulong msg_qbytes;
1677
  abi_ulong msg_lspid;
1678
  abi_ulong msg_lrpid;
1679
  abi_ulong __unused4;
1680
  abi_ulong __unused5;
1681
};
1682

    
1683
static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1684
                                               abi_ulong target_addr)
1685
{
1686
    struct target_msqid_ds *target_md;
1687

    
1688
    if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1689
        return -TARGET_EFAULT;
1690
    target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1691
    host_md->msg_stime = tswapl(target_md->msg_stime);
1692
    host_md->msg_rtime = tswapl(target_md->msg_rtime);
1693
    host_md->msg_ctime = tswapl(target_md->msg_ctime);
1694
    host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1695
    host_md->msg_qnum = tswapl(target_md->msg_qnum);
1696
    host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1697
    host_md->msg_lspid = tswapl(target_md->msg_lspid);
1698
    host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1699
    unlock_user_struct(target_md, target_addr, 0);
1700
    return 0;
1701
}
1702

    
1703
static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1704
                                               struct msqid_ds *host_md)
1705
{
1706
    struct target_msqid_ds *target_md;
1707

    
1708
    if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1709
        return -TARGET_EFAULT;
1710
    host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1711
    target_md->msg_stime = tswapl(host_md->msg_stime);
1712
    target_md->msg_rtime = tswapl(host_md->msg_rtime);
1713
    target_md->msg_ctime = tswapl(host_md->msg_ctime);
1714
    target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1715
    target_md->msg_qnum = tswapl(host_md->msg_qnum);
1716
    target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1717
    target_md->msg_lspid = tswapl(host_md->msg_lspid);
1718
    target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1719
    unlock_user_struct(target_md, target_addr, 1);
1720
    return 0;
1721
}
1722

    
1723
static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1724
{
1725
    struct msqid_ds dsarg;
1726
    int cmd = second&0xff;
1727
    abi_long ret = 0;
1728
    switch( cmd ) {
1729
    case IPC_STAT:
1730
    case IPC_SET:
1731
        target_to_host_msqid_ds(&dsarg,ptr);
1732
        ret = get_errno(msgctl(first, cmd, &dsarg));
1733
        host_to_target_msqid_ds(ptr,&dsarg);
1734
    default:
1735
        ret = get_errno(msgctl(first, cmd, &dsarg));
1736
    }
1737
    return ret;
1738
}
1739

    
1740
struct target_msgbuf {
1741
        abi_ulong mtype;
1742
        char        mtext[1];
1743
};
1744

    
1745
static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1746
                                 unsigned int msgsz, int msgflg)
1747
{
1748
    struct target_msgbuf *target_mb;
1749
    struct msgbuf *host_mb;
1750
    abi_long ret = 0;
1751

    
1752
    if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1753
        return -TARGET_EFAULT;
1754
    host_mb = malloc(msgsz+sizeof(long));
1755
    host_mb->mtype = tswapl(target_mb->mtype);
1756
    memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1757
    ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1758
    free(host_mb);
1759
    unlock_user_struct(target_mb, msgp, 0);
1760

    
1761
    return ret;
1762
}
1763

    
1764
static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1765
                                 unsigned int msgsz, int msgtype,
1766
                                 int msgflg)
1767
{
1768
    struct target_msgbuf *target_mb;
1769
    char *target_mtext;
1770
    struct msgbuf *host_mb;
1771
    abi_long ret = 0;
1772

    
1773
    if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1774
        return -TARGET_EFAULT;
1775
    host_mb = malloc(msgsz+sizeof(long));
1776
    ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1777
    if (ret > 0) {
1778
        abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1779
        target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1780
        if (!target_mtext) {
1781
            ret = -TARGET_EFAULT;
1782
            goto end;
1783
        }
1784
            memcpy(target_mb->mtext, host_mb->mtext, ret);
1785
        unlock_user(target_mtext, target_mtext_addr, ret);
1786
    }
1787
    target_mb->mtype = tswapl(host_mb->mtype);
1788
    free(host_mb);
1789

    
1790
end:
1791
    if (target_mb)
1792
        unlock_user_struct(target_mb, msgp, 1);
1793
    return ret;
1794
}
1795

    
1796
/* ??? This only works with linear mappings.  */
1797
/* do_ipc() must return target values and target errnos. */
1798
static abi_long do_ipc(unsigned int call, int first,
1799
                       int second, int third,
1800
                       abi_long ptr, abi_long fifth)
1801
{
1802
    int version;
1803
    abi_long ret = 0;
1804
    struct shmid_ds shm_info;
1805
    int i;
1806

    
1807
    version = call >> 16;
1808
    call &= 0xffff;
1809

    
1810
    switch (call) {
1811
    case IPCOP_semop:
1812
        ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
1813
        break;
1814

    
1815
    case IPCOP_semget:
1816
        ret = get_errno(semget(first, second, third));
1817
        break;
1818

    
1819
    case IPCOP_semctl:
1820
        ret = do_semctl(first, second, third, ptr);
1821
        break;
1822

    
1823
    case IPCOP_semtimedop:
1824
        gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1825
        ret = -TARGET_ENOSYS;
1826
        break;
1827

    
1828
        case IPCOP_msgget:
1829
                ret = get_errno(msgget(first, second));
1830
                break;
1831

    
1832
        case IPCOP_msgsnd:
1833
                ret = do_msgsnd(first, ptr, second, third);
1834
                break;
1835

    
1836
        case IPCOP_msgctl:
1837
                ret = do_msgctl(first, second, ptr);
1838
                break;
1839

    
1840
        case IPCOP_msgrcv:
1841
                {
1842
                      /* XXX: this code is not correct */
1843
                      struct ipc_kludge
1844
                      {
1845
                              void *__unbounded msgp;
1846
                              long int msgtyp;
1847
                      };
1848

    
1849
                      struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
1850
                      struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
1851

    
1852
                      ret = do_msgrcv(first, (long)msgp, second, 0, third);
1853

    
1854
                }
1855
                break;
1856

    
1857
    case IPCOP_shmat:
1858
        {
1859
            abi_ulong raddr;
1860
            void *host_addr;
1861
            /* SHM_* flags are the same on all linux platforms */
1862
            host_addr = shmat(first, (void *)g2h(ptr), second);
1863
            if (host_addr == (void *)-1) {
1864
                ret = get_errno((long)host_addr);
1865
                break;
1866
            }
1867
            raddr = h2g((unsigned long)host_addr);
1868
            /* find out the length of the shared memory segment */
1869
            
1870
            ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1871
            if (is_error(ret)) {
1872
                /* can't get length, bail out */
1873
                shmdt(host_addr);
1874
                break;
1875
            }
1876
            page_set_flags(raddr, raddr + shm_info.shm_segsz,
1877
                           PAGE_VALID | PAGE_READ |
1878
                           ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1879
            for (i = 0; i < N_SHM_REGIONS; ++i) {
1880
                if (shm_regions[i].start == 0) {
1881
                    shm_regions[i].start = raddr;
1882
                    shm_regions[i].size = shm_info.shm_segsz;
1883
                    break;
1884
                }
1885
            }
1886
            if (put_user(raddr, third, abi_ulong))
1887
                return -TARGET_EFAULT;
1888
            ret = 0;
1889
        }
1890
        break;
1891
    case IPCOP_shmdt:
1892
        for (i = 0; i < N_SHM_REGIONS; ++i) {
1893
            if (shm_regions[i].start == ptr) {
1894
                shm_regions[i].start = 0;
1895
                page_set_flags(ptr, shm_regions[i].size, 0);
1896
                break;
1897
            }
1898
        }
1899
        ret = get_errno(shmdt((void *)g2h(ptr)));
1900
        break;
1901

    
1902
    case IPCOP_shmget:
1903
        /* IPC_* flag values are the same on all linux platforms */
1904
        ret = get_errno(shmget(first, second, third));
1905
        break;
1906

    
1907
        /* IPC_* and SHM_* command values are the same on all linux platforms */
1908
    case IPCOP_shmctl:
1909
        switch(second) {
1910
        case IPC_RMID:
1911
        case SHM_LOCK:
1912
        case SHM_UNLOCK:
1913
            ret = get_errno(shmctl(first, second, NULL));
1914
            break;
1915
        default:
1916
            goto unimplemented;
1917
        }
1918
        break;
1919
    default:
1920
    unimplemented:
1921
        gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1922
        ret = -TARGET_ENOSYS;
1923
        break;
1924
    }
1925
    return ret;
1926
}
1927
#endif
1928

    
1929
/* kernel structure types definitions */
1930
#define IFNAMSIZ        16
1931

    
1932
#define STRUCT(name, list...) STRUCT_ ## name,
1933
#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1934
enum {
1935
#include "syscall_types.h"
1936
};
1937
#undef STRUCT
1938
#undef STRUCT_SPECIAL
1939

    
1940
#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1941
#define STRUCT_SPECIAL(name)
1942
#include "syscall_types.h"
1943
#undef STRUCT
1944
#undef STRUCT_SPECIAL
1945

    
1946
typedef struct IOCTLEntry {
1947
    unsigned int target_cmd;
1948
    unsigned int host_cmd;
1949
    const char *name;
1950
    int access;
1951
    const argtype arg_type[5];
1952
} IOCTLEntry;
1953

    
1954
#define IOC_R 0x0001
1955
#define IOC_W 0x0002
1956
#define IOC_RW (IOC_R | IOC_W)
1957

    
1958
#define MAX_STRUCT_SIZE 4096
1959

    
1960
IOCTLEntry ioctl_entries[] = {
1961
#define IOCTL(cmd, access, types...) \
1962
    { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1963
#include "ioctls.h"
1964
    { 0, 0, },
1965
};
1966

    
1967
/* ??? Implement proper locking for ioctls.  */
1968
/* do_ioctl() Must return target values and target errnos. */
1969
static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
1970
{
1971
    const IOCTLEntry *ie;
1972
    const argtype *arg_type;
1973
    abi_long ret;
1974
    uint8_t buf_temp[MAX_STRUCT_SIZE];
1975
    int target_size;
1976
    void *argptr;
1977

    
1978
    ie = ioctl_entries;
1979
    for(;;) {
1980
        if (ie->target_cmd == 0) {
1981
            gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
1982
            return -TARGET_ENOSYS;
1983
        }
1984
        if (ie->target_cmd == cmd)
1985
            break;
1986
        ie++;
1987
    }
1988
    arg_type = ie->arg_type;
1989
#if defined(DEBUG)
1990
    gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
1991
#endif
1992
    switch(arg_type[0]) {
1993
    case TYPE_NULL:
1994
        /* no argument */
1995
        ret = get_errno(ioctl(fd, ie->host_cmd));
1996
        break;
1997
    case TYPE_PTRVOID:
1998
    case TYPE_INT:
1999
        /* int argment */
2000
        ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2001
        break;
2002
    case TYPE_PTR:
2003
        arg_type++;
2004
        target_size = thunk_type_size(arg_type, 0);
2005
        switch(ie->access) {
2006
        case IOC_R:
2007
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2008
            if (!is_error(ret)) {
2009
                argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2010
                if (!argptr)
2011
                    return -TARGET_EFAULT;
2012
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2013
                unlock_user(argptr, arg, target_size);
2014
            }
2015
            break;
2016
        case IOC_W:
2017
            argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2018
            if (!argptr)
2019
                return -TARGET_EFAULT;
2020
            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2021
            unlock_user(argptr, arg, 0);
2022
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2023
            break;
2024
        default:
2025
        case IOC_RW:
2026
            argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2027
            if (!argptr)
2028
                return -TARGET_EFAULT;
2029
            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2030
            unlock_user(argptr, arg, 0);
2031
            ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2032
            if (!is_error(ret)) {
2033
                argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2034
                if (!argptr)
2035
                    return -TARGET_EFAULT;
2036
                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2037
                unlock_user(argptr, arg, target_size);
2038
            }
2039
            break;
2040
        }
2041
        break;
2042
    default:
2043
        gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2044
                 (long)cmd, arg_type[0]);
2045
        ret = -TARGET_ENOSYS;
2046
        break;
2047
    }
2048
    return ret;
2049
}
2050

    
2051
bitmask_transtbl iflag_tbl[] = {
2052
        { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2053
        { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2054
        { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2055
        { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2056
        { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2057
        { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2058
        { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2059
        { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2060
        { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2061
        { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2062
        { TARGET_IXON, TARGET_IXON, IXON, IXON },
2063
        { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2064
        { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2065
        { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2066
        { 0, 0, 0, 0 }
2067
};
2068

    
2069
bitmask_transtbl oflag_tbl[] = {
2070
        { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2071
        { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2072
        { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2073
        { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2074
        { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2075
        { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2076
        { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2077
        { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2078
        { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2079
        { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2080
        { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2081
        { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2082
        { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2083
        { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2084
        { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2085
        { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2086
        { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2087
        { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2088
        { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2089
        { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2090
        { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2091
        { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2092
        { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2093
        { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2094
        { 0, 0, 0, 0 }
2095
};
2096

    
2097
bitmask_transtbl cflag_tbl[] = {
2098
        { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2099
        { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2100
        { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2101
        { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2102
        { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2103
        { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2104
        { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2105
        { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2106
        { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2107
        { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2108
        { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2109
        { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2110
        { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2111
        { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2112
        { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2113
        { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2114
        { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2115
        { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2116
        { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2117
        { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2118
        { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2119
        { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2120
        { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2121
        { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2122
        { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2123
        { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2124
        { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2125
        { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2126
        { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2127
        { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2128
        { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2129
        { 0, 0, 0, 0 }
2130
};
2131

    
2132
bitmask_transtbl lflag_tbl[] = {
2133
        { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2134
        { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2135
        { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2136
        { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2137
        { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2138
        { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2139
        { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2140
        { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2141
        { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2142
        { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2143
        { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2144
        { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2145
        { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2146
        { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2147
        { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2148
        { 0, 0, 0, 0 }
2149
};
2150

    
2151
static void target_to_host_termios (void *dst, const void *src)
2152
{
2153
    struct host_termios *host = dst;
2154
    const struct target_termios *target = src;
2155

    
2156
    host->c_iflag =
2157
        target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2158
    host->c_oflag =
2159
        target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2160
    host->c_cflag =
2161
        target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2162
    host->c_lflag =
2163
        target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2164
    host->c_line = target->c_line;
2165

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

    
2185
static void host_to_target_termios (void *dst, const void *src)
2186
{
2187
    struct target_termios *target = dst;
2188
    const struct host_termios *host = src;
2189

    
2190
    target->c_iflag =
2191
        tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2192
    target->c_oflag =
2193
        tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2194
    target->c_cflag =
2195
        tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2196
    target->c_lflag =
2197
        tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2198
    target->c_line = host->c_line;
2199

    
2200
    target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2201
    target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2202
    target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2203
    target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2204
    target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2205
    target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2206
    target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2207
    target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2208
    target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2209
    target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2210
    target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2211
    target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2212
    target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2213
    target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2214
    target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2215
    target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2216
    target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2217
}
2218

    
2219
StructEntry struct_termios_def = {
2220
    .convert = { host_to_target_termios, target_to_host_termios },
2221
    .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2222
    .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2223
};
2224

    
2225
static bitmask_transtbl mmap_flags_tbl[] = {
2226
        { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2227
        { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2228
        { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2229
        { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2230
        { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2231
        { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2232
        { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2233
        { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2234
        { 0, 0, 0, 0 }
2235
};
2236

    
2237
static bitmask_transtbl fcntl_flags_tbl[] = {
2238
        { TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
2239
        { TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
2240
        { TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
2241
        { TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
2242
        { TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
2243
        { TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
2244
        { TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
2245
        { TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
2246
        { TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
2247
        { TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
2248
        { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2249
        { TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
2250
        { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2251
#if defined(O_DIRECT)
2252
        { TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
2253
#endif
2254
        { 0, 0, 0, 0 }
2255
};
2256

    
2257
#if defined(TARGET_I386)
2258

    
2259
/* NOTE: there is really one LDT for all the threads */
2260
uint8_t *ldt_table;
2261

    
2262
static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
2263
{
2264
    int size;
2265
    void *p;
2266

    
2267
    if (!ldt_table)
2268
        return 0;
2269
    size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2270
    if (size > bytecount)
2271
        size = bytecount;
2272
    p = lock_user(VERIFY_WRITE, ptr, size, 0);
2273
    if (!p)
2274
        return -TARGET_EFAULT;
2275
    /* ??? Should this by byteswapped?  */
2276
    memcpy(p, ldt_table, size);
2277
    unlock_user(p, ptr, size);
2278
    return size;
2279
}
2280

    
2281
/* XXX: add locking support */
2282
static abi_long write_ldt(CPUX86State *env,
2283
                          abi_ulong ptr, unsigned long bytecount, int oldmode)
2284
{
2285
    struct target_modify_ldt_ldt_s ldt_info;
2286
    struct target_modify_ldt_ldt_s *target_ldt_info;
2287
    int seg_32bit, contents, read_exec_only, limit_in_pages;
2288
    int seg_not_present, useable;
2289
    uint32_t *lp, entry_1, entry_2;
2290

    
2291
    if (bytecount != sizeof(ldt_info))
2292
        return -TARGET_EINVAL;
2293
    if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
2294
        return -TARGET_EFAULT;
2295
    ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2296
    ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2297
    ldt_info.limit = tswap32(target_ldt_info->limit);
2298
    ldt_info.flags = tswap32(target_ldt_info->flags);
2299
    unlock_user_struct(target_ldt_info, ptr, 0);
2300

    
2301
    if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2302
        return -TARGET_EINVAL;
2303
    seg_32bit = ldt_info.flags & 1;
2304
    contents = (ldt_info.flags >> 1) & 3;
2305
    read_exec_only = (ldt_info.flags >> 3) & 1;
2306
    limit_in_pages = (ldt_info.flags >> 4) & 1;
2307
    seg_not_present = (ldt_info.flags >> 5) & 1;
2308
    useable = (ldt_info.flags >> 6) & 1;
2309

    
2310
    if (contents == 3) {
2311
        if (oldmode)
2312
            return -TARGET_EINVAL;
2313
        if (seg_not_present == 0)
2314
            return -TARGET_EINVAL;
2315
    }
2316
    /* allocate the LDT */
2317
    if (!ldt_table) {
2318
        ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2319
        if (!ldt_table)
2320
            return -TARGET_ENOMEM;
2321
        memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2322
        env->ldt.base = h2g((unsigned long)ldt_table);
2323
        env->ldt.limit = 0xffff;
2324
    }
2325

    
2326
    /* NOTE: same code as Linux kernel */
2327
    /* Allow LDTs to be cleared by the user. */
2328
    if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2329
        if (oldmode ||
2330
            (contents == 0                &&
2331
             read_exec_only == 1        &&
2332
             seg_32bit == 0                &&
2333
             limit_in_pages == 0        &&
2334
             seg_not_present == 1        &&
2335
             useable == 0 )) {
2336
            entry_1 = 0;
2337
            entry_2 = 0;
2338
            goto install;
2339
        }
2340
    }
2341

    
2342
    entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2343
        (ldt_info.limit & 0x0ffff);
2344
    entry_2 = (ldt_info.base_addr & 0xff000000) |
2345
        ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2346
        (ldt_info.limit & 0xf0000) |
2347
        ((read_exec_only ^ 1) << 9) |
2348
        (contents << 10) |
2349
        ((seg_not_present ^ 1) << 15) |
2350
        (seg_32bit << 22) |
2351
        (limit_in_pages << 23) |
2352
        0x7000;
2353
    if (!oldmode)
2354
        entry_2 |= (useable << 20);
2355

    
2356
    /* Install the new entry ...  */
2357
install:
2358
    lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2359
    lp[0] = tswap32(entry_1);
2360
    lp[1] = tswap32(entry_2);
2361
    return 0;
2362
}
2363

    
2364
/* specific and weird i386 syscalls */
2365
abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, 
2366
                       unsigned long bytecount)
2367
{
2368
    abi_long ret;
2369

    
2370
    switch (func) {
2371
    case 0:
2372
        ret = read_ldt(ptr, bytecount);
2373
        break;
2374
    case 1:
2375
        ret = write_ldt(env, ptr, bytecount, 1);
2376
        break;
2377
    case 0x11:
2378
        ret = write_ldt(env, ptr, bytecount, 0);
2379
        break;
2380
    default:
2381
        ret = -TARGET_ENOSYS;
2382
        break;
2383
    }
2384
    return ret;
2385
}
2386

    
2387
#endif /* defined(TARGET_I386) */
2388

    
2389
/* this stack is the equivalent of the kernel stack associated with a
2390
   thread/process */
2391
#define NEW_STACK_SIZE 8192
2392

    
2393
static int clone_func(void *arg)
2394
{
2395
    CPUState *env = arg;
2396
    cpu_loop(env);
2397
    /* never exits */
2398
    return 0;
2399
}
2400

    
2401
/* do_fork() Must return host values and target errnos (unlike most
2402
   do_*() functions). */
2403
int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
2404
{
2405
    int ret;
2406
    TaskState *ts;
2407
    uint8_t *new_stack;
2408
    CPUState *new_env;
2409

    
2410
    if (flags & CLONE_VM) {
2411
        ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2412
        memset(ts, 0, sizeof(TaskState));
2413
        new_stack = ts->stack;
2414
        ts->used = 1;
2415
        /* add in task state list */
2416
        ts->next = first_task_state;
2417
        first_task_state = ts;
2418
        /* we create a new CPU instance. */
2419
        new_env = cpu_copy(env);
2420
#if defined(TARGET_I386)
2421
        if (!newsp)
2422
            newsp = env->regs[R_ESP];
2423
        new_env->regs[R_ESP] = newsp;
2424
        new_env->regs[R_EAX] = 0;
2425
#elif defined(TARGET_ARM)
2426
        if (!newsp)
2427
            newsp = env->regs[13];
2428
        new_env->regs[13] = newsp;
2429
        new_env->regs[0] = 0;
2430
#elif defined(TARGET_SPARC)
2431
        if (!newsp)
2432
            newsp = env->regwptr[22];
2433
        new_env->regwptr[22] = newsp;
2434
        new_env->regwptr[0] = 0;
2435
        /* XXXXX */
2436
        printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
2437
#elif defined(TARGET_M68K)
2438
        if (!newsp)
2439
            newsp = env->aregs[7];
2440
        new_env->aregs[7] = newsp;
2441
        new_env->dregs[0] = 0;
2442
        /* ??? is this sufficient?  */
2443
#elif defined(TARGET_MIPS)
2444
        if (!newsp)
2445
            newsp = env->gpr[29][env->current_tc];
2446
        new_env->gpr[29][env->current_tc] = newsp;
2447
#elif defined(TARGET_PPC)
2448
        if (!newsp)
2449
            newsp = env->gpr[1];
2450
        new_env->gpr[1] = newsp;
2451
        {
2452
            int i;
2453
            for (i = 7; i < 32; i++)
2454
                new_env->gpr[i] = 0;
2455
        }
2456
#elif defined(TARGET_SH4)
2457
        if (!newsp)
2458
          newsp = env->gregs[15];
2459
        new_env->gregs[15] = newsp;
2460
        /* XXXXX */
2461
#elif defined(TARGET_ALPHA)
2462
       if (!newsp)
2463
         newsp = env->ir[30];
2464
       new_env->ir[30] = newsp;
2465
        /* ? */
2466
        {
2467
            int i;
2468
            for (i = 7; i < 30; i++)
2469
                new_env->ir[i] = 0;
2470
        }
2471
#elif defined(TARGET_CRIS)
2472
        if (!newsp)
2473
          newsp = env->regs[14];
2474
        new_env->regs[14] = newsp;
2475
#else
2476
#error unsupported target CPU
2477
#endif
2478
        new_env->opaque = ts;
2479
#ifdef __ia64__
2480
        ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2481
#else
2482
        ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2483
#endif
2484
    } else {
2485
        /* if no CLONE_VM, we consider it is a fork */
2486
        if ((flags & ~CSIGNAL) != 0)
2487
            return -EINVAL;
2488
        ret = fork();
2489
    }
2490
    return ret;
2491
}
2492

    
2493
static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2494
{
2495
    struct flock fl;
2496
    struct target_flock *target_fl;
2497
    struct flock64 fl64;
2498
    struct target_flock64 *target_fl64;
2499
    abi_long ret;
2500

    
2501
    switch(cmd) {
2502
    case TARGET_F_GETLK:
2503
        if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2504
            return -TARGET_EFAULT;
2505
        fl.l_type = tswap16(target_fl->l_type);
2506
        fl.l_whence = tswap16(target_fl->l_whence);
2507
        fl.l_start = tswapl(target_fl->l_start);
2508
        fl.l_len = tswapl(target_fl->l_len);
2509
        fl.l_pid = tswapl(target_fl->l_pid);
2510
        unlock_user_struct(target_fl, arg, 0);
2511
        ret = get_errno(fcntl(fd, cmd, &fl));
2512
        if (ret == 0) {
2513
            if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2514
                return -TARGET_EFAULT;
2515
            target_fl->l_type = tswap16(fl.l_type);
2516
            target_fl->l_whence = tswap16(fl.l_whence);
2517
            target_fl->l_start = tswapl(fl.l_start);
2518
            target_fl->l_len = tswapl(fl.l_len);
2519
            target_fl->l_pid = tswapl(fl.l_pid);
2520
            unlock_user_struct(target_fl, arg, 1);
2521
        }
2522
        break;
2523

    
2524
    case TARGET_F_SETLK:
2525
    case TARGET_F_SETLKW:
2526
        if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2527
            return -TARGET_EFAULT;
2528
        fl.l_type = tswap16(target_fl->l_type);
2529
        fl.l_whence = tswap16(target_fl->l_whence);
2530
        fl.l_start = tswapl(target_fl->l_start);
2531
        fl.l_len = tswapl(target_fl->l_len);
2532
        fl.l_pid = tswapl(target_fl->l_pid);
2533
        unlock_user_struct(target_fl, arg, 0);
2534
        ret = get_errno(fcntl(fd, cmd, &fl));
2535
        break;
2536

    
2537
    case TARGET_F_GETLK64:
2538
        if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2539
            return -TARGET_EFAULT;
2540
        fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2541
        fl64.l_whence = tswap16(target_fl64->l_whence);
2542
        fl64.l_start = tswapl(target_fl64->l_start);
2543
        fl64.l_len = tswapl(target_fl64->l_len);
2544
        fl64.l_pid = tswap16(target_fl64->l_pid);
2545
        unlock_user_struct(target_fl64, arg, 0);
2546
        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2547
        if (ret == 0) {
2548
            if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2549
                return -TARGET_EFAULT;
2550
            target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2551
            target_fl64->l_whence = tswap16(fl64.l_whence);
2552
            target_fl64->l_start = tswapl(fl64.l_start);
2553
            target_fl64->l_len = tswapl(fl64.l_len);
2554
            target_fl64->l_pid = tswapl(fl64.l_pid);
2555
            unlock_user_struct(target_fl64, arg, 1);
2556
        }
2557
        break;
2558
    case TARGET_F_SETLK64:
2559
    case TARGET_F_SETLKW64:
2560
        if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2561
            return -TARGET_EFAULT;
2562
        fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2563
        fl64.l_whence = tswap16(target_fl64->l_whence);
2564
        fl64.l_start = tswapl(target_fl64->l_start);
2565
        fl64.l_len = tswapl(target_fl64->l_len);
2566
        fl64.l_pid = tswap16(target_fl64->l_pid);
2567
        unlock_user_struct(target_fl64, arg, 0);
2568
        ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2569
        break;
2570

    
2571
    case F_GETFL:
2572
        ret = get_errno(fcntl(fd, cmd, arg));
2573
        if (ret >= 0) {
2574
            ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2575
        }
2576
        break;
2577

    
2578
    case F_SETFL:
2579
        ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
2580
        break;
2581

    
2582
    default:
2583
        ret = get_errno(fcntl(fd, cmd, arg));
2584
        break;
2585
    }
2586
    return ret;
2587
}
2588

    
2589
#ifdef USE_UID16
2590

    
2591
static inline int high2lowuid(int uid)
2592
{
2593
    if (uid > 65535)
2594
        return 65534;
2595
    else
2596
        return uid;
2597
}
2598

    
2599
static inline int high2lowgid(int gid)
2600
{
2601
    if (gid > 65535)
2602
        return 65534;
2603
    else
2604
        return gid;
2605
}
2606

    
2607
static inline int low2highuid(int uid)
2608
{
2609
    if ((int16_t)uid == -1)
2610
        return -1;
2611
    else
2612
        return uid;
2613
}
2614

    
2615
static inline int low2highgid(int gid)
2616
{
2617
    if ((int16_t)gid == -1)
2618
        return -1;
2619
    else
2620
        return gid;
2621
}
2622

    
2623
#endif /* USE_UID16 */
2624

    
2625
void syscall_init(void)
2626
{
2627
    IOCTLEntry *ie;
2628
    const argtype *arg_type;
2629
    int size;
2630
    int i;
2631

    
2632
#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2633
#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
2634
#include "syscall_types.h"
2635
#undef STRUCT
2636
#undef STRUCT_SPECIAL
2637

    
2638
    /* we patch the ioctl size if necessary. We rely on the fact that
2639
       no ioctl has all the bits at '1' in the size field */
2640
    ie = ioctl_entries;
2641
    while (ie->target_cmd != 0) {
2642
        if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2643
            TARGET_IOC_SIZEMASK) {
2644
            arg_type = ie->arg_type;
2645
            if (arg_type[0] != TYPE_PTR) {
2646
                fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2647
                        ie->target_cmd);
2648
                exit(1);
2649
            }
2650
            arg_type++;
2651
            size = thunk_type_size(arg_type, 0);
2652
            ie->target_cmd = (ie->target_cmd &
2653
                              ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2654
                (size << TARGET_IOC_SIZESHIFT);
2655
        }
2656

    
2657
        /* Build target_to_host_errno_table[] table from
2658
         * host_to_target_errno_table[]. */
2659
        for (i=0; i < ERRNO_TABLE_SIZE; i++)
2660
                target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2661

    
2662
        /* automatic consistency check if same arch */
2663
#if defined(__i386__) && defined(TARGET_I386)
2664
        if (ie->target_cmd != ie->host_cmd) {
2665
            fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2666
                    ie->target_cmd, ie->host_cmd);
2667
        }
2668
#endif
2669
        ie++;
2670
    }
2671
}
2672

    
2673
#if TARGET_ABI_BITS == 32
2674
static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2675
{
2676
#ifdef TARGET_WORDS_BIG_ENDIAN
2677
    return ((uint64_t)word0 << 32) | word1;
2678
#else
2679
    return ((uint64_t)word1 << 32) | word0;
2680
#endif
2681
}
2682
#else /* TARGET_ABI_BITS == 32 */
2683
static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2684
{
2685
    return word0;
2686
}
2687
#endif /* TARGET_ABI_BITS != 32 */
2688

    
2689
#ifdef TARGET_NR_truncate64
2690
static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2691
                                         abi_long arg2,
2692
                                         abi_long arg3,
2693
                                         abi_long arg4)
2694
{
2695
#ifdef TARGET_ARM
2696
    if (((CPUARMState *)cpu_env)->eabi)
2697
      {
2698
        arg2 = arg3;
2699
        arg3 = arg4;
2700
      }
2701
#endif
2702
    return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2703
}
2704
#endif
2705

    
2706
#ifdef TARGET_NR_ftruncate64
2707
static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2708
                                          abi_long arg2,
2709
                                          abi_long arg3,
2710
                                          abi_long arg4)
2711
{
2712
#ifdef TARGET_ARM
2713
    if (((CPUARMState *)cpu_env)->eabi)
2714
      {
2715
        arg2 = arg3;
2716
        arg3 = arg4;
2717
      }
2718
#endif
2719
    return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2720
}
2721
#endif
2722

    
2723
static inline abi_long target_to_host_timespec(struct timespec *host_ts,
2724
                                               abi_ulong target_addr)
2725
{
2726
    struct target_timespec *target_ts;
2727

    
2728
    if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
2729
        return -TARGET_EFAULT;
2730
    host_ts->tv_sec = tswapl(target_ts->tv_sec);
2731
    host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2732
    unlock_user_struct(target_ts, target_addr, 0);
2733
}
2734

    
2735
static inline abi_long host_to_target_timespec(abi_ulong target_addr,
2736
                                               struct timespec *host_ts)
2737
{
2738
    struct target_timespec *target_ts;
2739

    
2740
    if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
2741
        return -TARGET_EFAULT;
2742
    target_ts->tv_sec = tswapl(host_ts->tv_sec);
2743
    target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2744
    unlock_user_struct(target_ts, target_addr, 1);
2745
}
2746

    
2747
/* do_syscall() should always have a single exit point at the end so
2748
   that actions, such as logging of syscall results, can be performed.
2749
   All errnos that do_syscall() returns must be -TARGET_<errcode>. */
2750
abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2751
                    abi_long arg2, abi_long arg3, abi_long arg4,
2752
                    abi_long arg5, abi_long arg6)
2753
{
2754
    abi_long ret;
2755
    struct stat st;
2756
    struct statfs stfs;
2757
    void *p;
2758

    
2759
#ifdef DEBUG
2760
    gemu_log("syscall %d", num);
2761
#endif
2762
    if(do_strace)
2763
        print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
2764

    
2765
    switch(num) {
2766
    case TARGET_NR_exit:
2767
#ifdef HAVE_GPROF
2768
        _mcleanup();
2769
#endif
2770
        gdb_exit(cpu_env, arg1);
2771
        /* XXX: should free thread stack and CPU env */
2772
        _exit(arg1);
2773
        ret = 0; /* avoid warning */
2774
        break;
2775
    case TARGET_NR_read:
2776
        page_unprotect_range(arg2, arg3);
2777
        if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
2778
            goto efault;
2779
        ret = get_errno(read(arg1, p, arg3));
2780
        unlock_user(p, arg2, ret);
2781
        break;
2782
    case TARGET_NR_write:
2783
        if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
2784
            goto efault;
2785
        ret = get_errno(write(arg1, p, arg3));
2786
        unlock_user(p, arg2, 0);
2787
        break;
2788
    case TARGET_NR_open:
2789
        if (!(p = lock_user_string(arg1))) {
2790
            return -TARGET_EFAULT;
2791
            goto fail;
2792
        }
2793
        ret = get_errno(open(path(p),
2794
                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
2795
                             arg3));
2796
        unlock_user(p, arg1, 0);
2797
        break;
2798
#if defined(TARGET_NR_openat) && defined(__NR_openat)
2799
    case TARGET_NR_openat:
2800
        if (!(p = lock_user_string(arg2)))
2801
            goto efault;
2802
        ret = get_errno(sys_openat(arg1,
2803
                                   path(p),
2804
                                   target_to_host_bitmask(arg3, fcntl_flags_tbl),
2805
                                   arg4));
2806
        unlock_user(p, arg2, 0);
2807
        break;
2808
#endif
2809
    case TARGET_NR_close:
2810
        ret = get_errno(close(arg1));
2811
        break;
2812
    case TARGET_NR_brk:
2813
        ret = do_brk(arg1);
2814
        break;
2815
    case TARGET_NR_fork:
2816
        ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
2817
        break;
2818
#ifdef TARGET_NR_waitpid
2819
    case TARGET_NR_waitpid:
2820
        {
2821
            int status;
2822
            ret = get_errno(waitpid(arg1, &status, arg3));
2823
            if (!is_error(ret) && arg2)
2824
                tput32(arg2, status);
2825
        }
2826
        break;
2827
#endif
2828
#ifdef TARGET_NR_creat /* not on alpha */
2829
    case TARGET_NR_creat:
2830
        if (!(p = lock_user_string(arg1)))
2831
            goto efault;
2832
        ret = get_errno(creat(p, arg2));
2833
        unlock_user(p, arg1, 0);
2834
        break;
2835
#endif
2836
    case TARGET_NR_link:
2837
        {
2838
            void * p2;
2839
            p = lock_user_string(arg1);
2840
            p2 = lock_user_string(arg2);
2841
            if (!p || !p2)
2842
                ret = -TARGET_EFAULT;
2843
            else
2844
                ret = get_errno(link(p, p2));
2845
            unlock_user(p2, arg2, 0);
2846
            unlock_user(p, arg1, 0);
2847
        }
2848
        break;
2849
#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2850
    case TARGET_NR_linkat:
2851
        {
2852
            void * p2 = NULL;
2853
            if (!arg2 || !arg4)
2854
                goto efault;
2855
            p  = lock_user_string(arg2);
2856
            p2 = lock_user_string(arg4);
2857
            if (!p || !p2)
2858
                ret = -TARGET_EFAULT;
2859
            else
2860
                ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
2861
            unlock_user(p, arg2, 0);
2862
            unlock_user(p2, arg4, 0);
2863
        }
2864
        break;
2865
#endif
2866
    case TARGET_NR_unlink:
2867
        if (!(p = lock_user_string(arg1)))
2868
            goto efault;
2869
        ret = get_errno(unlink(p));
2870
        unlock_user(p, arg1, 0);
2871
        break;
2872
#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2873
    case TARGET_NR_unlinkat:
2874
        if (!(p = lock_user_string(arg2)))
2875
            goto efault;
2876
        ret = get_errno(sys_unlinkat(arg1, p, arg3));
2877
        unlock_user(p, arg2, 0);
2878
#endif
2879
    case TARGET_NR_execve:
2880
        {
2881
            char **argp, **envp;
2882
            int argc, envc;
2883
            abi_ulong gp;
2884
            abi_ulong guest_argp;
2885
            abi_ulong guest_envp;
2886
            abi_ulong addr;
2887
            char **q;
2888

    
2889
            argc = 0;
2890
            guest_argp = arg2;
2891
            for (gp = guest_argp; tgetl(gp); gp++)
2892
                argc++;
2893
            envc = 0;
2894
            guest_envp = arg3;
2895
            for (gp = guest_envp; tgetl(gp); gp++)
2896
                envc++;
2897

    
2898
            argp = alloca((argc + 1) * sizeof(void *));
2899
            envp = alloca((envc + 1) * sizeof(void *));
2900

    
2901
            for (gp = guest_argp, q = argp; ;
2902
                  gp += sizeof(abi_ulong), q++) {
2903
                addr = tgetl(gp);
2904
                if (!addr)
2905
                    break;
2906
                if (!(*q = lock_user_string(addr))) {
2907
                    ret = -TARGET_EFAULT;
2908
                    goto execve_fail;
2909
                }
2910
            }
2911
            *q = NULL;
2912

    
2913
            for (gp = guest_envp, q = envp; ;
2914
                  gp += sizeof(abi_ulong), q++) {
2915
                addr = tgetl(gp);
2916
                if (!addr)
2917
                    break;
2918
                if (!(*q = lock_user_string(addr))) {
2919
                    ret = -TARGET_EFAULT;
2920
                    goto execve_fail;
2921
                }
2922
            }
2923
            *q = NULL;
2924

    
2925
            if (!(p = lock_user_string(arg1))) {
2926
                ret = -TARGET_EFAULT;
2927
                goto execve_fail;
2928
            }
2929
            ret = get_errno(execve(p, argp, envp));
2930
            unlock_user(p, arg1, 0);
2931

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

    
3317
            if (arg2) {
3318
                if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3319
                    goto efault;
3320
                act._sa_handler = old_act->_sa_handler;
3321
                target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3322
                act.sa_flags = old_act->sa_flags;
3323
                unlock_user_struct(old_act, arg2, 0);
3324
                pact = &act;
3325
            } else {
3326
                pact = NULL;
3327
            }
3328

    
3329
            ret = get_errno(do_sigaction(arg1, pact, &oact));
3330

    
3331
            if (!is_error(ret) && arg3) {
3332
                if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3333
                    goto efault;
3334
                old_act->_sa_handler = oact._sa_handler;
3335
                old_act->sa_flags = oact.sa_flags;
3336
                old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3337
                old_act->sa_mask.sig[1] = 0;
3338
                old_act->sa_mask.sig[2] = 0;
3339
                old_act->sa_mask.sig[3] = 0;
3340
                unlock_user_struct(old_act, arg3, 1);
3341
            }
3342
#endif
3343
        }
3344
        break;
3345
#endif
3346
    case TARGET_NR_rt_sigaction:
3347
        {
3348
            struct target_sigaction *act;
3349
            struct target_sigaction *oact;
3350

    
3351
            if (arg2) {
3352
                if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3353
                    goto efault;
3354
            } else
3355
                act = NULL;
3356
            if (arg3) {
3357
                if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3358
                    ret = -TARGET_EFAULT;
3359
                    goto rt_sigaction_fail;
3360
                }
3361
            } else
3362
                oact = NULL;
3363
            ret = get_errno(do_sigaction(arg1, act, oact));
3364
        rt_sigaction_fail:
3365
            if (act)
3366
                unlock_user_struct(act, arg2, 0);
3367
            if (oact)
3368
                unlock_user_struct(oact, arg3, 1);
3369
        }
3370
        break;
3371
#ifdef TARGET_NR_sgetmask /* not on alpha */
3372
    case TARGET_NR_sgetmask:
3373
        {
3374
            sigset_t cur_set;
3375
            abi_ulong target_set;
3376
            sigprocmask(0, NULL, &cur_set);
3377
            host_to_target_old_sigset(&target_set, &cur_set);
3378
            ret = target_set;
3379
        }
3380
        break;
3381
#endif
3382
#ifdef TARGET_NR_ssetmask /* not on alpha */
3383
    case TARGET_NR_ssetmask:
3384
        {
3385
            sigset_t set, oset, cur_set;
3386
            abi_ulong target_set = arg1;
3387
            sigprocmask(0, NULL, &cur_set);
3388
            target_to_host_old_sigset(&set, &target_set);
3389
            sigorset(&set, &set, &cur_set);
3390
            sigprocmask(SIG_SETMASK, &set, &oset);
3391
            host_to_target_old_sigset(&target_set, &oset);
3392
            ret = target_set;
3393
        }
3394
        break;
3395
#endif
3396
#ifdef TARGET_NR_sigprocmask
3397
    case TARGET_NR_sigprocmask:
3398
        {
3399
            int how = arg1;
3400
            sigset_t set, oldset, *set_ptr;
3401

    
3402
            if (arg2) {
3403
                switch(how) {
3404
                case TARGET_SIG_BLOCK:
3405
                    how = SIG_BLOCK;
3406
                    break;
3407
                case TARGET_SIG_UNBLOCK:
3408
                    how = SIG_UNBLOCK;
3409
                    break;
3410
                case TARGET_SIG_SETMASK:
3411
                    how = SIG_SETMASK;
3412
                    break;
3413
                default:
3414
                    ret = -TARGET_EINVAL;
3415
                    goto fail;
3416
                }
3417
                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3418
                    goto efault;
3419
                target_to_host_old_sigset(&set, p);
3420
                unlock_user(p, arg2, 0);
3421
                set_ptr = &set;
3422
            } else {
3423
                how = 0;
3424
                set_ptr = NULL;
3425
            }
3426
            ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
3427
            if (!is_error(ret) && arg3) {
3428
                if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3429
                    goto efault;
3430
                host_to_target_old_sigset(p, &oldset);
3431
                unlock_user(p, arg3, sizeof(target_sigset_t));
3432
            }
3433
        }
3434
        break;
3435
#endif
3436
    case TARGET_NR_rt_sigprocmask:
3437
        {
3438
            int how = arg1;
3439
            sigset_t set, oldset, *set_ptr;
3440

    
3441
            if (arg2) {
3442
                switch(how) {
3443
                case TARGET_SIG_BLOCK:
3444
                    how = SIG_BLOCK;
3445
                    break;
3446
                case TARGET_SIG_UNBLOCK:
3447
                    how = SIG_UNBLOCK;
3448
                    break;
3449
                case TARGET_SIG_SETMASK:
3450
                    how = SIG_SETMASK;
3451
                    break;
3452
                default:
3453
                    ret = -TARGET_EINVAL;
3454
                    goto fail;
3455
                }
3456
                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3457
                    goto efault;
3458
                target_to_host_sigset(&set, p);
3459
                unlock_user(p, arg2, 0);
3460
                set_ptr = &set;
3461
            } else {
3462
                how = 0;
3463
                set_ptr = NULL;
3464
            }
3465
            ret = get_errno(sigprocmask(how, set_ptr, &oldset));
3466
            if (!is_error(ret) && arg3) {
3467
                if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3468
                    goto efault;
3469
                host_to_target_sigset(p, &oldset);
3470
                unlock_user(p, arg3, sizeof(target_sigset_t));
3471
            }
3472
        }
3473
        break;
3474
#ifdef TARGET_NR_sigpending
3475
    case TARGET_NR_sigpending:
3476
        {
3477
            sigset_t set;
3478
            ret = get_errno(sigpending(&set));
3479
            if (!is_error(ret)) {
3480
                if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3481
                    goto efault;
3482
                host_to_target_old_sigset(p, &set);
3483
                unlock_user(p, arg1, sizeof(target_sigset_t));
3484
            }
3485
        }
3486
        break;
3487
#endif
3488
    case TARGET_NR_rt_sigpending:
3489
        {
3490
            sigset_t set;
3491
            ret = get_errno(sigpending(&set));
3492
            if (!is_error(ret)) {
3493
                if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3494
                    goto efault;
3495
                host_to_target_sigset(p, &set);
3496
                unlock_user(p, arg1, sizeof(target_sigset_t));
3497
            }
3498
        }
3499
        break;
3500
#ifdef TARGET_NR_sigsuspend
3501
    case TARGET_NR_sigsuspend:
3502
        {
3503
            sigset_t set;
3504
            if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3505
                goto efault;
3506
            target_to_host_old_sigset(&set, p);
3507
            unlock_user(p, arg1, 0);
3508
            ret = get_errno(sigsuspend(&set));
3509
        }
3510
        break;
3511
#endif
3512
    case TARGET_NR_rt_sigsuspend:
3513
        {
3514
            sigset_t set;
3515
            if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3516
                goto efault;
3517
            target_to_host_sigset(&set, p);
3518
            unlock_user(p, arg1, 0);
3519
            ret = get_errno(sigsuspend(&set));
3520
        }
3521
        break;
3522
    case TARGET_NR_rt_sigtimedwait:
3523
        {
3524
            sigset_t set;
3525
            struct timespec uts, *puts;
3526
            siginfo_t uinfo;
3527

    
3528
            if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3529
                goto efault;
3530
            target_to_host_sigset(&set, p);
3531
            unlock_user(p, arg1, 0);
3532
            if (arg3) {
3533
                puts = &uts;
3534
                target_to_host_timespec(puts, arg3);
3535
            } else {
3536
                puts = NULL;
3537
            }
3538
            ret = get_errno(sigtimedwait(&set, &uinfo, puts));
3539
            if (!is_error(ret) && arg2) {
3540
                if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3541
                    goto efault;
3542
                host_to_target_siginfo(p, &uinfo);
3543
                unlock_user(p, arg2, sizeof(target_sigset_t));
3544
            }
3545
        }
3546
        break;
3547
    case TARGET_NR_rt_sigqueueinfo:
3548
        {
3549
            siginfo_t uinfo;
3550
            if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3551
                goto efault;
3552
            target_to_host_siginfo(&uinfo, p);
3553
            unlock_user(p, arg1, 0);
3554
            ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3555
        }
3556
        break;
3557
#ifdef TARGET_NR_sigreturn
3558
    case TARGET_NR_sigreturn:
3559
        /* NOTE: ret is eax, so not transcoding must be done */
3560
        ret = do_sigreturn(cpu_env);
3561
        break;
3562
#endif
3563
    case TARGET_NR_rt_sigreturn:
3564
        /* NOTE: ret is eax, so not transcoding must be done */
3565
        ret = do_rt_sigreturn(cpu_env);
3566
        break;
3567
    case TARGET_NR_sethostname:
3568
        if (!(p = lock_user_string(arg1)))
3569
            goto efault;
3570
        ret = get_errno(sethostname(p, arg2));
3571
        unlock_user(p, arg1, 0);
3572
        break;
3573
    case TARGET_NR_setrlimit:
3574
        {
3575
            /* XXX: convert resource ? */
3576
            int resource = arg1;
3577
            struct target_rlimit *target_rlim;
3578
            struct rlimit rlim;
3579
            if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3580
                goto efault;
3581
            rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3582
            rlim.rlim_max = tswapl(target_rlim->rlim_max);
3583
            unlock_user_struct(target_rlim, arg2, 0);
3584
            ret = get_errno(setrlimit(resource, &rlim));
3585
        }
3586
        break;
3587
    case TARGET_NR_getrlimit:
3588
        {
3589
            /* XXX: convert resource ? */
3590
            int resource = arg1;
3591
            struct target_rlimit *target_rlim;
3592
            struct rlimit rlim;
3593

    
3594
            ret = get_errno(getrlimit(resource, &rlim));
3595
            if (!is_error(ret)) {
3596
                if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3597
                    goto efault;
3598
                rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3599
                rlim.rlim_max = tswapl(target_rlim->rlim_max);
3600
                unlock_user_struct(target_rlim, arg2, 1);
3601
            }
3602
        }
3603
        break;
3604
    case TARGET_NR_getrusage:
3605
        {
3606
            struct rusage rusage;
3607
            ret = get_errno(getrusage(arg1, &rusage));
3608
            if (!is_error(ret)) {
3609
                host_to_target_rusage(arg2, &rusage);
3610
            }
3611
        }
3612
        break;
3613
    case TARGET_NR_gettimeofday:
3614
        {
3615
            struct timeval tv;
3616
            ret = get_errno(gettimeofday(&tv, NULL));
3617
            if (!is_error(ret)) {
3618
                host_to_target_timeval(arg1, &tv);
3619
            }
3620
        }
3621
        break;
3622
    case TARGET_NR_settimeofday:
3623
        {
3624
            struct timeval tv;
3625
            target_to_host_timeval(&tv, arg1);
3626
            ret = get_errno(settimeofday(&tv, NULL));
3627
        }
3628
        break;
3629
#ifdef TARGET_NR_select
3630
    case TARGET_NR_select:
3631
        {
3632
            struct target_sel_arg_struct *sel;
3633
            abi_ulong inp, outp, exp, tvp;
3634
            long nsel;
3635

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

    
3845
            if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
3846
                goto efault;
3847
            __put_user(stfs.f_type, &target_stfs->f_type);
3848
            __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3849
            __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3850
            __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3851
            __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3852
            __put_user(stfs.f_files, &target_stfs->f_files);
3853
            __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3854
            __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3855
            __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3856
            __put_user(stfs.f_namelen, &target_stfs->f_namelen);
3857
            unlock_user_struct(target_stfs, arg2, 1);
3858
        }
3859
        break;
3860
    case TARGET_NR_fstatfs:
3861
        ret = get_errno(fstatfs(arg1, &stfs));
3862
        goto convert_statfs;
3863
#ifdef TARGET_NR_statfs64
3864
    case TARGET_NR_statfs64:
3865
        if (!(p = lock_user_string(arg1)))
3866
            goto efault;
3867
        ret = get_errno(statfs(path(p), &stfs));
3868
        unlock_user(p, arg1, 0);
3869
    convert_statfs64:
3870
        if (!is_error(ret)) {
3871
            struct target_statfs64 *target_stfs;
3872

    
3873
            if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
3874
                goto efault;
3875
            __put_user(stfs.f_type, &target_stfs->f_type);
3876
            __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3877
            __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3878
            __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3879
            __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3880
            __put_user(stfs.f_files, &target_stfs->f_files);
3881
            __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3882
            __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3883
            __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3884
            __put_user(stfs.f_namelen, &target_stfs->f_namelen);
3885
            unlock_user_struct(target_stfs, arg3, 1);
3886
        }
3887
        break;
3888
    case TARGET_NR_fstatfs64:
3889
        ret = get_errno(fstatfs(arg1, &stfs));
3890
        goto convert_statfs64;
3891
#endif
3892
#ifdef TARGET_NR_ioperm
3893
    case TARGET_NR_ioperm:
3894
        goto unimplemented;
3895
#endif
3896
#ifdef TARGET_NR_socketcall
3897
    case TARGET_NR_socketcall:
3898
        ret = do_socketcall(arg1, arg2);
3899
        break;
3900
#endif
3901
#ifdef TARGET_NR_accept
3902
    case TARGET_NR_accept:
3903
        ret = do_accept(arg1, arg2, arg3);
3904
        break;
3905
#endif
3906
#ifdef TARGET_NR_bind
3907
    case TARGET_NR_bind:
3908
        ret = do_bind(arg1, arg2, arg3);
3909
        break;
3910
#endif
3911
#ifdef TARGET_NR_connect
3912
    case TARGET_NR_connect:
3913
        ret = do_connect(arg1, arg2, arg3);
3914
        break;
3915
#endif
3916
#ifdef TARGET_NR_getpeername
3917
    case TARGET_NR_getpeername:
3918
        ret = do_getpeername(arg1, arg2, arg3);
3919
        break;
3920
#endif
3921
#ifdef TARGET_NR_getsockname
3922
    case TARGET_NR_getsockname:
3923
        ret = do_getsockname(arg1, arg2, arg3);
3924
        break;
3925
#endif
3926
#ifdef TARGET_NR_getsockopt
3927
    case TARGET_NR_getsockopt:
3928
        ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3929
        break;
3930
#endif
3931
#ifdef TARGET_NR_listen
3932
    case TARGET_NR_listen:
3933
        ret = get_errno(listen(arg1, arg2));
3934
        break;
3935
#endif
3936
#ifdef TARGET_NR_recv
3937
    case TARGET_NR_recv:
3938
        ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3939
        break;
3940
#endif
3941
#ifdef TARGET_NR_recvfrom
3942
    case TARGET_NR_recvfrom:
3943
        ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3944
        break;
3945
#endif
3946
#ifdef TARGET_NR_recvmsg
3947
    case TARGET_NR_recvmsg:
3948
        ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3949
        break;
3950
#endif
3951
#ifdef TARGET_NR_send
3952
    case TARGET_NR_send:
3953
        ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3954
        break;
3955
#endif
3956
#ifdef TARGET_NR_sendmsg
3957
    case TARGET_NR_sendmsg:
3958
        ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3959
        break;
3960
#endif
3961
#ifdef TARGET_NR_sendto
3962
    case TARGET_NR_sendto:
3963
        ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3964
        break;
3965
#endif
3966
#ifdef TARGET_NR_shutdown
3967
    case TARGET_NR_shutdown:
3968
        ret = get_errno(shutdown(arg1, arg2));
3969
        break;
3970
#endif
3971
#ifdef TARGET_NR_socket
3972
    case TARGET_NR_socket:
3973
        ret = do_socket(arg1, arg2, arg3);
3974
        break;
3975
#endif
3976
#ifdef TARGET_NR_socketpair
3977
    case TARGET_NR_socketpair:
3978
        ret = do_socketpair(arg1, arg2, arg3, arg4);
3979
        break;
3980
#endif
3981
#ifdef TARGET_NR_setsockopt
3982
    case TARGET_NR_setsockopt:
3983
        ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3984
        break;
3985
#endif
3986

    
3987
    case TARGET_NR_syslog:
3988
        if (!(p = lock_user_string(arg2)))
3989
            goto efault;
3990
        ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3991
        unlock_user(p, arg2, 0);
3992
        break;
3993

    
3994
    case TARGET_NR_setitimer:
3995
        {
3996
            struct itimerval value, ovalue, *pvalue;
3997

    
3998
            if (arg2) {
3999
                pvalue = &value;
4000
                target_to_host_timeval(&pvalue->it_interval,
4001
                                       arg2);
4002
                target_to_host_timeval(&pvalue->it_value,
4003
                                       arg2 + sizeof(struct target_timeval));
4004
            } else {
4005
                pvalue = NULL;
4006
            }
4007
            ret = get_errno(setitimer(arg1, pvalue, &ovalue));
4008
            if (!is_error(ret) && arg3) {
4009
                host_to_target_timeval(arg3,
4010
                                       &ovalue.it_interval);
4011
                host_to_target_timeval(arg3 + sizeof(struct target_timeval),
4012
                                       &ovalue.it_value);
4013
            }
4014
        }
4015
        break;
4016
    case TARGET_NR_getitimer:
4017
        {
4018
            struct itimerval value;
4019

    
4020
            ret = get_errno(getitimer(arg1, &value));
4021
            if (!is_error(ret) && arg2) {
4022
                host_to_target_timeval(arg2,
4023
                                       &value.it_interval);
4024
                host_to_target_timeval(arg2 + sizeof(struct target_timeval),
4025
                                       &value.it_value);
4026
            }
4027
        }
4028
        break;
4029
    case TARGET_NR_stat:
4030
        if (!(p = lock_user_string(arg1)))
4031
            goto efault;
4032
        ret = get_errno(stat(path(p), &st));
4033
        unlock_user(p, arg1, 0);
4034
        goto do_stat;
4035
    case TARGET_NR_lstat:
4036
        if (!(p = lock_user_string(arg1)))
4037
            goto efault;
4038
        ret = get_errno(lstat(path(p), &st));
4039
        unlock_user(p, arg1, 0);
4040
        goto do_stat;
4041
    case TARGET_NR_fstat:
4042
        {
4043
            ret = get_errno(fstat(arg1, &st));
4044
        do_stat:
4045
            if (!is_error(ret)) {
4046
                struct target_stat *target_st;
4047

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

    
4194
            if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4195
                goto efault;
4196
            ret = get_errno(sys_uname(buf));
4197
            if (!is_error(ret)) {
4198
                /* Overrite the native machine name with whatever is being
4199
                   emulated. */
4200
                strcpy (buf->machine, UNAME_MACHINE);
4201
                /* Allow the user to override the reported release.  */
4202
                if (qemu_uname_release && *qemu_uname_release)
4203
                  strcpy (buf->release, qemu_uname_release);
4204
            }
4205
            unlock_user_struct(buf, arg1, 1);
4206
        }
4207
        break;
4208
#ifdef TARGET_I386
4209
    case TARGET_NR_modify_ldt:
4210
        ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
4211
        break;
4212
#if !defined(TARGET_X86_64)
4213
    case TARGET_NR_vm86old:
4214
        goto unimplemented;
4215
    case TARGET_NR_vm86:
4216
        ret = do_vm86(cpu_env, arg1, arg2);
4217
        break;
4218
#endif
4219
#endif
4220
    case TARGET_NR_adjtimex:
4221
        goto unimplemented;
4222
#ifdef TARGET_NR_create_module
4223
    case TARGET_NR_create_module:
4224
#endif
4225
    case TARGET_NR_init_module:
4226
    case TARGET_NR_delete_module:
4227
#ifdef TARGET_NR_get_kernel_syms
4228
    case TARGET_NR_get_kernel_syms:
4229
#endif
4230
        goto unimplemented;
4231
    case TARGET_NR_quotactl:
4232
        goto unimplemented;
4233
    case TARGET_NR_getpgid:
4234
        ret = get_errno(getpgid(arg1));
4235
        break;
4236
    case TARGET_NR_fchdir:
4237
        ret = get_errno(fchdir(arg1));
4238
        break;
4239
#ifdef TARGET_NR_bdflush /* not on x86_64 */
4240
    case TARGET_NR_bdflush:
4241
        goto unimplemented;
4242
#endif
4243
#ifdef TARGET_NR_sysfs
4244
    case TARGET_NR_sysfs:
4245
        goto unimplemented;
4246
#endif
4247
    case TARGET_NR_personality:
4248
        ret = get_errno(personality(arg1));
4249
        break;
4250
#ifdef TARGET_NR_afs_syscall
4251
    case TARGET_NR_afs_syscall:
4252
        goto unimplemented;
4253
#endif
4254
#ifdef TARGET_NR__llseek /* Not on alpha */
4255
    case TARGET_NR__llseek:
4256
        {
4257
#if defined (__x86_64__)
4258
            ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
4259
            tput64(arg4, ret);
4260
#else
4261
            int64_t res;
4262
            ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4263
            tput64(arg4, res);
4264
#endif
4265
        }
4266
        break;
4267
#endif
4268
    case TARGET_NR_getdents:
4269
#if TARGET_ABI_BITS != 32
4270
        goto unimplemented;
4271
#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4272
        {
4273
            struct target_dirent *target_dirp;
4274
            struct dirent *dirp;
4275
            abi_long count = arg3;
4276

    
4277
            dirp = malloc(count);
4278
            if (!dirp) {
4279
                ret = -TARGET_ENOMEM;
4280
                goto fail;
4281
            }
4282

    
4283
            ret = get_errno(sys_getdents(arg1, dirp, count));
4284
            if (!is_error(ret)) {
4285
                struct dirent *de;
4286
                struct target_dirent *tde;
4287
                int len = ret;
4288
                int reclen, treclen;
4289
                int count1, tnamelen;
4290

    
4291
                count1 = 0;
4292
                de = dirp;
4293
                if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4294
                    goto efault;
4295
                tde = target_dirp;
4296
                while (len > 0) {
4297
                    reclen = de->d_reclen;
4298
                    treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4299
                    tde->d_reclen = tswap16(treclen);
4300
                    tde->d_ino = tswapl(de->d_ino);
4301
                    tde->d_off = tswapl(de->d_off);
4302
                    tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4303
                    if (tnamelen > 256)
4304
                        tnamelen = 256;
4305
                    /* XXX: may not be correct */
4306
                    strncpy(tde->d_name, de->d_name, tnamelen);
4307
                    de = (struct dirent *)((char *)de + reclen);
4308
                    len -= reclen;
4309
                    tde = (struct target_dirent *)((char *)tde + treclen);
4310
                    count1 += treclen;
4311
                }
4312
                ret = count1;
4313
                unlock_user(target_dirp, arg2, ret);
4314
            }
4315
            free(dirp);
4316
        }
4317
#else
4318
        {
4319
            struct dirent *dirp;
4320
            abi_long count = arg3;
4321

    
4322
            if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4323
                goto efault;
4324
            ret = get_errno(sys_getdents(arg1, dirp, count));
4325
            if (!is_error(ret)) {
4326
                struct dirent *de;
4327
                int len = ret;
4328
                int reclen;
4329
                de = dirp;
4330
                while (len > 0) {
4331
                    reclen = de->d_reclen;
4332
                    if (reclen > len)
4333
                        break;
4334
                    de->d_reclen = tswap16(reclen);
4335
                    tswapls(&de->d_ino);
4336
                    tswapls(&de->d_off);
4337
                    de = (struct dirent *)((char *)de + reclen);
4338
                    len -= reclen;
4339
                }
4340
            }
4341
            unlock_user(dirp, arg2, ret);
4342
        }
4343
#endif
4344
        break;
4345
#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
4346
    case TARGET_NR_getdents64:
4347
        {
4348
            struct dirent64 *dirp;
4349
            abi_long count = arg3;
4350
            if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4351
                goto efault;
4352
            ret = get_errno(sys_getdents64(arg1, dirp, count));
4353
            if (!is_error(ret)) {
4354
                struct dirent64 *de;
4355
                int len = ret;
4356
                int reclen;
4357
                de = dirp;
4358
                while (len > 0) {
4359
                    reclen = de->d_reclen;
4360
                    if (reclen > len)
4361
                        break;
4362
                    de->d_reclen = tswap16(reclen);
4363
                    tswap64s(&de->d_ino);
4364
                    tswap64s(&de->d_off);
4365
                    de = (struct dirent64 *)((char *)de + reclen);
4366
                    len -= reclen;
4367
                }
4368
            }
4369
            unlock_user(dirp, arg2, ret);
4370
        }
4371
        break;
4372
#endif /* TARGET_NR_getdents64 */
4373
#ifdef TARGET_NR__newselect
4374
    case TARGET_NR__newselect:
4375
        ret = do_select(arg1, arg2, arg3, arg4, arg5);
4376
        break;
4377
#endif
4378
#ifdef TARGET_NR_poll
4379
    case TARGET_NR_poll:
4380
        {
4381
            struct target_pollfd *target_pfd;
4382
            unsigned int nfds = arg2;
4383
            int timeout = arg3;
4384
            struct pollfd *pfd;
4385
            unsigned int i;
4386

    
4387
            target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4388
            if (!target_pfd)
4389
                goto efault;
4390
            pfd = alloca(sizeof(struct pollfd) * nfds);
4391
            for(i = 0; i < nfds; i++) {
4392
                pfd[i].fd = tswap32(target_pfd[i].fd);
4393
                pfd[i].events = tswap16(target_pfd[i].events);
4394
            }
4395
            ret = get_errno(poll(pfd, nfds, timeout));
4396
            if (!is_error(ret)) {
4397
                for(i = 0; i < nfds; i++) {
4398
                    target_pfd[i].revents = tswap16(pfd[i].revents);
4399
                }
4400
                ret += nfds * (sizeof(struct target_pollfd)
4401
                               - sizeof(struct pollfd));
4402
            }
4403
            unlock_user(target_pfd, arg1, ret);
4404
        }
4405
        break;
4406
#endif
4407
    case TARGET_NR_flock:
4408
        /* NOTE: the flock constant seems to be the same for every
4409
           Linux platform */
4410
        ret = get_errno(flock(arg1, arg2));
4411
        break;
4412
    case TARGET_NR_readv:
4413
        {
4414
            int count = arg3;
4415
            struct iovec *vec;
4416

    
4417
            vec = alloca(count * sizeof(struct iovec));
4418
            lock_iovec(VERIFY_WRITE, vec, arg2, count, 0);
4419
            ret = get_errno(readv(arg1, vec, count));
4420
            unlock_iovec(vec, arg2, count, 1);
4421
        }
4422
        break;
4423
    case TARGET_NR_writev:
4424
        {
4425
            int count = arg3;
4426
            struct iovec *vec;
4427

    
4428
            vec = alloca(count * sizeof(struct iovec));
4429
            lock_iovec(VERIFY_READ, vec, arg2, count, 1);
4430
            ret = get_errno(writev(arg1, vec, count));
4431
            unlock_iovec(vec, arg2, count, 0);
4432
        }
4433
        break;
4434
    case TARGET_NR_getsid:
4435
        ret = get_errno(getsid(arg1));
4436
        break;
4437
#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
4438
    case TARGET_NR_fdatasync:
4439
        ret = get_errno(fdatasync(arg1));
4440
        break;
4441
#endif
4442
    case TARGET_NR__sysctl:
4443
        /* We don't implement this, but ENOTDIR is always a safe
4444
           return value. */
4445
        ret = -TARGET_ENOTDIR;
4446
        break;
4447
    case TARGET_NR_sched_setparam:
4448
        {
4449
            struct sched_param *target_schp;
4450
            struct sched_param schp;
4451

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

    
4641
                    if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4642
                        goto efault;
4643
                    memset(target_st, 0, sizeof(struct target_eabi_stat64));
4644
                    __put_user(st.st_dev, &target_st->st_dev);
4645
                    __put_user(st.st_ino, &target_st->st_ino);
4646
#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4647
                    __put_user(st.st_ino, &target_st->__st_ino);
4648
#endif
4649
                    __put_user(st.st_mode, &target_st->st_mode);
4650
                    __put_user(st.st_nlink, &target_st->st_nlink);
4651
                    __put_user(st.st_uid, &target_st->st_uid);
4652
                    __put_user(st.st_gid, &target_st->st_gid);
4653
                    __put_user(st.st_rdev, &target_st->st_rdev);
4654
                    __put_user(st.st_size, &target_st->st_size);
4655
                    __put_user(st.st_blksize, &target_st->st_blksize);
4656
                    __put_user(st.st_blocks, &target_st->st_blocks);
4657
                    __put_user(st.st_atime, &target_st->target_st_atime);
4658
                    __put_user(st.st_mtime, &target_st->target_st_mtime);
4659
                    __put_user(st.st_ctime, &target_st->target_st_ctime);
4660
                    unlock_user_struct(target_st, arg2, 1);
4661
                } else
4662
#endif
4663
                {
4664
                    struct target_stat64 *target_st;
4665

    
4666
                    if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4667
                        goto efault;
4668
                    memset(target_st, 0, sizeof(struct target_stat64));
4669
                    __put_user(st.st_dev, &target_st->st_dev);
4670
                    __put_user(st.st_ino, &target_st->st_ino);
4671
#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4672
                    __put_user(st.st_ino, &target_st->__st_ino);
4673
#endif
4674
                    __put_user(st.st_mode, &target_st->st_mode);
4675
                    __put_user(st.st_nlink, &target_st->st_nlink);
4676
                    __put_user(st.st_uid, &target_st->st_uid);
4677
                    __put_user(st.st_gid, &target_st->st_gid);
4678
                    __put_user(st.st_rdev, &target_st->st_rdev);
4679
                    /* XXX: better use of kernel struct */
4680
                    __put_user(st.st_size, &target_st->st_size);
4681
                    __put_user(st.st_blksize, &target_st->st_blksize);
4682
                    __put_user(st.st_blocks, &target_st->st_blocks);
4683
                    __put_user(st.st_atime, &target_st->target_st_atime);
4684
                    __put_user(st.st_mtime, &target_st->target_st_mtime);
4685
                    __put_user(st.st_ctime, &target_st->target_st_ctime);
4686
                    unlock_user_struct(target_st, arg2, 1);
4687
                }
4688
            }
4689
        }
4690
        break;
4691
#endif
4692
#ifdef USE_UID16
4693
    case TARGET_NR_lchown:
4694
        if (!(p = lock_user_string(arg1)))
4695
            goto efault;
4696
        ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4697
        unlock_user(p, arg1, 0);
4698
        break;
4699
    case TARGET_NR_getuid:
4700
        ret = get_errno(high2lowuid(getuid()));
4701
        break;
4702
    case TARGET_NR_getgid:
4703
        ret = get_errno(high2lowgid(getgid()));
4704
        break;
4705
    case TARGET_NR_geteuid:
4706
        ret = get_errno(high2lowuid(geteuid()));
4707
        break;
4708
    case TARGET_NR_getegid:
4709
        ret = get_errno(high2lowgid(getegid()));
4710
        break;
4711
    case TARGET_NR_setreuid:
4712
        ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4713
        break;
4714
    case TARGET_NR_setregid:
4715
        ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4716
        break;
4717
    case TARGET_NR_getgroups:
4718
        {
4719
            int gidsetsize = arg1;
4720
            uint16_t *target_grouplist;
4721
            gid_t *grouplist;
4722
            int i;
4723

    
4724
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4725
            ret = get_errno(getgroups(gidsetsize, grouplist));
4726
            if (!is_error(ret)) {
4727
                target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
4728
                if (!target_grouplist)
4729
                    goto efault;
4730
                for(i = 0;i < gidsetsize; i++)
4731
                    target_grouplist[i] = tswap16(grouplist[i]);
4732
                unlock_user(target_grouplist, arg2, gidsetsize * 2);
4733
            }
4734
        }
4735
        break;
4736
    case TARGET_NR_setgroups:
4737
        {
4738
            int gidsetsize = arg1;
4739
            uint16_t *target_grouplist;
4740
            gid_t *grouplist;
4741
            int i;
4742

    
4743
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4744
            target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
4745
            if (!target_grouplist) {
4746
                ret = -TARGET_EFAULT;
4747
                goto fail;
4748
            }
4749
            for(i = 0;i < gidsetsize; i++)
4750
                grouplist[i] = tswap16(target_grouplist[i]);
4751
            unlock_user(target_grouplist, arg2, 0);
4752
            ret = get_errno(setgroups(gidsetsize, grouplist));
4753
        }
4754
        break;
4755
    case TARGET_NR_fchown:
4756
        ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4757
        break;
4758
#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4759
    case TARGET_NR_fchownat:
4760
        if (!(p = lock_user_string(arg2))) 
4761
            goto efault;
4762
        ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4763
        unlock_user(p, arg2, 0);
4764
        break;
4765
#endif
4766
#ifdef TARGET_NR_setresuid
4767
    case TARGET_NR_setresuid:
4768
        ret = get_errno(setresuid(low2highuid(arg1),
4769
                                  low2highuid(arg2),
4770
                                  low2highuid(arg3)));
4771
        break;
4772
#endif
4773
#ifdef TARGET_NR_getresuid
4774
    case TARGET_NR_getresuid:
4775
        {
4776
            uid_t ruid, euid, suid;
4777
            ret = get_errno(getresuid(&ruid, &euid, &suid));
4778
            if (!is_error(ret)) {
4779
                tput16(arg1, tswap16(high2lowuid(ruid)));
4780
                tput16(arg2, tswap16(high2lowuid(euid)));
4781
                tput16(arg3, tswap16(high2lowuid(suid)));
4782
            }
4783
        }
4784
        break;
4785
#endif
4786
#ifdef TARGET_NR_getresgid
4787
    case TARGET_NR_setresgid:
4788
        ret = get_errno(setresgid(low2highgid(arg1),
4789
                                  low2highgid(arg2),
4790
                                  low2highgid(arg3)));
4791
        break;
4792
#endif
4793
#ifdef TARGET_NR_getresgid
4794
    case TARGET_NR_getresgid:
4795
        {
4796
            gid_t rgid, egid, sgid;
4797
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
4798
            if (!is_error(ret)) {
4799
                tput16(arg1, tswap16(high2lowgid(rgid)));
4800
                tput16(arg2, tswap16(high2lowgid(egid)));
4801
                tput16(arg3, tswap16(high2lowgid(sgid)));
4802
            }
4803
        }
4804
        break;
4805
#endif
4806
    case TARGET_NR_chown:
4807
        if (!(p = lock_user_string(arg1)))
4808
            goto efault;
4809
        ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4810
        unlock_user(p, arg1, 0);
4811
        break;
4812
    case TARGET_NR_setuid:
4813
        ret = get_errno(setuid(low2highuid(arg1)));
4814
        break;
4815
    case TARGET_NR_setgid:
4816
        ret = get_errno(setgid(low2highgid(arg1)));
4817
        break;
4818
    case TARGET_NR_setfsuid:
4819
        ret = get_errno(setfsuid(arg1));
4820
        break;
4821
    case TARGET_NR_setfsgid:
4822
        ret = get_errno(setfsgid(arg1));
4823
        break;
4824
#endif /* USE_UID16 */
4825

    
4826
#ifdef TARGET_NR_lchown32
4827
    case TARGET_NR_lchown32:
4828
        if (!(p = lock_user_string(arg1)))
4829
            goto efault;
4830
        ret = get_errno(lchown(p, arg2, arg3));
4831
        unlock_user(p, arg1, 0);
4832
        break;
4833
#endif
4834
#ifdef TARGET_NR_getuid32
4835
    case TARGET_NR_getuid32:
4836
        ret = get_errno(getuid());
4837
        break;
4838
#endif
4839
#ifdef TARGET_NR_getgid32
4840
    case TARGET_NR_getgid32:
4841
        ret = get_errno(getgid());
4842
        break;
4843
#endif
4844
#ifdef TARGET_NR_geteuid32
4845
    case TARGET_NR_geteuid32:
4846
        ret = get_errno(geteuid());
4847
        break;
4848
#endif
4849
#ifdef TARGET_NR_getegid32
4850
    case TARGET_NR_getegid32:
4851
        ret = get_errno(getegid());
4852
        break;
4853
#endif
4854
#ifdef TARGET_NR_setreuid32
4855
    case TARGET_NR_setreuid32:
4856
        ret = get_errno(setreuid(arg1, arg2));
4857
        break;
4858
#endif
4859
#ifdef TARGET_NR_setregid32
4860
    case TARGET_NR_setregid32:
4861
        ret = get_errno(setregid(arg1, arg2));
4862
        break;
4863
#endif
4864
#ifdef TARGET_NR_getgroups32
4865
    case TARGET_NR_getgroups32:
4866
        {
4867
            int gidsetsize = arg1;
4868
            uint32_t *target_grouplist;
4869
            gid_t *grouplist;
4870
            int i;
4871

    
4872
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4873
            ret = get_errno(getgroups(gidsetsize, grouplist));
4874
            if (!is_error(ret)) {
4875
                target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
4876
                if (!target_grouplist) {
4877
                    ret = -TARGET_EFAULT;
4878
                    goto fail;
4879
                }
4880
                for(i = 0;i < gidsetsize; i++)
4881
                    target_grouplist[i] = tswap32(grouplist[i]);
4882
                unlock_user(target_grouplist, arg2, gidsetsize * 4);
4883
            }
4884
        }
4885
        break;
4886
#endif
4887
#ifdef TARGET_NR_setgroups32
4888
    case TARGET_NR_setgroups32:
4889
        {
4890
            int gidsetsize = arg1;
4891
            uint32_t *target_grouplist;
4892
            gid_t *grouplist;
4893
            int i;
4894

    
4895
            grouplist = alloca(gidsetsize * sizeof(gid_t));
4896
            target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
4897
            if (!target_grouplist) {
4898
                ret = -TARGET_EFAULT;
4899
                goto fail;
4900
            }
4901
            for(i = 0;i < gidsetsize; i++)
4902
                grouplist[i] = tswap32(target_grouplist[i]);
4903
            unlock_user(target_grouplist, arg2, 0);
4904
            ret = get_errno(setgroups(gidsetsize, grouplist));
4905
        }
4906
        break;
4907
#endif
4908
#ifdef TARGET_NR_fchown32
4909
    case TARGET_NR_fchown32:
4910
        ret = get_errno(fchown(arg1, arg2, arg3));
4911
        break;
4912
#endif
4913
#ifdef TARGET_NR_setresuid32
4914
    case TARGET_NR_setresuid32:
4915
        ret = get_errno(setresuid(arg1, arg2, arg3));
4916
        break;
4917
#endif
4918
#ifdef TARGET_NR_getresuid32
4919
    case TARGET_NR_getresuid32:
4920
        {
4921
            uid_t ruid, euid, suid;
4922
            ret = get_errno(getresuid(&ruid, &euid, &suid));
4923
            if (!is_error(ret)) {
4924
                tput32(arg1, tswap32(ruid));
4925
                tput32(arg2, tswap32(euid));
4926
                tput32(arg3, tswap32(suid));
4927
            }
4928
        }
4929
        break;
4930
#endif
4931
#ifdef TARGET_NR_setresgid32
4932
    case TARGET_NR_setresgid32:
4933
        ret = get_errno(setresgid(arg1, arg2, arg3));
4934
        break;
4935
#endif
4936
#ifdef TARGET_NR_getresgid32
4937
    case TARGET_NR_getresgid32:
4938
        {
4939
            gid_t rgid, egid, sgid;
4940
            ret = get_errno(getresgid(&rgid, &egid, &sgid));
4941
            if (!is_error(ret)) {
4942
                tput32(arg1, tswap32(rgid));
4943
                tput32(arg2, tswap32(egid));
4944
                tput32(arg3, tswap32(sgid));
4945
            }
4946
        }
4947
        break;
4948
#endif
4949
#ifdef TARGET_NR_chown32
4950
    case TARGET_NR_chown32:
4951
        if (!(p = lock_user_string(arg1)))
4952
            goto efault;
4953
        ret = get_errno(chown(p, arg2, arg3));
4954
        unlock_user(p, arg1, 0);
4955
        break;
4956
#endif
4957
#ifdef TARGET_NR_setuid32
4958
    case TARGET_NR_setuid32:
4959
        ret = get_errno(setuid(arg1));
4960
        break;
4961
#endif
4962
#ifdef TARGET_NR_setgid32
4963
    case TARGET_NR_setgid32:
4964
        ret = get_errno(setgid(arg1));
4965
        break;
4966
#endif
4967
#ifdef TARGET_NR_setfsuid32
4968
    case TARGET_NR_setfsuid32:
4969
        ret = get_errno(setfsuid(arg1));
4970
        break;
4971
#endif
4972
#ifdef TARGET_NR_setfsgid32
4973
    case TARGET_NR_setfsgid32:
4974
        ret = get_errno(setfsgid(arg1));
4975
        break;
4976
#endif
4977

    
4978
    case TARGET_NR_pivot_root:
4979
        goto unimplemented;
4980
#ifdef TARGET_NR_mincore
4981
    case TARGET_NR_mincore:
4982
        goto unimplemented;
4983
#endif
4984
#ifdef TARGET_NR_madvise
4985
    case TARGET_NR_madvise:
4986
        /* A straight passthrough may not be safe because qemu sometimes
4987
           turns private flie-backed mappings into anonymous mappings.
4988
           This will break MADV_DONTNEED.
4989
           This is a hint, so ignoring and returning success is ok.  */
4990
        ret = get_errno(0);
4991
        break;
4992
#endif
4993
#if TARGET_ABI_BITS == 32
4994
    case TARGET_NR_fcntl64:
4995
    {
4996
        int cmd;
4997