xref: /openbmc/qemu/linux-user/syscall.c (revision 048f6b4d)
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>
23 #include <string.h>
24 #include <elf.h>
25 #include <endian.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <time.h>
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>
42 #include <sys/poll.h>
43 #include <sys/times.h>
44 #include <sys/shm.h>
45 #include <utime.h>
46 #include <sys/sysinfo.h>
47 //#include <sys/user.h>
48 #include <netinet/ip.h>
49 #include <netinet/tcp.h>
50 
51 #define termios host_termios
52 #define winsize host_winsize
53 #define termio host_termio
54 #define sgttyb host_sgttyb /* same as target */
55 #define tchars host_tchars /* same as target */
56 #define ltchars host_ltchars /* same as target */
57 
58 #include <linux/termios.h>
59 #include <linux/unistd.h>
60 #include <linux/utsname.h>
61 #include <linux/cdrom.h>
62 #include <linux/hdreg.h>
63 #include <linux/soundcard.h>
64 #include <linux/dirent.h>
65 #include <linux/kd.h>
66 
67 #include "qemu.h"
68 
69 //#define DEBUG
70 
71 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC)
72 /* 16 bit uid wrappers emulation */
73 #define USE_UID16
74 #endif
75 
76 //#include <linux/msdos_fs.h>
77 #define	VFAT_IOCTL_READDIR_BOTH		_IOR('r', 1, struct dirent [2])
78 #define	VFAT_IOCTL_READDIR_SHORT	_IOR('r', 2, struct dirent [2])
79 
80 
81 #if defined(__powerpc__)
82 #undef __syscall_nr
83 #undef __sc_loadargs_0
84 #undef __sc_loadargs_1
85 #undef __sc_loadargs_2
86 #undef __sc_loadargs_3
87 #undef __sc_loadargs_4
88 #undef __sc_loadargs_5
89 #undef __sc_asm_input_0
90 #undef __sc_asm_input_1
91 #undef __sc_asm_input_2
92 #undef __sc_asm_input_3
93 #undef __sc_asm_input_4
94 #undef __sc_asm_input_5
95 #undef _syscall0
96 #undef _syscall1
97 #undef _syscall2
98 #undef _syscall3
99 #undef _syscall4
100 #undef _syscall5
101 
102 /* need to redefine syscalls as Linux kernel defines are incorrect for
103    the clobber list */
104 /* On powerpc a system call basically clobbers the same registers like a
105  * function call, with the exception of LR (which is needed for the
106  * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
107  * an error return status).
108  */
109 
110 #define __syscall_nr(nr, type, name, args...)				\
111 	unsigned long __sc_ret, __sc_err;				\
112 	{								\
113 		register unsigned long __sc_0  __asm__ ("r0");		\
114 		register unsigned long __sc_3  __asm__ ("r3");		\
115 		register unsigned long __sc_4  __asm__ ("r4");		\
116 		register unsigned long __sc_5  __asm__ ("r5");		\
117 		register unsigned long __sc_6  __asm__ ("r6");		\
118 		register unsigned long __sc_7  __asm__ ("r7");		\
119 									\
120 		__sc_loadargs_##nr(name, args);				\
121 		__asm__ __volatile__					\
122 			("sc           \n\t"				\
123 			 "mfcr %0      "				\
124 			: "=&r" (__sc_0),				\
125 			  "=&r" (__sc_3),  "=&r" (__sc_4),		\
126 			  "=&r" (__sc_5),  "=&r" (__sc_6),		\
127 			  "=&r" (__sc_7)				\
128 			: __sc_asm_input_##nr				\
129 			: "cr0", "ctr", "memory",			\
130 			  "r8", "r9", "r10","r11", "r12");		\
131 		__sc_ret = __sc_3;					\
132 		__sc_err = __sc_0;					\
133 	}								\
134 	if (__sc_err & 0x10000000)					\
135 	{								\
136 		errno = __sc_ret;					\
137 		__sc_ret = -1;						\
138 	}								\
139 	return (type) __sc_ret
140 
141 #define __sc_loadargs_0(name, dummy...)					\
142 	__sc_0 = __NR_##name
143 #define __sc_loadargs_1(name, arg1)					\
144 	__sc_loadargs_0(name);						\
145 	__sc_3 = (unsigned long) (arg1)
146 #define __sc_loadargs_2(name, arg1, arg2)				\
147 	__sc_loadargs_1(name, arg1);					\
148 	__sc_4 = (unsigned long) (arg2)
149 #define __sc_loadargs_3(name, arg1, arg2, arg3)				\
150 	__sc_loadargs_2(name, arg1, arg2);				\
151 	__sc_5 = (unsigned long) (arg3)
152 #define __sc_loadargs_4(name, arg1, arg2, arg3, arg4)			\
153 	__sc_loadargs_3(name, arg1, arg2, arg3);			\
154 	__sc_6 = (unsigned long) (arg4)
155 #define __sc_loadargs_5(name, arg1, arg2, arg3, arg4, arg5)		\
156 	__sc_loadargs_4(name, arg1, arg2, arg3, arg4);			\
157 	__sc_7 = (unsigned long) (arg5)
158 
159 #define __sc_asm_input_0 "0" (__sc_0)
160 #define __sc_asm_input_1 __sc_asm_input_0, "1" (__sc_3)
161 #define __sc_asm_input_2 __sc_asm_input_1, "2" (__sc_4)
162 #define __sc_asm_input_3 __sc_asm_input_2, "3" (__sc_5)
163 #define __sc_asm_input_4 __sc_asm_input_3, "4" (__sc_6)
164 #define __sc_asm_input_5 __sc_asm_input_4, "5" (__sc_7)
165 
166 #define _syscall0(type,name)						\
167 type name(void)								\
168 {									\
169 	__syscall_nr(0, type, name);					\
170 }
171 
172 #define _syscall1(type,name,type1,arg1)					\
173 type name(type1 arg1)							\
174 {									\
175 	__syscall_nr(1, type, name, arg1);				\
176 }
177 
178 #define _syscall2(type,name,type1,arg1,type2,arg2)			\
179 type name(type1 arg1, type2 arg2)					\
180 {									\
181 	__syscall_nr(2, type, name, arg1, arg2);			\
182 }
183 
184 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)		\
185 type name(type1 arg1, type2 arg2, type3 arg3)				\
186 {									\
187 	__syscall_nr(3, type, name, arg1, arg2, arg3);			\
188 }
189 
190 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
191 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4)		\
192 {									\
193 	__syscall_nr(4, type, name, arg1, arg2, arg3, arg4);		\
194 }
195 
196 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
197 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)	\
198 {									\
199 	__syscall_nr(5, type, name, arg1, arg2, arg3, arg4, arg5);	\
200 }
201 #endif
202 
203 #define __NR_sys_uname __NR_uname
204 #define __NR_sys_getcwd1 __NR_getcwd
205 #define __NR_sys_statfs __NR_statfs
206 #define __NR_sys_fstatfs __NR_fstatfs
207 #define __NR_sys_getdents __NR_getdents
208 #define __NR_sys_getdents64 __NR_getdents64
209 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
210 
211 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
212 #define __NR__llseek __NR_lseek
213 #endif
214 
215 #ifdef __NR_gettid
216 _syscall0(int, gettid)
217 #else
218 static int gettid(void) {
219     return -ENOSYS;
220 }
221 #endif
222 _syscall1(int,sys_uname,struct new_utsname *,buf)
223 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
224 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
225 _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
226 _syscall5(int, _llseek,  uint,  fd, ulong, hi, ulong, lo,
227           loff_t *, res, uint, wh);
228 _syscall2(int,sys_statfs,const char *,path,struct kernel_statfs *,buf)
229 _syscall2(int,sys_fstatfs,int,fd,struct kernel_statfs *,buf)
230 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
231 #ifdef __NR_exit_group
232 _syscall1(int,exit_group,int,error_code)
233 #endif
234 
235 extern int personality(int);
236 extern int flock(int, int);
237 extern int setfsuid(int);
238 extern int setfsgid(int);
239 extern int setresuid(uid_t, uid_t, uid_t);
240 extern int getresuid(uid_t *, uid_t *, uid_t *);
241 extern int setresgid(gid_t, gid_t, gid_t);
242 extern int getresgid(gid_t *, gid_t *, gid_t *);
243 extern int setgroups(int, gid_t *);
244 
245 static inline long get_errno(long ret)
246 {
247     if (ret == -1)
248         return -errno;
249     else
250         return ret;
251 }
252 
253 static inline int is_error(long ret)
254 {
255     return (unsigned long)ret >= (unsigned long)(-4096);
256 }
257 
258 static char *target_brk;
259 static char *target_original_brk;
260 
261 void target_set_brk(char *new_brk)
262 {
263     target_brk = new_brk;
264     target_original_brk = new_brk;
265 }
266 
267 long do_brk(char *new_brk)
268 {
269     char *brk_page;
270     long mapped_addr;
271     int	new_alloc_size;
272 
273     if (!new_brk)
274         return (long)target_brk;
275     if (new_brk < target_original_brk)
276         return -ENOMEM;
277 
278     brk_page = (char *)HOST_PAGE_ALIGN((unsigned long)target_brk);
279 
280     /* If the new brk is less than this, set it and we're done... */
281     if (new_brk < brk_page) {
282 	target_brk = new_brk;
283     	return (long)target_brk;
284     }
285 
286     /* We need to allocate more memory after the brk... */
287     new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
288     mapped_addr = get_errno(target_mmap((unsigned long)brk_page, new_alloc_size,
289                                         PROT_READ|PROT_WRITE,
290                                         MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
291     if (is_error(mapped_addr)) {
292 	return mapped_addr;
293     } else {
294 	target_brk = new_brk;
295     	return (long)target_brk;
296     }
297 }
298 
299 static inline fd_set *target_to_host_fds(fd_set *fds,
300                                          target_long *target_fds, int n)
301 {
302 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
303     return (fd_set *)target_fds;
304 #else
305     int i, b;
306     if (target_fds) {
307         FD_ZERO(fds);
308         for(i = 0;i < n; i++) {
309             b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
310                  (i & (TARGET_LONG_BITS - 1))) & 1;
311             if (b)
312                 FD_SET(i, fds);
313         }
314         return fds;
315     } else {
316         return NULL;
317     }
318 #endif
319 }
320 
321 static inline void host_to_target_fds(target_long *target_fds,
322                                       fd_set *fds, int n)
323 {
324 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
325     /* nothing to do */
326 #else
327     int i, nw, j, k;
328     target_long v;
329 
330     if (target_fds) {
331         nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
332         k = 0;
333         for(i = 0;i < nw; i++) {
334             v = 0;
335             for(j = 0; j < TARGET_LONG_BITS; j++) {
336                 v |= ((FD_ISSET(k, fds) != 0) << j);
337                 k++;
338             }
339             target_fds[i] = tswapl(v);
340         }
341     }
342 #endif
343 }
344 
345 #if defined(__alpha__)
346 #define HOST_HZ 1024
347 #else
348 #define HOST_HZ 100
349 #endif
350 
351 static inline long host_to_target_clock_t(long ticks)
352 {
353 #if HOST_HZ == TARGET_HZ
354     return ticks;
355 #else
356     return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
357 #endif
358 }
359 
360 static inline void host_to_target_rusage(struct target_rusage *target_rusage,
361                                          const struct rusage *rusage)
362 {
363     target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
364     target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
365     target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
366     target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
367     target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
368     target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
369     target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
370     target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
371     target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
372     target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
373     target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
374     target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
375     target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
376     target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
377     target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
378     target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
379     target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
380     target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
381 }
382 
383 static inline void target_to_host_timeval(struct timeval *tv,
384                                           const struct target_timeval *target_tv)
385 {
386     tv->tv_sec = tswapl(target_tv->tv_sec);
387     tv->tv_usec = tswapl(target_tv->tv_usec);
388 }
389 
390 static inline void host_to_target_timeval(struct target_timeval *target_tv,
391                                           const struct timeval *tv)
392 {
393     target_tv->tv_sec = tswapl(tv->tv_sec);
394     target_tv->tv_usec = tswapl(tv->tv_usec);
395 }
396 
397 
398 static long do_select(long n,
399                       target_long *target_rfds, target_long *target_wfds,
400                       target_long *target_efds, struct target_timeval *target_tv)
401 {
402     fd_set rfds, wfds, efds;
403     fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
404     struct timeval tv, *tv_ptr;
405     long ret;
406 
407     rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
408     wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
409     efds_ptr = target_to_host_fds(&efds, target_efds, n);
410 
411     if (target_tv) {
412         target_to_host_timeval(&tv, target_tv);
413         tv_ptr = &tv;
414     } else {
415         tv_ptr = NULL;
416     }
417     ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
418     if (!is_error(ret)) {
419         host_to_target_fds(target_rfds, rfds_ptr, n);
420         host_to_target_fds(target_wfds, wfds_ptr, n);
421         host_to_target_fds(target_efds, efds_ptr, n);
422 
423         if (target_tv) {
424             host_to_target_timeval(target_tv, &tv);
425         }
426     }
427     return ret;
428 }
429 
430 static inline void target_to_host_sockaddr(struct sockaddr *addr,
431                                            struct target_sockaddr *target_addr,
432                                            socklen_t len)
433 {
434     memcpy(addr, target_addr, len);
435     addr->sa_family = tswap16(target_addr->sa_family);
436 }
437 
438 static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
439                                            struct sockaddr *addr,
440                                            socklen_t len)
441 {
442     memcpy(target_addr, addr, len);
443     target_addr->sa_family = tswap16(addr->sa_family);
444 }
445 
446 static inline void target_to_host_cmsg(struct msghdr *msgh,
447                                        struct target_msghdr *target_msgh)
448 {
449     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
450     struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
451     socklen_t space = 0;
452 
453     while (cmsg && target_cmsg) {
454         void *data = CMSG_DATA(cmsg);
455         void *target_data = TARGET_CMSG_DATA(target_cmsg);
456 
457         int len = tswapl(target_cmsg->cmsg_len)
458                   - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
459 
460         space += CMSG_SPACE(len);
461         if (space > msgh->msg_controllen) {
462             space -= CMSG_SPACE(len);
463             gemu_log("Host cmsg overflow");
464             break;
465         }
466 
467         cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
468         cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
469         cmsg->cmsg_len = CMSG_LEN(len);
470 
471         if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
472             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
473             memcpy(data, target_data, len);
474         } else {
475             int *fd = (int *)data;
476             int *target_fd = (int *)target_data;
477             int i, numfds = len / sizeof(int);
478 
479             for (i = 0; i < numfds; i++)
480                 fd[i] = tswap32(target_fd[i]);
481         }
482 
483         cmsg = CMSG_NXTHDR(msgh, cmsg);
484         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
485     }
486 
487     msgh->msg_controllen = space;
488 }
489 
490 static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
491                                        struct msghdr *msgh)
492 {
493     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
494     struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
495     socklen_t space = 0;
496 
497     while (cmsg && target_cmsg) {
498         void *data = CMSG_DATA(cmsg);
499         void *target_data = TARGET_CMSG_DATA(target_cmsg);
500 
501         int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
502 
503         space += TARGET_CMSG_SPACE(len);
504         if (space > tswapl(target_msgh->msg_controllen)) {
505             space -= TARGET_CMSG_SPACE(len);
506             gemu_log("Target cmsg overflow");
507             break;
508         }
509 
510         target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
511         target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
512         target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
513 
514         if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
515             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
516             memcpy(target_data, data, len);
517         } else {
518             int *fd = (int *)data;
519             int *target_fd = (int *)target_data;
520             int i, numfds = len / sizeof(int);
521 
522             for (i = 0; i < numfds; i++)
523                 target_fd[i] = tswap32(fd[i]);
524         }
525 
526         cmsg = CMSG_NXTHDR(msgh, cmsg);
527         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
528     }
529 
530     msgh->msg_controllen = tswapl(space);
531 }
532 
533 static long do_setsockopt(int sockfd, int level, int optname,
534                           void *optval, socklen_t optlen)
535 {
536     int val, ret;
537 
538     switch(level) {
539     case SOL_TCP:
540         /* TCP options all take an 'int' value.  */
541         if (optlen < sizeof(uint32_t))
542             return -EINVAL;
543 
544         if (get_user(val, (uint32_t *)optval))
545             return -EFAULT;
546         ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
547         break;
548     case SOL_IP:
549         switch(optname) {
550         case IP_TOS:
551         case IP_TTL:
552         case IP_HDRINCL:
553         case IP_ROUTER_ALERT:
554         case IP_RECVOPTS:
555         case IP_RETOPTS:
556         case IP_PKTINFO:
557         case IP_MTU_DISCOVER:
558         case IP_RECVERR:
559         case IP_RECVTOS:
560 #ifdef IP_FREEBIND
561         case IP_FREEBIND:
562 #endif
563         case IP_MULTICAST_TTL:
564         case IP_MULTICAST_LOOP:
565             val = 0;
566             if (optlen >= sizeof(uint32_t)) {
567                 if (get_user(val, (uint32_t *)optval))
568                     return -EFAULT;
569             } else if (optlen >= 1) {
570                 if (get_user(val, (uint8_t *)optval))
571                     return -EFAULT;
572             }
573             ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
574             break;
575         default:
576             goto unimplemented;
577         }
578         break;
579     case SOL_SOCKET:
580         switch (optname) {
581             /* Options with 'int' argument.  */
582         case SO_DEBUG:
583         case SO_REUSEADDR:
584         case SO_TYPE:
585         case SO_ERROR:
586         case SO_DONTROUTE:
587         case SO_BROADCAST:
588         case SO_SNDBUF:
589         case SO_RCVBUF:
590         case SO_KEEPALIVE:
591         case SO_OOBINLINE:
592         case SO_NO_CHECK:
593         case SO_PRIORITY:
594 #ifdef SO_BSDCOMPAT
595         case SO_BSDCOMPAT:
596 #endif
597         case SO_PASSCRED:
598         case SO_TIMESTAMP:
599         case SO_RCVLOWAT:
600         case SO_RCVTIMEO:
601         case SO_SNDTIMEO:
602             if (optlen < sizeof(uint32_t))
603                 return -EINVAL;
604             if (get_user(val, (uint32_t *)optval))
605                 return -EFAULT;
606             ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
607             break;
608         default:
609             goto unimplemented;
610         }
611         break;
612     default:
613     unimplemented:
614         gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
615         ret = -ENOSYS;
616     }
617     return ret;
618 }
619 
620 static long do_getsockopt(int sockfd, int level, int optname,
621                           void *optval, socklen_t *optlen)
622 {
623     int len, lv, val, ret;
624 
625     switch(level) {
626     case SOL_SOCKET:
627 	switch (optname) {
628 	case SO_LINGER:
629 	case SO_RCVTIMEO:
630 	case SO_SNDTIMEO:
631 	case SO_PEERCRED:
632 	case SO_PEERNAME:
633 	    /* These don't just return a single integer */
634 	    goto unimplemented;
635         default:
636             goto int_case;
637         }
638         break;
639     case SOL_TCP:
640         /* TCP options all take an 'int' value.  */
641     int_case:
642         if (get_user(len, optlen))
643             return -EFAULT;
644         if (len < 0)
645             return -EINVAL;
646         lv = sizeof(int);
647         ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
648         if (ret < 0)
649             return ret;
650         val = tswap32(val);
651         if (len > lv)
652             len = lv;
653         if (copy_to_user(optval, &val, len))
654             return -EFAULT;
655         if (put_user(len, optlen))
656             return -EFAULT;
657         break;
658     case SOL_IP:
659         switch(optname) {
660         case IP_TOS:
661         case IP_TTL:
662         case IP_HDRINCL:
663         case IP_ROUTER_ALERT:
664         case IP_RECVOPTS:
665         case IP_RETOPTS:
666         case IP_PKTINFO:
667         case IP_MTU_DISCOVER:
668         case IP_RECVERR:
669         case IP_RECVTOS:
670 #ifdef IP_FREEBIND
671         case IP_FREEBIND:
672 #endif
673         case IP_MULTICAST_TTL:
674         case IP_MULTICAST_LOOP:
675             if (get_user(len, optlen))
676                 return -EFAULT;
677             if (len < 0)
678                 return -EINVAL;
679             lv = sizeof(int);
680             ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
681             if (ret < 0)
682                 return ret;
683             if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
684                 unsigned char ucval = val;
685                 len = 1;
686 		if (put_user(len, optlen))
687                     return -EFAULT;
688 		if (copy_to_user(optval,&ucval,1))
689                     return -EFAULT;
690             } else {
691                 val = tswap32(val);
692                 if (len > sizeof(int))
693                     len = sizeof(int);
694                 if (put_user(len, optlen))
695                     return -EFAULT;
696                 if (copy_to_user(optval, &val, len))
697                     return -EFAULT;
698             }
699             break;
700         default:
701             goto unimplemented;
702         }
703         break;
704     default:
705     unimplemented:
706         gemu_log("getsockopt level=%d optname=%d not yet supported\n",
707                  level, optname);
708         ret = -ENOSYS;
709         break;
710     }
711     return ret;
712 }
713 
714 static long do_socketcall(int num, int32_t *vptr)
715 {
716     long ret;
717 
718     switch(num) {
719     case SOCKOP_socket:
720 	{
721             int domain = tswap32(vptr[0]);
722             int type = tswap32(vptr[1]);
723             int protocol = tswap32(vptr[2]);
724 
725             ret = get_errno(socket(domain, type, protocol));
726 	}
727         break;
728     case SOCKOP_bind:
729 	{
730             int sockfd = tswap32(vptr[0]);
731             void *target_addr = (void *)tswap32(vptr[1]);
732             socklen_t addrlen = tswap32(vptr[2]);
733             void *addr = alloca(addrlen);
734 
735             target_to_host_sockaddr(addr, target_addr, addrlen);
736             ret = get_errno(bind(sockfd, addr, addrlen));
737         }
738         break;
739     case SOCKOP_connect:
740         {
741             int sockfd = tswap32(vptr[0]);
742             void *target_addr = (void *)tswap32(vptr[1]);
743             socklen_t addrlen = tswap32(vptr[2]);
744             void *addr = alloca(addrlen);
745 
746             target_to_host_sockaddr(addr, target_addr, addrlen);
747             ret = get_errno(connect(sockfd, addr, addrlen));
748         }
749         break;
750     case SOCKOP_listen:
751         {
752             int sockfd = tswap32(vptr[0]);
753             int backlog = tswap32(vptr[1]);
754 
755             ret = get_errno(listen(sockfd, backlog));
756         }
757         break;
758     case SOCKOP_accept:
759         {
760             int sockfd = tswap32(vptr[0]);
761             void *target_addr = (void *)tswap32(vptr[1]);
762             uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
763             socklen_t addrlen = tswap32(*target_addrlen);
764             void *addr = alloca(addrlen);
765 
766             ret = get_errno(accept(sockfd, addr, &addrlen));
767             if (!is_error(ret)) {
768                 host_to_target_sockaddr(target_addr, addr, addrlen);
769                 *target_addrlen = tswap32(addrlen);
770             }
771         }
772         break;
773     case SOCKOP_getsockname:
774         {
775             int sockfd = tswap32(vptr[0]);
776             void *target_addr = (void *)tswap32(vptr[1]);
777             uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
778             socklen_t addrlen = tswap32(*target_addrlen);
779             void *addr = alloca(addrlen);
780 
781             ret = get_errno(getsockname(sockfd, addr, &addrlen));
782             if (!is_error(ret)) {
783                 host_to_target_sockaddr(target_addr, addr, addrlen);
784                 *target_addrlen = tswap32(addrlen);
785             }
786         }
787         break;
788     case SOCKOP_getpeername:
789         {
790             int sockfd = tswap32(vptr[0]);
791             void *target_addr = (void *)tswap32(vptr[1]);
792             uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
793             socklen_t addrlen = tswap32(*target_addrlen);
794             void *addr = alloca(addrlen);
795 
796             ret = get_errno(getpeername(sockfd, addr, &addrlen));
797             if (!is_error(ret)) {
798                 host_to_target_sockaddr(target_addr, addr, addrlen);
799                 *target_addrlen = tswap32(addrlen);
800             }
801         }
802         break;
803     case SOCKOP_socketpair:
804         {
805             int domain = tswap32(vptr[0]);
806             int type = tswap32(vptr[1]);
807             int protocol = tswap32(vptr[2]);
808             int32_t *target_tab = (void *)tswap32(vptr[3]);
809             int tab[2];
810 
811             ret = get_errno(socketpair(domain, type, protocol, tab));
812             if (!is_error(ret)) {
813                 target_tab[0] = tswap32(tab[0]);
814                 target_tab[1] = tswap32(tab[1]);
815             }
816         }
817         break;
818     case SOCKOP_send:
819         {
820             int sockfd = tswap32(vptr[0]);
821             void *msg = (void *)tswap32(vptr[1]);
822             size_t len = tswap32(vptr[2]);
823             int flags = tswap32(vptr[3]);
824 
825             ret = get_errno(send(sockfd, msg, len, flags));
826         }
827         break;
828     case SOCKOP_recv:
829         {
830             int sockfd = tswap32(vptr[0]);
831             void *msg = (void *)tswap32(vptr[1]);
832             size_t len = tswap32(vptr[2]);
833             int flags = tswap32(vptr[3]);
834 
835             ret = get_errno(recv(sockfd, msg, len, flags));
836         }
837         break;
838     case SOCKOP_sendto:
839         {
840             int sockfd = tswap32(vptr[0]);
841             void *msg = (void *)tswap32(vptr[1]);
842             size_t len = tswap32(vptr[2]);
843             int flags = tswap32(vptr[3]);
844             void *target_addr = (void *)tswap32(vptr[4]);
845             socklen_t addrlen = tswap32(vptr[5]);
846             void *addr = alloca(addrlen);
847 
848             target_to_host_sockaddr(addr, target_addr, addrlen);
849             ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
850         }
851         break;
852     case SOCKOP_recvfrom:
853         {
854             int sockfd = tswap32(vptr[0]);
855             void *msg = (void *)tswap32(vptr[1]);
856             size_t len = tswap32(vptr[2]);
857             int flags = tswap32(vptr[3]);
858             void *target_addr = (void *)tswap32(vptr[4]);
859             uint32_t *target_addrlen = (void *)tswap32(vptr[5]);
860             socklen_t addrlen = tswap32(*target_addrlen);
861             void *addr = alloca(addrlen);
862 
863             ret = get_errno(recvfrom(sockfd, msg, len, flags, addr, &addrlen));
864             if (!is_error(ret)) {
865                 host_to_target_sockaddr(target_addr, addr, addrlen);
866                 *target_addrlen = tswap32(addrlen);
867             }
868         }
869         break;
870     case SOCKOP_shutdown:
871         {
872             int sockfd = tswap32(vptr[0]);
873             int how = tswap32(vptr[1]);
874 
875             ret = get_errno(shutdown(sockfd, how));
876         }
877         break;
878     case SOCKOP_sendmsg:
879     case SOCKOP_recvmsg:
880         {
881             int fd;
882             struct target_msghdr *msgp;
883             struct msghdr msg;
884             int flags, count, i;
885             struct iovec *vec;
886             struct target_iovec *target_vec;
887 
888             msgp = (void *)tswap32(vptr[1]);
889             msg.msg_name = (void *)tswapl(msgp->msg_name);
890             msg.msg_namelen = tswapl(msgp->msg_namelen);
891             msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
892             msg.msg_control = alloca(msg.msg_controllen);
893             msg.msg_flags = tswap32(msgp->msg_flags);
894 
895             count = tswapl(msgp->msg_iovlen);
896             vec = alloca(count * sizeof(struct iovec));
897             target_vec = (void *)tswapl(msgp->msg_iov);
898             for(i = 0;i < count; i++) {
899                 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
900                 vec[i].iov_len = tswapl(target_vec[i].iov_len);
901             }
902             msg.msg_iovlen = count;
903             msg.msg_iov = vec;
904 
905             fd = tswap32(vptr[0]);
906             flags = tswap32(vptr[2]);
907             if (num == SOCKOP_sendmsg) {
908                 target_to_host_cmsg(&msg, msgp);
909                 ret = get_errno(sendmsg(fd, &msg, flags));
910             } else {
911                 ret = get_errno(recvmsg(fd, &msg, flags));
912                 if (!is_error(ret))
913                   host_to_target_cmsg(msgp, &msg);
914             }
915         }
916         break;
917     case SOCKOP_setsockopt:
918         {
919             int sockfd = tswap32(vptr[0]);
920             int level = tswap32(vptr[1]);
921             int optname = tswap32(vptr[2]);
922             void *optval = (void *)tswap32(vptr[3]);
923             socklen_t optlen = tswap32(vptr[4]);
924 
925             ret = do_setsockopt(sockfd, level, optname, optval, optlen);
926         }
927         break;
928     case SOCKOP_getsockopt:
929         {
930             int sockfd = tswap32(vptr[0]);
931             int level = tswap32(vptr[1]);
932             int optname = tswap32(vptr[2]);
933             void *optval = (void *)tswap32(vptr[3]);
934             uint32_t *poptlen = (void *)tswap32(vptr[4]);
935 
936             ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
937         }
938         break;
939     default:
940         gemu_log("Unsupported socketcall: %d\n", num);
941         ret = -ENOSYS;
942         break;
943     }
944     return ret;
945 }
946 
947 
948 #define N_SHM_REGIONS	32
949 
950 static struct shm_region {
951     uint32_t	start;
952     uint32_t	size;
953 } shm_regions[N_SHM_REGIONS];
954 
955 static long do_ipc(long call, long first, long second, long third,
956 		   long ptr, long fifth)
957 {
958     int version;
959     long ret = 0;
960     unsigned long raddr;
961     struct shmid_ds shm_info;
962     int i;
963 
964     version = call >> 16;
965     call &= 0xffff;
966 
967     switch (call) {
968     case IPCOP_shmat:
969 	/* SHM_* flags are the same on all linux platforms */
970 	ret = get_errno((long) shmat(first, (void *) ptr, second));
971         if (is_error(ret))
972             break;
973         raddr = ret;
974 	/* find out the length of the shared memory segment */
975 
976         ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
977         if (is_error(ret)) {
978             /* can't get length, bail out */
979             shmdt((void *) raddr);
980 	    break;
981 	}
982 	page_set_flags(raddr, raddr + shm_info.shm_segsz,
983 		       PAGE_VALID | PAGE_READ |
984 		       ((second & SHM_RDONLY)? 0: PAGE_WRITE));
985 	for (i = 0; i < N_SHM_REGIONS; ++i) {
986 	    if (shm_regions[i].start == 0) {
987 		shm_regions[i].start = raddr;
988 		shm_regions[i].size = shm_info.shm_segsz;
989                 break;
990 	    }
991 	}
992 	if (put_user(raddr, (uint32_t *)third))
993             return -EFAULT;
994         ret = 0;
995 	break;
996     case IPCOP_shmdt:
997 	for (i = 0; i < N_SHM_REGIONS; ++i) {
998 	    if (shm_regions[i].start == ptr) {
999 		shm_regions[i].start = 0;
1000 		page_set_flags(ptr, shm_regions[i].size, 0);
1001 		break;
1002 	    }
1003 	}
1004 	ret = get_errno(shmdt((void *) ptr));
1005 	break;
1006 
1007     case IPCOP_shmget:
1008 	/* IPC_* flag values are the same on all linux platforms */
1009 	ret = get_errno(shmget(first, second, third));
1010 	break;
1011 
1012 	/* IPC_* and SHM_* command values are the same on all linux platforms */
1013     case IPCOP_shmctl:
1014         switch(second) {
1015         case IPC_RMID:
1016         case SHM_LOCK:
1017         case SHM_UNLOCK:
1018             ret = get_errno(shmctl(first, second, NULL));
1019             break;
1020         default:
1021             goto unimplemented;
1022         }
1023         break;
1024     default:
1025     unimplemented:
1026 	gemu_log("Unsupported ipc call: %ld (version %d)\n", call, version);
1027 	ret = -ENOSYS;
1028 	break;
1029     }
1030     return ret;
1031 }
1032 
1033 /* kernel structure types definitions */
1034 #define IFNAMSIZ        16
1035 
1036 #define STRUCT(name, list...) STRUCT_ ## name,
1037 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
1038 enum {
1039 #include "syscall_types.h"
1040 };
1041 #undef STRUCT
1042 #undef STRUCT_SPECIAL
1043 
1044 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1045 #define STRUCT_SPECIAL(name)
1046 #include "syscall_types.h"
1047 #undef STRUCT
1048 #undef STRUCT_SPECIAL
1049 
1050 typedef struct IOCTLEntry {
1051     unsigned int target_cmd;
1052     unsigned int host_cmd;
1053     const char *name;
1054     int access;
1055     const argtype arg_type[5];
1056 } IOCTLEntry;
1057 
1058 #define IOC_R 0x0001
1059 #define IOC_W 0x0002
1060 #define IOC_RW (IOC_R | IOC_W)
1061 
1062 #define MAX_STRUCT_SIZE 4096
1063 
1064 IOCTLEntry ioctl_entries[] = {
1065 #define IOCTL(cmd, access, types...) \
1066     { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1067 #include "ioctls.h"
1068     { 0, 0, },
1069 };
1070 
1071 static long do_ioctl(long fd, long cmd, long arg)
1072 {
1073     const IOCTLEntry *ie;
1074     const argtype *arg_type;
1075     long ret;
1076     uint8_t buf_temp[MAX_STRUCT_SIZE];
1077 
1078     ie = ioctl_entries;
1079     for(;;) {
1080         if (ie->target_cmd == 0) {
1081             gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
1082             return -ENOSYS;
1083         }
1084         if (ie->target_cmd == cmd)
1085             break;
1086         ie++;
1087     }
1088     arg_type = ie->arg_type;
1089 #if defined(DEBUG)
1090     gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
1091 #endif
1092     switch(arg_type[0]) {
1093     case TYPE_NULL:
1094         /* no argument */
1095         ret = get_errno(ioctl(fd, ie->host_cmd));
1096         break;
1097     case TYPE_PTRVOID:
1098     case TYPE_INT:
1099         /* int argment */
1100         ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1101         break;
1102     case TYPE_PTR:
1103         arg_type++;
1104         switch(ie->access) {
1105         case IOC_R:
1106             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1107             if (!is_error(ret)) {
1108                 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
1109             }
1110             break;
1111         case IOC_W:
1112             thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
1113             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1114             break;
1115         default:
1116         case IOC_RW:
1117             thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
1118             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1119             if (!is_error(ret)) {
1120                 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
1121             }
1122             break;
1123         }
1124         break;
1125     default:
1126         gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
1127         ret = -ENOSYS;
1128         break;
1129     }
1130     return ret;
1131 }
1132 
1133 bitmask_transtbl iflag_tbl[] = {
1134         { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1135         { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1136         { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1137         { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1138         { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1139         { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1140         { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1141         { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1142         { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1143         { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1144         { TARGET_IXON, TARGET_IXON, IXON, IXON },
1145         { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1146         { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1147         { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1148         { 0, 0, 0, 0 }
1149 };
1150 
1151 bitmask_transtbl oflag_tbl[] = {
1152 	{ TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1153 	{ TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1154 	{ TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1155 	{ TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1156 	{ TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1157 	{ TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1158 	{ TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1159 	{ TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1160 	{ TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1161 	{ TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1162 	{ TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1163 	{ TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1164 	{ TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1165 	{ TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1166 	{ TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1167 	{ TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1168 	{ TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1169 	{ TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1170 	{ TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1171 	{ TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1172 	{ TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1173 	{ TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1174 	{ TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1175 	{ TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1176 	{ 0, 0, 0, 0 }
1177 };
1178 
1179 bitmask_transtbl cflag_tbl[] = {
1180 	{ TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1181 	{ TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1182 	{ TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1183 	{ TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1184 	{ TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1185 	{ TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1186 	{ TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1187 	{ TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1188 	{ TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1189 	{ TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1190 	{ TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1191 	{ TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1192 	{ TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1193 	{ TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1194 	{ TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1195 	{ TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1196 	{ TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1197 	{ TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1198 	{ TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1199 	{ TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1200 	{ TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1201 	{ TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1202 	{ TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1203 	{ TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1204 	{ TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1205 	{ TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1206 	{ TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1207 	{ TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1208 	{ TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1209 	{ TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1210 	{ TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1211 	{ 0, 0, 0, 0 }
1212 };
1213 
1214 bitmask_transtbl lflag_tbl[] = {
1215 	{ TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1216 	{ TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1217 	{ TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1218 	{ TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1219 	{ TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1220 	{ TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1221 	{ TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1222 	{ TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1223 	{ TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1224 	{ TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1225 	{ TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1226 	{ TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1227 	{ TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1228 	{ TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1229 	{ TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1230 	{ 0, 0, 0, 0 }
1231 };
1232 
1233 static void target_to_host_termios (void *dst, const void *src)
1234 {
1235     struct host_termios *host = dst;
1236     const struct target_termios *target = src;
1237 
1238     host->c_iflag =
1239         target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
1240     host->c_oflag =
1241         target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
1242     host->c_cflag =
1243         target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
1244     host->c_lflag =
1245         target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
1246     host->c_line = target->c_line;
1247 
1248     host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
1249     host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
1250     host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
1251     host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
1252     host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
1253     host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
1254     host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
1255     host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
1256     host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
1257     host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
1258     host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
1259     host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
1260     host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
1261     host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
1262     host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
1263     host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
1264     host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
1265 }
1266 
1267 static void host_to_target_termios (void *dst, const void *src)
1268 {
1269     struct target_termios *target = dst;
1270     const struct host_termios *host = src;
1271 
1272     target->c_iflag =
1273         tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
1274     target->c_oflag =
1275         tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
1276     target->c_cflag =
1277         tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
1278     target->c_lflag =
1279         tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
1280     target->c_line = host->c_line;
1281 
1282     target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
1283     target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
1284     target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
1285     target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
1286     target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
1287     target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
1288     target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
1289     target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
1290     target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
1291     target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
1292     target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
1293     target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
1294     target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
1295     target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
1296     target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
1297     target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
1298     target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
1299 }
1300 
1301 StructEntry struct_termios_def = {
1302     .convert = { host_to_target_termios, target_to_host_termios },
1303     .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
1304     .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
1305 };
1306 
1307 static bitmask_transtbl mmap_flags_tbl[] = {
1308 	{ TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
1309 	{ TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
1310 	{ TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
1311 	{ TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
1312 	{ TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
1313 	{ TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
1314 	{ TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
1315 	{ TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
1316 	{ 0, 0, 0, 0 }
1317 };
1318 
1319 static bitmask_transtbl fcntl_flags_tbl[] = {
1320 	{ TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
1321 	{ TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
1322 	{ TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
1323 	{ TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
1324 	{ TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
1325 	{ TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
1326 	{ TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
1327 	{ TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
1328 	{ TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
1329 	{ TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
1330 	{ TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
1331 	{ TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
1332 	{ TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
1333 #if defined(O_DIRECT)
1334 	{ TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
1335 #endif
1336 	{ 0, 0, 0, 0 }
1337 };
1338 
1339 #if defined(TARGET_I386)
1340 
1341 /* NOTE: there is really one LDT for all the threads */
1342 uint8_t *ldt_table;
1343 
1344 static int read_ldt(void *ptr, unsigned long bytecount)
1345 {
1346     int size;
1347 
1348     if (!ldt_table)
1349         return 0;
1350     size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
1351     if (size > bytecount)
1352         size = bytecount;
1353     memcpy(ptr, ldt_table, size);
1354     return size;
1355 }
1356 
1357 /* XXX: add locking support */
1358 static int write_ldt(CPUX86State *env,
1359                      void *ptr, unsigned long bytecount, int oldmode)
1360 {
1361     struct target_modify_ldt_ldt_s ldt_info;
1362     int seg_32bit, contents, read_exec_only, limit_in_pages;
1363     int seg_not_present, useable;
1364     uint32_t *lp, entry_1, entry_2;
1365 
1366     if (bytecount != sizeof(ldt_info))
1367         return -EINVAL;
1368     memcpy(&ldt_info, ptr, sizeof(ldt_info));
1369     tswap32s(&ldt_info.entry_number);
1370     tswapls((long *)&ldt_info.base_addr);
1371     tswap32s(&ldt_info.limit);
1372     tswap32s(&ldt_info.flags);
1373 
1374     if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
1375         return -EINVAL;
1376     seg_32bit = ldt_info.flags & 1;
1377     contents = (ldt_info.flags >> 1) & 3;
1378     read_exec_only = (ldt_info.flags >> 3) & 1;
1379     limit_in_pages = (ldt_info.flags >> 4) & 1;
1380     seg_not_present = (ldt_info.flags >> 5) & 1;
1381     useable = (ldt_info.flags >> 6) & 1;
1382 
1383     if (contents == 3) {
1384         if (oldmode)
1385             return -EINVAL;
1386         if (seg_not_present == 0)
1387             return -EINVAL;
1388     }
1389     /* allocate the LDT */
1390     if (!ldt_table) {
1391         ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1392         if (!ldt_table)
1393             return -ENOMEM;
1394         memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1395         env->ldt.base = (long)ldt_table;
1396         env->ldt.limit = 0xffff;
1397     }
1398 
1399     /* NOTE: same code as Linux kernel */
1400     /* Allow LDTs to be cleared by the user. */
1401     if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
1402         if (oldmode ||
1403             (contents == 0		&&
1404              read_exec_only == 1	&&
1405              seg_32bit == 0		&&
1406              limit_in_pages == 0	&&
1407              seg_not_present == 1	&&
1408              useable == 0 )) {
1409             entry_1 = 0;
1410             entry_2 = 0;
1411             goto install;
1412         }
1413     }
1414 
1415     entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
1416         (ldt_info.limit & 0x0ffff);
1417     entry_2 = (ldt_info.base_addr & 0xff000000) |
1418         ((ldt_info.base_addr & 0x00ff0000) >> 16) |
1419         (ldt_info.limit & 0xf0000) |
1420         ((read_exec_only ^ 1) << 9) |
1421         (contents << 10) |
1422         ((seg_not_present ^ 1) << 15) |
1423         (seg_32bit << 22) |
1424         (limit_in_pages << 23) |
1425         0x7000;
1426     if (!oldmode)
1427         entry_2 |= (useable << 20);
1428 
1429     /* Install the new entry ...  */
1430 install:
1431     lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
1432     lp[0] = tswap32(entry_1);
1433     lp[1] = tswap32(entry_2);
1434     return 0;
1435 }
1436 
1437 /* specific and weird i386 syscalls */
1438 int do_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
1439 {
1440     int ret = -ENOSYS;
1441 
1442     switch (func) {
1443     case 0:
1444         ret = read_ldt(ptr, bytecount);
1445         break;
1446     case 1:
1447         ret = write_ldt(env, ptr, bytecount, 1);
1448         break;
1449     case 0x11:
1450         ret = write_ldt(env, ptr, bytecount, 0);
1451         break;
1452     }
1453     return ret;
1454 }
1455 
1456 #endif /* defined(TARGET_I386) */
1457 
1458 /* this stack is the equivalent of the kernel stack associated with a
1459    thread/process */
1460 #define NEW_STACK_SIZE 8192
1461 
1462 static int clone_func(void *arg)
1463 {
1464     CPUState *env = arg;
1465     cpu_loop(env);
1466     /* never exits */
1467     return 0;
1468 }
1469 
1470 int do_fork(CPUState *env, unsigned int flags, unsigned long newsp)
1471 {
1472     int ret;
1473     TaskState *ts;
1474     uint8_t *new_stack;
1475     CPUState *new_env;
1476 
1477     if (flags & CLONE_VM) {
1478         ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
1479         memset(ts, 0, sizeof(TaskState));
1480         new_stack = ts->stack;
1481         ts->used = 1;
1482         /* add in task state list */
1483         ts->next = first_task_state;
1484         first_task_state = ts;
1485         /* we create a new CPU instance. */
1486         new_env = cpu_init();
1487         memcpy(new_env, env, sizeof(CPUState));
1488 #if defined(TARGET_I386)
1489         if (!newsp)
1490             newsp = env->regs[R_ESP];
1491         new_env->regs[R_ESP] = newsp;
1492         new_env->regs[R_EAX] = 0;
1493 #elif defined(TARGET_ARM)
1494         if (!newsp)
1495             newsp = env->regs[13];
1496         new_env->regs[13] = newsp;
1497         new_env->regs[0] = 0;
1498 #elif defined(TARGET_SPARC)
1499         printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
1500 #elif defined(TARGET_MIPS)
1501         printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
1502 #elif defined(TARGET_PPC)
1503         if (!newsp)
1504             newsp = env->gpr[1];
1505         new_env->gpr[1] = newsp;
1506         {
1507             int i;
1508             for (i = 7; i < 32; i++)
1509                 new_env->gpr[i] = 0;
1510         }
1511 #else
1512 #error unsupported target CPU
1513 #endif
1514         new_env->opaque = ts;
1515 #ifdef __ia64__
1516         ret = clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1517 #else
1518 	ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1519 #endif
1520     } else {
1521         /* if no CLONE_VM, we consider it is a fork */
1522         if ((flags & ~CSIGNAL) != 0)
1523             return -EINVAL;
1524         ret = fork();
1525     }
1526     return ret;
1527 }
1528 
1529 static long do_fcntl(int fd, int cmd, unsigned long arg)
1530 {
1531     struct flock fl;
1532     struct target_flock *target_fl = (void *)arg;
1533     long ret;
1534 
1535     switch(cmd) {
1536     case TARGET_F_GETLK:
1537         ret = fcntl(fd, cmd, &fl);
1538         if (ret == 0) {
1539             target_fl->l_type = tswap16(fl.l_type);
1540             target_fl->l_whence = tswap16(fl.l_whence);
1541             target_fl->l_start = tswapl(fl.l_start);
1542             target_fl->l_len = tswapl(fl.l_len);
1543             target_fl->l_pid = tswapl(fl.l_pid);
1544         }
1545         break;
1546 
1547     case TARGET_F_SETLK:
1548     case TARGET_F_SETLKW:
1549         fl.l_type = tswap16(target_fl->l_type);
1550         fl.l_whence = tswap16(target_fl->l_whence);
1551         fl.l_start = tswapl(target_fl->l_start);
1552         fl.l_len = tswapl(target_fl->l_len);
1553         fl.l_pid = tswapl(target_fl->l_pid);
1554         ret = fcntl(fd, cmd, &fl);
1555         break;
1556 
1557     case TARGET_F_GETLK64:
1558     case TARGET_F_SETLK64:
1559     case TARGET_F_SETLKW64:
1560         ret = -1;
1561         errno = EINVAL;
1562         break;
1563 
1564     case F_GETFL:
1565         ret = fcntl(fd, cmd, arg);
1566         ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
1567         break;
1568 
1569     case F_SETFL:
1570         ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
1571         break;
1572 
1573     default:
1574         ret = fcntl(fd, cmd, arg);
1575         break;
1576     }
1577     return ret;
1578 }
1579 
1580 #ifdef USE_UID16
1581 
1582 static inline int high2lowuid(int uid)
1583 {
1584     if (uid > 65535)
1585         return 65534;
1586     else
1587         return uid;
1588 }
1589 
1590 static inline int high2lowgid(int gid)
1591 {
1592     if (gid > 65535)
1593         return 65534;
1594     else
1595         return gid;
1596 }
1597 
1598 static inline int low2highuid(int uid)
1599 {
1600     if ((int16_t)uid == -1)
1601         return -1;
1602     else
1603         return uid;
1604 }
1605 
1606 static inline int low2highgid(int gid)
1607 {
1608     if ((int16_t)gid == -1)
1609         return -1;
1610     else
1611         return gid;
1612 }
1613 
1614 #endif /* USE_UID16 */
1615 
1616 void syscall_init(void)
1617 {
1618     IOCTLEntry *ie;
1619     const argtype *arg_type;
1620     int size;
1621 
1622 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
1623 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
1624 #include "syscall_types.h"
1625 #undef STRUCT
1626 #undef STRUCT_SPECIAL
1627 
1628     /* we patch the ioctl size if necessary. We rely on the fact that
1629        no ioctl has all the bits at '1' in the size field */
1630     ie = ioctl_entries;
1631     while (ie->target_cmd != 0) {
1632         if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
1633             TARGET_IOC_SIZEMASK) {
1634             arg_type = ie->arg_type;
1635             if (arg_type[0] != TYPE_PTR) {
1636                 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
1637                         ie->target_cmd);
1638                 exit(1);
1639             }
1640             arg_type++;
1641             size = thunk_type_size(arg_type, 0);
1642             ie->target_cmd = (ie->target_cmd &
1643                               ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
1644                 (size << TARGET_IOC_SIZESHIFT);
1645         }
1646         /* automatic consistency check if same arch */
1647 #if defined(__i386__) && defined(TARGET_I386)
1648         if (ie->target_cmd != ie->host_cmd) {
1649             fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
1650                     ie->target_cmd, ie->host_cmd);
1651         }
1652 #endif
1653         ie++;
1654     }
1655 }
1656 
1657 long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
1658                 long arg4, long arg5, long arg6)
1659 {
1660     long ret;
1661     struct stat st;
1662     struct kernel_statfs *stfs;
1663 
1664 #ifdef DEBUG
1665     gemu_log("syscall %d", num);
1666 #endif
1667     switch(num) {
1668     case TARGET_NR_exit:
1669 #ifdef HAVE_GPROF
1670         _mcleanup();
1671 #endif
1672         gdb_exit(cpu_env, arg1);
1673         /* XXX: should free thread stack and CPU env */
1674         _exit(arg1);
1675         ret = 0; /* avoid warning */
1676         break;
1677     case TARGET_NR_read:
1678         page_unprotect_range((void *)arg2, arg3);
1679         ret = get_errno(read(arg1, (void *)arg2, arg3));
1680         break;
1681     case TARGET_NR_write:
1682         ret = get_errno(write(arg1, (void *)arg2, arg3));
1683         break;
1684     case TARGET_NR_open:
1685         ret = get_errno(open(path((const char *)arg1),
1686                              target_to_host_bitmask(arg2, fcntl_flags_tbl),
1687                              arg3));
1688         break;
1689     case TARGET_NR_close:
1690         ret = get_errno(close(arg1));
1691         break;
1692     case TARGET_NR_brk:
1693         ret = do_brk((char *)arg1);
1694         break;
1695     case TARGET_NR_fork:
1696         ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
1697         break;
1698     case TARGET_NR_waitpid:
1699         {
1700             int *status = (int *)arg2;
1701             ret = get_errno(waitpid(arg1, status, arg3));
1702             if (!is_error(ret) && status)
1703                 tswapls((long *)&status);
1704         }
1705         break;
1706     case TARGET_NR_creat:
1707         ret = get_errno(creat((const char *)arg1, arg2));
1708         break;
1709     case TARGET_NR_link:
1710         ret = get_errno(link((const char *)arg1, (const char *)arg2));
1711         break;
1712     case TARGET_NR_unlink:
1713         ret = get_errno(unlink((const char *)arg1));
1714         break;
1715     case TARGET_NR_execve:
1716         {
1717             char **argp, **envp;
1718             int argc, envc;
1719             uint32_t *p;
1720             char **q;
1721 
1722             argc = 0;
1723             for (p = (void *)arg2; *p; p++)
1724                 argc++;
1725             envc = 0;
1726             for (p = (void *)arg3; *p; p++)
1727                 envc++;
1728 
1729             argp = alloca((argc + 1) * sizeof(void *));
1730             envp = alloca((envc + 1) * sizeof(void *));
1731 
1732             for (p = (void *)arg2, q = argp; *p; p++, q++)
1733                 *q = (void *)tswap32(*p);
1734             *q = NULL;
1735 
1736             for (p = (void *)arg3, q = envp; *p; p++, q++)
1737                 *q = (void *)tswap32(*p);
1738             *q = NULL;
1739 
1740             ret = get_errno(execve((const char *)arg1, argp, envp));
1741         }
1742         break;
1743     case TARGET_NR_chdir:
1744         ret = get_errno(chdir((const char *)arg1));
1745         break;
1746 #ifdef TARGET_NR_time
1747     case TARGET_NR_time:
1748         {
1749             int *time_ptr = (int *)arg1;
1750             ret = get_errno(time((time_t *)time_ptr));
1751             if (!is_error(ret) && time_ptr)
1752                 tswap32s(time_ptr);
1753         }
1754         break;
1755 #endif
1756     case TARGET_NR_mknod:
1757         ret = get_errno(mknod((const char *)arg1, arg2, arg3));
1758         break;
1759     case TARGET_NR_chmod:
1760         ret = get_errno(chmod((const char *)arg1, arg2));
1761         break;
1762 #ifdef TARGET_NR_break
1763     case TARGET_NR_break:
1764         goto unimplemented;
1765 #endif
1766 #ifdef TARGET_NR_oldstat
1767     case TARGET_NR_oldstat:
1768         goto unimplemented;
1769 #endif
1770     case TARGET_NR_lseek:
1771         ret = get_errno(lseek(arg1, arg2, arg3));
1772         break;
1773     case TARGET_NR_getpid:
1774         ret = get_errno(getpid());
1775         break;
1776     case TARGET_NR_mount:
1777         /* need to look at the data field */
1778         goto unimplemented;
1779     case TARGET_NR_umount:
1780         ret = get_errno(umount((const char *)arg1));
1781         break;
1782     case TARGET_NR_stime:
1783         {
1784             int *time_ptr = (int *)arg1;
1785             if (time_ptr)
1786                 tswap32s(time_ptr);
1787             ret = get_errno(stime((time_t *)time_ptr));
1788         }
1789         break;
1790     case TARGET_NR_ptrace:
1791         goto unimplemented;
1792     case TARGET_NR_alarm:
1793         ret = alarm(arg1);
1794         break;
1795 #ifdef TARGET_NR_oldfstat
1796     case TARGET_NR_oldfstat:
1797         goto unimplemented;
1798 #endif
1799     case TARGET_NR_pause:
1800         ret = get_errno(pause());
1801         break;
1802     case TARGET_NR_utime:
1803         {
1804             struct utimbuf tbuf, *tbuf1;
1805             struct target_utimbuf *target_tbuf = (void *)arg2;
1806             if (target_tbuf) {
1807                 get_user(tbuf.actime, &target_tbuf->actime);
1808                 get_user(tbuf.modtime, &target_tbuf->modtime);
1809                 tbuf1 = &tbuf;
1810             } else {
1811                 tbuf1 = NULL;
1812             }
1813             ret = get_errno(utime((const char *)arg1, tbuf1));
1814         }
1815         break;
1816     case TARGET_NR_utimes:
1817         {
1818             struct target_timeval *target_tvp = (struct target_timeval *)arg2;
1819             struct timeval *tvp, tv[2];
1820             if (target_tvp) {
1821                 target_to_host_timeval(&tv[0], &target_tvp[0]);
1822                 target_to_host_timeval(&tv[1], &target_tvp[1]);
1823                 tvp = tv;
1824             } else {
1825                 tvp = NULL;
1826             }
1827             ret = get_errno(utimes((const char *)arg1, tvp));
1828         }
1829         break;
1830 #ifdef TARGET_NR_stty
1831     case TARGET_NR_stty:
1832         goto unimplemented;
1833 #endif
1834 #ifdef TARGET_NR_gtty
1835     case TARGET_NR_gtty:
1836         goto unimplemented;
1837 #endif
1838     case TARGET_NR_access:
1839         ret = get_errno(access((const char *)arg1, arg2));
1840         break;
1841     case TARGET_NR_nice:
1842         ret = get_errno(nice(arg1));
1843         break;
1844 #ifdef TARGET_NR_ftime
1845     case TARGET_NR_ftime:
1846         goto unimplemented;
1847 #endif
1848     case TARGET_NR_sync:
1849         sync();
1850         ret = 0;
1851         break;
1852     case TARGET_NR_kill:
1853         ret = get_errno(kill(arg1, arg2));
1854         break;
1855     case TARGET_NR_rename:
1856         ret = get_errno(rename((const char *)arg1, (const char *)arg2));
1857         break;
1858     case TARGET_NR_mkdir:
1859         ret = get_errno(mkdir((const char *)arg1, arg2));
1860         break;
1861     case TARGET_NR_rmdir:
1862         ret = get_errno(rmdir((const char *)arg1));
1863         break;
1864     case TARGET_NR_dup:
1865         ret = get_errno(dup(arg1));
1866         break;
1867     case TARGET_NR_pipe:
1868         {
1869             int *pipe_ptr = (int *)arg1;
1870             ret = get_errno(pipe(pipe_ptr));
1871             if (!is_error(ret)) {
1872                 tswap32s(&pipe_ptr[0]);
1873                 tswap32s(&pipe_ptr[1]);
1874             }
1875         }
1876         break;
1877     case TARGET_NR_times:
1878         {
1879             struct target_tms *tmsp = (void *)arg1;
1880             struct tms tms;
1881             ret = get_errno(times(&tms));
1882             if (tmsp) {
1883                 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
1884                 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
1885                 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
1886                 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
1887             }
1888             if (!is_error(ret))
1889                 ret = host_to_target_clock_t(ret);
1890         }
1891         break;
1892 #ifdef TARGET_NR_prof
1893     case TARGET_NR_prof:
1894         goto unimplemented;
1895 #endif
1896     case TARGET_NR_signal:
1897         goto unimplemented;
1898 
1899     case TARGET_NR_acct:
1900         goto unimplemented;
1901     case TARGET_NR_umount2:
1902         ret = get_errno(umount2((const char *)arg1, arg2));
1903         break;
1904 #ifdef TARGET_NR_lock
1905     case TARGET_NR_lock:
1906         goto unimplemented;
1907 #endif
1908     case TARGET_NR_ioctl:
1909         ret = do_ioctl(arg1, arg2, arg3);
1910         break;
1911     case TARGET_NR_fcntl:
1912         ret = get_errno(do_fcntl(arg1, arg2, arg3));
1913         break;
1914 #ifdef TARGET_NR_mpx
1915     case TARGET_NR_mpx:
1916         goto unimplemented;
1917 #endif
1918     case TARGET_NR_setpgid:
1919         ret = get_errno(setpgid(arg1, arg2));
1920         break;
1921 #ifdef TARGET_NR_ulimit
1922     case TARGET_NR_ulimit:
1923         goto unimplemented;
1924 #endif
1925 #ifdef TARGET_NR_oldolduname
1926     case TARGET_NR_oldolduname:
1927         goto unimplemented;
1928 #endif
1929     case TARGET_NR_umask:
1930         ret = get_errno(umask(arg1));
1931         break;
1932     case TARGET_NR_chroot:
1933         ret = get_errno(chroot((const char *)arg1));
1934         break;
1935     case TARGET_NR_ustat:
1936         goto unimplemented;
1937     case TARGET_NR_dup2:
1938         ret = get_errno(dup2(arg1, arg2));
1939         break;
1940     case TARGET_NR_getppid:
1941         ret = get_errno(getppid());
1942         break;
1943     case TARGET_NR_getpgrp:
1944         ret = get_errno(getpgrp());
1945         break;
1946     case TARGET_NR_setsid:
1947         ret = get_errno(setsid());
1948         break;
1949     case TARGET_NR_sigaction:
1950         {
1951             struct target_old_sigaction *old_act = (void *)arg2;
1952             struct target_old_sigaction *old_oact = (void *)arg3;
1953             struct target_sigaction act, oact, *pact;
1954             if (old_act) {
1955                 act._sa_handler = old_act->_sa_handler;
1956                 target_siginitset(&act.sa_mask, old_act->sa_mask);
1957                 act.sa_flags = old_act->sa_flags;
1958                 act.sa_restorer = old_act->sa_restorer;
1959                 pact = &act;
1960             } else {
1961                 pact = NULL;
1962             }
1963             ret = get_errno(do_sigaction(arg1, pact, &oact));
1964             if (!is_error(ret) && old_oact) {
1965                 old_oact->_sa_handler = oact._sa_handler;
1966                 old_oact->sa_mask = oact.sa_mask.sig[0];
1967                 old_oact->sa_flags = oact.sa_flags;
1968                 old_oact->sa_restorer = oact.sa_restorer;
1969             }
1970         }
1971         break;
1972     case TARGET_NR_rt_sigaction:
1973         ret = get_errno(do_sigaction(arg1, (void *)arg2, (void *)arg3));
1974         break;
1975     case TARGET_NR_sgetmask:
1976         {
1977             sigset_t cur_set;
1978             target_ulong target_set;
1979             sigprocmask(0, NULL, &cur_set);
1980             host_to_target_old_sigset(&target_set, &cur_set);
1981             ret = target_set;
1982         }
1983         break;
1984     case TARGET_NR_ssetmask:
1985         {
1986             sigset_t set, oset, cur_set;
1987             target_ulong target_set = arg1;
1988             sigprocmask(0, NULL, &cur_set);
1989             target_to_host_old_sigset(&set, &target_set);
1990             sigorset(&set, &set, &cur_set);
1991             sigprocmask(SIG_SETMASK, &set, &oset);
1992             host_to_target_old_sigset(&target_set, &oset);
1993             ret = target_set;
1994         }
1995         break;
1996     case TARGET_NR_sigprocmask:
1997         {
1998             int how = arg1;
1999             sigset_t set, oldset, *set_ptr;
2000             target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
2001 
2002             if (pset) {
2003                 switch(how) {
2004                 case TARGET_SIG_BLOCK:
2005                     how = SIG_BLOCK;
2006                     break;
2007                 case TARGET_SIG_UNBLOCK:
2008                     how = SIG_UNBLOCK;
2009                     break;
2010                 case TARGET_SIG_SETMASK:
2011                     how = SIG_SETMASK;
2012                     break;
2013                 default:
2014                     ret = -EINVAL;
2015                     goto fail;
2016                 }
2017                 target_to_host_old_sigset(&set, pset);
2018                 set_ptr = &set;
2019             } else {
2020                 how = 0;
2021                 set_ptr = NULL;
2022             }
2023             ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
2024             if (!is_error(ret) && poldset) {
2025                 host_to_target_old_sigset(poldset, &oldset);
2026             }
2027         }
2028         break;
2029     case TARGET_NR_rt_sigprocmask:
2030         {
2031             int how = arg1;
2032             sigset_t set, oldset, *set_ptr;
2033             target_sigset_t *pset = (void *)arg2;
2034             target_sigset_t *poldset = (void *)arg3;
2035 
2036             if (pset) {
2037                 switch(how) {
2038                 case TARGET_SIG_BLOCK:
2039                     how = SIG_BLOCK;
2040                     break;
2041                 case TARGET_SIG_UNBLOCK:
2042                     how = SIG_UNBLOCK;
2043                     break;
2044                 case TARGET_SIG_SETMASK:
2045                     how = SIG_SETMASK;
2046                     break;
2047                 default:
2048                     ret = -EINVAL;
2049                     goto fail;
2050                 }
2051                 target_to_host_sigset(&set, pset);
2052                 set_ptr = &set;
2053             } else {
2054                 how = 0;
2055                 set_ptr = NULL;
2056             }
2057             ret = get_errno(sigprocmask(how, set_ptr, &oldset));
2058             if (!is_error(ret) && poldset) {
2059                 host_to_target_sigset(poldset, &oldset);
2060             }
2061         }
2062         break;
2063     case TARGET_NR_sigpending:
2064         {
2065             sigset_t set;
2066             ret = get_errno(sigpending(&set));
2067             if (!is_error(ret)) {
2068                 host_to_target_old_sigset((target_ulong *)arg1, &set);
2069             }
2070         }
2071         break;
2072     case TARGET_NR_rt_sigpending:
2073         {
2074             sigset_t set;
2075             ret = get_errno(sigpending(&set));
2076             if (!is_error(ret)) {
2077                 host_to_target_sigset((target_sigset_t *)arg1, &set);
2078             }
2079         }
2080         break;
2081     case TARGET_NR_sigsuspend:
2082         {
2083             sigset_t set;
2084             target_to_host_old_sigset(&set, (target_ulong *)arg1);
2085             ret = get_errno(sigsuspend(&set));
2086         }
2087         break;
2088     case TARGET_NR_rt_sigsuspend:
2089         {
2090             sigset_t set;
2091             target_to_host_sigset(&set, (target_sigset_t *)arg1);
2092             ret = get_errno(sigsuspend(&set));
2093         }
2094         break;
2095     case TARGET_NR_rt_sigtimedwait:
2096         {
2097             target_sigset_t *target_set = (void *)arg1;
2098             target_siginfo_t *target_uinfo = (void *)arg2;
2099             struct target_timespec *target_uts = (void *)arg3;
2100             sigset_t set;
2101             struct timespec uts, *puts;
2102             siginfo_t uinfo;
2103 
2104             target_to_host_sigset(&set, target_set);
2105             if (target_uts) {
2106                 puts = &uts;
2107                 puts->tv_sec = tswapl(target_uts->tv_sec);
2108                 puts->tv_nsec = tswapl(target_uts->tv_nsec);
2109             } else {
2110                 puts = NULL;
2111             }
2112             ret = get_errno(sigtimedwait(&set, &uinfo, puts));
2113             if (!is_error(ret) && target_uinfo) {
2114                 host_to_target_siginfo(target_uinfo, &uinfo);
2115             }
2116         }
2117         break;
2118     case TARGET_NR_rt_sigqueueinfo:
2119         {
2120             siginfo_t uinfo;
2121             target_to_host_siginfo(&uinfo, (target_siginfo_t *)arg3);
2122             ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
2123         }
2124         break;
2125     case TARGET_NR_sigreturn:
2126         /* NOTE: ret is eax, so not transcoding must be done */
2127         ret = do_sigreturn(cpu_env);
2128         break;
2129     case TARGET_NR_rt_sigreturn:
2130         /* NOTE: ret is eax, so not transcoding must be done */
2131         ret = do_rt_sigreturn(cpu_env);
2132         break;
2133     case TARGET_NR_sethostname:
2134         ret = get_errno(sethostname((const char *)arg1, arg2));
2135         break;
2136     case TARGET_NR_setrlimit:
2137         {
2138             /* XXX: convert resource ? */
2139             int resource = arg1;
2140             struct target_rlimit *target_rlim = (void *)arg2;
2141             struct rlimit rlim;
2142             rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
2143             rlim.rlim_max = tswapl(target_rlim->rlim_max);
2144             ret = get_errno(setrlimit(resource, &rlim));
2145         }
2146         break;
2147     case TARGET_NR_getrlimit:
2148         {
2149             /* XXX: convert resource ? */
2150             int resource = arg1;
2151             struct target_rlimit *target_rlim = (void *)arg2;
2152             struct rlimit rlim;
2153 
2154             ret = get_errno(getrlimit(resource, &rlim));
2155             if (!is_error(ret)) {
2156                 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
2157                 target_rlim->rlim_max = tswapl(rlim.rlim_max);
2158             }
2159         }
2160         break;
2161     case TARGET_NR_getrusage:
2162         {
2163             struct rusage rusage;
2164             struct target_rusage *target_rusage = (void *)arg2;
2165             ret = get_errno(getrusage(arg1, &rusage));
2166             if (!is_error(ret)) {
2167                 host_to_target_rusage(target_rusage, &rusage);
2168             }
2169         }
2170         break;
2171     case TARGET_NR_gettimeofday:
2172         {
2173             struct target_timeval *target_tv = (void *)arg1;
2174             struct timeval tv;
2175             ret = get_errno(gettimeofday(&tv, NULL));
2176             if (!is_error(ret)) {
2177                 host_to_target_timeval(target_tv, &tv);
2178             }
2179         }
2180         break;
2181     case TARGET_NR_settimeofday:
2182         {
2183             struct target_timeval *target_tv = (void *)arg1;
2184             struct timeval tv;
2185             target_to_host_timeval(&tv, target_tv);
2186             ret = get_errno(settimeofday(&tv, NULL));
2187         }
2188         break;
2189 #ifdef TARGET_NR_select
2190     case TARGET_NR_select:
2191         {
2192             struct target_sel_arg_struct *sel = (void *)arg1;
2193             sel->n = tswapl(sel->n);
2194             sel->inp = tswapl(sel->inp);
2195             sel->outp = tswapl(sel->outp);
2196             sel->exp = tswapl(sel->exp);
2197             sel->tvp = tswapl(sel->tvp);
2198             ret = do_select(sel->n, (void *)sel->inp, (void *)sel->outp,
2199                             (void *)sel->exp, (void *)sel->tvp);
2200         }
2201         break;
2202 #endif
2203     case TARGET_NR_symlink:
2204         ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
2205         break;
2206 #ifdef TARGET_NR_oldlstat
2207     case TARGET_NR_oldlstat:
2208         goto unimplemented;
2209 #endif
2210     case TARGET_NR_readlink:
2211         ret = get_errno(readlink(path((const char *)arg1), (char *)arg2, arg3));
2212         break;
2213     case TARGET_NR_uselib:
2214         goto unimplemented;
2215     case TARGET_NR_swapon:
2216         ret = get_errno(swapon((const char *)arg1, arg2));
2217         break;
2218     case TARGET_NR_reboot:
2219         goto unimplemented;
2220     case TARGET_NR_readdir:
2221         goto unimplemented;
2222     case TARGET_NR_mmap:
2223 #if defined(TARGET_I386) || defined(TARGET_ARM)
2224         {
2225             uint32_t v1, v2, v3, v4, v5, v6, *vptr;
2226             vptr = (uint32_t *)arg1;
2227             v1 = tswap32(vptr[0]);
2228             v2 = tswap32(vptr[1]);
2229             v3 = tswap32(vptr[2]);
2230             v4 = tswap32(vptr[3]);
2231             v5 = tswap32(vptr[4]);
2232             v6 = tswap32(vptr[5]);
2233             ret = get_errno(target_mmap(v1, v2, v3,
2234                                         target_to_host_bitmask(v4, mmap_flags_tbl),
2235                                         v5, v6));
2236         }
2237 #else
2238         ret = get_errno(target_mmap(arg1, arg2, arg3,
2239                                     target_to_host_bitmask(arg4, mmap_flags_tbl),
2240                                     arg5,
2241                                     arg6));
2242 #endif
2243         break;
2244 #ifdef TARGET_NR_mmap2
2245     case TARGET_NR_mmap2:
2246 #if defined(TARGET_SPARC)
2247 #define MMAP_SHIFT 12
2248 #else
2249 #define MMAP_SHIFT TARGET_PAGE_BITS
2250 #endif
2251         ret = get_errno(target_mmap(arg1, arg2, arg3,
2252                                     target_to_host_bitmask(arg4, mmap_flags_tbl),
2253                                     arg5,
2254                                     arg6 << MMAP_SHIFT));
2255         break;
2256 #endif
2257     case TARGET_NR_munmap:
2258         ret = get_errno(target_munmap(arg1, arg2));
2259         break;
2260     case TARGET_NR_mprotect:
2261         ret = get_errno(target_mprotect(arg1, arg2, arg3));
2262         break;
2263     case TARGET_NR_mremap:
2264         ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
2265         break;
2266     case TARGET_NR_msync:
2267         ret = get_errno(msync((void *)arg1, arg2, arg3));
2268         break;
2269     case TARGET_NR_mlock:
2270         ret = get_errno(mlock((void *)arg1, arg2));
2271         break;
2272     case TARGET_NR_munlock:
2273         ret = get_errno(munlock((void *)arg1, arg2));
2274         break;
2275     case TARGET_NR_mlockall:
2276         ret = get_errno(mlockall(arg1));
2277         break;
2278     case TARGET_NR_munlockall:
2279         ret = get_errno(munlockall());
2280         break;
2281     case TARGET_NR_truncate:
2282         ret = get_errno(truncate((const char *)arg1, arg2));
2283         break;
2284     case TARGET_NR_ftruncate:
2285         ret = get_errno(ftruncate(arg1, arg2));
2286         break;
2287     case TARGET_NR_fchmod:
2288         ret = get_errno(fchmod(arg1, arg2));
2289         break;
2290     case TARGET_NR_getpriority:
2291         ret = get_errno(getpriority(arg1, arg2));
2292         break;
2293     case TARGET_NR_setpriority:
2294         ret = get_errno(setpriority(arg1, arg2, arg3));
2295         break;
2296 #ifdef TARGET_NR_profil
2297     case TARGET_NR_profil:
2298         goto unimplemented;
2299 #endif
2300     case TARGET_NR_statfs:
2301         stfs = (void *)arg2;
2302         ret = get_errno(sys_statfs(path((const char *)arg1), stfs));
2303     convert_statfs:
2304         if (!is_error(ret)) {
2305             tswap32s(&stfs->f_type);
2306             tswap32s(&stfs->f_bsize);
2307             tswap32s(&stfs->f_blocks);
2308             tswap32s(&stfs->f_bfree);
2309             tswap32s(&stfs->f_bavail);
2310             tswap32s(&stfs->f_files);
2311             tswap32s(&stfs->f_ffree);
2312             tswap32s(&stfs->f_fsid.val[0]);
2313             tswap32s(&stfs->f_fsid.val[1]);
2314             tswap32s(&stfs->f_namelen);
2315         }
2316         break;
2317     case TARGET_NR_fstatfs:
2318         stfs = (void *)arg2;
2319         ret = get_errno(sys_fstatfs(arg1, stfs));
2320         goto convert_statfs;
2321 #ifdef TARGET_NR_ioperm
2322     case TARGET_NR_ioperm:
2323         goto unimplemented;
2324 #endif
2325     case TARGET_NR_socketcall:
2326         ret = do_socketcall(arg1, (int32_t *)arg2);
2327         break;
2328     case TARGET_NR_syslog:
2329         goto unimplemented;
2330     case TARGET_NR_setitimer:
2331         {
2332             struct target_itimerval *target_value = (void *)arg2;
2333             struct target_itimerval *target_ovalue = (void *)arg3;
2334             struct itimerval value, ovalue, *pvalue;
2335 
2336             if (target_value) {
2337                 pvalue = &value;
2338                 target_to_host_timeval(&pvalue->it_interval,
2339                                        &target_value->it_interval);
2340                 target_to_host_timeval(&pvalue->it_value,
2341                                        &target_value->it_value);
2342             } else {
2343                 pvalue = NULL;
2344             }
2345             ret = get_errno(setitimer(arg1, pvalue, &ovalue));
2346             if (!is_error(ret) && target_ovalue) {
2347                 host_to_target_timeval(&target_ovalue->it_interval,
2348                                        &ovalue.it_interval);
2349                 host_to_target_timeval(&target_ovalue->it_value,
2350                                        &ovalue.it_value);
2351             }
2352         }
2353         break;
2354     case TARGET_NR_getitimer:
2355         {
2356             struct target_itimerval *target_value = (void *)arg2;
2357             struct itimerval value;
2358 
2359             ret = get_errno(getitimer(arg1, &value));
2360             if (!is_error(ret) && target_value) {
2361                 host_to_target_timeval(&target_value->it_interval,
2362                                        &value.it_interval);
2363                 host_to_target_timeval(&target_value->it_value,
2364                                        &value.it_value);
2365             }
2366         }
2367         break;
2368     case TARGET_NR_stat:
2369         ret = get_errno(stat(path((const char *)arg1), &st));
2370         goto do_stat;
2371     case TARGET_NR_lstat:
2372         ret = get_errno(lstat(path((const char *)arg1), &st));
2373         goto do_stat;
2374     case TARGET_NR_fstat:
2375         {
2376             ret = get_errno(fstat(arg1, &st));
2377         do_stat:
2378             if (!is_error(ret)) {
2379                 struct target_stat *target_st = (void *)arg2;
2380                 target_st->st_dev = tswap16(st.st_dev);
2381                 target_st->st_ino = tswapl(st.st_ino);
2382 #if defined(TARGET_PPC)
2383                 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
2384                 target_st->st_uid = tswap32(st.st_uid);
2385                 target_st->st_gid = tswap32(st.st_gid);
2386 #else
2387                 target_st->st_mode = tswap16(st.st_mode);
2388                 target_st->st_uid = tswap16(st.st_uid);
2389                 target_st->st_gid = tswap16(st.st_gid);
2390 #endif
2391                 target_st->st_nlink = tswap16(st.st_nlink);
2392                 target_st->st_rdev = tswap16(st.st_rdev);
2393                 target_st->st_size = tswapl(st.st_size);
2394                 target_st->st_blksize = tswapl(st.st_blksize);
2395                 target_st->st_blocks = tswapl(st.st_blocks);
2396                 target_st->target_st_atime = tswapl(st.st_atime);
2397                 target_st->target_st_mtime = tswapl(st.st_mtime);
2398                 target_st->target_st_ctime = tswapl(st.st_ctime);
2399             }
2400         }
2401         break;
2402 #ifdef TARGET_NR_olduname
2403     case TARGET_NR_olduname:
2404         goto unimplemented;
2405 #endif
2406 #ifdef TARGET_NR_iopl
2407     case TARGET_NR_iopl:
2408         goto unimplemented;
2409 #endif
2410     case TARGET_NR_vhangup:
2411         ret = get_errno(vhangup());
2412         break;
2413 #ifdef TARGET_NR_idle
2414     case TARGET_NR_idle:
2415         goto unimplemented;
2416 #endif
2417 #ifdef TARGET_NR_syscall
2418     case TARGET_NR_syscall:
2419     	ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
2420     	break;
2421 #endif
2422     case TARGET_NR_wait4:
2423         {
2424             int status;
2425             target_long *status_ptr = (void *)arg2;
2426             struct rusage rusage, *rusage_ptr;
2427             struct target_rusage *target_rusage = (void *)arg4;
2428             if (target_rusage)
2429                 rusage_ptr = &rusage;
2430             else
2431                 rusage_ptr = NULL;
2432             ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
2433             if (!is_error(ret)) {
2434                 if (status_ptr)
2435                     *status_ptr = tswap32(status);
2436                 if (target_rusage) {
2437                     host_to_target_rusage(target_rusage, &rusage);
2438                 }
2439             }
2440         }
2441         break;
2442     case TARGET_NR_swapoff:
2443         ret = get_errno(swapoff((const char *)arg1));
2444         break;
2445     case TARGET_NR_sysinfo:
2446         {
2447             struct target_sysinfo *target_value = (void *)arg1;
2448             struct sysinfo value;
2449             ret = get_errno(sysinfo(&value));
2450             if (!is_error(ret) && target_value)
2451             {
2452                 __put_user(value.uptime, &target_value->uptime);
2453                 __put_user(value.loads[0], &target_value->loads[0]);
2454                 __put_user(value.loads[1], &target_value->loads[1]);
2455                 __put_user(value.loads[2], &target_value->loads[2]);
2456                 __put_user(value.totalram, &target_value->totalram);
2457                 __put_user(value.freeram, &target_value->freeram);
2458                 __put_user(value.sharedram, &target_value->sharedram);
2459                 __put_user(value.bufferram, &target_value->bufferram);
2460                 __put_user(value.totalswap, &target_value->totalswap);
2461                 __put_user(value.freeswap, &target_value->freeswap);
2462                 __put_user(value.procs, &target_value->procs);
2463                 __put_user(value.totalhigh, &target_value->totalhigh);
2464                 __put_user(value.freehigh, &target_value->freehigh);
2465                 __put_user(value.mem_unit, &target_value->mem_unit);
2466             }
2467         }
2468         break;
2469     case TARGET_NR_ipc:
2470 	ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
2471 	break;
2472     case TARGET_NR_fsync:
2473         ret = get_errno(fsync(arg1));
2474         break;
2475     case TARGET_NR_clone:
2476         ret = get_errno(do_fork(cpu_env, arg1, arg2));
2477         break;
2478 #ifdef __NR_exit_group
2479         /* new thread calls */
2480     case TARGET_NR_exit_group:
2481         gdb_exit(cpu_env, arg1);
2482         ret = get_errno(exit_group(arg1));
2483         break;
2484 #endif
2485     case TARGET_NR_setdomainname:
2486         ret = get_errno(setdomainname((const char *)arg1, arg2));
2487         break;
2488     case TARGET_NR_uname:
2489         /* no need to transcode because we use the linux syscall */
2490         {
2491             struct new_utsname * buf;
2492 
2493             buf = (struct new_utsname *)arg1;
2494             ret = get_errno(sys_uname(buf));
2495             if (!is_error(ret)) {
2496                 /* Overrite the native machine name with whatever is being
2497                    emulated. */
2498                 strcpy (buf->machine, UNAME_MACHINE);
2499             }
2500         }
2501         break;
2502 #ifdef TARGET_I386
2503     case TARGET_NR_modify_ldt:
2504         ret = get_errno(do_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
2505         break;
2506     case TARGET_NR_vm86old:
2507         goto unimplemented;
2508     case TARGET_NR_vm86:
2509         ret = do_vm86(cpu_env, arg1, (void *)arg2);
2510         break;
2511 #endif
2512     case TARGET_NR_adjtimex:
2513         goto unimplemented;
2514     case TARGET_NR_create_module:
2515     case TARGET_NR_init_module:
2516     case TARGET_NR_delete_module:
2517     case TARGET_NR_get_kernel_syms:
2518         goto unimplemented;
2519     case TARGET_NR_quotactl:
2520         goto unimplemented;
2521     case TARGET_NR_getpgid:
2522         ret = get_errno(getpgid(arg1));
2523         break;
2524     case TARGET_NR_fchdir:
2525         ret = get_errno(fchdir(arg1));
2526         break;
2527     case TARGET_NR_bdflush:
2528         goto unimplemented;
2529     case TARGET_NR_sysfs:
2530         goto unimplemented;
2531     case TARGET_NR_personality:
2532         ret = get_errno(personality(arg1));
2533         break;
2534     case TARGET_NR_afs_syscall:
2535         goto unimplemented;
2536     case TARGET_NR__llseek:
2537         {
2538 #if defined (__x86_64__)
2539             ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
2540             *(int64_t *)arg4 = ret;
2541 #else
2542             int64_t res;
2543             ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
2544             *(int64_t *)arg4 = tswap64(res);
2545 #endif
2546         }
2547         break;
2548     case TARGET_NR_getdents:
2549 #if TARGET_LONG_SIZE != 4
2550 #warning not supported
2551 #elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
2552         {
2553             struct target_dirent *target_dirp = (void *)arg2;
2554             struct dirent *dirp;
2555             long count = arg3;
2556 
2557 	    dirp = malloc(count);
2558 	    if (!dirp)
2559                 return -ENOMEM;
2560 
2561             ret = get_errno(sys_getdents(arg1, dirp, count));
2562             if (!is_error(ret)) {
2563                 struct dirent *de;
2564 		struct target_dirent *tde;
2565                 int len = ret;
2566                 int reclen, treclen;
2567 		int count1, tnamelen;
2568 
2569 		count1 = 0;
2570                 de = dirp;
2571 		tde = target_dirp;
2572                 while (len > 0) {
2573                     reclen = de->d_reclen;
2574 		    treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
2575                     tde->d_reclen = tswap16(treclen);
2576                     tde->d_ino = tswapl(de->d_ino);
2577                     tde->d_off = tswapl(de->d_off);
2578 		    tnamelen = treclen - (2 * sizeof(target_long) + 2);
2579 		    if (tnamelen > 256)
2580                         tnamelen = 256;
2581                     /* XXX: may not be correct */
2582 		    strncpy(tde->d_name, de->d_name, tnamelen);
2583                     de = (struct dirent *)((char *)de + reclen);
2584                     len -= reclen;
2585                     tde = (struct dirent *)((char *)tde + treclen);
2586 		    count1 += treclen;
2587                 }
2588 		ret = count1;
2589             }
2590 	    free(dirp);
2591         }
2592 #else
2593         {
2594             struct dirent *dirp = (void *)arg2;
2595             long count = arg3;
2596 
2597             ret = get_errno(sys_getdents(arg1, dirp, count));
2598             if (!is_error(ret)) {
2599                 struct dirent *de;
2600                 int len = ret;
2601                 int reclen;
2602                 de = dirp;
2603                 while (len > 0) {
2604                     reclen = de->d_reclen;
2605                     if (reclen > len)
2606                         break;
2607                     de->d_reclen = tswap16(reclen);
2608                     tswapls(&de->d_ino);
2609                     tswapls(&de->d_off);
2610                     de = (struct dirent *)((char *)de + reclen);
2611                     len -= reclen;
2612                 }
2613             }
2614         }
2615 #endif
2616         break;
2617 #ifdef TARGET_NR_getdents64
2618     case TARGET_NR_getdents64:
2619         {
2620             struct dirent64 *dirp = (void *)arg2;
2621             long count = arg3;
2622             ret = get_errno(sys_getdents64(arg1, dirp, count));
2623             if (!is_error(ret)) {
2624                 struct dirent64 *de;
2625                 int len = ret;
2626                 int reclen;
2627                 de = dirp;
2628                 while (len > 0) {
2629                     reclen = de->d_reclen;
2630                     if (reclen > len)
2631                         break;
2632                     de->d_reclen = tswap16(reclen);
2633                     tswap64s(&de->d_ino);
2634                     tswap64s(&de->d_off);
2635                     de = (struct dirent64 *)((char *)de + reclen);
2636                     len -= reclen;
2637                 }
2638             }
2639         }
2640         break;
2641 #endif /* TARGET_NR_getdents64 */
2642     case TARGET_NR__newselect:
2643         ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4,
2644                         (void *)arg5);
2645         break;
2646     case TARGET_NR_poll:
2647         {
2648             struct target_pollfd *target_pfd = (void *)arg1;
2649             unsigned int nfds = arg2;
2650             int timeout = arg3;
2651             struct pollfd *pfd;
2652             unsigned int i;
2653 
2654             pfd = alloca(sizeof(struct pollfd) * nfds);
2655             for(i = 0; i < nfds; i++) {
2656                 pfd[i].fd = tswap32(target_pfd[i].fd);
2657                 pfd[i].events = tswap16(target_pfd[i].events);
2658             }
2659             ret = get_errno(poll(pfd, nfds, timeout));
2660             if (!is_error(ret)) {
2661                 for(i = 0; i < nfds; i++) {
2662                     target_pfd[i].revents = tswap16(pfd[i].revents);
2663                 }
2664             }
2665         }
2666         break;
2667     case TARGET_NR_flock:
2668         /* NOTE: the flock constant seems to be the same for every
2669            Linux platform */
2670         ret = get_errno(flock(arg1, arg2));
2671         break;
2672     case TARGET_NR_readv:
2673         {
2674             int count = arg3;
2675             int i;
2676             struct iovec *vec;
2677             struct target_iovec *target_vec = (void *)arg2;
2678 
2679             vec = alloca(count * sizeof(struct iovec));
2680             for(i = 0;i < count; i++) {
2681                 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2682                 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2683             }
2684             ret = get_errno(readv(arg1, vec, count));
2685         }
2686         break;
2687     case TARGET_NR_writev:
2688         {
2689             int count = arg3;
2690             int i;
2691             struct iovec *vec;
2692             struct target_iovec *target_vec = (void *)arg2;
2693 
2694             vec = alloca(count * sizeof(struct iovec));
2695             for(i = 0;i < count; i++) {
2696                 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2697                 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2698             }
2699             ret = get_errno(writev(arg1, vec, count));
2700         }
2701         break;
2702     case TARGET_NR_getsid:
2703         ret = get_errno(getsid(arg1));
2704         break;
2705     case TARGET_NR_fdatasync:
2706         ret = get_errno(fdatasync(arg1));
2707         break;
2708     case TARGET_NR__sysctl:
2709         /* We don't implement this, but ENODIR is always a safe
2710            return value. */
2711         return -ENOTDIR;
2712     case TARGET_NR_sched_setparam:
2713         {
2714             struct sched_param *target_schp = (void *)arg2;
2715             struct sched_param schp;
2716             schp.sched_priority = tswap32(target_schp->sched_priority);
2717             ret = get_errno(sched_setparam(arg1, &schp));
2718         }
2719         break;
2720     case TARGET_NR_sched_getparam:
2721         {
2722             struct sched_param *target_schp = (void *)arg2;
2723             struct sched_param schp;
2724             ret = get_errno(sched_getparam(arg1, &schp));
2725             if (!is_error(ret)) {
2726                 target_schp->sched_priority = tswap32(schp.sched_priority);
2727             }
2728         }
2729         break;
2730     case TARGET_NR_sched_setscheduler:
2731         {
2732             struct sched_param *target_schp = (void *)arg3;
2733             struct sched_param schp;
2734             schp.sched_priority = tswap32(target_schp->sched_priority);
2735             ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
2736         }
2737         break;
2738     case TARGET_NR_sched_getscheduler:
2739         ret = get_errno(sched_getscheduler(arg1));
2740         break;
2741     case TARGET_NR_sched_yield:
2742         ret = get_errno(sched_yield());
2743         break;
2744     case TARGET_NR_sched_get_priority_max:
2745         ret = get_errno(sched_get_priority_max(arg1));
2746         break;
2747     case TARGET_NR_sched_get_priority_min:
2748         ret = get_errno(sched_get_priority_min(arg1));
2749         break;
2750     case TARGET_NR_sched_rr_get_interval:
2751         {
2752             struct target_timespec *target_ts = (void *)arg2;
2753             struct timespec ts;
2754             ret = get_errno(sched_rr_get_interval(arg1, &ts));
2755             if (!is_error(ret)) {
2756                 target_ts->tv_sec = tswapl(ts.tv_sec);
2757                 target_ts->tv_nsec = tswapl(ts.tv_nsec);
2758             }
2759         }
2760         break;
2761     case TARGET_NR_nanosleep:
2762         {
2763             struct target_timespec *target_req = (void *)arg1;
2764             struct target_timespec *target_rem = (void *)arg2;
2765             struct timespec req, rem;
2766             req.tv_sec = tswapl(target_req->tv_sec);
2767             req.tv_nsec = tswapl(target_req->tv_nsec);
2768             ret = get_errno(nanosleep(&req, &rem));
2769             if (is_error(ret) && target_rem) {
2770                 target_rem->tv_sec = tswapl(rem.tv_sec);
2771                 target_rem->tv_nsec = tswapl(rem.tv_nsec);
2772             }
2773         }
2774         break;
2775     case TARGET_NR_query_module:
2776         goto unimplemented;
2777     case TARGET_NR_nfsservctl:
2778         goto unimplemented;
2779     case TARGET_NR_prctl:
2780         goto unimplemented;
2781 #ifdef TARGET_NR_pread
2782     case TARGET_NR_pread:
2783         page_unprotect_range((void *)arg2, arg3);
2784         ret = get_errno(pread(arg1, (void *)arg2, arg3, arg4));
2785         break;
2786     case TARGET_NR_pwrite:
2787         ret = get_errno(pwrite(arg1, (void *)arg2, arg3, arg4));
2788         break;
2789 #endif
2790     case TARGET_NR_getcwd:
2791         ret = get_errno(sys_getcwd1((char *)arg1, arg2));
2792         break;
2793     case TARGET_NR_capget:
2794         goto unimplemented;
2795     case TARGET_NR_capset:
2796         goto unimplemented;
2797     case TARGET_NR_sigaltstack:
2798         goto unimplemented;
2799     case TARGET_NR_sendfile:
2800         goto unimplemented;
2801 #ifdef TARGET_NR_getpmsg
2802     case TARGET_NR_getpmsg:
2803         goto unimplemented;
2804 #endif
2805 #ifdef TARGET_NR_putpmsg
2806     case TARGET_NR_putpmsg:
2807         goto unimplemented;
2808 #endif
2809 #ifdef TARGET_NR_vfork
2810     case TARGET_NR_vfork:
2811         ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
2812         break;
2813 #endif
2814 #ifdef TARGET_NR_ugetrlimit
2815     case TARGET_NR_ugetrlimit:
2816     {
2817 	struct rlimit rlim;
2818 	ret = get_errno(getrlimit(arg1, &rlim));
2819 	if (!is_error(ret)) {
2820 	    struct target_rlimit *target_rlim = (void *)arg2;
2821 	    target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
2822 	    target_rlim->rlim_max = tswapl(rlim.rlim_max);
2823 	}
2824 	break;
2825     }
2826 #endif
2827 #ifdef TARGET_NR_truncate64
2828     case TARGET_NR_truncate64:
2829 	ret = get_errno(truncate64((const char *)arg1, arg2));
2830 	break;
2831 #endif
2832 #ifdef TARGET_NR_ftruncate64
2833     case TARGET_NR_ftruncate64:
2834 	ret = get_errno(ftruncate64(arg1, arg2));
2835 	break;
2836 #endif
2837 #ifdef TARGET_NR_stat64
2838     case TARGET_NR_stat64:
2839         ret = get_errno(stat(path((const char *)arg1), &st));
2840         goto do_stat64;
2841 #endif
2842 #ifdef TARGET_NR_lstat64
2843     case TARGET_NR_lstat64:
2844         ret = get_errno(lstat(path((const char *)arg1), &st));
2845         goto do_stat64;
2846 #endif
2847 #ifdef TARGET_NR_fstat64
2848     case TARGET_NR_fstat64:
2849         {
2850             ret = get_errno(fstat(arg1, &st));
2851         do_stat64:
2852             if (!is_error(ret)) {
2853                 struct target_stat64 *target_st = (void *)arg2;
2854                 memset(target_st, 0, sizeof(struct target_stat64));
2855                 put_user(st.st_dev, &target_st->st_dev);
2856                 put_user(st.st_ino, &target_st->st_ino);
2857 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
2858                 put_user(st.st_ino, &target_st->__st_ino);
2859 #endif
2860                 put_user(st.st_mode, &target_st->st_mode);
2861                 put_user(st.st_nlink, &target_st->st_nlink);
2862                 put_user(st.st_uid, &target_st->st_uid);
2863                 put_user(st.st_gid, &target_st->st_gid);
2864                 put_user(st.st_rdev, &target_st->st_rdev);
2865                 /* XXX: better use of kernel struct */
2866                 put_user(st.st_size, &target_st->st_size);
2867                 put_user(st.st_blksize, &target_st->st_blksize);
2868                 put_user(st.st_blocks, &target_st->st_blocks);
2869                 put_user(st.st_atime, &target_st->target_st_atime);
2870                 put_user(st.st_mtime, &target_st->target_st_mtime);
2871                 put_user(st.st_ctime, &target_st->target_st_ctime);
2872             }
2873         }
2874         break;
2875 #endif
2876 #ifdef USE_UID16
2877     case TARGET_NR_lchown:
2878         ret = get_errno(lchown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
2879         break;
2880     case TARGET_NR_getuid:
2881         ret = get_errno(high2lowuid(getuid()));
2882         break;
2883     case TARGET_NR_getgid:
2884         ret = get_errno(high2lowgid(getgid()));
2885         break;
2886     case TARGET_NR_geteuid:
2887         ret = get_errno(high2lowuid(geteuid()));
2888         break;
2889     case TARGET_NR_getegid:
2890         ret = get_errno(high2lowgid(getegid()));
2891         break;
2892     case TARGET_NR_setreuid:
2893         ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
2894         break;
2895     case TARGET_NR_setregid:
2896         ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
2897         break;
2898     case TARGET_NR_getgroups:
2899         {
2900             int gidsetsize = arg1;
2901             uint16_t *target_grouplist = (void *)arg2;
2902             gid_t *grouplist;
2903             int i;
2904 
2905             grouplist = alloca(gidsetsize * sizeof(gid_t));
2906             ret = get_errno(getgroups(gidsetsize, grouplist));
2907             if (!is_error(ret)) {
2908                 for(i = 0;i < gidsetsize; i++)
2909                     target_grouplist[i] = tswap16(grouplist[i]);
2910             }
2911         }
2912         break;
2913     case TARGET_NR_setgroups:
2914         {
2915             int gidsetsize = arg1;
2916             uint16_t *target_grouplist = (void *)arg2;
2917             gid_t *grouplist;
2918             int i;
2919 
2920             grouplist = alloca(gidsetsize * sizeof(gid_t));
2921             for(i = 0;i < gidsetsize; i++)
2922                 grouplist[i] = tswap16(target_grouplist[i]);
2923             ret = get_errno(setgroups(gidsetsize, grouplist));
2924         }
2925         break;
2926     case TARGET_NR_fchown:
2927         ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
2928         break;
2929 #ifdef TARGET_NR_setresuid
2930     case TARGET_NR_setresuid:
2931         ret = get_errno(setresuid(low2highuid(arg1),
2932                                   low2highuid(arg2),
2933                                   low2highuid(arg3)));
2934         break;
2935 #endif
2936 #ifdef TARGET_NR_getresuid
2937     case TARGET_NR_getresuid:
2938         {
2939             int ruid, euid, suid;
2940             ret = get_errno(getresuid(&ruid, &euid, &suid));
2941             if (!is_error(ret)) {
2942                 *(uint16_t *)arg1 = tswap16(high2lowuid(ruid));
2943                 *(uint16_t *)arg2 = tswap16(high2lowuid(euid));
2944                 *(uint16_t *)arg3 = tswap16(high2lowuid(suid));
2945             }
2946         }
2947         break;
2948 #endif
2949 #ifdef TARGET_NR_getresgid
2950     case TARGET_NR_setresgid:
2951         ret = get_errno(setresgid(low2highgid(arg1),
2952                                   low2highgid(arg2),
2953                                   low2highgid(arg3)));
2954         break;
2955 #endif
2956 #ifdef TARGET_NR_getresgid
2957     case TARGET_NR_getresgid:
2958         {
2959             int rgid, egid, sgid;
2960             ret = get_errno(getresgid(&rgid, &egid, &sgid));
2961             if (!is_error(ret)) {
2962                 *(uint16_t *)arg1 = tswap16(high2lowgid(rgid));
2963                 *(uint16_t *)arg2 = tswap16(high2lowgid(egid));
2964                 *(uint16_t *)arg3 = tswap16(high2lowgid(sgid));
2965             }
2966         }
2967         break;
2968 #endif
2969     case TARGET_NR_chown:
2970         ret = get_errno(chown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
2971         break;
2972     case TARGET_NR_setuid:
2973         ret = get_errno(setuid(low2highuid(arg1)));
2974         break;
2975     case TARGET_NR_setgid:
2976         ret = get_errno(setgid(low2highgid(arg1)));
2977         break;
2978     case TARGET_NR_setfsuid:
2979         ret = get_errno(setfsuid(arg1));
2980         break;
2981     case TARGET_NR_setfsgid:
2982         ret = get_errno(setfsgid(arg1));
2983         break;
2984 #endif /* USE_UID16 */
2985 
2986 #ifdef TARGET_NR_lchown32
2987     case TARGET_NR_lchown32:
2988         ret = get_errno(lchown((const char *)arg1, arg2, arg3));
2989         break;
2990 #endif
2991 #ifdef TARGET_NR_getuid32
2992     case TARGET_NR_getuid32:
2993         ret = get_errno(getuid());
2994         break;
2995 #endif
2996 #ifdef TARGET_NR_getgid32
2997     case TARGET_NR_getgid32:
2998         ret = get_errno(getgid());
2999         break;
3000 #endif
3001 #ifdef TARGET_NR_geteuid32
3002     case TARGET_NR_geteuid32:
3003         ret = get_errno(geteuid());
3004         break;
3005 #endif
3006 #ifdef TARGET_NR_getegid32
3007     case TARGET_NR_getegid32:
3008         ret = get_errno(getegid());
3009         break;
3010 #endif
3011 #ifdef TARGET_NR_setreuid32
3012     case TARGET_NR_setreuid32:
3013         ret = get_errno(setreuid(arg1, arg2));
3014         break;
3015 #endif
3016 #ifdef TARGET_NR_setregid32
3017     case TARGET_NR_setregid32:
3018         ret = get_errno(setregid(arg1, arg2));
3019         break;
3020 #endif
3021 #ifdef TARGET_NR_getgroups32
3022     case TARGET_NR_getgroups32:
3023         {
3024             int gidsetsize = arg1;
3025             uint32_t *target_grouplist = (void *)arg2;
3026             gid_t *grouplist;
3027             int i;
3028 
3029             grouplist = alloca(gidsetsize * sizeof(gid_t));
3030             ret = get_errno(getgroups(gidsetsize, grouplist));
3031             if (!is_error(ret)) {
3032                 for(i = 0;i < gidsetsize; i++)
3033                     put_user(grouplist[i], &target_grouplist[i]);
3034             }
3035         }
3036         break;
3037 #endif
3038 #ifdef TARGET_NR_setgroups32
3039     case TARGET_NR_setgroups32:
3040         {
3041             int gidsetsize = arg1;
3042             uint32_t *target_grouplist = (void *)arg2;
3043             gid_t *grouplist;
3044             int i;
3045 
3046             grouplist = alloca(gidsetsize * sizeof(gid_t));
3047             for(i = 0;i < gidsetsize; i++)
3048                 get_user(grouplist[i], &target_grouplist[i]);
3049             ret = get_errno(setgroups(gidsetsize, grouplist));
3050         }
3051         break;
3052 #endif
3053 #ifdef TARGET_NR_fchown32
3054     case TARGET_NR_fchown32:
3055         ret = get_errno(fchown(arg1, arg2, arg3));
3056         break;
3057 #endif
3058 #ifdef TARGET_NR_setresuid32
3059     case TARGET_NR_setresuid32:
3060         ret = get_errno(setresuid(arg1, arg2, arg3));
3061         break;
3062 #endif
3063 #ifdef TARGET_NR_getresuid32
3064     case TARGET_NR_getresuid32:
3065         {
3066             int ruid, euid, suid;
3067             ret = get_errno(getresuid(&ruid, &euid, &suid));
3068             if (!is_error(ret)) {
3069                 *(uint32_t *)arg1 = tswap32(ruid);
3070                 *(uint32_t *)arg2 = tswap32(euid);
3071                 *(uint32_t *)arg3 = tswap32(suid);
3072             }
3073         }
3074         break;
3075 #endif
3076 #ifdef TARGET_NR_setresgid32
3077     case TARGET_NR_setresgid32:
3078         ret = get_errno(setresgid(arg1, arg2, arg3));
3079         break;
3080 #endif
3081 #ifdef TARGET_NR_getresgid32
3082     case TARGET_NR_getresgid32:
3083         {
3084             int rgid, egid, sgid;
3085             ret = get_errno(getresgid(&rgid, &egid, &sgid));
3086             if (!is_error(ret)) {
3087                 *(uint32_t *)arg1 = tswap32(rgid);
3088                 *(uint32_t *)arg2 = tswap32(egid);
3089                 *(uint32_t *)arg3 = tswap32(sgid);
3090             }
3091         }
3092         break;
3093 #endif
3094 #ifdef TARGET_NR_chown32
3095     case TARGET_NR_chown32:
3096         ret = get_errno(chown((const char *)arg1, arg2, arg3));
3097         break;
3098 #endif
3099 #ifdef TARGET_NR_setuid32
3100     case TARGET_NR_setuid32:
3101         ret = get_errno(setuid(arg1));
3102         break;
3103 #endif
3104 #ifdef TARGET_NR_setgid32
3105     case TARGET_NR_setgid32:
3106         ret = get_errno(setgid(arg1));
3107         break;
3108 #endif
3109 #ifdef TARGET_NR_setfsuid32
3110     case TARGET_NR_setfsuid32:
3111         ret = get_errno(setfsuid(arg1));
3112         break;
3113 #endif
3114 #ifdef TARGET_NR_setfsgid32
3115     case TARGET_NR_setfsgid32:
3116         ret = get_errno(setfsgid(arg1));
3117         break;
3118 #endif
3119 
3120     case TARGET_NR_pivot_root:
3121         goto unimplemented;
3122 #ifdef TARGET_NR_mincore
3123     case TARGET_NR_mincore:
3124         goto unimplemented;
3125 #endif
3126 #ifdef TARGET_NR_madvise
3127     case TARGET_NR_madvise:
3128         goto unimplemented;
3129 #endif
3130 #if TARGET_LONG_BITS == 32
3131     case TARGET_NR_fcntl64:
3132     {
3133 	struct flock64 fl;
3134 	struct target_flock64 *target_fl = (void *)arg3;
3135 
3136         switch(arg2) {
3137         case F_GETLK64:
3138             ret = get_errno(fcntl(arg1, arg2, &fl));
3139 	    if (ret == 0) {
3140 		target_fl->l_type = tswap16(fl.l_type);
3141 		target_fl->l_whence = tswap16(fl.l_whence);
3142 		target_fl->l_start = tswap64(fl.l_start);
3143 		target_fl->l_len = tswap64(fl.l_len);
3144 		target_fl->l_pid = tswapl(fl.l_pid);
3145 	    }
3146 	    break;
3147 
3148         case F_SETLK64:
3149         case F_SETLKW64:
3150 	    fl.l_type = tswap16(target_fl->l_type);
3151 	    fl.l_whence = tswap16(target_fl->l_whence);
3152 	    fl.l_start = tswap64(target_fl->l_start);
3153 	    fl.l_len = tswap64(target_fl->l_len);
3154 	    fl.l_pid = tswapl(target_fl->l_pid);
3155             ret = get_errno(fcntl(arg1, arg2, &fl));
3156 	    break;
3157         default:
3158             ret = get_errno(do_fcntl(arg1, arg2, arg3));
3159             break;
3160         }
3161 	break;
3162     }
3163 #endif
3164 #ifdef TARGET_NR_security
3165     case TARGET_NR_security:
3166         goto unimplemented;
3167 #endif
3168 #ifdef TARGET_NR_getpagesize
3169     case TARGET_NR_getpagesize:
3170         ret = TARGET_PAGE_SIZE;
3171         break;
3172 #endif
3173     case TARGET_NR_gettid:
3174         ret = get_errno(gettid());
3175         break;
3176     case TARGET_NR_readahead:
3177         goto unimplemented;
3178 #ifdef TARGET_NR_setxattr
3179     case TARGET_NR_setxattr:
3180     case TARGET_NR_lsetxattr:
3181     case TARGET_NR_fsetxattr:
3182     case TARGET_NR_getxattr:
3183     case TARGET_NR_lgetxattr:
3184     case TARGET_NR_fgetxattr:
3185     case TARGET_NR_listxattr:
3186     case TARGET_NR_llistxattr:
3187     case TARGET_NR_flistxattr:
3188     case TARGET_NR_removexattr:
3189     case TARGET_NR_lremovexattr:
3190     case TARGET_NR_fremovexattr:
3191         goto unimplemented_nowarn;
3192 #endif
3193 #ifdef TARGET_NR_set_thread_area
3194     case TARGET_NR_set_thread_area:
3195     case TARGET_NR_get_thread_area:
3196         goto unimplemented_nowarn;
3197 #endif
3198     default:
3199     unimplemented:
3200         gemu_log("qemu: Unsupported syscall: %d\n", num);
3201 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_set_thread_area)
3202     unimplemented_nowarn:
3203 #endif
3204         ret = -ENOSYS;
3205         break;
3206     }
3207  fail:
3208 #ifdef DEBUG
3209     gemu_log(" = %ld\n", ret);
3210 #endif
3211     return ret;
3212 }
3213 
3214