syscall.c 100 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>
bellard's avatar
bellard committed
45
#include <sys/statfs.h>
46
#include <utime.h>
bellard's avatar
bellard committed
47
#include <sys/sysinfo.h>
bellard's avatar
bellard committed
48
//#include <sys/user.h>
49
#include <netinet/ip.h>
bellard's avatar
bellard committed
50
#include <netinet/tcp.h>
51
52
53
54

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

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

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

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

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

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

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
203

#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

204
#define __NR_sys_uname __NR_uname
bellard's avatar
bellard committed
205
206
#define __NR_sys_getcwd1 __NR_getcwd
#define __NR_sys_getdents __NR_getdents
bellard's avatar
bellard committed
207
#define __NR_sys_getdents64 __NR_getdents64
bellard's avatar
bellard committed
208
#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
209

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

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

extern int personality(int);
bellard's avatar
bellard committed
233
234
235
extern int flock(int, int);
extern int setfsuid(int);
extern int setfsgid(int);
bellard's avatar
bellard committed
236
237
238
239
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 *);
240
extern int setgroups(int, gid_t *);
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

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;
}

264
long do_brk(char *new_brk)
265
266
267
268
269
270
271
272
273
274
{
    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
275
    brk_page = (char *)HOST_PAGE_ALIGN((unsigned long)target_brk);
276
277
278
279
280
281
282
283

    /* 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
284
285
286
287
    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));
288
289
290
291
292
293
294
295
296
297
298
    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
299
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    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
321
#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
322
323
324
325
326
327
    /* nothing to do */
#else
    int i, nw, j, k;
    target_long v;

    if (target_fds) {
328
        nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
329
330
331
332
333
334
335
336
337
338
339
340
341
        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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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
380
static inline void target_to_host_timeval(struct timeval *tv, 
bellard's avatar
bellard committed
381
                                          const struct target_timeval *target_tv)
382
{
bellard's avatar
bellard committed
383
384
    tv->tv_sec = tswapl(target_tv->tv_sec);
    tv->tv_usec = tswapl(target_tv->tv_usec);
385
386
}

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


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
409
        target_to_host_timeval(&tv, target_tv);
410
411
412
413
414
415
416
417
418
419
420
        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
421
            host_to_target_timeval(target_tv, &tv);
422
423
424
425
426
        }
    }
    return ret;
}

bellard's avatar
bellard committed
427
428
429
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
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)
{
533
534
535
536
    int val, ret;
            
    switch(level) {
    case SOL_TCP:
bellard's avatar
bellard committed
537
538
539
        /* TCP options all take an 'int' value.  */
        if (optlen < sizeof(uint32_t))
            return -EINVAL;
540
541
542
543
544
545
546
        
        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) {
bellard's avatar
bellard committed
547
548
        case IP_TOS:
        case IP_TTL:
549
        case IP_HDRINCL:
bellard's avatar
bellard committed
550
551
552
553
554
555
556
557
558
559
560
561
        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:
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
            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:
bellard's avatar
bellard committed
591
#ifdef SO_BSDCOMPAT
592
        case SO_BSDCOMPAT:
bellard's avatar
bellard committed
593
#endif
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
        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
609
    default:
610
611
612
    unimplemented:
        gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
        ret = -ENOSYS;
bellard's avatar
bellard committed
613
    }
614
    return ret;
bellard's avatar
bellard committed
615
616
617
618
619
}

static long do_getsockopt(int sockfd, int level, int optname, 
                          void *optval, socklen_t *optlen)
{
620
621
622
623
624
625
626
627
628
629
630
631
632
    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:
bellard's avatar
bellard committed
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
            goto int_case;
        }
        break;
    case SOL_TCP:
        /* TCP options all take an 'int' value.  */
    int_case:
        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;
    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:
672
673
674
675
676
677
678
679
            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;
bellard's avatar
bellard committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
            if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
                unsigned char ucval = val;
                len = 1;
		if (put_user(len, optlen))
                    return -EFAULT;
		if (copy_to_user(optval,&ucval,1))
                    return -EFAULT;
            } else {
                val = tswap32(val);
                if (len > sizeof(int))
                    len = sizeof(int);
                if (put_user(len, optlen))
                    return -EFAULT;
                if (copy_to_user(optval, &val, len))
                    return -EFAULT;
            }
696
            break;
bellard's avatar
bellard committed
697
698
        default:
            goto unimplemented;
699
700
701
702
703
704
705
706
707
708
        }
        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
709
710
711
}

