syscall.c 95.8 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
31
32
33
34
35
36
37
38
39
40
41
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/mount.h>
#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
42
#include <sys/poll.h>
bellard's avatar
bellard committed
43
#include <sys/times.h>
44
#include <sys/shm.h>
45
#include <utime.h>
bellard's avatar
bellard committed
46
#include <sys/sysinfo.h>
bellard's avatar
bellard committed
47
//#include <sys/user.h>
48
#include <netinet/ip.h>
bellard's avatar
bellard committed
49
#include <netinet/tcp.h>
50
51
52
53

#define termios host_termios
#define winsize host_winsize
#define termio host_termio
bellard's avatar
bellard committed
54
55
56
#define sgttyb host_sgttyb /* same as target */
#define tchars host_tchars /* same as target */
#define ltchars host_ltchars /* same as target */
57
58
59
60
61
62
63

#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
64
#include <linux/dirent.h>
65
#include <linux/kd.h>
66

bellard's avatar
bellard committed
67
#include "qemu.h"
68

bellard's avatar
bellard committed
69
//#define DEBUG
70

71
72
73
74
75
#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC)
/* 16 bit uid wrappers emulation */
#define USE_UID16
#endif

bellard's avatar
bellard committed
76
77
78
79
//#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])

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

#if defined(__powerpc__)
#undef __syscall_nr
#undef __sc_loadargs_0
#undef __sc_loadargs_1
#undef __sc_loadargs_2
#undef __sc_loadargs_3
#undef __sc_loadargs_4
#undef __sc_loadargs_5
#undef __sc_asm_input_0
#undef __sc_asm_input_1
#undef __sc_asm_input_2
#undef __sc_asm_input_3
#undef __sc_asm_input_4
#undef __sc_asm_input_5
#undef _syscall0
#undef _syscall1
#undef _syscall2
#undef _syscall3
#undef _syscall4
#undef _syscall5

/* need to redefine syscalls as Linux kernel defines are incorrect for
   the clobber list */
/* On powerpc a system call basically clobbers the same registers like a
 * function call, with the exception of LR (which is needed for the
 * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
 * an error return status).
 */

#define __syscall_nr(nr, type, name, args...)				\
	unsigned long __sc_ret, __sc_err;				\
	{								\
		register unsigned long __sc_0  __asm__ ("r0");		\
		register unsigned long __sc_3  __asm__ ("r3");		\
		register unsigned long __sc_4  __asm__ ("r4");		\
		register unsigned long __sc_5  __asm__ ("r5");		\
		register unsigned long __sc_6  __asm__ ("r6");		\
		register unsigned long __sc_7  __asm__ ("r7");		\
									\
		__sc_loadargs_##nr(name, args);				\
		__asm__ __volatile__					\
			("sc           \n\t"				\
			 "mfcr %0      "				\
			: "=&r" (__sc_0),				\
			  "=&r" (__sc_3),  "=&r" (__sc_4),		\
			  "=&r" (__sc_5),  "=&r" (__sc_6),		\
			  "=&r" (__sc_7)				\
			: __sc_asm_input_##nr				\
			: "cr0", "ctr", "memory",			\
			  "r8", "r9", "r10","r11", "r12");		\
		__sc_ret = __sc_3;					\
		__sc_err = __sc_0;					\
	}								\
	if (__sc_err & 0x10000000)					\
	{								\
		errno = __sc_ret;					\
		__sc_ret = -1;						\
	}								\
	return (type) __sc_ret

#define __sc_loadargs_0(name, dummy...)					\
	__sc_0 = __NR_##name
#define __sc_loadargs_1(name, arg1)					\
	__sc_loadargs_0(name);						\
	__sc_3 = (unsigned long) (arg1)
#define __sc_loadargs_2(name, arg1, arg2)				\
	__sc_loadargs_1(name, arg1);					\
	__sc_4 = (unsigned long) (arg2)
