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