]> git.proxmox.com Git - qemu.git/blob - linux-user/syscall.c
[v2] linux-user: implement m68k atomic syscalls
[qemu.git] / linux-user / syscall.c
1 /*
2 * Linux syscalls
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #define _ATFILE_SOURCE
20 #include <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 <signal.h>
47 #include <sched.h>
48 #ifdef __ia64__
49 int __clone2(int (*fn)(void *), void *child_stack_base,
50 size_t stack_size, int flags, void *arg, ...);
51 #endif
52 #include <sys/socket.h>
53 #include <sys/un.h>
54 #include <sys/uio.h>
55 #include <sys/poll.h>
56 #include <sys/times.h>
57 #include <sys/shm.h>
58 #include <sys/sem.h>
59 #include <sys/statfs.h>
60 #include <utime.h>
61 #include <sys/sysinfo.h>
62 #include <sys/utsname.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 #ifdef TARGET_GPROF
70 #include <sys/gmon.h>
71 #endif
72 #ifdef CONFIG_EVENTFD
73 #include <sys/eventfd.h>
74 #endif
75 #ifdef CONFIG_EPOLL
76 #include <sys/epoll.h>
77 #endif
78 #ifdef CONFIG_ATTR
79 #include "qemu/xattr.h"
80 #endif
81 #ifdef CONFIG_SENDFILE
82 #include <sys/sendfile.h>
83 #endif
84
85 #define termios host_termios
86 #define winsize host_winsize
87 #define termio host_termio
88 #define sgttyb host_sgttyb /* same as target */
89 #define tchars host_tchars /* same as target */
90 #define ltchars host_ltchars /* same as target */
91
92 #include <linux/termios.h>
93 #include <linux/unistd.h>
94 #include <linux/utsname.h>
95 #include <linux/cdrom.h>
96 #include <linux/hdreg.h>
97 #include <linux/soundcard.h>
98 #include <linux/kd.h>
99 #include <linux/mtio.h>
100 #include <linux/fs.h>
101 #if defined(CONFIG_FIEMAP)
102 #include <linux/fiemap.h>
103 #endif
104 #include <linux/fb.h>
105 #include <linux/vt.h>
106 #include <linux/dm-ioctl.h>
107 #include <linux/reboot.h>
108 #include <linux/route.h>
109 #include <linux/filter.h>
110 #include "linux_loop.h"
111 #include "cpu-uname.h"
112
113 #include "qemu.h"
114
115 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
116 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
117
118 //#define DEBUG
119
120 //#include <linux/msdos_fs.h>
121 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
122 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
123
124
125 #undef _syscall0
126 #undef _syscall1
127 #undef _syscall2
128 #undef _syscall3
129 #undef _syscall4
130 #undef _syscall5
131 #undef _syscall6
132
133 #define _syscall0(type,name) \
134 static type name (void) \
135 { \
136 return syscall(__NR_##name); \
137 }
138
139 #define _syscall1(type,name,type1,arg1) \
140 static type name (type1 arg1) \
141 { \
142 return syscall(__NR_##name, arg1); \
143 }
144
145 #define _syscall2(type,name,type1,arg1,type2,arg2) \
146 static type name (type1 arg1,type2 arg2) \
147 { \
148 return syscall(__NR_##name, arg1, arg2); \
149 }
150
151 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
152 static type name (type1 arg1,type2 arg2,type3 arg3) \
153 { \
154 return syscall(__NR_##name, arg1, arg2, arg3); \
155 }
156
157 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
158 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
159 { \
160 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
161 }
162
163 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
164 type5,arg5) \
165 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
166 { \
167 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
168 }
169
170
171 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
172 type5,arg5,type6,arg6) \
173 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
174 type6 arg6) \
175 { \
176 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
177 }
178
179
180 #define __NR_sys_uname __NR_uname
181 #define __NR_sys_getcwd1 __NR_getcwd
182 #define __NR_sys_getdents __NR_getdents
183 #define __NR_sys_getdents64 __NR_getdents64
184 #define __NR_sys_getpriority __NR_getpriority
185 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
186 #define __NR_sys_syslog __NR_syslog
187 #define __NR_sys_tgkill __NR_tgkill
188 #define __NR_sys_tkill __NR_tkill
189 #define __NR_sys_futex __NR_futex
190 #define __NR_sys_inotify_init __NR_inotify_init
191 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
192 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
193
194 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
195 defined(__s390x__)
196 #define __NR__llseek __NR_lseek
197 #endif
198
199 #ifdef __NR_gettid
200 _syscall0(int, gettid)
201 #else
202 /* This is a replacement for the host gettid() and must return a host
203 errno. */
204 static int gettid(void) {
205 return -ENOSYS;
206 }
207 #endif
208 #ifdef __NR_getdents
209 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
210 #endif
211 #if !defined(__NR_getdents) || \
212 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
213 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
214 #endif
215 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
216 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
217 loff_t *, res, uint, wh);
218 #endif
219 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
220 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
221 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
222 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
223 #endif
224 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
225 _syscall2(int,sys_tkill,int,tid,int,sig)
226 #endif
227 #ifdef __NR_exit_group
228 _syscall1(int,exit_group,int,error_code)
229 #endif
230 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
231 _syscall1(int,set_tid_address,int *,tidptr)
232 #endif
233 #if defined(TARGET_NR_futex) && defined(__NR_futex)
234 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
235 const struct timespec *,timeout,int *,uaddr2,int,val3)
236 #endif
237 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
238 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
239 unsigned long *, user_mask_ptr);
240 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
241 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
242 unsigned long *, user_mask_ptr);
243 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
244 void *, arg);
245
246 static bitmask_transtbl fcntl_flags_tbl[] = {
247 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
248 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
249 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
250 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
251 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
252 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
253 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
254 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
255 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
256 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
257 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
258 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
259 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
260 #if defined(O_DIRECT)
261 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
262 #endif
263 #if defined(O_NOATIME)
264 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
265 #endif
266 #if defined(O_CLOEXEC)
267 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
268 #endif
269 #if defined(O_PATH)
270 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
271 #endif
272 /* Don't terminate the list prematurely on 64-bit host+guest. */
273 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
274 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
275 #endif
276 { 0, 0, 0, 0 }
277 };
278
279 #define COPY_UTSNAME_FIELD(dest, src) \
280 do { \
281 /* __NEW_UTS_LEN doesn't include terminating null */ \
282 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
283 (dest)[__NEW_UTS_LEN] = '\0'; \
284 } while (0)
285
286 static int sys_uname(struct new_utsname *buf)
287 {
288 struct utsname uts_buf;
289
290 if (uname(&uts_buf) < 0)
291 return (-1);
292
293 /*
294 * Just in case these have some differences, we
295 * translate utsname to new_utsname (which is the
296 * struct linux kernel uses).
297 */
298
299 memset(buf, 0, sizeof(*buf));
300 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
301 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
302 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
303 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
304 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
305 #ifdef _GNU_SOURCE
306 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
307 #endif
308 return (0);
309
310 #undef COPY_UTSNAME_FIELD
311 }
312
313 static int sys_getcwd1(char *buf, size_t size)
314 {
315 if (getcwd(buf, size) == NULL) {
316 /* getcwd() sets errno */
317 return (-1);
318 }
319 return strlen(buf)+1;
320 }
321
322 #ifdef TARGET_NR_openat
323 static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
324 {
325 /*
326 * open(2) has extra parameter 'mode' when called with
327 * flag O_CREAT.
328 */
329 if ((flags & O_CREAT) != 0) {
330 return (openat(dirfd, pathname, flags, mode));
331 }
332 return (openat(dirfd, pathname, flags));
333 }
334 #endif
335
336 #ifdef TARGET_NR_utimensat
337 #ifdef CONFIG_UTIMENSAT
338 static int sys_utimensat(int dirfd, const char *pathname,
339 const struct timespec times[2], int flags)
340 {
341 if (pathname == NULL)
342 return futimens(dirfd, times);
343 else
344 return utimensat(dirfd, pathname, times, flags);
345 }
346 #elif defined(__NR_utimensat)
347 #define __NR_sys_utimensat __NR_utimensat
348 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
349 const struct timespec *,tsp,int,flags)
350 #else
351 static int sys_utimensat(int dirfd, const char *pathname,
352 const struct timespec times[2], int flags)
353 {
354 errno = ENOSYS;
355 return -1;
356 }
357 #endif
358 #endif /* TARGET_NR_utimensat */
359
360 #ifdef CONFIG_INOTIFY
361 #include <sys/inotify.h>
362
363 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
364 static int sys_inotify_init(void)
365 {
366 return (inotify_init());
367 }
368 #endif
369 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
370 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
371 {
372 return (inotify_add_watch(fd, pathname, mask));
373 }
374 #endif
375 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
376 static int sys_inotify_rm_watch(int fd, int32_t wd)
377 {
378 return (inotify_rm_watch(fd, wd));
379 }
380 #endif
381 #ifdef CONFIG_INOTIFY1
382 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
383 static int sys_inotify_init1(int flags)
384 {
385 return (inotify_init1(flags));
386 }
387 #endif
388 #endif
389 #else
390 /* Userspace can usually survive runtime without inotify */
391 #undef TARGET_NR_inotify_init
392 #undef TARGET_NR_inotify_init1
393 #undef TARGET_NR_inotify_add_watch
394 #undef TARGET_NR_inotify_rm_watch
395 #endif /* CONFIG_INOTIFY */
396
397 #if defined(TARGET_NR_ppoll)
398 #ifndef __NR_ppoll
399 # define __NR_ppoll -1
400 #endif
401 #define __NR_sys_ppoll __NR_ppoll
402 _syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
403 struct timespec *, timeout, const __sigset_t *, sigmask,
404 size_t, sigsetsize)
405 #endif
406
407 #if defined(TARGET_NR_pselect6)
408 #ifndef __NR_pselect6
409 # define __NR_pselect6 -1
410 #endif
411 #define __NR_sys_pselect6 __NR_pselect6
412 _syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
413 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
414 #endif
415
416 #if defined(TARGET_NR_prlimit64)
417 #ifndef __NR_prlimit64
418 # define __NR_prlimit64 -1
419 #endif
420 #define __NR_sys_prlimit64 __NR_prlimit64
421 /* The glibc rlimit structure may not be that used by the underlying syscall */
422 struct host_rlimit64 {
423 uint64_t rlim_cur;
424 uint64_t rlim_max;
425 };
426 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
427 const struct host_rlimit64 *, new_limit,
428 struct host_rlimit64 *, old_limit)
429 #endif
430
431 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
432 #ifdef TARGET_ARM
433 static inline int regpairs_aligned(void *cpu_env) {
434 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
435 }
436 #elif defined(TARGET_MIPS)
437 static inline int regpairs_aligned(void *cpu_env) { return 1; }
438 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
439 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
440 * of registers which translates to the same as ARM/MIPS, because we start with
441 * r3 as arg1 */
442 static inline int regpairs_aligned(void *cpu_env) { return 1; }
443 #else
444 static inline int regpairs_aligned(void *cpu_env) { return 0; }
445 #endif
446
447 #define ERRNO_TABLE_SIZE 1200
448
449 /* target_to_host_errno_table[] is initialized from
450 * host_to_target_errno_table[] in syscall_init(). */
451 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
452 };
453
454 /*
455 * This list is the union of errno values overridden in asm-<arch>/errno.h
456 * minus the errnos that are not actually generic to all archs.
457 */
458 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
459 [EIDRM] = TARGET_EIDRM,
460 [ECHRNG] = TARGET_ECHRNG,
461 [EL2NSYNC] = TARGET_EL2NSYNC,
462 [EL3HLT] = TARGET_EL3HLT,
463 [EL3RST] = TARGET_EL3RST,
464 [ELNRNG] = TARGET_ELNRNG,
465 [EUNATCH] = TARGET_EUNATCH,
466 [ENOCSI] = TARGET_ENOCSI,
467 [EL2HLT] = TARGET_EL2HLT,
468 [EDEADLK] = TARGET_EDEADLK,
469 [ENOLCK] = TARGET_ENOLCK,
470 [EBADE] = TARGET_EBADE,
471 [EBADR] = TARGET_EBADR,
472 [EXFULL] = TARGET_EXFULL,
473 [ENOANO] = TARGET_ENOANO,
474 [EBADRQC] = TARGET_EBADRQC,
475 [EBADSLT] = TARGET_EBADSLT,
476 [EBFONT] = TARGET_EBFONT,
477 [ENOSTR] = TARGET_ENOSTR,
478 [ENODATA] = TARGET_ENODATA,
479 [ETIME] = TARGET_ETIME,
480 [ENOSR] = TARGET_ENOSR,
481 [ENONET] = TARGET_ENONET,
482 [ENOPKG] = TARGET_ENOPKG,
483 [EREMOTE] = TARGET_EREMOTE,
484 [ENOLINK] = TARGET_ENOLINK,
485 [EADV] = TARGET_EADV,
486 [ESRMNT] = TARGET_ESRMNT,
487 [ECOMM] = TARGET_ECOMM,
488 [EPROTO] = TARGET_EPROTO,
489 [EDOTDOT] = TARGET_EDOTDOT,
490 [EMULTIHOP] = TARGET_EMULTIHOP,
491 [EBADMSG] = TARGET_EBADMSG,
492 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
493 [EOVERFLOW] = TARGET_EOVERFLOW,
494 [ENOTUNIQ] = TARGET_ENOTUNIQ,
495 [EBADFD] = TARGET_EBADFD,
496 [EREMCHG] = TARGET_EREMCHG,
497 [ELIBACC] = TARGET_ELIBACC,
498 [ELIBBAD] = TARGET_ELIBBAD,
499 [ELIBSCN] = TARGET_ELIBSCN,
500 [ELIBMAX] = TARGET_ELIBMAX,
501 [ELIBEXEC] = TARGET_ELIBEXEC,
502 [EILSEQ] = TARGET_EILSEQ,
503 [ENOSYS] = TARGET_ENOSYS,
504 [ELOOP] = TARGET_ELOOP,
505 [ERESTART] = TARGET_ERESTART,
506 [ESTRPIPE] = TARGET_ESTRPIPE,
507 [ENOTEMPTY] = TARGET_ENOTEMPTY,
508 [EUSERS] = TARGET_EUSERS,
509 [ENOTSOCK] = TARGET_ENOTSOCK,
510 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
511 [EMSGSIZE] = TARGET_EMSGSIZE,
512 [EPROTOTYPE] = TARGET_EPROTOTYPE,
513 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
514 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
515 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
516 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
517 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
518 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
519 [EADDRINUSE] = TARGET_EADDRINUSE,
520 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
521 [ENETDOWN] = TARGET_ENETDOWN,
522 [ENETUNREACH] = TARGET_ENETUNREACH,
523 [ENETRESET] = TARGET_ENETRESET,
524 [ECONNABORTED] = TARGET_ECONNABORTED,
525 [ECONNRESET] = TARGET_ECONNRESET,
526 [ENOBUFS] = TARGET_ENOBUFS,
527 [EISCONN] = TARGET_EISCONN,
528 [ENOTCONN] = TARGET_ENOTCONN,
529 [EUCLEAN] = TARGET_EUCLEAN,
530 [ENOTNAM] = TARGET_ENOTNAM,
531 [ENAVAIL] = TARGET_ENAVAIL,
532 [EISNAM] = TARGET_EISNAM,
533 [EREMOTEIO] = TARGET_EREMOTEIO,
534 [ESHUTDOWN] = TARGET_ESHUTDOWN,
535 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
536 [ETIMEDOUT] = TARGET_ETIMEDOUT,
537 [ECONNREFUSED] = TARGET_ECONNREFUSED,
538 [EHOSTDOWN] = TARGET_EHOSTDOWN,
539 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
540 [EALREADY] = TARGET_EALREADY,
541 [EINPROGRESS] = TARGET_EINPROGRESS,
542 [ESTALE] = TARGET_ESTALE,
543 [ECANCELED] = TARGET_ECANCELED,
544 [ENOMEDIUM] = TARGET_ENOMEDIUM,
545 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
546 #ifdef ENOKEY
547 [ENOKEY] = TARGET_ENOKEY,
548 #endif
549 #ifdef EKEYEXPIRED
550 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
551 #endif
552 #ifdef EKEYREVOKED
553 [EKEYREVOKED] = TARGET_EKEYREVOKED,
554 #endif
555 #ifdef EKEYREJECTED
556 [EKEYREJECTED] = TARGET_EKEYREJECTED,
557 #endif
558 #ifdef EOWNERDEAD
559 [EOWNERDEAD] = TARGET_EOWNERDEAD,
560 #endif
561 #ifdef ENOTRECOVERABLE
562 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
563 #endif
564 };
565
566 static inline int host_to_target_errno(int err)
567 {
568 if(host_to_target_errno_table[err])
569 return host_to_target_errno_table[err];
570 return err;
571 }
572
573 static inline int target_to_host_errno(int err)
574 {
575 if (target_to_host_errno_table[err])
576 return target_to_host_errno_table[err];
577 return err;
578 }
579
580 static inline abi_long get_errno(abi_long ret)
581 {
582 if (ret == -1)
583 return -host_to_target_errno(errno);
584 else
585 return ret;
586 }
587
588 static inline int is_error(abi_long ret)
589 {
590 return (abi_ulong)ret >= (abi_ulong)(-4096);
591 }
592
593 char *target_strerror(int err)
594 {
595 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
596 return NULL;
597 }
598 return strerror(target_to_host_errno(err));
599 }
600
601 static abi_ulong target_brk;
602 static abi_ulong target_original_brk;
603 static abi_ulong brk_page;
604
605 void target_set_brk(abi_ulong new_brk)
606 {
607 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
608 brk_page = HOST_PAGE_ALIGN(target_brk);
609 }
610
611 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
612 #define DEBUGF_BRK(message, args...)
613
614 /* do_brk() must return target values and target errnos. */
615 abi_long do_brk(abi_ulong new_brk)
616 {
617 abi_long mapped_addr;
618 int new_alloc_size;
619
620 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
621
622 if (!new_brk) {
623 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
624 return target_brk;
625 }
626 if (new_brk < target_original_brk) {
627 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
628 target_brk);
629 return target_brk;
630 }
631
632 /* If the new brk is less than the highest page reserved to the
633 * target heap allocation, set it and we're almost done... */
634 if (new_brk <= brk_page) {
635 /* Heap contents are initialized to zero, as for anonymous
636 * mapped pages. */
637 if (new_brk > target_brk) {
638 memset(g2h(target_brk), 0, new_brk - target_brk);
639 }
640 target_brk = new_brk;
641 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
642 return target_brk;
643 }
644
645 /* We need to allocate more memory after the brk... Note that
646 * we don't use MAP_FIXED because that will map over the top of
647 * any existing mapping (like the one with the host libc or qemu
648 * itself); instead we treat "mapped but at wrong address" as
649 * a failure and unmap again.
650 */
651 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
652 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
653 PROT_READ|PROT_WRITE,
654 MAP_ANON|MAP_PRIVATE, 0, 0));
655
656 if (mapped_addr == brk_page) {
657 /* Heap contents are initialized to zero, as for anonymous
658 * mapped pages. Technically the new pages are already
659 * initialized to zero since they *are* anonymous mapped
660 * pages, however we have to take care with the contents that
661 * come from the remaining part of the previous page: it may
662 * contains garbage data due to a previous heap usage (grown
663 * then shrunken). */
664 memset(g2h(target_brk), 0, brk_page - target_brk);
665
666 target_brk = new_brk;
667 brk_page = HOST_PAGE_ALIGN(target_brk);
668 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
669 target_brk);
670 return target_brk;
671 } else if (mapped_addr != -1) {
672 /* Mapped but at wrong address, meaning there wasn't actually
673 * enough space for this brk.
674 */
675 target_munmap(mapped_addr, new_alloc_size);
676 mapped_addr = -1;
677 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
678 }
679 else {
680 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
681 }
682
683 #if defined(TARGET_ALPHA)
684 /* We (partially) emulate OSF/1 on Alpha, which requires we
685 return a proper errno, not an unchanged brk value. */
686 return -TARGET_ENOMEM;
687 #endif
688 /* For everything else, return the previous break. */
689 return target_brk;
690 }
691
692 static inline abi_long copy_from_user_fdset(fd_set *fds,
693 abi_ulong target_fds_addr,
694 int n)
695 {
696 int i, nw, j, k;
697 abi_ulong b, *target_fds;
698
699 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
700 if (!(target_fds = lock_user(VERIFY_READ,
701 target_fds_addr,
702 sizeof(abi_ulong) * nw,
703 1)))
704 return -TARGET_EFAULT;
705
706 FD_ZERO(fds);
707 k = 0;
708 for (i = 0; i < nw; i++) {
709 /* grab the abi_ulong */
710 __get_user(b, &target_fds[i]);
711 for (j = 0; j < TARGET_ABI_BITS; j++) {
712 /* check the bit inside the abi_ulong */
713 if ((b >> j) & 1)
714 FD_SET(k, fds);
715 k++;
716 }
717 }
718
719 unlock_user(target_fds, target_fds_addr, 0);
720
721 return 0;
722 }
723
724 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
725 abi_ulong target_fds_addr,
726 int n)
727 {
728 if (target_fds_addr) {
729 if (copy_from_user_fdset(fds, target_fds_addr, n))
730 return -TARGET_EFAULT;
731 *fds_ptr = fds;
732 } else {
733 *fds_ptr = NULL;
734 }
735 return 0;
736 }
737
738 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
739 const fd_set *fds,
740 int n)
741 {
742 int i, nw, j, k;
743 abi_long v;
744 abi_ulong *target_fds;
745
746 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
747 if (!(target_fds = lock_user(VERIFY_WRITE,
748 target_fds_addr,
749 sizeof(abi_ulong) * nw,
750 0)))
751 return -TARGET_EFAULT;
752
753 k = 0;
754 for (i = 0; i < nw; i++) {
755 v = 0;
756 for (j = 0; j < TARGET_ABI_BITS; j++) {
757 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
758 k++;
759 }
760 __put_user(v, &target_fds[i]);
761 }
762
763 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
764
765 return 0;
766 }
767
768 #if defined(__alpha__)
769 #define HOST_HZ 1024
770 #else
771 #define HOST_HZ 100
772 #endif
773
774 static inline abi_long host_to_target_clock_t(long ticks)
775 {
776 #if HOST_HZ == TARGET_HZ
777 return ticks;
778 #else
779 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
780 #endif
781 }
782
783 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
784 const struct rusage *rusage)
785 {
786 struct target_rusage *target_rusage;
787
788 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
789 return -TARGET_EFAULT;
790 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
791 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
792 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
793 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
794 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
795 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
796 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
797 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
798 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
799 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
800 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
801 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
802 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
803 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
804 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
805 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
806 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
807 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
808 unlock_user_struct(target_rusage, target_addr, 1);
809
810 return 0;
811 }
812
813 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
814 {
815 abi_ulong target_rlim_swap;
816 rlim_t result;
817
818 target_rlim_swap = tswapal(target_rlim);
819 if (target_rlim_swap == TARGET_RLIM_INFINITY)
820 return RLIM_INFINITY;
821
822 result = target_rlim_swap;
823 if (target_rlim_swap != (rlim_t)result)
824 return RLIM_INFINITY;
825
826 return result;
827 }
828
829 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
830 {
831 abi_ulong target_rlim_swap;
832 abi_ulong result;
833
834 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
835 target_rlim_swap = TARGET_RLIM_INFINITY;
836 else
837 target_rlim_swap = rlim;
838 result = tswapal(target_rlim_swap);
839
840 return result;
841 }
842
843 static inline int target_to_host_resource(int code)
844 {
845 switch (code) {
846 case TARGET_RLIMIT_AS:
847 return RLIMIT_AS;
848 case TARGET_RLIMIT_CORE:
849 return RLIMIT_CORE;
850 case TARGET_RLIMIT_CPU:
851 return RLIMIT_CPU;
852 case TARGET_RLIMIT_DATA:
853 return RLIMIT_DATA;
854 case TARGET_RLIMIT_FSIZE:
855 return RLIMIT_FSIZE;
856 case TARGET_RLIMIT_LOCKS:
857 return RLIMIT_LOCKS;
858 case TARGET_RLIMIT_MEMLOCK:
859 return RLIMIT_MEMLOCK;
860 case TARGET_RLIMIT_MSGQUEUE:
861 return RLIMIT_MSGQUEUE;
862 case TARGET_RLIMIT_NICE:
863 return RLIMIT_NICE;
864 case TARGET_RLIMIT_NOFILE:
865 return RLIMIT_NOFILE;
866 case TARGET_RLIMIT_NPROC:
867 return RLIMIT_NPROC;
868 case TARGET_RLIMIT_RSS:
869 return RLIMIT_RSS;
870 case TARGET_RLIMIT_RTPRIO:
871 return RLIMIT_RTPRIO;
872 case TARGET_RLIMIT_SIGPENDING:
873 return RLIMIT_SIGPENDING;
874 case TARGET_RLIMIT_STACK:
875 return RLIMIT_STACK;
876 default:
877 return code;
878 }
879 }
880
881 static inline abi_long copy_from_user_timeval(struct timeval *tv,
882 abi_ulong target_tv_addr)
883 {
884 struct target_timeval *target_tv;
885
886 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
887 return -TARGET_EFAULT;
888
889 __get_user(tv->tv_sec, &target_tv->tv_sec);
890 __get_user(tv->tv_usec, &target_tv->tv_usec);
891
892 unlock_user_struct(target_tv, target_tv_addr, 0);
893
894 return 0;
895 }
896
897 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
898 const struct timeval *tv)
899 {
900 struct target_timeval *target_tv;
901
902 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
903 return -TARGET_EFAULT;
904
905 __put_user(tv->tv_sec, &target_tv->tv_sec);
906 __put_user(tv->tv_usec, &target_tv->tv_usec);
907
908 unlock_user_struct(target_tv, target_tv_addr, 1);
909
910 return 0;
911 }
912
913 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
914 #include <mqueue.h>
915
916 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
917 abi_ulong target_mq_attr_addr)
918 {
919 struct target_mq_attr *target_mq_attr;
920
921 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
922 target_mq_attr_addr, 1))
923 return -TARGET_EFAULT;
924
925 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
926 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
927 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
928 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
929
930 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
931
932 return 0;
933 }
934
935 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
936 const struct mq_attr *attr)
937 {
938 struct target_mq_attr *target_mq_attr;
939
940 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
941 target_mq_attr_addr, 0))
942 return -TARGET_EFAULT;
943
944 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
945 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
946 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
947 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
948
949 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
950
951 return 0;
952 }
953 #endif
954
955 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
956 /* do_select() must return target values and target errnos. */
957 static abi_long do_select(int n,
958 abi_ulong rfd_addr, abi_ulong wfd_addr,
959 abi_ulong efd_addr, abi_ulong target_tv_addr)
960 {
961 fd_set rfds, wfds, efds;
962 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
963 struct timeval tv, *tv_ptr;
964 abi_long ret;
965
966 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
967 if (ret) {
968 return ret;
969 }
970 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
971 if (ret) {
972 return ret;
973 }
974 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
975 if (ret) {
976 return ret;
977 }
978
979 if (target_tv_addr) {
980 if (copy_from_user_timeval(&tv, target_tv_addr))
981 return -TARGET_EFAULT;
982 tv_ptr = &tv;
983 } else {
984 tv_ptr = NULL;
985 }
986
987 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
988
989 if (!is_error(ret)) {
990 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
991 return -TARGET_EFAULT;
992 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
993 return -TARGET_EFAULT;
994 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
995 return -TARGET_EFAULT;
996
997 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
998 return -TARGET_EFAULT;
999 }
1000
1001 return ret;
1002 }
1003 #endif
1004
1005 static abi_long do_pipe2(int host_pipe[], int flags)
1006 {
1007 #ifdef CONFIG_PIPE2
1008 return pipe2(host_pipe, flags);
1009 #else
1010 return -ENOSYS;
1011 #endif
1012 }
1013
1014 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1015 int flags, int is_pipe2)
1016 {
1017 int host_pipe[2];
1018 abi_long ret;
1019 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1020
1021 if (is_error(ret))
1022 return get_errno(ret);
1023
1024 /* Several targets have special calling conventions for the original
1025 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1026 if (!is_pipe2) {
1027 #if defined(TARGET_ALPHA)
1028 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1029 return host_pipe[0];
1030 #elif defined(TARGET_MIPS)
1031 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1032 return host_pipe[0];
1033 #elif defined(TARGET_SH4)
1034 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1035 return host_pipe[0];
1036 #elif defined(TARGET_SPARC)
1037 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1038 return host_pipe[0];
1039 #endif
1040 }
1041
1042 if (put_user_s32(host_pipe[0], pipedes)
1043 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1044 return -TARGET_EFAULT;
1045 return get_errno(ret);
1046 }
1047
1048 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1049 abi_ulong target_addr,
1050 socklen_t len)
1051 {
1052 struct target_ip_mreqn *target_smreqn;
1053
1054 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1055 if (!target_smreqn)
1056 return -TARGET_EFAULT;
1057 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1058 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1059 if (len == sizeof(struct target_ip_mreqn))
1060 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
1061 unlock_user(target_smreqn, target_addr, 0);
1062
1063 return 0;
1064 }
1065
1066 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1067 abi_ulong target_addr,
1068 socklen_t len)
1069 {
1070 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1071 sa_family_t sa_family;
1072 struct target_sockaddr *target_saddr;
1073
1074 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1075 if (!target_saddr)
1076 return -TARGET_EFAULT;
1077
1078 sa_family = tswap16(target_saddr->sa_family);
1079
1080 /* Oops. The caller might send a incomplete sun_path; sun_path
1081 * must be terminated by \0 (see the manual page), but
1082 * unfortunately it is quite common to specify sockaddr_un
1083 * length as "strlen(x->sun_path)" while it should be
1084 * "strlen(...) + 1". We'll fix that here if needed.
1085 * Linux kernel has a similar feature.
1086 */
1087
1088 if (sa_family == AF_UNIX) {
1089 if (len < unix_maxlen && len > 0) {
1090 char *cp = (char*)target_saddr;
1091
1092 if ( cp[len-1] && !cp[len] )
1093 len++;
1094 }
1095 if (len > unix_maxlen)
1096 len = unix_maxlen;
1097 }
1098
1099 memcpy(addr, target_saddr, len);
1100 addr->sa_family = sa_family;
1101 unlock_user(target_saddr, target_addr, 0);
1102
1103 return 0;
1104 }
1105
1106 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1107 struct sockaddr *addr,
1108 socklen_t len)
1109 {
1110 struct target_sockaddr *target_saddr;
1111
1112 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1113 if (!target_saddr)
1114 return -TARGET_EFAULT;
1115 memcpy(target_saddr, addr, len);
1116 target_saddr->sa_family = tswap16(addr->sa_family);
1117 unlock_user(target_saddr, target_addr, len);
1118
1119 return 0;
1120 }
1121
1122 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1123 struct target_msghdr *target_msgh)
1124 {
1125 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1126 abi_long msg_controllen;
1127 abi_ulong target_cmsg_addr;
1128 struct target_cmsghdr *target_cmsg;
1129 socklen_t space = 0;
1130
1131 msg_controllen = tswapal(target_msgh->msg_controllen);
1132 if (msg_controllen < sizeof (struct target_cmsghdr))
1133 goto the_end;
1134 target_cmsg_addr = tswapal(target_msgh->msg_control);
1135 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1136 if (!target_cmsg)
1137 return -TARGET_EFAULT;
1138
1139 while (cmsg && target_cmsg) {
1140 void *data = CMSG_DATA(cmsg);
1141 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1142
1143 int len = tswapal(target_cmsg->cmsg_len)
1144 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1145
1146 space += CMSG_SPACE(len);
1147 if (space > msgh->msg_controllen) {
1148 space -= CMSG_SPACE(len);
1149 gemu_log("Host cmsg overflow\n");
1150 break;
1151 }
1152
1153 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1154 cmsg->cmsg_level = SOL_SOCKET;
1155 } else {
1156 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1157 }
1158 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1159 cmsg->cmsg_len = CMSG_LEN(len);
1160
1161 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1162 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1163 memcpy(data, target_data, len);
1164 } else {
1165 int *fd = (int *)data;
1166 int *target_fd = (int *)target_data;
1167 int i, numfds = len / sizeof(int);
1168
1169 for (i = 0; i < numfds; i++)
1170 fd[i] = tswap32(target_fd[i]);
1171 }
1172
1173 cmsg = CMSG_NXTHDR(msgh, cmsg);
1174 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1175 }
1176 unlock_user(target_cmsg, target_cmsg_addr, 0);
1177 the_end:
1178 msgh->msg_controllen = space;
1179 return 0;
1180 }
1181
1182 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1183 struct msghdr *msgh)
1184 {
1185 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1186 abi_long msg_controllen;
1187 abi_ulong target_cmsg_addr;
1188 struct target_cmsghdr *target_cmsg;
1189 socklen_t space = 0;
1190
1191 msg_controllen = tswapal(target_msgh->msg_controllen);
1192 if (msg_controllen < sizeof (struct target_cmsghdr))
1193 goto the_end;
1194 target_cmsg_addr = tswapal(target_msgh->msg_control);
1195 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1196 if (!target_cmsg)
1197 return -TARGET_EFAULT;
1198
1199 while (cmsg && target_cmsg) {
1200 void *data = CMSG_DATA(cmsg);
1201 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1202
1203 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1204
1205 space += TARGET_CMSG_SPACE(len);
1206 if (space > msg_controllen) {
1207 space -= TARGET_CMSG_SPACE(len);
1208 gemu_log("Target cmsg overflow\n");
1209 break;
1210 }
1211
1212 if (cmsg->cmsg_level == SOL_SOCKET) {
1213 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1214 } else {
1215 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1216 }
1217 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1218 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
1219
1220 if ((cmsg->cmsg_level == SOL_SOCKET) &&
1221 (cmsg->cmsg_type == SCM_RIGHTS)) {
1222 int *fd = (int *)data;
1223 int *target_fd = (int *)target_data;
1224 int i, numfds = len / sizeof(int);
1225
1226 for (i = 0; i < numfds; i++)
1227 target_fd[i] = tswap32(fd[i]);
1228 } else if ((cmsg->cmsg_level == SOL_SOCKET) &&
1229 (cmsg->cmsg_type == SO_TIMESTAMP) &&
1230 (len == sizeof(struct timeval))) {
1231 /* copy struct timeval to target */
1232 struct timeval *tv = (struct timeval *)data;
1233 struct target_timeval *target_tv =
1234 (struct target_timeval *)target_data;
1235
1236 target_tv->tv_sec = tswapal(tv->tv_sec);
1237 target_tv->tv_usec = tswapal(tv->tv_usec);
1238 } else {
1239 gemu_log("Unsupported ancillary data: %d/%d\n",
1240 cmsg->cmsg_level, cmsg->cmsg_type);
1241 memcpy(target_data, data, len);
1242 }
1243
1244 cmsg = CMSG_NXTHDR(msgh, cmsg);
1245 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1246 }
1247 unlock_user(target_cmsg, target_cmsg_addr, space);
1248 the_end:
1249 target_msgh->msg_controllen = tswapal(space);
1250 return 0;
1251 }
1252
1253 /* do_setsockopt() Must return target values and target errnos. */
1254 static abi_long do_setsockopt(int sockfd, int level, int optname,
1255 abi_ulong optval_addr, socklen_t optlen)
1256 {
1257 abi_long ret;
1258 int val;
1259 struct ip_mreqn *ip_mreq;
1260 struct ip_mreq_source *ip_mreq_source;
1261
1262 switch(level) {
1263 case SOL_TCP:
1264 /* TCP options all take an 'int' value. */
1265 if (optlen < sizeof(uint32_t))
1266 return -TARGET_EINVAL;
1267
1268 if (get_user_u32(val, optval_addr))
1269 return -TARGET_EFAULT;
1270 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1271 break;
1272 case SOL_IP:
1273 switch(optname) {
1274 case IP_TOS:
1275 case IP_TTL:
1276 case IP_HDRINCL:
1277 case IP_ROUTER_ALERT:
1278 case IP_RECVOPTS:
1279 case IP_RETOPTS:
1280 case IP_PKTINFO:
1281 case IP_MTU_DISCOVER:
1282 case IP_RECVERR:
1283 case IP_RECVTOS:
1284 #ifdef IP_FREEBIND
1285 case IP_FREEBIND:
1286 #endif
1287 case IP_MULTICAST_TTL:
1288 case IP_MULTICAST_LOOP:
1289 val = 0;
1290 if (optlen >= sizeof(uint32_t)) {
1291 if (get_user_u32(val, optval_addr))
1292 return -TARGET_EFAULT;
1293 } else if (optlen >= 1) {
1294 if (get_user_u8(val, optval_addr))
1295 return -TARGET_EFAULT;
1296 }
1297 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1298 break;
1299 case IP_ADD_MEMBERSHIP:
1300 case IP_DROP_MEMBERSHIP:
1301 if (optlen < sizeof (struct target_ip_mreq) ||
1302 optlen > sizeof (struct target_ip_mreqn))
1303 return -TARGET_EINVAL;
1304
1305 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1306 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1307 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1308 break;
1309
1310 case IP_BLOCK_SOURCE:
1311 case IP_UNBLOCK_SOURCE:
1312 case IP_ADD_SOURCE_MEMBERSHIP:
1313 case IP_DROP_SOURCE_MEMBERSHIP:
1314 if (optlen != sizeof (struct target_ip_mreq_source))
1315 return -TARGET_EINVAL;
1316
1317 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1318 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1319 unlock_user (ip_mreq_source, optval_addr, 0);
1320 break;
1321
1322 default:
1323 goto unimplemented;
1324 }
1325 break;
1326 case SOL_IPV6:
1327 switch (optname) {
1328 case IPV6_MTU_DISCOVER:
1329 case IPV6_MTU:
1330 case IPV6_V6ONLY:
1331 case IPV6_RECVPKTINFO:
1332 val = 0;
1333 if (optlen < sizeof(uint32_t)) {
1334 return -TARGET_EINVAL;
1335 }
1336 if (get_user_u32(val, optval_addr)) {
1337 return -TARGET_EFAULT;
1338 }
1339 ret = get_errno(setsockopt(sockfd, level, optname,
1340 &val, sizeof(val)));
1341 break;
1342 default:
1343 goto unimplemented;
1344 }
1345 break;
1346 case SOL_RAW:
1347 switch (optname) {
1348 case ICMP_FILTER:
1349 /* struct icmp_filter takes an u32 value */
1350 if (optlen < sizeof(uint32_t)) {
1351 return -TARGET_EINVAL;
1352 }
1353
1354 if (get_user_u32(val, optval_addr)) {
1355 return -TARGET_EFAULT;
1356 }
1357 ret = get_errno(setsockopt(sockfd, level, optname,
1358 &val, sizeof(val)));
1359 break;
1360
1361 default:
1362 goto unimplemented;
1363 }
1364 break;
1365 case TARGET_SOL_SOCKET:
1366 switch (optname) {
1367 case TARGET_SO_RCVTIMEO:
1368 {
1369 struct timeval tv;
1370
1371 optname = SO_RCVTIMEO;
1372
1373 set_timeout:
1374 if (optlen != sizeof(struct target_timeval)) {
1375 return -TARGET_EINVAL;
1376 }
1377
1378 if (copy_from_user_timeval(&tv, optval_addr)) {
1379 return -TARGET_EFAULT;
1380 }
1381
1382 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1383 &tv, sizeof(tv)));
1384 return ret;
1385 }
1386 case TARGET_SO_SNDTIMEO:
1387 optname = SO_SNDTIMEO;
1388 goto set_timeout;
1389 case TARGET_SO_ATTACH_FILTER:
1390 {
1391 struct target_sock_fprog *tfprog;
1392 struct target_sock_filter *tfilter;
1393 struct sock_fprog fprog;
1394 struct sock_filter *filter;
1395 int i;
1396
1397 if (optlen != sizeof(*tfprog)) {
1398 return -TARGET_EINVAL;
1399 }
1400 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
1401 return -TARGET_EFAULT;
1402 }
1403 if (!lock_user_struct(VERIFY_READ, tfilter,
1404 tswapal(tfprog->filter), 0)) {
1405 unlock_user_struct(tfprog, optval_addr, 1);
1406 return -TARGET_EFAULT;
1407 }
1408
1409 fprog.len = tswap16(tfprog->len);
1410 filter = malloc(fprog.len * sizeof(*filter));
1411 if (filter == NULL) {
1412 unlock_user_struct(tfilter, tfprog->filter, 1);
1413 unlock_user_struct(tfprog, optval_addr, 1);
1414 return -TARGET_ENOMEM;
1415 }
1416 for (i = 0; i < fprog.len; i++) {
1417 filter[i].code = tswap16(tfilter[i].code);
1418 filter[i].jt = tfilter[i].jt;
1419 filter[i].jf = tfilter[i].jf;
1420 filter[i].k = tswap32(tfilter[i].k);
1421 }
1422 fprog.filter = filter;
1423
1424 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
1425 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
1426 free(filter);
1427
1428 unlock_user_struct(tfilter, tfprog->filter, 1);
1429 unlock_user_struct(tfprog, optval_addr, 1);
1430 return ret;
1431 }
1432 /* Options with 'int' argument. */
1433 case TARGET_SO_DEBUG:
1434 optname = SO_DEBUG;
1435 break;
1436 case TARGET_SO_REUSEADDR:
1437 optname = SO_REUSEADDR;
1438 break;
1439 case TARGET_SO_TYPE:
1440 optname = SO_TYPE;
1441 break;
1442 case TARGET_SO_ERROR:
1443 optname = SO_ERROR;
1444 break;
1445 case TARGET_SO_DONTROUTE:
1446 optname = SO_DONTROUTE;
1447 break;
1448 case TARGET_SO_BROADCAST:
1449 optname = SO_BROADCAST;
1450 break;
1451 case TARGET_SO_SNDBUF:
1452 optname = SO_SNDBUF;
1453 break;
1454 case TARGET_SO_RCVBUF:
1455 optname = SO_RCVBUF;
1456 break;
1457 case TARGET_SO_KEEPALIVE:
1458 optname = SO_KEEPALIVE;
1459 break;
1460 case TARGET_SO_OOBINLINE:
1461 optname = SO_OOBINLINE;
1462 break;
1463 case TARGET_SO_NO_CHECK:
1464 optname = SO_NO_CHECK;
1465 break;
1466 case TARGET_SO_PRIORITY:
1467 optname = SO_PRIORITY;
1468 break;
1469 #ifdef SO_BSDCOMPAT
1470 case TARGET_SO_BSDCOMPAT:
1471 optname = SO_BSDCOMPAT;
1472 break;
1473 #endif
1474 case TARGET_SO_PASSCRED:
1475 optname = SO_PASSCRED;
1476 break;
1477 case TARGET_SO_TIMESTAMP:
1478 optname = SO_TIMESTAMP;
1479 break;
1480 case TARGET_SO_RCVLOWAT:
1481 optname = SO_RCVLOWAT;
1482 break;
1483 break;
1484 default:
1485 goto unimplemented;
1486 }
1487 if (optlen < sizeof(uint32_t))
1488 return -TARGET_EINVAL;
1489
1490 if (get_user_u32(val, optval_addr))
1491 return -TARGET_EFAULT;
1492 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
1493 break;
1494 default:
1495 unimplemented:
1496 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
1497 ret = -TARGET_ENOPROTOOPT;
1498 }
1499 return ret;
1500 }
1501
1502 /* do_getsockopt() Must return target values and target errnos. */
1503 static abi_long do_getsockopt(int sockfd, int level, int optname,
1504 abi_ulong optval_addr, abi_ulong optlen)
1505 {
1506 abi_long ret;
1507 int len, val;
1508 socklen_t lv;
1509
1510 switch(level) {
1511 case TARGET_SOL_SOCKET:
1512 level = SOL_SOCKET;
1513 switch (optname) {
1514 /* These don't just return a single integer */
1515 case TARGET_SO_LINGER:
1516 case TARGET_SO_RCVTIMEO:
1517 case TARGET_SO_SNDTIMEO:
1518 case TARGET_SO_PEERNAME:
1519 goto unimplemented;
1520 case TARGET_SO_PEERCRED: {
1521 struct ucred cr;
1522 socklen_t crlen;
1523 struct target_ucred *tcr;
1524
1525 if (get_user_u32(len, optlen)) {
1526 return -TARGET_EFAULT;
1527 }
1528 if (len < 0) {
1529 return -TARGET_EINVAL;
1530 }
1531
1532 crlen = sizeof(cr);
1533 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1534 &cr, &crlen));
1535 if (ret < 0) {
1536 return ret;
1537 }
1538 if (len > crlen) {
1539 len = crlen;
1540 }
1541 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1542 return -TARGET_EFAULT;
1543 }
1544 __put_user(cr.pid, &tcr->pid);
1545 __put_user(cr.uid, &tcr->uid);
1546 __put_user(cr.gid, &tcr->gid);
1547 unlock_user_struct(tcr, optval_addr, 1);
1548 if (put_user_u32(len, optlen)) {
1549 return -TARGET_EFAULT;
1550 }
1551 break;
1552 }
1553 /* Options with 'int' argument. */
1554 case TARGET_SO_DEBUG:
1555 optname = SO_DEBUG;
1556 goto int_case;
1557 case TARGET_SO_REUSEADDR:
1558 optname = SO_REUSEADDR;
1559 goto int_case;
1560 case TARGET_SO_TYPE:
1561 optname = SO_TYPE;
1562 goto int_case;
1563 case TARGET_SO_ERROR:
1564 optname = SO_ERROR;
1565 goto int_case;
1566 case TARGET_SO_DONTROUTE:
1567 optname = SO_DONTROUTE;
1568 goto int_case;
1569 case TARGET_SO_BROADCAST:
1570 optname = SO_BROADCAST;
1571 goto int_case;
1572 case TARGET_SO_SNDBUF:
1573 optname = SO_SNDBUF;
1574 goto int_case;
1575 case TARGET_SO_RCVBUF:
1576 optname = SO_RCVBUF;
1577 goto int_case;
1578 case TARGET_SO_KEEPALIVE:
1579 optname = SO_KEEPALIVE;
1580 goto int_case;
1581 case TARGET_SO_OOBINLINE:
1582 optname = SO_OOBINLINE;
1583 goto int_case;
1584 case TARGET_SO_NO_CHECK:
1585 optname = SO_NO_CHECK;
1586 goto int_case;
1587 case TARGET_SO_PRIORITY:
1588 optname = SO_PRIORITY;
1589 goto int_case;
1590 #ifdef SO_BSDCOMPAT
1591 case TARGET_SO_BSDCOMPAT:
1592 optname = SO_BSDCOMPAT;
1593 goto int_case;
1594 #endif
1595 case TARGET_SO_PASSCRED:
1596 optname = SO_PASSCRED;
1597 goto int_case;
1598 case TARGET_SO_TIMESTAMP:
1599 optname = SO_TIMESTAMP;
1600 goto int_case;
1601 case TARGET_SO_RCVLOWAT:
1602 optname = SO_RCVLOWAT;
1603 goto int_case;
1604 default:
1605 goto int_case;
1606 }
1607 break;
1608 case SOL_TCP:
1609 /* TCP options all take an 'int' value. */
1610 int_case:
1611 if (get_user_u32(len, optlen))
1612 return -TARGET_EFAULT;
1613 if (len < 0)
1614 return -TARGET_EINVAL;
1615 lv = sizeof(lv);
1616 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1617 if (ret < 0)
1618 return ret;
1619 if (len > lv)
1620 len = lv;
1621 if (len == 4) {
1622 if (put_user_u32(val, optval_addr))
1623 return -TARGET_EFAULT;
1624 } else {
1625 if (put_user_u8(val, optval_addr))
1626 return -TARGET_EFAULT;
1627 }
1628 if (put_user_u32(len, optlen))
1629 return -TARGET_EFAULT;
1630 break;
1631 case SOL_IP:
1632 switch(optname) {
1633 case IP_TOS:
1634 case IP_TTL:
1635 case IP_HDRINCL:
1636 case IP_ROUTER_ALERT:
1637 case IP_RECVOPTS:
1638 case IP_RETOPTS:
1639 case IP_PKTINFO:
1640 case IP_MTU_DISCOVER:
1641 case IP_RECVERR:
1642 case IP_RECVTOS:
1643 #ifdef IP_FREEBIND
1644 case IP_FREEBIND:
1645 #endif
1646 case IP_MULTICAST_TTL:
1647 case IP_MULTICAST_LOOP:
1648 if (get_user_u32(len, optlen))
1649 return -TARGET_EFAULT;
1650 if (len < 0)
1651 return -TARGET_EINVAL;
1652 lv = sizeof(lv);
1653 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1654 if (ret < 0)
1655 return ret;
1656 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1657 len = 1;
1658 if (put_user_u32(len, optlen)
1659 || put_user_u8(val, optval_addr))
1660 return -TARGET_EFAULT;
1661 } else {
1662 if (len > sizeof(int))
1663 len = sizeof(int);
1664 if (put_user_u32(len, optlen)
1665 || put_user_u32(val, optval_addr))
1666 return -TARGET_EFAULT;
1667 }
1668 break;
1669 default:
1670 ret = -TARGET_ENOPROTOOPT;
1671 break;
1672 }
1673 break;
1674 default:
1675 unimplemented:
1676 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1677 level, optname);
1678 ret = -TARGET_EOPNOTSUPP;
1679 break;
1680 }
1681 return ret;
1682 }
1683
1684 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1685 int count, int copy)
1686 {
1687 struct target_iovec *target_vec;
1688 struct iovec *vec;
1689 abi_ulong total_len, max_len;
1690 int i;
1691
1692 if (count == 0) {
1693 errno = 0;
1694 return NULL;
1695 }
1696 if (count < 0 || count > IOV_MAX) {
1697 errno = EINVAL;
1698 return NULL;
1699 }
1700
1701 vec = calloc(count, sizeof(struct iovec));
1702 if (vec == NULL) {
1703 errno = ENOMEM;
1704 return NULL;
1705 }
1706
1707 target_vec = lock_user(VERIFY_READ, target_addr,
1708 count * sizeof(struct target_iovec), 1);
1709 if (target_vec == NULL) {
1710 errno = EFAULT;
1711 goto fail2;
1712 }
1713
1714 /* ??? If host page size > target page size, this will result in a
1715 value larger than what we can actually support. */
1716 max_len = 0x7fffffff & TARGET_PAGE_MASK;
1717 total_len = 0;
1718
1719 for (i = 0; i < count; i++) {
1720 abi_ulong base = tswapal(target_vec[i].iov_base);
1721 abi_long len = tswapal(target_vec[i].iov_len);
1722
1723 if (len < 0) {
1724 errno = EINVAL;
1725 goto fail;
1726 } else if (len == 0) {
1727 /* Zero length pointer is ignored. */
1728 vec[i].iov_base = 0;
1729 } else {
1730 vec[i].iov_base = lock_user(type, base, len, copy);
1731 if (!vec[i].iov_base) {
1732 errno = EFAULT;
1733 goto fail;
1734 }
1735 if (len > max_len - total_len) {
1736 len = max_len - total_len;
1737 }
1738 }
1739 vec[i].iov_len = len;
1740 total_len += len;
1741 }
1742
1743 unlock_user(target_vec, target_addr, 0);
1744 return vec;
1745
1746 fail:
1747 free(vec);
1748 fail2:
1749 unlock_user(target_vec, target_addr, 0);
1750 return NULL;
1751 }
1752
1753 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1754 int count, int copy)
1755 {
1756 struct target_iovec *target_vec;
1757 int i;
1758
1759 target_vec = lock_user(VERIFY_READ, target_addr,
1760 count * sizeof(struct target_iovec), 1);
1761 if (target_vec) {
1762 for (i = 0; i < count; i++) {
1763 abi_ulong base = tswapal(target_vec[i].iov_base);
1764 abi_long len = tswapal(target_vec[i].iov_base);
1765 if (len < 0) {
1766 break;
1767 }
1768 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1769 }
1770 unlock_user(target_vec, target_addr, 0);
1771 }
1772
1773 free(vec);
1774 }
1775
1776 static inline void target_to_host_sock_type(int *type)
1777 {
1778 int host_type = 0;
1779 int target_type = *type;
1780
1781 switch (target_type & TARGET_SOCK_TYPE_MASK) {
1782 case TARGET_SOCK_DGRAM:
1783 host_type = SOCK_DGRAM;
1784 break;
1785 case TARGET_SOCK_STREAM:
1786 host_type = SOCK_STREAM;
1787 break;
1788 default:
1789 host_type = target_type & TARGET_SOCK_TYPE_MASK;
1790 break;
1791 }
1792 if (target_type & TARGET_SOCK_CLOEXEC) {
1793 host_type |= SOCK_CLOEXEC;
1794 }
1795 if (target_type & TARGET_SOCK_NONBLOCK) {
1796 host_type |= SOCK_NONBLOCK;
1797 }
1798 *type = host_type;
1799 }
1800
1801 /* do_socket() Must return target values and target errnos. */
1802 static abi_long do_socket(int domain, int type, int protocol)
1803 {
1804 target_to_host_sock_type(&type);
1805
1806 if (domain == PF_NETLINK)
1807 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1808 return get_errno(socket(domain, type, protocol));
1809 }
1810
1811 /* do_bind() Must return target values and target errnos. */
1812 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1813 socklen_t addrlen)
1814 {
1815 void *addr;
1816 abi_long ret;
1817
1818 if ((int)addrlen < 0) {
1819 return -TARGET_EINVAL;
1820 }
1821
1822 addr = alloca(addrlen+1);
1823
1824 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1825 if (ret)
1826 return ret;
1827
1828 return get_errno(bind(sockfd, addr, addrlen));
1829 }
1830
1831 /* do_connect() Must return target values and target errnos. */
1832 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1833 socklen_t addrlen)
1834 {
1835 void *addr;
1836 abi_long ret;
1837
1838 if ((int)addrlen < 0) {
1839 return -TARGET_EINVAL;
1840 }
1841
1842 addr = alloca(addrlen);
1843
1844 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1845 if (ret)
1846 return ret;
1847
1848 return get_errno(connect(sockfd, addr, addrlen));
1849 }
1850
1851 /* do_sendrecvmsg() Must return target values and target errnos. */
1852 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1853 int flags, int send)
1854 {
1855 abi_long ret, len;
1856 struct target_msghdr *msgp;
1857 struct msghdr msg;
1858 int count;
1859 struct iovec *vec;
1860 abi_ulong target_vec;
1861
1862 /* FIXME */
1863 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1864 msgp,
1865 target_msg,
1866 send ? 1 : 0))
1867 return -TARGET_EFAULT;
1868 if (msgp->msg_name) {
1869 msg.msg_namelen = tswap32(msgp->msg_namelen);
1870 msg.msg_name = alloca(msg.msg_namelen);
1871 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
1872 msg.msg_namelen);
1873 if (ret) {
1874 goto out2;
1875 }
1876 } else {
1877 msg.msg_name = NULL;
1878 msg.msg_namelen = 0;
1879 }
1880 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
1881 msg.msg_control = alloca(msg.msg_controllen);
1882 msg.msg_flags = tswap32(msgp->msg_flags);
1883
1884 count = tswapal(msgp->msg_iovlen);
1885 target_vec = tswapal(msgp->msg_iov);
1886 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
1887 target_vec, count, send);
1888 if (vec == NULL) {
1889 ret = -host_to_target_errno(errno);
1890 goto out2;
1891 }
1892 msg.msg_iovlen = count;
1893 msg.msg_iov = vec;
1894
1895 if (send) {
1896 ret = target_to_host_cmsg(&msg, msgp);
1897 if (ret == 0)
1898 ret = get_errno(sendmsg(fd, &msg, flags));
1899 } else {
1900 ret = get_errno(recvmsg(fd, &msg, flags));
1901 if (!is_error(ret)) {
1902 len = ret;
1903 ret = host_to_target_cmsg(msgp, &msg);
1904 if (!is_error(ret)) {
1905 msgp->msg_namelen = tswap32(msg.msg_namelen);
1906 if (msg.msg_name != NULL) {
1907 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
1908 msg.msg_name, msg.msg_namelen);
1909 if (ret) {
1910 goto out;
1911 }
1912 }
1913
1914 ret = len;
1915 }
1916 }
1917 }
1918
1919 out:
1920 unlock_iovec(vec, target_vec, count, !send);
1921 out2:
1922 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1923 return ret;
1924 }
1925
1926 /* If we don't have a system accept4() then just call accept.
1927 * The callsites to do_accept4() will ensure that they don't
1928 * pass a non-zero flags argument in this config.
1929 */
1930 #ifndef CONFIG_ACCEPT4
1931 static inline int accept4(int sockfd, struct sockaddr *addr,
1932 socklen_t *addrlen, int flags)
1933 {
1934 assert(flags == 0);
1935 return accept(sockfd, addr, addrlen);
1936 }
1937 #endif
1938
1939 /* do_accept4() Must return target values and target errnos. */
1940 static abi_long do_accept4(int fd, abi_ulong target_addr,
1941 abi_ulong target_addrlen_addr, int flags)
1942 {
1943 socklen_t addrlen;
1944 void *addr;
1945 abi_long ret;
1946
1947 if (target_addr == 0) {
1948 return get_errno(accept4(fd, NULL, NULL, flags));
1949 }
1950
1951 /* linux returns EINVAL if addrlen pointer is invalid */
1952 if (get_user_u32(addrlen, target_addrlen_addr))
1953 return -TARGET_EINVAL;
1954
1955 if ((int)addrlen < 0) {
1956 return -TARGET_EINVAL;
1957 }
1958
1959 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1960 return -TARGET_EINVAL;
1961
1962 addr = alloca(addrlen);
1963
1964 ret = get_errno(accept4(fd, addr, &addrlen, flags));
1965 if (!is_error(ret)) {
1966 host_to_target_sockaddr(target_addr, addr, addrlen);
1967 if (put_user_u32(addrlen, target_addrlen_addr))
1968 ret = -TARGET_EFAULT;
1969 }
1970 return ret;
1971 }
1972
1973 /* do_getpeername() Must return target values and target errnos. */
1974 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1975 abi_ulong target_addrlen_addr)
1976 {
1977 socklen_t addrlen;
1978 void *addr;
1979 abi_long ret;
1980
1981 if (get_user_u32(addrlen, target_addrlen_addr))
1982 return -TARGET_EFAULT;
1983
1984 if ((int)addrlen < 0) {
1985 return -TARGET_EINVAL;
1986 }
1987
1988 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1989 return -TARGET_EFAULT;
1990
1991 addr = alloca(addrlen);
1992
1993 ret = get_errno(getpeername(fd, addr, &addrlen));
1994 if (!is_error(ret)) {
1995 host_to_target_sockaddr(target_addr, addr, addrlen);
1996 if (put_user_u32(addrlen, target_addrlen_addr))
1997 ret = -TARGET_EFAULT;
1998 }
1999 return ret;
2000 }
2001
2002 /* do_getsockname() Must return target values and target errnos. */
2003 static abi_long do_getsockname(int fd, abi_ulong target_addr,
2004 abi_ulong target_addrlen_addr)
2005 {
2006 socklen_t addrlen;
2007 void *addr;
2008 abi_long ret;
2009
2010 if (get_user_u32(addrlen, target_addrlen_addr))
2011 return -TARGET_EFAULT;
2012
2013 if ((int)addrlen < 0) {
2014 return -TARGET_EINVAL;
2015 }
2016
2017 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2018 return -TARGET_EFAULT;
2019
2020 addr = alloca(addrlen);
2021
2022 ret = get_errno(getsockname(fd, addr, &addrlen));
2023 if (!is_error(ret)) {
2024 host_to_target_sockaddr(target_addr, addr, addrlen);
2025 if (put_user_u32(addrlen, target_addrlen_addr))
2026 ret = -TARGET_EFAULT;
2027 }
2028 return ret;
2029 }
2030
2031 /* do_socketpair() Must return target values and target errnos. */
2032 static abi_long do_socketpair(int domain, int type, int protocol,
2033 abi_ulong target_tab_addr)
2034 {
2035 int tab[2];
2036 abi_long ret;
2037
2038 target_to_host_sock_type(&type);
2039
2040 ret = get_errno(socketpair(domain, type, protocol, tab));
2041 if (!is_error(ret)) {
2042 if (put_user_s32(tab[0], target_tab_addr)
2043 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2044 ret = -TARGET_EFAULT;
2045 }
2046 return ret;
2047 }
2048
2049 /* do_sendto() Must return target values and target errnos. */
2050 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2051 abi_ulong target_addr, socklen_t addrlen)
2052 {
2053 void *addr;
2054 void *host_msg;
2055 abi_long ret;
2056
2057 if ((int)addrlen < 0) {
2058 return -TARGET_EINVAL;
2059 }
2060
2061 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2062 if (!host_msg)
2063 return -TARGET_EFAULT;
2064 if (target_addr) {
2065 addr = alloca(addrlen);
2066 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2067 if (ret) {
2068 unlock_user(host_msg, msg, 0);
2069 return ret;
2070 }
2071 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2072 } else {
2073 ret = get_errno(send(fd, host_msg, len, flags));
2074 }
2075 unlock_user(host_msg, msg, 0);
2076 return ret;
2077 }
2078
2079 /* do_recvfrom() Must return target values and target errnos. */
2080 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2081 abi_ulong target_addr,
2082 abi_ulong target_addrlen)
2083 {
2084 socklen_t addrlen;
2085 void *addr;
2086 void *host_msg;
2087 abi_long ret;
2088
2089 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2090 if (!host_msg)
2091 return -TARGET_EFAULT;
2092 if (target_addr) {
2093 if (get_user_u32(addrlen, target_addrlen)) {
2094 ret = -TARGET_EFAULT;
2095 goto fail;
2096 }
2097 if ((int)addrlen < 0) {
2098 ret = -TARGET_EINVAL;
2099 goto fail;
2100 }
2101 addr = alloca(addrlen);
2102 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2103 } else {
2104 addr = NULL; /* To keep compiler quiet. */
2105 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
2106 }
2107 if (!is_error(ret)) {
2108 if (target_addr) {
2109 host_to_target_sockaddr(target_addr, addr, addrlen);
2110 if (put_user_u32(addrlen, target_addrlen)) {
2111 ret = -TARGET_EFAULT;
2112 goto fail;
2113 }
2114 }
2115 unlock_user(host_msg, msg, len);
2116 } else {
2117 fail:
2118 unlock_user(host_msg, msg, 0);
2119 }
2120 return ret;
2121 }
2122
2123 #ifdef TARGET_NR_socketcall
2124 /* do_socketcall() Must return target values and target errnos. */
2125 static abi_long do_socketcall(int num, abi_ulong vptr)
2126 {
2127 abi_long ret;
2128 const int n = sizeof(abi_ulong);
2129
2130 switch(num) {
2131 case SOCKOP_socket:
2132 {
2133 abi_ulong domain, type, protocol;
2134
2135 if (get_user_ual(domain, vptr)
2136 || get_user_ual(type, vptr + n)
2137 || get_user_ual(protocol, vptr + 2 * n))
2138 return -TARGET_EFAULT;
2139
2140 ret = do_socket(domain, type, protocol);
2141 }
2142 break;
2143 case SOCKOP_bind:
2144 {
2145 abi_ulong sockfd;
2146 abi_ulong target_addr;
2147 socklen_t addrlen;
2148
2149 if (get_user_ual(sockfd, vptr)
2150 || get_user_ual(target_addr, vptr + n)
2151 || get_user_ual(addrlen, vptr + 2 * n))
2152 return -TARGET_EFAULT;
2153
2154 ret = do_bind(sockfd, target_addr, addrlen);
2155 }
2156 break;
2157 case SOCKOP_connect:
2158 {
2159 abi_ulong sockfd;
2160 abi_ulong target_addr;
2161 socklen_t addrlen;
2162
2163 if (get_user_ual(sockfd, vptr)
2164 || get_user_ual(target_addr, vptr + n)
2165 || get_user_ual(addrlen, vptr + 2 * n))
2166 return -TARGET_EFAULT;
2167
2168 ret = do_connect(sockfd, target_addr, addrlen);
2169 }
2170 break;
2171 case SOCKOP_listen:
2172 {
2173 abi_ulong sockfd, backlog;
2174
2175 if (get_user_ual(sockfd, vptr)
2176 || get_user_ual(backlog, vptr + n))
2177 return -TARGET_EFAULT;
2178
2179 ret = get_errno(listen(sockfd, backlog));
2180 }
2181 break;
2182 case SOCKOP_accept:
2183 {
2184 abi_ulong sockfd;
2185 abi_ulong target_addr, target_addrlen;
2186
2187 if (get_user_ual(sockfd, vptr)
2188 || get_user_ual(target_addr, vptr + n)
2189 || get_user_ual(target_addrlen, vptr + 2 * n))
2190 return -TARGET_EFAULT;
2191
2192 ret = do_accept4(sockfd, target_addr, target_addrlen, 0);
2193 }
2194 break;
2195 case SOCKOP_getsockname:
2196 {
2197 abi_ulong sockfd;
2198 abi_ulong target_addr, target_addrlen;
2199
2200 if (get_user_ual(sockfd, vptr)
2201 || get_user_ual(target_addr, vptr + n)
2202 || get_user_ual(target_addrlen, vptr + 2 * n))
2203 return -TARGET_EFAULT;
2204
2205 ret = do_getsockname(sockfd, target_addr, target_addrlen);
2206 }
2207 break;
2208 case SOCKOP_getpeername:
2209 {
2210 abi_ulong sockfd;
2211 abi_ulong target_addr, target_addrlen;
2212
2213 if (get_user_ual(sockfd, vptr)
2214 || get_user_ual(target_addr, vptr + n)
2215 || get_user_ual(target_addrlen, vptr + 2 * n))
2216 return -TARGET_EFAULT;
2217
2218 ret = do_getpeername(sockfd, target_addr, target_addrlen);
2219 }
2220 break;
2221 case SOCKOP_socketpair:
2222 {
2223 abi_ulong domain, type, protocol;
2224 abi_ulong tab;
2225
2226 if (get_user_ual(domain, vptr)
2227 || get_user_ual(type, vptr + n)
2228 || get_user_ual(protocol, vptr + 2 * n)
2229 || get_user_ual(tab, vptr + 3 * n))
2230 return -TARGET_EFAULT;
2231
2232 ret = do_socketpair(domain, type, protocol, tab);
2233 }
2234 break;
2235 case SOCKOP_send:
2236 {
2237 abi_ulong sockfd;
2238 abi_ulong msg;
2239 size_t len;
2240 abi_ulong flags;
2241
2242 if (get_user_ual(sockfd, vptr)
2243 || get_user_ual(msg, vptr + n)
2244 || get_user_ual(len, vptr + 2 * n)
2245 || get_user_ual(flags, vptr + 3 * n))
2246 return -TARGET_EFAULT;
2247
2248 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
2249 }
2250 break;
2251 case SOCKOP_recv:
2252 {
2253 abi_ulong sockfd;
2254 abi_ulong msg;
2255 size_t len;
2256 abi_ulong flags;
2257
2258 if (get_user_ual(sockfd, vptr)
2259 || get_user_ual(msg, vptr + n)
2260 || get_user_ual(len, vptr + 2 * n)
2261 || get_user_ual(flags, vptr + 3 * n))
2262 return -TARGET_EFAULT;
2263
2264 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
2265 }
2266 break;
2267 case SOCKOP_sendto:
2268 {
2269 abi_ulong sockfd;
2270 abi_ulong msg;
2271 size_t len;
2272 abi_ulong flags;
2273 abi_ulong addr;
2274 socklen_t addrlen;
2275
2276 if (get_user_ual(sockfd, vptr)
2277 || get_user_ual(msg, vptr + n)
2278 || get_user_ual(len, vptr + 2 * n)
2279 || get_user_ual(flags, vptr + 3 * n)
2280 || get_user_ual(addr, vptr + 4 * n)
2281 || get_user_ual(addrlen, vptr + 5 * n))
2282 return -TARGET_EFAULT;
2283
2284 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
2285 }
2286 break;
2287 case SOCKOP_recvfrom:
2288 {
2289 abi_ulong sockfd;
2290 abi_ulong msg;
2291 size_t len;
2292 abi_ulong flags;
2293 abi_ulong addr;
2294 socklen_t addrlen;
2295
2296 if (get_user_ual(sockfd, vptr)
2297 || get_user_ual(msg, vptr + n)
2298 || get_user_ual(len, vptr + 2 * n)
2299 || get_user_ual(flags, vptr + 3 * n)
2300 || get_user_ual(addr, vptr + 4 * n)
2301 || get_user_ual(addrlen, vptr + 5 * n))
2302 return -TARGET_EFAULT;
2303
2304 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
2305 }
2306 break;
2307 case SOCKOP_shutdown:
2308 {
2309 abi_ulong sockfd, how;
2310
2311 if (get_user_ual(sockfd, vptr)
2312 || get_user_ual(how, vptr + n))
2313 return -TARGET_EFAULT;
2314
2315 ret = get_errno(shutdown(sockfd, how));
2316 }
2317 break;
2318 case SOCKOP_sendmsg:
2319 case SOCKOP_recvmsg:
2320 {
2321 abi_ulong fd;
2322 abi_ulong target_msg;
2323 abi_ulong flags;
2324
2325 if (get_user_ual(fd, vptr)
2326 || get_user_ual(target_msg, vptr + n)
2327 || get_user_ual(flags, vptr + 2 * n))
2328 return -TARGET_EFAULT;
2329
2330 ret = do_sendrecvmsg(fd, target_msg, flags,
2331 (num == SOCKOP_sendmsg));
2332 }
2333 break;
2334 case SOCKOP_setsockopt:
2335 {
2336 abi_ulong sockfd;
2337 abi_ulong level;
2338 abi_ulong optname;
2339 abi_ulong optval;
2340 socklen_t optlen;
2341
2342 if (get_user_ual(sockfd, vptr)
2343 || get_user_ual(level, vptr + n)
2344 || get_user_ual(optname, vptr + 2 * n)
2345 || get_user_ual(optval, vptr + 3 * n)
2346 || get_user_ual(optlen, vptr + 4 * n))
2347 return -TARGET_EFAULT;
2348
2349 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2350 }
2351 break;
2352 case SOCKOP_getsockopt:
2353 {
2354 abi_ulong sockfd;
2355 abi_ulong level;
2356 abi_ulong optname;
2357 abi_ulong optval;
2358 socklen_t optlen;
2359
2360 if (get_user_ual(sockfd, vptr)
2361 || get_user_ual(level, vptr + n)
2362 || get_user_ual(optname, vptr + 2 * n)
2363 || get_user_ual(optval, vptr + 3 * n)
2364 || get_user_ual(optlen, vptr + 4 * n))
2365 return -TARGET_EFAULT;
2366
2367 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
2368 }
2369 break;
2370 default:
2371 gemu_log("Unsupported socketcall: %d\n", num);
2372 ret = -TARGET_ENOSYS;
2373 break;
2374 }
2375 return ret;
2376 }
2377 #endif
2378
2379 #define N_SHM_REGIONS 32
2380
2381 static struct shm_region {
2382 abi_ulong start;
2383 abi_ulong size;
2384 } shm_regions[N_SHM_REGIONS];
2385
2386 struct target_ipc_perm
2387 {
2388 abi_long __key;
2389 abi_ulong uid;
2390 abi_ulong gid;
2391 abi_ulong cuid;
2392 abi_ulong cgid;
2393 unsigned short int mode;
2394 unsigned short int __pad1;
2395 unsigned short int __seq;
2396 unsigned short int __pad2;
2397 abi_ulong __unused1;
2398 abi_ulong __unused2;
2399 };
2400
2401 struct target_semid_ds
2402 {
2403 struct target_ipc_perm sem_perm;
2404 abi_ulong sem_otime;
2405 abi_ulong __unused1;
2406 abi_ulong sem_ctime;
2407 abi_ulong __unused2;
2408 abi_ulong sem_nsems;
2409 abi_ulong __unused3;
2410 abi_ulong __unused4;
2411 };
2412
2413 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2414 abi_ulong target_addr)
2415 {
2416 struct target_ipc_perm *target_ip;
2417 struct target_semid_ds *target_sd;
2418
2419 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2420 return -TARGET_EFAULT;
2421 target_ip = &(target_sd->sem_perm);
2422 host_ip->__key = tswapal(target_ip->__key);
2423 host_ip->uid = tswapal(target_ip->uid);
2424 host_ip->gid = tswapal(target_ip->gid);
2425 host_ip->cuid = tswapal(target_ip->cuid);
2426 host_ip->cgid = tswapal(target_ip->cgid);
2427 host_ip->mode = tswap16(target_ip->mode);
2428 unlock_user_struct(target_sd, target_addr, 0);
2429 return 0;
2430 }
2431
2432 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2433 struct ipc_perm *host_ip)
2434 {
2435 struct target_ipc_perm *target_ip;
2436 struct target_semid_ds *target_sd;
2437
2438 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2439 return -TARGET_EFAULT;
2440 target_ip = &(target_sd->sem_perm);
2441 target_ip->__key = tswapal(host_ip->__key);
2442 target_ip->uid = tswapal(host_ip->uid);
2443 target_ip->gid = tswapal(host_ip->gid);
2444 target_ip->cuid = tswapal(host_ip->cuid);
2445 target_ip->cgid = tswapal(host_ip->cgid);
2446 target_ip->mode = tswap16(host_ip->mode);
2447 unlock_user_struct(target_sd, target_addr, 1);
2448 return 0;
2449 }
2450
2451 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2452 abi_ulong target_addr)
2453 {
2454 struct target_semid_ds *target_sd;
2455
2456 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2457 return -TARGET_EFAULT;
2458 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2459 return -TARGET_EFAULT;
2460 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2461 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2462 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
2463 unlock_user_struct(target_sd, target_addr, 0);
2464 return 0;
2465 }
2466
2467 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2468 struct semid_ds *host_sd)
2469 {
2470 struct target_semid_ds *target_sd;
2471
2472 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2473 return -TARGET_EFAULT;
2474 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2475 return -TARGET_EFAULT;
2476 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2477 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2478 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
2479 unlock_user_struct(target_sd, target_addr, 1);
2480 return 0;
2481 }
2482
2483 struct target_seminfo {
2484 int semmap;
2485 int semmni;
2486 int semmns;
2487 int semmnu;
2488 int semmsl;
2489 int semopm;
2490 int semume;
2491 int semusz;
2492 int semvmx;
2493 int semaem;
2494 };
2495
2496 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2497 struct seminfo *host_seminfo)
2498 {
2499 struct target_seminfo *target_seminfo;
2500 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2501 return -TARGET_EFAULT;
2502 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2503 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2504 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2505 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2506 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2507 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2508 __put_user(host_seminfo->semume, &target_seminfo->semume);
2509 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2510 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2511 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2512 unlock_user_struct(target_seminfo, target_addr, 1);
2513 return 0;
2514 }
2515
2516 union semun {
2517 int val;
2518 struct semid_ds *buf;
2519 unsigned short *array;
2520 struct seminfo *__buf;
2521 };
2522
2523 union target_semun {
2524 int val;
2525 abi_ulong buf;
2526 abi_ulong array;
2527 abi_ulong __buf;
2528 };
2529
2530 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2531 abi_ulong target_addr)
2532 {
2533 int nsems;
2534 unsigned short *array;
2535 union semun semun;
2536 struct semid_ds semid_ds;
2537 int i, ret;
2538
2539 semun.buf = &semid_ds;
2540
2541 ret = semctl(semid, 0, IPC_STAT, semun);
2542 if (ret == -1)
2543 return get_errno(ret);
2544
2545 nsems = semid_ds.sem_nsems;
2546
2547 *host_array = malloc(nsems*sizeof(unsigned short));
2548 array = lock_user(VERIFY_READ, target_addr,
2549 nsems*sizeof(unsigned short), 1);
2550 if (!array)
2551 return -TARGET_EFAULT;
2552
2553 for(i=0; i<nsems; i++) {
2554 __get_user((*host_array)[i], &array[i]);
2555 }
2556 unlock_user(array, target_addr, 0);
2557
2558 return 0;
2559 }
2560
2561 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2562 unsigned short **host_array)
2563 {
2564 int nsems;
2565 unsigned short *array;
2566 union semun semun;
2567 struct semid_ds semid_ds;
2568 int i, ret;
2569
2570 semun.buf = &semid_ds;
2571
2572 ret = semctl(semid, 0, IPC_STAT, semun);
2573 if (ret == -1)
2574 return get_errno(ret);
2575
2576 nsems = semid_ds.sem_nsems;
2577
2578 array = lock_user(VERIFY_WRITE, target_addr,
2579 nsems*sizeof(unsigned short), 0);
2580 if (!array)
2581 return -TARGET_EFAULT;
2582
2583 for(i=0; i<nsems; i++) {
2584 __put_user((*host_array)[i], &array[i]);
2585 }
2586 free(*host_array);
2587 unlock_user(array, target_addr, 1);
2588
2589 return 0;
2590 }
2591
2592 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2593 union target_semun target_su)
2594 {
2595 union semun arg;
2596 struct semid_ds dsarg;
2597 unsigned short *array = NULL;
2598 struct seminfo seminfo;
2599 abi_long ret = -TARGET_EINVAL;
2600 abi_long err;
2601 cmd &= 0xff;
2602
2603 switch( cmd ) {
2604 case GETVAL:
2605 case SETVAL:
2606 arg.val = tswap32(target_su.val);
2607 ret = get_errno(semctl(semid, semnum, cmd, arg));
2608 target_su.val = tswap32(arg.val);
2609 break;
2610 case GETALL:
2611 case SETALL:
2612 err = target_to_host_semarray(semid, &array, target_su.array);
2613 if (err)
2614 return err;
2615 arg.array = array;
2616 ret = get_errno(semctl(semid, semnum, cmd, arg));
2617 err = host_to_target_semarray(semid, target_su.array, &array);
2618 if (err)
2619 return err;
2620 break;
2621 case IPC_STAT:
2622 case IPC_SET:
2623 case SEM_STAT:
2624 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2625 if (err)
2626 return err;
2627 arg.buf = &dsarg;
2628 ret = get_errno(semctl(semid, semnum, cmd, arg));
2629 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2630 if (err)
2631 return err;
2632 break;
2633 case IPC_INFO:
2634 case SEM_INFO:
2635 arg.__buf = &seminfo;
2636 ret = get_errno(semctl(semid, semnum, cmd, arg));
2637 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2638 if (err)
2639 return err;
2640 break;
2641 case IPC_RMID:
2642 case GETPID:
2643 case GETNCNT:
2644 case GETZCNT:
2645 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2646 break;
2647 }
2648
2649 return ret;
2650 }
2651
2652 struct target_sembuf {
2653 unsigned short sem_num;
2654 short sem_op;
2655 short sem_flg;
2656 };
2657
2658 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2659 abi_ulong target_addr,
2660 unsigned nsops)
2661 {
2662 struct target_sembuf *target_sembuf;
2663 int i;
2664
2665 target_sembuf = lock_user(VERIFY_READ, target_addr,
2666 nsops*sizeof(struct target_sembuf), 1);
2667 if (!target_sembuf)
2668 return -TARGET_EFAULT;
2669
2670 for(i=0; i<nsops; i++) {
2671 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2672 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2673 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2674 }
2675
2676 unlock_user(target_sembuf, target_addr, 0);
2677
2678 return 0;
2679 }
2680
2681 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2682 {
2683 struct sembuf sops[nsops];
2684
2685 if (target_to_host_sembuf(sops, ptr, nsops))
2686 return -TARGET_EFAULT;
2687
2688 return get_errno(semop(semid, sops, nsops));
2689 }
2690
2691 struct target_msqid_ds
2692 {
2693 struct target_ipc_perm msg_perm;
2694 abi_ulong msg_stime;
2695 #if TARGET_ABI_BITS == 32
2696 abi_ulong __unused1;
2697 #endif
2698 abi_ulong msg_rtime;
2699 #if TARGET_ABI_BITS == 32
2700 abi_ulong __unused2;
2701 #endif
2702 abi_ulong msg_ctime;
2703 #if TARGET_ABI_BITS == 32
2704 abi_ulong __unused3;
2705 #endif
2706 abi_ulong __msg_cbytes;
2707 abi_ulong msg_qnum;
2708 abi_ulong msg_qbytes;
2709 abi_ulong msg_lspid;
2710 abi_ulong msg_lrpid;
2711 abi_ulong __unused4;
2712 abi_ulong __unused5;
2713 };
2714
2715 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2716 abi_ulong target_addr)
2717 {
2718 struct target_msqid_ds *target_md;
2719
2720 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2721 return -TARGET_EFAULT;
2722 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2723 return -TARGET_EFAULT;
2724 host_md->msg_stime = tswapal(target_md->msg_stime);
2725 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2726 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2727 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2728 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2729 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2730 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2731 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
2732 unlock_user_struct(target_md, target_addr, 0);
2733 return 0;
2734 }
2735
2736 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2737 struct msqid_ds *host_md)
2738 {
2739 struct target_msqid_ds *target_md;
2740
2741 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2742 return -TARGET_EFAULT;
2743 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2744 return -TARGET_EFAULT;
2745 target_md->msg_stime = tswapal(host_md->msg_stime);
2746 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2747 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2748 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2749 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2750 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2751 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2752 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
2753 unlock_user_struct(target_md, target_addr, 1);
2754 return 0;
2755 }
2756
2757 struct target_msginfo {
2758 int msgpool;
2759 int msgmap;
2760 int msgmax;
2761 int msgmnb;
2762 int msgmni;
2763 int msgssz;
2764 int msgtql;
2765 unsigned short int msgseg;
2766 };
2767
2768 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2769 struct msginfo *host_msginfo)
2770 {
2771 struct target_msginfo *target_msginfo;
2772 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2773 return -TARGET_EFAULT;
2774 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2775 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2776 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2777 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2778 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2779 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2780 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2781 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2782 unlock_user_struct(target_msginfo, target_addr, 1);
2783 return 0;
2784 }
2785
2786 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
2787 {
2788 struct msqid_ds dsarg;
2789 struct msginfo msginfo;
2790 abi_long ret = -TARGET_EINVAL;
2791
2792 cmd &= 0xff;
2793
2794 switch (cmd) {
2795 case IPC_STAT:
2796 case IPC_SET:
2797 case MSG_STAT:
2798 if (target_to_host_msqid_ds(&dsarg,ptr))
2799 return -TARGET_EFAULT;
2800 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2801 if (host_to_target_msqid_ds(ptr,&dsarg))
2802 return -TARGET_EFAULT;
2803 break;
2804 case IPC_RMID:
2805 ret = get_errno(msgctl(msgid, cmd, NULL));
2806 break;
2807 case IPC_INFO:
2808 case MSG_INFO:
2809 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2810 if (host_to_target_msginfo(ptr, &msginfo))
2811 return -TARGET_EFAULT;
2812 break;
2813 }
2814
2815 return ret;
2816 }
2817
2818 struct target_msgbuf {
2819 abi_long mtype;
2820 char mtext[1];
2821 };
2822
2823 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2824 unsigned int msgsz, int msgflg)
2825 {
2826 struct target_msgbuf *target_mb;
2827 struct msgbuf *host_mb;
2828 abi_long ret = 0;
2829
2830 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2831 return -TARGET_EFAULT;
2832 host_mb = malloc(msgsz+sizeof(long));
2833 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
2834 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
2835 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2836 free(host_mb);
2837 unlock_user_struct(target_mb, msgp, 0);
2838
2839 return ret;
2840 }
2841
2842 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2843 unsigned int msgsz, abi_long msgtyp,
2844 int msgflg)
2845 {
2846 struct target_msgbuf *target_mb;
2847 char *target_mtext;
2848 struct msgbuf *host_mb;
2849 abi_long ret = 0;
2850
2851 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2852 return -TARGET_EFAULT;
2853
2854 host_mb = g_malloc(msgsz+sizeof(long));
2855 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
2856
2857 if (ret > 0) {
2858 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2859 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2860 if (!target_mtext) {
2861 ret = -TARGET_EFAULT;
2862 goto end;
2863 }
2864 memcpy(target_mb->mtext, host_mb->mtext, ret);
2865 unlock_user(target_mtext, target_mtext_addr, ret);
2866 }
2867
2868 target_mb->mtype = tswapal(host_mb->mtype);
2869
2870 end:
2871 if (target_mb)
2872 unlock_user_struct(target_mb, msgp, 1);
2873 g_free(host_mb);
2874 return ret;
2875 }
2876
2877 struct target_shmid_ds
2878 {
2879 struct target_ipc_perm shm_perm;
2880 abi_ulong shm_segsz;
2881 abi_ulong shm_atime;
2882 #if TARGET_ABI_BITS == 32
2883 abi_ulong __unused1;
2884 #endif
2885 abi_ulong shm_dtime;
2886 #if TARGET_ABI_BITS == 32
2887 abi_ulong __unused2;
2888 #endif
2889 abi_ulong shm_ctime;
2890 #if TARGET_ABI_BITS == 32
2891 abi_ulong __unused3;
2892 #endif
2893 int shm_cpid;
2894 int shm_lpid;
2895 abi_ulong shm_nattch;
2896 unsigned long int __unused4;
2897 unsigned long int __unused5;
2898 };
2899
2900 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2901 abi_ulong target_addr)
2902 {
2903 struct target_shmid_ds *target_sd;
2904
2905 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2906 return -TARGET_EFAULT;
2907 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2908 return -TARGET_EFAULT;
2909 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2910 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2911 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2912 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2913 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2914 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2915 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2916 unlock_user_struct(target_sd, target_addr, 0);
2917 return 0;
2918 }
2919
2920 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2921 struct shmid_ds *host_sd)
2922 {
2923 struct target_shmid_ds *target_sd;
2924
2925 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2926 return -TARGET_EFAULT;
2927 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2928 return -TARGET_EFAULT;
2929 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2930 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2931 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2932 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2933 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2934 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2935 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2936 unlock_user_struct(target_sd, target_addr, 1);
2937 return 0;
2938 }
2939
2940 struct target_shminfo {
2941 abi_ulong shmmax;
2942 abi_ulong shmmin;
2943 abi_ulong shmmni;
2944 abi_ulong shmseg;
2945 abi_ulong shmall;
2946 };
2947
2948 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2949 struct shminfo *host_shminfo)
2950 {
2951 struct target_shminfo *target_shminfo;
2952 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2953 return -TARGET_EFAULT;
2954 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2955 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2956 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2957 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2958 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2959 unlock_user_struct(target_shminfo, target_addr, 1);
2960 return 0;
2961 }
2962
2963 struct target_shm_info {
2964 int used_ids;
2965 abi_ulong shm_tot;
2966 abi_ulong shm_rss;
2967 abi_ulong shm_swp;
2968 abi_ulong swap_attempts;
2969 abi_ulong swap_successes;
2970 };
2971
2972 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2973 struct shm_info *host_shm_info)
2974 {
2975 struct target_shm_info *target_shm_info;
2976 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2977 return -TARGET_EFAULT;
2978 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2979 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2980 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2981 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2982 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2983 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2984 unlock_user_struct(target_shm_info, target_addr, 1);
2985 return 0;
2986 }
2987
2988 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2989 {
2990 struct shmid_ds dsarg;
2991 struct shminfo shminfo;
2992 struct shm_info shm_info;
2993 abi_long ret = -TARGET_EINVAL;
2994
2995 cmd &= 0xff;
2996
2997 switch(cmd) {
2998 case IPC_STAT:
2999 case IPC_SET:
3000 case SHM_STAT:
3001 if (target_to_host_shmid_ds(&dsarg, buf))
3002 return -TARGET_EFAULT;
3003 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3004 if (host_to_target_shmid_ds(buf, &dsarg))
3005 return -TARGET_EFAULT;
3006 break;
3007 case IPC_INFO:
3008 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3009 if (host_to_target_shminfo(buf, &shminfo))
3010 return -TARGET_EFAULT;
3011 break;
3012 case SHM_INFO:
3013 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3014 if (host_to_target_shm_info(buf, &shm_info))
3015 return -TARGET_EFAULT;
3016 break;
3017 case IPC_RMID:
3018 case SHM_LOCK:
3019 case SHM_UNLOCK:
3020 ret = get_errno(shmctl(shmid, cmd, NULL));
3021 break;
3022 }
3023
3024 return ret;
3025 }
3026
3027 static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3028 {
3029 abi_long raddr;
3030 void *host_raddr;
3031 struct shmid_ds shm_info;
3032 int i,ret;
3033
3034 /* find out the length of the shared memory segment */
3035 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3036 if (is_error(ret)) {
3037 /* can't get length, bail out */
3038 return ret;
3039 }
3040
3041 mmap_lock();
3042
3043 if (shmaddr)
3044 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3045 else {
3046 abi_ulong mmap_start;
3047
3048 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3049
3050 if (mmap_start == -1) {
3051 errno = ENOMEM;
3052 host_raddr = (void *)-1;
3053 } else
3054 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3055 }
3056
3057 if (host_raddr == (void *)-1) {
3058 mmap_unlock();
3059 return get_errno((long)host_raddr);
3060 }
3061 raddr=h2g((unsigned long)host_raddr);
3062
3063 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3064 PAGE_VALID | PAGE_READ |
3065 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3066
3067 for (i = 0; i < N_SHM_REGIONS; i++) {
3068 if (shm_regions[i].start == 0) {
3069 shm_regions[i].start = raddr;
3070 shm_regions[i].size = shm_info.shm_segsz;
3071 break;
3072 }
3073 }
3074
3075 mmap_unlock();
3076 return raddr;
3077
3078 }
3079
3080 static inline abi_long do_shmdt(abi_ulong shmaddr)
3081 {
3082 int i;
3083
3084 for (i = 0; i < N_SHM_REGIONS; ++i) {
3085 if (shm_regions[i].start == shmaddr) {
3086 shm_regions[i].start = 0;
3087 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
3088 break;
3089 }
3090 }
3091
3092 return get_errno(shmdt(g2h(shmaddr)));
3093 }
3094
3095 #ifdef TARGET_NR_ipc
3096 /* ??? This only works with linear mappings. */
3097 /* do_ipc() must return target values and target errnos. */
3098 static abi_long do_ipc(unsigned int call, int first,
3099 int second, int third,
3100 abi_long ptr, abi_long fifth)
3101 {
3102 int version;
3103 abi_long ret = 0;
3104
3105 version = call >> 16;
3106 call &= 0xffff;
3107
3108 switch (call) {
3109 case IPCOP_semop:
3110 ret = do_semop(first, ptr, second);
3111 break;
3112
3113 case IPCOP_semget:
3114 ret = get_errno(semget(first, second, third));
3115 break;
3116
3117 case IPCOP_semctl:
3118 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
3119 break;
3120
3121 case IPCOP_msgget:
3122 ret = get_errno(msgget(first, second));
3123 break;
3124
3125 case IPCOP_msgsnd:
3126 ret = do_msgsnd(first, ptr, second, third);
3127 break;
3128
3129 case IPCOP_msgctl:
3130 ret = do_msgctl(first, second, ptr);
3131 break;
3132
3133 case IPCOP_msgrcv:
3134 switch (version) {
3135 case 0:
3136 {
3137 struct target_ipc_kludge {
3138 abi_long msgp;
3139 abi_long msgtyp;
3140 } *tmp;
3141
3142 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3143 ret = -TARGET_EFAULT;
3144 break;
3145 }
3146
3147 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
3148
3149 unlock_user_struct(tmp, ptr, 0);
3150 break;
3151 }
3152 default:
3153 ret = do_msgrcv(first, ptr, second, fifth, third);
3154 }
3155 break;
3156
3157 case IPCOP_shmat:
3158 switch (version) {
3159 default:
3160 {
3161 abi_ulong raddr;
3162 raddr = do_shmat(first, ptr, second);
3163 if (is_error(raddr))
3164 return get_errno(raddr);
3165 if (put_user_ual(raddr, third))
3166 return -TARGET_EFAULT;
3167 break;
3168 }
3169 case 1:
3170 ret = -TARGET_EINVAL;
3171 break;
3172 }
3173 break;
3174 case IPCOP_shmdt:
3175 ret = do_shmdt(ptr);
3176 break;
3177
3178 case IPCOP_shmget:
3179 /* IPC_* flag values are the same on all linux platforms */
3180 ret = get_errno(shmget(first, second, third));
3181 break;
3182
3183 /* IPC_* and SHM_* command values are the same on all linux platforms */
3184 case IPCOP_shmctl:
3185 ret = do_shmctl(first, second, third);
3186 break;
3187 default:
3188 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
3189 ret = -TARGET_ENOSYS;
3190 break;
3191 }
3192 return ret;
3193 }
3194 #endif
3195
3196 /* kernel structure types definitions */
3197
3198 #define STRUCT(name, ...) STRUCT_ ## name,
3199 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
3200 enum {
3201 #include "syscall_types.h"
3202 };
3203 #undef STRUCT
3204 #undef STRUCT_SPECIAL
3205
3206 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
3207 #define STRUCT_SPECIAL(name)
3208 #include "syscall_types.h"
3209 #undef STRUCT
3210 #undef STRUCT_SPECIAL
3211
3212 typedef struct IOCTLEntry IOCTLEntry;
3213
3214 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3215 int fd, abi_long cmd, abi_long arg);
3216
3217 struct IOCTLEntry {
3218 unsigned int target_cmd;
3219 unsigned int host_cmd;
3220 const char *name;
3221 int access;
3222 do_ioctl_fn *do_ioctl;
3223 const argtype arg_type[5];
3224 };
3225
3226 #define IOC_R 0x0001
3227 #define IOC_W 0x0002
3228 #define IOC_RW (IOC_R | IOC_W)
3229
3230 #define MAX_STRUCT_SIZE 4096
3231
3232 #ifdef CONFIG_FIEMAP
3233 /* So fiemap access checks don't overflow on 32 bit systems.
3234 * This is very slightly smaller than the limit imposed by
3235 * the underlying kernel.
3236 */
3237 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3238 / sizeof(struct fiemap_extent))
3239
3240 static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3241 int fd, abi_long cmd, abi_long arg)
3242 {
3243 /* The parameter for this ioctl is a struct fiemap followed
3244 * by an array of struct fiemap_extent whose size is set
3245 * in fiemap->fm_extent_count. The array is filled in by the
3246 * ioctl.
3247 */
3248 int target_size_in, target_size_out;
3249 struct fiemap *fm;
3250 const argtype *arg_type = ie->arg_type;
3251 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3252 void *argptr, *p;
3253 abi_long ret;
3254 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3255 uint32_t outbufsz;
3256 int free_fm = 0;
3257
3258 assert(arg_type[0] == TYPE_PTR);
3259 assert(ie->access == IOC_RW);
3260 arg_type++;
3261 target_size_in = thunk_type_size(arg_type, 0);
3262 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3263 if (!argptr) {
3264 return -TARGET_EFAULT;
3265 }
3266 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3267 unlock_user(argptr, arg, 0);
3268 fm = (struct fiemap *)buf_temp;
3269 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3270 return -TARGET_EINVAL;
3271 }
3272
3273 outbufsz = sizeof (*fm) +
3274 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3275
3276 if (outbufsz > MAX_STRUCT_SIZE) {
3277 /* We can't fit all the extents into the fixed size buffer.
3278 * Allocate one that is large enough and use it instead.
3279 */
3280 fm = malloc(outbufsz);
3281 if (!fm) {
3282 return -TARGET_ENOMEM;
3283 }
3284 memcpy(fm, buf_temp, sizeof(struct fiemap));
3285 free_fm = 1;
3286 }
3287 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3288 if (!is_error(ret)) {
3289 target_size_out = target_size_in;
3290 /* An extent_count of 0 means we were only counting the extents
3291 * so there are no structs to copy
3292 */
3293 if (fm->fm_extent_count != 0) {
3294 target_size_out += fm->fm_mapped_extents * extent_size;
3295 }
3296 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3297 if (!argptr) {
3298 ret = -TARGET_EFAULT;
3299 } else {
3300 /* Convert the struct fiemap */
3301 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3302 if (fm->fm_extent_count != 0) {
3303 p = argptr + target_size_in;
3304 /* ...and then all the struct fiemap_extents */
3305 for (i = 0; i < fm->fm_mapped_extents; i++) {
3306 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3307 THUNK_TARGET);
3308 p += extent_size;
3309 }
3310 }
3311 unlock_user(argptr, arg, target_size_out);
3312 }
3313 }
3314 if (free_fm) {
3315 free(fm);
3316 }
3317 return ret;
3318 }
3319 #endif
3320
3321 static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3322 int fd, abi_long cmd, abi_long arg)
3323 {
3324 const argtype *arg_type = ie->arg_type;
3325 int target_size;
3326 void *argptr;
3327 int ret;
3328 struct ifconf *host_ifconf;
3329 uint32_t outbufsz;
3330 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3331 int target_ifreq_size;
3332 int nb_ifreq;
3333 int free_buf = 0;
3334 int i;
3335 int target_ifc_len;
3336 abi_long target_ifc_buf;
3337 int host_ifc_len;
3338 char *host_ifc_buf;
3339
3340 assert(arg_type[0] == TYPE_PTR);
3341 assert(ie->access == IOC_RW);
3342
3343 arg_type++;
3344 target_size = thunk_type_size(arg_type, 0);
3345
3346 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3347 if (!argptr)
3348 return -TARGET_EFAULT;
3349 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3350 unlock_user(argptr, arg, 0);
3351
3352 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3353 target_ifc_len = host_ifconf->ifc_len;
3354 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3355
3356 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3357 nb_ifreq = target_ifc_len / target_ifreq_size;
3358 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3359
3360 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3361 if (outbufsz > MAX_STRUCT_SIZE) {
3362 /* We can't fit all the extents into the fixed size buffer.
3363 * Allocate one that is large enough and use it instead.
3364 */
3365 host_ifconf = malloc(outbufsz);
3366 if (!host_ifconf) {
3367 return -TARGET_ENOMEM;
3368 }
3369 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3370 free_buf = 1;
3371 }
3372 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3373
3374 host_ifconf->ifc_len = host_ifc_len;
3375 host_ifconf->ifc_buf = host_ifc_buf;
3376
3377 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3378 if (!is_error(ret)) {
3379 /* convert host ifc_len to target ifc_len */
3380
3381 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3382 target_ifc_len = nb_ifreq * target_ifreq_size;
3383 host_ifconf->ifc_len = target_ifc_len;
3384
3385 /* restore target ifc_buf */
3386
3387 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3388
3389 /* copy struct ifconf to target user */
3390
3391 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3392 if (!argptr)
3393 return -TARGET_EFAULT;
3394 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3395 unlock_user(argptr, arg, target_size);
3396
3397 /* copy ifreq[] to target user */
3398
3399 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3400 for (i = 0; i < nb_ifreq ; i++) {
3401 thunk_convert(argptr + i * target_ifreq_size,
3402 host_ifc_buf + i * sizeof(struct ifreq),
3403 ifreq_arg_type, THUNK_TARGET);
3404 }
3405 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3406 }
3407
3408 if (free_buf) {
3409 free(host_ifconf);
3410 }
3411
3412 return ret;
3413 }
3414
3415 static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3416 abi_long cmd, abi_long arg)
3417 {
3418 void *argptr;
3419 struct dm_ioctl *host_dm;
3420 abi_long guest_data;
3421 uint32_t guest_data_size;
3422 int target_size;
3423 const argtype *arg_type = ie->arg_type;
3424 abi_long ret;
3425 void *big_buf = NULL;
3426 char *host_data;
3427
3428 arg_type++;
3429 target_size = thunk_type_size(arg_type, 0);
3430 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3431 if (!argptr) {
3432 ret = -TARGET_EFAULT;
3433 goto out;
3434 }
3435 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3436 unlock_user(argptr, arg, 0);
3437
3438 /* buf_temp is too small, so fetch things into a bigger buffer */
3439 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3440 memcpy(big_buf, buf_temp, target_size);
3441 buf_temp = big_buf;
3442 host_dm = big_buf;
3443
3444 guest_data = arg + host_dm->data_start;
3445 if ((guest_data - arg) < 0) {
3446 ret = -EINVAL;
3447 goto out;
3448 }
3449 guest_data_size = host_dm->data_size - host_dm->data_start;
3450 host_data = (char*)host_dm + host_dm->data_start;
3451
3452 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3453 switch (ie->host_cmd) {
3454 case DM_REMOVE_ALL:
3455 case DM_LIST_DEVICES:
3456 case DM_DEV_CREATE:
3457 case DM_DEV_REMOVE:
3458 case DM_DEV_SUSPEND:
3459 case DM_DEV_STATUS:
3460 case DM_DEV_WAIT:
3461 case DM_TABLE_STATUS:
3462 case DM_TABLE_CLEAR:
3463 case DM_TABLE_DEPS:
3464 case DM_LIST_VERSIONS:
3465 /* no input data */
3466 break;
3467 case DM_DEV_RENAME:
3468 case DM_DEV_SET_GEOMETRY:
3469 /* data contains only strings */
3470 memcpy(host_data, argptr, guest_data_size);
3471 break;
3472 case DM_TARGET_MSG:
3473 memcpy(host_data, argptr, guest_data_size);
3474 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3475 break;
3476 case DM_TABLE_LOAD:
3477 {
3478 void *gspec = argptr;
3479 void *cur_data = host_data;
3480 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3481 int spec_size = thunk_type_size(arg_type, 0);
3482 int i;
3483
3484 for (i = 0; i < host_dm->target_count; i++) {
3485 struct dm_target_spec *spec = cur_data;
3486 uint32_t next;
3487 int slen;
3488
3489 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3490 slen = strlen((char*)gspec + spec_size) + 1;
3491 next = spec->next;
3492 spec->next = sizeof(*spec) + slen;
3493 strcpy((char*)&spec[1], gspec + spec_size);
3494 gspec += next;
3495 cur_data += spec->next;
3496 }
3497 break;
3498 }
3499 default:
3500 ret = -TARGET_EINVAL;
3501 goto out;
3502 }
3503 unlock_user(argptr, guest_data, 0);
3504
3505 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3506 if (!is_error(ret)) {
3507 guest_data = arg + host_dm->data_start;
3508 guest_data_size = host_dm->data_size - host_dm->data_start;
3509 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3510 switch (ie->host_cmd) {
3511 case DM_REMOVE_ALL:
3512 case DM_DEV_CREATE:
3513 case DM_DEV_REMOVE:
3514 case DM_DEV_RENAME:
3515 case DM_DEV_SUSPEND:
3516 case DM_DEV_STATUS:
3517 case DM_TABLE_LOAD:
3518 case DM_TABLE_CLEAR:
3519 case DM_TARGET_MSG:
3520 case DM_DEV_SET_GEOMETRY:
3521 /* no return data */
3522 break;
3523 case DM_LIST_DEVICES:
3524 {
3525 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3526 uint32_t remaining_data = guest_data_size;
3527 void *cur_data = argptr;
3528 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3529 int nl_size = 12; /* can't use thunk_size due to alignment */
3530
3531 while (1) {
3532 uint32_t next = nl->next;
3533 if (next) {
3534 nl->next = nl_size + (strlen(nl->name) + 1);
3535 }
3536 if (remaining_data < nl->next) {
3537 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3538 break;
3539 }
3540 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3541 strcpy(cur_data + nl_size, nl->name);
3542 cur_data += nl->next;
3543 remaining_data -= nl->next;
3544 if (!next) {
3545 break;
3546 }
3547 nl = (void*)nl + next;
3548 }
3549 break;
3550 }
3551 case DM_DEV_WAIT:
3552 case DM_TABLE_STATUS:
3553 {
3554 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3555 void *cur_data = argptr;
3556 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3557 int spec_size = thunk_type_size(arg_type, 0);
3558 int i;
3559
3560 for (i = 0; i < host_dm->target_count; i++) {
3561 uint32_t next = spec->next;
3562 int slen = strlen((char*)&spec[1]) + 1;
3563 spec->next = (cur_data - argptr) + spec_size + slen;
3564 if (guest_data_size < spec->next) {
3565 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3566 break;
3567 }
3568 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3569 strcpy(cur_data + spec_size, (char*)&spec[1]);
3570 cur_data = argptr + spec->next;
3571 spec = (void*)host_dm + host_dm->data_start + next;
3572 }
3573 break;
3574 }
3575 case DM_TABLE_DEPS:
3576 {
3577 void *hdata = (void*)host_dm + host_dm->data_start;
3578 int count = *(uint32_t*)hdata;
3579 uint64_t *hdev = hdata + 8;
3580 uint64_t *gdev = argptr + 8;
3581 int i;
3582
3583 *(uint32_t*)argptr = tswap32(count);
3584 for (i = 0; i < count; i++) {
3585 *gdev = tswap64(*hdev);
3586 gdev++;
3587 hdev++;
3588 }
3589 break;
3590 }
3591 case DM_LIST_VERSIONS:
3592 {
3593 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3594 uint32_t remaining_data = guest_data_size;
3595 void *cur_data = argptr;
3596 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3597 int vers_size = thunk_type_size(arg_type, 0);
3598
3599 while (1) {
3600 uint32_t next = vers->next;
3601 if (next) {
3602 vers->next = vers_size + (strlen(vers->name) + 1);
3603 }
3604 if (remaining_data < vers->next) {
3605 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3606 break;
3607 }
3608 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3609 strcpy(cur_data + vers_size, vers->name);
3610 cur_data += vers->next;
3611 remaining_data -= vers->next;
3612 if (!next) {
3613 break;
3614 }
3615 vers = (void*)vers + next;
3616 }
3617 break;
3618 }
3619 default:
3620 ret = -TARGET_EINVAL;
3621 goto out;
3622 }
3623 unlock_user(argptr, guest_data, guest_data_size);
3624
3625 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3626 if (!argptr) {
3627 ret = -TARGET_EFAULT;
3628 goto out;
3629 }
3630 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3631 unlock_user(argptr, arg, target_size);
3632 }
3633 out:
3634 g_free(big_buf);
3635 return ret;
3636 }
3637
3638 static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
3639 int fd, abi_long cmd, abi_long arg)
3640 {
3641 const argtype *arg_type = ie->arg_type;
3642 const StructEntry *se;
3643 const argtype *field_types;
3644 const int *dst_offsets, *src_offsets;
3645 int target_size;
3646 void *argptr;
3647 abi_ulong *target_rt_dev_ptr;
3648 unsigned long *host_rt_dev_ptr;
3649 abi_long ret;
3650 int i;
3651
3652 assert(ie->access == IOC_W);
3653 assert(*arg_type == TYPE_PTR);
3654 arg_type++;
3655 assert(*arg_type == TYPE_STRUCT);
3656 target_size = thunk_type_size(arg_type, 0);
3657 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3658 if (!argptr) {
3659 return -TARGET_EFAULT;
3660 }
3661 arg_type++;
3662 assert(*arg_type == (int)STRUCT_rtentry);
3663 se = struct_entries + *arg_type++;
3664 assert(se->convert[0] == NULL);
3665 /* convert struct here to be able to catch rt_dev string */
3666 field_types = se->field_types;
3667 dst_offsets = se->field_offsets[THUNK_HOST];
3668 src_offsets = se->field_offsets[THUNK_TARGET];
3669 for (i = 0; i < se->nb_fields; i++) {
3670 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
3671 assert(*field_types == TYPE_PTRVOID);
3672 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
3673 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
3674 if (*target_rt_dev_ptr != 0) {
3675 *host_rt_dev_ptr = (unsigned long)lock_user_string(
3676 tswapal(*target_rt_dev_ptr));
3677 if (!*host_rt_dev_ptr) {
3678 unlock_user(argptr, arg, 0);
3679 return -TARGET_EFAULT;
3680 }
3681 } else {
3682 *host_rt_dev_ptr = 0;
3683 }
3684 field_types++;
3685 continue;
3686 }
3687 field_types = thunk_convert(buf_temp + dst_offsets[i],
3688 argptr + src_offsets[i],
3689 field_types, THUNK_HOST);
3690 }
3691 unlock_user(argptr, arg, 0);
3692
3693 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3694 if (*host_rt_dev_ptr != 0) {
3695 unlock_user((void *)*host_rt_dev_ptr,
3696 *target_rt_dev_ptr, 0);
3697 }
3698 return ret;
3699 }
3700
3701 static IOCTLEntry ioctl_entries[] = {
3702 #define IOCTL(cmd, access, ...) \
3703 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3704 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3705 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
3706 #include "ioctls.h"
3707 { 0, 0, },
3708 };
3709
3710 /* ??? Implement proper locking for ioctls. */
3711 /* do_ioctl() Must return target values and target errnos. */
3712 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
3713 {
3714 const IOCTLEntry *ie;
3715 const argtype *arg_type;
3716 abi_long ret;
3717 uint8_t buf_temp[MAX_STRUCT_SIZE];
3718 int target_size;
3719 void *argptr;
3720
3721 ie = ioctl_entries;
3722 for(;;) {
3723 if (ie->target_cmd == 0) {
3724 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
3725 return -TARGET_ENOSYS;
3726 }
3727 if (ie->target_cmd == cmd)
3728 break;
3729 ie++;
3730 }
3731 arg_type = ie->arg_type;
3732 #if defined(DEBUG)
3733 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
3734 #endif
3735 if (ie->do_ioctl) {
3736 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3737 }
3738
3739 switch(arg_type[0]) {
3740 case TYPE_NULL:
3741 /* no argument */
3742 ret = get_errno(ioctl(fd, ie->host_cmd));
3743 break;
3744 case TYPE_PTRVOID:
3745 case TYPE_INT:
3746 /* int argment */
3747 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3748 break;
3749 case TYPE_PTR:
3750 arg_type++;
3751 target_size = thunk_type_size(arg_type, 0);
3752 switch(ie->access) {
3753 case IOC_R:
3754 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3755 if (!is_error(ret)) {
3756 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3757 if (!argptr)
3758 return -TARGET_EFAULT;
3759 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3760 unlock_user(argptr, arg, target_size);
3761 }
3762 break;
3763 case IOC_W:
3764 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3765 if (!argptr)
3766 return -TARGET_EFAULT;
3767 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3768 unlock_user(argptr, arg, 0);
3769 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3770 break;
3771 default:
3772 case IOC_RW:
3773 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3774 if (!argptr)
3775 return -TARGET_EFAULT;
3776 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3777 unlock_user(argptr, arg, 0);
3778 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3779 if (!is_error(ret)) {
3780 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3781 if (!argptr)
3782 return -TARGET_EFAULT;
3783 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3784 unlock_user(argptr, arg, target_size);
3785 }
3786 break;
3787 }
3788 break;
3789 default:
3790 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3791 (long)cmd, arg_type[0]);
3792 ret = -TARGET_ENOSYS;
3793 break;
3794 }
3795 return ret;
3796 }
3797
3798 static const bitmask_transtbl iflag_tbl[] = {
3799 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3800 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3801 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3802 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3803 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3804 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3805 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3806 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3807 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3808 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3809 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3810 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3811 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3812 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3813 { 0, 0, 0, 0 }
3814 };
3815
3816 static const bitmask_transtbl oflag_tbl[] = {
3817 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3818 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3819 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3820 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3821 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3822 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3823 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3824 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3825 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3826 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3827 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3828 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3829 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3830 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3831 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3832 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3833 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3834 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3835 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3836 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3837 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3838 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3839 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3840 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3841 { 0, 0, 0, 0 }
3842 };
3843
3844 static const bitmask_transtbl cflag_tbl[] = {
3845 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3846 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3847 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3848 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3849 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3850 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3851 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3852 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3853 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3854 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3855 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3856 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3857 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3858 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3859 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3860 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3861 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3862 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3863 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3864 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3865 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3866 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3867 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3868 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3869 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3870 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3871 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3872 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3873 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3874 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3875 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3876 { 0, 0, 0, 0 }
3877 };
3878
3879 static const bitmask_transtbl lflag_tbl[] = {
3880 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3881 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3882 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3883 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3884 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3885 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3886 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3887 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3888 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3889 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3890 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3891 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3892 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3893 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3894 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3895 { 0, 0, 0, 0 }
3896 };
3897
3898 static void target_to_host_termios (void *dst, const void *src)
3899 {
3900 struct host_termios *host = dst;
3901 const struct target_termios *target = src;
3902
3903 host->c_iflag =
3904 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
3905 host->c_oflag =
3906 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
3907 host->c_cflag =
3908 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
3909 host->c_lflag =
3910 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3911 host->c_line = target->c_line;
3912
3913 memset(host->c_cc, 0, sizeof(host->c_cc));
3914 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3915 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3916 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
3917 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3918 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
3919 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3920 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
3921 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3922 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
3923 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3924 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3925 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3926 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3927 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3928 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3929 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
3930 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
3931 }
3932
3933 static void host_to_target_termios (void *dst, const void *src)
3934 {
3935 struct target_termios *target = dst;
3936 const struct host_termios *host = src;
3937
3938 target->c_iflag =
3939 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
3940 target->c_oflag =
3941 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
3942 target->c_cflag =
3943 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
3944 target->c_lflag =
3945 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3946 target->c_line = host->c_line;
3947
3948 memset(target->c_cc, 0, sizeof(target->c_cc));
3949 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3950 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3951 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3952 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3953 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3954 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3955 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3956 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3957 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3958 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3959 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3960 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3961 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3962 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3963 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3964 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3965 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3966 }
3967
3968 static const StructEntry struct_termios_def = {
3969 .convert = { host_to_target_termios, target_to_host_termios },
3970 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3971 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3972 };
3973
3974 static bitmask_transtbl mmap_flags_tbl[] = {
3975 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3976 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3977 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3978 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3979 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3980 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3981 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3982 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3983 { 0, 0, 0, 0 }
3984 };
3985
3986 #if defined(TARGET_I386)
3987
3988 /* NOTE: there is really one LDT for all the threads */
3989 static uint8_t *ldt_table;
3990
3991 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
3992 {
3993 int size;
3994 void *p;
3995
3996 if (!ldt_table)
3997 return 0;
3998 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3999 if (size > bytecount)
4000 size = bytecount;
4001 p = lock_user(VERIFY_WRITE, ptr, size, 0);
4002 if (!p)
4003 return -TARGET_EFAULT;
4004 /* ??? Should this by byteswapped? */
4005 memcpy(p, ldt_table, size);
4006 unlock_user(p, ptr, size);
4007 return size;
4008 }
4009
4010 /* XXX: add locking support */
4011 static abi_long write_ldt(CPUX86State *env,
4012 abi_ulong ptr, unsigned long bytecount, int oldmode)
4013 {
4014 struct target_modify_ldt_ldt_s ldt_info;
4015 struct target_modify_ldt_ldt_s *target_ldt_info;
4016 int seg_32bit, contents, read_exec_only, limit_in_pages;
4017 int seg_not_present, useable, lm;
4018 uint32_t *lp, entry_1, entry_2;
4019
4020 if (bytecount != sizeof(ldt_info))
4021 return -TARGET_EINVAL;
4022 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
4023 return -TARGET_EFAULT;
4024 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
4025 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
4026 ldt_info.limit = tswap32(target_ldt_info->limit);
4027 ldt_info.flags = tswap32(target_ldt_info->flags);
4028 unlock_user_struct(target_ldt_info, ptr, 0);
4029
4030 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
4031 return -TARGET_EINVAL;
4032 seg_32bit = ldt_info.flags & 1;
4033 contents = (ldt_info.flags >> 1) & 3;
4034 read_exec_only = (ldt_info.flags >> 3) & 1;
4035 limit_in_pages = (ldt_info.flags >> 4) & 1;
4036 seg_not_present = (ldt_info.flags >> 5) & 1;
4037 useable = (ldt_info.flags >> 6) & 1;
4038 #ifdef TARGET_ABI32
4039 lm = 0;
4040 #else
4041 lm = (ldt_info.flags >> 7) & 1;
4042 #endif
4043 if (contents == 3) {
4044 if (oldmode)
4045 return -TARGET_EINVAL;
4046 if (seg_not_present == 0)
4047 return -TARGET_EINVAL;
4048 }
4049 /* allocate the LDT */
4050 if (!ldt_table) {
4051 env->ldt.base = target_mmap(0,
4052 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
4053 PROT_READ|PROT_WRITE,
4054 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
4055 if (env->ldt.base == -1)
4056 return -TARGET_ENOMEM;
4057 memset(g2h(env->ldt.base), 0,
4058 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
4059 env->ldt.limit = 0xffff;
4060 ldt_table = g2h(env->ldt.base);
4061 }
4062
4063 /* NOTE: same code as Linux kernel */
4064 /* Allow LDTs to be cleared by the user. */
4065 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4066 if (oldmode ||
4067 (contents == 0 &&
4068 read_exec_only == 1 &&
4069 seg_32bit == 0 &&
4070 limit_in_pages == 0 &&
4071 seg_not_present == 1 &&
4072 useable == 0 )) {
4073 entry_1 = 0;
4074 entry_2 = 0;
4075 goto install;
4076 }
4077 }
4078
4079 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4080 (ldt_info.limit & 0x0ffff);
4081 entry_2 = (ldt_info.base_addr & 0xff000000) |
4082 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4083 (ldt_info.limit & 0xf0000) |
4084 ((read_exec_only ^ 1) << 9) |
4085 (contents << 10) |
4086 ((seg_not_present ^ 1) << 15) |
4087 (seg_32bit << 22) |
4088 (limit_in_pages << 23) |
4089 (lm << 21) |
4090 0x7000;
4091 if (!oldmode)
4092 entry_2 |= (useable << 20);
4093
4094 /* Install the new entry ... */
4095 install:
4096 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4097 lp[0] = tswap32(entry_1);
4098 lp[1] = tswap32(entry_2);
4099 return 0;
4100 }
4101
4102 /* specific and weird i386 syscalls */
4103 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4104 unsigned long bytecount)
4105 {
4106 abi_long ret;
4107
4108 switch (func) {
4109 case 0:
4110 ret = read_ldt(ptr, bytecount);
4111 break;
4112 case 1:
4113 ret = write_ldt(env, ptr, bytecount, 1);
4114 break;
4115 case 0x11:
4116 ret = write_ldt(env, ptr, bytecount, 0);
4117 break;
4118 default:
4119 ret = -TARGET_ENOSYS;
4120 break;
4121 }
4122 return ret;
4123 }
4124
4125 #if defined(TARGET_I386) && defined(TARGET_ABI32)
4126 abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
4127 {
4128 uint64_t *gdt_table = g2h(env->gdt.base);
4129 struct target_modify_ldt_ldt_s ldt_info;
4130 struct target_modify_ldt_ldt_s *target_ldt_info;
4131 int seg_32bit, contents, read_exec_only, limit_in_pages;
4132 int seg_not_present, useable, lm;
4133 uint32_t *lp, entry_1, entry_2;
4134 int i;
4135
4136 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4137 if (!target_ldt_info)
4138 return -TARGET_EFAULT;
4139 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
4140 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
4141 ldt_info.limit = tswap32(target_ldt_info->limit);
4142 ldt_info.flags = tswap32(target_ldt_info->flags);
4143 if (ldt_info.entry_number == -1) {
4144 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4145 if (gdt_table[i] == 0) {
4146 ldt_info.entry_number = i;
4147 target_ldt_info->entry_number = tswap32(i);
4148 break;
4149 }
4150 }
4151 }
4152 unlock_user_struct(target_ldt_info, ptr, 1);
4153
4154 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4155 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4156 return -TARGET_EINVAL;
4157 seg_32bit = ldt_info.flags & 1;
4158 contents = (ldt_info.flags >> 1) & 3;
4159 read_exec_only = (ldt_info.flags >> 3) & 1;
4160 limit_in_pages = (ldt_info.flags >> 4) & 1;
4161 seg_not_present = (ldt_info.flags >> 5) & 1;
4162 useable = (ldt_info.flags >> 6) & 1;
4163 #ifdef TARGET_ABI32
4164 lm = 0;
4165 #else
4166 lm = (ldt_info.flags >> 7) & 1;
4167 #endif
4168
4169 if (contents == 3) {
4170 if (seg_not_present == 0)
4171 return -TARGET_EINVAL;
4172 }
4173
4174 /* NOTE: same code as Linux kernel */
4175 /* Allow LDTs to be cleared by the user. */
4176 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4177 if ((contents == 0 &&
4178 read_exec_only == 1 &&
4179 seg_32bit == 0 &&
4180 limit_in_pages == 0 &&
4181 seg_not_present == 1 &&
4182 useable == 0 )) {
4183 entry_1 = 0;
4184 entry_2 = 0;
4185 goto install;
4186 }
4187 }
4188
4189 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4190 (ldt_info.limit & 0x0ffff);
4191 entry_2 = (ldt_info.base_addr & 0xff000000) |
4192 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4193 (ldt_info.limit & 0xf0000) |
4194 ((read_exec_only ^ 1) << 9) |
4195 (contents << 10) |
4196 ((seg_not_present ^ 1) << 15) |
4197 (seg_32bit << 22) |
4198 (limit_in_pages << 23) |
4199 (useable << 20) |
4200 (lm << 21) |
4201 0x7000;
4202
4203 /* Install the new entry ... */
4204 install:
4205 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4206 lp[0] = tswap32(entry_1);
4207 lp[1] = tswap32(entry_2);
4208 return 0;
4209 }
4210
4211 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
4212 {
4213 struct target_modify_ldt_ldt_s *target_ldt_info;
4214 uint64_t *gdt_table = g2h(env->gdt.base);
4215 uint32_t base_addr, limit, flags;
4216 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4217 int seg_not_present, useable, lm;
4218 uint32_t *lp, entry_1, entry_2;
4219
4220 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4221 if (!target_ldt_info)
4222 return -TARGET_EFAULT;
4223 idx = tswap32(target_ldt_info->entry_number);
4224 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4225 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4226 unlock_user_struct(target_ldt_info, ptr, 1);
4227 return -TARGET_EINVAL;
4228 }
4229 lp = (uint32_t *)(gdt_table + idx);
4230 entry_1 = tswap32(lp[0]);
4231 entry_2 = tswap32(lp[1]);
4232
4233 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4234 contents = (entry_2 >> 10) & 3;
4235 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4236 seg_32bit = (entry_2 >> 22) & 1;
4237 limit_in_pages = (entry_2 >> 23) & 1;
4238 useable = (entry_2 >> 20) & 1;
4239 #ifdef TARGET_ABI32
4240 lm = 0;
4241 #else
4242 lm = (entry_2 >> 21) & 1;
4243 #endif
4244 flags = (seg_32bit << 0) | (contents << 1) |
4245 (read_exec_only << 3) | (limit_in_pages << 4) |
4246 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4247 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4248 base_addr = (entry_1 >> 16) |
4249 (entry_2 & 0xff000000) |
4250 ((entry_2 & 0xff) << 16);
4251 target_ldt_info->base_addr = tswapal(base_addr);
4252 target_ldt_info->limit = tswap32(limit);
4253 target_ldt_info->flags = tswap32(flags);
4254 unlock_user_struct(target_ldt_info, ptr, 1);
4255 return 0;
4256 }
4257 #endif /* TARGET_I386 && TARGET_ABI32 */
4258
4259 #ifndef TARGET_ABI32
4260 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
4261 {
4262 abi_long ret = 0;
4263 abi_ulong val;
4264 int idx;
4265
4266 switch(code) {
4267 case TARGET_ARCH_SET_GS:
4268 case TARGET_ARCH_SET_FS:
4269 if (code == TARGET_ARCH_SET_GS)
4270 idx = R_GS;
4271 else
4272 idx = R_FS;
4273 cpu_x86_load_seg(env, idx, 0);
4274 env->segs[idx].base = addr;
4275 break;
4276 case TARGET_ARCH_GET_GS:
4277 case TARGET_ARCH_GET_FS:
4278 if (code == TARGET_ARCH_GET_GS)
4279 idx = R_GS;
4280 else
4281 idx = R_FS;
4282 val = env->segs[idx].base;
4283 if (put_user(val, addr, abi_ulong))
4284 ret = -TARGET_EFAULT;
4285 break;
4286 default:
4287 ret = -TARGET_EINVAL;
4288 break;
4289 }
4290 return ret;
4291 }
4292 #endif
4293
4294 #endif /* defined(TARGET_I386) */
4295
4296 #define NEW_STACK_SIZE 0x40000
4297
4298
4299 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4300 typedef struct {
4301 CPUArchState *env;
4302 pthread_mutex_t mutex;
4303 pthread_cond_t cond;
4304 pthread_t thread;
4305 uint32_t tid;
4306 abi_ulong child_tidptr;
4307 abi_ulong parent_tidptr;
4308 sigset_t sigmask;
4309 } new_thread_info;
4310
4311 static void *clone_func(void *arg)
4312 {
4313 new_thread_info *info = arg;
4314 CPUArchState *env;
4315 CPUState *cpu;
4316 TaskState *ts;
4317
4318 env = info->env;
4319 cpu = ENV_GET_CPU(env);
4320 thread_cpu = cpu;
4321 ts = (TaskState *)env->opaque;
4322 info->tid = gettid();
4323 cpu->host_tid = info->tid;
4324 task_settid(ts);
4325 if (info->child_tidptr)
4326 put_user_u32(info->tid, info->child_tidptr);
4327 if (info->parent_tidptr)
4328 put_user_u32(info->tid, info->parent_tidptr);
4329 /* Enable signals. */
4330 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4331 /* Signal to the parent that we're ready. */
4332 pthread_mutex_lock(&info->mutex);
4333 pthread_cond_broadcast(&info->cond);
4334 pthread_mutex_unlock(&info->mutex);
4335 /* Wait until the parent has finshed initializing the tls state. */
4336 pthread_mutex_lock(&clone_lock);
4337 pthread_mutex_unlock(&clone_lock);
4338 cpu_loop(env);
4339 /* never exits */
4340 return NULL;
4341 }
4342
4343 /* do_fork() Must return host values and target errnos (unlike most
4344 do_*() functions). */
4345 static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
4346 abi_ulong parent_tidptr, target_ulong newtls,
4347 abi_ulong child_tidptr)
4348 {
4349 int ret;
4350 TaskState *ts;
4351 CPUArchState *new_env;
4352 unsigned int nptl_flags;
4353 sigset_t sigmask;
4354
4355 /* Emulate vfork() with fork() */
4356 if (flags & CLONE_VFORK)
4357 flags &= ~(CLONE_VFORK | CLONE_VM);
4358
4359 if (flags & CLONE_VM) {
4360 TaskState *parent_ts = (TaskState *)env->opaque;
4361 new_thread_info info;
4362 pthread_attr_t attr;
4363
4364 ts = g_malloc0(sizeof(TaskState));
4365 init_task_state(ts);
4366 /* we create a new CPU instance. */
4367 new_env = cpu_copy(env);
4368 /* Init regs that differ from the parent. */
4369 cpu_clone_regs(new_env, newsp);
4370 new_env->opaque = ts;
4371 ts->bprm = parent_ts->bprm;
4372 ts->info = parent_ts->info;
4373 nptl_flags = flags;
4374 flags &= ~CLONE_NPTL_FLAGS2;
4375
4376 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4377 ts->child_tidptr = child_tidptr;
4378 }
4379
4380 if (nptl_flags & CLONE_SETTLS)
4381 cpu_set_tls (new_env, newtls);
4382
4383 /* Grab a mutex so that thread setup appears atomic. */
4384 pthread_mutex_lock(&clone_lock);
4385
4386 memset(&info, 0, sizeof(info));
4387 pthread_mutex_init(&info.mutex, NULL);
4388 pthread_mutex_lock(&info.mutex);
4389 pthread_cond_init(&info.cond, NULL);
4390 info.env = new_env;
4391 if (nptl_flags & CLONE_CHILD_SETTID)
4392 info.child_tidptr = child_tidptr;
4393 if (nptl_flags & CLONE_PARENT_SETTID)
4394 info.parent_tidptr = parent_tidptr;
4395
4396 ret = pthread_attr_init(&attr);
4397 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4398 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
4399 /* It is not safe to deliver signals until the child has finished
4400 initializing, so temporarily block all signals. */
4401 sigfillset(&sigmask);
4402 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4403
4404 ret = pthread_create(&info.thread, &attr, clone_func, &info);
4405 /* TODO: Free new CPU state if thread creation failed. */
4406
4407 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4408 pthread_attr_destroy(&attr);
4409 if (ret == 0) {
4410 /* Wait for the child to initialize. */
4411 pthread_cond_wait(&info.cond, &info.mutex);
4412 ret = info.tid;
4413 if (flags & CLONE_PARENT_SETTID)
4414 put_user_u32(ret, parent_tidptr);
4415 } else {
4416 ret = -1;
4417 }
4418 pthread_mutex_unlock(&info.mutex);
4419 pthread_cond_destroy(&info.cond);
4420 pthread_mutex_destroy(&info.mutex);
4421 pthread_mutex_unlock(&clone_lock);
4422 } else {
4423 /* if no CLONE_VM, we consider it is a fork */
4424 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
4425 return -EINVAL;
4426 fork_start();
4427 ret = fork();
4428 if (ret == 0) {
4429 /* Child Process. */
4430 cpu_clone_regs(env, newsp);
4431 fork_end(1);
4432 /* There is a race condition here. The parent process could
4433 theoretically read the TID in the child process before the child
4434 tid is set. This would require using either ptrace
4435 (not implemented) or having *_tidptr to point at a shared memory
4436 mapping. We can't repeat the spinlock hack used above because
4437 the child process gets its own copy of the lock. */
4438 if (flags & CLONE_CHILD_SETTID)
4439 put_user_u32(gettid(), child_tidptr);
4440 if (flags & CLONE_PARENT_SETTID)
4441 put_user_u32(gettid(), parent_tidptr);
4442 ts = (TaskState *)env->opaque;
4443 if (flags & CLONE_SETTLS)
4444 cpu_set_tls (env, newtls);
4445 if (flags & CLONE_CHILD_CLEARTID)
4446 ts->child_tidptr = child_tidptr;
4447 } else {
4448 fork_end(0);
4449 }
4450 }
4451 return ret;
4452 }
4453
4454 /* warning : doesn't handle linux specific flags... */
4455 static int target_to_host_fcntl_cmd(int cmd)
4456 {
4457 switch(cmd) {
4458 case TARGET_F_DUPFD:
4459 case TARGET_F_GETFD:
4460 case TARGET_F_SETFD:
4461 case TARGET_F_GETFL:
4462 case TARGET_F_SETFL:
4463 return cmd;
4464 case TARGET_F_GETLK:
4465 return F_GETLK;
4466 case TARGET_F_SETLK:
4467 return F_SETLK;
4468 case TARGET_F_SETLKW:
4469 return F_SETLKW;
4470 case TARGET_F_GETOWN:
4471 return F_GETOWN;
4472 case TARGET_F_SETOWN:
4473 return F_SETOWN;
4474 case TARGET_F_GETSIG:
4475 return F_GETSIG;
4476 case TARGET_F_SETSIG:
4477 return F_SETSIG;
4478 #if TARGET_ABI_BITS == 32
4479 case TARGET_F_GETLK64:
4480 return F_GETLK64;
4481 case TARGET_F_SETLK64:
4482 return F_SETLK64;
4483 case TARGET_F_SETLKW64:
4484 return F_SETLKW64;
4485 #endif
4486 case TARGET_F_SETLEASE:
4487 return F_SETLEASE;
4488 case TARGET_F_GETLEASE:
4489 return F_GETLEASE;
4490 #ifdef F_DUPFD_CLOEXEC
4491 case TARGET_F_DUPFD_CLOEXEC:
4492 return F_DUPFD_CLOEXEC;
4493 #endif
4494 case TARGET_F_NOTIFY:
4495 return F_NOTIFY;
4496 default:
4497 return -TARGET_EINVAL;
4498 }
4499 return -TARGET_EINVAL;
4500 }
4501
4502 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4503 static const bitmask_transtbl flock_tbl[] = {
4504 TRANSTBL_CONVERT(F_RDLCK),
4505 TRANSTBL_CONVERT(F_WRLCK),
4506 TRANSTBL_CONVERT(F_UNLCK),
4507 TRANSTBL_CONVERT(F_EXLCK),
4508 TRANSTBL_CONVERT(F_SHLCK),
4509 { 0, 0, 0, 0 }
4510 };
4511
4512 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
4513 {
4514 struct flock fl;
4515 struct target_flock *target_fl;
4516 struct flock64 fl64;
4517 struct target_flock64 *target_fl64;
4518 abi_long ret;
4519 int host_cmd = target_to_host_fcntl_cmd(cmd);
4520
4521 if (host_cmd == -TARGET_EINVAL)
4522 return host_cmd;
4523
4524 switch(cmd) {
4525 case TARGET_F_GETLK:
4526 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4527 return -TARGET_EFAULT;
4528 fl.l_type =
4529 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
4530 fl.l_whence = tswap16(target_fl->l_whence);
4531 fl.l_start = tswapal(target_fl->l_start);
4532 fl.l_len = tswapal(target_fl->l_len);
4533 fl.l_pid = tswap32(target_fl->l_pid);
4534 unlock_user_struct(target_fl, arg, 0);
4535 ret = get_errno(fcntl(fd, host_cmd, &fl));
4536 if (ret == 0) {
4537 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4538 return -TARGET_EFAULT;
4539 target_fl->l_type =
4540 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
4541 target_fl->l_whence = tswap16(fl.l_whence);
4542 target_fl->l_start = tswapal(fl.l_start);
4543 target_fl->l_len = tswapal(fl.l_len);
4544 target_fl->l_pid = tswap32(fl.l_pid);
4545 unlock_user_struct(target_fl, arg, 1);
4546 }
4547 break;
4548
4549 case TARGET_F_SETLK:
4550 case TARGET_F_SETLKW:
4551 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4552 return -TARGET_EFAULT;
4553 fl.l_type =
4554 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
4555 fl.l_whence = tswap16(target_fl->l_whence);
4556 fl.l_start = tswapal(target_fl->l_start);
4557 fl.l_len = tswapal(target_fl->l_len);
4558 fl.l_pid = tswap32(target_fl->l_pid);
4559 unlock_user_struct(target_fl, arg, 0);
4560 ret = get_errno(fcntl(fd, host_cmd, &fl));
4561 break;
4562
4563 case TARGET_F_GETLK64:
4564 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4565 return -TARGET_EFAULT;
4566 fl64.l_type =
4567 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
4568 fl64.l_whence = tswap16(target_fl64->l_whence);
4569 fl64.l_start = tswap64(target_fl64->l_start);
4570 fl64.l_len = tswap64(target_fl64->l_len);
4571 fl64.l_pid = tswap32(target_fl64->l_pid);
4572 unlock_user_struct(target_fl64, arg, 0);
4573 ret = get_errno(fcntl(fd, host_cmd, &fl64));
4574 if (ret == 0) {
4575 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4576 return -TARGET_EFAULT;
4577 target_fl64->l_type =
4578 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
4579 target_fl64->l_whence = tswap16(fl64.l_whence);
4580 target_fl64->l_start = tswap64(fl64.l_start);
4581 target_fl64->l_len = tswap64(fl64.l_len);
4582 target_fl64->l_pid = tswap32(fl64.l_pid);
4583 unlock_user_struct(target_fl64, arg, 1);
4584 }
4585 break;
4586 case TARGET_F_SETLK64:
4587 case TARGET_F_SETLKW64:
4588 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4589 return -TARGET_EFAULT;
4590 fl64.l_type =
4591 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
4592 fl64.l_whence = tswap16(target_fl64->l_whence);
4593 fl64.l_start = tswap64(target_fl64->l_start);
4594 fl64.l_len = tswap64(target_fl64->l_len);
4595 fl64.l_pid = tswap32(target_fl64->l_pid);
4596 unlock_user_struct(target_fl64, arg, 0);
4597 ret = get_errno(fcntl(fd, host_cmd, &fl64));
4598 break;
4599
4600 case TARGET_F_GETFL:
4601 ret = get_errno(fcntl(fd, host_cmd, arg));
4602 if (ret >= 0) {
4603 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4604 }
4605 break;
4606
4607 case TARGET_F_SETFL:
4608 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4609 break;
4610
4611 case TARGET_F_SETOWN:
4612 case TARGET_F_GETOWN:
4613 case TARGET_F_SETSIG:
4614 case TARGET_F_GETSIG:
4615 case TARGET_F_SETLEASE:
4616 case TARGET_F_GETLEASE:
4617 ret = get_errno(fcntl(fd, host_cmd, arg));
4618 break;
4619
4620 default:
4621 ret = get_errno(fcntl(fd, cmd, arg));
4622 break;
4623 }
4624 return ret;
4625 }
4626
4627 #ifdef USE_UID16
4628
4629 static inline int high2lowuid(int uid)
4630 {
4631 if (uid > 65535)
4632 return 65534;
4633 else
4634 return uid;
4635 }
4636
4637 static inline int high2lowgid(int gid)
4638 {
4639 if (gid > 65535)
4640 return 65534;
4641 else
4642 return gid;
4643 }
4644
4645 static inline int low2highuid(int uid)
4646 {
4647 if ((int16_t)uid == -1)
4648 return -1;
4649 else
4650 return uid;
4651 }
4652
4653 static inline int low2highgid(int gid)
4654 {
4655 if ((int16_t)gid == -1)
4656 return -1;
4657 else
4658 return gid;
4659 }
4660 static inline int tswapid(int id)
4661 {
4662 return tswap16(id);
4663 }
4664 #else /* !USE_UID16 */
4665 static inline int high2lowuid(int uid)
4666 {
4667 return uid;
4668 }
4669 static inline int high2lowgid(int gid)
4670 {
4671 return gid;
4672 }
4673 static inline int low2highuid(int uid)
4674 {
4675 return uid;
4676 }
4677 static inline int low2highgid(int gid)
4678 {
4679 return gid;
4680 }
4681 static inline int tswapid(int id)
4682 {
4683 return tswap32(id);
4684 }
4685 #endif /* USE_UID16 */
4686
4687 void syscall_init(void)
4688 {
4689 IOCTLEntry *ie;
4690 const argtype *arg_type;
4691 int size;
4692 int i;
4693
4694 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
4695 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
4696 #include "syscall_types.h"
4697 #undef STRUCT
4698 #undef STRUCT_SPECIAL
4699
4700 /* Build target_to_host_errno_table[] table from
4701 * host_to_target_errno_table[]. */
4702 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
4703 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4704 }
4705
4706 /* we patch the ioctl size if necessary. We rely on the fact that
4707 no ioctl has all the bits at '1' in the size field */
4708 ie = ioctl_entries;
4709 while (ie->target_cmd != 0) {
4710 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4711 TARGET_IOC_SIZEMASK) {
4712 arg_type = ie->arg_type;
4713 if (arg_type[0] != TYPE_PTR) {
4714 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
4715 ie->target_cmd);
4716 exit(1);
4717 }
4718 arg_type++;
4719 size = thunk_type_size(arg_type, 0);
4720 ie->target_cmd = (ie->target_cmd &
4721 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4722 (size << TARGET_IOC_SIZESHIFT);
4723 }
4724
4725 /* automatic consistency check if same arch */
4726 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4727 (defined(__x86_64__) && defined(TARGET_X86_64))
4728 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4729 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4730 ie->name, ie->target_cmd, ie->host_cmd);
4731 }
4732 #endif
4733 ie++;
4734 }
4735 }
4736
4737 #if TARGET_ABI_BITS == 32
4738 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4739 {
4740 #ifdef TARGET_WORDS_BIGENDIAN
4741 return ((uint64_t)word0 << 32) | word1;
4742 #else
4743 return ((uint64_t)word1 << 32) | word0;
4744 #endif
4745 }
4746 #else /* TARGET_ABI_BITS == 32 */
4747 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4748 {
4749 return word0;
4750 }
4751 #endif /* TARGET_ABI_BITS != 32 */
4752
4753 #ifdef TARGET_NR_truncate64
4754 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4755 abi_long arg2,
4756 abi_long arg3,
4757 abi_long arg4)
4758 {
4759 if (regpairs_aligned(cpu_env)) {
4760 arg2 = arg3;
4761 arg3 = arg4;
4762 }
4763 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4764 }
4765 #endif
4766
4767 #ifdef TARGET_NR_ftruncate64
4768 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4769 abi_long arg2,
4770 abi_long arg3,
4771 abi_long arg4)
4772 {
4773 if (regpairs_aligned(cpu_env)) {
4774 arg2 = arg3;
4775 arg3 = arg4;
4776 }
4777 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4778 }
4779 #endif
4780
4781 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4782 abi_ulong target_addr)
4783 {
4784 struct target_timespec *target_ts;
4785
4786 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4787 return -TARGET_EFAULT;
4788 host_ts->tv_sec = tswapal(target_ts->tv_sec);
4789 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
4790 unlock_user_struct(target_ts, target_addr, 0);
4791 return 0;
4792 }
4793
4794 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4795 struct timespec *host_ts)
4796 {
4797 struct target_timespec *target_ts;
4798
4799 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4800 return -TARGET_EFAULT;
4801 target_ts->tv_sec = tswapal(host_ts->tv_sec);
4802 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
4803 unlock_user_struct(target_ts, target_addr, 1);
4804 return 0;
4805 }
4806
4807 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
4808 static inline abi_long host_to_target_stat64(void *cpu_env,
4809 abi_ulong target_addr,
4810 struct stat *host_st)
4811 {
4812 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
4813 if (((CPUARMState *)cpu_env)->eabi) {
4814 struct target_eabi_stat64 *target_st;
4815
4816 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4817 return -TARGET_EFAULT;
4818 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4819 __put_user(host_st->st_dev, &target_st->st_dev);
4820 __put_user(host_st->st_ino, &target_st->st_ino);
4821 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4822 __put_user(host_st->st_ino, &target_st->__st_ino);
4823 #endif
4824 __put_user(host_st->st_mode, &target_st->st_mode);
4825 __put_user(host_st->st_nlink, &target_st->st_nlink);
4826 __put_user(host_st->st_uid, &target_st->st_uid);
4827 __put_user(host_st->st_gid, &target_st->st_gid);
4828 __put_user(host_st->st_rdev, &target_st->st_rdev);
4829 __put_user(host_st->st_size, &target_st->st_size);
4830 __put_user(host_st->st_blksize, &target_st->st_blksize);
4831 __put_user(host_st->st_blocks, &target_st->st_blocks);
4832 __put_user(host_st->st_atime, &target_st->target_st_atime);
4833 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4834 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4835 unlock_user_struct(target_st, target_addr, 1);
4836 } else
4837 #endif
4838 {
4839 #if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
4840 struct target_stat *target_st;
4841 #else
4842 struct target_stat64 *target_st;
4843 #endif
4844
4845 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4846 return -TARGET_EFAULT;
4847 memset(target_st, 0, sizeof(*target_st));
4848 __put_user(host_st->st_dev, &target_st->st_dev);
4849 __put_user(host_st->st_ino, &target_st->st_ino);
4850 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4851 __put_user(host_st->st_ino, &target_st->__st_ino);
4852 #endif
4853 __put_user(host_st->st_mode, &target_st->st_mode);
4854 __put_user(host_st->st_nlink, &target_st->st_nlink);
4855 __put_user(host_st->st_uid, &target_st->st_uid);
4856 __put_user(host_st->st_gid, &target_st->st_gid);
4857 __put_user(host_st->st_rdev, &target_st->st_rdev);
4858 /* XXX: better use of kernel struct */
4859 __put_user(host_st->st_size, &target_st->st_size);
4860 __put_user(host_st->st_blksize, &target_st->st_blksize);
4861 __put_user(host_st->st_blocks, &target_st->st_blocks);
4862 __put_user(host_st->st_atime, &target_st->target_st_atime);
4863 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4864 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4865 unlock_user_struct(target_st, target_addr, 1);
4866 }
4867
4868 return 0;
4869 }
4870 #endif
4871
4872 /* ??? Using host futex calls even when target atomic operations
4873 are not really atomic probably breaks things. However implementing
4874 futexes locally would make futexes shared between multiple processes
4875 tricky. However they're probably useless because guest atomic
4876 operations won't work either. */
4877 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4878 target_ulong uaddr2, int val3)
4879 {
4880 struct timespec ts, *pts;
4881 int base_op;
4882
4883 /* ??? We assume FUTEX_* constants are the same on both host
4884 and target. */
4885 #ifdef FUTEX_CMD_MASK
4886 base_op = op & FUTEX_CMD_MASK;
4887 #else
4888 base_op = op;
4889 #endif
4890 switch (base_op) {
4891 case FUTEX_WAIT:
4892 case FUTEX_WAIT_BITSET:
4893 if (timeout) {
4894 pts = &ts;
4895 target_to_host_timespec(pts, timeout);
4896 } else {
4897 pts = NULL;
4898 }
4899 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
4900 pts, NULL, val3));
4901 case FUTEX_WAKE:
4902 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4903 case FUTEX_FD:
4904 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4905 case FUTEX_REQUEUE:
4906 case FUTEX_CMP_REQUEUE:
4907 case FUTEX_WAKE_OP:
4908 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4909 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4910 But the prototype takes a `struct timespec *'; insert casts
4911 to satisfy the compiler. We do not need to tswap TIMEOUT
4912 since it's not compared to guest memory. */
4913 pts = (struct timespec *)(uintptr_t) timeout;
4914 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4915 g2h(uaddr2),
4916 (base_op == FUTEX_CMP_REQUEUE
4917 ? tswap32(val3)
4918 : val3)));
4919 default:
4920 return -TARGET_ENOSYS;
4921 }
4922 }
4923
4924 /* Map host to target signal numbers for the wait family of syscalls.
4925 Assume all other status bits are the same. */
4926 int host_to_target_waitstatus(int status)
4927 {
4928 if (WIFSIGNALED(status)) {
4929 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4930 }
4931 if (WIFSTOPPED(status)) {
4932 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4933 | (status & 0xff);
4934 }
4935 return status;
4936 }
4937
4938 static int relstr_to_int(const char *s)
4939 {
4940 /* Convert a uname release string like "2.6.18" to an integer
4941 * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
4942 */
4943 int i, n, tmp;
4944
4945 tmp = 0;
4946 for (i = 0; i < 3; i++) {
4947 n = 0;
4948 while (*s >= '0' && *s <= '9') {
4949 n *= 10;
4950 n += *s - '0';
4951 s++;
4952 }
4953 tmp = (tmp << 8) + n;
4954 if (*s == '.') {
4955 s++;
4956 }
4957 }
4958 return tmp;
4959 }
4960
4961 int get_osversion(void)
4962 {
4963 static int osversion;
4964 struct new_utsname buf;
4965 const char *s;
4966
4967 if (osversion)
4968 return osversion;
4969 if (qemu_uname_release && *qemu_uname_release) {
4970 s = qemu_uname_release;
4971 } else {
4972 if (sys_uname(&buf))
4973 return 0;
4974 s = buf.release;
4975 }
4976 osversion = relstr_to_int(s);
4977 return osversion;
4978 }
4979
4980 void init_qemu_uname_release(void)
4981 {
4982 /* Initialize qemu_uname_release for later use.
4983 * If the host kernel is too old and the user hasn't asked for
4984 * a specific fake version number, we might want to fake a minimum
4985 * target kernel version.
4986 */
4987 #ifdef UNAME_MINIMUM_RELEASE
4988 struct new_utsname buf;
4989
4990 if (qemu_uname_release && *qemu_uname_release) {
4991 return;
4992 }
4993
4994 if (sys_uname(&buf)) {
4995 return;
4996 }
4997
4998 if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
4999 qemu_uname_release = UNAME_MINIMUM_RELEASE;
5000 }
5001 #endif
5002 }
5003
5004 static int open_self_maps(void *cpu_env, int fd)
5005 {
5006 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
5007 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
5008 #endif
5009 FILE *fp;
5010 char *line = NULL;
5011 size_t len = 0;
5012 ssize_t read;
5013
5014 fp = fopen("/proc/self/maps", "r");
5015 if (fp == NULL) {
5016 return -EACCES;
5017 }
5018
5019 while ((read = getline(&line, &len, fp)) != -1) {
5020 int fields, dev_maj, dev_min, inode;
5021 uint64_t min, max, offset;
5022 char flag_r, flag_w, flag_x, flag_p;
5023 char path[512] = "";
5024 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
5025 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
5026 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
5027
5028 if ((fields < 10) || (fields > 11)) {
5029 continue;
5030 }
5031 if (!strncmp(path, "[stack]", 7)) {
5032 continue;
5033 }
5034 if (h2g_valid(min) && h2g_valid(max)) {
5035 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
5036 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
5037 h2g(min), h2g(max), flag_r, flag_w,
5038 flag_x, flag_p, offset, dev_maj, dev_min, inode,
5039 path[0] ? " " : "", path);
5040 }
5041 }
5042
5043 free(line);
5044 fclose(fp);
5045
5046 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
5047 dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
5048 (unsigned long long)ts->info->stack_limit,
5049 (unsigned long long)(ts->info->start_stack +
5050 (TARGET_PAGE_SIZE - 1)) & TARGET_PAGE_MASK,
5051 (unsigned long long)0);
5052 #endif
5053
5054 return 0;
5055 }
5056
5057 static int open_self_stat(void *cpu_env, int fd)
5058 {
5059 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
5060 abi_ulong start_stack = ts->info->start_stack;
5061 int i;
5062
5063 for (i = 0; i < 44; i++) {
5064 char buf[128];
5065 int len;
5066 uint64_t val = 0;
5067
5068 if (i == 0) {
5069 /* pid */
5070 val = getpid();
5071 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5072 } else if (i == 1) {
5073 /* app name */
5074 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
5075 } else if (i == 27) {
5076 /* stack bottom */
5077 val = start_stack;
5078 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5079 } else {
5080 /* for the rest, there is MasterCard */
5081 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
5082 }
5083
5084 len = strlen(buf);
5085 if (write(fd, buf, len) != len) {
5086 return -1;
5087 }
5088 }
5089
5090 return 0;
5091 }
5092
5093 static int open_self_auxv(void *cpu_env, int fd)
5094 {
5095 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
5096 abi_ulong auxv = ts->info->saved_auxv;
5097 abi_ulong len = ts->info->auxv_len;
5098 char *ptr;
5099
5100 /*
5101 * Auxiliary vector is stored in target process stack.
5102 * read in whole auxv vector and copy it to file
5103 */
5104 ptr = lock_user(VERIFY_READ, auxv, len, 0);
5105 if (ptr != NULL) {
5106 while (len > 0) {
5107 ssize_t r;
5108 r = write(fd, ptr, len);
5109 if (r <= 0) {
5110 break;
5111 }
5112 len -= r;
5113 ptr += r;
5114 }
5115 lseek(fd, 0, SEEK_SET);
5116 unlock_user(ptr, auxv, len);
5117 }
5118
5119 return 0;
5120 }
5121
5122 static int is_proc_myself(const char *filename, const char *entry)
5123 {
5124 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
5125 filename += strlen("/proc/");
5126 if (!strncmp(filename, "self/", strlen("self/"))) {
5127 filename += strlen("self/");
5128 } else if (*filename >= '1' && *filename <= '9') {
5129 char myself[80];
5130 snprintf(myself, sizeof(myself), "%d/", getpid());
5131 if (!strncmp(filename, myself, strlen(myself))) {
5132 filename += strlen(myself);
5133 } else {
5134 return 0;
5135 }
5136 } else {
5137 return 0;
5138 }
5139 if (!strcmp(filename, entry)) {
5140 return 1;
5141 }
5142 }
5143 return 0;
5144 }
5145
5146 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5147 static int is_proc(const char *filename, const char *entry)
5148 {
5149 return strcmp(filename, entry) == 0;
5150 }
5151
5152 static int open_net_route(void *cpu_env, int fd)
5153 {
5154 FILE *fp;
5155 char *line = NULL;
5156 size_t len = 0;
5157 ssize_t read;
5158
5159 fp = fopen("/proc/net/route", "r");
5160 if (fp == NULL) {
5161 return -EACCES;
5162 }
5163
5164 /* read header */
5165
5166 read = getline(&line, &len, fp);
5167 dprintf(fd, "%s", line);
5168
5169 /* read routes */
5170
5171 while ((read = getline(&line, &len, fp)) != -1) {
5172 char iface[16];
5173 uint32_t dest, gw, mask;
5174 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
5175 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5176 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
5177 &mask, &mtu, &window, &irtt);
5178 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5179 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
5180 metric, tswap32(mask), mtu, window, irtt);
5181 }
5182
5183 free(line);
5184 fclose(fp);
5185
5186 return 0;
5187 }
5188 #endif
5189
5190 static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
5191 {
5192 struct fake_open {
5193 const char *filename;
5194 int (*fill)(void *cpu_env, int fd);
5195 int (*cmp)(const char *s1, const char *s2);
5196 };
5197 const struct fake_open *fake_open;
5198 static const struct fake_open fakes[] = {
5199 { "maps", open_self_maps, is_proc_myself },
5200 { "stat", open_self_stat, is_proc_myself },
5201 { "auxv", open_self_auxv, is_proc_myself },
5202 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5203 { "/proc/net/route", open_net_route, is_proc },
5204 #endif
5205 { NULL, NULL, NULL }
5206 };
5207
5208 for (fake_open = fakes; fake_open->filename; fake_open++) {
5209 if (fake_open->cmp(pathname, fake_open->filename)) {
5210 break;
5211 }
5212 }
5213
5214 if (fake_open->filename) {
5215 const char *tmpdir;
5216 char filename[PATH_MAX];
5217 int fd, r;
5218
5219 /* create temporary file to map stat to */
5220 tmpdir = getenv("TMPDIR");
5221 if (!tmpdir)
5222 tmpdir = "/tmp";
5223 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5224 fd = mkstemp(filename);
5225 if (fd < 0) {
5226 return fd;
5227 }
5228 unlink(filename);
5229
5230 if ((r = fake_open->fill(cpu_env, fd))) {
5231 close(fd);
5232 return r;
5233 }
5234 lseek(fd, 0, SEEK_SET);
5235
5236 return fd;
5237 }
5238
5239 return get_errno(open(path(pathname), flags, mode));
5240 }
5241
5242 /* do_syscall() should always have a single exit point at the end so
5243 that actions, such as logging of syscall results, can be performed.
5244 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
5245 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5246 abi_long arg2, abi_long arg3, abi_long arg4,
5247 abi_long arg5, abi_long arg6, abi_long arg7,
5248 abi_long arg8)
5249 {
5250 CPUState *cpu = ENV_GET_CPU(cpu_env);
5251 abi_long ret;
5252 struct stat st;
5253 struct statfs stfs;
5254 void *p;
5255
5256 #ifdef DEBUG
5257 gemu_log("syscall %d", num);
5258 #endif
5259 if(do_strace)
5260 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5261
5262 switch(num) {
5263 case TARGET_NR_exit:
5264 /* In old applications this may be used to implement _exit(2).
5265 However in threaded applictions it is used for thread termination,
5266 and _exit_group is used for application termination.
5267 Do thread termination if we have more then one thread. */
5268 /* FIXME: This probably breaks if a signal arrives. We should probably
5269 be disabling signals. */
5270 if (CPU_NEXT(first_cpu)) {
5271 TaskState *ts;
5272
5273 cpu_list_lock();
5274 /* Remove the CPU from the list. */
5275 QTAILQ_REMOVE(&cpus, cpu, node);
5276 cpu_list_unlock();
5277 ts = ((CPUArchState *)cpu_env)->opaque;
5278 if (ts->child_tidptr) {
5279 put_user_u32(0, ts->child_tidptr);
5280 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5281 NULL, NULL, 0);
5282 }
5283 thread_cpu = NULL;
5284 object_unref(OBJECT(ENV_GET_CPU(cpu_env)));
5285 g_free(ts);
5286 pthread_exit(NULL);
5287 }
5288 #ifdef TARGET_GPROF
5289 _mcleanup();
5290 #endif
5291 gdb_exit(cpu_env, arg1);
5292 _exit(arg1);
5293 ret = 0; /* avoid warning */
5294 break;
5295 case TARGET_NR_read:
5296 if (arg3 == 0)
5297 ret = 0;
5298 else {
5299 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5300 goto efault;
5301 ret = get_errno(read(arg1, p, arg3));
5302 unlock_user(p, arg2, ret);
5303 }
5304 break;
5305 case TARGET_NR_write:
5306 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5307 goto efault;
5308 ret = get_errno(write(arg1, p, arg3));
5309 unlock_user(p, arg2, 0);
5310 break;
5311 case TARGET_NR_open:
5312 if (!(p = lock_user_string(arg1)))
5313 goto efault;
5314 ret = get_errno(do_open(cpu_env, p,
5315 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5316 arg3));
5317 unlock_user(p, arg1, 0);
5318 break;
5319 #if defined(TARGET_NR_openat) && defined(__NR_openat)
5320 case TARGET_NR_openat:
5321 if (!(p = lock_user_string(arg2)))
5322 goto efault;
5323 ret = get_errno(sys_openat(arg1,
5324 path(p),
5325 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5326 arg4));
5327 unlock_user(p, arg2, 0);
5328 break;
5329 #endif
5330 case TARGET_NR_close:
5331 ret = get_errno(close(arg1));
5332 break;
5333 case TARGET_NR_brk:
5334 ret = do_brk(arg1);
5335 break;
5336 case TARGET_NR_fork:
5337 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
5338 break;
5339 #ifdef TARGET_NR_waitpid
5340 case TARGET_NR_waitpid:
5341 {
5342 int status;
5343 ret = get_errno(waitpid(arg1, &status, arg3));
5344 if (!is_error(ret) && arg2 && ret
5345 && put_user_s32(host_to_target_waitstatus(status), arg2))
5346 goto efault;
5347 }
5348 break;
5349 #endif
5350 #ifdef TARGET_NR_waitid
5351 case TARGET_NR_waitid:
5352 {
5353 siginfo_t info;
5354 info.si_pid = 0;
5355 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5356 if (!is_error(ret) && arg3 && info.si_pid != 0) {
5357 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
5358 goto efault;
5359 host_to_target_siginfo(p, &info);
5360 unlock_user(p, arg3, sizeof(target_siginfo_t));
5361 }
5362 }
5363 break;
5364 #endif
5365 #ifdef TARGET_NR_creat /* not on alpha */
5366 case TARGET_NR_creat:
5367 if (!(p = lock_user_string(arg1)))
5368 goto efault;
5369 ret = get_errno(creat(p, arg2));
5370 unlock_user(p, arg1, 0);
5371 break;
5372 #endif
5373 case TARGET_NR_link:
5374 {
5375 void * p2;
5376 p = lock_user_string(arg1);
5377 p2 = lock_user_string(arg2);
5378 if (!p || !p2)
5379 ret = -TARGET_EFAULT;
5380 else
5381 ret = get_errno(link(p, p2));
5382 unlock_user(p2, arg2, 0);
5383 unlock_user(p, arg1, 0);
5384 }
5385 break;
5386 #if defined(TARGET_NR_linkat)
5387 case TARGET_NR_linkat:
5388 {
5389 void * p2 = NULL;
5390 if (!arg2 || !arg4)
5391 goto efault;
5392 p = lock_user_string(arg2);
5393 p2 = lock_user_string(arg4);
5394 if (!p || !p2)
5395 ret = -TARGET_EFAULT;
5396 else
5397 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
5398 unlock_user(p, arg2, 0);
5399 unlock_user(p2, arg4, 0);
5400 }
5401 break;
5402 #endif
5403 case TARGET_NR_unlink:
5404 if (!(p = lock_user_string(arg1)))
5405 goto efault;
5406 ret = get_errno(unlink(p));
5407 unlock_user(p, arg1, 0);
5408 break;
5409 #if defined(TARGET_NR_unlinkat)
5410 case TARGET_NR_unlinkat:
5411 if (!(p = lock_user_string(arg2)))
5412 goto efault;
5413 ret = get_errno(unlinkat(arg1, p, arg3));
5414 unlock_user(p, arg2, 0);
5415 break;
5416 #endif
5417 case TARGET_NR_execve:
5418 {
5419 char **argp, **envp;
5420 int argc, envc;
5421 abi_ulong gp;
5422 abi_ulong guest_argp;
5423 abi_ulong guest_envp;
5424 abi_ulong addr;
5425 char **q;
5426 int total_size = 0;
5427
5428 argc = 0;
5429 guest_argp = arg2;
5430 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
5431 if (get_user_ual(addr, gp))
5432 goto efault;
5433 if (!addr)
5434 break;
5435 argc++;
5436 }
5437 envc = 0;
5438 guest_envp = arg3;
5439 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
5440 if (get_user_ual(addr, gp))
5441 goto efault;
5442 if (!addr)
5443 break;
5444 envc++;
5445 }
5446
5447 argp = alloca((argc + 1) * sizeof(void *));
5448 envp = alloca((envc + 1) * sizeof(void *));
5449
5450 for (gp = guest_argp, q = argp; gp;
5451 gp += sizeof(abi_ulong), q++) {
5452 if (get_user_ual(addr, gp))
5453 goto execve_efault;
5454 if (!addr)
5455 break;
5456 if (!(*q = lock_user_string(addr)))
5457 goto execve_efault;
5458 total_size += strlen(*q) + 1;
5459 }
5460 *q = NULL;
5461
5462 for (gp = guest_envp, q = envp; gp;
5463 gp += sizeof(abi_ulong), q++) {
5464 if (get_user_ual(addr, gp))
5465 goto execve_efault;
5466 if (!addr)
5467 break;
5468 if (!(*q = lock_user_string(addr)))
5469 goto execve_efault;
5470 total_size += strlen(*q) + 1;
5471 }
5472 *q = NULL;
5473
5474 /* This case will not be caught by the host's execve() if its
5475 page size is bigger than the target's. */
5476 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5477 ret = -TARGET_E2BIG;
5478 goto execve_end;
5479 }
5480 if (!(p = lock_user_string(arg1)))
5481 goto execve_efault;
5482 ret = get_errno(execve(p, argp, envp));
5483 unlock_user(p, arg1, 0);
5484
5485 goto execve_end;
5486
5487 execve_efault:
5488 ret = -TARGET_EFAULT;
5489
5490 execve_end:
5491 for (gp = guest_argp, q = argp; *q;
5492 gp += sizeof(abi_ulong), q++) {
5493 if (get_user_ual(addr, gp)
5494 || !addr)
5495 break;
5496 unlock_user(*q, addr, 0);
5497 }
5498 for (gp = guest_envp, q = envp; *q;
5499 gp += sizeof(abi_ulong), q++) {
5500 if (get_user_ual(addr, gp)
5501 || !addr)
5502 break;
5503 unlock_user(*q, addr, 0);
5504 }
5505 }
5506 break;
5507 case TARGET_NR_chdir:
5508 if (!(p = lock_user_string(arg1)))
5509 goto efault;
5510 ret = get_errno(chdir(p));
5511 unlock_user(p, arg1, 0);
5512 break;
5513 #ifdef TARGET_NR_time
5514 case TARGET_NR_time:
5515 {
5516 time_t host_time;
5517 ret = get_errno(time(&host_time));
5518 if (!is_error(ret)
5519 && arg1
5520 && put_user_sal(host_time, arg1))
5521 goto efault;
5522 }
5523 break;
5524 #endif
5525 case TARGET_NR_mknod:
5526 if (!(p = lock_user_string(arg1)))
5527 goto efault;
5528 ret = get_errno(mknod(p, arg2, arg3));
5529 unlock_user(p, arg1, 0);
5530 break;
5531 #if defined(TARGET_NR_mknodat)
5532 case TARGET_NR_mknodat:
5533 if (!(p = lock_user_string(arg2)))
5534 goto efault;
5535 ret = get_errno(mknodat(arg1, p, arg3, arg4));
5536 unlock_user(p, arg2, 0);
5537 break;
5538 #endif
5539 case TARGET_NR_chmod:
5540 if (!(p = lock_user_string(arg1)))
5541 goto efault;
5542 ret = get_errno(chmod(p, arg2));
5543 unlock_user(p, arg1, 0);
5544 break;
5545 #ifdef TARGET_NR_break
5546 case TARGET_NR_break:
5547 goto unimplemented;
5548 #endif
5549 #ifdef TARGET_NR_oldstat
5550 case TARGET_NR_oldstat:
5551 goto unimplemented;
5552 #endif
5553 case TARGET_NR_lseek:
5554 ret = get_errno(lseek(arg1, arg2, arg3));
5555 break;
5556 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5557 /* Alpha specific */
5558 case TARGET_NR_getxpid:
5559 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
5560 ret = get_errno(getpid());
5561 break;
5562 #endif
5563 #ifdef TARGET_NR_getpid
5564 case TARGET_NR_getpid:
5565 ret = get_errno(getpid());
5566 break;
5567 #endif
5568 case TARGET_NR_mount:
5569 {
5570 /* need to look at the data field */
5571 void *p2, *p3;
5572 p = lock_user_string(arg1);
5573 p2 = lock_user_string(arg2);
5574 p3 = lock_user_string(arg3);
5575 if (!p || !p2 || !p3)
5576 ret = -TARGET_EFAULT;
5577 else {
5578 /* FIXME - arg5 should be locked, but it isn't clear how to
5579 * do that since it's not guaranteed to be a NULL-terminated
5580 * string.
5581 */
5582 if ( ! arg5 )
5583 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
5584 else
5585 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
5586 }
5587 unlock_user(p, arg1, 0);
5588 unlock_user(p2, arg2, 0);
5589 unlock_user(p3, arg3, 0);
5590 break;
5591 }
5592 #ifdef TARGET_NR_umount
5593 case TARGET_NR_umount:
5594 if (!(p = lock_user_string(arg1)))
5595 goto efault;
5596 ret = get_errno(umount(p));
5597 unlock_user(p, arg1, 0);
5598 break;
5599 #endif
5600 #ifdef TARGET_NR_stime /* not on alpha */
5601 case TARGET_NR_stime:
5602 {
5603 time_t host_time;
5604 if (get_user_sal(host_time, arg1))
5605 goto efault;
5606 ret = get_errno(stime(&host_time));
5607 }
5608 break;
5609 #endif
5610 case TARGET_NR_ptrace:
5611 goto unimplemented;
5612 #ifdef TARGET_NR_alarm /* not on alpha */
5613 case TARGET_NR_alarm:
5614 ret = alarm(arg1);
5615 break;
5616 #endif
5617 #ifdef TARGET_NR_oldfstat
5618 case TARGET_NR_oldfstat:
5619 goto unimplemented;
5620 #endif
5621 #ifdef TARGET_NR_pause /* not on alpha */
5622 case TARGET_NR_pause:
5623 ret = get_errno(pause());
5624 break;
5625 #endif
5626 #ifdef TARGET_NR_utime
5627 case TARGET_NR_utime:
5628 {
5629 struct utimbuf tbuf, *host_tbuf;
5630 struct target_utimbuf *target_tbuf;
5631 if (arg2) {
5632 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5633 goto efault;
5634 tbuf.actime = tswapal(target_tbuf->actime);
5635 tbuf.modtime = tswapal(target_tbuf->modtime);
5636 unlock_user_struct(target_tbuf, arg2, 0);
5637 host_tbuf = &tbuf;
5638 } else {
5639 host_tbuf = NULL;
5640 }
5641 if (!(p = lock_user_string(arg1)))
5642 goto efault;
5643 ret = get_errno(utime(p, host_tbuf));
5644 unlock_user(p, arg1, 0);
5645 }
5646 break;
5647 #endif
5648 case TARGET_NR_utimes:
5649 {
5650 struct timeval *tvp, tv[2];
5651 if (arg2) {
5652 if (copy_from_user_timeval(&tv[0], arg2)
5653 || copy_from_user_timeval(&tv[1],
5654 arg2 + sizeof(struct target_timeval)))
5655 goto efault;
5656 tvp = tv;
5657 } else {
5658 tvp = NULL;
5659 }
5660 if (!(p = lock_user_string(arg1)))
5661 goto efault;
5662 ret = get_errno(utimes(p, tvp));
5663 unlock_user(p, arg1, 0);
5664 }
5665 break;
5666 #if defined(TARGET_NR_futimesat)
5667 case TARGET_NR_futimesat:
5668 {
5669 struct timeval *tvp, tv[2];
5670 if (arg3) {
5671 if (copy_from_user_timeval(&tv[0], arg3)
5672 || copy_from_user_timeval(&tv[1],
5673 arg3 + sizeof(struct target_timeval)))
5674 goto efault;
5675 tvp = tv;
5676 } else {
5677 tvp = NULL;
5678 }
5679 if (!(p = lock_user_string(arg2)))
5680 goto efault;
5681 ret = get_errno(futimesat(arg1, path(p), tvp));
5682 unlock_user(p, arg2, 0);
5683 }
5684 break;
5685 #endif
5686 #ifdef TARGET_NR_stty
5687 case TARGET_NR_stty:
5688 goto unimplemented;
5689 #endif
5690 #ifdef TARGET_NR_gtty
5691 case TARGET_NR_gtty:
5692 goto unimplemented;
5693 #endif
5694 case TARGET_NR_access:
5695 if (!(p = lock_user_string(arg1)))
5696 goto efault;
5697 ret = get_errno(access(path(p), arg2));
5698 unlock_user(p, arg1, 0);
5699 break;
5700 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5701 case TARGET_NR_faccessat:
5702 if (!(p = lock_user_string(arg2)))
5703 goto efault;
5704 ret = get_errno(faccessat(arg1, p, arg3, 0));
5705 unlock_user(p, arg2, 0);
5706 break;
5707 #endif
5708 #ifdef TARGET_NR_nice /* not on alpha */
5709 case TARGET_NR_nice:
5710 ret = get_errno(nice(arg1));
5711 break;
5712 #endif
5713 #ifdef TARGET_NR_ftime
5714 case TARGET_NR_ftime:
5715 goto unimplemented;
5716 #endif
5717 case TARGET_NR_sync:
5718 sync();
5719 ret = 0;
5720 break;
5721 case TARGET_NR_kill:
5722 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
5723 break;
5724 case TARGET_NR_rename:
5725 {
5726 void *p2;
5727 p = lock_user_string(arg1);
5728 p2 = lock_user_string(arg2);
5729 if (!p || !p2)
5730 ret = -TARGET_EFAULT;
5731 else
5732 ret = get_errno(rename(p, p2));
5733 unlock_user(p2, arg2, 0);
5734 unlock_user(p, arg1, 0);
5735 }
5736 break;
5737 #if defined(TARGET_NR_renameat)
5738 case TARGET_NR_renameat:
5739 {
5740 void *p2;
5741 p = lock_user_string(arg2);
5742 p2 = lock_user_string(arg4);
5743 if (!p || !p2)
5744 ret = -TARGET_EFAULT;
5745 else
5746 ret = get_errno(renameat(arg1, p, arg3, p2));
5747 unlock_user(p2, arg4, 0);
5748 unlock_user(p, arg2, 0);
5749 }
5750 break;
5751 #endif
5752 case TARGET_NR_mkdir:
5753 if (!(p = lock_user_string(arg1)))
5754 goto efault;
5755 ret = get_errno(mkdir(p, arg2));
5756 unlock_user(p, arg1, 0);
5757 break;
5758 #if defined(TARGET_NR_mkdirat)
5759 case TARGET_NR_mkdirat:
5760 if (!(p = lock_user_string(arg2)))
5761 goto efault;
5762 ret = get_errno(mkdirat(arg1, p, arg3));
5763 unlock_user(p, arg2, 0);
5764 break;
5765 #endif
5766 case TARGET_NR_rmdir:
5767 if (!(p = lock_user_string(arg1)))
5768 goto efault;
5769 ret = get_errno(rmdir(p));
5770 unlock_user(p, arg1, 0);
5771 break;
5772 case TARGET_NR_dup:
5773 ret = get_errno(dup(arg1));
5774 break;
5775 case TARGET_NR_pipe:
5776 ret = do_pipe(cpu_env, arg1, 0, 0);
5777 break;
5778 #ifdef TARGET_NR_pipe2
5779 case TARGET_NR_pipe2:
5780 ret = do_pipe(cpu_env, arg1,
5781 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
5782 break;
5783 #endif
5784 case TARGET_NR_times:
5785 {
5786 struct target_tms *tmsp;
5787 struct tms tms;
5788 ret = get_errno(times(&tms));
5789 if (arg1) {
5790 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5791 if (!tmsp)
5792 goto efault;
5793 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
5794 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
5795 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
5796 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
5797 }
5798 if (!is_error(ret))
5799 ret = host_to_target_clock_t(ret);
5800 }
5801 break;
5802 #ifdef TARGET_NR_prof
5803 case TARGET_NR_prof:
5804 goto unimplemented;
5805 #endif
5806 #ifdef TARGET_NR_signal
5807 case TARGET_NR_signal:
5808 goto unimplemented;
5809 #endif
5810 case TARGET_NR_acct:
5811 if (arg1 == 0) {
5812 ret = get_errno(acct(NULL));
5813 } else {
5814 if (!(p = lock_user_string(arg1)))
5815 goto efault;
5816 ret = get_errno(acct(path(p)));
5817 unlock_user(p, arg1, 0);
5818 }
5819 break;
5820 #ifdef TARGET_NR_umount2
5821 case TARGET_NR_umount2:
5822 if (!(p = lock_user_string(arg1)))
5823 goto efault;
5824 ret = get_errno(umount2(p, arg2));
5825 unlock_user(p, arg1, 0);
5826 break;
5827 #endif
5828 #ifdef TARGET_NR_lock
5829 case TARGET_NR_lock:
5830 goto unimplemented;
5831 #endif
5832 case TARGET_NR_ioctl:
5833 ret = do_ioctl(arg1, arg2, arg3);
5834 break;
5835 case TARGET_NR_fcntl:
5836 ret = do_fcntl(arg1, arg2, arg3);
5837 break;
5838 #ifdef TARGET_NR_mpx
5839 case TARGET_NR_mpx:
5840 goto unimplemented;
5841 #endif
5842 case TARGET_NR_setpgid:
5843 ret = get_errno(setpgid(arg1, arg2));
5844 break;
5845 #ifdef TARGET_NR_ulimit
5846 case TARGET_NR_ulimit:
5847 goto unimplemented;
5848 #endif
5849 #ifdef TARGET_NR_oldolduname
5850 case TARGET_NR_oldolduname:
5851 goto unimplemented;
5852 #endif
5853 case TARGET_NR_umask:
5854 ret = get_errno(umask(arg1));
5855 break;
5856 case TARGET_NR_chroot:
5857 if (!(p = lock_user_string(arg1)))
5858 goto efault;
5859 ret = get_errno(chroot(p));
5860 unlock_user(p, arg1, 0);
5861 break;
5862 case TARGET_NR_ustat:
5863 goto unimplemented;
5864 case TARGET_NR_dup2:
5865 ret = get_errno(dup2(arg1, arg2));
5866 break;
5867 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5868 case TARGET_NR_dup3:
5869 ret = get_errno(dup3(arg1, arg2, arg3));
5870 break;
5871 #endif
5872 #ifdef TARGET_NR_getppid /* not on alpha */
5873 case TARGET_NR_getppid:
5874 ret = get_errno(getppid());
5875 break;
5876 #endif
5877 case TARGET_NR_getpgrp:
5878 ret = get_errno(getpgrp());
5879 break;
5880 case TARGET_NR_setsid:
5881 ret = get_errno(setsid());
5882 break;
5883 #ifdef TARGET_NR_sigaction
5884 case TARGET_NR_sigaction:
5885 {
5886 #if defined(TARGET_ALPHA)
5887 struct target_sigaction act, oact, *pact = 0;
5888 struct target_old_sigaction *old_act;
5889 if (arg2) {
5890 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5891 goto efault;
5892 act._sa_handler = old_act->_sa_handler;
5893 target_siginitset(&act.sa_mask, old_act->sa_mask);
5894 act.sa_flags = old_act->sa_flags;
5895 act.sa_restorer = 0;
5896 unlock_user_struct(old_act, arg2, 0);
5897 pact = &act;
5898 }
5899 ret = get_errno(do_sigaction(arg1, pact, &oact));
5900 if (!is_error(ret) && arg3) {
5901 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5902 goto efault;
5903 old_act->_sa_handler = oact._sa_handler;
5904 old_act->sa_mask = oact.sa_mask.sig[0];
5905 old_act->sa_flags = oact.sa_flags;
5906 unlock_user_struct(old_act, arg3, 1);
5907 }
5908 #elif defined(TARGET_MIPS)
5909 struct target_sigaction act, oact, *pact, *old_act;
5910
5911 if (arg2) {
5912 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5913 goto efault;
5914 act._sa_handler = old_act->_sa_handler;
5915 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5916 act.sa_flags = old_act->sa_flags;
5917 unlock_user_struct(old_act, arg2, 0);
5918 pact = &act;
5919 } else {
5920 pact = NULL;
5921 }
5922
5923 ret = get_errno(do_sigaction(arg1, pact, &oact));
5924
5925 if (!is_error(ret) && arg3) {
5926 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5927 goto efault;
5928 old_act->_sa_handler = oact._sa_handler;
5929 old_act->sa_flags = oact.sa_flags;
5930 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5931 old_act->sa_mask.sig[1] = 0;
5932 old_act->sa_mask.sig[2] = 0;
5933 old_act->sa_mask.sig[3] = 0;
5934 unlock_user_struct(old_act, arg3, 1);
5935 }
5936 #else
5937 struct target_old_sigaction *old_act;
5938 struct target_sigaction act, oact, *pact;
5939 if (arg2) {
5940 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5941 goto efault;
5942 act._sa_handler = old_act->_sa_handler;
5943 target_siginitset(&act.sa_mask, old_act->sa_mask);
5944 act.sa_flags = old_act->sa_flags;
5945 act.sa_restorer = old_act->sa_restorer;
5946 unlock_user_struct(old_act, arg2, 0);
5947 pact = &act;
5948 } else {
5949 pact = NULL;
5950 }
5951 ret = get_errno(do_sigaction(arg1, pact, &oact));
5952 if (!is_error(ret) && arg3) {
5953 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5954 goto efault;
5955 old_act->_sa_handler = oact._sa_handler;
5956 old_act->sa_mask = oact.sa_mask.sig[0];
5957 old_act->sa_flags = oact.sa_flags;
5958 old_act->sa_restorer = oact.sa_restorer;
5959 unlock_user_struct(old_act, arg3, 1);
5960 }
5961 #endif
5962 }
5963 break;
5964 #endif
5965 case TARGET_NR_rt_sigaction:
5966 {
5967 #if defined(TARGET_ALPHA)
5968 struct target_sigaction act, oact, *pact = 0;
5969 struct target_rt_sigaction *rt_act;
5970 /* ??? arg4 == sizeof(sigset_t). */
5971 if (arg2) {
5972 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5973 goto efault;
5974 act._sa_handler = rt_act->_sa_handler;
5975 act.sa_mask = rt_act->sa_mask;
5976 act.sa_flags = rt_act->sa_flags;
5977 act.sa_restorer = arg5;
5978 unlock_user_struct(rt_act, arg2, 0);
5979 pact = &act;
5980 }
5981 ret = get_errno(do_sigaction(arg1, pact, &oact));
5982 if (!is_error(ret) && arg3) {
5983 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5984 goto efault;
5985 rt_act->_sa_handler = oact._sa_handler;
5986 rt_act->sa_mask = oact.sa_mask;
5987 rt_act->sa_flags = oact.sa_flags;
5988 unlock_user_struct(rt_act, arg3, 1);
5989 }
5990 #else
5991 struct target_sigaction *act;
5992 struct target_sigaction *oact;
5993
5994 if (arg2) {
5995 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5996 goto efault;
5997 } else
5998 act = NULL;
5999 if (arg3) {
6000 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
6001 ret = -TARGET_EFAULT;
6002 goto rt_sigaction_fail;
6003 }
6004 } else
6005 oact = NULL;
6006 ret = get_errno(do_sigaction(arg1, act, oact));
6007 rt_sigaction_fail:
6008 if (act)
6009 unlock_user_struct(act, arg2, 0);
6010 if (oact)
6011 unlock_user_struct(oact, arg3, 1);
6012 #endif
6013 }
6014 break;
6015 #ifdef TARGET_NR_sgetmask /* not on alpha */
6016 case TARGET_NR_sgetmask:
6017 {
6018 sigset_t cur_set;
6019 abi_ulong target_set;
6020 sigprocmask(0, NULL, &cur_set);
6021 host_to_target_old_sigset(&target_set, &cur_set);
6022 ret = target_set;
6023 }
6024 break;
6025 #endif
6026 #ifdef TARGET_NR_ssetmask /* not on alpha */
6027 case TARGET_NR_ssetmask:
6028 {
6029 sigset_t set, oset, cur_set;
6030 abi_ulong target_set = arg1;
6031 sigprocmask(0, NULL, &cur_set);
6032 target_to_host_old_sigset(&set, &target_set);
6033 sigorset(&set, &set, &cur_set);
6034 sigprocmask(SIG_SETMASK, &set, &oset);
6035 host_to_target_old_sigset(&target_set, &oset);
6036 ret = target_set;
6037 }
6038 break;
6039 #endif
6040 #ifdef TARGET_NR_sigprocmask
6041 case TARGET_NR_sigprocmask:
6042 {
6043 #if defined(TARGET_ALPHA)
6044 sigset_t set, oldset;
6045 abi_ulong mask;
6046 int how;
6047
6048 switch (arg1) {
6049 case TARGET_SIG_BLOCK:
6050 how = SIG_BLOCK;
6051 break;
6052 case TARGET_SIG_UNBLOCK:
6053 how = SIG_UNBLOCK;
6054 break;
6055 case TARGET_SIG_SETMASK:
6056 how = SIG_SETMASK;
6057 break;
6058 default:
6059 ret = -TARGET_EINVAL;
6060 goto fail;
6061 }
6062 mask = arg2;
6063 target_to_host_old_sigset(&set, &mask);
6064
6065 ret = get_errno(sigprocmask(how, &set, &oldset));
6066 if (!is_error(ret)) {
6067 host_to_target_old_sigset(&mask, &oldset);
6068 ret = mask;
6069 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
6070 }
6071 #else
6072 sigset_t set, oldset, *set_ptr;
6073 int how;
6074
6075 if (arg2) {
6076 switch (arg1) {
6077 case TARGET_SIG_BLOCK:
6078 how = SIG_BLOCK;
6079 break;
6080 case TARGET_SIG_UNBLOCK:
6081 how = SIG_UNBLOCK;
6082 break;
6083 case TARGET_SIG_SETMASK:
6084 how = SIG_SETMASK;
6085 break;
6086 default:
6087 ret = -TARGET_EINVAL;
6088 goto fail;
6089 }
6090 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
6091 goto efault;
6092 target_to_host_old_sigset(&set, p);
6093 unlock_user(p, arg2, 0);
6094 set_ptr = &set;
6095 } else {
6096 how = 0;
6097 set_ptr = NULL;
6098 }
6099 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
6100 if (!is_error(ret) && arg3) {
6101 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
6102 goto efault;
6103 host_to_target_old_sigset(p, &oldset);
6104 unlock_user(p, arg3, sizeof(target_sigset_t));
6105 }
6106 #endif
6107 }
6108 break;
6109 #endif
6110 case TARGET_NR_rt_sigprocmask:
6111 {
6112 int how = arg1;
6113 sigset_t set, oldset, *set_ptr;
6114
6115 if (arg2) {
6116 switch(how) {
6117 case TARGET_SIG_BLOCK:
6118 how = SIG_BLOCK;
6119 break;
6120 case TARGET_SIG_UNBLOCK:
6121 how = SIG_UNBLOCK;
6122 break;
6123 case TARGET_SIG_SETMASK:
6124 how = SIG_SETMASK;
6125 break;
6126 default:
6127 ret = -TARGET_EINVAL;
6128 goto fail;
6129 }
6130 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
6131 goto efault;
6132 target_to_host_sigset(&set, p);
6133 unlock_user(p, arg2, 0);
6134 set_ptr = &set;
6135 } else {
6136 how = 0;
6137 set_ptr = NULL;
6138 }
6139 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
6140 if (!is_error(ret) && arg3) {
6141 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
6142 goto efault;
6143 host_to_target_sigset(p, &oldset);
6144 unlock_user(p, arg3, sizeof(target_sigset_t));
6145 }
6146 }
6147 break;
6148 #ifdef TARGET_NR_sigpending
6149 case TARGET_NR_sigpending:
6150 {
6151 sigset_t set;
6152 ret = get_errno(sigpending(&set));
6153 if (!is_error(ret)) {
6154 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
6155 goto efault;
6156 host_to_target_old_sigset(p, &set);
6157 unlock_user(p, arg1, sizeof(target_sigset_t));
6158 }
6159 }
6160 break;
6161 #endif
6162 case TARGET_NR_rt_sigpending:
6163 {
6164 sigset_t set;
6165 ret = get_errno(sigpending(&set));
6166 if (!is_error(ret)) {
6167 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
6168 goto efault;
6169 host_to_target_sigset(p, &set);
6170 unlock_user(p, arg1, sizeof(target_sigset_t));
6171 }
6172 }
6173 break;
6174 #ifdef TARGET_NR_sigsuspend
6175 case TARGET_NR_sigsuspend:
6176 {
6177 sigset_t set;
6178 #if defined(TARGET_ALPHA)
6179 abi_ulong mask = arg1;
6180 target_to_host_old_sigset(&set, &mask);
6181 #else
6182 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
6183 goto efault;
6184 target_to_host_old_sigset(&set, p);
6185 unlock_user(p, arg1, 0);
6186 #endif
6187 ret = get_errno(sigsuspend(&set));
6188 }
6189 break;
6190 #endif
6191 case TARGET_NR_rt_sigsuspend:
6192 {
6193 sigset_t set;
6194 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
6195 goto efault;
6196 target_to_host_sigset(&set, p);
6197 unlock_user(p, arg1, 0);
6198 ret = get_errno(sigsuspend(&set));
6199 }
6200 break;
6201 case TARGET_NR_rt_sigtimedwait:
6202 {
6203 sigset_t set;
6204 struct timespec uts, *puts;
6205 siginfo_t uinfo;
6206
6207 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
6208 goto efault;
6209 target_to_host_sigset(&set, p);
6210 unlock_user(p, arg1, 0);
6211 if (arg3) {
6212 puts = &uts;
6213 target_to_host_timespec(puts, arg3);
6214 } else {
6215 puts = NULL;
6216 }
6217 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
6218 if (!is_error(ret) && arg2) {
6219 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
6220 goto efault;
6221 host_to_target_siginfo(p, &uinfo);
6222 unlock_user(p, arg2, sizeof(target_siginfo_t));
6223 }
6224 }
6225 break;
6226 case TARGET_NR_rt_sigqueueinfo:
6227 {
6228 siginfo_t uinfo;
6229 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
6230 goto efault;
6231 target_to_host_siginfo(&uinfo, p);
6232 unlock_user(p, arg1, 0);
6233 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6234 }
6235 break;
6236 #ifdef TARGET_NR_sigreturn
6237 case TARGET_NR_sigreturn:
6238 /* NOTE: ret is eax, so not transcoding must be done */
6239 ret = do_sigreturn(cpu_env);
6240 break;
6241 #endif
6242 case TARGET_NR_rt_sigreturn:
6243 /* NOTE: ret is eax, so not transcoding must be done */
6244 ret = do_rt_sigreturn(cpu_env);
6245 break;
6246 case TARGET_NR_sethostname:
6247 if (!(p = lock_user_string(arg1)))
6248 goto efault;
6249 ret = get_errno(sethostname(p, arg2));
6250 unlock_user(p, arg1, 0);
6251 break;
6252 case TARGET_NR_setrlimit:
6253 {
6254 int resource = target_to_host_resource(arg1);
6255 struct target_rlimit *target_rlim;
6256 struct rlimit rlim;
6257 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6258 goto efault;
6259 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6260 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
6261 unlock_user_struct(target_rlim, arg2, 0);
6262 ret = get_errno(setrlimit(resource, &rlim));
6263 }
6264 break;
6265 case TARGET_NR_getrlimit:
6266 {
6267 int resource = target_to_host_resource(arg1);
6268 struct target_rlimit *target_rlim;
6269 struct rlimit rlim;
6270
6271 ret = get_errno(getrlimit(resource, &rlim));
6272 if (!is_error(ret)) {
6273 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6274 goto efault;
6275 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6276 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
6277 unlock_user_struct(target_rlim, arg2, 1);
6278 }
6279 }
6280 break;
6281 case TARGET_NR_getrusage:
6282 {
6283 struct rusage rusage;
6284 ret = get_errno(getrusage(arg1, &rusage));
6285 if (!is_error(ret)) {
6286 host_to_target_rusage(arg2, &rusage);
6287 }
6288 }
6289 break;
6290 case TARGET_NR_gettimeofday:
6291 {
6292 struct timeval tv;
6293 ret = get_errno(gettimeofday(&tv, NULL));
6294 if (!is_error(ret)) {
6295 if (copy_to_user_timeval(arg1, &tv))
6296 goto efault;
6297 }
6298 }
6299 break;
6300 case TARGET_NR_settimeofday:
6301 {
6302 struct timeval tv;
6303 if (copy_from_user_timeval(&tv, arg1))
6304 goto efault;
6305 ret = get_errno(settimeofday(&tv, NULL));
6306 }
6307 break;
6308 #if defined(TARGET_NR_select)
6309 case TARGET_NR_select:
6310 #if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6311 ret = do_select(arg1, arg2, arg3, arg4, arg5);
6312 #else
6313 {
6314 struct target_sel_arg_struct *sel;
6315 abi_ulong inp, outp, exp, tvp;
6316 long nsel;
6317
6318 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6319 goto efault;
6320 nsel = tswapal(sel->n);
6321 inp = tswapal(sel->inp);
6322 outp = tswapal(sel->outp);
6323 exp = tswapal(sel->exp);
6324 tvp = tswapal(sel->tvp);
6325 unlock_user_struct(sel, arg1, 0);
6326 ret = do_select(nsel, inp, outp, exp, tvp);
6327 }
6328 #endif
6329 break;
6330 #endif
6331 #ifdef TARGET_NR_pselect6
6332 case TARGET_NR_pselect6:
6333 {
6334 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6335 fd_set rfds, wfds, efds;
6336 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6337 struct timespec ts, *ts_ptr;
6338
6339 /*
6340 * The 6th arg is actually two args smashed together,
6341 * so we cannot use the C library.
6342 */
6343 sigset_t set;
6344 struct {
6345 sigset_t *set;
6346 size_t size;
6347 } sig, *sig_ptr;
6348
6349 abi_ulong arg_sigset, arg_sigsize, *arg7;
6350 target_sigset_t *target_sigset;
6351
6352 n = arg1;
6353 rfd_addr = arg2;
6354 wfd_addr = arg3;
6355 efd_addr = arg4;
6356 ts_addr = arg5;
6357
6358 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
6359 if (ret) {
6360 goto fail;
6361 }
6362 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
6363 if (ret) {
6364 goto fail;
6365 }
6366 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
6367 if (ret) {
6368 goto fail;
6369 }
6370
6371 /*
6372 * This takes a timespec, and not a timeval, so we cannot
6373 * use the do_select() helper ...
6374 */
6375 if (ts_addr) {
6376 if (target_to_host_timespec(&ts, ts_addr)) {
6377 goto efault;
6378 }
6379 ts_ptr = &ts;
6380 } else {
6381 ts_ptr = NULL;
6382 }
6383
6384 /* Extract the two packed args for the sigset */
6385 if (arg6) {
6386 sig_ptr = &sig;
6387 sig.size = _NSIG / 8;
6388
6389 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
6390 if (!arg7) {
6391 goto efault;
6392 }
6393 arg_sigset = tswapal(arg7[0]);
6394 arg_sigsize = tswapal(arg7[1]);
6395 unlock_user(arg7, arg6, 0);
6396
6397 if (arg_sigset) {
6398 sig.set = &set;
6399 if (arg_sigsize != sizeof(*target_sigset)) {
6400 /* Like the kernel, we enforce correct size sigsets */
6401 ret = -TARGET_EINVAL;
6402 goto fail;
6403 }
6404 target_sigset = lock_user(VERIFY_READ, arg_sigset,
6405 sizeof(*target_sigset), 1);
6406 if (!target_sigset) {
6407 goto efault;
6408 }
6409 target_to_host_sigset(&set, target_sigset);
6410 unlock_user(target_sigset, arg_sigset, 0);
6411 } else {
6412 sig.set = NULL;
6413 }
6414 } else {
6415 sig_ptr = NULL;
6416 }
6417
6418 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
6419 ts_ptr, sig_ptr));
6420
6421 if (!is_error(ret)) {
6422 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
6423 goto efault;
6424 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
6425 goto efault;
6426 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
6427 goto efault;
6428
6429 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
6430 goto efault;
6431 }
6432 }
6433 break;
6434 #endif
6435 case TARGET_NR_symlink:
6436 {
6437 void *p2;
6438 p = lock_user_string(arg1);
6439 p2 = lock_user_string(arg2);
6440 if (!p || !p2)
6441 ret = -TARGET_EFAULT;
6442 else
6443 ret = get_errno(symlink(p, p2));
6444 unlock_user(p2, arg2, 0);
6445 unlock_user(p, arg1, 0);
6446 }
6447 break;
6448 #if defined(TARGET_NR_symlinkat)
6449 case TARGET_NR_symlinkat:
6450 {
6451 void *p2;
6452 p = lock_user_string(arg1);
6453 p2 = lock_user_string(arg3);
6454 if (!p || !p2)
6455 ret = -TARGET_EFAULT;
6456 else
6457 ret = get_errno(symlinkat(p, arg2, p2));
6458 unlock_user(p2, arg3, 0);
6459 unlock_user(p, arg1, 0);
6460 }
6461 break;
6462 #endif
6463 #ifdef TARGET_NR_oldlstat
6464 case TARGET_NR_oldlstat:
6465 goto unimplemented;
6466 #endif
6467 case TARGET_NR_readlink:
6468 {
6469 void *p2;
6470 p = lock_user_string(arg1);
6471 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
6472 if (!p || !p2) {
6473 ret = -TARGET_EFAULT;
6474 } else if (is_proc_myself((const char *)p, "exe")) {
6475 char real[PATH_MAX], *temp;
6476 temp = realpath(exec_path, real);
6477 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
6478 snprintf((char *)p2, arg3, "%s", real);
6479 } else {
6480 ret = get_errno(readlink(path(p), p2, arg3));
6481 }
6482 unlock_user(p2, arg2, ret);
6483 unlock_user(p, arg1, 0);
6484 }
6485 break;
6486 #if defined(TARGET_NR_readlinkat)
6487 case TARGET_NR_readlinkat:
6488 {
6489 void *p2;
6490 p = lock_user_string(arg2);
6491 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
6492 if (!p || !p2) {
6493 ret = -TARGET_EFAULT;
6494 } else if (is_proc_myself((const char *)p, "exe")) {
6495 char real[PATH_MAX], *temp;
6496 temp = realpath(exec_path, real);
6497 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
6498 snprintf((char *)p2, arg4, "%s", real);
6499 } else {
6500 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
6501 }
6502 unlock_user(p2, arg3, ret);
6503 unlock_user(p, arg2, 0);
6504 }
6505 break;
6506 #endif
6507 #ifdef TARGET_NR_uselib
6508 case TARGET_NR_uselib:
6509 goto unimplemented;
6510 #endif
6511 #ifdef TARGET_NR_swapon
6512 case TARGET_NR_swapon:
6513 if (!(p = lock_user_string(arg1)))
6514 goto efault;
6515 ret = get_errno(swapon(p, arg2));
6516 unlock_user(p, arg1, 0);
6517 break;
6518 #endif
6519 case TARGET_NR_reboot:
6520 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
6521 /* arg4 must be ignored in all other cases */
6522 p = lock_user_string(arg4);
6523 if (!p) {
6524 goto efault;
6525 }
6526 ret = get_errno(reboot(arg1, arg2, arg3, p));
6527 unlock_user(p, arg4, 0);
6528 } else {
6529 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
6530 }
6531 break;
6532 #ifdef TARGET_NR_readdir
6533 case TARGET_NR_readdir:
6534 goto unimplemented;
6535 #endif
6536 #ifdef TARGET_NR_mmap
6537 case TARGET_NR_mmap:
6538 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6539 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
6540 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6541 || defined(TARGET_S390X)
6542 {
6543 abi_ulong *v;
6544 abi_ulong v1, v2, v3, v4, v5, v6;
6545 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6546 goto efault;
6547 v1 = tswapal(v[0]);
6548 v2 = tswapal(v[1]);
6549 v3 = tswapal(v[2]);
6550 v4 = tswapal(v[3]);
6551 v5 = tswapal(v[4]);
6552 v6 = tswapal(v[5]);
6553 unlock_user(v, arg1, 0);
6554 ret = get_errno(target_mmap(v1, v2, v3,
6555 target_to_host_bitmask(v4, mmap_flags_tbl),
6556 v5, v6));
6557 }
6558 #else
6559 ret = get_errno(target_mmap(arg1, arg2, arg3,
6560 target_to_host_bitmask(arg4, mmap_flags_tbl),
6561 arg5,
6562 arg6));
6563 #endif
6564 break;
6565 #endif
6566 #ifdef TARGET_NR_mmap2
6567 case TARGET_NR_mmap2:
6568 #ifndef MMAP_SHIFT
6569 #define MMAP_SHIFT 12
6570 #endif
6571 ret = get_errno(target_mmap(arg1, arg2, arg3,
6572 target_to_host_bitmask(arg4, mmap_flags_tbl),
6573 arg5,
6574 arg6 << MMAP_SHIFT));
6575 break;
6576 #endif
6577 case TARGET_NR_munmap:
6578 ret = get_errno(target_munmap(arg1, arg2));
6579 break;
6580 case TARGET_NR_mprotect:
6581 {
6582 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
6583 /* Special hack to detect libc making the stack executable. */
6584 if ((arg3 & PROT_GROWSDOWN)
6585 && arg1 >= ts->info->stack_limit
6586 && arg1 <= ts->info->start_stack) {
6587 arg3 &= ~PROT_GROWSDOWN;
6588 arg2 = arg2 + arg1 - ts->info->stack_limit;
6589 arg1 = ts->info->stack_limit;
6590 }
6591 }
6592 ret = get_errno(target_mprotect(arg1, arg2, arg3));
6593 break;
6594 #ifdef TARGET_NR_mremap
6595 case TARGET_NR_mremap:
6596 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
6597 break;
6598 #endif
6599 /* ??? msync/mlock/munlock are broken for softmmu. */
6600 #ifdef TARGET_NR_msync
6601 case TARGET_NR_msync:
6602 ret = get_errno(msync(g2h(arg1), arg2, arg3));
6603 break;
6604 #endif
6605 #ifdef TARGET_NR_mlock
6606 case TARGET_NR_mlock:
6607 ret = get_errno(mlock(g2h(arg1), arg2));
6608 break;
6609 #endif
6610 #ifdef TARGET_NR_munlock
6611 case TARGET_NR_munlock:
6612 ret = get_errno(munlock(g2h(arg1), arg2));
6613 break;
6614 #endif
6615 #ifdef TARGET_NR_mlockall
6616 case TARGET_NR_mlockall:
6617 ret = get_errno(mlockall(arg1));
6618 break;
6619 #endif
6620 #ifdef TARGET_NR_munlockall
6621 case TARGET_NR_munlockall:
6622 ret = get_errno(munlockall());
6623 break;
6624 #endif
6625 case TARGET_NR_truncate:
6626 if (!(p = lock_user_string(arg1)))
6627 goto efault;
6628 ret = get_errno(truncate(p, arg2));
6629 unlock_user(p, arg1, 0);
6630 break;
6631 case TARGET_NR_ftruncate:
6632 ret = get_errno(ftruncate(arg1, arg2));
6633 break;
6634 case TARGET_NR_fchmod:
6635 ret = get_errno(fchmod(arg1, arg2));
6636 break;
6637 #if defined(TARGET_NR_fchmodat)
6638 case TARGET_NR_fchmodat:
6639 if (!(p = lock_user_string(arg2)))
6640 goto efault;
6641 ret = get_errno(fchmodat(arg1, p, arg3, 0));
6642 unlock_user(p, arg2, 0);
6643 break;
6644 #endif
6645 case TARGET_NR_getpriority:
6646 /* Note that negative values are valid for getpriority, so we must
6647 differentiate based on errno settings. */
6648 errno = 0;
6649 ret = getpriority(arg1, arg2);
6650 if (ret == -1 && errno != 0) {
6651 ret = -host_to_target_errno(errno);
6652 break;
6653 }
6654 #ifdef TARGET_ALPHA
6655 /* Return value is the unbiased priority. Signal no error. */
6656 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
6657 #else
6658 /* Return value is a biased priority to avoid negative numbers. */
6659 ret = 20 - ret;
6660 #endif
6661 break;
6662 case TARGET_NR_setpriority:
6663 ret = get_errno(setpriority(arg1, arg2, arg3));
6664 break;
6665 #ifdef TARGET_NR_profil
6666 case TARGET_NR_profil:
6667 goto unimplemented;
6668 #endif
6669 case TARGET_NR_statfs:
6670 if (!(p = lock_user_string(arg1)))
6671 goto efault;
6672 ret = get_errno(statfs(path(p), &stfs));
6673 unlock_user(p, arg1, 0);
6674 convert_statfs:
6675 if (!is_error(ret)) {
6676 struct target_statfs *target_stfs;
6677
6678 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6679 goto efault;
6680 __put_user(stfs.f_type, &target_stfs->f_type);
6681 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6682 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6683 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6684 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6685 __put_user(stfs.f_files, &target_stfs->f_files);
6686 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6687 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6688 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6689 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
6690 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6691 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
6692 unlock_user_struct(target_stfs, arg2, 1);
6693 }
6694 break;
6695 case TARGET_NR_fstatfs:
6696 ret = get_errno(fstatfs(arg1, &stfs));
6697 goto convert_statfs;
6698 #ifdef TARGET_NR_statfs64
6699 case TARGET_NR_statfs64:
6700 if (!(p = lock_user_string(arg1)))
6701 goto efault;
6702 ret = get_errno(statfs(path(p), &stfs));
6703 unlock_user(p, arg1, 0);
6704 convert_statfs64:
6705 if (!is_error(ret)) {
6706 struct target_statfs64 *target_stfs;
6707
6708 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6709 goto efault;
6710 __put_user(stfs.f_type, &target_stfs->f_type);
6711 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6712 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6713 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6714 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6715 __put_user(stfs.f_files, &target_stfs->f_files);
6716 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6717 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6718 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6719 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
6720 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6721 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
6722 unlock_user_struct(target_stfs, arg3, 1);
6723 }
6724 break;
6725 case TARGET_NR_fstatfs64:
6726 ret = get_errno(fstatfs(arg1, &stfs));
6727 goto convert_statfs64;
6728 #endif
6729 #ifdef TARGET_NR_ioperm
6730 case TARGET_NR_ioperm:
6731 goto unimplemented;
6732 #endif
6733 #ifdef TARGET_NR_socketcall
6734 case TARGET_NR_socketcall:
6735 ret = do_socketcall(arg1, arg2);
6736 break;
6737 #endif
6738 #ifdef TARGET_NR_accept
6739 case TARGET_NR_accept:
6740 ret = do_accept4(arg1, arg2, arg3, 0);
6741 break;
6742 #endif
6743 #ifdef TARGET_NR_accept4
6744 case TARGET_NR_accept4:
6745 #ifdef CONFIG_ACCEPT4
6746 ret = do_accept4(arg1, arg2, arg3, arg4);
6747 #else
6748 goto unimplemented;
6749 #endif
6750 break;
6751 #endif
6752 #ifdef TARGET_NR_bind
6753 case TARGET_NR_bind:
6754 ret = do_bind(arg1, arg2, arg3);
6755 break;
6756 #endif
6757 #ifdef TARGET_NR_connect
6758 case TARGET_NR_connect:
6759 ret = do_connect(arg1, arg2, arg3);
6760 break;
6761 #endif
6762 #ifdef TARGET_NR_getpeername
6763 case TARGET_NR_getpeername:
6764 ret = do_getpeername(arg1, arg2, arg3);
6765 break;
6766 #endif
6767 #ifdef TARGET_NR_getsockname
6768 case TARGET_NR_getsockname:
6769 ret = do_getsockname(arg1, arg2, arg3);
6770 break;
6771 #endif
6772 #ifdef TARGET_NR_getsockopt
6773 case TARGET_NR_getsockopt:
6774 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6775 break;
6776 #endif
6777 #ifdef TARGET_NR_listen
6778 case TARGET_NR_listen:
6779 ret = get_errno(listen(arg1, arg2));
6780 break;
6781 #endif
6782 #ifdef TARGET_NR_recv
6783 case TARGET_NR_recv:
6784 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
6785 break;
6786 #endif
6787 #ifdef TARGET_NR_recvfrom
6788 case TARGET_NR_recvfrom:
6789 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
6790 break;
6791 #endif
6792 #ifdef TARGET_NR_recvmsg
6793 case TARGET_NR_recvmsg:
6794 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6795 break;
6796 #endif
6797 #ifdef TARGET_NR_send
6798 case TARGET_NR_send:
6799 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
6800 break;
6801 #endif
6802 #ifdef TARGET_NR_sendmsg
6803 case TARGET_NR_sendmsg:
6804 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
6805 break;
6806 #endif
6807 #ifdef TARGET_NR_sendto
6808 case TARGET_NR_sendto:
6809 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
6810 break;
6811 #endif
6812 #ifdef TARGET_NR_shutdown
6813 case TARGET_NR_shutdown:
6814 ret = get_errno(shutdown(arg1, arg2));
6815 break;
6816 #endif
6817 #ifdef TARGET_NR_socket
6818 case TARGET_NR_socket:
6819 ret = do_socket(arg1, arg2, arg3);
6820 break;
6821 #endif
6822 #ifdef TARGET_NR_socketpair
6823 case TARGET_NR_socketpair:
6824 ret = do_socketpair(arg1, arg2, arg3, arg4);
6825 break;
6826 #endif
6827 #ifdef TARGET_NR_setsockopt
6828 case TARGET_NR_setsockopt:
6829 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
6830 break;
6831 #endif
6832
6833 case TARGET_NR_syslog:
6834 if (!(p = lock_user_string(arg2)))
6835 goto efault;
6836 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
6837 unlock_user(p, arg2, 0);
6838 break;
6839
6840 case TARGET_NR_setitimer:
6841 {
6842 struct itimerval value, ovalue, *pvalue;
6843
6844 if (arg2) {
6845 pvalue = &value;
6846 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
6847 || copy_from_user_timeval(&pvalue->it_value,
6848 arg2 + sizeof(struct target_timeval)))
6849 goto efault;
6850 } else {
6851 pvalue = NULL;
6852 }
6853 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
6854 if (!is_error(ret) && arg3) {
6855 if (copy_to_user_timeval(arg3,
6856 &ovalue.it_interval)
6857 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
6858 &ovalue.it_value))
6859 goto efault;
6860 }
6861 }
6862 break;
6863 case TARGET_NR_getitimer:
6864 {
6865 struct itimerval value;
6866
6867 ret = get_errno(getitimer(arg1, &value));
6868 if (!is_error(ret) && arg2) {
6869 if (copy_to_user_timeval(arg2,
6870 &value.it_interval)
6871 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
6872 &value.it_value))
6873 goto efault;
6874 }
6875 }
6876 break;
6877 case TARGET_NR_stat:
6878 if (!(p = lock_user_string(arg1)))
6879 goto efault;
6880 ret = get_errno(stat(path(p), &st));
6881 unlock_user(p, arg1, 0);
6882 goto do_stat;
6883 case TARGET_NR_lstat:
6884 if (!(p = lock_user_string(arg1)))
6885 goto efault;
6886 ret = get_errno(lstat(path(p), &st));
6887 unlock_user(p, arg1, 0);
6888 goto do_stat;
6889 case TARGET_NR_fstat:
6890 {
6891 ret = get_errno(fstat(arg1, &st));
6892 do_stat:
6893 if (!is_error(ret)) {
6894 struct target_stat *target_st;
6895
6896 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
6897 goto efault;
6898 memset(target_st, 0, sizeof(*target_st));
6899 __put_user(st.st_dev, &target_st->st_dev);
6900 __put_user(st.st_ino, &target_st->st_ino);
6901 __put_user(st.st_mode, &target_st->st_mode);
6902 __put_user(st.st_uid, &target_st->st_uid);
6903 __put_user(st.st_gid, &target_st->st_gid);
6904 __put_user(st.st_nlink, &target_st->st_nlink);
6905 __put_user(st.st_rdev, &target_st->st_rdev);
6906 __put_user(st.st_size, &target_st->st_size);
6907 __put_user(st.st_blksize, &target_st->st_blksize);
6908 __put_user(st.st_blocks, &target_st->st_blocks);
6909 __put_user(st.st_atime, &target_st->target_st_atime);
6910 __put_user(st.st_mtime, &target_st->target_st_mtime);
6911 __put_user(st.st_ctime, &target_st->target_st_ctime);
6912 unlock_user_struct(target_st, arg2, 1);
6913 }
6914 }
6915 break;
6916 #ifdef TARGET_NR_olduname
6917 case TARGET_NR_olduname:
6918 goto unimplemented;
6919 #endif
6920 #ifdef TARGET_NR_iopl
6921 case TARGET_NR_iopl:
6922 goto unimplemented;
6923 #endif
6924 case TARGET_NR_vhangup:
6925 ret = get_errno(vhangup());
6926 break;
6927 #ifdef TARGET_NR_idle
6928 case TARGET_NR_idle:
6929 goto unimplemented;
6930 #endif
6931 #ifdef TARGET_NR_syscall
6932 case TARGET_NR_syscall:
6933 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
6934 arg6, arg7, arg8, 0);
6935 break;
6936 #endif
6937 case TARGET_NR_wait4:
6938 {
6939 int status;
6940 abi_long status_ptr = arg2;
6941 struct rusage rusage, *rusage_ptr;
6942 abi_ulong target_rusage = arg4;
6943 if (target_rusage)
6944 rusage_ptr = &rusage;
6945 else
6946 rusage_ptr = NULL;
6947 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6948 if (!is_error(ret)) {
6949 if (status_ptr && ret) {
6950 status = host_to_target_waitstatus(status);
6951 if (put_user_s32(status, status_ptr))
6952 goto efault;
6953 }
6954 if (target_rusage)
6955 host_to_target_rusage(target_rusage, &rusage);
6956 }
6957 }
6958 break;
6959 #ifdef TARGET_NR_swapoff
6960 case TARGET_NR_swapoff:
6961 if (!(p = lock_user_string(arg1)))
6962 goto efault;
6963 ret = get_errno(swapoff(p));
6964 unlock_user(p, arg1, 0);
6965 break;
6966 #endif
6967 case TARGET_NR_sysinfo:
6968 {
6969 struct target_sysinfo *target_value;
6970 struct sysinfo value;
6971 ret = get_errno(sysinfo(&value));
6972 if (!is_error(ret) && arg1)
6973 {
6974 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6975 goto efault;
6976 __put_user(value.uptime, &target_value->uptime);
6977 __put_user(value.loads[0], &target_value->loads[0]);
6978 __put_user(value.loads[1], &target_value->loads[1]);
6979 __put_user(value.loads[2], &target_value->loads[2]);
6980 __put_user(value.totalram, &target_value->totalram);
6981 __put_user(value.freeram, &target_value->freeram);
6982 __put_user(value.sharedram, &target_value->sharedram);
6983 __put_user(value.bufferram, &target_value->bufferram);
6984 __put_user(value.totalswap, &target_value->totalswap);
6985 __put_user(value.freeswap, &target_value->freeswap);
6986 __put_user(value.procs, &target_value->procs);
6987 __put_user(value.totalhigh, &target_value->totalhigh);
6988 __put_user(value.freehigh, &target_value->freehigh);
6989 __put_user(value.mem_unit, &target_value->mem_unit);
6990 unlock_user_struct(target_value, arg1, 1);
6991 }
6992 }
6993 break;
6994 #ifdef TARGET_NR_ipc
6995 case TARGET_NR_ipc:
6996 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6997 break;
6998 #endif
6999 #ifdef TARGET_NR_semget
7000 case TARGET_NR_semget:
7001 ret = get_errno(semget(arg1, arg2, arg3));
7002 break;
7003 #endif
7004 #ifdef TARGET_NR_semop
7005 case TARGET_NR_semop:
7006 ret = do_semop(arg1, arg2, arg3);
7007 break;
7008 #endif
7009 #ifdef TARGET_NR_semctl
7010 case TARGET_NR_semctl:
7011 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
7012 break;
7013 #endif
7014 #ifdef TARGET_NR_msgctl
7015 case TARGET_NR_msgctl:
7016 ret = do_msgctl(arg1, arg2, arg3);
7017 break;
7018 #endif
7019 #ifdef TARGET_NR_msgget
7020 case TARGET_NR_msgget:
7021 ret = get_errno(msgget(arg1, arg2));
7022 break;
7023 #endif
7024 #ifdef TARGET_NR_msgrcv
7025 case TARGET_NR_msgrcv:
7026 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
7027 break;
7028 #endif
7029 #ifdef TARGET_NR_msgsnd
7030 case TARGET_NR_msgsnd:
7031 ret = do_msgsnd(arg1, arg2, arg3, arg4);
7032 break;
7033 #endif
7034 #ifdef TARGET_NR_shmget
7035 case TARGET_NR_shmget:
7036 ret = get_errno(shmget(arg1, arg2, arg3));
7037 break;
7038 #endif
7039 #ifdef TARGET_NR_shmctl
7040 case TARGET_NR_shmctl:
7041 ret = do_shmctl(arg1, arg2, arg3);
7042 break;
7043 #endif
7044 #ifdef TARGET_NR_shmat
7045 case TARGET_NR_shmat:
7046 ret = do_shmat(arg1, arg2, arg3);
7047 break;
7048 #endif
7049 #ifdef TARGET_NR_shmdt
7050 case TARGET_NR_shmdt:
7051 ret = do_shmdt(arg1);
7052 break;
7053 #endif
7054 case TARGET_NR_fsync:
7055 ret = get_errno(fsync(arg1));
7056 break;
7057 case TARGET_NR_clone:
7058 /* Linux manages to have three different orderings for its
7059 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7060 * match the kernel's CONFIG_CLONE_* settings.
7061 * Microblaze is further special in that it uses a sixth
7062 * implicit argument to clone for the TLS pointer.
7063 */
7064 #if defined(TARGET_MICROBLAZE)
7065 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
7066 #elif defined(TARGET_CLONE_BACKWARDS)
7067 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
7068 #elif defined(TARGET_CLONE_BACKWARDS2)
7069 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
7070 #else
7071 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
7072 #endif
7073 break;
7074 #ifdef __NR_exit_group
7075 /* new thread calls */
7076 case TARGET_NR_exit_group:
7077 #ifdef TARGET_GPROF
7078 _mcleanup();
7079 #endif
7080 gdb_exit(cpu_env, arg1);
7081 ret = get_errno(exit_group(arg1));
7082 break;
7083 #endif
7084 case TARGET_NR_setdomainname:
7085 if (!(p = lock_user_string(arg1)))
7086 goto efault;
7087 ret = get_errno(setdomainname(p, arg2));
7088 unlock_user(p, arg1, 0);
7089 break;
7090 case TARGET_NR_uname:
7091 /* no need to transcode because we use the linux syscall */
7092 {
7093 struct new_utsname * buf;
7094
7095 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
7096 goto efault;
7097 ret = get_errno(sys_uname(buf));
7098 if (!is_error(ret)) {
7099 /* Overrite the native machine name with whatever is being
7100 emulated. */
7101 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
7102 /* Allow the user to override the reported release. */
7103 if (qemu_uname_release && *qemu_uname_release)
7104 strcpy (buf->release, qemu_uname_release);
7105 }
7106 unlock_user_struct(buf, arg1, 1);
7107 }
7108 break;
7109 #ifdef TARGET_I386
7110 case TARGET_NR_modify_ldt:
7111 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
7112 break;
7113 #if !defined(TARGET_X86_64)
7114 case TARGET_NR_vm86old:
7115 goto unimplemented;
7116 case TARGET_NR_vm86:
7117 ret = do_vm86(cpu_env, arg1, arg2);
7118 break;
7119 #endif
7120 #endif
7121 case TARGET_NR_adjtimex:
7122 goto unimplemented;
7123 #ifdef TARGET_NR_create_module
7124 case TARGET_NR_create_module:
7125 #endif
7126 case TARGET_NR_init_module:
7127 case TARGET_NR_delete_module:
7128 #ifdef TARGET_NR_get_kernel_syms
7129 case TARGET_NR_get_kernel_syms:
7130 #endif
7131 goto unimplemented;
7132 case TARGET_NR_quotactl:
7133 goto unimplemented;
7134 case TARGET_NR_getpgid:
7135 ret = get_errno(getpgid(arg1));
7136 break;
7137 case TARGET_NR_fchdir:
7138 ret = get_errno(fchdir(arg1));
7139 break;
7140 #ifdef TARGET_NR_bdflush /* not on x86_64 */
7141 case TARGET_NR_bdflush:
7142 goto unimplemented;
7143 #endif
7144 #ifdef TARGET_NR_sysfs
7145 case TARGET_NR_sysfs:
7146 goto unimplemented;
7147 #endif
7148 case TARGET_NR_personality:
7149 ret = get_errno(personality(arg1));
7150 break;
7151 #ifdef TARGET_NR_afs_syscall
7152 case TARGET_NR_afs_syscall:
7153 goto unimplemented;
7154 #endif
7155 #ifdef TARGET_NR__llseek /* Not on alpha */
7156 case TARGET_NR__llseek:
7157 {
7158 int64_t res;
7159 #if !defined(__NR_llseek)
7160 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
7161 if (res == -1) {
7162 ret = get_errno(res);
7163 } else {
7164 ret = 0;
7165 }
7166 #else
7167 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
7168 #endif
7169 if ((ret == 0) && put_user_s64(res, arg4)) {
7170 goto efault;
7171 }
7172 }
7173 break;
7174 #endif
7175 case TARGET_NR_getdents:
7176 #ifdef __NR_getdents
7177 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
7178 {
7179 struct target_dirent *target_dirp;
7180 struct linux_dirent *dirp;
7181 abi_long count = arg3;
7182
7183 dirp = malloc(count);
7184 if (!dirp) {
7185 ret = -TARGET_ENOMEM;
7186 goto fail;
7187 }
7188
7189 ret = get_errno(sys_getdents(arg1, dirp, count));
7190 if (!is_error(ret)) {
7191 struct linux_dirent *de;
7192 struct target_dirent *tde;
7193 int len = ret;
7194 int reclen, treclen;
7195 int count1, tnamelen;
7196
7197 count1 = 0;
7198 de = dirp;
7199 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7200 goto efault;
7201 tde = target_dirp;
7202 while (len > 0) {
7203 reclen = de->d_reclen;
7204 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
7205 assert(tnamelen >= 0);
7206 treclen = tnamelen + offsetof(struct target_dirent, d_name);
7207 assert(count1 + treclen <= count);
7208 tde->d_reclen = tswap16(treclen);
7209 tde->d_ino = tswapal(de->d_ino);
7210 tde->d_off = tswapal(de->d_off);
7211 memcpy(tde->d_name, de->d_name, tnamelen);
7212 de = (struct linux_dirent *)((char *)de + reclen);
7213 len -= reclen;
7214 tde = (struct target_dirent *)((char *)tde + treclen);
7215 count1 += treclen;
7216 }
7217 ret = count1;
7218 unlock_user(target_dirp, arg2, ret);
7219 }
7220 free(dirp);
7221 }
7222 #else
7223 {
7224 struct linux_dirent *dirp;
7225 abi_long count = arg3;
7226
7227 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7228 goto efault;
7229 ret = get_errno(sys_getdents(arg1, dirp, count));
7230 if (!is_error(ret)) {
7231 struct linux_dirent *de;
7232 int len = ret;
7233 int reclen;
7234 de = dirp;
7235 while (len > 0) {
7236 reclen = de->d_reclen;
7237 if (reclen > len)
7238 break;
7239 de->d_reclen = tswap16(reclen);
7240 tswapls(&de->d_ino);
7241 tswapls(&de->d_off);
7242 de = (struct linux_dirent *)((char *)de + reclen);
7243 len -= reclen;
7244 }
7245 }
7246 unlock_user(dirp, arg2, ret);
7247 }
7248 #endif
7249 #else
7250 /* Implement getdents in terms of getdents64 */
7251 {
7252 struct linux_dirent64 *dirp;
7253 abi_long count = arg3;
7254
7255 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
7256 if (!dirp) {
7257 goto efault;
7258 }
7259 ret = get_errno(sys_getdents64(arg1, dirp, count));
7260 if (!is_error(ret)) {
7261 /* Convert the dirent64 structs to target dirent. We do this
7262 * in-place, since we can guarantee that a target_dirent is no
7263 * larger than a dirent64; however this means we have to be
7264 * careful to read everything before writing in the new format.
7265 */
7266 struct linux_dirent64 *de;
7267 struct target_dirent *tde;
7268 int len = ret;
7269 int tlen = 0;
7270
7271 de = dirp;
7272 tde = (struct target_dirent *)dirp;
7273 while (len > 0) {
7274 int namelen, treclen;
7275 int reclen = de->d_reclen;
7276 uint64_t ino = de->d_ino;
7277 int64_t off = de->d_off;
7278 uint8_t type = de->d_type;
7279
7280 namelen = strlen(de->d_name);
7281 treclen = offsetof(struct target_dirent, d_name)
7282 + namelen + 2;
7283 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
7284
7285 memmove(tde->d_name, de->d_name, namelen + 1);
7286 tde->d_ino = tswapal(ino);
7287 tde->d_off = tswapal(off);
7288 tde->d_reclen = tswap16(treclen);
7289 /* The target_dirent type is in what was formerly a padding
7290 * byte at the end of the structure:
7291 */
7292 *(((char *)tde) + treclen - 1) = type;
7293
7294 de = (struct linux_dirent64 *)((char *)de + reclen);
7295 tde = (struct target_dirent *)((char *)tde + treclen);
7296 len -= reclen;
7297 tlen += treclen;
7298 }
7299 ret = tlen;
7300 }
7301 unlock_user(dirp, arg2, ret);
7302 }
7303 #endif
7304 break;
7305 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
7306 case TARGET_NR_getdents64:
7307 {
7308 struct linux_dirent64 *dirp;
7309 abi_long count = arg3;
7310 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7311 goto efault;
7312 ret = get_errno(sys_getdents64(arg1, dirp, count));
7313 if (!is_error(ret)) {
7314 struct linux_dirent64 *de;
7315 int len = ret;
7316 int reclen;
7317 de = dirp;
7318 while (len > 0) {
7319 reclen = de->d_reclen;
7320 if (reclen > len)
7321 break;
7322 de->d_reclen = tswap16(reclen);
7323 tswap64s((uint64_t *)&de->d_ino);
7324 tswap64s((uint64_t *)&de->d_off);
7325 de = (struct linux_dirent64 *)((char *)de + reclen);
7326 len -= reclen;
7327 }
7328 }
7329 unlock_user(dirp, arg2, ret);
7330 }
7331 break;
7332 #endif /* TARGET_NR_getdents64 */
7333 #if defined(TARGET_NR__newselect)
7334 case TARGET_NR__newselect:
7335 ret = do_select(arg1, arg2, arg3, arg4, arg5);
7336 break;
7337 #endif
7338 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7339 # ifdef TARGET_NR_poll
7340 case TARGET_NR_poll:
7341 # endif
7342 # ifdef TARGET_NR_ppoll
7343 case TARGET_NR_ppoll:
7344 # endif
7345 {
7346 struct target_pollfd *target_pfd;
7347 unsigned int nfds = arg2;
7348 int timeout = arg3;
7349 struct pollfd *pfd;
7350 unsigned int i;
7351
7352 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
7353 if (!target_pfd)
7354 goto efault;
7355
7356 pfd = alloca(sizeof(struct pollfd) * nfds);
7357 for(i = 0; i < nfds; i++) {
7358 pfd[i].fd = tswap32(target_pfd[i].fd);
7359 pfd[i].events = tswap16(target_pfd[i].events);
7360 }
7361
7362 # ifdef TARGET_NR_ppoll
7363 if (num == TARGET_NR_ppoll) {
7364 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
7365 target_sigset_t *target_set;
7366 sigset_t _set, *set = &_set;
7367
7368 if (arg3) {
7369 if (target_to_host_timespec(timeout_ts, arg3)) {
7370 unlock_user(target_pfd, arg1, 0);
7371 goto efault;
7372 }
7373 } else {
7374 timeout_ts = NULL;
7375 }
7376
7377 if (arg4) {
7378 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
7379 if (!target_set) {
7380 unlock_user(target_pfd, arg1, 0);
7381 goto efault;
7382 }
7383 target_to_host_sigset(set, target_set);
7384 } else {
7385 set = NULL;
7386 }
7387
7388 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
7389
7390 if (!is_error(ret) && arg3) {
7391 host_to_target_timespec(arg3, timeout_ts);
7392 }
7393 if (arg4) {
7394 unlock_user(target_set, arg4, 0);
7395 }
7396 } else
7397 # endif
7398 ret = get_errno(poll(pfd, nfds, timeout));
7399
7400 if (!is_error(ret)) {
7401 for(i = 0; i < nfds; i++) {
7402 target_pfd[i].revents = tswap16(pfd[i].revents);
7403 }
7404 }
7405 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
7406 }
7407 break;
7408 #endif
7409 case TARGET_NR_flock:
7410 /* NOTE: the flock constant seems to be the same for every
7411 Linux platform */
7412 ret = get_errno(flock(arg1, arg2));
7413 break;
7414 case TARGET_NR_readv:
7415 {
7416 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
7417 if (vec != NULL) {
7418 ret = get_errno(readv(arg1, vec, arg3));
7419 unlock_iovec(vec, arg2, arg3, 1);
7420 } else {
7421 ret = -host_to_target_errno(errno);
7422 }
7423 }
7424 break;
7425 case TARGET_NR_writev:
7426 {
7427 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
7428 if (vec != NULL) {
7429 ret = get_errno(writev(arg1, vec, arg3));
7430 unlock_iovec(vec, arg2, arg3, 0);
7431 } else {
7432 ret = -host_to_target_errno(errno);
7433 }
7434 }
7435 break;
7436 case TARGET_NR_getsid:
7437 ret = get_errno(getsid(arg1));
7438 break;
7439 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
7440 case TARGET_NR_fdatasync:
7441 ret = get_errno(fdatasync(arg1));
7442 break;
7443 #endif
7444 case TARGET_NR__sysctl:
7445 /* We don't implement this, but ENOTDIR is always a safe
7446 return value. */
7447 ret = -TARGET_ENOTDIR;
7448 break;
7449 case TARGET_NR_sched_getaffinity:
7450 {
7451 unsigned int mask_size;
7452 unsigned long *mask;
7453
7454 /*
7455 * sched_getaffinity needs multiples of ulong, so need to take
7456 * care of mismatches between target ulong and host ulong sizes.
7457 */
7458 if (arg2 & (sizeof(abi_ulong) - 1)) {
7459 ret = -TARGET_EINVAL;
7460 break;
7461 }
7462 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7463
7464 mask = alloca(mask_size);
7465 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
7466
7467 if (!is_error(ret)) {
7468 if (copy_to_user(arg3, mask, ret)) {
7469 goto efault;
7470 }
7471 }
7472 }
7473 break;
7474 case TARGET_NR_sched_setaffinity:
7475 {
7476 unsigned int mask_size;
7477 unsigned long *mask;
7478
7479 /*
7480 * sched_setaffinity needs multiples of ulong, so need to take
7481 * care of mismatches between target ulong and host ulong sizes.
7482 */
7483 if (arg2 & (sizeof(abi_ulong) - 1)) {
7484 ret = -TARGET_EINVAL;
7485 break;
7486 }
7487 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7488
7489 mask = alloca(mask_size);
7490 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
7491 goto efault;
7492 }
7493 memcpy(mask, p, arg2);
7494 unlock_user_struct(p, arg2, 0);
7495
7496 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
7497 }
7498 break;
7499 case TARGET_NR_sched_setparam:
7500 {
7501 struct sched_param *target_schp;
7502 struct sched_param schp;
7503
7504 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
7505 goto efault;
7506 schp.sched_priority = tswap32(target_schp->sched_priority);
7507 unlock_user_struct(target_schp, arg2, 0);
7508 ret = get_errno(sched_setparam(arg1, &schp));
7509 }
7510 break;
7511 case TARGET_NR_sched_getparam:
7512 {
7513 struct sched_param *target_schp;
7514 struct sched_param schp;
7515 ret = get_errno(sched_getparam(arg1, &schp));
7516 if (!is_error(ret)) {
7517 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
7518 goto efault;
7519 target_schp->sched_priority = tswap32(schp.sched_priority);
7520 unlock_user_struct(target_schp, arg2, 1);
7521 }
7522 }
7523 break;
7524 case TARGET_NR_sched_setscheduler:
7525 {
7526 struct sched_param *target_schp;
7527 struct sched_param schp;
7528 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
7529 goto efault;
7530 schp.sched_priority = tswap32(target_schp->sched_priority);
7531 unlock_user_struct(target_schp, arg3, 0);
7532 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
7533 }
7534 break;
7535 case TARGET_NR_sched_getscheduler:
7536 ret = get_errno(sched_getscheduler(arg1));
7537 break;
7538 case TARGET_NR_sched_yield:
7539 ret = get_errno(sched_yield());
7540 break;
7541 case TARGET_NR_sched_get_priority_max:
7542 ret = get_errno(sched_get_priority_max(arg1));
7543 break;
7544 case TARGET_NR_sched_get_priority_min:
7545 ret = get_errno(sched_get_priority_min(arg1));
7546 break;
7547 case TARGET_NR_sched_rr_get_interval:
7548 {
7549 struct timespec ts;
7550 ret = get_errno(sched_rr_get_interval(arg1, &ts));
7551 if (!is_error(ret)) {
7552 host_to_target_timespec(arg2, &ts);
7553 }
7554 }
7555 break;
7556 case TARGET_NR_nanosleep:
7557 {
7558 struct timespec req, rem;
7559 target_to_host_timespec(&req, arg1);
7560 ret = get_errno(nanosleep(&req, &rem));
7561 if (is_error(ret) && arg2) {
7562 host_to_target_timespec(arg2, &rem);
7563 }
7564 }
7565 break;
7566 #ifdef TARGET_NR_query_module
7567 case TARGET_NR_query_module:
7568 goto unimplemented;
7569 #endif
7570 #ifdef TARGET_NR_nfsservctl
7571 case TARGET_NR_nfsservctl:
7572 goto unimplemented;
7573 #endif
7574 case TARGET_NR_prctl:
7575 switch (arg1) {
7576 case PR_GET_PDEATHSIG:
7577 {
7578 int deathsig;
7579 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
7580 if (!is_error(ret) && arg2
7581 && put_user_ual(deathsig, arg2)) {
7582 goto efault;
7583 }
7584 break;
7585 }
7586 #ifdef PR_GET_NAME
7587 case PR_GET_NAME:
7588 {
7589 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
7590 if (!name) {
7591 goto efault;
7592 }
7593 ret = get_errno(prctl(arg1, (unsigned long)name,
7594 arg3, arg4, arg5));
7595 unlock_user(name, arg2, 16);
7596 break;
7597 }
7598 case PR_SET_NAME:
7599 {
7600 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
7601 if (!name) {
7602 goto efault;
7603 }
7604 ret = get_errno(prctl(arg1, (unsigned long)name,
7605 arg3, arg4, arg5));
7606 unlock_user(name, arg2, 0);
7607 break;
7608 }
7609 #endif
7610 default:
7611 /* Most prctl options have no pointer arguments */
7612 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
7613 break;
7614 }
7615 break;
7616 #ifdef TARGET_NR_arch_prctl
7617 case TARGET_NR_arch_prctl:
7618 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
7619 ret = do_arch_prctl(cpu_env, arg1, arg2);
7620 break;
7621 #else
7622 goto unimplemented;
7623 #endif
7624 #endif
7625 #ifdef TARGET_NR_pread64
7626 case TARGET_NR_pread64:
7627 if (regpairs_aligned(cpu_env)) {
7628 arg4 = arg5;
7629 arg5 = arg6;
7630 }
7631 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7632 goto efault;
7633 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
7634 unlock_user(p, arg2, ret);
7635 break;
7636 case TARGET_NR_pwrite64:
7637 if (regpairs_aligned(cpu_env)) {
7638 arg4 = arg5;
7639 arg5 = arg6;
7640 }
7641 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7642 goto efault;
7643 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
7644 unlock_user(p, arg2, 0);
7645 break;
7646 #endif
7647 case TARGET_NR_getcwd:
7648 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
7649 goto efault;
7650 ret = get_errno(sys_getcwd1(p, arg2));
7651 unlock_user(p, arg1, ret);
7652 break;
7653 case TARGET_NR_capget:
7654 goto unimplemented;
7655 case TARGET_NR_capset:
7656 goto unimplemented;
7657 case TARGET_NR_sigaltstack:
7658 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
7659 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
7660 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
7661 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
7662 break;
7663 #else
7664 goto unimplemented;
7665 #endif
7666
7667 #ifdef CONFIG_SENDFILE
7668 case TARGET_NR_sendfile:
7669 {
7670 off_t *offp = NULL;
7671 off_t off;
7672 if (arg3) {
7673 ret = get_user_sal(off, arg3);
7674 if (is_error(ret)) {
7675 break;
7676 }
7677 offp = &off;
7678 }
7679 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7680 if (!is_error(ret) && arg3) {
7681 abi_long ret2 = put_user_sal(off, arg3);
7682 if (is_error(ret2)) {
7683 ret = ret2;
7684 }
7685 }
7686 break;
7687 }
7688 #ifdef TARGET_NR_sendfile64
7689 case TARGET_NR_sendfile64:
7690 {
7691 off_t *offp = NULL;
7692 off_t off;
7693 if (arg3) {
7694 ret = get_user_s64(off, arg3);
7695 if (is_error(ret)) {
7696 break;
7697 }
7698 offp = &off;
7699 }
7700 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7701 if (!is_error(ret) && arg3) {
7702 abi_long ret2 = put_user_s64(off, arg3);
7703 if (is_error(ret2)) {
7704 ret = ret2;
7705 }
7706 }
7707 break;
7708 }
7709 #endif
7710 #else
7711 case TARGET_NR_sendfile:
7712 #ifdef TARGET_NR_sendfile64
7713 case TARGET_NR_sendfile64:
7714 #endif
7715 goto unimplemented;
7716 #endif
7717
7718 #ifdef TARGET_NR_getpmsg
7719 case TARGET_NR_getpmsg:
7720 goto unimplemented;
7721 #endif
7722 #ifdef TARGET_NR_putpmsg
7723 case TARGET_NR_putpmsg:
7724 goto unimplemented;
7725 #endif
7726 #ifdef TARGET_NR_vfork
7727 case TARGET_NR_vfork:
7728 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
7729 0, 0, 0, 0));
7730 break;
7731 #endif
7732 #ifdef TARGET_NR_ugetrlimit
7733 case TARGET_NR_ugetrlimit:
7734 {
7735 struct rlimit rlim;
7736 int resource = target_to_host_resource(arg1);
7737 ret = get_errno(getrlimit(resource, &rlim));
7738 if (!is_error(ret)) {
7739 struct target_rlimit *target_rlim;
7740 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7741 goto efault;
7742 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7743 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
7744 unlock_user_struct(target_rlim, arg2, 1);
7745 }
7746 break;
7747 }
7748 #endif
7749 #ifdef TARGET_NR_truncate64
7750 case TARGET_NR_truncate64:
7751 if (!(p = lock_user_string(arg1)))
7752 goto efault;
7753 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
7754 unlock_user(p, arg1, 0);
7755 break;
7756 #endif
7757 #ifdef TARGET_NR_ftruncate64
7758 case TARGET_NR_ftruncate64:
7759 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
7760 break;
7761 #endif
7762 #ifdef TARGET_NR_stat64
7763 case TARGET_NR_stat64:
7764 if (!(p = lock_user_string(arg1)))
7765 goto efault;
7766 ret = get_errno(stat(path(p), &st));
7767 unlock_user(p, arg1, 0);
7768 if (!is_error(ret))
7769 ret = host_to_target_stat64(cpu_env, arg2, &st);
7770 break;
7771 #endif
7772 #ifdef TARGET_NR_lstat64
7773 case TARGET_NR_lstat64:
7774 if (!(p = lock_user_string(arg1)))
7775 goto efault;
7776 ret = get_errno(lstat(path(p), &st));
7777 unlock_user(p, arg1, 0);
7778 if (!is_error(ret))
7779 ret = host_to_target_stat64(cpu_env, arg2, &st);
7780 break;
7781 #endif
7782 #ifdef TARGET_NR_fstat64
7783 case TARGET_NR_fstat64:
7784 ret = get_errno(fstat(arg1, &st));
7785 if (!is_error(ret))
7786 ret = host_to_target_stat64(cpu_env, arg2, &st);
7787 break;
7788 #endif
7789 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
7790 #ifdef TARGET_NR_fstatat64
7791 case TARGET_NR_fstatat64:
7792 #endif
7793 #ifdef TARGET_NR_newfstatat
7794 case TARGET_NR_newfstatat:
7795 #endif
7796 if (!(p = lock_user_string(arg2)))
7797 goto efault;
7798 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
7799 if (!is_error(ret))
7800 ret = host_to_target_stat64(cpu_env, arg3, &st);
7801 break;
7802 #endif
7803 case TARGET_NR_lchown:
7804 if (!(p = lock_user_string(arg1)))
7805 goto efault;
7806 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
7807 unlock_user(p, arg1, 0);
7808 break;
7809 #ifdef TARGET_NR_getuid
7810 case TARGET_NR_getuid:
7811 ret = get_errno(high2lowuid(getuid()));
7812 break;
7813 #endif
7814 #ifdef TARGET_NR_getgid
7815 case TARGET_NR_getgid:
7816 ret = get_errno(high2lowgid(getgid()));
7817 break;
7818 #endif
7819 #ifdef TARGET_NR_geteuid
7820 case TARGET_NR_geteuid:
7821 ret = get_errno(high2lowuid(geteuid()));
7822 break;
7823 #endif
7824 #ifdef TARGET_NR_getegid
7825 case TARGET_NR_getegid:
7826 ret = get_errno(high2lowgid(getegid()));
7827 break;
7828 #endif
7829 case TARGET_NR_setreuid:
7830 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
7831 break;
7832 case TARGET_NR_setregid:
7833 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
7834 break;
7835 case TARGET_NR_getgroups:
7836 {
7837 int gidsetsize = arg1;
7838 target_id *target_grouplist;
7839 gid_t *grouplist;
7840 int i;
7841
7842 grouplist = alloca(gidsetsize * sizeof(gid_t));
7843 ret = get_errno(getgroups(gidsetsize, grouplist));
7844 if (gidsetsize == 0)
7845 break;
7846 if (!is_error(ret)) {
7847 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
7848 if (!target_grouplist)
7849 goto efault;
7850 for(i = 0;i < ret; i++)
7851 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
7852 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
7853 }
7854 }
7855 break;
7856 case TARGET_NR_setgroups:
7857 {
7858 int gidsetsize = arg1;
7859 target_id *target_grouplist;
7860 gid_t *grouplist = NULL;
7861 int i;
7862 if (gidsetsize) {
7863 grouplist = alloca(gidsetsize * sizeof(gid_t));
7864 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
7865 if (!target_grouplist) {
7866 ret = -TARGET_EFAULT;
7867 goto fail;
7868 }
7869 for (i = 0; i < gidsetsize; i++) {
7870 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
7871 }
7872 unlock_user(target_grouplist, arg2, 0);
7873 }
7874 ret = get_errno(setgroups(gidsetsize, grouplist));
7875 }
7876 break;
7877 case TARGET_NR_fchown:
7878 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
7879 break;
7880 #if defined(TARGET_NR_fchownat)
7881 case TARGET_NR_fchownat:
7882 if (!(p = lock_user_string(arg2)))
7883 goto efault;
7884 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
7885 low2highgid(arg4), arg5));
7886 unlock_user(p, arg2, 0);
7887 break;
7888 #endif
7889 #ifdef TARGET_NR_setresuid
7890 case TARGET_NR_setresuid:
7891 ret = get_errno(setresuid(low2highuid(arg1),
7892 low2highuid(arg2),
7893 low2highuid(arg3)));
7894 break;
7895 #endif
7896 #ifdef TARGET_NR_getresuid
7897 case TARGET_NR_getresuid:
7898 {
7899 uid_t ruid, euid, suid;
7900 ret = get_errno(getresuid(&ruid, &euid, &suid));
7901 if (!is_error(ret)) {
7902 if (put_user_u16(high2lowuid(ruid), arg1)
7903 || put_user_u16(high2lowuid(euid), arg2)
7904 || put_user_u16(high2lowuid(suid), arg3))
7905 goto efault;
7906 }
7907 }
7908 break;
7909 #endif
7910 #ifdef TARGET_NR_getresgid
7911 case TARGET_NR_setresgid:
7912 ret = get_errno(setresgid(low2highgid(arg1),
7913 low2highgid(arg2),
7914 low2highgid(arg3)));
7915 break;
7916 #endif
7917 #ifdef TARGET_NR_getresgid
7918 case TARGET_NR_getresgid:
7919 {
7920 gid_t rgid, egid, sgid;
7921 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7922 if (!is_error(ret)) {
7923 if (put_user_u16(high2lowgid(rgid), arg1)
7924 || put_user_u16(high2lowgid(egid), arg2)
7925 || put_user_u16(high2lowgid(sgid), arg3))
7926 goto efault;
7927 }
7928 }
7929 break;
7930 #endif
7931 case TARGET_NR_chown:
7932 if (!(p = lock_user_string(arg1)))
7933 goto efault;
7934 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
7935 unlock_user(p, arg1, 0);
7936 break;
7937 case TARGET_NR_setuid:
7938 ret = get_errno(setuid(low2highuid(arg1)));
7939 break;
7940 case TARGET_NR_setgid:
7941 ret = get_errno(setgid(low2highgid(arg1)));
7942 break;
7943 case TARGET_NR_setfsuid:
7944 ret = get_errno(setfsuid(arg1));
7945 break;
7946 case TARGET_NR_setfsgid:
7947 ret = get_errno(setfsgid(arg1));
7948 break;
7949
7950 #ifdef TARGET_NR_lchown32
7951 case TARGET_NR_lchown32:
7952 if (!(p = lock_user_string(arg1)))
7953 goto efault;
7954 ret = get_errno(lchown(p, arg2, arg3));
7955 unlock_user(p, arg1, 0);
7956 break;
7957 #endif
7958 #ifdef TARGET_NR_getuid32
7959 case TARGET_NR_getuid32:
7960 ret = get_errno(getuid());
7961 break;
7962 #endif
7963
7964 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7965 /* Alpha specific */
7966 case TARGET_NR_getxuid:
7967 {
7968 uid_t euid;
7969 euid=geteuid();
7970 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
7971 }
7972 ret = get_errno(getuid());
7973 break;
7974 #endif
7975 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
7976 /* Alpha specific */
7977 case TARGET_NR_getxgid:
7978 {
7979 uid_t egid;
7980 egid=getegid();
7981 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
7982 }
7983 ret = get_errno(getgid());
7984 break;
7985 #endif
7986 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
7987 /* Alpha specific */
7988 case TARGET_NR_osf_getsysinfo:
7989 ret = -TARGET_EOPNOTSUPP;
7990 switch (arg1) {
7991 case TARGET_GSI_IEEE_FP_CONTROL:
7992 {
7993 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
7994
7995 /* Copied from linux ieee_fpcr_to_swcr. */
7996 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
7997 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
7998 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
7999 | SWCR_TRAP_ENABLE_DZE
8000 | SWCR_TRAP_ENABLE_OVF);
8001 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
8002 | SWCR_TRAP_ENABLE_INE);
8003 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
8004 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
8005
8006 if (put_user_u64 (swcr, arg2))
8007 goto efault;
8008 ret = 0;
8009 }
8010 break;
8011
8012 /* case GSI_IEEE_STATE_AT_SIGNAL:
8013 -- Not implemented in linux kernel.
8014 case GSI_UACPROC:
8015 -- Retrieves current unaligned access state; not much used.
8016 case GSI_PROC_TYPE:
8017 -- Retrieves implver information; surely not used.
8018 case GSI_GET_HWRPB:
8019 -- Grabs a copy of the HWRPB; surely not used.
8020 */
8021 }
8022 break;
8023 #endif
8024 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8025 /* Alpha specific */
8026 case TARGET_NR_osf_setsysinfo:
8027 ret = -TARGET_EOPNOTSUPP;
8028 switch (arg1) {
8029 case TARGET_SSI_IEEE_FP_CONTROL:
8030 {
8031 uint64_t swcr, fpcr, orig_fpcr;
8032
8033 if (get_user_u64 (swcr, arg2)) {
8034 goto efault;
8035 }
8036 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
8037 fpcr = orig_fpcr & FPCR_DYN_MASK;
8038
8039 /* Copied from linux ieee_swcr_to_fpcr. */
8040 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
8041 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
8042 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
8043 | SWCR_TRAP_ENABLE_DZE
8044 | SWCR_TRAP_ENABLE_OVF)) << 48;
8045 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
8046 | SWCR_TRAP_ENABLE_INE)) << 57;
8047 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
8048 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
8049
8050 cpu_alpha_store_fpcr(cpu_env, fpcr);
8051 ret = 0;
8052 }
8053 break;
8054
8055 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
8056 {
8057 uint64_t exc, fpcr, orig_fpcr;
8058 int si_code;
8059
8060 if (get_user_u64(exc, arg2)) {
8061 goto efault;
8062 }
8063
8064 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
8065
8066 /* We only add to the exception status here. */
8067 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
8068
8069 cpu_alpha_store_fpcr(cpu_env, fpcr);
8070 ret = 0;
8071
8072 /* Old exceptions are not signaled. */
8073 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
8074
8075 /* If any exceptions set by this call,
8076 and are unmasked, send a signal. */
8077 si_code = 0;
8078 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
8079 si_code = TARGET_FPE_FLTRES;
8080 }
8081 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
8082 si_code = TARGET_FPE_FLTUND;
8083 }
8084 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
8085 si_code = TARGET_FPE_FLTOVF;
8086 }
8087 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
8088 si_code = TARGET_FPE_FLTDIV;
8089 }
8090 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
8091 si_code = TARGET_FPE_FLTINV;
8092 }
8093 if (si_code != 0) {
8094 target_siginfo_t info;
8095 info.si_signo = SIGFPE;
8096 info.si_errno = 0;
8097 info.si_code = si_code;
8098 info._sifields._sigfault._addr
8099 = ((CPUArchState *)cpu_env)->pc;
8100 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
8101 }
8102 }
8103 break;
8104
8105 /* case SSI_NVPAIRS:
8106 -- Used with SSIN_UACPROC to enable unaligned accesses.
8107 case SSI_IEEE_STATE_AT_SIGNAL:
8108 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8109 -- Not implemented in linux kernel
8110 */
8111 }
8112 break;
8113 #endif
8114 #ifdef TARGET_NR_osf_sigprocmask
8115 /* Alpha specific. */
8116 case TARGET_NR_osf_sigprocmask:
8117 {
8118 abi_ulong mask;
8119 int how;
8120 sigset_t set, oldset;
8121
8122 switch(arg1) {
8123 case TARGET_SIG_BLOCK:
8124 how = SIG_BLOCK;
8125 break;
8126 case TARGET_SIG_UNBLOCK:
8127 how = SIG_UNBLOCK;
8128 break;
8129 case TARGET_SIG_SETMASK:
8130 how = SIG_SETMASK;
8131 break;
8132 default:
8133 ret = -TARGET_EINVAL;
8134 goto fail;
8135 }
8136 mask = arg2;
8137 target_to_host_old_sigset(&set, &mask);
8138 sigprocmask(how, &set, &oldset);
8139 host_to_target_old_sigset(&mask, &oldset);
8140 ret = mask;
8141 }
8142 break;
8143 #endif
8144
8145 #ifdef TARGET_NR_getgid32
8146 case TARGET_NR_getgid32:
8147 ret = get_errno(getgid());
8148 break;
8149 #endif
8150 #ifdef TARGET_NR_geteuid32
8151 case TARGET_NR_geteuid32:
8152 ret = get_errno(geteuid());
8153 break;
8154 #endif
8155 #ifdef TARGET_NR_getegid32
8156 case TARGET_NR_getegid32:
8157 ret = get_errno(getegid());
8158 break;
8159 #endif
8160 #ifdef TARGET_NR_setreuid32
8161 case TARGET_NR_setreuid32:
8162 ret = get_errno(setreuid(arg1, arg2));
8163 break;
8164 #endif
8165 #ifdef TARGET_NR_setregid32
8166 case TARGET_NR_setregid32:
8167 ret = get_errno(setregid(arg1, arg2));
8168 break;
8169 #endif
8170 #ifdef TARGET_NR_getgroups32
8171 case TARGET_NR_getgroups32:
8172 {
8173 int gidsetsize = arg1;
8174 uint32_t *target_grouplist;
8175 gid_t *grouplist;
8176 int i;
8177
8178 grouplist = alloca(gidsetsize * sizeof(gid_t));
8179 ret = get_errno(getgroups(gidsetsize, grouplist));
8180 if (gidsetsize == 0)
8181 break;
8182 if (!is_error(ret)) {
8183 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
8184 if (!target_grouplist) {
8185 ret = -TARGET_EFAULT;
8186 goto fail;
8187 }
8188 for(i = 0;i < ret; i++)
8189 target_grouplist[i] = tswap32(grouplist[i]);
8190 unlock_user(target_grouplist, arg2, gidsetsize * 4);
8191 }
8192 }
8193 break;
8194 #endif
8195 #ifdef TARGET_NR_setgroups32
8196 case TARGET_NR_setgroups32:
8197 {
8198 int gidsetsize = arg1;
8199 uint32_t *target_grouplist;
8200 gid_t *grouplist;
8201 int i;
8202
8203 grouplist = alloca(gidsetsize * sizeof(gid_t));
8204 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
8205 if (!target_grouplist) {
8206 ret = -TARGET_EFAULT;
8207 goto fail;
8208 }
8209 for(i = 0;i < gidsetsize; i++)
8210 grouplist[i] = tswap32(target_grouplist[i]);
8211 unlock_user(target_grouplist, arg2, 0);
8212 ret = get_errno(setgroups(gidsetsize, grouplist));
8213 }
8214 break;
8215 #endif
8216 #ifdef TARGET_NR_fchown32
8217 case TARGET_NR_fchown32:
8218 ret = get_errno(fchown(arg1, arg2, arg3));
8219 break;
8220 #endif
8221 #ifdef TARGET_NR_setresuid32
8222 case TARGET_NR_setresuid32:
8223 ret = get_errno(setresuid(arg1, arg2, arg3));
8224 break;
8225 #endif
8226 #ifdef TARGET_NR_getresuid32
8227 case TARGET_NR_getresuid32:
8228 {
8229 uid_t ruid, euid, suid;
8230 ret = get_errno(getresuid(&ruid, &euid, &suid));
8231 if (!is_error(ret)) {
8232 if (put_user_u32(ruid, arg1)
8233 || put_user_u32(euid, arg2)
8234 || put_user_u32(suid, arg3))
8235 goto efault;
8236 }
8237 }
8238 break;
8239 #endif
8240 #ifdef TARGET_NR_setresgid32
8241 case TARGET_NR_setresgid32:
8242 ret = get_errno(setresgid(arg1, arg2, arg3));
8243 break;
8244 #endif
8245 #ifdef TARGET_NR_getresgid32
8246 case TARGET_NR_getresgid32:
8247 {
8248 gid_t rgid, egid, sgid;
8249 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8250 if (!is_error(ret)) {
8251 if (put_user_u32(rgid, arg1)
8252 || put_user_u32(egid, arg2)
8253 || put_user_u32(sgid, arg3))
8254 goto efault;
8255 }
8256 }
8257 break;
8258 #endif
8259 #ifdef TARGET_NR_chown32
8260 case TARGET_NR_chown32:
8261 if (!(p = lock_user_string(arg1)))
8262 goto efault;
8263 ret = get_errno(chown(p, arg2, arg3));
8264 unlock_user(p, arg1, 0);
8265 break;
8266 #endif
8267 #ifdef TARGET_NR_setuid32
8268 case TARGET_NR_setuid32:
8269 ret = get_errno(setuid(arg1));
8270 break;
8271 #endif
8272 #ifdef TARGET_NR_setgid32
8273 case TARGET_NR_setgid32:
8274 ret = get_errno(setgid(arg1));
8275 break;
8276 #endif
8277 #ifdef TARGET_NR_setfsuid32
8278 case TARGET_NR_setfsuid32:
8279 ret = get_errno(setfsuid(arg1));
8280 break;
8281 #endif
8282 #ifdef TARGET_NR_setfsgid32
8283 case TARGET_NR_setfsgid32:
8284 ret = get_errno(setfsgid(arg1));
8285 break;
8286 #endif
8287
8288 case TARGET_NR_pivot_root:
8289 goto unimplemented;
8290 #ifdef TARGET_NR_mincore
8291 case TARGET_NR_mincore:
8292 {
8293 void *a;
8294 ret = -TARGET_EFAULT;
8295 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
8296 goto efault;
8297 if (!(p = lock_user_string(arg3)))
8298 goto mincore_fail;
8299 ret = get_errno(mincore(a, arg2, p));
8300 unlock_user(p, arg3, ret);
8301 mincore_fail:
8302 unlock_user(a, arg1, 0);
8303 }
8304 break;
8305 #endif
8306 #ifdef TARGET_NR_arm_fadvise64_64
8307 case TARGET_NR_arm_fadvise64_64:
8308 {
8309 /*
8310 * arm_fadvise64_64 looks like fadvise64_64 but
8311 * with different argument order
8312 */
8313 abi_long temp;
8314 temp = arg3;
8315 arg3 = arg4;
8316 arg4 = temp;
8317 }
8318 #endif
8319 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
8320 #ifdef TARGET_NR_fadvise64_64
8321 case TARGET_NR_fadvise64_64:
8322 #endif
8323 #ifdef TARGET_NR_fadvise64
8324 case TARGET_NR_fadvise64:
8325 #endif
8326 #ifdef TARGET_S390X
8327 switch (arg4) {
8328 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
8329 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
8330 case 6: arg4 = POSIX_FADV_DONTNEED; break;
8331 case 7: arg4 = POSIX_FADV_NOREUSE; break;
8332 default: break;
8333 }
8334 #endif
8335 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
8336 break;
8337 #endif
8338 #ifdef TARGET_NR_madvise
8339 case TARGET_NR_madvise:
8340 /* A straight passthrough may not be safe because qemu sometimes
8341 turns private file-backed mappings into anonymous mappings.
8342 This will break MADV_DONTNEED.
8343 This is a hint, so ignoring and returning success is ok. */
8344 ret = get_errno(0);
8345 break;
8346 #endif
8347 #if TARGET_ABI_BITS == 32
8348 case TARGET_NR_fcntl64:
8349 {
8350 int cmd;
8351 struct flock64 fl;
8352 struct target_flock64 *target_fl;
8353 #ifdef TARGET_ARM
8354 struct target_eabi_flock64 *target_efl;
8355 #endif
8356
8357 cmd = target_to_host_fcntl_cmd(arg2);
8358 if (cmd == -TARGET_EINVAL) {
8359 ret = cmd;
8360 break;
8361 }
8362
8363 switch(arg2) {
8364 case TARGET_F_GETLK64:
8365 #ifdef TARGET_ARM
8366 if (((CPUARMState *)cpu_env)->eabi) {
8367 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8368 goto efault;
8369 fl.l_type = tswap16(target_efl->l_type);
8370 fl.l_whence = tswap16(target_efl->l_whence);
8371 fl.l_start = tswap64(target_efl->l_start);
8372 fl.l_len = tswap64(target_efl->l_len);
8373 fl.l_pid = tswap32(target_efl->l_pid);
8374 unlock_user_struct(target_efl, arg3, 0);
8375 } else
8376 #endif
8377 {
8378 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8379 goto efault;
8380 fl.l_type = tswap16(target_fl->l_type);
8381 fl.l_whence = tswap16(target_fl->l_whence);
8382 fl.l_start = tswap64(target_fl->l_start);
8383 fl.l_len = tswap64(target_fl->l_len);
8384 fl.l_pid = tswap32(target_fl->l_pid);
8385 unlock_user_struct(target_fl, arg3, 0);
8386 }
8387 ret = get_errno(fcntl(arg1, cmd, &fl));
8388 if (ret == 0) {
8389 #ifdef TARGET_ARM
8390 if (((CPUARMState *)cpu_env)->eabi) {
8391 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
8392 goto efault;
8393 target_efl->l_type = tswap16(fl.l_type);
8394 target_efl->l_whence = tswap16(fl.l_whence);
8395 target_efl->l_start = tswap64(fl.l_start);
8396 target_efl->l_len = tswap64(fl.l_len);
8397 target_efl->l_pid = tswap32(fl.l_pid);
8398 unlock_user_struct(target_efl, arg3, 1);
8399 } else
8400 #endif
8401 {
8402 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
8403 goto efault;
8404 target_fl->l_type = tswap16(fl.l_type);
8405 target_fl->l_whence = tswap16(fl.l_whence);
8406 target_fl->l_start = tswap64(fl.l_start);
8407 target_fl->l_len = tswap64(fl.l_len);
8408 target_fl->l_pid = tswap32(fl.l_pid);
8409 unlock_user_struct(target_fl, arg3, 1);
8410 }
8411 }
8412 break;
8413
8414 case TARGET_F_SETLK64:
8415 case TARGET_F_SETLKW64:
8416 #ifdef TARGET_ARM
8417 if (((CPUARMState *)cpu_env)->eabi) {
8418 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8419 goto efault;
8420 fl.l_type = tswap16(target_efl->l_type);
8421 fl.l_whence = tswap16(target_efl->l_whence);
8422 fl.l_start = tswap64(target_efl->l_start);
8423 fl.l_len = tswap64(target_efl->l_len);
8424 fl.l_pid = tswap32(target_efl->l_pid);
8425 unlock_user_struct(target_efl, arg3, 0);
8426 } else
8427 #endif
8428 {
8429 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8430 goto efault;
8431 fl.l_type = tswap16(target_fl->l_type);
8432 fl.l_whence = tswap16(target_fl->l_whence);
8433 fl.l_start = tswap64(target_fl->l_start);
8434 fl.l_len = tswap64(target_fl->l_len);
8435 fl.l_pid = tswap32(target_fl->l_pid);
8436 unlock_user_struct(target_fl, arg3, 0);
8437 }
8438 ret = get_errno(fcntl(arg1, cmd, &fl));
8439 break;
8440 default:
8441 ret = do_fcntl(arg1, arg2, arg3);
8442 break;
8443 }
8444 break;
8445 }
8446 #endif
8447 #ifdef TARGET_NR_cacheflush
8448 case TARGET_NR_cacheflush:
8449 /* self-modifying code is handled automatically, so nothing needed */
8450 ret = 0;
8451 break;
8452 #endif
8453 #ifdef TARGET_NR_security
8454 case TARGET_NR_security:
8455 goto unimplemented;
8456 #endif
8457 #ifdef TARGET_NR_getpagesize
8458 case TARGET_NR_getpagesize:
8459 ret = TARGET_PAGE_SIZE;
8460 break;
8461 #endif
8462 case TARGET_NR_gettid:
8463 ret = get_errno(gettid());
8464 break;
8465 #ifdef TARGET_NR_readahead
8466 case TARGET_NR_readahead:
8467 #if TARGET_ABI_BITS == 32
8468 if (regpairs_aligned(cpu_env)) {
8469 arg2 = arg3;
8470 arg3 = arg4;
8471 arg4 = arg5;
8472 }
8473 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
8474 #else
8475 ret = get_errno(readahead(arg1, arg2, arg3));
8476 #endif
8477 break;
8478 #endif
8479 #ifdef CONFIG_ATTR
8480 #ifdef TARGET_NR_setxattr
8481 case TARGET_NR_listxattr:
8482 case TARGET_NR_llistxattr:
8483 {
8484 void *p, *b = 0;
8485 if (arg2) {
8486 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8487 if (!b) {
8488 ret = -TARGET_EFAULT;
8489 break;
8490 }
8491 }
8492 p = lock_user_string(arg1);
8493 if (p) {
8494 if (num == TARGET_NR_listxattr) {
8495 ret = get_errno(listxattr(p, b, arg3));
8496 } else {
8497 ret = get_errno(llistxattr(p, b, arg3));
8498 }
8499 } else {
8500 ret = -TARGET_EFAULT;
8501 }
8502 unlock_user(p, arg1, 0);
8503 unlock_user(b, arg2, arg3);
8504 break;
8505 }
8506 case TARGET_NR_flistxattr:
8507 {
8508 void *b = 0;
8509 if (arg2) {
8510 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8511 if (!b) {
8512 ret = -TARGET_EFAULT;
8513 break;
8514 }
8515 }
8516 ret = get_errno(flistxattr(arg1, b, arg3));
8517 unlock_user(b, arg2, arg3);
8518 break;
8519 }
8520 case TARGET_NR_setxattr:
8521 case TARGET_NR_lsetxattr:
8522 {
8523 void *p, *n, *v = 0;
8524 if (arg3) {
8525 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8526 if (!v) {
8527 ret = -TARGET_EFAULT;
8528 break;
8529 }
8530 }
8531 p = lock_user_string(arg1);
8532 n = lock_user_string(arg2);
8533 if (p && n) {
8534 if (num == TARGET_NR_setxattr) {
8535 ret = get_errno(setxattr(p, n, v, arg4, arg5));
8536 } else {
8537 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
8538 }
8539 } else {
8540 ret = -TARGET_EFAULT;
8541 }
8542 unlock_user(p, arg1, 0);
8543 unlock_user(n, arg2, 0);
8544 unlock_user(v, arg3, 0);
8545 }
8546 break;
8547 case TARGET_NR_fsetxattr:
8548 {
8549 void *n, *v = 0;
8550 if (arg3) {
8551 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8552 if (!v) {
8553 ret = -TARGET_EFAULT;
8554 break;
8555 }
8556 }
8557 n = lock_user_string(arg2);
8558 if (n) {
8559 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
8560 } else {
8561 ret = -TARGET_EFAULT;
8562 }
8563 unlock_user(n, arg2, 0);
8564 unlock_user(v, arg3, 0);
8565 }
8566 break;
8567 case TARGET_NR_getxattr:
8568 case TARGET_NR_lgetxattr:
8569 {
8570 void *p, *n, *v = 0;
8571 if (arg3) {
8572 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8573 if (!v) {
8574 ret = -TARGET_EFAULT;
8575 break;
8576 }
8577 }
8578 p = lock_user_string(arg1);
8579 n = lock_user_string(arg2);
8580 if (p && n) {
8581 if (num == TARGET_NR_getxattr) {
8582 ret = get_errno(getxattr(p, n, v, arg4));
8583 } else {
8584 ret = get_errno(lgetxattr(p, n, v, arg4));
8585 }
8586 } else {
8587 ret = -TARGET_EFAULT;
8588 }
8589 unlock_user(p, arg1, 0);
8590 unlock_user(n, arg2, 0);
8591 unlock_user(v, arg3, arg4);
8592 }
8593 break;
8594 case TARGET_NR_fgetxattr:
8595 {
8596 void *n, *v = 0;
8597 if (arg3) {
8598 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8599 if (!v) {
8600 ret = -TARGET_EFAULT;
8601 break;
8602 }
8603 }
8604 n = lock_user_string(arg2);
8605 if (n) {
8606 ret = get_errno(fgetxattr(arg1, n, v, arg4));
8607 } else {
8608 ret = -TARGET_EFAULT;
8609 }
8610 unlock_user(n, arg2, 0);
8611 unlock_user(v, arg3, arg4);
8612 }
8613 break;
8614 case TARGET_NR_removexattr:
8615 case TARGET_NR_lremovexattr:
8616 {
8617 void *p, *n;
8618 p = lock_user_string(arg1);
8619 n = lock_user_string(arg2);
8620 if (p && n) {
8621 if (num == TARGET_NR_removexattr) {
8622 ret = get_errno(removexattr(p, n));
8623 } else {
8624 ret = get_errno(lremovexattr(p, n));
8625 }
8626 } else {
8627 ret = -TARGET_EFAULT;
8628 }
8629 unlock_user(p, arg1, 0);
8630 unlock_user(n, arg2, 0);
8631 }
8632 break;
8633 case TARGET_NR_fremovexattr:
8634 {
8635 void *n;
8636 n = lock_user_string(arg2);
8637 if (n) {
8638 ret = get_errno(fremovexattr(arg1, n));
8639 } else {
8640 ret = -TARGET_EFAULT;
8641 }
8642 unlock_user(n, arg2, 0);
8643 }
8644 break;
8645 #endif
8646 #endif /* CONFIG_ATTR */
8647 #ifdef TARGET_NR_set_thread_area
8648 case TARGET_NR_set_thread_area:
8649 #if defined(TARGET_MIPS)
8650 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
8651 ret = 0;
8652 break;
8653 #elif defined(TARGET_CRIS)
8654 if (arg1 & 0xff)
8655 ret = -TARGET_EINVAL;
8656 else {
8657 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
8658 ret = 0;
8659 }
8660 break;
8661 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
8662 ret = do_set_thread_area(cpu_env, arg1);
8663 break;
8664 #elif defined(TARGET_M68K)
8665 {
8666 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
8667 ts->tp_value = arg1;
8668 ret = 0;
8669 break;
8670 }
8671 #else
8672 goto unimplemented_nowarn;
8673 #endif
8674 #endif
8675 #ifdef TARGET_NR_get_thread_area
8676 case TARGET_NR_get_thread_area:
8677 #if defined(TARGET_I386) && defined(TARGET_ABI32)
8678 ret = do_get_thread_area(cpu_env, arg1);
8679 break;
8680 #elif defined(TARGET_M68K)
8681 {
8682 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
8683 ret = ts->tp_value;
8684 break;
8685 }
8686 #else
8687 goto unimplemented_nowarn;
8688 #endif
8689 #endif
8690 #ifdef TARGET_NR_getdomainname
8691 case TARGET_NR_getdomainname:
8692 goto unimplemented_nowarn;
8693 #endif
8694
8695 #ifdef TARGET_NR_clock_gettime
8696 case TARGET_NR_clock_gettime:
8697 {
8698 struct timespec ts;
8699 ret = get_errno(clock_gettime(arg1, &ts));
8700 if (!is_error(ret)) {
8701 host_to_target_timespec(arg2, &ts);
8702 }
8703 break;
8704 }
8705 #endif
8706 #ifdef TARGET_NR_clock_getres
8707 case TARGET_NR_clock_getres:
8708 {
8709 struct timespec ts;
8710 ret = get_errno(clock_getres(arg1, &ts));
8711 if (!is_error(ret)) {
8712 host_to_target_timespec(arg2, &ts);
8713 }
8714 break;
8715 }
8716 #endif
8717 #ifdef TARGET_NR_clock_nanosleep
8718 case TARGET_NR_clock_nanosleep:
8719 {
8720 struct timespec ts;
8721 target_to_host_timespec(&ts, arg3);
8722 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
8723 if (arg4)
8724 host_to_target_timespec(arg4, &ts);
8725 break;
8726 }
8727 #endif
8728
8729 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8730 case TARGET_NR_set_tid_address:
8731 ret = get_errno(set_tid_address((int *)g2h(arg1)));
8732 break;
8733 #endif
8734
8735 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
8736 case TARGET_NR_tkill:
8737 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
8738 break;
8739 #endif
8740
8741 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
8742 case TARGET_NR_tgkill:
8743 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
8744 target_to_host_signal(arg3)));
8745 break;
8746 #endif
8747
8748 #ifdef TARGET_NR_set_robust_list
8749 case TARGET_NR_set_robust_list:
8750 case TARGET_NR_get_robust_list:
8751 /* The ABI for supporting robust futexes has userspace pass
8752 * the kernel a pointer to a linked list which is updated by
8753 * userspace after the syscall; the list is walked by the kernel
8754 * when the thread exits. Since the linked list in QEMU guest
8755 * memory isn't a valid linked list for the host and we have
8756 * no way to reliably intercept the thread-death event, we can't
8757 * support these. Silently return ENOSYS so that guest userspace
8758 * falls back to a non-robust futex implementation (which should
8759 * be OK except in the corner case of the guest crashing while
8760 * holding a mutex that is shared with another process via
8761 * shared memory).
8762 */
8763 goto unimplemented_nowarn;
8764 #endif
8765
8766 #if defined(TARGET_NR_utimensat)
8767 case TARGET_NR_utimensat:
8768 {
8769 struct timespec *tsp, ts[2];
8770 if (!arg3) {
8771 tsp = NULL;
8772 } else {
8773 target_to_host_timespec(ts, arg3);
8774 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
8775 tsp = ts;
8776 }
8777 if (!arg2)
8778 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
8779 else {
8780 if (!(p = lock_user_string(arg2))) {
8781 ret = -TARGET_EFAULT;
8782 goto fail;
8783 }
8784 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
8785 unlock_user(p, arg2, 0);
8786 }
8787 }
8788 break;
8789 #endif
8790 case TARGET_NR_futex:
8791 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
8792 break;
8793 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
8794 case TARGET_NR_inotify_init:
8795 ret = get_errno(sys_inotify_init());
8796 break;
8797 #endif
8798 #ifdef CONFIG_INOTIFY1
8799 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8800 case TARGET_NR_inotify_init1:
8801 ret = get_errno(sys_inotify_init1(arg1));
8802 break;
8803 #endif
8804 #endif
8805 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
8806 case TARGET_NR_inotify_add_watch:
8807 p = lock_user_string(arg2);
8808 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
8809 unlock_user(p, arg2, 0);
8810 break;
8811 #endif
8812 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
8813 case TARGET_NR_inotify_rm_watch:
8814 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
8815 break;
8816 #endif
8817
8818 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
8819 case TARGET_NR_mq_open:
8820 {
8821 struct mq_attr posix_mq_attr;
8822
8823 p = lock_user_string(arg1 - 1);
8824 if (arg4 != 0)
8825 copy_from_user_mq_attr (&posix_mq_attr, arg4);
8826 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
8827 unlock_user (p, arg1, 0);
8828 }
8829 break;
8830
8831 case TARGET_NR_mq_unlink:
8832 p = lock_user_string(arg1 - 1);
8833 ret = get_errno(mq_unlink(p));
8834 unlock_user (p, arg1, 0);
8835 break;
8836
8837 case TARGET_NR_mq_timedsend:
8838 {
8839 struct timespec ts;
8840
8841 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8842 if (arg5 != 0) {
8843 target_to_host_timespec(&ts, arg5);
8844 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
8845 host_to_target_timespec(arg5, &ts);
8846 }
8847 else
8848 ret = get_errno(mq_send(arg1, p, arg3, arg4));
8849 unlock_user (p, arg2, arg3);
8850 }
8851 break;
8852
8853 case TARGET_NR_mq_timedreceive:
8854 {
8855 struct timespec ts;
8856 unsigned int prio;
8857
8858 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8859 if (arg5 != 0) {
8860 target_to_host_timespec(&ts, arg5);
8861 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
8862 host_to_target_timespec(arg5, &ts);
8863 }
8864 else
8865 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
8866 unlock_user (p, arg2, arg3);
8867 if (arg4 != 0)
8868 put_user_u32(prio, arg4);
8869 }
8870 break;
8871
8872 /* Not implemented for now... */
8873 /* case TARGET_NR_mq_notify: */
8874 /* break; */
8875
8876 case TARGET_NR_mq_getsetattr:
8877 {
8878 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
8879 ret = 0;
8880 if (arg3 != 0) {
8881 ret = mq_getattr(arg1, &posix_mq_attr_out);
8882 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
8883 }
8884 if (arg2 != 0) {
8885 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
8886 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
8887 }
8888
8889 }
8890 break;
8891 #endif
8892
8893 #ifdef CONFIG_SPLICE
8894 #ifdef TARGET_NR_tee
8895 case TARGET_NR_tee:
8896 {
8897 ret = get_errno(tee(arg1,arg2,arg3,arg4));
8898 }
8899 break;
8900 #endif
8901 #ifdef TARGET_NR_splice
8902 case TARGET_NR_splice:
8903 {
8904 loff_t loff_in, loff_out;
8905 loff_t *ploff_in = NULL, *ploff_out = NULL;
8906 if(arg2) {
8907 get_user_u64(loff_in, arg2);
8908 ploff_in = &loff_in;
8909 }
8910 if(arg4) {
8911 get_user_u64(loff_out, arg2);
8912 ploff_out = &loff_out;
8913 }
8914 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
8915 }
8916 break;
8917 #endif
8918 #ifdef TARGET_NR_vmsplice
8919 case TARGET_NR_vmsplice:
8920 {
8921 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
8922 if (vec != NULL) {
8923 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
8924 unlock_iovec(vec, arg2, arg3, 0);
8925 } else {
8926 ret = -host_to_target_errno(errno);
8927 }
8928 }
8929 break;
8930 #endif
8931 #endif /* CONFIG_SPLICE */
8932 #ifdef CONFIG_EVENTFD
8933 #if defined(TARGET_NR_eventfd)
8934 case TARGET_NR_eventfd:
8935 ret = get_errno(eventfd(arg1, 0));
8936 break;
8937 #endif
8938 #if defined(TARGET_NR_eventfd2)
8939 case TARGET_NR_eventfd2:
8940 {
8941 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
8942 if (arg2 & TARGET_O_NONBLOCK) {
8943 host_flags |= O_NONBLOCK;
8944 }
8945 if (arg2 & TARGET_O_CLOEXEC) {
8946 host_flags |= O_CLOEXEC;
8947 }
8948 ret = get_errno(eventfd(arg1, host_flags));
8949 break;
8950 }
8951 #endif
8952 #endif /* CONFIG_EVENTFD */
8953 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
8954 case TARGET_NR_fallocate:
8955 #if TARGET_ABI_BITS == 32
8956 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
8957 target_offset64(arg5, arg6)));
8958 #else
8959 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
8960 #endif
8961 break;
8962 #endif
8963 #if defined(CONFIG_SYNC_FILE_RANGE)
8964 #if defined(TARGET_NR_sync_file_range)
8965 case TARGET_NR_sync_file_range:
8966 #if TARGET_ABI_BITS == 32
8967 #if defined(TARGET_MIPS)
8968 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8969 target_offset64(arg5, arg6), arg7));
8970 #else
8971 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
8972 target_offset64(arg4, arg5), arg6));
8973 #endif /* !TARGET_MIPS */
8974 #else
8975 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
8976 #endif
8977 break;
8978 #endif
8979 #if defined(TARGET_NR_sync_file_range2)
8980 case TARGET_NR_sync_file_range2:
8981 /* This is like sync_file_range but the arguments are reordered */
8982 #if TARGET_ABI_BITS == 32
8983 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8984 target_offset64(arg5, arg6), arg2));
8985 #else
8986 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
8987 #endif
8988 break;
8989 #endif
8990 #endif
8991 #if defined(CONFIG_EPOLL)
8992 #if defined(TARGET_NR_epoll_create)
8993 case TARGET_NR_epoll_create:
8994 ret = get_errno(epoll_create(arg1));
8995 break;
8996 #endif
8997 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
8998 case TARGET_NR_epoll_create1:
8999 ret = get_errno(epoll_create1(arg1));
9000 break;
9001 #endif
9002 #if defined(TARGET_NR_epoll_ctl)
9003 case TARGET_NR_epoll_ctl:
9004 {
9005 struct epoll_event ep;
9006 struct epoll_event *epp = 0;
9007 if (arg4) {
9008 struct target_epoll_event *target_ep;
9009 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
9010 goto efault;
9011 }
9012 ep.events = tswap32(target_ep->events);
9013 /* The epoll_data_t union is just opaque data to the kernel,
9014 * so we transfer all 64 bits across and need not worry what
9015 * actual data type it is.
9016 */
9017 ep.data.u64 = tswap64(target_ep->data.u64);
9018 unlock_user_struct(target_ep, arg4, 0);
9019 epp = &ep;
9020 }
9021 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
9022 break;
9023 }
9024 #endif
9025
9026 #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9027 #define IMPLEMENT_EPOLL_PWAIT
9028 #endif
9029 #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9030 #if defined(TARGET_NR_epoll_wait)
9031 case TARGET_NR_epoll_wait:
9032 #endif
9033 #if defined(IMPLEMENT_EPOLL_PWAIT)
9034 case TARGET_NR_epoll_pwait:
9035 #endif
9036 {
9037 struct target_epoll_event *target_ep;
9038 struct epoll_event *ep;
9039 int epfd = arg1;
9040 int maxevents = arg3;
9041 int timeout = arg4;
9042
9043 target_ep = lock_user(VERIFY_WRITE, arg2,
9044 maxevents * sizeof(struct target_epoll_event), 1);
9045 if (!target_ep) {
9046 goto efault;
9047 }
9048
9049 ep = alloca(maxevents * sizeof(struct epoll_event));
9050
9051 switch (num) {
9052 #if defined(IMPLEMENT_EPOLL_PWAIT)
9053 case TARGET_NR_epoll_pwait:
9054 {
9055 target_sigset_t *target_set;
9056 sigset_t _set, *set = &_set;
9057
9058 if (arg5) {
9059 target_set = lock_user(VERIFY_READ, arg5,
9060 sizeof(target_sigset_t), 1);
9061 if (!target_set) {
9062 unlock_user(target_ep, arg2, 0);
9063 goto efault;
9064 }
9065 target_to_host_sigset(set, target_set);
9066 unlock_user(target_set, arg5, 0);
9067 } else {
9068 set = NULL;
9069 }
9070
9071 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
9072 break;
9073 }
9074 #endif
9075 #if defined(TARGET_NR_epoll_wait)
9076 case TARGET_NR_epoll_wait:
9077 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
9078 break;
9079 #endif
9080 default:
9081 ret = -TARGET_ENOSYS;
9082 }
9083 if (!is_error(ret)) {
9084 int i;
9085 for (i = 0; i < ret; i++) {
9086 target_ep[i].events = tswap32(ep[i].events);
9087 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
9088 }
9089 }
9090 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
9091 break;
9092 }
9093 #endif
9094 #endif
9095 #ifdef TARGET_NR_prlimit64
9096 case TARGET_NR_prlimit64:
9097 {
9098 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9099 struct target_rlimit64 *target_rnew, *target_rold;
9100 struct host_rlimit64 rnew, rold, *rnewp = 0;
9101 if (arg3) {
9102 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
9103 goto efault;
9104 }
9105 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
9106 rnew.rlim_max = tswap64(target_rnew->rlim_max);
9107 unlock_user_struct(target_rnew, arg3, 0);
9108 rnewp = &rnew;
9109 }
9110
9111 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
9112 if (!is_error(ret) && arg4) {
9113 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
9114 goto efault;
9115 }
9116 target_rold->rlim_cur = tswap64(rold.rlim_cur);
9117 target_rold->rlim_max = tswap64(rold.rlim_max);
9118 unlock_user_struct(target_rold, arg4, 1);
9119 }
9120 break;
9121 }
9122 #endif
9123 #ifdef TARGET_NR_gethostname
9124 case TARGET_NR_gethostname:
9125 {
9126 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
9127 if (name) {
9128 ret = get_errno(gethostname(name, arg2));
9129 unlock_user(name, arg1, arg2);
9130 } else {
9131 ret = -TARGET_EFAULT;
9132 }
9133 break;
9134 }
9135 #endif
9136 #ifdef TARGET_NR_atomic_cmpxchg_32
9137 case TARGET_NR_atomic_cmpxchg_32:
9138 {
9139 /* should use start_exclusive from main.c */
9140 abi_ulong mem_value;
9141 if (get_user_u32(mem_value, arg6)) {
9142 target_siginfo_t info;
9143 info.si_signo = SIGSEGV;
9144 info.si_errno = 0;
9145 info.si_code = TARGET_SEGV_MAPERR;
9146 info._sifields._sigfault._addr = arg6;
9147 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
9148 ret = 0xdeadbeef;
9149
9150 }
9151 if (mem_value == arg2)
9152 put_user_u32(arg1, arg6);
9153 ret = mem_value;
9154 break;
9155 }
9156 #endif
9157 #ifdef TARGET_NR_atomic_barrier
9158 case TARGET_NR_atomic_barrier:
9159 {
9160 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
9161 break;
9162 }
9163 #endif
9164 default:
9165 unimplemented:
9166 gemu_log("qemu: Unsupported syscall: %d\n", num);
9167 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
9168 unimplemented_nowarn:
9169 #endif
9170 ret = -TARGET_ENOSYS;
9171 break;
9172 }
9173 fail:
9174 #ifdef DEBUG
9175 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
9176 #endif
9177 if(do_strace)
9178 print_syscall_ret(num, ret);
9179 return ret;
9180 efault:
9181 ret = -TARGET_EFAULT;
9182 goto fail;
9183 }