Improve log message for unaligned i/o
[qemu/ar7.git] / linux-user / syscall.c
blob37e1a1f9d0535bc4b880ed12d07c26f12fec7c9f
1 /*
2 * Linux syscalls
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define _ATFILE_SOURCE
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <elf.h>
25 #include <endian.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <time.h>
30 #include <limits.h>
31 #include <grp.h>
32 #include <sys/types.h>
33 #include <sys/ipc.h>
34 #include <sys/msg.h>
35 #include <sys/wait.h>
36 #include <sys/time.h>
37 #include <sys/stat.h>
38 #include <sys/mount.h>
39 #include <sys/file.h>
40 #include <sys/fsuid.h>
41 #include <sys/personality.h>
42 #include <sys/prctl.h>
43 #include <sys/resource.h>
44 #include <sys/mman.h>
45 #include <sys/swap.h>
46 #include <linux/capability.h>
47 #include <signal.h>
48 #include <sched.h>
49 #ifdef __ia64__
50 int __clone2(int (*fn)(void *), void *child_stack_base,
51 size_t stack_size, int flags, void *arg, ...);
52 #endif
53 #include <sys/socket.h>
54 #include <sys/un.h>
55 #include <sys/uio.h>
56 #include <sys/poll.h>
57 #include <sys/times.h>
58 #include <sys/shm.h>
59 #include <sys/sem.h>
60 #include <sys/statfs.h>
61 #include <utime.h>
62 #include <sys/sysinfo.h>
63 //#include <sys/user.h>
64 #include <netinet/ip.h>
65 #include <netinet/tcp.h>
66 #include <linux/wireless.h>
67 #include <linux/icmp.h>
68 #include "qemu-common.h"
69 #include "qemu/sockets.h"
70 #ifdef CONFIG_TIMERFD
71 #include <sys/timerfd.h>
72 #endif
73 #ifdef TARGET_GPROF
74 #include <sys/gmon.h>
75 #endif
76 #ifdef CONFIG_EVENTFD
77 #include <sys/eventfd.h>
78 #endif
79 #ifdef CONFIG_EPOLL
80 #include <sys/epoll.h>
81 #endif
82 #ifdef CONFIG_ATTR
83 #include "qemu/xattr.h"
84 #endif
85 #ifdef CONFIG_SENDFILE
86 #include <sys/sendfile.h>
87 #endif
89 #define termios host_termios
90 #define winsize host_winsize
91 #define termio host_termio
92 #define sgttyb host_sgttyb /* same as target */
93 #define tchars host_tchars /* same as target */
94 #define ltchars host_ltchars /* same as target */
96 #include <linux/termios.h>
97 #include <linux/unistd.h>
98 #include <linux/cdrom.h>
99 #include <linux/hdreg.h>
100 #include <linux/soundcard.h>
101 #include <linux/kd.h>
102 #include <linux/mtio.h>
103 #include <linux/fs.h>
104 #if defined(CONFIG_FIEMAP)
105 #include <linux/fiemap.h>
106 #endif
107 #include <linux/fb.h>
108 #include <linux/vt.h>
109 #include <linux/dm-ioctl.h>
110 #include <linux/reboot.h>
111 #include <linux/route.h>
112 #include <linux/filter.h>
113 #include <linux/blkpg.h>
114 #include "linux_loop.h"
115 #include "uname.h"
117 #include "qemu.h"
119 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
120 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
122 //#define DEBUG
124 //#include <linux/msdos_fs.h>
125 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
126 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
129 #undef _syscall0
130 #undef _syscall1
131 #undef _syscall2
132 #undef _syscall3
133 #undef _syscall4
134 #undef _syscall5
135 #undef _syscall6
137 #define _syscall0(type,name) \
138 static type name (void) \
140 return syscall(__NR_##name); \
143 #define _syscall1(type,name,type1,arg1) \
144 static type name (type1 arg1) \
146 return syscall(__NR_##name, arg1); \
149 #define _syscall2(type,name,type1,arg1,type2,arg2) \
150 static type name (type1 arg1,type2 arg2) \
152 return syscall(__NR_##name, arg1, arg2); \
155 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
156 static type name (type1 arg1,type2 arg2,type3 arg3) \
158 return syscall(__NR_##name, arg1, arg2, arg3); \
161 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
162 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
164 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
167 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
168 type5,arg5) \
169 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
171 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
175 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
176 type5,arg5,type6,arg6) \
177 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
178 type6 arg6) \
180 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
184 #define __NR_sys_uname __NR_uname
185 #define __NR_sys_getcwd1 __NR_getcwd
186 #define __NR_sys_getdents __NR_getdents
187 #define __NR_sys_getdents64 __NR_getdents64
188 #define __NR_sys_getpriority __NR_getpriority
189 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
190 #define __NR_sys_syslog __NR_syslog
191 #define __NR_sys_tgkill __NR_tgkill
192 #define __NR_sys_tkill __NR_tkill
193 #define __NR_sys_futex __NR_futex
194 #define __NR_sys_inotify_init __NR_inotify_init
195 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
196 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
198 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
199 defined(__s390x__)
200 #define __NR__llseek __NR_lseek
201 #endif
203 /* Newer kernel ports have llseek() instead of _llseek() */
204 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
205 #define TARGET_NR__llseek TARGET_NR_llseek
206 #endif
208 #ifdef __NR_gettid
209 _syscall0(int, gettid)
210 #else
211 /* This is a replacement for the host gettid() and must return a host
212 errno. */
213 static int gettid(void) {
214 return -ENOSYS;
216 #endif
217 #ifdef __NR_getdents
218 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
219 #endif
220 #if !defined(__NR_getdents) || \
221 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
222 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
223 #endif
224 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
225 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
226 loff_t *, res, uint, wh);
227 #endif
228 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
229 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
230 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
231 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
232 #endif
233 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
234 _syscall2(int,sys_tkill,int,tid,int,sig)
235 #endif
236 #ifdef __NR_exit_group
237 _syscall1(int,exit_group,int,error_code)
238 #endif
239 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
240 _syscall1(int,set_tid_address,int *,tidptr)
241 #endif
242 #if defined(TARGET_NR_futex) && defined(__NR_futex)
243 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
244 const struct timespec *,timeout,int *,uaddr2,int,val3)
245 #endif
246 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
247 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
248 unsigned long *, user_mask_ptr);
249 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
250 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
251 unsigned long *, user_mask_ptr);
252 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
253 void *, arg);
254 _syscall2(int, capget, struct __user_cap_header_struct *, header,
255 struct __user_cap_data_struct *, data);
256 _syscall2(int, capset, struct __user_cap_header_struct *, header,
257 struct __user_cap_data_struct *, data);
258 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
259 _syscall2(int, ioprio_get, int, which, int, who)
260 #endif
261 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
262 _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
263 #endif
265 static bitmask_transtbl fcntl_flags_tbl[] = {
266 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
267 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
268 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
269 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
270 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
271 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
272 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
273 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
274 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
275 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
276 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
277 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
278 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
279 #if defined(O_DIRECT)
280 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
281 #endif
282 #if defined(O_NOATIME)
283 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
284 #endif
285 #if defined(O_CLOEXEC)
286 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
287 #endif
288 #if defined(O_PATH)
289 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
290 #endif
291 /* Don't terminate the list prematurely on 64-bit host+guest. */
292 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
293 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
294 #endif
295 { 0, 0, 0, 0 }
298 static int sys_getcwd1(char *buf, size_t size)
300 if (getcwd(buf, size) == NULL) {
301 /* getcwd() sets errno */
302 return (-1);
304 return strlen(buf)+1;
307 static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
310 * open(2) has extra parameter 'mode' when called with
311 * flag O_CREAT.
313 if ((flags & O_CREAT) != 0) {
314 return (openat(dirfd, pathname, flags, mode));
316 return (openat(dirfd, pathname, flags));
319 #ifdef TARGET_NR_utimensat
320 #ifdef CONFIG_UTIMENSAT
321 static int sys_utimensat(int dirfd, const char *pathname,
322 const struct timespec times[2], int flags)
324 if (pathname == NULL)
325 return futimens(dirfd, times);
326 else
327 return utimensat(dirfd, pathname, times, flags);
329 #elif defined(__NR_utimensat)
330 #define __NR_sys_utimensat __NR_utimensat
331 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
332 const struct timespec *,tsp,int,flags)
333 #else
334 static int sys_utimensat(int dirfd, const char *pathname,
335 const struct timespec times[2], int flags)
337 errno = ENOSYS;
338 return -1;
340 #endif
341 #endif /* TARGET_NR_utimensat */
343 #ifdef CONFIG_INOTIFY
344 #include <sys/inotify.h>
346 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
347 static int sys_inotify_init(void)
349 return (inotify_init());
351 #endif
352 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
353 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
355 return (inotify_add_watch(fd, pathname, mask));
357 #endif
358 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
359 static int sys_inotify_rm_watch(int fd, int32_t wd)
361 return (inotify_rm_watch(fd, wd));
363 #endif
364 #ifdef CONFIG_INOTIFY1
365 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
366 static int sys_inotify_init1(int flags)
368 return (inotify_init1(flags));
370 #endif
371 #endif
372 #else
373 /* Userspace can usually survive runtime without inotify */
374 #undef TARGET_NR_inotify_init
375 #undef TARGET_NR_inotify_init1
376 #undef TARGET_NR_inotify_add_watch
377 #undef TARGET_NR_inotify_rm_watch
378 #endif /* CONFIG_INOTIFY */
380 #if defined(TARGET_NR_ppoll)
381 #ifndef __NR_ppoll
382 # define __NR_ppoll -1
383 #endif
384 #define __NR_sys_ppoll __NR_ppoll
385 _syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
386 struct timespec *, timeout, const sigset_t *, sigmask,
387 size_t, sigsetsize)
388 #endif
390 #if defined(TARGET_NR_pselect6)
391 #ifndef __NR_pselect6
392 # define __NR_pselect6 -1
393 #endif
394 #define __NR_sys_pselect6 __NR_pselect6
395 _syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
396 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
397 #endif
399 #if defined(TARGET_NR_prlimit64)
400 #ifndef __NR_prlimit64
401 # define __NR_prlimit64 -1
402 #endif
403 #define __NR_sys_prlimit64 __NR_prlimit64
404 /* The glibc rlimit structure may not be that used by the underlying syscall */
405 struct host_rlimit64 {
406 uint64_t rlim_cur;
407 uint64_t rlim_max;
409 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
410 const struct host_rlimit64 *, new_limit,
411 struct host_rlimit64 *, old_limit)
412 #endif
415 #if defined(TARGET_NR_timer_create)
416 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
417 static timer_t g_posix_timers[32] = { 0, } ;
419 static inline int next_free_host_timer(void)
421 int k ;
422 /* FIXME: Does finding the next free slot require a lock? */
423 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
424 if (g_posix_timers[k] == 0) {
425 g_posix_timers[k] = (timer_t) 1;
426 return k;
429 return -1;
431 #endif
433 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
434 #ifdef TARGET_ARM
435 static inline int regpairs_aligned(void *cpu_env) {
436 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
438 #elif defined(TARGET_MIPS)
439 static inline int regpairs_aligned(void *cpu_env) { return 1; }
440 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
441 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
442 * of registers which translates to the same as ARM/MIPS, because we start with
443 * r3 as arg1 */
444 static inline int regpairs_aligned(void *cpu_env) { return 1; }
445 #else
446 static inline int regpairs_aligned(void *cpu_env) { return 0; }
447 #endif
449 #define ERRNO_TABLE_SIZE 1200
451 /* target_to_host_errno_table[] is initialized from
452 * host_to_target_errno_table[] in syscall_init(). */
453 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
457 * This list is the union of errno values overridden in asm-<arch>/errno.h
458 * minus the errnos that are not actually generic to all archs.
460 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
461 [EIDRM] = TARGET_EIDRM,
462 [ECHRNG] = TARGET_ECHRNG,
463 [EL2NSYNC] = TARGET_EL2NSYNC,
464 [EL3HLT] = TARGET_EL3HLT,
465 [EL3RST] = TARGET_EL3RST,
466 [ELNRNG] = TARGET_ELNRNG,
467 [EUNATCH] = TARGET_EUNATCH,
468 [ENOCSI] = TARGET_ENOCSI,
469 [EL2HLT] = TARGET_EL2HLT,
470 [EDEADLK] = TARGET_EDEADLK,
471 [ENOLCK] = TARGET_ENOLCK,
472 [EBADE] = TARGET_EBADE,
473 [EBADR] = TARGET_EBADR,
474 [EXFULL] = TARGET_EXFULL,
475 [ENOANO] = TARGET_ENOANO,
476 [EBADRQC] = TARGET_EBADRQC,
477 [EBADSLT] = TARGET_EBADSLT,
478 [EBFONT] = TARGET_EBFONT,
479 [ENOSTR] = TARGET_ENOSTR,
480 [ENODATA] = TARGET_ENODATA,
481 [ETIME] = TARGET_ETIME,
482 [ENOSR] = TARGET_ENOSR,
483 [ENONET] = TARGET_ENONET,
484 [ENOPKG] = TARGET_ENOPKG,
485 [EREMOTE] = TARGET_EREMOTE,
486 [ENOLINK] = TARGET_ENOLINK,
487 [EADV] = TARGET_EADV,
488 [ESRMNT] = TARGET_ESRMNT,
489 [ECOMM] = TARGET_ECOMM,
490 [EPROTO] = TARGET_EPROTO,
491 [EDOTDOT] = TARGET_EDOTDOT,
492 [EMULTIHOP] = TARGET_EMULTIHOP,
493 [EBADMSG] = TARGET_EBADMSG,
494 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
495 [EOVERFLOW] = TARGET_EOVERFLOW,
496 [ENOTUNIQ] = TARGET_ENOTUNIQ,
497 [EBADFD] = TARGET_EBADFD,
498 [EREMCHG] = TARGET_EREMCHG,
499 [ELIBACC] = TARGET_ELIBACC,
500 [ELIBBAD] = TARGET_ELIBBAD,
501 [ELIBSCN] = TARGET_ELIBSCN,
502 [ELIBMAX] = TARGET_ELIBMAX,
503 [ELIBEXEC] = TARGET_ELIBEXEC,
504 [EILSEQ] = TARGET_EILSEQ,
505 [ENOSYS] = TARGET_ENOSYS,
506 [ELOOP] = TARGET_ELOOP,
507 [ERESTART] = TARGET_ERESTART,
508 [ESTRPIPE] = TARGET_ESTRPIPE,
509 [ENOTEMPTY] = TARGET_ENOTEMPTY,
510 [EUSERS] = TARGET_EUSERS,
511 [ENOTSOCK] = TARGET_ENOTSOCK,
512 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
513 [EMSGSIZE] = TARGET_EMSGSIZE,
514 [EPROTOTYPE] = TARGET_EPROTOTYPE,
515 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
516 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
517 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
518 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
519 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
520 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
521 [EADDRINUSE] = TARGET_EADDRINUSE,
522 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
523 [ENETDOWN] = TARGET_ENETDOWN,
524 [ENETUNREACH] = TARGET_ENETUNREACH,
525 [ENETRESET] = TARGET_ENETRESET,
526 [ECONNABORTED] = TARGET_ECONNABORTED,
527 [ECONNRESET] = TARGET_ECONNRESET,
528 [ENOBUFS] = TARGET_ENOBUFS,
529 [EISCONN] = TARGET_EISCONN,
530 [ENOTCONN] = TARGET_ENOTCONN,
531 [EUCLEAN] = TARGET_EUCLEAN,
532 [ENOTNAM] = TARGET_ENOTNAM,
533 [ENAVAIL] = TARGET_ENAVAIL,
534 [EISNAM] = TARGET_EISNAM,
535 [EREMOTEIO] = TARGET_EREMOTEIO,
536 [ESHUTDOWN] = TARGET_ESHUTDOWN,
537 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
538 [ETIMEDOUT] = TARGET_ETIMEDOUT,
539 [ECONNREFUSED] = TARGET_ECONNREFUSED,
540 [EHOSTDOWN] = TARGET_EHOSTDOWN,
541 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
542 [EALREADY] = TARGET_EALREADY,
543 [EINPROGRESS] = TARGET_EINPROGRESS,
544 [ESTALE] = TARGET_ESTALE,
545 [ECANCELED] = TARGET_ECANCELED,
546 [ENOMEDIUM] = TARGET_ENOMEDIUM,
547 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
548 #ifdef ENOKEY
549 [ENOKEY] = TARGET_ENOKEY,
550 #endif
551 #ifdef EKEYEXPIRED
552 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
553 #endif
554 #ifdef EKEYREVOKED
555 [EKEYREVOKED] = TARGET_EKEYREVOKED,
556 #endif
557 #ifdef EKEYREJECTED
558 [EKEYREJECTED] = TARGET_EKEYREJECTED,
559 #endif
560 #ifdef EOWNERDEAD
561 [EOWNERDEAD] = TARGET_EOWNERDEAD,
562 #endif
563 #ifdef ENOTRECOVERABLE
564 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
565 #endif
568 static inline int host_to_target_errno(int err)
570 if(host_to_target_errno_table[err])
571 return host_to_target_errno_table[err];
572 return err;
575 static inline int target_to_host_errno(int err)
577 if (target_to_host_errno_table[err])
578 return target_to_host_errno_table[err];
579 return err;
582 static inline abi_long get_errno(abi_long ret)
584 if (ret == -1)
585 return -host_to_target_errno(errno);
586 else
587 return ret;
590 static inline int is_error(abi_long ret)
592 return (abi_ulong)ret >= (abi_ulong)(-4096);
595 char *target_strerror(int err)
597 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
598 return NULL;
600 return strerror(target_to_host_errno(err));
603 static inline int host_to_target_sock_type(int host_type)
605 int target_type;
607 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
608 case SOCK_DGRAM:
609 target_type = TARGET_SOCK_DGRAM;
610 break;
611 case SOCK_STREAM:
612 target_type = TARGET_SOCK_STREAM;
613 break;
614 default:
615 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
616 break;
619 #if defined(SOCK_CLOEXEC)
620 if (host_type & SOCK_CLOEXEC) {
621 target_type |= TARGET_SOCK_CLOEXEC;
623 #endif
625 #if defined(SOCK_NONBLOCK)
626 if (host_type & SOCK_NONBLOCK) {
627 target_type |= TARGET_SOCK_NONBLOCK;
629 #endif
631 return target_type;
634 static abi_ulong target_brk;
635 static abi_ulong target_original_brk;
636 static abi_ulong brk_page;
638 void target_set_brk(abi_ulong new_brk)
640 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
641 brk_page = HOST_PAGE_ALIGN(target_brk);
644 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
645 #define DEBUGF_BRK(message, args...)
647 /* do_brk() must return target values and target errnos. */
648 abi_long do_brk(abi_ulong new_brk)
650 abi_long mapped_addr;
651 int new_alloc_size;
653 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
655 if (!new_brk) {
656 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
657 return target_brk;
659 if (new_brk < target_original_brk) {
660 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
661 target_brk);
662 return target_brk;
665 /* If the new brk is less than the highest page reserved to the
666 * target heap allocation, set it and we're almost done... */
667 if (new_brk <= brk_page) {
668 /* Heap contents are initialized to zero, as for anonymous
669 * mapped pages. */
670 if (new_brk > target_brk) {
671 memset(g2h(target_brk), 0, new_brk - target_brk);
673 target_brk = new_brk;
674 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
675 return target_brk;
678 /* We need to allocate more memory after the brk... Note that
679 * we don't use MAP_FIXED because that will map over the top of
680 * any existing mapping (like the one with the host libc or qemu
681 * itself); instead we treat "mapped but at wrong address" as
682 * a failure and unmap again.
684 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
685 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
686 PROT_READ|PROT_WRITE,
687 MAP_ANON|MAP_PRIVATE, 0, 0));
689 if (mapped_addr == brk_page) {
690 /* Heap contents are initialized to zero, as for anonymous
691 * mapped pages. Technically the new pages are already
692 * initialized to zero since they *are* anonymous mapped
693 * pages, however we have to take care with the contents that
694 * come from the remaining part of the previous page: it may
695 * contains garbage data due to a previous heap usage (grown
696 * then shrunken). */
697 memset(g2h(target_brk), 0, brk_page - target_brk);
699 target_brk = new_brk;
700 brk_page = HOST_PAGE_ALIGN(target_brk);
701 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
702 target_brk);
703 return target_brk;
704 } else if (mapped_addr != -1) {
705 /* Mapped but at wrong address, meaning there wasn't actually
706 * enough space for this brk.
708 target_munmap(mapped_addr, new_alloc_size);
709 mapped_addr = -1;
710 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
712 else {
713 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
716 #if defined(TARGET_ALPHA)
717 /* We (partially) emulate OSF/1 on Alpha, which requires we
718 return a proper errno, not an unchanged brk value. */
719 return -TARGET_ENOMEM;
720 #endif
721 /* For everything else, return the previous break. */
722 return target_brk;
725 static inline abi_long copy_from_user_fdset(fd_set *fds,
726 abi_ulong target_fds_addr,
727 int n)
729 int i, nw, j, k;
730 abi_ulong b, *target_fds;
732 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
733 if (!(target_fds = lock_user(VERIFY_READ,
734 target_fds_addr,
735 sizeof(abi_ulong) * nw,
736 1)))
737 return -TARGET_EFAULT;
739 FD_ZERO(fds);
740 k = 0;
741 for (i = 0; i < nw; i++) {
742 /* grab the abi_ulong */
743 __get_user(b, &target_fds[i]);
744 for (j = 0; j < TARGET_ABI_BITS; j++) {
745 /* check the bit inside the abi_ulong */
746 if ((b >> j) & 1)
747 FD_SET(k, fds);
748 k++;
752 unlock_user(target_fds, target_fds_addr, 0);
754 return 0;
757 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
758 abi_ulong target_fds_addr,
759 int n)
761 if (target_fds_addr) {
762 if (copy_from_user_fdset(fds, target_fds_addr, n))
763 return -TARGET_EFAULT;
764 *fds_ptr = fds;
765 } else {
766 *fds_ptr = NULL;
768 return 0;
771 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
772 const fd_set *fds,
773 int n)
775 int i, nw, j, k;
776 abi_long v;
777 abi_ulong *target_fds;
779 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
780 if (!(target_fds = lock_user(VERIFY_WRITE,
781 target_fds_addr,
782 sizeof(abi_ulong) * nw,
783 0)))
784 return -TARGET_EFAULT;
786 k = 0;
787 for (i = 0; i < nw; i++) {
788 v = 0;
789 for (j = 0; j < TARGET_ABI_BITS; j++) {
790 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
791 k++;
793 __put_user(v, &target_fds[i]);
796 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
798 return 0;
801 #if defined(__alpha__)
802 #define HOST_HZ 1024
803 #else
804 #define HOST_HZ 100
805 #endif
807 static inline abi_long host_to_target_clock_t(long ticks)
809 #if HOST_HZ == TARGET_HZ
810 return ticks;
811 #else
812 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
813 #endif
816 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
817 const struct rusage *rusage)
819 struct target_rusage *target_rusage;
821 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
822 return -TARGET_EFAULT;
823 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
824 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
825 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
826 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
827 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
828 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
829 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
830 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
831 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
832 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
833 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
834 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
835 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
836 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
837 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
838 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
839 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
840 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
841 unlock_user_struct(target_rusage, target_addr, 1);
843 return 0;
846 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
848 abi_ulong target_rlim_swap;
849 rlim_t result;
851 target_rlim_swap = tswapal(target_rlim);
852 if (target_rlim_swap == TARGET_RLIM_INFINITY)
853 return RLIM_INFINITY;
855 result = target_rlim_swap;
856 if (target_rlim_swap != (rlim_t)result)
857 return RLIM_INFINITY;
859 return result;
862 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
864 abi_ulong target_rlim_swap;
865 abi_ulong result;
867 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
868 target_rlim_swap = TARGET_RLIM_INFINITY;
869 else
870 target_rlim_swap = rlim;
871 result = tswapal(target_rlim_swap);
873 return result;
876 static inline int target_to_host_resource(int code)
878 switch (code) {
879 case TARGET_RLIMIT_AS:
880 return RLIMIT_AS;
881 case TARGET_RLIMIT_CORE:
882 return RLIMIT_CORE;
883 case TARGET_RLIMIT_CPU:
884 return RLIMIT_CPU;
885 case TARGET_RLIMIT_DATA:
886 return RLIMIT_DATA;
887 case TARGET_RLIMIT_FSIZE:
888 return RLIMIT_FSIZE;
889 case TARGET_RLIMIT_LOCKS:
890 return RLIMIT_LOCKS;
891 case TARGET_RLIMIT_MEMLOCK:
892 return RLIMIT_MEMLOCK;
893 case TARGET_RLIMIT_MSGQUEUE:
894 return RLIMIT_MSGQUEUE;
895 case TARGET_RLIMIT_NICE:
896 return RLIMIT_NICE;
897 case TARGET_RLIMIT_NOFILE:
898 return RLIMIT_NOFILE;
899 case TARGET_RLIMIT_NPROC:
900 return RLIMIT_NPROC;
901 case TARGET_RLIMIT_RSS:
902 return RLIMIT_RSS;
903 case TARGET_RLIMIT_RTPRIO:
904 return RLIMIT_RTPRIO;
905 case TARGET_RLIMIT_SIGPENDING:
906 return RLIMIT_SIGPENDING;
907 case TARGET_RLIMIT_STACK:
908 return RLIMIT_STACK;
909 default:
910 return code;
914 static inline abi_long copy_from_user_timeval(struct timeval *tv,
915 abi_ulong target_tv_addr)
917 struct target_timeval *target_tv;
919 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
920 return -TARGET_EFAULT;
922 __get_user(tv->tv_sec, &target_tv->tv_sec);
923 __get_user(tv->tv_usec, &target_tv->tv_usec);
925 unlock_user_struct(target_tv, target_tv_addr, 0);
927 return 0;
930 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
931 const struct timeval *tv)
933 struct target_timeval *target_tv;
935 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
936 return -TARGET_EFAULT;
938 __put_user(tv->tv_sec, &target_tv->tv_sec);
939 __put_user(tv->tv_usec, &target_tv->tv_usec);
941 unlock_user_struct(target_tv, target_tv_addr, 1);
943 return 0;
946 static inline abi_long copy_from_user_timezone(struct timezone *tz,
947 abi_ulong target_tz_addr)
949 struct target_timezone *target_tz;
951 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
952 return -TARGET_EFAULT;
955 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
956 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
958 unlock_user_struct(target_tz, target_tz_addr, 0);
960 return 0;
963 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
964 #include <mqueue.h>
966 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
967 abi_ulong target_mq_attr_addr)
969 struct target_mq_attr *target_mq_attr;
971 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
972 target_mq_attr_addr, 1))
973 return -TARGET_EFAULT;
975 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
976 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
977 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
978 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
980 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
982 return 0;
985 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
986 const struct mq_attr *attr)
988 struct target_mq_attr *target_mq_attr;
990 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
991 target_mq_attr_addr, 0))
992 return -TARGET_EFAULT;
994 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
995 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
996 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
997 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
999 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1001 return 0;
1003 #endif
1005 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1006 /* do_select() must return target values and target errnos. */
1007 static abi_long do_select(int n,
1008 abi_ulong rfd_addr, abi_ulong wfd_addr,
1009 abi_ulong efd_addr, abi_ulong target_tv_addr)
1011 fd_set rfds, wfds, efds;
1012 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1013 struct timeval tv, *tv_ptr;
1014 abi_long ret;
1016 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1017 if (ret) {
1018 return ret;
1020 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1021 if (ret) {
1022 return ret;
1024 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1025 if (ret) {
1026 return ret;
1029 if (target_tv_addr) {
1030 if (copy_from_user_timeval(&tv, target_tv_addr))
1031 return -TARGET_EFAULT;
1032 tv_ptr = &tv;
1033 } else {
1034 tv_ptr = NULL;
1037 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
1039 if (!is_error(ret)) {
1040 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1041 return -TARGET_EFAULT;
1042 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1043 return -TARGET_EFAULT;
1044 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1045 return -TARGET_EFAULT;
1047 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1048 return -TARGET_EFAULT;
1051 return ret;
1053 #endif
1055 static abi_long do_pipe2(int host_pipe[], int flags)
1057 #ifdef CONFIG_PIPE2
1058 return pipe2(host_pipe, flags);
1059 #else
1060 return -ENOSYS;
1061 #endif
1064 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1065 int flags, int is_pipe2)
1067 int host_pipe[2];
1068 abi_long ret;
1069 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1071 if (is_error(ret))
1072 return get_errno(ret);
1074 /* Several targets have special calling conventions for the original
1075 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1076 if (!is_pipe2) {
1077 #if defined(TARGET_ALPHA)
1078 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1079 return host_pipe[0];
1080 #elif defined(TARGET_MIPS)
1081 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1082 return host_pipe[0];
1083 #elif defined(TARGET_SH4)
1084 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1085 return host_pipe[0];
1086 #elif defined(TARGET_SPARC)
1087 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1088 return host_pipe[0];
1089 #endif
1092 if (put_user_s32(host_pipe[0], pipedes)
1093 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1094 return -TARGET_EFAULT;
1095 return get_errno(ret);
1098 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1099 abi_ulong target_addr,
1100 socklen_t len)
1102 struct target_ip_mreqn *target_smreqn;
1104 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1105 if (!target_smreqn)
1106 return -TARGET_EFAULT;
1107 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1108 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1109 if (len == sizeof(struct target_ip_mreqn))
1110 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
1111 unlock_user(target_smreqn, target_addr, 0);
1113 return 0;
1116 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1117 abi_ulong target_addr,
1118 socklen_t len)
1120 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1121 sa_family_t sa_family;
1122 struct target_sockaddr *target_saddr;
1124 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1125 if (!target_saddr)
1126 return -TARGET_EFAULT;
1128 sa_family = tswap16(target_saddr->sa_family);
1130 /* Oops. The caller might send a incomplete sun_path; sun_path
1131 * must be terminated by \0 (see the manual page), but
1132 * unfortunately it is quite common to specify sockaddr_un
1133 * length as "strlen(x->sun_path)" while it should be
1134 * "strlen(...) + 1". We'll fix that here if needed.
1135 * Linux kernel has a similar feature.
1138 if (sa_family == AF_UNIX) {
1139 if (len < unix_maxlen && len > 0) {
1140 char *cp = (char*)target_saddr;
1142 if ( cp[len-1] && !cp[len] )
1143 len++;
1145 if (len > unix_maxlen)
1146 len = unix_maxlen;
1149 memcpy(addr, target_saddr, len);
1150 addr->sa_family = sa_family;
1151 if (sa_family == AF_PACKET) {
1152 struct target_sockaddr_ll *lladdr;
1154 lladdr = (struct target_sockaddr_ll *)addr;
1155 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1156 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1158 unlock_user(target_saddr, target_addr, 0);
1160 return 0;
1163 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1164 struct sockaddr *addr,
1165 socklen_t len)
1167 struct target_sockaddr *target_saddr;
1169 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1170 if (!target_saddr)
1171 return -TARGET_EFAULT;
1172 memcpy(target_saddr, addr, len);
1173 target_saddr->sa_family = tswap16(addr->sa_family);
1174 unlock_user(target_saddr, target_addr, len);
1176 return 0;
1179 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1180 struct target_msghdr *target_msgh)
1182 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1183 abi_long msg_controllen;
1184 abi_ulong target_cmsg_addr;
1185 struct target_cmsghdr *target_cmsg;
1186 socklen_t space = 0;
1188 msg_controllen = tswapal(target_msgh->msg_controllen);
1189 if (msg_controllen < sizeof (struct target_cmsghdr))
1190 goto the_end;
1191 target_cmsg_addr = tswapal(target_msgh->msg_control);
1192 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1193 if (!target_cmsg)
1194 return -TARGET_EFAULT;
1196 while (cmsg && target_cmsg) {
1197 void *data = CMSG_DATA(cmsg);
1198 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1200 int len = tswapal(target_cmsg->cmsg_len)
1201 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1203 space += CMSG_SPACE(len);
1204 if (space > msgh->msg_controllen) {
1205 space -= CMSG_SPACE(len);
1206 /* This is a QEMU bug, since we allocated the payload
1207 * area ourselves (unlike overflow in host-to-target
1208 * conversion, which is just the guest giving us a buffer
1209 * that's too small). It can't happen for the payload types
1210 * we currently support; if it becomes an issue in future
1211 * we would need to improve our allocation strategy to
1212 * something more intelligent than "twice the size of the
1213 * target buffer we're reading from".
1215 gemu_log("Host cmsg overflow\n");
1216 break;
1219 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1220 cmsg->cmsg_level = SOL_SOCKET;
1221 } else {
1222 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1224 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1225 cmsg->cmsg_len = CMSG_LEN(len);
1227 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
1228 int *fd = (int *)data;
1229 int *target_fd = (int *)target_data;
1230 int i, numfds = len / sizeof(int);
1232 for (i = 0; i < numfds; i++) {
1233 __get_user(fd[i], target_fd + i);
1235 } else if (cmsg->cmsg_level == SOL_SOCKET
1236 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1237 struct ucred *cred = (struct ucred *)data;
1238 struct target_ucred *target_cred =
1239 (struct target_ucred *)target_data;
1241 __get_user(cred->pid, &target_cred->pid);
1242 __get_user(cred->uid, &target_cred->uid);
1243 __get_user(cred->gid, &target_cred->gid);
1244 } else {
1245 gemu_log("Unsupported ancillary data: %d/%d\n",
1246 cmsg->cmsg_level, cmsg->cmsg_type);
1247 memcpy(data, target_data, len);
1250 cmsg = CMSG_NXTHDR(msgh, cmsg);
1251 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1253 unlock_user(target_cmsg, target_cmsg_addr, 0);
1254 the_end:
1255 msgh->msg_controllen = space;
1256 return 0;
1259 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1260 struct msghdr *msgh)
1262 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1263 abi_long msg_controllen;
1264 abi_ulong target_cmsg_addr;
1265 struct target_cmsghdr *target_cmsg;
1266 socklen_t space = 0;
1268 msg_controllen = tswapal(target_msgh->msg_controllen);
1269 if (msg_controllen < sizeof (struct target_cmsghdr))
1270 goto the_end;
1271 target_cmsg_addr = tswapal(target_msgh->msg_control);
1272 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1273 if (!target_cmsg)
1274 return -TARGET_EFAULT;
1276 while (cmsg && target_cmsg) {
1277 void *data = CMSG_DATA(cmsg);
1278 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1280 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1281 int tgt_len, tgt_space;
1283 /* We never copy a half-header but may copy half-data;
1284 * this is Linux's behaviour in put_cmsg(). Note that
1285 * truncation here is a guest problem (which we report
1286 * to the guest via the CTRUNC bit), unlike truncation
1287 * in target_to_host_cmsg, which is a QEMU bug.
1289 if (msg_controllen < sizeof(struct cmsghdr)) {
1290 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1291 break;
1294 if (cmsg->cmsg_level == SOL_SOCKET) {
1295 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1296 } else {
1297 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1299 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1301 tgt_len = TARGET_CMSG_LEN(len);
1303 /* Payload types which need a different size of payload on
1304 * the target must adjust tgt_len here.
1306 switch (cmsg->cmsg_level) {
1307 case SOL_SOCKET:
1308 switch (cmsg->cmsg_type) {
1309 case SO_TIMESTAMP:
1310 tgt_len = sizeof(struct target_timeval);
1311 break;
1312 default:
1313 break;
1315 default:
1316 break;
1319 if (msg_controllen < tgt_len) {
1320 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1321 tgt_len = msg_controllen;
1324 /* We must now copy-and-convert len bytes of payload
1325 * into tgt_len bytes of destination space. Bear in mind
1326 * that in both source and destination we may be dealing
1327 * with a truncated value!
1329 switch (cmsg->cmsg_level) {
1330 case SOL_SOCKET:
1331 switch (cmsg->cmsg_type) {
1332 case SCM_RIGHTS:
1334 int *fd = (int *)data;
1335 int *target_fd = (int *)target_data;
1336 int i, numfds = tgt_len / sizeof(int);
1338 for (i = 0; i < numfds; i++) {
1339 __put_user(fd[i], target_fd + i);
1341 break;
1343 case SO_TIMESTAMP:
1345 struct timeval *tv = (struct timeval *)data;
1346 struct target_timeval *target_tv =
1347 (struct target_timeval *)target_data;
1349 if (len != sizeof(struct timeval) ||
1350 tgt_len != sizeof(struct target_timeval)) {
1351 goto unimplemented;
1354 /* copy struct timeval to target */
1355 __put_user(tv->tv_sec, &target_tv->tv_sec);
1356 __put_user(tv->tv_usec, &target_tv->tv_usec);
1357 break;
1359 case SCM_CREDENTIALS:
1361 struct ucred *cred = (struct ucred *)data;
1362 struct target_ucred *target_cred =
1363 (struct target_ucred *)target_data;
1365 __put_user(cred->pid, &target_cred->pid);
1366 __put_user(cred->uid, &target_cred->uid);
1367 __put_user(cred->gid, &target_cred->gid);
1368 break;
1370 default:
1371 goto unimplemented;
1373 break;
1375 default:
1376 unimplemented:
1377 gemu_log("Unsupported ancillary data: %d/%d\n",
1378 cmsg->cmsg_level, cmsg->cmsg_type);
1379 memcpy(target_data, data, MIN(len, tgt_len));
1380 if (tgt_len > len) {
1381 memset(target_data + len, 0, tgt_len - len);
1385 target_cmsg->cmsg_len = tswapal(tgt_len);
1386 tgt_space = TARGET_CMSG_SPACE(tgt_len);
1387 if (msg_controllen < tgt_space) {
1388 tgt_space = msg_controllen;
1390 msg_controllen -= tgt_space;
1391 space += tgt_space;
1392 cmsg = CMSG_NXTHDR(msgh, cmsg);
1393 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1395 unlock_user(target_cmsg, target_cmsg_addr, space);
1396 the_end:
1397 target_msgh->msg_controllen = tswapal(space);
1398 return 0;
1401 /* do_setsockopt() Must return target values and target errnos. */
1402 static abi_long do_setsockopt(int sockfd, int level, int optname,
1403 abi_ulong optval_addr, socklen_t optlen)
1405 abi_long ret;
1406 int val;
1407 struct ip_mreqn *ip_mreq;
1408 struct ip_mreq_source *ip_mreq_source;
1410 switch(level) {
1411 case SOL_TCP:
1412 /* TCP options all take an 'int' value. */
1413 if (optlen < sizeof(uint32_t))
1414 return -TARGET_EINVAL;
1416 if (get_user_u32(val, optval_addr))
1417 return -TARGET_EFAULT;
1418 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1419 break;
1420 case SOL_IP:
1421 switch(optname) {
1422 case IP_TOS:
1423 case IP_TTL:
1424 case IP_HDRINCL:
1425 case IP_ROUTER_ALERT:
1426 case IP_RECVOPTS:
1427 case IP_RETOPTS:
1428 case IP_PKTINFO:
1429 case IP_MTU_DISCOVER:
1430 case IP_RECVERR:
1431 case IP_RECVTOS:
1432 #ifdef IP_FREEBIND
1433 case IP_FREEBIND:
1434 #endif
1435 case IP_MULTICAST_TTL:
1436 case IP_MULTICAST_LOOP:
1437 val = 0;
1438 if (optlen >= sizeof(uint32_t)) {
1439 if (get_user_u32(val, optval_addr))
1440 return -TARGET_EFAULT;
1441 } else if (optlen >= 1) {
1442 if (get_user_u8(val, optval_addr))
1443 return -TARGET_EFAULT;
1445 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1446 break;
1447 case IP_ADD_MEMBERSHIP:
1448 case IP_DROP_MEMBERSHIP:
1449 if (optlen < sizeof (struct target_ip_mreq) ||
1450 optlen > sizeof (struct target_ip_mreqn))
1451 return -TARGET_EINVAL;
1453 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1454 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1455 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1456 break;
1458 case IP_BLOCK_SOURCE:
1459 case IP_UNBLOCK_SOURCE:
1460 case IP_ADD_SOURCE_MEMBERSHIP:
1461 case IP_DROP_SOURCE_MEMBERSHIP:
1462 if (optlen != sizeof (struct target_ip_mreq_source))
1463 return -TARGET_EINVAL;
1465 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1466 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1467 unlock_user (ip_mreq_source, optval_addr, 0);
1468 break;
1470 default:
1471 goto unimplemented;
1473 break;
1474 case SOL_IPV6:
1475 switch (optname) {
1476 case IPV6_MTU_DISCOVER:
1477 case IPV6_MTU:
1478 case IPV6_V6ONLY:
1479 case IPV6_RECVPKTINFO:
1480 val = 0;
1481 if (optlen < sizeof(uint32_t)) {
1482 return -TARGET_EINVAL;
1484 if (get_user_u32(val, optval_addr)) {
1485 return -TARGET_EFAULT;
1487 ret = get_errno(setsockopt(sockfd, level, optname,
1488 &val, sizeof(val)));
1489 break;
1490 default:
1491 goto unimplemented;
1493 break;
1494 case SOL_RAW:
1495 switch (optname) {
1496 case ICMP_FILTER:
1497 /* struct icmp_filter takes an u32 value */
1498 if (optlen < sizeof(uint32_t)) {
1499 return -TARGET_EINVAL;
1502 if (get_user_u32(val, optval_addr)) {
1503 return -TARGET_EFAULT;
1505 ret = get_errno(setsockopt(sockfd, level, optname,
1506 &val, sizeof(val)));
1507 break;
1509 default:
1510 goto unimplemented;
1512 break;
1513 case TARGET_SOL_SOCKET:
1514 switch (optname) {
1515 case TARGET_SO_RCVTIMEO:
1517 struct timeval tv;
1519 optname = SO_RCVTIMEO;
1521 set_timeout:
1522 if (optlen != sizeof(struct target_timeval)) {
1523 return -TARGET_EINVAL;
1526 if (copy_from_user_timeval(&tv, optval_addr)) {
1527 return -TARGET_EFAULT;
1530 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1531 &tv, sizeof(tv)));
1532 return ret;
1534 case TARGET_SO_SNDTIMEO:
1535 optname = SO_SNDTIMEO;
1536 goto set_timeout;
1537 case TARGET_SO_ATTACH_FILTER:
1539 struct target_sock_fprog *tfprog;
1540 struct target_sock_filter *tfilter;
1541 struct sock_fprog fprog;
1542 struct sock_filter *filter;
1543 int i;
1545 if (optlen != sizeof(*tfprog)) {
1546 return -TARGET_EINVAL;
1548 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
1549 return -TARGET_EFAULT;
1551 if (!lock_user_struct(VERIFY_READ, tfilter,
1552 tswapal(tfprog->filter), 0)) {
1553 unlock_user_struct(tfprog, optval_addr, 1);
1554 return -TARGET_EFAULT;
1557 fprog.len = tswap16(tfprog->len);
1558 filter = malloc(fprog.len * sizeof(*filter));
1559 if (filter == NULL) {
1560 unlock_user_struct(tfilter, tfprog->filter, 1);
1561 unlock_user_struct(tfprog, optval_addr, 1);
1562 return -TARGET_ENOMEM;
1564 for (i = 0; i < fprog.len; i++) {
1565 filter[i].code = tswap16(tfilter[i].code);
1566 filter[i].jt = tfilter[i].jt;
1567 filter[i].jf = tfilter[i].jf;
1568 filter[i].k = tswap32(tfilter[i].k);
1570 fprog.filter = filter;
1572 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
1573 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
1574 free(filter);
1576 unlock_user_struct(tfilter, tfprog->filter, 1);
1577 unlock_user_struct(tfprog, optval_addr, 1);
1578 return ret;
1580 case TARGET_SO_BINDTODEVICE:
1582 char *dev_ifname, *addr_ifname;
1584 if (optlen > IFNAMSIZ - 1) {
1585 optlen = IFNAMSIZ - 1;
1587 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1588 if (!dev_ifname) {
1589 return -TARGET_EFAULT;
1591 optname = SO_BINDTODEVICE;
1592 addr_ifname = alloca(IFNAMSIZ);
1593 memcpy(addr_ifname, dev_ifname, optlen);
1594 addr_ifname[optlen] = 0;
1595 ret = get_errno(setsockopt(sockfd, level, optname, addr_ifname, optlen));
1596 unlock_user (dev_ifname, optval_addr, 0);
1597 return ret;
1599 /* Options with 'int' argument. */
1600 case TARGET_SO_DEBUG:
1601 optname = SO_DEBUG;
1602 break;
1603 case TARGET_SO_REUSEADDR:
1604 optname = SO_REUSEADDR;
1605 break;
1606 case TARGET_SO_TYPE:
1607 optname = SO_TYPE;
1608 break;
1609 case TARGET_SO_ERROR:
1610 optname = SO_ERROR;
1611 break;
1612 case TARGET_SO_DONTROUTE:
1613 optname = SO_DONTROUTE;
1614 break;
1615 case TARGET_SO_BROADCAST:
1616 optname = SO_BROADCAST;
1617 break;
1618 case TARGET_SO_SNDBUF:
1619 optname = SO_SNDBUF;
1620 break;
1621 case TARGET_SO_SNDBUFFORCE:
1622 optname = SO_SNDBUFFORCE;
1623 break;
1624 case TARGET_SO_RCVBUF:
1625 optname = SO_RCVBUF;
1626 break;
1627 case TARGET_SO_RCVBUFFORCE:
1628 optname = SO_RCVBUFFORCE;
1629 break;
1630 case TARGET_SO_KEEPALIVE:
1631 optname = SO_KEEPALIVE;
1632 break;
1633 case TARGET_SO_OOBINLINE:
1634 optname = SO_OOBINLINE;
1635 break;
1636 case TARGET_SO_NO_CHECK:
1637 optname = SO_NO_CHECK;
1638 break;
1639 case TARGET_SO_PRIORITY:
1640 optname = SO_PRIORITY;
1641 break;
1642 #ifdef SO_BSDCOMPAT
1643 case TARGET_SO_BSDCOMPAT:
1644 optname = SO_BSDCOMPAT;
1645 break;
1646 #endif
1647 case TARGET_SO_PASSCRED:
1648 optname = SO_PASSCRED;
1649 break;
1650 case TARGET_SO_PASSSEC:
1651 optname = SO_PASSSEC;
1652 break;
1653 case TARGET_SO_TIMESTAMP:
1654 optname = SO_TIMESTAMP;
1655 break;
1656 case TARGET_SO_RCVLOWAT:
1657 optname = SO_RCVLOWAT;
1658 break;
1659 break;
1660 default:
1661 goto unimplemented;
1663 if (optlen < sizeof(uint32_t))
1664 return -TARGET_EINVAL;
1666 if (get_user_u32(val, optval_addr))
1667 return -TARGET_EFAULT;
1668 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
1669 break;
1670 default:
1671 unimplemented:
1672 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
1673 ret = -TARGET_ENOPROTOOPT;
1675 return ret;
1678 /* do_getsockopt() Must return target values and target errnos. */
1679 static abi_long do_getsockopt(int sockfd, int level, int optname,
1680 abi_ulong optval_addr, abi_ulong optlen)
1682 abi_long ret;
1683 int len, val;
1684 socklen_t lv;
1686 switch(level) {
1687 case TARGET_SOL_SOCKET:
1688 level = SOL_SOCKET;
1689 switch (optname) {
1690 /* These don't just return a single integer */
1691 case TARGET_SO_LINGER:
1692 case TARGET_SO_RCVTIMEO:
1693 case TARGET_SO_SNDTIMEO:
1694 case TARGET_SO_PEERNAME:
1695 goto unimplemented;
1696 case TARGET_SO_PEERCRED: {
1697 struct ucred cr;
1698 socklen_t crlen;
1699 struct target_ucred *tcr;
1701 if (get_user_u32(len, optlen)) {
1702 return -TARGET_EFAULT;
1704 if (len < 0) {
1705 return -TARGET_EINVAL;
1708 crlen = sizeof(cr);
1709 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1710 &cr, &crlen));
1711 if (ret < 0) {
1712 return ret;
1714 if (len > crlen) {
1715 len = crlen;
1717 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1718 return -TARGET_EFAULT;
1720 __put_user(cr.pid, &tcr->pid);
1721 __put_user(cr.uid, &tcr->uid);
1722 __put_user(cr.gid, &tcr->gid);
1723 unlock_user_struct(tcr, optval_addr, 1);
1724 if (put_user_u32(len, optlen)) {
1725 return -TARGET_EFAULT;
1727 break;
1729 /* Options with 'int' argument. */
1730 case TARGET_SO_DEBUG:
1731 optname = SO_DEBUG;
1732 goto int_case;
1733 case TARGET_SO_REUSEADDR:
1734 optname = SO_REUSEADDR;
1735 goto int_case;
1736 case TARGET_SO_TYPE:
1737 optname = SO_TYPE;
1738 goto int_case;
1739 case TARGET_SO_ERROR:
1740 optname = SO_ERROR;
1741 goto int_case;
1742 case TARGET_SO_DONTROUTE:
1743 optname = SO_DONTROUTE;
1744 goto int_case;
1745 case TARGET_SO_BROADCAST:
1746 optname = SO_BROADCAST;
1747 goto int_case;
1748 case TARGET_SO_SNDBUF:
1749 optname = SO_SNDBUF;
1750 goto int_case;
1751 case TARGET_SO_RCVBUF:
1752 optname = SO_RCVBUF;
1753 goto int_case;
1754 case TARGET_SO_KEEPALIVE:
1755 optname = SO_KEEPALIVE;
1756 goto int_case;
1757 case TARGET_SO_OOBINLINE:
1758 optname = SO_OOBINLINE;
1759 goto int_case;
1760 case TARGET_SO_NO_CHECK:
1761 optname = SO_NO_CHECK;
1762 goto int_case;
1763 case TARGET_SO_PRIORITY:
1764 optname = SO_PRIORITY;
1765 goto int_case;
1766 #ifdef SO_BSDCOMPAT
1767 case TARGET_SO_BSDCOMPAT:
1768 optname = SO_BSDCOMPAT;
1769 goto int_case;
1770 #endif
1771 case TARGET_SO_PASSCRED:
1772 optname = SO_PASSCRED;
1773 goto int_case;
1774 case TARGET_SO_TIMESTAMP:
1775 optname = SO_TIMESTAMP;
1776 goto int_case;
1777 case TARGET_SO_RCVLOWAT:
1778 optname = SO_RCVLOWAT;
1779 goto int_case;
1780 case TARGET_SO_ACCEPTCONN:
1781 optname = SO_ACCEPTCONN;
1782 goto int_case;
1783 default:
1784 goto int_case;
1786 break;
1787 case SOL_TCP:
1788 /* TCP options all take an 'int' value. */
1789 int_case:
1790 if (get_user_u32(len, optlen))
1791 return -TARGET_EFAULT;
1792 if (len < 0)
1793 return -TARGET_EINVAL;
1794 lv = sizeof(lv);
1795 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1796 if (ret < 0)
1797 return ret;
1798 if (optname == SO_TYPE) {
1799 val = host_to_target_sock_type(val);
1801 if (len > lv)
1802 len = lv;
1803 if (len == 4) {
1804 if (put_user_u32(val, optval_addr))
1805 return -TARGET_EFAULT;
1806 } else {
1807 if (put_user_u8(val, optval_addr))
1808 return -TARGET_EFAULT;
1810 if (put_user_u32(len, optlen))
1811 return -TARGET_EFAULT;
1812 break;
1813 case SOL_IP:
1814 switch(optname) {
1815 case IP_TOS:
1816 case IP_TTL:
1817 case IP_HDRINCL:
1818 case IP_ROUTER_ALERT:
1819 case IP_RECVOPTS:
1820 case IP_RETOPTS:
1821 case IP_PKTINFO:
1822 case IP_MTU_DISCOVER:
1823 case IP_RECVERR:
1824 case IP_RECVTOS:
1825 #ifdef IP_FREEBIND
1826 case IP_FREEBIND:
1827 #endif
1828 case IP_MULTICAST_TTL:
1829 case IP_MULTICAST_LOOP:
1830 if (get_user_u32(len, optlen))
1831 return -TARGET_EFAULT;
1832 if (len < 0)
1833 return -TARGET_EINVAL;
1834 lv = sizeof(lv);
1835 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1836 if (ret < 0)
1837 return ret;
1838 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1839 len = 1;
1840 if (put_user_u32(len, optlen)
1841 || put_user_u8(val, optval_addr))
1842 return -TARGET_EFAULT;
1843 } else {
1844 if (len > sizeof(int))
1845 len = sizeof(int);
1846 if (put_user_u32(len, optlen)
1847 || put_user_u32(val, optval_addr))
1848 return -TARGET_EFAULT;
1850 break;
1851 default:
1852 ret = -TARGET_ENOPROTOOPT;
1853 break;
1855 break;
1856 default:
1857 unimplemented:
1858 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1859 level, optname);
1860 ret = -TARGET_EOPNOTSUPP;
1861 break;
1863 return ret;
1866 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1867 int count, int copy)
1869 struct target_iovec *target_vec;
1870 struct iovec *vec;
1871 abi_ulong total_len, max_len;
1872 int i;
1873 int err = 0;
1874 bool bad_address = false;
1876 if (count == 0) {
1877 errno = 0;
1878 return NULL;
1880 if (count < 0 || count > IOV_MAX) {
1881 errno = EINVAL;
1882 return NULL;
1885 vec = calloc(count, sizeof(struct iovec));
1886 if (vec == NULL) {
1887 errno = ENOMEM;
1888 return NULL;
1891 target_vec = lock_user(VERIFY_READ, target_addr,
1892 count * sizeof(struct target_iovec), 1);
1893 if (target_vec == NULL) {
1894 err = EFAULT;
1895 goto fail2;
1898 /* ??? If host page size > target page size, this will result in a
1899 value larger than what we can actually support. */
1900 max_len = 0x7fffffff & TARGET_PAGE_MASK;
1901 total_len = 0;
1903 for (i = 0; i < count; i++) {
1904 abi_ulong base = tswapal(target_vec[i].iov_base);
1905 abi_long len = tswapal(target_vec[i].iov_len);
1907 if (len < 0) {
1908 err = EINVAL;
1909 goto fail;
1910 } else if (len == 0) {
1911 /* Zero length pointer is ignored. */
1912 vec[i].iov_base = 0;
1913 } else {
1914 vec[i].iov_base = lock_user(type, base, len, copy);
1915 /* If the first buffer pointer is bad, this is a fault. But
1916 * subsequent bad buffers will result in a partial write; this
1917 * is realized by filling the vector with null pointers and
1918 * zero lengths. */
1919 if (!vec[i].iov_base) {
1920 if (i == 0) {
1921 err = EFAULT;
1922 goto fail;
1923 } else {
1924 bad_address = true;
1927 if (bad_address) {
1928 len = 0;
1930 if (len > max_len - total_len) {
1931 len = max_len - total_len;
1934 vec[i].iov_len = len;
1935 total_len += len;
1938 unlock_user(target_vec, target_addr, 0);
1939 return vec;
1941 fail:
1942 while (--i >= 0) {
1943 if (tswapal(target_vec[i].iov_len) > 0) {
1944 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
1947 unlock_user(target_vec, target_addr, 0);
1948 fail2:
1949 free(vec);
1950 errno = err;
1951 return NULL;
1954 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1955 int count, int copy)
1957 struct target_iovec *target_vec;
1958 int i;
1960 target_vec = lock_user(VERIFY_READ, target_addr,
1961 count * sizeof(struct target_iovec), 1);
1962 if (target_vec) {
1963 for (i = 0; i < count; i++) {
1964 abi_ulong base = tswapal(target_vec[i].iov_base);
1965 abi_long len = tswapal(target_vec[i].iov_len);
1966 if (len < 0) {
1967 break;
1969 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1971 unlock_user(target_vec, target_addr, 0);
1974 free(vec);
1977 static inline int target_to_host_sock_type(int *type)
1979 int host_type = 0;
1980 int target_type = *type;
1982 switch (target_type & TARGET_SOCK_TYPE_MASK) {
1983 case TARGET_SOCK_DGRAM:
1984 host_type = SOCK_DGRAM;
1985 break;
1986 case TARGET_SOCK_STREAM:
1987 host_type = SOCK_STREAM;
1988 break;
1989 default:
1990 host_type = target_type & TARGET_SOCK_TYPE_MASK;
1991 break;
1993 if (target_type & TARGET_SOCK_CLOEXEC) {
1994 #if defined(SOCK_CLOEXEC)
1995 host_type |= SOCK_CLOEXEC;
1996 #else
1997 return -TARGET_EINVAL;
1998 #endif
2000 if (target_type & TARGET_SOCK_NONBLOCK) {
2001 #if defined(SOCK_NONBLOCK)
2002 host_type |= SOCK_NONBLOCK;
2003 #elif !defined(O_NONBLOCK)
2004 return -TARGET_EINVAL;
2005 #endif
2007 *type = host_type;
2008 return 0;
2011 /* Try to emulate socket type flags after socket creation. */
2012 static int sock_flags_fixup(int fd, int target_type)
2014 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2015 if (target_type & TARGET_SOCK_NONBLOCK) {
2016 int flags = fcntl(fd, F_GETFL);
2017 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
2018 close(fd);
2019 return -TARGET_EINVAL;
2022 #endif
2023 return fd;
2026 /* do_socket() Must return target values and target errnos. */
2027 static abi_long do_socket(int domain, int type, int protocol)
2029 int target_type = type;
2030 int ret;
2032 ret = target_to_host_sock_type(&type);
2033 if (ret) {
2034 return ret;
2037 if (domain == PF_NETLINK)
2038 return -TARGET_EAFNOSUPPORT;
2039 ret = get_errno(socket(domain, type, protocol));
2040 if (ret >= 0) {
2041 ret = sock_flags_fixup(ret, target_type);
2043 return ret;
2046 /* do_bind() Must return target values and target errnos. */
2047 static abi_long do_bind(int sockfd, abi_ulong target_addr,
2048 socklen_t addrlen)
2050 void *addr;
2051 abi_long ret;
2053 if ((int)addrlen < 0) {
2054 return -TARGET_EINVAL;
2057 addr = alloca(addrlen+1);
2059 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2060 if (ret)
2061 return ret;
2063 return get_errno(bind(sockfd, addr, addrlen));
2066 /* do_connect() Must return target values and target errnos. */
2067 static abi_long do_connect(int sockfd, abi_ulong target_addr,
2068 socklen_t addrlen)
2070 void *addr;
2071 abi_long ret;
2073 if ((int)addrlen < 0) {
2074 return -TARGET_EINVAL;
2077 addr = alloca(addrlen+1);
2079 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2080 if (ret)
2081 return ret;
2083 return get_errno(connect(sockfd, addr, addrlen));
2086 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
2087 static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
2088 int flags, int send)
2090 abi_long ret, len;
2091 struct msghdr msg;
2092 int count;
2093 struct iovec *vec;
2094 abi_ulong target_vec;
2096 if (msgp->msg_name) {
2097 msg.msg_namelen = tswap32(msgp->msg_namelen);
2098 msg.msg_name = alloca(msg.msg_namelen+1);
2099 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
2100 msg.msg_namelen);
2101 if (ret) {
2102 goto out2;
2104 } else {
2105 msg.msg_name = NULL;
2106 msg.msg_namelen = 0;
2108 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
2109 msg.msg_control = alloca(msg.msg_controllen);
2110 msg.msg_flags = tswap32(msgp->msg_flags);
2112 count = tswapal(msgp->msg_iovlen);
2113 target_vec = tswapal(msgp->msg_iov);
2114 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
2115 target_vec, count, send);
2116 if (vec == NULL) {
2117 ret = -host_to_target_errno(errno);
2118 goto out2;
2120 msg.msg_iovlen = count;
2121 msg.msg_iov = vec;
2123 if (send) {
2124 ret = target_to_host_cmsg(&msg, msgp);
2125 if (ret == 0)
2126 ret = get_errno(sendmsg(fd, &msg, flags));
2127 } else {
2128 ret = get_errno(recvmsg(fd, &msg, flags));
2129 if (!is_error(ret)) {
2130 len = ret;
2131 ret = host_to_target_cmsg(msgp, &msg);
2132 if (!is_error(ret)) {
2133 msgp->msg_namelen = tswap32(msg.msg_namelen);
2134 if (msg.msg_name != NULL) {
2135 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
2136 msg.msg_name, msg.msg_namelen);
2137 if (ret) {
2138 goto out;
2142 ret = len;
2147 out:
2148 unlock_iovec(vec, target_vec, count, !send);
2149 out2:
2150 return ret;
2153 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
2154 int flags, int send)
2156 abi_long ret;
2157 struct target_msghdr *msgp;
2159 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
2160 msgp,
2161 target_msg,
2162 send ? 1 : 0)) {
2163 return -TARGET_EFAULT;
2165 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
2166 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
2167 return ret;
2170 #ifdef TARGET_NR_sendmmsg
2171 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
2172 * so it might not have this *mmsg-specific flag either.
2174 #ifndef MSG_WAITFORONE
2175 #define MSG_WAITFORONE 0x10000
2176 #endif
2178 static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
2179 unsigned int vlen, unsigned int flags,
2180 int send)
2182 struct target_mmsghdr *mmsgp;
2183 abi_long ret = 0;
2184 int i;
2186 if (vlen > UIO_MAXIOV) {
2187 vlen = UIO_MAXIOV;
2190 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
2191 if (!mmsgp) {
2192 return -TARGET_EFAULT;
2195 for (i = 0; i < vlen; i++) {
2196 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
2197 if (is_error(ret)) {
2198 break;
2200 mmsgp[i].msg_len = tswap32(ret);
2201 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2202 if (flags & MSG_WAITFORONE) {
2203 flags |= MSG_DONTWAIT;
2207 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
2209 /* Return number of datagrams sent if we sent any at all;
2210 * otherwise return the error.
2212 if (i) {
2213 return i;
2215 return ret;
2217 #endif
2219 /* If we don't have a system accept4() then just call accept.
2220 * The callsites to do_accept4() will ensure that they don't
2221 * pass a non-zero flags argument in this config.
2223 #ifndef CONFIG_ACCEPT4
2224 static inline int accept4(int sockfd, struct sockaddr *addr,
2225 socklen_t *addrlen, int flags)
2227 assert(flags == 0);
2228 return accept(sockfd, addr, addrlen);
2230 #endif
2232 /* do_accept4() Must return target values and target errnos. */
2233 static abi_long do_accept4(int fd, abi_ulong target_addr,
2234 abi_ulong target_addrlen_addr, int flags)
2236 socklen_t addrlen;
2237 void *addr;
2238 abi_long ret;
2239 int host_flags;
2241 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
2243 if (target_addr == 0) {
2244 return get_errno(accept4(fd, NULL, NULL, host_flags));
2247 /* linux returns EINVAL if addrlen pointer is invalid */
2248 if (get_user_u32(addrlen, target_addrlen_addr))
2249 return -TARGET_EINVAL;
2251 if ((int)addrlen < 0) {
2252 return -TARGET_EINVAL;
2255 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2256 return -TARGET_EINVAL;
2258 addr = alloca(addrlen);
2260 ret = get_errno(accept4(fd, addr, &addrlen, host_flags));
2261 if (!is_error(ret)) {
2262 host_to_target_sockaddr(target_addr, addr, addrlen);
2263 if (put_user_u32(addrlen, target_addrlen_addr))
2264 ret = -TARGET_EFAULT;
2266 return ret;
2269 /* do_getpeername() Must return target values and target errnos. */
2270 static abi_long do_getpeername(int fd, abi_ulong target_addr,
2271 abi_ulong target_addrlen_addr)
2273 socklen_t addrlen;
2274 void *addr;
2275 abi_long ret;
2277 if (get_user_u32(addrlen, target_addrlen_addr))
2278 return -TARGET_EFAULT;
2280 if ((int)addrlen < 0) {
2281 return -TARGET_EINVAL;
2284 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2285 return -TARGET_EFAULT;
2287 addr = alloca(addrlen);
2289 ret = get_errno(getpeername(fd, addr, &addrlen));
2290 if (!is_error(ret)) {
2291 host_to_target_sockaddr(target_addr, addr, addrlen);
2292 if (put_user_u32(addrlen, target_addrlen_addr))
2293 ret = -TARGET_EFAULT;
2295 return ret;
2298 /* do_getsockname() Must return target values and target errnos. */
2299 static abi_long do_getsockname(int fd, abi_ulong target_addr,
2300 abi_ulong target_addrlen_addr)
2302 socklen_t addrlen;
2303 void *addr;
2304 abi_long ret;
2306 if (get_user_u32(addrlen, target_addrlen_addr))
2307 return -TARGET_EFAULT;
2309 if ((int)addrlen < 0) {
2310 return -TARGET_EINVAL;
2313 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2314 return -TARGET_EFAULT;
2316 addr = alloca(addrlen);
2318 ret = get_errno(getsockname(fd, addr, &addrlen));
2319 if (!is_error(ret)) {
2320 host_to_target_sockaddr(target_addr, addr, addrlen);
2321 if (put_user_u32(addrlen, target_addrlen_addr))
2322 ret = -TARGET_EFAULT;
2324 return ret;
2327 /* do_socketpair() Must return target values and target errnos. */
2328 static abi_long do_socketpair(int domain, int type, int protocol,
2329 abi_ulong target_tab_addr)
2331 int tab[2];
2332 abi_long ret;
2334 target_to_host_sock_type(&type);
2336 ret = get_errno(socketpair(domain, type, protocol, tab));
2337 if (!is_error(ret)) {
2338 if (put_user_s32(tab[0], target_tab_addr)
2339 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2340 ret = -TARGET_EFAULT;
2342 return ret;
2345 /* do_sendto() Must return target values and target errnos. */
2346 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2347 abi_ulong target_addr, socklen_t addrlen)
2349 void *addr;
2350 void *host_msg;
2351 abi_long ret;
2353 if ((int)addrlen < 0) {
2354 return -TARGET_EINVAL;
2357 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2358 if (!host_msg)
2359 return -TARGET_EFAULT;
2360 if (target_addr) {
2361 addr = alloca(addrlen+1);
2362 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2363 if (ret) {
2364 unlock_user(host_msg, msg, 0);
2365 return ret;
2367 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2368 } else {
2369 ret = get_errno(send(fd, host_msg, len, flags));
2371 unlock_user(host_msg, msg, 0);
2372 return ret;
2375 /* do_recvfrom() Must return target values and target errnos. */
2376 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2377 abi_ulong target_addr,
2378 abi_ulong target_addrlen)
2380 socklen_t addrlen;
2381 void *addr;
2382 void *host_msg;
2383 abi_long ret;
2385 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2386 if (!host_msg)
2387 return -TARGET_EFAULT;
2388 if (target_addr) {
2389 if (get_user_u32(addrlen, target_addrlen)) {
2390 ret = -TARGET_EFAULT;
2391 goto fail;
2393 if ((int)addrlen < 0) {
2394 ret = -TARGET_EINVAL;
2395 goto fail;
2397 addr = alloca(addrlen);
2398 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2399 } else {
2400 addr = NULL; /* To keep compiler quiet. */
2401 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
2403 if (!is_error(ret)) {
2404 if (target_addr) {
2405 host_to_target_sockaddr(target_addr, addr, addrlen);
2406 if (put_user_u32(addrlen, target_addrlen)) {
2407 ret = -TARGET_EFAULT;
2408 goto fail;
2411 unlock_user(host_msg, msg, len);
2412 } else {
2413 fail:
2414 unlock_user(host_msg, msg, 0);
2416 return ret;
2419 #ifdef TARGET_NR_socketcall
2420 /* do_socketcall() Must return target values and target errnos. */
2421 static abi_long do_socketcall(int num, abi_ulong vptr)
2423 static const unsigned ac[] = { /* number of arguments per call */
2424 [SOCKOP_socket] = 3, /* domain, type, protocol */
2425 [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */
2426 [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */
2427 [SOCKOP_listen] = 2, /* sockfd, backlog */
2428 [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */
2429 [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */
2430 [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */
2431 [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */
2432 [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */
2433 [SOCKOP_send] = 4, /* sockfd, msg, len, flags */
2434 [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */
2435 [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2436 [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2437 [SOCKOP_shutdown] = 2, /* sockfd, how */
2438 [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */
2439 [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */
2440 [SOCKOP_setsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2441 [SOCKOP_getsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2443 abi_long a[6]; /* max 6 args */
2445 /* first, collect the arguments in a[] according to ac[] */
2446 if (num >= 0 && num < ARRAY_SIZE(ac)) {
2447 unsigned i;
2448 assert(ARRAY_SIZE(a) >= ac[num]); /* ensure we have space for args */
2449 for (i = 0; i < ac[num]; ++i) {
2450 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
2451 return -TARGET_EFAULT;
2456 /* now when we have the args, actually handle the call */
2457 switch (num) {
2458 case SOCKOP_socket: /* domain, type, protocol */
2459 return do_socket(a[0], a[1], a[2]);
2460 case SOCKOP_bind: /* sockfd, addr, addrlen */
2461 return do_bind(a[0], a[1], a[2]);
2462 case SOCKOP_connect: /* sockfd, addr, addrlen */
2463 return do_connect(a[0], a[1], a[2]);
2464 case SOCKOP_listen: /* sockfd, backlog */
2465 return get_errno(listen(a[0], a[1]));
2466 case SOCKOP_accept: /* sockfd, addr, addrlen */
2467 return do_accept4(a[0], a[1], a[2], 0);
2468 case SOCKOP_accept4: /* sockfd, addr, addrlen, flags */
2469 return do_accept4(a[0], a[1], a[2], a[3]);
2470 case SOCKOP_getsockname: /* sockfd, addr, addrlen */
2471 return do_getsockname(a[0], a[1], a[2]);
2472 case SOCKOP_getpeername: /* sockfd, addr, addrlen */
2473 return do_getpeername(a[0], a[1], a[2]);
2474 case SOCKOP_socketpair: /* domain, type, protocol, tab */
2475 return do_socketpair(a[0], a[1], a[2], a[3]);
2476 case SOCKOP_send: /* sockfd, msg, len, flags */
2477 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
2478 case SOCKOP_recv: /* sockfd, msg, len, flags */
2479 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
2480 case SOCKOP_sendto: /* sockfd, msg, len, flags, addr, addrlen */
2481 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
2482 case SOCKOP_recvfrom: /* sockfd, msg, len, flags, addr, addrlen */
2483 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
2484 case SOCKOP_shutdown: /* sockfd, how */
2485 return get_errno(shutdown(a[0], a[1]));
2486 case SOCKOP_sendmsg: /* sockfd, msg, flags */
2487 return do_sendrecvmsg(a[0], a[1], a[2], 1);
2488 case SOCKOP_recvmsg: /* sockfd, msg, flags */
2489 return do_sendrecvmsg(a[0], a[1], a[2], 0);
2490 case SOCKOP_setsockopt: /* sockfd, level, optname, optval, optlen */
2491 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
2492 case SOCKOP_getsockopt: /* sockfd, level, optname, optval, optlen */
2493 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
2494 default:
2495 gemu_log("Unsupported socketcall: %d\n", num);
2496 return -TARGET_ENOSYS;
2499 #endif
2501 #define N_SHM_REGIONS 32
2503 static struct shm_region {
2504 abi_ulong start;
2505 abi_ulong size;
2506 } shm_regions[N_SHM_REGIONS];
2508 struct target_semid_ds
2510 struct target_ipc_perm sem_perm;
2511 abi_ulong sem_otime;
2512 #if !defined(TARGET_PPC64)
2513 abi_ulong __unused1;
2514 #endif
2515 abi_ulong sem_ctime;
2516 #if !defined(TARGET_PPC64)
2517 abi_ulong __unused2;
2518 #endif
2519 abi_ulong sem_nsems;
2520 abi_ulong __unused3;
2521 abi_ulong __unused4;
2524 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2525 abi_ulong target_addr)
2527 struct target_ipc_perm *target_ip;
2528 struct target_semid_ds *target_sd;
2530 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2531 return -TARGET_EFAULT;
2532 target_ip = &(target_sd->sem_perm);
2533 host_ip->__key = tswap32(target_ip->__key);
2534 host_ip->uid = tswap32(target_ip->uid);
2535 host_ip->gid = tswap32(target_ip->gid);
2536 host_ip->cuid = tswap32(target_ip->cuid);
2537 host_ip->cgid = tswap32(target_ip->cgid);
2538 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2539 host_ip->mode = tswap32(target_ip->mode);
2540 #else
2541 host_ip->mode = tswap16(target_ip->mode);
2542 #endif
2543 #if defined(TARGET_PPC)
2544 host_ip->__seq = tswap32(target_ip->__seq);
2545 #else
2546 host_ip->__seq = tswap16(target_ip->__seq);
2547 #endif
2548 unlock_user_struct(target_sd, target_addr, 0);
2549 return 0;
2552 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2553 struct ipc_perm *host_ip)
2555 struct target_ipc_perm *target_ip;
2556 struct target_semid_ds *target_sd;
2558 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2559 return -TARGET_EFAULT;
2560 target_ip = &(target_sd->sem_perm);
2561 target_ip->__key = tswap32(host_ip->__key);
2562 target_ip->uid = tswap32(host_ip->uid);
2563 target_ip->gid = tswap32(host_ip->gid);
2564 target_ip->cuid = tswap32(host_ip->cuid);
2565 target_ip->cgid = tswap32(host_ip->cgid);
2566 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2567 target_ip->mode = tswap32(host_ip->mode);
2568 #else
2569 target_ip->mode = tswap16(host_ip->mode);
2570 #endif
2571 #if defined(TARGET_PPC)
2572 target_ip->__seq = tswap32(host_ip->__seq);
2573 #else
2574 target_ip->__seq = tswap16(host_ip->__seq);
2575 #endif
2576 unlock_user_struct(target_sd, target_addr, 1);
2577 return 0;
2580 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2581 abi_ulong target_addr)
2583 struct target_semid_ds *target_sd;
2585 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2586 return -TARGET_EFAULT;
2587 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2588 return -TARGET_EFAULT;
2589 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2590 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2591 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
2592 unlock_user_struct(target_sd, target_addr, 0);
2593 return 0;
2596 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2597 struct semid_ds *host_sd)
2599 struct target_semid_ds *target_sd;
2601 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2602 return -TARGET_EFAULT;
2603 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2604 return -TARGET_EFAULT;
2605 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2606 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2607 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
2608 unlock_user_struct(target_sd, target_addr, 1);
2609 return 0;
2612 struct target_seminfo {
2613 int semmap;
2614 int semmni;
2615 int semmns;
2616 int semmnu;
2617 int semmsl;
2618 int semopm;
2619 int semume;
2620 int semusz;
2621 int semvmx;
2622 int semaem;
2625 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2626 struct seminfo *host_seminfo)
2628 struct target_seminfo *target_seminfo;
2629 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2630 return -TARGET_EFAULT;
2631 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2632 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2633 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2634 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2635 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2636 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2637 __put_user(host_seminfo->semume, &target_seminfo->semume);
2638 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2639 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2640 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2641 unlock_user_struct(target_seminfo, target_addr, 1);
2642 return 0;
2645 union semun {
2646 int val;
2647 struct semid_ds *buf;
2648 unsigned short *array;
2649 struct seminfo *__buf;
2652 union target_semun {
2653 int val;
2654 abi_ulong buf;
2655 abi_ulong array;
2656 abi_ulong __buf;
2659 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2660 abi_ulong target_addr)
2662 int nsems;
2663 unsigned short *array;
2664 union semun semun;
2665 struct semid_ds semid_ds;
2666 int i, ret;
2668 semun.buf = &semid_ds;
2670 ret = semctl(semid, 0, IPC_STAT, semun);
2671 if (ret == -1)
2672 return get_errno(ret);
2674 nsems = semid_ds.sem_nsems;
2676 *host_array = malloc(nsems*sizeof(unsigned short));
2677 if (!*host_array) {
2678 return -TARGET_ENOMEM;
2680 array = lock_user(VERIFY_READ, target_addr,
2681 nsems*sizeof(unsigned short), 1);
2682 if (!array) {
2683 free(*host_array);
2684 return -TARGET_EFAULT;
2687 for(i=0; i<nsems; i++) {
2688 __get_user((*host_array)[i], &array[i]);
2690 unlock_user(array, target_addr, 0);
2692 return 0;
2695 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2696 unsigned short **host_array)
2698 int nsems;
2699 unsigned short *array;
2700 union semun semun;
2701 struct semid_ds semid_ds;
2702 int i, ret;
2704 semun.buf = &semid_ds;
2706 ret = semctl(semid, 0, IPC_STAT, semun);
2707 if (ret == -1)
2708 return get_errno(ret);
2710 nsems = semid_ds.sem_nsems;
2712 array = lock_user(VERIFY_WRITE, target_addr,
2713 nsems*sizeof(unsigned short), 0);
2714 if (!array)
2715 return -TARGET_EFAULT;
2717 for(i=0; i<nsems; i++) {
2718 __put_user((*host_array)[i], &array[i]);
2720 free(*host_array);
2721 unlock_user(array, target_addr, 1);
2723 return 0;
2726 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2727 union target_semun target_su)
2729 union semun arg;
2730 struct semid_ds dsarg;
2731 unsigned short *array = NULL;
2732 struct seminfo seminfo;
2733 abi_long ret = -TARGET_EINVAL;
2734 abi_long err;
2735 cmd &= 0xff;
2737 switch( cmd ) {
2738 case GETVAL:
2739 case SETVAL:
2740 /* In 64 bit cross-endian situations, we will erroneously pick up
2741 * the wrong half of the union for the "val" element. To rectify
2742 * this, the entire 8-byte structure is byteswapped, followed by
2743 * a swap of the 4 byte val field. In other cases, the data is
2744 * already in proper host byte order. */
2745 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
2746 target_su.buf = tswapal(target_su.buf);
2747 arg.val = tswap32(target_su.val);
2748 } else {
2749 arg.val = target_su.val;
2751 ret = get_errno(semctl(semid, semnum, cmd, arg));
2752 break;
2753 case GETALL:
2754 case SETALL:
2755 err = target_to_host_semarray(semid, &array, target_su.array);
2756 if (err)
2757 return err;
2758 arg.array = array;
2759 ret = get_errno(semctl(semid, semnum, cmd, arg));
2760 err = host_to_target_semarray(semid, target_su.array, &array);
2761 if (err)
2762 return err;
2763 break;
2764 case IPC_STAT:
2765 case IPC_SET:
2766 case SEM_STAT:
2767 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2768 if (err)
2769 return err;
2770 arg.buf = &dsarg;
2771 ret = get_errno(semctl(semid, semnum, cmd, arg));
2772 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2773 if (err)
2774 return err;
2775 break;
2776 case IPC_INFO:
2777 case SEM_INFO:
2778 arg.__buf = &seminfo;
2779 ret = get_errno(semctl(semid, semnum, cmd, arg));
2780 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2781 if (err)
2782 return err;
2783 break;
2784 case IPC_RMID:
2785 case GETPID:
2786 case GETNCNT:
2787 case GETZCNT:
2788 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2789 break;
2792 return ret;
2795 struct target_sembuf {
2796 unsigned short sem_num;
2797 short sem_op;
2798 short sem_flg;
2801 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2802 abi_ulong target_addr,
2803 unsigned nsops)
2805 struct target_sembuf *target_sembuf;
2806 int i;
2808 target_sembuf = lock_user(VERIFY_READ, target_addr,
2809 nsops*sizeof(struct target_sembuf), 1);
2810 if (!target_sembuf)
2811 return -TARGET_EFAULT;
2813 for(i=0; i<nsops; i++) {
2814 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2815 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2816 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2819 unlock_user(target_sembuf, target_addr, 0);
2821 return 0;
2824 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2826 struct sembuf sops[nsops];
2828 if (target_to_host_sembuf(sops, ptr, nsops))
2829 return -TARGET_EFAULT;
2831 return get_errno(semop(semid, sops, nsops));
2834 struct target_msqid_ds
2836 struct target_ipc_perm msg_perm;
2837 abi_ulong msg_stime;
2838 #if TARGET_ABI_BITS == 32
2839 abi_ulong __unused1;
2840 #endif
2841 abi_ulong msg_rtime;
2842 #if TARGET_ABI_BITS == 32
2843 abi_ulong __unused2;
2844 #endif
2845 abi_ulong msg_ctime;
2846 #if TARGET_ABI_BITS == 32
2847 abi_ulong __unused3;
2848 #endif
2849 abi_ulong __msg_cbytes;
2850 abi_ulong msg_qnum;
2851 abi_ulong msg_qbytes;
2852 abi_ulong msg_lspid;
2853 abi_ulong msg_lrpid;
2854 abi_ulong __unused4;
2855 abi_ulong __unused5;
2858 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2859 abi_ulong target_addr)
2861 struct target_msqid_ds *target_md;
2863 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2864 return -TARGET_EFAULT;
2865 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2866 return -TARGET_EFAULT;
2867 host_md->msg_stime = tswapal(target_md->msg_stime);
2868 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2869 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2870 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2871 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2872 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2873 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2874 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
2875 unlock_user_struct(target_md, target_addr, 0);
2876 return 0;
2879 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2880 struct msqid_ds *host_md)
2882 struct target_msqid_ds *target_md;
2884 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2885 return -TARGET_EFAULT;
2886 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2887 return -TARGET_EFAULT;
2888 target_md->msg_stime = tswapal(host_md->msg_stime);
2889 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2890 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2891 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2892 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2893 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2894 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2895 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
2896 unlock_user_struct(target_md, target_addr, 1);
2897 return 0;
2900 struct target_msginfo {
2901 int msgpool;
2902 int msgmap;
2903 int msgmax;
2904 int msgmnb;
2905 int msgmni;
2906 int msgssz;
2907 int msgtql;
2908 unsigned short int msgseg;
2911 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2912 struct msginfo *host_msginfo)
2914 struct target_msginfo *target_msginfo;
2915 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2916 return -TARGET_EFAULT;
2917 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2918 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2919 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2920 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2921 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2922 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2923 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2924 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2925 unlock_user_struct(target_msginfo, target_addr, 1);
2926 return 0;
2929 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
2931 struct msqid_ds dsarg;
2932 struct msginfo msginfo;
2933 abi_long ret = -TARGET_EINVAL;
2935 cmd &= 0xff;
2937 switch (cmd) {
2938 case IPC_STAT:
2939 case IPC_SET:
2940 case MSG_STAT:
2941 if (target_to_host_msqid_ds(&dsarg,ptr))
2942 return -TARGET_EFAULT;
2943 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2944 if (host_to_target_msqid_ds(ptr,&dsarg))
2945 return -TARGET_EFAULT;
2946 break;
2947 case IPC_RMID:
2948 ret = get_errno(msgctl(msgid, cmd, NULL));
2949 break;
2950 case IPC_INFO:
2951 case MSG_INFO:
2952 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2953 if (host_to_target_msginfo(ptr, &msginfo))
2954 return -TARGET_EFAULT;
2955 break;
2958 return ret;
2961 struct target_msgbuf {
2962 abi_long mtype;
2963 char mtext[1];
2966 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2967 ssize_t msgsz, int msgflg)
2969 struct target_msgbuf *target_mb;
2970 struct msgbuf *host_mb;
2971 abi_long ret = 0;
2973 if (msgsz < 0) {
2974 return -TARGET_EINVAL;
2977 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2978 return -TARGET_EFAULT;
2979 host_mb = malloc(msgsz+sizeof(long));
2980 if (!host_mb) {
2981 unlock_user_struct(target_mb, msgp, 0);
2982 return -TARGET_ENOMEM;
2984 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
2985 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
2986 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2987 free(host_mb);
2988 unlock_user_struct(target_mb, msgp, 0);
2990 return ret;
2993 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2994 unsigned int msgsz, abi_long msgtyp,
2995 int msgflg)
2997 struct target_msgbuf *target_mb;
2998 char *target_mtext;
2999 struct msgbuf *host_mb;
3000 abi_long ret = 0;
3002 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
3003 return -TARGET_EFAULT;
3005 host_mb = g_malloc(msgsz+sizeof(long));
3006 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
3008 if (ret > 0) {
3009 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
3010 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
3011 if (!target_mtext) {
3012 ret = -TARGET_EFAULT;
3013 goto end;
3015 memcpy(target_mb->mtext, host_mb->mtext, ret);
3016 unlock_user(target_mtext, target_mtext_addr, ret);
3019 target_mb->mtype = tswapal(host_mb->mtype);
3021 end:
3022 if (target_mb)
3023 unlock_user_struct(target_mb, msgp, 1);
3024 g_free(host_mb);
3025 return ret;
3028 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
3029 abi_ulong target_addr)
3031 struct target_shmid_ds *target_sd;
3033 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3034 return -TARGET_EFAULT;
3035 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
3036 return -TARGET_EFAULT;
3037 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3038 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
3039 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3040 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3041 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3042 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3043 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3044 unlock_user_struct(target_sd, target_addr, 0);
3045 return 0;
3048 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
3049 struct shmid_ds *host_sd)
3051 struct target_shmid_ds *target_sd;
3053 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3054 return -TARGET_EFAULT;
3055 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
3056 return -TARGET_EFAULT;
3057 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3058 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
3059 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3060 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3061 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3062 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3063 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3064 unlock_user_struct(target_sd, target_addr, 1);
3065 return 0;
3068 struct target_shminfo {
3069 abi_ulong shmmax;
3070 abi_ulong shmmin;
3071 abi_ulong shmmni;
3072 abi_ulong shmseg;
3073 abi_ulong shmall;
3076 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
3077 struct shminfo *host_shminfo)
3079 struct target_shminfo *target_shminfo;
3080 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
3081 return -TARGET_EFAULT;
3082 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
3083 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
3084 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
3085 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
3086 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
3087 unlock_user_struct(target_shminfo, target_addr, 1);
3088 return 0;
3091 struct target_shm_info {
3092 int used_ids;
3093 abi_ulong shm_tot;
3094 abi_ulong shm_rss;
3095 abi_ulong shm_swp;
3096 abi_ulong swap_attempts;
3097 abi_ulong swap_successes;
3100 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3101 struct shm_info *host_shm_info)
3103 struct target_shm_info *target_shm_info;
3104 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3105 return -TARGET_EFAULT;
3106 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3107 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3108 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3109 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3110 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3111 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3112 unlock_user_struct(target_shm_info, target_addr, 1);
3113 return 0;
3116 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
3118 struct shmid_ds dsarg;
3119 struct shminfo shminfo;
3120 struct shm_info shm_info;
3121 abi_long ret = -TARGET_EINVAL;
3123 cmd &= 0xff;
3125 switch(cmd) {
3126 case IPC_STAT:
3127 case IPC_SET:
3128 case SHM_STAT:
3129 if (target_to_host_shmid_ds(&dsarg, buf))
3130 return -TARGET_EFAULT;
3131 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3132 if (host_to_target_shmid_ds(buf, &dsarg))
3133 return -TARGET_EFAULT;
3134 break;
3135 case IPC_INFO:
3136 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3137 if (host_to_target_shminfo(buf, &shminfo))
3138 return -TARGET_EFAULT;
3139 break;
3140 case SHM_INFO:
3141 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3142 if (host_to_target_shm_info(buf, &shm_info))
3143 return -TARGET_EFAULT;
3144 break;
3145 case IPC_RMID:
3146 case SHM_LOCK:
3147 case SHM_UNLOCK:
3148 ret = get_errno(shmctl(shmid, cmd, NULL));
3149 break;
3152 return ret;
3155 static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3157 abi_long raddr;
3158 void *host_raddr;
3159 struct shmid_ds shm_info;
3160 int i,ret;
3162 /* find out the length of the shared memory segment */
3163 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3164 if (is_error(ret)) {
3165 /* can't get length, bail out */
3166 return ret;
3169 mmap_lock();
3171 if (shmaddr)
3172 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3173 else {
3174 abi_ulong mmap_start;
3176 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3178 if (mmap_start == -1) {
3179 errno = ENOMEM;
3180 host_raddr = (void *)-1;
3181 } else
3182 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3185 if (host_raddr == (void *)-1) {
3186 mmap_unlock();
3187 return get_errno((long)host_raddr);
3189 raddr=h2g((unsigned long)host_raddr);
3191 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3192 PAGE_VALID | PAGE_READ |
3193 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3195 for (i = 0; i < N_SHM_REGIONS; i++) {
3196 if (shm_regions[i].start == 0) {
3197 shm_regions[i].start = raddr;
3198 shm_regions[i].size = shm_info.shm_segsz;
3199 break;
3203 mmap_unlock();
3204 return raddr;
3208 static inline abi_long do_shmdt(abi_ulong shmaddr)
3210 int i;
3212 for (i = 0; i < N_SHM_REGIONS; ++i) {
3213 if (shm_regions[i].start == shmaddr) {
3214 shm_regions[i].start = 0;
3215 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
3216 break;
3220 return get_errno(shmdt(g2h(shmaddr)));
3223 #ifdef TARGET_NR_ipc
3224 /* ??? This only works with linear mappings. */
3225 /* do_ipc() must return target values and target errnos. */
3226 static abi_long do_ipc(unsigned int call, abi_long first,
3227 abi_long second, abi_long third,
3228 abi_long ptr, abi_long fifth)
3230 int version;
3231 abi_long ret = 0;
3233 version = call >> 16;
3234 call &= 0xffff;
3236 switch (call) {
3237 case IPCOP_semop:
3238 ret = do_semop(first, ptr, second);
3239 break;
3241 case IPCOP_semget:
3242 ret = get_errno(semget(first, second, third));
3243 break;
3245 case IPCOP_semctl: {
3246 /* The semun argument to semctl is passed by value, so dereference the
3247 * ptr argument. */
3248 abi_ulong atptr;
3249 get_user_ual(atptr, ptr);
3250 ret = do_semctl(first, second, third,
3251 (union target_semun) atptr);
3252 break;
3255 case IPCOP_msgget:
3256 ret = get_errno(msgget(first, second));
3257 break;
3259 case IPCOP_msgsnd:
3260 ret = do_msgsnd(first, ptr, second, third);
3261 break;
3263 case IPCOP_msgctl:
3264 ret = do_msgctl(first, second, ptr);
3265 break;
3267 case IPCOP_msgrcv:
3268 switch (version) {
3269 case 0:
3271 struct target_ipc_kludge {
3272 abi_long msgp;
3273 abi_long msgtyp;
3274 } *tmp;
3276 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3277 ret = -TARGET_EFAULT;
3278 break;
3281 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
3283 unlock_user_struct(tmp, ptr, 0);
3284 break;
3286 default:
3287 ret = do_msgrcv(first, ptr, second, fifth, third);
3289 break;
3291 case IPCOP_shmat:
3292 switch (version) {
3293 default:
3295 abi_ulong raddr;
3296 raddr = do_shmat(first, ptr, second);
3297 if (is_error(raddr))
3298 return get_errno(raddr);
3299 if (put_user_ual(raddr, third))
3300 return -TARGET_EFAULT;
3301 break;
3303 case 1:
3304 ret = -TARGET_EINVAL;
3305 break;
3307 break;
3308 case IPCOP_shmdt:
3309 ret = do_shmdt(ptr);
3310 break;
3312 case IPCOP_shmget:
3313 /* IPC_* flag values are the same on all linux platforms */
3314 ret = get_errno(shmget(first, second, third));
3315 break;
3317 /* IPC_* and SHM_* command values are the same on all linux platforms */
3318 case IPCOP_shmctl:
3319 ret = do_shmctl(first, second, ptr);
3320 break;
3321 default:
3322 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
3323 ret = -TARGET_ENOSYS;
3324 break;
3326 return ret;
3328 #endif
3330 /* kernel structure types definitions */
3332 #define STRUCT(name, ...) STRUCT_ ## name,
3333 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
3334 enum {
3335 #include "syscall_types.h"
3336 STRUCT_MAX
3338 #undef STRUCT
3339 #undef STRUCT_SPECIAL
3341 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
3342 #define STRUCT_SPECIAL(name)
3343 #include "syscall_types.h"
3344 #undef STRUCT
3345 #undef STRUCT_SPECIAL
3347 typedef struct IOCTLEntry IOCTLEntry;
3349 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3350 int fd, int cmd, abi_long arg);
3352 struct IOCTLEntry {
3353 int target_cmd;
3354 unsigned int host_cmd;
3355 const char *name;
3356 int access;
3357 do_ioctl_fn *do_ioctl;
3358 const argtype arg_type[5];
3361 #define IOC_R 0x0001
3362 #define IOC_W 0x0002
3363 #define IOC_RW (IOC_R | IOC_W)
3365 #define MAX_STRUCT_SIZE 4096
3367 #ifdef CONFIG_FIEMAP
3368 /* So fiemap access checks don't overflow on 32 bit systems.
3369 * This is very slightly smaller than the limit imposed by
3370 * the underlying kernel.
3372 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3373 / sizeof(struct fiemap_extent))
3375 static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3376 int fd, int cmd, abi_long arg)
3378 /* The parameter for this ioctl is a struct fiemap followed
3379 * by an array of struct fiemap_extent whose size is set
3380 * in fiemap->fm_extent_count. The array is filled in by the
3381 * ioctl.
3383 int target_size_in, target_size_out;
3384 struct fiemap *fm;
3385 const argtype *arg_type = ie->arg_type;
3386 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3387 void *argptr, *p;
3388 abi_long ret;
3389 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3390 uint32_t outbufsz;
3391 int free_fm = 0;
3393 assert(arg_type[0] == TYPE_PTR);
3394 assert(ie->access == IOC_RW);
3395 arg_type++;
3396 target_size_in = thunk_type_size(arg_type, 0);
3397 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3398 if (!argptr) {
3399 return -TARGET_EFAULT;
3401 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3402 unlock_user(argptr, arg, 0);
3403 fm = (struct fiemap *)buf_temp;
3404 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3405 return -TARGET_EINVAL;
3408 outbufsz = sizeof (*fm) +
3409 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3411 if (outbufsz > MAX_STRUCT_SIZE) {
3412 /* We can't fit all the extents into the fixed size buffer.
3413 * Allocate one that is large enough and use it instead.
3415 fm = malloc(outbufsz);
3416 if (!fm) {
3417 return -TARGET_ENOMEM;
3419 memcpy(fm, buf_temp, sizeof(struct fiemap));
3420 free_fm = 1;
3422 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3423 if (!is_error(ret)) {
3424 target_size_out = target_size_in;
3425 /* An extent_count of 0 means we were only counting the extents
3426 * so there are no structs to copy
3428 if (fm->fm_extent_count != 0) {
3429 target_size_out += fm->fm_mapped_extents * extent_size;
3431 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3432 if (!argptr) {
3433 ret = -TARGET_EFAULT;
3434 } else {
3435 /* Convert the struct fiemap */
3436 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3437 if (fm->fm_extent_count != 0) {
3438 p = argptr + target_size_in;
3439 /* ...and then all the struct fiemap_extents */
3440 for (i = 0; i < fm->fm_mapped_extents; i++) {
3441 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3442 THUNK_TARGET);
3443 p += extent_size;
3446 unlock_user(argptr, arg, target_size_out);
3449 if (free_fm) {
3450 free(fm);
3452 return ret;
3454 #endif
3456 static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3457 int fd, int cmd, abi_long arg)
3459 const argtype *arg_type = ie->arg_type;
3460 int target_size;
3461 void *argptr;
3462 int ret;
3463 struct ifconf *host_ifconf;
3464 uint32_t outbufsz;
3465 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3466 int target_ifreq_size;
3467 int nb_ifreq;
3468 int free_buf = 0;
3469 int i;
3470 int target_ifc_len;
3471 abi_long target_ifc_buf;
3472 int host_ifc_len;
3473 char *host_ifc_buf;
3475 assert(arg_type[0] == TYPE_PTR);
3476 assert(ie->access == IOC_RW);
3478 arg_type++;
3479 target_size = thunk_type_size(arg_type, 0);
3481 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3482 if (!argptr)
3483 return -TARGET_EFAULT;
3484 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3485 unlock_user(argptr, arg, 0);
3487 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3488 target_ifc_len = host_ifconf->ifc_len;
3489 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3491 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3492 nb_ifreq = target_ifc_len / target_ifreq_size;
3493 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3495 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3496 if (outbufsz > MAX_STRUCT_SIZE) {
3497 /* We can't fit all the extents into the fixed size buffer.
3498 * Allocate one that is large enough and use it instead.
3500 host_ifconf = malloc(outbufsz);
3501 if (!host_ifconf) {
3502 return -TARGET_ENOMEM;
3504 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3505 free_buf = 1;
3507 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3509 host_ifconf->ifc_len = host_ifc_len;
3510 host_ifconf->ifc_buf = host_ifc_buf;
3512 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3513 if (!is_error(ret)) {
3514 /* convert host ifc_len to target ifc_len */
3516 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3517 target_ifc_len = nb_ifreq * target_ifreq_size;
3518 host_ifconf->ifc_len = target_ifc_len;
3520 /* restore target ifc_buf */
3522 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3524 /* copy struct ifconf to target user */
3526 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3527 if (!argptr)
3528 return -TARGET_EFAULT;
3529 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3530 unlock_user(argptr, arg, target_size);
3532 /* copy ifreq[] to target user */
3534 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3535 for (i = 0; i < nb_ifreq ; i++) {
3536 thunk_convert(argptr + i * target_ifreq_size,
3537 host_ifc_buf + i * sizeof(struct ifreq),
3538 ifreq_arg_type, THUNK_TARGET);
3540 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3543 if (free_buf) {
3544 free(host_ifconf);
3547 return ret;
3550 static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3551 int cmd, abi_long arg)
3553 void *argptr;
3554 struct dm_ioctl *host_dm;
3555 abi_long guest_data;
3556 uint32_t guest_data_size;
3557 int target_size;
3558 const argtype *arg_type = ie->arg_type;
3559 abi_long ret;
3560 void *big_buf = NULL;
3561 char *host_data;
3563 arg_type++;
3564 target_size = thunk_type_size(arg_type, 0);
3565 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3566 if (!argptr) {
3567 ret = -TARGET_EFAULT;
3568 goto out;
3570 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3571 unlock_user(argptr, arg, 0);
3573 /* buf_temp is too small, so fetch things into a bigger buffer */
3574 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3575 memcpy(big_buf, buf_temp, target_size);
3576 buf_temp = big_buf;
3577 host_dm = big_buf;
3579 guest_data = arg + host_dm->data_start;
3580 if ((guest_data - arg) < 0) {
3581 ret = -EINVAL;
3582 goto out;
3584 guest_data_size = host_dm->data_size - host_dm->data_start;
3585 host_data = (char*)host_dm + host_dm->data_start;
3587 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3588 switch (ie->host_cmd) {
3589 case DM_REMOVE_ALL:
3590 case DM_LIST_DEVICES:
3591 case DM_DEV_CREATE:
3592 case DM_DEV_REMOVE:
3593 case DM_DEV_SUSPEND:
3594 case DM_DEV_STATUS:
3595 case DM_DEV_WAIT:
3596 case DM_TABLE_STATUS:
3597 case DM_TABLE_CLEAR:
3598 case DM_TABLE_DEPS:
3599 case DM_LIST_VERSIONS:
3600 /* no input data */
3601 break;
3602 case DM_DEV_RENAME:
3603 case DM_DEV_SET_GEOMETRY:
3604 /* data contains only strings */
3605 memcpy(host_data, argptr, guest_data_size);
3606 break;
3607 case DM_TARGET_MSG:
3608 memcpy(host_data, argptr, guest_data_size);
3609 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3610 break;
3611 case DM_TABLE_LOAD:
3613 void *gspec = argptr;
3614 void *cur_data = host_data;
3615 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3616 int spec_size = thunk_type_size(arg_type, 0);
3617 int i;
3619 for (i = 0; i < host_dm->target_count; i++) {
3620 struct dm_target_spec *spec = cur_data;
3621 uint32_t next;
3622 int slen;
3624 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3625 slen = strlen((char*)gspec + spec_size) + 1;
3626 next = spec->next;
3627 spec->next = sizeof(*spec) + slen;
3628 strcpy((char*)&spec[1], gspec + spec_size);
3629 gspec += next;
3630 cur_data += spec->next;
3632 break;
3634 default:
3635 ret = -TARGET_EINVAL;
3636 unlock_user(argptr, guest_data, 0);
3637 goto out;
3639 unlock_user(argptr, guest_data, 0);
3641 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3642 if (!is_error(ret)) {
3643 guest_data = arg + host_dm->data_start;
3644 guest_data_size = host_dm->data_size - host_dm->data_start;
3645 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3646 switch (ie->host_cmd) {
3647 case DM_REMOVE_ALL:
3648 case DM_DEV_CREATE:
3649 case DM_DEV_REMOVE:
3650 case DM_DEV_RENAME:
3651 case DM_DEV_SUSPEND:
3652 case DM_DEV_STATUS:
3653 case DM_TABLE_LOAD:
3654 case DM_TABLE_CLEAR:
3655 case DM_TARGET_MSG:
3656 case DM_DEV_SET_GEOMETRY:
3657 /* no return data */
3658 break;
3659 case DM_LIST_DEVICES:
3661 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3662 uint32_t remaining_data = guest_data_size;
3663 void *cur_data = argptr;
3664 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3665 int nl_size = 12; /* can't use thunk_size due to alignment */
3667 while (1) {
3668 uint32_t next = nl->next;
3669 if (next) {
3670 nl->next = nl_size + (strlen(nl->name) + 1);
3672 if (remaining_data < nl->next) {
3673 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3674 break;
3676 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3677 strcpy(cur_data + nl_size, nl->name);
3678 cur_data += nl->next;
3679 remaining_data -= nl->next;
3680 if (!next) {
3681 break;
3683 nl = (void*)nl + next;
3685 break;
3687 case DM_DEV_WAIT:
3688 case DM_TABLE_STATUS:
3690 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3691 void *cur_data = argptr;
3692 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3693 int spec_size = thunk_type_size(arg_type, 0);
3694 int i;
3696 for (i = 0; i < host_dm->target_count; i++) {
3697 uint32_t next = spec->next;
3698 int slen = strlen((char*)&spec[1]) + 1;
3699 spec->next = (cur_data - argptr) + spec_size + slen;
3700 if (guest_data_size < spec->next) {
3701 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3702 break;
3704 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3705 strcpy(cur_data + spec_size, (char*)&spec[1]);
3706 cur_data = argptr + spec->next;
3707 spec = (void*)host_dm + host_dm->data_start + next;
3709 break;
3711 case DM_TABLE_DEPS:
3713 void *hdata = (void*)host_dm + host_dm->data_start;
3714 int count = *(uint32_t*)hdata;
3715 uint64_t *hdev = hdata + 8;
3716 uint64_t *gdev = argptr + 8;
3717 int i;
3719 *(uint32_t*)argptr = tswap32(count);
3720 for (i = 0; i < count; i++) {
3721 *gdev = tswap64(*hdev);
3722 gdev++;
3723 hdev++;
3725 break;
3727 case DM_LIST_VERSIONS:
3729 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3730 uint32_t remaining_data = guest_data_size;
3731 void *cur_data = argptr;
3732 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3733 int vers_size = thunk_type_size(arg_type, 0);
3735 while (1) {
3736 uint32_t next = vers->next;
3737 if (next) {
3738 vers->next = vers_size + (strlen(vers->name) + 1);
3740 if (remaining_data < vers->next) {
3741 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3742 break;
3744 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3745 strcpy(cur_data + vers_size, vers->name);
3746 cur_data += vers->next;
3747 remaining_data -= vers->next;
3748 if (!next) {
3749 break;
3751 vers = (void*)vers + next;
3753 break;
3755 default:
3756 unlock_user(argptr, guest_data, 0);
3757 ret = -TARGET_EINVAL;
3758 goto out;
3760 unlock_user(argptr, guest_data, guest_data_size);
3762 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3763 if (!argptr) {
3764 ret = -TARGET_EFAULT;
3765 goto out;
3767 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3768 unlock_user(argptr, arg, target_size);
3770 out:
3771 g_free(big_buf);
3772 return ret;
3775 static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3776 int cmd, abi_long arg)
3778 void *argptr;
3779 int target_size;
3780 const argtype *arg_type = ie->arg_type;
3781 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
3782 abi_long ret;
3784 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
3785 struct blkpg_partition host_part;
3787 /* Read and convert blkpg */
3788 arg_type++;
3789 target_size = thunk_type_size(arg_type, 0);
3790 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3791 if (!argptr) {
3792 ret = -TARGET_EFAULT;
3793 goto out;
3795 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3796 unlock_user(argptr, arg, 0);
3798 switch (host_blkpg->op) {
3799 case BLKPG_ADD_PARTITION:
3800 case BLKPG_DEL_PARTITION:
3801 /* payload is struct blkpg_partition */
3802 break;
3803 default:
3804 /* Unknown opcode */
3805 ret = -TARGET_EINVAL;
3806 goto out;
3809 /* Read and convert blkpg->data */
3810 arg = (abi_long)(uintptr_t)host_blkpg->data;
3811 target_size = thunk_type_size(part_arg_type, 0);
3812 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3813 if (!argptr) {
3814 ret = -TARGET_EFAULT;
3815 goto out;
3817 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
3818 unlock_user(argptr, arg, 0);
3820 /* Swizzle the data pointer to our local copy and call! */
3821 host_blkpg->data = &host_part;
3822 ret = get_errno(ioctl(fd, ie->host_cmd, host_blkpg));
3824 out:
3825 return ret;
3828 static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
3829 int fd, int cmd, abi_long arg)
3831 const argtype *arg_type = ie->arg_type;
3832 const StructEntry *se;
3833 const argtype *field_types;
3834 const int *dst_offsets, *src_offsets;
3835 int target_size;
3836 void *argptr;
3837 abi_ulong *target_rt_dev_ptr;
3838 unsigned long *host_rt_dev_ptr;
3839 abi_long ret;
3840 int i;
3842 assert(ie->access == IOC_W);
3843 assert(*arg_type == TYPE_PTR);
3844 arg_type++;
3845 assert(*arg_type == TYPE_STRUCT);
3846 target_size = thunk_type_size(arg_type, 0);
3847 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3848 if (!argptr) {
3849 return -TARGET_EFAULT;
3851 arg_type++;
3852 assert(*arg_type == (int)STRUCT_rtentry);
3853 se = struct_entries + *arg_type++;
3854 assert(se->convert[0] == NULL);
3855 /* convert struct here to be able to catch rt_dev string */
3856 field_types = se->field_types;
3857 dst_offsets = se->field_offsets[THUNK_HOST];
3858 src_offsets = se->field_offsets[THUNK_TARGET];
3859 for (i = 0; i < se->nb_fields; i++) {
3860 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
3861 assert(*field_types == TYPE_PTRVOID);
3862 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
3863 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
3864 if (*target_rt_dev_ptr != 0) {
3865 *host_rt_dev_ptr = (unsigned long)lock_user_string(
3866 tswapal(*target_rt_dev_ptr));
3867 if (!*host_rt_dev_ptr) {
3868 unlock_user(argptr, arg, 0);
3869 return -TARGET_EFAULT;
3871 } else {
3872 *host_rt_dev_ptr = 0;
3874 field_types++;
3875 continue;
3877 field_types = thunk_convert(buf_temp + dst_offsets[i],
3878 argptr + src_offsets[i],
3879 field_types, THUNK_HOST);
3881 unlock_user(argptr, arg, 0);
3883 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3884 if (*host_rt_dev_ptr != 0) {
3885 unlock_user((void *)*host_rt_dev_ptr,
3886 *target_rt_dev_ptr, 0);
3888 return ret;
3891 static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
3892 int fd, int cmd, abi_long arg)
3894 int sig = target_to_host_signal(arg);
3895 return get_errno(ioctl(fd, ie->host_cmd, sig));
3898 static IOCTLEntry ioctl_entries[] = {
3899 #define IOCTL(cmd, access, ...) \
3900 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3901 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3902 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
3903 #include "ioctls.h"
3904 { 0, 0, },
3907 /* ??? Implement proper locking for ioctls. */
3908 /* do_ioctl() Must return target values and target errnos. */
3909 static abi_long do_ioctl(int fd, int cmd, abi_long arg)
3911 const IOCTLEntry *ie;
3912 const argtype *arg_type;
3913 abi_long ret;
3914 uint8_t buf_temp[MAX_STRUCT_SIZE];
3915 int target_size;
3916 void *argptr;
3918 ie = ioctl_entries;
3919 for(;;) {
3920 if (ie->target_cmd == 0) {
3921 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
3922 return -TARGET_ENOSYS;
3924 if (ie->target_cmd == cmd)
3925 break;
3926 ie++;
3928 arg_type = ie->arg_type;
3929 #if defined(DEBUG)
3930 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
3931 #endif
3932 if (ie->do_ioctl) {
3933 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3936 switch(arg_type[0]) {
3937 case TYPE_NULL:
3938 /* no argument */
3939 ret = get_errno(ioctl(fd, ie->host_cmd));
3940 break;
3941 case TYPE_PTRVOID:
3942 case TYPE_INT:
3943 /* int argment */
3944 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3945 break;
3946 case TYPE_PTR:
3947 arg_type++;
3948 target_size = thunk_type_size(arg_type, 0);
3949 switch(ie->access) {
3950 case IOC_R:
3951 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3952 if (!is_error(ret)) {
3953 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3954 if (!argptr)
3955 return -TARGET_EFAULT;
3956 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3957 unlock_user(argptr, arg, target_size);
3959 break;
3960 case IOC_W:
3961 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3962 if (!argptr)
3963 return -TARGET_EFAULT;
3964 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3965 unlock_user(argptr, arg, 0);
3966 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3967 break;
3968 default:
3969 case IOC_RW:
3970 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3971 if (!argptr)
3972 return -TARGET_EFAULT;
3973 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3974 unlock_user(argptr, arg, 0);
3975 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3976 if (!is_error(ret)) {
3977 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3978 if (!argptr)
3979 return -TARGET_EFAULT;
3980 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3981 unlock_user(argptr, arg, target_size);
3983 break;
3985 break;
3986 default:
3987 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3988 (long)cmd, arg_type[0]);
3989 ret = -TARGET_ENOSYS;
3990 break;
3992 return ret;
3995 static const bitmask_transtbl iflag_tbl[] = {
3996 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3997 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3998 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3999 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
4000 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
4001 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
4002 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
4003 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
4004 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
4005 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
4006 { TARGET_IXON, TARGET_IXON, IXON, IXON },
4007 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
4008 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
4009 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
4010 { 0, 0, 0, 0 }
4013 static const bitmask_transtbl oflag_tbl[] = {
4014 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
4015 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
4016 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
4017 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
4018 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
4019 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
4020 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
4021 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
4022 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
4023 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
4024 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
4025 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
4026 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
4027 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
4028 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
4029 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
4030 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
4031 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
4032 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
4033 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
4034 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
4035 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
4036 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
4037 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
4038 { 0, 0, 0, 0 }
4041 static const bitmask_transtbl cflag_tbl[] = {
4042 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
4043 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
4044 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
4045 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
4046 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
4047 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
4048 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
4049 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
4050 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
4051 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
4052 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
4053 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
4054 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
4055 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
4056 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
4057 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
4058 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
4059 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
4060 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
4061 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
4062 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
4063 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
4064 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
4065 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
4066 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
4067 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
4068 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
4069 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
4070 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
4071 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
4072 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
4073 { 0, 0, 0, 0 }
4076 static const bitmask_transtbl lflag_tbl[] = {
4077 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
4078 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
4079 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
4080 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
4081 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
4082 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
4083 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
4084 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
4085 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
4086 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
4087 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
4088 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
4089 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
4090 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
4091 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
4092 { 0, 0, 0, 0 }
4095 static void target_to_host_termios (void *dst, const void *src)
4097 struct host_termios *host = dst;
4098 const struct target_termios *target = src;
4100 host->c_iflag =
4101 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
4102 host->c_oflag =
4103 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
4104 host->c_cflag =
4105 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
4106 host->c_lflag =
4107 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
4108 host->c_line = target->c_line;
4110 memset(host->c_cc, 0, sizeof(host->c_cc));
4111 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
4112 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
4113 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
4114 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
4115 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
4116 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
4117 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
4118 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
4119 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
4120 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
4121 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
4122 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
4123 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
4124 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
4125 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
4126 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
4127 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
4130 static void host_to_target_termios (void *dst, const void *src)
4132 struct target_termios *target = dst;
4133 const struct host_termios *host = src;
4135 target->c_iflag =
4136 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
4137 target->c_oflag =
4138 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
4139 target->c_cflag =
4140 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
4141 target->c_lflag =
4142 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
4143 target->c_line = host->c_line;
4145 memset(target->c_cc, 0, sizeof(target->c_cc));
4146 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
4147 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
4148 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
4149 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
4150 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
4151 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
4152 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
4153 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
4154 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
4155 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
4156 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
4157 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
4158 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
4159 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
4160 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
4161 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
4162 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
4165 static const StructEntry struct_termios_def = {
4166 .convert = { host_to_target_termios, target_to_host_termios },
4167 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
4168 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
4171 static bitmask_transtbl mmap_flags_tbl[] = {
4172 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
4173 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
4174 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
4175 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
4176 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
4177 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
4178 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
4179 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
4180 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, MAP_NORESERVE,
4181 MAP_NORESERVE },
4182 { 0, 0, 0, 0 }
4185 #if defined(TARGET_I386)
4187 /* NOTE: there is really one LDT for all the threads */
4188 static uint8_t *ldt_table;
4190 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
4192 int size;
4193 void *p;
4195 if (!ldt_table)
4196 return 0;
4197 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
4198 if (size > bytecount)
4199 size = bytecount;
4200 p = lock_user(VERIFY_WRITE, ptr, size, 0);
4201 if (!p)
4202 return -TARGET_EFAULT;
4203 /* ??? Should this by byteswapped? */
4204 memcpy(p, ldt_table, size);
4205 unlock_user(p, ptr, size);
4206 return size;
4209 /* XXX: add locking support */
4210 static abi_long write_ldt(CPUX86State *env,
4211 abi_ulong ptr, unsigned long bytecount, int oldmode)
4213 struct target_modify_ldt_ldt_s ldt_info;
4214 struct target_modify_ldt_ldt_s *target_ldt_info;
4215 int seg_32bit, contents, read_exec_only, limit_in_pages;
4216 int seg_not_present, useable, lm;
4217 uint32_t *lp, entry_1, entry_2;
4219 if (bytecount != sizeof(ldt_info))
4220 return -TARGET_EINVAL;
4221 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
4222 return -TARGET_EFAULT;
4223 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
4224 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
4225 ldt_info.limit = tswap32(target_ldt_info->limit);
4226 ldt_info.flags = tswap32(target_ldt_info->flags);
4227 unlock_user_struct(target_ldt_info, ptr, 0);
4229 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
4230 return -TARGET_EINVAL;
4231 seg_32bit = ldt_info.flags & 1;
4232 contents = (ldt_info.flags >> 1) & 3;
4233 read_exec_only = (ldt_info.flags >> 3) & 1;
4234 limit_in_pages = (ldt_info.flags >> 4) & 1;
4235 seg_not_present = (ldt_info.flags >> 5) & 1;
4236 useable = (ldt_info.flags >> 6) & 1;
4237 #ifdef TARGET_ABI32
4238 lm = 0;
4239 #else
4240 lm = (ldt_info.flags >> 7) & 1;
4241 #endif
4242 if (contents == 3) {
4243 if (oldmode)
4244 return -TARGET_EINVAL;
4245 if (seg_not_present == 0)
4246 return -TARGET_EINVAL;
4248 /* allocate the LDT */
4249 if (!ldt_table) {
4250 env->ldt.base = target_mmap(0,
4251 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
4252 PROT_READ|PROT_WRITE,
4253 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
4254 if (env->ldt.base == -1)
4255 return -TARGET_ENOMEM;
4256 memset(g2h(env->ldt.base), 0,
4257 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
4258 env->ldt.limit = 0xffff;
4259 ldt_table = g2h(env->ldt.base);
4262 /* NOTE: same code as Linux kernel */
4263 /* Allow LDTs to be cleared by the user. */
4264 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4265 if (oldmode ||
4266 (contents == 0 &&
4267 read_exec_only == 1 &&
4268 seg_32bit == 0 &&
4269 limit_in_pages == 0 &&
4270 seg_not_present == 1 &&
4271 useable == 0 )) {
4272 entry_1 = 0;
4273 entry_2 = 0;
4274 goto install;
4278 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4279 (ldt_info.limit & 0x0ffff);
4280 entry_2 = (ldt_info.base_addr & 0xff000000) |
4281 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4282 (ldt_info.limit & 0xf0000) |
4283 ((read_exec_only ^ 1) << 9) |
4284 (contents << 10) |
4285 ((seg_not_present ^ 1) << 15) |
4286 (seg_32bit << 22) |
4287 (limit_in_pages << 23) |
4288 (lm << 21) |
4289 0x7000;
4290 if (!oldmode)
4291 entry_2 |= (useable << 20);
4293 /* Install the new entry ... */
4294 install:
4295 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4296 lp[0] = tswap32(entry_1);
4297 lp[1] = tswap32(entry_2);
4298 return 0;
4301 /* specific and weird i386 syscalls */
4302 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4303 unsigned long bytecount)
4305 abi_long ret;
4307 switch (func) {
4308 case 0:
4309 ret = read_ldt(ptr, bytecount);
4310 break;
4311 case 1:
4312 ret = write_ldt(env, ptr, bytecount, 1);
4313 break;
4314 case 0x11:
4315 ret = write_ldt(env, ptr, bytecount, 0);
4316 break;
4317 default:
4318 ret = -TARGET_ENOSYS;
4319 break;
4321 return ret;
4324 #if defined(TARGET_I386) && defined(TARGET_ABI32)
4325 abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
4327 uint64_t *gdt_table = g2h(env->gdt.base);
4328 struct target_modify_ldt_ldt_s ldt_info;
4329 struct target_modify_ldt_ldt_s *target_ldt_info;
4330 int seg_32bit, contents, read_exec_only, limit_in_pages;
4331 int seg_not_present, useable, lm;
4332 uint32_t *lp, entry_1, entry_2;
4333 int i;
4335 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4336 if (!target_ldt_info)
4337 return -TARGET_EFAULT;
4338 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
4339 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
4340 ldt_info.limit = tswap32(target_ldt_info->limit);
4341 ldt_info.flags = tswap32(target_ldt_info->flags);
4342 if (ldt_info.entry_number == -1) {
4343 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4344 if (gdt_table[i] == 0) {
4345 ldt_info.entry_number = i;
4346 target_ldt_info->entry_number = tswap32(i);
4347 break;
4351 unlock_user_struct(target_ldt_info, ptr, 1);
4353 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4354 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4355 return -TARGET_EINVAL;
4356 seg_32bit = ldt_info.flags & 1;
4357 contents = (ldt_info.flags >> 1) & 3;
4358 read_exec_only = (ldt_info.flags >> 3) & 1;
4359 limit_in_pages = (ldt_info.flags >> 4) & 1;
4360 seg_not_present = (ldt_info.flags >> 5) & 1;
4361 useable = (ldt_info.flags >> 6) & 1;
4362 #ifdef TARGET_ABI32
4363 lm = 0;
4364 #else
4365 lm = (ldt_info.flags >> 7) & 1;
4366 #endif
4368 if (contents == 3) {
4369 if (seg_not_present == 0)
4370 return -TARGET_EINVAL;
4373 /* NOTE: same code as Linux kernel */
4374 /* Allow LDTs to be cleared by the user. */
4375 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4376 if ((contents == 0 &&
4377 read_exec_only == 1 &&
4378 seg_32bit == 0 &&
4379 limit_in_pages == 0 &&
4380 seg_not_present == 1 &&
4381 useable == 0 )) {
4382 entry_1 = 0;
4383 entry_2 = 0;
4384 goto install;
4388 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4389 (ldt_info.limit & 0x0ffff);
4390 entry_2 = (ldt_info.base_addr & 0xff000000) |
4391 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4392 (ldt_info.limit & 0xf0000) |
4393 ((read_exec_only ^ 1) << 9) |
4394 (contents << 10) |
4395 ((seg_not_present ^ 1) << 15) |
4396 (seg_32bit << 22) |
4397 (limit_in_pages << 23) |
4398 (useable << 20) |
4399 (lm << 21) |
4400 0x7000;
4402 /* Install the new entry ... */
4403 install:
4404 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4405 lp[0] = tswap32(entry_1);
4406 lp[1] = tswap32(entry_2);
4407 return 0;
4410 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
4412 struct target_modify_ldt_ldt_s *target_ldt_info;
4413 uint64_t *gdt_table = g2h(env->gdt.base);
4414 uint32_t base_addr, limit, flags;
4415 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4416 int seg_not_present, useable, lm;
4417 uint32_t *lp, entry_1, entry_2;
4419 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4420 if (!target_ldt_info)
4421 return -TARGET_EFAULT;
4422 idx = tswap32(target_ldt_info->entry_number);
4423 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4424 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4425 unlock_user_struct(target_ldt_info, ptr, 1);
4426 return -TARGET_EINVAL;
4428 lp = (uint32_t *)(gdt_table + idx);
4429 entry_1 = tswap32(lp[0]);
4430 entry_2 = tswap32(lp[1]);
4432 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4433 contents = (entry_2 >> 10) & 3;
4434 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4435 seg_32bit = (entry_2 >> 22) & 1;
4436 limit_in_pages = (entry_2 >> 23) & 1;
4437 useable = (entry_2 >> 20) & 1;
4438 #ifdef TARGET_ABI32
4439 lm = 0;
4440 #else
4441 lm = (entry_2 >> 21) & 1;
4442 #endif
4443 flags = (seg_32bit << 0) | (contents << 1) |
4444 (read_exec_only << 3) | (limit_in_pages << 4) |
4445 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4446 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4447 base_addr = (entry_1 >> 16) |
4448 (entry_2 & 0xff000000) |
4449 ((entry_2 & 0xff) << 16);
4450 target_ldt_info->base_addr = tswapal(base_addr);
4451 target_ldt_info->limit = tswap32(limit);
4452 target_ldt_info->flags = tswap32(flags);
4453 unlock_user_struct(target_ldt_info, ptr, 1);
4454 return 0;
4456 #endif /* TARGET_I386 && TARGET_ABI32 */
4458 #ifndef TARGET_ABI32
4459 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
4461 abi_long ret = 0;
4462 abi_ulong val;
4463 int idx;
4465 switch(code) {
4466 case TARGET_ARCH_SET_GS:
4467 case TARGET_ARCH_SET_FS:
4468 if (code == TARGET_ARCH_SET_GS)
4469 idx = R_GS;
4470 else
4471 idx = R_FS;
4472 cpu_x86_load_seg(env, idx, 0);
4473 env->segs[idx].base = addr;
4474 break;
4475 case TARGET_ARCH_GET_GS:
4476 case TARGET_ARCH_GET_FS:
4477 if (code == TARGET_ARCH_GET_GS)
4478 idx = R_GS;
4479 else
4480 idx = R_FS;
4481 val = env->segs[idx].base;
4482 if (put_user(val, addr, abi_ulong))
4483 ret = -TARGET_EFAULT;
4484 break;
4485 default:
4486 ret = -TARGET_EINVAL;
4487 break;
4489 return ret;
4491 #endif
4493 #endif /* defined(TARGET_I386) */
4495 #define NEW_STACK_SIZE 0x40000
4498 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4499 typedef struct {
4500 CPUArchState *env;
4501 pthread_mutex_t mutex;
4502 pthread_cond_t cond;
4503 pthread_t thread;
4504 uint32_t tid;
4505 abi_ulong child_tidptr;
4506 abi_ulong parent_tidptr;
4507 sigset_t sigmask;
4508 } new_thread_info;
4510 static void * QEMU_NORETURN clone_func(void *arg)
4512 new_thread_info *info = arg;
4513 CPUArchState *env;
4514 CPUState *cpu;
4515 TaskState *ts;
4517 env = info->env;
4518 cpu = ENV_GET_CPU(env);
4519 thread_cpu = cpu;
4520 ts = (TaskState *)cpu->opaque;
4521 info->tid = gettid();
4522 cpu->host_tid = info->tid;
4523 task_settid(ts);
4524 if (info->child_tidptr)
4525 put_user_u32(info->tid, info->child_tidptr);
4526 if (info->parent_tidptr)
4527 put_user_u32(info->tid, info->parent_tidptr);
4528 /* Enable signals. */
4529 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4530 /* Signal to the parent that we're ready. */
4531 pthread_mutex_lock(&info->mutex);
4532 pthread_cond_broadcast(&info->cond);
4533 pthread_mutex_unlock(&info->mutex);
4534 /* Wait until the parent has finshed initializing the tls state. */
4535 pthread_mutex_lock(&clone_lock);
4536 pthread_mutex_unlock(&clone_lock);
4537 cpu_loop(env);
4538 /* never exits */
4541 /* do_fork() Must return host values and target errnos (unlike most
4542 do_*() functions). */
4543 static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
4544 abi_ulong parent_tidptr, target_ulong newtls,
4545 abi_ulong child_tidptr)
4547 CPUState *cpu = ENV_GET_CPU(env);
4548 int ret;
4549 TaskState *ts;
4550 CPUState *new_cpu;
4551 CPUArchState *new_env;
4552 unsigned int nptl_flags;
4553 sigset_t sigmask;
4555 /* Emulate vfork() with fork() */
4556 if (flags & CLONE_VFORK)
4557 flags &= ~(CLONE_VFORK | CLONE_VM);
4559 if (flags & CLONE_VM) {
4560 TaskState *parent_ts = (TaskState *)cpu->opaque;
4561 new_thread_info info;
4562 pthread_attr_t attr;
4564 ts = g_malloc0(sizeof(TaskState));
4565 init_task_state(ts);
4566 /* we create a new CPU instance. */
4567 new_env = cpu_copy(env);
4568 /* Init regs that differ from the parent. */
4569 cpu_clone_regs(new_env, newsp);
4570 new_cpu = ENV_GET_CPU(new_env);
4571 new_cpu->opaque = ts;
4572 ts->bprm = parent_ts->bprm;
4573 ts->info = parent_ts->info;
4574 nptl_flags = flags;
4575 flags &= ~CLONE_NPTL_FLAGS2;
4577 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4578 ts->child_tidptr = child_tidptr;
4581 if (nptl_flags & CLONE_SETTLS)
4582 cpu_set_tls (new_env, newtls);
4584 /* Grab a mutex so that thread setup appears atomic. */
4585 pthread_mutex_lock(&clone_lock);
4587 memset(&info, 0, sizeof(info));
4588 pthread_mutex_init(&info.mutex, NULL);
4589 pthread_mutex_lock(&info.mutex);
4590 pthread_cond_init(&info.cond, NULL);
4591 info.env = new_env;
4592 if (nptl_flags & CLONE_CHILD_SETTID)
4593 info.child_tidptr = child_tidptr;
4594 if (nptl_flags & CLONE_PARENT_SETTID)
4595 info.parent_tidptr = parent_tidptr;
4597 ret = pthread_attr_init(&attr);
4598 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4599 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
4600 /* It is not safe to deliver signals until the child has finished
4601 initializing, so temporarily block all signals. */
4602 sigfillset(&sigmask);
4603 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4605 ret = pthread_create(&info.thread, &attr, clone_func, &info);
4606 /* TODO: Free new CPU state if thread creation failed. */
4608 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4609 pthread_attr_destroy(&attr);
4610 if (ret == 0) {
4611 /* Wait for the child to initialize. */
4612 pthread_cond_wait(&info.cond, &info.mutex);
4613 ret = info.tid;
4614 if (flags & CLONE_PARENT_SETTID)
4615 put_user_u32(ret, parent_tidptr);
4616 } else {
4617 ret = -1;
4619 pthread_mutex_unlock(&info.mutex);
4620 pthread_cond_destroy(&info.cond);
4621 pthread_mutex_destroy(&info.mutex);
4622 pthread_mutex_unlock(&clone_lock);
4623 } else {
4624 /* if no CLONE_VM, we consider it is a fork */
4625 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
4626 return -EINVAL;
4627 fork_start();
4628 ret = fork();
4629 if (ret == 0) {
4630 /* Child Process. */
4631 rcu_after_fork();
4632 cpu_clone_regs(env, newsp);
4633 fork_end(1);
4634 /* There is a race condition here. The parent process could
4635 theoretically read the TID in the child process before the child
4636 tid is set. This would require using either ptrace
4637 (not implemented) or having *_tidptr to point at a shared memory
4638 mapping. We can't repeat the spinlock hack used above because
4639 the child process gets its own copy of the lock. */
4640 if (flags & CLONE_CHILD_SETTID)
4641 put_user_u32(gettid(), child_tidptr);
4642 if (flags & CLONE_PARENT_SETTID)
4643 put_user_u32(gettid(), parent_tidptr);
4644 ts = (TaskState *)cpu->opaque;
4645 if (flags & CLONE_SETTLS)
4646 cpu_set_tls (env, newtls);
4647 if (flags & CLONE_CHILD_CLEARTID)
4648 ts->child_tidptr = child_tidptr;
4649 } else {
4650 fork_end(0);
4653 return ret;
4656 /* warning : doesn't handle linux specific flags... */
4657 static int target_to_host_fcntl_cmd(int cmd)
4659 switch(cmd) {
4660 case TARGET_F_DUPFD:
4661 case TARGET_F_GETFD:
4662 case TARGET_F_SETFD:
4663 case TARGET_F_GETFL:
4664 case TARGET_F_SETFL:
4665 return cmd;
4666 case TARGET_F_GETLK:
4667 return F_GETLK;
4668 case TARGET_F_SETLK:
4669 return F_SETLK;
4670 case TARGET_F_SETLKW:
4671 return F_SETLKW;
4672 case TARGET_F_GETOWN:
4673 return F_GETOWN;
4674 case TARGET_F_SETOWN:
4675 return F_SETOWN;
4676 case TARGET_F_GETSIG:
4677 return F_GETSIG;
4678 case TARGET_F_SETSIG:
4679 return F_SETSIG;
4680 #if TARGET_ABI_BITS == 32
4681 case TARGET_F_GETLK64:
4682 return F_GETLK64;
4683 case TARGET_F_SETLK64:
4684 return F_SETLK64;
4685 case TARGET_F_SETLKW64:
4686 return F_SETLKW64;
4687 #endif
4688 case TARGET_F_SETLEASE:
4689 return F_SETLEASE;
4690 case TARGET_F_GETLEASE:
4691 return F_GETLEASE;
4692 #ifdef F_DUPFD_CLOEXEC
4693 case TARGET_F_DUPFD_CLOEXEC:
4694 return F_DUPFD_CLOEXEC;
4695 #endif
4696 case TARGET_F_NOTIFY:
4697 return F_NOTIFY;
4698 #ifdef F_GETOWN_EX
4699 case TARGET_F_GETOWN_EX:
4700 return F_GETOWN_EX;
4701 #endif
4702 #ifdef F_SETOWN_EX
4703 case TARGET_F_SETOWN_EX:
4704 return F_SETOWN_EX;
4705 #endif
4706 default:
4707 return -TARGET_EINVAL;
4709 return -TARGET_EINVAL;
4712 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4713 static const bitmask_transtbl flock_tbl[] = {
4714 TRANSTBL_CONVERT(F_RDLCK),
4715 TRANSTBL_CONVERT(F_WRLCK),
4716 TRANSTBL_CONVERT(F_UNLCK),
4717 TRANSTBL_CONVERT(F_EXLCK),
4718 TRANSTBL_CONVERT(F_SHLCK),
4719 { 0, 0, 0, 0 }
4722 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
4724 struct flock fl;
4725 struct target_flock *target_fl;
4726 struct flock64 fl64;
4727 struct target_flock64 *target_fl64;
4728 #ifdef F_GETOWN_EX
4729 struct f_owner_ex fox;
4730 struct target_f_owner_ex *target_fox;
4731 #endif
4732 abi_long ret;
4733 int host_cmd = target_to_host_fcntl_cmd(cmd);
4735 if (host_cmd == -TARGET_EINVAL)
4736 return host_cmd;
4738 switch(cmd) {
4739 case TARGET_F_GETLK:
4740 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4741 return -TARGET_EFAULT;
4742 fl.l_type =
4743 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
4744 fl.l_whence = tswap16(target_fl->l_whence);
4745 fl.l_start = tswapal(target_fl->l_start);
4746 fl.l_len = tswapal(target_fl->l_len);
4747 fl.l_pid = tswap32(target_fl->l_pid);
4748 unlock_user_struct(target_fl, arg, 0);
4749 ret = get_errno(fcntl(fd, host_cmd, &fl));
4750 if (ret == 0) {
4751 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4752 return -TARGET_EFAULT;
4753 target_fl->l_type =
4754 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
4755 target_fl->l_whence = tswap16(fl.l_whence);
4756 target_fl->l_start = tswapal(fl.l_start);
4757 target_fl->l_len = tswapal(fl.l_len);
4758 target_fl->l_pid = tswap32(fl.l_pid);
4759 unlock_user_struct(target_fl, arg, 1);
4761 break;
4763 case TARGET_F_SETLK:
4764 case TARGET_F_SETLKW:
4765 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4766 return -TARGET_EFAULT;
4767 fl.l_type =
4768 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
4769 fl.l_whence = tswap16(target_fl->l_whence);
4770 fl.l_start = tswapal(target_fl->l_start);
4771 fl.l_len = tswapal(target_fl->l_len);
4772 fl.l_pid = tswap32(target_fl->l_pid);
4773 unlock_user_struct(target_fl, arg, 0);
4774 ret = get_errno(fcntl(fd, host_cmd, &fl));
4775 break;
4777 case TARGET_F_GETLK64:
4778 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4779 return -TARGET_EFAULT;
4780 fl64.l_type =
4781 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
4782 fl64.l_whence = tswap16(target_fl64->l_whence);
4783 fl64.l_start = tswap64(target_fl64->l_start);
4784 fl64.l_len = tswap64(target_fl64->l_len);
4785 fl64.l_pid = tswap32(target_fl64->l_pid);
4786 unlock_user_struct(target_fl64, arg, 0);
4787 ret = get_errno(fcntl(fd, host_cmd, &fl64));
4788 if (ret == 0) {
4789 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4790 return -TARGET_EFAULT;
4791 target_fl64->l_type =
4792 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
4793 target_fl64->l_whence = tswap16(fl64.l_whence);
4794 target_fl64->l_start = tswap64(fl64.l_start);
4795 target_fl64->l_len = tswap64(fl64.l_len);
4796 target_fl64->l_pid = tswap32(fl64.l_pid);
4797 unlock_user_struct(target_fl64, arg, 1);
4799 break;
4800 case TARGET_F_SETLK64:
4801 case TARGET_F_SETLKW64:
4802 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4803 return -TARGET_EFAULT;
4804 fl64.l_type =
4805 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
4806 fl64.l_whence = tswap16(target_fl64->l_whence);
4807 fl64.l_start = tswap64(target_fl64->l_start);
4808 fl64.l_len = tswap64(target_fl64->l_len);
4809 fl64.l_pid = tswap32(target_fl64->l_pid);
4810 unlock_user_struct(target_fl64, arg, 0);
4811 ret = get_errno(fcntl(fd, host_cmd, &fl64));
4812 break;
4814 case TARGET_F_GETFL:
4815 ret = get_errno(fcntl(fd, host_cmd, arg));
4816 if (ret >= 0) {
4817 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4819 break;
4821 case TARGET_F_SETFL:
4822 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4823 break;
4825 #ifdef F_GETOWN_EX
4826 case TARGET_F_GETOWN_EX:
4827 ret = get_errno(fcntl(fd, host_cmd, &fox));
4828 if (ret >= 0) {
4829 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
4830 return -TARGET_EFAULT;
4831 target_fox->type = tswap32(fox.type);
4832 target_fox->pid = tswap32(fox.pid);
4833 unlock_user_struct(target_fox, arg, 1);
4835 break;
4836 #endif
4838 #ifdef F_SETOWN_EX
4839 case TARGET_F_SETOWN_EX:
4840 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
4841 return -TARGET_EFAULT;
4842 fox.type = tswap32(target_fox->type);
4843 fox.pid = tswap32(target_fox->pid);
4844 unlock_user_struct(target_fox, arg, 0);
4845 ret = get_errno(fcntl(fd, host_cmd, &fox));
4846 break;
4847 #endif
4849 case TARGET_F_SETOWN:
4850 case TARGET_F_GETOWN:
4851 case TARGET_F_SETSIG:
4852 case TARGET_F_GETSIG:
4853 case TARGET_F_SETLEASE:
4854 case TARGET_F_GETLEASE:
4855 ret = get_errno(fcntl(fd, host_cmd, arg));
4856 break;
4858 default:
4859 ret = get_errno(fcntl(fd, cmd, arg));
4860 break;
4862 return ret;
4865 #ifdef USE_UID16
4867 static inline int high2lowuid(int uid)
4869 if (uid > 65535)
4870 return 65534;
4871 else
4872 return uid;
4875 static inline int high2lowgid(int gid)
4877 if (gid > 65535)
4878 return 65534;
4879 else
4880 return gid;
4883 static inline int low2highuid(int uid)
4885 if ((int16_t)uid == -1)
4886 return -1;
4887 else
4888 return uid;
4891 static inline int low2highgid(int gid)
4893 if ((int16_t)gid == -1)
4894 return -1;
4895 else
4896 return gid;
4898 static inline int tswapid(int id)
4900 return tswap16(id);
4903 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
4905 #else /* !USE_UID16 */
4906 static inline int high2lowuid(int uid)
4908 return uid;
4910 static inline int high2lowgid(int gid)
4912 return gid;
4914 static inline int low2highuid(int uid)
4916 return uid;
4918 static inline int low2highgid(int gid)
4920 return gid;
4922 static inline int tswapid(int id)
4924 return tswap32(id);
4927 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
4929 #endif /* USE_UID16 */
4931 void syscall_init(void)
4933 IOCTLEntry *ie;
4934 const argtype *arg_type;
4935 int size;
4936 int i;
4938 thunk_init(STRUCT_MAX);
4940 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
4941 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
4942 #include "syscall_types.h"
4943 #undef STRUCT
4944 #undef STRUCT_SPECIAL
4946 /* Build target_to_host_errno_table[] table from
4947 * host_to_target_errno_table[]. */
4948 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
4949 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4952 /* we patch the ioctl size if necessary. We rely on the fact that
4953 no ioctl has all the bits at '1' in the size field */
4954 ie = ioctl_entries;
4955 while (ie->target_cmd != 0) {
4956 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4957 TARGET_IOC_SIZEMASK) {
4958 arg_type = ie->arg_type;
4959 if (arg_type[0] != TYPE_PTR) {
4960 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
4961 ie->target_cmd);
4962 exit(1);
4964 arg_type++;
4965 size = thunk_type_size(arg_type, 0);
4966 ie->target_cmd = (ie->target_cmd &
4967 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4968 (size << TARGET_IOC_SIZESHIFT);
4971 /* automatic consistency check if same arch */
4972 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4973 (defined(__x86_64__) && defined(TARGET_X86_64))
4974 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4975 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4976 ie->name, ie->target_cmd, ie->host_cmd);
4978 #endif
4979 ie++;
4983 #if TARGET_ABI_BITS == 32
4984 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4986 #ifdef TARGET_WORDS_BIGENDIAN
4987 return ((uint64_t)word0 << 32) | word1;
4988 #else
4989 return ((uint64_t)word1 << 32) | word0;
4990 #endif
4992 #else /* TARGET_ABI_BITS == 32 */
4993 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4995 return word0;
4997 #endif /* TARGET_ABI_BITS != 32 */
4999 #ifdef TARGET_NR_truncate64
5000 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
5001 abi_long arg2,
5002 abi_long arg3,
5003 abi_long arg4)
5005 if (regpairs_aligned(cpu_env)) {
5006 arg2 = arg3;
5007 arg3 = arg4;
5009 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
5011 #endif
5013 #ifdef TARGET_NR_ftruncate64
5014 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
5015 abi_long arg2,
5016 abi_long arg3,
5017 abi_long arg4)
5019 if (regpairs_aligned(cpu_env)) {
5020 arg2 = arg3;
5021 arg3 = arg4;
5023 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
5025 #endif
5027 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
5028 abi_ulong target_addr)
5030 struct target_timespec *target_ts;
5032 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
5033 return -TARGET_EFAULT;
5034 host_ts->tv_sec = tswapal(target_ts->tv_sec);
5035 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
5036 unlock_user_struct(target_ts, target_addr, 0);
5037 return 0;
5040 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
5041 struct timespec *host_ts)
5043 struct target_timespec *target_ts;
5045 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
5046 return -TARGET_EFAULT;
5047 target_ts->tv_sec = tswapal(host_ts->tv_sec);
5048 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
5049 unlock_user_struct(target_ts, target_addr, 1);
5050 return 0;
5053 static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
5054 abi_ulong target_addr)
5056 struct target_itimerspec *target_itspec;
5058 if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
5059 return -TARGET_EFAULT;
5062 host_itspec->it_interval.tv_sec =
5063 tswapal(target_itspec->it_interval.tv_sec);
5064 host_itspec->it_interval.tv_nsec =
5065 tswapal(target_itspec->it_interval.tv_nsec);
5066 host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
5067 host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
5069 unlock_user_struct(target_itspec, target_addr, 1);
5070 return 0;
5073 static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
5074 struct itimerspec *host_its)
5076 struct target_itimerspec *target_itspec;
5078 if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
5079 return -TARGET_EFAULT;
5082 target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
5083 target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
5085 target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
5086 target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
5088 unlock_user_struct(target_itspec, target_addr, 0);
5089 return 0;
5092 static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
5093 abi_ulong target_addr)
5095 struct target_sigevent *target_sevp;
5097 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
5098 return -TARGET_EFAULT;
5101 /* This union is awkward on 64 bit systems because it has a 32 bit
5102 * integer and a pointer in it; we follow the conversion approach
5103 * used for handling sigval types in signal.c so the guest should get
5104 * the correct value back even if we did a 64 bit byteswap and it's
5105 * using the 32 bit integer.
5107 host_sevp->sigev_value.sival_ptr =
5108 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
5109 host_sevp->sigev_signo =
5110 target_to_host_signal(tswap32(target_sevp->sigev_signo));
5111 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
5112 host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
5114 unlock_user_struct(target_sevp, target_addr, 1);
5115 return 0;
5118 #if defined(TARGET_NR_mlockall)
5119 static inline int target_to_host_mlockall_arg(int arg)
5121 int result = 0;
5123 if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
5124 result |= MCL_CURRENT;
5126 if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
5127 result |= MCL_FUTURE;
5129 return result;
5131 #endif
5133 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
5134 static inline abi_long host_to_target_stat64(void *cpu_env,
5135 abi_ulong target_addr,
5136 struct stat *host_st)
5138 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
5139 if (((CPUARMState *)cpu_env)->eabi) {
5140 struct target_eabi_stat64 *target_st;
5142 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
5143 return -TARGET_EFAULT;
5144 memset(target_st, 0, sizeof(struct target_eabi_stat64));
5145 __put_user(host_st->st_dev, &target_st->st_dev);
5146 __put_user(host_st->st_ino, &target_st->st_ino);
5147 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5148 __put_user(host_st->st_ino, &target_st->__st_ino);
5149 #endif
5150 __put_user(host_st->st_mode, &target_st->st_mode);
5151 __put_user(host_st->st_nlink, &target_st->st_nlink);
5152 __put_user(host_st->st_uid, &target_st->st_uid);
5153 __put_user(host_st->st_gid, &target_st->st_gid);
5154 __put_user(host_st->st_rdev, &target_st->st_rdev);
5155 __put_user(host_st->st_size, &target_st->st_size);
5156 __put_user(host_st->st_blksize, &target_st->st_blksize);
5157 __put_user(host_st->st_blocks, &target_st->st_blocks);
5158 __put_user(host_st->st_atime, &target_st->target_st_atime);
5159 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
5160 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
5161 unlock_user_struct(target_st, target_addr, 1);
5162 } else
5163 #endif
5165 #if defined(TARGET_HAS_STRUCT_STAT64)
5166 struct target_stat64 *target_st;
5167 #else
5168 struct target_stat *target_st;
5169 #endif
5171 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
5172 return -TARGET_EFAULT;
5173 memset(target_st, 0, sizeof(*target_st));
5174 __put_user(host_st->st_dev, &target_st->st_dev);
5175 __put_user(host_st->st_ino, &target_st->st_ino);
5176 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5177 __put_user(host_st->st_ino, &target_st->__st_ino);
5178 #endif
5179 __put_user(host_st->st_mode, &target_st->st_mode);
5180 __put_user(host_st->st_nlink, &target_st->st_nlink);
5181 __put_user(host_st->st_uid, &target_st->st_uid);
5182 __put_user(host_st->st_gid, &target_st->st_gid);
5183 __put_user(host_st->st_rdev, &target_st->st_rdev);
5184 /* XXX: better use of kernel struct */
5185 __put_user(host_st->st_size, &target_st->st_size);
5186 __put_user(host_st->st_blksize, &target_st->st_blksize);
5187 __put_user(host_st->st_blocks, &target_st->st_blocks);
5188 __put_user(host_st->st_atime, &target_st->target_st_atime);
5189 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
5190 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
5191 unlock_user_struct(target_st, target_addr, 1);
5194 return 0;
5196 #endif
5198 /* ??? Using host futex calls even when target atomic operations
5199 are not really atomic probably breaks things. However implementing
5200 futexes locally would make futexes shared between multiple processes
5201 tricky. However they're probably useless because guest atomic
5202 operations won't work either. */
5203 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
5204 target_ulong uaddr2, int val3)
5206 struct timespec ts, *pts;
5207 int base_op;
5209 /* ??? We assume FUTEX_* constants are the same on both host
5210 and target. */
5211 #ifdef FUTEX_CMD_MASK
5212 base_op = op & FUTEX_CMD_MASK;
5213 #else
5214 base_op = op;
5215 #endif
5216 switch (base_op) {
5217 case FUTEX_WAIT:
5218 case FUTEX_WAIT_BITSET:
5219 if (timeout) {
5220 pts = &ts;
5221 target_to_host_timespec(pts, timeout);
5222 } else {
5223 pts = NULL;
5225 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
5226 pts, NULL, val3));
5227 case FUTEX_WAKE:
5228 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
5229 case FUTEX_FD:
5230 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
5231 case FUTEX_REQUEUE:
5232 case FUTEX_CMP_REQUEUE:
5233 case FUTEX_WAKE_OP:
5234 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
5235 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
5236 But the prototype takes a `struct timespec *'; insert casts
5237 to satisfy the compiler. We do not need to tswap TIMEOUT
5238 since it's not compared to guest memory. */
5239 pts = (struct timespec *)(uintptr_t) timeout;
5240 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
5241 g2h(uaddr2),
5242 (base_op == FUTEX_CMP_REQUEUE
5243 ? tswap32(val3)
5244 : val3)));
5245 default:
5246 return -TARGET_ENOSYS;
5250 /* Map host to target signal numbers for the wait family of syscalls.
5251 Assume all other status bits are the same. */
5252 int host_to_target_waitstatus(int status)
5254 if (WIFSIGNALED(status)) {
5255 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
5257 if (WIFSTOPPED(status)) {
5258 return (host_to_target_signal(WSTOPSIG(status)) << 8)
5259 | (status & 0xff);
5261 return status;
5264 static int open_self_cmdline(void *cpu_env, int fd)
5266 int fd_orig = -1;
5267 bool word_skipped = false;
5269 fd_orig = open("/proc/self/cmdline", O_RDONLY);
5270 if (fd_orig < 0) {
5271 return fd_orig;
5274 while (true) {
5275 ssize_t nb_read;
5276 char buf[128];
5277 char *cp_buf = buf;
5279 nb_read = read(fd_orig, buf, sizeof(buf));
5280 if (nb_read < 0) {
5281 fd_orig = close(fd_orig);
5282 return -1;
5283 } else if (nb_read == 0) {
5284 break;
5287 if (!word_skipped) {
5288 /* Skip the first string, which is the path to qemu-*-static
5289 instead of the actual command. */
5290 cp_buf = memchr(buf, 0, sizeof(buf));
5291 if (cp_buf) {
5292 /* Null byte found, skip one string */
5293 cp_buf++;
5294 nb_read -= cp_buf - buf;
5295 word_skipped = true;
5299 if (word_skipped) {
5300 if (write(fd, cp_buf, nb_read) != nb_read) {
5301 close(fd_orig);
5302 return -1;
5307 return close(fd_orig);
5310 static int open_self_maps(void *cpu_env, int fd)
5312 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5313 TaskState *ts = cpu->opaque;
5314 FILE *fp;
5315 char *line = NULL;
5316 size_t len = 0;
5317 ssize_t read;
5319 fp = fopen("/proc/self/maps", "r");
5320 if (fp == NULL) {
5321 return -EACCES;
5324 while ((read = getline(&line, &len, fp)) != -1) {
5325 int fields, dev_maj, dev_min, inode;
5326 uint64_t min, max, offset;
5327 char flag_r, flag_w, flag_x, flag_p;
5328 char path[512] = "";
5329 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
5330 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
5331 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
5333 if ((fields < 10) || (fields > 11)) {
5334 continue;
5336 if (h2g_valid(min)) {
5337 int flags = page_get_flags(h2g(min));
5338 max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX);
5339 if (page_check_range(h2g(min), max - min, flags) == -1) {
5340 continue;
5342 if (h2g(min) == ts->info->stack_limit) {
5343 pstrcpy(path, sizeof(path), " [stack]");
5345 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
5346 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
5347 h2g(min), h2g(max - 1) + 1, flag_r, flag_w,
5348 flag_x, flag_p, offset, dev_maj, dev_min, inode,
5349 path[0] ? " " : "", path);
5353 free(line);
5354 fclose(fp);
5356 return 0;
5359 static int open_self_stat(void *cpu_env, int fd)
5361 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5362 TaskState *ts = cpu->opaque;
5363 abi_ulong start_stack = ts->info->start_stack;
5364 int i;
5366 for (i = 0; i < 44; i++) {
5367 char buf[128];
5368 int len;
5369 uint64_t val = 0;
5371 if (i == 0) {
5372 /* pid */
5373 val = getpid();
5374 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5375 } else if (i == 1) {
5376 /* app name */
5377 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
5378 } else if (i == 27) {
5379 /* stack bottom */
5380 val = start_stack;
5381 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5382 } else {
5383 /* for the rest, there is MasterCard */
5384 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
5387 len = strlen(buf);
5388 if (write(fd, buf, len) != len) {
5389 return -1;
5393 return 0;
5396 static int open_self_auxv(void *cpu_env, int fd)
5398 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5399 TaskState *ts = cpu->opaque;
5400 abi_ulong auxv = ts->info->saved_auxv;
5401 abi_ulong len = ts->info->auxv_len;
5402 char *ptr;
5405 * Auxiliary vector is stored in target process stack.
5406 * read in whole auxv vector and copy it to file
5408 ptr = lock_user(VERIFY_READ, auxv, len, 0);
5409 if (ptr != NULL) {
5410 while (len > 0) {
5411 ssize_t r;
5412 r = write(fd, ptr, len);
5413 if (r <= 0) {
5414 break;
5416 len -= r;
5417 ptr += r;
5419 lseek(fd, 0, SEEK_SET);
5420 unlock_user(ptr, auxv, len);
5423 return 0;
5426 static int is_proc_myself(const char *filename, const char *entry)
5428 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
5429 filename += strlen("/proc/");
5430 if (!strncmp(filename, "self/", strlen("self/"))) {
5431 filename += strlen("self/");
5432 } else if (*filename >= '1' && *filename <= '9') {
5433 char myself[80];
5434 snprintf(myself, sizeof(myself), "%d/", getpid());
5435 if (!strncmp(filename, myself, strlen(myself))) {
5436 filename += strlen(myself);
5437 } else {
5438 return 0;
5440 } else {
5441 return 0;
5443 if (!strcmp(filename, entry)) {
5444 return 1;
5447 return 0;
5450 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5451 static int is_proc(const char *filename, const char *entry)
5453 return strcmp(filename, entry) == 0;
5456 static int open_net_route(void *cpu_env, int fd)
5458 FILE *fp;
5459 char *line = NULL;
5460 size_t len = 0;
5461 ssize_t read;
5463 fp = fopen("/proc/net/route", "r");
5464 if (fp == NULL) {
5465 return -EACCES;
5468 /* read header */
5470 read = getline(&line, &len, fp);
5471 dprintf(fd, "%s", line);
5473 /* read routes */
5475 while ((read = getline(&line, &len, fp)) != -1) {
5476 char iface[16];
5477 uint32_t dest, gw, mask;
5478 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
5479 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5480 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
5481 &mask, &mtu, &window, &irtt);
5482 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5483 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
5484 metric, tswap32(mask), mtu, window, irtt);
5487 free(line);
5488 fclose(fp);
5490 return 0;
5492 #endif
5494 static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
5496 struct fake_open {
5497 const char *filename;
5498 int (*fill)(void *cpu_env, int fd);
5499 int (*cmp)(const char *s1, const char *s2);
5501 const struct fake_open *fake_open;
5502 static const struct fake_open fakes[] = {
5503 { "maps", open_self_maps, is_proc_myself },
5504 { "stat", open_self_stat, is_proc_myself },
5505 { "auxv", open_self_auxv, is_proc_myself },
5506 { "cmdline", open_self_cmdline, is_proc_myself },
5507 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5508 { "/proc/net/route", open_net_route, is_proc },
5509 #endif
5510 { NULL, NULL, NULL }
5513 if (is_proc_myself(pathname, "exe")) {
5514 int execfd = qemu_getauxval(AT_EXECFD);
5515 return execfd ? execfd : get_errno(sys_openat(dirfd, exec_path, flags, mode));
5518 for (fake_open = fakes; fake_open->filename; fake_open++) {
5519 if (fake_open->cmp(pathname, fake_open->filename)) {
5520 break;
5524 if (fake_open->filename) {
5525 const char *tmpdir;
5526 char filename[PATH_MAX];
5527 int fd, r;
5529 /* create temporary file to map stat to */
5530 tmpdir = getenv("TMPDIR");
5531 if (!tmpdir)
5532 tmpdir = "/tmp";
5533 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5534 fd = mkstemp(filename);
5535 if (fd < 0) {
5536 return fd;
5538 unlink(filename);
5540 if ((r = fake_open->fill(cpu_env, fd))) {
5541 close(fd);
5542 return r;
5544 lseek(fd, 0, SEEK_SET);
5546 return fd;
5549 return get_errno(sys_openat(dirfd, path(pathname), flags, mode));
5552 #define TIMER_MAGIC 0x0caf0000
5553 #define TIMER_MAGIC_MASK 0xffff0000
5555 /* Convert QEMU provided timer ID back to internal 16bit index format */
5556 static target_timer_t get_timer_id(abi_long arg)
5558 target_timer_t timerid = arg;
5560 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
5561 return -TARGET_EINVAL;
5564 timerid &= 0xffff;
5566 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
5567 return -TARGET_EINVAL;
5570 return timerid;
5573 /* do_syscall() should always have a single exit point at the end so
5574 that actions, such as logging of syscall results, can be performed.
5575 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
5576 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5577 abi_long arg2, abi_long arg3, abi_long arg4,
5578 abi_long arg5, abi_long arg6, abi_long arg7,
5579 abi_long arg8)
5581 CPUState *cpu = ENV_GET_CPU(cpu_env);
5582 abi_long ret;
5583 struct stat st;
5584 struct statfs stfs;
5585 void *p;
5587 #ifdef DEBUG
5588 gemu_log("syscall %d", num);
5589 #endif
5590 if(do_strace)
5591 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5593 switch(num) {
5594 case TARGET_NR_exit:
5595 /* In old applications this may be used to implement _exit(2).
5596 However in threaded applictions it is used for thread termination,
5597 and _exit_group is used for application termination.
5598 Do thread termination if we have more then one thread. */
5599 /* FIXME: This probably breaks if a signal arrives. We should probably
5600 be disabling signals. */
5601 if (CPU_NEXT(first_cpu)) {
5602 TaskState *ts;
5604 cpu_list_lock();
5605 /* Remove the CPU from the list. */
5606 QTAILQ_REMOVE(&cpus, cpu, node);
5607 cpu_list_unlock();
5608 ts = cpu->opaque;
5609 if (ts->child_tidptr) {
5610 put_user_u32(0, ts->child_tidptr);
5611 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5612 NULL, NULL, 0);
5614 thread_cpu = NULL;
5615 object_unref(OBJECT(cpu));
5616 g_free(ts);
5617 pthread_exit(NULL);
5619 #ifdef TARGET_GPROF
5620 _mcleanup();
5621 #endif
5622 gdb_exit(cpu_env, arg1);
5623 _exit(arg1);
5624 ret = 0; /* avoid warning */
5625 break;
5626 case TARGET_NR_read:
5627 if (arg3 == 0)
5628 ret = 0;
5629 else {
5630 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5631 goto efault;
5632 ret = get_errno(read(arg1, p, arg3));
5633 unlock_user(p, arg2, ret);
5635 break;
5636 case TARGET_NR_write:
5637 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5638 goto efault;
5639 ret = get_errno(write(arg1, p, arg3));
5640 unlock_user(p, arg2, 0);
5641 break;
5642 case TARGET_NR_open:
5643 if (!(p = lock_user_string(arg1)))
5644 goto efault;
5645 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
5646 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5647 arg3));
5648 unlock_user(p, arg1, 0);
5649 break;
5650 case TARGET_NR_openat:
5651 if (!(p = lock_user_string(arg2)))
5652 goto efault;
5653 ret = get_errno(do_openat(cpu_env, arg1, p,
5654 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5655 arg4));
5656 unlock_user(p, arg2, 0);
5657 break;
5658 case TARGET_NR_close:
5659 ret = get_errno(close(arg1));
5660 break;
5661 case TARGET_NR_brk:
5662 ret = do_brk(arg1);
5663 break;
5664 case TARGET_NR_fork:
5665 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
5666 break;
5667 #ifdef TARGET_NR_waitpid
5668 case TARGET_NR_waitpid:
5670 int status;
5671 ret = get_errno(waitpid(arg1, &status, arg3));
5672 if (!is_error(ret) && arg2 && ret
5673 && put_user_s32(host_to_target_waitstatus(status), arg2))
5674 goto efault;
5676 break;
5677 #endif
5678 #ifdef TARGET_NR_waitid
5679 case TARGET_NR_waitid:
5681 siginfo_t info;
5682 info.si_pid = 0;
5683 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5684 if (!is_error(ret) && arg3 && info.si_pid != 0) {
5685 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
5686 goto efault;
5687 host_to_target_siginfo(p, &info);
5688 unlock_user(p, arg3, sizeof(target_siginfo_t));
5691 break;
5692 #endif
5693 #ifdef TARGET_NR_creat /* not on alpha */
5694 case TARGET_NR_creat:
5695 if (!(p = lock_user_string(arg1)))
5696 goto efault;
5697 ret = get_errno(creat(p, arg2));
5698 unlock_user(p, arg1, 0);
5699 break;
5700 #endif
5701 case TARGET_NR_link:
5703 void * p2;
5704 p = lock_user_string(arg1);
5705 p2 = lock_user_string(arg2);
5706 if (!p || !p2)
5707 ret = -TARGET_EFAULT;
5708 else
5709 ret = get_errno(link(p, p2));
5710 unlock_user(p2, arg2, 0);
5711 unlock_user(p, arg1, 0);
5713 break;
5714 #if defined(TARGET_NR_linkat)
5715 case TARGET_NR_linkat:
5717 void * p2 = NULL;
5718 if (!arg2 || !arg4)
5719 goto efault;
5720 p = lock_user_string(arg2);
5721 p2 = lock_user_string(arg4);
5722 if (!p || !p2)
5723 ret = -TARGET_EFAULT;
5724 else
5725 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
5726 unlock_user(p, arg2, 0);
5727 unlock_user(p2, arg4, 0);
5729 break;
5730 #endif
5731 case TARGET_NR_unlink:
5732 if (!(p = lock_user_string(arg1)))
5733 goto efault;
5734 ret = get_errno(unlink(p));
5735 unlock_user(p, arg1, 0);
5736 break;
5737 #if defined(TARGET_NR_unlinkat)
5738 case TARGET_NR_unlinkat:
5739 if (!(p = lock_user_string(arg2)))
5740 goto efault;
5741 ret = get_errno(unlinkat(arg1, p, arg3));
5742 unlock_user(p, arg2, 0);
5743 break;
5744 #endif
5745 case TARGET_NR_execve:
5747 char **argp, **envp;
5748 int argc, envc;
5749 abi_ulong gp;
5750 abi_ulong guest_argp;
5751 abi_ulong guest_envp;
5752 abi_ulong addr;
5753 char **q;
5754 int total_size = 0;
5756 argc = 0;
5757 guest_argp = arg2;
5758 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
5759 if (get_user_ual(addr, gp))
5760 goto efault;
5761 if (!addr)
5762 break;
5763 argc++;
5765 envc = 0;
5766 guest_envp = arg3;
5767 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
5768 if (get_user_ual(addr, gp))
5769 goto efault;
5770 if (!addr)
5771 break;
5772 envc++;
5775 argp = alloca((argc + 1) * sizeof(void *));
5776 envp = alloca((envc + 1) * sizeof(void *));
5778 for (gp = guest_argp, q = argp; gp;
5779 gp += sizeof(abi_ulong), q++) {
5780 if (get_user_ual(addr, gp))
5781 goto execve_efault;
5782 if (!addr)
5783 break;
5784 if (!(*q = lock_user_string(addr)))
5785 goto execve_efault;
5786 total_size += strlen(*q) + 1;
5788 *q = NULL;
5790 for (gp = guest_envp, q = envp; gp;
5791 gp += sizeof(abi_ulong), q++) {
5792 if (get_user_ual(addr, gp))
5793 goto execve_efault;
5794 if (!addr)
5795 break;
5796 if (!(*q = lock_user_string(addr)))
5797 goto execve_efault;
5798 total_size += strlen(*q) + 1;
5800 *q = NULL;
5802 /* This case will not be caught by the host's execve() if its
5803 page size is bigger than the target's. */
5804 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5805 ret = -TARGET_E2BIG;
5806 goto execve_end;
5808 if (!(p = lock_user_string(arg1)))
5809 goto execve_efault;
5810 ret = get_errno(execve(p, argp, envp));
5811 unlock_user(p, arg1, 0);
5813 goto execve_end;
5815 execve_efault:
5816 ret = -TARGET_EFAULT;
5818 execve_end:
5819 for (gp = guest_argp, q = argp; *q;
5820 gp += sizeof(abi_ulong), q++) {
5821 if (get_user_ual(addr, gp)
5822 || !addr)
5823 break;
5824 unlock_user(*q, addr, 0);
5826 for (gp = guest_envp, q = envp; *q;
5827 gp += sizeof(abi_ulong), q++) {
5828 if (get_user_ual(addr, gp)
5829 || !addr)
5830 break;
5831 unlock_user(*q, addr, 0);
5834 break;
5835 case TARGET_NR_chdir:
5836 if (!(p = lock_user_string(arg1)))
5837 goto efault;
5838 ret = get_errno(chdir(p));
5839 unlock_user(p, arg1, 0);
5840 break;
5841 #ifdef TARGET_NR_time
5842 case TARGET_NR_time:
5844 time_t host_time;
5845 ret = get_errno(time(&host_time));
5846 if (!is_error(ret)
5847 && arg1
5848 && put_user_sal(host_time, arg1))
5849 goto efault;
5851 break;
5852 #endif
5853 case TARGET_NR_mknod:
5854 if (!(p = lock_user_string(arg1)))
5855 goto efault;
5856 ret = get_errno(mknod(p, arg2, arg3));
5857 unlock_user(p, arg1, 0);
5858 break;
5859 #if defined(TARGET_NR_mknodat)
5860 case TARGET_NR_mknodat:
5861 if (!(p = lock_user_string(arg2)))
5862 goto efault;
5863 ret = get_errno(mknodat(arg1, p, arg3, arg4));
5864 unlock_user(p, arg2, 0);
5865 break;
5866 #endif
5867 case TARGET_NR_chmod:
5868 if (!(p = lock_user_string(arg1)))
5869 goto efault;
5870 ret = get_errno(chmod(p, arg2));
5871 unlock_user(p, arg1, 0);
5872 break;
5873 #ifdef TARGET_NR_break
5874 case TARGET_NR_break:
5875 goto unimplemented;
5876 #endif
5877 #ifdef TARGET_NR_oldstat
5878 case TARGET_NR_oldstat:
5879 goto unimplemented;
5880 #endif
5881 case TARGET_NR_lseek:
5882 ret = get_errno(lseek(arg1, arg2, arg3));
5883 break;
5884 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5885 /* Alpha specific */
5886 case TARGET_NR_getxpid:
5887 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
5888 ret = get_errno(getpid());
5889 break;
5890 #endif
5891 #ifdef TARGET_NR_getpid
5892 case TARGET_NR_getpid:
5893 ret = get_errno(getpid());
5894 break;
5895 #endif
5896 case TARGET_NR_mount:
5898 /* need to look at the data field */
5899 void *p2, *p3;
5901 if (arg1) {
5902 p = lock_user_string(arg1);
5903 if (!p) {
5904 goto efault;
5906 } else {
5907 p = NULL;
5910 p2 = lock_user_string(arg2);
5911 if (!p2) {
5912 if (arg1) {
5913 unlock_user(p, arg1, 0);
5915 goto efault;
5918 if (arg3) {
5919 p3 = lock_user_string(arg3);
5920 if (!p3) {
5921 if (arg1) {
5922 unlock_user(p, arg1, 0);
5924 unlock_user(p2, arg2, 0);
5925 goto efault;
5927 } else {
5928 p3 = NULL;
5931 /* FIXME - arg5 should be locked, but it isn't clear how to
5932 * do that since it's not guaranteed to be a NULL-terminated
5933 * string.
5935 if (!arg5) {
5936 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
5937 } else {
5938 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
5940 ret = get_errno(ret);
5942 if (arg1) {
5943 unlock_user(p, arg1, 0);
5945 unlock_user(p2, arg2, 0);
5946 if (arg3) {
5947 unlock_user(p3, arg3, 0);
5950 break;
5951 #ifdef TARGET_NR_umount
5952 case TARGET_NR_umount:
5953 if (!(p = lock_user_string(arg1)))
5954 goto efault;
5955 ret = get_errno(umount(p));
5956 unlock_user(p, arg1, 0);
5957 break;
5958 #endif
5959 #ifdef TARGET_NR_stime /* not on alpha */
5960 case TARGET_NR_stime:
5962 time_t host_time;
5963 if (get_user_sal(host_time, arg1))
5964 goto efault;
5965 ret = get_errno(stime(&host_time));
5967 break;
5968 #endif
5969 case TARGET_NR_ptrace:
5970 goto unimplemented;
5971 #ifdef TARGET_NR_alarm /* not on alpha */
5972 case TARGET_NR_alarm:
5973 ret = alarm(arg1);
5974 break;
5975 #endif
5976 #ifdef TARGET_NR_oldfstat
5977 case TARGET_NR_oldfstat:
5978 goto unimplemented;
5979 #endif
5980 #ifdef TARGET_NR_pause /* not on alpha */
5981 case TARGET_NR_pause:
5982 ret = get_errno(pause());
5983 break;
5984 #endif
5985 #ifdef TARGET_NR_utime
5986 case TARGET_NR_utime:
5988 struct utimbuf tbuf, *host_tbuf;
5989 struct target_utimbuf *target_tbuf;
5990 if (arg2) {
5991 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5992 goto efault;
5993 tbuf.actime = tswapal(target_tbuf->actime);
5994 tbuf.modtime = tswapal(target_tbuf->modtime);
5995 unlock_user_struct(target_tbuf, arg2, 0);
5996 host_tbuf = &tbuf;
5997 } else {
5998 host_tbuf = NULL;
6000 if (!(p = lock_user_string(arg1)))
6001 goto efault;
6002 ret = get_errno(utime(p, host_tbuf));
6003 unlock_user(p, arg1, 0);
6005 break;
6006 #endif
6007 case TARGET_NR_utimes:
6009 struct timeval *tvp, tv[2];
6010 if (arg2) {
6011 if (copy_from_user_timeval(&tv[0], arg2)
6012 || copy_from_user_timeval(&tv[1],
6013 arg2 + sizeof(struct target_timeval)))
6014 goto efault;
6015 tvp = tv;
6016 } else {
6017 tvp = NULL;
6019 if (!(p = lock_user_string(arg1)))
6020 goto efault;
6021 ret = get_errno(utimes(p, tvp));
6022 unlock_user(p, arg1, 0);
6024 break;
6025 #if defined(TARGET_NR_futimesat)
6026 case TARGET_NR_futimesat:
6028 struct timeval *tvp, tv[2];
6029 if (arg3) {
6030 if (copy_from_user_timeval(&tv[0], arg3)
6031 || copy_from_user_timeval(&tv[1],
6032 arg3 + sizeof(struct target_timeval)))
6033 goto efault;
6034 tvp = tv;
6035 } else {
6036 tvp = NULL;
6038 if (!(p = lock_user_string(arg2)))
6039 goto efault;
6040 ret = get_errno(futimesat(arg1, path(p), tvp));
6041 unlock_user(p, arg2, 0);
6043 break;
6044 #endif
6045 #ifdef TARGET_NR_stty
6046 case TARGET_NR_stty:
6047 goto unimplemented;
6048 #endif
6049 #ifdef TARGET_NR_gtty
6050 case TARGET_NR_gtty:
6051 goto unimplemented;
6052 #endif
6053 case TARGET_NR_access:
6054 if (!(p = lock_user_string(arg1)))
6055 goto efault;
6056 ret = get_errno(access(path(p), arg2));
6057 unlock_user(p, arg1, 0);
6058 break;
6059 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
6060 case TARGET_NR_faccessat:
6061 if (!(p = lock_user_string(arg2)))
6062 goto efault;
6063 ret = get_errno(faccessat(arg1, p, arg3, 0));
6064 unlock_user(p, arg2, 0);
6065 break;
6066 #endif
6067 #ifdef TARGET_NR_nice /* not on alpha */
6068 case TARGET_NR_nice:
6069 ret = get_errno(nice(arg1));
6070 break;
6071 #endif
6072 #ifdef TARGET_NR_ftime
6073 case TARGET_NR_ftime:
6074 goto unimplemented;
6075 #endif
6076 case TARGET_NR_sync:
6077 sync();
6078 ret = 0;
6079 break;
6080 case TARGET_NR_kill:
6081 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
6082 break;
6083 case TARGET_NR_rename:
6085 void *p2;
6086 p = lock_user_string(arg1);
6087 p2 = lock_user_string(arg2);
6088 if (!p || !p2)
6089 ret = -TARGET_EFAULT;
6090 else
6091 ret = get_errno(rename(p, p2));
6092 unlock_user(p2, arg2, 0);
6093 unlock_user(p, arg1, 0);
6095 break;
6096 #if defined(TARGET_NR_renameat)
6097 case TARGET_NR_renameat:
6099 void *p2;
6100 p = lock_user_string(arg2);
6101 p2 = lock_user_string(arg4);
6102 if (!p || !p2)
6103 ret = -TARGET_EFAULT;
6104 else
6105 ret = get_errno(renameat(arg1, p, arg3, p2));
6106 unlock_user(p2, arg4, 0);
6107 unlock_user(p, arg2, 0);
6109 break;
6110 #endif
6111 case TARGET_NR_mkdir:
6112 if (!(p = lock_user_string(arg1)))
6113 goto efault;
6114 ret = get_errno(mkdir(p, arg2));
6115 unlock_user(p, arg1, 0);
6116 break;
6117 #if defined(TARGET_NR_mkdirat)
6118 case TARGET_NR_mkdirat:
6119 if (!(p = lock_user_string(arg2)))
6120 goto efault;
6121 ret = get_errno(mkdirat(arg1, p, arg3));
6122 unlock_user(p, arg2, 0);
6123 break;
6124 #endif
6125 case TARGET_NR_rmdir:
6126 if (!(p = lock_user_string(arg1)))
6127 goto efault;
6128 ret = get_errno(rmdir(p));
6129 unlock_user(p, arg1, 0);
6130 break;
6131 case TARGET_NR_dup:
6132 ret = get_errno(dup(arg1));
6133 break;
6134 case TARGET_NR_pipe:
6135 ret = do_pipe(cpu_env, arg1, 0, 0);
6136 break;
6137 #ifdef TARGET_NR_pipe2
6138 case TARGET_NR_pipe2:
6139 ret = do_pipe(cpu_env, arg1,
6140 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
6141 break;
6142 #endif
6143 case TARGET_NR_times:
6145 struct target_tms *tmsp;
6146 struct tms tms;
6147 ret = get_errno(times(&tms));
6148 if (arg1) {
6149 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
6150 if (!tmsp)
6151 goto efault;
6152 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
6153 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
6154 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
6155 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
6157 if (!is_error(ret))
6158 ret = host_to_target_clock_t(ret);
6160 break;
6161 #ifdef TARGET_NR_prof
6162 case TARGET_NR_prof:
6163 goto unimplemented;
6164 #endif
6165 #ifdef TARGET_NR_signal
6166 case TARGET_NR_signal:
6167 goto unimplemented;
6168 #endif
6169 case TARGET_NR_acct:
6170 if (arg1 == 0) {
6171 ret = get_errno(acct(NULL));
6172 } else {
6173 if (!(p = lock_user_string(arg1)))
6174 goto efault;
6175 ret = get_errno(acct(path(p)));
6176 unlock_user(p, arg1, 0);
6178 break;
6179 #ifdef TARGET_NR_umount2
6180 case TARGET_NR_umount2:
6181 if (!(p = lock_user_string(arg1)))
6182 goto efault;
6183 ret = get_errno(umount2(p, arg2));
6184 unlock_user(p, arg1, 0);
6185 break;
6186 #endif
6187 #ifdef TARGET_NR_lock
6188 case TARGET_NR_lock:
6189 goto unimplemented;
6190 #endif
6191 case TARGET_NR_ioctl:
6192 ret = do_ioctl(arg1, arg2, arg3);
6193 break;
6194 case TARGET_NR_fcntl:
6195 ret = do_fcntl(arg1, arg2, arg3);
6196 break;
6197 #ifdef TARGET_NR_mpx
6198 case TARGET_NR_mpx:
6199 goto unimplemented;
6200 #endif
6201 case TARGET_NR_setpgid:
6202 ret = get_errno(setpgid(arg1, arg2));
6203 break;
6204 #ifdef TARGET_NR_ulimit
6205 case TARGET_NR_ulimit:
6206 goto unimplemented;
6207 #endif
6208 #ifdef TARGET_NR_oldolduname
6209 case TARGET_NR_oldolduname:
6210 goto unimplemented;
6211 #endif
6212 case TARGET_NR_umask:
6213 ret = get_errno(umask(arg1));
6214 break;
6215 case TARGET_NR_chroot:
6216 if (!(p = lock_user_string(arg1)))
6217 goto efault;
6218 ret = get_errno(chroot(p));
6219 unlock_user(p, arg1, 0);
6220 break;
6221 case TARGET_NR_ustat:
6222 goto unimplemented;
6223 case TARGET_NR_dup2:
6224 ret = get_errno(dup2(arg1, arg2));
6225 break;
6226 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
6227 case TARGET_NR_dup3:
6228 ret = get_errno(dup3(arg1, arg2, arg3));
6229 break;
6230 #endif
6231 #ifdef TARGET_NR_getppid /* not on alpha */
6232 case TARGET_NR_getppid:
6233 ret = get_errno(getppid());
6234 break;
6235 #endif
6236 case TARGET_NR_getpgrp:
6237 ret = get_errno(getpgrp());
6238 break;
6239 case TARGET_NR_setsid:
6240 ret = get_errno(setsid());
6241 break;
6242 #ifdef TARGET_NR_sigaction
6243 case TARGET_NR_sigaction:
6245 #if defined(TARGET_ALPHA)
6246 struct target_sigaction act, oact, *pact = 0;
6247 struct target_old_sigaction *old_act;
6248 if (arg2) {
6249 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6250 goto efault;
6251 act._sa_handler = old_act->_sa_handler;
6252 target_siginitset(&act.sa_mask, old_act->sa_mask);
6253 act.sa_flags = old_act->sa_flags;
6254 act.sa_restorer = 0;
6255 unlock_user_struct(old_act, arg2, 0);
6256 pact = &act;
6258 ret = get_errno(do_sigaction(arg1, pact, &oact));
6259 if (!is_error(ret) && arg3) {
6260 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6261 goto efault;
6262 old_act->_sa_handler = oact._sa_handler;
6263 old_act->sa_mask = oact.sa_mask.sig[0];
6264 old_act->sa_flags = oact.sa_flags;
6265 unlock_user_struct(old_act, arg3, 1);
6267 #elif defined(TARGET_MIPS)
6268 struct target_sigaction act, oact, *pact, *old_act;
6270 if (arg2) {
6271 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6272 goto efault;
6273 act._sa_handler = old_act->_sa_handler;
6274 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
6275 act.sa_flags = old_act->sa_flags;
6276 unlock_user_struct(old_act, arg2, 0);
6277 pact = &act;
6278 } else {
6279 pact = NULL;
6282 ret = get_errno(do_sigaction(arg1, pact, &oact));
6284 if (!is_error(ret) && arg3) {
6285 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6286 goto efault;
6287 old_act->_sa_handler = oact._sa_handler;
6288 old_act->sa_flags = oact.sa_flags;
6289 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
6290 old_act->sa_mask.sig[1] = 0;
6291 old_act->sa_mask.sig[2] = 0;
6292 old_act->sa_mask.sig[3] = 0;
6293 unlock_user_struct(old_act, arg3, 1);
6295 #else
6296 struct target_old_sigaction *old_act;
6297 struct target_sigaction act, oact, *pact;
6298 if (arg2) {
6299 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6300 goto efault;
6301 act._sa_handler = old_act->_sa_handler;
6302 target_siginitset(&act.sa_mask, old_act->sa_mask);
6303 act.sa_flags = old_act->sa_flags;
6304 act.sa_restorer = old_act->sa_restorer;
6305 unlock_user_struct(old_act, arg2, 0);
6306 pact = &act;
6307 } else {
6308 pact = NULL;
6310 ret = get_errno(do_sigaction(arg1, pact, &oact));
6311 if (!is_error(ret) && arg3) {
6312 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6313 goto efault;
6314 old_act->_sa_handler = oact._sa_handler;
6315 old_act->sa_mask = oact.sa_mask.sig[0];
6316 old_act->sa_flags = oact.sa_flags;
6317 old_act->sa_restorer = oact.sa_restorer;
6318 unlock_user_struct(old_act, arg3, 1);
6320 #endif
6322 break;
6323 #endif
6324 case TARGET_NR_rt_sigaction:
6326 #if defined(TARGET_ALPHA)
6327 struct target_sigaction act, oact, *pact = 0;
6328 struct target_rt_sigaction *rt_act;
6329 /* ??? arg4 == sizeof(sigset_t). */
6330 if (arg2) {
6331 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
6332 goto efault;
6333 act._sa_handler = rt_act->_sa_handler;
6334 act.sa_mask = rt_act->sa_mask;
6335 act.sa_flags = rt_act->sa_flags;
6336 act.sa_restorer = arg5;
6337 unlock_user_struct(rt_act, arg2, 0);
6338 pact = &act;
6340 ret = get_errno(do_sigaction(arg1, pact, &oact));
6341 if (!is_error(ret) && arg3) {
6342 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
6343 goto efault;
6344 rt_act->_sa_handler = oact._sa_handler;
6345 rt_act->sa_mask = oact.sa_mask;
6346 rt_act->sa_flags = oact.sa_flags;
6347 unlock_user_struct(rt_act, arg3, 1);
6349 #else
6350 struct target_sigaction *act;
6351 struct target_sigaction *oact;
6353 if (arg2) {
6354 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
6355 goto efault;
6356 } else
6357 act = NULL;
6358 if (arg3) {
6359 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
6360 ret = -TARGET_EFAULT;
6361 goto rt_sigaction_fail;
6363 } else
6364 oact = NULL;
6365 ret = get_errno(do_sigaction(arg1, act, oact));
6366 rt_sigaction_fail:
6367 if (act)
6368 unlock_user_struct(act, arg2, 0);
6369 if (oact)
6370 unlock_user_struct(oact, arg3, 1);
6371 #endif
6373 break;
6374 #ifdef TARGET_NR_sgetmask /* not on alpha */
6375 case TARGET_NR_sgetmask:
6377 sigset_t cur_set;
6378 abi_ulong target_set;
6379 do_sigprocmask(0, NULL, &cur_set);
6380 host_to_target_old_sigset(&target_set, &cur_set);
6381 ret = target_set;
6383 break;
6384 #endif
6385 #ifdef TARGET_NR_ssetmask /* not on alpha */
6386 case TARGET_NR_ssetmask:
6388 sigset_t set, oset, cur_set;
6389 abi_ulong target_set = arg1;
6390 do_sigprocmask(0, NULL, &cur_set);
6391 target_to_host_old_sigset(&set, &target_set);
6392 sigorset(&set, &set, &cur_set);
6393 do_sigprocmask(SIG_SETMASK, &set, &oset);
6394 host_to_target_old_sigset(&target_set, &oset);
6395 ret = target_set;
6397 break;
6398 #endif
6399 #ifdef TARGET_NR_sigprocmask
6400 case TARGET_NR_sigprocmask:
6402 #if defined(TARGET_ALPHA)
6403 sigset_t set, oldset;
6404 abi_ulong mask;
6405 int how;
6407 switch (arg1) {
6408 case TARGET_SIG_BLOCK:
6409 how = SIG_BLOCK;
6410 break;
6411 case TARGET_SIG_UNBLOCK:
6412 how = SIG_UNBLOCK;
6413 break;
6414 case TARGET_SIG_SETMASK:
6415 how = SIG_SETMASK;
6416 break;
6417 default:
6418 ret = -TARGET_EINVAL;
6419 goto fail;
6421 mask = arg2;
6422 target_to_host_old_sigset(&set, &mask);
6424 ret = get_errno(do_sigprocmask(how, &set, &oldset));
6425 if (!is_error(ret)) {
6426 host_to_target_old_sigset(&mask, &oldset);
6427 ret = mask;
6428 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
6430 #else
6431 sigset_t set, oldset, *set_ptr;
6432 int how;
6434 if (arg2) {
6435 switch (arg1) {
6436 case TARGET_SIG_BLOCK:
6437 how = SIG_BLOCK;
6438 break;
6439 case TARGET_SIG_UNBLOCK:
6440 how = SIG_UNBLOCK;
6441 break;
6442 case TARGET_SIG_SETMASK:
6443 how = SIG_SETMASK;
6444 break;
6445 default:
6446 ret = -TARGET_EINVAL;
6447 goto fail;
6449 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
6450 goto efault;
6451 target_to_host_old_sigset(&set, p);
6452 unlock_user(p, arg2, 0);
6453 set_ptr = &set;
6454 } else {
6455 how = 0;
6456 set_ptr = NULL;
6458 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
6459 if (!is_error(ret) && arg3) {
6460 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
6461 goto efault;
6462 host_to_target_old_sigset(p, &oldset);
6463 unlock_user(p, arg3, sizeof(target_sigset_t));
6465 #endif
6467 break;
6468 #endif
6469 case TARGET_NR_rt_sigprocmask:
6471 int how = arg1;
6472 sigset_t set, oldset, *set_ptr;
6474 if (arg2) {
6475 switch(how) {
6476 case TARGET_SIG_BLOCK:
6477 how = SIG_BLOCK;
6478 break;
6479 case TARGET_SIG_UNBLOCK:
6480 how = SIG_UNBLOCK;
6481 break;
6482 case TARGET_SIG_SETMASK:
6483 how = SIG_SETMASK;
6484 break;
6485 default:
6486 ret = -TARGET_EINVAL;
6487 goto fail;
6489 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
6490 goto efault;
6491 target_to_host_sigset(&set, p);
6492 unlock_user(p, arg2, 0);
6493 set_ptr = &set;
6494 } else {
6495 how = 0;
6496 set_ptr = NULL;
6498 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
6499 if (!is_error(ret) && arg3) {
6500 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
6501 goto efault;
6502 host_to_target_sigset(p, &oldset);
6503 unlock_user(p, arg3, sizeof(target_sigset_t));
6506 break;
6507 #ifdef TARGET_NR_sigpending
6508 case TARGET_NR_sigpending:
6510 sigset_t set;
6511 ret = get_errno(sigpending(&set));
6512 if (!is_error(ret)) {
6513 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
6514 goto efault;
6515 host_to_target_old_sigset(p, &set);
6516 unlock_user(p, arg1, sizeof(target_sigset_t));
6519 break;
6520 #endif
6521 case TARGET_NR_rt_sigpending:
6523 sigset_t set;
6524 ret = get_errno(sigpending(&set));
6525 if (!is_error(ret)) {
6526 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
6527 goto efault;
6528 host_to_target_sigset(p, &set);
6529 unlock_user(p, arg1, sizeof(target_sigset_t));
6532 break;
6533 #ifdef TARGET_NR_sigsuspend
6534 case TARGET_NR_sigsuspend:
6536 sigset_t set;
6537 #if defined(TARGET_ALPHA)
6538 abi_ulong mask = arg1;
6539 target_to_host_old_sigset(&set, &mask);
6540 #else
6541 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
6542 goto efault;
6543 target_to_host_old_sigset(&set, p);
6544 unlock_user(p, arg1, 0);
6545 #endif
6546 ret = get_errno(sigsuspend(&set));
6548 break;
6549 #endif
6550 case TARGET_NR_rt_sigsuspend:
6552 sigset_t set;
6553 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
6554 goto efault;
6555 target_to_host_sigset(&set, p);
6556 unlock_user(p, arg1, 0);
6557 ret = get_errno(sigsuspend(&set));
6559 break;
6560 case TARGET_NR_rt_sigtimedwait:
6562 sigset_t set;
6563 struct timespec uts, *puts;
6564 siginfo_t uinfo;
6566 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
6567 goto efault;
6568 target_to_host_sigset(&set, p);
6569 unlock_user(p, arg1, 0);
6570 if (arg3) {
6571 puts = &uts;
6572 target_to_host_timespec(puts, arg3);
6573 } else {
6574 puts = NULL;
6576 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
6577 if (!is_error(ret)) {
6578 if (arg2) {
6579 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
6581 if (!p) {
6582 goto efault;
6584 host_to_target_siginfo(p, &uinfo);
6585 unlock_user(p, arg2, sizeof(target_siginfo_t));
6587 ret = host_to_target_signal(ret);
6590 break;
6591 case TARGET_NR_rt_sigqueueinfo:
6593 siginfo_t uinfo;
6594 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
6595 goto efault;
6596 target_to_host_siginfo(&uinfo, p);
6597 unlock_user(p, arg1, 0);
6598 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6600 break;
6601 #ifdef TARGET_NR_sigreturn
6602 case TARGET_NR_sigreturn:
6603 /* NOTE: ret is eax, so not transcoding must be done */
6604 ret = do_sigreturn(cpu_env);
6605 break;
6606 #endif
6607 case TARGET_NR_rt_sigreturn:
6608 /* NOTE: ret is eax, so not transcoding must be done */
6609 ret = do_rt_sigreturn(cpu_env);
6610 break;
6611 case TARGET_NR_sethostname:
6612 if (!(p = lock_user_string(arg1)))
6613 goto efault;
6614 ret = get_errno(sethostname(p, arg2));
6615 unlock_user(p, arg1, 0);
6616 break;
6617 case TARGET_NR_setrlimit:
6619 int resource = target_to_host_resource(arg1);
6620 struct target_rlimit *target_rlim;
6621 struct rlimit rlim;
6622 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6623 goto efault;
6624 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6625 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
6626 unlock_user_struct(target_rlim, arg2, 0);
6627 ret = get_errno(setrlimit(resource, &rlim));
6629 break;
6630 case TARGET_NR_getrlimit:
6632 int resource = target_to_host_resource(arg1);
6633 struct target_rlimit *target_rlim;
6634 struct rlimit rlim;
6636 ret = get_errno(getrlimit(resource, &rlim));
6637 if (!is_error(ret)) {
6638 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6639 goto efault;
6640 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6641 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
6642 unlock_user_struct(target_rlim, arg2, 1);
6645 break;
6646 case TARGET_NR_getrusage:
6648 struct rusage rusage;
6649 ret = get_errno(getrusage(arg1, &rusage));
6650 if (!is_error(ret)) {
6651 ret = host_to_target_rusage(arg2, &rusage);
6654 break;
6655 case TARGET_NR_gettimeofday:
6657 struct timeval tv;
6658 ret = get_errno(gettimeofday(&tv, NULL));
6659 if (!is_error(ret)) {
6660 if (copy_to_user_timeval(arg1, &tv))
6661 goto efault;
6664 break;
6665 case TARGET_NR_settimeofday:
6667 struct timeval tv, *ptv = NULL;
6668 struct timezone tz, *ptz = NULL;
6670 if (arg1) {
6671 if (copy_from_user_timeval(&tv, arg1)) {
6672 goto efault;
6674 ptv = &tv;
6677 if (arg2) {
6678 if (copy_from_user_timezone(&tz, arg2)) {
6679 goto efault;
6681 ptz = &tz;
6684 ret = get_errno(settimeofday(ptv, ptz));
6686 break;
6687 #if defined(TARGET_NR_select)
6688 case TARGET_NR_select:
6689 #if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6690 ret = do_select(arg1, arg2, arg3, arg4, arg5);
6691 #else
6693 struct target_sel_arg_struct *sel;
6694 abi_ulong inp, outp, exp, tvp;
6695 long nsel;
6697 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6698 goto efault;
6699 nsel = tswapal(sel->n);
6700 inp = tswapal(sel->inp);
6701 outp = tswapal(sel->outp);
6702 exp = tswapal(sel->exp);
6703 tvp = tswapal(sel->tvp);
6704 unlock_user_struct(sel, arg1, 0);
6705 ret = do_select(nsel, inp, outp, exp, tvp);
6707 #endif
6708 break;
6709 #endif
6710 #ifdef TARGET_NR_pselect6
6711 case TARGET_NR_pselect6:
6713 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6714 fd_set rfds, wfds, efds;
6715 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6716 struct timespec ts, *ts_ptr;
6719 * The 6th arg is actually two args smashed together,
6720 * so we cannot use the C library.
6722 sigset_t set;
6723 struct {
6724 sigset_t *set;
6725 size_t size;
6726 } sig, *sig_ptr;
6728 abi_ulong arg_sigset, arg_sigsize, *arg7;
6729 target_sigset_t *target_sigset;
6731 n = arg1;
6732 rfd_addr = arg2;
6733 wfd_addr = arg3;
6734 efd_addr = arg4;
6735 ts_addr = arg5;
6737 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
6738 if (ret) {
6739 goto fail;
6741 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
6742 if (ret) {
6743 goto fail;
6745 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
6746 if (ret) {
6747 goto fail;
6751 * This takes a timespec, and not a timeval, so we cannot
6752 * use the do_select() helper ...
6754 if (ts_addr) {
6755 if (target_to_host_timespec(&ts, ts_addr)) {
6756 goto efault;
6758 ts_ptr = &ts;
6759 } else {
6760 ts_ptr = NULL;
6763 /* Extract the two packed args for the sigset */
6764 if (arg6) {
6765 sig_ptr = &sig;
6766 sig.size = _NSIG / 8;
6768 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
6769 if (!arg7) {
6770 goto efault;
6772 arg_sigset = tswapal(arg7[0]);
6773 arg_sigsize = tswapal(arg7[1]);
6774 unlock_user(arg7, arg6, 0);
6776 if (arg_sigset) {
6777 sig.set = &set;
6778 if (arg_sigsize != sizeof(*target_sigset)) {
6779 /* Like the kernel, we enforce correct size sigsets */
6780 ret = -TARGET_EINVAL;
6781 goto fail;
6783 target_sigset = lock_user(VERIFY_READ, arg_sigset,
6784 sizeof(*target_sigset), 1);
6785 if (!target_sigset) {
6786 goto efault;
6788 target_to_host_sigset(&set, target_sigset);
6789 unlock_user(target_sigset, arg_sigset, 0);
6790 } else {
6791 sig.set = NULL;
6793 } else {
6794 sig_ptr = NULL;
6797 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
6798 ts_ptr, sig_ptr));
6800 if (!is_error(ret)) {
6801 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
6802 goto efault;
6803 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
6804 goto efault;
6805 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
6806 goto efault;
6808 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
6809 goto efault;
6812 break;
6813 #endif
6814 case TARGET_NR_symlink:
6816 void *p2;
6817 p = lock_user_string(arg1);
6818 p2 = lock_user_string(arg2);
6819 if (!p || !p2)
6820 ret = -TARGET_EFAULT;
6821 else
6822 ret = get_errno(symlink(p, p2));
6823 unlock_user(p2, arg2, 0);
6824 unlock_user(p, arg1, 0);
6826 break;
6827 #if defined(TARGET_NR_symlinkat)
6828 case TARGET_NR_symlinkat:
6830 void *p2;
6831 p = lock_user_string(arg1);
6832 p2 = lock_user_string(arg3);
6833 if (!p || !p2)
6834 ret = -TARGET_EFAULT;
6835 else
6836 ret = get_errno(symlinkat(p, arg2, p2));
6837 unlock_user(p2, arg3, 0);
6838 unlock_user(p, arg1, 0);
6840 break;
6841 #endif
6842 #ifdef TARGET_NR_oldlstat
6843 case TARGET_NR_oldlstat:
6844 goto unimplemented;
6845 #endif
6846 case TARGET_NR_readlink:
6848 void *p2;
6849 p = lock_user_string(arg1);
6850 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
6851 if (!p || !p2) {
6852 ret = -TARGET_EFAULT;
6853 } else if (!arg3) {
6854 /* Short circuit this for the magic exe check. */
6855 ret = -TARGET_EINVAL;
6856 } else if (is_proc_myself((const char *)p, "exe")) {
6857 char real[PATH_MAX], *temp;
6858 temp = realpath(exec_path, real);
6859 /* Return value is # of bytes that we wrote to the buffer. */
6860 if (temp == NULL) {
6861 ret = get_errno(-1);
6862 } else {
6863 /* Don't worry about sign mismatch as earlier mapping
6864 * logic would have thrown a bad address error. */
6865 ret = MIN(strlen(real), arg3);
6866 /* We cannot NUL terminate the string. */
6867 memcpy(p2, real, ret);
6869 } else {
6870 ret = get_errno(readlink(path(p), p2, arg3));
6872 unlock_user(p2, arg2, ret);
6873 unlock_user(p, arg1, 0);
6875 break;
6876 #if defined(TARGET_NR_readlinkat)
6877 case TARGET_NR_readlinkat:
6879 void *p2;
6880 p = lock_user_string(arg2);
6881 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
6882 if (!p || !p2) {
6883 ret = -TARGET_EFAULT;
6884 } else if (is_proc_myself((const char *)p, "exe")) {
6885 char real[PATH_MAX], *temp;
6886 temp = realpath(exec_path, real);
6887 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
6888 snprintf((char *)p2, arg4, "%s", real);
6889 } else {
6890 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
6892 unlock_user(p2, arg3, ret);
6893 unlock_user(p, arg2, 0);
6895 break;
6896 #endif
6897 #ifdef TARGET_NR_uselib
6898 case TARGET_NR_uselib:
6899 goto unimplemented;
6900 #endif
6901 #ifdef TARGET_NR_swapon
6902 case TARGET_NR_swapon:
6903 if (!(p = lock_user_string(arg1)))
6904 goto efault;
6905 ret = get_errno(swapon(p, arg2));
6906 unlock_user(p, arg1, 0);
6907 break;
6908 #endif
6909 case TARGET_NR_reboot:
6910 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
6911 /* arg4 must be ignored in all other cases */
6912 p = lock_user_string(arg4);
6913 if (!p) {
6914 goto efault;
6916 ret = get_errno(reboot(arg1, arg2, arg3, p));
6917 unlock_user(p, arg4, 0);
6918 } else {
6919 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
6921 break;
6922 #ifdef TARGET_NR_readdir
6923 case TARGET_NR_readdir:
6924 goto unimplemented;
6925 #endif
6926 #ifdef TARGET_NR_mmap
6927 case TARGET_NR_mmap:
6928 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6929 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
6930 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6931 || defined(TARGET_S390X)
6933 abi_ulong *v;
6934 abi_ulong v1, v2, v3, v4, v5, v6;
6935 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6936 goto efault;
6937 v1 = tswapal(v[0]);
6938 v2 = tswapal(v[1]);
6939 v3 = tswapal(v[2]);
6940 v4 = tswapal(v[3]);
6941 v5 = tswapal(v[4]);
6942 v6 = tswapal(v[5]);
6943 unlock_user(v, arg1, 0);
6944 ret = get_errno(target_mmap(v1, v2, v3,
6945 target_to_host_bitmask(v4, mmap_flags_tbl),
6946 v5, v6));
6948 #else
6949 ret = get_errno(target_mmap(arg1, arg2, arg3,
6950 target_to_host_bitmask(arg4, mmap_flags_tbl),
6951 arg5,
6952 arg6));
6953 #endif
6954 break;
6955 #endif
6956 #ifdef TARGET_NR_mmap2
6957 case TARGET_NR_mmap2:
6958 #ifndef MMAP_SHIFT
6959 #define MMAP_SHIFT 12
6960 #endif
6961 ret = get_errno(target_mmap(arg1, arg2, arg3,
6962 target_to_host_bitmask(arg4, mmap_flags_tbl),
6963 arg5,
6964 arg6 << MMAP_SHIFT));
6965 break;
6966 #endif
6967 case TARGET_NR_munmap:
6968 ret = get_errno(target_munmap(arg1, arg2));
6969 break;
6970 case TARGET_NR_mprotect:
6972 TaskState *ts = cpu->opaque;
6973 /* Special hack to detect libc making the stack executable. */
6974 if ((arg3 & PROT_GROWSDOWN)
6975 && arg1 >= ts->info->stack_limit
6976 && arg1 <= ts->info->start_stack) {
6977 arg3 &= ~PROT_GROWSDOWN;
6978 arg2 = arg2 + arg1 - ts->info->stack_limit;
6979 arg1 = ts->info->stack_limit;
6982 ret = get_errno(target_mprotect(arg1, arg2, arg3));
6983 break;
6984 #ifdef TARGET_NR_mremap
6985 case TARGET_NR_mremap:
6986 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
6987 break;
6988 #endif
6989 /* ??? msync/mlock/munlock are broken for softmmu. */
6990 #ifdef TARGET_NR_msync
6991 case TARGET_NR_msync:
6992 ret = get_errno(msync(g2h(arg1), arg2, arg3));
6993 break;
6994 #endif
6995 #ifdef TARGET_NR_mlock
6996 case TARGET_NR_mlock:
6997 ret = get_errno(mlock(g2h(arg1), arg2));
6998 break;
6999 #endif
7000 #ifdef TARGET_NR_munlock
7001 case TARGET_NR_munlock:
7002 ret = get_errno(munlock(g2h(arg1), arg2));
7003 break;
7004 #endif
7005 #ifdef TARGET_NR_mlockall
7006 case TARGET_NR_mlockall:
7007 ret = get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
7008 break;
7009 #endif
7010 #ifdef TARGET_NR_munlockall
7011 case TARGET_NR_munlockall:
7012 ret = get_errno(munlockall());
7013 break;
7014 #endif
7015 case TARGET_NR_truncate:
7016 if (!(p = lock_user_string(arg1)))
7017 goto efault;
7018 ret = get_errno(truncate(p, arg2));
7019 unlock_user(p, arg1, 0);
7020 break;
7021 case TARGET_NR_ftruncate:
7022 ret = get_errno(ftruncate(arg1, arg2));
7023 break;
7024 case TARGET_NR_fchmod:
7025 ret = get_errno(fchmod(arg1, arg2));
7026 break;
7027 #if defined(TARGET_NR_fchmodat)
7028 case TARGET_NR_fchmodat:
7029 if (!(p = lock_user_string(arg2)))
7030 goto efault;
7031 ret = get_errno(fchmodat(arg1, p, arg3, 0));
7032 unlock_user(p, arg2, 0);
7033 break;
7034 #endif
7035 case TARGET_NR_getpriority:
7036 /* Note that negative values are valid for getpriority, so we must
7037 differentiate based on errno settings. */
7038 errno = 0;
7039 ret = getpriority(arg1, arg2);
7040 if (ret == -1 && errno != 0) {
7041 ret = -host_to_target_errno(errno);
7042 break;
7044 #ifdef TARGET_ALPHA
7045 /* Return value is the unbiased priority. Signal no error. */
7046 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
7047 #else
7048 /* Return value is a biased priority to avoid negative numbers. */
7049 ret = 20 - ret;
7050 #endif
7051 break;
7052 case TARGET_NR_setpriority:
7053 ret = get_errno(setpriority(arg1, arg2, arg3));
7054 break;
7055 #ifdef TARGET_NR_profil
7056 case TARGET_NR_profil:
7057 goto unimplemented;
7058 #endif
7059 case TARGET_NR_statfs:
7060 if (!(p = lock_user_string(arg1)))
7061 goto efault;
7062 ret = get_errno(statfs(path(p), &stfs));
7063 unlock_user(p, arg1, 0);
7064 convert_statfs:
7065 if (!is_error(ret)) {
7066 struct target_statfs *target_stfs;
7068 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
7069 goto efault;
7070 __put_user(stfs.f_type, &target_stfs->f_type);
7071 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
7072 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
7073 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
7074 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
7075 __put_user(stfs.f_files, &target_stfs->f_files);
7076 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
7077 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
7078 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
7079 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
7080 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
7081 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
7082 unlock_user_struct(target_stfs, arg2, 1);
7084 break;
7085 case TARGET_NR_fstatfs:
7086 ret = get_errno(fstatfs(arg1, &stfs));
7087 goto convert_statfs;
7088 #ifdef TARGET_NR_statfs64
7089 case TARGET_NR_statfs64:
7090 if (!(p = lock_user_string(arg1)))
7091 goto efault;
7092 ret = get_errno(statfs(path(p), &stfs));
7093 unlock_user(p, arg1, 0);
7094 convert_statfs64:
7095 if (!is_error(ret)) {
7096 struct target_statfs64 *target_stfs;
7098 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
7099 goto efault;
7100 __put_user(stfs.f_type, &target_stfs->f_type);
7101 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
7102 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
7103 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
7104 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
7105 __put_user(stfs.f_files, &target_stfs->f_files);
7106 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
7107 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
7108 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
7109 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
7110 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
7111 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
7112 unlock_user_struct(target_stfs, arg3, 1);
7114 break;
7115 case TARGET_NR_fstatfs64:
7116 ret = get_errno(fstatfs(arg1, &stfs));
7117 goto convert_statfs64;
7118 #endif
7119 #ifdef TARGET_NR_ioperm
7120 case TARGET_NR_ioperm:
7121 goto unimplemented;
7122 #endif
7123 #ifdef TARGET_NR_socketcall
7124 case TARGET_NR_socketcall:
7125 ret = do_socketcall(arg1, arg2);
7126 break;
7127 #endif
7128 #ifdef TARGET_NR_accept
7129 case TARGET_NR_accept:
7130 ret = do_accept4(arg1, arg2, arg3, 0);
7131 break;
7132 #endif
7133 #ifdef TARGET_NR_accept4
7134 case TARGET_NR_accept4:
7135 #ifdef CONFIG_ACCEPT4
7136 ret = do_accept4(arg1, arg2, arg3, arg4);
7137 #else
7138 goto unimplemented;
7139 #endif
7140 break;
7141 #endif
7142 #ifdef TARGET_NR_bind
7143 case TARGET_NR_bind:
7144 ret = do_bind(arg1, arg2, arg3);
7145 break;
7146 #endif
7147 #ifdef TARGET_NR_connect
7148 case TARGET_NR_connect:
7149 ret = do_connect(arg1, arg2, arg3);
7150 break;
7151 #endif
7152 #ifdef TARGET_NR_getpeername
7153 case TARGET_NR_getpeername:
7154 ret = do_getpeername(arg1, arg2, arg3);
7155 break;
7156 #endif
7157 #ifdef TARGET_NR_getsockname
7158 case TARGET_NR_getsockname:
7159 ret = do_getsockname(arg1, arg2, arg3);
7160 break;
7161 #endif
7162 #ifdef TARGET_NR_getsockopt
7163 case TARGET_NR_getsockopt:
7164 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
7165 break;
7166 #endif
7167 #ifdef TARGET_NR_listen
7168 case TARGET_NR_listen:
7169 ret = get_errno(listen(arg1, arg2));
7170 break;
7171 #endif
7172 #ifdef TARGET_NR_recv
7173 case TARGET_NR_recv:
7174 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
7175 break;
7176 #endif
7177 #ifdef TARGET_NR_recvfrom
7178 case TARGET_NR_recvfrom:
7179 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
7180 break;
7181 #endif
7182 #ifdef TARGET_NR_recvmsg
7183 case TARGET_NR_recvmsg:
7184 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
7185 break;
7186 #endif
7187 #ifdef TARGET_NR_send
7188 case TARGET_NR_send:
7189 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
7190 break;
7191 #endif
7192 #ifdef TARGET_NR_sendmsg
7193 case TARGET_NR_sendmsg:
7194 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
7195 break;
7196 #endif
7197 #ifdef TARGET_NR_sendmmsg
7198 case TARGET_NR_sendmmsg:
7199 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
7200 break;
7201 case TARGET_NR_recvmmsg:
7202 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
7203 break;
7204 #endif
7205 #ifdef TARGET_NR_sendto
7206 case TARGET_NR_sendto:
7207 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
7208 break;
7209 #endif
7210 #ifdef TARGET_NR_shutdown
7211 case TARGET_NR_shutdown:
7212 ret = get_errno(shutdown(arg1, arg2));
7213 break;
7214 #endif
7215 #ifdef TARGET_NR_socket
7216 case TARGET_NR_socket:
7217 ret = do_socket(arg1, arg2, arg3);
7218 break;
7219 #endif
7220 #ifdef TARGET_NR_socketpair
7221 case TARGET_NR_socketpair:
7222 ret = do_socketpair(arg1, arg2, arg3, arg4);
7223 break;
7224 #endif
7225 #ifdef TARGET_NR_setsockopt
7226 case TARGET_NR_setsockopt:
7227 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
7228 break;
7229 #endif
7231 case TARGET_NR_syslog:
7232 if (!(p = lock_user_string(arg2)))
7233 goto efault;
7234 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
7235 unlock_user(p, arg2, 0);
7236 break;
7238 case TARGET_NR_setitimer:
7240 struct itimerval value, ovalue, *pvalue;
7242 if (arg2) {
7243 pvalue = &value;
7244 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
7245 || copy_from_user_timeval(&pvalue->it_value,
7246 arg2 + sizeof(struct target_timeval)))
7247 goto efault;
7248 } else {
7249 pvalue = NULL;
7251 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
7252 if (!is_error(ret) && arg3) {
7253 if (copy_to_user_timeval(arg3,
7254 &ovalue.it_interval)
7255 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
7256 &ovalue.it_value))
7257 goto efault;
7260 break;
7261 case TARGET_NR_getitimer:
7263 struct itimerval value;
7265 ret = get_errno(getitimer(arg1, &value));
7266 if (!is_error(ret) && arg2) {
7267 if (copy_to_user_timeval(arg2,
7268 &value.it_interval)
7269 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
7270 &value.it_value))
7271 goto efault;
7274 break;
7275 case TARGET_NR_stat:
7276 if (!(p = lock_user_string(arg1)))
7277 goto efault;
7278 ret = get_errno(stat(path(p), &st));
7279 unlock_user(p, arg1, 0);
7280 goto do_stat;
7281 case TARGET_NR_lstat:
7282 if (!(p = lock_user_string(arg1)))
7283 goto efault;
7284 ret = get_errno(lstat(path(p), &st));
7285 unlock_user(p, arg1, 0);
7286 goto do_stat;
7287 case TARGET_NR_fstat:
7289 ret = get_errno(fstat(arg1, &st));
7290 do_stat:
7291 if (!is_error(ret)) {
7292 struct target_stat *target_st;
7294 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
7295 goto efault;
7296 memset(target_st, 0, sizeof(*target_st));
7297 __put_user(st.st_dev, &target_st->st_dev);
7298 __put_user(st.st_ino, &target_st->st_ino);
7299 __put_user(st.st_mode, &target_st->st_mode);
7300 __put_user(st.st_uid, &target_st->st_uid);
7301 __put_user(st.st_gid, &target_st->st_gid);
7302 __put_user(st.st_nlink, &target_st->st_nlink);
7303 __put_user(st.st_rdev, &target_st->st_rdev);
7304 __put_user(st.st_size, &target_st->st_size);
7305 __put_user(st.st_blksize, &target_st->st_blksize);
7306 __put_user(st.st_blocks, &target_st->st_blocks);
7307 __put_user(st.st_atime, &target_st->target_st_atime);
7308 __put_user(st.st_mtime, &target_st->target_st_mtime);
7309 __put_user(st.st_ctime, &target_st->target_st_ctime);
7310 unlock_user_struct(target_st, arg2, 1);
7313 break;
7314 #ifdef TARGET_NR_olduname
7315 case TARGET_NR_olduname:
7316 goto unimplemented;
7317 #endif
7318 #ifdef TARGET_NR_iopl
7319 case TARGET_NR_iopl:
7320 goto unimplemented;
7321 #endif
7322 case TARGET_NR_vhangup:
7323 ret = get_errno(vhangup());
7324 break;
7325 #ifdef TARGET_NR_idle
7326 case TARGET_NR_idle:
7327 goto unimplemented;
7328 #endif
7329 #ifdef TARGET_NR_syscall
7330 case TARGET_NR_syscall:
7331 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
7332 arg6, arg7, arg8, 0);
7333 break;
7334 #endif
7335 case TARGET_NR_wait4:
7337 int status;
7338 abi_long status_ptr = arg2;
7339 struct rusage rusage, *rusage_ptr;
7340 abi_ulong target_rusage = arg4;
7341 abi_long rusage_err;
7342 if (target_rusage)
7343 rusage_ptr = &rusage;
7344 else
7345 rusage_ptr = NULL;
7346 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
7347 if (!is_error(ret)) {
7348 if (status_ptr && ret) {
7349 status = host_to_target_waitstatus(status);
7350 if (put_user_s32(status, status_ptr))
7351 goto efault;
7353 if (target_rusage) {
7354 rusage_err = host_to_target_rusage(target_rusage, &rusage);
7355 if (rusage_err) {
7356 ret = rusage_err;
7361 break;
7362 #ifdef TARGET_NR_swapoff
7363 case TARGET_NR_swapoff:
7364 if (!(p = lock_user_string(arg1)))
7365 goto efault;
7366 ret = get_errno(swapoff(p));
7367 unlock_user(p, arg1, 0);
7368 break;
7369 #endif
7370 case TARGET_NR_sysinfo:
7372 struct target_sysinfo *target_value;
7373 struct sysinfo value;
7374 ret = get_errno(sysinfo(&value));
7375 if (!is_error(ret) && arg1)
7377 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
7378 goto efault;
7379 __put_user(value.uptime, &target_value->uptime);
7380 __put_user(value.loads[0], &target_value->loads[0]);
7381 __put_user(value.loads[1], &target_value->loads[1]);
7382 __put_user(value.loads[2], &target_value->loads[2]);
7383 __put_user(value.totalram, &target_value->totalram);
7384 __put_user(value.freeram, &target_value->freeram);
7385 __put_user(value.sharedram, &target_value->sharedram);
7386 __put_user(value.bufferram, &target_value->bufferram);
7387 __put_user(value.totalswap, &target_value->totalswap);
7388 __put_user(value.freeswap, &target_value->freeswap);
7389 __put_user(value.procs, &target_value->procs);
7390 __put_user(value.totalhigh, &target_value->totalhigh);
7391 __put_user(value.freehigh, &target_value->freehigh);
7392 __put_user(value.mem_unit, &target_value->mem_unit);
7393 unlock_user_struct(target_value, arg1, 1);
7396 break;
7397 #ifdef TARGET_NR_ipc
7398 case TARGET_NR_ipc:
7399 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
7400 break;
7401 #endif
7402 #ifdef TARGET_NR_semget
7403 case TARGET_NR_semget:
7404 ret = get_errno(semget(arg1, arg2, arg3));
7405 break;
7406 #endif
7407 #ifdef TARGET_NR_semop
7408 case TARGET_NR_semop:
7409 ret = do_semop(arg1, arg2, arg3);
7410 break;
7411 #endif
7412 #ifdef TARGET_NR_semctl
7413 case TARGET_NR_semctl:
7414 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
7415 break;
7416 #endif
7417 #ifdef TARGET_NR_msgctl
7418 case TARGET_NR_msgctl:
7419 ret = do_msgctl(arg1, arg2, arg3);
7420 break;
7421 #endif
7422 #ifdef TARGET_NR_msgget
7423 case TARGET_NR_msgget:
7424 ret = get_errno(msgget(arg1, arg2));
7425 break;
7426 #endif
7427 #ifdef TARGET_NR_msgrcv
7428 case TARGET_NR_msgrcv:
7429 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
7430 break;
7431 #endif
7432 #ifdef TARGET_NR_msgsnd
7433 case TARGET_NR_msgsnd:
7434 ret = do_msgsnd(arg1, arg2, arg3, arg4);
7435 break;
7436 #endif
7437 #ifdef TARGET_NR_shmget
7438 case TARGET_NR_shmget:
7439 ret = get_errno(shmget(arg1, arg2, arg3));
7440 break;
7441 #endif
7442 #ifdef TARGET_NR_shmctl
7443 case TARGET_NR_shmctl:
7444 ret = do_shmctl(arg1, arg2, arg3);
7445 break;
7446 #endif
7447 #ifdef TARGET_NR_shmat
7448 case TARGET_NR_shmat:
7449 ret = do_shmat(arg1, arg2, arg3);
7450 break;
7451 #endif
7452 #ifdef TARGET_NR_shmdt
7453 case TARGET_NR_shmdt:
7454 ret = do_shmdt(arg1);
7455 break;
7456 #endif
7457 case TARGET_NR_fsync:
7458 ret = get_errno(fsync(arg1));
7459 break;
7460 case TARGET_NR_clone:
7461 /* Linux manages to have three different orderings for its
7462 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7463 * match the kernel's CONFIG_CLONE_* settings.
7464 * Microblaze is further special in that it uses a sixth
7465 * implicit argument to clone for the TLS pointer.
7467 #if defined(TARGET_MICROBLAZE)
7468 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
7469 #elif defined(TARGET_CLONE_BACKWARDS)
7470 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
7471 #elif defined(TARGET_CLONE_BACKWARDS2)
7472 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
7473 #else
7474 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
7475 #endif
7476 break;
7477 #ifdef __NR_exit_group
7478 /* new thread calls */
7479 case TARGET_NR_exit_group:
7480 #ifdef TARGET_GPROF
7481 _mcleanup();
7482 #endif
7483 gdb_exit(cpu_env, arg1);
7484 ret = get_errno(exit_group(arg1));
7485 break;
7486 #endif
7487 case TARGET_NR_setdomainname:
7488 if (!(p = lock_user_string(arg1)))
7489 goto efault;
7490 ret = get_errno(setdomainname(p, arg2));
7491 unlock_user(p, arg1, 0);
7492 break;
7493 case TARGET_NR_uname:
7494 /* no need to transcode because we use the linux syscall */
7496 struct new_utsname * buf;
7498 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
7499 goto efault;
7500 ret = get_errno(sys_uname(buf));
7501 if (!is_error(ret)) {
7502 /* Overrite the native machine name with whatever is being
7503 emulated. */
7504 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
7505 /* Allow the user to override the reported release. */
7506 if (qemu_uname_release && *qemu_uname_release)
7507 strcpy (buf->release, qemu_uname_release);
7509 unlock_user_struct(buf, arg1, 1);
7511 break;
7512 #ifdef TARGET_I386
7513 case TARGET_NR_modify_ldt:
7514 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
7515 break;
7516 #if !defined(TARGET_X86_64)
7517 case TARGET_NR_vm86old:
7518 goto unimplemented;
7519 case TARGET_NR_vm86:
7520 ret = do_vm86(cpu_env, arg1, arg2);
7521 break;
7522 #endif
7523 #endif
7524 case TARGET_NR_adjtimex:
7525 goto unimplemented;
7526 #ifdef TARGET_NR_create_module
7527 case TARGET_NR_create_module:
7528 #endif
7529 case TARGET_NR_init_module:
7530 case TARGET_NR_delete_module:
7531 #ifdef TARGET_NR_get_kernel_syms
7532 case TARGET_NR_get_kernel_syms:
7533 #endif
7534 goto unimplemented;
7535 case TARGET_NR_quotactl:
7536 goto unimplemented;
7537 case TARGET_NR_getpgid:
7538 ret = get_errno(getpgid(arg1));
7539 break;
7540 case TARGET_NR_fchdir:
7541 ret = get_errno(fchdir(arg1));
7542 break;
7543 #ifdef TARGET_NR_bdflush /* not on x86_64 */
7544 case TARGET_NR_bdflush:
7545 goto unimplemented;
7546 #endif
7547 #ifdef TARGET_NR_sysfs
7548 case TARGET_NR_sysfs:
7549 goto unimplemented;
7550 #endif
7551 case TARGET_NR_personality:
7552 ret = get_errno(personality(arg1));
7553 break;
7554 #ifdef TARGET_NR_afs_syscall
7555 case TARGET_NR_afs_syscall:
7556 goto unimplemented;
7557 #endif
7558 #ifdef TARGET_NR__llseek /* Not on alpha */
7559 case TARGET_NR__llseek:
7561 int64_t res;
7562 #if !defined(__NR_llseek)
7563 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
7564 if (res == -1) {
7565 ret = get_errno(res);
7566 } else {
7567 ret = 0;
7569 #else
7570 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
7571 #endif
7572 if ((ret == 0) && put_user_s64(res, arg4)) {
7573 goto efault;
7576 break;
7577 #endif
7578 case TARGET_NR_getdents:
7579 #ifdef __NR_getdents
7580 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
7582 struct target_dirent *target_dirp;
7583 struct linux_dirent *dirp;
7584 abi_long count = arg3;
7586 dirp = malloc(count);
7587 if (!dirp) {
7588 ret = -TARGET_ENOMEM;
7589 goto fail;
7592 ret = get_errno(sys_getdents(arg1, dirp, count));
7593 if (!is_error(ret)) {
7594 struct linux_dirent *de;
7595 struct target_dirent *tde;
7596 int len = ret;
7597 int reclen, treclen;
7598 int count1, tnamelen;
7600 count1 = 0;
7601 de = dirp;
7602 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7603 goto efault;
7604 tde = target_dirp;
7605 while (len > 0) {
7606 reclen = de->d_reclen;
7607 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
7608 assert(tnamelen >= 0);
7609 treclen = tnamelen + offsetof(struct target_dirent, d_name);
7610 assert(count1 + treclen <= count);
7611 tde->d_reclen = tswap16(treclen);
7612 tde->d_ino = tswapal(de->d_ino);
7613 tde->d_off = tswapal(de->d_off);
7614 memcpy(tde->d_name, de->d_name, tnamelen);
7615 de = (struct linux_dirent *)((char *)de + reclen);
7616 len -= reclen;
7617 tde = (struct target_dirent *)((char *)tde + treclen);
7618 count1 += treclen;
7620 ret = count1;
7621 unlock_user(target_dirp, arg2, ret);
7623 free(dirp);
7625 #else
7627 struct linux_dirent *dirp;
7628 abi_long count = arg3;
7630 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7631 goto efault;
7632 ret = get_errno(sys_getdents(arg1, dirp, count));
7633 if (!is_error(ret)) {
7634 struct linux_dirent *de;
7635 int len = ret;
7636 int reclen;
7637 de = dirp;
7638 while (len > 0) {
7639 reclen = de->d_reclen;
7640 if (reclen > len)
7641 break;
7642 de->d_reclen = tswap16(reclen);
7643 tswapls(&de->d_ino);
7644 tswapls(&de->d_off);
7645 de = (struct linux_dirent *)((char *)de + reclen);
7646 len -= reclen;
7649 unlock_user(dirp, arg2, ret);
7651 #endif
7652 #else
7653 /* Implement getdents in terms of getdents64 */
7655 struct linux_dirent64 *dirp;
7656 abi_long count = arg3;
7658 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
7659 if (!dirp) {
7660 goto efault;
7662 ret = get_errno(sys_getdents64(arg1, dirp, count));
7663 if (!is_error(ret)) {
7664 /* Convert the dirent64 structs to target dirent. We do this
7665 * in-place, since we can guarantee that a target_dirent is no
7666 * larger than a dirent64; however this means we have to be
7667 * careful to read everything before writing in the new format.
7669 struct linux_dirent64 *de;
7670 struct target_dirent *tde;
7671 int len = ret;
7672 int tlen = 0;
7674 de = dirp;
7675 tde = (struct target_dirent *)dirp;
7676 while (len > 0) {
7677 int namelen, treclen;
7678 int reclen = de->d_reclen;
7679 uint64_t ino = de->d_ino;
7680 int64_t off = de->d_off;
7681 uint8_t type = de->d_type;
7683 namelen = strlen(de->d_name);
7684 treclen = offsetof(struct target_dirent, d_name)
7685 + namelen + 2;
7686 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
7688 memmove(tde->d_name, de->d_name, namelen + 1);
7689 tde->d_ino = tswapal(ino);
7690 tde->d_off = tswapal(off);
7691 tde->d_reclen = tswap16(treclen);
7692 /* The target_dirent type is in what was formerly a padding
7693 * byte at the end of the structure:
7695 *(((char *)tde) + treclen - 1) = type;
7697 de = (struct linux_dirent64 *)((char *)de + reclen);
7698 tde = (struct target_dirent *)((char *)tde + treclen);
7699 len -= reclen;
7700 tlen += treclen;
7702 ret = tlen;
7704 unlock_user(dirp, arg2, ret);
7706 #endif
7707 break;
7708 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
7709 case TARGET_NR_getdents64:
7711 struct linux_dirent64 *dirp;
7712 abi_long count = arg3;
7713 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7714 goto efault;
7715 ret = get_errno(sys_getdents64(arg1, dirp, count));
7716 if (!is_error(ret)) {
7717 struct linux_dirent64 *de;
7718 int len = ret;
7719 int reclen;
7720 de = dirp;
7721 while (len > 0) {
7722 reclen = de->d_reclen;
7723 if (reclen > len)
7724 break;
7725 de->d_reclen = tswap16(reclen);
7726 tswap64s((uint64_t *)&de->d_ino);
7727 tswap64s((uint64_t *)&de->d_off);
7728 de = (struct linux_dirent64 *)((char *)de + reclen);
7729 len -= reclen;
7732 unlock_user(dirp, arg2, ret);
7734 break;
7735 #endif /* TARGET_NR_getdents64 */
7736 #if defined(TARGET_NR__newselect)
7737 case TARGET_NR__newselect:
7738 ret = do_select(arg1, arg2, arg3, arg4, arg5);
7739 break;
7740 #endif
7741 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7742 # ifdef TARGET_NR_poll
7743 case TARGET_NR_poll:
7744 # endif
7745 # ifdef TARGET_NR_ppoll
7746 case TARGET_NR_ppoll:
7747 # endif
7749 struct target_pollfd *target_pfd;
7750 unsigned int nfds = arg2;
7751 int timeout = arg3;
7752 struct pollfd *pfd;
7753 unsigned int i;
7755 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
7756 if (!target_pfd)
7757 goto efault;
7759 pfd = alloca(sizeof(struct pollfd) * nfds);
7760 for(i = 0; i < nfds; i++) {
7761 pfd[i].fd = tswap32(target_pfd[i].fd);
7762 pfd[i].events = tswap16(target_pfd[i].events);
7765 # ifdef TARGET_NR_ppoll
7766 if (num == TARGET_NR_ppoll) {
7767 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
7768 target_sigset_t *target_set;
7769 sigset_t _set, *set = &_set;
7771 if (arg3) {
7772 if (target_to_host_timespec(timeout_ts, arg3)) {
7773 unlock_user(target_pfd, arg1, 0);
7774 goto efault;
7776 } else {
7777 timeout_ts = NULL;
7780 if (arg4) {
7781 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
7782 if (!target_set) {
7783 unlock_user(target_pfd, arg1, 0);
7784 goto efault;
7786 target_to_host_sigset(set, target_set);
7787 } else {
7788 set = NULL;
7791 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
7793 if (!is_error(ret) && arg3) {
7794 host_to_target_timespec(arg3, timeout_ts);
7796 if (arg4) {
7797 unlock_user(target_set, arg4, 0);
7799 } else
7800 # endif
7801 ret = get_errno(poll(pfd, nfds, timeout));
7803 if (!is_error(ret)) {
7804 for(i = 0; i < nfds; i++) {
7805 target_pfd[i].revents = tswap16(pfd[i].revents);
7808 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
7810 break;
7811 #endif
7812 case TARGET_NR_flock:
7813 /* NOTE: the flock constant seems to be the same for every
7814 Linux platform */
7815 ret = get_errno(flock(arg1, arg2));
7816 break;
7817 case TARGET_NR_readv:
7819 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
7820 if (vec != NULL) {
7821 ret = get_errno(readv(arg1, vec, arg3));
7822 unlock_iovec(vec, arg2, arg3, 1);
7823 } else {
7824 ret = -host_to_target_errno(errno);
7827 break;
7828 case TARGET_NR_writev:
7830 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
7831 if (vec != NULL) {
7832 ret = get_errno(writev(arg1, vec, arg3));
7833 unlock_iovec(vec, arg2, arg3, 0);
7834 } else {
7835 ret = -host_to_target_errno(errno);
7838 break;
7839 case TARGET_NR_getsid:
7840 ret = get_errno(getsid(arg1));
7841 break;
7842 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
7843 case TARGET_NR_fdatasync:
7844 ret = get_errno(fdatasync(arg1));
7845 break;
7846 #endif
7847 case TARGET_NR__sysctl:
7848 /* We don't implement this, but ENOTDIR is always a safe
7849 return value. */
7850 ret = -TARGET_ENOTDIR;
7851 break;
7852 case TARGET_NR_sched_getaffinity:
7854 unsigned int mask_size;
7855 unsigned long *mask;
7858 * sched_getaffinity needs multiples of ulong, so need to take
7859 * care of mismatches between target ulong and host ulong sizes.
7861 if (arg2 & (sizeof(abi_ulong) - 1)) {
7862 ret = -TARGET_EINVAL;
7863 break;
7865 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7867 mask = alloca(mask_size);
7868 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
7870 if (!is_error(ret)) {
7871 if (ret > arg2) {
7872 /* More data returned than the caller's buffer will fit.
7873 * This only happens if sizeof(abi_long) < sizeof(long)
7874 * and the caller passed us a buffer holding an odd number
7875 * of abi_longs. If the host kernel is actually using the
7876 * extra 4 bytes then fail EINVAL; otherwise we can just
7877 * ignore them and only copy the interesting part.
7879 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
7880 if (numcpus > arg2 * 8) {
7881 ret = -TARGET_EINVAL;
7882 break;
7884 ret = arg2;
7887 if (copy_to_user(arg3, mask, ret)) {
7888 goto efault;
7892 break;
7893 case TARGET_NR_sched_setaffinity:
7895 unsigned int mask_size;
7896 unsigned long *mask;
7899 * sched_setaffinity needs multiples of ulong, so need to take
7900 * care of mismatches between target ulong and host ulong sizes.
7902 if (arg2 & (sizeof(abi_ulong) - 1)) {
7903 ret = -TARGET_EINVAL;
7904 break;
7906 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7908 mask = alloca(mask_size);
7909 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
7910 goto efault;
7912 memcpy(mask, p, arg2);
7913 unlock_user_struct(p, arg2, 0);
7915 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
7917 break;
7918 case TARGET_NR_sched_setparam:
7920 struct sched_param *target_schp;
7921 struct sched_param schp;
7923 if (arg2 == 0) {
7924 return -TARGET_EINVAL;
7926 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
7927 goto efault;
7928 schp.sched_priority = tswap32(target_schp->sched_priority);
7929 unlock_user_struct(target_schp, arg2, 0);
7930 ret = get_errno(sched_setparam(arg1, &schp));
7932 break;
7933 case TARGET_NR_sched_getparam:
7935 struct sched_param *target_schp;
7936 struct sched_param schp;
7938 if (arg2 == 0) {
7939 return -TARGET_EINVAL;
7941 ret = get_errno(sched_getparam(arg1, &schp));
7942 if (!is_error(ret)) {
7943 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
7944 goto efault;
7945 target_schp->sched_priority = tswap32(schp.sched_priority);
7946 unlock_user_struct(target_schp, arg2, 1);
7949 break;
7950 case TARGET_NR_sched_setscheduler:
7952 struct sched_param *target_schp;
7953 struct sched_param schp;
7954 if (arg3 == 0) {
7955 return -TARGET_EINVAL;
7957 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
7958 goto efault;
7959 schp.sched_priority = tswap32(target_schp->sched_priority);
7960 unlock_user_struct(target_schp, arg3, 0);
7961 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
7963 break;
7964 case TARGET_NR_sched_getscheduler:
7965 ret = get_errno(sched_getscheduler(arg1));
7966 break;
7967 case TARGET_NR_sched_yield:
7968 ret = get_errno(sched_yield());
7969 break;
7970 case TARGET_NR_sched_get_priority_max:
7971 ret = get_errno(sched_get_priority_max(arg1));
7972 break;
7973 case TARGET_NR_sched_get_priority_min:
7974 ret = get_errno(sched_get_priority_min(arg1));
7975 break;
7976 case TARGET_NR_sched_rr_get_interval:
7978 struct timespec ts;
7979 ret = get_errno(sched_rr_get_interval(arg1, &ts));
7980 if (!is_error(ret)) {
7981 ret = host_to_target_timespec(arg2, &ts);
7984 break;
7985 case TARGET_NR_nanosleep:
7987 struct timespec req, rem;
7988 target_to_host_timespec(&req, arg1);
7989 ret = get_errno(nanosleep(&req, &rem));
7990 if (is_error(ret) && arg2) {
7991 host_to_target_timespec(arg2, &rem);
7994 break;
7995 #ifdef TARGET_NR_query_module
7996 case TARGET_NR_query_module:
7997 goto unimplemented;
7998 #endif
7999 #ifdef TARGET_NR_nfsservctl
8000 case TARGET_NR_nfsservctl:
8001 goto unimplemented;
8002 #endif
8003 case TARGET_NR_prctl:
8004 switch (arg1) {
8005 case PR_GET_PDEATHSIG:
8007 int deathsig;
8008 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
8009 if (!is_error(ret) && arg2
8010 && put_user_ual(deathsig, arg2)) {
8011 goto efault;
8013 break;
8015 #ifdef PR_GET_NAME
8016 case PR_GET_NAME:
8018 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
8019 if (!name) {
8020 goto efault;
8022 ret = get_errno(prctl(arg1, (unsigned long)name,
8023 arg3, arg4, arg5));
8024 unlock_user(name, arg2, 16);
8025 break;
8027 case PR_SET_NAME:
8029 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
8030 if (!name) {
8031 goto efault;
8033 ret = get_errno(prctl(arg1, (unsigned long)name,
8034 arg3, arg4, arg5));
8035 unlock_user(name, arg2, 0);
8036 break;
8038 #endif
8039 default:
8040 /* Most prctl options have no pointer arguments */
8041 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
8042 break;
8044 break;
8045 #ifdef TARGET_NR_arch_prctl
8046 case TARGET_NR_arch_prctl:
8047 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
8048 ret = do_arch_prctl(cpu_env, arg1, arg2);
8049 break;
8050 #else
8051 goto unimplemented;
8052 #endif
8053 #endif
8054 #ifdef TARGET_NR_pread64
8055 case TARGET_NR_pread64:
8056 if (regpairs_aligned(cpu_env)) {
8057 arg4 = arg5;
8058 arg5 = arg6;
8060 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
8061 goto efault;
8062 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
8063 unlock_user(p, arg2, ret);
8064 break;
8065 case TARGET_NR_pwrite64:
8066 if (regpairs_aligned(cpu_env)) {
8067 arg4 = arg5;
8068 arg5 = arg6;
8070 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
8071 goto efault;
8072 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
8073 unlock_user(p, arg2, 0);
8074 break;
8075 #endif
8076 case TARGET_NR_getcwd:
8077 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
8078 goto efault;
8079 ret = get_errno(sys_getcwd1(p, arg2));
8080 unlock_user(p, arg1, ret);
8081 break;
8082 case TARGET_NR_capget:
8083 case TARGET_NR_capset:
8085 struct target_user_cap_header *target_header;
8086 struct target_user_cap_data *target_data = NULL;
8087 struct __user_cap_header_struct header;
8088 struct __user_cap_data_struct data[2];
8089 struct __user_cap_data_struct *dataptr = NULL;
8090 int i, target_datalen;
8091 int data_items = 1;
8093 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
8094 goto efault;
8096 header.version = tswap32(target_header->version);
8097 header.pid = tswap32(target_header->pid);
8099 if (header.version != _LINUX_CAPABILITY_VERSION) {
8100 /* Version 2 and up takes pointer to two user_data structs */
8101 data_items = 2;
8104 target_datalen = sizeof(*target_data) * data_items;
8106 if (arg2) {
8107 if (num == TARGET_NR_capget) {
8108 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
8109 } else {
8110 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
8112 if (!target_data) {
8113 unlock_user_struct(target_header, arg1, 0);
8114 goto efault;
8117 if (num == TARGET_NR_capset) {
8118 for (i = 0; i < data_items; i++) {
8119 data[i].effective = tswap32(target_data[i].effective);
8120 data[i].permitted = tswap32(target_data[i].permitted);
8121 data[i].inheritable = tswap32(target_data[i].inheritable);
8125 dataptr = data;
8128 if (num == TARGET_NR_capget) {
8129 ret = get_errno(capget(&header, dataptr));
8130 } else {
8131 ret = get_errno(capset(&header, dataptr));
8134 /* The kernel always updates version for both capget and capset */
8135 target_header->version = tswap32(header.version);
8136 unlock_user_struct(target_header, arg1, 1);
8138 if (arg2) {
8139 if (num == TARGET_NR_capget) {
8140 for (i = 0; i < data_items; i++) {
8141 target_data[i].effective = tswap32(data[i].effective);
8142 target_data[i].permitted = tswap32(data[i].permitted);
8143 target_data[i].inheritable = tswap32(data[i].inheritable);
8145 unlock_user(target_data, arg2, target_datalen);
8146 } else {
8147 unlock_user(target_data, arg2, 0);
8150 break;
8152 case TARGET_NR_sigaltstack:
8153 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
8154 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
8155 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
8156 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
8157 break;
8158 #else
8159 goto unimplemented;
8160 #endif
8162 #ifdef CONFIG_SENDFILE
8163 case TARGET_NR_sendfile:
8165 off_t *offp = NULL;
8166 off_t off;
8167 if (arg3) {
8168 ret = get_user_sal(off, arg3);
8169 if (is_error(ret)) {
8170 break;
8172 offp = &off;
8174 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
8175 if (!is_error(ret) && arg3) {
8176 abi_long ret2 = put_user_sal(off, arg3);
8177 if (is_error(ret2)) {
8178 ret = ret2;
8181 break;
8183 #ifdef TARGET_NR_sendfile64
8184 case TARGET_NR_sendfile64:
8186 off_t *offp = NULL;
8187 off_t off;
8188 if (arg3) {
8189 ret = get_user_s64(off, arg3);
8190 if (is_error(ret)) {
8191 break;
8193 offp = &off;
8195 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
8196 if (!is_error(ret) && arg3) {
8197 abi_long ret2 = put_user_s64(off, arg3);
8198 if (is_error(ret2)) {
8199 ret = ret2;
8202 break;
8204 #endif
8205 #else
8206 case TARGET_NR_sendfile:
8207 #ifdef TARGET_NR_sendfile64
8208 case TARGET_NR_sendfile64:
8209 #endif
8210 goto unimplemented;
8211 #endif
8213 #ifdef TARGET_NR_getpmsg
8214 case TARGET_NR_getpmsg:
8215 goto unimplemented;
8216 #endif
8217 #ifdef TARGET_NR_putpmsg
8218 case TARGET_NR_putpmsg:
8219 goto unimplemented;
8220 #endif
8221 #ifdef TARGET_NR_vfork
8222 case TARGET_NR_vfork:
8223 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
8224 0, 0, 0, 0));
8225 break;
8226 #endif
8227 #ifdef TARGET_NR_ugetrlimit
8228 case TARGET_NR_ugetrlimit:
8230 struct rlimit rlim;
8231 int resource = target_to_host_resource(arg1);
8232 ret = get_errno(getrlimit(resource, &rlim));
8233 if (!is_error(ret)) {
8234 struct target_rlimit *target_rlim;
8235 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
8236 goto efault;
8237 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
8238 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
8239 unlock_user_struct(target_rlim, arg2, 1);
8241 break;
8243 #endif
8244 #ifdef TARGET_NR_truncate64
8245 case TARGET_NR_truncate64:
8246 if (!(p = lock_user_string(arg1)))
8247 goto efault;
8248 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
8249 unlock_user(p, arg1, 0);
8250 break;
8251 #endif
8252 #ifdef TARGET_NR_ftruncate64
8253 case TARGET_NR_ftruncate64:
8254 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
8255 break;
8256 #endif
8257 #ifdef TARGET_NR_stat64
8258 case TARGET_NR_stat64:
8259 if (!(p = lock_user_string(arg1)))
8260 goto efault;
8261 ret = get_errno(stat(path(p), &st));
8262 unlock_user(p, arg1, 0);
8263 if (!is_error(ret))
8264 ret = host_to_target_stat64(cpu_env, arg2, &st);
8265 break;
8266 #endif
8267 #ifdef TARGET_NR_lstat64
8268 case TARGET_NR_lstat64:
8269 if (!(p = lock_user_string(arg1)))
8270 goto efault;
8271 ret = get_errno(lstat(path(p), &st));
8272 unlock_user(p, arg1, 0);
8273 if (!is_error(ret))
8274 ret = host_to_target_stat64(cpu_env, arg2, &st);
8275 break;
8276 #endif
8277 #ifdef TARGET_NR_fstat64
8278 case TARGET_NR_fstat64:
8279 ret = get_errno(fstat(arg1, &st));
8280 if (!is_error(ret))
8281 ret = host_to_target_stat64(cpu_env, arg2, &st);
8282 break;
8283 #endif
8284 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
8285 #ifdef TARGET_NR_fstatat64
8286 case TARGET_NR_fstatat64:
8287 #endif
8288 #ifdef TARGET_NR_newfstatat
8289 case TARGET_NR_newfstatat:
8290 #endif
8291 if (!(p = lock_user_string(arg2)))
8292 goto efault;
8293 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
8294 if (!is_error(ret))
8295 ret = host_to_target_stat64(cpu_env, arg3, &st);
8296 break;
8297 #endif
8298 case TARGET_NR_lchown:
8299 if (!(p = lock_user_string(arg1)))
8300 goto efault;
8301 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
8302 unlock_user(p, arg1, 0);
8303 break;
8304 #ifdef TARGET_NR_getuid
8305 case TARGET_NR_getuid:
8306 ret = get_errno(high2lowuid(getuid()));
8307 break;
8308 #endif
8309 #ifdef TARGET_NR_getgid
8310 case TARGET_NR_getgid:
8311 ret = get_errno(high2lowgid(getgid()));
8312 break;
8313 #endif
8314 #ifdef TARGET_NR_geteuid
8315 case TARGET_NR_geteuid:
8316 ret = get_errno(high2lowuid(geteuid()));
8317 break;
8318 #endif
8319 #ifdef TARGET_NR_getegid
8320 case TARGET_NR_getegid:
8321 ret = get_errno(high2lowgid(getegid()));
8322 break;
8323 #endif
8324 case TARGET_NR_setreuid:
8325 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
8326 break;
8327 case TARGET_NR_setregid:
8328 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
8329 break;
8330 case TARGET_NR_getgroups:
8332 int gidsetsize = arg1;
8333 target_id *target_grouplist;
8334 gid_t *grouplist;
8335 int i;
8337 grouplist = alloca(gidsetsize * sizeof(gid_t));
8338 ret = get_errno(getgroups(gidsetsize, grouplist));
8339 if (gidsetsize == 0)
8340 break;
8341 if (!is_error(ret)) {
8342 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
8343 if (!target_grouplist)
8344 goto efault;
8345 for(i = 0;i < ret; i++)
8346 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
8347 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
8350 break;
8351 case TARGET_NR_setgroups:
8353 int gidsetsize = arg1;
8354 target_id *target_grouplist;
8355 gid_t *grouplist = NULL;
8356 int i;
8357 if (gidsetsize) {
8358 grouplist = alloca(gidsetsize * sizeof(gid_t));
8359 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
8360 if (!target_grouplist) {
8361 ret = -TARGET_EFAULT;
8362 goto fail;
8364 for (i = 0; i < gidsetsize; i++) {
8365 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
8367 unlock_user(target_grouplist, arg2, 0);
8369 ret = get_errno(setgroups(gidsetsize, grouplist));
8371 break;
8372 case TARGET_NR_fchown:
8373 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
8374 break;
8375 #if defined(TARGET_NR_fchownat)
8376 case TARGET_NR_fchownat:
8377 if (!(p = lock_user_string(arg2)))
8378 goto efault;
8379 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
8380 low2highgid(arg4), arg5));
8381 unlock_user(p, arg2, 0);
8382 break;
8383 #endif
8384 #ifdef TARGET_NR_setresuid
8385 case TARGET_NR_setresuid:
8386 ret = get_errno(setresuid(low2highuid(arg1),
8387 low2highuid(arg2),
8388 low2highuid(arg3)));
8389 break;
8390 #endif
8391 #ifdef TARGET_NR_getresuid
8392 case TARGET_NR_getresuid:
8394 uid_t ruid, euid, suid;
8395 ret = get_errno(getresuid(&ruid, &euid, &suid));
8396 if (!is_error(ret)) {
8397 if (put_user_id(high2lowuid(ruid), arg1)
8398 || put_user_id(high2lowuid(euid), arg2)
8399 || put_user_id(high2lowuid(suid), arg3))
8400 goto efault;
8403 break;
8404 #endif
8405 #ifdef TARGET_NR_getresgid
8406 case TARGET_NR_setresgid:
8407 ret = get_errno(setresgid(low2highgid(arg1),
8408 low2highgid(arg2),
8409 low2highgid(arg3)));
8410 break;
8411 #endif
8412 #ifdef TARGET_NR_getresgid
8413 case TARGET_NR_getresgid:
8415 gid_t rgid, egid, sgid;
8416 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8417 if (!is_error(ret)) {
8418 if (put_user_id(high2lowgid(rgid), arg1)
8419 || put_user_id(high2lowgid(egid), arg2)
8420 || put_user_id(high2lowgid(sgid), arg3))
8421 goto efault;
8424 break;
8425 #endif
8426 case TARGET_NR_chown:
8427 if (!(p = lock_user_string(arg1)))
8428 goto efault;
8429 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
8430 unlock_user(p, arg1, 0);
8431 break;
8432 case TARGET_NR_setuid:
8433 ret = get_errno(setuid(low2highuid(arg1)));
8434 break;
8435 case TARGET_NR_setgid:
8436 ret = get_errno(setgid(low2highgid(arg1)));
8437 break;
8438 case TARGET_NR_setfsuid:
8439 ret = get_errno(setfsuid(arg1));
8440 break;
8441 case TARGET_NR_setfsgid:
8442 ret = get_errno(setfsgid(arg1));
8443 break;
8445 #ifdef TARGET_NR_lchown32
8446 case TARGET_NR_lchown32:
8447 if (!(p = lock_user_string(arg1)))
8448 goto efault;
8449 ret = get_errno(lchown(p, arg2, arg3));
8450 unlock_user(p, arg1, 0);
8451 break;
8452 #endif
8453 #ifdef TARGET_NR_getuid32
8454 case TARGET_NR_getuid32:
8455 ret = get_errno(getuid());
8456 break;
8457 #endif
8459 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
8460 /* Alpha specific */
8461 case TARGET_NR_getxuid:
8463 uid_t euid;
8464 euid=geteuid();
8465 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
8467 ret = get_errno(getuid());
8468 break;
8469 #endif
8470 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
8471 /* Alpha specific */
8472 case TARGET_NR_getxgid:
8474 uid_t egid;
8475 egid=getegid();
8476 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
8478 ret = get_errno(getgid());
8479 break;
8480 #endif
8481 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
8482 /* Alpha specific */
8483 case TARGET_NR_osf_getsysinfo:
8484 ret = -TARGET_EOPNOTSUPP;
8485 switch (arg1) {
8486 case TARGET_GSI_IEEE_FP_CONTROL:
8488 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
8490 /* Copied from linux ieee_fpcr_to_swcr. */
8491 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
8492 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
8493 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
8494 | SWCR_TRAP_ENABLE_DZE
8495 | SWCR_TRAP_ENABLE_OVF);
8496 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
8497 | SWCR_TRAP_ENABLE_INE);
8498 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
8499 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
8501 if (put_user_u64 (swcr, arg2))
8502 goto efault;
8503 ret = 0;
8505 break;
8507 /* case GSI_IEEE_STATE_AT_SIGNAL:
8508 -- Not implemented in linux kernel.
8509 case GSI_UACPROC:
8510 -- Retrieves current unaligned access state; not much used.
8511 case GSI_PROC_TYPE:
8512 -- Retrieves implver information; surely not used.
8513 case GSI_GET_HWRPB:
8514 -- Grabs a copy of the HWRPB; surely not used.
8517 break;
8518 #endif
8519 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8520 /* Alpha specific */
8521 case TARGET_NR_osf_setsysinfo:
8522 ret = -TARGET_EOPNOTSUPP;
8523 switch (arg1) {
8524 case TARGET_SSI_IEEE_FP_CONTROL:
8526 uint64_t swcr, fpcr, orig_fpcr;
8528 if (get_user_u64 (swcr, arg2)) {
8529 goto efault;
8531 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
8532 fpcr = orig_fpcr & FPCR_DYN_MASK;
8534 /* Copied from linux ieee_swcr_to_fpcr. */
8535 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
8536 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
8537 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
8538 | SWCR_TRAP_ENABLE_DZE
8539 | SWCR_TRAP_ENABLE_OVF)) << 48;
8540 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
8541 | SWCR_TRAP_ENABLE_INE)) << 57;
8542 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
8543 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
8545 cpu_alpha_store_fpcr(cpu_env, fpcr);
8546 ret = 0;
8548 break;
8550 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
8552 uint64_t exc, fpcr, orig_fpcr;
8553 int si_code;
8555 if (get_user_u64(exc, arg2)) {
8556 goto efault;
8559 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
8561 /* We only add to the exception status here. */
8562 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
8564 cpu_alpha_store_fpcr(cpu_env, fpcr);
8565 ret = 0;
8567 /* Old exceptions are not signaled. */
8568 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
8570 /* If any exceptions set by this call,
8571 and are unmasked, send a signal. */
8572 si_code = 0;
8573 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
8574 si_code = TARGET_FPE_FLTRES;
8576 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
8577 si_code = TARGET_FPE_FLTUND;
8579 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
8580 si_code = TARGET_FPE_FLTOVF;
8582 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
8583 si_code = TARGET_FPE_FLTDIV;
8585 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
8586 si_code = TARGET_FPE_FLTINV;
8588 if (si_code != 0) {
8589 target_siginfo_t info;
8590 info.si_signo = SIGFPE;
8591 info.si_errno = 0;
8592 info.si_code = si_code;
8593 info._sifields._sigfault._addr
8594 = ((CPUArchState *)cpu_env)->pc;
8595 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
8598 break;
8600 /* case SSI_NVPAIRS:
8601 -- Used with SSIN_UACPROC to enable unaligned accesses.
8602 case SSI_IEEE_STATE_AT_SIGNAL:
8603 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8604 -- Not implemented in linux kernel
8607 break;
8608 #endif
8609 #ifdef TARGET_NR_osf_sigprocmask
8610 /* Alpha specific. */
8611 case TARGET_NR_osf_sigprocmask:
8613 abi_ulong mask;
8614 int how;
8615 sigset_t set, oldset;
8617 switch(arg1) {
8618 case TARGET_SIG_BLOCK:
8619 how = SIG_BLOCK;
8620 break;
8621 case TARGET_SIG_UNBLOCK:
8622 how = SIG_UNBLOCK;
8623 break;
8624 case TARGET_SIG_SETMASK:
8625 how = SIG_SETMASK;
8626 break;
8627 default:
8628 ret = -TARGET_EINVAL;
8629 goto fail;
8631 mask = arg2;
8632 target_to_host_old_sigset(&set, &mask);
8633 do_sigprocmask(how, &set, &oldset);
8634 host_to_target_old_sigset(&mask, &oldset);
8635 ret = mask;
8637 break;
8638 #endif
8640 #ifdef TARGET_NR_getgid32
8641 case TARGET_NR_getgid32:
8642 ret = get_errno(getgid());
8643 break;
8644 #endif
8645 #ifdef TARGET_NR_geteuid32
8646 case TARGET_NR_geteuid32:
8647 ret = get_errno(geteuid());
8648 break;
8649 #endif
8650 #ifdef TARGET_NR_getegid32
8651 case TARGET_NR_getegid32:
8652 ret = get_errno(getegid());
8653 break;
8654 #endif
8655 #ifdef TARGET_NR_setreuid32
8656 case TARGET_NR_setreuid32:
8657 ret = get_errno(setreuid(arg1, arg2));
8658 break;
8659 #endif
8660 #ifdef TARGET_NR_setregid32
8661 case TARGET_NR_setregid32:
8662 ret = get_errno(setregid(arg1, arg2));
8663 break;
8664 #endif
8665 #ifdef TARGET_NR_getgroups32
8666 case TARGET_NR_getgroups32:
8668 int gidsetsize = arg1;
8669 uint32_t *target_grouplist;
8670 gid_t *grouplist;
8671 int i;
8673 grouplist = alloca(gidsetsize * sizeof(gid_t));
8674 ret = get_errno(getgroups(gidsetsize, grouplist));
8675 if (gidsetsize == 0)
8676 break;
8677 if (!is_error(ret)) {
8678 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
8679 if (!target_grouplist) {
8680 ret = -TARGET_EFAULT;
8681 goto fail;
8683 for(i = 0;i < ret; i++)
8684 target_grouplist[i] = tswap32(grouplist[i]);
8685 unlock_user(target_grouplist, arg2, gidsetsize * 4);
8688 break;
8689 #endif
8690 #ifdef TARGET_NR_setgroups32
8691 case TARGET_NR_setgroups32:
8693 int gidsetsize = arg1;
8694 uint32_t *target_grouplist;
8695 gid_t *grouplist;
8696 int i;
8698 grouplist = alloca(gidsetsize * sizeof(gid_t));
8699 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
8700 if (!target_grouplist) {
8701 ret = -TARGET_EFAULT;
8702 goto fail;
8704 for(i = 0;i < gidsetsize; i++)
8705 grouplist[i] = tswap32(target_grouplist[i]);
8706 unlock_user(target_grouplist, arg2, 0);
8707 ret = get_errno(setgroups(gidsetsize, grouplist));
8709 break;
8710 #endif
8711 #ifdef TARGET_NR_fchown32
8712 case TARGET_NR_fchown32:
8713 ret = get_errno(fchown(arg1, arg2, arg3));
8714 break;
8715 #endif
8716 #ifdef TARGET_NR_setresuid32
8717 case TARGET_NR_setresuid32:
8718 ret = get_errno(setresuid(arg1, arg2, arg3));
8719 break;
8720 #endif
8721 #ifdef TARGET_NR_getresuid32
8722 case TARGET_NR_getresuid32:
8724 uid_t ruid, euid, suid;
8725 ret = get_errno(getresuid(&ruid, &euid, &suid));
8726 if (!is_error(ret)) {
8727 if (put_user_u32(ruid, arg1)
8728 || put_user_u32(euid, arg2)
8729 || put_user_u32(suid, arg3))
8730 goto efault;
8733 break;
8734 #endif
8735 #ifdef TARGET_NR_setresgid32
8736 case TARGET_NR_setresgid32:
8737 ret = get_errno(setresgid(arg1, arg2, arg3));
8738 break;
8739 #endif
8740 #ifdef TARGET_NR_getresgid32
8741 case TARGET_NR_getresgid32:
8743 gid_t rgid, egid, sgid;
8744 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8745 if (!is_error(ret)) {
8746 if (put_user_u32(rgid, arg1)
8747 || put_user_u32(egid, arg2)
8748 || put_user_u32(sgid, arg3))
8749 goto efault;
8752 break;
8753 #endif
8754 #ifdef TARGET_NR_chown32
8755 case TARGET_NR_chown32:
8756 if (!(p = lock_user_string(arg1)))
8757 goto efault;
8758 ret = get_errno(chown(p, arg2, arg3));
8759 unlock_user(p, arg1, 0);
8760 break;
8761 #endif
8762 #ifdef TARGET_NR_setuid32
8763 case TARGET_NR_setuid32:
8764 ret = get_errno(setuid(arg1));
8765 break;
8766 #endif
8767 #ifdef TARGET_NR_setgid32
8768 case TARGET_NR_setgid32:
8769 ret = get_errno(setgid(arg1));
8770 break;
8771 #endif
8772 #ifdef TARGET_NR_setfsuid32
8773 case TARGET_NR_setfsuid32:
8774 ret = get_errno(setfsuid(arg1));
8775 break;
8776 #endif
8777 #ifdef TARGET_NR_setfsgid32
8778 case TARGET_NR_setfsgid32:
8779 ret = get_errno(setfsgid(arg1));
8780 break;
8781 #endif
8783 case TARGET_NR_pivot_root:
8784 goto unimplemented;
8785 #ifdef TARGET_NR_mincore
8786 case TARGET_NR_mincore:
8788 void *a;
8789 ret = -TARGET_EFAULT;
8790 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
8791 goto efault;
8792 if (!(p = lock_user_string(arg3)))
8793 goto mincore_fail;
8794 ret = get_errno(mincore(a, arg2, p));
8795 unlock_user(p, arg3, ret);
8796 mincore_fail:
8797 unlock_user(a, arg1, 0);
8799 break;
8800 #endif
8801 #ifdef TARGET_NR_arm_fadvise64_64
8802 case TARGET_NR_arm_fadvise64_64:
8805 * arm_fadvise64_64 looks like fadvise64_64 but
8806 * with different argument order
8808 abi_long temp;
8809 temp = arg3;
8810 arg3 = arg4;
8811 arg4 = temp;
8813 #endif
8814 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
8815 #ifdef TARGET_NR_fadvise64_64
8816 case TARGET_NR_fadvise64_64:
8817 #endif
8818 #ifdef TARGET_NR_fadvise64
8819 case TARGET_NR_fadvise64:
8820 #endif
8821 #ifdef TARGET_S390X
8822 switch (arg4) {
8823 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
8824 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
8825 case 6: arg4 = POSIX_FADV_DONTNEED; break;
8826 case 7: arg4 = POSIX_FADV_NOREUSE; break;
8827 default: break;
8829 #endif
8830 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
8831 break;
8832 #endif
8833 #ifdef TARGET_NR_madvise
8834 case TARGET_NR_madvise:
8835 /* A straight passthrough may not be safe because qemu sometimes
8836 turns private file-backed mappings into anonymous mappings.
8837 This will break MADV_DONTNEED.
8838 This is a hint, so ignoring and returning success is ok. */
8839 ret = get_errno(0);
8840 break;
8841 #endif
8842 #if TARGET_ABI_BITS == 32
8843 case TARGET_NR_fcntl64:
8845 int cmd;
8846 struct flock64 fl;
8847 struct target_flock64 *target_fl;
8848 #ifdef TARGET_ARM
8849 struct target_eabi_flock64 *target_efl;
8850 #endif
8852 cmd = target_to_host_fcntl_cmd(arg2);
8853 if (cmd == -TARGET_EINVAL) {
8854 ret = cmd;
8855 break;
8858 switch(arg2) {
8859 case TARGET_F_GETLK64:
8860 #ifdef TARGET_ARM
8861 if (((CPUARMState *)cpu_env)->eabi) {
8862 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8863 goto efault;
8864 fl.l_type = tswap16(target_efl->l_type);
8865 fl.l_whence = tswap16(target_efl->l_whence);
8866 fl.l_start = tswap64(target_efl->l_start);
8867 fl.l_len = tswap64(target_efl->l_len);
8868 fl.l_pid = tswap32(target_efl->l_pid);
8869 unlock_user_struct(target_efl, arg3, 0);
8870 } else
8871 #endif
8873 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8874 goto efault;
8875 fl.l_type = tswap16(target_fl->l_type);
8876 fl.l_whence = tswap16(target_fl->l_whence);
8877 fl.l_start = tswap64(target_fl->l_start);
8878 fl.l_len = tswap64(target_fl->l_len);
8879 fl.l_pid = tswap32(target_fl->l_pid);
8880 unlock_user_struct(target_fl, arg3, 0);
8882 ret = get_errno(fcntl(arg1, cmd, &fl));
8883 if (ret == 0) {
8884 #ifdef TARGET_ARM
8885 if (((CPUARMState *)cpu_env)->eabi) {
8886 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
8887 goto efault;
8888 target_efl->l_type = tswap16(fl.l_type);
8889 target_efl->l_whence = tswap16(fl.l_whence);
8890 target_efl->l_start = tswap64(fl.l_start);
8891 target_efl->l_len = tswap64(fl.l_len);
8892 target_efl->l_pid = tswap32(fl.l_pid);
8893 unlock_user_struct(target_efl, arg3, 1);
8894 } else
8895 #endif
8897 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
8898 goto efault;
8899 target_fl->l_type = tswap16(fl.l_type);
8900 target_fl->l_whence = tswap16(fl.l_whence);
8901 target_fl->l_start = tswap64(fl.l_start);
8902 target_fl->l_len = tswap64(fl.l_len);
8903 target_fl->l_pid = tswap32(fl.l_pid);
8904 unlock_user_struct(target_fl, arg3, 1);
8907 break;
8909 case TARGET_F_SETLK64:
8910 case TARGET_F_SETLKW64:
8911 #ifdef TARGET_ARM
8912 if (((CPUARMState *)cpu_env)->eabi) {
8913 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8914 goto efault;
8915 fl.l_type = tswap16(target_efl->l_type);
8916 fl.l_whence = tswap16(target_efl->l_whence);
8917 fl.l_start = tswap64(target_efl->l_start);
8918 fl.l_len = tswap64(target_efl->l_len);
8919 fl.l_pid = tswap32(target_efl->l_pid);
8920 unlock_user_struct(target_efl, arg3, 0);
8921 } else
8922 #endif
8924 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8925 goto efault;
8926 fl.l_type = tswap16(target_fl->l_type);
8927 fl.l_whence = tswap16(target_fl->l_whence);
8928 fl.l_start = tswap64(target_fl->l_start);
8929 fl.l_len = tswap64(target_fl->l_len);
8930 fl.l_pid = tswap32(target_fl->l_pid);
8931 unlock_user_struct(target_fl, arg3, 0);
8933 ret = get_errno(fcntl(arg1, cmd, &fl));
8934 break;
8935 default:
8936 ret = do_fcntl(arg1, arg2, arg3);
8937 break;
8939 break;
8941 #endif
8942 #ifdef TARGET_NR_cacheflush
8943 case TARGET_NR_cacheflush:
8944 /* self-modifying code is handled automatically, so nothing needed */
8945 ret = 0;
8946 break;
8947 #endif
8948 #ifdef TARGET_NR_security
8949 case TARGET_NR_security:
8950 goto unimplemented;
8951 #endif
8952 #ifdef TARGET_NR_getpagesize
8953 case TARGET_NR_getpagesize:
8954 ret = TARGET_PAGE_SIZE;
8955 break;
8956 #endif
8957 case TARGET_NR_gettid:
8958 ret = get_errno(gettid());
8959 break;
8960 #ifdef TARGET_NR_readahead
8961 case TARGET_NR_readahead:
8962 #if TARGET_ABI_BITS == 32
8963 if (regpairs_aligned(cpu_env)) {
8964 arg2 = arg3;
8965 arg3 = arg4;
8966 arg4 = arg5;
8968 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
8969 #else
8970 ret = get_errno(readahead(arg1, arg2, arg3));
8971 #endif
8972 break;
8973 #endif
8974 #ifdef CONFIG_ATTR
8975 #ifdef TARGET_NR_setxattr
8976 case TARGET_NR_listxattr:
8977 case TARGET_NR_llistxattr:
8979 void *p, *b = 0;
8980 if (arg2) {
8981 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8982 if (!b) {
8983 ret = -TARGET_EFAULT;
8984 break;
8987 p = lock_user_string(arg1);
8988 if (p) {
8989 if (num == TARGET_NR_listxattr) {
8990 ret = get_errno(listxattr(p, b, arg3));
8991 } else {
8992 ret = get_errno(llistxattr(p, b, arg3));
8994 } else {
8995 ret = -TARGET_EFAULT;
8997 unlock_user(p, arg1, 0);
8998 unlock_user(b, arg2, arg3);
8999 break;
9001 case TARGET_NR_flistxattr:
9003 void *b = 0;
9004 if (arg2) {
9005 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
9006 if (!b) {
9007 ret = -TARGET_EFAULT;
9008 break;
9011 ret = get_errno(flistxattr(arg1, b, arg3));
9012 unlock_user(b, arg2, arg3);
9013 break;
9015 case TARGET_NR_setxattr:
9016 case TARGET_NR_lsetxattr:
9018 void *p, *n, *v = 0;
9019 if (arg3) {
9020 v = lock_user(VERIFY_READ, arg3, arg4, 1);
9021 if (!v) {
9022 ret = -TARGET_EFAULT;
9023 break;
9026 p = lock_user_string(arg1);
9027 n = lock_user_string(arg2);
9028 if (p && n) {
9029 if (num == TARGET_NR_setxattr) {
9030 ret = get_errno(setxattr(p, n, v, arg4, arg5));
9031 } else {
9032 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
9034 } else {
9035 ret = -TARGET_EFAULT;
9037 unlock_user(p, arg1, 0);
9038 unlock_user(n, arg2, 0);
9039 unlock_user(v, arg3, 0);
9041 break;
9042 case TARGET_NR_fsetxattr:
9044 void *n, *v = 0;
9045 if (arg3) {
9046 v = lock_user(VERIFY_READ, arg3, arg4, 1);
9047 if (!v) {
9048 ret = -TARGET_EFAULT;
9049 break;
9052 n = lock_user_string(arg2);
9053 if (n) {
9054 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
9055 } else {
9056 ret = -TARGET_EFAULT;
9058 unlock_user(n, arg2, 0);
9059 unlock_user(v, arg3, 0);
9061 break;
9062 case TARGET_NR_getxattr:
9063 case TARGET_NR_lgetxattr:
9065 void *p, *n, *v = 0;
9066 if (arg3) {
9067 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
9068 if (!v) {
9069 ret = -TARGET_EFAULT;
9070 break;
9073 p = lock_user_string(arg1);
9074 n = lock_user_string(arg2);
9075 if (p && n) {
9076 if (num == TARGET_NR_getxattr) {
9077 ret = get_errno(getxattr(p, n, v, arg4));
9078 } else {
9079 ret = get_errno(lgetxattr(p, n, v, arg4));
9081 } else {
9082 ret = -TARGET_EFAULT;
9084 unlock_user(p, arg1, 0);
9085 unlock_user(n, arg2, 0);
9086 unlock_user(v, arg3, arg4);
9088 break;
9089 case TARGET_NR_fgetxattr:
9091 void *n, *v = 0;
9092 if (arg3) {
9093 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
9094 if (!v) {
9095 ret = -TARGET_EFAULT;
9096 break;
9099 n = lock_user_string(arg2);
9100 if (n) {
9101 ret = get_errno(fgetxattr(arg1, n, v, arg4));
9102 } else {
9103 ret = -TARGET_EFAULT;
9105 unlock_user(n, arg2, 0);
9106 unlock_user(v, arg3, arg4);
9108 break;
9109 case TARGET_NR_removexattr:
9110 case TARGET_NR_lremovexattr:
9112 void *p, *n;
9113 p = lock_user_string(arg1);
9114 n = lock_user_string(arg2);
9115 if (p && n) {
9116 if (num == TARGET_NR_removexattr) {
9117 ret = get_errno(removexattr(p, n));
9118 } else {
9119 ret = get_errno(lremovexattr(p, n));
9121 } else {
9122 ret = -TARGET_EFAULT;
9124 unlock_user(p, arg1, 0);
9125 unlock_user(n, arg2, 0);
9127 break;
9128 case TARGET_NR_fremovexattr:
9130 void *n;
9131 n = lock_user_string(arg2);
9132 if (n) {
9133 ret = get_errno(fremovexattr(arg1, n));
9134 } else {
9135 ret = -TARGET_EFAULT;
9137 unlock_user(n, arg2, 0);
9139 break;
9140 #endif
9141 #endif /* CONFIG_ATTR */
9142 #ifdef TARGET_NR_set_thread_area
9143 case TARGET_NR_set_thread_area:
9144 #if defined(TARGET_MIPS)
9145 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
9146 ret = 0;
9147 break;
9148 #elif defined(TARGET_CRIS)
9149 if (arg1 & 0xff)
9150 ret = -TARGET_EINVAL;
9151 else {
9152 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
9153 ret = 0;
9155 break;
9156 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
9157 ret = do_set_thread_area(cpu_env, arg1);
9158 break;
9159 #elif defined(TARGET_M68K)
9161 TaskState *ts = cpu->opaque;
9162 ts->tp_value = arg1;
9163 ret = 0;
9164 break;
9166 #else
9167 goto unimplemented_nowarn;
9168 #endif
9169 #endif
9170 #ifdef TARGET_NR_get_thread_area
9171 case TARGET_NR_get_thread_area:
9172 #if defined(TARGET_I386) && defined(TARGET_ABI32)
9173 ret = do_get_thread_area(cpu_env, arg1);
9174 break;
9175 #elif defined(TARGET_M68K)
9177 TaskState *ts = cpu->opaque;
9178 ret = ts->tp_value;
9179 break;
9181 #else
9182 goto unimplemented_nowarn;
9183 #endif
9184 #endif
9185 #ifdef TARGET_NR_getdomainname
9186 case TARGET_NR_getdomainname:
9187 goto unimplemented_nowarn;
9188 #endif
9190 #ifdef TARGET_NR_clock_gettime
9191 case TARGET_NR_clock_gettime:
9193 struct timespec ts;
9194 ret = get_errno(clock_gettime(arg1, &ts));
9195 if (!is_error(ret)) {
9196 host_to_target_timespec(arg2, &ts);
9198 break;
9200 #endif
9201 #ifdef TARGET_NR_clock_getres
9202 case TARGET_NR_clock_getres:
9204 struct timespec ts;
9205 ret = get_errno(clock_getres(arg1, &ts));
9206 if (!is_error(ret)) {
9207 host_to_target_timespec(arg2, &ts);
9209 break;
9211 #endif
9212 #ifdef TARGET_NR_clock_nanosleep
9213 case TARGET_NR_clock_nanosleep:
9215 struct timespec ts;
9216 target_to_host_timespec(&ts, arg3);
9217 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
9218 if (arg4)
9219 host_to_target_timespec(arg4, &ts);
9221 #if defined(TARGET_PPC)
9222 /* clock_nanosleep is odd in that it returns positive errno values.
9223 * On PPC, CR0 bit 3 should be set in such a situation. */
9224 if (ret) {
9225 ((CPUPPCState *)cpu_env)->crf[0] |= 1;
9227 #endif
9228 break;
9230 #endif
9232 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
9233 case TARGET_NR_set_tid_address:
9234 ret = get_errno(set_tid_address((int *)g2h(arg1)));
9235 break;
9236 #endif
9238 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
9239 case TARGET_NR_tkill:
9240 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
9241 break;
9242 #endif
9244 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
9245 case TARGET_NR_tgkill:
9246 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
9247 target_to_host_signal(arg3)));
9248 break;
9249 #endif
9251 #ifdef TARGET_NR_set_robust_list
9252 case TARGET_NR_set_robust_list:
9253 case TARGET_NR_get_robust_list:
9254 /* The ABI for supporting robust futexes has userspace pass
9255 * the kernel a pointer to a linked list which is updated by
9256 * userspace after the syscall; the list is walked by the kernel
9257 * when the thread exits. Since the linked list in QEMU guest
9258 * memory isn't a valid linked list for the host and we have
9259 * no way to reliably intercept the thread-death event, we can't
9260 * support these. Silently return ENOSYS so that guest userspace
9261 * falls back to a non-robust futex implementation (which should
9262 * be OK except in the corner case of the guest crashing while
9263 * holding a mutex that is shared with another process via
9264 * shared memory).
9266 goto unimplemented_nowarn;
9267 #endif
9269 #if defined(TARGET_NR_utimensat)
9270 case TARGET_NR_utimensat:
9272 struct timespec *tsp, ts[2];
9273 if (!arg3) {
9274 tsp = NULL;
9275 } else {
9276 target_to_host_timespec(ts, arg3);
9277 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
9278 tsp = ts;
9280 if (!arg2)
9281 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
9282 else {
9283 if (!(p = lock_user_string(arg2))) {
9284 ret = -TARGET_EFAULT;
9285 goto fail;
9287 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
9288 unlock_user(p, arg2, 0);
9291 break;
9292 #endif
9293 case TARGET_NR_futex:
9294 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
9295 break;
9296 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
9297 case TARGET_NR_inotify_init:
9298 ret = get_errno(sys_inotify_init());
9299 break;
9300 #endif
9301 #ifdef CONFIG_INOTIFY1
9302 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
9303 case TARGET_NR_inotify_init1:
9304 ret = get_errno(sys_inotify_init1(arg1));
9305 break;
9306 #endif
9307 #endif
9308 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
9309 case TARGET_NR_inotify_add_watch:
9310 p = lock_user_string(arg2);
9311 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
9312 unlock_user(p, arg2, 0);
9313 break;
9314 #endif
9315 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
9316 case TARGET_NR_inotify_rm_watch:
9317 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
9318 break;
9319 #endif
9321 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
9322 case TARGET_NR_mq_open:
9324 struct mq_attr posix_mq_attr, *attrp;
9326 p = lock_user_string(arg1 - 1);
9327 if (arg4 != 0) {
9328 copy_from_user_mq_attr (&posix_mq_attr, arg4);
9329 attrp = &posix_mq_attr;
9330 } else {
9331 attrp = 0;
9333 ret = get_errno(mq_open(p, arg2, arg3, attrp));
9334 unlock_user (p, arg1, 0);
9336 break;
9338 case TARGET_NR_mq_unlink:
9339 p = lock_user_string(arg1 - 1);
9340 ret = get_errno(mq_unlink(p));
9341 unlock_user (p, arg1, 0);
9342 break;
9344 case TARGET_NR_mq_timedsend:
9346 struct timespec ts;
9348 p = lock_user (VERIFY_READ, arg2, arg3, 1);
9349 if (arg5 != 0) {
9350 target_to_host_timespec(&ts, arg5);
9351 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
9352 host_to_target_timespec(arg5, &ts);
9354 else
9355 ret = get_errno(mq_send(arg1, p, arg3, arg4));
9356 unlock_user (p, arg2, arg3);
9358 break;
9360 case TARGET_NR_mq_timedreceive:
9362 struct timespec ts;
9363 unsigned int prio;
9365 p = lock_user (VERIFY_READ, arg2, arg3, 1);
9366 if (arg5 != 0) {
9367 target_to_host_timespec(&ts, arg5);
9368 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
9369 host_to_target_timespec(arg5, &ts);
9371 else
9372 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
9373 unlock_user (p, arg2, arg3);
9374 if (arg4 != 0)
9375 put_user_u32(prio, arg4);
9377 break;
9379 /* Not implemented for now... */
9380 /* case TARGET_NR_mq_notify: */
9381 /* break; */
9383 case TARGET_NR_mq_getsetattr:
9385 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
9386 ret = 0;
9387 if (arg3 != 0) {
9388 ret = mq_getattr(arg1, &posix_mq_attr_out);
9389 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
9391 if (arg2 != 0) {
9392 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
9393 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
9397 break;
9398 #endif
9400 #ifdef CONFIG_SPLICE
9401 #ifdef TARGET_NR_tee
9402 case TARGET_NR_tee:
9404 ret = get_errno(tee(arg1,arg2,arg3,arg4));
9406 break;
9407 #endif
9408 #ifdef TARGET_NR_splice
9409 case TARGET_NR_splice:
9411 loff_t loff_in, loff_out;
9412 loff_t *ploff_in = NULL, *ploff_out = NULL;
9413 if (arg2) {
9414 if (get_user_u64(loff_in, arg2)) {
9415 goto efault;
9417 ploff_in = &loff_in;
9419 if (arg4) {
9420 if (get_user_u64(loff_out, arg4)) {
9421 goto efault;
9423 ploff_out = &loff_out;
9425 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
9426 if (arg2) {
9427 if (put_user_u64(loff_in, arg2)) {
9428 goto efault;
9431 if (arg4) {
9432 if (put_user_u64(loff_out, arg4)) {
9433 goto efault;
9437 break;
9438 #endif
9439 #ifdef TARGET_NR_vmsplice
9440 case TARGET_NR_vmsplice:
9442 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
9443 if (vec != NULL) {
9444 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
9445 unlock_iovec(vec, arg2, arg3, 0);
9446 } else {
9447 ret = -host_to_target_errno(errno);
9450 break;
9451 #endif
9452 #endif /* CONFIG_SPLICE */
9453 #ifdef CONFIG_EVENTFD
9454 #if defined(TARGET_NR_eventfd)
9455 case TARGET_NR_eventfd:
9456 ret = get_errno(eventfd(arg1, 0));
9457 break;
9458 #endif
9459 #if defined(TARGET_NR_eventfd2)
9460 case TARGET_NR_eventfd2:
9462 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
9463 if (arg2 & TARGET_O_NONBLOCK) {
9464 host_flags |= O_NONBLOCK;
9466 if (arg2 & TARGET_O_CLOEXEC) {
9467 host_flags |= O_CLOEXEC;
9469 ret = get_errno(eventfd(arg1, host_flags));
9470 break;
9472 #endif
9473 #endif /* CONFIG_EVENTFD */
9474 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
9475 case TARGET_NR_fallocate:
9476 #if TARGET_ABI_BITS == 32
9477 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
9478 target_offset64(arg5, arg6)));
9479 #else
9480 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
9481 #endif
9482 break;
9483 #endif
9484 #if defined(CONFIG_SYNC_FILE_RANGE)
9485 #if defined(TARGET_NR_sync_file_range)
9486 case TARGET_NR_sync_file_range:
9487 #if TARGET_ABI_BITS == 32
9488 #if defined(TARGET_MIPS)
9489 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
9490 target_offset64(arg5, arg6), arg7));
9491 #else
9492 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
9493 target_offset64(arg4, arg5), arg6));
9494 #endif /* !TARGET_MIPS */
9495 #else
9496 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
9497 #endif
9498 break;
9499 #endif
9500 #if defined(TARGET_NR_sync_file_range2)
9501 case TARGET_NR_sync_file_range2:
9502 /* This is like sync_file_range but the arguments are reordered */
9503 #if TARGET_ABI_BITS == 32
9504 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
9505 target_offset64(arg5, arg6), arg2));
9506 #else
9507 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
9508 #endif
9509 break;
9510 #endif
9511 #endif
9512 #if defined(CONFIG_EPOLL)
9513 #if defined(TARGET_NR_epoll_create)
9514 case TARGET_NR_epoll_create:
9515 ret = get_errno(epoll_create(arg1));
9516 break;
9517 #endif
9518 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
9519 case TARGET_NR_epoll_create1:
9520 ret = get_errno(epoll_create1(arg1));
9521 break;
9522 #endif
9523 #if defined(TARGET_NR_epoll_ctl)
9524 case TARGET_NR_epoll_ctl:
9526 struct epoll_event ep;
9527 struct epoll_event *epp = 0;
9528 if (arg4) {
9529 struct target_epoll_event *target_ep;
9530 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
9531 goto efault;
9533 ep.events = tswap32(target_ep->events);
9534 /* The epoll_data_t union is just opaque data to the kernel,
9535 * so we transfer all 64 bits across and need not worry what
9536 * actual data type it is.
9538 ep.data.u64 = tswap64(target_ep->data.u64);
9539 unlock_user_struct(target_ep, arg4, 0);
9540 epp = &ep;
9542 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
9543 break;
9545 #endif
9547 #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9548 #define IMPLEMENT_EPOLL_PWAIT
9549 #endif
9550 #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9551 #if defined(TARGET_NR_epoll_wait)
9552 case TARGET_NR_epoll_wait:
9553 #endif
9554 #if defined(IMPLEMENT_EPOLL_PWAIT)
9555 case TARGET_NR_epoll_pwait:
9556 #endif
9558 struct target_epoll_event *target_ep;
9559 struct epoll_event *ep;
9560 int epfd = arg1;
9561 int maxevents = arg3;
9562 int timeout = arg4;
9564 target_ep = lock_user(VERIFY_WRITE, arg2,
9565 maxevents * sizeof(struct target_epoll_event), 1);
9566 if (!target_ep) {
9567 goto efault;
9570 ep = alloca(maxevents * sizeof(struct epoll_event));
9572 switch (num) {
9573 #if defined(IMPLEMENT_EPOLL_PWAIT)
9574 case TARGET_NR_epoll_pwait:
9576 target_sigset_t *target_set;
9577 sigset_t _set, *set = &_set;
9579 if (arg5) {
9580 target_set = lock_user(VERIFY_READ, arg5,
9581 sizeof(target_sigset_t), 1);
9582 if (!target_set) {
9583 unlock_user(target_ep, arg2, 0);
9584 goto efault;
9586 target_to_host_sigset(set, target_set);
9587 unlock_user(target_set, arg5, 0);
9588 } else {
9589 set = NULL;
9592 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
9593 break;
9595 #endif
9596 #if defined(TARGET_NR_epoll_wait)
9597 case TARGET_NR_epoll_wait:
9598 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
9599 break;
9600 #endif
9601 default:
9602 ret = -TARGET_ENOSYS;
9604 if (!is_error(ret)) {
9605 int i;
9606 for (i = 0; i < ret; i++) {
9607 target_ep[i].events = tswap32(ep[i].events);
9608 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
9611 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
9612 break;
9614 #endif
9615 #endif
9616 #ifdef TARGET_NR_prlimit64
9617 case TARGET_NR_prlimit64:
9619 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9620 struct target_rlimit64 *target_rnew, *target_rold;
9621 struct host_rlimit64 rnew, rold, *rnewp = 0;
9622 int resource = target_to_host_resource(arg2);
9623 if (arg3) {
9624 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
9625 goto efault;
9627 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
9628 rnew.rlim_max = tswap64(target_rnew->rlim_max);
9629 unlock_user_struct(target_rnew, arg3, 0);
9630 rnewp = &rnew;
9633 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
9634 if (!is_error(ret) && arg4) {
9635 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
9636 goto efault;
9638 target_rold->rlim_cur = tswap64(rold.rlim_cur);
9639 target_rold->rlim_max = tswap64(rold.rlim_max);
9640 unlock_user_struct(target_rold, arg4, 1);
9642 break;
9644 #endif
9645 #ifdef TARGET_NR_gethostname
9646 case TARGET_NR_gethostname:
9648 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
9649 if (name) {
9650 ret = get_errno(gethostname(name, arg2));
9651 unlock_user(name, arg1, arg2);
9652 } else {
9653 ret = -TARGET_EFAULT;
9655 break;
9657 #endif
9658 #ifdef TARGET_NR_atomic_cmpxchg_32
9659 case TARGET_NR_atomic_cmpxchg_32:
9661 /* should use start_exclusive from main.c */
9662 abi_ulong mem_value;
9663 if (get_user_u32(mem_value, arg6)) {
9664 target_siginfo_t info;
9665 info.si_signo = SIGSEGV;
9666 info.si_errno = 0;
9667 info.si_code = TARGET_SEGV_MAPERR;
9668 info._sifields._sigfault._addr = arg6;
9669 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
9670 ret = 0xdeadbeef;
9673 if (mem_value == arg2)
9674 put_user_u32(arg1, arg6);
9675 ret = mem_value;
9676 break;
9678 #endif
9679 #ifdef TARGET_NR_atomic_barrier
9680 case TARGET_NR_atomic_barrier:
9682 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
9683 ret = 0;
9684 break;
9686 #endif
9688 #ifdef TARGET_NR_timer_create
9689 case TARGET_NR_timer_create:
9691 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
9693 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
9695 int clkid = arg1;
9696 int timer_index = next_free_host_timer();
9698 if (timer_index < 0) {
9699 ret = -TARGET_EAGAIN;
9700 } else {
9701 timer_t *phtimer = g_posix_timers + timer_index;
9703 if (arg2) {
9704 phost_sevp = &host_sevp;
9705 ret = target_to_host_sigevent(phost_sevp, arg2);
9706 if (ret != 0) {
9707 break;
9711 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
9712 if (ret) {
9713 phtimer = NULL;
9714 } else {
9715 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
9716 goto efault;
9720 break;
9722 #endif
9724 #ifdef TARGET_NR_timer_settime
9725 case TARGET_NR_timer_settime:
9727 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
9728 * struct itimerspec * old_value */
9729 target_timer_t timerid = get_timer_id(arg1);
9731 if (timerid < 0) {
9732 ret = timerid;
9733 } else if (arg3 == 0) {
9734 ret = -TARGET_EINVAL;
9735 } else {
9736 timer_t htimer = g_posix_timers[timerid];
9737 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
9739 target_to_host_itimerspec(&hspec_new, arg3);
9740 ret = get_errno(
9741 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
9742 host_to_target_itimerspec(arg2, &hspec_old);
9744 break;
9746 #endif
9748 #ifdef TARGET_NR_timer_gettime
9749 case TARGET_NR_timer_gettime:
9751 /* args: timer_t timerid, struct itimerspec *curr_value */
9752 target_timer_t timerid = get_timer_id(arg1);
9754 if (timerid < 0) {
9755 ret = timerid;
9756 } else if (!arg2) {
9757 ret = -TARGET_EFAULT;
9758 } else {
9759 timer_t htimer = g_posix_timers[timerid];
9760 struct itimerspec hspec;
9761 ret = get_errno(timer_gettime(htimer, &hspec));
9763 if (host_to_target_itimerspec(arg2, &hspec)) {
9764 ret = -TARGET_EFAULT;
9767 break;
9769 #endif
9771 #ifdef TARGET_NR_timer_getoverrun
9772 case TARGET_NR_timer_getoverrun:
9774 /* args: timer_t timerid */
9775 target_timer_t timerid = get_timer_id(arg1);
9777 if (timerid < 0) {
9778 ret = timerid;
9779 } else {
9780 timer_t htimer = g_posix_timers[timerid];
9781 ret = get_errno(timer_getoverrun(htimer));
9783 break;
9785 #endif
9787 #ifdef TARGET_NR_timer_delete
9788 case TARGET_NR_timer_delete:
9790 /* args: timer_t timerid */
9791 target_timer_t timerid = get_timer_id(arg1);
9793 if (timerid < 0) {
9794 ret = timerid;
9795 } else {
9796 timer_t htimer = g_posix_timers[timerid];
9797 ret = get_errno(timer_delete(htimer));
9798 g_posix_timers[timerid] = 0;
9800 break;
9802 #endif
9804 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
9805 case TARGET_NR_timerfd_create:
9806 ret = get_errno(timerfd_create(arg1,
9807 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
9808 break;
9809 #endif
9811 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
9812 case TARGET_NR_timerfd_gettime:
9814 struct itimerspec its_curr;
9816 ret = get_errno(timerfd_gettime(arg1, &its_curr));
9818 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
9819 goto efault;
9822 break;
9823 #endif
9825 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
9826 case TARGET_NR_timerfd_settime:
9828 struct itimerspec its_new, its_old, *p_new;
9830 if (arg3) {
9831 if (target_to_host_itimerspec(&its_new, arg3)) {
9832 goto efault;
9834 p_new = &its_new;
9835 } else {
9836 p_new = NULL;
9839 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
9841 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
9842 goto efault;
9845 break;
9846 #endif
9848 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
9849 case TARGET_NR_ioprio_get:
9850 ret = get_errno(ioprio_get(arg1, arg2));
9851 break;
9852 #endif
9854 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
9855 case TARGET_NR_ioprio_set:
9856 ret = get_errno(ioprio_set(arg1, arg2, arg3));
9857 break;
9858 #endif
9860 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
9861 case TARGET_NR_setns:
9862 ret = get_errno(setns(arg1, arg2));
9863 break;
9864 #endif
9865 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
9866 case TARGET_NR_unshare:
9867 ret = get_errno(unshare(arg1));
9868 break;
9869 #endif
9871 default:
9872 unimplemented:
9873 gemu_log("qemu: Unsupported syscall: %d\n", num);
9874 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
9875 unimplemented_nowarn:
9876 #endif
9877 ret = -TARGET_ENOSYS;
9878 break;
9880 fail:
9881 #ifdef DEBUG
9882 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
9883 #endif
9884 if(do_strace)
9885 print_syscall_ret(num, ret);
9886 return ret;
9887 efault:
9888 ret = -TARGET_EFAULT;
9889 goto fail;