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 #include <sys/futex.h>
59 #define termios host_termios
60 #define winsize host_winsize
61 #define termio host_termio
62 #define sgttyb host_sgttyb /* same as target */
63 #define tchars host_tchars /* same as target */
64 #define ltchars host_ltchars /* same as target */
66 #include <linux/termios.h>
67 #include <linux/unistd.h>
68 #include <linux/utsname.h>
69 #include <linux/cdrom.h>
70 #include <linux/hdreg.h>
71 #include <linux/soundcard.h>
72 #include <linux/dirent.h>
74 #include "linux_loop.h"
80 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
81 || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
82 /* 16 bit uid wrappers emulation */
86 //#include <linux/msdos_fs.h>
87 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
88 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
99 #define _syscall0(type,name) \
102 return syscall(__NR_##name); \
105 #define _syscall1(type,name,type1,arg1) \
106 type name (type1 arg1) \
108 return syscall(__NR_##name, arg1); \
111 #define _syscall2(type,name,type1,arg1,type2,arg2) \
112 type name (type1 arg1,type2 arg2) \
114 return syscall(__NR_##name, arg1, arg2); \
117 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
118 type name (type1 arg1,type2 arg2,type3 arg3) \
120 return syscall(__NR_##name, arg1, arg2, arg3); \
123 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
124 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
126 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
129 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
131 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
133 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
137 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
138 type5,arg5,type6,arg6) \
139 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
141 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
145 #define __NR_sys_uname __NR_uname
146 #define __NR_sys_faccessat __NR_faccessat
147 #define __NR_sys_fchmodat __NR_fchmodat
148 #define __NR_sys_fchownat __NR_fchownat
149 #define __NR_sys_getcwd1 __NR_getcwd
150 #define __NR_sys_getdents __NR_getdents
151 #define __NR_sys_getdents64 __NR_getdents64
152 #define __NR_sys_getpriority __NR_getpriority
153 #define __NR_sys_linkat __NR_linkat
154 #define __NR_sys_mkdirat __NR_mkdirat
155 #define __NR_sys_mknodat __NR_mknodat
156 #define __NR_sys_openat __NR_openat
157 #define __NR_sys_readlinkat __NR_readlinkat
158 #define __NR_sys_renameat __NR_renameat
159 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
160 #define __NR_sys_symlinkat __NR_symlinkat
161 #define __NR_sys_syslog __NR_syslog
162 #define __NR_sys_tgkill __NR_tgkill
163 #define __NR_sys_tkill __NR_tkill
164 #define __NR_sys_unlinkat __NR_unlinkat
165 #define __NR_sys_utimensat __NR_utimensat
166 #define __NR_sys_futex __NR_futex
168 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
169 #define __NR__llseek __NR_lseek
173 _syscall0(int, gettid)
175 /* This is a replacement for the host gettid() and must return a host
177 static int gettid(void) {
181 _syscall1(int,sys_uname,struct new_utsname *,buf)
182 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
183 _syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
185 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
186 _syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
187 mode_t,mode,int,flags)
189 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
190 _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
191 uid_t,owner,gid_t,group,int,flags)
193 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
194 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
195 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
196 _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
198 _syscall2(int, sys_getpriority, int, which, int, who);
199 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
200 loff_t *, res, uint, wh);
201 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
202 _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
203 int,newdirfd,const char *,newpath,int,flags)
205 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
206 _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
208 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
209 _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
210 mode_t,mode,dev_t,dev)
212 #if defined(TARGET_NR_openat) && defined(__NR_openat)
213 _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
215 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
216 _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
217 char *,buf,size_t,bufsize)
219 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
220 _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
221 int,newdirfd,const char *,newpath)
223 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
224 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
225 _syscall3(int,sys_symlinkat,const char *,oldpath,
226 int,newdirfd,const char *,newpath)
228 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
229 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
230 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
232 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
233 _syscall2(int,sys_tkill,int,tid,int,sig)
235 #ifdef __NR_exit_group
236 _syscall1(int,exit_group,int,error_code)
238 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
239 _syscall1(int,set_tid_address,int *,tidptr)
241 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
242 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
244 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
245 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
246 const struct timespec *,tsp,int,flags)
248 #if defined(TARGET_NR_futex) && defined(__NR_futex)
249 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
250 const struct timespec *,timeout,int *,uaddr2,int,val3)
254 extern int personality(int);
255 extern int flock(int, int);
256 extern int setfsuid(int);
257 extern int setfsgid(int);
258 extern int setresuid(uid_t, uid_t, uid_t);
259 extern int getresuid(uid_t *, uid_t *, uid_t *);
260 extern int setresgid(gid_t, gid_t, gid_t);
261 extern int getresgid(gid_t *, gid_t *, gid_t *);
262 extern int setgroups(int, gid_t *);
264 #define ERRNO_TABLE_SIZE 1200
266 /* target_to_host_errno_table[] is initialized from
267 * host_to_target_errno_table[] in syscall_init(). */
268 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
272 * This list is the union of errno values overridden in asm-<arch>/errno.h
273 * minus the errnos that are not actually generic to all archs.
275 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
276 [EIDRM] = TARGET_EIDRM,
277 [ECHRNG] = TARGET_ECHRNG,
278 [EL2NSYNC] = TARGET_EL2NSYNC,
279 [EL3HLT] = TARGET_EL3HLT,
280 [EL3RST] = TARGET_EL3RST,
281 [ELNRNG] = TARGET_ELNRNG,
282 [EUNATCH] = TARGET_EUNATCH,
283 [ENOCSI] = TARGET_ENOCSI,
284 [EL2HLT] = TARGET_EL2HLT,
285 [EDEADLK] = TARGET_EDEADLK,
286 [ENOLCK] = TARGET_ENOLCK,
287 [EBADE] = TARGET_EBADE,
288 [EBADR] = TARGET_EBADR,
289 [EXFULL] = TARGET_EXFULL,
290 [ENOANO] = TARGET_ENOANO,
291 [EBADRQC] = TARGET_EBADRQC,
292 [EBADSLT] = TARGET_EBADSLT,
293 [EBFONT] = TARGET_EBFONT,
294 [ENOSTR] = TARGET_ENOSTR,
295 [ENODATA] = TARGET_ENODATA,
296 [ETIME] = TARGET_ETIME,
297 [ENOSR] = TARGET_ENOSR,
298 [ENONET] = TARGET_ENONET,
299 [ENOPKG] = TARGET_ENOPKG,
300 [EREMOTE] = TARGET_EREMOTE,
301 [ENOLINK] = TARGET_ENOLINK,
302 [EADV] = TARGET_EADV,
303 [ESRMNT] = TARGET_ESRMNT,
304 [ECOMM] = TARGET_ECOMM,
305 [EPROTO] = TARGET_EPROTO,
306 [EDOTDOT] = TARGET_EDOTDOT,
307 [EMULTIHOP] = TARGET_EMULTIHOP,
308 [EBADMSG] = TARGET_EBADMSG,
309 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
310 [EOVERFLOW] = TARGET_EOVERFLOW,
311 [ENOTUNIQ] = TARGET_ENOTUNIQ,
312 [EBADFD] = TARGET_EBADFD,
313 [EREMCHG] = TARGET_EREMCHG,
314 [ELIBACC] = TARGET_ELIBACC,
315 [ELIBBAD] = TARGET_ELIBBAD,
316 [ELIBSCN] = TARGET_ELIBSCN,
317 [ELIBMAX] = TARGET_ELIBMAX,
318 [ELIBEXEC] = TARGET_ELIBEXEC,
319 [EILSEQ] = TARGET_EILSEQ,
320 [ENOSYS] = TARGET_ENOSYS,
321 [ELOOP] = TARGET_ELOOP,
322 [ERESTART] = TARGET_ERESTART,
323 [ESTRPIPE] = TARGET_ESTRPIPE,
324 [ENOTEMPTY] = TARGET_ENOTEMPTY,
325 [EUSERS] = TARGET_EUSERS,
326 [ENOTSOCK] = TARGET_ENOTSOCK,
327 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
328 [EMSGSIZE] = TARGET_EMSGSIZE,
329 [EPROTOTYPE] = TARGET_EPROTOTYPE,
330 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
331 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
332 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
333 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
334 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
335 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
336 [EADDRINUSE] = TARGET_EADDRINUSE,
337 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
338 [ENETDOWN] = TARGET_ENETDOWN,
339 [ENETUNREACH] = TARGET_ENETUNREACH,
340 [ENETRESET] = TARGET_ENETRESET,
341 [ECONNABORTED] = TARGET_ECONNABORTED,
342 [ECONNRESET] = TARGET_ECONNRESET,
343 [ENOBUFS] = TARGET_ENOBUFS,
344 [EISCONN] = TARGET_EISCONN,
345 [ENOTCONN] = TARGET_ENOTCONN,
346 [EUCLEAN] = TARGET_EUCLEAN,
347 [ENOTNAM] = TARGET_ENOTNAM,
348 [ENAVAIL] = TARGET_ENAVAIL,
349 [EISNAM] = TARGET_EISNAM,
350 [EREMOTEIO] = TARGET_EREMOTEIO,
351 [ESHUTDOWN] = TARGET_ESHUTDOWN,
352 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
353 [ETIMEDOUT] = TARGET_ETIMEDOUT,
354 [ECONNREFUSED] = TARGET_ECONNREFUSED,
355 [EHOSTDOWN] = TARGET_EHOSTDOWN,
356 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
357 [EALREADY] = TARGET_EALREADY,
358 [EINPROGRESS] = TARGET_EINPROGRESS,
359 [ESTALE] = TARGET_ESTALE,
360 [ECANCELED] = TARGET_ECANCELED,
361 [ENOMEDIUM] = TARGET_ENOMEDIUM,
362 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
364 [ENOKEY] = TARGET_ENOKEY,
367 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
370 [EKEYREVOKED] = TARGET_EKEYREVOKED,
373 [EKEYREJECTED] = TARGET_EKEYREJECTED,
376 [EOWNERDEAD] = TARGET_EOWNERDEAD,
378 #ifdef ENOTRECOVERABLE
379 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
383 static inline int host_to_target_errno(int err)
385 if(host_to_target_errno_table[err])
386 return host_to_target_errno_table[err];
390 static inline int target_to_host_errno(int err)
392 if (target_to_host_errno_table[err])
393 return target_to_host_errno_table[err];
397 static inline abi_long get_errno(abi_long ret)
400 return -host_to_target_errno(errno);
405 static inline int is_error(abi_long ret)
407 return (abi_ulong)ret >= (abi_ulong)(-4096);
410 char *target_strerror(int err)
412 return strerror(target_to_host_errno(err));
415 static abi_ulong target_brk;
416 static abi_ulong target_original_brk;
418 void target_set_brk(abi_ulong new_brk)
420 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
423 /* do_brk() must return target values and target errnos. */
424 abi_long do_brk(abi_ulong new_brk)
427 abi_long mapped_addr;
432 if (new_brk < target_original_brk)
435 brk_page = HOST_PAGE_ALIGN(target_brk);
437 /* If the new brk is less than this, set it and we're done... */
438 if (new_brk < brk_page) {
439 target_brk = new_brk;
443 /* We need to allocate more memory after the brk... */
444 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
445 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
446 PROT_READ|PROT_WRITE,
447 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
449 if (!is_error(mapped_addr))
450 target_brk = new_brk;
455 static inline abi_long copy_from_user_fdset(fd_set *fds,
456 abi_ulong target_fds_addr,
460 abi_ulong b, *target_fds;
462 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
463 if (!(target_fds = lock_user(VERIFY_READ,
465 sizeof(abi_ulong) * nw,
467 return -TARGET_EFAULT;
471 for (i = 0; i < nw; i++) {
472 /* grab the abi_ulong */
473 __get_user(b, &target_fds[i]);
474 for (j = 0; j < TARGET_ABI_BITS; j++) {
475 /* check the bit inside the abi_ulong */
482 unlock_user(target_fds, target_fds_addr, 0);
487 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
493 abi_ulong *target_fds;
495 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
496 if (!(target_fds = lock_user(VERIFY_WRITE,
498 sizeof(abi_ulong) * nw,
500 return -TARGET_EFAULT;
503 for (i = 0; i < nw; i++) {
505 for (j = 0; j < TARGET_ABI_BITS; j++) {
506 v |= ((FD_ISSET(k, fds) != 0) << j);
509 __put_user(v, &target_fds[i]);
512 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
517 #if defined(__alpha__)
523 static inline abi_long host_to_target_clock_t(long ticks)
525 #if HOST_HZ == TARGET_HZ
528 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
532 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
533 const struct rusage *rusage)
535 struct target_rusage *target_rusage;
537 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
538 return -TARGET_EFAULT;
539 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
540 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
541 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
542 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
543 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
544 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
545 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
546 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
547 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
548 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
549 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
550 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
551 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
552 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
553 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
554 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
555 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
556 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
557 unlock_user_struct(target_rusage, target_addr, 1);
562 static inline abi_long copy_from_user_timeval(struct timeval *tv,
563 abi_ulong target_tv_addr)
565 struct target_timeval *target_tv;
567 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
568 return -TARGET_EFAULT;
570 __get_user(tv->tv_sec, &target_tv->tv_sec);
571 __get_user(tv->tv_usec, &target_tv->tv_usec);
573 unlock_user_struct(target_tv, target_tv_addr, 0);
578 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
579 const struct timeval *tv)
581 struct target_timeval *target_tv;
583 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
584 return -TARGET_EFAULT;
586 __put_user(tv->tv_sec, &target_tv->tv_sec);
587 __put_user(tv->tv_usec, &target_tv->tv_usec);
589 unlock_user_struct(target_tv, target_tv_addr, 1);
595 /* do_select() must return target values and target errnos. */
596 static abi_long do_select(int n,
597 abi_ulong rfd_addr, abi_ulong wfd_addr,
598 abi_ulong efd_addr, abi_ulong target_tv_addr)
600 fd_set rfds, wfds, efds;
601 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
602 struct timeval tv, *tv_ptr;
606 if (copy_from_user_fdset(&rfds, rfd_addr, n))
607 return -TARGET_EFAULT;
613 if (copy_from_user_fdset(&wfds, wfd_addr, n))
614 return -TARGET_EFAULT;
620 if (copy_from_user_fdset(&efds, efd_addr, n))
621 return -TARGET_EFAULT;
627 if (target_tv_addr) {
628 if (copy_from_user_timeval(&tv, target_tv_addr))
629 return -TARGET_EFAULT;
635 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
637 if (!is_error(ret)) {
638 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
639 return -TARGET_EFAULT;
640 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
641 return -TARGET_EFAULT;
642 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
643 return -TARGET_EFAULT;
645 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
646 return -TARGET_EFAULT;
652 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
653 abi_ulong target_addr,
656 struct target_sockaddr *target_saddr;
658 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
660 return -TARGET_EFAULT;
661 memcpy(addr, target_saddr, len);
662 addr->sa_family = tswap16(target_saddr->sa_family);
663 unlock_user(target_saddr, target_addr, 0);
668 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
669 struct sockaddr *addr,
672 struct target_sockaddr *target_saddr;
674 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
676 return -TARGET_EFAULT;
677 memcpy(target_saddr, addr, len);
678 target_saddr->sa_family = tswap16(addr->sa_family);
679 unlock_user(target_saddr, target_addr, len);
684 /* ??? Should this also swap msgh->name? */
685 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
686 struct target_msghdr *target_msgh)
688 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
689 abi_long msg_controllen;
690 abi_ulong target_cmsg_addr;
691 struct target_cmsghdr *target_cmsg;
694 msg_controllen = tswapl(target_msgh->msg_controllen);
695 if (msg_controllen < sizeof (struct target_cmsghdr))
697 target_cmsg_addr = tswapl(target_msgh->msg_control);
698 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
700 return -TARGET_EFAULT;
702 while (cmsg && target_cmsg) {
703 void *data = CMSG_DATA(cmsg);
704 void *target_data = TARGET_CMSG_DATA(target_cmsg);
706 int len = tswapl(target_cmsg->cmsg_len)
707 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
709 space += CMSG_SPACE(len);
710 if (space > msgh->msg_controllen) {
711 space -= CMSG_SPACE(len);
712 gemu_log("Host cmsg overflow\n");
716 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
717 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
718 cmsg->cmsg_len = CMSG_LEN(len);
720 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
721 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
722 memcpy(data, target_data, len);
724 int *fd = (int *)data;
725 int *target_fd = (int *)target_data;
726 int i, numfds = len / sizeof(int);
728 for (i = 0; i < numfds; i++)
729 fd[i] = tswap32(target_fd[i]);
732 cmsg = CMSG_NXTHDR(msgh, cmsg);
733 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
735 unlock_user(target_cmsg, target_cmsg_addr, 0);
737 msgh->msg_controllen = space;
741 /* ??? Should this also swap msgh->name? */
742 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
745 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
746 abi_long msg_controllen;
747 abi_ulong target_cmsg_addr;
748 struct target_cmsghdr *target_cmsg;
751 msg_controllen = tswapl(target_msgh->msg_controllen);
752 if (msg_controllen < sizeof (struct target_cmsghdr))
754 target_cmsg_addr = tswapl(target_msgh->msg_control);
755 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
757 return -TARGET_EFAULT;
759 while (cmsg && target_cmsg) {
760 void *data = CMSG_DATA(cmsg);
761 void *target_data = TARGET_CMSG_DATA(target_cmsg);
763 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
765 space += TARGET_CMSG_SPACE(len);
766 if (space > msg_controllen) {
767 space -= TARGET_CMSG_SPACE(len);
768 gemu_log("Target cmsg overflow\n");
772 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
773 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
774 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
776 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
777 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
778 memcpy(target_data, data, len);
780 int *fd = (int *)data;
781 int *target_fd = (int *)target_data;
782 int i, numfds = len / sizeof(int);
784 for (i = 0; i < numfds; i++)
785 target_fd[i] = tswap32(fd[i]);
788 cmsg = CMSG_NXTHDR(msgh, cmsg);
789 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
791 unlock_user(target_cmsg, target_cmsg_addr, space);
793 target_msgh->msg_controllen = tswapl(space);
797 /* do_setsockopt() Must return target values and target errnos. */
798 static abi_long do_setsockopt(int sockfd, int level, int optname,
799 abi_ulong optval_addr, socklen_t optlen)
806 /* TCP options all take an 'int' value. */
807 if (optlen < sizeof(uint32_t))
808 return -TARGET_EINVAL;
810 if (get_user_u32(val, optval_addr))
811 return -TARGET_EFAULT;
812 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
819 case IP_ROUTER_ALERT:
823 case IP_MTU_DISCOVER:
829 case IP_MULTICAST_TTL:
830 case IP_MULTICAST_LOOP:
832 if (optlen >= sizeof(uint32_t)) {
833 if (get_user_u32(val, optval_addr))
834 return -TARGET_EFAULT;
835 } else if (optlen >= 1) {
836 if (get_user_u8(val, optval_addr))
837 return -TARGET_EFAULT;
839 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
845 case TARGET_SOL_SOCKET:
847 /* Options with 'int' argument. */
848 case TARGET_SO_DEBUG:
851 case TARGET_SO_REUSEADDR:
852 optname = SO_REUSEADDR;
857 case TARGET_SO_ERROR:
860 case TARGET_SO_DONTROUTE:
861 optname = SO_DONTROUTE;
863 case TARGET_SO_BROADCAST:
864 optname = SO_BROADCAST;
866 case TARGET_SO_SNDBUF:
869 case TARGET_SO_RCVBUF:
872 case TARGET_SO_KEEPALIVE:
873 optname = SO_KEEPALIVE;
875 case TARGET_SO_OOBINLINE:
876 optname = SO_OOBINLINE;
878 case TARGET_SO_NO_CHECK:
879 optname = SO_NO_CHECK;
881 case TARGET_SO_PRIORITY:
882 optname = SO_PRIORITY;
885 case TARGET_SO_BSDCOMPAT:
886 optname = SO_BSDCOMPAT;
889 case TARGET_SO_PASSCRED:
890 optname = SO_PASSCRED;
892 case TARGET_SO_TIMESTAMP:
893 optname = SO_TIMESTAMP;
895 case TARGET_SO_RCVLOWAT:
896 optname = SO_RCVLOWAT;
898 case TARGET_SO_RCVTIMEO:
899 optname = SO_RCVTIMEO;
901 case TARGET_SO_SNDTIMEO:
902 optname = SO_SNDTIMEO;
908 if (optlen < sizeof(uint32_t))
909 return -TARGET_EINVAL;
911 if (get_user_u32(val, optval_addr))
912 return -TARGET_EFAULT;
913 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
917 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
918 ret = -TARGET_ENOPROTOOPT;
923 /* do_getsockopt() Must return target values and target errnos. */
924 static abi_long do_getsockopt(int sockfd, int level, int optname,
925 abi_ulong optval_addr, abi_ulong optlen)
931 case TARGET_SOL_SOCKET:
934 case TARGET_SO_LINGER:
935 case TARGET_SO_RCVTIMEO:
936 case TARGET_SO_SNDTIMEO:
937 case TARGET_SO_PEERCRED:
938 case TARGET_SO_PEERNAME:
939 /* These don't just return a single integer */
946 /* TCP options all take an 'int' value. */
948 if (get_user_u32(len, optlen))
949 return -TARGET_EFAULT;
951 return -TARGET_EINVAL;
953 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
960 if (put_user_u32(val, optval_addr))
961 return -TARGET_EFAULT;
963 if (put_user_u8(val, optval_addr))
964 return -TARGET_EFAULT;
966 if (put_user_u32(len, optlen))
967 return -TARGET_EFAULT;
974 case IP_ROUTER_ALERT:
978 case IP_MTU_DISCOVER:
984 case IP_MULTICAST_TTL:
985 case IP_MULTICAST_LOOP:
986 if (get_user_u32(len, optlen))
987 return -TARGET_EFAULT;
989 return -TARGET_EINVAL;
991 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
994 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
996 if (put_user_u32(len, optlen)
997 || put_user_u8(val, optval_addr))
998 return -TARGET_EFAULT;
1000 if (len > sizeof(int))
1002 if (put_user_u32(len, optlen)
1003 || put_user_u32(val, optval_addr))
1004 return -TARGET_EFAULT;
1008 ret = -TARGET_ENOPROTOOPT;
1014 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1016 ret = -TARGET_EOPNOTSUPP;
1023 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1024 * other lock functions have a return code of 0 for failure.
1026 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1027 int count, int copy)
1029 struct target_iovec *target_vec;
1033 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1035 return -TARGET_EFAULT;
1036 for(i = 0;i < count; i++) {
1037 base = tswapl(target_vec[i].iov_base);
1038 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1039 if (vec[i].iov_len != 0) {
1040 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1041 if (!vec[i].iov_base && vec[i].iov_len)
1044 /* zero length pointer is ignored */
1045 vec[i].iov_base = NULL;
1048 unlock_user (target_vec, target_addr, 0);
1051 /* failure - unwind locks */
1052 for (j = 0; j < i; j++) {
1053 base = tswapl(target_vec[j].iov_base);
1054 unlock_user(vec[j].iov_base, base, 0);
1056 unlock_user (target_vec, target_addr, 0);
1057 return -TARGET_EFAULT;
1060 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1061 int count, int copy)
1063 struct target_iovec *target_vec;
1067 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1069 return -TARGET_EFAULT;
1070 for(i = 0;i < count; i++) {
1071 base = tswapl(target_vec[i].iov_base);
1072 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1074 unlock_user (target_vec, target_addr, 0);
1079 /* do_socket() Must return target values and target errnos. */
1080 static abi_long do_socket(int domain, int type, int protocol)
1082 #if defined(TARGET_MIPS)
1084 case TARGET_SOCK_DGRAM:
1087 case TARGET_SOCK_STREAM:
1090 case TARGET_SOCK_RAW:
1093 case TARGET_SOCK_RDM:
1096 case TARGET_SOCK_SEQPACKET:
1097 type = SOCK_SEQPACKET;
1099 case TARGET_SOCK_PACKET:
1104 if (domain == PF_NETLINK)
1105 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1106 return get_errno(socket(domain, type, protocol));
1109 /* do_bind() Must return target values and target errnos. */
1110 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1113 void *addr = alloca(addrlen);
1115 target_to_host_sockaddr(addr, target_addr, addrlen);
1116 return get_errno(bind(sockfd, addr, addrlen));
1119 /* do_connect() Must return target values and target errnos. */
1120 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1123 void *addr = alloca(addrlen);
1125 target_to_host_sockaddr(addr, target_addr, addrlen);
1126 return get_errno(connect(sockfd, addr, addrlen));
1129 /* do_sendrecvmsg() Must return target values and target errnos. */
1130 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1131 int flags, int send)
1134 struct target_msghdr *msgp;
1138 abi_ulong target_vec;
1141 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1145 return -TARGET_EFAULT;
1146 if (msgp->msg_name) {
1147 msg.msg_namelen = tswap32(msgp->msg_namelen);
1148 msg.msg_name = alloca(msg.msg_namelen);
1149 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1152 msg.msg_name = NULL;
1153 msg.msg_namelen = 0;
1155 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1156 msg.msg_control = alloca(msg.msg_controllen);
1157 msg.msg_flags = tswap32(msgp->msg_flags);
1159 count = tswapl(msgp->msg_iovlen);
1160 vec = alloca(count * sizeof(struct iovec));
1161 target_vec = tswapl(msgp->msg_iov);
1162 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1163 msg.msg_iovlen = count;
1167 ret = target_to_host_cmsg(&msg, msgp);
1169 ret = get_errno(sendmsg(fd, &msg, flags));
1171 ret = get_errno(recvmsg(fd, &msg, flags));
1173 ret = host_to_target_cmsg(msgp, &msg);
1175 unlock_iovec(vec, target_vec, count, !send);
1176 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1180 /* do_accept() Must return target values and target errnos. */
1181 static abi_long do_accept(int fd, abi_ulong target_addr,
1182 abi_ulong target_addrlen_addr)
1188 if (get_user_u32(addrlen, target_addrlen_addr))
1189 return -TARGET_EFAULT;
1191 addr = alloca(addrlen);
1193 ret = get_errno(accept(fd, addr, &addrlen));
1194 if (!is_error(ret)) {
1195 host_to_target_sockaddr(target_addr, addr, addrlen);
1196 if (put_user_u32(addrlen, target_addrlen_addr))
1197 ret = -TARGET_EFAULT;
1202 /* do_getpeername() Must return target values and target errnos. */
1203 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1204 abi_ulong target_addrlen_addr)
1210 if (get_user_u32(addrlen, target_addrlen_addr))
1211 return -TARGET_EFAULT;
1213 addr = alloca(addrlen);
1215 ret = get_errno(getpeername(fd, addr, &addrlen));
1216 if (!is_error(ret)) {
1217 host_to_target_sockaddr(target_addr, addr, addrlen);
1218 if (put_user_u32(addrlen, target_addrlen_addr))
1219 ret = -TARGET_EFAULT;
1224 /* do_getsockname() Must return target values and target errnos. */
1225 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1226 abi_ulong target_addrlen_addr)
1232 if (get_user_u32(addrlen, target_addrlen_addr))
1233 return -TARGET_EFAULT;
1235 addr = alloca(addrlen);
1237 ret = get_errno(getsockname(fd, addr, &addrlen));
1238 if (!is_error(ret)) {
1239 host_to_target_sockaddr(target_addr, addr, addrlen);
1240 if (put_user_u32(addrlen, target_addrlen_addr))
1241 ret = -TARGET_EFAULT;
1246 /* do_socketpair() Must return target values and target errnos. */
1247 static abi_long do_socketpair(int domain, int type, int protocol,
1248 abi_ulong target_tab_addr)
1253 ret = get_errno(socketpair(domain, type, protocol, tab));
1254 if (!is_error(ret)) {
1255 if (put_user_s32(tab[0], target_tab_addr)
1256 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1257 ret = -TARGET_EFAULT;
1262 /* do_sendto() Must return target values and target errnos. */
1263 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1264 abi_ulong target_addr, socklen_t addrlen)
1270 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1272 return -TARGET_EFAULT;
1274 addr = alloca(addrlen);
1275 target_to_host_sockaddr(addr, target_addr, addrlen);
1276 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1278 ret = get_errno(send(fd, host_msg, len, flags));
1280 unlock_user(host_msg, msg, 0);
1284 /* do_recvfrom() Must return target values and target errnos. */
1285 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1286 abi_ulong target_addr,
1287 abi_ulong target_addrlen)
1294 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1296 return -TARGET_EFAULT;
1298 if (get_user_u32(addrlen, target_addrlen)) {
1299 ret = -TARGET_EFAULT;
1302 addr = alloca(addrlen);
1303 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1305 addr = NULL; /* To keep compiler quiet. */
1306 ret = get_errno(recv(fd, host_msg, len, flags));
1308 if (!is_error(ret)) {
1310 host_to_target_sockaddr(target_addr, addr, addrlen);
1311 if (put_user_u32(addrlen, target_addrlen)) {
1312 ret = -TARGET_EFAULT;
1316 unlock_user(host_msg, msg, len);
1319 unlock_user(host_msg, msg, 0);
1324 #ifdef TARGET_NR_socketcall
1325 /* do_socketcall() Must return target values and target errnos. */
1326 static abi_long do_socketcall(int num, abi_ulong vptr)
1329 const int n = sizeof(abi_ulong);
1334 int domain, type, protocol;
1336 if (get_user_s32(domain, vptr)
1337 || get_user_s32(type, vptr + n)
1338 || get_user_s32(protocol, vptr + 2 * n))
1339 return -TARGET_EFAULT;
1341 ret = do_socket(domain, type, protocol);
1347 abi_ulong target_addr;
1350 if (get_user_s32(sockfd, vptr)
1351 || get_user_ual(target_addr, vptr + n)
1352 || get_user_u32(addrlen, vptr + 2 * n))
1353 return -TARGET_EFAULT;
1355 ret = do_bind(sockfd, target_addr, addrlen);
1358 case SOCKOP_connect:
1361 abi_ulong target_addr;
1364 if (get_user_s32(sockfd, vptr)
1365 || get_user_ual(target_addr, vptr + n)
1366 || get_user_u32(addrlen, vptr + 2 * n))
1367 return -TARGET_EFAULT;
1369 ret = do_connect(sockfd, target_addr, addrlen);
1374 int sockfd, backlog;
1376 if (get_user_s32(sockfd, vptr)
1377 || get_user_s32(backlog, vptr + n))
1378 return -TARGET_EFAULT;
1380 ret = get_errno(listen(sockfd, backlog));
1386 abi_ulong target_addr, target_addrlen;
1388 if (get_user_s32(sockfd, vptr)
1389 || get_user_ual(target_addr, vptr + n)
1390 || get_user_u32(target_addrlen, vptr + 2 * n))
1391 return -TARGET_EFAULT;
1393 ret = do_accept(sockfd, target_addr, target_addrlen);
1396 case SOCKOP_getsockname:
1399 abi_ulong target_addr, target_addrlen;
1401 if (get_user_s32(sockfd, vptr)
1402 || get_user_ual(target_addr, vptr + n)
1403 || get_user_u32(target_addrlen, vptr + 2 * n))
1404 return -TARGET_EFAULT;
1406 ret = do_getsockname(sockfd, target_addr, target_addrlen);
1409 case SOCKOP_getpeername:
1412 abi_ulong target_addr, target_addrlen;
1414 if (get_user_s32(sockfd, vptr)
1415 || get_user_ual(target_addr, vptr + n)
1416 || get_user_u32(target_addrlen, vptr + 2 * n))
1417 return -TARGET_EFAULT;
1419 ret = do_getpeername(sockfd, target_addr, target_addrlen);
1422 case SOCKOP_socketpair:
1424 int domain, type, protocol;
1427 if (get_user_s32(domain, vptr)
1428 || get_user_s32(type, vptr + n)
1429 || get_user_s32(protocol, vptr + 2 * n)
1430 || get_user_ual(tab, vptr + 3 * n))
1431 return -TARGET_EFAULT;
1433 ret = do_socketpair(domain, type, protocol, tab);
1443 if (get_user_s32(sockfd, vptr)
1444 || get_user_ual(msg, vptr + n)
1445 || get_user_ual(len, vptr + 2 * n)
1446 || get_user_s32(flags, vptr + 3 * n))
1447 return -TARGET_EFAULT;
1449 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1459 if (get_user_s32(sockfd, vptr)
1460 || get_user_ual(msg, vptr + n)
1461 || get_user_ual(len, vptr + 2 * n)
1462 || get_user_s32(flags, vptr + 3 * n))
1463 return -TARGET_EFAULT;
1465 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1477 if (get_user_s32(sockfd, vptr)
1478 || get_user_ual(msg, vptr + n)
1479 || get_user_ual(len, vptr + 2 * n)
1480 || get_user_s32(flags, vptr + 3 * n)
1481 || get_user_ual(addr, vptr + 4 * n)
1482 || get_user_u32(addrlen, vptr + 5 * n))
1483 return -TARGET_EFAULT;
1485 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1488 case SOCKOP_recvfrom:
1497 if (get_user_s32(sockfd, vptr)
1498 || get_user_ual(msg, vptr + n)
1499 || get_user_ual(len, vptr + 2 * n)
1500 || get_user_s32(flags, vptr + 3 * n)
1501 || get_user_ual(addr, vptr + 4 * n)
1502 || get_user_u32(addrlen, vptr + 5 * n))
1503 return -TARGET_EFAULT;
1505 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1508 case SOCKOP_shutdown:
1512 if (get_user_s32(sockfd, vptr)
1513 || get_user_s32(how, vptr + n))
1514 return -TARGET_EFAULT;
1516 ret = get_errno(shutdown(sockfd, how));
1519 case SOCKOP_sendmsg:
1520 case SOCKOP_recvmsg:
1523 abi_ulong target_msg;
1526 if (get_user_s32(fd, vptr)
1527 || get_user_ual(target_msg, vptr + n)
1528 || get_user_s32(flags, vptr + 2 * n))
1529 return -TARGET_EFAULT;
1531 ret = do_sendrecvmsg(fd, target_msg, flags,
1532 (num == SOCKOP_sendmsg));
1535 case SOCKOP_setsockopt:
1543 if (get_user_s32(sockfd, vptr)
1544 || get_user_s32(level, vptr + n)
1545 || get_user_s32(optname, vptr + 2 * n)
1546 || get_user_ual(optval, vptr + 3 * n)
1547 || get_user_u32(optlen, vptr + 4 * n))
1548 return -TARGET_EFAULT;
1550 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1553 case SOCKOP_getsockopt:
1561 if (get_user_s32(sockfd, vptr)
1562 || get_user_s32(level, vptr + n)
1563 || get_user_s32(optname, vptr + 2 * n)
1564 || get_user_ual(optval, vptr + 3 * n)
1565 || get_user_u32(optlen, vptr + 4 * n))
1566 return -TARGET_EFAULT;
1568 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
1572 gemu_log("Unsupported socketcall: %d\n", num);
1573 ret = -TARGET_ENOSYS;
1580 #ifdef TARGET_NR_ipc
1581 #define N_SHM_REGIONS 32
1583 static struct shm_region {
1586 } shm_regions[N_SHM_REGIONS];
1588 struct target_ipc_perm
1595 unsigned short int mode;
1596 unsigned short int __pad1;
1597 unsigned short int __seq;
1598 unsigned short int __pad2;
1599 abi_ulong __unused1;
1600 abi_ulong __unused2;
1603 struct target_semid_ds
1605 struct target_ipc_perm sem_perm;
1606 abi_ulong sem_otime;
1607 abi_ulong __unused1;
1608 abi_ulong sem_ctime;
1609 abi_ulong __unused2;
1610 abi_ulong sem_nsems;
1611 abi_ulong __unused3;
1612 abi_ulong __unused4;
1615 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1616 abi_ulong target_addr)
1618 struct target_ipc_perm *target_ip;
1619 struct target_semid_ds *target_sd;
1621 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1622 return -TARGET_EFAULT;
1623 target_ip=&(target_sd->sem_perm);
1624 host_ip->__key = tswapl(target_ip->__key);
1625 host_ip->uid = tswapl(target_ip->uid);
1626 host_ip->gid = tswapl(target_ip->gid);
1627 host_ip->cuid = tswapl(target_ip->cuid);
1628 host_ip->cgid = tswapl(target_ip->cgid);
1629 host_ip->mode = tswapl(target_ip->mode);
1630 unlock_user_struct(target_sd, target_addr, 0);
1634 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1635 struct ipc_perm *host_ip)
1637 struct target_ipc_perm *target_ip;
1638 struct target_semid_ds *target_sd;
1640 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1641 return -TARGET_EFAULT;
1642 target_ip = &(target_sd->sem_perm);
1643 target_ip->__key = tswapl(host_ip->__key);
1644 target_ip->uid = tswapl(host_ip->uid);
1645 target_ip->gid = tswapl(host_ip->gid);
1646 target_ip->cuid = tswapl(host_ip->cuid);
1647 target_ip->cgid = tswapl(host_ip->cgid);
1648 target_ip->mode = tswapl(host_ip->mode);
1649 unlock_user_struct(target_sd, target_addr, 1);
1653 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1654 abi_ulong target_addr)
1656 struct target_semid_ds *target_sd;
1658 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1659 return -TARGET_EFAULT;
1660 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1661 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1662 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1663 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1664 unlock_user_struct(target_sd, target_addr, 0);
1668 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1669 struct semid_ds *host_sd)
1671 struct target_semid_ds *target_sd;
1673 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1674 return -TARGET_EFAULT;
1675 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1676 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1677 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1678 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1679 unlock_user_struct(target_sd, target_addr, 1);
1685 struct semid_ds *buf;
1686 unsigned short *array;
1689 union target_semun {
1692 unsigned short int *array;
1695 static inline abi_long target_to_host_semun(int cmd,
1696 union semun *host_su,
1697 abi_ulong target_addr,
1698 struct semid_ds *ds)
1700 union target_semun *target_su;
1705 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1706 return -TARGET_EFAULT;
1707 target_to_host_semid_ds(ds,target_su->buf);
1709 unlock_user_struct(target_su, target_addr, 0);
1713 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1714 return -TARGET_EFAULT;
1715 host_su->val = tswapl(target_su->val);
1716 unlock_user_struct(target_su, target_addr, 0);
1720 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1721 return -TARGET_EFAULT;
1722 *host_su->array = tswap16(*target_su->array);
1723 unlock_user_struct(target_su, target_addr, 0);
1726 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1731 static inline abi_long host_to_target_semun(int cmd,
1732 abi_ulong target_addr,
1733 union semun *host_su,
1734 struct semid_ds *ds)
1736 union target_semun *target_su;
1741 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1742 return -TARGET_EFAULT;
1743 host_to_target_semid_ds(target_su->buf,ds);
1744 unlock_user_struct(target_su, target_addr, 1);
1748 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1749 return -TARGET_EFAULT;
1750 target_su->val = tswapl(host_su->val);
1751 unlock_user_struct(target_su, target_addr, 1);
1755 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1756 return -TARGET_EFAULT;
1757 *target_su->array = tswap16(*host_su->array);
1758 unlock_user_struct(target_su, target_addr, 1);
1761 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1766 static inline abi_long do_semctl(int first, int second, int third,
1770 struct semid_ds dsarg;
1771 int cmd = third&0xff;
1776 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1777 ret = get_errno(semctl(first, second, cmd, arg));
1778 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1781 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1782 ret = get_errno(semctl(first, second, cmd, arg));
1783 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1786 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1787 ret = get_errno(semctl(first, second, cmd, arg));
1788 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1791 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1792 ret = get_errno(semctl(first, second, cmd, arg));
1793 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1796 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1797 ret = get_errno(semctl(first, second, cmd, arg));
1798 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1801 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1802 ret = get_errno(semctl(first, second, cmd, arg));
1803 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1806 ret = get_errno(semctl(first, second, cmd, arg));
1812 struct target_msqid_ds
1814 struct target_ipc_perm msg_perm;
1815 abi_ulong msg_stime;
1816 abi_ulong __unused1;
1817 abi_ulong msg_rtime;
1818 abi_ulong __unused2;
1819 abi_ulong msg_ctime;
1820 abi_ulong __unused3;
1821 abi_ulong __msg_cbytes;
1823 abi_ulong msg_qbytes;
1824 abi_ulong msg_lspid;
1825 abi_ulong msg_lrpid;
1826 abi_ulong __unused4;
1827 abi_ulong __unused5;
1830 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1831 abi_ulong target_addr)
1833 struct target_msqid_ds *target_md;
1835 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1836 return -TARGET_EFAULT;
1837 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1838 host_md->msg_stime = tswapl(target_md->msg_stime);
1839 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1840 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1841 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1842 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1843 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1844 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1845 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1846 unlock_user_struct(target_md, target_addr, 0);
1850 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1851 struct msqid_ds *host_md)
1853 struct target_msqid_ds *target_md;
1855 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1856 return -TARGET_EFAULT;
1857 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1858 target_md->msg_stime = tswapl(host_md->msg_stime);
1859 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1860 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1861 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1862 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1863 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1864 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1865 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1866 unlock_user_struct(target_md, target_addr, 1);
1870 static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1872 struct msqid_ds dsarg;
1873 int cmd = second&0xff;
1878 target_to_host_msqid_ds(&dsarg,ptr);
1879 ret = get_errno(msgctl(first, cmd, &dsarg));
1880 host_to_target_msqid_ds(ptr,&dsarg);
1882 ret = get_errno(msgctl(first, cmd, &dsarg));
1887 struct target_msgbuf {
1892 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1893 unsigned int msgsz, int msgflg)
1895 struct target_msgbuf *target_mb;
1896 struct msgbuf *host_mb;
1899 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1900 return -TARGET_EFAULT;
1901 host_mb = malloc(msgsz+sizeof(long));
1902 host_mb->mtype = tswapl(target_mb->mtype);
1903 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1904 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1906 unlock_user_struct(target_mb, msgp, 0);
1911 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1912 unsigned int msgsz, int msgtype,
1915 struct target_msgbuf *target_mb;
1917 struct msgbuf *host_mb;
1920 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1921 return -TARGET_EFAULT;
1922 host_mb = malloc(msgsz+sizeof(long));
1923 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1925 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1926 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1927 if (!target_mtext) {
1928 ret = -TARGET_EFAULT;
1931 memcpy(target_mb->mtext, host_mb->mtext, ret);
1932 unlock_user(target_mtext, target_mtext_addr, ret);
1934 target_mb->mtype = tswapl(host_mb->mtype);
1939 unlock_user_struct(target_mb, msgp, 1);
1943 /* ??? This only works with linear mappings. */
1944 /* do_ipc() must return target values and target errnos. */
1945 static abi_long do_ipc(unsigned int call, int first,
1946 int second, int third,
1947 abi_long ptr, abi_long fifth)
1951 struct shmid_ds shm_info;
1954 version = call >> 16;
1959 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
1963 ret = get_errno(semget(first, second, third));
1967 ret = do_semctl(first, second, third, ptr);
1970 case IPCOP_semtimedop:
1971 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1972 ret = -TARGET_ENOSYS;
1976 ret = get_errno(msgget(first, second));
1980 ret = do_msgsnd(first, ptr, second, third);
1984 ret = do_msgctl(first, second, ptr);
1989 /* XXX: this code is not correct */
1992 void *__unbounded msgp;
1996 struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
1997 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
1999 ret = do_msgrcv(first, (long)msgp, second, 0, third);
2008 /* SHM_* flags are the same on all linux platforms */
2009 host_addr = shmat(first, (void *)g2h(ptr), second);
2010 if (host_addr == (void *)-1) {
2011 ret = get_errno((long)host_addr);
2014 raddr = h2g((unsigned long)host_addr);
2015 /* find out the length of the shared memory segment */
2017 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
2018 if (is_error(ret)) {
2019 /* can't get length, bail out */
2023 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2024 PAGE_VALID | PAGE_READ |
2025 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2026 for (i = 0; i < N_SHM_REGIONS; ++i) {
2027 if (shm_regions[i].start == 0) {
2028 shm_regions[i].start = raddr;
2029 shm_regions[i].size = shm_info.shm_segsz;
2033 if (put_user_ual(raddr, third))
2034 return -TARGET_EFAULT;
2039 for (i = 0; i < N_SHM_REGIONS; ++i) {
2040 if (shm_regions[i].start == ptr) {
2041 shm_regions[i].start = 0;
2042 page_set_flags(ptr, shm_regions[i].size, 0);
2046 ret = get_errno(shmdt((void *)g2h(ptr)));
2050 /* IPC_* flag values are the same on all linux platforms */
2051 ret = get_errno(shmget(first, second, third));
2054 /* IPC_* and SHM_* command values are the same on all linux platforms */
2060 ret = get_errno(shmctl(first, second, NULL));
2068 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2069 ret = -TARGET_ENOSYS;
2076 /* kernel structure types definitions */
2079 #define STRUCT(name, list...) STRUCT_ ## name,
2080 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2082 #include "syscall_types.h"
2085 #undef STRUCT_SPECIAL
2087 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2088 #define STRUCT_SPECIAL(name)
2089 #include "syscall_types.h"
2091 #undef STRUCT_SPECIAL
2093 typedef struct IOCTLEntry {
2094 unsigned int target_cmd;
2095 unsigned int host_cmd;
2098 const argtype arg_type[5];
2101 #define IOC_R 0x0001
2102 #define IOC_W 0x0002
2103 #define IOC_RW (IOC_R | IOC_W)
2105 #define MAX_STRUCT_SIZE 4096
2107 IOCTLEntry ioctl_entries[] = {
2108 #define IOCTL(cmd, access, types...) \
2109 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2114 /* ??? Implement proper locking for ioctls. */
2115 /* do_ioctl() Must return target values and target errnos. */
2116 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
2118 const IOCTLEntry *ie;
2119 const argtype *arg_type;
2121 uint8_t buf_temp[MAX_STRUCT_SIZE];
2127 if (ie->target_cmd == 0) {
2128 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
2129 return -TARGET_ENOSYS;
2131 if (ie->target_cmd == cmd)
2135 arg_type = ie->arg_type;
2137 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
2139 switch(arg_type[0]) {
2142 ret = get_errno(ioctl(fd, ie->host_cmd));
2147 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2151 target_size = thunk_type_size(arg_type, 0);
2152 switch(ie->access) {
2154 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2155 if (!is_error(ret)) {
2156 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2158 return -TARGET_EFAULT;
2159 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2160 unlock_user(argptr, arg, target_size);
2164 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2166 return -TARGET_EFAULT;
2167 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2168 unlock_user(argptr, arg, 0);
2169 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2173 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2175 return -TARGET_EFAULT;
2176 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2177 unlock_user(argptr, arg, 0);
2178 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2179 if (!is_error(ret)) {
2180 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2182 return -TARGET_EFAULT;
2183 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2184 unlock_user(argptr, arg, target_size);
2190 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2191 (long)cmd, arg_type[0]);
2192 ret = -TARGET_ENOSYS;
2198 bitmask_transtbl iflag_tbl[] = {
2199 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2200 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2201 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2202 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2203 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2204 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2205 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2206 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2207 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2208 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2209 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2210 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2211 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2212 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2216 bitmask_transtbl oflag_tbl[] = {
2217 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2218 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2219 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2220 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2221 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2222 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2223 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2224 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2225 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2226 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2227 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2228 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2229 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2230 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2231 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2232 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2233 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2234 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2235 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2236 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2237 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2238 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2239 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2240 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2244 bitmask_transtbl cflag_tbl[] = {
2245 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2246 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2247 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2248 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2249 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2250 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2251 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2252 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2253 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2254 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2255 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2256 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2257 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2258 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2259 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2260 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2261 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2262 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2263 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2264 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2265 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2266 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2267 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2268 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2269 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2270 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2271 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2272 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2273 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2274 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2275 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2279 bitmask_transtbl lflag_tbl[] = {
2280 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2281 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2282 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2283 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2284 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2285 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2286 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2287 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2288 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2289 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2290 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2291 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2292 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2293 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2294 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2298 static void target_to_host_termios (void *dst, const void *src)
2300 struct host_termios *host = dst;
2301 const struct target_termios *target = src;
2304 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2306 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2308 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2310 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2311 host->c_line = target->c_line;
2313 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2314 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2315 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2316 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2317 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2318 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2319 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2320 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2321 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2322 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2323 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2324 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2325 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2326 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2327 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2328 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2329 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2332 static void host_to_target_termios (void *dst, const void *src)
2334 struct target_termios *target = dst;
2335 const struct host_termios *host = src;
2338 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2340 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2342 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2344 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2345 target->c_line = host->c_line;
2347 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2348 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2349 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2350 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2351 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2352 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2353 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2354 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2355 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2356 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2357 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2358 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2359 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2360 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2361 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2362 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2363 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2366 StructEntry struct_termios_def = {
2367 .convert = { host_to_target_termios, target_to_host_termios },
2368 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2369 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2372 static bitmask_transtbl mmap_flags_tbl[] = {
2373 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2374 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2375 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2376 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2377 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2378 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2379 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2380 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2384 static bitmask_transtbl fcntl_flags_tbl[] = {
2385 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2386 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2387 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2388 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2389 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2390 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2391 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2392 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2393 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2394 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2395 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2396 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2397 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2398 #if defined(O_DIRECT)
2399 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
2404 #if defined(TARGET_I386)
2406 /* NOTE: there is really one LDT for all the threads */
2409 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
2416 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2417 if (size > bytecount)
2419 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2421 return -TARGET_EFAULT;
2422 /* ??? Should this by byteswapped? */
2423 memcpy(p, ldt_table, size);
2424 unlock_user(p, ptr, size);
2428 /* XXX: add locking support */
2429 static abi_long write_ldt(CPUX86State *env,
2430 abi_ulong ptr, unsigned long bytecount, int oldmode)
2432 struct target_modify_ldt_ldt_s ldt_info;
2433 struct target_modify_ldt_ldt_s *target_ldt_info;
2434 int seg_32bit, contents, read_exec_only, limit_in_pages;
2435 int seg_not_present, useable, lm;
2436 uint32_t *lp, entry_1, entry_2;
2438 if (bytecount != sizeof(ldt_info))
2439 return -TARGET_EINVAL;
2440 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
2441 return -TARGET_EFAULT;
2442 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2443 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2444 ldt_info.limit = tswap32(target_ldt_info->limit);
2445 ldt_info.flags = tswap32(target_ldt_info->flags);
2446 unlock_user_struct(target_ldt_info, ptr, 0);
2448 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2449 return -TARGET_EINVAL;
2450 seg_32bit = ldt_info.flags & 1;
2451 contents = (ldt_info.flags >> 1) & 3;
2452 read_exec_only = (ldt_info.flags >> 3) & 1;
2453 limit_in_pages = (ldt_info.flags >> 4) & 1;
2454 seg_not_present = (ldt_info.flags >> 5) & 1;
2455 useable = (ldt_info.flags >> 6) & 1;
2459 lm = (ldt_info.flags >> 7) & 1;
2461 if (contents == 3) {
2463 return -TARGET_EINVAL;
2464 if (seg_not_present == 0)
2465 return -TARGET_EINVAL;
2467 /* allocate the LDT */
2469 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2471 return -TARGET_ENOMEM;
2472 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2473 env->ldt.base = h2g((unsigned long)ldt_table);
2474 env->ldt.limit = 0xffff;
2477 /* NOTE: same code as Linux kernel */
2478 /* Allow LDTs to be cleared by the user. */
2479 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2482 read_exec_only == 1 &&
2484 limit_in_pages == 0 &&
2485 seg_not_present == 1 &&
2493 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2494 (ldt_info.limit & 0x0ffff);
2495 entry_2 = (ldt_info.base_addr & 0xff000000) |
2496 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2497 (ldt_info.limit & 0xf0000) |
2498 ((read_exec_only ^ 1) << 9) |
2500 ((seg_not_present ^ 1) << 15) |
2502 (limit_in_pages << 23) |
2506 entry_2 |= (useable << 20);
2508 /* Install the new entry ... */
2510 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2511 lp[0] = tswap32(entry_1);
2512 lp[1] = tswap32(entry_2);
2516 /* specific and weird i386 syscalls */
2517 abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2518 unsigned long bytecount)
2524 ret = read_ldt(ptr, bytecount);
2527 ret = write_ldt(env, ptr, bytecount, 1);
2530 ret = write_ldt(env, ptr, bytecount, 0);
2533 ret = -TARGET_ENOSYS;
2539 abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
2541 uint64_t *gdt_table = g2h(env->gdt.base);
2542 struct target_modify_ldt_ldt_s ldt_info;
2543 struct target_modify_ldt_ldt_s *target_ldt_info;
2544 int seg_32bit, contents, read_exec_only, limit_in_pages;
2545 int seg_not_present, useable, lm;
2546 uint32_t *lp, entry_1, entry_2;
2549 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2550 if (!target_ldt_info)
2551 return -TARGET_EFAULT;
2552 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2553 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2554 ldt_info.limit = tswap32(target_ldt_info->limit);
2555 ldt_info.flags = tswap32(target_ldt_info->flags);
2556 if (ldt_info.entry_number == -1) {
2557 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
2558 if (gdt_table[i] == 0) {
2559 ldt_info.entry_number = i;
2560 target_ldt_info->entry_number = tswap32(i);
2565 unlock_user_struct(target_ldt_info, ptr, 1);
2567 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
2568 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
2569 return -TARGET_EINVAL;
2570 seg_32bit = ldt_info.flags & 1;
2571 contents = (ldt_info.flags >> 1) & 3;
2572 read_exec_only = (ldt_info.flags >> 3) & 1;
2573 limit_in_pages = (ldt_info.flags >> 4) & 1;
2574 seg_not_present = (ldt_info.flags >> 5) & 1;
2575 useable = (ldt_info.flags >> 6) & 1;
2579 lm = (ldt_info.flags >> 7) & 1;
2582 if (contents == 3) {
2583 if (seg_not_present == 0)
2584 return -TARGET_EINVAL;
2587 /* NOTE: same code as Linux kernel */
2588 /* Allow LDTs to be cleared by the user. */
2589 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2590 if ((contents == 0 &&
2591 read_exec_only == 1 &&
2593 limit_in_pages == 0 &&
2594 seg_not_present == 1 &&
2602 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2603 (ldt_info.limit & 0x0ffff);
2604 entry_2 = (ldt_info.base_addr & 0xff000000) |
2605 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2606 (ldt_info.limit & 0xf0000) |
2607 ((read_exec_only ^ 1) << 9) |
2609 ((seg_not_present ^ 1) << 15) |
2611 (limit_in_pages << 23) |
2616 /* Install the new entry ... */
2618 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
2619 lp[0] = tswap32(entry_1);
2620 lp[1] = tswap32(entry_2);
2624 abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
2626 struct target_modify_ldt_ldt_s *target_ldt_info;
2627 uint64_t *gdt_table = g2h(env->gdt.base);
2628 uint32_t base_addr, limit, flags;
2629 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
2630 int seg_not_present, useable, lm;
2631 uint32_t *lp, entry_1, entry_2;
2633 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2634 if (!target_ldt_info)
2635 return -TARGET_EFAULT;
2636 idx = tswap32(target_ldt_info->entry_number);
2637 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
2638 idx > TARGET_GDT_ENTRY_TLS_MAX) {
2639 unlock_user_struct(target_ldt_info, ptr, 1);
2640 return -TARGET_EINVAL;
2642 lp = (uint32_t *)(gdt_table + idx);
2643 entry_1 = tswap32(lp[0]);
2644 entry_2 = tswap32(lp[1]);
2646 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
2647 contents = (entry_2 >> 10) & 3;
2648 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
2649 seg_32bit = (entry_2 >> 22) & 1;
2650 limit_in_pages = (entry_2 >> 23) & 1;
2651 useable = (entry_2 >> 20) & 1;
2655 lm = (entry_2 >> 21) & 1;
2657 flags = (seg_32bit << 0) | (contents << 1) |
2658 (read_exec_only << 3) | (limit_in_pages << 4) |
2659 (seg_not_present << 5) | (useable << 6) | (lm << 7);
2660 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
2661 base_addr = (entry_1 >> 16) |
2662 (entry_2 & 0xff000000) |
2663 ((entry_2 & 0xff) << 16);
2664 target_ldt_info->base_addr = tswapl(base_addr);
2665 target_ldt_info->limit = tswap32(limit);
2666 target_ldt_info->flags = tswap32(flags);
2667 unlock_user_struct(target_ldt_info, ptr, 1);
2671 #ifndef TARGET_ABI32
2672 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
2679 case TARGET_ARCH_SET_GS:
2680 case TARGET_ARCH_SET_FS:
2681 if (code == TARGET_ARCH_SET_GS)
2685 cpu_x86_load_seg(env, idx, 0);
2686 env->segs[idx].base = addr;
2688 case TARGET_ARCH_GET_GS:
2689 case TARGET_ARCH_GET_FS:
2690 if (code == TARGET_ARCH_GET_GS)
2694 val = env->segs[idx].base;
2695 if (put_user(val, addr, abi_ulong))
2696 return -TARGET_EFAULT;
2699 ret = -TARGET_EINVAL;
2706 #endif /* defined(TARGET_I386) */
2708 /* this stack is the equivalent of the kernel stack associated with a
2710 #define NEW_STACK_SIZE 8192
2712 static int clone_func(void *arg)
2714 CPUState *env = arg;
2720 /* do_fork() Must return host values and target errnos (unlike most
2721 do_*() functions). */
2722 int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
2729 if (flags & CLONE_VM) {
2730 #if defined(USE_NPTL)
2731 /* qemu is not threadsafe. Bail out immediately if application
2732 tries to create a thread. */
2733 if (!(flags & CLONE_VFORK)) {
2734 gemu_log ("clone(CLONE_VM) not supported\n");
2738 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2739 memset(ts, 0, sizeof(TaskState));
2740 new_stack = ts->stack;
2742 /* add in task state list */
2743 ts->next = first_task_state;
2744 first_task_state = ts;
2745 /* we create a new CPU instance. */
2746 new_env = cpu_copy(env);
2747 /* Init regs that differ from the parent. */
2748 cpu_clone_regs(new_env, newsp);
2749 new_env->opaque = ts;
2751 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2753 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2756 /* if no CLONE_VM, we consider it is a fork */
2757 if ((flags & ~CSIGNAL) != 0)
2761 cpu_clone_regs(env, newsp);
2767 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2770 struct target_flock *target_fl;
2771 struct flock64 fl64;
2772 struct target_flock64 *target_fl64;
2776 case TARGET_F_GETLK:
2777 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2778 return -TARGET_EFAULT;
2779 fl.l_type = tswap16(target_fl->l_type);
2780 fl.l_whence = tswap16(target_fl->l_whence);
2781 fl.l_start = tswapl(target_fl->l_start);
2782 fl.l_len = tswapl(target_fl->l_len);
2783 fl.l_pid = tswapl(target_fl->l_pid);
2784 unlock_user_struct(target_fl, arg, 0);
2785 ret = get_errno(fcntl(fd, cmd, &fl));
2787 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2788 return -TARGET_EFAULT;
2789 target_fl->l_type = tswap16(fl.l_type);
2790 target_fl->l_whence = tswap16(fl.l_whence);
2791 target_fl->l_start = tswapl(fl.l_start);
2792 target_fl->l_len = tswapl(fl.l_len);
2793 target_fl->l_pid = tswapl(fl.l_pid);
2794 unlock_user_struct(target_fl, arg, 1);
2798 case TARGET_F_SETLK:
2799 case TARGET_F_SETLKW:
2800 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2801 return -TARGET_EFAULT;
2802 fl.l_type = tswap16(target_fl->l_type);
2803 fl.l_whence = tswap16(target_fl->l_whence);
2804 fl.l_start = tswapl(target_fl->l_start);
2805 fl.l_len = tswapl(target_fl->l_len);
2806 fl.l_pid = tswapl(target_fl->l_pid);
2807 unlock_user_struct(target_fl, arg, 0);
2808 ret = get_errno(fcntl(fd, cmd, &fl));
2811 case TARGET_F_GETLK64:
2812 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2813 return -TARGET_EFAULT;
2814 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2815 fl64.l_whence = tswap16(target_fl64->l_whence);
2816 fl64.l_start = tswapl(target_fl64->l_start);
2817 fl64.l_len = tswapl(target_fl64->l_len);
2818 fl64.l_pid = tswap16(target_fl64->l_pid);
2819 unlock_user_struct(target_fl64, arg, 0);
2820 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2822 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2823 return -TARGET_EFAULT;
2824 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2825 target_fl64->l_whence = tswap16(fl64.l_whence);
2826 target_fl64->l_start = tswapl(fl64.l_start);
2827 target_fl64->l_len = tswapl(fl64.l_len);
2828 target_fl64->l_pid = tswapl(fl64.l_pid);
2829 unlock_user_struct(target_fl64, arg, 1);
2832 case TARGET_F_SETLK64:
2833 case TARGET_F_SETLKW64:
2834 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2835 return -TARGET_EFAULT;
2836 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2837 fl64.l_whence = tswap16(target_fl64->l_whence);
2838 fl64.l_start = tswapl(target_fl64->l_start);
2839 fl64.l_len = tswapl(target_fl64->l_len);
2840 fl64.l_pid = tswap16(target_fl64->l_pid);
2841 unlock_user_struct(target_fl64, arg, 0);
2842 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2846 ret = get_errno(fcntl(fd, cmd, arg));
2848 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2853 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
2857 ret = get_errno(fcntl(fd, cmd, arg));
2865 static inline int high2lowuid(int uid)
2873 static inline int high2lowgid(int gid)
2881 static inline int low2highuid(int uid)
2883 if ((int16_t)uid == -1)
2889 static inline int low2highgid(int gid)
2891 if ((int16_t)gid == -1)
2897 #endif /* USE_UID16 */
2899 void syscall_init(void)
2902 const argtype *arg_type;
2906 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2907 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
2908 #include "syscall_types.h"
2910 #undef STRUCT_SPECIAL
2912 /* we patch the ioctl size if necessary. We rely on the fact that
2913 no ioctl has all the bits at '1' in the size field */
2915 while (ie->target_cmd != 0) {
2916 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2917 TARGET_IOC_SIZEMASK) {
2918 arg_type = ie->arg_type;
2919 if (arg_type[0] != TYPE_PTR) {
2920 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2925 size = thunk_type_size(arg_type, 0);
2926 ie->target_cmd = (ie->target_cmd &
2927 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2928 (size << TARGET_IOC_SIZESHIFT);
2931 /* Build target_to_host_errno_table[] table from
2932 * host_to_target_errno_table[]. */
2933 for (i=0; i < ERRNO_TABLE_SIZE; i++)
2934 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2936 /* automatic consistency check if same arch */
2937 #if defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)
2938 if (ie->target_cmd != ie->host_cmd) {
2939 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2940 ie->target_cmd, ie->host_cmd);
2947 #if TARGET_ABI_BITS == 32
2948 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2950 #ifdef TARGET_WORDS_BIG_ENDIAN
2951 return ((uint64_t)word0 << 32) | word1;
2953 return ((uint64_t)word1 << 32) | word0;
2956 #else /* TARGET_ABI_BITS == 32 */
2957 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2961 #endif /* TARGET_ABI_BITS != 32 */
2963 #ifdef TARGET_NR_truncate64
2964 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2970 if (((CPUARMState *)cpu_env)->eabi)
2976 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2980 #ifdef TARGET_NR_ftruncate64
2981 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2987 if (((CPUARMState *)cpu_env)->eabi)
2993 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2997 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
2998 abi_ulong target_addr)
3000 struct target_timespec *target_ts;
3002 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3003 return -TARGET_EFAULT;
3004 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3005 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3006 unlock_user_struct(target_ts, target_addr, 0);
3010 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3011 struct timespec *host_ts)
3013 struct target_timespec *target_ts;
3015 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3016 return -TARGET_EFAULT;
3017 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3018 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3019 unlock_user_struct(target_ts, target_addr, 1);
3023 #if defined(USE_NPTL)
3024 /* ??? Using host futex calls even when target atomic operations
3025 are not really atomic probably breaks things. However implementing
3026 futexes locally would make futexes shared between multiple processes
3027 tricky. However they're probably useless because guest atomic
3028 operations won't work either. */
3029 int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
3030 target_ulong uaddr2, int val3)
3032 struct timespec ts, *pts;
3034 /* ??? We assume FUTEX_* constants are the same on both host
3040 target_to_host_timespec(pts, timeout);
3044 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAIT, tswap32(val),
3047 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAKE, val, NULL, NULL, 0));
3049 return get_errno(sys_futex(g2h(uaddr), FUTEX_FD, val, NULL, NULL, 0));
3051 return get_errno(sys_futex(g2h(uaddr), FUTEX_REQUEUE, val,
3052 NULL, g2h(uaddr2), 0));
3053 case FUTEX_CMP_REQUEUE:
3054 return get_errno(sys_futex(g2h(uaddr), FUTEX_CMP_REQUEUE, val,
3055 NULL, g2h(uaddr2), tswap32(val3)));
3057 return -TARGET_ENOSYS;
3062 int get_osversion(void)
3064 static int osversion;
3065 struct new_utsname buf;
3070 if (qemu_uname_release && *qemu_uname_release) {
3071 s = qemu_uname_release;
3073 if (sys_uname(&buf))
3078 for (i = 0; i < 3; i++) {
3080 while (*s >= '0' && *s <= '9') {
3085 tmp = (tmp << 8) + n;
3093 /* do_syscall() should always have a single exit point at the end so
3094 that actions, such as logging of syscall results, can be performed.
3095 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
3096 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3097 abi_long arg2, abi_long arg3, abi_long arg4,
3098 abi_long arg5, abi_long arg6)
3106 gemu_log("syscall %d", num);
3109 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3112 case TARGET_NR_exit:
3116 gdb_exit(cpu_env, arg1);
3117 /* XXX: should free thread stack and CPU env */
3119 ret = 0; /* avoid warning */
3121 case TARGET_NR_read:
3122 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3124 ret = get_errno(read(arg1, p, arg3));
3125 unlock_user(p, arg2, ret);
3127 case TARGET_NR_write:
3128 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3130 ret = get_errno(write(arg1, p, arg3));
3131 unlock_user(p, arg2, 0);
3133 case TARGET_NR_open:
3134 if (!(p = lock_user_string(arg1)))
3136 ret = get_errno(open(path(p),
3137 target_to_host_bitmask(arg2, fcntl_flags_tbl),
3139 unlock_user(p, arg1, 0);
3141 #if defined(TARGET_NR_openat) && defined(__NR_openat)
3142 case TARGET_NR_openat:
3143 if (!(p = lock_user_string(arg2)))
3145 ret = get_errno(sys_openat(arg1,
3147 target_to_host_bitmask(arg3, fcntl_flags_tbl),
3149 unlock_user(p, arg2, 0);
3152 case TARGET_NR_close:
3153 ret = get_errno(close(arg1));
3158 case TARGET_NR_fork:
3159 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
3161 #ifdef TARGET_NR_waitpid
3162 case TARGET_NR_waitpid:
3165 ret = get_errno(waitpid(arg1, &status, arg3));
3166 if (!is_error(ret) && arg2
3167 && put_user_s32(status, arg2))
3172 #ifdef TARGET_NR_waitid
3173 case TARGET_NR_waitid:
3177 ret = get_errno(waitid(arg1, arg2, &info, arg4));
3178 if (!is_error(ret) && arg3 && info.si_pid != 0) {
3179 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
3181 host_to_target_siginfo(p, &info);
3182 unlock_user(p, arg3, sizeof(target_siginfo_t));
3187 #ifdef TARGET_NR_creat /* not on alpha */
3188 case TARGET_NR_creat:
3189 if (!(p = lock_user_string(arg1)))
3191 ret = get_errno(creat(p, arg2));
3192 unlock_user(p, arg1, 0);
3195 case TARGET_NR_link:
3198 p = lock_user_string(arg1);
3199 p2 = lock_user_string(arg2);
3201 ret = -TARGET_EFAULT;
3203 ret = get_errno(link(p, p2));
3204 unlock_user(p2, arg2, 0);
3205 unlock_user(p, arg1, 0);
3208 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3209 case TARGET_NR_linkat:
3214 p = lock_user_string(arg2);
3215 p2 = lock_user_string(arg4);
3217 ret = -TARGET_EFAULT;
3219 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
3220 unlock_user(p, arg2, 0);
3221 unlock_user(p2, arg4, 0);
3225 case TARGET_NR_unlink:
3226 if (!(p = lock_user_string(arg1)))
3228 ret = get_errno(unlink(p));
3229 unlock_user(p, arg1, 0);
3231 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3232 case TARGET_NR_unlinkat:
3233 if (!(p = lock_user_string(arg2)))
3235 ret = get_errno(sys_unlinkat(arg1, p, arg3));
3236 unlock_user(p, arg2, 0);
3239 case TARGET_NR_execve:
3241 char **argp, **envp;
3244 abi_ulong guest_argp;
3245 abi_ulong guest_envp;
3251 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
3252 if (get_user_ual(addr, gp))
3260 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
3261 if (get_user_ual(addr, gp))
3268 argp = alloca((argc + 1) * sizeof(void *));
3269 envp = alloca((envc + 1) * sizeof(void *));
3271 for (gp = guest_argp, q = argp; gp;
3272 gp += sizeof(abi_ulong), q++) {
3273 if (get_user_ual(addr, gp))
3277 if (!(*q = lock_user_string(addr)))
3282 for (gp = guest_envp, q = envp; gp;
3283 gp += sizeof(abi_ulong), q++) {
3284 if (get_user_ual(addr, gp))
3288 if (!(*q = lock_user_string(addr)))
3293 if (!(p = lock_user_string(arg1)))
3295 ret = get_errno(execve(p, argp, envp));
3296 unlock_user(p, arg1, 0);
3301 ret = -TARGET_EFAULT;
3304 for (gp = guest_argp, q = argp; *q;
3305 gp += sizeof(abi_ulong), q++) {
3306 if (get_user_ual(addr, gp)
3309 unlock_user(*q, addr, 0);
3311 for (gp = guest_envp, q = envp; *q;
3312 gp += sizeof(abi_ulong), q++) {
3313 if (get_user_ual(addr, gp)
3316 unlock_user(*q, addr, 0);
3320 case TARGET_NR_chdir:
3321 if (!(p = lock_user_string(arg1)))
3323 ret = get_errno(chdir(p));
3324 unlock_user(p, arg1, 0);
3326 #ifdef TARGET_NR_time
3327 case TARGET_NR_time:
3330 ret = get_errno(time(&host_time));
3333 && put_user_sal(host_time, arg1))
3338 case TARGET_NR_mknod:
3339 if (!(p = lock_user_string(arg1)))
3341 ret = get_errno(mknod(p, arg2, arg3));
3342 unlock_user(p, arg1, 0);
3344 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
3345 case TARGET_NR_mknodat:
3346 if (!(p = lock_user_string(arg2)))
3348 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
3349 unlock_user(p, arg2, 0);
3352 case TARGET_NR_chmod:
3353 if (!(p = lock_user_string(arg1)))
3355 ret = get_errno(chmod(p, arg2));
3356 unlock_user(p, arg1, 0);
3358 #ifdef TARGET_NR_break
3359 case TARGET_NR_break:
3362 #ifdef TARGET_NR_oldstat
3363 case TARGET_NR_oldstat:
3366 case TARGET_NR_lseek:
3367 ret = get_errno(lseek(arg1, arg2, arg3));
3369 #ifdef TARGET_NR_getxpid
3370 case TARGET_NR_getxpid:
3372 case TARGET_NR_getpid:
3374 ret = get_errno(getpid());
3376 case TARGET_NR_mount:
3378 /* need to look at the data field */
3380 p = lock_user_string(arg1);
3381 p2 = lock_user_string(arg2);
3382 p3 = lock_user_string(arg3);
3383 if (!p || !p2 || !p3)
3384 ret = -TARGET_EFAULT;
3386 /* FIXME - arg5 should be locked, but it isn't clear how to
3387 * do that since it's not guaranteed to be a NULL-terminated
3390 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3391 unlock_user(p, arg1, 0);
3392 unlock_user(p2, arg2, 0);
3393 unlock_user(p3, arg3, 0);
3396 #ifdef TARGET_NR_umount
3397 case TARGET_NR_umount:
3398 if (!(p = lock_user_string(arg1)))
3400 ret = get_errno(umount(p));
3401 unlock_user(p, arg1, 0);
3404 #ifdef TARGET_NR_stime /* not on alpha */
3405 case TARGET_NR_stime:
3408 if (get_user_sal(host_time, arg1))
3410 ret = get_errno(stime(&host_time));
3414 case TARGET_NR_ptrace:
3416 #ifdef TARGET_NR_alarm /* not on alpha */
3417 case TARGET_NR_alarm:
3421 #ifdef TARGET_NR_oldfstat
3422 case TARGET_NR_oldfstat:
3425 #ifdef TARGET_NR_pause /* not on alpha */
3426 case TARGET_NR_pause:
3427 ret = get_errno(pause());
3430 #ifdef TARGET_NR_utime
3431 case TARGET_NR_utime:
3433 struct utimbuf tbuf, *host_tbuf;
3434 struct target_utimbuf *target_tbuf;
3436 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3438 tbuf.actime = tswapl(target_tbuf->actime);
3439 tbuf.modtime = tswapl(target_tbuf->modtime);
3440 unlock_user_struct(target_tbuf, arg2, 0);
3445 if (!(p = lock_user_string(arg1)))
3447 ret = get_errno(utime(p, host_tbuf));
3448 unlock_user(p, arg1, 0);
3452 case TARGET_NR_utimes:
3454 struct timeval *tvp, tv[2];
3456 if (copy_from_user_timeval(&tv[0], arg2)
3457 || copy_from_user_timeval(&tv[1],
3458 arg2 + sizeof(struct target_timeval)))
3464 if (!(p = lock_user_string(arg1)))
3466 ret = get_errno(utimes(p, tvp));
3467 unlock_user(p, arg1, 0);
3470 #ifdef TARGET_NR_stty
3471 case TARGET_NR_stty:
3474 #ifdef TARGET_NR_gtty
3475 case TARGET_NR_gtty:
3478 case TARGET_NR_access:
3479 if (!(p = lock_user_string(arg1)))
3481 ret = get_errno(access(p, arg2));
3482 unlock_user(p, arg1, 0);
3484 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3485 case TARGET_NR_faccessat:
3486 if (!(p = lock_user_string(arg2)))
3488 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3489 unlock_user(p, arg2, 0);
3492 #ifdef TARGET_NR_nice /* not on alpha */
3493 case TARGET_NR_nice:
3494 ret = get_errno(nice(arg1));
3497 #ifdef TARGET_NR_ftime
3498 case TARGET_NR_ftime:
3501 case TARGET_NR_sync:
3505 case TARGET_NR_kill:
3506 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
3508 case TARGET_NR_rename:
3511 p = lock_user_string(arg1);
3512 p2 = lock_user_string(arg2);
3514 ret = -TARGET_EFAULT;
3516 ret = get_errno(rename(p, p2));
3517 unlock_user(p2, arg2, 0);
3518 unlock_user(p, arg1, 0);
3521 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3522 case TARGET_NR_renameat:
3525 p = lock_user_string(arg2);
3526 p2 = lock_user_string(arg4);
3528 ret = -TARGET_EFAULT;
3530 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
3531 unlock_user(p2, arg4, 0);
3532 unlock_user(p, arg2, 0);
3536 case TARGET_NR_mkdir:
3537 if (!(p = lock_user_string(arg1)))
3539 ret = get_errno(mkdir(p, arg2));
3540 unlock_user(p, arg1, 0);
3542 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3543 case TARGET_NR_mkdirat:
3544 if (!(p = lock_user_string(arg2)))
3546 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3547 unlock_user(p, arg2, 0);
3550 case TARGET_NR_rmdir:
3551 if (!(p = lock_user_string(arg1)))
3553 ret = get_errno(rmdir(p));
3554 unlock_user(p, arg1, 0);
3557 ret = get_errno(dup(arg1));
3559 case TARGET_NR_pipe:
3562 ret = get_errno(pipe(host_pipe));
3563 if (!is_error(ret)) {
3564 #if defined(TARGET_MIPS)
3565 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3566 env->gpr[env->current_tc][3] = host_pipe[1];
3568 #elif defined(TARGET_SH4)
3569 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
3572 if (put_user_s32(host_pipe[0], arg1)
3573 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
3579 case TARGET_NR_times:
3581 struct target_tms *tmsp;
3583 ret = get_errno(times(&tms));
3585 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3588 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3589 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3590 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3591 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
3594 ret = host_to_target_clock_t(ret);
3597 #ifdef TARGET_NR_prof
3598 case TARGET_NR_prof:
3601 #ifdef TARGET_NR_signal
3602 case TARGET_NR_signal:
3605 case TARGET_NR_acct:
3606 if (!(p = lock_user_string(arg1)))
3608 ret = get_errno(acct(path(p)));
3609 unlock_user(p, arg1, 0);
3611 #ifdef TARGET_NR_umount2 /* not on alpha */
3612 case TARGET_NR_umount2:
3613 if (!(p = lock_user_string(arg1)))
3615 ret = get_errno(umount2(p, arg2));
3616 unlock_user(p, arg1, 0);
3619 #ifdef TARGET_NR_lock
3620 case TARGET_NR_lock:
3623 case TARGET_NR_ioctl:
3624 ret = do_ioctl(arg1, arg2, arg3);
3626 case TARGET_NR_fcntl:
3627 ret = do_fcntl(arg1, arg2, arg3);
3629 #ifdef TARGET_NR_mpx
3633 case TARGET_NR_setpgid:
3634 ret = get_errno(setpgid(arg1, arg2));
3636 #ifdef TARGET_NR_ulimit
3637 case TARGET_NR_ulimit:
3640 #ifdef TARGET_NR_oldolduname
3641 case TARGET_NR_oldolduname:
3644 case TARGET_NR_umask:
3645 ret = get_errno(umask(arg1));
3647 case TARGET_NR_chroot:
3648 if (!(p = lock_user_string(arg1)))
3650 ret = get_errno(chroot(p));
3651 unlock_user(p, arg1, 0);
3653 case TARGET_NR_ustat:
3655 case TARGET_NR_dup2:
3656 ret = get_errno(dup2(arg1, arg2));
3658 #ifdef TARGET_NR_getppid /* not on alpha */
3659 case TARGET_NR_getppid:
3660 ret = get_errno(getppid());
3663 case TARGET_NR_getpgrp:
3664 ret = get_errno(getpgrp());
3666 case TARGET_NR_setsid:
3667 ret = get_errno(setsid());
3669 #ifdef TARGET_NR_sigaction
3670 case TARGET_NR_sigaction:
3672 #if !defined(TARGET_MIPS)
3673 struct target_old_sigaction *old_act;
3674 struct target_sigaction act, oact, *pact;
3676 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3678 act._sa_handler = old_act->_sa_handler;
3679 target_siginitset(&act.sa_mask, old_act->sa_mask);
3680 act.sa_flags = old_act->sa_flags;
3681 act.sa_restorer = old_act->sa_restorer;
3682 unlock_user_struct(old_act, arg2, 0);
3687 ret = get_errno(do_sigaction(arg1, pact, &oact));
3688 if (!is_error(ret) && arg3) {
3689 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3691 old_act->_sa_handler = oact._sa_handler;
3692 old_act->sa_mask = oact.sa_mask.sig[0];
3693 old_act->sa_flags = oact.sa_flags;
3694 old_act->sa_restorer = oact.sa_restorer;
3695 unlock_user_struct(old_act, arg3, 1);
3698 struct target_sigaction act, oact, *pact, *old_act;
3701 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3703 act._sa_handler = old_act->_sa_handler;
3704 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3705 act.sa_flags = old_act->sa_flags;
3706 unlock_user_struct(old_act, arg2, 0);
3712 ret = get_errno(do_sigaction(arg1, pact, &oact));
3714 if (!is_error(ret) && arg3) {
3715 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3717 old_act->_sa_handler = oact._sa_handler;
3718 old_act->sa_flags = oact.sa_flags;
3719 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3720 old_act->sa_mask.sig[1] = 0;
3721 old_act->sa_mask.sig[2] = 0;
3722 old_act->sa_mask.sig[3] = 0;
3723 unlock_user_struct(old_act, arg3, 1);
3729 case TARGET_NR_rt_sigaction:
3731 struct target_sigaction *act;
3732 struct target_sigaction *oact;
3735 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3740 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3741 ret = -TARGET_EFAULT;
3742 goto rt_sigaction_fail;
3746 ret = get_errno(do_sigaction(arg1, act, oact));
3749 unlock_user_struct(act, arg2, 0);
3751 unlock_user_struct(oact, arg3, 1);
3754 #ifdef TARGET_NR_sgetmask /* not on alpha */
3755 case TARGET_NR_sgetmask:
3758 abi_ulong target_set;
3759 sigprocmask(0, NULL, &cur_set);
3760 host_to_target_old_sigset(&target_set, &cur_set);
3765 #ifdef TARGET_NR_ssetmask /* not on alpha */
3766 case TARGET_NR_ssetmask:
3768 sigset_t set, oset, cur_set;
3769 abi_ulong target_set = arg1;
3770 sigprocmask(0, NULL, &cur_set);
3771 target_to_host_old_sigset(&set, &target_set);
3772 sigorset(&set, &set, &cur_set);
3773 sigprocmask(SIG_SETMASK, &set, &oset);
3774 host_to_target_old_sigset(&target_set, &oset);
3779 #ifdef TARGET_NR_sigprocmask
3780 case TARGET_NR_sigprocmask:
3783 sigset_t set, oldset, *set_ptr;
3787 case TARGET_SIG_BLOCK:
3790 case TARGET_SIG_UNBLOCK:
3793 case TARGET_SIG_SETMASK:
3797 ret = -TARGET_EINVAL;
3800 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3802 target_to_host_old_sigset(&set, p);
3803 unlock_user(p, arg2, 0);
3809 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
3810 if (!is_error(ret) && arg3) {
3811 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3813 host_to_target_old_sigset(p, &oldset);
3814 unlock_user(p, arg3, sizeof(target_sigset_t));
3819 case TARGET_NR_rt_sigprocmask:
3822 sigset_t set, oldset, *set_ptr;
3826 case TARGET_SIG_BLOCK:
3829 case TARGET_SIG_UNBLOCK:
3832 case TARGET_SIG_SETMASK:
3836 ret = -TARGET_EINVAL;
3839 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3841 target_to_host_sigset(&set, p);
3842 unlock_user(p, arg2, 0);
3848 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
3849 if (!is_error(ret) && arg3) {
3850 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3852 host_to_target_sigset(p, &oldset);
3853 unlock_user(p, arg3, sizeof(target_sigset_t));
3857 #ifdef TARGET_NR_sigpending
3858 case TARGET_NR_sigpending:
3861 ret = get_errno(sigpending(&set));
3862 if (!is_error(ret)) {
3863 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3865 host_to_target_old_sigset(p, &set);
3866 unlock_user(p, arg1, sizeof(target_sigset_t));
3871 case TARGET_NR_rt_sigpending:
3874 ret = get_errno(sigpending(&set));
3875 if (!is_error(ret)) {
3876 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3878 host_to_target_sigset(p, &set);
3879 unlock_user(p, arg1, sizeof(target_sigset_t));
3883 #ifdef TARGET_NR_sigsuspend
3884 case TARGET_NR_sigsuspend:
3887 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3889 target_to_host_old_sigset(&set, p);
3890 unlock_user(p, arg1, 0);
3891 ret = get_errno(sigsuspend(&set));
3895 case TARGET_NR_rt_sigsuspend:
3898 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3900 target_to_host_sigset(&set, p);
3901 unlock_user(p, arg1, 0);
3902 ret = get_errno(sigsuspend(&set));
3905 case TARGET_NR_rt_sigtimedwait:
3908 struct timespec uts, *puts;
3911 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3913 target_to_host_sigset(&set, p);
3914 unlock_user(p, arg1, 0);
3917 target_to_host_timespec(puts, arg3);
3921 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
3922 if (!is_error(ret) && arg2) {
3923 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3925 host_to_target_siginfo(p, &uinfo);
3926 unlock_user(p, arg2, sizeof(target_sigset_t));
3930 case TARGET_NR_rt_sigqueueinfo:
3933 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3935 target_to_host_siginfo(&uinfo, p);
3936 unlock_user(p, arg1, 0);
3937 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3940 #ifdef TARGET_NR_sigreturn
3941 case TARGET_NR_sigreturn:
3942 /* NOTE: ret is eax, so not transcoding must be done */
3943 ret = do_sigreturn(cpu_env);
3946 case TARGET_NR_rt_sigreturn:
3947 /* NOTE: ret is eax, so not transcoding must be done */
3948 ret = do_rt_sigreturn(cpu_env);
3950 case TARGET_NR_sethostname:
3951 if (!(p = lock_user_string(arg1)))
3953 ret = get_errno(sethostname(p, arg2));
3954 unlock_user(p, arg1, 0);
3956 case TARGET_NR_setrlimit:
3958 /* XXX: convert resource ? */
3959 int resource = arg1;
3960 struct target_rlimit *target_rlim;
3962 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3964 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3965 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3966 unlock_user_struct(target_rlim, arg2, 0);
3967 ret = get_errno(setrlimit(resource, &rlim));
3970 case TARGET_NR_getrlimit:
3972 /* XXX: convert resource ? */
3973 int resource = arg1;
3974 struct target_rlimit *target_rlim;
3977 ret = get_errno(getrlimit(resource, &rlim));
3978 if (!is_error(ret)) {
3979 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3981 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3982 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3983 unlock_user_struct(target_rlim, arg2, 1);
3987 case TARGET_NR_getrusage:
3989 struct rusage rusage;
3990 ret = get_errno(getrusage(arg1, &rusage));
3991 if (!is_error(ret)) {
3992 host_to_target_rusage(arg2, &rusage);
3996 case TARGET_NR_gettimeofday:
3999 ret = get_errno(gettimeofday(&tv, NULL));
4000 if (!is_error(ret)) {
4001 if (copy_to_user_timeval(arg1, &tv))
4006 case TARGET_NR_settimeofday:
4009 if (copy_from_user_timeval(&tv, arg1))
4011 ret = get_errno(settimeofday(&tv, NULL));
4014 #ifdef TARGET_NR_select
4015 case TARGET_NR_select:
4017 struct target_sel_arg_struct *sel;
4018 abi_ulong inp, outp, exp, tvp;
4021 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
4023 nsel = tswapl(sel->n);
4024 inp = tswapl(sel->inp);
4025 outp = tswapl(sel->outp);
4026 exp = tswapl(sel->exp);
4027 tvp = tswapl(sel->tvp);
4028 unlock_user_struct(sel, arg1, 0);
4029 ret = do_select(nsel, inp, outp, exp, tvp);
4033 case TARGET_NR_symlink:
4036 p = lock_user_string(arg1);
4037 p2 = lock_user_string(arg2);
4039 ret = -TARGET_EFAULT;
4041 ret = get_errno(symlink(p, p2));
4042 unlock_user(p2, arg2, 0);
4043 unlock_user(p, arg1, 0);
4046 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
4047 case TARGET_NR_symlinkat:
4050 p = lock_user_string(arg1);
4051 p2 = lock_user_string(arg3);
4053 ret = -TARGET_EFAULT;
4055 ret = get_errno(sys_symlinkat(p, arg2, p2));
4056 unlock_user(p2, arg3, 0);
4057 unlock_user(p, arg1, 0);
4061 #ifdef TARGET_NR_oldlstat
4062 case TARGET_NR_oldlstat:
4065 case TARGET_NR_readlink:
4068 p = lock_user_string(arg1);
4069 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4071 ret = -TARGET_EFAULT;
4073 ret = get_errno(readlink(path(p), p2, arg3));
4074 unlock_user(p2, arg2, ret);
4075 unlock_user(p, arg1, 0);
4078 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4079 case TARGET_NR_readlinkat:
4082 p = lock_user_string(arg2);
4083 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4085 ret = -TARGET_EFAULT;
4087 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
4088 unlock_user(p2, arg3, ret);
4089 unlock_user(p, arg2, 0);
4093 #ifdef TARGET_NR_uselib
4094 case TARGET_NR_uselib:
4097 #ifdef TARGET_NR_swapon
4098 case TARGET_NR_swapon:
4099 if (!(p = lock_user_string(arg1)))
4101 ret = get_errno(swapon(p, arg2));
4102 unlock_user(p, arg1, 0);
4105 case TARGET_NR_reboot:
4107 #ifdef TARGET_NR_readdir
4108 case TARGET_NR_readdir:
4111 #ifdef TARGET_NR_mmap
4112 case TARGET_NR_mmap:
4113 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
4116 abi_ulong v1, v2, v3, v4, v5, v6;
4117 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4125 unlock_user(v, arg1, 0);
4126 ret = get_errno(target_mmap(v1, v2, v3,
4127 target_to_host_bitmask(v4, mmap_flags_tbl),
4131 ret = get_errno(target_mmap(arg1, arg2, arg3,
4132 target_to_host_bitmask(arg4, mmap_flags_tbl),
4138 #ifdef TARGET_NR_mmap2
4139 case TARGET_NR_mmap2:
4141 #define MMAP_SHIFT 12
4143 ret = get_errno(target_mmap(arg1, arg2, arg3,
4144 target_to_host_bitmask(arg4, mmap_flags_tbl),
4146 arg6 << MMAP_SHIFT));
4149 case TARGET_NR_munmap:
4150 ret = get_errno(target_munmap(arg1, arg2));
4152 case TARGET_NR_mprotect:
4153 ret = get_errno(target_mprotect(arg1, arg2, arg3));
4155 #ifdef TARGET_NR_mremap
4156 case TARGET_NR_mremap:
4157 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
4160 /* ??? msync/mlock/munlock are broken for softmmu. */
4161 #ifdef TARGET_NR_msync
4162 case TARGET_NR_msync:
4163 ret = get_errno(msync(g2h(arg1), arg2, arg3));
4166 #ifdef TARGET_NR_mlock
4167 case TARGET_NR_mlock:
4168 ret = get_errno(mlock(g2h(arg1), arg2));
4171 #ifdef TARGET_NR_munlock
4172 case TARGET_NR_munlock:
4173 ret = get_errno(munlock(g2h(arg1), arg2));
4176 #ifdef TARGET_NR_mlockall
4177 case TARGET_NR_mlockall:
4178 ret = get_errno(mlockall(arg1));
4181 #ifdef TARGET_NR_munlockall
4182 case TARGET_NR_munlockall:
4183 ret = get_errno(munlockall());
4186 case TARGET_NR_truncate:
4187 if (!(p = lock_user_string(arg1)))
4189 ret = get_errno(truncate(p, arg2));
4190 unlock_user(p, arg1, 0);
4192 case TARGET_NR_ftruncate:
4193 ret = get_errno(ftruncate(arg1, arg2));
4195 case TARGET_NR_fchmod:
4196 ret = get_errno(fchmod(arg1, arg2));
4198 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
4199 case TARGET_NR_fchmodat:
4200 if (!(p = lock_user_string(arg2)))
4202 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
4203 unlock_user(p, arg2, 0);
4206 case TARGET_NR_getpriority:
4207 /* libc does special remapping of the return value of
4208 * sys_getpriority() so it's just easiest to call
4209 * sys_getpriority() directly rather than through libc. */
4210 ret = sys_getpriority(arg1, arg2);
4212 case TARGET_NR_setpriority:
4213 ret = get_errno(setpriority(arg1, arg2, arg3));
4215 #ifdef TARGET_NR_profil
4216 case TARGET_NR_profil:
4219 case TARGET_NR_statfs:
4220 if (!(p = lock_user_string(arg1)))
4222 ret = get_errno(statfs(path(p), &stfs));
4223 unlock_user(p, arg1, 0);
4225 if (!is_error(ret)) {
4226 struct target_statfs *target_stfs;
4228 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
4230 __put_user(stfs.f_type, &target_stfs->f_type);
4231 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4232 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4233 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4234 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4235 __put_user(stfs.f_files, &target_stfs->f_files);
4236 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4237 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4238 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4239 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4240 unlock_user_struct(target_stfs, arg2, 1);
4243 case TARGET_NR_fstatfs:
4244 ret = get_errno(fstatfs(arg1, &stfs));
4245 goto convert_statfs;
4246 #ifdef TARGET_NR_statfs64
4247 case TARGET_NR_statfs64:
4248 if (!(p = lock_user_string(arg1)))
4250 ret = get_errno(statfs(path(p), &stfs));
4251 unlock_user(p, arg1, 0);
4253 if (!is_error(ret)) {
4254 struct target_statfs64 *target_stfs;
4256 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
4258 __put_user(stfs.f_type, &target_stfs->f_type);
4259 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4260 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4261 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4262 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4263 __put_user(stfs.f_files, &target_stfs->f_files);
4264 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4265 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4266 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4267 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4268 unlock_user_struct(target_stfs, arg3, 1);
4271 case TARGET_NR_fstatfs64:
4272 ret = get_errno(fstatfs(arg1, &stfs));
4273 goto convert_statfs64;
4275 #ifdef TARGET_NR_ioperm
4276 case TARGET_NR_ioperm:
4279 #ifdef TARGET_NR_socketcall
4280 case TARGET_NR_socketcall:
4281 ret = do_socketcall(arg1, arg2);
4284 #ifdef TARGET_NR_accept
4285 case TARGET_NR_accept:
4286 ret = do_accept(arg1, arg2, arg3);
4289 #ifdef TARGET_NR_bind
4290 case TARGET_NR_bind:
4291 ret = do_bind(arg1, arg2, arg3);
4294 #ifdef TARGET_NR_connect
4295 case TARGET_NR_connect:
4296 ret = do_connect(arg1, arg2, arg3);
4299 #ifdef TARGET_NR_getpeername
4300 case TARGET_NR_getpeername:
4301 ret = do_getpeername(arg1, arg2, arg3);
4304 #ifdef TARGET_NR_getsockname
4305 case TARGET_NR_getsockname:
4306 ret = do_getsockname(arg1, arg2, arg3);
4309 #ifdef TARGET_NR_getsockopt
4310 case TARGET_NR_getsockopt:
4311 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
4314 #ifdef TARGET_NR_listen
4315 case TARGET_NR_listen:
4316 ret = get_errno(listen(arg1, arg2));
4319 #ifdef TARGET_NR_recv
4320 case TARGET_NR_recv:
4321 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
4324 #ifdef TARGET_NR_recvfrom
4325 case TARGET_NR_recvfrom:
4326 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
4329 #ifdef TARGET_NR_recvmsg
4330 case TARGET_NR_recvmsg:
4331 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
4334 #ifdef TARGET_NR_send
4335 case TARGET_NR_send:
4336 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
4339 #ifdef TARGET_NR_sendmsg
4340 case TARGET_NR_sendmsg:
4341 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
4344 #ifdef TARGET_NR_sendto
4345 case TARGET_NR_sendto:
4346 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
4349 #ifdef TARGET_NR_shutdown
4350 case TARGET_NR_shutdown:
4351 ret = get_errno(shutdown(arg1, arg2));
4354 #ifdef TARGET_NR_socket
4355 case TARGET_NR_socket:
4356 ret = do_socket(arg1, arg2, arg3);
4359 #ifdef TARGET_NR_socketpair
4360 case TARGET_NR_socketpair:
4361 ret = do_socketpair(arg1, arg2, arg3, arg4);
4364 #ifdef TARGET_NR_setsockopt
4365 case TARGET_NR_setsockopt:
4366 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
4370 case TARGET_NR_syslog:
4371 if (!(p = lock_user_string(arg2)))
4373 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
4374 unlock_user(p, arg2, 0);
4377 case TARGET_NR_setitimer:
4379 struct itimerval value, ovalue, *pvalue;
4383 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
4384 || copy_from_user_timeval(&pvalue->it_value,
4385 arg2 + sizeof(struct target_timeval)))
4390 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
4391 if (!is_error(ret) && arg3) {
4392 if (copy_to_user_timeval(arg3,
4393 &ovalue.it_interval)
4394 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
4400 case TARGET_NR_getitimer:
4402 struct itimerval value;
4404 ret = get_errno(getitimer(arg1, &value));
4405 if (!is_error(ret) && arg2) {
4406 if (copy_to_user_timeval(arg2,
4408 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
4414 case TARGET_NR_stat:
4415 if (!(p = lock_user_string(arg1)))
4417 ret = get_errno(stat(path(p), &st));
4418 unlock_user(p, arg1, 0);
4420 case TARGET_NR_lstat:
4421 if (!(p = lock_user_string(arg1)))
4423 ret = get_errno(lstat(path(p), &st));
4424 unlock_user(p, arg1, 0);
4426 case TARGET_NR_fstat:
4428 ret = get_errno(fstat(arg1, &st));
4430 if (!is_error(ret)) {
4431 struct target_stat *target_st;
4433 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4435 __put_user(st.st_dev, &target_st->st_dev);
4436 __put_user(st.st_ino, &target_st->st_ino);
4437 __put_user(st.st_mode, &target_st->st_mode);
4438 __put_user(st.st_uid, &target_st->st_uid);
4439 __put_user(st.st_gid, &target_st->st_gid);
4440 __put_user(st.st_nlink, &target_st->st_nlink);
4441 __put_user(st.st_rdev, &target_st->st_rdev);
4442 __put_user(st.st_size, &target_st->st_size);
4443 __put_user(st.st_blksize, &target_st->st_blksize);
4444 __put_user(st.st_blocks, &target_st->st_blocks);
4445 __put_user(st.st_atime, &target_st->target_st_atime);
4446 __put_user(st.st_mtime, &target_st->target_st_mtime);
4447 __put_user(st.st_ctime, &target_st->target_st_ctime);
4448 unlock_user_struct(target_st, arg2, 1);
4452 #ifdef TARGET_NR_olduname
4453 case TARGET_NR_olduname:
4456 #ifdef TARGET_NR_iopl
4457 case TARGET_NR_iopl:
4460 case TARGET_NR_vhangup:
4461 ret = get_errno(vhangup());
4463 #ifdef TARGET_NR_idle
4464 case TARGET_NR_idle:
4467 #ifdef TARGET_NR_syscall
4468 case TARGET_NR_syscall:
4469 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4472 case TARGET_NR_wait4:
4475 abi_long status_ptr = arg2;
4476 struct rusage rusage, *rusage_ptr;
4477 abi_ulong target_rusage = arg4;
4479 rusage_ptr = &rusage;
4482 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4483 if (!is_error(ret)) {
4485 if (put_user_s32(status, status_ptr))
4489 host_to_target_rusage(target_rusage, &rusage);
4493 #ifdef TARGET_NR_swapoff
4494 case TARGET_NR_swapoff:
4495 if (!(p = lock_user_string(arg1)))
4497 ret = get_errno(swapoff(p));
4498 unlock_user(p, arg1, 0);
4501 case TARGET_NR_sysinfo:
4503 struct target_sysinfo *target_value;
4504 struct sysinfo value;
4505 ret = get_errno(sysinfo(&value));
4506 if (!is_error(ret) && arg1)
4508 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4510 __put_user(value.uptime, &target_value->uptime);
4511 __put_user(value.loads[0], &target_value->loads[0]);
4512 __put_user(value.loads[1], &target_value->loads[1]);
4513 __put_user(value.loads[2], &target_value->loads[2]);
4514 __put_user(value.totalram, &target_value->totalram);
4515 __put_user(value.freeram, &target_value->freeram);
4516 __put_user(value.sharedram, &target_value->sharedram);
4517 __put_user(value.bufferram, &target_value->bufferram);
4518 __put_user(value.totalswap, &target_value->totalswap);
4519 __put_user(value.freeswap, &target_value->freeswap);
4520 __put_user(value.procs, &target_value->procs);
4521 __put_user(value.totalhigh, &target_value->totalhigh);
4522 __put_user(value.freehigh, &target_value->freehigh);
4523 __put_user(value.mem_unit, &target_value->mem_unit);
4524 unlock_user_struct(target_value, arg1, 1);
4528 #ifdef TARGET_NR_ipc
4530 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4533 case TARGET_NR_fsync:
4534 ret = get_errno(fsync(arg1));
4536 case TARGET_NR_clone:
4537 ret = get_errno(do_fork(cpu_env, arg1, arg2));
4539 #ifdef __NR_exit_group
4540 /* new thread calls */
4541 case TARGET_NR_exit_group:
4542 gdb_exit(cpu_env, arg1);
4543 ret = get_errno(exit_group(arg1));
4546 case TARGET_NR_setdomainname:
4547 if (!(p = lock_user_string(arg1)))
4549 ret = get_errno(setdomainname(p, arg2));
4550 unlock_user(p, arg1, 0);
4552 case TARGET_NR_uname:
4553 /* no need to transcode because we use the linux syscall */
4555 struct new_utsname * buf;
4557 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4559 ret = get_errno(sys_uname(buf));
4560 if (!is_error(ret)) {
4561 /* Overrite the native machine name with whatever is being
4563 strcpy (buf->machine, UNAME_MACHINE);
4564 /* Allow the user to override the reported release. */
4565 if (qemu_uname_release && *qemu_uname_release)
4566 strcpy (buf->release, qemu_uname_release);
4568 unlock_user_struct(buf, arg1, 1);
4572 case TARGET_NR_modify_ldt:
4573 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
4575 #if !defined(TARGET_X86_64)
4576 case TARGET_NR_vm86old:
4578 case TARGET_NR_vm86:
4579 ret = do_vm86(cpu_env, arg1, arg2);
4583 case TARGET_NR_adjtimex:
4585 #ifdef TARGET_NR_create_module
4586 case TARGET_NR_create_module:
4588 case TARGET_NR_init_module:
4589 case TARGET_NR_delete_module:
4590 #ifdef TARGET_NR_get_kernel_syms
4591 case TARGET_NR_get_kernel_syms:
4594 case TARGET_NR_quotactl:
4596 case TARGET_NR_getpgid:
4597 ret = get_errno(getpgid(arg1));
4599 case TARGET_NR_fchdir:
4600 ret = get_errno(fchdir(arg1));
4602 #ifdef TARGET_NR_bdflush /* not on x86_64 */
4603 case TARGET_NR_bdflush:
4606 #ifdef TARGET_NR_sysfs
4607 case TARGET_NR_sysfs:
4610 case TARGET_NR_personality:
4611 ret = get_errno(personality(arg1));
4613 #ifdef TARGET_NR_afs_syscall
4614 case TARGET_NR_afs_syscall:
4617 #ifdef TARGET_NR__llseek /* Not on alpha */
4618 case TARGET_NR__llseek:
4620 #if defined (__x86_64__)
4621 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
4622 if (put_user_s64(ret, arg4))
4626 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4627 if (put_user_s64(res, arg4))
4633 case TARGET_NR_getdents:
4634 #if TARGET_ABI_BITS != 32
4636 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4638 struct target_dirent *target_dirp;
4639 struct dirent *dirp;
4640 abi_long count = arg3;
4642 dirp = malloc(count);
4644 ret = -TARGET_ENOMEM;
4648 ret = get_errno(sys_getdents(arg1, dirp, count));
4649 if (!is_error(ret)) {
4651 struct target_dirent *tde;
4653 int reclen, treclen;
4654 int count1, tnamelen;
4658 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4662 reclen = de->d_reclen;
4663 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4664 tde->d_reclen = tswap16(treclen);
4665 tde->d_ino = tswapl(de->d_ino);
4666 tde->d_off = tswapl(de->d_off);
4667 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4670 /* XXX: may not be correct */
4671 strncpy(tde->d_name, de->d_name, tnamelen);
4672 de = (struct dirent *)((char *)de + reclen);
4674 tde = (struct target_dirent *)((char *)tde + treclen);
4678 unlock_user(target_dirp, arg2, ret);
4684 struct dirent *dirp;
4685 abi_long count = arg3;
4687 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4689 ret = get_errno(sys_getdents(arg1, dirp, count));
4690 if (!is_error(ret)) {
4696 reclen = de->d_reclen;
4699 de->d_reclen = tswap16(reclen);
4700 tswapls(&de->d_ino);
4701 tswapls(&de->d_off);
4702 de = (struct dirent *)((char *)de + reclen);
4706 unlock_user(dirp, arg2, ret);
4710 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
4711 case TARGET_NR_getdents64:
4713 struct dirent64 *dirp;
4714 abi_long count = arg3;
4715 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4717 ret = get_errno(sys_getdents64(arg1, dirp, count));
4718 if (!is_error(ret)) {
4719 struct dirent64 *de;
4724 reclen = de->d_reclen;
4727 de->d_reclen = tswap16(reclen);
4728 tswap64s((uint64_t *)&de->d_ino);
4729 tswap64s((uint64_t *)&de->d_off);
4730 de = (struct dirent64 *)((char *)de + reclen);
4734 unlock_user(dirp, arg2, ret);
4737 #endif /* TARGET_NR_getdents64 */
4738 #ifdef TARGET_NR__newselect
4739 case TARGET_NR__newselect:
4740 ret = do_select(arg1, arg2, arg3, arg4, arg5);
4743 #ifdef TARGET_NR_poll
4744 case TARGET_NR_poll:
4746 struct target_pollfd *target_pfd;
4747 unsigned int nfds = arg2;
4752 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4755 pfd = alloca(sizeof(struct pollfd) * nfds);
4756 for(i = 0; i < nfds; i++) {
4757 pfd[i].fd = tswap32(target_pfd[i].fd);
4758 pfd[i].events = tswap16(target_pfd[i].events);
4760 ret = get_errno(poll(pfd, nfds, timeout));
4761 if (!is_error(ret)) {
4762 for(i = 0; i < nfds; i++) {
4763 target_pfd[i].revents = tswap16(pfd[i].revents);
4765 ret += nfds * (sizeof(struct target_pollfd)
4766 - sizeof(struct pollfd));
4768 unlock_user(target_pfd, arg1, ret);
4772 case TARGET_NR_flock:
4773 /* NOTE: the flock constant seems to be the same for every
4775 ret = get_errno(flock(arg1, arg2));
4777 case TARGET_NR_readv:
4782 vec = alloca(count * sizeof(struct iovec));
4783 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
4785 ret = get_errno(readv(arg1, vec, count));
4786 unlock_iovec(vec, arg2, count, 1);
4789 case TARGET_NR_writev:
4794 vec = alloca(count * sizeof(struct iovec));
4795 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
4797 ret = get_errno(writev(arg1, vec, count));
4798 unlock_iovec(vec, arg2, count, 0);
4801 case TARGET_NR_getsid:
4802 ret = get_errno(getsid(arg1));
4804 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
4805 case TARGET_NR_fdatasync:
4806 ret = get_errno(fdatasync(arg1));
4809 case TARGET_NR__sysctl:
4810 /* We don't implement this, but ENOTDIR is always a safe
4812 ret = -TARGET_ENOTDIR;
4814 case TARGET_NR_sched_setparam:
4816 struct sched_param *target_schp;
4817 struct sched_param schp;
4819 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4821 schp.sched_priority = tswap32(target_schp->sched_priority);
4822 unlock_user_struct(target_schp, arg2, 0);
4823 ret = get_errno(sched_setparam(arg1, &schp));
4826 case TARGET_NR_sched_getparam:
4828 struct sched_param *target_schp;
4829 struct sched_param schp;
4830 ret = get_errno(sched_getparam(arg1, &schp));
4831 if (!is_error(ret)) {
4832 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4834 target_schp->sched_priority = tswap32(schp.sched_priority);
4835 unlock_user_struct(target_schp, arg2, 1);
4839 case TARGET_NR_sched_setscheduler:
4841 struct sched_param *target_schp;
4842 struct sched_param schp;
4843 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4845 schp.sched_priority = tswap32(target_schp->sched_priority);
4846 unlock_user_struct(target_schp, arg3, 0);
4847 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4850 case TARGET_NR_sched_getscheduler:
4851 ret = get_errno(sched_getscheduler(arg1));
4853 case TARGET_NR_sched_yield:
4854 ret = get_errno(sched_yield());
4856 case TARGET_NR_sched_get_priority_max:
4857 ret = get_errno(sched_get_priority_max(arg1));
4859 case TARGET_NR_sched_get_priority_min:
4860 ret = get_errno(sched_get_priority_min(arg1));
4862 case TARGET_NR_sched_rr_get_interval:
4865 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4866 if (!is_error(ret)) {
4867 host_to_target_timespec(arg2, &ts);
4871 case TARGET_NR_nanosleep:
4873 struct timespec req, rem;
4874 target_to_host_timespec(&req, arg1);
4875 ret = get_errno(nanosleep(&req, &rem));
4876 if (is_error(ret) && arg2) {
4877 host_to_target_timespec(arg2, &rem);
4881 #ifdef TARGET_NR_query_module
4882 case TARGET_NR_query_module:
4885 #ifdef TARGET_NR_nfsservctl
4886 case TARGET_NR_nfsservctl:
4889 case TARGET_NR_prctl:
4892 case PR_GET_PDEATHSIG:
4895 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4896 if (!is_error(ret) && arg2
4897 && put_user_ual(deathsig, arg2))
4902 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4906 #ifdef TARGET_NR_arch_prctl
4907 case TARGET_NR_arch_prctl:
4908 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
4909 ret = do_arch_prctl(cpu_env, arg1, arg2);
4915 #ifdef TARGET_NR_pread
4916 case TARGET_NR_pread:
4917 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4919 ret = get_errno(pread(arg1, p, arg3, arg4));
4920 unlock_user(p, arg2, ret);
4922 case TARGET_NR_pwrite:
4923 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4925 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4926 unlock_user(p, arg2, 0);
4929 #ifdef TARGET_NR_pread64
4930 case TARGET_NR_pread64:
4931 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4933 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
4934 unlock_user(p, arg2, ret);
4936 case TARGET_NR_pwrite64:
4937 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4939 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
4940 unlock_user(p, arg2, 0);
4943 case TARGET_NR_getcwd:
4944 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4946 ret = get_errno(sys_getcwd1(p, arg2));
4947 unlock_user(p, arg1, ret);
4949 case TARGET_NR_capget:
4951 case TARGET_NR_capset:
4953 case TARGET_NR_sigaltstack:
4954 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4955 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
4956 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
4961 case TARGET_NR_sendfile:
4963 #ifdef TARGET_NR_getpmsg
4964 case TARGET_NR_getpmsg:
4967 #ifdef TARGET_NR_putpmsg
4968 case TARGET_NR_putpmsg:
4971 #ifdef TARGET_NR_vfork
4972 case TARGET_NR_vfork:
4973 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
4976 #ifdef TARGET_NR_ugetrlimit
4977 case TARGET_NR_ugetrlimit:
4980 ret = get_errno(getrlimit(arg1, &rlim));
4981 if (!is_error(ret)) {
4982 struct target_rlimit *target_rlim;
4983 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4985 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4986 target_rlim->rlim_max = tswapl(rlim.rlim_max);
4987 unlock_user_struct(target_rlim, arg2, 1);
4992 #ifdef TARGET_NR_truncate64
4993 case TARGET_NR_truncate64:
4994 if (!(p = lock_user_string(arg1)))
4996 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4997 unlock_user(p, arg1, 0);
5000 #ifdef TARGET_NR_ftruncate64
5001 case TARGET_NR_ftruncate64:
5002 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
5005 #ifdef TARGET_NR_stat64
5006 case TARGET_NR_stat64:
5007 if (!(p = lock_user_string(arg1)))
5009 ret = get_errno(stat(path(p), &st));
5010 unlock_user(p, arg1, 0);
5013 #ifdef TARGET_NR_lstat64
5014 case TARGET_NR_lstat64:
5015 if (!(p = lock_user_string(arg1)))
5017 ret = get_errno(lstat(path(p), &st));
5018 unlock_user(p, arg1, 0);
5021 #ifdef TARGET_NR_fstat64
5022 case TARGET_NR_fstat64:
5024 ret = get_errno(fstat(arg1, &st));
5026 if (!is_error(ret)) {
5028 if (((CPUARMState *)cpu_env)->eabi) {
5029 struct target_eabi_stat64 *target_st;
5031 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5033 memset(target_st, 0, sizeof(struct target_eabi_stat64));
5034 __put_user(st.st_dev, &target_st->st_dev);
5035 __put_user(st.st_ino, &target_st->st_ino);
5036 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5037 __put_user(st.st_ino, &target_st->__st_ino);
5039 __put_user(st.st_mode, &target_st->st_mode);
5040 __put_user(st.st_nlink, &target_st->st_nlink);
5041 __put_user(st.st_uid, &target_st->st_uid);
5042 __put_user(st.st_gid, &target_st->st_gid);
5043 __put_user(st.st_rdev, &target_st->st_rdev);
5044 __put_user(st.st_size, &target_st->st_size);
5045 __put_user(st.st_blksize, &target_st->st_blksize);
5046 __put_user(st.st_blocks, &target_st->st_blocks);
5047 __put_user(st.st_atime, &target_st->target_st_atime);
5048 __put_user(st.st_mtime, &target_st->target_st_mtime);
5049 __put_user(st.st_ctime, &target_st->target_st_ctime);
5050 unlock_user_struct(target_st, arg2, 1);
5054 struct target_stat64 *target_st;
5056 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5058 memset(target_st, 0, sizeof(struct target_stat64));
5059 __put_user(st.st_dev, &target_st->st_dev);
5060 __put_user(st.st_ino, &target_st->st_ino);
5061 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5062 __put_user(st.st_ino, &target_st->__st_ino);
5064 __put_user(st.st_mode, &target_st->st_mode);
5065 __put_user(st.st_nlink, &target_st->st_nlink);
5066 __put_user(st.st_uid, &target_st->st_uid);
5067 __put_user(st.st_gid, &target_st->st_gid);
5068 __put_user(st.st_rdev, &target_st->st_rdev);
5069 /* XXX: better use of kernel struct */
5070 __put_user(st.st_size, &target_st->st_size);
5071 __put_user(st.st_blksize, &target_st->st_blksize);
5072 __put_user(st.st_blocks, &target_st->st_blocks);
5073 __put_user(st.st_atime, &target_st->target_st_atime);
5074 __put_user(st.st_mtime, &target_st->target_st_mtime);
5075 __put_user(st.st_ctime, &target_st->target_st_ctime);
5076 unlock_user_struct(target_st, arg2, 1);
5083 case TARGET_NR_lchown:
5084 if (!(p = lock_user_string(arg1)))
5086 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5087 unlock_user(p, arg1, 0);
5089 case TARGET_NR_getuid:
5090 ret = get_errno(high2lowuid(getuid()));
5092 case TARGET_NR_getgid:
5093 ret = get_errno(high2lowgid(getgid()));
5095 case TARGET_NR_geteuid:
5096 ret = get_errno(high2lowuid(geteuid()));
5098 case TARGET_NR_getegid:
5099 ret = get_errno(high2lowgid(getegid()));
5101 case TARGET_NR_setreuid:
5102 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5104 case TARGET_NR_setregid:
5105 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5107 case TARGET_NR_getgroups:
5109 int gidsetsize = arg1;
5110 uint16_t *target_grouplist;
5114 grouplist = alloca(gidsetsize * sizeof(gid_t));
5115 ret = get_errno(getgroups(gidsetsize, grouplist));
5116 if (!is_error(ret)) {
5117 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5118 if (!target_grouplist)
5120 for(i = 0;i < gidsetsize; i++)
5121 target_grouplist[i] = tswap16(grouplist[i]);
5122 unlock_user(target_grouplist, arg2, gidsetsize * 2);
5126 case TARGET_NR_setgroups:
5128 int gidsetsize = arg1;
5129 uint16_t *target_grouplist;
5133 grouplist = alloca(gidsetsize * sizeof(gid_t));
5134 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5135 if (!target_grouplist) {
5136 ret = -TARGET_EFAULT;
5139 for(i = 0;i < gidsetsize; i++)
5140 grouplist[i] = tswap16(target_grouplist[i]);
5141 unlock_user(target_grouplist, arg2, 0);
5142 ret = get_errno(setgroups(gidsetsize, grouplist));
5145 case TARGET_NR_fchown:
5146 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5148 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5149 case TARGET_NR_fchownat:
5150 if (!(p = lock_user_string(arg2)))
5152 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5153 unlock_user(p, arg2, 0);
5156 #ifdef TARGET_NR_setresuid
5157 case TARGET_NR_setresuid:
5158 ret = get_errno(setresuid(low2highuid(arg1),
5160 low2highuid(arg3)));
5163 #ifdef TARGET_NR_getresuid
5164 case TARGET_NR_getresuid:
5166 uid_t ruid, euid, suid;
5167 ret = get_errno(getresuid(&ruid, &euid, &suid));
5168 if (!is_error(ret)) {
5169 if (put_user_u16(high2lowuid(ruid), arg1)
5170 || put_user_u16(high2lowuid(euid), arg2)
5171 || put_user_u16(high2lowuid(suid), arg3))
5177 #ifdef TARGET_NR_getresgid
5178 case TARGET_NR_setresgid:
5179 ret = get_errno(setresgid(low2highgid(arg1),
5181 low2highgid(arg3)));
5184 #ifdef TARGET_NR_getresgid
5185 case TARGET_NR_getresgid:
5187 gid_t rgid, egid, sgid;
5188 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5189 if (!is_error(ret)) {
5190 if (put_user_u16(high2lowgid(rgid), arg1)
5191 || put_user_u16(high2lowgid(egid), arg2)
5192 || put_user_u16(high2lowgid(sgid), arg3))
5198 case TARGET_NR_chown:
5199 if (!(p = lock_user_string(arg1)))
5201 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5202 unlock_user(p, arg1, 0);
5204 case TARGET_NR_setuid:
5205 ret = get_errno(setuid(low2highuid(arg1)));
5207 case TARGET_NR_setgid:
5208 ret = get_errno(setgid(low2highgid(arg1)));
5210 case TARGET_NR_setfsuid:
5211 ret = get_errno(setfsuid(arg1));
5213 case TARGET_NR_setfsgid:
5214 ret = get_errno(setfsgid(arg1));
5216 #endif /* USE_UID16 */
5218 #ifdef TARGET_NR_lchown32
5219 case TARGET_NR_lchown32:
5220 if (!(p = lock_user_string(arg1)))
5222 ret = get_errno(lchown(p, arg2, arg3));
5223 unlock_user(p, arg1, 0);
5226 #ifdef TARGET_NR_getuid32
5227 case TARGET_NR_getuid32:
5228 ret = get_errno(getuid());
5231 #ifdef TARGET_NR_getgid32
5232 case TARGET_NR_getgid32:
5233 ret = get_errno(getgid());
5236 #ifdef TARGET_NR_geteuid32
5237 case TARGET_NR_geteuid32:
5238 ret = get_errno(geteuid());
5241 #ifdef TARGET_NR_getegid32
5242 case TARGET_NR_getegid32:
5243 ret = get_errno(getegid());
5246 #ifdef TARGET_NR_setreuid32
5247 case TARGET_NR_setreuid32:
5248 ret = get_errno(setreuid(arg1, arg2));
5251 #ifdef TARGET_NR_setregid32
5252 case TARGET_NR_setregid32:
5253 ret = get_errno(setregid(arg1, arg2));
5256 #ifdef TARGET_NR_getgroups32
5257 case TARGET_NR_getgroups32:
5259 int gidsetsize = arg1;
5260 uint32_t *target_grouplist;
5264 grouplist = alloca(gidsetsize * sizeof(gid_t));
5265 ret = get_errno(getgroups(gidsetsize, grouplist));
5266 if (!is_error(ret)) {
5267 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
5268 if (!target_grouplist) {
5269 ret = -TARGET_EFAULT;
5272 for(i = 0;i < gidsetsize; i++)
5273 target_grouplist[i] = tswap32(grouplist[i]);
5274 unlock_user(target_grouplist, arg2, gidsetsize * 4);
5279 #ifdef TARGET_NR_setgroups32
5280 case TARGET_NR_setgroups32:
5282 int gidsetsize = arg1;
5283 uint32_t *target_grouplist;
5287 grouplist = alloca(gidsetsize * sizeof(gid_t));
5288 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
5289 if (!target_grouplist) {
5290 ret = -TARGET_EFAULT;
5293 for(i = 0;i < gidsetsize; i++)
5294 grouplist[i] = tswap32(target_grouplist[i]);
5295 unlock_user(target_grouplist, arg2, 0);
5296 ret = get_errno(setgroups(gidsetsize, grouplist));
5300 #ifdef TARGET_NR_fchown32
5301 case TARGET_NR_fchown32:
5302 ret = get_errno(fchown(arg1, arg2, arg3));
5305 #ifdef TARGET_NR_setresuid32
5306 case TARGET_NR_setresuid32:
5307 ret = get_errno(setresuid(arg1, arg2, arg3));
5310 #ifdef TARGET_NR_getresuid32
5311 case TARGET_NR_getresuid32:
5313 uid_t ruid, euid, suid;
5314 ret = get_errno(getresuid(&ruid, &euid, &suid));
5315 if (!is_error(ret)) {
5316 if (put_user_u32(ruid, arg1)
5317 || put_user_u32(euid, arg2)
5318 || put_user_u32(suid, arg3))
5324 #ifdef TARGET_NR_setresgid32
5325 case TARGET_NR_setresgid32:
5326 ret = get_errno(setresgid(arg1, arg2, arg3));
5329 #ifdef TARGET_NR_getresgid32
5330 case TARGET_NR_getresgid32:
5332 gid_t rgid, egid, sgid;
5333 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5334 if (!is_error(ret)) {
5335 if (put_user_u32(rgid, arg1)
5336 || put_user_u32(egid, arg2)
5337 || put_user_u32(sgid, arg3))
5343 #ifdef TARGET_NR_chown32
5344 case TARGET_NR_chown32:
5345 if (!(p = lock_user_string(arg1)))
5347 ret = get_errno(chown(p, arg2, arg3));
5348 unlock_user(p, arg1, 0);
5351 #ifdef TARGET_NR_setuid32
5352 case TARGET_NR_setuid32:
5353 ret = get_errno(setuid(arg1));
5356 #ifdef TARGET_NR_setgid32
5357 case TARGET_NR_setgid32:
5358 ret = get_errno(setgid(arg1));
5361 #ifdef TARGET_NR_setfsuid32
5362 case TARGET_NR_setfsuid32:
5363 ret = get_errno(setfsuid(arg1));
5366 #ifdef TARGET_NR_setfsgid32
5367 case TARGET_NR_setfsgid32:
5368 ret = get_errno(setfsgid(arg1));
5372 case TARGET_NR_pivot_root:
5374 #ifdef TARGET_NR_mincore
5375 case TARGET_NR_mincore:
5378 #ifdef TARGET_NR_madvise
5379 case TARGET_NR_madvise:
5380 /* A straight passthrough may not be safe because qemu sometimes
5381 turns private flie-backed mappings into anonymous mappings.
5382 This will break MADV_DONTNEED.
5383 This is a hint, so ignoring and returning success is ok. */
5387 #if TARGET_ABI_BITS == 32
5388 case TARGET_NR_fcntl64:
5392 struct target_flock64 *target_fl;
5394 struct target_eabi_flock64 *target_efl;
5398 case TARGET_F_GETLK64:
5401 case TARGET_F_SETLK64:
5404 case TARGET_F_SETLKW64:
5413 case TARGET_F_GETLK64:
5415 if (((CPUARMState *)cpu_env)->eabi) {
5416 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5418 fl.l_type = tswap16(target_efl->l_type);
5419 fl.l_whence = tswap16(target_efl->l_whence);
5420 fl.l_start = tswap64(target_efl->l_start);
5421 fl.l_len = tswap64(target_efl->l_len);
5422 fl.l_pid = tswapl(target_efl->l_pid);
5423 unlock_user_struct(target_efl, arg3, 0);
5427 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5429 fl.l_type = tswap16(target_fl->l_type);
5430 fl.l_whence = tswap16(target_fl->l_whence);
5431 fl.l_start = tswap64(target_fl->l_start);
5432 fl.l_len = tswap64(target_fl->l_len);
5433 fl.l_pid = tswapl(target_fl->l_pid);
5434 unlock_user_struct(target_fl, arg3, 0);
5436 ret = get_errno(fcntl(arg1, cmd, &fl));
5439 if (((CPUARMState *)cpu_env)->eabi) {
5440 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5442 target_efl->l_type = tswap16(fl.l_type);
5443 target_efl->l_whence = tswap16(fl.l_whence);
5444 target_efl->l_start = tswap64(fl.l_start);
5445 target_efl->l_len = tswap64(fl.l_len);
5446 target_efl->l_pid = tswapl(fl.l_pid);
5447 unlock_user_struct(target_efl, arg3, 1);
5451 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5453 target_fl->l_type = tswap16(fl.l_type);
5454 target_fl->l_whence = tswap16(fl.l_whence);
5455 target_fl->l_start = tswap64(fl.l_start);
5456 target_fl->l_len = tswap64(fl.l_len);
5457 target_fl->l_pid = tswapl(fl.l_pid);
5458 unlock_user_struct(target_fl, arg3, 1);
5463 case TARGET_F_SETLK64:
5464 case TARGET_F_SETLKW64:
5466 if (((CPUARMState *)cpu_env)->eabi) {
5467 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5469 fl.l_type = tswap16(target_efl->l_type);
5470 fl.l_whence = tswap16(target_efl->l_whence);
5471 fl.l_start = tswap64(target_efl->l_start);
5472 fl.l_len = tswap64(target_efl->l_len);
5473 fl.l_pid = tswapl(target_efl->l_pid);
5474 unlock_user_struct(target_efl, arg3, 0);
5478 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5480 fl.l_type = tswap16(target_fl->l_type);
5481 fl.l_whence = tswap16(target_fl->l_whence);
5482 fl.l_start = tswap64(target_fl->l_start);
5483 fl.l_len = tswap64(target_fl->l_len);
5484 fl.l_pid = tswapl(target_fl->l_pid);
5485 unlock_user_struct(target_fl, arg3, 0);
5487 ret = get_errno(fcntl(arg1, cmd, &fl));
5490 ret = do_fcntl(arg1, cmd, arg3);
5496 #ifdef TARGET_NR_cacheflush
5497 case TARGET_NR_cacheflush:
5498 /* self-modifying code is handled automatically, so nothing needed */
5502 #ifdef TARGET_NR_security
5503 case TARGET_NR_security:
5506 #ifdef TARGET_NR_getpagesize
5507 case TARGET_NR_getpagesize:
5508 ret = TARGET_PAGE_SIZE;
5511 case TARGET_NR_gettid:
5512 ret = get_errno(gettid());
5514 #ifdef TARGET_NR_readahead
5515 case TARGET_NR_readahead:
5518 #ifdef TARGET_NR_setxattr
5519 case TARGET_NR_setxattr:
5520 case TARGET_NR_lsetxattr:
5521 case TARGET_NR_fsetxattr:
5522 case TARGET_NR_getxattr:
5523 case TARGET_NR_lgetxattr:
5524 case TARGET_NR_fgetxattr:
5525 case TARGET_NR_listxattr:
5526 case TARGET_NR_llistxattr:
5527 case TARGET_NR_flistxattr:
5528 case TARGET_NR_removexattr:
5529 case TARGET_NR_lremovexattr:
5530 case TARGET_NR_fremovexattr:
5531 goto unimplemented_nowarn;
5533 #ifdef TARGET_NR_set_thread_area
5534 case TARGET_NR_set_thread_area:
5535 #if defined(TARGET_MIPS)
5536 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5539 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
5540 ret = do_set_thread_area(cpu_env, arg1);
5543 goto unimplemented_nowarn;
5546 #ifdef TARGET_NR_get_thread_area
5547 case TARGET_NR_get_thread_area:
5548 #if defined(TARGET_I386) && defined(TARGET_ABI32)
5549 ret = do_get_thread_area(cpu_env, arg1);
5551 goto unimplemented_nowarn;
5554 #ifdef TARGET_NR_getdomainname
5555 case TARGET_NR_getdomainname:
5556 goto unimplemented_nowarn;
5559 #ifdef TARGET_NR_clock_gettime
5560 case TARGET_NR_clock_gettime:
5563 ret = get_errno(clock_gettime(arg1, &ts));
5564 if (!is_error(ret)) {
5565 host_to_target_timespec(arg2, &ts);
5570 #ifdef TARGET_NR_clock_getres
5571 case TARGET_NR_clock_getres:
5574 ret = get_errno(clock_getres(arg1, &ts));
5575 if (!is_error(ret)) {
5576 host_to_target_timespec(arg2, &ts);
5581 #ifdef TARGET_NR_clock_nanosleep
5582 case TARGET_NR_clock_nanosleep:
5585 target_to_host_timespec(&ts, arg3);
5586 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
5588 host_to_target_timespec(arg4, &ts);
5593 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5594 case TARGET_NR_set_tid_address:
5595 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5599 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
5600 case TARGET_NR_tkill:
5601 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
5605 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
5606 case TARGET_NR_tgkill:
5607 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
5608 target_to_host_signal(arg3)));
5612 #ifdef TARGET_NR_set_robust_list
5613 case TARGET_NR_set_robust_list:
5614 goto unimplemented_nowarn;
5617 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5618 case TARGET_NR_utimensat:
5620 struct timespec ts[2];
5621 target_to_host_timespec(ts, arg3);
5622 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5624 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5626 if (!(p = lock_user_string(arg2))) {
5627 ret = -TARGET_EFAULT;
5630 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5631 unlock_user(p, arg2, 0);
5636 #if defined(USE_NPTL)
5637 case TARGET_NR_futex:
5638 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
5644 gemu_log("qemu: Unsupported syscall: %d\n", num);
5645 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5646 unimplemented_nowarn:
5648 ret = -TARGET_ENOSYS;
5653 gemu_log(" = %ld\n", ret);
5656 print_syscall_ret(num, ret);
5659 ret = -TARGET_EFAULT;