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

#define termios host_termios
#define winsize host_winsize
#define termio host_termio
bellard's avatar
bellard committed
59
60
61
#define sgttyb host_sgttyb /* same as target */
#define tchars host_tchars /* same as target */
#define ltchars host_ltchars /* same as target */
62
63
64
65
66
67
68

#include <linux/termios.h>
#include <linux/unistd.h>
#include <linux/utsname.h>
#include <linux/cdrom.h>
#include <linux/hdreg.h>
#include <linux/soundcard.h>
bellard's avatar
bellard committed
69
#include <linux/dirent.h>
70
#include <linux/kd.h>
71

bellard's avatar
bellard committed
72
#include "qemu.h"
73

bellard's avatar
bellard committed
74
//#define DEBUG
75

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

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

86
87
88
89
90
91
92

#undef _syscall0
#undef _syscall1
#undef _syscall2
#undef _syscall3
#undef _syscall4
#undef _syscall5
bellard's avatar
bellard committed
93
#undef _syscall6
94

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

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

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

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

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

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

bellard's avatar
bellard committed
132
133
134
135
136
137

#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,	\
		  type5,arg5,type6,arg6)					\
type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6)	\
{										\
	return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6);	\
138
}
bellard's avatar
bellard committed
139

140

141
#define __NR_sys_uname __NR_uname
bellard's avatar
bellard committed
142
143
#define __NR_sys_getcwd1 __NR_getcwd
#define __NR_sys_getdents __NR_getdents
bellard's avatar
bellard committed
144
#define __NR_sys_getdents64 __NR_getdents64
bellard's avatar
bellard committed
145
#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
146
#define __NR_sys_syslog __NR_syslog
ths's avatar
ths committed
147
#define __NR_sys_tgkill __NR_tgkill
148

149
#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
bellard's avatar
bellard committed
150
151
152
#define __NR__llseek __NR_lseek
#endif

bellard's avatar
bellard committed
153
#ifdef __NR_gettid
154
_syscall0(int, gettid)
bellard's avatar
bellard committed
155
156
157
158
159
#else
static int gettid(void) {
    return -ENOSYS;
}
#endif
160
_syscall1(int,sys_uname,struct new_utsname *,buf)
bellard's avatar
bellard committed
161
162
_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
bellard's avatar
bellard committed
163
_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
164
165
_syscall5(int, _llseek,  uint,  fd, ulong, hi, ulong, lo,
          loff_t *, res, uint, wh);
bellard's avatar
bellard committed
166
_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
167
_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
ths's avatar
ths committed
168
_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
169
170
171
#ifdef __NR_exit_group
_syscall1(int,exit_group,int,error_code)
#endif
172
173
174
#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
_syscall1(int,set_tid_address,int *,tidptr)
#endif
bellard's avatar
bellard committed
175
176

extern int personality(int);
bellard's avatar
bellard committed
177
178
179
extern int flock(int, int);
extern int setfsuid(int);
extern int setfsgid(int);
bellard's avatar
bellard committed
180
181
182
183
extern int setresuid(uid_t, uid_t, uid_t);
extern int getresuid(uid_t *, uid_t *, uid_t *);
extern int setresgid(gid_t, gid_t, gid_t);
extern int getresgid(gid_t *, gid_t *, gid_t *);
184
extern int setgroups(int, gid_t *);
185

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * This list is the union of errno values overidden in asm-<arch>/errno.h
 * minus the errnos that are not actually generic to all archs.
 */
