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 abi_long host_to_target_rusage(abi_ulong target_addr,
508 const struct rusage *rusage)
510 struct target_rusage *target_rusage;
512 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
513 return -TARGET_EFAULT;
514 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
515 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
516 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
517 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
518 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
519 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
520 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
521 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
522 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
523 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
524 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
525 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
526 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
527 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
528 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
529 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
530 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
531 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
532 unlock_user_struct(target_rusage, target_addr, 1);
537 static inline abi_long target_to_host_timeval(struct timeval *tv,
538 abi_ulong target_addr)
540 struct target_timeval *target_tv;
542 if (!lock_user_struct(VERIFY_READ, target_tv, target_addr, 1))
543 return -TARGET_EFAULT;
544 tv->tv_sec = tswapl(target_tv->tv_sec);
545 tv->tv_usec = tswapl(target_tv->tv_usec);
546 unlock_user_struct(target_tv, target_addr, 0);
551 static inline abi_long host_to_target_timeval(abi_ulong target_addr,
552 const struct timeval *tv)
554 struct target_timeval *target_tv;
556 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_addr, 0))
557 return -TARGET_EFAULT;
558 target_tv->tv_sec = tswapl(tv->tv_sec);
559 target_tv->tv_usec = tswapl(tv->tv_usec);
560 unlock_user_struct(target_tv, target_addr, 1);
566 /* do_select() must return target values and target errnos. */
567 static abi_long do_select(int n,
568 abi_ulong rfd_p, abi_ulong wfd_p,
569 abi_ulong efd_p, abi_ulong target_tv)
571 fd_set rfds, wfds, efds;
572 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
573 abi_long *target_rfds, *target_wfds, *target_efds;
574 struct timeval tv, *tv_ptr;
579 target_rfds = lock_user(VERIFY_WRITE, rfd_p, sizeof(abi_long) * n, 1);
581 ret = -TARGET_EFAULT;
584 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
590 target_wfds = lock_user(VERIFY_WRITE, wfd_p, sizeof(abi_long) * n, 1);
592 ret = -TARGET_EFAULT;
595 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
601 target_efds = lock_user(VERIFY_WRITE, efd_p, sizeof(abi_long) * n, 1);
603 ret = -TARGET_EFAULT;
606 efds_ptr = target_to_host_fds(&efds, target_efds, n);
613 target_to_host_timeval(&tv, target_tv);
618 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
622 host_to_target_fds(target_rfds, rfds_ptr, n);
623 host_to_target_fds(target_wfds, wfds_ptr, n);
624 host_to_target_fds(target_efds, efds_ptr, n);
627 host_to_target_timeval(target_tv, &tv);
632 unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0);
633 unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0);
634 unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0);
639 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
640 abi_ulong target_addr,
643 struct target_sockaddr *target_saddr;
645 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
647 return -TARGET_EFAULT;
648 memcpy(addr, target_saddr, len);
649 addr->sa_family = tswap16(target_saddr->sa_family);
650 unlock_user(target_saddr, target_addr, 0);
655 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
656 struct sockaddr *addr,
659 struct target_sockaddr *target_saddr;
661 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
663 return -TARGET_EFAULT;
664 memcpy(target_saddr, addr, len);
665 target_saddr->sa_family = tswap16(addr->sa_family);
666 unlock_user(target_saddr, target_addr, len);
671 /* ??? Should this also swap msgh->name? */
672 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
673 struct target_msghdr *target_msgh)
675 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
676 abi_long msg_controllen;
677 abi_ulong target_cmsg_addr;
678 struct target_cmsghdr *target_cmsg;
681 msg_controllen = tswapl(target_msgh->msg_controllen);
682 if (msg_controllen < sizeof (struct target_cmsghdr))
684 target_cmsg_addr = tswapl(target_msgh->msg_control);
685 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
687 return -TARGET_EFAULT;
689 while (cmsg && target_cmsg) {
690 void *data = CMSG_DATA(cmsg);
691 void *target_data = TARGET_CMSG_DATA(target_cmsg);
693 int len = tswapl(target_cmsg->cmsg_len)
694 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
696 space += CMSG_SPACE(len);
697 if (space > msgh->msg_controllen) {
698 space -= CMSG_SPACE(len);
699 gemu_log("Host cmsg overflow\n");
703 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
704 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
705 cmsg->cmsg_len = CMSG_LEN(len);
707 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
708 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
709 memcpy(data, target_data, len);
711 int *fd = (int *)data;
712 int *target_fd = (int *)target_data;
713 int i, numfds = len / sizeof(int);
715 for (i = 0; i < numfds; i++)
716 fd[i] = tswap32(target_fd[i]);
719 cmsg = CMSG_NXTHDR(msgh, cmsg);
720 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
722 unlock_user(target_cmsg, target_cmsg_addr, 0);
724 msgh->msg_controllen = space;
728 /* ??? Should this also swap msgh->name? */
729 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
732 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
733 abi_long msg_controllen;
734 abi_ulong target_cmsg_addr;
735 struct target_cmsghdr *target_cmsg;
738 msg_controllen = tswapl(target_msgh->msg_controllen);
739 if (msg_controllen < sizeof (struct target_cmsghdr))
741 target_cmsg_addr = tswapl(target_msgh->msg_control);
742 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
744 return -TARGET_EFAULT;
746 while (cmsg && target_cmsg) {
747 void *data = CMSG_DATA(cmsg);
748 void *target_data = TARGET_CMSG_DATA(target_cmsg);
750 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
752 space += TARGET_CMSG_SPACE(len);
753 if (space > msg_controllen) {
754 space -= TARGET_CMSG_SPACE(len);
755 gemu_log("Target cmsg overflow\n");
759 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
760 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
761 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
763 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
764 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
765 memcpy(target_data, data, len);
767 int *fd = (int *)data;
768 int *target_fd = (int *)target_data;
769 int i, numfds = len / sizeof(int);
771 for (i = 0; i < numfds; i++)
772 target_fd[i] = tswap32(fd[i]);
775 cmsg = CMSG_NXTHDR(msgh, cmsg);
776 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
778 unlock_user(target_cmsg, target_cmsg_addr, space);
780 target_msgh->msg_controllen = tswapl(space);
784 /* do_setsockopt() Must return target values and target errnos. */
785 static abi_long do_setsockopt(int sockfd, int level, int optname,
786 abi_ulong optval_addr, socklen_t optlen)
793 /* TCP options all take an 'int' value. */
794 if (optlen < sizeof(uint32_t))
795 return -TARGET_EINVAL;
797 if (get_user_u32(val, optval_addr))
798 return -TARGET_EFAULT;
799 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
806 case IP_ROUTER_ALERT:
810 case IP_MTU_DISCOVER:
816 case IP_MULTICAST_TTL:
817 case IP_MULTICAST_LOOP:
819 if (optlen >= sizeof(uint32_t)) {
820 if (get_user_u32(val, optval_addr))
821 return -TARGET_EFAULT;
822 } else if (optlen >= 1) {
823 if (get_user_u8(val, optval_addr))
824 return -TARGET_EFAULT;
826 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
832 case TARGET_SOL_SOCKET:
834 /* Options with 'int' argument. */
835 case TARGET_SO_DEBUG:
838 case TARGET_SO_REUSEADDR:
839 optname = SO_REUSEADDR;
844 case TARGET_SO_ERROR:
847 case TARGET_SO_DONTROUTE:
848 optname = SO_DONTROUTE;
850 case TARGET_SO_BROADCAST:
851 optname = SO_BROADCAST;
853 case TARGET_SO_SNDBUF:
856 case TARGET_SO_RCVBUF:
859 case TARGET_SO_KEEPALIVE:
860 optname = SO_KEEPALIVE;
862 case TARGET_SO_OOBINLINE:
863 optname = SO_OOBINLINE;
865 case TARGET_SO_NO_CHECK:
866 optname = SO_NO_CHECK;
868 case TARGET_SO_PRIORITY:
869 optname = SO_PRIORITY;
872 case TARGET_SO_BSDCOMPAT:
873 optname = SO_BSDCOMPAT;
876 case TARGET_SO_PASSCRED:
877 optname = SO_PASSCRED;
879 case TARGET_SO_TIMESTAMP:
880 optname = SO_TIMESTAMP;
882 case TARGET_SO_RCVLOWAT:
883 optname = SO_RCVLOWAT;
885 case TARGET_SO_RCVTIMEO:
886 optname = SO_RCVTIMEO;
888 case TARGET_SO_SNDTIMEO:
889 optname = SO_SNDTIMEO;
895 if (optlen < sizeof(uint32_t))
896 return -TARGET_EINVAL;
898 if (get_user_u32(val, optval_addr))
899 return -TARGET_EFAULT;
900 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
904 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
905 ret = -TARGET_ENOSYS;
910 /* do_getsockopt() Must return target values and target errnos. */
911 static abi_long do_getsockopt(int sockfd, int level, int optname,
912 abi_ulong optval_addr, abi_ulong optlen)
918 case TARGET_SOL_SOCKET:
921 case TARGET_SO_LINGER:
922 case TARGET_SO_RCVTIMEO:
923 case TARGET_SO_SNDTIMEO:
924 case TARGET_SO_PEERCRED:
925 case TARGET_SO_PEERNAME:
926 /* These don't just return a single integer */
933 /* TCP options all take an 'int' value. */
935 if (get_user_u32(len, optlen))
936 return -TARGET_EFAULT;
938 return -TARGET_EINVAL;
940 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
947 if (put_user_u32(val, optval_addr))
948 return -TARGET_EFAULT;
950 if (put_user_u8(val, optval_addr))
951 return -TARGET_EFAULT;
953 if (put_user_u32(len, optlen))
954 return -TARGET_EFAULT;
961 case IP_ROUTER_ALERT:
965 case IP_MTU_DISCOVER:
971 case IP_MULTICAST_TTL:
972 case IP_MULTICAST_LOOP:
973 if (get_user_u32(len, optlen))
974 return -TARGET_EFAULT;
976 return -TARGET_EINVAL;
978 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
981 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
983 if (put_user_u32(len, optlen)
984 || put_user_u8(val, optval_addr))
985 return -TARGET_EFAULT;
987 if (len > sizeof(int))
989 if (put_user_u32(len, optlen)
990 || put_user_u32(val, optval_addr))
991 return -TARGET_EFAULT;
1000 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1002 ret = -TARGET_ENOSYS;
1009 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1010 * other lock functions have a return code of 0 for failure.
1012 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1013 int count, int copy)
1015 struct target_iovec *target_vec;
1019 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1021 return -TARGET_EFAULT;
1022 for(i = 0;i < count; i++) {
1023 base = tswapl(target_vec[i].iov_base);
1024 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1025 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1026 if (!vec[i].iov_base)
1029 unlock_user (target_vec, target_addr, 0);
1032 /* failure - unwind locks */
1033 for (j = 0; j < i; j++) {
1034 base = tswapl(target_vec[j].iov_base);
1035 unlock_user(vec[j].iov_base, base, 0);
1037 unlock_user (target_vec, target_addr, 0);
1038 return -TARGET_EFAULT;
1041 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1042 int count, int copy)
1044 struct target_iovec *target_vec;
1048 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1050 return -TARGET_EFAULT;
1051 for(i = 0;i < count; i++) {
1052 base = tswapl(target_vec[i].iov_base);
1053 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1055 unlock_user (target_vec, target_addr, 0);
1060 /* do_socket() Must return target values and target errnos. */
1061 static abi_long do_socket(int domain, int type, int protocol)
1063 #if defined(TARGET_MIPS)
1065 case TARGET_SOCK_DGRAM:
1068 case TARGET_SOCK_STREAM:
1071 case TARGET_SOCK_RAW:
1074 case TARGET_SOCK_RDM:
1077 case TARGET_SOCK_SEQPACKET:
1078 type = SOCK_SEQPACKET;
1080 case TARGET_SOCK_PACKET:
1085 if (domain == PF_NETLINK)
1086 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1087 return get_errno(socket(domain, type, protocol));
1090 /* do_bind() Must return target values and target errnos. */
1091 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1094 void *addr = alloca(addrlen);
1096 target_to_host_sockaddr(addr, target_addr, addrlen);
1097 return get_errno(bind(sockfd, addr, addrlen));
1100 /* do_connect() Must return target values and target errnos. */
1101 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1104 void *addr = alloca(addrlen);
1106 target_to_host_sockaddr(addr, target_addr, addrlen);
1107 return get_errno(connect(sockfd, addr, addrlen));
1110 /* do_sendrecvmsg() Must return target values and target errnos. */
1111 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1112 int flags, int send)
1115 struct target_msghdr *msgp;
1119 abi_ulong target_vec;
1122 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1126 return -TARGET_EFAULT;
1127 if (msgp->msg_name) {
1128 msg.msg_namelen = tswap32(msgp->msg_namelen);
1129 msg.msg_name = alloca(msg.msg_namelen);
1130 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1133 msg.msg_name = NULL;
1134 msg.msg_namelen = 0;
1136 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1137 msg.msg_control = alloca(msg.msg_controllen);
1138 msg.msg_flags = tswap32(msgp->msg_flags);
1140 count = tswapl(msgp->msg_iovlen);
1141 vec = alloca(count * sizeof(struct iovec));
1142 target_vec = tswapl(msgp->msg_iov);
1143 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1144 msg.msg_iovlen = count;
1148 ret = target_to_host_cmsg(&msg, msgp);
1150 ret = get_errno(sendmsg(fd, &msg, flags));
1152 ret = get_errno(recvmsg(fd, &msg, flags));
1154 ret = host_to_target_cmsg(msgp, &msg);
1156 unlock_iovec(vec, target_vec, count, !send);
1157 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1161 /* do_accept() Must return target values and target errnos. */
1162 static abi_long do_accept(int fd, abi_ulong target_addr,
1163 abi_ulong target_addrlen_addr)
1169 if (get_user_u32(addrlen, target_addrlen_addr))
1170 return -TARGET_EFAULT;
1172 addr = alloca(addrlen);
1174 ret = get_errno(accept(fd, addr, &addrlen));
1175 if (!is_error(ret)) {
1176 host_to_target_sockaddr(target_addr, addr, addrlen);
1177 if (put_user_u32(addrlen, target_addrlen_addr))
1178 ret = -TARGET_EFAULT;
1183 /* do_getpeername() Must return target values and target errnos. */
1184 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1185 abi_ulong target_addrlen_addr)
1191 if (get_user_u32(addrlen, target_addrlen_addr))
1192 return -TARGET_EFAULT;
1194 addr = alloca(addrlen);
1196 ret = get_errno(getpeername(fd, addr, &addrlen));
1197 if (!is_error(ret)) {
1198 host_to_target_sockaddr(target_addr, addr, addrlen);
1199 if (put_user_u32(addrlen, target_addrlen_addr))
1200 ret = -TARGET_EFAULT;
1205 /* do_getsockname() Must return target values and target errnos. */
1206 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1207 abi_ulong target_addrlen_addr)
1213 if (get_user_u32(addrlen, target_addrlen_addr))
1214 return -TARGET_EFAULT;
1216 addr = alloca(addrlen);
1218 ret = get_errno(getsockname(fd, addr, &addrlen));
1219 if (!is_error(ret)) {
1220 host_to_target_sockaddr(target_addr, addr, addrlen);
1221 if (put_user_u32(addrlen, target_addrlen_addr))
1222 ret = -TARGET_EFAULT;
1227 /* do_socketpair() Must return target values and target errnos. */
1228 static abi_long do_socketpair(int domain, int type, int protocol,
1229 abi_ulong target_tab_addr)
1234 ret = get_errno(socketpair(domain, type, protocol, tab));
1235 if (!is_error(ret)) {
1236 if (put_user_s32(tab[0], target_tab_addr)
1237 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1238 ret = -TARGET_EFAULT;
1243 /* do_sendto() Must return target values and target errnos. */
1244 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1245 abi_ulong target_addr, socklen_t addrlen)
1251 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1253 return -TARGET_EFAULT;
1255 addr = alloca(addrlen);
1256 target_to_host_sockaddr(addr, target_addr, addrlen);
1257 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1259 ret = get_errno(send(fd, host_msg, len, flags));
1261 unlock_user(host_msg, msg, 0);
1265 /* do_recvfrom() Must return target values and target errnos. */
1266 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1267 abi_ulong target_addr,
1268 abi_ulong target_addrlen)
1275 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1277 return -TARGET_EFAULT;
1279 if (get_user_u32(addrlen, target_addrlen)) {
1280 ret = -TARGET_EFAULT;
1283 addr = alloca(addrlen);
1284 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1286 addr = NULL; /* To keep compiler quiet. */
1287 ret = get_errno(recv(fd, host_msg, len, flags));
1289 if (!is_error(ret)) {
1291 host_to_target_sockaddr(target_addr, addr, addrlen);
1292 if (put_user_u32(addrlen, target_addrlen)) {
1293 ret = -TARGET_EFAULT;
1297 unlock_user(host_msg, msg, len);
1300 unlock_user(host_msg, msg, 0);
1305 #ifdef TARGET_NR_socketcall
1306 /* do_socketcall() Must return target values and target errnos. */
1307 static abi_long do_socketcall(int num, abi_ulong vptr)
1310 const int n = sizeof(abi_ulong);
1315 int domain, type, protocol;
1317 if (get_user_s32(domain, vptr)
1318 || get_user_s32(type, vptr + n)
1319 || get_user_s32(protocol, vptr + 2 * n))
1320 return -TARGET_EFAULT;
1322 ret = do_socket(domain, type, protocol);
1328 abi_ulong target_addr;
1331 if (get_user_s32(sockfd, vptr)
1332 || get_user_ual(target_addr, vptr + n)
1333 || get_user_u32(addrlen, vptr + 2 * n))
1334 return -TARGET_EFAULT;
1336 ret = do_bind(sockfd, target_addr, addrlen);
1339 case SOCKOP_connect:
1342 abi_ulong target_addr;
1345 if (get_user_s32(sockfd, vptr)
1346 || get_user_ual(target_addr, vptr + n)
1347 || get_user_u32(addrlen, vptr + 2 * n))
1348 return -TARGET_EFAULT;
1350 ret = do_connect(sockfd, target_addr, addrlen);
1355 int sockfd, backlog;
1357 if (get_user_s32(sockfd, vptr)
1358 || get_user_s32(backlog, vptr + n))
1359 return -TARGET_EFAULT;
1361 ret = get_errno(listen(sockfd, backlog));
1367 abi_ulong target_addr, target_addrlen;
1369 if (get_user_s32(sockfd, vptr)
1370 || get_user_ual(target_addr, vptr + n)
1371 || get_user_u32(target_addrlen, vptr + 2 * n))
1372 return -TARGET_EFAULT;
1374 ret = do_accept(sockfd, target_addr, target_addrlen);
1377 case SOCKOP_getsockname:
1380 abi_ulong target_addr, target_addrlen;
1382 if (get_user_s32(sockfd, vptr)
1383 || get_user_ual(target_addr, vptr + n)
1384 || get_user_u32(target_addrlen, vptr + 2 * n))
1385 return -TARGET_EFAULT;
1387 ret = do_getsockname(sockfd, target_addr, target_addrlen);
1390 case SOCKOP_getpeername:
1393 abi_ulong target_addr, target_addrlen;
1395 if (get_user_s32(sockfd, vptr)
1396 || get_user_ual(target_addr, vptr + n)
1397 || get_user_u32(target_addrlen, vptr + 2 * n))
1398 return -TARGET_EFAULT;
1400 ret = do_getpeername(sockfd, target_addr, target_addrlen);
1403 case SOCKOP_socketpair:
1405 int domain, type, protocol;
1408 if (get_user_s32(domain, vptr)
1409 || get_user_s32(type, vptr + n)
1410 || get_user_s32(protocol, vptr + 2 * n)
1411 || get_user_ual(tab, vptr + 3 * n))
1412 return -TARGET_EFAULT;
1414 ret = do_socketpair(domain, type, protocol, tab);
1424 if (get_user_s32(sockfd, vptr)
1425 || get_user_ual(msg, vptr + n)
1426 || get_user_ual(len, vptr + 2 * n)
1427 || get_user_s32(flags, vptr + 3 * n))
1428 return -TARGET_EFAULT;
1430 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1440 if (get_user_s32(sockfd, vptr)
1441 || get_user_ual(msg, vptr + n)
1442 || get_user_ual(len, vptr + 2 * n)
1443 || get_user_s32(flags, vptr + 3 * n))
1444 return -TARGET_EFAULT;
1446 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1458 if (get_user_s32(sockfd, vptr)
1459 || get_user_ual(msg, vptr + n)
1460 || get_user_ual(len, vptr + 2 * n)
1461 || get_user_s32(flags, vptr + 3 * n)
1462 || get_user_ual(addr, vptr + 4 * n)
1463 || get_user_u32(addrlen, vptr + 5 * n))
1464 return -TARGET_EFAULT;
1466 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1469 case SOCKOP_recvfrom:
1478 if (get_user_s32(sockfd, vptr)
1479 || get_user_ual(msg, vptr + n)
1480 || get_user_ual(len, vptr + 2 * n)
1481 || get_user_s32(flags, vptr + 3 * n)
1482 || get_user_ual(addr, vptr + 4 * n)
1483 || get_user_u32(addrlen, vptr + 5 * n))
1484 return -TARGET_EFAULT;
1486 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1489 case SOCKOP_shutdown:
1493 if (get_user_s32(sockfd, vptr)
1494 || get_user_s32(how, vptr + n))
1495 return -TARGET_EFAULT;
1497 ret = get_errno(shutdown(sockfd, how));
1500 case SOCKOP_sendmsg:
1501 case SOCKOP_recvmsg:
1504 abi_ulong target_msg;
1507 if (get_user_s32(fd, vptr)
1508 || get_user_ual(target_msg, vptr + n)
1509 || get_user_s32(flags, vptr + 2 * n))
1510 return -TARGET_EFAULT;
1512 ret = do_sendrecvmsg(fd, target_msg, flags,
1513 (num == SOCKOP_sendmsg));
1516 case SOCKOP_setsockopt:
1524 if (get_user_s32(sockfd, vptr)
1525 || get_user_s32(level, vptr + n)
1526 || get_user_s32(optname, vptr + 2 * n)
1527 || get_user_ual(optval, vptr + 3 * n)
1528 || get_user_u32(optlen, vptr + 4 * n))
1529 return -TARGET_EFAULT;
1531 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1534 case SOCKOP_getsockopt:
1542 if (get_user_s32(sockfd, vptr)
1543 || get_user_s32(level, vptr + n)
1544 || get_user_s32(optname, vptr + 2 * n)
1545 || get_user_ual(optval, vptr + 3 * n)
1546 || get_user_u32(optlen, vptr + 4 * n))
1547 return -TARGET_EFAULT;
1549 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
1553 gemu_log("Unsupported socketcall: %d\n", num);
1554 ret = -TARGET_ENOSYS;
1561 #ifdef TARGET_NR_ipc
1562 #define N_SHM_REGIONS 32
1564 static struct shm_region {
1567 } shm_regions[N_SHM_REGIONS];
1569 struct target_ipc_perm
1576 unsigned short int mode;
1577 unsigned short int __pad1;
1578 unsigned short int __seq;
1579 unsigned short int __pad2;
1580 abi_ulong __unused1;
1581 abi_ulong __unused2;
1584 struct target_semid_ds
1586 struct target_ipc_perm sem_perm;
1587 abi_ulong sem_otime;
1588 abi_ulong __unused1;
1589 abi_ulong sem_ctime;
1590 abi_ulong __unused2;
1591 abi_ulong sem_nsems;
1592 abi_ulong __unused3;
1593 abi_ulong __unused4;
1596 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1597 abi_ulong target_addr)
1599 struct target_ipc_perm *target_ip;
1600 struct target_semid_ds *target_sd;
1602 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1603 return -TARGET_EFAULT;
1604 target_ip=&(target_sd->sem_perm);
1605 host_ip->__key = tswapl(target_ip->__key);
1606 host_ip->uid = tswapl(target_ip->uid);
1607 host_ip->gid = tswapl(target_ip->gid);
1608 host_ip->cuid = tswapl(target_ip->cuid);
1609 host_ip->cgid = tswapl(target_ip->cgid);
1610 host_ip->mode = tswapl(target_ip->mode);
1611 unlock_user_struct(target_sd, target_addr, 0);
1615 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1616 struct ipc_perm *host_ip)
1618 struct target_ipc_perm *target_ip;
1619 struct target_semid_ds *target_sd;
1621 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1622 return -TARGET_EFAULT;
1623 target_ip = &(target_sd->sem_perm);
1624 target_ip->__key = tswapl(host_ip->__key);
1625 target_ip->uid = tswapl(host_ip->uid);
1626 target_ip->gid = tswapl(host_ip->gid);
1627 target_ip->cuid = tswapl(host_ip->cuid);
1628 target_ip->cgid = tswapl(host_ip->cgid);
1629 target_ip->mode = tswapl(host_ip->mode);
1630 unlock_user_struct(target_sd, target_addr, 1);
1634 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1635 abi_ulong target_addr)
1637 struct target_semid_ds *target_sd;
1639 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1640 return -TARGET_EFAULT;
1641 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1642 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1643 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1644 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1645 unlock_user_struct(target_sd, target_addr, 0);
1649 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1650 struct semid_ds *host_sd)
1652 struct target_semid_ds *target_sd;
1654 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1655 return -TARGET_EFAULT;
1656 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1657 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1658 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1659 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1660 unlock_user_struct(target_sd, target_addr, 1);
1666 struct semid_ds *buf;
1667 unsigned short *array;
1670 union target_semun {
1673 unsigned short int *array;
1676 static inline abi_long target_to_host_semun(int cmd,
1677 union semun *host_su,
1678 abi_ulong target_addr,
1679 struct semid_ds *ds)
1681 union target_semun *target_su;
1686 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1687 return -TARGET_EFAULT;
1688 target_to_host_semid_ds(ds,target_su->buf);
1690 unlock_user_struct(target_su, target_addr, 0);
1694 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1695 return -TARGET_EFAULT;
1696 host_su->val = tswapl(target_su->val);
1697 unlock_user_struct(target_su, target_addr, 0);
1701 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1702 return -TARGET_EFAULT;
1703 *host_su->array = tswap16(*target_su->array);
1704 unlock_user_struct(target_su, target_addr, 0);
1707 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1712 static inline abi_long host_to_target_semun(int cmd,
1713 abi_ulong target_addr,
1714 union semun *host_su,
1715 struct semid_ds *ds)
1717 union target_semun *target_su;
1722 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1723 return -TARGET_EFAULT;
1724 host_to_target_semid_ds(target_su->buf,ds);
1725 unlock_user_struct(target_su, target_addr, 1);
1729 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1730 return -TARGET_EFAULT;
1731 target_su->val = tswapl(host_su->val);
1732 unlock_user_struct(target_su, target_addr, 1);
1736 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1737 return -TARGET_EFAULT;
1738 *target_su->array = tswap16(*host_su->array);
1739 unlock_user_struct(target_su, target_addr, 1);
1742 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1747 static inline abi_long do_semctl(int first, int second, int third,
1751 struct semid_ds dsarg;
1752 int cmd = third&0xff;
1757 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1758 ret = get_errno(semctl(first, second, cmd, arg));
1759 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1762 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1763 ret = get_errno(semctl(first, second, cmd, arg));
1764 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1767 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1768 ret = get_errno(semctl(first, second, cmd, arg));
1769 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1772 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1773 ret = get_errno(semctl(first, second, cmd, arg));
1774 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1777 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1778 ret = get_errno(semctl(first, second, cmd, arg));
1779 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1782 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1783 ret = get_errno(semctl(first, second, cmd, arg));
1784 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1787 ret = get_errno(semctl(first, second, cmd, arg));
1793 struct target_msqid_ds
1795 struct target_ipc_perm msg_perm;
1796 abi_ulong msg_stime;
1797 abi_ulong __unused1;
1798 abi_ulong msg_rtime;
1799 abi_ulong __unused2;
1800 abi_ulong msg_ctime;
1801 abi_ulong __unused3;
1802 abi_ulong __msg_cbytes;
1804 abi_ulong msg_qbytes;
1805 abi_ulong msg_lspid;
1806 abi_ulong msg_lrpid;
1807 abi_ulong __unused4;
1808 abi_ulong __unused5;
1811 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1812 abi_ulong target_addr)
1814 struct target_msqid_ds *target_md;
1816 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1817 return -TARGET_EFAULT;
1818 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1819 host_md->msg_stime = tswapl(target_md->msg_stime);
1820 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1821 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1822 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1823 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1824 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1825 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1826 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1827 unlock_user_struct(target_md, target_addr, 0);
1831 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1832 struct msqid_ds *host_md)
1834 struct target_msqid_ds *target_md;
1836 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1837 return -TARGET_EFAULT;
1838 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1839 target_md->msg_stime = tswapl(host_md->msg_stime);
1840 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1841 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1842 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1843 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1844 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1845 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1846 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1847 unlock_user_struct(target_md, target_addr, 1);
1851 static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1853 struct msqid_ds dsarg;
1854 int cmd = second&0xff;
1859 target_to_host_msqid_ds(&dsarg,ptr);
1860 ret = get_errno(msgctl(first, cmd, &dsarg));
1861 host_to_target_msqid_ds(ptr,&dsarg);
1863 ret = get_errno(msgctl(first, cmd, &dsarg));
1868 struct target_msgbuf {
1873 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1874 unsigned int msgsz, int msgflg)
1876 struct target_msgbuf *target_mb;
1877 struct msgbuf *host_mb;
1880 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1881 return -TARGET_EFAULT;
1882 host_mb = malloc(msgsz+sizeof(long));
1883 host_mb->mtype = tswapl(target_mb->mtype);
1884 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1885 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1887 unlock_user_struct(target_mb, msgp, 0);
1892 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1893 unsigned int msgsz, int msgtype,
1896 struct target_msgbuf *target_mb;
1898 struct msgbuf *host_mb;
1901 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1902 return -TARGET_EFAULT;
1903 host_mb = malloc(msgsz+sizeof(long));
1904 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1906 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1907 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1908 if (!target_mtext) {
1909 ret = -TARGET_EFAULT;
1912 memcpy(target_mb->mtext, host_mb->mtext, ret);
1913 unlock_user(target_mtext, target_mtext_addr, ret);
1915 target_mb->mtype = tswapl(host_mb->mtype);
1920 unlock_user_struct(target_mb, msgp, 1);
1924 /* ??? This only works with linear mappings. */
1925 /* do_ipc() must return target values and target errnos. */
1926 static abi_long do_ipc(unsigned int call, int first,
1927 int second, int third,
1928 abi_long ptr, abi_long fifth)
1932 struct shmid_ds shm_info;
1935 version = call >> 16;
1940 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
1944 ret = get_errno(semget(first, second, third));
1948 ret = do_semctl(first, second, third, ptr);
1951 case IPCOP_semtimedop:
1952 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1953 ret = -TARGET_ENOSYS;
1957 ret = get_errno(msgget(first, second));
1961 ret = do_msgsnd(first, ptr, second, third);
1965 ret = do_msgctl(first, second, ptr);
1970 /* XXX: this code is not correct */
1973 void *__unbounded msgp;
1977 struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
1978 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
1980 ret = do_msgrcv(first, (long)msgp, second, 0, third);
1989 /* SHM_* flags are the same on all linux platforms */
1990 host_addr = shmat(first, (void *)g2h(ptr), second);
1991 if (host_addr == (void *)-1) {
1992 ret = get_errno((long)host_addr);
1995 raddr = h2g((unsigned long)host_addr);
1996 /* find out the length of the shared memory segment */
1998 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1999 if (is_error(ret)) {
2000 /* can't get length, bail out */
2004 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2005 PAGE_VALID | PAGE_READ |
2006 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2007 for (i = 0; i < N_SHM_REGIONS; ++i) {
2008 if (shm_regions[i].start == 0) {
2009 shm_regions[i].start = raddr;
2010 shm_regions[i].size = shm_info.shm_segsz;
2014 if (put_user_ual(raddr, third))
2015 return -TARGET_EFAULT;
2020 for (i = 0; i < N_SHM_REGIONS; ++i) {
2021 if (shm_regions[i].start == ptr) {
2022 shm_regions[i].start = 0;
2023 page_set_flags(ptr, shm_regions[i].size, 0);
2027 ret = get_errno(shmdt((void *)g2h(ptr)));
2031 /* IPC_* flag values are the same on all linux platforms */
2032 ret = get_errno(shmget(first, second, third));
2035 /* IPC_* and SHM_* command values are the same on all linux platforms */
2041 ret = get_errno(shmctl(first, second, NULL));
2049 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2050 ret = -TARGET_ENOSYS;
2057 /* kernel structure types definitions */
2060 #define STRUCT(name, list...) STRUCT_ ## name,
2061 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2063 #include "syscall_types.h"
2066 #undef STRUCT_SPECIAL
2068 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2069 #define STRUCT_SPECIAL(name)
2070 #include "syscall_types.h"
2072 #undef STRUCT_SPECIAL
2074 typedef struct IOCTLEntry {
2075 unsigned int target_cmd;
2076 unsigned int host_cmd;
2079 const argtype arg_type[5];
2082 #define IOC_R 0x0001
2083 #define IOC_W 0x0002
2084 #define IOC_RW (IOC_R | IOC_W)
2086 #define MAX_STRUCT_SIZE 4096
2088 IOCTLEntry ioctl_entries[] = {
2089 #define IOCTL(cmd, access, types...) \
2090 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2095 /* ??? Implement proper locking for ioctls. */
2096 /* do_ioctl() Must return target values and target errnos. */
2097 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
2099 const IOCTLEntry *ie;
2100 const argtype *arg_type;
2102 uint8_t buf_temp[MAX_STRUCT_SIZE];
2108 if (ie->target_cmd == 0) {
2109 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
2110 return -TARGET_ENOSYS;
2112 if (ie->target_cmd == cmd)
2116 arg_type = ie->arg_type;
2118 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
2120 switch(arg_type[0]) {
2123 ret = get_errno(ioctl(fd, ie->host_cmd));
2128 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2132 target_size = thunk_type_size(arg_type, 0);
2133 switch(ie->access) {
2135 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2136 if (!is_error(ret)) {
2137 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2139 return -TARGET_EFAULT;
2140 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2141 unlock_user(argptr, arg, target_size);
2145 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2147 return -TARGET_EFAULT;
2148 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2149 unlock_user(argptr, arg, 0);
2150 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2154 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2156 return -TARGET_EFAULT;
2157 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2158 unlock_user(argptr, arg, 0);
2159 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2160 if (!is_error(ret)) {
2161 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2163 return -TARGET_EFAULT;
2164 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2165 unlock_user(argptr, arg, target_size);
2171 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2172 (long)cmd, arg_type[0]);
2173 ret = -TARGET_ENOSYS;
2179 bitmask_transtbl iflag_tbl[] = {
2180 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2181 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2182 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2183 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2184 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2185 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2186 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2187 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2188 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2189 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2190 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2191 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2192 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2193 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2197 bitmask_transtbl oflag_tbl[] = {
2198 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2199 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2200 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2201 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2202 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2203 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2204 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2205 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2206 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2207 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2208 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2209 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2210 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2211 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2212 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2213 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2214 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2215 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2216 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2217 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2218 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2219 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2220 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2221 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2225 bitmask_transtbl cflag_tbl[] = {
2226 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2227 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2228 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2229 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2230 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2231 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2232 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2233 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2234 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2235 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2236 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2237 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2238 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2239 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2240 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2241 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2242 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2243 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2244 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2245 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2246 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2247 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2248 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2249 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2250 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2251 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2252 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2253 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2254 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2255 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2256 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2260 bitmask_transtbl lflag_tbl[] = {
2261 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2262 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2263 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2264 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2265 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2266 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2267 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2268 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2269 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2270 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2271 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2272 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2273 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2274 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2275 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2279 static void target_to_host_termios (void *dst, const void *src)
2281 struct host_termios *host = dst;
2282 const struct target_termios *target = src;
2285 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2287 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2289 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2291 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2292 host->c_line = target->c_line;
2294 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2295 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2296 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2297 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2298 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2299 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2300 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2301 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2302 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2303 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2304 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2305 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2306 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2307 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2308 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2309 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2310 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2313 static void host_to_target_termios (void *dst, const void *src)
2315 struct target_termios *target = dst;
2316 const struct host_termios *host = src;
2319 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2321 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2323 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2325 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2326 target->c_line = host->c_line;
2328 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2329 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2330 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2331 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2332 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2333 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2334 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2335 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2336 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2337 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2338 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2339 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2340 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2341 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2342 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2343 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2344 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2347 StructEntry struct_termios_def = {
2348 .convert = { host_to_target_termios, target_to_host_termios },
2349 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2350 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2353 static bitmask_transtbl mmap_flags_tbl[] = {
2354 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2355 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2356 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2357 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2358 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2359 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2360 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2361 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2365 static bitmask_transtbl fcntl_flags_tbl[] = {
2366 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2367 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2368 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2369 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2370 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2371 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2372 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2373 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2374 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2375 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2376 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2377 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2378 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2379 #if defined(O_DIRECT)
2380 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
2385 #if defined(TARGET_I386)
2387 /* NOTE: there is really one LDT for all the threads */
2390 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
2397 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2398 if (size > bytecount)
2400 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2402 return -TARGET_EFAULT;
2403 /* ??? Should this by byteswapped? */
2404 memcpy(p, ldt_table, size);
2405 unlock_user(p, ptr, size);
2409 /* XXX: add locking support */
2410 static abi_long write_ldt(CPUX86State *env,
2411 abi_ulong ptr, unsigned long bytecount, int oldmode)
2413 struct target_modify_ldt_ldt_s ldt_info;
2414 struct target_modify_ldt_ldt_s *target_ldt_info;
2415 int seg_32bit, contents, read_exec_only, limit_in_pages;
2416 int seg_not_present, useable, lm;
2417 uint32_t *lp, entry_1, entry_2;
2419 if (bytecount != sizeof(ldt_info))
2420 return -TARGET_EINVAL;
2421 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
2422 return -TARGET_EFAULT;
2423 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2424 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2425 ldt_info.limit = tswap32(target_ldt_info->limit);
2426 ldt_info.flags = tswap32(target_ldt_info->flags);
2427 unlock_user_struct(target_ldt_info, ptr, 0);
2429 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2430 return -TARGET_EINVAL;
2431 seg_32bit = ldt_info.flags & 1;
2432 contents = (ldt_info.flags >> 1) & 3;
2433 read_exec_only = (ldt_info.flags >> 3) & 1;
2434 limit_in_pages = (ldt_info.flags >> 4) & 1;
2435 seg_not_present = (ldt_info.flags >> 5) & 1;
2436 useable = (ldt_info.flags >> 6) & 1;
2440 lm = (ldt_info.flags >> 7) & 1;
2442 if (contents == 3) {
2444 return -TARGET_EINVAL;
2445 if (seg_not_present == 0)
2446 return -TARGET_EINVAL;
2448 /* allocate the LDT */
2450 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2452 return -TARGET_ENOMEM;
2453 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2454 env->ldt.base = h2g((unsigned long)ldt_table);
2455 env->ldt.limit = 0xffff;
2458 /* NOTE: same code as Linux kernel */
2459 /* Allow LDTs to be cleared by the user. */
2460 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2463 read_exec_only == 1 &&
2465 limit_in_pages == 0 &&
2466 seg_not_present == 1 &&
2474 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2475 (ldt_info.limit & 0x0ffff);
2476 entry_2 = (ldt_info.base_addr & 0xff000000) |
2477 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2478 (ldt_info.limit & 0xf0000) |
2479 ((read_exec_only ^ 1) << 9) |
2481 ((seg_not_present ^ 1) << 15) |
2483 (limit_in_pages << 23) |
2487 entry_2 |= (useable << 20);
2489 /* Install the new entry ... */
2491 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2492 lp[0] = tswap32(entry_1);
2493 lp[1] = tswap32(entry_2);
2497 /* specific and weird i386 syscalls */
2498 abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2499 unsigned long bytecount)
2505 ret = read_ldt(ptr, bytecount);
2508 ret = write_ldt(env, ptr, bytecount, 1);
2511 ret = write_ldt(env, ptr, bytecount, 0);
2514 ret = -TARGET_ENOSYS;
2520 abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
2522 uint64_t *gdt_table = g2h(env->gdt.base);
2523 struct target_modify_ldt_ldt_s ldt_info;
2524 struct target_modify_ldt_ldt_s *target_ldt_info;
2525 int seg_32bit, contents, read_exec_only, limit_in_pages;
2526 int seg_not_present, useable, lm;
2527 uint32_t *lp, entry_1, entry_2;
2530 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2531 if (!target_ldt_info)
2532 return -TARGET_EFAULT;
2533 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2534 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2535 ldt_info.limit = tswap32(target_ldt_info->limit);
2536 ldt_info.flags = tswap32(target_ldt_info->flags);
2537 if (ldt_info.entry_number == -1) {
2538 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
2539 if (gdt_table[i] == 0) {
2540 ldt_info.entry_number = i;
2541 target_ldt_info->entry_number = tswap32(i);
2546 unlock_user_struct(target_ldt_info, ptr, 1);
2548 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
2549 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
2550 return -TARGET_EINVAL;
2551 seg_32bit = ldt_info.flags & 1;
2552 contents = (ldt_info.flags >> 1) & 3;
2553 read_exec_only = (ldt_info.flags >> 3) & 1;
2554 limit_in_pages = (ldt_info.flags >> 4) & 1;
2555 seg_not_present = (ldt_info.flags >> 5) & 1;
2556 useable = (ldt_info.flags >> 6) & 1;
2560 lm = (ldt_info.flags >> 7) & 1;
2563 if (contents == 3) {
2564 if (seg_not_present == 0)
2565 return -TARGET_EINVAL;
2568 /* NOTE: same code as Linux kernel */
2569 /* Allow LDTs to be cleared by the user. */
2570 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2571 if ((contents == 0 &&
2572 read_exec_only == 1 &&
2574 limit_in_pages == 0 &&
2575 seg_not_present == 1 &&
2583 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2584 (ldt_info.limit & 0x0ffff);
2585 entry_2 = (ldt_info.base_addr & 0xff000000) |
2586 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2587 (ldt_info.limit & 0xf0000) |
2588 ((read_exec_only ^ 1) << 9) |
2590 ((seg_not_present ^ 1) << 15) |
2592 (limit_in_pages << 23) |
2597 /* Install the new entry ... */
2599 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
2600 lp[0] = tswap32(entry_1);
2601 lp[1] = tswap32(entry_2);
2605 abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
2607 struct target_modify_ldt_ldt_s *target_ldt_info;
2608 uint64_t *gdt_table = g2h(env->gdt.base);
2609 uint32_t base_addr, limit, flags;
2610 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
2611 int seg_not_present, useable, lm;
2612 uint32_t *lp, entry_1, entry_2;
2614 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2615 if (!target_ldt_info)
2616 return -TARGET_EFAULT;
2617 idx = tswap32(target_ldt_info->entry_number);
2618 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
2619 idx > TARGET_GDT_ENTRY_TLS_MAX) {
2620 unlock_user_struct(target_ldt_info, ptr, 1);
2621 return -TARGET_EINVAL;
2623 lp = (uint32_t *)(gdt_table + idx);
2624 entry_1 = tswap32(lp[0]);
2625 entry_2 = tswap32(lp[1]);
2627 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
2628 contents = (entry_2 >> 10) & 3;
2629 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
2630 seg_32bit = (entry_2 >> 22) & 1;
2631 limit_in_pages = (entry_2 >> 23) & 1;
2632 useable = (entry_2 >> 20) & 1;
2636 lm = (entry_2 >> 21) & 1;
2638 flags = (seg_32bit << 0) | (contents << 1) |
2639 (read_exec_only << 3) | (limit_in_pages << 4) |
2640 (seg_not_present << 5) | (useable << 6) | (lm << 7);
2641 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
2642 base_addr = (entry_1 >> 16) |
2643 (entry_2 & 0xff000000) |
2644 ((entry_2 & 0xff) << 16);
2645 target_ldt_info->base_addr = tswapl(base_addr);
2646 target_ldt_info->limit = tswap32(limit);
2647 target_ldt_info->flags = tswap32(flags);
2648 unlock_user_struct(target_ldt_info, ptr, 1);
2652 #ifndef TARGET_ABI32
2653 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
2660 case TARGET_ARCH_SET_GS:
2661 case TARGET_ARCH_SET_FS:
2662 if (code == TARGET_ARCH_SET_GS)
2666 cpu_x86_load_seg(env, idx, 0);
2667 env->segs[idx].base = addr;
2669 case TARGET_ARCH_GET_GS:
2670 case TARGET_ARCH_GET_FS:
2671 if (code == TARGET_ARCH_GET_GS)
2675 val = env->segs[idx].base;
2676 if (put_user(val, addr, abi_ulong))
2677 return -TARGET_EFAULT;
2680 ret = -TARGET_EINVAL;
2687 #endif /* defined(TARGET_I386) */
2689 /* this stack is the equivalent of the kernel stack associated with a
2691 #define NEW_STACK_SIZE 8192
2693 static int clone_func(void *arg)
2695 CPUState *env = arg;
2701 /* do_fork() Must return host values and target errnos (unlike most
2702 do_*() functions). */
2703 int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
2710 if (flags & CLONE_VM) {
2711 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2712 memset(ts, 0, sizeof(TaskState));
2713 new_stack = ts->stack;
2715 /* add in task state list */
2716 ts->next = first_task_state;
2717 first_task_state = ts;
2718 /* we create a new CPU instance. */
2719 new_env = cpu_copy(env);
2720 #if defined(TARGET_I386)
2722 newsp = env->regs[R_ESP];
2723 new_env->regs[R_ESP] = newsp;
2724 new_env->regs[R_EAX] = 0;
2725 #elif defined(TARGET_ARM)
2727 newsp = env->regs[13];
2728 new_env->regs[13] = newsp;
2729 new_env->regs[0] = 0;
2730 #elif defined(TARGET_SPARC)
2732 newsp = env->regwptr[22];
2733 new_env->regwptr[22] = newsp;
2734 new_env->regwptr[0] = 0;
2736 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
2737 #elif defined(TARGET_M68K)
2739 newsp = env->aregs[7];
2740 new_env->aregs[7] = newsp;
2741 new_env->dregs[0] = 0;
2742 /* ??? is this sufficient? */
2743 #elif defined(TARGET_MIPS)
2745 newsp = env->gpr[29][env->current_tc];
2746 new_env->gpr[29][env->current_tc] = newsp;
2747 #elif defined(TARGET_PPC)
2749 newsp = env->gpr[1];
2750 new_env->gpr[1] = newsp;
2753 for (i = 7; i < 32; i++)
2754 new_env->gpr[i] = 0;
2756 #elif defined(TARGET_SH4)
2758 newsp = env->gregs[15];
2759 new_env->gregs[15] = newsp;
2761 #elif defined(TARGET_ALPHA)
2763 newsp = env->ir[30];
2764 new_env->ir[30] = newsp;
2768 for (i = 7; i < 30; i++)
2771 #elif defined(TARGET_CRIS)
2773 newsp = env->regs[14];
2774 new_env->regs[14] = newsp;
2776 #error unsupported target CPU
2778 new_env->opaque = ts;
2780 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2782 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2785 /* if no CLONE_VM, we consider it is a fork */
2786 if ((flags & ~CSIGNAL) != 0)
2793 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2796 struct target_flock *target_fl;
2797 struct flock64 fl64;
2798 struct target_flock64 *target_fl64;
2802 case TARGET_F_GETLK:
2803 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2804 return -TARGET_EFAULT;
2805 fl.l_type = tswap16(target_fl->l_type);
2806 fl.l_whence = tswap16(target_fl->l_whence);
2807 fl.l_start = tswapl(target_fl->l_start);
2808 fl.l_len = tswapl(target_fl->l_len);
2809 fl.l_pid = tswapl(target_fl->l_pid);
2810 unlock_user_struct(target_fl, arg, 0);
2811 ret = get_errno(fcntl(fd, cmd, &fl));
2813 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2814 return -TARGET_EFAULT;
2815 target_fl->l_type = tswap16(fl.l_type);
2816 target_fl->l_whence = tswap16(fl.l_whence);
2817 target_fl->l_start = tswapl(fl.l_start);
2818 target_fl->l_len = tswapl(fl.l_len);
2819 target_fl->l_pid = tswapl(fl.l_pid);
2820 unlock_user_struct(target_fl, arg, 1);
2824 case TARGET_F_SETLK:
2825 case TARGET_F_SETLKW:
2826 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2827 return -TARGET_EFAULT;
2828 fl.l_type = tswap16(target_fl->l_type);
2829 fl.l_whence = tswap16(target_fl->l_whence);
2830 fl.l_start = tswapl(target_fl->l_start);
2831 fl.l_len = tswapl(target_fl->l_len);
2832 fl.l_pid = tswapl(target_fl->l_pid);
2833 unlock_user_struct(target_fl, arg, 0);
2834 ret = get_errno(fcntl(fd, cmd, &fl));
2837 case TARGET_F_GETLK64:
2838 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2839 return -TARGET_EFAULT;
2840 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2841 fl64.l_whence = tswap16(target_fl64->l_whence);
2842 fl64.l_start = tswapl(target_fl64->l_start);
2843 fl64.l_len = tswapl(target_fl64->l_len);
2844 fl64.l_pid = tswap16(target_fl64->l_pid);
2845 unlock_user_struct(target_fl64, arg, 0);
2846 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2848 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2849 return -TARGET_EFAULT;
2850 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2851 target_fl64->l_whence = tswap16(fl64.l_whence);
2852 target_fl64->l_start = tswapl(fl64.l_start);
2853 target_fl64->l_len = tswapl(fl64.l_len);
2854 target_fl64->l_pid = tswapl(fl64.l_pid);
2855 unlock_user_struct(target_fl64, arg, 1);
2858 case TARGET_F_SETLK64:
2859 case TARGET_F_SETLKW64:
2860 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2861 return -TARGET_EFAULT;
2862 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2863 fl64.l_whence = tswap16(target_fl64->l_whence);
2864 fl64.l_start = tswapl(target_fl64->l_start);
2865 fl64.l_len = tswapl(target_fl64->l_len);
2866 fl64.l_pid = tswap16(target_fl64->l_pid);
2867 unlock_user_struct(target_fl64, arg, 0);
2868 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2872 ret = get_errno(fcntl(fd, cmd, arg));
2874 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2879 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
2883 ret = get_errno(fcntl(fd, cmd, arg));
2891 static inline int high2lowuid(int uid)
2899 static inline int high2lowgid(int gid)
2907 static inline int low2highuid(int uid)
2909 if ((int16_t)uid == -1)
2915 static inline int low2highgid(int gid)
2917 if ((int16_t)gid == -1)
2923 #endif /* USE_UID16 */
2925 void syscall_init(void)
2928 const argtype *arg_type;
2932 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2933 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
2934 #include "syscall_types.h"
2936 #undef STRUCT_SPECIAL
2938 /* we patch the ioctl size if necessary. We rely on the fact that
2939 no ioctl has all the bits at '1' in the size field */
2941 while (ie->target_cmd != 0) {
2942 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2943 TARGET_IOC_SIZEMASK) {
2944 arg_type = ie->arg_type;
2945 if (arg_type[0] != TYPE_PTR) {
2946 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2951 size = thunk_type_size(arg_type, 0);
2952 ie->target_cmd = (ie->target_cmd &
2953 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2954 (size << TARGET_IOC_SIZESHIFT);
2957 /* Build target_to_host_errno_table[] table from
2958 * host_to_target_errno_table[]. */
2959 for (i=0; i < ERRNO_TABLE_SIZE; i++)
2960 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2962 /* automatic consistency check if same arch */
2963 #if defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)
2964 if (ie->target_cmd != ie->host_cmd) {
2965 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2966 ie->target_cmd, ie->host_cmd);
2973 #if TARGET_ABI_BITS == 32
2974 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2976 #ifdef TARGET_WORDS_BIG_ENDIAN
2977 return ((uint64_t)word0 << 32) | word1;
2979 return ((uint64_t)word1 << 32) | word0;
2982 #else /* TARGET_ABI_BITS == 32 */
2983 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2987 #endif /* TARGET_ABI_BITS != 32 */
2989 #ifdef TARGET_NR_truncate64
2990 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2996 if (((CPUARMState *)cpu_env)->eabi)
3002 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3006 #ifdef TARGET_NR_ftruncate64
3007 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3013 if (((CPUARMState *)cpu_env)->eabi)
3019 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3023 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3024 abi_ulong target_addr)
3026 struct target_timespec *target_ts;
3028 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3029 return -TARGET_EFAULT;
3030 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3031 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3032 unlock_user_struct(target_ts, target_addr, 0);
3035 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3036 struct timespec *host_ts)
3038 struct target_timespec *target_ts;
3040 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3041 return -TARGET_EFAULT;
3042 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3043 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3044 unlock_user_struct(target_ts, target_addr, 1);
3047 /* do_syscall() should always have a single exit point at the end so
3048 that actions, such as logging of syscall results, can be performed.
3049 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
3050 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3051 abi_long arg2, abi_long arg3, abi_long arg4,
3052 abi_long arg5, abi_long arg6)
3060 gemu_log("syscall %d", num);
3063 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3066 case TARGET_NR_exit:
3070 gdb_exit(cpu_env, arg1);
3071 /* XXX: should free thread stack and CPU env */
3073 ret = 0; /* avoid warning */
3075 case TARGET_NR_read:
3076 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3078 ret = get_errno(read(arg1, p, arg3));
3079 unlock_user(p, arg2, ret);
3081 case TARGET_NR_write:
3082 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3084 ret = get_errno(write(arg1, p, arg3));
3085 unlock_user(p, arg2, 0);
3087 case TARGET_NR_open:
3088 if (!(p = lock_user_string(arg1)))
3090 ret = get_errno(open(path(p),
3091 target_to_host_bitmask(arg2, fcntl_flags_tbl),
3093 unlock_user(p, arg1, 0);
3095 #if defined(TARGET_NR_openat) && defined(__NR_openat)
3096 case TARGET_NR_openat:
3097 if (!(p = lock_user_string(arg2)))
3099 ret = get_errno(sys_openat(arg1,
3101 target_to_host_bitmask(arg3, fcntl_flags_tbl),
3103 unlock_user(p, arg2, 0);
3106 case TARGET_NR_close:
3107 ret = get_errno(close(arg1));
3112 case TARGET_NR_fork:
3113 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
3115 #ifdef TARGET_NR_waitpid
3116 case TARGET_NR_waitpid:
3119 ret = get_errno(waitpid(arg1, &status, arg3));
3120 if (!is_error(ret) && arg2
3121 && put_user_s32(status, arg2))
3126 #ifdef TARGET_NR_creat /* not on alpha */
3127 case TARGET_NR_creat:
3128 if (!(p = lock_user_string(arg1)))
3130 ret = get_errno(creat(p, arg2));
3131 unlock_user(p, arg1, 0);
3134 case TARGET_NR_link:
3137 p = lock_user_string(arg1);
3138 p2 = lock_user_string(arg2);
3140 ret = -TARGET_EFAULT;
3142 ret = get_errno(link(p, p2));
3143 unlock_user(p2, arg2, 0);
3144 unlock_user(p, arg1, 0);
3147 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3148 case TARGET_NR_linkat:
3153 p = lock_user_string(arg2);
3154 p2 = lock_user_string(arg4);
3156 ret = -TARGET_EFAULT;
3158 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
3159 unlock_user(p, arg2, 0);
3160 unlock_user(p2, arg4, 0);
3164 case TARGET_NR_unlink:
3165 if (!(p = lock_user_string(arg1)))
3167 ret = get_errno(unlink(p));
3168 unlock_user(p, arg1, 0);
3170 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3171 case TARGET_NR_unlinkat:
3172 if (!(p = lock_user_string(arg2)))
3174 ret = get_errno(sys_unlinkat(arg1, p, arg3));
3175 unlock_user(p, arg2, 0);
3177 case TARGET_NR_execve:
3179 char **argp, **envp;
3182 abi_ulong guest_argp;
3183 abi_ulong guest_envp;
3189 for (gp = guest_argp; ; gp++) {
3190 if (get_user_ual(addr, gp))
3198 for (gp = guest_envp; ; gp++) {
3199 if (get_user_ual(addr, gp))
3206 argp = alloca((argc + 1) * sizeof(void *));
3207 envp = alloca((envc + 1) * sizeof(void *));
3209 for (gp = guest_argp, q = argp; ;
3210 gp += sizeof(abi_ulong), q++) {
3211 if (get_user_ual(addr, gp))
3215 if (!(*q = lock_user_string(addr)))
3220 for (gp = guest_envp, q = envp; ;
3221 gp += sizeof(abi_ulong), q++) {
3222 if (get_user_ual(addr, gp))
3226 if (!(*q = lock_user_string(addr)))
3231 if (!(p = lock_user_string(arg1)))
3233 ret = get_errno(execve(p, argp, envp));
3234 unlock_user(p, arg1, 0);
3239 ret = -TARGET_EFAULT;
3242 for (gp = guest_argp, q = argp; *q;
3243 gp += sizeof(abi_ulong), q++) {
3244 if (get_user_ual(addr, gp)
3247 unlock_user(*q, addr, 0);
3249 for (gp = guest_envp, q = envp; *q;
3250 gp += sizeof(abi_ulong), q++) {
3251 if (get_user_ual(addr, gp)
3254 unlock_user(*q, addr, 0);
3258 case TARGET_NR_chdir:
3259 if (!(p = lock_user_string(arg1)))
3261 ret = get_errno(chdir(p));
3262 unlock_user(p, arg1, 0);
3264 #ifdef TARGET_NR_time
3265 case TARGET_NR_time:
3268 ret = get_errno(time(&host_time));
3271 && put_user_sal(host_time, arg1))
3276 case TARGET_NR_mknod:
3277 if (!(p = lock_user_string(arg1)))
3279 ret = get_errno(mknod(p, arg2, arg3));
3280 unlock_user(p, arg1, 0);
3282 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
3283 case TARGET_NR_mknodat:
3284 if (!(p = lock_user_string(arg2)))
3286 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
3287 unlock_user(p, arg2, 0);
3290 case TARGET_NR_chmod:
3291 if (!(p = lock_user_string(arg1)))
3293 ret = get_errno(chmod(p, arg2));
3294 unlock_user(p, arg1, 0);
3296 #ifdef TARGET_NR_break
3297 case TARGET_NR_break:
3300 #ifdef TARGET_NR_oldstat
3301 case TARGET_NR_oldstat:
3304 case TARGET_NR_lseek:
3305 ret = get_errno(lseek(arg1, arg2, arg3));
3307 #ifdef TARGET_NR_getxpid
3308 case TARGET_NR_getxpid:
3310 case TARGET_NR_getpid:
3312 ret = get_errno(getpid());
3314 case TARGET_NR_mount:
3316 /* need to look at the data field */
3318 p = lock_user_string(arg1);
3319 p2 = lock_user_string(arg2);
3320 p3 = lock_user_string(arg3);
3321 if (!p || !p2 || !p3)
3322 ret = -TARGET_EFAULT;
3324 /* FIXME - arg5 should be locked, but it isn't clear how to
3325 * do that since it's not guaranteed to be a NULL-terminated
3328 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3329 unlock_user(p, arg1, 0);
3330 unlock_user(p2, arg2, 0);
3331 unlock_user(p3, arg3, 0);
3334 #ifdef TARGET_NR_umount
3335 case TARGET_NR_umount:
3336 if (!(p = lock_user_string(arg1)))
3338 ret = get_errno(umount(p));
3339 unlock_user(p, arg1, 0);
3342 #ifdef TARGET_NR_stime /* not on alpha */
3343 case TARGET_NR_stime:
3346 if (get_user_sal(host_time, arg1))
3348 ret = get_errno(stime(&host_time));
3352 case TARGET_NR_ptrace:
3354 #ifdef TARGET_NR_alarm /* not on alpha */
3355 case TARGET_NR_alarm:
3359 #ifdef TARGET_NR_oldfstat
3360 case TARGET_NR_oldfstat:
3363 #ifdef TARGET_NR_pause /* not on alpha */
3364 case TARGET_NR_pause:
3365 ret = get_errno(pause());
3368 #ifdef TARGET_NR_utime
3369 case TARGET_NR_utime:
3371 struct utimbuf tbuf, *host_tbuf;
3372 struct target_utimbuf *target_tbuf;
3374 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3376 tbuf.actime = tswapl(target_tbuf->actime);
3377 tbuf.modtime = tswapl(target_tbuf->modtime);
3378 unlock_user_struct(target_tbuf, arg2, 0);
3383 if (!(p = lock_user_string(arg1)))
3385 ret = get_errno(utime(p, host_tbuf));
3386 unlock_user(p, arg1, 0);
3390 case TARGET_NR_utimes:
3392 struct timeval *tvp, tv[2];
3394 target_to_host_timeval(&tv[0], arg2);
3395 target_to_host_timeval(&tv[1],
3396 arg2 + sizeof (struct target_timeval));
3401 if (!(p = lock_user_string(arg1)))
3403 ret = get_errno(utimes(p, tvp));
3404 unlock_user(p, arg1, 0);
3407 #ifdef TARGET_NR_stty
3408 case TARGET_NR_stty:
3411 #ifdef TARGET_NR_gtty
3412 case TARGET_NR_gtty:
3415 case TARGET_NR_access:
3416 if (!(p = lock_user_string(arg1)))
3418 ret = get_errno(access(p, arg2));
3419 unlock_user(p, arg1, 0);
3421 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3422 case TARGET_NR_faccessat:
3423 if (!(p = lock_user_string(arg2)))
3425 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3426 unlock_user(p, arg2, 0);
3429 #ifdef TARGET_NR_nice /* not on alpha */
3430 case TARGET_NR_nice:
3431 ret = get_errno(nice(arg1));
3434 #ifdef TARGET_NR_ftime
3435 case TARGET_NR_ftime:
3438 case TARGET_NR_sync:
3442 case TARGET_NR_kill:
3443 ret = get_errno(kill(arg1, arg2));
3445 case TARGET_NR_rename:
3448 p = lock_user_string(arg1);
3449 p2 = lock_user_string(arg2);
3451 ret = -TARGET_EFAULT;
3453 ret = get_errno(rename(p, p2));
3454 unlock_user(p2, arg2, 0);
3455 unlock_user(p, arg1, 0);
3458 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3459 case TARGET_NR_renameat:
3462 p = lock_user_string(arg2);
3463 p2 = lock_user_string(arg4);
3465 ret = -TARGET_EFAULT;
3467 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
3468 unlock_user(p2, arg4, 0);
3469 unlock_user(p, arg2, 0);
3473 case TARGET_NR_mkdir:
3474 if (!(p = lock_user_string(arg1)))
3476 ret = get_errno(mkdir(p, arg2));
3477 unlock_user(p, arg1, 0);
3479 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3480 case TARGET_NR_mkdirat:
3481 if (!(p = lock_user_string(arg2)))
3483 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3484 unlock_user(p, arg2, 0);
3487 case TARGET_NR_rmdir:
3488 if (!(p = lock_user_string(arg1)))
3490 ret = get_errno(rmdir(p));
3491 unlock_user(p, arg1, 0);
3494 ret = get_errno(dup(arg1));
3496 case TARGET_NR_pipe:
3499 ret = get_errno(pipe(host_pipe));
3500 if (!is_error(ret)) {
3501 #if defined(TARGET_MIPS)
3502 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3503 env->gpr[3][env->current_tc] = host_pipe[1];
3506 if (put_user_s32(host_pipe[0], arg1)
3507 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
3513 case TARGET_NR_times:
3515 struct target_tms *tmsp;
3517 ret = get_errno(times(&tms));
3519 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3522 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3523 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3524 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3525 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
3528 ret = host_to_target_clock_t(ret);
3531 #ifdef TARGET_NR_prof
3532 case TARGET_NR_prof:
3535 #ifdef TARGET_NR_signal
3536 case TARGET_NR_signal:
3539 case TARGET_NR_acct:
3540 if (!(p = lock_user_string(arg1)))
3542 ret = get_errno(acct(path(p)));
3543 unlock_user(p, arg1, 0);
3545 #ifdef TARGET_NR_umount2 /* not on alpha */
3546 case TARGET_NR_umount2:
3547 if (!(p = lock_user_string(arg1)))
3549 ret = get_errno(umount2(p, arg2));
3550 unlock_user(p, arg1, 0);
3553 #ifdef TARGET_NR_lock
3554 case TARGET_NR_lock:
3557 case TARGET_NR_ioctl:
3558 ret = do_ioctl(arg1, arg2, arg3);
3560 case TARGET_NR_fcntl:
3561 ret = do_fcntl(arg1, arg2, arg3);
3563 #ifdef TARGET_NR_mpx
3567 case TARGET_NR_setpgid:
3568 ret = get_errno(setpgid(arg1, arg2));
3570 #ifdef TARGET_NR_ulimit
3571 case TARGET_NR_ulimit:
3574 #ifdef TARGET_NR_oldolduname
3575 case TARGET_NR_oldolduname:
3578 case TARGET_NR_umask:
3579 ret = get_errno(umask(arg1));
3581 case TARGET_NR_chroot:
3582 if (!(p = lock_user_string(arg1)))
3584 ret = get_errno(chroot(p));
3585 unlock_user(p, arg1, 0);
3587 case TARGET_NR_ustat:
3589 case TARGET_NR_dup2:
3590 ret = get_errno(dup2(arg1, arg2));
3592 #ifdef TARGET_NR_getppid /* not on alpha */
3593 case TARGET_NR_getppid:
3594 ret = get_errno(getppid());
3597 case TARGET_NR_getpgrp:
3598 ret = get_errno(getpgrp());
3600 case TARGET_NR_setsid:
3601 ret = get_errno(setsid());
3603 #ifdef TARGET_NR_sigaction
3604 case TARGET_NR_sigaction:
3606 #if !defined(TARGET_MIPS)
3607 struct target_old_sigaction *old_act;
3608 struct target_sigaction act, oact, *pact;
3610 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3612 act._sa_handler = old_act->_sa_handler;
3613 target_siginitset(&act.sa_mask, old_act->sa_mask);
3614 act.sa_flags = old_act->sa_flags;
3615 act.sa_restorer = old_act->sa_restorer;
3616 unlock_user_struct(old_act, arg2, 0);
3621 ret = get_errno(do_sigaction(arg1, pact, &oact));
3622 if (!is_error(ret) && arg3) {
3623 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3625 old_act->_sa_handler = oact._sa_handler;
3626 old_act->sa_mask = oact.sa_mask.sig[0];
3627 old_act->sa_flags = oact.sa_flags;
3628 old_act->sa_restorer = oact.sa_restorer;
3629 unlock_user_struct(old_act, arg3, 1);
3632 struct target_sigaction act, oact, *pact, *old_act;
3635 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3637 act._sa_handler = old_act->_sa_handler;
3638 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3639 act.sa_flags = old_act->sa_flags;
3640 unlock_user_struct(old_act, arg2, 0);
3646 ret = get_errno(do_sigaction(arg1, pact, &oact));
3648 if (!is_error(ret) && arg3) {
3649 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3651 old_act->_sa_handler = oact._sa_handler;
3652 old_act->sa_flags = oact.sa_flags;
3653 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3654 old_act->sa_mask.sig[1] = 0;
3655 old_act->sa_mask.sig[2] = 0;
3656 old_act->sa_mask.sig[3] = 0;
3657 unlock_user_struct(old_act, arg3, 1);
3663 case TARGET_NR_rt_sigaction:
3665 struct target_sigaction *act;
3666 struct target_sigaction *oact;
3669 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3674 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3675 ret = -TARGET_EFAULT;
3676 goto rt_sigaction_fail;
3680 ret = get_errno(do_sigaction(arg1, act, oact));
3683 unlock_user_struct(act, arg2, 0);
3685 unlock_user_struct(oact, arg3, 1);
3688 #ifdef TARGET_NR_sgetmask /* not on alpha */
3689 case TARGET_NR_sgetmask:
3692 abi_ulong target_set;
3693 sigprocmask(0, NULL, &cur_set);
3694 host_to_target_old_sigset(&target_set, &cur_set);
3699 #ifdef TARGET_NR_ssetmask /* not on alpha */
3700 case TARGET_NR_ssetmask:
3702 sigset_t set, oset, cur_set;
3703 abi_ulong target_set = arg1;
3704 sigprocmask(0, NULL, &cur_set);
3705 target_to_host_old_sigset(&set, &target_set);
3706 sigorset(&set, &set, &cur_set);
3707 sigprocmask(SIG_SETMASK, &set, &oset);
3708 host_to_target_old_sigset(&target_set, &oset);
3713 #ifdef TARGET_NR_sigprocmask
3714 case TARGET_NR_sigprocmask:
3717 sigset_t set, oldset, *set_ptr;
3721 case TARGET_SIG_BLOCK:
3724 case TARGET_SIG_UNBLOCK:
3727 case TARGET_SIG_SETMASK:
3731 ret = -TARGET_EINVAL;
3734 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3736 target_to_host_old_sigset(&set, p);
3737 unlock_user(p, arg2, 0);
3743 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
3744 if (!is_error(ret) && arg3) {
3745 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3747 host_to_target_old_sigset(p, &oldset);
3748 unlock_user(p, arg3, sizeof(target_sigset_t));
3753 case TARGET_NR_rt_sigprocmask:
3756 sigset_t set, oldset, *set_ptr;
3760 case TARGET_SIG_BLOCK:
3763 case TARGET_SIG_UNBLOCK:
3766 case TARGET_SIG_SETMASK:
3770 ret = -TARGET_EINVAL;
3773 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3775 target_to_host_sigset(&set, p);
3776 unlock_user(p, arg2, 0);
3782 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
3783 if (!is_error(ret) && arg3) {
3784 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3786 host_to_target_sigset(p, &oldset);
3787 unlock_user(p, arg3, sizeof(target_sigset_t));
3791 #ifdef TARGET_NR_sigpending
3792 case TARGET_NR_sigpending:
3795 ret = get_errno(sigpending(&set));
3796 if (!is_error(ret)) {
3797 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3799 host_to_target_old_sigset(p, &set);
3800 unlock_user(p, arg1, sizeof(target_sigset_t));
3805 case TARGET_NR_rt_sigpending:
3808 ret = get_errno(sigpending(&set));
3809 if (!is_error(ret)) {
3810 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3812 host_to_target_sigset(p, &set);
3813 unlock_user(p, arg1, sizeof(target_sigset_t));
3817 #ifdef TARGET_NR_sigsuspend
3818 case TARGET_NR_sigsuspend:
3821 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3823 target_to_host_old_sigset(&set, p);
3824 unlock_user(p, arg1, 0);
3825 ret = get_errno(sigsuspend(&set));
3829 case TARGET_NR_rt_sigsuspend:
3832 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3834 target_to_host_sigset(&set, p);
3835 unlock_user(p, arg1, 0);
3836 ret = get_errno(sigsuspend(&set));
3839 case TARGET_NR_rt_sigtimedwait:
3842 struct timespec uts, *puts;
3845 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3847 target_to_host_sigset(&set, p);
3848 unlock_user(p, arg1, 0);
3851 target_to_host_timespec(puts, arg3);
3855 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
3856 if (!is_error(ret) && arg2) {
3857 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3859 host_to_target_siginfo(p, &uinfo);
3860 unlock_user(p, arg2, sizeof(target_sigset_t));
3864 case TARGET_NR_rt_sigqueueinfo:
3867 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3869 target_to_host_siginfo(&uinfo, p);
3870 unlock_user(p, arg1, 0);
3871 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3874 #ifdef TARGET_NR_sigreturn
3875 case TARGET_NR_sigreturn:
3876 /* NOTE: ret is eax, so not transcoding must be done */
3877 ret = do_sigreturn(cpu_env);
3880 case TARGET_NR_rt_sigreturn:
3881 /* NOTE: ret is eax, so not transcoding must be done */
3882 ret = do_rt_sigreturn(cpu_env);
3884 case TARGET_NR_sethostname:
3885 if (!(p = lock_user_string(arg1)))
3887 ret = get_errno(sethostname(p, arg2));
3888 unlock_user(p, arg1, 0);
3890 case TARGET_NR_setrlimit:
3892 /* XXX: convert resource ? */
3893 int resource = arg1;
3894 struct target_rlimit *target_rlim;
3896 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3898 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3899 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3900 unlock_user_struct(target_rlim, arg2, 0);
3901 ret = get_errno(setrlimit(resource, &rlim));
3904 case TARGET_NR_getrlimit:
3906 /* XXX: convert resource ? */
3907 int resource = arg1;
3908 struct target_rlimit *target_rlim;
3911 ret = get_errno(getrlimit(resource, &rlim));
3912 if (!is_error(ret)) {
3913 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3915 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3916 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3917 unlock_user_struct(target_rlim, arg2, 1);
3921 case TARGET_NR_getrusage:
3923 struct rusage rusage;
3924 ret = get_errno(getrusage(arg1, &rusage));
3925 if (!is_error(ret)) {
3926 host_to_target_rusage(arg2, &rusage);
3930 case TARGET_NR_gettimeofday:
3933 ret = get_errno(gettimeofday(&tv, NULL));
3934 if (!is_error(ret)) {
3935 host_to_target_timeval(arg1, &tv);
3939 case TARGET_NR_settimeofday:
3942 target_to_host_timeval(&tv, arg1);
3943 ret = get_errno(settimeofday(&tv, NULL));
3946 #ifdef TARGET_NR_select
3947 case TARGET_NR_select:
3949 struct target_sel_arg_struct *sel;
3950 abi_ulong inp, outp, exp, tvp;
3953 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3955 nsel = tswapl(sel->n);
3956 inp = tswapl(sel->inp);
3957 outp = tswapl(sel->outp);
3958 exp = tswapl(sel->exp);
3959 tvp = tswapl(sel->tvp);
3960 unlock_user_struct(sel, arg1, 0);
3961 ret = do_select(nsel, inp, outp, exp, tvp);
3965 case TARGET_NR_symlink:
3968 p = lock_user_string(arg1);
3969 p2 = lock_user_string(arg2);
3971 ret = -TARGET_EFAULT;
3973 ret = get_errno(symlink(p, p2));
3974 unlock_user(p2, arg2, 0);
3975 unlock_user(p, arg1, 0);
3978 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3979 case TARGET_NR_symlinkat:
3982 p = lock_user_string(arg1);
3983 p2 = lock_user_string(arg3);
3985 ret = -TARGET_EFAULT;
3987 ret = get_errno(sys_symlinkat(p, arg2, p2));
3988 unlock_user(p2, arg3, 0);
3989 unlock_user(p, arg1, 0);
3993 #ifdef TARGET_NR_oldlstat
3994 case TARGET_NR_oldlstat:
3997 case TARGET_NR_readlink:
4000 p = lock_user_string(arg1);
4001 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4003 ret = -TARGET_EFAULT;
4005 ret = get_errno(readlink(path(p), p2, arg3));
4006 unlock_user(p2, arg2, ret);
4007 unlock_user(p, arg1, 0);
4010 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4011 case TARGET_NR_readlinkat:
4014 p = lock_user_string(arg2);
4015 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4017 ret = -TARGET_EFAULT;
4019 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
4020 unlock_user(p2, arg3, ret);
4021 unlock_user(p, arg2, 0);
4025 #ifdef TARGET_NR_uselib
4026 case TARGET_NR_uselib:
4029 #ifdef TARGET_NR_swapon
4030 case TARGET_NR_swapon:
4031 if (!(p = lock_user_string(arg1)))
4033 ret = get_errno(swapon(p, arg2));
4034 unlock_user(p, arg1, 0);
4037 case TARGET_NR_reboot:
4039 #ifdef TARGET_NR_readdir
4040 case TARGET_NR_readdir:
4043 #ifdef TARGET_NR_mmap
4044 case TARGET_NR_mmap:
4045 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
4048 abi_ulong v1, v2, v3, v4, v5, v6;
4049 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4057 unlock_user(v, arg1, 0);
4058 ret = get_errno(target_mmap(v1, v2, v3,
4059 target_to_host_bitmask(v4, mmap_flags_tbl),
4063 ret = get_errno(target_mmap(arg1, arg2, arg3,
4064 target_to_host_bitmask(arg4, mmap_flags_tbl),
4070 #ifdef TARGET_NR_mmap2
4071 case TARGET_NR_mmap2:
4072 #if defined(TARGET_SPARC) || defined(TARGET_MIPS)
4073 #define MMAP_SHIFT 12
4075 #define MMAP_SHIFT TARGET_PAGE_BITS
4077 ret = get_errno(target_mmap(arg1, arg2, arg3,
4078 target_to_host_bitmask(arg4, mmap_flags_tbl),
4080 arg6 << MMAP_SHIFT));
4083 case TARGET_NR_munmap:
4084 ret = get_errno(target_munmap(arg1, arg2));
4086 case TARGET_NR_mprotect:
4087 ret = get_errno(target_mprotect(arg1, arg2, arg3));
4089 #ifdef TARGET_NR_mremap
4090 case TARGET_NR_mremap:
4091 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
4094 /* ??? msync/mlock/munlock are broken for softmmu. */
4095 #ifdef TARGET_NR_msync
4096 case TARGET_NR_msync:
4097 ret = get_errno(msync(g2h(arg1), arg2, arg3));
4100 #ifdef TARGET_NR_mlock
4101 case TARGET_NR_mlock:
4102 ret = get_errno(mlock(g2h(arg1), arg2));
4105 #ifdef TARGET_NR_munlock
4106 case TARGET_NR_munlock:
4107 ret = get_errno(munlock(g2h(arg1), arg2));
4110 #ifdef TARGET_NR_mlockall
4111 case TARGET_NR_mlockall:
4112 ret = get_errno(mlockall(arg1));
4115 #ifdef TARGET_NR_munlockall
4116 case TARGET_NR_munlockall:
4117 ret = get_errno(munlockall());
4120 case TARGET_NR_truncate:
4121 if (!(p = lock_user_string(arg1)))
4123 ret = get_errno(truncate(p, arg2));
4124 unlock_user(p, arg1, 0);
4126 case TARGET_NR_ftruncate:
4127 ret = get_errno(ftruncate(arg1, arg2));
4129 case TARGET_NR_fchmod:
4130 ret = get_errno(fchmod(arg1, arg2));
4132 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
4133 case TARGET_NR_fchmodat:
4134 if (!(p = lock_user_string(arg2)))
4136 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
4137 unlock_user(p, arg2, 0);
4140 case TARGET_NR_getpriority:
4141 /* libc does special remapping of the return value of
4142 * sys_getpriority() so it's just easiest to call
4143 * sys_getpriority() directly rather than through libc. */
4144 ret = sys_getpriority(arg1, arg2);
4146 case TARGET_NR_setpriority:
4147 ret = get_errno(setpriority(arg1, arg2, arg3));
4149 #ifdef TARGET_NR_profil
4150 case TARGET_NR_profil:
4153 case TARGET_NR_statfs:
4154 if (!(p = lock_user_string(arg1)))
4156 ret = get_errno(statfs(path(p), &stfs));
4157 unlock_user(p, arg1, 0);
4159 if (!is_error(ret)) {
4160 struct target_statfs *target_stfs;
4162 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
4164 __put_user(stfs.f_type, &target_stfs->f_type);
4165 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4166 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4167 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4168 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4169 __put_user(stfs.f_files, &target_stfs->f_files);
4170 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4171 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4172 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4173 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4174 unlock_user_struct(target_stfs, arg2, 1);
4177 case TARGET_NR_fstatfs:
4178 ret = get_errno(fstatfs(arg1, &stfs));
4179 goto convert_statfs;
4180 #ifdef TARGET_NR_statfs64
4181 case TARGET_NR_statfs64:
4182 if (!(p = lock_user_string(arg1)))
4184 ret = get_errno(statfs(path(p), &stfs));
4185 unlock_user(p, arg1, 0);
4187 if (!is_error(ret)) {
4188 struct target_statfs64 *target_stfs;
4190 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
4192 __put_user(stfs.f_type, &target_stfs->f_type);
4193 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4194 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4195 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4196 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4197 __put_user(stfs.f_files, &target_stfs->f_files);
4198 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4199 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4200 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4201 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4202 unlock_user_struct(target_stfs, arg3, 1);
4205 case TARGET_NR_fstatfs64:
4206 ret = get_errno(fstatfs(arg1, &stfs));
4207 goto convert_statfs64;
4209 #ifdef TARGET_NR_ioperm
4210 case TARGET_NR_ioperm:
4213 #ifdef TARGET_NR_socketcall
4214 case TARGET_NR_socketcall:
4215 ret = do_socketcall(arg1, arg2);
4218 #ifdef TARGET_NR_accept
4219 case TARGET_NR_accept:
4220 ret = do_accept(arg1, arg2, arg3);
4223 #ifdef TARGET_NR_bind
4224 case TARGET_NR_bind:
4225 ret = do_bind(arg1, arg2, arg3);
4228 #ifdef TARGET_NR_connect
4229 case TARGET_NR_connect:
4230 ret = do_connect(arg1, arg2, arg3);
4233 #ifdef TARGET_NR_getpeername
4234 case TARGET_NR_getpeername:
4235 ret = do_getpeername(arg1, arg2, arg3);
4238 #ifdef TARGET_NR_getsockname
4239 case TARGET_NR_getsockname:
4240 ret = do_getsockname(arg1, arg2, arg3);
4243 #ifdef TARGET_NR_getsockopt
4244 case TARGET_NR_getsockopt:
4245 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
4248 #ifdef TARGET_NR_listen
4249 case TARGET_NR_listen:
4250 ret = get_errno(listen(arg1, arg2));
4253 #ifdef TARGET_NR_recv
4254 case TARGET_NR_recv:
4255 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
4258 #ifdef TARGET_NR_recvfrom
4259 case TARGET_NR_recvfrom:
4260 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
4263 #ifdef TARGET_NR_recvmsg
4264 case TARGET_NR_recvmsg:
4265 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
4268 #ifdef TARGET_NR_send
4269 case TARGET_NR_send:
4270 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
4273 #ifdef TARGET_NR_sendmsg
4274 case TARGET_NR_sendmsg:
4275 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
4278 #ifdef TARGET_NR_sendto
4279 case TARGET_NR_sendto:
4280 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
4283 #ifdef TARGET_NR_shutdown
4284 case TARGET_NR_shutdown:
4285 ret = get_errno(shutdown(arg1, arg2));
4288 #ifdef TARGET_NR_socket
4289 case TARGET_NR_socket:
4290 ret = do_socket(arg1, arg2, arg3);
4293 #ifdef TARGET_NR_socketpair
4294 case TARGET_NR_socketpair:
4295 ret = do_socketpair(arg1, arg2, arg3, arg4);
4298 #ifdef TARGET_NR_setsockopt
4299 case TARGET_NR_setsockopt:
4300 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
4304 case TARGET_NR_syslog:
4305 if (!(p = lock_user_string(arg2)))
4307 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
4308 unlock_user(p, arg2, 0);
4311 case TARGET_NR_setitimer:
4313 struct itimerval value, ovalue, *pvalue;
4317 target_to_host_timeval(&pvalue->it_interval,
4319 target_to_host_timeval(&pvalue->it_value,
4320 arg2 + sizeof(struct target_timeval));
4324 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
4325 if (!is_error(ret) && arg3) {
4326 host_to_target_timeval(arg3,
4327 &ovalue.it_interval);
4328 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
4333 case TARGET_NR_getitimer:
4335 struct itimerval value;
4337 ret = get_errno(getitimer(arg1, &value));
4338 if (!is_error(ret) && arg2) {
4339 host_to_target_timeval(arg2,
4340 &value.it_interval);
4341 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
4346 case TARGET_NR_stat:
4347 if (!(p = lock_user_string(arg1)))
4349 ret = get_errno(stat(path(p), &st));
4350 unlock_user(p, arg1, 0);
4352 case TARGET_NR_lstat:
4353 if (!(p = lock_user_string(arg1)))
4355 ret = get_errno(lstat(path(p), &st));
4356 unlock_user(p, arg1, 0);
4358 case TARGET_NR_fstat:
4360 ret = get_errno(fstat(arg1, &st));
4362 if (!is_error(ret)) {
4363 struct target_stat *target_st;
4365 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4367 __put_user(st.st_dev, &target_st->st_dev);
4368 __put_user(st.st_ino, &target_st->st_ino);
4369 __put_user(st.st_mode, &target_st->st_mode);
4370 __put_user(st.st_uid, &target_st->st_uid);
4371 __put_user(st.st_gid, &target_st->st_gid);
4372 __put_user(st.st_nlink, &target_st->st_nlink);
4373 __put_user(st.st_rdev, &target_st->st_rdev);
4374 __put_user(st.st_size, &target_st->st_size);
4375 __put_user(st.st_blksize, &target_st->st_blksize);
4376 __put_user(st.st_blocks, &target_st->st_blocks);
4377 __put_user(st.st_atime, &target_st->target_st_atime);
4378 __put_user(st.st_mtime, &target_st->target_st_mtime);
4379 __put_user(st.st_ctime, &target_st->target_st_ctime);
4380 unlock_user_struct(target_st, arg2, 1);
4384 #ifdef TARGET_NR_olduname
4385 case TARGET_NR_olduname:
4388 #ifdef TARGET_NR_iopl
4389 case TARGET_NR_iopl:
4392 case TARGET_NR_vhangup:
4393 ret = get_errno(vhangup());
4395 #ifdef TARGET_NR_idle
4396 case TARGET_NR_idle:
4399 #ifdef TARGET_NR_syscall
4400 case TARGET_NR_syscall:
4401 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4404 case TARGET_NR_wait4:
4407 abi_long status_ptr = arg2;
4408 struct rusage rusage, *rusage_ptr;
4409 abi_ulong target_rusage = arg4;
4411 rusage_ptr = &rusage;
4414 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4415 if (!is_error(ret)) {
4417 if (put_user_s32(status, status_ptr))
4421 host_to_target_rusage(target_rusage, &rusage);
4425 #ifdef TARGET_NR_swapoff
4426 case TARGET_NR_swapoff:
4427 if (!(p = lock_user_string(arg1)))
4429 ret = get_errno(swapoff(p));
4430 unlock_user(p, arg1, 0);
4433 case TARGET_NR_sysinfo:
4435 struct target_sysinfo *target_value;
4436 struct sysinfo value;
4437 ret = get_errno(sysinfo(&value));
4438 if (!is_error(ret) && arg1)
4440 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4442 __put_user(value.uptime, &target_value->uptime);
4443 __put_user(value.loads[0], &target_value->loads[0]);
4444 __put_user(value.loads[1], &target_value->loads[1]);
4445 __put_user(value.loads[2], &target_value->loads[2]);
4446 __put_user(value.totalram, &target_value->totalram);
4447 __put_user(value.freeram, &target_value->freeram);
4448 __put_user(value.sharedram, &target_value->sharedram);
4449 __put_user(value.bufferram, &target_value->bufferram);
4450 __put_user(value.totalswap, &target_value->totalswap);
4451 __put_user(value.freeswap, &target_value->freeswap);
4452 __put_user(value.procs, &target_value->procs);
4453 __put_user(value.totalhigh, &target_value->totalhigh);
4454 __put_user(value.freehigh, &target_value->freehigh);
4455 __put_user(value.mem_unit, &target_value->mem_unit);
4456 unlock_user_struct(target_value, arg1, 1);
4460 #ifdef TARGET_NR_ipc
4462 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4465 case TARGET_NR_fsync:
4466 ret = get_errno(fsync(arg1));
4468 case TARGET_NR_clone:
4469 ret = get_errno(do_fork(cpu_env, arg1, arg2));
4471 #ifdef __NR_exit_group
4472 /* new thread calls */
4473 case TARGET_NR_exit_group:
4474 gdb_exit(cpu_env, arg1);
4475 ret = get_errno(exit_group(arg1));
4478 case TARGET_NR_setdomainname:
4479 if (!(p = lock_user_string(arg1)))
4481 ret = get_errno(setdomainname(p, arg2));
4482 unlock_user(p, arg1, 0);
4484 case TARGET_NR_uname:
4485 /* no need to transcode because we use the linux syscall */
4487 struct new_utsname * buf;
4489 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4491 ret = get_errno(sys_uname(buf));
4492 if (!is_error(ret)) {
4493 /* Overrite the native machine name with whatever is being
4495 strcpy (buf->machine, UNAME_MACHINE);
4496 /* Allow the user to override the reported release. */
4497 if (qemu_uname_release && *qemu_uname_release)
4498 strcpy (buf->release, qemu_uname_release);
4500 unlock_user_struct(buf, arg1, 1);
4504 case TARGET_NR_modify_ldt:
4505 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
4507 #if !defined(TARGET_X86_64)
4508 case TARGET_NR_vm86old:
4510 case TARGET_NR_vm86:
4511 ret = do_vm86(cpu_env, arg1, arg2);
4515 case TARGET_NR_adjtimex:
4517 #ifdef TARGET_NR_create_module
4518 case TARGET_NR_create_module:
4520 case TARGET_NR_init_module:
4521 case TARGET_NR_delete_module:
4522 #ifdef TARGET_NR_get_kernel_syms
4523 case TARGET_NR_get_kernel_syms:
4526 case TARGET_NR_quotactl:
4528 case TARGET_NR_getpgid:
4529 ret = get_errno(getpgid(arg1));
4531 case TARGET_NR_fchdir:
4532 ret = get_errno(fchdir(arg1));
4534 #ifdef TARGET_NR_bdflush /* not on x86_64 */
4535 case TARGET_NR_bdflush:
4538 #ifdef TARGET_NR_sysfs
4539 case TARGET_NR_sysfs:
4542 case TARGET_NR_personality:
4543 ret = get_errno(personality(arg1));
4545 #ifdef TARGET_NR_afs_syscall
4546 case TARGET_NR_afs_syscall:
4549 #ifdef TARGET_NR__llseek /* Not on alpha */
4550 case TARGET_NR__llseek:
4552 #if defined (__x86_64__)
4553 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
4554 if (put_user_s64(ret, arg4))
4558 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4559 if (put_user_s64(res, arg4))
4565 case TARGET_NR_getdents:
4566 #if TARGET_ABI_BITS != 32
4568 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4570 struct target_dirent *target_dirp;
4571 struct dirent *dirp;
4572 abi_long count = arg3;
4574 dirp = malloc(count);
4576 ret = -TARGET_ENOMEM;
4580 ret = get_errno(sys_getdents(arg1, dirp, count));
4581 if (!is_error(ret)) {
4583 struct target_dirent *tde;
4585 int reclen, treclen;
4586 int count1, tnamelen;
4590 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4594 reclen = de->d_reclen;
4595 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4596 tde->d_reclen = tswap16(treclen);
4597 tde->d_ino = tswapl(de->d_ino);
4598 tde->d_off = tswapl(de->d_off);
4599 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4602 /* XXX: may not be correct */
4603 strncpy(tde->d_name, de->d_name, tnamelen);
4604 de = (struct dirent *)((char *)de + reclen);
4606 tde = (struct target_dirent *)((char *)tde + treclen);
4610 unlock_user(target_dirp, arg2, ret);
4616 struct dirent *dirp;
4617 abi_long count = arg3;
4619 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4621 ret = get_errno(sys_getdents(arg1, dirp, count));
4622 if (!is_error(ret)) {
4628 reclen = de->d_reclen;
4631 de->d_reclen = tswap16(reclen);
4632 tswapls(&de->d_ino);
4633 tswapls(&de->d_off);
4634 de = (struct dirent *)((char *)de + reclen);
4638 unlock_user(dirp, arg2, ret);
4642 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
4643 case TARGET_NR_getdents64:
4645 struct dirent64 *dirp;
4646 abi_long count = arg3;
4647 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4649 ret = get_errno(sys_getdents64(arg1, dirp, count));
4650 if (!is_error(ret)) {
4651 struct dirent64 *de;
4656 reclen = de->d_reclen;
4659 de->d_reclen = tswap16(reclen);
4660 tswap64s((uint64_t *)&de->d_ino);
4661 tswap64s((uint64_t *)&de->d_off);
4662 de = (struct dirent64 *)((char *)de + reclen);
4666 unlock_user(dirp, arg2, ret);
4669 #endif /* TARGET_NR_getdents64 */
4670 #ifdef TARGET_NR__newselect
4671 case TARGET_NR__newselect:
4672 ret = do_select(arg1, arg2, arg3, arg4, arg5);
4675 #ifdef TARGET_NR_poll
4676 case TARGET_NR_poll:
4678 struct target_pollfd *target_pfd;
4679 unsigned int nfds = arg2;
4684 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4687 pfd = alloca(sizeof(struct pollfd) * nfds);
4688 for(i = 0; i < nfds; i++) {
4689 pfd[i].fd = tswap32(target_pfd[i].fd);
4690 pfd[i].events = tswap16(target_pfd[i].events);
4692 ret = get_errno(poll(pfd, nfds, timeout));
4693 if (!is_error(ret)) {
4694 for(i = 0; i < nfds; i++) {
4695 target_pfd[i].revents = tswap16(pfd[i].revents);
4697 ret += nfds * (sizeof(struct target_pollfd)
4698 - sizeof(struct pollfd));
4700 unlock_user(target_pfd, arg1, ret);
4704 case TARGET_NR_flock:
4705 /* NOTE: the flock constant seems to be the same for every
4707 ret = get_errno(flock(arg1, arg2));
4709 case TARGET_NR_readv:
4714 vec = alloca(count * sizeof(struct iovec));
4715 lock_iovec(VERIFY_WRITE, vec, arg2, count, 0);
4716 ret = get_errno(readv(arg1, vec, count));
4717 unlock_iovec(vec, arg2, count, 1);
4720 case TARGET_NR_writev:
4725 vec = alloca(count * sizeof(struct iovec));
4726 lock_iovec(VERIFY_READ, vec, arg2, count, 1);
4727 ret = get_errno(writev(arg1, vec, count));
4728 unlock_iovec(vec, arg2, count, 0);
4731 case TARGET_NR_getsid:
4732 ret = get_errno(getsid(arg1));
4734 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
4735 case TARGET_NR_fdatasync:
4736 ret = get_errno(fdatasync(arg1));
4739 case TARGET_NR__sysctl:
4740 /* We don't implement this, but ENOTDIR is always a safe
4742 ret = -TARGET_ENOTDIR;
4744 case TARGET_NR_sched_setparam:
4746 struct sched_param *target_schp;
4747 struct sched_param schp;
4749 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4751 schp.sched_priority = tswap32(target_schp->sched_priority);
4752 unlock_user_struct(target_schp, arg2, 0);
4753 ret = get_errno(sched_setparam(arg1, &schp));
4756 case TARGET_NR_sched_getparam:
4758 struct sched_param *target_schp;
4759 struct sched_param schp;
4760 ret = get_errno(sched_getparam(arg1, &schp));
4761 if (!is_error(ret)) {
4762 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4764 target_schp->sched_priority = tswap32(schp.sched_priority);
4765 unlock_user_struct(target_schp, arg2, 1);
4769 case TARGET_NR_sched_setscheduler:
4771 struct sched_param *target_schp;
4772 struct sched_param schp;
4773 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4775 schp.sched_priority = tswap32(target_schp->sched_priority);
4776 unlock_user_struct(target_schp, arg3, 0);
4777 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4780 case TARGET_NR_sched_getscheduler:
4781 ret = get_errno(sched_getscheduler(arg1));
4783 case TARGET_NR_sched_yield:
4784 ret = get_errno(sched_yield());
4786 case TARGET_NR_sched_get_priority_max:
4787 ret = get_errno(sched_get_priority_max(arg1));
4789 case TARGET_NR_sched_get_priority_min:
4790 ret = get_errno(sched_get_priority_min(arg1));
4792 case TARGET_NR_sched_rr_get_interval:
4795 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4796 if (!is_error(ret)) {
4797 host_to_target_timespec(arg2, &ts);
4801 case TARGET_NR_nanosleep:
4803 struct timespec req, rem;
4804 target_to_host_timespec(&req, arg1);
4805 ret = get_errno(nanosleep(&req, &rem));
4806 if (is_error(ret) && arg2) {
4807 host_to_target_timespec(arg2, &rem);
4811 #ifdef TARGET_NR_query_module
4812 case TARGET_NR_query_module:
4815 #ifdef TARGET_NR_nfsservctl
4816 case TARGET_NR_nfsservctl:
4819 case TARGET_NR_prctl:
4822 case PR_GET_PDEATHSIG:
4825 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4826 if (!is_error(ret) && arg2
4827 && put_user_ual(deathsig, arg2))
4832 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4836 #ifdef TARGET_NR_arch_prctl
4837 case TARGET_NR_arch_prctl:
4838 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
4839 ret = do_arch_prctl(cpu_env, arg1, arg2);
4845 #ifdef TARGET_NR_pread
4846 case TARGET_NR_pread:
4847 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4849 ret = get_errno(pread(arg1, p, arg3, arg4));
4850 unlock_user(p, arg2, ret);
4852 case TARGET_NR_pwrite:
4853 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4855 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4856 unlock_user(p, arg2, 0);
4859 case TARGET_NR_getcwd:
4860 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4862 ret = get_errno(sys_getcwd1(p, arg2));
4863 unlock_user(p, arg1, ret);
4865 case TARGET_NR_capget:
4867 case TARGET_NR_capset:
4869 case TARGET_NR_sigaltstack:
4870 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4871 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
4872 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
4877 case TARGET_NR_sendfile:
4879 #ifdef TARGET_NR_getpmsg
4880 case TARGET_NR_getpmsg:
4883 #ifdef TARGET_NR_putpmsg
4884 case TARGET_NR_putpmsg:
4887 #ifdef TARGET_NR_vfork
4888 case TARGET_NR_vfork:
4889 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
4892 #ifdef TARGET_NR_ugetrlimit
4893 case TARGET_NR_ugetrlimit:
4896 ret = get_errno(getrlimit(arg1, &rlim));
4897 if (!is_error(ret)) {
4898 struct target_rlimit *target_rlim;
4899 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4901 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4902 target_rlim->rlim_max = tswapl(rlim.rlim_max);
4903 unlock_user_struct(target_rlim, arg2, 1);
4908 #ifdef TARGET_NR_truncate64
4909 case TARGET_NR_truncate64:
4910 if (!(p = lock_user_string(arg1)))
4912 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4913 unlock_user(p, arg1, 0);
4916 #ifdef TARGET_NR_ftruncate64
4917 case TARGET_NR_ftruncate64:
4918 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
4921 #ifdef TARGET_NR_stat64
4922 case TARGET_NR_stat64:
4923 if (!(p = lock_user_string(arg1)))
4925 ret = get_errno(stat(path(p), &st));
4926 unlock_user(p, arg1, 0);
4929 #ifdef TARGET_NR_lstat64
4930 case TARGET_NR_lstat64:
4931 if (!(p = lock_user_string(arg1)))
4933 ret = get_errno(lstat(path(p), &st));
4934 unlock_user(p, arg1, 0);
4937 #ifdef TARGET_NR_fstat64
4938 case TARGET_NR_fstat64:
4940 ret = get_errno(fstat(arg1, &st));
4942 if (!is_error(ret)) {
4944 if (((CPUARMState *)cpu_env)->eabi) {
4945 struct target_eabi_stat64 *target_st;
4947 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4949 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4950 __put_user(st.st_dev, &target_st->st_dev);
4951 __put_user(st.st_ino, &target_st->st_ino);
4952 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4953 __put_user(st.st_ino, &target_st->__st_ino);
4955 __put_user(st.st_mode, &target_st->st_mode);
4956 __put_user(st.st_nlink, &target_st->st_nlink);
4957 __put_user(st.st_uid, &target_st->st_uid);
4958 __put_user(st.st_gid, &target_st->st_gid);
4959 __put_user(st.st_rdev, &target_st->st_rdev);
4960 __put_user(st.st_size, &target_st->st_size);
4961 __put_user(st.st_blksize, &target_st->st_blksize);
4962 __put_user(st.st_blocks, &target_st->st_blocks);
4963 __put_user(st.st_atime, &target_st->target_st_atime);
4964 __put_user(st.st_mtime, &target_st->target_st_mtime);
4965 __put_user(st.st_ctime, &target_st->target_st_ctime);
4966 unlock_user_struct(target_st, arg2, 1);
4970 struct target_stat64 *target_st;
4972 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4974 memset(target_st, 0, sizeof(struct target_stat64));
4975 __put_user(st.st_dev, &target_st->st_dev);
4976 __put_user(st.st_ino, &target_st->st_ino);
4977 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4978 __put_user(st.st_ino, &target_st->__st_ino);
4980 __put_user(st.st_mode, &target_st->st_mode);
4981 __put_user(st.st_nlink, &target_st->st_nlink);
4982 __put_user(st.st_uid, &target_st->st_uid);
4983 __put_user(st.st_gid, &target_st->st_gid);
4984 __put_user(st.st_rdev, &target_st->st_rdev);
4985 /* XXX: better use of kernel struct */
4986 __put_user(st.st_size, &target_st->st_size);
4987 __put_user(st.st_blksize, &target_st->st_blksize);
4988 __put_user(st.st_blocks, &target_st->st_blocks);
4989 __put_user(st.st_atime, &target_st->target_st_atime);
4990 __put_user(st.st_mtime, &target_st->target_st_mtime);
4991 __put_user(st.st_ctime, &target_st->target_st_ctime);
4992 unlock_user_struct(target_st, arg2, 1);
4999 case TARGET_NR_lchown:
5000 if (!(p = lock_user_string(arg1)))
5002 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5003 unlock_user(p, arg1, 0);
5005 case TARGET_NR_getuid:
5006 ret = get_errno(high2lowuid(getuid()));
5008 case TARGET_NR_getgid:
5009 ret = get_errno(high2lowgid(getgid()));
5011 case TARGET_NR_geteuid:
5012 ret = get_errno(high2lowuid(geteuid()));
5014 case TARGET_NR_getegid:
5015 ret = get_errno(high2lowgid(getegid()));
5017 case TARGET_NR_setreuid:
5018 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5020 case TARGET_NR_setregid:
5021 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5023 case TARGET_NR_getgroups:
5025 int gidsetsize = arg1;
5026 uint16_t *target_grouplist;
5030 grouplist = alloca(gidsetsize * sizeof(gid_t));
5031 ret = get_errno(getgroups(gidsetsize, grouplist));
5032 if (!is_error(ret)) {
5033 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5034 if (!target_grouplist)
5036 for(i = 0;i < gidsetsize; i++)
5037 target_grouplist[i] = tswap16(grouplist[i]);
5038 unlock_user(target_grouplist, arg2, gidsetsize * 2);
5042 case TARGET_NR_setgroups:
5044 int gidsetsize = arg1;
5045 uint16_t *target_grouplist;
5049 grouplist = alloca(gidsetsize * sizeof(gid_t));
5050 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5051 if (!target_grouplist) {
5052 ret = -TARGET_EFAULT;
5055 for(i = 0;i < gidsetsize; i++)
5056 grouplist[i] = tswap16(target_grouplist[i]);
5057 unlock_user(target_grouplist, arg2, 0);
5058 ret = get_errno(setgroups(gidsetsize, grouplist));
5061 case TARGET_NR_fchown:
5062 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5064 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5065 case TARGET_NR_fchownat:
5066 if (!(p = lock_user_string(arg2)))
5068 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5069 unlock_user(p, arg2, 0);
5072 #ifdef TARGET_NR_setresuid
5073 case TARGET_NR_setresuid:
5074 ret = get_errno(setresuid(low2highuid(arg1),
5076 low2highuid(arg3)));
5079 #ifdef TARGET_NR_getresuid
5080 case TARGET_NR_getresuid:
5082 uid_t ruid, euid, suid;
5083 ret = get_errno(getresuid(&ruid, &euid, &suid));
5084 if (!is_error(ret)) {
5085 if (put_user_u16(high2lowuid(ruid), arg1)
5086 || put_user_u16(high2lowuid(euid), arg2)
5087 || put_user_u16(high2lowuid(suid), arg3))
5093 #ifdef TARGET_NR_getresgid
5094 case TARGET_NR_setresgid:
5095 ret = get_errno(setresgid(low2highgid(arg1),
5097 low2highgid(arg3)));
5100 #ifdef TARGET_NR_getresgid
5101 case TARGET_NR_getresgid:
5103 gid_t rgid, egid, sgid;
5104 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5105 if (!is_error(ret)) {
5106 if (put_user_u16(high2lowgid(rgid), arg1)
5107 || put_user_u16(high2lowgid(egid), arg2)
5108 || put_user_u16(high2lowgid(sgid), arg3))
5114 case TARGET_NR_chown:
5115 if (!(p = lock_user_string(arg1)))
5117 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5118 unlock_user(p, arg1, 0);
5120 case TARGET_NR_setuid:
5121 ret = get_errno(setuid(low2highuid(arg1)));
5123 case TARGET_NR_setgid:
5124 ret = get_errno(setgid(low2highgid(arg1)));
5126 case TARGET_NR_setfsuid:
5127 ret = get_errno(setfsuid(arg1));
5129 case TARGET_NR_setfsgid:
5130 ret = get_errno(setfsgid(arg1));
5132 #endif /* USE_UID16 */
5134 #ifdef TARGET_NR_lchown32
5135 case TARGET_NR_lchown32:
5136 if (!(p = lock_user_string(arg1)))
5138 ret = get_errno(lchown(p, arg2, arg3));
5139 unlock_user(p, arg1, 0);
5142 #ifdef TARGET_NR_getuid32
5143 case TARGET_NR_getuid32:
5144 ret = get_errno(getuid());
5147 #ifdef TARGET_NR_getgid32
5148 case TARGET_NR_getgid32:
5149 ret = get_errno(getgid());
5152 #ifdef TARGET_NR_geteuid32
5153 case TARGET_NR_geteuid32:
5154 ret = get_errno(geteuid());
5157 #ifdef TARGET_NR_getegid32
5158 case TARGET_NR_getegid32:
5159 ret = get_errno(getegid());
5162 #ifdef TARGET_NR_setreuid32
5163 case TARGET_NR_setreuid32:
5164 ret = get_errno(setreuid(arg1, arg2));
5167 #ifdef TARGET_NR_setregid32
5168 case TARGET_NR_setregid32:
5169 ret = get_errno(setregid(arg1, arg2));
5172 #ifdef TARGET_NR_getgroups32
5173 case TARGET_NR_getgroups32:
5175 int gidsetsize = arg1;
5176 uint32_t *target_grouplist;
5180 grouplist = alloca(gidsetsize * sizeof(gid_t));
5181 ret = get_errno(getgroups(gidsetsize, grouplist));
5182 if (!is_error(ret)) {
5183 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
5184 if (!target_grouplist) {
5185 ret = -TARGET_EFAULT;
5188 for(i = 0;i < gidsetsize; i++)
5189 target_grouplist[i] = tswap32(grouplist[i]);
5190 unlock_user(target_grouplist, arg2, gidsetsize * 4);
5195 #ifdef TARGET_NR_setgroups32
5196 case TARGET_NR_setgroups32:
5198 int gidsetsize = arg1;
5199 uint32_t *target_grouplist;
5203 grouplist = alloca(gidsetsize * sizeof(gid_t));
5204 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
5205 if (!target_grouplist) {
5206 ret = -TARGET_EFAULT;
5209 for(i = 0;i < gidsetsize; i++)
5210 grouplist[i] = tswap32(target_grouplist[i]);
5211 unlock_user(target_grouplist, arg2, 0);
5212 ret = get_errno(setgroups(gidsetsize, grouplist));
5216 #ifdef TARGET_NR_fchown32
5217 case TARGET_NR_fchown32:
5218 ret = get_errno(fchown(arg1, arg2, arg3));
5221 #ifdef TARGET_NR_setresuid32
5222 case TARGET_NR_setresuid32:
5223 ret = get_errno(setresuid(arg1, arg2, arg3));
5226 #ifdef TARGET_NR_getresuid32
5227 case TARGET_NR_getresuid32:
5229 uid_t ruid, euid, suid;
5230 ret = get_errno(getresuid(&ruid, &euid, &suid));
5231 if (!is_error(ret)) {
5232 if (put_user_u32(ruid, arg1)
5233 || put_user_u32(euid, arg2)
5234 || put_user_u32(suid, arg3))
5240 #ifdef TARGET_NR_setresgid32
5241 case TARGET_NR_setresgid32:
5242 ret = get_errno(setresgid(arg1, arg2, arg3));
5245 #ifdef TARGET_NR_getresgid32
5246 case TARGET_NR_getresgid32:
5248 gid_t rgid, egid, sgid;
5249 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5250 if (!is_error(ret)) {
5251 if (put_user_u32(rgid, arg1)
5252 || put_user_u32(egid, arg2)
5253 || put_user_u32(sgid, arg3))
5259 #ifdef TARGET_NR_chown32
5260 case TARGET_NR_chown32:
5261 if (!(p = lock_user_string(arg1)))
5263 ret = get_errno(chown(p, arg2, arg3));
5264 unlock_user(p, arg1, 0);
5267 #ifdef TARGET_NR_setuid32
5268 case TARGET_NR_setuid32:
5269 ret = get_errno(setuid(arg1));
5272 #ifdef TARGET_NR_setgid32
5273 case TARGET_NR_setgid32:
5274 ret = get_errno(setgid(arg1));
5277 #ifdef TARGET_NR_setfsuid32
5278 case TARGET_NR_setfsuid32:
5279 ret = get_errno(setfsuid(arg1));
5282 #ifdef TARGET_NR_setfsgid32
5283 case TARGET_NR_setfsgid32:
5284 ret = get_errno(setfsgid(arg1));
5288 case TARGET_NR_pivot_root:
5290 #ifdef TARGET_NR_mincore
5291 case TARGET_NR_mincore:
5294 #ifdef TARGET_NR_madvise
5295 case TARGET_NR_madvise:
5296 /* A straight passthrough may not be safe because qemu sometimes
5297 turns private flie-backed mappings into anonymous mappings.
5298 This will break MADV_DONTNEED.
5299 This is a hint, so ignoring and returning success is ok. */
5303 #if TARGET_ABI_BITS == 32
5304 case TARGET_NR_fcntl64:
5308 struct target_flock64 *target_fl;
5310 struct target_eabi_flock64 *target_efl;
5314 case TARGET_F_GETLK64:
5317 case TARGET_F_SETLK64:
5320 case TARGET_F_SETLKW64:
5329 case TARGET_F_GETLK64:
5331 if (((CPUARMState *)cpu_env)->eabi) {
5332 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5334 fl.l_type = tswap16(target_efl->l_type);
5335 fl.l_whence = tswap16(target_efl->l_whence);
5336 fl.l_start = tswap64(target_efl->l_start);
5337 fl.l_len = tswap64(target_efl->l_len);
5338 fl.l_pid = tswapl(target_efl->l_pid);
5339 unlock_user_struct(target_efl, arg3, 0);
5343 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5345 fl.l_type = tswap16(target_fl->l_type);
5346 fl.l_whence = tswap16(target_fl->l_whence);
5347 fl.l_start = tswap64(target_fl->l_start);
5348 fl.l_len = tswap64(target_fl->l_len);
5349 fl.l_pid = tswapl(target_fl->l_pid);
5350 unlock_user_struct(target_fl, arg3, 0);
5352 ret = get_errno(fcntl(arg1, cmd, &fl));
5355 if (((CPUARMState *)cpu_env)->eabi) {
5356 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5358 target_efl->l_type = tswap16(fl.l_type);
5359 target_efl->l_whence = tswap16(fl.l_whence);
5360 target_efl->l_start = tswap64(fl.l_start);
5361 target_efl->l_len = tswap64(fl.l_len);
5362 target_efl->l_pid = tswapl(fl.l_pid);
5363 unlock_user_struct(target_efl, arg3, 1);
5367 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5369 target_fl->l_type = tswap16(fl.l_type);
5370 target_fl->l_whence = tswap16(fl.l_whence);
5371 target_fl->l_start = tswap64(fl.l_start);
5372 target_fl->l_len = tswap64(fl.l_len);
5373 target_fl->l_pid = tswapl(fl.l_pid);
5374 unlock_user_struct(target_fl, arg3, 1);
5379 case TARGET_F_SETLK64:
5380 case TARGET_F_SETLKW64:
5382 if (((CPUARMState *)cpu_env)->eabi) {
5383 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5385 fl.l_type = tswap16(target_efl->l_type);
5386 fl.l_whence = tswap16(target_efl->l_whence);
5387 fl.l_start = tswap64(target_efl->l_start);
5388 fl.l_len = tswap64(target_efl->l_len);
5389 fl.l_pid = tswapl(target_efl->l_pid);
5390 unlock_user_struct(target_efl, arg3, 0);
5394 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5396 fl.l_type = tswap16(target_fl->l_type);
5397 fl.l_whence = tswap16(target_fl->l_whence);
5398 fl.l_start = tswap64(target_fl->l_start);
5399 fl.l_len = tswap64(target_fl->l_len);
5400 fl.l_pid = tswapl(target_fl->l_pid);
5401 unlock_user_struct(target_fl, arg3, 0);
5403 ret = get_errno(fcntl(arg1, cmd, &fl));
5406 ret = do_fcntl(arg1, cmd, arg3);
5412 #ifdef TARGET_NR_cacheflush
5413 case TARGET_NR_cacheflush:
5414 /* self-modifying code is handled automatically, so nothing needed */
5418 #ifdef TARGET_NR_security
5419 case TARGET_NR_security:
5422 #ifdef TARGET_NR_getpagesize
5423 case TARGET_NR_getpagesize:
5424 ret = TARGET_PAGE_SIZE;
5427 case TARGET_NR_gettid:
5428 ret = get_errno(gettid());
5430 #ifdef TARGET_NR_readahead
5431 case TARGET_NR_readahead:
5434 #ifdef TARGET_NR_setxattr
5435 case TARGET_NR_setxattr:
5436 case TARGET_NR_lsetxattr:
5437 case TARGET_NR_fsetxattr:
5438 case TARGET_NR_getxattr:
5439 case TARGET_NR_lgetxattr:
5440 case TARGET_NR_fgetxattr:
5441 case TARGET_NR_listxattr:
5442 case TARGET_NR_llistxattr:
5443 case TARGET_NR_flistxattr:
5444 case TARGET_NR_removexattr:
5445 case TARGET_NR_lremovexattr:
5446 case TARGET_NR_fremovexattr:
5447 goto unimplemented_nowarn;
5449 #ifdef TARGET_NR_set_thread_area
5450 case TARGET_NR_set_thread_area:
5451 #if defined(TARGET_MIPS)
5452 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5455 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
5456 ret = do_set_thread_area(cpu_env, arg1);
5459 goto unimplemented_nowarn;
5462 #ifdef TARGET_NR_get_thread_area
5463 case TARGET_NR_get_thread_area:
5464 #if defined(TARGET_I386) && defined(TARGET_ABI32)
5465 ret = do_get_thread_area(cpu_env, arg1);
5467 goto unimplemented_nowarn;
5470 #ifdef TARGET_NR_getdomainname
5471 case TARGET_NR_getdomainname:
5472 goto unimplemented_nowarn;
5475 #ifdef TARGET_NR_clock_gettime
5476 case TARGET_NR_clock_gettime:
5479 ret = get_errno(clock_gettime(arg1, &ts));
5480 if (!is_error(ret)) {
5481 host_to_target_timespec(arg2, &ts);
5486 #ifdef TARGET_NR_clock_getres
5487 case TARGET_NR_clock_getres:
5490 ret = get_errno(clock_getres(arg1, &ts));
5491 if (!is_error(ret)) {
5492 host_to_target_timespec(arg2, &ts);
5498 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5499 case TARGET_NR_set_tid_address:
5500 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5504 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
5505 case TARGET_NR_tkill:
5506 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5510 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
5511 case TARGET_NR_tgkill:
5512 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5516 #ifdef TARGET_NR_set_robust_list
5517 case TARGET_NR_set_robust_list:
5518 goto unimplemented_nowarn;
5521 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5522 case TARGET_NR_utimensat:
5524 struct timespec ts[2];
5525 target_to_host_timespec(ts, arg3);
5526 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5528 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5530 if (!(p = lock_user_string(arg2))) {
5531 ret = -TARGET_EFAULT;
5534 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5535 unlock_user(p, arg2, 0);
5543 gemu_log("qemu: Unsupported syscall: %d\n", num);
5544 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5545 unimplemented_nowarn:
5547 ret = -TARGET_ENOSYS;
5552 gemu_log(" = %ld\n", ret);
5555 print_syscall_ret(num, ret);
5558 ret = -TARGET_EFAULT;