]> git.proxmox.com Git - qemu.git/blame - linux-user/syscall.c
simplified invocation - added automatic IDE disk geometry guessing to reuse old disk...
[qemu.git] / linux-user / syscall.c
CommitLineData
31e31b8a
FB
1/*
2 * Linux syscalls
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <stdlib.h>
21#include <stdio.h>
22#include <stdarg.h>
04369ff2 23#include <string.h>
31e31b8a
FB
24#include <elf.h>
25#include <endian.h>
26#include <errno.h>
27#include <unistd.h>
28#include <fcntl.h>
7854b056 29#include <time.h>
31e31b8a
FB
30#include <sys/types.h>
31#include <sys/wait.h>
32#include <sys/time.h>
33#include <sys/stat.h>
34#include <sys/mount.h>
35#include <sys/resource.h>
36#include <sys/mman.h>
37#include <sys/swap.h>
38#include <signal.h>
39#include <sched.h>
40#include <sys/socket.h>
41#include <sys/uio.h>
9de5e440 42#include <sys/poll.h>
32f36bce 43#include <sys/times.h>
72f03900 44//#include <sys/user.h>
7854b056 45#include <netinet/tcp.h>
31e31b8a
FB
46
47#define termios host_termios
48#define winsize host_winsize
49#define termio host_termio
04369ff2
FB
50#define sgttyb host_sgttyb /* same as target */
51#define tchars host_tchars /* same as target */
52#define ltchars host_ltchars /* same as target */
31e31b8a
FB
53
54#include <linux/termios.h>
55#include <linux/unistd.h>
56#include <linux/utsname.h>
57#include <linux/cdrom.h>
58#include <linux/hdreg.h>
59#include <linux/soundcard.h>
dab2ed99 60#include <linux/dirent.h>
19b84f3c 61#include <linux/kd.h>
31e31b8a 62
3ef693a0 63#include "qemu.h"
31e31b8a 64
72f03900 65//#define DEBUG
31e31b8a 66
1a9353d2
FB
67//#include <linux/msdos_fs.h>
68#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
69#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
70
70a194b9
FB
71
72#if defined(__powerpc__)
73#undef __syscall_nr
74#undef __sc_loadargs_0
75#undef __sc_loadargs_1
76#undef __sc_loadargs_2
77#undef __sc_loadargs_3
78#undef __sc_loadargs_4
79#undef __sc_loadargs_5
80#undef __sc_asm_input_0
81#undef __sc_asm_input_1
82#undef __sc_asm_input_2
83#undef __sc_asm_input_3
84#undef __sc_asm_input_4
85#undef __sc_asm_input_5
86#undef _syscall0
87#undef _syscall1
88#undef _syscall2
89#undef _syscall3
90#undef _syscall4
91#undef _syscall5
92
93/* need to redefine syscalls as Linux kernel defines are incorrect for
94 the clobber list */
95/* On powerpc a system call basically clobbers the same registers like a
96 * function call, with the exception of LR (which is needed for the
97 * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
98 * an error return status).
99 */
100
101#define __syscall_nr(nr, type, name, args...) \
102 unsigned long __sc_ret, __sc_err; \
103 { \
104 register unsigned long __sc_0 __asm__ ("r0"); \
105 register unsigned long __sc_3 __asm__ ("r3"); \
106 register unsigned long __sc_4 __asm__ ("r4"); \
107 register unsigned long __sc_5 __asm__ ("r5"); \
108 register unsigned long __sc_6 __asm__ ("r6"); \
109 register unsigned long __sc_7 __asm__ ("r7"); \
110 \
111 __sc_loadargs_##nr(name, args); \
112 __asm__ __volatile__ \
113 ("sc \n\t" \
114 "mfcr %0 " \
115 : "=&r" (__sc_0), \
116 "=&r" (__sc_3), "=&r" (__sc_4), \
117 "=&r" (__sc_5), "=&r" (__sc_6), \
118 "=&r" (__sc_7) \
119 : __sc_asm_input_##nr \
120 : "cr0", "ctr", "memory", \
121 "r8", "r9", "r10","r11", "r12"); \
122 __sc_ret = __sc_3; \
123 __sc_err = __sc_0; \
124 } \
125 if (__sc_err & 0x10000000) \
126 { \
127 errno = __sc_ret; \
128 __sc_ret = -1; \
129 } \
130 return (type) __sc_ret
131
132#define __sc_loadargs_0(name, dummy...) \
133 __sc_0 = __NR_##name
134#define __sc_loadargs_1(name, arg1) \
135 __sc_loadargs_0(name); \
136 __sc_3 = (unsigned long) (arg1)
137#define __sc_loadargs_2(name, arg1, arg2) \
138 __sc_loadargs_1(name, arg1); \
139 __sc_4 = (unsigned long) (arg2)
140#define __sc_loadargs_3(name, arg1, arg2, arg3) \
141 __sc_loadargs_2(name, arg1, arg2); \
142 __sc_5 = (unsigned long) (arg3)
143#define __sc_loadargs_4(name, arg1, arg2, arg3, arg4) \
144 __sc_loadargs_3(name, arg1, arg2, arg3); \
145 __sc_6 = (unsigned long) (arg4)
146#define __sc_loadargs_5(name, arg1, arg2, arg3, arg4, arg5) \
147 __sc_loadargs_4(name, arg1, arg2, arg3, arg4); \
148 __sc_7 = (unsigned long) (arg5)
149
150#define __sc_asm_input_0 "0" (__sc_0)
151#define __sc_asm_input_1 __sc_asm_input_0, "1" (__sc_3)
152#define __sc_asm_input_2 __sc_asm_input_1, "2" (__sc_4)
153#define __sc_asm_input_3 __sc_asm_input_2, "3" (__sc_5)
154#define __sc_asm_input_4 __sc_asm_input_3, "4" (__sc_6)
155#define __sc_asm_input_5 __sc_asm_input_4, "5" (__sc_7)
156
157#define _syscall0(type,name) \
158type name(void) \
159{ \
160 __syscall_nr(0, type, name); \
161}
162
163#define _syscall1(type,name,type1,arg1) \
164type name(type1 arg1) \
165{ \
166 __syscall_nr(1, type, name, arg1); \
167}
168
169#define _syscall2(type,name,type1,arg1,type2,arg2) \
170type name(type1 arg1, type2 arg2) \
171{ \
172 __syscall_nr(2, type, name, arg1, arg2); \
173}
174
175#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
176type name(type1 arg1, type2 arg2, type3 arg3) \
177{ \
178 __syscall_nr(3, type, name, arg1, arg2, arg3); \
179}
180
181#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
182type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
183{ \
184 __syscall_nr(4, type, name, arg1, arg2, arg3, arg4); \
185}
186
187#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
188type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \
189{ \
190 __syscall_nr(5, type, name, arg1, arg2, arg3, arg4, arg5); \
191}
192#endif
193
31e31b8a 194#define __NR_sys_uname __NR_uname
72f03900 195#define __NR_sys_getcwd1 __NR_getcwd
31e31b8a
FB
196#define __NR_sys_statfs __NR_statfs
197#define __NR_sys_fstatfs __NR_fstatfs
72f03900 198#define __NR_sys_getdents __NR_getdents
dab2ed99 199#define __NR_sys_getdents64 __NR_getdents64
66fb9763 200#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
31e31b8a 201
27725c1d 202#if defined(__alpha__) || defined (__ia64__)
9af9eaaa
FB
203#define __NR__llseek __NR_lseek
204#endif
205
72f03900 206#ifdef __NR_gettid
31e31b8a 207_syscall0(int, gettid)
72f03900
FB
208#else
209static int gettid(void) {
210 return -ENOSYS;
211}
212#endif
31e31b8a 213_syscall1(int,sys_uname,struct new_utsname *,buf)
72f03900
FB
214_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
215_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
dab2ed99 216_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
31e31b8a
FB
217_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
218 loff_t *, res, uint, wh);
72f03900
FB
219_syscall2(int,sys_statfs,const char *,path,struct kernel_statfs *,buf)
220_syscall2(int,sys_fstatfs,int,fd,struct kernel_statfs *,buf)
66fb9763 221_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
ec86b0fb
FB
222#ifdef __NR_exit_group
223_syscall1(int,exit_group,int,error_code)
224#endif
66fb9763
FB
225
226extern int personality(int);
9de5e440
FB
227extern int flock(int, int);
228extern int setfsuid(int);
229extern int setfsgid(int);
5cd4393b
FB
230extern int setresuid(uid_t, uid_t, uid_t);
231extern int getresuid(uid_t *, uid_t *, uid_t *);
232extern int setresgid(gid_t, gid_t, gid_t);
233extern int getresgid(gid_t *, gid_t *, gid_t *);
19b84f3c 234extern int setgroups(int, gid_t *);
31e31b8a
FB
235
236static inline long get_errno(long ret)
237{
238 if (ret == -1)
239 return -errno;
240 else
241 return ret;
242}
243
244static inline int is_error(long ret)
245{
246 return (unsigned long)ret >= (unsigned long)(-4096);
247}
248
249static char *target_brk;
250static char *target_original_brk;
251
252void target_set_brk(char *new_brk)
253{
254 target_brk = new_brk;
255 target_original_brk = new_brk;
256}
257
258static long do_brk(char *new_brk)
259{
260 char *brk_page;
261 long mapped_addr;
262 int new_alloc_size;
263
264 if (!new_brk)
265 return (long)target_brk;
266 if (new_brk < target_original_brk)
267 return -ENOMEM;
268
54936004 269 brk_page = (char *)HOST_PAGE_ALIGN((unsigned long)target_brk);
31e31b8a
FB
270
271 /* If the new brk is less than this, set it and we're done... */
272 if (new_brk < brk_page) {
273 target_brk = new_brk;
274 return (long)target_brk;
275 }
276
277 /* We need to allocate more memory after the brk... */
54936004
FB
278 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
279 mapped_addr = get_errno(target_mmap((unsigned long)brk_page, new_alloc_size,
280 PROT_READ|PROT_WRITE,
281 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
31e31b8a
FB
282 if (is_error(mapped_addr)) {
283 return mapped_addr;
284 } else {
285 target_brk = new_brk;
286 return (long)target_brk;
287 }
288}
289
290static inline fd_set *target_to_host_fds(fd_set *fds,
291 target_long *target_fds, int n)
292{
7854b056 293#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
31e31b8a
FB
294 return (fd_set *)target_fds;
295#else
296 int i, b;
297 if (target_fds) {
298 FD_ZERO(fds);
299 for(i = 0;i < n; i++) {
300 b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
301 (i & (TARGET_LONG_BITS - 1))) & 1;
302 if (b)
303 FD_SET(i, fds);
304 }
305 return fds;
306 } else {
307 return NULL;
308 }
309#endif
310}
311
312static inline void host_to_target_fds(target_long *target_fds,
313 fd_set *fds, int n)
314{
7854b056 315#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
31e31b8a
FB
316 /* nothing to do */
317#else
318 int i, nw, j, k;
319 target_long v;
320
321 if (target_fds) {
322 nw = n / TARGET_LONG_BITS;
323 k = 0;
324 for(i = 0;i < nw; i++) {
325 v = 0;
326 for(j = 0; j < TARGET_LONG_BITS; j++) {
327 v |= ((FD_ISSET(k, fds) != 0) << j);
328 k++;
329 }
330 target_fds[i] = tswapl(v);
331 }
332 }
333#endif
334}
335
c596ed17
FB
336#if defined(__alpha__)
337#define HOST_HZ 1024
338#else
339#define HOST_HZ 100
340#endif
341
342static inline long host_to_target_clock_t(long ticks)
343{
344#if HOST_HZ == TARGET_HZ
345 return ticks;
346#else
347 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
348#endif
349}
350
b409186b
FB
351static inline void host_to_target_rusage(struct target_rusage *target_rusage,
352 const struct rusage *rusage)
353{
354 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
355 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
356 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
357 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
358 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
359 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
360 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
361 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
362 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
363 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
364 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
365 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
366 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
367 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
368 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
369 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
370 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
371 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
372}
373
66fb9763 374static inline void target_to_host_timeval(struct timeval *tv,
5cd4393b 375 const struct target_timeval *target_tv)
31e31b8a 376{
66fb9763
FB
377 tv->tv_sec = tswapl(target_tv->tv_sec);
378 tv->tv_usec = tswapl(target_tv->tv_usec);
31e31b8a
FB
379}
380
66fb9763 381static inline void host_to_target_timeval(struct target_timeval *target_tv,
5cd4393b 382 const struct timeval *tv)
31e31b8a 383{
66fb9763
FB
384 target_tv->tv_sec = tswapl(tv->tv_sec);
385 target_tv->tv_usec = tswapl(tv->tv_usec);
31e31b8a
FB
386}
387
388
389static long do_select(long n,
390 target_long *target_rfds, target_long *target_wfds,
391 target_long *target_efds, struct target_timeval *target_tv)
392{
393 fd_set rfds, wfds, efds;
394 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
395 struct timeval tv, *tv_ptr;
396 long ret;
397
398 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
399 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
400 efds_ptr = target_to_host_fds(&efds, target_efds, n);
401
402 if (target_tv) {
5cd4393b 403 target_to_host_timeval(&tv, target_tv);
31e31b8a
FB
404 tv_ptr = &tv;
405 } else {
406 tv_ptr = NULL;
407 }
408 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
409 if (!is_error(ret)) {
410 host_to_target_fds(target_rfds, rfds_ptr, n);
411 host_to_target_fds(target_wfds, wfds_ptr, n);
412 host_to_target_fds(target_efds, efds_ptr, n);
413
414 if (target_tv) {
5cd4393b 415 host_to_target_timeval(target_tv, &tv);
31e31b8a
FB
416 }
417 }
418 return ret;
419}
420
7854b056
FB
421static inline void target_to_host_sockaddr(struct sockaddr *addr,
422 struct target_sockaddr *target_addr,
423 socklen_t len)
424{
425 memcpy(addr, target_addr, len);
426 addr->sa_family = tswap16(target_addr->sa_family);
427}
428
429static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
430 struct sockaddr *addr,
431 socklen_t len)
432{
433 memcpy(target_addr, addr, len);
434 target_addr->sa_family = tswap16(addr->sa_family);
435}
436
437static inline void target_to_host_cmsg(struct msghdr *msgh,
438 struct target_msghdr *target_msgh)
439{
440 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
441 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
442 socklen_t space = 0;
443
444 while (cmsg && target_cmsg) {
445 void *data = CMSG_DATA(cmsg);
446 void *target_data = TARGET_CMSG_DATA(target_cmsg);
447
448 int len = tswapl(target_cmsg->cmsg_len)
449 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
450
451 space += CMSG_SPACE(len);
452 if (space > msgh->msg_controllen) {
453 space -= CMSG_SPACE(len);
454 gemu_log("Host cmsg overflow");
455 break;
456 }
457
458 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
459 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
460 cmsg->cmsg_len = CMSG_LEN(len);
461
462 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
463 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
464 memcpy(data, target_data, len);
465 } else {
466 int *fd = (int *)data;
467 int *target_fd = (int *)target_data;
468 int i, numfds = len / sizeof(int);
469
470 for (i = 0; i < numfds; i++)
471 fd[i] = tswap32(target_fd[i]);
472 }
473
474 cmsg = CMSG_NXTHDR(msgh, cmsg);
475 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
476 }
477
478 msgh->msg_controllen = space;
479}
480
481static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
482 struct msghdr *msgh)
483{
484 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
485 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
486 socklen_t space = 0;
487
488 while (cmsg && target_cmsg) {
489 void *data = CMSG_DATA(cmsg);
490 void *target_data = TARGET_CMSG_DATA(target_cmsg);
491
492 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
493
494 space += TARGET_CMSG_SPACE(len);
495 if (space > tswapl(target_msgh->msg_controllen)) {
496 space -= TARGET_CMSG_SPACE(len);
497 gemu_log("Target cmsg overflow");
498 break;
499 }
500
501 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
502 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
503 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
504
505 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
506 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
507 memcpy(target_data, data, len);
508 } else {
509 int *fd = (int *)data;
510 int *target_fd = (int *)target_data;
511 int i, numfds = len / sizeof(int);
512
513 for (i = 0; i < numfds; i++)
514 target_fd[i] = tswap32(fd[i]);
515 }
516
517 cmsg = CMSG_NXTHDR(msgh, cmsg);
518 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
519 }
520
521 msgh->msg_controllen = tswapl(space);
522}
523
524static long do_setsockopt(int sockfd, int level, int optname,
525 void *optval, socklen_t optlen)
526{
527 if (level == SOL_TCP) {
528 /* TCP options all take an 'int' value. */
529 int val;
530
531 if (optlen < sizeof(uint32_t))
532 return -EINVAL;
533
534 val = tswap32(*(uint32_t *)optval);
535 return get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
536 }
537
538 else if (level != SOL_SOCKET) {
539 gemu_log("Unsupported setsockopt level: %d\n", level);
540 return -ENOSYS;
541 }
542
543 switch (optname) {
544 /* Options with 'int' argument. */
545 case SO_DEBUG:
546 case SO_REUSEADDR:
547 case SO_TYPE:
548 case SO_ERROR:
549 case SO_DONTROUTE:
550 case SO_BROADCAST:
551 case SO_SNDBUF:
552 case SO_RCVBUF:
553 case SO_KEEPALIVE:
554 case SO_OOBINLINE:
555 case SO_NO_CHECK:
556 case SO_PRIORITY:
557 case SO_BSDCOMPAT:
558 case SO_PASSCRED:
559 case SO_TIMESTAMP:
560 case SO_RCVLOWAT:
561 case SO_RCVTIMEO:
562 case SO_SNDTIMEO:
563 {
564 int val;
565 if (optlen < sizeof(uint32_t))
566 return -EINVAL;
567 val = tswap32(*(uint32_t *)optval);
568 return get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
569 }
570
571 default:
572 gemu_log("Unsupported setsockopt SOL_SOCKET option: %d\n", optname);
573 return -ENOSYS;
574 }
575}
576
577static long do_getsockopt(int sockfd, int level, int optname,
578 void *optval, socklen_t *optlen)
579{
580 gemu_log("getsockopt not yet supported\n");
581 return -ENOSYS;
582}
583
584static long do_socketcall(int num, int32_t *vptr)
31e31b8a
FB
585{
586 long ret;
587
588 switch(num) {
589 case SOCKOP_socket:
7854b056
FB
590 {
591 int domain = tswap32(vptr[0]);
592 int type = tswap32(vptr[1]);
593 int protocol = tswap32(vptr[2]);
594
595 ret = get_errno(socket(domain, type, protocol));
596 }
31e31b8a
FB
597 break;
598 case SOCKOP_bind:
7854b056
FB
599 {
600 int sockfd = tswap32(vptr[0]);
601 void *target_addr = (void *)tswap32(vptr[1]);
602 socklen_t addrlen = tswap32(vptr[2]);
603 void *addr = alloca(addrlen);
604
605 target_to_host_sockaddr(addr, target_addr, addrlen);
606 ret = get_errno(bind(sockfd, addr, addrlen));
607 }
31e31b8a
FB
608 break;
609 case SOCKOP_connect:
7854b056
FB
610 {
611 int sockfd = tswap32(vptr[0]);
612 void *target_addr = (void *)tswap32(vptr[1]);
613 socklen_t addrlen = tswap32(vptr[2]);
614 void *addr = alloca(addrlen);
615
616 target_to_host_sockaddr(addr, target_addr, addrlen);
617 ret = get_errno(connect(sockfd, addr, addrlen));
618 }
31e31b8a
FB
619 break;
620 case SOCKOP_listen:
7854b056
FB
621 {
622 int sockfd = tswap32(vptr[0]);
623 int backlog = tswap32(vptr[1]);
624
625 ret = get_errno(listen(sockfd, backlog));
626 }
31e31b8a
FB
627 break;
628 case SOCKOP_accept:
629 {
7854b056
FB
630 int sockfd = tswap32(vptr[0]);
631 void *target_addr = (void *)tswap32(vptr[1]);
632 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
633 socklen_t addrlen = tswap32(*target_addrlen);
634 void *addr = alloca(addrlen);
635
636 ret = get_errno(accept(sockfd, addr, &addrlen));
637 if (!is_error(ret)) {
638 host_to_target_sockaddr(target_addr, addr, addrlen);
639 *target_addrlen = tswap32(addrlen);
640 }
31e31b8a
FB
641 }
642 break;
643 case SOCKOP_getsockname:
644 {
7854b056
FB
645 int sockfd = tswap32(vptr[0]);
646 void *target_addr = (void *)tswap32(vptr[1]);
647 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
648 socklen_t addrlen = tswap32(*target_addrlen);
649 void *addr = alloca(addrlen);
650
651 ret = get_errno(getsockname(sockfd, addr, &addrlen));
652 if (!is_error(ret)) {
653 host_to_target_sockaddr(target_addr, addr, addrlen);
654 *target_addrlen = tswap32(addrlen);
655 }
31e31b8a
FB
656 }
657 break;
658 case SOCKOP_getpeername:
659 {
7854b056
FB
660 int sockfd = tswap32(vptr[0]);
661 void *target_addr = (void *)tswap32(vptr[1]);
662 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
663 socklen_t addrlen = tswap32(*target_addrlen);
664 void *addr = alloca(addrlen);
665
666 ret = get_errno(getpeername(sockfd, addr, &addrlen));
667 if (!is_error(ret)) {
668 host_to_target_sockaddr(target_addr, addr, addrlen);
669 *target_addrlen = tswap32(addrlen);
670 }
31e31b8a
FB
671 }
672 break;
673 case SOCKOP_socketpair:
674 {
7854b056
FB
675 int domain = tswap32(vptr[0]);
676 int type = tswap32(vptr[1]);
677 int protocol = tswap32(vptr[2]);
678 int32_t *target_tab = (void *)tswap32(vptr[3]);
31e31b8a 679 int tab[2];
7854b056
FB
680
681 ret = get_errno(socketpair(domain, type, protocol, tab));
31e31b8a
FB
682 if (!is_error(ret)) {
683 target_tab[0] = tswap32(tab[0]);
684 target_tab[1] = tswap32(tab[1]);
685 }
686 }
687 break;
688 case SOCKOP_send:
7854b056
FB
689 {
690 int sockfd = tswap32(vptr[0]);
691 void *msg = (void *)tswap32(vptr[1]);
692 size_t len = tswap32(vptr[2]);
693 int flags = tswap32(vptr[3]);
694
695 ret = get_errno(send(sockfd, msg, len, flags));
696 }
31e31b8a
FB
697 break;
698 case SOCKOP_recv:
7854b056
FB
699 {
700 int sockfd = tswap32(vptr[0]);
701 void *msg = (void *)tswap32(vptr[1]);
702 size_t len = tswap32(vptr[2]);
703 int flags = tswap32(vptr[3]);
704
705 ret = get_errno(recv(sockfd, msg, len, flags));
706 }
31e31b8a
FB
707 break;
708 case SOCKOP_sendto:
7854b056
FB
709 {
710 int sockfd = tswap32(vptr[0]);
711 void *msg = (void *)tswap32(vptr[1]);
712 size_t len = tswap32(vptr[2]);
713 int flags = tswap32(vptr[3]);
714 void *target_addr = (void *)tswap32(vptr[4]);
715 socklen_t addrlen = tswap32(vptr[5]);
716 void *addr = alloca(addrlen);
717
718 target_to_host_sockaddr(addr, target_addr, addrlen);
719 ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
720 }
31e31b8a
FB
721 break;
722 case SOCKOP_recvfrom:
723 {
7854b056
FB
724 int sockfd = tswap32(vptr[0]);
725 void *msg = (void *)tswap32(vptr[1]);
726 size_t len = tswap32(vptr[2]);
727 int flags = tswap32(vptr[3]);
728 void *target_addr = (void *)tswap32(vptr[4]);
729 uint32_t *target_addrlen = (void *)tswap32(vptr[5]);
730 socklen_t addrlen = tswap32(*target_addrlen);
731 void *addr = alloca(addrlen);
732
733 ret = get_errno(recvfrom(sockfd, msg, len, flags, addr, &addrlen));
734 if (!is_error(ret)) {
735 host_to_target_sockaddr(target_addr, addr, addrlen);
736 *target_addrlen = tswap32(addrlen);
737 }
31e31b8a
FB
738 }
739 break;
740 case SOCKOP_shutdown:
7854b056
FB
741 {
742 int sockfd = tswap32(vptr[0]);
743 int how = tswap32(vptr[1]);
744
745 ret = get_errno(shutdown(sockfd, how));
746 }
31e31b8a
FB
747 break;
748 case SOCKOP_sendmsg:
749 case SOCKOP_recvmsg:
1a9353d2
FB
750 {
751 int fd;
752 struct target_msghdr *msgp;
753 struct msghdr msg;
754 int flags, count, i;
755 struct iovec *vec;
756 struct target_iovec *target_vec;
757
7854b056 758 msgp = (void *)tswap32(vptr[1]);
1a9353d2
FB
759 msg.msg_name = (void *)tswapl(msgp->msg_name);
760 msg.msg_namelen = tswapl(msgp->msg_namelen);
7854b056
FB
761 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
762 msg.msg_control = alloca(msg.msg_controllen);
1a9353d2
FB
763 msg.msg_flags = tswap32(msgp->msg_flags);
764
765 count = tswapl(msgp->msg_iovlen);
766 vec = alloca(count * sizeof(struct iovec));
767 target_vec = (void *)tswapl(msgp->msg_iov);
768 for(i = 0;i < count; i++) {
769 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
770 vec[i].iov_len = tswapl(target_vec[i].iov_len);
771 }
772 msg.msg_iovlen = count;
773 msg.msg_iov = vec;
774
7854b056
FB
775 fd = tswap32(vptr[0]);
776 flags = tswap32(vptr[2]);
777 if (num == SOCKOP_sendmsg) {
778 target_to_host_cmsg(&msg, msgp);
779 ret = get_errno(sendmsg(fd, &msg, flags));
780 } else {
781 ret = get_errno(recvmsg(fd, &msg, flags));
782 if (!is_error(ret))
783 host_to_target_cmsg(msgp, &msg);
784 }
1a9353d2
FB
785 }
786 break;
31e31b8a 787 case SOCKOP_setsockopt:
7854b056
FB
788 {
789 int sockfd = tswap32(vptr[0]);
790 int level = tswap32(vptr[1]);
791 int optname = tswap32(vptr[2]);
792 void *optval = (void *)tswap32(vptr[3]);
793 socklen_t optlen = tswap32(vptr[4]);
794
795 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
796 }
797 break;
31e31b8a 798 case SOCKOP_getsockopt:
7854b056
FB
799 {
800 int sockfd = tswap32(vptr[0]);
801 int level = tswap32(vptr[1]);
802 int optname = tswap32(vptr[2]);
803 void *optval = (void *)tswap32(vptr[3]);
804 uint32_t *target_len = (void *)tswap32(vptr[4]);
805 socklen_t optlen = tswap32(*target_len);
806
807 ret = do_getsockopt(sockfd, level, optname, optval, &optlen);
808 if (!is_error(ret))
809 *target_len = tswap32(optlen);
810 }
811 break;
31e31b8a
FB
812 default:
813 gemu_log("Unsupported socketcall: %d\n", num);
814 ret = -ENOSYS;
815 break;
816 }
817 return ret;
818}
819
820/* kernel structure types definitions */
821#define IFNAMSIZ 16
822
823#define STRUCT(name, list...) STRUCT_ ## name,
824#define STRUCT_SPECIAL(name) STRUCT_ ## name,
825enum {
826#include "syscall_types.h"
827};
828#undef STRUCT
829#undef STRUCT_SPECIAL
830
831#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
832#define STRUCT_SPECIAL(name)
833#include "syscall_types.h"
834#undef STRUCT
835#undef STRUCT_SPECIAL
836
837typedef struct IOCTLEntry {
2ab83ea7
FB
838 unsigned int target_cmd;
839 unsigned int host_cmd;
31e31b8a
FB
840 const char *name;
841 int access;
1a9353d2 842 const argtype arg_type[5];
31e31b8a
FB
843} IOCTLEntry;
844
845#define IOC_R 0x0001
846#define IOC_W 0x0002
847#define IOC_RW (IOC_R | IOC_W)
848
849#define MAX_STRUCT_SIZE 4096
850
2ab83ea7 851IOCTLEntry ioctl_entries[] = {
31e31b8a
FB
852#define IOCTL(cmd, access, types...) \
853 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
854#include "ioctls.h"
855 { 0, 0, },
856};
857
858static long do_ioctl(long fd, long cmd, long arg)
859{
860 const IOCTLEntry *ie;
861 const argtype *arg_type;
862 long ret;
863 uint8_t buf_temp[MAX_STRUCT_SIZE];
864
865 ie = ioctl_entries;
866 for(;;) {
867 if (ie->target_cmd == 0) {
868 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
869 return -ENOSYS;
870 }
871 if (ie->target_cmd == cmd)
872 break;
873 ie++;
874 }
875 arg_type = ie->arg_type;
9de5e440 876#if defined(DEBUG)
72f03900
FB
877 gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
878#endif
31e31b8a
FB
879 switch(arg_type[0]) {
880 case TYPE_NULL:
881 /* no argument */
882 ret = get_errno(ioctl(fd, ie->host_cmd));
883 break;
884 case TYPE_PTRVOID:
885 case TYPE_INT:
886 /* int argment */
887 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
888 break;
889 case TYPE_PTR:
890 arg_type++;
891 switch(ie->access) {
892 case IOC_R:
893 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
894 if (!is_error(ret)) {
895 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
896 }
897 break;
898 case IOC_W:
899 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
900 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
901 break;
902 default:
903 case IOC_RW:
904 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
905 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
906 if (!is_error(ret)) {
907 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
908 }
909 break;
910 }
911 break;
912 default:
913 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
914 ret = -ENOSYS;
915 break;
916 }
917 return ret;
918}
919
920bitmask_transtbl iflag_tbl[] = {
921 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
922 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
923 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
924 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
925 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
926 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
927 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
928 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
929 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
930 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
931 { TARGET_IXON, TARGET_IXON, IXON, IXON },
932 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
933 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
934 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
935 { 0, 0, 0, 0 }
936};
937
938bitmask_transtbl oflag_tbl[] = {
939 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
940 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
941 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
942 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
943 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
944 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
945 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
946 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
947 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
948 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
949 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
950 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
951 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
952 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
953 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
954 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
955 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
956 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
957 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
958 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
959 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
960 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
961 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
962 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
963 { 0, 0, 0, 0 }
964};
965
966bitmask_transtbl cflag_tbl[] = {
967 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
968 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
969 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
970 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
971 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
972 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
973 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
974 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
975 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
976 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
977 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
978 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
979 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
980 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
981 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
982 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
983 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
984 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
985 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
986 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
987 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
988 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
989 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
990 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
991 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
992 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
993 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
994 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
995 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
996 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
997 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
998 { 0, 0, 0, 0 }
999};
1000
1001bitmask_transtbl lflag_tbl[] = {
1002 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1003 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1004 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1005 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1006 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1007 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1008 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1009 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1010 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1011 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1012 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1013 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1014 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1015 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1016 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1017 { 0, 0, 0, 0 }
1018};
1019
1020static void target_to_host_termios (void *dst, const void *src)
1021{
1022 struct host_termios *host = dst;
1023 const struct target_termios *target = src;
1024
1025 host->c_iflag =
1026 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
1027 host->c_oflag =
1028 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
1029 host->c_cflag =
1030 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
1031 host->c_lflag =
1032 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
1033 host->c_line = target->c_line;
1034
1035 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
1036 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
1037 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
1038 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
1039 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
1040 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
1041 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
1042 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
1043 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
1044 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
1045 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
1046 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
1047 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
1048 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
1049 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
1050 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
1051 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
1052}
1053
1054static void host_to_target_termios (void *dst, const void *src)
1055{
1056 struct target_termios *target = dst;
1057 const struct host_termios *host = src;
1058
1059 target->c_iflag =
1060 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
1061 target->c_oflag =
1062 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
1063 target->c_cflag =
1064 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
1065 target->c_lflag =
1066 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
1067 target->c_line = host->c_line;
1068
1069 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
1070 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
1071 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
1072 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
1073 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
1074 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
1075 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
1076 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
1077 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
1078 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
1079 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
1080 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
1081 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
1082 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
1083 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
1084 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
1085 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
1086}
1087
1088StructEntry struct_termios_def = {
1089 .convert = { host_to_target_termios, target_to_host_termios },
1090 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
1091 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
1092};
1093
5286db75
FB
1094static bitmask_transtbl mmap_flags_tbl[] = {
1095 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
1096 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
1097 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
1098 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
1099 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
1100 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
1101 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
1102 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
1103 { 0, 0, 0, 0 }
1104};
1105
2ab83ea7 1106#if defined(TARGET_I386)
6dbad63e
FB
1107
1108/* NOTE: there is really one LDT for all the threads */
1109uint8_t *ldt_table;
1110
1111static int read_ldt(void *ptr, unsigned long bytecount)
1112{
1113 int size;
1114
1115 if (!ldt_table)
1116 return 0;
1117 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
1118 if (size > bytecount)
1119 size = bytecount;
1120 memcpy(ptr, ldt_table, size);
1121 return size;
1122}
1123
1124/* XXX: add locking support */
1125static int write_ldt(CPUX86State *env,
1126 void *ptr, unsigned long bytecount, int oldmode)
1127{
1128 struct target_modify_ldt_ldt_s ldt_info;
1129 int seg_32bit, contents, read_exec_only, limit_in_pages;
1130 int seg_not_present, useable;
1131 uint32_t *lp, entry_1, entry_2;
1132
1133 if (bytecount != sizeof(ldt_info))
1134 return -EINVAL;
1135 memcpy(&ldt_info, ptr, sizeof(ldt_info));
1136 tswap32s(&ldt_info.entry_number);
1137 tswapls((long *)&ldt_info.base_addr);
1138 tswap32s(&ldt_info.limit);
1139 tswap32s(&ldt_info.flags);
1140
1141 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
1142 return -EINVAL;
1143 seg_32bit = ldt_info.flags & 1;
1144 contents = (ldt_info.flags >> 1) & 3;
1145 read_exec_only = (ldt_info.flags >> 3) & 1;
1146 limit_in_pages = (ldt_info.flags >> 4) & 1;
1147 seg_not_present = (ldt_info.flags >> 5) & 1;
1148 useable = (ldt_info.flags >> 6) & 1;
1149
1150 if (contents == 3) {
1151 if (oldmode)
1152 return -EINVAL;
1153 if (seg_not_present == 0)
1154 return -EINVAL;
1155 }
1156 /* allocate the LDT */
1157 if (!ldt_table) {
1158 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1159 if (!ldt_table)
1160 return -ENOMEM;
1161 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1162 env->ldt.base = ldt_table;
1163 env->ldt.limit = 0xffff;
1164 }
1165
1166 /* NOTE: same code as Linux kernel */
1167 /* Allow LDTs to be cleared by the user. */
1168 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
1169 if (oldmode ||
1170 (contents == 0 &&
1171 read_exec_only == 1 &&
1172 seg_32bit == 0 &&
1173 limit_in_pages == 0 &&
1174 seg_not_present == 1 &&
1175 useable == 0 )) {
1176 entry_1 = 0;
1177 entry_2 = 0;
1178 goto install;
1179 }
1180 }
1181
1182 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
1183 (ldt_info.limit & 0x0ffff);
1184 entry_2 = (ldt_info.base_addr & 0xff000000) |
1185 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
1186 (ldt_info.limit & 0xf0000) |
1187 ((read_exec_only ^ 1) << 9) |
1188 (contents << 10) |
1189 ((seg_not_present ^ 1) << 15) |
1190 (seg_32bit << 22) |
1191 (limit_in_pages << 23) |
1192 0x7000;
1193 if (!oldmode)
1194 entry_2 |= (useable << 20);
14ae3ba7 1195
6dbad63e
FB
1196 /* Install the new entry ... */
1197install:
1198 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
1199 lp[0] = tswap32(entry_1);
1200 lp[1] = tswap32(entry_2);
1201 return 0;
1202}
1203
1204/* specific and weird i386 syscalls */
5cd4393b 1205int do_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
6dbad63e
FB
1206{
1207 int ret = -ENOSYS;
1208
1209 switch (func) {
1210 case 0:
1211 ret = read_ldt(ptr, bytecount);
1212 break;
1213 case 1:
1214 ret = write_ldt(env, ptr, bytecount, 1);
1215 break;
1216 case 0x11:
1217 ret = write_ldt(env, ptr, bytecount, 0);
1218 break;
1219 }
1220 return ret;
1221}
1b6b029e 1222
2ab83ea7
FB
1223#endif /* defined(TARGET_I386) */
1224
1b6b029e
FB
1225/* this stack is the equivalent of the kernel stack associated with a
1226 thread/process */
1227#define NEW_STACK_SIZE 8192
1228
1229static int clone_func(void *arg)
1230{
2ab83ea7 1231 CPUState *env = arg;
1b6b029e
FB
1232 cpu_loop(env);
1233 /* never exits */
1234 return 0;
1235}
1236
2ab83ea7 1237int do_fork(CPUState *env, unsigned int flags, unsigned long newsp)
1b6b029e
FB
1238{
1239 int ret;
5cd4393b 1240 TaskState *ts;
1b6b029e 1241 uint8_t *new_stack;
2ab83ea7 1242 CPUState *new_env;
1b6b029e
FB
1243
1244 if (flags & CLONE_VM) {
5cd4393b
FB
1245 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
1246 memset(ts, 0, sizeof(TaskState));
1247 new_stack = ts->stack;
1248 ts->used = 1;
1249 /* add in task state list */
1250 ts->next = first_task_state;
1251 first_task_state = ts;
1b6b029e 1252 /* we create a new CPU instance. */
2ab83ea7
FB
1253 new_env = cpu_init();
1254 memcpy(new_env, env, sizeof(CPUState));
1255#if defined(TARGET_I386)
1256 if (!newsp)
1257 newsp = env->regs[R_ESP];
1b6b029e
FB
1258 new_env->regs[R_ESP] = newsp;
1259 new_env->regs[R_EAX] = 0;
2ab83ea7
FB
1260#elif defined(TARGET_ARM)
1261 if (!newsp)
1262 newsp = env->regs[13];
1263 new_env->regs[13] = newsp;
1264 new_env->regs[0] = 0;
1265#else
1266#error unsupported target CPU
1267#endif
5cd4393b 1268 new_env->opaque = ts;
27725c1d
FB
1269#ifdef __ia64__
1270 ret = clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1271#else
1272 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1273#endif
1b6b029e
FB
1274 } else {
1275 /* if no CLONE_VM, we consider it is a fork */
1276 if ((flags & ~CSIGNAL) != 0)
1277 return -EINVAL;
1278 ret = fork();
1279 }
1280 return ret;
1281}
1282
7775e9ec
FB
1283static long do_fcntl(int fd, int cmd, unsigned long arg)
1284{
1285 struct flock fl;
1286 struct target_flock *target_fl = (void *)arg;
1287 long ret;
1288
1289 switch(cmd) {
1290 case TARGET_F_GETLK:
1291 ret = fcntl(fd, cmd, &fl);
1292 if (ret == 0) {
1293 target_fl->l_type = tswap16(fl.l_type);
1294 target_fl->l_whence = tswap16(fl.l_whence);
1295 target_fl->l_start = tswapl(fl.l_start);
1296 target_fl->l_len = tswapl(fl.l_len);
1297 target_fl->l_pid = tswapl(fl.l_pid);
1298 }
1299 break;
1300
1301 case TARGET_F_SETLK:
1302 case TARGET_F_SETLKW:
1303 fl.l_type = tswap16(target_fl->l_type);
1304 fl.l_whence = tswap16(target_fl->l_whence);
1305 fl.l_start = tswapl(target_fl->l_start);
1306 fl.l_len = tswapl(target_fl->l_len);
1307 fl.l_pid = tswapl(target_fl->l_pid);
1308 ret = fcntl(fd, cmd, &fl);
1309 break;
1310
1311 case TARGET_F_GETLK64:
1312 case TARGET_F_SETLK64:
1313 case TARGET_F_SETLKW64:
1314 ret = -1;
1315 errno = EINVAL;
1316 break;
1317
1318 default:
1319 ret = fcntl(fd, cmd, arg);
1320 break;
1321 }
1322 return ret;
1323}
1324
1325
b03c60f3
FB
1326#define high2lowuid(x) (x)
1327#define high2lowgid(x) (x)
1328#define low2highuid(x) (x)
1329#define low2highgid(x) (x)
1b6b029e 1330
31e31b8a
FB
1331void syscall_init(void)
1332{
2ab83ea7
FB
1333 IOCTLEntry *ie;
1334 const argtype *arg_type;
1335 int size;
1336
31e31b8a
FB
1337#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
1338#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
1339#include "syscall_types.h"
1340#undef STRUCT
1341#undef STRUCT_SPECIAL
2ab83ea7
FB
1342
1343 /* we patch the ioctl size if necessary. We rely on the fact that
1344 no ioctl has all the bits at '1' in the size field */
1345 ie = ioctl_entries;
1346 while (ie->target_cmd != 0) {
1347 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
1348 TARGET_IOC_SIZEMASK) {
1349 arg_type = ie->arg_type;
1350 if (arg_type[0] != TYPE_PTR) {
1351 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
1352 ie->target_cmd);
1353 exit(1);
1354 }
1355 arg_type++;
1356 size = thunk_type_size(arg_type, 0);
1357 ie->target_cmd = (ie->target_cmd &
1358 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
1359 (size << TARGET_IOC_SIZESHIFT);
1360 }
1361 /* automatic consistency check if same arch */
1362#if defined(__i386__) && defined(TARGET_I386)
1363 if (ie->target_cmd != ie->host_cmd) {
1364 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
1365 ie->target_cmd, ie->host_cmd);
1366 }
1367#endif
1368 ie++;
1369 }
31e31b8a
FB
1370}
1371
6dbad63e 1372long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
31e31b8a
FB
1373 long arg4, long arg5, long arg6)
1374{
1375 long ret;
1376 struct stat st;
72f03900 1377 struct kernel_statfs *stfs;
31e31b8a 1378
72f03900
FB
1379#ifdef DEBUG
1380 gemu_log("syscall %d\n", num);
1381#endif
31e31b8a
FB
1382 switch(num) {
1383 case TARGET_NR_exit:
7d13299d
FB
1384#ifdef HAVE_GPROF
1385 _mcleanup();
1386#endif
1b6b029e 1387 /* XXX: should free thread stack and CPU env */
31e31b8a
FB
1388 _exit(arg1);
1389 ret = 0; /* avoid warning */
1390 break;
1391 case TARGET_NR_read:
206f0fa7 1392 page_unprotect_range((void *)arg2, arg3);
31e31b8a
FB
1393 ret = get_errno(read(arg1, (void *)arg2, arg3));
1394 break;
1395 case TARGET_NR_write:
1396 ret = get_errno(write(arg1, (void *)arg2, arg3));
1397 break;
1398 case TARGET_NR_open:
ec86b0fb 1399 ret = get_errno(open(path((const char *)arg1), arg2, arg3));
31e31b8a
FB
1400 break;
1401 case TARGET_NR_close:
1402 ret = get_errno(close(arg1));
1403 break;
1404 case TARGET_NR_brk:
1405 ret = do_brk((char *)arg1);
1406 break;
1407 case TARGET_NR_fork:
1b6b029e 1408 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
31e31b8a
FB
1409 break;
1410 case TARGET_NR_waitpid:
1411 {
1412 int *status = (int *)arg2;
1413 ret = get_errno(waitpid(arg1, status, arg3));
1414 if (!is_error(ret) && status)
1415 tswapls((long *)&status);
1416 }
1417 break;
1418 case TARGET_NR_creat:
1419 ret = get_errno(creat((const char *)arg1, arg2));
1420 break;
1421 case TARGET_NR_link:
1422 ret = get_errno(link((const char *)arg1, (const char *)arg2));
1423 break;
1424 case TARGET_NR_unlink:
1425 ret = get_errno(unlink((const char *)arg1));
1426 break;
1427 case TARGET_NR_execve:
7854b056
FB
1428 {
1429 char **argp, **envp;
f7341ff4 1430 int argc, envc;
7854b056
FB
1431 uint32_t *p;
1432 char **q;
1433
f7341ff4 1434 argc = 0;
7854b056
FB
1435 for (p = (void *)arg2; *p; p++)
1436 argc++;
f7341ff4 1437 envc = 0;
7854b056
FB
1438 for (p = (void *)arg3; *p; p++)
1439 envc++;
1440
f7341ff4
FB
1441 argp = alloca((argc + 1) * sizeof(void *));
1442 envp = alloca((envc + 1) * sizeof(void *));
7854b056
FB
1443
1444 for (p = (void *)arg2, q = argp; *p; p++, q++)
1445 *q = (void *)tswap32(*p);
f7341ff4
FB
1446 *q = NULL;
1447
7854b056
FB
1448 for (p = (void *)arg3, q = envp; *p; p++, q++)
1449 *q = (void *)tswap32(*p);
f7341ff4 1450 *q = NULL;
7854b056
FB
1451
1452 ret = get_errno(execve((const char *)arg1, argp, envp));
1453 }
31e31b8a
FB
1454 break;
1455 case TARGET_NR_chdir:
1456 ret = get_errno(chdir((const char *)arg1));
1457 break;
1458 case TARGET_NR_time:
1459 {
1460 int *time_ptr = (int *)arg1;
1461 ret = get_errno(time((time_t *)time_ptr));
1462 if (!is_error(ret) && time_ptr)
1463 tswap32s(time_ptr);
1464 }
1465 break;
1466 case TARGET_NR_mknod:
1467 ret = get_errno(mknod((const char *)arg1, arg2, arg3));
1468 break;
1469 case TARGET_NR_chmod:
1470 ret = get_errno(chmod((const char *)arg1, arg2));
1471 break;
1472 case TARGET_NR_lchown:
1473 ret = get_errno(chown((const char *)arg1, arg2, arg3));
1474 break;
1475 case TARGET_NR_break:
1476 goto unimplemented;
1477 case TARGET_NR_oldstat:
1478 goto unimplemented;
1479 case TARGET_NR_lseek:
1480 ret = get_errno(lseek(arg1, arg2, arg3));
1481 break;
1482 case TARGET_NR_getpid:
1483 ret = get_errno(getpid());
1484 break;
1485 case TARGET_NR_mount:
1486 /* need to look at the data field */
1487 goto unimplemented;
1488 case TARGET_NR_umount:
1489 ret = get_errno(umount((const char *)arg1));
1490 break;
1491 case TARGET_NR_setuid:
b03c60f3 1492 ret = get_errno(setuid(low2highuid(arg1)));
31e31b8a
FB
1493 break;
1494 case TARGET_NR_getuid:
1495 ret = get_errno(getuid());
1496 break;
1497 case TARGET_NR_stime:
1498 {
1499 int *time_ptr = (int *)arg1;
1500 if (time_ptr)
1501 tswap32s(time_ptr);
1502 ret = get_errno(stime((time_t *)time_ptr));
1503 }
1504 break;
1505 case TARGET_NR_ptrace:
1506 goto unimplemented;
1507 case TARGET_NR_alarm:
1508 ret = alarm(arg1);
1509 break;
1510 case TARGET_NR_oldfstat:
1511 goto unimplemented;
1512 case TARGET_NR_pause:
1513 ret = get_errno(pause());
1514 break;
1515 case TARGET_NR_utime:
1516 goto unimplemented;
1517 case TARGET_NR_stty:
1518 goto unimplemented;
1519 case TARGET_NR_gtty:
1520 goto unimplemented;
1521 case TARGET_NR_access:
1522 ret = get_errno(access((const char *)arg1, arg2));
1523 break;
1524 case TARGET_NR_nice:
1525 ret = get_errno(nice(arg1));
1526 break;
1527 case TARGET_NR_ftime:
1528 goto unimplemented;
1529 case TARGET_NR_sync:
04369ff2
FB
1530 sync();
1531 ret = 0;
31e31b8a
FB
1532 break;
1533 case TARGET_NR_kill:
1534 ret = get_errno(kill(arg1, arg2));
1535 break;
1536 case TARGET_NR_rename:
1537 ret = get_errno(rename((const char *)arg1, (const char *)arg2));
1538 break;
1539 case TARGET_NR_mkdir:
1540 ret = get_errno(mkdir((const char *)arg1, arg2));
1541 break;
1542 case TARGET_NR_rmdir:
1543 ret = get_errno(rmdir((const char *)arg1));
1544 break;
1545 case TARGET_NR_dup:
1546 ret = get_errno(dup(arg1));
1547 break;
1548 case TARGET_NR_pipe:
1549 {
1550 int *pipe_ptr = (int *)arg1;
1551 ret = get_errno(pipe(pipe_ptr));
1552 if (!is_error(ret)) {
1553 tswap32s(&pipe_ptr[0]);
1554 tswap32s(&pipe_ptr[1]);
1555 }
1556 }
1557 break;
1558 case TARGET_NR_times:
32f36bce
FB
1559 {
1560 struct target_tms *tmsp = (void *)arg1;
1561 struct tms tms;
1562 ret = get_errno(times(&tms));
1563 if (tmsp) {
c596ed17
FB
1564 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
1565 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
1566 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
1567 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
32f36bce 1568 }
c596ed17
FB
1569 if (!is_error(ret))
1570 ret = host_to_target_clock_t(ret);
32f36bce
FB
1571 }
1572 break;
31e31b8a
FB
1573 case TARGET_NR_prof:
1574 goto unimplemented;
1575 case TARGET_NR_setgid:
b03c60f3 1576 ret = get_errno(setgid(low2highgid(arg1)));
31e31b8a
FB
1577 break;
1578 case TARGET_NR_getgid:
1579 ret = get_errno(getgid());
1580 break;
1581 case TARGET_NR_signal:
1582 goto unimplemented;
1583 case TARGET_NR_geteuid:
1584 ret = get_errno(geteuid());
1585 break;
1586 case TARGET_NR_getegid:
1587 ret = get_errno(getegid());
1588 break;
1589 case TARGET_NR_acct:
1590 goto unimplemented;
1591 case TARGET_NR_umount2:
1592 ret = get_errno(umount2((const char *)arg1, arg2));
1593 break;
1594 case TARGET_NR_lock:
1595 goto unimplemented;
1596 case TARGET_NR_ioctl:
1597 ret = do_ioctl(arg1, arg2, arg3);
1598 break;
1599 case TARGET_NR_fcntl:
7775e9ec 1600 ret = get_errno(do_fcntl(arg1, arg2, arg3));
31e31b8a
FB
1601 break;
1602 case TARGET_NR_mpx:
1603 goto unimplemented;
1604 case TARGET_NR_setpgid:
1605 ret = get_errno(setpgid(arg1, arg2));
1606 break;
1607 case TARGET_NR_ulimit:
1608 goto unimplemented;
1609 case TARGET_NR_oldolduname:
1610 goto unimplemented;
1611 case TARGET_NR_umask:
1612 ret = get_errno(umask(arg1));
1613 break;
1614 case TARGET_NR_chroot:
1615 ret = get_errno(chroot((const char *)arg1));
1616 break;
1617 case TARGET_NR_ustat:
1618 goto unimplemented;
1619 case TARGET_NR_dup2:
1620 ret = get_errno(dup2(arg1, arg2));
1621 break;
1622 case TARGET_NR_getppid:
1623 ret = get_errno(getppid());
1624 break;
1625 case TARGET_NR_getpgrp:
1626 ret = get_errno(getpgrp());
1627 break;
1628 case TARGET_NR_setsid:
1629 ret = get_errno(setsid());
1630 break;
1631 case TARGET_NR_sigaction:
31e31b8a 1632 {
66fb9763
FB
1633 struct target_old_sigaction *old_act = (void *)arg2;
1634 struct target_old_sigaction *old_oact = (void *)arg3;
1635 struct target_sigaction act, oact, *pact;
1636 if (old_act) {
1637 act._sa_handler = old_act->_sa_handler;
1638 target_siginitset(&act.sa_mask, old_act->sa_mask);
1639 act.sa_flags = old_act->sa_flags;
1640 act.sa_restorer = old_act->sa_restorer;
1641 pact = &act;
1642 } else {
1643 pact = NULL;
1644 }
1645 ret = get_errno(do_sigaction(arg1, pact, &oact));
1646 if (!is_error(ret) && old_oact) {
1647 old_oact->_sa_handler = oact._sa_handler;
1648 old_oact->sa_mask = oact.sa_mask.sig[0];
1649 old_oact->sa_flags = oact.sa_flags;
1650 old_oact->sa_restorer = oact.sa_restorer;
1651 }
31e31b8a
FB
1652 }
1653 break;
66fb9763
FB
1654 case TARGET_NR_rt_sigaction:
1655 ret = get_errno(do_sigaction(arg1, (void *)arg2, (void *)arg3));
1656 break;
31e31b8a 1657 case TARGET_NR_sgetmask:
66fb9763
FB
1658 {
1659 sigset_t cur_set;
1660 target_ulong target_set;
1661 sigprocmask(0, NULL, &cur_set);
1662 host_to_target_old_sigset(&target_set, &cur_set);
1663 ret = target_set;
1664 }
1665 break;
31e31b8a 1666 case TARGET_NR_ssetmask:
66fb9763
FB
1667 {
1668 sigset_t set, oset, cur_set;
1669 target_ulong target_set = arg1;
1670 sigprocmask(0, NULL, &cur_set);
1671 target_to_host_old_sigset(&set, &target_set);
1672 sigorset(&set, &set, &cur_set);
1673 sigprocmask(SIG_SETMASK, &set, &oset);
1674 host_to_target_old_sigset(&target_set, &oset);
1675 ret = target_set;
1676 }
1677 break;
1678 case TARGET_NR_sigprocmask:
1679 {
1680 int how = arg1;
1681 sigset_t set, oldset, *set_ptr;
1682 target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
1683
1684 if (pset) {
1685 switch(how) {
1686 case TARGET_SIG_BLOCK:
1687 how = SIG_BLOCK;
1688 break;
1689 case TARGET_SIG_UNBLOCK:
1690 how = SIG_UNBLOCK;
1691 break;
1692 case TARGET_SIG_SETMASK:
1693 how = SIG_SETMASK;
1694 break;
1695 default:
1696 ret = -EINVAL;
1697 goto fail;
1698 }
1699 target_to_host_old_sigset(&set, pset);
1700 set_ptr = &set;
1701 } else {
1702 how = 0;
1703 set_ptr = NULL;
1704 }
1705 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
1706 if (!is_error(ret) && poldset) {
1707 host_to_target_old_sigset(poldset, &oldset);
1708 }
1709 }
1710 break;
1711 case TARGET_NR_rt_sigprocmask:
1712 {
1713 int how = arg1;
1714 sigset_t set, oldset, *set_ptr;
1715 target_sigset_t *pset = (void *)arg2;
1716 target_sigset_t *poldset = (void *)arg3;
1717
1718 if (pset) {
1719 switch(how) {
1720 case TARGET_SIG_BLOCK:
1721 how = SIG_BLOCK;
1722 break;
1723 case TARGET_SIG_UNBLOCK:
1724 how = SIG_UNBLOCK;
1725 break;
1726 case TARGET_SIG_SETMASK:
1727 how = SIG_SETMASK;
1728 break;
1729 default:
1730 ret = -EINVAL;
1731 goto fail;
1732 }
1733 target_to_host_sigset(&set, pset);
1734 set_ptr = &set;
1735 } else {
1736 how = 0;
1737 set_ptr = NULL;
1738 }
1739 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
1740 if (!is_error(ret) && poldset) {
1741 host_to_target_sigset(poldset, &oldset);
1742 }
1743 }
1744 break;
1745 case TARGET_NR_sigpending:
1746 {
1747 sigset_t set;
1748 ret = get_errno(sigpending(&set));
1749 if (!is_error(ret)) {
1750 host_to_target_old_sigset((target_ulong *)arg1, &set);
1751 }
1752 }
1753 break;
1754 case TARGET_NR_rt_sigpending:
1755 {
1756 sigset_t set;
1757 ret = get_errno(sigpending(&set));
1758 if (!is_error(ret)) {
1759 host_to_target_sigset((target_sigset_t *)arg1, &set);
1760 }
1761 }
1762 break;
1763 case TARGET_NR_sigsuspend:
1764 {
1765 sigset_t set;
1766 target_to_host_old_sigset(&set, (target_ulong *)arg1);
1767 ret = get_errno(sigsuspend(&set));
1768 }
1769 break;
1770 case TARGET_NR_rt_sigsuspend:
1771 {
1772 sigset_t set;
1773 target_to_host_sigset(&set, (target_sigset_t *)arg1);
1774 ret = get_errno(sigsuspend(&set));
1775 }
1776 break;
1777 case TARGET_NR_rt_sigtimedwait:
1778 {
1779 target_sigset_t *target_set = (void *)arg1;
1780 target_siginfo_t *target_uinfo = (void *)arg2;
1781 struct target_timespec *target_uts = (void *)arg3;
1782 sigset_t set;
1783 struct timespec uts, *puts;
1784 siginfo_t uinfo;
1785
1786 target_to_host_sigset(&set, target_set);
1787 if (target_uts) {
1788 puts = &uts;
1789 puts->tv_sec = tswapl(target_uts->tv_sec);
1790 puts->tv_nsec = tswapl(target_uts->tv_nsec);
1791 } else {
1792 puts = NULL;
1793 }
1794 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
1795 if (!is_error(ret) && target_uinfo) {
1796 host_to_target_siginfo(target_uinfo, &uinfo);
1797 }
1798 }
1799 break;
1800 case TARGET_NR_rt_sigqueueinfo:
1801 {
1802 siginfo_t uinfo;
1803 target_to_host_siginfo(&uinfo, (target_siginfo_t *)arg3);
1804 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
1805 }
1806 break;
1807 case TARGET_NR_sigreturn:
1808 /* NOTE: ret is eax, so not transcoding must be done */
1809 ret = do_sigreturn(cpu_env);
1810 break;
1811 case TARGET_NR_rt_sigreturn:
1812 /* NOTE: ret is eax, so not transcoding must be done */
1813 ret = do_rt_sigreturn(cpu_env);
1814 break;
31e31b8a
FB
1815 case TARGET_NR_setreuid:
1816 ret = get_errno(setreuid(arg1, arg2));
1817 break;
1818 case TARGET_NR_setregid:
1819 ret = get_errno(setregid(arg1, arg2));
1820 break;
31e31b8a
FB
1821 case TARGET_NR_sethostname:
1822 ret = get_errno(sethostname((const char *)arg1, arg2));
1823 break;
1824 case TARGET_NR_setrlimit:
9de5e440
FB
1825 {
1826 /* XXX: convert resource ? */
1827 int resource = arg1;
1828 struct target_rlimit *target_rlim = (void *)arg2;
1829 struct rlimit rlim;
1830 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
1831 rlim.rlim_max = tswapl(target_rlim->rlim_max);
1832 ret = get_errno(setrlimit(resource, &rlim));
1833 }
1834 break;
31e31b8a 1835 case TARGET_NR_getrlimit:
9de5e440
FB
1836 {
1837 /* XXX: convert resource ? */
1838 int resource = arg1;
1839 struct target_rlimit *target_rlim = (void *)arg2;
1840 struct rlimit rlim;
1841
1842 ret = get_errno(getrlimit(resource, &rlim));
1843 if (!is_error(ret)) {
1844 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
1845 target_rlim->rlim_max = tswapl(rlim.rlim_max);
1846 }
1847 }
1848 break;
31e31b8a 1849 case TARGET_NR_getrusage:
b409186b
FB
1850 {
1851 struct rusage rusage;
1852 struct target_rusage *target_rusage = (void *)arg2;
1853 ret = get_errno(getrusage(arg1, &rusage));
1854 if (!is_error(ret)) {
1855 host_to_target_rusage(target_rusage, &rusage);
1856 }
1857 }
1858 break;
31e31b8a
FB
1859 case TARGET_NR_gettimeofday:
1860 {
1861 struct target_timeval *target_tv = (void *)arg1;
1862 struct timeval tv;
1863 ret = get_errno(gettimeofday(&tv, NULL));
1864 if (!is_error(ret)) {
5cd4393b 1865 host_to_target_timeval(target_tv, &tv);
31e31b8a
FB
1866 }
1867 }
1868 break;
1869 case TARGET_NR_settimeofday:
1870 {
1871 struct target_timeval *target_tv = (void *)arg1;
1872 struct timeval tv;
5cd4393b 1873 target_to_host_timeval(&tv, target_tv);
31e31b8a
FB
1874 ret = get_errno(settimeofday(&tv, NULL));
1875 }
1876 break;
1877 case TARGET_NR_getgroups:
19b84f3c
FB
1878 {
1879 int gidsetsize = arg1;
1880 uint16_t *target_grouplist = (void *)arg2;
1881 gid_t *grouplist;
1882 int i;
1883
1884 grouplist = alloca(gidsetsize * sizeof(gid_t));
1885 ret = get_errno(getgroups(gidsetsize, grouplist));
1886 if (!is_error(ret)) {
1887 for(i = 0;i < gidsetsize; i++)
1888 target_grouplist[i] = tswap16(grouplist[i]);
1889 }
1890 }
1891 break;
31e31b8a 1892 case TARGET_NR_setgroups:
19b84f3c
FB
1893 {
1894 int gidsetsize = arg1;
1895 uint16_t *target_grouplist = (void *)arg2;
1896 gid_t *grouplist;
1897 int i;
1898
1899 grouplist = alloca(gidsetsize * sizeof(gid_t));
1900 for(i = 0;i < gidsetsize; i++)
1901 grouplist[i] = tswap16(target_grouplist[i]);
1902 ret = get_errno(setgroups(gidsetsize, grouplist));
1903 }
1904 break;
31e31b8a 1905 case TARGET_NR_select:
f2674e31
FB
1906 {
1907 struct target_sel_arg_struct *sel = (void *)arg1;
1908 sel->n = tswapl(sel->n);
1909 sel->inp = tswapl(sel->inp);
1910 sel->outp = tswapl(sel->outp);
1911 sel->exp = tswapl(sel->exp);
1912 sel->tvp = tswapl(sel->tvp);
1913 ret = do_select(sel->n, (void *)sel->inp, (void *)sel->outp,
1914 (void *)sel->exp, (void *)sel->tvp);
1915 }
1916 break;
31e31b8a
FB
1917 case TARGET_NR_symlink:
1918 ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
1919 break;
1920 case TARGET_NR_oldlstat:
1921 goto unimplemented;
1922 case TARGET_NR_readlink:
ec86b0fb 1923 ret = get_errno(readlink(path((const char *)arg1), (char *)arg2, arg3));
31e31b8a
FB
1924 break;
1925 case TARGET_NR_uselib:
1926 goto unimplemented;
1927 case TARGET_NR_swapon:
1928 ret = get_errno(swapon((const char *)arg1, arg2));
1929 break;
1930 case TARGET_NR_reboot:
1931 goto unimplemented;
1932 case TARGET_NR_readdir:
1933 goto unimplemented;
31e31b8a 1934 case TARGET_NR_mmap:
6fb883e8 1935#if defined(TARGET_I386) || defined(TARGET_ARM)
31e31b8a
FB
1936 {
1937 uint32_t v1, v2, v3, v4, v5, v6, *vptr;
1938 vptr = (uint32_t *)arg1;
1939 v1 = tswap32(vptr[0]);
1940 v2 = tswap32(vptr[1]);
1941 v3 = tswap32(vptr[2]);
1942 v4 = tswap32(vptr[3]);
1943 v5 = tswap32(vptr[4]);
1944 v6 = tswap32(vptr[5]);
5286db75
FB
1945 ret = get_errno(target_mmap(v1, v2, v3,
1946 target_to_host_bitmask(v4, mmap_flags_tbl),
1947 v5, v6));
31e31b8a 1948 }
31e31b8a 1949#else
6fb883e8
FB
1950 ret = get_errno(target_mmap(arg1, arg2, arg3,
1951 target_to_host_bitmask(arg4, mmap_flags_tbl),
1952 arg5,
1953 arg6));
31e31b8a 1954#endif
6fb883e8
FB
1955 break;
1956 case TARGET_NR_mmap2:
5286db75
FB
1957 ret = get_errno(target_mmap(arg1, arg2, arg3,
1958 target_to_host_bitmask(arg4, mmap_flags_tbl),
1959 arg5,
14ae3ba7 1960 arg6 << TARGET_PAGE_BITS));
31e31b8a
FB
1961 break;
1962 case TARGET_NR_munmap:
54936004 1963 ret = get_errno(target_munmap(arg1, arg2));
31e31b8a 1964 break;
9de5e440 1965 case TARGET_NR_mprotect:
54936004 1966 ret = get_errno(target_mprotect(arg1, arg2, arg3));
9de5e440
FB
1967 break;
1968 case TARGET_NR_mremap:
54936004 1969 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
9de5e440
FB
1970 break;
1971 case TARGET_NR_msync:
1972 ret = get_errno(msync((void *)arg1, arg2, arg3));
1973 break;
1974 case TARGET_NR_mlock:
1975 ret = get_errno(mlock((void *)arg1, arg2));
1976 break;
1977 case TARGET_NR_munlock:
1978 ret = get_errno(munlock((void *)arg1, arg2));
1979 break;
1980 case TARGET_NR_mlockall:
1981 ret = get_errno(mlockall(arg1));
1982 break;
1983 case TARGET_NR_munlockall:
1984 ret = get_errno(munlockall());
1985 break;
31e31b8a
FB
1986 case TARGET_NR_truncate:
1987 ret = get_errno(truncate((const char *)arg1, arg2));
1988 break;
1989 case TARGET_NR_ftruncate:
1990 ret = get_errno(ftruncate(arg1, arg2));
1991 break;
1992 case TARGET_NR_fchmod:
1993 ret = get_errno(fchmod(arg1, arg2));
1994 break;
1995 case TARGET_NR_fchown:
1996 ret = get_errno(fchown(arg1, arg2, arg3));
1997 break;
1998 case TARGET_NR_getpriority:
1999 ret = get_errno(getpriority(arg1, arg2));
2000 break;
2001 case TARGET_NR_setpriority:
2002 ret = get_errno(setpriority(arg1, arg2, arg3));
2003 break;
2004 case TARGET_NR_profil:
2005 goto unimplemented;
2006 case TARGET_NR_statfs:
2007 stfs = (void *)arg2;
ec86b0fb 2008 ret = get_errno(sys_statfs(path((const char *)arg1), stfs));
31e31b8a
FB
2009 convert_statfs:
2010 if (!is_error(ret)) {
2011 tswap32s(&stfs->f_type);
2012 tswap32s(&stfs->f_bsize);
2013 tswap32s(&stfs->f_blocks);
2014 tswap32s(&stfs->f_bfree);
2015 tswap32s(&stfs->f_bavail);
2016 tswap32s(&stfs->f_files);
2017 tswap32s(&stfs->f_ffree);
2018 tswap32s(&stfs->f_fsid.val[0]);
2019 tswap32s(&stfs->f_fsid.val[1]);
2020 tswap32s(&stfs->f_namelen);
2021 }
2022 break;
2023 case TARGET_NR_fstatfs:
2024 stfs = (void *)arg2;
2025 ret = get_errno(sys_fstatfs(arg1, stfs));
2026 goto convert_statfs;
2027 case TARGET_NR_ioperm:
2028 goto unimplemented;
2029 case TARGET_NR_socketcall:
7854b056 2030 ret = do_socketcall(arg1, (int32_t *)arg2);
31e31b8a
FB
2031 break;
2032 case TARGET_NR_syslog:
2033 goto unimplemented;
2034 case TARGET_NR_setitimer:
66fb9763
FB
2035 {
2036 struct target_itimerval *target_value = (void *)arg2;
2037 struct target_itimerval *target_ovalue = (void *)arg3;
2038 struct itimerval value, ovalue, *pvalue;
2039
2040 if (target_value) {
2041 pvalue = &value;
2042 target_to_host_timeval(&pvalue->it_interval,
2043 &target_value->it_interval);
2044 target_to_host_timeval(&pvalue->it_value,
2045 &target_value->it_value);
2046 } else {
2047 pvalue = NULL;
2048 }
2049 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
2050 if (!is_error(ret) && target_ovalue) {
2051 host_to_target_timeval(&target_ovalue->it_interval,
2052 &ovalue.it_interval);
2053 host_to_target_timeval(&target_ovalue->it_value,
2054 &ovalue.it_value);
2055 }
2056 }
2057 break;
31e31b8a 2058 case TARGET_NR_getitimer:
66fb9763
FB
2059 {
2060 struct target_itimerval *target_value = (void *)arg2;
2061 struct itimerval value;
2062
2063 ret = get_errno(getitimer(arg1, &value));
2064 if (!is_error(ret) && target_value) {
2065 host_to_target_timeval(&target_value->it_interval,
2066 &value.it_interval);
2067 host_to_target_timeval(&target_value->it_value,
2068 &value.it_value);
2069 }
2070 }
2071 break;
31e31b8a 2072 case TARGET_NR_stat:
ec86b0fb 2073 ret = get_errno(stat(path((const char *)arg1), &st));
31e31b8a
FB
2074 goto do_stat;
2075 case TARGET_NR_lstat:
ec86b0fb 2076 ret = get_errno(lstat(path((const char *)arg1), &st));
31e31b8a
FB
2077 goto do_stat;
2078 case TARGET_NR_fstat:
2079 {
2080 ret = get_errno(fstat(arg1, &st));
2081 do_stat:
2082 if (!is_error(ret)) {
2083 struct target_stat *target_st = (void *)arg2;
2084 target_st->st_dev = tswap16(st.st_dev);
2085 target_st->st_ino = tswapl(st.st_ino);
ec86b0fb 2086 target_st->st_mode = tswap16(st.st_mode);
31e31b8a
FB
2087 target_st->st_nlink = tswap16(st.st_nlink);
2088 target_st->st_uid = tswap16(st.st_uid);
2089 target_st->st_gid = tswap16(st.st_gid);
2090 target_st->st_rdev = tswap16(st.st_rdev);
2091 target_st->st_size = tswapl(st.st_size);
2092 target_st->st_blksize = tswapl(st.st_blksize);
2093 target_st->st_blocks = tswapl(st.st_blocks);
7854b056
FB
2094 target_st->target_st_atime = tswapl(st.st_atime);
2095 target_st->target_st_mtime = tswapl(st.st_mtime);
2096 target_st->target_st_ctime = tswapl(st.st_ctime);
31e31b8a
FB
2097 }
2098 }
2099 break;
2100 case TARGET_NR_olduname:
2101 goto unimplemented;
2102 case TARGET_NR_iopl:
2103 goto unimplemented;
2104 case TARGET_NR_vhangup:
2105 ret = get_errno(vhangup());
2106 break;
2107 case TARGET_NR_idle:
2108 goto unimplemented;
31e31b8a
FB
2109 case TARGET_NR_wait4:
2110 {
2111 int status;
2112 target_long *status_ptr = (void *)arg2;
2113 struct rusage rusage, *rusage_ptr;
2114 struct target_rusage *target_rusage = (void *)arg4;
2115 if (target_rusage)
2116 rusage_ptr = &rusage;
2117 else
2118 rusage_ptr = NULL;
2119 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
2120 if (!is_error(ret)) {
2121 if (status_ptr)
2122 *status_ptr = tswap32(status);
2123 if (target_rusage) {
b409186b 2124 host_to_target_rusage(target_rusage, &rusage);
31e31b8a
FB
2125 }
2126 }
2127 }
2128 break;
2129 case TARGET_NR_swapoff:
2130 ret = get_errno(swapoff((const char *)arg1));
2131 break;
2132 case TARGET_NR_sysinfo:
2133 goto unimplemented;
2134 case TARGET_NR_ipc:
2135 goto unimplemented;
2136 case TARGET_NR_fsync:
2137 ret = get_errno(fsync(arg1));
2138 break;
31e31b8a 2139 case TARGET_NR_clone:
1b6b029e
FB
2140 ret = get_errno(do_fork(cpu_env, arg1, arg2));
2141 break;
ec86b0fb
FB
2142#ifdef __NR_exit_group
2143 /* new thread calls */
2144 case TARGET_NR_exit_group:
2145 ret = get_errno(exit_group(arg1));
2146 break;
2147#endif
31e31b8a
FB
2148 case TARGET_NR_setdomainname:
2149 ret = get_errno(setdomainname((const char *)arg1, arg2));
2150 break;
2151 case TARGET_NR_uname:
2152 /* no need to transcode because we use the linux syscall */
2153 ret = get_errno(sys_uname((struct new_utsname *)arg1));
2154 break;
6dbad63e 2155#ifdef TARGET_I386
31e31b8a 2156 case TARGET_NR_modify_ldt:
5cd4393b
FB
2157 ret = get_errno(do_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
2158 break;
2159 case TARGET_NR_vm86old:
2160 goto unimplemented;
2161 case TARGET_NR_vm86:
2162 ret = do_vm86(cpu_env, arg1, (void *)arg2);
6dbad63e
FB
2163 break;
2164#endif
31e31b8a
FB
2165 case TARGET_NR_adjtimex:
2166 goto unimplemented;
31e31b8a
FB
2167 case TARGET_NR_create_module:
2168 case TARGET_NR_init_module:
2169 case TARGET_NR_delete_module:
2170 case TARGET_NR_get_kernel_syms:
2171 goto unimplemented;
2172 case TARGET_NR_quotactl:
2173 goto unimplemented;
2174 case TARGET_NR_getpgid:
2175 ret = get_errno(getpgid(arg1));
2176 break;
2177 case TARGET_NR_fchdir:
2178 ret = get_errno(fchdir(arg1));
2179 break;
2180 case TARGET_NR_bdflush:
2181 goto unimplemented;
2182 case TARGET_NR_sysfs:
2183 goto unimplemented;
2184 case TARGET_NR_personality:
1b6b029e 2185 ret = get_errno(personality(arg1));
31e31b8a
FB
2186 break;
2187 case TARGET_NR_afs_syscall:
2188 goto unimplemented;
2189 case TARGET_NR_setfsuid:
9de5e440
FB
2190 ret = get_errno(setfsuid(arg1));
2191 break;
31e31b8a 2192 case TARGET_NR_setfsgid:
9de5e440
FB
2193 ret = get_errno(setfsgid(arg1));
2194 break;
31e31b8a
FB
2195 case TARGET_NR__llseek:
2196 {
2197 int64_t res;
2198 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
2199 *(int64_t *)arg4 = tswap64(res);
2200 }
2201 break;
2202 case TARGET_NR_getdents:
2203#if TARGET_LONG_SIZE != 4
2204#error not supported
4add45b4
FB
2205#elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
2206 {
2207 struct target_dirent *target_dirp = (void *)arg2;
2208 struct dirent *dirp;
2209 long count = arg3;
2210
2211 dirp = malloc(count);
2212 if (!dirp)
2213 return -ENOMEM;
2214
2215 ret = get_errno(sys_getdents(arg1, dirp, count));
2216 if (!is_error(ret)) {
2217 struct dirent *de;
2218 struct target_dirent *tde;
2219 int len = ret;
2220 int reclen, treclen;
2221 int count1, tnamelen;
2222
2223 count1 = 0;
2224 de = dirp;
2225 tde = target_dirp;
2226 while (len > 0) {
2227 reclen = de->d_reclen;
2228 treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
2229 tde->d_reclen = tswap16(treclen);
2230 tde->d_ino = tswapl(de->d_ino);
2231 tde->d_off = tswapl(de->d_off);
2232 tnamelen = treclen - (2 * sizeof(target_long) + 2);
2233 if (tnamelen > 256)
2234 tnamelen = 256;
2235 strncpy(tde->d_name, de->d_name, tnamelen);
2236 de = (struct dirent *)((char *)de + reclen);
2237 len -= reclen;
2238 tde = (struct dirent *)((char *)tde + treclen);
2239 count1 += treclen;
2240 }
2241 ret = count1;
2242 }
2243 free(dirp);
2244 }
2245#else
31e31b8a
FB
2246 {
2247 struct dirent *dirp = (void *)arg2;
2248 long count = arg3;
dab2ed99 2249
72f03900 2250 ret = get_errno(sys_getdents(arg1, dirp, count));
31e31b8a
FB
2251 if (!is_error(ret)) {
2252 struct dirent *de;
2253 int len = ret;
2254 int reclen;
2255 de = dirp;
2256 while (len > 0) {
8083a3e5 2257 reclen = de->d_reclen;
31e31b8a
FB
2258 if (reclen > len)
2259 break;
8083a3e5 2260 de->d_reclen = tswap16(reclen);
31e31b8a
FB
2261 tswapls(&de->d_ino);
2262 tswapls(&de->d_off);
2263 de = (struct dirent *)((char *)de + reclen);
2264 len -= reclen;
2265 }
2266 }
2267 }
4add45b4 2268#endif
31e31b8a 2269 break;
dab2ed99
FB
2270 case TARGET_NR_getdents64:
2271 {
2272 struct dirent64 *dirp = (void *)arg2;
2273 long count = arg3;
2274 ret = get_errno(sys_getdents64(arg1, dirp, count));
2275 if (!is_error(ret)) {
2276 struct dirent64 *de;
2277 int len = ret;
2278 int reclen;
2279 de = dirp;
2280 while (len > 0) {
8083a3e5 2281 reclen = de->d_reclen;
dab2ed99
FB
2282 if (reclen > len)
2283 break;
8083a3e5 2284 de->d_reclen = tswap16(reclen);
dab2ed99
FB
2285 tswap64s(&de->d_ino);
2286 tswap64s(&de->d_off);
2287 de = (struct dirent64 *)((char *)de + reclen);
2288 len -= reclen;
2289 }
2290 }
2291 }
2292 break;
31e31b8a
FB
2293 case TARGET_NR__newselect:
2294 ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4,
2295 (void *)arg5);
2296 break;
9de5e440
FB
2297 case TARGET_NR_poll:
2298 {
2299 struct target_pollfd *target_pfd = (void *)arg1;
2300 unsigned int nfds = arg2;
2301 int timeout = arg3;
2302 struct pollfd *pfd;
7854b056 2303 unsigned int i;
9de5e440
FB
2304
2305 pfd = alloca(sizeof(struct pollfd) * nfds);
2306 for(i = 0; i < nfds; i++) {
5cd4393b
FB
2307 pfd[i].fd = tswap32(target_pfd[i].fd);
2308 pfd[i].events = tswap16(target_pfd[i].events);
9de5e440
FB
2309 }
2310 ret = get_errno(poll(pfd, nfds, timeout));
2311 if (!is_error(ret)) {
2312 for(i = 0; i < nfds; i++) {
5cd4393b 2313 target_pfd[i].revents = tswap16(pfd[i].revents);
9de5e440
FB
2314 }
2315 }
2316 }
2317 break;
31e31b8a 2318 case TARGET_NR_flock:
9de5e440
FB
2319 /* NOTE: the flock constant seems to be the same for every
2320 Linux platform */
2321 ret = get_errno(flock(arg1, arg2));
31e31b8a
FB
2322 break;
2323 case TARGET_NR_readv:
2324 {
2325 int count = arg3;
2326 int i;
2327 struct iovec *vec;
2328 struct target_iovec *target_vec = (void *)arg2;
2329
2330 vec = alloca(count * sizeof(struct iovec));
2331 for(i = 0;i < count; i++) {
2332 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2333 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2334 }
2335 ret = get_errno(readv(arg1, vec, count));
2336 }
2337 break;
2338 case TARGET_NR_writev:
2339 {
2340 int count = arg3;
2341 int i;
2342 struct iovec *vec;
2343 struct target_iovec *target_vec = (void *)arg2;
2344
2345 vec = alloca(count * sizeof(struct iovec));
2346 for(i = 0;i < count; i++) {
2347 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2348 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2349 }
2350 ret = get_errno(writev(arg1, vec, count));
2351 }
2352 break;
2353 case TARGET_NR_getsid:
2354 ret = get_errno(getsid(arg1));
2355 break;
2356 case TARGET_NR_fdatasync:
5cd4393b
FB
2357 ret = get_errno(fdatasync(arg1));
2358 break;
31e31b8a
FB
2359 case TARGET_NR__sysctl:
2360 goto unimplemented;
31e31b8a 2361 case TARGET_NR_sched_setparam:
5cd4393b
FB
2362 {
2363 struct sched_param *target_schp = (void *)arg2;
2364 struct sched_param schp;
2365 schp.sched_priority = tswap32(target_schp->sched_priority);
2366 ret = get_errno(sched_setparam(arg1, &schp));
2367 }
2368 break;
31e31b8a 2369 case TARGET_NR_sched_getparam:
5cd4393b
FB
2370 {
2371 struct sched_param *target_schp = (void *)arg2;
2372 struct sched_param schp;
2373 ret = get_errno(sched_getparam(arg1, &schp));
2374 if (!is_error(ret)) {
2375 target_schp->sched_priority = tswap32(schp.sched_priority);
2376 }
2377 }
2378 break;
31e31b8a 2379 case TARGET_NR_sched_setscheduler:
5cd4393b
FB
2380 {
2381 struct sched_param *target_schp = (void *)arg3;
2382 struct sched_param schp;
2383 schp.sched_priority = tswap32(target_schp->sched_priority);
2384 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
2385 }
2386 break;
31e31b8a 2387 case TARGET_NR_sched_getscheduler:
5cd4393b
FB
2388 ret = get_errno(sched_getscheduler(arg1));
2389 break;
31e31b8a
FB
2390 case TARGET_NR_sched_yield:
2391 ret = get_errno(sched_yield());
2392 break;
2393 case TARGET_NR_sched_get_priority_max:
5cd4393b
FB
2394 ret = get_errno(sched_get_priority_max(arg1));
2395 break;
31e31b8a 2396 case TARGET_NR_sched_get_priority_min:
5cd4393b
FB
2397 ret = get_errno(sched_get_priority_min(arg1));
2398 break;
31e31b8a 2399 case TARGET_NR_sched_rr_get_interval:
5cd4393b
FB
2400 {
2401 struct target_timespec *target_ts = (void *)arg2;
2402 struct timespec ts;
2403 ret = get_errno(sched_rr_get_interval(arg1, &ts));
2404 if (!is_error(ret)) {
2405 target_ts->tv_sec = tswapl(ts.tv_sec);
2406 target_ts->tv_nsec = tswapl(ts.tv_nsec);
2407 }
2408 }
2409 break;
31e31b8a 2410 case TARGET_NR_nanosleep:
1b6b029e
FB
2411 {
2412 struct target_timespec *target_req = (void *)arg1;
2413 struct target_timespec *target_rem = (void *)arg2;
2414 struct timespec req, rem;
2415 req.tv_sec = tswapl(target_req->tv_sec);
2416 req.tv_nsec = tswapl(target_req->tv_nsec);
2417 ret = get_errno(nanosleep(&req, &rem));
2418 if (target_rem) {
2419 target_rem->tv_sec = tswapl(rem.tv_sec);
2420 target_rem->tv_nsec = tswapl(rem.tv_nsec);
2421 }
2422 }
2423 break;
31e31b8a 2424 case TARGET_NR_setresuid:
b03c60f3
FB
2425 ret = get_errno(setresuid(low2highuid(arg1),
2426 low2highuid(arg2),
2427 low2highuid(arg3)));
2428 break;
31e31b8a 2429 case TARGET_NR_getresuid:
b03c60f3
FB
2430 {
2431 int ruid, euid, suid;
2432 ret = get_errno(getresuid(&ruid, &euid, &suid));
2433 if (!is_error(ret)) {
2434 *(uint16_t *)arg1 = tswap16(high2lowuid(ruid));
2435 *(uint16_t *)arg2 = tswap16(high2lowuid(euid));
2436 *(uint16_t *)arg3 = tswap16(high2lowuid(suid));
2437 }
2438 }
2439 break;
2440 case TARGET_NR_setresgid:
2441 ret = get_errno(setresgid(low2highgid(arg1),
2442 low2highgid(arg2),
2443 low2highgid(arg3)));
2444 break;
2445 case TARGET_NR_getresgid:
2446 {
2447 int rgid, egid, sgid;
2448 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2449 if (!is_error(ret)) {
2450 *(uint16_t *)arg1 = high2lowgid(tswap16(rgid));
2451 *(uint16_t *)arg2 = high2lowgid(tswap16(egid));
2452 *(uint16_t *)arg3 = high2lowgid(tswap16(sgid));
2453 }
2454 }
2455 break;
31e31b8a 2456 case TARGET_NR_query_module:
5cd4393b 2457 goto unimplemented;
31e31b8a 2458 case TARGET_NR_nfsservctl:
5cd4393b 2459 goto unimplemented;
31e31b8a 2460 case TARGET_NR_prctl:
5cd4393b 2461 goto unimplemented;
31e31b8a 2462 case TARGET_NR_pread:
206f0fa7
FB
2463 page_unprotect_range((void *)arg2, arg3);
2464 ret = get_errno(pread(arg1, (void *)arg2, arg3, arg4));
2465 break;
31e31b8a 2466 case TARGET_NR_pwrite:
206f0fa7
FB
2467 ret = get_errno(pwrite(arg1, (void *)arg2, arg3, arg4));
2468 break;
31e31b8a
FB
2469 case TARGET_NR_chown:
2470 ret = get_errno(chown((const char *)arg1, arg2, arg3));
2471 break;
2472 case TARGET_NR_getcwd:
72f03900 2473 ret = get_errno(sys_getcwd1((char *)arg1, arg2));
31e31b8a
FB
2474 break;
2475 case TARGET_NR_capget:
5cd4393b 2476 goto unimplemented;
31e31b8a 2477 case TARGET_NR_capset:
5cd4393b 2478 goto unimplemented;
31e31b8a 2479 case TARGET_NR_sigaltstack:
5cd4393b 2480 goto unimplemented;
31e31b8a 2481 case TARGET_NR_sendfile:
5cd4393b 2482 goto unimplemented;
31e31b8a 2483 case TARGET_NR_getpmsg:
5cd4393b 2484 goto unimplemented;
31e31b8a 2485 case TARGET_NR_putpmsg:
5cd4393b 2486 goto unimplemented;
31e31b8a 2487 case TARGET_NR_vfork:
1b6b029e 2488 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
31e31b8a
FB
2489 break;
2490 case TARGET_NR_ugetrlimit:
728584be
FB
2491 {
2492 struct rlimit rlim;
2493 ret = get_errno(getrlimit(arg1, &rlim));
2494 if (!is_error(ret)) {
2495 struct target_rlimit *target_rlim = (void *)arg2;
2496 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
2497 target_rlim->rlim_max = tswapl(rlim.rlim_max);
2498 }
2499 break;
2500 }
31e31b8a 2501 case TARGET_NR_truncate64:
5cd4393b 2502 goto unimplemented;
31e31b8a 2503 case TARGET_NR_ftruncate64:
60cd49d5 2504 goto unimplemented;
31e31b8a 2505 case TARGET_NR_stat64:
ec86b0fb 2506 ret = get_errno(stat(path((const char *)arg1), &st));
60cd49d5 2507 goto do_stat64;
31e31b8a 2508 case TARGET_NR_lstat64:
ec86b0fb 2509 ret = get_errno(lstat(path((const char *)arg1), &st));
60cd49d5 2510 goto do_stat64;
31e31b8a 2511 case TARGET_NR_fstat64:
60cd49d5
FB
2512 {
2513 ret = get_errno(fstat(arg1, &st));
2514 do_stat64:
2515 if (!is_error(ret)) {
2516 struct target_stat64 *target_st = (void *)arg2;
ec86b0fb 2517 memset(target_st, 0, sizeof(struct target_stat64));
60cd49d5 2518 target_st->st_dev = tswap16(st.st_dev);
728584be 2519 target_st->st_ino = tswap64(st.st_ino);
ec86b0fb
FB
2520#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
2521 target_st->__st_ino = tswapl(st.st_ino);
2522#endif
9af9eaaa 2523 target_st->st_mode = tswap32(st.st_mode);
ec86b0fb
FB
2524 target_st->st_nlink = tswap32(st.st_nlink);
2525 target_st->st_uid = tswapl(st.st_uid);
2526 target_st->st_gid = tswapl(st.st_gid);
60cd49d5
FB
2527 target_st->st_rdev = tswap16(st.st_rdev);
2528 /* XXX: better use of kernel struct */
ec86b0fb 2529 target_st->st_size = tswap64(st.st_size);
60cd49d5
FB
2530 target_st->st_blksize = tswapl(st.st_blksize);
2531 target_st->st_blocks = tswapl(st.st_blocks);
7854b056
FB
2532 target_st->target_st_atime = tswapl(st.st_atime);
2533 target_st->target_st_mtime = tswapl(st.st_mtime);
2534 target_st->target_st_ctime = tswapl(st.st_ctime);
60cd49d5
FB
2535 }
2536 }
2537 break;
2538
31e31b8a 2539 case TARGET_NR_lchown32:
b03c60f3
FB
2540 ret = get_errno(lchown((const char *)arg1, arg2, arg3));
2541 break;
31e31b8a 2542 case TARGET_NR_getuid32:
b03c60f3
FB
2543 ret = get_errno(getuid());
2544 break;
31e31b8a 2545 case TARGET_NR_getgid32:
b03c60f3
FB
2546 ret = get_errno(getgid());
2547 break;
31e31b8a 2548 case TARGET_NR_geteuid32:
b03c60f3
FB
2549 ret = get_errno(geteuid());
2550 break;
31e31b8a 2551 case TARGET_NR_getegid32:
b03c60f3
FB
2552 ret = get_errno(getegid());
2553 break;
31e31b8a 2554 case TARGET_NR_setreuid32:
b03c60f3
FB
2555 ret = get_errno(setreuid(arg1, arg2));
2556 break;
31e31b8a 2557 case TARGET_NR_setregid32:
b03c60f3
FB
2558 ret = get_errno(setregid(arg1, arg2));
2559 break;
31e31b8a 2560 case TARGET_NR_getgroups32:
b03c60f3 2561 goto unimplemented;
31e31b8a 2562 case TARGET_NR_setgroups32:
b03c60f3 2563 goto unimplemented;
31e31b8a 2564 case TARGET_NR_fchown32:
b03c60f3
FB
2565 ret = get_errno(fchown(arg1, arg2, arg3));
2566 break;
31e31b8a 2567 case TARGET_NR_setresuid32:
b03c60f3
FB
2568 ret = get_errno(setresuid(arg1, arg2, arg3));
2569 break;
31e31b8a 2570 case TARGET_NR_getresuid32:
b03c60f3
FB
2571 {
2572 int ruid, euid, suid;
2573 ret = get_errno(getresuid(&ruid, &euid, &suid));
2574 if (!is_error(ret)) {
2575 *(uint32_t *)arg1 = tswap32(ruid);
2576 *(uint32_t *)arg2 = tswap32(euid);
2577 *(uint32_t *)arg3 = tswap32(suid);
2578 }
2579 }
2580 break;
31e31b8a 2581 case TARGET_NR_setresgid32:
b03c60f3
FB
2582 ret = get_errno(setresgid(arg1, arg2, arg3));
2583 break;
31e31b8a 2584 case TARGET_NR_getresgid32:
b03c60f3
FB
2585 {
2586 int rgid, egid, sgid;
2587 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2588 if (!is_error(ret)) {
2589 *(uint32_t *)arg1 = tswap32(rgid);
2590 *(uint32_t *)arg2 = tswap32(egid);
2591 *(uint32_t *)arg3 = tswap32(sgid);
2592 }
2593 }
2594 break;
31e31b8a 2595 case TARGET_NR_chown32:
b03c60f3
FB
2596 ret = get_errno(chown((const char *)arg1, arg2, arg3));
2597 break;
31e31b8a 2598 case TARGET_NR_setuid32:
b03c60f3
FB
2599 ret = get_errno(setuid(arg1));
2600 break;
31e31b8a 2601 case TARGET_NR_setgid32:
b03c60f3
FB
2602 ret = get_errno(setgid(arg1));
2603 break;
31e31b8a 2604 case TARGET_NR_setfsuid32:
b03c60f3
FB
2605 ret = get_errno(setfsuid(arg1));
2606 break;
31e31b8a 2607 case TARGET_NR_setfsgid32:
b03c60f3
FB
2608 ret = get_errno(setfsgid(arg1));
2609 break;
31e31b8a 2610 case TARGET_NR_pivot_root:
b03c60f3 2611 goto unimplemented;
31e31b8a 2612 case TARGET_NR_mincore:
b03c60f3 2613 goto unimplemented;
31e31b8a 2614 case TARGET_NR_madvise:
60cd49d5
FB
2615 goto unimplemented;
2616#if TARGET_LONG_BITS == 32
31e31b8a 2617 case TARGET_NR_fcntl64:
77e4672d
FB
2618 {
2619 struct flock64 fl;
2620 struct target_flock64 *target_fl = (void *)arg3;
2621
60cd49d5
FB
2622 switch(arg2) {
2623 case F_GETLK64:
77e4672d
FB
2624 ret = get_errno(fcntl(arg1, arg2, &fl));
2625 if (ret == 0) {
2626 target_fl->l_type = tswap16(fl.l_type);
2627 target_fl->l_whence = tswap16(fl.l_whence);
2628 target_fl->l_start = tswap64(fl.l_start);
2629 target_fl->l_len = tswap64(fl.l_len);
2630 target_fl->l_pid = tswapl(fl.l_pid);
2631 }
2632 break;
2633
60cd49d5
FB
2634 case F_SETLK64:
2635 case F_SETLKW64:
77e4672d
FB
2636 fl.l_type = tswap16(target_fl->l_type);
2637 fl.l_whence = tswap16(target_fl->l_whence);
2638 fl.l_start = tswap64(target_fl->l_start);
2639 fl.l_len = tswap64(target_fl->l_len);
2640 fl.l_pid = tswapl(target_fl->l_pid);
2641 ret = get_errno(fcntl(arg1, arg2, &fl));
2642 break;
60cd49d5 2643 default:
7775e9ec 2644 ret = get_errno(do_fcntl(arg1, arg2, arg3));
60cd49d5
FB
2645 break;
2646 }
77e4672d
FB
2647 break;
2648 }
60cd49d5 2649#endif
31e31b8a
FB
2650 case TARGET_NR_security:
2651 goto unimplemented;
2652 case TARGET_NR_gettid:
2653 ret = get_errno(gettid());
2654 break;
2655 case TARGET_NR_readahead:
5cd4393b 2656 goto unimplemented;
31e31b8a
FB
2657 case TARGET_NR_setxattr:
2658 case TARGET_NR_lsetxattr:
2659 case TARGET_NR_fsetxattr:
2660 case TARGET_NR_getxattr:
2661 case TARGET_NR_lgetxattr:
2662 case TARGET_NR_fgetxattr:
2663 case TARGET_NR_listxattr:
2664 case TARGET_NR_llistxattr:
2665 case TARGET_NR_flistxattr:
2666 case TARGET_NR_removexattr:
2667 case TARGET_NR_lremovexattr:
2668 case TARGET_NR_fremovexattr:
5cd4393b
FB
2669 goto unimplemented_nowarn;
2670 case TARGET_NR_set_thread_area:
2671 case TARGET_NR_get_thread_area:
2672 goto unimplemented_nowarn;
31e31b8a
FB
2673 default:
2674 unimplemented:
5cd4393b
FB
2675 gemu_log("qemu: Unsupported syscall: %d\n", num);
2676 unimplemented_nowarn:
31e31b8a
FB
2677 ret = -ENOSYS;
2678 break;
2679 }
2680 fail:
2681 return ret;
2682}
2683