static uint16_t host_to_target_errno_table[1200] = {
    [EIDRM]		= TARGET_EIDRM,
    [ECHRNG]		= TARGET_ECHRNG,
    [EL2NSYNC]		= TARGET_EL2NSYNC,
    [EL3HLT]		= TARGET_EL3HLT,
    [EL3RST]		= TARGET_EL3RST,
    [ELNRNG]		= TARGET_ELNRNG,
    [EUNATCH]		= TARGET_EUNATCH,
    [ENOCSI]		= TARGET_ENOCSI,
    [EL2HLT]		= TARGET_EL2HLT,
    [EDEADLK]		= TARGET_EDEADLK,
    [ENOLCK]		= TARGET_ENOLCK,
    [EBADE]		= TARGET_EBADE,
    [EBADR]		= TARGET_EBADR,
    [EXFULL]		= TARGET_EXFULL,
    [ENOANO]		= TARGET_ENOANO,
    [EBADRQC]		= TARGET_EBADRQC,
    [EBADSLT]		= TARGET_EBADSLT,
    [EBFONT]		= TARGET_EBFONT,
    [ENOSTR]		= TARGET_ENOSTR,
    [ENODATA]		= TARGET_ENODATA,
    [ETIME]		= TARGET_ETIME,
    [ENOSR]		= TARGET_ENOSR,
    [ENONET]		= TARGET_ENONET,
    [ENOPKG]		= TARGET_ENOPKG,
    [EREMOTE]		= TARGET_EREMOTE,
    [ENOLINK]		= TARGET_ENOLINK,
    [EADV]		= TARGET_EADV,
    [ESRMNT]		= TARGET_ESRMNT,
    [ECOMM]		= TARGET_ECOMM,
    [EPROTO]		= TARGET_EPROTO,
    [EDOTDOT]		= TARGET_EDOTDOT,
    [EMULTIHOP]		= TARGET_EMULTIHOP,
    [EBADMSG]		= TARGET_EBADMSG,
    [ENAMETOOLONG]	= TARGET_ENAMETOOLONG,
    [EOVERFLOW]		= TARGET_EOVERFLOW,
    [ENOTUNIQ]		= TARGET_ENOTUNIQ,
    [EBADFD]		= TARGET_EBADFD,
    [EREMCHG]		= TARGET_EREMCHG,
    [ELIBACC]		= TARGET_ELIBACC,
    [ELIBBAD]		= TARGET_ELIBBAD,
    [ELIBSCN]		= TARGET_ELIBSCN,
    [ELIBMAX]		= TARGET_ELIBMAX,
    [ELIBEXEC]		= TARGET_ELIBEXEC,
    [EILSEQ]		= TARGET_EILSEQ,
    [ENOSYS]		= TARGET_ENOSYS,
    [ELOOP]		= TARGET_ELOOP,
    [ERESTART]		= TARGET_ERESTART,
    [ESTRPIPE]		= TARGET_ESTRPIPE,
    [ENOTEMPTY]		= TARGET_ENOTEMPTY,
    [EUSERS]		= TARGET_EUSERS,
    [ENOTSOCK]		= TARGET_ENOTSOCK,
    [EDESTADDRREQ]	= TARGET_EDESTADDRREQ,
    [EMSGSIZE]		= TARGET_EMSGSIZE,
    [EPROTOTYPE]	= TARGET_EPROTOTYPE,
    [ENOPROTOOPT]	= TARGET_ENOPROTOOPT,
    [EPROTONOSUPPORT]	= TARGET_EPROTONOSUPPORT,
    [ESOCKTNOSUPPORT]	= TARGET_ESOCKTNOSUPPORT,
    [EOPNOTSUPP]	= TARGET_EOPNOTSUPP,
    [EPFNOSUPPORT]	= TARGET_EPFNOSUPPORT,
    [EAFNOSUPPORT]	= TARGET_EAFNOSUPPORT,
    [EADDRINUSE]	= TARGET_EADDRINUSE,
    [EADDRNOTAVAIL]	= TARGET_EADDRNOTAVAIL,
    [ENETDOWN]		= TARGET_ENETDOWN,
    [ENETUNREACH]	= TARGET_ENETUNREACH,
    [ENETRESET]		= TARGET_ENETRESET,
    [ECONNABORTED]	= TARGET_ECONNABORTED,
    [ECONNRESET]	= TARGET_ECONNRESET,
    [ENOBUFS]		= TARGET_ENOBUFS,
    [EISCONN]		= TARGET_EISCONN,
    [ENOTCONN]		= TARGET_ENOTCONN,
    [EUCLEAN]		= TARGET_EUCLEAN,
    [ENOTNAM]		= TARGET_ENOTNAM,
    [ENAVAIL]		= TARGET_ENAVAIL,
    [EISNAM]		= TARGET_EISNAM,
    [EREMOTEIO]		= TARGET_EREMOTEIO,
    [ESHUTDOWN]		= TARGET_ESHUTDOWN,
    [ETOOMANYREFS]	= TARGET_ETOOMANYREFS,
    [ETIMEDOUT]		= TARGET_ETIMEDOUT,
    [ECONNREFUSED]	= TARGET_ECONNREFUSED,
    [EHOSTDOWN]		= TARGET_EHOSTDOWN,
    [EHOSTUNREACH]	= TARGET_EHOSTUNREACH,
    [EALREADY]		= TARGET_EALREADY,
    [EINPROGRESS]	= TARGET_EINPROGRESS,
    [ESTALE]		= TARGET_ESTALE,
    [ECANCELED]		= TARGET_ECANCELED,
    [ENOMEDIUM]		= TARGET_ENOMEDIUM,
    [EMEDIUMTYPE]	= TARGET_EMEDIUMTYPE,
    [ENOKEY]		= TARGET_ENOKEY,
    [EKEYEXPIRED]	= TARGET_EKEYEXPIRED,
    [EKEYREVOKED]	= TARGET_EKEYREVOKED,
    [EKEYREJECTED]	= TARGET_EKEYREJECTED,
    [EOWNERDEAD]	= TARGET_EOWNERDEAD,
    [ENOTRECOVERABLE]	= TARGET_ENOTRECOVERABLE,
	};

static inline int host_to_target_errno(int err)
{
    if(host_to_target_errno_table[err])
        return host_to_target_errno_table[err];
    return err;
}

293
294
295
static inline long get_errno(long ret)
{
    if (ret == -1)
296
        return -host_to_target_errno(errno);
297
298
299
300
301
302
303
304
305
    else
        return ret;
}

static inline int is_error(long ret)
{
    return (unsigned long)ret >= (unsigned long)(-4096);
}

306
307
static target_ulong target_brk;
static target_ulong target_original_brk;
308

309
void target_set_brk(target_ulong new_brk)
310
{
311
    target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
312
313
}

314
long do_brk(target_ulong new_brk)
315
{
316
    target_ulong brk_page;
317
318
319
320
    long mapped_addr;
    int	new_alloc_size;

    if (!new_brk)
321
        return target_brk;
322
323
324
    if (new_brk < target_original_brk)
        return -ENOMEM;
    
325
    brk_page = HOST_PAGE_ALIGN(target_brk);
326
327
328
329

    /* If the new brk is less than this, set it and we're done... */
    if (new_brk < brk_page) {
	target_brk = new_brk;
330
    	return target_brk;
331
332
333
    }

    /* We need to allocate more memory after the brk... */
bellard's avatar
bellard committed
334
    new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
335
    mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size, 
bellard's avatar
bellard committed
336
337
                                        PROT_READ|PROT_WRITE,
                                        MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
338
339
340
341
    if (is_error(mapped_addr)) {
	return mapped_addr;
    } else {
	target_brk = new_brk;
342
    	return target_brk;
343
344
345
346
347
348
    }
}

static inline fd_set *target_to_host_fds(fd_set *fds, 
                                         target_long *target_fds, int n)
{
bellard's avatar
bellard committed
349
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
    return (fd_set *)target_fds;
#else
    int i, b;
    if (target_fds) {
        FD_ZERO(fds);
        for(i = 0;i < n; i++) {
            b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
                 (i & (TARGET_LONG_BITS - 1))) & 1;
            if (b)
                FD_SET(i, fds);
        }
        return fds;
    } else {
        return NULL;
    }
#endif
}