static long do_socketcall(int num, int32_t *vptr)
712
713
714
715
716
{
    long ret;

    switch(num) {
    case SOCKOP_socket:
bellard's avatar
bellard committed
717
718
719
720
721
722
723
	{
            int domain = tswap32(vptr[0]);
            int type = tswap32(vptr[1]);
            int protocol = tswap32(vptr[2]);

            ret = get_errno(socket(domain, type, protocol));
	}
724
725
        break;
    case SOCKOP_bind:
bellard's avatar
bellard committed
726
727
728
729
730
731
732
733
734
	{
            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));
        }
735
736
        break;
    case SOCKOP_connect:
bellard's avatar
bellard committed
737
738
739
740
741
742
743
744
745
        {
            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));
        }
746
747
        break;
    case SOCKOP_listen:
bellard's avatar
bellard committed
748
749
750
751
752
753
        {
            int sockfd = tswap32(vptr[0]);
            int backlog = tswap32(vptr[1]);

            ret = get_errno(listen(sockfd, backlog));
        }
754
755
756
        break;
    case SOCKOP_accept:
        {
bellard's avatar
bellard committed
757
758
759
760
761
762
763
764
765
766
767
            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);
            }
768
769
770
771
        }
        break;
    case SOCKOP_getsockname:
        {
bellard's avatar
bellard committed
772
773
774
775
776
777
778
779
780
781
782
            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);
            }
783
784
785
786
        }
        break;
    case SOCKOP_getpeername:
        {
bellard's avatar
bellard committed
787
788
789
790
791
792
793
794
795
796
797
            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);
            }
798
799
800
801
        }
        break;
    case SOCKOP_socketpair:
        {
bellard's avatar
bellard committed
802
803
804
805
            int domain = tswap32(vptr[0]);
            int type = tswap32(vptr[1]);
            int protocol = tswap32(vptr[2]);
            int32_t *target_tab = (void *)tswap32(vptr[3]);
806
            int tab[2];
bellard's avatar
bellard committed
807
808

            ret = get_errno(socketpair(domain, type, protocol, tab));
809
810
811
812
813
814
815
            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
816
817
818
819
820
821
822
823
        {
            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));
        }
824
825
        break;
    case SOCKOP_recv:
bellard's avatar
bellard committed
826
827
828
829
830
831
832
833
        {
            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));
        }
834
835
        break;
    case SOCKOP_sendto:
bellard's avatar
bellard committed
836
837
838
839
840
841
842
843
844
845
846
847
        {
            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));
        }
848
849
850
        break;
    case SOCKOP_recvfrom:
        {
bellard's avatar
bellard committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
            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);
            }
865
866
867
        }
        break;
    case SOCKOP_shutdown:
bellard's avatar
bellard committed
868
869
870
871
872
873
        {
            int sockfd = tswap32(vptr[0]);
            int how = tswap32(vptr[1]);

            ret = get_errno(shutdown(sockfd, how));
        }
874
875
876
        break;
    case SOCKOP_sendmsg:
    case SOCKOP_recvmsg:
bellard's avatar
bellard committed
877
878
879
880
881
882
883
884
        {
            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
885
            msgp = (void *)tswap32(vptr[1]);
bellard's avatar
bellard committed
886
887
            msg.msg_name = (void *)tswapl(msgp->msg_name);
            msg.msg_namelen = tswapl(msgp->msg_namelen);
bellard's avatar
bellard committed
888
889
            msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
            msg.msg_control = alloca(msg.msg_controllen);
bellard's avatar
bellard committed
890
891
892
893
894
895
896
897
898
899
900
901
            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
902
903
904
905
906
907
908
909
910
911
            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
912
913
        }
        break;
914
    case SOCKOP_setsockopt:
bellard's avatar
bellard committed
915
916
917
918
919
920
921
922
923
924
        {
            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;
925
    case SOCKOP_getsockopt:
bellard's avatar
bellard committed
926
927
928
929
930
        {
            int sockfd = tswap32(vptr[0]);
            int level = tswap32(vptr[1]);
            int optname = tswap32(vptr[2]);
            void *optval = (void *)tswap32(vptr[3]);
931
            uint32_t *poptlen = (void *)tswap32(vptr[4]);
bellard's avatar
bellard committed
932

933
            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard's avatar
bellard committed
934
935
        }
        break;
936
937
938
939
940
941
942
943
    default:
        gemu_log("Unsupported socketcall: %d\n", num);
        ret = -ENOSYS;
        break;
    }
    return ret;
}

944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

#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;
	    }
	}
For faster browsing, not all history is shown. View entire blame