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.
31 #include <sys/types.h>
37 #include <sys/mount.h>
38 #include <sys/prctl.h>
39 #include <sys/resource.h>
44 #include <sys/socket.h>
47 #include <sys/times.h>
50 #include <sys/statfs.h>
52 #include <sys/sysinfo.h>
53 //#include <sys/user.h>
54 #include <netinet/ip.h>
55 #include <netinet/tcp.h>
56 #include <qemu-common.h>
61 #define termios host_termios
62 #define winsize host_winsize
63 #define termio host_termio
64 #define sgttyb host_sgttyb /* same as target */
65 #define tchars host_tchars /* same as target */
66 #define ltchars host_ltchars /* same as target */
68 #include <linux/termios.h>
69 #include <linux/unistd.h>
70 #include <linux/utsname.h>
71 #include <linux/cdrom.h>
72 #include <linux/hdreg.h>
73 #include <linux/soundcard.h>
75 #include <linux/mtio.h>
76 #include "linux_loop.h"
79 #include "qemu-common.h"
82 #include <linux/futex.h>
83 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
84 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
86 /* XXX: Hardcode the above values. */
87 #define CLONE_NPTL_FLAGS2 0
92 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
93 || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
94 /* 16 bit uid wrappers emulation */
98 //#include <linux/msdos_fs.h>
99 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
100 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
111 #define _syscall0(type,name) \
112 static type name (void) \
114 return syscall(__NR_##name); \
117 #define _syscall1(type,name,type1,arg1) \
118 static type name (type1 arg1) \
120 return syscall(__NR_##name, arg1); \
123 #define _syscall2(type,name,type1,arg1,type2,arg2) \
124 static type name (type1 arg1,type2 arg2) \
126 return syscall(__NR_##name, arg1, arg2); \
129 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
130 static type name (type1 arg1,type2 arg2,type3 arg3) \
132 return syscall(__NR_##name, arg1, arg2, arg3); \
135 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
136 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
138 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
141 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
143 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
145 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
149 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
150 type5,arg5,type6,arg6) \
151 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
154 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
158 #define __NR_sys_exit __NR_exit
159 #define __NR_sys_uname __NR_uname
160 #define __NR_sys_faccessat __NR_faccessat
161 #define __NR_sys_fchmodat __NR_fchmodat
162 #define __NR_sys_fchownat __NR_fchownat
163 #define __NR_sys_fstatat64 __NR_fstatat64
164 #define __NR_sys_futimesat __NR_futimesat
165 #define __NR_sys_getcwd1 __NR_getcwd
166 #define __NR_sys_getdents __NR_getdents
167 #define __NR_sys_getdents64 __NR_getdents64
168 #define __NR_sys_getpriority __NR_getpriority
169 #define __NR_sys_linkat __NR_linkat
170 #define __NR_sys_mkdirat __NR_mkdirat
171 #define __NR_sys_mknodat __NR_mknodat
172 #define __NR_sys_openat __NR_openat
173 #define __NR_sys_readlinkat __NR_readlinkat
174 #define __NR_sys_renameat __NR_renameat
175 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
176 #define __NR_sys_symlinkat __NR_symlinkat
177 #define __NR_sys_syslog __NR_syslog
178 #define __NR_sys_tgkill __NR_tgkill
179 #define __NR_sys_tkill __NR_tkill
180 #define __NR_sys_unlinkat __NR_unlinkat
181 #define __NR_sys_utimensat __NR_utimensat
182 #define __NR_sys_futex __NR_futex
183 #define __NR_sys_inotify_init __NR_inotify_init
184 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
185 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
187 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
188 #define __NR__llseek __NR_lseek
192 _syscall0(int, gettid)
194 /* This is a replacement for the host gettid() and must return a host
196 static int gettid(void) {
200 _syscall1(int,sys_exit,int,status)
201 _syscall1(int,sys_uname,struct new_utsname *,buf)
202 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
203 _syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
205 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
206 _syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
207 mode_t,mode,int,flags)
209 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
210 _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
211 uid_t,owner,gid_t,group,int,flags)
213 #if defined(TARGET_NR_fstatat64) && defined(__NR_fstatat64)
214 _syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
215 struct stat *,buf,int,flags)
217 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
218 _syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
219 const struct timeval *,times)
221 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
222 #if TARGET_ABI_BITS == 32
223 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
225 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
226 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
228 _syscall2(int, sys_getpriority, int, which, int, who);
229 #if !defined (__x86_64__)
230 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
231 loff_t *, res, uint, wh);
233 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
234 _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
235 int,newdirfd,const char *,newpath,int,flags)
237 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
238 _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
240 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
241 _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
242 mode_t,mode,dev_t,dev)
244 #if defined(TARGET_NR_openat) && defined(__NR_openat)
245 _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
247 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
248 _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
249 char *,buf,size_t,bufsize)
251 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
252 _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
253 int,newdirfd,const char *,newpath)
255 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
256 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
257 _syscall3(int,sys_symlinkat,const char *,oldpath,
258 int,newdirfd,const char *,newpath)
260 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
261 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
262 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
264 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
265 _syscall2(int,sys_tkill,int,tid,int,sig)
267 #ifdef __NR_exit_group
268 _syscall1(int,exit_group,int,error_code)
270 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
271 _syscall1(int,set_tid_address,int *,tidptr)
273 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
274 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
276 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
277 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
278 const struct timespec *,tsp,int,flags)
280 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
281 _syscall0(int,sys_inotify_init)
283 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
284 _syscall3(int,sys_inotify_add_watch,int,fd,const char *,pathname,uint32_t,mask)
286 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
287 _syscall2(int,sys_inotify_rm_watch,int,fd,uint32_t,wd)
289 #if defined(USE_NPTL)
290 #if defined(TARGET_NR_futex) && defined(__NR_futex)
291 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
292 const struct timespec *,timeout,int *,uaddr2,int,val3)
296 extern int personality(int);
297 extern int flock(int, int);
298 extern int setfsuid(int);
299 extern int setfsgid(int);
300 extern int setgroups(int, gid_t *);
302 #define ERRNO_TABLE_SIZE 1200
304 /* target_to_host_errno_table[] is initialized from
305 * host_to_target_errno_table[] in syscall_init(). */
306 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
310 * This list is the union of errno values overridden in asm-<arch>/errno.h
311 * minus the errnos that are not actually generic to all archs.
313 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
314 [EIDRM] = TARGET_EIDRM,
315 [ECHRNG] = TARGET_ECHRNG,
316 [EL2NSYNC] = TARGET_EL2NSYNC,
317 [EL3HLT] = TARGET_EL3HLT,
318 [EL3RST] = TARGET_EL3RST,
319 [ELNRNG] = TARGET_ELNRNG,
320 [EUNATCH] = TARGET_EUNATCH,
321 [ENOCSI] = TARGET_ENOCSI,
322 [EL2HLT] = TARGET_EL2HLT,
323 [EDEADLK] = TARGET_EDEADLK,
324 [ENOLCK] = TARGET_ENOLCK,
325 [EBADE] = TARGET_EBADE,
326 [EBADR] = TARGET_EBADR,
327 [EXFULL] = TARGET_EXFULL,
328 [ENOANO] = TARGET_ENOANO,
329 [EBADRQC] = TARGET_EBADRQC,
330 [EBADSLT] = TARGET_EBADSLT,
331 [EBFONT] = TARGET_EBFONT,
332 [ENOSTR] = TARGET_ENOSTR,
333 [ENODATA] = TARGET_ENODATA,
334 [ETIME] = TARGET_ETIME,
335 [ENOSR] = TARGET_ENOSR,
336 [ENONET] = TARGET_ENONET,
337 [ENOPKG] = TARGET_ENOPKG,
338 [EREMOTE] = TARGET_EREMOTE,
339 [ENOLINK] = TARGET_ENOLINK,
340 [EADV] = TARGET_EADV,
341 [ESRMNT] = TARGET_ESRMNT,
342 [ECOMM] = TARGET_ECOMM,
343 [EPROTO] = TARGET_EPROTO,
344 [EDOTDOT] = TARGET_EDOTDOT,
345 [EMULTIHOP] = TARGET_EMULTIHOP,
346 [EBADMSG] = TARGET_EBADMSG,
347 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
348 [EOVERFLOW] = TARGET_EOVERFLOW,
349 [ENOTUNIQ] = TARGET_ENOTUNIQ,
350 [EBADFD] = TARGET_EBADFD,
351 [EREMCHG] = TARGET_EREMCHG,
352 [ELIBACC] = TARGET_ELIBACC,
353 [ELIBBAD] = TARGET_ELIBBAD,
354 [ELIBSCN] = TARGET_ELIBSCN,
355 [ELIBMAX] = TARGET_ELIBMAX,
356 [ELIBEXEC] = TARGET_ELIBEXEC,
357 [EILSEQ] = TARGET_EILSEQ,
358 [ENOSYS] = TARGET_ENOSYS,
359 [ELOOP] = TARGET_ELOOP,
360 [ERESTART] = TARGET_ERESTART,
361 [ESTRPIPE] = TARGET_ESTRPIPE,
362 [ENOTEMPTY] = TARGET_ENOTEMPTY,
363 [EUSERS] = TARGET_EUSERS,
364 [ENOTSOCK] = TARGET_ENOTSOCK,
365 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
366 [EMSGSIZE] = TARGET_EMSGSIZE,
367 [EPROTOTYPE] = TARGET_EPROTOTYPE,
368 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
369 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
370 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
371 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
372 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
373 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
374 [EADDRINUSE] = TARGET_EADDRINUSE,
375 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
376 [ENETDOWN] = TARGET_ENETDOWN,
377 [ENETUNREACH] = TARGET_ENETUNREACH,
378 [ENETRESET] = TARGET_ENETRESET,
379 [ECONNABORTED] = TARGET_ECONNABORTED,
380 [ECONNRESET] = TARGET_ECONNRESET,
381 [ENOBUFS] = TARGET_ENOBUFS,
382 [EISCONN] = TARGET_EISCONN,
383 [ENOTCONN] = TARGET_ENOTCONN,
384 [EUCLEAN] = TARGET_EUCLEAN,
385 [ENOTNAM] = TARGET_ENOTNAM,
386 [ENAVAIL] = TARGET_ENAVAIL,
387 [EISNAM] = TARGET_EISNAM,
388 [EREMOTEIO] = TARGET_EREMOTEIO,
389 [ESHUTDOWN] = TARGET_ESHUTDOWN,
390 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
391 [ETIMEDOUT] = TARGET_ETIMEDOUT,
392 [ECONNREFUSED] = TARGET_ECONNREFUSED,
393 [EHOSTDOWN] = TARGET_EHOSTDOWN,
394 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
395 [EALREADY] = TARGET_EALREADY,
396 [EINPROGRESS] = TARGET_EINPROGRESS,
397 [ESTALE] = TARGET_ESTALE,
398 [ECANCELED] = TARGET_ECANCELED,
399 [ENOMEDIUM] = TARGET_ENOMEDIUM,
400 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
402 [ENOKEY] = TARGET_ENOKEY,
405 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
408 [EKEYREVOKED] = TARGET_EKEYREVOKED,
411 [EKEYREJECTED] = TARGET_EKEYREJECTED,
414 [EOWNERDEAD] = TARGET_EOWNERDEAD,
416 #ifdef ENOTRECOVERABLE
417 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
421 static inline int host_to_target_errno(int err)
423 if(host_to_target_errno_table[err])
424 return host_to_target_errno_table[err];
428 static inline int target_to_host_errno(int err)
430 if (target_to_host_errno_table[err])
431 return target_to_host_errno_table[err];
435 static inline abi_long get_errno(abi_long ret)
438 return -host_to_target_errno(errno);
443 static inline int is_error(abi_long ret)
445 return (abi_ulong)ret >= (abi_ulong)(-4096);
448 char *target_strerror(int err)
450 return strerror(target_to_host_errno(err));
453 static abi_ulong target_brk;
454 static abi_ulong target_original_brk;
456 void target_set_brk(abi_ulong new_brk)
458 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
461 /* do_brk() must return target values and target errnos. */
462 abi_long do_brk(abi_ulong new_brk)
465 abi_long mapped_addr;
470 if (new_brk < target_original_brk)
473 brk_page = HOST_PAGE_ALIGN(target_brk);
475 /* If the new brk is less than this, set it and we're done... */
476 if (new_brk < brk_page) {
477 target_brk = new_brk;
481 /* We need to allocate more memory after the brk... */
482 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
483 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
484 PROT_READ|PROT_WRITE,
485 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
487 if (!is_error(mapped_addr))
488 target_brk = new_brk;
493 static inline abi_long copy_from_user_fdset(fd_set *fds,
494 abi_ulong target_fds_addr,
498 abi_ulong b, *target_fds;
500 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
501 if (!(target_fds = lock_user(VERIFY_READ,
503 sizeof(abi_ulong) * nw,
505 return -TARGET_EFAULT;
509 for (i = 0; i < nw; i++) {
510 /* grab the abi_ulong */
511 __get_user(b, &target_fds[i]);
512 for (j = 0; j < TARGET_ABI_BITS; j++) {
513 /* check the bit inside the abi_ulong */
520 unlock_user(target_fds, target_fds_addr, 0);
525 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
531 abi_ulong *target_fds;
533 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
534 if (!(target_fds = lock_user(VERIFY_WRITE,
536 sizeof(abi_ulong) * nw,
538 return -TARGET_EFAULT;
541 for (i = 0; i < nw; i++) {
543 for (j = 0; j < TARGET_ABI_BITS; j++) {
544 v |= ((FD_ISSET(k, fds) != 0) << j);
547 __put_user(v, &target_fds[i]);
550 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
555 #if defined(__alpha__)
561 static inline abi_long host_to_target_clock_t(long ticks)
563 #if HOST_HZ == TARGET_HZ
566 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
570 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
571 const struct rusage *rusage)
573 struct target_rusage *target_rusage;
575 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
576 return -TARGET_EFAULT;
577 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
578 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
579 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
580 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
581 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
582 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
583 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
584 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
585 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
586 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
587 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
588 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
589 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
590 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
591 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
592 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
593 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
594 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
595 unlock_user_struct(target_rusage, target_addr, 1);
600 static inline abi_long copy_from_user_timeval(struct timeval *tv,
601 abi_ulong target_tv_addr)
603 struct target_timeval *target_tv;
605 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
606 return -TARGET_EFAULT;
608 __get_user(tv->tv_sec, &target_tv->tv_sec);
609 __get_user(tv->tv_usec, &target_tv->tv_usec);
611 unlock_user_struct(target_tv, target_tv_addr, 0);
616 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
617 const struct timeval *tv)
619 struct target_timeval *target_tv;
621 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
622 return -TARGET_EFAULT;
624 __put_user(tv->tv_sec, &target_tv->tv_sec);
625 __put_user(tv->tv_usec, &target_tv->tv_usec);
627 unlock_user_struct(target_tv, target_tv_addr, 1);
633 /* do_select() must return target values and target errnos. */
634 static abi_long do_select(int n,
635 abi_ulong rfd_addr, abi_ulong wfd_addr,
636 abi_ulong efd_addr, abi_ulong target_tv_addr)
638 fd_set rfds, wfds, efds;
639 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
640 struct timeval tv, *tv_ptr;
644 if (copy_from_user_fdset(&rfds, rfd_addr, n))
645 return -TARGET_EFAULT;
651 if (copy_from_user_fdset(&wfds, wfd_addr, n))
652 return -TARGET_EFAULT;
658 if (copy_from_user_fdset(&efds, efd_addr, n))
659 return -TARGET_EFAULT;
665 if (target_tv_addr) {
666 if (copy_from_user_timeval(&tv, target_tv_addr))
667 return -TARGET_EFAULT;
673 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
675 if (!is_error(ret)) {
676 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
677 return -TARGET_EFAULT;
678 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
679 return -TARGET_EFAULT;
680 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
681 return -TARGET_EFAULT;
683 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
684 return -TARGET_EFAULT;
690 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
691 abi_ulong target_addr,
694 struct target_sockaddr *target_saddr;
696 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
698 return -TARGET_EFAULT;
699 memcpy(addr, target_saddr, len);
700 addr->sa_family = tswap16(target_saddr->sa_family);
701 unlock_user(target_saddr, target_addr, 0);
706 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
707 struct sockaddr *addr,
710 struct target_sockaddr *target_saddr;
712 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
714 return -TARGET_EFAULT;
715 memcpy(target_saddr, addr, len);
716 target_saddr->sa_family = tswap16(addr->sa_family);
717 unlock_user(target_saddr, target_addr, len);
722 /* ??? Should this also swap msgh->name? */
723 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
724 struct target_msghdr *target_msgh)
726 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
727 abi_long msg_controllen;
728 abi_ulong target_cmsg_addr;
729 struct target_cmsghdr *target_cmsg;
732 msg_controllen = tswapl(target_msgh->msg_controllen);
733 if (msg_controllen < sizeof (struct target_cmsghdr))
735 target_cmsg_addr = tswapl(target_msgh->msg_control);
736 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
738 return -TARGET_EFAULT;
740 while (cmsg && target_cmsg) {
741 void *data = CMSG_DATA(cmsg);
742 void *target_data = TARGET_CMSG_DATA(target_cmsg);
744 int len = tswapl(target_cmsg->cmsg_len)
745 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
747 space += CMSG_SPACE(len);
748 if (space > msgh->msg_controllen) {
749 space -= CMSG_SPACE(len);
750 gemu_log("Host cmsg overflow\n");
754 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
755 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
756 cmsg->cmsg_len = CMSG_LEN(len);
758 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
759 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
760 memcpy(data, target_data, len);
762 int *fd = (int *)data;
763 int *target_fd = (int *)target_data;
764 int i, numfds = len / sizeof(int);
766 for (i = 0; i < numfds; i++)
767 fd[i] = tswap32(target_fd[i]);
770 cmsg = CMSG_NXTHDR(msgh, cmsg);
771 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
773 unlock_user(target_cmsg, target_cmsg_addr, 0);
775 msgh->msg_controllen = space;
779 /* ??? Should this also swap msgh->name? */
780 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
783 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
784 abi_long msg_controllen;
785 abi_ulong target_cmsg_addr;
786 struct target_cmsghdr *target_cmsg;
789 msg_controllen = tswapl(target_msgh->msg_controllen);
790 if (msg_controllen < sizeof (struct target_cmsghdr))
792 target_cmsg_addr = tswapl(target_msgh->msg_control);
793 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
795 return -TARGET_EFAULT;
797 while (cmsg && target_cmsg) {
798 void *data = CMSG_DATA(cmsg);
799 void *target_data = TARGET_CMSG_DATA(target_cmsg);
801 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
803 space += TARGET_CMSG_SPACE(len);
804 if (space > msg_controllen) {
805 space -= TARGET_CMSG_SPACE(len);
806 gemu_log("Target cmsg overflow\n");
810 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
811 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
812 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
814 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
815 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
816 memcpy(target_data, data, len);
818 int *fd = (int *)data;
819 int *target_fd = (int *)target_data;
820 int i, numfds = len / sizeof(int);
822 for (i = 0; i < numfds; i++)
823 target_fd[i] = tswap32(fd[i]);
826 cmsg = CMSG_NXTHDR(msgh, cmsg);
827 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
829 unlock_user(target_cmsg, target_cmsg_addr, space);
831 target_msgh->msg_controllen = tswapl(space);
835 /* do_setsockopt() Must return target values and target errnos. */
836 static abi_long do_setsockopt(int sockfd, int level, int optname,
837 abi_ulong optval_addr, socklen_t optlen)
844 /* TCP options all take an 'int' value. */
845 if (optlen < sizeof(uint32_t))
846 return -TARGET_EINVAL;
848 if (get_user_u32(val, optval_addr))
849 return -TARGET_EFAULT;
850 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
857 case IP_ROUTER_ALERT:
861 case IP_MTU_DISCOVER:
867 case IP_MULTICAST_TTL:
868 case IP_MULTICAST_LOOP:
870 if (optlen >= sizeof(uint32_t)) {
871 if (get_user_u32(val, optval_addr))
872 return -TARGET_EFAULT;
873 } else if (optlen >= 1) {
874 if (get_user_u8(val, optval_addr))
875 return -TARGET_EFAULT;
877 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
883 case TARGET_SOL_SOCKET:
885 /* Options with 'int' argument. */
886 case TARGET_SO_DEBUG:
889 case TARGET_SO_REUSEADDR:
890 optname = SO_REUSEADDR;
895 case TARGET_SO_ERROR:
898 case TARGET_SO_DONTROUTE:
899 optname = SO_DONTROUTE;
901 case TARGET_SO_BROADCAST:
902 optname = SO_BROADCAST;
904 case TARGET_SO_SNDBUF:
907 case TARGET_SO_RCVBUF:
910 case TARGET_SO_KEEPALIVE:
911 optname = SO_KEEPALIVE;
913 case TARGET_SO_OOBINLINE:
914 optname = SO_OOBINLINE;
916 case TARGET_SO_NO_CHECK:
917 optname = SO_NO_CHECK;
919 case TARGET_SO_PRIORITY:
920 optname = SO_PRIORITY;
923 case TARGET_SO_BSDCOMPAT:
924 optname = SO_BSDCOMPAT;
927 case TARGET_SO_PASSCRED:
928 optname = SO_PASSCRED;
930 case TARGET_SO_TIMESTAMP:
931 optname = SO_TIMESTAMP;
933 case TARGET_SO_RCVLOWAT:
934 optname = SO_RCVLOWAT;
936 case TARGET_SO_RCVTIMEO:
937 optname = SO_RCVTIMEO;
939 case TARGET_SO_SNDTIMEO:
940 optname = SO_SNDTIMEO;
946 if (optlen < sizeof(uint32_t))
947 return -TARGET_EINVAL;
949 if (get_user_u32(val, optval_addr))
950 return -TARGET_EFAULT;
951 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
955 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
956 ret = -TARGET_ENOPROTOOPT;
961 /* do_getsockopt() Must return target values and target errnos. */
962 static abi_long do_getsockopt(int sockfd, int level, int optname,
963 abi_ulong optval_addr, abi_ulong optlen)
970 case TARGET_SOL_SOCKET:
973 case TARGET_SO_LINGER:
974 case TARGET_SO_RCVTIMEO:
975 case TARGET_SO_SNDTIMEO:
976 case TARGET_SO_PEERCRED:
977 case TARGET_SO_PEERNAME:
978 /* These don't just return a single integer */
985 /* TCP options all take an 'int' value. */
987 if (get_user_u32(len, optlen))
988 return -TARGET_EFAULT;
990 return -TARGET_EINVAL;
992 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
999 if (put_user_u32(val, optval_addr))
1000 return -TARGET_EFAULT;
1002 if (put_user_u8(val, optval_addr))
1003 return -TARGET_EFAULT;
1005 if (put_user_u32(len, optlen))
1006 return -TARGET_EFAULT;
1013 case IP_ROUTER_ALERT:
1017 case IP_MTU_DISCOVER:
1023 case IP_MULTICAST_TTL:
1024 case IP_MULTICAST_LOOP:
1025 if (get_user_u32(len, optlen))
1026 return -TARGET_EFAULT;
1028 return -TARGET_EINVAL;
1030 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1033 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1035 if (put_user_u32(len, optlen)
1036 || put_user_u8(val, optval_addr))
1037 return -TARGET_EFAULT;
1039 if (len > sizeof(int))
1041 if (put_user_u32(len, optlen)
1042 || put_user_u32(val, optval_addr))
1043 return -TARGET_EFAULT;
1047 ret = -TARGET_ENOPROTOOPT;
1053 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1055 ret = -TARGET_EOPNOTSUPP;
1062 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1063 * other lock functions have a return code of 0 for failure.
1065 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1066 int count, int copy)
1068 struct target_iovec *target_vec;
1072 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1074 return -TARGET_EFAULT;
1075 for(i = 0;i < count; i++) {
1076 base = tswapl(target_vec[i].iov_base);
1077 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1078 if (vec[i].iov_len != 0) {
1079 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1080 /* Don't check lock_user return value. We must call writev even
1081 if a element has invalid base address. */
1083 /* zero length pointer is ignored */
1084 vec[i].iov_base = NULL;
1087 unlock_user (target_vec, target_addr, 0);
1091 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1092 int count, int copy)
1094 struct target_iovec *target_vec;
1098 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1100 return -TARGET_EFAULT;
1101 for(i = 0;i < count; i++) {
1102 if (target_vec[i].iov_base) {
1103 base = tswapl(target_vec[i].iov_base);
1104 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1107 unlock_user (target_vec, target_addr, 0);
1112 /* do_socket() Must return target values and target errnos. */
1113 static abi_long do_socket(int domain, int type, int protocol)
1115 #if defined(TARGET_MIPS)
1117 case TARGET_SOCK_DGRAM:
1120 case TARGET_SOCK_STREAM:
1123 case TARGET_SOCK_RAW:
1126 case TARGET_SOCK_RDM:
1129 case TARGET_SOCK_SEQPACKET:
1130 type = SOCK_SEQPACKET;
1132 case TARGET_SOCK_PACKET:
1137 if (domain == PF_NETLINK)
1138 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1139 return get_errno(socket(domain, type, protocol));
1142 /* do_bind() Must return target values and target errnos. */
1143 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1146 void *addr = alloca(addrlen);
1148 target_to_host_sockaddr(addr, target_addr, addrlen);
1149 return get_errno(bind(sockfd, addr, addrlen));
1152 /* do_connect() Must return target values and target errnos. */
1153 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1156 void *addr = alloca(addrlen);
1158 target_to_host_sockaddr(addr, target_addr, addrlen);
1159 return get_errno(connect(sockfd, addr, addrlen));
1162 /* do_sendrecvmsg() Must return target values and target errnos. */
1163 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1164 int flags, int send)
1167 struct target_msghdr *msgp;
1171 abi_ulong target_vec;
1174 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1178 return -TARGET_EFAULT;
1179 if (msgp->msg_name) {
1180 msg.msg_namelen = tswap32(msgp->msg_namelen);
1181 msg.msg_name = alloca(msg.msg_namelen);
1182 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1185 msg.msg_name = NULL;
1186 msg.msg_namelen = 0;
1188 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1189 msg.msg_control = alloca(msg.msg_controllen);
1190 msg.msg_flags = tswap32(msgp->msg_flags);
1192 count = tswapl(msgp->msg_iovlen);
1193 vec = alloca(count * sizeof(struct iovec));
1194 target_vec = tswapl(msgp->msg_iov);
1195 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1196 msg.msg_iovlen = count;
1200 ret = target_to_host_cmsg(&msg, msgp);
1202 ret = get_errno(sendmsg(fd, &msg, flags));
1204 ret = get_errno(recvmsg(fd, &msg, flags));
1205 if (!is_error(ret)) {
1207 ret = host_to_target_cmsg(msgp, &msg);
1212 unlock_iovec(vec, target_vec, count, !send);
1213 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1217 /* do_accept() Must return target values and target errnos. */
1218 static abi_long do_accept(int fd, abi_ulong target_addr,
1219 abi_ulong target_addrlen_addr)
1225 if (get_user_u32(addrlen, target_addrlen_addr))
1226 return -TARGET_EFAULT;
1228 addr = alloca(addrlen);
1230 ret = get_errno(accept(fd, addr, &addrlen));
1231 if (!is_error(ret)) {
1232 host_to_target_sockaddr(target_addr, addr, addrlen);
1233 if (put_user_u32(addrlen, target_addrlen_addr))
1234 ret = -TARGET_EFAULT;
1239 /* do_getpeername() Must return target values and target errnos. */
1240 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1241 abi_ulong target_addrlen_addr)
1247 if (get_user_u32(addrlen, target_addrlen_addr))
1248 return -TARGET_EFAULT;
1250 addr = alloca(addrlen);
1252 ret = get_errno(getpeername(fd, addr, &addrlen));
1253 if (!is_error(ret)) {
1254 host_to_target_sockaddr(target_addr, addr, addrlen);
1255 if (put_user_u32(addrlen, target_addrlen_addr))
1256 ret = -TARGET_EFAULT;
1261 /* do_getsockname() Must return target values and target errnos. */
1262 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1263 abi_ulong target_addrlen_addr)
1269 if (get_user_u32(addrlen, target_addrlen_addr))
1270 return -TARGET_EFAULT;
1272 addr = alloca(addrlen);
1274 ret = get_errno(getsockname(fd, addr, &addrlen));
1275 if (!is_error(ret)) {
1276 host_to_target_sockaddr(target_addr, addr, addrlen);
1277 if (put_user_u32(addrlen, target_addrlen_addr))
1278 ret = -TARGET_EFAULT;
1283 /* do_socketpair() Must return target values and target errnos. */
1284 static abi_long do_socketpair(int domain, int type, int protocol,
1285 abi_ulong target_tab_addr)
1290 ret = get_errno(socketpair(domain, type, protocol, tab));
1291 if (!is_error(ret)) {
1292 if (put_user_s32(tab[0], target_tab_addr)
1293 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1294 ret = -TARGET_EFAULT;
1299 /* do_sendto() Must return target values and target errnos. */
1300 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1301 abi_ulong target_addr, socklen_t addrlen)
1307 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1309 return -TARGET_EFAULT;
1311 addr = alloca(addrlen);
1312 target_to_host_sockaddr(addr, target_addr, addrlen);
1313 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1315 ret = get_errno(send(fd, host_msg, len, flags));
1317 unlock_user(host_msg, msg, 0);
1321 /* do_recvfrom() Must return target values and target errnos. */
1322 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1323 abi_ulong target_addr,
1324 abi_ulong target_addrlen)
1331 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1333 return -TARGET_EFAULT;
1335 if (get_user_u32(addrlen, target_addrlen)) {
1336 ret = -TARGET_EFAULT;
1339 addr = alloca(addrlen);
1340 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1342 addr = NULL; /* To keep compiler quiet. */
1343 ret = get_errno(recv(fd, host_msg, len, flags));
1345 if (!is_error(ret)) {
1347 host_to_target_sockaddr(target_addr, addr, addrlen);
1348 if (put_user_u32(addrlen, target_addrlen)) {
1349 ret = -TARGET_EFAULT;
1353 unlock_user(host_msg, msg, len);
1356 unlock_user(host_msg, msg, 0);
1361 #ifdef TARGET_NR_socketcall
1362 /* do_socketcall() Must return target values and target errnos. */
1363 static abi_long do_socketcall(int num, abi_ulong vptr)
1366 const int n = sizeof(abi_ulong);
1371 int domain, type, protocol;
1373 if (get_user_s32(domain, vptr)
1374 || get_user_s32(type, vptr + n)
1375 || get_user_s32(protocol, vptr + 2 * n))
1376 return -TARGET_EFAULT;
1378 ret = do_socket(domain, type, protocol);
1384 abi_ulong target_addr;
1387 if (get_user_s32(sockfd, vptr)
1388 || get_user_ual(target_addr, vptr + n)
1389 || get_user_u32(addrlen, vptr + 2 * n))
1390 return -TARGET_EFAULT;
1392 ret = do_bind(sockfd, target_addr, addrlen);
1395 case SOCKOP_connect:
1398 abi_ulong target_addr;
1401 if (get_user_s32(sockfd, vptr)
1402 || get_user_ual(target_addr, vptr + n)
1403 || get_user_u32(addrlen, vptr + 2 * n))
1404 return -TARGET_EFAULT;
1406 ret = do_connect(sockfd, target_addr, addrlen);
1411 int sockfd, backlog;
1413 if (get_user_s32(sockfd, vptr)
1414 || get_user_s32(backlog, vptr + n))
1415 return -TARGET_EFAULT;
1417 ret = get_errno(listen(sockfd, backlog));
1423 abi_ulong target_addr, target_addrlen;
1425 if (get_user_s32(sockfd, vptr)
1426 || get_user_ual(target_addr, vptr + n)
1427 || get_user_u32(target_addrlen, vptr + 2 * n))
1428 return -TARGET_EFAULT;
1430 ret = do_accept(sockfd, target_addr, target_addrlen);
1433 case SOCKOP_getsockname:
1436 abi_ulong target_addr, target_addrlen;
1438 if (get_user_s32(sockfd, vptr)
1439 || get_user_ual(target_addr, vptr + n)
1440 || get_user_u32(target_addrlen, vptr + 2 * n))
1441 return -TARGET_EFAULT;
1443 ret = do_getsockname(sockfd, target_addr, target_addrlen);
1446 case SOCKOP_getpeername:
1449 abi_ulong target_addr, target_addrlen;
1451 if (get_user_s32(sockfd, vptr)
1452 || get_user_ual(target_addr, vptr + n)
1453 || get_user_u32(target_addrlen, vptr + 2 * n))
1454 return -TARGET_EFAULT;
1456 ret = do_getpeername(sockfd, target_addr, target_addrlen);
1459 case SOCKOP_socketpair:
1461 int domain, type, protocol;
1464 if (get_user_s32(domain, vptr)
1465 || get_user_s32(type, vptr + n)
1466 || get_user_s32(protocol, vptr + 2 * n)
1467 || get_user_ual(tab, vptr + 3 * n))
1468 return -TARGET_EFAULT;
1470 ret = do_socketpair(domain, type, protocol, tab);
1480 if (get_user_s32(sockfd, vptr)
1481 || get_user_ual(msg, vptr + n)
1482 || get_user_ual(len, vptr + 2 * n)
1483 || get_user_s32(flags, vptr + 3 * n))
1484 return -TARGET_EFAULT;
1486 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1496 if (get_user_s32(sockfd, vptr)
1497 || get_user_ual(msg, vptr + n)
1498 || get_user_ual(len, vptr + 2 * n)
1499 || get_user_s32(flags, vptr + 3 * n))
1500 return -TARGET_EFAULT;
1502 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1514 if (get_user_s32(sockfd, vptr)
1515 || get_user_ual(msg, vptr + n)
1516 || get_user_ual(len, vptr + 2 * n)
1517 || get_user_s32(flags, vptr + 3 * n)
1518 || get_user_ual(addr, vptr + 4 * n)
1519 || get_user_u32(addrlen, vptr + 5 * n))
1520 return -TARGET_EFAULT;
1522 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1525 case SOCKOP_recvfrom:
1534 if (get_user_s32(sockfd, vptr)
1535 || get_user_ual(msg, vptr + n)
1536 || get_user_ual(len, vptr + 2 * n)
1537 || get_user_s32(flags, vptr + 3 * n)
1538 || get_user_ual(addr, vptr + 4 * n)
1539 || get_user_u32(addrlen, vptr + 5 * n))
1540 return -TARGET_EFAULT;
1542 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1545 case SOCKOP_shutdown:
1549 if (get_user_s32(sockfd, vptr)
1550 || get_user_s32(how, vptr + n))
1551 return -TARGET_EFAULT;
1553 ret = get_errno(shutdown(sockfd, how));
1556 case SOCKOP_sendmsg:
1557 case SOCKOP_recvmsg:
1560 abi_ulong target_msg;
1563 if (get_user_s32(fd, vptr)
1564 || get_user_ual(target_msg, vptr + n)
1565 || get_user_s32(flags, vptr + 2 * n))
1566 return -TARGET_EFAULT;
1568 ret = do_sendrecvmsg(fd, target_msg, flags,
1569 (num == SOCKOP_sendmsg));
1572 case SOCKOP_setsockopt:
1580 if (get_user_s32(sockfd, vptr)
1581 || get_user_s32(level, vptr + n)
1582 || get_user_s32(optname, vptr + 2 * n)
1583 || get_user_ual(optval, vptr + 3 * n)
1584 || get_user_u32(optlen, vptr + 4 * n))
1585 return -TARGET_EFAULT;
1587 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1590 case SOCKOP_getsockopt:
1598 if (get_user_s32(sockfd, vptr)
1599 || get_user_s32(level, vptr + n)
1600 || get_user_s32(optname, vptr + 2 * n)
1601 || get_user_ual(optval, vptr + 3 * n)
1602 || get_user_u32(optlen, vptr + 4 * n))
1603 return -TARGET_EFAULT;
1605 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
1609 gemu_log("Unsupported socketcall: %d\n", num);
1610 ret = -TARGET_ENOSYS;
1617 #define N_SHM_REGIONS 32
1619 static struct shm_region {
1622 } shm_regions[N_SHM_REGIONS];
1624 struct target_ipc_perm
1631 unsigned short int mode;
1632 unsigned short int __pad1;
1633 unsigned short int __seq;
1634 unsigned short int __pad2;
1635 abi_ulong __unused1;
1636 abi_ulong __unused2;
1639 struct target_semid_ds
1641 struct target_ipc_perm sem_perm;
1642 abi_ulong sem_otime;
1643 abi_ulong __unused1;
1644 abi_ulong sem_ctime;
1645 abi_ulong __unused2;
1646 abi_ulong sem_nsems;
1647 abi_ulong __unused3;
1648 abi_ulong __unused4;
1651 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1652 abi_ulong target_addr)
1654 struct target_ipc_perm *target_ip;
1655 struct target_semid_ds *target_sd;
1657 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1658 return -TARGET_EFAULT;
1659 target_ip=&(target_sd->sem_perm);
1660 host_ip->__key = tswapl(target_ip->__key);
1661 host_ip->uid = tswapl(target_ip->uid);
1662 host_ip->gid = tswapl(target_ip->gid);
1663 host_ip->cuid = tswapl(target_ip->cuid);
1664 host_ip->cgid = tswapl(target_ip->cgid);
1665 host_ip->mode = tswapl(target_ip->mode);
1666 unlock_user_struct(target_sd, target_addr, 0);
1670 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1671 struct ipc_perm *host_ip)
1673 struct target_ipc_perm *target_ip;
1674 struct target_semid_ds *target_sd;
1676 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1677 return -TARGET_EFAULT;
1678 target_ip = &(target_sd->sem_perm);
1679 target_ip->__key = tswapl(host_ip->__key);
1680 target_ip->uid = tswapl(host_ip->uid);
1681 target_ip->gid = tswapl(host_ip->gid);
1682 target_ip->cuid = tswapl(host_ip->cuid);
1683 target_ip->cgid = tswapl(host_ip->cgid);
1684 target_ip->mode = tswapl(host_ip->mode);
1685 unlock_user_struct(target_sd, target_addr, 1);
1689 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1690 abi_ulong target_addr)
1692 struct target_semid_ds *target_sd;
1694 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1695 return -TARGET_EFAULT;
1696 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1697 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1698 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1699 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1700 unlock_user_struct(target_sd, target_addr, 0);
1704 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1705 struct semid_ds *host_sd)
1707 struct target_semid_ds *target_sd;
1709 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1710 return -TARGET_EFAULT;
1711 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1712 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1713 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1714 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1715 unlock_user_struct(target_sd, target_addr, 1);
1721 struct semid_ds *buf;
1722 unsigned short *array;
1725 union target_semun {
1728 unsigned short int *array;
1731 static inline abi_long target_to_host_semun(int cmd,
1732 union semun *host_su,
1733 abi_ulong target_addr,
1734 struct semid_ds *ds)
1736 union target_semun *target_su;
1741 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1742 return -TARGET_EFAULT;
1743 target_to_host_semid_ds(ds,target_su->buf);
1745 unlock_user_struct(target_su, target_addr, 0);
1749 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1750 return -TARGET_EFAULT;
1751 host_su->val = tswapl(target_su->val);
1752 unlock_user_struct(target_su, target_addr, 0);
1756 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1757 return -TARGET_EFAULT;
1758 *host_su->array = tswap16(*target_su->array);
1759 unlock_user_struct(target_su, target_addr, 0);
1762 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1767 static inline abi_long host_to_target_semun(int cmd,
1768 abi_ulong target_addr,
1769 union semun *host_su,
1770 struct semid_ds *ds)
1772 union target_semun *target_su;
1777 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1778 return -TARGET_EFAULT;
1779 host_to_target_semid_ds(target_su->buf,ds);
1780 unlock_user_struct(target_su, target_addr, 1);
1784 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1785 return -TARGET_EFAULT;
1786 target_su->val = tswapl(host_su->val);
1787 unlock_user_struct(target_su, target_addr, 1);
1791 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1792 return -TARGET_EFAULT;
1793 *target_su->array = tswap16(*host_su->array);
1794 unlock_user_struct(target_su, target_addr, 1);
1797 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1802 static inline abi_long do_semctl(int first, int second, int third,
1806 struct semid_ds dsarg;
1807 int cmd = third&0xff;
1812 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1813 ret = get_errno(semctl(first, second, cmd, arg));
1814 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1817 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1818 ret = get_errno(semctl(first, second, cmd, arg));
1819 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1822 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1823 ret = get_errno(semctl(first, second, cmd, arg));
1824 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1827 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1828 ret = get_errno(semctl(first, second, cmd, arg));
1829 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1832 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1833 ret = get_errno(semctl(first, second, cmd, arg));
1834 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1837 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1838 ret = get_errno(semctl(first, second, cmd, arg));
1839 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1842 ret = get_errno(semctl(first, second, cmd, arg));
1848 struct target_msqid_ds
1850 struct target_ipc_perm msg_perm;
1851 abi_ulong msg_stime;
1852 #if TARGET_ABI_BITS == 32
1853 abi_ulong __unused1;
1855 abi_ulong msg_rtime;
1856 #if TARGET_ABI_BITS == 32
1857 abi_ulong __unused2;
1859 abi_ulong msg_ctime;
1860 #if TARGET_ABI_BITS == 32
1861 abi_ulong __unused3;
1863 abi_ulong __msg_cbytes;
1865 abi_ulong msg_qbytes;
1866 abi_ulong msg_lspid;
1867 abi_ulong msg_lrpid;
1868 abi_ulong __unused4;
1869 abi_ulong __unused5;
1872 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1873 abi_ulong target_addr)
1875 struct target_msqid_ds *target_md;
1877 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1878 return -TARGET_EFAULT;
1879 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
1880 return -TARGET_EFAULT;
1881 host_md->msg_stime = tswapl(target_md->msg_stime);
1882 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1883 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1884 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1885 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1886 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1887 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1888 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1889 unlock_user_struct(target_md, target_addr, 0);
1893 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1894 struct msqid_ds *host_md)
1896 struct target_msqid_ds *target_md;
1898 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1899 return -TARGET_EFAULT;
1900 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
1901 return -TARGET_EFAULT;
1902 target_md->msg_stime = tswapl(host_md->msg_stime);
1903 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1904 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1905 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1906 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1907 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1908 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1909 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1910 unlock_user_struct(target_md, target_addr, 1);
1914 struct target_msginfo {
1922 unsigned short int msgseg;
1925 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
1926 struct msginfo *host_msginfo)
1928 struct target_msginfo *target_msginfo;
1929 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
1930 return -TARGET_EFAULT;
1931 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
1932 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
1933 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
1934 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
1935 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
1936 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
1937 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
1938 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
1939 unlock_user_struct(target_msginfo, target_addr, 1);
1943 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
1945 struct msqid_ds dsarg;
1946 struct msginfo msginfo;
1947 abi_long ret = -TARGET_EINVAL;
1955 if (target_to_host_msqid_ds(&dsarg,ptr))
1956 return -TARGET_EFAULT;
1957 ret = get_errno(msgctl(msgid, cmd, &dsarg));
1958 if (host_to_target_msqid_ds(ptr,&dsarg))
1959 return -TARGET_EFAULT;
1962 ret = get_errno(msgctl(msgid, cmd, NULL));
1966 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
1967 if (host_to_target_msginfo(ptr, &msginfo))
1968 return -TARGET_EFAULT;
1975 struct target_msgbuf {
1980 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1981 unsigned int msgsz, int msgflg)
1983 struct target_msgbuf *target_mb;
1984 struct msgbuf *host_mb;
1987 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1988 return -TARGET_EFAULT;
1989 host_mb = malloc(msgsz+sizeof(long));
1990 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
1991 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
1992 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1994 unlock_user_struct(target_mb, msgp, 0);
1999 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2000 unsigned int msgsz, abi_long msgtyp,
2003 struct target_msgbuf *target_mb;
2005 struct msgbuf *host_mb;
2008 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2009 return -TARGET_EFAULT;
2011 host_mb = malloc(msgsz+sizeof(long));
2012 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2015 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2016 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2017 if (!target_mtext) {
2018 ret = -TARGET_EFAULT;
2021 memcpy(target_mb->mtext, host_mb->mtext, ret);
2022 unlock_user(target_mtext, target_mtext_addr, ret);
2025 target_mb->mtype = tswapl(host_mb->mtype);
2030 unlock_user_struct(target_mb, msgp, 1);
2034 #ifdef TARGET_NR_ipc
2035 /* ??? This only works with linear mappings. */
2036 /* do_ipc() must return target values and target errnos. */
2037 static abi_long do_ipc(unsigned int call, int first,
2038 int second, int third,
2039 abi_long ptr, abi_long fifth)
2043 struct shmid_ds shm_info;
2046 version = call >> 16;
2051 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
2055 ret = get_errno(semget(first, second, third));
2059 ret = do_semctl(first, second, third, ptr);
2062 case IPCOP_semtimedop:
2063 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2064 ret = -TARGET_ENOSYS;
2068 ret = get_errno(msgget(first, second));
2072 ret = do_msgsnd(first, ptr, second, third);
2076 ret = do_msgctl(first, second, ptr);
2083 struct target_ipc_kludge {
2088 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2089 ret = -TARGET_EFAULT;
2093 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2095 unlock_user_struct(tmp, ptr, 0);
2099 ret = do_msgrcv(first, ptr, second, fifth, third);
2107 /* SHM_* flags are the same on all linux platforms */
2108 host_addr = shmat(first, (void *)g2h(ptr), second);
2109 if (host_addr == (void *)-1) {
2110 ret = get_errno((long)host_addr);
2113 raddr = h2g((unsigned long)host_addr);
2114 /* find out the length of the shared memory segment */
2116 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
2117 if (is_error(ret)) {
2118 /* can't get length, bail out */
2122 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2123 PAGE_VALID | PAGE_READ |
2124 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2125 for (i = 0; i < N_SHM_REGIONS; ++i) {
2126 if (shm_regions[i].start == 0) {
2127 shm_regions[i].start = raddr;
2128 shm_regions[i].size = shm_info.shm_segsz;
2132 if (put_user_ual(raddr, third))
2133 return -TARGET_EFAULT;
2138 for (i = 0; i < N_SHM_REGIONS; ++i) {
2139 if (shm_regions[i].start == ptr) {
2140 shm_regions[i].start = 0;
2141 page_set_flags(ptr, shm_regions[i].size, 0);
2145 ret = get_errno(shmdt((void *)g2h(ptr)));
2149 /* IPC_* flag values are the same on all linux platforms */
2150 ret = get_errno(shmget(first, second, third));
2153 /* IPC_* and SHM_* command values are the same on all linux platforms */
2159 ret = get_errno(shmctl(first, second, NULL));
2167 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2168 ret = -TARGET_ENOSYS;
2175 /* kernel structure types definitions */
2178 #define STRUCT(name, list...) STRUCT_ ## name,
2179 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2181 #include "syscall_types.h"
2184 #undef STRUCT_SPECIAL
2186 #define STRUCT(name, list...) static const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2187 #define STRUCT_SPECIAL(name)
2188 #include "syscall_types.h"
2190 #undef STRUCT_SPECIAL
2192 typedef struct IOCTLEntry {
2193 unsigned int target_cmd;
2194 unsigned int host_cmd;
2197 const argtype arg_type[5];
2200 #define IOC_R 0x0001
2201 #define IOC_W 0x0002
2202 #define IOC_RW (IOC_R | IOC_W)
2204 #define MAX_STRUCT_SIZE 4096
2206 static IOCTLEntry ioctl_entries[] = {
2207 #define IOCTL(cmd, access, types...) \
2208 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2213 /* ??? Implement proper locking for ioctls. */
2214 /* do_ioctl() Must return target values and target errnos. */
2215 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
2217 const IOCTLEntry *ie;
2218 const argtype *arg_type;
2220 uint8_t buf_temp[MAX_STRUCT_SIZE];
2226 if (ie->target_cmd == 0) {
2227 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
2228 return -TARGET_ENOSYS;
2230 if (ie->target_cmd == cmd)
2234 arg_type = ie->arg_type;
2236 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
2238 switch(arg_type[0]) {
2241 ret = get_errno(ioctl(fd, ie->host_cmd));
2246 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2250 target_size = thunk_type_size(arg_type, 0);
2251 switch(ie->access) {
2253 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2254 if (!is_error(ret)) {
2255 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2257 return -TARGET_EFAULT;
2258 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2259 unlock_user(argptr, arg, target_size);
2263 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2265 return -TARGET_EFAULT;
2266 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2267 unlock_user(argptr, arg, 0);
2268 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2272 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2274 return -TARGET_EFAULT;
2275 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2276 unlock_user(argptr, arg, 0);
2277 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2278 if (!is_error(ret)) {
2279 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2281 return -TARGET_EFAULT;
2282 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2283 unlock_user(argptr, arg, target_size);
2289 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2290 (long)cmd, arg_type[0]);
2291 ret = -TARGET_ENOSYS;
2297 static const bitmask_transtbl iflag_tbl[] = {
2298 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2299 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2300 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2301 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2302 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2303 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2304 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2305 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2306 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2307 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2308 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2309 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2310 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2311 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2315 static const bitmask_transtbl oflag_tbl[] = {
2316 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2317 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2318 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2319 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2320 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2321 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2322 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2323 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2324 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2325 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2326 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2327 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2328 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2329 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2330 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2331 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2332 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2333 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2334 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2335 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2336 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2337 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2338 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2339 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2343 static const bitmask_transtbl cflag_tbl[] = {
2344 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2345 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2346 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2347 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2348 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2349 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2350 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2351 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2352 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2353 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2354 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2355 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2356 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2357 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2358 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2359 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2360 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2361 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2362 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2363 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2364 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2365 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2366 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2367 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2368 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2369 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2370 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2371 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2372 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2373 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2374 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2378 static const bitmask_transtbl lflag_tbl[] = {
2379 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2380 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2381 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2382 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2383 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2384 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2385 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2386 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2387 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2388 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2389 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2390 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2391 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2392 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2393 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2397 static void target_to_host_termios (void *dst, const void *src)
2399 struct host_termios *host = dst;
2400 const struct target_termios *target = src;
2403 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2405 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2407 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2409 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2410 host->c_line = target->c_line;
2412 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2413 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2414 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2415 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2416 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2417 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2418 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2419 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2420 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2421 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2422 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2423 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2424 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2425 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2426 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2427 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2428 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2431 static void host_to_target_termios (void *dst, const void *src)
2433 struct target_termios *target = dst;
2434 const struct host_termios *host = src;
2437 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2439 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2441 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2443 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2444 target->c_line = host->c_line;
2446 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2447 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2448 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2449 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2450 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2451 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2452 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2453 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2454 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2455 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2456 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2457 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2458 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2459 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2460 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2461 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2462 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2465 static const StructEntry struct_termios_def = {
2466 .convert = { host_to_target_termios, target_to_host_termios },
2467 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2468 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2471 static bitmask_transtbl mmap_flags_tbl[] = {
2472 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2473 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2474 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2475 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2476 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2477 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2478 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2479 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2483 static bitmask_transtbl fcntl_flags_tbl[] = {
2484 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2485 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2486 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2487 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2488 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2489 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2490 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2491 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2492 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2493 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2494 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2495 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2496 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2497 #if defined(O_DIRECT)
2498 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
2503 #if defined(TARGET_I386)
2505 /* NOTE: there is really one LDT for all the threads */
2506 static uint8_t *ldt_table;
2508 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
2515 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2516 if (size > bytecount)
2518 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2520 return -TARGET_EFAULT;
2521 /* ??? Should this by byteswapped? */
2522 memcpy(p, ldt_table, size);
2523 unlock_user(p, ptr, size);
2527 /* XXX: add locking support */
2528 static abi_long write_ldt(CPUX86State *env,
2529 abi_ulong ptr, unsigned long bytecount, int oldmode)
2531 struct target_modify_ldt_ldt_s ldt_info;
2532 struct target_modify_ldt_ldt_s *target_ldt_info;
2533 int seg_32bit, contents, read_exec_only, limit_in_pages;
2534 int seg_not_present, useable, lm;
2535 uint32_t *lp, entry_1, entry_2;
2537 if (bytecount != sizeof(ldt_info))
2538 return -TARGET_EINVAL;
2539 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
2540 return -TARGET_EFAULT;
2541 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2542 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2543 ldt_info.limit = tswap32(target_ldt_info->limit);
2544 ldt_info.flags = tswap32(target_ldt_info->flags);
2545 unlock_user_struct(target_ldt_info, ptr, 0);
2547 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2548 return -TARGET_EINVAL;
2549 seg_32bit = ldt_info.flags & 1;
2550 contents = (ldt_info.flags >> 1) & 3;
2551 read_exec_only = (ldt_info.flags >> 3) & 1;
2552 limit_in_pages = (ldt_info.flags >> 4) & 1;
2553 seg_not_present = (ldt_info.flags >> 5) & 1;
2554 useable = (ldt_info.flags >> 6) & 1;
2558 lm = (ldt_info.flags >> 7) & 1;
2560 if (contents == 3) {
2562 return -TARGET_EINVAL;
2563 if (seg_not_present == 0)
2564 return -TARGET_EINVAL;
2566 /* allocate the LDT */
2568 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2570 return -TARGET_ENOMEM;
2571 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2572 env->ldt.base = h2g((unsigned long)ldt_table);
2573 env->ldt.limit = 0xffff;
2576 /* NOTE: same code as Linux kernel */
2577 /* Allow LDTs to be cleared by the user. */
2578 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2581 read_exec_only == 1 &&
2583 limit_in_pages == 0 &&
2584 seg_not_present == 1 &&
2592 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2593 (ldt_info.limit & 0x0ffff);
2594 entry_2 = (ldt_info.base_addr & 0xff000000) |
2595 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2596 (ldt_info.limit & 0xf0000) |
2597 ((read_exec_only ^ 1) << 9) |
2599 ((seg_not_present ^ 1) << 15) |
2601 (limit_in_pages << 23) |
2605 entry_2 |= (useable << 20);
2607 /* Install the new entry ... */
2609 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2610 lp[0] = tswap32(entry_1);
2611 lp[1] = tswap32(entry_2);
2615 /* specific and weird i386 syscalls */
2616 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2617 unsigned long bytecount)
2623 ret = read_ldt(ptr, bytecount);
2626 ret = write_ldt(env, ptr, bytecount, 1);
2629 ret = write_ldt(env, ptr, bytecount, 0);
2632 ret = -TARGET_ENOSYS;
2638 #if defined(TARGET_I386) && defined(TARGET_ABI32)
2639 static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
2641 uint64_t *gdt_table = g2h(env->gdt.base);
2642 struct target_modify_ldt_ldt_s ldt_info;
2643 struct target_modify_ldt_ldt_s *target_ldt_info;
2644 int seg_32bit, contents, read_exec_only, limit_in_pages;
2645 int seg_not_present, useable, lm;
2646 uint32_t *lp, entry_1, entry_2;
2649 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2650 if (!target_ldt_info)
2651 return -TARGET_EFAULT;
2652 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2653 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2654 ldt_info.limit = tswap32(target_ldt_info->limit);
2655 ldt_info.flags = tswap32(target_ldt_info->flags);
2656 if (ldt_info.entry_number == -1) {
2657 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
2658 if (gdt_table[i] == 0) {
2659 ldt_info.entry_number = i;
2660 target_ldt_info->entry_number = tswap32(i);
2665 unlock_user_struct(target_ldt_info, ptr, 1);
2667 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
2668 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
2669 return -TARGET_EINVAL;
2670 seg_32bit = ldt_info.flags & 1;
2671 contents = (ldt_info.flags >> 1) & 3;
2672 read_exec_only = (ldt_info.flags >> 3) & 1;
2673 limit_in_pages = (ldt_info.flags >> 4) & 1;
2674 seg_not_present = (ldt_info.flags >> 5) & 1;
2675 useable = (ldt_info.flags >> 6) & 1;
2679 lm = (ldt_info.flags >> 7) & 1;
2682 if (contents == 3) {
2683 if (seg_not_present == 0)
2684 return -TARGET_EINVAL;
2687 /* NOTE: same code as Linux kernel */
2688 /* Allow LDTs to be cleared by the user. */
2689 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2690 if ((contents == 0 &&
2691 read_exec_only == 1 &&
2693 limit_in_pages == 0 &&
2694 seg_not_present == 1 &&
2702 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2703 (ldt_info.limit & 0x0ffff);
2704 entry_2 = (ldt_info.base_addr & 0xff000000) |
2705 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2706 (ldt_info.limit & 0xf0000) |
2707 ((read_exec_only ^ 1) << 9) |
2709 ((seg_not_present ^ 1) << 15) |
2711 (limit_in_pages << 23) |
2716 /* Install the new entry ... */
2718 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
2719 lp[0] = tswap32(entry_1);
2720 lp[1] = tswap32(entry_2);
2724 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
2726 struct target_modify_ldt_ldt_s *target_ldt_info;
2727 uint64_t *gdt_table = g2h(env->gdt.base);
2728 uint32_t base_addr, limit, flags;
2729 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
2730 int seg_not_present, useable, lm;
2731 uint32_t *lp, entry_1, entry_2;
2733 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2734 if (!target_ldt_info)
2735 return -TARGET_EFAULT;
2736 idx = tswap32(target_ldt_info->entry_number);
2737 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
2738 idx > TARGET_GDT_ENTRY_TLS_MAX) {
2739 unlock_user_struct(target_ldt_info, ptr, 1);
2740 return -TARGET_EINVAL;
2742 lp = (uint32_t *)(gdt_table + idx);
2743 entry_1 = tswap32(lp[0]);
2744 entry_2 = tswap32(lp[1]);
2746 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
2747 contents = (entry_2 >> 10) & 3;
2748 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
2749 seg_32bit = (entry_2 >> 22) & 1;
2750 limit_in_pages = (entry_2 >> 23) & 1;
2751 useable = (entry_2 >> 20) & 1;
2755 lm = (entry_2 >> 21) & 1;
2757 flags = (seg_32bit << 0) | (contents << 1) |
2758 (read_exec_only << 3) | (limit_in_pages << 4) |
2759 (seg_not_present << 5) | (useable << 6) | (lm << 7);
2760 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
2761 base_addr = (entry_1 >> 16) |
2762 (entry_2 & 0xff000000) |
2763 ((entry_2 & 0xff) << 16);
2764 target_ldt_info->base_addr = tswapl(base_addr);
2765 target_ldt_info->limit = tswap32(limit);
2766 target_ldt_info->flags = tswap32(flags);
2767 unlock_user_struct(target_ldt_info, ptr, 1);
2770 #endif /* TARGET_I386 && TARGET_ABI32 */
2772 #ifndef TARGET_ABI32
2773 static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
2780 case TARGET_ARCH_SET_GS:
2781 case TARGET_ARCH_SET_FS:
2782 if (code == TARGET_ARCH_SET_GS)
2786 cpu_x86_load_seg(env, idx, 0);
2787 env->segs[idx].base = addr;
2789 case TARGET_ARCH_GET_GS:
2790 case TARGET_ARCH_GET_FS:
2791 if (code == TARGET_ARCH_GET_GS)
2795 val = env->segs[idx].base;
2796 if (put_user(val, addr, abi_ulong))
2797 return -TARGET_EFAULT;
2800 ret = -TARGET_EINVAL;
2807 #endif /* defined(TARGET_I386) */
2809 #if defined(USE_NPTL)
2811 #define NEW_STACK_SIZE PTHREAD_STACK_MIN
2813 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
2816 pthread_mutex_t mutex;
2817 pthread_cond_t cond;
2820 abi_ulong child_tidptr;
2821 abi_ulong parent_tidptr;
2825 static void *clone_func(void *arg)
2827 new_thread_info *info = arg;
2832 info->tid = gettid();
2833 if (info->child_tidptr)
2834 put_user_u32(info->tid, info->child_tidptr);
2835 if (info->parent_tidptr)
2836 put_user_u32(info->tid, info->parent_tidptr);
2837 /* Enable signals. */
2838 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
2839 /* Signal to the parent that we're ready. */
2840 pthread_mutex_lock(&info->mutex);
2841 pthread_cond_broadcast(&info->cond);
2842 pthread_mutex_unlock(&info->mutex);
2843 /* Wait until the parent has finshed initializing the tls state. */
2844 pthread_mutex_lock(&clone_lock);
2845 pthread_mutex_unlock(&clone_lock);
2851 /* this stack is the equivalent of the kernel stack associated with a
2853 #define NEW_STACK_SIZE 8192
2855 static int clone_func(void *arg)
2857 CPUState *env = arg;
2864 /* do_fork() Must return host values and target errnos (unlike most
2865 do_*() functions). */
2866 static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
2867 abi_ulong parent_tidptr, target_ulong newtls,
2868 abi_ulong child_tidptr)
2874 #if defined(USE_NPTL)
2875 unsigned int nptl_flags;
2879 /* Emulate vfork() with fork() */
2880 if (flags & CLONE_VFORK)
2881 flags &= ~(CLONE_VFORK | CLONE_VM);
2883 if (flags & CLONE_VM) {
2884 #if defined(USE_NPTL)
2885 new_thread_info info;
2886 pthread_attr_t attr;
2888 ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
2889 init_task_state(ts);
2890 new_stack = ts->stack;
2891 /* we create a new CPU instance. */
2892 new_env = cpu_copy(env);
2893 /* Init regs that differ from the parent. */
2894 cpu_clone_regs(new_env, newsp);
2895 new_env->opaque = ts;
2896 #if defined(USE_NPTL)
2898 flags &= ~CLONE_NPTL_FLAGS2;
2900 /* TODO: Implement CLONE_CHILD_CLEARTID. */
2901 if (nptl_flags & CLONE_SETTLS)
2902 cpu_set_tls (new_env, newtls);
2904 /* Grab a mutex so that thread setup appears atomic. */
2905 pthread_mutex_lock(&clone_lock);
2907 memset(&info, 0, sizeof(info));
2908 pthread_mutex_init(&info.mutex, NULL);
2909 pthread_mutex_lock(&info.mutex);
2910 pthread_cond_init(&info.cond, NULL);
2912 if (nptl_flags & CLONE_CHILD_SETTID)
2913 info.child_tidptr = child_tidptr;
2914 if (nptl_flags & CLONE_PARENT_SETTID)
2915 info.parent_tidptr = parent_tidptr;
2917 ret = pthread_attr_init(&attr);
2918 ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
2919 /* It is not safe to deliver signals until the child has finished
2920 initializing, so temporarily block all signals. */
2921 sigfillset(&sigmask);
2922 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
2924 ret = pthread_create(&info.thread, &attr, clone_func, &info);
2926 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
2927 pthread_attr_destroy(&attr);
2929 /* Wait for the child to initialize. */
2930 pthread_cond_wait(&info.cond, &info.mutex);
2932 if (flags & CLONE_PARENT_SETTID)
2933 put_user_u32(ret, parent_tidptr);
2937 pthread_mutex_unlock(&info.mutex);
2938 pthread_cond_destroy(&info.cond);
2939 pthread_mutex_destroy(&info.mutex);
2940 pthread_mutex_unlock(&clone_lock);
2942 if (flags & CLONE_NPTL_FLAGS2)
2944 /* This is probably going to die very quickly, but do it anyway. */
2946 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2948 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2952 /* if no CLONE_VM, we consider it is a fork */
2953 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
2957 #if defined(USE_NPTL)
2958 /* There is a race condition here. The parent process could
2959 theoretically read the TID in the child process before the child
2960 tid is set. This would require using either ptrace
2961 (not implemented) or having *_tidptr to point at a shared memory
2962 mapping. We can't repeat the spinlock hack used above because
2963 the child process gets its own copy of the lock. */
2965 cpu_clone_regs(env, newsp);
2967 /* Child Process. */
2968 if (flags & CLONE_CHILD_SETTID)
2969 put_user_u32(gettid(), child_tidptr);
2970 if (flags & CLONE_PARENT_SETTID)
2971 put_user_u32(gettid(), parent_tidptr);
2972 ts = (TaskState *)env->opaque;
2973 if (flags & CLONE_SETTLS)
2974 cpu_set_tls (env, newtls);
2975 /* TODO: Implement CLONE_CHILD_CLEARTID. */
2981 cpu_clone_regs(env, newsp);
2988 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2991 struct target_flock *target_fl;
2992 struct flock64 fl64;
2993 struct target_flock64 *target_fl64;
2997 case TARGET_F_GETLK:
2998 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2999 return -TARGET_EFAULT;
3000 fl.l_type = tswap16(target_fl->l_type);
3001 fl.l_whence = tswap16(target_fl->l_whence);
3002 fl.l_start = tswapl(target_fl->l_start);
3003 fl.l_len = tswapl(target_fl->l_len);
3004 fl.l_pid = tswapl(target_fl->l_pid);
3005 unlock_user_struct(target_fl, arg, 0);
3006 ret = get_errno(fcntl(fd, cmd, &fl));
3008 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3009 return -TARGET_EFAULT;
3010 target_fl->l_type = tswap16(fl.l_type);
3011 target_fl->l_whence = tswap16(fl.l_whence);
3012 target_fl->l_start = tswapl(fl.l_start);
3013 target_fl->l_len = tswapl(fl.l_len);
3014 target_fl->l_pid = tswapl(fl.l_pid);
3015 unlock_user_struct(target_fl, arg, 1);
3019 case TARGET_F_SETLK:
3020 case TARGET_F_SETLKW:
3021 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3022 return -TARGET_EFAULT;
3023 fl.l_type = tswap16(target_fl->l_type);
3024 fl.l_whence = tswap16(target_fl->l_whence);
3025 fl.l_start = tswapl(target_fl->l_start);
3026 fl.l_len = tswapl(target_fl->l_len);
3027 fl.l_pid = tswapl(target_fl->l_pid);
3028 unlock_user_struct(target_fl, arg, 0);
3029 ret = get_errno(fcntl(fd, cmd, &fl));
3032 case TARGET_F_GETLK64:
3033 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3034 return -TARGET_EFAULT;
3035 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3036 fl64.l_whence = tswap16(target_fl64->l_whence);
3037 fl64.l_start = tswapl(target_fl64->l_start);
3038 fl64.l_len = tswapl(target_fl64->l_len);
3039 fl64.l_pid = tswap16(target_fl64->l_pid);
3040 unlock_user_struct(target_fl64, arg, 0);
3041 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
3043 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3044 return -TARGET_EFAULT;
3045 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3046 target_fl64->l_whence = tswap16(fl64.l_whence);
3047 target_fl64->l_start = tswapl(fl64.l_start);
3048 target_fl64->l_len = tswapl(fl64.l_len);
3049 target_fl64->l_pid = tswapl(fl64.l_pid);
3050 unlock_user_struct(target_fl64, arg, 1);
3053 case TARGET_F_SETLK64:
3054 case TARGET_F_SETLKW64:
3055 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3056 return -TARGET_EFAULT;
3057 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3058 fl64.l_whence = tswap16(target_fl64->l_whence);
3059 fl64.l_start = tswapl(target_fl64->l_start);
3060 fl64.l_len = tswapl(target_fl64->l_len);
3061 fl64.l_pid = tswap16(target_fl64->l_pid);
3062 unlock_user_struct(target_fl64, arg, 0);
3063 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
3067 ret = get_errno(fcntl(fd, cmd, arg));
3069 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3074 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3078 ret = get_errno(fcntl(fd, cmd, arg));
3086 static inline int high2lowuid(int uid)
3094 static inline int high2lowgid(int gid)
3102 static inline int low2highuid(int uid)
3104 if ((int16_t)uid == -1)
3110 static inline int low2highgid(int gid)
3112 if ((int16_t)gid == -1)
3118 #endif /* USE_UID16 */
3120 void syscall_init(void)
3123 const argtype *arg_type;
3127 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
3128 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
3129 #include "syscall_types.h"
3131 #undef STRUCT_SPECIAL
3133 /* we patch the ioctl size if necessary. We rely on the fact that
3134 no ioctl has all the bits at '1' in the size field */
3136 while (ie->target_cmd != 0) {
3137 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3138 TARGET_IOC_SIZEMASK) {
3139 arg_type = ie->arg_type;
3140 if (arg_type[0] != TYPE_PTR) {
3141 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
3146 size = thunk_type_size(arg_type, 0);
3147 ie->target_cmd = (ie->target_cmd &
3148 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3149 (size << TARGET_IOC_SIZESHIFT);
3152 /* Build target_to_host_errno_table[] table from
3153 * host_to_target_errno_table[]. */
3154 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3155 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3157 /* automatic consistency check if same arch */
3158 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3159 (defined(__x86_64__) && defined(TARGET_X86_64))
3160 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3161 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3162 ie->name, ie->target_cmd, ie->host_cmd);
3169 #if TARGET_ABI_BITS == 32
3170 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3172 #ifdef TARGET_WORDS_BIGENDIAN
3173 return ((uint64_t)word0 << 32) | word1;
3175 return ((uint64_t)word1 << 32) | word0;
3178 #else /* TARGET_ABI_BITS == 32 */
3179 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3183 #endif /* TARGET_ABI_BITS != 32 */
3185 #ifdef TARGET_NR_truncate64
3186 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3192 if (((CPUARMState *)cpu_env)->eabi)
3198 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3202 #ifdef TARGET_NR_ftruncate64
3203 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3209 if (((CPUARMState *)cpu_env)->eabi)
3215 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3219 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3220 abi_ulong target_addr)
3222 struct target_timespec *target_ts;
3224 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3225 return -TARGET_EFAULT;
3226 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3227 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3228 unlock_user_struct(target_ts, target_addr, 0);
3232 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3233 struct timespec *host_ts)
3235 struct target_timespec *target_ts;
3237 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3238 return -TARGET_EFAULT;
3239 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3240 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3241 unlock_user_struct(target_ts, target_addr, 1);
3245 #ifdef TARGET_NR_stat64
3246 static inline abi_long host_to_target_stat64(void *cpu_env,
3247 abi_ulong target_addr,
3248 struct stat *host_st)
3251 if (((CPUARMState *)cpu_env)->eabi) {
3252 struct target_eabi_stat64 *target_st;
3254 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3255 return -TARGET_EFAULT;
3256 memset(target_st, 0, sizeof(struct target_eabi_stat64));
3257 __put_user(host_st->st_dev, &target_st->st_dev);
3258 __put_user(host_st->st_ino, &target_st->st_ino);
3259 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3260 __put_user(host_st->st_ino, &target_st->__st_ino);
3262 __put_user(host_st->st_mode, &target_st->st_mode);
3263 __put_user(host_st->st_nlink, &target_st->st_nlink);
3264 __put_user(host_st->st_uid, &target_st->st_uid);
3265 __put_user(host_st->st_gid, &target_st->st_gid);
3266 __put_user(host_st->st_rdev, &target_st->st_rdev);
3267 __put_user(host_st->st_size, &target_st->st_size);
3268 __put_user(host_st->st_blksize, &target_st->st_blksize);
3269 __put_user(host_st->st_blocks, &target_st->st_blocks);
3270 __put_user(host_st->st_atime, &target_st->target_st_atime);
3271 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3272 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3273 unlock_user_struct(target_st, target_addr, 1);
3277 struct target_stat64 *target_st;
3279 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3280 return -TARGET_EFAULT;
3281 memset(target_st, 0, sizeof(struct target_stat64));
3282 __put_user(host_st->st_dev, &target_st->st_dev);
3283 __put_user(host_st->st_ino, &target_st->st_ino);
3284 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3285 __put_user(host_st->st_ino, &target_st->__st_ino);
3287 __put_user(host_st->st_mode, &target_st->st_mode);
3288 __put_user(host_st->st_nlink, &target_st->st_nlink);
3289 __put_user(host_st->st_uid, &target_st->st_uid);
3290 __put_user(host_st->st_gid, &target_st->st_gid);
3291 __put_user(host_st->st_rdev, &target_st->st_rdev);
3292 /* XXX: better use of kernel struct */
3293 __put_user(host_st->st_size, &target_st->st_size);
3294 __put_user(host_st->st_blksize, &target_st->st_blksize);
3295 __put_user(host_st->st_blocks, &target_st->st_blocks);
3296 __put_user(host_st->st_atime, &target_st->target_st_atime);
3297 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3298 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3299 unlock_user_struct(target_st, target_addr, 1);
3306 #if defined(USE_NPTL)
3307 /* ??? Using host futex calls even when target atomic operations
3308 are not really atomic probably breaks things. However implementing
3309 futexes locally would make futexes shared between multiple processes
3310 tricky. However they're probably useless because guest atomic
3311 operations won't work either. */
3312 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
3313 target_ulong uaddr2, int val3)
3315 struct timespec ts, *pts;
3317 /* ??? We assume FUTEX_* constants are the same on both host
3323 target_to_host_timespec(pts, timeout);
3327 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAIT, tswap32(val),
3330 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAKE, val, NULL, NULL, 0));
3332 return get_errno(sys_futex(g2h(uaddr), FUTEX_FD, val, NULL, NULL, 0));
3334 return get_errno(sys_futex(g2h(uaddr), FUTEX_REQUEUE, val,
3335 NULL, g2h(uaddr2), 0));
3336 case FUTEX_CMP_REQUEUE:
3337 return get_errno(sys_futex(g2h(uaddr), FUTEX_CMP_REQUEUE, val,
3338 NULL, g2h(uaddr2), tswap32(val3)));
3340 return -TARGET_ENOSYS;
3345 int get_osversion(void)
3347 static int osversion;
3348 struct new_utsname buf;
3353 if (qemu_uname_release && *qemu_uname_release) {
3354 s = qemu_uname_release;
3356 if (sys_uname(&buf))
3361 for (i = 0; i < 3; i++) {
3363 while (*s >= '0' && *s <= '9') {
3368 tmp = (tmp << 8) + n;
3376 /* do_syscall() should always have a single exit point at the end so
3377 that actions, such as logging of syscall results, can be performed.
3378 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
3379 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3380 abi_long arg2, abi_long arg3, abi_long arg4,
3381 abi_long arg5, abi_long arg6)
3389 gemu_log("syscall %d", num);
3392 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3395 case TARGET_NR_exit:
3399 gdb_exit(cpu_env, arg1);
3400 /* XXX: should free thread stack and CPU env */
3402 ret = 0; /* avoid warning */
3404 case TARGET_NR_read:
3405 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3407 ret = get_errno(read(arg1, p, arg3));
3408 unlock_user(p, arg2, ret);
3410 case TARGET_NR_write:
3411 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3413 ret = get_errno(write(arg1, p, arg3));
3414 unlock_user(p, arg2, 0);
3416 case TARGET_NR_open:
3417 if (!(p = lock_user_string(arg1)))
3419 ret = get_errno(open(path(p),
3420 target_to_host_bitmask(arg2, fcntl_flags_tbl),
3422 unlock_user(p, arg1, 0);
3424 #if defined(TARGET_NR_openat) && defined(__NR_openat)
3425 case TARGET_NR_openat:
3426 if (!(p = lock_user_string(arg2)))
3428 ret = get_errno(sys_openat(arg1,
3430 target_to_host_bitmask(arg3, fcntl_flags_tbl),
3432 unlock_user(p, arg2, 0);
3435 case TARGET_NR_close:
3436 ret = get_errno(close(arg1));
3441 case TARGET_NR_fork:
3442 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
3444 #ifdef TARGET_NR_waitpid
3445 case TARGET_NR_waitpid:
3448 ret = get_errno(waitpid(arg1, &status, arg3));
3449 if (!is_error(ret) && arg2
3450 && put_user_s32(status, arg2))
3455 #ifdef TARGET_NR_waitid
3456 case TARGET_NR_waitid:
3460 ret = get_errno(waitid(arg1, arg2, &info, arg4));
3461 if (!is_error(ret) && arg3 && info.si_pid != 0) {
3462 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
3464 host_to_target_siginfo(p, &info);
3465 unlock_user(p, arg3, sizeof(target_siginfo_t));
3470 #ifdef TARGET_NR_creat /* not on alpha */
3471 case TARGET_NR_creat:
3472 if (!(p = lock_user_string(arg1)))
3474 ret = get_errno(creat(p, arg2));
3475 unlock_user(p, arg1, 0);
3478 case TARGET_NR_link:
3481 p = lock_user_string(arg1);
3482 p2 = lock_user_string(arg2);
3484 ret = -TARGET_EFAULT;
3486 ret = get_errno(link(p, p2));
3487 unlock_user(p2, arg2, 0);
3488 unlock_user(p, arg1, 0);
3491 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3492 case TARGET_NR_linkat:
3497 p = lock_user_string(arg2);
3498 p2 = lock_user_string(arg4);
3500 ret = -TARGET_EFAULT;
3502 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
3503 unlock_user(p, arg2, 0);
3504 unlock_user(p2, arg4, 0);
3508 case TARGET_NR_unlink:
3509 if (!(p = lock_user_string(arg1)))
3511 ret = get_errno(unlink(p));
3512 unlock_user(p, arg1, 0);
3514 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3515 case TARGET_NR_unlinkat:
3516 if (!(p = lock_user_string(arg2)))
3518 ret = get_errno(sys_unlinkat(arg1, p, arg3));
3519 unlock_user(p, arg2, 0);
3522 case TARGET_NR_execve:
3524 char **argp, **envp;
3527 abi_ulong guest_argp;
3528 abi_ulong guest_envp;
3534 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
3535 if (get_user_ual(addr, gp))
3543 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
3544 if (get_user_ual(addr, gp))
3551 argp = alloca((argc + 1) * sizeof(void *));
3552 envp = alloca((envc + 1) * sizeof(void *));
3554 for (gp = guest_argp, q = argp; gp;
3555 gp += sizeof(abi_ulong), q++) {
3556 if (get_user_ual(addr, gp))
3560 if (!(*q = lock_user_string(addr)))
3565 for (gp = guest_envp, q = envp; gp;
3566 gp += sizeof(abi_ulong), q++) {
3567 if (get_user_ual(addr, gp))
3571 if (!(*q = lock_user_string(addr)))
3576 if (!(p = lock_user_string(arg1)))
3578 ret = get_errno(execve(p, argp, envp));
3579 unlock_user(p, arg1, 0);
3584 ret = -TARGET_EFAULT;
3587 for (gp = guest_argp, q = argp; *q;
3588 gp += sizeof(abi_ulong), q++) {
3589 if (get_user_ual(addr, gp)
3592 unlock_user(*q, addr, 0);
3594 for (gp = guest_envp, q = envp; *q;
3595 gp += sizeof(abi_ulong), q++) {
3596 if (get_user_ual(addr, gp)
3599 unlock_user(*q, addr, 0);
3603 case TARGET_NR_chdir:
3604 if (!(p = lock_user_string(arg1)))
3606 ret = get_errno(chdir(p));
3607 unlock_user(p, arg1, 0);
3609 #ifdef TARGET_NR_time
3610 case TARGET_NR_time:
3613 ret = get_errno(time(&host_time));
3616 && put_user_sal(host_time, arg1))
3621 case TARGET_NR_mknod:
3622 if (!(p = lock_user_string(arg1)))
3624 ret = get_errno(mknod(p, arg2, arg3));
3625 unlock_user(p, arg1, 0);
3627 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
3628 case TARGET_NR_mknodat:
3629 if (!(p = lock_user_string(arg2)))
3631 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
3632 unlock_user(p, arg2, 0);
3635 case TARGET_NR_chmod:
3636 if (!(p = lock_user_string(arg1)))
3638 ret = get_errno(chmod(p, arg2));
3639 unlock_user(p, arg1, 0);
3641 #ifdef TARGET_NR_break
3642 case TARGET_NR_break:
3645 #ifdef TARGET_NR_oldstat
3646 case TARGET_NR_oldstat:
3649 case TARGET_NR_lseek:
3650 ret = get_errno(lseek(arg1, arg2, arg3));
3652 #ifdef TARGET_NR_getxpid
3653 case TARGET_NR_getxpid:
3655 case TARGET_NR_getpid:
3657 ret = get_errno(getpid());
3659 case TARGET_NR_mount:
3661 /* need to look at the data field */
3663 p = lock_user_string(arg1);
3664 p2 = lock_user_string(arg2);
3665 p3 = lock_user_string(arg3);
3666 if (!p || !p2 || !p3)
3667 ret = -TARGET_EFAULT;
3669 /* FIXME - arg5 should be locked, but it isn't clear how to
3670 * do that since it's not guaranteed to be a NULL-terminated
3673 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3674 unlock_user(p, arg1, 0);
3675 unlock_user(p2, arg2, 0);
3676 unlock_user(p3, arg3, 0);
3679 #ifdef TARGET_NR_umount
3680 case TARGET_NR_umount:
3681 if (!(p = lock_user_string(arg1)))
3683 ret = get_errno(umount(p));
3684 unlock_user(p, arg1, 0);
3687 #ifdef TARGET_NR_stime /* not on alpha */
3688 case TARGET_NR_stime:
3691 if (get_user_sal(host_time, arg1))
3693 ret = get_errno(stime(&host_time));
3697 case TARGET_NR_ptrace:
3699 #ifdef TARGET_NR_alarm /* not on alpha */
3700 case TARGET_NR_alarm:
3704 #ifdef TARGET_NR_oldfstat
3705 case TARGET_NR_oldfstat:
3708 #ifdef TARGET_NR_pause /* not on alpha */
3709 case TARGET_NR_pause:
3710 ret = get_errno(pause());
3713 #ifdef TARGET_NR_utime
3714 case TARGET_NR_utime:
3716 struct utimbuf tbuf, *host_tbuf;
3717 struct target_utimbuf *target_tbuf;
3719 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3721 tbuf.actime = tswapl(target_tbuf->actime);
3722 tbuf.modtime = tswapl(target_tbuf->modtime);
3723 unlock_user_struct(target_tbuf, arg2, 0);
3728 if (!(p = lock_user_string(arg1)))
3730 ret = get_errno(utime(p, host_tbuf));
3731 unlock_user(p, arg1, 0);
3735 case TARGET_NR_utimes:
3737 struct timeval *tvp, tv[2];
3739 if (copy_from_user_timeval(&tv[0], arg2)
3740 || copy_from_user_timeval(&tv[1],
3741 arg2 + sizeof(struct target_timeval)))
3747 if (!(p = lock_user_string(arg1)))
3749 ret = get_errno(utimes(p, tvp));
3750 unlock_user(p, arg1, 0);
3753 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
3754 case TARGET_NR_futimesat:
3756 struct timeval *tvp, tv[2];
3758 if (copy_from_user_timeval(&tv[0], arg3)
3759 || copy_from_user_timeval(&tv[1],
3760 arg3 + sizeof(struct target_timeval)))
3766 if (!(p = lock_user_string(arg2)))
3768 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
3769 unlock_user(p, arg2, 0);
3773 #ifdef TARGET_NR_stty
3774 case TARGET_NR_stty:
3777 #ifdef TARGET_NR_gtty
3778 case TARGET_NR_gtty:
3781 case TARGET_NR_access:
3782 if (!(p = lock_user_string(arg1)))
3784 ret = get_errno(access(p, arg2));
3785 unlock_user(p, arg1, 0);
3787 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3788 case TARGET_NR_faccessat:
3789 if (!(p = lock_user_string(arg2)))
3791 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3792 unlock_user(p, arg2, 0);
3795 #ifdef TARGET_NR_nice /* not on alpha */
3796 case TARGET_NR_nice:
3797 ret = get_errno(nice(arg1));
3800 #ifdef TARGET_NR_ftime
3801 case TARGET_NR_ftime:
3804 case TARGET_NR_sync:
3808 case TARGET_NR_kill:
3809 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
3811 case TARGET_NR_rename:
3814 p = lock_user_string(arg1);
3815 p2 = lock_user_string(arg2);
3817 ret = -TARGET_EFAULT;
3819 ret = get_errno(rename(p, p2));
3820 unlock_user(p2, arg2, 0);
3821 unlock_user(p, arg1, 0);
3824 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3825 case TARGET_NR_renameat:
3828 p = lock_user_string(arg2);
3829 p2 = lock_user_string(arg4);
3831 ret = -TARGET_EFAULT;
3833 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
3834 unlock_user(p2, arg4, 0);
3835 unlock_user(p, arg2, 0);
3839 case TARGET_NR_mkdir:
3840 if (!(p = lock_user_string(arg1)))
3842 ret = get_errno(mkdir(p, arg2));
3843 unlock_user(p, arg1, 0);
3845 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3846 case TARGET_NR_mkdirat:
3847 if (!(p = lock_user_string(arg2)))
3849 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3850 unlock_user(p, arg2, 0);
3853 case TARGET_NR_rmdir:
3854 if (!(p = lock_user_string(arg1)))
3856 ret = get_errno(rmdir(p));
3857 unlock_user(p, arg1, 0);
3860 ret = get_errno(dup(arg1));
3862 case TARGET_NR_pipe:
3865 ret = get_errno(pipe(host_pipe));
3866 if (!is_error(ret)) {
3867 #if defined(TARGET_MIPS)
3868 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3869 env->active_tc.gpr[3] = host_pipe[1];
3871 #elif defined(TARGET_SH4)
3872 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
3875 if (put_user_s32(host_pipe[0], arg1)
3876 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
3882 case TARGET_NR_times:
3884 struct target_tms *tmsp;
3886 ret = get_errno(times(&tms));
3888 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3891 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3892 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3893 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3894 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
3897 ret = host_to_target_clock_t(ret);
3900 #ifdef TARGET_NR_prof
3901 case TARGET_NR_prof:
3904 #ifdef TARGET_NR_signal
3905 case TARGET_NR_signal:
3908 case TARGET_NR_acct:
3909 if (!(p = lock_user_string(arg1)))
3911 ret = get_errno(acct(path(p)));
3912 unlock_user(p, arg1, 0);
3914 #ifdef TARGET_NR_umount2 /* not on alpha */
3915 case TARGET_NR_umount2:
3916 if (!(p = lock_user_string(arg1)))
3918 ret = get_errno(umount2(p, arg2));
3919 unlock_user(p, arg1, 0);
3922 #ifdef TARGET_NR_lock
3923 case TARGET_NR_lock:
3926 case TARGET_NR_ioctl:
3927 ret = do_ioctl(arg1, arg2, arg3);
3929 case TARGET_NR_fcntl:
3930 ret = do_fcntl(arg1, arg2, arg3);
3932 #ifdef TARGET_NR_mpx
3936 case TARGET_NR_setpgid:
3937 ret = get_errno(setpgid(arg1, arg2));
3939 #ifdef TARGET_NR_ulimit
3940 case TARGET_NR_ulimit:
3943 #ifdef TARGET_NR_oldolduname
3944 case TARGET_NR_oldolduname:
3947 case TARGET_NR_umask:
3948 ret = get_errno(umask(arg1));
3950 case TARGET_NR_chroot:
3951 if (!(p = lock_user_string(arg1)))
3953 ret = get_errno(chroot(p));
3954 unlock_user(p, arg1, 0);
3956 case TARGET_NR_ustat:
3958 case TARGET_NR_dup2:
3959 ret = get_errno(dup2(arg1, arg2));
3961 #ifdef TARGET_NR_getppid /* not on alpha */
3962 case TARGET_NR_getppid:
3963 ret = get_errno(getppid());
3966 case TARGET_NR_getpgrp:
3967 ret = get_errno(getpgrp());
3969 case TARGET_NR_setsid:
3970 ret = get_errno(setsid());
3972 #ifdef TARGET_NR_sigaction
3973 case TARGET_NR_sigaction:
3975 #if !defined(TARGET_MIPS)
3976 struct target_old_sigaction *old_act;
3977 struct target_sigaction act, oact, *pact;
3979 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3981 act._sa_handler = old_act->_sa_handler;
3982 target_siginitset(&act.sa_mask, old_act->sa_mask);
3983 act.sa_flags = old_act->sa_flags;
3984 act.sa_restorer = old_act->sa_restorer;
3985 unlock_user_struct(old_act, arg2, 0);
3990 ret = get_errno(do_sigaction(arg1, pact, &oact));
3991 if (!is_error(ret) && arg3) {
3992 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3994 old_act->_sa_handler = oact._sa_handler;
3995 old_act->sa_mask = oact.sa_mask.sig[0];
3996 old_act->sa_flags = oact.sa_flags;
3997 old_act->sa_restorer = oact.sa_restorer;
3998 unlock_user_struct(old_act, arg3, 1);
4001 struct target_sigaction act, oact, *pact, *old_act;
4004 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4006 act._sa_handler = old_act->_sa_handler;
4007 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4008 act.sa_flags = old_act->sa_flags;
4009 unlock_user_struct(old_act, arg2, 0);
4015 ret = get_errno(do_sigaction(arg1, pact, &oact));
4017 if (!is_error(ret) && arg3) {
4018 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4020 old_act->_sa_handler = oact._sa_handler;
4021 old_act->sa_flags = oact.sa_flags;
4022 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4023 old_act->sa_mask.sig[1] = 0;
4024 old_act->sa_mask.sig[2] = 0;
4025 old_act->sa_mask.sig[3] = 0;
4026 unlock_user_struct(old_act, arg3, 1);
4032 case TARGET_NR_rt_sigaction:
4034 struct target_sigaction *act;
4035 struct target_sigaction *oact;
4038 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
4043 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
4044 ret = -TARGET_EFAULT;
4045 goto rt_sigaction_fail;
4049 ret = get_errno(do_sigaction(arg1, act, oact));
4052 unlock_user_struct(act, arg2, 0);
4054 unlock_user_struct(oact, arg3, 1);
4057 #ifdef TARGET_NR_sgetmask /* not on alpha */
4058 case TARGET_NR_sgetmask:
4061 abi_ulong target_set;
4062 sigprocmask(0, NULL, &cur_set);
4063 host_to_target_old_sigset(&target_set, &cur_set);
4068 #ifdef TARGET_NR_ssetmask /* not on alpha */
4069 case TARGET_NR_ssetmask:
4071 sigset_t set, oset, cur_set;
4072 abi_ulong target_set = arg1;
4073 sigprocmask(0, NULL, &cur_set);
4074 target_to_host_old_sigset(&set, &target_set);
4075 sigorset(&set, &set, &cur_set);
4076 sigprocmask(SIG_SETMASK, &set, &oset);
4077 host_to_target_old_sigset(&target_set, &oset);
4082 #ifdef TARGET_NR_sigprocmask
4083 case TARGET_NR_sigprocmask:
4086 sigset_t set, oldset, *set_ptr;
4090 case TARGET_SIG_BLOCK:
4093 case TARGET_SIG_UNBLOCK:
4096 case TARGET_SIG_SETMASK:
4100 ret = -TARGET_EINVAL;
4103 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4105 target_to_host_old_sigset(&set, p);
4106 unlock_user(p, arg2, 0);
4112 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
4113 if (!is_error(ret) && arg3) {
4114 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4116 host_to_target_old_sigset(p, &oldset);
4117 unlock_user(p, arg3, sizeof(target_sigset_t));
4122 case TARGET_NR_rt_sigprocmask:
4125 sigset_t set, oldset, *set_ptr;
4129 case TARGET_SIG_BLOCK:
4132 case TARGET_SIG_UNBLOCK:
4135 case TARGET_SIG_SETMASK:
4139 ret = -TARGET_EINVAL;
4142 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4144 target_to_host_sigset(&set, p);
4145 unlock_user(p, arg2, 0);
4151 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
4152 if (!is_error(ret) && arg3) {
4153 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4155 host_to_target_sigset(p, &oldset);
4156 unlock_user(p, arg3, sizeof(target_sigset_t));
4160 #ifdef TARGET_NR_sigpending
4161 case TARGET_NR_sigpending:
4164 ret = get_errno(sigpending(&set));
4165 if (!is_error(ret)) {
4166 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4168 host_to_target_old_sigset(p, &set);
4169 unlock_user(p, arg1, sizeof(target_sigset_t));
4174 case TARGET_NR_rt_sigpending:
4177 ret = get_errno(sigpending(&set));
4178 if (!is_error(ret)) {
4179 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4181 host_to_target_sigset(p, &set);
4182 unlock_user(p, arg1, sizeof(target_sigset_t));
4186 #ifdef TARGET_NR_sigsuspend
4187 case TARGET_NR_sigsuspend:
4190 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4192 target_to_host_old_sigset(&set, p);
4193 unlock_user(p, arg1, 0);
4194 ret = get_errno(sigsuspend(&set));
4198 case TARGET_NR_rt_sigsuspend:
4201 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4203 target_to_host_sigset(&set, p);
4204 unlock_user(p, arg1, 0);
4205 ret = get_errno(sigsuspend(&set));
4208 case TARGET_NR_rt_sigtimedwait:
4211 struct timespec uts, *puts;
4214 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4216 target_to_host_sigset(&set, p);
4217 unlock_user(p, arg1, 0);
4220 target_to_host_timespec(puts, arg3);
4224 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
4225 if (!is_error(ret) && arg2) {
4226 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
4228 host_to_target_siginfo(p, &uinfo);
4229 unlock_user(p, arg2, sizeof(target_siginfo_t));
4233 case TARGET_NR_rt_sigqueueinfo:
4236 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
4238 target_to_host_siginfo(&uinfo, p);
4239 unlock_user(p, arg1, 0);
4240 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
4243 #ifdef TARGET_NR_sigreturn
4244 case TARGET_NR_sigreturn:
4245 /* NOTE: ret is eax, so not transcoding must be done */
4246 ret = do_sigreturn(cpu_env);
4249 case TARGET_NR_rt_sigreturn:
4250 /* NOTE: ret is eax, so not transcoding must be done */
4251 ret = do_rt_sigreturn(cpu_env);
4253 case TARGET_NR_sethostname:
4254 if (!(p = lock_user_string(arg1)))
4256 ret = get_errno(sethostname(p, arg2));
4257 unlock_user(p, arg1, 0);
4259 case TARGET_NR_setrlimit:
4261 /* XXX: convert resource ? */
4262 int resource = arg1;
4263 struct target_rlimit *target_rlim;
4265 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
4267 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4268 rlim.rlim_max = tswapl(target_rlim->rlim_max);
4269 unlock_user_struct(target_rlim, arg2, 0);
4270 ret = get_errno(setrlimit(resource, &rlim));
4273 case TARGET_NR_getrlimit:
4275 /* XXX: convert resource ? */
4276 int resource = arg1;
4277 struct target_rlimit *target_rlim;
4280 ret = get_errno(getrlimit(resource, &rlim));
4281 if (!is_error(ret)) {
4282 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4284 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4285 rlim.rlim_max = tswapl(target_rlim->rlim_max);
4286 unlock_user_struct(target_rlim, arg2, 1);
4290 case TARGET_NR_getrusage:
4292 struct rusage rusage;
4293 ret = get_errno(getrusage(arg1, &rusage));
4294 if (!is_error(ret)) {
4295 host_to_target_rusage(arg2, &rusage);
4299 case TARGET_NR_gettimeofday:
4302 ret = get_errno(gettimeofday(&tv, NULL));
4303 if (!is_error(ret)) {
4304 if (copy_to_user_timeval(arg1, &tv))
4309 case TARGET_NR_settimeofday:
4312 if (copy_from_user_timeval(&tv, arg1))
4314 ret = get_errno(settimeofday(&tv, NULL));
4317 #ifdef TARGET_NR_select
4318 case TARGET_NR_select:
4320 struct target_sel_arg_struct *sel;
4321 abi_ulong inp, outp, exp, tvp;
4324 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
4326 nsel = tswapl(sel->n);
4327 inp = tswapl(sel->inp);
4328 outp = tswapl(sel->outp);
4329 exp = tswapl(sel->exp);
4330 tvp = tswapl(sel->tvp);
4331 unlock_user_struct(sel, arg1, 0);
4332 ret = do_select(nsel, inp, outp, exp, tvp);
4336 case TARGET_NR_symlink:
4339 p = lock_user_string(arg1);
4340 p2 = lock_user_string(arg2);
4342 ret = -TARGET_EFAULT;
4344 ret = get_errno(symlink(p, p2));
4345 unlock_user(p2, arg2, 0);
4346 unlock_user(p, arg1, 0);
4349 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
4350 case TARGET_NR_symlinkat:
4353 p = lock_user_string(arg1);
4354 p2 = lock_user_string(arg3);
4356 ret = -TARGET_EFAULT;
4358 ret = get_errno(sys_symlinkat(p, arg2, p2));
4359 unlock_user(p2, arg3, 0);
4360 unlock_user(p, arg1, 0);
4364 #ifdef TARGET_NR_oldlstat
4365 case TARGET_NR_oldlstat:
4368 case TARGET_NR_readlink:
4371 p = lock_user_string(arg1);
4372 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4374 ret = -TARGET_EFAULT;
4376 ret = get_errno(readlink(path(p), p2, arg3));
4377 unlock_user(p2, arg2, ret);
4378 unlock_user(p, arg1, 0);
4381 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4382 case TARGET_NR_readlinkat:
4385 p = lock_user_string(arg2);
4386 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4388 ret = -TARGET_EFAULT;
4390 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
4391 unlock_user(p2, arg3, ret);
4392 unlock_user(p, arg2, 0);
4396 #ifdef TARGET_NR_uselib
4397 case TARGET_NR_uselib:
4400 #ifdef TARGET_NR_swapon
4401 case TARGET_NR_swapon:
4402 if (!(p = lock_user_string(arg1)))
4404 ret = get_errno(swapon(p, arg2));
4405 unlock_user(p, arg1, 0);
4408 case TARGET_NR_reboot:
4410 #ifdef TARGET_NR_readdir
4411 case TARGET_NR_readdir:
4414 #ifdef TARGET_NR_mmap
4415 case TARGET_NR_mmap:
4416 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
4419 abi_ulong v1, v2, v3, v4, v5, v6;
4420 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4428 unlock_user(v, arg1, 0);
4429 ret = get_errno(target_mmap(v1, v2, v3,
4430 target_to_host_bitmask(v4, mmap_flags_tbl),
4434 ret = get_errno(target_mmap(arg1, arg2, arg3,
4435 target_to_host_bitmask(arg4, mmap_flags_tbl),
4441 #ifdef TARGET_NR_mmap2
4442 case TARGET_NR_mmap2:
4444 #define MMAP_SHIFT 12
4446 ret = get_errno(target_mmap(arg1, arg2, arg3,
4447 target_to_host_bitmask(arg4, mmap_flags_tbl),
4449 arg6 << MMAP_SHIFT));
4452 case TARGET_NR_munmap:
4453 ret = get_errno(target_munmap(arg1, arg2));
4455 case TARGET_NR_mprotect:
4456 ret = get_errno(target_mprotect(arg1, arg2, arg3));
4458 #ifdef TARGET_NR_mremap
4459 case TARGET_NR_mremap:
4460 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
4463 /* ??? msync/mlock/munlock are broken for softmmu. */
4464 #ifdef TARGET_NR_msync
4465 case TARGET_NR_msync:
4466 ret = get_errno(msync(g2h(arg1), arg2, arg3));
4469 #ifdef TARGET_NR_mlock
4470 case TARGET_NR_mlock:
4471 ret = get_errno(mlock(g2h(arg1), arg2));
4474 #ifdef TARGET_NR_munlock
4475 case TARGET_NR_munlock:
4476 ret = get_errno(munlock(g2h(arg1), arg2));
4479 #ifdef TARGET_NR_mlockall
4480 case TARGET_NR_mlockall:
4481 ret = get_errno(mlockall(arg1));
4484 #ifdef TARGET_NR_munlockall
4485 case TARGET_NR_munlockall:
4486 ret = get_errno(munlockall());
4489 case TARGET_NR_truncate:
4490 if (!(p = lock_user_string(arg1)))
4492 ret = get_errno(truncate(p, arg2));
4493 unlock_user(p, arg1, 0);
4495 case TARGET_NR_ftruncate:
4496 ret = get_errno(ftruncate(arg1, arg2));
4498 case TARGET_NR_fchmod:
4499 ret = get_errno(fchmod(arg1, arg2));
4501 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
4502 case TARGET_NR_fchmodat:
4503 if (!(p = lock_user_string(arg2)))
4505 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
4506 unlock_user(p, arg2, 0);
4509 case TARGET_NR_getpriority:
4510 /* libc does special remapping of the return value of
4511 * sys_getpriority() so it's just easiest to call
4512 * sys_getpriority() directly rather than through libc. */
4513 ret = sys_getpriority(arg1, arg2);
4515 case TARGET_NR_setpriority:
4516 ret = get_errno(setpriority(arg1, arg2, arg3));
4518 #ifdef TARGET_NR_profil
4519 case TARGET_NR_profil:
4522 case TARGET_NR_statfs:
4523 if (!(p = lock_user_string(arg1)))
4525 ret = get_errno(statfs(path(p), &stfs));
4526 unlock_user(p, arg1, 0);
4528 if (!is_error(ret)) {
4529 struct target_statfs *target_stfs;
4531 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
4533 __put_user(stfs.f_type, &target_stfs->f_type);
4534 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4535 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4536 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4537 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4538 __put_user(stfs.f_files, &target_stfs->f_files);
4539 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4540 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4541 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4542 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4543 unlock_user_struct(target_stfs, arg2, 1);
4546 case TARGET_NR_fstatfs:
4547 ret = get_errno(fstatfs(arg1, &stfs));
4548 goto convert_statfs;
4549 #ifdef TARGET_NR_statfs64
4550 case TARGET_NR_statfs64:
4551 if (!(p = lock_user_string(arg1)))
4553 ret = get_errno(statfs(path(p), &stfs));
4554 unlock_user(p, arg1, 0);
4556 if (!is_error(ret)) {
4557 struct target_statfs64 *target_stfs;
4559 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
4561 __put_user(stfs.f_type, &target_stfs->f_type);
4562 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4563 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4564 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4565 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4566 __put_user(stfs.f_files, &target_stfs->f_files);
4567 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4568 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4569 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4570 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4571 unlock_user_struct(target_stfs, arg3, 1);
4574 case TARGET_NR_fstatfs64:
4575 ret = get_errno(fstatfs(arg1, &stfs));
4576 goto convert_statfs64;
4578 #ifdef TARGET_NR_ioperm
4579 case TARGET_NR_ioperm:
4582 #ifdef TARGET_NR_socketcall
4583 case TARGET_NR_socketcall:
4584 ret = do_socketcall(arg1, arg2);
4587 #ifdef TARGET_NR_accept
4588 case TARGET_NR_accept:
4589 ret = do_accept(arg1, arg2, arg3);
4592 #ifdef TARGET_NR_bind
4593 case TARGET_NR_bind:
4594 ret = do_bind(arg1, arg2, arg3);
4597 #ifdef TARGET_NR_connect
4598 case TARGET_NR_connect:
4599 ret = do_connect(arg1, arg2, arg3);
4602 #ifdef TARGET_NR_getpeername
4603 case TARGET_NR_getpeername:
4604 ret = do_getpeername(arg1, arg2, arg3);
4607 #ifdef TARGET_NR_getsockname
4608 case TARGET_NR_getsockname:
4609 ret = do_getsockname(arg1, arg2, arg3);
4612 #ifdef TARGET_NR_getsockopt
4613 case TARGET_NR_getsockopt:
4614 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
4617 #ifdef TARGET_NR_listen
4618 case TARGET_NR_listen:
4619 ret = get_errno(listen(arg1, arg2));
4622 #ifdef TARGET_NR_recv
4623 case TARGET_NR_recv:
4624 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
4627 #ifdef TARGET_NR_recvfrom
4628 case TARGET_NR_recvfrom:
4629 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
4632 #ifdef TARGET_NR_recvmsg
4633 case TARGET_NR_recvmsg:
4634 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
4637 #ifdef TARGET_NR_send
4638 case TARGET_NR_send:
4639 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
4642 #ifdef TARGET_NR_sendmsg
4643 case TARGET_NR_sendmsg:
4644 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
4647 #ifdef TARGET_NR_sendto
4648 case TARGET_NR_sendto:
4649 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
4652 #ifdef TARGET_NR_shutdown
4653 case TARGET_NR_shutdown:
4654 ret = get_errno(shutdown(arg1, arg2));
4657 #ifdef TARGET_NR_socket
4658 case TARGET_NR_socket:
4659 ret = do_socket(arg1, arg2, arg3);
4662 #ifdef TARGET_NR_socketpair
4663 case TARGET_NR_socketpair:
4664 ret = do_socketpair(arg1, arg2, arg3, arg4);
4667 #ifdef TARGET_NR_setsockopt
4668 case TARGET_NR_setsockopt:
4669 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
4673 case TARGET_NR_syslog:
4674 if (!(p = lock_user_string(arg2)))
4676 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
4677 unlock_user(p, arg2, 0);
4680 case TARGET_NR_setitimer:
4682 struct itimerval value, ovalue, *pvalue;
4686 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
4687 || copy_from_user_timeval(&pvalue->it_value,
4688 arg2 + sizeof(struct target_timeval)))
4693 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
4694 if (!is_error(ret) && arg3) {
4695 if (copy_to_user_timeval(arg3,
4696 &ovalue.it_interval)
4697 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
4703 case TARGET_NR_getitimer:
4705 struct itimerval value;
4707 ret = get_errno(getitimer(arg1, &value));
4708 if (!is_error(ret) && arg2) {
4709 if (copy_to_user_timeval(arg2,
4711 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
4717 case TARGET_NR_stat:
4718 if (!(p = lock_user_string(arg1)))
4720 ret = get_errno(stat(path(p), &st));
4721 unlock_user(p, arg1, 0);
4723 case TARGET_NR_lstat:
4724 if (!(p = lock_user_string(arg1)))
4726 ret = get_errno(lstat(path(p), &st));
4727 unlock_user(p, arg1, 0);
4729 case TARGET_NR_fstat:
4731 ret = get_errno(fstat(arg1, &st));
4733 if (!is_error(ret)) {
4734 struct target_stat *target_st;
4736 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4738 __put_user(st.st_dev, &target_st->st_dev);
4739 __put_user(st.st_ino, &target_st->st_ino);
4740 __put_user(st.st_mode, &target_st->st_mode);
4741 __put_user(st.st_uid, &target_st->st_uid);
4742 __put_user(st.st_gid, &target_st->st_gid);
4743 __put_user(st.st_nlink, &target_st->st_nlink);
4744 __put_user(st.st_rdev, &target_st->st_rdev);
4745 __put_user(st.st_size, &target_st->st_size);
4746 __put_user(st.st_blksize, &target_st->st_blksize);
4747 __put_user(st.st_blocks, &target_st->st_blocks);
4748 __put_user(st.st_atime, &target_st->target_st_atime);
4749 __put_user(st.st_mtime, &target_st->target_st_mtime);
4750 __put_user(st.st_ctime, &target_st->target_st_ctime);
4751 unlock_user_struct(target_st, arg2, 1);
4755 #ifdef TARGET_NR_olduname
4756 case TARGET_NR_olduname:
4759 #ifdef TARGET_NR_iopl
4760 case TARGET_NR_iopl:
4763 case TARGET_NR_vhangup:
4764 ret = get_errno(vhangup());
4766 #ifdef TARGET_NR_idle
4767 case TARGET_NR_idle:
4770 #ifdef TARGET_NR_syscall
4771 case TARGET_NR_syscall:
4772 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4775 case TARGET_NR_wait4:
4778 abi_long status_ptr = arg2;
4779 struct rusage rusage, *rusage_ptr;
4780 abi_ulong target_rusage = arg4;
4782 rusage_ptr = &rusage;
4785 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4786 if (!is_error(ret)) {
4788 if (put_user_s32(status, status_ptr))
4792 host_to_target_rusage(target_rusage, &rusage);
4796 #ifdef TARGET_NR_swapoff
4797 case TARGET_NR_swapoff:
4798 if (!(p = lock_user_string(arg1)))
4800 ret = get_errno(swapoff(p));
4801 unlock_user(p, arg1, 0);
4804 case TARGET_NR_sysinfo:
4806 struct target_sysinfo *target_value;
4807 struct sysinfo value;
4808 ret = get_errno(sysinfo(&value));
4809 if (!is_error(ret) && arg1)
4811 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4813 __put_user(value.uptime, &target_value->uptime);
4814 __put_user(value.loads[0], &target_value->loads[0]);
4815 __put_user(value.loads[1], &target_value->loads[1]);
4816 __put_user(value.loads[2], &target_value->loads[2]);
4817 __put_user(value.totalram, &target_value->totalram);
4818 __put_user(value.freeram, &target_value->freeram);
4819 __put_user(value.sharedram, &target_value->sharedram);
4820 __put_user(value.bufferram, &target_value->bufferram);
4821 __put_user(value.totalswap, &target_value->totalswap);
4822 __put_user(value.freeswap, &target_value->freeswap);
4823 __put_user(value.procs, &target_value->procs);
4824 __put_user(value.totalhigh, &target_value->totalhigh);
4825 __put_user(value.freehigh, &target_value->freehigh);
4826 __put_user(value.mem_unit, &target_value->mem_unit);
4827 unlock_user_struct(target_value, arg1, 1);
4831 #ifdef TARGET_NR_ipc
4833 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4837 #ifdef TARGET_NR_msgctl
4838 case TARGET_NR_msgctl:
4839 ret = do_msgctl(arg1, arg2, arg3);
4842 #ifdef TARGET_NR_msgget
4843 case TARGET_NR_msgget:
4844 ret = get_errno(msgget(arg1, arg2));
4847 #ifdef TARGET_NR_msgrcv
4848 case TARGET_NR_msgrcv:
4849 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
4852 #ifdef TARGET_NR_msgsnd
4853 case TARGET_NR_msgsnd:
4854 ret = do_msgsnd(arg1, arg2, arg3, arg4);
4857 case TARGET_NR_fsync:
4858 ret = get_errno(fsync(arg1));
4860 case TARGET_NR_clone:
4861 #if defined(TARGET_SH4)
4862 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
4864 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
4867 #ifdef __NR_exit_group
4868 /* new thread calls */
4869 case TARGET_NR_exit_group:
4873 gdb_exit(cpu_env, arg1);
4874 ret = get_errno(exit_group(arg1));
4877 case TARGET_NR_setdomainname:
4878 if (!(p = lock_user_string(arg1)))
4880 ret = get_errno(setdomainname(p, arg2));
4881 unlock_user(p, arg1, 0);
4883 case TARGET_NR_uname:
4884 /* no need to transcode because we use the linux syscall */
4886 struct new_utsname * buf;
4888 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4890 ret = get_errno(sys_uname(buf));
4891 if (!is_error(ret)) {
4892 /* Overrite the native machine name with whatever is being
4894 strcpy (buf->machine, UNAME_MACHINE);
4895 /* Allow the user to override the reported release. */
4896 if (qemu_uname_release && *qemu_uname_release)
4897 strcpy (buf->release, qemu_uname_release);
4899 unlock_user_struct(buf, arg1, 1);
4903 case TARGET_NR_modify_ldt:
4904 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
4906 #if !defined(TARGET_X86_64)
4907 case TARGET_NR_vm86old:
4909 case TARGET_NR_vm86:
4910 ret = do_vm86(cpu_env, arg1, arg2);
4914 case TARGET_NR_adjtimex:
4916 #ifdef TARGET_NR_create_module
4917 case TARGET_NR_create_module:
4919 case TARGET_NR_init_module:
4920 case TARGET_NR_delete_module:
4921 #ifdef TARGET_NR_get_kernel_syms
4922 case TARGET_NR_get_kernel_syms:
4925 case TARGET_NR_quotactl:
4927 case TARGET_NR_getpgid:
4928 ret = get_errno(getpgid(arg1));
4930 case TARGET_NR_fchdir:
4931 ret = get_errno(fchdir(arg1));
4933 #ifdef TARGET_NR_bdflush /* not on x86_64 */
4934 case TARGET_NR_bdflush:
4937 #ifdef TARGET_NR_sysfs
4938 case TARGET_NR_sysfs:
4941 case TARGET_NR_personality:
4942 ret = get_errno(personality(arg1));
4944 #ifdef TARGET_NR_afs_syscall
4945 case TARGET_NR_afs_syscall:
4948 #ifdef TARGET_NR__llseek /* Not on alpha */
4949 case TARGET_NR__llseek:
4951 #if defined (__x86_64__)
4952 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
4953 if (put_user_s64(ret, arg4))
4957 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4958 if (put_user_s64(res, arg4))
4964 case TARGET_NR_getdents:
4965 #if TARGET_ABI_BITS != 32
4967 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4969 struct target_dirent *target_dirp;
4970 struct linux_dirent *dirp;
4971 abi_long count = arg3;
4973 dirp = malloc(count);
4975 ret = -TARGET_ENOMEM;
4979 ret = get_errno(sys_getdents(arg1, dirp, count));
4980 if (!is_error(ret)) {
4981 struct linux_dirent *de;
4982 struct target_dirent *tde;
4984 int reclen, treclen;
4985 int count1, tnamelen;
4989 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4993 reclen = de->d_reclen;
4994 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4995 tde->d_reclen = tswap16(treclen);
4996 tde->d_ino = tswapl(de->d_ino);
4997 tde->d_off = tswapl(de->d_off);
4998 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
5001 /* XXX: may not be correct */
5002 pstrcpy(tde->d_name, tnamelen, de->d_name);
5003 de = (struct linux_dirent *)((char *)de + reclen);
5005 tde = (struct target_dirent *)((char *)tde + treclen);
5009 unlock_user(target_dirp, arg2, ret);
5015 struct linux_dirent *dirp;
5016 abi_long count = arg3;
5018 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5020 ret = get_errno(sys_getdents(arg1, dirp, count));
5021 if (!is_error(ret)) {
5022 struct linux_dirent *de;
5027 reclen = de->d_reclen;
5030 de->d_reclen = tswap16(reclen);
5031 tswapls(&de->d_ino);
5032 tswapls(&de->d_off);
5033 de = (struct linux_dirent *)((char *)de + reclen);
5037 unlock_user(dirp, arg2, ret);
5041 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
5042 case TARGET_NR_getdents64:
5044 struct linux_dirent64 *dirp;
5045 abi_long count = arg3;
5046 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5048 ret = get_errno(sys_getdents64(arg1, dirp, count));
5049 if (!is_error(ret)) {
5050 struct linux_dirent64 *de;
5055 reclen = de->d_reclen;
5058 de->d_reclen = tswap16(reclen);
5059 tswap64s((uint64_t *)&de->d_ino);
5060 tswap64s((uint64_t *)&de->d_off);
5061 de = (struct linux_dirent64 *)((char *)de + reclen);
5065 unlock_user(dirp, arg2, ret);
5068 #endif /* TARGET_NR_getdents64 */
5069 #ifdef TARGET_NR__newselect
5070 case TARGET_NR__newselect:
5071 ret = do_select(arg1, arg2, arg3, arg4, arg5);
5074 #ifdef TARGET_NR_poll
5075 case TARGET_NR_poll:
5077 struct target_pollfd *target_pfd;
5078 unsigned int nfds = arg2;
5083 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
5086 pfd = alloca(sizeof(struct pollfd) * nfds);
5087 for(i = 0; i < nfds; i++) {
5088 pfd[i].fd = tswap32(target_pfd[i].fd);
5089 pfd[i].events = tswap16(target_pfd[i].events);
5091 ret = get_errno(poll(pfd, nfds, timeout));
5092 if (!is_error(ret)) {
5093 for(i = 0; i < nfds; i++) {
5094 target_pfd[i].revents = tswap16(pfd[i].revents);
5096 ret += nfds * (sizeof(struct target_pollfd)
5097 - sizeof(struct pollfd));
5099 unlock_user(target_pfd, arg1, ret);
5103 case TARGET_NR_flock:
5104 /* NOTE: the flock constant seems to be the same for every
5106 ret = get_errno(flock(arg1, arg2));
5108 case TARGET_NR_readv:
5113 vec = alloca(count * sizeof(struct iovec));
5114 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
5116 ret = get_errno(readv(arg1, vec, count));
5117 unlock_iovec(vec, arg2, count, 1);
5120 case TARGET_NR_writev:
5125 vec = alloca(count * sizeof(struct iovec));
5126 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
5128 ret = get_errno(writev(arg1, vec, count));
5129 unlock_iovec(vec, arg2, count, 0);
5132 case TARGET_NR_getsid:
5133 ret = get_errno(getsid(arg1));
5135 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
5136 case TARGET_NR_fdatasync:
5137 ret = get_errno(fdatasync(arg1));
5140 case TARGET_NR__sysctl:
5141 /* We don't implement this, but ENOTDIR is always a safe
5143 ret = -TARGET_ENOTDIR;
5145 case TARGET_NR_sched_setparam:
5147 struct sched_param *target_schp;
5148 struct sched_param schp;
5150 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
5152 schp.sched_priority = tswap32(target_schp->sched_priority);
5153 unlock_user_struct(target_schp, arg2, 0);
5154 ret = get_errno(sched_setparam(arg1, &schp));
5157 case TARGET_NR_sched_getparam:
5159 struct sched_param *target_schp;
5160 struct sched_param schp;
5161 ret = get_errno(sched_getparam(arg1, &schp));
5162 if (!is_error(ret)) {
5163 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
5165 target_schp->sched_priority = tswap32(schp.sched_priority);
5166 unlock_user_struct(target_schp, arg2, 1);
5170 case TARGET_NR_sched_setscheduler:
5172 struct sched_param *target_schp;
5173 struct sched_param schp;
5174 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
5176 schp.sched_priority = tswap32(target_schp->sched_priority);
5177 unlock_user_struct(target_schp, arg3, 0);
5178 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
5181 case TARGET_NR_sched_getscheduler:
5182 ret = get_errno(sched_getscheduler(arg1));
5184 case TARGET_NR_sched_yield:
5185 ret = get_errno(sched_yield());
5187 case TARGET_NR_sched_get_priority_max:
5188 ret = get_errno(sched_get_priority_max(arg1));
5190 case TARGET_NR_sched_get_priority_min:
5191 ret = get_errno(sched_get_priority_min(arg1));
5193 case TARGET_NR_sched_rr_get_interval:
5196 ret = get_errno(sched_rr_get_interval(arg1, &ts));
5197 if (!is_error(ret)) {
5198 host_to_target_timespec(arg2, &ts);
5202 case TARGET_NR_nanosleep:
5204 struct timespec req, rem;
5205 target_to_host_timespec(&req, arg1);
5206 ret = get_errno(nanosleep(&req, &rem));
5207 if (is_error(ret) && arg2) {
5208 host_to_target_timespec(arg2, &rem);
5212 #ifdef TARGET_NR_query_module
5213 case TARGET_NR_query_module:
5216 #ifdef TARGET_NR_nfsservctl
5217 case TARGET_NR_nfsservctl:
5220 case TARGET_NR_prctl:
5223 case PR_GET_PDEATHSIG:
5226 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
5227 if (!is_error(ret) && arg2
5228 && put_user_ual(deathsig, arg2))
5233 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
5237 #ifdef TARGET_NR_arch_prctl
5238 case TARGET_NR_arch_prctl:
5239 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
5240 ret = do_arch_prctl(cpu_env, arg1, arg2);
5246 #ifdef TARGET_NR_pread
5247 case TARGET_NR_pread:
5249 if (((CPUARMState *)cpu_env)->eabi)
5252 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5254 ret = get_errno(pread(arg1, p, arg3, arg4));
5255 unlock_user(p, arg2, ret);
5257 case TARGET_NR_pwrite:
5259 if (((CPUARMState *)cpu_env)->eabi)
5262 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5264 ret = get_errno(pwrite(arg1, p, arg3, arg4));
5265 unlock_user(p, arg2, 0);
5268 #ifdef TARGET_NR_pread64
5269 case TARGET_NR_pread64:
5270 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5272 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
5273 unlock_user(p, arg2, ret);
5275 case TARGET_NR_pwrite64:
5276 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5278 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
5279 unlock_user(p, arg2, 0);
5282 case TARGET_NR_getcwd:
5283 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
5285 ret = get_errno(sys_getcwd1(p, arg2));
5286 unlock_user(p, arg1, ret);
5288 case TARGET_NR_capget:
5290 case TARGET_NR_capset:
5292 case TARGET_NR_sigaltstack:
5293 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
5294 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
5295 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
5300 case TARGET_NR_sendfile:
5302 #ifdef TARGET_NR_getpmsg
5303 case TARGET_NR_getpmsg:
5306 #ifdef TARGET_NR_putpmsg
5307 case TARGET_NR_putpmsg:
5310 #ifdef TARGET_NR_vfork
5311 case TARGET_NR_vfork:
5312 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
5316 #ifdef TARGET_NR_ugetrlimit
5317 case TARGET_NR_ugetrlimit:
5320 ret = get_errno(getrlimit(arg1, &rlim));
5321 if (!is_error(ret)) {
5322 struct target_rlimit *target_rlim;
5323 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5325 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
5326 target_rlim->rlim_max = tswapl(rlim.rlim_max);
5327 unlock_user_struct(target_rlim, arg2, 1);
5332 #ifdef TARGET_NR_truncate64
5333 case TARGET_NR_truncate64:
5334 if (!(p = lock_user_string(arg1)))
5336 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
5337 unlock_user(p, arg1, 0);
5340 #ifdef TARGET_NR_ftruncate64
5341 case TARGET_NR_ftruncate64:
5342 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
5345 #ifdef TARGET_NR_stat64
5346 case TARGET_NR_stat64:
5347 if (!(p = lock_user_string(arg1)))
5349 ret = get_errno(stat(path(p), &st));
5350 unlock_user(p, arg1, 0);
5352 ret = host_to_target_stat64(cpu_env, arg2, &st);
5355 #ifdef TARGET_NR_lstat64
5356 case TARGET_NR_lstat64:
5357 if (!(p = lock_user_string(arg1)))
5359 ret = get_errno(lstat(path(p), &st));
5360 unlock_user(p, arg1, 0);
5362 ret = host_to_target_stat64(cpu_env, arg2, &st);
5365 #ifdef TARGET_NR_fstat64
5366 case TARGET_NR_fstat64:
5367 ret = get_errno(fstat(arg1, &st));
5369 ret = host_to_target_stat64(cpu_env, arg2, &st);
5372 #if defined(TARGET_NR_fstatat64) && defined(__NR_fstatat64)
5373 case TARGET_NR_fstatat64:
5374 if (!(p = lock_user_string(arg2)))
5376 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
5378 ret = host_to_target_stat64(cpu_env, arg3, &st);
5382 case TARGET_NR_lchown:
5383 if (!(p = lock_user_string(arg1)))
5385 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5386 unlock_user(p, arg1, 0);
5388 case TARGET_NR_getuid:
5389 ret = get_errno(high2lowuid(getuid()));
5391 case TARGET_NR_getgid:
5392 ret = get_errno(high2lowgid(getgid()));
5394 case TARGET_NR_geteuid:
5395 ret = get_errno(high2lowuid(geteuid()));
5397 case TARGET_NR_getegid:
5398 ret = get_errno(high2lowgid(getegid()));
5400 case TARGET_NR_setreuid:
5401 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5403 case TARGET_NR_setregid:
5404 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5406 case TARGET_NR_getgroups:
5408 int gidsetsize = arg1;
5409 uint16_t *target_grouplist;
5413 grouplist = alloca(gidsetsize * sizeof(gid_t));
5414 ret = get_errno(getgroups(gidsetsize, grouplist));
5415 if (gidsetsize == 0)
5417 if (!is_error(ret)) {
5418 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5419 if (!target_grouplist)
5421 for(i = 0;i < ret; i++)
5422 target_grouplist[i] = tswap16(grouplist[i]);
5423 unlock_user(target_grouplist, arg2, gidsetsize * 2);
5427 case TARGET_NR_setgroups:
5429 int gidsetsize = arg1;
5430 uint16_t *target_grouplist;
5434 grouplist = alloca(gidsetsize * sizeof(gid_t));
5435 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5436 if (!target_grouplist) {
5437 ret = -TARGET_EFAULT;
5440 for(i = 0;i < gidsetsize; i++)
5441 grouplist[i] = tswap16(target_grouplist[i]);
5442 unlock_user(target_grouplist, arg2, 0);
5443 ret = get_errno(setgroups(gidsetsize, grouplist));
5446 case TARGET_NR_fchown:
5447 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5449 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5450 case TARGET_NR_fchownat:
5451 if (!(p = lock_user_string(arg2)))
5453 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5454 unlock_user(p, arg2, 0);
5457 #ifdef TARGET_NR_setresuid
5458 case TARGET_NR_setresuid:
5459 ret = get_errno(setresuid(low2highuid(arg1),
5461 low2highuid(arg3)));
5464 #ifdef TARGET_NR_getresuid
5465 case TARGET_NR_getresuid:
5467 uid_t ruid, euid, suid;
5468 ret = get_errno(getresuid(&ruid, &euid, &suid));
5469 if (!is_error(ret)) {
5470 if (put_user_u16(high2lowuid(ruid), arg1)
5471 || put_user_u16(high2lowuid(euid), arg2)
5472 || put_user_u16(high2lowuid(suid), arg3))
5478 #ifdef TARGET_NR_getresgid
5479 case TARGET_NR_setresgid:
5480 ret = get_errno(setresgid(low2highgid(arg1),
5482 low2highgid(arg3)));
5485 #ifdef TARGET_NR_getresgid
5486 case TARGET_NR_getresgid:
5488 gid_t rgid, egid, sgid;
5489 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5490 if (!is_error(ret)) {
5491 if (put_user_u16(high2lowgid(rgid), arg1)
5492 || put_user_u16(high2lowgid(egid), arg2)
5493 || put_user_u16(high2lowgid(sgid), arg3))
5499 case TARGET_NR_chown:
5500 if (!(p = lock_user_string(arg1)))
5502 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5503 unlock_user(p, arg1, 0);
5505 case TARGET_NR_setuid:
5506 ret = get_errno(setuid(low2highuid(arg1)));
5508 case TARGET_NR_setgid:
5509 ret = get_errno(setgid(low2highgid(arg1)));
5511 case TARGET_NR_setfsuid:
5512 ret = get_errno(setfsuid(arg1));
5514 case TARGET_NR_setfsgid:
5515 ret = get_errno(setfsgid(arg1));
5517 #endif /* USE_UID16 */
5519 #ifdef TARGET_NR_lchown32
5520 case TARGET_NR_lchown32:
5521 if (!(p = lock_user_string(arg1)))
5523 ret = get_errno(lchown(p, arg2, arg3));
5524 unlock_user(p, arg1, 0);
5527 #ifdef TARGET_NR_getuid32
5528 case TARGET_NR_getuid32:
5529 ret = get_errno(getuid());
5532 #ifdef TARGET_NR_getgid32
5533 case TARGET_NR_getgid32:
5534 ret = get_errno(getgid());
5537 #ifdef TARGET_NR_geteuid32
5538 case TARGET_NR_geteuid32:
5539 ret = get_errno(geteuid());
5542 #ifdef TARGET_NR_getegid32
5543 case TARGET_NR_getegid32:
5544 ret = get_errno(getegid());
5547 #ifdef TARGET_NR_setreuid32
5548 case TARGET_NR_setreuid32:
5549 ret = get_errno(setreuid(arg1, arg2));
5552 #ifdef TARGET_NR_setregid32
5553 case TARGET_NR_setregid32:
5554 ret = get_errno(setregid(arg1, arg2));
5557 #ifdef TARGET_NR_getgroups32
5558 case TARGET_NR_getgroups32:
5560 int gidsetsize = arg1;
5561 uint32_t *target_grouplist;
5565 grouplist = alloca(gidsetsize * sizeof(gid_t));
5566 ret = get_errno(getgroups(gidsetsize, grouplist));
5567 if (gidsetsize == 0)
5569 if (!is_error(ret)) {
5570 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
5571 if (!target_grouplist) {
5572 ret = -TARGET_EFAULT;
5575 for(i = 0;i < ret; i++)
5576 target_grouplist[i] = tswap32(grouplist[i]);
5577 unlock_user(target_grouplist, arg2, gidsetsize * 4);
5582 #ifdef TARGET_NR_setgroups32
5583 case TARGET_NR_setgroups32:
5585 int gidsetsize = arg1;
5586 uint32_t *target_grouplist;
5590 grouplist = alloca(gidsetsize * sizeof(gid_t));
5591 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
5592 if (!target_grouplist) {
5593 ret = -TARGET_EFAULT;
5596 for(i = 0;i < gidsetsize; i++)
5597 grouplist[i] = tswap32(target_grouplist[i]);
5598 unlock_user(target_grouplist, arg2, 0);
5599 ret = get_errno(setgroups(gidsetsize, grouplist));
5603 #ifdef TARGET_NR_fchown32
5604 case TARGET_NR_fchown32:
5605 ret = get_errno(fchown(arg1, arg2, arg3));
5608 #ifdef TARGET_NR_setresuid32
5609 case TARGET_NR_setresuid32:
5610 ret = get_errno(setresuid(arg1, arg2, arg3));
5613 #ifdef TARGET_NR_getresuid32
5614 case TARGET_NR_getresuid32:
5616 uid_t ruid, euid, suid;
5617 ret = get_errno(getresuid(&ruid, &euid, &suid));
5618 if (!is_error(ret)) {
5619 if (put_user_u32(ruid, arg1)
5620 || put_user_u32(euid, arg2)
5621 || put_user_u32(suid, arg3))
5627 #ifdef TARGET_NR_setresgid32
5628 case TARGET_NR_setresgid32:
5629 ret = get_errno(setresgid(arg1, arg2, arg3));
5632 #ifdef TARGET_NR_getresgid32
5633 case TARGET_NR_getresgid32:
5635 gid_t rgid, egid, sgid;
5636 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5637 if (!is_error(ret)) {
5638 if (put_user_u32(rgid, arg1)
5639 || put_user_u32(egid, arg2)
5640 || put_user_u32(sgid, arg3))
5646 #ifdef TARGET_NR_chown32
5647 case TARGET_NR_chown32:
5648 if (!(p = lock_user_string(arg1)))
5650 ret = get_errno(chown(p, arg2, arg3));
5651 unlock_user(p, arg1, 0);
5654 #ifdef TARGET_NR_setuid32
5655 case TARGET_NR_setuid32:
5656 ret = get_errno(setuid(arg1));
5659 #ifdef TARGET_NR_setgid32
5660 case TARGET_NR_setgid32:
5661 ret = get_errno(setgid(arg1));
5664 #ifdef TARGET_NR_setfsuid32
5665 case TARGET_NR_setfsuid32:
5666 ret = get_errno(setfsuid(arg1));
5669 #ifdef TARGET_NR_setfsgid32
5670 case TARGET_NR_setfsgid32:
5671 ret = get_errno(setfsgid(arg1));
5675 case TARGET_NR_pivot_root:
5677 #ifdef TARGET_NR_mincore
5678 case TARGET_NR_mincore:
5681 ret = -TARGET_EFAULT;
5682 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
5684 if (!(p = lock_user_string(arg3)))
5686 ret = get_errno(mincore(a, arg2, p));
5687 unlock_user(p, arg3, ret);
5689 unlock_user(a, arg1, 0);
5693 #ifdef TARGET_NR_arm_fadvise64_64
5694 case TARGET_NR_arm_fadvise64_64:
5697 * arm_fadvise64_64 looks like fadvise64_64 but
5698 * with different argument order
5706 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64)
5707 #ifdef TARGET_NR_fadvise64_64
5708 case TARGET_NR_fadvise64_64:
5710 /* This is a hint, so ignoring and returning success is ok. */
5714 #ifdef TARGET_NR_madvise
5715 case TARGET_NR_madvise:
5716 /* A straight passthrough may not be safe because qemu sometimes
5717 turns private flie-backed mappings into anonymous mappings.
5718 This will break MADV_DONTNEED.
5719 This is a hint, so ignoring and returning success is ok. */
5723 #if TARGET_ABI_BITS == 32
5724 case TARGET_NR_fcntl64:
5728 struct target_flock64 *target_fl;
5730 struct target_eabi_flock64 *target_efl;
5734 case TARGET_F_GETLK64:
5737 case TARGET_F_SETLK64:
5740 case TARGET_F_SETLKW64:
5749 case TARGET_F_GETLK64:
5751 if (((CPUARMState *)cpu_env)->eabi) {
5752 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5754 fl.l_type = tswap16(target_efl->l_type);
5755 fl.l_whence = tswap16(target_efl->l_whence);
5756 fl.l_start = tswap64(target_efl->l_start);
5757 fl.l_len = tswap64(target_efl->l_len);
5758 fl.l_pid = tswapl(target_efl->l_pid);
5759 unlock_user_struct(target_efl, arg3, 0);
5763 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5765 fl.l_type = tswap16(target_fl->l_type);
5766 fl.l_whence = tswap16(target_fl->l_whence);
5767 fl.l_start = tswap64(target_fl->l_start);
5768 fl.l_len = tswap64(target_fl->l_len);
5769 fl.l_pid = tswapl(target_fl->l_pid);
5770 unlock_user_struct(target_fl, arg3, 0);
5772 ret = get_errno(fcntl(arg1, cmd, &fl));
5775 if (((CPUARMState *)cpu_env)->eabi) {
5776 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5778 target_efl->l_type = tswap16(fl.l_type);
5779 target_efl->l_whence = tswap16(fl.l_whence);
5780 target_efl->l_start = tswap64(fl.l_start);
5781 target_efl->l_len = tswap64(fl.l_len);
5782 target_efl->l_pid = tswapl(fl.l_pid);
5783 unlock_user_struct(target_efl, arg3, 1);
5787 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5789 target_fl->l_type = tswap16(fl.l_type);
5790 target_fl->l_whence = tswap16(fl.l_whence);
5791 target_fl->l_start = tswap64(fl.l_start);
5792 target_fl->l_len = tswap64(fl.l_len);
5793 target_fl->l_pid = tswapl(fl.l_pid);
5794 unlock_user_struct(target_fl, arg3, 1);
5799 case TARGET_F_SETLK64:
5800 case TARGET_F_SETLKW64:
5802 if (((CPUARMState *)cpu_env)->eabi) {
5803 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5805 fl.l_type = tswap16(target_efl->l_type);
5806 fl.l_whence = tswap16(target_efl->l_whence);
5807 fl.l_start = tswap64(target_efl->l_start);
5808 fl.l_len = tswap64(target_efl->l_len);
5809 fl.l_pid = tswapl(target_efl->l_pid);
5810 unlock_user_struct(target_efl, arg3, 0);
5814 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5816 fl.l_type = tswap16(target_fl->l_type);
5817 fl.l_whence = tswap16(target_fl->l_whence);
5818 fl.l_start = tswap64(target_fl->l_start);
5819 fl.l_len = tswap64(target_fl->l_len);
5820 fl.l_pid = tswapl(target_fl->l_pid);
5821 unlock_user_struct(target_fl, arg3, 0);
5823 ret = get_errno(fcntl(arg1, cmd, &fl));
5826 ret = do_fcntl(arg1, cmd, arg3);
5832 #ifdef TARGET_NR_cacheflush
5833 case TARGET_NR_cacheflush:
5834 /* self-modifying code is handled automatically, so nothing needed */
5838 #ifdef TARGET_NR_security
5839 case TARGET_NR_security:
5842 #ifdef TARGET_NR_getpagesize
5843 case TARGET_NR_getpagesize:
5844 ret = TARGET_PAGE_SIZE;
5847 case TARGET_NR_gettid:
5848 ret = get_errno(gettid());
5850 #ifdef TARGET_NR_readahead
5851 case TARGET_NR_readahead:
5852 #if TARGET_ABI_BITS == 32
5854 if (((CPUARMState *)cpu_env)->eabi)
5861 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
5863 ret = get_errno(readahead(arg1, arg2, arg3));
5867 #ifdef TARGET_NR_setxattr
5868 case TARGET_NR_setxattr:
5869 case TARGET_NR_lsetxattr:
5870 case TARGET_NR_fsetxattr:
5871 case TARGET_NR_getxattr:
5872 case TARGET_NR_lgetxattr:
5873 case TARGET_NR_fgetxattr:
5874 case TARGET_NR_listxattr:
5875 case TARGET_NR_llistxattr:
5876 case TARGET_NR_flistxattr:
5877 case TARGET_NR_removexattr:
5878 case TARGET_NR_lremovexattr:
5879 case TARGET_NR_fremovexattr:
5880 goto unimplemented_nowarn;
5882 #ifdef TARGET_NR_set_thread_area
5883 case TARGET_NR_set_thread_area:
5884 #if defined(TARGET_MIPS)
5885 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5888 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
5889 ret = do_set_thread_area(cpu_env, arg1);
5892 goto unimplemented_nowarn;
5895 #ifdef TARGET_NR_get_thread_area
5896 case TARGET_NR_get_thread_area:
5897 #if defined(TARGET_I386) && defined(TARGET_ABI32)
5898 ret = do_get_thread_area(cpu_env, arg1);
5900 goto unimplemented_nowarn;
5903 #ifdef TARGET_NR_getdomainname
5904 case TARGET_NR_getdomainname:
5905 goto unimplemented_nowarn;
5908 #ifdef TARGET_NR_clock_gettime
5909 case TARGET_NR_clock_gettime:
5912 ret = get_errno(clock_gettime(arg1, &ts));
5913 if (!is_error(ret)) {
5914 host_to_target_timespec(arg2, &ts);
5919 #ifdef TARGET_NR_clock_getres
5920 case TARGET_NR_clock_getres:
5923 ret = get_errno(clock_getres(arg1, &ts));
5924 if (!is_error(ret)) {
5925 host_to_target_timespec(arg2, &ts);
5930 #ifdef TARGET_NR_clock_nanosleep
5931 case TARGET_NR_clock_nanosleep:
5934 target_to_host_timespec(&ts, arg3);
5935 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
5937 host_to_target_timespec(arg4, &ts);
5942 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5943 case TARGET_NR_set_tid_address:
5944 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5948 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
5949 case TARGET_NR_tkill:
5950 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
5954 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
5955 case TARGET_NR_tgkill:
5956 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
5957 target_to_host_signal(arg3)));
5961 #ifdef TARGET_NR_set_robust_list
5962 case TARGET_NR_set_robust_list:
5963 goto unimplemented_nowarn;
5966 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5967 case TARGET_NR_utimensat:
5969 struct timespec ts[2];
5970 target_to_host_timespec(ts, arg3);
5971 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5973 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5975 if (!(p = lock_user_string(arg2))) {
5976 ret = -TARGET_EFAULT;
5979 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5980 unlock_user(p, arg2, 0);
5985 #if defined(USE_NPTL)
5986 case TARGET_NR_futex:
5987 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
5990 #ifdef TARGET_NR_inotify_init
5991 case TARGET_NR_inotify_init:
5992 ret = get_errno(sys_inotify_init());
5995 #ifdef TARGET_NR_inotify_add_watch
5996 case TARGET_NR_inotify_add_watch:
5997 p = lock_user_string(arg2);
5998 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
5999 unlock_user(p, arg2, 0);
6002 #ifdef TARGET_NR_inotify_rm_watch
6003 case TARGET_NR_inotify_rm_watch:
6004 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
6010 gemu_log("qemu: Unsupported syscall: %d\n", num);
6011 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
6012 unimplemented_nowarn:
6014 ret = -TARGET_ENOSYS;
6019 gemu_log(" = %ld\n", ret);
6022 print_syscall_ret(num, ret);
6025 ret = -TARGET_EFAULT;