static inline void host_to_target_fds(target_long *target_fds, 
                                      fd_set *fds, int n)
{
bellard's avatar
bellard committed
371
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
372
373
374
375
376
377
    /* nothing to do */
#else
    int i, nw, j, k;
    target_long v;

    if (target_fds) {
378
        nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
379
380
381
382
383
384
385
386
387
388
389
390
391
        k = 0;
        for(i = 0;i < nw; i++) {
            v = 0;
            for(j = 0; j < TARGET_LONG_BITS; j++) {
                v |= ((FD_ISSET(k, fds) != 0) << j);
                k++;
            }
            target_fds[i] = tswapl(v);
        }
    }
#endif
}

bellard's avatar
bellard committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#if defined(__alpha__)
#define HOST_HZ 1024
#else
#define HOST_HZ 100
#endif

static inline long host_to_target_clock_t(long ticks)
{
#if HOST_HZ == TARGET_HZ
    return ticks;
#else
    return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
#endif
}

407
static inline void host_to_target_rusage(target_ulong target_addr,
bellard's avatar
bellard committed
408
409
                                         const struct rusage *rusage)
{
410
411
412
    struct target_rusage *target_rusage;

    lock_user_struct(target_rusage, target_addr, 0);
bellard's avatar
bellard committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
    target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
    target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
    target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
    target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
    target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
    target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
    target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
    target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
    target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
    target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
    target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
    target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
    target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
    target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
    target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
    target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
    target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
    target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
431
    unlock_user_struct(target_rusage, target_addr, 1);
bellard's avatar
bellard committed
432
433
}

434
435
static inline void target_to_host_timeval(struct timeval *tv,
                                          target_ulong target_addr)
436
{
437
438
439
    struct target_timeval *target_tv;

    lock_user_struct(target_tv, target_addr, 1);
bellard's avatar
bellard committed
440
441
    tv->tv_sec = tswapl(target_tv->tv_sec);
    tv->tv_usec = tswapl(target_tv->tv_usec);
442
    unlock_user_struct(target_tv, target_addr, 0);
443
444
}

445
static inline void host_to_target_timeval(target_ulong target_addr,
bellard's avatar
bellard committed
446
                                          const struct timeval *tv)
447
{
448
449
450
    struct target_timeval *target_tv;

    lock_user_struct(target_tv, target_addr, 0);
bellard's avatar
bellard committed
451
452
    target_tv->tv_sec = tswapl(tv->tv_sec);
    target_tv->tv_usec = tswapl(tv->tv_usec);
453
    unlock_user_struct(target_tv, target_addr, 1);
454
455
456
457
}


static long do_select(long n, 
458
459
                      target_ulong rfd_p, target_ulong wfd_p, 
                      target_ulong efd_p, target_ulong target_tv)
460
461
462
{
    fd_set rfds, wfds, efds;
    fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
463
    target_long *target_rfds, *target_wfds, *target_efds;
464
465
    struct timeval tv, *tv_ptr;
    long ret;
466
    int ok;
467

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
    if (rfd_p) {
        target_rfds = lock_user(rfd_p, sizeof(target_long) * n, 1);
        rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
    } else {
        target_rfds = NULL;
        rfds_ptr = NULL;
    }
    if (wfd_p) {
        target_wfds = lock_user(wfd_p, sizeof(target_long) * n, 1);
        wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
    } else {
        target_wfds = NULL;
        wfds_ptr = NULL;
    }
    if (efd_p) {
        target_efds = lock_user(efd_p, sizeof(target_long) * n, 1);
        efds_ptr = target_to_host_fds(&efds, target_efds, n);
    } else {
        target_efds = NULL;
        efds_ptr = NULL;
    }
489
490
            
    if (target_tv) {
bellard's avatar
bellard committed
491
        target_to_host_timeval(&tv, target_tv);
492
493
494
495
496
        tv_ptr = &tv;
    } else {
        tv_ptr = NULL;
    }
    ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
497
498
499
    ok = !is_error(ret);

    if (ok) {
500
501
502
503
504
        host_to_target_fds(target_rfds, rfds_ptr, n);
        host_to_target_fds(target_wfds, wfds_ptr, n);
        host_to_target_fds(target_efds, efds_ptr, n);

        if (target_tv) {
bellard's avatar
bellard committed
505
            host_to_target_timeval(target_tv, &tv);
506
507
        }
    }
508
509
510
511
512
513
514
    if (target_rfds)
        unlock_user(target_rfds, rfd_p, ok ? sizeof(target_long) * n : 0);
    if (target_wfds)
        unlock_user(target_wfds, wfd_p, ok ? sizeof(target_long) * n : 0);
    if (target_efds)
        unlock_user(target_efds, efd_p, ok ? sizeof(target_long) * n : 0);

515
516
517
    return ret;
}

bellard's avatar
bellard committed
518
static inline void target_to_host_sockaddr(struct sockaddr *addr,
519
                                           target_ulong target_addr,
bellard's avatar
bellard committed
520
521
                                           socklen_t len)
{
522
523
524
525
526
527
    struct target_sockaddr *target_saddr;

    target_saddr = lock_user(target_addr, len, 1);
    memcpy(addr, target_saddr, len);
    addr->sa_family = tswap16(target_saddr->sa_family);
    unlock_user(target_saddr, target_addr, 0);
bellard's avatar
bellard committed
528
529
}

530
static inline void host_to_target_sockaddr(target_ulong target_addr,
bellard's avatar
bellard committed
531
532
533
                                           struct sockaddr *addr,
                                           socklen_t len)
{
534
535
536
537
538
539
    struct target_sockaddr *target_saddr;

    target_saddr = lock_user(target_addr, len, 0);
    memcpy(target_saddr, addr, len);
    target_saddr->sa_family = tswap16(addr->sa_family);
    unlock_user(target_saddr, target_addr, len);
bellard's avatar
bellard committed
540
541
}

542
/* ??? Should this also swap msgh->name?  */
bellard's avatar
bellard committed
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
static inline void target_to_host_cmsg(struct msghdr *msgh,
                                       struct target_msghdr *target_msgh)
{
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
    struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
    socklen_t space = 0;