#define __sc_loadargs_3(name, arg1, arg2, arg3)				\
	__sc_loadargs_2(name, arg1, arg2);				\
	__sc_5 = (unsigned long) (arg3)
#define __sc_loadargs_4(name, arg1, arg2, arg3, arg4)			\
	__sc_loadargs_3(name, arg1, arg2, arg3);			\
	__sc_6 = (unsigned long) (arg4)
#define __sc_loadargs_5(name, arg1, arg2, arg3, arg4, arg5)		\
	__sc_loadargs_4(name, arg1, arg2, arg3, arg4);			\
	__sc_7 = (unsigned long) (arg5)

#define __sc_asm_input_0 "0" (__sc_0)
#define __sc_asm_input_1 __sc_asm_input_0, "1" (__sc_3)
#define __sc_asm_input_2 __sc_asm_input_1, "2" (__sc_4)
#define __sc_asm_input_3 __sc_asm_input_2, "3" (__sc_5)
#define __sc_asm_input_4 __sc_asm_input_3, "4" (__sc_6)
#define __sc_asm_input_5 __sc_asm_input_4, "5" (__sc_7)

#define _syscall0(type,name)						\
type name(void)								\
{									\
	__syscall_nr(0, type, name);					\
}

#define _syscall1(type,name,type1,arg1)					\
type name(type1 arg1)							\
{									\
	__syscall_nr(1, type, name, arg1);				\
}

#define _syscall2(type,name,type1,arg1,type2,arg2)			\
type name(type1 arg1, type2 arg2)					\
{									\
	__syscall_nr(2, type, name, arg1, arg2);			\
}

#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)		\
type name(type1 arg1, type2 arg2, type3 arg3)				\
{									\
	__syscall_nr(3, type, name, arg1, arg2, arg3);			\
}

#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4)		\
{									\
	__syscall_nr(4, type, name, arg1, arg2, arg3, arg4);		\
}

#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)	\
{									\
	__syscall_nr(5, type, name, arg1, arg2, arg3, arg4, arg5);	\
}
#endif

203
#define __NR_sys_uname __NR_uname
bellard's avatar
bellard committed
204
#define __NR_sys_getcwd1 __NR_getcwd
205
206
#define __NR_sys_statfs __NR_statfs
#define __NR_sys_fstatfs __NR_fstatfs
bellard's avatar
bellard committed
207
#define __NR_sys_getdents __NR_getdents
bellard's avatar
bellard committed
208
#define __NR_sys_getdents64 __NR_getdents64
bellard's avatar
bellard committed
209
#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
210

211
#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
bellard's avatar
bellard committed
212
213
214
#define __NR__llseek __NR_lseek
#endif

bellard's avatar
bellard committed
215
#ifdef __NR_gettid
216
_syscall0(int, gettid)
bellard's avatar
bellard committed
217
218
219
220
221
#else
static int gettid(void) {
    return -ENOSYS;
}
#endif
222
_syscall1(int,sys_uname,struct new_utsname *,buf)
bellard's avatar
bellard committed
223
224
_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
225
_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
226
227
_syscall5(int, _llseek,  uint,  fd, ulong, hi, ulong, lo,
          loff_t *, res, uint, wh);
bellard's avatar
bellard committed
228
229
_syscall2(int,sys_statfs,const char *,path,struct kernel_statfs *,buf)
_syscall2(int,sys_fstatfs,int,fd,struct kernel_statfs *,buf)
bellard's avatar
bellard committed
230
_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
231
232
233
#ifdef __NR_exit_group
_syscall1(int,exit_group,int,error_code)
#endif
bellard's avatar
bellard committed
234
235

extern int personality(int);
bellard's avatar
bellard committed
236
237
238
extern int flock(int, int);
extern int setfsuid(int);
extern int setfsgid(int);
bellard's avatar
bellard committed
239
240
241
242
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 *);
243
extern int setgroups(int, gid_t *);
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

