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