    while (cmsg && target_cmsg) {
        void *data = CMSG_DATA(cmsg);
        void *target_data = TARGET_CMSG_DATA(target_cmsg);

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

        space += CMSG_SPACE(len);
        if (space > msgh->msg_controllen) {
            space -= CMSG_SPACE(len);
bellard's avatar
bellard committed
560
            gemu_log("Host cmsg overflow\n");
bellard's avatar
bellard committed
561
562
563
564
565
566
567
            break;
        }

        cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
        cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
        cmsg->cmsg_len = CMSG_LEN(len);

568
        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard's avatar
bellard committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
            memcpy(data, target_data, len);
        } else {
            int *fd = (int *)data;
            int *target_fd = (int *)target_data;
            int i, numfds = len / sizeof(int);

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

        cmsg = CMSG_NXTHDR(msgh, cmsg);
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
    }

    msgh->msg_controllen = space;
}

587
/* ??? Should this also swap msgh->name?  */
bellard's avatar
bellard committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
                                       struct msghdr *msgh)
{
    struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
    struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
    socklen_t space = 0;

    while (cmsg && target_cmsg) {
        void *data = CMSG_DATA(cmsg);
        void *target_data = TARGET_CMSG_DATA(target_cmsg);

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

        space += TARGET_CMSG_SPACE(len);
        if (space > tswapl(target_msgh->msg_controllen)) {
            space -= TARGET_CMSG_SPACE(len);
bellard's avatar
bellard committed
604
            gemu_log("Target cmsg overflow\n");
bellard's avatar
bellard committed
605
606
607
608
609
610
611
            break;
        }

        target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
        target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
        target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));

612
        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard's avatar
bellard committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
            gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
            memcpy(target_data, data, len);
        } else {
            int *fd = (int *)data;
            int *target_fd = (int *)target_data;
            int i, numfds = len / sizeof(int);

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

        cmsg = CMSG_NXTHDR(msgh, cmsg);
        target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
    }

    msgh->msg_controllen = tswapl(space);
}

static long do_setsockopt(int sockfd, int level, int optname, 
632
                          target_ulong optval, socklen_t optlen)
bellard's avatar
bellard committed
633
{
634
635
636
637
    int val, ret;
            
    switch(level) {
    case SOL_TCP:
bellard's avatar
bellard committed
638
639
640
        /* TCP options all take an 'int' value.  */
        if (optlen < sizeof(uint32_t))
            return -EINVAL;
641
        
642
        val = tget32(optval);
643
644
645
646
        ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
        break;
    case SOL_IP:
        switch(optname) {
bellard's avatar
bellard committed
647
648
        case IP_TOS:
        case IP_TTL:
649
        case IP_HDRINCL:
bellard's avatar
bellard committed
650
651
652
653
654
655
656
657
658
659
660
661
        case IP_ROUTER_ALERT:
        case IP_RECVOPTS:
        case IP_RETOPTS:
        case IP_PKTINFO:
        case IP_MTU_DISCOVER:
        case IP_RECVERR:
        case IP_RECVTOS:
#ifdef IP_FREEBIND
        case IP_FREEBIND:
#endif
        case IP_MULTICAST_TTL:
        case IP_MULTICAST_LOOP:
662
663
            val = 0;
            if (optlen >= sizeof(uint32_t)) {
664
                val = tget32(optval);
665
            } else if (optlen >= 1) {
666
                val = tget8(optval);
667
668
669
670
671
672
673
            }
            ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
            break;
        default:
            goto unimplemented;
        }
        break;
674
    case TARGET_SOL_SOCKET:
675
676
        switch (optname) {
            /* Options with 'int' argument.  */
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
        case TARGET_SO_DEBUG:
		optname = SO_DEBUG;
		break;
        case TARGET_SO_REUSEADDR:
		optname = SO_REUSEADDR;
		break;
        case TARGET_SO_TYPE:
		optname = SO_TYPE;
		break;
        case TARGET_SO_ERROR:
		optname = SO_ERROR;
		break;
        case TARGET_SO_DONTROUTE:
		optname = SO_DONTROUTE;
		break;
        case TARGET_SO_BROADCAST:
		optname = SO_BROADCAST;
		break;
        case TARGET_SO_SNDBUF:
		optname = SO_SNDBUF;
		break;
        case TARGET_SO_RCVBUF:
		optname = SO_RCVBUF;
		break;
        case TARGET_SO_KEEPALIVE:
		optname = SO_KEEPALIVE;
		break;
        case TARGET_SO_OOBINLINE:
		optname = SO_OOBINLINE;
		break;
        case TARGET_SO_NO_CHECK:
		optname = SO_NO_CHECK;
		break;
        case TARGET_SO_PRIORITY:
		optname = SO_PRIORITY;
		break;
bellard's avatar
bellard committed
713
#ifdef SO_BSDCOMPAT
714
715
716
        case TARGET_SO_BSDCOMPAT:
		optname = SO_BSDCOMPAT;
		break;
bellard's avatar
bellard committed
717
#endif
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
        case TARGET_SO_PASSCRED:
		optname = SO_PASSCRED;
		break;
        case TARGET_SO_TIMESTAMP:
		optname = SO_TIMESTAMP;
		break;
        case TARGET_SO_RCVLOWAT:
		optname = SO_RCVLOWAT;
		break;
        case TARGET_SO_RCVTIMEO:
		optname = SO_RCVTIMEO;
		break;
        case TARGET_SO_SNDTIMEO:
		optname = SO_SNDTIMEO;
		break;
733
734
735
736
            break;
        default:
            goto unimplemented;
        }
737
738
739
740
741
	if (optlen < sizeof(uint32_t))
	return -EINVAL;

	val = tget32(optval);
	ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
742
        break;
bellard's avatar
bellard committed
743
    default:
744
745
746
    unimplemented:
        gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
        ret = -ENOSYS;
bellard's avatar
bellard committed
747
    }
748
    return ret;
bellard's avatar
bellard committed
749
750
751
}