static inline long get_errno(long ret)
{
    if (ret == -1)
        return -errno;
    else
        return ret;
}

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

static char *target_brk;
static char *target_original_brk;

void target_set_brk(char *new_brk)
{
    target_brk = new_brk;
    target_original_brk = new_brk;
}

static long do_brk(char *new_brk)
{
    char *brk_page;
    long mapped_addr;
    int	new_alloc_size;

    if (!new_brk)
        return (long)target_brk;
    if (new_brk < target_original_brk)
        return -ENOMEM;
    
bellard's avatar
bellard committed
278
    brk_page = (char *)HOST_PAGE_ALIGN((unsigned long)target_brk);
279
280
281
282
283
284
285
286

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

    /* We need to allocate more memory after the brk... */
bellard's avatar
bellard committed
287
288
289
290
    new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
    mapped_addr = get_errno(target_mmap((unsigned long)brk_page, new_alloc_size, 
                                        PROT_READ|PROT_WRITE,
                                        MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
291
292
293
294
295
296
297
298
299
300
301
    if (is_error(mapped_addr)) {
	return mapped_addr;
    } else {
	target_brk = new_brk;
    	return (long)target_brk;
    }
}

static inline fd_set *target_to_host_fds(fd_set *fds, 
                                         target_long *target_fds, int n)
{
bellard's avatar
bellard committed
302
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    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
324
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
325
326
327
328
329
330
    /* nothing to do */
#else
    int i, nw, j, k;
    target_long v;

    if (target_fds) {
331
        nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
332
333
334
335
336
337
338
339
340
341
342
343
344
        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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#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
}

bellard's avatar
bellard committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
static inline void host_to_target_rusage(struct target_rusage *target_rusage, 
                                         const struct rusage *rusage)
{
    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);
}

bellard's avatar
bellard committed
383
static inline void target_to_host_timeval(struct timeval *tv, 
bellard's avatar
bellard committed
384
                                          const struct target_timeval *target_tv)
385
{
bellard's avatar
bellard committed
386
387
    tv->tv_sec = tswapl(target_tv->tv_sec);
    tv->tv_usec = tswapl(target_tv->tv_usec);
388
389
}

bellard's avatar
bellard committed
390
static inline void host_to_target_timeval(struct target_timeval *target_tv, 
bellard's avatar
bellard committed
391
                                          const struct timeval *tv)
392
{
bellard's avatar
bellard committed
393
394
    target_tv->tv_sec = tswapl(tv->tv_sec);
    target_tv->tv_usec = tswapl(tv->tv_usec);
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
}


static long do_select(long n, 
                      target_long *target_rfds, target_long *target_wfds, 
                      target_long *target_efds, struct target_timeval *target_tv)
{
    fd_set rfds, wfds, efds;
    fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
    struct timeval tv, *tv_ptr;
    long ret;

    rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
    wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
    efds_ptr = target_to_host_fds(&efds, target_efds, n);
            
    if (target_tv) {
bellard's avatar
bellard committed
412
        target_to_host_timeval(&tv, target_tv);
413
414
415
416
417
418
419
420
421
422
423
        tv_ptr = &tv;
    } else {
        tv_ptr = NULL;
    }
    ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
    if (!is_error(ret)) {
        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
424
            host_to_target_timeval(target_tv, &tv);
425
426
427
428
429
        }
    }
    return ret;
}

bellard's avatar
bellard committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
static inline void target_to_host_sockaddr(struct sockaddr *addr,
                                           struct target_sockaddr *target_addr,
                                           socklen_t len)
{
    memcpy(addr, target_addr, len);
    addr->sa_family = tswap16(target_addr->sa_family);
}

static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
                                           struct sockaddr *addr,
                                           socklen_t len)
{
    memcpy(target_addr, addr, len);
    target_addr->sa_family = tswap16(addr->sa_family);
}

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);
            gemu_log("Host cmsg overflow");
            break;
        }

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

        if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
            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;
}

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);
            gemu_log("Target cmsg overflow");
            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));

        if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
            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, 
                          void *optval, socklen_t optlen)
{
536
537
538
539
    int val, ret;
            
    switch(level) {
    case SOL_TCP:
bellard's avatar
bellard committed
540
541
542
        /* TCP options all take an 'int' value.  */
        if (optlen < sizeof(uint32_t))
            return -EINVAL;
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
        
        if (get_user(val, (uint32_t *)optval))
            return -EFAULT;
        ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
        break;
    case SOL_IP:
        switch(optname) {
        case IP_HDRINCL:
            val = 0;
            if (optlen >= sizeof(uint32_t)) {
                if (get_user(val, (uint32_t *)optval))
                    return -EFAULT;
            } else if (optlen >= 1) {
                if (get_user(val, (uint8_t *)optval))
                    return -EFAULT;
            }
            ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
            break;
        default:
            goto unimplemented;
        }
        break;
    case SOL_SOCKET:
        switch (optname) {
            /* Options with 'int' argument.  */
        case SO_DEBUG:
        case SO_REUSEADDR:
        case SO_TYPE:
        case SO_ERROR:
        case SO_DONTROUTE:
        case SO_BROADCAST:
        case SO_SNDBUF:
        case SO_RCVBUF:
        case SO_KEEPALIVE:
        case SO_OOBINLINE:
        case SO_NO_CHECK:
        case SO_PRIORITY:
        case SO_BSDCOMPAT:
        case SO_PASSCRED:
        case SO_TIMESTAMP:
        case SO_RCVLOWAT:
        case SO_RCVTIMEO:
        case SO_SNDTIMEO:
            if (optlen < sizeof(uint32_t))
                return -EINVAL;
            if (get_user(val, (uint32_t *)optval))
                return -EFAULT;
            ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
            break;
        default:
            goto unimplemented;
        }
        break;
bellard's avatar
bellard committed
596
    default:
597
598
599
    unimplemented:
        gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
        ret = -ENOSYS;
bellard's avatar
bellard committed
600
    }
601
    return ret;
bellard's avatar
bellard committed
602
603
604
605
606
}

