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, see <http://www.gnu.org/licenses/>.
19 #define _ATFILE_SOURCE
20 #include "qemu/osdep.h"
21 #include "qemu/cutils.h"
22 #include "qemu/path.h"
29 #include <sys/mount.h>
31 #include <sys/fsuid.h>
32 #include <sys/personality.h>
33 #include <sys/prctl.h>
34 #include <sys/resource.h>
36 #include <linux/capability.h>
38 #include <sys/timex.h>
39 #include <sys/socket.h>
43 #include <sys/times.h>
46 #include <sys/statfs.h>
48 #include <sys/sysinfo.h>
49 #include <sys/signalfd.h>
50 //#include <sys/user.h>
51 #include <netinet/ip.h>
52 #include <netinet/tcp.h>
53 #include <linux/wireless.h>
54 #include <linux/icmp.h>
55 #include <linux/icmpv6.h>
56 #include <linux/errqueue.h>
57 #include <linux/random.h>
58 #include "qemu-common.h"
59 #include "qemu/sockets.h"
61 #include <sys/timerfd.h>
67 #include <sys/eventfd.h>
70 #include <sys/epoll.h>
73 #include "qemu/xattr.h"
75 #ifdef CONFIG_SENDFILE
76 #include <sys/sendfile.h>
79 #define termios host_termios
80 #define winsize host_winsize
81 #define termio host_termio
82 #define sgttyb host_sgttyb /* same as target */
83 #define tchars host_tchars /* same as target */
84 #define ltchars host_ltchars /* same as target */
86 #include <linux/termios.h>
87 #include <linux/unistd.h>
88 #include <linux/cdrom.h>
89 #include <linux/hdreg.h>
90 #include <linux/soundcard.h>
92 #include <linux/mtio.h>
94 #if defined(CONFIG_FIEMAP)
95 #include <linux/fiemap.h>
99 #include <linux/dm-ioctl.h>
100 #include <linux/reboot.h>
101 #include <linux/route.h>
102 #include <linux/filter.h>
103 #include <linux/blkpg.h>
104 #include <netpacket/packet.h>
105 #include <linux/netlink.h>
106 #ifdef CONFIG_RTNETLINK
107 #include <linux/rtnetlink.h>
108 #include <linux/if_bridge.h>
110 #include <linux/audit.h>
111 #include "linux_loop.h"
117 #define CLONE_IO 0x80000000 /* Clone io context */
120 /* We can't directly call the host clone syscall, because this will
121 * badly confuse libc (breaking mutexes, for example). So we must
122 * divide clone flags into:
123 * * flag combinations that look like pthread_create()
124 * * flag combinations that look like fork()
125 * * flags we can implement within QEMU itself
126 * * flags we can't support and will return an error for
128 /* For thread creation, all these flags must be present; for
129 * fork, none must be present.
131 #define CLONE_THREAD_FLAGS \
132 (CLONE_VM | CLONE_FS | CLONE_FILES | \
133 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
135 /* These flags are ignored:
136 * CLONE_DETACHED is now ignored by the kernel;
137 * CLONE_IO is just an optimisation hint to the I/O scheduler
139 #define CLONE_IGNORED_FLAGS \
140 (CLONE_DETACHED | CLONE_IO)
142 /* Flags for fork which we can implement within QEMU itself */
143 #define CLONE_OPTIONAL_FORK_FLAGS \
144 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
145 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
147 /* Flags for thread creation which we can implement within QEMU itself */
148 #define CLONE_OPTIONAL_THREAD_FLAGS \
149 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
150 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
152 #define CLONE_INVALID_FORK_FLAGS \
153 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
155 #define CLONE_INVALID_THREAD_FLAGS \
156 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
157 CLONE_IGNORED_FLAGS))
159 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
160 * have almost all been allocated. We cannot support any of
161 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
162 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
163 * The checks against the invalid thread masks above will catch these.
164 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
168 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
169 * once. This exercises the codepaths for restart.
171 //#define DEBUG_ERESTARTSYS
173 //#include <linux/msdos_fs.h>
174 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
175 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
185 #define _syscall0(type,name) \
186 static type name (void) \
188 return syscall(__NR_##name); \
191 #define _syscall1(type,name,type1,arg1) \
192 static type name (type1 arg1) \
194 return syscall(__NR_##name, arg1); \
197 #define _syscall2(type,name,type1,arg1,type2,arg2) \
198 static type name (type1 arg1,type2 arg2) \
200 return syscall(__NR_##name, arg1, arg2); \
203 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
204 static type name (type1 arg1,type2 arg2,type3 arg3) \
206 return syscall(__NR_##name, arg1, arg2, arg3); \
209 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
210 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
212 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
215 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
217 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
219 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
223 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
224 type5,arg5,type6,arg6) \
225 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
228 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
232 #define __NR_sys_uname __NR_uname
233 #define __NR_sys_getcwd1 __NR_getcwd
234 #define __NR_sys_getdents __NR_getdents
235 #define __NR_sys_getdents64 __NR_getdents64
236 #define __NR_sys_getpriority __NR_getpriority
237 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
238 #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
239 #define __NR_sys_syslog __NR_syslog
240 #define __NR_sys_futex __NR_futex
241 #define __NR_sys_inotify_init __NR_inotify_init
242 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
243 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
245 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
246 #define __NR__llseek __NR_lseek
249 /* Newer kernel ports have llseek() instead of _llseek() */
250 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
251 #define TARGET_NR__llseek TARGET_NR_llseek
255 _syscall0(int, gettid
)
257 /* This is a replacement for the host gettid() and must return a host
259 static int gettid(void) {
263 #if defined(TARGET_NR_getdents) && defined(__NR_getdents)
264 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
266 #if !defined(__NR_getdents) || \
267 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
268 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
270 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
271 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
272 loff_t
*, res
, uint
, wh
);
274 _syscall3(int, sys_rt_sigqueueinfo
, pid_t
, pid
, int, sig
, siginfo_t
*, uinfo
)
275 _syscall4(int, sys_rt_tgsigqueueinfo
, pid_t
, pid
, pid_t
, tid
, int, sig
,
277 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
278 #ifdef __NR_exit_group
279 _syscall1(int,exit_group
,int,error_code
)
281 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
282 _syscall1(int,set_tid_address
,int *,tidptr
)
284 #if defined(TARGET_NR_futex) && defined(__NR_futex)
285 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
286 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
288 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
289 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
290 unsigned long *, user_mask_ptr
);
291 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
292 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
293 unsigned long *, user_mask_ptr
);
294 #define __NR_sys_getcpu __NR_getcpu
295 _syscall3(int, sys_getcpu
, unsigned *, cpu
, unsigned *, node
, void *, tcache
);
296 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
298 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
299 struct __user_cap_data_struct
*, data
);
300 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
301 struct __user_cap_data_struct
*, data
);
302 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
303 _syscall2(int, ioprio_get
, int, which
, int, who
)
305 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
306 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
308 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
309 _syscall3(int, getrandom
, void *, buf
, size_t, buflen
, unsigned int, flags
)
312 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
313 _syscall5(int, kcmp
, pid_t
, pid1
, pid_t
, pid2
, int, type
,
314 unsigned long, idx1
, unsigned long, idx2
)
317 static bitmask_transtbl fcntl_flags_tbl
[] = {
318 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
319 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
320 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
321 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
322 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
323 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
324 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
325 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
326 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
327 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
328 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
329 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
330 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
331 #if defined(O_DIRECT)
332 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
334 #if defined(O_NOATIME)
335 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
337 #if defined(O_CLOEXEC)
338 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
341 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
343 #if defined(O_TMPFILE)
344 { TARGET_O_TMPFILE
, TARGET_O_TMPFILE
, O_TMPFILE
, O_TMPFILE
},
346 /* Don't terminate the list prematurely on 64-bit host+guest. */
347 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
348 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
355 QEMU_IFLA_BR_FORWARD_DELAY
,
356 QEMU_IFLA_BR_HELLO_TIME
,
357 QEMU_IFLA_BR_MAX_AGE
,
358 QEMU_IFLA_BR_AGEING_TIME
,
359 QEMU_IFLA_BR_STP_STATE
,
360 QEMU_IFLA_BR_PRIORITY
,
361 QEMU_IFLA_BR_VLAN_FILTERING
,
362 QEMU_IFLA_BR_VLAN_PROTOCOL
,
363 QEMU_IFLA_BR_GROUP_FWD_MASK
,
364 QEMU_IFLA_BR_ROOT_ID
,
365 QEMU_IFLA_BR_BRIDGE_ID
,
366 QEMU_IFLA_BR_ROOT_PORT
,
367 QEMU_IFLA_BR_ROOT_PATH_COST
,
368 QEMU_IFLA_BR_TOPOLOGY_CHANGE
,
369 QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED
,
370 QEMU_IFLA_BR_HELLO_TIMER
,
371 QEMU_IFLA_BR_TCN_TIMER
,
372 QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER
,
373 QEMU_IFLA_BR_GC_TIMER
,
374 QEMU_IFLA_BR_GROUP_ADDR
,
375 QEMU_IFLA_BR_FDB_FLUSH
,
376 QEMU_IFLA_BR_MCAST_ROUTER
,
377 QEMU_IFLA_BR_MCAST_SNOOPING
,
378 QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR
,
379 QEMU_IFLA_BR_MCAST_QUERIER
,
380 QEMU_IFLA_BR_MCAST_HASH_ELASTICITY
,
381 QEMU_IFLA_BR_MCAST_HASH_MAX
,
382 QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT
,
383 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT
,
384 QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL
,
385 QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL
,
386 QEMU_IFLA_BR_MCAST_QUERIER_INTVL
,
387 QEMU_IFLA_BR_MCAST_QUERY_INTVL
,
388 QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL
,
389 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL
,
390 QEMU_IFLA_BR_NF_CALL_IPTABLES
,
391 QEMU_IFLA_BR_NF_CALL_IP6TABLES
,
392 QEMU_IFLA_BR_NF_CALL_ARPTABLES
,
393 QEMU_IFLA_BR_VLAN_DEFAULT_PVID
,
395 QEMU_IFLA_BR_VLAN_STATS_ENABLED
,
396 QEMU_IFLA_BR_MCAST_STATS_ENABLED
,
420 QEMU_IFLA_NET_NS_PID
,
423 QEMU_IFLA_VFINFO_LIST
,
431 QEMU_IFLA_PROMISCUITY
,
432 QEMU_IFLA_NUM_TX_QUEUES
,
433 QEMU_IFLA_NUM_RX_QUEUES
,
435 QEMU_IFLA_PHYS_PORT_ID
,
436 QEMU_IFLA_CARRIER_CHANGES
,
437 QEMU_IFLA_PHYS_SWITCH_ID
,
438 QEMU_IFLA_LINK_NETNSID
,
439 QEMU_IFLA_PHYS_PORT_NAME
,
440 QEMU_IFLA_PROTO_DOWN
,
441 QEMU_IFLA_GSO_MAX_SEGS
,
442 QEMU_IFLA_GSO_MAX_SIZE
,
449 QEMU_IFLA_BRPORT_UNSPEC
,
450 QEMU_IFLA_BRPORT_STATE
,
451 QEMU_IFLA_BRPORT_PRIORITY
,
452 QEMU_IFLA_BRPORT_COST
,
453 QEMU_IFLA_BRPORT_MODE
,
454 QEMU_IFLA_BRPORT_GUARD
,
455 QEMU_IFLA_BRPORT_PROTECT
,
456 QEMU_IFLA_BRPORT_FAST_LEAVE
,
457 QEMU_IFLA_BRPORT_LEARNING
,
458 QEMU_IFLA_BRPORT_UNICAST_FLOOD
,
459 QEMU_IFLA_BRPORT_PROXYARP
,
460 QEMU_IFLA_BRPORT_LEARNING_SYNC
,
461 QEMU_IFLA_BRPORT_PROXYARP_WIFI
,
462 QEMU_IFLA_BRPORT_ROOT_ID
,
463 QEMU_IFLA_BRPORT_BRIDGE_ID
,
464 QEMU_IFLA_BRPORT_DESIGNATED_PORT
,
465 QEMU_IFLA_BRPORT_DESIGNATED_COST
,
468 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK
,
469 QEMU_IFLA_BRPORT_CONFIG_PENDING
,
470 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER
,
471 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER
,
472 QEMU_IFLA_BRPORT_HOLD_TIMER
,
473 QEMU_IFLA_BRPORT_FLUSH
,
474 QEMU_IFLA_BRPORT_MULTICAST_ROUTER
,
475 QEMU_IFLA_BRPORT_PAD
,
476 QEMU___IFLA_BRPORT_MAX
480 QEMU_IFLA_INFO_UNSPEC
,
483 QEMU_IFLA_INFO_XSTATS
,
484 QEMU_IFLA_INFO_SLAVE_KIND
,
485 QEMU_IFLA_INFO_SLAVE_DATA
,
486 QEMU___IFLA_INFO_MAX
,
490 QEMU_IFLA_INET_UNSPEC
,
492 QEMU___IFLA_INET_MAX
,
496 QEMU_IFLA_INET6_UNSPEC
,
497 QEMU_IFLA_INET6_FLAGS
,
498 QEMU_IFLA_INET6_CONF
,
499 QEMU_IFLA_INET6_STATS
,
500 QEMU_IFLA_INET6_MCAST
,
501 QEMU_IFLA_INET6_CACHEINFO
,
502 QEMU_IFLA_INET6_ICMP6STATS
,
503 QEMU_IFLA_INET6_TOKEN
,
504 QEMU_IFLA_INET6_ADDR_GEN_MODE
,
505 QEMU___IFLA_INET6_MAX
508 typedef abi_long (*TargetFdDataFunc
)(void *, size_t);
509 typedef abi_long (*TargetFdAddrFunc
)(void *, abi_ulong
, socklen_t
);
510 typedef struct TargetFdTrans
{
511 TargetFdDataFunc host_to_target_data
;
512 TargetFdDataFunc target_to_host_data
;
513 TargetFdAddrFunc target_to_host_addr
;
516 static TargetFdTrans
**target_fd_trans
;
518 static unsigned int target_fd_max
;
520 static TargetFdDataFunc
fd_trans_target_to_host_data(int fd
)
522 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
523 return target_fd_trans
[fd
]->target_to_host_data
;
528 static TargetFdDataFunc
fd_trans_host_to_target_data(int fd
)
530 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
531 return target_fd_trans
[fd
]->host_to_target_data
;
536 static TargetFdAddrFunc
fd_trans_target_to_host_addr(int fd
)
538 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
539 return target_fd_trans
[fd
]->target_to_host_addr
;
544 static void fd_trans_register(int fd
, TargetFdTrans
*trans
)
548 if (fd
>= target_fd_max
) {
549 oldmax
= target_fd_max
;
550 target_fd_max
= ((fd
>> 6) + 1) << 6; /* by slice of 64 entries */
551 target_fd_trans
= g_renew(TargetFdTrans
*,
552 target_fd_trans
, target_fd_max
);
553 memset((void *)(target_fd_trans
+ oldmax
), 0,
554 (target_fd_max
- oldmax
) * sizeof(TargetFdTrans
*));
556 target_fd_trans
[fd
] = trans
;
559 static void fd_trans_unregister(int fd
)
561 if (fd
>= 0 && fd
< target_fd_max
) {
562 target_fd_trans
[fd
] = NULL
;
566 static void fd_trans_dup(int oldfd
, int newfd
)
568 fd_trans_unregister(newfd
);
569 if (oldfd
< target_fd_max
&& target_fd_trans
[oldfd
]) {
570 fd_trans_register(newfd
, target_fd_trans
[oldfd
]);
574 static int sys_getcwd1(char *buf
, size_t size
)
576 if (getcwd(buf
, size
) == NULL
) {
577 /* getcwd() sets errno */
580 return strlen(buf
)+1;
583 #ifdef TARGET_NR_utimensat
584 #if defined(__NR_utimensat)
585 #define __NR_sys_utimensat __NR_utimensat
586 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
587 const struct timespec
*,tsp
,int,flags
)
589 static int sys_utimensat(int dirfd
, const char *pathname
,
590 const struct timespec times
[2], int flags
)
596 #endif /* TARGET_NR_utimensat */
598 #ifdef TARGET_NR_renameat2
599 #if defined(__NR_renameat2)
600 #define __NR_sys_renameat2 __NR_renameat2
601 _syscall5(int, sys_renameat2
, int, oldfd
, const char *, old
, int, newfd
,
602 const char *, new, unsigned int, flags
)
604 static int sys_renameat2(int oldfd
, const char *old
,
605 int newfd
, const char *new, int flags
)
608 return renameat(oldfd
, old
, newfd
, new);
614 #endif /* TARGET_NR_renameat2 */
616 #ifdef CONFIG_INOTIFY
617 #include <sys/inotify.h>
619 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
620 static int sys_inotify_init(void)
622 return (inotify_init());
625 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
626 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
628 return (inotify_add_watch(fd
, pathname
, mask
));
631 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
632 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
634 return (inotify_rm_watch(fd
, wd
));
637 #ifdef CONFIG_INOTIFY1
638 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
639 static int sys_inotify_init1(int flags
)
641 return (inotify_init1(flags
));
646 /* Userspace can usually survive runtime without inotify */
647 #undef TARGET_NR_inotify_init
648 #undef TARGET_NR_inotify_init1
649 #undef TARGET_NR_inotify_add_watch
650 #undef TARGET_NR_inotify_rm_watch
651 #endif /* CONFIG_INOTIFY */
653 #if defined(TARGET_NR_prlimit64)
654 #ifndef __NR_prlimit64
655 # define __NR_prlimit64 -1
657 #define __NR_sys_prlimit64 __NR_prlimit64
658 /* The glibc rlimit structure may not be that used by the underlying syscall */
659 struct host_rlimit64
{
663 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
664 const struct host_rlimit64
*, new_limit
,
665 struct host_rlimit64
*, old_limit
)
669 #if defined(TARGET_NR_timer_create)
670 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
671 static timer_t g_posix_timers
[32] = { 0, } ;
673 static inline int next_free_host_timer(void)
676 /* FIXME: Does finding the next free slot require a lock? */
677 for (k
= 0; k
< ARRAY_SIZE(g_posix_timers
); k
++) {
678 if (g_posix_timers
[k
] == 0) {
679 g_posix_timers
[k
] = (timer_t
) 1;
687 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
689 static inline int regpairs_aligned(void *cpu_env
, int num
)
691 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
693 #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
694 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
695 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
696 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
697 * of registers which translates to the same as ARM/MIPS, because we start with
699 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
700 #elif defined(TARGET_SH4)
701 /* SH4 doesn't align register pairs, except for p{read,write}64 */
702 static inline int regpairs_aligned(void *cpu_env
, int num
)
705 case TARGET_NR_pread64
:
706 case TARGET_NR_pwrite64
:
713 #elif defined(TARGET_XTENSA)
714 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
716 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 0; }
719 #define ERRNO_TABLE_SIZE 1200
721 /* target_to_host_errno_table[] is initialized from
722 * host_to_target_errno_table[] in syscall_init(). */
723 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
727 * This list is the union of errno values overridden in asm-<arch>/errno.h
728 * minus the errnos that are not actually generic to all archs.
730 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
731 [EAGAIN
] = TARGET_EAGAIN
,
732 [EIDRM
] = TARGET_EIDRM
,
733 [ECHRNG
] = TARGET_ECHRNG
,
734 [EL2NSYNC
] = TARGET_EL2NSYNC
,
735 [EL3HLT
] = TARGET_EL3HLT
,
736 [EL3RST
] = TARGET_EL3RST
,
737 [ELNRNG
] = TARGET_ELNRNG
,
738 [EUNATCH
] = TARGET_EUNATCH
,
739 [ENOCSI
] = TARGET_ENOCSI
,
740 [EL2HLT
] = TARGET_EL2HLT
,
741 [EDEADLK
] = TARGET_EDEADLK
,
742 [ENOLCK
] = TARGET_ENOLCK
,
743 [EBADE
] = TARGET_EBADE
,
744 [EBADR
] = TARGET_EBADR
,
745 [EXFULL
] = TARGET_EXFULL
,
746 [ENOANO
] = TARGET_ENOANO
,
747 [EBADRQC
] = TARGET_EBADRQC
,
748 [EBADSLT
] = TARGET_EBADSLT
,
749 [EBFONT
] = TARGET_EBFONT
,
750 [ENOSTR
] = TARGET_ENOSTR
,
751 [ENODATA
] = TARGET_ENODATA
,
752 [ETIME
] = TARGET_ETIME
,
753 [ENOSR
] = TARGET_ENOSR
,
754 [ENONET
] = TARGET_ENONET
,
755 [ENOPKG
] = TARGET_ENOPKG
,
756 [EREMOTE
] = TARGET_EREMOTE
,
757 [ENOLINK
] = TARGET_ENOLINK
,
758 [EADV
] = TARGET_EADV
,
759 [ESRMNT
] = TARGET_ESRMNT
,
760 [ECOMM
] = TARGET_ECOMM
,
761 [EPROTO
] = TARGET_EPROTO
,
762 [EDOTDOT
] = TARGET_EDOTDOT
,
763 [EMULTIHOP
] = TARGET_EMULTIHOP
,
764 [EBADMSG
] = TARGET_EBADMSG
,
765 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
766 [EOVERFLOW
] = TARGET_EOVERFLOW
,
767 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
768 [EBADFD
] = TARGET_EBADFD
,
769 [EREMCHG
] = TARGET_EREMCHG
,
770 [ELIBACC
] = TARGET_ELIBACC
,
771 [ELIBBAD
] = TARGET_ELIBBAD
,
772 [ELIBSCN
] = TARGET_ELIBSCN
,
773 [ELIBMAX
] = TARGET_ELIBMAX
,
774 [ELIBEXEC
] = TARGET_ELIBEXEC
,
775 [EILSEQ
] = TARGET_EILSEQ
,
776 [ENOSYS
] = TARGET_ENOSYS
,
777 [ELOOP
] = TARGET_ELOOP
,
778 [ERESTART
] = TARGET_ERESTART
,
779 [ESTRPIPE
] = TARGET_ESTRPIPE
,
780 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
781 [EUSERS
] = TARGET_EUSERS
,
782 [ENOTSOCK
] = TARGET_ENOTSOCK
,
783 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
784 [EMSGSIZE
] = TARGET_EMSGSIZE
,
785 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
786 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
787 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
788 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
789 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
790 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
791 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
792 [EADDRINUSE
] = TARGET_EADDRINUSE
,
793 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
794 [ENETDOWN
] = TARGET_ENETDOWN
,
795 [ENETUNREACH
] = TARGET_ENETUNREACH
,
796 [ENETRESET
] = TARGET_ENETRESET
,
797 [ECONNABORTED
] = TARGET_ECONNABORTED
,
798 [ECONNRESET
] = TARGET_ECONNRESET
,
799 [ENOBUFS
] = TARGET_ENOBUFS
,
800 [EISCONN
] = TARGET_EISCONN
,
801 [ENOTCONN
] = TARGET_ENOTCONN
,
802 [EUCLEAN
] = TARGET_EUCLEAN
,
803 [ENOTNAM
] = TARGET_ENOTNAM
,
804 [ENAVAIL
] = TARGET_ENAVAIL
,
805 [EISNAM
] = TARGET_EISNAM
,
806 [EREMOTEIO
] = TARGET_EREMOTEIO
,
807 [EDQUOT
] = TARGET_EDQUOT
,
808 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
809 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
810 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
811 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
812 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
813 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
814 [EALREADY
] = TARGET_EALREADY
,
815 [EINPROGRESS
] = TARGET_EINPROGRESS
,
816 [ESTALE
] = TARGET_ESTALE
,
817 [ECANCELED
] = TARGET_ECANCELED
,
818 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
819 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
821 [ENOKEY
] = TARGET_ENOKEY
,
824 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
827 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
830 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
833 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
835 #ifdef ENOTRECOVERABLE
836 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
839 [ENOMSG
] = TARGET_ENOMSG
,
842 [ERFKILL
] = TARGET_ERFKILL
,
845 [EHWPOISON
] = TARGET_EHWPOISON
,
849 static inline int host_to_target_errno(int err
)
851 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
852 host_to_target_errno_table
[err
]) {
853 return host_to_target_errno_table
[err
];
858 static inline int target_to_host_errno(int err
)
860 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
861 target_to_host_errno_table
[err
]) {
862 return target_to_host_errno_table
[err
];
867 static inline abi_long
get_errno(abi_long ret
)
870 return -host_to_target_errno(errno
);
875 static inline int is_error(abi_long ret
)
877 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
880 const char *target_strerror(int err
)
882 if (err
== TARGET_ERESTARTSYS
) {
883 return "To be restarted";
885 if (err
== TARGET_QEMU_ESIGRETURN
) {
886 return "Successful exit from sigreturn";
889 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
892 return strerror(target_to_host_errno(err
));
895 #define safe_syscall0(type, name) \
896 static type safe_##name(void) \
898 return safe_syscall(__NR_##name); \
901 #define safe_syscall1(type, name, type1, arg1) \
902 static type safe_##name(type1 arg1) \
904 return safe_syscall(__NR_##name, arg1); \
907 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
908 static type safe_##name(type1 arg1, type2 arg2) \
910 return safe_syscall(__NR_##name, arg1, arg2); \
913 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
914 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
916 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
919 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
921 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
923 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
926 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
927 type4, arg4, type5, arg5) \
928 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
931 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
934 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
935 type4, arg4, type5, arg5, type6, arg6) \
936 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
937 type5 arg5, type6 arg6) \
939 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
942 safe_syscall3(ssize_t
, read
, int, fd
, void *, buff
, size_t, count
)
943 safe_syscall3(ssize_t
, write
, int, fd
, const void *, buff
, size_t, count
)
944 safe_syscall4(int, openat
, int, dirfd
, const char *, pathname
, \
945 int, flags
, mode_t
, mode
)
946 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
947 struct rusage
*, rusage
)
948 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
949 int, options
, struct rusage
*, rusage
)
950 safe_syscall3(int, execve
, const char *, filename
, char **, argv
, char **, envp
)
951 safe_syscall6(int, pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
, \
952 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
)
953 safe_syscall5(int, ppoll
, struct pollfd
*, ufds
, unsigned int, nfds
,
954 struct timespec
*, tsp
, const sigset_t
*, sigmask
,
956 safe_syscall6(int, epoll_pwait
, int, epfd
, struct epoll_event
*, events
,
957 int, maxevents
, int, timeout
, const sigset_t
*, sigmask
,
959 safe_syscall6(int,futex
,int *,uaddr
,int,op
,int,val
, \
960 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
961 safe_syscall2(int, rt_sigsuspend
, sigset_t
*, newset
, size_t, sigsetsize
)
962 safe_syscall2(int, kill
, pid_t
, pid
, int, sig
)
963 safe_syscall2(int, tkill
, int, tid
, int, sig
)
964 safe_syscall3(int, tgkill
, int, tgid
, int, pid
, int, sig
)
965 safe_syscall3(ssize_t
, readv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
966 safe_syscall3(ssize_t
, writev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
967 safe_syscall5(ssize_t
, preadv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
968 unsigned long, pos_l
, unsigned long, pos_h
)
969 safe_syscall5(ssize_t
, pwritev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
970 unsigned long, pos_l
, unsigned long, pos_h
)
971 safe_syscall3(int, connect
, int, fd
, const struct sockaddr
*, addr
,
973 safe_syscall6(ssize_t
, sendto
, int, fd
, const void *, buf
, size_t, len
,
974 int, flags
, const struct sockaddr
*, addr
, socklen_t
, addrlen
)
975 safe_syscall6(ssize_t
, recvfrom
, int, fd
, void *, buf
, size_t, len
,
976 int, flags
, struct sockaddr
*, addr
, socklen_t
*, addrlen
)
977 safe_syscall3(ssize_t
, sendmsg
, int, fd
, const struct msghdr
*, msg
, int, flags
)
978 safe_syscall3(ssize_t
, recvmsg
, int, fd
, struct msghdr
*, msg
, int, flags
)
979 safe_syscall2(int, flock
, int, fd
, int, operation
)
980 safe_syscall4(int, rt_sigtimedwait
, const sigset_t
*, these
, siginfo_t
*, uinfo
,
981 const struct timespec
*, uts
, size_t, sigsetsize
)
982 safe_syscall4(int, accept4
, int, fd
, struct sockaddr
*, addr
, socklen_t
*, len
,
984 safe_syscall2(int, nanosleep
, const struct timespec
*, req
,
985 struct timespec
*, rem
)
986 #ifdef TARGET_NR_clock_nanosleep
987 safe_syscall4(int, clock_nanosleep
, const clockid_t
, clock
, int, flags
,
988 const struct timespec
*, req
, struct timespec
*, rem
)
991 safe_syscall4(int, msgsnd
, int, msgid
, const void *, msgp
, size_t, sz
,
993 safe_syscall5(int, msgrcv
, int, msgid
, void *, msgp
, size_t, sz
,
994 long, msgtype
, int, flags
)
995 safe_syscall4(int, semtimedop
, int, semid
, struct sembuf
*, tsops
,
996 unsigned, nsops
, const struct timespec
*, timeout
)
998 /* This host kernel architecture uses a single ipc syscall; fake up
999 * wrappers for the sub-operations to hide this implementation detail.
1000 * Annoyingly we can't include linux/ipc.h to get the constant definitions
1001 * for the call parameter because some structs in there conflict with the
1002 * sys/ipc.h ones. So we just define them here, and rely on them being
1003 * the same for all host architectures.
1005 #define Q_SEMTIMEDOP 4
1008 #define Q_IPCCALL(VERSION, OP) ((VERSION) << 16 | (OP))
1010 safe_syscall6(int, ipc
, int, call
, long, first
, long, second
, long, third
,
1011 void *, ptr
, long, fifth
)
1012 static int safe_msgsnd(int msgid
, const void *msgp
, size_t sz
, int flags
)
1014 return safe_ipc(Q_IPCCALL(0, Q_MSGSND
), msgid
, sz
, flags
, (void *)msgp
, 0);
1016 static int safe_msgrcv(int msgid
, void *msgp
, size_t sz
, long type
, int flags
)
1018 return safe_ipc(Q_IPCCALL(1, Q_MSGRCV
), msgid
, sz
, flags
, msgp
, type
);
1020 static int safe_semtimedop(int semid
, struct sembuf
*tsops
, unsigned nsops
,
1021 const struct timespec
*timeout
)
1023 return safe_ipc(Q_IPCCALL(0, Q_SEMTIMEDOP
), semid
, nsops
, 0, tsops
,
1027 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1028 safe_syscall5(int, mq_timedsend
, int, mqdes
, const char *, msg_ptr
,
1029 size_t, len
, unsigned, prio
, const struct timespec
*, timeout
)
1030 safe_syscall5(int, mq_timedreceive
, int, mqdes
, char *, msg_ptr
,
1031 size_t, len
, unsigned *, prio
, const struct timespec
*, timeout
)
1033 /* We do ioctl like this rather than via safe_syscall3 to preserve the
1034 * "third argument might be integer or pointer or not present" behaviour of
1035 * the libc function.
1037 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
1038 /* Similarly for fcntl. Note that callers must always:
1039 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
1040 * use the flock64 struct rather than unsuffixed flock
1041 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
1044 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
1046 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
1049 static inline int host_to_target_sock_type(int host_type
)
1053 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
1055 target_type
= TARGET_SOCK_DGRAM
;
1058 target_type
= TARGET_SOCK_STREAM
;
1061 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
1065 #if defined(SOCK_CLOEXEC)
1066 if (host_type
& SOCK_CLOEXEC
) {
1067 target_type
|= TARGET_SOCK_CLOEXEC
;
1071 #if defined(SOCK_NONBLOCK)
1072 if (host_type
& SOCK_NONBLOCK
) {
1073 target_type
|= TARGET_SOCK_NONBLOCK
;
1080 static abi_ulong target_brk
;
1081 static abi_ulong target_original_brk
;
1082 static abi_ulong brk_page
;
1084 void target_set_brk(abi_ulong new_brk
)
1086 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
1087 brk_page
= HOST_PAGE_ALIGN(target_brk
);
1090 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
1091 #define DEBUGF_BRK(message, args...)
1093 /* do_brk() must return target values and target errnos. */
1094 abi_long
do_brk(abi_ulong new_brk
)
1096 abi_long mapped_addr
;
1097 abi_ulong new_alloc_size
;
1099 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
1102 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
1105 if (new_brk
< target_original_brk
) {
1106 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
1111 /* If the new brk is less than the highest page reserved to the
1112 * target heap allocation, set it and we're almost done... */
1113 if (new_brk
<= brk_page
) {
1114 /* Heap contents are initialized to zero, as for anonymous
1116 if (new_brk
> target_brk
) {
1117 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
1119 target_brk
= new_brk
;
1120 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
1124 /* We need to allocate more memory after the brk... Note that
1125 * we don't use MAP_FIXED because that will map over the top of
1126 * any existing mapping (like the one with the host libc or qemu
1127 * itself); instead we treat "mapped but at wrong address" as
1128 * a failure and unmap again.
1130 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
1131 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
1132 PROT_READ
|PROT_WRITE
,
1133 MAP_ANON
|MAP_PRIVATE
, 0, 0));
1135 if (mapped_addr
== brk_page
) {
1136 /* Heap contents are initialized to zero, as for anonymous
1137 * mapped pages. Technically the new pages are already
1138 * initialized to zero since they *are* anonymous mapped
1139 * pages, however we have to take care with the contents that
1140 * come from the remaining part of the previous page: it may
1141 * contains garbage data due to a previous heap usage (grown
1142 * then shrunken). */
1143 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
1145 target_brk
= new_brk
;
1146 brk_page
= HOST_PAGE_ALIGN(target_brk
);
1147 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
1150 } else if (mapped_addr
!= -1) {
1151 /* Mapped but at wrong address, meaning there wasn't actually
1152 * enough space for this brk.
1154 target_munmap(mapped_addr
, new_alloc_size
);
1156 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
1159 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
1162 #if defined(TARGET_ALPHA)
1163 /* We (partially) emulate OSF/1 on Alpha, which requires we
1164 return a proper errno, not an unchanged brk value. */
1165 return -TARGET_ENOMEM
;
1167 /* For everything else, return the previous break. */
1171 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
1172 abi_ulong target_fds_addr
,
1176 abi_ulong b
, *target_fds
;
1178 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
1179 if (!(target_fds
= lock_user(VERIFY_READ
,
1181 sizeof(abi_ulong
) * nw
,
1183 return -TARGET_EFAULT
;
1187 for (i
= 0; i
< nw
; i
++) {
1188 /* grab the abi_ulong */
1189 __get_user(b
, &target_fds
[i
]);
1190 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
1191 /* check the bit inside the abi_ulong */
1198 unlock_user(target_fds
, target_fds_addr
, 0);
1203 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
1204 abi_ulong target_fds_addr
,
1207 if (target_fds_addr
) {
1208 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
1209 return -TARGET_EFAULT
;
1217 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
1223 abi_ulong
*target_fds
;
1225 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
1226 if (!(target_fds
= lock_user(VERIFY_WRITE
,
1228 sizeof(abi_ulong
) * nw
,
1230 return -TARGET_EFAULT
;
1233 for (i
= 0; i
< nw
; i
++) {
1235 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
1236 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
1239 __put_user(v
, &target_fds
[i
]);
1242 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
1247 #if defined(__alpha__)
1248 #define HOST_HZ 1024
1253 static inline abi_long
host_to_target_clock_t(long ticks
)
1255 #if HOST_HZ == TARGET_HZ
1258 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
1262 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
1263 const struct rusage
*rusage
)
1265 struct target_rusage
*target_rusage
;
1267 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
1268 return -TARGET_EFAULT
;
1269 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
1270 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
1271 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
1272 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
1273 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
1274 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
1275 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
1276 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
1277 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
1278 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
1279 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
1280 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
1281 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
1282 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
1283 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
1284 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
1285 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
1286 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
1287 unlock_user_struct(target_rusage
, target_addr
, 1);
1292 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
1294 abi_ulong target_rlim_swap
;
1297 target_rlim_swap
= tswapal(target_rlim
);
1298 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
1299 return RLIM_INFINITY
;
1301 result
= target_rlim_swap
;
1302 if (target_rlim_swap
!= (rlim_t
)result
)
1303 return RLIM_INFINITY
;
1308 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
1310 abi_ulong target_rlim_swap
;
1313 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
1314 target_rlim_swap
= TARGET_RLIM_INFINITY
;
1316 target_rlim_swap
= rlim
;
1317 result
= tswapal(target_rlim_swap
);
1322 static inline int target_to_host_resource(int code
)
1325 case TARGET_RLIMIT_AS
:
1327 case TARGET_RLIMIT_CORE
:
1329 case TARGET_RLIMIT_CPU
:
1331 case TARGET_RLIMIT_DATA
:
1333 case TARGET_RLIMIT_FSIZE
:
1334 return RLIMIT_FSIZE
;
1335 case TARGET_RLIMIT_LOCKS
:
1336 return RLIMIT_LOCKS
;
1337 case TARGET_RLIMIT_MEMLOCK
:
1338 return RLIMIT_MEMLOCK
;
1339 case TARGET_RLIMIT_MSGQUEUE
:
1340 return RLIMIT_MSGQUEUE
;
1341 case TARGET_RLIMIT_NICE
:
1343 case TARGET_RLIMIT_NOFILE
:
1344 return RLIMIT_NOFILE
;
1345 case TARGET_RLIMIT_NPROC
:
1346 return RLIMIT_NPROC
;
1347 case TARGET_RLIMIT_RSS
:
1349 case TARGET_RLIMIT_RTPRIO
:
1350 return RLIMIT_RTPRIO
;
1351 case TARGET_RLIMIT_SIGPENDING
:
1352 return RLIMIT_SIGPENDING
;
1353 case TARGET_RLIMIT_STACK
:
1354 return RLIMIT_STACK
;
1360 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1361 abi_ulong target_tv_addr
)
1363 struct target_timeval
*target_tv
;
1365 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1))
1366 return -TARGET_EFAULT
;
1368 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1369 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1371 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1376 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1377 const struct timeval
*tv
)
1379 struct target_timeval
*target_tv
;
1381 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0))
1382 return -TARGET_EFAULT
;
1384 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1385 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1387 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1392 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1393 abi_ulong target_tz_addr
)
1395 struct target_timezone
*target_tz
;
1397 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1398 return -TARGET_EFAULT
;
1401 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1402 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1404 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1409 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1412 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1413 abi_ulong target_mq_attr_addr
)
1415 struct target_mq_attr
*target_mq_attr
;
1417 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1418 target_mq_attr_addr
, 1))
1419 return -TARGET_EFAULT
;
1421 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1422 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1423 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1424 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1426 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1431 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1432 const struct mq_attr
*attr
)
1434 struct target_mq_attr
*target_mq_attr
;
1436 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1437 target_mq_attr_addr
, 0))
1438 return -TARGET_EFAULT
;
1440 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1441 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1442 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1443 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1445 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1451 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1452 /* do_select() must return target values and target errnos. */
1453 static abi_long
do_select(int n
,
1454 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1455 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1457 fd_set rfds
, wfds
, efds
;
1458 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1460 struct timespec ts
, *ts_ptr
;
1463 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1467 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1471 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1476 if (target_tv_addr
) {
1477 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1478 return -TARGET_EFAULT
;
1479 ts
.tv_sec
= tv
.tv_sec
;
1480 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1486 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1489 if (!is_error(ret
)) {
1490 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1491 return -TARGET_EFAULT
;
1492 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1493 return -TARGET_EFAULT
;
1494 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1495 return -TARGET_EFAULT
;
1497 if (target_tv_addr
) {
1498 tv
.tv_sec
= ts
.tv_sec
;
1499 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1500 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1501 return -TARGET_EFAULT
;
1509 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1510 static abi_long
do_old_select(abi_ulong arg1
)
1512 struct target_sel_arg_struct
*sel
;
1513 abi_ulong inp
, outp
, exp
, tvp
;
1516 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1)) {
1517 return -TARGET_EFAULT
;
1520 nsel
= tswapal(sel
->n
);
1521 inp
= tswapal(sel
->inp
);
1522 outp
= tswapal(sel
->outp
);
1523 exp
= tswapal(sel
->exp
);
1524 tvp
= tswapal(sel
->tvp
);
1526 unlock_user_struct(sel
, arg1
, 0);
1528 return do_select(nsel
, inp
, outp
, exp
, tvp
);
1533 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1536 return pipe2(host_pipe
, flags
);
1542 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1543 int flags
, int is_pipe2
)
1547 ret
= flags
? do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1550 return get_errno(ret
);
1552 /* Several targets have special calling conventions for the original
1553 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1555 #if defined(TARGET_ALPHA)
1556 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1557 return host_pipe
[0];
1558 #elif defined(TARGET_MIPS)
1559 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1560 return host_pipe
[0];
1561 #elif defined(TARGET_SH4)
1562 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1563 return host_pipe
[0];
1564 #elif defined(TARGET_SPARC)
1565 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1566 return host_pipe
[0];
1570 if (put_user_s32(host_pipe
[0], pipedes
)
1571 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1572 return -TARGET_EFAULT
;
1573 return get_errno(ret
);
1576 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1577 abi_ulong target_addr
,
1580 struct target_ip_mreqn
*target_smreqn
;
1582 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1584 return -TARGET_EFAULT
;
1585 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1586 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1587 if (len
== sizeof(struct target_ip_mreqn
))
1588 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1589 unlock_user(target_smreqn
, target_addr
, 0);
1594 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1595 abi_ulong target_addr
,
1598 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1599 sa_family_t sa_family
;
1600 struct target_sockaddr
*target_saddr
;
1602 if (fd_trans_target_to_host_addr(fd
)) {
1603 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1606 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1608 return -TARGET_EFAULT
;
1610 sa_family
= tswap16(target_saddr
->sa_family
);
1612 /* Oops. The caller might send a incomplete sun_path; sun_path
1613 * must be terminated by \0 (see the manual page), but
1614 * unfortunately it is quite common to specify sockaddr_un
1615 * length as "strlen(x->sun_path)" while it should be
1616 * "strlen(...) + 1". We'll fix that here if needed.
1617 * Linux kernel has a similar feature.
1620 if (sa_family
== AF_UNIX
) {
1621 if (len
< unix_maxlen
&& len
> 0) {
1622 char *cp
= (char*)target_saddr
;
1624 if ( cp
[len
-1] && !cp
[len
] )
1627 if (len
> unix_maxlen
)
1631 memcpy(addr
, target_saddr
, len
);
1632 addr
->sa_family
= sa_family
;
1633 if (sa_family
== AF_NETLINK
) {
1634 struct sockaddr_nl
*nladdr
;
1636 nladdr
= (struct sockaddr_nl
*)addr
;
1637 nladdr
->nl_pid
= tswap32(nladdr
->nl_pid
);
1638 nladdr
->nl_groups
= tswap32(nladdr
->nl_groups
);
1639 } else if (sa_family
== AF_PACKET
) {
1640 struct target_sockaddr_ll
*lladdr
;
1642 lladdr
= (struct target_sockaddr_ll
*)addr
;
1643 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1644 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1646 unlock_user(target_saddr
, target_addr
, 0);
1651 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1652 struct sockaddr
*addr
,
1655 struct target_sockaddr
*target_saddr
;
1662 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1664 return -TARGET_EFAULT
;
1665 memcpy(target_saddr
, addr
, len
);
1666 if (len
>= offsetof(struct target_sockaddr
, sa_family
) +
1667 sizeof(target_saddr
->sa_family
)) {
1668 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1670 if (addr
->sa_family
== AF_NETLINK
&& len
>= sizeof(struct sockaddr_nl
)) {
1671 struct sockaddr_nl
*target_nl
= (struct sockaddr_nl
*)target_saddr
;
1672 target_nl
->nl_pid
= tswap32(target_nl
->nl_pid
);
1673 target_nl
->nl_groups
= tswap32(target_nl
->nl_groups
);
1674 } else if (addr
->sa_family
== AF_PACKET
) {
1675 struct sockaddr_ll
*target_ll
= (struct sockaddr_ll
*)target_saddr
;
1676 target_ll
->sll_ifindex
= tswap32(target_ll
->sll_ifindex
);
1677 target_ll
->sll_hatype
= tswap16(target_ll
->sll_hatype
);
1678 } else if (addr
->sa_family
== AF_INET6
&&
1679 len
>= sizeof(struct target_sockaddr_in6
)) {
1680 struct target_sockaddr_in6
*target_in6
=
1681 (struct target_sockaddr_in6
*)target_saddr
;
1682 target_in6
->sin6_scope_id
= tswap16(target_in6
->sin6_scope_id
);
1684 unlock_user(target_saddr
, target_addr
, len
);
1689 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1690 struct target_msghdr
*target_msgh
)
1692 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1693 abi_long msg_controllen
;
1694 abi_ulong target_cmsg_addr
;
1695 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1696 socklen_t space
= 0;
1698 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1699 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1701 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1702 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1703 target_cmsg_start
= target_cmsg
;
1705 return -TARGET_EFAULT
;
1707 while (cmsg
&& target_cmsg
) {
1708 void *data
= CMSG_DATA(cmsg
);
1709 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1711 int len
= tswapal(target_cmsg
->cmsg_len
)
1712 - sizeof(struct target_cmsghdr
);
1714 space
+= CMSG_SPACE(len
);
1715 if (space
> msgh
->msg_controllen
) {
1716 space
-= CMSG_SPACE(len
);
1717 /* This is a QEMU bug, since we allocated the payload
1718 * area ourselves (unlike overflow in host-to-target
1719 * conversion, which is just the guest giving us a buffer
1720 * that's too small). It can't happen for the payload types
1721 * we currently support; if it becomes an issue in future
1722 * we would need to improve our allocation strategy to
1723 * something more intelligent than "twice the size of the
1724 * target buffer we're reading from".
1726 gemu_log("Host cmsg overflow\n");
1730 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1731 cmsg
->cmsg_level
= SOL_SOCKET
;
1733 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1735 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1736 cmsg
->cmsg_len
= CMSG_LEN(len
);
1738 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1739 int *fd
= (int *)data
;
1740 int *target_fd
= (int *)target_data
;
1741 int i
, numfds
= len
/ sizeof(int);
1743 for (i
= 0; i
< numfds
; i
++) {
1744 __get_user(fd
[i
], target_fd
+ i
);
1746 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1747 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1748 struct ucred
*cred
= (struct ucred
*)data
;
1749 struct target_ucred
*target_cred
=
1750 (struct target_ucred
*)target_data
;
1752 __get_user(cred
->pid
, &target_cred
->pid
);
1753 __get_user(cred
->uid
, &target_cred
->uid
);
1754 __get_user(cred
->gid
, &target_cred
->gid
);
1756 gemu_log("Unsupported ancillary data: %d/%d\n",
1757 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1758 memcpy(data
, target_data
, len
);
1761 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1762 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1765 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1767 msgh
->msg_controllen
= space
;
1771 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1772 struct msghdr
*msgh
)
1774 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1775 abi_long msg_controllen
;
1776 abi_ulong target_cmsg_addr
;
1777 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1778 socklen_t space
= 0;
1780 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1781 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1783 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1784 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1785 target_cmsg_start
= target_cmsg
;
1787 return -TARGET_EFAULT
;
1789 while (cmsg
&& target_cmsg
) {
1790 void *data
= CMSG_DATA(cmsg
);
1791 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1793 int len
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
1794 int tgt_len
, tgt_space
;
1796 /* We never copy a half-header but may copy half-data;
1797 * this is Linux's behaviour in put_cmsg(). Note that
1798 * truncation here is a guest problem (which we report
1799 * to the guest via the CTRUNC bit), unlike truncation
1800 * in target_to_host_cmsg, which is a QEMU bug.
1802 if (msg_controllen
< sizeof(struct target_cmsghdr
)) {
1803 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1807 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1808 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1810 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1812 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1814 /* Payload types which need a different size of payload on
1815 * the target must adjust tgt_len here.
1817 switch (cmsg
->cmsg_level
) {
1819 switch (cmsg
->cmsg_type
) {
1821 tgt_len
= sizeof(struct target_timeval
);
1831 if (msg_controllen
< TARGET_CMSG_LEN(tgt_len
)) {
1832 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1833 tgt_len
= msg_controllen
- sizeof(struct target_cmsghdr
);
1836 /* We must now copy-and-convert len bytes of payload
1837 * into tgt_len bytes of destination space. Bear in mind
1838 * that in both source and destination we may be dealing
1839 * with a truncated value!
1841 switch (cmsg
->cmsg_level
) {
1843 switch (cmsg
->cmsg_type
) {
1846 int *fd
= (int *)data
;
1847 int *target_fd
= (int *)target_data
;
1848 int i
, numfds
= tgt_len
/ sizeof(int);
1850 for (i
= 0; i
< numfds
; i
++) {
1851 __put_user(fd
[i
], target_fd
+ i
);
1857 struct timeval
*tv
= (struct timeval
*)data
;
1858 struct target_timeval
*target_tv
=
1859 (struct target_timeval
*)target_data
;
1861 if (len
!= sizeof(struct timeval
) ||
1862 tgt_len
!= sizeof(struct target_timeval
)) {
1866 /* copy struct timeval to target */
1867 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1868 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1871 case SCM_CREDENTIALS
:
1873 struct ucred
*cred
= (struct ucred
*)data
;
1874 struct target_ucred
*target_cred
=
1875 (struct target_ucred
*)target_data
;
1877 __put_user(cred
->pid
, &target_cred
->pid
);
1878 __put_user(cred
->uid
, &target_cred
->uid
);
1879 __put_user(cred
->gid
, &target_cred
->gid
);
1888 switch (cmsg
->cmsg_type
) {
1891 uint32_t *v
= (uint32_t *)data
;
1892 uint32_t *t_int
= (uint32_t *)target_data
;
1894 if (len
!= sizeof(uint32_t) ||
1895 tgt_len
!= sizeof(uint32_t)) {
1898 __put_user(*v
, t_int
);
1904 struct sock_extended_err ee
;
1905 struct sockaddr_in offender
;
1907 struct errhdr_t
*errh
= (struct errhdr_t
*)data
;
1908 struct errhdr_t
*target_errh
=
1909 (struct errhdr_t
*)target_data
;
1911 if (len
!= sizeof(struct errhdr_t
) ||
1912 tgt_len
!= sizeof(struct errhdr_t
)) {
1915 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1916 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1917 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1918 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1919 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1920 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1921 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1922 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1923 (void *) &errh
->offender
, sizeof(errh
->offender
));
1932 switch (cmsg
->cmsg_type
) {
1935 uint32_t *v
= (uint32_t *)data
;
1936 uint32_t *t_int
= (uint32_t *)target_data
;
1938 if (len
!= sizeof(uint32_t) ||
1939 tgt_len
!= sizeof(uint32_t)) {
1942 __put_user(*v
, t_int
);
1948 struct sock_extended_err ee
;
1949 struct sockaddr_in6 offender
;
1951 struct errhdr6_t
*errh
= (struct errhdr6_t
*)data
;
1952 struct errhdr6_t
*target_errh
=
1953 (struct errhdr6_t
*)target_data
;
1955 if (len
!= sizeof(struct errhdr6_t
) ||
1956 tgt_len
!= sizeof(struct errhdr6_t
)) {
1959 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1960 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1961 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1962 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1963 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1964 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1965 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1966 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1967 (void *) &errh
->offender
, sizeof(errh
->offender
));
1977 gemu_log("Unsupported ancillary data: %d/%d\n",
1978 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1979 memcpy(target_data
, data
, MIN(len
, tgt_len
));
1980 if (tgt_len
> len
) {
1981 memset(target_data
+ len
, 0, tgt_len
- len
);
1985 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(tgt_len
));
1986 tgt_space
= TARGET_CMSG_SPACE(tgt_len
);
1987 if (msg_controllen
< tgt_space
) {
1988 tgt_space
= msg_controllen
;
1990 msg_controllen
-= tgt_space
;
1992 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1993 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1996 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1998 target_msgh
->msg_controllen
= tswapal(space
);
2002 static void tswap_nlmsghdr(struct nlmsghdr
*nlh
)
2004 nlh
->nlmsg_len
= tswap32(nlh
->nlmsg_len
);
2005 nlh
->nlmsg_type
= tswap16(nlh
->nlmsg_type
);
2006 nlh
->nlmsg_flags
= tswap16(nlh
->nlmsg_flags
);
2007 nlh
->nlmsg_seq
= tswap32(nlh
->nlmsg_seq
);
2008 nlh
->nlmsg_pid
= tswap32(nlh
->nlmsg_pid
);
2011 static abi_long
host_to_target_for_each_nlmsg(struct nlmsghdr
*nlh
,
2013 abi_long (*host_to_target_nlmsg
)
2014 (struct nlmsghdr
*))
2019 while (len
> sizeof(struct nlmsghdr
)) {
2021 nlmsg_len
= nlh
->nlmsg_len
;
2022 if (nlmsg_len
< sizeof(struct nlmsghdr
) ||
2027 switch (nlh
->nlmsg_type
) {
2029 tswap_nlmsghdr(nlh
);
2035 struct nlmsgerr
*e
= NLMSG_DATA(nlh
);
2036 e
->error
= tswap32(e
->error
);
2037 tswap_nlmsghdr(&e
->msg
);
2038 tswap_nlmsghdr(nlh
);
2042 ret
= host_to_target_nlmsg(nlh
);
2044 tswap_nlmsghdr(nlh
);
2049 tswap_nlmsghdr(nlh
);
2050 len
-= NLMSG_ALIGN(nlmsg_len
);
2051 nlh
= (struct nlmsghdr
*)(((char*)nlh
) + NLMSG_ALIGN(nlmsg_len
));
2056 static abi_long
target_to_host_for_each_nlmsg(struct nlmsghdr
*nlh
,
2058 abi_long (*target_to_host_nlmsg
)
2059 (struct nlmsghdr
*))
2063 while (len
> sizeof(struct nlmsghdr
)) {
2064 if (tswap32(nlh
->nlmsg_len
) < sizeof(struct nlmsghdr
) ||
2065 tswap32(nlh
->nlmsg_len
) > len
) {
2068 tswap_nlmsghdr(nlh
);
2069 switch (nlh
->nlmsg_type
) {
2076 struct nlmsgerr
*e
= NLMSG_DATA(nlh
);
2077 e
->error
= tswap32(e
->error
);
2078 tswap_nlmsghdr(&e
->msg
);
2082 ret
= target_to_host_nlmsg(nlh
);
2087 len
-= NLMSG_ALIGN(nlh
->nlmsg_len
);
2088 nlh
= (struct nlmsghdr
*)(((char *)nlh
) + NLMSG_ALIGN(nlh
->nlmsg_len
));
2093 #ifdef CONFIG_RTNETLINK
2094 static abi_long
host_to_target_for_each_nlattr(struct nlattr
*nlattr
,
2095 size_t len
, void *context
,
2096 abi_long (*host_to_target_nlattr
)
2100 unsigned short nla_len
;
2103 while (len
> sizeof(struct nlattr
)) {
2104 nla_len
= nlattr
->nla_len
;
2105 if (nla_len
< sizeof(struct nlattr
) ||
2109 ret
= host_to_target_nlattr(nlattr
, context
);
2110 nlattr
->nla_len
= tswap16(nlattr
->nla_len
);
2111 nlattr
->nla_type
= tswap16(nlattr
->nla_type
);
2115 len
-= NLA_ALIGN(nla_len
);
2116 nlattr
= (struct nlattr
*)(((char *)nlattr
) + NLA_ALIGN(nla_len
));
2121 static abi_long
host_to_target_for_each_rtattr(struct rtattr
*rtattr
,
2123 abi_long (*host_to_target_rtattr
)
2126 unsigned short rta_len
;
2129 while (len
> sizeof(struct rtattr
)) {
2130 rta_len
= rtattr
->rta_len
;
2131 if (rta_len
< sizeof(struct rtattr
) ||
2135 ret
= host_to_target_rtattr(rtattr
);
2136 rtattr
->rta_len
= tswap16(rtattr
->rta_len
);
2137 rtattr
->rta_type
= tswap16(rtattr
->rta_type
);
2141 len
-= RTA_ALIGN(rta_len
);
2142 rtattr
= (struct rtattr
*)(((char *)rtattr
) + RTA_ALIGN(rta_len
));
2147 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
2149 static abi_long
host_to_target_data_bridge_nlattr(struct nlattr
*nlattr
,
2156 switch (nlattr
->nla_type
) {
2158 case QEMU_IFLA_BR_FDB_FLUSH
:
2161 case QEMU_IFLA_BR_GROUP_ADDR
:
2164 case QEMU_IFLA_BR_VLAN_FILTERING
:
2165 case QEMU_IFLA_BR_TOPOLOGY_CHANGE
:
2166 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED
:
2167 case QEMU_IFLA_BR_MCAST_ROUTER
:
2168 case QEMU_IFLA_BR_MCAST_SNOOPING
:
2169 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR
:
2170 case QEMU_IFLA_BR_MCAST_QUERIER
:
2171 case QEMU_IFLA_BR_NF_CALL_IPTABLES
:
2172 case QEMU_IFLA_BR_NF_CALL_IP6TABLES
:
2173 case QEMU_IFLA_BR_NF_CALL_ARPTABLES
:
2176 case QEMU_IFLA_BR_PRIORITY
:
2177 case QEMU_IFLA_BR_VLAN_PROTOCOL
:
2178 case QEMU_IFLA_BR_GROUP_FWD_MASK
:
2179 case QEMU_IFLA_BR_ROOT_PORT
:
2180 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID
:
2181 u16
= NLA_DATA(nlattr
);
2182 *u16
= tswap16(*u16
);
2185 case QEMU_IFLA_BR_FORWARD_DELAY
:
2186 case QEMU_IFLA_BR_HELLO_TIME
:
2187 case QEMU_IFLA_BR_MAX_AGE
:
2188 case QEMU_IFLA_BR_AGEING_TIME
:
2189 case QEMU_IFLA_BR_STP_STATE
:
2190 case QEMU_IFLA_BR_ROOT_PATH_COST
:
2191 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY
:
2192 case QEMU_IFLA_BR_MCAST_HASH_MAX
:
2193 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT
:
2194 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT
:
2195 u32
= NLA_DATA(nlattr
);
2196 *u32
= tswap32(*u32
);
2199 case QEMU_IFLA_BR_HELLO_TIMER
:
2200 case QEMU_IFLA_BR_TCN_TIMER
:
2201 case QEMU_IFLA_BR_GC_TIMER
:
2202 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER
:
2203 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL
:
2204 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL
:
2205 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL
:
2206 case QEMU_IFLA_BR_MCAST_QUERY_INTVL
:
2207 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL
:
2208 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL
:
2209 u64
= NLA_DATA(nlattr
);
2210 *u64
= tswap64(*u64
);
2212 /* ifla_bridge_id: uin8_t[] */
2213 case QEMU_IFLA_BR_ROOT_ID
:
2214 case QEMU_IFLA_BR_BRIDGE_ID
:
2217 gemu_log("Unknown QEMU_IFLA_BR type %d\n", nlattr
->nla_type
);
2223 static abi_long
host_to_target_slave_data_bridge_nlattr(struct nlattr
*nlattr
,
2230 switch (nlattr
->nla_type
) {
2232 case QEMU_IFLA_BRPORT_STATE
:
2233 case QEMU_IFLA_BRPORT_MODE
:
2234 case QEMU_IFLA_BRPORT_GUARD
:
2235 case QEMU_IFLA_BRPORT_PROTECT
:
2236 case QEMU_IFLA_BRPORT_FAST_LEAVE
:
2237 case QEMU_IFLA_BRPORT_LEARNING
:
2238 case QEMU_IFLA_BRPORT_UNICAST_FLOOD
:
2239 case QEMU_IFLA_BRPORT_PROXYARP
:
2240 case QEMU_IFLA_BRPORT_LEARNING_SYNC
:
2241 case QEMU_IFLA_BRPORT_PROXYARP_WIFI
:
2242 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK
:
2243 case QEMU_IFLA_BRPORT_CONFIG_PENDING
:
2244 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER
:
2247 case QEMU_IFLA_BRPORT_PRIORITY
:
2248 case QEMU_IFLA_BRPORT_DESIGNATED_PORT
:
2249 case QEMU_IFLA_BRPORT_DESIGNATED_COST
:
2250 case QEMU_IFLA_BRPORT_ID
:
2251 case QEMU_IFLA_BRPORT_NO
:
2252 u16
= NLA_DATA(nlattr
);
2253 *u16
= tswap16(*u16
);
2256 case QEMU_IFLA_BRPORT_COST
:
2257 u32
= NLA_DATA(nlattr
);
2258 *u32
= tswap32(*u32
);
2261 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER
:
2262 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER
:
2263 case QEMU_IFLA_BRPORT_HOLD_TIMER
:
2264 u64
= NLA_DATA(nlattr
);
2265 *u64
= tswap64(*u64
);
2267 /* ifla_bridge_id: uint8_t[] */
2268 case QEMU_IFLA_BRPORT_ROOT_ID
:
2269 case QEMU_IFLA_BRPORT_BRIDGE_ID
:
2272 gemu_log("Unknown QEMU_IFLA_BRPORT type %d\n", nlattr
->nla_type
);
2278 struct linkinfo_context
{
2285 static abi_long
host_to_target_data_linkinfo_nlattr(struct nlattr
*nlattr
,
2288 struct linkinfo_context
*li_context
= context
;
2290 switch (nlattr
->nla_type
) {
2292 case QEMU_IFLA_INFO_KIND
:
2293 li_context
->name
= NLA_DATA(nlattr
);
2294 li_context
->len
= nlattr
->nla_len
- NLA_HDRLEN
;
2296 case QEMU_IFLA_INFO_SLAVE_KIND
:
2297 li_context
->slave_name
= NLA_DATA(nlattr
);
2298 li_context
->slave_len
= nlattr
->nla_len
- NLA_HDRLEN
;
2301 case QEMU_IFLA_INFO_XSTATS
:
2302 /* FIXME: only used by CAN */
2305 case QEMU_IFLA_INFO_DATA
:
2306 if (strncmp(li_context
->name
, "bridge",
2307 li_context
->len
) == 0) {
2308 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
),
2311 host_to_target_data_bridge_nlattr
);
2313 gemu_log("Unknown QEMU_IFLA_INFO_KIND %s\n", li_context
->name
);
2316 case QEMU_IFLA_INFO_SLAVE_DATA
:
2317 if (strncmp(li_context
->slave_name
, "bridge",
2318 li_context
->slave_len
) == 0) {
2319 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
),
2322 host_to_target_slave_data_bridge_nlattr
);
2324 gemu_log("Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
2325 li_context
->slave_name
);
2329 gemu_log("Unknown host QEMU_IFLA_INFO type: %d\n", nlattr
->nla_type
);
2336 static abi_long
host_to_target_data_inet_nlattr(struct nlattr
*nlattr
,
2342 switch (nlattr
->nla_type
) {
2343 case QEMU_IFLA_INET_CONF
:
2344 u32
= NLA_DATA(nlattr
);
2345 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u32
);
2347 u32
[i
] = tswap32(u32
[i
]);
2351 gemu_log("Unknown host AF_INET type: %d\n", nlattr
->nla_type
);
2356 static abi_long
host_to_target_data_inet6_nlattr(struct nlattr
*nlattr
,
2361 struct ifla_cacheinfo
*ci
;
2364 switch (nlattr
->nla_type
) {
2366 case QEMU_IFLA_INET6_TOKEN
:
2369 case QEMU_IFLA_INET6_ADDR_GEN_MODE
:
2372 case QEMU_IFLA_INET6_FLAGS
:
2373 u32
= NLA_DATA(nlattr
);
2374 *u32
= tswap32(*u32
);
2377 case QEMU_IFLA_INET6_CONF
:
2378 u32
= NLA_DATA(nlattr
);
2379 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u32
);
2381 u32
[i
] = tswap32(u32
[i
]);
2384 /* ifla_cacheinfo */
2385 case QEMU_IFLA_INET6_CACHEINFO
:
2386 ci
= NLA_DATA(nlattr
);
2387 ci
->max_reasm_len
= tswap32(ci
->max_reasm_len
);
2388 ci
->tstamp
= tswap32(ci
->tstamp
);
2389 ci
->reachable_time
= tswap32(ci
->reachable_time
);
2390 ci
->retrans_time
= tswap32(ci
->retrans_time
);
2393 case QEMU_IFLA_INET6_STATS
:
2394 case QEMU_IFLA_INET6_ICMP6STATS
:
2395 u64
= NLA_DATA(nlattr
);
2396 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u64
);
2398 u64
[i
] = tswap64(u64
[i
]);
2402 gemu_log("Unknown host AF_INET6 type: %d\n", nlattr
->nla_type
);
2407 static abi_long
host_to_target_data_spec_nlattr(struct nlattr
*nlattr
,
2410 switch (nlattr
->nla_type
) {
2412 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
), nlattr
->nla_len
,
2414 host_to_target_data_inet_nlattr
);
2416 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
), nlattr
->nla_len
,
2418 host_to_target_data_inet6_nlattr
);
2420 gemu_log("Unknown host AF_SPEC type: %d\n", nlattr
->nla_type
);
2426 static abi_long
host_to_target_data_link_rtattr(struct rtattr
*rtattr
)
2429 struct rtnl_link_stats
*st
;
2430 struct rtnl_link_stats64
*st64
;
2431 struct rtnl_link_ifmap
*map
;
2432 struct linkinfo_context li_context
;
2434 switch (rtattr
->rta_type
) {
2436 case QEMU_IFLA_ADDRESS
:
2437 case QEMU_IFLA_BROADCAST
:
2439 case QEMU_IFLA_IFNAME
:
2440 case QEMU_IFLA_QDISC
:
2443 case QEMU_IFLA_OPERSTATE
:
2444 case QEMU_IFLA_LINKMODE
:
2445 case QEMU_IFLA_CARRIER
:
2446 case QEMU_IFLA_PROTO_DOWN
:
2450 case QEMU_IFLA_LINK
:
2451 case QEMU_IFLA_WEIGHT
:
2452 case QEMU_IFLA_TXQLEN
:
2453 case QEMU_IFLA_CARRIER_CHANGES
:
2454 case QEMU_IFLA_NUM_RX_QUEUES
:
2455 case QEMU_IFLA_NUM_TX_QUEUES
:
2456 case QEMU_IFLA_PROMISCUITY
:
2457 case QEMU_IFLA_EXT_MASK
:
2458 case QEMU_IFLA_LINK_NETNSID
:
2459 case QEMU_IFLA_GROUP
:
2460 case QEMU_IFLA_MASTER
:
2461 case QEMU_IFLA_NUM_VF
:
2462 case QEMU_IFLA_GSO_MAX_SEGS
:
2463 case QEMU_IFLA_GSO_MAX_SIZE
:
2464 u32
= RTA_DATA(rtattr
);
2465 *u32
= tswap32(*u32
);
2467 /* struct rtnl_link_stats */
2468 case QEMU_IFLA_STATS
:
2469 st
= RTA_DATA(rtattr
);
2470 st
->rx_packets
= tswap32(st
->rx_packets
);
2471 st
->tx_packets
= tswap32(st
->tx_packets
);
2472 st
->rx_bytes
= tswap32(st
->rx_bytes
);
2473 st
->tx_bytes
= tswap32(st
->tx_bytes
);
2474 st
->rx_errors
= tswap32(st
->rx_errors
);
2475 st
->tx_errors
= tswap32(st
->tx_errors
);
2476 st
->rx_dropped
= tswap32(st
->rx_dropped
);
2477 st
->tx_dropped
= tswap32(st
->tx_dropped
);
2478 st
->multicast
= tswap32(st
->multicast
);
2479 st
->collisions
= tswap32(st
->collisions
);
2481 /* detailed rx_errors: */
2482 st
->rx_length_errors
= tswap32(st
->rx_length_errors
);
2483 st
->rx_over_errors
= tswap32(st
->rx_over_errors
);
2484 st
->rx_crc_errors
= tswap32(st
->rx_crc_errors
);
2485 st
->rx_frame_errors
= tswap32(st
->rx_frame_errors
);
2486 st
->rx_fifo_errors
= tswap32(st
->rx_fifo_errors
);
2487 st
->rx_missed_errors
= tswap32(st
->rx_missed_errors
);
2489 /* detailed tx_errors */
2490 st
->tx_aborted_errors
= tswap32(st
->tx_aborted_errors
);
2491 st
->tx_carrier_errors
= tswap32(st
->tx_carrier_errors
);
2492 st
->tx_fifo_errors
= tswap32(st
->tx_fifo_errors
);
2493 st
->tx_heartbeat_errors
= tswap32(st
->tx_heartbeat_errors
);
2494 st
->tx_window_errors
= tswap32(st
->tx_window_errors
);
2497 st
->rx_compressed
= tswap32(st
->rx_compressed
);
2498 st
->tx_compressed
= tswap32(st
->tx_compressed
);
2500 /* struct rtnl_link_stats64 */
2501 case QEMU_IFLA_STATS64
:
2502 st64
= RTA_DATA(rtattr
);
2503 st64
->rx_packets
= tswap64(st64
->rx_packets
);
2504 st64
->tx_packets
= tswap64(st64
->tx_packets
);
2505 st64
->rx_bytes
= tswap64(st64
->rx_bytes
);
2506 st64
->tx_bytes
= tswap64(st64
->tx_bytes
);
2507 st64
->rx_errors
= tswap64(st64
->rx_errors
);
2508 st64
->tx_errors
= tswap64(st64
->tx_errors
);
2509 st64
->rx_dropped
= tswap64(st64
->rx_dropped
);
2510 st64
->tx_dropped
= tswap64(st64
->tx_dropped
);
2511 st64
->multicast
= tswap64(st64
->multicast
);
2512 st64
->collisions
= tswap64(st64
->collisions
);
2514 /* detailed rx_errors: */
2515 st64
->rx_length_errors
= tswap64(st64
->rx_length_errors
);
2516 st64
->rx_over_errors
= tswap64(st64
->rx_over_errors
);
2517 st64
->rx_crc_errors
= tswap64(st64
->rx_crc_errors
);
2518 st64
->rx_frame_errors
= tswap64(st64
->rx_frame_errors
);
2519 st64
->rx_fifo_errors
= tswap64(st64
->rx_fifo_errors
);
2520 st64
->rx_missed_errors
= tswap64(st64
->rx_missed_errors
);
2522 /* detailed tx_errors */
2523 st64
->tx_aborted_errors
= tswap64(st64
->tx_aborted_errors
);
2524 st64
->tx_carrier_errors
= tswap64(st64
->tx_carrier_errors
);
2525 st64
->tx_fifo_errors
= tswap64(st64
->tx_fifo_errors
);
2526 st64
->tx_heartbeat_errors
= tswap64(st64
->tx_heartbeat_errors
);
2527 st64
->tx_window_errors
= tswap64(st64
->tx_window_errors
);
2530 st64
->rx_compressed
= tswap64(st64
->rx_compressed
);
2531 st64
->tx_compressed
= tswap64(st64
->tx_compressed
);
2533 /* struct rtnl_link_ifmap */
2535 map
= RTA_DATA(rtattr
);
2536 map
->mem_start
= tswap64(map
->mem_start
);
2537 map
->mem_end
= tswap64(map
->mem_end
);
2538 map
->base_addr
= tswap64(map
->base_addr
);
2539 map
->irq
= tswap16(map
->irq
);
2542 case QEMU_IFLA_LINKINFO
:
2543 memset(&li_context
, 0, sizeof(li_context
));
2544 return host_to_target_for_each_nlattr(RTA_DATA(rtattr
), rtattr
->rta_len
,
2546 host_to_target_data_linkinfo_nlattr
);
2547 case QEMU_IFLA_AF_SPEC
:
2548 return host_to_target_for_each_nlattr(RTA_DATA(rtattr
), rtattr
->rta_len
,
2550 host_to_target_data_spec_nlattr
);
2552 gemu_log("Unknown host QEMU_IFLA type: %d\n", rtattr
->rta_type
);
2558 static abi_long
host_to_target_data_addr_rtattr(struct rtattr
*rtattr
)
2561 struct ifa_cacheinfo
*ci
;
2563 switch (rtattr
->rta_type
) {
2564 /* binary: depends on family type */
2574 u32
= RTA_DATA(rtattr
);
2575 *u32
= tswap32(*u32
);
2577 /* struct ifa_cacheinfo */
2579 ci
= RTA_DATA(rtattr
);
2580 ci
->ifa_prefered
= tswap32(ci
->ifa_prefered
);
2581 ci
->ifa_valid
= tswap32(ci
->ifa_valid
);
2582 ci
->cstamp
= tswap32(ci
->cstamp
);
2583 ci
->tstamp
= tswap32(ci
->tstamp
);
2586 gemu_log("Unknown host IFA type: %d\n", rtattr
->rta_type
);
2592 static abi_long
host_to_target_data_route_rtattr(struct rtattr
*rtattr
)
2595 switch (rtattr
->rta_type
) {
2596 /* binary: depends on family type */
2605 u32
= RTA_DATA(rtattr
);
2606 *u32
= tswap32(*u32
);
2609 gemu_log("Unknown host RTA type: %d\n", rtattr
->rta_type
);
2615 static abi_long
host_to_target_link_rtattr(struct rtattr
*rtattr
,
2616 uint32_t rtattr_len
)
2618 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
2619 host_to_target_data_link_rtattr
);
2622 static abi_long
host_to_target_addr_rtattr(struct rtattr
*rtattr
,
2623 uint32_t rtattr_len
)
2625 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
2626 host_to_target_data_addr_rtattr
);
2629 static abi_long
host_to_target_route_rtattr(struct rtattr
*rtattr
,
2630 uint32_t rtattr_len
)
2632 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
2633 host_to_target_data_route_rtattr
);
2636 static abi_long
host_to_target_data_route(struct nlmsghdr
*nlh
)
2639 struct ifinfomsg
*ifi
;
2640 struct ifaddrmsg
*ifa
;
2643 nlmsg_len
= nlh
->nlmsg_len
;
2644 switch (nlh
->nlmsg_type
) {
2648 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifi
))) {
2649 ifi
= NLMSG_DATA(nlh
);
2650 ifi
->ifi_type
= tswap16(ifi
->ifi_type
);
2651 ifi
->ifi_index
= tswap32(ifi
->ifi_index
);
2652 ifi
->ifi_flags
= tswap32(ifi
->ifi_flags
);
2653 ifi
->ifi_change
= tswap32(ifi
->ifi_change
);
2654 host_to_target_link_rtattr(IFLA_RTA(ifi
),
2655 nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
2661 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifa
))) {
2662 ifa
= NLMSG_DATA(nlh
);
2663 ifa
->ifa_index
= tswap32(ifa
->ifa_index
);
2664 host_to_target_addr_rtattr(IFA_RTA(ifa
),
2665 nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
2671 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*rtm
))) {
2672 rtm
= NLMSG_DATA(nlh
);
2673 rtm
->rtm_flags
= tswap32(rtm
->rtm_flags
);
2674 host_to_target_route_rtattr(RTM_RTA(rtm
),
2675 nlmsg_len
- NLMSG_LENGTH(sizeof(*rtm
)));
2679 return -TARGET_EINVAL
;
2684 static inline abi_long
host_to_target_nlmsg_route(struct nlmsghdr
*nlh
,
2687 return host_to_target_for_each_nlmsg(nlh
, len
, host_to_target_data_route
);
2690 static abi_long
target_to_host_for_each_rtattr(struct rtattr
*rtattr
,
2692 abi_long (*target_to_host_rtattr
)
2697 while (len
>= sizeof(struct rtattr
)) {
2698 if (tswap16(rtattr
->rta_len
) < sizeof(struct rtattr
) ||
2699 tswap16(rtattr
->rta_len
) > len
) {
2702 rtattr
->rta_len
= tswap16(rtattr
->rta_len
);
2703 rtattr
->rta_type
= tswap16(rtattr
->rta_type
);
2704 ret
= target_to_host_rtattr(rtattr
);
2708 len
-= RTA_ALIGN(rtattr
->rta_len
);
2709 rtattr
= (struct rtattr
*)(((char *)rtattr
) +
2710 RTA_ALIGN(rtattr
->rta_len
));
2715 static abi_long
target_to_host_data_link_rtattr(struct rtattr
*rtattr
)
2717 switch (rtattr
->rta_type
) {
2719 gemu_log("Unknown target QEMU_IFLA type: %d\n", rtattr
->rta_type
);
2725 static abi_long
target_to_host_data_addr_rtattr(struct rtattr
*rtattr
)
2727 switch (rtattr
->rta_type
) {
2728 /* binary: depends on family type */
2733 gemu_log("Unknown target IFA type: %d\n", rtattr
->rta_type
);
2739 static abi_long
target_to_host_data_route_rtattr(struct rtattr
*rtattr
)
2742 switch (rtattr
->rta_type
) {
2743 /* binary: depends on family type */
2751 u32
= RTA_DATA(rtattr
);
2752 *u32
= tswap32(*u32
);
2755 gemu_log("Unknown target RTA type: %d\n", rtattr
->rta_type
);
2761 static void target_to_host_link_rtattr(struct rtattr
*rtattr
,
2762 uint32_t rtattr_len
)
2764 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
2765 target_to_host_data_link_rtattr
);
2768 static void target_to_host_addr_rtattr(struct rtattr
*rtattr
,
2769 uint32_t rtattr_len
)
2771 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
2772 target_to_host_data_addr_rtattr
);
2775 static void target_to_host_route_rtattr(struct rtattr
*rtattr
,
2776 uint32_t rtattr_len
)
2778 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
2779 target_to_host_data_route_rtattr
);
2782 static abi_long
target_to_host_data_route(struct nlmsghdr
*nlh
)
2784 struct ifinfomsg
*ifi
;
2785 struct ifaddrmsg
*ifa
;
2788 switch (nlh
->nlmsg_type
) {
2793 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifi
))) {
2794 ifi
= NLMSG_DATA(nlh
);
2795 ifi
->ifi_type
= tswap16(ifi
->ifi_type
);
2796 ifi
->ifi_index
= tswap32(ifi
->ifi_index
);
2797 ifi
->ifi_flags
= tswap32(ifi
->ifi_flags
);
2798 ifi
->ifi_change
= tswap32(ifi
->ifi_change
);
2799 target_to_host_link_rtattr(IFLA_RTA(ifi
), nlh
->nlmsg_len
-
2800 NLMSG_LENGTH(sizeof(*ifi
)));
2806 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifa
))) {
2807 ifa
= NLMSG_DATA(nlh
);
2808 ifa
->ifa_index
= tswap32(ifa
->ifa_index
);
2809 target_to_host_addr_rtattr(IFA_RTA(ifa
), nlh
->nlmsg_len
-
2810 NLMSG_LENGTH(sizeof(*ifa
)));
2817 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*rtm
))) {
2818 rtm
= NLMSG_DATA(nlh
);
2819 rtm
->rtm_flags
= tswap32(rtm
->rtm_flags
);
2820 target_to_host_route_rtattr(RTM_RTA(rtm
), nlh
->nlmsg_len
-
2821 NLMSG_LENGTH(sizeof(*rtm
)));
2825 return -TARGET_EOPNOTSUPP
;
2830 static abi_long
target_to_host_nlmsg_route(struct nlmsghdr
*nlh
, size_t len
)
2832 return target_to_host_for_each_nlmsg(nlh
, len
, target_to_host_data_route
);
2834 #endif /* CONFIG_RTNETLINK */
2836 static abi_long
host_to_target_data_audit(struct nlmsghdr
*nlh
)
2838 switch (nlh
->nlmsg_type
) {
2840 gemu_log("Unknown host audit message type %d\n",
2842 return -TARGET_EINVAL
;
2847 static inline abi_long
host_to_target_nlmsg_audit(struct nlmsghdr
*nlh
,
2850 return host_to_target_for_each_nlmsg(nlh
, len
, host_to_target_data_audit
);
2853 static abi_long
target_to_host_data_audit(struct nlmsghdr
*nlh
)
2855 switch (nlh
->nlmsg_type
) {
2857 case AUDIT_FIRST_USER_MSG
... AUDIT_LAST_USER_MSG
:
2858 case AUDIT_FIRST_USER_MSG2
... AUDIT_LAST_USER_MSG2
:
2861 gemu_log("Unknown target audit message type %d\n",
2863 return -TARGET_EINVAL
;
2869 static abi_long
target_to_host_nlmsg_audit(struct nlmsghdr
*nlh
, size_t len
)
2871 return target_to_host_for_each_nlmsg(nlh
, len
, target_to_host_data_audit
);
2874 /* do_setsockopt() Must return target values and target errnos. */
2875 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
2876 abi_ulong optval_addr
, socklen_t optlen
)
2880 struct ip_mreqn
*ip_mreq
;
2881 struct ip_mreq_source
*ip_mreq_source
;
2885 /* TCP options all take an 'int' value. */
2886 if (optlen
< sizeof(uint32_t))
2887 return -TARGET_EINVAL
;
2889 if (get_user_u32(val
, optval_addr
))
2890 return -TARGET_EFAULT
;
2891 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2898 case IP_ROUTER_ALERT
:
2902 case IP_MTU_DISCOVER
:
2909 case IP_MULTICAST_TTL
:
2910 case IP_MULTICAST_LOOP
:
2912 if (optlen
>= sizeof(uint32_t)) {
2913 if (get_user_u32(val
, optval_addr
))
2914 return -TARGET_EFAULT
;
2915 } else if (optlen
>= 1) {
2916 if (get_user_u8(val
, optval_addr
))
2917 return -TARGET_EFAULT
;
2919 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2921 case IP_ADD_MEMBERSHIP
:
2922 case IP_DROP_MEMBERSHIP
:
2923 if (optlen
< sizeof (struct target_ip_mreq
) ||
2924 optlen
> sizeof (struct target_ip_mreqn
))
2925 return -TARGET_EINVAL
;
2927 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
2928 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
2929 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
2932 case IP_BLOCK_SOURCE
:
2933 case IP_UNBLOCK_SOURCE
:
2934 case IP_ADD_SOURCE_MEMBERSHIP
:
2935 case IP_DROP_SOURCE_MEMBERSHIP
:
2936 if (optlen
!= sizeof (struct target_ip_mreq_source
))
2937 return -TARGET_EINVAL
;
2939 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2940 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
2941 unlock_user (ip_mreq_source
, optval_addr
, 0);
2950 case IPV6_MTU_DISCOVER
:
2953 case IPV6_RECVPKTINFO
:
2954 case IPV6_UNICAST_HOPS
:
2956 case IPV6_RECVHOPLIMIT
:
2957 case IPV6_2292HOPLIMIT
:
2960 if (optlen
< sizeof(uint32_t)) {
2961 return -TARGET_EINVAL
;
2963 if (get_user_u32(val
, optval_addr
)) {
2964 return -TARGET_EFAULT
;
2966 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2967 &val
, sizeof(val
)));
2971 struct in6_pktinfo pki
;
2973 if (optlen
< sizeof(pki
)) {
2974 return -TARGET_EINVAL
;
2977 if (copy_from_user(&pki
, optval_addr
, sizeof(pki
))) {
2978 return -TARGET_EFAULT
;
2981 pki
.ipi6_ifindex
= tswap32(pki
.ipi6_ifindex
);
2983 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2984 &pki
, sizeof(pki
)));
2995 struct icmp6_filter icmp6f
;
2997 if (optlen
> sizeof(icmp6f
)) {
2998 optlen
= sizeof(icmp6f
);
3001 if (copy_from_user(&icmp6f
, optval_addr
, optlen
)) {
3002 return -TARGET_EFAULT
;
3005 for (val
= 0; val
< 8; val
++) {
3006 icmp6f
.data
[val
] = tswap32(icmp6f
.data
[val
]);
3009 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
3021 /* those take an u32 value */
3022 if (optlen
< sizeof(uint32_t)) {
3023 return -TARGET_EINVAL
;
3026 if (get_user_u32(val
, optval_addr
)) {
3027 return -TARGET_EFAULT
;
3029 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
3030 &val
, sizeof(val
)));
3037 case TARGET_SOL_SOCKET
:
3039 case TARGET_SO_RCVTIMEO
:
3043 optname
= SO_RCVTIMEO
;
3046 if (optlen
!= sizeof(struct target_timeval
)) {
3047 return -TARGET_EINVAL
;
3050 if (copy_from_user_timeval(&tv
, optval_addr
)) {
3051 return -TARGET_EFAULT
;
3054 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
3058 case TARGET_SO_SNDTIMEO
:
3059 optname
= SO_SNDTIMEO
;
3061 case TARGET_SO_ATTACH_FILTER
:
3063 struct target_sock_fprog
*tfprog
;
3064 struct target_sock_filter
*tfilter
;
3065 struct sock_fprog fprog
;
3066 struct sock_filter
*filter
;
3069 if (optlen
!= sizeof(*tfprog
)) {
3070 return -TARGET_EINVAL
;
3072 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
3073 return -TARGET_EFAULT
;
3075 if (!lock_user_struct(VERIFY_READ
, tfilter
,
3076 tswapal(tfprog
->filter
), 0)) {
3077 unlock_user_struct(tfprog
, optval_addr
, 1);
3078 return -TARGET_EFAULT
;
3081 fprog
.len
= tswap16(tfprog
->len
);
3082 filter
= g_try_new(struct sock_filter
, fprog
.len
);
3083 if (filter
== NULL
) {
3084 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
3085 unlock_user_struct(tfprog
, optval_addr
, 1);
3086 return -TARGET_ENOMEM
;
3088 for (i
= 0; i
< fprog
.len
; i
++) {
3089 filter
[i
].code
= tswap16(tfilter
[i
].code
);
3090 filter
[i
].jt
= tfilter
[i
].jt
;
3091 filter
[i
].jf
= tfilter
[i
].jf
;
3092 filter
[i
].k
= tswap32(tfilter
[i
].k
);
3094 fprog
.filter
= filter
;
3096 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
3097 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
3100 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
3101 unlock_user_struct(tfprog
, optval_addr
, 1);
3104 case TARGET_SO_BINDTODEVICE
:
3106 char *dev_ifname
, *addr_ifname
;
3108 if (optlen
> IFNAMSIZ
- 1) {
3109 optlen
= IFNAMSIZ
- 1;
3111 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
3113 return -TARGET_EFAULT
;
3115 optname
= SO_BINDTODEVICE
;
3116 addr_ifname
= alloca(IFNAMSIZ
);
3117 memcpy(addr_ifname
, dev_ifname
, optlen
);
3118 addr_ifname
[optlen
] = 0;
3119 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
3120 addr_ifname
, optlen
));
3121 unlock_user (dev_ifname
, optval_addr
, 0);
3124 /* Options with 'int' argument. */
3125 case TARGET_SO_DEBUG
:
3128 case TARGET_SO_REUSEADDR
:
3129 optname
= SO_REUSEADDR
;
3131 case TARGET_SO_TYPE
:
3134 case TARGET_SO_ERROR
:
3137 case TARGET_SO_DONTROUTE
:
3138 optname
= SO_DONTROUTE
;
3140 case TARGET_SO_BROADCAST
:
3141 optname
= SO_BROADCAST
;
3143 case TARGET_SO_SNDBUF
:
3144 optname
= SO_SNDBUF
;
3146 case TARGET_SO_SNDBUFFORCE
:
3147 optname
= SO_SNDBUFFORCE
;
3149 case TARGET_SO_RCVBUF
:
3150 optname
= SO_RCVBUF
;
3152 case TARGET_SO_RCVBUFFORCE
:
3153 optname
= SO_RCVBUFFORCE
;
3155 case TARGET_SO_KEEPALIVE
:
3156 optname
= SO_KEEPALIVE
;
3158 case TARGET_SO_OOBINLINE
:
3159 optname
= SO_OOBINLINE
;
3161 case TARGET_SO_NO_CHECK
:
3162 optname
= SO_NO_CHECK
;
3164 case TARGET_SO_PRIORITY
:
3165 optname
= SO_PRIORITY
;
3168 case TARGET_SO_BSDCOMPAT
:
3169 optname
= SO_BSDCOMPAT
;
3172 case TARGET_SO_PASSCRED
:
3173 optname
= SO_PASSCRED
;
3175 case TARGET_SO_PASSSEC
:
3176 optname
= SO_PASSSEC
;
3178 case TARGET_SO_TIMESTAMP
:
3179 optname
= SO_TIMESTAMP
;
3181 case TARGET_SO_RCVLOWAT
:
3182 optname
= SO_RCVLOWAT
;
3187 if (optlen
< sizeof(uint32_t))
3188 return -TARGET_EINVAL
;
3190 if (get_user_u32(val
, optval_addr
))
3191 return -TARGET_EFAULT
;
3192 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
3196 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level
, optname
);
3197 ret
= -TARGET_ENOPROTOOPT
;
3202 /* do_getsockopt() Must return target values and target errnos. */
3203 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
3204 abi_ulong optval_addr
, abi_ulong optlen
)
3211 case TARGET_SOL_SOCKET
:
3214 /* These don't just return a single integer */
3215 case TARGET_SO_LINGER
:
3216 case TARGET_SO_RCVTIMEO
:
3217 case TARGET_SO_SNDTIMEO
:
3218 case TARGET_SO_PEERNAME
:
3220 case TARGET_SO_PEERCRED
: {
3223 struct target_ucred
*tcr
;
3225 if (get_user_u32(len
, optlen
)) {
3226 return -TARGET_EFAULT
;
3229 return -TARGET_EINVAL
;
3233 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
3241 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
3242 return -TARGET_EFAULT
;
3244 __put_user(cr
.pid
, &tcr
->pid
);
3245 __put_user(cr
.uid
, &tcr
->uid
);
3246 __put_user(cr
.gid
, &tcr
->gid
);
3247 unlock_user_struct(tcr
, optval_addr
, 1);
3248 if (put_user_u32(len
, optlen
)) {
3249 return -TARGET_EFAULT
;
3253 /* Options with 'int' argument. */
3254 case TARGET_SO_DEBUG
:
3257 case TARGET_SO_REUSEADDR
:
3258 optname
= SO_REUSEADDR
;
3260 case TARGET_SO_TYPE
:
3263 case TARGET_SO_ERROR
:
3266 case TARGET_SO_DONTROUTE
:
3267 optname
= SO_DONTROUTE
;
3269 case TARGET_SO_BROADCAST
:
3270 optname
= SO_BROADCAST
;
3272 case TARGET_SO_SNDBUF
:
3273 optname
= SO_SNDBUF
;
3275 case TARGET_SO_RCVBUF
:
3276 optname
= SO_RCVBUF
;
3278 case TARGET_SO_KEEPALIVE
:
3279 optname
= SO_KEEPALIVE
;
3281 case TARGET_SO_OOBINLINE
:
3282 optname
= SO_OOBINLINE
;
3284 case TARGET_SO_NO_CHECK
:
3285 optname
= SO_NO_CHECK
;
3287 case TARGET_SO_PRIORITY
:
3288 optname
= SO_PRIORITY
;
3291 case TARGET_SO_BSDCOMPAT
:
3292 optname
= SO_BSDCOMPAT
;
3295 case TARGET_SO_PASSCRED
:
3296 optname
= SO_PASSCRED
;
3298 case TARGET_SO_TIMESTAMP
:
3299 optname
= SO_TIMESTAMP
;
3301 case TARGET_SO_RCVLOWAT
:
3302 optname
= SO_RCVLOWAT
;
3304 case TARGET_SO_ACCEPTCONN
:
3305 optname
= SO_ACCEPTCONN
;
3312 /* TCP options all take an 'int' value. */
3314 if (get_user_u32(len
, optlen
))
3315 return -TARGET_EFAULT
;
3317 return -TARGET_EINVAL
;
3319 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
3322 if (optname
== SO_TYPE
) {
3323 val
= host_to_target_sock_type(val
);
3328 if (put_user_u32(val
, optval_addr
))
3329 return -TARGET_EFAULT
;
3331 if (put_user_u8(val
, optval_addr
))
3332 return -TARGET_EFAULT
;
3334 if (put_user_u32(len
, optlen
))
3335 return -TARGET_EFAULT
;
3342 case IP_ROUTER_ALERT
:
3346 case IP_MTU_DISCOVER
:
3352 case IP_MULTICAST_TTL
:
3353 case IP_MULTICAST_LOOP
:
3354 if (get_user_u32(len
, optlen
))
3355 return -TARGET_EFAULT
;
3357 return -TARGET_EINVAL
;
3359 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
3362 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
3364 if (put_user_u32(len
, optlen
)
3365 || put_user_u8(val
, optval_addr
))
3366 return -TARGET_EFAULT
;
3368 if (len
> sizeof(int))
3370 if (put_user_u32(len
, optlen
)
3371 || put_user_u32(val
, optval_addr
))
3372 return -TARGET_EFAULT
;
3376 ret
= -TARGET_ENOPROTOOPT
;
3382 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
3384 ret
= -TARGET_EOPNOTSUPP
;
3390 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
3391 abi_ulong count
, int copy
)
3393 struct target_iovec
*target_vec
;
3395 abi_ulong total_len
, max_len
;
3398 bool bad_address
= false;
3404 if (count
> IOV_MAX
) {
3409 vec
= g_try_new0(struct iovec
, count
);
3415 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3416 count
* sizeof(struct target_iovec
), 1);
3417 if (target_vec
== NULL
) {
3422 /* ??? If host page size > target page size, this will result in a
3423 value larger than what we can actually support. */
3424 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
3427 for (i
= 0; i
< count
; i
++) {
3428 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3429 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3434 } else if (len
== 0) {
3435 /* Zero length pointer is ignored. */
3436 vec
[i
].iov_base
= 0;
3438 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
3439 /* If the first buffer pointer is bad, this is a fault. But
3440 * subsequent bad buffers will result in a partial write; this
3441 * is realized by filling the vector with null pointers and
3443 if (!vec
[i
].iov_base
) {
3454 if (len
> max_len
- total_len
) {
3455 len
= max_len
- total_len
;
3458 vec
[i
].iov_len
= len
;
3462 unlock_user(target_vec
, target_addr
, 0);
3467 if (tswapal(target_vec
[i
].iov_len
) > 0) {
3468 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
3471 unlock_user(target_vec
, target_addr
, 0);
3478 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
3479 abi_ulong count
, int copy
)
3481 struct target_iovec
*target_vec
;
3484 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3485 count
* sizeof(struct target_iovec
), 1);
3487 for (i
= 0; i
< count
; i
++) {
3488 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3489 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3493 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
3495 unlock_user(target_vec
, target_addr
, 0);
3501 static inline int target_to_host_sock_type(int *type
)
3504 int target_type
= *type
;
3506 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
3507 case TARGET_SOCK_DGRAM
:
3508 host_type
= SOCK_DGRAM
;
3510 case TARGET_SOCK_STREAM
:
3511 host_type
= SOCK_STREAM
;
3514 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
3517 if (target_type
& TARGET_SOCK_CLOEXEC
) {
3518 #if defined(SOCK_CLOEXEC)
3519 host_type
|= SOCK_CLOEXEC
;
3521 return -TARGET_EINVAL
;
3524 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3525 #if defined(SOCK_NONBLOCK)
3526 host_type
|= SOCK_NONBLOCK
;
3527 #elif !defined(O_NONBLOCK)
3528 return -TARGET_EINVAL
;
3535 /* Try to emulate socket type flags after socket creation. */
3536 static int sock_flags_fixup(int fd
, int target_type
)
3538 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3539 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3540 int flags
= fcntl(fd
, F_GETFL
);
3541 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
3543 return -TARGET_EINVAL
;
3550 static abi_long
packet_target_to_host_sockaddr(void *host_addr
,
3551 abi_ulong target_addr
,
3554 struct sockaddr
*addr
= host_addr
;
3555 struct target_sockaddr
*target_saddr
;
3557 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
3558 if (!target_saddr
) {
3559 return -TARGET_EFAULT
;
3562 memcpy(addr
, target_saddr
, len
);
3563 addr
->sa_family
= tswap16(target_saddr
->sa_family
);
3564 /* spkt_protocol is big-endian */
3566 unlock_user(target_saddr
, target_addr
, 0);
3570 static TargetFdTrans target_packet_trans
= {
3571 .target_to_host_addr
= packet_target_to_host_sockaddr
,
3574 #ifdef CONFIG_RTNETLINK
3575 static abi_long
netlink_route_target_to_host(void *buf
, size_t len
)
3579 ret
= target_to_host_nlmsg_route(buf
, len
);
3587 static abi_long
netlink_route_host_to_target(void *buf
, size_t len
)
3591 ret
= host_to_target_nlmsg_route(buf
, len
);
3599 static TargetFdTrans target_netlink_route_trans
= {
3600 .target_to_host_data
= netlink_route_target_to_host
,
3601 .host_to_target_data
= netlink_route_host_to_target
,
3603 #endif /* CONFIG_RTNETLINK */
3605 static abi_long
netlink_audit_target_to_host(void *buf
, size_t len
)
3609 ret
= target_to_host_nlmsg_audit(buf
, len
);
3617 static abi_long
netlink_audit_host_to_target(void *buf
, size_t len
)
3621 ret
= host_to_target_nlmsg_audit(buf
, len
);
3629 static TargetFdTrans target_netlink_audit_trans
= {
3630 .target_to_host_data
= netlink_audit_target_to_host
,
3631 .host_to_target_data
= netlink_audit_host_to_target
,
3634 /* do_socket() Must return target values and target errnos. */
3635 static abi_long
do_socket(int domain
, int type
, int protocol
)
3637 int target_type
= type
;
3640 ret
= target_to_host_sock_type(&type
);
3645 if (domain
== PF_NETLINK
&& !(
3646 #ifdef CONFIG_RTNETLINK
3647 protocol
== NETLINK_ROUTE
||
3649 protocol
== NETLINK_KOBJECT_UEVENT
||
3650 protocol
== NETLINK_AUDIT
)) {
3651 return -EPFNOSUPPORT
;
3654 if (domain
== AF_PACKET
||
3655 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
3656 protocol
= tswap16(protocol
);
3659 ret
= get_errno(socket(domain
, type
, protocol
));
3661 ret
= sock_flags_fixup(ret
, target_type
);
3662 if (type
== SOCK_PACKET
) {
3663 /* Manage an obsolete case :
3664 * if socket type is SOCK_PACKET, bind by name
3666 fd_trans_register(ret
, &target_packet_trans
);
3667 } else if (domain
== PF_NETLINK
) {
3669 #ifdef CONFIG_RTNETLINK
3671 fd_trans_register(ret
, &target_netlink_route_trans
);
3674 case NETLINK_KOBJECT_UEVENT
:
3675 /* nothing to do: messages are strings */
3678 fd_trans_register(ret
, &target_netlink_audit_trans
);
3681 g_assert_not_reached();
3688 /* do_bind() Must return target values and target errnos. */
3689 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
3695 if ((int)addrlen
< 0) {
3696 return -TARGET_EINVAL
;
3699 addr
= alloca(addrlen
+1);
3701 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3705 return get_errno(bind(sockfd
, addr
, addrlen
));
3708 /* do_connect() Must return target values and target errnos. */
3709 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
3715 if ((int)addrlen
< 0) {
3716 return -TARGET_EINVAL
;
3719 addr
= alloca(addrlen
+1);
3721 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3725 return get_errno(safe_connect(sockfd
, addr
, addrlen
));
3728 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3729 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
3730 int flags
, int send
)
3736 abi_ulong target_vec
;
3738 if (msgp
->msg_name
) {
3739 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
3740 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
3741 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
3742 tswapal(msgp
->msg_name
),
3744 if (ret
== -TARGET_EFAULT
) {
3745 /* For connected sockets msg_name and msg_namelen must
3746 * be ignored, so returning EFAULT immediately is wrong.
3747 * Instead, pass a bad msg_name to the host kernel, and
3748 * let it decide whether to return EFAULT or not.
3750 msg
.msg_name
= (void *)-1;
3755 msg
.msg_name
= NULL
;
3756 msg
.msg_namelen
= 0;
3758 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
3759 msg
.msg_control
= alloca(msg
.msg_controllen
);
3760 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
3762 count
= tswapal(msgp
->msg_iovlen
);
3763 target_vec
= tswapal(msgp
->msg_iov
);
3765 if (count
> IOV_MAX
) {
3766 /* sendrcvmsg returns a different errno for this condition than
3767 * readv/writev, so we must catch it here before lock_iovec() does.
3769 ret
= -TARGET_EMSGSIZE
;
3773 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
3774 target_vec
, count
, send
);
3776 ret
= -host_to_target_errno(errno
);
3779 msg
.msg_iovlen
= count
;
3783 if (fd_trans_target_to_host_data(fd
)) {
3786 host_msg
= g_malloc(msg
.msg_iov
->iov_len
);
3787 memcpy(host_msg
, msg
.msg_iov
->iov_base
, msg
.msg_iov
->iov_len
);
3788 ret
= fd_trans_target_to_host_data(fd
)(host_msg
,
3789 msg
.msg_iov
->iov_len
);
3791 msg
.msg_iov
->iov_base
= host_msg
;
3792 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3796 ret
= target_to_host_cmsg(&msg
, msgp
);
3798 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3802 ret
= get_errno(safe_recvmsg(fd
, &msg
, flags
));
3803 if (!is_error(ret
)) {
3805 if (fd_trans_host_to_target_data(fd
)) {
3806 ret
= fd_trans_host_to_target_data(fd
)(msg
.msg_iov
->iov_base
,
3809 ret
= host_to_target_cmsg(msgp
, &msg
);
3811 if (!is_error(ret
)) {
3812 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
3813 if (msg
.msg_name
!= NULL
&& msg
.msg_name
!= (void *)-1) {
3814 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
3815 msg
.msg_name
, msg
.msg_namelen
);
3827 unlock_iovec(vec
, target_vec
, count
, !send
);
3832 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
3833 int flags
, int send
)
3836 struct target_msghdr
*msgp
;
3838 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
3842 return -TARGET_EFAULT
;
3844 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
3845 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
3849 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3850 * so it might not have this *mmsg-specific flag either.
3852 #ifndef MSG_WAITFORONE
3853 #define MSG_WAITFORONE 0x10000
3856 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
3857 unsigned int vlen
, unsigned int flags
,
3860 struct target_mmsghdr
*mmsgp
;
3864 if (vlen
> UIO_MAXIOV
) {
3868 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
3870 return -TARGET_EFAULT
;
3873 for (i
= 0; i
< vlen
; i
++) {
3874 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
3875 if (is_error(ret
)) {
3878 mmsgp
[i
].msg_len
= tswap32(ret
);
3879 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3880 if (flags
& MSG_WAITFORONE
) {
3881 flags
|= MSG_DONTWAIT
;
3885 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
3887 /* Return number of datagrams sent if we sent any at all;
3888 * otherwise return the error.
3896 /* do_accept4() Must return target values and target errnos. */
3897 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
3898 abi_ulong target_addrlen_addr
, int flags
)
3905 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
3907 if (target_addr
== 0) {
3908 return get_errno(safe_accept4(fd
, NULL
, NULL
, host_flags
));
3911 /* linux returns EINVAL if addrlen pointer is invalid */
3912 if (get_user_u32(addrlen
, target_addrlen_addr
))
3913 return -TARGET_EINVAL
;
3915 if ((int)addrlen
< 0) {
3916 return -TARGET_EINVAL
;
3919 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3920 return -TARGET_EINVAL
;
3922 addr
= alloca(addrlen
);
3924 ret
= get_errno(safe_accept4(fd
, addr
, &addrlen
, host_flags
));
3925 if (!is_error(ret
)) {
3926 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3927 if (put_user_u32(addrlen
, target_addrlen_addr
))
3928 ret
= -TARGET_EFAULT
;
3933 /* do_getpeername() Must return target values and target errnos. */
3934 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
3935 abi_ulong target_addrlen_addr
)
3941 if (get_user_u32(addrlen
, target_addrlen_addr
))
3942 return -TARGET_EFAULT
;
3944 if ((int)addrlen
< 0) {
3945 return -TARGET_EINVAL
;
3948 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3949 return -TARGET_EFAULT
;
3951 addr
= alloca(addrlen
);
3953 ret
= get_errno(getpeername(fd
, addr
, &addrlen
));
3954 if (!is_error(ret
)) {
3955 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3956 if (put_user_u32(addrlen
, target_addrlen_addr
))
3957 ret
= -TARGET_EFAULT
;
3962 /* do_getsockname() Must return target values and target errnos. */
3963 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
3964 abi_ulong target_addrlen_addr
)
3970 if (get_user_u32(addrlen
, target_addrlen_addr
))
3971 return -TARGET_EFAULT
;
3973 if ((int)addrlen
< 0) {
3974 return -TARGET_EINVAL
;
3977 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3978 return -TARGET_EFAULT
;
3980 addr
= alloca(addrlen
);
3982 ret
= get_errno(getsockname(fd
, addr
, &addrlen
));
3983 if (!is_error(ret
)) {
3984 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3985 if (put_user_u32(addrlen
, target_addrlen_addr
))
3986 ret
= -TARGET_EFAULT
;
3991 /* do_socketpair() Must return target values and target errnos. */
3992 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
3993 abi_ulong target_tab_addr
)
3998 target_to_host_sock_type(&type
);
4000 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
4001 if (!is_error(ret
)) {
4002 if (put_user_s32(tab
[0], target_tab_addr
)
4003 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
4004 ret
= -TARGET_EFAULT
;
4009 /* do_sendto() Must return target values and target errnos. */
4010 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
4011 abi_ulong target_addr
, socklen_t addrlen
)
4015 void *copy_msg
= NULL
;
4018 if ((int)addrlen
< 0) {
4019 return -TARGET_EINVAL
;
4022 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
4024 return -TARGET_EFAULT
;
4025 if (fd_trans_target_to_host_data(fd
)) {
4026 copy_msg
= host_msg
;
4027 host_msg
= g_malloc(len
);
4028 memcpy(host_msg
, copy_msg
, len
);
4029 ret
= fd_trans_target_to_host_data(fd
)(host_msg
, len
);
4035 addr
= alloca(addrlen
+1);
4036 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);
4040 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
4042 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, NULL
, 0));
4047 host_msg
= copy_msg
;
4049 unlock_user(host_msg
, msg
, 0);
4053 /* do_recvfrom() Must return target values and target errnos. */
4054 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
4055 abi_ulong target_addr
,
4056 abi_ulong target_addrlen
)
4063 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
4065 return -TARGET_EFAULT
;
4067 if (get_user_u32(addrlen
, target_addrlen
)) {
4068 ret
= -TARGET_EFAULT
;
4071 if ((int)addrlen
< 0) {
4072 ret
= -TARGET_EINVAL
;
4075 addr
= alloca(addrlen
);
4076 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
,
4079 addr
= NULL
; /* To keep compiler quiet. */
4080 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
, NULL
, 0));
4082 if (!is_error(ret
)) {
4083 if (fd_trans_host_to_target_data(fd
)) {
4084 ret
= fd_trans_host_to_target_data(fd
)(host_msg
, ret
);
4087 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
4088 if (put_user_u32(addrlen
, target_addrlen
)) {
4089 ret
= -TARGET_EFAULT
;
4093 unlock_user(host_msg
, msg
, len
);
4096 unlock_user(host_msg
, msg
, 0);
4101 #ifdef TARGET_NR_socketcall
4102 /* do_socketcall() must return target values and target errnos. */
4103 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
4105 static const unsigned nargs
[] = { /* number of arguments per operation */
4106 [TARGET_SYS_SOCKET
] = 3, /* domain, type, protocol */
4107 [TARGET_SYS_BIND
] = 3, /* fd, addr, addrlen */
4108 [TARGET_SYS_CONNECT
] = 3, /* fd, addr, addrlen */
4109 [TARGET_SYS_LISTEN
] = 2, /* fd, backlog */
4110 [TARGET_SYS_ACCEPT
] = 3, /* fd, addr, addrlen */
4111 [TARGET_SYS_GETSOCKNAME
] = 3, /* fd, addr, addrlen */
4112 [TARGET_SYS_GETPEERNAME
] = 3, /* fd, addr, addrlen */
4113 [TARGET_SYS_SOCKETPAIR
] = 4, /* domain, type, protocol, tab */
4114 [TARGET_SYS_SEND
] = 4, /* fd, msg, len, flags */
4115 [TARGET_SYS_RECV
] = 4, /* fd, msg, len, flags */
4116 [TARGET_SYS_SENDTO
] = 6, /* fd, msg, len, flags, addr, addrlen */
4117 [TARGET_SYS_RECVFROM
] = 6, /* fd, msg, len, flags, addr, addrlen */
4118 [TARGET_SYS_SHUTDOWN
] = 2, /* fd, how */
4119 [TARGET_SYS_SETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
4120 [TARGET_SYS_GETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
4121 [TARGET_SYS_SENDMSG
] = 3, /* fd, msg, flags */
4122 [TARGET_SYS_RECVMSG
] = 3, /* fd, msg, flags */
4123 [TARGET_SYS_ACCEPT4
] = 4, /* fd, addr, addrlen, flags */
4124 [TARGET_SYS_RECVMMSG
] = 4, /* fd, msgvec, vlen, flags */
4125 [TARGET_SYS_SENDMMSG
] = 4, /* fd, msgvec, vlen, flags */
4127 abi_long a
[6]; /* max 6 args */
4130 /* check the range of the first argument num */
4131 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
4132 if (num
< 1 || num
> TARGET_SYS_SENDMMSG
) {
4133 return -TARGET_EINVAL
;
4135 /* ensure we have space for args */
4136 if (nargs
[num
] > ARRAY_SIZE(a
)) {
4137 return -TARGET_EINVAL
;
4139 /* collect the arguments in a[] according to nargs[] */
4140 for (i
= 0; i
< nargs
[num
]; ++i
) {
4141 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
4142 return -TARGET_EFAULT
;
4145 /* now when we have the args, invoke the appropriate underlying function */
4147 case TARGET_SYS_SOCKET
: /* domain, type, protocol */
4148 return do_socket(a
[0], a
[1], a
[2]);
4149 case TARGET_SYS_BIND
: /* sockfd, addr, addrlen */
4150 return do_bind(a
[0], a
[1], a
[2]);
4151 case TARGET_SYS_CONNECT
: /* sockfd, addr, addrlen */
4152 return do_connect(a
[0], a
[1], a
[2]);
4153 case TARGET_SYS_LISTEN
: /* sockfd, backlog */
4154 return get_errno(listen(a
[0], a
[1]));
4155 case TARGET_SYS_ACCEPT
: /* sockfd, addr, addrlen */
4156 return do_accept4(a
[0], a
[1], a
[2], 0);
4157 case TARGET_SYS_GETSOCKNAME
: /* sockfd, addr, addrlen */
4158 return do_getsockname(a
[0], a
[1], a
[2]);
4159 case TARGET_SYS_GETPEERNAME
: /* sockfd, addr, addrlen */
4160 return do_getpeername(a
[0], a
[1], a
[2]);
4161 case TARGET_SYS_SOCKETPAIR
: /* domain, type, protocol, tab */
4162 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
4163 case TARGET_SYS_SEND
: /* sockfd, msg, len, flags */
4164 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
4165 case TARGET_SYS_RECV
: /* sockfd, msg, len, flags */
4166 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
4167 case TARGET_SYS_SENDTO
: /* sockfd, msg, len, flags, addr, addrlen */
4168 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
4169 case TARGET_SYS_RECVFROM
: /* sockfd, msg, len, flags, addr, addrlen */
4170 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
4171 case TARGET_SYS_SHUTDOWN
: /* sockfd, how */
4172 return get_errno(shutdown(a
[0], a
[1]));
4173 case TARGET_SYS_SETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
4174 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
4175 case TARGET_SYS_GETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
4176 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
4177 case TARGET_SYS_SENDMSG
: /* sockfd, msg, flags */
4178 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
4179 case TARGET_SYS_RECVMSG
: /* sockfd, msg, flags */
4180 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
4181 case TARGET_SYS_ACCEPT4
: /* sockfd, addr, addrlen, flags */
4182 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
4183 case TARGET_SYS_RECVMMSG
: /* sockfd, msgvec, vlen, flags */
4184 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 0);
4185 case TARGET_SYS_SENDMMSG
: /* sockfd, msgvec, vlen, flags */
4186 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 1);
4188 gemu_log("Unsupported socketcall: %d\n", num
);
4189 return -TARGET_EINVAL
;
4194 #define N_SHM_REGIONS 32
4196 static struct shm_region
{
4200 } shm_regions
[N_SHM_REGIONS
];
4202 #ifndef TARGET_SEMID64_DS
4203 /* asm-generic version of this struct */
4204 struct target_semid64_ds
4206 struct target_ipc_perm sem_perm
;
4207 abi_ulong sem_otime
;
4208 #if TARGET_ABI_BITS == 32
4209 abi_ulong __unused1
;
4211 abi_ulong sem_ctime
;
4212 #if TARGET_ABI_BITS == 32
4213 abi_ulong __unused2
;
4215 abi_ulong sem_nsems
;
4216 abi_ulong __unused3
;
4217 abi_ulong __unused4
;
4221 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
4222 abi_ulong target_addr
)
4224 struct target_ipc_perm
*target_ip
;
4225 struct target_semid64_ds
*target_sd
;
4227 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4228 return -TARGET_EFAULT
;
4229 target_ip
= &(target_sd
->sem_perm
);
4230 host_ip
->__key
= tswap32(target_ip
->__key
);
4231 host_ip
->uid
= tswap32(target_ip
->uid
);
4232 host_ip
->gid
= tswap32(target_ip
->gid
);
4233 host_ip
->cuid
= tswap32(target_ip
->cuid
);
4234 host_ip
->cgid
= tswap32(target_ip
->cgid
);
4235 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
4236 host_ip
->mode
= tswap32(target_ip
->mode
);
4238 host_ip
->mode
= tswap16(target_ip
->mode
);
4240 #if defined(TARGET_PPC)
4241 host_ip
->__seq
= tswap32(target_ip
->__seq
);
4243 host_ip
->__seq
= tswap16(target_ip
->__seq
);
4245 unlock_user_struct(target_sd
, target_addr
, 0);
4249 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
4250 struct ipc_perm
*host_ip
)
4252 struct target_ipc_perm
*target_ip
;
4253 struct target_semid64_ds
*target_sd
;
4255 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4256 return -TARGET_EFAULT
;
4257 target_ip
= &(target_sd
->sem_perm
);
4258 target_ip
->__key
= tswap32(host_ip
->__key
);
4259 target_ip
->uid
= tswap32(host_ip
->uid
);
4260 target_ip
->gid
= tswap32(host_ip
->gid
);
4261 target_ip
->cuid
= tswap32(host_ip
->cuid
);
4262 target_ip
->cgid
= tswap32(host_ip
->cgid
);
4263 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
4264 target_ip
->mode
= tswap32(host_ip
->mode
);
4266 target_ip
->mode
= tswap16(host_ip
->mode
);
4268 #if defined(TARGET_PPC)
4269 target_ip
->__seq
= tswap32(host_ip
->__seq
);
4271 target_ip
->__seq
= tswap16(host_ip
->__seq
);
4273 unlock_user_struct(target_sd
, target_addr
, 1);
4277 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
4278 abi_ulong target_addr
)
4280 struct target_semid64_ds
*target_sd
;
4282 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4283 return -TARGET_EFAULT
;
4284 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
4285 return -TARGET_EFAULT
;
4286 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
4287 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
4288 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
4289 unlock_user_struct(target_sd
, target_addr
, 0);
4293 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
4294 struct semid_ds
*host_sd
)
4296 struct target_semid64_ds
*target_sd
;
4298 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4299 return -TARGET_EFAULT
;
4300 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
4301 return -TARGET_EFAULT
;
4302 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
4303 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
4304 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
4305 unlock_user_struct(target_sd
, target_addr
, 1);
4309 struct target_seminfo
{
4322 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
4323 struct seminfo
*host_seminfo
)
4325 struct target_seminfo
*target_seminfo
;
4326 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
4327 return -TARGET_EFAULT
;
4328 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
4329 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
4330 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
4331 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
4332 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
4333 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
4334 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
4335 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
4336 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
4337 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
4338 unlock_user_struct(target_seminfo
, target_addr
, 1);
4344 struct semid_ds
*buf
;
4345 unsigned short *array
;
4346 struct seminfo
*__buf
;
4349 union target_semun
{
4356 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
4357 abi_ulong target_addr
)
4360 unsigned short *array
;
4362 struct semid_ds semid_ds
;
4365 semun
.buf
= &semid_ds
;
4367 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
4369 return get_errno(ret
);
4371 nsems
= semid_ds
.sem_nsems
;
4373 *host_array
= g_try_new(unsigned short, nsems
);
4375 return -TARGET_ENOMEM
;
4377 array
= lock_user(VERIFY_READ
, target_addr
,
4378 nsems
*sizeof(unsigned short), 1);
4380 g_free(*host_array
);
4381 return -TARGET_EFAULT
;
4384 for(i
=0; i
<nsems
; i
++) {
4385 __get_user((*host_array
)[i
], &array
[i
]);
4387 unlock_user(array
, target_addr
, 0);
4392 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
4393 unsigned short **host_array
)
4396 unsigned short *array
;
4398 struct semid_ds semid_ds
;
4401 semun
.buf
= &semid_ds
;
4403 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
4405 return get_errno(ret
);
4407 nsems
= semid_ds
.sem_nsems
;
4409 array
= lock_user(VERIFY_WRITE
, target_addr
,
4410 nsems
*sizeof(unsigned short), 0);
4412 return -TARGET_EFAULT
;
4414 for(i
=0; i
<nsems
; i
++) {
4415 __put_user((*host_array
)[i
], &array
[i
]);
4417 g_free(*host_array
);
4418 unlock_user(array
, target_addr
, 1);
4423 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
4424 abi_ulong target_arg
)
4426 union target_semun target_su
= { .buf
= target_arg
};
4428 struct semid_ds dsarg
;
4429 unsigned short *array
= NULL
;
4430 struct seminfo seminfo
;
4431 abi_long ret
= -TARGET_EINVAL
;
4438 /* In 64 bit cross-endian situations, we will erroneously pick up
4439 * the wrong half of the union for the "val" element. To rectify
4440 * this, the entire 8-byte structure is byteswapped, followed by
4441 * a swap of the 4 byte val field. In other cases, the data is
4442 * already in proper host byte order. */
4443 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
4444 target_su
.buf
= tswapal(target_su
.buf
);
4445 arg
.val
= tswap32(target_su
.val
);
4447 arg
.val
= target_su
.val
;
4449 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4453 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
4457 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4458 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
4465 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
4469 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4470 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
4476 arg
.__buf
= &seminfo
;
4477 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4478 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
4486 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
4493 struct target_sembuf
{
4494 unsigned short sem_num
;
4499 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
4500 abi_ulong target_addr
,
4503 struct target_sembuf
*target_sembuf
;
4506 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
4507 nsops
*sizeof(struct target_sembuf
), 1);
4509 return -TARGET_EFAULT
;
4511 for(i
=0; i
<nsops
; i
++) {
4512 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
4513 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
4514 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
4517 unlock_user(target_sembuf
, target_addr
, 0);
4522 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
4524 struct sembuf sops
[nsops
];
4526 if (target_to_host_sembuf(sops
, ptr
, nsops
))
4527 return -TARGET_EFAULT
;
4529 return get_errno(safe_semtimedop(semid
, sops
, nsops
, NULL
));
4532 struct target_msqid_ds
4534 struct target_ipc_perm msg_perm
;
4535 abi_ulong msg_stime
;
4536 #if TARGET_ABI_BITS == 32
4537 abi_ulong __unused1
;
4539 abi_ulong msg_rtime
;
4540 #if TARGET_ABI_BITS == 32
4541 abi_ulong __unused2
;
4543 abi_ulong msg_ctime
;
4544 #if TARGET_ABI_BITS == 32
4545 abi_ulong __unused3
;
4547 abi_ulong __msg_cbytes
;
4549 abi_ulong msg_qbytes
;
4550 abi_ulong msg_lspid
;
4551 abi_ulong msg_lrpid
;
4552 abi_ulong __unused4
;
4553 abi_ulong __unused5
;
4556 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
4557 abi_ulong target_addr
)
4559 struct target_msqid_ds
*target_md
;
4561 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
4562 return -TARGET_EFAULT
;
4563 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
4564 return -TARGET_EFAULT
;
4565 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
4566 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
4567 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
4568 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
4569 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
4570 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
4571 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
4572 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
4573 unlock_user_struct(target_md
, target_addr
, 0);
4577 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
4578 struct msqid_ds
*host_md
)
4580 struct target_msqid_ds
*target_md
;
4582 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
4583 return -TARGET_EFAULT
;
4584 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
4585 return -TARGET_EFAULT
;
4586 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
4587 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
4588 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
4589 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
4590 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
4591 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
4592 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
4593 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
4594 unlock_user_struct(target_md
, target_addr
, 1);
4598 struct target_msginfo
{
4606 unsigned short int msgseg
;
4609 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
4610 struct msginfo
*host_msginfo
)
4612 struct target_msginfo
*target_msginfo
;
4613 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
4614 return -TARGET_EFAULT
;
4615 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
4616 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
4617 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
4618 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
4619 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
4620 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
4621 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
4622 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
4623 unlock_user_struct(target_msginfo
, target_addr
, 1);
4627 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
4629 struct msqid_ds dsarg
;
4630 struct msginfo msginfo
;
4631 abi_long ret
= -TARGET_EINVAL
;
4639 if (target_to_host_msqid_ds(&dsarg
,ptr
))
4640 return -TARGET_EFAULT
;
4641 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
4642 if (host_to_target_msqid_ds(ptr
,&dsarg
))
4643 return -TARGET_EFAULT
;
4646 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
4650 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
4651 if (host_to_target_msginfo(ptr
, &msginfo
))
4652 return -TARGET_EFAULT
;
4659 struct target_msgbuf
{
4664 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
4665 ssize_t msgsz
, int msgflg
)
4667 struct target_msgbuf
*target_mb
;
4668 struct msgbuf
*host_mb
;
4672 return -TARGET_EINVAL
;
4675 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
4676 return -TARGET_EFAULT
;
4677 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4679 unlock_user_struct(target_mb
, msgp
, 0);
4680 return -TARGET_ENOMEM
;
4682 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
4683 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
4684 ret
= get_errno(safe_msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
4686 unlock_user_struct(target_mb
, msgp
, 0);
4691 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
4692 ssize_t msgsz
, abi_long msgtyp
,
4695 struct target_msgbuf
*target_mb
;
4697 struct msgbuf
*host_mb
;
4701 return -TARGET_EINVAL
;
4704 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
4705 return -TARGET_EFAULT
;
4707 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4709 ret
= -TARGET_ENOMEM
;
4712 ret
= get_errno(safe_msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
4715 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
4716 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
4717 if (!target_mtext
) {
4718 ret
= -TARGET_EFAULT
;
4721 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
4722 unlock_user(target_mtext
, target_mtext_addr
, ret
);
4725 target_mb
->mtype
= tswapal(host_mb
->mtype
);
4729 unlock_user_struct(target_mb
, msgp
, 1);
4734 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
4735 abi_ulong target_addr
)
4737 struct target_shmid_ds
*target_sd
;
4739 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4740 return -TARGET_EFAULT
;
4741 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
4742 return -TARGET_EFAULT
;
4743 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4744 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4745 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4746 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4747 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4748 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4749 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4750 unlock_user_struct(target_sd
, target_addr
, 0);
4754 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
4755 struct shmid_ds
*host_sd
)
4757 struct target_shmid_ds
*target_sd
;
4759 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4760 return -TARGET_EFAULT
;
4761 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
4762 return -TARGET_EFAULT
;
4763 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4764 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4765 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4766 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4767 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4768 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4769 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4770 unlock_user_struct(target_sd
, target_addr
, 1);
4774 struct target_shminfo
{
4782 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
4783 struct shminfo
*host_shminfo
)
4785 struct target_shminfo
*target_shminfo
;
4786 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
4787 return -TARGET_EFAULT
;
4788 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
4789 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
4790 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
4791 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
4792 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
4793 unlock_user_struct(target_shminfo
, target_addr
, 1);
4797 struct target_shm_info
{
4802 abi_ulong swap_attempts
;
4803 abi_ulong swap_successes
;
4806 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
4807 struct shm_info
*host_shm_info
)
4809 struct target_shm_info
*target_shm_info
;
4810 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
4811 return -TARGET_EFAULT
;
4812 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
4813 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
4814 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
4815 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
4816 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
4817 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
4818 unlock_user_struct(target_shm_info
, target_addr
, 1);
4822 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
4824 struct shmid_ds dsarg
;
4825 struct shminfo shminfo
;
4826 struct shm_info shm_info
;
4827 abi_long ret
= -TARGET_EINVAL
;
4835 if (target_to_host_shmid_ds(&dsarg
, buf
))
4836 return -TARGET_EFAULT
;
4837 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
4838 if (host_to_target_shmid_ds(buf
, &dsarg
))
4839 return -TARGET_EFAULT
;
4842 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
4843 if (host_to_target_shminfo(buf
, &shminfo
))
4844 return -TARGET_EFAULT
;
4847 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
4848 if (host_to_target_shm_info(buf
, &shm_info
))
4849 return -TARGET_EFAULT
;
4854 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
4861 #ifndef TARGET_FORCE_SHMLBA
4862 /* For most architectures, SHMLBA is the same as the page size;
4863 * some architectures have larger values, in which case they should
4864 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
4865 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
4866 * and defining its own value for SHMLBA.
4868 * The kernel also permits SHMLBA to be set by the architecture to a
4869 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
4870 * this means that addresses are rounded to the large size if
4871 * SHM_RND is set but addresses not aligned to that size are not rejected
4872 * as long as they are at least page-aligned. Since the only architecture
4873 * which uses this is ia64 this code doesn't provide for that oddity.
4875 static inline abi_ulong
target_shmlba(CPUArchState
*cpu_env
)
4877 return TARGET_PAGE_SIZE
;
4881 static inline abi_ulong
do_shmat(CPUArchState
*cpu_env
,
4882 int shmid
, abi_ulong shmaddr
, int shmflg
)
4886 struct shmid_ds shm_info
;
4890 /* find out the length of the shared memory segment */
4891 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
4892 if (is_error(ret
)) {
4893 /* can't get length, bail out */
4897 shmlba
= target_shmlba(cpu_env
);
4899 if (shmaddr
& (shmlba
- 1)) {
4900 if (shmflg
& SHM_RND
) {
4901 shmaddr
&= ~(shmlba
- 1);
4903 return -TARGET_EINVAL
;
4906 if (!guest_range_valid(shmaddr
, shm_info
.shm_segsz
)) {
4907 return -TARGET_EINVAL
;
4913 host_raddr
= shmat(shmid
, (void *)g2h(shmaddr
), shmflg
);
4915 abi_ulong mmap_start
;
4917 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
);
4919 if (mmap_start
== -1) {
4921 host_raddr
= (void *)-1;
4923 host_raddr
= shmat(shmid
, g2h(mmap_start
), shmflg
| SHM_REMAP
);
4926 if (host_raddr
== (void *)-1) {
4928 return get_errno((long)host_raddr
);
4930 raddr
=h2g((unsigned long)host_raddr
);
4932 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
4933 PAGE_VALID
| PAGE_READ
|
4934 ((shmflg
& SHM_RDONLY
)? 0 : PAGE_WRITE
));
4936 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
4937 if (!shm_regions
[i
].in_use
) {
4938 shm_regions
[i
].in_use
= true;
4939 shm_regions
[i
].start
= raddr
;
4940 shm_regions
[i
].size
= shm_info
.shm_segsz
;
4950 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
4957 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
4958 if (shm_regions
[i
].in_use
&& shm_regions
[i
].start
== shmaddr
) {
4959 shm_regions
[i
].in_use
= false;
4960 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
, 0);
4964 rv
= get_errno(shmdt(g2h(shmaddr
)));
4971 #ifdef TARGET_NR_ipc
4972 /* ??? This only works with linear mappings. */
4973 /* do_ipc() must return target values and target errnos. */
4974 static abi_long
do_ipc(CPUArchState
*cpu_env
,
4975 unsigned int call
, abi_long first
,
4976 abi_long second
, abi_long third
,
4977 abi_long ptr
, abi_long fifth
)
4982 version
= call
>> 16;
4987 ret
= do_semop(first
, ptr
, second
);
4991 ret
= get_errno(semget(first
, second
, third
));
4994 case IPCOP_semctl
: {
4995 /* The semun argument to semctl is passed by value, so dereference the
4998 get_user_ual(atptr
, ptr
);
4999 ret
= do_semctl(first
, second
, third
, atptr
);
5004 ret
= get_errno(msgget(first
, second
));
5008 ret
= do_msgsnd(first
, ptr
, second
, third
);
5012 ret
= do_msgctl(first
, second
, ptr
);
5019 struct target_ipc_kludge
{
5024 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
5025 ret
= -TARGET_EFAULT
;
5029 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
5031 unlock_user_struct(tmp
, ptr
, 0);
5035 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
5044 raddr
= do_shmat(cpu_env
, first
, ptr
, second
);
5045 if (is_error(raddr
))
5046 return get_errno(raddr
);
5047 if (put_user_ual(raddr
, third
))
5048 return -TARGET_EFAULT
;
5052 ret
= -TARGET_EINVAL
;
5057 ret
= do_shmdt(ptr
);
5061 /* IPC_* flag values are the same on all linux platforms */
5062 ret
= get_errno(shmget(first
, second
, third
));
5065 /* IPC_* and SHM_* command values are the same on all linux platforms */
5067 ret
= do_shmctl(first
, second
, ptr
);
5070 gemu_log("Unsupported ipc call: %d (version %d)\n", call
, version
);
5071 ret
= -TARGET_ENOSYS
;
5078 /* kernel structure types definitions */
5080 #define STRUCT(name, ...) STRUCT_ ## name,
5081 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
5083 #include "syscall_types.h"
5087 #undef STRUCT_SPECIAL
5089 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
5090 #define STRUCT_SPECIAL(name)
5091 #include "syscall_types.h"
5093 #undef STRUCT_SPECIAL
5095 typedef struct IOCTLEntry IOCTLEntry
;
5097 typedef abi_long
do_ioctl_fn(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5098 int fd
, int cmd
, abi_long arg
);
5102 unsigned int host_cmd
;
5105 do_ioctl_fn
*do_ioctl
;
5106 const argtype arg_type
[5];
5109 #define IOC_R 0x0001
5110 #define IOC_W 0x0002
5111 #define IOC_RW (IOC_R | IOC_W)
5113 #define MAX_STRUCT_SIZE 4096
5115 #ifdef CONFIG_FIEMAP
5116 /* So fiemap access checks don't overflow on 32 bit systems.
5117 * This is very slightly smaller than the limit imposed by
5118 * the underlying kernel.
5120 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
5121 / sizeof(struct fiemap_extent))
5123 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5124 int fd
, int cmd
, abi_long arg
)
5126 /* The parameter for this ioctl is a struct fiemap followed
5127 * by an array of struct fiemap_extent whose size is set
5128 * in fiemap->fm_extent_count. The array is filled in by the
5131 int target_size_in
, target_size_out
;
5133 const argtype
*arg_type
= ie
->arg_type
;
5134 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
5137 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
5141 assert(arg_type
[0] == TYPE_PTR
);
5142 assert(ie
->access
== IOC_RW
);
5144 target_size_in
= thunk_type_size(arg_type
, 0);
5145 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
5147 return -TARGET_EFAULT
;
5149 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5150 unlock_user(argptr
, arg
, 0);
5151 fm
= (struct fiemap
*)buf_temp
;
5152 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
5153 return -TARGET_EINVAL
;
5156 outbufsz
= sizeof (*fm
) +
5157 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
5159 if (outbufsz
> MAX_STRUCT_SIZE
) {
5160 /* We can't fit all the extents into the fixed size buffer.
5161 * Allocate one that is large enough and use it instead.
5163 fm
= g_try_malloc(outbufsz
);
5165 return -TARGET_ENOMEM
;
5167 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
5170 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, fm
));
5171 if (!is_error(ret
)) {
5172 target_size_out
= target_size_in
;
5173 /* An extent_count of 0 means we were only counting the extents
5174 * so there are no structs to copy
5176 if (fm
->fm_extent_count
!= 0) {
5177 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
5179 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
5181 ret
= -TARGET_EFAULT
;
5183 /* Convert the struct fiemap */
5184 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
5185 if (fm
->fm_extent_count
!= 0) {
5186 p
= argptr
+ target_size_in
;
5187 /* ...and then all the struct fiemap_extents */
5188 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
5189 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
5194 unlock_user(argptr
, arg
, target_size_out
);
5204 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5205 int fd
, int cmd
, abi_long arg
)
5207 const argtype
*arg_type
= ie
->arg_type
;
5211 struct ifconf
*host_ifconf
;
5213 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
5214 int target_ifreq_size
;
5219 abi_long target_ifc_buf
;
5223 assert(arg_type
[0] == TYPE_PTR
);
5224 assert(ie
->access
== IOC_RW
);
5227 target_size
= thunk_type_size(arg_type
, 0);
5229 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5231 return -TARGET_EFAULT
;
5232 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5233 unlock_user(argptr
, arg
, 0);
5235 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
5236 target_ifc_len
= host_ifconf
->ifc_len
;
5237 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
5239 target_ifreq_size
= thunk_type_size(ifreq_arg_type
, 0);
5240 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
5241 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
5243 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
5244 if (outbufsz
> MAX_STRUCT_SIZE
) {
5245 /* We can't fit all the extents into the fixed size buffer.
5246 * Allocate one that is large enough and use it instead.
5248 host_ifconf
= malloc(outbufsz
);
5250 return -TARGET_ENOMEM
;
5252 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
5255 host_ifc_buf
= (char*)host_ifconf
+ sizeof(*host_ifconf
);
5257 host_ifconf
->ifc_len
= host_ifc_len
;
5258 host_ifconf
->ifc_buf
= host_ifc_buf
;
5260 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_ifconf
));
5261 if (!is_error(ret
)) {
5262 /* convert host ifc_len to target ifc_len */
5264 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
5265 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
5266 host_ifconf
->ifc_len
= target_ifc_len
;
5268 /* restore target ifc_buf */
5270 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
5272 /* copy struct ifconf to target user */
5274 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5276 return -TARGET_EFAULT
;
5277 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
5278 unlock_user(argptr
, arg
, target_size
);
5280 /* copy ifreq[] to target user */
5282 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
5283 for (i
= 0; i
< nb_ifreq
; i
++) {
5284 thunk_convert(argptr
+ i
* target_ifreq_size
,
5285 host_ifc_buf
+ i
* sizeof(struct ifreq
),
5286 ifreq_arg_type
, THUNK_TARGET
);
5288 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
5298 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5299 int cmd
, abi_long arg
)
5302 struct dm_ioctl
*host_dm
;
5303 abi_long guest_data
;
5304 uint32_t guest_data_size
;
5306 const argtype
*arg_type
= ie
->arg_type
;
5308 void *big_buf
= NULL
;
5312 target_size
= thunk_type_size(arg_type
, 0);
5313 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5315 ret
= -TARGET_EFAULT
;
5318 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5319 unlock_user(argptr
, arg
, 0);
5321 /* buf_temp is too small, so fetch things into a bigger buffer */
5322 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
5323 memcpy(big_buf
, buf_temp
, target_size
);
5327 guest_data
= arg
+ host_dm
->data_start
;
5328 if ((guest_data
- arg
) < 0) {
5329 ret
= -TARGET_EINVAL
;
5332 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5333 host_data
= (char*)host_dm
+ host_dm
->data_start
;
5335 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
5337 ret
= -TARGET_EFAULT
;
5341 switch (ie
->host_cmd
) {
5343 case DM_LIST_DEVICES
:
5346 case DM_DEV_SUSPEND
:
5349 case DM_TABLE_STATUS
:
5350 case DM_TABLE_CLEAR
:
5352 case DM_LIST_VERSIONS
:
5356 case DM_DEV_SET_GEOMETRY
:
5357 /* data contains only strings */
5358 memcpy(host_data
, argptr
, guest_data_size
);
5361 memcpy(host_data
, argptr
, guest_data_size
);
5362 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
5366 void *gspec
= argptr
;
5367 void *cur_data
= host_data
;
5368 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5369 int spec_size
= thunk_type_size(arg_type
, 0);
5372 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5373 struct dm_target_spec
*spec
= cur_data
;
5377 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
5378 slen
= strlen((char*)gspec
+ spec_size
) + 1;
5380 spec
->next
= sizeof(*spec
) + slen
;
5381 strcpy((char*)&spec
[1], gspec
+ spec_size
);
5383 cur_data
+= spec
->next
;
5388 ret
= -TARGET_EINVAL
;
5389 unlock_user(argptr
, guest_data
, 0);
5392 unlock_user(argptr
, guest_data
, 0);
5394 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5395 if (!is_error(ret
)) {
5396 guest_data
= arg
+ host_dm
->data_start
;
5397 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5398 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
5399 switch (ie
->host_cmd
) {
5404 case DM_DEV_SUSPEND
:
5407 case DM_TABLE_CLEAR
:
5409 case DM_DEV_SET_GEOMETRY
:
5410 /* no return data */
5412 case DM_LIST_DEVICES
:
5414 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
5415 uint32_t remaining_data
= guest_data_size
;
5416 void *cur_data
= argptr
;
5417 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
5418 int nl_size
= 12; /* can't use thunk_size due to alignment */
5421 uint32_t next
= nl
->next
;
5423 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
5425 if (remaining_data
< nl
->next
) {
5426 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5429 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
5430 strcpy(cur_data
+ nl_size
, nl
->name
);
5431 cur_data
+= nl
->next
;
5432 remaining_data
-= nl
->next
;
5436 nl
= (void*)nl
+ next
;
5441 case DM_TABLE_STATUS
:
5443 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
5444 void *cur_data
= argptr
;
5445 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5446 int spec_size
= thunk_type_size(arg_type
, 0);
5449 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5450 uint32_t next
= spec
->next
;
5451 int slen
= strlen((char*)&spec
[1]) + 1;
5452 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
5453 if (guest_data_size
< spec
->next
) {
5454 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5457 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
5458 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
5459 cur_data
= argptr
+ spec
->next
;
5460 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
5466 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
5467 int count
= *(uint32_t*)hdata
;
5468 uint64_t *hdev
= hdata
+ 8;
5469 uint64_t *gdev
= argptr
+ 8;
5472 *(uint32_t*)argptr
= tswap32(count
);
5473 for (i
= 0; i
< count
; i
++) {
5474 *gdev
= tswap64(*hdev
);
5480 case DM_LIST_VERSIONS
:
5482 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
5483 uint32_t remaining_data
= guest_data_size
;
5484 void *cur_data
= argptr
;
5485 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
5486 int vers_size
= thunk_type_size(arg_type
, 0);
5489 uint32_t next
= vers
->next
;
5491 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
5493 if (remaining_data
< vers
->next
) {
5494 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5497 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
5498 strcpy(cur_data
+ vers_size
, vers
->name
);
5499 cur_data
+= vers
->next
;
5500 remaining_data
-= vers
->next
;
5504 vers
= (void*)vers
+ next
;
5509 unlock_user(argptr
, guest_data
, 0);
5510 ret
= -TARGET_EINVAL
;
5513 unlock_user(argptr
, guest_data
, guest_data_size
);
5515 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5517 ret
= -TARGET_EFAULT
;
5520 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5521 unlock_user(argptr
, arg
, target_size
);
5528 static abi_long
do_ioctl_blkpg(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5529 int cmd
, abi_long arg
)
5533 const argtype
*arg_type
= ie
->arg_type
;
5534 const argtype part_arg_type
[] = { MK_STRUCT(STRUCT_blkpg_partition
) };
5537 struct blkpg_ioctl_arg
*host_blkpg
= (void*)buf_temp
;
5538 struct blkpg_partition host_part
;
5540 /* Read and convert blkpg */
5542 target_size
= thunk_type_size(arg_type
, 0);
5543 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5545 ret
= -TARGET_EFAULT
;
5548 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5549 unlock_user(argptr
, arg
, 0);
5551 switch (host_blkpg
->op
) {
5552 case BLKPG_ADD_PARTITION
:
5553 case BLKPG_DEL_PARTITION
:
5554 /* payload is struct blkpg_partition */
5557 /* Unknown opcode */
5558 ret
= -TARGET_EINVAL
;
5562 /* Read and convert blkpg->data */
5563 arg
= (abi_long
)(uintptr_t)host_blkpg
->data
;
5564 target_size
= thunk_type_size(part_arg_type
, 0);
5565 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5567 ret
= -TARGET_EFAULT
;
5570 thunk_convert(&host_part
, argptr
, part_arg_type
, THUNK_HOST
);
5571 unlock_user(argptr
, arg
, 0);
5573 /* Swizzle the data pointer to our local copy and call! */
5574 host_blkpg
->data
= &host_part
;
5575 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_blkpg
));
5581 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5582 int fd
, int cmd
, abi_long arg
)
5584 const argtype
*arg_type
= ie
->arg_type
;
5585 const StructEntry
*se
;
5586 const argtype
*field_types
;
5587 const int *dst_offsets
, *src_offsets
;
5590 abi_ulong
*target_rt_dev_ptr
;
5591 unsigned long *host_rt_dev_ptr
;
5595 assert(ie
->access
== IOC_W
);
5596 assert(*arg_type
== TYPE_PTR
);
5598 assert(*arg_type
== TYPE_STRUCT
);
5599 target_size
= thunk_type_size(arg_type
, 0);
5600 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5602 return -TARGET_EFAULT
;
5605 assert(*arg_type
== (int)STRUCT_rtentry
);
5606 se
= struct_entries
+ *arg_type
++;
5607 assert(se
->convert
[0] == NULL
);
5608 /* convert struct here to be able to catch rt_dev string */
5609 field_types
= se
->field_types
;
5610 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
5611 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
5612 for (i
= 0; i
< se
->nb_fields
; i
++) {
5613 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
5614 assert(*field_types
== TYPE_PTRVOID
);
5615 target_rt_dev_ptr
= (abi_ulong
*)(argptr
+ src_offsets
[i
]);
5616 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
5617 if (*target_rt_dev_ptr
!= 0) {
5618 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
5619 tswapal(*target_rt_dev_ptr
));
5620 if (!*host_rt_dev_ptr
) {
5621 unlock_user(argptr
, arg
, 0);
5622 return -TARGET_EFAULT
;
5625 *host_rt_dev_ptr
= 0;
5630 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
5631 argptr
+ src_offsets
[i
],
5632 field_types
, THUNK_HOST
);
5634 unlock_user(argptr
, arg
, 0);
5636 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5637 if (*host_rt_dev_ptr
!= 0) {
5638 unlock_user((void *)*host_rt_dev_ptr
,
5639 *target_rt_dev_ptr
, 0);
5644 static abi_long
do_ioctl_kdsigaccept(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5645 int fd
, int cmd
, abi_long arg
)
5647 int sig
= target_to_host_signal(arg
);
5648 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, sig
));
5652 static abi_long
do_ioctl_tiocgptpeer(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5653 int fd
, int cmd
, abi_long arg
)
5655 int flags
= target_to_host_bitmask(arg
, fcntl_flags_tbl
);
5656 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, flags
));
5660 static IOCTLEntry ioctl_entries
[] = {
5661 #define IOCTL(cmd, access, ...) \
5662 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5663 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5664 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
5665 #define IOCTL_IGNORE(cmd) \
5666 { TARGET_ ## cmd, 0, #cmd },
5671 /* ??? Implement proper locking for ioctls. */
5672 /* do_ioctl() Must return target values and target errnos. */
5673 static abi_long
do_ioctl(int fd
, int cmd
, abi_long arg
)
5675 const IOCTLEntry
*ie
;
5676 const argtype
*arg_type
;
5678 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
5684 if (ie
->target_cmd
== 0) {
5685 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
5686 return -TARGET_ENOSYS
;
5688 if (ie
->target_cmd
== cmd
)
5692 arg_type
= ie
->arg_type
;
5694 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd
, ie
->name
);
5697 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
5698 } else if (!ie
->host_cmd
) {
5699 /* Some architectures define BSD ioctls in their headers
5700 that are not implemented in Linux. */
5701 return -TARGET_ENOSYS
;
5704 switch(arg_type
[0]) {
5707 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
));
5711 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, arg
));
5715 target_size
= thunk_type_size(arg_type
, 0);
5716 switch(ie
->access
) {
5718 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5719 if (!is_error(ret
)) {
5720 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5722 return -TARGET_EFAULT
;
5723 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5724 unlock_user(argptr
, arg
, target_size
);
5728 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5730 return -TARGET_EFAULT
;
5731 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5732 unlock_user(argptr
, arg
, 0);
5733 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5737 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5739 return -TARGET_EFAULT
;
5740 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5741 unlock_user(argptr
, arg
, 0);
5742 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5743 if (!is_error(ret
)) {
5744 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5746 return -TARGET_EFAULT
;
5747 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5748 unlock_user(argptr
, arg
, target_size
);
5754 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5755 (long)cmd
, arg_type
[0]);
5756 ret
= -TARGET_ENOSYS
;
5762 static const bitmask_transtbl iflag_tbl
[] = {
5763 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
5764 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
5765 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
5766 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
5767 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
5768 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
5769 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
5770 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
5771 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
5772 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
5773 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
5774 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
5775 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
5776 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
5780 static const bitmask_transtbl oflag_tbl
[] = {
5781 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
5782 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
5783 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
5784 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
5785 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
5786 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
5787 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
5788 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
5789 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
5790 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
5791 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
5792 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
5793 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
5794 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
5795 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
5796 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
5797 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
5798 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
5799 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
5800 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
5801 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
5802 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
5803 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
5804 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
5808 static const bitmask_transtbl cflag_tbl
[] = {
5809 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
5810 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
5811 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
5812 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
5813 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
5814 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
5815 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
5816 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
5817 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
5818 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
5819 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
5820 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
5821 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
5822 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
5823 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
5824 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
5825 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
5826 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
5827 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
5828 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
5829 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
5830 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
5831 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
5832 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
5833 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
5834 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
5835 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
5836 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
5837 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
5838 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
5839 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
5843 static const bitmask_transtbl lflag_tbl
[] = {
5844 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
5845 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
5846 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
5847 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
5848 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
5849 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
5850 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
5851 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
5852 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
5853 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
5854 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
5855 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
5856 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
5857 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
5858 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
5862 static void target_to_host_termios (void *dst
, const void *src
)
5864 struct host_termios
*host
= dst
;
5865 const struct target_termios
*target
= src
;
5868 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
5870 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
5872 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
5874 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
5875 host
->c_line
= target
->c_line
;
5877 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
5878 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
5879 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
5880 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
5881 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
5882 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
5883 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
5884 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
5885 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
5886 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
5887 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
5888 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
5889 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
5890 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
5891 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
5892 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
5893 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
5894 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
5897 static void host_to_target_termios (void *dst
, const void *src
)
5899 struct target_termios
*target
= dst
;
5900 const struct host_termios
*host
= src
;
5903 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
5905 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
5907 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
5909 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
5910 target
->c_line
= host
->c_line
;
5912 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
5913 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
5914 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
5915 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
5916 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
5917 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
5918 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
5919 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
5920 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
5921 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
5922 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
5923 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
5924 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
5925 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
5926 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
5927 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
5928 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
5929 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
5932 static const StructEntry struct_termios_def
= {
5933 .convert
= { host_to_target_termios
, target_to_host_termios
},
5934 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
5935 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
5938 static bitmask_transtbl mmap_flags_tbl
[] = {
5939 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
5940 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
5941 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
5942 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
,
5943 MAP_ANONYMOUS
, MAP_ANONYMOUS
},
5944 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
,
5945 MAP_GROWSDOWN
, MAP_GROWSDOWN
},
5946 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
,
5947 MAP_DENYWRITE
, MAP_DENYWRITE
},
5948 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
,
5949 MAP_EXECUTABLE
, MAP_EXECUTABLE
},
5950 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
5951 { TARGET_MAP_NORESERVE
, TARGET_MAP_NORESERVE
,
5952 MAP_NORESERVE
, MAP_NORESERVE
},
5953 { TARGET_MAP_HUGETLB
, TARGET_MAP_HUGETLB
, MAP_HUGETLB
, MAP_HUGETLB
},
5954 /* MAP_STACK had been ignored by the kernel for quite some time.
5955 Recognize it for the target insofar as we do not want to pass
5956 it through to the host. */
5957 { TARGET_MAP_STACK
, TARGET_MAP_STACK
, 0, 0 },
5961 #if defined(TARGET_I386)
5963 /* NOTE: there is really one LDT for all the threads */
5964 static uint8_t *ldt_table
;
5966 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
5973 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
5974 if (size
> bytecount
)
5976 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
5978 return -TARGET_EFAULT
;
5979 /* ??? Should this by byteswapped? */
5980 memcpy(p
, ldt_table
, size
);
5981 unlock_user(p
, ptr
, size
);
5985 /* XXX: add locking support */
5986 static abi_long
write_ldt(CPUX86State
*env
,
5987 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
5989 struct target_modify_ldt_ldt_s ldt_info
;
5990 struct target_modify_ldt_ldt_s
*target_ldt_info
;
5991 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
5992 int seg_not_present
, useable
, lm
;
5993 uint32_t *lp
, entry_1
, entry_2
;
5995 if (bytecount
!= sizeof(ldt_info
))
5996 return -TARGET_EINVAL
;
5997 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
5998 return -TARGET_EFAULT
;
5999 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
6000 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
6001 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
6002 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
6003 unlock_user_struct(target_ldt_info
, ptr
, 0);
6005 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
6006 return -TARGET_EINVAL
;
6007 seg_32bit
= ldt_info
.flags
& 1;
6008 contents
= (ldt_info
.flags
>> 1) & 3;
6009 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
6010 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
6011 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
6012 useable
= (ldt_info
.flags
>> 6) & 1;
6016 lm
= (ldt_info
.flags
>> 7) & 1;
6018 if (contents
== 3) {
6020 return -TARGET_EINVAL
;
6021 if (seg_not_present
== 0)
6022 return -TARGET_EINVAL
;
6024 /* allocate the LDT */
6026 env
->ldt
.base
= target_mmap(0,
6027 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
6028 PROT_READ
|PROT_WRITE
,
6029 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
6030 if (env
->ldt
.base
== -1)
6031 return -TARGET_ENOMEM
;
6032 memset(g2h(env
->ldt
.base
), 0,
6033 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
6034 env
->ldt
.limit
= 0xffff;
6035 ldt_table
= g2h(env
->ldt
.base
);
6038 /* NOTE: same code as Linux kernel */
6039 /* Allow LDTs to be cleared by the user. */
6040 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6043 read_exec_only
== 1 &&
6045 limit_in_pages
== 0 &&
6046 seg_not_present
== 1 &&
6054 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6055 (ldt_info
.limit
& 0x0ffff);
6056 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6057 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6058 (ldt_info
.limit
& 0xf0000) |
6059 ((read_exec_only
^ 1) << 9) |
6061 ((seg_not_present
^ 1) << 15) |
6063 (limit_in_pages
<< 23) |
6067 entry_2
|= (useable
<< 20);
6069 /* Install the new entry ... */
6071 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
6072 lp
[0] = tswap32(entry_1
);
6073 lp
[1] = tswap32(entry_2
);
6077 /* specific and weird i386 syscalls */
6078 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
6079 unsigned long bytecount
)
6085 ret
= read_ldt(ptr
, bytecount
);
6088 ret
= write_ldt(env
, ptr
, bytecount
, 1);
6091 ret
= write_ldt(env
, ptr
, bytecount
, 0);
6094 ret
= -TARGET_ENOSYS
;
6100 #if defined(TARGET_I386) && defined(TARGET_ABI32)
6101 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6103 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
6104 struct target_modify_ldt_ldt_s ldt_info
;
6105 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6106 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
6107 int seg_not_present
, useable
, lm
;
6108 uint32_t *lp
, entry_1
, entry_2
;
6111 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6112 if (!target_ldt_info
)
6113 return -TARGET_EFAULT
;
6114 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
6115 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
6116 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
6117 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
6118 if (ldt_info
.entry_number
== -1) {
6119 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
6120 if (gdt_table
[i
] == 0) {
6121 ldt_info
.entry_number
= i
;
6122 target_ldt_info
->entry_number
= tswap32(i
);
6127 unlock_user_struct(target_ldt_info
, ptr
, 1);
6129 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
6130 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
6131 return -TARGET_EINVAL
;
6132 seg_32bit
= ldt_info
.flags
& 1;
6133 contents
= (ldt_info
.flags
>> 1) & 3;
6134 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
6135 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
6136 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
6137 useable
= (ldt_info
.flags
>> 6) & 1;
6141 lm
= (ldt_info
.flags
>> 7) & 1;
6144 if (contents
== 3) {
6145 if (seg_not_present
== 0)
6146 return -TARGET_EINVAL
;
6149 /* NOTE: same code as Linux kernel */
6150 /* Allow LDTs to be cleared by the user. */
6151 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6152 if ((contents
== 0 &&
6153 read_exec_only
== 1 &&
6155 limit_in_pages
== 0 &&
6156 seg_not_present
== 1 &&
6164 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6165 (ldt_info
.limit
& 0x0ffff);
6166 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6167 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6168 (ldt_info
.limit
& 0xf0000) |
6169 ((read_exec_only
^ 1) << 9) |
6171 ((seg_not_present
^ 1) << 15) |
6173 (limit_in_pages
<< 23) |
6178 /* Install the new entry ... */
6180 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
6181 lp
[0] = tswap32(entry_1
);
6182 lp
[1] = tswap32(entry_2
);
6186 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6188 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6189 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
6190 uint32_t base_addr
, limit
, flags
;
6191 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
6192 int seg_not_present
, useable
, lm
;
6193 uint32_t *lp
, entry_1
, entry_2
;
6195 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6196 if (!target_ldt_info
)
6197 return -TARGET_EFAULT
;
6198 idx
= tswap32(target_ldt_info
->entry_number
);
6199 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
6200 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
6201 unlock_user_struct(target_ldt_info
, ptr
, 1);
6202 return -TARGET_EINVAL
;
6204 lp
= (uint32_t *)(gdt_table
+ idx
);
6205 entry_1
= tswap32(lp
[0]);
6206 entry_2
= tswap32(lp
[1]);
6208 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
6209 contents
= (entry_2
>> 10) & 3;
6210 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
6211 seg_32bit
= (entry_2
>> 22) & 1;
6212 limit_in_pages
= (entry_2
>> 23) & 1;
6213 useable
= (entry_2
>> 20) & 1;
6217 lm
= (entry_2
>> 21) & 1;
6219 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
6220 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
6221 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
6222 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
6223 base_addr
= (entry_1
>> 16) |
6224 (entry_2
& 0xff000000) |
6225 ((entry_2
& 0xff) << 16);
6226 target_ldt_info
->base_addr
= tswapal(base_addr
);
6227 target_ldt_info
->limit
= tswap32(limit
);
6228 target_ldt_info
->flags
= tswap32(flags
);
6229 unlock_user_struct(target_ldt_info
, ptr
, 1);
6232 #endif /* TARGET_I386 && TARGET_ABI32 */
6234 #ifndef TARGET_ABI32
6235 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
6242 case TARGET_ARCH_SET_GS
:
6243 case TARGET_ARCH_SET_FS
:
6244 if (code
== TARGET_ARCH_SET_GS
)
6248 cpu_x86_load_seg(env
, idx
, 0);
6249 env
->segs
[idx
].base
= addr
;
6251 case TARGET_ARCH_GET_GS
:
6252 case TARGET_ARCH_GET_FS
:
6253 if (code
== TARGET_ARCH_GET_GS
)
6257 val
= env
->segs
[idx
].base
;
6258 if (put_user(val
, addr
, abi_ulong
))
6259 ret
= -TARGET_EFAULT
;
6262 ret
= -TARGET_EINVAL
;
6269 #endif /* defined(TARGET_I386) */
6271 #define NEW_STACK_SIZE 0x40000
6274 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
6277 pthread_mutex_t mutex
;
6278 pthread_cond_t cond
;
6281 abi_ulong child_tidptr
;
6282 abi_ulong parent_tidptr
;
6286 static void * QEMU_NORETURN
clone_func(void *arg
)
6288 new_thread_info
*info
= arg
;
6293 rcu_register_thread();
6294 tcg_register_thread();
6296 cpu
= ENV_GET_CPU(env
);
6298 ts
= (TaskState
*)cpu
->opaque
;
6299 info
->tid
= gettid();
6301 if (info
->child_tidptr
)
6302 put_user_u32(info
->tid
, info
->child_tidptr
);
6303 if (info
->parent_tidptr
)
6304 put_user_u32(info
->tid
, info
->parent_tidptr
);
6305 /* Enable signals. */
6306 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
6307 /* Signal to the parent that we're ready. */
6308 pthread_mutex_lock(&info
->mutex
);
6309 pthread_cond_broadcast(&info
->cond
);
6310 pthread_mutex_unlock(&info
->mutex
);
6311 /* Wait until the parent has finished initializing the tls state. */
6312 pthread_mutex_lock(&clone_lock
);
6313 pthread_mutex_unlock(&clone_lock
);
6318 /* do_fork() Must return host values and target errnos (unlike most
6319 do_*() functions). */
6320 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
6321 abi_ulong parent_tidptr
, target_ulong newtls
,
6322 abi_ulong child_tidptr
)
6324 CPUState
*cpu
= ENV_GET_CPU(env
);
6328 CPUArchState
*new_env
;
6331 flags
&= ~CLONE_IGNORED_FLAGS
;
6333 /* Emulate vfork() with fork() */
6334 if (flags
& CLONE_VFORK
)
6335 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
6337 if (flags
& CLONE_VM
) {
6338 TaskState
*parent_ts
= (TaskState
*)cpu
->opaque
;
6339 new_thread_info info
;
6340 pthread_attr_t attr
;
6342 if (((flags
& CLONE_THREAD_FLAGS
) != CLONE_THREAD_FLAGS
) ||
6343 (flags
& CLONE_INVALID_THREAD_FLAGS
)) {
6344 return -TARGET_EINVAL
;
6347 ts
= g_new0(TaskState
, 1);
6348 init_task_state(ts
);
6350 /* Grab a mutex so that thread setup appears atomic. */
6351 pthread_mutex_lock(&clone_lock
);
6353 /* we create a new CPU instance. */
6354 new_env
= cpu_copy(env
);
6355 /* Init regs that differ from the parent. */
6356 cpu_clone_regs(new_env
, newsp
);
6357 new_cpu
= ENV_GET_CPU(new_env
);
6358 new_cpu
->opaque
= ts
;
6359 ts
->bprm
= parent_ts
->bprm
;
6360 ts
->info
= parent_ts
->info
;
6361 ts
->signal_mask
= parent_ts
->signal_mask
;
6363 if (flags
& CLONE_CHILD_CLEARTID
) {
6364 ts
->child_tidptr
= child_tidptr
;
6367 if (flags
& CLONE_SETTLS
) {
6368 cpu_set_tls (new_env
, newtls
);
6371 memset(&info
, 0, sizeof(info
));
6372 pthread_mutex_init(&info
.mutex
, NULL
);
6373 pthread_mutex_lock(&info
.mutex
);
6374 pthread_cond_init(&info
.cond
, NULL
);
6376 if (flags
& CLONE_CHILD_SETTID
) {
6377 info
.child_tidptr
= child_tidptr
;
6379 if (flags
& CLONE_PARENT_SETTID
) {
6380 info
.parent_tidptr
= parent_tidptr
;
6383 ret
= pthread_attr_init(&attr
);
6384 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
6385 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
6386 /* It is not safe to deliver signals until the child has finished
6387 initializing, so temporarily block all signals. */
6388 sigfillset(&sigmask
);
6389 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
6391 /* If this is our first additional thread, we need to ensure we
6392 * generate code for parallel execution and flush old translations.
6394 if (!parallel_cpus
) {
6395 parallel_cpus
= true;
6399 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
6400 /* TODO: Free new CPU state if thread creation failed. */
6402 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
6403 pthread_attr_destroy(&attr
);
6405 /* Wait for the child to initialize. */
6406 pthread_cond_wait(&info
.cond
, &info
.mutex
);
6411 pthread_mutex_unlock(&info
.mutex
);
6412 pthread_cond_destroy(&info
.cond
);
6413 pthread_mutex_destroy(&info
.mutex
);
6414 pthread_mutex_unlock(&clone_lock
);
6416 /* if no CLONE_VM, we consider it is a fork */
6417 if (flags
& CLONE_INVALID_FORK_FLAGS
) {
6418 return -TARGET_EINVAL
;
6421 /* We can't support custom termination signals */
6422 if ((flags
& CSIGNAL
) != TARGET_SIGCHLD
) {
6423 return -TARGET_EINVAL
;
6426 if (block_signals()) {
6427 return -TARGET_ERESTARTSYS
;
6433 /* Child Process. */
6434 cpu_clone_regs(env
, newsp
);
6436 /* There is a race condition here. The parent process could
6437 theoretically read the TID in the child process before the child
6438 tid is set. This would require using either ptrace
6439 (not implemented) or having *_tidptr to point at a shared memory
6440 mapping. We can't repeat the spinlock hack used above because
6441 the child process gets its own copy of the lock. */
6442 if (flags
& CLONE_CHILD_SETTID
)
6443 put_user_u32(gettid(), child_tidptr
);
6444 if (flags
& CLONE_PARENT_SETTID
)
6445 put_user_u32(gettid(), parent_tidptr
);
6446 ts
= (TaskState
*)cpu
->opaque
;
6447 if (flags
& CLONE_SETTLS
)
6448 cpu_set_tls (env
, newtls
);
6449 if (flags
& CLONE_CHILD_CLEARTID
)
6450 ts
->child_tidptr
= child_tidptr
;
6458 /* warning : doesn't handle linux specific flags... */
6459 static int target_to_host_fcntl_cmd(int cmd
)
6462 case TARGET_F_DUPFD
:
6463 case TARGET_F_GETFD
:
6464 case TARGET_F_SETFD
:
6465 case TARGET_F_GETFL
:
6466 case TARGET_F_SETFL
:
6468 case TARGET_F_GETLK
:
6470 case TARGET_F_SETLK
:
6472 case TARGET_F_SETLKW
:
6474 case TARGET_F_GETOWN
:
6476 case TARGET_F_SETOWN
:
6478 case TARGET_F_GETSIG
:
6480 case TARGET_F_SETSIG
:
6482 #if TARGET_ABI_BITS == 32
6483 case TARGET_F_GETLK64
:
6485 case TARGET_F_SETLK64
:
6487 case TARGET_F_SETLKW64
:
6490 case TARGET_F_SETLEASE
:
6492 case TARGET_F_GETLEASE
:
6494 #ifdef F_DUPFD_CLOEXEC
6495 case TARGET_F_DUPFD_CLOEXEC
:
6496 return F_DUPFD_CLOEXEC
;
6498 case TARGET_F_NOTIFY
:
6501 case TARGET_F_GETOWN_EX
:
6505 case TARGET_F_SETOWN_EX
:
6509 case TARGET_F_SETPIPE_SZ
:
6510 return F_SETPIPE_SZ
;
6511 case TARGET_F_GETPIPE_SZ
:
6512 return F_GETPIPE_SZ
;
6515 return -TARGET_EINVAL
;
6517 return -TARGET_EINVAL
;
6520 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
6521 static const bitmask_transtbl flock_tbl
[] = {
6522 TRANSTBL_CONVERT(F_RDLCK
),
6523 TRANSTBL_CONVERT(F_WRLCK
),
6524 TRANSTBL_CONVERT(F_UNLCK
),
6525 TRANSTBL_CONVERT(F_EXLCK
),
6526 TRANSTBL_CONVERT(F_SHLCK
),
6530 static inline abi_long
copy_from_user_flock(struct flock64
*fl
,
6531 abi_ulong target_flock_addr
)
6533 struct target_flock
*target_fl
;
6536 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6537 return -TARGET_EFAULT
;
6540 __get_user(l_type
, &target_fl
->l_type
);
6541 fl
->l_type
= target_to_host_bitmask(l_type
, flock_tbl
);
6542 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6543 __get_user(fl
->l_start
, &target_fl
->l_start
);
6544 __get_user(fl
->l_len
, &target_fl
->l_len
);
6545 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6546 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6550 static inline abi_long
copy_to_user_flock(abi_ulong target_flock_addr
,
6551 const struct flock64
*fl
)
6553 struct target_flock
*target_fl
;
6556 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6557 return -TARGET_EFAULT
;
6560 l_type
= host_to_target_bitmask(fl
->l_type
, flock_tbl
);
6561 __put_user(l_type
, &target_fl
->l_type
);
6562 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6563 __put_user(fl
->l_start
, &target_fl
->l_start
);
6564 __put_user(fl
->l_len
, &target_fl
->l_len
);
6565 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6566 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6570 typedef abi_long
from_flock64_fn(struct flock64
*fl
, abi_ulong target_addr
);
6571 typedef abi_long
to_flock64_fn(abi_ulong target_addr
, const struct flock64
*fl
);
6573 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6574 static inline abi_long
copy_from_user_eabi_flock64(struct flock64
*fl
,
6575 abi_ulong target_flock_addr
)
6577 struct target_eabi_flock64
*target_fl
;
6580 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6581 return -TARGET_EFAULT
;
6584 __get_user(l_type
, &target_fl
->l_type
);
6585 fl
->l_type
= target_to_host_bitmask(l_type
, flock_tbl
);
6586 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6587 __get_user(fl
->l_start
, &target_fl
->l_start
);
6588 __get_user(fl
->l_len
, &target_fl
->l_len
);
6589 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6590 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6594 static inline abi_long
copy_to_user_eabi_flock64(abi_ulong target_flock_addr
,
6595 const struct flock64
*fl
)
6597 struct target_eabi_flock64
*target_fl
;
6600 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6601 return -TARGET_EFAULT
;
6604 l_type
= host_to_target_bitmask(fl
->l_type
, flock_tbl
);
6605 __put_user(l_type
, &target_fl
->l_type
);
6606 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6607 __put_user(fl
->l_start
, &target_fl
->l_start
);
6608 __put_user(fl
->l_len
, &target_fl
->l_len
);
6609 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6610 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6615 static inline abi_long
copy_from_user_flock64(struct flock64
*fl
,
6616 abi_ulong target_flock_addr
)
6618 struct target_flock64
*target_fl
;
6621 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6622 return -TARGET_EFAULT
;
6625 __get_user(l_type
, &target_fl
->l_type
);
6626 fl
->l_type
= target_to_host_bitmask(l_type
, flock_tbl
);
6627 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6628 __get_user(fl
->l_start
, &target_fl
->l_start
);
6629 __get_user(fl
->l_len
, &target_fl
->l_len
);
6630 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6631 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6635 static inline abi_long
copy_to_user_flock64(abi_ulong target_flock_addr
,
6636 const struct flock64
*fl
)
6638 struct target_flock64
*target_fl
;
6641 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6642 return -TARGET_EFAULT
;
6645 l_type
= host_to_target_bitmask(fl
->l_type
, flock_tbl
);
6646 __put_user(l_type
, &target_fl
->l_type
);
6647 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6648 __put_user(fl
->l_start
, &target_fl
->l_start
);
6649 __put_user(fl
->l_len
, &target_fl
->l_len
);
6650 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6651 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6655 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
6657 struct flock64 fl64
;
6659 struct f_owner_ex fox
;
6660 struct target_f_owner_ex
*target_fox
;
6663 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
6665 if (host_cmd
== -TARGET_EINVAL
)
6669 case TARGET_F_GETLK
:
6670 ret
= copy_from_user_flock(&fl64
, arg
);
6674 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6676 ret
= copy_to_user_flock(arg
, &fl64
);
6680 case TARGET_F_SETLK
:
6681 case TARGET_F_SETLKW
:
6682 ret
= copy_from_user_flock(&fl64
, arg
);
6686 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6689 case TARGET_F_GETLK64
:
6690 ret
= copy_from_user_flock64(&fl64
, arg
);
6694 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6696 ret
= copy_to_user_flock64(arg
, &fl64
);
6699 case TARGET_F_SETLK64
:
6700 case TARGET_F_SETLKW64
:
6701 ret
= copy_from_user_flock64(&fl64
, arg
);
6705 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6708 case TARGET_F_GETFL
:
6709 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
6711 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
6715 case TARGET_F_SETFL
:
6716 ret
= get_errno(safe_fcntl(fd
, host_cmd
,
6717 target_to_host_bitmask(arg
,
6722 case TARGET_F_GETOWN_EX
:
6723 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
6725 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
6726 return -TARGET_EFAULT
;
6727 target_fox
->type
= tswap32(fox
.type
);
6728 target_fox
->pid
= tswap32(fox
.pid
);
6729 unlock_user_struct(target_fox
, arg
, 1);
6735 case TARGET_F_SETOWN_EX
:
6736 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
6737 return -TARGET_EFAULT
;
6738 fox
.type
= tswap32(target_fox
->type
);
6739 fox
.pid
= tswap32(target_fox
->pid
);
6740 unlock_user_struct(target_fox
, arg
, 0);
6741 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
6745 case TARGET_F_SETOWN
:
6746 case TARGET_F_GETOWN
:
6747 case TARGET_F_SETSIG
:
6748 case TARGET_F_GETSIG
:
6749 case TARGET_F_SETLEASE
:
6750 case TARGET_F_GETLEASE
:
6751 case TARGET_F_SETPIPE_SZ
:
6752 case TARGET_F_GETPIPE_SZ
:
6753 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
6757 ret
= get_errno(safe_fcntl(fd
, cmd
, arg
));
6765 static inline int high2lowuid(int uid
)
6773 static inline int high2lowgid(int gid
)
6781 static inline int low2highuid(int uid
)
6783 if ((int16_t)uid
== -1)
6789 static inline int low2highgid(int gid
)
6791 if ((int16_t)gid
== -1)
6796 static inline int tswapid(int id
)
6801 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
6803 #else /* !USE_UID16 */
6804 static inline int high2lowuid(int uid
)
6808 static inline int high2lowgid(int gid
)
6812 static inline int low2highuid(int uid
)
6816 static inline int low2highgid(int gid
)
6820 static inline int tswapid(int id
)
6825 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
6827 #endif /* USE_UID16 */
6829 /* We must do direct syscalls for setting UID/GID, because we want to
6830 * implement the Linux system call semantics of "change only for this thread",
6831 * not the libc/POSIX semantics of "change for all threads in process".
6832 * (See http://ewontfix.com/17/ for more details.)
6833 * We use the 32-bit version of the syscalls if present; if it is not
6834 * then either the host architecture supports 32-bit UIDs natively with
6835 * the standard syscall, or the 16-bit UID is the best we can do.
6837 #ifdef __NR_setuid32
6838 #define __NR_sys_setuid __NR_setuid32
6840 #define __NR_sys_setuid __NR_setuid
6842 #ifdef __NR_setgid32
6843 #define __NR_sys_setgid __NR_setgid32
6845 #define __NR_sys_setgid __NR_setgid
6847 #ifdef __NR_setresuid32
6848 #define __NR_sys_setresuid __NR_setresuid32
6850 #define __NR_sys_setresuid __NR_setresuid
6852 #ifdef __NR_setresgid32
6853 #define __NR_sys_setresgid __NR_setresgid32
6855 #define __NR_sys_setresgid __NR_setresgid
6858 _syscall1(int, sys_setuid
, uid_t
, uid
)
6859 _syscall1(int, sys_setgid
, gid_t
, gid
)
6860 _syscall3(int, sys_setresuid
, uid_t
, ruid
, uid_t
, euid
, uid_t
, suid
)
6861 _syscall3(int, sys_setresgid
, gid_t
, rgid
, gid_t
, egid
, gid_t
, sgid
)
6863 void syscall_init(void)
6866 const argtype
*arg_type
;
6870 thunk_init(STRUCT_MAX
);
6872 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
6873 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
6874 #include "syscall_types.h"
6876 #undef STRUCT_SPECIAL
6878 /* Build target_to_host_errno_table[] table from
6879 * host_to_target_errno_table[]. */
6880 for (i
= 0; i
< ERRNO_TABLE_SIZE
; i
++) {
6881 target_to_host_errno_table
[host_to_target_errno_table
[i
]] = i
;
6884 /* we patch the ioctl size if necessary. We rely on the fact that
6885 no ioctl has all the bits at '1' in the size field */
6887 while (ie
->target_cmd
!= 0) {
6888 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
6889 TARGET_IOC_SIZEMASK
) {
6890 arg_type
= ie
->arg_type
;
6891 if (arg_type
[0] != TYPE_PTR
) {
6892 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
6897 size
= thunk_type_size(arg_type
, 0);
6898 ie
->target_cmd
= (ie
->target_cmd
&
6899 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
6900 (size
<< TARGET_IOC_SIZESHIFT
);
6903 /* automatic consistency check if same arch */
6904 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6905 (defined(__x86_64__) && defined(TARGET_X86_64))
6906 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
6907 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
6908 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
6915 #if TARGET_ABI_BITS == 32
6916 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
6918 #ifdef TARGET_WORDS_BIGENDIAN
6919 return ((uint64_t)word0
<< 32) | word1
;
6921 return ((uint64_t)word1
<< 32) | word0
;
6924 #else /* TARGET_ABI_BITS == 32 */
6925 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
6929 #endif /* TARGET_ABI_BITS != 32 */
6931 #ifdef TARGET_NR_truncate64
6932 static inline abi_long
target_truncate64(void *cpu_env
, const char *arg1
,
6937 if (regpairs_aligned(cpu_env
, TARGET_NR_truncate64
)) {
6941 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
6945 #ifdef TARGET_NR_ftruncate64
6946 static inline abi_long
target_ftruncate64(void *cpu_env
, abi_long arg1
,
6951 if (regpairs_aligned(cpu_env
, TARGET_NR_ftruncate64
)) {
6955 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
6959 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
6960 abi_ulong target_addr
)
6962 struct target_timespec
*target_ts
;
6964 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1))
6965 return -TARGET_EFAULT
;
6966 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
6967 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
6968 unlock_user_struct(target_ts
, target_addr
, 0);
6972 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
6973 struct timespec
*host_ts
)
6975 struct target_timespec
*target_ts
;
6977 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0))
6978 return -TARGET_EFAULT
;
6979 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
6980 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
6981 unlock_user_struct(target_ts
, target_addr
, 1);
6985 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_itspec
,
6986 abi_ulong target_addr
)
6988 struct target_itimerspec
*target_itspec
;
6990 if (!lock_user_struct(VERIFY_READ
, target_itspec
, target_addr
, 1)) {
6991 return -TARGET_EFAULT
;
6994 host_itspec
->it_interval
.tv_sec
=
6995 tswapal(target_itspec
->it_interval
.tv_sec
);
6996 host_itspec
->it_interval
.tv_nsec
=
6997 tswapal(target_itspec
->it_interval
.tv_nsec
);
6998 host_itspec
->it_value
.tv_sec
= tswapal(target_itspec
->it_value
.tv_sec
);
6999 host_itspec
->it_value
.tv_nsec
= tswapal(target_itspec
->it_value
.tv_nsec
);
7001 unlock_user_struct(target_itspec
, target_addr
, 1);
7005 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
7006 struct itimerspec
*host_its
)
7008 struct target_itimerspec
*target_itspec
;
7010 if (!lock_user_struct(VERIFY_WRITE
, target_itspec
, target_addr
, 0)) {
7011 return -TARGET_EFAULT
;
7014 target_itspec
->it_interval
.tv_sec
= tswapal(host_its
->it_interval
.tv_sec
);
7015 target_itspec
->it_interval
.tv_nsec
= tswapal(host_its
->it_interval
.tv_nsec
);
7017 target_itspec
->it_value
.tv_sec
= tswapal(host_its
->it_value
.tv_sec
);
7018 target_itspec
->it_value
.tv_nsec
= tswapal(host_its
->it_value
.tv_nsec
);
7020 unlock_user_struct(target_itspec
, target_addr
, 0);
7024 static inline abi_long
target_to_host_timex(struct timex
*host_tx
,
7025 abi_long target_addr
)
7027 struct target_timex
*target_tx
;
7029 if (!lock_user_struct(VERIFY_READ
, target_tx
, target_addr
, 1)) {
7030 return -TARGET_EFAULT
;
7033 __get_user(host_tx
->modes
, &target_tx
->modes
);
7034 __get_user(host_tx
->offset
, &target_tx
->offset
);
7035 __get_user(host_tx
->freq
, &target_tx
->freq
);
7036 __get_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7037 __get_user(host_tx
->esterror
, &target_tx
->esterror
);
7038 __get_user(host_tx
->status
, &target_tx
->status
);
7039 __get_user(host_tx
->constant
, &target_tx
->constant
);
7040 __get_user(host_tx
->precision
, &target_tx
->precision
);
7041 __get_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7042 __get_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7043 __get_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7044 __get_user(host_tx
->tick
, &target_tx
->tick
);
7045 __get_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7046 __get_user(host_tx
->jitter
, &target_tx
->jitter
);
7047 __get_user(host_tx
->shift
, &target_tx
->shift
);
7048 __get_user(host_tx
->stabil
, &target_tx
->stabil
);
7049 __get_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7050 __get_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7051 __get_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7052 __get_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7053 __get_user(host_tx
->tai
, &target_tx
->tai
);
7055 unlock_user_struct(target_tx
, target_addr
, 0);
7059 static inline abi_long
host_to_target_timex(abi_long target_addr
,
7060 struct timex
*host_tx
)
7062 struct target_timex
*target_tx
;
7064 if (!lock_user_struct(VERIFY_WRITE
, target_tx
, target_addr
, 0)) {
7065 return -TARGET_EFAULT
;
7068 __put_user(host_tx
->modes
, &target_tx
->modes
);
7069 __put_user(host_tx
->offset
, &target_tx
->offset
);
7070 __put_user(host_tx
->freq
, &target_tx
->freq
);
7071 __put_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7072 __put_user(host_tx
->esterror
, &target_tx
->esterror
);
7073 __put_user(host_tx
->status
, &target_tx
->status
);
7074 __put_user(host_tx
->constant
, &target_tx
->constant
);
7075 __put_user(host_tx
->precision
, &target_tx
->precision
);
7076 __put_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7077 __put_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7078 __put_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7079 __put_user(host_tx
->tick
, &target_tx
->tick
);
7080 __put_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7081 __put_user(host_tx
->jitter
, &target_tx
->jitter
);
7082 __put_user(host_tx
->shift
, &target_tx
->shift
);
7083 __put_user(host_tx
->stabil
, &target_tx
->stabil
);
7084 __put_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7085 __put_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7086 __put_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7087 __put_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7088 __put_user(host_tx
->tai
, &target_tx
->tai
);
7090 unlock_user_struct(target_tx
, target_addr
, 1);
7095 static inline abi_long
target_to_host_sigevent(struct sigevent
*host_sevp
,
7096 abi_ulong target_addr
)
7098 struct target_sigevent
*target_sevp
;
7100 if (!lock_user_struct(VERIFY_READ
, target_sevp
, target_addr
, 1)) {
7101 return -TARGET_EFAULT
;
7104 /* This union is awkward on 64 bit systems because it has a 32 bit
7105 * integer and a pointer in it; we follow the conversion approach
7106 * used for handling sigval types in signal.c so the guest should get
7107 * the correct value back even if we did a 64 bit byteswap and it's
7108 * using the 32 bit integer.
7110 host_sevp
->sigev_value
.sival_ptr
=
7111 (void *)(uintptr_t)tswapal(target_sevp
->sigev_value
.sival_ptr
);
7112 host_sevp
->sigev_signo
=
7113 target_to_host_signal(tswap32(target_sevp
->sigev_signo
));
7114 host_sevp
->sigev_notify
= tswap32(target_sevp
->sigev_notify
);
7115 host_sevp
->_sigev_un
._tid
= tswap32(target_sevp
->_sigev_un
._tid
);
7117 unlock_user_struct(target_sevp
, target_addr
, 1);
7121 #if defined(TARGET_NR_mlockall)
7122 static inline int target_to_host_mlockall_arg(int arg
)
7126 if (arg
& TARGET_MLOCKALL_MCL_CURRENT
) {
7127 result
|= MCL_CURRENT
;
7129 if (arg
& TARGET_MLOCKALL_MCL_FUTURE
) {
7130 result
|= MCL_FUTURE
;
7136 static inline abi_long
host_to_target_stat64(void *cpu_env
,
7137 abi_ulong target_addr
,
7138 struct stat
*host_st
)
7140 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
7141 if (((CPUARMState
*)cpu_env
)->eabi
) {
7142 struct target_eabi_stat64
*target_st
;
7144 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7145 return -TARGET_EFAULT
;
7146 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
7147 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7148 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7149 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7150 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7152 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7153 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7154 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7155 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7156 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7157 __put_user(host_st
->st_size
, &target_st
->st_size
);
7158 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7159 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7160 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7161 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7162 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7163 unlock_user_struct(target_st
, target_addr
, 1);
7167 #if defined(TARGET_HAS_STRUCT_STAT64)
7168 struct target_stat64
*target_st
;
7170 struct target_stat
*target_st
;
7173 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7174 return -TARGET_EFAULT
;
7175 memset(target_st
, 0, sizeof(*target_st
));
7176 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7177 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7178 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7179 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7181 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7182 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7183 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7184 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7185 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7186 /* XXX: better use of kernel struct */
7187 __put_user(host_st
->st_size
, &target_st
->st_size
);
7188 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7189 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7190 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7191 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7192 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7193 unlock_user_struct(target_st
, target_addr
, 1);
7199 /* ??? Using host futex calls even when target atomic operations
7200 are not really atomic probably breaks things. However implementing
7201 futexes locally would make futexes shared between multiple processes
7202 tricky. However they're probably useless because guest atomic
7203 operations won't work either. */
7204 static int do_futex(target_ulong uaddr
, int op
, int val
, target_ulong timeout
,
7205 target_ulong uaddr2
, int val3
)
7207 struct timespec ts
, *pts
;
7210 /* ??? We assume FUTEX_* constants are the same on both host
7212 #ifdef FUTEX_CMD_MASK
7213 base_op
= op
& FUTEX_CMD_MASK
;
7219 case FUTEX_WAIT_BITSET
:
7222 target_to_host_timespec(pts
, timeout
);
7226 return get_errno(safe_futex(g2h(uaddr
), op
, tswap32(val
),
7229 return get_errno(safe_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
7231 return get_errno(safe_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
7233 case FUTEX_CMP_REQUEUE
:
7235 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
7236 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
7237 But the prototype takes a `struct timespec *'; insert casts
7238 to satisfy the compiler. We do not need to tswap TIMEOUT
7239 since it's not compared to guest memory. */
7240 pts
= (struct timespec
*)(uintptr_t) timeout
;
7241 return get_errno(safe_futex(g2h(uaddr
), op
, val
, pts
,
7243 (base_op
== FUTEX_CMP_REQUEUE
7247 return -TARGET_ENOSYS
;
7250 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7251 static abi_long
do_name_to_handle_at(abi_long dirfd
, abi_long pathname
,
7252 abi_long handle
, abi_long mount_id
,
7255 struct file_handle
*target_fh
;
7256 struct file_handle
*fh
;
7260 unsigned int size
, total_size
;
7262 if (get_user_s32(size
, handle
)) {
7263 return -TARGET_EFAULT
;
7266 name
= lock_user_string(pathname
);
7268 return -TARGET_EFAULT
;
7271 total_size
= sizeof(struct file_handle
) + size
;
7272 target_fh
= lock_user(VERIFY_WRITE
, handle
, total_size
, 0);
7274 unlock_user(name
, pathname
, 0);
7275 return -TARGET_EFAULT
;
7278 fh
= g_malloc0(total_size
);
7279 fh
->handle_bytes
= size
;
7281 ret
= get_errno(name_to_handle_at(dirfd
, path(name
), fh
, &mid
, flags
));
7282 unlock_user(name
, pathname
, 0);
7284 /* man name_to_handle_at(2):
7285 * Other than the use of the handle_bytes field, the caller should treat
7286 * the file_handle structure as an opaque data type
7289 memcpy(target_fh
, fh
, total_size
);
7290 target_fh
->handle_bytes
= tswap32(fh
->handle_bytes
);
7291 target_fh
->handle_type
= tswap32(fh
->handle_type
);
7293 unlock_user(target_fh
, handle
, total_size
);
7295 if (put_user_s32(mid
, mount_id
)) {
7296 return -TARGET_EFAULT
;
7304 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7305 static abi_long
do_open_by_handle_at(abi_long mount_fd
, abi_long handle
,
7308 struct file_handle
*target_fh
;
7309 struct file_handle
*fh
;
7310 unsigned int size
, total_size
;
7313 if (get_user_s32(size
, handle
)) {
7314 return -TARGET_EFAULT
;
7317 total_size
= sizeof(struct file_handle
) + size
;
7318 target_fh
= lock_user(VERIFY_READ
, handle
, total_size
, 1);
7320 return -TARGET_EFAULT
;
7323 fh
= g_memdup(target_fh
, total_size
);
7324 fh
->handle_bytes
= size
;
7325 fh
->handle_type
= tswap32(target_fh
->handle_type
);
7327 ret
= get_errno(open_by_handle_at(mount_fd
, fh
,
7328 target_to_host_bitmask(flags
, fcntl_flags_tbl
)));
7332 unlock_user(target_fh
, handle
, total_size
);
7338 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7340 /* signalfd siginfo conversion */
7343 host_to_target_signalfd_siginfo(struct signalfd_siginfo
*tinfo
,
7344 const struct signalfd_siginfo
*info
)
7346 int sig
= host_to_target_signal(info
->ssi_signo
);
7348 /* linux/signalfd.h defines a ssi_addr_lsb
7349 * not defined in sys/signalfd.h but used by some kernels
7352 #ifdef BUS_MCEERR_AO
7353 if (tinfo
->ssi_signo
== SIGBUS
&&
7354 (tinfo
->ssi_code
== BUS_MCEERR_AR
||
7355 tinfo
->ssi_code
== BUS_MCEERR_AO
)) {
7356 uint16_t *ssi_addr_lsb
= (uint16_t *)(&info
->ssi_addr
+ 1);
7357 uint16_t *tssi_addr_lsb
= (uint16_t *)(&tinfo
->ssi_addr
+ 1);
7358 *tssi_addr_lsb
= tswap16(*ssi_addr_lsb
);
7362 tinfo
->ssi_signo
= tswap32(sig
);
7363 tinfo
->ssi_errno
= tswap32(tinfo
->ssi_errno
);
7364 tinfo
->ssi_code
= tswap32(info
->ssi_code
);
7365 tinfo
->ssi_pid
= tswap32(info
->ssi_pid
);
7366 tinfo
->ssi_uid
= tswap32(info
->ssi_uid
);
7367 tinfo
->ssi_fd
= tswap32(info
->ssi_fd
);
7368 tinfo
->ssi_tid
= tswap32(info
->ssi_tid
);
7369 tinfo
->ssi_band
= tswap32(info
->ssi_band
);
7370 tinfo
->ssi_overrun
= tswap32(info
->ssi_overrun
);
7371 tinfo
->ssi_trapno
= tswap32(info
->ssi_trapno
);
7372 tinfo
->ssi_status
= tswap32(info
->ssi_status
);
7373 tinfo
->ssi_int
= tswap32(info
->ssi_int
);
7374 tinfo
->ssi_ptr
= tswap64(info
->ssi_ptr
);
7375 tinfo
->ssi_utime
= tswap64(info
->ssi_utime
);
7376 tinfo
->ssi_stime
= tswap64(info
->ssi_stime
);
7377 tinfo
->ssi_addr
= tswap64(info
->ssi_addr
);
7380 static abi_long
host_to_target_data_signalfd(void *buf
, size_t len
)
7384 for (i
= 0; i
< len
; i
+= sizeof(struct signalfd_siginfo
)) {
7385 host_to_target_signalfd_siginfo(buf
+ i
, buf
+ i
);
7391 static TargetFdTrans target_signalfd_trans
= {
7392 .host_to_target_data
= host_to_target_data_signalfd
,
7395 static abi_long
do_signalfd4(int fd
, abi_long mask
, int flags
)
7398 target_sigset_t
*target_mask
;
7402 if (flags
& ~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
)) {
7403 return -TARGET_EINVAL
;
7405 if (!lock_user_struct(VERIFY_READ
, target_mask
, mask
, 1)) {
7406 return -TARGET_EFAULT
;
7409 target_to_host_sigset(&host_mask
, target_mask
);
7411 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
7413 ret
= get_errno(signalfd(fd
, &host_mask
, host_flags
));
7415 fd_trans_register(ret
, &target_signalfd_trans
);
7418 unlock_user_struct(target_mask
, mask
, 0);
7424 /* Map host to target signal numbers for the wait family of syscalls.
7425 Assume all other status bits are the same. */
7426 int host_to_target_waitstatus(int status
)
7428 if (WIFSIGNALED(status
)) {
7429 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
7431 if (WIFSTOPPED(status
)) {
7432 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
7438 static int open_self_cmdline(void *cpu_env
, int fd
)
7440 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
7441 struct linux_binprm
*bprm
= ((TaskState
*)cpu
->opaque
)->bprm
;
7444 for (i
= 0; i
< bprm
->argc
; i
++) {
7445 size_t len
= strlen(bprm
->argv
[i
]) + 1;
7447 if (write(fd
, bprm
->argv
[i
], len
) != len
) {
7455 static int open_self_maps(void *cpu_env
, int fd
)
7457 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
7458 TaskState
*ts
= cpu
->opaque
;
7464 fp
= fopen("/proc/self/maps", "r");
7469 while ((read
= getline(&line
, &len
, fp
)) != -1) {
7470 int fields
, dev_maj
, dev_min
, inode
;
7471 uint64_t min
, max
, offset
;
7472 char flag_r
, flag_w
, flag_x
, flag_p
;
7473 char path
[512] = "";
7474 fields
= sscanf(line
, "%"PRIx64
"-%"PRIx64
" %c%c%c%c %"PRIx64
" %x:%x %d"
7475 " %512s", &min
, &max
, &flag_r
, &flag_w
, &flag_x
,
7476 &flag_p
, &offset
, &dev_maj
, &dev_min
, &inode
, path
);
7478 if ((fields
< 10) || (fields
> 11)) {
7481 if (h2g_valid(min
)) {
7482 int flags
= page_get_flags(h2g(min
));
7483 max
= h2g_valid(max
- 1) ? max
: (uintptr_t)g2h(GUEST_ADDR_MAX
) + 1;
7484 if (page_check_range(h2g(min
), max
- min
, flags
) == -1) {
7487 if (h2g(min
) == ts
->info
->stack_limit
) {
7488 pstrcpy(path
, sizeof(path
), " [stack]");
7490 dprintf(fd
, TARGET_ABI_FMT_lx
"-" TARGET_ABI_FMT_lx
7491 " %c%c%c%c %08" PRIx64
" %02x:%02x %d %s%s\n",
7492 h2g(min
), h2g(max
- 1) + 1, flag_r
, flag_w
,
7493 flag_x
, flag_p
, offset
, dev_maj
, dev_min
, inode
,
7494 path
[0] ? " " : "", path
);
7504 static int open_self_stat(void *cpu_env
, int fd
)
7506 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
7507 TaskState
*ts
= cpu
->opaque
;
7508 abi_ulong start_stack
= ts
->info
->start_stack
;
7511 for (i
= 0; i
< 44; i
++) {
7519 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
7520 } else if (i
== 1) {
7522 snprintf(buf
, sizeof(buf
), "(%s) ", ts
->bprm
->argv
[0]);
7523 } else if (i
== 27) {
7526 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
7528 /* for the rest, there is MasterCard */
7529 snprintf(buf
, sizeof(buf
), "0%c", i
== 43 ? '\n' : ' ');
7533 if (write(fd
, buf
, len
) != len
) {
7541 static int open_self_auxv(void *cpu_env
, int fd
)
7543 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
7544 TaskState
*ts
= cpu
->opaque
;
7545 abi_ulong auxv
= ts
->info
->saved_auxv
;
7546 abi_ulong len
= ts
->info
->auxv_len
;
7550 * Auxiliary vector is stored in target process stack.
7551 * read in whole auxv vector and copy it to file
7553 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
7557 r
= write(fd
, ptr
, len
);
7564 lseek(fd
, 0, SEEK_SET
);
7565 unlock_user(ptr
, auxv
, len
);
7571 static int is_proc_myself(const char *filename
, const char *entry
)
7573 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
7574 filename
+= strlen("/proc/");
7575 if (!strncmp(filename
, "self/", strlen("self/"))) {
7576 filename
+= strlen("self/");
7577 } else if (*filename
>= '1' && *filename
<= '9') {
7579 snprintf(myself
, sizeof(myself
), "%d/", getpid());
7580 if (!strncmp(filename
, myself
, strlen(myself
))) {
7581 filename
+= strlen(myself
);
7588 if (!strcmp(filename
, entry
)) {
7595 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7596 static int is_proc(const char *filename
, const char *entry
)
7598 return strcmp(filename
, entry
) == 0;
7601 static int open_net_route(void *cpu_env
, int fd
)
7608 fp
= fopen("/proc/net/route", "r");
7615 read
= getline(&line
, &len
, fp
);
7616 dprintf(fd
, "%s", line
);
7620 while ((read
= getline(&line
, &len
, fp
)) != -1) {
7622 uint32_t dest
, gw
, mask
;
7623 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
7624 sscanf(line
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7625 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
7626 &mask
, &mtu
, &window
, &irtt
);
7627 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7628 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
7629 metric
, tswap32(mask
), mtu
, window
, irtt
);
7639 static int do_openat(void *cpu_env
, int dirfd
, const char *pathname
, int flags
, mode_t mode
)
7642 const char *filename
;
7643 int (*fill
)(void *cpu_env
, int fd
);
7644 int (*cmp
)(const char *s1
, const char *s2
);
7646 const struct fake_open
*fake_open
;
7647 static const struct fake_open fakes
[] = {
7648 { "maps", open_self_maps
, is_proc_myself
},
7649 { "stat", open_self_stat
, is_proc_myself
},
7650 { "auxv", open_self_auxv
, is_proc_myself
},
7651 { "cmdline", open_self_cmdline
, is_proc_myself
},
7652 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7653 { "/proc/net/route", open_net_route
, is_proc
},
7655 { NULL
, NULL
, NULL
}
7658 if (is_proc_myself(pathname
, "exe")) {
7659 int execfd
= qemu_getauxval(AT_EXECFD
);
7660 return execfd
? execfd
: safe_openat(dirfd
, exec_path
, flags
, mode
);
7663 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
7664 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
7669 if (fake_open
->filename
) {
7671 char filename
[PATH_MAX
];
7674 /* create temporary file to map stat to */
7675 tmpdir
= getenv("TMPDIR");
7678 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
7679 fd
= mkstemp(filename
);
7685 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
7691 lseek(fd
, 0, SEEK_SET
);
7696 return safe_openat(dirfd
, path(pathname
), flags
, mode
);
7699 #define TIMER_MAGIC 0x0caf0000
7700 #define TIMER_MAGIC_MASK 0xffff0000
7702 /* Convert QEMU provided timer ID back to internal 16bit index format */
7703 static target_timer_t
get_timer_id(abi_long arg
)
7705 target_timer_t timerid
= arg
;
7707 if ((timerid
& TIMER_MAGIC_MASK
) != TIMER_MAGIC
) {
7708 return -TARGET_EINVAL
;
7713 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
7714 return -TARGET_EINVAL
;
7720 static abi_long
swap_data_eventfd(void *buf
, size_t len
)
7722 uint64_t *counter
= buf
;
7725 if (len
< sizeof(uint64_t)) {
7729 for (i
= 0; i
< len
; i
+= sizeof(uint64_t)) {
7730 *counter
= tswap64(*counter
);
7737 static TargetFdTrans target_eventfd_trans
= {
7738 .host_to_target_data
= swap_data_eventfd
,
7739 .target_to_host_data
= swap_data_eventfd
,
7742 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
7743 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
7744 defined(__NR_inotify_init1))
7745 static abi_long
host_to_target_data_inotify(void *buf
, size_t len
)
7747 struct inotify_event
*ev
;
7751 for (i
= 0; i
< len
; i
+= sizeof(struct inotify_event
) + name_len
) {
7752 ev
= (struct inotify_event
*)((char *)buf
+ i
);
7755 ev
->wd
= tswap32(ev
->wd
);
7756 ev
->mask
= tswap32(ev
->mask
);
7757 ev
->cookie
= tswap32(ev
->cookie
);
7758 ev
->len
= tswap32(name_len
);
7764 static TargetFdTrans target_inotify_trans
= {
7765 .host_to_target_data
= host_to_target_data_inotify
,
7769 static int target_to_host_cpu_mask(unsigned long *host_mask
,
7771 abi_ulong target_addr
,
7774 unsigned target_bits
= sizeof(abi_ulong
) * 8;
7775 unsigned host_bits
= sizeof(*host_mask
) * 8;
7776 abi_ulong
*target_mask
;
7779 assert(host_size
>= target_size
);
7781 target_mask
= lock_user(VERIFY_READ
, target_addr
, target_size
, 1);
7783 return -TARGET_EFAULT
;
7785 memset(host_mask
, 0, host_size
);
7787 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
7788 unsigned bit
= i
* target_bits
;
7791 __get_user(val
, &target_mask
[i
]);
7792 for (j
= 0; j
< target_bits
; j
++, bit
++) {
7793 if (val
& (1UL << j
)) {
7794 host_mask
[bit
/ host_bits
] |= 1UL << (bit
% host_bits
);
7799 unlock_user(target_mask
, target_addr
, 0);
7803 static int host_to_target_cpu_mask(const unsigned long *host_mask
,
7805 abi_ulong target_addr
,
7808 unsigned target_bits
= sizeof(abi_ulong
) * 8;
7809 unsigned host_bits
= sizeof(*host_mask
) * 8;
7810 abi_ulong
*target_mask
;
7813 assert(host_size
>= target_size
);
7815 target_mask
= lock_user(VERIFY_WRITE
, target_addr
, target_size
, 0);
7817 return -TARGET_EFAULT
;
7820 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
7821 unsigned bit
= i
* target_bits
;
7824 for (j
= 0; j
< target_bits
; j
++, bit
++) {
7825 if (host_mask
[bit
/ host_bits
] & (1UL << (bit
% host_bits
))) {
7829 __put_user(val
, &target_mask
[i
]);
7832 unlock_user(target_mask
, target_addr
, target_size
);
7836 /* do_syscall() should always have a single exit point at the end so
7837 that actions, such as logging of syscall results, can be performed.
7838 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
7839 abi_long
do_syscall(void *cpu_env
, int num
, abi_long arg1
,
7840 abi_long arg2
, abi_long arg3
, abi_long arg4
,
7841 abi_long arg5
, abi_long arg6
, abi_long arg7
,
7844 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
7850 #if defined(DEBUG_ERESTARTSYS)
7851 /* Debug-only code for exercising the syscall-restart code paths
7852 * in the per-architecture cpu main loops: restart every syscall
7853 * the guest makes once before letting it through.
7860 return -TARGET_ERESTARTSYS
;
7866 gemu_log("syscall %d", num
);
7868 trace_guest_user_syscall(cpu
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
);
7870 print_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7873 case TARGET_NR_exit
:
7874 /* In old applications this may be used to implement _exit(2).
7875 However in threaded applictions it is used for thread termination,
7876 and _exit_group is used for application termination.
7877 Do thread termination if we have more then one thread. */
7879 if (block_signals()) {
7880 ret
= -TARGET_ERESTARTSYS
;
7886 if (CPU_NEXT(first_cpu
)) {
7889 /* Remove the CPU from the list. */
7890 QTAILQ_REMOVE(&cpus
, cpu
, node
);
7895 if (ts
->child_tidptr
) {
7896 put_user_u32(0, ts
->child_tidptr
);
7897 sys_futex(g2h(ts
->child_tidptr
), FUTEX_WAKE
, INT_MAX
,
7901 object_unref(OBJECT(cpu
));
7903 rcu_unregister_thread();
7911 gdb_exit(cpu_env
, arg1
);
7913 ret
= 0; /* avoid warning */
7915 case TARGET_NR_read
:
7919 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
7921 ret
= get_errno(safe_read(arg1
, p
, arg3
));
7923 fd_trans_host_to_target_data(arg1
)) {
7924 ret
= fd_trans_host_to_target_data(arg1
)(p
, ret
);
7926 unlock_user(p
, arg2
, ret
);
7929 case TARGET_NR_write
:
7930 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
7932 if (fd_trans_target_to_host_data(arg1
)) {
7933 void *copy
= g_malloc(arg3
);
7934 memcpy(copy
, p
, arg3
);
7935 ret
= fd_trans_target_to_host_data(arg1
)(copy
, arg3
);
7937 ret
= get_errno(safe_write(arg1
, copy
, ret
));
7941 ret
= get_errno(safe_write(arg1
, p
, arg3
));
7943 unlock_user(p
, arg2
, 0);
7945 #ifdef TARGET_NR_open
7946 case TARGET_NR_open
:
7947 if (!(p
= lock_user_string(arg1
)))
7949 ret
= get_errno(do_openat(cpu_env
, AT_FDCWD
, p
,
7950 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
7952 fd_trans_unregister(ret
);
7953 unlock_user(p
, arg1
, 0);
7956 case TARGET_NR_openat
:
7957 if (!(p
= lock_user_string(arg2
)))
7959 ret
= get_errno(do_openat(cpu_env
, arg1
, p
,
7960 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
7962 fd_trans_unregister(ret
);
7963 unlock_user(p
, arg2
, 0);
7965 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7966 case TARGET_NR_name_to_handle_at
:
7967 ret
= do_name_to_handle_at(arg1
, arg2
, arg3
, arg4
, arg5
);
7970 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7971 case TARGET_NR_open_by_handle_at
:
7972 ret
= do_open_by_handle_at(arg1
, arg2
, arg3
);
7973 fd_trans_unregister(ret
);
7976 case TARGET_NR_close
:
7977 fd_trans_unregister(arg1
);
7978 ret
= get_errno(close(arg1
));
7983 #ifdef TARGET_NR_fork
7984 case TARGET_NR_fork
:
7985 ret
= get_errno(do_fork(cpu_env
, TARGET_SIGCHLD
, 0, 0, 0, 0));
7988 #ifdef TARGET_NR_waitpid
7989 case TARGET_NR_waitpid
:
7992 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, 0));
7993 if (!is_error(ret
) && arg2
&& ret
7994 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
7999 #ifdef TARGET_NR_waitid
8000 case TARGET_NR_waitid
:
8004 ret
= get_errno(safe_waitid(arg1
, arg2
, &info
, arg4
, NULL
));
8005 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
8006 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
8008 host_to_target_siginfo(p
, &info
);
8009 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
8014 #ifdef TARGET_NR_creat /* not on alpha */
8015 case TARGET_NR_creat
:
8016 if (!(p
= lock_user_string(arg1
)))
8018 ret
= get_errno(creat(p
, arg2
));
8019 fd_trans_unregister(ret
);
8020 unlock_user(p
, arg1
, 0);
8023 #ifdef TARGET_NR_link
8024 case TARGET_NR_link
:
8027 p
= lock_user_string(arg1
);
8028 p2
= lock_user_string(arg2
);
8030 ret
= -TARGET_EFAULT
;
8032 ret
= get_errno(link(p
, p2
));
8033 unlock_user(p2
, arg2
, 0);
8034 unlock_user(p
, arg1
, 0);
8038 #if defined(TARGET_NR_linkat)
8039 case TARGET_NR_linkat
:
8044 p
= lock_user_string(arg2
);
8045 p2
= lock_user_string(arg4
);
8047 ret
= -TARGET_EFAULT
;
8049 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
8050 unlock_user(p
, arg2
, 0);
8051 unlock_user(p2
, arg4
, 0);
8055 #ifdef TARGET_NR_unlink
8056 case TARGET_NR_unlink
:
8057 if (!(p
= lock_user_string(arg1
)))
8059 ret
= get_errno(unlink(p
));
8060 unlock_user(p
, arg1
, 0);
8063 #if defined(TARGET_NR_unlinkat)
8064 case TARGET_NR_unlinkat
:
8065 if (!(p
= lock_user_string(arg2
)))
8067 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
8068 unlock_user(p
, arg2
, 0);
8071 case TARGET_NR_execve
:
8073 char **argp
, **envp
;
8076 abi_ulong guest_argp
;
8077 abi_ulong guest_envp
;
8084 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
8085 if (get_user_ual(addr
, gp
))
8093 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
8094 if (get_user_ual(addr
, gp
))
8101 argp
= g_new0(char *, argc
+ 1);
8102 envp
= g_new0(char *, envc
+ 1);
8104 for (gp
= guest_argp
, q
= argp
; gp
;
8105 gp
+= sizeof(abi_ulong
), q
++) {
8106 if (get_user_ual(addr
, gp
))
8110 if (!(*q
= lock_user_string(addr
)))
8112 total_size
+= strlen(*q
) + 1;
8116 for (gp
= guest_envp
, q
= envp
; gp
;
8117 gp
+= sizeof(abi_ulong
), q
++) {
8118 if (get_user_ual(addr
, gp
))
8122 if (!(*q
= lock_user_string(addr
)))
8124 total_size
+= strlen(*q
) + 1;
8128 if (!(p
= lock_user_string(arg1
)))
8130 /* Although execve() is not an interruptible syscall it is
8131 * a special case where we must use the safe_syscall wrapper:
8132 * if we allow a signal to happen before we make the host
8133 * syscall then we will 'lose' it, because at the point of
8134 * execve the process leaves QEMU's control. So we use the
8135 * safe syscall wrapper to ensure that we either take the
8136 * signal as a guest signal, or else it does not happen
8137 * before the execve completes and makes it the other
8138 * program's problem.
8140 ret
= get_errno(safe_execve(p
, argp
, envp
));
8141 unlock_user(p
, arg1
, 0);
8146 ret
= -TARGET_EFAULT
;
8149 for (gp
= guest_argp
, q
= argp
; *q
;
8150 gp
+= sizeof(abi_ulong
), q
++) {
8151 if (get_user_ual(addr
, gp
)
8154 unlock_user(*q
, addr
, 0);
8156 for (gp
= guest_envp
, q
= envp
; *q
;
8157 gp
+= sizeof(abi_ulong
), q
++) {
8158 if (get_user_ual(addr
, gp
)
8161 unlock_user(*q
, addr
, 0);
8168 case TARGET_NR_chdir
:
8169 if (!(p
= lock_user_string(arg1
)))
8171 ret
= get_errno(chdir(p
));
8172 unlock_user(p
, arg1
, 0);
8174 #ifdef TARGET_NR_time
8175 case TARGET_NR_time
:
8178 ret
= get_errno(time(&host_time
));
8181 && put_user_sal(host_time
, arg1
))
8186 #ifdef TARGET_NR_mknod
8187 case TARGET_NR_mknod
:
8188 if (!(p
= lock_user_string(arg1
)))
8190 ret
= get_errno(mknod(p
, arg2
, arg3
));
8191 unlock_user(p
, arg1
, 0);
8194 #if defined(TARGET_NR_mknodat)
8195 case TARGET_NR_mknodat
:
8196 if (!(p
= lock_user_string(arg2
)))
8198 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
8199 unlock_user(p
, arg2
, 0);
8202 #ifdef TARGET_NR_chmod
8203 case TARGET_NR_chmod
:
8204 if (!(p
= lock_user_string(arg1
)))
8206 ret
= get_errno(chmod(p
, arg2
));
8207 unlock_user(p
, arg1
, 0);
8210 #ifdef TARGET_NR_break
8211 case TARGET_NR_break
:
8214 #ifdef TARGET_NR_oldstat
8215 case TARGET_NR_oldstat
:
8218 case TARGET_NR_lseek
:
8219 ret
= get_errno(lseek(arg1
, arg2
, arg3
));
8221 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
8222 /* Alpha specific */
8223 case TARGET_NR_getxpid
:
8224 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = getppid();
8225 ret
= get_errno(getpid());
8228 #ifdef TARGET_NR_getpid
8229 case TARGET_NR_getpid
:
8230 ret
= get_errno(getpid());
8233 case TARGET_NR_mount
:
8235 /* need to look at the data field */
8239 p
= lock_user_string(arg1
);
8247 p2
= lock_user_string(arg2
);
8250 unlock_user(p
, arg1
, 0);
8256 p3
= lock_user_string(arg3
);
8259 unlock_user(p
, arg1
, 0);
8261 unlock_user(p2
, arg2
, 0);
8268 /* FIXME - arg5 should be locked, but it isn't clear how to
8269 * do that since it's not guaranteed to be a NULL-terminated
8273 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
);
8275 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(arg5
));
8277 ret
= get_errno(ret
);
8280 unlock_user(p
, arg1
, 0);
8282 unlock_user(p2
, arg2
, 0);
8284 unlock_user(p3
, arg3
, 0);
8288 #ifdef TARGET_NR_umount
8289 case TARGET_NR_umount
:
8290 if (!(p
= lock_user_string(arg1
)))
8292 ret
= get_errno(umount(p
));
8293 unlock_user(p
, arg1
, 0);
8296 #ifdef TARGET_NR_stime /* not on alpha */
8297 case TARGET_NR_stime
:
8300 if (get_user_sal(host_time
, arg1
))
8302 ret
= get_errno(stime(&host_time
));
8306 case TARGET_NR_ptrace
:
8308 #ifdef TARGET_NR_alarm /* not on alpha */
8309 case TARGET_NR_alarm
:
8313 #ifdef TARGET_NR_oldfstat
8314 case TARGET_NR_oldfstat
:
8317 #ifdef TARGET_NR_pause /* not on alpha */
8318 case TARGET_NR_pause
:
8319 if (!block_signals()) {
8320 sigsuspend(&((TaskState
*)cpu
->opaque
)->signal_mask
);
8322 ret
= -TARGET_EINTR
;
8325 #ifdef TARGET_NR_utime
8326 case TARGET_NR_utime
:
8328 struct utimbuf tbuf
, *host_tbuf
;
8329 struct target_utimbuf
*target_tbuf
;
8331 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
8333 tbuf
.actime
= tswapal(target_tbuf
->actime
);
8334 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
8335 unlock_user_struct(target_tbuf
, arg2
, 0);
8340 if (!(p
= lock_user_string(arg1
)))
8342 ret
= get_errno(utime(p
, host_tbuf
));
8343 unlock_user(p
, arg1
, 0);
8347 #ifdef TARGET_NR_utimes
8348 case TARGET_NR_utimes
:
8350 struct timeval
*tvp
, tv
[2];
8352 if (copy_from_user_timeval(&tv
[0], arg2
)
8353 || copy_from_user_timeval(&tv
[1],
8354 arg2
+ sizeof(struct target_timeval
)))
8360 if (!(p
= lock_user_string(arg1
)))
8362 ret
= get_errno(utimes(p
, tvp
));
8363 unlock_user(p
, arg1
, 0);
8367 #if defined(TARGET_NR_futimesat)
8368 case TARGET_NR_futimesat
:
8370 struct timeval
*tvp
, tv
[2];
8372 if (copy_from_user_timeval(&tv
[0], arg3
)
8373 || copy_from_user_timeval(&tv
[1],
8374 arg3
+ sizeof(struct target_timeval
)))
8380 if (!(p
= lock_user_string(arg2
)))
8382 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
8383 unlock_user(p
, arg2
, 0);
8387 #ifdef TARGET_NR_stty
8388 case TARGET_NR_stty
:
8391 #ifdef TARGET_NR_gtty
8392 case TARGET_NR_gtty
:
8395 #ifdef TARGET_NR_access
8396 case TARGET_NR_access
:
8397 if (!(p
= lock_user_string(arg1
)))
8399 ret
= get_errno(access(path(p
), arg2
));
8400 unlock_user(p
, arg1
, 0);
8403 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
8404 case TARGET_NR_faccessat
:
8405 if (!(p
= lock_user_string(arg2
)))
8407 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
8408 unlock_user(p
, arg2
, 0);
8411 #ifdef TARGET_NR_nice /* not on alpha */
8412 case TARGET_NR_nice
:
8413 ret
= get_errno(nice(arg1
));
8416 #ifdef TARGET_NR_ftime
8417 case TARGET_NR_ftime
:
8420 case TARGET_NR_sync
:
8424 #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
8425 case TARGET_NR_syncfs
:
8426 ret
= get_errno(syncfs(arg1
));
8429 case TARGET_NR_kill
:
8430 ret
= get_errno(safe_kill(arg1
, target_to_host_signal(arg2
)));
8432 #ifdef TARGET_NR_rename
8433 case TARGET_NR_rename
:
8436 p
= lock_user_string(arg1
);
8437 p2
= lock_user_string(arg2
);
8439 ret
= -TARGET_EFAULT
;
8441 ret
= get_errno(rename(p
, p2
));
8442 unlock_user(p2
, arg2
, 0);
8443 unlock_user(p
, arg1
, 0);
8447 #if defined(TARGET_NR_renameat)
8448 case TARGET_NR_renameat
:
8451 p
= lock_user_string(arg2
);
8452 p2
= lock_user_string(arg4
);
8454 ret
= -TARGET_EFAULT
;
8456 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
8457 unlock_user(p2
, arg4
, 0);
8458 unlock_user(p
, arg2
, 0);
8462 #if defined(TARGET_NR_renameat2)
8463 case TARGET_NR_renameat2
:
8466 p
= lock_user_string(arg2
);
8467 p2
= lock_user_string(arg4
);
8469 ret
= -TARGET_EFAULT
;
8471 ret
= get_errno(sys_renameat2(arg1
, p
, arg3
, p2
, arg5
));
8473 unlock_user(p2
, arg4
, 0);
8474 unlock_user(p
, arg2
, 0);
8478 #ifdef TARGET_NR_mkdir
8479 case TARGET_NR_mkdir
:
8480 if (!(p
= lock_user_string(arg1
)))
8482 ret
= get_errno(mkdir(p
, arg2
));
8483 unlock_user(p
, arg1
, 0);
8486 #if defined(TARGET_NR_mkdirat)
8487 case TARGET_NR_mkdirat
:
8488 if (!(p
= lock_user_string(arg2
)))
8490 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
8491 unlock_user(p
, arg2
, 0);
8494 #ifdef TARGET_NR_rmdir
8495 case TARGET_NR_rmdir
:
8496 if (!(p
= lock_user_string(arg1
)))
8498 ret
= get_errno(rmdir(p
));
8499 unlock_user(p
, arg1
, 0);
8503 ret
= get_errno(dup(arg1
));
8505 fd_trans_dup(arg1
, ret
);
8508 #ifdef TARGET_NR_pipe
8509 case TARGET_NR_pipe
:
8510 ret
= do_pipe(cpu_env
, arg1
, 0, 0);
8513 #ifdef TARGET_NR_pipe2
8514 case TARGET_NR_pipe2
:
8515 ret
= do_pipe(cpu_env
, arg1
,
8516 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
8519 case TARGET_NR_times
:
8521 struct target_tms
*tmsp
;
8523 ret
= get_errno(times(&tms
));
8525 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
8528 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
8529 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
8530 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
8531 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
8534 ret
= host_to_target_clock_t(ret
);
8537 #ifdef TARGET_NR_prof
8538 case TARGET_NR_prof
:
8541 #ifdef TARGET_NR_signal
8542 case TARGET_NR_signal
:
8545 case TARGET_NR_acct
:
8547 ret
= get_errno(acct(NULL
));
8549 if (!(p
= lock_user_string(arg1
)))
8551 ret
= get_errno(acct(path(p
)));
8552 unlock_user(p
, arg1
, 0);
8555 #ifdef TARGET_NR_umount2
8556 case TARGET_NR_umount2
:
8557 if (!(p
= lock_user_string(arg1
)))
8559 ret
= get_errno(umount2(p
, arg2
));
8560 unlock_user(p
, arg1
, 0);
8563 #ifdef TARGET_NR_lock
8564 case TARGET_NR_lock
:
8567 case TARGET_NR_ioctl
:
8568 ret
= do_ioctl(arg1
, arg2
, arg3
);
8570 #ifdef TARGET_NR_fcntl
8571 case TARGET_NR_fcntl
:
8572 ret
= do_fcntl(arg1
, arg2
, arg3
);
8575 #ifdef TARGET_NR_mpx
8579 case TARGET_NR_setpgid
:
8580 ret
= get_errno(setpgid(arg1
, arg2
));
8582 #ifdef TARGET_NR_ulimit
8583 case TARGET_NR_ulimit
:
8586 #ifdef TARGET_NR_oldolduname
8587 case TARGET_NR_oldolduname
:
8590 case TARGET_NR_umask
:
8591 ret
= get_errno(umask(arg1
));
8593 case TARGET_NR_chroot
:
8594 if (!(p
= lock_user_string(arg1
)))
8596 ret
= get_errno(chroot(p
));
8597 unlock_user(p
, arg1
, 0);
8599 #ifdef TARGET_NR_ustat
8600 case TARGET_NR_ustat
:
8603 #ifdef TARGET_NR_dup2
8604 case TARGET_NR_dup2
:
8605 ret
= get_errno(dup2(arg1
, arg2
));
8607 fd_trans_dup(arg1
, arg2
);
8611 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
8612 case TARGET_NR_dup3
:
8616 if ((arg3
& ~TARGET_O_CLOEXEC
) != 0) {
8619 host_flags
= target_to_host_bitmask(arg3
, fcntl_flags_tbl
);
8620 ret
= get_errno(dup3(arg1
, arg2
, host_flags
));
8622 fd_trans_dup(arg1
, arg2
);
8627 #ifdef TARGET_NR_getppid /* not on alpha */
8628 case TARGET_NR_getppid
:
8629 ret
= get_errno(getppid());
8632 #ifdef TARGET_NR_getpgrp
8633 case TARGET_NR_getpgrp
:
8634 ret
= get_errno(getpgrp());
8637 case TARGET_NR_setsid
:
8638 ret
= get_errno(setsid());
8640 #ifdef TARGET_NR_sigaction
8641 case TARGET_NR_sigaction
:
8643 #if defined(TARGET_ALPHA)
8644 struct target_sigaction act
, oact
, *pact
= 0;
8645 struct target_old_sigaction
*old_act
;
8647 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
8649 act
._sa_handler
= old_act
->_sa_handler
;
8650 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
8651 act
.sa_flags
= old_act
->sa_flags
;
8652 act
.sa_restorer
= 0;
8653 unlock_user_struct(old_act
, arg2
, 0);
8656 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8657 if (!is_error(ret
) && arg3
) {
8658 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
8660 old_act
->_sa_handler
= oact
._sa_handler
;
8661 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
8662 old_act
->sa_flags
= oact
.sa_flags
;
8663 unlock_user_struct(old_act
, arg3
, 1);
8665 #elif defined(TARGET_MIPS)
8666 struct target_sigaction act
, oact
, *pact
, *old_act
;
8669 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
8671 act
._sa_handler
= old_act
->_sa_handler
;
8672 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
8673 act
.sa_flags
= old_act
->sa_flags
;
8674 unlock_user_struct(old_act
, arg2
, 0);
8680 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8682 if (!is_error(ret
) && arg3
) {
8683 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
8685 old_act
->_sa_handler
= oact
._sa_handler
;
8686 old_act
->sa_flags
= oact
.sa_flags
;
8687 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
8688 old_act
->sa_mask
.sig
[1] = 0;
8689 old_act
->sa_mask
.sig
[2] = 0;
8690 old_act
->sa_mask
.sig
[3] = 0;
8691 unlock_user_struct(old_act
, arg3
, 1);
8694 struct target_old_sigaction
*old_act
;
8695 struct target_sigaction act
, oact
, *pact
;
8697 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
8699 act
._sa_handler
= old_act
->_sa_handler
;
8700 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
8701 act
.sa_flags
= old_act
->sa_flags
;
8702 act
.sa_restorer
= old_act
->sa_restorer
;
8703 #ifdef TARGET_ARCH_HAS_KA_RESTORER
8704 act
.ka_restorer
= 0;
8706 unlock_user_struct(old_act
, arg2
, 0);
8711 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8712 if (!is_error(ret
) && arg3
) {
8713 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
8715 old_act
->_sa_handler
= oact
._sa_handler
;
8716 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
8717 old_act
->sa_flags
= oact
.sa_flags
;
8718 old_act
->sa_restorer
= oact
.sa_restorer
;
8719 unlock_user_struct(old_act
, arg3
, 1);
8725 case TARGET_NR_rt_sigaction
:
8727 #if defined(TARGET_ALPHA)
8728 /* For Alpha and SPARC this is a 5 argument syscall, with
8729 * a 'restorer' parameter which must be copied into the
8730 * sa_restorer field of the sigaction struct.
8731 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
8732 * and arg5 is the sigsetsize.
8733 * Alpha also has a separate rt_sigaction struct that it uses
8734 * here; SPARC uses the usual sigaction struct.
8736 struct target_rt_sigaction
*rt_act
;
8737 struct target_sigaction act
, oact
, *pact
= 0;
8739 if (arg4
!= sizeof(target_sigset_t
)) {
8740 ret
= -TARGET_EINVAL
;
8744 if (!lock_user_struct(VERIFY_READ
, rt_act
, arg2
, 1))
8746 act
._sa_handler
= rt_act
->_sa_handler
;
8747 act
.sa_mask
= rt_act
->sa_mask
;
8748 act
.sa_flags
= rt_act
->sa_flags
;
8749 act
.sa_restorer
= arg5
;
8750 unlock_user_struct(rt_act
, arg2
, 0);
8753 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8754 if (!is_error(ret
) && arg3
) {
8755 if (!lock_user_struct(VERIFY_WRITE
, rt_act
, arg3
, 0))
8757 rt_act
->_sa_handler
= oact
._sa_handler
;
8758 rt_act
->sa_mask
= oact
.sa_mask
;
8759 rt_act
->sa_flags
= oact
.sa_flags
;
8760 unlock_user_struct(rt_act
, arg3
, 1);
8764 target_ulong restorer
= arg4
;
8765 target_ulong sigsetsize
= arg5
;
8767 target_ulong sigsetsize
= arg4
;
8769 struct target_sigaction
*act
;
8770 struct target_sigaction
*oact
;
8772 if (sigsetsize
!= sizeof(target_sigset_t
)) {
8773 ret
= -TARGET_EINVAL
;
8777 if (!lock_user_struct(VERIFY_READ
, act
, arg2
, 1)) {
8780 #ifdef TARGET_ARCH_HAS_KA_RESTORER
8781 act
->ka_restorer
= restorer
;
8787 if (!lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
8788 ret
= -TARGET_EFAULT
;
8789 goto rt_sigaction_fail
;
8793 ret
= get_errno(do_sigaction(arg1
, act
, oact
));
8796 unlock_user_struct(act
, arg2
, 0);
8798 unlock_user_struct(oact
, arg3
, 1);
8802 #ifdef TARGET_NR_sgetmask /* not on alpha */
8803 case TARGET_NR_sgetmask
:
8806 abi_ulong target_set
;
8807 ret
= do_sigprocmask(0, NULL
, &cur_set
);
8809 host_to_target_old_sigset(&target_set
, &cur_set
);
8815 #ifdef TARGET_NR_ssetmask /* not on alpha */
8816 case TARGET_NR_ssetmask
:
8819 abi_ulong target_set
= arg1
;
8820 target_to_host_old_sigset(&set
, &target_set
);
8821 ret
= do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
8823 host_to_target_old_sigset(&target_set
, &oset
);
8829 #ifdef TARGET_NR_sigprocmask
8830 case TARGET_NR_sigprocmask
:
8832 #if defined(TARGET_ALPHA)
8833 sigset_t set
, oldset
;
8838 case TARGET_SIG_BLOCK
:
8841 case TARGET_SIG_UNBLOCK
:
8844 case TARGET_SIG_SETMASK
:
8848 ret
= -TARGET_EINVAL
;
8852 target_to_host_old_sigset(&set
, &mask
);
8854 ret
= do_sigprocmask(how
, &set
, &oldset
);
8855 if (!is_error(ret
)) {
8856 host_to_target_old_sigset(&mask
, &oldset
);
8858 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0; /* force no error */
8861 sigset_t set
, oldset
, *set_ptr
;
8866 case TARGET_SIG_BLOCK
:
8869 case TARGET_SIG_UNBLOCK
:
8872 case TARGET_SIG_SETMASK
:
8876 ret
= -TARGET_EINVAL
;
8879 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
8881 target_to_host_old_sigset(&set
, p
);
8882 unlock_user(p
, arg2
, 0);
8888 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
8889 if (!is_error(ret
) && arg3
) {
8890 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
8892 host_to_target_old_sigset(p
, &oldset
);
8893 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
8899 case TARGET_NR_rt_sigprocmask
:
8902 sigset_t set
, oldset
, *set_ptr
;
8904 if (arg4
!= sizeof(target_sigset_t
)) {
8905 ret
= -TARGET_EINVAL
;
8911 case TARGET_SIG_BLOCK
:
8914 case TARGET_SIG_UNBLOCK
:
8917 case TARGET_SIG_SETMASK
:
8921 ret
= -TARGET_EINVAL
;
8924 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
8926 target_to_host_sigset(&set
, p
);
8927 unlock_user(p
, arg2
, 0);
8933 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
8934 if (!is_error(ret
) && arg3
) {
8935 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
8937 host_to_target_sigset(p
, &oldset
);
8938 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
8942 #ifdef TARGET_NR_sigpending
8943 case TARGET_NR_sigpending
:
8946 ret
= get_errno(sigpending(&set
));
8947 if (!is_error(ret
)) {
8948 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
8950 host_to_target_old_sigset(p
, &set
);
8951 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
8956 case TARGET_NR_rt_sigpending
:
8960 /* Yes, this check is >, not != like most. We follow the kernel's
8961 * logic and it does it like this because it implements
8962 * NR_sigpending through the same code path, and in that case
8963 * the old_sigset_t is smaller in size.
8965 if (arg2
> sizeof(target_sigset_t
)) {
8966 ret
= -TARGET_EINVAL
;
8970 ret
= get_errno(sigpending(&set
));
8971 if (!is_error(ret
)) {
8972 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
8974 host_to_target_sigset(p
, &set
);
8975 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
8979 #ifdef TARGET_NR_sigsuspend
8980 case TARGET_NR_sigsuspend
:
8982 TaskState
*ts
= cpu
->opaque
;
8983 #if defined(TARGET_ALPHA)
8984 abi_ulong mask
= arg1
;
8985 target_to_host_old_sigset(&ts
->sigsuspend_mask
, &mask
);
8987 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
8989 target_to_host_old_sigset(&ts
->sigsuspend_mask
, p
);
8990 unlock_user(p
, arg1
, 0);
8992 ret
= get_errno(safe_rt_sigsuspend(&ts
->sigsuspend_mask
,
8994 if (ret
!= -TARGET_ERESTARTSYS
) {
8995 ts
->in_sigsuspend
= 1;
9000 case TARGET_NR_rt_sigsuspend
:
9002 TaskState
*ts
= cpu
->opaque
;
9004 if (arg2
!= sizeof(target_sigset_t
)) {
9005 ret
= -TARGET_EINVAL
;
9008 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
9010 target_to_host_sigset(&ts
->sigsuspend_mask
, p
);
9011 unlock_user(p
, arg1
, 0);
9012 ret
= get_errno(safe_rt_sigsuspend(&ts
->sigsuspend_mask
,
9014 if (ret
!= -TARGET_ERESTARTSYS
) {
9015 ts
->in_sigsuspend
= 1;
9019 case TARGET_NR_rt_sigtimedwait
:
9022 struct timespec uts
, *puts
;
9025 if (arg4
!= sizeof(target_sigset_t
)) {
9026 ret
= -TARGET_EINVAL
;
9030 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
9032 target_to_host_sigset(&set
, p
);
9033 unlock_user(p
, arg1
, 0);
9036 target_to_host_timespec(puts
, arg3
);
9040 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
9042 if (!is_error(ret
)) {
9044 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
9049 host_to_target_siginfo(p
, &uinfo
);
9050 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
9052 ret
= host_to_target_signal(ret
);
9056 case TARGET_NR_rt_sigqueueinfo
:
9060 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
9064 target_to_host_siginfo(&uinfo
, p
);
9065 unlock_user(p
, arg3
, 0);
9066 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, arg2
, &uinfo
));
9069 case TARGET_NR_rt_tgsigqueueinfo
:
9073 p
= lock_user(VERIFY_READ
, arg4
, sizeof(target_siginfo_t
), 1);
9077 target_to_host_siginfo(&uinfo
, p
);
9078 unlock_user(p
, arg4
, 0);
9079 ret
= get_errno(sys_rt_tgsigqueueinfo(arg1
, arg2
, arg3
, &uinfo
));
9082 #ifdef TARGET_NR_sigreturn
9083 case TARGET_NR_sigreturn
:
9084 if (block_signals()) {
9085 ret
= -TARGET_ERESTARTSYS
;
9087 ret
= do_sigreturn(cpu_env
);
9091 case TARGET_NR_rt_sigreturn
:
9092 if (block_signals()) {
9093 ret
= -TARGET_ERESTARTSYS
;
9095 ret
= do_rt_sigreturn(cpu_env
);
9098 case TARGET_NR_sethostname
:
9099 if (!(p
= lock_user_string(arg1
)))
9101 ret
= get_errno(sethostname(p
, arg2
));
9102 unlock_user(p
, arg1
, 0);
9104 case TARGET_NR_setrlimit
:
9106 int resource
= target_to_host_resource(arg1
);
9107 struct target_rlimit
*target_rlim
;
9109 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
9111 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
9112 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
9113 unlock_user_struct(target_rlim
, arg2
, 0);
9114 ret
= get_errno(setrlimit(resource
, &rlim
));
9117 case TARGET_NR_getrlimit
:
9119 int resource
= target_to_host_resource(arg1
);
9120 struct target_rlimit
*target_rlim
;
9123 ret
= get_errno(getrlimit(resource
, &rlim
));
9124 if (!is_error(ret
)) {
9125 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
9127 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
9128 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
9129 unlock_user_struct(target_rlim
, arg2
, 1);
9133 case TARGET_NR_getrusage
:
9135 struct rusage rusage
;
9136 ret
= get_errno(getrusage(arg1
, &rusage
));
9137 if (!is_error(ret
)) {
9138 ret
= host_to_target_rusage(arg2
, &rusage
);
9142 case TARGET_NR_gettimeofday
:
9145 ret
= get_errno(gettimeofday(&tv
, NULL
));
9146 if (!is_error(ret
)) {
9147 if (copy_to_user_timeval(arg1
, &tv
))
9152 case TARGET_NR_settimeofday
:
9154 struct timeval tv
, *ptv
= NULL
;
9155 struct timezone tz
, *ptz
= NULL
;
9158 if (copy_from_user_timeval(&tv
, arg1
)) {
9165 if (copy_from_user_timezone(&tz
, arg2
)) {
9171 ret
= get_errno(settimeofday(ptv
, ptz
));
9174 #if defined(TARGET_NR_select)
9175 case TARGET_NR_select
:
9176 #if defined(TARGET_WANT_NI_OLD_SELECT)
9177 /* some architectures used to have old_select here
9178 * but now ENOSYS it.
9180 ret
= -TARGET_ENOSYS
;
9181 #elif defined(TARGET_WANT_OLD_SYS_SELECT)
9182 ret
= do_old_select(arg1
);
9184 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
9188 #ifdef TARGET_NR_pselect6
9189 case TARGET_NR_pselect6
:
9191 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
9192 fd_set rfds
, wfds
, efds
;
9193 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
9194 struct timespec ts
, *ts_ptr
;
9197 * The 6th arg is actually two args smashed together,
9198 * so we cannot use the C library.
9206 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
9207 target_sigset_t
*target_sigset
;
9215 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
9219 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
9223 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
9229 * This takes a timespec, and not a timeval, so we cannot
9230 * use the do_select() helper ...
9233 if (target_to_host_timespec(&ts
, ts_addr
)) {
9241 /* Extract the two packed args for the sigset */
9244 sig
.size
= SIGSET_T_SIZE
;
9246 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
9250 arg_sigset
= tswapal(arg7
[0]);
9251 arg_sigsize
= tswapal(arg7
[1]);
9252 unlock_user(arg7
, arg6
, 0);
9256 if (arg_sigsize
!= sizeof(*target_sigset
)) {
9257 /* Like the kernel, we enforce correct size sigsets */
9258 ret
= -TARGET_EINVAL
;
9261 target_sigset
= lock_user(VERIFY_READ
, arg_sigset
,
9262 sizeof(*target_sigset
), 1);
9263 if (!target_sigset
) {
9266 target_to_host_sigset(&set
, target_sigset
);
9267 unlock_user(target_sigset
, arg_sigset
, 0);
9275 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
9278 if (!is_error(ret
)) {
9279 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
9281 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
9283 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
9286 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
))
9292 #ifdef TARGET_NR_symlink
9293 case TARGET_NR_symlink
:
9296 p
= lock_user_string(arg1
);
9297 p2
= lock_user_string(arg2
);
9299 ret
= -TARGET_EFAULT
;
9301 ret
= get_errno(symlink(p
, p2
));
9302 unlock_user(p2
, arg2
, 0);
9303 unlock_user(p
, arg1
, 0);
9307 #if defined(TARGET_NR_symlinkat)
9308 case TARGET_NR_symlinkat
:
9311 p
= lock_user_string(arg1
);
9312 p2
= lock_user_string(arg3
);
9314 ret
= -TARGET_EFAULT
;
9316 ret
= get_errno(symlinkat(p
, arg2
, p2
));
9317 unlock_user(p2
, arg3
, 0);
9318 unlock_user(p
, arg1
, 0);
9322 #ifdef TARGET_NR_oldlstat
9323 case TARGET_NR_oldlstat
:
9326 #ifdef TARGET_NR_readlink
9327 case TARGET_NR_readlink
:
9330 p
= lock_user_string(arg1
);
9331 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
9333 ret
= -TARGET_EFAULT
;
9335 /* Short circuit this for the magic exe check. */
9336 ret
= -TARGET_EINVAL
;
9337 } else if (is_proc_myself((const char *)p
, "exe")) {
9338 char real
[PATH_MAX
], *temp
;
9339 temp
= realpath(exec_path
, real
);
9340 /* Return value is # of bytes that we wrote to the buffer. */
9342 ret
= get_errno(-1);
9344 /* Don't worry about sign mismatch as earlier mapping
9345 * logic would have thrown a bad address error. */
9346 ret
= MIN(strlen(real
), arg3
);
9347 /* We cannot NUL terminate the string. */
9348 memcpy(p2
, real
, ret
);
9351 ret
= get_errno(readlink(path(p
), p2
, arg3
));
9353 unlock_user(p2
, arg2
, ret
);
9354 unlock_user(p
, arg1
, 0);
9358 #if defined(TARGET_NR_readlinkat)
9359 case TARGET_NR_readlinkat
:
9362 p
= lock_user_string(arg2
);
9363 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
9365 ret
= -TARGET_EFAULT
;
9366 } else if (is_proc_myself((const char *)p
, "exe")) {
9367 char real
[PATH_MAX
], *temp
;
9368 temp
= realpath(exec_path
, real
);
9369 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
9370 snprintf((char *)p2
, arg4
, "%s", real
);
9372 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
9374 unlock_user(p2
, arg3
, ret
);
9375 unlock_user(p
, arg2
, 0);
9379 #ifdef TARGET_NR_uselib
9380 case TARGET_NR_uselib
:
9383 #ifdef TARGET_NR_swapon
9384 case TARGET_NR_swapon
:
9385 if (!(p
= lock_user_string(arg1
)))
9387 ret
= get_errno(swapon(p
, arg2
));
9388 unlock_user(p
, arg1
, 0);
9391 case TARGET_NR_reboot
:
9392 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
9393 /* arg4 must be ignored in all other cases */
9394 p
= lock_user_string(arg4
);
9398 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
9399 unlock_user(p
, arg4
, 0);
9401 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
9404 #ifdef TARGET_NR_readdir
9405 case TARGET_NR_readdir
:
9408 #ifdef TARGET_NR_mmap
9409 case TARGET_NR_mmap
:
9410 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
9411 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
9412 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
9413 || defined(TARGET_S390X)
9416 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
9417 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
9425 unlock_user(v
, arg1
, 0);
9426 ret
= get_errno(target_mmap(v1
, v2
, v3
,
9427 target_to_host_bitmask(v4
, mmap_flags_tbl
),
9431 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
9432 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
9438 #ifdef TARGET_NR_mmap2
9439 case TARGET_NR_mmap2
:
9441 #define MMAP_SHIFT 12
9443 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
9444 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
9446 arg6
<< MMAP_SHIFT
));
9449 case TARGET_NR_munmap
:
9450 ret
= get_errno(target_munmap(arg1
, arg2
));
9452 case TARGET_NR_mprotect
:
9454 TaskState
*ts
= cpu
->opaque
;
9455 /* Special hack to detect libc making the stack executable. */
9456 if ((arg3
& PROT_GROWSDOWN
)
9457 && arg1
>= ts
->info
->stack_limit
9458 && arg1
<= ts
->info
->start_stack
) {
9459 arg3
&= ~PROT_GROWSDOWN
;
9460 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
9461 arg1
= ts
->info
->stack_limit
;
9464 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
9466 #ifdef TARGET_NR_mremap
9467 case TARGET_NR_mremap
:
9468 ret
= get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
9471 /* ??? msync/mlock/munlock are broken for softmmu. */
9472 #ifdef TARGET_NR_msync
9473 case TARGET_NR_msync
:
9474 ret
= get_errno(msync(g2h(arg1
), arg2
, arg3
));
9477 #ifdef TARGET_NR_mlock
9478 case TARGET_NR_mlock
:
9479 ret
= get_errno(mlock(g2h(arg1
), arg2
));
9482 #ifdef TARGET_NR_munlock
9483 case TARGET_NR_munlock
:
9484 ret
= get_errno(munlock(g2h(arg1
), arg2
));
9487 #ifdef TARGET_NR_mlockall
9488 case TARGET_NR_mlockall
:
9489 ret
= get_errno(mlockall(target_to_host_mlockall_arg(arg1
)));
9492 #ifdef TARGET_NR_munlockall
9493 case TARGET_NR_munlockall
:
9494 ret
= get_errno(munlockall());
9497 case TARGET_NR_truncate
:
9498 if (!(p
= lock_user_string(arg1
)))
9500 ret
= get_errno(truncate(p
, arg2
));
9501 unlock_user(p
, arg1
, 0);
9503 case TARGET_NR_ftruncate
:
9504 ret
= get_errno(ftruncate(arg1
, arg2
));
9506 case TARGET_NR_fchmod
:
9507 ret
= get_errno(fchmod(arg1
, arg2
));
9509 #if defined(TARGET_NR_fchmodat)
9510 case TARGET_NR_fchmodat
:
9511 if (!(p
= lock_user_string(arg2
)))
9513 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
9514 unlock_user(p
, arg2
, 0);
9517 case TARGET_NR_getpriority
:
9518 /* Note that negative values are valid for getpriority, so we must
9519 differentiate based on errno settings. */
9521 ret
= getpriority(arg1
, arg2
);
9522 if (ret
== -1 && errno
!= 0) {
9523 ret
= -host_to_target_errno(errno
);
9527 /* Return value is the unbiased priority. Signal no error. */
9528 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0;
9530 /* Return value is a biased priority to avoid negative numbers. */
9534 case TARGET_NR_setpriority
:
9535 ret
= get_errno(setpriority(arg1
, arg2
, arg3
));
9537 #ifdef TARGET_NR_profil
9538 case TARGET_NR_profil
:
9541 case TARGET_NR_statfs
:
9542 if (!(p
= lock_user_string(arg1
)))
9544 ret
= get_errno(statfs(path(p
), &stfs
));
9545 unlock_user(p
, arg1
, 0);
9547 if (!is_error(ret
)) {
9548 struct target_statfs
*target_stfs
;
9550 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
9552 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
9553 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
9554 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
9555 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
9556 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
9557 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
9558 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
9559 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
9560 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
9561 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
9562 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
9563 #ifdef _STATFS_F_FLAGS
9564 __put_user(stfs
.f_flags
, &target_stfs
->f_flags
);
9566 __put_user(0, &target_stfs
->f_flags
);
9568 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
9569 unlock_user_struct(target_stfs
, arg2
, 1);
9572 case TARGET_NR_fstatfs
:
9573 ret
= get_errno(fstatfs(arg1
, &stfs
));
9574 goto convert_statfs
;
9575 #ifdef TARGET_NR_statfs64
9576 case TARGET_NR_statfs64
:
9577 if (!(p
= lock_user_string(arg1
)))
9579 ret
= get_errno(statfs(path(p
), &stfs
));
9580 unlock_user(p
, arg1
, 0);
9582 if (!is_error(ret
)) {
9583 struct target_statfs64
*target_stfs
;
9585 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
9587 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
9588 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
9589 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
9590 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
9591 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
9592 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
9593 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
9594 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
9595 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
9596 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
9597 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
9598 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
9599 unlock_user_struct(target_stfs
, arg3
, 1);
9602 case TARGET_NR_fstatfs64
:
9603 ret
= get_errno(fstatfs(arg1
, &stfs
));
9604 goto convert_statfs64
;
9606 #ifdef TARGET_NR_ioperm
9607 case TARGET_NR_ioperm
:
9610 #ifdef TARGET_NR_socketcall
9611 case TARGET_NR_socketcall
:
9612 ret
= do_socketcall(arg1
, arg2
);
9615 #ifdef TARGET_NR_accept
9616 case TARGET_NR_accept
:
9617 ret
= do_accept4(arg1
, arg2
, arg3
, 0);
9620 #ifdef TARGET_NR_accept4
9621 case TARGET_NR_accept4
:
9622 ret
= do_accept4(arg1
, arg2
, arg3
, arg4
);
9625 #ifdef TARGET_NR_bind
9626 case TARGET_NR_bind
:
9627 ret
= do_bind(arg1
, arg2
, arg3
);
9630 #ifdef TARGET_NR_connect
9631 case TARGET_NR_connect
:
9632 ret
= do_connect(arg1
, arg2
, arg3
);
9635 #ifdef TARGET_NR_getpeername
9636 case TARGET_NR_getpeername
:
9637 ret
= do_getpeername(arg1
, arg2
, arg3
);
9640 #ifdef TARGET_NR_getsockname
9641 case TARGET_NR_getsockname
:
9642 ret
= do_getsockname(arg1
, arg2
, arg3
);
9645 #ifdef TARGET_NR_getsockopt
9646 case TARGET_NR_getsockopt
:
9647 ret
= do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
9650 #ifdef TARGET_NR_listen
9651 case TARGET_NR_listen
:
9652 ret
= get_errno(listen(arg1
, arg2
));
9655 #ifdef TARGET_NR_recv
9656 case TARGET_NR_recv
:
9657 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
9660 #ifdef TARGET_NR_recvfrom
9661 case TARGET_NR_recvfrom
:
9662 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9665 #ifdef TARGET_NR_recvmsg
9666 case TARGET_NR_recvmsg
:
9667 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
9670 #ifdef TARGET_NR_send
9671 case TARGET_NR_send
:
9672 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
9675 #ifdef TARGET_NR_sendmsg
9676 case TARGET_NR_sendmsg
:
9677 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
9680 #ifdef TARGET_NR_sendmmsg
9681 case TARGET_NR_sendmmsg
:
9682 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
9684 case TARGET_NR_recvmmsg
:
9685 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
9688 #ifdef TARGET_NR_sendto
9689 case TARGET_NR_sendto
:
9690 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9693 #ifdef TARGET_NR_shutdown
9694 case TARGET_NR_shutdown
:
9695 ret
= get_errno(shutdown(arg1
, arg2
));
9698 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
9699 case TARGET_NR_getrandom
:
9700 p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
9704 ret
= get_errno(getrandom(p
, arg2
, arg3
));
9705 unlock_user(p
, arg1
, ret
);
9708 #ifdef TARGET_NR_socket
9709 case TARGET_NR_socket
:
9710 ret
= do_socket(arg1
, arg2
, arg3
);
9713 #ifdef TARGET_NR_socketpair
9714 case TARGET_NR_socketpair
:
9715 ret
= do_socketpair(arg1
, arg2
, arg3
, arg4
);
9718 #ifdef TARGET_NR_setsockopt
9719 case TARGET_NR_setsockopt
:
9720 ret
= do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
9723 #if defined(TARGET_NR_syslog)
9724 case TARGET_NR_syslog
:
9729 case TARGET_SYSLOG_ACTION_CLOSE
: /* Close log */
9730 case TARGET_SYSLOG_ACTION_OPEN
: /* Open log */
9731 case TARGET_SYSLOG_ACTION_CLEAR
: /* Clear ring buffer */
9732 case TARGET_SYSLOG_ACTION_CONSOLE_OFF
: /* Disable logging */
9733 case TARGET_SYSLOG_ACTION_CONSOLE_ON
: /* Enable logging */
9734 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL
: /* Set messages level */
9735 case TARGET_SYSLOG_ACTION_SIZE_UNREAD
: /* Number of chars */
9736 case TARGET_SYSLOG_ACTION_SIZE_BUFFER
: /* Size of the buffer */
9738 ret
= get_errno(sys_syslog((int)arg1
, NULL
, (int)arg3
));
9741 case TARGET_SYSLOG_ACTION_READ
: /* Read from log */
9742 case TARGET_SYSLOG_ACTION_READ_CLEAR
: /* Read/clear msgs */
9743 case TARGET_SYSLOG_ACTION_READ_ALL
: /* Read last messages */
9745 ret
= -TARGET_EINVAL
;
9753 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
9755 ret
= -TARGET_EFAULT
;
9758 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
9759 unlock_user(p
, arg2
, arg3
);
9769 case TARGET_NR_setitimer
:
9771 struct itimerval value
, ovalue
, *pvalue
;
9775 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
9776 || copy_from_user_timeval(&pvalue
->it_value
,
9777 arg2
+ sizeof(struct target_timeval
)))
9782 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
9783 if (!is_error(ret
) && arg3
) {
9784 if (copy_to_user_timeval(arg3
,
9785 &ovalue
.it_interval
)
9786 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
9792 case TARGET_NR_getitimer
:
9794 struct itimerval value
;
9796 ret
= get_errno(getitimer(arg1
, &value
));
9797 if (!is_error(ret
) && arg2
) {
9798 if (copy_to_user_timeval(arg2
,
9800 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
9806 #ifdef TARGET_NR_stat
9807 case TARGET_NR_stat
:
9808 if (!(p
= lock_user_string(arg1
)))
9810 ret
= get_errno(stat(path(p
), &st
));
9811 unlock_user(p
, arg1
, 0);
9814 #ifdef TARGET_NR_lstat
9815 case TARGET_NR_lstat
:
9816 if (!(p
= lock_user_string(arg1
)))
9818 ret
= get_errno(lstat(path(p
), &st
));
9819 unlock_user(p
, arg1
, 0);
9822 case TARGET_NR_fstat
:
9824 ret
= get_errno(fstat(arg1
, &st
));
9825 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
9828 if (!is_error(ret
)) {
9829 struct target_stat
*target_st
;
9831 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
9833 memset(target_st
, 0, sizeof(*target_st
));
9834 __put_user(st
.st_dev
, &target_st
->st_dev
);
9835 __put_user(st
.st_ino
, &target_st
->st_ino
);
9836 __put_user(st
.st_mode
, &target_st
->st_mode
);
9837 __put_user(st
.st_uid
, &target_st
->st_uid
);
9838 __put_user(st
.st_gid
, &target_st
->st_gid
);
9839 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
9840 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
9841 __put_user(st
.st_size
, &target_st
->st_size
);
9842 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
9843 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
9844 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
9845 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
9846 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
9847 unlock_user_struct(target_st
, arg2
, 1);
9851 #ifdef TARGET_NR_olduname
9852 case TARGET_NR_olduname
:
9855 #ifdef TARGET_NR_iopl
9856 case TARGET_NR_iopl
:
9859 case TARGET_NR_vhangup
:
9860 ret
= get_errno(vhangup());
9862 #ifdef TARGET_NR_idle
9863 case TARGET_NR_idle
:
9866 #ifdef TARGET_NR_syscall
9867 case TARGET_NR_syscall
:
9868 ret
= do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
9869 arg6
, arg7
, arg8
, 0);
9872 case TARGET_NR_wait4
:
9875 abi_long status_ptr
= arg2
;
9876 struct rusage rusage
, *rusage_ptr
;
9877 abi_ulong target_rusage
= arg4
;
9878 abi_long rusage_err
;
9880 rusage_ptr
= &rusage
;
9883 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, rusage_ptr
));
9884 if (!is_error(ret
)) {
9885 if (status_ptr
&& ret
) {
9886 status
= host_to_target_waitstatus(status
);
9887 if (put_user_s32(status
, status_ptr
))
9890 if (target_rusage
) {
9891 rusage_err
= host_to_target_rusage(target_rusage
, &rusage
);
9899 #ifdef TARGET_NR_swapoff
9900 case TARGET_NR_swapoff
:
9901 if (!(p
= lock_user_string(arg1
)))
9903 ret
= get_errno(swapoff(p
));
9904 unlock_user(p
, arg1
, 0);
9907 case TARGET_NR_sysinfo
:
9909 struct target_sysinfo
*target_value
;
9910 struct sysinfo value
;
9911 ret
= get_errno(sysinfo(&value
));
9912 if (!is_error(ret
) && arg1
)
9914 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
9916 __put_user(value
.uptime
, &target_value
->uptime
);
9917 __put_user(value
.loads
[0], &target_value
->loads
[0]);
9918 __put_user(value
.loads
[1], &target_value
->loads
[1]);
9919 __put_user(value
.loads
[2], &target_value
->loads
[2]);
9920 __put_user(value
.totalram
, &target_value
->totalram
);
9921 __put_user(value
.freeram
, &target_value
->freeram
);
9922 __put_user(value
.sharedram
, &target_value
->sharedram
);
9923 __put_user(value
.bufferram
, &target_value
->bufferram
);
9924 __put_user(value
.totalswap
, &target_value
->totalswap
);
9925 __put_user(value
.freeswap
, &target_value
->freeswap
);
9926 __put_user(value
.procs
, &target_value
->procs
);
9927 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
9928 __put_user(value
.freehigh
, &target_value
->freehigh
);
9929 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
9930 unlock_user_struct(target_value
, arg1
, 1);
9934 #ifdef TARGET_NR_ipc
9936 ret
= do_ipc(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9939 #ifdef TARGET_NR_semget
9940 case TARGET_NR_semget
:
9941 ret
= get_errno(semget(arg1
, arg2
, arg3
));
9944 #ifdef TARGET_NR_semop
9945 case TARGET_NR_semop
:
9946 ret
= do_semop(arg1
, arg2
, arg3
);
9949 #ifdef TARGET_NR_semctl
9950 case TARGET_NR_semctl
:
9951 ret
= do_semctl(arg1
, arg2
, arg3
, arg4
);
9954 #ifdef TARGET_NR_msgctl
9955 case TARGET_NR_msgctl
:
9956 ret
= do_msgctl(arg1
, arg2
, arg3
);
9959 #ifdef TARGET_NR_msgget
9960 case TARGET_NR_msgget
:
9961 ret
= get_errno(msgget(arg1
, arg2
));
9964 #ifdef TARGET_NR_msgrcv
9965 case TARGET_NR_msgrcv
:
9966 ret
= do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
9969 #ifdef TARGET_NR_msgsnd
9970 case TARGET_NR_msgsnd
:
9971 ret
= do_msgsnd(arg1
, arg2
, arg3
, arg4
);
9974 #ifdef TARGET_NR_shmget
9975 case TARGET_NR_shmget
:
9976 ret
= get_errno(shmget(arg1
, arg2
, arg3
));
9979 #ifdef TARGET_NR_shmctl
9980 case TARGET_NR_shmctl
:
9981 ret
= do_shmctl(arg1
, arg2
, arg3
);
9984 #ifdef TARGET_NR_shmat
9985 case TARGET_NR_shmat
:
9986 ret
= do_shmat(cpu_env
, arg1
, arg2
, arg3
);
9989 #ifdef TARGET_NR_shmdt
9990 case TARGET_NR_shmdt
:
9991 ret
= do_shmdt(arg1
);
9994 case TARGET_NR_fsync
:
9995 ret
= get_errno(fsync(arg1
));
9997 case TARGET_NR_clone
:
9998 /* Linux manages to have three different orderings for its
9999 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
10000 * match the kernel's CONFIG_CLONE_* settings.
10001 * Microblaze is further special in that it uses a sixth
10002 * implicit argument to clone for the TLS pointer.
10004 #if defined(TARGET_MICROBLAZE)
10005 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
10006 #elif defined(TARGET_CLONE_BACKWARDS)
10007 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
10008 #elif defined(TARGET_CLONE_BACKWARDS2)
10009 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
10011 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
10014 #ifdef __NR_exit_group
10015 /* new thread calls */
10016 case TARGET_NR_exit_group
:
10017 #ifdef TARGET_GPROF
10020 gdb_exit(cpu_env
, arg1
);
10021 ret
= get_errno(exit_group(arg1
));
10024 case TARGET_NR_setdomainname
:
10025 if (!(p
= lock_user_string(arg1
)))
10027 ret
= get_errno(setdomainname(p
, arg2
));
10028 unlock_user(p
, arg1
, 0);
10030 case TARGET_NR_uname
:
10031 /* no need to transcode because we use the linux syscall */
10033 struct new_utsname
* buf
;
10035 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
10037 ret
= get_errno(sys_uname(buf
));
10038 if (!is_error(ret
)) {
10039 /* Overwrite the native machine name with whatever is being
10041 strcpy (buf
->machine
, cpu_to_uname_machine(cpu_env
));
10042 /* Allow the user to override the reported release. */
10043 if (qemu_uname_release
&& *qemu_uname_release
) {
10044 g_strlcpy(buf
->release
, qemu_uname_release
,
10045 sizeof(buf
->release
));
10048 unlock_user_struct(buf
, arg1
, 1);
10052 case TARGET_NR_modify_ldt
:
10053 ret
= do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
10055 #if !defined(TARGET_X86_64)
10056 case TARGET_NR_vm86old
:
10057 goto unimplemented
;
10058 case TARGET_NR_vm86
:
10059 ret
= do_vm86(cpu_env
, arg1
, arg2
);
10063 case TARGET_NR_adjtimex
:
10065 struct timex host_buf
;
10067 if (target_to_host_timex(&host_buf
, arg1
) != 0) {
10070 ret
= get_errno(adjtimex(&host_buf
));
10071 if (!is_error(ret
)) {
10072 if (host_to_target_timex(arg1
, &host_buf
) != 0) {
10078 #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
10079 case TARGET_NR_clock_adjtime
:
10081 struct timex htx
, *phtx
= &htx
;
10083 if (target_to_host_timex(phtx
, arg2
) != 0) {
10086 ret
= get_errno(clock_adjtime(arg1
, phtx
));
10087 if (!is_error(ret
) && phtx
) {
10088 if (host_to_target_timex(arg2
, phtx
) != 0) {
10095 #ifdef TARGET_NR_create_module
10096 case TARGET_NR_create_module
:
10098 case TARGET_NR_init_module
:
10099 case TARGET_NR_delete_module
:
10100 #ifdef TARGET_NR_get_kernel_syms
10101 case TARGET_NR_get_kernel_syms
:
10103 goto unimplemented
;
10104 case TARGET_NR_quotactl
:
10105 goto unimplemented
;
10106 case TARGET_NR_getpgid
:
10107 ret
= get_errno(getpgid(arg1
));
10109 case TARGET_NR_fchdir
:
10110 ret
= get_errno(fchdir(arg1
));
10112 #ifdef TARGET_NR_bdflush /* not on x86_64 */
10113 case TARGET_NR_bdflush
:
10114 goto unimplemented
;
10116 #ifdef TARGET_NR_sysfs
10117 case TARGET_NR_sysfs
:
10118 goto unimplemented
;
10120 case TARGET_NR_personality
:
10121 ret
= get_errno(personality(arg1
));
10123 #ifdef TARGET_NR_afs_syscall
10124 case TARGET_NR_afs_syscall
:
10125 goto unimplemented
;
10127 #ifdef TARGET_NR__llseek /* Not on alpha */
10128 case TARGET_NR__llseek
:
10131 #if !defined(__NR_llseek)
10132 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | (abi_ulong
)arg3
, arg5
);
10134 ret
= get_errno(res
);
10139 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
10141 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
10147 #ifdef TARGET_NR_getdents
10148 case TARGET_NR_getdents
:
10149 #ifdef __NR_getdents
10150 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
10152 struct target_dirent
*target_dirp
;
10153 struct linux_dirent
*dirp
;
10154 abi_long count
= arg3
;
10156 dirp
= g_try_malloc(count
);
10158 ret
= -TARGET_ENOMEM
;
10162 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
10163 if (!is_error(ret
)) {
10164 struct linux_dirent
*de
;
10165 struct target_dirent
*tde
;
10167 int reclen
, treclen
;
10168 int count1
, tnamelen
;
10172 if (!(target_dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
10176 reclen
= de
->d_reclen
;
10177 tnamelen
= reclen
- offsetof(struct linux_dirent
, d_name
);
10178 assert(tnamelen
>= 0);
10179 treclen
= tnamelen
+ offsetof(struct target_dirent
, d_name
);
10180 assert(count1
+ treclen
<= count
);
10181 tde
->d_reclen
= tswap16(treclen
);
10182 tde
->d_ino
= tswapal(de
->d_ino
);
10183 tde
->d_off
= tswapal(de
->d_off
);
10184 memcpy(tde
->d_name
, de
->d_name
, tnamelen
);
10185 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
10187 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
10191 unlock_user(target_dirp
, arg2
, ret
);
10197 struct linux_dirent
*dirp
;
10198 abi_long count
= arg3
;
10200 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
10202 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
10203 if (!is_error(ret
)) {
10204 struct linux_dirent
*de
;
10209 reclen
= de
->d_reclen
;
10212 de
->d_reclen
= tswap16(reclen
);
10213 tswapls(&de
->d_ino
);
10214 tswapls(&de
->d_off
);
10215 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
10219 unlock_user(dirp
, arg2
, ret
);
10223 /* Implement getdents in terms of getdents64 */
10225 struct linux_dirent64
*dirp
;
10226 abi_long count
= arg3
;
10228 dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
10232 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
10233 if (!is_error(ret
)) {
10234 /* Convert the dirent64 structs to target dirent. We do this
10235 * in-place, since we can guarantee that a target_dirent is no
10236 * larger than a dirent64; however this means we have to be
10237 * careful to read everything before writing in the new format.
10239 struct linux_dirent64
*de
;
10240 struct target_dirent
*tde
;
10245 tde
= (struct target_dirent
*)dirp
;
10247 int namelen
, treclen
;
10248 int reclen
= de
->d_reclen
;
10249 uint64_t ino
= de
->d_ino
;
10250 int64_t off
= de
->d_off
;
10251 uint8_t type
= de
->d_type
;
10253 namelen
= strlen(de
->d_name
);
10254 treclen
= offsetof(struct target_dirent
, d_name
)
10256 treclen
= QEMU_ALIGN_UP(treclen
, sizeof(abi_long
));
10258 memmove(tde
->d_name
, de
->d_name
, namelen
+ 1);
10259 tde
->d_ino
= tswapal(ino
);
10260 tde
->d_off
= tswapal(off
);
10261 tde
->d_reclen
= tswap16(treclen
);
10262 /* The target_dirent type is in what was formerly a padding
10263 * byte at the end of the structure:
10265 *(((char *)tde
) + treclen
- 1) = type
;
10267 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
10268 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
10274 unlock_user(dirp
, arg2
, ret
);
10278 #endif /* TARGET_NR_getdents */
10279 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
10280 case TARGET_NR_getdents64
:
10282 struct linux_dirent64
*dirp
;
10283 abi_long count
= arg3
;
10284 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
10286 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
10287 if (!is_error(ret
)) {
10288 struct linux_dirent64
*de
;
10293 reclen
= de
->d_reclen
;
10296 de
->d_reclen
= tswap16(reclen
);
10297 tswap64s((uint64_t *)&de
->d_ino
);
10298 tswap64s((uint64_t *)&de
->d_off
);
10299 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
10303 unlock_user(dirp
, arg2
, ret
);
10306 #endif /* TARGET_NR_getdents64 */
10307 #if defined(TARGET_NR__newselect)
10308 case TARGET_NR__newselect
:
10309 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
10312 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
10313 # ifdef TARGET_NR_poll
10314 case TARGET_NR_poll
:
10316 # ifdef TARGET_NR_ppoll
10317 case TARGET_NR_ppoll
:
10320 struct target_pollfd
*target_pfd
;
10321 unsigned int nfds
= arg2
;
10322 struct pollfd
*pfd
;
10328 if (nfds
> (INT_MAX
/ sizeof(struct target_pollfd
))) {
10329 ret
= -TARGET_EINVAL
;
10333 target_pfd
= lock_user(VERIFY_WRITE
, arg1
,
10334 sizeof(struct target_pollfd
) * nfds
, 1);
10339 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
10340 for (i
= 0; i
< nfds
; i
++) {
10341 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
10342 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
10347 # ifdef TARGET_NR_ppoll
10348 case TARGET_NR_ppoll
:
10350 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
10351 target_sigset_t
*target_set
;
10352 sigset_t _set
, *set
= &_set
;
10355 if (target_to_host_timespec(timeout_ts
, arg3
)) {
10356 unlock_user(target_pfd
, arg1
, 0);
10364 if (arg5
!= sizeof(target_sigset_t
)) {
10365 unlock_user(target_pfd
, arg1
, 0);
10366 ret
= -TARGET_EINVAL
;
10370 target_set
= lock_user(VERIFY_READ
, arg4
, sizeof(target_sigset_t
), 1);
10372 unlock_user(target_pfd
, arg1
, 0);
10375 target_to_host_sigset(set
, target_set
);
10380 ret
= get_errno(safe_ppoll(pfd
, nfds
, timeout_ts
,
10381 set
, SIGSET_T_SIZE
));
10383 if (!is_error(ret
) && arg3
) {
10384 host_to_target_timespec(arg3
, timeout_ts
);
10387 unlock_user(target_set
, arg4
, 0);
10392 # ifdef TARGET_NR_poll
10393 case TARGET_NR_poll
:
10395 struct timespec ts
, *pts
;
10398 /* Convert ms to secs, ns */
10399 ts
.tv_sec
= arg3
/ 1000;
10400 ts
.tv_nsec
= (arg3
% 1000) * 1000000LL;
10403 /* -ve poll() timeout means "infinite" */
10406 ret
= get_errno(safe_ppoll(pfd
, nfds
, pts
, NULL
, 0));
10411 g_assert_not_reached();
10414 if (!is_error(ret
)) {
10415 for(i
= 0; i
< nfds
; i
++) {
10416 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
10419 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
10423 case TARGET_NR_flock
:
10424 /* NOTE: the flock constant seems to be the same for every
10426 ret
= get_errno(safe_flock(arg1
, arg2
));
10428 case TARGET_NR_readv
:
10430 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
10432 ret
= get_errno(safe_readv(arg1
, vec
, arg3
));
10433 unlock_iovec(vec
, arg2
, arg3
, 1);
10435 ret
= -host_to_target_errno(errno
);
10439 case TARGET_NR_writev
:
10441 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
10443 ret
= get_errno(safe_writev(arg1
, vec
, arg3
));
10444 unlock_iovec(vec
, arg2
, arg3
, 0);
10446 ret
= -host_to_target_errno(errno
);
10450 #if defined(TARGET_NR_preadv)
10451 case TARGET_NR_preadv
:
10453 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
10455 ret
= get_errno(safe_preadv(arg1
, vec
, arg3
, arg4
, arg5
));
10456 unlock_iovec(vec
, arg2
, arg3
, 1);
10458 ret
= -host_to_target_errno(errno
);
10463 #if defined(TARGET_NR_pwritev)
10464 case TARGET_NR_pwritev
:
10466 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
10468 ret
= get_errno(safe_pwritev(arg1
, vec
, arg3
, arg4
, arg5
));
10469 unlock_iovec(vec
, arg2
, arg3
, 0);
10471 ret
= -host_to_target_errno(errno
);
10476 case TARGET_NR_getsid
:
10477 ret
= get_errno(getsid(arg1
));
10479 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
10480 case TARGET_NR_fdatasync
:
10481 ret
= get_errno(fdatasync(arg1
));
10484 #ifdef TARGET_NR__sysctl
10485 case TARGET_NR__sysctl
:
10486 /* We don't implement this, but ENOTDIR is always a safe
10488 ret
= -TARGET_ENOTDIR
;
10491 case TARGET_NR_sched_getaffinity
:
10493 unsigned int mask_size
;
10494 unsigned long *mask
;
10497 * sched_getaffinity needs multiples of ulong, so need to take
10498 * care of mismatches between target ulong and host ulong sizes.
10500 if (arg2
& (sizeof(abi_ulong
) - 1)) {
10501 ret
= -TARGET_EINVAL
;
10504 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
10506 mask
= alloca(mask_size
);
10507 memset(mask
, 0, mask_size
);
10508 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
10510 if (!is_error(ret
)) {
10512 /* More data returned than the caller's buffer will fit.
10513 * This only happens if sizeof(abi_long) < sizeof(long)
10514 * and the caller passed us a buffer holding an odd number
10515 * of abi_longs. If the host kernel is actually using the
10516 * extra 4 bytes then fail EINVAL; otherwise we can just
10517 * ignore them and only copy the interesting part.
10519 int numcpus
= sysconf(_SC_NPROCESSORS_CONF
);
10520 if (numcpus
> arg2
* 8) {
10521 ret
= -TARGET_EINVAL
;
10527 if (host_to_target_cpu_mask(mask
, mask_size
, arg3
, ret
)) {
10533 case TARGET_NR_sched_setaffinity
:
10535 unsigned int mask_size
;
10536 unsigned long *mask
;
10539 * sched_setaffinity needs multiples of ulong, so need to take
10540 * care of mismatches between target ulong and host ulong sizes.
10542 if (arg2
& (sizeof(abi_ulong
) - 1)) {
10543 ret
= -TARGET_EINVAL
;
10546 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
10547 mask
= alloca(mask_size
);
10549 ret
= target_to_host_cpu_mask(mask
, mask_size
, arg3
, arg2
);
10554 ret
= get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
10557 case TARGET_NR_getcpu
:
10559 unsigned cpu
, node
;
10560 ret
= get_errno(sys_getcpu(arg1
? &cpu
: NULL
,
10561 arg2
? &node
: NULL
,
10563 if (is_error(ret
)) {
10566 if (arg1
&& put_user_u32(cpu
, arg1
)) {
10569 if (arg2
&& put_user_u32(node
, arg2
)) {
10574 case TARGET_NR_sched_setparam
:
10576 struct sched_param
*target_schp
;
10577 struct sched_param schp
;
10580 return -TARGET_EINVAL
;
10582 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1))
10584 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
10585 unlock_user_struct(target_schp
, arg2
, 0);
10586 ret
= get_errno(sched_setparam(arg1
, &schp
));
10589 case TARGET_NR_sched_getparam
:
10591 struct sched_param
*target_schp
;
10592 struct sched_param schp
;
10595 return -TARGET_EINVAL
;
10597 ret
= get_errno(sched_getparam(arg1
, &schp
));
10598 if (!is_error(ret
)) {
10599 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0))
10601 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
10602 unlock_user_struct(target_schp
, arg2
, 1);
10606 case TARGET_NR_sched_setscheduler
:
10608 struct sched_param
*target_schp
;
10609 struct sched_param schp
;
10611 return -TARGET_EINVAL
;
10613 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1))
10615 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
10616 unlock_user_struct(target_schp
, arg3
, 0);
10617 ret
= get_errno(sched_setscheduler(arg1
, arg2
, &schp
));
10620 case TARGET_NR_sched_getscheduler
:
10621 ret
= get_errno(sched_getscheduler(arg1
));
10623 case TARGET_NR_sched_yield
:
10624 ret
= get_errno(sched_yield());
10626 case TARGET_NR_sched_get_priority_max
:
10627 ret
= get_errno(sched_get_priority_max(arg1
));
10629 case TARGET_NR_sched_get_priority_min
:
10630 ret
= get_errno(sched_get_priority_min(arg1
));
10632 case TARGET_NR_sched_rr_get_interval
:
10634 struct timespec ts
;
10635 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
10636 if (!is_error(ret
)) {
10637 ret
= host_to_target_timespec(arg2
, &ts
);
10641 case TARGET_NR_nanosleep
:
10643 struct timespec req
, rem
;
10644 target_to_host_timespec(&req
, arg1
);
10645 ret
= get_errno(safe_nanosleep(&req
, &rem
));
10646 if (is_error(ret
) && arg2
) {
10647 host_to_target_timespec(arg2
, &rem
);
10651 #ifdef TARGET_NR_query_module
10652 case TARGET_NR_query_module
:
10653 goto unimplemented
;
10655 #ifdef TARGET_NR_nfsservctl
10656 case TARGET_NR_nfsservctl
:
10657 goto unimplemented
;
10659 case TARGET_NR_prctl
:
10661 case PR_GET_PDEATHSIG
:
10664 ret
= get_errno(prctl(arg1
, &deathsig
, arg3
, arg4
, arg5
));
10665 if (!is_error(ret
) && arg2
10666 && put_user_ual(deathsig
, arg2
)) {
10674 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
10678 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
10679 arg3
, arg4
, arg5
));
10680 unlock_user(name
, arg2
, 16);
10685 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
10689 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
10690 arg3
, arg4
, arg5
));
10691 unlock_user(name
, arg2
, 0);
10695 #ifdef TARGET_AARCH64
10696 case TARGET_PR_SVE_SET_VL
:
10697 /* We cannot support either PR_SVE_SET_VL_ONEXEC
10698 or PR_SVE_VL_INHERIT. Therefore, anything above
10699 ARM_MAX_VQ results in EINVAL. */
10700 ret
= -TARGET_EINVAL
;
10701 if (arm_feature(cpu_env
, ARM_FEATURE_SVE
)
10702 && arg2
>= 0 && arg2
<= ARM_MAX_VQ
* 16 && !(arg2
& 15)) {
10703 CPUARMState
*env
= cpu_env
;
10704 int old_vq
= (env
->vfp
.zcr_el
[1] & 0xf) + 1;
10705 int vq
= MAX(arg2
/ 16, 1);
10708 aarch64_sve_narrow_vq(env
, vq
);
10710 env
->vfp
.zcr_el
[1] = vq
- 1;
10714 case TARGET_PR_SVE_GET_VL
:
10715 ret
= -TARGET_EINVAL
;
10716 if (arm_feature(cpu_env
, ARM_FEATURE_SVE
)) {
10717 CPUARMState
*env
= cpu_env
;
10718 ret
= ((env
->vfp
.zcr_el
[1] & 0xf) + 1) * 16;
10721 #endif /* AARCH64 */
10722 case PR_GET_SECCOMP
:
10723 case PR_SET_SECCOMP
:
10724 /* Disable seccomp to prevent the target disabling syscalls we
10726 ret
= -TARGET_EINVAL
;
10729 /* Most prctl options have no pointer arguments */
10730 ret
= get_errno(prctl(arg1
, arg2
, arg3
, arg4
, arg5
));
10734 #ifdef TARGET_NR_arch_prctl
10735 case TARGET_NR_arch_prctl
:
10736 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
10737 ret
= do_arch_prctl(cpu_env
, arg1
, arg2
);
10740 goto unimplemented
;
10743 #ifdef TARGET_NR_pread64
10744 case TARGET_NR_pread64
:
10745 if (regpairs_aligned(cpu_env
, num
)) {
10749 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
10751 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
10752 unlock_user(p
, arg2
, ret
);
10754 case TARGET_NR_pwrite64
:
10755 if (regpairs_aligned(cpu_env
, num
)) {
10759 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
10761 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
10762 unlock_user(p
, arg2
, 0);
10765 case TARGET_NR_getcwd
:
10766 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
10768 ret
= get_errno(sys_getcwd1(p
, arg2
));
10769 unlock_user(p
, arg1
, ret
);
10771 case TARGET_NR_capget
:
10772 case TARGET_NR_capset
:
10774 struct target_user_cap_header
*target_header
;
10775 struct target_user_cap_data
*target_data
= NULL
;
10776 struct __user_cap_header_struct header
;
10777 struct __user_cap_data_struct data
[2];
10778 struct __user_cap_data_struct
*dataptr
= NULL
;
10779 int i
, target_datalen
;
10780 int data_items
= 1;
10782 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
10785 header
.version
= tswap32(target_header
->version
);
10786 header
.pid
= tswap32(target_header
->pid
);
10788 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
10789 /* Version 2 and up takes pointer to two user_data structs */
10793 target_datalen
= sizeof(*target_data
) * data_items
;
10796 if (num
== TARGET_NR_capget
) {
10797 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
10799 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
10801 if (!target_data
) {
10802 unlock_user_struct(target_header
, arg1
, 0);
10806 if (num
== TARGET_NR_capset
) {
10807 for (i
= 0; i
< data_items
; i
++) {
10808 data
[i
].effective
= tswap32(target_data
[i
].effective
);
10809 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
10810 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
10817 if (num
== TARGET_NR_capget
) {
10818 ret
= get_errno(capget(&header
, dataptr
));
10820 ret
= get_errno(capset(&header
, dataptr
));
10823 /* The kernel always updates version for both capget and capset */
10824 target_header
->version
= tswap32(header
.version
);
10825 unlock_user_struct(target_header
, arg1
, 1);
10828 if (num
== TARGET_NR_capget
) {
10829 for (i
= 0; i
< data_items
; i
++) {
10830 target_data
[i
].effective
= tswap32(data
[i
].effective
);
10831 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
10832 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
10834 unlock_user(target_data
, arg2
, target_datalen
);
10836 unlock_user(target_data
, arg2
, 0);
10841 case TARGET_NR_sigaltstack
:
10842 ret
= do_sigaltstack(arg1
, arg2
, get_sp_from_cpustate((CPUArchState
*)cpu_env
));
10845 #ifdef CONFIG_SENDFILE
10846 case TARGET_NR_sendfile
:
10848 off_t
*offp
= NULL
;
10851 ret
= get_user_sal(off
, arg3
);
10852 if (is_error(ret
)) {
10857 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
10858 if (!is_error(ret
) && arg3
) {
10859 abi_long ret2
= put_user_sal(off
, arg3
);
10860 if (is_error(ret2
)) {
10866 #ifdef TARGET_NR_sendfile64
10867 case TARGET_NR_sendfile64
:
10869 off_t
*offp
= NULL
;
10872 ret
= get_user_s64(off
, arg3
);
10873 if (is_error(ret
)) {
10878 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
10879 if (!is_error(ret
) && arg3
) {
10880 abi_long ret2
= put_user_s64(off
, arg3
);
10881 if (is_error(ret2
)) {
10889 case TARGET_NR_sendfile
:
10890 #ifdef TARGET_NR_sendfile64
10891 case TARGET_NR_sendfile64
:
10893 goto unimplemented
;
10896 #ifdef TARGET_NR_getpmsg
10897 case TARGET_NR_getpmsg
:
10898 goto unimplemented
;
10900 #ifdef TARGET_NR_putpmsg
10901 case TARGET_NR_putpmsg
:
10902 goto unimplemented
;
10904 #ifdef TARGET_NR_vfork
10905 case TARGET_NR_vfork
:
10906 ret
= get_errno(do_fork(cpu_env
,
10907 CLONE_VFORK
| CLONE_VM
| TARGET_SIGCHLD
,
10911 #ifdef TARGET_NR_ugetrlimit
10912 case TARGET_NR_ugetrlimit
:
10914 struct rlimit rlim
;
10915 int resource
= target_to_host_resource(arg1
);
10916 ret
= get_errno(getrlimit(resource
, &rlim
));
10917 if (!is_error(ret
)) {
10918 struct target_rlimit
*target_rlim
;
10919 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
10921 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
10922 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
10923 unlock_user_struct(target_rlim
, arg2
, 1);
10928 #ifdef TARGET_NR_truncate64
10929 case TARGET_NR_truncate64
:
10930 if (!(p
= lock_user_string(arg1
)))
10932 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
10933 unlock_user(p
, arg1
, 0);
10936 #ifdef TARGET_NR_ftruncate64
10937 case TARGET_NR_ftruncate64
:
10938 ret
= target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
10941 #ifdef TARGET_NR_stat64
10942 case TARGET_NR_stat64
:
10943 if (!(p
= lock_user_string(arg1
)))
10945 ret
= get_errno(stat(path(p
), &st
));
10946 unlock_user(p
, arg1
, 0);
10947 if (!is_error(ret
))
10948 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10951 #ifdef TARGET_NR_lstat64
10952 case TARGET_NR_lstat64
:
10953 if (!(p
= lock_user_string(arg1
)))
10955 ret
= get_errno(lstat(path(p
), &st
));
10956 unlock_user(p
, arg1
, 0);
10957 if (!is_error(ret
))
10958 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10961 #ifdef TARGET_NR_fstat64
10962 case TARGET_NR_fstat64
:
10963 ret
= get_errno(fstat(arg1
, &st
));
10964 if (!is_error(ret
))
10965 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10968 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
10969 #ifdef TARGET_NR_fstatat64
10970 case TARGET_NR_fstatat64
:
10972 #ifdef TARGET_NR_newfstatat
10973 case TARGET_NR_newfstatat
:
10975 if (!(p
= lock_user_string(arg2
)))
10977 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
10978 if (!is_error(ret
))
10979 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
10982 #ifdef TARGET_NR_lchown
10983 case TARGET_NR_lchown
:
10984 if (!(p
= lock_user_string(arg1
)))
10986 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
10987 unlock_user(p
, arg1
, 0);
10990 #ifdef TARGET_NR_getuid
10991 case TARGET_NR_getuid
:
10992 ret
= get_errno(high2lowuid(getuid()));
10995 #ifdef TARGET_NR_getgid
10996 case TARGET_NR_getgid
:
10997 ret
= get_errno(high2lowgid(getgid()));
11000 #ifdef TARGET_NR_geteuid
11001 case TARGET_NR_geteuid
:
11002 ret
= get_errno(high2lowuid(geteuid()));
11005 #ifdef TARGET_NR_getegid
11006 case TARGET_NR_getegid
:
11007 ret
= get_errno(high2lowgid(getegid()));
11010 case TARGET_NR_setreuid
:
11011 ret
= get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
11013 case TARGET_NR_setregid
:
11014 ret
= get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
11016 case TARGET_NR_getgroups
:
11018 int gidsetsize
= arg1
;
11019 target_id
*target_grouplist
;
11023 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11024 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
11025 if (gidsetsize
== 0)
11027 if (!is_error(ret
)) {
11028 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* sizeof(target_id
), 0);
11029 if (!target_grouplist
)
11031 for(i
= 0;i
< ret
; i
++)
11032 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
11033 unlock_user(target_grouplist
, arg2
, gidsetsize
* sizeof(target_id
));
11037 case TARGET_NR_setgroups
:
11039 int gidsetsize
= arg1
;
11040 target_id
*target_grouplist
;
11041 gid_t
*grouplist
= NULL
;
11044 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11045 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* sizeof(target_id
), 1);
11046 if (!target_grouplist
) {
11047 ret
= -TARGET_EFAULT
;
11050 for (i
= 0; i
< gidsetsize
; i
++) {
11051 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
11053 unlock_user(target_grouplist
, arg2
, 0);
11055 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
11058 case TARGET_NR_fchown
:
11059 ret
= get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
11061 #if defined(TARGET_NR_fchownat)
11062 case TARGET_NR_fchownat
:
11063 if (!(p
= lock_user_string(arg2
)))
11065 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
11066 low2highgid(arg4
), arg5
));
11067 unlock_user(p
, arg2
, 0);
11070 #ifdef TARGET_NR_setresuid
11071 case TARGET_NR_setresuid
:
11072 ret
= get_errno(sys_setresuid(low2highuid(arg1
),
11074 low2highuid(arg3
)));
11077 #ifdef TARGET_NR_getresuid
11078 case TARGET_NR_getresuid
:
11080 uid_t ruid
, euid
, suid
;
11081 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
11082 if (!is_error(ret
)) {
11083 if (put_user_id(high2lowuid(ruid
), arg1
)
11084 || put_user_id(high2lowuid(euid
), arg2
)
11085 || put_user_id(high2lowuid(suid
), arg3
))
11091 #ifdef TARGET_NR_getresgid
11092 case TARGET_NR_setresgid
:
11093 ret
= get_errno(sys_setresgid(low2highgid(arg1
),
11095 low2highgid(arg3
)));
11098 #ifdef TARGET_NR_getresgid
11099 case TARGET_NR_getresgid
:
11101 gid_t rgid
, egid
, sgid
;
11102 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
11103 if (!is_error(ret
)) {
11104 if (put_user_id(high2lowgid(rgid
), arg1
)
11105 || put_user_id(high2lowgid(egid
), arg2
)
11106 || put_user_id(high2lowgid(sgid
), arg3
))
11112 #ifdef TARGET_NR_chown
11113 case TARGET_NR_chown
:
11114 if (!(p
= lock_user_string(arg1
)))
11116 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
11117 unlock_user(p
, arg1
, 0);
11120 case TARGET_NR_setuid
:
11121 ret
= get_errno(sys_setuid(low2highuid(arg1
)));
11123 case TARGET_NR_setgid
:
11124 ret
= get_errno(sys_setgid(low2highgid(arg1
)));
11126 case TARGET_NR_setfsuid
:
11127 ret
= get_errno(setfsuid(arg1
));
11129 case TARGET_NR_setfsgid
:
11130 ret
= get_errno(setfsgid(arg1
));
11133 #ifdef TARGET_NR_lchown32
11134 case TARGET_NR_lchown32
:
11135 if (!(p
= lock_user_string(arg1
)))
11137 ret
= get_errno(lchown(p
, arg2
, arg3
));
11138 unlock_user(p
, arg1
, 0);
11141 #ifdef TARGET_NR_getuid32
11142 case TARGET_NR_getuid32
:
11143 ret
= get_errno(getuid());
11147 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11148 /* Alpha specific */
11149 case TARGET_NR_getxuid
:
11153 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=euid
;
11155 ret
= get_errno(getuid());
11158 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11159 /* Alpha specific */
11160 case TARGET_NR_getxgid
:
11164 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=egid
;
11166 ret
= get_errno(getgid());
11169 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11170 /* Alpha specific */
11171 case TARGET_NR_osf_getsysinfo
:
11172 ret
= -TARGET_EOPNOTSUPP
;
11174 case TARGET_GSI_IEEE_FP_CONTROL
:
11176 uint64_t swcr
, fpcr
= cpu_alpha_load_fpcr (cpu_env
);
11178 /* Copied from linux ieee_fpcr_to_swcr. */
11179 swcr
= (fpcr
>> 35) & SWCR_STATUS_MASK
;
11180 swcr
|= (fpcr
>> 36) & SWCR_MAP_DMZ
;
11181 swcr
|= (~fpcr
>> 48) & (SWCR_TRAP_ENABLE_INV
11182 | SWCR_TRAP_ENABLE_DZE
11183 | SWCR_TRAP_ENABLE_OVF
);
11184 swcr
|= (~fpcr
>> 57) & (SWCR_TRAP_ENABLE_UNF
11185 | SWCR_TRAP_ENABLE_INE
);
11186 swcr
|= (fpcr
>> 47) & SWCR_MAP_UMZ
;
11187 swcr
|= (~fpcr
>> 41) & SWCR_TRAP_ENABLE_DNO
;
11189 if (put_user_u64 (swcr
, arg2
))
11195 /* case GSI_IEEE_STATE_AT_SIGNAL:
11196 -- Not implemented in linux kernel.
11198 -- Retrieves current unaligned access state; not much used.
11199 case GSI_PROC_TYPE:
11200 -- Retrieves implver information; surely not used.
11201 case GSI_GET_HWRPB:
11202 -- Grabs a copy of the HWRPB; surely not used.
11207 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
11208 /* Alpha specific */
11209 case TARGET_NR_osf_setsysinfo
:
11210 ret
= -TARGET_EOPNOTSUPP
;
11212 case TARGET_SSI_IEEE_FP_CONTROL
:
11214 uint64_t swcr
, fpcr
, orig_fpcr
;
11216 if (get_user_u64 (swcr
, arg2
)) {
11219 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11220 fpcr
= orig_fpcr
& FPCR_DYN_MASK
;
11222 /* Copied from linux ieee_swcr_to_fpcr. */
11223 fpcr
|= (swcr
& SWCR_STATUS_MASK
) << 35;
11224 fpcr
|= (swcr
& SWCR_MAP_DMZ
) << 36;
11225 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_INV
11226 | SWCR_TRAP_ENABLE_DZE
11227 | SWCR_TRAP_ENABLE_OVF
)) << 48;
11228 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_UNF
11229 | SWCR_TRAP_ENABLE_INE
)) << 57;
11230 fpcr
|= (swcr
& SWCR_MAP_UMZ
? FPCR_UNDZ
| FPCR_UNFD
: 0);
11231 fpcr
|= (~swcr
& SWCR_TRAP_ENABLE_DNO
) << 41;
11233 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11238 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
11240 uint64_t exc
, fpcr
, orig_fpcr
;
11243 if (get_user_u64(exc
, arg2
)) {
11247 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11249 /* We only add to the exception status here. */
11250 fpcr
= orig_fpcr
| ((exc
& SWCR_STATUS_MASK
) << 35);
11252 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11255 /* Old exceptions are not signaled. */
11256 fpcr
&= ~(orig_fpcr
& FPCR_STATUS_MASK
);
11258 /* If any exceptions set by this call,
11259 and are unmasked, send a signal. */
11261 if ((fpcr
& (FPCR_INE
| FPCR_INED
)) == FPCR_INE
) {
11262 si_code
= TARGET_FPE_FLTRES
;
11264 if ((fpcr
& (FPCR_UNF
| FPCR_UNFD
)) == FPCR_UNF
) {
11265 si_code
= TARGET_FPE_FLTUND
;
11267 if ((fpcr
& (FPCR_OVF
| FPCR_OVFD
)) == FPCR_OVF
) {
11268 si_code
= TARGET_FPE_FLTOVF
;
11270 if ((fpcr
& (FPCR_DZE
| FPCR_DZED
)) == FPCR_DZE
) {
11271 si_code
= TARGET_FPE_FLTDIV
;
11273 if ((fpcr
& (FPCR_INV
| FPCR_INVD
)) == FPCR_INV
) {
11274 si_code
= TARGET_FPE_FLTINV
;
11276 if (si_code
!= 0) {
11277 target_siginfo_t info
;
11278 info
.si_signo
= SIGFPE
;
11280 info
.si_code
= si_code
;
11281 info
._sifields
._sigfault
._addr
11282 = ((CPUArchState
*)cpu_env
)->pc
;
11283 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
,
11284 QEMU_SI_FAULT
, &info
);
11289 /* case SSI_NVPAIRS:
11290 -- Used with SSIN_UACPROC to enable unaligned accesses.
11291 case SSI_IEEE_STATE_AT_SIGNAL:
11292 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
11293 -- Not implemented in linux kernel
11298 #ifdef TARGET_NR_osf_sigprocmask
11299 /* Alpha specific. */
11300 case TARGET_NR_osf_sigprocmask
:
11304 sigset_t set
, oldset
;
11307 case TARGET_SIG_BLOCK
:
11310 case TARGET_SIG_UNBLOCK
:
11313 case TARGET_SIG_SETMASK
:
11317 ret
= -TARGET_EINVAL
;
11321 target_to_host_old_sigset(&set
, &mask
);
11322 ret
= do_sigprocmask(how
, &set
, &oldset
);
11324 host_to_target_old_sigset(&mask
, &oldset
);
11331 #ifdef TARGET_NR_getgid32
11332 case TARGET_NR_getgid32
:
11333 ret
= get_errno(getgid());
11336 #ifdef TARGET_NR_geteuid32
11337 case TARGET_NR_geteuid32
:
11338 ret
= get_errno(geteuid());
11341 #ifdef TARGET_NR_getegid32
11342 case TARGET_NR_getegid32
:
11343 ret
= get_errno(getegid());
11346 #ifdef TARGET_NR_setreuid32
11347 case TARGET_NR_setreuid32
:
11348 ret
= get_errno(setreuid(arg1
, arg2
));
11351 #ifdef TARGET_NR_setregid32
11352 case TARGET_NR_setregid32
:
11353 ret
= get_errno(setregid(arg1
, arg2
));
11356 #ifdef TARGET_NR_getgroups32
11357 case TARGET_NR_getgroups32
:
11359 int gidsetsize
= arg1
;
11360 uint32_t *target_grouplist
;
11364 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11365 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
11366 if (gidsetsize
== 0)
11368 if (!is_error(ret
)) {
11369 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
11370 if (!target_grouplist
) {
11371 ret
= -TARGET_EFAULT
;
11374 for(i
= 0;i
< ret
; i
++)
11375 target_grouplist
[i
] = tswap32(grouplist
[i
]);
11376 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
11381 #ifdef TARGET_NR_setgroups32
11382 case TARGET_NR_setgroups32
:
11384 int gidsetsize
= arg1
;
11385 uint32_t *target_grouplist
;
11389 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11390 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
11391 if (!target_grouplist
) {
11392 ret
= -TARGET_EFAULT
;
11395 for(i
= 0;i
< gidsetsize
; i
++)
11396 grouplist
[i
] = tswap32(target_grouplist
[i
]);
11397 unlock_user(target_grouplist
, arg2
, 0);
11398 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
11402 #ifdef TARGET_NR_fchown32
11403 case TARGET_NR_fchown32
:
11404 ret
= get_errno(fchown(arg1
, arg2
, arg3
));
11407 #ifdef TARGET_NR_setresuid32
11408 case TARGET_NR_setresuid32
:
11409 ret
= get_errno(sys_setresuid(arg1
, arg2
, arg3
));
11412 #ifdef TARGET_NR_getresuid32
11413 case TARGET_NR_getresuid32
:
11415 uid_t ruid
, euid
, suid
;
11416 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
11417 if (!is_error(ret
)) {
11418 if (put_user_u32(ruid
, arg1
)
11419 || put_user_u32(euid
, arg2
)
11420 || put_user_u32(suid
, arg3
))
11426 #ifdef TARGET_NR_setresgid32
11427 case TARGET_NR_setresgid32
:
11428 ret
= get_errno(sys_setresgid(arg1
, arg2
, arg3
));
11431 #ifdef TARGET_NR_getresgid32
11432 case TARGET_NR_getresgid32
:
11434 gid_t rgid
, egid
, sgid
;
11435 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
11436 if (!is_error(ret
)) {
11437 if (put_user_u32(rgid
, arg1
)
11438 || put_user_u32(egid
, arg2
)
11439 || put_user_u32(sgid
, arg3
))
11445 #ifdef TARGET_NR_chown32
11446 case TARGET_NR_chown32
:
11447 if (!(p
= lock_user_string(arg1
)))
11449 ret
= get_errno(chown(p
, arg2
, arg3
));
11450 unlock_user(p
, arg1
, 0);
11453 #ifdef TARGET_NR_setuid32
11454 case TARGET_NR_setuid32
:
11455 ret
= get_errno(sys_setuid(arg1
));
11458 #ifdef TARGET_NR_setgid32
11459 case TARGET_NR_setgid32
:
11460 ret
= get_errno(sys_setgid(arg1
));
11463 #ifdef TARGET_NR_setfsuid32
11464 case TARGET_NR_setfsuid32
:
11465 ret
= get_errno(setfsuid(arg1
));
11468 #ifdef TARGET_NR_setfsgid32
11469 case TARGET_NR_setfsgid32
:
11470 ret
= get_errno(setfsgid(arg1
));
11474 case TARGET_NR_pivot_root
:
11475 goto unimplemented
;
11476 #ifdef TARGET_NR_mincore
11477 case TARGET_NR_mincore
:
11480 ret
= -TARGET_ENOMEM
;
11481 a
= lock_user(VERIFY_READ
, arg1
, arg2
, 0);
11485 ret
= -TARGET_EFAULT
;
11486 p
= lock_user_string(arg3
);
11490 ret
= get_errno(mincore(a
, arg2
, p
));
11491 unlock_user(p
, arg3
, ret
);
11493 unlock_user(a
, arg1
, 0);
11497 #ifdef TARGET_NR_arm_fadvise64_64
11498 case TARGET_NR_arm_fadvise64_64
:
11499 /* arm_fadvise64_64 looks like fadvise64_64 but
11500 * with different argument order: fd, advice, offset, len
11501 * rather than the usual fd, offset, len, advice.
11502 * Note that offset and len are both 64-bit so appear as
11503 * pairs of 32-bit registers.
11505 ret
= posix_fadvise(arg1
, target_offset64(arg3
, arg4
),
11506 target_offset64(arg5
, arg6
), arg2
);
11507 ret
= -host_to_target_errno(ret
);
11511 #if TARGET_ABI_BITS == 32
11513 #ifdef TARGET_NR_fadvise64_64
11514 case TARGET_NR_fadvise64_64
:
11515 #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
11516 /* 6 args: fd, advice, offset (high, low), len (high, low) */
11524 /* 6 args: fd, offset (high, low), len (high, low), advice */
11525 if (regpairs_aligned(cpu_env
, num
)) {
11526 /* offset is in (3,4), len in (5,6) and advice in 7 */
11534 ret
= -host_to_target_errno(posix_fadvise(arg1
,
11535 target_offset64(arg2
, arg3
),
11536 target_offset64(arg4
, arg5
),
11541 #ifdef TARGET_NR_fadvise64
11542 case TARGET_NR_fadvise64
:
11543 /* 5 args: fd, offset (high, low), len, advice */
11544 if (regpairs_aligned(cpu_env
, num
)) {
11545 /* offset is in (3,4), len in 5 and advice in 6 */
11551 ret
= -host_to_target_errno(posix_fadvise(arg1
,
11552 target_offset64(arg2
, arg3
),
11557 #else /* not a 32-bit ABI */
11558 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
11559 #ifdef TARGET_NR_fadvise64_64
11560 case TARGET_NR_fadvise64_64
:
11562 #ifdef TARGET_NR_fadvise64
11563 case TARGET_NR_fadvise64
:
11565 #ifdef TARGET_S390X
11567 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
11568 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
11569 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
11570 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
11574 ret
= -host_to_target_errno(posix_fadvise(arg1
, arg2
, arg3
, arg4
));
11577 #endif /* end of 64-bit ABI fadvise handling */
11579 #ifdef TARGET_NR_madvise
11580 case TARGET_NR_madvise
:
11581 /* A straight passthrough may not be safe because qemu sometimes
11582 turns private file-backed mappings into anonymous mappings.
11583 This will break MADV_DONTNEED.
11584 This is a hint, so ignoring and returning success is ok. */
11585 ret
= get_errno(0);
11588 #if TARGET_ABI_BITS == 32
11589 case TARGET_NR_fcntl64
:
11593 from_flock64_fn
*copyfrom
= copy_from_user_flock64
;
11594 to_flock64_fn
*copyto
= copy_to_user_flock64
;
11597 if (((CPUARMState
*)cpu_env
)->eabi
) {
11598 copyfrom
= copy_from_user_eabi_flock64
;
11599 copyto
= copy_to_user_eabi_flock64
;
11603 cmd
= target_to_host_fcntl_cmd(arg2
);
11604 if (cmd
== -TARGET_EINVAL
) {
11610 case TARGET_F_GETLK64
:
11611 ret
= copyfrom(&fl
, arg3
);
11615 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
11617 ret
= copyto(arg3
, &fl
);
11621 case TARGET_F_SETLK64
:
11622 case TARGET_F_SETLKW64
:
11623 ret
= copyfrom(&fl
, arg3
);
11627 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
11630 ret
= do_fcntl(arg1
, arg2
, arg3
);
11636 #ifdef TARGET_NR_cacheflush
11637 case TARGET_NR_cacheflush
:
11638 /* self-modifying code is handled automatically, so nothing needed */
11642 #ifdef TARGET_NR_security
11643 case TARGET_NR_security
:
11644 goto unimplemented
;
11646 #ifdef TARGET_NR_getpagesize
11647 case TARGET_NR_getpagesize
:
11648 ret
= TARGET_PAGE_SIZE
;
11651 case TARGET_NR_gettid
:
11652 ret
= get_errno(gettid());
11654 #ifdef TARGET_NR_readahead
11655 case TARGET_NR_readahead
:
11656 #if TARGET_ABI_BITS == 32
11657 if (regpairs_aligned(cpu_env
, num
)) {
11662 ret
= get_errno(readahead(arg1
, target_offset64(arg2
, arg3
) , arg4
));
11664 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
11669 #ifdef TARGET_NR_setxattr
11670 case TARGET_NR_listxattr
:
11671 case TARGET_NR_llistxattr
:
11675 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11677 ret
= -TARGET_EFAULT
;
11681 p
= lock_user_string(arg1
);
11683 if (num
== TARGET_NR_listxattr
) {
11684 ret
= get_errno(listxattr(p
, b
, arg3
));
11686 ret
= get_errno(llistxattr(p
, b
, arg3
));
11689 ret
= -TARGET_EFAULT
;
11691 unlock_user(p
, arg1
, 0);
11692 unlock_user(b
, arg2
, arg3
);
11695 case TARGET_NR_flistxattr
:
11699 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11701 ret
= -TARGET_EFAULT
;
11705 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
11706 unlock_user(b
, arg2
, arg3
);
11709 case TARGET_NR_setxattr
:
11710 case TARGET_NR_lsetxattr
:
11712 void *p
, *n
, *v
= 0;
11714 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
11716 ret
= -TARGET_EFAULT
;
11720 p
= lock_user_string(arg1
);
11721 n
= lock_user_string(arg2
);
11723 if (num
== TARGET_NR_setxattr
) {
11724 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
11726 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
11729 ret
= -TARGET_EFAULT
;
11731 unlock_user(p
, arg1
, 0);
11732 unlock_user(n
, arg2
, 0);
11733 unlock_user(v
, arg3
, 0);
11736 case TARGET_NR_fsetxattr
:
11740 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
11742 ret
= -TARGET_EFAULT
;
11746 n
= lock_user_string(arg2
);
11748 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
11750 ret
= -TARGET_EFAULT
;
11752 unlock_user(n
, arg2
, 0);
11753 unlock_user(v
, arg3
, 0);
11756 case TARGET_NR_getxattr
:
11757 case TARGET_NR_lgetxattr
:
11759 void *p
, *n
, *v
= 0;
11761 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
11763 ret
= -TARGET_EFAULT
;
11767 p
= lock_user_string(arg1
);
11768 n
= lock_user_string(arg2
);
11770 if (num
== TARGET_NR_getxattr
) {
11771 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
11773 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
11776 ret
= -TARGET_EFAULT
;
11778 unlock_user(p
, arg1
, 0);
11779 unlock_user(n
, arg2
, 0);
11780 unlock_user(v
, arg3
, arg4
);
11783 case TARGET_NR_fgetxattr
:
11787 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
11789 ret
= -TARGET_EFAULT
;
11793 n
= lock_user_string(arg2
);
11795 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
11797 ret
= -TARGET_EFAULT
;
11799 unlock_user(n
, arg2
, 0);
11800 unlock_user(v
, arg3
, arg4
);
11803 case TARGET_NR_removexattr
:
11804 case TARGET_NR_lremovexattr
:
11807 p
= lock_user_string(arg1
);
11808 n
= lock_user_string(arg2
);
11810 if (num
== TARGET_NR_removexattr
) {
11811 ret
= get_errno(removexattr(p
, n
));
11813 ret
= get_errno(lremovexattr(p
, n
));
11816 ret
= -TARGET_EFAULT
;
11818 unlock_user(p
, arg1
, 0);
11819 unlock_user(n
, arg2
, 0);
11822 case TARGET_NR_fremovexattr
:
11825 n
= lock_user_string(arg2
);
11827 ret
= get_errno(fremovexattr(arg1
, n
));
11829 ret
= -TARGET_EFAULT
;
11831 unlock_user(n
, arg2
, 0);
11835 #endif /* CONFIG_ATTR */
11836 #ifdef TARGET_NR_set_thread_area
11837 case TARGET_NR_set_thread_area
:
11838 #if defined(TARGET_MIPS)
11839 ((CPUMIPSState
*) cpu_env
)->active_tc
.CP0_UserLocal
= arg1
;
11842 #elif defined(TARGET_CRIS)
11844 ret
= -TARGET_EINVAL
;
11846 ((CPUCRISState
*) cpu_env
)->pregs
[PR_PID
] = arg1
;
11850 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
11851 ret
= do_set_thread_area(cpu_env
, arg1
);
11853 #elif defined(TARGET_M68K)
11855 TaskState
*ts
= cpu
->opaque
;
11856 ts
->tp_value
= arg1
;
11861 goto unimplemented_nowarn
;
11864 #ifdef TARGET_NR_get_thread_area
11865 case TARGET_NR_get_thread_area
:
11866 #if defined(TARGET_I386) && defined(TARGET_ABI32)
11867 ret
= do_get_thread_area(cpu_env
, arg1
);
11869 #elif defined(TARGET_M68K)
11871 TaskState
*ts
= cpu
->opaque
;
11872 ret
= ts
->tp_value
;
11876 goto unimplemented_nowarn
;
11879 #ifdef TARGET_NR_getdomainname
11880 case TARGET_NR_getdomainname
:
11881 goto unimplemented_nowarn
;
11884 #ifdef TARGET_NR_clock_settime
11885 case TARGET_NR_clock_settime
:
11887 struct timespec ts
;
11889 ret
= target_to_host_timespec(&ts
, arg2
);
11890 if (!is_error(ret
)) {
11891 ret
= get_errno(clock_settime(arg1
, &ts
));
11896 #ifdef TARGET_NR_clock_gettime
11897 case TARGET_NR_clock_gettime
:
11899 struct timespec ts
;
11900 ret
= get_errno(clock_gettime(arg1
, &ts
));
11901 if (!is_error(ret
)) {
11902 ret
= host_to_target_timespec(arg2
, &ts
);
11907 #ifdef TARGET_NR_clock_getres
11908 case TARGET_NR_clock_getres
:
11910 struct timespec ts
;
11911 ret
= get_errno(clock_getres(arg1
, &ts
));
11912 if (!is_error(ret
)) {
11913 host_to_target_timespec(arg2
, &ts
);
11918 #ifdef TARGET_NR_clock_nanosleep
11919 case TARGET_NR_clock_nanosleep
:
11921 struct timespec ts
;
11922 target_to_host_timespec(&ts
, arg3
);
11923 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
11924 &ts
, arg4
? &ts
: NULL
));
11926 host_to_target_timespec(arg4
, &ts
);
11928 #if defined(TARGET_PPC)
11929 /* clock_nanosleep is odd in that it returns positive errno values.
11930 * On PPC, CR0 bit 3 should be set in such a situation. */
11931 if (ret
&& ret
!= -TARGET_ERESTARTSYS
) {
11932 ((CPUPPCState
*)cpu_env
)->crf
[0] |= 1;
11939 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
11940 case TARGET_NR_set_tid_address
:
11941 ret
= get_errno(set_tid_address((int *)g2h(arg1
)));
11945 case TARGET_NR_tkill
:
11946 ret
= get_errno(safe_tkill((int)arg1
, target_to_host_signal(arg2
)));
11949 case TARGET_NR_tgkill
:
11950 ret
= get_errno(safe_tgkill((int)arg1
, (int)arg2
,
11951 target_to_host_signal(arg3
)));
11954 #ifdef TARGET_NR_set_robust_list
11955 case TARGET_NR_set_robust_list
:
11956 case TARGET_NR_get_robust_list
:
11957 /* The ABI for supporting robust futexes has userspace pass
11958 * the kernel a pointer to a linked list which is updated by
11959 * userspace after the syscall; the list is walked by the kernel
11960 * when the thread exits. Since the linked list in QEMU guest
11961 * memory isn't a valid linked list for the host and we have
11962 * no way to reliably intercept the thread-death event, we can't
11963 * support these. Silently return ENOSYS so that guest userspace
11964 * falls back to a non-robust futex implementation (which should
11965 * be OK except in the corner case of the guest crashing while
11966 * holding a mutex that is shared with another process via
11969 goto unimplemented_nowarn
;
11972 #if defined(TARGET_NR_utimensat)
11973 case TARGET_NR_utimensat
:
11975 struct timespec
*tsp
, ts
[2];
11979 target_to_host_timespec(ts
, arg3
);
11980 target_to_host_timespec(ts
+1, arg3
+sizeof(struct target_timespec
));
11984 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
11986 if (!(p
= lock_user_string(arg2
))) {
11987 ret
= -TARGET_EFAULT
;
11990 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
11991 unlock_user(p
, arg2
, 0);
11996 case TARGET_NR_futex
:
11997 ret
= do_futex(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
11999 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
12000 case TARGET_NR_inotify_init
:
12001 ret
= get_errno(sys_inotify_init());
12003 fd_trans_register(ret
, &target_inotify_trans
);
12007 #ifdef CONFIG_INOTIFY1
12008 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
12009 case TARGET_NR_inotify_init1
:
12010 ret
= get_errno(sys_inotify_init1(target_to_host_bitmask(arg1
,
12011 fcntl_flags_tbl
)));
12013 fd_trans_register(ret
, &target_inotify_trans
);
12018 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
12019 case TARGET_NR_inotify_add_watch
:
12020 p
= lock_user_string(arg2
);
12021 ret
= get_errno(sys_inotify_add_watch(arg1
, path(p
), arg3
));
12022 unlock_user(p
, arg2
, 0);
12025 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
12026 case TARGET_NR_inotify_rm_watch
:
12027 ret
= get_errno(sys_inotify_rm_watch(arg1
, arg2
));
12031 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
12032 case TARGET_NR_mq_open
:
12034 struct mq_attr posix_mq_attr
;
12035 struct mq_attr
*pposix_mq_attr
;
12038 host_flags
= target_to_host_bitmask(arg2
, fcntl_flags_tbl
);
12039 pposix_mq_attr
= NULL
;
12041 if (copy_from_user_mq_attr(&posix_mq_attr
, arg4
) != 0) {
12044 pposix_mq_attr
= &posix_mq_attr
;
12046 p
= lock_user_string(arg1
- 1);
12050 ret
= get_errno(mq_open(p
, host_flags
, arg3
, pposix_mq_attr
));
12051 unlock_user (p
, arg1
, 0);
12055 case TARGET_NR_mq_unlink
:
12056 p
= lock_user_string(arg1
- 1);
12058 ret
= -TARGET_EFAULT
;
12061 ret
= get_errno(mq_unlink(p
));
12062 unlock_user (p
, arg1
, 0);
12065 case TARGET_NR_mq_timedsend
:
12067 struct timespec ts
;
12069 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12071 target_to_host_timespec(&ts
, arg5
);
12072 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
12073 host_to_target_timespec(arg5
, &ts
);
12075 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
12077 unlock_user (p
, arg2
, arg3
);
12081 case TARGET_NR_mq_timedreceive
:
12083 struct timespec ts
;
12086 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12088 target_to_host_timespec(&ts
, arg5
);
12089 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12091 host_to_target_timespec(arg5
, &ts
);
12093 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12096 unlock_user (p
, arg2
, arg3
);
12098 put_user_u32(prio
, arg4
);
12102 /* Not implemented for now... */
12103 /* case TARGET_NR_mq_notify: */
12106 case TARGET_NR_mq_getsetattr
:
12108 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
12111 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
12112 ret
= get_errno(mq_setattr(arg1
, &posix_mq_attr_in
,
12113 &posix_mq_attr_out
));
12114 } else if (arg3
!= 0) {
12115 ret
= get_errno(mq_getattr(arg1
, &posix_mq_attr_out
));
12117 if (ret
== 0 && arg3
!= 0) {
12118 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
12124 #ifdef CONFIG_SPLICE
12125 #ifdef TARGET_NR_tee
12126 case TARGET_NR_tee
:
12128 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
12132 #ifdef TARGET_NR_splice
12133 case TARGET_NR_splice
:
12135 loff_t loff_in
, loff_out
;
12136 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
12138 if (get_user_u64(loff_in
, arg2
)) {
12141 ploff_in
= &loff_in
;
12144 if (get_user_u64(loff_out
, arg4
)) {
12147 ploff_out
= &loff_out
;
12149 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
12151 if (put_user_u64(loff_in
, arg2
)) {
12156 if (put_user_u64(loff_out
, arg4
)) {
12163 #ifdef TARGET_NR_vmsplice
12164 case TARGET_NR_vmsplice
:
12166 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
12168 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
12169 unlock_iovec(vec
, arg2
, arg3
, 0);
12171 ret
= -host_to_target_errno(errno
);
12176 #endif /* CONFIG_SPLICE */
12177 #ifdef CONFIG_EVENTFD
12178 #if defined(TARGET_NR_eventfd)
12179 case TARGET_NR_eventfd
:
12180 ret
= get_errno(eventfd(arg1
, 0));
12182 fd_trans_register(ret
, &target_eventfd_trans
);
12186 #if defined(TARGET_NR_eventfd2)
12187 case TARGET_NR_eventfd2
:
12189 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
));
12190 if (arg2
& TARGET_O_NONBLOCK
) {
12191 host_flags
|= O_NONBLOCK
;
12193 if (arg2
& TARGET_O_CLOEXEC
) {
12194 host_flags
|= O_CLOEXEC
;
12196 ret
= get_errno(eventfd(arg1
, host_flags
));
12198 fd_trans_register(ret
, &target_eventfd_trans
);
12203 #endif /* CONFIG_EVENTFD */
12204 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
12205 case TARGET_NR_fallocate
:
12206 #if TARGET_ABI_BITS == 32
12207 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
12208 target_offset64(arg5
, arg6
)));
12210 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
12214 #if defined(CONFIG_SYNC_FILE_RANGE)
12215 #if defined(TARGET_NR_sync_file_range)
12216 case TARGET_NR_sync_file_range
:
12217 #if TARGET_ABI_BITS == 32
12218 #if defined(TARGET_MIPS)
12219 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
12220 target_offset64(arg5
, arg6
), arg7
));
12222 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
12223 target_offset64(arg4
, arg5
), arg6
));
12224 #endif /* !TARGET_MIPS */
12226 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
12230 #if defined(TARGET_NR_sync_file_range2)
12231 case TARGET_NR_sync_file_range2
:
12232 /* This is like sync_file_range but the arguments are reordered */
12233 #if TARGET_ABI_BITS == 32
12234 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
12235 target_offset64(arg5
, arg6
), arg2
));
12237 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
12242 #if defined(TARGET_NR_signalfd4)
12243 case TARGET_NR_signalfd4
:
12244 ret
= do_signalfd4(arg1
, arg2
, arg4
);
12247 #if defined(TARGET_NR_signalfd)
12248 case TARGET_NR_signalfd
:
12249 ret
= do_signalfd4(arg1
, arg2
, 0);
12252 #if defined(CONFIG_EPOLL)
12253 #if defined(TARGET_NR_epoll_create)
12254 case TARGET_NR_epoll_create
:
12255 ret
= get_errno(epoll_create(arg1
));
12258 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
12259 case TARGET_NR_epoll_create1
:
12260 ret
= get_errno(epoll_create1(arg1
));
12263 #if defined(TARGET_NR_epoll_ctl)
12264 case TARGET_NR_epoll_ctl
:
12266 struct epoll_event ep
;
12267 struct epoll_event
*epp
= 0;
12269 struct target_epoll_event
*target_ep
;
12270 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
12273 ep
.events
= tswap32(target_ep
->events
);
12274 /* The epoll_data_t union is just opaque data to the kernel,
12275 * so we transfer all 64 bits across and need not worry what
12276 * actual data type it is.
12278 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
12279 unlock_user_struct(target_ep
, arg4
, 0);
12282 ret
= get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
12287 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
12288 #if defined(TARGET_NR_epoll_wait)
12289 case TARGET_NR_epoll_wait
:
12291 #if defined(TARGET_NR_epoll_pwait)
12292 case TARGET_NR_epoll_pwait
:
12295 struct target_epoll_event
*target_ep
;
12296 struct epoll_event
*ep
;
12298 int maxevents
= arg3
;
12299 int timeout
= arg4
;
12301 if (maxevents
<= 0 || maxevents
> TARGET_EP_MAX_EVENTS
) {
12302 ret
= -TARGET_EINVAL
;
12306 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
12307 maxevents
* sizeof(struct target_epoll_event
), 1);
12312 ep
= g_try_new(struct epoll_event
, maxevents
);
12314 unlock_user(target_ep
, arg2
, 0);
12315 ret
= -TARGET_ENOMEM
;
12320 #if defined(TARGET_NR_epoll_pwait)
12321 case TARGET_NR_epoll_pwait
:
12323 target_sigset_t
*target_set
;
12324 sigset_t _set
, *set
= &_set
;
12327 if (arg6
!= sizeof(target_sigset_t
)) {
12328 ret
= -TARGET_EINVAL
;
12332 target_set
= lock_user(VERIFY_READ
, arg5
,
12333 sizeof(target_sigset_t
), 1);
12335 ret
= -TARGET_EFAULT
;
12338 target_to_host_sigset(set
, target_set
);
12339 unlock_user(target_set
, arg5
, 0);
12344 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
12345 set
, SIGSET_T_SIZE
));
12349 #if defined(TARGET_NR_epoll_wait)
12350 case TARGET_NR_epoll_wait
:
12351 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
12356 ret
= -TARGET_ENOSYS
;
12358 if (!is_error(ret
)) {
12360 for (i
= 0; i
< ret
; i
++) {
12361 target_ep
[i
].events
= tswap32(ep
[i
].events
);
12362 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
12364 unlock_user(target_ep
, arg2
,
12365 ret
* sizeof(struct target_epoll_event
));
12367 unlock_user(target_ep
, arg2
, 0);
12374 #ifdef TARGET_NR_prlimit64
12375 case TARGET_NR_prlimit64
:
12377 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
12378 struct target_rlimit64
*target_rnew
, *target_rold
;
12379 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
12380 int resource
= target_to_host_resource(arg2
);
12382 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
12385 rnew
.rlim_cur
= tswap64(target_rnew
->rlim_cur
);
12386 rnew
.rlim_max
= tswap64(target_rnew
->rlim_max
);
12387 unlock_user_struct(target_rnew
, arg3
, 0);
12391 ret
= get_errno(sys_prlimit64(arg1
, resource
, rnewp
, arg4
? &rold
: 0));
12392 if (!is_error(ret
) && arg4
) {
12393 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
12396 target_rold
->rlim_cur
= tswap64(rold
.rlim_cur
);
12397 target_rold
->rlim_max
= tswap64(rold
.rlim_max
);
12398 unlock_user_struct(target_rold
, arg4
, 1);
12403 #ifdef TARGET_NR_gethostname
12404 case TARGET_NR_gethostname
:
12406 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
12408 ret
= get_errno(gethostname(name
, arg2
));
12409 unlock_user(name
, arg1
, arg2
);
12411 ret
= -TARGET_EFAULT
;
12416 #ifdef TARGET_NR_atomic_cmpxchg_32
12417 case TARGET_NR_atomic_cmpxchg_32
:
12419 /* should use start_exclusive from main.c */
12420 abi_ulong mem_value
;
12421 if (get_user_u32(mem_value
, arg6
)) {
12422 target_siginfo_t info
;
12423 info
.si_signo
= SIGSEGV
;
12425 info
.si_code
= TARGET_SEGV_MAPERR
;
12426 info
._sifields
._sigfault
._addr
= arg6
;
12427 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
,
12428 QEMU_SI_FAULT
, &info
);
12432 if (mem_value
== arg2
)
12433 put_user_u32(arg1
, arg6
);
12438 #ifdef TARGET_NR_atomic_barrier
12439 case TARGET_NR_atomic_barrier
:
12441 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
12447 #ifdef TARGET_NR_timer_create
12448 case TARGET_NR_timer_create
:
12450 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
12452 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
12455 int timer_index
= next_free_host_timer();
12457 if (timer_index
< 0) {
12458 ret
= -TARGET_EAGAIN
;
12460 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
12463 phost_sevp
= &host_sevp
;
12464 ret
= target_to_host_sigevent(phost_sevp
, arg2
);
12470 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
12474 if (put_user(TIMER_MAGIC
| timer_index
, arg3
, target_timer_t
)) {
12483 #ifdef TARGET_NR_timer_settime
12484 case TARGET_NR_timer_settime
:
12486 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
12487 * struct itimerspec * old_value */
12488 target_timer_t timerid
= get_timer_id(arg1
);
12492 } else if (arg3
== 0) {
12493 ret
= -TARGET_EINVAL
;
12495 timer_t htimer
= g_posix_timers
[timerid
];
12496 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
12498 if (target_to_host_itimerspec(&hspec_new
, arg3
)) {
12502 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
12503 if (arg4
&& host_to_target_itimerspec(arg4
, &hspec_old
)) {
12511 #ifdef TARGET_NR_timer_gettime
12512 case TARGET_NR_timer_gettime
:
12514 /* args: timer_t timerid, struct itimerspec *curr_value */
12515 target_timer_t timerid
= get_timer_id(arg1
);
12519 } else if (!arg2
) {
12520 ret
= -TARGET_EFAULT
;
12522 timer_t htimer
= g_posix_timers
[timerid
];
12523 struct itimerspec hspec
;
12524 ret
= get_errno(timer_gettime(htimer
, &hspec
));
12526 if (host_to_target_itimerspec(arg2
, &hspec
)) {
12527 ret
= -TARGET_EFAULT
;
12534 #ifdef TARGET_NR_timer_getoverrun
12535 case TARGET_NR_timer_getoverrun
:
12537 /* args: timer_t timerid */
12538 target_timer_t timerid
= get_timer_id(arg1
);
12543 timer_t htimer
= g_posix_timers
[timerid
];
12544 ret
= get_errno(timer_getoverrun(htimer
));
12546 fd_trans_unregister(ret
);
12551 #ifdef TARGET_NR_timer_delete
12552 case TARGET_NR_timer_delete
:
12554 /* args: timer_t timerid */
12555 target_timer_t timerid
= get_timer_id(arg1
);
12560 timer_t htimer
= g_posix_timers
[timerid
];
12561 ret
= get_errno(timer_delete(htimer
));
12562 g_posix_timers
[timerid
] = 0;
12568 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
12569 case TARGET_NR_timerfd_create
:
12570 ret
= get_errno(timerfd_create(arg1
,
12571 target_to_host_bitmask(arg2
, fcntl_flags_tbl
)));
12575 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
12576 case TARGET_NR_timerfd_gettime
:
12578 struct itimerspec its_curr
;
12580 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
12582 if (arg2
&& host_to_target_itimerspec(arg2
, &its_curr
)) {
12589 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
12590 case TARGET_NR_timerfd_settime
:
12592 struct itimerspec its_new
, its_old
, *p_new
;
12595 if (target_to_host_itimerspec(&its_new
, arg3
)) {
12603 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
12605 if (arg4
&& host_to_target_itimerspec(arg4
, &its_old
)) {
12612 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
12613 case TARGET_NR_ioprio_get
:
12614 ret
= get_errno(ioprio_get(arg1
, arg2
));
12618 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
12619 case TARGET_NR_ioprio_set
:
12620 ret
= get_errno(ioprio_set(arg1
, arg2
, arg3
));
12624 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
12625 case TARGET_NR_setns
:
12626 ret
= get_errno(setns(arg1
, arg2
));
12629 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
12630 case TARGET_NR_unshare
:
12631 ret
= get_errno(unshare(arg1
));
12634 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
12635 case TARGET_NR_kcmp
:
12636 ret
= get_errno(kcmp(arg1
, arg2
, arg3
, arg4
, arg5
));
12642 gemu_log("qemu: Unsupported syscall: %d\n", num
);
12643 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
12644 unimplemented_nowarn
:
12646 ret
= -TARGET_ENOSYS
;
12651 gemu_log(" = " TARGET_ABI_FMT_ld
"\n", ret
);
12654 print_syscall_ret(num
, ret
);
12655 trace_guest_user_syscall_ret(cpu
, num
, ret
);
12658 ret
= -TARGET_EFAULT
;