static long do_getsockopt(int sockfd, int level, int optname, 
752
                          target_ulong optval, target_ulong optlen)
bellard's avatar
bellard committed
753
{
754
755
756
    int len, lv, val, ret;

    switch(level) {
757
758
    case TARGET_SOL_SOCKET:
    	level = SOL_SOCKET;
759
	switch (optname) {
760
761
762
763
764
	case TARGET_SO_LINGER:
	case TARGET_SO_RCVTIMEO:
	case TARGET_SO_SNDTIMEO:
	case TARGET_SO_PEERCRED:
	case TARGET_SO_PEERNAME:
765
766
767
	    /* These don't just return a single integer */
	    goto unimplemented;
        default:
bellard's avatar
bellard committed
768
769
770
771
772
773
            goto int_case;
        }
        break;
    case SOL_TCP:
        /* TCP options all take an 'int' value.  */
    int_case:
774
        len = tget32(optlen);
bellard's avatar
bellard committed
775
776
777
778
779
780
781
782
783
        if (len < 0)
            return -EINVAL;
        lv = sizeof(int);
        ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
        if (ret < 0)
            return ret;
        val = tswap32(val);
        if (len > lv)
            len = lv;
784
785
786
787
788
        if (len == 4)
            tput32(optval, val);
        else
            tput8(optval, val);
        tput32(optlen, len);
bellard's avatar
bellard committed
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
        break;
    case SOL_IP:
        switch(optname) {
        case IP_TOS:
        case IP_TTL:
        case IP_HDRINCL:
        case IP_ROUTER_ALERT:
        case IP_RECVOPTS:
        case IP_RETOPTS:
        case IP_PKTINFO:
        case IP_MTU_DISCOVER:
        case IP_RECVERR:
        case IP_RECVTOS:
#ifdef IP_FREEBIND
        case IP_FREEBIND:
#endif
        case IP_MULTICAST_TTL:
        case IP_MULTICAST_LOOP:
807
            len = tget32(optlen);
808
809
810
811
812
813
            if (len < 0)
                return -EINVAL;
            lv = sizeof(int);
            ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
            if (ret < 0)
                return ret;
bellard's avatar
bellard committed
814
815
            if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
                len = 1;
816
817
                tput32(optlen, len);
                tput8(optval, val);
bellard's avatar
bellard committed
818
819
820
            } else {
                if (len > sizeof(int))
                    len = sizeof(int);
821
822
                tput32(optlen, len);
                tput32(optval, val);
bellard's avatar
bellard committed
823
            }
824
            break;
bellard's avatar
bellard committed
825
826
        default:
            goto unimplemented;
827
828
829
830
831
832
833
834
835
836
        }
        break;
    default:
    unimplemented:
        gemu_log("getsockopt level=%d optname=%d not yet supported\n",
                 level, optname);
        ret = -ENOSYS;
        break;
    }
    return ret;
bellard's avatar
bellard committed
837
838
}

839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
static void lock_iovec(struct iovec *vec, target_ulong target_addr,
                       int count, int copy)
{
    struct target_iovec *target_vec;
    target_ulong base;
    int i;

    target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
    for(i = 0;i < count; i++) {
        base = tswapl(target_vec[i].iov_base);
        vec[i].iov_len = tswapl(target_vec[i].iov_len);
        vec[i].iov_base = lock_user(base, vec[i].iov_len, copy);
    }
    unlock_user (target_vec, target_addr, 0);
}

static void unlock_iovec(struct iovec *vec, target_ulong target_addr,
                         int count, int copy)
{
    struct target_iovec *target_vec;
    target_ulong base;
    int i;

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

870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
static long do_socket(int domain, int type, int protocol)
{
#if defined(TARGET_MIPS)
    switch(type) {
    case TARGET_SOCK_DGRAM:
        type = SOCK_DGRAM;
        break;
    case TARGET_SOCK_STREAM:
        type = SOCK_STREAM;
        break;
    case TARGET_SOCK_RAW:
        type = SOCK_RAW;
        break;
    case TARGET_SOCK_RDM:
        type = SOCK_RDM;
        break;
    case TARGET_SOCK_SEQPACKET:
        type = SOCK_SEQPACKET;
        break;
    case TARGET_SOCK_PACKET:
        type = SOCK_PACKET;
        break;
    }
#endif
    return get_errno(socket(domain, type, protocol));
}

static long do_bind(int sockfd, target_ulong target_addr,
                    socklen_t addrlen)
{
    void *addr = alloca(addrlen);
    
    target_to_host_sockaddr(addr, target_addr, addrlen);
    return get_errno(bind(sockfd, addr, addrlen));
}

static long do_connect(int sockfd, target_ulong target_addr,
                    socklen_t addrlen)
{
    void *addr = alloca(addrlen);
    
    target_to_host_sockaddr(addr, target_addr, addrlen);
    return get_errno(connect(sockfd, addr, addrlen));
}

static long do_sendrecvmsg(int fd, target_ulong target_msg,
                           int flags, int send)
{
    long ret;
    struct target_msghdr *msgp;
    struct msghdr msg;
    int count;
    struct iovec *vec;
    target_ulong target_vec;

    lock_user_struct(msgp, target_msg, 1);
    if (msgp->msg_name) {
        msg.msg_namelen = tswap32(msgp->msg_namelen);
        msg.msg_name = alloca(msg.msg_namelen);
        target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
                                msg.msg_namelen);
    } else {
        msg.msg_name = NULL;
        msg.msg_namelen = 0;
    }
    msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
    msg.msg_control = alloca(msg.msg_controllen);
    msg.msg_flags = tswap32(msgp->msg_flags);
    
    count = tswapl(msgp->msg_iovlen);
    vec = alloca(count * sizeof(struct iovec));
    target_vec = tswapl(msgp->msg_iov);
    lock_iovec(vec, target_vec, count, send);
    msg.msg_iovlen = count;
    msg.msg_iov = vec;
    
    if (send) {
        target_to_host_cmsg(&msg, msgp);
        ret = get_errno(sendmsg(fd, &msg, flags));
    } else {
        ret = get_errno(recvmsg(fd, &msg, flags));
        if (!is_error(ret))
            host_to_target_cmsg(msgp, &msg);
    }
    unlock_iovec(vec, target_vec, count, !send);
    return ret;
}

