quorum: Inline quorum_aio_cb()
[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 <time.h>
52 #include <utime.h>
53 #include <sys/sysinfo.h>
54 #include <sys/signalfd.h>
55 //#include <sys/user.h>
56 #include <netinet/ip.h>
57 #include <netinet/tcp.h>
58 #include <linux/wireless.h>
59 #include <linux/icmp.h>
60 #include "qemu-common.h"
61 #ifdef CONFIG_TIMERFD
62 #include <sys/timerfd.h>
63 #endif
64 #ifdef TARGET_GPROF
65 #include <sys/gmon.h>
66 #endif
67 #ifdef CONFIG_EVENTFD
68 #include <sys/eventfd.h>
69 #endif
70 #ifdef CONFIG_EPOLL
71 #include <sys/epoll.h>
72 #endif
73 #ifdef CONFIG_ATTR
74 #include "qemu/xattr.h"
75 #endif
76 #ifdef CONFIG_SENDFILE
77 #include <sys/sendfile.h>
78 #endif
79
80 #define termios host_termios
81 #define winsize host_winsize
82 #define termio host_termio
83 #define sgttyb host_sgttyb /* same as target */
84 #define tchars host_tchars /* same as target */
85 #define ltchars host_ltchars /* same as target */
86
87 #include <linux/termios.h>
88 #include <linux/unistd.h>
89 #include <linux/cdrom.h>
90 #include <linux/hdreg.h>
91 #include <linux/soundcard.h>
92 #include <linux/kd.h>
93 #include <linux/mtio.h>
94 #include <linux/fs.h>
95 #if defined(CONFIG_FIEMAP)
96 #include <linux/fiemap.h>
97 #endif
98 #include <linux/fb.h>
99 #include <linux/vt.h>
100 #include <linux/dm-ioctl.h>
101 #include <linux/reboot.h>
102 #include <linux/route.h>
103 #include <linux/filter.h>
104 #include <linux/blkpg.h>
105 #include <netpacket/packet.h>
106 #include <linux/netlink.h>
107 #ifdef CONFIG_RTNETLINK
108 #include <linux/rtnetlink.h>
109 #include <linux/if_bridge.h>
110 #endif
111 #include <linux/audit.h>
112 #include "linux_loop.h"
113 #include "uname.h"
114
115 #include "qemu.h"
116
117 #ifndef CLONE_IO
118 #define CLONE_IO 0x80000000 /* Clone io context */
119 #endif
120
121 /* We can't directly call the host clone syscall, because this will
122 * badly confuse libc (breaking mutexes, for example). So we must
123 * divide clone flags into:
124 * * flag combinations that look like pthread_create()
125 * * flag combinations that look like fork()
126 * * flags we can implement within QEMU itself
127 * * flags we can't support and will return an error for
128 */
129 /* For thread creation, all these flags must be present; for
130 * fork, none must be present.
131 */
132 #define CLONE_THREAD_FLAGS \
133 (CLONE_VM | CLONE_FS | CLONE_FILES | \
134 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
135
136 /* These flags are ignored:
137 * CLONE_DETACHED is now ignored by the kernel;
138 * CLONE_IO is just an optimisation hint to the I/O scheduler
139 */
140 #define CLONE_IGNORED_FLAGS \
141 (CLONE_DETACHED | CLONE_IO)
142
143 /* Flags for fork which we can implement within QEMU itself */
144 #define CLONE_OPTIONAL_FORK_FLAGS \
145 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
146 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
147
148 /* Flags for thread creation which we can implement within QEMU itself */
149 #define CLONE_OPTIONAL_THREAD_FLAGS \
150 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
151 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
152
153 #define CLONE_INVALID_FORK_FLAGS \
154 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
155
156 #define CLONE_INVALID_THREAD_FLAGS \
157 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
158 CLONE_IGNORED_FLAGS))
159
160 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
161 * have almost all been allocated. We cannot support any of
162 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
163 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
164 * The checks against the invalid thread masks above will catch these.
165 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
166 */
167
168 //#define DEBUG
169 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
170 * once. This exercises the codepaths for restart.
171 */
172 //#define DEBUG_ERESTARTSYS
173
174 //#include <linux/msdos_fs.h>
175 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
176 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
177
178 #undef _syscall0
179 #undef _syscall1
180 #undef _syscall2
181 #undef _syscall3
182 #undef _syscall4
183 #undef _syscall5
184 #undef _syscall6
185
186 #define _syscall0(type,name) \
187 static type name (void) \
188 { \
189 return syscall(__NR_##name); \
190 }
191
192 #define _syscall1(type,name,type1,arg1) \
193 static type name (type1 arg1) \
194 { \
195 return syscall(__NR_##name, arg1); \
196 }
197
198 #define _syscall2(type,name,type1,arg1,type2,arg2) \
199 static type name (type1 arg1,type2 arg2) \
200 { \
201 return syscall(__NR_##name, arg1, arg2); \
202 }
203
204 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
205 static type name (type1 arg1,type2 arg2,type3 arg3) \
206 { \
207 return syscall(__NR_##name, arg1, arg2, arg3); \
208 }
209
210 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
211 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
212 { \
213 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
214 }
215
216 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
217 type5,arg5) \
218 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
219 { \
220 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
221 }
222
223
224 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
225 type5,arg5,type6,arg6) \
226 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
227 type6 arg6) \
228 { \
229 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
230 }
231
232
233 #define __NR_sys_uname __NR_uname
234 #define __NR_sys_getcwd1 __NR_getcwd
235 #define __NR_sys_getdents __NR_getdents
236 #define __NR_sys_getdents64 __NR_getdents64
237 #define __NR_sys_getpriority __NR_getpriority
238 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
239 #define __NR_sys_syslog __NR_syslog
240 #define __NR_sys_futex __NR_futex
241 #define __NR_sys_inotify_init __NR_inotify_init
242 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
243 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
244
245 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
246 defined(__s390x__)
247 #define __NR__llseek __NR_lseek
248 #endif
249
250 /* Newer kernel ports have llseek() instead of _llseek() */
251 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
252 #define TARGET_NR__llseek TARGET_NR_llseek
253 #endif
254
255 #ifdef __NR_gettid
256 _syscall0(int, gettid)
257 #else
258 /* This is a replacement for the host gettid() and must return a host
259 errno. */
260 static int gettid(void) {
261 return -ENOSYS;
262 }
263 #endif
264 #if defined(TARGET_NR_getdents) && defined(__NR_getdents)
265 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
266 #endif
267 #if !defined(__NR_getdents) || \
268 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
269 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
270 #endif
271 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
272 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
273 loff_t *, res, uint, wh);
274 #endif
275 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
276 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
277 #ifdef __NR_exit_group
278 _syscall1(int,exit_group,int,error_code)
279 #endif
280 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
281 _syscall1(int,set_tid_address,int *,tidptr)
282 #endif
283 #if defined(TARGET_NR_futex) && defined(__NR_futex)
284 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
285 const struct timespec *,timeout,int *,uaddr2,int,val3)
286 #endif
287 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
288 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
289 unsigned long *, user_mask_ptr);
290 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
291 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
292 unsigned long *, user_mask_ptr);
293 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
294 void *, arg);
295 _syscall2(int, capget, struct __user_cap_header_struct *, header,
296 struct __user_cap_data_struct *, data);
297 _syscall2(int, capset, struct __user_cap_header_struct *, header,
298 struct __user_cap_data_struct *, data);
299 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
300 _syscall2(int, ioprio_get, int, which, int, who)
301 #endif
302 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
303 _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
304 #endif
305 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
306 _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
307 #endif
308
309 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
310 _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
311 unsigned long, idx1, unsigned long, idx2)
312 #endif
313
314 static bitmask_transtbl fcntl_flags_tbl[] = {
315 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
316 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
317 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
318 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
319 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
320 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
321 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
322 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
323 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
324 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
325 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
326 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
327 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
328 #if defined(O_DIRECT)
329 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
330 #endif
331 #if defined(O_NOATIME)
332 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
333 #endif
334 #if defined(O_CLOEXEC)
335 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
336 #endif
337 #if defined(O_PATH)
338 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
339 #endif
340 /* Don't terminate the list prematurely on 64-bit host+guest. */
341 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
342 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
343 #endif
344 { 0, 0, 0, 0 }
345 };
346
347 enum {
348 QEMU_IFLA_BR_UNSPEC,
349 QEMU_IFLA_BR_FORWARD_DELAY,
350 QEMU_IFLA_BR_HELLO_TIME,
351 QEMU_IFLA_BR_MAX_AGE,
352 QEMU_IFLA_BR_AGEING_TIME,
353 QEMU_IFLA_BR_STP_STATE,
354 QEMU_IFLA_BR_PRIORITY,
355 QEMU_IFLA_BR_VLAN_FILTERING,
356 QEMU_IFLA_BR_VLAN_PROTOCOL,
357 QEMU_IFLA_BR_GROUP_FWD_MASK,
358 QEMU_IFLA_BR_ROOT_ID,
359 QEMU_IFLA_BR_BRIDGE_ID,
360 QEMU_IFLA_BR_ROOT_PORT,
361 QEMU_IFLA_BR_ROOT_PATH_COST,
362 QEMU_IFLA_BR_TOPOLOGY_CHANGE,
363 QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
364 QEMU_IFLA_BR_HELLO_TIMER,
365 QEMU_IFLA_BR_TCN_TIMER,
366 QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER,
367 QEMU_IFLA_BR_GC_TIMER,
368 QEMU_IFLA_BR_GROUP_ADDR,
369 QEMU_IFLA_BR_FDB_FLUSH,
370 QEMU_IFLA_BR_MCAST_ROUTER,
371 QEMU_IFLA_BR_MCAST_SNOOPING,
372 QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR,
373 QEMU_IFLA_BR_MCAST_QUERIER,
374 QEMU_IFLA_BR_MCAST_HASH_ELASTICITY,
375 QEMU_IFLA_BR_MCAST_HASH_MAX,
376 QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT,
377 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT,
378 QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL,
379 QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL,
380 QEMU_IFLA_BR_MCAST_QUERIER_INTVL,
381 QEMU_IFLA_BR_MCAST_QUERY_INTVL,
382 QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL,
383 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL,
384 QEMU_IFLA_BR_NF_CALL_IPTABLES,
385 QEMU_IFLA_BR_NF_CALL_IP6TABLES,
386 QEMU_IFLA_BR_NF_CALL_ARPTABLES,
387 QEMU_IFLA_BR_VLAN_DEFAULT_PVID,
388 QEMU_IFLA_BR_PAD,
389 QEMU_IFLA_BR_VLAN_STATS_ENABLED,
390 QEMU_IFLA_BR_MCAST_STATS_ENABLED,
391 QEMU___IFLA_BR_MAX,
392 };
393
394 enum {
395 QEMU_IFLA_UNSPEC,
396 QEMU_IFLA_ADDRESS,
397 QEMU_IFLA_BROADCAST,
398 QEMU_IFLA_IFNAME,
399 QEMU_IFLA_MTU,
400 QEMU_IFLA_LINK,
401 QEMU_IFLA_QDISC,
402 QEMU_IFLA_STATS,
403 QEMU_IFLA_COST,
404 QEMU_IFLA_PRIORITY,
405 QEMU_IFLA_MASTER,
406 QEMU_IFLA_WIRELESS,
407 QEMU_IFLA_PROTINFO,
408 QEMU_IFLA_TXQLEN,
409 QEMU_IFLA_MAP,
410 QEMU_IFLA_WEIGHT,
411 QEMU_IFLA_OPERSTATE,
412 QEMU_IFLA_LINKMODE,
413 QEMU_IFLA_LINKINFO,
414 QEMU_IFLA_NET_NS_PID,
415 QEMU_IFLA_IFALIAS,
416 QEMU_IFLA_NUM_VF,
417 QEMU_IFLA_VFINFO_LIST,
418 QEMU_IFLA_STATS64,
419 QEMU_IFLA_VF_PORTS,
420 QEMU_IFLA_PORT_SELF,
421 QEMU_IFLA_AF_SPEC,
422 QEMU_IFLA_GROUP,
423 QEMU_IFLA_NET_NS_FD,
424 QEMU_IFLA_EXT_MASK,
425 QEMU_IFLA_PROMISCUITY,
426 QEMU_IFLA_NUM_TX_QUEUES,
427 QEMU_IFLA_NUM_RX_QUEUES,
428 QEMU_IFLA_CARRIER,
429 QEMU_IFLA_PHYS_PORT_ID,
430 QEMU_IFLA_CARRIER_CHANGES,
431 QEMU_IFLA_PHYS_SWITCH_ID,
432 QEMU_IFLA_LINK_NETNSID,
433 QEMU_IFLA_PHYS_PORT_NAME,
434 QEMU_IFLA_PROTO_DOWN,
435 QEMU_IFLA_GSO_MAX_SEGS,
436 QEMU_IFLA_GSO_MAX_SIZE,
437 QEMU_IFLA_PAD,
438 QEMU_IFLA_XDP,
439 QEMU___IFLA_MAX
440 };
441
442 enum {
443 QEMU_IFLA_BRPORT_UNSPEC,
444 QEMU_IFLA_BRPORT_STATE,
445 QEMU_IFLA_BRPORT_PRIORITY,
446 QEMU_IFLA_BRPORT_COST,
447 QEMU_IFLA_BRPORT_MODE,
448 QEMU_IFLA_BRPORT_GUARD,
449 QEMU_IFLA_BRPORT_PROTECT,
450 QEMU_IFLA_BRPORT_FAST_LEAVE,
451 QEMU_IFLA_BRPORT_LEARNING,
452 QEMU_IFLA_BRPORT_UNICAST_FLOOD,
453 QEMU_IFLA_BRPORT_PROXYARP,
454 QEMU_IFLA_BRPORT_LEARNING_SYNC,
455 QEMU_IFLA_BRPORT_PROXYARP_WIFI,
456 QEMU_IFLA_BRPORT_ROOT_ID,
457 QEMU_IFLA_BRPORT_BRIDGE_ID,
458 QEMU_IFLA_BRPORT_DESIGNATED_PORT,
459 QEMU_IFLA_BRPORT_DESIGNATED_COST,
460 QEMU_IFLA_BRPORT_ID,
461 QEMU_IFLA_BRPORT_NO,
462 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
463 QEMU_IFLA_BRPORT_CONFIG_PENDING,
464 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER,
465 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER,
466 QEMU_IFLA_BRPORT_HOLD_TIMER,
467 QEMU_IFLA_BRPORT_FLUSH,
468 QEMU_IFLA_BRPORT_MULTICAST_ROUTER,
469 QEMU_IFLA_BRPORT_PAD,
470 QEMU___IFLA_BRPORT_MAX
471 };
472
473 enum {
474 QEMU_IFLA_INFO_UNSPEC,
475 QEMU_IFLA_INFO_KIND,
476 QEMU_IFLA_INFO_DATA,
477 QEMU_IFLA_INFO_XSTATS,
478 QEMU_IFLA_INFO_SLAVE_KIND,
479 QEMU_IFLA_INFO_SLAVE_DATA,
480 QEMU___IFLA_INFO_MAX,
481 };
482
483 enum {
484 QEMU_IFLA_INET_UNSPEC,
485 QEMU_IFLA_INET_CONF,
486 QEMU___IFLA_INET_MAX,
487 };
488
489 enum {
490 QEMU_IFLA_INET6_UNSPEC,
491 QEMU_IFLA_INET6_FLAGS,
492 QEMU_IFLA_INET6_CONF,
493 QEMU_IFLA_INET6_STATS,
494 QEMU_IFLA_INET6_MCAST,
495 QEMU_IFLA_INET6_CACHEINFO,
496 QEMU_IFLA_INET6_ICMP6STATS,
497 QEMU_IFLA_INET6_TOKEN,
498 QEMU_IFLA_INET6_ADDR_GEN_MODE,
499 QEMU___IFLA_INET6_MAX
500 };
501
502 typedef abi_long (*TargetFdDataFunc)(void *, size_t);
503 typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
504 typedef struct TargetFdTrans {
505 TargetFdDataFunc host_to_target_data;
506 TargetFdDataFunc target_to_host_data;
507 TargetFdAddrFunc target_to_host_addr;
508 } TargetFdTrans;
509
510 static TargetFdTrans **target_fd_trans;
511
512 static unsigned int target_fd_max;
513
514 static TargetFdDataFunc fd_trans_target_to_host_data(int fd)
515 {
516 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
517 return target_fd_trans[fd]->target_to_host_data;
518 }
519 return NULL;
520 }
521
522 static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
523 {
524 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
525 return target_fd_trans[fd]->host_to_target_data;
526 }
527 return NULL;
528 }
529
530 static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
531 {
532 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
533 return target_fd_trans[fd]->target_to_host_addr;
534 }
535 return NULL;
536 }
537
538 static void fd_trans_register(int fd, TargetFdTrans *trans)
539 {
540 unsigned int oldmax;
541
542 if (fd >= target_fd_max) {
543 oldmax = target_fd_max;
544 target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
545 target_fd_trans = g_renew(TargetFdTrans *,
546 target_fd_trans, target_fd_max);
547 memset((void *)(target_fd_trans + oldmax), 0,
548 (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
549 }
550 target_fd_trans[fd] = trans;
551 }
552
553 static void fd_trans_unregister(int fd)
554 {
555 if (fd >= 0 && fd < target_fd_max) {
556 target_fd_trans[fd] = NULL;
557 }
558 }
559
560 static void fd_trans_dup(int oldfd, int newfd)
561 {
562 fd_trans_unregister(newfd);
563 if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
564 fd_trans_register(newfd, target_fd_trans[oldfd]);
565 }
566 }
567
568 static int sys_getcwd1(char *buf, size_t size)
569 {
570 if (getcwd(buf, size) == NULL) {
571 /* getcwd() sets errno */
572 return (-1);
573 }
574 return strlen(buf)+1;
575 }
576
577 #ifdef TARGET_NR_utimensat
578 #if defined(__NR_utimensat)
579 #define __NR_sys_utimensat __NR_utimensat
580 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
581 const struct timespec *,tsp,int,flags)
582 #else
583 static int sys_utimensat(int dirfd, const char *pathname,
584 const struct timespec times[2], int flags)
585 {
586 errno = ENOSYS;
587 return -1;
588 }
589 #endif
590 #endif /* TARGET_NR_utimensat */
591
592 #ifdef CONFIG_INOTIFY
593 #include <sys/inotify.h>
594
595 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
596 static int sys_inotify_init(void)
597 {
598 return (inotify_init());
599 }
600 #endif
601 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
602 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
603 {
604 return (inotify_add_watch(fd, pathname, mask));
605 }
606 #endif
607 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
608 static int sys_inotify_rm_watch(int fd, int32_t wd)
609 {
610 return (inotify_rm_watch(fd, wd));
611 }
612 #endif
613 #ifdef CONFIG_INOTIFY1
614 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
615 static int sys_inotify_init1(int flags)
616 {
617 return (inotify_init1(flags));
618 }
619 #endif
620 #endif
621 #else
622 /* Userspace can usually survive runtime without inotify */
623 #undef TARGET_NR_inotify_init
624 #undef TARGET_NR_inotify_init1
625 #undef TARGET_NR_inotify_add_watch
626 #undef TARGET_NR_inotify_rm_watch
627 #endif /* CONFIG_INOTIFY */
628
629 #if defined(TARGET_NR_prlimit64)
630 #ifndef __NR_prlimit64
631 # define __NR_prlimit64 -1
632 #endif
633 #define __NR_sys_prlimit64 __NR_prlimit64
634 /* The glibc rlimit structure may not be that used by the underlying syscall */
635 struct host_rlimit64 {
636 uint64_t rlim_cur;
637 uint64_t rlim_max;
638 };
639 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
640 const struct host_rlimit64 *, new_limit,
641 struct host_rlimit64 *, old_limit)
642 #endif
643
644
645 #if defined(TARGET_NR_timer_create)
646 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
647 static timer_t g_posix_timers[32] = { 0, } ;
648
649 static inline int next_free_host_timer(void)
650 {
651 int k ;
652 /* FIXME: Does finding the next free slot require a lock? */
653 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
654 if (g_posix_timers[k] == 0) {
655 g_posix_timers[k] = (timer_t) 1;
656 return k;
657 }
658 }
659 return -1;
660 }
661 #endif
662
663 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
664 #ifdef TARGET_ARM
665 static inline int regpairs_aligned(void *cpu_env) {
666 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
667 }
668 #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
669 static inline int regpairs_aligned(void *cpu_env) { return 1; }
670 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
671 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
672 * of registers which translates to the same as ARM/MIPS, because we start with
673 * r3 as arg1 */
674 static inline int regpairs_aligned(void *cpu_env) { return 1; }
675 #else
676 static inline int regpairs_aligned(void *cpu_env) { return 0; }
677 #endif
678
679 #define ERRNO_TABLE_SIZE 1200
680
681 /* target_to_host_errno_table[] is initialized from
682 * host_to_target_errno_table[] in syscall_init(). */
683 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
684 };
685
686 /*
687 * This list is the union of errno values overridden in asm-<arch>/errno.h
688 * minus the errnos that are not actually generic to all archs.
689 */
690 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
691 [EAGAIN] = TARGET_EAGAIN,
692 [EIDRM] = TARGET_EIDRM,
693 [ECHRNG] = TARGET_ECHRNG,
694 [EL2NSYNC] = TARGET_EL2NSYNC,
695 [EL3HLT] = TARGET_EL3HLT,
696 [EL3RST] = TARGET_EL3RST,
697 [ELNRNG] = TARGET_ELNRNG,
698 [EUNATCH] = TARGET_EUNATCH,
699 [ENOCSI] = TARGET_ENOCSI,
700 [EL2HLT] = TARGET_EL2HLT,
701 [EDEADLK] = TARGET_EDEADLK,
702 [ENOLCK] = TARGET_ENOLCK,
703 [EBADE] = TARGET_EBADE,
704 [EBADR] = TARGET_EBADR,
705 [EXFULL] = TARGET_EXFULL,
706 [ENOANO] = TARGET_ENOANO,
707 [EBADRQC] = TARGET_EBADRQC,
708 [EBADSLT] = TARGET_EBADSLT,
709 [EBFONT] = TARGET_EBFONT,
710 [ENOSTR] = TARGET_ENOSTR,
711 [ENODATA] = TARGET_ENODATA,
712 [ETIME] = TARGET_ETIME,
713 [ENOSR] = TARGET_ENOSR,
714 [ENONET] = TARGET_ENONET,
715 [ENOPKG] = TARGET_ENOPKG,
716 [EREMOTE] = TARGET_EREMOTE,
717 [ENOLINK] = TARGET_ENOLINK,
718 [EADV] = TARGET_EADV,
719 [ESRMNT] = TARGET_ESRMNT,
720 [ECOMM] = TARGET_ECOMM,
721 [EPROTO] = TARGET_EPROTO,
722 [EDOTDOT] = TARGET_EDOTDOT,
723 [EMULTIHOP] = TARGET_EMULTIHOP,
724 [EBADMSG] = TARGET_EBADMSG,
725 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
726 [EOVERFLOW] = TARGET_EOVERFLOW,
727 [ENOTUNIQ] = TARGET_ENOTUNIQ,
728 [EBADFD] = TARGET_EBADFD,
729 [EREMCHG] = TARGET_EREMCHG,
730 [ELIBACC] = TARGET_ELIBACC,
731 [ELIBBAD] = TARGET_ELIBBAD,
732 [ELIBSCN] = TARGET_ELIBSCN,
733 [ELIBMAX] = TARGET_ELIBMAX,
734 [ELIBEXEC] = TARGET_ELIBEXEC,
735 [EILSEQ] = TARGET_EILSEQ,
736 [ENOSYS] = TARGET_ENOSYS,
737 [ELOOP] = TARGET_ELOOP,
738 [ERESTART] = TARGET_ERESTART,
739 [ESTRPIPE] = TARGET_ESTRPIPE,
740 [ENOTEMPTY] = TARGET_ENOTEMPTY,
741 [EUSERS] = TARGET_EUSERS,
742 [ENOTSOCK] = TARGET_ENOTSOCK,
743 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
744 [EMSGSIZE] = TARGET_EMSGSIZE,
745 [EPROTOTYPE] = TARGET_EPROTOTYPE,
746 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
747 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
748 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
749 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
750 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
751 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
752 [EADDRINUSE] = TARGET_EADDRINUSE,
753 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
754 [ENETDOWN] = TARGET_ENETDOWN,
755 [ENETUNREACH] = TARGET_ENETUNREACH,
756 [ENETRESET] = TARGET_ENETRESET,
757 [ECONNABORTED] = TARGET_ECONNABORTED,
758 [ECONNRESET] = TARGET_ECONNRESET,
759 [ENOBUFS] = TARGET_ENOBUFS,
760 [EISCONN] = TARGET_EISCONN,
761 [ENOTCONN] = TARGET_ENOTCONN,
762 [EUCLEAN] = TARGET_EUCLEAN,
763 [ENOTNAM] = TARGET_ENOTNAM,
764 [ENAVAIL] = TARGET_ENAVAIL,
765 [EISNAM] = TARGET_EISNAM,
766 [EREMOTEIO] = TARGET_EREMOTEIO,
767 [EDQUOT] = TARGET_EDQUOT,
768 [ESHUTDOWN] = TARGET_ESHUTDOWN,
769 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
770 [ETIMEDOUT] = TARGET_ETIMEDOUT,
771 [ECONNREFUSED] = TARGET_ECONNREFUSED,
772 [EHOSTDOWN] = TARGET_EHOSTDOWN,
773 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
774 [EALREADY] = TARGET_EALREADY,
775 [EINPROGRESS] = TARGET_EINPROGRESS,
776 [ESTALE] = TARGET_ESTALE,
777 [ECANCELED] = TARGET_ECANCELED,
778 [ENOMEDIUM] = TARGET_ENOMEDIUM,
779 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
780 #ifdef ENOKEY
781 [ENOKEY] = TARGET_ENOKEY,
782 #endif
783 #ifdef EKEYEXPIRED
784 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
785 #endif
786 #ifdef EKEYREVOKED
787 [EKEYREVOKED] = TARGET_EKEYREVOKED,
788 #endif
789 #ifdef EKEYREJECTED
790 [EKEYREJECTED] = TARGET_EKEYREJECTED,
791 #endif
792 #ifdef EOWNERDEAD
793 [EOWNERDEAD] = TARGET_EOWNERDEAD,
794 #endif
795 #ifdef ENOTRECOVERABLE
796 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
797 #endif
798 #ifdef ENOMSG
799 [ENOMSG] = TARGET_ENOMSG,
800 #endif
801 };
802
803 static inline int host_to_target_errno(int err)
804 {
805 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
806 host_to_target_errno_table[err]) {
807 return host_to_target_errno_table[err];
808 }
809 return err;
810 }
811
812 static inline int target_to_host_errno(int err)
813 {
814 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
815 target_to_host_errno_table[err]) {
816 return target_to_host_errno_table[err];
817 }
818 return err;
819 }
820
821 static inline abi_long get_errno(abi_long ret)
822 {
823 if (ret == -1)
824 return -host_to_target_errno(errno);
825 else
826 return ret;
827 }
828
829 static inline int is_error(abi_long ret)
830 {
831 return (abi_ulong)ret >= (abi_ulong)(-4096);
832 }
833
834 const char *target_strerror(int err)
835 {
836 if (err == TARGET_ERESTARTSYS) {
837 return "To be restarted";
838 }
839 if (err == TARGET_QEMU_ESIGRETURN) {
840 return "Successful exit from sigreturn";
841 }
842
843 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
844 return NULL;
845 }
846 return strerror(target_to_host_errno(err));
847 }
848
849 #define safe_syscall0(type, name) \
850 static type safe_##name(void) \
851 { \
852 return safe_syscall(__NR_##name); \
853 }
854
855 #define safe_syscall1(type, name, type1, arg1) \
856 static type safe_##name(type1 arg1) \
857 { \
858 return safe_syscall(__NR_##name, arg1); \
859 }
860
861 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
862 static type safe_##name(type1 arg1, type2 arg2) \
863 { \
864 return safe_syscall(__NR_##name, arg1, arg2); \
865 }
866
867 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
868 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
869 { \
870 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
871 }
872
873 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
874 type4, arg4) \
875 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
876 { \
877 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
878 }
879
880 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
881 type4, arg4, type5, arg5) \
882 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
883 type5 arg5) \
884 { \
885 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
886 }
887
888 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
889 type4, arg4, type5, arg5, type6, arg6) \
890 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
891 type5 arg5, type6 arg6) \
892 { \
893 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
894 }
895
896 safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
897 safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
898 safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
899 int, flags, mode_t, mode)
900 safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
901 struct rusage *, rusage)
902 safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
903 int, options, struct rusage *, rusage)
904 safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
905 safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
906 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
907 safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
908 struct timespec *, tsp, const sigset_t *, sigmask,
909 size_t, sigsetsize)
910 safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
911 int, maxevents, int, timeout, const sigset_t *, sigmask,
912 size_t, sigsetsize)
913 safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
914 const struct timespec *,timeout,int *,uaddr2,int,val3)
915 safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
916 safe_syscall2(int, kill, pid_t, pid, int, sig)
917 safe_syscall2(int, tkill, int, tid, int, sig)
918 safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
919 safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
920 safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
921 safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
922 unsigned long, pos_l, unsigned long, pos_h)
923 safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
924 unsigned long, pos_l, unsigned long, pos_h)
925 safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
926 socklen_t, addrlen)
927 safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
928 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
929 safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
930 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
931 safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
932 safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
933 safe_syscall2(int, flock, int, fd, int, operation)
934 safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
935 const struct timespec *, uts, size_t, sigsetsize)
936 safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
937 int, flags)
938 safe_syscall2(int, nanosleep, const struct timespec *, req,
939 struct timespec *, rem)
940 #ifdef TARGET_NR_clock_nanosleep
941 safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
942 const struct timespec *, req, struct timespec *, rem)
943 #endif
944 #ifdef __NR_msgsnd
945 safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
946 int, flags)
947 safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
948 long, msgtype, int, flags)
949 safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
950 unsigned, nsops, const struct timespec *, timeout)
951 #else
952 /* This host kernel architecture uses a single ipc syscall; fake up
953 * wrappers for the sub-operations to hide this implementation detail.
954 * Annoyingly we can't include linux/ipc.h to get the constant definitions
955 * for the call parameter because some structs in there conflict with the
956 * sys/ipc.h ones. So we just define them here, and rely on them being
957 * the same for all host architectures.
958 */
959 #define Q_SEMTIMEDOP 4
960 #define Q_MSGSND 11
961 #define Q_MSGRCV 12
962 #define Q_IPCCALL(VERSION, OP) ((VERSION) << 16 | (OP))
963
964 safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
965 void *, ptr, long, fifth)
966 static int safe_msgsnd(int msgid, const void *msgp, size_t sz, int flags)
967 {
968 return safe_ipc(Q_IPCCALL(0, Q_MSGSND), msgid, sz, flags, (void *)msgp, 0);
969 }
970 static int safe_msgrcv(int msgid, void *msgp, size_t sz, long type, int flags)
971 {
972 return safe_ipc(Q_IPCCALL(1, Q_MSGRCV), msgid, sz, flags, msgp, type);
973 }
974 static int safe_semtimedop(int semid, struct sembuf *tsops, unsigned nsops,
975 const struct timespec *timeout)
976 {
977 return safe_ipc(Q_IPCCALL(0, Q_SEMTIMEDOP), semid, nsops, 0, tsops,
978 (long)timeout);
979 }
980 #endif
981 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
982 safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
983 size_t, len, unsigned, prio, const struct timespec *, timeout)
984 safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
985 size_t, len, unsigned *, prio, const struct timespec *, timeout)
986 #endif
987 /* We do ioctl like this rather than via safe_syscall3 to preserve the
988 * "third argument might be integer or pointer or not present" behaviour of
989 * the libc function.
990 */
991 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
992 /* Similarly for fcntl. Note that callers must always:
993 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
994 * use the flock64 struct rather than unsuffixed flock
995 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
996 */
997 #ifdef __NR_fcntl64
998 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
999 #else
1000 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
1001 #endif
1002
1003 static inline int host_to_target_sock_type(int host_type)
1004 {
1005 int target_type;
1006
1007 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
1008 case SOCK_DGRAM:
1009 target_type = TARGET_SOCK_DGRAM;
1010 break;
1011 case SOCK_STREAM:
1012 target_type = TARGET_SOCK_STREAM;
1013 break;
1014 default:
1015 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
1016 break;
1017 }
1018
1019 #if defined(SOCK_CLOEXEC)
1020 if (host_type & SOCK_CLOEXEC) {
1021 target_type |= TARGET_SOCK_CLOEXEC;
1022 }
1023 #endif
1024
1025 #if defined(SOCK_NONBLOCK)
1026 if (host_type & SOCK_NONBLOCK) {
1027 target_type |= TARGET_SOCK_NONBLOCK;
1028 }
1029 #endif
1030
1031 return target_type;
1032 }
1033
1034 static abi_ulong target_brk;
1035 static abi_ulong target_original_brk;
1036 static abi_ulong brk_page;
1037
1038 void target_set_brk(abi_ulong new_brk)
1039 {
1040 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
1041 brk_page = HOST_PAGE_ALIGN(target_brk);
1042 }
1043
1044 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
1045 #define DEBUGF_BRK(message, args...)
1046
1047 /* do_brk() must return target values and target errnos. */
1048 abi_long do_brk(abi_ulong new_brk)
1049 {
1050 abi_long mapped_addr;
1051 abi_ulong new_alloc_size;
1052
1053 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
1054
1055 if (!new_brk) {
1056 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
1057 return target_brk;
1058 }
1059 if (new_brk < target_original_brk) {
1060 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
1061 target_brk);
1062 return target_brk;
1063 }
1064
1065 /* If the new brk is less than the highest page reserved to the
1066 * target heap allocation, set it and we're almost done... */
1067 if (new_brk <= brk_page) {
1068 /* Heap contents are initialized to zero, as for anonymous
1069 * mapped pages. */
1070 if (new_brk > target_brk) {
1071 memset(g2h(target_brk), 0, new_brk - target_brk);
1072 }
1073 target_brk = new_brk;
1074 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
1075 return target_brk;
1076 }
1077
1078 /* We need to allocate more memory after the brk... Note that
1079 * we don't use MAP_FIXED because that will map over the top of
1080 * any existing mapping (like the one with the host libc or qemu
1081 * itself); instead we treat "mapped but at wrong address" as
1082 * a failure and unmap again.
1083 */
1084 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
1085 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
1086 PROT_READ|PROT_WRITE,
1087 MAP_ANON|MAP_PRIVATE, 0, 0));
1088
1089 if (mapped_addr == brk_page) {
1090 /* Heap contents are initialized to zero, as for anonymous
1091 * mapped pages. Technically the new pages are already
1092 * initialized to zero since they *are* anonymous mapped
1093 * pages, however we have to take care with the contents that
1094 * come from the remaining part of the previous page: it may
1095 * contains garbage data due to a previous heap usage (grown
1096 * then shrunken). */
1097 memset(g2h(target_brk), 0, brk_page - target_brk);
1098
1099 target_brk = new_brk;
1100 brk_page = HOST_PAGE_ALIGN(target_brk);
1101 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
1102 target_brk);
1103 return target_brk;
1104 } else if (mapped_addr != -1) {
1105 /* Mapped but at wrong address, meaning there wasn't actually
1106 * enough space for this brk.
1107 */
1108 target_munmap(mapped_addr, new_alloc_size);
1109 mapped_addr = -1;
1110 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
1111 }
1112 else {
1113 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
1114 }
1115
1116 #if defined(TARGET_ALPHA)
1117 /* We (partially) emulate OSF/1 on Alpha, which requires we
1118 return a proper errno, not an unchanged brk value. */
1119 return -TARGET_ENOMEM;
1120 #endif
1121 /* For everything else, return the previous break. */
1122 return target_brk;
1123 }
1124
1125 static inline abi_long copy_from_user_fdset(fd_set *fds,
1126 abi_ulong target_fds_addr,
1127 int n)
1128 {
1129 int i, nw, j, k;
1130 abi_ulong b, *target_fds;
1131
1132 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
1133 if (!(target_fds = lock_user(VERIFY_READ,
1134 target_fds_addr,
1135 sizeof(abi_ulong) * nw,
1136 1)))
1137 return -TARGET_EFAULT;
1138
1139 FD_ZERO(fds);
1140 k = 0;
1141 for (i = 0; i < nw; i++) {
1142 /* grab the abi_ulong */
1143 __get_user(b, &target_fds[i]);
1144 for (j = 0; j < TARGET_ABI_BITS; j++) {
1145 /* check the bit inside the abi_ulong */
1146 if ((b >> j) & 1)
1147 FD_SET(k, fds);
1148 k++;
1149 }
1150 }
1151
1152 unlock_user(target_fds, target_fds_addr, 0);
1153
1154 return 0;
1155 }
1156
1157 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
1158 abi_ulong target_fds_addr,
1159 int n)
1160 {
1161 if (target_fds_addr) {
1162 if (copy_from_user_fdset(fds, target_fds_addr, n))
1163 return -TARGET_EFAULT;
1164 *fds_ptr = fds;
1165 } else {
1166 *fds_ptr = NULL;
1167 }
1168 return 0;
1169 }
1170
1171 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
1172 const fd_set *fds,
1173 int n)
1174 {
1175 int i, nw, j, k;
1176 abi_long v;
1177 abi_ulong *target_fds;
1178
1179 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
1180 if (!(target_fds = lock_user(VERIFY_WRITE,
1181 target_fds_addr,
1182 sizeof(abi_ulong) * nw,
1183 0)))
1184 return -TARGET_EFAULT;
1185
1186 k = 0;
1187 for (i = 0; i < nw; i++) {
1188 v = 0;
1189 for (j = 0; j < TARGET_ABI_BITS; j++) {
1190 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
1191 k++;
1192 }
1193 __put_user(v, &target_fds[i]);
1194 }
1195
1196 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
1197
1198 return 0;
1199 }
1200
1201 #if defined(__alpha__)
1202 #define HOST_HZ 1024
1203 #else
1204 #define HOST_HZ 100
1205 #endif
1206
1207 static inline abi_long host_to_target_clock_t(long ticks)
1208 {
1209 #if HOST_HZ == TARGET_HZ
1210 return ticks;
1211 #else
1212 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
1213 #endif
1214 }
1215
1216 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
1217 const struct rusage *rusage)
1218 {
1219 struct target_rusage *target_rusage;
1220
1221 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
1222 return -TARGET_EFAULT;
1223 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
1224 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
1225 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
1226 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
1227 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
1228 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
1229 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
1230 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
1231 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
1232 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
1233 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
1234 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
1235 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
1236 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
1237 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
1238 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
1239 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
1240 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
1241 unlock_user_struct(target_rusage, target_addr, 1);
1242
1243 return 0;
1244 }
1245
1246 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
1247 {
1248 abi_ulong target_rlim_swap;
1249 rlim_t result;
1250
1251 target_rlim_swap = tswapal(target_rlim);
1252 if (target_rlim_swap == TARGET_RLIM_INFINITY)
1253 return RLIM_INFINITY;
1254
1255 result = target_rlim_swap;
1256 if (target_rlim_swap != (rlim_t)result)
1257 return RLIM_INFINITY;
1258
1259 return result;
1260 }
1261
1262 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
1263 {
1264 abi_ulong target_rlim_swap;
1265 abi_ulong result;
1266
1267 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
1268 target_rlim_swap = TARGET_RLIM_INFINITY;
1269 else
1270 target_rlim_swap = rlim;
1271 result = tswapal(target_rlim_swap);
1272
1273 return result;
1274 }
1275
1276 static inline int target_to_host_resource(int code)
1277 {
1278 switch (code) {
1279 case TARGET_RLIMIT_AS:
1280 return RLIMIT_AS;
1281 case TARGET_RLIMIT_CORE:
1282 return RLIMIT_CORE;
1283 case TARGET_RLIMIT_CPU:
1284 return RLIMIT_CPU;
1285 case TARGET_RLIMIT_DATA:
1286 return RLIMIT_DATA;
1287 case TARGET_RLIMIT_FSIZE:
1288 return RLIMIT_FSIZE;
1289 case TARGET_RLIMIT_LOCKS:
1290 return RLIMIT_LOCKS;
1291 case TARGET_RLIMIT_MEMLOCK:
1292 return RLIMIT_MEMLOCK;
1293 case TARGET_RLIMIT_MSGQUEUE:
1294 return RLIMIT_MSGQUEUE;
1295 case TARGET_RLIMIT_NICE:
1296 return RLIMIT_NICE;
1297 case TARGET_RLIMIT_NOFILE:
1298 return RLIMIT_NOFILE;
1299 case TARGET_RLIMIT_NPROC:
1300 return RLIMIT_NPROC;
1301 case TARGET_RLIMIT_RSS:
1302 return RLIMIT_RSS;
1303 case TARGET_RLIMIT_RTPRIO:
1304 return RLIMIT_RTPRIO;
1305 case TARGET_RLIMIT_SIGPENDING:
1306 return RLIMIT_SIGPENDING;
1307 case TARGET_RLIMIT_STACK:
1308 return RLIMIT_STACK;
1309 default:
1310 return code;
1311 }
1312 }
1313
1314 static inline abi_long copy_from_user_timeval(struct timeval *tv,
1315 abi_ulong target_tv_addr)
1316 {
1317 struct target_timeval *target_tv;
1318
1319 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
1320 return -TARGET_EFAULT;
1321
1322 __get_user(tv->tv_sec, &target_tv->tv_sec);
1323 __get_user(tv->tv_usec, &target_tv->tv_usec);
1324
1325 unlock_user_struct(target_tv, target_tv_addr, 0);
1326
1327 return 0;
1328 }
1329
1330 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1331 const struct timeval *tv)
1332 {
1333 struct target_timeval *target_tv;
1334
1335 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
1336 return -TARGET_EFAULT;
1337
1338 __put_user(tv->tv_sec, &target_tv->tv_sec);
1339 __put_user(tv->tv_usec, &target_tv->tv_usec);
1340
1341 unlock_user_struct(target_tv, target_tv_addr, 1);
1342
1343 return 0;
1344 }
1345
1346 static inline abi_long copy_from_user_timezone(struct timezone *tz,
1347 abi_ulong target_tz_addr)
1348 {
1349 struct target_timezone *target_tz;
1350
1351 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1352 return -TARGET_EFAULT;
1353 }
1354
1355 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1356 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1357
1358 unlock_user_struct(target_tz, target_tz_addr, 0);
1359
1360 return 0;
1361 }
1362
1363 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1364 #include <mqueue.h>
1365
1366 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1367 abi_ulong target_mq_attr_addr)
1368 {
1369 struct target_mq_attr *target_mq_attr;
1370
1371 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1372 target_mq_attr_addr, 1))
1373 return -TARGET_EFAULT;
1374
1375 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1376 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1377 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1378 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1379
1380 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1381
1382 return 0;
1383 }
1384
1385 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1386 const struct mq_attr *attr)
1387 {
1388 struct target_mq_attr *target_mq_attr;
1389
1390 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1391 target_mq_attr_addr, 0))
1392 return -TARGET_EFAULT;
1393
1394 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1395 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1396 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1397 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1398
1399 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1400
1401 return 0;
1402 }
1403 #endif
1404
1405 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1406 /* do_select() must return target values and target errnos. */
1407 static abi_long do_select(int n,
1408 abi_ulong rfd_addr, abi_ulong wfd_addr,
1409 abi_ulong efd_addr, abi_ulong target_tv_addr)
1410 {
1411 fd_set rfds, wfds, efds;
1412 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1413 struct timeval tv;
1414 struct timespec ts, *ts_ptr;
1415 abi_long ret;
1416
1417 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1418 if (ret) {
1419 return ret;
1420 }
1421 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1422 if (ret) {
1423 return ret;
1424 }
1425 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1426 if (ret) {
1427 return ret;
1428 }
1429
1430 if (target_tv_addr) {
1431 if (copy_from_user_timeval(&tv, target_tv_addr))
1432 return -TARGET_EFAULT;
1433 ts.tv_sec = tv.tv_sec;
1434 ts.tv_nsec = tv.tv_usec * 1000;
1435 ts_ptr = &ts;
1436 } else {
1437 ts_ptr = NULL;
1438 }
1439
1440 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1441 ts_ptr, NULL));
1442
1443 if (!is_error(ret)) {
1444 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1445 return -TARGET_EFAULT;
1446 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1447 return -TARGET_EFAULT;
1448 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1449 return -TARGET_EFAULT;
1450
1451 if (target_tv_addr) {
1452 tv.tv_sec = ts.tv_sec;
1453 tv.tv_usec = ts.tv_nsec / 1000;
1454 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1455 return -TARGET_EFAULT;
1456 }
1457 }
1458 }
1459
1460 return ret;
1461 }
1462
1463 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1464 static abi_long do_old_select(abi_ulong arg1)
1465 {
1466 struct target_sel_arg_struct *sel;
1467 abi_ulong inp, outp, exp, tvp;
1468 long nsel;
1469
1470 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
1471 return -TARGET_EFAULT;
1472 }
1473
1474 nsel = tswapal(sel->n);
1475 inp = tswapal(sel->inp);
1476 outp = tswapal(sel->outp);
1477 exp = tswapal(sel->exp);
1478 tvp = tswapal(sel->tvp);
1479
1480 unlock_user_struct(sel, arg1, 0);
1481
1482 return do_select(nsel, inp, outp, exp, tvp);
1483 }
1484 #endif
1485 #endif
1486
1487 static abi_long do_pipe2(int host_pipe[], int flags)
1488 {
1489 #ifdef CONFIG_PIPE2
1490 return pipe2(host_pipe, flags);
1491 #else
1492 return -ENOSYS;
1493 #endif
1494 }
1495
1496 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1497 int flags, int is_pipe2)
1498 {
1499 int host_pipe[2];
1500 abi_long ret;
1501 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1502
1503 if (is_error(ret))
1504 return get_errno(ret);
1505
1506 /* Several targets have special calling conventions for the original
1507 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1508 if (!is_pipe2) {
1509 #if defined(TARGET_ALPHA)
1510 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1511 return host_pipe[0];
1512 #elif defined(TARGET_MIPS)
1513 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1514 return host_pipe[0];
1515 #elif defined(TARGET_SH4)
1516 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1517 return host_pipe[0];
1518 #elif defined(TARGET_SPARC)
1519 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1520 return host_pipe[0];
1521 #endif
1522 }
1523
1524 if (put_user_s32(host_pipe[0], pipedes)
1525 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1526 return -TARGET_EFAULT;
1527 return get_errno(ret);
1528 }
1529
1530 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1531 abi_ulong target_addr,
1532 socklen_t len)
1533 {
1534 struct target_ip_mreqn *target_smreqn;
1535
1536 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1537 if (!target_smreqn)
1538 return -TARGET_EFAULT;
1539 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1540 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1541 if (len == sizeof(struct target_ip_mreqn))
1542 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
1543 unlock_user(target_smreqn, target_addr, 0);
1544
1545 return 0;
1546 }
1547
1548 static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
1549 abi_ulong target_addr,
1550 socklen_t len)
1551 {
1552 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1553 sa_family_t sa_family;
1554 struct target_sockaddr *target_saddr;
1555
1556 if (fd_trans_target_to_host_addr(fd)) {
1557 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1558 }
1559
1560 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1561 if (!target_saddr)
1562 return -TARGET_EFAULT;
1563
1564 sa_family = tswap16(target_saddr->sa_family);
1565
1566 /* Oops. The caller might send a incomplete sun_path; sun_path
1567 * must be terminated by \0 (see the manual page), but
1568 * unfortunately it is quite common to specify sockaddr_un
1569 * length as "strlen(x->sun_path)" while it should be
1570 * "strlen(...) + 1". We'll fix that here if needed.
1571 * Linux kernel has a similar feature.
1572 */
1573
1574 if (sa_family == AF_UNIX) {
1575 if (len < unix_maxlen && len > 0) {
1576 char *cp = (char*)target_saddr;
1577
1578 if ( cp[len-1] && !cp[len] )
1579 len++;
1580 }
1581 if (len > unix_maxlen)
1582 len = unix_maxlen;
1583 }
1584
1585 memcpy(addr, target_saddr, len);
1586 addr->sa_family = sa_family;
1587 if (sa_family == AF_NETLINK) {
1588 struct sockaddr_nl *nladdr;
1589
1590 nladdr = (struct sockaddr_nl *)addr;
1591 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1592 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1593 } else if (sa_family == AF_PACKET) {
1594 struct target_sockaddr_ll *lladdr;
1595
1596 lladdr = (struct target_sockaddr_ll *)addr;
1597 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1598 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1599 }
1600 unlock_user(target_saddr, target_addr, 0);
1601
1602 return 0;
1603 }
1604
1605 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1606 struct sockaddr *addr,
1607 socklen_t len)
1608 {
1609 struct target_sockaddr *target_saddr;
1610
1611 if (len == 0) {
1612 return 0;
1613 }
1614
1615 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1616 if (!target_saddr)
1617 return -TARGET_EFAULT;
1618 memcpy(target_saddr, addr, len);
1619 if (len >= offsetof(struct target_sockaddr, sa_family) +
1620 sizeof(target_saddr->sa_family)) {
1621 target_saddr->sa_family = tswap16(addr->sa_family);
1622 }
1623 if (addr->sa_family == AF_NETLINK && len >= sizeof(struct sockaddr_nl)) {
1624 struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr;
1625 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1626 target_nl->nl_groups = tswap32(target_nl->nl_groups);
1627 } else if (addr->sa_family == AF_PACKET) {
1628 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
1629 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
1630 target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
1631 }
1632 unlock_user(target_saddr, target_addr, len);
1633
1634 return 0;
1635 }
1636
1637 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1638 struct target_msghdr *target_msgh)
1639 {
1640 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1641 abi_long msg_controllen;
1642 abi_ulong target_cmsg_addr;
1643 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1644 socklen_t space = 0;
1645
1646 msg_controllen = tswapal(target_msgh->msg_controllen);
1647 if (msg_controllen < sizeof (struct target_cmsghdr))
1648 goto the_end;
1649 target_cmsg_addr = tswapal(target_msgh->msg_control);
1650 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1651 target_cmsg_start = target_cmsg;
1652 if (!target_cmsg)
1653 return -TARGET_EFAULT;
1654
1655 while (cmsg && target_cmsg) {
1656 void *data = CMSG_DATA(cmsg);
1657 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1658
1659 int len = tswapal(target_cmsg->cmsg_len)
1660 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1661
1662 space += CMSG_SPACE(len);
1663 if (space > msgh->msg_controllen) {
1664 space -= CMSG_SPACE(len);
1665 /* This is a QEMU bug, since we allocated the payload
1666 * area ourselves (unlike overflow in host-to-target
1667 * conversion, which is just the guest giving us a buffer
1668 * that's too small). It can't happen for the payload types
1669 * we currently support; if it becomes an issue in future
1670 * we would need to improve our allocation strategy to
1671 * something more intelligent than "twice the size of the
1672 * target buffer we're reading from".
1673 */
1674 gemu_log("Host cmsg overflow\n");
1675 break;
1676 }
1677
1678 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1679 cmsg->cmsg_level = SOL_SOCKET;
1680 } else {
1681 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1682 }
1683 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1684 cmsg->cmsg_len = CMSG_LEN(len);
1685
1686 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
1687 int *fd = (int *)data;
1688 int *target_fd = (int *)target_data;
1689 int i, numfds = len / sizeof(int);
1690
1691 for (i = 0; i < numfds; i++) {
1692 __get_user(fd[i], target_fd + i);
1693 }
1694 } else if (cmsg->cmsg_level == SOL_SOCKET
1695 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1696 struct ucred *cred = (struct ucred *)data;
1697 struct target_ucred *target_cred =
1698 (struct target_ucred *)target_data;
1699
1700 __get_user(cred->pid, &target_cred->pid);
1701 __get_user(cred->uid, &target_cred->uid);
1702 __get_user(cred->gid, &target_cred->gid);
1703 } else {
1704 gemu_log("Unsupported ancillary data: %d/%d\n",
1705 cmsg->cmsg_level, cmsg->cmsg_type);
1706 memcpy(data, target_data, len);
1707 }
1708
1709 cmsg = CMSG_NXTHDR(msgh, cmsg);
1710 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1711 target_cmsg_start);
1712 }
1713 unlock_user(target_cmsg, target_cmsg_addr, 0);
1714 the_end:
1715 msgh->msg_controllen = space;
1716 return 0;
1717 }
1718
1719 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1720 struct msghdr *msgh)
1721 {
1722 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1723 abi_long msg_controllen;
1724 abi_ulong target_cmsg_addr;
1725 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1726 socklen_t space = 0;
1727
1728 msg_controllen = tswapal(target_msgh->msg_controllen);
1729 if (msg_controllen < sizeof (struct target_cmsghdr))
1730 goto the_end;
1731 target_cmsg_addr = tswapal(target_msgh->msg_control);
1732 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1733 target_cmsg_start = target_cmsg;
1734 if (!target_cmsg)
1735 return -TARGET_EFAULT;
1736
1737 while (cmsg && target_cmsg) {
1738 void *data = CMSG_DATA(cmsg);
1739 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1740
1741 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1742 int tgt_len, tgt_space;
1743
1744 /* We never copy a half-header but may copy half-data;
1745 * this is Linux's behaviour in put_cmsg(). Note that
1746 * truncation here is a guest problem (which we report
1747 * to the guest via the CTRUNC bit), unlike truncation
1748 * in target_to_host_cmsg, which is a QEMU bug.
1749 */
1750 if (msg_controllen < sizeof(struct cmsghdr)) {
1751 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1752 break;
1753 }
1754
1755 if (cmsg->cmsg_level == SOL_SOCKET) {
1756 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1757 } else {
1758 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1759 }
1760 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1761
1762 tgt_len = TARGET_CMSG_LEN(len);
1763
1764 /* Payload types which need a different size of payload on
1765 * the target must adjust tgt_len here.
1766 */
1767 switch (cmsg->cmsg_level) {
1768 case SOL_SOCKET:
1769 switch (cmsg->cmsg_type) {
1770 case SO_TIMESTAMP:
1771 tgt_len = sizeof(struct target_timeval);
1772 break;
1773 default:
1774 break;
1775 }
1776 default:
1777 break;
1778 }
1779
1780 if (msg_controllen < tgt_len) {
1781 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1782 tgt_len = msg_controllen;
1783 }
1784
1785 /* We must now copy-and-convert len bytes of payload
1786 * into tgt_len bytes of destination space. Bear in mind
1787 * that in both source and destination we may be dealing
1788 * with a truncated value!
1789 */
1790 switch (cmsg->cmsg_level) {
1791 case SOL_SOCKET:
1792 switch (cmsg->cmsg_type) {
1793 case SCM_RIGHTS:
1794 {
1795 int *fd = (int *)data;
1796 int *target_fd = (int *)target_data;
1797 int i, numfds = tgt_len / sizeof(int);
1798
1799 for (i = 0; i < numfds; i++) {
1800 __put_user(fd[i], target_fd + i);
1801 }
1802 break;
1803 }
1804 case SO_TIMESTAMP:
1805 {
1806 struct timeval *tv = (struct timeval *)data;
1807 struct target_timeval *target_tv =
1808 (struct target_timeval *)target_data;
1809
1810 if (len != sizeof(struct timeval) ||
1811 tgt_len != sizeof(struct target_timeval)) {
1812 goto unimplemented;
1813 }
1814
1815 /* copy struct timeval to target */
1816 __put_user(tv->tv_sec, &target_tv->tv_sec);
1817 __put_user(tv->tv_usec, &target_tv->tv_usec);
1818 break;
1819 }
1820 case SCM_CREDENTIALS:
1821 {
1822 struct ucred *cred = (struct ucred *)data;
1823 struct target_ucred *target_cred =
1824 (struct target_ucred *)target_data;
1825
1826 __put_user(cred->pid, &target_cred->pid);
1827 __put_user(cred->uid, &target_cred->uid);
1828 __put_user(cred->gid, &target_cred->gid);
1829 break;
1830 }
1831 default:
1832 goto unimplemented;
1833 }
1834 break;
1835
1836 default:
1837 unimplemented:
1838 gemu_log("Unsupported ancillary data: %d/%d\n",
1839 cmsg->cmsg_level, cmsg->cmsg_type);
1840 memcpy(target_data, data, MIN(len, tgt_len));
1841 if (tgt_len > len) {
1842 memset(target_data + len, 0, tgt_len - len);
1843 }
1844 }
1845
1846 target_cmsg->cmsg_len = tswapal(tgt_len);
1847 tgt_space = TARGET_CMSG_SPACE(len);
1848 if (msg_controllen < tgt_space) {
1849 tgt_space = msg_controllen;
1850 }
1851 msg_controllen -= tgt_space;
1852 space += tgt_space;
1853 cmsg = CMSG_NXTHDR(msgh, cmsg);
1854 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1855 target_cmsg_start);
1856 }
1857 unlock_user(target_cmsg, target_cmsg_addr, space);
1858 the_end:
1859 target_msgh->msg_controllen = tswapal(space);
1860 return 0;
1861 }
1862
1863 static void tswap_nlmsghdr(struct nlmsghdr *nlh)
1864 {
1865 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
1866 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
1867 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
1868 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
1869 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
1870 }
1871
1872 static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
1873 size_t len,
1874 abi_long (*host_to_target_nlmsg)
1875 (struct nlmsghdr *))
1876 {
1877 uint32_t nlmsg_len;
1878 abi_long ret;
1879
1880 while (len > sizeof(struct nlmsghdr)) {
1881
1882 nlmsg_len = nlh->nlmsg_len;
1883 if (nlmsg_len < sizeof(struct nlmsghdr) ||
1884 nlmsg_len > len) {
1885 break;
1886 }
1887
1888 switch (nlh->nlmsg_type) {
1889 case NLMSG_DONE:
1890 tswap_nlmsghdr(nlh);
1891 return 0;
1892 case NLMSG_NOOP:
1893 break;
1894 case NLMSG_ERROR:
1895 {
1896 struct nlmsgerr *e = NLMSG_DATA(nlh);
1897 e->error = tswap32(e->error);
1898 tswap_nlmsghdr(&e->msg);
1899 tswap_nlmsghdr(nlh);
1900 return 0;
1901 }
1902 default:
1903 ret = host_to_target_nlmsg(nlh);
1904 if (ret < 0) {
1905 tswap_nlmsghdr(nlh);
1906 return ret;
1907 }
1908 break;
1909 }
1910 tswap_nlmsghdr(nlh);
1911 len -= NLMSG_ALIGN(nlmsg_len);
1912 nlh = (struct nlmsghdr *)(((char*)nlh) + NLMSG_ALIGN(nlmsg_len));
1913 }
1914 return 0;
1915 }
1916
1917 static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
1918 size_t len,
1919 abi_long (*target_to_host_nlmsg)
1920 (struct nlmsghdr *))
1921 {
1922 int ret;
1923
1924 while (len > sizeof(struct nlmsghdr)) {
1925 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
1926 tswap32(nlh->nlmsg_len) > len) {
1927 break;
1928 }
1929 tswap_nlmsghdr(nlh);
1930 switch (nlh->nlmsg_type) {
1931 case NLMSG_DONE:
1932 return 0;
1933 case NLMSG_NOOP:
1934 break;
1935 case NLMSG_ERROR:
1936 {
1937 struct nlmsgerr *e = NLMSG_DATA(nlh);
1938 e->error = tswap32(e->error);
1939 tswap_nlmsghdr(&e->msg);
1940 return 0;
1941 }
1942 default:
1943 ret = target_to_host_nlmsg(nlh);
1944 if (ret < 0) {
1945 return ret;
1946 }
1947 }
1948 len -= NLMSG_ALIGN(nlh->nlmsg_len);
1949 nlh = (struct nlmsghdr *)(((char *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1950 }
1951 return 0;
1952 }
1953
1954 #ifdef CONFIG_RTNETLINK
1955 static abi_long host_to_target_for_each_nlattr(struct nlattr *nlattr,
1956 size_t len, void *context,
1957 abi_long (*host_to_target_nlattr)
1958 (struct nlattr *,
1959 void *context))
1960 {
1961 unsigned short nla_len;
1962 abi_long ret;
1963
1964 while (len > sizeof(struct nlattr)) {
1965 nla_len = nlattr->nla_len;
1966 if (nla_len < sizeof(struct nlattr) ||
1967 nla_len > len) {
1968 break;
1969 }
1970 ret = host_to_target_nlattr(nlattr, context);
1971 nlattr->nla_len = tswap16(nlattr->nla_len);
1972 nlattr->nla_type = tswap16(nlattr->nla_type);
1973 if (ret < 0) {
1974 return ret;
1975 }
1976 len -= NLA_ALIGN(nla_len);
1977 nlattr = (struct nlattr *)(((char *)nlattr) + NLA_ALIGN(nla_len));
1978 }
1979 return 0;
1980 }
1981
1982 static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
1983 size_t len,
1984 abi_long (*host_to_target_rtattr)
1985 (struct rtattr *))
1986 {
1987 unsigned short rta_len;
1988 abi_long ret;
1989
1990 while (len > sizeof(struct rtattr)) {
1991 rta_len = rtattr->rta_len;
1992 if (rta_len < sizeof(struct rtattr) ||
1993 rta_len > len) {
1994 break;
1995 }
1996 ret = host_to_target_rtattr(rtattr);
1997 rtattr->rta_len = tswap16(rtattr->rta_len);
1998 rtattr->rta_type = tswap16(rtattr->rta_type);
1999 if (ret < 0) {
2000 return ret;
2001 }
2002 len -= RTA_ALIGN(rta_len);
2003 rtattr = (struct rtattr *)(((char *)rtattr) + RTA_ALIGN(rta_len));
2004 }
2005 return 0;
2006 }
2007
2008 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
2009
2010 static abi_long host_to_target_data_bridge_nlattr(struct nlattr *nlattr,
2011 void *context)
2012 {
2013 uint16_t *u16;
2014 uint32_t *u32;
2015 uint64_t *u64;
2016
2017 switch (nlattr->nla_type) {
2018 /* no data */
2019 case QEMU_IFLA_BR_FDB_FLUSH:
2020 break;
2021 /* binary */
2022 case QEMU_IFLA_BR_GROUP_ADDR:
2023 break;
2024 /* uint8_t */
2025 case QEMU_IFLA_BR_VLAN_FILTERING:
2026 case QEMU_IFLA_BR_TOPOLOGY_CHANGE:
2027 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED:
2028 case QEMU_IFLA_BR_MCAST_ROUTER:
2029 case QEMU_IFLA_BR_MCAST_SNOOPING:
2030 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR:
2031 case QEMU_IFLA_BR_MCAST_QUERIER:
2032 case QEMU_IFLA_BR_NF_CALL_IPTABLES:
2033 case QEMU_IFLA_BR_NF_CALL_IP6TABLES:
2034 case QEMU_IFLA_BR_NF_CALL_ARPTABLES:
2035 break;
2036 /* uint16_t */
2037 case QEMU_IFLA_BR_PRIORITY:
2038 case QEMU_IFLA_BR_VLAN_PROTOCOL:
2039 case QEMU_IFLA_BR_GROUP_FWD_MASK:
2040 case QEMU_IFLA_BR_ROOT_PORT:
2041 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID:
2042 u16 = NLA_DATA(nlattr);
2043 *u16 = tswap16(*u16);
2044 break;
2045 /* uint32_t */
2046 case QEMU_IFLA_BR_FORWARD_DELAY:
2047 case QEMU_IFLA_BR_HELLO_TIME:
2048 case QEMU_IFLA_BR_MAX_AGE:
2049 case QEMU_IFLA_BR_AGEING_TIME:
2050 case QEMU_IFLA_BR_STP_STATE:
2051 case QEMU_IFLA_BR_ROOT_PATH_COST:
2052 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY:
2053 case QEMU_IFLA_BR_MCAST_HASH_MAX:
2054 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT:
2055 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT:
2056 u32 = NLA_DATA(nlattr);
2057 *u32 = tswap32(*u32);
2058 break;
2059 /* uint64_t */
2060 case QEMU_IFLA_BR_HELLO_TIMER:
2061 case QEMU_IFLA_BR_TCN_TIMER:
2062 case QEMU_IFLA_BR_GC_TIMER:
2063 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER:
2064 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL:
2065 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL:
2066 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL:
2067 case QEMU_IFLA_BR_MCAST_QUERY_INTVL:
2068 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL:
2069 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL:
2070 u64 = NLA_DATA(nlattr);
2071 *u64 = tswap64(*u64);
2072 break;
2073 /* ifla_bridge_id: uin8_t[] */
2074 case QEMU_IFLA_BR_ROOT_ID:
2075 case QEMU_IFLA_BR_BRIDGE_ID:
2076 break;
2077 default:
2078 gemu_log("Unknown QEMU_IFLA_BR type %d\n", nlattr->nla_type);
2079 break;
2080 }
2081 return 0;
2082 }
2083
2084 static abi_long host_to_target_slave_data_bridge_nlattr(struct nlattr *nlattr,
2085 void *context)
2086 {
2087 uint16_t *u16;
2088 uint32_t *u32;
2089 uint64_t *u64;
2090
2091 switch (nlattr->nla_type) {
2092 /* uint8_t */
2093 case QEMU_IFLA_BRPORT_STATE:
2094 case QEMU_IFLA_BRPORT_MODE:
2095 case QEMU_IFLA_BRPORT_GUARD:
2096 case QEMU_IFLA_BRPORT_PROTECT:
2097 case QEMU_IFLA_BRPORT_FAST_LEAVE:
2098 case QEMU_IFLA_BRPORT_LEARNING:
2099 case QEMU_IFLA_BRPORT_UNICAST_FLOOD:
2100 case QEMU_IFLA_BRPORT_PROXYARP:
2101 case QEMU_IFLA_BRPORT_LEARNING_SYNC:
2102 case QEMU_IFLA_BRPORT_PROXYARP_WIFI:
2103 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK:
2104 case QEMU_IFLA_BRPORT_CONFIG_PENDING:
2105 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER:
2106 break;
2107 /* uint16_t */
2108 case QEMU_IFLA_BRPORT_PRIORITY:
2109 case QEMU_IFLA_BRPORT_DESIGNATED_PORT:
2110 case QEMU_IFLA_BRPORT_DESIGNATED_COST:
2111 case QEMU_IFLA_BRPORT_ID:
2112 case QEMU_IFLA_BRPORT_NO:
2113 u16 = NLA_DATA(nlattr);
2114 *u16 = tswap16(*u16);
2115 break;
2116 /* uin32_t */
2117 case QEMU_IFLA_BRPORT_COST:
2118 u32 = NLA_DATA(nlattr);
2119 *u32 = tswap32(*u32);
2120 break;
2121 /* uint64_t */
2122 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER:
2123 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER:
2124 case QEMU_IFLA_BRPORT_HOLD_TIMER:
2125 u64 = NLA_DATA(nlattr);
2126 *u64 = tswap64(*u64);
2127 break;
2128 /* ifla_bridge_id: uint8_t[] */
2129 case QEMU_IFLA_BRPORT_ROOT_ID:
2130 case QEMU_IFLA_BRPORT_BRIDGE_ID:
2131 break;
2132 default:
2133 gemu_log("Unknown QEMU_IFLA_BRPORT type %d\n", nlattr->nla_type);
2134 break;
2135 }
2136 return 0;
2137 }
2138
2139 struct linkinfo_context {
2140 int len;
2141 char *name;
2142 int slave_len;
2143 char *slave_name;
2144 };
2145
2146 static abi_long host_to_target_data_linkinfo_nlattr(struct nlattr *nlattr,
2147 void *context)
2148 {
2149 struct linkinfo_context *li_context = context;
2150
2151 switch (nlattr->nla_type) {
2152 /* string */
2153 case QEMU_IFLA_INFO_KIND:
2154 li_context->name = NLA_DATA(nlattr);
2155 li_context->len = nlattr->nla_len - NLA_HDRLEN;
2156 break;
2157 case QEMU_IFLA_INFO_SLAVE_KIND:
2158 li_context->slave_name = NLA_DATA(nlattr);
2159 li_context->slave_len = nlattr->nla_len - NLA_HDRLEN;
2160 break;
2161 /* stats */
2162 case QEMU_IFLA_INFO_XSTATS:
2163 /* FIXME: only used by CAN */
2164 break;
2165 /* nested */
2166 case QEMU_IFLA_INFO_DATA:
2167 if (strncmp(li_context->name, "bridge",
2168 li_context->len) == 0) {
2169 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
2170 nlattr->nla_len,
2171 NULL,
2172 host_to_target_data_bridge_nlattr);
2173 } else {
2174 gemu_log("Unknown QEMU_IFLA_INFO_KIND %s\n", li_context->name);
2175 }
2176 break;
2177 case QEMU_IFLA_INFO_SLAVE_DATA:
2178 if (strncmp(li_context->slave_name, "bridge",
2179 li_context->slave_len) == 0) {
2180 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
2181 nlattr->nla_len,
2182 NULL,
2183 host_to_target_slave_data_bridge_nlattr);
2184 } else {
2185 gemu_log("Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
2186 li_context->slave_name);
2187 }
2188 break;
2189 default:
2190 gemu_log("Unknown host QEMU_IFLA_INFO type: %d\n", nlattr->nla_type);
2191 break;
2192 }
2193
2194 return 0;
2195 }
2196
2197 static abi_long host_to_target_data_inet_nlattr(struct nlattr *nlattr,
2198 void *context)
2199 {
2200 uint32_t *u32;
2201 int i;
2202
2203 switch (nlattr->nla_type) {
2204 case QEMU_IFLA_INET_CONF:
2205 u32 = NLA_DATA(nlattr);
2206 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
2207 i++) {
2208 u32[i] = tswap32(u32[i]);
2209 }
2210 break;
2211 default:
2212 gemu_log("Unknown host AF_INET type: %d\n", nlattr->nla_type);
2213 }
2214 return 0;
2215 }
2216
2217 static abi_long host_to_target_data_inet6_nlattr(struct nlattr *nlattr,
2218 void *context)
2219 {
2220 uint32_t *u32;
2221 uint64_t *u64;
2222 struct ifla_cacheinfo *ci;
2223 int i;
2224
2225 switch (nlattr->nla_type) {
2226 /* binaries */
2227 case QEMU_IFLA_INET6_TOKEN:
2228 break;
2229 /* uint8_t */
2230 case QEMU_IFLA_INET6_ADDR_GEN_MODE:
2231 break;
2232 /* uint32_t */
2233 case QEMU_IFLA_INET6_FLAGS:
2234 u32 = NLA_DATA(nlattr);
2235 *u32 = tswap32(*u32);
2236 break;
2237 /* uint32_t[] */
2238 case QEMU_IFLA_INET6_CONF:
2239 u32 = NLA_DATA(nlattr);
2240 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
2241 i++) {
2242 u32[i] = tswap32(u32[i]);
2243 }
2244 break;
2245 /* ifla_cacheinfo */
2246 case QEMU_IFLA_INET6_CACHEINFO:
2247 ci = NLA_DATA(nlattr);
2248 ci->max_reasm_len = tswap32(ci->max_reasm_len);
2249 ci->tstamp = tswap32(ci->tstamp);
2250 ci->reachable_time = tswap32(ci->reachable_time);
2251 ci->retrans_time = tswap32(ci->retrans_time);
2252 break;
2253 /* uint64_t[] */
2254 case QEMU_IFLA_INET6_STATS:
2255 case QEMU_IFLA_INET6_ICMP6STATS:
2256 u64 = NLA_DATA(nlattr);
2257 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u64);
2258 i++) {
2259 u64[i] = tswap64(u64[i]);
2260 }
2261 break;
2262 default:
2263 gemu_log("Unknown host AF_INET6 type: %d\n", nlattr->nla_type);
2264 }
2265 return 0;
2266 }
2267
2268 static abi_long host_to_target_data_spec_nlattr(struct nlattr *nlattr,
2269 void *context)
2270 {
2271 switch (nlattr->nla_type) {
2272 case AF_INET:
2273 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
2274 NULL,
2275 host_to_target_data_inet_nlattr);
2276 case AF_INET6:
2277 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
2278 NULL,
2279 host_to_target_data_inet6_nlattr);
2280 default:
2281 gemu_log("Unknown host AF_SPEC type: %d\n", nlattr->nla_type);
2282 break;
2283 }
2284 return 0;
2285 }
2286
2287 static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
2288 {
2289 uint32_t *u32;
2290 struct rtnl_link_stats *st;
2291 struct rtnl_link_stats64 *st64;
2292 struct rtnl_link_ifmap *map;
2293 struct linkinfo_context li_context;
2294
2295 switch (rtattr->rta_type) {
2296 /* binary stream */
2297 case QEMU_IFLA_ADDRESS:
2298 case QEMU_IFLA_BROADCAST:
2299 /* string */
2300 case QEMU_IFLA_IFNAME:
2301 case QEMU_IFLA_QDISC:
2302 break;
2303 /* uin8_t */
2304 case QEMU_IFLA_OPERSTATE:
2305 case QEMU_IFLA_LINKMODE:
2306 case QEMU_IFLA_CARRIER:
2307 case QEMU_IFLA_PROTO_DOWN:
2308 break;
2309 /* uint32_t */
2310 case QEMU_IFLA_MTU:
2311 case QEMU_IFLA_LINK:
2312 case QEMU_IFLA_WEIGHT:
2313 case QEMU_IFLA_TXQLEN:
2314 case QEMU_IFLA_CARRIER_CHANGES:
2315 case QEMU_IFLA_NUM_RX_QUEUES:
2316 case QEMU_IFLA_NUM_TX_QUEUES:
2317 case QEMU_IFLA_PROMISCUITY:
2318 case QEMU_IFLA_EXT_MASK:
2319 case QEMU_IFLA_LINK_NETNSID:
2320 case QEMU_IFLA_GROUP:
2321 case QEMU_IFLA_MASTER:
2322 case QEMU_IFLA_NUM_VF:
2323 u32 = RTA_DATA(rtattr);
2324 *u32 = tswap32(*u32);
2325 break;
2326 /* struct rtnl_link_stats */
2327 case QEMU_IFLA_STATS:
2328 st = RTA_DATA(rtattr);
2329 st->rx_packets = tswap32(st->rx_packets);
2330 st->tx_packets = tswap32(st->tx_packets);
2331 st->rx_bytes = tswap32(st->rx_bytes);
2332 st->tx_bytes = tswap32(st->tx_bytes);
2333 st->rx_errors = tswap32(st->rx_errors);
2334 st->tx_errors = tswap32(st->tx_errors);
2335 st->rx_dropped = tswap32(st->rx_dropped);
2336 st->tx_dropped = tswap32(st->tx_dropped);
2337 st->multicast = tswap32(st->multicast);
2338 st->collisions = tswap32(st->collisions);
2339
2340 /* detailed rx_errors: */
2341 st->rx_length_errors = tswap32(st->rx_length_errors);
2342 st->rx_over_errors = tswap32(st->rx_over_errors);
2343 st->rx_crc_errors = tswap32(st->rx_crc_errors);
2344 st->rx_frame_errors = tswap32(st->rx_frame_errors);
2345 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
2346 st->rx_missed_errors = tswap32(st->rx_missed_errors);
2347
2348 /* detailed tx_errors */
2349 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
2350 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
2351 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
2352 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
2353 st->tx_window_errors = tswap32(st->tx_window_errors);
2354
2355 /* for cslip etc */
2356 st->rx_compressed = tswap32(st->rx_compressed);
2357 st->tx_compressed = tswap32(st->tx_compressed);
2358 break;
2359 /* struct rtnl_link_stats64 */
2360 case QEMU_IFLA_STATS64:
2361 st64 = RTA_DATA(rtattr);
2362 st64->rx_packets = tswap64(st64->rx_packets);
2363 st64->tx_packets = tswap64(st64->tx_packets);
2364 st64->rx_bytes = tswap64(st64->rx_bytes);
2365 st64->tx_bytes = tswap64(st64->tx_bytes);
2366 st64->rx_errors = tswap64(st64->rx_errors);
2367 st64->tx_errors = tswap64(st64->tx_errors);
2368 st64->rx_dropped = tswap64(st64->rx_dropped);
2369 st64->tx_dropped = tswap64(st64->tx_dropped);
2370 st64->multicast = tswap64(st64->multicast);
2371 st64->collisions = tswap64(st64->collisions);
2372
2373 /* detailed rx_errors: */
2374 st64->rx_length_errors = tswap64(st64->rx_length_errors);
2375 st64->rx_over_errors = tswap64(st64->rx_over_errors);
2376 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
2377 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
2378 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
2379 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
2380
2381 /* detailed tx_errors */
2382 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
2383 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
2384 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
2385 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
2386 st64->tx_window_errors = tswap64(st64->tx_window_errors);
2387
2388 /* for cslip etc */
2389 st64->rx_compressed = tswap64(st64->rx_compressed);
2390 st64->tx_compressed = tswap64(st64->tx_compressed);
2391 break;
2392 /* struct rtnl_link_ifmap */
2393 case QEMU_IFLA_MAP:
2394 map = RTA_DATA(rtattr);
2395 map->mem_start = tswap64(map->mem_start);
2396 map->mem_end = tswap64(map->mem_end);
2397 map->base_addr = tswap64(map->base_addr);
2398 map->irq = tswap16(map->irq);
2399 break;
2400 /* nested */
2401 case QEMU_IFLA_LINKINFO:
2402 memset(&li_context, 0, sizeof(li_context));
2403 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
2404 &li_context,
2405 host_to_target_data_linkinfo_nlattr);
2406 case QEMU_IFLA_AF_SPEC:
2407 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
2408 NULL,
2409 host_to_target_data_spec_nlattr);
2410 default:
2411 gemu_log("Unknown host QEMU_IFLA type: %d\n", rtattr->rta_type);
2412 break;
2413 }
2414 return 0;
2415 }
2416
2417 static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
2418 {
2419 uint32_t *u32;
2420 struct ifa_cacheinfo *ci;
2421
2422 switch (rtattr->rta_type) {
2423 /* binary: depends on family type */
2424 case IFA_ADDRESS:
2425 case IFA_LOCAL:
2426 break;
2427 /* string */
2428 case IFA_LABEL:
2429 break;
2430 /* u32 */
2431 case IFA_FLAGS:
2432 case IFA_BROADCAST:
2433 u32 = RTA_DATA(rtattr);
2434 *u32 = tswap32(*u32);
2435 break;
2436 /* struct ifa_cacheinfo */
2437 case IFA_CACHEINFO:
2438 ci = RTA_DATA(rtattr);
2439 ci->ifa_prefered = tswap32(ci->ifa_prefered);
2440 ci->ifa_valid = tswap32(ci->ifa_valid);
2441 ci->cstamp = tswap32(ci->cstamp);
2442 ci->tstamp = tswap32(ci->tstamp);
2443 break;
2444 default:
2445 gemu_log("Unknown host IFA type: %d\n", rtattr->rta_type);
2446 break;
2447 }
2448 return 0;
2449 }
2450
2451 static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
2452 {
2453 uint32_t *u32;
2454 switch (rtattr->rta_type) {
2455 /* binary: depends on family type */
2456 case RTA_GATEWAY:
2457 case RTA_DST:
2458 case RTA_PREFSRC:
2459 break;
2460 /* u32 */
2461 case RTA_PRIORITY:
2462 case RTA_TABLE:
2463 case RTA_OIF:
2464 u32 = RTA_DATA(rtattr);
2465 *u32 = tswap32(*u32);
2466 break;
2467 default:
2468 gemu_log("Unknown host RTA type: %d\n", rtattr->rta_type);
2469 break;
2470 }
2471 return 0;
2472 }
2473
2474 static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
2475 uint32_t rtattr_len)
2476 {
2477 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
2478 host_to_target_data_link_rtattr);
2479 }
2480
2481 static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
2482 uint32_t rtattr_len)
2483 {
2484 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
2485 host_to_target_data_addr_rtattr);
2486 }
2487
2488 static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
2489 uint32_t rtattr_len)
2490 {
2491 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
2492 host_to_target_data_route_rtattr);
2493 }
2494
2495 static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
2496 {
2497 uint32_t nlmsg_len;
2498 struct ifinfomsg *ifi;
2499 struct ifaddrmsg *ifa;
2500 struct rtmsg *rtm;
2501
2502 nlmsg_len = nlh->nlmsg_len;
2503 switch (nlh->nlmsg_type) {
2504 case RTM_NEWLINK:
2505 case RTM_DELLINK:
2506 case RTM_GETLINK:
2507 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
2508 ifi = NLMSG_DATA(nlh);
2509 ifi->ifi_type = tswap16(ifi->ifi_type);
2510 ifi->ifi_index = tswap32(ifi->ifi_index);
2511 ifi->ifi_flags = tswap32(ifi->ifi_flags);
2512 ifi->ifi_change = tswap32(ifi->ifi_change);
2513 host_to_target_link_rtattr(IFLA_RTA(ifi),
2514 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
2515 }
2516 break;
2517 case RTM_NEWADDR:
2518 case RTM_DELADDR:
2519 case RTM_GETADDR:
2520 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
2521 ifa = NLMSG_DATA(nlh);
2522 ifa->ifa_index = tswap32(ifa->ifa_index);
2523 host_to_target_addr_rtattr(IFA_RTA(ifa),
2524 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
2525 }
2526 break;
2527 case RTM_NEWROUTE:
2528 case RTM_DELROUTE:
2529 case RTM_GETROUTE:
2530 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
2531 rtm = NLMSG_DATA(nlh);
2532 rtm->rtm_flags = tswap32(rtm->rtm_flags);
2533 host_to_target_route_rtattr(RTM_RTA(rtm),
2534 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
2535 }
2536 break;
2537 default:
2538 return -TARGET_EINVAL;
2539 }
2540 return 0;
2541 }
2542
2543 static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
2544 size_t len)
2545 {
2546 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
2547 }
2548
2549 static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
2550 size_t len,
2551 abi_long (*target_to_host_rtattr)
2552 (struct rtattr *))
2553 {
2554 abi_long ret;
2555
2556 while (len >= sizeof(struct rtattr)) {
2557 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
2558 tswap16(rtattr->rta_len) > len) {
2559 break;
2560 }
2561 rtattr->rta_len = tswap16(rtattr->rta_len);
2562 rtattr->rta_type = tswap16(rtattr->rta_type);
2563 ret = target_to_host_rtattr(rtattr);
2564 if (ret < 0) {
2565 return ret;
2566 }
2567 len -= RTA_ALIGN(rtattr->rta_len);
2568 rtattr = (struct rtattr *)(((char *)rtattr) +
2569 RTA_ALIGN(rtattr->rta_len));
2570 }
2571 return 0;
2572 }
2573
2574 static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
2575 {
2576 switch (rtattr->rta_type) {
2577 default:
2578 gemu_log("Unknown target QEMU_IFLA type: %d\n", rtattr->rta_type);
2579 break;
2580 }
2581 return 0;
2582 }
2583
2584 static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
2585 {
2586 switch (rtattr->rta_type) {
2587 /* binary: depends on family type */
2588 case IFA_LOCAL:
2589 case IFA_ADDRESS:
2590 break;
2591 default:
2592 gemu_log("Unknown target IFA type: %d\n", rtattr->rta_type);
2593 break;
2594 }
2595 return 0;
2596 }
2597
2598 static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
2599 {
2600 uint32_t *u32;
2601 switch (rtattr->rta_type) {
2602 /* binary: depends on family type */
2603 case RTA_DST:
2604 case RTA_SRC:
2605 case RTA_GATEWAY:
2606 break;
2607 /* u32 */
2608 case RTA_PRIORITY:
2609 case RTA_OIF:
2610 u32 = RTA_DATA(rtattr);
2611 *u32 = tswap32(*u32);
2612 break;
2613 default:
2614 gemu_log("Unknown target RTA type: %d\n", rtattr->rta_type);
2615 break;
2616 }
2617 return 0;
2618 }
2619
2620 static void target_to_host_link_rtattr(struct rtattr *rtattr,
2621 uint32_t rtattr_len)
2622 {
2623 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2624 target_to_host_data_link_rtattr);
2625 }
2626
2627 static void target_to_host_addr_rtattr(struct rtattr *rtattr,
2628 uint32_t rtattr_len)
2629 {
2630 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2631 target_to_host_data_addr_rtattr);
2632 }
2633
2634 static void target_to_host_route_rtattr(struct rtattr *rtattr,
2635 uint32_t rtattr_len)
2636 {
2637 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2638 target_to_host_data_route_rtattr);
2639 }
2640
2641 static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
2642 {
2643 struct ifinfomsg *ifi;
2644 struct ifaddrmsg *ifa;
2645 struct rtmsg *rtm;
2646
2647 switch (nlh->nlmsg_type) {
2648 case RTM_GETLINK:
2649 break;
2650 case RTM_NEWLINK:
2651 case RTM_DELLINK:
2652 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
2653 ifi = NLMSG_DATA(nlh);
2654 ifi->ifi_type = tswap16(ifi->ifi_type);
2655 ifi->ifi_index = tswap32(ifi->ifi_index);
2656 ifi->ifi_flags = tswap32(ifi->ifi_flags);
2657 ifi->ifi_change = tswap32(ifi->ifi_change);
2658 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
2659 NLMSG_LENGTH(sizeof(*ifi)));
2660 }
2661 break;
2662 case RTM_GETADDR:
2663 case RTM_NEWADDR:
2664 case RTM_DELADDR:
2665 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
2666 ifa = NLMSG_DATA(nlh);
2667 ifa->ifa_index = tswap32(ifa->ifa_index);
2668 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
2669 NLMSG_LENGTH(sizeof(*ifa)));
2670 }
2671 break;
2672 case RTM_GETROUTE:
2673 break;
2674 case RTM_NEWROUTE:
2675 case RTM_DELROUTE:
2676 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
2677 rtm = NLMSG_DATA(nlh);
2678 rtm->rtm_flags = tswap32(rtm->rtm_flags);
2679 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
2680 NLMSG_LENGTH(sizeof(*rtm)));
2681 }
2682 break;
2683 default:
2684 return -TARGET_EOPNOTSUPP;
2685 }
2686 return 0;
2687 }
2688
2689 static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
2690 {
2691 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
2692 }
2693 #endif /* CONFIG_RTNETLINK */
2694
2695 static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
2696 {
2697 switch (nlh->nlmsg_type) {
2698 default:
2699 gemu_log("Unknown host audit message type %d\n",
2700 nlh->nlmsg_type);
2701 return -TARGET_EINVAL;
2702 }
2703 return 0;
2704 }
2705
2706 static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
2707 size_t len)
2708 {
2709 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
2710 }
2711
2712 static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
2713 {
2714 switch (nlh->nlmsg_type) {
2715 case AUDIT_USER:
2716 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
2717 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
2718 break;
2719 default:
2720 gemu_log("Unknown target audit message type %d\n",
2721 nlh->nlmsg_type);
2722 return -TARGET_EINVAL;
2723 }
2724
2725 return 0;
2726 }
2727
2728 static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
2729 {
2730 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
2731 }
2732
2733 /* do_setsockopt() Must return target values and target errnos. */
2734 static abi_long do_setsockopt(int sockfd, int level, int optname,
2735 abi_ulong optval_addr, socklen_t optlen)
2736 {
2737 abi_long ret;
2738 int val;
2739 struct ip_mreqn *ip_mreq;
2740 struct ip_mreq_source *ip_mreq_source;
2741
2742 switch(level) {
2743 case SOL_TCP:
2744 /* TCP options all take an 'int' value. */
2745 if (optlen < sizeof(uint32_t))
2746 return -TARGET_EINVAL;
2747
2748 if (get_user_u32(val, optval_addr))
2749 return -TARGET_EFAULT;
2750 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2751 break;
2752 case SOL_IP:
2753 switch(optname) {
2754 case IP_TOS:
2755 case IP_TTL:
2756 case IP_HDRINCL:
2757 case IP_ROUTER_ALERT:
2758 case IP_RECVOPTS:
2759 case IP_RETOPTS:
2760 case IP_PKTINFO:
2761 case IP_MTU_DISCOVER:
2762 case IP_RECVERR:
2763 case IP_RECVTOS:
2764 #ifdef IP_FREEBIND
2765 case IP_FREEBIND:
2766 #endif
2767 case IP_MULTICAST_TTL:
2768 case IP_MULTICAST_LOOP:
2769 val = 0;
2770 if (optlen >= sizeof(uint32_t)) {
2771 if (get_user_u32(val, optval_addr))
2772 return -TARGET_EFAULT;
2773 } else if (optlen >= 1) {
2774 if (get_user_u8(val, optval_addr))
2775 return -TARGET_EFAULT;
2776 }
2777 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2778 break;
2779 case IP_ADD_MEMBERSHIP:
2780 case IP_DROP_MEMBERSHIP:
2781 if (optlen < sizeof (struct target_ip_mreq) ||
2782 optlen > sizeof (struct target_ip_mreqn))
2783 return -TARGET_EINVAL;
2784
2785 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2786 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2787 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2788 break;
2789
2790 case IP_BLOCK_SOURCE:
2791 case IP_UNBLOCK_SOURCE:
2792 case IP_ADD_SOURCE_MEMBERSHIP:
2793 case IP_DROP_SOURCE_MEMBERSHIP:
2794 if (optlen != sizeof (struct target_ip_mreq_source))
2795 return -TARGET_EINVAL;
2796
2797 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2798 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2799 unlock_user (ip_mreq_source, optval_addr, 0);
2800 break;
2801
2802 default:
2803 goto unimplemented;
2804 }
2805 break;
2806 case SOL_IPV6:
2807 switch (optname) {
2808 case IPV6_MTU_DISCOVER:
2809 case IPV6_MTU:
2810 case IPV6_V6ONLY:
2811 case IPV6_RECVPKTINFO:
2812 val = 0;
2813 if (optlen < sizeof(uint32_t)) {
2814 return -TARGET_EINVAL;
2815 }
2816 if (get_user_u32(val, optval_addr)) {
2817 return -TARGET_EFAULT;
2818 }
2819 ret = get_errno(setsockopt(sockfd, level, optname,
2820 &val, sizeof(val)));
2821 break;
2822 default:
2823 goto unimplemented;
2824 }
2825 break;
2826 case SOL_RAW:
2827 switch (optname) {
2828 case ICMP_FILTER:
2829 /* struct icmp_filter takes an u32 value */
2830 if (optlen < sizeof(uint32_t)) {
2831 return -TARGET_EINVAL;
2832 }
2833
2834 if (get_user_u32(val, optval_addr)) {
2835 return -TARGET_EFAULT;
2836 }
2837 ret = get_errno(setsockopt(sockfd, level, optname,
2838 &val, sizeof(val)));
2839 break;
2840
2841 default:
2842 goto unimplemented;
2843 }
2844 break;
2845 case TARGET_SOL_SOCKET:
2846 switch (optname) {
2847 case TARGET_SO_RCVTIMEO:
2848 {
2849 struct timeval tv;
2850
2851 optname = SO_RCVTIMEO;
2852
2853 set_timeout:
2854 if (optlen != sizeof(struct target_timeval)) {
2855 return -TARGET_EINVAL;
2856 }
2857
2858 if (copy_from_user_timeval(&tv, optval_addr)) {
2859 return -TARGET_EFAULT;
2860 }
2861
2862 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2863 &tv, sizeof(tv)));
2864 return ret;
2865 }
2866 case TARGET_SO_SNDTIMEO:
2867 optname = SO_SNDTIMEO;
2868 goto set_timeout;
2869 case TARGET_SO_ATTACH_FILTER:
2870 {
2871 struct target_sock_fprog *tfprog;
2872 struct target_sock_filter *tfilter;
2873 struct sock_fprog fprog;
2874 struct sock_filter *filter;
2875 int i;
2876
2877 if (optlen != sizeof(*tfprog)) {
2878 return -TARGET_EINVAL;
2879 }
2880 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2881 return -TARGET_EFAULT;
2882 }
2883 if (!lock_user_struct(VERIFY_READ, tfilter,
2884 tswapal(tfprog->filter), 0)) {
2885 unlock_user_struct(tfprog, optval_addr, 1);
2886 return -TARGET_EFAULT;
2887 }
2888
2889 fprog.len = tswap16(tfprog->len);
2890 filter = g_try_new(struct sock_filter, fprog.len);
2891 if (filter == NULL) {
2892 unlock_user_struct(tfilter, tfprog->filter, 1);
2893 unlock_user_struct(tfprog, optval_addr, 1);
2894 return -TARGET_ENOMEM;
2895 }
2896 for (i = 0; i < fprog.len; i++) {
2897 filter[i].code = tswap16(tfilter[i].code);
2898 filter[i].jt = tfilter[i].jt;
2899 filter[i].jf = tfilter[i].jf;
2900 filter[i].k = tswap32(tfilter[i].k);
2901 }
2902 fprog.filter = filter;
2903
2904 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2905 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
2906 g_free(filter);
2907
2908 unlock_user_struct(tfilter, tfprog->filter, 1);
2909 unlock_user_struct(tfprog, optval_addr, 1);
2910 return ret;
2911 }
2912 case TARGET_SO_BINDTODEVICE:
2913 {
2914 char *dev_ifname, *addr_ifname;
2915
2916 if (optlen > IFNAMSIZ - 1) {
2917 optlen = IFNAMSIZ - 1;
2918 }
2919 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2920 if (!dev_ifname) {
2921 return -TARGET_EFAULT;
2922 }
2923 optname = SO_BINDTODEVICE;
2924 addr_ifname = alloca(IFNAMSIZ);
2925 memcpy(addr_ifname, dev_ifname, optlen);
2926 addr_ifname[optlen] = 0;
2927 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2928 addr_ifname, optlen));
2929 unlock_user (dev_ifname, optval_addr, 0);
2930 return ret;
2931 }
2932 /* Options with 'int' argument. */
2933 case TARGET_SO_DEBUG:
2934 optname = SO_DEBUG;
2935 break;
2936 case TARGET_SO_REUSEADDR:
2937 optname = SO_REUSEADDR;
2938 break;
2939 case TARGET_SO_TYPE:
2940 optname = SO_TYPE;
2941 break;
2942 case TARGET_SO_ERROR:
2943 optname = SO_ERROR;
2944 break;
2945 case TARGET_SO_DONTROUTE:
2946 optname = SO_DONTROUTE;
2947 break;
2948 case TARGET_SO_BROADCAST:
2949 optname = SO_BROADCAST;
2950 break;
2951 case TARGET_SO_SNDBUF:
2952 optname = SO_SNDBUF;
2953 break;
2954 case TARGET_SO_SNDBUFFORCE:
2955 optname = SO_SNDBUFFORCE;
2956 break;
2957 case TARGET_SO_RCVBUF:
2958 optname = SO_RCVBUF;
2959 break;
2960 case TARGET_SO_RCVBUFFORCE:
2961 optname = SO_RCVBUFFORCE;
2962 break;
2963 case TARGET_SO_KEEPALIVE:
2964 optname = SO_KEEPALIVE;
2965 break;
2966 case TARGET_SO_OOBINLINE:
2967 optname = SO_OOBINLINE;
2968 break;
2969 case TARGET_SO_NO_CHECK:
2970 optname = SO_NO_CHECK;
2971 break;
2972 case TARGET_SO_PRIORITY:
2973 optname = SO_PRIORITY;
2974 break;
2975 #ifdef SO_BSDCOMPAT
2976 case TARGET_SO_BSDCOMPAT:
2977 optname = SO_BSDCOMPAT;
2978 break;
2979 #endif
2980 case TARGET_SO_PASSCRED:
2981 optname = SO_PASSCRED;
2982 break;
2983 case TARGET_SO_PASSSEC:
2984 optname = SO_PASSSEC;
2985 break;
2986 case TARGET_SO_TIMESTAMP:
2987 optname = SO_TIMESTAMP;
2988 break;
2989 case TARGET_SO_RCVLOWAT:
2990 optname = SO_RCVLOWAT;
2991 break;
2992 break;
2993 default:
2994 goto unimplemented;
2995 }
2996 if (optlen < sizeof(uint32_t))
2997 return -TARGET_EINVAL;
2998
2999 if (get_user_u32(val, optval_addr))
3000 return -TARGET_EFAULT;
3001 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
3002 break;
3003 default:
3004 unimplemented:
3005 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
3006 ret = -TARGET_ENOPROTOOPT;
3007 }
3008 return ret;
3009 }
3010
3011 /* do_getsockopt() Must return target values and target errnos. */
3012 static abi_long do_getsockopt(int sockfd, int level, int optname,
3013 abi_ulong optval_addr, abi_ulong optlen)
3014 {
3015 abi_long ret;
3016 int len, val;
3017 socklen_t lv;
3018
3019 switch(level) {
3020 case TARGET_SOL_SOCKET:
3021 level = SOL_SOCKET;
3022 switch (optname) {
3023 /* These don't just return a single integer */
3024 case TARGET_SO_LINGER:
3025 case TARGET_SO_RCVTIMEO:
3026 case TARGET_SO_SNDTIMEO:
3027 case TARGET_SO_PEERNAME:
3028 goto unimplemented;
3029 case TARGET_SO_PEERCRED: {
3030 struct ucred cr;
3031 socklen_t crlen;
3032 struct target_ucred *tcr;
3033
3034 if (get_user_u32(len, optlen)) {
3035 return -TARGET_EFAULT;
3036 }
3037 if (len < 0) {
3038 return -TARGET_EINVAL;
3039 }
3040
3041 crlen = sizeof(cr);
3042 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
3043 &cr, &crlen));
3044 if (ret < 0) {
3045 return ret;
3046 }
3047 if (len > crlen) {
3048 len = crlen;
3049 }
3050 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
3051 return -TARGET_EFAULT;
3052 }
3053 __put_user(cr.pid, &tcr->pid);
3054 __put_user(cr.uid, &tcr->uid);
3055 __put_user(cr.gid, &tcr->gid);
3056 unlock_user_struct(tcr, optval_addr, 1);
3057 if (put_user_u32(len, optlen)) {
3058 return -TARGET_EFAULT;
3059 }
3060 break;
3061 }
3062 /* Options with 'int' argument. */
3063 case TARGET_SO_DEBUG:
3064 optname = SO_DEBUG;
3065 goto int_case;
3066 case TARGET_SO_REUSEADDR:
3067 optname = SO_REUSEADDR;
3068 goto int_case;
3069 case TARGET_SO_TYPE:
3070 optname = SO_TYPE;
3071 goto int_case;
3072 case TARGET_SO_ERROR:
3073 optname = SO_ERROR;
3074 goto int_case;
3075 case TARGET_SO_DONTROUTE:
3076 optname = SO_DONTROUTE;
3077 goto int_case;
3078 case TARGET_SO_BROADCAST:
3079 optname = SO_BROADCAST;
3080 goto int_case;
3081 case TARGET_SO_SNDBUF:
3082 optname = SO_SNDBUF;
3083 goto int_case;
3084 case TARGET_SO_RCVBUF:
3085 optname = SO_RCVBUF;
3086 goto int_case;
3087 case TARGET_SO_KEEPALIVE:
3088 optname = SO_KEEPALIVE;
3089 goto int_case;
3090 case TARGET_SO_OOBINLINE:
3091 optname = SO_OOBINLINE;
3092 goto int_case;
3093 case TARGET_SO_NO_CHECK:
3094 optname = SO_NO_CHECK;
3095 goto int_case;
3096 case TARGET_SO_PRIORITY:
3097 optname = SO_PRIORITY;
3098 goto int_case;
3099 #ifdef SO_BSDCOMPAT
3100 case TARGET_SO_BSDCOMPAT:
3101 optname = SO_BSDCOMPAT;
3102 goto int_case;
3103 #endif
3104 case TARGET_SO_PASSCRED:
3105 optname = SO_PASSCRED;
3106 goto int_case;
3107 case TARGET_SO_TIMESTAMP:
3108 optname = SO_TIMESTAMP;
3109 goto int_case;
3110 case TARGET_SO_RCVLOWAT:
3111 optname = SO_RCVLOWAT;
3112 goto int_case;
3113 case TARGET_SO_ACCEPTCONN:
3114 optname = SO_ACCEPTCONN;
3115 goto int_case;
3116 default:
3117 goto int_case;
3118 }
3119 break;
3120 case SOL_TCP:
3121 /* TCP options all take an 'int' value. */
3122 int_case:
3123 if (get_user_u32(len, optlen))
3124 return -TARGET_EFAULT;
3125 if (len < 0)
3126 return -TARGET_EINVAL;
3127 lv = sizeof(lv);
3128 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
3129 if (ret < 0)
3130 return ret;
3131 if (optname == SO_TYPE) {
3132 val = host_to_target_sock_type(val);
3133 }
3134 if (len > lv)
3135 len = lv;
3136 if (len == 4) {
3137 if (put_user_u32(val, optval_addr))
3138 return -TARGET_EFAULT;
3139 } else {
3140 if (put_user_u8(val, optval_addr))
3141 return -TARGET_EFAULT;
3142 }
3143 if (put_user_u32(len, optlen))
3144 return -TARGET_EFAULT;
3145 break;
3146 case SOL_IP:
3147 switch(optname) {
3148 case IP_TOS:
3149 case IP_TTL:
3150 case IP_HDRINCL:
3151 case IP_ROUTER_ALERT:
3152 case IP_RECVOPTS:
3153 case IP_RETOPTS:
3154 case IP_PKTINFO:
3155 case IP_MTU_DISCOVER:
3156 case IP_RECVERR:
3157 case IP_RECVTOS:
3158 #ifdef IP_FREEBIND
3159 case IP_FREEBIND:
3160 #endif
3161 case IP_MULTICAST_TTL:
3162 case IP_MULTICAST_LOOP:
3163 if (get_user_u32(len, optlen))
3164 return -TARGET_EFAULT;
3165 if (len < 0)
3166 return -TARGET_EINVAL;
3167 lv = sizeof(lv);
3168 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
3169 if (ret < 0)
3170 return ret;
3171 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
3172 len = 1;
3173 if (put_user_u32(len, optlen)
3174 || put_user_u8(val, optval_addr))
3175 return -TARGET_EFAULT;
3176 } else {
3177 if (len > sizeof(int))
3178 len = sizeof(int);
3179 if (put_user_u32(len, optlen)
3180 || put_user_u32(val, optval_addr))
3181 return -TARGET_EFAULT;
3182 }
3183 break;
3184 default:
3185 ret = -TARGET_ENOPROTOOPT;
3186 break;
3187 }
3188 break;
3189 default:
3190 unimplemented:
3191 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
3192 level, optname);
3193 ret = -TARGET_EOPNOTSUPP;
3194 break;
3195 }
3196 return ret;
3197 }
3198
3199 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
3200 abi_ulong count, int copy)
3201 {
3202 struct target_iovec *target_vec;
3203 struct iovec *vec;
3204 abi_ulong total_len, max_len;
3205 int i;
3206 int err = 0;
3207 bool bad_address = false;
3208
3209 if (count == 0) {
3210 errno = 0;
3211 return NULL;
3212 }
3213 if (count > IOV_MAX) {
3214 errno = EINVAL;
3215 return NULL;
3216 }
3217
3218 vec = g_try_new0(struct iovec, count);
3219 if (vec == NULL) {
3220 errno = ENOMEM;
3221 return NULL;
3222 }
3223
3224 target_vec = lock_user(VERIFY_READ, target_addr,
3225 count * sizeof(struct target_iovec), 1);
3226 if (target_vec == NULL) {
3227 err = EFAULT;
3228 goto fail2;
3229 }
3230
3231 /* ??? If host page size > target page size, this will result in a
3232 value larger than what we can actually support. */
3233 max_len = 0x7fffffff & TARGET_PAGE_MASK;
3234 total_len = 0;
3235
3236 for (i = 0; i < count; i++) {
3237 abi_ulong base = tswapal(target_vec[i].iov_base);
3238 abi_long len = tswapal(target_vec[i].iov_len);
3239
3240 if (len < 0) {
3241 err = EINVAL;
3242 goto fail;
3243 } else if (len == 0) {
3244 /* Zero length pointer is ignored. */
3245 vec[i].iov_base = 0;
3246 } else {
3247 vec[i].iov_base = lock_user(type, base, len, copy);
3248 /* If the first buffer pointer is bad, this is a fault. But
3249 * subsequent bad buffers will result in a partial write; this
3250 * is realized by filling the vector with null pointers and
3251 * zero lengths. */
3252 if (!vec[i].iov_base) {
3253 if (i == 0) {
3254 err = EFAULT;
3255 goto fail;
3256 } else {
3257 bad_address = true;
3258 }
3259 }
3260 if (bad_address) {
3261 len = 0;
3262 }
3263 if (len > max_len - total_len) {
3264 len = max_len - total_len;
3265 }
3266 }
3267 vec[i].iov_len = len;
3268 total_len += len;
3269 }
3270
3271 unlock_user(target_vec, target_addr, 0);
3272 return vec;
3273
3274 fail:
3275 while (--i >= 0) {
3276 if (tswapal(target_vec[i].iov_len) > 0) {
3277 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
3278 }
3279 }
3280 unlock_user(target_vec, target_addr, 0);
3281 fail2:
3282 g_free(vec);
3283 errno = err;
3284 return NULL;
3285 }
3286
3287 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
3288 abi_ulong count, int copy)
3289 {
3290 struct target_iovec *target_vec;
3291 int i;
3292
3293 target_vec = lock_user(VERIFY_READ, target_addr,
3294 count * sizeof(struct target_iovec), 1);
3295 if (target_vec) {
3296 for (i = 0; i < count; i++) {
3297 abi_ulong base = tswapal(target_vec[i].iov_base);
3298 abi_long len = tswapal(target_vec[i].iov_len);
3299 if (len < 0) {
3300 break;
3301 }
3302 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
3303 }
3304 unlock_user(target_vec, target_addr, 0);
3305 }
3306
3307 g_free(vec);
3308 }
3309
3310 static inline int target_to_host_sock_type(int *type)
3311 {
3312 int host_type = 0;
3313 int target_type = *type;
3314
3315 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3316 case TARGET_SOCK_DGRAM:
3317 host_type = SOCK_DGRAM;
3318 break;
3319 case TARGET_SOCK_STREAM:
3320 host_type = SOCK_STREAM;
3321 break;
3322 default: