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