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