pbrook's avatar
pbrook committed
958
959
960
961
static long do_accept(int fd, target_ulong target_addr,
                      target_ulong target_addrlen)
{
    socklen_t addrlen = tget32(target_addrlen);
ths's avatar
ths committed
962
    void *addr = alloca(addrlen);
pbrook's avatar
pbrook committed
963
964
965
966
967
968
969
970
971
972
973
974
975
976
    long ret;

    ret = get_errno(accept(fd, addr, &addrlen));
    if (!is_error(ret)) {
        host_to_target_sockaddr(target_addr, addr, addrlen);
        tput32(target_addrlen, addrlen);
    }
    return ret;
}

static long do_getpeername(int fd, target_ulong target_addr,
                           target_ulong target_addrlen)
{
    socklen_t addrlen = tget32(target_addrlen);
977
    void *addr = alloca(addrlen);
pbrook's avatar
pbrook committed
978
979
980
981
982
983
984
985
986
987
988
989
990
991
    long ret;

    ret = get_errno(getpeername(fd, addr, &addrlen));
    if (!is_error(ret)) {
        host_to_target_sockaddr(target_addr, addr, addrlen);
        tput32(target_addrlen, addrlen);
    }
    return ret;
}

static long do_getsockname(int fd, target_ulong target_addr,
                           target_ulong target_addrlen)
{
    socklen_t addrlen = tget32(target_addrlen);
992
    void *addr = alloca(addrlen);
pbrook's avatar
pbrook committed
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
    long ret;

    ret = get_errno(getsockname(fd, addr, &addrlen));
    if (!is_error(ret)) {
        host_to_target_sockaddr(target_addr, addr, addrlen);
        tput32(target_addrlen, addrlen);
    }
    return ret;
}

static long do_socketpair(int domain, int type, int protocol,
                          target_ulong target_tab)
{
    int tab[2];
    long ret;

    ret = get_errno(socketpair(domain, type, protocol, tab));
    if (!is_error(ret)) {
        tput32(target_tab, tab[0]);
        tput32(target_tab + 4, tab[1]);
    }
    return ret;
}

static long do_sendto(int fd, target_ulong msg, size_t len, int flags,
                      target_ulong target_addr, socklen_t addrlen)
{
    void *addr;
    void *host_msg;
    long ret;

    host_msg = lock_user(msg, len, 1);
    if (target_addr) {
        addr = alloca(addrlen);
        target_to_host_sockaddr(addr, target_addr, addrlen);
        ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
    } else {
        ret = get_errno(send(fd, host_msg, len, flags));
    }
    unlock_user(host_msg, msg, 0);
    return ret;
}

static long do_recvfrom(int fd, target_ulong msg, size_t len, int flags,
                        target_ulong target_addr, target_ulong target_addrlen)
{
    socklen_t addrlen;
    void *addr;
    void *host_msg;
    long ret;

    host_msg = lock_user(msg, len, 0);
    if (target_addr) {
        addrlen = tget32(target_addrlen);
        addr = alloca(addrlen);
        ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
    } else {
        addr = NULL; /* To keep compiler quiet.  */
        ret = get_errno(recv(fd, host_msg, len, flags));
    }
    if (!is_error(ret)) {
        if (target_addr) {
            host_to_target_sockaddr(target_addr, addr, addrlen);
            tput32(target_addrlen, addrlen);
        }
        unlock_user(host_msg, msg, len);
    } else {
        unlock_user(host_msg, msg, 0);
    }
    return ret;
}

