]> git.proxmox.com Git - mirror_qemu.git/blob - linux-user/syscall.c
linux-user: Don't use alloca() for epoll_wait's epoll event array
[mirror_qemu.git] / linux-user / syscall.c
1 /*
2 * Linux syscalls
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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/>.
18 */
19 #define _ATFILE_SOURCE
20 #include "qemu/osdep.h"
21 #include "qemu/cutils.h"
22 #include "qemu/path.h"
23 #include <elf.h>
24 #include <endian.h>
25 #include <grp.h>
26 #include <sys/ipc.h>
27 #include <sys/msg.h>
28 #include <sys/wait.h>
29 #include <sys/mount.h>
30 #include <sys/file.h>
31 #include <sys/fsuid.h>
32 #include <sys/personality.h>
33 #include <sys/prctl.h>
34 #include <sys/resource.h>
35 #include <sys/swap.h>
36 #include <linux/capability.h>
37 #include <sched.h>
38 #include <sys/timex.h>
39 #ifdef __ia64__
40 int __clone2(int (*fn)(void *), void *child_stack_base,
41 size_t stack_size, int flags, void *arg, ...);
42 #endif
43 #include <sys/socket.h>
44 #include <sys/un.h>
45 #include <sys/uio.h>
46 #include <poll.h>
47 #include <sys/times.h>
48 #include <sys/shm.h>
49 #include <sys/sem.h>
50 #include <sys/statfs.h>
51 #include <utime.h>
52 #include <sys/sysinfo.h>
53 #include <sys/signalfd.h>
54 //#include <sys/user.h>
55 #include <netinet/ip.h>
56 #include <netinet/tcp.h>
57 #include <linux/wireless.h>
58 #include <linux/icmp.h>
59 #include "qemu-common.h"
60 #ifdef CONFIG_TIMERFD
61 #include <sys/timerfd.h>
62 #endif
63 #ifdef TARGET_GPROF
64 #include <sys/gmon.h>
65 #endif
66 #ifdef CONFIG_EVENTFD
67 #include <sys/eventfd.h>
68 #endif
69 #ifdef CONFIG_EPOLL
70 #include <sys/epoll.h>
71 #endif
72 #ifdef CONFIG_ATTR
73 #include "qemu/xattr.h"
74 #endif
75 #ifdef CONFIG_SENDFILE
76 #include <sys/sendfile.h>
77 #endif
78
79 #define termios host_termios
80 #define winsize host_winsize
81 #define termio host_termio
82 #define sgttyb host_sgttyb /* same as target */
83 #define tchars host_tchars /* same as target */
84 #define ltchars host_ltchars /* same as target */
85
86 #include <linux/termios.h>
87 #include <linux/unistd.h>
88 #include <linux/cdrom.h>
89 #include <linux/hdreg.h>
90 #include <linux/soundcard.h>
91 #include <linux/kd.h>
92 #include <linux/mtio.h>
93 #include <linux/fs.h>
94 #if defined(CONFIG_FIEMAP)
95 #include <linux/fiemap.h>
96 #endif
97 #include <linux/fb.h>
98 #include <linux/vt.h>
99 #include <linux/dm-ioctl.h>
100 #include <linux/reboot.h>
101 #include <linux/route.h>
102 #include <linux/filter.h>
103 #include <linux/blkpg.h>
104 #include <netpacket/packet.h>
105 #include <linux/netlink.h>
106 #ifdef CONFIG_RTNETLINK
107 #include <linux/rtnetlink.h>
108 #include <linux/if_bridge.h>
109 #endif
110 #include <linux/audit.h>
111 #include "linux_loop.h"
112 #include "uname.h"
113
114 #include "qemu.h"
115
116 #ifndef CLONE_IO
117 #define CLONE_IO 0x80000000 /* Clone io context */
118 #endif
119
120 /* We can't directly call the host clone syscall, because this will
121 * badly confuse libc (breaking mutexes, for example). So we must
122 * divide clone flags into:
123 * * flag combinations that look like pthread_create()
124 * * flag combinations that look like fork()
125 * * flags we can implement within QEMU itself
126 * * flags we can't support and will return an error for
127 */
128 /* For thread creation, all these flags must be present; for
129 * fork, none must be present.
130 */
131 #define CLONE_THREAD_FLAGS \
132 (CLONE_VM | CLONE_FS | CLONE_FILES | \
133 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
134
135 /* These flags are ignored:
136 * CLONE_DETACHED is now ignored by the kernel;
137 * CLONE_IO is just an optimisation hint to the I/O scheduler
138 */
139 #define CLONE_IGNORED_FLAGS \
140 (CLONE_DETACHED | CLONE_IO)
141
142 /* Flags for fork which we can implement within QEMU itself */
143 #define CLONE_OPTIONAL_FORK_FLAGS \
144 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
145 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
146
147 /* Flags for thread creation which we can implement within QEMU itself */
148 #define CLONE_OPTIONAL_THREAD_FLAGS \
149 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
150 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
151
152 #define CLONE_INVALID_FORK_FLAGS \
153 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
154
155 #define CLONE_INVALID_THREAD_FLAGS \
156 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
157 CLONE_IGNORED_FLAGS))
158
159 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
160 * have almost all been allocated. We cannot support any of
161 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
162 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
163 * The checks against the invalid thread masks above will catch these.
164 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
165 */
166
167 //#define DEBUG
168 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
169 * once. This exercises the codepaths for restart.
170 */
171 //#define DEBUG_ERESTARTSYS
172
173 //#include <linux/msdos_fs.h>
174 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
175 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
176
177 #undef _syscall0
178 #undef _syscall1
179 #undef _syscall2
180 #undef _syscall3
181 #undef _syscall4
182 #undef _syscall5
183 #undef _syscall6
184
185 #define _syscall0(type,name) \
186 static type name (void) \
187 { \
188 return syscall(__NR_##name); \
189 }
190
191 #define _syscall1(type,name,type1,arg1) \
192 static type name (type1 arg1) \
193 { \
194 return syscall(__NR_##name, arg1); \
195 }
196
197 #define _syscall2(type,name,type1,arg1,type2,arg2) \
198 static type name (type1 arg1,type2 arg2) \
199 { \
200 return syscall(__NR_##name, arg1, arg2); \
201 }
202
203 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
204 static type name (type1 arg1,type2 arg2,type3 arg3) \
205 { \
206 return syscall(__NR_##name, arg1, arg2, arg3); \
207 }
208
209 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
210 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
211 { \
212 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
213 }
214
215 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
216 type5,arg5) \
217 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
218 { \
219 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
220 }
221
222
223 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
224 type5,arg5,type6,arg6) \
225 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
226 type6 arg6) \
227 { \
228 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
229 }
230
231
232 #define __NR_sys_uname __NR_uname
233 #define __NR_sys_getcwd1 __NR_getcwd
234 #define __NR_sys_getdents __NR_getdents
235 #define __NR_sys_getdents64 __NR_getdents64
236 #define __NR_sys_getpriority __NR_getpriority
237 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
238 #define __NR_sys_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
243
244 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
245 defined(__s390x__)
246 #define __NR__llseek __NR_lseek
247 #endif
248
249 /* Newer kernel ports have llseek() instead of _llseek() */
250 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
251 #define TARGET_NR__llseek TARGET_NR_llseek
252 #endif
253
254 #ifdef __NR_gettid
255 _syscall0(int, gettid)
256 #else
257 /* This is a replacement for the host gettid() and must return a host
258 errno. */
259 static int gettid(void) {
260 return -ENOSYS;
261 }
262 #endif
263 #if defined(TARGET_NR_getdents) && defined(__NR_getdents)
264 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
265 #endif
266 #if !defined(__NR_getdents) || \
267 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
268 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
269 #endif
270 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
271 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
272 loff_t *, res, uint, wh);
273 #endif
274 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
275 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
276 #ifdef __NR_exit_group
277 _syscall1(int,exit_group,int,error_code)
278 #endif
279 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
280 _syscall1(int,set_tid_address,int *,tidptr)
281 #endif
282 #if defined(TARGET_NR_futex) && defined(__NR_futex)
283 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
284 const struct timespec *,timeout,int *,uaddr2,int,val3)
285 #endif
286 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
287 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
288 unsigned long *, user_mask_ptr);
289 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
290 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
291 unsigned long *, user_mask_ptr);
292 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
293 void *, arg);
294 _syscall2(int, capget, struct __user_cap_header_struct *, header,
295 struct __user_cap_data_struct *, data);
296 _syscall2(int, capset, struct __user_cap_header_struct *, header,
297 struct __user_cap_data_struct *, data);
298 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
299 _syscall2(int, ioprio_get, int, which, int, who)
300 #endif
301 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
302 _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
303 #endif
304 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
305 _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
306 #endif
307
308 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
309 _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
310 unsigned long, idx1, unsigned long, idx2)
311 #endif
312
313 static bitmask_transtbl fcntl_flags_tbl[] = {
314 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
315 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
316 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
317 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
318 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
319 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
320 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
321 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
322 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
323 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
324 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
325 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
326 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
327 #if defined(O_DIRECT)
328 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
329 #endif
330 #if defined(O_NOATIME)
331 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
332 #endif
333 #if defined(O_CLOEXEC)
334 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
335 #endif
336 #if defined(O_PATH)
337 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
338 #endif
339 /* Don't terminate the list prematurely on 64-bit host+guest. */
340 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
341 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
342 #endif
343 { 0, 0, 0, 0 }
344 };
345
346 enum {
347 QEMU_IFLA_BR_UNSPEC,
348 QEMU_IFLA_BR_FORWARD_DELAY,
349 QEMU_IFLA_BR_HELLO_TIME,
350 QEMU_IFLA_BR_MAX_AGE,
351 QEMU_IFLA_BR_AGEING_TIME,
352 QEMU_IFLA_BR_STP_STATE,
353 QEMU_IFLA_BR_PRIORITY,
354 QEMU_IFLA_BR_VLAN_FILTERING,
355 QEMU_IFLA_BR_VLAN_PROTOCOL,
356 QEMU_IFLA_BR_GROUP_FWD_MASK,
357 QEMU_IFLA_BR_ROOT_ID,
358 QEMU_IFLA_BR_BRIDGE_ID,
359 QEMU_IFLA_BR_ROOT_PORT,
360 QEMU_IFLA_BR_ROOT_PATH_COST,
361 QEMU_IFLA_BR_TOPOLOGY_CHANGE,
362 QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
363 QEMU_IFLA_BR_HELLO_TIMER,
364 QEMU_IFLA_BR_TCN_TIMER,
365 QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER,
366 QEMU_IFLA_BR_GC_TIMER,
367 QEMU_IFLA_BR_GROUP_ADDR,
368 QEMU_IFLA_BR_FDB_FLUSH,
369 QEMU_IFLA_BR_MCAST_ROUTER,
370 QEMU_IFLA_BR_MCAST_SNOOPING,
371 QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR,
372 QEMU_IFLA_BR_MCAST_QUERIER,
373 QEMU_IFLA_BR_MCAST_HASH_ELASTICITY,
374 QEMU_IFLA_BR_MCAST_HASH_MAX,
375 QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT,
376 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT,
377 QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL,
378 QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL,
379 QEMU_IFLA_BR_MCAST_QUERIER_INTVL,
380 QEMU_IFLA_BR_MCAST_QUERY_INTVL,
381 QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL,
382 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL,
383 QEMU_IFLA_BR_NF_CALL_IPTABLES,
384 QEMU_IFLA_BR_NF_CALL_IP6TABLES,
385 QEMU_IFLA_BR_NF_CALL_ARPTABLES,
386 QEMU_IFLA_BR_VLAN_DEFAULT_PVID,
387 QEMU_IFLA_BR_PAD,
388 QEMU_IFLA_BR_VLAN_STATS_ENABLED,
389 QEMU_IFLA_BR_MCAST_STATS_ENABLED,
390 QEMU___IFLA_BR_MAX,
391 };
392
393 enum {
394 QEMU_IFLA_UNSPEC,
395 QEMU_IFLA_ADDRESS,
396 QEMU_IFLA_BROADCAST,
397 QEMU_IFLA_IFNAME,
398 QEMU_IFLA_MTU,
399 QEMU_IFLA_LINK,
400 QEMU_IFLA_QDISC,
401 QEMU_IFLA_STATS,
402 QEMU_IFLA_COST,
403 QEMU_IFLA_PRIORITY,
404 QEMU_IFLA_MASTER,
405 QEMU_IFLA_WIRELESS,
406 QEMU_IFLA_PROTINFO,
407 QEMU_IFLA_TXQLEN,
408 QEMU_IFLA_MAP,
409 QEMU_IFLA_WEIGHT,
410 QEMU_IFLA_OPERSTATE,
411 QEMU_IFLA_LINKMODE,
412 QEMU_IFLA_LINKINFO,
413 QEMU_IFLA_NET_NS_PID,
414 QEMU_IFLA_IFALIAS,
415 QEMU_IFLA_NUM_VF,
416 QEMU_IFLA_VFINFO_LIST,
417 QEMU_IFLA_STATS64,
418 QEMU_IFLA_VF_PORTS,
419 QEMU_IFLA_PORT_SELF,
420 QEMU_IFLA_AF_SPEC,
421 QEMU_IFLA_GROUP,
422 QEMU_IFLA_NET_NS_FD,
423 QEMU_IFLA_EXT_MASK,
424 QEMU_IFLA_PROMISCUITY,
425 QEMU_IFLA_NUM_TX_QUEUES,
426 QEMU_IFLA_NUM_RX_QUEUES,
427 QEMU_IFLA_CARRIER,
428 QEMU_IFLA_PHYS_PORT_ID,
429 QEMU_IFLA_CARRIER_CHANGES,
430 QEMU_IFLA_PHYS_SWITCH_ID,
431 QEMU_IFLA_LINK_NETNSID,
432 QEMU_IFLA_PHYS_PORT_NAME,
433 QEMU_IFLA_PROTO_DOWN,
434 QEMU_IFLA_GSO_MAX_SEGS,
435 QEMU_IFLA_GSO_MAX_SIZE,
436 QEMU_IFLA_PAD,
437 QEMU_IFLA_XDP,
438 QEMU___IFLA_MAX
439 };
440
441 enum {
442 QEMU_IFLA_BRPORT_UNSPEC,
443 QEMU_IFLA_BRPORT_STATE,
444 QEMU_IFLA_BRPORT_PRIORITY,
445 QEMU_IFLA_BRPORT_COST,
446 QEMU_IFLA_BRPORT_MODE,
447 QEMU_IFLA_BRPORT_GUARD,
448 QEMU_IFLA_BRPORT_PROTECT,
449 QEMU_IFLA_BRPORT_FAST_LEAVE,
450 QEMU_IFLA_BRPORT_LEARNING,
451 QEMU_IFLA_BRPORT_UNICAST_FLOOD,
452 QEMU_IFLA_BRPORT_PROXYARP,
453 QEMU_IFLA_BRPORT_LEARNING_SYNC,
454 QEMU_IFLA_BRPORT_PROXYARP_WIFI,
455 QEMU_IFLA_BRPORT_ROOT_ID,
456 QEMU_IFLA_BRPORT_BRIDGE_ID,
457 QEMU_IFLA_BRPORT_DESIGNATED_PORT,
458 QEMU_IFLA_BRPORT_DESIGNATED_COST,
459 QEMU_IFLA_BRPORT_ID,
460 QEMU_IFLA_BRPORT_NO,
461 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
462 QEMU_IFLA_BRPORT_CONFIG_PENDING,
463 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER,
464 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER,
465 QEMU_IFLA_BRPORT_HOLD_TIMER,
466 QEMU_IFLA_BRPORT_FLUSH,
467 QEMU_IFLA_BRPORT_MULTICAST_ROUTER,
468 QEMU_IFLA_BRPORT_PAD,
469 QEMU___IFLA_BRPORT_MAX
470 };
471
472 enum {
473 QEMU_IFLA_INFO_UNSPEC,
474 QEMU_IFLA_INFO_KIND,
475 QEMU_IFLA_INFO_DATA,
476 QEMU_IFLA_INFO_XSTATS,
477 QEMU_IFLA_INFO_SLAVE_KIND,
478 QEMU_IFLA_INFO_SLAVE_DATA,
479 QEMU___IFLA_INFO_MAX,
480 };
481
482 enum {
483 QEMU_IFLA_INET_UNSPEC,
484 QEMU_IFLA_INET_CONF,
485 QEMU___IFLA_INET_MAX,
486 };
487
488 enum {
489 QEMU_IFLA_INET6_UNSPEC,
490 QEMU_IFLA_INET6_FLAGS,
491 QEMU_IFLA_INET6_CONF,
492 QEMU_IFLA_INET6_STATS,
493 QEMU_IFLA_INET6_MCAST,
494 QEMU_IFLA_INET6_CACHEINFO,
495 QEMU_IFLA_INET6_ICMP6STATS,
496 QEMU_IFLA_INET6_TOKEN,
497 QEMU_IFLA_INET6_ADDR_GEN_MODE,
498 QEMU___IFLA_INET6_MAX
499 };
500
501 typedef abi_long (*TargetFdDataFunc)(void *, size_t);
502 typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
503 typedef struct TargetFdTrans {
504 TargetFdDataFunc host_to_target_data;
505 TargetFdDataFunc target_to_host_data;
506 TargetFdAddrFunc target_to_host_addr;
507 } TargetFdTrans;
508
509 static TargetFdTrans **target_fd_trans;
510
511 static unsigned int target_fd_max;
512
513 static TargetFdDataFunc fd_trans_target_to_host_data(int fd)
514 {
515 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
516 return target_fd_trans[fd]->target_to_host_data;
517 }
518 return NULL;
519 }
520
521 static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
522 {
523 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
524 return target_fd_trans[fd]->host_to_target_data;
525 }
526 return NULL;
527 }
528
529 static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
530 {
531 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
532 return target_fd_trans[fd]->target_to_host_addr;
533 }
534 return NULL;
535 }
536
537 static void fd_trans_register(int fd, TargetFdTrans *trans)
538 {
539 unsigned int oldmax;
540
541 if (fd >= target_fd_max) {
542 oldmax = target_fd_max;
543 target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
544 target_fd_trans = g_renew(TargetFdTrans *,
545 target_fd_trans, target_fd_max);
546 memset((void *)(target_fd_trans + oldmax), 0,
547 (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
548 }
549 target_fd_trans[fd] = trans;
550 }
551
552 static void fd_trans_unregister(int fd)
553 {
554 if (fd >= 0 && fd < target_fd_max) {
555 target_fd_trans[fd] = NULL;
556 }
557 }
558
559 static void fd_trans_dup(int oldfd, int newfd)
560 {
561 fd_trans_unregister(newfd);
562 if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
563 fd_trans_register(newfd, target_fd_trans[oldfd]);
564 }
565 }
566
567 static int sys_getcwd1(char *buf, size_t size)
568 {
569 if (getcwd(buf, size) == NULL) {
570 /* getcwd() sets errno */
571 return (-1);
572 }
573 return strlen(buf)+1;
574 }
575
576 #ifdef TARGET_NR_utimensat
577 #if defined(__NR_utimensat)
578 #define __NR_sys_utimensat __NR_utimensat
579 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
580 const struct timespec *,tsp,int,flags)
581 #else
582 static int sys_utimensat(int dirfd, const char *pathname,
583 const struct timespec times[2], int flags)
584 {
585 errno = ENOSYS;
586 return -1;
587 }
588 #endif
589 #endif /* TARGET_NR_utimensat */
590
591 #ifdef CONFIG_INOTIFY
592 #include <sys/inotify.h>
593
594 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
595 static int sys_inotify_init(void)
596 {
597 return (inotify_init());
598 }
599 #endif
600 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
601 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
602 {
603 return (inotify_add_watch(fd, pathname, mask));
604 }
605 #endif
606 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
607 static int sys_inotify_rm_watch(int fd, int32_t wd)
608 {
609 return (inotify_rm_watch(fd, wd));
610 }
611 #endif
612 #ifdef CONFIG_INOTIFY1
613 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
614 static int sys_inotify_init1(int flags)
615 {
616 return (inotify_init1(flags));
617 }
618 #endif
619 #endif
620 #else
621 /* Userspace can usually survive runtime without inotify */
622 #undef TARGET_NR_inotify_init
623 #undef TARGET_NR_inotify_init1
624 #undef TARGET_NR_inotify_add_watch
625 #undef TARGET_NR_inotify_rm_watch
626 #endif /* CONFIG_INOTIFY */
627
628 #if defined(TARGET_NR_prlimit64)
629 #ifndef __NR_prlimit64
630 # define __NR_prlimit64 -1
631 #endif
632 #define __NR_sys_prlimit64 __NR_prlimit64
633 /* The glibc rlimit structure may not be that used by the underlying syscall */
634 struct host_rlimit64 {
635 uint64_t rlim_cur;
636 uint64_t rlim_max;
637 };
638 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
639 const struct host_rlimit64 *, new_limit,
640 struct host_rlimit64 *, old_limit)
641 #endif
642
643
644 #if defined(TARGET_NR_timer_create)
645 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
646 static timer_t g_posix_timers[32] = { 0, } ;
647
648 static inline int next_free_host_timer(void)
649 {
650 int k ;
651 /* FIXME: Does finding the next free slot require a lock? */
652 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
653 if (g_posix_timers[k] == 0) {
654 g_posix_timers[k] = (timer_t) 1;
655 return k;
656 }
657 }
658 return -1;
659 }
660 #endif
661
662 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
663 #ifdef TARGET_ARM
664 static inline int regpairs_aligned(void *cpu_env) {
665 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
666 }
667 #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
668 static inline int regpairs_aligned(void *cpu_env) { return 1; }
669 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
670 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
671 * of registers which translates to the same as ARM/MIPS, because we start with
672 * r3 as arg1 */
673 static inline int regpairs_aligned(void *cpu_env) { return 1; }
674 #else
675 static inline int regpairs_aligned(void *cpu_env) { return 0; }
676 #endif
677
678 #define ERRNO_TABLE_SIZE 1200
679
680 /* target_to_host_errno_table[] is initialized from
681 * host_to_target_errno_table[] in syscall_init(). */
682 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
683 };
684
685 /*
686 * This list is the union of errno values overridden in asm-<arch>/errno.h
687 * minus the errnos that are not actually generic to all archs.
688 */
689 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
690 [EAGAIN] = TARGET_EAGAIN,
691 [EIDRM] = TARGET_EIDRM,
692 [ECHRNG] = TARGET_ECHRNG,
693 [EL2NSYNC] = TARGET_EL2NSYNC,
694 [EL3HLT] = TARGET_EL3HLT,
695 [EL3RST] = TARGET_EL3RST,
696 [ELNRNG] = TARGET_ELNRNG,
697 [EUNATCH] = TARGET_EUNATCH,
698 [ENOCSI] = TARGET_ENOCSI,
699 [EL2HLT] = TARGET_EL2HLT,
700 [EDEADLK] = TARGET_EDEADLK,
701 [ENOLCK] = TARGET_ENOLCK,
702 [EBADE] = TARGET_EBADE,
703 [EBADR] = TARGET_EBADR,
704 [EXFULL] = TARGET_EXFULL,
705 [ENOANO] = TARGET_ENOANO,
706 [EBADRQC] = TARGET_EBADRQC,
707 [EBADSLT] = TARGET_EBADSLT,
708 [EBFONT] = TARGET_EBFONT,
709 [ENOSTR] = TARGET_ENOSTR,
710 [ENODATA] = TARGET_ENODATA,
711 [ETIME] = TARGET_ETIME,
712 [ENOSR] = TARGET_ENOSR,
713 [ENONET] = TARGET_ENONET,
714 [ENOPKG] = TARGET_ENOPKG,
715 [EREMOTE] = TARGET_EREMOTE,
716 [ENOLINK] = TARGET_ENOLINK,
717 [EADV] = TARGET_EADV,
718 [ESRMNT] = TARGET_ESRMNT,
719 [ECOMM] = TARGET_ECOMM,
720 [EPROTO] = TARGET_EPROTO,
721 [EDOTDOT] = TARGET_EDOTDOT,
722 [EMULTIHOP] = TARGET_EMULTIHOP,
723 [EBADMSG] = TARGET_EBADMSG,
724 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
725 [EOVERFLOW] = TARGET_EOVERFLOW,
726 [ENOTUNIQ] = TARGET_ENOTUNIQ,
727 [EBADFD] = TARGET_EBADFD,
728 [EREMCHG] = TARGET_EREMCHG,
729 [ELIBACC] = TARGET_ELIBACC,
730 [ELIBBAD] = TARGET_ELIBBAD,
731 [ELIBSCN] = TARGET_ELIBSCN,
732 [ELIBMAX] = TARGET_ELIBMAX,
733 [ELIBEXEC] = TARGET_ELIBEXEC,
734 [EILSEQ] = TARGET_EILSEQ,
735 [ENOSYS] = TARGET_ENOSYS,
736 [ELOOP] = TARGET_ELOOP,
737 [ERESTART] = TARGET_ERESTART,
738 [ESTRPIPE] = TARGET_ESTRPIPE,
739 [ENOTEMPTY] = TARGET_ENOTEMPTY,
740 [EUSERS] = TARGET_EUSERS,
741 [ENOTSOCK] = TARGET_ENOTSOCK,
742 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
743 [EMSGSIZE] = TARGET_EMSGSIZE,
744 [EPROTOTYPE] = TARGET_EPROTOTYPE,
745 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
746 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
747 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
748 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
749 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
750 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
751 [EADDRINUSE] = TARGET_EADDRINUSE,
752 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
753 [ENETDOWN] = TARGET_ENETDOWN,
754 [ENETUNREACH] = TARGET_ENETUNREACH,
755 [ENETRESET] = TARGET_ENETRESET,
756 [ECONNABORTED] = TARGET_ECONNABORTED,
757 [ECONNRESET] = TARGET_ECONNRESET,
758 [ENOBUFS] = TARGET_ENOBUFS,
759 [EISCONN] = TARGET_EISCONN,
760 [ENOTCONN] = TARGET_ENOTCONN,
761 [EUCLEAN] = TARGET_EUCLEAN,
762 [ENOTNAM] = TARGET_ENOTNAM,
763 [ENAVAIL] = TARGET_ENAVAIL,
764 [EISNAM] = TARGET_EISNAM,
765 [EREMOTEIO] = TARGET_EREMOTEIO,
766 [EDQUOT] = TARGET_EDQUOT,
767 [ESHUTDOWN] = TARGET_ESHUTDOWN,
768 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
769 [ETIMEDOUT] = TARGET_ETIMEDOUT,
770 [ECONNREFUSED] = TARGET_ECONNREFUSED,
771 [EHOSTDOWN] = TARGET_EHOSTDOWN,
772 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
773 [EALREADY] = TARGET_EALREADY,
774 [EINPROGRESS] = TARGET_EINPROGRESS,
775 [ESTALE] = TARGET_ESTALE,
776 [ECANCELED] = TARGET_ECANCELED,
777 [ENOMEDIUM] = TARGET_ENOMEDIUM,
778 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
779 #ifdef ENOKEY
780 [ENOKEY] = TARGET_ENOKEY,
781 #endif
782 #ifdef EKEYEXPIRED
783 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
784 #endif
785 #ifdef EKEYREVOKED
786 [EKEYREVOKED] = TARGET_EKEYREVOKED,
787 #endif
788 #ifdef EKEYREJECTED
789 [EKEYREJECTED] = TARGET_EKEYREJECTED,
790 #endif
791 #ifdef EOWNERDEAD
792 [EOWNERDEAD] = TARGET_EOWNERDEAD,
793 #endif
794 #ifdef ENOTRECOVERABLE
795 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
796 #endif
797 #ifdef ENOMSG
798 [ENOMSG] = TARGET_ENOMSG,
799 #endif
800 };
801
802 static inline int host_to_target_errno(int err)
803 {
804 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
805 host_to_target_errno_table[err]) {
806 return host_to_target_errno_table[err];
807 }
808 return err;
809 }
810
811 static inline int target_to_host_errno(int err)
812 {
813 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
814 target_to_host_errno_table[err]) {
815 return target_to_host_errno_table[err];
816 }
817 return err;
818 }
819
820 static inline abi_long get_errno(abi_long ret)
821 {
822 if (ret == -1)
823 return -host_to_target_errno(errno);
824 else
825 return ret;
826 }
827
828 static inline int is_error(abi_long ret)
829 {
830 return (abi_ulong)ret >= (abi_ulong)(-4096);
831 }
832
833 const char *target_strerror(int err)
834 {
835 if (err == TARGET_ERESTARTSYS) {
836 return "To be restarted";
837 }
838 if (err == TARGET_QEMU_ESIGRETURN) {
839 return "Successful exit from sigreturn";
840 }
841
842 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
843 return NULL;
844 }
845 return strerror(target_to_host_errno(err));
846 }
847
848 #define safe_syscall0(type, name) \
849 static type safe_##name(void) \
850 { \
851 return safe_syscall(__NR_##name); \
852 }
853
854 #define safe_syscall1(type, name, type1, arg1) \
855 static type safe_##name(type1 arg1) \
856 { \
857 return safe_syscall(__NR_##name, arg1); \
858 }
859
860 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
861 static type safe_##name(type1 arg1, type2 arg2) \
862 { \
863 return safe_syscall(__NR_##name, arg1, arg2); \
864 }
865
866 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
867 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
868 { \
869 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
870 }
871
872 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
873 type4, arg4) \
874 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
875 { \
876 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
877 }
878
879 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
880 type4, arg4, type5, arg5) \
881 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
882 type5 arg5) \
883 { \
884 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
885 }
886
887 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
888 type4, arg4, type5, arg5, type6, arg6) \
889 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
890 type5 arg5, type6 arg6) \
891 { \
892 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
893 }
894
895 safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
896 safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
897 safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
898 int, flags, mode_t, mode)
899 safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
900 struct rusage *, rusage)
901 safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
902 int, options, struct rusage *, rusage)
903 safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
904 safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
905 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
906 safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
907 struct timespec *, tsp, const sigset_t *, sigmask,
908 size_t, sigsetsize)
909 safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
910 int, maxevents, int, timeout, const sigset_t *, sigmask,
911 size_t, sigsetsize)
912 safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
913 const struct timespec *,timeout,int *,uaddr2,int,val3)
914 safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
915 safe_syscall2(int, kill, pid_t, pid, int, sig)
916 safe_syscall2(int, tkill, int, tid, int, sig)
917 safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
918 safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
919 safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
920 safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
921 socklen_t, addrlen)
922 safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
923 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
924 safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
925 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
926 safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
927 safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
928 safe_syscall2(int, flock, int, fd, int, operation)
929 safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
930 const struct timespec *, uts, size_t, sigsetsize)
931 safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
932 int, flags)
933 safe_syscall2(int, nanosleep, const struct timespec *, req,
934 struct timespec *, rem)
935 #ifdef TARGET_NR_clock_nanosleep
936 safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
937 const struct timespec *, req, struct timespec *, rem)
938 #endif
939 #ifdef __NR_msgsnd
940 safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
941 int, flags)
942 safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
943 long, msgtype, int, flags)
944 safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
945 unsigned, nsops, const struct timespec *, timeout)
946 #else
947 /* This host kernel architecture uses a single ipc syscall; fake up
948 * wrappers for the sub-operations to hide this implementation detail.
949 * Annoyingly we can't include linux/ipc.h to get the constant definitions
950 * for the call parameter because some structs in there conflict with the
951 * sys/ipc.h ones. So we just define them here, and rely on them being
952 * the same for all host architectures.
953 */
954 #define Q_SEMTIMEDOP 4
955 #define Q_MSGSND 11
956 #define Q_MSGRCV 12
957 #define Q_IPCCALL(VERSION, OP) ((VERSION) << 16 | (OP))
958
959 safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
960 void *, ptr, long, fifth)
961 static int safe_msgsnd(int msgid, const void *msgp, size_t sz, int flags)
962 {
963 return safe_ipc(Q_IPCCALL(0, Q_MSGSND), msgid, sz, flags, (void *)msgp, 0);
964 }
965 static int safe_msgrcv(int msgid, void *msgp, size_t sz, long type, int flags)
966 {
967 return safe_ipc(Q_IPCCALL(1, Q_MSGRCV), msgid, sz, flags, msgp, type);
968 }
969 static int safe_semtimedop(int semid, struct sembuf *tsops, unsigned nsops,
970 const struct timespec *timeout)
971 {
972 return safe_ipc(Q_IPCCALL(0, Q_SEMTIMEDOP), semid, nsops, 0, tsops,
973 (long)timeout);
974 }
975 #endif
976 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
977 safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
978 size_t, len, unsigned, prio, const struct timespec *, timeout)
979 safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
980 size_t, len, unsigned *, prio, const struct timespec *, timeout)
981 #endif
982 /* We do ioctl like this rather than via safe_syscall3 to preserve the
983 * "third argument might be integer or pointer or not present" behaviour of
984 * the libc function.
985 */
986 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
987 /* Similarly for fcntl. Note that callers must always:
988 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
989 * use the flock64 struct rather than unsuffixed flock
990 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
991 */
992 #ifdef __NR_fcntl64
993 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
994 #else
995 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
996 #endif
997
998 static inline int host_to_target_sock_type(int host_type)
999 {
1000 int target_type;
1001
1002 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
1003 case SOCK_DGRAM:
1004 target_type = TARGET_SOCK_DGRAM;
1005 break;
1006 case SOCK_STREAM:
1007 target_type = TARGET_SOCK_STREAM;
1008 break;
1009 default:
1010 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
1011 break;
1012 }
1013
1014 #if defined(SOCK_CLOEXEC)
1015 if (host_type & SOCK_CLOEXEC) {
1016 target_type |= TARGET_SOCK_CLOEXEC;
1017 }
1018 #endif
1019
1020 #if defined(SOCK_NONBLOCK)
1021 if (host_type & SOCK_NONBLOCK) {
1022 target_type |= TARGET_SOCK_NONBLOCK;
1023 }
1024 #endif
1025
1026 return target_type;
1027 }
1028
1029 static abi_ulong target_brk;
1030 static abi_ulong target_original_brk;
1031 static abi_ulong brk_page;
1032
1033 void target_set_brk(abi_ulong new_brk)
1034 {
1035 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
1036 brk_page = HOST_PAGE_ALIGN(target_brk);
1037 }
1038
1039 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
1040 #define DEBUGF_BRK(message, args...)
1041
1042 /* do_brk() must return target values and target errnos. */
1043 abi_long do_brk(abi_ulong new_brk)
1044 {
1045 abi_long mapped_addr;
1046 abi_ulong new_alloc_size;
1047
1048 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
1049
1050 if (!new_brk) {
1051 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
1052 return target_brk;
1053 }
1054 if (new_brk < target_original_brk) {
1055 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
1056 target_brk);
1057 return target_brk;
1058 }
1059
1060 /* If the new brk is less than the highest page reserved to the
1061 * target heap allocation, set it and we're almost done... */
1062 if (new_brk <= brk_page) {
1063 /* Heap contents are initialized to zero, as for anonymous
1064 * mapped pages. */
1065 if (new_brk > target_brk) {
1066 memset(g2h(target_brk), 0, new_brk - target_brk);
1067 }
1068 target_brk = new_brk;
1069 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
1070 return target_brk;
1071 }
1072
1073 /* We need to allocate more memory after the brk... Note that
1074 * we don't use MAP_FIXED because that will map over the top of
1075 * any existing mapping (like the one with the host libc or qemu
1076 * itself); instead we treat "mapped but at wrong address" as
1077 * a failure and unmap again.
1078 */
1079 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
1080 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
1081 PROT_READ|PROT_WRITE,
1082 MAP_ANON|MAP_PRIVATE, 0, 0));
1083
1084 if (mapped_addr == brk_page) {
1085 /* Heap contents are initialized to zero, as for anonymous
1086 * mapped pages. Technically the new pages are already
1087 * initialized to zero since they *are* anonymous mapped
1088 * pages, however we have to take care with the contents that
1089 * come from the remaining part of the previous page: it may
1090 * contains garbage data due to a previous heap usage (grown
1091 * then shrunken). */
1092 memset(g2h(target_brk), 0, brk_page - target_brk);
1093
1094 target_brk = new_brk;
1095 brk_page = HOST_PAGE_ALIGN(target_brk);
1096 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
1097 target_brk);
1098 return target_brk;
1099 } else if (mapped_addr != -1) {
1100 /* Mapped but at wrong address, meaning there wasn't actually
1101 * enough space for this brk.
1102 */
1103 target_munmap(mapped_addr, new_alloc_size);
1104 mapped_addr = -1;
1105 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
1106 }
1107 else {
1108 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
1109 }
1110
1111 #if defined(TARGET_ALPHA)
1112 /* We (partially) emulate OSF/1 on Alpha, which requires we
1113 return a proper errno, not an unchanged brk value. */
1114 return -TARGET_ENOMEM;
1115 #endif
1116 /* For everything else, return the previous break. */
1117 return target_brk;
1118 }
1119
1120 static inline abi_long copy_from_user_fdset(fd_set *fds,
1121 abi_ulong target_fds_addr,
1122 int n)
1123 {
1124 int i, nw, j, k;
1125 abi_ulong b, *target_fds;
1126
1127 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
1128 if (!(target_fds = lock_user(VERIFY_READ,
1129 target_fds_addr,
1130 sizeof(abi_ulong) * nw,
1131 1)))
1132 return -TARGET_EFAULT;
1133
1134 FD_ZERO(fds);
1135 k = 0;
1136 for (i = 0; i < nw; i++) {
1137 /* grab the abi_ulong */
1138 __get_user(b, &target_fds[i]);
1139 for (j = 0; j < TARGET_ABI_BITS; j++) {
1140 /* check the bit inside the abi_ulong */
1141 if ((b >> j) & 1)
1142 FD_SET(k, fds);
1143 k++;
1144 }
1145 }
1146
1147 unlock_user(target_fds, target_fds_addr, 0);
1148
1149 return 0;
1150 }
1151
1152 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
1153 abi_ulong target_fds_addr,
1154 int n)
1155 {
1156 if (target_fds_addr) {
1157 if (copy_from_user_fdset(fds, target_fds_addr, n))
1158 return -TARGET_EFAULT;
1159 *fds_ptr = fds;
1160 } else {
1161 *fds_ptr = NULL;
1162 }
1163 return 0;
1164 }
1165
1166 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
1167 const fd_set *fds,
1168 int n)
1169 {
1170 int i, nw, j, k;
1171 abi_long v;
1172 abi_ulong *target_fds;
1173
1174 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
1175 if (!(target_fds = lock_user(VERIFY_WRITE,
1176 target_fds_addr,
1177 sizeof(abi_ulong) * nw,
1178 0)))
1179 return -TARGET_EFAULT;
1180
1181 k = 0;
1182 for (i = 0; i < nw; i++) {
1183 v = 0;
1184 for (j = 0; j < TARGET_ABI_BITS; j++) {
1185 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
1186 k++;
1187 }
1188 __put_user(v, &target_fds[i]);
1189 }
1190
1191 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
1192
1193 return 0;
1194 }
1195
1196 #if defined(__alpha__)
1197 #define HOST_HZ 1024
1198 #else
1199 #define HOST_HZ 100
1200 #endif
1201
1202 static inline abi_long host_to_target_clock_t(long ticks)
1203 {
1204 #if HOST_HZ == TARGET_HZ
1205 return ticks;
1206 #else
1207 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
1208 #endif
1209 }
1210
1211 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
1212 const struct rusage *rusage)
1213 {
1214 struct target_rusage *target_rusage;
1215
1216 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
1217 return -TARGET_EFAULT;
1218 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
1219 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
1220 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
1221 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
1222 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
1223 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
1224 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
1225 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
1226 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
1227 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
1228 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
1229 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
1230 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
1231 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
1232 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
1233 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
1234 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
1235 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
1236 unlock_user_struct(target_rusage, target_addr, 1);
1237
1238 return 0;
1239 }
1240
1241 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
1242 {
1243 abi_ulong target_rlim_swap;
1244 rlim_t result;
1245
1246 target_rlim_swap = tswapal(target_rlim);
1247 if (target_rlim_swap == TARGET_RLIM_INFINITY)
1248 return RLIM_INFINITY;
1249
1250 result = target_rlim_swap;
1251 if (target_rlim_swap != (rlim_t)result)
1252 return RLIM_INFINITY;
1253
1254 return result;
1255 }
1256
1257 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
1258 {
1259 abi_ulong target_rlim_swap;
1260 abi_ulong result;
1261
1262 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
1263 target_rlim_swap = TARGET_RLIM_INFINITY;
1264 else
1265 target_rlim_swap = rlim;
1266 result = tswapal(target_rlim_swap);
1267
1268 return result;
1269 }
1270
1271 static inline int target_to_host_resource(int code)
1272 {
1273 switch (code) {
1274 case TARGET_RLIMIT_AS:
1275 return RLIMIT_AS;
1276 case TARGET_RLIMIT_CORE:
1277 return RLIMIT_CORE;
1278 case TARGET_RLIMIT_CPU:
1279 return RLIMIT_CPU;
1280 case TARGET_RLIMIT_DATA:
1281 return RLIMIT_DATA;
1282 case TARGET_RLIMIT_FSIZE:
1283 return RLIMIT_FSIZE;
1284 case TARGET_RLIMIT_LOCKS:
1285 return RLIMIT_LOCKS;
1286 case TARGET_RLIMIT_MEMLOCK:
1287 return RLIMIT_MEMLOCK;
1288 case TARGET_RLIMIT_MSGQUEUE:
1289 return RLIMIT_MSGQUEUE;
1290 case TARGET_RLIMIT_NICE:
1291 return RLIMIT_NICE;
1292 case TARGET_RLIMIT_NOFILE:
1293 return RLIMIT_NOFILE;
1294 case TARGET_RLIMIT_NPROC:
1295 return RLIMIT_NPROC;
1296 case TARGET_RLIMIT_RSS:
1297 return RLIMIT_RSS;
1298 case TARGET_RLIMIT_RTPRIO:
1299 return RLIMIT_RTPRIO;
1300 case TARGET_RLIMIT_SIGPENDING:
1301 return RLIMIT_SIGPENDING;
1302 case TARGET_RLIMIT_STACK:
1303 return RLIMIT_STACK;
1304 default:
1305 return code;
1306 }
1307 }
1308
1309 static inline abi_long copy_from_user_timeval(struct timeval *tv,
1310 abi_ulong target_tv_addr)
1311 {
1312 struct target_timeval *target_tv;
1313
1314 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
1315 return -TARGET_EFAULT;
1316
1317 __get_user(tv->tv_sec, &target_tv->tv_sec);
1318 __get_user(tv->tv_usec, &target_tv->tv_usec);
1319
1320 unlock_user_struct(target_tv, target_tv_addr, 0);
1321
1322 return 0;
1323 }
1324
1325 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1326 const struct timeval *tv)
1327 {
1328 struct target_timeval *target_tv;
1329
1330 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
1331 return -TARGET_EFAULT;
1332
1333 __put_user(tv->tv_sec, &target_tv->tv_sec);
1334 __put_user(tv->tv_usec, &target_tv->tv_usec);
1335
1336 unlock_user_struct(target_tv, target_tv_addr, 1);
1337
1338 return 0;
1339 }
1340
1341 static inline abi_long copy_from_user_timezone(struct timezone *tz,
1342 abi_ulong target_tz_addr)
1343 {
1344 struct target_timezone *target_tz;
1345
1346 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1347 return -TARGET_EFAULT;
1348 }
1349
1350 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1351 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1352
1353 unlock_user_struct(target_tz, target_tz_addr, 0);
1354
1355 return 0;
1356 }
1357
1358 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1359 #include <mqueue.h>
1360
1361 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1362 abi_ulong target_mq_attr_addr)
1363 {
1364 struct target_mq_attr *target_mq_attr;
1365
1366 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1367 target_mq_attr_addr, 1))
1368 return -TARGET_EFAULT;
1369
1370 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1371 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1372 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1373 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1374
1375 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1376
1377 return 0;
1378 }
1379
1380 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1381 const struct mq_attr *attr)
1382 {
1383 struct target_mq_attr *target_mq_attr;
1384
1385 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1386 target_mq_attr_addr, 0))
1387 return -TARGET_EFAULT;
1388
1389 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1390 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1391 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1392 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1393
1394 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1395
1396 return 0;
1397 }
1398 #endif
1399
1400 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1401 /* do_select() must return target values and target errnos. */
1402 static abi_long do_select(int n,
1403 abi_ulong rfd_addr, abi_ulong wfd_addr,
1404 abi_ulong efd_addr, abi_ulong target_tv_addr)
1405 {
1406 fd_set rfds, wfds, efds;
1407 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1408 struct timeval tv;
1409 struct timespec ts, *ts_ptr;
1410 abi_long ret;
1411
1412 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1413 if (ret) {
1414 return ret;
1415 }
1416 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1417 if (ret) {
1418 return ret;
1419 }
1420 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1421 if (ret) {
1422 return ret;
1423 }
1424
1425 if (target_tv_addr) {
1426 if (copy_from_user_timeval(&tv, target_tv_addr))
1427 return -TARGET_EFAULT;
1428 ts.tv_sec = tv.tv_sec;
1429 ts.tv_nsec = tv.tv_usec * 1000;
1430 ts_ptr = &ts;
1431 } else {
1432 ts_ptr = NULL;
1433 }
1434
1435 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1436 ts_ptr, NULL));
1437
1438 if (!is_error(ret)) {
1439 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1440 return -TARGET_EFAULT;
1441 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1442 return -TARGET_EFAULT;
1443 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1444 return -TARGET_EFAULT;
1445
1446 if (target_tv_addr) {
1447 tv.tv_sec = ts.tv_sec;
1448 tv.tv_usec = ts.tv_nsec / 1000;
1449 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1450 return -TARGET_EFAULT;
1451 }
1452 }
1453 }
1454
1455 return ret;
1456 }
1457
1458 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1459 static abi_long do_old_select(abi_ulong arg1)
1460 {
1461 struct target_sel_arg_struct *sel;
1462 abi_ulong inp, outp, exp, tvp;
1463 long nsel;
1464
1465 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
1466 return -TARGET_EFAULT;
1467 }
1468
1469 nsel = tswapal(sel->n);
1470 inp = tswapal(sel->inp);
1471 outp = tswapal(sel->outp);
1472 exp = tswapal(sel->exp);
1473 tvp = tswapal(sel->tvp);
1474
1475 unlock_user_struct(sel, arg1, 0);
1476
1477 return do_select(nsel, inp, outp, exp, tvp);
1478 }
1479 #endif
1480 #endif
1481
1482 static abi_long do_pipe2(int host_pipe[], int flags)
1483 {
1484 #ifdef CONFIG_PIPE2
1485 return pipe2(host_pipe, flags);
1486 #else
1487 return -ENOSYS;
1488 #endif
1489 }
1490
1491 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1492 int flags, int is_pipe2)
1493 {
1494 int host_pipe[2];
1495 abi_long ret;
1496 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1497
1498 if (is_error(ret))
1499 return get_errno(ret);
1500
1501 /* Several targets have special calling conventions for the original
1502 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1503 if (!is_pipe2) {
1504 #if defined(TARGET_ALPHA)
1505 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1506 return host_pipe[0];
1507 #elif defined(TARGET_MIPS)
1508 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1509 return host_pipe[0];
1510 #elif defined(TARGET_SH4)
1511 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1512 return host_pipe[0];
1513 #elif defined(TARGET_SPARC)
1514 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1515 return host_pipe[0];
1516 #endif
1517 }
1518
1519 if (put_user_s32(host_pipe[0], pipedes)
1520 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1521 return -TARGET_EFAULT;
1522 return get_errno(ret);
1523 }
1524
1525 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1526 abi_ulong target_addr,
1527 socklen_t len)
1528 {
1529 struct target_ip_mreqn *target_smreqn;
1530
1531 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1532 if (!target_smreqn)
1533 return -TARGET_EFAULT;
1534 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1535 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1536 if (len == sizeof(struct target_ip_mreqn))
1537 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
1538 unlock_user(target_smreqn, target_addr, 0);
1539
1540 return 0;
1541 }
1542
1543 static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
1544 abi_ulong target_addr,
1545 socklen_t len)
1546 {
1547 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1548 sa_family_t sa_family;
1549 struct target_sockaddr *target_saddr;
1550
1551 if (fd_trans_target_to_host_addr(fd)) {
1552 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1553 }
1554
1555 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1556 if (!target_saddr)
1557 return -TARGET_EFAULT;
1558
1559 sa_family = tswap16(target_saddr->sa_family);
1560
1561 /* Oops. The caller might send a incomplete sun_path; sun_path
1562 * must be terminated by \0 (see the manual page), but
1563 * unfortunately it is quite common to specify sockaddr_un
1564 * length as "strlen(x->sun_path)" while it should be
1565 * "strlen(...) + 1". We'll fix that here if needed.
1566 * Linux kernel has a similar feature.
1567 */
1568
1569 if (sa_family == AF_UNIX) {
1570 if (len < unix_maxlen && len > 0) {
1571 char *cp = (char*)target_saddr;
1572
1573 if ( cp[len-1] && !cp[len] )
1574 len++;
1575 }
1576 if (len > unix_maxlen)
1577 len = unix_maxlen;
1578 }
1579
1580 memcpy(addr, target_saddr, len);
1581 addr->sa_family = sa_family;
1582 if (sa_family == AF_NETLINK) {
1583 struct sockaddr_nl *nladdr;
1584
1585 nladdr = (struct sockaddr_nl *)addr;
1586 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1587 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1588 } else if (sa_family == AF_PACKET) {
1589 struct target_sockaddr_ll *lladdr;
1590
1591 lladdr = (struct target_sockaddr_ll *)addr;
1592 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1593 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1594 }
1595 unlock_user(target_saddr, target_addr, 0);
1596
1597 return 0;
1598 }
1599
1600 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1601 struct sockaddr *addr,
1602 socklen_t len)
1603 {
1604 struct target_sockaddr *target_saddr;
1605
1606 if (len == 0) {
1607 return 0;
1608 }
1609
1610 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1611 if (!target_saddr)
1612 return -TARGET_EFAULT;
1613 memcpy(target_saddr, addr, len);
1614 if (len >= offsetof(struct target_sockaddr, sa_family) +
1615 sizeof(target_saddr->sa_family)) {
1616 target_saddr->sa_family = tswap16(addr->sa_family);
1617 }
1618 if (addr->sa_family == AF_NETLINK && len >= sizeof(struct sockaddr_nl)) {
1619 struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr;
1620 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1621 target_nl->nl_groups = tswap32(target_nl->nl_groups);
1622 } else if (addr->sa_family == AF_PACKET) {
1623 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
1624 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
1625 target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
1626 }
1627 unlock_user(target_saddr, target_addr, len);
1628
1629 return 0;
1630 }
1631
1632 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1633 struct target_msghdr *target_msgh)
1634 {
1635 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1636 abi_long msg_controllen;
1637 abi_ulong target_cmsg_addr;
1638 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1639 socklen_t space = 0;
1640
1641 msg_controllen = tswapal(target_msgh->msg_controllen);
1642 if (msg_controllen < sizeof (struct target_cmsghdr))
1643 goto the_end;
1644 target_cmsg_addr = tswapal(target_msgh->msg_control);
1645 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1646 target_cmsg_start = target_cmsg;
1647 if (!target_cmsg)
1648 return -TARGET_EFAULT;
1649
1650 while (cmsg && target_cmsg) {
1651 void *data = CMSG_DATA(cmsg);
1652 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1653
1654 int len = tswapal(target_cmsg->cmsg_len)
1655 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1656
1657 space += CMSG_SPACE(len);
1658 if (space > msgh->msg_controllen) {
1659 space -= CMSG_SPACE(len);
1660 /* This is a QEMU bug, since we allocated the payload
1661 * area ourselves (unlike overflow in host-to-target
1662 * conversion, which is just the guest giving us a buffer
1663 * that's too small). It can't happen for the payload types
1664 * we currently support; if it becomes an issue in future
1665 * we would need to improve our allocation strategy to
1666 * something more intelligent than "twice the size of the
1667 * target buffer we're reading from".
1668 */
1669 gemu_log("Host cmsg overflow\n");
1670 break;
1671 }
1672
1673 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1674 cmsg->cmsg_level = SOL_SOCKET;
1675 } else {
1676 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1677 }
1678 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1679 cmsg->cmsg_len = CMSG_LEN(len);
1680
1681 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
1682 int *fd = (int *)data;
1683 int *target_fd = (int *)target_data;
1684 int i, numfds = len / sizeof(int);
1685
1686 for (i = 0; i < numfds; i++) {
1687 __get_user(fd[i], target_fd + i);
1688 }
1689 } else if (cmsg->cmsg_level == SOL_SOCKET
1690 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1691 struct ucred *cred = (struct ucred *)data;
1692 struct target_ucred *target_cred =
1693 (struct target_ucred *)target_data;
1694
1695 __get_user(cred->pid, &target_cred->pid);
1696 __get_user(cred->uid, &target_cred->uid);
1697 __get_user(cred->gid, &target_cred->gid);
1698 } else {
1699 gemu_log("Unsupported ancillary data: %d/%d\n",
1700 cmsg->cmsg_level, cmsg->cmsg_type);
1701 memcpy(data, target_data, len);
1702 }
1703
1704 cmsg = CMSG_NXTHDR(msgh, cmsg);
1705 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1706 target_cmsg_start);
1707 }
1708 unlock_user(target_cmsg, target_cmsg_addr, 0);
1709 the_end:
1710 msgh->msg_controllen = space;
1711 return 0;
1712 }
1713
1714 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1715 struct msghdr *msgh)
1716 {
1717 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1718 abi_long msg_controllen;
1719 abi_ulong target_cmsg_addr;
1720 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1721 socklen_t space = 0;
1722
1723 msg_controllen = tswapal(target_msgh->msg_controllen);
1724 if (msg_controllen < sizeof (struct target_cmsghdr))
1725 goto the_end;
1726 target_cmsg_addr = tswapal(target_msgh->msg_control);
1727 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1728 target_cmsg_start = target_cmsg;
1729 if (!target_cmsg)
1730 return -TARGET_EFAULT;
1731
1732 while (cmsg && target_cmsg) {
1733 void *data = CMSG_DATA(cmsg);
1734 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1735
1736 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1737 int tgt_len, tgt_space;
1738
1739 /* We never copy a half-header but may copy half-data;
1740 * this is Linux's behaviour in put_cmsg(). Note that
1741 * truncation here is a guest problem (which we report
1742 * to the guest via the CTRUNC bit), unlike truncation
1743 * in target_to_host_cmsg, which is a QEMU bug.
1744 */
1745 if (msg_controllen < sizeof(struct cmsghdr)) {
1746 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1747 break;
1748 }
1749
1750 if (cmsg->cmsg_level == SOL_SOCKET) {
1751 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1752 } else {
1753 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1754 }
1755 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1756
1757 tgt_len = TARGET_CMSG_LEN(len);
1758
1759 /* Payload types which need a different size of payload on
1760 * the target must adjust tgt_len here.
1761 */
1762 switch (cmsg->cmsg_level) {
1763 case SOL_SOCKET:
1764 switch (cmsg->cmsg_type) {
1765 case SO_TIMESTAMP:
1766 tgt_len = sizeof(struct target_timeval);
1767 break;
1768 default:
1769 break;
1770 }
1771 default:
1772 break;
1773 }
1774
1775 if (msg_controllen < tgt_len) {
1776 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1777 tgt_len = msg_controllen;
1778 }
1779
1780 /* We must now copy-and-convert len bytes of payload
1781 * into tgt_len bytes of destination space. Bear in mind
1782 * that in both source and destination we may be dealing
1783 * with a truncated value!
1784 */
1785 switch (cmsg->cmsg_level) {
1786 case SOL_SOCKET:
1787 switch (cmsg->cmsg_type) {
1788 case SCM_RIGHTS:
1789 {
1790 int *fd = (int *)data;
1791 int *target_fd = (int *)target_data;
1792 int i, numfds = tgt_len / sizeof(int);
1793
1794 for (i = 0; i < numfds; i++) {
1795 __put_user(fd[i], target_fd + i);
1796 }
1797 break;
1798 }
1799 case SO_TIMESTAMP:
1800 {
1801 struct timeval *tv = (struct timeval *)data;
1802 struct target_timeval *target_tv =
1803 (struct target_timeval *)target_data;
1804
1805 if (len != sizeof(struct timeval) ||
1806 tgt_len != sizeof(struct target_timeval)) {
1807 goto unimplemented;
1808 }
1809
1810 /* copy struct timeval to target */
1811 __put_user(tv->tv_sec, &target_tv->tv_sec);
1812 __put_user(tv->tv_usec, &target_tv->tv_usec);
1813 break;
1814 }
1815 case SCM_CREDENTIALS:
1816 {
1817 struct ucred *cred = (struct ucred *)data;
1818 struct target_ucred *target_cred =
1819 (struct target_ucred *)target_data;
1820
1821 __put_user(cred->pid, &target_cred->pid);
1822 __put_user(cred->uid, &target_cred->uid);
1823 __put_user(cred->gid, &target_cred->gid);
1824 break;
1825 }
1826 default:
1827 goto unimplemented;
1828 }
1829 break;
1830
1831 default:
1832 unimplemented:
1833 gemu_log("Unsupported ancillary data: %d/%d\n",
1834 cmsg->cmsg_level, cmsg->cmsg_type);
1835 memcpy(target_data, data, MIN(len, tgt_len));
1836 if (tgt_len > len) {
1837 memset(target_data + len, 0, tgt_len - len);
1838 }
1839 }
1840
1841 target_cmsg->cmsg_len = tswapal(tgt_len);
1842 tgt_space = TARGET_CMSG_SPACE(len);
1843 if (msg_controllen < tgt_space) {
1844 tgt_space = msg_controllen;
1845 }
1846 msg_controllen -= tgt_space;
1847 space += tgt_space;
1848 cmsg = CMSG_NXTHDR(msgh, cmsg);
1849 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1850 target_cmsg_start);
1851 }
1852 unlock_user(target_cmsg, target_cmsg_addr, space);
1853 the_end:
1854 target_msgh->msg_controllen = tswapal(space);
1855 return 0;
1856 }
1857
1858 static void tswap_nlmsghdr(struct nlmsghdr *nlh)
1859 {
1860 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
1861 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
1862 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
1863 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
1864 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
1865 }
1866
1867 static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
1868 size_t len,
1869 abi_long (*host_to_target_nlmsg)
1870 (struct nlmsghdr *))
1871 {
1872 uint32_t nlmsg_len;
1873 abi_long ret;
1874
1875 while (len > sizeof(struct nlmsghdr)) {
1876
1877 nlmsg_len = nlh->nlmsg_len;
1878 if (nlmsg_len < sizeof(struct nlmsghdr) ||
1879 nlmsg_len > len) {
1880 break;
1881 }
1882
1883 switch (nlh->nlmsg_type) {
1884 case NLMSG_DONE:
1885 tswap_nlmsghdr(nlh);
1886 return 0;
1887 case NLMSG_NOOP:
1888 break;
1889 case NLMSG_ERROR:
1890 {
1891 struct nlmsgerr *e = NLMSG_DATA(nlh);
1892 e->error = tswap32(e->error);
1893 tswap_nlmsghdr(&e->msg);
1894 tswap_nlmsghdr(nlh);
1895 return 0;
1896 }
1897 default:
1898 ret = host_to_target_nlmsg(nlh);
1899 if (ret < 0) {
1900 tswap_nlmsghdr(nlh);
1901 return ret;
1902 }
1903 break;
1904 }
1905 tswap_nlmsghdr(nlh);
1906 len -= NLMSG_ALIGN(nlmsg_len);
1907 nlh = (struct nlmsghdr *)(((char*)nlh) + NLMSG_ALIGN(nlmsg_len));
1908 }
1909 return 0;
1910 }
1911
1912 static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
1913 size_t len,
1914 abi_long (*target_to_host_nlmsg)
1915 (struct nlmsghdr *))
1916 {
1917 int ret;
1918
1919 while (len > sizeof(struct nlmsghdr)) {
1920 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
1921 tswap32(nlh->nlmsg_len) > len) {
1922 break;
1923 }
1924 tswap_nlmsghdr(nlh);
1925 switch (nlh->nlmsg_type) {
1926 case NLMSG_DONE:
1927 return 0;
1928 case NLMSG_NOOP:
1929 break;
1930 case NLMSG_ERROR:
1931 {
1932 struct nlmsgerr *e = NLMSG_DATA(nlh);
1933 e->error = tswap32(e->error);
1934 tswap_nlmsghdr(&e->msg);
1935 return 0;
1936 }
1937 default:
1938 ret = target_to_host_nlmsg(nlh);
1939 if (ret < 0) {
1940 return ret;
1941 }
1942 }
1943 len -= NLMSG_ALIGN(nlh->nlmsg_len);
1944 nlh = (struct nlmsghdr *)(((char *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1945 }
1946 return 0;
1947 }
1948
1949 #ifdef CONFIG_RTNETLINK
1950 static abi_long host_to_target_for_each_nlattr(struct nlattr *nlattr,
1951 size_t len, void *context,
1952 abi_long (*host_to_target_nlattr)
1953 (struct nlattr *,
1954 void *context))
1955 {
1956 unsigned short nla_len;
1957 abi_long ret;
1958
1959 while (len > sizeof(struct nlattr)) {
1960 nla_len = nlattr->nla_len;
1961 if (nla_len < sizeof(struct nlattr) ||
1962 nla_len > len) {
1963 break;
1964 }
1965 ret = host_to_target_nlattr(nlattr, context);
1966 nlattr->nla_len = tswap16(nlattr->nla_len);
1967 nlattr->nla_type = tswap16(nlattr->nla_type);
1968 if (ret < 0) {
1969 return ret;
1970 }
1971 len -= NLA_ALIGN(nla_len);
1972 nlattr = (struct nlattr *)(((char *)nlattr) + NLA_ALIGN(nla_len));
1973 }
1974 return 0;
1975 }
1976
1977 static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
1978 size_t len,
1979 abi_long (*host_to_target_rtattr)
1980 (struct rtattr *))
1981 {
1982 unsigned short rta_len;
1983 abi_long ret;
1984
1985 while (len > sizeof(struct rtattr)) {
1986 rta_len = rtattr->rta_len;
1987 if (rta_len < sizeof(struct rtattr) ||
1988 rta_len > len) {
1989 break;
1990 }
1991 ret = host_to_target_rtattr(rtattr);
1992 rtattr->rta_len = tswap16(rtattr->rta_len);
1993 rtattr->rta_type = tswap16(rtattr->rta_type);
1994 if (ret < 0) {
1995 return ret;
1996 }
1997 len -= RTA_ALIGN(rta_len);
1998 rtattr = (struct rtattr *)(((char *)rtattr) + RTA_ALIGN(rta_len));
1999 }
2000 return 0;
2001 }
2002
2003 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
2004
2005 static abi_long host_to_target_data_bridge_nlattr(struct nlattr *nlattr,
2006 void *context)
2007 {
2008 uint16_t *u16;
2009 uint32_t *u32;
2010 uint64_t *u64;
2011
2012 switch (nlattr->nla_type) {
2013 /* no data */
2014 case QEMU_IFLA_BR_FDB_FLUSH:
2015 break;
2016 /* binary */
2017 case QEMU_IFLA_BR_GROUP_ADDR:
2018 break;
2019 /* uint8_t */
2020 case QEMU_IFLA_BR_VLAN_FILTERING:
2021 case QEMU_IFLA_BR_TOPOLOGY_CHANGE:
2022 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED:
2023 case QEMU_IFLA_BR_MCAST_ROUTER:
2024 case QEMU_IFLA_BR_MCAST_SNOOPING:
2025 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR:
2026 case QEMU_IFLA_BR_MCAST_QUERIER:
2027 case QEMU_IFLA_BR_NF_CALL_IPTABLES:
2028 case QEMU_IFLA_BR_NF_CALL_IP6TABLES:
2029 case QEMU_IFLA_BR_NF_CALL_ARPTABLES:
2030 break;
2031 /* uint16_t */
2032 case QEMU_IFLA_BR_PRIORITY:
2033 case QEMU_IFLA_BR_VLAN_PROTOCOL:
2034 case QEMU_IFLA_BR_GROUP_FWD_MASK:
2035 case QEMU_IFLA_BR_ROOT_PORT:
2036 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID:
2037 u16 = NLA_DATA(nlattr);
2038 *u16 = tswap16(*u16);
2039 break;
2040 /* uint32_t */
2041 case QEMU_IFLA_BR_FORWARD_DELAY:
2042 case QEMU_IFLA_BR_HELLO_TIME:
2043 case QEMU_IFLA_BR_MAX_AGE:
2044 case QEMU_IFLA_BR_AGEING_TIME:
2045 case QEMU_IFLA_BR_STP_STATE:
2046 case QEMU_IFLA_BR_ROOT_PATH_COST:
2047 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY:
2048 case QEMU_IFLA_BR_MCAST_HASH_MAX:
2049 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT:
2050 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT:
2051 u32 = NLA_DATA(nlattr);
2052 *u32 = tswap32(*u32);
2053 break;
2054 /* uint64_t */
2055 case QEMU_IFLA_BR_HELLO_TIMER:
2056 case QEMU_IFLA_BR_TCN_TIMER:
2057 case QEMU_IFLA_BR_GC_TIMER:
2058 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER:
2059 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL:
2060 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL:
2061 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL:
2062 case QEMU_IFLA_BR_MCAST_QUERY_INTVL:
2063 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL:
2064 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL:
2065 u64 = NLA_DATA(nlattr);
2066 *u64 = tswap64(*u64);
2067 break;
2068 /* ifla_bridge_id: uin8_t[] */
2069 case QEMU_IFLA_BR_ROOT_ID:
2070 case QEMU_IFLA_BR_BRIDGE_ID:
2071 break;
2072 default:
2073 gemu_log("Unknown QEMU_IFLA_BR type %d\n", nlattr->nla_type);
2074 break;
2075 }
2076 return 0;
2077 }
2078
2079 static abi_long host_to_target_slave_data_bridge_nlattr(struct nlattr *nlattr,
2080 void *context)
2081 {
2082 uint16_t *u16;
2083 uint32_t *u32;
2084 uint64_t *u64;
2085
2086 switch (nlattr->nla_type) {
2087 /* uint8_t */
2088 case QEMU_IFLA_BRPORT_STATE:
2089 case QEMU_IFLA_BRPORT_MODE:
2090 case QEMU_IFLA_BRPORT_GUARD:
2091 case QEMU_IFLA_BRPORT_PROTECT:
2092 case QEMU_IFLA_BRPORT_FAST_LEAVE:
2093 case QEMU_IFLA_BRPORT_LEARNING:
2094 case QEMU_IFLA_BRPORT_UNICAST_FLOOD:
2095 case QEMU_IFLA_BRPORT_PROXYARP:
2096 case QEMU_IFLA_BRPORT_LEARNING_SYNC:
2097 case QEMU_IFLA_BRPORT_PROXYARP_WIFI:
2098 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK:
2099 case QEMU_IFLA_BRPORT_CONFIG_PENDING:
2100 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER:
2101 break;
2102 /* uint16_t */
2103 case QEMU_IFLA_BRPORT_PRIORITY:
2104 case QEMU_IFLA_BRPORT_DESIGNATED_PORT:
2105 case QEMU_IFLA_BRPORT_DESIGNATED_COST:
2106 case QEMU_IFLA_BRPORT_ID:
2107 case QEMU_IFLA_BRPORT_NO:
2108 u16 = NLA_DATA(nlattr);
2109 *u16 = tswap16(*u16);
2110 break;
2111 /* uin32_t */
2112 case QEMU_IFLA_BRPORT_COST:
2113 u32 = NLA_DATA(nlattr);
2114 *u32 = tswap32(*u32);
2115 break;
2116 /* uint64_t */
2117 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER:
2118 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER:
2119 case QEMU_IFLA_BRPORT_HOLD_TIMER:
2120 u64 = NLA_DATA(nlattr);
2121 *u64 = tswap64(*u64);
2122 break;
2123 /* ifla_bridge_id: uint8_t[] */
2124 case QEMU_IFLA_BRPORT_ROOT_ID:
2125 case QEMU_IFLA_BRPORT_BRIDGE_ID:
2126 break;
2127 default:
2128 gemu_log("Unknown QEMU_IFLA_BRPORT type %d\n", nlattr->nla_type);
2129 break;
2130 }
2131 return 0;
2132 }
2133
2134 struct linkinfo_context {
2135 int len;
2136 char *name;
2137 int slave_len;
2138 char *slave_name;
2139 };
2140
2141 static abi_long host_to_target_data_linkinfo_nlattr(struct nlattr *nlattr,
2142 void *context)
2143 {
2144 struct linkinfo_context *li_context = context;
2145
2146 switch (nlattr->nla_type) {
2147 /* string */
2148 case QEMU_IFLA_INFO_KIND:
2149 li_context->name = NLA_DATA(nlattr);
2150 li_context->len = nlattr->nla_len - NLA_HDRLEN;
2151 break;
2152 case QEMU_IFLA_INFO_SLAVE_KIND:
2153 li_context->slave_name = NLA_DATA(nlattr);
2154 li_context->slave_len = nlattr->nla_len - NLA_HDRLEN;
2155 break;
2156 /* stats */
2157 case QEMU_IFLA_INFO_XSTATS:
2158 /* FIXME: only used by CAN */
2159 break;
2160 /* nested */
2161 case QEMU_IFLA_INFO_DATA:
2162 if (strncmp(li_context->name, "bridge",
2163 li_context->len) == 0) {
2164 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
2165 nlattr->nla_len,
2166 NULL,
2167 host_to_target_data_bridge_nlattr);
2168 } else {
2169 gemu_log("Unknown QEMU_IFLA_INFO_KIND %s\n", li_context->name);
2170 }
2171 break;
2172 case QEMU_IFLA_INFO_SLAVE_DATA:
2173 if (strncmp(li_context->slave_name, "bridge",
2174 li_context->slave_len) == 0) {
2175 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
2176 nlattr->nla_len,
2177 NULL,
2178 host_to_target_slave_data_bridge_nlattr);
2179 } else {
2180 gemu_log("Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
2181 li_context->slave_name);
2182 }
2183 break;
2184 default:
2185 gemu_log("Unknown host QEMU_IFLA_INFO type: %d\n", nlattr->nla_type);
2186 break;
2187 }
2188
2189 return 0;
2190 }
2191
2192 static abi_long host_to_target_data_inet_nlattr(struct nlattr *nlattr,
2193 void *context)
2194 {
2195 uint32_t *u32;
2196 int i;
2197
2198 switch (nlattr->nla_type) {
2199 case QEMU_IFLA_INET_CONF:
2200 u32 = NLA_DATA(nlattr);
2201 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
2202 i++) {
2203 u32[i] = tswap32(u32[i]);
2204 }
2205 break;
2206 default:
2207 gemu_log("Unknown host AF_INET type: %d\n", nlattr->nla_type);
2208 }
2209 return 0;
2210 }
2211
2212 static abi_long host_to_target_data_inet6_nlattr(struct nlattr *nlattr,
2213 void *context)
2214 {
2215 uint32_t *u32;
2216 uint64_t *u64;
2217 struct ifla_cacheinfo *ci;
2218 int i;
2219
2220 switch (nlattr->nla_type) {
2221 /* binaries */
2222 case QEMU_IFLA_INET6_TOKEN:
2223 break;
2224 /* uint8_t */
2225 case QEMU_IFLA_INET6_ADDR_GEN_MODE:
2226 break;
2227 /* uint32_t */
2228 case QEMU_IFLA_INET6_FLAGS:
2229 u32 = NLA_DATA(nlattr);
2230 *u32 = tswap32(*u32);
2231 break;
2232 /* uint32_t[] */
2233 case QEMU_IFLA_INET6_CONF:
2234 u32 = NLA_DATA(nlattr);
2235 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
2236 i++) {
2237 u32[i] = tswap32(u32[i]);
2238 }
2239 break;
2240 /* ifla_cacheinfo */
2241 case QEMU_IFLA_INET6_CACHEINFO:
2242 ci = NLA_DATA(nlattr);
2243 ci->max_reasm_len = tswap32(ci->max_reasm_len);
2244 ci->tstamp = tswap32(ci->tstamp);
2245 ci->reachable_time = tswap32(ci->reachable_time);
2246 ci->retrans_time = tswap32(ci->retrans_time);
2247 break;
2248 /* uint64_t[] */
2249 case QEMU_IFLA_INET6_STATS:
2250 case QEMU_IFLA_INET6_ICMP6STATS:
2251 u64 = NLA_DATA(nlattr);
2252 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u64);
2253 i++) {
2254 u64[i] = tswap64(u64[i]);
2255 }
2256 break;
2257 default:
2258 gemu_log("Unknown host AF_INET6 type: %d\n", nlattr->nla_type);
2259 }
2260 return 0;
2261 }
2262
2263 static abi_long host_to_target_data_spec_nlattr(struct nlattr *nlattr,
2264 void *context)
2265 {
2266 switch (nlattr->nla_type) {
2267 case AF_INET:
2268 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
2269 NULL,
2270 host_to_target_data_inet_nlattr);
2271 case AF_INET6:
2272 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
2273 NULL,
2274 host_to_target_data_inet6_nlattr);
2275 default:
2276 gemu_log("Unknown host AF_SPEC type: %d\n", nlattr->nla_type);
2277 break;
2278 }
2279 return 0;
2280 }
2281
2282 static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
2283 {
2284 uint32_t *u32;
2285 struct rtnl_link_stats *st;
2286 struct rtnl_link_stats64 *st64;
2287 struct rtnl_link_ifmap *map;
2288 struct linkinfo_context li_context;
2289
2290 switch (rtattr->rta_type) {
2291 /* binary stream */
2292 case QEMU_IFLA_ADDRESS:
2293 case QEMU_IFLA_BROADCAST:
2294 /* string */
2295 case QEMU_IFLA_IFNAME:
2296 case QEMU_IFLA_QDISC:
2297 break;
2298 /* uin8_t */
2299 case QEMU_IFLA_OPERSTATE:
2300 case QEMU_IFLA_LINKMODE:
2301 case QEMU_IFLA_CARRIER:
2302 case QEMU_IFLA_PROTO_DOWN:
2303 break;
2304 /* uint32_t */
2305 case QEMU_IFLA_MTU:
2306 case QEMU_IFLA_LINK:
2307 case QEMU_IFLA_WEIGHT:
2308 case QEMU_IFLA_TXQLEN:
2309 case QEMU_IFLA_CARRIER_CHANGES:
2310 case QEMU_IFLA_NUM_RX_QUEUES:
2311 case QEMU_IFLA_NUM_TX_QUEUES:
2312 case QEMU_IFLA_PROMISCUITY:
2313 case QEMU_IFLA_EXT_MASK:
2314 case QEMU_IFLA_LINK_NETNSID:
2315 case QEMU_IFLA_GROUP:
2316 case QEMU_IFLA_MASTER:
2317 case QEMU_IFLA_NUM_VF:
2318 u32 = RTA_DATA(rtattr);
2319 *u32 = tswap32(*u32);
2320 break;
2321 /* struct rtnl_link_stats */
2322 case QEMU_IFLA_STATS:
2323 st = RTA_DATA(rtattr);
2324 st->rx_packets = tswap32(st->rx_packets);
2325 st->tx_packets = tswap32(st->tx_packets);
2326 st->rx_bytes = tswap32(st->rx_bytes);
2327 st->tx_bytes = tswap32(st->tx_bytes);
2328 st->rx_errors = tswap32(st->rx_errors);
2329 st->tx_errors = tswap32(st->tx_errors);
2330 st->rx_dropped = tswap32(st->rx_dropped);
2331 st->tx_dropped = tswap32(st->tx_dropped);
2332 st->multicast = tswap32(st->multicast);
2333 st->collisions = tswap32(st->collisions);
2334
2335 /* detailed rx_errors: */
2336 st->rx_length_errors = tswap32(st->rx_length_errors);
2337 st->rx_over_errors = tswap32(st->rx_over_errors);
2338 st->rx_crc_errors = tswap32(st->rx_crc_errors);
2339 st->rx_frame_errors = tswap32(st->rx_frame_errors);
2340 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
2341 st->rx_missed_errors = tswap32(st->rx_missed_errors);
2342
2343 /* detailed tx_errors */
2344 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
2345 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
2346 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
2347 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
2348 st->tx_window_errors = tswap32(st->tx_window_errors);
2349
2350 /* for cslip etc */
2351 st->rx_compressed = tswap32(st->rx_compressed);
2352 st->tx_compressed = tswap32(st->tx_compressed);
2353 break;
2354 /* struct rtnl_link_stats64 */
2355 case QEMU_IFLA_STATS64:
2356 st64 = RTA_DATA(rtattr);
2357 st64->rx_packets = tswap64(st64->rx_packets);
2358 st64->tx_packets = tswap64(st64->tx_packets);
2359 st64->rx_bytes = tswap64(st64->rx_bytes);
2360 st64->tx_bytes = tswap64(st64->tx_bytes);
2361 st64->rx_errors = tswap64(st64->rx_errors);
2362 st64->tx_errors = tswap64(st64->tx_errors);
2363 st64->rx_dropped = tswap64(st64->rx_dropped);
2364 st64->tx_dropped = tswap64(st64->tx_dropped);
2365 st64->multicast = tswap64(st64->multicast);
2366 st64->collisions = tswap64(st64->collisions);
2367
2368 /* detailed rx_errors: */
2369 st64->rx_length_errors = tswap64(st64->rx_length_errors);
2370 st64->rx_over_errors = tswap64(st64->rx_over_errors);
2371 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
2372 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
2373 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
2374 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
2375
2376 /* detailed tx_errors */
2377 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
2378 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
2379 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
2380 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
2381 st64->tx_window_errors = tswap64(st64->tx_window_errors);
2382
2383 /* for cslip etc */
2384 st64->rx_compressed = tswap64(st64->rx_compressed);
2385 st64->tx_compressed = tswap64(st64->tx_compressed);
2386 break;
2387 /* struct rtnl_link_ifmap */
2388 case QEMU_IFLA_MAP:
2389 map = RTA_DATA(rtattr);
2390 map->mem_start = tswap64(map->mem_start);
2391 map->mem_end = tswap64(map->mem_end);
2392 map->base_addr = tswap64(map->base_addr);
2393 map->irq = tswap16(map->irq);
2394 break;
2395 /* nested */
2396 case QEMU_IFLA_LINKINFO:
2397 memset(&li_context, 0, sizeof(li_context));
2398 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
2399 &li_context,
2400 host_to_target_data_linkinfo_nlattr);
2401 case QEMU_IFLA_AF_SPEC:
2402 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
2403 NULL,
2404 host_to_target_data_spec_nlattr);
2405 default:
2406 gemu_log("Unknown host QEMU_IFLA type: %d\n", rtattr->rta_type);
2407 break;
2408 }
2409 return 0;
2410 }
2411
2412 static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
2413 {
2414 uint32_t *u32;
2415 struct ifa_cacheinfo *ci;
2416
2417 switch (rtattr->rta_type) {
2418 /* binary: depends on family type */
2419 case IFA_ADDRESS:
2420 case IFA_LOCAL:
2421 break;
2422 /* string */
2423 case IFA_LABEL:
2424 break;
2425 /* u32 */
2426 case IFA_FLAGS:
2427 case IFA_BROADCAST:
2428 u32 = RTA_DATA(rtattr);
2429 *u32 = tswap32(*u32);
2430 break;
2431 /* struct ifa_cacheinfo */
2432 case IFA_CACHEINFO:
2433 ci = RTA_DATA(rtattr);
2434 ci->ifa_prefered = tswap32(ci->ifa_prefered);
2435 ci->ifa_valid = tswap32(ci->ifa_valid);
2436 ci->cstamp = tswap32(ci->cstamp);
2437 ci->tstamp = tswap32(ci->tstamp);
2438 break;
2439 default:
2440 gemu_log("Unknown host IFA type: %d\n", rtattr->rta_type);
2441 break;
2442 }
2443 return 0;
2444 }
2445
2446 static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
2447 {
2448 uint32_t *u32;
2449 switch (rtattr->rta_type) {
2450 /* binary: depends on family type */
2451 case RTA_GATEWAY:
2452 case RTA_DST:
2453 case RTA_PREFSRC:
2454 break;
2455 /* u32 */
2456 case RTA_PRIORITY:
2457 case RTA_TABLE:
2458 case RTA_OIF:
2459 u32 = RTA_DATA(rtattr);
2460 *u32 = tswap32(*u32);
2461 break;
2462 default:
2463 gemu_log("Unknown host RTA type: %d\n", rtattr->rta_type);
2464 break;
2465 }
2466 return 0;
2467 }
2468
2469 static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
2470 uint32_t rtattr_len)
2471 {
2472 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
2473 host_to_target_data_link_rtattr);
2474 }
2475
2476 static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
2477 uint32_t rtattr_len)
2478 {
2479 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
2480 host_to_target_data_addr_rtattr);
2481 }
2482
2483 static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
2484 uint32_t rtattr_len)
2485 {
2486 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
2487 host_to_target_data_route_rtattr);
2488 }
2489
2490 static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
2491 {
2492 uint32_t nlmsg_len;
2493 struct ifinfomsg *ifi;
2494 struct ifaddrmsg *ifa;
2495 struct rtmsg *rtm;
2496
2497 nlmsg_len = nlh->nlmsg_len;
2498 switch (nlh->nlmsg_type) {
2499 case RTM_NEWLINK:
2500 case RTM_DELLINK:
2501 case RTM_GETLINK:
2502 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
2503 ifi = NLMSG_DATA(nlh);
2504 ifi->ifi_type = tswap16(ifi->ifi_type);
2505 ifi->ifi_index = tswap32(ifi->ifi_index);
2506 ifi->ifi_flags = tswap32(ifi->ifi_flags);
2507 ifi->ifi_change = tswap32(ifi->ifi_change);
2508 host_to_target_link_rtattr(IFLA_RTA(ifi),
2509 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
2510 }
2511 break;
2512 case RTM_NEWADDR:
2513 case RTM_DELADDR:
2514 case RTM_GETADDR:
2515 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
2516 ifa = NLMSG_DATA(nlh);
2517 ifa->ifa_index = tswap32(ifa->ifa_index);
2518 host_to_target_addr_rtattr(IFA_RTA(ifa),
2519 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
2520 }
2521 break;
2522 case RTM_NEWROUTE:
2523 case RTM_DELROUTE:
2524 case RTM_GETROUTE:
2525 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
2526 rtm = NLMSG_DATA(nlh);
2527 rtm->rtm_flags = tswap32(rtm->rtm_flags);
2528 host_to_target_route_rtattr(RTM_RTA(rtm),
2529 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
2530 }
2531 break;
2532 default:
2533 return -TARGET_EINVAL;
2534 }
2535 return 0;
2536 }
2537
2538 static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
2539 size_t len)
2540 {
2541 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
2542 }
2543
2544 static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
2545 size_t len,
2546 abi_long (*target_to_host_rtattr)
2547 (struct rtattr *))
2548 {
2549 abi_long ret;
2550
2551 while (len >= sizeof(struct rtattr)) {
2552 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
2553 tswap16(rtattr->rta_len) > len) {
2554 break;
2555 }
2556 rtattr->rta_len = tswap16(rtattr->rta_len);
2557 rtattr->rta_type = tswap16(rtattr->rta_type);
2558 ret = target_to_host_rtattr(rtattr);
2559 if (ret < 0) {
2560 return ret;
2561 }
2562 len -= RTA_ALIGN(rtattr->rta_len);
2563 rtattr = (struct rtattr *)(((char *)rtattr) +
2564 RTA_ALIGN(rtattr->rta_len));
2565 }
2566 return 0;
2567 }
2568
2569 static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
2570 {
2571 switch (rtattr->rta_type) {
2572 default:
2573 gemu_log("Unknown target QEMU_IFLA type: %d\n", rtattr->rta_type);
2574 break;
2575 }
2576 return 0;
2577 }
2578
2579 static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
2580 {
2581 switch (rtattr->rta_type) {
2582 /* binary: depends on family type */
2583 case IFA_LOCAL:
2584 case IFA_ADDRESS:
2585 break;
2586 default:
2587 gemu_log("Unknown target IFA type: %d\n", rtattr->rta_type);
2588 break;
2589 }
2590 return 0;
2591 }
2592
2593 static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
2594 {
2595 uint32_t *u32;
2596 switch (rtattr->rta_type) {
2597 /* binary: depends on family type */
2598 case RTA_DST:
2599 case RTA_SRC:
2600 case RTA_GATEWAY:
2601 break;
2602 /* u32 */
2603 case RTA_PRIORITY:
2604 case RTA_OIF:
2605 u32 = RTA_DATA(rtattr);
2606 *u32 = tswap32(*u32);
2607 break;
2608 default:
2609 gemu_log("Unknown target RTA type: %d\n", rtattr->rta_type);
2610 break;
2611 }
2612 return 0;
2613 }
2614
2615 static void target_to_host_link_rtattr(struct rtattr *rtattr,
2616 uint32_t rtattr_len)
2617 {
2618 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2619 target_to_host_data_link_rtattr);
2620 }
2621
2622 static void target_to_host_addr_rtattr(struct rtattr *rtattr,
2623 uint32_t rtattr_len)
2624 {
2625 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2626 target_to_host_data_addr_rtattr);
2627 }
2628
2629 static void target_to_host_route_rtattr(struct rtattr *rtattr,
2630 uint32_t rtattr_len)
2631 {
2632 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2633 target_to_host_data_route_rtattr);
2634 }
2635
2636 static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
2637 {
2638 struct ifinfomsg *ifi;
2639 struct ifaddrmsg *ifa;
2640 struct rtmsg *rtm;
2641
2642 switch (nlh->nlmsg_type) {
2643 case RTM_GETLINK:
2644 break;
2645 case RTM_NEWLINK:
2646 case RTM_DELLINK:
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 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
2654 NLMSG_LENGTH(sizeof(*ifi)));
2655 }
2656 break;
2657 case RTM_GETADDR:
2658 case RTM_NEWADDR:
2659 case RTM_DELADDR:
2660 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
2661 ifa = NLMSG_DATA(nlh);
2662 ifa->ifa_index = tswap32(ifa->ifa_index);
2663 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
2664 NLMSG_LENGTH(sizeof(*ifa)));
2665 }
2666 break;
2667 case RTM_GETROUTE:
2668 break;
2669 case RTM_NEWROUTE:
2670 case RTM_DELROUTE:
2671 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
2672 rtm = NLMSG_DATA(nlh);
2673 rtm->rtm_flags = tswap32(rtm->rtm_flags);
2674 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
2675 NLMSG_LENGTH(sizeof(*rtm)));
2676 }
2677 break;
2678 default:
2679 return -TARGET_EOPNOTSUPP;
2680 }
2681 return 0;
2682 }
2683
2684 static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
2685 {
2686 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
2687 }
2688 #endif /* CONFIG_RTNETLINK */
2689
2690 static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
2691 {
2692 switch (nlh->nlmsg_type) {
2693 default:
2694 gemu_log("Unknown host audit message type %d\n",
2695 nlh->nlmsg_type);
2696 return -TARGET_EINVAL;
2697 }
2698 return 0;
2699 }
2700
2701 static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
2702 size_t len)
2703 {
2704 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
2705 }
2706
2707 static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
2708 {
2709 switch (nlh->nlmsg_type) {
2710 case AUDIT_USER:
2711 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
2712 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
2713 break;
2714 default:
2715 gemu_log("Unknown target audit message type %d\n",
2716 nlh->nlmsg_type);
2717 return -TARGET_EINVAL;
2718 }
2719
2720 return 0;
2721 }
2722
2723 static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
2724 {
2725 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
2726 }
2727
2728 /* do_setsockopt() Must return target values and target errnos. */
2729 static abi_long do_setsockopt(int sockfd, int level, int optname,
2730 abi_ulong optval_addr, socklen_t optlen)
2731 {
2732 abi_long ret;
2733 int val;
2734 struct ip_mreqn *ip_mreq;
2735 struct ip_mreq_source *ip_mreq_source;
2736
2737 switch(level) {
2738 case SOL_TCP:
2739 /* TCP options all take an 'int' value. */
2740 if (optlen < sizeof(uint32_t))
2741 return -TARGET_EINVAL;
2742
2743 if (get_user_u32(val, optval_addr))
2744 return -TARGET_EFAULT;
2745 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2746 break;
2747 case SOL_IP:
2748 switch(optname) {
2749 case IP_TOS:
2750 case IP_TTL:
2751 case IP_HDRINCL:
2752 case IP_ROUTER_ALERT:
2753 case IP_RECVOPTS:
2754 case IP_RETOPTS:
2755 case IP_PKTINFO:
2756 case IP_MTU_DISCOVER:
2757 case IP_RECVERR:
2758 case IP_RECVTOS:
2759 #ifdef IP_FREEBIND
2760 case IP_FREEBIND:
2761 #endif
2762 case IP_MULTICAST_TTL:
2763 case IP_MULTICAST_LOOP:
2764 val = 0;
2765 if (optlen >= sizeof(uint32_t)) {
2766 if (get_user_u32(val, optval_addr))
2767 return -TARGET_EFAULT;
2768 } else if (optlen >= 1) {
2769 if (get_user_u8(val, optval_addr))
2770 return -TARGET_EFAULT;
2771 }
2772 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2773 break;
2774 case IP_ADD_MEMBERSHIP:
2775 case IP_DROP_MEMBERSHIP:
2776 if (optlen < sizeof (struct target_ip_mreq) ||
2777 optlen > sizeof (struct target_ip_mreqn))
2778 return -TARGET_EINVAL;
2779
2780 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2781 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2782 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2783 break;
2784
2785 case IP_BLOCK_SOURCE:
2786 case IP_UNBLOCK_SOURCE:
2787 case IP_ADD_SOURCE_MEMBERSHIP:
2788 case IP_DROP_SOURCE_MEMBERSHIP:
2789 if (optlen != sizeof (struct target_ip_mreq_source))
2790 return -TARGET_EINVAL;
2791
2792 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2793 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2794 unlock_user (ip_mreq_source, optval_addr, 0);
2795 break;
2796
2797 default:
2798 goto unimplemented;
2799 }
2800 break;
2801 case SOL_IPV6:
2802 switch (optname) {
2803 case IPV6_MTU_DISCOVER:
2804 case IPV6_MTU:
2805 case IPV6_V6ONLY:
2806 case IPV6_RECVPKTINFO:
2807 val = 0;
2808 if (optlen < sizeof(uint32_t)) {
2809 return -TARGET_EINVAL;
2810 }
2811 if (get_user_u32(val, optval_addr)) {
2812 return -TARGET_EFAULT;
2813 }
2814 ret = get_errno(setsockopt(sockfd, level, optname,
2815 &val, sizeof(val)));
2816 break;
2817 default:
2818 goto unimplemented;
2819 }
2820 break;
2821 case SOL_RAW:
2822 switch (optname) {
2823 case ICMP_FILTER:
2824 /* struct icmp_filter takes an u32 value */
2825 if (optlen < sizeof(uint32_t)) {
2826 return -TARGET_EINVAL;
2827 }
2828
2829 if (get_user_u32(val, optval_addr)) {
2830 return -TARGET_EFAULT;
2831 }
2832 ret = get_errno(setsockopt(sockfd, level, optname,
2833 &val, sizeof(val)));
2834 break;
2835
2836 default:
2837 goto unimplemented;
2838 }
2839 break;
2840 case TARGET_SOL_SOCKET:
2841 switch (optname) {
2842 case TARGET_SO_RCVTIMEO:
2843 {
2844 struct timeval tv;
2845
2846 optname = SO_RCVTIMEO;
2847
2848 set_timeout:
2849 if (optlen != sizeof(struct target_timeval)) {
2850 return -TARGET_EINVAL;
2851 }
2852
2853 if (copy_from_user_timeval(&tv, optval_addr)) {
2854 return -TARGET_EFAULT;
2855 }
2856
2857 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2858 &tv, sizeof(tv)));
2859 return ret;
2860 }
2861 case TARGET_SO_SNDTIMEO:
2862 optname = SO_SNDTIMEO;
2863 goto set_timeout;
2864 case TARGET_SO_ATTACH_FILTER:
2865 {
2866 struct target_sock_fprog *tfprog;
2867 struct target_sock_filter *tfilter;
2868 struct sock_fprog fprog;
2869 struct sock_filter *filter;
2870 int i;
2871
2872 if (optlen != sizeof(*tfprog)) {
2873 return -TARGET_EINVAL;
2874 }
2875 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2876 return -TARGET_EFAULT;
2877 }
2878 if (!lock_user_struct(VERIFY_READ, tfilter,
2879 tswapal(tfprog->filter), 0)) {
2880 unlock_user_struct(tfprog, optval_addr, 1);
2881 return -TARGET_EFAULT;
2882 }
2883
2884 fprog.len = tswap16(tfprog->len);
2885 filter = g_try_new(struct sock_filter, fprog.len);
2886 if (filter == NULL) {
2887 unlock_user_struct(tfilter, tfprog->filter, 1);
2888 unlock_user_struct(tfprog, optval_addr, 1);
2889 return -TARGET_ENOMEM;
2890 }
2891 for (i = 0; i < fprog.len; i++) {
2892 filter[i].code = tswap16(tfilter[i].code);
2893 filter[i].jt = tfilter[i].jt;
2894 filter[i].jf = tfilter[i].jf;
2895 filter[i].k = tswap32(tfilter[i].k);
2896 }
2897 fprog.filter = filter;
2898
2899 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2900 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
2901 g_free(filter);
2902
2903 unlock_user_struct(tfilter, tfprog->filter, 1);
2904 unlock_user_struct(tfprog, optval_addr, 1);
2905 return ret;
2906 }
2907 case TARGET_SO_BINDTODEVICE:
2908 {
2909 char *dev_ifname, *addr_ifname;
2910
2911 if (optlen > IFNAMSIZ - 1) {
2912 optlen = IFNAMSIZ - 1;
2913 }
2914 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2915 if (!dev_ifname) {
2916 return -TARGET_EFAULT;
2917 }
2918 optname = SO_BINDTODEVICE;
2919 addr_ifname = alloca(IFNAMSIZ);
2920 memcpy(addr_ifname, dev_ifname, optlen);
2921 addr_ifname[optlen] = 0;
2922 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2923 addr_ifname, optlen));
2924 unlock_user (dev_ifname, optval_addr, 0);
2925 return ret;
2926 }
2927 /* Options with 'int' argument. */
2928 case TARGET_SO_DEBUG:
2929 optname = SO_DEBUG;
2930 break;
2931 case TARGET_SO_REUSEADDR:
2932 optname = SO_REUSEADDR;
2933 break;
2934 case TARGET_SO_TYPE:
2935 optname = SO_TYPE;
2936 break;
2937 case TARGET_SO_ERROR:
2938 optname = SO_ERROR;
2939 break;
2940 case TARGET_SO_DONTROUTE:
2941 optname = SO_DONTROUTE;
2942 break;
2943 case TARGET_SO_BROADCAST:
2944 optname = SO_BROADCAST;
2945 break;
2946 case TARGET_SO_SNDBUF:
2947 optname = SO_SNDBUF;
2948 break;
2949 case TARGET_SO_SNDBUFFORCE:
2950 optname = SO_SNDBUFFORCE;
2951 break;
2952 case TARGET_SO_RCVBUF:
2953 optname = SO_RCVBUF;
2954 break;
2955 case TARGET_SO_RCVBUFFORCE:
2956 optname = SO_RCVBUFFORCE;
2957 break;
2958 case TARGET_SO_KEEPALIVE:
2959 optname = SO_KEEPALIVE;
2960 break;
2961 case TARGET_SO_OOBINLINE:
2962 optname = SO_OOBINLINE;
2963 break;
2964 case TARGET_SO_NO_CHECK:
2965 optname = SO_NO_CHECK;
2966 break;
2967 case TARGET_SO_PRIORITY:
2968 optname = SO_PRIORITY;
2969 break;
2970 #ifdef SO_BSDCOMPAT
2971 case TARGET_SO_BSDCOMPAT:
2972 optname = SO_BSDCOMPAT;
2973 break;
2974 #endif
2975 case TARGET_SO_PASSCRED:
2976 optname = SO_PASSCRED;
2977 break;
2978 case TARGET_SO_PASSSEC:
2979 optname = SO_PASSSEC;
2980 break;
2981 case TARGET_SO_TIMESTAMP:
2982 optname = SO_TIMESTAMP;
2983 break;
2984 case TARGET_SO_RCVLOWAT:
2985 optname = SO_RCVLOWAT;
2986 break;
2987 break;
2988 default:
2989 goto unimplemented;
2990 }
2991 if (optlen < sizeof(uint32_t))
2992 return -TARGET_EINVAL;
2993
2994 if (get_user_u32(val, optval_addr))
2995 return -TARGET_EFAULT;
2996 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
2997 break;
2998 default:
2999 unimplemented:
3000 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
3001 ret = -TARGET_ENOPROTOOPT;
3002 }
3003 return ret;
3004 }
3005
3006 /* do_getsockopt() Must return target values and target errnos. */
3007 static abi_long do_getsockopt(int sockfd, int level, int optname,
3008 abi_ulong optval_addr, abi_ulong optlen)
3009 {
3010 abi_long ret;
3011 int len, val;
3012 socklen_t lv;
3013
3014 switch(level) {
3015 case TARGET_SOL_SOCKET:
3016 level = SOL_SOCKET;
3017 switch (optname) {
3018 /* These don't just return a single integer */
3019 case TARGET_SO_LINGER:
3020 case TARGET_SO_RCVTIMEO:
3021 case TARGET_SO_SNDTIMEO:
3022 case TARGET_SO_PEERNAME:
3023 goto unimplemented;
3024 case TARGET_SO_PEERCRED: {
3025 struct ucred cr;
3026 socklen_t crlen;
3027 struct target_ucred *tcr;
3028
3029 if (get_user_u32(len, optlen)) {
3030 return -TARGET_EFAULT;
3031 }
3032 if (len < 0) {
3033 return -TARGET_EINVAL;
3034 }
3035
3036 crlen = sizeof(cr);
3037 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
3038 &cr, &crlen));
3039 if (ret < 0) {
3040 return ret;
3041 }
3042 if (len > crlen) {
3043 len = crlen;
3044 }
3045 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
3046 return -TARGET_EFAULT;
3047 }
3048 __put_user(cr.pid, &tcr->pid);
3049 __put_user(cr.uid, &tcr->uid);
3050 __put_user(cr.gid, &tcr->gid);
3051 unlock_user_struct(tcr, optval_addr, 1);
3052 if (put_user_u32(len, optlen)) {
3053 return -TARGET_EFAULT;
3054 }
3055 break;
3056 }
3057 /* Options with 'int' argument. */
3058 case TARGET_SO_DEBUG:
3059 optname = SO_DEBUG;
3060 goto int_case;
3061 case TARGET_SO_REUSEADDR:
3062 optname = SO_REUSEADDR;
3063 goto int_case;
3064 case TARGET_SO_TYPE:
3065 optname = SO_TYPE;
3066 goto int_case;
3067 case TARGET_SO_ERROR:
3068 optname = SO_ERROR;
3069 goto int_case;
3070 case TARGET_SO_DONTROUTE:
3071 optname = SO_DONTROUTE;
3072 goto int_case;
3073 case TARGET_SO_BROADCAST:
3074 optname = SO_BROADCAST;
3075 goto int_case;
3076 case TARGET_SO_SNDBUF:
3077 optname = SO_SNDBUF;
3078 goto int_case;
3079 case TARGET_SO_RCVBUF:
3080 optname = SO_RCVBUF;
3081 goto int_case;
3082 case TARGET_SO_KEEPALIVE:
3083 optname = SO_KEEPALIVE;
3084 goto int_case;
3085 case TARGET_SO_OOBINLINE:
3086 optname = SO_OOBINLINE;
3087 goto int_case;
3088 case TARGET_SO_NO_CHECK:
3089 optname = SO_NO_CHECK;
3090 goto int_case;
3091 case TARGET_SO_PRIORITY:
3092 optname = SO_PRIORITY;
3093 goto int_case;
3094 #ifdef SO_BSDCOMPAT
3095 case TARGET_SO_BSDCOMPAT:
3096 optname = SO_BSDCOMPAT;
3097 goto int_case;
3098 #endif
3099 case TARGET_SO_PASSCRED:
3100 optname = SO_PASSCRED;
3101 goto int_case;
3102 case TARGET_SO_TIMESTAMP:
3103 optname = SO_TIMESTAMP;
3104 goto int_case;
3105 case TARGET_SO_RCVLOWAT:
3106 optname = SO_RCVLOWAT;
3107 goto int_case;
3108 case TARGET_SO_ACCEPTCONN:
3109 optname = SO_ACCEPTCONN;
3110 goto int_case;
3111 default:
3112 goto int_case;
3113 }
3114 break;
3115 case SOL_TCP:
3116 /* TCP options all take an 'int' value. */
3117 int_case:
3118 if (get_user_u32(len, optlen))
3119 return -TARGET_EFAULT;
3120 if (len < 0)
3121 return -TARGET_EINVAL;
3122 lv = sizeof(lv);
3123 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
3124 if (ret < 0)
3125 return ret;
3126 if (optname == SO_TYPE) {
3127 val = host_to_target_sock_type(val);
3128 }
3129 if (len > lv)
3130 len = lv;
3131 if (len == 4) {
3132 if (put_user_u32(val, optval_addr))
3133 return -TARGET_EFAULT;
3134 } else {
3135 if (put_user_u8(val, optval_addr))
3136 return -TARGET_EFAULT;
3137 }
3138 if (put_user_u32(len, optlen))
3139 return -TARGET_EFAULT;
3140 break;
3141 case SOL_IP:
3142 switch(optname) {
3143 case IP_TOS:
3144 case IP_TTL:
3145 case IP_HDRINCL:
3146 case IP_ROUTER_ALERT:
3147 case IP_RECVOPTS:
3148 case IP_RETOPTS:
3149 case IP_PKTINFO:
3150 case IP_MTU_DISCOVER:
3151 case IP_RECVERR:
3152 case IP_RECVTOS:
3153 #ifdef IP_FREEBIND
3154 case IP_FREEBIND:
3155 #endif
3156 case IP_MULTICAST_TTL:
3157 case IP_MULTICAST_LOOP:
3158 if (get_user_u32(len, optlen))
3159 return -TARGET_EFAULT;
3160 if (len < 0)
3161 return -TARGET_EINVAL;
3162 lv = sizeof(lv);
3163 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
3164 if (ret < 0)
3165 return ret;
3166 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
3167 len = 1;
3168 if (put_user_u32(len, optlen)
3169 || put_user_u8(val, optval_addr))
3170 return -TARGET_EFAULT;
3171 } else {
3172 if (len > sizeof(int))
3173 len = sizeof(int);
3174 if (put_user_u32(len, optlen)
3175 || put_user_u32(val, optval_addr))
3176 return -TARGET_EFAULT;
3177 }
3178 break;
3179 default:
3180 ret = -TARGET_ENOPROTOOPT;
3181 break;
3182 }
3183 break;
3184 default:
3185 unimplemented:
3186 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
3187 level, optname);
3188 ret = -TARGET_EOPNOTSUPP;
3189 break;
3190 }
3191 return ret;
3192 }
3193
3194 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
3195 abi_ulong count, int copy)
3196 {
3197 struct target_iovec *target_vec;
3198 struct iovec *vec;
3199 abi_ulong total_len, max_len;
3200 int i;
3201 int err = 0;
3202 bool bad_address = false;
3203
3204 if (count == 0) {
3205 errno = 0;
3206 return NULL;
3207 }
3208 if (count > IOV_MAX) {
3209 errno = EINVAL;
3210 return NULL;
3211 }
3212
3213 vec = g_try_new0(struct iovec, count);
3214 if (vec == NULL) {
3215 errno = ENOMEM;
3216 return NULL;
3217 }
3218
3219 target_vec = lock_user(VERIFY_READ, target_addr,
3220 count * sizeof(struct target_iovec), 1);
3221 if (target_vec == NULL) {
3222 err = EFAULT;
3223 goto fail2;
3224 }
3225
3226 /* ??? If host page size > target page size, this will result in a
3227 value larger than what we can actually support. */
3228 max_len = 0x7fffffff & TARGET_PAGE_MASK;
3229 total_len = 0;
3230
3231 for (i = 0; i < count; i++) {
3232 abi_ulong base = tswapal(target_vec[i].iov_base);
3233 abi_long len = tswapal(target_vec[i].iov_len);
3234
3235 if (len < 0) {
3236 err = EINVAL;
3237 goto fail;
3238 } else if (len == 0) {
3239 /* Zero length pointer is ignored. */
3240 vec[i].iov_base = 0;
3241 } else {
3242 vec[i].iov_base = lock_user(type, base, len, copy);
3243 /* If the first buffer pointer is bad, this is a fault. But
3244 * subsequent bad buffers will result in a partial write; this
3245 * is realized by filling the vector with null pointers and
3246 * zero lengths. */
3247 if (!vec[i].iov_base) {
3248 if (i == 0) {
3249 err = EFAULT;
3250 goto fail;
3251 } else {
3252 bad_address = true;
3253 }
3254 }
3255 if (bad_address) {
3256 len = 0;
3257 }
3258 if (len > max_len - total_len) {
3259 len = max_len - total_len;
3260 }
3261 }
3262 vec[i].iov_len = len;
3263 total_len += len;
3264 }
3265
3266 unlock_user(target_vec, target_addr, 0);
3267 return vec;
3268
3269 fail:
3270 while (--i >= 0) {
3271 if (tswapal(target_vec[i].iov_len) > 0) {
3272 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
3273 }
3274 }
3275 unlock_user(target_vec, target_addr, 0);
3276 fail2:
3277 g_free(vec);
3278 errno = err;
3279 return NULL;
3280 }
3281
3282 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
3283 abi_ulong count, int copy)
3284 {
3285 struct target_iovec *target_vec;
3286 int i;
3287
3288 target_vec = lock_user(VERIFY_READ, target_addr,
3289 count * sizeof(struct target_iovec), 1);
3290 if (target_vec) {
3291 for (i = 0; i < count; i++) {
3292 abi_ulong base = tswapal(target_vec[i].iov_base);
3293 abi_long len = tswapal(target_vec[i].iov_len);
3294 if (len < 0) {
3295 break;
3296 }
3297 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
3298 }
3299 unlock_user(target_vec, target_addr, 0);
3300 }
3301
3302 g_free(vec);
3303 }
3304
3305 static inline int target_to_host_sock_type(int *type)
3306 {
3307 int host_type = 0;
3308 int target_type = *type;
3309
3310 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3311 case TARGET_SOCK_DGRAM:
3312 host_type = SOCK_DGRAM;
3313 break;
3314 case TARGET_SOCK_STREAM:
3315 host_type = SOCK_STREAM;
3316 break;
3317 default:
3318 host_type = target_type & TARGET_SOCK_TYPE_MASK;
3319 break;
3320 }
3321 if (target_type & TARGET_SOCK_CLOEXEC) {
3322 #if defined(SOCK_CLOEXEC)
3323 host_type |= SOCK_CLOEXEC;
3324 #else
3325 return -TARGET_EINVAL;
3326 #endif
3327 }
3328 if (target_type & TARGET_SOCK_NONBLOCK) {
3329 #if defined(SOCK_NONBLOCK)
3330 host_type |= SOCK_NONBLOCK;
3331 #elif !defined(O_NONBLOCK)
3332 return -TARGET_EINVAL;
3333 #endif
3334 }
3335 *type = host_type;
3336 return 0;
3337 }
3338
3339 /* Try to emulate socket type flags after socket creation. */
3340 static int sock_flags_fixup(int fd, int target_type)
3341 {
3342 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3343 if (target_type & TARGET_SOCK_NONBLOCK) {
3344 int flags = fcntl(fd, F_GETFL);
3345 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
3346 close(fd);
3347 return -TARGET_EINVAL;
3348 }
3349 }
3350 #endif
3351 return fd;
3352 }
3353
3354 static abi_long packet_target_to_host_sockaddr(void *host_addr,
3355 abi_ulong target_addr,
3356 socklen_t len)
3357 {
3358 struct sockaddr *addr = host_addr;
3359 struct target_sockaddr *target_saddr;
3360
3361 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
3362 if (!target_saddr) {
3363 return -TARGET_EFAULT;
3364 }
3365
3366 memcpy(addr, target_saddr, len);
3367 addr->sa_family = tswap16(target_saddr->sa_family);
3368 /* spkt_protocol is big-endian */
3369
3370 unlock_user(target_saddr, target_addr, 0);
3371 return 0;
3372 }
3373
3374 static TargetFdTrans target_packet_trans = {
3375 .target_to_host_addr = packet_target_to_host_sockaddr,
3376 };
3377
3378 #ifdef CONFIG_RTNETLINK
3379 static abi_long netlink_route_target_to_host(void *buf, size_t len)
3380 {
3381 abi_long ret;
3382
3383 ret = target_to_host_nlmsg_route(buf, len);
3384 if (ret < 0) {
3385 return ret;
3386 }
3387
3388 return len;
3389 }
3390
3391 static abi_long netlink_route_host_to_target(void *buf, size_t len)
3392 {
3393 abi_long ret;
3394
3395 ret = host_to_target_nlmsg_route(buf, len);
3396 if (ret < 0) {
3397 return ret;
3398 }
3399
3400 return len;
3401 }
3402
3403 static TargetFdTrans target_netlink_route_trans = {
3404 .target_to_host_data = netlink_route_target_to_host,
3405 .host_to_target_data = netlink_route_host_to_target,
3406 };
3407 #endif /* CONFIG_RTNETLINK */
3408
3409 static abi_long netlink_audit_target_to_host(void *buf, size_t len)
3410 {
3411 abi_long ret;
3412
3413 ret = target_to_host_nlmsg_audit(buf, len);
3414 if (ret < 0) {
3415 return ret;
3416 }
3417
3418 return len;
3419 }
3420
3421 static abi_long netlink_audit_host_to_target(void *buf, size_t len)
3422 {
3423 abi_long ret;
3424
3425 ret = host_to_target_nlmsg_audit(buf, len);
3426 if (ret < 0) {
3427 return ret;
3428 }
3429
3430 return len;
3431 }
3432
3433 static TargetFdTrans target_netlink_audit_trans = {
3434 .target_to_host_data = netlink_audit_target_to_host,
3435 .host_to_target_data = netlink_audit_host_to_target,
3436 };
3437
3438 /* do_socket() Must return target values and target errnos. */
3439 static abi_long do_socket(int domain, int type, int protocol)
3440 {
3441 int target_type = type;
3442 int ret;
3443
3444 ret = target_to_host_sock_type(&type);
3445 if (ret) {
3446 return ret;
3447 }
3448
3449 if (domain == PF_NETLINK && !(
3450 #ifdef CONFIG_RTNETLINK
3451 protocol == NETLINK_ROUTE ||
3452 #endif
3453 protocol == NETLINK_KOBJECT_UEVENT ||
3454 protocol == NETLINK_AUDIT)) {
3455 return -EPFNOSUPPORT;
3456 }
3457
3458 if (domain == AF_PACKET ||
3459 (domain == AF_INET && type == SOCK_PACKET)) {
3460 protocol = tswap16(protocol);
3461 }
3462
3463 ret = get_errno(socket(domain, type, protocol));
3464 if (ret >= 0) {
3465 ret = sock_flags_fixup(ret, target_type);
3466 if (type == SOCK_PACKET) {
3467 /* Manage an obsolete case :
3468 * if socket type is SOCK_PACKET, bind by name
3469 */
3470 fd_trans_register(ret, &target_packet_trans);
3471 } else if (domain == PF_NETLINK) {
3472 switch (protocol) {
3473 #ifdef CONFIG_RTNETLINK
3474 case NETLINK_ROUTE:
3475 fd_trans_register(ret, &target_netlink_route_trans);
3476 break;
3477 #endif
3478 case NETLINK_KOBJECT_UEVENT:
3479 /* nothing to do: messages are strings */
3480 break;
3481 case NETLINK_AUDIT:
3482 fd_trans_register(ret, &target_netlink_audit_trans);
3483 break;
3484 default:
3485 g_assert_not_reached();
3486 }
3487 }
3488 }
3489 return ret;
3490 }
3491
3492 /* do_bind() Must return target values and target errnos. */
3493 static abi_long do_bind(int sockfd, abi_ulong target_addr,
3494 socklen_t addrlen)
3495 {
3496 void *addr;
3497 abi_long ret;
3498
3499 if ((int)addrlen < 0) {
3500 return -TARGET_EINVAL;
3501 }
3502
3503 addr = alloca(addrlen+1);
3504
3505 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
3506 if (ret)
3507 return ret;
3508
3509 return get_errno(bind(sockfd, addr, addrlen));
3510 }
3511
3512 /* do_connect() Must return target values and target errnos. */
3513 static abi_long do_connect(int sockfd, abi_ulong target_addr,
3514 socklen_t addrlen)
3515 {
3516 void *addr;
3517 abi_long ret;
3518
3519 if ((int)addrlen < 0) {
3520 return -TARGET_EINVAL;
3521 }
3522
3523 addr = alloca(addrlen+1);
3524
3525 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
3526 if (ret)
3527 return ret;
3528
3529 return get_errno(safe_connect(sockfd, addr, addrlen));
3530 }
3531
3532 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3533 static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
3534 int flags, int send)
3535 {
3536 abi_long ret, len;
3537 struct msghdr msg;
3538 abi_ulong count;
3539 struct iovec *vec;
3540 abi_ulong target_vec;
3541
3542 if (msgp->msg_name) {
3543 msg.msg_namelen = tswap32(msgp->msg_namelen);
3544 msg.msg_name = alloca(msg.msg_namelen+1);
3545 ret = target_to_host_sockaddr(fd, msg.msg_name,
3546 tswapal(msgp->msg_name),
3547 msg.msg_namelen);
3548 if (ret == -TARGET_EFAULT) {
3549 /* For connected sockets msg_name and msg_namelen must
3550 * be ignored, so returning EFAULT immediately is wrong.
3551 * Instead, pass a bad msg_name to the host kernel, and
3552 * let it decide whether to return EFAULT or not.
3553 */
3554 msg.msg_name = (void *)-1;
3555 } else if (ret) {
3556 goto out2;
3557 }
3558 } else {
3559 msg.msg_name = NULL;
3560 msg.msg_namelen = 0;
3561 }
3562 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
3563 msg.msg_control = alloca(msg.msg_controllen);
3564 msg.msg_flags = tswap32(msgp->msg_flags);
3565
3566 count = tswapal(msgp->msg_iovlen);
3567 target_vec = tswapal(msgp->msg_iov);
3568
3569 if (count > IOV_MAX) {
3570 /* sendrcvmsg returns a different errno for this condition than
3571 * readv/writev, so we must catch it here before lock_iovec() does.
3572 */
3573 ret = -TARGET_EMSGSIZE;
3574 goto out2;
3575 }
3576
3577 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
3578 target_vec, count, send);
3579 if (vec == NULL) {
3580 ret = -host_to_target_errno(errno);
3581 goto out2;
3582 }
3583 msg.msg_iovlen = count;
3584 msg.msg_iov = vec;
3585
3586 if (send) {
3587 if (fd_trans_target_to_host_data(fd)) {
3588 void *host_msg;
3589
3590 host_msg = g_malloc(msg.msg_iov->iov_len);
3591 memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
3592 ret = fd_trans_target_to_host_data(fd)(host_msg,
3593 msg.msg_iov->iov_len);
3594 if (ret >= 0) {
3595 msg.msg_iov->iov_base = host_msg;
3596 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3597 }
3598 g_free(host_msg);
3599 } else {
3600 ret = target_to_host_cmsg(&msg, msgp);
3601 if (ret == 0) {
3602 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3603 }
3604 }
3605 } else {
3606 ret = get_errno(safe_recvmsg(fd, &msg, flags));
3607 if (!is_error(ret)) {
3608 len = ret;
3609 if (fd_trans_host_to_target_data(fd)) {
3610 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
3611 len);
3612 } else {
3613 ret = host_to_target_cmsg(msgp, &msg);
3614 }
3615 if (!is_error(ret)) {
3616 msgp->msg_namelen = tswap32(msg.msg_namelen);
3617 if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
3618 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
3619 msg.msg_name, msg.msg_namelen);
3620 if (ret) {
3621 goto out;
3622 }
3623 }
3624
3625 ret = len;
3626 }
3627 }
3628 }
3629
3630 out:
3631 unlock_iovec(vec, target_vec, count, !send);
3632 out2:
3633 return ret;
3634 }
3635
3636 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3637 int flags, int send)
3638 {
3639 abi_long ret;
3640 struct target_msghdr *msgp;
3641
3642 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3643 msgp,
3644 target_msg,
3645 send ? 1 : 0)) {
3646 return -TARGET_EFAULT;
3647 }
3648 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
3649 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3650 return ret;
3651 }
3652
3653 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3654 * so it might not have this *mmsg-specific flag either.
3655 */
3656 #ifndef MSG_WAITFORONE
3657 #define MSG_WAITFORONE 0x10000
3658 #endif
3659
3660 static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3661 unsigned int vlen, unsigned int flags,
3662 int send)
3663 {
3664 struct target_mmsghdr *mmsgp;
3665 abi_long ret = 0;
3666 int i;
3667
3668 if (vlen > UIO_MAXIOV) {
3669 vlen = UIO_MAXIOV;
3670 }
3671
3672 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3673 if (!mmsgp) {
3674 return -TARGET_EFAULT;
3675 }
3676
3677 for (i = 0; i < vlen; i++) {
3678 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
3679 if (is_error(ret)) {
3680 break;
3681 }
3682 mmsgp[i].msg_len = tswap32(ret);
3683 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3684 if (flags & MSG_WAITFORONE) {
3685 flags |= MSG_DONTWAIT;
3686 }
3687 }
3688
3689 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
3690
3691 /* Return number of datagrams sent if we sent any at all;
3692 * otherwise return the error.
3693 */
3694 if (i) {
3695 return i;
3696 }
3697 return ret;
3698 }
3699
3700 /* do_accept4() Must return target values and target errnos. */
3701 static abi_long do_accept4(int fd, abi_ulong target_addr,
3702 abi_ulong target_addrlen_addr, int flags)
3703 {
3704 socklen_t addrlen;
3705 void *addr;
3706 abi_long ret;
3707 int host_flags;
3708
3709 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
3710
3711 if (target_addr == 0) {
3712 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
3713 }
3714
3715 /* linux returns EINVAL if addrlen pointer is invalid */
3716 if (get_user_u32(addrlen, target_addrlen_addr))
3717 return -TARGET_EINVAL;
3718
3719 if ((int)addrlen < 0) {
3720 return -TARGET_EINVAL;
3721 }
3722
3723 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3724 return -TARGET_EINVAL;
3725
3726 addr = alloca(addrlen);
3727
3728 ret = get_errno(safe_accept4(fd, addr, &addrlen, host_flags));
3729 if (!is_error(ret)) {
3730 host_to_target_sockaddr(target_addr, addr, addrlen);
3731 if (put_user_u32(addrlen, target_addrlen_addr))
3732 ret = -TARGET_EFAULT;
3733 }
3734 return ret;
3735 }
3736
3737 /* do_getpeername() Must return target values and target errnos. */
3738 static abi_long do_getpeername(int fd, abi_ulong target_addr,
3739 abi_ulong target_addrlen_addr)
3740 {
3741 socklen_t addrlen;
3742 void *addr;
3743 abi_long ret;
3744
3745 if (get_user_u32(addrlen, target_addrlen_addr))
3746 return -TARGET_EFAULT;
3747
3748 if ((int)addrlen < 0) {
3749 return -TARGET_EINVAL;
3750 }
3751
3752 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3753 return -TARGET_EFAULT;
3754
3755 addr = alloca(addrlen);
3756
3757 ret = get_errno(getpeername(fd, addr, &addrlen));
3758 if (!is_error(ret)) {
3759 host_to_target_sockaddr(target_addr, addr, addrlen);
3760 if (put_user_u32(addrlen, target_addrlen_addr))
3761 ret = -TARGET_EFAULT;
3762 }
3763 return ret;
3764 }
3765
3766 /* do_getsockname() Must return target values and target errnos. */
3767 static abi_long do_getsockname(int fd, abi_ulong target_addr,
3768 abi_ulong target_addrlen_addr)
3769 {
3770 socklen_t addrlen;
3771 void *addr;
3772 abi_long ret;
3773
3774 if (get_user_u32(addrlen, target_addrlen_addr))
3775 return -TARGET_EFAULT;
3776
3777 if ((int)addrlen < 0) {
3778 return -TARGET_EINVAL;
3779 }
3780
3781 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3782 return -TARGET_EFAULT;
3783
3784 addr = alloca(addrlen);
3785
3786 ret = get_errno(getsockname(fd, addr, &addrlen));
3787 if (!is_error(ret)) {
3788 host_to_target_sockaddr(target_addr, addr, addrlen);
3789 if (put_user_u32(addrlen, target_addrlen_addr))
3790 ret = -TARGET_EFAULT;
3791 }
3792 return ret;
3793 }
3794
3795 /* do_socketpair() Must return target values and target errnos. */
3796 static abi_long do_socketpair(int domain, int type, int protocol,
3797 abi_ulong target_tab_addr)
3798 {
3799 int tab[2];
3800 abi_long ret;
3801
3802 target_to_host_sock_type(&type);
3803
3804 ret = get_errno(socketpair(domain, type, protocol, tab));
3805 if (!is_error(ret)) {
3806 if (put_user_s32(tab[0], target_tab_addr)
3807 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3808 ret = -TARGET_EFAULT;
3809 }
3810 return ret;
3811 }
3812
3813 /* do_sendto() Must return target values and target errnos. */
3814 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3815 abi_ulong target_addr, socklen_t addrlen)
3816 {
3817 void *addr;
3818 void *host_msg;
3819 void *copy_msg = NULL;
3820 abi_long ret;
3821
3822 if ((int)addrlen < 0) {
3823 return -TARGET_EINVAL;
3824 }
3825
3826 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3827 if (!host_msg)
3828 return -TARGET_EFAULT;
3829 if (fd_trans_target_to_host_data(fd)) {
3830 copy_msg = host_msg;
3831 host_msg = g_malloc(len);
3832 memcpy(host_msg, copy_msg, len);
3833 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3834 if (ret < 0) {
3835 goto fail;
3836 }
3837 }
3838 if (target_addr) {
3839 addr = alloca(addrlen+1);
3840 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
3841 if (ret) {
3842 goto fail;
3843 }
3844 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
3845 } else {
3846 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
3847 }
3848 fail:
3849 if (copy_msg) {
3850 g_free(host_msg);
3851 host_msg = copy_msg;
3852 }
3853 unlock_user(host_msg, msg, 0);
3854 return ret;
3855 }
3856
3857 /* do_recvfrom() Must return target values and target errnos. */
3858 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3859 abi_ulong target_addr,
3860 abi_ulong target_addrlen)
3861 {
3862 socklen_t addrlen;
3863 void *addr;
3864 void *host_msg;
3865 abi_long ret;
3866
3867 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3868 if (!host_msg)
3869 return -TARGET_EFAULT;
3870 if (target_addr) {
3871 if (get_user_u32(addrlen, target_addrlen)) {
3872 ret = -TARGET_EFAULT;
3873 goto fail;
3874 }
3875 if ((int)addrlen < 0) {
3876 ret = -TARGET_EINVAL;
3877 goto fail;
3878 }
3879 addr = alloca(addrlen);
3880 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
3881 addr, &addrlen));
3882 } else {
3883 addr = NULL; /* To keep compiler quiet. */
3884 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
3885 }
3886 if (!is_error(ret)) {
3887 if (fd_trans_host_to_target_data(fd)) {
3888 ret = fd_trans_host_to_target_data(fd)(host_msg, ret);
3889 }
3890 if (target_addr) {
3891 host_to_target_sockaddr(target_addr, addr, addrlen);
3892 if (put_user_u32(addrlen, target_addrlen)) {
3893 ret = -TARGET_EFAULT;
3894 goto fail;
3895 }
3896 }
3897 unlock_user(host_msg, msg, len);
3898 } else {
3899 fail:
3900 unlock_user(host_msg, msg, 0);
3901 }
3902 return ret;
3903 }
3904
3905 #ifdef TARGET_NR_socketcall
3906 /* do_socketcall() must return target values and target errnos. */
3907 static abi_long do_socketcall(int num, abi_ulong vptr)
3908 {
3909 static const unsigned nargs[] = { /* number of arguments per operation */
3910 [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
3911 [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
3912 [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
3913 [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
3914 [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
3915 [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
3916 [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
3917 [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
3918 [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
3919 [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
3920 [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
3921 [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
3922 [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
3923 [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3924 [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3925 [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
3926 [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
3927 [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
3928 [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
3929 [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
3930 };
3931 abi_long a[6]; /* max 6 args */
3932 unsigned i;
3933
3934 /* check the range of the first argument num */
3935 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3936 if (num < 1 || num > TARGET_SYS_SENDMMSG) {
3937 return -TARGET_EINVAL;
3938 }
3939 /* ensure we have space for args */
3940 if (nargs[num] > ARRAY_SIZE(a)) {
3941 return -TARGET_EINVAL;
3942 }
3943 /* collect the arguments in a[] according to nargs[] */
3944 for (i = 0; i < nargs[num]; ++i) {
3945 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
3946 return -TARGET_EFAULT;
3947 }
3948 }
3949 /* now when we have the args, invoke the appropriate underlying function */
3950 switch (num) {
3951 case TARGET_SYS_SOCKET: /* domain, type, protocol */
3952 return do_socket(a[0], a[1], a[2]);
3953 case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
3954 return do_bind(a[0], a[1], a[2]);
3955 case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
3956 return do_connect(a[0], a[1], a[2]);
3957 case TARGET_SYS_LISTEN: /* sockfd, backlog */
3958 return get_errno(listen(a[0], a[1]));
3959 case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
3960 return do_accept4(a[0], a[1], a[2], 0);
3961 case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
3962 return do_getsockname(a[0], a[1], a[2]);
3963 case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
3964 return do_getpeername(a[0], a[1], a[2]);
3965 case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
3966 return do_socketpair(a[0], a[1], a[2], a[3]);
3967 case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
3968 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
3969 case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
3970 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
3971 case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
3972 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
3973 case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
3974 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
3975 case TARGET_SYS_SHUTDOWN: /* sockfd, how */
3976 return get_errno(shutdown(a[0], a[1]));
3977 case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3978 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
3979 case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3980 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
3981 case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
3982 return do_sendrecvmsg(a[0], a[1], a[2], 1);
3983 case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
3984 return do_sendrecvmsg(a[0], a[1], a[2], 0);
3985 case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
3986 return do_accept4(a[0], a[1], a[2], a[3]);
3987 case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
3988 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
3989 case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
3990 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
3991 default:
3992 gemu_log("Unsupported socketcall: %d\n", num);
3993 return -TARGET_EINVAL;
3994 }
3995 }
3996 #endif
3997
3998 #define N_SHM_REGIONS 32
3999
4000 static struct shm_region {
4001 abi_ulong start;
4002 abi_ulong size;
4003 bool in_use;
4004 } shm_regions[N_SHM_REGIONS];
4005
4006 #ifndef TARGET_SEMID64_DS
4007 /* asm-generic version of this struct */
4008 struct target_semid64_ds
4009 {
4010 struct target_ipc_perm sem_perm;
4011 abi_ulong sem_otime;
4012 #if TARGET_ABI_BITS == 32
4013 abi_ulong __unused1;
4014 #endif
4015 abi_ulong sem_ctime;
4016 #if TARGET_ABI_BITS == 32
4017 abi_ulong __unused2;
4018 #endif
4019 abi_ulong sem_nsems;
4020 abi_ulong __unused3;
4021 abi_ulong __unused4;
4022 };
4023 #endif
4024
4025 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
4026 abi_ulong target_addr)
4027 {
4028 struct target_ipc_perm *target_ip;
4029 struct target_semid64_ds *target_sd;
4030
4031 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4032 return -TARGET_EFAULT;
4033 target_ip = &(target_sd->sem_perm);
4034 host_ip->__key = tswap32(target_ip->__key);
4035 host_ip->uid = tswap32(target_ip->uid);
4036 host_ip->gid = tswap32(target_ip->gid);
4037 host_ip->cuid = tswap32(target_ip->cuid);
4038 host_ip->cgid = tswap32(target_ip->cgid);
4039 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
4040 host_ip->mode = tswap32(target_ip->mode);
4041 #else
4042 host_ip->mode = tswap16(target_ip->mode);
4043 #endif
4044 #if defined(TARGET_PPC)
4045 host_ip->__seq = tswap32(target_ip->__seq);
4046 #else
4047 host_ip->__seq = tswap16(target_ip->__seq);
4048 #endif
4049 unlock_user_struct(target_sd, target_addr, 0);
4050 return 0;
4051 }
4052
4053 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
4054 struct ipc_perm *host_ip)
4055 {
4056 struct target_ipc_perm *target_ip;
4057 struct target_semid64_ds *target_sd;
4058
4059 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4060 return -TARGET_EFAULT;
4061 target_ip = &(target_sd->sem_perm);
4062 target_ip->__key = tswap32(host_ip->__key);
4063 target_ip->uid = tswap32(host_ip->uid);
4064 target_ip->gid = tswap32(host_ip->gid);
4065 target_ip->cuid = tswap32(host_ip->cuid);
4066 target_ip->cgid = tswap32(host_ip->cgid);
4067 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
4068 target_ip->mode = tswap32(host_ip->mode);
4069 #else
4070 target_ip->mode = tswap16(host_ip->mode);
4071 #endif
4072 #if defined(TARGET_PPC)
4073 target_ip->__seq = tswap32(host_ip->__seq);
4074 #else
4075 target_ip->__seq = tswap16(host_ip->__seq);
4076 #endif
4077 unlock_user_struct(target_sd, target_addr, 1);
4078 return 0;
4079 }
4080
4081 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
4082 abi_ulong target_addr)
4083 {
4084 struct target_semid64_ds *target_sd;
4085
4086 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4087 return -TARGET_EFAULT;
4088 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
4089 return -TARGET_EFAULT;
4090 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
4091 host_sd->sem_otime = tswapal(target_sd->sem_otime);
4092 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
4093 unlock_user_struct(target_sd, target_addr, 0);
4094 return 0;
4095 }
4096
4097 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
4098 struct semid_ds *host_sd)
4099 {
4100 struct target_semid64_ds *target_sd;
4101
4102 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4103 return -TARGET_EFAULT;
4104 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
4105 return -TARGET_EFAULT;
4106 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
4107 target_sd->sem_otime = tswapal(host_sd->sem_otime);
4108 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
4109 unlock_user_struct(target_sd, target_addr, 1);
4110 return 0;
4111 }
4112
4113 struct target_seminfo {
4114 int semmap;
4115 int semmni;
4116 int semmns;
4117 int semmnu;
4118 int semmsl;
4119 int semopm;
4120 int semume;
4121 int semusz;
4122 int semvmx;
4123 int semaem;
4124 };
4125
4126 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
4127 struct seminfo *host_seminfo)
4128 {
4129 struct target_seminfo *target_seminfo;
4130 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
4131 return -TARGET_EFAULT;
4132 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
4133 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
4134 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
4135 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
4136 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
4137 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
4138 __put_user(host_seminfo->semume, &target_seminfo->semume);
4139 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
4140 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
4141 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
4142 unlock_user_struct(target_seminfo, target_addr, 1);
4143 return 0;
4144 }
4145
4146 union semun {
4147 int val;
4148 struct semid_ds *buf;
4149 unsigned short *array;
4150 struct seminfo *__buf;
4151 };
4152
4153 union target_semun {
4154 int val;
4155 abi_ulong buf;
4156 abi_ulong array;
4157 abi_ulong __buf;
4158 };
4159
4160 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
4161 abi_ulong target_addr)
4162 {
4163 int nsems;
4164 unsigned short *array;
4165 union semun semun;
4166 struct semid_ds semid_ds;
4167 int i, ret;
4168
4169 semun.buf = &semid_ds;
4170
4171 ret = semctl(semid, 0, IPC_STAT, semun);
4172 if (ret == -1)
4173 return get_errno(ret);
4174
4175 nsems = semid_ds.sem_nsems;
4176
4177 *host_array = g_try_new(unsigned short, nsems);
4178 if (!*host_array) {
4179 return -TARGET_ENOMEM;
4180 }
4181 array = lock_user(VERIFY_READ, target_addr,
4182 nsems*sizeof(unsigned short), 1);
4183 if (!array) {
4184 g_free(*host_array);
4185 return -TARGET_EFAULT;
4186 }
4187
4188 for(i=0; i<nsems; i++) {
4189 __get_user((*host_array)[i], &array[i]);
4190 }
4191 unlock_user(array, target_addr, 0);
4192
4193 return 0;
4194 }
4195
4196 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
4197 unsigned short **host_array)
4198 {
4199 int nsems;
4200 unsigned short *array;
4201 union semun semun;
4202 struct semid_ds semid_ds;
4203 int i, ret;
4204
4205 semun.buf = &semid_ds;
4206
4207 ret = semctl(semid, 0, IPC_STAT, semun);
4208 if (ret == -1)
4209 return get_errno(ret);
4210
4211 nsems = semid_ds.sem_nsems;
4212
4213 array = lock_user(VERIFY_WRITE, target_addr,
4214 nsems*sizeof(unsigned short), 0);
4215 if (!array)
4216 return -TARGET_EFAULT;
4217
4218 for(i=0; i<nsems; i++) {
4219 __put_user((*host_array)[i], &array[i]);
4220 }
4221 g_free(*host_array);
4222 unlock_user(array, target_addr, 1);
4223
4224 return 0;
4225 }
4226
4227 static inline abi_long do_semctl(int semid, int semnum, int cmd,
4228 abi_ulong target_arg)
4229 {
4230 union target_semun target_su = { .buf = target_arg };
4231 union semun arg;
4232 struct semid_ds dsarg;
4233 unsigned short *array = NULL;
4234 struct seminfo seminfo;
4235 abi_long ret = -TARGET_EINVAL;
4236 abi_long err;
4237 cmd &= 0xff;
4238
4239 switch( cmd ) {
4240 case GETVAL:
4241 case SETVAL:
4242 /* In 64 bit cross-endian situations, we will erroneously pick up
4243 * the wrong half of the union for the "val" element. To rectify
4244 * this, the entire 8-byte structure is byteswapped, followed by
4245 * a swap of the 4 byte val field. In other cases, the data is
4246 * already in proper host byte order. */
4247 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
4248 target_su.buf = tswapal(target_su.buf);
4249 arg.val = tswap32(target_su.val);
4250 } else {
4251 arg.val = target_su.val;
4252 }
4253 ret = get_errno(semctl(semid, semnum, cmd, arg));
4254 break;
4255 case GETALL:
4256 case SETALL:
4257 err = target_to_host_semarray(semid, &array, target_su.array);
4258 if (err)
4259 return err;
4260 arg.array = array;
4261 ret = get_errno(semctl(semid, semnum, cmd, arg));
4262 err = host_to_target_semarray(semid, target_su.array, &array);
4263 if (err)
4264 return err;
4265 break;
4266 case IPC_STAT:
4267 case IPC_SET:
4268 case SEM_STAT:
4269 err = target_to_host_semid_ds(&dsarg, target_su.buf);
4270 if (err)
4271 return err;
4272 arg.buf = &dsarg;
4273 ret = get_errno(semctl(semid, semnum, cmd, arg));
4274 err = host_to_target_semid_ds(target_su.buf, &dsarg);
4275 if (err)
4276 return err;
4277 break;
4278 case IPC_INFO:
4279 case SEM_INFO:
4280 arg.__buf = &seminfo;
4281 ret = get_errno(semctl(semid, semnum, cmd, arg));
4282 err = host_to_target_seminfo(target_su.__buf, &seminfo);
4283 if (err)
4284 return err;
4285 break;
4286 case IPC_RMID:
4287 case GETPID:
4288 case GETNCNT:
4289 case GETZCNT:
4290 ret = get_errno(semctl(semid, semnum, cmd, NULL));
4291 break;
4292 }
4293
4294 return ret;
4295 }
4296
4297 struct target_sembuf {
4298 unsigned short sem_num;
4299 short sem_op;
4300 short sem_flg;
4301 };
4302
4303 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
4304 abi_ulong target_addr,
4305 unsigned nsops)
4306 {
4307 struct target_sembuf *target_sembuf;
4308 int i;
4309
4310 target_sembuf = lock_user(VERIFY_READ, target_addr,
4311 nsops*sizeof(struct target_sembuf), 1);
4312 if (!target_sembuf)
4313 return -TARGET_EFAULT;
4314
4315 for(i=0; i<nsops; i++) {
4316 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
4317 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
4318 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
4319 }
4320
4321 unlock_user(target_sembuf, target_addr, 0);
4322
4323 return 0;
4324 }
4325
4326 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
4327 {
4328 struct sembuf sops[nsops];
4329
4330 if (target_to_host_sembuf(sops, ptr, nsops))
4331 return -TARGET_EFAULT;
4332
4333 return get_errno(safe_semtimedop(semid, sops, nsops, NULL));
4334 }
4335
4336 struct target_msqid_ds
4337 {
4338 struct target_ipc_perm msg_perm;
4339 abi_ulong msg_stime;
4340 #if TARGET_ABI_BITS == 32
4341 abi_ulong __unused1;
4342 #endif
4343 abi_ulong msg_rtime;
4344 #if TARGET_ABI_BITS == 32
4345 abi_ulong __unused2;
4346 #endif
4347 abi_ulong msg_ctime;
4348 #if TARGET_ABI_BITS == 32
4349 abi_ulong __unused3;
4350 #endif
4351 abi_ulong __msg_cbytes;
4352 abi_ulong msg_qnum;
4353 abi_ulong msg_qbytes;
4354 abi_ulong msg_lspid;
4355 abi_ulong msg_lrpid;
4356 abi_ulong __unused4;
4357 abi_ulong __unused5;
4358 };
4359
4360 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
4361 abi_ulong target_addr)
4362 {
4363 struct target_msqid_ds *target_md;
4364
4365 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
4366 return -TARGET_EFAULT;
4367 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
4368 return -TARGET_EFAULT;
4369 host_md->msg_stime = tswapal(target_md->msg_stime);
4370 host_md->msg_rtime = tswapal(target_md->msg_rtime);
4371 host_md->msg_ctime = tswapal(target_md->msg_ctime);
4372 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
4373 host_md->msg_qnum = tswapal(target_md->msg_qnum);
4374 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
4375 host_md->msg_lspid = tswapal(target_md->msg_lspid);
4376 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
4377 unlock_user_struct(target_md, target_addr, 0);
4378 return 0;
4379 }
4380
4381 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
4382 struct msqid_ds *host_md)
4383 {
4384 struct target_msqid_ds *target_md;
4385
4386 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
4387 return -TARGET_EFAULT;
4388 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
4389 return -TARGET_EFAULT;
4390 target_md->msg_stime = tswapal(host_md->msg_stime);
4391 target_md->msg_rtime = tswapal(host_md->msg_rtime);
4392 target_md->msg_ctime = tswapal(host_md->msg_ctime);
4393 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
4394 target_md->msg_qnum = tswapal(host_md->msg_qnum);
4395 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
4396 target_md->msg_lspid = tswapal(host_md->msg_lspid);
4397 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
4398 unlock_user_struct(target_md, target_addr, 1);
4399 return 0;
4400 }
4401
4402 struct target_msginfo {
4403 int msgpool;
4404 int msgmap;
4405 int msgmax;
4406 int msgmnb;
4407 int msgmni;
4408 int msgssz;
4409 int msgtql;
4410 unsigned short int msgseg;
4411 };
4412
4413 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
4414 struct msginfo *host_msginfo)
4415 {
4416 struct target_msginfo *target_msginfo;
4417 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
4418 return -TARGET_EFAULT;
4419 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
4420 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
4421 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
4422 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
4423 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
4424 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
4425 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
4426 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
4427 unlock_user_struct(target_msginfo, target_addr, 1);
4428 return 0;
4429 }
4430
4431 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
4432 {
4433 struct msqid_ds dsarg;
4434 struct msginfo msginfo;
4435 abi_long ret = -TARGET_EINVAL;
4436
4437 cmd &= 0xff;
4438
4439 switch (cmd) {
4440 case IPC_STAT:
4441 case IPC_SET:
4442 case MSG_STAT:
4443 if (target_to_host_msqid_ds(&dsarg,ptr))
4444 return -TARGET_EFAULT;
4445 ret = get_errno(msgctl(msgid, cmd, &dsarg));
4446 if (host_to_target_msqid_ds(ptr,&dsarg))
4447 return -TARGET_EFAULT;
4448 break;
4449 case IPC_RMID:
4450 ret = get_errno(msgctl(msgid, cmd, NULL));
4451 break;
4452 case IPC_INFO:
4453 case MSG_INFO:
4454 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
4455 if (host_to_target_msginfo(ptr, &msginfo))
4456 return -TARGET_EFAULT;
4457 break;
4458 }
4459
4460 return ret;
4461 }
4462
4463 struct target_msgbuf {
4464 abi_long mtype;
4465 char mtext[1];
4466 };
4467
4468 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
4469 ssize_t msgsz, int msgflg)
4470 {
4471 struct target_msgbuf *target_mb;
4472 struct msgbuf *host_mb;
4473 abi_long ret = 0;
4474
4475 if (msgsz < 0) {
4476 return -TARGET_EINVAL;
4477 }
4478
4479 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
4480 return -TARGET_EFAULT;
4481 host_mb = g_try_malloc(msgsz + sizeof(long));
4482 if (!host_mb) {
4483 unlock_user_struct(target_mb, msgp, 0);
4484 return -TARGET_ENOMEM;
4485 }
4486 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
4487 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
4488 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
4489 g_free(host_mb);
4490 unlock_user_struct(target_mb, msgp, 0);
4491
4492 return ret;
4493 }
4494
4495 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
4496 ssize_t msgsz, abi_long msgtyp,
4497 int msgflg)
4498 {
4499 struct target_msgbuf *target_mb;
4500 char *target_mtext;
4501 struct msgbuf *host_mb;
4502 abi_long ret = 0;
4503
4504 if (msgsz < 0) {
4505 return -TARGET_EINVAL;
4506 }
4507
4508 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
4509 return -TARGET_EFAULT;
4510
4511 host_mb = g_try_malloc(msgsz + sizeof(long));
4512 if (!host_mb) {
4513 ret = -TARGET_ENOMEM;
4514 goto end;
4515 }
4516 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
4517
4518 if (ret > 0) {
4519 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
4520 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
4521 if (!target_mtext) {
4522 ret = -TARGET_EFAULT;
4523 goto end;
4524 }
4525 memcpy(target_mb->mtext, host_mb->mtext, ret);
4526 unlock_user(target_mtext, target_mtext_addr, ret);
4527 }
4528
4529 target_mb->mtype = tswapal(host_mb->mtype);
4530
4531 end:
4532 if (target_mb)
4533 unlock_user_struct(target_mb, msgp, 1);
4534 g_free(host_mb);
4535 return ret;
4536 }
4537
4538 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
4539 abi_ulong target_addr)
4540 {
4541 struct target_shmid_ds *target_sd;
4542
4543 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4544 return -TARGET_EFAULT;
4545 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
4546 return -TARGET_EFAULT;
4547 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4548 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
4549 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4550 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4551 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4552 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4553 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4554 unlock_user_struct(target_sd, target_addr, 0);
4555 return 0;
4556 }
4557
4558 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
4559 struct shmid_ds *host_sd)
4560 {
4561 struct target_shmid_ds *target_sd;
4562
4563 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4564 return -TARGET_EFAULT;
4565 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
4566 return -TARGET_EFAULT;
4567 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4568 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
4569 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4570 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4571 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4572 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4573 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4574 unlock_user_struct(target_sd, target_addr, 1);
4575 return 0;
4576 }
4577
4578 struct target_shminfo {
4579 abi_ulong shmmax;
4580 abi_ulong shmmin;
4581 abi_ulong shmmni;
4582 abi_ulong shmseg;
4583 abi_ulong shmall;
4584 };
4585
4586 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
4587 struct shminfo *host_shminfo)
4588 {
4589 struct target_shminfo *target_shminfo;
4590 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
4591 return -TARGET_EFAULT;
4592 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
4593 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
4594 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
4595 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
4596 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
4597 unlock_user_struct(target_shminfo, target_addr, 1);
4598 return 0;
4599 }
4600
4601 struct target_shm_info {
4602 int used_ids;
4603 abi_ulong shm_tot;
4604 abi_ulong shm_rss;
4605 abi_ulong shm_swp;
4606 abi_ulong swap_attempts;
4607 abi_ulong swap_successes;
4608 };
4609
4610 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
4611 struct shm_info *host_shm_info)
4612 {
4613 struct target_shm_info *target_shm_info;
4614 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
4615 return -TARGET_EFAULT;
4616 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
4617 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
4618 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
4619 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
4620 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
4621 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
4622 unlock_user_struct(target_shm_info, target_addr, 1);
4623 return 0;
4624 }
4625
4626 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
4627 {
4628 struct shmid_ds dsarg;
4629 struct shminfo shminfo;
4630 struct shm_info shm_info;
4631 abi_long ret = -TARGET_EINVAL;
4632
4633 cmd &= 0xff;
4634
4635 switch(cmd) {
4636 case IPC_STAT:
4637 case IPC_SET:
4638 case SHM_STAT:
4639 if (target_to_host_shmid_ds(&dsarg, buf))
4640 return -TARGET_EFAULT;
4641 ret = get_errno(shmctl(shmid, cmd, &dsarg));
4642 if (host_to_target_shmid_ds(buf, &dsarg))
4643 return -TARGET_EFAULT;
4644 break;
4645 case IPC_INFO:
4646 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
4647 if (host_to_target_shminfo(buf, &shminfo))
4648 return -TARGET_EFAULT;
4649 break;
4650 case SHM_INFO:
4651 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
4652 if (host_to_target_shm_info(buf, &shm_info))
4653 return -TARGET_EFAULT;
4654 break;
4655 case IPC_RMID:
4656 case SHM_LOCK:
4657 case SHM_UNLOCK:
4658 ret = get_errno(shmctl(shmid, cmd, NULL));
4659 break;
4660 }
4661
4662 return ret;
4663 }
4664
4665 #ifndef TARGET_FORCE_SHMLBA
4666 /* For most architectures, SHMLBA is the same as the page size;
4667 * some architectures have larger values, in which case they should
4668 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
4669 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
4670 * and defining its own value for SHMLBA.
4671 *
4672 * The kernel also permits SHMLBA to be set by the architecture to a
4673 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
4674 * this means that addresses are rounded to the large size if
4675 * SHM_RND is set but addresses not aligned to that size are not rejected
4676 * as long as they are at least page-aligned. Since the only architecture
4677 * which uses this is ia64 this code doesn't provide for that oddity.
4678 */
4679 static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
4680 {
4681 return TARGET_PAGE_SIZE;
4682 }
4683 #endif
4684
4685 static inline abi_ulong do_shmat(CPUArchState *cpu_env,
4686 int shmid, abi_ulong shmaddr, int shmflg)
4687 {
4688 abi_long raddr;
4689 void *host_raddr;
4690 struct shmid_ds shm_info;
4691 int i,ret;
4692 abi_ulong shmlba;
4693
4694 /* find out the length of the shared memory segment */
4695 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
4696 if (is_error(ret)) {
4697 /* can't get length, bail out */
4698 return ret;
4699 }
4700
4701 shmlba = target_shmlba(cpu_env);
4702
4703 if (shmaddr & (shmlba - 1)) {
4704 if (shmflg & SHM_RND) {
4705 shmaddr &= ~(shmlba - 1);
4706 } else {
4707 return -TARGET_EINVAL;
4708 }
4709 }
4710
4711 mmap_lock();
4712
4713 if (shmaddr)
4714 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
4715 else {
4716 abi_ulong mmap_start;
4717
4718 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
4719
4720 if (mmap_start == -1) {
4721 errno = ENOMEM;
4722 host_raddr = (void *)-1;
4723 } else
4724 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
4725 }
4726
4727 if (host_raddr == (void *)-1) {
4728 mmap_unlock();
4729 return get_errno((long)host_raddr);
4730 }
4731 raddr=h2g((unsigned long)host_raddr);
4732
4733 page_set_flags(raddr, raddr + shm_info.shm_segsz,
4734 PAGE_VALID | PAGE_READ |
4735 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
4736
4737 for (i = 0; i < N_SHM_REGIONS; i++) {
4738 if (!shm_regions[i].in_use) {
4739 shm_regions[i].in_use = true;
4740 shm_regions[i].start = raddr;
4741 shm_regions[i].size = shm_info.shm_segsz;
4742 break;
4743 }
4744 }
4745
4746 mmap_unlock();
4747 return raddr;
4748
4749 }
4750
4751 static inline abi_long do_shmdt(abi_ulong shmaddr)
4752 {
4753 int i;
4754
4755 for (i = 0; i < N_SHM_REGIONS; ++i) {
4756 if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
4757 shm_regions[i].in_use = false;
4758 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
4759 break;
4760 }
4761 }
4762
4763 return get_errno(shmdt(g2h(shmaddr)));
4764 }
4765
4766 #ifdef TARGET_NR_ipc
4767 /* ??? This only works with linear mappings. */
4768 /* do_ipc() must return target values and target errnos. */
4769 static abi_long do_ipc(CPUArchState *cpu_env,
4770 unsigned int call, abi_long first,
4771 abi_long second, abi_long third,
4772 abi_long ptr, abi_long fifth)
4773 {
4774 int version;
4775 abi_long ret = 0;
4776
4777 version = call >> 16;
4778 call &= 0xffff;
4779
4780 switch (call) {
4781 case IPCOP_semop:
4782 ret = do_semop(first, ptr, second);
4783 break;
4784
4785 case IPCOP_semget:
4786 ret = get_errno(semget(first, second, third));
4787 break;
4788
4789 case IPCOP_semctl: {
4790 /* The semun argument to semctl is passed by value, so dereference the
4791 * ptr argument. */
4792 abi_ulong atptr;
4793 get_user_ual(atptr, ptr);
4794 ret = do_semctl(first, second, third, atptr);
4795 break;
4796 }
4797
4798 case IPCOP_msgget:
4799 ret = get_errno(msgget(first, second));
4800 break;
4801
4802 case IPCOP_msgsnd:
4803 ret = do_msgsnd(first, ptr, second, third);
4804 break;
4805
4806 case IPCOP_msgctl:
4807 ret = do_msgctl(first, second, ptr);
4808 break;
4809
4810 case IPCOP_msgrcv:
4811 switch (version) {
4812 case 0:
4813 {
4814 struct target_ipc_kludge {
4815 abi_long msgp;
4816 abi_long msgtyp;
4817 } *tmp;
4818
4819 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
4820 ret = -TARGET_EFAULT;
4821 break;
4822 }
4823
4824 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
4825
4826 unlock_user_struct(tmp, ptr, 0);
4827 break;
4828 }
4829 default:
4830 ret = do_msgrcv(first, ptr, second, fifth, third);
4831 }
4832 break;
4833
4834 case IPCOP_shmat:
4835 switch (version) {
4836 default:
4837 {
4838 abi_ulong raddr;
4839 raddr = do_shmat(cpu_env, first, ptr, second);
4840 if (is_error(raddr))
4841 return get_errno(raddr);
4842 if (put_user_ual(raddr, third))
4843 return -TARGET_EFAULT;
4844 break;
4845 }
4846 case 1:
4847 ret = -TARGET_EINVAL;
4848 break;
4849 }
4850 break;
4851 case IPCOP_shmdt:
4852 ret = do_shmdt(ptr);
4853 break;
4854
4855 case IPCOP_shmget:
4856 /* IPC_* flag values are the same on all linux platforms */
4857 ret = get_errno(shmget(first, second, third));
4858 break;
4859
4860 /* IPC_* and SHM_* command values are the same on all linux platforms */
4861 case IPCOP_shmctl:
4862 ret = do_shmctl(first, second, ptr);
4863 break;
4864 default:
4865 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
4866 ret = -TARGET_ENOSYS;
4867 break;
4868 }
4869 return ret;
4870 }
4871 #endif
4872
4873 /* kernel structure types definitions */
4874
4875 #define STRUCT(name, ...) STRUCT_ ## name,
4876 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
4877 enum {
4878 #include "syscall_types.h"
4879 STRUCT_MAX
4880 };
4881 #undef STRUCT
4882 #undef STRUCT_SPECIAL
4883
4884 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
4885 #define STRUCT_SPECIAL(name)
4886 #include "syscall_types.h"
4887 #undef STRUCT
4888 #undef STRUCT_SPECIAL
4889
4890 typedef struct IOCTLEntry IOCTLEntry;
4891
4892 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
4893 int fd, int cmd, abi_long arg);
4894
4895 struct IOCTLEntry {
4896 int target_cmd;
4897 unsigned int host_cmd;
4898 const char *name;
4899 int access;
4900 do_ioctl_fn *do_ioctl;
4901 const argtype arg_type[5];
4902 };
4903
4904 #define IOC_R 0x0001
4905 #define IOC_W 0x0002
4906 #define IOC_RW (IOC_R | IOC_W)
4907
4908 #define MAX_STRUCT_SIZE 4096
4909
4910 #ifdef CONFIG_FIEMAP
4911 /* So fiemap access checks don't overflow on 32 bit systems.
4912 * This is very slightly smaller than the limit imposed by
4913 * the underlying kernel.
4914 */
4915 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4916 / sizeof(struct fiemap_extent))
4917
4918 static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
4919 int fd, int cmd, abi_long arg)
4920 {
4921 /* The parameter for this ioctl is a struct fiemap followed
4922 * by an array of struct fiemap_extent whose size is set
4923 * in fiemap->fm_extent_count. The array is filled in by the
4924 * ioctl.
4925 */
4926 int target_size_in, target_size_out;
4927 struct fiemap *fm;
4928 const argtype *arg_type = ie->arg_type;
4929 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
4930 void *argptr, *p;
4931 abi_long ret;
4932 int i, extent_size = thunk_type_size(extent_arg_type, 0);
4933 uint32_t outbufsz;
4934 int free_fm = 0;
4935
4936 assert(arg_type[0] == TYPE_PTR);
4937 assert(ie->access == IOC_RW);
4938 arg_type++;
4939 target_size_in = thunk_type_size(arg_type, 0);
4940 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
4941 if (!argptr) {
4942 return -TARGET_EFAULT;
4943 }
4944 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4945 unlock_user(argptr, arg, 0);
4946 fm = (struct fiemap *)buf_temp;
4947 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
4948 return -TARGET_EINVAL;
4949 }
4950
4951 outbufsz = sizeof (*fm) +
4952 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
4953
4954 if (outbufsz > MAX_STRUCT_SIZE) {
4955 /* We can't fit all the extents into the fixed size buffer.
4956 * Allocate one that is large enough and use it instead.
4957 */
4958 fm = g_try_malloc(outbufsz);
4959 if (!fm) {
4960 return -TARGET_ENOMEM;
4961 }
4962 memcpy(fm, buf_temp, sizeof(struct fiemap));
4963 free_fm = 1;
4964 }
4965 ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
4966 if (!is_error(ret)) {
4967 target_size_out = target_size_in;
4968 /* An extent_count of 0 means we were only counting the extents
4969 * so there are no structs to copy
4970 */
4971 if (fm->fm_extent_count != 0) {
4972 target_size_out += fm->fm_mapped_extents * extent_size;
4973 }
4974 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
4975 if (!argptr) {
4976 ret = -TARGET_EFAULT;
4977 } else {
4978 /* Convert the struct fiemap */
4979 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
4980 if (fm->fm_extent_count != 0) {
4981 p = argptr + target_size_in;
4982 /* ...and then all the struct fiemap_extents */
4983 for (i = 0; i < fm->fm_mapped_extents; i++) {
4984 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
4985 THUNK_TARGET);
4986 p += extent_size;
4987 }
4988 }
4989 unlock_user(argptr, arg, target_size_out);
4990 }
4991 }
4992 if (free_fm) {
4993 g_free(fm);
4994 }
4995 return ret;
4996 }
4997 #endif
4998
4999 static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
5000 int fd, int cmd, abi_long arg)
5001 {
5002 const argtype *arg_type = ie->arg_type;
5003 int target_size;
5004 void *argptr;
5005 int ret;
5006 struct ifconf *host_ifconf;
5007 uint32_t outbufsz;
5008 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
5009 int target_ifreq_size;
5010 int nb_ifreq;
5011 int free_buf = 0;
5012 int i;
5013 int target_ifc_len;
5014 abi_long target_ifc_buf;
5015 int host_ifc_len;
5016 char *host_ifc_buf;
5017
5018 assert(arg_type[0] == TYPE_PTR);
5019 assert(ie->access == IOC_RW);
5020
5021 arg_type++;
5022 target_size = thunk_type_size(arg_type, 0);
5023
5024 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5025 if (!argptr)
5026 return -TARGET_EFAULT;
5027 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5028 unlock_user(argptr, arg, 0);
5029
5030 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
5031 target_ifc_len = host_ifconf->ifc_len;
5032 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
5033
5034 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
5035 nb_ifreq = target_ifc_len / target_ifreq_size;
5036 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
5037
5038 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
5039 if (outbufsz > MAX_STRUCT_SIZE) {
5040 /* We can't fit all the extents into the fixed size buffer.
5041 * Allocate one that is large enough and use it instead.
5042 */
5043 host_ifconf = malloc(outbufsz);
5044 if (!host_ifconf) {
5045 return -TARGET_ENOMEM;
5046 }
5047 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
5048 free_buf = 1;
5049 }
5050 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
5051
5052 host_ifconf->ifc_len = host_ifc_len;
5053 host_ifconf->ifc_buf = host_ifc_buf;
5054
5055 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
5056 if (!is_error(ret)) {
5057 /* convert host ifc_len to target ifc_len */
5058
5059 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
5060 target_ifc_len = nb_ifreq * target_ifreq_size;
5061 host_ifconf->ifc_len = target_ifc_len;
5062
5063 /* restore target ifc_buf */
5064
5065 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
5066
5067 /* copy struct ifconf to target user */
5068
5069 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5070 if (!argptr)
5071 return -TARGET_EFAULT;
5072 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
5073 unlock_user(argptr, arg, target_size);
5074
5075 /* copy ifreq[] to target user */
5076
5077 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
5078 for (i = 0; i < nb_ifreq ; i++) {
5079 thunk_convert(argptr + i * target_ifreq_size,
5080 host_ifc_buf + i * sizeof(struct ifreq),
5081 ifreq_arg_type, THUNK_TARGET);
5082 }
5083 unlock_user(argptr, target_ifc_buf, target_ifc_len);
5084 }
5085
5086 if (free_buf) {
5087 free(host_ifconf);
5088 }
5089
5090 return ret;
5091 }
5092
5093 static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
5094 int cmd, abi_long arg)
5095 {
5096 void *argptr;
5097 struct dm_ioctl *host_dm;
5098 abi_long guest_data;
5099 uint32_t guest_data_size;
5100 int target_size;
5101 const argtype *arg_type = ie->arg_type;
5102 abi_long ret;
5103 void *big_buf = NULL;
5104 char *host_data;
5105
5106 arg_type++;
5107 target_size = thunk_type_size(arg_type, 0);
5108 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5109 if (!argptr) {
5110 ret = -TARGET_EFAULT;
5111 goto out;
5112 }
5113 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5114 unlock_user(argptr, arg, 0);
5115
5116 /* buf_temp is too small, so fetch things into a bigger buffer */
5117 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
5118 memcpy(big_buf, buf_temp, target_size);
5119 buf_temp = big_buf;
5120 host_dm = big_buf;
5121
5122 guest_data = arg + host_dm->data_start;
5123 if ((guest_data - arg) < 0) {
5124 ret = -TARGET_EINVAL;
5125 goto out;
5126 }
5127 guest_data_size = host_dm->data_size - host_dm->data_start;
5128 host_data = (char*)host_dm + host_dm->data_start;
5129
5130 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
5131 if (!argptr) {
5132 ret = -TARGET_EFAULT;
5133 goto out;
5134 }
5135
5136 switch (ie->host_cmd) {
5137 case DM_REMOVE_ALL:
5138 case DM_LIST_DEVICES:
5139 case DM_DEV_CREATE:
5140 case DM_DEV_REMOVE:
5141 case DM_DEV_SUSPEND:
5142 case DM_DEV_STATUS:
5143 case DM_DEV_WAIT:
5144 case DM_TABLE_STATUS:
5145 case DM_TABLE_CLEAR:
5146 case DM_TABLE_DEPS:
5147 case DM_LIST_VERSIONS:
5148 /* no input data */
5149 break;
5150 case DM_DEV_RENAME:
5151 case DM_DEV_SET_GEOMETRY:
5152 /* data contains only strings */
5153 memcpy(host_data, argptr, guest_data_size);
5154 break;
5155 case DM_TARGET_MSG:
5156 memcpy(host_data, argptr, guest_data_size);
5157 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
5158 break;
5159 case DM_TABLE_LOAD:
5160 {
5161 void *gspec = argptr;
5162 void *cur_data = host_data;
5163 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5164 int spec_size = thunk_type_size(arg_type, 0);
5165 int i;
5166
5167 for (i = 0; i < host_dm->target_count; i++) {
5168 struct dm_target_spec *spec = cur_data;
5169 uint32_t next;
5170 int slen;
5171
5172 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
5173 slen = strlen((char*)gspec + spec_size) + 1;
5174 next = spec->next;
5175 spec->next = sizeof(*spec) + slen;
5176 strcpy((char*)&spec[1], gspec + spec_size);
5177 gspec += next;
5178 cur_data += spec->next;
5179 }
5180 break;
5181 }
5182 default:
5183 ret = -TARGET_EINVAL;
5184 unlock_user(argptr, guest_data, 0);
5185 goto out;
5186 }
5187 unlock_user(argptr, guest_data, 0);
5188
5189 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
5190 if (!is_error(ret)) {
5191 guest_data = arg + host_dm->data_start;
5192 guest_data_size = host_dm->data_size - host_dm->data_start;
5193 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
5194 switch (ie->host_cmd) {
5195 case DM_REMOVE_ALL:
5196 case DM_DEV_CREATE:
5197 case DM_DEV_REMOVE:
5198 case DM_DEV_RENAME:
5199 case DM_DEV_SUSPEND:
5200 case DM_DEV_STATUS:
5201 case DM_TABLE_LOAD:
5202 case DM_TABLE_CLEAR:
5203 case DM_TARGET_MSG:
5204 case DM_DEV_SET_GEOMETRY:
5205 /* no return data */
5206 break;
5207 case DM_LIST_DEVICES:
5208 {
5209 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
5210 uint32_t remaining_data = guest_data_size;
5211 void *cur_data = argptr;
5212 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
5213 int nl_size = 12; /* can't use thunk_size due to alignment */
5214
5215 while (1) {
5216 uint32_t next = nl->next;
5217 if (next) {
5218 nl->next = nl_size + (strlen(nl->name) + 1);
5219 }
5220 if (remaining_data < nl->next) {
5221 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5222 break;
5223 }
5224 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
5225 strcpy(cur_data + nl_size, nl->name);
5226 cur_data += nl->next;
5227 remaining_data -= nl->next;
5228 if (!next) {
5229 break;
5230 }
5231 nl = (void*)nl + next;
5232 }
5233 break;
5234 }
5235 case DM_DEV_WAIT:
5236 case DM_TABLE_STATUS:
5237 {
5238 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
5239 void *cur_data = argptr;
5240 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5241 int spec_size = thunk_type_size(arg_type, 0);
5242 int i;
5243
5244 for (i = 0; i < host_dm->target_count; i++) {
5245 uint32_t next = spec->next;
5246 int slen = strlen((char*)&spec[1]) + 1;
5247 spec->next = (cur_data - argptr) + spec_size + slen;
5248 if (guest_data_size < spec->next) {
5249 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5250 break;
5251 }
5252 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
5253 strcpy(cur_data + spec_size, (char*)&spec[1]);
5254 cur_data = argptr + spec->next;
5255 spec = (void*)host_dm + host_dm->data_start + next;
5256 }
5257 break;
5258 }
5259 case DM_TABLE_DEPS:
5260 {
5261 void *hdata = (void*)host_dm + host_dm->data_start;
5262 int count = *(uint32_t*)hdata;
5263 uint64_t *hdev = hdata + 8;
5264 uint64_t *gdev = argptr + 8;
5265 int i;
5266
5267 *(uint32_t*)argptr = tswap32(count);
5268 for (i = 0; i < count; i++) {
5269 *gdev = tswap64(*hdev);
5270 gdev++;
5271 hdev++;
5272 }
5273 break;
5274 }
5275 case DM_LIST_VERSIONS:
5276 {
5277 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
5278 uint32_t remaining_data = guest_data_size;
5279 void *cur_data = argptr;
5280 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
5281 int vers_size = thunk_type_size(arg_type, 0);
5282
5283 while (1) {
5284 uint32_t next = vers->next;
5285 if (next) {
5286 vers->next = vers_size + (strlen(vers->name) + 1);
5287 }
5288 if (remaining_data < vers->next) {
5289 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5290 break;
5291 }
5292 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
5293 strcpy(cur_data + vers_size, vers->name);
5294 cur_data += vers->next;
5295 remaining_data -= vers->next;
5296 if (!next) {
5297 break;
5298 }
5299 vers = (void*)vers + next;
5300 }
5301 break;
5302 }
5303 default:
5304 unlock_user(argptr, guest_data, 0);
5305 ret = -TARGET_EINVAL;
5306 goto out;
5307 }
5308 unlock_user(argptr, guest_data, guest_data_size);
5309
5310 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5311 if (!argptr) {
5312 ret = -TARGET_EFAULT;
5313 goto out;
5314 }
5315 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5316 unlock_user(argptr, arg, target_size);
5317 }
5318 out:
5319 g_free(big_buf);
5320 return ret;
5321 }
5322
5323 static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
5324 int cmd, abi_long arg)
5325 {
5326 void *argptr;
5327 int target_size;
5328 const argtype *arg_type = ie->arg_type;
5329 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
5330 abi_long ret;
5331
5332 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
5333 struct blkpg_partition host_part;
5334
5335 /* Read and convert blkpg */
5336 arg_type++;
5337 target_size = thunk_type_size(arg_type, 0);
5338 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5339 if (!argptr) {
5340 ret = -TARGET_EFAULT;
5341 goto out;
5342 }
5343 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5344 unlock_user(argptr, arg, 0);
5345
5346 switch (host_blkpg->op) {
5347 case BLKPG_ADD_PARTITION:
5348 case BLKPG_DEL_PARTITION:
5349 /* payload is struct blkpg_partition */
5350 break;
5351 default:
5352 /* Unknown opcode */
5353 ret = -TARGET_EINVAL;
5354 goto out;
5355 }
5356
5357 /* Read and convert blkpg->data */
5358 arg = (abi_long)(uintptr_t)host_blkpg->data;
5359 target_size = thunk_type_size(part_arg_type, 0);
5360 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5361 if (!argptr) {
5362 ret = -TARGET_EFAULT;
5363 goto out;
5364 }
5365 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
5366 unlock_user(argptr, arg, 0);
5367
5368 /* Swizzle the data pointer to our local copy and call! */
5369 host_blkpg->data = &host_part;
5370 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
5371
5372 out:
5373 return ret;
5374 }
5375
5376 static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
5377 int fd, int cmd, abi_long arg)
5378 {
5379 const argtype *arg_type = ie->arg_type;
5380 const StructEntry *se;
5381 const argtype *field_types;
5382 const int *dst_offsets, *src_offsets;
5383 int target_size;
5384 void *argptr;
5385 abi_ulong *target_rt_dev_ptr;
5386 unsigned long *host_rt_dev_ptr;
5387 abi_long ret;
5388 int i;
5389
5390 assert(ie->access == IOC_W);
5391 assert(*arg_type == TYPE_PTR);
5392 arg_type++;
5393 assert(*arg_type == TYPE_STRUCT);
5394 target_size = thunk_type_size(arg_type, 0);
5395 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5396 if (!argptr) {
5397 return -TARGET_EFAULT;
5398 }
5399 arg_type++;
5400 assert(*arg_type == (int)STRUCT_rtentry);
5401 se = struct_entries + *arg_type++;
5402 assert(se->convert[0] == NULL);
5403 /* convert struct here to be able to catch rt_dev string */
5404 field_types = se->field_types;
5405 dst_offsets = se->field_offsets[THUNK_HOST];
5406 src_offsets = se->field_offsets[THUNK_TARGET];
5407 for (i = 0; i < se->nb_fields; i++) {
5408 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
5409 assert(*field_types == TYPE_PTRVOID);
5410 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
5411 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
5412 if (*target_rt_dev_ptr != 0) {
5413 *host_rt_dev_ptr = (unsigned long)lock_user_string(
5414 tswapal(*target_rt_dev_ptr));
5415 if (!*host_rt_dev_ptr) {
5416 unlock_user(argptr, arg, 0);
5417 return -TARGET_EFAULT;
5418 }
5419 } else {
5420 *host_rt_dev_ptr = 0;
5421 }
5422 field_types++;
5423 continue;
5424 }
5425 field_types = thunk_convert(buf_temp + dst_offsets[i],
5426 argptr + src_offsets[i],
5427 field_types, THUNK_HOST);
5428 }
5429 unlock_user(argptr, arg, 0);
5430
5431 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
5432 if (*host_rt_dev_ptr != 0) {
5433 unlock_user((void *)*host_rt_dev_ptr,
5434 *target_rt_dev_ptr, 0);
5435 }
5436 return ret;
5437 }
5438
5439 static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
5440 int fd, int cmd, abi_long arg)
5441 {
5442 int sig = target_to_host_signal(arg);
5443 return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
5444 }
5445
5446 static IOCTLEntry ioctl_entries[] = {
5447 #define IOCTL(cmd, access, ...) \
5448 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5449 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5450 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
5451 #include "ioctls.h"
5452 { 0, 0, },
5453 };
5454
5455 /* ??? Implement proper locking for ioctls. */
5456 /* do_ioctl() Must return target values and target errnos. */
5457 static abi_long do_ioctl(int fd, int cmd, abi_long arg)
5458 {
5459 const IOCTLEntry *ie;
5460 const argtype *arg_type;
5461 abi_long ret;
5462 uint8_t buf_temp[MAX_STRUCT_SIZE];
5463 int target_size;
5464 void *argptr;
5465
5466 ie = ioctl_entries;
5467 for(;;) {
5468 if (ie->target_cmd == 0) {
5469 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
5470 return -TARGET_ENOSYS;
5471 }
5472 if (ie->target_cmd == cmd)
5473 break;
5474 ie++;
5475 }
5476 arg_type = ie->arg_type;
5477 #if defined(DEBUG)
5478 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
5479 #endif
5480 if (ie->do_ioctl) {
5481 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
5482 }
5483
5484 switch(arg_type[0]) {
5485 case TYPE_NULL:
5486 /* no argument */
5487 ret = get_errno(safe_ioctl(fd, ie->host_cmd));
5488 break;
5489 case TYPE_PTRVOID:
5490 case TYPE_INT:
5491 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
5492 break;
5493 case TYPE_PTR:
5494 arg_type++;
5495 target_size = thunk_type_size(arg_type, 0);
5496 switch(ie->access) {
5497 case IOC_R:
5498 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
5499 if (!is_error(ret)) {
5500 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5501 if (!argptr)
5502 return -TARGET_EFAULT;
5503 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5504 unlock_user(argptr, arg, target_size);
5505 }
5506 break;
5507 case IOC_W:
5508 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5509 if (!argptr)
5510 return -TARGET_EFAULT;
5511 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5512 unlock_user(argptr, arg, 0);
5513 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
5514 break;
5515 default:
5516 case IOC_RW:
5517 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5518 if (!argptr)
5519 return -TARGET_EFAULT;
5520 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5521 unlock_user(argptr, arg, 0);
5522 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
5523 if (!is_error(ret)) {
5524 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5525 if (!argptr)
5526 return -TARGET_EFAULT;
5527 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5528 unlock_user(argptr, arg, target_size);
5529 }
5530 break;
5531 }
5532 break;
5533 default:
5534 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5535 (long)cmd, arg_type[0]);
5536 ret = -TARGET_ENOSYS;
5537 break;
5538 }
5539 return ret;
5540 }
5541
5542 static const bitmask_transtbl iflag_tbl[] = {
5543 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
5544 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
5545 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
5546 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
5547 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
5548 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
5549 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
5550 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
5551 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
5552 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
5553 { TARGET_IXON, TARGET_IXON, IXON, IXON },
5554 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
5555 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
5556 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
5557 { 0, 0, 0, 0 }
5558 };
5559
5560 static const bitmask_transtbl oflag_tbl[] = {
5561 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
5562 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
5563 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
5564 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
5565 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
5566 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
5567 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
5568 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
5569 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
5570 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
5571 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
5572 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
5573 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
5574 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
5575 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
5576 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
5577 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
5578 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
5579 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
5580 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
5581 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
5582 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
5583 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
5584 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
5585 { 0, 0, 0, 0 }
5586 };
5587
5588 static const bitmask_transtbl cflag_tbl[] = {
5589 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
5590 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
5591 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
5592 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
5593 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
5594 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
5595 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
5596 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
5597 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
5598 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
5599 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
5600 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
5601 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
5602 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
5603 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
5604 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
5605 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
5606 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
5607 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
5608 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
5609 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
5610 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
5611 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
5612 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
5613 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
5614 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
5615 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
5616 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
5617 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
5618 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
5619 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
5620 { 0, 0, 0, 0 }
5621 };
5622
5623 static const bitmask_transtbl lflag_tbl[] = {
5624 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
5625 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
5626 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
5627 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
5628 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
5629 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
5630 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
5631 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
5632 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
5633 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
5634 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
5635 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
5636 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
5637 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
5638 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
5639 { 0, 0, 0, 0 }
5640 };
5641
5642 static void target_to_host_termios (void *dst, const void *src)
5643 {
5644 struct host_termios *host = dst;
5645 const struct target_termios *target = src;
5646
5647 host->c_iflag =
5648 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
5649 host->c_oflag =
5650 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
5651 host->c_cflag =
5652 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
5653 host->c_lflag =
5654 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
5655 host->c_line = target->c_line;
5656
5657 memset(host->c_cc, 0, sizeof(host->c_cc));
5658 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
5659 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
5660 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
5661 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
5662 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
5663 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
5664 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
5665 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
5666 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
5667 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
5668 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
5669 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
5670 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
5671 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
5672 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
5673 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
5674 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
5675 }
5676
5677 static void host_to_target_termios (void *dst, const void *src)
5678 {
5679 struct target_termios *target = dst;
5680 const struct host_termios *host = src;
5681
5682 target->c_iflag =
5683 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
5684 target->c_oflag =
5685 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
5686 target->c_cflag =
5687 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
5688 target->c_lflag =
5689 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
5690 target->c_line = host->c_line;
5691
5692 memset(target->c_cc, 0, sizeof(target->c_cc));
5693 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
5694 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
5695 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
5696 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
5697 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
5698 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
5699 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
5700 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
5701 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
5702 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
5703 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
5704 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
5705 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
5706 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
5707 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
5708 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
5709 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
5710 }
5711
5712 static const StructEntry struct_termios_def = {
5713 .convert = { host_to_target_termios, target_to_host_termios },
5714 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
5715 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
5716 };
5717
5718 static bitmask_transtbl mmap_flags_tbl[] = {
5719 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
5720 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
5721 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
5722 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
5723 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
5724 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
5725 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
5726 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
5727 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, MAP_NORESERVE,
5728 MAP_NORESERVE },
5729 { 0, 0, 0, 0 }
5730 };
5731
5732 #if defined(TARGET_I386)
5733
5734 /* NOTE: there is really one LDT for all the threads */
5735 static uint8_t *ldt_table;
5736
5737 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
5738 {
5739 int size;
5740 void *p;
5741
5742 if (!ldt_table)
5743 return 0;
5744 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
5745 if (size > bytecount)
5746 size = bytecount;
5747 p = lock_user(VERIFY_WRITE, ptr, size, 0);
5748 if (!p)
5749 return -TARGET_EFAULT;
5750 /* ??? Should this by byteswapped? */
5751 memcpy(p, ldt_table, size);
5752 unlock_user(p, ptr, size);
5753 return size;
5754 }
5755
5756 /* XXX: add locking support */
5757 static abi_long write_ldt(CPUX86State *env,
5758 abi_ulong ptr, unsigned long bytecount, int oldmode)
5759 {
5760 struct target_modify_ldt_ldt_s ldt_info;
5761 struct target_modify_ldt_ldt_s *target_ldt_info;
5762 int seg_32bit, contents, read_exec_only, limit_in_pages;
5763 int seg_not_present, useable, lm;
5764 uint32_t *lp, entry_1, entry_2;
5765
5766 if (bytecount != sizeof(ldt_info))
5767 return -TARGET_EINVAL;
5768 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
5769 return -TARGET_EFAULT;
5770 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
5771 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
5772 ldt_info.limit = tswap32(target_ldt_info->limit);
5773 ldt_info.flags = tswap32(target_ldt_info->flags);
5774 unlock_user_struct(target_ldt_info, ptr, 0);
5775
5776 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
5777 return -TARGET_EINVAL;
5778 seg_32bit = ldt_info.flags & 1;
5779 contents = (ldt_info.flags >> 1) & 3;
5780 read_exec_only = (ldt_info.flags >> 3) & 1;
5781 limit_in_pages = (ldt_info.flags >> 4) & 1;
5782 seg_not_present = (ldt_info.flags >> 5) & 1;
5783 useable = (ldt_info.flags >> 6) & 1;
5784 #ifdef TARGET_ABI32
5785 lm = 0;
5786 #else
5787 lm = (ldt_info.flags >> 7) & 1;
5788 #endif
5789 if (contents == 3) {
5790 if (oldmode)
5791 return -TARGET_EINVAL;
5792 if (seg_not_present == 0)
5793 return -TARGET_EINVAL;
5794 }
5795 /* allocate the LDT */
5796 if (!ldt_table) {
5797 env->ldt.base = target_mmap(0,
5798 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
5799 PROT_READ|PROT_WRITE,
5800 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
5801 if (env->ldt.base == -1)
5802 return -TARGET_ENOMEM;
5803 memset(g2h(env->ldt.base), 0,
5804 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
5805 env->ldt.limit = 0xffff;
5806 ldt_table = g2h(env->ldt.base);
5807 }
5808
5809 /* NOTE: same code as Linux kernel */
5810 /* Allow LDTs to be cleared by the user. */
5811 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
5812 if (oldmode ||
5813 (contents == 0 &&
5814 read_exec_only == 1 &&
5815 seg_32bit == 0 &&
5816 limit_in_pages == 0 &&
5817 seg_not_present == 1 &&
5818 useable == 0 )) {
5819 entry_1 = 0;
5820 entry_2 = 0;
5821 goto install;
5822 }
5823 }
5824
5825 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
5826 (ldt_info.limit & 0x0ffff);
5827 entry_2 = (ldt_info.base_addr & 0xff000000) |
5828 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
5829 (ldt_info.limit & 0xf0000) |
5830 ((read_exec_only ^ 1) << 9) |
5831 (contents << 10) |
5832 ((seg_not_present ^ 1) << 15) |
5833 (seg_32bit << 22) |
5834 (limit_in_pages << 23) |
5835 (lm << 21) |
5836 0x7000;
5837 if (!oldmode)
5838 entry_2 |= (useable << 20);
5839
5840 /* Install the new entry ... */
5841 install:
5842 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
5843 lp[0] = tswap32(entry_1);
5844 lp[1] = tswap32(entry_2);
5845 return 0;
5846 }
5847
5848 /* specific and weird i386 syscalls */
5849 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
5850 unsigned long bytecount)
5851 {
5852 abi_long ret;
5853
5854 switch (func) {
5855 case 0:
5856 ret = read_ldt(ptr, bytecount);
5857 break;
5858 case 1:
5859 ret = write_ldt(env, ptr, bytecount, 1);
5860 break;
5861 case 0x11:
5862 ret = write_ldt(env, ptr, bytecount, 0);
5863 break;
5864 default:
5865 ret = -TARGET_ENOSYS;
5866 break;
5867 }
5868 return ret;
5869 }
5870
5871 #if defined(TARGET_I386) && defined(TARGET_ABI32)
5872 abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
5873 {
5874 uint64_t *gdt_table = g2h(env->gdt.base);
5875 struct target_modify_ldt_ldt_s ldt_info;
5876 struct target_modify_ldt_ldt_s *target_ldt_info;
5877 int seg_32bit, contents, read_exec_only, limit_in_pages;
5878 int seg_not_present, useable, lm;
5879 uint32_t *lp, entry_1, entry_2;
5880 int i;
5881
5882 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
5883 if (!target_ldt_info)
5884 return -TARGET_EFAULT;
5885 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
5886 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
5887 ldt_info.limit = tswap32(target_ldt_info->limit);
5888 ldt_info.flags = tswap32(target_ldt_info->flags);
5889 if (ldt_info.entry_number == -1) {
5890 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
5891 if (gdt_table[i] == 0) {
5892 ldt_info.entry_number = i;
5893 target_ldt_info->entry_number = tswap32(i);
5894 break;
5895 }
5896 }
5897 }
5898 unlock_user_struct(target_ldt_info, ptr, 1);
5899
5900 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
5901 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
5902 return -TARGET_EINVAL;
5903 seg_32bit = ldt_info.flags & 1;
5904 contents = (ldt_info.flags >> 1) & 3;
5905 read_exec_only = (ldt_info.flags >> 3) & 1;
5906 limit_in_pages = (ldt_info.flags >> 4) & 1;
5907 seg_not_present = (ldt_info.flags >> 5) & 1;
5908 useable = (ldt_info.flags >> 6) & 1;
5909 #ifdef TARGET_ABI32
5910 lm = 0;
5911 #else
5912 lm = (ldt_info.flags >> 7) & 1;
5913 #endif
5914
5915 if (contents == 3) {
5916 if (seg_not_present == 0)
5917 return -TARGET_EINVAL;
5918 }
5919
5920 /* NOTE: same code as Linux kernel */
5921 /* Allow LDTs to be cleared by the user. */
5922 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
5923 if ((contents == 0 &&
5924 read_exec_only == 1 &&
5925 seg_32bit == 0 &&
5926 limit_in_pages == 0 &&
5927 seg_not_present == 1 &&
5928 useable == 0 )) {
5929 entry_1 = 0;
5930 entry_2 = 0;
5931 goto install;
5932 }
5933 }
5934
5935 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
5936 (ldt_info.limit & 0x0ffff);
5937 entry_2 = (ldt_info.base_addr & 0xff000000) |
5938 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
5939 (ldt_info.limit & 0xf0000) |
5940 ((read_exec_only ^ 1) << 9) |
5941 (contents << 10) |
5942 ((seg_not_present ^ 1) << 15) |
5943 (seg_32bit << 22) |
5944 (limit_in_pages << 23) |
5945 (useable << 20) |
5946 (lm << 21) |
5947 0x7000;
5948
5949 /* Install the new entry ... */
5950 install:
5951 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
5952 lp[0] = tswap32(entry_1);
5953 lp[1] = tswap32(entry_2);
5954 return 0;
5955 }
5956
5957 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
5958 {
5959 struct target_modify_ldt_ldt_s *target_ldt_info;
5960 uint64_t *gdt_table = g2h(env->gdt.base);
5961 uint32_t base_addr, limit, flags;
5962 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
5963 int seg_not_present, useable, lm;
5964 uint32_t *lp, entry_1, entry_2;
5965
5966 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
5967 if (!target_ldt_info)
5968 return -TARGET_EFAULT;
5969 idx = tswap32(target_ldt_info->entry_number);
5970 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
5971 idx > TARGET_GDT_ENTRY_TLS_MAX) {
5972 unlock_user_struct(target_ldt_info, ptr, 1);
5973 return -TARGET_EINVAL;
5974 }
5975 lp = (uint32_t *)(gdt_table + idx);
5976 entry_1 = tswap32(lp[0]);
5977 entry_2 = tswap32(lp[1]);
5978
5979 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
5980 contents = (entry_2 >> 10) & 3;
5981 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
5982 seg_32bit = (entry_2 >> 22) & 1;
5983 limit_in_pages = (entry_2 >> 23) & 1;
5984 useable = (entry_2 >> 20) & 1;
5985 #ifdef TARGET_ABI32
5986 lm = 0;
5987 #else
5988 lm = (entry_2 >> 21) & 1;
5989 #endif
5990 flags = (seg_32bit << 0) | (contents << 1) |
5991 (read_exec_only << 3) | (limit_in_pages << 4) |
5992 (seg_not_present << 5) | (useable << 6) | (lm << 7);
5993 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
5994 base_addr = (entry_1 >> 16) |
5995 (entry_2 & 0xff000000) |
5996 ((entry_2 & 0xff) << 16);
5997 target_ldt_info->base_addr = tswapal(base_addr);
5998 target_ldt_info->limit = tswap32(limit);
5999 target_ldt_info->flags = tswap32(flags);
6000 unlock_user_struct(target_ldt_info, ptr, 1);
6001 return 0;
6002 }
6003 #endif /* TARGET_I386 && TARGET_ABI32 */
6004
6005 #ifndef TARGET_ABI32
6006 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
6007 {
6008 abi_long ret = 0;
6009 abi_ulong val;
6010 int idx;
6011
6012 switch(code) {
6013 case TARGET_ARCH_SET_GS:
6014 case TARGET_ARCH_SET_FS:
6015 if (code == TARGET_ARCH_SET_GS)
6016 idx = R_GS;
6017 else
6018 idx = R_FS;
6019 cpu_x86_load_seg(env, idx, 0);
6020 env->segs[idx].base = addr;
6021 break;
6022 case TARGET_ARCH_GET_GS:
6023 case TARGET_ARCH_GET_FS:
6024 if (code == TARGET_ARCH_GET_GS)
6025 idx = R_GS;
6026 else
6027 idx = R_FS;
6028 val = env->segs[idx].base;
6029 if (put_user(val, addr, abi_ulong))
6030 ret = -TARGET_EFAULT;
6031 break;
6032 default:
6033 ret = -TARGET_EINVAL;
6034 break;
6035 }
6036 return ret;
6037 }
6038 #endif
6039
6040 #endif /* defined(TARGET_I386) */
6041
6042 #define NEW_STACK_SIZE 0x40000
6043
6044
6045 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
6046 typedef struct {
6047 CPUArchState *env;
6048 pthread_mutex_t mutex;
6049 pthread_cond_t cond;
6050 pthread_t thread;
6051 uint32_t tid;
6052 abi_ulong child_tidptr;
6053 abi_ulong parent_tidptr;
6054 sigset_t sigmask;
6055 } new_thread_info;
6056
6057 static void *clone_func(void *arg)
6058 {
6059 new_thread_info *info = arg;
6060 CPUArchState *env;
6061 CPUState *cpu;
6062 TaskState *ts;
6063
6064 rcu_register_thread();
6065 env = info->env;
6066 cpu = ENV_GET_CPU(env);
6067 thread_cpu = cpu;
6068 ts = (TaskState *)cpu->opaque;
6069 info->tid = gettid();
6070 cpu->host_tid = info->tid;
6071 task_settid(ts);
6072 if (info->child_tidptr)
6073 put_user_u32(info->tid, info->child_tidptr);
6074 if (info->parent_tidptr)
6075 put_user_u32(info->tid, info->parent_tidptr);
6076 /* Enable signals. */
6077 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
6078 /* Signal to the parent that we're ready. */
6079 pthread_mutex_lock(&info->mutex);
6080 pthread_cond_broadcast(&info->cond);
6081 pthread_mutex_unlock(&info->mutex);
6082 /* Wait until the parent has finshed initializing the tls state. */
6083 pthread_mutex_lock(&clone_lock);
6084 pthread_mutex_unlock(&clone_lock);
6085 cpu_loop(env);
6086 /* never exits */
6087 return NULL;
6088 }
6089
6090 /* do_fork() Must return host values and target errnos (unlike most
6091 do_*() functions). */
6092 static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
6093 abi_ulong parent_tidptr, target_ulong newtls,
6094 abi_ulong child_tidptr)
6095 {
6096 CPUState *cpu = ENV_GET_CPU(env);
6097 int ret;
6098 TaskState *ts;
6099 CPUState *new_cpu;
6100 CPUArchState *new_env;
6101 sigset_t sigmask;
6102
6103 flags &= ~CLONE_IGNORED_FLAGS;
6104
6105 /* Emulate vfork() with fork() */
6106 if (flags & CLONE_VFORK)
6107 flags &= ~(CLONE_VFORK | CLONE_VM);
6108
6109 if (flags & CLONE_VM) {
6110 TaskState *parent_ts = (TaskState *)cpu->opaque;
6111 new_thread_info info;
6112 pthread_attr_t attr;
6113
6114 if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
6115 (flags & CLONE_INVALID_THREAD_FLAGS)) {
6116 return -TARGET_EINVAL;
6117 }
6118
6119 ts = g_new0(TaskState, 1);
6120 init_task_state(ts);
6121 /* we create a new CPU instance. */
6122 new_env = cpu_copy(env);
6123 /* Init regs that differ from the parent. */
6124 cpu_clone_regs(new_env, newsp);
6125 new_cpu = ENV_GET_CPU(new_env);
6126 new_cpu->opaque = ts;
6127 ts->bprm = parent_ts->bprm;
6128 ts->info = parent_ts->info;
6129 ts->signal_mask = parent_ts->signal_mask;
6130
6131 if (flags & CLONE_CHILD_CLEARTID) {
6132 ts->child_tidptr = child_tidptr;
6133 }
6134
6135 if (flags & CLONE_SETTLS) {
6136 cpu_set_tls (new_env, newtls);
6137 }
6138
6139 /* Grab a mutex so that thread setup appears atomic. */
6140 pthread_mutex_lock(&clone_lock);
6141
6142 memset(&info, 0, sizeof(info));
6143 pthread_mutex_init(&info.mutex, NULL);
6144 pthread_mutex_lock(&info.mutex);
6145 pthread_cond_init(&info.cond, NULL);
6146 info.env = new_env;
6147 if (flags & CLONE_CHILD_SETTID) {
6148 info.child_tidptr = child_tidptr;
6149 }
6150 if (flags & CLONE_PARENT_SETTID) {
6151 info.parent_tidptr = parent_tidptr;
6152 }
6153
6154 ret = pthread_attr_init(&attr);
6155 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
6156 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
6157 /* It is not safe to deliver signals until the child has finished
6158 initializing, so temporarily block all signals. */
6159 sigfillset(&sigmask);
6160 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
6161
6162 ret = pthread_create(&info.thread, &attr, clone_func, &info);
6163 /* TODO: Free new CPU state if thread creation failed. */
6164
6165 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
6166 pthread_attr_destroy(&attr);
6167 if (ret == 0) {
6168 /* Wait for the child to initialize. */
6169 pthread_cond_wait(&info.cond, &info.mutex);
6170 ret = info.tid;
6171 } else {
6172 ret = -1;
6173 }
6174 pthread_mutex_unlock(&info.mutex);
6175 pthread_cond_destroy(&info.cond);
6176 pthread_mutex_destroy(&info.mutex);
6177 pthread_mutex_unlock(&clone_lock);
6178 } else {
6179 /* if no CLONE_VM, we consider it is a fork */
6180 if (flags & CLONE_INVALID_FORK_FLAGS) {
6181 return -TARGET_EINVAL;
6182 }
6183
6184 /* We can't support custom termination signals */
6185 if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
6186 return -TARGET_EINVAL;
6187 }
6188
6189 if (block_signals()) {
6190 return -TARGET_ERESTARTSYS;
6191 }
6192
6193 fork_start();
6194 ret = fork();
6195 if (ret == 0) {
6196 /* Child Process. */
6197 rcu_after_fork();
6198 cpu_clone_regs(env, newsp);
6199 fork_end(1);
6200 /* There is a race condition here. The parent process could
6201 theoretically read the TID in the child process before the child
6202 tid is set. This would require using either ptrace
6203 (not implemented) or having *_tidptr to point at a shared memory
6204 mapping. We can't repeat the spinlock hack used above because
6205 the child process gets its own copy of the lock. */
6206 if (flags & CLONE_CHILD_SETTID)
6207 put_user_u32(gettid(), child_tidptr);
6208 if (flags & CLONE_PARENT_SETTID)
6209 put_user_u32(gettid(), parent_tidptr);
6210 ts = (TaskState *)cpu->opaque;
6211 if (flags & CLONE_SETTLS)
6212 cpu_set_tls (env, newtls);
6213 if (flags & CLONE_CHILD_CLEARTID)
6214 ts->child_tidptr = child_tidptr;
6215 } else {
6216 fork_end(0);
6217 }
6218 }
6219 return ret;
6220 }
6221
6222 /* warning : doesn't handle linux specific flags... */
6223 static int target_to_host_fcntl_cmd(int cmd)
6224 {
6225 switch(cmd) {
6226 case TARGET_F_DUPFD:
6227 case TARGET_F_GETFD:
6228 case TARGET_F_SETFD:
6229 case TARGET_F_GETFL:
6230 case TARGET_F_SETFL:
6231 return cmd;
6232 case TARGET_F_GETLK:
6233 return F_GETLK64;
6234 case TARGET_F_SETLK:
6235 return F_SETLK64;
6236 case TARGET_F_SETLKW:
6237 return F_SETLKW64;
6238 case TARGET_F_GETOWN:
6239 return F_GETOWN;
6240 case TARGET_F_SETOWN:
6241 return F_SETOWN;
6242 case TARGET_F_GETSIG:
6243 return F_GETSIG;
6244 case TARGET_F_SETSIG:
6245 return F_SETSIG;
6246 #if TARGET_ABI_BITS == 32
6247 case TARGET_F_GETLK64:
6248 return F_GETLK64;
6249 case TARGET_F_SETLK64:
6250 return F_SETLK64;
6251 case TARGET_F_SETLKW64:
6252 return F_SETLKW64;
6253 #endif
6254 case TARGET_F_SETLEASE:
6255 return F_SETLEASE;
6256 case TARGET_F_GETLEASE:
6257 return F_GETLEASE;
6258 #ifdef F_DUPFD_CLOEXEC
6259 case TARGET_F_DUPFD_CLOEXEC:
6260 return F_DUPFD_CLOEXEC;
6261 #endif
6262 case TARGET_F_NOTIFY:
6263 return F_NOTIFY;
6264 #ifdef F_GETOWN_EX
6265 case TARGET_F_GETOWN_EX:
6266 return F_GETOWN_EX;
6267 #endif
6268 #ifdef F_SETOWN_EX
6269 case TARGET_F_SETOWN_EX:
6270 return F_SETOWN_EX;
6271 #endif
6272 #ifdef F_SETPIPE_SZ
6273 case TARGET_F_SETPIPE_SZ:
6274 return F_SETPIPE_SZ;
6275 case TARGET_F_GETPIPE_SZ:
6276 return F_GETPIPE_SZ;
6277 #endif
6278 default:
6279 return -TARGET_EINVAL;
6280 }
6281 return -TARGET_EINVAL;
6282 }
6283
6284 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
6285 static const bitmask_transtbl flock_tbl[] = {
6286 TRANSTBL_CONVERT(F_RDLCK),
6287 TRANSTBL_CONVERT(F_WRLCK),
6288 TRANSTBL_CONVERT(F_UNLCK),
6289 TRANSTBL_CONVERT(F_EXLCK),
6290 TRANSTBL_CONVERT(F_SHLCK),
6291 { 0, 0, 0, 0 }
6292 };
6293
6294 static inline abi_long copy_from_user_flock(struct flock64 *fl,
6295 abi_ulong target_flock_addr)
6296 {
6297 struct target_flock *target_fl;
6298 short l_type;
6299
6300 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6301 return -TARGET_EFAULT;
6302 }
6303
6304 __get_user(l_type, &target_fl->l_type);
6305 fl->l_type = target_to_host_bitmask(l_type, flock_tbl);
6306 __get_user(fl->l_whence, &target_fl->l_whence);
6307 __get_user(fl->l_start, &target_fl->l_start);
6308 __get_user(fl->l_len, &target_fl->l_len);
6309 __get_user(fl->l_pid, &target_fl->l_pid);
6310 unlock_user_struct(target_fl, target_flock_addr, 0);
6311 return 0;
6312 }
6313
6314 static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
6315 const struct flock64 *fl)
6316 {
6317 struct target_flock *target_fl;
6318 short l_type;
6319
6320 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6321 return -TARGET_EFAULT;
6322 }
6323
6324 l_type = host_to_target_bitmask(fl->l_type, flock_tbl);
6325 __put_user(l_type, &target_fl->l_type);
6326 __put_user(fl->l_whence, &target_fl->l_whence);
6327 __put_user(fl->l_start, &target_fl->l_start);
6328 __put_user(fl->l_len, &target_fl->l_len);
6329 __put_user(fl->l_pid, &target_fl->l_pid);
6330 unlock_user_struct(target_fl, target_flock_addr, 1);
6331 return 0;
6332 }
6333
6334 typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
6335 typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
6336
6337 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6338 static inline abi_long copy_from_user_eabi_flock64(struct flock64 *fl,
6339 abi_ulong target_flock_addr)
6340 {
6341 struct target_eabi_flock64 *target_fl;
6342 short l_type;
6343
6344 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6345 return -TARGET_EFAULT;
6346 }
6347
6348 __get_user(l_type, &target_fl->l_type);
6349 fl->l_type = target_to_host_bitmask(l_type, flock_tbl);
6350 __get_user(fl->l_whence, &target_fl->l_whence);
6351 __get_user(fl->l_start, &target_fl->l_start);
6352 __get_user(fl->l_len, &target_fl->l_len);
6353 __get_user(fl->l_pid, &target_fl->l_pid);
6354 unlock_user_struct(target_fl, target_flock_addr, 0);
6355 return 0;
6356 }
6357
6358 static inline abi_long copy_to_user_eabi_flock64(abi_ulong target_flock_addr,
6359 const struct flock64 *fl)
6360 {
6361 struct target_eabi_flock64 *target_fl;
6362 short l_type;
6363
6364 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6365 return -TARGET_EFAULT;
6366 }
6367
6368 l_type = host_to_target_bitmask(fl->l_type, flock_tbl);
6369 __put_user(l_type, &target_fl->l_type);
6370 __put_user(fl->l_whence, &target_fl->l_whence);
6371 __put_user(fl->l_start, &target_fl->l_start);
6372 __put_user(fl->l_len, &target_fl->l_len);
6373 __put_user(fl->l_pid, &target_fl->l_pid);
6374 unlock_user_struct(target_fl, target_flock_addr, 1);
6375 return 0;
6376 }
6377 #endif
6378
6379 static inline abi_long copy_from_user_flock64(struct flock64 *fl,
6380 abi_ulong target_flock_addr)
6381 {
6382 struct target_flock64 *target_fl;
6383 short l_type;
6384
6385 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6386 return -TARGET_EFAULT;
6387 }
6388
6389 __get_user(l_type, &target_fl->l_type);
6390 fl->l_type = target_to_host_bitmask(l_type, flock_tbl);
6391 __get_user(fl->l_whence, &target_fl->l_whence);
6392 __get_user(fl->l_start, &target_fl->l_start);
6393 __get_user(fl->l_len, &target_fl->l_len);
6394 __get_user(fl->l_pid, &target_fl->l_pid);
6395 unlock_user_struct(target_fl, target_flock_addr, 0);
6396 return 0;
6397 }
6398
6399 static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
6400 const struct flock64 *fl)
6401 {
6402 struct target_flock64 *target_fl;
6403 short l_type;
6404
6405 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6406 return -TARGET_EFAULT;
6407 }
6408
6409 l_type = host_to_target_bitmask(fl->l_type, flock_tbl);
6410 __put_user(l_type, &target_fl->l_type);
6411 __put_user(fl->l_whence, &target_fl->l_whence);
6412 __put_user(fl->l_start, &target_fl->l_start);
6413 __put_user(fl->l_len, &target_fl->l_len);
6414 __put_user(fl->l_pid, &target_fl->l_pid);
6415 unlock_user_struct(target_fl, target_flock_addr, 1);
6416 return 0;
6417 }
6418
6419 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
6420 {
6421 struct flock64 fl64;
6422 #ifdef F_GETOWN_EX
6423 struct f_owner_ex fox;
6424 struct target_f_owner_ex *target_fox;
6425 #endif
6426 abi_long ret;
6427 int host_cmd = target_to_host_fcntl_cmd(cmd);
6428
6429 if (host_cmd == -TARGET_EINVAL)
6430 return host_cmd;
6431
6432 switch(cmd) {
6433 case TARGET_F_GETLK:
6434 ret = copy_from_user_flock(&fl64, arg);
6435 if (ret) {
6436 return ret;
6437 }
6438 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
6439 if (ret == 0) {
6440 ret = copy_to_user_flock(arg, &fl64);
6441 }
6442 break;
6443
6444 case TARGET_F_SETLK:
6445 case TARGET_F_SETLKW:
6446 ret = copy_from_user_flock(&fl64, arg);
6447 if (ret) {
6448 return ret;
6449 }
6450 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
6451 break;
6452
6453 case TARGET_F_GETLK64:
6454 ret = copy_from_user_flock64(&fl64, arg);
6455 if (ret) {
6456 return ret;
6457 }
6458 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
6459 if (ret == 0) {
6460 ret = copy_to_user_flock64(arg, &fl64);
6461 }
6462 break;
6463 case TARGET_F_SETLK64:
6464 case TARGET_F_SETLKW64:
6465 ret = copy_from_user_flock64(&fl64, arg);
6466 if (ret) {
6467 return ret;
6468 }
6469 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
6470 break;
6471
6472 case TARGET_F_GETFL:
6473 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
6474 if (ret >= 0) {
6475 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
6476 }
6477 break;
6478
6479 case TARGET_F_SETFL:
6480 ret = get_errno(safe_fcntl(fd, host_cmd,
6481 target_to_host_bitmask(arg,
6482 fcntl_flags_tbl)));
6483 break;
6484
6485 #ifdef F_GETOWN_EX
6486 case TARGET_F_GETOWN_EX:
6487 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
6488 if (ret >= 0) {
6489 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
6490 return -TARGET_EFAULT;
6491 target_fox->type = tswap32(fox.type);
6492 target_fox->pid = tswap32(fox.pid);
6493 unlock_user_struct(target_fox, arg, 1);
6494 }
6495 break;
6496 #endif
6497
6498 #ifdef F_SETOWN_EX
6499 case TARGET_F_SETOWN_EX:
6500 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
6501 return -TARGET_EFAULT;
6502 fox.type = tswap32(target_fox->type);
6503 fox.pid = tswap32(target_fox->pid);
6504 unlock_user_struct(target_fox, arg, 0);
6505 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
6506 break;
6507 #endif
6508
6509 case TARGET_F_SETOWN:
6510 case TARGET_F_GETOWN:
6511 case TARGET_F_SETSIG:
6512 case TARGET_F_GETSIG:
6513 case TARGET_F_SETLEASE:
6514 case TARGET_F_GETLEASE:
6515 case TARGET_F_SETPIPE_SZ:
6516 case TARGET_F_GETPIPE_SZ:
6517 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
6518 break;
6519
6520 default:
6521 ret = get_errno(safe_fcntl(fd, cmd, arg));
6522 break;
6523 }
6524 return ret;
6525 }
6526
6527 #ifdef USE_UID16
6528
6529 static inline int high2lowuid(int uid)
6530 {
6531 if (uid > 65535)
6532 return 65534;
6533 else
6534 return uid;
6535 }
6536
6537 static inline int high2lowgid(int gid)
6538 {
6539 if (gid > 65535)
6540 return 65534;
6541 else
6542 return gid;
6543 }
6544
6545 static inline int low2highuid(int uid)
6546 {
6547 if ((int16_t)uid == -1)
6548 return -1;
6549 else
6550 return uid;
6551 }
6552
6553 static inline int low2highgid(int gid)
6554 {
6555 if ((int16_t)gid == -1)
6556 return -1;
6557 else
6558 return gid;
6559 }
6560 static inline int tswapid(int id)
6561 {
6562 return tswap16(id);
6563 }
6564
6565 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
6566
6567 #else /* !USE_UID16 */
6568 static inline int high2lowuid(int uid)
6569 {
6570 return uid;
6571 }
6572 static inline int high2lowgid(int gid)
6573 {
6574 return gid;
6575 }
6576 static inline int low2highuid(int uid)
6577 {
6578 return uid;
6579 }
6580 static inline int low2highgid(int gid)
6581 {
6582 return gid;
6583 }
6584 static inline int tswapid(int id)
6585 {
6586 return tswap32(id);
6587 }
6588
6589 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
6590
6591 #endif /* USE_UID16 */
6592
6593 /* We must do direct syscalls for setting UID/GID, because we want to
6594 * implement the Linux system call semantics of "change only for this thread",
6595 * not the libc/POSIX semantics of "change for all threads in process".
6596 * (See http://ewontfix.com/17/ for more details.)
6597 * We use the 32-bit version of the syscalls if present; if it is not
6598 * then either the host architecture supports 32-bit UIDs natively with
6599 * the standard syscall, or the 16-bit UID is the best we can do.
6600 */
6601 #ifdef __NR_setuid32
6602 #define __NR_sys_setuid __NR_setuid32
6603 #else
6604 #define __NR_sys_setuid __NR_setuid
6605 #endif
6606 #ifdef __NR_setgid32
6607 #define __NR_sys_setgid __NR_setgid32
6608 #else
6609 #define __NR_sys_setgid __NR_setgid
6610 #endif
6611 #ifdef __NR_setresuid32
6612 #define __NR_sys_setresuid __NR_setresuid32
6613 #else
6614 #define __NR_sys_setresuid __NR_setresuid
6615 #endif
6616 #ifdef __NR_setresgid32
6617 #define __NR_sys_setresgid __NR_setresgid32
6618 #else
6619 #define __NR_sys_setresgid __NR_setresgid
6620 #endif
6621
6622 _syscall1(int, sys_setuid, uid_t, uid)
6623 _syscall1(int, sys_setgid, gid_t, gid)
6624 _syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
6625 _syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
6626
6627 void syscall_init(void)
6628 {
6629 IOCTLEntry *ie;
6630 const argtype *arg_type;
6631 int size;
6632 int i;
6633
6634 thunk_init(STRUCT_MAX);
6635
6636 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
6637 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
6638 #include "syscall_types.h"
6639 #undef STRUCT
6640 #undef STRUCT_SPECIAL
6641
6642 /* Build target_to_host_errno_table[] table from
6643 * host_to_target_errno_table[]. */
6644 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
6645 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
6646 }
6647
6648 /* we patch the ioctl size if necessary. We rely on the fact that
6649 no ioctl has all the bits at '1' in the size field */
6650 ie = ioctl_entries;
6651 while (ie->target_cmd != 0) {
6652 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
6653 TARGET_IOC_SIZEMASK) {
6654 arg_type = ie->arg_type;
6655 if (arg_type[0] != TYPE_PTR) {
6656 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
6657 ie->target_cmd);
6658 exit(1);
6659 }
6660 arg_type++;
6661 size = thunk_type_size(arg_type, 0);
6662 ie->target_cmd = (ie->target_cmd &
6663 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
6664 (size << TARGET_IOC_SIZESHIFT);
6665 }
6666
6667 /* automatic consistency check if same arch */
6668 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6669 (defined(__x86_64__) && defined(TARGET_X86_64))
6670 if (unlikely(ie->target_cmd != ie->host_cmd)) {
6671 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
6672 ie->name, ie->target_cmd, ie->host_cmd);
6673 }
6674 #endif
6675 ie++;
6676 }
6677 }
6678
6679 #if TARGET_ABI_BITS == 32
6680 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
6681 {
6682 #ifdef TARGET_WORDS_BIGENDIAN
6683 return ((uint64_t)word0 << 32) | word1;
6684 #else
6685 return ((uint64_t)word1 << 32) | word0;
6686 #endif
6687 }
6688 #else /* TARGET_ABI_BITS == 32 */
6689 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
6690 {
6691 return word0;
6692 }
6693 #endif /* TARGET_ABI_BITS != 32 */
6694
6695 #ifdef TARGET_NR_truncate64
6696 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
6697 abi_long arg2,
6698 abi_long arg3,
6699 abi_long arg4)
6700 {
6701 if (regpairs_aligned(cpu_env)) {
6702 arg2 = arg3;
6703 arg3 = arg4;
6704 }
6705 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
6706 }
6707 #endif
6708
6709 #ifdef TARGET_NR_ftruncate64
6710 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
6711 abi_long arg2,
6712 abi_long arg3,
6713 abi_long arg4)
6714 {
6715 if (regpairs_aligned(cpu_env)) {
6716 arg2 = arg3;
6717 arg3 = arg4;
6718 }
6719 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
6720 }
6721 #endif
6722
6723 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
6724 abi_ulong target_addr)
6725 {
6726 struct target_timespec *target_ts;
6727
6728 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
6729 return -TARGET_EFAULT;
6730 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
6731 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
6732 unlock_user_struct(target_ts, target_addr, 0);
6733 return 0;
6734 }
6735
6736 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
6737 struct timespec *host_ts)
6738 {
6739 struct target_timespec *target_ts;
6740
6741 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
6742 return -TARGET_EFAULT;
6743 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
6744 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
6745 unlock_user_struct(target_ts, target_addr, 1);
6746 return 0;
6747 }
6748
6749 static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
6750 abi_ulong target_addr)
6751 {
6752 struct target_itimerspec *target_itspec;
6753
6754 if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
6755 return -TARGET_EFAULT;
6756 }
6757
6758 host_itspec->it_interval.tv_sec =
6759 tswapal(target_itspec->it_interval.tv_sec);
6760 host_itspec->it_interval.tv_nsec =
6761 tswapal(target_itspec->it_interval.tv_nsec);
6762 host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
6763 host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
6764
6765 unlock_user_struct(target_itspec, target_addr, 1);
6766 return 0;
6767 }
6768
6769 static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
6770 struct itimerspec *host_its)
6771 {
6772 struct target_itimerspec *target_itspec;
6773
6774 if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
6775 return -TARGET_EFAULT;
6776 }
6777
6778 target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
6779 target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
6780
6781 target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
6782 target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
6783
6784 unlock_user_struct(target_itspec, target_addr, 0);
6785 return 0;
6786 }
6787
6788 static inline abi_long target_to_host_timex(struct timex *host_tx,
6789 abi_long target_addr)
6790 {
6791 struct target_timex *target_tx;
6792
6793 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
6794 return -TARGET_EFAULT;
6795 }
6796
6797 __get_user(host_tx->modes, &target_tx->modes);
6798 __get_user(host_tx->offset, &target_tx->offset);
6799 __get_user(host_tx->freq, &target_tx->freq);
6800 __get_user(host_tx->maxerror, &target_tx->maxerror);
6801 __get_user(host_tx->esterror, &target_tx->esterror);
6802 __get_user(host_tx->status, &target_tx->status);
6803 __get_user(host_tx->constant, &target_tx->constant);
6804 __get_user(host_tx->precision, &target_tx->precision);
6805 __get_user(host_tx->tolerance, &target_tx->tolerance);
6806 __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
6807 __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
6808 __get_user(host_tx->tick, &target_tx->tick);
6809 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
6810 __get_user(host_tx->jitter, &target_tx->jitter);
6811 __get_user(host_tx->shift, &target_tx->shift);
6812 __get_user(host_tx->stabil, &target_tx->stabil);
6813 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
6814 __get_user(host_tx->calcnt, &target_tx->calcnt);
6815 __get_user(host_tx->errcnt, &target_tx->errcnt);
6816 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
6817 __get_user(host_tx->tai, &target_tx->tai);
6818
6819 unlock_user_struct(target_tx, target_addr, 0);
6820 return 0;
6821 }
6822
6823 static inline abi_long host_to_target_timex(abi_long target_addr,
6824 struct timex *host_tx)
6825 {
6826 struct target_timex *target_tx;
6827
6828 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
6829 return -TARGET_EFAULT;
6830 }
6831
6832 __put_user(host_tx->modes, &target_tx->modes);
6833 __put_user(host_tx->offset, &target_tx->offset);
6834 __put_user(host_tx->freq, &target_tx->freq);
6835 __put_user(host_tx->maxerror, &target_tx->maxerror);
6836 __put_user(host_tx->esterror, &target_tx->esterror);
6837 __put_user(host_tx->status, &target_tx->status);
6838 __put_user(host_tx->constant, &target_tx->constant);
6839 __put_user(host_tx->precision, &target_tx->precision);
6840 __put_user(host_tx->tolerance, &target_tx->tolerance);
6841 __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
6842 __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
6843 __put_user(host_tx->tick, &target_tx->tick);
6844 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
6845 __put_user(host_tx->jitter, &target_tx->jitter);
6846 __put_user(host_tx->shift, &target_tx->shift);
6847 __put_user(host_tx->stabil, &target_tx->stabil);
6848 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
6849 __put_user(host_tx->calcnt, &target_tx->calcnt);
6850 __put_user(host_tx->errcnt, &target_tx->errcnt);
6851 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
6852 __put_user(host_tx->tai, &target_tx->tai);
6853
6854 unlock_user_struct(target_tx, target_addr, 1);
6855 return 0;
6856 }
6857
6858
6859 static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
6860 abi_ulong target_addr)
6861 {
6862 struct target_sigevent *target_sevp;
6863
6864 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
6865 return -TARGET_EFAULT;
6866 }
6867
6868 /* This union is awkward on 64 bit systems because it has a 32 bit
6869 * integer and a pointer in it; we follow the conversion approach
6870 * used for handling sigval types in signal.c so the guest should get
6871 * the correct value back even if we did a 64 bit byteswap and it's
6872 * using the 32 bit integer.
6873 */
6874 host_sevp->sigev_value.sival_ptr =
6875 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
6876 host_sevp->sigev_signo =
6877 target_to_host_signal(tswap32(target_sevp->sigev_signo));
6878 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
6879 host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
6880
6881 unlock_user_struct(target_sevp, target_addr, 1);
6882 return 0;
6883 }
6884
6885 #if defined(TARGET_NR_mlockall)
6886 static inline int target_to_host_mlockall_arg(int arg)
6887 {
6888 int result = 0;
6889
6890 if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
6891 result |= MCL_CURRENT;
6892 }
6893 if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
6894 result |= MCL_FUTURE;
6895 }
6896 return result;
6897 }
6898 #endif
6899
6900 static inline abi_long host_to_target_stat64(void *cpu_env,
6901 abi_ulong target_addr,
6902 struct stat *host_st)
6903 {
6904 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
6905 if (((CPUARMState *)cpu_env)->eabi) {
6906 struct target_eabi_stat64 *target_st;
6907
6908 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
6909 return -TARGET_EFAULT;
6910 memset(target_st, 0, sizeof(struct target_eabi_stat64));
6911 __put_user(host_st->st_dev, &target_st->st_dev);
6912 __put_user(host_st->st_ino, &target_st->st_ino);
6913 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
6914 __put_user(host_st->st_ino, &target_st->__st_ino);
6915 #endif
6916 __put_user(host_st->st_mode, &target_st->st_mode);
6917 __put_user(host_st->st_nlink, &target_st->st_nlink);
6918 __put_user(host_st->st_uid, &target_st->st_uid);
6919 __put_user(host_st->st_gid, &target_st->st_gid);
6920 __put_user(host_st->st_rdev, &target_st->st_rdev);
6921 __put_user(host_st->st_size, &target_st->st_size);
6922 __put_user(host_st->st_blksize, &target_st->st_blksize);
6923 __put_user(host_st->st_blocks, &target_st->st_blocks);
6924 __put_user(host_st->st_atime, &target_st->target_st_atime);
6925 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
6926 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
6927 unlock_user_struct(target_st, target_addr, 1);
6928 } else
6929 #endif
6930 {
6931 #if defined(TARGET_HAS_STRUCT_STAT64)
6932 struct target_stat64 *target_st;
6933 #else
6934 struct target_stat *target_st;
6935 #endif
6936
6937 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
6938 return -TARGET_EFAULT;
6939 memset(target_st, 0, sizeof(*target_st));
6940 __put_user(host_st->st_dev, &target_st->st_dev);
6941 __put_user(host_st->st_ino, &target_st->st_ino);
6942 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
6943 __put_user(host_st->st_ino, &target_st->__st_ino);
6944 #endif
6945 __put_user(host_st->st_mode, &target_st->st_mode);
6946 __put_user(host_st->st_nlink, &target_st->st_nlink);
6947 __put_user(host_st->st_uid, &target_st->st_uid);
6948 __put_user(host_st->st_gid, &target_st->st_gid);
6949 __put_user(host_st->st_rdev, &target_st->st_rdev);
6950 /* XXX: better use of kernel struct */
6951 __put_user(host_st->st_size, &target_st->st_size);
6952 __put_user(host_st->st_blksize, &target_st->st_blksize);
6953 __put_user(host_st->st_blocks, &target_st->st_blocks);
6954 __put_user(host_st->st_atime, &target_st->target_st_atime);
6955 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
6956 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
6957 unlock_user_struct(target_st, target_addr, 1);
6958 }
6959
6960 return 0;
6961 }
6962
6963 /* ??? Using host futex calls even when target atomic operations
6964 are not really atomic probably breaks things. However implementing
6965 futexes locally would make futexes shared between multiple processes
6966 tricky. However they're probably useless because guest atomic
6967 operations won't work either. */
6968 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
6969 target_ulong uaddr2, int val3)
6970 {
6971 struct timespec ts, *pts;
6972 int base_op;
6973
6974 /* ??? We assume FUTEX_* constants are the same on both host
6975 and target. */
6976 #ifdef FUTEX_CMD_MASK
6977 base_op = op & FUTEX_CMD_MASK;
6978 #else
6979 base_op = op;
6980 #endif
6981 switch (base_op) {
6982 case FUTEX_WAIT:
6983 case FUTEX_WAIT_BITSET:
6984 if (timeout) {
6985 pts = &ts;
6986 target_to_host_timespec(pts, timeout);
6987 } else {
6988 pts = NULL;
6989 }
6990 return get_errno(safe_futex(g2h(uaddr), op, tswap32(val),
6991 pts, NULL, val3));
6992 case FUTEX_WAKE:
6993 return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
6994 case FUTEX_FD:
6995 return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
6996 case FUTEX_REQUEUE:
6997 case FUTEX_CMP_REQUEUE:
6998 case FUTEX_WAKE_OP:
6999 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
7000 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
7001 But the prototype takes a `struct timespec *'; insert casts
7002 to satisfy the compiler. We do not need to tswap TIMEOUT
7003 since it's not compared to guest memory. */
7004 pts = (struct timespec *)(uintptr_t) timeout;
7005 return get_errno(safe_futex(g2h(uaddr), op, val, pts,
7006 g2h(uaddr2),
7007 (base_op == FUTEX_CMP_REQUEUE
7008 ? tswap32(val3)
7009 : val3)));
7010 default:
7011 return -TARGET_ENOSYS;
7012 }
7013 }
7014 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7015 static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
7016 abi_long handle, abi_long mount_id,
7017 abi_long flags)
7018 {
7019 struct file_handle *target_fh;
7020 struct file_handle *fh;
7021 int mid = 0;
7022 abi_long ret;
7023 char *name;
7024 unsigned int size, total_size;
7025
7026 if (get_user_s32(size, handle)) {
7027 return -TARGET_EFAULT;
7028 }
7029
7030 name = lock_user_string(pathname);
7031 if (!name) {
7032 return -TARGET_EFAULT;
7033 }
7034
7035 total_size = sizeof(struct file_handle) + size;
7036 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
7037 if (!target_fh) {
7038 unlock_user(name, pathname, 0);
7039 return -TARGET_EFAULT;
7040 }
7041
7042 fh = g_malloc0(total_size);
7043 fh->handle_bytes = size;
7044
7045 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
7046 unlock_user(name, pathname, 0);
7047
7048 /* man name_to_handle_at(2):
7049 * Other than the use of the handle_bytes field, the caller should treat
7050 * the file_handle structure as an opaque data type
7051 */
7052
7053 memcpy(target_fh, fh, total_size);
7054 target_fh->handle_bytes = tswap32(fh->handle_bytes);
7055 target_fh->handle_type = tswap32(fh->handle_type);
7056 g_free(fh);
7057 unlock_user(target_fh, handle, total_size);
7058
7059 if (put_user_s32(mid, mount_id)) {
7060 return -TARGET_EFAULT;
7061 }
7062
7063 return ret;
7064
7065 }
7066 #endif
7067
7068 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7069 static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
7070 abi_long flags)
7071 {
7072 struct file_handle *target_fh;
7073 struct file_handle *fh;
7074 unsigned int size, total_size;
7075 abi_long ret;
7076
7077 if (get_user_s32(size, handle)) {
7078 return -TARGET_EFAULT;
7079 }
7080
7081 total_size = sizeof(struct file_handle) + size;
7082 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
7083 if (!target_fh) {
7084 return -TARGET_EFAULT;
7085 }
7086
7087 fh = g_memdup(target_fh, total_size);
7088 fh->handle_bytes = size;
7089 fh->handle_type = tswap32(target_fh->handle_type);
7090
7091 ret = get_errno(open_by_handle_at(mount_fd, fh,
7092 target_to_host_bitmask(flags, fcntl_flags_tbl)));
7093
7094 g_free(fh);
7095
7096 unlock_user(target_fh, handle, total_size);
7097
7098 return ret;
7099 }
7100 #endif
7101
7102 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7103
7104 /* signalfd siginfo conversion */
7105
7106 static void
7107 host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
7108 const struct signalfd_siginfo *info)
7109 {
7110 int sig = host_to_target_signal(info->ssi_signo);
7111
7112 /* linux/signalfd.h defines a ssi_addr_lsb
7113 * not defined in sys/signalfd.h but used by some kernels
7114 */
7115
7116 #ifdef BUS_MCEERR_AO
7117 if (tinfo->ssi_signo == SIGBUS &&
7118 (tinfo->ssi_code == BUS_MCEERR_AR ||
7119 tinfo->ssi_code == BUS_MCEERR_AO)) {
7120 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
7121 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
7122 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
7123 }
7124 #endif
7125
7126 tinfo->ssi_signo = tswap32(sig);
7127 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
7128 tinfo->ssi_code = tswap32(info->ssi_code);
7129 tinfo->ssi_pid = tswap32(info->ssi_pid);
7130 tinfo->ssi_uid = tswap32(info->ssi_uid);
7131 tinfo->ssi_fd = tswap32(info->ssi_fd);
7132 tinfo->ssi_tid = tswap32(info->ssi_tid);
7133 tinfo->ssi_band = tswap32(info->ssi_band);
7134 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
7135 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
7136 tinfo->ssi_status = tswap32(info->ssi_status);
7137 tinfo->ssi_int = tswap32(info->ssi_int);
7138 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
7139 tinfo->ssi_utime = tswap64(info->ssi_utime);
7140 tinfo->ssi_stime = tswap64(info->ssi_stime);
7141 tinfo->ssi_addr = tswap64(info->ssi_addr);
7142 }
7143
7144 static abi_long host_to_target_data_signalfd(void *buf, size_t len)
7145 {
7146 int i;
7147
7148 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
7149 host_to_target_signalfd_siginfo(buf + i, buf + i);
7150 }
7151
7152 return len;
7153 }
7154
7155 static TargetFdTrans target_signalfd_trans = {
7156 .host_to_target_data = host_to_target_data_signalfd,
7157 };
7158
7159 static abi_long do_signalfd4(int fd, abi_long mask, int flags)
7160 {
7161 int host_flags;
7162 target_sigset_t *target_mask;
7163 sigset_t host_mask;
7164 abi_long ret;
7165
7166 if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
7167 return -TARGET_EINVAL;
7168 }
7169 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
7170 return -TARGET_EFAULT;
7171 }
7172
7173 target_to_host_sigset(&host_mask, target_mask);
7174
7175 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
7176
7177 ret = get_errno(signalfd(fd, &host_mask, host_flags));
7178 if (ret >= 0) {
7179 fd_trans_register(ret, &target_signalfd_trans);
7180 }
7181
7182 unlock_user_struct(target_mask, mask, 0);
7183
7184 return ret;
7185 }
7186 #endif
7187
7188 /* Map host to target signal numbers for the wait family of syscalls.
7189 Assume all other status bits are the same. */
7190 int host_to_target_waitstatus(int status)
7191 {
7192 if (WIFSIGNALED(status)) {
7193 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
7194 }
7195 if (WIFSTOPPED(status)) {
7196 return (host_to_target_signal(WSTOPSIG(status)) << 8)
7197 | (status & 0xff);
7198 }
7199 return status;
7200 }
7201
7202 static int open_self_cmdline(void *cpu_env, int fd)
7203 {
7204 int fd_orig = -1;
7205 bool word_skipped = false;
7206
7207 fd_orig = open("/proc/self/cmdline", O_RDONLY);
7208 if (fd_orig < 0) {
7209 return fd_orig;
7210 }
7211
7212 while (true) {
7213 ssize_t nb_read;
7214 char buf[128];
7215 char *cp_buf = buf;
7216
7217 nb_read = read(fd_orig, buf, sizeof(buf));
7218 if (nb_read < 0) {
7219 int e = errno;
7220 fd_orig = close(fd_orig);
7221 errno = e;
7222 return -1;
7223 } else if (nb_read == 0) {
7224 break;
7225 }
7226
7227 if (!word_skipped) {
7228 /* Skip the first string, which is the path to qemu-*-static
7229 instead of the actual command. */
7230 cp_buf = memchr(buf, 0, nb_read);
7231 if (cp_buf) {
7232 /* Null byte found, skip one string */
7233 cp_buf++;
7234 nb_read -= cp_buf - buf;
7235 word_skipped = true;
7236 }
7237 }
7238
7239 if (word_skipped) {
7240 if (write(fd, cp_buf, nb_read) != nb_read) {
7241 int e = errno;
7242 close(fd_orig);
7243 errno = e;
7244 return -1;
7245 }
7246 }
7247 }
7248
7249 return close(fd_orig);
7250 }
7251
7252 static int open_self_maps(void *cpu_env, int fd)
7253 {
7254 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
7255 TaskState *ts = cpu->opaque;
7256 FILE *fp;
7257 char *line = NULL;
7258 size_t len = 0;
7259 ssize_t read;
7260
7261 fp = fopen("/proc/self/maps", "r");
7262 if (fp == NULL) {
7263 return -1;
7264 }
7265
7266 while ((read = getline(&line, &len, fp)) != -1) {
7267 int fields, dev_maj, dev_min, inode;
7268 uint64_t min, max, offset;
7269 char flag_r, flag_w, flag_x, flag_p;
7270 char path[512] = "";
7271 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
7272 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
7273 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
7274
7275 if ((fields < 10) || (fields > 11)) {
7276 continue;
7277 }
7278 if (h2g_valid(min)) {
7279 int flags = page_get_flags(h2g(min));
7280 max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX);
7281 if (page_check_range(h2g(min), max - min, flags) == -1) {
7282 continue;
7283 }
7284 if (h2g(min) == ts->info->stack_limit) {
7285 pstrcpy(path, sizeof(path), " [stack]");
7286 }
7287 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
7288 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
7289 h2g(min), h2g(max - 1) + 1, flag_r, flag_w,
7290 flag_x, flag_p, offset, dev_maj, dev_min, inode,
7291 path[0] ? " " : "", path);
7292 }
7293 }
7294
7295 free(line);
7296 fclose(fp);
7297
7298 return 0;
7299 }
7300
7301 static int open_self_stat(void *cpu_env, int fd)
7302 {
7303 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
7304 TaskState *ts = cpu->opaque;
7305 abi_ulong start_stack = ts->info->start_stack;
7306 int i;
7307
7308 for (i = 0; i < 44; i++) {
7309 char buf[128];
7310 int len;
7311 uint64_t val = 0;
7312
7313 if (i == 0) {
7314 /* pid */
7315 val = getpid();
7316 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
7317 } else if (i == 1) {
7318 /* app name */
7319 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
7320 } else if (i == 27) {
7321 /* stack bottom */
7322 val = start_stack;
7323 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
7324 } else {
7325 /* for the rest, there is MasterCard */
7326 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
7327 }
7328
7329 len = strlen(buf);
7330 if (write(fd, buf, len) != len) {
7331 return -1;
7332 }
7333 }
7334
7335 return 0;
7336 }
7337
7338 static int open_self_auxv(void *cpu_env, int fd)
7339 {
7340 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
7341 TaskState *ts = cpu->opaque;
7342 abi_ulong auxv = ts->info->saved_auxv;
7343 abi_ulong len = ts->info->auxv_len;
7344 char *ptr;
7345
7346 /*
7347 * Auxiliary vector is stored in target process stack.
7348 * read in whole auxv vector and copy it to file
7349 */
7350 ptr = lock_user(VERIFY_READ, auxv, len, 0);
7351 if (ptr != NULL) {
7352 while (len > 0) {
7353 ssize_t r;
7354 r = write(fd, ptr, len);
7355 if (r <= 0) {
7356 break;
7357 }
7358 len -= r;
7359 ptr += r;
7360 }
7361 lseek(fd, 0, SEEK_SET);
7362 unlock_user(ptr, auxv, len);
7363 }
7364
7365 return 0;
7366 }
7367
7368 static int is_proc_myself(const char *filename, const char *entry)
7369 {
7370 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
7371 filename += strlen("/proc/");
7372 if (!strncmp(filename, "self/", strlen("self/"))) {
7373 filename += strlen("self/");
7374 } else if (*filename >= '1' && *filename <= '9') {
7375 char myself[80];
7376 snprintf(myself, sizeof(myself), "%d/", getpid());
7377 if (!strncmp(filename, myself, strlen(myself))) {
7378 filename += strlen(myself);
7379 } else {
7380 return 0;
7381 }
7382 } else {
7383 return 0;
7384 }
7385 if (!strcmp(filename, entry)) {
7386 return 1;
7387 }
7388 }
7389 return 0;
7390 }
7391
7392 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7393 static int is_proc(const char *filename, const char *entry)
7394 {
7395 return strcmp(filename, entry) == 0;
7396 }
7397
7398 static int open_net_route(void *cpu_env, int fd)
7399 {
7400 FILE *fp;
7401 char *line = NULL;
7402 size_t len = 0;
7403 ssize_t read;
7404
7405 fp = fopen("/proc/net/route", "r");
7406 if (fp == NULL) {
7407 return -1;
7408 }
7409
7410 /* read header */
7411
7412 read = getline(&line, &len, fp);
7413 dprintf(fd, "%s", line);
7414
7415 /* read routes */
7416
7417 while ((read = getline(&line, &len, fp)) != -1) {
7418 char iface[16];
7419 uint32_t dest, gw, mask;
7420 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
7421 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7422 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
7423 &mask, &mtu, &window, &irtt);
7424 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7425 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
7426 metric, tswap32(mask), mtu, window, irtt);
7427 }
7428
7429 free(line);
7430 fclose(fp);
7431
7432 return 0;
7433 }
7434 #endif
7435
7436 static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
7437 {
7438 struct fake_open {
7439 const char *filename;
7440 int (*fill)(void *cpu_env, int fd);
7441 int (*cmp)(const char *s1, const char *s2);
7442 };
7443 const struct fake_open *fake_open;
7444 static const struct fake_open fakes[] = {
7445 { "maps", open_self_maps, is_proc_myself },
7446 { "stat", open_self_stat, is_proc_myself },
7447 { "auxv", open_self_auxv, is_proc_myself },
7448 { "cmdline", open_self_cmdline, is_proc_myself },
7449 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7450 { "/proc/net/route", open_net_route, is_proc },
7451 #endif
7452 { NULL, NULL, NULL }
7453 };
7454
7455 if (is_proc_myself(pathname, "exe")) {
7456 int execfd = qemu_getauxval(AT_EXECFD);
7457 return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
7458 }
7459
7460 for (fake_open = fakes; fake_open->filename; fake_open++) {
7461 if (fake_open->cmp(pathname, fake_open->filename)) {
7462 break;
7463 }
7464 }
7465
7466 if (fake_open->filename) {
7467 const char *tmpdir;
7468 char filename[PATH_MAX];
7469 int fd, r;
7470
7471 /* create temporary file to map stat to */
7472 tmpdir = getenv("TMPDIR");
7473 if (!tmpdir)
7474 tmpdir = "/tmp";
7475 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
7476 fd = mkstemp(filename);
7477 if (fd < 0) {
7478 return fd;
7479 }
7480 unlink(filename);
7481
7482 if ((r = fake_open->fill(cpu_env, fd))) {
7483 int e = errno;
7484 close(fd);
7485 errno = e;
7486 return r;
7487 }
7488 lseek(fd, 0, SEEK_SET);
7489
7490 return fd;
7491 }
7492
7493 return safe_openat(dirfd, path(pathname), flags, mode);
7494 }
7495
7496 #define TIMER_MAGIC 0x0caf0000
7497 #define TIMER_MAGIC_MASK 0xffff0000
7498
7499 /* Convert QEMU provided timer ID back to internal 16bit index format */
7500 static target_timer_t get_timer_id(abi_long arg)
7501 {
7502 target_timer_t timerid = arg;
7503
7504 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
7505 return -TARGET_EINVAL;
7506 }
7507
7508 timerid &= 0xffff;
7509
7510 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
7511 return -TARGET_EINVAL;
7512 }
7513
7514 return timerid;
7515 }
7516
7517 /* do_syscall() should always have a single exit point at the end so
7518 that actions, such as logging of syscall results, can be performed.
7519 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
7520 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
7521 abi_long arg2, abi_long arg3, abi_long arg4,
7522 abi_long arg5, abi_long arg6, abi_long arg7,
7523 abi_long arg8)
7524 {
7525 CPUState *cpu = ENV_GET_CPU(cpu_env);
7526 abi_long ret;
7527 struct stat st;
7528 struct statfs stfs;
7529 void *p;
7530
7531 #if defined(DEBUG_ERESTARTSYS)
7532 /* Debug-only code for exercising the syscall-restart code paths
7533 * in the per-architecture cpu main loops: restart every syscall
7534 * the guest makes once before letting it through.
7535 */
7536 {
7537 static int flag;
7538
7539 flag = !flag;
7540 if (flag) {
7541 return -TARGET_ERESTARTSYS;
7542 }
7543 }
7544 #endif
7545
7546 #ifdef DEBUG
7547 gemu_log("syscall %d", num);
7548 #endif
7549 trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
7550 if(do_strace)
7551 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
7552
7553 switch(num) {
7554 case TARGET_NR_exit:
7555 /* In old applications this may be used to implement _exit(2).
7556 However in threaded applictions it is used for thread termination,
7557 and _exit_group is used for application termination.
7558 Do thread termination if we have more then one thread. */
7559
7560 if (block_signals()) {
7561 ret = -TARGET_ERESTARTSYS;
7562 break;
7563 }
7564
7565 cpu_list_lock();
7566
7567 if (CPU_NEXT(first_cpu)) {
7568 TaskState *ts;
7569
7570 /* Remove the CPU from the list. */
7571 QTAILQ_REMOVE(&cpus, cpu, node);
7572
7573 cpu_list_unlock();
7574
7575 ts = cpu->opaque;
7576 if (ts->child_tidptr) {
7577 put_user_u32(0, ts->child_tidptr);
7578 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
7579 NULL, NULL, 0);
7580 }
7581 thread_cpu = NULL;
7582 object_unref(OBJECT(cpu));
7583 g_free(ts);
7584 rcu_unregister_thread();
7585 pthread_exit(NULL);
7586 }
7587
7588 cpu_list_unlock();
7589 #ifdef TARGET_GPROF
7590 _mcleanup();
7591 #endif
7592 gdb_exit(cpu_env, arg1);
7593 _exit(arg1);
7594 ret = 0; /* avoid warning */
7595 break;
7596 case TARGET_NR_read:
7597 if (arg3 == 0)
7598 ret = 0;
7599 else {
7600 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7601 goto efault;
7602 ret = get_errno(safe_read(arg1, p, arg3));
7603 if (ret >= 0 &&
7604 fd_trans_host_to_target_data(arg1)) {
7605 ret = fd_trans_host_to_target_data(arg1)(p, ret);
7606 }
7607 unlock_user(p, arg2, ret);
7608 }
7609 break;
7610 case TARGET_NR_write:
7611 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7612 goto efault;
7613 ret = get_errno(safe_write(arg1, p, arg3));
7614 unlock_user(p, arg2, 0);
7615 break;
7616 #ifdef TARGET_NR_open
7617 case TARGET_NR_open:
7618 if (!(p = lock_user_string(arg1)))
7619 goto efault;
7620 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
7621 target_to_host_bitmask(arg2, fcntl_flags_tbl),
7622 arg3));
7623 fd_trans_unregister(ret);
7624 unlock_user(p, arg1, 0);
7625 break;
7626 #endif
7627 case TARGET_NR_openat:
7628 if (!(p = lock_user_string(arg2)))
7629 goto efault;
7630 ret = get_errno(do_openat(cpu_env, arg1, p,
7631 target_to_host_bitmask(arg3, fcntl_flags_tbl),
7632 arg4));
7633 fd_trans_unregister(ret);
7634 unlock_user(p, arg2, 0);
7635 break;
7636 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7637 case TARGET_NR_name_to_handle_at:
7638 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
7639 break;
7640 #endif
7641 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7642 case TARGET_NR_open_by_handle_at:
7643 ret = do_open_by_handle_at(arg1, arg2, arg3);
7644 fd_trans_unregister(ret);
7645 break;
7646 #endif
7647 case TARGET_NR_close:
7648 fd_trans_unregister(arg1);
7649 ret = get_errno(close(arg1));
7650 break;
7651 case TARGET_NR_brk:
7652 ret = do_brk(arg1);
7653 break;
7654 #ifdef TARGET_NR_fork
7655 case TARGET_NR_fork:
7656 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
7657 break;
7658 #endif
7659 #ifdef TARGET_NR_waitpid
7660 case TARGET_NR_waitpid:
7661 {
7662 int status;
7663 ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
7664 if (!is_error(ret) && arg2 && ret
7665 && put_user_s32(host_to_target_waitstatus(status), arg2))
7666 goto efault;
7667 }
7668 break;
7669 #endif
7670 #ifdef TARGET_NR_waitid
7671 case TARGET_NR_waitid:
7672 {
7673 siginfo_t info;
7674 info.si_pid = 0;
7675 ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
7676 if (!is_error(ret) && arg3 && info.si_pid != 0) {
7677 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
7678 goto efault;
7679 host_to_target_siginfo(p, &info);
7680 unlock_user(p, arg3, sizeof(target_siginfo_t));
7681 }
7682 }
7683 break;
7684 #endif
7685 #ifdef TARGET_NR_creat /* not on alpha */
7686 case TARGET_NR_creat:
7687 if (!(p = lock_user_string(arg1)))
7688 goto efault;
7689 ret = get_errno(creat(p, arg2));
7690 fd_trans_unregister(ret);
7691 unlock_user(p, arg1, 0);
7692 break;
7693 #endif
7694 #ifdef TARGET_NR_link
7695 case TARGET_NR_link:
7696 {
7697 void * p2;
7698 p = lock_user_string(arg1);
7699 p2 = lock_user_string(arg2);
7700 if (!p || !p2)
7701 ret = -TARGET_EFAULT;
7702 else
7703 ret = get_errno(link(p, p2));
7704 unlock_user(p2, arg2, 0);
7705 unlock_user(p, arg1, 0);
7706 }
7707 break;
7708 #endif
7709 #if defined(TARGET_NR_linkat)
7710 case TARGET_NR_linkat:
7711 {
7712 void * p2 = NULL;
7713 if (!arg2 || !arg4)
7714 goto efault;
7715 p = lock_user_string(arg2);
7716 p2 = lock_user_string(arg4);
7717 if (!p || !p2)
7718 ret = -TARGET_EFAULT;
7719 else
7720 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
7721 unlock_user(p, arg2, 0);
7722 unlock_user(p2, arg4, 0);
7723 }
7724 break;
7725 #endif
7726 #ifdef TARGET_NR_unlink
7727 case TARGET_NR_unlink:
7728 if (!(p = lock_user_string(arg1)))
7729 goto efault;
7730 ret = get_errno(unlink(p));
7731 unlock_user(p, arg1, 0);
7732 break;
7733 #endif
7734 #if defined(TARGET_NR_unlinkat)
7735 case TARGET_NR_unlinkat:
7736 if (!(p = lock_user_string(arg2)))
7737 goto efault;
7738 ret = get_errno(unlinkat(arg1, p, arg3));
7739 unlock_user(p, arg2, 0);
7740 break;
7741 #endif
7742 case TARGET_NR_execve:
7743 {
7744 char **argp, **envp;
7745 int argc, envc;
7746 abi_ulong gp;
7747 abi_ulong guest_argp;
7748 abi_ulong guest_envp;
7749 abi_ulong addr;
7750 char **q;
7751 int total_size = 0;
7752
7753 argc = 0;
7754 guest_argp = arg2;
7755 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
7756 if (get_user_ual(addr, gp))
7757 goto efault;
7758 if (!addr)
7759 break;
7760 argc++;
7761 }
7762 envc = 0;
7763 guest_envp = arg3;
7764 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
7765 if (get_user_ual(addr, gp))
7766 goto efault;
7767 if (!addr)
7768 break;
7769 envc++;
7770 }
7771
7772 argp = alloca((argc + 1) * sizeof(void *));
7773 envp = alloca((envc + 1) * sizeof(void *));
7774
7775 for (gp = guest_argp, q = argp; gp;
7776 gp += sizeof(abi_ulong), q++) {
7777 if (get_user_ual(addr, gp))
7778 goto execve_efault;
7779 if (!addr)
7780 break;
7781 if (!(*q = lock_user_string(addr)))
7782 goto execve_efault;
7783 total_size += strlen(*q) + 1;
7784 }
7785 *q = NULL;
7786
7787 for (gp = guest_envp, q = envp; gp;
7788 gp += sizeof(abi_ulong), q++) {
7789 if (get_user_ual(addr, gp))
7790 goto execve_efault;
7791 if (!addr)
7792 break;
7793 if (!(*q = lock_user_string(addr)))
7794 goto execve_efault;
7795 total_size += strlen(*q) + 1;
7796 }
7797 *q = NULL;
7798
7799 if (!(p = lock_user_string(arg1)))
7800 goto execve_efault;
7801 /* Although execve() is not an interruptible syscall it is
7802 * a special case where we must use the safe_syscall wrapper:
7803 * if we allow a signal to happen before we make the host
7804 * syscall then we will 'lose' it, because at the point of
7805 * execve the process leaves QEMU's control. So we use the
7806 * safe syscall wrapper to ensure that we either take the
7807 * signal as a guest signal, or else it does not happen
7808 * before the execve completes and makes it the other
7809 * program's problem.
7810 */
7811 ret = get_errno(safe_execve(p, argp, envp));
7812 unlock_user(p, arg1, 0);
7813
7814 goto execve_end;
7815
7816 execve_efault:
7817 ret = -TARGET_EFAULT;
7818
7819 execve_end:
7820 for (gp = guest_argp, q = argp; *q;
7821 gp += sizeof(abi_ulong), q++) {
7822 if (get_user_ual(addr, gp)
7823 || !addr)
7824 break;
7825 unlock_user(*q, addr, 0);
7826 }
7827 for (gp = guest_envp, q = envp; *q;
7828 gp += sizeof(abi_ulong), q++) {
7829 if (get_user_ual(addr, gp)
7830 || !addr)
7831 break;
7832 unlock_user(*q, addr, 0);
7833 }
7834 }
7835 break;
7836 case TARGET_NR_chdir:
7837 if (!(p = lock_user_string(arg1)))
7838 goto efault;
7839 ret = get_errno(chdir(p));
7840 unlock_user(p, arg1, 0);
7841 break;
7842 #ifdef TARGET_NR_time
7843 case TARGET_NR_time:
7844 {
7845 time_t host_time;
7846 ret = get_errno(time(&host_time));
7847 if (!is_error(ret)
7848 && arg1
7849 && put_user_sal(host_time, arg1))
7850 goto efault;
7851 }
7852 break;
7853 #endif
7854 #ifdef TARGET_NR_mknod
7855 case TARGET_NR_mknod:
7856 if (!(p = lock_user_string(arg1)))
7857 goto efault;
7858 ret = get_errno(mknod(p, arg2, arg3));
7859 unlock_user(p, arg1, 0);
7860 break;
7861 #endif
7862 #if defined(TARGET_NR_mknodat)
7863 case TARGET_NR_mknodat:
7864 if (!(p = lock_user_string(arg2)))
7865 goto efault;
7866 ret = get_errno(mknodat(arg1, p, arg3, arg4));
7867 unlock_user(p, arg2, 0);
7868 break;
7869 #endif
7870 #ifdef TARGET_NR_chmod
7871 case TARGET_NR_chmod:
7872 if (!(p = lock_user_string(arg1)))
7873 goto efault;
7874 ret = get_errno(chmod(p, arg2));
7875 unlock_user(p, arg1, 0);
7876 break;
7877 #endif
7878 #ifdef TARGET_NR_break
7879 case TARGET_NR_break:
7880 goto unimplemented;
7881 #endif
7882 #ifdef TARGET_NR_oldstat
7883 case TARGET_NR_oldstat:
7884 goto unimplemented;
7885 #endif
7886 case TARGET_NR_lseek:
7887 ret = get_errno(lseek(arg1, arg2, arg3));
7888 break;
7889 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
7890 /* Alpha specific */
7891 case TARGET_NR_getxpid:
7892 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
7893 ret = get_errno(getpid());
7894 break;
7895 #endif
7896 #ifdef TARGET_NR_getpid
7897 case TARGET_NR_getpid:
7898 ret = get_errno(getpid());
7899 break;
7900 #endif
7901 case TARGET_NR_mount:
7902 {
7903 /* need to look at the data field */
7904 void *p2, *p3;
7905
7906 if (arg1) {
7907 p = lock_user_string(arg1);
7908 if (!p) {
7909 goto efault;
7910 }
7911 } else {
7912 p = NULL;
7913 }
7914
7915 p2 = lock_user_string(arg2);
7916 if (!p2) {
7917 if (arg1) {
7918 unlock_user(p, arg1, 0);
7919 }
7920 goto efault;
7921 }
7922
7923 if (arg3) {
7924 p3 = lock_user_string(arg3);
7925 if (!p3) {
7926 if (arg1) {
7927 unlock_user(p, arg1, 0);
7928 }
7929 unlock_user(p2, arg2, 0);
7930 goto efault;
7931 }
7932 } else {
7933 p3 = NULL;
7934 }
7935
7936 /* FIXME - arg5 should be locked, but it isn't clear how to
7937 * do that since it's not guaranteed to be a NULL-terminated
7938 * string.
7939 */
7940 if (!arg5) {
7941 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
7942 } else {
7943 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
7944 }
7945 ret = get_errno(ret);
7946
7947 if (arg1) {
7948 unlock_user(p, arg1, 0);
7949 }
7950 unlock_user(p2, arg2, 0);
7951 if (arg3) {
7952 unlock_user(p3, arg3, 0);
7953 }
7954 }
7955 break;
7956 #ifdef TARGET_NR_umount
7957 case TARGET_NR_umount:
7958 if (!(p = lock_user_string(arg1)))
7959 goto efault;
7960 ret = get_errno(umount(p));
7961 unlock_user(p, arg1, 0);
7962 break;
7963 #endif
7964 #ifdef TARGET_NR_stime /* not on alpha */
7965 case TARGET_NR_stime:
7966 {
7967 time_t host_time;
7968 if (get_user_sal(host_time, arg1))
7969 goto efault;
7970 ret = get_errno(stime(&host_time));
7971 }
7972 break;
7973 #endif
7974 case TARGET_NR_ptrace:
7975 goto unimplemented;
7976 #ifdef TARGET_NR_alarm /* not on alpha */
7977 case TARGET_NR_alarm:
7978 ret = alarm(arg1);
7979 break;
7980 #endif
7981 #ifdef TARGET_NR_oldfstat
7982 case TARGET_NR_oldfstat:
7983 goto unimplemented;
7984 #endif
7985 #ifdef TARGET_NR_pause /* not on alpha */
7986 case TARGET_NR_pause:
7987 if (!block_signals()) {
7988 sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
7989 }
7990 ret = -TARGET_EINTR;
7991 break;
7992 #endif
7993 #ifdef TARGET_NR_utime
7994 case TARGET_NR_utime:
7995 {
7996 struct utimbuf tbuf, *host_tbuf;
7997 struct target_utimbuf *target_tbuf;
7998 if (arg2) {
7999 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
8000 goto efault;
8001 tbuf.actime = tswapal(target_tbuf->actime);
8002 tbuf.modtime = tswapal(target_tbuf->modtime);
8003 unlock_user_struct(target_tbuf, arg2, 0);
8004 host_tbuf = &tbuf;
8005 } else {
8006 host_tbuf = NULL;
8007 }
8008 if (!(p = lock_user_string(arg1)))
8009 goto efault;
8010 ret = get_errno(utime(p, host_tbuf));
8011 unlock_user(p, arg1, 0);
8012 }
8013 break;
8014 #endif
8015 #ifdef TARGET_NR_utimes
8016 case TARGET_NR_utimes:
8017 {
8018 struct timeval *tvp, tv[2];
8019 if (arg2) {
8020 if (copy_from_user_timeval(&tv[0], arg2)
8021 || copy_from_user_timeval(&tv[1],
8022 arg2 + sizeof(struct target_timeval)))
8023 goto efault;
8024 tvp = tv;
8025 } else {
8026 tvp = NULL;
8027 }
8028 if (!(p = lock_user_string(arg1)))
8029 goto efault;
8030 ret = get_errno(utimes(p, tvp));
8031 unlock_user(p, arg1, 0);
8032 }
8033 break;
8034 #endif
8035 #if defined(TARGET_NR_futimesat)
8036 case TARGET_NR_futimesat:
8037 {
8038 struct timeval *tvp, tv[2];
8039 if (arg3) {
8040 if (copy_from_user_timeval(&tv[0], arg3)
8041 || copy_from_user_timeval(&tv[1],
8042 arg3 + sizeof(struct target_timeval)))
8043 goto efault;
8044 tvp = tv;
8045 } else {
8046 tvp = NULL;
8047 }
8048 if (!(p = lock_user_string(arg2)))
8049 goto efault;
8050 ret = get_errno(futimesat(arg1, path(p), tvp));
8051 unlock_user(p, arg2, 0);
8052 }
8053 break;
8054 #endif
8055 #ifdef TARGET_NR_stty
8056 case TARGET_NR_stty:
8057 goto unimplemented;
8058 #endif
8059 #ifdef TARGET_NR_gtty
8060 case TARGET_NR_gtty:
8061 goto unimplemented;
8062 #endif
8063 #ifdef TARGET_NR_access
8064 case TARGET_NR_access:
8065 if (!(p = lock_user_string(arg1)))
8066 goto efault;
8067 ret = get_errno(access(path(p), arg2));
8068 unlock_user(p, arg1, 0);
8069 break;
8070 #endif
8071 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
8072 case TARGET_NR_faccessat:
8073 if (!(p = lock_user_string(arg2)))
8074 goto efault;
8075 ret = get_errno(faccessat(arg1, p, arg3, 0));
8076 unlock_user(p, arg2, 0);
8077 break;
8078 #endif
8079 #ifdef TARGET_NR_nice /* not on alpha */
8080 case TARGET_NR_nice:
8081 ret = get_errno(nice(arg1));
8082 break;
8083 #endif
8084 #ifdef TARGET_NR_ftime
8085 case TARGET_NR_ftime:
8086 goto unimplemented;
8087 #endif
8088 case TARGET_NR_sync:
8089 sync();
8090 ret = 0;
8091 break;
8092 case TARGET_NR_kill:
8093 ret = get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
8094 break;
8095 #ifdef TARGET_NR_rename
8096 case TARGET_NR_rename:
8097 {
8098 void *p2;
8099 p = lock_user_string(arg1);
8100 p2 = lock_user_string(arg2);
8101 if (!p || !p2)
8102 ret = -TARGET_EFAULT;
8103 else
8104 ret = get_errno(rename(p, p2));
8105 unlock_user(p2, arg2, 0);
8106 unlock_user(p, arg1, 0);
8107 }
8108 break;
8109 #endif
8110 #if defined(TARGET_NR_renameat)
8111 case TARGET_NR_renameat:
8112 {
8113 void *p2;
8114 p = lock_user_string(arg2);
8115 p2 = lock_user_string(arg4);
8116 if (!p || !p2)
8117 ret = -TARGET_EFAULT;
8118 else
8119 ret = get_errno(renameat(arg1, p, arg3, p2));
8120 unlock_user(p2, arg4, 0);
8121 unlock_user(p, arg2, 0);
8122 }
8123 break;
8124 #endif
8125 #ifdef TARGET_NR_mkdir
8126 case TARGET_NR_mkdir:
8127 if (!(p = lock_user_string(arg1)))
8128 goto efault;
8129 ret = get_errno(mkdir(p, arg2));
8130 unlock_user(p, arg1, 0);
8131 break;
8132 #endif
8133 #if defined(TARGET_NR_mkdirat)
8134 case TARGET_NR_mkdirat:
8135 if (!(p = lock_user_string(arg2)))
8136 goto efault;
8137 ret = get_errno(mkdirat(arg1, p, arg3));
8138 unlock_user(p, arg2, 0);
8139 break;
8140 #endif
8141 #ifdef TARGET_NR_rmdir
8142 case TARGET_NR_rmdir:
8143 if (!(p = lock_user_string(arg1)))
8144 goto efault;
8145 ret = get_errno(rmdir(p));
8146 unlock_user(p, arg1, 0);
8147 break;
8148 #endif
8149 case TARGET_NR_dup:
8150 ret = get_errno(dup(arg1));
8151 if (ret >= 0) {
8152 fd_trans_dup(arg1, ret);
8153 }
8154 break;
8155 #ifdef TARGET_NR_pipe
8156 case TARGET_NR_pipe:
8157 ret = do_pipe(cpu_env, arg1, 0, 0);
8158 break;
8159 #endif
8160 #ifdef TARGET_NR_pipe2
8161 case TARGET_NR_pipe2:
8162 ret = do_pipe(cpu_env, arg1,
8163 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
8164 break;
8165 #endif
8166 case TARGET_NR_times:
8167 {
8168 struct target_tms *tmsp;
8169 struct tms tms;
8170 ret = get_errno(times(&tms));
8171 if (arg1) {
8172 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
8173 if (!tmsp)
8174 goto efault;
8175 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
8176 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
8177 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
8178 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
8179 }
8180 if (!is_error(ret))
8181 ret = host_to_target_clock_t(ret);
8182 }
8183 break;
8184 #ifdef TARGET_NR_prof
8185 case TARGET_NR_prof:
8186 goto unimplemented;
8187 #endif
8188 #ifdef TARGET_NR_signal
8189 case TARGET_NR_signal:
8190 goto unimplemented;
8191 #endif
8192 case TARGET_NR_acct:
8193 if (arg1 == 0) {
8194 ret = get_errno(acct(NULL));
8195 } else {
8196 if (!(p = lock_user_string(arg1)))
8197 goto efault;
8198 ret = get_errno(acct(path(p)));
8199 unlock_user(p, arg1, 0);
8200 }
8201 break;
8202 #ifdef TARGET_NR_umount2
8203 case TARGET_NR_umount2:
8204 if (!(p = lock_user_string(arg1)))
8205 goto efault;
8206 ret = get_errno(umount2(p, arg2));
8207 unlock_user(p, arg1, 0);
8208 break;
8209 #endif
8210 #ifdef TARGET_NR_lock
8211 case TARGET_NR_lock:
8212 goto unimplemented;
8213 #endif
8214 case TARGET_NR_ioctl:
8215 ret = do_ioctl(arg1, arg2, arg3);
8216 break;
8217 case TARGET_NR_fcntl:
8218 ret = do_fcntl(arg1, arg2, arg3);
8219 break;
8220 #ifdef TARGET_NR_mpx
8221 case TARGET_NR_mpx:
8222 goto unimplemented;
8223 #endif
8224 case TARGET_NR_setpgid:
8225 ret = get_errno(setpgid(arg1, arg2));
8226 break;
8227 #ifdef TARGET_NR_ulimit
8228 case TARGET_NR_ulimit:
8229 goto unimplemented;
8230 #endif
8231 #ifdef TARGET_NR_oldolduname
8232 case TARGET_NR_oldolduname:
8233 goto unimplemented;
8234 #endif
8235 case TARGET_NR_umask:
8236 ret = get_errno(umask(arg1));
8237 break;
8238 case TARGET_NR_chroot:
8239 if (!(p = lock_user_string(arg1)))
8240 goto efault;
8241 ret = get_errno(chroot(p));
8242 unlock_user(p, arg1, 0);
8243 break;
8244 #ifdef TARGET_NR_ustat
8245 case TARGET_NR_ustat:
8246 goto unimplemented;
8247 #endif
8248 #ifdef TARGET_NR_dup2
8249 case TARGET_NR_dup2:
8250 ret = get_errno(dup2(arg1, arg2));
8251 if (ret >= 0) {
8252 fd_trans_dup(arg1, arg2);
8253 }
8254 break;
8255 #endif
8256 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
8257 case TARGET_NR_dup3:
8258 ret = get_errno(dup3(arg1, arg2, arg3));
8259 if (ret >= 0) {
8260 fd_trans_dup(arg1, arg2);
8261 }
8262 break;
8263 #endif
8264 #ifdef TARGET_NR_getppid /* not on alpha */
8265 case TARGET_NR_getppid:
8266 ret = get_errno(getppid());
8267 break;
8268 #endif
8269 #ifdef TARGET_NR_getpgrp
8270 case TARGET_NR_getpgrp:
8271 ret = get_errno(getpgrp());
8272 break;
8273 #endif
8274 case TARGET_NR_setsid:
8275 ret = get_errno(setsid());
8276 break;
8277 #ifdef TARGET_NR_sigaction
8278 case TARGET_NR_sigaction:
8279 {
8280 #if defined(TARGET_ALPHA)
8281 struct target_sigaction act, oact, *pact = 0;
8282 struct target_old_sigaction *old_act;
8283 if (arg2) {
8284 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
8285 goto efault;
8286 act._sa_handler = old_act->_sa_handler;
8287 target_siginitset(&act.sa_mask, old_act->sa_mask);
8288 act.sa_flags = old_act->sa_flags;
8289 act.sa_restorer = 0;
8290 unlock_user_struct(old_act, arg2, 0);
8291 pact = &act;
8292 }
8293 ret = get_errno(do_sigaction(arg1, pact, &oact));
8294 if (!is_error(ret) && arg3) {
8295 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
8296 goto efault;
8297 old_act->_sa_handler = oact._sa_handler;
8298 old_act->sa_mask = oact.sa_mask.sig[0];
8299 old_act->sa_flags = oact.sa_flags;
8300 unlock_user_struct(old_act, arg3, 1);
8301 }
8302 #elif defined(TARGET_MIPS)
8303 struct target_sigaction act, oact, *pact, *old_act;
8304
8305 if (arg2) {
8306 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
8307 goto efault;
8308 act._sa_handler = old_act->_sa_handler;
8309 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
8310 act.sa_flags = old_act->sa_flags;
8311 unlock_user_struct(old_act, arg2, 0);
8312 pact = &act;
8313 } else {
8314 pact = NULL;
8315 }
8316
8317 ret = get_errno(do_sigaction(arg1, pact, &oact));
8318
8319 if (!is_error(ret) && arg3) {
8320 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
8321 goto efault;
8322 old_act->_sa_handler = oact._sa_handler;
8323 old_act->sa_flags = oact.sa_flags;
8324 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
8325 old_act->sa_mask.sig[1] = 0;
8326 old_act->sa_mask.sig[2] = 0;
8327 old_act->sa_mask.sig[3] = 0;
8328 unlock_user_struct(old_act, arg3, 1);
8329 }
8330 #else
8331 struct target_old_sigaction *old_act;
8332 struct target_sigaction act, oact, *pact;
8333 if (arg2) {
8334 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
8335 goto efault;
8336 act._sa_handler = old_act->_sa_handler;
8337 target_siginitset(&act.sa_mask, old_act->sa_mask);
8338 act.sa_flags = old_act->sa_flags;
8339 act.sa_restorer = old_act->sa_restorer;
8340 unlock_user_struct(old_act, arg2, 0);
8341 pact = &act;
8342 } else {
8343 pact = NULL;
8344 }
8345 ret = get_errno(do_sigaction(arg1, pact, &oact));
8346 if (!is_error(ret) && arg3) {
8347 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
8348 goto efault;
8349 old_act->_sa_handler = oact._sa_handler;
8350 old_act->sa_mask = oact.sa_mask.sig[0];
8351 old_act->sa_flags = oact.sa_flags;
8352 old_act->sa_restorer = oact.sa_restorer;
8353 unlock_user_struct(old_act, arg3, 1);
8354 }
8355 #endif
8356 }
8357 break;
8358 #endif
8359 case TARGET_NR_rt_sigaction:
8360 {
8361 #if defined(TARGET_ALPHA)
8362 struct target_sigaction act, oact, *pact = 0;
8363 struct target_rt_sigaction *rt_act;
8364
8365 if (arg4 != sizeof(target_sigset_t)) {
8366 ret = -TARGET_EINVAL;
8367 break;
8368 }
8369 if (arg2) {
8370 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
8371 goto efault;
8372 act._sa_handler = rt_act->_sa_handler;
8373 act.sa_mask = rt_act->sa_mask;
8374 act.sa_flags = rt_act->sa_flags;
8375 act.sa_restorer = arg5;
8376 unlock_user_struct(rt_act, arg2, 0);
8377 pact = &act;
8378 }
8379 ret = get_errno(do_sigaction(arg1, pact, &oact));
8380 if (!is_error(ret) && arg3) {
8381 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
8382 goto efault;
8383 rt_act->_sa_handler = oact._sa_handler;
8384 rt_act->sa_mask = oact.sa_mask;
8385 rt_act->sa_flags = oact.sa_flags;
8386 unlock_user_struct(rt_act, arg3, 1);
8387 }
8388 #else
8389 struct target_sigaction *act;
8390 struct target_sigaction *oact;
8391
8392 if (arg4 != sizeof(target_sigset_t)) {
8393 ret = -TARGET_EINVAL;
8394 break;
8395 }
8396 if (arg2) {
8397 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
8398 goto efault;
8399 } else
8400 act = NULL;
8401 if (arg3) {
8402 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
8403 ret = -TARGET_EFAULT;
8404 goto rt_sigaction_fail;
8405 }
8406 } else
8407 oact = NULL;
8408 ret = get_errno(do_sigaction(arg1, act, oact));
8409 rt_sigaction_fail:
8410 if (act)
8411 unlock_user_struct(act, arg2, 0);
8412 if (oact)
8413 unlock_user_struct(oact, arg3, 1);
8414 #endif
8415 }
8416 break;
8417 #ifdef TARGET_NR_sgetmask /* not on alpha */
8418 case TARGET_NR_sgetmask:
8419 {
8420 sigset_t cur_set;
8421 abi_ulong target_set;
8422 ret = do_sigprocmask(0, NULL, &cur_set);
8423 if (!ret) {
8424 host_to_target_old_sigset(&target_set, &cur_set);
8425 ret = target_set;
8426 }
8427 }
8428 break;
8429 #endif
8430 #ifdef TARGET_NR_ssetmask /* not on alpha */
8431 case TARGET_NR_ssetmask:
8432 {
8433 sigset_t set, oset, cur_set;
8434 abi_ulong target_set = arg1;
8435 /* We only have one word of the new mask so we must read
8436 * the rest of it with do_sigprocmask() and OR in this word.
8437 * We are guaranteed that a do_sigprocmask() that only queries
8438 * the signal mask will not fail.
8439 */
8440 ret = do_sigprocmask(0, NULL, &cur_set);
8441 assert(!ret);
8442 target_to_host_old_sigset(&set, &target_set);
8443 sigorset(&set, &set, &cur_set);
8444 ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
8445 if (!ret) {
8446 host_to_target_old_sigset(&target_set, &oset);
8447 ret = target_set;
8448 }
8449 }
8450 break;
8451 #endif
8452 #ifdef TARGET_NR_sigprocmask
8453 case TARGET_NR_sigprocmask:
8454 {
8455 #if defined(TARGET_ALPHA)
8456 sigset_t set, oldset;
8457 abi_ulong mask;
8458 int how;
8459
8460 switch (arg1) {
8461 case TARGET_SIG_BLOCK:
8462 how = SIG_BLOCK;
8463 break;
8464 case TARGET_SIG_UNBLOCK:
8465 how = SIG_UNBLOCK;
8466 break;
8467 case TARGET_SIG_SETMASK:
8468 how = SIG_SETMASK;
8469 break;
8470 default:
8471 ret = -TARGET_EINVAL;
8472 goto fail;
8473 }
8474 mask = arg2;
8475 target_to_host_old_sigset(&set, &mask);
8476
8477 ret = do_sigprocmask(how, &set, &oldset);
8478 if (!is_error(ret)) {
8479 host_to_target_old_sigset(&mask, &oldset);
8480 ret = mask;
8481 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
8482 }
8483 #else
8484 sigset_t set, oldset, *set_ptr;
8485 int how;
8486
8487 if (arg2) {
8488 switch (arg1) {
8489 case TARGET_SIG_BLOCK:
8490 how = SIG_BLOCK;
8491 break;
8492 case TARGET_SIG_UNBLOCK:
8493 how = SIG_UNBLOCK;
8494 break;
8495 case TARGET_SIG_SETMASK:
8496 how = SIG_SETMASK;
8497 break;
8498 default:
8499 ret = -TARGET_EINVAL;
8500 goto fail;
8501 }
8502 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
8503 goto efault;
8504 target_to_host_old_sigset(&set, p);
8505 unlock_user(p, arg2, 0);
8506 set_ptr = &set;
8507 } else {
8508 how = 0;
8509 set_ptr = NULL;
8510 }
8511 ret = do_sigprocmask(how, set_ptr, &oldset);
8512 if (!is_error(ret) && arg3) {
8513 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
8514 goto efault;
8515 host_to_target_old_sigset(p, &oldset);
8516 unlock_user(p, arg3, sizeof(target_sigset_t));
8517 }
8518 #endif
8519 }
8520 break;
8521 #endif
8522 case TARGET_NR_rt_sigprocmask:
8523 {
8524 int how = arg1;
8525 sigset_t set, oldset, *set_ptr;
8526
8527 if (arg4 != sizeof(target_sigset_t)) {
8528 ret = -TARGET_EINVAL;
8529 break;
8530 }
8531
8532 if (arg2) {
8533 switch(how) {
8534 case TARGET_SIG_BLOCK:
8535 how = SIG_BLOCK;
8536 break;
8537 case TARGET_SIG_UNBLOCK:
8538 how = SIG_UNBLOCK;
8539 break;
8540 case TARGET_SIG_SETMASK:
8541 how = SIG_SETMASK;
8542 break;
8543 default:
8544 ret = -TARGET_EINVAL;
8545 goto fail;
8546 }
8547 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
8548 goto efault;
8549 target_to_host_sigset(&set, p);
8550 unlock_user(p, arg2, 0);
8551 set_ptr = &set;
8552 } else {
8553 how = 0;
8554 set_ptr = NULL;
8555 }
8556 ret = do_sigprocmask(how, set_ptr, &oldset);
8557 if (!is_error(ret) && arg3) {
8558 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
8559 goto efault;
8560 host_to_target_sigset(p, &oldset);
8561 unlock_user(p, arg3, sizeof(target_sigset_t));
8562 }
8563 }
8564 break;
8565 #ifdef TARGET_NR_sigpending
8566 case TARGET_NR_sigpending:
8567 {
8568 sigset_t set;
8569 ret = get_errno(sigpending(&set));
8570 if (!is_error(ret)) {
8571 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
8572 goto efault;
8573 host_to_target_old_sigset(p, &set);
8574 unlock_user(p, arg1, sizeof(target_sigset_t));
8575 }
8576 }
8577 break;
8578 #endif
8579 case TARGET_NR_rt_sigpending:
8580 {
8581 sigset_t set;
8582
8583 /* Yes, this check is >, not != like most. We follow the kernel's
8584 * logic and it does it like this because it implements
8585 * NR_sigpending through the same code path, and in that case
8586 * the old_sigset_t is smaller in size.
8587 */
8588 if (arg2 > sizeof(target_sigset_t)) {
8589 ret = -TARGET_EINVAL;
8590 break;
8591 }
8592
8593 ret = get_errno(sigpending(&set));
8594 if (!is_error(ret)) {
8595 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
8596 goto efault;
8597 host_to_target_sigset(p, &set);
8598 unlock_user(p, arg1, sizeof(target_sigset_t));
8599 }
8600 }
8601 break;
8602 #ifdef TARGET_NR_sigsuspend
8603 case TARGET_NR_sigsuspend:
8604 {
8605 TaskState *ts = cpu->opaque;
8606 #if defined(TARGET_ALPHA)
8607 abi_ulong mask = arg1;
8608 target_to_host_old_sigset(&ts->sigsuspend_mask, &mask);
8609 #else
8610 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
8611 goto efault;
8612 target_to_host_old_sigset(&ts->sigsuspend_mask, p);
8613 unlock_user(p, arg1, 0);
8614 #endif
8615 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
8616 SIGSET_T_SIZE));
8617 if (ret != -TARGET_ERESTARTSYS) {
8618 ts->in_sigsuspend = 1;
8619 }
8620 }
8621 break;
8622 #endif
8623 case TARGET_NR_rt_sigsuspend:
8624 {
8625 TaskState *ts = cpu->opaque;
8626
8627 if (arg2 != sizeof(target_sigset_t)) {
8628 ret = -TARGET_EINVAL;
8629 break;
8630 }
8631 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
8632 goto efault;
8633 target_to_host_sigset(&ts->sigsuspend_mask, p);
8634 unlock_user(p, arg1, 0);
8635 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
8636 SIGSET_T_SIZE));
8637 if (ret != -TARGET_ERESTARTSYS) {
8638 ts->in_sigsuspend = 1;
8639 }
8640 }
8641 break;
8642 case TARGET_NR_rt_sigtimedwait:
8643 {
8644 sigset_t set;
8645 struct timespec uts, *puts;
8646 siginfo_t uinfo;
8647
8648 if (arg4 != sizeof(target_sigset_t)) {
8649 ret = -TARGET_EINVAL;
8650 break;
8651 }
8652
8653 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
8654 goto efault;
8655 target_to_host_sigset(&set, p);
8656 unlock_user(p, arg1, 0);
8657 if (arg3) {
8658 puts = &uts;
8659 target_to_host_timespec(puts, arg3);
8660 } else {
8661 puts = NULL;
8662 }
8663 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
8664 SIGSET_T_SIZE));
8665 if (!is_error(ret)) {
8666 if (arg2) {
8667 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
8668 0);
8669 if (!p) {
8670 goto efault;
8671 }
8672 host_to_target_siginfo(p, &uinfo);
8673 unlock_user(p, arg2, sizeof(target_siginfo_t));
8674 }
8675 ret = host_to_target_signal(ret);
8676 }
8677 }
8678 break;
8679 case TARGET_NR_rt_sigqueueinfo:
8680 {
8681 siginfo_t uinfo;
8682
8683 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
8684 if (!p) {
8685 goto efault;
8686 }
8687 target_to_host_siginfo(&uinfo, p);
8688 unlock_user(p, arg1, 0);
8689 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
8690 }
8691 break;
8692 #ifdef TARGET_NR_sigreturn
8693 case TARGET_NR_sigreturn:
8694 if (block_signals()) {
8695 ret = -TARGET_ERESTARTSYS;
8696 } else {
8697 ret = do_sigreturn(cpu_env);
8698 }
8699 break;
8700 #endif
8701 case TARGET_NR_rt_sigreturn:
8702 if (block_signals()) {
8703 ret = -TARGET_ERESTARTSYS;
8704 } else {
8705 ret = do_rt_sigreturn(cpu_env);
8706 }
8707 break;
8708 case TARGET_NR_sethostname:
8709 if (!(p = lock_user_string(arg1)))
8710 goto efault;
8711 ret = get_errno(sethostname(p, arg2));
8712 unlock_user(p, arg1, 0);
8713 break;
8714 case TARGET_NR_setrlimit:
8715 {
8716 int resource = target_to_host_resource(arg1);
8717 struct target_rlimit *target_rlim;
8718 struct rlimit rlim;
8719 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
8720 goto efault;
8721 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
8722 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
8723 unlock_user_struct(target_rlim, arg2, 0);
8724 ret = get_errno(setrlimit(resource, &rlim));
8725 }
8726 break;
8727 case TARGET_NR_getrlimit:
8728 {
8729 int resource = target_to_host_resource(arg1);
8730 struct target_rlimit *target_rlim;
8731 struct rlimit rlim;
8732
8733 ret = get_errno(getrlimit(resource, &rlim));
8734 if (!is_error(ret)) {
8735 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
8736 goto efault;
8737 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
8738 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
8739 unlock_user_struct(target_rlim, arg2, 1);
8740 }
8741 }
8742 break;
8743 case TARGET_NR_getrusage:
8744 {
8745 struct rusage rusage;
8746 ret = get_errno(getrusage(arg1, &rusage));
8747 if (!is_error(ret)) {
8748 ret = host_to_target_rusage(arg2, &rusage);
8749 }
8750 }
8751 break;
8752 case TARGET_NR_gettimeofday:
8753 {
8754 struct timeval tv;
8755 ret = get_errno(gettimeofday(&tv, NULL));
8756 if (!is_error(ret)) {
8757 if (copy_to_user_timeval(arg1, &tv))
8758 goto efault;
8759 }
8760 }
8761 break;
8762 case TARGET_NR_settimeofday:
8763 {
8764 struct timeval tv, *ptv = NULL;
8765 struct timezone tz, *ptz = NULL;
8766
8767 if (arg1) {
8768 if (copy_from_user_timeval(&tv, arg1)) {
8769 goto efault;
8770 }
8771 ptv = &tv;
8772 }
8773
8774 if (arg2) {
8775 if (copy_from_user_timezone(&tz, arg2)) {
8776 goto efault;
8777 }
8778 ptz = &tz;
8779 }
8780
8781 ret = get_errno(settimeofday(ptv, ptz));
8782 }
8783 break;
8784 #if defined(TARGET_NR_select)
8785 case TARGET_NR_select:
8786 #if defined(TARGET_WANT_NI_OLD_SELECT)
8787 /* some architectures used to have old_select here
8788 * but now ENOSYS it.
8789 */
8790 ret = -TARGET_ENOSYS;
8791 #elif defined(TARGET_WANT_OLD_SYS_SELECT)
8792 ret = do_old_select(arg1);
8793 #else
8794 ret = do_select(arg1, arg2, arg3, arg4, arg5);
8795 #endif
8796 break;
8797 #endif
8798 #ifdef TARGET_NR_pselect6
8799 case TARGET_NR_pselect6:
8800 {
8801 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
8802 fd_set rfds, wfds, efds;
8803 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
8804 struct timespec ts, *ts_ptr;
8805
8806 /*
8807 * The 6th arg is actually two args smashed together,
8808 * so we cannot use the C library.
8809 */
8810 sigset_t set;
8811 struct {
8812 sigset_t *set;
8813 size_t size;
8814 } sig, *sig_ptr;
8815
8816 abi_ulong arg_sigset, arg_sigsize, *arg7;
8817 target_sigset_t *target_sigset;
8818
8819 n = arg1;
8820 rfd_addr = arg2;
8821 wfd_addr = arg3;
8822 efd_addr = arg4;
8823 ts_addr = arg5;
8824
8825 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
8826 if (ret) {
8827 goto fail;
8828 }
8829 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
8830 if (ret) {
8831 goto fail;
8832 }
8833 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
8834 if (ret) {
8835 goto fail;
8836 }
8837
8838 /*
8839 * This takes a timespec, and not a timeval, so we cannot
8840 * use the do_select() helper ...
8841 */
8842 if (ts_addr) {
8843 if (target_to_host_timespec(&ts, ts_addr)) {
8844 goto efault;
8845 }
8846 ts_ptr = &ts;
8847 } else {
8848 ts_ptr = NULL;
8849 }
8850
8851 /* Extract the two packed args for the sigset */
8852 if (arg6) {
8853 sig_ptr = &sig;
8854 sig.size = SIGSET_T_SIZE;
8855
8856 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
8857 if (!arg7) {
8858 goto efault;
8859 }
8860 arg_sigset = tswapal(arg7[0]);
8861 arg_sigsize = tswapal(arg7[1]);
8862 unlock_user(arg7, arg6, 0);
8863
8864 if (arg_sigset) {
8865 sig.set = &set;
8866 if (arg_sigsize != sizeof(*target_sigset)) {
8867 /* Like the kernel, we enforce correct size sigsets */
8868 ret = -TARGET_EINVAL;
8869 goto fail;
8870 }
8871 target_sigset = lock_user(VERIFY_READ, arg_sigset,
8872 sizeof(*target_sigset), 1);
8873 if (!target_sigset) {
8874 goto efault;
8875 }
8876 target_to_host_sigset(&set, target_sigset);
8877 unlock_user(target_sigset, arg_sigset, 0);
8878 } else {
8879 sig.set = NULL;
8880 }
8881 } else {
8882 sig_ptr = NULL;
8883 }
8884
8885 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
8886 ts_ptr, sig_ptr));
8887
8888 if (!is_error(ret)) {
8889 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
8890 goto efault;
8891 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
8892 goto efault;
8893 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
8894 goto efault;
8895
8896 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
8897 goto efault;
8898 }
8899 }
8900 break;
8901 #endif
8902 #ifdef TARGET_NR_symlink
8903 case TARGET_NR_symlink:
8904 {
8905 void *p2;
8906 p = lock_user_string(arg1);
8907 p2 = lock_user_string(arg2);
8908 if (!p || !p2)
8909 ret = -TARGET_EFAULT;
8910 else
8911 ret = get_errno(symlink(p, p2));
8912 unlock_user(p2, arg2, 0);
8913 unlock_user(p, arg1, 0);
8914 }
8915 break;
8916 #endif
8917 #if defined(TARGET_NR_symlinkat)
8918 case TARGET_NR_symlinkat:
8919 {
8920 void *p2;
8921 p = lock_user_string(arg1);
8922 p2 = lock_user_string(arg3);
8923 if (!p || !p2)
8924 ret = -TARGET_EFAULT;
8925 else
8926 ret = get_errno(symlinkat(p, arg2, p2));
8927 unlock_user(p2, arg3, 0);
8928 unlock_user(p, arg1, 0);
8929 }
8930 break;
8931 #endif
8932 #ifdef TARGET_NR_oldlstat
8933 case TARGET_NR_oldlstat:
8934 goto unimplemented;
8935 #endif
8936 #ifdef TARGET_NR_readlink
8937 case TARGET_NR_readlink:
8938 {
8939 void *p2;
8940 p = lock_user_string(arg1);
8941 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8942 if (!p || !p2) {
8943 ret = -TARGET_EFAULT;
8944 } else if (!arg3) {
8945 /* Short circuit this for the magic exe check. */
8946 ret = -TARGET_EINVAL;
8947 } else if (is_proc_myself((const char *)p, "exe")) {
8948 char real[PATH_MAX], *temp;
8949 temp = realpath(exec_path, real);
8950 /* Return value is # of bytes that we wrote to the buffer. */
8951 if (temp == NULL) {
8952 ret = get_errno(-1);
8953 } else {
8954 /* Don't worry about sign mismatch as earlier mapping
8955 * logic would have thrown a bad address error. */
8956 ret = MIN(strlen(real), arg3);
8957 /* We cannot NUL terminate the string. */
8958 memcpy(p2, real, ret);
8959 }
8960 } else {
8961 ret = get_errno(readlink(path(p), p2, arg3));
8962 }
8963 unlock_user(p2, arg2, ret);
8964 unlock_user(p, arg1, 0);
8965 }
8966 break;
8967 #endif
8968 #if defined(TARGET_NR_readlinkat)
8969 case TARGET_NR_readlinkat:
8970 {
8971 void *p2;
8972 p = lock_user_string(arg2);
8973 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8974 if (!p || !p2) {
8975 ret = -TARGET_EFAULT;
8976 } else if (is_proc_myself((const char *)p, "exe")) {
8977 char real[PATH_MAX], *temp;
8978 temp = realpath(exec_path, real);
8979 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
8980 snprintf((char *)p2, arg4, "%s", real);
8981 } else {
8982 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
8983 }
8984 unlock_user(p2, arg3, ret);
8985 unlock_user(p, arg2, 0);
8986 }
8987 break;
8988 #endif
8989 #ifdef TARGET_NR_uselib
8990 case TARGET_NR_uselib:
8991 goto unimplemented;
8992 #endif
8993 #ifdef TARGET_NR_swapon
8994 case TARGET_NR_swapon:
8995 if (!(p = lock_user_string(arg1)))
8996 goto efault;
8997 ret = get_errno(swapon(p, arg2));
8998 unlock_user(p, arg1, 0);
8999 break;
9000 #endif
9001 case TARGET_NR_reboot:
9002 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
9003 /* arg4 must be ignored in all other cases */
9004 p = lock_user_string(arg4);
9005 if (!p) {
9006 goto efault;
9007 }
9008 ret = get_errno(reboot(arg1, arg2, arg3, p));
9009 unlock_user(p, arg4, 0);
9010 } else {
9011 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
9012 }
9013 break;
9014 #ifdef TARGET_NR_readdir
9015 case TARGET_NR_readdir:
9016 goto unimplemented;
9017 #endif
9018 #ifdef TARGET_NR_mmap
9019 case TARGET_NR_mmap:
9020 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
9021 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
9022 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
9023 || defined(TARGET_S390X)
9024 {
9025 abi_ulong *v;
9026 abi_ulong v1, v2, v3, v4, v5, v6;
9027 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
9028 goto efault;
9029 v1 = tswapal(v[0]);
9030 v2 = tswapal(v[1]);
9031 v3 = tswapal(v[2]);
9032 v4 = tswapal(v[3]);
9033 v5 = tswapal(v[4]);
9034 v6 = tswapal(v[5]);
9035 unlock_user(v, arg1, 0);
9036 ret = get_errno(target_mmap(v1, v2, v3,
9037 target_to_host_bitmask(v4, mmap_flags_tbl),
9038 v5, v6));
9039 }
9040 #else
9041 ret = get_errno(target_mmap(arg1, arg2, arg3,
9042 target_to_host_bitmask(arg4, mmap_flags_tbl),
9043 arg5,
9044 arg6));
9045 #endif
9046 break;
9047 #endif
9048 #ifdef TARGET_NR_mmap2
9049 case TARGET_NR_mmap2:
9050 #ifndef MMAP_SHIFT
9051 #define MMAP_SHIFT 12
9052 #endif
9053 ret = get_errno(target_mmap(arg1, arg2, arg3,
9054 target_to_host_bitmask(arg4, mmap_flags_tbl),
9055 arg5,
9056 arg6 << MMAP_SHIFT));
9057 break;
9058 #endif
9059 case TARGET_NR_munmap:
9060 ret = get_errno(target_munmap(arg1, arg2));
9061 break;
9062 case TARGET_NR_mprotect:
9063 {
9064 TaskState *ts = cpu->opaque;
9065 /* Special hack to detect libc making the stack executable. */
9066 if ((arg3 & PROT_GROWSDOWN)
9067 && arg1 >= ts->info->stack_limit
9068 && arg1 <= ts->info->start_stack) {
9069 arg3 &= ~PROT_GROWSDOWN;
9070 arg2 = arg2 + arg1 - ts->info->stack_limit;
9071 arg1 = ts->info->stack_limit;
9072 }
9073 }
9074 ret = get_errno(target_mprotect(arg1, arg2, arg3));
9075 break;
9076 #ifdef TARGET_NR_mremap
9077 case TARGET_NR_mremap:
9078 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
9079 break;
9080 #endif
9081 /* ??? msync/mlock/munlock are broken for softmmu. */
9082 #ifdef TARGET_NR_msync
9083 case TARGET_NR_msync:
9084 ret = get_errno(msync(g2h(arg1), arg2, arg3));
9085 break;
9086 #endif
9087 #ifdef TARGET_NR_mlock
9088 case TARGET_NR_mlock:
9089 ret = get_errno(mlock(g2h(arg1), arg2));
9090 break;
9091 #endif
9092 #ifdef TARGET_NR_munlock
9093 case TARGET_NR_munlock:
9094 ret = get_errno(munlock(g2h(arg1), arg2));
9095 break;
9096 #endif
9097 #ifdef TARGET_NR_mlockall
9098 case TARGET_NR_mlockall:
9099 ret = get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
9100 break;
9101 #endif
9102 #ifdef TARGET_NR_munlockall
9103 case TARGET_NR_munlockall:
9104 ret = get_errno(munlockall());
9105 break;
9106 #endif
9107 case TARGET_NR_truncate:
9108 if (!(p = lock_user_string(arg1)))
9109 goto efault;
9110 ret = get_errno(truncate(p, arg2));
9111 unlock_user(p, arg1, 0);
9112 break;
9113 case TARGET_NR_ftruncate:
9114 ret = get_errno(ftruncate(arg1, arg2));
9115 break;
9116 case TARGET_NR_fchmod:
9117 ret = get_errno(fchmod(arg1, arg2));
9118 break;
9119 #if defined(TARGET_NR_fchmodat)
9120 case TARGET_NR_fchmodat:
9121 if (!(p = lock_user_string(arg2)))
9122 goto efault;
9123 ret = get_errno(fchmodat(arg1, p, arg3, 0));
9124 unlock_user(p, arg2, 0);
9125 break;
9126 #endif
9127 case TARGET_NR_getpriority:
9128 /* Note that negative values are valid for getpriority, so we must
9129 differentiate based on errno settings. */
9130 errno = 0;
9131 ret = getpriority(arg1, arg2);
9132 if (ret == -1 && errno != 0) {
9133 ret = -host_to_target_errno(errno);
9134 break;
9135 }
9136 #ifdef TARGET_ALPHA
9137 /* Return value is the unbiased priority. Signal no error. */
9138 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
9139 #else
9140 /* Return value is a biased priority to avoid negative numbers. */
9141 ret = 20 - ret;
9142 #endif
9143 break;
9144 case TARGET_NR_setpriority:
9145 ret = get_errno(setpriority(arg1, arg2, arg3));
9146 break;
9147 #ifdef TARGET_NR_profil
9148 case TARGET_NR_profil:
9149 goto unimplemented;
9150 #endif
9151 case TARGET_NR_statfs:
9152 if (!(p = lock_user_string(arg1)))
9153 goto efault;
9154 ret = get_errno(statfs(path(p), &stfs));
9155 unlock_user(p, arg1, 0);
9156 convert_statfs:
9157 if (!is_error(ret)) {
9158 struct target_statfs *target_stfs;
9159
9160 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
9161 goto efault;
9162 __put_user(stfs.f_type, &target_stfs->f_type);
9163 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
9164 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
9165 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
9166 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
9167 __put_user(stfs.f_files, &target_stfs->f_files);
9168 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
9169 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
9170 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
9171 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
9172 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
9173 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
9174 unlock_user_struct(target_stfs, arg2, 1);
9175 }
9176 break;
9177 case TARGET_NR_fstatfs:
9178 ret = get_errno(fstatfs(arg1, &stfs));
9179 goto convert_statfs;
9180 #ifdef TARGET_NR_statfs64
9181 case TARGET_NR_statfs64:
9182 if (!(p = lock_user_string(arg1)))
9183 goto efault;
9184 ret = get_errno(statfs(path(p), &stfs));
9185 unlock_user(p, arg1, 0);
9186 convert_statfs64:
9187 if (!is_error(ret)) {
9188 struct target_statfs64 *target_stfs;
9189
9190 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
9191 goto efault;
9192 __put_user(stfs.f_type, &target_stfs->f_type);
9193 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
9194 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
9195 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
9196 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
9197 __put_user(stfs.f_files, &target_stfs->f_files);
9198 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
9199 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
9200 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
9201 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
9202 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
9203 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
9204 unlock_user_struct(target_stfs, arg3, 1);
9205 }
9206 break;
9207 case TARGET_NR_fstatfs64:
9208 ret = get_errno(fstatfs(arg1, &stfs));
9209 goto convert_statfs64;
9210 #endif
9211 #ifdef TARGET_NR_ioperm
9212 case TARGET_NR_ioperm:
9213 goto unimplemented;
9214 #endif
9215 #ifdef TARGET_NR_socketcall
9216 case TARGET_NR_socketcall:
9217 ret = do_socketcall(arg1, arg2);
9218 break;
9219 #endif
9220 #ifdef TARGET_NR_accept
9221 case TARGET_NR_accept:
9222 ret = do_accept4(arg1, arg2, arg3, 0);
9223 break;
9224 #endif
9225 #ifdef TARGET_NR_accept4
9226 case TARGET_NR_accept4:
9227 ret = do_accept4(arg1, arg2, arg3, arg4);
9228 break;
9229 #endif
9230 #ifdef TARGET_NR_bind
9231 case TARGET_NR_bind:
9232 ret = do_bind(arg1, arg2, arg3);
9233 break;
9234 #endif
9235 #ifdef TARGET_NR_connect
9236 case TARGET_NR_connect:
9237 ret = do_connect(arg1, arg2, arg3);
9238 break;
9239 #endif
9240 #ifdef TARGET_NR_getpeername
9241 case TARGET_NR_getpeername:
9242 ret = do_getpeername(arg1, arg2, arg3);
9243 break;
9244 #endif
9245 #ifdef TARGET_NR_getsockname
9246 case TARGET_NR_getsockname:
9247 ret = do_getsockname(arg1, arg2, arg3);
9248 break;
9249 #endif
9250 #ifdef TARGET_NR_getsockopt
9251 case TARGET_NR_getsockopt:
9252 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
9253 break;
9254 #endif
9255 #ifdef TARGET_NR_listen
9256 case TARGET_NR_listen:
9257 ret = get_errno(listen(arg1, arg2));
9258 break;
9259 #endif
9260 #ifdef TARGET_NR_recv
9261 case TARGET_NR_recv:
9262 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
9263 break;
9264 #endif
9265 #ifdef TARGET_NR_recvfrom
9266 case TARGET_NR_recvfrom:
9267 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
9268 break;
9269 #endif
9270 #ifdef TARGET_NR_recvmsg
9271 case TARGET_NR_recvmsg:
9272 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
9273 break;
9274 #endif
9275 #ifdef TARGET_NR_send
9276 case TARGET_NR_send:
9277 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
9278 break;
9279 #endif
9280 #ifdef TARGET_NR_sendmsg
9281 case TARGET_NR_sendmsg:
9282 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
9283 break;
9284 #endif
9285 #ifdef TARGET_NR_sendmmsg
9286 case TARGET_NR_sendmmsg:
9287 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
9288 break;
9289 case TARGET_NR_recvmmsg:
9290 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
9291 break;
9292 #endif
9293 #ifdef TARGET_NR_sendto
9294 case TARGET_NR_sendto:
9295 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
9296 break;
9297 #endif
9298 #ifdef TARGET_NR_shutdown
9299 case TARGET_NR_shutdown:
9300 ret = get_errno(shutdown(arg1, arg2));
9301 break;
9302 #endif
9303 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
9304 case TARGET_NR_getrandom:
9305 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
9306 if (!p) {
9307 goto efault;
9308 }
9309 ret = get_errno(getrandom(p, arg2, arg3));
9310 unlock_user(p, arg1, ret);
9311 break;
9312 #endif
9313 #ifdef TARGET_NR_socket
9314 case TARGET_NR_socket:
9315 ret = do_socket(arg1, arg2, arg3);
9316 fd_trans_unregister(ret);
9317 break;
9318 #endif
9319 #ifdef TARGET_NR_socketpair
9320 case TARGET_NR_socketpair:
9321 ret = do_socketpair(arg1, arg2, arg3, arg4);
9322 break;
9323 #endif
9324 #ifdef TARGET_NR_setsockopt
9325 case TARGET_NR_setsockopt:
9326 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
9327 break;
9328 #endif
9329 #if defined(TARGET_NR_syslog)
9330 case TARGET_NR_syslog:
9331 {
9332 int len = arg2;
9333
9334 switch (arg1) {
9335 case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
9336 case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
9337 case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
9338 case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
9339 case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
9340 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
9341 case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
9342 case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
9343 {
9344 ret = get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
9345 }
9346 break;
9347 case TARGET_SYSLOG_ACTION_READ: /* Read from log */
9348 case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
9349 case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
9350 {
9351 ret = -TARGET_EINVAL;
9352 if (len < 0) {
9353 goto fail;
9354 }
9355 ret = 0;
9356 if (len == 0) {
9357 break;
9358 }
9359 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
9360 if (!p) {
9361 ret = -TARGET_EFAULT;
9362 goto fail;
9363 }
9364 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
9365 unlock_user(p, arg2, arg3);
9366 }
9367 break;
9368 default:
9369 ret = -EINVAL;
9370 break;
9371 }
9372 }
9373 break;
9374 #endif
9375 case TARGET_NR_setitimer:
9376 {
9377 struct itimerval value, ovalue, *pvalue;
9378
9379 if (arg2) {
9380 pvalue = &value;
9381 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
9382 || copy_from_user_timeval(&pvalue->it_value,
9383 arg2 + sizeof(struct target_timeval)))
9384 goto efault;
9385 } else {
9386 pvalue = NULL;
9387 }
9388 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
9389 if (!is_error(ret) && arg3) {
9390 if (copy_to_user_timeval(arg3,
9391 &ovalue.it_interval)
9392 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
9393 &ovalue.it_value))
9394 goto efault;
9395 }
9396 }
9397 break;
9398 case TARGET_NR_getitimer:
9399 {
9400 struct itimerval value;
9401
9402 ret = get_errno(getitimer(arg1, &value));
9403 if (!is_error(ret) && arg2) {
9404 if (copy_to_user_timeval(arg2,
9405 &value.it_interval)
9406 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
9407 &value.it_value))
9408 goto efault;
9409 }
9410 }
9411 break;
9412 #ifdef TARGET_NR_stat
9413 case TARGET_NR_stat:
9414 if (!(p = lock_user_string(arg1)))
9415 goto efault;
9416 ret = get_errno(stat(path(p), &st));
9417 unlock_user(p, arg1, 0);
9418 goto do_stat;
9419 #endif
9420 #ifdef TARGET_NR_lstat
9421 case TARGET_NR_lstat:
9422 if (!(p = lock_user_string(arg1)))
9423 goto efault;
9424 ret = get_errno(lstat(path(p), &st));
9425 unlock_user(p, arg1, 0);
9426 goto do_stat;
9427 #endif
9428 case TARGET_NR_fstat:
9429 {
9430 ret = get_errno(fstat(arg1, &st));
9431 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
9432 do_stat:
9433 #endif
9434 if (!is_error(ret)) {
9435 struct target_stat *target_st;
9436
9437 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
9438 goto efault;
9439 memset(target_st, 0, sizeof(*target_st));
9440 __put_user(st.st_dev, &target_st->st_dev);
9441 __put_user(st.st_ino, &target_st->st_ino);
9442 __put_user(st.st_mode, &target_st->st_mode);
9443 __put_user(st.st_uid, &target_st->st_uid);
9444 __put_user(st.st_gid, &target_st->st_gid);
9445 __put_user(st.st_nlink, &target_st->st_nlink);
9446 __put_user(st.st_rdev, &target_st->st_rdev);
9447 __put_user(st.st_size, &target_st->st_size);
9448 __put_user(st.st_blksize, &target_st->st_blksize);
9449 __put_user(st.st_blocks, &target_st->st_blocks);
9450 __put_user(st.st_atime, &target_st->target_st_atime);
9451 __put_user(st.st_mtime, &target_st->target_st_mtime);
9452 __put_user(st.st_ctime, &target_st->target_st_ctime);
9453 unlock_user_struct(target_st, arg2, 1);
9454 }
9455 }
9456 break;
9457 #ifdef TARGET_NR_olduname
9458 case TARGET_NR_olduname:
9459 goto unimplemented;
9460 #endif
9461 #ifdef TARGET_NR_iopl
9462 case TARGET_NR_iopl:
9463 goto unimplemented;
9464 #endif
9465 case TARGET_NR_vhangup:
9466 ret = get_errno(vhangup());
9467 break;
9468 #ifdef TARGET_NR_idle
9469 case TARGET_NR_idle:
9470 goto unimplemented;
9471 #endif
9472 #ifdef TARGET_NR_syscall
9473 case TARGET_NR_syscall:
9474 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
9475 arg6, arg7, arg8, 0);
9476 break;
9477 #endif
9478 case TARGET_NR_wait4:
9479 {
9480 int status;
9481 abi_long status_ptr = arg2;
9482 struct rusage rusage, *rusage_ptr;
9483 abi_ulong target_rusage = arg4;
9484 abi_long rusage_err;
9485 if (target_rusage)
9486 rusage_ptr = &rusage;
9487 else
9488 rusage_ptr = NULL;
9489 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
9490 if (!is_error(ret)) {
9491 if (status_ptr && ret) {
9492 status = host_to_target_waitstatus(status);
9493 if (put_user_s32(status, status_ptr))
9494 goto efault;
9495 }
9496 if (target_rusage) {
9497 rusage_err = host_to_target_rusage(target_rusage, &rusage);
9498 if (rusage_err) {
9499 ret = rusage_err;
9500 }
9501 }
9502 }
9503 }
9504 break;
9505 #ifdef TARGET_NR_swapoff
9506 case TARGET_NR_swapoff:
9507 if (!(p = lock_user_string(arg1)))
9508 goto efault;
9509 ret = get_errno(swapoff(p));
9510 unlock_user(p, arg1, 0);
9511 break;
9512 #endif
9513 case TARGET_NR_sysinfo:
9514 {
9515 struct target_sysinfo *target_value;
9516 struct sysinfo value;
9517 ret = get_errno(sysinfo(&value));
9518 if (!is_error(ret) && arg1)
9519 {
9520 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
9521 goto efault;
9522 __put_user(value.uptime, &target_value->uptime);
9523 __put_user(value.loads[0], &target_value->loads[0]);
9524 __put_user(value.loads[1], &target_value->loads[1]);
9525 __put_user(value.loads[2], &target_value->loads[2]);
9526 __put_user(value.totalram, &target_value->totalram);
9527 __put_user(value.freeram, &target_value->freeram);
9528 __put_user(value.sharedram, &target_value->sharedram);
9529 __put_user(value.bufferram, &target_value->bufferram);
9530 __put_user(value.totalswap, &target_value->totalswap);
9531 __put_user(value.freeswap, &target_value->freeswap);
9532 __put_user(value.procs, &target_value->procs);
9533 __put_user(value.totalhigh, &target_value->totalhigh);
9534 __put_user(value.freehigh, &target_value->freehigh);
9535 __put_user(value.mem_unit, &target_value->mem_unit);
9536 unlock_user_struct(target_value, arg1, 1);
9537 }
9538 }
9539 break;
9540 #ifdef TARGET_NR_ipc
9541 case TARGET_NR_ipc:
9542 ret = do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
9543 break;
9544 #endif
9545 #ifdef TARGET_NR_semget
9546 case TARGET_NR_semget:
9547 ret = get_errno(semget(arg1, arg2, arg3));
9548 break;
9549 #endif
9550 #ifdef TARGET_NR_semop
9551 case TARGET_NR_semop:
9552 ret = do_semop(arg1, arg2, arg3);
9553 break;
9554 #endif
9555 #ifdef TARGET_NR_semctl
9556 case TARGET_NR_semctl:
9557 ret = do_semctl(arg1, arg2, arg3, arg4);
9558 break;
9559 #endif
9560 #ifdef TARGET_NR_msgctl
9561 case TARGET_NR_msgctl:
9562 ret = do_msgctl(arg1, arg2, arg3);
9563 break;
9564 #endif
9565 #ifdef TARGET_NR_msgget
9566 case TARGET_NR_msgget:
9567 ret = get_errno(msgget(arg1, arg2));
9568 break;
9569 #endif
9570 #ifdef TARGET_NR_msgrcv
9571 case TARGET_NR_msgrcv:
9572 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
9573 break;
9574 #endif
9575 #ifdef TARGET_NR_msgsnd
9576 case TARGET_NR_msgsnd:
9577 ret = do_msgsnd(arg1, arg2, arg3, arg4);
9578 break;
9579 #endif
9580 #ifdef TARGET_NR_shmget
9581 case TARGET_NR_shmget:
9582 ret = get_errno(shmget(arg1, arg2, arg3));
9583 break;
9584 #endif
9585 #ifdef TARGET_NR_shmctl
9586 case TARGET_NR_shmctl:
9587 ret = do_shmctl(arg1, arg2, arg3);
9588 break;
9589 #endif
9590 #ifdef TARGET_NR_shmat
9591 case TARGET_NR_shmat:
9592 ret = do_shmat(cpu_env, arg1, arg2, arg3);
9593 break;
9594 #endif
9595 #ifdef TARGET_NR_shmdt
9596 case TARGET_NR_shmdt:
9597 ret = do_shmdt(arg1);
9598 break;
9599 #endif
9600 case TARGET_NR_fsync:
9601 ret = get_errno(fsync(arg1));
9602 break;
9603 case TARGET_NR_clone:
9604 /* Linux manages to have three different orderings for its
9605 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
9606 * match the kernel's CONFIG_CLONE_* settings.
9607 * Microblaze is further special in that it uses a sixth
9608 * implicit argument to clone for the TLS pointer.
9609 */
9610 #if defined(TARGET_MICROBLAZE)
9611 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
9612 #elif defined(TARGET_CLONE_BACKWARDS)
9613 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
9614 #elif defined(TARGET_CLONE_BACKWARDS2)
9615 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
9616 #else
9617 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
9618 #endif
9619 break;
9620 #ifdef __NR_exit_group
9621 /* new thread calls */
9622 case TARGET_NR_exit_group:
9623 #ifdef TARGET_GPROF
9624 _mcleanup();
9625 #endif
9626 gdb_exit(cpu_env, arg1);
9627 ret = get_errno(exit_group(arg1));
9628 break;
9629 #endif
9630 case TARGET_NR_setdomainname:
9631 if (!(p = lock_user_string(arg1)))
9632 goto efault;
9633 ret = get_errno(setdomainname(p, arg2));
9634 unlock_user(p, arg1, 0);
9635 break;
9636 case TARGET_NR_uname:
9637 /* no need to transcode because we use the linux syscall */
9638 {
9639 struct new_utsname * buf;
9640
9641 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
9642 goto efault;
9643 ret = get_errno(sys_uname(buf));
9644 if (!is_error(ret)) {
9645 /* Overwrite the native machine name with whatever is being
9646 emulated. */
9647 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
9648 /* Allow the user to override the reported release. */
9649 if (qemu_uname_release && *qemu_uname_release) {
9650 g_strlcpy(buf->release, qemu_uname_release,
9651 sizeof(buf->release));
9652 }
9653 }
9654 unlock_user_struct(buf, arg1, 1);
9655 }
9656 break;
9657 #ifdef TARGET_I386
9658 case TARGET_NR_modify_ldt:
9659 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
9660 break;
9661 #if !defined(TARGET_X86_64)
9662 case TARGET_NR_vm86old:
9663 goto unimplemented;
9664 case TARGET_NR_vm86:
9665 ret = do_vm86(cpu_env, arg1, arg2);
9666 break;
9667 #endif
9668 #endif
9669 case TARGET_NR_adjtimex:
9670 {
9671 struct timex host_buf;
9672
9673 if (target_to_host_timex(&host_buf, arg1) != 0) {
9674 goto efault;
9675 }
9676 ret = get_errno(adjtimex(&host_buf));
9677 if (!is_error(ret)) {
9678 if (host_to_target_timex(arg1, &host_buf) != 0) {
9679 goto efault;
9680 }
9681 }
9682 }
9683 break;
9684 #ifdef TARGET_NR_create_module
9685 case TARGET_NR_create_module:
9686 #endif
9687 case TARGET_NR_init_module:
9688 case TARGET_NR_delete_module:
9689 #ifdef TARGET_NR_get_kernel_syms
9690 case TARGET_NR_get_kernel_syms:
9691 #endif
9692 goto unimplemented;
9693 case TARGET_NR_quotactl:
9694 goto unimplemented;
9695 case TARGET_NR_getpgid:
9696 ret = get_errno(getpgid(arg1));
9697 break;
9698 case TARGET_NR_fchdir:
9699 ret = get_errno(fchdir(arg1));
9700 break;
9701 #ifdef TARGET_NR_bdflush /* not on x86_64 */
9702 case TARGET_NR_bdflush:
9703 goto unimplemented;
9704 #endif
9705 #ifdef TARGET_NR_sysfs
9706 case TARGET_NR_sysfs:
9707 goto unimplemented;
9708 #endif
9709 case TARGET_NR_personality:
9710 ret = get_errno(personality(arg1));
9711 break;
9712 #ifdef TARGET_NR_afs_syscall
9713 case TARGET_NR_afs_syscall:
9714 goto unimplemented;
9715 #endif
9716 #ifdef TARGET_NR__llseek /* Not on alpha */
9717 case TARGET_NR__llseek:
9718 {
9719 int64_t res;
9720 #if !defined(__NR_llseek)
9721 res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
9722 if (res == -1) {
9723 ret = get_errno(res);
9724 } else {
9725 ret = 0;
9726 }
9727 #else
9728 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
9729 #endif
9730 if ((ret == 0) && put_user_s64(res, arg4)) {
9731 goto efault;
9732 }
9733 }
9734 break;
9735 #endif
9736 #ifdef TARGET_NR_getdents
9737 case TARGET_NR_getdents:
9738 #ifdef __NR_getdents
9739 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
9740 {
9741 struct target_dirent *target_dirp;
9742 struct linux_dirent *dirp;
9743 abi_long count = arg3;
9744
9745 dirp = g_try_malloc(count);
9746 if (!dirp) {
9747 ret = -TARGET_ENOMEM;
9748 goto fail;
9749 }
9750
9751 ret = get_errno(sys_getdents(arg1, dirp, count));
9752 if (!is_error(ret)) {
9753 struct linux_dirent *de;
9754 struct target_dirent *tde;
9755 int len = ret;
9756 int reclen, treclen;
9757 int count1, tnamelen;
9758
9759 count1 = 0;
9760 de = dirp;
9761 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
9762 goto efault;
9763 tde = target_dirp;
9764 while (len > 0) {
9765 reclen = de->d_reclen;
9766 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
9767 assert(tnamelen >= 0);
9768 treclen = tnamelen + offsetof(struct target_dirent, d_name);
9769 assert(count1 + treclen <= count);
9770 tde->d_reclen = tswap16(treclen);
9771 tde->d_ino = tswapal(de->d_ino);
9772 tde->d_off = tswapal(de->d_off);
9773 memcpy(tde->d_name, de->d_name, tnamelen);
9774 de = (struct linux_dirent *)((char *)de + reclen);
9775 len -= reclen;
9776 tde = (struct target_dirent *)((char *)tde + treclen);
9777 count1 += treclen;
9778 }
9779 ret = count1;
9780 unlock_user(target_dirp, arg2, ret);
9781 }
9782 g_free(dirp);
9783 }
9784 #else
9785 {
9786 struct linux_dirent *dirp;
9787 abi_long count = arg3;
9788
9789 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
9790 goto efault;
9791 ret = get_errno(sys_getdents(arg1, dirp, count));
9792 if (!is_error(ret)) {
9793 struct linux_dirent *de;
9794 int len = ret;
9795 int reclen;
9796 de = dirp;
9797 while (len > 0) {
9798 reclen = de->d_reclen;
9799 if (reclen > len)
9800 break;
9801 de->d_reclen = tswap16(reclen);
9802 tswapls(&de->d_ino);
9803 tswapls(&de->d_off);
9804 de = (struct linux_dirent *)((char *)de + reclen);
9805 len -= reclen;
9806 }
9807 }
9808 unlock_user(dirp, arg2, ret);
9809 }
9810 #endif
9811 #else
9812 /* Implement getdents in terms of getdents64 */
9813 {
9814 struct linux_dirent64 *dirp;
9815 abi_long count = arg3;
9816
9817 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
9818 if (!dirp) {
9819 goto efault;
9820 }
9821 ret = get_errno(sys_getdents64(arg1, dirp, count));
9822 if (!is_error(ret)) {
9823 /* Convert the dirent64 structs to target dirent. We do this
9824 * in-place, since we can guarantee that a target_dirent is no
9825 * larger than a dirent64; however this means we have to be
9826 * careful to read everything before writing in the new format.
9827 */
9828 struct linux_dirent64 *de;
9829 struct target_dirent *tde;
9830 int len = ret;
9831 int tlen = 0;
9832
9833 de = dirp;
9834 tde = (struct target_dirent *)dirp;
9835 while (len > 0) {
9836 int namelen, treclen;
9837 int reclen = de->d_reclen;
9838 uint64_t ino = de->d_ino;
9839 int64_t off = de->d_off;
9840 uint8_t type = de->d_type;
9841
9842 namelen = strlen(de->d_name);
9843 treclen = offsetof(struct target_dirent, d_name)
9844 + namelen + 2;
9845 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
9846
9847 memmove(tde->d_name, de->d_name, namelen + 1);
9848 tde->d_ino = tswapal(ino);
9849 tde->d_off = tswapal(off);
9850 tde->d_reclen = tswap16(treclen);
9851 /* The target_dirent type is in what was formerly a padding
9852 * byte at the end of the structure:
9853 */
9854 *(((char *)tde) + treclen - 1) = type;
9855
9856 de = (struct linux_dirent64 *)((char *)de + reclen);
9857 tde = (struct target_dirent *)((char *)tde + treclen);
9858 len -= reclen;
9859 tlen += treclen;
9860 }
9861 ret = tlen;
9862 }
9863 unlock_user(dirp, arg2, ret);
9864 }
9865 #endif
9866 break;
9867 #endif /* TARGET_NR_getdents */
9868 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
9869 case TARGET_NR_getdents64:
9870 {
9871 struct linux_dirent64 *dirp;
9872 abi_long count = arg3;
9873 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
9874 goto efault;
9875 ret = get_errno(sys_getdents64(arg1, dirp, count));
9876 if (!is_error(ret)) {
9877 struct linux_dirent64 *de;
9878 int len = ret;
9879 int reclen;
9880 de = dirp;
9881 while (len > 0) {
9882 reclen = de->d_reclen;
9883 if (reclen > len)
9884 break;
9885 de->d_reclen = tswap16(reclen);
9886 tswap64s((uint64_t *)&de->d_ino);
9887 tswap64s((uint64_t *)&de->d_off);
9888 de = (struct linux_dirent64 *)((char *)de + reclen);
9889 len -= reclen;
9890 }
9891 }
9892 unlock_user(dirp, arg2, ret);
9893 }
9894 break;
9895 #endif /* TARGET_NR_getdents64 */
9896 #if defined(TARGET_NR__newselect)
9897 case TARGET_NR__newselect:
9898 ret = do_select(arg1, arg2, arg3, arg4, arg5);
9899 break;
9900 #endif
9901 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
9902 # ifdef TARGET_NR_poll
9903 case TARGET_NR_poll:
9904 # endif
9905 # ifdef TARGET_NR_ppoll
9906 case TARGET_NR_ppoll:
9907 # endif
9908 {
9909 struct target_pollfd *target_pfd;
9910 unsigned int nfds = arg2;
9911 struct pollfd *pfd;
9912 unsigned int i;
9913
9914 pfd = NULL;
9915 target_pfd = NULL;
9916 if (nfds) {
9917 if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
9918 ret = -TARGET_EINVAL;
9919 break;
9920 }
9921
9922 target_pfd = lock_user(VERIFY_WRITE, arg1,
9923 sizeof(struct target_pollfd) * nfds, 1);
9924 if (!target_pfd) {
9925 goto efault;
9926 }
9927
9928 pfd = alloca(sizeof(struct pollfd) * nfds);
9929 for (i = 0; i < nfds; i++) {
9930 pfd[i].fd = tswap32(target_pfd[i].fd);
9931 pfd[i].events = tswap16(target_pfd[i].events);
9932 }
9933 }
9934
9935 switch (num) {
9936 # ifdef TARGET_NR_ppoll
9937 case TARGET_NR_ppoll:
9938 {
9939 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
9940 target_sigset_t *target_set;
9941 sigset_t _set, *set = &_set;
9942
9943 if (arg3) {
9944 if (target_to_host_timespec(timeout_ts, arg3)) {
9945 unlock_user(target_pfd, arg1, 0);
9946 goto efault;
9947 }
9948 } else {
9949 timeout_ts = NULL;
9950 }
9951
9952 if (arg4) {
9953 if (arg5 != sizeof(target_sigset_t)) {
9954 unlock_user(target_pfd, arg1, 0);
9955 ret = -TARGET_EINVAL;
9956 break;
9957 }
9958
9959 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
9960 if (!target_set) {
9961 unlock_user(target_pfd, arg1, 0);
9962 goto efault;
9963 }
9964 target_to_host_sigset(set, target_set);
9965 } else {
9966 set = NULL;
9967 }
9968
9969 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
9970 set, SIGSET_T_SIZE));
9971
9972 if (!is_error(ret) && arg3) {
9973 host_to_target_timespec(arg3, timeout_ts);
9974 }
9975 if (arg4) {
9976 unlock_user(target_set, arg4, 0);
9977 }
9978 break;
9979 }
9980 # endif
9981 # ifdef TARGET_NR_poll
9982 case TARGET_NR_poll:
9983 {
9984 struct timespec ts, *pts;
9985
9986 if (arg3 >= 0) {
9987 /* Convert ms to secs, ns */
9988 ts.tv_sec = arg3 / 1000;
9989 ts.tv_nsec = (arg3 % 1000) * 1000000LL;
9990 pts = &ts;
9991 } else {
9992 /* -ve poll() timeout means "infinite" */
9993 pts = NULL;
9994 }
9995 ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
9996 break;
9997 }
9998 # endif
9999 default:
10000 g_assert_not_reached();
10001 }
10002
10003 if (!is_error(ret)) {
10004 for(i = 0; i < nfds; i++) {
10005 target_pfd[i].revents = tswap16(pfd[i].revents);
10006 }
10007 }
10008 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
10009 }
10010 break;
10011 #endif
10012 case TARGET_NR_flock:
10013 /* NOTE: the flock constant seems to be the same for every
10014 Linux platform */
10015 ret = get_errno(safe_flock(arg1, arg2));
10016 break;
10017 case TARGET_NR_readv:
10018 {
10019 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
10020 if (vec != NULL) {
10021 ret = get_errno(safe_readv(arg1, vec, arg3));
10022 unlock_iovec(vec, arg2, arg3, 1);
10023 } else {
10024 ret = -host_to_target_errno(errno);
10025 }
10026 }
10027 break;
10028 case TARGET_NR_writev:
10029 {
10030 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
10031 if (vec != NULL) {
10032 ret = get_errno(safe_writev(arg1, vec, arg3));
10033 unlock_iovec(vec, arg2, arg3, 0);
10034 } else {
10035 ret = -host_to_target_errno(errno);
10036 }
10037 }
10038 break;
10039 case TARGET_NR_getsid:
10040 ret = get_errno(getsid(arg1));
10041 break;
10042 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
10043 case TARGET_NR_fdatasync:
10044 ret = get_errno(fdatasync(arg1));
10045 break;
10046 #endif
10047 #ifdef TARGET_NR__sysctl
10048 case TARGET_NR__sysctl:
10049 /* We don't implement this, but ENOTDIR is always a safe
10050 return value. */
10051 ret = -TARGET_ENOTDIR;
10052 break;
10053 #endif
10054 case TARGET_NR_sched_getaffinity:
10055 {
10056 unsigned int mask_size;
10057 unsigned long *mask;
10058
10059 /*
10060 * sched_getaffinity needs multiples of ulong, so need to take
10061 * care of mismatches between target ulong and host ulong sizes.
10062 */
10063 if (arg2 & (sizeof(abi_ulong) - 1)) {
10064 ret = -TARGET_EINVAL;
10065 break;
10066 }
10067 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
10068
10069 mask = alloca(mask_size);
10070 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
10071
10072 if (!is_error(ret)) {
10073 if (ret > arg2) {
10074 /* More data returned than the caller's buffer will fit.
10075 * This only happens if sizeof(abi_long) < sizeof(long)
10076 * and the caller passed us a buffer holding an odd number
10077 * of abi_longs. If the host kernel is actually using the
10078 * extra 4 bytes then fail EINVAL; otherwise we can just
10079 * ignore them and only copy the interesting part.
10080 */
10081 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
10082 if (numcpus > arg2 * 8) {
10083 ret = -TARGET_EINVAL;
10084 break;
10085 }
10086 ret = arg2;
10087 }
10088
10089 if (copy_to_user(arg3, mask, ret)) {
10090 goto efault;
10091 }
10092 }
10093 }
10094 break;
10095 case TARGET_NR_sched_setaffinity:
10096 {
10097 unsigned int mask_size;
10098 unsigned long *mask;
10099
10100 /*
10101 * sched_setaffinity needs multiples of ulong, so need to take
10102 * care of mismatches between target ulong and host ulong sizes.
10103 */
10104 if (arg2 & (sizeof(abi_ulong) - 1)) {
10105 ret = -TARGET_EINVAL;
10106 break;
10107 }
10108 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
10109
10110 mask = alloca(mask_size);
10111 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
10112 goto efault;
10113 }
10114 memcpy(mask, p, arg2);
10115 unlock_user_struct(p, arg2, 0);
10116
10117 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
10118 }
10119 break;
10120 case TARGET_NR_sched_setparam:
10121 {
10122 struct sched_param *target_schp;
10123 struct sched_param schp;
10124
10125 if (arg2 == 0) {
10126 return -TARGET_EINVAL;
10127 }
10128 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
10129 goto efault;
10130 schp.sched_priority = tswap32(target_schp->sched_priority);
10131 unlock_user_struct(target_schp, arg2, 0);
10132 ret = get_errno(sched_setparam(arg1, &schp));
10133 }
10134 break;
10135 case TARGET_NR_sched_getparam:
10136 {
10137 struct sched_param *target_schp;
10138 struct sched_param schp;
10139
10140 if (arg2 == 0) {
10141 return -TARGET_EINVAL;
10142 }
10143 ret = get_errno(sched_getparam(arg1, &schp));
10144 if (!is_error(ret)) {
10145 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
10146 goto efault;
10147 target_schp->sched_priority = tswap32(schp.sched_priority);
10148 unlock_user_struct(target_schp, arg2, 1);
10149 }
10150 }
10151 break;
10152 case TARGET_NR_sched_setscheduler:
10153 {
10154 struct sched_param *target_schp;
10155 struct sched_param schp;
10156 if (arg3 == 0) {
10157 return -TARGET_EINVAL;
10158 }
10159 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
10160 goto efault;
10161 schp.sched_priority = tswap32(target_schp->sched_priority);
10162 unlock_user_struct(target_schp, arg3, 0);
10163 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
10164 }
10165 break;
10166 case TARGET_NR_sched_getscheduler:
10167 ret = get_errno(sched_getscheduler(arg1));
10168 break;
10169 case TARGET_NR_sched_yield:
10170 ret = get_errno(sched_yield());
10171 break;
10172 case TARGET_NR_sched_get_priority_max:
10173 ret = get_errno(sched_get_priority_max(arg1));
10174 break;
10175 case TARGET_NR_sched_get_priority_min:
10176 ret = get_errno(sched_get_priority_min(arg1));
10177 break;
10178 case TARGET_NR_sched_rr_get_interval:
10179 {
10180 struct timespec ts;
10181 ret = get_errno(sched_rr_get_interval(arg1, &ts));
10182 if (!is_error(ret)) {
10183 ret = host_to_target_timespec(arg2, &ts);
10184 }
10185 }
10186 break;
10187 case TARGET_NR_nanosleep:
10188 {
10189 struct timespec req, rem;
10190 target_to_host_timespec(&req, arg1);
10191 ret = get_errno(safe_nanosleep(&req, &rem));
10192 if (is_error(ret) && arg2) {
10193 host_to_target_timespec(arg2, &rem);
10194 }
10195 }
10196 break;
10197 #ifdef TARGET_NR_query_module
10198 case TARGET_NR_query_module:
10199 goto unimplemented;
10200 #endif
10201 #ifdef TARGET_NR_nfsservctl
10202 case TARGET_NR_nfsservctl:
10203 goto unimplemented;
10204 #endif
10205 case TARGET_NR_prctl:
10206 switch (arg1) {
10207 case PR_GET_PDEATHSIG:
10208 {
10209 int deathsig;
10210 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
10211 if (!is_error(ret) && arg2
10212 && put_user_ual(deathsig, arg2)) {
10213 goto efault;
10214 }
10215 break;
10216 }
10217 #ifdef PR_GET_NAME
10218 case PR_GET_NAME:
10219 {
10220 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
10221 if (!name) {
10222 goto efault;
10223 }
10224 ret = get_errno(prctl(arg1, (unsigned long)name,
10225 arg3, arg4, arg5));
10226 unlock_user(name, arg2, 16);
10227 break;
10228 }
10229 case PR_SET_NAME:
10230 {
10231 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
10232 if (!name) {
10233 goto efault;
10234 }
10235 ret = get_errno(prctl(arg1, (unsigned long)name,
10236 arg3, arg4, arg5));
10237 unlock_user(name, arg2, 0);
10238 break;
10239 }
10240 #endif
10241 default:
10242 /* Most prctl options have no pointer arguments */
10243 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
10244 break;
10245 }
10246 break;
10247 #ifdef TARGET_NR_arch_prctl
10248 case TARGET_NR_arch_prctl:
10249 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
10250 ret = do_arch_prctl(cpu_env, arg1, arg2);
10251 break;
10252 #else
10253 goto unimplemented;
10254 #endif
10255 #endif
10256 #ifdef TARGET_NR_pread64
10257 case TARGET_NR_pread64:
10258 if (regpairs_aligned(cpu_env)) {
10259 arg4 = arg5;
10260 arg5 = arg6;
10261 }
10262 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
10263 goto efault;
10264 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
10265 unlock_user(p, arg2, ret);
10266 break;
10267 case TARGET_NR_pwrite64:
10268 if (regpairs_aligned(cpu_env)) {
10269 arg4 = arg5;
10270 arg5 = arg6;
10271 }
10272 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
10273 goto efault;
10274 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
10275 unlock_user(p, arg2, 0);
10276 break;
10277 #endif
10278 case TARGET_NR_getcwd:
10279 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
10280 goto efault;
10281 ret = get_errno(sys_getcwd1(p, arg2));
10282 unlock_user(p, arg1, ret);
10283 break;
10284 case TARGET_NR_capget:
10285 case TARGET_NR_capset:
10286 {
10287 struct target_user_cap_header *target_header;
10288 struct target_user_cap_data *target_data = NULL;
10289 struct __user_cap_header_struct header;
10290 struct __user_cap_data_struct data[2];
10291 struct __user_cap_data_struct *dataptr = NULL;
10292 int i, target_datalen;
10293 int data_items = 1;
10294
10295 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
10296 goto efault;
10297 }
10298 header.version = tswap32(target_header->version);
10299 header.pid = tswap32(target_header->pid);
10300
10301 if (header.version != _LINUX_CAPABILITY_VERSION) {
10302 /* Version 2 and up takes pointer to two user_data structs */
10303 data_items = 2;
10304 }
10305
10306 target_datalen = sizeof(*target_data) * data_items;
10307
10308 if (arg2) {
10309 if (num == TARGET_NR_capget) {
10310 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
10311 } else {
10312 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
10313 }
10314 if (!target_data) {
10315 unlock_user_struct(target_header, arg1, 0);
10316 goto efault;
10317 }
10318
10319 if (num == TARGET_NR_capset) {
10320 for (i = 0; i < data_items; i++) {
10321 data[i].effective = tswap32(target_data[i].effective);
10322 data[i].permitted = tswap32(target_data[i].permitted);
10323 data[i].inheritable = tswap32(target_data[i].inheritable);
10324 }
10325 }
10326
10327 dataptr = data;
10328 }
10329
10330 if (num == TARGET_NR_capget) {
10331 ret = get_errno(capget(&header, dataptr));
10332 } else {
10333 ret = get_errno(capset(&header, dataptr));
10334 }
10335
10336 /* The kernel always updates version for both capget and capset */
10337 target_header->version = tswap32(header.version);
10338 unlock_user_struct(target_header, arg1, 1);
10339
10340 if (arg2) {
10341 if (num == TARGET_NR_capget) {
10342 for (i = 0; i < data_items; i++) {
10343 target_data[i].effective = tswap32(data[i].effective);
10344 target_data[i].permitted = tswap32(data[i].permitted);
10345 target_data[i].inheritable = tswap32(data[i].inheritable);
10346 }
10347 unlock_user(target_data, arg2, target_datalen);
10348 } else {
10349 unlock_user(target_data, arg2, 0);
10350 }
10351 }
10352 break;
10353 }
10354 case TARGET_NR_sigaltstack:
10355 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
10356 break;
10357
10358 #ifdef CONFIG_SENDFILE
10359 case TARGET_NR_sendfile:
10360 {
10361 off_t *offp = NULL;
10362 off_t off;
10363 if (arg3) {
10364 ret = get_user_sal(off, arg3);
10365 if (is_error(ret)) {
10366 break;
10367 }
10368 offp = &off;
10369 }
10370 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
10371 if (!is_error(ret) && arg3) {
10372 abi_long ret2 = put_user_sal(off, arg3);
10373 if (is_error(ret2)) {
10374 ret = ret2;
10375 }
10376 }
10377 break;
10378 }
10379 #ifdef TARGET_NR_sendfile64
10380 case TARGET_NR_sendfile64:
10381 {
10382 off_t *offp = NULL;
10383 off_t off;
10384 if (arg3) {
10385 ret = get_user_s64(off, arg3);
10386 if (is_error(ret)) {
10387 break;
10388 }
10389 offp = &off;
10390 }
10391 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
10392 if (!is_error(ret) && arg3) {
10393 abi_long ret2 = put_user_s64(off, arg3);
10394 if (is_error(ret2)) {
10395 ret = ret2;
10396 }
10397 }
10398 break;
10399 }
10400 #endif
10401 #else
10402 case TARGET_NR_sendfile:
10403 #ifdef TARGET_NR_sendfile64
10404 case TARGET_NR_sendfile64:
10405 #endif
10406 goto unimplemented;
10407 #endif
10408
10409 #ifdef TARGET_NR_getpmsg
10410 case TARGET_NR_getpmsg:
10411 goto unimplemented;
10412 #endif
10413 #ifdef TARGET_NR_putpmsg
10414 case TARGET_NR_putpmsg:
10415 goto unimplemented;
10416 #endif
10417 #ifdef TARGET_NR_vfork
10418 case TARGET_NR_vfork:
10419 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
10420 0, 0, 0, 0));
10421 break;
10422 #endif
10423 #ifdef TARGET_NR_ugetrlimit
10424 case TARGET_NR_ugetrlimit:
10425 {
10426 struct rlimit rlim;
10427 int resource = target_to_host_resource(arg1);
10428 ret = get_errno(getrlimit(resource, &rlim));
10429 if (!is_error(ret)) {
10430 struct target_rlimit *target_rlim;
10431 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
10432 goto efault;
10433 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
10434 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
10435 unlock_user_struct(target_rlim, arg2, 1);
10436 }
10437 break;
10438 }
10439 #endif
10440 #ifdef TARGET_NR_truncate64
10441 case TARGET_NR_truncate64:
10442 if (!(p = lock_user_string(arg1)))
10443 goto efault;
10444 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
10445 unlock_user(p, arg1, 0);
10446 break;
10447 #endif
10448 #ifdef TARGET_NR_ftruncate64
10449 case TARGET_NR_ftruncate64:
10450 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
10451 break;
10452 #endif
10453 #ifdef TARGET_NR_stat64
10454 case TARGET_NR_stat64:
10455 if (!(p = lock_user_string(arg1)))
10456 goto efault;
10457 ret = get_errno(stat(path(p), &st));
10458 unlock_user(p, arg1, 0);
10459 if (!is_error(ret))
10460 ret = host_to_target_stat64(cpu_env, arg2, &st);
10461 break;
10462 #endif
10463 #ifdef TARGET_NR_lstat64
10464 case TARGET_NR_lstat64:
10465 if (!(p = lock_user_string(arg1)))
10466 goto efault;
10467 ret = get_errno(lstat(path(p), &st));
10468 unlock_user(p, arg1, 0);
10469 if (!is_error(ret))
10470 ret = host_to_target_stat64(cpu_env, arg2, &st);
10471 break;
10472 #endif
10473 #ifdef TARGET_NR_fstat64
10474 case TARGET_NR_fstat64:
10475 ret = get_errno(fstat(arg1, &st));
10476 if (!is_error(ret))
10477 ret = host_to_target_stat64(cpu_env, arg2, &st);
10478 break;
10479 #endif
10480 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
10481 #ifdef TARGET_NR_fstatat64
10482 case TARGET_NR_fstatat64:
10483 #endif
10484 #ifdef TARGET_NR_newfstatat
10485 case TARGET_NR_newfstatat:
10486 #endif
10487 if (!(p = lock_user_string(arg2)))
10488 goto efault;
10489 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
10490 if (!is_error(ret))
10491 ret = host_to_target_stat64(cpu_env, arg3, &st);
10492 break;
10493 #endif
10494 #ifdef TARGET_NR_lchown
10495 case TARGET_NR_lchown:
10496 if (!(p = lock_user_string(arg1)))
10497 goto efault;
10498 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
10499 unlock_user(p, arg1, 0);
10500 break;
10501 #endif
10502 #ifdef TARGET_NR_getuid
10503 case TARGET_NR_getuid:
10504 ret = get_errno(high2lowuid(getuid()));
10505 break;
10506 #endif
10507 #ifdef TARGET_NR_getgid
10508 case TARGET_NR_getgid:
10509 ret = get_errno(high2lowgid(getgid()));
10510 break;
10511 #endif
10512 #ifdef TARGET_NR_geteuid
10513 case TARGET_NR_geteuid:
10514 ret = get_errno(high2lowuid(geteuid()));
10515 break;
10516 #endif
10517 #ifdef TARGET_NR_getegid
10518 case TARGET_NR_getegid:
10519 ret = get_errno(high2lowgid(getegid()));
10520 break;
10521 #endif
10522 case TARGET_NR_setreuid:
10523 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
10524 break;
10525 case TARGET_NR_setregid:
10526 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
10527 break;
10528 case TARGET_NR_getgroups:
10529 {
10530 int gidsetsize = arg1;
10531 target_id *target_grouplist;
10532 gid_t *grouplist;
10533 int i;
10534
10535 grouplist = alloca(gidsetsize * sizeof(gid_t));
10536 ret = get_errno(getgroups(gidsetsize, grouplist));
10537 if (gidsetsize == 0)
10538 break;
10539 if (!is_error(ret)) {
10540 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
10541 if (!target_grouplist)
10542 goto efault;
10543 for(i = 0;i < ret; i++)
10544 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
10545 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
10546 }
10547 }
10548 break;
10549 case TARGET_NR_setgroups:
10550 {
10551 int gidsetsize = arg1;
10552 target_id *target_grouplist;
10553 gid_t *grouplist = NULL;
10554 int i;
10555 if (gidsetsize) {
10556 grouplist = alloca(gidsetsize * sizeof(gid_t));
10557 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
10558 if (!target_grouplist) {
10559 ret = -TARGET_EFAULT;
10560 goto fail;
10561 }
10562 for (i = 0; i < gidsetsize; i++) {
10563 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
10564 }
10565 unlock_user(target_grouplist, arg2, 0);
10566 }
10567 ret = get_errno(setgroups(gidsetsize, grouplist));
10568 }
10569 break;
10570 case TARGET_NR_fchown:
10571 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
10572 break;
10573 #if defined(TARGET_NR_fchownat)
10574 case TARGET_NR_fchownat:
10575 if (!(p = lock_user_string(arg2)))
10576 goto efault;
10577 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
10578 low2highgid(arg4), arg5));
10579 unlock_user(p, arg2, 0);
10580 break;
10581 #endif
10582 #ifdef TARGET_NR_setresuid
10583 case TARGET_NR_setresuid:
10584 ret = get_errno(sys_setresuid(low2highuid(arg1),
10585 low2highuid(arg2),
10586 low2highuid(arg3)));
10587 break;
10588 #endif
10589 #ifdef TARGET_NR_getresuid
10590 case TARGET_NR_getresuid:
10591 {
10592 uid_t ruid, euid, suid;
10593 ret = get_errno(getresuid(&ruid, &euid, &suid));
10594 if (!is_error(ret)) {
10595 if (put_user_id(high2lowuid(ruid), arg1)
10596 || put_user_id(high2lowuid(euid), arg2)
10597 || put_user_id(high2lowuid(suid), arg3))
10598 goto efault;
10599 }
10600 }
10601 break;
10602 #endif
10603 #ifdef TARGET_NR_getresgid
10604 case TARGET_NR_setresgid:
10605 ret = get_errno(sys_setresgid(low2highgid(arg1),
10606 low2highgid(arg2),
10607 low2highgid(arg3)));
10608 break;
10609 #endif
10610 #ifdef TARGET_NR_getresgid
10611 case TARGET_NR_getresgid:
10612 {
10613 gid_t rgid, egid, sgid;
10614 ret = get_errno(getresgid(&rgid, &egid, &sgid));
10615 if (!is_error(ret)) {
10616 if (put_user_id(high2lowgid(rgid), arg1)
10617 || put_user_id(high2lowgid(egid), arg2)
10618 || put_user_id(high2lowgid(sgid), arg3))
10619 goto efault;
10620 }
10621 }
10622 break;
10623 #endif
10624 #ifdef TARGET_NR_chown
10625 case TARGET_NR_chown:
10626 if (!(p = lock_user_string(arg1)))
10627 goto efault;
10628 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
10629 unlock_user(p, arg1, 0);
10630 break;
10631 #endif
10632 case TARGET_NR_setuid:
10633 ret = get_errno(sys_setuid(low2highuid(arg1)));
10634 break;
10635 case TARGET_NR_setgid:
10636 ret = get_errno(sys_setgid(low2highgid(arg1)));
10637 break;
10638 case TARGET_NR_setfsuid:
10639 ret = get_errno(setfsuid(arg1));
10640 break;
10641 case TARGET_NR_setfsgid:
10642 ret = get_errno(setfsgid(arg1));
10643 break;
10644
10645 #ifdef TARGET_NR_lchown32
10646 case TARGET_NR_lchown32:
10647 if (!(p = lock_user_string(arg1)))
10648 goto efault;
10649 ret = get_errno(lchown(p, arg2, arg3));
10650 unlock_user(p, arg1, 0);
10651 break;
10652 #endif
10653 #ifdef TARGET_NR_getuid32
10654 case TARGET_NR_getuid32:
10655 ret = get_errno(getuid());
10656 break;
10657 #endif
10658
10659 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
10660 /* Alpha specific */
10661 case TARGET_NR_getxuid:
10662 {
10663 uid_t euid;
10664 euid=geteuid();
10665 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
10666 }
10667 ret = get_errno(getuid());
10668 break;
10669 #endif
10670 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
10671 /* Alpha specific */
10672 case TARGET_NR_getxgid:
10673 {
10674 uid_t egid;
10675 egid=getegid();
10676 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
10677 }
10678 ret = get_errno(getgid());
10679 break;
10680 #endif
10681 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
10682 /* Alpha specific */
10683 case TARGET_NR_osf_getsysinfo:
10684 ret = -TARGET_EOPNOTSUPP;
10685 switch (arg1) {
10686 case TARGET_GSI_IEEE_FP_CONTROL:
10687 {
10688 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
10689
10690 /* Copied from linux ieee_fpcr_to_swcr. */
10691 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
10692 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
10693 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
10694 | SWCR_TRAP_ENABLE_DZE
10695 | SWCR_TRAP_ENABLE_OVF);
10696 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
10697 | SWCR_TRAP_ENABLE_INE);
10698 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
10699 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
10700
10701 if (put_user_u64 (swcr, arg2))
10702 goto efault;
10703 ret = 0;
10704 }
10705 break;
10706
10707 /* case GSI_IEEE_STATE_AT_SIGNAL:
10708 -- Not implemented in linux kernel.
10709 case GSI_UACPROC:
10710 -- Retrieves current unaligned access state; not much used.
10711 case GSI_PROC_TYPE:
10712 -- Retrieves implver information; surely not used.
10713 case GSI_GET_HWRPB:
10714 -- Grabs a copy of the HWRPB; surely not used.
10715 */
10716 }
10717 break;
10718 #endif
10719 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
10720 /* Alpha specific */
10721 case TARGET_NR_osf_setsysinfo:
10722 ret = -TARGET_EOPNOTSUPP;
10723 switch (arg1) {
10724 case TARGET_SSI_IEEE_FP_CONTROL:
10725 {
10726 uint64_t swcr, fpcr, orig_fpcr;
10727
10728 if (get_user_u64 (swcr, arg2)) {
10729 goto efault;
10730 }
10731 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
10732 fpcr = orig_fpcr & FPCR_DYN_MASK;
10733
10734 /* Copied from linux ieee_swcr_to_fpcr. */
10735 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
10736 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
10737 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
10738 | SWCR_TRAP_ENABLE_DZE
10739 | SWCR_TRAP_ENABLE_OVF)) << 48;
10740 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
10741 | SWCR_TRAP_ENABLE_INE)) << 57;
10742 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
10743 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
10744
10745 cpu_alpha_store_fpcr(cpu_env, fpcr);
10746 ret = 0;
10747 }
10748 break;
10749
10750 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
10751 {
10752 uint64_t exc, fpcr, orig_fpcr;
10753 int si_code;
10754
10755 if (get_user_u64(exc, arg2)) {
10756 goto efault;
10757 }
10758
10759 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
10760
10761 /* We only add to the exception status here. */
10762 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
10763
10764 cpu_alpha_store_fpcr(cpu_env, fpcr);
10765 ret = 0;
10766
10767 /* Old exceptions are not signaled. */
10768 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
10769
10770 /* If any exceptions set by this call,
10771 and are unmasked, send a signal. */
10772 si_code = 0;
10773 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
10774 si_code = TARGET_FPE_FLTRES;
10775 }
10776 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
10777 si_code = TARGET_FPE_FLTUND;
10778 }
10779 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
10780 si_code = TARGET_FPE_FLTOVF;
10781 }
10782 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
10783 si_code = TARGET_FPE_FLTDIV;
10784 }
10785 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
10786 si_code = TARGET_FPE_FLTINV;
10787 }
10788 if (si_code != 0) {
10789 target_siginfo_t info;
10790 info.si_signo = SIGFPE;
10791 info.si_errno = 0;
10792 info.si_code = si_code;
10793 info._sifields._sigfault._addr
10794 = ((CPUArchState *)cpu_env)->pc;
10795 queue_signal((CPUArchState *)cpu_env, info.si_signo,
10796 QEMU_SI_FAULT, &info);
10797 }
10798 }
10799 break;
10800
10801 /* case SSI_NVPAIRS:
10802 -- Used with SSIN_UACPROC to enable unaligned accesses.
10803 case SSI_IEEE_STATE_AT_SIGNAL:
10804 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
10805 -- Not implemented in linux kernel
10806 */
10807 }
10808 break;
10809 #endif
10810 #ifdef TARGET_NR_osf_sigprocmask
10811 /* Alpha specific. */
10812 case TARGET_NR_osf_sigprocmask:
10813 {
10814 abi_ulong mask;
10815 int how;
10816 sigset_t set, oldset;
10817
10818 switch(arg1) {
10819 case TARGET_SIG_BLOCK:
10820 how = SIG_BLOCK;
10821 break;
10822 case TARGET_SIG_UNBLOCK:
10823 how = SIG_UNBLOCK;
10824 break;
10825 case TARGET_SIG_SETMASK:
10826 how = SIG_SETMASK;
10827 break;
10828 default:
10829 ret = -TARGET_EINVAL;
10830 goto fail;
10831 }
10832 mask = arg2;
10833 target_to_host_old_sigset(&set, &mask);
10834 ret = do_sigprocmask(how, &set, &oldset);
10835 if (!ret) {
10836 host_to_target_old_sigset(&mask, &oldset);
10837 ret = mask;
10838 }
10839 }
10840 break;
10841 #endif
10842
10843 #ifdef TARGET_NR_getgid32
10844 case TARGET_NR_getgid32:
10845 ret = get_errno(getgid());
10846 break;
10847 #endif
10848 #ifdef TARGET_NR_geteuid32
10849 case TARGET_NR_geteuid32:
10850 ret = get_errno(geteuid());
10851 break;
10852 #endif
10853 #ifdef TARGET_NR_getegid32
10854 case TARGET_NR_getegid32:
10855 ret = get_errno(getegid());
10856 break;
10857 #endif
10858 #ifdef TARGET_NR_setreuid32
10859 case TARGET_NR_setreuid32:
10860 ret = get_errno(setreuid(arg1, arg2));
10861 break;
10862 #endif
10863 #ifdef TARGET_NR_setregid32
10864 case TARGET_NR_setregid32:
10865 ret = get_errno(setregid(arg1, arg2));
10866 break;
10867 #endif
10868 #ifdef TARGET_NR_getgroups32
10869 case TARGET_NR_getgroups32:
10870 {
10871 int gidsetsize = arg1;
10872 uint32_t *target_grouplist;
10873 gid_t *grouplist;
10874 int i;
10875
10876 grouplist = alloca(gidsetsize * sizeof(gid_t));
10877 ret = get_errno(getgroups(gidsetsize, grouplist));
10878 if (gidsetsize == 0)
10879 break;
10880 if (!is_error(ret)) {
10881 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
10882 if (!target_grouplist) {
10883 ret = -TARGET_EFAULT;
10884 goto fail;
10885 }
10886 for(i = 0;i < ret; i++)
10887 target_grouplist[i] = tswap32(grouplist[i]);
10888 unlock_user(target_grouplist, arg2, gidsetsize * 4);
10889 }
10890 }
10891 break;
10892 #endif
10893 #ifdef TARGET_NR_setgroups32
10894 case TARGET_NR_setgroups32:
10895 {
10896 int gidsetsize = arg1;
10897 uint32_t *target_grouplist;
10898 gid_t *grouplist;
10899 int i;
10900
10901 grouplist = alloca(gidsetsize * sizeof(gid_t));
10902 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
10903 if (!target_grouplist) {
10904 ret = -TARGET_EFAULT;
10905 goto fail;
10906 }
10907 for(i = 0;i < gidsetsize; i++)
10908 grouplist[i] = tswap32(target_grouplist[i]);
10909 unlock_user(target_grouplist, arg2, 0);
10910 ret = get_errno(setgroups(gidsetsize, grouplist));
10911 }
10912 break;
10913 #endif
10914 #ifdef TARGET_NR_fchown32
10915 case TARGET_NR_fchown32:
10916 ret = get_errno(fchown(arg1, arg2, arg3));
10917 break;
10918 #endif
10919 #ifdef TARGET_NR_setresuid32
10920 case TARGET_NR_setresuid32:
10921 ret = get_errno(sys_setresuid(arg1, arg2, arg3));
10922 break;
10923 #endif
10924 #ifdef TARGET_NR_getresuid32
10925 case TARGET_NR_getresuid32:
10926 {
10927 uid_t ruid, euid, suid;
10928 ret = get_errno(getresuid(&ruid, &euid, &suid));
10929 if (!is_error(ret)) {
10930 if (put_user_u32(ruid, arg1)
10931 || put_user_u32(euid, arg2)
10932 || put_user_u32(suid, arg3))
10933 goto efault;
10934 }
10935 }
10936 break;
10937 #endif
10938 #ifdef TARGET_NR_setresgid32
10939 case TARGET_NR_setresgid32:
10940 ret = get_errno(sys_setresgid(arg1, arg2, arg3));
10941 break;
10942 #endif
10943 #ifdef TARGET_NR_getresgid32
10944 case TARGET_NR_getresgid32:
10945 {
10946 gid_t rgid, egid, sgid;
10947 ret = get_errno(getresgid(&rgid, &egid, &sgid));
10948 if (!is_error(ret)) {
10949 if (put_user_u32(rgid, arg1)
10950 || put_user_u32(egid, arg2)
10951 || put_user_u32(sgid, arg3))
10952 goto efault;
10953 }
10954 }
10955 break;
10956 #endif
10957 #ifdef TARGET_NR_chown32
10958 case TARGET_NR_chown32:
10959 if (!(p = lock_user_string(arg1)))
10960 goto efault;
10961 ret = get_errno(chown(p, arg2, arg3));
10962 unlock_user(p, arg1, 0);
10963 break;
10964 #endif
10965 #ifdef TARGET_NR_setuid32
10966 case TARGET_NR_setuid32:
10967 ret = get_errno(sys_setuid(arg1));
10968 break;
10969 #endif
10970 #ifdef TARGET_NR_setgid32
10971 case TARGET_NR_setgid32:
10972 ret = get_errno(sys_setgid(arg1));
10973 break;
10974 #endif
10975 #ifdef TARGET_NR_setfsuid32
10976 case TARGET_NR_setfsuid32:
10977 ret = get_errno(setfsuid(arg1));
10978 break;
10979 #endif
10980 #ifdef TARGET_NR_setfsgid32
10981 case TARGET_NR_setfsgid32:
10982 ret = get_errno(setfsgid(arg1));
10983 break;
10984 #endif
10985
10986 case TARGET_NR_pivot_root:
10987 goto unimplemented;
10988 #ifdef TARGET_NR_mincore
10989 case TARGET_NR_mincore:
10990 {
10991 void *a;
10992 ret = -TARGET_EFAULT;
10993 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
10994 goto efault;
10995 if (!(p = lock_user_string(arg3)))
10996 goto mincore_fail;
10997 ret = get_errno(mincore(a, arg2, p));
10998 unlock_user(p, arg3, ret);
10999 mincore_fail:
11000 unlock_user(a, arg1, 0);
11001 }
11002 break;
11003 #endif
11004 #ifdef TARGET_NR_arm_fadvise64_64
11005 case TARGET_NR_arm_fadvise64_64:
11006 /* arm_fadvise64_64 looks like fadvise64_64 but
11007 * with different argument order: fd, advice, offset, len
11008 * rather than the usual fd, offset, len, advice.
11009 * Note that offset and len are both 64-bit so appear as
11010 * pairs of 32-bit registers.
11011 */
11012 ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
11013 target_offset64(arg5, arg6), arg2);
11014 ret = -host_to_target_errno(ret);
11015 break;
11016 #endif
11017
11018 #if TARGET_ABI_BITS == 32
11019
11020 #ifdef TARGET_NR_fadvise64_64
11021 case TARGET_NR_fadvise64_64:
11022 /* 6 args: fd, offset (high, low), len (high, low), advice */
11023 if (regpairs_aligned(cpu_env)) {
11024 /* offset is in (3,4), len in (5,6) and advice in 7 */
11025 arg2 = arg3;
11026 arg3 = arg4;
11027 arg4 = arg5;
11028 arg5 = arg6;
11029 arg6 = arg7;
11030 }
11031 ret = -host_to_target_errno(posix_fadvise(arg1,
11032 target_offset64(arg2, arg3),
11033 target_offset64(arg4, arg5),
11034 arg6));
11035 break;
11036 #endif
11037
11038 #ifdef TARGET_NR_fadvise64
11039 case TARGET_NR_fadvise64:
11040 /* 5 args: fd, offset (high, low), len, advice */
11041 if (regpairs_aligned(cpu_env)) {
11042 /* offset is in (3,4), len in 5 and advice in 6 */
11043 arg2 = arg3;
11044 arg3 = arg4;
11045 arg4 = arg5;
11046 arg5 = arg6;
11047 }
11048 ret = -host_to_target_errno(posix_fadvise(arg1,
11049 target_offset64(arg2, arg3),
11050 arg4, arg5));
11051 break;
11052 #endif
11053
11054 #else /* not a 32-bit ABI */
11055 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
11056 #ifdef TARGET_NR_fadvise64_64
11057 case TARGET_NR_fadvise64_64:
11058 #endif
11059 #ifdef TARGET_NR_fadvise64
11060 case TARGET_NR_fadvise64:
11061 #endif
11062 #ifdef TARGET_S390X
11063 switch (arg4) {
11064 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
11065 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
11066 case 6: arg4 = POSIX_FADV_DONTNEED; break;
11067 case 7: arg4 = POSIX_FADV_NOREUSE; break;
11068 default: break;
11069 }
11070 #endif
11071 ret = -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
11072 break;
11073 #endif
11074 #endif /* end of 64-bit ABI fadvise handling */
11075
11076 #ifdef TARGET_NR_madvise
11077 case TARGET_NR_madvise:
11078 /* A straight passthrough may not be safe because qemu sometimes
11079 turns private file-backed mappings into anonymous mappings.
11080 This will break MADV_DONTNEED.
11081 This is a hint, so ignoring and returning success is ok. */
11082 ret = get_errno(0);
11083 break;
11084 #endif
11085 #if TARGET_ABI_BITS == 32
11086 case TARGET_NR_fcntl64:
11087 {
11088 int cmd;
11089 struct flock64 fl;
11090 from_flock64_fn *copyfrom = copy_from_user_flock64;
11091 to_flock64_fn *copyto = copy_to_user_flock64;
11092
11093 #ifdef TARGET_ARM
11094 if (((CPUARMState *)cpu_env)->eabi) {
11095 copyfrom = copy_from_user_eabi_flock64;
11096 copyto = copy_to_user_eabi_flock64;
11097 }
11098 #endif
11099
11100 cmd = target_to_host_fcntl_cmd(arg2);
11101 if (cmd == -TARGET_EINVAL) {
11102 ret = cmd;
11103 break;
11104 }
11105
11106 switch(arg2) {
11107 case TARGET_F_GETLK64:
11108 ret = copyfrom(&fl, arg3);
11109 if (ret) {
11110 break;
11111 }
11112 ret = get_errno(fcntl(arg1, cmd, &fl));
11113 if (ret == 0) {
11114 ret = copyto(arg3, &fl);
11115 }
11116 break;
11117
11118 case TARGET_F_SETLK64:
11119 case TARGET_F_SETLKW64:
11120 ret = copyfrom(&fl, arg3);
11121 if (ret) {
11122 break;
11123 }
11124 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
11125 break;
11126 default:
11127 ret = do_fcntl(arg1, arg2, arg3);
11128 break;
11129 }
11130 break;
11131 }
11132 #endif
11133 #ifdef TARGET_NR_cacheflush
11134 case TARGET_NR_cacheflush:
11135 /* self-modifying code is handled automatically, so nothing needed */
11136 ret = 0;
11137 break;
11138 #endif
11139 #ifdef TARGET_NR_security
11140 case TARGET_NR_security:
11141 goto unimplemented;
11142 #endif
11143 #ifdef TARGET_NR_getpagesize
11144 case TARGET_NR_getpagesize:
11145 ret = TARGET_PAGE_SIZE;
11146 break;
11147 #endif
11148 case TARGET_NR_gettid:
11149 ret = get_errno(gettid());
11150 break;
11151 #ifdef TARGET_NR_readahead
11152 case TARGET_NR_readahead:
11153 #if TARGET_ABI_BITS == 32
11154 if (regpairs_aligned(cpu_env)) {
11155 arg2 = arg3;
11156 arg3 = arg4;
11157 arg4 = arg5;
11158 }
11159 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
11160 #else
11161 ret = get_errno(readahead(arg1, arg2, arg3));
11162 #endif
11163 break;
11164 #endif
11165 #ifdef CONFIG_ATTR
11166 #ifdef TARGET_NR_setxattr
11167 case TARGET_NR_listxattr:
11168 case TARGET_NR_llistxattr:
11169 {
11170 void *p, *b = 0;
11171 if (arg2) {
11172 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11173 if (!b) {
11174 ret = -TARGET_EFAULT;
11175 break;
11176 }
11177 }
11178 p = lock_user_string(arg1);
11179 if (p) {
11180 if (num == TARGET_NR_listxattr) {
11181 ret = get_errno(listxattr(p, b, arg3));
11182 } else {
11183 ret = get_errno(llistxattr(p, b, arg3));
11184 }
11185 } else {
11186 ret = -TARGET_EFAULT;
11187 }
11188 unlock_user(p, arg1, 0);
11189 unlock_user(b, arg2, arg3);
11190 break;
11191 }
11192 case TARGET_NR_flistxattr:
11193 {
11194 void *b = 0;
11195 if (arg2) {
11196 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11197 if (!b) {
11198 ret = -TARGET_EFAULT;
11199 break;
11200 }
11201 }
11202 ret = get_errno(flistxattr(arg1, b, arg3));
11203 unlock_user(b, arg2, arg3);
11204 break;
11205 }
11206 case TARGET_NR_setxattr:
11207 case TARGET_NR_lsetxattr:
11208 {
11209 void *p, *n, *v = 0;
11210 if (arg3) {
11211 v = lock_user(VERIFY_READ, arg3, arg4, 1);
11212 if (!v) {
11213 ret = -TARGET_EFAULT;
11214 break;
11215 }
11216 }
11217 p = lock_user_string(arg1);
11218 n = lock_user_string(arg2);
11219 if (p && n) {
11220 if (num == TARGET_NR_setxattr) {
11221 ret = get_errno(setxattr(p, n, v, arg4, arg5));
11222 } else {
11223 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
11224 }
11225 } else {
11226 ret = -TARGET_EFAULT;
11227 }
11228 unlock_user(p, arg1, 0);
11229 unlock_user(n, arg2, 0);
11230 unlock_user(v, arg3, 0);
11231 }
11232 break;
11233 case TARGET_NR_fsetxattr:
11234 {
11235 void *n, *v = 0;
11236 if (arg3) {
11237 v = lock_user(VERIFY_READ, arg3, arg4, 1);
11238 if (!v) {
11239 ret = -TARGET_EFAULT;
11240 break;
11241 }
11242 }
11243 n = lock_user_string(arg2);
11244 if (n) {
11245 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
11246 } else {
11247 ret = -TARGET_EFAULT;
11248 }
11249 unlock_user(n, arg2, 0);
11250 unlock_user(v, arg3, 0);
11251 }
11252 break;
11253 case TARGET_NR_getxattr:
11254 case TARGET_NR_lgetxattr:
11255 {
11256 void *p, *n, *v = 0;
11257 if (arg3) {
11258 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
11259 if (!v) {
11260 ret = -TARGET_EFAULT;
11261 break;
11262 }
11263 }
11264 p = lock_user_string(arg1);
11265 n = lock_user_string(arg2);
11266 if (p && n) {
11267 if (num == TARGET_NR_getxattr) {
11268 ret = get_errno(getxattr(p, n, v, arg4));
11269 } else {
11270 ret = get_errno(lgetxattr(p, n, v, arg4));
11271 }
11272 } else {
11273 ret = -TARGET_EFAULT;
11274 }
11275 unlock_user(p, arg1, 0);
11276 unlock_user(n, arg2, 0);
11277 unlock_user(v, arg3, arg4);
11278 }
11279 break;
11280 case TARGET_NR_fgetxattr:
11281 {
11282 void *n, *v = 0;
11283 if (arg3) {
11284 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
11285 if (!v) {
11286 ret = -TARGET_EFAULT;
11287 break;
11288 }
11289 }
11290 n = lock_user_string(arg2);
11291 if (n) {
11292 ret = get_errno(fgetxattr(arg1, n, v, arg4));
11293 } else {
11294 ret = -TARGET_EFAULT;
11295 }
11296 unlock_user(n, arg2, 0);
11297 unlock_user(v, arg3, arg4);
11298 }
11299 break;
11300 case TARGET_NR_removexattr:
11301 case TARGET_NR_lremovexattr:
11302 {
11303 void *p, *n;
11304 p = lock_user_string(arg1);
11305 n = lock_user_string(arg2);
11306 if (p && n) {
11307 if (num == TARGET_NR_removexattr) {
11308 ret = get_errno(removexattr(p, n));
11309 } else {
11310 ret = get_errno(lremovexattr(p, n));
11311 }
11312 } else {
11313 ret = -TARGET_EFAULT;
11314 }
11315 unlock_user(p, arg1, 0);
11316 unlock_user(n, arg2, 0);
11317 }
11318 break;
11319 case TARGET_NR_fremovexattr:
11320 {
11321 void *n;
11322 n = lock_user_string(arg2);
11323 if (n) {
11324 ret = get_errno(fremovexattr(arg1, n));
11325 } else {
11326 ret = -TARGET_EFAULT;
11327 }
11328 unlock_user(n, arg2, 0);
11329 }
11330 break;
11331 #endif
11332 #endif /* CONFIG_ATTR */
11333 #ifdef TARGET_NR_set_thread_area
11334 case TARGET_NR_set_thread_area:
11335 #if defined(TARGET_MIPS)
11336 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
11337 ret = 0;
11338 break;
11339 #elif defined(TARGET_CRIS)
11340 if (arg1 & 0xff)
11341 ret = -TARGET_EINVAL;
11342 else {
11343 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
11344 ret = 0;
11345 }
11346 break;
11347 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
11348 ret = do_set_thread_area(cpu_env, arg1);
11349 break;
11350 #elif defined(TARGET_M68K)
11351 {
11352 TaskState *ts = cpu->opaque;
11353 ts->tp_value = arg1;
11354 ret = 0;
11355 break;
11356 }
11357 #else
11358 goto unimplemented_nowarn;
11359 #endif
11360 #endif
11361 #ifdef TARGET_NR_get_thread_area
11362 case TARGET_NR_get_thread_area:
11363 #if defined(TARGET_I386) && defined(TARGET_ABI32)
11364 ret = do_get_thread_area(cpu_env, arg1);
11365 break;
11366 #elif defined(TARGET_M68K)
11367 {
11368 TaskState *ts = cpu->opaque;
11369 ret = ts->tp_value;
11370 break;
11371 }
11372 #else
11373 goto unimplemented_nowarn;
11374 #endif
11375 #endif
11376 #ifdef TARGET_NR_getdomainname
11377 case TARGET_NR_getdomainname:
11378 goto unimplemented_nowarn;
11379 #endif
11380
11381 #ifdef TARGET_NR_clock_gettime
11382 case TARGET_NR_clock_gettime:
11383 {
11384 struct timespec ts;
11385 ret = get_errno(clock_gettime(arg1, &ts));
11386 if (!is_error(ret)) {
11387 host_to_target_timespec(arg2, &ts);
11388 }
11389 break;
11390 }
11391 #endif
11392 #ifdef TARGET_NR_clock_getres
11393 case TARGET_NR_clock_getres:
11394 {
11395 struct timespec ts;
11396 ret = get_errno(clock_getres(arg1, &ts));
11397 if (!is_error(ret)) {
11398 host_to_target_timespec(arg2, &ts);
11399 }
11400 break;
11401 }
11402 #endif
11403 #ifdef TARGET_NR_clock_nanosleep
11404 case TARGET_NR_clock_nanosleep:
11405 {
11406 struct timespec ts;
11407 target_to_host_timespec(&ts, arg3);
11408 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
11409 &ts, arg4 ? &ts : NULL));
11410 if (arg4)
11411 host_to_target_timespec(arg4, &ts);
11412
11413 #if defined(TARGET_PPC)
11414 /* clock_nanosleep is odd in that it returns positive errno values.
11415 * On PPC, CR0 bit 3 should be set in such a situation. */
11416 if (ret && ret != -TARGET_ERESTARTSYS) {
11417 ((CPUPPCState *)cpu_env)->crf[0] |= 1;
11418 }
11419 #endif
11420 break;
11421 }
11422 #endif
11423
11424 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
11425 case TARGET_NR_set_tid_address:
11426 ret = get_errno(set_tid_address((int *)g2h(arg1)));
11427 break;
11428 #endif
11429
11430 case TARGET_NR_tkill:
11431 ret = get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
11432 break;
11433
11434 case TARGET_NR_tgkill:
11435 ret = get_errno(safe_tgkill((int)arg1, (int)arg2,
11436 target_to_host_signal(arg3)));
11437 break;
11438
11439 #ifdef TARGET_NR_set_robust_list
11440 case TARGET_NR_set_robust_list:
11441 case TARGET_NR_get_robust_list:
11442 /* The ABI for supporting robust futexes has userspace pass
11443 * the kernel a pointer to a linked list which is updated by
11444 * userspace after the syscall; the list is walked by the kernel
11445 * when the thread exits. Since the linked list in QEMU guest
11446 * memory isn't a valid linked list for the host and we have
11447 * no way to reliably intercept the thread-death event, we can't
11448 * support these. Silently return ENOSYS so that guest userspace
11449 * falls back to a non-robust futex implementation (which should
11450 * be OK except in the corner case of the guest crashing while
11451 * holding a mutex that is shared with another process via
11452 * shared memory).
11453 */
11454 goto unimplemented_nowarn;
11455 #endif
11456
11457 #if defined(TARGET_NR_utimensat)
11458 case TARGET_NR_utimensat:
11459 {
11460 struct timespec *tsp, ts[2];
11461 if (!arg3) {
11462 tsp = NULL;
11463 } else {
11464 target_to_host_timespec(ts, arg3);
11465 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
11466 tsp = ts;
11467 }
11468 if (!arg2)
11469 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
11470 else {
11471 if (!(p = lock_user_string(arg2))) {
11472 ret = -TARGET_EFAULT;
11473 goto fail;
11474 }
11475 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
11476 unlock_user(p, arg2, 0);
11477 }
11478 }
11479 break;
11480 #endif
11481 case TARGET_NR_futex:
11482 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
11483 break;
11484 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
11485 case TARGET_NR_inotify_init:
11486 ret = get_errno(sys_inotify_init());
11487 break;
11488 #endif
11489 #ifdef CONFIG_INOTIFY1
11490 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
11491 case TARGET_NR_inotify_init1:
11492 ret = get_errno(sys_inotify_init1(arg1));
11493 break;
11494 #endif
11495 #endif
11496 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
11497 case TARGET_NR_inotify_add_watch:
11498 p = lock_user_string(arg2);
11499 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
11500 unlock_user(p, arg2, 0);
11501 break;
11502 #endif
11503 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
11504 case TARGET_NR_inotify_rm_watch:
11505 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
11506 break;
11507 #endif
11508
11509 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
11510 case TARGET_NR_mq_open:
11511 {
11512 struct mq_attr posix_mq_attr;
11513 int host_flags;
11514
11515 host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
11516 if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
11517 goto efault;
11518 }
11519 p = lock_user_string(arg1 - 1);
11520 if (!p) {
11521 goto efault;
11522 }
11523 ret = get_errno(mq_open(p, host_flags, arg3, &posix_mq_attr));
11524 unlock_user (p, arg1, 0);
11525 }
11526 break;
11527
11528 case TARGET_NR_mq_unlink:
11529 p = lock_user_string(arg1 - 1);
11530 if (!p) {
11531 ret = -TARGET_EFAULT;
11532 break;
11533 }
11534 ret = get_errno(mq_unlink(p));
11535 unlock_user (p, arg1, 0);
11536 break;
11537
11538 case TARGET_NR_mq_timedsend:
11539 {
11540 struct timespec ts;
11541
11542 p = lock_user (VERIFY_READ, arg2, arg3, 1);
11543 if (arg5 != 0) {
11544 target_to_host_timespec(&ts, arg5);
11545 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
11546 host_to_target_timespec(arg5, &ts);
11547 } else {
11548 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
11549 }
11550 unlock_user (p, arg2, arg3);
11551 }
11552 break;
11553
11554 case TARGET_NR_mq_timedreceive:
11555 {
11556 struct timespec ts;
11557 unsigned int prio;
11558
11559 p = lock_user (VERIFY_READ, arg2, arg3, 1);
11560 if (arg5 != 0) {
11561 target_to_host_timespec(&ts, arg5);
11562 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
11563 &prio, &ts));
11564 host_to_target_timespec(arg5, &ts);
11565 } else {
11566 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
11567 &prio, NULL));
11568 }
11569 unlock_user (p, arg2, arg3);
11570 if (arg4 != 0)
11571 put_user_u32(prio, arg4);
11572 }
11573 break;
11574
11575 /* Not implemented for now... */
11576 /* case TARGET_NR_mq_notify: */
11577 /* break; */
11578
11579 case TARGET_NR_mq_getsetattr:
11580 {
11581 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
11582 ret = 0;
11583 if (arg3 != 0) {
11584 ret = mq_getattr(arg1, &posix_mq_attr_out);
11585 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
11586 }
11587 if (arg2 != 0) {
11588 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
11589 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
11590 }
11591
11592 }
11593 break;
11594 #endif
11595
11596 #ifdef CONFIG_SPLICE
11597 #ifdef TARGET_NR_tee
11598 case TARGET_NR_tee:
11599 {
11600 ret = get_errno(tee(arg1,arg2,arg3,arg4));
11601 }
11602 break;
11603 #endif
11604 #ifdef TARGET_NR_splice
11605 case TARGET_NR_splice:
11606 {
11607 loff_t loff_in, loff_out;
11608 loff_t *ploff_in = NULL, *ploff_out = NULL;
11609 if (arg2) {
11610 if (get_user_u64(loff_in, arg2)) {
11611 goto efault;
11612 }
11613 ploff_in = &loff_in;
11614 }
11615 if (arg4) {
11616 if (get_user_u64(loff_out, arg4)) {
11617 goto efault;
11618 }
11619 ploff_out = &loff_out;
11620 }
11621 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
11622 if (arg2) {
11623 if (put_user_u64(loff_in, arg2)) {
11624 goto efault;
11625 }
11626 }
11627 if (arg4) {
11628 if (put_user_u64(loff_out, arg4)) {
11629 goto efault;
11630 }
11631 }
11632 }
11633 break;
11634 #endif
11635 #ifdef TARGET_NR_vmsplice
11636 case TARGET_NR_vmsplice:
11637 {
11638 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
11639 if (vec != NULL) {
11640 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
11641 unlock_iovec(vec, arg2, arg3, 0);
11642 } else {
11643 ret = -host_to_target_errno(errno);
11644 }
11645 }
11646 break;
11647 #endif
11648 #endif /* CONFIG_SPLICE */
11649 #ifdef CONFIG_EVENTFD
11650 #if defined(TARGET_NR_eventfd)
11651 case TARGET_NR_eventfd:
11652 ret = get_errno(eventfd(arg1, 0));
11653 fd_trans_unregister(ret);
11654 break;
11655 #endif
11656 #if defined(TARGET_NR_eventfd2)
11657 case TARGET_NR_eventfd2:
11658 {
11659 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
11660 if (arg2 & TARGET_O_NONBLOCK) {
11661 host_flags |= O_NONBLOCK;
11662 }
11663 if (arg2 & TARGET_O_CLOEXEC) {
11664 host_flags |= O_CLOEXEC;
11665 }
11666 ret = get_errno(eventfd(arg1, host_flags));
11667 fd_trans_unregister(ret);
11668 break;
11669 }
11670 #endif
11671 #endif /* CONFIG_EVENTFD */
11672 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
11673 case TARGET_NR_fallocate:
11674 #if TARGET_ABI_BITS == 32
11675 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
11676 target_offset64(arg5, arg6)));
11677 #else
11678 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
11679 #endif
11680 break;
11681 #endif
11682 #if defined(CONFIG_SYNC_FILE_RANGE)
11683 #if defined(TARGET_NR_sync_file_range)
11684 case TARGET_NR_sync_file_range:
11685 #if TARGET_ABI_BITS == 32
11686 #if defined(TARGET_MIPS)
11687 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
11688 target_offset64(arg5, arg6), arg7));
11689 #else
11690 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
11691 target_offset64(arg4, arg5), arg6));
11692 #endif /* !TARGET_MIPS */
11693 #else
11694 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
11695 #endif
11696 break;
11697 #endif
11698 #if defined(TARGET_NR_sync_file_range2)
11699 case TARGET_NR_sync_file_range2:
11700 /* This is like sync_file_range but the arguments are reordered */
11701 #if TARGET_ABI_BITS == 32
11702 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
11703 target_offset64(arg5, arg6), arg2));
11704 #else
11705 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
11706 #endif
11707 break;
11708 #endif
11709 #endif
11710 #if defined(TARGET_NR_signalfd4)
11711 case TARGET_NR_signalfd4:
11712 ret = do_signalfd4(arg1, arg2, arg4);
11713 break;
11714 #endif
11715 #if defined(TARGET_NR_signalfd)
11716 case TARGET_NR_signalfd:
11717 ret = do_signalfd4(arg1, arg2, 0);
11718 break;
11719 #endif
11720 #if defined(CONFIG_EPOLL)
11721 #if defined(TARGET_NR_epoll_create)
11722 case TARGET_NR_epoll_create:
11723 ret = get_errno(epoll_create(arg1));
11724 break;
11725 #endif
11726 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
11727 case TARGET_NR_epoll_create1:
11728 ret = get_errno(epoll_create1(arg1));
11729 break;
11730 #endif
11731 #if defined(TARGET_NR_epoll_ctl)
11732 case TARGET_NR_epoll_ctl:
11733 {
11734 struct epoll_event ep;
11735 struct epoll_event *epp = 0;
11736 if (arg4) {
11737 struct target_epoll_event *target_ep;
11738 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
11739 goto efault;
11740 }
11741 ep.events = tswap32(target_ep->events);
11742 /* The epoll_data_t union is just opaque data to the kernel,
11743 * so we transfer all 64 bits across and need not worry what
11744 * actual data type it is.
11745 */
11746 ep.data.u64 = tswap64(target_ep->data.u64);
11747 unlock_user_struct(target_ep, arg4, 0);
11748 epp = &ep;
11749 }
11750 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
11751 break;
11752 }
11753 #endif
11754
11755 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
11756 #if defined(TARGET_NR_epoll_wait)
11757 case TARGET_NR_epoll_wait:
11758 #endif
11759 #if defined(TARGET_NR_epoll_pwait)
11760 case TARGET_NR_epoll_pwait:
11761 #endif
11762 {
11763 struct target_epoll_event *target_ep;
11764 struct epoll_event *ep;
11765 int epfd = arg1;
11766 int maxevents = arg3;
11767 int timeout = arg4;
11768
11769 if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
11770 ret = -TARGET_EINVAL;
11771 break;
11772 }
11773
11774 target_ep = lock_user(VERIFY_WRITE, arg2,
11775 maxevents * sizeof(struct target_epoll_event), 1);
11776 if (!target_ep) {
11777 goto efault;
11778 }
11779
11780 ep = g_try_new(struct epoll_event, maxevents);
11781 if (!ep) {
11782 unlock_user(target_ep, arg2, 0);
11783 ret = -TARGET_ENOMEM;
11784 break;
11785 }
11786
11787 switch (num) {
11788 #if defined(TARGET_NR_epoll_pwait)
11789 case TARGET_NR_epoll_pwait:
11790 {
11791 target_sigset_t *target_set;
11792 sigset_t _set, *set = &_set;
11793
11794 if (arg5) {
11795 if (arg6 != sizeof(target_sigset_t)) {
11796 ret = -TARGET_EINVAL;
11797 break;
11798 }
11799
11800 target_set = lock_user(VERIFY_READ, arg5,
11801 sizeof(target_sigset_t), 1);
11802 if (!target_set) {
11803 ret = -TARGET_EFAULT;
11804 break;
11805 }
11806 target_to_host_sigset(set, target_set);
11807 unlock_user(target_set, arg5, 0);
11808 } else {
11809 set = NULL;
11810 }
11811
11812 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
11813 set, SIGSET_T_SIZE));
11814 break;
11815 }
11816 #endif
11817 #if defined(TARGET_NR_epoll_wait)
11818 case TARGET_NR_epoll_wait:
11819 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
11820 NULL, 0));
11821 break;
11822 #endif
11823 default:
11824 ret = -TARGET_ENOSYS;
11825 }
11826 if (!is_error(ret)) {
11827 int i;
11828 for (i = 0; i < ret; i++) {
11829 target_ep[i].events = tswap32(ep[i].events);
11830 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
11831 }
11832 unlock_user(target_ep, arg2,
11833 ret * sizeof(struct target_epoll_event));
11834 } else {
11835 unlock_user(target_ep, arg2, 0);
11836 }
11837 g_free(ep);
11838 break;
11839 }
11840 #endif
11841 #endif
11842 #ifdef TARGET_NR_prlimit64
11843 case TARGET_NR_prlimit64:
11844 {
11845 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
11846 struct target_rlimit64 *target_rnew, *target_rold;
11847 struct host_rlimit64 rnew, rold, *rnewp = 0;
11848 int resource = target_to_host_resource(arg2);
11849 if (arg3) {
11850 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
11851 goto efault;
11852 }
11853 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
11854 rnew.rlim_max = tswap64(target_rnew->rlim_max);
11855 unlock_user_struct(target_rnew, arg3, 0);
11856 rnewp = &rnew;
11857 }
11858
11859 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
11860 if (!is_error(ret) && arg4) {
11861 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
11862 goto efault;
11863 }
11864 target_rold->rlim_cur = tswap64(rold.rlim_cur);
11865 target_rold->rlim_max = tswap64(rold.rlim_max);
11866 unlock_user_struct(target_rold, arg4, 1);
11867 }
11868 break;
11869 }
11870 #endif
11871 #ifdef TARGET_NR_gethostname
11872 case TARGET_NR_gethostname:
11873 {
11874 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
11875 if (name) {
11876 ret = get_errno(gethostname(name, arg2));
11877 unlock_user(name, arg1, arg2);
11878 } else {
11879 ret = -TARGET_EFAULT;
11880 }
11881 break;
11882 }
11883 #endif
11884 #ifdef TARGET_NR_atomic_cmpxchg_32
11885 case TARGET_NR_atomic_cmpxchg_32:
11886 {
11887 /* should use start_exclusive from main.c */
11888 abi_ulong mem_value;
11889 if (get_user_u32(mem_value, arg6)) {
11890 target_siginfo_t info;
11891 info.si_signo = SIGSEGV;
11892 info.si_errno = 0;
11893 info.si_code = TARGET_SEGV_MAPERR;
11894 info._sifields._sigfault._addr = arg6;
11895 queue_signal((CPUArchState *)cpu_env, info.si_signo,
11896 QEMU_SI_FAULT, &info);
11897 ret = 0xdeadbeef;
11898
11899 }
11900 if (mem_value == arg2)
11901 put_user_u32(arg1, arg6);
11902 ret = mem_value;
11903 break;
11904 }
11905 #endif
11906 #ifdef TARGET_NR_atomic_barrier
11907 case TARGET_NR_atomic_barrier:
11908 {
11909 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
11910 ret = 0;
11911 break;
11912 }
11913 #endif
11914
11915 #ifdef TARGET_NR_timer_create
11916 case TARGET_NR_timer_create:
11917 {
11918 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
11919
11920 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
11921
11922 int clkid = arg1;
11923 int timer_index = next_free_host_timer();
11924
11925 if (timer_index < 0) {
11926 ret = -TARGET_EAGAIN;
11927 } else {
11928 timer_t *phtimer = g_posix_timers + timer_index;
11929
11930 if (arg2) {
11931 phost_sevp = &host_sevp;
11932 ret = target_to_host_sigevent(phost_sevp, arg2);
11933 if (ret != 0) {
11934 break;
11935 }
11936 }
11937
11938 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
11939 if (ret) {
11940 phtimer = NULL;
11941 } else {
11942 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
11943 goto efault;
11944 }
11945 }
11946 }
11947 break;
11948 }
11949 #endif
11950
11951 #ifdef TARGET_NR_timer_settime
11952 case TARGET_NR_timer_settime:
11953 {
11954 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
11955 * struct itimerspec * old_value */
11956 target_timer_t timerid = get_timer_id(arg1);
11957
11958 if (timerid < 0) {
11959 ret = timerid;
11960 } else if (arg3 == 0) {
11961 ret = -TARGET_EINVAL;
11962 } else {
11963 timer_t htimer = g_posix_timers[timerid];
11964 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
11965
11966 target_to_host_itimerspec(&hspec_new, arg3);
11967 ret = get_errno(
11968 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
11969 host_to_target_itimerspec(arg2, &hspec_old);
11970 }
11971 break;
11972 }
11973 #endif
11974
11975 #ifdef TARGET_NR_timer_gettime
11976 case TARGET_NR_timer_gettime:
11977 {
11978 /* args: timer_t timerid, struct itimerspec *curr_value */
11979 target_timer_t timerid = get_timer_id(arg1);
11980
11981 if (timerid < 0) {
11982 ret = timerid;
11983 } else if (!arg2) {
11984 ret = -TARGET_EFAULT;
11985 } else {
11986 timer_t htimer = g_posix_timers[timerid];
11987 struct itimerspec hspec;
11988 ret = get_errno(timer_gettime(htimer, &hspec));
11989
11990 if (host_to_target_itimerspec(arg2, &hspec)) {
11991 ret = -TARGET_EFAULT;
11992 }
11993 }
11994 break;
11995 }
11996 #endif
11997
11998 #ifdef TARGET_NR_timer_getoverrun
11999 case TARGET_NR_timer_getoverrun:
12000 {
12001 /* args: timer_t timerid */
12002 target_timer_t timerid = get_timer_id(arg1);
12003
12004 if (timerid < 0) {
12005 ret = timerid;
12006 } else {
12007 timer_t htimer = g_posix_timers[timerid];
12008 ret = get_errno(timer_getoverrun(htimer));
12009 }
12010 fd_trans_unregister(ret);
12011 break;
12012 }
12013 #endif
12014
12015 #ifdef TARGET_NR_timer_delete
12016 case TARGET_NR_timer_delete:
12017 {
12018 /* args: timer_t timerid */
12019 target_timer_t timerid = get_timer_id(arg1);
12020
12021 if (timerid < 0) {
12022 ret = timerid;
12023 } else {
12024 timer_t htimer = g_posix_timers[timerid];
12025 ret = get_errno(timer_delete(htimer));
12026 g_posix_timers[timerid] = 0;
12027 }
12028 break;
12029 }
12030 #endif
12031
12032 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
12033 case TARGET_NR_timerfd_create:
12034 ret = get_errno(timerfd_create(arg1,
12035 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
12036 break;
12037 #endif
12038
12039 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
12040 case TARGET_NR_timerfd_gettime:
12041 {
12042 struct itimerspec its_curr;
12043
12044 ret = get_errno(timerfd_gettime(arg1, &its_curr));
12045
12046 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
12047 goto efault;
12048 }
12049 }
12050 break;
12051 #endif
12052
12053 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
12054 case TARGET_NR_timerfd_settime:
12055 {
12056 struct itimerspec its_new, its_old, *p_new;
12057
12058 if (arg3) {
12059 if (target_to_host_itimerspec(&its_new, arg3)) {
12060 goto efault;
12061 }
12062 p_new = &its_new;
12063 } else {
12064 p_new = NULL;
12065 }
12066
12067 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
12068
12069 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
12070 goto efault;
12071 }
12072 }
12073 break;
12074 #endif
12075
12076 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
12077 case TARGET_NR_ioprio_get:
12078 ret = get_errno(ioprio_get(arg1, arg2));
12079 break;
12080 #endif
12081
12082 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
12083 case TARGET_NR_ioprio_set:
12084 ret = get_errno(ioprio_set(arg1, arg2, arg3));
12085 break;
12086 #endif
12087
12088 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
12089 case TARGET_NR_setns:
12090 ret = get_errno(setns(arg1, arg2));
12091 break;
12092 #endif
12093 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
12094 case TARGET_NR_unshare:
12095 ret = get_errno(unshare(arg1));
12096 break;
12097 #endif
12098 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
12099 case TARGET_NR_kcmp:
12100 ret = get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
12101 break;
12102 #endif
12103
12104 default:
12105 unimplemented:
12106 gemu_log("qemu: Unsupported syscall: %d\n", num);
12107 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
12108 unimplemented_nowarn:
12109 #endif
12110 ret = -TARGET_ENOSYS;
12111 break;
12112 }
12113 fail:
12114 #ifdef DEBUG
12115 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
12116 #endif
12117 if(do_strace)
12118 print_syscall_ret(num, ret);
12119 trace_guest_user_syscall_ret(cpu, num, ret);
12120 return ret;
12121 efault:
12122 ret = -TARGET_EFAULT;
12123 goto fail;
12124 }