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., 51 Franklin Street - Fifth Floor, Boston,
21 #define _ATFILE_SOURCE
34 #include <sys/types.h>
40 #include <sys/mount.h>
41 #include <sys/prctl.h>
42 #include <sys/resource.h>
47 #include <sys/socket.h>
51 #include <sys/times.h>
54 #include <sys/statfs.h>
56 #include <sys/sysinfo.h>
57 #include <sys/utsname.h>
58 //#include <sys/user.h>
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
61 #include <qemu-common.h>
66 #define termios host_termios
67 #define winsize host_winsize
68 #define termio host_termio
69 #define sgttyb host_sgttyb /* same as target */
70 #define tchars host_tchars /* same as target */
71 #define ltchars host_ltchars /* same as target */
73 #include <linux/termios.h>
74 #include <linux/unistd.h>
75 #include <linux/utsname.h>
76 #include <linux/cdrom.h>
77 #include <linux/hdreg.h>
78 #include <linux/soundcard.h>
80 #include <linux/mtio.h>
82 #include "linux_loop.h"
85 #include "qemu-common.h"
88 #include <linux/futex.h>
89 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
90 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
92 /* XXX: Hardcode the above values. */
93 #define CLONE_NPTL_FLAGS2 0
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_uname __NR_uname
159 #define __NR_sys_faccessat __NR_faccessat
160 #define __NR_sys_fchmodat __NR_fchmodat
161 #define __NR_sys_fchownat __NR_fchownat
162 #define __NR_sys_fstatat64 __NR_fstatat64
163 #define __NR_sys_futimesat __NR_futimesat
164 #define __NR_sys_getcwd1 __NR_getcwd
165 #define __NR_sys_getdents __NR_getdents
166 #define __NR_sys_getdents64 __NR_getdents64
167 #define __NR_sys_getpriority __NR_getpriority
168 #define __NR_sys_linkat __NR_linkat
169 #define __NR_sys_mkdirat __NR_mkdirat
170 #define __NR_sys_mknodat __NR_mknodat
171 #define __NR_sys_newfstatat __NR_newfstatat
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 #if TARGET_ABI_BITS == 32
201 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
203 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
204 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
206 _syscall2(int, sys_getpriority
, int, which
, int, who
);
207 #if defined(TARGET_NR__llseek) && !defined (__x86_64__)
208 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
209 loff_t
*, res
, uint
, wh
);
211 _syscall3(int,sys_rt_sigqueueinfo
,int,pid
,int,sig
,siginfo_t
*,uinfo
)
212 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
213 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
214 _syscall3(int,sys_tgkill
,int,tgid
,int,pid
,int,sig
)
216 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
217 _syscall2(int,sys_tkill
,int,tid
,int,sig
)
219 #ifdef __NR_exit_group
220 _syscall1(int,exit_group
,int,error_code
)
222 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
223 _syscall1(int,set_tid_address
,int *,tidptr
)
225 #if defined(USE_NPTL)
226 #if defined(TARGET_NR_futex) && defined(__NR_futex)
227 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
228 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
232 static bitmask_transtbl fcntl_flags_tbl
[] = {
233 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
234 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
235 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
236 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
237 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
238 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
239 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
240 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
241 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
242 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
243 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
244 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
245 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
246 #if defined(O_DIRECT)
247 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
252 #define COPY_UTSNAME_FIELD(dest, src) \
254 /* __NEW_UTS_LEN doesn't include terminating null */ \
255 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
256 (dest)[__NEW_UTS_LEN] = '\0'; \
259 static int sys_uname(struct new_utsname
*buf
)
261 struct utsname uts_buf
;
263 if (uname(&uts_buf
) < 0)
267 * Just in case these have some differences, we
268 * translate utsname to new_utsname (which is the
269 * struct linux kernel uses).
272 bzero(buf
, sizeof (*buf
));
273 COPY_UTSNAME_FIELD(buf
->sysname
, uts_buf
.sysname
);
274 COPY_UTSNAME_FIELD(buf
->nodename
, uts_buf
.nodename
);
275 COPY_UTSNAME_FIELD(buf
->release
, uts_buf
.release
);
276 COPY_UTSNAME_FIELD(buf
->version
, uts_buf
.version
);
277 COPY_UTSNAME_FIELD(buf
->machine
, uts_buf
.machine
);
279 COPY_UTSNAME_FIELD(buf
->domainname
, uts_buf
.domainname
);
283 #undef COPY_UTSNAME_FIELD
286 static int sys_getcwd1(char *buf
, size_t size
)
288 if (getcwd(buf
, size
) == NULL
) {
289 /* getcwd() sets errno */
292 return strlen(buf
)+1;
297 * Host system seems to have atfile syscall stubs available. We
298 * now enable them one by one as specified by target syscall_nr.h.
301 #ifdef TARGET_NR_faccessat
302 static int sys_faccessat(int dirfd
, const char *pathname
, int mode
)
304 return (faccessat(dirfd
, pathname
, mode
, 0));
307 #ifdef TARGET_NR_fchmodat
308 static int sys_fchmodat(int dirfd
, const char *pathname
, mode_t mode
)
310 return (fchmodat(dirfd
, pathname
, mode
, 0));
313 #if defined(TARGET_NR_fchownat) && defined(USE_UID16)
314 static int sys_fchownat(int dirfd
, const char *pathname
, uid_t owner
,
315 gid_t group
, int flags
)
317 return (fchownat(dirfd
, pathname
, owner
, group
, flags
));
320 #ifdef __NR_fstatat64
321 static int sys_fstatat64(int dirfd
, const char *pathname
, struct stat
*buf
,
324 return (fstatat(dirfd
, pathname
, buf
, flags
));
327 #ifdef __NR_newfstatat
328 static int sys_newfstatat(int dirfd
, const char *pathname
, struct stat
*buf
,
331 return (fstatat(dirfd
, pathname
, buf
, flags
));
334 #ifdef TARGET_NR_futimesat
335 static int sys_futimesat(int dirfd
, const char *pathname
,
336 const struct timeval times
[2])
338 return (futimesat(dirfd
, pathname
, times
));
341 #ifdef TARGET_NR_linkat
342 static int sys_linkat(int olddirfd
, const char *oldpath
,
343 int newdirfd
, const char *newpath
, int flags
)
345 return (linkat(olddirfd
, oldpath
, newdirfd
, newpath
, flags
));
348 #ifdef TARGET_NR_mkdirat
349 static int sys_mkdirat(int dirfd
, const char *pathname
, mode_t mode
)
351 return (mkdirat(dirfd
, pathname
, mode
));
354 #ifdef TARGET_NR_mknodat
355 static int sys_mknodat(int dirfd
, const char *pathname
, mode_t mode
,
358 return (mknodat(dirfd
, pathname
, mode
, dev
));
361 #ifdef TARGET_NR_openat
362 static int sys_openat(int dirfd
, const char *pathname
, int flags
, ...)
365 * open(2) has extra parameter 'mode' when called with
368 if ((flags
& O_CREAT
) != 0) {
373 * Get the 'mode' parameter and translate it to
377 mode
= va_arg(ap
, mode_t
);
378 mode
= target_to_host_bitmask(mode
, fcntl_flags_tbl
);
381 return (openat(dirfd
, pathname
, flags
, mode
));
383 return (openat(dirfd
, pathname
, flags
));
386 #ifdef TARGET_NR_readlinkat
387 static int sys_readlinkat(int dirfd
, const char *pathname
, char *buf
, size_t bufsiz
)
389 return (readlinkat(dirfd
, pathname
, buf
, bufsiz
));
392 #ifdef TARGET_NR_renameat
393 static int sys_renameat(int olddirfd
, const char *oldpath
,
394 int newdirfd
, const char *newpath
)
396 return (renameat(olddirfd
, oldpath
, newdirfd
, newpath
));
399 #ifdef TARGET_NR_symlinkat
400 static int sys_symlinkat(const char *oldpath
, int newdirfd
, const char *newpath
)
402 return (symlinkat(oldpath
, newdirfd
, newpath
));
405 #ifdef TARGET_NR_unlinkat
406 static int sys_unlinkat(int dirfd
, const char *pathname
, int flags
)
408 return (unlinkat(dirfd
, pathname
, flags
));
411 #else /* !CONFIG_ATFILE */
414 * Try direct syscalls instead
416 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
417 _syscall3(int,sys_faccessat
,int,dirfd
,const char *,pathname
,int,mode
)
419 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
420 _syscall3(int,sys_fchmodat
,int,dirfd
,const char *,pathname
, mode_t
,mode
)
422 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
423 _syscall5(int,sys_fchownat
,int,dirfd
,const char *,pathname
,
424 uid_t
,owner
,gid_t
,group
,int,flags
)
426 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
427 defined(__NR_fstatat64)
428 _syscall4(int,sys_fstatat64
,int,dirfd
,const char *,pathname
,
429 struct stat
*,buf
,int,flags
)
431 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
432 _syscall3(int,sys_futimesat
,int,dirfd
,const char *,pathname
,
433 const struct timeval
*,times
)
435 #if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
436 defined(__NR_newfstatat)
437 _syscall4(int,sys_newfstatat
,int,dirfd
,const char *,pathname
,
438 struct stat
*,buf
,int,flags
)
440 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
441 _syscall5(int,sys_linkat
,int,olddirfd
,const char *,oldpath
,
442 int,newdirfd
,const char *,newpath
,int,flags
)
444 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
445 _syscall3(int,sys_mkdirat
,int,dirfd
,const char *,pathname
,mode_t
,mode
)
447 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
448 _syscall4(int,sys_mknodat
,int,dirfd
,const char *,pathname
,
449 mode_t
,mode
,dev_t
,dev
)
451 #if defined(TARGET_NR_openat) && defined(__NR_openat)
452 _syscall4(int,sys_openat
,int,dirfd
,const char *,pathname
,int,flags
,mode_t
,mode
)
454 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
455 _syscall4(int,sys_readlinkat
,int,dirfd
,const char *,pathname
,
456 char *,buf
,size_t,bufsize
)
458 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
459 _syscall4(int,sys_renameat
,int,olddirfd
,const char *,oldpath
,
460 int,newdirfd
,const char *,newpath
)
462 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
463 _syscall3(int,sys_symlinkat
,const char *,oldpath
,
464 int,newdirfd
,const char *,newpath
)
466 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
467 _syscall3(int,sys_unlinkat
,int,dirfd
,const char *,pathname
,int,flags
)
470 #endif /* CONFIG_ATFILE */
472 #ifdef CONFIG_UTIMENSAT
473 static int sys_utimensat(int dirfd
, const char *pathname
,
474 const struct timespec times
[2], int flags
)
476 if (pathname
== NULL
)
477 return futimens(dirfd
, times
);
479 return utimensat(dirfd
, pathname
, times
, flags
);
482 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
483 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
484 const struct timespec
*,tsp
,int,flags
)
486 #endif /* CONFIG_UTIMENSAT */
488 #ifdef CONFIG_INOTIFY
489 #include <sys/inotify.h>
491 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
492 static int sys_inotify_init(void)
494 return (inotify_init());
497 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
498 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
500 return (inotify_add_watch(fd
, pathname
, mask
));
503 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
504 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
506 return (inotify_rm_watch(fd
, wd
));
510 /* Userspace can usually survive runtime without inotify */
511 #undef TARGET_NR_inotify_init
512 #undef TARGET_NR_inotify_add_watch
513 #undef TARGET_NR_inotify_rm_watch
514 #endif /* CONFIG_INOTIFY */
517 extern int personality(int);
518 extern int flock(int, int);
519 extern int setfsuid(int);
520 extern int setfsgid(int);
521 extern int setgroups(int, gid_t
*);
523 #define ERRNO_TABLE_SIZE 1200
525 /* target_to_host_errno_table[] is initialized from
526 * host_to_target_errno_table[] in syscall_init(). */
527 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
531 * This list is the union of errno values overridden in asm-<arch>/errno.h
532 * minus the errnos that are not actually generic to all archs.
534 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
535 [EIDRM
] = TARGET_EIDRM
,
536 [ECHRNG
] = TARGET_ECHRNG
,
537 [EL2NSYNC
] = TARGET_EL2NSYNC
,
538 [EL3HLT
] = TARGET_EL3HLT
,
539 [EL3RST
] = TARGET_EL3RST
,
540 [ELNRNG
] = TARGET_ELNRNG
,
541 [EUNATCH
] = TARGET_EUNATCH
,
542 [ENOCSI
] = TARGET_ENOCSI
,
543 [EL2HLT
] = TARGET_EL2HLT
,
544 [EDEADLK
] = TARGET_EDEADLK
,
545 [ENOLCK
] = TARGET_ENOLCK
,
546 [EBADE
] = TARGET_EBADE
,
547 [EBADR
] = TARGET_EBADR
,
548 [EXFULL
] = TARGET_EXFULL
,
549 [ENOANO
] = TARGET_ENOANO
,
550 [EBADRQC
] = TARGET_EBADRQC
,
551 [EBADSLT
] = TARGET_EBADSLT
,
552 [EBFONT
] = TARGET_EBFONT
,
553 [ENOSTR
] = TARGET_ENOSTR
,
554 [ENODATA
] = TARGET_ENODATA
,
555 [ETIME
] = TARGET_ETIME
,
556 [ENOSR
] = TARGET_ENOSR
,
557 [ENONET
] = TARGET_ENONET
,
558 [ENOPKG
] = TARGET_ENOPKG
,
559 [EREMOTE
] = TARGET_EREMOTE
,
560 [ENOLINK
] = TARGET_ENOLINK
,
561 [EADV
] = TARGET_EADV
,
562 [ESRMNT
] = TARGET_ESRMNT
,
563 [ECOMM
] = TARGET_ECOMM
,
564 [EPROTO
] = TARGET_EPROTO
,
565 [EDOTDOT
] = TARGET_EDOTDOT
,
566 [EMULTIHOP
] = TARGET_EMULTIHOP
,
567 [EBADMSG
] = TARGET_EBADMSG
,
568 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
569 [EOVERFLOW
] = TARGET_EOVERFLOW
,
570 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
571 [EBADFD
] = TARGET_EBADFD
,
572 [EREMCHG
] = TARGET_EREMCHG
,
573 [ELIBACC
] = TARGET_ELIBACC
,
574 [ELIBBAD
] = TARGET_ELIBBAD
,
575 [ELIBSCN
] = TARGET_ELIBSCN
,
576 [ELIBMAX
] = TARGET_ELIBMAX
,
577 [ELIBEXEC
] = TARGET_ELIBEXEC
,
578 [EILSEQ
] = TARGET_EILSEQ
,
579 [ENOSYS
] = TARGET_ENOSYS
,
580 [ELOOP
] = TARGET_ELOOP
,
581 [ERESTART
] = TARGET_ERESTART
,
582 [ESTRPIPE
] = TARGET_ESTRPIPE
,
583 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
584 [EUSERS
] = TARGET_EUSERS
,
585 [ENOTSOCK
] = TARGET_ENOTSOCK
,
586 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
587 [EMSGSIZE
] = TARGET_EMSGSIZE
,
588 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
589 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
590 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
591 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
592 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
593 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
594 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
595 [EADDRINUSE
] = TARGET_EADDRINUSE
,
596 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
597 [ENETDOWN
] = TARGET_ENETDOWN
,
598 [ENETUNREACH
] = TARGET_ENETUNREACH
,
599 [ENETRESET
] = TARGET_ENETRESET
,
600 [ECONNABORTED
] = TARGET_ECONNABORTED
,
601 [ECONNRESET
] = TARGET_ECONNRESET
,
602 [ENOBUFS
] = TARGET_ENOBUFS
,
603 [EISCONN
] = TARGET_EISCONN
,
604 [ENOTCONN
] = TARGET_ENOTCONN
,
605 [EUCLEAN
] = TARGET_EUCLEAN
,
606 [ENOTNAM
] = TARGET_ENOTNAM
,
607 [ENAVAIL
] = TARGET_ENAVAIL
,
608 [EISNAM
] = TARGET_EISNAM
,
609 [EREMOTEIO
] = TARGET_EREMOTEIO
,
610 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
611 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
612 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
613 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
614 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
615 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
616 [EALREADY
] = TARGET_EALREADY
,
617 [EINPROGRESS
] = TARGET_EINPROGRESS
,
618 [ESTALE
] = TARGET_ESTALE
,
619 [ECANCELED
] = TARGET_ECANCELED
,
620 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
621 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
623 [ENOKEY
] = TARGET_ENOKEY
,
626 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
629 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
632 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
635 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
637 #ifdef ENOTRECOVERABLE
638 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
642 static inline int host_to_target_errno(int err
)
644 if(host_to_target_errno_table
[err
])
645 return host_to_target_errno_table
[err
];
649 static inline int target_to_host_errno(int err
)
651 if (target_to_host_errno_table
[err
])
652 return target_to_host_errno_table
[err
];
656 static inline abi_long
get_errno(abi_long ret
)
659 return -host_to_target_errno(errno
);
664 static inline int is_error(abi_long ret
)
666 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
669 char *target_strerror(int err
)
671 return strerror(target_to_host_errno(err
));
674 static abi_ulong target_brk
;
675 static abi_ulong target_original_brk
;
677 void target_set_brk(abi_ulong new_brk
)
679 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
682 /* do_brk() must return target values and target errnos. */
683 abi_long
do_brk(abi_ulong new_brk
)
686 abi_long mapped_addr
;
691 if (new_brk
< target_original_brk
)
694 brk_page
= HOST_PAGE_ALIGN(target_brk
);
696 /* If the new brk is less than this, set it and we're done... */
697 if (new_brk
< brk_page
) {
698 target_brk
= new_brk
;
702 /* We need to allocate more memory after the brk... */
703 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
+ 1);
704 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
705 PROT_READ
|PROT_WRITE
,
706 MAP_ANON
|MAP_FIXED
|MAP_PRIVATE
, 0, 0));
708 if (!is_error(mapped_addr
))
709 target_brk
= new_brk
;
714 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
715 abi_ulong target_fds_addr
,
719 abi_ulong b
, *target_fds
;
721 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
722 if (!(target_fds
= lock_user(VERIFY_READ
,
724 sizeof(abi_ulong
) * nw
,
726 return -TARGET_EFAULT
;
730 for (i
= 0; i
< nw
; i
++) {
731 /* grab the abi_ulong */
732 __get_user(b
, &target_fds
[i
]);
733 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
734 /* check the bit inside the abi_ulong */
741 unlock_user(target_fds
, target_fds_addr
, 0);
746 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
752 abi_ulong
*target_fds
;
754 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
755 if (!(target_fds
= lock_user(VERIFY_WRITE
,
757 sizeof(abi_ulong
) * nw
,
759 return -TARGET_EFAULT
;
762 for (i
= 0; i
< nw
; i
++) {
764 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
765 v
|= ((FD_ISSET(k
, fds
) != 0) << j
);
768 __put_user(v
, &target_fds
[i
]);
771 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
776 #if defined(__alpha__)
782 static inline abi_long
host_to_target_clock_t(long ticks
)
784 #if HOST_HZ == TARGET_HZ
787 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
791 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
792 const struct rusage
*rusage
)
794 struct target_rusage
*target_rusage
;
796 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
797 return -TARGET_EFAULT
;
798 target_rusage
->ru_utime
.tv_sec
= tswapl(rusage
->ru_utime
.tv_sec
);
799 target_rusage
->ru_utime
.tv_usec
= tswapl(rusage
->ru_utime
.tv_usec
);
800 target_rusage
->ru_stime
.tv_sec
= tswapl(rusage
->ru_stime
.tv_sec
);
801 target_rusage
->ru_stime
.tv_usec
= tswapl(rusage
->ru_stime
.tv_usec
);
802 target_rusage
->ru_maxrss
= tswapl(rusage
->ru_maxrss
);
803 target_rusage
->ru_ixrss
= tswapl(rusage
->ru_ixrss
);
804 target_rusage
->ru_idrss
= tswapl(rusage
->ru_idrss
);
805 target_rusage
->ru_isrss
= tswapl(rusage
->ru_isrss
);
806 target_rusage
->ru_minflt
= tswapl(rusage
->ru_minflt
);
807 target_rusage
->ru_majflt
= tswapl(rusage
->ru_majflt
);
808 target_rusage
->ru_nswap
= tswapl(rusage
->ru_nswap
);
809 target_rusage
->ru_inblock
= tswapl(rusage
->ru_inblock
);
810 target_rusage
->ru_oublock
= tswapl(rusage
->ru_oublock
);
811 target_rusage
->ru_msgsnd
= tswapl(rusage
->ru_msgsnd
);
812 target_rusage
->ru_msgrcv
= tswapl(rusage
->ru_msgrcv
);
813 target_rusage
->ru_nsignals
= tswapl(rusage
->ru_nsignals
);
814 target_rusage
->ru_nvcsw
= tswapl(rusage
->ru_nvcsw
);
815 target_rusage
->ru_nivcsw
= tswapl(rusage
->ru_nivcsw
);
816 unlock_user_struct(target_rusage
, target_addr
, 1);
821 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
822 abi_ulong target_tv_addr
)
824 struct target_timeval
*target_tv
;
826 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1))
827 return -TARGET_EFAULT
;
829 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
830 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
832 unlock_user_struct(target_tv
, target_tv_addr
, 0);
837 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
838 const struct timeval
*tv
)
840 struct target_timeval
*target_tv
;
842 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0))
843 return -TARGET_EFAULT
;
845 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
846 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
848 unlock_user_struct(target_tv
, target_tv_addr
, 1);
853 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
854 abi_ulong target_mq_attr_addr
)
856 struct target_mq_attr
*target_mq_attr
;
858 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
859 target_mq_attr_addr
, 1))
860 return -TARGET_EFAULT
;
862 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
863 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
864 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
865 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
867 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
872 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
873 const struct mq_attr
*attr
)
875 struct target_mq_attr
*target_mq_attr
;
877 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
878 target_mq_attr_addr
, 0))
879 return -TARGET_EFAULT
;
881 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
882 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
883 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
884 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
886 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
891 /* do_select() must return target values and target errnos. */
892 static abi_long
do_select(int n
,
893 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
894 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
896 fd_set rfds
, wfds
, efds
;
897 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
898 struct timeval tv
, *tv_ptr
;
902 if (copy_from_user_fdset(&rfds
, rfd_addr
, n
))
903 return -TARGET_EFAULT
;
909 if (copy_from_user_fdset(&wfds
, wfd_addr
, n
))
910 return -TARGET_EFAULT
;
916 if (copy_from_user_fdset(&efds
, efd_addr
, n
))
917 return -TARGET_EFAULT
;
923 if (target_tv_addr
) {
924 if (copy_from_user_timeval(&tv
, target_tv_addr
))
925 return -TARGET_EFAULT
;
931 ret
= get_errno(select(n
, rfds_ptr
, wfds_ptr
, efds_ptr
, tv_ptr
));
933 if (!is_error(ret
)) {
934 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
935 return -TARGET_EFAULT
;
936 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
937 return -TARGET_EFAULT
;
938 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
939 return -TARGET_EFAULT
;
941 if (target_tv_addr
&& copy_to_user_timeval(target_tv_addr
, &tv
))
942 return -TARGET_EFAULT
;
948 static abi_long
do_pipe2(int host_pipe
[], int flags
)
951 return pipe2(host_pipe
, flags
);
957 static abi_long
do_pipe(void *cpu_env
, int pipedes
, int flags
)
961 ret
= flags
? do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
964 return get_errno(ret
);
965 #if defined(TARGET_MIPS)
966 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
968 #elif defined(TARGET_SH4)
969 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
972 if (put_user_s32(host_pipe
[0], pipedes
)
973 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
974 return -TARGET_EFAULT
;
976 return get_errno(ret
);
979 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
980 abi_ulong target_addr
,
983 struct target_ip_mreqn
*target_smreqn
;
985 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
987 return -TARGET_EFAULT
;
988 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
989 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
990 if (len
== sizeof(struct target_ip_mreqn
))
991 mreqn
->imr_ifindex
= tswapl(target_smreqn
->imr_ifindex
);
992 unlock_user(target_smreqn
, target_addr
, 0);
997 static inline abi_long
target_to_host_sockaddr(struct sockaddr
*addr
,
998 abi_ulong target_addr
,
1001 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1002 sa_family_t sa_family
;
1003 struct target_sockaddr
*target_saddr
;
1005 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1007 return -TARGET_EFAULT
;
1009 sa_family
= tswap16(target_saddr
->sa_family
);
1011 /* Oops. The caller might send a incomplete sun_path; sun_path
1012 * must be terminated by \0 (see the manual page), but
1013 * unfortunately it is quite common to specify sockaddr_un
1014 * length as "strlen(x->sun_path)" while it should be
1015 * "strlen(...) + 1". We'll fix that here if needed.
1016 * Linux kernel has a similar feature.
1019 if (sa_family
== AF_UNIX
) {
1020 if (len
< unix_maxlen
&& len
> 0) {
1021 char *cp
= (char*)target_saddr
;
1023 if ( cp
[len
-1] && !cp
[len
] )
1026 if (len
> unix_maxlen
)
1030 memcpy(addr
, target_saddr
, len
);
1031 addr
->sa_family
= sa_family
;
1032 unlock_user(target_saddr
, target_addr
, 0);
1037 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1038 struct sockaddr
*addr
,
1041 struct target_sockaddr
*target_saddr
;
1043 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1045 return -TARGET_EFAULT
;
1046 memcpy(target_saddr
, addr
, len
);
1047 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1048 unlock_user(target_saddr
, target_addr
, len
);
1053 /* ??? Should this also swap msgh->name? */
1054 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1055 struct target_msghdr
*target_msgh
)
1057 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1058 abi_long msg_controllen
;
1059 abi_ulong target_cmsg_addr
;
1060 struct target_cmsghdr
*target_cmsg
;
1061 socklen_t space
= 0;
1063 msg_controllen
= tswapl(target_msgh
->msg_controllen
);
1064 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1066 target_cmsg_addr
= tswapl(target_msgh
->msg_control
);
1067 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1069 return -TARGET_EFAULT
;
1071 while (cmsg
&& target_cmsg
) {
1072 void *data
= CMSG_DATA(cmsg
);
1073 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1075 int len
= tswapl(target_cmsg
->cmsg_len
)
1076 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr
));
1078 space
+= CMSG_SPACE(len
);
1079 if (space
> msgh
->msg_controllen
) {
1080 space
-= CMSG_SPACE(len
);
1081 gemu_log("Host cmsg overflow\n");
1085 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1086 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1087 cmsg
->cmsg_len
= CMSG_LEN(len
);
1089 if (cmsg
->cmsg_level
!= TARGET_SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
1090 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1091 memcpy(data
, target_data
, len
);
1093 int *fd
= (int *)data
;
1094 int *target_fd
= (int *)target_data
;
1095 int i
, numfds
= len
/ sizeof(int);
1097 for (i
= 0; i
< numfds
; i
++)
1098 fd
[i
] = tswap32(target_fd
[i
]);
1101 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1102 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
);
1104 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1106 msgh
->msg_controllen
= space
;
1110 /* ??? Should this also swap msgh->name? */
1111 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1112 struct msghdr
*msgh
)
1114 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1115 abi_long msg_controllen
;
1116 abi_ulong target_cmsg_addr
;
1117 struct target_cmsghdr
*target_cmsg
;
1118 socklen_t space
= 0;
1120 msg_controllen
= tswapl(target_msgh
->msg_controllen
);
1121 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1123 target_cmsg_addr
= tswapl(target_msgh
->msg_control
);
1124 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1126 return -TARGET_EFAULT
;
1128 while (cmsg
&& target_cmsg
) {
1129 void *data
= CMSG_DATA(cmsg
);
1130 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1132 int len
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof (struct cmsghdr
));
1134 space
+= TARGET_CMSG_SPACE(len
);
1135 if (space
> msg_controllen
) {
1136 space
-= TARGET_CMSG_SPACE(len
);
1137 gemu_log("Target cmsg overflow\n");
1141 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1142 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1143 target_cmsg
->cmsg_len
= tswapl(TARGET_CMSG_LEN(len
));
1145 if (cmsg
->cmsg_level
!= TARGET_SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
1146 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1147 memcpy(target_data
, data
, len
);
1149 int *fd
= (int *)data
;
1150 int *target_fd
= (int *)target_data
;
1151 int i
, numfds
= len
/ sizeof(int);
1153 for (i
= 0; i
< numfds
; i
++)
1154 target_fd
[i
] = tswap32(fd
[i
]);
1157 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1158 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
);
1160 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1162 target_msgh
->msg_controllen
= tswapl(space
);
1166 /* do_setsockopt() Must return target values and target errnos. */
1167 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
1168 abi_ulong optval_addr
, socklen_t optlen
)
1172 struct ip_mreqn
*ip_mreq
;
1173 struct ip_mreq_source
*ip_mreq_source
;
1177 /* TCP options all take an 'int' value. */
1178 if (optlen
< sizeof(uint32_t))
1179 return -TARGET_EINVAL
;
1181 if (get_user_u32(val
, optval_addr
))
1182 return -TARGET_EFAULT
;
1183 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1190 case IP_ROUTER_ALERT
:
1194 case IP_MTU_DISCOVER
:
1200 case IP_MULTICAST_TTL
:
1201 case IP_MULTICAST_LOOP
:
1203 if (optlen
>= sizeof(uint32_t)) {
1204 if (get_user_u32(val
, optval_addr
))
1205 return -TARGET_EFAULT
;
1206 } else if (optlen
>= 1) {
1207 if (get_user_u8(val
, optval_addr
))
1208 return -TARGET_EFAULT
;
1210 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1212 case IP_ADD_MEMBERSHIP
:
1213 case IP_DROP_MEMBERSHIP
:
1214 if (optlen
< sizeof (struct target_ip_mreq
) ||
1215 optlen
> sizeof (struct target_ip_mreqn
))
1216 return -TARGET_EINVAL
;
1218 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
1219 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
1220 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
1223 case IP_BLOCK_SOURCE
:
1224 case IP_UNBLOCK_SOURCE
:
1225 case IP_ADD_SOURCE_MEMBERSHIP
:
1226 case IP_DROP_SOURCE_MEMBERSHIP
:
1227 if (optlen
!= sizeof (struct target_ip_mreq_source
))
1228 return -TARGET_EINVAL
;
1230 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1231 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
1232 unlock_user (ip_mreq_source
, optval_addr
, 0);
1239 case TARGET_SOL_SOCKET
:
1241 /* Options with 'int' argument. */
1242 case TARGET_SO_DEBUG
:
1245 case TARGET_SO_REUSEADDR
:
1246 optname
= SO_REUSEADDR
;
1248 case TARGET_SO_TYPE
:
1251 case TARGET_SO_ERROR
:
1254 case TARGET_SO_DONTROUTE
:
1255 optname
= SO_DONTROUTE
;
1257 case TARGET_SO_BROADCAST
:
1258 optname
= SO_BROADCAST
;
1260 case TARGET_SO_SNDBUF
:
1261 optname
= SO_SNDBUF
;
1263 case TARGET_SO_RCVBUF
:
1264 optname
= SO_RCVBUF
;
1266 case TARGET_SO_KEEPALIVE
:
1267 optname
= SO_KEEPALIVE
;
1269 case TARGET_SO_OOBINLINE
:
1270 optname
= SO_OOBINLINE
;
1272 case TARGET_SO_NO_CHECK
:
1273 optname
= SO_NO_CHECK
;
1275 case TARGET_SO_PRIORITY
:
1276 optname
= SO_PRIORITY
;
1279 case TARGET_SO_BSDCOMPAT
:
1280 optname
= SO_BSDCOMPAT
;
1283 case TARGET_SO_PASSCRED
:
1284 optname
= SO_PASSCRED
;
1286 case TARGET_SO_TIMESTAMP
:
1287 optname
= SO_TIMESTAMP
;
1289 case TARGET_SO_RCVLOWAT
:
1290 optname
= SO_RCVLOWAT
;
1292 case TARGET_SO_RCVTIMEO
:
1293 optname
= SO_RCVTIMEO
;
1295 case TARGET_SO_SNDTIMEO
:
1296 optname
= SO_SNDTIMEO
;
1302 if (optlen
< sizeof(uint32_t))
1303 return -TARGET_EINVAL
;
1305 if (get_user_u32(val
, optval_addr
))
1306 return -TARGET_EFAULT
;
1307 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
1311 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level
, optname
);
1312 ret
= -TARGET_ENOPROTOOPT
;
1317 /* do_getsockopt() Must return target values and target errnos. */
1318 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
1319 abi_ulong optval_addr
, abi_ulong optlen
)
1326 case TARGET_SOL_SOCKET
:
1329 case TARGET_SO_LINGER
:
1330 case TARGET_SO_RCVTIMEO
:
1331 case TARGET_SO_SNDTIMEO
:
1332 case TARGET_SO_PEERCRED
:
1333 case TARGET_SO_PEERNAME
:
1334 /* These don't just return a single integer */
1341 /* TCP options all take an 'int' value. */
1343 if (get_user_u32(len
, optlen
))
1344 return -TARGET_EFAULT
;
1346 return -TARGET_EINVAL
;
1348 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1354 if (put_user_u32(val
, optval_addr
))
1355 return -TARGET_EFAULT
;
1357 if (put_user_u8(val
, optval_addr
))
1358 return -TARGET_EFAULT
;
1360 if (put_user_u32(len
, optlen
))
1361 return -TARGET_EFAULT
;
1368 case IP_ROUTER_ALERT
:
1372 case IP_MTU_DISCOVER
:
1378 case IP_MULTICAST_TTL
:
1379 case IP_MULTICAST_LOOP
:
1380 if (get_user_u32(len
, optlen
))
1381 return -TARGET_EFAULT
;
1383 return -TARGET_EINVAL
;
1385 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1388 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
1390 if (put_user_u32(len
, optlen
)
1391 || put_user_u8(val
, optval_addr
))
1392 return -TARGET_EFAULT
;
1394 if (len
> sizeof(int))
1396 if (put_user_u32(len
, optlen
)
1397 || put_user_u32(val
, optval_addr
))
1398 return -TARGET_EFAULT
;
1402 ret
= -TARGET_ENOPROTOOPT
;
1408 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1410 ret
= -TARGET_EOPNOTSUPP
;
1417 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1418 * other lock functions have a return code of 0 for failure.
1420 static abi_long
lock_iovec(int type
, struct iovec
*vec
, abi_ulong target_addr
,
1421 int count
, int copy
)
1423 struct target_iovec
*target_vec
;
1427 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
1429 return -TARGET_EFAULT
;
1430 for(i
= 0;i
< count
; i
++) {
1431 base
= tswapl(target_vec
[i
].iov_base
);
1432 vec
[i
].iov_len
= tswapl(target_vec
[i
].iov_len
);
1433 if (vec
[i
].iov_len
!= 0) {
1434 vec
[i
].iov_base
= lock_user(type
, base
, vec
[i
].iov_len
, copy
);
1435 /* Don't check lock_user return value. We must call writev even
1436 if a element has invalid base address. */
1438 /* zero length pointer is ignored */
1439 vec
[i
].iov_base
= NULL
;
1442 unlock_user (target_vec
, target_addr
, 0);
1446 static abi_long
unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
1447 int count
, int copy
)
1449 struct target_iovec
*target_vec
;
1453 target_vec
= lock_user(VERIFY_READ
, target_addr
, count
* sizeof(struct target_iovec
), 1);
1455 return -TARGET_EFAULT
;
1456 for(i
= 0;i
< count
; i
++) {
1457 if (target_vec
[i
].iov_base
) {
1458 base
= tswapl(target_vec
[i
].iov_base
);
1459 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
1462 unlock_user (target_vec
, target_addr
, 0);
1467 /* do_socket() Must return target values and target errnos. */
1468 static abi_long
do_socket(int domain
, int type
, int protocol
)
1470 #if defined(TARGET_MIPS)
1472 case TARGET_SOCK_DGRAM
:
1475 case TARGET_SOCK_STREAM
:
1478 case TARGET_SOCK_RAW
:
1481 case TARGET_SOCK_RDM
:
1484 case TARGET_SOCK_SEQPACKET
:
1485 type
= SOCK_SEQPACKET
;
1487 case TARGET_SOCK_PACKET
:
1492 if (domain
== PF_NETLINK
)
1493 return -EAFNOSUPPORT
; /* do not NETLINK socket connections possible */
1494 return get_errno(socket(domain
, type
, protocol
));
1497 /* do_bind() Must return target values and target errnos. */
1498 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
1504 return -TARGET_EINVAL
;
1506 addr
= alloca(addrlen
+1);
1508 target_to_host_sockaddr(addr
, target_addr
, addrlen
);
1509 return get_errno(bind(sockfd
, addr
, addrlen
));
1512 /* do_connect() Must return target values and target errnos. */
1513 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
1519 return -TARGET_EINVAL
;
1521 addr
= alloca(addrlen
);
1523 target_to_host_sockaddr(addr
, target_addr
, addrlen
);
1524 return get_errno(connect(sockfd
, addr
, addrlen
));
1527 /* do_sendrecvmsg() Must return target values and target errnos. */
1528 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
1529 int flags
, int send
)
1532 struct target_msghdr
*msgp
;
1536 abi_ulong target_vec
;
1539 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
1543 return -TARGET_EFAULT
;
1544 if (msgp
->msg_name
) {
1545 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
1546 msg
.msg_name
= alloca(msg
.msg_namelen
);
1547 target_to_host_sockaddr(msg
.msg_name
, tswapl(msgp
->msg_name
),
1550 msg
.msg_name
= NULL
;
1551 msg
.msg_namelen
= 0;
1553 msg
.msg_controllen
= 2 * tswapl(msgp
->msg_controllen
);
1554 msg
.msg_control
= alloca(msg
.msg_controllen
);
1555 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
1557 count
= tswapl(msgp
->msg_iovlen
);
1558 vec
= alloca(count
* sizeof(struct iovec
));
1559 target_vec
= tswapl(msgp
->msg_iov
);
1560 lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
, vec
, target_vec
, count
, send
);
1561 msg
.msg_iovlen
= count
;
1565 ret
= target_to_host_cmsg(&msg
, msgp
);
1567 ret
= get_errno(sendmsg(fd
, &msg
, flags
));
1569 ret
= get_errno(recvmsg(fd
, &msg
, flags
));
1570 if (!is_error(ret
)) {
1572 ret
= host_to_target_cmsg(msgp
, &msg
);
1577 unlock_iovec(vec
, target_vec
, count
, !send
);
1578 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
1582 /* do_accept() Must return target values and target errnos. */
1583 static abi_long
do_accept(int fd
, abi_ulong target_addr
,
1584 abi_ulong target_addrlen_addr
)
1590 if (get_user_u32(addrlen
, target_addrlen_addr
))
1591 return -TARGET_EFAULT
;
1594 return -TARGET_EINVAL
;
1596 addr
= alloca(addrlen
);
1598 ret
= get_errno(accept(fd
, addr
, &addrlen
));
1599 if (!is_error(ret
)) {
1600 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
1601 if (put_user_u32(addrlen
, target_addrlen_addr
))
1602 ret
= -TARGET_EFAULT
;
1607 /* do_getpeername() Must return target values and target errnos. */
1608 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
1609 abi_ulong target_addrlen_addr
)
1615 if (get_user_u32(addrlen
, target_addrlen_addr
))
1616 return -TARGET_EFAULT
;
1619 return -TARGET_EINVAL
;
1621 addr
= alloca(addrlen
);
1623 ret
= get_errno(getpeername(fd
, addr
, &addrlen
));
1624 if (!is_error(ret
)) {
1625 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
1626 if (put_user_u32(addrlen
, target_addrlen_addr
))
1627 ret
= -TARGET_EFAULT
;
1632 /* do_getsockname() Must return target values and target errnos. */
1633 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
1634 abi_ulong target_addrlen_addr
)
1640 if (target_addr
== 0)
1641 return get_errno(accept(fd
, NULL
, NULL
));
1643 if (get_user_u32(addrlen
, target_addrlen_addr
))
1644 return -TARGET_EFAULT
;
1647 return -TARGET_EINVAL
;
1649 addr
= alloca(addrlen
);
1651 ret
= get_errno(getsockname(fd
, addr
, &addrlen
));
1652 if (!is_error(ret
)) {
1653 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
1654 if (put_user_u32(addrlen
, target_addrlen_addr
))
1655 ret
= -TARGET_EFAULT
;
1660 /* do_socketpair() Must return target values and target errnos. */
1661 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
1662 abi_ulong target_tab_addr
)
1667 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
1668 if (!is_error(ret
)) {
1669 if (put_user_s32(tab
[0], target_tab_addr
)
1670 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
1671 ret
= -TARGET_EFAULT
;
1676 /* do_sendto() Must return target values and target errnos. */
1677 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
1678 abi_ulong target_addr
, socklen_t addrlen
)
1685 return -TARGET_EINVAL
;
1687 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
1689 return -TARGET_EFAULT
;
1691 addr
= alloca(addrlen
);
1692 target_to_host_sockaddr(addr
, target_addr
, addrlen
);
1693 ret
= get_errno(sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
1695 ret
= get_errno(send(fd
, host_msg
, len
, flags
));
1697 unlock_user(host_msg
, msg
, 0);
1701 /* do_recvfrom() Must return target values and target errnos. */
1702 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
1703 abi_ulong target_addr
,
1704 abi_ulong target_addrlen
)
1711 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
1713 return -TARGET_EFAULT
;
1715 if (get_user_u32(addrlen
, target_addrlen
)) {
1716 ret
= -TARGET_EFAULT
;
1720 ret
= -TARGET_EINVAL
;
1723 addr
= alloca(addrlen
);
1724 ret
= get_errno(recvfrom(fd
, host_msg
, len
, flags
, addr
, &addrlen
));
1726 addr
= NULL
; /* To keep compiler quiet. */
1727 ret
= get_errno(recv(fd
, host_msg
, len
, flags
));
1729 if (!is_error(ret
)) {
1731 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
1732 if (put_user_u32(addrlen
, target_addrlen
)) {
1733 ret
= -TARGET_EFAULT
;
1737 unlock_user(host_msg
, msg
, len
);
1740 unlock_user(host_msg
, msg
, 0);
1745 #ifdef TARGET_NR_socketcall
1746 /* do_socketcall() Must return target values and target errnos. */
1747 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
1750 const int n
= sizeof(abi_ulong
);
1755 int domain
, type
, protocol
;
1757 if (get_user_s32(domain
, vptr
)
1758 || get_user_s32(type
, vptr
+ n
)
1759 || get_user_s32(protocol
, vptr
+ 2 * n
))
1760 return -TARGET_EFAULT
;
1762 ret
= do_socket(domain
, type
, protocol
);
1768 abi_ulong target_addr
;
1771 if (get_user_s32(sockfd
, vptr
)
1772 || get_user_ual(target_addr
, vptr
+ n
)
1773 || get_user_u32(addrlen
, vptr
+ 2 * n
))
1774 return -TARGET_EFAULT
;
1776 ret
= do_bind(sockfd
, target_addr
, addrlen
);
1779 case SOCKOP_connect
:
1782 abi_ulong target_addr
;
1785 if (get_user_s32(sockfd
, vptr
)
1786 || get_user_ual(target_addr
, vptr
+ n
)
1787 || get_user_u32(addrlen
, vptr
+ 2 * n
))
1788 return -TARGET_EFAULT
;
1790 ret
= do_connect(sockfd
, target_addr
, addrlen
);
1795 int sockfd
, backlog
;
1797 if (get_user_s32(sockfd
, vptr
)
1798 || get_user_s32(backlog
, vptr
+ n
))
1799 return -TARGET_EFAULT
;
1801 ret
= get_errno(listen(sockfd
, backlog
));
1807 abi_ulong target_addr
, target_addrlen
;
1809 if (get_user_s32(sockfd
, vptr
)
1810 || get_user_ual(target_addr
, vptr
+ n
)
1811 || get_user_u32(target_addrlen
, vptr
+ 2 * n
))
1812 return -TARGET_EFAULT
;
1814 ret
= do_accept(sockfd
, target_addr
, target_addrlen
);
1817 case SOCKOP_getsockname
:
1820 abi_ulong target_addr
, target_addrlen
;
1822 if (get_user_s32(sockfd
, vptr
)
1823 || get_user_ual(target_addr
, vptr
+ n
)
1824 || get_user_u32(target_addrlen
, vptr
+ 2 * n
))
1825 return -TARGET_EFAULT
;
1827 ret
= do_getsockname(sockfd
, target_addr
, target_addrlen
);
1830 case SOCKOP_getpeername
:
1833 abi_ulong target_addr
, target_addrlen
;
1835 if (get_user_s32(sockfd
, vptr
)
1836 || get_user_ual(target_addr
, vptr
+ n
)
1837 || get_user_u32(target_addrlen
, vptr
+ 2 * n
))
1838 return -TARGET_EFAULT
;
1840 ret
= do_getpeername(sockfd
, target_addr
, target_addrlen
);
1843 case SOCKOP_socketpair
:
1845 int domain
, type
, protocol
;
1848 if (get_user_s32(domain
, vptr
)
1849 || get_user_s32(type
, vptr
+ n
)
1850 || get_user_s32(protocol
, vptr
+ 2 * n
)
1851 || get_user_ual(tab
, vptr
+ 3 * n
))
1852 return -TARGET_EFAULT
;
1854 ret
= do_socketpair(domain
, type
, protocol
, tab
);
1864 if (get_user_s32(sockfd
, vptr
)
1865 || get_user_ual(msg
, vptr
+ n
)
1866 || get_user_ual(len
, vptr
+ 2 * n
)
1867 || get_user_s32(flags
, vptr
+ 3 * n
))
1868 return -TARGET_EFAULT
;
1870 ret
= do_sendto(sockfd
, msg
, len
, flags
, 0, 0);
1880 if (get_user_s32(sockfd
, vptr
)
1881 || get_user_ual(msg
, vptr
+ n
)
1882 || get_user_ual(len
, vptr
+ 2 * n
)
1883 || get_user_s32(flags
, vptr
+ 3 * n
))
1884 return -TARGET_EFAULT
;
1886 ret
= do_recvfrom(sockfd
, msg
, len
, flags
, 0, 0);
1898 if (get_user_s32(sockfd
, vptr
)
1899 || get_user_ual(msg
, vptr
+ n
)
1900 || get_user_ual(len
, vptr
+ 2 * n
)
1901 || get_user_s32(flags
, vptr
+ 3 * n
)
1902 || get_user_ual(addr
, vptr
+ 4 * n
)
1903 || get_user_u32(addrlen
, vptr
+ 5 * n
))
1904 return -TARGET_EFAULT
;
1906 ret
= do_sendto(sockfd
, msg
, len
, flags
, addr
, addrlen
);
1909 case SOCKOP_recvfrom
:
1918 if (get_user_s32(sockfd
, vptr
)
1919 || get_user_ual(msg
, vptr
+ n
)
1920 || get_user_ual(len
, vptr
+ 2 * n
)
1921 || get_user_s32(flags
, vptr
+ 3 * n
)
1922 || get_user_ual(addr
, vptr
+ 4 * n
)
1923 || get_user_u32(addrlen
, vptr
+ 5 * n
))
1924 return -TARGET_EFAULT
;
1926 ret
= do_recvfrom(sockfd
, msg
, len
, flags
, addr
, addrlen
);
1929 case SOCKOP_shutdown
:
1933 if (get_user_s32(sockfd
, vptr
)
1934 || get_user_s32(how
, vptr
+ n
))
1935 return -TARGET_EFAULT
;
1937 ret
= get_errno(shutdown(sockfd
, how
));
1940 case SOCKOP_sendmsg
:
1941 case SOCKOP_recvmsg
:
1944 abi_ulong target_msg
;
1947 if (get_user_s32(fd
, vptr
)
1948 || get_user_ual(target_msg
, vptr
+ n
)
1949 || get_user_s32(flags
, vptr
+ 2 * n
))
1950 return -TARGET_EFAULT
;
1952 ret
= do_sendrecvmsg(fd
, target_msg
, flags
,
1953 (num
== SOCKOP_sendmsg
));
1956 case SOCKOP_setsockopt
:
1964 if (get_user_s32(sockfd
, vptr
)
1965 || get_user_s32(level
, vptr
+ n
)
1966 || get_user_s32(optname
, vptr
+ 2 * n
)
1967 || get_user_ual(optval
, vptr
+ 3 * n
)
1968 || get_user_u32(optlen
, vptr
+ 4 * n
))
1969 return -TARGET_EFAULT
;
1971 ret
= do_setsockopt(sockfd
, level
, optname
, optval
, optlen
);
1974 case SOCKOP_getsockopt
:
1982 if (get_user_s32(sockfd
, vptr
)
1983 || get_user_s32(level
, vptr
+ n
)
1984 || get_user_s32(optname
, vptr
+ 2 * n
)
1985 || get_user_ual(optval
, vptr
+ 3 * n
)
1986 || get_user_u32(optlen
, vptr
+ 4 * n
))
1987 return -TARGET_EFAULT
;
1989 ret
= do_getsockopt(sockfd
, level
, optname
, optval
, optlen
);
1993 gemu_log("Unsupported socketcall: %d\n", num
);
1994 ret
= -TARGET_ENOSYS
;
2001 #define N_SHM_REGIONS 32
2003 static struct shm_region
{
2006 } shm_regions
[N_SHM_REGIONS
];
2008 struct target_ipc_perm
2015 unsigned short int mode
;
2016 unsigned short int __pad1
;
2017 unsigned short int __seq
;
2018 unsigned short int __pad2
;
2019 abi_ulong __unused1
;
2020 abi_ulong __unused2
;
2023 struct target_semid_ds
2025 struct target_ipc_perm sem_perm
;
2026 abi_ulong sem_otime
;
2027 abi_ulong __unused1
;
2028 abi_ulong sem_ctime
;
2029 abi_ulong __unused2
;
2030 abi_ulong sem_nsems
;
2031 abi_ulong __unused3
;
2032 abi_ulong __unused4
;
2035 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
2036 abi_ulong target_addr
)
2038 struct target_ipc_perm
*target_ip
;
2039 struct target_semid_ds
*target_sd
;
2041 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2042 return -TARGET_EFAULT
;
2043 target_ip
=&(target_sd
->sem_perm
);
2044 host_ip
->__key
= tswapl(target_ip
->__key
);
2045 host_ip
->uid
= tswapl(target_ip
->uid
);
2046 host_ip
->gid
= tswapl(target_ip
->gid
);
2047 host_ip
->cuid
= tswapl(target_ip
->cuid
);
2048 host_ip
->cgid
= tswapl(target_ip
->cgid
);
2049 host_ip
->mode
= tswapl(target_ip
->mode
);
2050 unlock_user_struct(target_sd
, target_addr
, 0);
2054 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
2055 struct ipc_perm
*host_ip
)
2057 struct target_ipc_perm
*target_ip
;
2058 struct target_semid_ds
*target_sd
;
2060 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2061 return -TARGET_EFAULT
;
2062 target_ip
= &(target_sd
->sem_perm
);
2063 target_ip
->__key
= tswapl(host_ip
->__key
);
2064 target_ip
->uid
= tswapl(host_ip
->uid
);
2065 target_ip
->gid
= tswapl(host_ip
->gid
);
2066 target_ip
->cuid
= tswapl(host_ip
->cuid
);
2067 target_ip
->cgid
= tswapl(host_ip
->cgid
);
2068 target_ip
->mode
= tswapl(host_ip
->mode
);
2069 unlock_user_struct(target_sd
, target_addr
, 1);
2073 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
2074 abi_ulong target_addr
)
2076 struct target_semid_ds
*target_sd
;
2078 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2079 return -TARGET_EFAULT
;
2080 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
2081 return -TARGET_EFAULT
;
2082 host_sd
->sem_nsems
= tswapl(target_sd
->sem_nsems
);
2083 host_sd
->sem_otime
= tswapl(target_sd
->sem_otime
);
2084 host_sd
->sem_ctime
= tswapl(target_sd
->sem_ctime
);
2085 unlock_user_struct(target_sd
, target_addr
, 0);
2089 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
2090 struct semid_ds
*host_sd
)
2092 struct target_semid_ds
*target_sd
;
2094 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2095 return -TARGET_EFAULT
;
2096 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
2097 return -TARGET_EFAULT
;;
2098 target_sd
->sem_nsems
= tswapl(host_sd
->sem_nsems
);
2099 target_sd
->sem_otime
= tswapl(host_sd
->sem_otime
);
2100 target_sd
->sem_ctime
= tswapl(host_sd
->sem_ctime
);
2101 unlock_user_struct(target_sd
, target_addr
, 1);
2105 struct target_seminfo
{
2118 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
2119 struct seminfo
*host_seminfo
)
2121 struct target_seminfo
*target_seminfo
;
2122 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
2123 return -TARGET_EFAULT
;
2124 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
2125 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
2126 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
2127 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
2128 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
2129 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
2130 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
2131 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
2132 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
2133 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
2134 unlock_user_struct(target_seminfo
, target_addr
, 1);
2140 struct semid_ds
*buf
;
2141 unsigned short *array
;
2142 struct seminfo
*__buf
;
2145 union target_semun
{
2152 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
2153 abi_ulong target_addr
)
2156 unsigned short *array
;
2158 struct semid_ds semid_ds
;
2161 semun
.buf
= &semid_ds
;
2163 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2165 return get_errno(ret
);
2167 nsems
= semid_ds
.sem_nsems
;
2169 *host_array
= malloc(nsems
*sizeof(unsigned short));
2170 array
= lock_user(VERIFY_READ
, target_addr
,
2171 nsems
*sizeof(unsigned short), 1);
2173 return -TARGET_EFAULT
;
2175 for(i
=0; i
<nsems
; i
++) {
2176 __get_user((*host_array
)[i
], &array
[i
]);
2178 unlock_user(array
, target_addr
, 0);
2183 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
2184 unsigned short **host_array
)
2187 unsigned short *array
;
2189 struct semid_ds semid_ds
;
2192 semun
.buf
= &semid_ds
;
2194 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2196 return get_errno(ret
);
2198 nsems
= semid_ds
.sem_nsems
;
2200 array
= lock_user(VERIFY_WRITE
, target_addr
,
2201 nsems
*sizeof(unsigned short), 0);
2203 return -TARGET_EFAULT
;
2205 for(i
=0; i
<nsems
; i
++) {
2206 __put_user((*host_array
)[i
], &array
[i
]);
2209 unlock_user(array
, target_addr
, 1);
2214 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
2215 union target_semun target_su
)
2218 struct semid_ds dsarg
;
2219 unsigned short *array
;
2220 struct seminfo seminfo
;
2221 abi_long ret
= -TARGET_EINVAL
;
2228 arg
.val
= tswapl(target_su
.val
);
2229 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2230 target_su
.val
= tswapl(arg
.val
);
2234 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
2238 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2239 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
2246 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
2250 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2251 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
2257 arg
.__buf
= &seminfo
;
2258 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2259 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
2267 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
2274 struct target_sembuf
{
2275 unsigned short sem_num
;
2280 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
2281 abi_ulong target_addr
,
2284 struct target_sembuf
*target_sembuf
;
2287 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
2288 nsops
*sizeof(struct target_sembuf
), 1);
2290 return -TARGET_EFAULT
;
2292 for(i
=0; i
<nsops
; i
++) {
2293 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
2294 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
2295 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
2298 unlock_user(target_sembuf
, target_addr
, 0);
2303 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
2305 struct sembuf sops
[nsops
];
2307 if (target_to_host_sembuf(sops
, ptr
, nsops
))
2308 return -TARGET_EFAULT
;
2310 return semop(semid
, sops
, nsops
);
2313 struct target_msqid_ds
2315 struct target_ipc_perm msg_perm
;
2316 abi_ulong msg_stime
;
2317 #if TARGET_ABI_BITS == 32
2318 abi_ulong __unused1
;
2320 abi_ulong msg_rtime
;
2321 #if TARGET_ABI_BITS == 32
2322 abi_ulong __unused2
;
2324 abi_ulong msg_ctime
;
2325 #if TARGET_ABI_BITS == 32
2326 abi_ulong __unused3
;
2328 abi_ulong __msg_cbytes
;
2330 abi_ulong msg_qbytes
;
2331 abi_ulong msg_lspid
;
2332 abi_ulong msg_lrpid
;
2333 abi_ulong __unused4
;
2334 abi_ulong __unused5
;
2337 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
2338 abi_ulong target_addr
)
2340 struct target_msqid_ds
*target_md
;
2342 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
2343 return -TARGET_EFAULT
;
2344 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
2345 return -TARGET_EFAULT
;
2346 host_md
->msg_stime
= tswapl(target_md
->msg_stime
);
2347 host_md
->msg_rtime
= tswapl(target_md
->msg_rtime
);
2348 host_md
->msg_ctime
= tswapl(target_md
->msg_ctime
);
2349 host_md
->__msg_cbytes
= tswapl(target_md
->__msg_cbytes
);
2350 host_md
->msg_qnum
= tswapl(target_md
->msg_qnum
);
2351 host_md
->msg_qbytes
= tswapl(target_md
->msg_qbytes
);
2352 host_md
->msg_lspid
= tswapl(target_md
->msg_lspid
);
2353 host_md
->msg_lrpid
= tswapl(target_md
->msg_lrpid
);
2354 unlock_user_struct(target_md
, target_addr
, 0);
2358 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
2359 struct msqid_ds
*host_md
)
2361 struct target_msqid_ds
*target_md
;
2363 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
2364 return -TARGET_EFAULT
;
2365 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
2366 return -TARGET_EFAULT
;
2367 target_md
->msg_stime
= tswapl(host_md
->msg_stime
);
2368 target_md
->msg_rtime
= tswapl(host_md
->msg_rtime
);
2369 target_md
->msg_ctime
= tswapl(host_md
->msg_ctime
);
2370 target_md
->__msg_cbytes
= tswapl(host_md
->__msg_cbytes
);
2371 target_md
->msg_qnum
= tswapl(host_md
->msg_qnum
);
2372 target_md
->msg_qbytes
= tswapl(host_md
->msg_qbytes
);
2373 target_md
->msg_lspid
= tswapl(host_md
->msg_lspid
);
2374 target_md
->msg_lrpid
= tswapl(host_md
->msg_lrpid
);
2375 unlock_user_struct(target_md
, target_addr
, 1);
2379 struct target_msginfo
{
2387 unsigned short int msgseg
;
2390 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
2391 struct msginfo
*host_msginfo
)
2393 struct target_msginfo
*target_msginfo
;
2394 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
2395 return -TARGET_EFAULT
;
2396 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
2397 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
2398 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
2399 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
2400 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
2401 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
2402 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
2403 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
2404 unlock_user_struct(target_msginfo
, target_addr
, 1);
2408 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
2410 struct msqid_ds dsarg
;
2411 struct msginfo msginfo
;
2412 abi_long ret
= -TARGET_EINVAL
;
2420 if (target_to_host_msqid_ds(&dsarg
,ptr
))
2421 return -TARGET_EFAULT
;
2422 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
2423 if (host_to_target_msqid_ds(ptr
,&dsarg
))
2424 return -TARGET_EFAULT
;
2427 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
2431 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
2432 if (host_to_target_msginfo(ptr
, &msginfo
))
2433 return -TARGET_EFAULT
;
2440 struct target_msgbuf
{
2445 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
2446 unsigned int msgsz
, int msgflg
)
2448 struct target_msgbuf
*target_mb
;
2449 struct msgbuf
*host_mb
;
2452 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
2453 return -TARGET_EFAULT
;
2454 host_mb
= malloc(msgsz
+sizeof(long));
2455 host_mb
->mtype
= (abi_long
) tswapl(target_mb
->mtype
);
2456 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
2457 ret
= get_errno(msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
2459 unlock_user_struct(target_mb
, msgp
, 0);
2464 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
2465 unsigned int msgsz
, abi_long msgtyp
,
2468 struct target_msgbuf
*target_mb
;
2470 struct msgbuf
*host_mb
;
2473 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
2474 return -TARGET_EFAULT
;
2476 host_mb
= malloc(msgsz
+sizeof(long));
2477 ret
= get_errno(msgrcv(msqid
, host_mb
, msgsz
, tswapl(msgtyp
), msgflg
));
2480 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
2481 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
2482 if (!target_mtext
) {
2483 ret
= -TARGET_EFAULT
;
2486 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
2487 unlock_user(target_mtext
, target_mtext_addr
, ret
);
2490 target_mb
->mtype
= tswapl(host_mb
->mtype
);
2495 unlock_user_struct(target_mb
, msgp
, 1);
2499 struct target_shmid_ds
2501 struct target_ipc_perm shm_perm
;
2502 abi_ulong shm_segsz
;
2503 abi_ulong shm_atime
;
2504 #if TARGET_ABI_BITS == 32
2505 abi_ulong __unused1
;
2507 abi_ulong shm_dtime
;
2508 #if TARGET_ABI_BITS == 32
2509 abi_ulong __unused2
;
2511 abi_ulong shm_ctime
;
2512 #if TARGET_ABI_BITS == 32
2513 abi_ulong __unused3
;
2517 abi_ulong shm_nattch
;
2518 unsigned long int __unused4
;
2519 unsigned long int __unused5
;
2522 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
2523 abi_ulong target_addr
)
2525 struct target_shmid_ds
*target_sd
;
2527 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2528 return -TARGET_EFAULT
;
2529 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
2530 return -TARGET_EFAULT
;
2531 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
2532 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
2533 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
2534 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
2535 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
2536 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
2537 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
2538 unlock_user_struct(target_sd
, target_addr
, 0);
2542 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
2543 struct shmid_ds
*host_sd
)
2545 struct target_shmid_ds
*target_sd
;
2547 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2548 return -TARGET_EFAULT
;
2549 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
2550 return -TARGET_EFAULT
;
2551 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
2552 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
2553 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
2554 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
2555 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
2556 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
2557 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
2558 unlock_user_struct(target_sd
, target_addr
, 1);
2562 struct target_shminfo
{
2570 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
2571 struct shminfo
*host_shminfo
)
2573 struct target_shminfo
*target_shminfo
;
2574 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
2575 return -TARGET_EFAULT
;
2576 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
2577 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
2578 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
2579 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
2580 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
2581 unlock_user_struct(target_shminfo
, target_addr
, 1);
2585 struct target_shm_info
{
2590 abi_ulong swap_attempts
;
2591 abi_ulong swap_successes
;
2594 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
2595 struct shm_info
*host_shm_info
)
2597 struct target_shm_info
*target_shm_info
;
2598 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
2599 return -TARGET_EFAULT
;
2600 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
2601 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
2602 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
2603 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
2604 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
2605 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
2606 unlock_user_struct(target_shm_info
, target_addr
, 1);
2610 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
2612 struct shmid_ds dsarg
;
2613 struct shminfo shminfo
;
2614 struct shm_info shm_info
;
2615 abi_long ret
= -TARGET_EINVAL
;
2623 if (target_to_host_shmid_ds(&dsarg
, buf
))
2624 return -TARGET_EFAULT
;
2625 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
2626 if (host_to_target_shmid_ds(buf
, &dsarg
))
2627 return -TARGET_EFAULT
;
2630 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
2631 if (host_to_target_shminfo(buf
, &shminfo
))
2632 return -TARGET_EFAULT
;
2635 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
2636 if (host_to_target_shm_info(buf
, &shm_info
))
2637 return -TARGET_EFAULT
;
2642 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
2649 static inline abi_ulong
do_shmat(int shmid
, abi_ulong shmaddr
, int shmflg
)
2653 struct shmid_ds shm_info
;
2656 /* find out the length of the shared memory segment */
2657 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
2658 if (is_error(ret
)) {
2659 /* can't get length, bail out */
2666 host_raddr
= shmat(shmid
, (void *)g2h(shmaddr
), shmflg
);
2668 abi_ulong mmap_start
;
2670 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
);
2672 if (mmap_start
== -1) {
2674 host_raddr
= (void *)-1;
2676 host_raddr
= shmat(shmid
, g2h(mmap_start
), shmflg
| SHM_REMAP
);
2679 if (host_raddr
== (void *)-1) {
2681 return get_errno((long)host_raddr
);
2683 raddr
=h2g((unsigned long)host_raddr
);
2685 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
2686 PAGE_VALID
| PAGE_READ
|
2687 ((shmflg
& SHM_RDONLY
)? 0 : PAGE_WRITE
));
2689 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
2690 if (shm_regions
[i
].start
== 0) {
2691 shm_regions
[i
].start
= raddr
;
2692 shm_regions
[i
].size
= shm_info
.shm_segsz
;
2702 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
2706 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
2707 if (shm_regions
[i
].start
== shmaddr
) {
2708 shm_regions
[i
].start
= 0;
2709 page_set_flags(shmaddr
, shm_regions
[i
].size
, 0);
2714 return get_errno(shmdt(g2h(shmaddr
)));
2717 #ifdef TARGET_NR_ipc
2718 /* ??? This only works with linear mappings. */
2719 /* do_ipc() must return target values and target errnos. */
2720 static abi_long
do_ipc(unsigned int call
, int first
,
2721 int second
, int third
,
2722 abi_long ptr
, abi_long fifth
)
2727 version
= call
>> 16;
2732 ret
= do_semop(first
, ptr
, second
);
2736 ret
= get_errno(semget(first
, second
, third
));
2740 ret
= do_semctl(first
, second
, third
, (union target_semun
)(abi_ulong
) ptr
);
2744 ret
= get_errno(msgget(first
, second
));
2748 ret
= do_msgsnd(first
, ptr
, second
, third
);
2752 ret
= do_msgctl(first
, second
, ptr
);
2759 struct target_ipc_kludge
{
2764 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
2765 ret
= -TARGET_EFAULT
;
2769 ret
= do_msgrcv(first
, tmp
->msgp
, second
, tmp
->msgtyp
, third
);
2771 unlock_user_struct(tmp
, ptr
, 0);
2775 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
2784 raddr
= do_shmat(first
, ptr
, second
);
2785 if (is_error(raddr
))
2786 return get_errno(raddr
);
2787 if (put_user_ual(raddr
, third
))
2788 return -TARGET_EFAULT
;
2792 ret
= -TARGET_EINVAL
;
2797 ret
= do_shmdt(ptr
);
2801 /* IPC_* flag values are the same on all linux platforms */
2802 ret
= get_errno(shmget(first
, second
, third
));
2805 /* IPC_* and SHM_* command values are the same on all linux platforms */
2807 ret
= do_shmctl(first
, second
, third
);
2810 gemu_log("Unsupported ipc call: %d (version %d)\n", call
, version
);
2811 ret
= -TARGET_ENOSYS
;
2818 /* kernel structure types definitions */
2821 #define STRUCT(name, ...) STRUCT_ ## name,
2822 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2824 #include "syscall_types.h"
2827 #undef STRUCT_SPECIAL
2829 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
2830 #define STRUCT_SPECIAL(name)
2831 #include "syscall_types.h"
2833 #undef STRUCT_SPECIAL
2835 typedef struct IOCTLEntry
{
2836 unsigned int target_cmd
;
2837 unsigned int host_cmd
;
2840 const argtype arg_type
[5];
2843 #define IOC_R 0x0001
2844 #define IOC_W 0x0002
2845 #define IOC_RW (IOC_R | IOC_W)
2847 #define MAX_STRUCT_SIZE 4096
2849 static IOCTLEntry ioctl_entries
[] = {
2850 #define IOCTL(cmd, access, ...) \
2851 { TARGET_ ## cmd, cmd, #cmd, access, { __VA_ARGS__ } },
2856 /* ??? Implement proper locking for ioctls. */
2857 /* do_ioctl() Must return target values and target errnos. */
2858 static abi_long
do_ioctl(int fd
, abi_long cmd
, abi_long arg
)
2860 const IOCTLEntry
*ie
;
2861 const argtype
*arg_type
;
2863 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
2869 if (ie
->target_cmd
== 0) {
2870 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
2871 return -TARGET_ENOSYS
;
2873 if (ie
->target_cmd
== cmd
)
2877 arg_type
= ie
->arg_type
;
2879 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd
, ie
->name
);
2881 switch(arg_type
[0]) {
2884 ret
= get_errno(ioctl(fd
, ie
->host_cmd
));
2889 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, arg
));
2893 target_size
= thunk_type_size(arg_type
, 0);
2894 switch(ie
->access
) {
2896 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
2897 if (!is_error(ret
)) {
2898 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
2900 return -TARGET_EFAULT
;
2901 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
2902 unlock_user(argptr
, arg
, target_size
);
2906 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
2908 return -TARGET_EFAULT
;
2909 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
2910 unlock_user(argptr
, arg
, 0);
2911 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
2915 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
2917 return -TARGET_EFAULT
;
2918 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
2919 unlock_user(argptr
, arg
, 0);
2920 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
2921 if (!is_error(ret
)) {
2922 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
2924 return -TARGET_EFAULT
;
2925 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
2926 unlock_user(argptr
, arg
, target_size
);
2932 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2933 (long)cmd
, arg_type
[0]);
2934 ret
= -TARGET_ENOSYS
;
2940 static const bitmask_transtbl iflag_tbl
[] = {
2941 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
2942 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
2943 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
2944 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
2945 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
2946 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
2947 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
2948 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
2949 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
2950 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
2951 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
2952 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
2953 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
2954 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
2958 static const bitmask_transtbl oflag_tbl
[] = {
2959 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
2960 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
2961 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
2962 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
2963 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
2964 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
2965 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
2966 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
2967 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
2968 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
2969 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
2970 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
2971 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
2972 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
2973 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
2974 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
2975 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
2976 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
2977 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
2978 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
2979 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
2980 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
2981 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
2982 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
2986 static const bitmask_transtbl cflag_tbl
[] = {
2987 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
2988 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
2989 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
2990 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
2991 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
2992 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
2993 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
2994 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
2995 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
2996 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
2997 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
2998 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
2999 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
3000 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
3001 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
3002 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
3003 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
3004 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
3005 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
3006 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
3007 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
3008 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
3009 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
3010 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
3011 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
3012 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
3013 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
3014 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
3015 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
3016 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
3017 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
3021 static const bitmask_transtbl lflag_tbl
[] = {
3022 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
3023 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
3024 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
3025 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
3026 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
3027 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
3028 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
3029 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
3030 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
3031 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
3032 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
3033 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
3034 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
3035 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
3036 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
3040 static void target_to_host_termios (void *dst
, const void *src
)
3042 struct host_termios
*host
= dst
;
3043 const struct target_termios
*target
= src
;
3046 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
3048 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
3050 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
3052 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
3053 host
->c_line
= target
->c_line
;
3055 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
3056 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
3057 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
3058 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
3059 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
3060 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
3061 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
3062 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
3063 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
3064 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
3065 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
3066 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
3067 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
3068 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
3069 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
3070 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
3071 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
3072 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
3075 static void host_to_target_termios (void *dst
, const void *src
)
3077 struct target_termios
*target
= dst
;
3078 const struct host_termios
*host
= src
;
3081 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
3083 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
3085 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
3087 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
3088 target
->c_line
= host
->c_line
;
3090 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
3091 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
3092 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
3093 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
3094 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
3095 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
3096 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
3097 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
3098 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
3099 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
3100 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
3101 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
3102 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
3103 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
3104 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
3105 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
3106 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
3107 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
3110 static const StructEntry struct_termios_def
= {
3111 .convert
= { host_to_target_termios
, target_to_host_termios
},
3112 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
3113 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
3116 static bitmask_transtbl mmap_flags_tbl
[] = {
3117 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
3118 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
3119 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
3120 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
, MAP_ANONYMOUS
, MAP_ANONYMOUS
},
3121 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
, MAP_GROWSDOWN
, MAP_GROWSDOWN
},
3122 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
, MAP_DENYWRITE
, MAP_DENYWRITE
},
3123 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
, MAP_EXECUTABLE
, MAP_EXECUTABLE
},
3124 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
3128 #if defined(TARGET_I386)
3130 /* NOTE: there is really one LDT for all the threads */
3131 static uint8_t *ldt_table
;
3133 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
3140 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
3141 if (size
> bytecount
)
3143 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
3145 return -TARGET_EFAULT
;
3146 /* ??? Should this by byteswapped? */
3147 memcpy(p
, ldt_table
, size
);
3148 unlock_user(p
, ptr
, size
);
3152 /* XXX: add locking support */
3153 static abi_long
write_ldt(CPUX86State
*env
,
3154 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
3156 struct target_modify_ldt_ldt_s ldt_info
;
3157 struct target_modify_ldt_ldt_s
*target_ldt_info
;
3158 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
3159 int seg_not_present
, useable
, lm
;
3160 uint32_t *lp
, entry_1
, entry_2
;
3162 if (bytecount
!= sizeof(ldt_info
))
3163 return -TARGET_EINVAL
;
3164 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
3165 return -TARGET_EFAULT
;
3166 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
3167 ldt_info
.base_addr
= tswapl(target_ldt_info
->base_addr
);
3168 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
3169 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
3170 unlock_user_struct(target_ldt_info
, ptr
, 0);
3172 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
3173 return -TARGET_EINVAL
;
3174 seg_32bit
= ldt_info
.flags
& 1;
3175 contents
= (ldt_info
.flags
>> 1) & 3;
3176 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
3177 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
3178 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
3179 useable
= (ldt_info
.flags
>> 6) & 1;
3183 lm
= (ldt_info
.flags
>> 7) & 1;
3185 if (contents
== 3) {
3187 return -TARGET_EINVAL
;
3188 if (seg_not_present
== 0)
3189 return -TARGET_EINVAL
;
3191 /* allocate the LDT */
3193 env
->ldt
.base
= target_mmap(0,
3194 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
3195 PROT_READ
|PROT_WRITE
,
3196 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
3197 if (env
->ldt
.base
== -1)
3198 return -TARGET_ENOMEM
;
3199 memset(g2h(env
->ldt
.base
), 0,
3200 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
3201 env
->ldt
.limit
= 0xffff;
3202 ldt_table
= g2h(env
->ldt
.base
);
3205 /* NOTE: same code as Linux kernel */
3206 /* Allow LDTs to be cleared by the user. */
3207 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
3210 read_exec_only
== 1 &&
3212 limit_in_pages
== 0 &&
3213 seg_not_present
== 1 &&
3221 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
3222 (ldt_info
.limit
& 0x0ffff);
3223 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
3224 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
3225 (ldt_info
.limit
& 0xf0000) |
3226 ((read_exec_only
^ 1) << 9) |
3228 ((seg_not_present
^ 1) << 15) |
3230 (limit_in_pages
<< 23) |
3234 entry_2
|= (useable
<< 20);
3236 /* Install the new entry ... */
3238 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
3239 lp
[0] = tswap32(entry_1
);
3240 lp
[1] = tswap32(entry_2
);
3244 /* specific and weird i386 syscalls */
3245 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
3246 unsigned long bytecount
)
3252 ret
= read_ldt(ptr
, bytecount
);
3255 ret
= write_ldt(env
, ptr
, bytecount
, 1);
3258 ret
= write_ldt(env
, ptr
, bytecount
, 0);
3261 ret
= -TARGET_ENOSYS
;
3267 #if defined(TARGET_I386) && defined(TARGET_ABI32)
3268 static abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
3270 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
3271 struct target_modify_ldt_ldt_s ldt_info
;
3272 struct target_modify_ldt_ldt_s
*target_ldt_info
;
3273 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
3274 int seg_not_present
, useable
, lm
;
3275 uint32_t *lp
, entry_1
, entry_2
;
3278 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
3279 if (!target_ldt_info
)
3280 return -TARGET_EFAULT
;
3281 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
3282 ldt_info
.base_addr
= tswapl(target_ldt_info
->base_addr
);
3283 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
3284 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
3285 if (ldt_info
.entry_number
== -1) {
3286 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
3287 if (gdt_table
[i
] == 0) {
3288 ldt_info
.entry_number
= i
;
3289 target_ldt_info
->entry_number
= tswap32(i
);
3294 unlock_user_struct(target_ldt_info
, ptr
, 1);
3296 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
3297 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
3298 return -TARGET_EINVAL
;
3299 seg_32bit
= ldt_info
.flags
& 1;
3300 contents
= (ldt_info
.flags
>> 1) & 3;
3301 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
3302 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
3303 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
3304 useable
= (ldt_info
.flags
>> 6) & 1;
3308 lm
= (ldt_info
.flags
>> 7) & 1;
3311 if (contents
== 3) {
3312 if (seg_not_present
== 0)
3313 return -TARGET_EINVAL
;
3316 /* NOTE: same code as Linux kernel */
3317 /* Allow LDTs to be cleared by the user. */
3318 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
3319 if ((contents
== 0 &&
3320 read_exec_only
== 1 &&
3322 limit_in_pages
== 0 &&
3323 seg_not_present
== 1 &&
3331 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
3332 (ldt_info
.limit
& 0x0ffff);
3333 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
3334 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
3335 (ldt_info
.limit
& 0xf0000) |
3336 ((read_exec_only
^ 1) << 9) |
3338 ((seg_not_present
^ 1) << 15) |
3340 (limit_in_pages
<< 23) |
3345 /* Install the new entry ... */
3347 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
3348 lp
[0] = tswap32(entry_1
);
3349 lp
[1] = tswap32(entry_2
);
3353 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
3355 struct target_modify_ldt_ldt_s
*target_ldt_info
;
3356 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
3357 uint32_t base_addr
, limit
, flags
;
3358 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
3359 int seg_not_present
, useable
, lm
;
3360 uint32_t *lp
, entry_1
, entry_2
;
3362 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
3363 if (!target_ldt_info
)
3364 return -TARGET_EFAULT
;
3365 idx
= tswap32(target_ldt_info
->entry_number
);
3366 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
3367 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
3368 unlock_user_struct(target_ldt_info
, ptr
, 1);
3369 return -TARGET_EINVAL
;
3371 lp
= (uint32_t *)(gdt_table
+ idx
);
3372 entry_1
= tswap32(lp
[0]);
3373 entry_2
= tswap32(lp
[1]);
3375 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
3376 contents
= (entry_2
>> 10) & 3;
3377 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
3378 seg_32bit
= (entry_2
>> 22) & 1;
3379 limit_in_pages
= (entry_2
>> 23) & 1;
3380 useable
= (entry_2
>> 20) & 1;
3384 lm
= (entry_2
>> 21) & 1;
3386 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
3387 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
3388 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
3389 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
3390 base_addr
= (entry_1
>> 16) |
3391 (entry_2
& 0xff000000) |
3392 ((entry_2
& 0xff) << 16);
3393 target_ldt_info
->base_addr
= tswapl(base_addr
);
3394 target_ldt_info
->limit
= tswap32(limit
);
3395 target_ldt_info
->flags
= tswap32(flags
);
3396 unlock_user_struct(target_ldt_info
, ptr
, 1);
3399 #endif /* TARGET_I386 && TARGET_ABI32 */
3401 #ifndef TARGET_ABI32
3402 static abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
3409 case TARGET_ARCH_SET_GS
:
3410 case TARGET_ARCH_SET_FS
:
3411 if (code
== TARGET_ARCH_SET_GS
)
3415 cpu_x86_load_seg(env
, idx
, 0);
3416 env
->segs
[idx
].base
= addr
;
3418 case TARGET_ARCH_GET_GS
:
3419 case TARGET_ARCH_GET_FS
:
3420 if (code
== TARGET_ARCH_GET_GS
)
3424 val
= env
->segs
[idx
].base
;
3425 if (put_user(val
, addr
, abi_ulong
))
3426 return -TARGET_EFAULT
;
3429 ret
= -TARGET_EINVAL
;
3436 #endif /* defined(TARGET_I386) */
3438 #if defined(USE_NPTL)
3440 #define NEW_STACK_SIZE PTHREAD_STACK_MIN
3442 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
3445 pthread_mutex_t mutex
;
3446 pthread_cond_t cond
;
3449 abi_ulong child_tidptr
;
3450 abi_ulong parent_tidptr
;
3454 static void *clone_func(void *arg
)
3456 new_thread_info
*info
= arg
;
3462 ts
= (TaskState
*)thread_env
->opaque
;
3463 info
->tid
= gettid();
3464 env
->host_tid
= info
->tid
;
3466 if (info
->child_tidptr
)
3467 put_user_u32(info
->tid
, info
->child_tidptr
);
3468 if (info
->parent_tidptr
)
3469 put_user_u32(info
->tid
, info
->parent_tidptr
);
3470 /* Enable signals. */
3471 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
3472 /* Signal to the parent that we're ready. */
3473 pthread_mutex_lock(&info
->mutex
);
3474 pthread_cond_broadcast(&info
->cond
);
3475 pthread_mutex_unlock(&info
->mutex
);
3476 /* Wait until the parent has finshed initializing the tls state. */
3477 pthread_mutex_lock(&clone_lock
);
3478 pthread_mutex_unlock(&clone_lock
);
3484 /* this stack is the equivalent of the kernel stack associated with a
3486 #define NEW_STACK_SIZE 8192
3488 static int clone_func(void *arg
)
3490 CPUState
*env
= arg
;
3497 /* do_fork() Must return host values and target errnos (unlike most
3498 do_*() functions). */
3499 static int do_fork(CPUState
*env
, unsigned int flags
, abi_ulong newsp
,
3500 abi_ulong parent_tidptr
, target_ulong newtls
,
3501 abi_ulong child_tidptr
)
3507 #if defined(USE_NPTL)
3508 unsigned int nptl_flags
;
3512 /* Emulate vfork() with fork() */
3513 if (flags
& CLONE_VFORK
)
3514 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
3516 if (flags
& CLONE_VM
) {
3517 TaskState
*parent_ts
= (TaskState
*)env
->opaque
;
3518 #if defined(USE_NPTL)
3519 new_thread_info info
;
3520 pthread_attr_t attr
;
3522 ts
= qemu_mallocz(sizeof(TaskState
) + NEW_STACK_SIZE
);
3523 init_task_state(ts
);
3524 new_stack
= ts
->stack
;
3525 /* we create a new CPU instance. */
3526 new_env
= cpu_copy(env
);
3527 /* Init regs that differ from the parent. */
3528 cpu_clone_regs(new_env
, newsp
);
3529 new_env
->opaque
= ts
;
3530 ts
->bprm
= parent_ts
->bprm
;
3531 ts
->info
= parent_ts
->info
;
3532 #if defined(USE_NPTL)
3534 flags
&= ~CLONE_NPTL_FLAGS2
;
3536 if (nptl_flags
& CLONE_CHILD_CLEARTID
) {
3537 ts
->child_tidptr
= child_tidptr
;
3540 if (nptl_flags
& CLONE_SETTLS
)
3541 cpu_set_tls (new_env
, newtls
);
3543 /* Grab a mutex so that thread setup appears atomic. */
3544 pthread_mutex_lock(&clone_lock
);
3546 memset(&info
, 0, sizeof(info
));
3547 pthread_mutex_init(&info
.mutex
, NULL
);
3548 pthread_mutex_lock(&info
.mutex
);
3549 pthread_cond_init(&info
.cond
, NULL
);
3551 if (nptl_flags
& CLONE_CHILD_SETTID
)
3552 info
.child_tidptr
= child_tidptr
;
3553 if (nptl_flags
& CLONE_PARENT_SETTID
)
3554 info
.parent_tidptr
= parent_tidptr
;
3556 ret
= pthread_attr_init(&attr
);
3557 ret
= pthread_attr_setstack(&attr
, new_stack
, NEW_STACK_SIZE
);
3558 /* It is not safe to deliver signals until the child has finished
3559 initializing, so temporarily block all signals. */
3560 sigfillset(&sigmask
);
3561 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
3563 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
3564 /* TODO: Free new CPU state if thread creation failed. */
3566 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
3567 pthread_attr_destroy(&attr
);
3569 /* Wait for the child to initialize. */
3570 pthread_cond_wait(&info
.cond
, &info
.mutex
);
3572 if (flags
& CLONE_PARENT_SETTID
)
3573 put_user_u32(ret
, parent_tidptr
);
3577 pthread_mutex_unlock(&info
.mutex
);
3578 pthread_cond_destroy(&info
.cond
);
3579 pthread_mutex_destroy(&info
.mutex
);
3580 pthread_mutex_unlock(&clone_lock
);
3582 if (flags
& CLONE_NPTL_FLAGS2
)
3584 /* This is probably going to die very quickly, but do it anyway. */
3586 ret
= __clone2(clone_func
, new_stack
+ NEW_STACK_SIZE
, flags
, new_env
);
3588 ret
= clone(clone_func
, new_stack
+ NEW_STACK_SIZE
, flags
, new_env
);
3592 /* if no CLONE_VM, we consider it is a fork */
3593 if ((flags
& ~(CSIGNAL
| CLONE_NPTL_FLAGS2
)) != 0)
3598 /* Child Process. */
3599 cpu_clone_regs(env
, newsp
);
3601 #if defined(USE_NPTL)
3602 /* There is a race condition here. The parent process could
3603 theoretically read the TID in the child process before the child
3604 tid is set. This would require using either ptrace
3605 (not implemented) or having *_tidptr to point at a shared memory
3606 mapping. We can't repeat the spinlock hack used above because
3607 the child process gets its own copy of the lock. */
3608 if (flags
& CLONE_CHILD_SETTID
)
3609 put_user_u32(gettid(), child_tidptr
);
3610 if (flags
& CLONE_PARENT_SETTID
)
3611 put_user_u32(gettid(), parent_tidptr
);
3612 ts
= (TaskState
*)env
->opaque
;
3613 if (flags
& CLONE_SETTLS
)
3614 cpu_set_tls (env
, newtls
);
3615 if (flags
& CLONE_CHILD_CLEARTID
)
3616 ts
->child_tidptr
= child_tidptr
;
3625 /* warning : doesn't handle linux specific flags... */
3626 static int target_to_host_fcntl_cmd(int cmd
)
3629 case TARGET_F_DUPFD
:
3630 case TARGET_F_GETFD
:
3631 case TARGET_F_SETFD
:
3632 case TARGET_F_GETFL
:
3633 case TARGET_F_SETFL
:
3635 case TARGET_F_GETLK
:
3637 case TARGET_F_SETLK
:
3639 case TARGET_F_SETLKW
:
3641 case TARGET_F_GETOWN
:
3643 case TARGET_F_SETOWN
:
3645 case TARGET_F_GETSIG
:
3647 case TARGET_F_SETSIG
:
3649 #if TARGET_ABI_BITS == 32
3650 case TARGET_F_GETLK64
:
3652 case TARGET_F_SETLK64
:
3654 case TARGET_F_SETLKW64
:
3658 return -TARGET_EINVAL
;
3660 return -TARGET_EINVAL
;
3663 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
3666 struct target_flock
*target_fl
;
3667 struct flock64 fl64
;
3668 struct target_flock64
*target_fl64
;
3670 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
3672 if (host_cmd
== -TARGET_EINVAL
)
3676 case TARGET_F_GETLK
:
3677 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
3678 return -TARGET_EFAULT
;
3679 fl
.l_type
= tswap16(target_fl
->l_type
);
3680 fl
.l_whence
= tswap16(target_fl
->l_whence
);
3681 fl
.l_start
= tswapl(target_fl
->l_start
);
3682 fl
.l_len
= tswapl(target_fl
->l_len
);
3683 fl
.l_pid
= tswapl(target_fl
->l_pid
);
3684 unlock_user_struct(target_fl
, arg
, 0);
3685 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
3687 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg
, 0))
3688 return -TARGET_EFAULT
;
3689 target_fl
->l_type
= tswap16(fl
.l_type
);
3690 target_fl
->l_whence
= tswap16(fl
.l_whence
);
3691 target_fl
->l_start
= tswapl(fl
.l_start
);
3692 target_fl
->l_len
= tswapl(fl
.l_len
);
3693 target_fl
->l_pid
= tswapl(fl
.l_pid
);
3694 unlock_user_struct(target_fl
, arg
, 1);
3698 case TARGET_F_SETLK
:
3699 case TARGET_F_SETLKW
:
3700 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
3701 return -TARGET_EFAULT
;
3702 fl
.l_type
= tswap16(target_fl
->l_type
);
3703 fl
.l_whence
= tswap16(target_fl
->l_whence
);
3704 fl
.l_start
= tswapl(target_fl
->l_start
);
3705 fl
.l_len
= tswapl(target_fl
->l_len
);
3706 fl
.l_pid
= tswapl(target_fl
->l_pid
);
3707 unlock_user_struct(target_fl
, arg
, 0);
3708 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
3711 case TARGET_F_GETLK64
:
3712 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
3713 return -TARGET_EFAULT
;
3714 fl64
.l_type
= tswap16(target_fl64
->l_type
) >> 1;
3715 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
3716 fl64
.l_start
= tswapl(target_fl64
->l_start
);
3717 fl64
.l_len
= tswapl(target_fl64
->l_len
);
3718 fl64
.l_pid
= tswap16(target_fl64
->l_pid
);
3719 unlock_user_struct(target_fl64
, arg
, 0);
3720 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
3722 if (!lock_user_struct(VERIFY_WRITE
, target_fl64
, arg
, 0))
3723 return -TARGET_EFAULT
;
3724 target_fl64
->l_type
= tswap16(fl64
.l_type
) >> 1;
3725 target_fl64
->l_whence
= tswap16(fl64
.l_whence
);
3726 target_fl64
->l_start
= tswapl(fl64
.l_start
);
3727 target_fl64
->l_len
= tswapl(fl64
.l_len
);
3728 target_fl64
->l_pid
= tswapl(fl64
.l_pid
);
3729 unlock_user_struct(target_fl64
, arg
, 1);
3732 case TARGET_F_SETLK64
:
3733 case TARGET_F_SETLKW64
:
3734 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
3735 return -TARGET_EFAULT
;
3736 fl64
.l_type
= tswap16(target_fl64
->l_type
) >> 1;
3737 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
3738 fl64
.l_start
= tswapl(target_fl64
->l_start
);
3739 fl64
.l_len
= tswapl(target_fl64
->l_len
);
3740 fl64
.l_pid
= tswap16(target_fl64
->l_pid
);
3741 unlock_user_struct(target_fl64
, arg
, 0);
3742 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
3745 case TARGET_F_GETFL
:
3746 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
3748 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
3752 case TARGET_F_SETFL
:
3753 ret
= get_errno(fcntl(fd
, host_cmd
, target_to_host_bitmask(arg
, fcntl_flags_tbl
)));
3756 case TARGET_F_SETOWN
:
3757 case TARGET_F_GETOWN
:
3758 case TARGET_F_SETSIG
:
3759 case TARGET_F_GETSIG
:
3760 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
3764 ret
= get_errno(fcntl(fd
, cmd
, arg
));
3772 static inline int high2lowuid(int uid
)
3780 static inline int high2lowgid(int gid
)
3788 static inline int low2highuid(int uid
)
3790 if ((int16_t)uid
== -1)
3796 static inline int low2highgid(int gid
)
3798 if ((int16_t)gid
== -1)
3804 #endif /* USE_UID16 */
3806 void syscall_init(void)
3809 const argtype
*arg_type
;
3813 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
3814 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
3815 #include "syscall_types.h"
3817 #undef STRUCT_SPECIAL
3819 /* we patch the ioctl size if necessary. We rely on the fact that
3820 no ioctl has all the bits at '1' in the size field */
3822 while (ie
->target_cmd
!= 0) {
3823 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
3824 TARGET_IOC_SIZEMASK
) {
3825 arg_type
= ie
->arg_type
;
3826 if (arg_type
[0] != TYPE_PTR
) {
3827 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
3832 size
= thunk_type_size(arg_type
, 0);
3833 ie
->target_cmd
= (ie
->target_cmd
&
3834 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
3835 (size
<< TARGET_IOC_SIZESHIFT
);
3838 /* Build target_to_host_errno_table[] table from
3839 * host_to_target_errno_table[]. */
3840 for (i
=0; i
< ERRNO_TABLE_SIZE
; i
++)
3841 target_to_host_errno_table
[host_to_target_errno_table
[i
]] = i
;
3843 /* automatic consistency check if same arch */
3844 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3845 (defined(__x86_64__) && defined(TARGET_X86_64))
3846 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
3847 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3848 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
3855 #if TARGET_ABI_BITS == 32
3856 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
3858 #ifdef TARGET_WORDS_BIGENDIAN
3859 return ((uint64_t)word0
<< 32) | word1
;
3861 return ((uint64_t)word1
<< 32) | word0
;
3864 #else /* TARGET_ABI_BITS == 32 */
3865 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
3869 #endif /* TARGET_ABI_BITS != 32 */
3871 #ifdef TARGET_NR_truncate64
3872 static inline abi_long
target_truncate64(void *cpu_env
, const char *arg1
,
3878 if (((CPUARMState
*)cpu_env
)->eabi
)
3884 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
3888 #ifdef TARGET_NR_ftruncate64
3889 static inline abi_long
target_ftruncate64(void *cpu_env
, abi_long arg1
,
3895 if (((CPUARMState
*)cpu_env
)->eabi
)
3901 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
3905 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
3906 abi_ulong target_addr
)
3908 struct target_timespec
*target_ts
;
3910 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1))
3911 return -TARGET_EFAULT
;
3912 host_ts
->tv_sec
= tswapl(target_ts
->tv_sec
);
3913 host_ts
->tv_nsec
= tswapl(target_ts
->tv_nsec
);
3914 unlock_user_struct(target_ts
, target_addr
, 0);
3918 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
3919 struct timespec
*host_ts
)
3921 struct target_timespec
*target_ts
;
3923 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0))
3924 return -TARGET_EFAULT
;
3925 target_ts
->tv_sec
= tswapl(host_ts
->tv_sec
);
3926 target_ts
->tv_nsec
= tswapl(host_ts
->tv_nsec
);
3927 unlock_user_struct(target_ts
, target_addr
, 1);
3931 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
3932 static inline abi_long
host_to_target_stat64(void *cpu_env
,
3933 abi_ulong target_addr
,
3934 struct stat
*host_st
)
3937 if (((CPUARMState
*)cpu_env
)->eabi
) {
3938 struct target_eabi_stat64
*target_st
;
3940 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
3941 return -TARGET_EFAULT
;
3942 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
3943 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
3944 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
3945 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3946 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
3948 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
3949 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
3950 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
3951 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
3952 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
3953 __put_user(host_st
->st_size
, &target_st
->st_size
);
3954 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
3955 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
3956 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
3957 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
3958 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
3959 unlock_user_struct(target_st
, target_addr
, 1);
3963 #if TARGET_LONG_BITS == 64
3964 struct target_stat
*target_st
;
3966 struct target_stat64
*target_st
;
3969 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
3970 return -TARGET_EFAULT
;
3971 memset(target_st
, 0, sizeof(*target_st
));
3972 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
3973 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
3974 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3975 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
3977 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
3978 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
3979 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
3980 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
3981 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
3982 /* XXX: better use of kernel struct */
3983 __put_user(host_st
->st_size
, &target_st
->st_size
);
3984 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
3985 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
3986 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
3987 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
3988 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
3989 unlock_user_struct(target_st
, target_addr
, 1);
3996 #if defined(USE_NPTL)
3997 /* ??? Using host futex calls even when target atomic operations
3998 are not really atomic probably breaks things. However implementing
3999 futexes locally would make futexes shared between multiple processes
4000 tricky. However they're probably useless because guest atomic
4001 operations won't work either. */
4002 static int do_futex(target_ulong uaddr
, int op
, int val
, target_ulong timeout
,
4003 target_ulong uaddr2
, int val3
)
4005 struct timespec ts
, *pts
;
4007 /* ??? We assume FUTEX_* constants are the same on both host
4009 #ifdef FUTEX_CMD_MASK
4010 switch ((op
&FUTEX_CMD_MASK
)) {
4017 target_to_host_timespec(pts
, timeout
);
4021 return get_errno(sys_futex(g2h(uaddr
), op
, tswap32(val
),
4024 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
4026 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, g2h(uaddr2
), val3
));
4028 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
4030 return get_errno(sys_futex(g2h(uaddr
), op
, val
,
4031 NULL
, g2h(uaddr2
), 0));
4032 case FUTEX_CMP_REQUEUE
:
4033 return get_errno(sys_futex(g2h(uaddr
), op
, val
,
4034 NULL
, g2h(uaddr2
), tswap32(val3
)));
4036 return -TARGET_ENOSYS
;
4041 /* Map host to target signal numbers for the wait family of syscalls.
4042 Assume all other status bits are the same. */
4043 static int host_to_target_waitstatus(int status
)
4045 if (WIFSIGNALED(status
)) {
4046 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
4048 if (WIFSTOPPED(status
)) {
4049 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
4055 int get_osversion(void)
4057 static int osversion
;
4058 struct new_utsname buf
;
4063 if (qemu_uname_release
&& *qemu_uname_release
) {
4064 s
= qemu_uname_release
;
4066 if (sys_uname(&buf
))
4071 for (i
= 0; i
< 3; i
++) {
4073 while (*s
>= '0' && *s
<= '9') {
4078 tmp
= (tmp
<< 8) + n
;
4086 /* do_syscall() should always have a single exit point at the end so
4087 that actions, such as logging of syscall results, can be performed.
4088 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
4089 abi_long
do_syscall(void *cpu_env
, int num
, abi_long arg1
,
4090 abi_long arg2
, abi_long arg3
, abi_long arg4
,
4091 abi_long arg5
, abi_long arg6
)
4099 gemu_log("syscall %d", num
);
4102 print_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
4105 case TARGET_NR_exit
:
4107 /* In old applications this may be used to implement _exit(2).
4108 However in threaded applictions it is used for thread termination,
4109 and _exit_group is used for application termination.
4110 Do thread termination if we have more then one thread. */
4111 /* FIXME: This probably breaks if a signal arrives. We should probably
4112 be disabling signals. */
4113 if (first_cpu
->next_cpu
) {
4121 while (p
&& p
!= (CPUState
*)cpu_env
) {
4122 lastp
= &p
->next_cpu
;
4125 /* If we didn't find the CPU for this thread then something is
4129 /* Remove the CPU from the list. */
4130 *lastp
= p
->next_cpu
;
4132 ts
= ((CPUState
*)cpu_env
)->opaque
;
4133 if (ts
->child_tidptr
) {
4134 put_user_u32(0, ts
->child_tidptr
);
4135 sys_futex(g2h(ts
->child_tidptr
), FUTEX_WAKE
, INT_MAX
,
4138 /* TODO: Free CPU state. */
4145 gdb_exit(cpu_env
, arg1
);
4147 ret
= 0; /* avoid warning */
4149 case TARGET_NR_read
:
4153 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
4155 ret
= get_errno(read(arg1
, p
, arg3
));
4156 unlock_user(p
, arg2
, ret
);
4159 case TARGET_NR_write
:
4160 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
4162 ret
= get_errno(write(arg1
, p
, arg3
));
4163 unlock_user(p
, arg2
, 0);
4165 case TARGET_NR_open
:
4166 if (!(p
= lock_user_string(arg1
)))
4168 ret
= get_errno(open(path(p
),
4169 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
4171 unlock_user(p
, arg1
, 0);
4173 #if defined(TARGET_NR_openat) && defined(__NR_openat)
4174 case TARGET_NR_openat
:
4175 if (!(p
= lock_user_string(arg2
)))
4177 ret
= get_errno(sys_openat(arg1
,
4179 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
4181 unlock_user(p
, arg2
, 0);
4184 case TARGET_NR_close
:
4185 ret
= get_errno(close(arg1
));
4190 case TARGET_NR_fork
:
4191 ret
= get_errno(do_fork(cpu_env
, SIGCHLD
, 0, 0, 0, 0));
4193 #ifdef TARGET_NR_waitpid
4194 case TARGET_NR_waitpid
:
4197 ret
= get_errno(waitpid(arg1
, &status
, arg3
));
4198 if (!is_error(ret
) && arg2
4199 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
4204 #ifdef TARGET_NR_waitid
4205 case TARGET_NR_waitid
:
4209 ret
= get_errno(waitid(arg1
, arg2
, &info
, arg4
));
4210 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
4211 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
4213 host_to_target_siginfo(p
, &info
);
4214 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
4219 #ifdef TARGET_NR_creat /* not on alpha */
4220 case TARGET_NR_creat
:
4221 if (!(p
= lock_user_string(arg1
)))
4223 ret
= get_errno(creat(p
, arg2
));
4224 unlock_user(p
, arg1
, 0);
4227 case TARGET_NR_link
:
4230 p
= lock_user_string(arg1
);
4231 p2
= lock_user_string(arg2
);
4233 ret
= -TARGET_EFAULT
;
4235 ret
= get_errno(link(p
, p2
));
4236 unlock_user(p2
, arg2
, 0);
4237 unlock_user(p
, arg1
, 0);
4240 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4241 case TARGET_NR_linkat
:
4246 p
= lock_user_string(arg2
);
4247 p2
= lock_user_string(arg4
);
4249 ret
= -TARGET_EFAULT
;
4251 ret
= get_errno(sys_linkat(arg1
, p
, arg3
, p2
, arg5
));
4252 unlock_user(p
, arg2
, 0);
4253 unlock_user(p2
, arg4
, 0);
4257 case TARGET_NR_unlink
:
4258 if (!(p
= lock_user_string(arg1
)))
4260 ret
= get_errno(unlink(p
));
4261 unlock_user(p
, arg1
, 0);
4263 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4264 case TARGET_NR_unlinkat
:
4265 if (!(p
= lock_user_string(arg2
)))
4267 ret
= get_errno(sys_unlinkat(arg1
, p
, arg3
));
4268 unlock_user(p
, arg2
, 0);
4271 case TARGET_NR_execve
:
4273 char **argp
, **envp
;
4276 abi_ulong guest_argp
;
4277 abi_ulong guest_envp
;
4283 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
4284 if (get_user_ual(addr
, gp
))
4292 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
4293 if (get_user_ual(addr
, gp
))
4300 argp
= alloca((argc
+ 1) * sizeof(void *));
4301 envp
= alloca((envc
+ 1) * sizeof(void *));
4303 for (gp
= guest_argp
, q
= argp
; gp
;
4304 gp
+= sizeof(abi_ulong
), q
++) {
4305 if (get_user_ual(addr
, gp
))
4309 if (!(*q
= lock_user_string(addr
)))
4314 for (gp
= guest_envp
, q
= envp
; gp
;
4315 gp
+= sizeof(abi_ulong
), q
++) {
4316 if (get_user_ual(addr
, gp
))
4320 if (!(*q
= lock_user_string(addr
)))
4325 if (!(p
= lock_user_string(arg1
)))
4327 ret
= get_errno(execve(p
, argp
, envp
));
4328 unlock_user(p
, arg1
, 0);
4333 ret
= -TARGET_EFAULT
;
4336 for (gp
= guest_argp
, q
= argp
; *q
;
4337 gp
+= sizeof(abi_ulong
), q
++) {
4338 if (get_user_ual(addr
, gp
)
4341 unlock_user(*q
, addr
, 0);
4343 for (gp
= guest_envp
, q
= envp
; *q
;
4344 gp
+= sizeof(abi_ulong
), q
++) {
4345 if (get_user_ual(addr
, gp
)
4348 unlock_user(*q
, addr
, 0);
4352 case TARGET_NR_chdir
:
4353 if (!(p
= lock_user_string(arg1
)))
4355 ret
= get_errno(chdir(p
));
4356 unlock_user(p
, arg1
, 0);
4358 #ifdef TARGET_NR_time
4359 case TARGET_NR_time
:
4362 ret
= get_errno(time(&host_time
));
4365 && put_user_sal(host_time
, arg1
))
4370 case TARGET_NR_mknod
:
4371 if (!(p
= lock_user_string(arg1
)))
4373 ret
= get_errno(mknod(p
, arg2
, arg3
));
4374 unlock_user(p
, arg1
, 0);
4376 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4377 case TARGET_NR_mknodat
:
4378 if (!(p
= lock_user_string(arg2
)))
4380 ret
= get_errno(sys_mknodat(arg1
, p
, arg3
, arg4
));
4381 unlock_user(p
, arg2
, 0);
4384 case TARGET_NR_chmod
:
4385 if (!(p
= lock_user_string(arg1
)))
4387 ret
= get_errno(chmod(p
, arg2
));
4388 unlock_user(p
, arg1
, 0);
4390 #ifdef TARGET_NR_break
4391 case TARGET_NR_break
:
4394 #ifdef TARGET_NR_oldstat
4395 case TARGET_NR_oldstat
:
4398 case TARGET_NR_lseek
:
4399 ret
= get_errno(lseek(arg1
, arg2
, arg3
));
4401 #ifdef TARGET_NR_getxpid
4402 case TARGET_NR_getxpid
:
4404 case TARGET_NR_getpid
:
4406 ret
= get_errno(getpid());
4408 case TARGET_NR_mount
:
4410 /* need to look at the data field */
4412 p
= lock_user_string(arg1
);
4413 p2
= lock_user_string(arg2
);
4414 p3
= lock_user_string(arg3
);
4415 if (!p
|| !p2
|| !p3
)
4416 ret
= -TARGET_EFAULT
;
4418 /* FIXME - arg5 should be locked, but it isn't clear how to
4419 * do that since it's not guaranteed to be a NULL-terminated
4422 ret
= get_errno(mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(arg5
)));
4423 unlock_user(p
, arg1
, 0);
4424 unlock_user(p2
, arg2
, 0);
4425 unlock_user(p3
, arg3
, 0);
4428 #ifdef TARGET_NR_umount
4429 case TARGET_NR_umount
:
4430 if (!(p
= lock_user_string(arg1
)))
4432 ret
= get_errno(umount(p
));
4433 unlock_user(p
, arg1
, 0);
4436 #ifdef TARGET_NR_stime /* not on alpha */
4437 case TARGET_NR_stime
:
4440 if (get_user_sal(host_time
, arg1
))
4442 ret
= get_errno(stime(&host_time
));
4446 case TARGET_NR_ptrace
:
4448 #ifdef TARGET_NR_alarm /* not on alpha */
4449 case TARGET_NR_alarm
:
4453 #ifdef TARGET_NR_oldfstat
4454 case TARGET_NR_oldfstat
:
4457 #ifdef TARGET_NR_pause /* not on alpha */
4458 case TARGET_NR_pause
:
4459 ret
= get_errno(pause());
4462 #ifdef TARGET_NR_utime
4463 case TARGET_NR_utime
:
4465 struct utimbuf tbuf
, *host_tbuf
;
4466 struct target_utimbuf
*target_tbuf
;
4468 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
4470 tbuf
.actime
= tswapl(target_tbuf
->actime
);
4471 tbuf
.modtime
= tswapl(target_tbuf
->modtime
);
4472 unlock_user_struct(target_tbuf
, arg2
, 0);
4477 if (!(p
= lock_user_string(arg1
)))
4479 ret
= get_errno(utime(p
, host_tbuf
));
4480 unlock_user(p
, arg1
, 0);
4484 case TARGET_NR_utimes
:
4486 struct timeval
*tvp
, tv
[2];
4488 if (copy_from_user_timeval(&tv
[0], arg2
)
4489 || copy_from_user_timeval(&tv
[1],
4490 arg2
+ sizeof(struct target_timeval
)))
4496 if (!(p
= lock_user_string(arg1
)))
4498 ret
= get_errno(utimes(p
, tvp
));
4499 unlock_user(p
, arg1
, 0);
4502 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4503 case TARGET_NR_futimesat
:
4505 struct timeval
*tvp
, tv
[2];
4507 if (copy_from_user_timeval(&tv
[0], arg3
)
4508 || copy_from_user_timeval(&tv
[1],
4509 arg3
+ sizeof(struct target_timeval
)))
4515 if (!(p
= lock_user_string(arg2
)))
4517 ret
= get_errno(sys_futimesat(arg1
, path(p
), tvp
));
4518 unlock_user(p
, arg2
, 0);
4522 #ifdef TARGET_NR_stty
4523 case TARGET_NR_stty
:
4526 #ifdef TARGET_NR_gtty
4527 case TARGET_NR_gtty
:
4530 case TARGET_NR_access
:
4531 if (!(p
= lock_user_string(arg1
)))
4533 ret
= get_errno(access(p
, arg2
));
4534 unlock_user(p
, arg1
, 0);
4536 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4537 case TARGET_NR_faccessat
:
4538 if (!(p
= lock_user_string(arg2
)))
4540 ret
= get_errno(sys_faccessat(arg1
, p
, arg3
));
4541 unlock_user(p
, arg2
, 0);
4544 #ifdef TARGET_NR_nice /* not on alpha */
4545 case TARGET_NR_nice
:
4546 ret
= get_errno(nice(arg1
));
4549 #ifdef TARGET_NR_ftime
4550 case TARGET_NR_ftime
:
4553 case TARGET_NR_sync
:
4557 case TARGET_NR_kill
:
4558 ret
= get_errno(kill(arg1
, target_to_host_signal(arg2
)));
4560 case TARGET_NR_rename
:
4563 p
= lock_user_string(arg1
);
4564 p2
= lock_user_string(arg2
);
4566 ret
= -TARGET_EFAULT
;
4568 ret
= get_errno(rename(p
, p2
));
4569 unlock_user(p2
, arg2
, 0);
4570 unlock_user(p
, arg1
, 0);
4573 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4574 case TARGET_NR_renameat
:
4577 p
= lock_user_string(arg2
);
4578 p2
= lock_user_string(arg4
);
4580 ret
= -TARGET_EFAULT
;
4582 ret
= get_errno(sys_renameat(arg1
, p
, arg3
, p2
));
4583 unlock_user(p2
, arg4
, 0);
4584 unlock_user(p
, arg2
, 0);
4588 case TARGET_NR_mkdir
:
4589 if (!(p
= lock_user_string(arg1
)))
4591 ret
= get_errno(mkdir(p
, arg2
));
4592 unlock_user(p
, arg1
, 0);
4594 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4595 case TARGET_NR_mkdirat
:
4596 if (!(p
= lock_user_string(arg2
)))
4598 ret
= get_errno(sys_mkdirat(arg1
, p
, arg3
));
4599 unlock_user(p
, arg2
, 0);
4602 case TARGET_NR_rmdir
:
4603 if (!(p
= lock_user_string(arg1
)))
4605 ret
= get_errno(rmdir(p
));
4606 unlock_user(p
, arg1
, 0);
4609 ret
= get_errno(dup(arg1
));
4611 case TARGET_NR_pipe
:
4612 ret
= do_pipe(cpu_env
, arg1
, 0);
4614 #ifdef TARGET_NR_pipe2
4615 case TARGET_NR_pipe2
:
4616 ret
= do_pipe(cpu_env
, arg1
, arg2
);
4619 case TARGET_NR_times
:
4621 struct target_tms
*tmsp
;
4623 ret
= get_errno(times(&tms
));
4625 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
4628 tmsp
->tms_utime
= tswapl(host_to_target_clock_t(tms
.tms_utime
));
4629 tmsp
->tms_stime
= tswapl(host_to_target_clock_t(tms
.tms_stime
));
4630 tmsp
->tms_cutime
= tswapl(host_to_target_clock_t(tms
.tms_cutime
));
4631 tmsp
->tms_cstime
= tswapl(host_to_target_clock_t(tms
.tms_cstime
));
4634 ret
= host_to_target_clock_t(ret
);
4637 #ifdef TARGET_NR_prof
4638 case TARGET_NR_prof
:
4641 #ifdef TARGET_NR_signal
4642 case TARGET_NR_signal
:
4645 case TARGET_NR_acct
:
4647 ret
= get_errno(acct(NULL
));
4649 if (!(p
= lock_user_string(arg1
)))
4651 ret
= get_errno(acct(path(p
)));
4652 unlock_user(p
, arg1
, 0);
4655 #ifdef TARGET_NR_umount2 /* not on alpha */
4656 case TARGET_NR_umount2
:
4657 if (!(p
= lock_user_string(arg1
)))
4659 ret
= get_errno(umount2(p
, arg2
));
4660 unlock_user(p
, arg1
, 0);
4663 #ifdef TARGET_NR_lock
4664 case TARGET_NR_lock
:
4667 case TARGET_NR_ioctl
:
4668 ret
= do_ioctl(arg1
, arg2
, arg3
);
4670 case TARGET_NR_fcntl
:
4671 ret
= do_fcntl(arg1
, arg2
, arg3
);
4673 #ifdef TARGET_NR_mpx
4677 case TARGET_NR_setpgid
:
4678 ret
= get_errno(setpgid(arg1
, arg2
));
4680 #ifdef TARGET_NR_ulimit
4681 case TARGET_NR_ulimit
:
4684 #ifdef TARGET_NR_oldolduname
4685 case TARGET_NR_oldolduname
:
4688 case TARGET_NR_umask
:
4689 ret
= get_errno(umask(arg1
));
4691 case TARGET_NR_chroot
:
4692 if (!(p
= lock_user_string(arg1
)))
4694 ret
= get_errno(chroot(p
));
4695 unlock_user(p
, arg1
, 0);
4697 case TARGET_NR_ustat
:
4699 case TARGET_NR_dup2
:
4700 ret
= get_errno(dup2(arg1
, arg2
));
4702 #ifdef TARGET_NR_getppid /* not on alpha */
4703 case TARGET_NR_getppid
:
4704 ret
= get_errno(getppid());
4707 case TARGET_NR_getpgrp
:
4708 ret
= get_errno(getpgrp());
4710 case TARGET_NR_setsid
:
4711 ret
= get_errno(setsid());
4713 #ifdef TARGET_NR_sigaction
4714 case TARGET_NR_sigaction
:
4716 #if !defined(TARGET_MIPS)
4717 struct target_old_sigaction
*old_act
;
4718 struct target_sigaction act
, oact
, *pact
;
4720 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
4722 act
._sa_handler
= old_act
->_sa_handler
;
4723 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
4724 act
.sa_flags
= old_act
->sa_flags
;
4725 act
.sa_restorer
= old_act
->sa_restorer
;
4726 unlock_user_struct(old_act
, arg2
, 0);
4731 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
4732 if (!is_error(ret
) && arg3
) {
4733 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
4735 old_act
->_sa_handler
= oact
._sa_handler
;
4736 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
4737 old_act
->sa_flags
= oact
.sa_flags
;
4738 old_act
->sa_restorer
= oact
.sa_restorer
;
4739 unlock_user_struct(old_act
, arg3
, 1);
4742 struct target_sigaction act
, oact
, *pact
, *old_act
;
4745 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
4747 act
._sa_handler
= old_act
->_sa_handler
;
4748 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
4749 act
.sa_flags
= old_act
->sa_flags
;
4750 unlock_user_struct(old_act
, arg2
, 0);
4756 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
4758 if (!is_error(ret
) && arg3
) {
4759 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
4761 old_act
->_sa_handler
= oact
._sa_handler
;
4762 old_act
->sa_flags
= oact
.sa_flags
;
4763 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
4764 old_act
->sa_mask
.sig
[1] = 0;
4765 old_act
->sa_mask
.sig
[2] = 0;
4766 old_act
->sa_mask
.sig
[3] = 0;
4767 unlock_user_struct(old_act
, arg3
, 1);
4773 case TARGET_NR_rt_sigaction
:
4775 struct target_sigaction
*act
;
4776 struct target_sigaction
*oact
;
4779 if (!lock_user_struct(VERIFY_READ
, act
, arg2
, 1))
4784 if (!lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
4785 ret
= -TARGET_EFAULT
;
4786 goto rt_sigaction_fail
;
4790 ret
= get_errno(do_sigaction(arg1
, act
, oact
));
4793 unlock_user_struct(act
, arg2
, 0);
4795 unlock_user_struct(oact
, arg3
, 1);
4798 #ifdef TARGET_NR_sgetmask /* not on alpha */
4799 case TARGET_NR_sgetmask
:
4802 abi_ulong target_set
;
4803 sigprocmask(0, NULL
, &cur_set
);
4804 host_to_target_old_sigset(&target_set
, &cur_set
);
4809 #ifdef TARGET_NR_ssetmask /* not on alpha */
4810 case TARGET_NR_ssetmask
:
4812 sigset_t set
, oset
, cur_set
;
4813 abi_ulong target_set
= arg1
;
4814 sigprocmask(0, NULL
, &cur_set
);
4815 target_to_host_old_sigset(&set
, &target_set
);
4816 sigorset(&set
, &set
, &cur_set
);
4817 sigprocmask(SIG_SETMASK
, &set
, &oset
);
4818 host_to_target_old_sigset(&target_set
, &oset
);
4823 #ifdef TARGET_NR_sigprocmask
4824 case TARGET_NR_sigprocmask
:
4827 sigset_t set
, oldset
, *set_ptr
;
4831 case TARGET_SIG_BLOCK
:
4834 case TARGET_SIG_UNBLOCK
:
4837 case TARGET_SIG_SETMASK
:
4841 ret
= -TARGET_EINVAL
;
4844 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
4846 target_to_host_old_sigset(&set
, p
);
4847 unlock_user(p
, arg2
, 0);
4853 ret
= get_errno(sigprocmask(arg1
, set_ptr
, &oldset
));
4854 if (!is_error(ret
) && arg3
) {
4855 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
4857 host_to_target_old_sigset(p
, &oldset
);
4858 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
4863 case TARGET_NR_rt_sigprocmask
:
4866 sigset_t set
, oldset
, *set_ptr
;
4870 case TARGET_SIG_BLOCK
:
4873 case TARGET_SIG_UNBLOCK
:
4876 case TARGET_SIG_SETMASK
:
4880 ret
= -TARGET_EINVAL
;
4883 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
4885 target_to_host_sigset(&set
, p
);
4886 unlock_user(p
, arg2
, 0);
4892 ret
= get_errno(sigprocmask(how
, set_ptr
, &oldset
));
4893 if (!is_error(ret
) && arg3
) {
4894 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
4896 host_to_target_sigset(p
, &oldset
);
4897 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
4901 #ifdef TARGET_NR_sigpending
4902 case TARGET_NR_sigpending
:
4905 ret
= get_errno(sigpending(&set
));
4906 if (!is_error(ret
)) {
4907 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
4909 host_to_target_old_sigset(p
, &set
);
4910 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
4915 case TARGET_NR_rt_sigpending
:
4918 ret
= get_errno(sigpending(&set
));
4919 if (!is_error(ret
)) {
4920 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
4922 host_to_target_sigset(p
, &set
);
4923 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
4927 #ifdef TARGET_NR_sigsuspend
4928 case TARGET_NR_sigsuspend
:
4931 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
4933 target_to_host_old_sigset(&set
, p
);
4934 unlock_user(p
, arg1
, 0);
4935 ret
= get_errno(sigsuspend(&set
));
4939 case TARGET_NR_rt_sigsuspend
:
4942 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
4944 target_to_host_sigset(&set
, p
);
4945 unlock_user(p
, arg1
, 0);
4946 ret
= get_errno(sigsuspend(&set
));
4949 case TARGET_NR_rt_sigtimedwait
:
4952 struct timespec uts
, *puts
;
4955 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
4957 target_to_host_sigset(&set
, p
);
4958 unlock_user(p
, arg1
, 0);
4961 target_to_host_timespec(puts
, arg3
);
4965 ret
= get_errno(sigtimedwait(&set
, &uinfo
, puts
));
4966 if (!is_error(ret
) && arg2
) {
4967 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
), 0)))
4969 host_to_target_siginfo(p
, &uinfo
);
4970 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
4974 case TARGET_NR_rt_sigqueueinfo
:
4977 if (!(p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_sigset_t
), 1)))
4979 target_to_host_siginfo(&uinfo
, p
);
4980 unlock_user(p
, arg1
, 0);
4981 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, arg2
, &uinfo
));
4984 #ifdef TARGET_NR_sigreturn
4985 case TARGET_NR_sigreturn
:
4986 /* NOTE: ret is eax, so not transcoding must be done */
4987 ret
= do_sigreturn(cpu_env
);
4990 case TARGET_NR_rt_sigreturn
:
4991 /* NOTE: ret is eax, so not transcoding must be done */
4992 ret
= do_rt_sigreturn(cpu_env
);
4994 case TARGET_NR_sethostname
:
4995 if (!(p
= lock_user_string(arg1
)))
4997 ret
= get_errno(sethostname(p
, arg2
));
4998 unlock_user(p
, arg1
, 0);
5000 case TARGET_NR_setrlimit
:
5002 /* XXX: convert resource ? */
5003 int resource
= arg1
;
5004 struct target_rlimit
*target_rlim
;
5006 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
5008 rlim
.rlim_cur
= tswapl(target_rlim
->rlim_cur
);
5009 rlim
.rlim_max
= tswapl(target_rlim
->rlim_max
);
5010 unlock_user_struct(target_rlim
, arg2
, 0);
5011 ret
= get_errno(setrlimit(resource
, &rlim
));
5014 case TARGET_NR_getrlimit
:
5016 /* XXX: convert resource ? */
5017 int resource
= arg1
;
5018 struct target_rlimit
*target_rlim
;
5021 ret
= get_errno(getrlimit(resource
, &rlim
));
5022 if (!is_error(ret
)) {
5023 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
5025 rlim
.rlim_cur
= tswapl(target_rlim
->rlim_cur
);
5026 rlim
.rlim_max
= tswapl(target_rlim
->rlim_max
);
5027 unlock_user_struct(target_rlim
, arg2
, 1);
5031 case TARGET_NR_getrusage
:
5033 struct rusage rusage
;
5034 ret
= get_errno(getrusage(arg1
, &rusage
));
5035 if (!is_error(ret
)) {
5036 host_to_target_rusage(arg2
, &rusage
);
5040 case TARGET_NR_gettimeofday
:
5043 ret
= get_errno(gettimeofday(&tv
, NULL
));
5044 if (!is_error(ret
)) {
5045 if (copy_to_user_timeval(arg1
, &tv
))
5050 case TARGET_NR_settimeofday
:
5053 if (copy_from_user_timeval(&tv
, arg1
))
5055 ret
= get_errno(settimeofday(&tv
, NULL
));
5058 #ifdef TARGET_NR_select
5059 case TARGET_NR_select
:
5061 struct target_sel_arg_struct
*sel
;
5062 abi_ulong inp
, outp
, exp
, tvp
;
5065 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1))
5067 nsel
= tswapl(sel
->n
);
5068 inp
= tswapl(sel
->inp
);
5069 outp
= tswapl(sel
->outp
);
5070 exp
= tswapl(sel
->exp
);
5071 tvp
= tswapl(sel
->tvp
);
5072 unlock_user_struct(sel
, arg1
, 0);
5073 ret
= do_select(nsel
, inp
, outp
, exp
, tvp
);
5077 case TARGET_NR_symlink
:
5080 p
= lock_user_string(arg1
);
5081 p2
= lock_user_string(arg2
);
5083 ret
= -TARGET_EFAULT
;
5085 ret
= get_errno(symlink(p
, p2
));
5086 unlock_user(p2
, arg2
, 0);
5087 unlock_user(p
, arg1
, 0);
5090 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5091 case TARGET_NR_symlinkat
:
5094 p
= lock_user_string(arg1
);
5095 p2
= lock_user_string(arg3
);
5097 ret
= -TARGET_EFAULT
;
5099 ret
= get_errno(sys_symlinkat(p
, arg2
, p2
));
5100 unlock_user(p2
, arg3
, 0);
5101 unlock_user(p
, arg1
, 0);
5105 #ifdef TARGET_NR_oldlstat
5106 case TARGET_NR_oldlstat
:
5109 case TARGET_NR_readlink
:
5112 p
= lock_user_string(arg1
);
5113 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
5115 ret
= -TARGET_EFAULT
;
5117 if (strncmp((const char *)p
, "/proc/self/exe", 14) == 0) {
5118 char real
[PATH_MAX
];
5119 temp
= realpath(exec_path
,real
);
5120 ret
= (temp
==NULL
) ? get_errno(-1) : strlen(real
) ;
5121 snprintf((char *)p2
, arg3
, "%s", real
);
5124 ret
= get_errno(readlink(path(p
), p2
, arg3
));
5126 unlock_user(p2
, arg2
, ret
);
5127 unlock_user(p
, arg1
, 0);
5130 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5131 case TARGET_NR_readlinkat
:
5134 p
= lock_user_string(arg2
);
5135 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
5137 ret
= -TARGET_EFAULT
;
5139 ret
= get_errno(sys_readlinkat(arg1
, path(p
), p2
, arg4
));
5140 unlock_user(p2
, arg3
, ret
);
5141 unlock_user(p
, arg2
, 0);
5145 #ifdef TARGET_NR_uselib
5146 case TARGET_NR_uselib
:
5149 #ifdef TARGET_NR_swapon
5150 case TARGET_NR_swapon
:
5151 if (!(p
= lock_user_string(arg1
)))
5153 ret
= get_errno(swapon(p
, arg2
));
5154 unlock_user(p
, arg1
, 0);
5157 case TARGET_NR_reboot
:
5159 #ifdef TARGET_NR_readdir
5160 case TARGET_NR_readdir
:
5163 #ifdef TARGET_NR_mmap
5164 case TARGET_NR_mmap
:
5165 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE)
5168 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
5169 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
5177 unlock_user(v
, arg1
, 0);
5178 ret
= get_errno(target_mmap(v1
, v2
, v3
,
5179 target_to_host_bitmask(v4
, mmap_flags_tbl
),
5183 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
5184 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
5190 #ifdef TARGET_NR_mmap2
5191 case TARGET_NR_mmap2
:
5193 #define MMAP_SHIFT 12
5195 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
5196 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
5198 arg6
<< MMAP_SHIFT
));
5201 case TARGET_NR_munmap
:
5202 ret
= get_errno(target_munmap(arg1
, arg2
));
5204 case TARGET_NR_mprotect
:
5205 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
5207 #ifdef TARGET_NR_mremap
5208 case TARGET_NR_mremap
:
5209 ret
= get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
5212 /* ??? msync/mlock/munlock are broken for softmmu. */
5213 #ifdef TARGET_NR_msync
5214 case TARGET_NR_msync
:
5215 ret
= get_errno(msync(g2h(arg1
), arg2
, arg3
));
5218 #ifdef TARGET_NR_mlock
5219 case TARGET_NR_mlock
:
5220 ret
= get_errno(mlock(g2h(arg1
), arg2
));
5223 #ifdef TARGET_NR_munlock
5224 case TARGET_NR_munlock
:
5225 ret
= get_errno(munlock(g2h(arg1
), arg2
));
5228 #ifdef TARGET_NR_mlockall
5229 case TARGET_NR_mlockall
:
5230 ret
= get_errno(mlockall(arg1
));
5233 #ifdef TARGET_NR_munlockall
5234 case TARGET_NR_munlockall
:
5235 ret
= get_errno(munlockall());
5238 case TARGET_NR_truncate
:
5239 if (!(p
= lock_user_string(arg1
)))
5241 ret
= get_errno(truncate(p
, arg2
));
5242 unlock_user(p
, arg1
, 0);
5244 case TARGET_NR_ftruncate
:
5245 ret
= get_errno(ftruncate(arg1
, arg2
));
5247 case TARGET_NR_fchmod
:
5248 ret
= get_errno(fchmod(arg1
, arg2
));
5250 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5251 case TARGET_NR_fchmodat
:
5252 if (!(p
= lock_user_string(arg2
)))
5254 ret
= get_errno(sys_fchmodat(arg1
, p
, arg3
));
5255 unlock_user(p
, arg2
, 0);
5258 case TARGET_NR_getpriority
:
5259 /* libc does special remapping of the return value of
5260 * sys_getpriority() so it's just easiest to call
5261 * sys_getpriority() directly rather than through libc. */
5262 ret
= sys_getpriority(arg1
, arg2
);
5264 case TARGET_NR_setpriority
:
5265 ret
= get_errno(setpriority(arg1
, arg2
, arg3
));
5267 #ifdef TARGET_NR_profil
5268 case TARGET_NR_profil
:
5271 case TARGET_NR_statfs
:
5272 if (!(p
= lock_user_string(arg1
)))
5274 ret
= get_errno(statfs(path(p
), &stfs
));
5275 unlock_user(p
, arg1
, 0);
5277 if (!is_error(ret
)) {
5278 struct target_statfs
*target_stfs
;
5280 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
5282 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
5283 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
5284 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
5285 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
5286 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
5287 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
5288 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
5289 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
5290 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
5291 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
5292 unlock_user_struct(target_stfs
, arg2
, 1);
5295 case TARGET_NR_fstatfs
:
5296 ret
= get_errno(fstatfs(arg1
, &stfs
));
5297 goto convert_statfs
;
5298 #ifdef TARGET_NR_statfs64
5299 case TARGET_NR_statfs64
:
5300 if (!(p
= lock_user_string(arg1
)))
5302 ret
= get_errno(statfs(path(p
), &stfs
));
5303 unlock_user(p
, arg1
, 0);
5305 if (!is_error(ret
)) {
5306 struct target_statfs64
*target_stfs
;
5308 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
5310 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
5311 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
5312 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
5313 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
5314 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
5315 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
5316 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
5317 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
5318 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
5319 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
5320 unlock_user_struct(target_stfs
, arg3
, 1);
5323 case TARGET_NR_fstatfs64
:
5324 ret
= get_errno(fstatfs(arg1
, &stfs
));
5325 goto convert_statfs64
;
5327 #ifdef TARGET_NR_ioperm
5328 case TARGET_NR_ioperm
:
5331 #ifdef TARGET_NR_socketcall
5332 case TARGET_NR_socketcall
:
5333 ret
= do_socketcall(arg1
, arg2
);
5336 #ifdef TARGET_NR_accept
5337 case TARGET_NR_accept
:
5338 ret
= do_accept(arg1
, arg2
, arg3
);
5341 #ifdef TARGET_NR_bind
5342 case TARGET_NR_bind
:
5343 ret
= do_bind(arg1
, arg2
, arg3
);
5346 #ifdef TARGET_NR_connect
5347 case TARGET_NR_connect
:
5348 ret
= do_connect(arg1
, arg2
, arg3
);
5351 #ifdef TARGET_NR_getpeername
5352 case TARGET_NR_getpeername
:
5353 ret
= do_getpeername(arg1
, arg2
, arg3
);
5356 #ifdef TARGET_NR_getsockname
5357 case TARGET_NR_getsockname
:
5358 ret
= do_getsockname(arg1
, arg2
, arg3
);
5361 #ifdef TARGET_NR_getsockopt
5362 case TARGET_NR_getsockopt
:
5363 ret
= do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
5366 #ifdef TARGET_NR_listen
5367 case TARGET_NR_listen
:
5368 ret
= get_errno(listen(arg1
, arg2
));
5371 #ifdef TARGET_NR_recv
5372 case TARGET_NR_recv
:
5373 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
5376 #ifdef TARGET_NR_recvfrom
5377 case TARGET_NR_recvfrom
:
5378 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
5381 #ifdef TARGET_NR_recvmsg
5382 case TARGET_NR_recvmsg
:
5383 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
5386 #ifdef TARGET_NR_send
5387 case TARGET_NR_send
:
5388 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
5391 #ifdef TARGET_NR_sendmsg
5392 case TARGET_NR_sendmsg
:
5393 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
5396 #ifdef TARGET_NR_sendto
5397 case TARGET_NR_sendto
:
5398 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
5401 #ifdef TARGET_NR_shutdown
5402 case TARGET_NR_shutdown
:
5403 ret
= get_errno(shutdown(arg1
, arg2
));
5406 #ifdef TARGET_NR_socket
5407 case TARGET_NR_socket
:
5408 ret
= do_socket(arg1
, arg2
, arg3
);
5411 #ifdef TARGET_NR_socketpair
5412 case TARGET_NR_socketpair
:
5413 ret
= do_socketpair(arg1
, arg2
, arg3
, arg4
);
5416 #ifdef TARGET_NR_setsockopt
5417 case TARGET_NR_setsockopt
:
5418 ret
= do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
5422 case TARGET_NR_syslog
:
5423 if (!(p
= lock_user_string(arg2
)))
5425 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
5426 unlock_user(p
, arg2
, 0);
5429 case TARGET_NR_setitimer
:
5431 struct itimerval value
, ovalue
, *pvalue
;
5435 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
5436 || copy_from_user_timeval(&pvalue
->it_value
,
5437 arg2
+ sizeof(struct target_timeval
)))
5442 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
5443 if (!is_error(ret
) && arg3
) {
5444 if (copy_to_user_timeval(arg3
,
5445 &ovalue
.it_interval
)
5446 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
5452 case TARGET_NR_getitimer
:
5454 struct itimerval value
;
5456 ret
= get_errno(getitimer(arg1
, &value
));
5457 if (!is_error(ret
) && arg2
) {
5458 if (copy_to_user_timeval(arg2
,
5460 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
5466 case TARGET_NR_stat
:
5467 if (!(p
= lock_user_string(arg1
)))
5469 ret
= get_errno(stat(path(p
), &st
));
5470 unlock_user(p
, arg1
, 0);
5472 case TARGET_NR_lstat
:
5473 if (!(p
= lock_user_string(arg1
)))
5475 ret
= get_errno(lstat(path(p
), &st
));
5476 unlock_user(p
, arg1
, 0);
5478 case TARGET_NR_fstat
:
5480 ret
= get_errno(fstat(arg1
, &st
));
5482 if (!is_error(ret
)) {
5483 struct target_stat
*target_st
;
5485 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
5487 __put_user(st
.st_dev
, &target_st
->st_dev
);
5488 __put_user(st
.st_ino
, &target_st
->st_ino
);
5489 __put_user(st
.st_mode
, &target_st
->st_mode
);
5490 __put_user(st
.st_uid
, &target_st
->st_uid
);
5491 __put_user(st
.st_gid
, &target_st
->st_gid
);
5492 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
5493 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
5494 __put_user(st
.st_size
, &target_st
->st_size
);
5495 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
5496 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
5497 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
5498 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
5499 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
5500 unlock_user_struct(target_st
, arg2
, 1);
5504 #ifdef TARGET_NR_olduname
5505 case TARGET_NR_olduname
:
5508 #ifdef TARGET_NR_iopl
5509 case TARGET_NR_iopl
:
5512 case TARGET_NR_vhangup
:
5513 ret
= get_errno(vhangup());
5515 #ifdef TARGET_NR_idle
5516 case TARGET_NR_idle
:
5519 #ifdef TARGET_NR_syscall
5520 case TARGET_NR_syscall
:
5521 ret
= do_syscall(cpu_env
,arg1
& 0xffff,arg2
,arg3
,arg4
,arg5
,arg6
,0);
5524 case TARGET_NR_wait4
:
5527 abi_long status_ptr
= arg2
;
5528 struct rusage rusage
, *rusage_ptr
;
5529 abi_ulong target_rusage
= arg4
;
5531 rusage_ptr
= &rusage
;
5534 ret
= get_errno(wait4(arg1
, &status
, arg3
, rusage_ptr
));
5535 if (!is_error(ret
)) {
5537 status
= host_to_target_waitstatus(status
);
5538 if (put_user_s32(status
, status_ptr
))
5542 host_to_target_rusage(target_rusage
, &rusage
);
5546 #ifdef TARGET_NR_swapoff
5547 case TARGET_NR_swapoff
:
5548 if (!(p
= lock_user_string(arg1
)))
5550 ret
= get_errno(swapoff(p
));
5551 unlock_user(p
, arg1
, 0);
5554 case TARGET_NR_sysinfo
:
5556 struct target_sysinfo
*target_value
;
5557 struct sysinfo value
;
5558 ret
= get_errno(sysinfo(&value
));
5559 if (!is_error(ret
) && arg1
)
5561 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
5563 __put_user(value
.uptime
, &target_value
->uptime
);
5564 __put_user(value
.loads
[0], &target_value
->loads
[0]);
5565 __put_user(value
.loads
[1], &target_value
->loads
[1]);
5566 __put_user(value
.loads
[2], &target_value
->loads
[2]);
5567 __put_user(value
.totalram
, &target_value
->totalram
);
5568 __put_user(value
.freeram
, &target_value
->freeram
);
5569 __put_user(value
.sharedram
, &target_value
->sharedram
);
5570 __put_user(value
.bufferram
, &target_value
->bufferram
);
5571 __put_user(value
.totalswap
, &target_value
->totalswap
);
5572 __put_user(value
.freeswap
, &target_value
->freeswap
);
5573 __put_user(value
.procs
, &target_value
->procs
);
5574 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
5575 __put_user(value
.freehigh
, &target_value
->freehigh
);
5576 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
5577 unlock_user_struct(target_value
, arg1
, 1);
5581 #ifdef TARGET_NR_ipc
5583 ret
= do_ipc(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
5586 #ifdef TARGET_NR_semget
5587 case TARGET_NR_semget
:
5588 ret
= get_errno(semget(arg1
, arg2
, arg3
));
5591 #ifdef TARGET_NR_semop
5592 case TARGET_NR_semop
:
5593 ret
= get_errno(do_semop(arg1
, arg2
, arg3
));
5596 #ifdef TARGET_NR_semctl
5597 case TARGET_NR_semctl
:
5598 ret
= do_semctl(arg1
, arg2
, arg3
, (union target_semun
)(abi_ulong
)arg4
);
5601 #ifdef TARGET_NR_msgctl
5602 case TARGET_NR_msgctl
:
5603 ret
= do_msgctl(arg1
, arg2
, arg3
);
5606 #ifdef TARGET_NR_msgget
5607 case TARGET_NR_msgget
:
5608 ret
= get_errno(msgget(arg1
, arg2
));
5611 #ifdef TARGET_NR_msgrcv
5612 case TARGET_NR_msgrcv
:
5613 ret
= do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
5616 #ifdef TARGET_NR_msgsnd
5617 case TARGET_NR_msgsnd
:
5618 ret
= do_msgsnd(arg1
, arg2
, arg3
, arg4
);
5621 #ifdef TARGET_NR_shmget
5622 case TARGET_NR_shmget
:
5623 ret
= get_errno(shmget(arg1
, arg2
, arg3
));
5626 #ifdef TARGET_NR_shmctl
5627 case TARGET_NR_shmctl
:
5628 ret
= do_shmctl(arg1
, arg2
, arg3
);
5631 #ifdef TARGET_NR_shmat
5632 case TARGET_NR_shmat
:
5633 ret
= do_shmat(arg1
, arg2
, arg3
);
5636 #ifdef TARGET_NR_shmdt
5637 case TARGET_NR_shmdt
:
5638 ret
= do_shmdt(arg1
);
5641 case TARGET_NR_fsync
:
5642 ret
= get_errno(fsync(arg1
));
5644 case TARGET_NR_clone
:
5645 #if defined(TARGET_SH4)
5646 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
5647 #elif defined(TARGET_CRIS)
5648 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg4
, arg5
));
5650 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
5653 #ifdef __NR_exit_group
5654 /* new thread calls */
5655 case TARGET_NR_exit_group
:
5659 gdb_exit(cpu_env
, arg1
);
5660 ret
= get_errno(exit_group(arg1
));
5663 case TARGET_NR_setdomainname
:
5664 if (!(p
= lock_user_string(arg1
)))
5666 ret
= get_errno(setdomainname(p
, arg2
));
5667 unlock_user(p
, arg1
, 0);
5669 case TARGET_NR_uname
:
5670 /* no need to transcode because we use the linux syscall */
5672 struct new_utsname
* buf
;
5674 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
5676 ret
= get_errno(sys_uname(buf
));
5677 if (!is_error(ret
)) {
5678 /* Overrite the native machine name with whatever is being
5680 strcpy (buf
->machine
, UNAME_MACHINE
);
5681 /* Allow the user to override the reported release. */
5682 if (qemu_uname_release
&& *qemu_uname_release
)
5683 strcpy (buf
->release
, qemu_uname_release
);
5685 unlock_user_struct(buf
, arg1
, 1);
5689 case TARGET_NR_modify_ldt
:
5690 ret
= do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
5692 #if !defined(TARGET_X86_64)
5693 case TARGET_NR_vm86old
:
5695 case TARGET_NR_vm86
:
5696 ret
= do_vm86(cpu_env
, arg1
, arg2
);
5700 case TARGET_NR_adjtimex
:
5702 #ifdef TARGET_NR_create_module
5703 case TARGET_NR_create_module
:
5705 case TARGET_NR_init_module
:
5706 case TARGET_NR_delete_module
:
5707 #ifdef TARGET_NR_get_kernel_syms
5708 case TARGET_NR_get_kernel_syms
:
5711 case TARGET_NR_quotactl
:
5713 case TARGET_NR_getpgid
:
5714 ret
= get_errno(getpgid(arg1
));
5716 case TARGET_NR_fchdir
:
5717 ret
= get_errno(fchdir(arg1
));
5719 #ifdef TARGET_NR_bdflush /* not on x86_64 */
5720 case TARGET_NR_bdflush
:
5723 #ifdef TARGET_NR_sysfs
5724 case TARGET_NR_sysfs
:
5727 case TARGET_NR_personality
:
5728 ret
= get_errno(personality(arg1
));
5730 #ifdef TARGET_NR_afs_syscall
5731 case TARGET_NR_afs_syscall
:
5734 #ifdef TARGET_NR__llseek /* Not on alpha */
5735 case TARGET_NR__llseek
:
5737 #if defined (__x86_64__)
5738 ret
= get_errno(lseek(arg1
, ((uint64_t )arg2
<< 32) | arg3
, arg5
));
5739 if (put_user_s64(ret
, arg4
))
5743 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
5744 if (put_user_s64(res
, arg4
))
5750 case TARGET_NR_getdents
:
5751 #if TARGET_ABI_BITS != 32
5753 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
5755 struct target_dirent
*target_dirp
;
5756 struct linux_dirent
*dirp
;
5757 abi_long count
= arg3
;
5759 dirp
= malloc(count
);
5761 ret
= -TARGET_ENOMEM
;
5765 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
5766 if (!is_error(ret
)) {
5767 struct linux_dirent
*de
;
5768 struct target_dirent
*tde
;
5770 int reclen
, treclen
;
5771 int count1
, tnamelen
;
5775 if (!(target_dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
5779 reclen
= de
->d_reclen
;
5780 treclen
= reclen
- (2 * (sizeof(long) - sizeof(abi_long
)));
5781 tde
->d_reclen
= tswap16(treclen
);
5782 tde
->d_ino
= tswapl(de
->d_ino
);
5783 tde
->d_off
= tswapl(de
->d_off
);
5784 tnamelen
= treclen
- (2 * sizeof(abi_long
) + 2);
5787 /* XXX: may not be correct */
5788 pstrcpy(tde
->d_name
, tnamelen
, de
->d_name
);
5789 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
5791 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
5795 unlock_user(target_dirp
, arg2
, ret
);
5801 struct linux_dirent
*dirp
;
5802 abi_long count
= arg3
;
5804 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
5806 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
5807 if (!is_error(ret
)) {
5808 struct linux_dirent
*de
;
5813 reclen
= de
->d_reclen
;
5816 de
->d_reclen
= tswap16(reclen
);
5817 tswapls(&de
->d_ino
);
5818 tswapls(&de
->d_off
);
5819 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
5823 unlock_user(dirp
, arg2
, ret
);
5827 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
5828 case TARGET_NR_getdents64
:
5830 struct linux_dirent64
*dirp
;
5831 abi_long count
= arg3
;
5832 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
5834 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
5835 if (!is_error(ret
)) {
5836 struct linux_dirent64
*de
;
5841 reclen
= de
->d_reclen
;
5844 de
->d_reclen
= tswap16(reclen
);
5845 tswap64s((uint64_t *)&de
->d_ino
);
5846 tswap64s((uint64_t *)&de
->d_off
);
5847 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
5851 unlock_user(dirp
, arg2
, ret
);
5854 #endif /* TARGET_NR_getdents64 */
5855 #ifdef TARGET_NR__newselect
5856 case TARGET_NR__newselect
:
5857 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
5860 #ifdef TARGET_NR_poll
5861 case TARGET_NR_poll
:
5863 struct target_pollfd
*target_pfd
;
5864 unsigned int nfds
= arg2
;
5869 target_pfd
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_pollfd
) * nfds
, 1);
5872 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
5873 for(i
= 0; i
< nfds
; i
++) {
5874 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
5875 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
5877 ret
= get_errno(poll(pfd
, nfds
, timeout
));
5878 if (!is_error(ret
)) {
5879 for(i
= 0; i
< nfds
; i
++) {
5880 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
5882 ret
+= nfds
* (sizeof(struct target_pollfd
)
5883 - sizeof(struct pollfd
));
5885 unlock_user(target_pfd
, arg1
, ret
);
5889 case TARGET_NR_flock
:
5890 /* NOTE: the flock constant seems to be the same for every
5892 ret
= get_errno(flock(arg1
, arg2
));
5894 case TARGET_NR_readv
:
5899 vec
= alloca(count
* sizeof(struct iovec
));
5900 if (lock_iovec(VERIFY_WRITE
, vec
, arg2
, count
, 0) < 0)
5902 ret
= get_errno(readv(arg1
, vec
, count
));
5903 unlock_iovec(vec
, arg2
, count
, 1);
5906 case TARGET_NR_writev
:
5911 vec
= alloca(count
* sizeof(struct iovec
));
5912 if (lock_iovec(VERIFY_READ
, vec
, arg2
, count
, 1) < 0)
5914 ret
= get_errno(writev(arg1
, vec
, count
));
5915 unlock_iovec(vec
, arg2
, count
, 0);
5918 case TARGET_NR_getsid
:
5919 ret
= get_errno(getsid(arg1
));
5921 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
5922 case TARGET_NR_fdatasync
:
5923 ret
= get_errno(fdatasync(arg1
));
5926 case TARGET_NR__sysctl
:
5927 /* We don't implement this, but ENOTDIR is always a safe
5929 ret
= -TARGET_ENOTDIR
;
5931 case TARGET_NR_sched_setparam
:
5933 struct sched_param
*target_schp
;
5934 struct sched_param schp
;
5936 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1))
5938 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
5939 unlock_user_struct(target_schp
, arg2
, 0);
5940 ret
= get_errno(sched_setparam(arg1
, &schp
));
5943 case TARGET_NR_sched_getparam
:
5945 struct sched_param
*target_schp
;
5946 struct sched_param schp
;
5947 ret
= get_errno(sched_getparam(arg1
, &schp
));
5948 if (!is_error(ret
)) {
5949 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0))
5951 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
5952 unlock_user_struct(target_schp
, arg2
, 1);
5956 case TARGET_NR_sched_setscheduler
:
5958 struct sched_param
*target_schp
;
5959 struct sched_param schp
;
5960 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1))
5962 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
5963 unlock_user_struct(target_schp
, arg3
, 0);
5964 ret
= get_errno(sched_setscheduler(arg1
, arg2
, &schp
));
5967 case TARGET_NR_sched_getscheduler
:
5968 ret
= get_errno(sched_getscheduler(arg1
));
5970 case TARGET_NR_sched_yield
:
5971 ret
= get_errno(sched_yield());
5973 case TARGET_NR_sched_get_priority_max
:
5974 ret
= get_errno(sched_get_priority_max(arg1
));
5976 case TARGET_NR_sched_get_priority_min
:
5977 ret
= get_errno(sched_get_priority_min(arg1
));
5979 case TARGET_NR_sched_rr_get_interval
:
5982 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
5983 if (!is_error(ret
)) {
5984 host_to_target_timespec(arg2
, &ts
);
5988 case TARGET_NR_nanosleep
:
5990 struct timespec req
, rem
;
5991 target_to_host_timespec(&req
, arg1
);
5992 ret
= get_errno(nanosleep(&req
, &rem
));
5993 if (is_error(ret
) && arg2
) {
5994 host_to_target_timespec(arg2
, &rem
);
5998 #ifdef TARGET_NR_query_module
5999 case TARGET_NR_query_module
:
6002 #ifdef TARGET_NR_nfsservctl
6003 case TARGET_NR_nfsservctl
:
6006 case TARGET_NR_prctl
:
6009 case PR_GET_PDEATHSIG
:
6012 ret
= get_errno(prctl(arg1
, &deathsig
, arg3
, arg4
, arg5
));
6013 if (!is_error(ret
) && arg2
6014 && put_user_ual(deathsig
, arg2
))
6019 ret
= get_errno(prctl(arg1
, arg2
, arg3
, arg4
, arg5
));
6023 #ifdef TARGET_NR_arch_prctl
6024 case TARGET_NR_arch_prctl
:
6025 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
6026 ret
= do_arch_prctl(cpu_env
, arg1
, arg2
);
6032 #ifdef TARGET_NR_pread
6033 case TARGET_NR_pread
:
6035 if (((CPUARMState
*)cpu_env
)->eabi
)
6038 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
6040 ret
= get_errno(pread(arg1
, p
, arg3
, arg4
));
6041 unlock_user(p
, arg2
, ret
);
6043 case TARGET_NR_pwrite
:
6045 if (((CPUARMState
*)cpu_env
)->eabi
)
6048 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
6050 ret
= get_errno(pwrite(arg1
, p
, arg3
, arg4
));
6051 unlock_user(p
, arg2
, 0);
6054 #ifdef TARGET_NR_pread64
6055 case TARGET_NR_pread64
:
6056 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
6058 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
6059 unlock_user(p
, arg2
, ret
);
6061 case TARGET_NR_pwrite64
:
6062 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
6064 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
6065 unlock_user(p
, arg2
, 0);
6068 case TARGET_NR_getcwd
:
6069 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
6071 ret
= get_errno(sys_getcwd1(p
, arg2
));
6072 unlock_user(p
, arg1
, ret
);
6074 case TARGET_NR_capget
:
6076 case TARGET_NR_capset
:
6078 case TARGET_NR_sigaltstack
:
6079 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
6080 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
6081 ret
= do_sigaltstack(arg1
, arg2
, get_sp_from_cpustate((CPUState
*)cpu_env
));
6086 case TARGET_NR_sendfile
:
6088 #ifdef TARGET_NR_getpmsg
6089 case TARGET_NR_getpmsg
:
6092 #ifdef TARGET_NR_putpmsg
6093 case TARGET_NR_putpmsg
:
6096 #ifdef TARGET_NR_vfork
6097 case TARGET_NR_vfork
:
6098 ret
= get_errno(do_fork(cpu_env
, CLONE_VFORK
| CLONE_VM
| SIGCHLD
,
6102 #ifdef TARGET_NR_ugetrlimit
6103 case TARGET_NR_ugetrlimit
:
6106 ret
= get_errno(getrlimit(arg1
, &rlim
));
6107 if (!is_error(ret
)) {
6108 struct target_rlimit
*target_rlim
;
6109 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
6111 target_rlim
->rlim_cur
= tswapl(rlim
.rlim_cur
);
6112 target_rlim
->rlim_max
= tswapl(rlim
.rlim_max
);
6113 unlock_user_struct(target_rlim
, arg2
, 1);
6118 #ifdef TARGET_NR_truncate64
6119 case TARGET_NR_truncate64
:
6120 if (!(p
= lock_user_string(arg1
)))
6122 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
6123 unlock_user(p
, arg1
, 0);
6126 #ifdef TARGET_NR_ftruncate64
6127 case TARGET_NR_ftruncate64
:
6128 ret
= target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
6131 #ifdef TARGET_NR_stat64
6132 case TARGET_NR_stat64
:
6133 if (!(p
= lock_user_string(arg1
)))
6135 ret
= get_errno(stat(path(p
), &st
));
6136 unlock_user(p
, arg1
, 0);
6138 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
6141 #ifdef TARGET_NR_lstat64
6142 case TARGET_NR_lstat64
:
6143 if (!(p
= lock_user_string(arg1
)))
6145 ret
= get_errno(lstat(path(p
), &st
));
6146 unlock_user(p
, arg1
, 0);
6148 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
6151 #ifdef TARGET_NR_fstat64
6152 case TARGET_NR_fstat64
:
6153 ret
= get_errno(fstat(arg1
, &st
));
6155 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
6158 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6159 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6160 #ifdef TARGET_NR_fstatat64
6161 case TARGET_NR_fstatat64
:
6163 #ifdef TARGET_NR_newfstatat
6164 case TARGET_NR_newfstatat
:
6166 if (!(p
= lock_user_string(arg2
)))
6168 #ifdef __NR_fstatat64
6169 ret
= get_errno(sys_fstatat64(arg1
, path(p
), &st
, arg4
));
6171 ret
= get_errno(sys_newfstatat(arg1
, path(p
), &st
, arg4
));
6174 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
6178 case TARGET_NR_lchown
:
6179 if (!(p
= lock_user_string(arg1
)))
6181 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
6182 unlock_user(p
, arg1
, 0);
6184 case TARGET_NR_getuid
:
6185 ret
= get_errno(high2lowuid(getuid()));
6187 case TARGET_NR_getgid
:
6188 ret
= get_errno(high2lowgid(getgid()));
6190 case TARGET_NR_geteuid
:
6191 ret
= get_errno(high2lowuid(geteuid()));
6193 case TARGET_NR_getegid
:
6194 ret
= get_errno(high2lowgid(getegid()));
6196 case TARGET_NR_setreuid
:
6197 ret
= get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
6199 case TARGET_NR_setregid
:
6200 ret
= get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
6202 case TARGET_NR_getgroups
:
6204 int gidsetsize
= arg1
;
6205 uint16_t *target_grouplist
;
6209 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
6210 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
6211 if (gidsetsize
== 0)
6213 if (!is_error(ret
)) {
6214 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 2, 0);
6215 if (!target_grouplist
)
6217 for(i
= 0;i
< ret
; i
++)
6218 target_grouplist
[i
] = tswap16(grouplist
[i
]);
6219 unlock_user(target_grouplist
, arg2
, gidsetsize
* 2);
6223 case TARGET_NR_setgroups
:
6225 int gidsetsize
= arg1
;
6226 uint16_t *target_grouplist
;
6230 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
6231 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 2, 1);
6232 if (!target_grouplist
) {
6233 ret
= -TARGET_EFAULT
;
6236 for(i
= 0;i
< gidsetsize
; i
++)
6237 grouplist
[i
] = tswap16(target_grouplist
[i
]);
6238 unlock_user(target_grouplist
, arg2
, 0);
6239 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
6242 case TARGET_NR_fchown
:
6243 ret
= get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
6245 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6246 case TARGET_NR_fchownat
:
6247 if (!(p
= lock_user_string(arg2
)))
6249 ret
= get_errno(sys_fchownat(arg1
, p
, low2highuid(arg3
), low2highgid(arg4
), arg5
));
6250 unlock_user(p
, arg2
, 0);
6253 #ifdef TARGET_NR_setresuid
6254 case TARGET_NR_setresuid
:
6255 ret
= get_errno(setresuid(low2highuid(arg1
),
6257 low2highuid(arg3
)));
6260 #ifdef TARGET_NR_getresuid
6261 case TARGET_NR_getresuid
:
6263 uid_t ruid
, euid
, suid
;
6264 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
6265 if (!is_error(ret
)) {
6266 if (put_user_u16(high2lowuid(ruid
), arg1
)
6267 || put_user_u16(high2lowuid(euid
), arg2
)
6268 || put_user_u16(high2lowuid(suid
), arg3
))
6274 #ifdef TARGET_NR_getresgid
6275 case TARGET_NR_setresgid
:
6276 ret
= get_errno(setresgid(low2highgid(arg1
),
6278 low2highgid(arg3
)));
6281 #ifdef TARGET_NR_getresgid
6282 case TARGET_NR_getresgid
:
6284 gid_t rgid
, egid
, sgid
;
6285 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
6286 if (!is_error(ret
)) {
6287 if (put_user_u16(high2lowgid(rgid
), arg1
)
6288 || put_user_u16(high2lowgid(egid
), arg2
)
6289 || put_user_u16(high2lowgid(sgid
), arg3
))
6295 case TARGET_NR_chown
:
6296 if (!(p
= lock_user_string(arg1
)))
6298 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
6299 unlock_user(p
, arg1
, 0);
6301 case TARGET_NR_setuid
:
6302 ret
= get_errno(setuid(low2highuid(arg1
)));
6304 case TARGET_NR_setgid
:
6305 ret
= get_errno(setgid(low2highgid(arg1
)));
6307 case TARGET_NR_setfsuid
:
6308 ret
= get_errno(setfsuid(arg1
));
6310 case TARGET_NR_setfsgid
:
6311 ret
= get_errno(setfsgid(arg1
));
6313 #endif /* USE_UID16 */
6315 #ifdef TARGET_NR_lchown32
6316 case TARGET_NR_lchown32
:
6317 if (!(p
= lock_user_string(arg1
)))
6319 ret
= get_errno(lchown(p
, arg2
, arg3
));
6320 unlock_user(p
, arg1
, 0);
6323 #ifdef TARGET_NR_getuid32
6324 case TARGET_NR_getuid32
:
6325 ret
= get_errno(getuid());
6329 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6330 /* Alpha specific */
6331 case TARGET_NR_getxuid
:
6335 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=euid
;
6337 ret
= get_errno(getuid());
6340 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6341 /* Alpha specific */
6342 case TARGET_NR_getxgid
:
6346 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=egid
;
6348 ret
= get_errno(getgid());
6352 #ifdef TARGET_NR_getgid32
6353 case TARGET_NR_getgid32
:
6354 ret
= get_errno(getgid());
6357 #ifdef TARGET_NR_geteuid32
6358 case TARGET_NR_geteuid32
:
6359 ret
= get_errno(geteuid());
6362 #ifdef TARGET_NR_getegid32
6363 case TARGET_NR_getegid32
:
6364 ret
= get_errno(getegid());
6367 #ifdef TARGET_NR_setreuid32
6368 case TARGET_NR_setreuid32
:
6369 ret
= get_errno(setreuid(arg1
, arg2
));
6372 #ifdef TARGET_NR_setregid32
6373 case TARGET_NR_setregid32
:
6374 ret
= get_errno(setregid(arg1
, arg2
));
6377 #ifdef TARGET_NR_getgroups32
6378 case TARGET_NR_getgroups32
:
6380 int gidsetsize
= arg1
;
6381 uint32_t *target_grouplist
;
6385 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
6386 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
6387 if (gidsetsize
== 0)
6389 if (!is_error(ret
)) {
6390 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
6391 if (!target_grouplist
) {
6392 ret
= -TARGET_EFAULT
;
6395 for(i
= 0;i
< ret
; i
++)
6396 target_grouplist
[i
] = tswap32(grouplist
[i
]);
6397 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
6402 #ifdef TARGET_NR_setgroups32
6403 case TARGET_NR_setgroups32
:
6405 int gidsetsize
= arg1
;
6406 uint32_t *target_grouplist
;
6410 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
6411 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
6412 if (!target_grouplist
) {
6413 ret
= -TARGET_EFAULT
;
6416 for(i
= 0;i
< gidsetsize
; i
++)
6417 grouplist
[i
] = tswap32(target_grouplist
[i
]);
6418 unlock_user(target_grouplist
, arg2
, 0);
6419 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
6423 #ifdef TARGET_NR_fchown32
6424 case TARGET_NR_fchown32
:
6425 ret
= get_errno(fchown(arg1
, arg2
, arg3
));
6428 #ifdef TARGET_NR_setresuid32
6429 case TARGET_NR_setresuid32
:
6430 ret
= get_errno(setresuid(arg1
, arg2
, arg3
));
6433 #ifdef TARGET_NR_getresuid32
6434 case TARGET_NR_getresuid32
:
6436 uid_t ruid
, euid
, suid
;
6437 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
6438 if (!is_error(ret
)) {
6439 if (put_user_u32(ruid
, arg1
)
6440 || put_user_u32(euid
, arg2
)
6441 || put_user_u32(suid
, arg3
))
6447 #ifdef TARGET_NR_setresgid32
6448 case TARGET_NR_setresgid32
:
6449 ret
= get_errno(setresgid(arg1
, arg2
, arg3
));
6452 #ifdef TARGET_NR_getresgid32
6453 case TARGET_NR_getresgid32
:
6455 gid_t rgid
, egid
, sgid
;
6456 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
6457 if (!is_error(ret
)) {
6458 if (put_user_u32(rgid
, arg1
)
6459 || put_user_u32(egid
, arg2
)
6460 || put_user_u32(sgid
, arg3
))
6466 #ifdef TARGET_NR_chown32
6467 case TARGET_NR_chown32
:
6468 if (!(p
= lock_user_string(arg1
)))
6470 ret
= get_errno(chown(p
, arg2
, arg3
));
6471 unlock_user(p
, arg1
, 0);
6474 #ifdef TARGET_NR_setuid32
6475 case TARGET_NR_setuid32
:
6476 ret
= get_errno(setuid(arg1
));
6479 #ifdef TARGET_NR_setgid32
6480 case TARGET_NR_setgid32
:
6481 ret
= get_errno(setgid(arg1
));
6484 #ifdef TARGET_NR_setfsuid32
6485 case TARGET_NR_setfsuid32
:
6486 ret
= get_errno(setfsuid(arg1
));
6489 #ifdef TARGET_NR_setfsgid32
6490 case TARGET_NR_setfsgid32
:
6491 ret
= get_errno(setfsgid(arg1
));
6495 case TARGET_NR_pivot_root
:
6497 #ifdef TARGET_NR_mincore
6498 case TARGET_NR_mincore
:
6501 ret
= -TARGET_EFAULT
;
6502 if (!(a
= lock_user(VERIFY_READ
, arg1
,arg2
, 0)))
6504 if (!(p
= lock_user_string(arg3
)))
6506 ret
= get_errno(mincore(a
, arg2
, p
));
6507 unlock_user(p
, arg3
, ret
);
6509 unlock_user(a
, arg1
, 0);
6513 #ifdef TARGET_NR_arm_fadvise64_64
6514 case TARGET_NR_arm_fadvise64_64
:
6517 * arm_fadvise64_64 looks like fadvise64_64 but
6518 * with different argument order
6526 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64)
6527 #ifdef TARGET_NR_fadvise64_64
6528 case TARGET_NR_fadvise64_64
:
6530 /* This is a hint, so ignoring and returning success is ok. */
6534 #ifdef TARGET_NR_madvise
6535 case TARGET_NR_madvise
:
6536 /* A straight passthrough may not be safe because qemu sometimes
6537 turns private flie-backed mappings into anonymous mappings.
6538 This will break MADV_DONTNEED.
6539 This is a hint, so ignoring and returning success is ok. */
6543 #if TARGET_ABI_BITS == 32
6544 case TARGET_NR_fcntl64
:
6548 struct target_flock64
*target_fl
;
6550 struct target_eabi_flock64
*target_efl
;
6553 cmd
= target_to_host_fcntl_cmd(arg2
);
6554 if (cmd
== -TARGET_EINVAL
)
6558 case TARGET_F_GETLK64
:
6560 if (((CPUARMState
*)cpu_env
)->eabi
) {
6561 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
6563 fl
.l_type
= tswap16(target_efl
->l_type
);
6564 fl
.l_whence
= tswap16(target_efl
->l_whence
);
6565 fl
.l_start
= tswap64(target_efl
->l_start
);
6566 fl
.l_len
= tswap64(target_efl
->l_len
);
6567 fl
.l_pid
= tswapl(target_efl
->l_pid
);
6568 unlock_user_struct(target_efl
, arg3
, 0);
6572 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
6574 fl
.l_type
= tswap16(target_fl
->l_type
);
6575 fl
.l_whence
= tswap16(target_fl
->l_whence
);
6576 fl
.l_start
= tswap64(target_fl
->l_start
);
6577 fl
.l_len
= tswap64(target_fl
->l_len
);
6578 fl
.l_pid
= tswapl(target_fl
->l_pid
);
6579 unlock_user_struct(target_fl
, arg3
, 0);
6581 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
6584 if (((CPUARMState
*)cpu_env
)->eabi
) {
6585 if (!lock_user_struct(VERIFY_WRITE
, target_efl
, arg3
, 0))
6587 target_efl
->l_type
= tswap16(fl
.l_type
);
6588 target_efl
->l_whence
= tswap16(fl
.l_whence
);
6589 target_efl
->l_start
= tswap64(fl
.l_start
);
6590 target_efl
->l_len
= tswap64(fl
.l_len
);
6591 target_efl
->l_pid
= tswapl(fl
.l_pid
);
6592 unlock_user_struct(target_efl
, arg3
, 1);
6596 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg3
, 0))
6598 target_fl
->l_type
= tswap16(fl
.l_type
);
6599 target_fl
->l_whence
= tswap16(fl
.l_whence
);
6600 target_fl
->l_start
= tswap64(fl
.l_start
);
6601 target_fl
->l_len
= tswap64(fl
.l_len
);
6602 target_fl
->l_pid
= tswapl(fl
.l_pid
);
6603 unlock_user_struct(target_fl
, arg3
, 1);
6608 case TARGET_F_SETLK64
:
6609 case TARGET_F_SETLKW64
:
6611 if (((CPUARMState
*)cpu_env
)->eabi
) {
6612 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
6614 fl
.l_type
= tswap16(target_efl
->l_type
);
6615 fl
.l_whence
= tswap16(target_efl
->l_whence
);
6616 fl
.l_start
= tswap64(target_efl
->l_start
);
6617 fl
.l_len
= tswap64(target_efl
->l_len
);
6618 fl
.l_pid
= tswapl(target_efl
->l_pid
);
6619 unlock_user_struct(target_efl
, arg3
, 0);
6623 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
6625 fl
.l_type
= tswap16(target_fl
->l_type
);
6626 fl
.l_whence
= tswap16(target_fl
->l_whence
);
6627 fl
.l_start
= tswap64(target_fl
->l_start
);
6628 fl
.l_len
= tswap64(target_fl
->l_len
);
6629 fl
.l_pid
= tswapl(target_fl
->l_pid
);
6630 unlock_user_struct(target_fl
, arg3
, 0);
6632 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
6635 ret
= do_fcntl(arg1
, arg2
, arg3
);
6641 #ifdef TARGET_NR_cacheflush
6642 case TARGET_NR_cacheflush
:
6643 /* self-modifying code is handled automatically, so nothing needed */
6647 #ifdef TARGET_NR_security
6648 case TARGET_NR_security
:
6651 #ifdef TARGET_NR_getpagesize
6652 case TARGET_NR_getpagesize
:
6653 ret
= TARGET_PAGE_SIZE
;
6656 case TARGET_NR_gettid
:
6657 ret
= get_errno(gettid());
6659 #ifdef TARGET_NR_readahead
6660 case TARGET_NR_readahead
:
6661 #if TARGET_ABI_BITS == 32
6663 if (((CPUARMState
*)cpu_env
)->eabi
)
6670 ret
= get_errno(readahead(arg1
, ((off64_t
)arg3
<< 32) | arg2
, arg4
));
6672 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
6676 #ifdef TARGET_NR_setxattr
6677 case TARGET_NR_setxattr
:
6678 case TARGET_NR_lsetxattr
:
6679 case TARGET_NR_fsetxattr
:
6680 case TARGET_NR_getxattr
:
6681 case TARGET_NR_lgetxattr
:
6682 case TARGET_NR_fgetxattr
:
6683 case TARGET_NR_listxattr
:
6684 case TARGET_NR_llistxattr
:
6685 case TARGET_NR_flistxattr
:
6686 case TARGET_NR_removexattr
:
6687 case TARGET_NR_lremovexattr
:
6688 case TARGET_NR_fremovexattr
:
6689 ret
= -TARGET_EOPNOTSUPP
;
6692 #ifdef TARGET_NR_set_thread_area
6693 case TARGET_NR_set_thread_area
:
6694 #if defined(TARGET_MIPS)
6695 ((CPUMIPSState
*) cpu_env
)->tls_value
= arg1
;
6698 #elif defined(TARGET_CRIS)
6700 ret
= -TARGET_EINVAL
;
6702 ((CPUCRISState
*) cpu_env
)->pregs
[PR_PID
] = arg1
;
6706 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
6707 ret
= do_set_thread_area(cpu_env
, arg1
);
6710 goto unimplemented_nowarn
;
6713 #ifdef TARGET_NR_get_thread_area
6714 case TARGET_NR_get_thread_area
:
6715 #if defined(TARGET_I386) && defined(TARGET_ABI32)
6716 ret
= do_get_thread_area(cpu_env
, arg1
);
6718 goto unimplemented_nowarn
;
6721 #ifdef TARGET_NR_getdomainname
6722 case TARGET_NR_getdomainname
:
6723 goto unimplemented_nowarn
;
6726 #ifdef TARGET_NR_clock_gettime
6727 case TARGET_NR_clock_gettime
:
6730 ret
= get_errno(clock_gettime(arg1
, &ts
));
6731 if (!is_error(ret
)) {
6732 host_to_target_timespec(arg2
, &ts
);
6737 #ifdef TARGET_NR_clock_getres
6738 case TARGET_NR_clock_getres
:
6741 ret
= get_errno(clock_getres(arg1
, &ts
));
6742 if (!is_error(ret
)) {
6743 host_to_target_timespec(arg2
, &ts
);
6748 #ifdef TARGET_NR_clock_nanosleep
6749 case TARGET_NR_clock_nanosleep
:
6752 target_to_host_timespec(&ts
, arg3
);
6753 ret
= get_errno(clock_nanosleep(arg1
, arg2
, &ts
, arg4
? &ts
: NULL
));
6755 host_to_target_timespec(arg4
, &ts
);
6760 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
6761 case TARGET_NR_set_tid_address
:
6762 ret
= get_errno(set_tid_address((int *)g2h(arg1
)));
6766 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
6767 case TARGET_NR_tkill
:
6768 ret
= get_errno(sys_tkill((int)arg1
, target_to_host_signal(arg2
)));
6772 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
6773 case TARGET_NR_tgkill
:
6774 ret
= get_errno(sys_tgkill((int)arg1
, (int)arg2
,
6775 target_to_host_signal(arg3
)));
6779 #ifdef TARGET_NR_set_robust_list
6780 case TARGET_NR_set_robust_list
:
6781 goto unimplemented_nowarn
;
6784 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
6785 case TARGET_NR_utimensat
:
6787 struct timespec
*tsp
, ts
[2];
6791 target_to_host_timespec(ts
, arg3
);
6792 target_to_host_timespec(ts
+1, arg3
+sizeof(struct target_timespec
));
6796 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
6798 if (!(p
= lock_user_string(arg2
))) {
6799 ret
= -TARGET_EFAULT
;
6802 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
6803 unlock_user(p
, arg2
, 0);
6808 #if defined(USE_NPTL)
6809 case TARGET_NR_futex
:
6810 ret
= do_futex(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
6813 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
6814 case TARGET_NR_inotify_init
:
6815 ret
= get_errno(sys_inotify_init());
6818 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
6819 case TARGET_NR_inotify_add_watch
:
6820 p
= lock_user_string(arg2
);
6821 ret
= get_errno(sys_inotify_add_watch(arg1
, path(p
), arg3
));
6822 unlock_user(p
, arg2
, 0);
6825 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
6826 case TARGET_NR_inotify_rm_watch
:
6827 ret
= get_errno(sys_inotify_rm_watch(arg1
, arg2
));
6831 #ifdef TARGET_NR_mq_open
6832 case TARGET_NR_mq_open
:
6834 struct mq_attr posix_mq_attr
;
6836 p
= lock_user_string(arg1
- 1);
6838 copy_from_user_mq_attr (&posix_mq_attr
, arg4
);
6839 ret
= get_errno(mq_open(p
, arg2
, arg3
, &posix_mq_attr
));
6840 unlock_user (p
, arg1
, 0);
6844 case TARGET_NR_mq_unlink
:
6845 p
= lock_user_string(arg1
- 1);
6846 ret
= get_errno(mq_unlink(p
));
6847 unlock_user (p
, arg1
, 0);
6850 case TARGET_NR_mq_timedsend
:
6854 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
6856 target_to_host_timespec(&ts
, arg5
);
6857 ret
= get_errno(mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
6858 host_to_target_timespec(arg5
, &ts
);
6861 ret
= get_errno(mq_send(arg1
, p
, arg3
, arg4
));
6862 unlock_user (p
, arg2
, arg3
);
6866 case TARGET_NR_mq_timedreceive
:
6871 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
6873 target_to_host_timespec(&ts
, arg5
);
6874 ret
= get_errno(mq_timedreceive(arg1
, p
, arg3
, &prio
, &ts
));
6875 host_to_target_timespec(arg5
, &ts
);
6878 ret
= get_errno(mq_receive(arg1
, p
, arg3
, &prio
));
6879 unlock_user (p
, arg2
, arg3
);
6881 put_user_u32(prio
, arg4
);
6885 /* Not implemented for now... */
6886 /* case TARGET_NR_mq_notify: */
6889 case TARGET_NR_mq_getsetattr
:
6891 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
6894 ret
= mq_getattr(arg1
, &posix_mq_attr_out
);
6895 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
6898 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
6899 ret
|= mq_setattr(arg1
, &posix_mq_attr_in
, &posix_mq_attr_out
);
6906 #ifdef CONFIG_SPLICE
6907 #ifdef TARGET_NR_tee
6910 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
6914 #ifdef TARGET_NR_splice
6915 case TARGET_NR_splice
:
6917 loff_t loff_in
, loff_out
;
6918 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
6920 get_user_u64(loff_in
, arg2
);
6921 ploff_in
= &loff_in
;
6924 get_user_u64(loff_out
, arg2
);
6925 ploff_out
= &loff_out
;
6927 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
6931 #ifdef TARGET_NR_vmsplice
6932 case TARGET_NR_vmsplice
:
6937 vec
= alloca(count
* sizeof(struct iovec
));
6938 if (lock_iovec(VERIFY_READ
, vec
, arg2
, count
, 1) < 0)
6940 ret
= get_errno(vmsplice(arg1
, vec
, count
, arg4
));
6941 unlock_iovec(vec
, arg2
, count
, 0);
6945 #endif /* CONFIG_SPLICE */
6948 gemu_log("qemu: Unsupported syscall: %d\n", num
);
6949 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
6950 unimplemented_nowarn
:
6952 ret
= -TARGET_ENOSYS
;
6957 gemu_log(" = %ld\n", ret
);
6960 print_syscall_ret(num
, ret
);
6963 ret
= -TARGET_EFAULT
;