1065
static long do_socketcall(int num, target_ulong vptr)
1066
1067
{
    long ret;
1068
    const int n = sizeof(target_ulong);
1069
1070
1071

    switch(num) {
    case SOCKOP_socket:
bellard's avatar
bellard committed
1072
	{
1073
1074
1075
            int domain = tgetl(vptr);
            int type = tgetl(vptr + n);
            int protocol = tgetl(vptr + 2 * n);
1076
            ret = do_socket(domain, type, protocol);
bellard's avatar
bellard committed
1077
	}
1078
1079
        break;
    case SOCKOP_bind:
bellard's avatar
bellard committed
1080
	{
1081
1082
1083
            int sockfd = tgetl(vptr);
            target_ulong target_addr = tgetl(vptr + n);
            socklen_t addrlen = tgetl(vptr + 2 * n);
1084
            ret = do_bind(sockfd, target_addr, addrlen);
bellard's avatar
bellard committed
1085
        }
1086
1087
        break;
    case SOCKOP_connect:
bellard's avatar
bellard committed
1088
        {
1089
1090
1091
            int sockfd = tgetl(vptr);
            target_ulong target_addr = tgetl(vptr + n);
            socklen_t addrlen = tgetl(vptr + 2 * n);
1092
            ret = do_connect(sockfd, target_addr, addrlen);
bellard's avatar
bellard committed
1093
        }
1094
1095
        break;
    case SOCKOP_listen:
bellard's avatar
bellard committed
1096
        {
1097
1098
            int sockfd = tgetl(vptr);
            int backlog = tgetl(vptr + n);
bellard's avatar
bellard committed
1099
1100
            ret = get_errno(listen(sockfd, backlog));
        }
1101
1102
1103
        break;
    case SOCKOP_accept:
        {
1104
1105
1106
            int sockfd = tgetl(vptr);
            target_ulong target_addr = tgetl(vptr + n);
            target_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook's avatar
pbrook committed
1107
            ret = do_accept(sockfd, target_addr, target_addrlen);
1108
1109
1110
1111
        }
        break;
    case SOCKOP_getsockname:
        {
1112
1113
1114
            int sockfd = tgetl(vptr);
            target_ulong target_addr = tgetl(vptr + n);
            target_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook's avatar
pbrook committed
1115
            ret = do_getsockname(sockfd, target_addr, target_addrlen);
1116
1117
1118
1119
        }
        break;
    case SOCKOP_getpeername:
        {
1120
1121
1122
            int sockfd = tgetl(vptr);
            target_ulong target_addr = tgetl(vptr + n);
            target_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook's avatar
pbrook committed
1123
            ret = do_getpeername(sockfd, target_addr, target_addrlen);
1124
1125
1126
1127
        }
        break;
    case SOCKOP_socketpair:
        {
1128
1129
1130
            int domain = tgetl(vptr);
            int type = tgetl(vptr + n);
            int protocol = tgetl(vptr + 2 * n);
pbrook's avatar
pbrook committed
1131
1132
            target_ulong tab = tgetl(vptr + 3 * n);
            ret = do_socketpair(domain, type, protocol, tab);
1133
1134
1135
        }
        break;
    case SOCKOP_send:
bellard's avatar
bellard committed
1136
        {
1137
1138
1139
1140
            int sockfd = tgetl(vptr);
            target_ulong msg = tgetl(vptr + n);
            size_t len = tgetl(vptr + 2 * n);
            int flags = tgetl(vptr + 3 * n);
pbrook's avatar
pbrook committed
1141
            ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard's avatar
bellard committed
1142
        }
1143
1144
        break;
    case SOCKOP_recv:
bellard's avatar
bellard committed
1145
        {
1146
1147
1148
1149
            int sockfd = tgetl(vptr);
            target_ulong msg = tgetl(vptr + n);
            size_t len = tgetl(vptr + 2 * n);
            int flags = tgetl(vptr + 3 * n);
pbrook's avatar
pbrook committed
1150
            ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard's avatar
bellard committed
1151
        }
1152
1153
        break;
    case SOCKOP_sendto:
bellard's avatar
bellard committed
1154
        {
1155
1156
1157
1158
            int sockfd = tgetl(vptr);
            target_ulong msg = tgetl(vptr + n);
            size_t len = tgetl(vptr + 2 * n);
            int flags = tgetl(vptr + 3 * n);
pbrook's avatar
pbrook committed
1159
            target_ulong addr = tgetl(vptr + 4 * n);
1160
            socklen_t addrlen = tgetl(vptr + 5 * n);
pbrook's avatar
pbrook committed
1161
            ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard's avatar
bellard committed
1162
        }
1163
1164
1165
        break;
    case SOCKOP_recvfrom:
        {
1166
1167
1168
1169
            int sockfd = tgetl(vptr);
            target_ulong msg = tgetl(vptr + n);
            size_t len = tgetl(vptr + 2 * n);
            int flags = tgetl(vptr + 3 * n);
pbrook's avatar
pbrook committed
1170
1171
1172
            target_ulong addr = tgetl(vptr + 4 * n);
            target_ulong addrlen = tgetl(vptr + 5 * n);
            ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1173
1174
1175
        }
        break;
    case SOCKOP_shutdown:
bellard's avatar
bellard committed
1176
        {
1177
1178
            int sockfd = tgetl(vptr);
            int how = tgetl(vptr + n);
bellard's avatar
bellard committed
1179
1180
1181

            ret = get_errno(shutdown(sockfd, how));
        }
1182
1183
1184
        break;
    case SOCKOP_sendmsg:
    case SOCKOP_recvmsg:
bellard's avatar
bellard committed
1185
1186
        {
            int fd;
1187
            target_ulong target_msg;
1188
            int flags;
bellard's avatar
bellard committed
1189

1190
            fd = tgetl(vptr);
1191
            target_msg = tgetl(vptr + n);
1192
            flags = tgetl(vptr + 2 * n);
1193
1194
1195

            ret = do_sendrecvmsg(fd, target_msg, flags, 
                                 (num == SOCKOP_sendmsg));
bellard's avatar
bellard committed
1196
1197
        }
        break;
1198
    case SOCKOP_setsockopt:
bellard's avatar
bellard committed
1199
        {
1200
1201
1202
1203
1204
            int sockfd = tgetl(vptr);
            int level = tgetl(vptr + n);
            int optname = tgetl(vptr + 2 * n);
            target_ulong optval = tgetl(vptr + 3 * n);
            socklen_t optlen = tgetl(vptr + 4 * n);
bellard's avatar
bellard committed
1205
1206
1207
1208

            ret = do_setsockopt(sockfd, level, optname, optval, optlen);
        }
        break;
1209
    case SOCKOP_getsockopt:
bellard's avatar
bellard committed
1210
        {
1211
1212
1213
1214
1215
            int sockfd = tgetl(vptr);
            int level = tgetl(vptr + n);
            int optname = tgetl(vptr + 2 * n);
            target_ulong optval = tgetl(vptr + 3 * n);
            target_ulong poptlen = tgetl(vptr + 4 * n);
bellard's avatar
bellard committed
1216

1217
            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard's avatar
bellard committed
1218
1219
        }
        break;
1220
1221
1222
1223
1224
1225
1226
1227
    default:
        gemu_log("Unsupported socketcall: %d\n", num);
        ret = -ENOSYS;
        break;
    }
    return ret;
}

1228
1229
1230
1231
1232
1233
1234
#define N_SHM_REGIONS	32

static struct shm_region {
    uint32_t	start;
    uint32_t	size;
} shm_regions[N_SHM_REGIONS];

1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
struct target_ipc_perm
{
    target_long __key;
    target_ulong uid;
    target_ulong gid;
    target_ulong cuid;
    target_ulong cgid;
    unsigned short int mode;
    unsigned short int __pad1;
    unsigned short int __seq;
    unsigned short int __pad2;
    target_ulong __unused1;
    target_ulong __unused2;
};

