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