static long do_getsockopt(int sockfd, int level, int optname, 
                          void *optval, socklen_t *optlen)
{
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
    int len, lv, val, ret;

    switch(level) {
    case SOL_SOCKET:
	switch (optname) {
	case SO_LINGER:
	case SO_RCVTIMEO:
	case SO_SNDTIMEO:
	case SO_PEERCRED:
	case SO_PEERNAME:
	    /* These don't just return a single integer */
	    goto unimplemented;
        default:
            if (get_user(len, optlen))
                return -EFAULT;
            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;
            if (copy_to_user(optval, &val, len))
                return -EFAULT;
            if (put_user(len, optlen))
                return -EFAULT;
            break;
        }
        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
646
647
648
}

static long do_socketcall(int num, int32_t *vptr)
649
650
651
652
653
{
    long ret;

    switch(num) {
    case SOCKOP_socket:
bellard's avatar
bellard committed
654
655
656
657
658
659
660
	{
            int domain = tswap32(vptr[0]);
            int type = tswap32(vptr[1]);
            int protocol = tswap32(vptr[2]);

            ret = get_errno(socket(domain, type, protocol));
	}
661
662
        break;
    case SOCKOP_bind:
bellard's avatar
bellard committed
663
664
665
666
667
668
669
670
671
	{
            int sockfd = tswap32(vptr[0]);
            void *target_addr = (void *)tswap32(vptr[1]);
            socklen_t addrlen = tswap32(vptr[2]);
            void *addr = alloca(addrlen);

            target_to_host_sockaddr(addr, target_addr, addrlen);
            ret = get_errno(bind(sockfd, addr, addrlen));
        }
672
673
        break;
    case SOCKOP_connect:
bellard's avatar
bellard committed
674
675
676
677
678
679
680
681
682
        {
            int sockfd = tswap32(vptr[0]);
            void *target_addr = (void *)tswap32(vptr[1]);
            socklen_t addrlen = tswap32(vptr[2]);
            void *addr = alloca(addrlen);

            target_to_host_sockaddr(addr, target_addr, addrlen);
            ret = get_errno(connect(sockfd, addr, addrlen));
        }
683
684
        break;
    case SOCKOP_listen:
bellard's avatar
bellard committed
685
686
687
688
689
690
        {
            int sockfd = tswap32(vptr[0]);
            int backlog = tswap32(vptr[1]);

            ret = get_errno(listen(sockfd, backlog));
        }
691
692
693
        break;
    case SOCKOP_accept:
        {
bellard's avatar
bellard committed
694
695
696
697
698
699
700
701
702
703
704
            int sockfd = tswap32(vptr[0]);
            void *target_addr = (void *)tswap32(vptr[1]);
            uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
            socklen_t addrlen = tswap32(*target_addrlen);
            void *addr = alloca(addrlen);

            ret = get_errno(accept(sockfd, addr, &addrlen));
            if (!is_error(ret)) {
                host_to_target_sockaddr(target_addr, addr, addrlen);
                *target_addrlen = tswap32(addrlen);
            }
705
706
707
708
        }
        break;
    case SOCKOP_getsockname:
        {
bellard's avatar
bellard committed
709
710
711
712
713
714
715
716
717
718
719
            int sockfd = tswap32(vptr[0]);
            void *target_addr = (void *)tswap32(vptr[1]);
            uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
            socklen_t addrlen = tswap32(*target_addrlen);
            void *addr = alloca(addrlen);

            ret = get_errno(getsockname(sockfd, addr, &addrlen));
            if (!is_error(ret)) {
                host_to_target_sockaddr(target_addr, addr, addrlen);
                *target_addrlen = tswap32(addrlen);
            }
720
721
722
723
        }
        break;
    case SOCKOP_getpeername:
        {
bellard's avatar
bellard committed
724
725
726
727
728
729
730
731
732
733
734
            int sockfd = tswap32(vptr[0]);
            void *target_addr = (void *)tswap32(vptr[1]);
            uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
            socklen_t addrlen = tswap32(*target_addrlen);
            void *addr = alloca(addrlen);

            ret = get_errno(getpeername(sockfd, addr, &addrlen));
            if (!is_error(ret)) {
                host_to_target_sockaddr(target_addr, addr, addrlen);
                *target_addrlen = tswap32(addrlen);
            }
735
736
737
738
        }
        break;
    case SOCKOP_socketpair:
        {
bellard's avatar
bellard committed
739
740
741
742
            int domain = tswap32(vptr[0]);
            int type = tswap32(vptr[1]);
            int protocol = tswap32(vptr[2]);
            int32_t *target_tab = (void *)tswap32(vptr[3]);
743
            int tab[2];
bellard's avatar
bellard committed
744
745

            ret = get_errno(socketpair(domain, type, protocol, tab));
746
747
748
749
750
751
752
            if (!is_error(ret)) {
                target_tab[0] = tswap32(tab[0]);
                target_tab[1] = tswap32(tab[1]);
            }
        }
        break;
    case SOCKOP_send:
bellard's avatar
bellard committed
753
754
755
756
757
758
759
760
        {
            int sockfd = tswap32(vptr[0]);
            void *msg = (void *)tswap32(vptr[1]);
            size_t len = tswap32(vptr[2]);
            int flags = tswap32(vptr[3]);

            ret = get_errno(send(sockfd, msg, len, flags));
        }
761
762
        break;
    case SOCKOP_recv:
bellard's avatar
bellard committed
763
764
765
766
767
768
769
770
        {
            int sockfd = tswap32(vptr[0]);
            void *msg = (void *)tswap32(vptr[1]);
            size_t len = tswap32(vptr[2]);
            int flags = tswap32(vptr[3]);

            ret = get_errno(recv(sockfd, msg, len, flags));
        }
771
772
        break;
    case SOCKOP_sendto:
bellard's avatar
bellard committed
773
774
775
776
777
778
779
780
781
782
783
784
        {
            int sockfd = tswap32(vptr[0]);
            void *msg = (void *)tswap32(vptr[1]);
            size_t len = tswap32(vptr[2]);
            int flags = tswap32(vptr[3]);
            void *target_addr = (void *)tswap32(vptr[4]);
            socklen_t addrlen = tswap32(vptr[5]);
            void *addr = alloca(addrlen);

            target_to_host_sockaddr(addr, target_addr, addrlen);
            ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
        }
785
786
787
        break;
    case SOCKOP_recvfrom:
        {
bellard's avatar
bellard committed
788
789
790
791
792
793
794
795
796
797
798
799
800
801
            int sockfd = tswap32(vptr[0]);
            void *msg = (void *)tswap32(vptr[1]);
            size_t len = tswap32(vptr[2]);
            int flags = tswap32(vptr[3]);
            void *target_addr = (void *)tswap32(vptr[4]);
            uint32_t *target_addrlen = (void *)tswap32(vptr[5]);
            socklen_t addrlen = tswap32(*target_addrlen);
            void *addr = alloca(addrlen);

            ret = get_errno(recvfrom(sockfd, msg, len, flags, addr, &addrlen));
            if (!is_error(ret)) {
                host_to_target_sockaddr(target_addr, addr, addrlen);
                *target_addrlen = tswap32(addrlen);
            }
802
803
804
        }
        break;
    case SOCKOP_shutdown:
bellard's avatar
bellard committed
805
806
807
808
809
810
        {
            int sockfd = tswap32(vptr[0]);
            int how = tswap32(vptr[1]);

            ret = get_errno(shutdown(sockfd, how));
        }
811
812
813
        break;
    case SOCKOP_sendmsg:
    case SOCKOP_recvmsg:
bellard's avatar
bellard committed
814
815
816
817
818
819
820
821
        {
            int fd;
            struct target_msghdr *msgp;
            struct msghdr msg;
            int flags, count, i;
            struct iovec *vec;
            struct target_iovec *target_vec;

bellard's avatar
bellard committed
822
            msgp = (void *)tswap32(vptr[1]);
bellard's avatar
bellard committed
823
824
            msg.msg_name = (void *)tswapl(msgp->msg_name);
            msg.msg_namelen = tswapl(msgp->msg_namelen);
bellard's avatar
bellard committed
825
826
            msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
            msg.msg_control = alloca(msg.msg_controllen);
bellard's avatar
bellard committed
827
828
829
830
831
832
833
834
835
836
837
838
            msg.msg_flags = tswap32(msgp->msg_flags);

            count = tswapl(msgp->msg_iovlen);
            vec = alloca(count * sizeof(struct iovec));
            target_vec = (void *)tswapl(msgp->msg_iov);
            for(i = 0;i < count; i++) {
                vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
                vec[i].iov_len = tswapl(target_vec[i].iov_len);
            }
            msg.msg_iovlen = count;
            msg.msg_iov = vec;

bellard's avatar
bellard committed
839
840
841
842
843
844
845
846
847
848
            fd = tswap32(vptr[0]);
            flags = tswap32(vptr[2]);
            if (num == SOCKOP_sendmsg) {
                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);
            }
bellard's avatar
bellard committed
849
850
        }
        break;
851
    case SOCKOP_setsockopt:
bellard's avatar
bellard committed
852
853
854
855
856
857
858
859
860
861
        {
            int sockfd = tswap32(vptr[0]);
            int level = tswap32(vptr[1]);
            int optname = tswap32(vptr[2]);
            void *optval = (void *)tswap32(vptr[3]);
            socklen_t optlen = tswap32(vptr[4]);

            ret = do_setsockopt(sockfd, level, optname, optval, optlen);
        }
        break;
862
    case SOCKOP_getsockopt:
bellard's avatar
bellard committed
863
864
865
866
867
        {
            int sockfd = tswap32(vptr[0]);
            int level = tswap32(vptr[1]);
            int optname = tswap32(vptr[2]);
            void *optval = (void *)tswap32(vptr[3]);
868
            uint32_t *poptlen = (void *)tswap32(vptr[4]);
bellard's avatar
bellard committed
869

870
            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard's avatar
bellard committed
871
872
        }
        break;
873
874
875
876
877
878
879
880
    default:
        gemu_log("Unsupported socketcall: %d\n", num);
        ret = -ENOSYS;
        break;
    }
    return ret;
}

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
958
959
960
961
962
963
964
965
966

