4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <sys/types.h>
36 #include <sys/mount.h>
37 #include <sys/prctl.h>
38 #include <sys/resource.h>
43 #include <sys/socket.h>
46 #include <sys/times.h>
49 #include <sys/statfs.h>
51 #include <sys/sysinfo.h>
52 //#include <sys/user.h>
53 #include <netinet/ip.h>
54 #include <netinet/tcp.h>
56 #define termios host_termios
57 #define winsize host_winsize
58 #define termio host_termio
59 #define sgttyb host_sgttyb /* same as target */
60 #define tchars host_tchars /* same as target */
61 #define ltchars host_ltchars /* same as target */
63 #include <linux/termios.h>
64 #include <linux/unistd.h>
65 #include <linux/utsname.h>
66 #include <linux/cdrom.h>
67 #include <linux/hdreg.h>
68 #include <linux/soundcard.h>
69 #include <linux/dirent.h>
76 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
77 || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
78 /* 16 bit uid wrappers emulation */
82 //#include <linux/msdos_fs.h>
83 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
84 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
95 #define _syscall0(type,name) \
98 return syscall(__NR_##name); \
101 #define _syscall1(type,name,type1,arg1) \
102 type name (type1 arg1) \
104 return syscall(__NR_##name, arg1); \
107 #define _syscall2(type,name,type1,arg1,type2,arg2) \
108 type name (type1 arg1,type2 arg2) \
110 return syscall(__NR_##name, arg1, arg2); \
113 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
114 type name (type1 arg1,type2 arg2,type3 arg3) \
116 return syscall(__NR_##name, arg1, arg2, arg3); \
119 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
120 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
122 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
125 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
127 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
129 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
133 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
134 type5,arg5,type6,arg6) \
135 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
137 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
141 #define __NR_sys_uname __NR_uname
142 #define __NR_sys_faccessat __NR_faccessat
143 #define __NR_sys_fchmodat __NR_fchmodat
144 #define __NR_sys_fchownat __NR_fchownat
145 #define __NR_sys_getcwd1 __NR_getcwd
146 #define __NR_sys_getdents __NR_getdents
147 #define __NR_sys_getdents64 __NR_getdents64
148 #define __NR_sys_getpriority __NR_getpriority
149 #define __NR_sys_linkat __NR_linkat
150 #define __NR_sys_mkdirat __NR_mkdirat
151 #define __NR_sys_mknodat __NR_mknodat
152 #define __NR_sys_openat __NR_openat
153 #define __NR_sys_readlinkat __NR_readlinkat
154 #define __NR_sys_renameat __NR_renameat
155 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
156 #define __NR_sys_symlinkat __NR_symlinkat
157 #define __NR_sys_syslog __NR_syslog
158 #define __NR_sys_tgkill __NR_tgkill
159 #define __NR_sys_tkill __NR_tkill
160 #define __NR_sys_unlinkat __NR_unlinkat
161 #define __NR_sys_utimensat __NR_utimensat
163 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
164 #define __NR__llseek __NR_lseek
168 _syscall0(int, gettid)
170 /* This is a replacement for the host gettid() and must return a host
172 static int gettid(void) {
176 _syscall1(int,sys_uname,struct new_utsname *,buf)
177 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
178 _syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
180 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
181 _syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
182 mode_t,mode,int,flags)
184 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
185 _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
186 uid_t,owner,gid_t,group,int,flags)
188 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
189 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
190 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
191 _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
193 _syscall2(int, sys_getpriority, int, which, int, who);
194 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
195 loff_t *, res, uint, wh);
196 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
197 _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
198 int,newdirfd,const char *,newpath,int,flags)
200 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
201 _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
203 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
204 _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
205 mode_t,mode,dev_t,dev)
207 #if defined(TARGET_NR_openat) && defined(__NR_openat)
208 _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
210 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
211 _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
212 char *,buf,size_t,bufsize)
214 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
215 _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
216 int,newdirfd,const char *,newpath)
218 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
219 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
220 _syscall3(int,sys_symlinkat,const char *,oldpath,
221 int,newdirfd,const char *,newpath)
223 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
224 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
225 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
227 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
228 _syscall2(int,sys_tkill,int,tid,int,sig)
230 #ifdef __NR_exit_group
231 _syscall1(int,exit_group,int,error_code)
233 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234 _syscall1(int,set_tid_address,int *,tidptr)
236 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
237 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
239 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
240 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
241 const struct timespec *,tsp,int,flags)
244 extern int personality(int);
245 extern int flock(int, int);
246 extern int setfsuid(int);
247 extern int setfsgid(int);
248 extern int setresuid(uid_t, uid_t, uid_t);
249 extern int getresuid(uid_t *, uid_t *, uid_t *);
250 extern int setresgid(gid_t, gid_t, gid_t);
251 extern int getresgid(gid_t *, gid_t *, gid_t *);
252 extern int setgroups(int, gid_t *);
254 #define ERRNO_TABLE_SIZE 1200
256 /* target_to_host_errno_table[] is initialized from
257 * host_to_target_errno_table[] in syscall_init(). */
258 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
262 * This list is the union of errno values overridden in asm-<arch>/errno.h
263 * minus the errnos that are not actually generic to all archs.
265 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
266 [EIDRM] = TARGET_EIDRM,
267 [ECHRNG] = TARGET_ECHRNG,
268 [EL2NSYNC] = TARGET_EL2NSYNC,
269 [EL3HLT] = TARGET_EL3HLT,
270 [EL3RST] = TARGET_EL3RST,
271 [ELNRNG] = TARGET_ELNRNG,
272 [EUNATCH] = TARGET_EUNATCH,
273 [ENOCSI] = TARGET_ENOCSI,
274 [EL2HLT] = TARGET_EL2HLT,
275 [EDEADLK] = TARGET_EDEADLK,
276 [ENOLCK] = TARGET_ENOLCK,
277 [EBADE] = TARGET_EBADE,
278 [EBADR] = TARGET_EBADR,
279 [EXFULL] = TARGET_EXFULL,
280 [ENOANO] = TARGET_ENOANO,
281 [EBADRQC] = TARGET_EBADRQC,
282 [EBADSLT] = TARGET_EBADSLT,
283 [EBFONT] = TARGET_EBFONT,
284 [ENOSTR] = TARGET_ENOSTR,
285 [ENODATA] = TARGET_ENODATA,
286 [ETIME] = TARGET_ETIME,
287 [ENOSR] = TARGET_ENOSR,
288 [ENONET] = TARGET_ENONET,
289 [ENOPKG] = TARGET_ENOPKG,
290 [EREMOTE] = TARGET_EREMOTE,
291 [ENOLINK] = TARGET_ENOLINK,
292 [EADV] = TARGET_EADV,
293 [ESRMNT] = TARGET_ESRMNT,
294 [ECOMM] = TARGET_ECOMM,
295 [EPROTO] = TARGET_EPROTO,
296 [EDOTDOT] = TARGET_EDOTDOT,
297 [EMULTIHOP] = TARGET_EMULTIHOP,
298 [EBADMSG] = TARGET_EBADMSG,
299 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
300 [EOVERFLOW] = TARGET_EOVERFLOW,
301 [ENOTUNIQ] = TARGET_ENOTUNIQ,
302 [EBADFD] = TARGET_EBADFD,
303 [EREMCHG] = TARGET_EREMCHG,
304 [ELIBACC] = TARGET_ELIBACC,
305 [ELIBBAD] = TARGET_ELIBBAD,
306 [ELIBSCN] = TARGET_ELIBSCN,
307 [ELIBMAX] = TARGET_ELIBMAX,
308 [ELIBEXEC] = TARGET_ELIBEXEC,
309 [EILSEQ] = TARGET_EILSEQ,
310 [ENOSYS] = TARGET_ENOSYS,
311 [ELOOP] = TARGET_ELOOP,
312 [ERESTART] = TARGET_ERESTART,
313 [ESTRPIPE] = TARGET_ESTRPIPE,
314 [ENOTEMPTY] = TARGET_ENOTEMPTY,
315 [EUSERS] = TARGET_EUSERS,
316 [ENOTSOCK] = TARGET_ENOTSOCK,
317 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
318 [EMSGSIZE] = TARGET_EMSGSIZE,
319 [EPROTOTYPE] = TARGET_EPROTOTYPE,
320 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
321 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
322 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
323 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
324 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
325 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
326 [EADDRINUSE] = TARGET_EADDRINUSE,
327 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
328 [ENETDOWN] = TARGET_ENETDOWN,
329 [ENETUNREACH] = TARGET_ENETUNREACH,
330 [ENETRESET] = TARGET_ENETRESET,
331 [ECONNABORTED] = TARGET_ECONNABORTED,
332 [ECONNRESET] = TARGET_ECONNRESET,
333 [ENOBUFS] = TARGET_ENOBUFS,
334 [EISCONN] = TARGET_EISCONN,
335 [ENOTCONN] = TARGET_ENOTCONN,
336 [EUCLEAN] = TARGET_EUCLEAN,
337 [ENOTNAM] = TARGET_ENOTNAM,
338 [ENAVAIL] = TARGET_ENAVAIL,
339 [EISNAM] = TARGET_EISNAM,
340 [EREMOTEIO] = TARGET_EREMOTEIO,
341 [ESHUTDOWN] = TARGET_ESHUTDOWN,
342 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
343 [ETIMEDOUT] = TARGET_ETIMEDOUT,
344 [ECONNREFUSED] = TARGET_ECONNREFUSED,
345 [EHOSTDOWN] = TARGET_EHOSTDOWN,
346 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
347 [EALREADY] = TARGET_EALREADY,
348 [EINPROGRESS] = TARGET_EINPROGRESS,
349 [ESTALE] = TARGET_ESTALE,
350 [ECANCELED] = TARGET_ECANCELED,
351 [ENOMEDIUM] = TARGET_ENOMEDIUM,
352 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
354 [ENOKEY] = TARGET_ENOKEY,
357 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
360 [EKEYREVOKED] = TARGET_EKEYREVOKED,
363 [EKEYREJECTED] = TARGET_EKEYREJECTED,
366 [EOWNERDEAD] = TARGET_EOWNERDEAD,
368 #ifdef ENOTRECOVERABLE
369 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
373 static inline int host_to_target_errno(int err)
375 if(host_to_target_errno_table[err])
376 return host_to_target_errno_table[err];
380 static inline int target_to_host_errno(int err)
382 if (target_to_host_errno_table[err])
383 return target_to_host_errno_table[err];
387 static inline abi_long get_errno(abi_long ret)
390 return -host_to_target_errno(errno);
395 static inline int is_error(abi_long ret)
397 return (abi_ulong)ret >= (abi_ulong)(-4096);
400 char *target_strerror(int err)
402 return strerror(target_to_host_errno(err));
405 static abi_ulong target_brk;
406 static abi_ulong target_original_brk;
408 void target_set_brk(abi_ulong new_brk)
410 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
413 /* do_brk() must return target values and target errnos. */
414 abi_long do_brk(abi_ulong new_brk)
417 abi_long mapped_addr;
422 if (new_brk < target_original_brk)
423 return -TARGET_ENOMEM;
425 brk_page = HOST_PAGE_ALIGN(target_brk);
427 /* If the new brk is less than this, set it and we're done... */
428 if (new_brk < brk_page) {
429 target_brk = new_brk;
433 /* We need to allocate more memory after the brk... */
434 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
435 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
436 PROT_READ|PROT_WRITE,
437 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
438 if (is_error(mapped_addr)) {
441 target_brk = new_brk;
446 static inline fd_set *target_to_host_fds(fd_set *fds,
447 abi_long *target_fds, int n)
449 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
450 return (fd_set *)target_fds;
455 for(i = 0;i < n; i++) {
456 b = (tswapl(target_fds[i / TARGET_ABI_BITS]) >>
457 (i & (TARGET_ABI_BITS - 1))) & 1;
468 static inline void host_to_target_fds(abi_long *target_fds,
471 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
478 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
480 for(i = 0;i < nw; i++) {
482 for(j = 0; j < TARGET_ABI_BITS; j++) {
483 v |= ((FD_ISSET(k, fds) != 0) << j);
486 target_fds[i] = tswapl(v);
492 #if defined(__alpha__)
498 static inline abi_long host_to_target_clock_t(long ticks)
500 #if HOST_HZ == TARGET_HZ
503 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
507 static inline void host_to_target_rusage(abi_ulong target_addr,
508 const struct rusage *rusage)
510 struct target_rusage *target_rusage;
512 lock_user_struct(target_rusage, target_addr, 0);
513 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
514 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
515 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
516 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
517 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
518 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
519 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
520 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
521 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
522 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
523 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
524 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
525 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
526 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
527 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
528 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
529 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
530 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
531 unlock_user_struct(target_rusage, target_addr, 1);
534 static inline void target_to_host_timeval(struct timeval *tv,
535 abi_ulong target_addr)
537 struct target_timeval *target_tv;
539 lock_user_struct(target_tv, target_addr, 1);
540 tv->tv_sec = tswapl(target_tv->tv_sec);
541 tv->tv_usec = tswapl(target_tv->tv_usec);
542 unlock_user_struct(target_tv, target_addr, 0);
545 static inline void host_to_target_timeval(abi_ulong target_addr,
546 const struct timeval *tv)
548 struct target_timeval *target_tv;
550 lock_user_struct(target_tv, target_addr, 0);
551 target_tv->tv_sec = tswapl(tv->tv_sec);
552 target_tv->tv_usec = tswapl(tv->tv_usec);
553 unlock_user_struct(target_tv, target_addr, 1);
557 /* do_select() must return target values and target errnos. */
558 static abi_long do_select(int n,
559 abi_ulong rfd_p, abi_ulong wfd_p,
560 abi_ulong efd_p, abi_ulong target_tv)
562 fd_set rfds, wfds, efds;
563 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
564 abi_long *target_rfds, *target_wfds, *target_efds;
565 struct timeval tv, *tv_ptr;
570 target_rfds = lock_user(rfd_p, sizeof(abi_long) * n, 1);
571 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
577 target_wfds = lock_user(wfd_p, sizeof(abi_long) * n, 1);
578 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
584 target_efds = lock_user(efd_p, sizeof(abi_long) * n, 1);
585 efds_ptr = target_to_host_fds(&efds, target_efds, n);
592 target_to_host_timeval(&tv, target_tv);
597 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
601 host_to_target_fds(target_rfds, rfds_ptr, n);
602 host_to_target_fds(target_wfds, wfds_ptr, n);
603 host_to_target_fds(target_efds, efds_ptr, n);
606 host_to_target_timeval(target_tv, &tv);
610 unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0);
612 unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0);
614 unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0);
619 static inline void target_to_host_sockaddr(struct sockaddr *addr,
620 abi_ulong target_addr,
623 struct target_sockaddr *target_saddr;
625 target_saddr = lock_user(target_addr, len, 1);
626 memcpy(addr, target_saddr, len);
627 addr->sa_family = tswap16(target_saddr->sa_family);
628 unlock_user(target_saddr, target_addr, 0);
631 static inline void host_to_target_sockaddr(abi_ulong target_addr,
632 struct sockaddr *addr,
635 struct target_sockaddr *target_saddr;
637 target_saddr = lock_user(target_addr, len, 0);
638 memcpy(target_saddr, addr, len);
639 target_saddr->sa_family = tswap16(addr->sa_family);
640 unlock_user(target_saddr, target_addr, len);
643 /* ??? Should this also swap msgh->name? */
644 static inline void target_to_host_cmsg(struct msghdr *msgh,
645 struct target_msghdr *target_msgh)
647 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
648 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
651 while (cmsg && target_cmsg) {
652 void *data = CMSG_DATA(cmsg);
653 void *target_data = TARGET_CMSG_DATA(target_cmsg);
655 int len = tswapl(target_cmsg->cmsg_len)
656 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
658 space += CMSG_SPACE(len);
659 if (space > msgh->msg_controllen) {
660 space -= CMSG_SPACE(len);
661 gemu_log("Host cmsg overflow\n");
665 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
666 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
667 cmsg->cmsg_len = CMSG_LEN(len);
669 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
670 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
671 memcpy(data, target_data, len);
673 int *fd = (int *)data;
674 int *target_fd = (int *)target_data;
675 int i, numfds = len / sizeof(int);
677 for (i = 0; i < numfds; i++)
678 fd[i] = tswap32(target_fd[i]);
681 cmsg = CMSG_NXTHDR(msgh, cmsg);
682 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
685 msgh->msg_controllen = space;
688 /* ??? Should this also swap msgh->name? */
689 static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
692 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
693 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
696 while (cmsg && target_cmsg) {
697 void *data = CMSG_DATA(cmsg);
698 void *target_data = TARGET_CMSG_DATA(target_cmsg);
700 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
702 space += TARGET_CMSG_SPACE(len);
703 if (space > tswapl(target_msgh->msg_controllen)) {
704 space -= TARGET_CMSG_SPACE(len);
705 gemu_log("Target cmsg overflow\n");
709 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
710 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
711 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
713 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
714 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
715 memcpy(target_data, data, len);
717 int *fd = (int *)data;
718 int *target_fd = (int *)target_data;
719 int i, numfds = len / sizeof(int);
721 for (i = 0; i < numfds; i++)
722 target_fd[i] = tswap32(fd[i]);
725 cmsg = CMSG_NXTHDR(msgh, cmsg);
726 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
729 msgh->msg_controllen = tswapl(space);
732 /* do_setsockopt() Must return target values and target errnos. */
733 static abi_long do_setsockopt(int sockfd, int level, int optname,
734 abi_ulong optval, socklen_t optlen)
741 /* TCP options all take an 'int' value. */
742 if (optlen < sizeof(uint32_t))
743 return -TARGET_EINVAL;
745 val = tget32(optval);
746 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
753 case IP_ROUTER_ALERT:
757 case IP_MTU_DISCOVER:
763 case IP_MULTICAST_TTL:
764 case IP_MULTICAST_LOOP:
766 if (optlen >= sizeof(uint32_t)) {
767 val = tget32(optval);
768 } else if (optlen >= 1) {
771 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
777 case TARGET_SOL_SOCKET:
779 /* Options with 'int' argument. */
780 case TARGET_SO_DEBUG:
783 case TARGET_SO_REUSEADDR:
784 optname = SO_REUSEADDR;
789 case TARGET_SO_ERROR:
792 case TARGET_SO_DONTROUTE:
793 optname = SO_DONTROUTE;
795 case TARGET_SO_BROADCAST:
796 optname = SO_BROADCAST;
798 case TARGET_SO_SNDBUF:
801 case TARGET_SO_RCVBUF:
804 case TARGET_SO_KEEPALIVE:
805 optname = SO_KEEPALIVE;
807 case TARGET_SO_OOBINLINE:
808 optname = SO_OOBINLINE;
810 case TARGET_SO_NO_CHECK:
811 optname = SO_NO_CHECK;
813 case TARGET_SO_PRIORITY:
814 optname = SO_PRIORITY;
817 case TARGET_SO_BSDCOMPAT:
818 optname = SO_BSDCOMPAT;
821 case TARGET_SO_PASSCRED:
822 optname = SO_PASSCRED;
824 case TARGET_SO_TIMESTAMP:
825 optname = SO_TIMESTAMP;
827 case TARGET_SO_RCVLOWAT:
828 optname = SO_RCVLOWAT;
830 case TARGET_SO_RCVTIMEO:
831 optname = SO_RCVTIMEO;
833 case TARGET_SO_SNDTIMEO:
834 optname = SO_SNDTIMEO;
840 if (optlen < sizeof(uint32_t))
841 return -TARGET_EINVAL;
843 val = tget32(optval);
844 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
848 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
849 ret = -TARGET_ENOSYS;
854 /* do_getsockopt() Must return target values and target errnos. */
855 static abi_long do_getsockopt(int sockfd, int level, int optname,
856 abi_ulong optval, abi_ulong optlen)
862 case TARGET_SOL_SOCKET:
865 case TARGET_SO_LINGER:
866 case TARGET_SO_RCVTIMEO:
867 case TARGET_SO_SNDTIMEO:
868 case TARGET_SO_PEERCRED:
869 case TARGET_SO_PEERNAME:
870 /* These don't just return a single integer */
877 /* TCP options all take an 'int' value. */
879 len = tget32(optlen);
881 return -TARGET_EINVAL;
883 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
900 case IP_ROUTER_ALERT:
904 case IP_MTU_DISCOVER:
910 case IP_MULTICAST_TTL:
911 case IP_MULTICAST_LOOP:
912 len = tget32(optlen);
914 return -TARGET_EINVAL;
916 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
919 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
924 if (len > sizeof(int))
936 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
938 ret = -TARGET_ENOSYS;
944 static void lock_iovec(struct iovec *vec, abi_ulong target_addr,
947 struct target_iovec *target_vec;
951 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
952 for(i = 0;i < count; i++) {
953 base = tswapl(target_vec[i].iov_base);
954 vec[i].iov_len = tswapl(target_vec[i].iov_len);
955 vec[i].iov_base = lock_user(base, vec[i].iov_len, copy);
957 unlock_user (target_vec, target_addr, 0);
960 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
963 struct target_iovec *target_vec;
967 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
968 for(i = 0;i < count; i++) {
969 base = tswapl(target_vec[i].iov_base);
970 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
972 unlock_user (target_vec, target_addr, 0);
975 /* do_socket() Must return target values and target errnos. */
976 static abi_long do_socket(int domain, int type, int protocol)
978 #if defined(TARGET_MIPS)
980 case TARGET_SOCK_DGRAM:
983 case TARGET_SOCK_STREAM:
986 case TARGET_SOCK_RAW:
989 case TARGET_SOCK_RDM:
992 case TARGET_SOCK_SEQPACKET:
993 type = SOCK_SEQPACKET;
995 case TARGET_SOCK_PACKET:
1000 if (domain == PF_NETLINK)
1001 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1002 return get_errno(socket(domain, type, protocol));
1005 /* do_bind() Must return target values and target errnos. */
1006 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1009 void *addr = alloca(addrlen);
1011 target_to_host_sockaddr(addr, target_addr, addrlen);
1012 return get_errno(bind(sockfd, addr, addrlen));
1015 /* do_connect() Must return target values and target errnos. */
1016 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1019 void *addr = alloca(addrlen);
1021 target_to_host_sockaddr(addr, target_addr, addrlen);
1022 return get_errno(connect(sockfd, addr, addrlen));
1025 /* do_sendrecvmsg() Must return target values and target errnos. */
1026 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1027 int flags, int send)
1030 struct target_msghdr *msgp;
1034 abi_ulong target_vec;
1036 lock_user_struct(msgp, target_msg, 1);
1037 if (msgp->msg_name) {
1038 msg.msg_namelen = tswap32(msgp->msg_namelen);
1039 msg.msg_name = alloca(msg.msg_namelen);
1040 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1043 msg.msg_name = NULL;
1044 msg.msg_namelen = 0;
1046 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1047 msg.msg_control = alloca(msg.msg_controllen);
1048 msg.msg_flags = tswap32(msgp->msg_flags);
1050 count = tswapl(msgp->msg_iovlen);
1051 vec = alloca(count * sizeof(struct iovec));
1052 target_vec = tswapl(msgp->msg_iov);
1053 lock_iovec(vec, target_vec, count, send);
1054 msg.msg_iovlen = count;
1058 target_to_host_cmsg(&msg, msgp);
1059 ret = get_errno(sendmsg(fd, &msg, flags));
1061 ret = get_errno(recvmsg(fd, &msg, flags));
1063 host_to_target_cmsg(msgp, &msg);
1065 unlock_iovec(vec, target_vec, count, !send);
1069 /* do_accept() Must return target values and target errnos. */
1070 static abi_long do_accept(int fd, abi_ulong target_addr,
1071 abi_ulong target_addrlen)
1073 socklen_t addrlen = tget32(target_addrlen);
1074 void *addr = alloca(addrlen);
1077 ret = get_errno(accept(fd, addr, &addrlen));
1078 if (!is_error(ret)) {
1079 host_to_target_sockaddr(target_addr, addr, addrlen);
1080 tput32(target_addrlen, addrlen);
1085 /* do_getpeername() Must return target values and target errnos. */
1086 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1087 abi_ulong target_addrlen)
1089 socklen_t addrlen = tget32(target_addrlen);
1090 void *addr = alloca(addrlen);
1093 ret = get_errno(getpeername(fd, addr, &addrlen));
1094 if (!is_error(ret)) {
1095 host_to_target_sockaddr(target_addr, addr, addrlen);
1096 tput32(target_addrlen, addrlen);
1101 /* do_getsockname() Must return target values and target errnos. */
1102 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1103 abi_ulong target_addrlen)
1105 socklen_t addrlen = tget32(target_addrlen);
1106 void *addr = alloca(addrlen);
1109 ret = get_errno(getsockname(fd, addr, &addrlen));
1110 if (!is_error(ret)) {
1111 host_to_target_sockaddr(target_addr, addr, addrlen);
1112 tput32(target_addrlen, addrlen);
1117 /* do_socketpair() Must return target values and target errnos. */
1118 static abi_long do_socketpair(int domain, int type, int protocol,
1119 abi_ulong target_tab)
1124 ret = get_errno(socketpair(domain, type, protocol, tab));
1125 if (!is_error(ret)) {
1126 tput32(target_tab, tab[0]);
1127 tput32(target_tab + 4, tab[1]);
1132 /* do_sendto() Must return target values and target errnos. */
1133 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1134 abi_ulong target_addr, socklen_t addrlen)
1140 host_msg = lock_user(msg, len, 1);
1142 addr = alloca(addrlen);
1143 target_to_host_sockaddr(addr, target_addr, addrlen);
1144 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1146 ret = get_errno(send(fd, host_msg, len, flags));
1148 unlock_user(host_msg, msg, 0);
1152 /* do_recvfrom() Must return target values and target errnos. */
1153 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1154 abi_ulong target_addr,
1155 abi_ulong target_addrlen)
1162 host_msg = lock_user(msg, len, 0);
1164 addrlen = tget32(target_addrlen);
1165 addr = alloca(addrlen);
1166 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1168 addr = NULL; /* To keep compiler quiet. */
1169 ret = get_errno(recv(fd, host_msg, len, flags));
1171 if (!is_error(ret)) {
1173 host_to_target_sockaddr(target_addr, addr, addrlen);
1174 tput32(target_addrlen, addrlen);
1176 unlock_user(host_msg, msg, len);
1178 unlock_user(host_msg, msg, 0);
1183 #ifdef TARGET_NR_socketcall
1184 /* do_socketcall() Must return target values and target errnos. */
1185 static abi_long do_socketcall(int num, abi_ulong vptr)
1188 const int n = sizeof(abi_ulong);
1193 int domain = tgetl(vptr);
1194 int type = tgetl(vptr + n);
1195 int protocol = tgetl(vptr + 2 * n);
1196 ret = do_socket(domain, type, protocol);
1201 int sockfd = tgetl(vptr);
1202 abi_ulong target_addr = tgetl(vptr + n);
1203 socklen_t addrlen = tgetl(vptr + 2 * n);
1204 ret = do_bind(sockfd, target_addr, addrlen);
1207 case SOCKOP_connect:
1209 int sockfd = tgetl(vptr);
1210 abi_ulong target_addr = tgetl(vptr + n);
1211 socklen_t addrlen = tgetl(vptr + 2 * n);
1212 ret = do_connect(sockfd, target_addr, addrlen);
1217 int sockfd = tgetl(vptr);
1218 int backlog = tgetl(vptr + n);
1219 ret = get_errno(listen(sockfd, backlog));
1224 int sockfd = tgetl(vptr);
1225 abi_ulong target_addr = tgetl(vptr + n);
1226 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1227 ret = do_accept(sockfd, target_addr, target_addrlen);
1230 case SOCKOP_getsockname:
1232 int sockfd = tgetl(vptr);
1233 abi_ulong target_addr = tgetl(vptr + n);
1234 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1235 ret = do_getsockname(sockfd, target_addr, target_addrlen);
1238 case SOCKOP_getpeername:
1240 int sockfd = tgetl(vptr);
1241 abi_ulong target_addr = tgetl(vptr + n);
1242 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1243 ret = do_getpeername(sockfd, target_addr, target_addrlen);
1246 case SOCKOP_socketpair:
1248 int domain = tgetl(vptr);
1249 int type = tgetl(vptr + n);
1250 int protocol = tgetl(vptr + 2 * n);
1251 abi_ulong tab = tgetl(vptr + 3 * n);
1252 ret = do_socketpair(domain, type, protocol, tab);
1257 int sockfd = tgetl(vptr);
1258 abi_ulong msg = tgetl(vptr + n);
1259 size_t len = tgetl(vptr + 2 * n);
1260 int flags = tgetl(vptr + 3 * n);
1261 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1266 int sockfd = tgetl(vptr);
1267 abi_ulong msg = tgetl(vptr + n);
1268 size_t len = tgetl(vptr + 2 * n);
1269 int flags = tgetl(vptr + 3 * n);
1270 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1275 int sockfd = tgetl(vptr);
1276 abi_ulong msg = tgetl(vptr + n);
1277 size_t len = tgetl(vptr + 2 * n);
1278 int flags = tgetl(vptr + 3 * n);
1279 abi_ulong addr = tgetl(vptr + 4 * n);
1280 socklen_t addrlen = tgetl(vptr + 5 * n);
1281 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1284 case SOCKOP_recvfrom:
1286 int sockfd = tgetl(vptr);
1287 abi_ulong msg = tgetl(vptr + n);
1288 size_t len = tgetl(vptr + 2 * n);
1289 int flags = tgetl(vptr + 3 * n);
1290 abi_ulong addr = tgetl(vptr + 4 * n);
1291 abi_ulong addrlen = tgetl(vptr + 5 * n);
1292 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1295 case SOCKOP_shutdown:
1297 int sockfd = tgetl(vptr);
1298 int how = tgetl(vptr + n);
1300 ret = get_errno(shutdown(sockfd, how));
1303 case SOCKOP_sendmsg:
1304 case SOCKOP_recvmsg:
1307 abi_ulong target_msg;
1311 target_msg = tgetl(vptr + n);
1312 flags = tgetl(vptr + 2 * n);
1314 ret = do_sendrecvmsg(fd, target_msg, flags,
1315 (num == SOCKOP_sendmsg));
1318 case SOCKOP_setsockopt:
1320 int sockfd = tgetl(vptr);
1321 int level = tgetl(vptr + n);
1322 int optname = tgetl(vptr + 2 * n);
1323 abi_ulong optval = tgetl(vptr + 3 * n);
1324 socklen_t optlen = tgetl(vptr + 4 * n);
1326 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1329 case SOCKOP_getsockopt:
1331 int sockfd = tgetl(vptr);
1332 int level = tgetl(vptr + n);
1333 int optname = tgetl(vptr + 2 * n);
1334 abi_ulong optval = tgetl(vptr + 3 * n);
1335 abi_ulong poptlen = tgetl(vptr + 4 * n);
1337 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
1341 gemu_log("Unsupported socketcall: %d\n", num);
1342 ret = -TARGET_ENOSYS;
1349 #ifdef TARGET_NR_ipc
1350 #define N_SHM_REGIONS 32
1352 static struct shm_region {
1355 } shm_regions[N_SHM_REGIONS];
1357 struct target_ipc_perm
1364 unsigned short int mode;
1365 unsigned short int __pad1;
1366 unsigned short int __seq;
1367 unsigned short int __pad2;
1368 abi_ulong __unused1;
1369 abi_ulong __unused2;
1372 struct target_semid_ds
1374 struct target_ipc_perm sem_perm;
1375 abi_ulong sem_otime;
1376 abi_ulong __unused1;
1377 abi_ulong sem_ctime;
1378 abi_ulong __unused2;
1379 abi_ulong sem_nsems;
1380 abi_ulong __unused3;
1381 abi_ulong __unused4;
1384 static inline void target_to_host_ipc_perm(struct ipc_perm *host_ip,
1385 abi_ulong target_addr)
1387 struct target_ipc_perm *target_ip;
1388 struct target_semid_ds *target_sd;
1390 lock_user_struct(target_sd, target_addr, 1);
1391 target_ip=&(target_sd->sem_perm);
1392 host_ip->__key = tswapl(target_ip->__key);
1393 host_ip->uid = tswapl(target_ip->uid);
1394 host_ip->gid = tswapl(target_ip->gid);
1395 host_ip->cuid = tswapl(target_ip->cuid);
1396 host_ip->cgid = tswapl(target_ip->cgid);
1397 host_ip->mode = tswapl(target_ip->mode);
1398 unlock_user_struct(target_sd, target_addr, 0);
1401 static inline void host_to_target_ipc_perm(abi_ulong target_addr,
1402 struct ipc_perm *host_ip)
1404 struct target_ipc_perm *target_ip;
1405 struct target_semid_ds *target_sd;
1407 lock_user_struct(target_sd, target_addr, 0);
1408 target_ip = &(target_sd->sem_perm);
1409 target_ip->__key = tswapl(host_ip->__key);
1410 target_ip->uid = tswapl(host_ip->uid);
1411 target_ip->gid = tswapl(host_ip->gid);
1412 target_ip->cuid = tswapl(host_ip->cuid);
1413 target_ip->cgid = tswapl(host_ip->cgid);
1414 target_ip->mode = tswapl(host_ip->mode);
1415 unlock_user_struct(target_sd, target_addr, 1);
1418 static inline void target_to_host_semid_ds(struct semid_ds *host_sd,
1419 abi_ulong target_addr)
1421 struct target_semid_ds *target_sd;
1423 lock_user_struct(target_sd, target_addr, 1);
1424 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1425 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1426 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1427 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1428 unlock_user_struct(target_sd, target_addr, 0);
1431 static inline void host_to_target_semid_ds(abi_ulong target_addr,
1432 struct semid_ds *host_sd)
1434 struct target_semid_ds *target_sd;
1436 lock_user_struct(target_sd, target_addr, 0);
1437 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1438 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1439 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1440 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1441 unlock_user_struct(target_sd, target_addr, 1);
1446 struct semid_ds *buf;
1447 unsigned short *array;
1450 union target_semun {
1453 unsigned short int *array;
1456 static inline void target_to_host_semun(int cmd,
1457 union semun *host_su,
1458 abi_ulong target_addr,
1459 struct semid_ds *ds)
1461 union target_semun *target_su;
1466 lock_user_struct(target_su, target_addr, 1);
1467 target_to_host_semid_ds(ds,target_su->buf);
1469 unlock_user_struct(target_su, target_addr, 0);
1473 lock_user_struct(target_su, target_addr, 1);
1474 host_su->val = tswapl(target_su->val);
1475 unlock_user_struct(target_su, target_addr, 0);
1479 lock_user_struct(target_su, target_addr, 1);
1480 *host_su->array = tswap16(*target_su->array);
1481 unlock_user_struct(target_su, target_addr, 0);
1484 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1488 static inline void host_to_target_semun(int cmd,
1489 abi_ulong target_addr,
1490 union semun *host_su,
1491 struct semid_ds *ds)
1493 union target_semun *target_su;
1498 lock_user_struct(target_su, target_addr, 0);
1499 host_to_target_semid_ds(target_su->buf,ds);
1500 unlock_user_struct(target_su, target_addr, 1);
1504 lock_user_struct(target_su, target_addr, 0);
1505 target_su->val = tswapl(host_su->val);
1506 unlock_user_struct(target_su, target_addr, 1);
1510 lock_user_struct(target_su, target_addr, 0);
1511 *target_su->array = tswap16(*host_su->array);
1512 unlock_user_struct(target_su, target_addr, 1);
1515 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1519 static inline abi_long do_semctl(int first, int second, int third,
1523 struct semid_ds dsarg;
1524 int cmd = third&0xff;
1529 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1530 ret = get_errno(semctl(first, second, cmd, arg));
1531 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1534 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1535 ret = get_errno(semctl(first, second, cmd, arg));
1536 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1539 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1540 ret = get_errno(semctl(first, second, cmd, arg));
1541 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1544 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1545 ret = get_errno(semctl(first, second, cmd, arg));
1546 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1549 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1550 ret = get_errno(semctl(first, second, cmd, arg));
1551 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1554 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1555 ret = get_errno(semctl(first, second, cmd, arg));
1556 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1559 ret = get_errno(semctl(first, second, cmd, arg));
1565 struct target_msqid_ds
1567 struct target_ipc_perm msg_perm;
1568 abi_ulong msg_stime;
1569 abi_ulong __unused1;
1570 abi_ulong msg_rtime;
1571 abi_ulong __unused2;
1572 abi_ulong msg_ctime;
1573 abi_ulong __unused3;
1574 abi_ulong __msg_cbytes;
1576 abi_ulong msg_qbytes;
1577 abi_ulong msg_lspid;
1578 abi_ulong msg_lrpid;
1579 abi_ulong __unused4;
1580 abi_ulong __unused5;
1583 static inline void target_to_host_msqid_ds(struct msqid_ds *host_md,
1584 abi_ulong target_addr)
1586 struct target_msqid_ds *target_md;
1588 lock_user_struct(target_md, target_addr, 1);
1589 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1590 host_md->msg_stime = tswapl(target_md->msg_stime);
1591 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1592 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1593 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1594 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1595 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1596 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1597 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1598 unlock_user_struct(target_md, target_addr, 0);
1601 static inline void host_to_target_msqid_ds(abi_ulong target_addr,
1602 struct msqid_ds *host_md)
1604 struct target_msqid_ds *target_md;
1606 lock_user_struct(target_md, target_addr, 0);
1607 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1608 target_md->msg_stime = tswapl(host_md->msg_stime);
1609 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1610 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1611 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1612 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1613 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1614 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1615 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1616 unlock_user_struct(target_md, target_addr, 1);
1619 static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1621 struct msqid_ds dsarg;
1622 int cmd = second&0xff;
1627 target_to_host_msqid_ds(&dsarg,ptr);
1628 ret = get_errno(msgctl(first, cmd, &dsarg));
1629 host_to_target_msqid_ds(ptr,&dsarg);
1631 ret = get_errno(msgctl(first, cmd, &dsarg));
1636 struct target_msgbuf {
1641 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1642 unsigned int msgsz, int msgflg)
1644 struct target_msgbuf *target_mb;
1645 struct msgbuf *host_mb;
1648 lock_user_struct(target_mb,msgp,0);
1649 host_mb = malloc(msgsz+sizeof(long));
1650 host_mb->mtype = tswapl(target_mb->mtype);
1651 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1652 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1654 unlock_user_struct(target_mb, msgp, 0);
1659 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1660 unsigned int msgsz, int msgtype,
1663 struct target_msgbuf *target_mb;
1664 struct msgbuf *host_mb;
1667 lock_user_struct(target_mb, msgp, 0);
1668 host_mb = malloc(msgsz+sizeof(long));
1669 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1671 memcpy(target_mb->mtext, host_mb->mtext, ret);
1672 target_mb->mtype = tswapl(host_mb->mtype);
1674 unlock_user_struct(target_mb, msgp, 0);
1679 /* ??? This only works with linear mappings. */
1680 /* do_ipc() must return target values and target errnos. */
1681 static abi_long do_ipc(unsigned int call, int first,
1682 int second, int third,
1683 abi_long ptr, abi_long fifth)
1687 unsigned long raddr;
1688 struct shmid_ds shm_info;
1691 version = call >> 16;
1696 ret = get_errno(semop(first,(struct sembuf *) ptr, second));
1700 ret = get_errno(semget(first, second, third));
1704 ret = do_semctl(first, second, third, ptr);
1707 case IPCOP_semtimedop:
1708 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1709 ret = -TARGET_ENOSYS;
1713 ret = get_errno(msgget(first, second));
1717 ret = do_msgsnd(first, ptr, second, third);
1721 ret = do_msgctl(first, second, ptr);
1728 void *__unbounded msgp;
1732 struct ipc_kludge *foo = (struct ipc_kludge *) ptr;
1733 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
1735 ret = do_msgrcv(first, (long)msgp, second, 0, third);
1741 /* SHM_* flags are the same on all linux platforms */
1742 ret = get_errno((long) shmat(first, (void *) ptr, second));
1746 /* find out the length of the shared memory segment */
1748 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1749 if (is_error(ret)) {
1750 /* can't get length, bail out */
1751 shmdt((void *) raddr);
1754 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1755 PAGE_VALID | PAGE_READ |
1756 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1757 for (i = 0; i < N_SHM_REGIONS; ++i) {
1758 if (shm_regions[i].start == 0) {
1759 shm_regions[i].start = raddr;
1760 shm_regions[i].size = shm_info.shm_segsz;
1764 if (put_user(raddr, (abi_ulong *)third))
1765 return -TARGET_EFAULT;
1769 for (i = 0; i < N_SHM_REGIONS; ++i) {
1770 if (shm_regions[i].start == ptr) {
1771 shm_regions[i].start = 0;
1772 page_set_flags(ptr, shm_regions[i].size, 0);
1776 ret = get_errno(shmdt((void *) ptr));
1780 /* IPC_* flag values are the same on all linux platforms */
1781 ret = get_errno(shmget(first, second, third));
1784 /* IPC_* and SHM_* command values are the same on all linux platforms */
1790 ret = get_errno(shmctl(first, second, NULL));
1798 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1799 ret = -TARGET_ENOSYS;
1806 /* kernel structure types definitions */
1809 #define STRUCT(name, list...) STRUCT_ ## name,
1810 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
1812 #include "syscall_types.h"
1815 #undef STRUCT_SPECIAL
1817 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1818 #define STRUCT_SPECIAL(name)
1819 #include "syscall_types.h"
1821 #undef STRUCT_SPECIAL
1823 typedef struct IOCTLEntry {
1824 unsigned int target_cmd;
1825 unsigned int host_cmd;
1828 const argtype arg_type[5];
1831 #define IOC_R 0x0001
1832 #define IOC_W 0x0002
1833 #define IOC_RW (IOC_R | IOC_W)
1835 #define MAX_STRUCT_SIZE 4096
1837 IOCTLEntry ioctl_entries[] = {
1838 #define IOCTL(cmd, access, types...) \
1839 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1844 /* ??? Implement proper locking for ioctls. */
1845 /* do_ioctl() Must return target values and target errnos. */
1846 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
1848 const IOCTLEntry *ie;
1849 const argtype *arg_type;
1851 uint8_t buf_temp[MAX_STRUCT_SIZE];
1857 if (ie->target_cmd == 0) {
1858 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
1859 return -TARGET_ENOSYS;
1861 if (ie->target_cmd == cmd)
1865 arg_type = ie->arg_type;
1867 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
1869 switch(arg_type[0]) {
1872 ret = get_errno(ioctl(fd, ie->host_cmd));
1877 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1881 target_size = thunk_type_size(arg_type, 0);
1882 switch(ie->access) {
1884 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1885 if (!is_error(ret)) {
1886 argptr = lock_user(arg, target_size, 0);
1887 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1888 unlock_user(argptr, arg, target_size);
1892 argptr = lock_user(arg, target_size, 1);
1893 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1894 unlock_user(argptr, arg, 0);
1895 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1899 argptr = lock_user(arg, target_size, 1);
1900 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1901 unlock_user(argptr, arg, 0);
1902 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1903 if (!is_error(ret)) {
1904 argptr = lock_user(arg, target_size, 0);
1905 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1906 unlock_user(argptr, arg, target_size);
1912 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
1913 (long)cmd, arg_type[0]);
1914 ret = -TARGET_ENOSYS;
1920 bitmask_transtbl iflag_tbl[] = {
1921 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1922 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1923 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1924 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1925 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1926 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1927 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1928 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1929 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1930 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1931 { TARGET_IXON, TARGET_IXON, IXON, IXON },
1932 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1933 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1934 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1938 bitmask_transtbl oflag_tbl[] = {
1939 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1940 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1941 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1942 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1943 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1944 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1945 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1946 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1947 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1948 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1949 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1950 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1951 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1952 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1953 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1954 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1955 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1956 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1957 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1958 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1959 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1960 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1961 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1962 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1966 bitmask_transtbl cflag_tbl[] = {
1967 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1968 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1969 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1970 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1971 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1972 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1973 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1974 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1975 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1976 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1977 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1978 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1979 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1980 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1981 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1982 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1983 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1984 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1985 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1986 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1987 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1988 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1989 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1990 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1991 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1992 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1993 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1994 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1995 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1996 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1997 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2001 bitmask_transtbl lflag_tbl[] = {
2002 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2003 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2004 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2005 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2006 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2007 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2008 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2009 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2010 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2011 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2012 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2013 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2014 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2015 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2016 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2020 static void target_to_host_termios (void *dst, const void *src)
2022 struct host_termios *host = dst;
2023 const struct target_termios *target = src;
2026 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2028 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2030 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2032 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2033 host->c_line = target->c_line;
2035 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2036 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2037 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2038 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2039 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2040 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2041 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2042 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2043 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2044 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2045 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2046 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2047 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2048 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2049 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2050 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2051 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2054 static void host_to_target_termios (void *dst, const void *src)
2056 struct target_termios *target = dst;
2057 const struct host_termios *host = src;
2060 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2062 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2064 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2066 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2067 target->c_line = host->c_line;
2069 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2070 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2071 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2072 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2073 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2074 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2075 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2076 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2077 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2078 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2079 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2080 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2081 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2082 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2083 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2084 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2085 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2088 StructEntry struct_termios_def = {
2089 .convert = { host_to_target_termios, target_to_host_termios },
2090 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2091 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2094 static bitmask_transtbl mmap_flags_tbl[] = {
2095 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2096 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2097 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2098 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2099 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2100 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2101 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2102 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2106 static bitmask_transtbl fcntl_flags_tbl[] = {
2107 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2108 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2109 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2110 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2111 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2112 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2113 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2114 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2115 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2116 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2117 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2118 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2119 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2120 #if defined(O_DIRECT)
2121 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
2126 #if defined(TARGET_I386)
2128 /* NOTE: there is really one LDT for all the threads */
2131 static int read_ldt(abi_ulong ptr, unsigned long bytecount)
2138 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2139 if (size > bytecount)
2141 p = lock_user(ptr, size, 0);
2142 /* ??? Shoudl this by byteswapped? */
2143 memcpy(p, ldt_table, size);
2144 unlock_user(p, ptr, size);
2148 /* XXX: add locking support */
2149 /* write_ldt() returns host errnos */
2150 static int write_ldt(CPUX86State *env,
2151 abi_ulong ptr, unsigned long bytecount, int oldmode)
2153 struct target_modify_ldt_ldt_s ldt_info;
2154 struct target_modify_ldt_ldt_s *target_ldt_info;
2155 int seg_32bit, contents, read_exec_only, limit_in_pages;
2156 int seg_not_present, useable;
2157 uint32_t *lp, entry_1, entry_2;
2159 if (bytecount != sizeof(ldt_info))
2161 lock_user_struct(target_ldt_info, ptr, 1);
2162 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2163 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2164 ldt_info.limit = tswap32(target_ldt_info->limit);
2165 ldt_info.flags = tswap32(target_ldt_info->flags);
2166 unlock_user_struct(target_ldt_info, ptr, 0);
2168 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2170 seg_32bit = ldt_info.flags & 1;
2171 contents = (ldt_info.flags >> 1) & 3;
2172 read_exec_only = (ldt_info.flags >> 3) & 1;
2173 limit_in_pages = (ldt_info.flags >> 4) & 1;
2174 seg_not_present = (ldt_info.flags >> 5) & 1;
2175 useable = (ldt_info.flags >> 6) & 1;
2177 if (contents == 3) {
2180 if (seg_not_present == 0)
2183 /* allocate the LDT */
2185 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2188 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2189 env->ldt.base = h2g(ldt_table);
2190 env->ldt.limit = 0xffff;
2193 /* NOTE: same code as Linux kernel */
2194 /* Allow LDTs to be cleared by the user. */
2195 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2198 read_exec_only == 1 &&
2200 limit_in_pages == 0 &&
2201 seg_not_present == 1 &&
2209 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2210 (ldt_info.limit & 0x0ffff);
2211 entry_2 = (ldt_info.base_addr & 0xff000000) |
2212 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2213 (ldt_info.limit & 0xf0000) |
2214 ((read_exec_only ^ 1) << 9) |
2216 ((seg_not_present ^ 1) << 15) |
2218 (limit_in_pages << 23) |
2221 entry_2 |= (useable << 20);
2223 /* Install the new entry ... */
2225 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2226 lp[0] = tswap32(entry_1);
2227 lp[1] = tswap32(entry_2);
2231 /* specific and weird i386 syscalls */
2232 /* do_modify_ldt() returns host errnos (it is inconsistent with the
2233 other do_*() functions which return target errnos). */
2234 int do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, unsigned long bytecount)
2240 ret = read_ldt(ptr, bytecount);
2243 ret = write_ldt(env, ptr, bytecount, 1);
2246 ret = write_ldt(env, ptr, bytecount, 0);
2252 #endif /* defined(TARGET_I386) */
2254 /* this stack is the equivalent of the kernel stack associated with a
2256 #define NEW_STACK_SIZE 8192
2258 static int clone_func(void *arg)
2260 CPUState *env = arg;
2266 /* do_fork() Must return host values and target errnos (unlike most
2267 do_*() functions). */
2268 int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
2275 if (flags & CLONE_VM) {
2276 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2277 memset(ts, 0, sizeof(TaskState));
2278 new_stack = ts->stack;
2280 /* add in task state list */
2281 ts->next = first_task_state;
2282 first_task_state = ts;
2283 /* we create a new CPU instance. */
2284 new_env = cpu_copy(env);
2285 #if defined(TARGET_I386)
2287 newsp = env->regs[R_ESP];
2288 new_env->regs[R_ESP] = newsp;
2289 new_env->regs[R_EAX] = 0;
2290 #elif defined(TARGET_ARM)
2292 newsp = env->regs[13];
2293 new_env->regs[13] = newsp;
2294 new_env->regs[0] = 0;
2295 #elif defined(TARGET_SPARC)
2297 newsp = env->regwptr[22];
2298 new_env->regwptr[22] = newsp;
2299 new_env->regwptr[0] = 0;
2301 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
2302 #elif defined(TARGET_M68K)
2304 newsp = env->aregs[7];
2305 new_env->aregs[7] = newsp;
2306 new_env->dregs[0] = 0;
2307 /* ??? is this sufficient? */
2308 #elif defined(TARGET_MIPS)
2310 newsp = env->gpr[29][env->current_tc];
2311 new_env->gpr[29][env->current_tc] = newsp;
2312 #elif defined(TARGET_PPC)
2314 newsp = env->gpr[1];
2315 new_env->gpr[1] = newsp;
2318 for (i = 7; i < 32; i++)
2319 new_env->gpr[i] = 0;
2321 #elif defined(TARGET_SH4)
2323 newsp = env->gregs[15];
2324 new_env->gregs[15] = newsp;
2326 #elif defined(TARGET_ALPHA)
2328 newsp = env->ir[30];
2329 new_env->ir[30] = newsp;
2333 for (i = 7; i < 30; i++)
2336 #elif defined(TARGET_CRIS)
2338 newsp = env->regs[14];
2339 new_env->regs[14] = newsp;
2341 #error unsupported target CPU
2343 new_env->opaque = ts;
2345 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2347 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2350 /* if no CLONE_VM, we consider it is a fork */
2351 if ((flags & ~CSIGNAL) != 0)
2358 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2361 struct target_flock *target_fl;
2362 struct flock64 fl64;
2363 struct target_flock64 *target_fl64;
2367 case TARGET_F_GETLK:
2368 lock_user_struct(target_fl, arg, 1);
2369 fl.l_type = tswap16(target_fl->l_type);
2370 fl.l_whence = tswap16(target_fl->l_whence);
2371 fl.l_start = tswapl(target_fl->l_start);
2372 fl.l_len = tswapl(target_fl->l_len);
2373 fl.l_pid = tswapl(target_fl->l_pid);
2374 unlock_user_struct(target_fl, arg, 0);
2375 ret = fcntl(fd, cmd, &fl);
2377 lock_user_struct(target_fl, arg, 0);
2378 target_fl->l_type = tswap16(fl.l_type);
2379 target_fl->l_whence = tswap16(fl.l_whence);
2380 target_fl->l_start = tswapl(fl.l_start);
2381 target_fl->l_len = tswapl(fl.l_len);
2382 target_fl->l_pid = tswapl(fl.l_pid);
2383 unlock_user_struct(target_fl, arg, 1);
2387 case TARGET_F_SETLK:
2388 case TARGET_F_SETLKW:
2389 lock_user_struct(target_fl, arg, 1);
2390 fl.l_type = tswap16(target_fl->l_type);
2391 fl.l_whence = tswap16(target_fl->l_whence);
2392 fl.l_start = tswapl(target_fl->l_start);
2393 fl.l_len = tswapl(target_fl->l_len);
2394 fl.l_pid = tswapl(target_fl->l_pid);
2395 unlock_user_struct(target_fl, arg, 0);
2396 ret = fcntl(fd, cmd, &fl);
2399 case TARGET_F_GETLK64:
2400 lock_user_struct(target_fl64, arg, 1);
2401 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2402 fl64.l_whence = tswap16(target_fl64->l_whence);
2403 fl64.l_start = tswapl(target_fl64->l_start);
2404 fl64.l_len = tswapl(target_fl64->l_len);
2405 fl64.l_pid = tswap16(target_fl64->l_pid);
2406 unlock_user_struct(target_fl64, arg, 0);
2407 ret = fcntl(fd, cmd >> 1, &fl64);
2409 lock_user_struct(target_fl64, arg, 0);
2410 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2411 target_fl64->l_whence = tswap16(fl64.l_whence);
2412 target_fl64->l_start = tswapl(fl64.l_start);
2413 target_fl64->l_len = tswapl(fl64.l_len);
2414 target_fl64->l_pid = tswapl(fl64.l_pid);
2415 unlock_user_struct(target_fl64, arg, 1);
2418 case TARGET_F_SETLK64:
2419 case TARGET_F_SETLKW64:
2420 lock_user_struct(target_fl64, arg, 1);
2421 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2422 fl64.l_whence = tswap16(target_fl64->l_whence);
2423 fl64.l_start = tswapl(target_fl64->l_start);
2424 fl64.l_len = tswapl(target_fl64->l_len);
2425 fl64.l_pid = tswap16(target_fl64->l_pid);
2426 unlock_user_struct(target_fl64, arg, 0);
2427 ret = fcntl(fd, cmd >> 1, &fl64);
2431 ret = fcntl(fd, cmd, arg);
2432 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2436 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2440 ret = fcntl(fd, cmd, arg);
2448 static inline int high2lowuid(int uid)
2456 static inline int high2lowgid(int gid)
2464 static inline int low2highuid(int uid)
2466 if ((int16_t)uid == -1)
2472 static inline int low2highgid(int gid)
2474 if ((int16_t)gid == -1)
2480 #endif /* USE_UID16 */
2482 void syscall_init(void)
2485 const argtype *arg_type;
2489 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2490 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
2491 #include "syscall_types.h"
2493 #undef STRUCT_SPECIAL
2495 /* we patch the ioctl size if necessary. We rely on the fact that
2496 no ioctl has all the bits at '1' in the size field */
2498 while (ie->target_cmd != 0) {
2499 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2500 TARGET_IOC_SIZEMASK) {
2501 arg_type = ie->arg_type;
2502 if (arg_type[0] != TYPE_PTR) {
2503 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2508 size = thunk_type_size(arg_type, 0);
2509 ie->target_cmd = (ie->target_cmd &
2510 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2511 (size << TARGET_IOC_SIZESHIFT);
2514 /* Build target_to_host_errno_table[] table from
2515 * host_to_target_errno_table[]. */
2516 for (i=0; i < ERRNO_TABLE_SIZE; i++)
2517 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2519 /* automatic consistency check if same arch */
2520 #if defined(__i386__) && defined(TARGET_I386)
2521 if (ie->target_cmd != ie->host_cmd) {
2522 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2523 ie->target_cmd, ie->host_cmd);
2530 #if TARGET_ABI_BITS == 32
2531 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2533 #ifdef TARGET_WORDS_BIG_ENDIAN
2534 return ((uint64_t)word0 << 32) | word1;
2536 return ((uint64_t)word1 << 32) | word0;
2539 #else /* TARGET_ABI_BITS == 32 */
2540 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2544 #endif /* TARGET_ABI_BITS != 32 */
2546 #ifdef TARGET_NR_truncate64
2547 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2553 if (((CPUARMState *)cpu_env)->eabi)
2559 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2563 #ifdef TARGET_NR_ftruncate64
2564 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2570 if (((CPUARMState *)cpu_env)->eabi)
2576 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2580 static inline void target_to_host_timespec(struct timespec *host_ts,
2581 abi_ulong target_addr)
2583 struct target_timespec *target_ts;
2585 lock_user_struct(target_ts, target_addr, 1);
2586 host_ts->tv_sec = tswapl(target_ts->tv_sec);
2587 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2588 unlock_user_struct(target_ts, target_addr, 0);
2591 static inline void host_to_target_timespec(abi_ulong target_addr,
2592 struct timespec *host_ts)
2594 struct target_timespec *target_ts;
2596 lock_user_struct(target_ts, target_addr, 0);
2597 target_ts->tv_sec = tswapl(host_ts->tv_sec);
2598 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2599 unlock_user_struct(target_ts, target_addr, 1);
2602 /* do_syscall() should always have a single exit point at the end so
2603 that actions, such as logging of syscall results, can be performed.
2604 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
2605 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2606 abi_long arg2, abi_long arg3, abi_long arg4,
2607 abi_long arg5, abi_long arg6)
2615 gemu_log("syscall %d", num);
2618 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
2621 case TARGET_NR_exit:
2625 gdb_exit(cpu_env, arg1);
2626 /* XXX: should free thread stack and CPU env */
2628 ret = 0; /* avoid warning */
2630 case TARGET_NR_read:
2631 page_unprotect_range(arg2, arg3);
2632 p = lock_user(arg2, arg3, 0);
2633 ret = get_errno(read(arg1, p, arg3));
2634 unlock_user(p, arg2, ret);
2636 case TARGET_NR_write:
2637 p = lock_user(arg2, arg3, 1);
2638 ret = get_errno(write(arg1, p, arg3));
2639 unlock_user(p, arg2, 0);
2641 case TARGET_NR_open:
2642 p = lock_user_string(arg1);
2643 ret = get_errno(open(path(p),
2644 target_to_host_bitmask(arg2, fcntl_flags_tbl),
2646 unlock_user(p, arg1, 0);
2648 #if defined(TARGET_NR_openat) && defined(__NR_openat)
2649 case TARGET_NR_openat:
2651 ret = -TARGET_EFAULT;
2654 p = lock_user_string(arg2);
2655 if (!access_ok(VERIFY_READ, p, 1))
2656 /* Don't "goto fail" so that cleanup can happen. */
2657 ret = -TARGET_EFAULT;
2659 ret = get_errno(sys_openat(arg1,
2661 target_to_host_bitmask(arg3, fcntl_flags_tbl),
2664 unlock_user(p, arg2, 0);
2667 case TARGET_NR_close:
2668 ret = get_errno(close(arg1));
2673 case TARGET_NR_fork:
2674 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
2676 #ifdef TARGET_NR_waitpid
2677 case TARGET_NR_waitpid:
2680 ret = get_errno(waitpid(arg1, &status, arg3));
2681 if (!is_error(ret) && arg2)
2682 tput32(arg2, status);
2686 #ifdef TARGET_NR_creat /* not on alpha */
2687 case TARGET_NR_creat:
2688 p = lock_user_string(arg1);
2689 ret = get_errno(creat(p, arg2));
2690 unlock_user(p, arg1, 0);
2693 case TARGET_NR_link:
2696 p = lock_user_string(arg1);
2697 p2 = lock_user_string(arg2);
2698 ret = get_errno(link(p, p2));
2699 unlock_user(p2, arg2, 0);
2700 unlock_user(p, arg1, 0);
2703 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2704 case TARGET_NR_linkat:
2705 if (!arg2 || !arg4) {
2706 ret = -TARGET_EFAULT;
2711 p = lock_user_string(arg2);
2712 p2 = lock_user_string(arg4);
2713 if (!access_ok(VERIFY_READ, p, 1)
2714 || !access_ok(VERIFY_READ, p2, 1))
2715 /* Don't "goto fail" so that cleanup can happen. */
2716 ret = -TARGET_EFAULT;
2718 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
2720 unlock_user(p, arg2, 0);
2722 unlock_user(p2, arg4, 0);
2726 case TARGET_NR_unlink:
2727 p = lock_user_string(arg1);
2728 ret = get_errno(unlink(p));
2729 unlock_user(p, arg1, 0);
2731 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2732 case TARGET_NR_unlinkat:
2734 ret = -TARGET_EFAULT;
2737 p = lock_user_string(arg2);
2738 if (!access_ok(VERIFY_READ, p, 1))
2739 /* Don't "goto fail" so that cleanup can happen. */
2740 ret = -TARGET_EFAULT;
2742 ret = get_errno(sys_unlinkat(arg1, p, arg3));
2744 unlock_user(p, arg2, 0);
2747 case TARGET_NR_execve:
2749 char **argp, **envp;
2752 abi_ulong guest_argp;
2753 abi_ulong guest_envp;
2759 for (gp = guest_argp; tgetl(gp); gp++)
2763 for (gp = guest_envp; tgetl(gp); gp++)
2766 argp = alloca((argc + 1) * sizeof(void *));
2767 envp = alloca((envc + 1) * sizeof(void *));
2769 for (gp = guest_argp, q = argp; ;
2770 gp += sizeof(abi_ulong), q++) {
2774 *q = lock_user_string(addr);
2778 for (gp = guest_envp, q = envp; ;
2779 gp += sizeof(abi_ulong), q++) {
2783 *q = lock_user_string(addr);
2787 p = lock_user_string(arg1);
2788 ret = get_errno(execve(p, argp, envp));
2789 unlock_user(p, arg1, 0);
2791 for (gp = guest_argp, q = argp; *q;
2792 gp += sizeof(abi_ulong), q++) {
2794 unlock_user(*q, addr, 0);
2796 for (gp = guest_envp, q = envp; *q;
2797 gp += sizeof(abi_ulong), q++) {
2799 unlock_user(*q, addr, 0);
2803 case TARGET_NR_chdir:
2804 p = lock_user_string(arg1);
2805 ret = get_errno(chdir(p));
2806 unlock_user(p, arg1, 0);
2808 #ifdef TARGET_NR_time
2809 case TARGET_NR_time:
2812 ret = get_errno(time(&host_time));
2813 if (!is_error(ret) && arg1)
2814 tputl(arg1, host_time);
2818 case TARGET_NR_mknod:
2819 p = lock_user_string(arg1);
2820 ret = get_errno(mknod(p, arg2, arg3));
2821 unlock_user(p, arg1, 0);
2823 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2824 case TARGET_NR_mknodat:
2826 ret = -TARGET_EFAULT;
2829 p = lock_user_string(arg2);
2830 if (!access_ok(VERIFY_READ, p, 1))
2831 /* Don't "goto fail" so that cleanup can happen. */
2832 ret = -TARGET_EFAULT;
2834 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2836 unlock_user(p, arg2, 0);
2839 case TARGET_NR_chmod:
2840 p = lock_user_string(arg1);
2841 ret = get_errno(chmod(p, arg2));
2842 unlock_user(p, arg1, 0);
2844 #ifdef TARGET_NR_break
2845 case TARGET_NR_break:
2848 #ifdef TARGET_NR_oldstat
2849 case TARGET_NR_oldstat:
2852 case TARGET_NR_lseek:
2853 ret = get_errno(lseek(arg1, arg2, arg3));
2855 #ifdef TARGET_NR_getxpid
2856 case TARGET_NR_getxpid:
2858 case TARGET_NR_getpid:
2860 ret = get_errno(getpid());
2862 case TARGET_NR_mount:
2864 /* need to look at the data field */
2866 p = lock_user_string(arg1);
2867 p2 = lock_user_string(arg2);
2868 p3 = lock_user_string(arg3);
2869 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, (const void *)arg5));
2870 unlock_user(p, arg1, 0);
2871 unlock_user(p2, arg2, 0);
2872 unlock_user(p3, arg3, 0);
2875 #ifdef TARGET_NR_umount
2876 case TARGET_NR_umount:
2877 p = lock_user_string(arg1);
2878 ret = get_errno(umount(p));
2879 unlock_user(p, arg1, 0);
2882 #ifdef TARGET_NR_stime /* not on alpha */
2883 case TARGET_NR_stime:
2886 host_time = tgetl(arg1);
2887 ret = get_errno(stime(&host_time));
2891 case TARGET_NR_ptrace:
2893 #ifdef TARGET_NR_alarm /* not on alpha */
2894 case TARGET_NR_alarm:
2898 #ifdef TARGET_NR_oldfstat
2899 case TARGET_NR_oldfstat:
2902 #ifdef TARGET_NR_pause /* not on alpha */
2903 case TARGET_NR_pause:
2904 ret = get_errno(pause());
2907 #ifdef TARGET_NR_utime
2908 case TARGET_NR_utime:
2910 struct utimbuf tbuf, *host_tbuf;
2911 struct target_utimbuf *target_tbuf;
2913 lock_user_struct(target_tbuf, arg2, 1);
2914 tbuf.actime = tswapl(target_tbuf->actime);
2915 tbuf.modtime = tswapl(target_tbuf->modtime);
2916 unlock_user_struct(target_tbuf, arg2, 0);
2921 p = lock_user_string(arg1);
2922 ret = get_errno(utime(p, host_tbuf));
2923 unlock_user(p, arg1, 0);
2927 case TARGET_NR_utimes:
2929 struct timeval *tvp, tv[2];
2931 target_to_host_timeval(&tv[0], arg2);
2932 target_to_host_timeval(&tv[1],
2933 arg2 + sizeof (struct target_timeval));
2938 p = lock_user_string(arg1);
2939 ret = get_errno(utimes(p, tvp));
2940 unlock_user(p, arg1, 0);
2943 #ifdef TARGET_NR_stty
2944 case TARGET_NR_stty:
2947 #ifdef TARGET_NR_gtty
2948 case TARGET_NR_gtty:
2951 case TARGET_NR_access:
2952 p = lock_user_string(arg1);
2953 ret = get_errno(access(p, arg2));
2954 unlock_user(p, arg1, 0);
2956 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
2957 case TARGET_NR_faccessat:
2959 ret = -TARGET_EFAULT;
2962 p = lock_user_string(arg2);
2963 if (!access_ok(VERIFY_READ, p, 1))
2964 /* Don't "goto fail" so that cleanup can happen. */
2965 ret = -TARGET_EFAULT;
2967 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
2969 unlock_user(p, arg2, 0);
2972 #ifdef TARGET_NR_nice /* not on alpha */
2973 case TARGET_NR_nice:
2974 ret = get_errno(nice(arg1));
2977 #ifdef TARGET_NR_ftime
2978 case TARGET_NR_ftime:
2981 case TARGET_NR_sync:
2985 case TARGET_NR_kill:
2986 ret = get_errno(kill(arg1, arg2));
2988 case TARGET_NR_rename:
2991 p = lock_user_string(arg1);
2992 p2 = lock_user_string(arg2);
2993 ret = get_errno(rename(p, p2));
2994 unlock_user(p2, arg2, 0);
2995 unlock_user(p, arg1, 0);
2998 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
2999 case TARGET_NR_renameat:
3000 if (!arg2 || !arg4) {
3001 ret = -TARGET_EFAULT;
3006 p = lock_user_string(arg2);
3007 p2 = lock_user_string(arg4);
3008 if (!access_ok(VERIFY_READ, p, 1)
3009 || !access_ok(VERIFY_READ, p2, 1))
3010 /* Don't "goto fail" so that cleanup can happen. */
3011 ret = -TARGET_EFAULT;
3013 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
3015 unlock_user(p2, arg4, 0);
3017 unlock_user(p, arg2, 0);
3021 case TARGET_NR_mkdir:
3022 p = lock_user_string(arg1);
3023 ret = get_errno(mkdir(p, arg2));
3024 unlock_user(p, arg1, 0);
3026 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3027 case TARGET_NR_mkdirat:
3029 ret = -TARGET_EFAULT;
3032 p = lock_user_string(arg2);
3033 if (!access_ok(VERIFY_READ, p, 1))
3034 /* Don't "goto fail" so that cleanup can happen. */
3035 ret = -TARGET_EFAULT;
3037 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3039 unlock_user(p, arg2, 0);
3042 case TARGET_NR_rmdir:
3043 p = lock_user_string(arg1);
3044 ret = get_errno(rmdir(p));
3045 unlock_user(p, arg1, 0);
3048 ret = get_errno(dup(arg1));
3050 case TARGET_NR_pipe:
3053 ret = get_errno(pipe(host_pipe));
3054 if (!is_error(ret)) {
3055 #if defined(TARGET_MIPS)
3056 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3057 env->gpr[3][env->current_tc] = host_pipe[1];
3060 tput32(arg1, host_pipe[0]);
3061 tput32(arg1 + 4, host_pipe[1]);
3066 case TARGET_NR_times:
3068 struct target_tms *tmsp;
3070 ret = get_errno(times(&tms));
3072 tmsp = lock_user(arg1, sizeof(struct target_tms), 0);
3073 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3074 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3075 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3076 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
3079 ret = host_to_target_clock_t(ret);
3082 #ifdef TARGET_NR_prof
3083 case TARGET_NR_prof:
3086 #ifdef TARGET_NR_signal
3087 case TARGET_NR_signal:
3090 case TARGET_NR_acct:
3091 p = lock_user_string(arg1);
3092 ret = get_errno(acct(path(p)));
3093 unlock_user(p, arg1, 0);
3095 #ifdef TARGET_NR_umount2 /* not on alpha */
3096 case TARGET_NR_umount2:
3097 p = lock_user_string(arg1);
3098 ret = get_errno(umount2(p, arg2));
3099 unlock_user(p, arg1, 0);
3102 #ifdef TARGET_NR_lock
3103 case TARGET_NR_lock:
3106 case TARGET_NR_ioctl:
3107 ret = do_ioctl(arg1, arg2, arg3);
3109 case TARGET_NR_fcntl:
3110 ret = get_errno(do_fcntl(arg1, arg2, arg3));
3112 #ifdef TARGET_NR_mpx
3116 case TARGET_NR_setpgid:
3117 ret = get_errno(setpgid(arg1, arg2));
3119 #ifdef TARGET_NR_ulimit
3120 case TARGET_NR_ulimit:
3123 #ifdef TARGET_NR_oldolduname
3124 case TARGET_NR_oldolduname:
3127 case TARGET_NR_umask:
3128 ret = get_errno(umask(arg1));
3130 case TARGET_NR_chroot:
3131 p = lock_user_string(arg1);
3132 ret = get_errno(chroot(p));
3133 unlock_user(p, arg1, 0);
3135 case TARGET_NR_ustat:
3137 case TARGET_NR_dup2:
3138 ret = get_errno(dup2(arg1, arg2));
3140 #ifdef TARGET_NR_getppid /* not on alpha */
3141 case TARGET_NR_getppid:
3142 ret = get_errno(getppid());
3145 case TARGET_NR_getpgrp:
3146 ret = get_errno(getpgrp());
3148 case TARGET_NR_setsid:
3149 ret = get_errno(setsid());
3151 #ifdef TARGET_NR_sigaction
3152 case TARGET_NR_sigaction:
3154 #if !defined(TARGET_MIPS)
3155 struct target_old_sigaction *old_act;
3156 struct target_sigaction act, oact, *pact;
3158 lock_user_struct(old_act, arg2, 1);
3159 act._sa_handler = old_act->_sa_handler;
3160 target_siginitset(&act.sa_mask, old_act->sa_mask);
3161 act.sa_flags = old_act->sa_flags;
3162 act.sa_restorer = old_act->sa_restorer;
3163 unlock_user_struct(old_act, arg2, 0);
3168 ret = get_errno(do_sigaction(arg1, pact, &oact));
3169 if (!is_error(ret) && arg3) {
3170 lock_user_struct(old_act, arg3, 0);
3171 old_act->_sa_handler = oact._sa_handler;
3172 old_act->sa_mask = oact.sa_mask.sig[0];
3173 old_act->sa_flags = oact.sa_flags;
3174 old_act->sa_restorer = oact.sa_restorer;
3175 unlock_user_struct(old_act, arg3, 1);
3178 struct target_sigaction act, oact, *pact, *old_act;
3181 lock_user_struct(old_act, arg2, 1);
3182 act._sa_handler = old_act->_sa_handler;
3183 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3184 act.sa_flags = old_act->sa_flags;
3185 unlock_user_struct(old_act, arg2, 0);
3191 ret = get_errno(do_sigaction(arg1, pact, &oact));
3193 if (!is_error(ret) && arg3) {
3194 lock_user_struct(old_act, arg3, 0);
3195 old_act->_sa_handler = oact._sa_handler;
3196 old_act->sa_flags = oact.sa_flags;
3197 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3198 old_act->sa_mask.sig[1] = 0;
3199 old_act->sa_mask.sig[2] = 0;
3200 old_act->sa_mask.sig[3] = 0;
3201 unlock_user_struct(old_act, arg3, 1);
3207 case TARGET_NR_rt_sigaction:
3209 struct target_sigaction *act;
3210 struct target_sigaction *oact;
3213 lock_user_struct(act, arg2, 1);
3217 lock_user_struct(oact, arg3, 0);
3220 ret = get_errno(do_sigaction(arg1, act, oact));
3222 unlock_user_struct(act, arg2, 0);
3224 unlock_user_struct(oact, arg3, 1);
3227 #ifdef TARGET_NR_sgetmask /* not on alpha */
3228 case TARGET_NR_sgetmask:
3231 abi_ulong target_set;
3232 sigprocmask(0, NULL, &cur_set);
3233 host_to_target_old_sigset(&target_set, &cur_set);
3238 #ifdef TARGET_NR_ssetmask /* not on alpha */
3239 case TARGET_NR_ssetmask:
3241 sigset_t set, oset, cur_set;
3242 abi_ulong target_set = arg1;
3243 sigprocmask(0, NULL, &cur_set);
3244 target_to_host_old_sigset(&set, &target_set);
3245 sigorset(&set, &set, &cur_set);
3246 sigprocmask(SIG_SETMASK, &set, &oset);
3247 host_to_target_old_sigset(&target_set, &oset);
3252 #ifdef TARGET_NR_sigprocmask
3253 case TARGET_NR_sigprocmask:
3256 sigset_t set, oldset, *set_ptr;
3260 case TARGET_SIG_BLOCK:
3263 case TARGET_SIG_UNBLOCK:
3266 case TARGET_SIG_SETMASK:
3270 ret = -TARGET_EINVAL;
3273 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3274 target_to_host_old_sigset(&set, p);
3275 unlock_user(p, arg2, 0);
3281 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
3282 if (!is_error(ret) && arg3) {
3283 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3284 host_to_target_old_sigset(p, &oldset);
3285 unlock_user(p, arg3, sizeof(target_sigset_t));
3290 case TARGET_NR_rt_sigprocmask:
3293 sigset_t set, oldset, *set_ptr;
3297 case TARGET_SIG_BLOCK:
3300 case TARGET_SIG_UNBLOCK:
3303 case TARGET_SIG_SETMASK:
3307 ret = -TARGET_EINVAL;
3310 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3311 target_to_host_sigset(&set, p);
3312 unlock_user(p, arg2, 0);
3318 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
3319 if (!is_error(ret) && arg3) {
3320 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3321 host_to_target_sigset(p, &oldset);
3322 unlock_user(p, arg3, sizeof(target_sigset_t));
3326 #ifdef TARGET_NR_sigpending
3327 case TARGET_NR_sigpending:
3330 ret = get_errno(sigpending(&set));
3331 if (!is_error(ret)) {
3332 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3333 host_to_target_old_sigset(p, &set);
3334 unlock_user(p, arg1, sizeof(target_sigset_t));
3339 case TARGET_NR_rt_sigpending:
3342 ret = get_errno(sigpending(&set));
3343 if (!is_error(ret)) {
3344 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3345 host_to_target_sigset(p, &set);
3346 unlock_user(p, arg1, sizeof(target_sigset_t));
3350 #ifdef TARGET_NR_sigsuspend
3351 case TARGET_NR_sigsuspend:
3354 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3355 target_to_host_old_sigset(&set, p);
3356 unlock_user(p, arg1, 0);
3357 ret = get_errno(sigsuspend(&set));
3361 case TARGET_NR_rt_sigsuspend:
3364 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3365 target_to_host_sigset(&set, p);
3366 unlock_user(p, arg1, 0);
3367 ret = get_errno(sigsuspend(&set));
3370 case TARGET_NR_rt_sigtimedwait:
3373 struct timespec uts, *puts;
3376 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3377 target_to_host_sigset(&set, p);
3378 unlock_user(p, arg1, 0);
3381 target_to_host_timespec(puts, arg3);
3385 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
3386 if (!is_error(ret) && arg2) {
3387 p = lock_user(arg2, sizeof(target_sigset_t), 0);
3388 host_to_target_siginfo(p, &uinfo);
3389 unlock_user(p, arg2, sizeof(target_sigset_t));
3393 case TARGET_NR_rt_sigqueueinfo:
3396 p = lock_user(arg3, sizeof(target_sigset_t), 1);
3397 target_to_host_siginfo(&uinfo, p);
3398 unlock_user(p, arg1, 0);
3399 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3402 #ifdef TARGET_NR_sigreturn
3403 case TARGET_NR_sigreturn:
3404 /* NOTE: ret is eax, so not transcoding must be done */
3405 ret = do_sigreturn(cpu_env);
3408 case TARGET_NR_rt_sigreturn:
3409 /* NOTE: ret is eax, so not transcoding must be done */
3410 ret = do_rt_sigreturn(cpu_env);
3412 case TARGET_NR_sethostname:
3413 p = lock_user_string(arg1);
3414 ret = get_errno(sethostname(p, arg2));
3415 unlock_user(p, arg1, 0);
3417 case TARGET_NR_setrlimit:
3419 /* XXX: convert resource ? */
3420 int resource = arg1;
3421 struct target_rlimit *target_rlim;
3423 lock_user_struct(target_rlim, arg2, 1);
3424 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3425 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3426 unlock_user_struct(target_rlim, arg2, 0);
3427 ret = get_errno(setrlimit(resource, &rlim));
3430 case TARGET_NR_getrlimit:
3432 /* XXX: convert resource ? */
3433 int resource = arg1;
3434 struct target_rlimit *target_rlim;
3437 ret = get_errno(getrlimit(resource, &rlim));
3438 if (!is_error(ret)) {
3439 lock_user_struct(target_rlim, arg2, 0);
3440 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3441 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3442 unlock_user_struct(target_rlim, arg2, 1);
3446 case TARGET_NR_getrusage:
3448 struct rusage rusage;
3449 ret = get_errno(getrusage(arg1, &rusage));
3450 if (!is_error(ret)) {
3451 host_to_target_rusage(arg2, &rusage);
3455 case TARGET_NR_gettimeofday:
3458 ret = get_errno(gettimeofday(&tv, NULL));
3459 if (!is_error(ret)) {
3460 host_to_target_timeval(arg1, &tv);
3464 case TARGET_NR_settimeofday:
3467 target_to_host_timeval(&tv, arg1);
3468 ret = get_errno(settimeofday(&tv, NULL));
3471 #ifdef TARGET_NR_select
3472 case TARGET_NR_select:
3474 struct target_sel_arg_struct *sel;
3475 abi_ulong inp, outp, exp, tvp;
3478 lock_user_struct(sel, arg1, 1);
3479 nsel = tswapl(sel->n);
3480 inp = tswapl(sel->inp);
3481 outp = tswapl(sel->outp);
3482 exp = tswapl(sel->exp);
3483 tvp = tswapl(sel->tvp);
3484 unlock_user_struct(sel, arg1, 0);
3485 ret = do_select(nsel, inp, outp, exp, tvp);
3489 case TARGET_NR_symlink:
3492 p = lock_user_string(arg1);
3493 p2 = lock_user_string(arg2);
3494 ret = get_errno(symlink(p, p2));
3495 unlock_user(p2, arg2, 0);
3496 unlock_user(p, arg1, 0);
3499 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3500 case TARGET_NR_symlinkat:
3501 if (!arg1 || !arg3) {
3502 ret = -TARGET_EFAULT;
3507 p = lock_user_string(arg1);
3508 p2 = lock_user_string(arg3);
3509 if (!access_ok(VERIFY_READ, p, 1)
3510 || !access_ok(VERIFY_READ, p2, 1))
3511 /* Don't "goto fail" so that cleanup can happen. */
3512 ret = -TARGET_EFAULT;
3514 ret = get_errno(sys_symlinkat(p, arg2, p2));
3516 unlock_user(p2, arg3, 0);
3518 unlock_user(p, arg1, 0);
3522 #ifdef TARGET_NR_oldlstat
3523 case TARGET_NR_oldlstat:
3526 case TARGET_NR_readlink:
3529 p = lock_user_string(arg1);
3530 p2 = lock_user(arg2, arg3, 0);
3531 ret = get_errno(readlink(path(p), p2, arg3));
3532 unlock_user(p2, arg2, ret);
3533 unlock_user(p, arg1, 0);
3536 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3537 case TARGET_NR_readlinkat:
3538 if (!arg2 || !arg3) {
3539 ret = -TARGET_EFAULT;
3544 p = lock_user_string(arg2);
3545 p2 = lock_user(arg3, arg4, 0);
3546 if (!access_ok(VERIFY_READ, p, 1)
3547 || !access_ok(VERIFY_READ, p2, 1))
3548 /* Don't "goto fail" so that cleanup can happen. */
3549 ret = -TARGET_EFAULT;
3551 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
3553 unlock_user(p2, arg3, ret);
3555 unlock_user(p, arg2, 0);
3559 #ifdef TARGET_NR_uselib
3560 case TARGET_NR_uselib:
3563 #ifdef TARGET_NR_swapon
3564 case TARGET_NR_swapon:
3565 p = lock_user_string(arg1);
3566 ret = get_errno(swapon(p, arg2));
3567 unlock_user(p, arg1, 0);
3570 case TARGET_NR_reboot:
3572 #ifdef TARGET_NR_readdir
3573 case TARGET_NR_readdir:
3576 #ifdef TARGET_NR_mmap
3577 case TARGET_NR_mmap:
3578 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
3581 abi_ulong v1, v2, v3, v4, v5, v6;
3582 v = lock_user(arg1, 6 * sizeof(abi_ulong), 1);
3589 unlock_user(v, arg1, 0);
3590 ret = get_errno(target_mmap(v1, v2, v3,
3591 target_to_host_bitmask(v4, mmap_flags_tbl),
3595 ret = get_errno(target_mmap(arg1, arg2, arg3,
3596 target_to_host_bitmask(arg4, mmap_flags_tbl),
3602 #ifdef TARGET_NR_mmap2
3603 case TARGET_NR_mmap2:
3604 #if defined(TARGET_SPARC) || defined(TARGET_MIPS)
3605 #define MMAP_SHIFT 12
3607 #define MMAP_SHIFT TARGET_PAGE_BITS
3609 ret = get_errno(target_mmap(arg1, arg2, arg3,
3610 target_to_host_bitmask(arg4, mmap_flags_tbl),
3612 arg6 << MMAP_SHIFT));
3615 case TARGET_NR_munmap:
3616 ret = get_errno(target_munmap(arg1, arg2));
3618 case TARGET_NR_mprotect:
3619 ret = get_errno(target_mprotect(arg1, arg2, arg3));
3621 #ifdef TARGET_NR_mremap
3622 case TARGET_NR_mremap:
3623 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
3626 /* ??? msync/mlock/munlock are broken for softmmu. */
3627 #ifdef TARGET_NR_msync
3628 case TARGET_NR_msync:
3629 ret = get_errno(msync(g2h(arg1), arg2, arg3));
3632 #ifdef TARGET_NR_mlock
3633 case TARGET_NR_mlock:
3634 ret = get_errno(mlock(g2h(arg1), arg2));
3637 #ifdef TARGET_NR_munlock
3638 case TARGET_NR_munlock:
3639 ret = get_errno(munlock(g2h(arg1), arg2));
3642 #ifdef TARGET_NR_mlockall
3643 case TARGET_NR_mlockall:
3644 ret = get_errno(mlockall(arg1));
3647 #ifdef TARGET_NR_munlockall
3648 case TARGET_NR_munlockall:
3649 ret = get_errno(munlockall());
3652 case TARGET_NR_truncate:
3653 p = lock_user_string(arg1);
3654 ret = get_errno(truncate(p, arg2));
3655 unlock_user(p, arg1, 0);
3657 case TARGET_NR_ftruncate:
3658 ret = get_errno(ftruncate(arg1, arg2));
3660 case TARGET_NR_fchmod:
3661 ret = get_errno(fchmod(arg1, arg2));
3663 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3664 case TARGET_NR_fchmodat:
3666 ret = -TARGET_EFAULT;
3669 p = lock_user_string(arg2);
3670 if (!access_ok(VERIFY_READ, p, 1))
3671 /* Don't "goto fail" so that cleanup can happen. */
3672 ret = -TARGET_EFAULT;
3674 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3676 unlock_user(p, arg2, 0);
3679 case TARGET_NR_getpriority:
3680 /* libc does special remapping of the return value of
3681 * sys_getpriority() so it's just easiest to call
3682 * sys_getpriority() directly rather than through libc. */
3683 ret = sys_getpriority(arg1, arg2);
3685 case TARGET_NR_setpriority:
3686 ret = get_errno(setpriority(arg1, arg2, arg3));
3688 #ifdef TARGET_NR_profil
3689 case TARGET_NR_profil:
3692 case TARGET_NR_statfs:
3693 p = lock_user_string(arg1);
3694 ret = get_errno(statfs(path(p), &stfs));
3695 unlock_user(p, arg1, 0);
3697 if (!is_error(ret)) {
3698 struct target_statfs *target_stfs;
3700 lock_user_struct(target_stfs, arg2, 0);
3701 /* ??? put_user is probably wrong. */
3702 put_user(stfs.f_type, &target_stfs->f_type);
3703 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3704 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3705 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3706 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3707 put_user(stfs.f_files, &target_stfs->f_files);
3708 put_user(stfs.f_ffree, &target_stfs->f_ffree);
3709 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3710 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3711 put_user(stfs.f_namelen, &target_stfs->f_namelen);
3712 unlock_user_struct(target_stfs, arg2, 1);
3715 case TARGET_NR_fstatfs:
3716 ret = get_errno(fstatfs(arg1, &stfs));
3717 goto convert_statfs;
3718 #ifdef TARGET_NR_statfs64
3719 case TARGET_NR_statfs64:
3720 p = lock_user_string(arg1);
3721 ret = get_errno(statfs(path(p), &stfs));
3722 unlock_user(p, arg1, 0);
3724 if (!is_error(ret)) {
3725 struct target_statfs64 *target_stfs;
3727 lock_user_struct(target_stfs, arg3, 0);
3728 /* ??? put_user is probably wrong. */
3729 put_user(stfs.f_type, &target_stfs->f_type);
3730 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3731 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3732 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3733 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3734 put_user(stfs.f_files, &target_stfs->f_files);
3735 put_user(stfs.f_ffree, &target_stfs->f_ffree);
3736 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3737 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3738 put_user(stfs.f_namelen, &target_stfs->f_namelen);
3739 unlock_user_struct(target_stfs, arg3, 0);
3742 case TARGET_NR_fstatfs64:
3743 ret = get_errno(fstatfs(arg1, &stfs));
3744 goto convert_statfs64;
3746 #ifdef TARGET_NR_ioperm
3747 case TARGET_NR_ioperm:
3750 #ifdef TARGET_NR_socketcall
3751 case TARGET_NR_socketcall:
3752 ret = do_socketcall(arg1, arg2);
3755 #ifdef TARGET_NR_accept
3756 case TARGET_NR_accept:
3757 ret = do_accept(arg1, arg2, arg3);
3760 #ifdef TARGET_NR_bind
3761 case TARGET_NR_bind:
3762 ret = do_bind(arg1, arg2, arg3);
3765 #ifdef TARGET_NR_connect
3766 case TARGET_NR_connect:
3767 ret = do_connect(arg1, arg2, arg3);
3770 #ifdef TARGET_NR_getpeername
3771 case TARGET_NR_getpeername:
3772 ret = do_getpeername(arg1, arg2, arg3);
3775 #ifdef TARGET_NR_getsockname
3776 case TARGET_NR_getsockname:
3777 ret = do_getsockname(arg1, arg2, arg3);
3780 #ifdef TARGET_NR_getsockopt
3781 case TARGET_NR_getsockopt:
3782 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3785 #ifdef TARGET_NR_listen
3786 case TARGET_NR_listen:
3787 ret = get_errno(listen(arg1, arg2));
3790 #ifdef TARGET_NR_recv
3791 case TARGET_NR_recv:
3792 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3795 #ifdef TARGET_NR_recvfrom
3796 case TARGET_NR_recvfrom:
3797 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3800 #ifdef TARGET_NR_recvmsg
3801 case TARGET_NR_recvmsg:
3802 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3805 #ifdef TARGET_NR_send
3806 case TARGET_NR_send:
3807 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3810 #ifdef TARGET_NR_sendmsg
3811 case TARGET_NR_sendmsg:
3812 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3815 #ifdef TARGET_NR_sendto
3816 case TARGET_NR_sendto:
3817 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3820 #ifdef TARGET_NR_shutdown
3821 case TARGET_NR_shutdown:
3822 ret = get_errno(shutdown(arg1, arg2));
3825 #ifdef TARGET_NR_socket
3826 case TARGET_NR_socket:
3827 ret = do_socket(arg1, arg2, arg3);
3830 #ifdef TARGET_NR_socketpair
3831 case TARGET_NR_socketpair:
3832 ret = do_socketpair(arg1, arg2, arg3, arg4);
3835 #ifdef TARGET_NR_setsockopt
3836 case TARGET_NR_setsockopt:
3837 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3841 case TARGET_NR_syslog:
3842 p = lock_user_string(arg2);
3843 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3844 unlock_user(p, arg2, 0);
3847 case TARGET_NR_setitimer:
3849 struct itimerval value, ovalue, *pvalue;
3853 target_to_host_timeval(&pvalue->it_interval,
3855 target_to_host_timeval(&pvalue->it_value,
3856 arg2 + sizeof(struct target_timeval));
3860 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
3861 if (!is_error(ret) && arg3) {
3862 host_to_target_timeval(arg3,
3863 &ovalue.it_interval);
3864 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
3869 case TARGET_NR_getitimer:
3871 struct itimerval value;
3873 ret = get_errno(getitimer(arg1, &value));
3874 if (!is_error(ret) && arg2) {
3875 host_to_target_timeval(arg2,
3876 &value.it_interval);
3877 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
3882 case TARGET_NR_stat:
3883 p = lock_user_string(arg1);
3884 ret = get_errno(stat(path(p), &st));
3885 unlock_user(p, arg1, 0);
3887 case TARGET_NR_lstat:
3888 p = lock_user_string(arg1);
3889 ret = get_errno(lstat(path(p), &st));
3890 unlock_user(p, arg1, 0);
3892 case TARGET_NR_fstat:
3894 ret = get_errno(fstat(arg1, &st));
3896 if (!is_error(ret)) {
3897 struct target_stat *target_st;
3899 lock_user_struct(target_st, arg2, 0);
3900 #if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32))
3901 target_st->st_dev = tswap32(st.st_dev);
3903 target_st->st_dev = tswap16(st.st_dev);
3905 target_st->st_ino = tswapl(st.st_ino);
3906 #if defined(TARGET_PPC) || defined(TARGET_MIPS)
3907 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
3908 target_st->st_uid = tswap32(st.st_uid);
3909 target_st->st_gid = tswap32(st.st_gid);
3910 #elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
3911 target_st->st_mode = tswap32(st.st_mode);
3912 target_st->st_uid = tswap32(st.st_uid);
3913 target_st->st_gid = tswap32(st.st_gid);
3915 target_st->st_mode = tswap16(st.st_mode);
3916 target_st->st_uid = tswap16(st.st_uid);
3917 target_st->st_gid = tswap16(st.st_gid);
3919 #if defined(TARGET_MIPS)
3920 /* If this is the same on PPC, then just merge w/ the above ifdef */
3921 target_st->st_nlink = tswapl(st.st_nlink);
3922 target_st->st_rdev = tswapl(st.st_rdev);
3923 #elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
3924 target_st->st_nlink = tswap32(st.st_nlink);
3925 target_st->st_rdev = tswap32(st.st_rdev);
3927 target_st->st_nlink = tswap16(st.st_nlink);
3928 target_st->st_rdev = tswap16(st.st_rdev);
3930 target_st->st_size = tswapl(st.st_size);
3931 target_st->st_blksize = tswapl(st.st_blksize);
3932 target_st->st_blocks = tswapl(st.st_blocks);
3933 target_st->target_st_atime = tswapl(st.st_atime);
3934 target_st->target_st_mtime = tswapl(st.st_mtime);
3935 target_st->target_st_ctime = tswapl(st.st_ctime);
3936 unlock_user_struct(target_st, arg2, 1);
3940 #ifdef TARGET_NR_olduname
3941 case TARGET_NR_olduname:
3944 #ifdef TARGET_NR_iopl
3945 case TARGET_NR_iopl:
3948 case TARGET_NR_vhangup:
3949 ret = get_errno(vhangup());
3951 #ifdef TARGET_NR_idle
3952 case TARGET_NR_idle:
3955 #ifdef TARGET_NR_syscall
3956 case TARGET_NR_syscall:
3957 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
3960 case TARGET_NR_wait4:
3963 abi_long status_ptr = arg2;
3964 struct rusage rusage, *rusage_ptr;
3965 abi_ulong target_rusage = arg4;
3967 rusage_ptr = &rusage;
3970 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
3971 if (!is_error(ret)) {
3973 tputl(status_ptr, status);
3974 if (target_rusage) {
3975 host_to_target_rusage(target_rusage, &rusage);
3980 #ifdef TARGET_NR_swapoff
3981 case TARGET_NR_swapoff:
3982 p = lock_user_string(arg1);
3983 ret = get_errno(swapoff(p));
3984 unlock_user(p, arg1, 0);
3987 case TARGET_NR_sysinfo:
3989 struct target_sysinfo *target_value;
3990 struct sysinfo value;
3991 ret = get_errno(sysinfo(&value));
3992 if (!is_error(ret) && arg1)
3994 /* ??? __put_user is probably wrong. */
3995 lock_user_struct(target_value, arg1, 0);
3996 __put_user(value.uptime, &target_value->uptime);
3997 __put_user(value.loads[0], &target_value->loads[0]);
3998 __put_user(value.loads[1], &target_value->loads[1]);
3999 __put_user(value.loads[2], &target_value->loads[2]);
4000 __put_user(value.totalram, &target_value->totalram);
4001 __put_user(value.freeram, &target_value->freeram);
4002 __put_user(value.sharedram, &target_value->sharedram);
4003 __put_user(value.bufferram, &target_value->bufferram);
4004 __put_user(value.totalswap, &target_value->totalswap);
4005 __put_user(value.freeswap, &target_value->freeswap);
4006 __put_user(value.procs, &target_value->procs);
4007 __put_user(value.totalhigh, &target_value->totalhigh);
4008 __put_user(value.freehigh, &target_value->freehigh);
4009 __put_user(value.mem_unit, &target_value->mem_unit);
4010 unlock_user_struct(target_value, arg1, 1);
4014 #ifdef TARGET_NR_ipc
4016 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4019 case TARGET_NR_fsync:
4020 ret = get_errno(fsync(arg1));
4022 case TARGET_NR_clone:
4023 ret = get_errno(do_fork(cpu_env, arg1, arg2));
4025 #ifdef __NR_exit_group
4026 /* new thread calls */
4027 case TARGET_NR_exit_group:
4028 gdb_exit(cpu_env, arg1);
4029 ret = get_errno(exit_group(arg1));
4032 case TARGET_NR_setdomainname:
4033 p = lock_user_string(arg1);
4034 ret = get_errno(setdomainname(p, arg2));
4035 unlock_user(p, arg1, 0);
4037 case TARGET_NR_uname:
4038 /* no need to transcode because we use the linux syscall */
4040 struct new_utsname * buf;
4042 lock_user_struct(buf, arg1, 0);
4043 ret = get_errno(sys_uname(buf));
4044 if (!is_error(ret)) {
4045 /* Overrite the native machine name with whatever is being
4047 strcpy (buf->machine, UNAME_MACHINE);
4048 /* Allow the user to override the reported release. */
4049 if (qemu_uname_release && *qemu_uname_release)
4050 strcpy (buf->release, qemu_uname_release);
4052 unlock_user_struct(buf, arg1, 1);
4056 case TARGET_NR_modify_ldt:
4057 ret = get_errno(do_modify_ldt(cpu_env, arg1, arg2, arg3));
4059 #if !defined(TARGET_X86_64)
4060 case TARGET_NR_vm86old:
4062 case TARGET_NR_vm86:
4063 ret = do_vm86(cpu_env, arg1, arg2);
4067 case TARGET_NR_adjtimex:
4069 #ifdef TARGET_NR_create_module
4070 case TARGET_NR_create_module:
4072 case TARGET_NR_init_module:
4073 case TARGET_NR_delete_module:
4074 #ifdef TARGET_NR_get_kernel_syms
4075 case TARGET_NR_get_kernel_syms:
4078 case TARGET_NR_quotactl:
4080 case TARGET_NR_getpgid:
4081 ret = get_errno(getpgid(arg1));
4083 case TARGET_NR_fchdir:
4084 ret = get_errno(fchdir(arg1));
4086 #ifdef TARGET_NR_bdflush /* not on x86_64 */
4087 case TARGET_NR_bdflush:
4090 #ifdef TARGET_NR_sysfs
4091 case TARGET_NR_sysfs:
4094 case TARGET_NR_personality:
4095 ret = get_errno(personality(arg1));
4097 #ifdef TARGET_NR_afs_syscall
4098 case TARGET_NR_afs_syscall:
4101 #ifdef TARGET_NR__llseek /* Not on alpha */
4102 case TARGET_NR__llseek:
4104 #if defined (__x86_64__)
4105 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
4109 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4115 case TARGET_NR_getdents:
4116 #if TARGET_ABI_BITS != 32
4118 #warning not supported
4119 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4121 struct target_dirent *target_dirp;
4122 struct dirent *dirp;
4123 abi_long count = arg3;
4125 dirp = malloc(count);
4127 ret = -TARGET_EFAULT;
4131 ret = get_errno(sys_getdents(arg1, dirp, count));
4132 if (!is_error(ret)) {
4134 struct target_dirent *tde;
4136 int reclen, treclen;
4137 int count1, tnamelen;
4141 target_dirp = lock_user(arg2, count, 0);
4144 reclen = de->d_reclen;
4145 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4146 tde->d_reclen = tswap16(treclen);
4147 tde->d_ino = tswapl(de->d_ino);
4148 tde->d_off = tswapl(de->d_off);
4149 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4152 /* XXX: may not be correct */
4153 strncpy(tde->d_name, de->d_name, tnamelen);
4154 de = (struct dirent *)((char *)de + reclen);
4156 tde = (struct target_dirent *)((char *)tde + treclen);
4161 unlock_user(target_dirp, arg2, ret);
4166 struct dirent *dirp;
4167 abi_long count = arg3;
4169 dirp = lock_user(arg2, count, 0);
4170 ret = get_errno(sys_getdents(arg1, dirp, count));
4171 if (!is_error(ret)) {
4177 reclen = de->d_reclen;
4180 de->d_reclen = tswap16(reclen);
4181 tswapls(&de->d_ino);
4182 tswapls(&de->d_off);
4183 de = (struct dirent *)((char *)de + reclen);
4187 unlock_user(dirp, arg2, ret);
4191 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
4192 case TARGET_NR_getdents64:
4194 struct dirent64 *dirp;
4195 abi_long count = arg3;
4196 dirp = lock_user(arg2, count, 0);
4197 ret = get_errno(sys_getdents64(arg1, dirp, count));
4198 if (!is_error(ret)) {
4199 struct dirent64 *de;
4204 reclen = de->d_reclen;
4207 de->d_reclen = tswap16(reclen);
4208 tswap64s(&de->d_ino);
4209 tswap64s(&de->d_off);
4210 de = (struct dirent64 *)((char *)de + reclen);
4214 unlock_user(dirp, arg2, ret);
4217 #endif /* TARGET_NR_getdents64 */
4218 #ifdef TARGET_NR__newselect
4219 case TARGET_NR__newselect:
4220 ret = do_select(arg1, arg2, arg3, arg4, arg5);
4223 #ifdef TARGET_NR_poll
4224 case TARGET_NR_poll:
4226 struct target_pollfd *target_pfd;
4227 unsigned int nfds = arg2;
4232 target_pfd = lock_user(arg1, sizeof(struct target_pollfd) * nfds, 1);
4233 pfd = alloca(sizeof(struct pollfd) * nfds);
4234 for(i = 0; i < nfds; i++) {
4235 pfd[i].fd = tswap32(target_pfd[i].fd);
4236 pfd[i].events = tswap16(target_pfd[i].events);
4238 ret = get_errno(poll(pfd, nfds, timeout));
4239 if (!is_error(ret)) {
4240 for(i = 0; i < nfds; i++) {
4241 target_pfd[i].revents = tswap16(pfd[i].revents);
4243 ret += nfds * (sizeof(struct target_pollfd)
4244 - sizeof(struct pollfd));
4246 unlock_user(target_pfd, arg1, ret);
4250 case TARGET_NR_flock:
4251 /* NOTE: the flock constant seems to be the same for every
4253 ret = get_errno(flock(arg1, arg2));
4255 case TARGET_NR_readv:
4260 vec = alloca(count * sizeof(struct iovec));
4261 lock_iovec(vec, arg2, count, 0);
4262 ret = get_errno(readv(arg1, vec, count));
4263 unlock_iovec(vec, arg2, count, 1);
4266 case TARGET_NR_writev:
4271 vec = alloca(count * sizeof(struct iovec));
4272 lock_iovec(vec, arg2, count, 1);
4273 ret = get_errno(writev(arg1, vec, count));
4274 unlock_iovec(vec, arg2, count, 0);
4277 case TARGET_NR_getsid:
4278 ret = get_errno(getsid(arg1));
4280 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
4281 case TARGET_NR_fdatasync:
4282 ret = get_errno(fdatasync(arg1));
4285 case TARGET_NR__sysctl:
4286 /* We don't implement this, but ENOTDIR is always a safe
4288 ret = -TARGET_ENOTDIR;
4290 case TARGET_NR_sched_setparam:
4292 struct sched_param *target_schp;
4293 struct sched_param schp;
4295 lock_user_struct(target_schp, arg2, 1);
4296 schp.sched_priority = tswap32(target_schp->sched_priority);
4297 unlock_user_struct(target_schp, arg2, 0);
4298 ret = get_errno(sched_setparam(arg1, &schp));
4301 case TARGET_NR_sched_getparam:
4303 struct sched_param *target_schp;
4304 struct sched_param schp;
4305 ret = get_errno(sched_getparam(arg1, &schp));
4306 if (!is_error(ret)) {
4307 lock_user_struct(target_schp, arg2, 0);
4308 target_schp->sched_priority = tswap32(schp.sched_priority);
4309 unlock_user_struct(target_schp, arg2, 1);
4313 case TARGET_NR_sched_setscheduler:
4315 struct sched_param *target_schp;
4316 struct sched_param schp;
4317 lock_user_struct(target_schp, arg3, 1);
4318 schp.sched_priority = tswap32(target_schp->sched_priority);
4319 unlock_user_struct(target_schp, arg3, 0);
4320 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4323 case TARGET_NR_sched_getscheduler:
4324 ret = get_errno(sched_getscheduler(arg1));
4326 case TARGET_NR_sched_yield:
4327 ret = get_errno(sched_yield());
4329 case TARGET_NR_sched_get_priority_max:
4330 ret = get_errno(sched_get_priority_max(arg1));
4332 case TARGET_NR_sched_get_priority_min:
4333 ret = get_errno(sched_get_priority_min(arg1));
4335 case TARGET_NR_sched_rr_get_interval:
4338 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4339 if (!is_error(ret)) {
4340 host_to_target_timespec(arg2, &ts);
4344 case TARGET_NR_nanosleep:
4346 struct timespec req, rem;
4347 target_to_host_timespec(&req, arg1);
4348 ret = get_errno(nanosleep(&req, &rem));
4349 if (is_error(ret) && arg2) {
4350 host_to_target_timespec(arg2, &rem);
4354 #ifdef TARGET_NR_query_module
4355 case TARGET_NR_query_module:
4358 #ifdef TARGET_NR_nfsservctl
4359 case TARGET_NR_nfsservctl:
4362 case TARGET_NR_prctl:
4365 case PR_GET_PDEATHSIG:
4368 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4369 if (!is_error(ret) && arg2)
4370 tput32(arg2, deathsig);
4374 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4378 #ifdef TARGET_NR_pread
4379 case TARGET_NR_pread:
4380 page_unprotect_range(arg2, arg3);
4381 p = lock_user(arg2, arg3, 0);
4382 ret = get_errno(pread(arg1, p, arg3, arg4));
4383 unlock_user(p, arg2, ret);
4385 case TARGET_NR_pwrite:
4386 p = lock_user(arg2, arg3, 1);
4387 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4388 unlock_user(p, arg2, 0);
4391 case TARGET_NR_getcwd:
4392 p = lock_user(arg1, arg2, 0);
4393 ret = get_errno(sys_getcwd1(p, arg2));
4394 unlock_user(p, arg1, ret);
4396 case TARGET_NR_capget:
4398 case TARGET_NR_capset:
4400 case TARGET_NR_sigaltstack:
4401 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4402 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
4403 ret = do_sigaltstack((struct target_sigaltstack *)arg1,
4404 (struct target_sigaltstack *)arg2,
4405 get_sp_from_cpustate((CPUState *)cpu_env));
4410 case TARGET_NR_sendfile:
4412 #ifdef TARGET_NR_getpmsg
4413 case TARGET_NR_getpmsg:
4416 #ifdef TARGET_NR_putpmsg
4417 case TARGET_NR_putpmsg:
4420 #ifdef TARGET_NR_vfork
4421 case TARGET_NR_vfork:
4422 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
4425 #ifdef TARGET_NR_ugetrlimit
4426 case TARGET_NR_ugetrlimit:
4429 ret = get_errno(getrlimit(arg1, &rlim));
4430 if (!is_error(ret)) {
4431 struct target_rlimit *target_rlim;
4432 lock_user_struct(target_rlim, arg2, 0);
4433 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4434 target_rlim->rlim_max = tswapl(rlim.rlim_max);
4435 unlock_user_struct(target_rlim, arg2, 1);
4440 #ifdef TARGET_NR_truncate64
4441 case TARGET_NR_truncate64:
4442 p = lock_user_string(arg1);
4443 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4444 unlock_user(p, arg1, 0);
4447 #ifdef TARGET_NR_ftruncate64
4448 case TARGET_NR_ftruncate64:
4449 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
4452 #ifdef TARGET_NR_stat64
4453 case TARGET_NR_stat64:
4454 p = lock_user_string(arg1);
4455 ret = get_errno(stat(path(p), &st));
4456 unlock_user(p, arg1, 0);
4459 #ifdef TARGET_NR_lstat64
4460 case TARGET_NR_lstat64:
4461 p = lock_user_string(arg1);
4462 ret = get_errno(lstat(path(p), &st));
4463 unlock_user(p, arg1, 0);
4466 #ifdef TARGET_NR_fstat64
4467 case TARGET_NR_fstat64:
4469 ret = get_errno(fstat(arg1, &st));
4471 if (!is_error(ret)) {
4473 if (((CPUARMState *)cpu_env)->eabi) {
4474 struct target_eabi_stat64 *target_st;
4475 lock_user_struct(target_st, arg2, 1);
4476 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4477 /* put_user is probably wrong. */
4478 put_user(st.st_dev, &target_st->st_dev);
4479 put_user(st.st_ino, &target_st->st_ino);
4480 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4481 put_user(st.st_ino, &target_st->__st_ino);
4483 put_user(st.st_mode, &target_st->st_mode);
4484 put_user(st.st_nlink, &target_st->st_nlink);
4485 put_user(st.st_uid, &target_st->st_uid);
4486 put_user(st.st_gid, &target_st->st_gid);
4487 put_user(st.st_rdev, &target_st->st_rdev);
4488 /* XXX: better use of kernel struct */
4489 put_user(st.st_size, &target_st->st_size);
4490 put_user(st.st_blksize, &target_st->st_blksize);
4491 put_user(st.st_blocks, &target_st->st_blocks);
4492 put_user(st.st_atime, &target_st->target_st_atime);
4493 put_user(st.st_mtime, &target_st->target_st_mtime);
4494 put_user(st.st_ctime, &target_st->target_st_ctime);
4495 unlock_user_struct(target_st, arg2, 0);
4499 struct target_stat64 *target_st;
4500 lock_user_struct(target_st, arg2, 1);
4501 memset(target_st, 0, sizeof(struct target_stat64));
4502 /* ??? put_user is probably wrong. */
4503 put_user(st.st_dev, &target_st->st_dev);
4504 put_user(st.st_ino, &target_st->st_ino);
4505 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4506 put_user(st.st_ino, &target_st->__st_ino);
4508 put_user(st.st_mode, &target_st->st_mode);
4509 put_user(st.st_nlink, &target_st->st_nlink);
4510 put_user(st.st_uid, &target_st->st_uid);
4511 put_user(st.st_gid, &target_st->st_gid);
4512 put_user(st.st_rdev, &target_st->st_rdev);
4513 /* XXX: better use of kernel struct */
4514 put_user(st.st_size, &target_st->st_size);
4515 put_user(st.st_blksize, &target_st->st_blksize);
4516 put_user(st.st_blocks, &target_st->st_blocks);
4517 put_user(st.st_atime, &target_st->target_st_atime);
4518 put_user(st.st_mtime, &target_st->target_st_mtime);
4519 put_user(st.st_ctime, &target_st->target_st_ctime);
4520 unlock_user_struct(target_st, arg2, 0);
4527 case TARGET_NR_lchown:
4528 p = lock_user_string(arg1);
4529 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4530 unlock_user(p, arg1, 0);
4532 case TARGET_NR_getuid:
4533 ret = get_errno(high2lowuid(getuid()));
4535 case TARGET_NR_getgid:
4536 ret = get_errno(high2lowgid(getgid()));
4538 case TARGET_NR_geteuid:
4539 ret = get_errno(high2lowuid(geteuid()));
4541 case TARGET_NR_getegid:
4542 ret = get_errno(high2lowgid(getegid()));
4544 case TARGET_NR_setreuid:
4545 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4547 case TARGET_NR_setregid:
4548 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4550 case TARGET_NR_getgroups:
4552 int gidsetsize = arg1;
4553 uint16_t *target_grouplist;
4557 grouplist = alloca(gidsetsize * sizeof(gid_t));
4558 ret = get_errno(getgroups(gidsetsize, grouplist));
4559 if (!is_error(ret)) {
4560 target_grouplist = lock_user(arg2, gidsetsize * 2, 0);
4561 for(i = 0;i < gidsetsize; i++)
4562 target_grouplist[i] = tswap16(grouplist[i]);
4563 unlock_user(target_grouplist, arg2, gidsetsize * 2);
4567 case TARGET_NR_setgroups:
4569 int gidsetsize = arg1;
4570 uint16_t *target_grouplist;
4574 grouplist = alloca(gidsetsize * sizeof(gid_t));
4575 target_grouplist = lock_user(arg2, gidsetsize * 2, 1);
4576 for(i = 0;i < gidsetsize; i++)
4577 grouplist[i] = tswap16(target_grouplist[i]);
4578 unlock_user(target_grouplist, arg2, 0);
4579 ret = get_errno(setgroups(gidsetsize, grouplist));
4582 case TARGET_NR_fchown:
4583 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4585 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4586 case TARGET_NR_fchownat:
4588 ret = -TARGET_EFAULT;
4591 p = lock_user_string(arg2);
4592 if (!access_ok(VERIFY_READ, p, 1))
4593 /* Don't "goto fail" so that cleanup can happen. */
4594 ret = -TARGET_EFAULT;
4596 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4598 unlock_user(p, arg2, 0);
4601 #ifdef TARGET_NR_setresuid
4602 case TARGET_NR_setresuid:
4603 ret = get_errno(setresuid(low2highuid(arg1),
4605 low2highuid(arg3)));
4608 #ifdef TARGET_NR_getresuid
4609 case TARGET_NR_getresuid:
4611 uid_t ruid, euid, suid;
4612 ret = get_errno(getresuid(&ruid, &euid, &suid));
4613 if (!is_error(ret)) {
4614 tput16(arg1, tswap16(high2lowuid(ruid)));
4615 tput16(arg2, tswap16(high2lowuid(euid)));
4616 tput16(arg3, tswap16(high2lowuid(suid)));
4621 #ifdef TARGET_NR_getresgid
4622 case TARGET_NR_setresgid:
4623 ret = get_errno(setresgid(low2highgid(arg1),
4625 low2highgid(arg3)));
4628 #ifdef TARGET_NR_getresgid
4629 case TARGET_NR_getresgid:
4631 gid_t rgid, egid, sgid;
4632 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4633 if (!is_error(ret)) {
4634 tput16(arg1, tswap16(high2lowgid(rgid)));
4635 tput16(arg2, tswap16(high2lowgid(egid)));
4636 tput16(arg3, tswap16(high2lowgid(sgid)));
4641 case TARGET_NR_chown:
4642 p = lock_user_string(arg1);
4643 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4644 unlock_user(p, arg1, 0);
4646 case TARGET_NR_setuid:
4647 ret = get_errno(setuid(low2highuid(arg1)));
4649 case TARGET_NR_setgid:
4650 ret = get_errno(setgid(low2highgid(arg1)));
4652 case TARGET_NR_setfsuid:
4653 ret = get_errno(setfsuid(arg1));
4655 case TARGET_NR_setfsgid:
4656 ret = get_errno(setfsgid(arg1));
4658 #endif /* USE_UID16 */
4660 #ifdef TARGET_NR_lchown32
4661 case TARGET_NR_lchown32:
4662 p = lock_user_string(arg1);
4663 ret = get_errno(lchown(p, arg2, arg3));
4664 unlock_user(p, arg1, 0);
4667 #ifdef TARGET_NR_getuid32
4668 case TARGET_NR_getuid32:
4669 ret = get_errno(getuid());
4672 #ifdef TARGET_NR_getgid32
4673 case TARGET_NR_getgid32:
4674 ret = get_errno(getgid());
4677 #ifdef TARGET_NR_geteuid32
4678 case TARGET_NR_geteuid32:
4679 ret = get_errno(geteuid());
4682 #ifdef TARGET_NR_getegid32
4683 case TARGET_NR_getegid32:
4684 ret = get_errno(getegid());
4687 #ifdef TARGET_NR_setreuid32
4688 case TARGET_NR_setreuid32:
4689 ret = get_errno(setreuid(arg1, arg2));
4692 #ifdef TARGET_NR_setregid32
4693 case TARGET_NR_setregid32:
4694 ret = get_errno(setregid(arg1, arg2));
4697 #ifdef TARGET_NR_getgroups32
4698 case TARGET_NR_getgroups32:
4700 int gidsetsize = arg1;
4701 uint32_t *target_grouplist;
4705 grouplist = alloca(gidsetsize * sizeof(gid_t));
4706 ret = get_errno(getgroups(gidsetsize, grouplist));
4707 if (!is_error(ret)) {
4708 target_grouplist = lock_user(arg2, gidsetsize * 4, 0);
4709 for(i = 0;i < gidsetsize; i++)
4710 target_grouplist[i] = tswap32(grouplist[i]);
4711 unlock_user(target_grouplist, arg2, gidsetsize * 4);
4716 #ifdef TARGET_NR_setgroups32
4717 case TARGET_NR_setgroups32:
4719 int gidsetsize = arg1;
4720 uint32_t *target_grouplist;
4724 grouplist = alloca(gidsetsize * sizeof(gid_t));
4725 target_grouplist = lock_user(arg2, gidsetsize * 4, 1);
4726 for(i = 0;i < gidsetsize; i++)
4727 grouplist[i] = tswap32(target_grouplist[i]);
4728 unlock_user(target_grouplist, arg2, 0);
4729 ret = get_errno(setgroups(gidsetsize, grouplist));
4733 #ifdef TARGET_NR_fchown32
4734 case TARGET_NR_fchown32:
4735 ret = get_errno(fchown(arg1, arg2, arg3));
4738 #ifdef TARGET_NR_setresuid32
4739 case TARGET_NR_setresuid32:
4740 ret = get_errno(setresuid(arg1, arg2, arg3));
4743 #ifdef TARGET_NR_getresuid32
4744 case TARGET_NR_getresuid32:
4746 uid_t ruid, euid, suid;
4747 ret = get_errno(getresuid(&ruid, &euid, &suid));
4748 if (!is_error(ret)) {
4749 tput32(arg1, tswap32(ruid));
4750 tput32(arg2, tswap32(euid));
4751 tput32(arg3, tswap32(suid));
4756 #ifdef TARGET_NR_setresgid32
4757 case TARGET_NR_setresgid32:
4758 ret = get_errno(setresgid(arg1, arg2, arg3));
4761 #ifdef TARGET_NR_getresgid32
4762 case TARGET_NR_getresgid32:
4764 gid_t rgid, egid, sgid;
4765 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4766 if (!is_error(ret)) {
4767 tput32(arg1, tswap32(rgid));
4768 tput32(arg2, tswap32(egid));
4769 tput32(arg3, tswap32(sgid));
4774 #ifdef TARGET_NR_chown32
4775 case TARGET_NR_chown32:
4776 p = lock_user_string(arg1);
4777 ret = get_errno(chown(p, arg2, arg3));
4778 unlock_user(p, arg1, 0);
4781 #ifdef TARGET_NR_setuid32
4782 case TARGET_NR_setuid32:
4783 ret = get_errno(setuid(arg1));
4786 #ifdef TARGET_NR_setgid32
4787 case TARGET_NR_setgid32:
4788 ret = get_errno(setgid(arg1));
4791 #ifdef TARGET_NR_setfsuid32
4792 case TARGET_NR_setfsuid32:
4793 ret = get_errno(setfsuid(arg1));
4796 #ifdef TARGET_NR_setfsgid32
4797 case TARGET_NR_setfsgid32:
4798 ret = get_errno(setfsgid(arg1));
4802 case TARGET_NR_pivot_root:
4804 #ifdef TARGET_NR_mincore
4805 case TARGET_NR_mincore:
4808 #ifdef TARGET_NR_madvise
4809 case TARGET_NR_madvise:
4810 /* A straight passthrough may not be safe because qemu sometimes
4811 turns private flie-backed mappings into anonymous mappings.
4812 This will break MADV_DONTNEED.
4813 This is a hint, so ignoring and returning success is ok. */
4817 #if TARGET_ABI_BITS == 32
4818 case TARGET_NR_fcntl64:
4822 struct target_flock64 *target_fl;
4824 struct target_eabi_flock64 *target_efl;
4828 case TARGET_F_GETLK64:
4831 case TARGET_F_SETLK64:
4834 case TARGET_F_SETLKW64:
4843 case TARGET_F_GETLK64:
4845 if (((CPUARMState *)cpu_env)->eabi) {
4846 lock_user_struct(target_efl, arg3, 1);
4847 fl.l_type = tswap16(target_efl->l_type);
4848 fl.l_whence = tswap16(target_efl->l_whence);
4849 fl.l_start = tswap64(target_efl->l_start);
4850 fl.l_len = tswap64(target_efl->l_len);
4851 fl.l_pid = tswapl(target_efl->l_pid);
4852 unlock_user_struct(target_efl, arg3, 0);
4856 lock_user_struct(target_fl, arg3, 1);
4857 fl.l_type = tswap16(target_fl->l_type);
4858 fl.l_whence = tswap16(target_fl->l_whence);
4859 fl.l_start = tswap64(target_fl->l_start);
4860 fl.l_len = tswap64(target_fl->l_len);
4861 fl.l_pid = tswapl(target_fl->l_pid);
4862 unlock_user_struct(target_fl, arg3, 0);
4864 ret = get_errno(fcntl(arg1, cmd, &fl));
4867 if (((CPUARMState *)cpu_env)->eabi) {
4868 lock_user_struct(target_efl, arg3, 0);
4869 target_efl->l_type = tswap16(fl.l_type);
4870 target_efl->l_whence = tswap16(fl.l_whence);
4871 target_efl->l_start = tswap64(fl.l_start);
4872 target_efl->l_len = tswap64(fl.l_len);
4873 target_efl->l_pid = tswapl(fl.l_pid);
4874 unlock_user_struct(target_efl, arg3, 1);
4878 lock_user_struct(target_fl, arg3, 0);
4879 target_fl->l_type = tswap16(fl.l_type);
4880 target_fl->l_whence = tswap16(fl.l_whence);
4881 target_fl->l_start = tswap64(fl.l_start);
4882 target_fl->l_len = tswap64(fl.l_len);
4883 target_fl->l_pid = tswapl(fl.l_pid);
4884 unlock_user_struct(target_fl, arg3, 1);
4889 case TARGET_F_SETLK64:
4890 case TARGET_F_SETLKW64:
4892 if (((CPUARMState *)cpu_env)->eabi) {
4893 lock_user_struct(target_efl, arg3, 1);
4894 fl.l_type = tswap16(target_efl->l_type);
4895 fl.l_whence = tswap16(target_efl->l_whence);
4896 fl.l_start = tswap64(target_efl->l_start);
4897 fl.l_len = tswap64(target_efl->l_len);
4898 fl.l_pid = tswapl(target_efl->l_pid);
4899 unlock_user_struct(target_efl, arg3, 0);
4903 lock_user_struct(target_fl, arg3, 1);
4904 fl.l_type = tswap16(target_fl->l_type);
4905 fl.l_whence = tswap16(target_fl->l_whence);
4906 fl.l_start = tswap64(target_fl->l_start);
4907 fl.l_len = tswap64(target_fl->l_len);
4908 fl.l_pid = tswapl(target_fl->l_pid);
4909 unlock_user_struct(target_fl, arg3, 0);
4911 ret = get_errno(fcntl(arg1, cmd, &fl));
4914 ret = get_errno(do_fcntl(arg1, cmd, arg3));
4920 #ifdef TARGET_NR_cacheflush
4921 case TARGET_NR_cacheflush:
4922 /* self-modifying code is handled automatically, so nothing needed */
4926 #ifdef TARGET_NR_security
4927 case TARGET_NR_security:
4930 #ifdef TARGET_NR_getpagesize
4931 case TARGET_NR_getpagesize:
4932 ret = TARGET_PAGE_SIZE;
4935 case TARGET_NR_gettid:
4936 ret = get_errno(gettid());
4938 #ifdef TARGET_NR_readahead
4939 case TARGET_NR_readahead:
4942 #ifdef TARGET_NR_setxattr
4943 case TARGET_NR_setxattr:
4944 case TARGET_NR_lsetxattr:
4945 case TARGET_NR_fsetxattr:
4946 case TARGET_NR_getxattr:
4947 case TARGET_NR_lgetxattr:
4948 case TARGET_NR_fgetxattr:
4949 case TARGET_NR_listxattr:
4950 case TARGET_NR_llistxattr:
4951 case TARGET_NR_flistxattr:
4952 case TARGET_NR_removexattr:
4953 case TARGET_NR_lremovexattr:
4954 case TARGET_NR_fremovexattr:
4955 goto unimplemented_nowarn;
4957 #ifdef TARGET_NR_set_thread_area
4958 case TARGET_NR_set_thread_area:
4960 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
4964 goto unimplemented_nowarn;
4967 #ifdef TARGET_NR_get_thread_area
4968 case TARGET_NR_get_thread_area:
4969 goto unimplemented_nowarn;
4971 #ifdef TARGET_NR_getdomainname
4972 case TARGET_NR_getdomainname:
4973 goto unimplemented_nowarn;
4976 #ifdef TARGET_NR_clock_gettime
4977 case TARGET_NR_clock_gettime:
4980 ret = get_errno(clock_gettime(arg1, &ts));
4981 if (!is_error(ret)) {
4982 host_to_target_timespec(arg2, &ts);
4987 #ifdef TARGET_NR_clock_getres
4988 case TARGET_NR_clock_getres:
4991 ret = get_errno(clock_getres(arg1, &ts));
4992 if (!is_error(ret)) {
4993 host_to_target_timespec(arg2, &ts);
4999 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5000 case TARGET_NR_set_tid_address:
5001 ret = get_errno(set_tid_address((int *) arg1));
5005 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
5006 case TARGET_NR_tkill:
5007 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5011 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
5012 case TARGET_NR_tgkill:
5013 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5017 #ifdef TARGET_NR_set_robust_list
5018 case TARGET_NR_set_robust_list:
5019 goto unimplemented_nowarn;
5022 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5023 case TARGET_NR_utimensat:
5025 struct timespec ts[2];
5026 target_to_host_timespec(ts, arg3);
5027 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5029 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5031 p = lock_user_string(arg2);
5032 if (!access_ok(VERIFY_READ, p, 1))
5033 /* Don't "goto fail" so that cleanup can happen. */
5034 ret = -TARGET_EFAULT;
5036 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5038 unlock_user(p, arg2, 0);
5046 gemu_log("qemu: Unsupported syscall: %d\n", num);
5047 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5048 unimplemented_nowarn:
5050 ret = -TARGET_ENOSYS;
5055 gemu_log(" = %ld\n", ret);
5058 print_syscall_ret(num, ret);