struct target_semid_ds
{
  struct target_ipc_perm sem_perm;
  target_ulong sem_otime;
  target_ulong __unused1;
  target_ulong sem_ctime;
  target_ulong __unused2;
  target_ulong sem_nsems;
  target_ulong __unused3;
  target_ulong __unused4;
};

static inline void target_to_host_ipc_perm(struct ipc_perm *host_ip,
                                           target_ulong target_addr)
{
    struct target_ipc_perm *target_ip;
    struct target_semid_ds *target_sd;

    lock_user_struct(target_sd, target_addr, 1);
    target_ip=&(target_sd->sem_perm);
    host_ip->__key = tswapl(target_ip->__key);
    host_ip->uid = tswapl(target_ip->uid);
    host_ip->gid = tswapl(target_ip->gid);
    host_ip->cuid = tswapl(target_ip->cuid);
    host_ip->cgid = tswapl(target_ip->cgid);
    host_ip->mode = tswapl(target_ip->mode);
    unlock_user_struct(target_sd, target_addr, 0);
}

static inline void host_to_target_ipc_perm(target_ulong target_addr,
                                           struct ipc_perm *host_ip)
{
    struct target_ipc_perm *target_ip;
    struct target_semid_ds *target_sd;

    lock_user_struct(target_sd, target_addr, 0);
    target_ip = &(target_sd->sem_perm);
    target_ip->__key = tswapl(host_ip->__key);
    target_ip->uid = tswapl(host_ip->uid);
    target_ip->gid = tswapl(host_ip->gid);
    target_ip->cuid = tswapl(host_ip->cuid);
    target_ip->cgid = tswapl(host_ip->cgid);
    target_ip->mode = tswapl(host_ip->mode);
    unlock_user_struct(target_sd, target_addr, 1);
}

static inline void target_to_host_semid_ds(struct semid_ds *host_sd,
                                          target_ulong target_addr)
{
    struct target_semid_ds *target_sd;

    lock_user_struct(target_sd, target_addr, 1);
    target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
    host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
    host_sd->sem_otime = tswapl(target_sd->sem_otime);
    host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
    unlock_user_struct(target_sd, target_addr, 0);
}

static inline void host_to_target_semid_ds(target_ulong target_addr,
                                           struct semid_ds *host_sd)
{
    struct target_semid_ds *target_sd;

    lock_user_struct(target_sd, target_addr, 0);
    host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
    target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
    target_sd->sem_otime = tswapl(host_sd->sem_otime);
    target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
    unlock_user_struct(target_sd, target_addr, 1);
}

1322
1323
union semun {
	int val;
1324
	struct semid_ds *buf;
1325
1326
1327
	unsigned short *array;
};

1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
union target_semun {
	int val;
	target_long buf;
	unsigned short int *array;
};

static inline void target_to_host_semun(unsigned long cmd,
                                        union semun *host_su,
                                        target_ulong target_addr,
                                        struct semid_ds *ds)
{
    union target_semun *target_su;

    switch( cmd ) {
	case IPC_STAT:
	case IPC_SET:
           lock_user_struct(target_su, target_addr, 1);
	   target_to_host_semid_ds(ds,target_su->buf);
	   host_su->buf = ds;
           unlock_user_struct(target_su, target_addr, 0);
	   break;
	case GETVAL:
	case SETVAL:
           lock_user_struct(target_su, target_addr, 1);
	   host_su->val = tswapl(target_su->val);
           unlock_user_struct(target_su, target_addr, 0);
	   break;
	case GETALL:
	case SETALL:
           lock_user_struct(target_su, target_addr, 1);
	   *host_su->array = tswap16(*target_su->array);
           unlock_user_struct(target_su, target_addr, 0);
	   break;
	default:
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
    }
}

static inline void host_to_target_semun(unsigned long cmd,
                                        target_ulong target_addr,
                                        union semun *host_su,
                                        struct semid_ds *ds)
{
    union target_semun *target_su;

    switch( cmd ) {
	case IPC_STAT:
	case IPC_SET:
           lock_user_struct(target_su, target_addr, 0);
	   host_to_target_semid_ds(target_su->buf,ds);
           unlock_user_struct(target_su, target_addr, 1);
	   break;
	case GETVAL:
	case SETVAL:
           lock_user_struct(target_su, target_addr, 0);
	   target_su->val = tswapl(host_su->val);
           unlock_user_struct(target_su, target_addr, 1);
	   break;
	case GETALL:
	case SETALL:
           lock_user_struct(target_su, target_addr, 0);
	   *target_su->array = tswap16(*host_su->array);
           unlock_user_struct(target_su, target_addr, 1);
	   break;
        default:
           gemu_log("semun operation not fully supported: %d\n", (int)cmd);
    }
}

static inline long do_semctl(long first, long second, long third, long ptr)
{
    union semun arg;
    struct semid_ds dsarg;
    int cmd = third&0xff;
    long ret = 0;

    switch( cmd ) {
	case GETVAL:
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
            ret = get_errno(semctl(first, second, cmd, arg));
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
            break;
	case SETVAL:
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
            ret = get_errno(semctl(first, second, cmd, arg));
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
            break;
	case GETALL:
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
            ret = get_errno(semctl(first, second, cmd, arg));
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
            break;
	case SETALL:
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
            ret = get_errno(semctl(first, second, cmd, arg));
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
            break;
	case IPC_STAT:
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
            ret = get_errno(semctl(first, second, cmd, arg));
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
            break;
	case IPC_SET:
            target_to_host_semun(cmd,&arg,ptr,&dsarg);
            ret = get_errno(semctl(first, second, cmd, arg));
            host_to_target_semun(cmd,ptr,&arg,&dsarg);
            break;
    default:
            ret = get_errno(semctl(first, second, cmd, arg));
    }

    return ret;
}

ths's avatar
ths committed
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
struct target_msqid_ds
{
  struct target_ipc_perm msg_perm;
  target_ulong msg_stime;
  target_ulong __unused1;
  target_ulong msg_rtime;
  target_ulong __unused2;
  target_ulong msg_ctime;
  target_ulong __unused3;
  target_ulong