#define N_SHM_REGIONS	32

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

static long do_ipc(long call, long first, long second, long third,
		   long ptr, long fifth)
{
    int version;
    long ret = 0;
    unsigned long raddr;
    struct shmid_ds shm_info;
    int i;

    version = call >> 16;
    call &= 0xffff;

    switch (call) {
    case IPCOP_shmat:
	/* SHM_* flags are the same on all linux platforms */
	ret = get_errno((long) shmat(first, (void *) ptr, second));
        if (is_error(ret))
            break;
        raddr = ret;
	/* find out the length of the shared memory segment */
        
        ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
        if (is_error(ret)) {
            /* can't get length, bail out */
            shmdt((void *) raddr);
	    break;
	}
	page_set_flags(raddr, raddr + shm_info.shm_segsz,
		       PAGE_VALID | PAGE_READ |
		       ((second & SHM_RDONLY)? 0: PAGE_WRITE));
	for (i = 0; i < N_SHM_REGIONS; ++i) {
	    if (shm_regions[i].start == 0) {
		shm_regions[i].start = raddr;
		shm_regions[i].size = shm_info.shm_segsz;
                break;
	    }
	}
	if (put_user(raddr, (uint32_t *)third))
            return -EFAULT;
        ret = 0;
	break;
    case IPCOP_shmdt:
	for (i = 0; i < N_SHM_REGIONS; ++i) {
	    if (shm_regions[i].start == ptr) {
		shm_regions[i].start = 0;
		page_set_flags(ptr, shm_regions[i].size, 0);
		break;
	    }
	}
	ret = get_errno(shmdt((void *) ptr));
	break;

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

	/* IPC_* and SHM_* command values are the same on all linux platforms */
    case IPCOP_shmctl:
        switch(second) {
        case IPC_RMID:
        case SHM_LOCK:
        case SHM_UNLOCK:
            ret = get_errno(shmctl(first, second, NULL));
            break;
        default:
            goto unimplemented;
        }
        break;
    default:
    unimplemented:
	gemu_log("Unsupported ipc call: %ld (version %d)\n", call, version);
	ret = -ENOSYS;
	break;
    }
    return ret;
}

967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/* kernel structure types definitions */
#define IFNAMSIZ        16

#define STRUCT(name, list...) STRUCT_ ## name,
#define STRUCT_SPECIAL(name) STRUCT_ ## name,
enum {
#include "syscall_types.h"
};
#undef STRUCT
#undef STRUCT_SPECIAL

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

typedef struct IOCTLEntry {
985
986
    unsigned int target_cmd;
    unsigned int host_cmd;
987
988
    const char *name;
    int access;
bellard's avatar
bellard committed
989
    const argtype arg_type[5];
990
991
992
993
994
995
996
997
} IOCTLEntry;

#define IOC_R 0x0001
#define IOC_W 0x0002
#define IOC_RW (IOC_R | IOC_W)

#define MAX_STRUCT_SIZE 4096

998
IOCTLEntry ioctl_entries[] = {
999
1000
#define IOCTL(cmd, access, types...) \
    { TARGET_ ## cmd, cmd, #cmd, access, { types } },
For faster browsing, not all history is shown. View entire blame