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