4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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.
30 #include <sys/types.h>
34 #include <sys/mount.h>
35 #include <sys/resource.h>
40 #include <sys/socket.h>
43 #include <sys/times.h>
46 //#include <sys/user.h>
47 #include <netinet/ip.h>
48 #include <netinet/tcp.h>
50 #define termios host_termios
51 #define winsize host_winsize
52 #define termio host_termio
53 #define sgttyb host_sgttyb /* same as target */
54 #define tchars host_tchars /* same as target */
55 #define ltchars host_ltchars /* same as target */
57 #include <linux/termios.h>
58 #include <linux/unistd.h>
59 #include <linux/utsname.h>
60 #include <linux/cdrom.h>
61 #include <linux/hdreg.h>
62 #include <linux/soundcard.h>
63 #include <linux/dirent.h>
70 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC)
71 /* 16 bit uid wrappers emulation */
75 //#include <linux/msdos_fs.h>
76 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
77 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
80 #if defined(__powerpc__)
82 #undef __sc_loadargs_0
83 #undef __sc_loadargs_1
84 #undef __sc_loadargs_2
85 #undef __sc_loadargs_3
86 #undef __sc_loadargs_4
87 #undef __sc_loadargs_5
88 #undef __sc_asm_input_0
89 #undef __sc_asm_input_1
90 #undef __sc_asm_input_2
91 #undef __sc_asm_input_3
92 #undef __sc_asm_input_4
93 #undef __sc_asm_input_5
101 /* need to redefine syscalls as Linux kernel defines are incorrect for
103 /* On powerpc a system call basically clobbers the same registers like a
104 * function call, with the exception of LR (which is needed for the
105 * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
106 * an error return status).
109 #define __syscall_nr(nr, type, name, args...) \
110 unsigned long __sc_ret, __sc_err; \
112 register unsigned long __sc_0 __asm__ ("r0"); \
113 register unsigned long __sc_3 __asm__ ("r3"); \
114 register unsigned long __sc_4 __asm__ ("r4"); \
115 register unsigned long __sc_5 __asm__ ("r5"); \
116 register unsigned long __sc_6 __asm__ ("r6"); \
117 register unsigned long __sc_7 __asm__ ("r7"); \
119 __sc_loadargs_##nr(name, args); \
120 __asm__ __volatile__ \
124 "=&r" (__sc_3), "=&r" (__sc_4), \
125 "=&r" (__sc_5), "=&r" (__sc_6), \
127 : __sc_asm_input_##nr \
128 : "cr0", "ctr", "memory", \
129 "r8", "r9", "r10","r11", "r12"); \
133 if (__sc_err & 0x10000000) \
138 return (type) __sc_ret
140 #define __sc_loadargs_0(name, dummy...) \
142 #define __sc_loadargs_1(name, arg1) \
143 __sc_loadargs_0(name); \
144 __sc_3 = (unsigned long) (arg1)
145 #define __sc_loadargs_2(name, arg1, arg2) \
146 __sc_loadargs_1(name, arg1); \
147 __sc_4 = (unsigned long) (arg2)
148 #define __sc_loadargs_3(name, arg1, arg2, arg3) \
149 __sc_loadargs_2(name, arg1, arg2); \
150 __sc_5 = (unsigned long) (arg3)
151 #define __sc_loadargs_4(name, arg1, arg2, arg3, arg4) \
152 __sc_loadargs_3(name, arg1, arg2, arg3); \
153 __sc_6 = (unsigned long) (arg4)
154 #define __sc_loadargs_5(name, arg1, arg2, arg3, arg4, arg5) \
155 __sc_loadargs_4(name, arg1, arg2, arg3, arg4); \
156 __sc_7 = (unsigned long) (arg5)
158 #define __sc_asm_input_0 "0" (__sc_0)
159 #define __sc_asm_input_1 __sc_asm_input_0, "1" (__sc_3)
160 #define __sc_asm_input_2 __sc_asm_input_1, "2" (__sc_4)
161 #define __sc_asm_input_3 __sc_asm_input_2, "3" (__sc_5)
162 #define __sc_asm_input_4 __sc_asm_input_3, "4" (__sc_6)
163 #define __sc_asm_input_5 __sc_asm_input_4, "5" (__sc_7)
165 #define _syscall0(type,name) \
168 __syscall_nr(0, type, name); \
171 #define _syscall1(type,name,type1,arg1) \
172 type name(type1 arg1) \
174 __syscall_nr(1, type, name, arg1); \
177 #define _syscall2(type,name,type1,arg1,type2,arg2) \
178 type name(type1 arg1, type2 arg2) \
180 __syscall_nr(2, type, name, arg1, arg2); \
183 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
184 type name(type1 arg1, type2 arg2, type3 arg3) \
186 __syscall_nr(3, type, name, arg1, arg2, arg3); \
189 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
190 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
192 __syscall_nr(4, type, name, arg1, arg2, arg3, arg4); \
195 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
196 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \
198 __syscall_nr(5, type, name, arg1, arg2, arg3, arg4, arg5); \
202 #define __NR_sys_uname __NR_uname
203 #define __NR_sys_getcwd1 __NR_getcwd
204 #define __NR_sys_statfs __NR_statfs
205 #define __NR_sys_fstatfs __NR_fstatfs
206 #define __NR_sys_getdents __NR_getdents
207 #define __NR_sys_getdents64 __NR_getdents64
208 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
210 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
211 #define __NR__llseek __NR_lseek
215 _syscall0(int, gettid)
217 static int gettid(void) {
221 _syscall1(int,sys_uname,struct new_utsname *,buf)
222 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
223 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
224 _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
225 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
226 loff_t *, res, uint, wh);
227 _syscall2(int,sys_statfs,const char *,path,struct kernel_statfs *,buf)
228 _syscall2(int,sys_fstatfs,int,fd,struct kernel_statfs *,buf)
229 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
230 #ifdef __NR_exit_group
231 _syscall1(int,exit_group,int,error_code)
234 extern int personality(int);
235 extern int flock(int, int);
236 extern int setfsuid(int);
237 extern int setfsgid(int);
238 extern int setresuid(uid_t, uid_t, uid_t);
239 extern int getresuid(uid_t *, uid_t *, uid_t *);
240 extern int setresgid(gid_t, gid_t, gid_t);
241 extern int getresgid(gid_t *, gid_t *, gid_t *);
242 extern int setgroups(int, gid_t *);
244 static inline long get_errno(long ret)
252 static inline int is_error(long ret)
254 return (unsigned long)ret >= (unsigned long)(-4096);
257 static char *target_brk;
258 static char *target_original_brk;
260 void target_set_brk(char *new_brk)
262 target_brk = new_brk;
263 target_original_brk = new_brk;
266 static long do_brk(char *new_brk)
273 return (long)target_brk;
274 if (new_brk < target_original_brk)
277 brk_page = (char *)HOST_PAGE_ALIGN((unsigned long)target_brk);
279 /* If the new brk is less than this, set it and we're done... */
280 if (new_brk < brk_page) {
281 target_brk = new_brk;
282 return (long)target_brk;
285 /* We need to allocate more memory after the brk... */
286 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
287 mapped_addr = get_errno(target_mmap((unsigned long)brk_page, new_alloc_size,
288 PROT_READ|PROT_WRITE,
289 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
290 if (is_error(mapped_addr)) {
293 target_brk = new_brk;
294 return (long)target_brk;
298 static inline fd_set *target_to_host_fds(fd_set *fds,
299 target_long *target_fds, int n)
301 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
302 return (fd_set *)target_fds;
307 for(i = 0;i < n; i++) {
308 b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
309 (i & (TARGET_LONG_BITS - 1))) & 1;
320 static inline void host_to_target_fds(target_long *target_fds,
323 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
330 nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
332 for(i = 0;i < nw; i++) {
334 for(j = 0; j < TARGET_LONG_BITS; j++) {
335 v |= ((FD_ISSET(k, fds) != 0) << j);
338 target_fds[i] = tswapl(v);
344 #if defined(__alpha__)
350 static inline long host_to_target_clock_t(long ticks)
352 #if HOST_HZ == TARGET_HZ
355 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
359 static inline void host_to_target_rusage(struct target_rusage *target_rusage,
360 const struct rusage *rusage)
362 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
363 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
364 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
365 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
366 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
367 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
368 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
369 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
370 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
371 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
372 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
373 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
374 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
375 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
376 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
377 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
378 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
379 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
382 static inline void target_to_host_timeval(struct timeval *tv,
383 const struct target_timeval *target_tv)
385 tv->tv_sec = tswapl(target_tv->tv_sec);
386 tv->tv_usec = tswapl(target_tv->tv_usec);
389 static inline void host_to_target_timeval(struct target_timeval *target_tv,
390 const struct timeval *tv)
392 target_tv->tv_sec = tswapl(tv->tv_sec);
393 target_tv->tv_usec = tswapl(tv->tv_usec);
397 static long do_select(long n,
398 target_long *target_rfds, target_long *target_wfds,
399 target_long *target_efds, struct target_timeval *target_tv)
401 fd_set rfds, wfds, efds;
402 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
403 struct timeval tv, *tv_ptr;
406 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
407 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
408 efds_ptr = target_to_host_fds(&efds, target_efds, n);
411 target_to_host_timeval(&tv, target_tv);
416 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
417 if (!is_error(ret)) {
418 host_to_target_fds(target_rfds, rfds_ptr, n);
419 host_to_target_fds(target_wfds, wfds_ptr, n);
420 host_to_target_fds(target_efds, efds_ptr, n);
423 host_to_target_timeval(target_tv, &tv);
429 static inline void target_to_host_sockaddr(struct sockaddr *addr,
430 struct target_sockaddr *target_addr,
433 memcpy(addr, target_addr, len);
434 addr->sa_family = tswap16(target_addr->sa_family);
437 static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
438 struct sockaddr *addr,
441 memcpy(target_addr, addr, len);
442 target_addr->sa_family = tswap16(addr->sa_family);
445 static inline void target_to_host_cmsg(struct msghdr *msgh,
446 struct target_msghdr *target_msgh)
448 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
449 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
452 while (cmsg && target_cmsg) {
453 void *data = CMSG_DATA(cmsg);
454 void *target_data = TARGET_CMSG_DATA(target_cmsg);
456 int len = tswapl(target_cmsg->cmsg_len)
457 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
459 space += CMSG_SPACE(len);
460 if (space > msgh->msg_controllen) {
461 space -= CMSG_SPACE(len);
462 gemu_log("Host cmsg overflow");
466 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
467 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
468 cmsg->cmsg_len = CMSG_LEN(len);
470 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
471 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
472 memcpy(data, target_data, len);
474 int *fd = (int *)data;
475 int *target_fd = (int *)target_data;
476 int i, numfds = len / sizeof(int);
478 for (i = 0; i < numfds; i++)
479 fd[i] = tswap32(target_fd[i]);
482 cmsg = CMSG_NXTHDR(msgh, cmsg);
483 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
486 msgh->msg_controllen = space;
489 static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
492 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
493 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
496 while (cmsg && target_cmsg) {
497 void *data = CMSG_DATA(cmsg);
498 void *target_data = TARGET_CMSG_DATA(target_cmsg);
500 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
502 space += TARGET_CMSG_SPACE(len);
503 if (space > tswapl(target_msgh->msg_controllen)) {
504 space -= TARGET_CMSG_SPACE(len);
505 gemu_log("Target cmsg overflow");
509 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
510 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
511 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
513 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
514 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
515 memcpy(target_data, data, len);
517 int *fd = (int *)data;
518 int *target_fd = (int *)target_data;
519 int i, numfds = len / sizeof(int);
521 for (i = 0; i < numfds; i++)
522 target_fd[i] = tswap32(fd[i]);
525 cmsg = CMSG_NXTHDR(msgh, cmsg);
526 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
529 msgh->msg_controllen = tswapl(space);
532 static long do_setsockopt(int sockfd, int level, int optname,
533 void *optval, socklen_t optlen)
539 /* TCP options all take an 'int' value. */
540 if (optlen < sizeof(uint32_t))
543 if (get_user(val, (uint32_t *)optval))
545 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
551 if (optlen >= sizeof(uint32_t)) {
552 if (get_user(val, (uint32_t *)optval))
554 } else if (optlen >= 1) {
555 if (get_user(val, (uint8_t *)optval))
558 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
566 /* Options with 'int' argument. */
585 if (optlen < sizeof(uint32_t))
587 if (get_user(val, (uint32_t *)optval))
589 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
597 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
603 static long do_getsockopt(int sockfd, int level, int optname,
604 void *optval, socklen_t *optlen)
606 int len, lv, val, ret;
616 /* These don't just return a single integer */
619 if (get_user(len, optlen))
624 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
630 if (copy_to_user(optval, &val, len))
632 if (put_user(len, optlen))
639 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
647 static long do_socketcall(int num, int32_t *vptr)
654 int domain = tswap32(vptr[0]);
655 int type = tswap32(vptr[1]);
656 int protocol = tswap32(vptr[2]);
658 ret = get_errno(socket(domain, type, protocol));
663 int sockfd = tswap32(vptr[0]);
664 void *target_addr = (void *)tswap32(vptr[1]);
665 socklen_t addrlen = tswap32(vptr[2]);
666 void *addr = alloca(addrlen);
668 target_to_host_sockaddr(addr, target_addr, addrlen);
669 ret = get_errno(bind(sockfd, addr, addrlen));
674 int sockfd = tswap32(vptr[0]);
675 void *target_addr = (void *)tswap32(vptr[1]);
676 socklen_t addrlen = tswap32(vptr[2]);
677 void *addr = alloca(addrlen);
679 target_to_host_sockaddr(addr, target_addr, addrlen);
680 ret = get_errno(connect(sockfd, addr, addrlen));
685 int sockfd = tswap32(vptr[0]);
686 int backlog = tswap32(vptr[1]);
688 ret = get_errno(listen(sockfd, backlog));
693 int sockfd = tswap32(vptr[0]);
694 void *target_addr = (void *)tswap32(vptr[1]);
695 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
696 socklen_t addrlen = tswap32(*target_addrlen);
697 void *addr = alloca(addrlen);
699 ret = get_errno(accept(sockfd, addr, &addrlen));
700 if (!is_error(ret)) {
701 host_to_target_sockaddr(target_addr, addr, addrlen);
702 *target_addrlen = tswap32(addrlen);
706 case SOCKOP_getsockname:
708 int sockfd = tswap32(vptr[0]);
709 void *target_addr = (void *)tswap32(vptr[1]);
710 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
711 socklen_t addrlen = tswap32(*target_addrlen);
712 void *addr = alloca(addrlen);
714 ret = get_errno(getsockname(sockfd, addr, &addrlen));
715 if (!is_error(ret)) {
716 host_to_target_sockaddr(target_addr, addr, addrlen);
717 *target_addrlen = tswap32(addrlen);
721 case SOCKOP_getpeername:
723 int sockfd = tswap32(vptr[0]);
724 void *target_addr = (void *)tswap32(vptr[1]);
725 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
726 socklen_t addrlen = tswap32(*target_addrlen);
727 void *addr = alloca(addrlen);
729 ret = get_errno(getpeername(sockfd, addr, &addrlen));
730 if (!is_error(ret)) {
731 host_to_target_sockaddr(target_addr, addr, addrlen);
732 *target_addrlen = tswap32(addrlen);
736 case SOCKOP_socketpair:
738 int domain = tswap32(vptr[0]);
739 int type = tswap32(vptr[1]);
740 int protocol = tswap32(vptr[2]);
741 int32_t *target_tab = (void *)tswap32(vptr[3]);
744 ret = get_errno(socketpair(domain, type, protocol, tab));
745 if (!is_error(ret)) {
746 target_tab[0] = tswap32(tab[0]);
747 target_tab[1] = tswap32(tab[1]);
753 int sockfd = tswap32(vptr[0]);
754 void *msg = (void *)tswap32(vptr[1]);
755 size_t len = tswap32(vptr[2]);
756 int flags = tswap32(vptr[3]);
758 ret = get_errno(send(sockfd, msg, len, flags));
763 int sockfd = tswap32(vptr[0]);
764 void *msg = (void *)tswap32(vptr[1]);
765 size_t len = tswap32(vptr[2]);
766 int flags = tswap32(vptr[3]);
768 ret = get_errno(recv(sockfd, msg, len, flags));
773 int sockfd = tswap32(vptr[0]);
774 void *msg = (void *)tswap32(vptr[1]);
775 size_t len = tswap32(vptr[2]);
776 int flags = tswap32(vptr[3]);
777 void *target_addr = (void *)tswap32(vptr[4]);
778 socklen_t addrlen = tswap32(vptr[5]);
779 void *addr = alloca(addrlen);
781 target_to_host_sockaddr(addr, target_addr, addrlen);
782 ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
785 case SOCKOP_recvfrom:
787 int sockfd = tswap32(vptr[0]);
788 void *msg = (void *)tswap32(vptr[1]);
789 size_t len = tswap32(vptr[2]);
790 int flags = tswap32(vptr[3]);
791 void *target_addr = (void *)tswap32(vptr[4]);
792 uint32_t *target_addrlen = (void *)tswap32(vptr[5]);
793 socklen_t addrlen = tswap32(*target_addrlen);
794 void *addr = alloca(addrlen);
796 ret = get_errno(recvfrom(sockfd, msg, len, flags, addr, &addrlen));
797 if (!is_error(ret)) {
798 host_to_target_sockaddr(target_addr, addr, addrlen);
799 *target_addrlen = tswap32(addrlen);
803 case SOCKOP_shutdown:
805 int sockfd = tswap32(vptr[0]);
806 int how = tswap32(vptr[1]);
808 ret = get_errno(shutdown(sockfd, how));
815 struct target_msghdr *msgp;
819 struct target_iovec *target_vec;
821 msgp = (void *)tswap32(vptr[1]);
822 msg.msg_name = (void *)tswapl(msgp->msg_name);
823 msg.msg_namelen = tswapl(msgp->msg_namelen);
824 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
825 msg.msg_control = alloca(msg.msg_controllen);
826 msg.msg_flags = tswap32(msgp->msg_flags);
828 count = tswapl(msgp->msg_iovlen);
829 vec = alloca(count * sizeof(struct iovec));
830 target_vec = (void *)tswapl(msgp->msg_iov);
831 for(i = 0;i < count; i++) {
832 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
833 vec[i].iov_len = tswapl(target_vec[i].iov_len);
835 msg.msg_iovlen = count;
838 fd = tswap32(vptr[0]);
839 flags = tswap32(vptr[2]);
840 if (num == SOCKOP_sendmsg) {
841 target_to_host_cmsg(&msg, msgp);
842 ret = get_errno(sendmsg(fd, &msg, flags));
844 ret = get_errno(recvmsg(fd, &msg, flags));
846 host_to_target_cmsg(msgp, &msg);
850 case SOCKOP_setsockopt:
852 int sockfd = tswap32(vptr[0]);
853 int level = tswap32(vptr[1]);
854 int optname = tswap32(vptr[2]);
855 void *optval = (void *)tswap32(vptr[3]);
856 socklen_t optlen = tswap32(vptr[4]);
858 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
861 case SOCKOP_getsockopt:
863 int sockfd = tswap32(vptr[0]);
864 int level = tswap32(vptr[1]);
865 int optname = tswap32(vptr[2]);
866 void *optval = (void *)tswap32(vptr[3]);
867 uint32_t *poptlen = (void *)tswap32(vptr[4]);
869 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
873 gemu_log("Unsupported socketcall: %d\n", num);
881 #define N_SHM_REGIONS 32
883 static struct shm_region {
886 } shm_regions[N_SHM_REGIONS];
888 static long do_ipc(long call, long first, long second, long third,
889 long ptr, long fifth)
894 struct shmid_ds shm_info;
897 version = call >> 16;
902 /* SHM_* flags are the same on all linux platforms */
903 ret = get_errno((long) shmat(first, (void *) ptr, second));
907 /* find out the length of the shared memory segment */
909 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
911 /* can't get length, bail out */
912 shmdt((void *) raddr);
915 page_set_flags(raddr, raddr + shm_info.shm_segsz,
916 PAGE_VALID | PAGE_READ |
917 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
918 for (i = 0; i < N_SHM_REGIONS; ++i) {
919 if (shm_regions[i].start == 0) {
920 shm_regions[i].start = raddr;
921 shm_regions[i].size = shm_info.shm_segsz;
925 if (put_user(raddr, (uint32_t *)third))
930 for (i = 0; i < N_SHM_REGIONS; ++i) {
931 if (shm_regions[i].start == ptr) {
932 shm_regions[i].start = 0;
933 page_set_flags(ptr, shm_regions[i].size, 0);
937 ret = get_errno(shmdt((void *) ptr));
941 /* IPC_* flag values are the same on all linux platforms */
942 ret = get_errno(shmget(first, second, third));
945 /* IPC_* and SHM_* command values are the same on all linux platforms */
951 ret = get_errno(shmctl(first, second, NULL));
959 gemu_log("Unsupported ipc call: %ld (version %d)\n", call, version);
966 /* kernel structure types definitions */
969 #define STRUCT(name, list...) STRUCT_ ## name,
970 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
972 #include "syscall_types.h"
975 #undef STRUCT_SPECIAL
977 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
978 #define STRUCT_SPECIAL(name)
979 #include "syscall_types.h"
981 #undef STRUCT_SPECIAL
983 typedef struct IOCTLEntry {
984 unsigned int target_cmd;
985 unsigned int host_cmd;
988 const argtype arg_type[5];
993 #define IOC_RW (IOC_R | IOC_W)
995 #define MAX_STRUCT_SIZE 4096
997 IOCTLEntry ioctl_entries[] = {
998 #define IOCTL(cmd, access, types...) \
999 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1004 static long do_ioctl(long fd, long cmd, long arg)
1006 const IOCTLEntry *ie;
1007 const argtype *arg_type;
1009 uint8_t buf_temp[MAX_STRUCT_SIZE];
1013 if (ie->target_cmd == 0) {
1014 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
1017 if (ie->target_cmd == cmd)
1021 arg_type = ie->arg_type;
1023 gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
1025 switch(arg_type[0]) {
1028 ret = get_errno(ioctl(fd, ie->host_cmd));
1033 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1037 switch(ie->access) {
1039 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1040 if (!is_error(ret)) {
1041 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
1045 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
1046 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1050 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
1051 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1052 if (!is_error(ret)) {
1053 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
1059 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
1066 bitmask_transtbl iflag_tbl[] = {
1067 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1068 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1069 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1070 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1071 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1072 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1073 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1074 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1075 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1076 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1077 { TARGET_IXON, TARGET_IXON, IXON, IXON },
1078 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1079 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1080 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1084 bitmask_transtbl oflag_tbl[] = {
1085 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1086 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1087 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1088 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1089 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1090 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1091 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1092 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1093 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1094 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1095 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1096 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1097 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1098 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1099 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1100 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1101 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1102 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1103 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1104 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1105 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1106 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1107 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1108 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1112 bitmask_transtbl cflag_tbl[] = {
1113 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1114 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1115 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1116 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1117 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1118 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1119 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1120 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1121 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1122 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1123 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1124 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1125 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1126 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1127 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1128 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1129 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1130 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1131 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1132 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1133 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1134 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1135 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1136 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1137 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1138 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1139 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1140 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1141 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1142 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1143 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1147 bitmask_transtbl lflag_tbl[] = {
1148 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1149 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1150 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1151 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1152 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1153 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1154 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1155 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1156 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1157 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1158 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1159 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1160 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1161 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1162 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1166 static void target_to_host_termios (void *dst, const void *src)
1168 struct host_termios *host = dst;
1169 const struct target_termios *target = src;
1172 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
1174 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
1176 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
1178 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
1179 host->c_line = target->c_line;
1181 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
1182 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
1183 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
1184 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
1185 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
1186 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
1187 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
1188 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
1189 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
1190 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
1191 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
1192 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
1193 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
1194 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
1195 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
1196 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
1197 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
1200 static void host_to_target_termios (void *dst, const void *src)
1202 struct target_termios *target = dst;
1203 const struct host_termios *host = src;
1206 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
1208 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
1210 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
1212 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
1213 target->c_line = host->c_line;
1215 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
1216 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
1217 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
1218 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
1219 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
1220 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
1221 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
1222 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
1223 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
1224 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
1225 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
1226 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
1227 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
1228 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
1229 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
1230 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
1231 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
1234 StructEntry struct_termios_def = {
1235 .convert = { host_to_target_termios, target_to_host_termios },
1236 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
1237 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
1240 static bitmask_transtbl mmap_flags_tbl[] = {
1241 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
1242 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
1243 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
1244 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
1245 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
1246 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
1247 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
1248 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
1252 static bitmask_transtbl fcntl_flags_tbl[] = {
1253 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
1254 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
1255 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
1256 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
1257 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
1258 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
1259 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
1260 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
1261 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
1262 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
1263 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
1264 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
1265 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
1266 #if defined(O_DIRECT)
1267 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
1272 #if defined(TARGET_I386)
1274 /* NOTE: there is really one LDT for all the threads */
1277 static int read_ldt(void *ptr, unsigned long bytecount)
1283 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
1284 if (size > bytecount)
1286 memcpy(ptr, ldt_table, size);
1290 /* XXX: add locking support */
1291 static int write_ldt(CPUX86State *env,
1292 void *ptr, unsigned long bytecount, int oldmode)
1294 struct target_modify_ldt_ldt_s ldt_info;
1295 int seg_32bit, contents, read_exec_only, limit_in_pages;
1296 int seg_not_present, useable;
1297 uint32_t *lp, entry_1, entry_2;
1299 if (bytecount != sizeof(ldt_info))
1301 memcpy(&ldt_info, ptr, sizeof(ldt_info));
1302 tswap32s(&ldt_info.entry_number);
1303 tswapls((long *)&ldt_info.base_addr);
1304 tswap32s(&ldt_info.limit);
1305 tswap32s(&ldt_info.flags);
1307 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
1309 seg_32bit = ldt_info.flags & 1;
1310 contents = (ldt_info.flags >> 1) & 3;
1311 read_exec_only = (ldt_info.flags >> 3) & 1;
1312 limit_in_pages = (ldt_info.flags >> 4) & 1;
1313 seg_not_present = (ldt_info.flags >> 5) & 1;
1314 useable = (ldt_info.flags >> 6) & 1;
1316 if (contents == 3) {
1319 if (seg_not_present == 0)
1322 /* allocate the LDT */
1324 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1327 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1328 env->ldt.base = ldt_table;
1329 env->ldt.limit = 0xffff;
1332 /* NOTE: same code as Linux kernel */
1333 /* Allow LDTs to be cleared by the user. */
1334 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
1337 read_exec_only == 1 &&
1339 limit_in_pages == 0 &&
1340 seg_not_present == 1 &&
1348 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
1349 (ldt_info.limit & 0x0ffff);
1350 entry_2 = (ldt_info.base_addr & 0xff000000) |
1351 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
1352 (ldt_info.limit & 0xf0000) |
1353 ((read_exec_only ^ 1) << 9) |
1355 ((seg_not_present ^ 1) << 15) |
1357 (limit_in_pages << 23) |
1360 entry_2 |= (useable << 20);
1362 /* Install the new entry ... */
1364 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
1365 lp[0] = tswap32(entry_1);
1366 lp[1] = tswap32(entry_2);
1370 /* specific and weird i386 syscalls */
1371 int do_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
1377 ret = read_ldt(ptr, bytecount);
1380 ret = write_ldt(env, ptr, bytecount, 1);
1383 ret = write_ldt(env, ptr, bytecount, 0);
1389 #endif /* defined(TARGET_I386) */
1391 /* this stack is the equivalent of the kernel stack associated with a
1393 #define NEW_STACK_SIZE 8192
1395 static int clone_func(void *arg)
1397 CPUState *env = arg;
1403 int do_fork(CPUState *env, unsigned int flags, unsigned long newsp)
1410 if (flags & CLONE_VM) {
1411 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
1412 memset(ts, 0, sizeof(TaskState));
1413 new_stack = ts->stack;
1415 /* add in task state list */
1416 ts->next = first_task_state;
1417 first_task_state = ts;
1418 /* we create a new CPU instance. */
1419 new_env = cpu_init();
1420 memcpy(new_env, env, sizeof(CPUState));
1421 #if defined(TARGET_I386)
1423 newsp = env->regs[R_ESP];
1424 new_env->regs[R_ESP] = newsp;
1425 new_env->regs[R_EAX] = 0;
1426 #elif defined(TARGET_ARM)
1428 newsp = env->regs[13];
1429 new_env->regs[13] = newsp;
1430 new_env->regs[0] = 0;
1431 #elif defined(TARGET_SPARC)
1432 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
1433 #elif defined(TARGET_PPC)
1435 newsp = env->gpr[1];
1436 new_env->gpr[1] = newsp;
1439 for (i = 7; i < 32; i++)
1440 new_env->gpr[i] = 0;
1443 #error unsupported target CPU
1445 new_env->opaque = ts;
1447 ret = clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1449 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1452 /* if no CLONE_VM, we consider it is a fork */
1453 if ((flags & ~CSIGNAL) != 0)
1460 static long do_fcntl(int fd, int cmd, unsigned long arg)
1463 struct target_flock *target_fl = (void *)arg;
1467 case TARGET_F_GETLK:
1468 ret = fcntl(fd, cmd, &fl);
1470 target_fl->l_type = tswap16(fl.l_type);
1471 target_fl->l_whence = tswap16(fl.l_whence);
1472 target_fl->l_start = tswapl(fl.l_start);
1473 target_fl->l_len = tswapl(fl.l_len);
1474 target_fl->l_pid = tswapl(fl.l_pid);
1478 case TARGET_F_SETLK:
1479 case TARGET_F_SETLKW:
1480 fl.l_type = tswap16(target_fl->l_type);
1481 fl.l_whence = tswap16(target_fl->l_whence);
1482 fl.l_start = tswapl(target_fl->l_start);
1483 fl.l_len = tswapl(target_fl->l_len);
1484 fl.l_pid = tswapl(target_fl->l_pid);
1485 ret = fcntl(fd, cmd, &fl);
1488 case TARGET_F_GETLK64:
1489 case TARGET_F_SETLK64:
1490 case TARGET_F_SETLKW64:
1496 ret = fcntl(fd, cmd, arg);
1497 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
1501 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
1505 ret = fcntl(fd, cmd, arg);
1513 static inline int high2lowuid(int uid)
1521 static inline int high2lowgid(int gid)
1529 static inline int low2highuid(int uid)
1531 if ((int16_t)uid == -1)
1537 static inline int low2highgid(int gid)
1539 if ((int16_t)gid == -1)
1545 #endif /* USE_UID16 */
1547 void syscall_init(void)
1550 const argtype *arg_type;
1553 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
1554 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
1555 #include "syscall_types.h"
1557 #undef STRUCT_SPECIAL
1559 /* we patch the ioctl size if necessary. We rely on the fact that
1560 no ioctl has all the bits at '1' in the size field */
1562 while (ie->target_cmd != 0) {
1563 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
1564 TARGET_IOC_SIZEMASK) {
1565 arg_type = ie->arg_type;
1566 if (arg_type[0] != TYPE_PTR) {
1567 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
1572 size = thunk_type_size(arg_type, 0);
1573 ie->target_cmd = (ie->target_cmd &
1574 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
1575 (size << TARGET_IOC_SIZESHIFT);
1577 /* automatic consistency check if same arch */
1578 #if defined(__i386__) && defined(TARGET_I386)
1579 if (ie->target_cmd != ie->host_cmd) {
1580 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
1581 ie->target_cmd, ie->host_cmd);
1588 long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
1589 long arg4, long arg5, long arg6)
1593 struct kernel_statfs *stfs;
1596 gemu_log("syscall %d", num);
1599 case TARGET_NR_exit:
1603 /* XXX: should free thread stack and CPU env */
1605 ret = 0; /* avoid warning */
1607 case TARGET_NR_read:
1608 page_unprotect_range((void *)arg2, arg3);
1609 ret = get_errno(read(arg1, (void *)arg2, arg3));
1611 case TARGET_NR_write:
1612 ret = get_errno(write(arg1, (void *)arg2, arg3));
1614 case TARGET_NR_open:
1615 ret = get_errno(open(path((const char *)arg1),
1616 target_to_host_bitmask(arg2, fcntl_flags_tbl),
1619 case TARGET_NR_close:
1620 ret = get_errno(close(arg1));
1623 ret = do_brk((char *)arg1);
1625 case TARGET_NR_fork:
1626 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
1628 case TARGET_NR_waitpid:
1630 int *status = (int *)arg2;
1631 ret = get_errno(waitpid(arg1, status, arg3));
1632 if (!is_error(ret) && status)
1633 tswapls((long *)&status);
1636 case TARGET_NR_creat:
1637 ret = get_errno(creat((const char *)arg1, arg2));
1639 case TARGET_NR_link:
1640 ret = get_errno(link((const char *)arg1, (const char *)arg2));
1642 case TARGET_NR_unlink:
1643 ret = get_errno(unlink((const char *)arg1));
1645 case TARGET_NR_execve:
1647 char **argp, **envp;
1653 for (p = (void *)arg2; *p; p++)
1656 for (p = (void *)arg3; *p; p++)
1659 argp = alloca((argc + 1) * sizeof(void *));
1660 envp = alloca((envc + 1) * sizeof(void *));
1662 for (p = (void *)arg2, q = argp; *p; p++, q++)
1663 *q = (void *)tswap32(*p);
1666 for (p = (void *)arg3, q = envp; *p; p++, q++)
1667 *q = (void *)tswap32(*p);
1670 ret = get_errno(execve((const char *)arg1, argp, envp));
1673 case TARGET_NR_chdir:
1674 ret = get_errno(chdir((const char *)arg1));
1676 case TARGET_NR_time:
1678 int *time_ptr = (int *)arg1;
1679 ret = get_errno(time((time_t *)time_ptr));
1680 if (!is_error(ret) && time_ptr)
1684 case TARGET_NR_mknod:
1685 ret = get_errno(mknod((const char *)arg1, arg2, arg3));
1687 case TARGET_NR_chmod:
1688 ret = get_errno(chmod((const char *)arg1, arg2));
1690 #ifdef TARGET_NR_break
1691 case TARGET_NR_break:
1694 #ifdef TARGET_NR_oldstat
1695 case TARGET_NR_oldstat:
1698 case TARGET_NR_lseek:
1699 ret = get_errno(lseek(arg1, arg2, arg3));
1701 case TARGET_NR_getpid:
1702 ret = get_errno(getpid());
1704 case TARGET_NR_mount:
1705 /* need to look at the data field */
1707 case TARGET_NR_umount:
1708 ret = get_errno(umount((const char *)arg1));
1710 case TARGET_NR_stime:
1712 int *time_ptr = (int *)arg1;
1715 ret = get_errno(stime((time_t *)time_ptr));
1718 case TARGET_NR_ptrace:
1720 case TARGET_NR_alarm:
1723 #ifdef TARGET_NR_oldfstat
1724 case TARGET_NR_oldfstat:
1727 case TARGET_NR_pause:
1728 ret = get_errno(pause());
1730 case TARGET_NR_utime:
1732 struct utimbuf tbuf;
1733 struct target_utimbuf *target_tbuf = (void *)arg2;
1734 tbuf.actime = tswapl(target_tbuf->actime);
1735 tbuf.modtime = tswapl(target_tbuf->modtime);
1736 ret = get_errno(utime((const char *)arg1, &tbuf));
1739 #ifdef TARGET_NR_stty
1740 case TARGET_NR_stty:
1743 #ifdef TARGET_NR_gtty
1744 case TARGET_NR_gtty:
1747 case TARGET_NR_access:
1748 ret = get_errno(access((const char *)arg1, arg2));
1750 case TARGET_NR_nice:
1751 ret = get_errno(nice(arg1));
1753 #ifdef TARGET_NR_ftime
1754 case TARGET_NR_ftime:
1757 case TARGET_NR_sync:
1761 case TARGET_NR_kill:
1762 ret = get_errno(kill(arg1, arg2));
1764 case TARGET_NR_rename:
1765 ret = get_errno(rename((const char *)arg1, (const char *)arg2));
1767 case TARGET_NR_mkdir:
1768 ret = get_errno(mkdir((const char *)arg1, arg2));
1770 case TARGET_NR_rmdir:
1771 ret = get_errno(rmdir((const char *)arg1));
1774 ret = get_errno(dup(arg1));
1776 case TARGET_NR_pipe:
1778 int *pipe_ptr = (int *)arg1;
1779 ret = get_errno(pipe(pipe_ptr));
1780 if (!is_error(ret)) {
1781 tswap32s(&pipe_ptr[0]);
1782 tswap32s(&pipe_ptr[1]);
1786 case TARGET_NR_times:
1788 struct target_tms *tmsp = (void *)arg1;
1790 ret = get_errno(times(&tms));
1792 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
1793 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
1794 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
1795 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
1798 ret = host_to_target_clock_t(ret);
1801 #ifdef TARGET_NR_prof
1802 case TARGET_NR_prof:
1805 case TARGET_NR_signal:
1808 case TARGET_NR_acct:
1810 case TARGET_NR_umount2:
1811 ret = get_errno(umount2((const char *)arg1, arg2));
1813 #ifdef TARGET_NR_lock
1814 case TARGET_NR_lock:
1817 case TARGET_NR_ioctl:
1818 ret = do_ioctl(arg1, arg2, arg3);
1820 case TARGET_NR_fcntl:
1821 ret = get_errno(do_fcntl(arg1, arg2, arg3));
1823 #ifdef TARGET_NR_mpx
1827 case TARGET_NR_setpgid:
1828 ret = get_errno(setpgid(arg1, arg2));
1830 #ifdef TARGET_NR_ulimit
1831 case TARGET_NR_ulimit:
1834 #ifdef TARGET_NR_oldolduname
1835 case TARGET_NR_oldolduname:
1838 case TARGET_NR_umask:
1839 ret = get_errno(umask(arg1));
1841 case TARGET_NR_chroot:
1842 ret = get_errno(chroot((const char *)arg1));
1844 case TARGET_NR_ustat:
1846 case TARGET_NR_dup2:
1847 ret = get_errno(dup2(arg1, arg2));
1849 case TARGET_NR_getppid:
1850 ret = get_errno(getppid());
1852 case TARGET_NR_getpgrp:
1853 ret = get_errno(getpgrp());
1855 case TARGET_NR_setsid:
1856 ret = get_errno(setsid());
1858 case TARGET_NR_sigaction:
1860 struct target_old_sigaction *old_act = (void *)arg2;
1861 struct target_old_sigaction *old_oact = (void *)arg3;
1862 struct target_sigaction act, oact, *pact;
1864 act._sa_handler = old_act->_sa_handler;
1865 target_siginitset(&act.sa_mask, old_act->sa_mask);
1866 act.sa_flags = old_act->sa_flags;
1867 act.sa_restorer = old_act->sa_restorer;
1872 ret = get_errno(do_sigaction(arg1, pact, &oact));
1873 if (!is_error(ret) && old_oact) {
1874 old_oact->_sa_handler = oact._sa_handler;
1875 old_oact->sa_mask = oact.sa_mask.sig[0];
1876 old_oact->sa_flags = oact.sa_flags;
1877 old_oact->sa_restorer = oact.sa_restorer;
1881 case TARGET_NR_rt_sigaction:
1882 ret = get_errno(do_sigaction(arg1, (void *)arg2, (void *)arg3));
1884 case TARGET_NR_sgetmask:
1887 target_ulong target_set;
1888 sigprocmask(0, NULL, &cur_set);
1889 host_to_target_old_sigset(&target_set, &cur_set);
1893 case TARGET_NR_ssetmask:
1895 sigset_t set, oset, cur_set;
1896 target_ulong target_set = arg1;
1897 sigprocmask(0, NULL, &cur_set);
1898 target_to_host_old_sigset(&set, &target_set);
1899 sigorset(&set, &set, &cur_set);
1900 sigprocmask(SIG_SETMASK, &set, &oset);
1901 host_to_target_old_sigset(&target_set, &oset);
1905 case TARGET_NR_sigprocmask:
1908 sigset_t set, oldset, *set_ptr;
1909 target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
1913 case TARGET_SIG_BLOCK:
1916 case TARGET_SIG_UNBLOCK:
1919 case TARGET_SIG_SETMASK:
1926 target_to_host_old_sigset(&set, pset);
1932 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
1933 if (!is_error(ret) && poldset) {
1934 host_to_target_old_sigset(poldset, &oldset);
1938 case TARGET_NR_rt_sigprocmask:
1941 sigset_t set, oldset, *set_ptr;
1942 target_sigset_t *pset = (void *)arg2;
1943 target_sigset_t *poldset = (void *)arg3;
1947 case TARGET_SIG_BLOCK:
1950 case TARGET_SIG_UNBLOCK:
1953 case TARGET_SIG_SETMASK:
1960 target_to_host_sigset(&set, pset);
1966 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
1967 if (!is_error(ret) && poldset) {
1968 host_to_target_sigset(poldset, &oldset);
1972 case TARGET_NR_sigpending:
1975 ret = get_errno(sigpending(&set));
1976 if (!is_error(ret)) {
1977 host_to_target_old_sigset((target_ulong *)arg1, &set);
1981 case TARGET_NR_rt_sigpending:
1984 ret = get_errno(sigpending(&set));
1985 if (!is_error(ret)) {
1986 host_to_target_sigset((target_sigset_t *)arg1, &set);
1990 case TARGET_NR_sigsuspend:
1993 target_to_host_old_sigset(&set, (target_ulong *)arg1);
1994 ret = get_errno(sigsuspend(&set));
1997 case TARGET_NR_rt_sigsuspend:
2000 target_to_host_sigset(&set, (target_sigset_t *)arg1);
2001 ret = get_errno(sigsuspend(&set));
2004 case TARGET_NR_rt_sigtimedwait:
2006 target_sigset_t *target_set = (void *)arg1;
2007 target_siginfo_t *target_uinfo = (void *)arg2;
2008 struct target_timespec *target_uts = (void *)arg3;
2010 struct timespec uts, *puts;
2013 target_to_host_sigset(&set, target_set);
2016 puts->tv_sec = tswapl(target_uts->tv_sec);
2017 puts->tv_nsec = tswapl(target_uts->tv_nsec);
2021 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
2022 if (!is_error(ret) && target_uinfo) {
2023 host_to_target_siginfo(target_uinfo, &uinfo);
2027 case TARGET_NR_rt_sigqueueinfo:
2030 target_to_host_siginfo(&uinfo, (target_siginfo_t *)arg3);
2031 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
2034 case TARGET_NR_sigreturn:
2035 /* NOTE: ret is eax, so not transcoding must be done */
2036 ret = do_sigreturn(cpu_env);
2038 case TARGET_NR_rt_sigreturn:
2039 /* NOTE: ret is eax, so not transcoding must be done */
2040 ret = do_rt_sigreturn(cpu_env);
2042 case TARGET_NR_sethostname:
2043 ret = get_errno(sethostname((const char *)arg1, arg2));
2045 case TARGET_NR_setrlimit:
2047 /* XXX: convert resource ? */
2048 int resource = arg1;
2049 struct target_rlimit *target_rlim = (void *)arg2;
2051 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
2052 rlim.rlim_max = tswapl(target_rlim->rlim_max);
2053 ret = get_errno(setrlimit(resource, &rlim));
2056 case TARGET_NR_getrlimit:
2058 /* XXX: convert resource ? */
2059 int resource = arg1;
2060 struct target_rlimit *target_rlim = (void *)arg2;
2063 ret = get_errno(getrlimit(resource, &rlim));
2064 if (!is_error(ret)) {
2065 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
2066 target_rlim->rlim_max = tswapl(rlim.rlim_max);
2070 case TARGET_NR_getrusage:
2072 struct rusage rusage;
2073 struct target_rusage *target_rusage = (void *)arg2;
2074 ret = get_errno(getrusage(arg1, &rusage));
2075 if (!is_error(ret)) {
2076 host_to_target_rusage(target_rusage, &rusage);
2080 case TARGET_NR_gettimeofday:
2082 struct target_timeval *target_tv = (void *)arg1;
2084 ret = get_errno(gettimeofday(&tv, NULL));
2085 if (!is_error(ret)) {
2086 host_to_target_timeval(target_tv, &tv);
2090 case TARGET_NR_settimeofday:
2092 struct target_timeval *target_tv = (void *)arg1;
2094 target_to_host_timeval(&tv, target_tv);
2095 ret = get_errno(settimeofday(&tv, NULL));
2098 case TARGET_NR_select:
2100 struct target_sel_arg_struct *sel = (void *)arg1;
2101 sel->n = tswapl(sel->n);
2102 sel->inp = tswapl(sel->inp);
2103 sel->outp = tswapl(sel->outp);
2104 sel->exp = tswapl(sel->exp);
2105 sel->tvp = tswapl(sel->tvp);
2106 ret = do_select(sel->n, (void *)sel->inp, (void *)sel->outp,
2107 (void *)sel->exp, (void *)sel->tvp);
2110 case TARGET_NR_symlink:
2111 ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
2113 #ifdef TARGET_NR_oldlstat
2114 case TARGET_NR_oldlstat:
2117 case TARGET_NR_readlink:
2118 ret = get_errno(readlink(path((const char *)arg1), (char *)arg2, arg3));
2120 case TARGET_NR_uselib:
2122 case TARGET_NR_swapon:
2123 ret = get_errno(swapon((const char *)arg1, arg2));
2125 case TARGET_NR_reboot:
2127 case TARGET_NR_readdir:
2129 case TARGET_NR_mmap:
2130 #if defined(TARGET_I386) || defined(TARGET_ARM)
2132 uint32_t v1, v2, v3, v4, v5, v6, *vptr;
2133 vptr = (uint32_t *)arg1;
2134 v1 = tswap32(vptr[0]);
2135 v2 = tswap32(vptr[1]);
2136 v3 = tswap32(vptr[2]);
2137 v4 = tswap32(vptr[3]);
2138 v5 = tswap32(vptr[4]);
2139 v6 = tswap32(vptr[5]);
2140 ret = get_errno(target_mmap(v1, v2, v3,
2141 target_to_host_bitmask(v4, mmap_flags_tbl),
2145 ret = get_errno(target_mmap(arg1, arg2, arg3,
2146 target_to_host_bitmask(arg4, mmap_flags_tbl),
2151 case TARGET_NR_mmap2:
2152 #if defined(TARGET_SPARC)
2153 #define MMAP_SHIFT 12
2155 #define MMAP_SHIFT TARGET_PAGE_BITS
2157 ret = get_errno(target_mmap(arg1, arg2, arg3,
2158 target_to_host_bitmask(arg4, mmap_flags_tbl),
2160 arg6 << MMAP_SHIFT));
2162 case TARGET_NR_munmap:
2163 ret = get_errno(target_munmap(arg1, arg2));
2165 case TARGET_NR_mprotect:
2166 ret = get_errno(target_mprotect(arg1, arg2, arg3));
2168 case TARGET_NR_mremap:
2169 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
2171 case TARGET_NR_msync:
2172 ret = get_errno(msync((void *)arg1, arg2, arg3));
2174 case TARGET_NR_mlock:
2175 ret = get_errno(mlock((void *)arg1, arg2));
2177 case TARGET_NR_munlock:
2178 ret = get_errno(munlock((void *)arg1, arg2));
2180 case TARGET_NR_mlockall:
2181 ret = get_errno(mlockall(arg1));
2183 case TARGET_NR_munlockall:
2184 ret = get_errno(munlockall());
2186 case TARGET_NR_truncate:
2187 ret = get_errno(truncate((const char *)arg1, arg2));
2189 case TARGET_NR_ftruncate:
2190 ret = get_errno(ftruncate(arg1, arg2));
2192 case TARGET_NR_fchmod:
2193 ret = get_errno(fchmod(arg1, arg2));
2195 case TARGET_NR_getpriority:
2196 ret = get_errno(getpriority(arg1, arg2));
2198 case TARGET_NR_setpriority:
2199 ret = get_errno(setpriority(arg1, arg2, arg3));
2201 #ifdef TARGET_NR_profil
2202 case TARGET_NR_profil:
2205 case TARGET_NR_statfs:
2206 stfs = (void *)arg2;
2207 ret = get_errno(sys_statfs(path((const char *)arg1), stfs));
2209 if (!is_error(ret)) {
2210 tswap32s(&stfs->f_type);
2211 tswap32s(&stfs->f_bsize);
2212 tswap32s(&stfs->f_blocks);
2213 tswap32s(&stfs->f_bfree);
2214 tswap32s(&stfs->f_bavail);
2215 tswap32s(&stfs->f_files);
2216 tswap32s(&stfs->f_ffree);
2217 tswap32s(&stfs->f_fsid.val[0]);
2218 tswap32s(&stfs->f_fsid.val[1]);
2219 tswap32s(&stfs->f_namelen);
2222 case TARGET_NR_fstatfs:
2223 stfs = (void *)arg2;
2224 ret = get_errno(sys_fstatfs(arg1, stfs));
2225 goto convert_statfs;
2226 #ifdef TARGET_NR_ioperm
2227 case TARGET_NR_ioperm:
2230 case TARGET_NR_socketcall:
2231 ret = do_socketcall(arg1, (int32_t *)arg2);
2233 case TARGET_NR_syslog:
2235 case TARGET_NR_setitimer:
2237 struct target_itimerval *target_value = (void *)arg2;
2238 struct target_itimerval *target_ovalue = (void *)arg3;
2239 struct itimerval value, ovalue, *pvalue;
2243 target_to_host_timeval(&pvalue->it_interval,
2244 &target_value->it_interval);
2245 target_to_host_timeval(&pvalue->it_value,
2246 &target_value->it_value);
2250 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
2251 if (!is_error(ret) && target_ovalue) {
2252 host_to_target_timeval(&target_ovalue->it_interval,
2253 &ovalue.it_interval);
2254 host_to_target_timeval(&target_ovalue->it_value,
2259 case TARGET_NR_getitimer:
2261 struct target_itimerval *target_value = (void *)arg2;
2262 struct itimerval value;
2264 ret = get_errno(getitimer(arg1, &value));
2265 if (!is_error(ret) && target_value) {
2266 host_to_target_timeval(&target_value->it_interval,
2267 &value.it_interval);
2268 host_to_target_timeval(&target_value->it_value,
2273 case TARGET_NR_stat:
2274 ret = get_errno(stat(path((const char *)arg1), &st));
2276 case TARGET_NR_lstat:
2277 ret = get_errno(lstat(path((const char *)arg1), &st));
2279 case TARGET_NR_fstat:
2281 ret = get_errno(fstat(arg1, &st));
2283 if (!is_error(ret)) {
2284 struct target_stat *target_st = (void *)arg2;
2285 target_st->st_dev = tswap16(st.st_dev);
2286 target_st->st_ino = tswapl(st.st_ino);
2287 #if defined(TARGET_PPC)
2288 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
2289 target_st->st_uid = tswap32(st.st_uid);
2290 target_st->st_gid = tswap32(st.st_gid);
2292 target_st->st_mode = tswap16(st.st_mode);
2293 target_st->st_uid = tswap16(st.st_uid);
2294 target_st->st_gid = tswap16(st.st_gid);
2296 target_st->st_nlink = tswap16(st.st_nlink);
2297 target_st->st_rdev = tswap16(st.st_rdev);
2298 target_st->st_size = tswapl(st.st_size);
2299 target_st->st_blksize = tswapl(st.st_blksize);
2300 target_st->st_blocks = tswapl(st.st_blocks);
2301 target_st->target_st_atime = tswapl(st.st_atime);
2302 target_st->target_st_mtime = tswapl(st.st_mtime);
2303 target_st->target_st_ctime = tswapl(st.st_ctime);
2307 #ifdef TARGET_NR_olduname
2308 case TARGET_NR_olduname:
2311 #ifdef TARGET_NR_iopl
2312 case TARGET_NR_iopl:
2315 case TARGET_NR_vhangup:
2316 ret = get_errno(vhangup());
2318 #ifdef TARGET_NR_idle
2319 case TARGET_NR_idle:
2322 case TARGET_NR_wait4:
2325 target_long *status_ptr = (void *)arg2;
2326 struct rusage rusage, *rusage_ptr;
2327 struct target_rusage *target_rusage = (void *)arg4;
2329 rusage_ptr = &rusage;
2332 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
2333 if (!is_error(ret)) {
2335 *status_ptr = tswap32(status);
2336 if (target_rusage) {
2337 host_to_target_rusage(target_rusage, &rusage);
2342 case TARGET_NR_swapoff:
2343 ret = get_errno(swapoff((const char *)arg1));
2345 case TARGET_NR_sysinfo:
2348 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
2350 case TARGET_NR_fsync:
2351 ret = get_errno(fsync(arg1));
2353 case TARGET_NR_clone:
2354 ret = get_errno(do_fork(cpu_env, arg1, arg2));
2356 #ifdef __NR_exit_group
2357 /* new thread calls */
2358 case TARGET_NR_exit_group:
2359 ret = get_errno(exit_group(arg1));
2362 case TARGET_NR_setdomainname:
2363 ret = get_errno(setdomainname((const char *)arg1, arg2));
2365 case TARGET_NR_uname:
2366 /* no need to transcode because we use the linux syscall */
2367 ret = get_errno(sys_uname((struct new_utsname *)arg1));
2370 case TARGET_NR_modify_ldt:
2371 ret = get_errno(do_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
2373 case TARGET_NR_vm86old:
2375 case TARGET_NR_vm86:
2376 ret = do_vm86(cpu_env, arg1, (void *)arg2);
2379 case TARGET_NR_adjtimex:
2381 case TARGET_NR_create_module:
2382 case TARGET_NR_init_module:
2383 case TARGET_NR_delete_module:
2384 case TARGET_NR_get_kernel_syms:
2386 case TARGET_NR_quotactl:
2388 case TARGET_NR_getpgid:
2389 ret = get_errno(getpgid(arg1));
2391 case TARGET_NR_fchdir:
2392 ret = get_errno(fchdir(arg1));
2394 case TARGET_NR_bdflush:
2396 case TARGET_NR_sysfs:
2398 case TARGET_NR_personality:
2399 ret = get_errno(personality(arg1));
2401 case TARGET_NR_afs_syscall:
2403 case TARGET_NR__llseek:
2406 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
2407 *(int64_t *)arg4 = tswap64(res);
2410 case TARGET_NR_getdents:
2411 #if TARGET_LONG_SIZE != 4
2412 #error not supported
2413 #elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
2415 struct target_dirent *target_dirp = (void *)arg2;
2416 struct dirent *dirp;
2419 dirp = malloc(count);
2423 ret = get_errno(sys_getdents(arg1, dirp, count));
2424 if (!is_error(ret)) {
2426 struct target_dirent *tde;
2428 int reclen, treclen;
2429 int count1, tnamelen;
2435 reclen = de->d_reclen;
2436 treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
2437 tde->d_reclen = tswap16(treclen);
2438 tde->d_ino = tswapl(de->d_ino);
2439 tde->d_off = tswapl(de->d_off);
2440 tnamelen = treclen - (2 * sizeof(target_long) + 2);
2443 strncpy(tde->d_name, de->d_name, tnamelen);
2444 de = (struct dirent *)((char *)de + reclen);
2446 tde = (struct dirent *)((char *)tde + treclen);
2455 struct dirent *dirp = (void *)arg2;
2458 ret = get_errno(sys_getdents(arg1, dirp, count));
2459 if (!is_error(ret)) {
2465 reclen = de->d_reclen;
2468 de->d_reclen = tswap16(reclen);
2469 tswapls(&de->d_ino);
2470 tswapls(&de->d_off);
2471 de = (struct dirent *)((char *)de + reclen);
2478 case TARGET_NR_getdents64:
2480 struct dirent64 *dirp = (void *)arg2;
2482 ret = get_errno(sys_getdents64(arg1, dirp, count));
2483 if (!is_error(ret)) {
2484 struct dirent64 *de;
2489 reclen = de->d_reclen;
2492 de->d_reclen = tswap16(reclen);
2493 tswap64s(&de->d_ino);
2494 tswap64s(&de->d_off);
2495 de = (struct dirent64 *)((char *)de + reclen);
2501 case TARGET_NR__newselect:
2502 ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4,
2505 case TARGET_NR_poll:
2507 struct target_pollfd *target_pfd = (void *)arg1;
2508 unsigned int nfds = arg2;
2513 pfd = alloca(sizeof(struct pollfd) * nfds);
2514 for(i = 0; i < nfds; i++) {
2515 pfd[i].fd = tswap32(target_pfd[i].fd);
2516 pfd[i].events = tswap16(target_pfd[i].events);
2518 ret = get_errno(poll(pfd, nfds, timeout));
2519 if (!is_error(ret)) {
2520 for(i = 0; i < nfds; i++) {
2521 target_pfd[i].revents = tswap16(pfd[i].revents);
2526 case TARGET_NR_flock:
2527 /* NOTE: the flock constant seems to be the same for every
2529 ret = get_errno(flock(arg1, arg2));
2531 case TARGET_NR_readv:
2536 struct target_iovec *target_vec = (void *)arg2;
2538 vec = alloca(count * sizeof(struct iovec));
2539 for(i = 0;i < count; i++) {
2540 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2541 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2543 ret = get_errno(readv(arg1, vec, count));
2546 case TARGET_NR_writev:
2551 struct target_iovec *target_vec = (void *)arg2;
2553 vec = alloca(count * sizeof(struct iovec));
2554 for(i = 0;i < count; i++) {
2555 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2556 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2558 ret = get_errno(writev(arg1, vec, count));
2561 case TARGET_NR_getsid:
2562 ret = get_errno(getsid(arg1));
2564 case TARGET_NR_fdatasync:
2565 ret = get_errno(fdatasync(arg1));
2567 case TARGET_NR__sysctl:
2569 case TARGET_NR_sched_setparam:
2571 struct sched_param *target_schp = (void *)arg2;
2572 struct sched_param schp;
2573 schp.sched_priority = tswap32(target_schp->sched_priority);
2574 ret = get_errno(sched_setparam(arg1, &schp));
2577 case TARGET_NR_sched_getparam:
2579 struct sched_param *target_schp = (void *)arg2;
2580 struct sched_param schp;
2581 ret = get_errno(sched_getparam(arg1, &schp));
2582 if (!is_error(ret)) {
2583 target_schp->sched_priority = tswap32(schp.sched_priority);
2587 case TARGET_NR_sched_setscheduler:
2589 struct sched_param *target_schp = (void *)arg3;
2590 struct sched_param schp;
2591 schp.sched_priority = tswap32(target_schp->sched_priority);
2592 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
2595 case TARGET_NR_sched_getscheduler:
2596 ret = get_errno(sched_getscheduler(arg1));
2598 case TARGET_NR_sched_yield:
2599 ret = get_errno(sched_yield());
2601 case TARGET_NR_sched_get_priority_max:
2602 ret = get_errno(sched_get_priority_max(arg1));
2604 case TARGET_NR_sched_get_priority_min:
2605 ret = get_errno(sched_get_priority_min(arg1));
2607 case TARGET_NR_sched_rr_get_interval:
2609 struct target_timespec *target_ts = (void *)arg2;
2611 ret = get_errno(sched_rr_get_interval(arg1, &ts));
2612 if (!is_error(ret)) {
2613 target_ts->tv_sec = tswapl(ts.tv_sec);
2614 target_ts->tv_nsec = tswapl(ts.tv_nsec);
2618 case TARGET_NR_nanosleep:
2620 struct target_timespec *target_req = (void *)arg1;
2621 struct target_timespec *target_rem = (void *)arg2;
2622 struct timespec req, rem;
2623 req.tv_sec = tswapl(target_req->tv_sec);
2624 req.tv_nsec = tswapl(target_req->tv_nsec);
2625 ret = get_errno(nanosleep(&req, &rem));
2626 if (is_error(ret) && target_rem) {
2627 target_rem->tv_sec = tswapl(rem.tv_sec);
2628 target_rem->tv_nsec = tswapl(rem.tv_nsec);
2632 case TARGET_NR_query_module:
2634 case TARGET_NR_nfsservctl:
2636 case TARGET_NR_prctl:
2638 #ifdef TARGET_NR_pread
2639 case TARGET_NR_pread:
2640 page_unprotect_range((void *)arg2, arg3);
2641 ret = get_errno(pread(arg1, (void *)arg2, arg3, arg4));
2643 case TARGET_NR_pwrite:
2644 ret = get_errno(pwrite(arg1, (void *)arg2, arg3, arg4));
2647 case TARGET_NR_getcwd:
2648 ret = get_errno(sys_getcwd1((char *)arg1, arg2));
2650 case TARGET_NR_capget:
2652 case TARGET_NR_capset:
2654 case TARGET_NR_sigaltstack:
2656 case TARGET_NR_sendfile:
2658 #ifdef TARGET_NR_getpmsg
2659 case TARGET_NR_getpmsg:
2662 #ifdef TARGET_NR_putpmsg
2663 case TARGET_NR_putpmsg:
2666 case TARGET_NR_vfork:
2667 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
2669 #ifdef TARGET_NR_ugetrlimit
2670 case TARGET_NR_ugetrlimit:
2673 ret = get_errno(getrlimit(arg1, &rlim));
2674 if (!is_error(ret)) {
2675 struct target_rlimit *target_rlim = (void *)arg2;
2676 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
2677 target_rlim->rlim_max = tswapl(rlim.rlim_max);
2682 case TARGET_NR_truncate64:
2684 case TARGET_NR_ftruncate64:
2686 case TARGET_NR_stat64:
2687 ret = get_errno(stat(path((const char *)arg1), &st));
2689 case TARGET_NR_lstat64:
2690 ret = get_errno(lstat(path((const char *)arg1), &st));
2692 case TARGET_NR_fstat64:
2694 ret = get_errno(fstat(arg1, &st));
2696 if (!is_error(ret)) {
2697 struct target_stat64 *target_st = (void *)arg2;
2698 memset(target_st, 0, sizeof(struct target_stat64));
2699 put_user(st.st_dev, &target_st->st_dev);
2700 put_user(st.st_ino, &target_st->st_ino);
2701 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
2702 put_user(st.st_ino, &target_st->__st_ino);
2704 put_user(st.st_mode, &target_st->st_mode);
2705 put_user(st.st_nlink, &target_st->st_nlink);
2706 put_user(st.st_uid, &target_st->st_uid);
2707 put_user(st.st_gid, &target_st->st_gid);
2708 put_user(st.st_rdev, &target_st->st_rdev);
2709 /* XXX: better use of kernel struct */
2710 put_user(st.st_size, &target_st->st_size);
2711 put_user(st.st_blksize, &target_st->st_blksize);
2712 put_user(st.st_blocks, &target_st->st_blocks);
2713 put_user(st.st_atime, &target_st->target_st_atime);
2714 put_user(st.st_mtime, &target_st->target_st_mtime);
2715 put_user(st.st_ctime, &target_st->target_st_ctime);
2721 case TARGET_NR_lchown:
2722 ret = get_errno(lchown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
2724 case TARGET_NR_getuid:
2725 ret = get_errno(high2lowuid(getuid()));
2727 case TARGET_NR_getgid:
2728 ret = get_errno(high2lowgid(getgid()));
2730 case TARGET_NR_geteuid:
2731 ret = get_errno(high2lowuid(geteuid()));
2733 case TARGET_NR_getegid:
2734 ret = get_errno(high2lowgid(getegid()));
2736 case TARGET_NR_setreuid:
2737 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
2739 case TARGET_NR_setregid:
2740 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
2742 case TARGET_NR_getgroups:
2744 int gidsetsize = arg1;
2745 uint16_t *target_grouplist = (void *)arg2;
2749 grouplist = alloca(gidsetsize * sizeof(gid_t));
2750 ret = get_errno(getgroups(gidsetsize, grouplist));
2751 if (!is_error(ret)) {
2752 for(i = 0;i < gidsetsize; i++)
2753 target_grouplist[i] = tswap16(grouplist[i]);
2757 case TARGET_NR_setgroups:
2759 int gidsetsize = arg1;
2760 uint16_t *target_grouplist = (void *)arg2;
2764 grouplist = alloca(gidsetsize * sizeof(gid_t));
2765 for(i = 0;i < gidsetsize; i++)
2766 grouplist[i] = tswap16(target_grouplist[i]);
2767 ret = get_errno(setgroups(gidsetsize, grouplist));
2770 case TARGET_NR_fchown:
2771 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
2773 #ifdef TARGET_NR_setresuid
2774 case TARGET_NR_setresuid:
2775 ret = get_errno(setresuid(low2highuid(arg1),
2777 low2highuid(arg3)));
2780 #ifdef TARGET_NR_getresuid
2781 case TARGET_NR_getresuid:
2783 int ruid, euid, suid;
2784 ret = get_errno(getresuid(&ruid, &euid, &suid));
2785 if (!is_error(ret)) {
2786 *(uint16_t *)arg1 = tswap16(high2lowuid(ruid));
2787 *(uint16_t *)arg2 = tswap16(high2lowuid(euid));
2788 *(uint16_t *)arg3 = tswap16(high2lowuid(suid));
2793 #ifdef TARGET_NR_getresgid
2794 case TARGET_NR_setresgid:
2795 ret = get_errno(setresgid(low2highgid(arg1),
2797 low2highgid(arg3)));
2800 #ifdef TARGET_NR_getresgid
2801 case TARGET_NR_getresgid:
2803 int rgid, egid, sgid;
2804 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2805 if (!is_error(ret)) {
2806 *(uint16_t *)arg1 = tswap16(high2lowgid(rgid));
2807 *(uint16_t *)arg2 = tswap16(high2lowgid(egid));
2808 *(uint16_t *)arg3 = tswap16(high2lowgid(sgid));
2813 case TARGET_NR_chown:
2814 ret = get_errno(chown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
2816 case TARGET_NR_setuid:
2817 ret = get_errno(setuid(low2highuid(arg1)));
2819 case TARGET_NR_setgid:
2820 ret = get_errno(setgid(low2highgid(arg1)));
2822 case TARGET_NR_setfsuid:
2823 ret = get_errno(setfsuid(arg1));
2825 case TARGET_NR_setfsgid:
2826 ret = get_errno(setfsgid(arg1));
2828 #endif /* USE_UID16 */
2830 case TARGET_NR_lchown32:
2831 ret = get_errno(lchown((const char *)arg1, arg2, arg3));
2833 case TARGET_NR_getuid32:
2834 ret = get_errno(getuid());
2836 case TARGET_NR_getgid32:
2837 ret = get_errno(getgid());
2839 case TARGET_NR_geteuid32:
2840 ret = get_errno(geteuid());
2842 case TARGET_NR_getegid32:
2843 ret = get_errno(getegid());
2845 case TARGET_NR_setreuid32:
2846 ret = get_errno(setreuid(arg1, arg2));
2848 case TARGET_NR_setregid32:
2849 ret = get_errno(setregid(arg1, arg2));
2851 case TARGET_NR_getgroups32:
2853 case TARGET_NR_setgroups32:
2855 case TARGET_NR_fchown32:
2856 ret = get_errno(fchown(arg1, arg2, arg3));
2858 case TARGET_NR_setresuid32:
2859 ret = get_errno(setresuid(arg1, arg2, arg3));
2861 case TARGET_NR_getresuid32:
2863 int ruid, euid, suid;
2864 ret = get_errno(getresuid(&ruid, &euid, &suid));
2865 if (!is_error(ret)) {
2866 *(uint32_t *)arg1 = tswap32(ruid);
2867 *(uint32_t *)arg2 = tswap32(euid);
2868 *(uint32_t *)arg3 = tswap32(suid);
2872 case TARGET_NR_setresgid32:
2873 ret = get_errno(setresgid(arg1, arg2, arg3));
2875 case TARGET_NR_getresgid32:
2877 int rgid, egid, sgid;
2878 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2879 if (!is_error(ret)) {
2880 *(uint32_t *)arg1 = tswap32(rgid);
2881 *(uint32_t *)arg2 = tswap32(egid);
2882 *(uint32_t *)arg3 = tswap32(sgid);
2886 case TARGET_NR_chown32:
2887 ret = get_errno(chown((const char *)arg1, arg2, arg3));
2889 case TARGET_NR_setuid32:
2890 ret = get_errno(setuid(arg1));
2892 case TARGET_NR_setgid32:
2893 ret = get_errno(setgid(arg1));
2895 case TARGET_NR_setfsuid32:
2896 ret = get_errno(setfsuid(arg1));
2898 case TARGET_NR_setfsgid32:
2899 ret = get_errno(setfsgid(arg1));
2902 case TARGET_NR_pivot_root:
2904 #ifdef TARGET_NR_mincore
2905 case TARGET_NR_mincore:
2908 #ifdef TARGET_NR_madvise
2909 case TARGET_NR_madvise:
2912 #if TARGET_LONG_BITS == 32
2913 case TARGET_NR_fcntl64:
2916 struct target_flock64 *target_fl = (void *)arg3;
2920 ret = get_errno(fcntl(arg1, arg2, &fl));
2922 target_fl->l_type = tswap16(fl.l_type);
2923 target_fl->l_whence = tswap16(fl.l_whence);
2924 target_fl->l_start = tswap64(fl.l_start);
2925 target_fl->l_len = tswap64(fl.l_len);
2926 target_fl->l_pid = tswapl(fl.l_pid);
2932 fl.l_type = tswap16(target_fl->l_type);
2933 fl.l_whence = tswap16(target_fl->l_whence);
2934 fl.l_start = tswap64(target_fl->l_start);
2935 fl.l_len = tswap64(target_fl->l_len);
2936 fl.l_pid = tswapl(target_fl->l_pid);
2937 ret = get_errno(fcntl(arg1, arg2, &fl));
2940 ret = get_errno(do_fcntl(arg1, arg2, arg3));
2946 #ifdef TARGET_NR_security
2947 case TARGET_NR_security:
2950 #ifdef TARGET_NR_getpagesize
2951 case TARGET_NR_getpagesize:
2952 ret = TARGET_PAGE_SIZE;
2955 case TARGET_NR_gettid:
2956 ret = get_errno(gettid());
2958 case TARGET_NR_readahead:
2960 #ifdef TARGET_NR_setxattr
2961 case TARGET_NR_setxattr:
2962 case TARGET_NR_lsetxattr:
2963 case TARGET_NR_fsetxattr:
2964 case TARGET_NR_getxattr:
2965 case TARGET_NR_lgetxattr:
2966 case TARGET_NR_fgetxattr:
2967 case TARGET_NR_listxattr:
2968 case TARGET_NR_llistxattr:
2969 case TARGET_NR_flistxattr:
2970 case TARGET_NR_removexattr:
2971 case TARGET_NR_lremovexattr:
2972 case TARGET_NR_fremovexattr:
2973 goto unimplemented_nowarn;
2975 #ifdef TARGET_NR_set_thread_area
2976 case TARGET_NR_set_thread_area:
2977 case TARGET_NR_get_thread_area:
2978 goto unimplemented_nowarn;
2982 gemu_log("qemu: Unsupported syscall: %d\n", num);
2983 unimplemented_nowarn:
2989 gemu_log(" = %ld\n", ret);