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"
60 #include <sys/timerfd.h>
66 #include <sys/eventfd.h>
69 #include <sys/epoll.h>
72 #include "qemu/xattr.h"
74 #ifdef CONFIG_SENDFILE
75 #include <sys/sendfile.h>
78 #define termios host_termios
79 #define winsize host_winsize
80 #define termio host_termio
81 #define sgttyb host_sgttyb /* same as target */
82 #define tchars host_tchars /* same as target */
83 #define ltchars host_ltchars /* same as target */
85 #include <linux/termios.h>
86 #include <linux/unistd.h>
87 #include <linux/cdrom.h>
88 #include <linux/hdreg.h>
89 #include <linux/soundcard.h>
91 #include <linux/mtio.h>
93 #if defined(CONFIG_FIEMAP)
94 #include <linux/fiemap.h>
98 #include <linux/dm-ioctl.h>
99 #include <linux/reboot.h>
100 #include <linux/route.h>
101 #include <linux/filter.h>
102 #include <linux/blkpg.h>
103 #include <netpacket/packet.h>
104 #include <linux/netlink.h>
105 #ifdef CONFIG_RTNETLINK
106 #include <linux/rtnetlink.h>
107 #include <linux/if_bridge.h>
109 #include <linux/audit.h>
110 #include "linux_loop.h"
116 #define CLONE_IO 0x80000000 /* Clone io context */
119 /* We can't directly call the host clone syscall, because this will
120 * badly confuse libc (breaking mutexes, for example). So we must
121 * divide clone flags into:
122 * * flag combinations that look like pthread_create()
123 * * flag combinations that look like fork()
124 * * flags we can implement within QEMU itself
125 * * flags we can't support and will return an error for
127 /* For thread creation, all these flags must be present; for
128 * fork, none must be present.
130 #define CLONE_THREAD_FLAGS \
131 (CLONE_VM | CLONE_FS | CLONE_FILES | \
132 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
134 /* These flags are ignored:
135 * CLONE_DETACHED is now ignored by the kernel;
136 * CLONE_IO is just an optimisation hint to the I/O scheduler
138 #define CLONE_IGNORED_FLAGS \
139 (CLONE_DETACHED | CLONE_IO)
141 /* Flags for fork which we can implement within QEMU itself */
142 #define CLONE_OPTIONAL_FORK_FLAGS \
143 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
144 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
146 /* Flags for thread creation which we can implement within QEMU itself */
147 #define CLONE_OPTIONAL_THREAD_FLAGS \
148 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
149 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
151 #define CLONE_INVALID_FORK_FLAGS \
152 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
154 #define CLONE_INVALID_THREAD_FLAGS \
155 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
156 CLONE_IGNORED_FLAGS))
158 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
159 * have almost all been allocated. We cannot support any of
160 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
161 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
162 * The checks against the invalid thread masks above will catch these.
163 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
167 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
168 * once. This exercises the codepaths for restart.
170 //#define DEBUG_ERESTARTSYS
172 //#include <linux/msdos_fs.h>
173 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
174 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
184 #define _syscall0(type,name) \
185 static type name (void) \
187 return syscall(__NR_##name); \
190 #define _syscall1(type,name,type1,arg1) \
191 static type name (type1 arg1) \
193 return syscall(__NR_##name, arg1); \
196 #define _syscall2(type,name,type1,arg1,type2,arg2) \
197 static type name (type1 arg1,type2 arg2) \
199 return syscall(__NR_##name, arg1, arg2); \
202 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
203 static type name (type1 arg1,type2 arg2,type3 arg3) \
205 return syscall(__NR_##name, arg1, arg2, arg3); \
208 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
209 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
211 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
214 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
216 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
218 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
222 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
223 type5,arg5,type6,arg6) \
224 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
227 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
231 #define __NR_sys_uname __NR_uname
232 #define __NR_sys_getcwd1 __NR_getcwd
233 #define __NR_sys_getdents __NR_getdents
234 #define __NR_sys_getdents64 __NR_getdents64
235 #define __NR_sys_getpriority __NR_getpriority
236 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
237 #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
238 #define __NR_sys_syslog __NR_syslog
239 #define __NR_sys_futex __NR_futex
240 #define __NR_sys_inotify_init __NR_inotify_init
241 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
242 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
244 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
245 #define __NR__llseek __NR_lseek
248 /* Newer kernel ports have llseek() instead of _llseek() */
249 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
250 #define TARGET_NR__llseek TARGET_NR_llseek
254 _syscall0(int, gettid
)
256 /* This is a replacement for the host gettid() and must return a host
258 static int gettid(void) {
262 #if defined(TARGET_NR_getdents) && defined(__NR_getdents)
263 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
265 #if !defined(__NR_getdents) || \
266 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
267 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
269 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
270 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
271 loff_t
*, res
, uint
, wh
);
273 _syscall3(int, sys_rt_sigqueueinfo
, pid_t
, pid
, int, sig
, siginfo_t
*, uinfo
)
274 _syscall4(int, sys_rt_tgsigqueueinfo
, pid_t
, pid
, pid_t
, tid
, int, sig
,
276 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
277 #ifdef __NR_exit_group
278 _syscall1(int,exit_group
,int,error_code
)
280 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
281 _syscall1(int,set_tid_address
,int *,tidptr
)
283 #if defined(TARGET_NR_futex) && defined(__NR_futex)
284 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
285 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
287 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
288 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
289 unsigned long *, user_mask_ptr
);
290 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
291 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
292 unsigned long *, user_mask_ptr
);
293 #define __NR_sys_getcpu __NR_getcpu
294 _syscall3(int, sys_getcpu
, unsigned *, cpu
, unsigned *, node
, void *, tcache
);
295 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
297 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
298 struct __user_cap_data_struct
*, data
);
299 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
300 struct __user_cap_data_struct
*, data
);
301 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
302 _syscall2(int, ioprio_get
, int, which
, int, who
)
304 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
305 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
307 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
308 _syscall3(int, getrandom
, void *, buf
, size_t, buflen
, unsigned int, flags
)
311 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
312 _syscall5(int, kcmp
, pid_t
, pid1
, pid_t
, pid2
, int, type
,
313 unsigned long, idx1
, unsigned long, idx2
)
316 static bitmask_transtbl fcntl_flags_tbl
[] = {
317 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
318 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
319 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
320 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
321 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
322 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
323 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
324 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
325 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
326 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
327 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
328 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
329 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
330 #if defined(O_DIRECT)
331 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
333 #if defined(O_NOATIME)
334 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
336 #if defined(O_CLOEXEC)
337 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
340 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
342 #if defined(O_TMPFILE)
343 { TARGET_O_TMPFILE
, TARGET_O_TMPFILE
, O_TMPFILE
, O_TMPFILE
},
345 /* Don't terminate the list prematurely on 64-bit host+guest. */
346 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
347 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
354 QEMU_IFLA_BR_FORWARD_DELAY
,
355 QEMU_IFLA_BR_HELLO_TIME
,
356 QEMU_IFLA_BR_MAX_AGE
,
357 QEMU_IFLA_BR_AGEING_TIME
,
358 QEMU_IFLA_BR_STP_STATE
,
359 QEMU_IFLA_BR_PRIORITY
,
360 QEMU_IFLA_BR_VLAN_FILTERING
,
361 QEMU_IFLA_BR_VLAN_PROTOCOL
,
362 QEMU_IFLA_BR_GROUP_FWD_MASK
,
363 QEMU_IFLA_BR_ROOT_ID
,
364 QEMU_IFLA_BR_BRIDGE_ID
,
365 QEMU_IFLA_BR_ROOT_PORT
,
366 QEMU_IFLA_BR_ROOT_PATH_COST
,
367 QEMU_IFLA_BR_TOPOLOGY_CHANGE
,
368 QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED
,
369 QEMU_IFLA_BR_HELLO_TIMER
,
370 QEMU_IFLA_BR_TCN_TIMER
,
371 QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER
,
372 QEMU_IFLA_BR_GC_TIMER
,
373 QEMU_IFLA_BR_GROUP_ADDR
,
374 QEMU_IFLA_BR_FDB_FLUSH
,
375 QEMU_IFLA_BR_MCAST_ROUTER
,
376 QEMU_IFLA_BR_MCAST_SNOOPING
,
377 QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR
,
378 QEMU_IFLA_BR_MCAST_QUERIER
,
379 QEMU_IFLA_BR_MCAST_HASH_ELASTICITY
,
380 QEMU_IFLA_BR_MCAST_HASH_MAX
,
381 QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT
,
382 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT
,
383 QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL
,
384 QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL
,
385 QEMU_IFLA_BR_MCAST_QUERIER_INTVL
,
386 QEMU_IFLA_BR_MCAST_QUERY_INTVL
,
387 QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL
,
388 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL
,
389 QEMU_IFLA_BR_NF_CALL_IPTABLES
,
390 QEMU_IFLA_BR_NF_CALL_IP6TABLES
,
391 QEMU_IFLA_BR_NF_CALL_ARPTABLES
,
392 QEMU_IFLA_BR_VLAN_DEFAULT_PVID
,
394 QEMU_IFLA_BR_VLAN_STATS_ENABLED
,
395 QEMU_IFLA_BR_MCAST_STATS_ENABLED
,
419 QEMU_IFLA_NET_NS_PID
,
422 QEMU_IFLA_VFINFO_LIST
,
430 QEMU_IFLA_PROMISCUITY
,
431 QEMU_IFLA_NUM_TX_QUEUES
,
432 QEMU_IFLA_NUM_RX_QUEUES
,
434 QEMU_IFLA_PHYS_PORT_ID
,
435 QEMU_IFLA_CARRIER_CHANGES
,
436 QEMU_IFLA_PHYS_SWITCH_ID
,
437 QEMU_IFLA_LINK_NETNSID
,
438 QEMU_IFLA_PHYS_PORT_NAME
,
439 QEMU_IFLA_PROTO_DOWN
,
440 QEMU_IFLA_GSO_MAX_SEGS
,
441 QEMU_IFLA_GSO_MAX_SIZE
,
448 QEMU_IFLA_BRPORT_UNSPEC
,
449 QEMU_IFLA_BRPORT_STATE
,
450 QEMU_IFLA_BRPORT_PRIORITY
,
451 QEMU_IFLA_BRPORT_COST
,
452 QEMU_IFLA_BRPORT_MODE
,
453 QEMU_IFLA_BRPORT_GUARD
,
454 QEMU_IFLA_BRPORT_PROTECT
,
455 QEMU_IFLA_BRPORT_FAST_LEAVE
,
456 QEMU_IFLA_BRPORT_LEARNING
,
457 QEMU_IFLA_BRPORT_UNICAST_FLOOD
,
458 QEMU_IFLA_BRPORT_PROXYARP
,
459 QEMU_IFLA_BRPORT_LEARNING_SYNC
,
460 QEMU_IFLA_BRPORT_PROXYARP_WIFI
,
461 QEMU_IFLA_BRPORT_ROOT_ID
,
462 QEMU_IFLA_BRPORT_BRIDGE_ID
,
463 QEMU_IFLA_BRPORT_DESIGNATED_PORT
,
464 QEMU_IFLA_BRPORT_DESIGNATED_COST
,
467 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK
,
468 QEMU_IFLA_BRPORT_CONFIG_PENDING
,
469 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER
,
470 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER
,
471 QEMU_IFLA_BRPORT_HOLD_TIMER
,
472 QEMU_IFLA_BRPORT_FLUSH
,
473 QEMU_IFLA_BRPORT_MULTICAST_ROUTER
,
474 QEMU_IFLA_BRPORT_PAD
,
475 QEMU___IFLA_BRPORT_MAX
479 QEMU_IFLA_INFO_UNSPEC
,
482 QEMU_IFLA_INFO_XSTATS
,
483 QEMU_IFLA_INFO_SLAVE_KIND
,
484 QEMU_IFLA_INFO_SLAVE_DATA
,
485 QEMU___IFLA_INFO_MAX
,
489 QEMU_IFLA_INET_UNSPEC
,
491 QEMU___IFLA_INET_MAX
,
495 QEMU_IFLA_INET6_UNSPEC
,
496 QEMU_IFLA_INET6_FLAGS
,
497 QEMU_IFLA_INET6_CONF
,
498 QEMU_IFLA_INET6_STATS
,
499 QEMU_IFLA_INET6_MCAST
,
500 QEMU_IFLA_INET6_CACHEINFO
,
501 QEMU_IFLA_INET6_ICMP6STATS
,
502 QEMU_IFLA_INET6_TOKEN
,
503 QEMU_IFLA_INET6_ADDR_GEN_MODE
,
504 QEMU___IFLA_INET6_MAX
507 typedef abi_long (*TargetFdDataFunc
)(void *, size_t);
508 typedef abi_long (*TargetFdAddrFunc
)(void *, abi_ulong
, socklen_t
);
509 typedef struct TargetFdTrans
{
510 TargetFdDataFunc host_to_target_data
;
511 TargetFdDataFunc target_to_host_data
;
512 TargetFdAddrFunc target_to_host_addr
;
515 static TargetFdTrans
**target_fd_trans
;
517 static unsigned int target_fd_max
;
519 static TargetFdDataFunc
fd_trans_target_to_host_data(int fd
)
521 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
522 return target_fd_trans
[fd
]->target_to_host_data
;
527 static TargetFdDataFunc
fd_trans_host_to_target_data(int fd
)
529 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
530 return target_fd_trans
[fd
]->host_to_target_data
;
535 static TargetFdAddrFunc
fd_trans_target_to_host_addr(int fd
)
537 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
538 return target_fd_trans
[fd
]->target_to_host_addr
;
543 static void fd_trans_register(int fd
, TargetFdTrans
*trans
)
547 if (fd
>= target_fd_max
) {
548 oldmax
= target_fd_max
;
549 target_fd_max
= ((fd
>> 6) + 1) << 6; /* by slice of 64 entries */
550 target_fd_trans
= g_renew(TargetFdTrans
*,
551 target_fd_trans
, target_fd_max
);
552 memset((void *)(target_fd_trans
+ oldmax
), 0,
553 (target_fd_max
- oldmax
) * sizeof(TargetFdTrans
*));
555 target_fd_trans
[fd
] = trans
;
558 static void fd_trans_unregister(int fd
)
560 if (fd
>= 0 && fd
< target_fd_max
) {
561 target_fd_trans
[fd
] = NULL
;
565 static void fd_trans_dup(int oldfd
, int newfd
)
567 fd_trans_unregister(newfd
);
568 if (oldfd
< target_fd_max
&& target_fd_trans
[oldfd
]) {
569 fd_trans_register(newfd
, target_fd_trans
[oldfd
]);
573 static int sys_getcwd1(char *buf
, size_t size
)
575 if (getcwd(buf
, size
) == NULL
) {
576 /* getcwd() sets errno */
579 return strlen(buf
)+1;
582 #ifdef TARGET_NR_utimensat
583 #if defined(__NR_utimensat)
584 #define __NR_sys_utimensat __NR_utimensat
585 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
586 const struct timespec
*,tsp
,int,flags
)
588 static int sys_utimensat(int dirfd
, const char *pathname
,
589 const struct timespec times
[2], int flags
)
595 #endif /* TARGET_NR_utimensat */
597 #ifdef TARGET_NR_renameat2
598 #if defined(__NR_renameat2)
599 #define __NR_sys_renameat2 __NR_renameat2
600 _syscall5(int, sys_renameat2
, int, oldfd
, const char *, old
, int, newfd
,
601 const char *, new, unsigned int, flags
)
603 static int sys_renameat2(int oldfd
, const char *old
,
604 int newfd
, const char *new, int flags
)
607 return renameat(oldfd
, old
, newfd
, new);
613 #endif /* TARGET_NR_renameat2 */
615 #ifdef CONFIG_INOTIFY
616 #include <sys/inotify.h>
618 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
619 static int sys_inotify_init(void)
621 return (inotify_init());
624 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
625 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
627 return (inotify_add_watch(fd
, pathname
, mask
));
630 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
631 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
633 return (inotify_rm_watch(fd
, wd
));
636 #ifdef CONFIG_INOTIFY1
637 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
638 static int sys_inotify_init1(int flags
)
640 return (inotify_init1(flags
));
645 /* Userspace can usually survive runtime without inotify */
646 #undef TARGET_NR_inotify_init
647 #undef TARGET_NR_inotify_init1
648 #undef TARGET_NR_inotify_add_watch
649 #undef TARGET_NR_inotify_rm_watch
650 #endif /* CONFIG_INOTIFY */
652 #if defined(TARGET_NR_prlimit64)
653 #ifndef __NR_prlimit64
654 # define __NR_prlimit64 -1
656 #define __NR_sys_prlimit64 __NR_prlimit64
657 /* The glibc rlimit structure may not be that used by the underlying syscall */
658 struct host_rlimit64
{
662 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
663 const struct host_rlimit64
*, new_limit
,
664 struct host_rlimit64
*, old_limit
)
668 #if defined(TARGET_NR_timer_create)
669 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
670 static timer_t g_posix_timers
[32] = { 0, } ;
672 static inline int next_free_host_timer(void)
675 /* FIXME: Does finding the next free slot require a lock? */
676 for (k
= 0; k
< ARRAY_SIZE(g_posix_timers
); k
++) {
677 if (g_posix_timers
[k
] == 0) {
678 g_posix_timers
[k
] = (timer_t
) 1;
686 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
688 static inline int regpairs_aligned(void *cpu_env
, int num
)
690 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
692 #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
693 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
694 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
695 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
696 * of registers which translates to the same as ARM/MIPS, because we start with
698 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
699 #elif defined(TARGET_SH4)
700 /* SH4 doesn't align register pairs, except for p{read,write}64 */
701 static inline int regpairs_aligned(void *cpu_env
, int num
)
704 case TARGET_NR_pread64
:
705 case TARGET_NR_pwrite64
:
712 #elif defined(TARGET_XTENSA)
713 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
715 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 0; }
718 #define ERRNO_TABLE_SIZE 1200
720 /* target_to_host_errno_table[] is initialized from
721 * host_to_target_errno_table[] in syscall_init(). */
722 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
726 * This list is the union of errno values overridden in asm-<arch>/errno.h
727 * minus the errnos that are not actually generic to all archs.
729 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
730 [EAGAIN
] = TARGET_EAGAIN
,
731 [EIDRM
] = TARGET_EIDRM
,
732 [ECHRNG
] = TARGET_ECHRNG
,
733 [EL2NSYNC
] = TARGET_EL2NSYNC
,
734 [EL3HLT
] = TARGET_EL3HLT
,
735 [EL3RST
] = TARGET_EL3RST
,
736 [ELNRNG
] = TARGET_ELNRNG
,
737 [EUNATCH
] = TARGET_EUNATCH
,
738 [ENOCSI
] = TARGET_ENOCSI
,
739 [EL2HLT
] = TARGET_EL2HLT
,
740 [EDEADLK
] = TARGET_EDEADLK
,
741 [ENOLCK
] = TARGET_ENOLCK
,
742 [EBADE
] = TARGET_EBADE
,
743 [EBADR
] = TARGET_EBADR
,
744 [EXFULL
] = TARGET_EXFULL
,
745 [ENOANO
] = TARGET_ENOANO
,
746 [EBADRQC
] = TARGET_EBADRQC
,
747 [EBADSLT
] = TARGET_EBADSLT
,
748 [EBFONT
] = TARGET_EBFONT
,
749 [ENOSTR
] = TARGET_ENOSTR
,
750 [ENODATA
] = TARGET_ENODATA
,
751 [ETIME
] = TARGET_ETIME
,
752 [ENOSR
] = TARGET_ENOSR
,
753 [ENONET
] = TARGET_ENONET
,
754 [ENOPKG
] = TARGET_ENOPKG
,
755 [EREMOTE
] = TARGET_EREMOTE
,
756 [ENOLINK
] = TARGET_ENOLINK
,
757 [EADV
] = TARGET_EADV
,
758 [ESRMNT
] = TARGET_ESRMNT
,
759 [ECOMM
] = TARGET_ECOMM
,
760 [EPROTO
] = TARGET_EPROTO
,
761 [EDOTDOT
] = TARGET_EDOTDOT
,
762 [EMULTIHOP
] = TARGET_EMULTIHOP
,
763 [EBADMSG
] = TARGET_EBADMSG
,
764 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
765 [EOVERFLOW
] = TARGET_EOVERFLOW
,
766 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
767 [EBADFD
] = TARGET_EBADFD
,
768 [EREMCHG
] = TARGET_EREMCHG
,
769 [ELIBACC
] = TARGET_ELIBACC
,
770 [ELIBBAD
] = TARGET_ELIBBAD
,
771 [ELIBSCN
] = TARGET_ELIBSCN
,
772 [ELIBMAX
] = TARGET_ELIBMAX
,
773 [ELIBEXEC
] = TARGET_ELIBEXEC
,
774 [EILSEQ
] = TARGET_EILSEQ
,
775 [ENOSYS
] = TARGET_ENOSYS
,
776 [ELOOP
] = TARGET_ELOOP
,
777 [ERESTART
] = TARGET_ERESTART
,
778 [ESTRPIPE
] = TARGET_ESTRPIPE
,
779 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
780 [EUSERS
] = TARGET_EUSERS
,
781 [ENOTSOCK
] = TARGET_ENOTSOCK
,
782 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
783 [EMSGSIZE
] = TARGET_EMSGSIZE
,
784 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
785 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
786 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
787 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
788 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
789 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
790 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
791 [EADDRINUSE
] = TARGET_EADDRINUSE
,
792 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
793 [ENETDOWN
] = TARGET_ENETDOWN
,
794 [ENETUNREACH
] = TARGET_ENETUNREACH
,
795 [ENETRESET
] = TARGET_ENETRESET
,
796 [ECONNABORTED
] = TARGET_ECONNABORTED
,
797 [ECONNRESET
] = TARGET_ECONNRESET
,
798 [ENOBUFS
] = TARGET_ENOBUFS
,
799 [EISCONN
] = TARGET_EISCONN
,
800 [ENOTCONN
] = TARGET_ENOTCONN
,
801 [EUCLEAN
] = TARGET_EUCLEAN
,
802 [ENOTNAM
] = TARGET_ENOTNAM
,
803 [ENAVAIL
] = TARGET_ENAVAIL
,
804 [EISNAM
] = TARGET_EISNAM
,
805 [EREMOTEIO
] = TARGET_EREMOTEIO
,
806 [EDQUOT
] = TARGET_EDQUOT
,
807 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
808 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
809 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
810 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
811 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
812 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
813 [EALREADY
] = TARGET_EALREADY
,
814 [EINPROGRESS
] = TARGET_EINPROGRESS
,
815 [ESTALE
] = TARGET_ESTALE
,
816 [ECANCELED
] = TARGET_ECANCELED
,
817 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
818 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
820 [ENOKEY
] = TARGET_ENOKEY
,
823 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
826 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
829 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
832 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
834 #ifdef ENOTRECOVERABLE
835 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
838 [ENOMSG
] = TARGET_ENOMSG
,
841 [ERFKILL
] = TARGET_ERFKILL
,
844 [EHWPOISON
] = TARGET_EHWPOISON
,
848 static inline int host_to_target_errno(int err
)
850 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
851 host_to_target_errno_table
[err
]) {
852 return host_to_target_errno_table
[err
];
857 static inline int target_to_host_errno(int err
)
859 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
860 target_to_host_errno_table
[err
]) {
861 return target_to_host_errno_table
[err
];
866 static inline abi_long
get_errno(abi_long ret
)
869 return -host_to_target_errno(errno
);
874 static inline int is_error(abi_long ret
)
876 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
879 const char *target_strerror(int err
)
881 if (err
== TARGET_ERESTARTSYS
) {
882 return "To be restarted";
884 if (err
== TARGET_QEMU_ESIGRETURN
) {
885 return "Successful exit from sigreturn";
888 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
891 return strerror(target_to_host_errno(err
));
894 #define safe_syscall0(type, name) \
895 static type safe_##name(void) \
897 return safe_syscall(__NR_##name); \
900 #define safe_syscall1(type, name, type1, arg1) \
901 static type safe_##name(type1 arg1) \
903 return safe_syscall(__NR_##name, arg1); \
906 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
907 static type safe_##name(type1 arg1, type2 arg2) \
909 return safe_syscall(__NR_##name, arg1, arg2); \
912 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
913 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
915 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
918 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
920 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
922 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
925 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
926 type4, arg4, type5, arg5) \
927 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
930 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
933 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
934 type4, arg4, type5, arg5, type6, arg6) \
935 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
936 type5 arg5, type6 arg6) \
938 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
941 safe_syscall3(ssize_t
, read
, int, fd
, void *, buff
, size_t, count
)
942 safe_syscall3(ssize_t
, write
, int, fd
, const void *, buff
, size_t, count
)
943 safe_syscall4(int, openat
, int, dirfd
, const char *, pathname
, \
944 int, flags
, mode_t
, mode
)
945 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
946 struct rusage
*, rusage
)
947 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
948 int, options
, struct rusage
*, rusage
)
949 safe_syscall3(int, execve
, const char *, filename
, char **, argv
, char **, envp
)
950 safe_syscall6(int, pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
, \
951 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
)
952 safe_syscall5(int, ppoll
, struct pollfd
*, ufds
, unsigned int, nfds
,
953 struct timespec
*, tsp
, const sigset_t
*, sigmask
,
955 safe_syscall6(int, epoll_pwait
, int, epfd
, struct epoll_event
*, events
,
956 int, maxevents
, int, timeout
, const sigset_t
*, sigmask
,
958 safe_syscall6(int,futex
,int *,uaddr
,int,op
,int,val
, \
959 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
960 safe_syscall2(int, rt_sigsuspend
, sigset_t
*, newset
, size_t, sigsetsize
)
961 safe_syscall2(int, kill
, pid_t
, pid
, int, sig
)
962 safe_syscall2(int, tkill
, int, tid
, int, sig
)
963 safe_syscall3(int, tgkill
, int, tgid
, int, pid
, int, sig
)
964 safe_syscall3(ssize_t
, readv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
965 safe_syscall3(ssize_t
, writev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
966 safe_syscall5(ssize_t
, preadv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
967 unsigned long, pos_l
, unsigned long, pos_h
)
968 safe_syscall5(ssize_t
, pwritev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
969 unsigned long, pos_l
, unsigned long, pos_h
)
970 safe_syscall3(int, connect
, int, fd
, const struct sockaddr
*, addr
,
972 safe_syscall6(ssize_t
, sendto
, int, fd
, const void *, buf
, size_t, len
,
973 int, flags
, const struct sockaddr
*, addr
, socklen_t
, addrlen
)
974 safe_syscall6(ssize_t
, recvfrom
, int, fd
, void *, buf
, size_t, len
,
975 int, flags
, struct sockaddr
*, addr
, socklen_t
*, addrlen
)
976 safe_syscall3(ssize_t
, sendmsg
, int, fd
, const struct msghdr
*, msg
, int, flags
)
977 safe_syscall3(ssize_t
, recvmsg
, int, fd
, struct msghdr
*, msg
, int, flags
)
978 safe_syscall2(int, flock
, int, fd
, int, operation
)
979 safe_syscall4(int, rt_sigtimedwait
, const sigset_t
*, these
, siginfo_t
*, uinfo
,
980 const struct timespec
*, uts
, size_t, sigsetsize
)
981 safe_syscall4(int, accept4
, int, fd
, struct sockaddr
*, addr
, socklen_t
*, len
,
983 safe_syscall2(int, nanosleep
, const struct timespec
*, req
,
984 struct timespec
*, rem
)
985 #ifdef TARGET_NR_clock_nanosleep
986 safe_syscall4(int, clock_nanosleep
, const clockid_t
, clock
, int, flags
,
987 const struct timespec
*, req
, struct timespec
*, rem
)
990 safe_syscall4(int, msgsnd
, int, msgid
, const void *, msgp
, size_t, sz
,
992 safe_syscall5(int, msgrcv
, int, msgid
, void *, msgp
, size_t, sz
,
993 long, msgtype
, int, flags
)
994 safe_syscall4(int, semtimedop
, int, semid
, struct sembuf
*, tsops
,
995 unsigned, nsops
, const struct timespec
*, timeout
)
997 /* This host kernel architecture uses a single ipc syscall; fake up
998 * wrappers for the sub-operations to hide this implementation detail.
999 * Annoyingly we can't include linux/ipc.h to get the constant definitions
1000 * for the call parameter because some structs in there conflict with the
1001 * sys/ipc.h ones. So we just define them here, and rely on them being
1002 * the same for all host architectures.
1004 #define Q_SEMTIMEDOP 4
1007 #define Q_IPCCALL(VERSION, OP) ((VERSION) << 16 | (OP))
1009 safe_syscall6(int, ipc
, int, call
, long, first
, long, second
, long, third
,
1010 void *, ptr
, long, fifth
)
1011 static int safe_msgsnd(int msgid
, const void *msgp
, size_t sz
, int flags
)
1013 return safe_ipc(Q_IPCCALL(0, Q_MSGSND
), msgid
, sz
, flags
, (void *)msgp
, 0);
1015 static int safe_msgrcv(int msgid
, void *msgp
, size_t sz
, long type
, int flags
)
1017 return safe_ipc(Q_IPCCALL(1, Q_MSGRCV
), msgid
, sz
, flags
, msgp
, type
);
1019 static int safe_semtimedop(int semid
, struct sembuf
*tsops
, unsigned nsops
,
1020 const struct timespec
*timeout
)
1022 return safe_ipc(Q_IPCCALL(0, Q_SEMTIMEDOP
), semid
, nsops
, 0, tsops
,
1026 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1027 safe_syscall5(int, mq_timedsend
, int, mqdes
, const char *, msg_ptr
,
1028 size_t, len
, unsigned, prio
, const struct timespec
*, timeout
)
1029 safe_syscall5(int, mq_timedreceive
, int, mqdes
, char *, msg_ptr
,
1030 size_t, len
, unsigned *, prio
, const struct timespec
*, timeout
)
1032 /* We do ioctl like this rather than via safe_syscall3 to preserve the
1033 * "third argument might be integer or pointer or not present" behaviour of
1034 * the libc function.
1036 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
1037 /* Similarly for fcntl. Note that callers must always:
1038 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
1039 * use the flock64 struct rather than unsuffixed flock
1040 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
1043 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
1045 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
1048 static inline int host_to_target_sock_type(int host_type
)
1052 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
1054 target_type
= TARGET_SOCK_DGRAM
;
1057 target_type
= TARGET_SOCK_STREAM
;
1060 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
1064 #if defined(SOCK_CLOEXEC)
1065 if (host_type
& SOCK_CLOEXEC
) {
1066 target_type
|= TARGET_SOCK_CLOEXEC
;
1070 #if defined(SOCK_NONBLOCK)
1071 if (host_type
& SOCK_NONBLOCK
) {
1072 target_type
|= TARGET_SOCK_NONBLOCK
;
1079 static abi_ulong target_brk
;
1080 static abi_ulong target_original_brk
;
1081 static abi_ulong brk_page
;
1083 void target_set_brk(abi_ulong new_brk
)
1085 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
1086 brk_page
= HOST_PAGE_ALIGN(target_brk
);
1089 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
1090 #define DEBUGF_BRK(message, args...)
1092 /* do_brk() must return target values and target errnos. */
1093 abi_long
do_brk(abi_ulong new_brk
)
1095 abi_long mapped_addr
;
1096 abi_ulong new_alloc_size
;
1098 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
1101 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
1104 if (new_brk
< target_original_brk
) {
1105 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
1110 /* If the new brk is less than the highest page reserved to the
1111 * target heap allocation, set it and we're almost done... */
1112 if (new_brk
<= brk_page
) {
1113 /* Heap contents are initialized to zero, as for anonymous
1115 if (new_brk
> target_brk
) {
1116 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
1118 target_brk
= new_brk
;
1119 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
1123 /* We need to allocate more memory after the brk... Note that
1124 * we don't use MAP_FIXED because that will map over the top of
1125 * any existing mapping (like the one with the host libc or qemu
1126 * itself); instead we treat "mapped but at wrong address" as
1127 * a failure and unmap again.
1129 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
1130 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
1131 PROT_READ
|PROT_WRITE
,
1132 MAP_ANON
|MAP_PRIVATE
, 0, 0));
1134 if (mapped_addr
== brk_page
) {
1135 /* Heap contents are initialized to zero, as for anonymous
1136 * mapped pages. Technically the new pages are already
1137 * initialized to zero since they *are* anonymous mapped
1138 * pages, however we have to take care with the contents that
1139 * come from the remaining part of the previous page: it may
1140 * contains garbage data due to a previous heap usage (grown
1141 * then shrunken). */
1142 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
1144 target_brk
= new_brk
;
1145 brk_page
= HOST_PAGE_ALIGN(target_brk
);
1146 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
1149 } else if (mapped_addr
!= -1) {
1150 /* Mapped but at wrong address, meaning there wasn't actually
1151 * enough space for this brk.
1153 target_munmap(mapped_addr
, new_alloc_size
);
1155 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
1158 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
1161 #if defined(TARGET_ALPHA)
1162 /* We (partially) emulate OSF/1 on Alpha, which requires we
1163 return a proper errno, not an unchanged brk value. */
1164 return -TARGET_ENOMEM
;
1166 /* For everything else, return the previous break. */
1170 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
1171 abi_ulong target_fds_addr
,
1175 abi_ulong b
, *target_fds
;
1177 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
1178 if (!(target_fds
= lock_user(VERIFY_READ
,
1180 sizeof(abi_ulong
) * nw
,
1182 return -TARGET_EFAULT
;
1186 for (i
= 0; i
< nw
; i
++) {
1187 /* grab the abi_ulong */
1188 __get_user(b
, &target_fds
[i
]);
1189 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
1190 /* check the bit inside the abi_ulong */
1197 unlock_user(target_fds
, target_fds_addr
, 0);
1202 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
1203 abi_ulong target_fds_addr
,
1206 if (target_fds_addr
) {
1207 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
1208 return -TARGET_EFAULT
;
1216 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
1222 abi_ulong
*target_fds
;
1224 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
1225 if (!(target_fds
= lock_user(VERIFY_WRITE
,
1227 sizeof(abi_ulong
) * nw
,
1229 return -TARGET_EFAULT
;
1232 for (i
= 0; i
< nw
; i
++) {
1234 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
1235 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
1238 __put_user(v
, &target_fds
[i
]);
1241 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
1246 #if defined(__alpha__)
1247 #define HOST_HZ 1024
1252 static inline abi_long
host_to_target_clock_t(long ticks
)
1254 #if HOST_HZ == TARGET_HZ
1257 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
1261 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
1262 const struct rusage
*rusage
)
1264 struct target_rusage
*target_rusage
;
1266 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
1267 return -TARGET_EFAULT
;
1268 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
1269 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
1270 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
1271 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
1272 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
1273 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
1274 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
1275 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
1276 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
1277 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
1278 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
1279 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
1280 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
1281 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
1282 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
1283 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
1284 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
1285 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
1286 unlock_user_struct(target_rusage
, target_addr
, 1);
1291 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
1293 abi_ulong target_rlim_swap
;
1296 target_rlim_swap
= tswapal(target_rlim
);
1297 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
1298 return RLIM_INFINITY
;
1300 result
= target_rlim_swap
;
1301 if (target_rlim_swap
!= (rlim_t
)result
)
1302 return RLIM_INFINITY
;
1307 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
1309 abi_ulong target_rlim_swap
;
1312 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
1313 target_rlim_swap
= TARGET_RLIM_INFINITY
;
1315 target_rlim_swap
= rlim
;
1316 result
= tswapal(target_rlim_swap
);
1321 static inline int target_to_host_resource(int code
)
1324 case TARGET_RLIMIT_AS
:
1326 case TARGET_RLIMIT_CORE
:
1328 case TARGET_RLIMIT_CPU
:
1330 case TARGET_RLIMIT_DATA
:
1332 case TARGET_RLIMIT_FSIZE
:
1333 return RLIMIT_FSIZE
;
1334 case TARGET_RLIMIT_LOCKS
:
1335 return RLIMIT_LOCKS
;
1336 case TARGET_RLIMIT_MEMLOCK
:
1337 return RLIMIT_MEMLOCK
;
1338 case TARGET_RLIMIT_MSGQUEUE
:
1339 return RLIMIT_MSGQUEUE
;
1340 case TARGET_RLIMIT_NICE
:
1342 case TARGET_RLIMIT_NOFILE
:
1343 return RLIMIT_NOFILE
;
1344 case TARGET_RLIMIT_NPROC
:
1345 return RLIMIT_NPROC
;
1346 case TARGET_RLIMIT_RSS
:
1348 case TARGET_RLIMIT_RTPRIO
:
1349 return RLIMIT_RTPRIO
;
1350 case TARGET_RLIMIT_SIGPENDING
:
1351 return RLIMIT_SIGPENDING
;
1352 case TARGET_RLIMIT_STACK
:
1353 return RLIMIT_STACK
;
1359 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1360 abi_ulong target_tv_addr
)
1362 struct target_timeval
*target_tv
;
1364 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1))
1365 return -TARGET_EFAULT
;
1367 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1368 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1370 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1375 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1376 const struct timeval
*tv
)
1378 struct target_timeval
*target_tv
;
1380 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0))
1381 return -TARGET_EFAULT
;
1383 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1384 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1386 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1391 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1392 abi_ulong target_tz_addr
)
1394 struct target_timezone
*target_tz
;
1396 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1397 return -TARGET_EFAULT
;
1400 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1401 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1403 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1408 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1411 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1412 abi_ulong target_mq_attr_addr
)
1414 struct target_mq_attr
*target_mq_attr
;
1416 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1417 target_mq_attr_addr
, 1))
1418 return -TARGET_EFAULT
;
1420 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1421 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1422 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1423 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1425 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1430 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1431 const struct mq_attr
*attr
)
1433 struct target_mq_attr
*target_mq_attr
;
1435 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1436 target_mq_attr_addr
, 0))
1437 return -TARGET_EFAULT
;
1439 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1440 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1441 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1442 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1444 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1450 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1451 /* do_select() must return target values and target errnos. */
1452 static abi_long
do_select(int n
,
1453 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1454 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1456 fd_set rfds
, wfds
, efds
;
1457 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1459 struct timespec ts
, *ts_ptr
;
1462 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1466 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1470 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1475 if (target_tv_addr
) {
1476 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1477 return -TARGET_EFAULT
;
1478 ts
.tv_sec
= tv
.tv_sec
;
1479 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1485 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1488 if (!is_error(ret
)) {
1489 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1490 return -TARGET_EFAULT
;
1491 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1492 return -TARGET_EFAULT
;
1493 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1494 return -TARGET_EFAULT
;
1496 if (target_tv_addr
) {
1497 tv
.tv_sec
= ts
.tv_sec
;
1498 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1499 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1500 return -TARGET_EFAULT
;
1508 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1509 static abi_long
do_old_select(abi_ulong arg1
)
1511 struct target_sel_arg_struct
*sel
;
1512 abi_ulong inp
, outp
, exp
, tvp
;
1515 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1)) {
1516 return -TARGET_EFAULT
;
1519 nsel
= tswapal(sel
->n
);
1520 inp
= tswapal(sel
->inp
);
1521 outp
= tswapal(sel
->outp
);
1522 exp
= tswapal(sel
->exp
);
1523 tvp
= tswapal(sel
->tvp
);
1525 unlock_user_struct(sel
, arg1
, 0);
1527 return do_select(nsel
, inp
, outp
, exp
, tvp
);
1532 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1535 return pipe2(host_pipe
, flags
);
1541 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1542 int flags
, int is_pipe2
)
1546 ret
= flags
? do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1549 return get_errno(ret
);
1551 /* Several targets have special calling conventions for the original
1552 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1554 #if defined(TARGET_ALPHA)
1555 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1556 return host_pipe
[0];
1557 #elif defined(TARGET_MIPS)
1558 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1559 return host_pipe
[0];
1560 #elif defined(TARGET_SH4)
1561 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1562 return host_pipe
[0];
1563 #elif defined(TARGET_SPARC)
1564 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1565 return host_pipe
[0];
1569 if (put_user_s32(host_pipe
[0], pipedes
)
1570 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1571 return -TARGET_EFAULT
;
1572 return get_errno(ret
);
1575 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1576 abi_ulong target_addr
,
1579 struct target_ip_mreqn
*target_smreqn
;
1581 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1583 return -TARGET_EFAULT
;
1584 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1585 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1586 if (len
== sizeof(struct target_ip_mreqn
))
1587 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1588 unlock_user(target_smreqn
, target_addr
, 0);
1593 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1594 abi_ulong target_addr
,
1597 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1598 sa_family_t sa_family
;
1599 struct target_sockaddr
*target_saddr
;
1601 if (fd_trans_target_to_host_addr(fd
)) {
1602 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1605 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1607 return -TARGET_EFAULT
;
1609 sa_family
= tswap16(target_saddr
->sa_family
);
1611 /* Oops. The caller might send a incomplete sun_path; sun_path
1612 * must be terminated by \0 (see the manual page), but
1613 * unfortunately it is quite common to specify sockaddr_un
1614 * length as "strlen(x->sun_path)" while it should be
1615 * "strlen(...) + 1". We'll fix that here if needed.
1616 * Linux kernel has a similar feature.
1619 if (sa_family
== AF_UNIX
) {
1620 if (len
< unix_maxlen
&& len
> 0) {
1621 char *cp
= (char*)target_saddr
;
1623 if ( cp
[len
-1] && !cp
[len
] )
1626 if (len
> unix_maxlen
)
1630 memcpy(addr
, target_saddr
, len
);
1631 addr
->sa_family
= sa_family
;
1632 if (sa_family
== AF_NETLINK
) {
1633 struct sockaddr_nl
*nladdr
;
1635 nladdr
= (struct sockaddr_nl
*)addr
;
1636 nladdr
->nl_pid
= tswap32(nladdr
->nl_pid
);
1637 nladdr
->nl_groups
= tswap32(nladdr
->nl_groups
);
1638 } else if (sa_family
== AF_PACKET
) {
1639 struct target_sockaddr_ll
*lladdr
;
1641 lladdr
= (struct target_sockaddr_ll
*)addr
;
1642 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1643 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1645 unlock_user(target_saddr
, target_addr
, 0);
1650 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1651 struct sockaddr
*addr
,
1654 struct target_sockaddr
*target_saddr
;
1661 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1663 return -TARGET_EFAULT
;
1664 memcpy(target_saddr
, addr
, len
);
1665 if (len
>= offsetof(struct target_sockaddr
, sa_family
) +
1666 sizeof(target_saddr
->sa_family
)) {
1667 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1669 if (addr
->sa_family
== AF_NETLINK
&& len
>= sizeof(struct sockaddr_nl
)) {
1670 struct sockaddr_nl
*target_nl
= (struct sockaddr_nl
*)target_saddr
;
1671 target_nl
->nl_pid
= tswap32(target_nl
->nl_pid
);
1672 target_nl
->nl_groups
= tswap32(target_nl
->nl_groups
);
1673 } else if (addr
->sa_family
== AF_PACKET
) {
1674 struct sockaddr_ll
*target_ll
= (struct sockaddr_ll
*)target_saddr
;
1675 target_ll
->sll_ifindex
= tswap32(target_ll
->sll_ifindex
);
1676 target_ll
->sll_hatype
= tswap16(target_ll
->sll_hatype
);
1677 } else if (addr
->sa_family
== AF_INET6
&&
1678 len
>= sizeof(struct target_sockaddr_in6
)) {
1679 struct target_sockaddr_in6
*target_in6
=
1680 (struct target_sockaddr_in6
*)target_saddr
;
1681 target_in6
->sin6_scope_id
= tswap16(target_in6
->sin6_scope_id
);
1683 unlock_user(target_saddr
, target_addr
, len
);
1688 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1689 struct target_msghdr
*target_msgh
)
1691 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1692 abi_long msg_controllen
;
1693 abi_ulong target_cmsg_addr
;
1694 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1695 socklen_t space
= 0;
1697 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1698 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1700 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1701 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1702 target_cmsg_start
= target_cmsg
;
1704 return -TARGET_EFAULT
;
1706 while (cmsg
&& target_cmsg
) {
1707 void *data
= CMSG_DATA(cmsg
);
1708 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1710 int len
= tswapal(target_cmsg
->cmsg_len
)
1711 - sizeof(struct target_cmsghdr
);
1713 space
+= CMSG_SPACE(len
);
1714 if (space
> msgh
->msg_controllen
) {
1715 space
-= CMSG_SPACE(len
);
1716 /* This is a QEMU bug, since we allocated the payload
1717 * area ourselves (unlike overflow in host-to-target
1718 * conversion, which is just the guest giving us a buffer
1719 * that's too small). It can't happen for the payload types
1720 * we currently support; if it becomes an issue in future
1721 * we would need to improve our allocation strategy to
1722 * something more intelligent than "twice the size of the
1723 * target buffer we're reading from".
1725 gemu_log("Host cmsg overflow\n");
1729 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1730 cmsg
->cmsg_level
= SOL_SOCKET
;
1732 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1734 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1735 cmsg
->cmsg_len
= CMSG_LEN(len
);
1737 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1738 int *fd
= (int *)data
;
1739 int *target_fd
= (int *)target_data
;
1740 int i
, numfds
= len
/ sizeof(int);
1742 for (i
= 0; i
< numfds
; i
++) {
1743 __get_user(fd
[i
], target_fd
+ i
);
1745 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1746 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1747 struct ucred
*cred
= (struct ucred
*)data
;
1748 struct target_ucred
*target_cred
=
1749 (struct target_ucred
*)target_data
;
1751 __get_user(cred
->pid
, &target_cred
->pid
);
1752 __get_user(cred
->uid
, &target_cred
->uid
);
1753 __get_user(cred
->gid
, &target_cred
->gid
);
1755 gemu_log("Unsupported ancillary data: %d/%d\n",
1756 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1757 memcpy(data
, target_data
, len
);
1760 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1761 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1764 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1766 msgh
->msg_controllen
= space
;
1770 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1771 struct msghdr
*msgh
)
1773 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1774 abi_long msg_controllen
;
1775 abi_ulong target_cmsg_addr
;
1776 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1777 socklen_t space
= 0;
1779 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1780 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1782 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1783 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1784 target_cmsg_start
= target_cmsg
;
1786 return -TARGET_EFAULT
;
1788 while (cmsg
&& target_cmsg
) {
1789 void *data
= CMSG_DATA(cmsg
);
1790 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1792 int len
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
1793 int tgt_len
, tgt_space
;
1795 /* We never copy a half-header but may copy half-data;
1796 * this is Linux's behaviour in put_cmsg(). Note that
1797 * truncation here is a guest problem (which we report
1798 * to the guest via the CTRUNC bit), unlike truncation
1799 * in target_to_host_cmsg, which is a QEMU bug.
1801 if (msg_controllen
< sizeof(struct target_cmsghdr
)) {
1802 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1806 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1807 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1809 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1811 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1813 /* Payload types which need a different size of payload on
1814 * the target must adjust tgt_len here.
1816 switch (cmsg
->cmsg_level
) {
1818 switch (cmsg
->cmsg_type
) {
1820 tgt_len
= sizeof(struct target_timeval
);
1830 if (msg_controllen
< TARGET_CMSG_LEN(tgt_len
)) {
1831 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1832 tgt_len
= msg_controllen
- sizeof(struct target_cmsghdr
);
1835 /* We must now copy-and-convert len bytes of payload
1836 * into tgt_len bytes of destination space. Bear in mind
1837 * that in both source and destination we may be dealing
1838 * with a truncated value!
1840 switch (cmsg
->cmsg_level
) {
1842 switch (cmsg
->cmsg_type
) {
1845 int *fd
= (int *)data
;
1846 int *target_fd
= (int *)target_data
;
1847 int i
, numfds
= tgt_len
/ sizeof(int);
1849 for (i
= 0; i
< numfds
; i
++) {
1850 __put_user(fd
[i
], target_fd
+ i
);
1856 struct timeval
*tv
= (struct timeval
*)data
;
1857 struct target_timeval
*target_tv
=
1858 (struct target_timeval
*)target_data
;
1860 if (len
!= sizeof(struct timeval
) ||
1861 tgt_len
!= sizeof(struct target_timeval
)) {
1865 /* copy struct timeval to target */
1866 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1867 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1870 case SCM_CREDENTIALS
:
1872 struct ucred
*cred
= (struct ucred
*)data
;
1873 struct target_ucred
*target_cred
=
1874 (struct target_ucred
*)target_data
;
1876 __put_user(cred
->pid
, &target_cred
->pid
);
1877 __put_user(cred
->uid
, &target_cred
->uid
);
1878 __put_user(cred
->gid
, &target_cred
->gid
);
1887 switch (cmsg
->cmsg_type
) {
1890 uint32_t *v
= (uint32_t *)data
;
1891 uint32_t *t_int
= (uint32_t *)target_data
;
1893 if (len
!= sizeof(uint32_t) ||
1894 tgt_len
!= sizeof(uint32_t)) {
1897 __put_user(*v
, t_int
);
1903 struct sock_extended_err ee
;
1904 struct sockaddr_in offender
;
1906 struct errhdr_t
*errh
= (struct errhdr_t
*)data
;
1907 struct errhdr_t
*target_errh
=
1908 (struct errhdr_t
*)target_data
;
1910 if (len
!= sizeof(struct errhdr_t
) ||
1911 tgt_len
!= sizeof(struct errhdr_t
)) {
1914 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1915 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1916 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1917 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1918 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1919 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1920 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1921 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1922 (void *) &errh
->offender
, sizeof(errh
->offender
));
1931 switch (cmsg
->cmsg_type
) {
1934 uint32_t *v
= (uint32_t *)data
;
1935 uint32_t *t_int
= (uint32_t *)target_data
;
1937 if (len
!= sizeof(uint32_t) ||
1938 tgt_len
!= sizeof(uint32_t)) {
1941 __put_user(*v
, t_int
);
1947 struct sock_extended_err ee
;
1948 struct sockaddr_in6 offender
;
1950 struct errhdr6_t
*errh
= (struct errhdr6_t
*)data
;
1951 struct errhdr6_t
*target_errh
=
1952 (struct errhdr6_t
*)target_data
;
1954 if (len
!= sizeof(struct errhdr6_t
) ||
1955 tgt_len
!= sizeof(struct errhdr6_t
)) {
1958 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1959 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1960 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1961 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1962 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1963 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1964 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1965 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1966 (void *) &errh
->offender
, sizeof(errh
->offender
));
1976 gemu_log("Unsupported ancillary data: %d/%d\n",
1977 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1978 memcpy(target_data
, data
, MIN(len
, tgt_len
));
1979 if (tgt_len
> len
) {
1980 memset(target_data
+ len
, 0, tgt_len
- len
);
1984 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(tgt_len
));
1985 tgt_space
= TARGET_CMSG_SPACE(tgt_len
);
1986 if (msg_controllen
< tgt_space
) {
1987 tgt_space
= msg_controllen
;
1989 msg_controllen
-= tgt_space
;
1991 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1992 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1995 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1997 target_msgh
->msg_controllen
= tswapal(space
);
2001 static void tswap_nlmsghdr(struct nlmsghdr
*nlh
)
2003 nlh
->nlmsg_len
= tswap32(nlh
->nlmsg_len
);
2004 nlh
->nlmsg_type
= tswap16(nlh
->nlmsg_type
);
2005 nlh
->nlmsg_flags
= tswap16(nlh
->nlmsg_flags
);
2006 nlh
->nlmsg_seq
= tswap32(nlh
->nlmsg_seq
);
2007 nlh
->nlmsg_pid
= tswap32(nlh
->nlmsg_pid
);
2010 static abi_long
host_to_target_for_each_nlmsg(struct nlmsghdr
*nlh
,
2012 abi_long (*host_to_target_nlmsg
)
2013 (struct nlmsghdr
*))
2018 while (len
> sizeof(struct nlmsghdr
)) {
2020 nlmsg_len
= nlh
->nlmsg_len
;
2021 if (nlmsg_len
< sizeof(struct nlmsghdr
) ||
2026 switch (nlh
->nlmsg_type
) {
2028 tswap_nlmsghdr(nlh
);
2034 struct nlmsgerr
*e
= NLMSG_DATA(nlh
);
2035 e
->error
= tswap32(e
->error
);
2036 tswap_nlmsghdr(&e
->msg
);
2037 tswap_nlmsghdr(nlh
);
2041 ret
= host_to_target_nlmsg(nlh
);
2043 tswap_nlmsghdr(nlh
);
2048 tswap_nlmsghdr(nlh
);
2049 len
-= NLMSG_ALIGN(nlmsg_len
);
2050 nlh
= (struct nlmsghdr
*)(((char*)nlh
) + NLMSG_ALIGN(nlmsg_len
));
2055 static abi_long
target_to_host_for_each_nlmsg(struct nlmsghdr
*nlh
,
2057 abi_long (*target_to_host_nlmsg
)
2058 (struct nlmsghdr
*))
2062 while (len
> sizeof(struct nlmsghdr
)) {
2063 if (tswap32(nlh
->nlmsg_len
) < sizeof(struct nlmsghdr
) ||
2064 tswap32(nlh
->nlmsg_len
) > len
) {
2067 tswap_nlmsghdr(nlh
);
2068 switch (nlh
->nlmsg_type
) {
2075 struct nlmsgerr
*e
= NLMSG_DATA(nlh
);
2076 e
->error
= tswap32(e
->error
);
2077 tswap_nlmsghdr(&e
->msg
);
2081 ret
= target_to_host_nlmsg(nlh
);
2086 len
-= NLMSG_ALIGN(nlh
->nlmsg_len
);
2087 nlh
= (struct nlmsghdr
*)(((char *)nlh
) + NLMSG_ALIGN(nlh
->nlmsg_len
));
2092 #ifdef CONFIG_RTNETLINK
2093 static abi_long
host_to_target_for_each_nlattr(struct nlattr
*nlattr
,
2094 size_t len
, void *context
,
2095 abi_long (*host_to_target_nlattr
)
2099 unsigned short nla_len
;
2102 while (len
> sizeof(struct nlattr
)) {
2103 nla_len
= nlattr
->nla_len
;
2104 if (nla_len
< sizeof(struct nlattr
) ||
2108 ret
= host_to_target_nlattr(nlattr
, context
);
2109 nlattr
->nla_len
= tswap16(nlattr
->nla_len
);
2110 nlattr
->nla_type
= tswap16(nlattr
->nla_type
);
2114 len
-= NLA_ALIGN(nla_len
);
2115 nlattr
= (struct nlattr
*)(((char *)nlattr
) + NLA_ALIGN(nla_len
));
2120 static abi_long
host_to_target_for_each_rtattr(struct rtattr
*rtattr
,
2122 abi_long (*host_to_target_rtattr
)
2125 unsigned short rta_len
;
2128 while (len
> sizeof(struct rtattr
)) {
2129 rta_len
= rtattr
->rta_len
;
2130 if (rta_len
< sizeof(struct rtattr
) ||
2134 ret
= host_to_target_rtattr(rtattr
);
2135 rtattr
->rta_len
= tswap16(rtattr
->rta_len
);
2136 rtattr
->rta_type
= tswap16(rtattr
->rta_type
);
2140 len
-= RTA_ALIGN(rta_len
);
2141 rtattr
= (struct rtattr
*)(((char *)rtattr
) + RTA_ALIGN(rta_len
));
2146 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
2148 static abi_long
host_to_target_data_bridge_nlattr(struct nlattr
*nlattr
,
2155 switch (nlattr
->nla_type
) {
2157 case QEMU_IFLA_BR_FDB_FLUSH
:
2160 case QEMU_IFLA_BR_GROUP_ADDR
:
2163 case QEMU_IFLA_BR_VLAN_FILTERING
:
2164 case QEMU_IFLA_BR_TOPOLOGY_CHANGE
:
2165 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED
:
2166 case QEMU_IFLA_BR_MCAST_ROUTER
:
2167 case QEMU_IFLA_BR_MCAST_SNOOPING
:
2168 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR
:
2169 case QEMU_IFLA_BR_MCAST_QUERIER
:
2170 case QEMU_IFLA_BR_NF_CALL_IPTABLES
:
2171 case QEMU_IFLA_BR_NF_CALL_IP6TABLES
:
2172 case QEMU_IFLA_BR_NF_CALL_ARPTABLES
:
2175 case QEMU_IFLA_BR_PRIORITY
:
2176 case QEMU_IFLA_BR_VLAN_PROTOCOL
:
2177 case QEMU_IFLA_BR_GROUP_FWD_MASK
:
2178 case QEMU_IFLA_BR_ROOT_PORT
:
2179 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID
:
2180 u16
= NLA_DATA(nlattr
);
2181 *u16
= tswap16(*u16
);
2184 case QEMU_IFLA_BR_FORWARD_DELAY
:
2185 case QEMU_IFLA_BR_HELLO_TIME
:
2186 case QEMU_IFLA_BR_MAX_AGE
:
2187 case QEMU_IFLA_BR_AGEING_TIME
:
2188 case QEMU_IFLA_BR_STP_STATE
:
2189 case QEMU_IFLA_BR_ROOT_PATH_COST
:
2190 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY
:
2191 case QEMU_IFLA_BR_MCAST_HASH_MAX
:
2192 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT
:
2193 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT
:
2194 u32
= NLA_DATA(nlattr
);
2195 *u32
= tswap32(*u32
);
2198 case QEMU_IFLA_BR_HELLO_TIMER
:
2199 case QEMU_IFLA_BR_TCN_TIMER
:
2200 case QEMU_IFLA_BR_GC_TIMER
:
2201 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER
:
2202 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL
:
2203 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL
:
2204 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL
:
2205 case QEMU_IFLA_BR_MCAST_QUERY_INTVL
:
2206 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL
:
2207 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL
:
2208 u64
= NLA_DATA(nlattr
);
2209 *u64
= tswap64(*u64
);
2211 /* ifla_bridge_id: uin8_t[] */
2212 case QEMU_IFLA_BR_ROOT_ID
:
2213 case QEMU_IFLA_BR_BRIDGE_ID
:
2216 gemu_log("Unknown QEMU_IFLA_BR type %d\n", nlattr
->nla_type
);
2222 static abi_long
host_to_target_slave_data_bridge_nlattr(struct nlattr
*nlattr
,
2229 switch (nlattr
->nla_type
) {
2231 case QEMU_IFLA_BRPORT_STATE
:
2232 case QEMU_IFLA_BRPORT_MODE
:
2233 case QEMU_IFLA_BRPORT_GUARD
:
2234 case QEMU_IFLA_BRPORT_PROTECT
:
2235 case QEMU_IFLA_BRPORT_FAST_LEAVE
:
2236 case QEMU_IFLA_BRPORT_LEARNING
:
2237 case QEMU_IFLA_BRPORT_UNICAST_FLOOD
:
2238 case QEMU_IFLA_BRPORT_PROXYARP
:
2239 case QEMU_IFLA_BRPORT_LEARNING_SYNC
:
2240 case QEMU_IFLA_BRPORT_PROXYARP_WIFI
:
2241 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK
:
2242 case QEMU_IFLA_BRPORT_CONFIG_PENDING
:
2243 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER
:
2246 case QEMU_IFLA_BRPORT_PRIORITY
:
2247 case QEMU_IFLA_BRPORT_DESIGNATED_PORT
:
2248 case QEMU_IFLA_BRPORT_DESIGNATED_COST
:
2249 case QEMU_IFLA_BRPORT_ID
:
2250 case QEMU_IFLA_BRPORT_NO
:
2251 u16
= NLA_DATA(nlattr
);
2252 *u16
= tswap16(*u16
);
2255 case QEMU_IFLA_BRPORT_COST
:
2256 u32
= NLA_DATA(nlattr
);
2257 *u32
= tswap32(*u32
);
2260 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER
:
2261 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER
:
2262 case QEMU_IFLA_BRPORT_HOLD_TIMER
:
2263 u64
= NLA_DATA(nlattr
);
2264 *u64
= tswap64(*u64
);
2266 /* ifla_bridge_id: uint8_t[] */
2267 case QEMU_IFLA_BRPORT_ROOT_ID
:
2268 case QEMU_IFLA_BRPORT_BRIDGE_ID
:
2271 gemu_log("Unknown QEMU_IFLA_BRPORT type %d\n", nlattr
->nla_type
);
2277 struct linkinfo_context
{
2284 static abi_long
host_to_target_data_linkinfo_nlattr(struct nlattr
*nlattr
,
2287 struct linkinfo_context
*li_context
= context
;
2289 switch (nlattr
->nla_type
) {
2291 case QEMU_IFLA_INFO_KIND
:
2292 li_context
->name
= NLA_DATA(nlattr
);
2293 li_context
->len
= nlattr
->nla_len
- NLA_HDRLEN
;
2295 case QEMU_IFLA_INFO_SLAVE_KIND
:
2296 li_context
->slave_name
= NLA_DATA(nlattr
);
2297 li_context
->slave_len
= nlattr
->nla_len
- NLA_HDRLEN
;
2300 case QEMU_IFLA_INFO_XSTATS
:
2301 /* FIXME: only used by CAN */
2304 case QEMU_IFLA_INFO_DATA
:
2305 if (strncmp(li_context
->name
, "bridge",
2306 li_context
->len
) == 0) {
2307 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
),
2310 host_to_target_data_bridge_nlattr
);
2312 gemu_log("Unknown QEMU_IFLA_INFO_KIND %s\n", li_context
->name
);
2315 case QEMU_IFLA_INFO_SLAVE_DATA
:
2316 if (strncmp(li_context
->slave_name
, "bridge",
2317 li_context
->slave_len
) == 0) {
2318 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
),
2321 host_to_target_slave_data_bridge_nlattr
);
2323 gemu_log("Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
2324 li_context
->slave_name
);
2328 gemu_log("Unknown host QEMU_IFLA_INFO type: %d\n", nlattr
->nla_type
);
2335 static abi_long
host_to_target_data_inet_nlattr(struct nlattr
*nlattr
,
2341 switch (nlattr
->nla_type
) {
2342 case QEMU_IFLA_INET_CONF
:
2343 u32
= NLA_DATA(nlattr
);
2344 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u32
);
2346 u32
[i
] = tswap32(u32
[i
]);
2350 gemu_log("Unknown host AF_INET type: %d\n", nlattr
->nla_type
);
2355 static abi_long
host_to_target_data_inet6_nlattr(struct nlattr
*nlattr
,
2360 struct ifla_cacheinfo
*ci
;
2363 switch (nlattr
->nla_type
) {
2365 case QEMU_IFLA_INET6_TOKEN
:
2368 case QEMU_IFLA_INET6_ADDR_GEN_MODE
:
2371 case QEMU_IFLA_INET6_FLAGS
:
2372 u32
= NLA_DATA(nlattr
);
2373 *u32
= tswap32(*u32
);
2376 case QEMU_IFLA_INET6_CONF
:
2377 u32
= NLA_DATA(nlattr
);
2378 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u32
);
2380 u32
[i
] = tswap32(u32
[i
]);
2383 /* ifla_cacheinfo */
2384 case QEMU_IFLA_INET6_CACHEINFO
:
2385 ci
= NLA_DATA(nlattr
);
2386 ci
->max_reasm_len
= tswap32(ci
->max_reasm_len
);
2387 ci
->tstamp
= tswap32(ci
->tstamp
);
2388 ci
->reachable_time
= tswap32(ci
->reachable_time
);
2389 ci
->retrans_time
= tswap32(ci
->retrans_time
);
2392 case QEMU_IFLA_INET6_STATS
:
2393 case QEMU_IFLA_INET6_ICMP6STATS
:
2394 u64
= NLA_DATA(nlattr
);
2395 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u64
);
2397 u64
[i
] = tswap64(u64
[i
]);
2401 gemu_log("Unknown host AF_INET6 type: %d\n", nlattr
->nla_type
);
2406 static abi_long
host_to_target_data_spec_nlattr(struct nlattr
*nlattr
,
2409 switch (nlattr
->nla_type
) {
2411 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
), nlattr
->nla_len
,
2413 host_to_target_data_inet_nlattr
);
2415 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
), nlattr
->nla_len
,
2417 host_to_target_data_inet6_nlattr
);
2419 gemu_log("Unknown host AF_SPEC type: %d\n", nlattr
->nla_type
);
2425 static abi_long
host_to_target_data_link_rtattr(struct rtattr
*rtattr
)
2428 struct rtnl_link_stats
*st
;
2429 struct rtnl_link_stats64
*st64
;
2430 struct rtnl_link_ifmap
*map
;
2431 struct linkinfo_context li_context
;
2433 switch (rtattr
->rta_type
) {
2435 case QEMU_IFLA_ADDRESS
:
2436 case QEMU_IFLA_BROADCAST
:
2438 case QEMU_IFLA_IFNAME
:
2439 case QEMU_IFLA_QDISC
:
2442 case QEMU_IFLA_OPERSTATE
:
2443 case QEMU_IFLA_LINKMODE
:
2444 case QEMU_IFLA_CARRIER
:
2445 case QEMU_IFLA_PROTO_DOWN
:
2449 case QEMU_IFLA_LINK
:
2450 case QEMU_IFLA_WEIGHT
:
2451 case QEMU_IFLA_TXQLEN
:
2452 case QEMU_IFLA_CARRIER_CHANGES
:
2453 case QEMU_IFLA_NUM_RX_QUEUES
:
2454 case QEMU_IFLA_NUM_TX_QUEUES
:
2455 case QEMU_IFLA_PROMISCUITY
:
2456 case QEMU_IFLA_EXT_MASK
:
2457 case QEMU_IFLA_LINK_NETNSID
:
2458 case QEMU_IFLA_GROUP
:
2459 case QEMU_IFLA_MASTER
:
2460 case QEMU_IFLA_NUM_VF
:
2461 case QEMU_IFLA_GSO_MAX_SEGS
:
2462 case QEMU_IFLA_GSO_MAX_SIZE
:
2463 u32
= RTA_DATA(rtattr
);
2464 *u32
= tswap32(*u32
);
2466 /* struct rtnl_link_stats */
2467 case QEMU_IFLA_STATS
:
2468 st
= RTA_DATA(rtattr
);
2469 st
->rx_packets
= tswap32(st
->rx_packets
);
2470 st
->tx_packets
= tswap32(st
->tx_packets
);
2471 st
->rx_bytes
= tswap32(st
->rx_bytes
);
2472 st
->tx_bytes
= tswap32(st
->tx_bytes
);
2473 st
->rx_errors
= tswap32(st
->rx_errors
);
2474 st
->tx_errors
= tswap32(st
->tx_errors
);
2475 st
->rx_dropped
= tswap32(st
->rx_dropped
);
2476 st
->tx_dropped
= tswap32(st
->tx_dropped
);
2477 st
->multicast
= tswap32(st
->multicast
);
2478 st
->collisions
= tswap32(st
->collisions
);
2480 /* detailed rx_errors: */
2481 st
->rx_length_errors
= tswap32(st
->rx_length_errors
);
2482 st
->rx_over_errors
= tswap32(st
->rx_over_errors
);
2483 st
->rx_crc_errors
= tswap32(st
->rx_crc_errors
);
2484 st
->rx_frame_errors
= tswap32(st
->rx_frame_errors
);
2485 st
->rx_fifo_errors
= tswap32(st
->rx_fifo_errors
);
2486 st
->rx_missed_errors
= tswap32(st
->rx_missed_errors
);
2488 /* detailed tx_errors */
2489 st
->tx_aborted_errors
= tswap32(st
->tx_aborted_errors
);
2490 st
->tx_carrier_errors
= tswap32(st
->tx_carrier_errors
);
2491 st
->tx_fifo_errors
= tswap32(st
->tx_fifo_errors
);
2492 st
->tx_heartbeat_errors
= tswap32(st
->tx_heartbeat_errors
);
2493 st
->tx_window_errors
= tswap32(st
->tx_window_errors
);
2496 st
->rx_compressed
= tswap32(st
->rx_compressed
);
2497 st
->tx_compressed
= tswap32(st
->tx_compressed
);
2499 /* struct rtnl_link_stats64 */
2500 case QEMU_IFLA_STATS64
:
2501 st64
= RTA_DATA(rtattr
);
2502 st64
->rx_packets
= tswap64(st64
->rx_packets
);
2503 st64
->tx_packets
= tswap64(st64
->tx_packets
);
2504 st64
->rx_bytes
= tswap64(st64
->rx_bytes
);
2505 st64
->tx_bytes
= tswap64(st64
->tx_bytes
);
2506 st64
->rx_errors
= tswap64(st64
->rx_errors
);
2507 st64
->tx_errors
= tswap64(st64
->tx_errors
);
2508 st64
->rx_dropped
= tswap64(st64
->rx_dropped
);
2509 st64
->tx_dropped
= tswap64(st64
->tx_dropped
);
2510 st64
->multicast
= tswap64(st64
->multicast
);
2511 st64
->collisions
= tswap64(st64
->collisions
);
2513 /* detailed rx_errors: */
2514 st64
->rx_length_errors
= tswap64(st64
->rx_length_errors
);
2515 st64
->rx_over_errors
= tswap64(st64
->rx_over_errors
);
2516 st64
->rx_crc_errors
= tswap64(st64
->rx_crc_errors
);
2517 st64
->rx_frame_errors
= tswap64(st64
->rx_frame_errors
);
2518 st64
->rx_fifo_errors
= tswap64(st64
->rx_fifo_errors
);
2519 st64
->rx_missed_errors
= tswap64(st64
->rx_missed_errors
);
2521 /* detailed tx_errors */
2522 st64
->tx_aborted_errors
= tswap64(st64
->tx_aborted_errors
);
2523 st64
->tx_carrier_errors
= tswap64(st64
->tx_carrier_errors
);
2524 st64
->tx_fifo_errors
= tswap64(st64
->tx_fifo_errors
);
2525 st64
->tx_heartbeat_errors
= tswap64(st64
->tx_heartbeat_errors
);
2526 st64
->tx_window_errors
= tswap64(st64
->tx_window_errors
);
2529 st64
->rx_compressed
= tswap64(st64
->rx_compressed
);
2530 st64
->tx_compressed
= tswap64(st64
->tx_compressed
);
2532 /* struct rtnl_link_ifmap */
2534 map
= RTA_DATA(rtattr
);
2535 map
->mem_start
= tswap64(map
->mem_start
);
2536 map
->mem_end
= tswap64(map
->mem_end
);
2537 map
->base_addr
= tswap64(map
->base_addr
);
2538 map
->irq
= tswap16(map
->irq
);
2541 case QEMU_IFLA_LINKINFO
:
2542 memset(&li_context
, 0, sizeof(li_context
));
2543 return host_to_target_for_each_nlattr(RTA_DATA(rtattr
), rtattr
->rta_len
,
2545 host_to_target_data_linkinfo_nlattr
);
2546 case QEMU_IFLA_AF_SPEC
:
2547 return host_to_target_for_each_nlattr(RTA_DATA(rtattr
), rtattr
->rta_len
,
2549 host_to_target_data_spec_nlattr
);
2551 gemu_log("Unknown host QEMU_IFLA type: %d\n", rtattr
->rta_type
);
2557 static abi_long
host_to_target_data_addr_rtattr(struct rtattr
*rtattr
)
2560 struct ifa_cacheinfo
*ci
;
2562 switch (rtattr
->rta_type
) {
2563 /* binary: depends on family type */
2573 u32
= RTA_DATA(rtattr
);
2574 *u32
= tswap32(*u32
);
2576 /* struct ifa_cacheinfo */
2578 ci
= RTA_DATA(rtattr
);
2579 ci
->ifa_prefered
= tswap32(ci
->ifa_prefered
);
2580 ci
->ifa_valid
= tswap32(ci
->ifa_valid
);
2581 ci
->cstamp
= tswap32(ci
->cstamp
);
2582 ci
->tstamp
= tswap32(ci
->tstamp
);
2585 gemu_log("Unknown host IFA type: %d\n", rtattr
->rta_type
);
2591 static abi_long
host_to_target_data_route_rtattr(struct rtattr
*rtattr
)
2594 switch (rtattr
->rta_type
) {
2595 /* binary: depends on family type */
2604 u32
= RTA_DATA(rtattr
);
2605 *u32
= tswap32(*u32
);
2608 gemu_log("Unknown host RTA type: %d\n", rtattr
->rta_type
);
2614 static abi_long
host_to_target_link_rtattr(struct rtattr
*rtattr
,
2615 uint32_t rtattr_len
)
2617 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
2618 host_to_target_data_link_rtattr
);
2621 static abi_long
host_to_target_addr_rtattr(struct rtattr
*rtattr
,
2622 uint32_t rtattr_len
)
2624 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
2625 host_to_target_data_addr_rtattr
);
2628 static abi_long
host_to_target_route_rtattr(struct rtattr
*rtattr
,
2629 uint32_t rtattr_len
)
2631 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
2632 host_to_target_data_route_rtattr
);
2635 static abi_long
host_to_target_data_route(struct nlmsghdr
*nlh
)
2638 struct ifinfomsg
*ifi
;
2639 struct ifaddrmsg
*ifa
;
2642 nlmsg_len
= nlh
->nlmsg_len
;
2643 switch (nlh
->nlmsg_type
) {
2647 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifi
))) {
2648 ifi
= NLMSG_DATA(nlh
);
2649 ifi
->ifi_type
= tswap16(ifi
->ifi_type
);
2650 ifi
->ifi_index
= tswap32(ifi
->ifi_index
);
2651 ifi
->ifi_flags
= tswap32(ifi
->ifi_flags
);
2652 ifi
->ifi_change
= tswap32(ifi
->ifi_change
);
2653 host_to_target_link_rtattr(IFLA_RTA(ifi
),
2654 nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
2660 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifa
))) {
2661 ifa
= NLMSG_DATA(nlh
);
2662 ifa
->ifa_index
= tswap32(ifa
->ifa_index
);
2663 host_to_target_addr_rtattr(IFA_RTA(ifa
),
2664 nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
2670 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*rtm
))) {
2671 rtm
= NLMSG_DATA(nlh
);
2672 rtm
->rtm_flags
= tswap32(rtm
->rtm_flags
);
2673 host_to_target_route_rtattr(RTM_RTA(rtm
),
2674 nlmsg_len
- NLMSG_LENGTH(sizeof(*rtm
)));
2678 return -TARGET_EINVAL
;
2683 static inline abi_long
host_to_target_nlmsg_route(struct nlmsghdr
*nlh
,
2686 return host_to_target_for_each_nlmsg(nlh
, len
, host_to_target_data_route
);
2689 static abi_long
target_to_host_for_each_rtattr(struct rtattr
*rtattr
,
2691 abi_long (*target_to_host_rtattr
)
2696 while (len
>= sizeof(struct rtattr
)) {
2697 if (tswap16(rtattr
->rta_len
) < sizeof(struct rtattr
) ||
2698 tswap16(rtattr
->rta_len
) > len
) {
2701 rtattr
->rta_len
= tswap16(rtattr
->rta_len
);
2702 rtattr
->rta_type
= tswap16(rtattr
->rta_type
);
2703 ret
= target_to_host_rtattr(rtattr
);
2707 len
-= RTA_ALIGN(rtattr
->rta_len
);
2708 rtattr
= (struct rtattr
*)(((char *)rtattr
) +
2709 RTA_ALIGN(rtattr
->rta_len
));
2714 static abi_long
target_to_host_data_link_rtattr(struct rtattr
*rtattr
)
2716 switch (rtattr
->rta_type
) {
2718 gemu_log("Unknown target QEMU_IFLA type: %d\n", rtattr
->rta_type
);
2724 static abi_long
target_to_host_data_addr_rtattr(struct rtattr
*rtattr
)
2726 switch (rtattr
->rta_type
) {
2727 /* binary: depends on family type */
2732 gemu_log("Unknown target IFA type: %d\n", rtattr
->rta_type
);
2738 static abi_long
target_to_host_data_route_rtattr(struct rtattr
*rtattr
)
2741 switch (rtattr
->rta_type
) {
2742 /* binary: depends on family type */
2750 u32
= RTA_DATA(rtattr
);
2751 *u32
= tswap32(*u32
);
2754 gemu_log("Unknown target RTA type: %d\n", rtattr
->rta_type
);
2760 static void target_to_host_link_rtattr(struct rtattr
*rtattr
,
2761 uint32_t rtattr_len
)
2763 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
2764 target_to_host_data_link_rtattr
);
2767 static void target_to_host_addr_rtattr(struct rtattr
*rtattr
,
2768 uint32_t rtattr_len
)
2770 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
2771 target_to_host_data_addr_rtattr
);
2774 static void target_to_host_route_rtattr(struct rtattr
*rtattr
,
2775 uint32_t rtattr_len
)
2777 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
2778 target_to_host_data_route_rtattr
);
2781 static abi_long
target_to_host_data_route(struct nlmsghdr
*nlh
)
2783 struct ifinfomsg
*ifi
;
2784 struct ifaddrmsg
*ifa
;
2787 switch (nlh
->nlmsg_type
) {
2792 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifi
))) {
2793 ifi
= NLMSG_DATA(nlh
);
2794 ifi
->ifi_type
= tswap16(ifi
->ifi_type
);
2795 ifi
->ifi_index
= tswap32(ifi
->ifi_index
);
2796 ifi
->ifi_flags
= tswap32(ifi
->ifi_flags
);
2797 ifi
->ifi_change
= tswap32(ifi
->ifi_change
);
2798 target_to_host_link_rtattr(IFLA_RTA(ifi
), nlh
->nlmsg_len
-
2799 NLMSG_LENGTH(sizeof(*ifi
)));
2805 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifa
))) {
2806 ifa
= NLMSG_DATA(nlh
);
2807 ifa
->ifa_index
= tswap32(ifa
->ifa_index
);
2808 target_to_host_addr_rtattr(IFA_RTA(ifa
), nlh
->nlmsg_len
-
2809 NLMSG_LENGTH(sizeof(*ifa
)));
2816 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*rtm
))) {
2817 rtm
= NLMSG_DATA(nlh
);
2818 rtm
->rtm_flags
= tswap32(rtm
->rtm_flags
);
2819 target_to_host_route_rtattr(RTM_RTA(rtm
), nlh
->nlmsg_len
-
2820 NLMSG_LENGTH(sizeof(*rtm
)));
2824 return -TARGET_EOPNOTSUPP
;
2829 static abi_long
target_to_host_nlmsg_route(struct nlmsghdr
*nlh
, size_t len
)
2831 return target_to_host_for_each_nlmsg(nlh
, len
, target_to_host_data_route
);
2833 #endif /* CONFIG_RTNETLINK */
2835 static abi_long
host_to_target_data_audit(struct nlmsghdr
*nlh
)
2837 switch (nlh
->nlmsg_type
) {
2839 gemu_log("Unknown host audit message type %d\n",
2841 return -TARGET_EINVAL
;
2846 static inline abi_long
host_to_target_nlmsg_audit(struct nlmsghdr
*nlh
,
2849 return host_to_target_for_each_nlmsg(nlh
, len
, host_to_target_data_audit
);
2852 static abi_long
target_to_host_data_audit(struct nlmsghdr
*nlh
)
2854 switch (nlh
->nlmsg_type
) {
2856 case AUDIT_FIRST_USER_MSG
... AUDIT_LAST_USER_MSG
:
2857 case AUDIT_FIRST_USER_MSG2
... AUDIT_LAST_USER_MSG2
:
2860 gemu_log("Unknown target audit message type %d\n",
2862 return -TARGET_EINVAL
;
2868 static abi_long
target_to_host_nlmsg_audit(struct nlmsghdr
*nlh
, size_t len
)
2870 return target_to_host_for_each_nlmsg(nlh
, len
, target_to_host_data_audit
);
2873 /* do_setsockopt() Must return target values and target errnos. */
2874 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
2875 abi_ulong optval_addr
, socklen_t optlen
)
2879 struct ip_mreqn
*ip_mreq
;
2880 struct ip_mreq_source
*ip_mreq_source
;
2884 /* TCP options all take an 'int' value. */
2885 if (optlen
< sizeof(uint32_t))
2886 return -TARGET_EINVAL
;
2888 if (get_user_u32(val
, optval_addr
))
2889 return -TARGET_EFAULT
;
2890 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2897 case IP_ROUTER_ALERT
:
2901 case IP_MTU_DISCOVER
:
2908 case IP_MULTICAST_TTL
:
2909 case IP_MULTICAST_LOOP
:
2911 if (optlen
>= sizeof(uint32_t)) {
2912 if (get_user_u32(val
, optval_addr
))
2913 return -TARGET_EFAULT
;
2914 } else if (optlen
>= 1) {
2915 if (get_user_u8(val
, optval_addr
))
2916 return -TARGET_EFAULT
;
2918 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2920 case IP_ADD_MEMBERSHIP
:
2921 case IP_DROP_MEMBERSHIP
:
2922 if (optlen
< sizeof (struct target_ip_mreq
) ||
2923 optlen
> sizeof (struct target_ip_mreqn
))
2924 return -TARGET_EINVAL
;
2926 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
2927 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
2928 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
2931 case IP_BLOCK_SOURCE
:
2932 case IP_UNBLOCK_SOURCE
:
2933 case IP_ADD_SOURCE_MEMBERSHIP
:
2934 case IP_DROP_SOURCE_MEMBERSHIP
:
2935 if (optlen
!= sizeof (struct target_ip_mreq_source
))
2936 return -TARGET_EINVAL
;
2938 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2939 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
2940 unlock_user (ip_mreq_source
, optval_addr
, 0);
2949 case IPV6_MTU_DISCOVER
:
2952 case IPV6_RECVPKTINFO
:
2953 case IPV6_UNICAST_HOPS
:
2955 case IPV6_RECVHOPLIMIT
:
2956 case IPV6_2292HOPLIMIT
:
2959 if (optlen
< sizeof(uint32_t)) {
2960 return -TARGET_EINVAL
;
2962 if (get_user_u32(val
, optval_addr
)) {
2963 return -TARGET_EFAULT
;
2965 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2966 &val
, sizeof(val
)));
2970 struct in6_pktinfo pki
;
2972 if (optlen
< sizeof(pki
)) {
2973 return -TARGET_EINVAL
;
2976 if (copy_from_user(&pki
, optval_addr
, sizeof(pki
))) {
2977 return -TARGET_EFAULT
;
2980 pki
.ipi6_ifindex
= tswap32(pki
.ipi6_ifindex
);
2982 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2983 &pki
, sizeof(pki
)));
2994 struct icmp6_filter icmp6f
;
2996 if (optlen
> sizeof(icmp6f
)) {
2997 optlen
= sizeof(icmp6f
);
3000 if (copy_from_user(&icmp6f
, optval_addr
, optlen
)) {
3001 return -TARGET_EFAULT
;
3004 for (val
= 0; val
< 8; val
++) {
3005 icmp6f
.data
[val
] = tswap32(icmp6f
.data
[val
]);
3008 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
3020 /* those take an u32 value */
3021 if (optlen
< sizeof(uint32_t)) {
3022 return -TARGET_EINVAL
;
3025 if (get_user_u32(val
, optval_addr
)) {
3026 return -TARGET_EFAULT
;
3028 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
3029 &val
, sizeof(val
)));
3036 case TARGET_SOL_SOCKET
:
3038 case TARGET_SO_RCVTIMEO
:
3042 optname
= SO_RCVTIMEO
;
3045 if (optlen
!= sizeof(struct target_timeval
)) {
3046 return -TARGET_EINVAL
;
3049 if (copy_from_user_timeval(&tv
, optval_addr
)) {
3050 return -TARGET_EFAULT
;
3053 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
3057 case TARGET_SO_SNDTIMEO
:
3058 optname
= SO_SNDTIMEO
;
3060 case TARGET_SO_ATTACH_FILTER
:
3062 struct target_sock_fprog
*tfprog
;
3063 struct target_sock_filter
*tfilter
;
3064 struct sock_fprog fprog
;
3065 struct sock_filter
*filter
;
3068 if (optlen
!= sizeof(*tfprog
)) {
3069 return -TARGET_EINVAL
;
3071 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
3072 return -TARGET_EFAULT
;
3074 if (!lock_user_struct(VERIFY_READ
, tfilter
,
3075 tswapal(tfprog
->filter
), 0)) {
3076 unlock_user_struct(tfprog
, optval_addr
, 1);
3077 return -TARGET_EFAULT
;
3080 fprog
.len
= tswap16(tfprog
->len
);
3081 filter
= g_try_new(struct sock_filter
, fprog
.len
);
3082 if (filter
== NULL
) {
3083 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
3084 unlock_user_struct(tfprog
, optval_addr
, 1);
3085 return -TARGET_ENOMEM
;
3087 for (i
= 0; i
< fprog
.len
; i
++) {
3088 filter
[i
].code
= tswap16(tfilter
[i
].code
);
3089 filter
[i
].jt
= tfilter
[i
].jt
;
3090 filter
[i
].jf
= tfilter
[i
].jf
;
3091 filter
[i
].k
= tswap32(tfilter
[i
].k
);
3093 fprog
.filter
= filter
;
3095 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
3096 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
3099 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
3100 unlock_user_struct(tfprog
, optval_addr
, 1);
3103 case TARGET_SO_BINDTODEVICE
:
3105 char *dev_ifname
, *addr_ifname
;
3107 if (optlen
> IFNAMSIZ
- 1) {
3108 optlen
= IFNAMSIZ
- 1;
3110 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
3112 return -TARGET_EFAULT
;
3114 optname
= SO_BINDTODEVICE
;
3115 addr_ifname
= alloca(IFNAMSIZ
);
3116 memcpy(addr_ifname
, dev_ifname
, optlen
);
3117 addr_ifname
[optlen
] = 0;
3118 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
3119 addr_ifname
, optlen
));
3120 unlock_user (dev_ifname
, optval_addr
, 0);
3123 /* Options with 'int' argument. */
3124 case TARGET_SO_DEBUG
:
3127 case TARGET_SO_REUSEADDR
:
3128 optname
= SO_REUSEADDR
;
3130 case TARGET_SO_TYPE
:
3133 case TARGET_SO_ERROR
:
3136 case TARGET_SO_DONTROUTE
:
3137 optname
= SO_DONTROUTE
;
3139 case TARGET_SO_BROADCAST
:
3140 optname
= SO_BROADCAST
;
3142 case TARGET_SO_SNDBUF
:
3143 optname
= SO_SNDBUF
;
3145 case TARGET_SO_SNDBUFFORCE
:
3146 optname
= SO_SNDBUFFORCE
;
3148 case TARGET_SO_RCVBUF
:
3149 optname
= SO_RCVBUF
;
3151 case TARGET_SO_RCVBUFFORCE
:
3152 optname
= SO_RCVBUFFORCE
;
3154 case TARGET_SO_KEEPALIVE
:
3155 optname
= SO_KEEPALIVE
;
3157 case TARGET_SO_OOBINLINE
:
3158 optname
= SO_OOBINLINE
;
3160 case TARGET_SO_NO_CHECK
:
3161 optname
= SO_NO_CHECK
;
3163 case TARGET_SO_PRIORITY
:
3164 optname
= SO_PRIORITY
;
3167 case TARGET_SO_BSDCOMPAT
:
3168 optname
= SO_BSDCOMPAT
;
3171 case TARGET_SO_PASSCRED
:
3172 optname
= SO_PASSCRED
;
3174 case TARGET_SO_PASSSEC
:
3175 optname
= SO_PASSSEC
;
3177 case TARGET_SO_TIMESTAMP
:
3178 optname
= SO_TIMESTAMP
;
3180 case TARGET_SO_RCVLOWAT
:
3181 optname
= SO_RCVLOWAT
;
3186 if (optlen
< sizeof(uint32_t))
3187 return -TARGET_EINVAL
;
3189 if (get_user_u32(val
, optval_addr
))
3190 return -TARGET_EFAULT
;
3191 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
3195 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level
, optname
);
3196 ret
= -TARGET_ENOPROTOOPT
;
3201 /* do_getsockopt() Must return target values and target errnos. */
3202 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
3203 abi_ulong optval_addr
, abi_ulong optlen
)
3210 case TARGET_SOL_SOCKET
:
3213 /* These don't just return a single integer */
3214 case TARGET_SO_LINGER
:
3215 case TARGET_SO_RCVTIMEO
:
3216 case TARGET_SO_SNDTIMEO
:
3217 case TARGET_SO_PEERNAME
:
3219 case TARGET_SO_PEERCRED
: {
3222 struct target_ucred
*tcr
;
3224 if (get_user_u32(len
, optlen
)) {
3225 return -TARGET_EFAULT
;
3228 return -TARGET_EINVAL
;
3232 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
3240 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
3241 return -TARGET_EFAULT
;
3243 __put_user(cr
.pid
, &tcr
->pid
);
3244 __put_user(cr
.uid
, &tcr
->uid
);
3245 __put_user(cr
.gid
, &tcr
->gid
);
3246 unlock_user_struct(tcr
, optval_addr
, 1);
3247 if (put_user_u32(len
, optlen
)) {
3248 return -TARGET_EFAULT
;
3252 /* Options with 'int' argument. */
3253 case TARGET_SO_DEBUG
:
3256 case TARGET_SO_REUSEADDR
:
3257 optname
= SO_REUSEADDR
;
3259 case TARGET_SO_TYPE
:
3262 case TARGET_SO_ERROR
:
3265 case TARGET_SO_DONTROUTE
:
3266 optname
= SO_DONTROUTE
;
3268 case TARGET_SO_BROADCAST
:
3269 optname
= SO_BROADCAST
;
3271 case TARGET_SO_SNDBUF
:
3272 optname
= SO_SNDBUF
;
3274 case TARGET_SO_RCVBUF
:
3275 optname
= SO_RCVBUF
;
3277 case TARGET_SO_KEEPALIVE
:
3278 optname
= SO_KEEPALIVE
;
3280 case TARGET_SO_OOBINLINE
:
3281 optname
= SO_OOBINLINE
;
3283 case TARGET_SO_NO_CHECK
:
3284 optname
= SO_NO_CHECK
;
3286 case TARGET_SO_PRIORITY
:
3287 optname
= SO_PRIORITY
;
3290 case TARGET_SO_BSDCOMPAT
:
3291 optname
= SO_BSDCOMPAT
;
3294 case TARGET_SO_PASSCRED
:
3295 optname
= SO_PASSCRED
;
3297 case TARGET_SO_TIMESTAMP
:
3298 optname
= SO_TIMESTAMP
;
3300 case TARGET_SO_RCVLOWAT
:
3301 optname
= SO_RCVLOWAT
;
3303 case TARGET_SO_ACCEPTCONN
:
3304 optname
= SO_ACCEPTCONN
;
3311 /* TCP options all take an 'int' value. */
3313 if (get_user_u32(len
, optlen
))
3314 return -TARGET_EFAULT
;
3316 return -TARGET_EINVAL
;
3318 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
3321 if (optname
== SO_TYPE
) {
3322 val
= host_to_target_sock_type(val
);
3327 if (put_user_u32(val
, optval_addr
))
3328 return -TARGET_EFAULT
;
3330 if (put_user_u8(val
, optval_addr
))
3331 return -TARGET_EFAULT
;
3333 if (put_user_u32(len
, optlen
))
3334 return -TARGET_EFAULT
;
3341 case IP_ROUTER_ALERT
:
3345 case IP_MTU_DISCOVER
:
3351 case IP_MULTICAST_TTL
:
3352 case IP_MULTICAST_LOOP
:
3353 if (get_user_u32(len
, optlen
))
3354 return -TARGET_EFAULT
;
3356 return -TARGET_EINVAL
;
3358 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
3361 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
3363 if (put_user_u32(len
, optlen
)
3364 || put_user_u8(val
, optval_addr
))
3365 return -TARGET_EFAULT
;
3367 if (len
> sizeof(int))
3369 if (put_user_u32(len
, optlen
)
3370 || put_user_u32(val
, optval_addr
))
3371 return -TARGET_EFAULT
;
3375 ret
= -TARGET_ENOPROTOOPT
;
3381 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
3383 ret
= -TARGET_EOPNOTSUPP
;
3389 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
3390 abi_ulong count
, int copy
)
3392 struct target_iovec
*target_vec
;
3394 abi_ulong total_len
, max_len
;
3397 bool bad_address
= false;
3403 if (count
> IOV_MAX
) {
3408 vec
= g_try_new0(struct iovec
, count
);
3414 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3415 count
* sizeof(struct target_iovec
), 1);
3416 if (target_vec
== NULL
) {
3421 /* ??? If host page size > target page size, this will result in a
3422 value larger than what we can actually support. */
3423 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
3426 for (i
= 0; i
< count
; i
++) {
3427 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3428 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3433 } else if (len
== 0) {
3434 /* Zero length pointer is ignored. */
3435 vec
[i
].iov_base
= 0;
3437 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
3438 /* If the first buffer pointer is bad, this is a fault. But
3439 * subsequent bad buffers will result in a partial write; this
3440 * is realized by filling the vector with null pointers and
3442 if (!vec
[i
].iov_base
) {
3453 if (len
> max_len
- total_len
) {
3454 len
= max_len
- total_len
;
3457 vec
[i
].iov_len
= len
;
3461 unlock_user(target_vec
, target_addr
, 0);
3466 if (tswapal(target_vec
[i
].iov_len
) > 0) {
3467 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
3470 unlock_user(target_vec
, target_addr
, 0);
3477 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
3478 abi_ulong count
, int copy
)
3480 struct target_iovec
*target_vec
;
3483 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3484 count
* sizeof(struct target_iovec
), 1);
3486 for (i
= 0; i
< count
; i
++) {
3487 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3488 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3492 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
3494 unlock_user(target_vec
, target_addr
, 0);
3500 static inline int target_to_host_sock_type(int *type
)
3503 int target_type
= *type
;
3505 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
3506 case TARGET_SOCK_DGRAM
:
3507 host_type
= SOCK_DGRAM
;
3509 case TARGET_SOCK_STREAM
:
3510 host_type
= SOCK_STREAM
;
3513 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
3516 if (target_type
& TARGET_SOCK_CLOEXEC
) {
3517 #if defined(SOCK_CLOEXEC)
3518 host_type
|= SOCK_CLOEXEC
;
3520 return -TARGET_EINVAL
;
3523 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3524 #if defined(SOCK_NONBLOCK)
3525 host_type
|= SOCK_NONBLOCK
;
3526 #elif !defined(O_NONBLOCK)
3527 return -TARGET_EINVAL
;
3534 /* Try to emulate socket type flags after socket creation. */
3535 static int sock_flags_fixup(int fd
, int target_type
)
3537 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3538 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3539 int flags
= fcntl(fd
, F_GETFL
);
3540 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
3542 return -TARGET_EINVAL
;
3549 static abi_long
packet_target_to_host_sockaddr(void *host_addr
,
3550 abi_ulong target_addr
,
3553 struct sockaddr
*addr
= host_addr
;
3554 struct target_sockaddr
*target_saddr
;
3556 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
3557 if (!target_saddr
) {
3558 return -TARGET_EFAULT
;
3561 memcpy(addr
, target_saddr
, len
);
3562 addr
->sa_family
= tswap16(target_saddr
->sa_family
);
3563 /* spkt_protocol is big-endian */
3565 unlock_user(target_saddr
, target_addr
, 0);
3569 static TargetFdTrans target_packet_trans
= {
3570 .target_to_host_addr
= packet_target_to_host_sockaddr
,
3573 #ifdef CONFIG_RTNETLINK
3574 static abi_long
netlink_route_target_to_host(void *buf
, size_t len
)
3578 ret
= target_to_host_nlmsg_route(buf
, len
);
3586 static abi_long
netlink_route_host_to_target(void *buf
, size_t len
)
3590 ret
= host_to_target_nlmsg_route(buf
, len
);
3598 static TargetFdTrans target_netlink_route_trans
= {
3599 .target_to_host_data
= netlink_route_target_to_host
,
3600 .host_to_target_data
= netlink_route_host_to_target
,
3602 #endif /* CONFIG_RTNETLINK */
3604 static abi_long
netlink_audit_target_to_host(void *buf
, size_t len
)
3608 ret
= target_to_host_nlmsg_audit(buf
, len
);
3616 static abi_long
netlink_audit_host_to_target(void *buf
, size_t len
)
3620 ret
= host_to_target_nlmsg_audit(buf
, len
);
3628 static TargetFdTrans target_netlink_audit_trans
= {
3629 .target_to_host_data
= netlink_audit_target_to_host
,
3630 .host_to_target_data
= netlink_audit_host_to_target
,
3633 /* do_socket() Must return target values and target errnos. */
3634 static abi_long
do_socket(int domain
, int type
, int protocol
)
3636 int target_type
= type
;
3639 ret
= target_to_host_sock_type(&type
);
3644 if (domain
== PF_NETLINK
&& !(
3645 #ifdef CONFIG_RTNETLINK
3646 protocol
== NETLINK_ROUTE
||
3648 protocol
== NETLINK_KOBJECT_UEVENT
||
3649 protocol
== NETLINK_AUDIT
)) {
3650 return -EPFNOSUPPORT
;
3653 if (domain
== AF_PACKET
||
3654 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
3655 protocol
= tswap16(protocol
);
3658 ret
= get_errno(socket(domain
, type
, protocol
));
3660 ret
= sock_flags_fixup(ret
, target_type
);
3661 if (type
== SOCK_PACKET
) {
3662 /* Manage an obsolete case :
3663 * if socket type is SOCK_PACKET, bind by name
3665 fd_trans_register(ret
, &target_packet_trans
);
3666 } else if (domain
== PF_NETLINK
) {
3668 #ifdef CONFIG_RTNETLINK
3670 fd_trans_register(ret
, &target_netlink_route_trans
);
3673 case NETLINK_KOBJECT_UEVENT
:
3674 /* nothing to do: messages are strings */
3677 fd_trans_register(ret
, &target_netlink_audit_trans
);
3680 g_assert_not_reached();
3687 /* do_bind() Must return target values and target errnos. */
3688 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
3694 if ((int)addrlen
< 0) {
3695 return -TARGET_EINVAL
;
3698 addr
= alloca(addrlen
+1);
3700 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3704 return get_errno(bind(sockfd
, addr
, addrlen
));
3707 /* do_connect() Must return target values and target errnos. */
3708 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
3714 if ((int)addrlen
< 0) {
3715 return -TARGET_EINVAL
;
3718 addr
= alloca(addrlen
+1);
3720 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3724 return get_errno(safe_connect(sockfd
, addr
, addrlen
));
3727 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3728 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
3729 int flags
, int send
)
3735 abi_ulong target_vec
;
3737 if (msgp
->msg_name
) {
3738 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
3739 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
3740 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
3741 tswapal(msgp
->msg_name
),
3743 if (ret
== -TARGET_EFAULT
) {
3744 /* For connected sockets msg_name and msg_namelen must
3745 * be ignored, so returning EFAULT immediately is wrong.
3746 * Instead, pass a bad msg_name to the host kernel, and
3747 * let it decide whether to return EFAULT or not.
3749 msg
.msg_name
= (void *)-1;
3754 msg
.msg_name
= NULL
;
3755 msg
.msg_namelen
= 0;
3757 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
3758 msg
.msg_control
= alloca(msg
.msg_controllen
);
3759 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
3761 count
= tswapal(msgp
->msg_iovlen
);
3762 target_vec
= tswapal(msgp
->msg_iov
);
3764 if (count
> IOV_MAX
) {
3765 /* sendrcvmsg returns a different errno for this condition than
3766 * readv/writev, so we must catch it here before lock_iovec() does.
3768 ret
= -TARGET_EMSGSIZE
;
3772 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
3773 target_vec
, count
, send
);
3775 ret
= -host_to_target_errno(errno
);
3778 msg
.msg_iovlen
= count
;
3782 if (fd_trans_target_to_host_data(fd
)) {
3785 host_msg
= g_malloc(msg
.msg_iov
->iov_len
);
3786 memcpy(host_msg
, msg
.msg_iov
->iov_base
, msg
.msg_iov
->iov_len
);
3787 ret
= fd_trans_target_to_host_data(fd
)(host_msg
,
3788 msg
.msg_iov
->iov_len
);
3790 msg
.msg_iov
->iov_base
= host_msg
;
3791 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3795 ret
= target_to_host_cmsg(&msg
, msgp
);
3797 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3801 ret
= get_errno(safe_recvmsg(fd
, &msg
, flags
));
3802 if (!is_error(ret
)) {
3804 if (fd_trans_host_to_target_data(fd
)) {
3805 ret
= fd_trans_host_to_target_data(fd
)(msg
.msg_iov
->iov_base
,
3808 ret
= host_to_target_cmsg(msgp
, &msg
);
3810 if (!is_error(ret
)) {
3811 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
3812 if (msg
.msg_name
!= NULL
&& msg
.msg_name
!= (void *)-1) {
3813 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
3814 msg
.msg_name
, msg
.msg_namelen
);
3826 unlock_iovec(vec
, target_vec
, count
, !send
);
3831 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
3832 int flags
, int send
)
3835 struct target_msghdr
*msgp
;
3837 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
3841 return -TARGET_EFAULT
;
3843 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
3844 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
3848 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3849 * so it might not have this *mmsg-specific flag either.
3851 #ifndef MSG_WAITFORONE
3852 #define MSG_WAITFORONE 0x10000
3855 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
3856 unsigned int vlen
, unsigned int flags
,
3859 struct target_mmsghdr
*mmsgp
;
3863 if (vlen
> UIO_MAXIOV
) {
3867 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
3869 return -TARGET_EFAULT
;
3872 for (i
= 0; i
< vlen
; i
++) {
3873 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
3874 if (is_error(ret
)) {
3877 mmsgp
[i
].msg_len
= tswap32(ret
);
3878 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3879 if (flags
& MSG_WAITFORONE
) {
3880 flags
|= MSG_DONTWAIT
;
3884 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
3886 /* Return number of datagrams sent if we sent any at all;
3887 * otherwise return the error.
3895 /* do_accept4() Must return target values and target errnos. */
3896 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
3897 abi_ulong target_addrlen_addr
, int flags
)
3904 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
3906 if (target_addr
== 0) {
3907 return get_errno(safe_accept4(fd
, NULL
, NULL
, host_flags
));
3910 /* linux returns EINVAL if addrlen pointer is invalid */
3911 if (get_user_u32(addrlen
, target_addrlen_addr
))
3912 return -TARGET_EINVAL
;
3914 if ((int)addrlen
< 0) {
3915 return -TARGET_EINVAL
;
3918 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3919 return -TARGET_EINVAL
;
3921 addr
= alloca(addrlen
);
3923 ret
= get_errno(safe_accept4(fd
, addr
, &addrlen
, host_flags
));
3924 if (!is_error(ret
)) {
3925 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3926 if (put_user_u32(addrlen
, target_addrlen_addr
))
3927 ret
= -TARGET_EFAULT
;
3932 /* do_getpeername() Must return target values and target errnos. */
3933 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
3934 abi_ulong target_addrlen_addr
)
3940 if (get_user_u32(addrlen
, target_addrlen_addr
))
3941 return -TARGET_EFAULT
;
3943 if ((int)addrlen
< 0) {
3944 return -TARGET_EINVAL
;
3947 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3948 return -TARGET_EFAULT
;
3950 addr
= alloca(addrlen
);
3952 ret
= get_errno(getpeername(fd
, addr
, &addrlen
));
3953 if (!is_error(ret
)) {
3954 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3955 if (put_user_u32(addrlen
, target_addrlen_addr
))
3956 ret
= -TARGET_EFAULT
;
3961 /* do_getsockname() Must return target values and target errnos. */
3962 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
3963 abi_ulong target_addrlen_addr
)
3969 if (get_user_u32(addrlen
, target_addrlen_addr
))
3970 return -TARGET_EFAULT
;
3972 if ((int)addrlen
< 0) {
3973 return -TARGET_EINVAL
;
3976 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3977 return -TARGET_EFAULT
;
3979 addr
= alloca(addrlen
);
3981 ret
= get_errno(getsockname(fd
, addr
, &addrlen
));
3982 if (!is_error(ret
)) {
3983 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3984 if (put_user_u32(addrlen
, target_addrlen_addr
))
3985 ret
= -TARGET_EFAULT
;
3990 /* do_socketpair() Must return target values and target errnos. */
3991 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
3992 abi_ulong target_tab_addr
)
3997 target_to_host_sock_type(&type
);
3999 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
4000 if (!is_error(ret
)) {
4001 if (put_user_s32(tab
[0], target_tab_addr
)
4002 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
4003 ret
= -TARGET_EFAULT
;
4008 /* do_sendto() Must return target values and target errnos. */
4009 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
4010 abi_ulong target_addr
, socklen_t addrlen
)
4014 void *copy_msg
= NULL
;
4017 if ((int)addrlen
< 0) {
4018 return -TARGET_EINVAL
;
4021 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
4023 return -TARGET_EFAULT
;
4024 if (fd_trans_target_to_host_data(fd
)) {
4025 copy_msg
= host_msg
;
4026 host_msg
= g_malloc(len
);
4027 memcpy(host_msg
, copy_msg
, len
);
4028 ret
= fd_trans_target_to_host_data(fd
)(host_msg
, len
);
4034 addr
= alloca(addrlen
+1);
4035 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);
4039 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
4041 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, NULL
, 0));
4046 host_msg
= copy_msg
;
4048 unlock_user(host_msg
, msg
, 0);
4052 /* do_recvfrom() Must return target values and target errnos. */
4053 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
4054 abi_ulong target_addr
,
4055 abi_ulong target_addrlen
)
4062 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
4064 return -TARGET_EFAULT
;
4066 if (get_user_u32(addrlen
, target_addrlen
)) {
4067 ret
= -TARGET_EFAULT
;
4070 if ((int)addrlen
< 0) {
4071 ret
= -TARGET_EINVAL
;
4074 addr
= alloca(addrlen
);
4075 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
,
4078 addr
= NULL
; /* To keep compiler quiet. */
4079 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
, NULL
, 0));
4081 if (!is_error(ret
)) {
4082 if (fd_trans_host_to_target_data(fd
)) {
4083 ret
= fd_trans_host_to_target_data(fd
)(host_msg
, ret
);
4086 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
4087 if (put_user_u32(addrlen
, target_addrlen
)) {
4088 ret
= -TARGET_EFAULT
;
4092 unlock_user(host_msg
, msg
, len
);
4095 unlock_user(host_msg
, msg
, 0);
4100 #ifdef TARGET_NR_socketcall
4101 /* do_socketcall() must return target values and target errnos. */
4102 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
4104 static const unsigned nargs
[] = { /* number of arguments per operation */
4105 [TARGET_SYS_SOCKET
] = 3, /* domain, type, protocol */
4106 [TARGET_SYS_BIND
] = 3, /* fd, addr, addrlen */
4107 [TARGET_SYS_CONNECT
] = 3, /* fd, addr, addrlen */
4108 [TARGET_SYS_LISTEN
] = 2, /* fd, backlog */
4109 [TARGET_SYS_ACCEPT
] = 3, /* fd, addr, addrlen */
4110 [TARGET_SYS_GETSOCKNAME
] = 3, /* fd, addr, addrlen */
4111 [TARGET_SYS_GETPEERNAME
] = 3, /* fd, addr, addrlen */
4112 [TARGET_SYS_SOCKETPAIR
] = 4, /* domain, type, protocol, tab */
4113 [TARGET_SYS_SEND
] = 4, /* fd, msg, len, flags */
4114 [TARGET_SYS_RECV
] = 4, /* fd, msg, len, flags */
4115 [TARGET_SYS_SENDTO
] = 6, /* fd, msg, len, flags, addr, addrlen */
4116 [TARGET_SYS_RECVFROM
] = 6, /* fd, msg, len, flags, addr, addrlen */
4117 [TARGET_SYS_SHUTDOWN
] = 2, /* fd, how */
4118 [TARGET_SYS_SETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
4119 [TARGET_SYS_GETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
4120 [TARGET_SYS_SENDMSG
] = 3, /* fd, msg, flags */
4121 [TARGET_SYS_RECVMSG
] = 3, /* fd, msg, flags */
4122 [TARGET_SYS_ACCEPT4
] = 4, /* fd, addr, addrlen, flags */
4123 [TARGET_SYS_RECVMMSG
] = 4, /* fd, msgvec, vlen, flags */
4124 [TARGET_SYS_SENDMMSG
] = 4, /* fd, msgvec, vlen, flags */
4126 abi_long a
[6]; /* max 6 args */
4129 /* check the range of the first argument num */
4130 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
4131 if (num
< 1 || num
> TARGET_SYS_SENDMMSG
) {
4132 return -TARGET_EINVAL
;
4134 /* ensure we have space for args */
4135 if (nargs
[num
] > ARRAY_SIZE(a
)) {
4136 return -TARGET_EINVAL
;
4138 /* collect the arguments in a[] according to nargs[] */
4139 for (i
= 0; i
< nargs
[num
]; ++i
) {
4140 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
4141 return -TARGET_EFAULT
;
4144 /* now when we have the args, invoke the appropriate underlying function */
4146 case TARGET_SYS_SOCKET
: /* domain, type, protocol */
4147 return do_socket(a
[0], a
[1], a
[2]);
4148 case TARGET_SYS_BIND
: /* sockfd, addr, addrlen */
4149 return do_bind(a
[0], a
[1], a
[2]);
4150 case TARGET_SYS_CONNECT
: /* sockfd, addr, addrlen */
4151 return do_connect(a
[0], a
[1], a
[2]);
4152 case TARGET_SYS_LISTEN
: /* sockfd, backlog */
4153 return get_errno(listen(a
[0], a
[1]));
4154 case TARGET_SYS_ACCEPT
: /* sockfd, addr, addrlen */
4155 return do_accept4(a
[0], a
[1], a
[2], 0);
4156 case TARGET_SYS_GETSOCKNAME
: /* sockfd, addr, addrlen */
4157 return do_getsockname(a
[0], a
[1], a
[2]);
4158 case TARGET_SYS_GETPEERNAME
: /* sockfd, addr, addrlen */
4159 return do_getpeername(a
[0], a
[1], a
[2]);
4160 case TARGET_SYS_SOCKETPAIR
: /* domain, type, protocol, tab */
4161 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
4162 case TARGET_SYS_SEND
: /* sockfd, msg, len, flags */
4163 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
4164 case TARGET_SYS_RECV
: /* sockfd, msg, len, flags */
4165 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
4166 case TARGET_SYS_SENDTO
: /* sockfd, msg, len, flags, addr, addrlen */
4167 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
4168 case TARGET_SYS_RECVFROM
: /* sockfd, msg, len, flags, addr, addrlen */
4169 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
4170 case TARGET_SYS_SHUTDOWN
: /* sockfd, how */
4171 return get_errno(shutdown(a
[0], a
[1]));
4172 case TARGET_SYS_SETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
4173 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
4174 case TARGET_SYS_GETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
4175 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
4176 case TARGET_SYS_SENDMSG
: /* sockfd, msg, flags */
4177 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
4178 case TARGET_SYS_RECVMSG
: /* sockfd, msg, flags */
4179 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
4180 case TARGET_SYS_ACCEPT4
: /* sockfd, addr, addrlen, flags */
4181 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
4182 case TARGET_SYS_RECVMMSG
: /* sockfd, msgvec, vlen, flags */
4183 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 0);
4184 case TARGET_SYS_SENDMMSG
: /* sockfd, msgvec, vlen, flags */
4185 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 1);
4187 gemu_log("Unsupported socketcall: %d\n", num
);
4188 return -TARGET_EINVAL
;
4193 #define N_SHM_REGIONS 32
4195 static struct shm_region
{
4199 } shm_regions
[N_SHM_REGIONS
];
4201 #ifndef TARGET_SEMID64_DS
4202 /* asm-generic version of this struct */
4203 struct target_semid64_ds
4205 struct target_ipc_perm sem_perm
;
4206 abi_ulong sem_otime
;
4207 #if TARGET_ABI_BITS == 32
4208 abi_ulong __unused1
;
4210 abi_ulong sem_ctime
;
4211 #if TARGET_ABI_BITS == 32
4212 abi_ulong __unused2
;
4214 abi_ulong sem_nsems
;
4215 abi_ulong __unused3
;
4216 abi_ulong __unused4
;
4220 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
4221 abi_ulong target_addr
)
4223 struct target_ipc_perm
*target_ip
;
4224 struct target_semid64_ds
*target_sd
;
4226 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4227 return -TARGET_EFAULT
;
4228 target_ip
= &(target_sd
->sem_perm
);
4229 host_ip
->__key
= tswap32(target_ip
->__key
);
4230 host_ip
->uid
= tswap32(target_ip
->uid
);
4231 host_ip
->gid
= tswap32(target_ip
->gid
);
4232 host_ip
->cuid
= tswap32(target_ip
->cuid
);
4233 host_ip
->cgid
= tswap32(target_ip
->cgid
);
4234 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
4235 host_ip
->mode
= tswap32(target_ip
->mode
);
4237 host_ip
->mode
= tswap16(target_ip
->mode
);
4239 #if defined(TARGET_PPC)
4240 host_ip
->__seq
= tswap32(target_ip
->__seq
);
4242 host_ip
->__seq
= tswap16(target_ip
->__seq
);
4244 unlock_user_struct(target_sd
, target_addr
, 0);
4248 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
4249 struct ipc_perm
*host_ip
)
4251 struct target_ipc_perm
*target_ip
;
4252 struct target_semid64_ds
*target_sd
;
4254 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4255 return -TARGET_EFAULT
;
4256 target_ip
= &(target_sd
->sem_perm
);
4257 target_ip
->__key
= tswap32(host_ip
->__key
);
4258 target_ip
->uid
= tswap32(host_ip
->uid
);
4259 target_ip
->gid
= tswap32(host_ip
->gid
);
4260 target_ip
->cuid
= tswap32(host_ip
->cuid
);
4261 target_ip
->cgid
= tswap32(host_ip
->cgid
);
4262 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
4263 target_ip
->mode
= tswap32(host_ip
->mode
);
4265 target_ip
->mode
= tswap16(host_ip
->mode
);
4267 #if defined(TARGET_PPC)
4268 target_ip
->__seq
= tswap32(host_ip
->__seq
);
4270 target_ip
->__seq
= tswap16(host_ip
->__seq
);
4272 unlock_user_struct(target_sd
, target_addr
, 1);
4276 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
4277 abi_ulong target_addr
)
4279 struct target_semid64_ds
*target_sd
;
4281 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4282 return -TARGET_EFAULT
;
4283 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
4284 return -TARGET_EFAULT
;
4285 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
4286 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
4287 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
4288 unlock_user_struct(target_sd
, target_addr
, 0);
4292 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
4293 struct semid_ds
*host_sd
)
4295 struct target_semid64_ds
*target_sd
;
4297 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4298 return -TARGET_EFAULT
;
4299 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
4300 return -TARGET_EFAULT
;
4301 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
4302 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
4303 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
4304 unlock_user_struct(target_sd
, target_addr
, 1);
4308 struct target_seminfo
{
4321 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
4322 struct seminfo
*host_seminfo
)
4324 struct target_seminfo
*target_seminfo
;
4325 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
4326 return -TARGET_EFAULT
;
4327 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
4328 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
4329 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
4330 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
4331 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
4332 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
4333 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
4334 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
4335 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
4336 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
4337 unlock_user_struct(target_seminfo
, target_addr
, 1);
4343 struct semid_ds
*buf
;
4344 unsigned short *array
;
4345 struct seminfo
*__buf
;
4348 union target_semun
{
4355 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
4356 abi_ulong target_addr
)
4359 unsigned short *array
;
4361 struct semid_ds semid_ds
;
4364 semun
.buf
= &semid_ds
;
4366 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
4368 return get_errno(ret
);
4370 nsems
= semid_ds
.sem_nsems
;
4372 *host_array
= g_try_new(unsigned short, nsems
);
4374 return -TARGET_ENOMEM
;
4376 array
= lock_user(VERIFY_READ
, target_addr
,
4377 nsems
*sizeof(unsigned short), 1);
4379 g_free(*host_array
);
4380 return -TARGET_EFAULT
;
4383 for(i
=0; i
<nsems
; i
++) {
4384 __get_user((*host_array
)[i
], &array
[i
]);
4386 unlock_user(array
, target_addr
, 0);
4391 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
4392 unsigned short **host_array
)
4395 unsigned short *array
;
4397 struct semid_ds semid_ds
;
4400 semun
.buf
= &semid_ds
;
4402 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
4404 return get_errno(ret
);
4406 nsems
= semid_ds
.sem_nsems
;
4408 array
= lock_user(VERIFY_WRITE
, target_addr
,
4409 nsems
*sizeof(unsigned short), 0);
4411 return -TARGET_EFAULT
;
4413 for(i
=0; i
<nsems
; i
++) {
4414 __put_user((*host_array
)[i
], &array
[i
]);
4416 g_free(*host_array
);
4417 unlock_user(array
, target_addr
, 1);
4422 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
4423 abi_ulong target_arg
)
4425 union target_semun target_su
= { .buf
= target_arg
};
4427 struct semid_ds dsarg
;
4428 unsigned short *array
= NULL
;
4429 struct seminfo seminfo
;
4430 abi_long ret
= -TARGET_EINVAL
;
4437 /* In 64 bit cross-endian situations, we will erroneously pick up
4438 * the wrong half of the union for the "val" element. To rectify
4439 * this, the entire 8-byte structure is byteswapped, followed by
4440 * a swap of the 4 byte val field. In other cases, the data is
4441 * already in proper host byte order. */
4442 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
4443 target_su
.buf
= tswapal(target_su
.buf
);
4444 arg
.val
= tswap32(target_su
.val
);
4446 arg
.val
= target_su
.val
;
4448 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4452 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
4456 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4457 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
4464 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
4468 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4469 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
4475 arg
.__buf
= &seminfo
;
4476 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4477 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
4485 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
4492 struct target_sembuf
{
4493 unsigned short sem_num
;
4498 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
4499 abi_ulong target_addr
,
4502 struct target_sembuf
*target_sembuf
;
4505 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
4506 nsops
*sizeof(struct target_sembuf
), 1);
4508 return -TARGET_EFAULT
;
4510 for(i
=0; i
<nsops
; i
++) {
4511 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
4512 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
4513 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
4516 unlock_user(target_sembuf
, target_addr
, 0);
4521 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
4523 struct sembuf sops
[nsops
];
4525 if (target_to_host_sembuf(sops
, ptr
, nsops
))
4526 return -TARGET_EFAULT
;
4528 return get_errno(safe_semtimedop(semid
, sops
, nsops
, NULL
));
4531 struct target_msqid_ds
4533 struct target_ipc_perm msg_perm
;
4534 abi_ulong msg_stime
;
4535 #if TARGET_ABI_BITS == 32
4536 abi_ulong __unused1
;
4538 abi_ulong msg_rtime
;
4539 #if TARGET_ABI_BITS == 32
4540 abi_ulong __unused2
;
4542 abi_ulong msg_ctime
;
4543 #if TARGET_ABI_BITS == 32
4544 abi_ulong __unused3
;
4546 abi_ulong __msg_cbytes
;
4548 abi_ulong msg_qbytes
;
4549 abi_ulong msg_lspid
;
4550 abi_ulong msg_lrpid
;
4551 abi_ulong __unused4
;
4552 abi_ulong __unused5
;
4555 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
4556 abi_ulong target_addr
)
4558 struct target_msqid_ds
*target_md
;
4560 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
4561 return -TARGET_EFAULT
;
4562 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
4563 return -TARGET_EFAULT
;
4564 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
4565 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
4566 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
4567 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
4568 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
4569 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
4570 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
4571 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
4572 unlock_user_struct(target_md
, target_addr
, 0);
4576 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
4577 struct msqid_ds
*host_md
)
4579 struct target_msqid_ds
*target_md
;
4581 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
4582 return -TARGET_EFAULT
;
4583 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
4584 return -TARGET_EFAULT
;
4585 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
4586 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
4587 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
4588 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
4589 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
4590 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
4591 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
4592 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
4593 unlock_user_struct(target_md
, target_addr
, 1);
4597 struct target_msginfo
{
4605 unsigned short int msgseg
;
4608 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
4609 struct msginfo
*host_msginfo
)
4611 struct target_msginfo
*target_msginfo
;
4612 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
4613 return -TARGET_EFAULT
;
4614 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
4615 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
4616 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
4617 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
4618 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
4619 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
4620 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
4621 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
4622 unlock_user_struct(target_msginfo
, target_addr
, 1);
4626 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
4628 struct msqid_ds dsarg
;
4629 struct msginfo msginfo
;
4630 abi_long ret
= -TARGET_EINVAL
;
4638 if (target_to_host_msqid_ds(&dsarg
,ptr
))
4639 return -TARGET_EFAULT
;
4640 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
4641 if (host_to_target_msqid_ds(ptr
,&dsarg
))
4642 return -TARGET_EFAULT
;
4645 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
4649 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
4650 if (host_to_target_msginfo(ptr
, &msginfo
))
4651 return -TARGET_EFAULT
;
4658 struct target_msgbuf
{
4663 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
4664 ssize_t msgsz
, int msgflg
)
4666 struct target_msgbuf
*target_mb
;
4667 struct msgbuf
*host_mb
;
4671 return -TARGET_EINVAL
;
4674 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
4675 return -TARGET_EFAULT
;
4676 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4678 unlock_user_struct(target_mb
, msgp
, 0);
4679 return -TARGET_ENOMEM
;
4681 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
4682 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
4683 ret
= get_errno(safe_msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
4685 unlock_user_struct(target_mb
, msgp
, 0);
4690 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
4691 ssize_t msgsz
, abi_long msgtyp
,
4694 struct target_msgbuf
*target_mb
;
4696 struct msgbuf
*host_mb
;
4700 return -TARGET_EINVAL
;
4703 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
4704 return -TARGET_EFAULT
;
4706 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4708 ret
= -TARGET_ENOMEM
;
4711 ret
= get_errno(safe_msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
4714 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
4715 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
4716 if (!target_mtext
) {
4717 ret
= -TARGET_EFAULT
;
4720 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
4721 unlock_user(target_mtext
, target_mtext_addr
, ret
);
4724 target_mb
->mtype
= tswapal(host_mb
->mtype
);
4728 unlock_user_struct(target_mb
, msgp
, 1);
4733 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
4734 abi_ulong target_addr
)
4736 struct target_shmid_ds
*target_sd
;
4738 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4739 return -TARGET_EFAULT
;
4740 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
4741 return -TARGET_EFAULT
;
4742 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4743 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4744 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4745 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4746 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4747 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4748 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4749 unlock_user_struct(target_sd
, target_addr
, 0);
4753 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
4754 struct shmid_ds
*host_sd
)
4756 struct target_shmid_ds
*target_sd
;
4758 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4759 return -TARGET_EFAULT
;
4760 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
4761 return -TARGET_EFAULT
;
4762 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4763 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4764 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4765 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4766 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4767 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4768 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4769 unlock_user_struct(target_sd
, target_addr
, 1);
4773 struct target_shminfo
{
4781 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
4782 struct shminfo
*host_shminfo
)
4784 struct target_shminfo
*target_shminfo
;
4785 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
4786 return -TARGET_EFAULT
;
4787 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
4788 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
4789 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
4790 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
4791 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
4792 unlock_user_struct(target_shminfo
, target_addr
, 1);
4796 struct target_shm_info
{
4801 abi_ulong swap_attempts
;
4802 abi_ulong swap_successes
;
4805 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
4806 struct shm_info
*host_shm_info
)
4808 struct target_shm_info
*target_shm_info
;
4809 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
4810 return -TARGET_EFAULT
;
4811 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
4812 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
4813 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
4814 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
4815 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
4816 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
4817 unlock_user_struct(target_shm_info
, target_addr
, 1);
4821 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
4823 struct shmid_ds dsarg
;
4824 struct shminfo shminfo
;
4825 struct shm_info shm_info
;
4826 abi_long ret
= -TARGET_EINVAL
;
4834 if (target_to_host_shmid_ds(&dsarg
, buf
))
4835 return -TARGET_EFAULT
;
4836 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
4837 if (host_to_target_shmid_ds(buf
, &dsarg
))
4838 return -TARGET_EFAULT
;
4841 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
4842 if (host_to_target_shminfo(buf
, &shminfo
))
4843 return -TARGET_EFAULT
;
4846 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
4847 if (host_to_target_shm_info(buf
, &shm_info
))
4848 return -TARGET_EFAULT
;
4853 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
4860 #ifndef TARGET_FORCE_SHMLBA
4861 /* For most architectures, SHMLBA is the same as the page size;
4862 * some architectures have larger values, in which case they should
4863 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
4864 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
4865 * and defining its own value for SHMLBA.
4867 * The kernel also permits SHMLBA to be set by the architecture to a
4868 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
4869 * this means that addresses are rounded to the large size if
4870 * SHM_RND is set but addresses not aligned to that size are not rejected
4871 * as long as they are at least page-aligned. Since the only architecture
4872 * which uses this is ia64 this code doesn't provide for that oddity.
4874 static inline abi_ulong
target_shmlba(CPUArchState
*cpu_env
)
4876 return TARGET_PAGE_SIZE
;
4880 static inline abi_ulong
do_shmat(CPUArchState
*cpu_env
,
4881 int shmid
, abi_ulong shmaddr
, int shmflg
)
4885 struct shmid_ds shm_info
;
4889 /* find out the length of the shared memory segment */
4890 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
4891 if (is_error(ret
)) {
4892 /* can't get length, bail out */
4896 shmlba
= target_shmlba(cpu_env
);
4898 if (shmaddr
& (shmlba
- 1)) {
4899 if (shmflg
& SHM_RND
) {
4900 shmaddr
&= ~(shmlba
- 1);
4902 return -TARGET_EINVAL
;
4905 if (!guest_range_valid(shmaddr
, shm_info
.shm_segsz
)) {
4906 return -TARGET_EINVAL
;
4912 host_raddr
= shmat(shmid
, (void *)g2h(shmaddr
), shmflg
);
4914 abi_ulong mmap_start
;
4916 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
);
4918 if (mmap_start
== -1) {
4920 host_raddr
= (void *)-1;
4922 host_raddr
= shmat(shmid
, g2h(mmap_start
), shmflg
| SHM_REMAP
);
4925 if (host_raddr
== (void *)-1) {
4927 return get_errno((long)host_raddr
);
4929 raddr
=h2g((unsigned long)host_raddr
);
4931 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
4932 PAGE_VALID
| PAGE_READ
|
4933 ((shmflg
& SHM_RDONLY
)? 0 : PAGE_WRITE
));
4935 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
4936 if (!shm_regions
[i
].in_use
) {
4937 shm_regions
[i
].in_use
= true;
4938 shm_regions
[i
].start
= raddr
;
4939 shm_regions
[i
].size
= shm_info
.shm_segsz
;
4949 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
4956 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
4957 if (shm_regions
[i
].in_use
&& shm_regions
[i
].start
== shmaddr
) {
4958 shm_regions
[i
].in_use
= false;
4959 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
, 0);
4963 rv
= get_errno(shmdt(g2h(shmaddr
)));
4970 #ifdef TARGET_NR_ipc
4971 /* ??? This only works with linear mappings. */
4972 /* do_ipc() must return target values and target errnos. */
4973 static abi_long
do_ipc(CPUArchState
*cpu_env
,
4974 unsigned int call
, abi_long first
,
4975 abi_long second
, abi_long third
,
4976 abi_long ptr
, abi_long fifth
)
4981 version
= call
>> 16;
4986 ret
= do_semop(first
, ptr
, second
);
4990 ret
= get_errno(semget(first
, second
, third
));
4993 case IPCOP_semctl
: {
4994 /* The semun argument to semctl is passed by value, so dereference the
4997 get_user_ual(atptr
, ptr
);
4998 ret
= do_semctl(first
, second
, third
, atptr
);
5003 ret
= get_errno(msgget(first
, second
));
5007 ret
= do_msgsnd(first
, ptr
, second
, third
);
5011 ret
= do_msgctl(first
, second
, ptr
);
5018 struct target_ipc_kludge
{
5023 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
5024 ret
= -TARGET_EFAULT
;
5028 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
5030 unlock_user_struct(tmp
, ptr
, 0);
5034 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
5043 raddr
= do_shmat(cpu_env
, first
, ptr
, second
);
5044 if (is_error(raddr
))
5045 return get_errno(raddr
);
5046 if (put_user_ual(raddr
, third
))
5047 return -TARGET_EFAULT
;
5051 ret
= -TARGET_EINVAL
;
5056 ret
= do_shmdt(ptr
);
5060 /* IPC_* flag values are the same on all linux platforms */
5061 ret
= get_errno(shmget(first
, second
, third
));
5064 /* IPC_* and SHM_* command values are the same on all linux platforms */
5066 ret
= do_shmctl(first
, second
, ptr
);
5069 gemu_log("Unsupported ipc call: %d (version %d)\n", call
, version
);
5070 ret
= -TARGET_ENOSYS
;
5077 /* kernel structure types definitions */
5079 #define STRUCT(name, ...) STRUCT_ ## name,
5080 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
5082 #include "syscall_types.h"
5086 #undef STRUCT_SPECIAL
5088 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
5089 #define STRUCT_SPECIAL(name)
5090 #include "syscall_types.h"
5092 #undef STRUCT_SPECIAL
5094 typedef struct IOCTLEntry IOCTLEntry
;
5096 typedef abi_long
do_ioctl_fn(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5097 int fd
, int cmd
, abi_long arg
);
5101 unsigned int host_cmd
;
5104 do_ioctl_fn
*do_ioctl
;
5105 const argtype arg_type
[5];
5108 #define IOC_R 0x0001
5109 #define IOC_W 0x0002
5110 #define IOC_RW (IOC_R | IOC_W)
5112 #define MAX_STRUCT_SIZE 4096
5114 #ifdef CONFIG_FIEMAP
5115 /* So fiemap access checks don't overflow on 32 bit systems.
5116 * This is very slightly smaller than the limit imposed by
5117 * the underlying kernel.
5119 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
5120 / sizeof(struct fiemap_extent))
5122 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5123 int fd
, int cmd
, abi_long arg
)
5125 /* The parameter for this ioctl is a struct fiemap followed
5126 * by an array of struct fiemap_extent whose size is set
5127 * in fiemap->fm_extent_count. The array is filled in by the
5130 int target_size_in
, target_size_out
;
5132 const argtype
*arg_type
= ie
->arg_type
;
5133 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
5136 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
5140 assert(arg_type
[0] == TYPE_PTR
);
5141 assert(ie
->access
== IOC_RW
);
5143 target_size_in
= thunk_type_size(arg_type
, 0);
5144 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
5146 return -TARGET_EFAULT
;
5148 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5149 unlock_user(argptr
, arg
, 0);
5150 fm
= (struct fiemap
*)buf_temp
;
5151 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
5152 return -TARGET_EINVAL
;
5155 outbufsz
= sizeof (*fm
) +
5156 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
5158 if (outbufsz
> MAX_STRUCT_SIZE
) {
5159 /* We can't fit all the extents into the fixed size buffer.
5160 * Allocate one that is large enough and use it instead.
5162 fm
= g_try_malloc(outbufsz
);
5164 return -TARGET_ENOMEM
;
5166 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
5169 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, fm
));
5170 if (!is_error(ret
)) {
5171 target_size_out
= target_size_in
;
5172 /* An extent_count of 0 means we were only counting the extents
5173 * so there are no structs to copy
5175 if (fm
->fm_extent_count
!= 0) {
5176 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
5178 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
5180 ret
= -TARGET_EFAULT
;
5182 /* Convert the struct fiemap */
5183 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
5184 if (fm
->fm_extent_count
!= 0) {
5185 p
= argptr
+ target_size_in
;
5186 /* ...and then all the struct fiemap_extents */
5187 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
5188 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
5193 unlock_user(argptr
, arg
, target_size_out
);
5203 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5204 int fd
, int cmd
, abi_long arg
)
5206 const argtype
*arg_type
= ie
->arg_type
;
5210 struct ifconf
*host_ifconf
;
5212 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
5213 int target_ifreq_size
;
5218 abi_long target_ifc_buf
;
5222 assert(arg_type
[0] == TYPE_PTR
);
5223 assert(ie
->access
== IOC_RW
);
5226 target_size
= thunk_type_size(arg_type
, 0);
5228 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5230 return -TARGET_EFAULT
;
5231 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5232 unlock_user(argptr
, arg
, 0);
5234 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
5235 target_ifc_len
= host_ifconf
->ifc_len
;
5236 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
5238 target_ifreq_size
= thunk_type_size(ifreq_arg_type
, 0);
5239 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
5240 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
5242 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
5243 if (outbufsz
> MAX_STRUCT_SIZE
) {
5244 /* We can't fit all the extents into the fixed size buffer.
5245 * Allocate one that is large enough and use it instead.
5247 host_ifconf
= malloc(outbufsz
);
5249 return -TARGET_ENOMEM
;
5251 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
5254 host_ifc_buf
= (char*)host_ifconf
+ sizeof(*host_ifconf
);
5256 host_ifconf
->ifc_len
= host_ifc_len
;
5257 host_ifconf
->ifc_buf
= host_ifc_buf
;
5259 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_ifconf
));
5260 if (!is_error(ret
)) {
5261 /* convert host ifc_len to target ifc_len */
5263 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
5264 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
5265 host_ifconf
->ifc_len
= target_ifc_len
;
5267 /* restore target ifc_buf */
5269 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
5271 /* copy struct ifconf to target user */
5273 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5275 return -TARGET_EFAULT
;
5276 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
5277 unlock_user(argptr
, arg
, target_size
);
5279 /* copy ifreq[] to target user */
5281 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
5282 for (i
= 0; i
< nb_ifreq
; i
++) {
5283 thunk_convert(argptr
+ i
* target_ifreq_size
,
5284 host_ifc_buf
+ i
* sizeof(struct ifreq
),
5285 ifreq_arg_type
, THUNK_TARGET
);
5287 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
5297 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5298 int cmd
, abi_long arg
)
5301 struct dm_ioctl
*host_dm
;
5302 abi_long guest_data
;
5303 uint32_t guest_data_size
;
5305 const argtype
*arg_type
= ie
->arg_type
;
5307 void *big_buf
= NULL
;
5311 target_size
= thunk_type_size(arg_type
, 0);
5312 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5314 ret
= -TARGET_EFAULT
;
5317 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5318 unlock_user(argptr
, arg
, 0);
5320 /* buf_temp is too small, so fetch things into a bigger buffer */
5321 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
5322 memcpy(big_buf
, buf_temp
, target_size
);
5326 guest_data
= arg
+ host_dm
->data_start
;
5327 if ((guest_data
- arg
) < 0) {
5328 ret
= -TARGET_EINVAL
;
5331 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5332 host_data
= (char*)host_dm
+ host_dm
->data_start
;
5334 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
5336 ret
= -TARGET_EFAULT
;
5340 switch (ie
->host_cmd
) {
5342 case DM_LIST_DEVICES
:
5345 case DM_DEV_SUSPEND
:
5348 case DM_TABLE_STATUS
:
5349 case DM_TABLE_CLEAR
:
5351 case DM_LIST_VERSIONS
:
5355 case DM_DEV_SET_GEOMETRY
:
5356 /* data contains only strings */
5357 memcpy(host_data
, argptr
, guest_data_size
);
5360 memcpy(host_data
, argptr
, guest_data_size
);
5361 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
5365 void *gspec
= argptr
;
5366 void *cur_data
= host_data
;
5367 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5368 int spec_size
= thunk_type_size(arg_type
, 0);
5371 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5372 struct dm_target_spec
*spec
= cur_data
;
5376 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
5377 slen
= strlen((char*)gspec
+ spec_size
) + 1;
5379 spec
->next
= sizeof(*spec
) + slen
;
5380 strcpy((char*)&spec
[1], gspec
+ spec_size
);
5382 cur_data
+= spec
->next
;
5387 ret
= -TARGET_EINVAL
;
5388 unlock_user(argptr
, guest_data
, 0);
5391 unlock_user(argptr
, guest_data
, 0);
5393 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5394 if (!is_error(ret
)) {
5395 guest_data
= arg
+ host_dm
->data_start
;
5396 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5397 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
5398 switch (ie
->host_cmd
) {
5403 case DM_DEV_SUSPEND
:
5406 case DM_TABLE_CLEAR
:
5408 case DM_DEV_SET_GEOMETRY
:
5409 /* no return data */
5411 case DM_LIST_DEVICES
:
5413 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
5414 uint32_t remaining_data
= guest_data_size
;
5415 void *cur_data
= argptr
;
5416 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
5417 int nl_size
= 12; /* can't use thunk_size due to alignment */
5420 uint32_t next
= nl
->next
;
5422 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
5424 if (remaining_data
< nl
->next
) {
5425 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5428 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
5429 strcpy(cur_data
+ nl_size
, nl
->name
);
5430 cur_data
+= nl
->next
;
5431 remaining_data
-= nl
->next
;
5435 nl
= (void*)nl
+ next
;
5440 case DM_TABLE_STATUS
:
5442 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
5443 void *cur_data
= argptr
;
5444 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5445 int spec_size
= thunk_type_size(arg_type
, 0);
5448 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5449 uint32_t next
= spec
->next
;
5450 int slen
= strlen((char*)&spec
[1]) + 1;
5451 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
5452 if (guest_data_size
< spec
->next
) {
5453 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5456 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
5457 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
5458 cur_data
= argptr
+ spec
->next
;
5459 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
5465 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
5466 int count
= *(uint32_t*)hdata
;
5467 uint64_t *hdev
= hdata
+ 8;
5468 uint64_t *gdev
= argptr
+ 8;
5471 *(uint32_t*)argptr
= tswap32(count
);
5472 for (i
= 0; i
< count
; i
++) {
5473 *gdev
= tswap64(*hdev
);
5479 case DM_LIST_VERSIONS
:
5481 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
5482 uint32_t remaining_data
= guest_data_size
;
5483 void *cur_data
= argptr
;
5484 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
5485 int vers_size
= thunk_type_size(arg_type
, 0);
5488 uint32_t next
= vers
->next
;
5490 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
5492 if (remaining_data
< vers
->next
) {
5493 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5496 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
5497 strcpy(cur_data
+ vers_size
, vers
->name
);
5498 cur_data
+= vers
->next
;
5499 remaining_data
-= vers
->next
;
5503 vers
= (void*)vers
+ next
;
5508 unlock_user(argptr
, guest_data
, 0);
5509 ret
= -TARGET_EINVAL
;
5512 unlock_user(argptr
, guest_data
, guest_data_size
);
5514 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5516 ret
= -TARGET_EFAULT
;
5519 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5520 unlock_user(argptr
, arg
, target_size
);
5527 static abi_long
do_ioctl_blkpg(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5528 int cmd
, abi_long arg
)
5532 const argtype
*arg_type
= ie
->arg_type
;
5533 const argtype part_arg_type
[] = { MK_STRUCT(STRUCT_blkpg_partition
) };
5536 struct blkpg_ioctl_arg
*host_blkpg
= (void*)buf_temp
;
5537 struct blkpg_partition host_part
;
5539 /* Read and convert blkpg */
5541 target_size
= thunk_type_size(arg_type
, 0);
5542 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5544 ret
= -TARGET_EFAULT
;
5547 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5548 unlock_user(argptr
, arg
, 0);
5550 switch (host_blkpg
->op
) {
5551 case BLKPG_ADD_PARTITION
:
5552 case BLKPG_DEL_PARTITION
:
5553 /* payload is struct blkpg_partition */
5556 /* Unknown opcode */
5557 ret
= -TARGET_EINVAL
;
5561 /* Read and convert blkpg->data */
5562 arg
= (abi_long
)(uintptr_t)host_blkpg
->data
;
5563 target_size
= thunk_type_size(part_arg_type
, 0);
5564 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5566 ret
= -TARGET_EFAULT
;
5569 thunk_convert(&host_part
, argptr
, part_arg_type
, THUNK_HOST
);
5570 unlock_user(argptr
, arg
, 0);
5572 /* Swizzle the data pointer to our local copy and call! */
5573 host_blkpg
->data
= &host_part
;
5574 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_blkpg
));
5580 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5581 int fd
, int cmd
, abi_long arg
)
5583 const argtype
*arg_type
= ie
->arg_type
;
5584 const StructEntry
*se
;
5585 const argtype
*field_types
;
5586 const int *dst_offsets
, *src_offsets
;
5589 abi_ulong
*target_rt_dev_ptr
;
5590 unsigned long *host_rt_dev_ptr
;
5594 assert(ie
->access
== IOC_W
);
5595 assert(*arg_type
== TYPE_PTR
);
5597 assert(*arg_type
== TYPE_STRUCT
);
5598 target_size
= thunk_type_size(arg_type
, 0);
5599 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5601 return -TARGET_EFAULT
;
5604 assert(*arg_type
== (int)STRUCT_rtentry
);
5605 se
= struct_entries
+ *arg_type
++;
5606 assert(se
->convert
[0] == NULL
);
5607 /* convert struct here to be able to catch rt_dev string */
5608 field_types
= se
->field_types
;
5609 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
5610 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
5611 for (i
= 0; i
< se
->nb_fields
; i
++) {
5612 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
5613 assert(*field_types
== TYPE_PTRVOID
);
5614 target_rt_dev_ptr
= (abi_ulong
*)(argptr
+ src_offsets
[i
]);
5615 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
5616 if (*target_rt_dev_ptr
!= 0) {
5617 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
5618 tswapal(*target_rt_dev_ptr
));
5619 if (!*host_rt_dev_ptr
) {
5620 unlock_user(argptr
, arg
, 0);
5621 return -TARGET_EFAULT
;
5624 *host_rt_dev_ptr
= 0;
5629 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
5630 argptr
+ src_offsets
[i
],
5631 field_types
, THUNK_HOST
);
5633 unlock_user(argptr
, arg
, 0);
5635 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5636 if (*host_rt_dev_ptr
!= 0) {
5637 unlock_user((void *)*host_rt_dev_ptr
,
5638 *target_rt_dev_ptr
, 0);
5643 static abi_long
do_ioctl_kdsigaccept(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5644 int fd
, int cmd
, abi_long arg
)
5646 int sig
= target_to_host_signal(arg
);
5647 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, sig
));
5651 static abi_long
do_ioctl_tiocgptpeer(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5652 int fd
, int cmd
, abi_long arg
)
5654 int flags
= target_to_host_bitmask(arg
, fcntl_flags_tbl
);
5655 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, flags
));
5659 static IOCTLEntry ioctl_entries
[] = {
5660 #define IOCTL(cmd, access, ...) \
5661 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5662 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5663 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
5664 #define IOCTL_IGNORE(cmd) \
5665 { TARGET_ ## cmd, 0, #cmd },
5670 /* ??? Implement proper locking for ioctls. */
5671 /* do_ioctl() Must return target values and target errnos. */
5672 static abi_long
do_ioctl(int fd
, int cmd
, abi_long arg
)
5674 const IOCTLEntry
*ie
;
5675 const argtype
*arg_type
;
5677 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
5683 if (ie
->target_cmd
== 0) {
5684 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
5685 return -TARGET_ENOSYS
;
5687 if (ie
->target_cmd
== cmd
)
5691 arg_type
= ie
->arg_type
;
5693 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd
, ie
->name
);
5696 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
5697 } else if (!ie
->host_cmd
) {
5698 /* Some architectures define BSD ioctls in their headers
5699 that are not implemented in Linux. */
5700 return -TARGET_ENOSYS
;
5703 switch(arg_type
[0]) {
5706 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
));
5710 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, arg
));
5714 target_size
= thunk_type_size(arg_type
, 0);
5715 switch(ie
->access
) {
5717 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5718 if (!is_error(ret
)) {
5719 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5721 return -TARGET_EFAULT
;
5722 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5723 unlock_user(argptr
, arg
, target_size
);
5727 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5729 return -TARGET_EFAULT
;
5730 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5731 unlock_user(argptr
, arg
, 0);
5732 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5736 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5738 return -TARGET_EFAULT
;
5739 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5740 unlock_user(argptr
, arg
, 0);
5741 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5742 if (!is_error(ret
)) {
5743 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5745 return -TARGET_EFAULT
;
5746 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5747 unlock_user(argptr
, arg
, target_size
);
5753 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5754 (long)cmd
, arg_type
[0]);
5755 ret
= -TARGET_ENOSYS
;
5761 static const bitmask_transtbl iflag_tbl
[] = {
5762 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
5763 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
5764 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
5765 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
5766 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
5767 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
5768 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
5769 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
5770 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
5771 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
5772 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
5773 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
5774 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
5775 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
5779 static const bitmask_transtbl oflag_tbl
[] = {
5780 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
5781 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
5782 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
5783 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
5784 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
5785 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
5786 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
5787 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
5788 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
5789 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
5790 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
5791 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
5792 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
5793 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
5794 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
5795 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
5796 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
5797 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
5798 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
5799 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
5800 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
5801 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
5802 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
5803 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
5807 static const bitmask_transtbl cflag_tbl
[] = {
5808 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
5809 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
5810 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
5811 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
5812 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
5813 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
5814 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
5815 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
5816 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
5817 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
5818 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
5819 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
5820 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
5821 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
5822 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
5823 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
5824 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
5825 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
5826 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
5827 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
5828 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
5829 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
5830 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
5831 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
5832 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
5833 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
5834 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
5835 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
5836 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
5837 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
5838 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
5842 static const bitmask_transtbl lflag_tbl
[] = {
5843 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
5844 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
5845 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
5846 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
5847 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
5848 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
5849 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
5850 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
5851 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
5852 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
5853 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
5854 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
5855 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
5856 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
5857 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
5861 static void target_to_host_termios (void *dst
, const void *src
)
5863 struct host_termios
*host
= dst
;
5864 const struct target_termios
*target
= src
;
5867 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
5869 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
5871 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
5873 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
5874 host
->c_line
= target
->c_line
;
5876 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
5877 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
5878 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
5879 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
5880 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
5881 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
5882 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
5883 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
5884 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
5885 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
5886 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
5887 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
5888 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
5889 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
5890 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
5891 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
5892 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
5893 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
5896 static void host_to_target_termios (void *dst
, const void *src
)
5898 struct target_termios
*target
= dst
;
5899 const struct host_termios
*host
= src
;
5902 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
5904 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
5906 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
5908 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
5909 target
->c_line
= host
->c_line
;
5911 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
5912 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
5913 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
5914 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
5915 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
5916 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
5917 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
5918 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
5919 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
5920 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
5921 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
5922 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
5923 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
5924 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
5925 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
5926 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
5927 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
5928 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
5931 static const StructEntry struct_termios_def
= {
5932 .convert
= { host_to_target_termios
, target_to_host_termios
},
5933 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
5934 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
5937 static bitmask_transtbl mmap_flags_tbl
[] = {
5938 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
5939 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
5940 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
5941 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
,
5942 MAP_ANONYMOUS
, MAP_ANONYMOUS
},
5943 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
,
5944 MAP_GROWSDOWN
, MAP_GROWSDOWN
},
5945 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
,
5946 MAP_DENYWRITE
, MAP_DENYWRITE
},
5947 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
,
5948 MAP_EXECUTABLE
, MAP_EXECUTABLE
},
5949 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
5950 { TARGET_MAP_NORESERVE
, TARGET_MAP_NORESERVE
,
5951 MAP_NORESERVE
, MAP_NORESERVE
},
5952 { TARGET_MAP_HUGETLB
, TARGET_MAP_HUGETLB
, MAP_HUGETLB
, MAP_HUGETLB
},
5953 /* MAP_STACK had been ignored by the kernel for quite some time.
5954 Recognize it for the target insofar as we do not want to pass
5955 it through to the host. */
5956 { TARGET_MAP_STACK
, TARGET_MAP_STACK
, 0, 0 },
5960 #if defined(TARGET_I386)
5962 /* NOTE: there is really one LDT for all the threads */
5963 static uint8_t *ldt_table
;
5965 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
5972 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
5973 if (size
> bytecount
)
5975 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
5977 return -TARGET_EFAULT
;
5978 /* ??? Should this by byteswapped? */
5979 memcpy(p
, ldt_table
, size
);
5980 unlock_user(p
, ptr
, size
);
5984 /* XXX: add locking support */
5985 static abi_long
write_ldt(CPUX86State
*env
,
5986 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
5988 struct target_modify_ldt_ldt_s ldt_info
;
5989 struct target_modify_ldt_ldt_s
*target_ldt_info
;
5990 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
5991 int seg_not_present
, useable
, lm
;
5992 uint32_t *lp
, entry_1
, entry_2
;
5994 if (bytecount
!= sizeof(ldt_info
))
5995 return -TARGET_EINVAL
;
5996 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
5997 return -TARGET_EFAULT
;
5998 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
5999 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
6000 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
6001 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
6002 unlock_user_struct(target_ldt_info
, ptr
, 0);
6004 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
6005 return -TARGET_EINVAL
;
6006 seg_32bit
= ldt_info
.flags
& 1;
6007 contents
= (ldt_info
.flags
>> 1) & 3;
6008 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
6009 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
6010 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
6011 useable
= (ldt_info
.flags
>> 6) & 1;
6015 lm
= (ldt_info
.flags
>> 7) & 1;
6017 if (contents
== 3) {
6019 return -TARGET_EINVAL
;
6020 if (seg_not_present
== 0)
6021 return -TARGET_EINVAL
;
6023 /* allocate the LDT */
6025 env
->ldt
.base
= target_mmap(0,
6026 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
6027 PROT_READ
|PROT_WRITE
,
6028 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
6029 if (env
->ldt
.base
== -1)
6030 return -TARGET_ENOMEM
;
6031 memset(g2h(env
->ldt
.base
), 0,
6032 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
6033 env
->ldt
.limit
= 0xffff;
6034 ldt_table
= g2h(env
->ldt
.base
);
6037 /* NOTE: same code as Linux kernel */
6038 /* Allow LDTs to be cleared by the user. */
6039 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6042 read_exec_only
== 1 &&
6044 limit_in_pages
== 0 &&
6045 seg_not_present
== 1 &&
6053 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6054 (ldt_info
.limit
& 0x0ffff);
6055 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6056 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6057 (ldt_info
.limit
& 0xf0000) |
6058 ((read_exec_only
^ 1) << 9) |
6060 ((seg_not_present
^ 1) << 15) |
6062 (limit_in_pages
<< 23) |
6066 entry_2
|= (useable
<< 20);
6068 /* Install the new entry ... */
6070 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
6071 lp
[0] = tswap32(entry_1
);
6072 lp
[1] = tswap32(entry_2
);
6076 /* specific and weird i386 syscalls */
6077 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
6078 unsigned long bytecount
)
6084 ret
= read_ldt(ptr
, bytecount
);
6087 ret
= write_ldt(env
, ptr
, bytecount
, 1);
6090 ret
= write_ldt(env
, ptr
, bytecount
, 0);
6093 ret
= -TARGET_ENOSYS
;
6099 #if defined(TARGET_I386) && defined(TARGET_ABI32)
6100 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6102 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
6103 struct target_modify_ldt_ldt_s ldt_info
;
6104 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6105 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
6106 int seg_not_present
, useable
, lm
;
6107 uint32_t *lp
, entry_1
, entry_2
;
6110 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6111 if (!target_ldt_info
)
6112 return -TARGET_EFAULT
;
6113 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
6114 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
6115 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
6116 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
6117 if (ldt_info
.entry_number
== -1) {
6118 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
6119 if (gdt_table
[i
] == 0) {
6120 ldt_info
.entry_number
= i
;
6121 target_ldt_info
->entry_number
= tswap32(i
);
6126 unlock_user_struct(target_ldt_info
, ptr
, 1);
6128 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
6129 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
6130 return -TARGET_EINVAL
;
6131 seg_32bit
= ldt_info
.flags
& 1;
6132 contents
= (ldt_info
.flags
>> 1) & 3;
6133 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
6134 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
6135 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
6136 useable
= (ldt_info
.flags
>> 6) & 1;
6140 lm
= (ldt_info
.flags
>> 7) & 1;
6143 if (contents
== 3) {
6144 if (seg_not_present
== 0)
6145 return -TARGET_EINVAL
;
6148 /* NOTE: same code as Linux kernel */
6149 /* Allow LDTs to be cleared by the user. */
6150 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6151 if ((contents
== 0 &&
6152 read_exec_only
== 1 &&
6154 limit_in_pages
== 0 &&
6155 seg_not_present
== 1 &&
6163 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6164 (ldt_info
.limit
& 0x0ffff);
6165 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6166 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6167 (ldt_info
.limit
& 0xf0000) |
6168 ((read_exec_only
^ 1) << 9) |
6170 ((seg_not_present
^ 1) << 15) |
6172 (limit_in_pages
<< 23) |
6177 /* Install the new entry ... */
6179 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
6180 lp
[0] = tswap32(entry_1
);
6181 lp
[1] = tswap32(entry_2
);
6185 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6187 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6188 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
6189 uint32_t base_addr
, limit
, flags
;
6190 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
6191 int seg_not_present
, useable
, lm
;
6192 uint32_t *lp
, entry_1
, entry_2
;
6194 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6195 if (!target_ldt_info
)
6196 return -TARGET_EFAULT
;
6197 idx
= tswap32(target_ldt_info
->entry_number
);
6198 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
6199 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
6200 unlock_user_struct(target_ldt_info
, ptr
, 1);
6201 return -TARGET_EINVAL
;
6203 lp
= (uint32_t *)(gdt_table
+ idx
);
6204 entry_1
= tswap32(lp
[0]);
6205 entry_2
= tswap32(lp
[1]);
6207 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
6208 contents
= (entry_2
>> 10) & 3;
6209 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
6210 seg_32bit
= (entry_2
>> 22) & 1;
6211 limit_in_pages
= (entry_2
>> 23) & 1;
6212 useable
= (entry_2
>> 20) & 1;
6216 lm
= (entry_2
>> 21) & 1;
6218 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
6219 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
6220 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
6221 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
6222 base_addr
= (entry_1
>> 16) |
6223 (entry_2
& 0xff000000) |
6224 ((entry_2
& 0xff) << 16);
6225 target_ldt_info
->base_addr
= tswapal(base_addr
);
6226 target_ldt_info
->limit
= tswap32(limit
);
6227 target_ldt_info
->flags
= tswap32(flags
);
6228 unlock_user_struct(target_ldt_info
, ptr
, 1);
6231 #endif /* TARGET_I386 && TARGET_ABI32 */
6233 #ifndef TARGET_ABI32
6234 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
6241 case TARGET_ARCH_SET_GS
:
6242 case TARGET_ARCH_SET_FS
:
6243 if (code
== TARGET_ARCH_SET_GS
)
6247 cpu_x86_load_seg(env
, idx
, 0);
6248 env
->segs
[idx
].base
= addr
;
6250 case TARGET_ARCH_GET_GS
:
6251 case TARGET_ARCH_GET_FS
:
6252 if (code
== TARGET_ARCH_GET_GS
)
6256 val
= env
->segs
[idx
].base
;
6257 if (put_user(val
, addr
, abi_ulong
))
6258 ret
= -TARGET_EFAULT
;
6261 ret
= -TARGET_EINVAL
;
6268 #endif /* defined(TARGET_I386) */
6270 #define NEW_STACK_SIZE 0x40000
6273 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
6276 pthread_mutex_t mutex
;
6277 pthread_cond_t cond
;
6280 abi_ulong child_tidptr
;
6281 abi_ulong parent_tidptr
;
6285 static void *clone_func(void *arg
)
6287 new_thread_info
*info
= arg
;
6292 rcu_register_thread();
6293 tcg_register_thread();
6295 cpu
= ENV_GET_CPU(env
);
6297 ts
= (TaskState
*)cpu
->opaque
;
6298 info
->tid
= gettid();
6300 if (info
->child_tidptr
)
6301 put_user_u32(info
->tid
, info
->child_tidptr
);
6302 if (info
->parent_tidptr
)
6303 put_user_u32(info
->tid
, info
->parent_tidptr
);
6304 /* Enable signals. */
6305 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
6306 /* Signal to the parent that we're ready. */
6307 pthread_mutex_lock(&info
->mutex
);
6308 pthread_cond_broadcast(&info
->cond
);
6309 pthread_mutex_unlock(&info
->mutex
);
6310 /* Wait until the parent has finished initializing the tls state. */
6311 pthread_mutex_lock(&clone_lock
);
6312 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
);
6349 /* we create a new CPU instance. */
6350 new_env
= cpu_copy(env
);
6351 /* Init regs that differ from the parent. */
6352 cpu_clone_regs(new_env
, newsp
);
6353 new_cpu
= ENV_GET_CPU(new_env
);
6354 new_cpu
->opaque
= ts
;
6355 ts
->bprm
= parent_ts
->bprm
;
6356 ts
->info
= parent_ts
->info
;
6357 ts
->signal_mask
= parent_ts
->signal_mask
;
6359 if (flags
& CLONE_CHILD_CLEARTID
) {
6360 ts
->child_tidptr
= child_tidptr
;
6363 if (flags
& CLONE_SETTLS
) {
6364 cpu_set_tls (new_env
, newtls
);
6367 /* Grab a mutex so that thread setup appears atomic. */
6368 pthread_mutex_lock(&clone_lock
);
6370 memset(&info
, 0, sizeof(info
));
6371 pthread_mutex_init(&info
.mutex
, NULL
);
6372 pthread_mutex_lock(&info
.mutex
);
6373 pthread_cond_init(&info
.cond
, NULL
);
6375 if (flags
& CLONE_CHILD_SETTID
) {
6376 info
.child_tidptr
= child_tidptr
;
6378 if (flags
& CLONE_PARENT_SETTID
) {
6379 info
.parent_tidptr
= parent_tidptr
;
6382 ret
= pthread_attr_init(&attr
);
6383 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
6384 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
6385 /* It is not safe to deliver signals until the child has finished
6386 initializing, so temporarily block all signals. */
6387 sigfillset(&sigmask
);
6388 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
6390 /* If this is our first additional thread, we need to ensure we
6391 * generate code for parallel execution and flush old translations.
6393 if (!parallel_cpus
) {
6394 parallel_cpus
= true;
6398 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
6399 /* TODO: Free new CPU state if thread creation failed. */
6401 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
6402 pthread_attr_destroy(&attr
);
6404 /* Wait for the child to initialize. */
6405 pthread_cond_wait(&info
.cond
, &info
.mutex
);
6410 pthread_mutex_unlock(&info
.mutex
);
6411 pthread_cond_destroy(&info
.cond
);
6412 pthread_mutex_destroy(&info
.mutex
);
6413 pthread_mutex_unlock(&clone_lock
);
6415 /* if no CLONE_VM, we consider it is a fork */
6416 if (flags
& CLONE_INVALID_FORK_FLAGS
) {
6417 return -TARGET_EINVAL
;
6420 /* We can't support custom termination signals */
6421 if ((flags
& CSIGNAL
) != TARGET_SIGCHLD
) {
6422 return -TARGET_EINVAL
;
6425 if (block_signals()) {
6426 return -TARGET_ERESTARTSYS
;
6432 /* Child Process. */
6433 cpu_clone_regs(env
, newsp
);
6435 /* There is a race condition here. The parent process could
6436 theoretically read the TID in the child process before the child
6437 tid is set. This would require using either ptrace
6438 (not implemented) or having *_tidptr to point at a shared memory
6439 mapping. We can't repeat the spinlock hack used above because
6440 the child process gets its own copy of the lock. */
6441 if (flags
& CLONE_CHILD_SETTID
)
6442 put_user_u32(gettid(), child_tidptr
);
6443 if (flags
& CLONE_PARENT_SETTID
)
6444 put_user_u32(gettid(), parent_tidptr
);
6445 ts
= (TaskState
*)cpu
->opaque
;
6446 if (flags
& CLONE_SETTLS
)
6447 cpu_set_tls (env
, newtls
);
6448 if (flags
& CLONE_CHILD_CLEARTID
)
6449 ts
->child_tidptr
= child_tidptr
;
6457 /* warning : doesn't handle linux specific flags... */
6458 static int target_to_host_fcntl_cmd(int cmd
)
6461 case TARGET_F_DUPFD
:
6462 case TARGET_F_GETFD
:
6463 case TARGET_F_SETFD
:
6464 case TARGET_F_GETFL
:
6465 case TARGET_F_SETFL
:
6467 case TARGET_F_GETLK
:
6469 case TARGET_F_SETLK
:
6471 case TARGET_F_SETLKW
:
6473 case TARGET_F_GETOWN
:
6475 case TARGET_F_SETOWN
:
6477 case TARGET_F_GETSIG
:
6479 case TARGET_F_SETSIG
:
6481 #if TARGET_ABI_BITS == 32
6482 case TARGET_F_GETLK64
:
6484 case TARGET_F_SETLK64
:
6486 case TARGET_F_SETLKW64
:
6489 case TARGET_F_SETLEASE
:
6491 case TARGET_F_GETLEASE
:
6493 #ifdef F_DUPFD_CLOEXEC
6494 case TARGET_F_DUPFD_CLOEXEC
:
6495 return F_DUPFD_CLOEXEC
;
6497 case TARGET_F_NOTIFY
:
6500 case TARGET_F_GETOWN_EX
:
6504 case TARGET_F_SETOWN_EX
:
6508 case TARGET_F_SETPIPE_SZ
:
6509 return F_SETPIPE_SZ
;
6510 case TARGET_F_GETPIPE_SZ
:
6511 return F_GETPIPE_SZ
;
6514 return -TARGET_EINVAL
;
6516 return -TARGET_EINVAL
;
6519 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
6520 static const bitmask_transtbl flock_tbl
[] = {
6521 TRANSTBL_CONVERT(F_RDLCK
),
6522 TRANSTBL_CONVERT(F_WRLCK
),
6523 TRANSTBL_CONVERT(F_UNLCK
),
6524 TRANSTBL_CONVERT(F_EXLCK
),
6525 TRANSTBL_CONVERT(F_SHLCK
),
6529 static inline abi_long
copy_from_user_flock(struct flock64
*fl
,
6530 abi_ulong target_flock_addr
)
6532 struct target_flock
*target_fl
;
6535 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6536 return -TARGET_EFAULT
;
6539 __get_user(l_type
, &target_fl
->l_type
);
6540 fl
->l_type
= target_to_host_bitmask(l_type
, flock_tbl
);
6541 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6542 __get_user(fl
->l_start
, &target_fl
->l_start
);
6543 __get_user(fl
->l_len
, &target_fl
->l_len
);
6544 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6545 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6549 static inline abi_long
copy_to_user_flock(abi_ulong target_flock_addr
,
6550 const struct flock64
*fl
)
6552 struct target_flock
*target_fl
;
6555 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6556 return -TARGET_EFAULT
;
6559 l_type
= host_to_target_bitmask(fl
->l_type
, flock_tbl
);
6560 __put_user(l_type
, &target_fl
->l_type
);
6561 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6562 __put_user(fl
->l_start
, &target_fl
->l_start
);
6563 __put_user(fl
->l_len
, &target_fl
->l_len
);
6564 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6565 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6569 typedef abi_long
from_flock64_fn(struct flock64
*fl
, abi_ulong target_addr
);
6570 typedef abi_long
to_flock64_fn(abi_ulong target_addr
, const struct flock64
*fl
);
6572 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6573 static inline abi_long
copy_from_user_eabi_flock64(struct flock64
*fl
,
6574 abi_ulong target_flock_addr
)
6576 struct target_eabi_flock64
*target_fl
;
6579 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6580 return -TARGET_EFAULT
;
6583 __get_user(l_type
, &target_fl
->l_type
);
6584 fl
->l_type
= target_to_host_bitmask(l_type
, flock_tbl
);
6585 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6586 __get_user(fl
->l_start
, &target_fl
->l_start
);
6587 __get_user(fl
->l_len
, &target_fl
->l_len
);
6588 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6589 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6593 static inline abi_long
copy_to_user_eabi_flock64(abi_ulong target_flock_addr
,
6594 const struct flock64
*fl
)
6596 struct target_eabi_flock64
*target_fl
;
6599 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6600 return -TARGET_EFAULT
;
6603 l_type
= host_to_target_bitmask(fl
->l_type
, flock_tbl
);
6604 __put_user(l_type
, &target_fl
->l_type
);
6605 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6606 __put_user(fl
->l_start
, &target_fl
->l_start
);
6607 __put_user(fl
->l_len
, &target_fl
->l_len
);
6608 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6609 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6614 static inline abi_long
copy_from_user_flock64(struct flock64
*fl
,
6615 abi_ulong target_flock_addr
)
6617 struct target_flock64
*target_fl
;
6620 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6621 return -TARGET_EFAULT
;
6624 __get_user(l_type
, &target_fl
->l_type
);
6625 fl
->l_type
= target_to_host_bitmask(l_type
, flock_tbl
);
6626 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6627 __get_user(fl
->l_start
, &target_fl
->l_start
);
6628 __get_user(fl
->l_len
, &target_fl
->l_len
);
6629 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6630 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6634 static inline abi_long
copy_to_user_flock64(abi_ulong target_flock_addr
,
6635 const struct flock64
*fl
)
6637 struct target_flock64
*target_fl
;
6640 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6641 return -TARGET_EFAULT
;
6644 l_type
= host_to_target_bitmask(fl
->l_type
, flock_tbl
);
6645 __put_user(l_type
, &target_fl
->l_type
);
6646 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6647 __put_user(fl
->l_start
, &target_fl
->l_start
);
6648 __put_user(fl
->l_len
, &target_fl
->l_len
);
6649 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6650 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6654 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
6656 struct flock64 fl64
;
6658 struct f_owner_ex fox
;
6659 struct target_f_owner_ex
*target_fox
;
6662 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
6664 if (host_cmd
== -TARGET_EINVAL
)
6668 case TARGET_F_GETLK
:
6669 ret
= copy_from_user_flock(&fl64
, arg
);
6673 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6675 ret
= copy_to_user_flock(arg
, &fl64
);
6679 case TARGET_F_SETLK
:
6680 case TARGET_F_SETLKW
:
6681 ret
= copy_from_user_flock(&fl64
, arg
);
6685 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6688 case TARGET_F_GETLK64
:
6689 ret
= copy_from_user_flock64(&fl64
, arg
);
6693 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6695 ret
= copy_to_user_flock64(arg
, &fl64
);
6698 case TARGET_F_SETLK64
:
6699 case TARGET_F_SETLKW64
:
6700 ret
= copy_from_user_flock64(&fl64
, arg
);
6704 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6707 case TARGET_F_GETFL
:
6708 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
6710 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
6714 case TARGET_F_SETFL
:
6715 ret
= get_errno(safe_fcntl(fd
, host_cmd
,
6716 target_to_host_bitmask(arg
,
6721 case TARGET_F_GETOWN_EX
:
6722 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
6724 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
6725 return -TARGET_EFAULT
;
6726 target_fox
->type
= tswap32(fox
.type
);
6727 target_fox
->pid
= tswap32(fox
.pid
);
6728 unlock_user_struct(target_fox
, arg
, 1);
6734 case TARGET_F_SETOWN_EX
:
6735 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
6736 return -TARGET_EFAULT
;
6737 fox
.type
= tswap32(target_fox
->type
);
6738 fox
.pid
= tswap32(target_fox
->pid
);
6739 unlock_user_struct(target_fox
, arg
, 0);
6740 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
6744 case TARGET_F_SETOWN
:
6745 case TARGET_F_GETOWN
:
6746 case TARGET_F_SETSIG
:
6747 case TARGET_F_GETSIG
:
6748 case TARGET_F_SETLEASE
:
6749 case TARGET_F_GETLEASE
:
6750 case TARGET_F_SETPIPE_SZ
:
6751 case TARGET_F_GETPIPE_SZ
:
6752 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
6756 ret
= get_errno(safe_fcntl(fd
, cmd
, arg
));
6764 static inline int high2lowuid(int uid
)
6772 static inline int high2lowgid(int gid
)
6780 static inline int low2highuid(int uid
)
6782 if ((int16_t)uid
== -1)
6788 static inline int low2highgid(int gid
)
6790 if ((int16_t)gid
== -1)
6795 static inline int tswapid(int id
)
6800 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
6802 #else /* !USE_UID16 */
6803 static inline int high2lowuid(int uid
)
6807 static inline int high2lowgid(int gid
)
6811 static inline int low2highuid(int uid
)
6815 static inline int low2highgid(int gid
)
6819 static inline int tswapid(int id
)
6824 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
6826 #endif /* USE_UID16 */
6828 /* We must do direct syscalls for setting UID/GID, because we want to
6829 * implement the Linux system call semantics of "change only for this thread",
6830 * not the libc/POSIX semantics of "change for all threads in process".
6831 * (See http://ewontfix.com/17/ for more details.)
6832 * We use the 32-bit version of the syscalls if present; if it is not
6833 * then either the host architecture supports 32-bit UIDs natively with
6834 * the standard syscall, or the 16-bit UID is the best we can do.
6836 #ifdef __NR_setuid32
6837 #define __NR_sys_setuid __NR_setuid32
6839 #define __NR_sys_setuid __NR_setuid
6841 #ifdef __NR_setgid32
6842 #define __NR_sys_setgid __NR_setgid32
6844 #define __NR_sys_setgid __NR_setgid
6846 #ifdef __NR_setresuid32
6847 #define __NR_sys_setresuid __NR_setresuid32
6849 #define __NR_sys_setresuid __NR_setresuid
6851 #ifdef __NR_setresgid32
6852 #define __NR_sys_setresgid __NR_setresgid32
6854 #define __NR_sys_setresgid __NR_setresgid
6857 _syscall1(int, sys_setuid
, uid_t
, uid
)
6858 _syscall1(int, sys_setgid
, gid_t
, gid
)
6859 _syscall3(int, sys_setresuid
, uid_t
, ruid
, uid_t
, euid
, uid_t
, suid
)
6860 _syscall3(int, sys_setresgid
, gid_t
, rgid
, gid_t
, egid
, gid_t
, sgid
)
6862 void syscall_init(void)
6865 const argtype
*arg_type
;
6869 thunk_init(STRUCT_MAX
);
6871 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
6872 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
6873 #include "syscall_types.h"
6875 #undef STRUCT_SPECIAL
6877 /* Build target_to_host_errno_table[] table from
6878 * host_to_target_errno_table[]. */
6879 for (i
= 0; i
< ERRNO_TABLE_SIZE
; i
++) {
6880 target_to_host_errno_table
[host_to_target_errno_table
[i
]] = i
;
6883 /* we patch the ioctl size if necessary. We rely on the fact that
6884 no ioctl has all the bits at '1' in the size field */
6886 while (ie
->target_cmd
!= 0) {
6887 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
6888 TARGET_IOC_SIZEMASK
) {
6889 arg_type
= ie
->arg_type
;
6890 if (arg_type
[0] != TYPE_PTR
) {
6891 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
6896 size
= thunk_type_size(arg_type
, 0);
6897 ie
->target_cmd
= (ie
->target_cmd
&
6898 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
6899 (size
<< TARGET_IOC_SIZESHIFT
);
6902 /* automatic consistency check if same arch */
6903 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6904 (defined(__x86_64__) && defined(TARGET_X86_64))
6905 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
6906 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
6907 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
6914 #if TARGET_ABI_BITS == 32
6915 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
6917 #ifdef TARGET_WORDS_BIGENDIAN
6918 return ((uint64_t)word0
<< 32) | word1
;
6920 return ((uint64_t)word1
<< 32) | word0
;
6923 #else /* TARGET_ABI_BITS == 32 */
6924 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
6928 #endif /* TARGET_ABI_BITS != 32 */
6930 #ifdef TARGET_NR_truncate64
6931 static inline abi_long
target_truncate64(void *cpu_env
, const char *arg1
,
6936 if (regpairs_aligned(cpu_env
, TARGET_NR_truncate64
)) {
6940 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
6944 #ifdef TARGET_NR_ftruncate64
6945 static inline abi_long
target_ftruncate64(void *cpu_env
, abi_long arg1
,
6950 if (regpairs_aligned(cpu_env
, TARGET_NR_ftruncate64
)) {
6954 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
6958 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
6959 abi_ulong target_addr
)
6961 struct target_timespec
*target_ts
;
6963 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1))
6964 return -TARGET_EFAULT
;
6965 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
6966 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
6967 unlock_user_struct(target_ts
, target_addr
, 0);
6971 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
6972 struct timespec
*host_ts
)
6974 struct target_timespec
*target_ts
;
6976 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0))
6977 return -TARGET_EFAULT
;
6978 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
6979 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
6980 unlock_user_struct(target_ts
, target_addr
, 1);
6984 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_itspec
,
6985 abi_ulong target_addr
)
6987 struct target_itimerspec
*target_itspec
;
6989 if (!lock_user_struct(VERIFY_READ
, target_itspec
, target_addr
, 1)) {
6990 return -TARGET_EFAULT
;
6993 host_itspec
->it_interval
.tv_sec
=
6994 tswapal(target_itspec
->it_interval
.tv_sec
);
6995 host_itspec
->it_interval
.tv_nsec
=
6996 tswapal(target_itspec
->it_interval
.tv_nsec
);
6997 host_itspec
->it_value
.tv_sec
= tswapal(target_itspec
->it_value
.tv_sec
);
6998 host_itspec
->it_value
.tv_nsec
= tswapal(target_itspec
->it_value
.tv_nsec
);
7000 unlock_user_struct(target_itspec
, target_addr
, 1);
7004 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
7005 struct itimerspec
*host_its
)
7007 struct target_itimerspec
*target_itspec
;
7009 if (!lock_user_struct(VERIFY_WRITE
, target_itspec
, target_addr
, 0)) {
7010 return -TARGET_EFAULT
;
7013 target_itspec
->it_interval
.tv_sec
= tswapal(host_its
->it_interval
.tv_sec
);
7014 target_itspec
->it_interval
.tv_nsec
= tswapal(host_its
->it_interval
.tv_nsec
);
7016 target_itspec
->it_value
.tv_sec
= tswapal(host_its
->it_value
.tv_sec
);
7017 target_itspec
->it_value
.tv_nsec
= tswapal(host_its
->it_value
.tv_nsec
);
7019 unlock_user_struct(target_itspec
, target_addr
, 0);
7023 static inline abi_long
target_to_host_timex(struct timex
*host_tx
,
7024 abi_long target_addr
)
7026 struct target_timex
*target_tx
;
7028 if (!lock_user_struct(VERIFY_READ
, target_tx
, target_addr
, 1)) {
7029 return -TARGET_EFAULT
;
7032 __get_user(host_tx
->modes
, &target_tx
->modes
);
7033 __get_user(host_tx
->offset
, &target_tx
->offset
);
7034 __get_user(host_tx
->freq
, &target_tx
->freq
);
7035 __get_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7036 __get_user(host_tx
->esterror
, &target_tx
->esterror
);
7037 __get_user(host_tx
->status
, &target_tx
->status
);
7038 __get_user(host_tx
->constant
, &target_tx
->constant
);
7039 __get_user(host_tx
->precision
, &target_tx
->precision
);
7040 __get_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7041 __get_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7042 __get_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7043 __get_user(host_tx
->tick
, &target_tx
->tick
);
7044 __get_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7045 __get_user(host_tx
->jitter
, &target_tx
->jitter
);
7046 __get_user(host_tx
->shift
, &target_tx
->shift
);
7047 __get_user(host_tx
->stabil
, &target_tx
->stabil
);
7048 __get_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7049 __get_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7050 __get_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7051 __get_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7052 __get_user(host_tx
->tai
, &target_tx
->tai
);
7054 unlock_user_struct(target_tx
, target_addr
, 0);
7058 static inline abi_long
host_to_target_timex(abi_long target_addr
,
7059 struct timex
*host_tx
)
7061 struct target_timex
*target_tx
;
7063 if (!lock_user_struct(VERIFY_WRITE
, target_tx
, target_addr
, 0)) {
7064 return -TARGET_EFAULT
;
7067 __put_user(host_tx
->modes
, &target_tx
->modes
);
7068 __put_user(host_tx
->offset
, &target_tx
->offset
);
7069 __put_user(host_tx
->freq
, &target_tx
->freq
);
7070 __put_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7071 __put_user(host_tx
->esterror
, &target_tx
->esterror
);
7072 __put_user(host_tx
->status
, &target_tx
->status
);
7073 __put_user(host_tx
->constant
, &target_tx
->constant
);
7074 __put_user(host_tx
->precision
, &target_tx
->precision
);
7075 __put_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7076 __put_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7077 __put_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7078 __put_user(host_tx
->tick
, &target_tx
->tick
);
7079 __put_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7080 __put_user(host_tx
->jitter
, &target_tx
->jitter
);
7081 __put_user(host_tx
->shift
, &target_tx
->shift
);
7082 __put_user(host_tx
->stabil
, &target_tx
->stabil
);
7083 __put_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7084 __put_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7085 __put_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7086 __put_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7087 __put_user(host_tx
->tai
, &target_tx
->tai
);
7089 unlock_user_struct(target_tx
, target_addr
, 1);
7094 static inline abi_long
target_to_host_sigevent(struct sigevent
*host_sevp
,
7095 abi_ulong target_addr
)
7097 struct target_sigevent
*target_sevp
;
7099 if (!lock_user_struct(VERIFY_READ
, target_sevp
, target_addr
, 1)) {
7100 return -TARGET_EFAULT
;
7103 /* This union is awkward on 64 bit systems because it has a 32 bit
7104 * integer and a pointer in it; we follow the conversion approach
7105 * used for handling sigval types in signal.c so the guest should get
7106 * the correct value back even if we did a 64 bit byteswap and it's
7107 * using the 32 bit integer.
7109 host_sevp
->sigev_value
.sival_ptr
=
7110 (void *)(uintptr_t)tswapal(target_sevp
->sigev_value
.sival_ptr
);
7111 host_sevp
->sigev_signo
=
7112 target_to_host_signal(tswap32(target_sevp
->sigev_signo
));
7113 host_sevp
->sigev_notify
= tswap32(target_sevp
->sigev_notify
);
7114 host_sevp
->_sigev_un
._tid
= tswap32(target_sevp
->_sigev_un
._tid
);
7116 unlock_user_struct(target_sevp
, target_addr
, 1);
7120 #if defined(TARGET_NR_mlockall)
7121 static inline int target_to_host_mlockall_arg(int arg
)
7125 if (arg
& TARGET_MLOCKALL_MCL_CURRENT
) {
7126 result
|= MCL_CURRENT
;
7128 if (arg
& TARGET_MLOCKALL_MCL_FUTURE
) {
7129 result
|= MCL_FUTURE
;
7135 static inline abi_long
host_to_target_stat64(void *cpu_env
,
7136 abi_ulong target_addr
,
7137 struct stat
*host_st
)
7139 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
7140 if (((CPUARMState
*)cpu_env
)->eabi
) {
7141 struct target_eabi_stat64
*target_st
;
7143 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7144 return -TARGET_EFAULT
;
7145 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
7146 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7147 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7148 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7149 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7151 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7152 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7153 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7154 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7155 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7156 __put_user(host_st
->st_size
, &target_st
->st_size
);
7157 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7158 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7159 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7160 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7161 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7162 unlock_user_struct(target_st
, target_addr
, 1);
7166 #if defined(TARGET_HAS_STRUCT_STAT64)
7167 struct target_stat64
*target_st
;
7169 struct target_stat
*target_st
;
7172 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7173 return -TARGET_EFAULT
;
7174 memset(target_st
, 0, sizeof(*target_st
));
7175 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7176 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7177 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7178 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7180 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7181 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7182 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7183 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7184 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7185 /* XXX: better use of kernel struct */
7186 __put_user(host_st
->st_size
, &target_st
->st_size
);
7187 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7188 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7189 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7190 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7191 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7192 unlock_user_struct(target_st
, target_addr
, 1);
7198 /* ??? Using host futex calls even when target atomic operations
7199 are not really atomic probably breaks things. However implementing
7200 futexes locally would make futexes shared between multiple processes
7201 tricky. However they're probably useless because guest atomic
7202 operations won't work either. */
7203 static int do_futex(target_ulong uaddr
, int op
, int val
, target_ulong timeout
,
7204 target_ulong uaddr2
, int val3
)
7206 struct timespec ts
, *pts
;
7209 /* ??? We assume FUTEX_* constants are the same on both host
7211 #ifdef FUTEX_CMD_MASK
7212 base_op
= op
& FUTEX_CMD_MASK
;
7218 case FUTEX_WAIT_BITSET
:
7221 target_to_host_timespec(pts
, timeout
);
7225 return get_errno(safe_futex(g2h(uaddr
), op
, tswap32(val
),
7228 return get_errno(safe_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
7230 return get_errno(safe_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
7232 case FUTEX_CMP_REQUEUE
:
7234 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
7235 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
7236 But the prototype takes a `struct timespec *'; insert casts
7237 to satisfy the compiler. We do not need to tswap TIMEOUT
7238 since it's not compared to guest memory. */
7239 pts
= (struct timespec
*)(uintptr_t) timeout
;
7240 return get_errno(safe_futex(g2h(uaddr
), op
, val
, pts
,
7242 (base_op
== FUTEX_CMP_REQUEUE
7246 return -TARGET_ENOSYS
;
7249 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7250 static abi_long
do_name_to_handle_at(abi_long dirfd
, abi_long pathname
,
7251 abi_long handle
, abi_long mount_id
,
7254 struct file_handle
*target_fh
;
7255 struct file_handle
*fh
;
7259 unsigned int size
, total_size
;
7261 if (get_user_s32(size
, handle
)) {
7262 return -TARGET_EFAULT
;
7265 name
= lock_user_string(pathname
);
7267 return -TARGET_EFAULT
;
7270 total_size
= sizeof(struct file_handle
) + size
;
7271 target_fh
= lock_user(VERIFY_WRITE
, handle
, total_size
, 0);
7273 unlock_user(name
, pathname
, 0);
7274 return -TARGET_EFAULT
;
7277 fh
= g_malloc0(total_size
);
7278 fh
->handle_bytes
= size
;
7280 ret
= get_errno(name_to_handle_at(dirfd
, path(name
), fh
, &mid
, flags
));
7281 unlock_user(name
, pathname
, 0);
7283 /* man name_to_handle_at(2):
7284 * Other than the use of the handle_bytes field, the caller should treat
7285 * the file_handle structure as an opaque data type
7288 memcpy(target_fh
, fh
, total_size
);
7289 target_fh
->handle_bytes
= tswap32(fh
->handle_bytes
);
7290 target_fh
->handle_type
= tswap32(fh
->handle_type
);
7292 unlock_user(target_fh
, handle
, total_size
);
7294 if (put_user_s32(mid
, mount_id
)) {
7295 return -TARGET_EFAULT
;
7303 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7304 static abi_long
do_open_by_handle_at(abi_long mount_fd
, abi_long handle
,
7307 struct file_handle
*target_fh
;
7308 struct file_handle
*fh
;
7309 unsigned int size
, total_size
;
7312 if (get_user_s32(size
, handle
)) {
7313 return -TARGET_EFAULT
;
7316 total_size
= sizeof(struct file_handle
) + size
;
7317 target_fh
= lock_user(VERIFY_READ
, handle
, total_size
, 1);
7319 return -TARGET_EFAULT
;
7322 fh
= g_memdup(target_fh
, total_size
);
7323 fh
->handle_bytes
= size
;
7324 fh
->handle_type
= tswap32(target_fh
->handle_type
);
7326 ret
= get_errno(open_by_handle_at(mount_fd
, fh
,
7327 target_to_host_bitmask(flags
, fcntl_flags_tbl
)));
7331 unlock_user(target_fh
, handle
, total_size
);
7337 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7339 /* signalfd siginfo conversion */
7342 host_to_target_signalfd_siginfo(struct signalfd_siginfo
*tinfo
,
7343 const struct signalfd_siginfo
*info
)
7345 int sig
= host_to_target_signal(info
->ssi_signo
);
7347 /* linux/signalfd.h defines a ssi_addr_lsb
7348 * not defined in sys/signalfd.h but used by some kernels
7351 #ifdef BUS_MCEERR_AO
7352 if (tinfo
->ssi_signo
== SIGBUS
&&
7353 (tinfo
->ssi_code
== BUS_MCEERR_AR
||
7354 tinfo
->ssi_code
== BUS_MCEERR_AO
)) {
7355 uint16_t *ssi_addr_lsb
= (uint16_t *)(&info
->ssi_addr
+ 1);
7356 uint16_t *tssi_addr_lsb
= (uint16_t *)(&tinfo
->ssi_addr
+ 1);
7357 *tssi_addr_lsb
= tswap16(*ssi_addr_lsb
);
7361 tinfo
->ssi_signo
= tswap32(sig
);
7362 tinfo
->ssi_errno
= tswap32(tinfo
->ssi_errno
);
7363 tinfo
->ssi_code
= tswap32(info
->ssi_code
);
7364 tinfo
->ssi_pid
= tswap32(info
->ssi_pid
);
7365 tinfo
->ssi_uid
= tswap32(info
->ssi_uid
);
7366 tinfo
->ssi_fd
= tswap32(info
->ssi_fd
);
7367 tinfo
->ssi_tid
= tswap32(info
->ssi_tid
);
7368 tinfo
->ssi_band
= tswap32(info
->ssi_band
);
7369 tinfo
->ssi_overrun
= tswap32(info
->ssi_overrun
);
7370 tinfo
->ssi_trapno
= tswap32(info
->ssi_trapno
);
7371 tinfo
->ssi_status
= tswap32(info
->ssi_status
);
7372 tinfo
->ssi_int
= tswap32(info
->ssi_int
);
7373 tinfo
->ssi_ptr
= tswap64(info
->ssi_ptr
);
7374 tinfo
->ssi_utime
= tswap64(info
->ssi_utime
);
7375 tinfo
->ssi_stime
= tswap64(info
->ssi_stime
);
7376 tinfo
->ssi_addr
= tswap64(info
->ssi_addr
);
7379 static abi_long
host_to_target_data_signalfd(void *buf
, size_t len
)
7383 for (i
= 0; i
< len
; i
+= sizeof(struct signalfd_siginfo
)) {
7384 host_to_target_signalfd_siginfo(buf
+ i
, buf
+ i
);
7390 static TargetFdTrans target_signalfd_trans
= {
7391 .host_to_target_data
= host_to_target_data_signalfd
,
7394 static abi_long
do_signalfd4(int fd
, abi_long mask
, int flags
)
7397 target_sigset_t
*target_mask
;
7401 if (flags
& ~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
)) {
7402 return -TARGET_EINVAL
;
7404 if (!lock_user_struct(VERIFY_READ
, target_mask
, mask
, 1)) {
7405 return -TARGET_EFAULT
;
7408 target_to_host_sigset(&host_mask
, target_mask
);
7410 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
7412 ret
= get_errno(signalfd(fd
, &host_mask
, host_flags
));
7414 fd_trans_register(ret
, &target_signalfd_trans
);
7417 unlock_user_struct(target_mask
, mask
, 0);
7423 /* Map host to target signal numbers for the wait family of syscalls.
7424 Assume all other status bits are the same. */
7425 int host_to_target_waitstatus(int status
)
7427 if (WIFSIGNALED(status
)) {
7428 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
7430 if (WIFSTOPPED(status
)) {
7431 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
7437 static int open_self_cmdline(void *cpu_env
, int fd
)
7439 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
7440 struct linux_binprm
*bprm
= ((TaskState
*)cpu
->opaque
)->bprm
;
7443 for (i
= 0; i
< bprm
->argc
; i
++) {
7444 size_t len
= strlen(bprm
->argv
[i
]) + 1;
7446 if (write(fd
, bprm
->argv
[i
], len
) != len
) {
7454 static int open_self_maps(void *cpu_env
, int fd
)
7456 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
7457 TaskState
*ts
= cpu
->opaque
;
7463 fp
= fopen("/proc/self/maps", "r");
7468 while ((read
= getline(&line
, &len
, fp
)) != -1) {
7469 int fields
, dev_maj
, dev_min
, inode
;
7470 uint64_t min
, max
, offset
;
7471 char flag_r
, flag_w
, flag_x
, flag_p
;
7472 char path
[512] = "";
7473 fields
= sscanf(line
, "%"PRIx64
"-%"PRIx64
" %c%c%c%c %"PRIx64
" %x:%x %d"
7474 " %512s", &min
, &max
, &flag_r
, &flag_w
, &flag_x
,
7475 &flag_p
, &offset
, &dev_maj
, &dev_min
, &inode
, path
);
7477 if ((fields
< 10) || (fields
> 11)) {
7480 if (h2g_valid(min
)) {
7481 int flags
= page_get_flags(h2g(min
));
7482 max
= h2g_valid(max
- 1) ? max
: (uintptr_t)g2h(GUEST_ADDR_MAX
) + 1;
7483 if (page_check_range(h2g(min
), max
- min
, flags
) == -1) {
7486 if (h2g(min
) == ts
->info
->stack_limit
) {
7487 pstrcpy(path
, sizeof(path
), " [stack]");
7489 dprintf(fd
, TARGET_ABI_FMT_lx
"-" TARGET_ABI_FMT_lx
7490 " %c%c%c%c %08" PRIx64
" %02x:%02x %d %s%s\n",
7491 h2g(min
), h2g(max
- 1) + 1, flag_r
, flag_w
,
7492 flag_x
, flag_p
, offset
, dev_maj
, dev_min
, inode
,
7493 path
[0] ? " " : "", path
);
7503 static int open_self_stat(void *cpu_env
, int fd
)
7505 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
7506 TaskState
*ts
= cpu
->opaque
;
7507 abi_ulong start_stack
= ts
->info
->start_stack
;
7510 for (i
= 0; i
< 44; i
++) {
7518 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
7519 } else if (i
== 1) {
7521 snprintf(buf
, sizeof(buf
), "(%s) ", ts
->bprm
->argv
[0]);
7522 } else if (i
== 27) {
7525 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
7527 /* for the rest, there is MasterCard */
7528 snprintf(buf
, sizeof(buf
), "0%c", i
== 43 ? '\n' : ' ');
7532 if (write(fd
, buf
, len
) != len
) {
7540 static int open_self_auxv(void *cpu_env
, int fd
)
7542 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
7543 TaskState
*ts
= cpu
->opaque
;
7544 abi_ulong auxv
= ts
->info
->saved_auxv
;
7545 abi_ulong len
= ts
->info
->auxv_len
;
7549 * Auxiliary vector is stored in target process stack.
7550 * read in whole auxv vector and copy it to file
7552 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
7556 r
= write(fd
, ptr
, len
);
7563 lseek(fd
, 0, SEEK_SET
);
7564 unlock_user(ptr
, auxv
, len
);
7570 static int is_proc_myself(const char *filename
, const char *entry
)
7572 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
7573 filename
+= strlen("/proc/");
7574 if (!strncmp(filename
, "self/", strlen("self/"))) {
7575 filename
+= strlen("self/");
7576 } else if (*filename
>= '1' && *filename
<= '9') {
7578 snprintf(myself
, sizeof(myself
), "%d/", getpid());
7579 if (!strncmp(filename
, myself
, strlen(myself
))) {
7580 filename
+= strlen(myself
);
7587 if (!strcmp(filename
, entry
)) {
7594 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7595 static int is_proc(const char *filename
, const char *entry
)
7597 return strcmp(filename
, entry
) == 0;
7600 static int open_net_route(void *cpu_env
, int fd
)
7607 fp
= fopen("/proc/net/route", "r");
7614 read
= getline(&line
, &len
, fp
);
7615 dprintf(fd
, "%s", line
);
7619 while ((read
= getline(&line
, &len
, fp
)) != -1) {
7621 uint32_t dest
, gw
, mask
;
7622 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
7623 sscanf(line
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7624 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
7625 &mask
, &mtu
, &window
, &irtt
);
7626 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7627 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
7628 metric
, tswap32(mask
), mtu
, window
, irtt
);
7638 static int do_openat(void *cpu_env
, int dirfd
, const char *pathname
, int flags
, mode_t mode
)
7641 const char *filename
;
7642 int (*fill
)(void *cpu_env
, int fd
);
7643 int (*cmp
)(const char *s1
, const char *s2
);
7645 const struct fake_open
*fake_open
;
7646 static const struct fake_open fakes
[] = {
7647 { "maps", open_self_maps
, is_proc_myself
},
7648 { "stat", open_self_stat
, is_proc_myself
},
7649 { "auxv", open_self_auxv
, is_proc_myself
},
7650 { "cmdline", open_self_cmdline
, is_proc_myself
},
7651 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7652 { "/proc/net/route", open_net_route
, is_proc
},
7654 { NULL
, NULL
, NULL
}
7657 if (is_proc_myself(pathname
, "exe")) {
7658 int execfd
= qemu_getauxval(AT_EXECFD
);
7659 return execfd
? execfd
: safe_openat(dirfd
, exec_path
, flags
, mode
);
7662 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
7663 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
7668 if (fake_open
->filename
) {
7670 char filename
[PATH_MAX
];
7673 /* create temporary file to map stat to */
7674 tmpdir
= getenv("TMPDIR");
7677 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
7678 fd
= mkstemp(filename
);
7684 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
7690 lseek(fd
, 0, SEEK_SET
);
7695 return safe_openat(dirfd
, path(pathname
), flags
, mode
);
7698 #define TIMER_MAGIC 0x0caf0000
7699 #define TIMER_MAGIC_MASK 0xffff0000
7701 /* Convert QEMU provided timer ID back to internal 16bit index format */
7702 static target_timer_t
get_timer_id(abi_long arg
)
7704 target_timer_t timerid
= arg
;
7706 if ((timerid
& TIMER_MAGIC_MASK
) != TIMER_MAGIC
) {
7707 return -TARGET_EINVAL
;
7712 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
7713 return -TARGET_EINVAL
;
7719 static abi_long
swap_data_eventfd(void *buf
, size_t len
)
7721 uint64_t *counter
= buf
;
7724 if (len
< sizeof(uint64_t)) {
7728 for (i
= 0; i
< len
; i
+= sizeof(uint64_t)) {
7729 *counter
= tswap64(*counter
);
7736 static TargetFdTrans target_eventfd_trans
= {
7737 .host_to_target_data
= swap_data_eventfd
,
7738 .target_to_host_data
= swap_data_eventfd
,
7741 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
7742 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
7743 defined(__NR_inotify_init1))
7744 static abi_long
host_to_target_data_inotify(void *buf
, size_t len
)
7746 struct inotify_event
*ev
;
7750 for (i
= 0; i
< len
; i
+= sizeof(struct inotify_event
) + name_len
) {
7751 ev
= (struct inotify_event
*)((char *)buf
+ i
);
7754 ev
->wd
= tswap32(ev
->wd
);
7755 ev
->mask
= tswap32(ev
->mask
);
7756 ev
->cookie
= tswap32(ev
->cookie
);
7757 ev
->len
= tswap32(name_len
);
7763 static TargetFdTrans target_inotify_trans
= {
7764 .host_to_target_data
= host_to_target_data_inotify
,
7768 static int target_to_host_cpu_mask(unsigned long *host_mask
,
7770 abi_ulong target_addr
,
7773 unsigned target_bits
= sizeof(abi_ulong
) * 8;
7774 unsigned host_bits
= sizeof(*host_mask
) * 8;
7775 abi_ulong
*target_mask
;
7778 assert(host_size
>= target_size
);
7780 target_mask
= lock_user(VERIFY_READ
, target_addr
, target_size
, 1);
7782 return -TARGET_EFAULT
;
7784 memset(host_mask
, 0, host_size
);
7786 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
7787 unsigned bit
= i
* target_bits
;
7790 __get_user(val
, &target_mask
[i
]);
7791 for (j
= 0; j
< target_bits
; j
++, bit
++) {
7792 if (val
& (1UL << j
)) {
7793 host_mask
[bit
/ host_bits
] |= 1UL << (bit
% host_bits
);
7798 unlock_user(target_mask
, target_addr
, 0);
7802 static int host_to_target_cpu_mask(const unsigned long *host_mask
,
7804 abi_ulong target_addr
,
7807 unsigned target_bits
= sizeof(abi_ulong
) * 8;
7808 unsigned host_bits
= sizeof(*host_mask
) * 8;
7809 abi_ulong
*target_mask
;
7812 assert(host_size
>= target_size
);
7814 target_mask
= lock_user(VERIFY_WRITE
, target_addr
, target_size
, 0);
7816 return -TARGET_EFAULT
;
7819 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
7820 unsigned bit
= i
* target_bits
;
7823 for (j
= 0; j
< target_bits
; j
++, bit
++) {
7824 if (host_mask
[bit
/ host_bits
] & (1UL << (bit
% host_bits
))) {
7828 __put_user(val
, &target_mask
[i
]);
7831 unlock_user(target_mask
, target_addr
, target_size
);
7835 /* do_syscall() should always have a single exit point at the end so
7836 that actions, such as logging of syscall results, can be performed.
7837 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
7838 abi_long
do_syscall(void *cpu_env
, int num
, abi_long arg1
,
7839 abi_long arg2
, abi_long arg3
, abi_long arg4
,
7840 abi_long arg5
, abi_long arg6
, abi_long arg7
,
7843 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
7849 #if defined(DEBUG_ERESTARTSYS)
7850 /* Debug-only code for exercising the syscall-restart code paths
7851 * in the per-architecture cpu main loops: restart every syscall
7852 * the guest makes once before letting it through.
7859 return -TARGET_ERESTARTSYS
;
7865 gemu_log("syscall %d", num
);
7867 trace_guest_user_syscall(cpu
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
);
7869 print_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7872 case TARGET_NR_exit
:
7873 /* In old applications this may be used to implement _exit(2).
7874 However in threaded applictions it is used for thread termination,
7875 and _exit_group is used for application termination.
7876 Do thread termination if we have more then one thread. */
7878 if (block_signals()) {
7879 ret
= -TARGET_ERESTARTSYS
;
7885 if (CPU_NEXT(first_cpu
)) {
7888 /* Remove the CPU from the list. */
7889 QTAILQ_REMOVE(&cpus
, cpu
, node
);
7894 if (ts
->child_tidptr
) {
7895 put_user_u32(0, ts
->child_tidptr
);
7896 sys_futex(g2h(ts
->child_tidptr
), FUTEX_WAKE
, INT_MAX
,
7900 object_unref(OBJECT(cpu
));
7902 rcu_unregister_thread();
7910 gdb_exit(cpu_env
, arg1
);
7912 ret
= 0; /* avoid warning */
7914 case TARGET_NR_read
:
7918 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
7920 ret
= get_errno(safe_read(arg1
, p
, arg3
));
7922 fd_trans_host_to_target_data(arg1
)) {
7923 ret
= fd_trans_host_to_target_data(arg1
)(p
, ret
);
7925 unlock_user(p
, arg2
, ret
);
7928 case TARGET_NR_write
:
7929 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
7931 if (fd_trans_target_to_host_data(arg1
)) {
7932 void *copy
= g_malloc(arg3
);
7933 memcpy(copy
, p
, arg3
);
7934 ret
= fd_trans_target_to_host_data(arg1
)(copy
, arg3
);
7936 ret
= get_errno(safe_write(arg1
, copy
, ret
));
7940 ret
= get_errno(safe_write(arg1
, p
, arg3
));
7942 unlock_user(p
, arg2
, 0);
7944 #ifdef TARGET_NR_open
7945 case TARGET_NR_open
:
7946 if (!(p
= lock_user_string(arg1
)))
7948 ret
= get_errno(do_openat(cpu_env
, AT_FDCWD
, p
,
7949 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
7951 fd_trans_unregister(ret
);
7952 unlock_user(p
, arg1
, 0);
7955 case TARGET_NR_openat
:
7956 if (!(p
= lock_user_string(arg2
)))
7958 ret
= get_errno(do_openat(cpu_env
, arg1
, p
,
7959 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
7961 fd_trans_unregister(ret
);
7962 unlock_user(p
, arg2
, 0);
7964 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7965 case TARGET_NR_name_to_handle_at
:
7966 ret
= do_name_to_handle_at(arg1
, arg2
, arg3
, arg4
, arg5
);
7969 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7970 case TARGET_NR_open_by_handle_at
:
7971 ret
= do_open_by_handle_at(arg1
, arg2
, arg3
);
7972 fd_trans_unregister(ret
);
7975 case TARGET_NR_close
:
7976 fd_trans_unregister(arg1
);
7977 ret
= get_errno(close(arg1
));
7982 #ifdef TARGET_NR_fork
7983 case TARGET_NR_fork
:
7984 ret
= get_errno(do_fork(cpu_env
, TARGET_SIGCHLD
, 0, 0, 0, 0));
7987 #ifdef TARGET_NR_waitpid
7988 case TARGET_NR_waitpid
:
7991 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, 0));
7992 if (!is_error(ret
) && arg2
&& ret
7993 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
7998 #ifdef TARGET_NR_waitid
7999 case TARGET_NR_waitid
:
8003 ret
= get_errno(safe_waitid(arg1
, arg2
, &info
, arg4
, NULL
));
8004 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
8005 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
8007 host_to_target_siginfo(p
, &info
);
8008 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
8013 #ifdef TARGET_NR_creat /* not on alpha */
8014 case TARGET_NR_creat
:
8015 if (!(p
= lock_user_string(arg1
)))
8017 ret
= get_errno(creat(p
, arg2
));
8018 fd_trans_unregister(ret
);
8019 unlock_user(p
, arg1
, 0);
8022 #ifdef TARGET_NR_link
8023 case TARGET_NR_link
:
8026 p
= lock_user_string(arg1
);
8027 p2
= lock_user_string(arg2
);
8029 ret
= -TARGET_EFAULT
;
8031 ret
= get_errno(link(p
, p2
));
8032 unlock_user(p2
, arg2
, 0);
8033 unlock_user(p
, arg1
, 0);
8037 #if defined(TARGET_NR_linkat)
8038 case TARGET_NR_linkat
:
8043 p
= lock_user_string(arg2
);
8044 p2
= lock_user_string(arg4
);
8046 ret
= -TARGET_EFAULT
;
8048 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
8049 unlock_user(p
, arg2
, 0);
8050 unlock_user(p2
, arg4
, 0);
8054 #ifdef TARGET_NR_unlink
8055 case TARGET_NR_unlink
:
8056 if (!(p
= lock_user_string(arg1
)))
8058 ret
= get_errno(unlink(p
));
8059 unlock_user(p
, arg1
, 0);
8062 #if defined(TARGET_NR_unlinkat)
8063 case TARGET_NR_unlinkat
:
8064 if (!(p
= lock_user_string(arg2
)))
8066 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
8067 unlock_user(p
, arg2
, 0);
8070 case TARGET_NR_execve
:
8072 char **argp
, **envp
;
8075 abi_ulong guest_argp
;
8076 abi_ulong guest_envp
;
8083 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
8084 if (get_user_ual(addr
, gp
))
8092 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
8093 if (get_user_ual(addr
, gp
))
8100 argp
= g_new0(char *, argc
+ 1);
8101 envp
= g_new0(char *, envc
+ 1);
8103 for (gp
= guest_argp
, q
= argp
; gp
;
8104 gp
+= sizeof(abi_ulong
), q
++) {
8105 if (get_user_ual(addr
, gp
))
8109 if (!(*q
= lock_user_string(addr
)))
8111 total_size
+= strlen(*q
) + 1;
8115 for (gp
= guest_envp
, q
= envp
; gp
;
8116 gp
+= sizeof(abi_ulong
), q
++) {
8117 if (get_user_ual(addr
, gp
))
8121 if (!(*q
= lock_user_string(addr
)))
8123 total_size
+= strlen(*q
) + 1;
8127 if (!(p
= lock_user_string(arg1
)))
8129 /* Although execve() is not an interruptible syscall it is
8130 * a special case where we must use the safe_syscall wrapper:
8131 * if we allow a signal to happen before we make the host
8132 * syscall then we will 'lose' it, because at the point of
8133 * execve the process leaves QEMU's control. So we use the
8134 * safe syscall wrapper to ensure that we either take the
8135 * signal as a guest signal, or else it does not happen
8136 * before the execve completes and makes it the other
8137 * program's problem.
8139 ret
= get_errno(safe_execve(p
, argp
, envp
));
8140 unlock_user(p
, arg1
, 0);
8145 ret
= -TARGET_EFAULT
;
8148 for (gp
= guest_argp
, q
= argp
; *q
;
8149 gp
+= sizeof(abi_ulong
), q
++) {
8150 if (get_user_ual(addr
, gp
)
8153 unlock_user(*q
, addr
, 0);
8155 for (gp
= guest_envp
, q
= envp
; *q
;
8156 gp
+= sizeof(abi_ulong
), q
++) {
8157 if (get_user_ual(addr
, gp
)
8160 unlock_user(*q
, addr
, 0);
8167 case TARGET_NR_chdir
:
8168 if (!(p
= lock_user_string(arg1
)))
8170 ret
= get_errno(chdir(p
));
8171 unlock_user(p
, arg1
, 0);
8173 #ifdef TARGET_NR_time
8174 case TARGET_NR_time
:
8177 ret
= get_errno(time(&host_time
));
8180 && put_user_sal(host_time
, arg1
))
8185 #ifdef TARGET_NR_mknod
8186 case TARGET_NR_mknod
:
8187 if (!(p
= lock_user_string(arg1
)))
8189 ret
= get_errno(mknod(p
, arg2
, arg3
));
8190 unlock_user(p
, arg1
, 0);
8193 #if defined(TARGET_NR_mknodat)
8194 case TARGET_NR_mknodat
:
8195 if (!(p
= lock_user_string(arg2
)))
8197 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
8198 unlock_user(p
, arg2
, 0);
8201 #ifdef TARGET_NR_chmod
8202 case TARGET_NR_chmod
:
8203 if (!(p
= lock_user_string(arg1
)))
8205 ret
= get_errno(chmod(p
, arg2
));
8206 unlock_user(p
, arg1
, 0);
8209 #ifdef TARGET_NR_break
8210 case TARGET_NR_break
:
8213 #ifdef TARGET_NR_oldstat
8214 case TARGET_NR_oldstat
:
8217 case TARGET_NR_lseek
:
8218 ret
= get_errno(lseek(arg1
, arg2
, arg3
));
8220 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
8221 /* Alpha specific */
8222 case TARGET_NR_getxpid
:
8223 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = getppid();
8224 ret
= get_errno(getpid());
8227 #ifdef TARGET_NR_getpid
8228 case TARGET_NR_getpid
:
8229 ret
= get_errno(getpid());
8232 case TARGET_NR_mount
:
8234 /* need to look at the data field */
8238 p
= lock_user_string(arg1
);
8246 p2
= lock_user_string(arg2
);
8249 unlock_user(p
, arg1
, 0);
8255 p3
= lock_user_string(arg3
);
8258 unlock_user(p
, arg1
, 0);
8260 unlock_user(p2
, arg2
, 0);
8267 /* FIXME - arg5 should be locked, but it isn't clear how to
8268 * do that since it's not guaranteed to be a NULL-terminated
8272 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
);
8274 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(arg5
));
8276 ret
= get_errno(ret
);
8279 unlock_user(p
, arg1
, 0);
8281 unlock_user(p2
, arg2
, 0);
8283 unlock_user(p3
, arg3
, 0);
8287 #ifdef TARGET_NR_umount
8288 case TARGET_NR_umount
:
8289 if (!(p
= lock_user_string(arg1
)))
8291 ret
= get_errno(umount(p
));
8292 unlock_user(p
, arg1
, 0);
8295 #ifdef TARGET_NR_stime /* not on alpha */
8296 case TARGET_NR_stime
:
8299 if (get_user_sal(host_time
, arg1
))
8301 ret
= get_errno(stime(&host_time
));
8305 case TARGET_NR_ptrace
:
8307 #ifdef TARGET_NR_alarm /* not on alpha */
8308 case TARGET_NR_alarm
:
8312 #ifdef TARGET_NR_oldfstat
8313 case TARGET_NR_oldfstat
:
8316 #ifdef TARGET_NR_pause /* not on alpha */
8317 case TARGET_NR_pause
:
8318 if (!block_signals()) {
8319 sigsuspend(&((TaskState
*)cpu
->opaque
)->signal_mask
);
8321 ret
= -TARGET_EINTR
;
8324 #ifdef TARGET_NR_utime
8325 case TARGET_NR_utime
:
8327 struct utimbuf tbuf
, *host_tbuf
;
8328 struct target_utimbuf
*target_tbuf
;
8330 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
8332 tbuf
.actime
= tswapal(target_tbuf
->actime
);
8333 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
8334 unlock_user_struct(target_tbuf
, arg2
, 0);
8339 if (!(p
= lock_user_string(arg1
)))
8341 ret
= get_errno(utime(p
, host_tbuf
));
8342 unlock_user(p
, arg1
, 0);
8346 #ifdef TARGET_NR_utimes
8347 case TARGET_NR_utimes
:
8349 struct timeval
*tvp
, tv
[2];
8351 if (copy_from_user_timeval(&tv
[0], arg2
)
8352 || copy_from_user_timeval(&tv
[1],
8353 arg2
+ sizeof(struct target_timeval
)))
8359 if (!(p
= lock_user_string(arg1
)))
8361 ret
= get_errno(utimes(p
, tvp
));
8362 unlock_user(p
, arg1
, 0);
8366 #if defined(TARGET_NR_futimesat)
8367 case TARGET_NR_futimesat
:
8369 struct timeval
*tvp
, tv
[2];
8371 if (copy_from_user_timeval(&tv
[0], arg3
)
8372 || copy_from_user_timeval(&tv
[1],
8373 arg3
+ sizeof(struct target_timeval
)))
8379 if (!(p
= lock_user_string(arg2
)))
8381 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
8382 unlock_user(p
, arg2
, 0);
8386 #ifdef TARGET_NR_stty
8387 case TARGET_NR_stty
:
8390 #ifdef TARGET_NR_gtty
8391 case TARGET_NR_gtty
:
8394 #ifdef TARGET_NR_access
8395 case TARGET_NR_access
:
8396 if (!(p
= lock_user_string(arg1
)))
8398 ret
= get_errno(access(path(p
), arg2
));
8399 unlock_user(p
, arg1
, 0);
8402 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
8403 case TARGET_NR_faccessat
:
8404 if (!(p
= lock_user_string(arg2
)))
8406 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
8407 unlock_user(p
, arg2
, 0);
8410 #ifdef TARGET_NR_nice /* not on alpha */
8411 case TARGET_NR_nice
:
8412 ret
= get_errno(nice(arg1
));
8415 #ifdef TARGET_NR_ftime
8416 case TARGET_NR_ftime
:
8419 case TARGET_NR_sync
:
8423 #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
8424 case TARGET_NR_syncfs
:
8425 ret
= get_errno(syncfs(arg1
));
8428 case TARGET_NR_kill
:
8429 ret
= get_errno(safe_kill(arg1
, target_to_host_signal(arg2
)));
8431 #ifdef TARGET_NR_rename
8432 case TARGET_NR_rename
:
8435 p
= lock_user_string(arg1
);
8436 p2
= lock_user_string(arg2
);
8438 ret
= -TARGET_EFAULT
;
8440 ret
= get_errno(rename(p
, p2
));
8441 unlock_user(p2
, arg2
, 0);
8442 unlock_user(p
, arg1
, 0);
8446 #if defined(TARGET_NR_renameat)
8447 case TARGET_NR_renameat
:
8450 p
= lock_user_string(arg2
);
8451 p2
= lock_user_string(arg4
);
8453 ret
= -TARGET_EFAULT
;
8455 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
8456 unlock_user(p2
, arg4
, 0);
8457 unlock_user(p
, arg2
, 0);
8461 #if defined(TARGET_NR_renameat2)
8462 case TARGET_NR_renameat2
:
8465 p
= lock_user_string(arg2
);
8466 p2
= lock_user_string(arg4
);
8468 ret
= -TARGET_EFAULT
;
8470 ret
= get_errno(sys_renameat2(arg1
, p
, arg3
, p2
, arg5
));
8472 unlock_user(p2
, arg4
, 0);
8473 unlock_user(p
, arg2
, 0);
8477 #ifdef TARGET_NR_mkdir
8478 case TARGET_NR_mkdir
:
8479 if (!(p
= lock_user_string(arg1
)))
8481 ret
= get_errno(mkdir(p
, arg2
));
8482 unlock_user(p
, arg1
, 0);
8485 #if defined(TARGET_NR_mkdirat)
8486 case TARGET_NR_mkdirat
:
8487 if (!(p
= lock_user_string(arg2
)))
8489 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
8490 unlock_user(p
, arg2
, 0);
8493 #ifdef TARGET_NR_rmdir
8494 case TARGET_NR_rmdir
:
8495 if (!(p
= lock_user_string(arg1
)))
8497 ret
= get_errno(rmdir(p
));
8498 unlock_user(p
, arg1
, 0);
8502 ret
= get_errno(dup(arg1
));
8504 fd_trans_dup(arg1
, ret
);
8507 #ifdef TARGET_NR_pipe
8508 case TARGET_NR_pipe
:
8509 ret
= do_pipe(cpu_env
, arg1
, 0, 0);
8512 #ifdef TARGET_NR_pipe2
8513 case TARGET_NR_pipe2
:
8514 ret
= do_pipe(cpu_env
, arg1
,
8515 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
8518 case TARGET_NR_times
:
8520 struct target_tms
*tmsp
;
8522 ret
= get_errno(times(&tms
));
8524 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
8527 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
8528 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
8529 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
8530 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
8533 ret
= host_to_target_clock_t(ret
);
8536 #ifdef TARGET_NR_prof
8537 case TARGET_NR_prof
:
8540 #ifdef TARGET_NR_signal
8541 case TARGET_NR_signal
:
8544 case TARGET_NR_acct
:
8546 ret
= get_errno(acct(NULL
));
8548 if (!(p
= lock_user_string(arg1
)))
8550 ret
= get_errno(acct(path(p
)));
8551 unlock_user(p
, arg1
, 0);
8554 #ifdef TARGET_NR_umount2
8555 case TARGET_NR_umount2
:
8556 if (!(p
= lock_user_string(arg1
)))
8558 ret
= get_errno(umount2(p
, arg2
));
8559 unlock_user(p
, arg1
, 0);
8562 #ifdef TARGET_NR_lock
8563 case TARGET_NR_lock
:
8566 case TARGET_NR_ioctl
:
8567 ret
= do_ioctl(arg1
, arg2
, arg3
);
8569 #ifdef TARGET_NR_fcntl
8570 case TARGET_NR_fcntl
:
8571 ret
= do_fcntl(arg1
, arg2
, arg3
);
8574 #ifdef TARGET_NR_mpx
8578 case TARGET_NR_setpgid
:
8579 ret
= get_errno(setpgid(arg1
, arg2
));
8581 #ifdef TARGET_NR_ulimit
8582 case TARGET_NR_ulimit
:
8585 #ifdef TARGET_NR_oldolduname
8586 case TARGET_NR_oldolduname
:
8589 case TARGET_NR_umask
:
8590 ret
= get_errno(umask(arg1
));
8592 case TARGET_NR_chroot
:
8593 if (!(p
= lock_user_string(arg1
)))
8595 ret
= get_errno(chroot(p
));
8596 unlock_user(p
, arg1
, 0);
8598 #ifdef TARGET_NR_ustat
8599 case TARGET_NR_ustat
:
8602 #ifdef TARGET_NR_dup2
8603 case TARGET_NR_dup2
:
8604 ret
= get_errno(dup2(arg1
, arg2
));
8606 fd_trans_dup(arg1
, arg2
);
8610 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
8611 case TARGET_NR_dup3
:
8615 if ((arg3
& ~TARGET_O_CLOEXEC
) != 0) {
8618 host_flags
= target_to_host_bitmask(arg3
, fcntl_flags_tbl
);
8619 ret
= get_errno(dup3(arg1
, arg2
, host_flags
));
8621 fd_trans_dup(arg1
, arg2
);
8626 #ifdef TARGET_NR_getppid /* not on alpha */
8627 case TARGET_NR_getppid
:
8628 ret
= get_errno(getppid());
8631 #ifdef TARGET_NR_getpgrp
8632 case TARGET_NR_getpgrp
:
8633 ret
= get_errno(getpgrp());
8636 case TARGET_NR_setsid
:
8637 ret
= get_errno(setsid());
8639 #ifdef TARGET_NR_sigaction
8640 case TARGET_NR_sigaction
:
8642 #if defined(TARGET_ALPHA)
8643 struct target_sigaction act
, oact
, *pact
= 0;
8644 struct target_old_sigaction
*old_act
;
8646 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
8648 act
._sa_handler
= old_act
->_sa_handler
;
8649 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
8650 act
.sa_flags
= old_act
->sa_flags
;
8651 act
.sa_restorer
= 0;
8652 unlock_user_struct(old_act
, arg2
, 0);
8655 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8656 if (!is_error(ret
) && arg3
) {
8657 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
8659 old_act
->_sa_handler
= oact
._sa_handler
;
8660 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
8661 old_act
->sa_flags
= oact
.sa_flags
;
8662 unlock_user_struct(old_act
, arg3
, 1);
8664 #elif defined(TARGET_MIPS)
8665 struct target_sigaction act
, oact
, *pact
, *old_act
;
8668 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
8670 act
._sa_handler
= old_act
->_sa_handler
;
8671 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
8672 act
.sa_flags
= old_act
->sa_flags
;
8673 unlock_user_struct(old_act
, arg2
, 0);
8679 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8681 if (!is_error(ret
) && arg3
) {
8682 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
8684 old_act
->_sa_handler
= oact
._sa_handler
;
8685 old_act
->sa_flags
= oact
.sa_flags
;
8686 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
8687 old_act
->sa_mask
.sig
[1] = 0;
8688 old_act
->sa_mask
.sig
[2] = 0;
8689 old_act
->sa_mask
.sig
[3] = 0;
8690 unlock_user_struct(old_act
, arg3
, 1);
8693 struct target_old_sigaction
*old_act
;
8694 struct target_sigaction act
, oact
, *pact
;
8696 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
8698 act
._sa_handler
= old_act
->_sa_handler
;
8699 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
8700 act
.sa_flags
= old_act
->sa_flags
;
8701 act
.sa_restorer
= old_act
->sa_restorer
;
8702 unlock_user_struct(old_act
, arg2
, 0);
8707 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8708 if (!is_error(ret
) && arg3
) {
8709 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
8711 old_act
->_sa_handler
= oact
._sa_handler
;
8712 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
8713 old_act
->sa_flags
= oact
.sa_flags
;
8714 old_act
->sa_restorer
= oact
.sa_restorer
;
8715 unlock_user_struct(old_act
, arg3
, 1);
8721 case TARGET_NR_rt_sigaction
:
8723 #if defined(TARGET_ALPHA)
8724 /* For Alpha and SPARC this is a 5 argument syscall, with
8725 * a 'restorer' parameter which must be copied into the
8726 * sa_restorer field of the sigaction struct.
8727 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
8728 * and arg5 is the sigsetsize.
8729 * Alpha also has a separate rt_sigaction struct that it uses
8730 * here; SPARC uses the usual sigaction struct.
8732 struct target_rt_sigaction
*rt_act
;
8733 struct target_sigaction act
, oact
, *pact
= 0;
8735 if (arg4
!= sizeof(target_sigset_t
)) {
8736 ret
= -TARGET_EINVAL
;
8740 if (!lock_user_struct(VERIFY_READ
, rt_act
, arg2
, 1))
8742 act
._sa_handler
= rt_act
->_sa_handler
;
8743 act
.sa_mask
= rt_act
->sa_mask
;
8744 act
.sa_flags
= rt_act
->sa_flags
;
8745 act
.sa_restorer
= arg5
;
8746 unlock_user_struct(rt_act
, arg2
, 0);
8749 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8750 if (!is_error(ret
) && arg3
) {
8751 if (!lock_user_struct(VERIFY_WRITE
, rt_act
, arg3
, 0))
8753 rt_act
->_sa_handler
= oact
._sa_handler
;
8754 rt_act
->sa_mask
= oact
.sa_mask
;
8755 rt_act
->sa_flags
= oact
.sa_flags
;
8756 unlock_user_struct(rt_act
, arg3
, 1);
8760 target_ulong restorer
= arg4
;
8761 target_ulong sigsetsize
= arg5
;
8763 target_ulong sigsetsize
= arg4
;
8765 struct target_sigaction
*act
;
8766 struct target_sigaction
*oact
;
8768 if (sigsetsize
!= sizeof(target_sigset_t
)) {
8769 ret
= -TARGET_EINVAL
;
8773 if (!lock_user_struct(VERIFY_READ
, act
, arg2
, 1)) {
8777 act
->sa_restorer
= restorer
;
8783 if (!lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
8784 ret
= -TARGET_EFAULT
;
8785 goto rt_sigaction_fail
;
8789 ret
= get_errno(do_sigaction(arg1
, act
, oact
));
8792 unlock_user_struct(act
, arg2
, 0);
8794 unlock_user_struct(oact
, arg3
, 1);
8798 #ifdef TARGET_NR_sgetmask /* not on alpha */
8799 case TARGET_NR_sgetmask
:
8802 abi_ulong target_set
;
8803 ret
= do_sigprocmask(0, NULL
, &cur_set
);
8805 host_to_target_old_sigset(&target_set
, &cur_set
);
8811 #ifdef TARGET_NR_ssetmask /* not on alpha */
8812 case TARGET_NR_ssetmask
:
8815 abi_ulong target_set
= arg1
;
8816 target_to_host_old_sigset(&set
, &target_set
);
8817 ret
= do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
8819 host_to_target_old_sigset(&target_set
, &oset
);
8825 #ifdef TARGET_NR_sigprocmask
8826 case TARGET_NR_sigprocmask
:
8828 #if defined(TARGET_ALPHA)
8829 sigset_t set
, oldset
;
8834 case TARGET_SIG_BLOCK
:
8837 case TARGET_SIG_UNBLOCK
:
8840 case TARGET_SIG_SETMASK
:
8844 ret
= -TARGET_EINVAL
;
8848 target_to_host_old_sigset(&set
, &mask
);
8850 ret
= do_sigprocmask(how
, &set
, &oldset
);
8851 if (!is_error(ret
)) {
8852 host_to_target_old_sigset(&mask
, &oldset
);
8854 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0; /* force no error */
8857 sigset_t set
, oldset
, *set_ptr
;
8862 case TARGET_SIG_BLOCK
:
8865 case TARGET_SIG_UNBLOCK
:
8868 case TARGET_SIG_SETMASK
:
8872 ret
= -TARGET_EINVAL
;
8875 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
8877 target_to_host_old_sigset(&set
, p
);
8878 unlock_user(p
, arg2
, 0);
8884 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
8885 if (!is_error(ret
) && arg3
) {
8886 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
8888 host_to_target_old_sigset(p
, &oldset
);
8889 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
8895 case TARGET_NR_rt_sigprocmask
:
8898 sigset_t set
, oldset
, *set_ptr
;
8900 if (arg4
!= sizeof(target_sigset_t
)) {
8901 ret
= -TARGET_EINVAL
;
8907 case TARGET_SIG_BLOCK
:
8910 case TARGET_SIG_UNBLOCK
:
8913 case TARGET_SIG_SETMASK
:
8917 ret
= -TARGET_EINVAL
;
8920 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
8922 target_to_host_sigset(&set
, p
);
8923 unlock_user(p
, arg2
, 0);
8929 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
8930 if (!is_error(ret
) && arg3
) {
8931 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
8933 host_to_target_sigset(p
, &oldset
);
8934 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
8938 #ifdef TARGET_NR_sigpending
8939 case TARGET_NR_sigpending
:
8942 ret
= get_errno(sigpending(&set
));
8943 if (!is_error(ret
)) {
8944 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
8946 host_to_target_old_sigset(p
, &set
);
8947 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
8952 case TARGET_NR_rt_sigpending
:
8956 /* Yes, this check is >, not != like most. We follow the kernel's
8957 * logic and it does it like this because it implements
8958 * NR_sigpending through the same code path, and in that case
8959 * the old_sigset_t is smaller in size.
8961 if (arg2
> sizeof(target_sigset_t
)) {
8962 ret
= -TARGET_EINVAL
;
8966 ret
= get_errno(sigpending(&set
));
8967 if (!is_error(ret
)) {
8968 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
8970 host_to_target_sigset(p
, &set
);
8971 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
8975 #ifdef TARGET_NR_sigsuspend
8976 case TARGET_NR_sigsuspend
:
8978 TaskState
*ts
= cpu
->opaque
;
8979 #if defined(TARGET_ALPHA)
8980 abi_ulong mask
= arg1
;
8981 target_to_host_old_sigset(&ts
->sigsuspend_mask
, &mask
);
8983 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
8985 target_to_host_old_sigset(&ts
->sigsuspend_mask
, p
);
8986 unlock_user(p
, arg1
, 0);
8988 ret
= get_errno(safe_rt_sigsuspend(&ts
->sigsuspend_mask
,
8990 if (ret
!= -TARGET_ERESTARTSYS
) {
8991 ts
->in_sigsuspend
= 1;
8996 case TARGET_NR_rt_sigsuspend
:
8998 TaskState
*ts
= cpu
->opaque
;
9000 if (arg2
!= sizeof(target_sigset_t
)) {
9001 ret
= -TARGET_EINVAL
;
9004 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
9006 target_to_host_sigset(&ts
->sigsuspend_mask
, p
);
9007 unlock_user(p
, arg1
, 0);
9008 ret
= get_errno(safe_rt_sigsuspend(&ts
->sigsuspend_mask
,
9010 if (ret
!= -TARGET_ERESTARTSYS
) {
9011 ts
->in_sigsuspend
= 1;
9015 case TARGET_NR_rt_sigtimedwait
:
9018 struct timespec uts
, *puts
;
9021 if (arg4
!= sizeof(target_sigset_t
)) {
9022 ret
= -TARGET_EINVAL
;
9026 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
9028 target_to_host_sigset(&set
, p
);
9029 unlock_user(p
, arg1
, 0);
9032 target_to_host_timespec(puts
, arg3
);
9036 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
9038 if (!is_error(ret
)) {
9040 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
9045 host_to_target_siginfo(p
, &uinfo
);
9046 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
9048 ret
= host_to_target_signal(ret
);
9052 case TARGET_NR_rt_sigqueueinfo
:
9056 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
9060 target_to_host_siginfo(&uinfo
, p
);
9061 unlock_user(p
, arg3
, 0);
9062 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, arg2
, &uinfo
));
9065 case TARGET_NR_rt_tgsigqueueinfo
:
9069 p
= lock_user(VERIFY_READ
, arg4
, sizeof(target_siginfo_t
), 1);
9073 target_to_host_siginfo(&uinfo
, p
);
9074 unlock_user(p
, arg4
, 0);
9075 ret
= get_errno(sys_rt_tgsigqueueinfo(arg1
, arg2
, arg3
, &uinfo
));
9078 #ifdef TARGET_NR_sigreturn
9079 case TARGET_NR_sigreturn
:
9080 if (block_signals()) {
9081 ret
= -TARGET_ERESTARTSYS
;
9083 ret
= do_sigreturn(cpu_env
);
9087 case TARGET_NR_rt_sigreturn
:
9088 if (block_signals()) {
9089 ret
= -TARGET_ERESTARTSYS
;
9091 ret
= do_rt_sigreturn(cpu_env
);
9094 case TARGET_NR_sethostname
:
9095 if (!(p
= lock_user_string(arg1
)))
9097 ret
= get_errno(sethostname(p
, arg2
));
9098 unlock_user(p
, arg1
, 0);
9100 case TARGET_NR_setrlimit
:
9102 int resource
= target_to_host_resource(arg1
);
9103 struct target_rlimit
*target_rlim
;
9105 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
9107 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
9108 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
9109 unlock_user_struct(target_rlim
, arg2
, 0);
9110 ret
= get_errno(setrlimit(resource
, &rlim
));
9113 case TARGET_NR_getrlimit
:
9115 int resource
= target_to_host_resource(arg1
);
9116 struct target_rlimit
*target_rlim
;
9119 ret
= get_errno(getrlimit(resource
, &rlim
));
9120 if (!is_error(ret
)) {
9121 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
9123 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
9124 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
9125 unlock_user_struct(target_rlim
, arg2
, 1);
9129 case TARGET_NR_getrusage
:
9131 struct rusage rusage
;
9132 ret
= get_errno(getrusage(arg1
, &rusage
));
9133 if (!is_error(ret
)) {
9134 ret
= host_to_target_rusage(arg2
, &rusage
);
9138 case TARGET_NR_gettimeofday
:
9141 ret
= get_errno(gettimeofday(&tv
, NULL
));
9142 if (!is_error(ret
)) {
9143 if (copy_to_user_timeval(arg1
, &tv
))
9148 case TARGET_NR_settimeofday
:
9150 struct timeval tv
, *ptv
= NULL
;
9151 struct timezone tz
, *ptz
= NULL
;
9154 if (copy_from_user_timeval(&tv
, arg1
)) {
9161 if (copy_from_user_timezone(&tz
, arg2
)) {
9167 ret
= get_errno(settimeofday(ptv
, ptz
));
9170 #if defined(TARGET_NR_select)
9171 case TARGET_NR_select
:
9172 #if defined(TARGET_WANT_NI_OLD_SELECT)
9173 /* some architectures used to have old_select here
9174 * but now ENOSYS it.
9176 ret
= -TARGET_ENOSYS
;
9177 #elif defined(TARGET_WANT_OLD_SYS_SELECT)
9178 ret
= do_old_select(arg1
);
9180 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
9184 #ifdef TARGET_NR_pselect6
9185 case TARGET_NR_pselect6
:
9187 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
9188 fd_set rfds
, wfds
, efds
;
9189 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
9190 struct timespec ts
, *ts_ptr
;
9193 * The 6th arg is actually two args smashed together,
9194 * so we cannot use the C library.
9202 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
9203 target_sigset_t
*target_sigset
;
9211 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
9215 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
9219 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
9225 * This takes a timespec, and not a timeval, so we cannot
9226 * use the do_select() helper ...
9229 if (target_to_host_timespec(&ts
, ts_addr
)) {
9237 /* Extract the two packed args for the sigset */
9240 sig
.size
= SIGSET_T_SIZE
;
9242 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
9246 arg_sigset
= tswapal(arg7
[0]);
9247 arg_sigsize
= tswapal(arg7
[1]);
9248 unlock_user(arg7
, arg6
, 0);
9252 if (arg_sigsize
!= sizeof(*target_sigset
)) {
9253 /* Like the kernel, we enforce correct size sigsets */
9254 ret
= -TARGET_EINVAL
;
9257 target_sigset
= lock_user(VERIFY_READ
, arg_sigset
,
9258 sizeof(*target_sigset
), 1);
9259 if (!target_sigset
) {
9262 target_to_host_sigset(&set
, target_sigset
);
9263 unlock_user(target_sigset
, arg_sigset
, 0);
9271 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
9274 if (!is_error(ret
)) {
9275 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
9277 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
9279 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
9282 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
))
9288 #ifdef TARGET_NR_symlink
9289 case TARGET_NR_symlink
:
9292 p
= lock_user_string(arg1
);
9293 p2
= lock_user_string(arg2
);
9295 ret
= -TARGET_EFAULT
;
9297 ret
= get_errno(symlink(p
, p2
));
9298 unlock_user(p2
, arg2
, 0);
9299 unlock_user(p
, arg1
, 0);
9303 #if defined(TARGET_NR_symlinkat)
9304 case TARGET_NR_symlinkat
:
9307 p
= lock_user_string(arg1
);
9308 p2
= lock_user_string(arg3
);
9310 ret
= -TARGET_EFAULT
;
9312 ret
= get_errno(symlinkat(p
, arg2
, p2
));
9313 unlock_user(p2
, arg3
, 0);
9314 unlock_user(p
, arg1
, 0);
9318 #ifdef TARGET_NR_oldlstat
9319 case TARGET_NR_oldlstat
:
9322 #ifdef TARGET_NR_readlink
9323 case TARGET_NR_readlink
:
9326 p
= lock_user_string(arg1
);
9327 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
9329 ret
= -TARGET_EFAULT
;
9331 /* Short circuit this for the magic exe check. */
9332 ret
= -TARGET_EINVAL
;
9333 } else if (is_proc_myself((const char *)p
, "exe")) {
9334 char real
[PATH_MAX
], *temp
;
9335 temp
= realpath(exec_path
, real
);
9336 /* Return value is # of bytes that we wrote to the buffer. */
9338 ret
= get_errno(-1);
9340 /* Don't worry about sign mismatch as earlier mapping
9341 * logic would have thrown a bad address error. */
9342 ret
= MIN(strlen(real
), arg3
);
9343 /* We cannot NUL terminate the string. */
9344 memcpy(p2
, real
, ret
);
9347 ret
= get_errno(readlink(path(p
), p2
, arg3
));
9349 unlock_user(p2
, arg2
, ret
);
9350 unlock_user(p
, arg1
, 0);
9354 #if defined(TARGET_NR_readlinkat)
9355 case TARGET_NR_readlinkat
:
9358 p
= lock_user_string(arg2
);
9359 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
9361 ret
= -TARGET_EFAULT
;
9362 } else if (is_proc_myself((const char *)p
, "exe")) {
9363 char real
[PATH_MAX
], *temp
;
9364 temp
= realpath(exec_path
, real
);
9365 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
9366 snprintf((char *)p2
, arg4
, "%s", real
);
9368 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
9370 unlock_user(p2
, arg3
, ret
);
9371 unlock_user(p
, arg2
, 0);
9375 #ifdef TARGET_NR_uselib
9376 case TARGET_NR_uselib
:
9379 #ifdef TARGET_NR_swapon
9380 case TARGET_NR_swapon
:
9381 if (!(p
= lock_user_string(arg1
)))
9383 ret
= get_errno(swapon(p
, arg2
));
9384 unlock_user(p
, arg1
, 0);
9387 case TARGET_NR_reboot
:
9388 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
9389 /* arg4 must be ignored in all other cases */
9390 p
= lock_user_string(arg4
);
9394 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
9395 unlock_user(p
, arg4
, 0);
9397 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
9400 #ifdef TARGET_NR_readdir
9401 case TARGET_NR_readdir
:
9404 #ifdef TARGET_NR_mmap
9405 case TARGET_NR_mmap
:
9406 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
9407 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
9408 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
9409 || defined(TARGET_S390X)
9412 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
9413 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
9421 unlock_user(v
, arg1
, 0);
9422 ret
= get_errno(target_mmap(v1
, v2
, v3
,
9423 target_to_host_bitmask(v4
, mmap_flags_tbl
),
9427 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
9428 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
9434 #ifdef TARGET_NR_mmap2
9435 case TARGET_NR_mmap2
:
9437 #define MMAP_SHIFT 12
9439 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
9440 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
9442 arg6
<< MMAP_SHIFT
));
9445 case TARGET_NR_munmap
:
9446 ret
= get_errno(target_munmap(arg1
, arg2
));
9448 case TARGET_NR_mprotect
:
9450 TaskState
*ts
= cpu
->opaque
;
9451 /* Special hack to detect libc making the stack executable. */
9452 if ((arg3
& PROT_GROWSDOWN
)
9453 && arg1
>= ts
->info
->stack_limit
9454 && arg1
<= ts
->info
->start_stack
) {
9455 arg3
&= ~PROT_GROWSDOWN
;
9456 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
9457 arg1
= ts
->info
->stack_limit
;
9460 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
9462 #ifdef TARGET_NR_mremap
9463 case TARGET_NR_mremap
:
9464 ret
= get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
9467 /* ??? msync/mlock/munlock are broken for softmmu. */
9468 #ifdef TARGET_NR_msync
9469 case TARGET_NR_msync
:
9470 ret
= get_errno(msync(g2h(arg1
), arg2
, arg3
));
9473 #ifdef TARGET_NR_mlock
9474 case TARGET_NR_mlock
:
9475 ret
= get_errno(mlock(g2h(arg1
), arg2
));
9478 #ifdef TARGET_NR_munlock
9479 case TARGET_NR_munlock
:
9480 ret
= get_errno(munlock(g2h(arg1
), arg2
));
9483 #ifdef TARGET_NR_mlockall
9484 case TARGET_NR_mlockall
:
9485 ret
= get_errno(mlockall(target_to_host_mlockall_arg(arg1
)));
9488 #ifdef TARGET_NR_munlockall
9489 case TARGET_NR_munlockall
:
9490 ret
= get_errno(munlockall());
9493 case TARGET_NR_truncate
:
9494 if (!(p
= lock_user_string(arg1
)))
9496 ret
= get_errno(truncate(p
, arg2
));
9497 unlock_user(p
, arg1
, 0);
9499 case TARGET_NR_ftruncate
:
9500 ret
= get_errno(ftruncate(arg1
, arg2
));
9502 case TARGET_NR_fchmod
:
9503 ret
= get_errno(fchmod(arg1
, arg2
));
9505 #if defined(TARGET_NR_fchmodat)
9506 case TARGET_NR_fchmodat
:
9507 if (!(p
= lock_user_string(arg2
)))
9509 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
9510 unlock_user(p
, arg2
, 0);
9513 case TARGET_NR_getpriority
:
9514 /* Note that negative values are valid for getpriority, so we must
9515 differentiate based on errno settings. */
9517 ret
= getpriority(arg1
, arg2
);
9518 if (ret
== -1 && errno
!= 0) {
9519 ret
= -host_to_target_errno(errno
);
9523 /* Return value is the unbiased priority. Signal no error. */
9524 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0;
9526 /* Return value is a biased priority to avoid negative numbers. */
9530 case TARGET_NR_setpriority
:
9531 ret
= get_errno(setpriority(arg1
, arg2
, arg3
));
9533 #ifdef TARGET_NR_profil
9534 case TARGET_NR_profil
:
9537 case TARGET_NR_statfs
:
9538 if (!(p
= lock_user_string(arg1
)))
9540 ret
= get_errno(statfs(path(p
), &stfs
));
9541 unlock_user(p
, arg1
, 0);
9543 if (!is_error(ret
)) {
9544 struct target_statfs
*target_stfs
;
9546 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
9548 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
9549 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
9550 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
9551 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
9552 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
9553 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
9554 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
9555 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
9556 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
9557 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
9558 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
9559 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
9560 unlock_user_struct(target_stfs
, arg2
, 1);
9563 case TARGET_NR_fstatfs
:
9564 ret
= get_errno(fstatfs(arg1
, &stfs
));
9565 goto convert_statfs
;
9566 #ifdef TARGET_NR_statfs64
9567 case TARGET_NR_statfs64
:
9568 if (!(p
= lock_user_string(arg1
)))
9570 ret
= get_errno(statfs(path(p
), &stfs
));
9571 unlock_user(p
, arg1
, 0);
9573 if (!is_error(ret
)) {
9574 struct target_statfs64
*target_stfs
;
9576 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
9578 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
9579 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
9580 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
9581 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
9582 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
9583 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
9584 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
9585 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
9586 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
9587 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
9588 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
9589 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
9590 unlock_user_struct(target_stfs
, arg3
, 1);
9593 case TARGET_NR_fstatfs64
:
9594 ret
= get_errno(fstatfs(arg1
, &stfs
));
9595 goto convert_statfs64
;
9597 #ifdef TARGET_NR_ioperm
9598 case TARGET_NR_ioperm
:
9601 #ifdef TARGET_NR_socketcall
9602 case TARGET_NR_socketcall
:
9603 ret
= do_socketcall(arg1
, arg2
);
9606 #ifdef TARGET_NR_accept
9607 case TARGET_NR_accept
:
9608 ret
= do_accept4(arg1
, arg2
, arg3
, 0);
9611 #ifdef TARGET_NR_accept4
9612 case TARGET_NR_accept4
:
9613 ret
= do_accept4(arg1
, arg2
, arg3
, arg4
);
9616 #ifdef TARGET_NR_bind
9617 case TARGET_NR_bind
:
9618 ret
= do_bind(arg1
, arg2
, arg3
);
9621 #ifdef TARGET_NR_connect
9622 case TARGET_NR_connect
:
9623 ret
= do_connect(arg1
, arg2
, arg3
);
9626 #ifdef TARGET_NR_getpeername
9627 case TARGET_NR_getpeername
:
9628 ret
= do_getpeername(arg1
, arg2
, arg3
);
9631 #ifdef TARGET_NR_getsockname
9632 case TARGET_NR_getsockname
:
9633 ret
= do_getsockname(arg1
, arg2
, arg3
);
9636 #ifdef TARGET_NR_getsockopt
9637 case TARGET_NR_getsockopt
:
9638 ret
= do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
9641 #ifdef TARGET_NR_listen
9642 case TARGET_NR_listen
:
9643 ret
= get_errno(listen(arg1
, arg2
));
9646 #ifdef TARGET_NR_recv
9647 case TARGET_NR_recv
:
9648 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
9651 #ifdef TARGET_NR_recvfrom
9652 case TARGET_NR_recvfrom
:
9653 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9656 #ifdef TARGET_NR_recvmsg
9657 case TARGET_NR_recvmsg
:
9658 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
9661 #ifdef TARGET_NR_send
9662 case TARGET_NR_send
:
9663 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
9666 #ifdef TARGET_NR_sendmsg
9667 case TARGET_NR_sendmsg
:
9668 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
9671 #ifdef TARGET_NR_sendmmsg
9672 case TARGET_NR_sendmmsg
:
9673 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
9675 case TARGET_NR_recvmmsg
:
9676 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
9679 #ifdef TARGET_NR_sendto
9680 case TARGET_NR_sendto
:
9681 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9684 #ifdef TARGET_NR_shutdown
9685 case TARGET_NR_shutdown
:
9686 ret
= get_errno(shutdown(arg1
, arg2
));
9689 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
9690 case TARGET_NR_getrandom
:
9691 p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
9695 ret
= get_errno(getrandom(p
, arg2
, arg3
));
9696 unlock_user(p
, arg1
, ret
);
9699 #ifdef TARGET_NR_socket
9700 case TARGET_NR_socket
:
9701 ret
= do_socket(arg1
, arg2
, arg3
);
9704 #ifdef TARGET_NR_socketpair
9705 case TARGET_NR_socketpair
:
9706 ret
= do_socketpair(arg1
, arg2
, arg3
, arg4
);
9709 #ifdef TARGET_NR_setsockopt
9710 case TARGET_NR_setsockopt
:
9711 ret
= do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
9714 #if defined(TARGET_NR_syslog)
9715 case TARGET_NR_syslog
:
9720 case TARGET_SYSLOG_ACTION_CLOSE
: /* Close log */
9721 case TARGET_SYSLOG_ACTION_OPEN
: /* Open log */
9722 case TARGET_SYSLOG_ACTION_CLEAR
: /* Clear ring buffer */
9723 case TARGET_SYSLOG_ACTION_CONSOLE_OFF
: /* Disable logging */
9724 case TARGET_SYSLOG_ACTION_CONSOLE_ON
: /* Enable logging */
9725 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL
: /* Set messages level */
9726 case TARGET_SYSLOG_ACTION_SIZE_UNREAD
: /* Number of chars */
9727 case TARGET_SYSLOG_ACTION_SIZE_BUFFER
: /* Size of the buffer */
9729 ret
= get_errno(sys_syslog((int)arg1
, NULL
, (int)arg3
));
9732 case TARGET_SYSLOG_ACTION_READ
: /* Read from log */
9733 case TARGET_SYSLOG_ACTION_READ_CLEAR
: /* Read/clear msgs */
9734 case TARGET_SYSLOG_ACTION_READ_ALL
: /* Read last messages */
9736 ret
= -TARGET_EINVAL
;
9744 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
9746 ret
= -TARGET_EFAULT
;
9749 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
9750 unlock_user(p
, arg2
, arg3
);
9760 case TARGET_NR_setitimer
:
9762 struct itimerval value
, ovalue
, *pvalue
;
9766 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
9767 || copy_from_user_timeval(&pvalue
->it_value
,
9768 arg2
+ sizeof(struct target_timeval
)))
9773 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
9774 if (!is_error(ret
) && arg3
) {
9775 if (copy_to_user_timeval(arg3
,
9776 &ovalue
.it_interval
)
9777 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
9783 case TARGET_NR_getitimer
:
9785 struct itimerval value
;
9787 ret
= get_errno(getitimer(arg1
, &value
));
9788 if (!is_error(ret
) && arg2
) {
9789 if (copy_to_user_timeval(arg2
,
9791 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
9797 #ifdef TARGET_NR_stat
9798 case TARGET_NR_stat
:
9799 if (!(p
= lock_user_string(arg1
)))
9801 ret
= get_errno(stat(path(p
), &st
));
9802 unlock_user(p
, arg1
, 0);
9805 #ifdef TARGET_NR_lstat
9806 case TARGET_NR_lstat
:
9807 if (!(p
= lock_user_string(arg1
)))
9809 ret
= get_errno(lstat(path(p
), &st
));
9810 unlock_user(p
, arg1
, 0);
9813 case TARGET_NR_fstat
:
9815 ret
= get_errno(fstat(arg1
, &st
));
9816 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
9819 if (!is_error(ret
)) {
9820 struct target_stat
*target_st
;
9822 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
9824 memset(target_st
, 0, sizeof(*target_st
));
9825 __put_user(st
.st_dev
, &target_st
->st_dev
);
9826 __put_user(st
.st_ino
, &target_st
->st_ino
);
9827 __put_user(st
.st_mode
, &target_st
->st_mode
);
9828 __put_user(st
.st_uid
, &target_st
->st_uid
);
9829 __put_user(st
.st_gid
, &target_st
->st_gid
);
9830 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
9831 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
9832 __put_user(st
.st_size
, &target_st
->st_size
);
9833 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
9834 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
9835 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
9836 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
9837 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
9838 unlock_user_struct(target_st
, arg2
, 1);
9842 #ifdef TARGET_NR_olduname
9843 case TARGET_NR_olduname
:
9846 #ifdef TARGET_NR_iopl
9847 case TARGET_NR_iopl
:
9850 case TARGET_NR_vhangup
:
9851 ret
= get_errno(vhangup());
9853 #ifdef TARGET_NR_idle
9854 case TARGET_NR_idle
:
9857 #ifdef TARGET_NR_syscall
9858 case TARGET_NR_syscall
:
9859 ret
= do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
9860 arg6
, arg7
, arg8
, 0);
9863 case TARGET_NR_wait4
:
9866 abi_long status_ptr
= arg2
;
9867 struct rusage rusage
, *rusage_ptr
;
9868 abi_ulong target_rusage
= arg4
;
9869 abi_long rusage_err
;
9871 rusage_ptr
= &rusage
;
9874 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, rusage_ptr
));
9875 if (!is_error(ret
)) {
9876 if (status_ptr
&& ret
) {
9877 status
= host_to_target_waitstatus(status
);
9878 if (put_user_s32(status
, status_ptr
))
9881 if (target_rusage
) {
9882 rusage_err
= host_to_target_rusage(target_rusage
, &rusage
);
9890 #ifdef TARGET_NR_swapoff
9891 case TARGET_NR_swapoff
:
9892 if (!(p
= lock_user_string(arg1
)))
9894 ret
= get_errno(swapoff(p
));
9895 unlock_user(p
, arg1
, 0);
9898 case TARGET_NR_sysinfo
:
9900 struct target_sysinfo
*target_value
;
9901 struct sysinfo value
;
9902 ret
= get_errno(sysinfo(&value
));
9903 if (!is_error(ret
) && arg1
)
9905 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
9907 __put_user(value
.uptime
, &target_value
->uptime
);
9908 __put_user(value
.loads
[0], &target_value
->loads
[0]);
9909 __put_user(value
.loads
[1], &target_value
->loads
[1]);
9910 __put_user(value
.loads
[2], &target_value
->loads
[2]);
9911 __put_user(value
.totalram
, &target_value
->totalram
);
9912 __put_user(value
.freeram
, &target_value
->freeram
);
9913 __put_user(value
.sharedram
, &target_value
->sharedram
);
9914 __put_user(value
.bufferram
, &target_value
->bufferram
);
9915 __put_user(value
.totalswap
, &target_value
->totalswap
);
9916 __put_user(value
.freeswap
, &target_value
->freeswap
);
9917 __put_user(value
.procs
, &target_value
->procs
);
9918 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
9919 __put_user(value
.freehigh
, &target_value
->freehigh
);
9920 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
9921 unlock_user_struct(target_value
, arg1
, 1);
9925 #ifdef TARGET_NR_ipc
9927 ret
= do_ipc(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9930 #ifdef TARGET_NR_semget
9931 case TARGET_NR_semget
:
9932 ret
= get_errno(semget(arg1
, arg2
, arg3
));
9935 #ifdef TARGET_NR_semop
9936 case TARGET_NR_semop
:
9937 ret
= do_semop(arg1
, arg2
, arg3
);
9940 #ifdef TARGET_NR_semctl
9941 case TARGET_NR_semctl
:
9942 ret
= do_semctl(arg1
, arg2
, arg3
, arg4
);
9945 #ifdef TARGET_NR_msgctl
9946 case TARGET_NR_msgctl
:
9947 ret
= do_msgctl(arg1
, arg2
, arg3
);
9950 #ifdef TARGET_NR_msgget
9951 case TARGET_NR_msgget
:
9952 ret
= get_errno(msgget(arg1
, arg2
));
9955 #ifdef TARGET_NR_msgrcv
9956 case TARGET_NR_msgrcv
:
9957 ret
= do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
9960 #ifdef TARGET_NR_msgsnd
9961 case TARGET_NR_msgsnd
:
9962 ret
= do_msgsnd(arg1
, arg2
, arg3
, arg4
);
9965 #ifdef TARGET_NR_shmget
9966 case TARGET_NR_shmget
:
9967 ret
= get_errno(shmget(arg1
, arg2
, arg3
));
9970 #ifdef TARGET_NR_shmctl
9971 case TARGET_NR_shmctl
:
9972 ret
= do_shmctl(arg1
, arg2
, arg3
);
9975 #ifdef TARGET_NR_shmat
9976 case TARGET_NR_shmat
:
9977 ret
= do_shmat(cpu_env
, arg1
, arg2
, arg3
);
9980 #ifdef TARGET_NR_shmdt
9981 case TARGET_NR_shmdt
:
9982 ret
= do_shmdt(arg1
);
9985 case TARGET_NR_fsync
:
9986 ret
= get_errno(fsync(arg1
));
9988 case TARGET_NR_clone
:
9989 /* Linux manages to have three different orderings for its
9990 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
9991 * match the kernel's CONFIG_CLONE_* settings.
9992 * Microblaze is further special in that it uses a sixth
9993 * implicit argument to clone for the TLS pointer.
9995 #if defined(TARGET_MICROBLAZE)
9996 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
9997 #elif defined(TARGET_CLONE_BACKWARDS)
9998 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
9999 #elif defined(TARGET_CLONE_BACKWARDS2)
10000 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
10002 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
10005 #ifdef __NR_exit_group
10006 /* new thread calls */
10007 case TARGET_NR_exit_group
:
10008 #ifdef TARGET_GPROF
10011 gdb_exit(cpu_env
, arg1
);
10012 ret
= get_errno(exit_group(arg1
));
10015 case TARGET_NR_setdomainname
:
10016 if (!(p
= lock_user_string(arg1
)))
10018 ret
= get_errno(setdomainname(p
, arg2
));
10019 unlock_user(p
, arg1
, 0);
10021 case TARGET_NR_uname
:
10022 /* no need to transcode because we use the linux syscall */
10024 struct new_utsname
* buf
;
10026 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
10028 ret
= get_errno(sys_uname(buf
));
10029 if (!is_error(ret
)) {
10030 /* Overwrite the native machine name with whatever is being
10032 strcpy (buf
->machine
, cpu_to_uname_machine(cpu_env
));
10033 /* Allow the user to override the reported release. */
10034 if (qemu_uname_release
&& *qemu_uname_release
) {
10035 g_strlcpy(buf
->release
, qemu_uname_release
,
10036 sizeof(buf
->release
));
10039 unlock_user_struct(buf
, arg1
, 1);
10043 case TARGET_NR_modify_ldt
:
10044 ret
= do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
10046 #if !defined(TARGET_X86_64)
10047 case TARGET_NR_vm86old
:
10048 goto unimplemented
;
10049 case TARGET_NR_vm86
:
10050 ret
= do_vm86(cpu_env
, arg1
, arg2
);
10054 case TARGET_NR_adjtimex
:
10056 struct timex host_buf
;
10058 if (target_to_host_timex(&host_buf
, arg1
) != 0) {
10061 ret
= get_errno(adjtimex(&host_buf
));
10062 if (!is_error(ret
)) {
10063 if (host_to_target_timex(arg1
, &host_buf
) != 0) {
10069 #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
10070 case TARGET_NR_clock_adjtime
:
10072 struct timex htx
, *phtx
= &htx
;
10074 if (target_to_host_timex(phtx
, arg2
) != 0) {
10077 ret
= get_errno(clock_adjtime(arg1
, phtx
));
10078 if (!is_error(ret
) && phtx
) {
10079 if (host_to_target_timex(arg2
, phtx
) != 0) {
10086 #ifdef TARGET_NR_create_module
10087 case TARGET_NR_create_module
:
10089 case TARGET_NR_init_module
:
10090 case TARGET_NR_delete_module
:
10091 #ifdef TARGET_NR_get_kernel_syms
10092 case TARGET_NR_get_kernel_syms
:
10094 goto unimplemented
;
10095 case TARGET_NR_quotactl
:
10096 goto unimplemented
;
10097 case TARGET_NR_getpgid
:
10098 ret
= get_errno(getpgid(arg1
));
10100 case TARGET_NR_fchdir
:
10101 ret
= get_errno(fchdir(arg1
));
10103 #ifdef TARGET_NR_bdflush /* not on x86_64 */
10104 case TARGET_NR_bdflush
:
10105 goto unimplemented
;
10107 #ifdef TARGET_NR_sysfs
10108 case TARGET_NR_sysfs
:
10109 goto unimplemented
;
10111 case TARGET_NR_personality
:
10112 ret
= get_errno(personality(arg1
));
10114 #ifdef TARGET_NR_afs_syscall
10115 case TARGET_NR_afs_syscall
:
10116 goto unimplemented
;
10118 #ifdef TARGET_NR__llseek /* Not on alpha */
10119 case TARGET_NR__llseek
:
10122 #if !defined(__NR_llseek)
10123 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | (abi_ulong
)arg3
, arg5
);
10125 ret
= get_errno(res
);
10130 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
10132 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
10138 #ifdef TARGET_NR_getdents
10139 case TARGET_NR_getdents
:
10140 #ifdef __NR_getdents
10141 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
10143 struct target_dirent
*target_dirp
;
10144 struct linux_dirent
*dirp
;
10145 abi_long count
= arg3
;
10147 dirp
= g_try_malloc(count
);
10149 ret
= -TARGET_ENOMEM
;
10153 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
10154 if (!is_error(ret
)) {
10155 struct linux_dirent
*de
;
10156 struct target_dirent
*tde
;
10158 int reclen
, treclen
;
10159 int count1
, tnamelen
;
10163 if (!(target_dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
10167 reclen
= de
->d_reclen
;
10168 tnamelen
= reclen
- offsetof(struct linux_dirent
, d_name
);
10169 assert(tnamelen
>= 0);
10170 treclen
= tnamelen
+ offsetof(struct target_dirent
, d_name
);
10171 assert(count1
+ treclen
<= count
);
10172 tde
->d_reclen
= tswap16(treclen
);
10173 tde
->d_ino
= tswapal(de
->d_ino
);
10174 tde
->d_off
= tswapal(de
->d_off
);
10175 memcpy(tde
->d_name
, de
->d_name
, tnamelen
);
10176 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
10178 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
10182 unlock_user(target_dirp
, arg2
, ret
);
10188 struct linux_dirent
*dirp
;
10189 abi_long count
= arg3
;
10191 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
10193 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
10194 if (!is_error(ret
)) {
10195 struct linux_dirent
*de
;
10200 reclen
= de
->d_reclen
;
10203 de
->d_reclen
= tswap16(reclen
);
10204 tswapls(&de
->d_ino
);
10205 tswapls(&de
->d_off
);
10206 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
10210 unlock_user(dirp
, arg2
, ret
);
10214 /* Implement getdents in terms of getdents64 */
10216 struct linux_dirent64
*dirp
;
10217 abi_long count
= arg3
;
10219 dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
10223 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
10224 if (!is_error(ret
)) {
10225 /* Convert the dirent64 structs to target dirent. We do this
10226 * in-place, since we can guarantee that a target_dirent is no
10227 * larger than a dirent64; however this means we have to be
10228 * careful to read everything before writing in the new format.
10230 struct linux_dirent64
*de
;
10231 struct target_dirent
*tde
;
10236 tde
= (struct target_dirent
*)dirp
;
10238 int namelen
, treclen
;
10239 int reclen
= de
->d_reclen
;
10240 uint64_t ino
= de
->d_ino
;
10241 int64_t off
= de
->d_off
;
10242 uint8_t type
= de
->d_type
;
10244 namelen
= strlen(de
->d_name
);
10245 treclen
= offsetof(struct target_dirent
, d_name
)
10247 treclen
= QEMU_ALIGN_UP(treclen
, sizeof(abi_long
));
10249 memmove(tde
->d_name
, de
->d_name
, namelen
+ 1);
10250 tde
->d_ino
= tswapal(ino
);
10251 tde
->d_off
= tswapal(off
);
10252 tde
->d_reclen
= tswap16(treclen
);
10253 /* The target_dirent type is in what was formerly a padding
10254 * byte at the end of the structure:
10256 *(((char *)tde
) + treclen
- 1) = type
;
10258 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
10259 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
10265 unlock_user(dirp
, arg2
, ret
);
10269 #endif /* TARGET_NR_getdents */
10270 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
10271 case TARGET_NR_getdents64
:
10273 struct linux_dirent64
*dirp
;
10274 abi_long count
= arg3
;
10275 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
10277 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
10278 if (!is_error(ret
)) {
10279 struct linux_dirent64
*de
;
10284 reclen
= de
->d_reclen
;
10287 de
->d_reclen
= tswap16(reclen
);
10288 tswap64s((uint64_t *)&de
->d_ino
);
10289 tswap64s((uint64_t *)&de
->d_off
);
10290 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
10294 unlock_user(dirp
, arg2
, ret
);
10297 #endif /* TARGET_NR_getdents64 */
10298 #if defined(TARGET_NR__newselect)
10299 case TARGET_NR__newselect
:
10300 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
10303 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
10304 # ifdef TARGET_NR_poll
10305 case TARGET_NR_poll
:
10307 # ifdef TARGET_NR_ppoll
10308 case TARGET_NR_ppoll
:
10311 struct target_pollfd
*target_pfd
;
10312 unsigned int nfds
= arg2
;
10313 struct pollfd
*pfd
;
10319 if (nfds
> (INT_MAX
/ sizeof(struct target_pollfd
))) {
10320 ret
= -TARGET_EINVAL
;
10324 target_pfd
= lock_user(VERIFY_WRITE
, arg1
,
10325 sizeof(struct target_pollfd
) * nfds
, 1);
10330 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
10331 for (i
= 0; i
< nfds
; i
++) {
10332 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
10333 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
10338 # ifdef TARGET_NR_ppoll
10339 case TARGET_NR_ppoll
:
10341 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
10342 target_sigset_t
*target_set
;
10343 sigset_t _set
, *set
= &_set
;
10346 if (target_to_host_timespec(timeout_ts
, arg3
)) {
10347 unlock_user(target_pfd
, arg1
, 0);
10355 if (arg5
!= sizeof(target_sigset_t
)) {
10356 unlock_user(target_pfd
, arg1
, 0);
10357 ret
= -TARGET_EINVAL
;
10361 target_set
= lock_user(VERIFY_READ
, arg4
, sizeof(target_sigset_t
), 1);
10363 unlock_user(target_pfd
, arg1
, 0);
10366 target_to_host_sigset(set
, target_set
);
10371 ret
= get_errno(safe_ppoll(pfd
, nfds
, timeout_ts
,
10372 set
, SIGSET_T_SIZE
));
10374 if (!is_error(ret
) && arg3
) {
10375 host_to_target_timespec(arg3
, timeout_ts
);
10378 unlock_user(target_set
, arg4
, 0);
10383 # ifdef TARGET_NR_poll
10384 case TARGET_NR_poll
:
10386 struct timespec ts
, *pts
;
10389 /* Convert ms to secs, ns */
10390 ts
.tv_sec
= arg3
/ 1000;
10391 ts
.tv_nsec
= (arg3
% 1000) * 1000000LL;
10394 /* -ve poll() timeout means "infinite" */
10397 ret
= get_errno(safe_ppoll(pfd
, nfds
, pts
, NULL
, 0));
10402 g_assert_not_reached();
10405 if (!is_error(ret
)) {
10406 for(i
= 0; i
< nfds
; i
++) {
10407 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
10410 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
10414 case TARGET_NR_flock
:
10415 /* NOTE: the flock constant seems to be the same for every
10417 ret
= get_errno(safe_flock(arg1
, arg2
));
10419 case TARGET_NR_readv
:
10421 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
10423 ret
= get_errno(safe_readv(arg1
, vec
, arg3
));
10424 unlock_iovec(vec
, arg2
, arg3
, 1);
10426 ret
= -host_to_target_errno(errno
);
10430 case TARGET_NR_writev
:
10432 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
10434 ret
= get_errno(safe_writev(arg1
, vec
, arg3
));
10435 unlock_iovec(vec
, arg2
, arg3
, 0);
10437 ret
= -host_to_target_errno(errno
);
10441 #if defined(TARGET_NR_preadv)
10442 case TARGET_NR_preadv
:
10444 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
10446 ret
= get_errno(safe_preadv(arg1
, vec
, arg3
, arg4
, arg5
));
10447 unlock_iovec(vec
, arg2
, arg3
, 1);
10449 ret
= -host_to_target_errno(errno
);
10454 #if defined(TARGET_NR_pwritev)
10455 case TARGET_NR_pwritev
:
10457 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
10459 ret
= get_errno(safe_pwritev(arg1
, vec
, arg3
, arg4
, arg5
));
10460 unlock_iovec(vec
, arg2
, arg3
, 0);
10462 ret
= -host_to_target_errno(errno
);
10467 case TARGET_NR_getsid
:
10468 ret
= get_errno(getsid(arg1
));
10470 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
10471 case TARGET_NR_fdatasync
:
10472 ret
= get_errno(fdatasync(arg1
));
10475 #ifdef TARGET_NR__sysctl
10476 case TARGET_NR__sysctl
:
10477 /* We don't implement this, but ENOTDIR is always a safe
10479 ret
= -TARGET_ENOTDIR
;
10482 case TARGET_NR_sched_getaffinity
:
10484 unsigned int mask_size
;
10485 unsigned long *mask
;
10488 * sched_getaffinity needs multiples of ulong, so need to take
10489 * care of mismatches between target ulong and host ulong sizes.
10491 if (arg2
& (sizeof(abi_ulong
) - 1)) {
10492 ret
= -TARGET_EINVAL
;
10495 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
10497 mask
= alloca(mask_size
);
10498 memset(mask
, 0, mask_size
);
10499 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
10501 if (!is_error(ret
)) {
10503 /* More data returned than the caller's buffer will fit.
10504 * This only happens if sizeof(abi_long) < sizeof(long)
10505 * and the caller passed us a buffer holding an odd number
10506 * of abi_longs. If the host kernel is actually using the
10507 * extra 4 bytes then fail EINVAL; otherwise we can just
10508 * ignore them and only copy the interesting part.
10510 int numcpus
= sysconf(_SC_NPROCESSORS_CONF
);
10511 if (numcpus
> arg2
* 8) {
10512 ret
= -TARGET_EINVAL
;
10518 if (host_to_target_cpu_mask(mask
, mask_size
, arg3
, ret
)) {
10524 case TARGET_NR_sched_setaffinity
:
10526 unsigned int mask_size
;
10527 unsigned long *mask
;
10530 * sched_setaffinity needs multiples of ulong, so need to take
10531 * care of mismatches between target ulong and host ulong sizes.
10533 if (arg2
& (sizeof(abi_ulong
) - 1)) {
10534 ret
= -TARGET_EINVAL
;
10537 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
10538 mask
= alloca(mask_size
);
10540 ret
= target_to_host_cpu_mask(mask
, mask_size
, arg3
, arg2
);
10545 ret
= get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
10548 case TARGET_NR_getcpu
:
10550 unsigned cpu
, node
;
10551 ret
= get_errno(sys_getcpu(arg1
? &cpu
: NULL
,
10552 arg2
? &node
: NULL
,
10554 if (is_error(ret
)) {
10557 if (arg1
&& put_user_u32(cpu
, arg1
)) {
10560 if (arg2
&& put_user_u32(node
, arg2
)) {
10565 case TARGET_NR_sched_setparam
:
10567 struct sched_param
*target_schp
;
10568 struct sched_param schp
;
10571 return -TARGET_EINVAL
;
10573 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1))
10575 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
10576 unlock_user_struct(target_schp
, arg2
, 0);
10577 ret
= get_errno(sched_setparam(arg1
, &schp
));
10580 case TARGET_NR_sched_getparam
:
10582 struct sched_param
*target_schp
;
10583 struct sched_param schp
;
10586 return -TARGET_EINVAL
;
10588 ret
= get_errno(sched_getparam(arg1
, &schp
));
10589 if (!is_error(ret
)) {
10590 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0))
10592 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
10593 unlock_user_struct(target_schp
, arg2
, 1);
10597 case TARGET_NR_sched_setscheduler
:
10599 struct sched_param
*target_schp
;
10600 struct sched_param schp
;
10602 return -TARGET_EINVAL
;
10604 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1))
10606 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
10607 unlock_user_struct(target_schp
, arg3
, 0);
10608 ret
= get_errno(sched_setscheduler(arg1
, arg2
, &schp
));
10611 case TARGET_NR_sched_getscheduler
:
10612 ret
= get_errno(sched_getscheduler(arg1
));
10614 case TARGET_NR_sched_yield
:
10615 ret
= get_errno(sched_yield());
10617 case TARGET_NR_sched_get_priority_max
:
10618 ret
= get_errno(sched_get_priority_max(arg1
));
10620 case TARGET_NR_sched_get_priority_min
:
10621 ret
= get_errno(sched_get_priority_min(arg1
));
10623 case TARGET_NR_sched_rr_get_interval
:
10625 struct timespec ts
;
10626 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
10627 if (!is_error(ret
)) {
10628 ret
= host_to_target_timespec(arg2
, &ts
);
10632 case TARGET_NR_nanosleep
:
10634 struct timespec req
, rem
;
10635 target_to_host_timespec(&req
, arg1
);
10636 ret
= get_errno(safe_nanosleep(&req
, &rem
));
10637 if (is_error(ret
) && arg2
) {
10638 host_to_target_timespec(arg2
, &rem
);
10642 #ifdef TARGET_NR_query_module
10643 case TARGET_NR_query_module
:
10644 goto unimplemented
;
10646 #ifdef TARGET_NR_nfsservctl
10647 case TARGET_NR_nfsservctl
:
10648 goto unimplemented
;
10650 case TARGET_NR_prctl
:
10652 case PR_GET_PDEATHSIG
:
10655 ret
= get_errno(prctl(arg1
, &deathsig
, arg3
, arg4
, arg5
));
10656 if (!is_error(ret
) && arg2
10657 && put_user_ual(deathsig
, arg2
)) {
10665 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
10669 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
10670 arg3
, arg4
, arg5
));
10671 unlock_user(name
, arg2
, 16);
10676 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
10680 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
10681 arg3
, arg4
, arg5
));
10682 unlock_user(name
, arg2
, 0);
10686 #ifdef TARGET_AARCH64
10687 case TARGET_PR_SVE_SET_VL
:
10688 /* We cannot support either PR_SVE_SET_VL_ONEXEC
10689 or PR_SVE_VL_INHERIT. Therefore, anything above
10690 ARM_MAX_VQ results in EINVAL. */
10691 ret
= -TARGET_EINVAL
;
10692 if (arm_feature(cpu_env
, ARM_FEATURE_SVE
)
10693 && arg2
>= 0 && arg2
<= ARM_MAX_VQ
* 16 && !(arg2
& 15)) {
10694 CPUARMState
*env
= cpu_env
;
10695 int old_vq
= (env
->vfp
.zcr_el
[1] & 0xf) + 1;
10696 int vq
= MAX(arg2
/ 16, 1);
10699 aarch64_sve_narrow_vq(env
, vq
);
10701 env
->vfp
.zcr_el
[1] = vq
- 1;
10705 case TARGET_PR_SVE_GET_VL
:
10706 ret
= -TARGET_EINVAL
;
10707 if (arm_feature(cpu_env
, ARM_FEATURE_SVE
)) {
10708 CPUARMState
*env
= cpu_env
;
10709 ret
= ((env
->vfp
.zcr_el
[1] & 0xf) + 1) * 16;
10712 #endif /* AARCH64 */
10713 case PR_GET_SECCOMP
:
10714 case PR_SET_SECCOMP
:
10715 /* Disable seccomp to prevent the target disabling syscalls we
10717 ret
= -TARGET_EINVAL
;
10720 /* Most prctl options have no pointer arguments */
10721 ret
= get_errno(prctl(arg1
, arg2
, arg3
, arg4
, arg5
));
10725 #ifdef TARGET_NR_arch_prctl
10726 case TARGET_NR_arch_prctl
:
10727 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
10728 ret
= do_arch_prctl(cpu_env
, arg1
, arg2
);
10731 goto unimplemented
;
10734 #ifdef TARGET_NR_pread64
10735 case TARGET_NR_pread64
:
10736 if (regpairs_aligned(cpu_env
, num
)) {
10740 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
10742 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
10743 unlock_user(p
, arg2
, ret
);
10745 case TARGET_NR_pwrite64
:
10746 if (regpairs_aligned(cpu_env
, num
)) {
10750 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
10752 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
10753 unlock_user(p
, arg2
, 0);
10756 case TARGET_NR_getcwd
:
10757 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
10759 ret
= get_errno(sys_getcwd1(p
, arg2
));
10760 unlock_user(p
, arg1
, ret
);
10762 case TARGET_NR_capget
:
10763 case TARGET_NR_capset
:
10765 struct target_user_cap_header
*target_header
;
10766 struct target_user_cap_data
*target_data
= NULL
;
10767 struct __user_cap_header_struct header
;
10768 struct __user_cap_data_struct data
[2];
10769 struct __user_cap_data_struct
*dataptr
= NULL
;
10770 int i
, target_datalen
;
10771 int data_items
= 1;
10773 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
10776 header
.version
= tswap32(target_header
->version
);
10777 header
.pid
= tswap32(target_header
->pid
);
10779 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
10780 /* Version 2 and up takes pointer to two user_data structs */
10784 target_datalen
= sizeof(*target_data
) * data_items
;
10787 if (num
== TARGET_NR_capget
) {
10788 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
10790 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
10792 if (!target_data
) {
10793 unlock_user_struct(target_header
, arg1
, 0);
10797 if (num
== TARGET_NR_capset
) {
10798 for (i
= 0; i
< data_items
; i
++) {
10799 data
[i
].effective
= tswap32(target_data
[i
].effective
);
10800 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
10801 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
10808 if (num
== TARGET_NR_capget
) {
10809 ret
= get_errno(capget(&header
, dataptr
));
10811 ret
= get_errno(capset(&header
, dataptr
));
10814 /* The kernel always updates version for both capget and capset */
10815 target_header
->version
= tswap32(header
.version
);
10816 unlock_user_struct(target_header
, arg1
, 1);
10819 if (num
== TARGET_NR_capget
) {
10820 for (i
= 0; i
< data_items
; i
++) {
10821 target_data
[i
].effective
= tswap32(data
[i
].effective
);
10822 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
10823 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
10825 unlock_user(target_data
, arg2
, target_datalen
);
10827 unlock_user(target_data
, arg2
, 0);
10832 case TARGET_NR_sigaltstack
:
10833 ret
= do_sigaltstack(arg1
, arg2
, get_sp_from_cpustate((CPUArchState
*)cpu_env
));
10836 #ifdef CONFIG_SENDFILE
10837 case TARGET_NR_sendfile
:
10839 off_t
*offp
= NULL
;
10842 ret
= get_user_sal(off
, arg3
);
10843 if (is_error(ret
)) {
10848 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
10849 if (!is_error(ret
) && arg3
) {
10850 abi_long ret2
= put_user_sal(off
, arg3
);
10851 if (is_error(ret2
)) {
10857 #ifdef TARGET_NR_sendfile64
10858 case TARGET_NR_sendfile64
:
10860 off_t
*offp
= NULL
;
10863 ret
= get_user_s64(off
, arg3
);
10864 if (is_error(ret
)) {
10869 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
10870 if (!is_error(ret
) && arg3
) {
10871 abi_long ret2
= put_user_s64(off
, arg3
);
10872 if (is_error(ret2
)) {
10880 case TARGET_NR_sendfile
:
10881 #ifdef TARGET_NR_sendfile64
10882 case TARGET_NR_sendfile64
:
10884 goto unimplemented
;
10887 #ifdef TARGET_NR_getpmsg
10888 case TARGET_NR_getpmsg
:
10889 goto unimplemented
;
10891 #ifdef TARGET_NR_putpmsg
10892 case TARGET_NR_putpmsg
:
10893 goto unimplemented
;
10895 #ifdef TARGET_NR_vfork
10896 case TARGET_NR_vfork
:
10897 ret
= get_errno(do_fork(cpu_env
,
10898 CLONE_VFORK
| CLONE_VM
| TARGET_SIGCHLD
,
10902 #ifdef TARGET_NR_ugetrlimit
10903 case TARGET_NR_ugetrlimit
:
10905 struct rlimit rlim
;
10906 int resource
= target_to_host_resource(arg1
);
10907 ret
= get_errno(getrlimit(resource
, &rlim
));
10908 if (!is_error(ret
)) {
10909 struct target_rlimit
*target_rlim
;
10910 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
10912 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
10913 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
10914 unlock_user_struct(target_rlim
, arg2
, 1);
10919 #ifdef TARGET_NR_truncate64
10920 case TARGET_NR_truncate64
:
10921 if (!(p
= lock_user_string(arg1
)))
10923 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
10924 unlock_user(p
, arg1
, 0);
10927 #ifdef TARGET_NR_ftruncate64
10928 case TARGET_NR_ftruncate64
:
10929 ret
= target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
10932 #ifdef TARGET_NR_stat64
10933 case TARGET_NR_stat64
:
10934 if (!(p
= lock_user_string(arg1
)))
10936 ret
= get_errno(stat(path(p
), &st
));
10937 unlock_user(p
, arg1
, 0);
10938 if (!is_error(ret
))
10939 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10942 #ifdef TARGET_NR_lstat64
10943 case TARGET_NR_lstat64
:
10944 if (!(p
= lock_user_string(arg1
)))
10946 ret
= get_errno(lstat(path(p
), &st
));
10947 unlock_user(p
, arg1
, 0);
10948 if (!is_error(ret
))
10949 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10952 #ifdef TARGET_NR_fstat64
10953 case TARGET_NR_fstat64
:
10954 ret
= get_errno(fstat(arg1
, &st
));
10955 if (!is_error(ret
))
10956 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10959 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
10960 #ifdef TARGET_NR_fstatat64
10961 case TARGET_NR_fstatat64
:
10963 #ifdef TARGET_NR_newfstatat
10964 case TARGET_NR_newfstatat
:
10966 if (!(p
= lock_user_string(arg2
)))
10968 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
10969 if (!is_error(ret
))
10970 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
10973 #ifdef TARGET_NR_lchown
10974 case TARGET_NR_lchown
:
10975 if (!(p
= lock_user_string(arg1
)))
10977 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
10978 unlock_user(p
, arg1
, 0);
10981 #ifdef TARGET_NR_getuid
10982 case TARGET_NR_getuid
:
10983 ret
= get_errno(high2lowuid(getuid()));
10986 #ifdef TARGET_NR_getgid
10987 case TARGET_NR_getgid
:
10988 ret
= get_errno(high2lowgid(getgid()));
10991 #ifdef TARGET_NR_geteuid
10992 case TARGET_NR_geteuid
:
10993 ret
= get_errno(high2lowuid(geteuid()));
10996 #ifdef TARGET_NR_getegid
10997 case TARGET_NR_getegid
:
10998 ret
= get_errno(high2lowgid(getegid()));
11001 case TARGET_NR_setreuid
:
11002 ret
= get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
11004 case TARGET_NR_setregid
:
11005 ret
= get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
11007 case TARGET_NR_getgroups
:
11009 int gidsetsize
= arg1
;
11010 target_id
*target_grouplist
;
11014 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11015 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
11016 if (gidsetsize
== 0)
11018 if (!is_error(ret
)) {
11019 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* sizeof(target_id
), 0);
11020 if (!target_grouplist
)
11022 for(i
= 0;i
< ret
; i
++)
11023 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
11024 unlock_user(target_grouplist
, arg2
, gidsetsize
* sizeof(target_id
));
11028 case TARGET_NR_setgroups
:
11030 int gidsetsize
= arg1
;
11031 target_id
*target_grouplist
;
11032 gid_t
*grouplist
= NULL
;
11035 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11036 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* sizeof(target_id
), 1);
11037 if (!target_grouplist
) {
11038 ret
= -TARGET_EFAULT
;
11041 for (i
= 0; i
< gidsetsize
; i
++) {
11042 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
11044 unlock_user(target_grouplist
, arg2
, 0);
11046 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
11049 case TARGET_NR_fchown
:
11050 ret
= get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
11052 #if defined(TARGET_NR_fchownat)
11053 case TARGET_NR_fchownat
:
11054 if (!(p
= lock_user_string(arg2
)))
11056 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
11057 low2highgid(arg4
), arg5
));
11058 unlock_user(p
, arg2
, 0);
11061 #ifdef TARGET_NR_setresuid
11062 case TARGET_NR_setresuid
:
11063 ret
= get_errno(sys_setresuid(low2highuid(arg1
),
11065 low2highuid(arg3
)));
11068 #ifdef TARGET_NR_getresuid
11069 case TARGET_NR_getresuid
:
11071 uid_t ruid
, euid
, suid
;
11072 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
11073 if (!is_error(ret
)) {
11074 if (put_user_id(high2lowuid(ruid
), arg1
)
11075 || put_user_id(high2lowuid(euid
), arg2
)
11076 || put_user_id(high2lowuid(suid
), arg3
))
11082 #ifdef TARGET_NR_getresgid
11083 case TARGET_NR_setresgid
:
11084 ret
= get_errno(sys_setresgid(low2highgid(arg1
),
11086 low2highgid(arg3
)));
11089 #ifdef TARGET_NR_getresgid
11090 case TARGET_NR_getresgid
:
11092 gid_t rgid
, egid
, sgid
;
11093 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
11094 if (!is_error(ret
)) {
11095 if (put_user_id(high2lowgid(rgid
), arg1
)
11096 || put_user_id(high2lowgid(egid
), arg2
)
11097 || put_user_id(high2lowgid(sgid
), arg3
))
11103 #ifdef TARGET_NR_chown
11104 case TARGET_NR_chown
:
11105 if (!(p
= lock_user_string(arg1
)))
11107 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
11108 unlock_user(p
, arg1
, 0);
11111 case TARGET_NR_setuid
:
11112 ret
= get_errno(sys_setuid(low2highuid(arg1
)));
11114 case TARGET_NR_setgid
:
11115 ret
= get_errno(sys_setgid(low2highgid(arg1
)));
11117 case TARGET_NR_setfsuid
:
11118 ret
= get_errno(setfsuid(arg1
));
11120 case TARGET_NR_setfsgid
:
11121 ret
= get_errno(setfsgid(arg1
));
11124 #ifdef TARGET_NR_lchown32
11125 case TARGET_NR_lchown32
:
11126 if (!(p
= lock_user_string(arg1
)))
11128 ret
= get_errno(lchown(p
, arg2
, arg3
));
11129 unlock_user(p
, arg1
, 0);
11132 #ifdef TARGET_NR_getuid32
11133 case TARGET_NR_getuid32
:
11134 ret
= get_errno(getuid());
11138 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11139 /* Alpha specific */
11140 case TARGET_NR_getxuid
:
11144 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=euid
;
11146 ret
= get_errno(getuid());
11149 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11150 /* Alpha specific */
11151 case TARGET_NR_getxgid
:
11155 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=egid
;
11157 ret
= get_errno(getgid());
11160 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11161 /* Alpha specific */
11162 case TARGET_NR_osf_getsysinfo
:
11163 ret
= -TARGET_EOPNOTSUPP
;
11165 case TARGET_GSI_IEEE_FP_CONTROL
:
11167 uint64_t swcr
, fpcr
= cpu_alpha_load_fpcr (cpu_env
);
11169 /* Copied from linux ieee_fpcr_to_swcr. */
11170 swcr
= (fpcr
>> 35) & SWCR_STATUS_MASK
;
11171 swcr
|= (fpcr
>> 36) & SWCR_MAP_DMZ
;
11172 swcr
|= (~fpcr
>> 48) & (SWCR_TRAP_ENABLE_INV
11173 | SWCR_TRAP_ENABLE_DZE
11174 | SWCR_TRAP_ENABLE_OVF
);
11175 swcr
|= (~fpcr
>> 57) & (SWCR_TRAP_ENABLE_UNF
11176 | SWCR_TRAP_ENABLE_INE
);
11177 swcr
|= (fpcr
>> 47) & SWCR_MAP_UMZ
;
11178 swcr
|= (~fpcr
>> 41) & SWCR_TRAP_ENABLE_DNO
;
11180 if (put_user_u64 (swcr
, arg2
))
11186 /* case GSI_IEEE_STATE_AT_SIGNAL:
11187 -- Not implemented in linux kernel.
11189 -- Retrieves current unaligned access state; not much used.
11190 case GSI_PROC_TYPE:
11191 -- Retrieves implver information; surely not used.
11192 case GSI_GET_HWRPB:
11193 -- Grabs a copy of the HWRPB; surely not used.
11198 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
11199 /* Alpha specific */
11200 case TARGET_NR_osf_setsysinfo
:
11201 ret
= -TARGET_EOPNOTSUPP
;
11203 case TARGET_SSI_IEEE_FP_CONTROL
:
11205 uint64_t swcr
, fpcr
, orig_fpcr
;
11207 if (get_user_u64 (swcr
, arg2
)) {
11210 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11211 fpcr
= orig_fpcr
& FPCR_DYN_MASK
;
11213 /* Copied from linux ieee_swcr_to_fpcr. */
11214 fpcr
|= (swcr
& SWCR_STATUS_MASK
) << 35;
11215 fpcr
|= (swcr
& SWCR_MAP_DMZ
) << 36;
11216 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_INV
11217 | SWCR_TRAP_ENABLE_DZE
11218 | SWCR_TRAP_ENABLE_OVF
)) << 48;
11219 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_UNF
11220 | SWCR_TRAP_ENABLE_INE
)) << 57;
11221 fpcr
|= (swcr
& SWCR_MAP_UMZ
? FPCR_UNDZ
| FPCR_UNFD
: 0);
11222 fpcr
|= (~swcr
& SWCR_TRAP_ENABLE_DNO
) << 41;
11224 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11229 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
11231 uint64_t exc
, fpcr
, orig_fpcr
;
11234 if (get_user_u64(exc
, arg2
)) {
11238 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11240 /* We only add to the exception status here. */
11241 fpcr
= orig_fpcr
| ((exc
& SWCR_STATUS_MASK
) << 35);
11243 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11246 /* Old exceptions are not signaled. */
11247 fpcr
&= ~(orig_fpcr
& FPCR_STATUS_MASK
);
11249 /* If any exceptions set by this call,
11250 and are unmasked, send a signal. */
11252 if ((fpcr
& (FPCR_INE
| FPCR_INED
)) == FPCR_INE
) {
11253 si_code
= TARGET_FPE_FLTRES
;
11255 if ((fpcr
& (FPCR_UNF
| FPCR_UNFD
)) == FPCR_UNF
) {
11256 si_code
= TARGET_FPE_FLTUND
;
11258 if ((fpcr
& (FPCR_OVF
| FPCR_OVFD
)) == FPCR_OVF
) {
11259 si_code
= TARGET_FPE_FLTOVF
;
11261 if ((fpcr
& (FPCR_DZE
| FPCR_DZED
)) == FPCR_DZE
) {
11262 si_code
= TARGET_FPE_FLTDIV
;
11264 if ((fpcr
& (FPCR_INV
| FPCR_INVD
)) == FPCR_INV
) {
11265 si_code
= TARGET_FPE_FLTINV
;
11267 if (si_code
!= 0) {
11268 target_siginfo_t info
;
11269 info
.si_signo
= SIGFPE
;
11271 info
.si_code
= si_code
;
11272 info
._sifields
._sigfault
._addr
11273 = ((CPUArchState
*)cpu_env
)->pc
;
11274 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
,
11275 QEMU_SI_FAULT
, &info
);
11280 /* case SSI_NVPAIRS:
11281 -- Used with SSIN_UACPROC to enable unaligned accesses.
11282 case SSI_IEEE_STATE_AT_SIGNAL:
11283 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
11284 -- Not implemented in linux kernel
11289 #ifdef TARGET_NR_osf_sigprocmask
11290 /* Alpha specific. */
11291 case TARGET_NR_osf_sigprocmask
:
11295 sigset_t set
, oldset
;
11298 case TARGET_SIG_BLOCK
:
11301 case TARGET_SIG_UNBLOCK
:
11304 case TARGET_SIG_SETMASK
:
11308 ret
= -TARGET_EINVAL
;
11312 target_to_host_old_sigset(&set
, &mask
);
11313 ret
= do_sigprocmask(how
, &set
, &oldset
);
11315 host_to_target_old_sigset(&mask
, &oldset
);
11322 #ifdef TARGET_NR_getgid32
11323 case TARGET_NR_getgid32
:
11324 ret
= get_errno(getgid());
11327 #ifdef TARGET_NR_geteuid32
11328 case TARGET_NR_geteuid32
:
11329 ret
= get_errno(geteuid());
11332 #ifdef TARGET_NR_getegid32
11333 case TARGET_NR_getegid32
:
11334 ret
= get_errno(getegid());
11337 #ifdef TARGET_NR_setreuid32
11338 case TARGET_NR_setreuid32
:
11339 ret
= get_errno(setreuid(arg1
, arg2
));
11342 #ifdef TARGET_NR_setregid32
11343 case TARGET_NR_setregid32
:
11344 ret
= get_errno(setregid(arg1
, arg2
));
11347 #ifdef TARGET_NR_getgroups32
11348 case TARGET_NR_getgroups32
:
11350 int gidsetsize
= arg1
;
11351 uint32_t *target_grouplist
;
11355 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11356 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
11357 if (gidsetsize
== 0)
11359 if (!is_error(ret
)) {
11360 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
11361 if (!target_grouplist
) {
11362 ret
= -TARGET_EFAULT
;
11365 for(i
= 0;i
< ret
; i
++)
11366 target_grouplist
[i
] = tswap32(grouplist
[i
]);
11367 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
11372 #ifdef TARGET_NR_setgroups32
11373 case TARGET_NR_setgroups32
:
11375 int gidsetsize
= arg1
;
11376 uint32_t *target_grouplist
;
11380 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11381 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
11382 if (!target_grouplist
) {
11383 ret
= -TARGET_EFAULT
;
11386 for(i
= 0;i
< gidsetsize
; i
++)
11387 grouplist
[i
] = tswap32(target_grouplist
[i
]);
11388 unlock_user(target_grouplist
, arg2
, 0);
11389 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
11393 #ifdef TARGET_NR_fchown32
11394 case TARGET_NR_fchown32
:
11395 ret
= get_errno(fchown(arg1
, arg2
, arg3
));
11398 #ifdef TARGET_NR_setresuid32
11399 case TARGET_NR_setresuid32
:
11400 ret
= get_errno(sys_setresuid(arg1
, arg2
, arg3
));
11403 #ifdef TARGET_NR_getresuid32
11404 case TARGET_NR_getresuid32
:
11406 uid_t ruid
, euid
, suid
;
11407 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
11408 if (!is_error(ret
)) {
11409 if (put_user_u32(ruid
, arg1
)
11410 || put_user_u32(euid
, arg2
)
11411 || put_user_u32(suid
, arg3
))
11417 #ifdef TARGET_NR_setresgid32
11418 case TARGET_NR_setresgid32
:
11419 ret
= get_errno(sys_setresgid(arg1
, arg2
, arg3
));
11422 #ifdef TARGET_NR_getresgid32
11423 case TARGET_NR_getresgid32
:
11425 gid_t rgid
, egid
, sgid
;
11426 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
11427 if (!is_error(ret
)) {
11428 if (put_user_u32(rgid
, arg1
)
11429 || put_user_u32(egid
, arg2
)
11430 || put_user_u32(sgid
, arg3
))
11436 #ifdef TARGET_NR_chown32
11437 case TARGET_NR_chown32
:
11438 if (!(p
= lock_user_string(arg1
)))
11440 ret
= get_errno(chown(p
, arg2
, arg3
));
11441 unlock_user(p
, arg1
, 0);
11444 #ifdef TARGET_NR_setuid32
11445 case TARGET_NR_setuid32
:
11446 ret
= get_errno(sys_setuid(arg1
));
11449 #ifdef TARGET_NR_setgid32
11450 case TARGET_NR_setgid32
:
11451 ret
= get_errno(sys_setgid(arg1
));
11454 #ifdef TARGET_NR_setfsuid32
11455 case TARGET_NR_setfsuid32
:
11456 ret
= get_errno(setfsuid(arg1
));
11459 #ifdef TARGET_NR_setfsgid32
11460 case TARGET_NR_setfsgid32
:
11461 ret
= get_errno(setfsgid(arg1
));
11465 case TARGET_NR_pivot_root
:
11466 goto unimplemented
;
11467 #ifdef TARGET_NR_mincore
11468 case TARGET_NR_mincore
:
11471 ret
= -TARGET_ENOMEM
;
11472 a
= lock_user(VERIFY_READ
, arg1
, arg2
, 0);
11476 ret
= -TARGET_EFAULT
;
11477 p
= lock_user_string(arg3
);
11481 ret
= get_errno(mincore(a
, arg2
, p
));
11482 unlock_user(p
, arg3
, ret
);
11484 unlock_user(a
, arg1
, 0);
11488 #ifdef TARGET_NR_arm_fadvise64_64
11489 case TARGET_NR_arm_fadvise64_64
:
11490 /* arm_fadvise64_64 looks like fadvise64_64 but
11491 * with different argument order: fd, advice, offset, len
11492 * rather than the usual fd, offset, len, advice.
11493 * Note that offset and len are both 64-bit so appear as
11494 * pairs of 32-bit registers.
11496 ret
= posix_fadvise(arg1
, target_offset64(arg3
, arg4
),
11497 target_offset64(arg5
, arg6
), arg2
);
11498 ret
= -host_to_target_errno(ret
);
11502 #if TARGET_ABI_BITS == 32
11504 #ifdef TARGET_NR_fadvise64_64
11505 case TARGET_NR_fadvise64_64
:
11506 #if defined(TARGET_PPC)
11507 /* 6 args: fd, advice, offset (high, low), len (high, low) */
11515 /* 6 args: fd, offset (high, low), len (high, low), advice */
11516 if (regpairs_aligned(cpu_env
, num
)) {
11517 /* offset is in (3,4), len in (5,6) and advice in 7 */
11525 ret
= -host_to_target_errno(posix_fadvise(arg1
,
11526 target_offset64(arg2
, arg3
),
11527 target_offset64(arg4
, arg5
),
11532 #ifdef TARGET_NR_fadvise64
11533 case TARGET_NR_fadvise64
:
11534 /* 5 args: fd, offset (high, low), len, advice */
11535 if (regpairs_aligned(cpu_env
, num
)) {
11536 /* offset is in (3,4), len in 5 and advice in 6 */
11542 ret
= -host_to_target_errno(posix_fadvise(arg1
,
11543 target_offset64(arg2
, arg3
),
11548 #else /* not a 32-bit ABI */
11549 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
11550 #ifdef TARGET_NR_fadvise64_64
11551 case TARGET_NR_fadvise64_64
:
11553 #ifdef TARGET_NR_fadvise64
11554 case TARGET_NR_fadvise64
:
11556 #ifdef TARGET_S390X
11558 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
11559 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
11560 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
11561 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
11565 ret
= -host_to_target_errno(posix_fadvise(arg1
, arg2
, arg3
, arg4
));
11568 #endif /* end of 64-bit ABI fadvise handling */
11570 #ifdef TARGET_NR_madvise
11571 case TARGET_NR_madvise
:
11572 /* A straight passthrough may not be safe because qemu sometimes
11573 turns private file-backed mappings into anonymous mappings.
11574 This will break MADV_DONTNEED.
11575 This is a hint, so ignoring and returning success is ok. */
11576 ret
= get_errno(0);
11579 #if TARGET_ABI_BITS == 32
11580 case TARGET_NR_fcntl64
:
11584 from_flock64_fn
*copyfrom
= copy_from_user_flock64
;
11585 to_flock64_fn
*copyto
= copy_to_user_flock64
;
11588 if (((CPUARMState
*)cpu_env
)->eabi
) {
11589 copyfrom
= copy_from_user_eabi_flock64
;
11590 copyto
= copy_to_user_eabi_flock64
;
11594 cmd
= target_to_host_fcntl_cmd(arg2
);
11595 if (cmd
== -TARGET_EINVAL
) {
11601 case TARGET_F_GETLK64
:
11602 ret
= copyfrom(&fl
, arg3
);
11606 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
11608 ret
= copyto(arg3
, &fl
);
11612 case TARGET_F_SETLK64
:
11613 case TARGET_F_SETLKW64
:
11614 ret
= copyfrom(&fl
, arg3
);
11618 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
11621 ret
= do_fcntl(arg1
, arg2
, arg3
);
11627 #ifdef TARGET_NR_cacheflush
11628 case TARGET_NR_cacheflush
:
11629 /* self-modifying code is handled automatically, so nothing needed */
11633 #ifdef TARGET_NR_security
11634 case TARGET_NR_security
:
11635 goto unimplemented
;
11637 #ifdef TARGET_NR_getpagesize
11638 case TARGET_NR_getpagesize
:
11639 ret
= TARGET_PAGE_SIZE
;
11642 case TARGET_NR_gettid
:
11643 ret
= get_errno(gettid());
11645 #ifdef TARGET_NR_readahead
11646 case TARGET_NR_readahead
:
11647 #if TARGET_ABI_BITS == 32
11648 if (regpairs_aligned(cpu_env
, num
)) {
11653 ret
= get_errno(readahead(arg1
, target_offset64(arg2
, arg3
) , arg4
));
11655 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
11660 #ifdef TARGET_NR_setxattr
11661 case TARGET_NR_listxattr
:
11662 case TARGET_NR_llistxattr
:
11666 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11668 ret
= -TARGET_EFAULT
;
11672 p
= lock_user_string(arg1
);
11674 if (num
== TARGET_NR_listxattr
) {
11675 ret
= get_errno(listxattr(p
, b
, arg3
));
11677 ret
= get_errno(llistxattr(p
, b
, arg3
));
11680 ret
= -TARGET_EFAULT
;
11682 unlock_user(p
, arg1
, 0);
11683 unlock_user(b
, arg2
, arg3
);
11686 case TARGET_NR_flistxattr
:
11690 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11692 ret
= -TARGET_EFAULT
;
11696 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
11697 unlock_user(b
, arg2
, arg3
);
11700 case TARGET_NR_setxattr
:
11701 case TARGET_NR_lsetxattr
:
11703 void *p
, *n
, *v
= 0;
11705 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
11707 ret
= -TARGET_EFAULT
;
11711 p
= lock_user_string(arg1
);
11712 n
= lock_user_string(arg2
);
11714 if (num
== TARGET_NR_setxattr
) {
11715 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
11717 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
11720 ret
= -TARGET_EFAULT
;
11722 unlock_user(p
, arg1
, 0);
11723 unlock_user(n
, arg2
, 0);
11724 unlock_user(v
, arg3
, 0);
11727 case TARGET_NR_fsetxattr
:
11731 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
11733 ret
= -TARGET_EFAULT
;
11737 n
= lock_user_string(arg2
);
11739 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
11741 ret
= -TARGET_EFAULT
;
11743 unlock_user(n
, arg2
, 0);
11744 unlock_user(v
, arg3
, 0);
11747 case TARGET_NR_getxattr
:
11748 case TARGET_NR_lgetxattr
:
11750 void *p
, *n
, *v
= 0;
11752 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
11754 ret
= -TARGET_EFAULT
;
11758 p
= lock_user_string(arg1
);
11759 n
= lock_user_string(arg2
);
11761 if (num
== TARGET_NR_getxattr
) {
11762 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
11764 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
11767 ret
= -TARGET_EFAULT
;
11769 unlock_user(p
, arg1
, 0);
11770 unlock_user(n
, arg2
, 0);
11771 unlock_user(v
, arg3
, arg4
);
11774 case TARGET_NR_fgetxattr
:
11778 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
11780 ret
= -TARGET_EFAULT
;
11784 n
= lock_user_string(arg2
);
11786 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
11788 ret
= -TARGET_EFAULT
;
11790 unlock_user(n
, arg2
, 0);
11791 unlock_user(v
, arg3
, arg4
);
11794 case TARGET_NR_removexattr
:
11795 case TARGET_NR_lremovexattr
:
11798 p
= lock_user_string(arg1
);
11799 n
= lock_user_string(arg2
);
11801 if (num
== TARGET_NR_removexattr
) {
11802 ret
= get_errno(removexattr(p
, n
));
11804 ret
= get_errno(lremovexattr(p
, n
));
11807 ret
= -TARGET_EFAULT
;
11809 unlock_user(p
, arg1
, 0);
11810 unlock_user(n
, arg2
, 0);
11813 case TARGET_NR_fremovexattr
:
11816 n
= lock_user_string(arg2
);
11818 ret
= get_errno(fremovexattr(arg1
, n
));
11820 ret
= -TARGET_EFAULT
;
11822 unlock_user(n
, arg2
, 0);
11826 #endif /* CONFIG_ATTR */
11827 #ifdef TARGET_NR_set_thread_area
11828 case TARGET_NR_set_thread_area
:
11829 #if defined(TARGET_MIPS)
11830 ((CPUMIPSState
*) cpu_env
)->active_tc
.CP0_UserLocal
= arg1
;
11833 #elif defined(TARGET_CRIS)
11835 ret
= -TARGET_EINVAL
;
11837 ((CPUCRISState
*) cpu_env
)->pregs
[PR_PID
] = arg1
;
11841 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
11842 ret
= do_set_thread_area(cpu_env
, arg1
);
11844 #elif defined(TARGET_M68K)
11846 TaskState
*ts
= cpu
->opaque
;
11847 ts
->tp_value
= arg1
;
11852 goto unimplemented_nowarn
;
11855 #ifdef TARGET_NR_get_thread_area
11856 case TARGET_NR_get_thread_area
:
11857 #if defined(TARGET_I386) && defined(TARGET_ABI32)
11858 ret
= do_get_thread_area(cpu_env
, arg1
);
11860 #elif defined(TARGET_M68K)
11862 TaskState
*ts
= cpu
->opaque
;
11863 ret
= ts
->tp_value
;
11867 goto unimplemented_nowarn
;
11870 #ifdef TARGET_NR_getdomainname
11871 case TARGET_NR_getdomainname
:
11872 goto unimplemented_nowarn
;
11875 #ifdef TARGET_NR_clock_gettime
11876 case TARGET_NR_clock_gettime
:
11878 struct timespec ts
;
11879 ret
= get_errno(clock_gettime(arg1
, &ts
));
11880 if (!is_error(ret
)) {
11881 host_to_target_timespec(arg2
, &ts
);
11886 #ifdef TARGET_NR_clock_getres
11887 case TARGET_NR_clock_getres
:
11889 struct timespec ts
;
11890 ret
= get_errno(clock_getres(arg1
, &ts
));
11891 if (!is_error(ret
)) {
11892 host_to_target_timespec(arg2
, &ts
);
11897 #ifdef TARGET_NR_clock_nanosleep
11898 case TARGET_NR_clock_nanosleep
:
11900 struct timespec ts
;
11901 target_to_host_timespec(&ts
, arg3
);
11902 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
11903 &ts
, arg4
? &ts
: NULL
));
11905 host_to_target_timespec(arg4
, &ts
);
11907 #if defined(TARGET_PPC)
11908 /* clock_nanosleep is odd in that it returns positive errno values.
11909 * On PPC, CR0 bit 3 should be set in such a situation. */
11910 if (ret
&& ret
!= -TARGET_ERESTARTSYS
) {
11911 ((CPUPPCState
*)cpu_env
)->crf
[0] |= 1;
11918 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
11919 case TARGET_NR_set_tid_address
:
11920 ret
= get_errno(set_tid_address((int *)g2h(arg1
)));
11924 case TARGET_NR_tkill
:
11925 ret
= get_errno(safe_tkill((int)arg1
, target_to_host_signal(arg2
)));
11928 case TARGET_NR_tgkill
:
11929 ret
= get_errno(safe_tgkill((int)arg1
, (int)arg2
,
11930 target_to_host_signal(arg3
)));
11933 #ifdef TARGET_NR_set_robust_list
11934 case TARGET_NR_set_robust_list
:
11935 case TARGET_NR_get_robust_list
:
11936 /* The ABI for supporting robust futexes has userspace pass
11937 * the kernel a pointer to a linked list which is updated by
11938 * userspace after the syscall; the list is walked by the kernel
11939 * when the thread exits. Since the linked list in QEMU guest
11940 * memory isn't a valid linked list for the host and we have
11941 * no way to reliably intercept the thread-death event, we can't
11942 * support these. Silently return ENOSYS so that guest userspace
11943 * falls back to a non-robust futex implementation (which should
11944 * be OK except in the corner case of the guest crashing while
11945 * holding a mutex that is shared with another process via
11948 goto unimplemented_nowarn
;
11951 #if defined(TARGET_NR_utimensat)
11952 case TARGET_NR_utimensat
:
11954 struct timespec
*tsp
, ts
[2];
11958 target_to_host_timespec(ts
, arg3
);
11959 target_to_host_timespec(ts
+1, arg3
+sizeof(struct target_timespec
));
11963 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
11965 if (!(p
= lock_user_string(arg2
))) {
11966 ret
= -TARGET_EFAULT
;
11969 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
11970 unlock_user(p
, arg2
, 0);
11975 case TARGET_NR_futex
:
11976 ret
= do_futex(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
11978 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
11979 case TARGET_NR_inotify_init
:
11980 ret
= get_errno(sys_inotify_init());
11982 fd_trans_register(ret
, &target_inotify_trans
);
11986 #ifdef CONFIG_INOTIFY1
11987 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
11988 case TARGET_NR_inotify_init1
:
11989 ret
= get_errno(sys_inotify_init1(target_to_host_bitmask(arg1
,
11990 fcntl_flags_tbl
)));
11992 fd_trans_register(ret
, &target_inotify_trans
);
11997 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
11998 case TARGET_NR_inotify_add_watch
:
11999 p
= lock_user_string(arg2
);
12000 ret
= get_errno(sys_inotify_add_watch(arg1
, path(p
), arg3
));
12001 unlock_user(p
, arg2
, 0);
12004 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
12005 case TARGET_NR_inotify_rm_watch
:
12006 ret
= get_errno(sys_inotify_rm_watch(arg1
, arg2
));
12010 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
12011 case TARGET_NR_mq_open
:
12013 struct mq_attr posix_mq_attr
;
12014 struct mq_attr
*pposix_mq_attr
;
12017 host_flags
= target_to_host_bitmask(arg2
, fcntl_flags_tbl
);
12018 pposix_mq_attr
= NULL
;
12020 if (copy_from_user_mq_attr(&posix_mq_attr
, arg4
) != 0) {
12023 pposix_mq_attr
= &posix_mq_attr
;
12025 p
= lock_user_string(arg1
- 1);
12029 ret
= get_errno(mq_open(p
, host_flags
, arg3
, pposix_mq_attr
));
12030 unlock_user (p
, arg1
, 0);
12034 case TARGET_NR_mq_unlink
:
12035 p
= lock_user_string(arg1
- 1);
12037 ret
= -TARGET_EFAULT
;
12040 ret
= get_errno(mq_unlink(p
));
12041 unlock_user (p
, arg1
, 0);
12044 case TARGET_NR_mq_timedsend
:
12046 struct timespec ts
;
12048 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12050 target_to_host_timespec(&ts
, arg5
);
12051 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
12052 host_to_target_timespec(arg5
, &ts
);
12054 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
12056 unlock_user (p
, arg2
, arg3
);
12060 case TARGET_NR_mq_timedreceive
:
12062 struct timespec ts
;
12065 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12067 target_to_host_timespec(&ts
, arg5
);
12068 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12070 host_to_target_timespec(arg5
, &ts
);
12072 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12075 unlock_user (p
, arg2
, arg3
);
12077 put_user_u32(prio
, arg4
);
12081 /* Not implemented for now... */
12082 /* case TARGET_NR_mq_notify: */
12085 case TARGET_NR_mq_getsetattr
:
12087 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
12090 ret
= mq_getattr(arg1
, &posix_mq_attr_out
);
12091 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
12094 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
12095 ret
|= mq_setattr(arg1
, &posix_mq_attr_in
, &posix_mq_attr_out
);
12102 #ifdef CONFIG_SPLICE
12103 #ifdef TARGET_NR_tee
12104 case TARGET_NR_tee
:
12106 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
12110 #ifdef TARGET_NR_splice
12111 case TARGET_NR_splice
:
12113 loff_t loff_in
, loff_out
;
12114 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
12116 if (get_user_u64(loff_in
, arg2
)) {
12119 ploff_in
= &loff_in
;
12122 if (get_user_u64(loff_out
, arg4
)) {
12125 ploff_out
= &loff_out
;
12127 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
12129 if (put_user_u64(loff_in
, arg2
)) {
12134 if (put_user_u64(loff_out
, arg4
)) {
12141 #ifdef TARGET_NR_vmsplice
12142 case TARGET_NR_vmsplice
:
12144 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
12146 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
12147 unlock_iovec(vec
, arg2
, arg3
, 0);
12149 ret
= -host_to_target_errno(errno
);
12154 #endif /* CONFIG_SPLICE */
12155 #ifdef CONFIG_EVENTFD
12156 #if defined(TARGET_NR_eventfd)
12157 case TARGET_NR_eventfd
:
12158 ret
= get_errno(eventfd(arg1
, 0));
12160 fd_trans_register(ret
, &target_eventfd_trans
);
12164 #if defined(TARGET_NR_eventfd2)
12165 case TARGET_NR_eventfd2
:
12167 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
));
12168 if (arg2
& TARGET_O_NONBLOCK
) {
12169 host_flags
|= O_NONBLOCK
;
12171 if (arg2
& TARGET_O_CLOEXEC
) {
12172 host_flags
|= O_CLOEXEC
;
12174 ret
= get_errno(eventfd(arg1
, host_flags
));
12176 fd_trans_register(ret
, &target_eventfd_trans
);
12181 #endif /* CONFIG_EVENTFD */
12182 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
12183 case TARGET_NR_fallocate
:
12184 #if TARGET_ABI_BITS == 32
12185 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
12186 target_offset64(arg5
, arg6
)));
12188 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
12192 #if defined(CONFIG_SYNC_FILE_RANGE)
12193 #if defined(TARGET_NR_sync_file_range)
12194 case TARGET_NR_sync_file_range
:
12195 #if TARGET_ABI_BITS == 32
12196 #if defined(TARGET_MIPS)
12197 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
12198 target_offset64(arg5
, arg6
), arg7
));
12200 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
12201 target_offset64(arg4
, arg5
), arg6
));
12202 #endif /* !TARGET_MIPS */
12204 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
12208 #if defined(TARGET_NR_sync_file_range2)
12209 case TARGET_NR_sync_file_range2
:
12210 /* This is like sync_file_range but the arguments are reordered */
12211 #if TARGET_ABI_BITS == 32
12212 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
12213 target_offset64(arg5
, arg6
), arg2
));
12215 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
12220 #if defined(TARGET_NR_signalfd4)
12221 case TARGET_NR_signalfd4
:
12222 ret
= do_signalfd4(arg1
, arg2
, arg4
);
12225 #if defined(TARGET_NR_signalfd)
12226 case TARGET_NR_signalfd
:
12227 ret
= do_signalfd4(arg1
, arg2
, 0);
12230 #if defined(CONFIG_EPOLL)
12231 #if defined(TARGET_NR_epoll_create)
12232 case TARGET_NR_epoll_create
:
12233 ret
= get_errno(epoll_create(arg1
));
12236 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
12237 case TARGET_NR_epoll_create1
:
12238 ret
= get_errno(epoll_create1(arg1
));
12241 #if defined(TARGET_NR_epoll_ctl)
12242 case TARGET_NR_epoll_ctl
:
12244 struct epoll_event ep
;
12245 struct epoll_event
*epp
= 0;
12247 struct target_epoll_event
*target_ep
;
12248 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
12251 ep
.events
= tswap32(target_ep
->events
);
12252 /* The epoll_data_t union is just opaque data to the kernel,
12253 * so we transfer all 64 bits across and need not worry what
12254 * actual data type it is.
12256 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
12257 unlock_user_struct(target_ep
, arg4
, 0);
12260 ret
= get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
12265 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
12266 #if defined(TARGET_NR_epoll_wait)
12267 case TARGET_NR_epoll_wait
:
12269 #if defined(TARGET_NR_epoll_pwait)
12270 case TARGET_NR_epoll_pwait
:
12273 struct target_epoll_event
*target_ep
;
12274 struct epoll_event
*ep
;
12276 int maxevents
= arg3
;
12277 int timeout
= arg4
;
12279 if (maxevents
<= 0 || maxevents
> TARGET_EP_MAX_EVENTS
) {
12280 ret
= -TARGET_EINVAL
;
12284 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
12285 maxevents
* sizeof(struct target_epoll_event
), 1);
12290 ep
= g_try_new(struct epoll_event
, maxevents
);
12292 unlock_user(target_ep
, arg2
, 0);
12293 ret
= -TARGET_ENOMEM
;
12298 #if defined(TARGET_NR_epoll_pwait)
12299 case TARGET_NR_epoll_pwait
:
12301 target_sigset_t
*target_set
;
12302 sigset_t _set
, *set
= &_set
;
12305 if (arg6
!= sizeof(target_sigset_t
)) {
12306 ret
= -TARGET_EINVAL
;
12310 target_set
= lock_user(VERIFY_READ
, arg5
,
12311 sizeof(target_sigset_t
), 1);
12313 ret
= -TARGET_EFAULT
;
12316 target_to_host_sigset(set
, target_set
);
12317 unlock_user(target_set
, arg5
, 0);
12322 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
12323 set
, SIGSET_T_SIZE
));
12327 #if defined(TARGET_NR_epoll_wait)
12328 case TARGET_NR_epoll_wait
:
12329 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
12334 ret
= -TARGET_ENOSYS
;
12336 if (!is_error(ret
)) {
12338 for (i
= 0; i
< ret
; i
++) {
12339 target_ep
[i
].events
= tswap32(ep
[i
].events
);
12340 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
12342 unlock_user(target_ep
, arg2
,
12343 ret
* sizeof(struct target_epoll_event
));
12345 unlock_user(target_ep
, arg2
, 0);
12352 #ifdef TARGET_NR_prlimit64
12353 case TARGET_NR_prlimit64
:
12355 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
12356 struct target_rlimit64
*target_rnew
, *target_rold
;
12357 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
12358 int resource
= target_to_host_resource(arg2
);
12360 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
12363 rnew
.rlim_cur
= tswap64(target_rnew
->rlim_cur
);
12364 rnew
.rlim_max
= tswap64(target_rnew
->rlim_max
);
12365 unlock_user_struct(target_rnew
, arg3
, 0);
12369 ret
= get_errno(sys_prlimit64(arg1
, resource
, rnewp
, arg4
? &rold
: 0));
12370 if (!is_error(ret
) && arg4
) {
12371 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
12374 target_rold
->rlim_cur
= tswap64(rold
.rlim_cur
);
12375 target_rold
->rlim_max
= tswap64(rold
.rlim_max
);
12376 unlock_user_struct(target_rold
, arg4
, 1);
12381 #ifdef TARGET_NR_gethostname
12382 case TARGET_NR_gethostname
:
12384 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
12386 ret
= get_errno(gethostname(name
, arg2
));
12387 unlock_user(name
, arg1
, arg2
);
12389 ret
= -TARGET_EFAULT
;
12394 #ifdef TARGET_NR_atomic_cmpxchg_32
12395 case TARGET_NR_atomic_cmpxchg_32
:
12397 /* should use start_exclusive from main.c */
12398 abi_ulong mem_value
;
12399 if (get_user_u32(mem_value
, arg6
)) {
12400 target_siginfo_t info
;
12401 info
.si_signo
= SIGSEGV
;
12403 info
.si_code
= TARGET_SEGV_MAPERR
;
12404 info
._sifields
._sigfault
._addr
= arg6
;
12405 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
,
12406 QEMU_SI_FAULT
, &info
);
12410 if (mem_value
== arg2
)
12411 put_user_u32(arg1
, arg6
);
12416 #ifdef TARGET_NR_atomic_barrier
12417 case TARGET_NR_atomic_barrier
:
12419 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
12425 #ifdef TARGET_NR_timer_create
12426 case TARGET_NR_timer_create
:
12428 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
12430 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
12433 int timer_index
= next_free_host_timer();
12435 if (timer_index
< 0) {
12436 ret
= -TARGET_EAGAIN
;
12438 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
12441 phost_sevp
= &host_sevp
;
12442 ret
= target_to_host_sigevent(phost_sevp
, arg2
);
12448 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
12452 if (put_user(TIMER_MAGIC
| timer_index
, arg3
, target_timer_t
)) {
12461 #ifdef TARGET_NR_timer_settime
12462 case TARGET_NR_timer_settime
:
12464 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
12465 * struct itimerspec * old_value */
12466 target_timer_t timerid
= get_timer_id(arg1
);
12470 } else if (arg3
== 0) {
12471 ret
= -TARGET_EINVAL
;
12473 timer_t htimer
= g_posix_timers
[timerid
];
12474 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
12476 if (target_to_host_itimerspec(&hspec_new
, arg3
)) {
12480 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
12481 if (arg4
&& host_to_target_itimerspec(arg4
, &hspec_old
)) {
12489 #ifdef TARGET_NR_timer_gettime
12490 case TARGET_NR_timer_gettime
:
12492 /* args: timer_t timerid, struct itimerspec *curr_value */
12493 target_timer_t timerid
= get_timer_id(arg1
);
12497 } else if (!arg2
) {
12498 ret
= -TARGET_EFAULT
;
12500 timer_t htimer
= g_posix_timers
[timerid
];
12501 struct itimerspec hspec
;
12502 ret
= get_errno(timer_gettime(htimer
, &hspec
));
12504 if (host_to_target_itimerspec(arg2
, &hspec
)) {
12505 ret
= -TARGET_EFAULT
;
12512 #ifdef TARGET_NR_timer_getoverrun
12513 case TARGET_NR_timer_getoverrun
:
12515 /* args: timer_t timerid */
12516 target_timer_t timerid
= get_timer_id(arg1
);
12521 timer_t htimer
= g_posix_timers
[timerid
];
12522 ret
= get_errno(timer_getoverrun(htimer
));
12524 fd_trans_unregister(ret
);
12529 #ifdef TARGET_NR_timer_delete
12530 case TARGET_NR_timer_delete
:
12532 /* args: timer_t timerid */
12533 target_timer_t timerid
= get_timer_id(arg1
);
12538 timer_t htimer
= g_posix_timers
[timerid
];
12539 ret
= get_errno(timer_delete(htimer
));
12540 g_posix_timers
[timerid
] = 0;
12546 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
12547 case TARGET_NR_timerfd_create
:
12548 ret
= get_errno(timerfd_create(arg1
,
12549 target_to_host_bitmask(arg2
, fcntl_flags_tbl
)));
12553 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
12554 case TARGET_NR_timerfd_gettime
:
12556 struct itimerspec its_curr
;
12558 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
12560 if (arg2
&& host_to_target_itimerspec(arg2
, &its_curr
)) {
12567 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
12568 case TARGET_NR_timerfd_settime
:
12570 struct itimerspec its_new
, its_old
, *p_new
;
12573 if (target_to_host_itimerspec(&its_new
, arg3
)) {
12581 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
12583 if (arg4
&& host_to_target_itimerspec(arg4
, &its_old
)) {
12590 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
12591 case TARGET_NR_ioprio_get
:
12592 ret
= get_errno(ioprio_get(arg1
, arg2
));
12596 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
12597 case TARGET_NR_ioprio_set
:
12598 ret
= get_errno(ioprio_set(arg1
, arg2
, arg3
));
12602 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
12603 case TARGET_NR_setns
:
12604 ret
= get_errno(setns(arg1
, arg2
));
12607 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
12608 case TARGET_NR_unshare
:
12609 ret
= get_errno(unshare(arg1
));
12612 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
12613 case TARGET_NR_kcmp
:
12614 ret
= get_errno(kcmp(arg1
, arg2
, arg3
, arg4
, arg5
));
12620 gemu_log("qemu: Unsupported syscall: %d\n", num
);
12621 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
12622 unimplemented_nowarn
:
12624 ret
= -TARGET_ENOSYS
;
12629 gemu_log(" = " TARGET_ABI_FMT_ld
"\n", ret
);
12632 print_syscall_ret(num
, ret
);
12633 trace_guest_user_syscall_ret(cpu
, num
, ret
);
12636 ret
= -TARGET_EFAULT
;