mips: rlimit codes are not the same
[qemu/ar7.git] / linux-user / syscall.c
blob9eb41a01acf4a6cdcba67789209c1be2a559373c
1 /*
2 * Linux syscalls
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define _ATFILE_SOURCE
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <elf.h>
25 #include <endian.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <time.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #include <sys/ipc.h>
33 #include <sys/msg.h>
34 #include <sys/wait.h>
35 #include <sys/time.h>
36 #include <sys/stat.h>
37 #include <sys/mount.h>
38 #include <sys/prctl.h>
39 #include <sys/resource.h>
40 #include <sys/mman.h>
41 #include <sys/swap.h>
42 #include <signal.h>
43 #include <sched.h>
44 #ifdef __ia64__
45 int __clone2(int (*fn)(void *), void *child_stack_base,
46 size_t stack_size, int flags, void *arg, ...);
47 #endif
48 #include <sys/socket.h>
49 #include <sys/un.h>
50 #include <sys/uio.h>
51 #include <sys/poll.h>
52 #include <sys/times.h>
53 #include <sys/shm.h>
54 #include <sys/sem.h>
55 #include <sys/statfs.h>
56 #include <utime.h>
57 #include <sys/sysinfo.h>
58 #include <sys/utsname.h>
59 //#include <sys/user.h>
60 #include <netinet/ip.h>
61 #include <netinet/tcp.h>
62 #include <linux/wireless.h>
63 #include <qemu-common.h>
64 #ifdef TARGET_GPROF
65 #include <sys/gmon.h>
66 #endif
67 #ifdef CONFIG_EVENTFD
68 #include <sys/eventfd.h>
69 #endif
70 #ifdef CONFIG_EPOLL
71 #include <sys/epoll.h>
72 #endif
74 #define termios host_termios
75 #define winsize host_winsize
76 #define termio host_termio
77 #define sgttyb host_sgttyb /* same as target */
78 #define tchars host_tchars /* same as target */
79 #define ltchars host_ltchars /* same as target */
81 #include <linux/termios.h>
82 #include <linux/unistd.h>
83 #include <linux/utsname.h>
84 #include <linux/cdrom.h>
85 #include <linux/hdreg.h>
86 #include <linux/soundcard.h>
87 #include <linux/kd.h>
88 #include <linux/mtio.h>
89 #include <linux/fs.h>
90 #if defined(CONFIG_FIEMAP)
91 #include <linux/fiemap.h>
92 #endif
93 #include <linux/fb.h>
94 #include <linux/vt.h>
95 #include "linux_loop.h"
96 #include "cpu-uname.h"
98 #include "qemu.h"
99 #include "qemu-common.h"
101 #if defined(CONFIG_USE_NPTL)
102 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
103 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
104 #else
105 /* XXX: Hardcode the above values. */
106 #define CLONE_NPTL_FLAGS2 0
107 #endif
109 //#define DEBUG
111 //#include <linux/msdos_fs.h>
112 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
113 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
116 #undef _syscall0
117 #undef _syscall1
118 #undef _syscall2
119 #undef _syscall3
120 #undef _syscall4
121 #undef _syscall5
122 #undef _syscall6
124 #define _syscall0(type,name) \
125 static type name (void) \
127 return syscall(__NR_##name); \
130 #define _syscall1(type,name,type1,arg1) \
131 static type name (type1 arg1) \
133 return syscall(__NR_##name, arg1); \
136 #define _syscall2(type,name,type1,arg1,type2,arg2) \
137 static type name (type1 arg1,type2 arg2) \
139 return syscall(__NR_##name, arg1, arg2); \
142 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
143 static type name (type1 arg1,type2 arg2,type3 arg3) \
145 return syscall(__NR_##name, arg1, arg2, arg3); \
148 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
149 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
151 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
154 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
155 type5,arg5) \
156 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
158 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
162 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
163 type5,arg5,type6,arg6) \
164 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
165 type6 arg6) \
167 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
171 #define __NR_sys_uname __NR_uname
172 #define __NR_sys_faccessat __NR_faccessat
173 #define __NR_sys_fchmodat __NR_fchmodat
174 #define __NR_sys_fchownat __NR_fchownat
175 #define __NR_sys_fstatat64 __NR_fstatat64
176 #define __NR_sys_futimesat __NR_futimesat
177 #define __NR_sys_getcwd1 __NR_getcwd
178 #define __NR_sys_getdents __NR_getdents
179 #define __NR_sys_getdents64 __NR_getdents64
180 #define __NR_sys_getpriority __NR_getpriority
181 #define __NR_sys_linkat __NR_linkat
182 #define __NR_sys_mkdirat __NR_mkdirat
183 #define __NR_sys_mknodat __NR_mknodat
184 #define __NR_sys_newfstatat __NR_newfstatat
185 #define __NR_sys_openat __NR_openat
186 #define __NR_sys_readlinkat __NR_readlinkat
187 #define __NR_sys_renameat __NR_renameat
188 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
189 #define __NR_sys_symlinkat __NR_symlinkat
190 #define __NR_sys_syslog __NR_syslog
191 #define __NR_sys_tgkill __NR_tgkill
192 #define __NR_sys_tkill __NR_tkill
193 #define __NR_sys_unlinkat __NR_unlinkat
194 #define __NR_sys_utimensat __NR_utimensat
195 #define __NR_sys_futex __NR_futex
196 #define __NR_sys_inotify_init __NR_inotify_init
197 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
198 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
200 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
201 defined(__s390x__)
202 #define __NR__llseek __NR_lseek
203 #endif
205 #ifdef __NR_gettid
206 _syscall0(int, gettid)
207 #else
208 /* This is a replacement for the host gettid() and must return a host
209 errno. */
210 static int gettid(void) {
211 return -ENOSYS;
213 #endif
214 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
215 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
216 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
217 #endif
218 _syscall2(int, sys_getpriority, int, which, int, who);
219 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
220 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
221 loff_t *, res, uint, wh);
222 #endif
223 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
224 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
225 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
226 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
227 #endif
228 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
229 _syscall2(int,sys_tkill,int,tid,int,sig)
230 #endif
231 #ifdef __NR_exit_group
232 _syscall1(int,exit_group,int,error_code)
233 #endif
234 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
235 _syscall1(int,set_tid_address,int *,tidptr)
236 #endif
237 #if defined(CONFIG_USE_NPTL)
238 #if defined(TARGET_NR_futex) && defined(__NR_futex)
239 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
240 const struct timespec *,timeout,int *,uaddr2,int,val3)
241 #endif
242 #endif
243 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
244 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
245 unsigned long *, user_mask_ptr);
246 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
247 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
248 unsigned long *, user_mask_ptr);
250 static bitmask_transtbl fcntl_flags_tbl[] = {
251 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
252 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
253 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
254 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
255 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
256 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
257 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
258 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
259 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
260 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
261 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
262 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
263 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
264 #if defined(O_DIRECT)
265 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
266 #endif
267 { 0, 0, 0, 0 }
270 #define COPY_UTSNAME_FIELD(dest, src) \
271 do { \
272 /* __NEW_UTS_LEN doesn't include terminating null */ \
273 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
274 (dest)[__NEW_UTS_LEN] = '\0'; \
275 } while (0)
277 static int sys_uname(struct new_utsname *buf)
279 struct utsname uts_buf;
281 if (uname(&uts_buf) < 0)
282 return (-1);
285 * Just in case these have some differences, we
286 * translate utsname to new_utsname (which is the
287 * struct linux kernel uses).
290 memset(buf, 0, sizeof(*buf));
291 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
292 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
293 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
294 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
295 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
296 #ifdef _GNU_SOURCE
297 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
298 #endif
299 return (0);
301 #undef COPY_UTSNAME_FIELD
304 static int sys_getcwd1(char *buf, size_t size)
306 if (getcwd(buf, size) == NULL) {
307 /* getcwd() sets errno */
308 return (-1);
310 return strlen(buf)+1;
313 #ifdef CONFIG_ATFILE
315 * Host system seems to have atfile syscall stubs available. We
316 * now enable them one by one as specified by target syscall_nr.h.
319 #ifdef TARGET_NR_faccessat
320 static int sys_faccessat(int dirfd, const char *pathname, int mode)
322 return (faccessat(dirfd, pathname, mode, 0));
324 #endif
325 #ifdef TARGET_NR_fchmodat
326 static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
328 return (fchmodat(dirfd, pathname, mode, 0));
330 #endif
331 #if defined(TARGET_NR_fchownat)
332 static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
333 gid_t group, int flags)
335 return (fchownat(dirfd, pathname, owner, group, flags));
337 #endif
338 #ifdef __NR_fstatat64
339 static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
340 int flags)
342 return (fstatat(dirfd, pathname, buf, flags));
344 #endif
345 #ifdef __NR_newfstatat
346 static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
347 int flags)
349 return (fstatat(dirfd, pathname, buf, flags));
351 #endif
352 #ifdef TARGET_NR_futimesat
353 static int sys_futimesat(int dirfd, const char *pathname,
354 const struct timeval times[2])
356 return (futimesat(dirfd, pathname, times));
358 #endif
359 #ifdef TARGET_NR_linkat
360 static int sys_linkat(int olddirfd, const char *oldpath,
361 int newdirfd, const char *newpath, int flags)
363 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
365 #endif
366 #ifdef TARGET_NR_mkdirat
367 static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
369 return (mkdirat(dirfd, pathname, mode));
371 #endif
372 #ifdef TARGET_NR_mknodat
373 static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
374 dev_t dev)
376 return (mknodat(dirfd, pathname, mode, dev));
378 #endif
379 #ifdef TARGET_NR_openat
380 static int sys_openat(int dirfd, const char *pathname, int flags, ...)
383 * open(2) has extra parameter 'mode' when called with
384 * flag O_CREAT.
386 if ((flags & O_CREAT) != 0) {
387 va_list ap;
388 mode_t mode;
391 * Get the 'mode' parameter and translate it to
392 * host bits.
394 va_start(ap, flags);
395 mode = va_arg(ap, mode_t);
396 mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
397 va_end(ap);
399 return (openat(dirfd, pathname, flags, mode));
401 return (openat(dirfd, pathname, flags));
403 #endif
404 #ifdef TARGET_NR_readlinkat
405 static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
407 return (readlinkat(dirfd, pathname, buf, bufsiz));
409 #endif
410 #ifdef TARGET_NR_renameat
411 static int sys_renameat(int olddirfd, const char *oldpath,
412 int newdirfd, const char *newpath)
414 return (renameat(olddirfd, oldpath, newdirfd, newpath));
416 #endif
417 #ifdef TARGET_NR_symlinkat
418 static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
420 return (symlinkat(oldpath, newdirfd, newpath));
422 #endif
423 #ifdef TARGET_NR_unlinkat
424 static int sys_unlinkat(int dirfd, const char *pathname, int flags)
426 return (unlinkat(dirfd, pathname, flags));
428 #endif
429 #else /* !CONFIG_ATFILE */
432 * Try direct syscalls instead
434 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
435 _syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
436 #endif
437 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
438 _syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
439 #endif
440 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
441 _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
442 uid_t,owner,gid_t,group,int,flags)
443 #endif
444 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
445 defined(__NR_fstatat64)
446 _syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
447 struct stat *,buf,int,flags)
448 #endif
449 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
450 _syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
451 const struct timeval *,times)
452 #endif
453 #if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
454 defined(__NR_newfstatat)
455 _syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
456 struct stat *,buf,int,flags)
457 #endif
458 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
459 _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
460 int,newdirfd,const char *,newpath,int,flags)
461 #endif
462 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
463 _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
464 #endif
465 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
466 _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
467 mode_t,mode,dev_t,dev)
468 #endif
469 #if defined(TARGET_NR_openat) && defined(__NR_openat)
470 _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
471 #endif
472 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
473 _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
474 char *,buf,size_t,bufsize)
475 #endif
476 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
477 _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
478 int,newdirfd,const char *,newpath)
479 #endif
480 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
481 _syscall3(int,sys_symlinkat,const char *,oldpath,
482 int,newdirfd,const char *,newpath)
483 #endif
484 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
485 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
486 #endif
488 #endif /* CONFIG_ATFILE */
490 #ifdef CONFIG_UTIMENSAT
491 static int sys_utimensat(int dirfd, const char *pathname,
492 const struct timespec times[2], int flags)
494 if (pathname == NULL)
495 return futimens(dirfd, times);
496 else
497 return utimensat(dirfd, pathname, times, flags);
499 #else
500 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
501 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
502 const struct timespec *,tsp,int,flags)
503 #endif
504 #endif /* CONFIG_UTIMENSAT */
506 #ifdef CONFIG_INOTIFY
507 #include <sys/inotify.h>
509 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
510 static int sys_inotify_init(void)
512 return (inotify_init());
514 #endif
515 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
516 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
518 return (inotify_add_watch(fd, pathname, mask));
520 #endif
521 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
522 static int sys_inotify_rm_watch(int fd, int32_t wd)
524 return (inotify_rm_watch(fd, wd));
526 #endif
527 #ifdef CONFIG_INOTIFY1
528 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
529 static int sys_inotify_init1(int flags)
531 return (inotify_init1(flags));
533 #endif
534 #endif
535 #else
536 /* Userspace can usually survive runtime without inotify */
537 #undef TARGET_NR_inotify_init
538 #undef TARGET_NR_inotify_init1
539 #undef TARGET_NR_inotify_add_watch
540 #undef TARGET_NR_inotify_rm_watch
541 #endif /* CONFIG_INOTIFY */
543 #if defined(TARGET_NR_ppoll)
544 #ifndef __NR_ppoll
545 # define __NR_ppoll -1
546 #endif
547 #define __NR_sys_ppoll __NR_ppoll
548 _syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
549 struct timespec *, timeout, const __sigset_t *, sigmask,
550 size_t, sigsetsize)
551 #endif
553 #if defined(TARGET_NR_pselect6)
554 #ifndef __NR_pselect6
555 # define __NR_pselect6 -1
556 #endif
557 #define __NR_sys_pselect6 __NR_pselect6
558 _syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
559 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
560 #endif
562 #if defined(TARGET_NR_prlimit64)
563 #ifndef __NR_prlimit64
564 # define __NR_prlimit64 -1
565 #endif
566 #define __NR_sys_prlimit64 __NR_prlimit64
567 /* The glibc rlimit structure may not be that used by the underlying syscall */
568 struct host_rlimit64 {
569 uint64_t rlim_cur;
570 uint64_t rlim_max;
572 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
573 const struct host_rlimit64 *, new_limit,
574 struct host_rlimit64 *, old_limit)
575 #endif
577 extern int personality(int);
578 extern int flock(int, int);
579 extern int setfsuid(int);
580 extern int setfsgid(int);
581 extern int setgroups(int, gid_t *);
583 #define ERRNO_TABLE_SIZE 1200
585 /* target_to_host_errno_table[] is initialized from
586 * host_to_target_errno_table[] in syscall_init(). */
587 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
591 * This list is the union of errno values overridden in asm-<arch>/errno.h
592 * minus the errnos that are not actually generic to all archs.
594 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
595 [EIDRM] = TARGET_EIDRM,
596 [ECHRNG] = TARGET_ECHRNG,
597 [EL2NSYNC] = TARGET_EL2NSYNC,
598 [EL3HLT] = TARGET_EL3HLT,
599 [EL3RST] = TARGET_EL3RST,
600 [ELNRNG] = TARGET_ELNRNG,
601 [EUNATCH] = TARGET_EUNATCH,
602 [ENOCSI] = TARGET_ENOCSI,
603 [EL2HLT] = TARGET_EL2HLT,
604 [EDEADLK] = TARGET_EDEADLK,
605 [ENOLCK] = TARGET_ENOLCK,
606 [EBADE] = TARGET_EBADE,
607 [EBADR] = TARGET_EBADR,
608 [EXFULL] = TARGET_EXFULL,
609 [ENOANO] = TARGET_ENOANO,
610 [EBADRQC] = TARGET_EBADRQC,
611 [EBADSLT] = TARGET_EBADSLT,
612 [EBFONT] = TARGET_EBFONT,
613 [ENOSTR] = TARGET_ENOSTR,
614 [ENODATA] = TARGET_ENODATA,
615 [ETIME] = TARGET_ETIME,
616 [ENOSR] = TARGET_ENOSR,
617 [ENONET] = TARGET_ENONET,
618 [ENOPKG] = TARGET_ENOPKG,
619 [EREMOTE] = TARGET_EREMOTE,
620 [ENOLINK] = TARGET_ENOLINK,
621 [EADV] = TARGET_EADV,
622 [ESRMNT] = TARGET_ESRMNT,
623 [ECOMM] = TARGET_ECOMM,
624 [EPROTO] = TARGET_EPROTO,
625 [EDOTDOT] = TARGET_EDOTDOT,
626 [EMULTIHOP] = TARGET_EMULTIHOP,
627 [EBADMSG] = TARGET_EBADMSG,
628 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
629 [EOVERFLOW] = TARGET_EOVERFLOW,
630 [ENOTUNIQ] = TARGET_ENOTUNIQ,
631 [EBADFD] = TARGET_EBADFD,
632 [EREMCHG] = TARGET_EREMCHG,
633 [ELIBACC] = TARGET_ELIBACC,
634 [ELIBBAD] = TARGET_ELIBBAD,
635 [ELIBSCN] = TARGET_ELIBSCN,
636 [ELIBMAX] = TARGET_ELIBMAX,
637 [ELIBEXEC] = TARGET_ELIBEXEC,
638 [EILSEQ] = TARGET_EILSEQ,
639 [ENOSYS] = TARGET_ENOSYS,
640 [ELOOP] = TARGET_ELOOP,
641 [ERESTART] = TARGET_ERESTART,
642 [ESTRPIPE] = TARGET_ESTRPIPE,
643 [ENOTEMPTY] = TARGET_ENOTEMPTY,
644 [EUSERS] = TARGET_EUSERS,
645 [ENOTSOCK] = TARGET_ENOTSOCK,
646 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
647 [EMSGSIZE] = TARGET_EMSGSIZE,
648 [EPROTOTYPE] = TARGET_EPROTOTYPE,
649 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
650 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
651 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
652 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
653 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
654 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
655 [EADDRINUSE] = TARGET_EADDRINUSE,
656 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
657 [ENETDOWN] = TARGET_ENETDOWN,
658 [ENETUNREACH] = TARGET_ENETUNREACH,
659 [ENETRESET] = TARGET_ENETRESET,
660 [ECONNABORTED] = TARGET_ECONNABORTED,
661 [ECONNRESET] = TARGET_ECONNRESET,
662 [ENOBUFS] = TARGET_ENOBUFS,
663 [EISCONN] = TARGET_EISCONN,
664 [ENOTCONN] = TARGET_ENOTCONN,
665 [EUCLEAN] = TARGET_EUCLEAN,
666 [ENOTNAM] = TARGET_ENOTNAM,
667 [ENAVAIL] = TARGET_ENAVAIL,
668 [EISNAM] = TARGET_EISNAM,
669 [EREMOTEIO] = TARGET_EREMOTEIO,
670 [ESHUTDOWN] = TARGET_ESHUTDOWN,
671 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
672 [ETIMEDOUT] = TARGET_ETIMEDOUT,
673 [ECONNREFUSED] = TARGET_ECONNREFUSED,
674 [EHOSTDOWN] = TARGET_EHOSTDOWN,
675 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
676 [EALREADY] = TARGET_EALREADY,
677 [EINPROGRESS] = TARGET_EINPROGRESS,
678 [ESTALE] = TARGET_ESTALE,
679 [ECANCELED] = TARGET_ECANCELED,
680 [ENOMEDIUM] = TARGET_ENOMEDIUM,
681 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
682 #ifdef ENOKEY
683 [ENOKEY] = TARGET_ENOKEY,
684 #endif
685 #ifdef EKEYEXPIRED
686 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
687 #endif
688 #ifdef EKEYREVOKED
689 [EKEYREVOKED] = TARGET_EKEYREVOKED,
690 #endif
691 #ifdef EKEYREJECTED
692 [EKEYREJECTED] = TARGET_EKEYREJECTED,
693 #endif
694 #ifdef EOWNERDEAD
695 [EOWNERDEAD] = TARGET_EOWNERDEAD,
696 #endif
697 #ifdef ENOTRECOVERABLE
698 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
699 #endif
702 static inline int host_to_target_errno(int err)
704 if(host_to_target_errno_table[err])
705 return host_to_target_errno_table[err];
706 return err;
709 static inline int target_to_host_errno(int err)
711 if (target_to_host_errno_table[err])
712 return target_to_host_errno_table[err];
713 return err;
716 static inline abi_long get_errno(abi_long ret)
718 if (ret == -1)
719 return -host_to_target_errno(errno);
720 else
721 return ret;
724 static inline int is_error(abi_long ret)
726 return (abi_ulong)ret >= (abi_ulong)(-4096);
729 char *target_strerror(int err)
731 return strerror(target_to_host_errno(err));
734 static abi_ulong target_brk;
735 static abi_ulong target_original_brk;
736 static abi_ulong brk_page;
738 void target_set_brk(abi_ulong new_brk)
740 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
741 brk_page = HOST_PAGE_ALIGN(target_brk);
744 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
745 #define DEBUGF_BRK(message, args...)
747 /* do_brk() must return target values and target errnos. */
748 abi_long do_brk(abi_ulong new_brk)
750 abi_long mapped_addr;
751 int new_alloc_size;
753 DEBUGF_BRK("do_brk(%#010x) -> ", new_brk);
755 if (!new_brk) {
756 DEBUGF_BRK("%#010x (!new_brk)\n", target_brk);
757 return target_brk;
759 if (new_brk < target_original_brk) {
760 DEBUGF_BRK("%#010x (new_brk < target_original_brk)\n", target_brk);
761 return target_brk;
764 /* If the new brk is less than the highest page reserved to the
765 * target heap allocation, set it and we're almost done... */
766 if (new_brk <= brk_page) {
767 /* Heap contents are initialized to zero, as for anonymous
768 * mapped pages. */
769 if (new_brk > target_brk) {
770 memset(g2h(target_brk), 0, new_brk - target_brk);
772 target_brk = new_brk;
773 DEBUGF_BRK("%#010x (new_brk <= brk_page)\n", target_brk);
774 return target_brk;
777 /* We need to allocate more memory after the brk... Note that
778 * we don't use MAP_FIXED because that will map over the top of
779 * any existing mapping (like the one with the host libc or qemu
780 * itself); instead we treat "mapped but at wrong address" as
781 * a failure and unmap again.
783 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
784 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
785 PROT_READ|PROT_WRITE,
786 MAP_ANON|MAP_PRIVATE, 0, 0));
788 if (mapped_addr == brk_page) {
789 target_brk = new_brk;
790 brk_page = HOST_PAGE_ALIGN(target_brk);
791 DEBUGF_BRK("%#010x (mapped_addr == brk_page)\n", target_brk);
792 return target_brk;
793 } else if (mapped_addr != -1) {
794 /* Mapped but at wrong address, meaning there wasn't actually
795 * enough space for this brk.
797 target_munmap(mapped_addr, new_alloc_size);
798 mapped_addr = -1;
799 DEBUGF_BRK("%#010x (mapped_addr != -1)\n", target_brk);
801 else {
802 DEBUGF_BRK("%#010x (otherwise)\n", target_brk);
805 #if defined(TARGET_ALPHA)
806 /* We (partially) emulate OSF/1 on Alpha, which requires we
807 return a proper errno, not an unchanged brk value. */
808 return -TARGET_ENOMEM;
809 #endif
810 /* For everything else, return the previous break. */
811 return target_brk;
814 static inline abi_long copy_from_user_fdset(fd_set *fds,
815 abi_ulong target_fds_addr,
816 int n)
818 int i, nw, j, k;
819 abi_ulong b, *target_fds;
821 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
822 if (!(target_fds = lock_user(VERIFY_READ,
823 target_fds_addr,
824 sizeof(abi_ulong) * nw,
825 1)))
826 return -TARGET_EFAULT;
828 FD_ZERO(fds);
829 k = 0;
830 for (i = 0; i < nw; i++) {
831 /* grab the abi_ulong */
832 __get_user(b, &target_fds[i]);
833 for (j = 0; j < TARGET_ABI_BITS; j++) {
834 /* check the bit inside the abi_ulong */
835 if ((b >> j) & 1)
836 FD_SET(k, fds);
837 k++;
841 unlock_user(target_fds, target_fds_addr, 0);
843 return 0;
846 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
847 abi_ulong target_fds_addr,
848 int n)
850 if (target_fds_addr) {
851 if (copy_from_user_fdset(fds, target_fds_addr, n))
852 return -TARGET_EFAULT;
853 *fds_ptr = fds;
854 } else {
855 *fds_ptr = NULL;
857 return 0;
860 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
861 const fd_set *fds,
862 int n)
864 int i, nw, j, k;
865 abi_long v;
866 abi_ulong *target_fds;
868 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
869 if (!(target_fds = lock_user(VERIFY_WRITE,
870 target_fds_addr,
871 sizeof(abi_ulong) * nw,
872 0)))
873 return -TARGET_EFAULT;
875 k = 0;
876 for (i = 0; i < nw; i++) {
877 v = 0;
878 for (j = 0; j < TARGET_ABI_BITS; j++) {
879 v |= ((FD_ISSET(k, fds) != 0) << j);
880 k++;
882 __put_user(v, &target_fds[i]);
885 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
887 return 0;
890 #if defined(__alpha__)
891 #define HOST_HZ 1024
892 #else
893 #define HOST_HZ 100
894 #endif
896 static inline abi_long host_to_target_clock_t(long ticks)
898 #if HOST_HZ == TARGET_HZ
899 return ticks;
900 #else
901 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
902 #endif
905 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
906 const struct rusage *rusage)
908 struct target_rusage *target_rusage;
910 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
911 return -TARGET_EFAULT;
912 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
913 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
914 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
915 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
916 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
917 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
918 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
919 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
920 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
921 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
922 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
923 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
924 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
925 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
926 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
927 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
928 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
929 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
930 unlock_user_struct(target_rusage, target_addr, 1);
932 return 0;
935 static inline rlim_t target_to_host_rlim(target_ulong target_rlim)
937 target_ulong target_rlim_swap;
938 rlim_t result;
940 target_rlim_swap = tswapl(target_rlim);
941 if (target_rlim_swap == TARGET_RLIM_INFINITY || target_rlim_swap != (rlim_t)target_rlim_swap)
942 result = RLIM_INFINITY;
943 else
944 result = target_rlim_swap;
946 return result;
949 static inline target_ulong host_to_target_rlim(rlim_t rlim)
951 target_ulong target_rlim_swap;
952 target_ulong result;
954 if (rlim == RLIM_INFINITY || rlim != (target_long)rlim)
955 target_rlim_swap = TARGET_RLIM_INFINITY;
956 else
957 target_rlim_swap = rlim;
958 result = tswapl(target_rlim_swap);
960 return result;
963 static inline int target_to_host_resource(int code)
965 switch (code) {
966 case TARGET_RLIMIT_AS:
967 return RLIMIT_AS;
968 case TARGET_RLIMIT_CORE:
969 return RLIMIT_CORE;
970 case TARGET_RLIMIT_CPU:
971 return RLIMIT_CPU;
972 case TARGET_RLIMIT_DATA:
973 return RLIMIT_DATA;
974 case TARGET_RLIMIT_FSIZE:
975 return RLIMIT_FSIZE;
976 case TARGET_RLIMIT_LOCKS:
977 return RLIMIT_LOCKS;
978 case TARGET_RLIMIT_MEMLOCK:
979 return RLIMIT_MEMLOCK;
980 case TARGET_RLIMIT_MSGQUEUE:
981 return RLIMIT_MSGQUEUE;
982 case TARGET_RLIMIT_NICE:
983 return RLIMIT_NICE;
984 case TARGET_RLIMIT_NOFILE:
985 return RLIMIT_NOFILE;
986 case TARGET_RLIMIT_NPROC:
987 return RLIMIT_NPROC;
988 case TARGET_RLIMIT_RSS:
989 return RLIMIT_RSS;
990 case TARGET_RLIMIT_RTPRIO:
991 return RLIMIT_RTPRIO;
992 case TARGET_RLIMIT_SIGPENDING:
993 return RLIMIT_SIGPENDING;
994 case TARGET_RLIMIT_STACK:
995 return RLIMIT_STACK;
996 default:
997 return code;
1001 static inline abi_long copy_from_user_timeval(struct timeval *tv,
1002 abi_ulong target_tv_addr)
1004 struct target_timeval *target_tv;
1006 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
1007 return -TARGET_EFAULT;
1009 __get_user(tv->tv_sec, &target_tv->tv_sec);
1010 __get_user(tv->tv_usec, &target_tv->tv_usec);
1012 unlock_user_struct(target_tv, target_tv_addr, 0);
1014 return 0;
1017 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1018 const struct timeval *tv)
1020 struct target_timeval *target_tv;
1022 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
1023 return -TARGET_EFAULT;
1025 __put_user(tv->tv_sec, &target_tv->tv_sec);
1026 __put_user(tv->tv_usec, &target_tv->tv_usec);
1028 unlock_user_struct(target_tv, target_tv_addr, 1);
1030 return 0;
1033 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1034 #include <mqueue.h>
1036 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1037 abi_ulong target_mq_attr_addr)
1039 struct target_mq_attr *target_mq_attr;
1041 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1042 target_mq_attr_addr, 1))
1043 return -TARGET_EFAULT;
1045 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1046 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1047 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1048 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1050 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1052 return 0;
1055 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1056 const struct mq_attr *attr)
1058 struct target_mq_attr *target_mq_attr;
1060 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1061 target_mq_attr_addr, 0))
1062 return -TARGET_EFAULT;
1064 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1065 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1066 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1067 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1069 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1071 return 0;
1073 #endif
1075 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1076 /* do_select() must return target values and target errnos. */
1077 static abi_long do_select(int n,
1078 abi_ulong rfd_addr, abi_ulong wfd_addr,
1079 abi_ulong efd_addr, abi_ulong target_tv_addr)
1081 fd_set rfds, wfds, efds;
1082 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1083 struct timeval tv, *tv_ptr;
1084 abi_long ret;
1086 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1087 if (ret) {
1088 return ret;
1090 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1091 if (ret) {
1092 return ret;
1094 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1095 if (ret) {
1096 return ret;
1099 if (target_tv_addr) {
1100 if (copy_from_user_timeval(&tv, target_tv_addr))
1101 return -TARGET_EFAULT;
1102 tv_ptr = &tv;
1103 } else {
1104 tv_ptr = NULL;
1107 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
1109 if (!is_error(ret)) {
1110 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1111 return -TARGET_EFAULT;
1112 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1113 return -TARGET_EFAULT;
1114 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1115 return -TARGET_EFAULT;
1117 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1118 return -TARGET_EFAULT;
1121 return ret;
1123 #endif
1125 static abi_long do_pipe2(int host_pipe[], int flags)
1127 #ifdef CONFIG_PIPE2
1128 return pipe2(host_pipe, flags);
1129 #else
1130 return -ENOSYS;
1131 #endif
1134 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1135 int flags, int is_pipe2)
1137 int host_pipe[2];
1138 abi_long ret;
1139 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1141 if (is_error(ret))
1142 return get_errno(ret);
1144 /* Several targets have special calling conventions for the original
1145 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1146 if (!is_pipe2) {
1147 #if defined(TARGET_ALPHA)
1148 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1149 return host_pipe[0];
1150 #elif defined(TARGET_MIPS)
1151 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1152 return host_pipe[0];
1153 #elif defined(TARGET_SH4)
1154 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1155 return host_pipe[0];
1156 #endif
1159 if (put_user_s32(host_pipe[0], pipedes)
1160 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1161 return -TARGET_EFAULT;
1162 return get_errno(ret);
1165 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1166 abi_ulong target_addr,
1167 socklen_t len)
1169 struct target_ip_mreqn *target_smreqn;
1171 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1172 if (!target_smreqn)
1173 return -TARGET_EFAULT;
1174 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1175 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1176 if (len == sizeof(struct target_ip_mreqn))
1177 mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
1178 unlock_user(target_smreqn, target_addr, 0);
1180 return 0;
1183 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1184 abi_ulong target_addr,
1185 socklen_t len)
1187 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1188 sa_family_t sa_family;
1189 struct target_sockaddr *target_saddr;
1191 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1192 if (!target_saddr)
1193 return -TARGET_EFAULT;
1195 sa_family = tswap16(target_saddr->sa_family);
1197 /* Oops. The caller might send a incomplete sun_path; sun_path
1198 * must be terminated by \0 (see the manual page), but
1199 * unfortunately it is quite common to specify sockaddr_un
1200 * length as "strlen(x->sun_path)" while it should be
1201 * "strlen(...) + 1". We'll fix that here if needed.
1202 * Linux kernel has a similar feature.
1205 if (sa_family == AF_UNIX) {
1206 if (len < unix_maxlen && len > 0) {
1207 char *cp = (char*)target_saddr;
1209 if ( cp[len-1] && !cp[len] )
1210 len++;
1212 if (len > unix_maxlen)
1213 len = unix_maxlen;
1216 memcpy(addr, target_saddr, len);
1217 addr->sa_family = sa_family;
1218 unlock_user(target_saddr, target_addr, 0);
1220 return 0;
1223 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1224 struct sockaddr *addr,
1225 socklen_t len)
1227 struct target_sockaddr *target_saddr;
1229 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1230 if (!target_saddr)
1231 return -TARGET_EFAULT;
1232 memcpy(target_saddr, addr, len);
1233 target_saddr->sa_family = tswap16(addr->sa_family);
1234 unlock_user(target_saddr, target_addr, len);
1236 return 0;
1239 /* ??? Should this also swap msgh->name? */
1240 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1241 struct target_msghdr *target_msgh)
1243 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1244 abi_long msg_controllen;
1245 abi_ulong target_cmsg_addr;
1246 struct target_cmsghdr *target_cmsg;
1247 socklen_t space = 0;
1249 msg_controllen = tswapl(target_msgh->msg_controllen);
1250 if (msg_controllen < sizeof (struct target_cmsghdr))
1251 goto the_end;
1252 target_cmsg_addr = tswapl(target_msgh->msg_control);
1253 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1254 if (!target_cmsg)
1255 return -TARGET_EFAULT;
1257 while (cmsg && target_cmsg) {
1258 void *data = CMSG_DATA(cmsg);
1259 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1261 int len = tswapl(target_cmsg->cmsg_len)
1262 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1264 space += CMSG_SPACE(len);
1265 if (space > msgh->msg_controllen) {
1266 space -= CMSG_SPACE(len);
1267 gemu_log("Host cmsg overflow\n");
1268 break;
1271 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1272 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1273 cmsg->cmsg_len = CMSG_LEN(len);
1275 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1276 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1277 memcpy(data, target_data, len);
1278 } else {
1279 int *fd = (int *)data;
1280 int *target_fd = (int *)target_data;
1281 int i, numfds = len / sizeof(int);
1283 for (i = 0; i < numfds; i++)
1284 fd[i] = tswap32(target_fd[i]);
1287 cmsg = CMSG_NXTHDR(msgh, cmsg);
1288 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1290 unlock_user(target_cmsg, target_cmsg_addr, 0);
1291 the_end:
1292 msgh->msg_controllen = space;
1293 return 0;
1296 /* ??? Should this also swap msgh->name? */
1297 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1298 struct msghdr *msgh)
1300 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1301 abi_long msg_controllen;
1302 abi_ulong target_cmsg_addr;
1303 struct target_cmsghdr *target_cmsg;
1304 socklen_t space = 0;
1306 msg_controllen = tswapl(target_msgh->msg_controllen);
1307 if (msg_controllen < sizeof (struct target_cmsghdr))
1308 goto the_end;
1309 target_cmsg_addr = tswapl(target_msgh->msg_control);
1310 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1311 if (!target_cmsg)
1312 return -TARGET_EFAULT;
1314 while (cmsg && target_cmsg) {
1315 void *data = CMSG_DATA(cmsg);
1316 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1318 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1320 space += TARGET_CMSG_SPACE(len);
1321 if (space > msg_controllen) {
1322 space -= TARGET_CMSG_SPACE(len);
1323 gemu_log("Target cmsg overflow\n");
1324 break;
1327 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1328 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1329 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1331 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1332 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1333 memcpy(target_data, data, len);
1334 } else {
1335 int *fd = (int *)data;
1336 int *target_fd = (int *)target_data;
1337 int i, numfds = len / sizeof(int);
1339 for (i = 0; i < numfds; i++)
1340 target_fd[i] = tswap32(fd[i]);
1343 cmsg = CMSG_NXTHDR(msgh, cmsg);
1344 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1346 unlock_user(target_cmsg, target_cmsg_addr, space);
1347 the_end:
1348 target_msgh->msg_controllen = tswapl(space);
1349 return 0;
1352 /* do_setsockopt() Must return target values and target errnos. */
1353 static abi_long do_setsockopt(int sockfd, int level, int optname,
1354 abi_ulong optval_addr, socklen_t optlen)
1356 abi_long ret;
1357 int val;
1358 struct ip_mreqn *ip_mreq;
1359 struct ip_mreq_source *ip_mreq_source;
1361 switch(level) {
1362 case SOL_TCP:
1363 /* TCP options all take an 'int' value. */
1364 if (optlen < sizeof(uint32_t))
1365 return -TARGET_EINVAL;
1367 if (get_user_u32(val, optval_addr))
1368 return -TARGET_EFAULT;
1369 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1370 break;
1371 case SOL_IP:
1372 switch(optname) {
1373 case IP_TOS:
1374 case IP_TTL:
1375 case IP_HDRINCL:
1376 case IP_ROUTER_ALERT:
1377 case IP_RECVOPTS:
1378 case IP_RETOPTS:
1379 case IP_PKTINFO:
1380 case IP_MTU_DISCOVER:
1381 case IP_RECVERR:
1382 case IP_RECVTOS:
1383 #ifdef IP_FREEBIND
1384 case IP_FREEBIND:
1385 #endif
1386 case IP_MULTICAST_TTL:
1387 case IP_MULTICAST_LOOP:
1388 val = 0;
1389 if (optlen >= sizeof(uint32_t)) {
1390 if (get_user_u32(val, optval_addr))
1391 return -TARGET_EFAULT;
1392 } else if (optlen >= 1) {
1393 if (get_user_u8(val, optval_addr))
1394 return -TARGET_EFAULT;
1396 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1397 break;
1398 case IP_ADD_MEMBERSHIP:
1399 case IP_DROP_MEMBERSHIP:
1400 if (optlen < sizeof (struct target_ip_mreq) ||
1401 optlen > sizeof (struct target_ip_mreqn))
1402 return -TARGET_EINVAL;
1404 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1405 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1406 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1407 break;
1409 case IP_BLOCK_SOURCE:
1410 case IP_UNBLOCK_SOURCE:
1411 case IP_ADD_SOURCE_MEMBERSHIP:
1412 case IP_DROP_SOURCE_MEMBERSHIP:
1413 if (optlen != sizeof (struct target_ip_mreq_source))
1414 return -TARGET_EINVAL;
1416 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1417 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1418 unlock_user (ip_mreq_source, optval_addr, 0);
1419 break;
1421 default:
1422 goto unimplemented;
1424 break;
1425 case TARGET_SOL_SOCKET:
1426 switch (optname) {
1427 /* Options with 'int' argument. */
1428 case TARGET_SO_DEBUG:
1429 optname = SO_DEBUG;
1430 break;
1431 case TARGET_SO_REUSEADDR:
1432 optname = SO_REUSEADDR;
1433 break;
1434 case TARGET_SO_TYPE:
1435 optname = SO_TYPE;
1436 break;
1437 case TARGET_SO_ERROR:
1438 optname = SO_ERROR;
1439 break;
1440 case TARGET_SO_DONTROUTE:
1441 optname = SO_DONTROUTE;
1442 break;
1443 case TARGET_SO_BROADCAST:
1444 optname = SO_BROADCAST;
1445 break;
1446 case TARGET_SO_SNDBUF:
1447 optname = SO_SNDBUF;
1448 break;
1449 case TARGET_SO_RCVBUF:
1450 optname = SO_RCVBUF;
1451 break;
1452 case TARGET_SO_KEEPALIVE:
1453 optname = SO_KEEPALIVE;
1454 break;
1455 case TARGET_SO_OOBINLINE:
1456 optname = SO_OOBINLINE;
1457 break;
1458 case TARGET_SO_NO_CHECK:
1459 optname = SO_NO_CHECK;
1460 break;
1461 case TARGET_SO_PRIORITY:
1462 optname = SO_PRIORITY;
1463 break;
1464 #ifdef SO_BSDCOMPAT
1465 case TARGET_SO_BSDCOMPAT:
1466 optname = SO_BSDCOMPAT;
1467 break;
1468 #endif
1469 case TARGET_SO_PASSCRED:
1470 optname = SO_PASSCRED;
1471 break;
1472 case TARGET_SO_TIMESTAMP:
1473 optname = SO_TIMESTAMP;
1474 break;
1475 case TARGET_SO_RCVLOWAT:
1476 optname = SO_RCVLOWAT;
1477 break;
1478 case TARGET_SO_RCVTIMEO:
1479 optname = SO_RCVTIMEO;
1480 break;
1481 case TARGET_SO_SNDTIMEO:
1482 optname = SO_SNDTIMEO;
1483 break;
1484 break;
1485 default:
1486 goto unimplemented;
1488 if (optlen < sizeof(uint32_t))
1489 return -TARGET_EINVAL;
1491 if (get_user_u32(val, optval_addr))
1492 return -TARGET_EFAULT;
1493 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
1494 break;
1495 default:
1496 unimplemented:
1497 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
1498 ret = -TARGET_ENOPROTOOPT;
1500 return ret;
1503 /* do_getsockopt() Must return target values and target errnos. */
1504 static abi_long do_getsockopt(int sockfd, int level, int optname,
1505 abi_ulong optval_addr, abi_ulong optlen)
1507 abi_long ret;
1508 int len, val;
1509 socklen_t lv;
1511 switch(level) {
1512 case TARGET_SOL_SOCKET:
1513 level = SOL_SOCKET;
1514 switch (optname) {
1515 /* These don't just return a single integer */
1516 case TARGET_SO_LINGER:
1517 case TARGET_SO_RCVTIMEO:
1518 case TARGET_SO_SNDTIMEO:
1519 case TARGET_SO_PEERCRED:
1520 case TARGET_SO_PEERNAME:
1521 goto unimplemented;
1522 /* Options with 'int' argument. */
1523 case TARGET_SO_DEBUG:
1524 optname = SO_DEBUG;
1525 goto int_case;
1526 case TARGET_SO_REUSEADDR:
1527 optname = SO_REUSEADDR;
1528 goto int_case;
1529 case TARGET_SO_TYPE:
1530 optname = SO_TYPE;
1531 goto int_case;
1532 case TARGET_SO_ERROR:
1533 optname = SO_ERROR;
1534 goto int_case;
1535 case TARGET_SO_DONTROUTE:
1536 optname = SO_DONTROUTE;
1537 goto int_case;
1538 case TARGET_SO_BROADCAST:
1539 optname = SO_BROADCAST;
1540 goto int_case;
1541 case TARGET_SO_SNDBUF:
1542 optname = SO_SNDBUF;
1543 goto int_case;
1544 case TARGET_SO_RCVBUF:
1545 optname = SO_RCVBUF;
1546 goto int_case;
1547 case TARGET_SO_KEEPALIVE:
1548 optname = SO_KEEPALIVE;
1549 goto int_case;
1550 case TARGET_SO_OOBINLINE:
1551 optname = SO_OOBINLINE;
1552 goto int_case;
1553 case TARGET_SO_NO_CHECK:
1554 optname = SO_NO_CHECK;
1555 goto int_case;
1556 case TARGET_SO_PRIORITY:
1557 optname = SO_PRIORITY;
1558 goto int_case;
1559 #ifdef SO_BSDCOMPAT
1560 case TARGET_SO_BSDCOMPAT:
1561 optname = SO_BSDCOMPAT;
1562 goto int_case;
1563 #endif
1564 case TARGET_SO_PASSCRED:
1565 optname = SO_PASSCRED;
1566 goto int_case;
1567 case TARGET_SO_TIMESTAMP:
1568 optname = SO_TIMESTAMP;
1569 goto int_case;
1570 case TARGET_SO_RCVLOWAT:
1571 optname = SO_RCVLOWAT;
1572 goto int_case;
1573 default:
1574 goto int_case;
1576 break;
1577 case SOL_TCP:
1578 /* TCP options all take an 'int' value. */
1579 int_case:
1580 if (get_user_u32(len, optlen))
1581 return -TARGET_EFAULT;
1582 if (len < 0)
1583 return -TARGET_EINVAL;
1584 lv = sizeof(lv);
1585 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1586 if (ret < 0)
1587 return ret;
1588 if (len > lv)
1589 len = lv;
1590 if (len == 4) {
1591 if (put_user_u32(val, optval_addr))
1592 return -TARGET_EFAULT;
1593 } else {
1594 if (put_user_u8(val, optval_addr))
1595 return -TARGET_EFAULT;
1597 if (put_user_u32(len, optlen))
1598 return -TARGET_EFAULT;
1599 break;
1600 case SOL_IP:
1601 switch(optname) {
1602 case IP_TOS:
1603 case IP_TTL:
1604 case IP_HDRINCL:
1605 case IP_ROUTER_ALERT:
1606 case IP_RECVOPTS:
1607 case IP_RETOPTS:
1608 case IP_PKTINFO:
1609 case IP_MTU_DISCOVER:
1610 case IP_RECVERR:
1611 case IP_RECVTOS:
1612 #ifdef IP_FREEBIND
1613 case IP_FREEBIND:
1614 #endif
1615 case IP_MULTICAST_TTL:
1616 case IP_MULTICAST_LOOP:
1617 if (get_user_u32(len, optlen))
1618 return -TARGET_EFAULT;
1619 if (len < 0)
1620 return -TARGET_EINVAL;
1621 lv = sizeof(lv);
1622 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1623 if (ret < 0)
1624 return ret;
1625 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1626 len = 1;
1627 if (put_user_u32(len, optlen)
1628 || put_user_u8(val, optval_addr))
1629 return -TARGET_EFAULT;
1630 } else {
1631 if (len > sizeof(int))
1632 len = sizeof(int);
1633 if (put_user_u32(len, optlen)
1634 || put_user_u32(val, optval_addr))
1635 return -TARGET_EFAULT;
1637 break;
1638 default:
1639 ret = -TARGET_ENOPROTOOPT;
1640 break;
1642 break;
1643 default:
1644 unimplemented:
1645 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1646 level, optname);
1647 ret = -TARGET_EOPNOTSUPP;
1648 break;
1650 return ret;
1653 /* FIXME
1654 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1655 * other lock functions have a return code of 0 for failure.
1657 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1658 int count, int copy)
1660 struct target_iovec *target_vec;
1661 abi_ulong base;
1662 int i;
1664 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1665 if (!target_vec)
1666 return -TARGET_EFAULT;
1667 for(i = 0;i < count; i++) {
1668 base = tswapl(target_vec[i].iov_base);
1669 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1670 if (vec[i].iov_len != 0) {
1671 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1672 /* Don't check lock_user return value. We must call writev even
1673 if a element has invalid base address. */
1674 } else {
1675 /* zero length pointer is ignored */
1676 vec[i].iov_base = NULL;
1679 unlock_user (target_vec, target_addr, 0);
1680 return 0;
1683 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1684 int count, int copy)
1686 struct target_iovec *target_vec;
1687 abi_ulong base;
1688 int i;
1690 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1691 if (!target_vec)
1692 return -TARGET_EFAULT;
1693 for(i = 0;i < count; i++) {
1694 if (target_vec[i].iov_base) {
1695 base = tswapl(target_vec[i].iov_base);
1696 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1699 unlock_user (target_vec, target_addr, 0);
1701 return 0;
1704 /* do_socket() Must return target values and target errnos. */
1705 static abi_long do_socket(int domain, int type, int protocol)
1707 #if defined(TARGET_MIPS)
1708 switch(type) {
1709 case TARGET_SOCK_DGRAM:
1710 type = SOCK_DGRAM;
1711 break;
1712 case TARGET_SOCK_STREAM:
1713 type = SOCK_STREAM;
1714 break;
1715 case TARGET_SOCK_RAW:
1716 type = SOCK_RAW;
1717 break;
1718 case TARGET_SOCK_RDM:
1719 type = SOCK_RDM;
1720 break;
1721 case TARGET_SOCK_SEQPACKET:
1722 type = SOCK_SEQPACKET;
1723 break;
1724 case TARGET_SOCK_PACKET:
1725 type = SOCK_PACKET;
1726 break;
1728 #endif
1729 if (domain == PF_NETLINK)
1730 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1731 return get_errno(socket(domain, type, protocol));
1734 /* do_bind() Must return target values and target errnos. */
1735 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1736 socklen_t addrlen)
1738 void *addr;
1739 abi_long ret;
1741 if ((int)addrlen < 0) {
1742 return -TARGET_EINVAL;
1745 addr = alloca(addrlen+1);
1747 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1748 if (ret)
1749 return ret;
1751 return get_errno(bind(sockfd, addr, addrlen));
1754 /* do_connect() Must return target values and target errnos. */
1755 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1756 socklen_t addrlen)
1758 void *addr;
1759 abi_long ret;
1761 if ((int)addrlen < 0) {
1762 return -TARGET_EINVAL;
1765 addr = alloca(addrlen);
1767 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1768 if (ret)
1769 return ret;
1771 return get_errno(connect(sockfd, addr, addrlen));
1774 /* do_sendrecvmsg() Must return target values and target errnos. */
1775 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1776 int flags, int send)
1778 abi_long ret, len;
1779 struct target_msghdr *msgp;
1780 struct msghdr msg;
1781 int count;
1782 struct iovec *vec;
1783 abi_ulong target_vec;
1785 /* FIXME */
1786 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1787 msgp,
1788 target_msg,
1789 send ? 1 : 0))
1790 return -TARGET_EFAULT;
1791 if (msgp->msg_name) {
1792 msg.msg_namelen = tswap32(msgp->msg_namelen);
1793 msg.msg_name = alloca(msg.msg_namelen);
1794 ret = target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1795 msg.msg_namelen);
1796 if (ret) {
1797 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1798 return ret;
1800 } else {
1801 msg.msg_name = NULL;
1802 msg.msg_namelen = 0;
1804 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1805 msg.msg_control = alloca(msg.msg_controllen);
1806 msg.msg_flags = tswap32(msgp->msg_flags);
1808 count = tswapl(msgp->msg_iovlen);
1809 vec = alloca(count * sizeof(struct iovec));
1810 target_vec = tswapl(msgp->msg_iov);
1811 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1812 msg.msg_iovlen = count;
1813 msg.msg_iov = vec;
1815 if (send) {
1816 ret = target_to_host_cmsg(&msg, msgp);
1817 if (ret == 0)
1818 ret = get_errno(sendmsg(fd, &msg, flags));
1819 } else {
1820 ret = get_errno(recvmsg(fd, &msg, flags));
1821 if (!is_error(ret)) {
1822 len = ret;
1823 ret = host_to_target_cmsg(msgp, &msg);
1824 if (!is_error(ret))
1825 ret = len;
1828 unlock_iovec(vec, target_vec, count, !send);
1829 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1830 return ret;
1833 /* do_accept() Must return target values and target errnos. */
1834 static abi_long do_accept(int fd, abi_ulong target_addr,
1835 abi_ulong target_addrlen_addr)
1837 socklen_t addrlen;
1838 void *addr;
1839 abi_long ret;
1841 if (target_addr == 0)
1842 return get_errno(accept(fd, NULL, NULL));
1844 /* linux returns EINVAL if addrlen pointer is invalid */
1845 if (get_user_u32(addrlen, target_addrlen_addr))
1846 return -TARGET_EINVAL;
1848 if ((int)addrlen < 0) {
1849 return -TARGET_EINVAL;
1852 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1853 return -TARGET_EINVAL;
1855 addr = alloca(addrlen);
1857 ret = get_errno(accept(fd, addr, &addrlen));
1858 if (!is_error(ret)) {
1859 host_to_target_sockaddr(target_addr, addr, addrlen);
1860 if (put_user_u32(addrlen, target_addrlen_addr))
1861 ret = -TARGET_EFAULT;
1863 return ret;
1866 /* do_getpeername() Must return target values and target errnos. */
1867 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1868 abi_ulong target_addrlen_addr)
1870 socklen_t addrlen;
1871 void *addr;
1872 abi_long ret;
1874 if (get_user_u32(addrlen, target_addrlen_addr))
1875 return -TARGET_EFAULT;
1877 if ((int)addrlen < 0) {
1878 return -TARGET_EINVAL;
1881 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1882 return -TARGET_EFAULT;
1884 addr = alloca(addrlen);
1886 ret = get_errno(getpeername(fd, addr, &addrlen));
1887 if (!is_error(ret)) {
1888 host_to_target_sockaddr(target_addr, addr, addrlen);
1889 if (put_user_u32(addrlen, target_addrlen_addr))
1890 ret = -TARGET_EFAULT;
1892 return ret;
1895 /* do_getsockname() Must return target values and target errnos. */
1896 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1897 abi_ulong target_addrlen_addr)
1899 socklen_t addrlen;
1900 void *addr;
1901 abi_long ret;
1903 if (get_user_u32(addrlen, target_addrlen_addr))
1904 return -TARGET_EFAULT;
1906 if ((int)addrlen < 0) {
1907 return -TARGET_EINVAL;
1910 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1911 return -TARGET_EFAULT;
1913 addr = alloca(addrlen);
1915 ret = get_errno(getsockname(fd, addr, &addrlen));
1916 if (!is_error(ret)) {
1917 host_to_target_sockaddr(target_addr, addr, addrlen);
1918 if (put_user_u32(addrlen, target_addrlen_addr))
1919 ret = -TARGET_EFAULT;
1921 return ret;
1924 /* do_socketpair() Must return target values and target errnos. */
1925 static abi_long do_socketpair(int domain, int type, int protocol,
1926 abi_ulong target_tab_addr)
1928 int tab[2];
1929 abi_long ret;
1931 ret = get_errno(socketpair(domain, type, protocol, tab));
1932 if (!is_error(ret)) {
1933 if (put_user_s32(tab[0], target_tab_addr)
1934 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1935 ret = -TARGET_EFAULT;
1937 return ret;
1940 /* do_sendto() Must return target values and target errnos. */
1941 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1942 abi_ulong target_addr, socklen_t addrlen)
1944 void *addr;
1945 void *host_msg;
1946 abi_long ret;
1948 if ((int)addrlen < 0) {
1949 return -TARGET_EINVAL;
1952 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1953 if (!host_msg)
1954 return -TARGET_EFAULT;
1955 if (target_addr) {
1956 addr = alloca(addrlen);
1957 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1958 if (ret) {
1959 unlock_user(host_msg, msg, 0);
1960 return ret;
1962 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1963 } else {
1964 ret = get_errno(send(fd, host_msg, len, flags));
1966 unlock_user(host_msg, msg, 0);
1967 return ret;
1970 /* do_recvfrom() Must return target values and target errnos. */
1971 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1972 abi_ulong target_addr,
1973 abi_ulong target_addrlen)
1975 socklen_t addrlen;
1976 void *addr;
1977 void *host_msg;
1978 abi_long ret;
1980 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1981 if (!host_msg)
1982 return -TARGET_EFAULT;
1983 if (target_addr) {
1984 if (get_user_u32(addrlen, target_addrlen)) {
1985 ret = -TARGET_EFAULT;
1986 goto fail;
1988 if ((int)addrlen < 0) {
1989 ret = -TARGET_EINVAL;
1990 goto fail;
1992 addr = alloca(addrlen);
1993 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1994 } else {
1995 addr = NULL; /* To keep compiler quiet. */
1996 ret = get_errno(recv(fd, host_msg, len, flags));
1998 if (!is_error(ret)) {
1999 if (target_addr) {
2000 host_to_target_sockaddr(target_addr, addr, addrlen);
2001 if (put_user_u32(addrlen, target_addrlen)) {
2002 ret = -TARGET_EFAULT;
2003 goto fail;
2006 unlock_user(host_msg, msg, len);
2007 } else {
2008 fail:
2009 unlock_user(host_msg, msg, 0);
2011 return ret;
2014 #ifdef TARGET_NR_socketcall
2015 /* do_socketcall() Must return target values and target errnos. */
2016 static abi_long do_socketcall(int num, abi_ulong vptr)
2018 abi_long ret;
2019 const int n = sizeof(abi_ulong);
2021 switch(num) {
2022 case SOCKOP_socket:
2024 abi_ulong domain, type, protocol;
2026 if (get_user_ual(domain, vptr)
2027 || get_user_ual(type, vptr + n)
2028 || get_user_ual(protocol, vptr + 2 * n))
2029 return -TARGET_EFAULT;
2031 ret = do_socket(domain, type, protocol);
2033 break;
2034 case SOCKOP_bind:
2036 abi_ulong sockfd;
2037 abi_ulong target_addr;
2038 socklen_t addrlen;
2040 if (get_user_ual(sockfd, vptr)
2041 || get_user_ual(target_addr, vptr + n)
2042 || get_user_ual(addrlen, vptr + 2 * n))
2043 return -TARGET_EFAULT;
2045 ret = do_bind(sockfd, target_addr, addrlen);
2047 break;
2048 case SOCKOP_connect:
2050 abi_ulong sockfd;
2051 abi_ulong target_addr;
2052 socklen_t addrlen;
2054 if (get_user_ual(sockfd, vptr)
2055 || get_user_ual(target_addr, vptr + n)
2056 || get_user_ual(addrlen, vptr + 2 * n))
2057 return -TARGET_EFAULT;
2059 ret = do_connect(sockfd, target_addr, addrlen);
2061 break;
2062 case SOCKOP_listen:
2064 abi_ulong sockfd, backlog;
2066 if (get_user_ual(sockfd, vptr)
2067 || get_user_ual(backlog, vptr + n))
2068 return -TARGET_EFAULT;
2070 ret = get_errno(listen(sockfd, backlog));
2072 break;
2073 case SOCKOP_accept:
2075 abi_ulong sockfd;
2076 abi_ulong target_addr, target_addrlen;
2078 if (get_user_ual(sockfd, vptr)
2079 || get_user_ual(target_addr, vptr + n)
2080 || get_user_ual(target_addrlen, vptr + 2 * n))
2081 return -TARGET_EFAULT;
2083 ret = do_accept(sockfd, target_addr, target_addrlen);
2085 break;
2086 case SOCKOP_getsockname:
2088 abi_ulong sockfd;
2089 abi_ulong target_addr, target_addrlen;
2091 if (get_user_ual(sockfd, vptr)
2092 || get_user_ual(target_addr, vptr + n)
2093 || get_user_ual(target_addrlen, vptr + 2 * n))
2094 return -TARGET_EFAULT;
2096 ret = do_getsockname(sockfd, target_addr, target_addrlen);
2098 break;
2099 case SOCKOP_getpeername:
2101 abi_ulong sockfd;
2102 abi_ulong target_addr, target_addrlen;
2104 if (get_user_ual(sockfd, vptr)
2105 || get_user_ual(target_addr, vptr + n)
2106 || get_user_ual(target_addrlen, vptr + 2 * n))
2107 return -TARGET_EFAULT;
2109 ret = do_getpeername(sockfd, target_addr, target_addrlen);
2111 break;
2112 case SOCKOP_socketpair:
2114 abi_ulong domain, type, protocol;
2115 abi_ulong tab;
2117 if (get_user_ual(domain, vptr)
2118 || get_user_ual(type, vptr + n)
2119 || get_user_ual(protocol, vptr + 2 * n)
2120 || get_user_ual(tab, vptr + 3 * n))
2121 return -TARGET_EFAULT;
2123 ret = do_socketpair(domain, type, protocol, tab);
2125 break;
2126 case SOCKOP_send:
2128 abi_ulong sockfd;
2129 abi_ulong msg;
2130 size_t len;
2131 abi_ulong flags;
2133 if (get_user_ual(sockfd, vptr)
2134 || get_user_ual(msg, vptr + n)
2135 || get_user_ual(len, vptr + 2 * n)
2136 || get_user_ual(flags, vptr + 3 * n))
2137 return -TARGET_EFAULT;
2139 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
2141 break;
2142 case SOCKOP_recv:
2144 abi_ulong sockfd;
2145 abi_ulong msg;
2146 size_t len;
2147 abi_ulong flags;
2149 if (get_user_ual(sockfd, vptr)
2150 || get_user_ual(msg, vptr + n)
2151 || get_user_ual(len, vptr + 2 * n)
2152 || get_user_ual(flags, vptr + 3 * n))
2153 return -TARGET_EFAULT;
2155 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
2157 break;
2158 case SOCKOP_sendto:
2160 abi_ulong sockfd;
2161 abi_ulong msg;
2162 size_t len;
2163 abi_ulong flags;
2164 abi_ulong addr;
2165 socklen_t addrlen;
2167 if (get_user_ual(sockfd, vptr)
2168 || get_user_ual(msg, vptr + n)
2169 || get_user_ual(len, vptr + 2 * n)
2170 || get_user_ual(flags, vptr + 3 * n)
2171 || get_user_ual(addr, vptr + 4 * n)
2172 || get_user_ual(addrlen, vptr + 5 * n))
2173 return -TARGET_EFAULT;
2175 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
2177 break;
2178 case SOCKOP_recvfrom:
2180 abi_ulong sockfd;
2181 abi_ulong msg;
2182 size_t len;
2183 abi_ulong flags;
2184 abi_ulong addr;
2185 socklen_t addrlen;
2187 if (get_user_ual(sockfd, vptr)
2188 || get_user_ual(msg, vptr + n)
2189 || get_user_ual(len, vptr + 2 * n)
2190 || get_user_ual(flags, vptr + 3 * n)
2191 || get_user_ual(addr, vptr + 4 * n)
2192 || get_user_ual(addrlen, vptr + 5 * n))
2193 return -TARGET_EFAULT;
2195 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
2197 break;
2198 case SOCKOP_shutdown:
2200 abi_ulong sockfd, how;
2202 if (get_user_ual(sockfd, vptr)
2203 || get_user_ual(how, vptr + n))
2204 return -TARGET_EFAULT;
2206 ret = get_errno(shutdown(sockfd, how));
2208 break;
2209 case SOCKOP_sendmsg:
2210 case SOCKOP_recvmsg:
2212 abi_ulong fd;
2213 abi_ulong target_msg;
2214 abi_ulong flags;
2216 if (get_user_ual(fd, vptr)
2217 || get_user_ual(target_msg, vptr + n)
2218 || get_user_ual(flags, vptr + 2 * n))
2219 return -TARGET_EFAULT;
2221 ret = do_sendrecvmsg(fd, target_msg, flags,
2222 (num == SOCKOP_sendmsg));
2224 break;
2225 case SOCKOP_setsockopt:
2227 abi_ulong sockfd;
2228 abi_ulong level;
2229 abi_ulong optname;
2230 abi_ulong optval;
2231 socklen_t optlen;
2233 if (get_user_ual(sockfd, vptr)
2234 || get_user_ual(level, vptr + n)
2235 || get_user_ual(optname, vptr + 2 * n)
2236 || get_user_ual(optval, vptr + 3 * n)
2237 || get_user_ual(optlen, vptr + 4 * n))
2238 return -TARGET_EFAULT;
2240 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2242 break;
2243 case SOCKOP_getsockopt:
2245 abi_ulong sockfd;
2246 abi_ulong level;
2247 abi_ulong optname;
2248 abi_ulong optval;
2249 socklen_t optlen;
2251 if (get_user_ual(sockfd, vptr)
2252 || get_user_ual(level, vptr + n)
2253 || get_user_ual(optname, vptr + 2 * n)
2254 || get_user_ual(optval, vptr + 3 * n)
2255 || get_user_ual(optlen, vptr + 4 * n))
2256 return -TARGET_EFAULT;
2258 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
2260 break;
2261 default:
2262 gemu_log("Unsupported socketcall: %d\n", num);
2263 ret = -TARGET_ENOSYS;
2264 break;
2266 return ret;
2268 #endif
2270 #define N_SHM_REGIONS 32
2272 static struct shm_region {
2273 abi_ulong start;
2274 abi_ulong size;
2275 } shm_regions[N_SHM_REGIONS];
2277 struct target_ipc_perm
2279 abi_long __key;
2280 abi_ulong uid;
2281 abi_ulong gid;
2282 abi_ulong cuid;
2283 abi_ulong cgid;
2284 unsigned short int mode;
2285 unsigned short int __pad1;
2286 unsigned short int __seq;
2287 unsigned short int __pad2;
2288 abi_ulong __unused1;
2289 abi_ulong __unused2;
2292 struct target_semid_ds
2294 struct target_ipc_perm sem_perm;
2295 abi_ulong sem_otime;
2296 abi_ulong __unused1;
2297 abi_ulong sem_ctime;
2298 abi_ulong __unused2;
2299 abi_ulong sem_nsems;
2300 abi_ulong __unused3;
2301 abi_ulong __unused4;
2304 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2305 abi_ulong target_addr)
2307 struct target_ipc_perm *target_ip;
2308 struct target_semid_ds *target_sd;
2310 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2311 return -TARGET_EFAULT;
2312 target_ip = &(target_sd->sem_perm);
2313 host_ip->__key = tswapl(target_ip->__key);
2314 host_ip->uid = tswapl(target_ip->uid);
2315 host_ip->gid = tswapl(target_ip->gid);
2316 host_ip->cuid = tswapl(target_ip->cuid);
2317 host_ip->cgid = tswapl(target_ip->cgid);
2318 host_ip->mode = tswapl(target_ip->mode);
2319 unlock_user_struct(target_sd, target_addr, 0);
2320 return 0;
2323 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2324 struct ipc_perm *host_ip)
2326 struct target_ipc_perm *target_ip;
2327 struct target_semid_ds *target_sd;
2329 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2330 return -TARGET_EFAULT;
2331 target_ip = &(target_sd->sem_perm);
2332 target_ip->__key = tswapl(host_ip->__key);
2333 target_ip->uid = tswapl(host_ip->uid);
2334 target_ip->gid = tswapl(host_ip->gid);
2335 target_ip->cuid = tswapl(host_ip->cuid);
2336 target_ip->cgid = tswapl(host_ip->cgid);
2337 target_ip->mode = tswapl(host_ip->mode);
2338 unlock_user_struct(target_sd, target_addr, 1);
2339 return 0;
2342 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2343 abi_ulong target_addr)
2345 struct target_semid_ds *target_sd;
2347 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2348 return -TARGET_EFAULT;
2349 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2350 return -TARGET_EFAULT;
2351 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2352 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2353 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2354 unlock_user_struct(target_sd, target_addr, 0);
2355 return 0;
2358 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2359 struct semid_ds *host_sd)
2361 struct target_semid_ds *target_sd;
2363 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2364 return -TARGET_EFAULT;
2365 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2366 return -TARGET_EFAULT;;
2367 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2368 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2369 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2370 unlock_user_struct(target_sd, target_addr, 1);
2371 return 0;
2374 struct target_seminfo {
2375 int semmap;
2376 int semmni;
2377 int semmns;
2378 int semmnu;
2379 int semmsl;
2380 int semopm;
2381 int semume;
2382 int semusz;
2383 int semvmx;
2384 int semaem;
2387 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2388 struct seminfo *host_seminfo)
2390 struct target_seminfo *target_seminfo;
2391 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2392 return -TARGET_EFAULT;
2393 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2394 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2395 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2396 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2397 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2398 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2399 __put_user(host_seminfo->semume, &target_seminfo->semume);
2400 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2401 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2402 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2403 unlock_user_struct(target_seminfo, target_addr, 1);
2404 return 0;
2407 union semun {
2408 int val;
2409 struct semid_ds *buf;
2410 unsigned short *array;
2411 struct seminfo *__buf;
2414 union target_semun {
2415 int val;
2416 abi_ulong buf;
2417 abi_ulong array;
2418 abi_ulong __buf;
2421 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2422 abi_ulong target_addr)
2424 int nsems;
2425 unsigned short *array;
2426 union semun semun;
2427 struct semid_ds semid_ds;
2428 int i, ret;
2430 semun.buf = &semid_ds;
2432 ret = semctl(semid, 0, IPC_STAT, semun);
2433 if (ret == -1)
2434 return get_errno(ret);
2436 nsems = semid_ds.sem_nsems;
2438 *host_array = malloc(nsems*sizeof(unsigned short));
2439 array = lock_user(VERIFY_READ, target_addr,
2440 nsems*sizeof(unsigned short), 1);
2441 if (!array)
2442 return -TARGET_EFAULT;
2444 for(i=0; i<nsems; i++) {
2445 __get_user((*host_array)[i], &array[i]);
2447 unlock_user(array, target_addr, 0);
2449 return 0;
2452 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2453 unsigned short **host_array)
2455 int nsems;
2456 unsigned short *array;
2457 union semun semun;
2458 struct semid_ds semid_ds;
2459 int i, ret;
2461 semun.buf = &semid_ds;
2463 ret = semctl(semid, 0, IPC_STAT, semun);
2464 if (ret == -1)
2465 return get_errno(ret);
2467 nsems = semid_ds.sem_nsems;
2469 array = lock_user(VERIFY_WRITE, target_addr,
2470 nsems*sizeof(unsigned short), 0);
2471 if (!array)
2472 return -TARGET_EFAULT;
2474 for(i=0; i<nsems; i++) {
2475 __put_user((*host_array)[i], &array[i]);
2477 free(*host_array);
2478 unlock_user(array, target_addr, 1);
2480 return 0;
2483 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2484 union target_semun target_su)
2486 union semun arg;
2487 struct semid_ds dsarg;
2488 unsigned short *array = NULL;
2489 struct seminfo seminfo;
2490 abi_long ret = -TARGET_EINVAL;
2491 abi_long err;
2492 cmd &= 0xff;
2494 switch( cmd ) {
2495 case GETVAL:
2496 case SETVAL:
2497 arg.val = tswapl(target_su.val);
2498 ret = get_errno(semctl(semid, semnum, cmd, arg));
2499 target_su.val = tswapl(arg.val);
2500 break;
2501 case GETALL:
2502 case SETALL:
2503 err = target_to_host_semarray(semid, &array, target_su.array);
2504 if (err)
2505 return err;
2506 arg.array = array;
2507 ret = get_errno(semctl(semid, semnum, cmd, arg));
2508 err = host_to_target_semarray(semid, target_su.array, &array);
2509 if (err)
2510 return err;
2511 break;
2512 case IPC_STAT:
2513 case IPC_SET:
2514 case SEM_STAT:
2515 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2516 if (err)
2517 return err;
2518 arg.buf = &dsarg;
2519 ret = get_errno(semctl(semid, semnum, cmd, arg));
2520 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2521 if (err)
2522 return err;
2523 break;
2524 case IPC_INFO:
2525 case SEM_INFO:
2526 arg.__buf = &seminfo;
2527 ret = get_errno(semctl(semid, semnum, cmd, arg));
2528 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2529 if (err)
2530 return err;
2531 break;
2532 case IPC_RMID:
2533 case GETPID:
2534 case GETNCNT:
2535 case GETZCNT:
2536 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2537 break;
2540 return ret;
2543 struct target_sembuf {
2544 unsigned short sem_num;
2545 short sem_op;
2546 short sem_flg;
2549 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2550 abi_ulong target_addr,
2551 unsigned nsops)
2553 struct target_sembuf *target_sembuf;
2554 int i;
2556 target_sembuf = lock_user(VERIFY_READ, target_addr,
2557 nsops*sizeof(struct target_sembuf), 1);
2558 if (!target_sembuf)
2559 return -TARGET_EFAULT;
2561 for(i=0; i<nsops; i++) {
2562 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2563 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2564 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2567 unlock_user(target_sembuf, target_addr, 0);
2569 return 0;
2572 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2574 struct sembuf sops[nsops];
2576 if (target_to_host_sembuf(sops, ptr, nsops))
2577 return -TARGET_EFAULT;
2579 return semop(semid, sops, nsops);
2582 struct target_msqid_ds
2584 struct target_ipc_perm msg_perm;
2585 abi_ulong msg_stime;
2586 #if TARGET_ABI_BITS == 32
2587 abi_ulong __unused1;
2588 #endif
2589 abi_ulong msg_rtime;
2590 #if TARGET_ABI_BITS == 32
2591 abi_ulong __unused2;
2592 #endif
2593 abi_ulong msg_ctime;
2594 #if TARGET_ABI_BITS == 32
2595 abi_ulong __unused3;
2596 #endif
2597 abi_ulong __msg_cbytes;
2598 abi_ulong msg_qnum;
2599 abi_ulong msg_qbytes;
2600 abi_ulong msg_lspid;
2601 abi_ulong msg_lrpid;
2602 abi_ulong __unused4;
2603 abi_ulong __unused5;
2606 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2607 abi_ulong target_addr)
2609 struct target_msqid_ds *target_md;
2611 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2612 return -TARGET_EFAULT;
2613 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2614 return -TARGET_EFAULT;
2615 host_md->msg_stime = tswapl(target_md->msg_stime);
2616 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2617 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2618 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2619 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2620 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2621 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2622 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2623 unlock_user_struct(target_md, target_addr, 0);
2624 return 0;
2627 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2628 struct msqid_ds *host_md)
2630 struct target_msqid_ds *target_md;
2632 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2633 return -TARGET_EFAULT;
2634 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2635 return -TARGET_EFAULT;
2636 target_md->msg_stime = tswapl(host_md->msg_stime);
2637 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2638 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2639 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2640 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2641 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2642 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2643 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2644 unlock_user_struct(target_md, target_addr, 1);
2645 return 0;
2648 struct target_msginfo {
2649 int msgpool;
2650 int msgmap;
2651 int msgmax;
2652 int msgmnb;
2653 int msgmni;
2654 int msgssz;
2655 int msgtql;
2656 unsigned short int msgseg;
2659 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2660 struct msginfo *host_msginfo)
2662 struct target_msginfo *target_msginfo;
2663 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2664 return -TARGET_EFAULT;
2665 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2666 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2667 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2668 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2669 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2670 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2671 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2672 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2673 unlock_user_struct(target_msginfo, target_addr, 1);
2674 return 0;
2677 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
2679 struct msqid_ds dsarg;
2680 struct msginfo msginfo;
2681 abi_long ret = -TARGET_EINVAL;
2683 cmd &= 0xff;
2685 switch (cmd) {
2686 case IPC_STAT:
2687 case IPC_SET:
2688 case MSG_STAT:
2689 if (target_to_host_msqid_ds(&dsarg,ptr))
2690 return -TARGET_EFAULT;
2691 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2692 if (host_to_target_msqid_ds(ptr,&dsarg))
2693 return -TARGET_EFAULT;
2694 break;
2695 case IPC_RMID:
2696 ret = get_errno(msgctl(msgid, cmd, NULL));
2697 break;
2698 case IPC_INFO:
2699 case MSG_INFO:
2700 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2701 if (host_to_target_msginfo(ptr, &msginfo))
2702 return -TARGET_EFAULT;
2703 break;
2706 return ret;
2709 struct target_msgbuf {
2710 abi_long mtype;
2711 char mtext[1];
2714 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2715 unsigned int msgsz, int msgflg)
2717 struct target_msgbuf *target_mb;
2718 struct msgbuf *host_mb;
2719 abi_long ret = 0;
2721 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2722 return -TARGET_EFAULT;
2723 host_mb = malloc(msgsz+sizeof(long));
2724 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2725 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
2726 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2727 free(host_mb);
2728 unlock_user_struct(target_mb, msgp, 0);
2730 return ret;
2733 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2734 unsigned int msgsz, abi_long msgtyp,
2735 int msgflg)
2737 struct target_msgbuf *target_mb;
2738 char *target_mtext;
2739 struct msgbuf *host_mb;
2740 abi_long ret = 0;
2742 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2743 return -TARGET_EFAULT;
2745 host_mb = malloc(msgsz+sizeof(long));
2746 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2748 if (ret > 0) {
2749 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2750 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2751 if (!target_mtext) {
2752 ret = -TARGET_EFAULT;
2753 goto end;
2755 memcpy(target_mb->mtext, host_mb->mtext, ret);
2756 unlock_user(target_mtext, target_mtext_addr, ret);
2759 target_mb->mtype = tswapl(host_mb->mtype);
2760 free(host_mb);
2762 end:
2763 if (target_mb)
2764 unlock_user_struct(target_mb, msgp, 1);
2765 return ret;
2768 struct target_shmid_ds
2770 struct target_ipc_perm shm_perm;
2771 abi_ulong shm_segsz;
2772 abi_ulong shm_atime;
2773 #if TARGET_ABI_BITS == 32
2774 abi_ulong __unused1;
2775 #endif
2776 abi_ulong shm_dtime;
2777 #if TARGET_ABI_BITS == 32
2778 abi_ulong __unused2;
2779 #endif
2780 abi_ulong shm_ctime;
2781 #if TARGET_ABI_BITS == 32
2782 abi_ulong __unused3;
2783 #endif
2784 int shm_cpid;
2785 int shm_lpid;
2786 abi_ulong shm_nattch;
2787 unsigned long int __unused4;
2788 unsigned long int __unused5;
2791 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2792 abi_ulong target_addr)
2794 struct target_shmid_ds *target_sd;
2796 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2797 return -TARGET_EFAULT;
2798 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2799 return -TARGET_EFAULT;
2800 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2801 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2802 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2803 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2804 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2805 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2806 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2807 unlock_user_struct(target_sd, target_addr, 0);
2808 return 0;
2811 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2812 struct shmid_ds *host_sd)
2814 struct target_shmid_ds *target_sd;
2816 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2817 return -TARGET_EFAULT;
2818 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2819 return -TARGET_EFAULT;
2820 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2821 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2822 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2823 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2824 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2825 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2826 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2827 unlock_user_struct(target_sd, target_addr, 1);
2828 return 0;
2831 struct target_shminfo {
2832 abi_ulong shmmax;
2833 abi_ulong shmmin;
2834 abi_ulong shmmni;
2835 abi_ulong shmseg;
2836 abi_ulong shmall;
2839 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2840 struct shminfo *host_shminfo)
2842 struct target_shminfo *target_shminfo;
2843 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2844 return -TARGET_EFAULT;
2845 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2846 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2847 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2848 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2849 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2850 unlock_user_struct(target_shminfo, target_addr, 1);
2851 return 0;
2854 struct target_shm_info {
2855 int used_ids;
2856 abi_ulong shm_tot;
2857 abi_ulong shm_rss;
2858 abi_ulong shm_swp;
2859 abi_ulong swap_attempts;
2860 abi_ulong swap_successes;
2863 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2864 struct shm_info *host_shm_info)
2866 struct target_shm_info *target_shm_info;
2867 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2868 return -TARGET_EFAULT;
2869 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2870 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2871 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2872 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2873 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2874 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2875 unlock_user_struct(target_shm_info, target_addr, 1);
2876 return 0;
2879 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2881 struct shmid_ds dsarg;
2882 struct shminfo shminfo;
2883 struct shm_info shm_info;
2884 abi_long ret = -TARGET_EINVAL;
2886 cmd &= 0xff;
2888 switch(cmd) {
2889 case IPC_STAT:
2890 case IPC_SET:
2891 case SHM_STAT:
2892 if (target_to_host_shmid_ds(&dsarg, buf))
2893 return -TARGET_EFAULT;
2894 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2895 if (host_to_target_shmid_ds(buf, &dsarg))
2896 return -TARGET_EFAULT;
2897 break;
2898 case IPC_INFO:
2899 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2900 if (host_to_target_shminfo(buf, &shminfo))
2901 return -TARGET_EFAULT;
2902 break;
2903 case SHM_INFO:
2904 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2905 if (host_to_target_shm_info(buf, &shm_info))
2906 return -TARGET_EFAULT;
2907 break;
2908 case IPC_RMID:
2909 case SHM_LOCK:
2910 case SHM_UNLOCK:
2911 ret = get_errno(shmctl(shmid, cmd, NULL));
2912 break;
2915 return ret;
2918 static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2920 abi_long raddr;
2921 void *host_raddr;
2922 struct shmid_ds shm_info;
2923 int i,ret;
2925 /* find out the length of the shared memory segment */
2926 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2927 if (is_error(ret)) {
2928 /* can't get length, bail out */
2929 return ret;
2932 mmap_lock();
2934 if (shmaddr)
2935 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2936 else {
2937 abi_ulong mmap_start;
2939 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2941 if (mmap_start == -1) {
2942 errno = ENOMEM;
2943 host_raddr = (void *)-1;
2944 } else
2945 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2948 if (host_raddr == (void *)-1) {
2949 mmap_unlock();
2950 return get_errno((long)host_raddr);
2952 raddr=h2g((unsigned long)host_raddr);
2954 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2955 PAGE_VALID | PAGE_READ |
2956 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2958 for (i = 0; i < N_SHM_REGIONS; i++) {
2959 if (shm_regions[i].start == 0) {
2960 shm_regions[i].start = raddr;
2961 shm_regions[i].size = shm_info.shm_segsz;
2962 break;
2966 mmap_unlock();
2967 return raddr;
2971 static inline abi_long do_shmdt(abi_ulong shmaddr)
2973 int i;
2975 for (i = 0; i < N_SHM_REGIONS; ++i) {
2976 if (shm_regions[i].start == shmaddr) {
2977 shm_regions[i].start = 0;
2978 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
2979 break;
2983 return get_errno(shmdt(g2h(shmaddr)));
2986 #ifdef TARGET_NR_ipc
2987 /* ??? This only works with linear mappings. */
2988 /* do_ipc() must return target values and target errnos. */
2989 static abi_long do_ipc(unsigned int call, int first,
2990 int second, int third,
2991 abi_long ptr, abi_long fifth)
2993 int version;
2994 abi_long ret = 0;
2996 version = call >> 16;
2997 call &= 0xffff;
2999 switch (call) {
3000 case IPCOP_semop:
3001 ret = do_semop(first, ptr, second);
3002 break;
3004 case IPCOP_semget:
3005 ret = get_errno(semget(first, second, third));
3006 break;
3008 case IPCOP_semctl:
3009 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
3010 break;
3012 case IPCOP_msgget:
3013 ret = get_errno(msgget(first, second));
3014 break;
3016 case IPCOP_msgsnd:
3017 ret = do_msgsnd(first, ptr, second, third);
3018 break;
3020 case IPCOP_msgctl:
3021 ret = do_msgctl(first, second, ptr);
3022 break;
3024 case IPCOP_msgrcv:
3025 switch (version) {
3026 case 0:
3028 struct target_ipc_kludge {
3029 abi_long msgp;
3030 abi_long msgtyp;
3031 } *tmp;
3033 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3034 ret = -TARGET_EFAULT;
3035 break;
3038 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
3040 unlock_user_struct(tmp, ptr, 0);
3041 break;
3043 default:
3044 ret = do_msgrcv(first, ptr, second, fifth, third);
3046 break;
3048 case IPCOP_shmat:
3049 switch (version) {
3050 default:
3052 abi_ulong raddr;
3053 raddr = do_shmat(first, ptr, second);
3054 if (is_error(raddr))
3055 return get_errno(raddr);
3056 if (put_user_ual(raddr, third))
3057 return -TARGET_EFAULT;
3058 break;
3060 case 1:
3061 ret = -TARGET_EINVAL;
3062 break;
3064 break;
3065 case IPCOP_shmdt:
3066 ret = do_shmdt(ptr);
3067 break;
3069 case IPCOP_shmget:
3070 /* IPC_* flag values are the same on all linux platforms */
3071 ret = get_errno(shmget(first, second, third));
3072 break;
3074 /* IPC_* and SHM_* command values are the same on all linux platforms */
3075 case IPCOP_shmctl:
3076 ret = do_shmctl(first, second, third);
3077 break;
3078 default:
3079 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
3080 ret = -TARGET_ENOSYS;
3081 break;
3083 return ret;
3085 #endif
3087 /* kernel structure types definitions */
3089 #define STRUCT(name, ...) STRUCT_ ## name,
3090 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
3091 enum {
3092 #include "syscall_types.h"
3094 #undef STRUCT
3095 #undef STRUCT_SPECIAL
3097 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
3098 #define STRUCT_SPECIAL(name)
3099 #include "syscall_types.h"
3100 #undef STRUCT
3101 #undef STRUCT_SPECIAL
3103 typedef struct IOCTLEntry IOCTLEntry;
3105 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3106 int fd, abi_long cmd, abi_long arg);
3108 struct IOCTLEntry {
3109 unsigned int target_cmd;
3110 unsigned int host_cmd;
3111 const char *name;
3112 int access;
3113 do_ioctl_fn *do_ioctl;
3114 const argtype arg_type[5];
3117 #define IOC_R 0x0001
3118 #define IOC_W 0x0002
3119 #define IOC_RW (IOC_R | IOC_W)
3121 #define MAX_STRUCT_SIZE 4096
3123 #ifdef CONFIG_FIEMAP
3124 /* So fiemap access checks don't overflow on 32 bit systems.
3125 * This is very slightly smaller than the limit imposed by
3126 * the underlying kernel.
3128 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3129 / sizeof(struct fiemap_extent))
3131 static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3132 int fd, abi_long cmd, abi_long arg)
3134 /* The parameter for this ioctl is a struct fiemap followed
3135 * by an array of struct fiemap_extent whose size is set
3136 * in fiemap->fm_extent_count. The array is filled in by the
3137 * ioctl.
3139 int target_size_in, target_size_out;
3140 struct fiemap *fm;
3141 const argtype *arg_type = ie->arg_type;
3142 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3143 void *argptr, *p;
3144 abi_long ret;
3145 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3146 uint32_t outbufsz;
3147 int free_fm = 0;
3149 assert(arg_type[0] == TYPE_PTR);
3150 assert(ie->access == IOC_RW);
3151 arg_type++;
3152 target_size_in = thunk_type_size(arg_type, 0);
3153 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3154 if (!argptr) {
3155 return -TARGET_EFAULT;
3157 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3158 unlock_user(argptr, arg, 0);
3159 fm = (struct fiemap *)buf_temp;
3160 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3161 return -TARGET_EINVAL;
3164 outbufsz = sizeof (*fm) +
3165 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3167 if (outbufsz > MAX_STRUCT_SIZE) {
3168 /* We can't fit all the extents into the fixed size buffer.
3169 * Allocate one that is large enough and use it instead.
3171 fm = malloc(outbufsz);
3172 if (!fm) {
3173 return -TARGET_ENOMEM;
3175 memcpy(fm, buf_temp, sizeof(struct fiemap));
3176 free_fm = 1;
3178 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3179 if (!is_error(ret)) {
3180 target_size_out = target_size_in;
3181 /* An extent_count of 0 means we were only counting the extents
3182 * so there are no structs to copy
3184 if (fm->fm_extent_count != 0) {
3185 target_size_out += fm->fm_mapped_extents * extent_size;
3187 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3188 if (!argptr) {
3189 ret = -TARGET_EFAULT;
3190 } else {
3191 /* Convert the struct fiemap */
3192 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3193 if (fm->fm_extent_count != 0) {
3194 p = argptr + target_size_in;
3195 /* ...and then all the struct fiemap_extents */
3196 for (i = 0; i < fm->fm_mapped_extents; i++) {
3197 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3198 THUNK_TARGET);
3199 p += extent_size;
3202 unlock_user(argptr, arg, target_size_out);
3205 if (free_fm) {
3206 free(fm);
3208 return ret;
3210 #endif
3212 static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3213 int fd, abi_long cmd, abi_long arg)
3215 const argtype *arg_type = ie->arg_type;
3216 int target_size;
3217 void *argptr;
3218 int ret;
3219 struct ifconf *host_ifconf;
3220 uint32_t outbufsz;
3221 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3222 int target_ifreq_size;
3223 int nb_ifreq;
3224 int free_buf = 0;
3225 int i;
3226 int target_ifc_len;
3227 abi_long target_ifc_buf;
3228 int host_ifc_len;
3229 char *host_ifc_buf;
3231 assert(arg_type[0] == TYPE_PTR);
3232 assert(ie->access == IOC_RW);
3234 arg_type++;
3235 target_size = thunk_type_size(arg_type, 0);
3237 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3238 if (!argptr)
3239 return -TARGET_EFAULT;
3240 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3241 unlock_user(argptr, arg, 0);
3243 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3244 target_ifc_len = host_ifconf->ifc_len;
3245 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3247 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3248 nb_ifreq = target_ifc_len / target_ifreq_size;
3249 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3251 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3252 if (outbufsz > MAX_STRUCT_SIZE) {
3253 /* We can't fit all the extents into the fixed size buffer.
3254 * Allocate one that is large enough and use it instead.
3256 host_ifconf = malloc(outbufsz);
3257 if (!host_ifconf) {
3258 return -TARGET_ENOMEM;
3260 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3261 free_buf = 1;
3263 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3265 host_ifconf->ifc_len = host_ifc_len;
3266 host_ifconf->ifc_buf = host_ifc_buf;
3268 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3269 if (!is_error(ret)) {
3270 /* convert host ifc_len to target ifc_len */
3272 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3273 target_ifc_len = nb_ifreq * target_ifreq_size;
3274 host_ifconf->ifc_len = target_ifc_len;
3276 /* restore target ifc_buf */
3278 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3280 /* copy struct ifconf to target user */
3282 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3283 if (!argptr)
3284 return -TARGET_EFAULT;
3285 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3286 unlock_user(argptr, arg, target_size);
3288 /* copy ifreq[] to target user */
3290 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3291 for (i = 0; i < nb_ifreq ; i++) {
3292 thunk_convert(argptr + i * target_ifreq_size,
3293 host_ifc_buf + i * sizeof(struct ifreq),
3294 ifreq_arg_type, THUNK_TARGET);
3296 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3299 if (free_buf) {
3300 free(host_ifconf);
3303 return ret;
3306 static IOCTLEntry ioctl_entries[] = {
3307 #define IOCTL(cmd, access, ...) \
3308 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3309 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3310 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
3311 #include "ioctls.h"
3312 { 0, 0, },
3315 /* ??? Implement proper locking for ioctls. */
3316 /* do_ioctl() Must return target values and target errnos. */
3317 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
3319 const IOCTLEntry *ie;
3320 const argtype *arg_type;
3321 abi_long ret;
3322 uint8_t buf_temp[MAX_STRUCT_SIZE];
3323 int target_size;
3324 void *argptr;
3326 ie = ioctl_entries;
3327 for(;;) {
3328 if (ie->target_cmd == 0) {
3329 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
3330 return -TARGET_ENOSYS;
3332 if (ie->target_cmd == cmd)
3333 break;
3334 ie++;
3336 arg_type = ie->arg_type;
3337 #if defined(DEBUG)
3338 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
3339 #endif
3340 if (ie->do_ioctl) {
3341 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3344 switch(arg_type[0]) {
3345 case TYPE_NULL:
3346 /* no argument */
3347 ret = get_errno(ioctl(fd, ie->host_cmd));
3348 break;
3349 case TYPE_PTRVOID:
3350 case TYPE_INT:
3351 /* int argment */
3352 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3353 break;
3354 case TYPE_PTR:
3355 arg_type++;
3356 target_size = thunk_type_size(arg_type, 0);
3357 switch(ie->access) {
3358 case IOC_R:
3359 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3360 if (!is_error(ret)) {
3361 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3362 if (!argptr)
3363 return -TARGET_EFAULT;
3364 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3365 unlock_user(argptr, arg, target_size);
3367 break;
3368 case IOC_W:
3369 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3370 if (!argptr)
3371 return -TARGET_EFAULT;
3372 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3373 unlock_user(argptr, arg, 0);
3374 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3375 break;
3376 default:
3377 case IOC_RW:
3378 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3379 if (!argptr)
3380 return -TARGET_EFAULT;
3381 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3382 unlock_user(argptr, arg, 0);
3383 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3384 if (!is_error(ret)) {
3385 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3386 if (!argptr)
3387 return -TARGET_EFAULT;
3388 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3389 unlock_user(argptr, arg, target_size);
3391 break;
3393 break;
3394 default:
3395 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3396 (long)cmd, arg_type[0]);
3397 ret = -TARGET_ENOSYS;
3398 break;
3400 return ret;
3403 static const bitmask_transtbl iflag_tbl[] = {
3404 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3405 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3406 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3407 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3408 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3409 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3410 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3411 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3412 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3413 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3414 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3415 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3416 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3417 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3418 { 0, 0, 0, 0 }
3421 static const bitmask_transtbl oflag_tbl[] = {
3422 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3423 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3424 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3425 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3426 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3427 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3428 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3429 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3430 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3431 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3432 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3433 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3434 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3435 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3436 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3437 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3438 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3439 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3440 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3441 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3442 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3443 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3444 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3445 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3446 { 0, 0, 0, 0 }
3449 static const bitmask_transtbl cflag_tbl[] = {
3450 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3451 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3452 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3453 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3454 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3455 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3456 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3457 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3458 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3459 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3460 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3461 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3462 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3463 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3464 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3465 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3466 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3467 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3468 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3469 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3470 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3471 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3472 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3473 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3474 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3475 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3476 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3477 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3478 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3479 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3480 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3481 { 0, 0, 0, 0 }
3484 static const bitmask_transtbl lflag_tbl[] = {
3485 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3486 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3487 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3488 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3489 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3490 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3491 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3492 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3493 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3494 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3495 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3496 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3497 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3498 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3499 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3500 { 0, 0, 0, 0 }
3503 static void target_to_host_termios (void *dst, const void *src)
3505 struct host_termios *host = dst;
3506 const struct target_termios *target = src;
3508 host->c_iflag =
3509 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
3510 host->c_oflag =
3511 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
3512 host->c_cflag =
3513 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
3514 host->c_lflag =
3515 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3516 host->c_line = target->c_line;
3518 memset(host->c_cc, 0, sizeof(host->c_cc));
3519 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3520 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3521 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
3522 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3523 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
3524 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3525 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
3526 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3527 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
3528 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3529 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3530 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3531 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3532 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3533 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3534 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
3535 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
3538 static void host_to_target_termios (void *dst, const void *src)
3540 struct target_termios *target = dst;
3541 const struct host_termios *host = src;
3543 target->c_iflag =
3544 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
3545 target->c_oflag =
3546 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
3547 target->c_cflag =
3548 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
3549 target->c_lflag =
3550 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3551 target->c_line = host->c_line;
3553 memset(target->c_cc, 0, sizeof(target->c_cc));
3554 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3555 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3556 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3557 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3558 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3559 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3560 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3561 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3562 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3563 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3564 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3565 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3566 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3567 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3568 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3569 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3570 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3573 static const StructEntry struct_termios_def = {
3574 .convert = { host_to_target_termios, target_to_host_termios },
3575 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3576 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3579 static bitmask_transtbl mmap_flags_tbl[] = {
3580 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3581 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3582 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3583 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3584 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3585 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3586 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3587 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3588 { 0, 0, 0, 0 }
3591 #if defined(TARGET_I386)
3593 /* NOTE: there is really one LDT for all the threads */
3594 static uint8_t *ldt_table;
3596 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
3598 int size;
3599 void *p;
3601 if (!ldt_table)
3602 return 0;
3603 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3604 if (size > bytecount)
3605 size = bytecount;
3606 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3607 if (!p)
3608 return -TARGET_EFAULT;
3609 /* ??? Should this by byteswapped? */
3610 memcpy(p, ldt_table, size);
3611 unlock_user(p, ptr, size);
3612 return size;
3615 /* XXX: add locking support */
3616 static abi_long write_ldt(CPUX86State *env,
3617 abi_ulong ptr, unsigned long bytecount, int oldmode)
3619 struct target_modify_ldt_ldt_s ldt_info;
3620 struct target_modify_ldt_ldt_s *target_ldt_info;
3621 int seg_32bit, contents, read_exec_only, limit_in_pages;
3622 int seg_not_present, useable, lm;
3623 uint32_t *lp, entry_1, entry_2;
3625 if (bytecount != sizeof(ldt_info))
3626 return -TARGET_EINVAL;
3627 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
3628 return -TARGET_EFAULT;
3629 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3630 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3631 ldt_info.limit = tswap32(target_ldt_info->limit);
3632 ldt_info.flags = tswap32(target_ldt_info->flags);
3633 unlock_user_struct(target_ldt_info, ptr, 0);
3635 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
3636 return -TARGET_EINVAL;
3637 seg_32bit = ldt_info.flags & 1;
3638 contents = (ldt_info.flags >> 1) & 3;
3639 read_exec_only = (ldt_info.flags >> 3) & 1;
3640 limit_in_pages = (ldt_info.flags >> 4) & 1;
3641 seg_not_present = (ldt_info.flags >> 5) & 1;
3642 useable = (ldt_info.flags >> 6) & 1;
3643 #ifdef TARGET_ABI32
3644 lm = 0;
3645 #else
3646 lm = (ldt_info.flags >> 7) & 1;
3647 #endif
3648 if (contents == 3) {
3649 if (oldmode)
3650 return -TARGET_EINVAL;
3651 if (seg_not_present == 0)
3652 return -TARGET_EINVAL;
3654 /* allocate the LDT */
3655 if (!ldt_table) {
3656 env->ldt.base = target_mmap(0,
3657 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3658 PROT_READ|PROT_WRITE,
3659 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3660 if (env->ldt.base == -1)
3661 return -TARGET_ENOMEM;
3662 memset(g2h(env->ldt.base), 0,
3663 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
3664 env->ldt.limit = 0xffff;
3665 ldt_table = g2h(env->ldt.base);
3668 /* NOTE: same code as Linux kernel */
3669 /* Allow LDTs to be cleared by the user. */
3670 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3671 if (oldmode ||
3672 (contents == 0 &&
3673 read_exec_only == 1 &&
3674 seg_32bit == 0 &&
3675 limit_in_pages == 0 &&
3676 seg_not_present == 1 &&
3677 useable == 0 )) {
3678 entry_1 = 0;
3679 entry_2 = 0;
3680 goto install;
3684 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3685 (ldt_info.limit & 0x0ffff);
3686 entry_2 = (ldt_info.base_addr & 0xff000000) |
3687 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3688 (ldt_info.limit & 0xf0000) |
3689 ((read_exec_only ^ 1) << 9) |
3690 (contents << 10) |
3691 ((seg_not_present ^ 1) << 15) |
3692 (seg_32bit << 22) |
3693 (limit_in_pages << 23) |
3694 (lm << 21) |
3695 0x7000;
3696 if (!oldmode)
3697 entry_2 |= (useable << 20);
3699 /* Install the new entry ... */
3700 install:
3701 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3702 lp[0] = tswap32(entry_1);
3703 lp[1] = tswap32(entry_2);
3704 return 0;
3707 /* specific and weird i386 syscalls */
3708 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3709 unsigned long bytecount)
3711 abi_long ret;
3713 switch (func) {
3714 case 0:
3715 ret = read_ldt(ptr, bytecount);
3716 break;
3717 case 1:
3718 ret = write_ldt(env, ptr, bytecount, 1);
3719 break;
3720 case 0x11:
3721 ret = write_ldt(env, ptr, bytecount, 0);
3722 break;
3723 default:
3724 ret = -TARGET_ENOSYS;
3725 break;
3727 return ret;
3730 #if defined(TARGET_I386) && defined(TARGET_ABI32)
3731 static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
3733 uint64_t *gdt_table = g2h(env->gdt.base);
3734 struct target_modify_ldt_ldt_s ldt_info;
3735 struct target_modify_ldt_ldt_s *target_ldt_info;
3736 int seg_32bit, contents, read_exec_only, limit_in_pages;
3737 int seg_not_present, useable, lm;
3738 uint32_t *lp, entry_1, entry_2;
3739 int i;
3741 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3742 if (!target_ldt_info)
3743 return -TARGET_EFAULT;
3744 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3745 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3746 ldt_info.limit = tswap32(target_ldt_info->limit);
3747 ldt_info.flags = tswap32(target_ldt_info->flags);
3748 if (ldt_info.entry_number == -1) {
3749 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3750 if (gdt_table[i] == 0) {
3751 ldt_info.entry_number = i;
3752 target_ldt_info->entry_number = tswap32(i);
3753 break;
3757 unlock_user_struct(target_ldt_info, ptr, 1);
3759 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3760 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3761 return -TARGET_EINVAL;
3762 seg_32bit = ldt_info.flags & 1;
3763 contents = (ldt_info.flags >> 1) & 3;
3764 read_exec_only = (ldt_info.flags >> 3) & 1;
3765 limit_in_pages = (ldt_info.flags >> 4) & 1;
3766 seg_not_present = (ldt_info.flags >> 5) & 1;
3767 useable = (ldt_info.flags >> 6) & 1;
3768 #ifdef TARGET_ABI32
3769 lm = 0;
3770 #else
3771 lm = (ldt_info.flags >> 7) & 1;
3772 #endif
3774 if (contents == 3) {
3775 if (seg_not_present == 0)
3776 return -TARGET_EINVAL;
3779 /* NOTE: same code as Linux kernel */
3780 /* Allow LDTs to be cleared by the user. */
3781 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3782 if ((contents == 0 &&
3783 read_exec_only == 1 &&
3784 seg_32bit == 0 &&
3785 limit_in_pages == 0 &&
3786 seg_not_present == 1 &&
3787 useable == 0 )) {
3788 entry_1 = 0;
3789 entry_2 = 0;
3790 goto install;
3794 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3795 (ldt_info.limit & 0x0ffff);
3796 entry_2 = (ldt_info.base_addr & 0xff000000) |
3797 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3798 (ldt_info.limit & 0xf0000) |
3799 ((read_exec_only ^ 1) << 9) |
3800 (contents << 10) |
3801 ((seg_not_present ^ 1) << 15) |
3802 (seg_32bit << 22) |
3803 (limit_in_pages << 23) |
3804 (useable << 20) |
3805 (lm << 21) |
3806 0x7000;
3808 /* Install the new entry ... */
3809 install:
3810 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3811 lp[0] = tswap32(entry_1);
3812 lp[1] = tswap32(entry_2);
3813 return 0;
3816 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
3818 struct target_modify_ldt_ldt_s *target_ldt_info;
3819 uint64_t *gdt_table = g2h(env->gdt.base);
3820 uint32_t base_addr, limit, flags;
3821 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3822 int seg_not_present, useable, lm;
3823 uint32_t *lp, entry_1, entry_2;
3825 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3826 if (!target_ldt_info)
3827 return -TARGET_EFAULT;
3828 idx = tswap32(target_ldt_info->entry_number);
3829 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3830 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3831 unlock_user_struct(target_ldt_info, ptr, 1);
3832 return -TARGET_EINVAL;
3834 lp = (uint32_t *)(gdt_table + idx);
3835 entry_1 = tswap32(lp[0]);
3836 entry_2 = tswap32(lp[1]);
3838 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3839 contents = (entry_2 >> 10) & 3;
3840 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3841 seg_32bit = (entry_2 >> 22) & 1;
3842 limit_in_pages = (entry_2 >> 23) & 1;
3843 useable = (entry_2 >> 20) & 1;
3844 #ifdef TARGET_ABI32
3845 lm = 0;
3846 #else
3847 lm = (entry_2 >> 21) & 1;
3848 #endif
3849 flags = (seg_32bit << 0) | (contents << 1) |
3850 (read_exec_only << 3) | (limit_in_pages << 4) |
3851 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3852 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3853 base_addr = (entry_1 >> 16) |
3854 (entry_2 & 0xff000000) |
3855 ((entry_2 & 0xff) << 16);
3856 target_ldt_info->base_addr = tswapl(base_addr);
3857 target_ldt_info->limit = tswap32(limit);
3858 target_ldt_info->flags = tswap32(flags);
3859 unlock_user_struct(target_ldt_info, ptr, 1);
3860 return 0;
3862 #endif /* TARGET_I386 && TARGET_ABI32 */
3864 #ifndef TARGET_ABI32
3865 static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
3867 abi_long ret = 0;
3868 abi_ulong val;
3869 int idx;
3871 switch(code) {
3872 case TARGET_ARCH_SET_GS:
3873 case TARGET_ARCH_SET_FS:
3874 if (code == TARGET_ARCH_SET_GS)
3875 idx = R_GS;
3876 else
3877 idx = R_FS;
3878 cpu_x86_load_seg(env, idx, 0);
3879 env->segs[idx].base = addr;
3880 break;
3881 case TARGET_ARCH_GET_GS:
3882 case TARGET_ARCH_GET_FS:
3883 if (code == TARGET_ARCH_GET_GS)
3884 idx = R_GS;
3885 else
3886 idx = R_FS;
3887 val = env->segs[idx].base;
3888 if (put_user(val, addr, abi_ulong))
3889 ret = -TARGET_EFAULT;
3890 break;
3891 default:
3892 ret = -TARGET_EINVAL;
3893 break;
3895 return ret;
3897 #endif
3899 #endif /* defined(TARGET_I386) */
3901 #define NEW_STACK_SIZE 0x40000
3903 #if defined(CONFIG_USE_NPTL)
3905 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3906 typedef struct {
3907 CPUState *env;
3908 pthread_mutex_t mutex;
3909 pthread_cond_t cond;
3910 pthread_t thread;
3911 uint32_t tid;
3912 abi_ulong child_tidptr;
3913 abi_ulong parent_tidptr;
3914 sigset_t sigmask;
3915 } new_thread_info;
3917 static void *clone_func(void *arg)
3919 new_thread_info *info = arg;
3920 CPUState *env;
3921 TaskState *ts;
3923 env = info->env;
3924 thread_env = env;
3925 ts = (TaskState *)thread_env->opaque;
3926 info->tid = gettid();
3927 env->host_tid = info->tid;
3928 task_settid(ts);
3929 if (info->child_tidptr)
3930 put_user_u32(info->tid, info->child_tidptr);
3931 if (info->parent_tidptr)
3932 put_user_u32(info->tid, info->parent_tidptr);
3933 /* Enable signals. */
3934 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3935 /* Signal to the parent that we're ready. */
3936 pthread_mutex_lock(&info->mutex);
3937 pthread_cond_broadcast(&info->cond);
3938 pthread_mutex_unlock(&info->mutex);
3939 /* Wait until the parent has finshed initializing the tls state. */
3940 pthread_mutex_lock(&clone_lock);
3941 pthread_mutex_unlock(&clone_lock);
3942 cpu_loop(env);
3943 /* never exits */
3944 return NULL;
3946 #else
3948 static int clone_func(void *arg)
3950 CPUState *env = arg;
3951 cpu_loop(env);
3952 /* never exits */
3953 return 0;
3955 #endif
3957 /* do_fork() Must return host values and target errnos (unlike most
3958 do_*() functions). */
3959 static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3960 abi_ulong parent_tidptr, target_ulong newtls,
3961 abi_ulong child_tidptr)
3963 int ret;
3964 TaskState *ts;
3965 CPUState *new_env;
3966 #if defined(CONFIG_USE_NPTL)
3967 unsigned int nptl_flags;
3968 sigset_t sigmask;
3969 #else
3970 uint8_t *new_stack;
3971 #endif
3973 /* Emulate vfork() with fork() */
3974 if (flags & CLONE_VFORK)
3975 flags &= ~(CLONE_VFORK | CLONE_VM);
3977 if (flags & CLONE_VM) {
3978 TaskState *parent_ts = (TaskState *)env->opaque;
3979 #if defined(CONFIG_USE_NPTL)
3980 new_thread_info info;
3981 pthread_attr_t attr;
3982 #endif
3983 ts = qemu_mallocz(sizeof(TaskState));
3984 init_task_state(ts);
3985 /* we create a new CPU instance. */
3986 new_env = cpu_copy(env);
3987 #if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3988 cpu_reset(new_env);
3989 #endif
3990 /* Init regs that differ from the parent. */
3991 cpu_clone_regs(new_env, newsp);
3992 new_env->opaque = ts;
3993 ts->bprm = parent_ts->bprm;
3994 ts->info = parent_ts->info;
3995 #if defined(CONFIG_USE_NPTL)
3996 nptl_flags = flags;
3997 flags &= ~CLONE_NPTL_FLAGS2;
3999 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4000 ts->child_tidptr = child_tidptr;
4003 if (nptl_flags & CLONE_SETTLS)
4004 cpu_set_tls (new_env, newtls);
4006 /* Grab a mutex so that thread setup appears atomic. */
4007 pthread_mutex_lock(&clone_lock);
4009 memset(&info, 0, sizeof(info));
4010 pthread_mutex_init(&info.mutex, NULL);
4011 pthread_mutex_lock(&info.mutex);
4012 pthread_cond_init(&info.cond, NULL);
4013 info.env = new_env;
4014 if (nptl_flags & CLONE_CHILD_SETTID)
4015 info.child_tidptr = child_tidptr;
4016 if (nptl_flags & CLONE_PARENT_SETTID)
4017 info.parent_tidptr = parent_tidptr;
4019 ret = pthread_attr_init(&attr);
4020 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4021 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
4022 /* It is not safe to deliver signals until the child has finished
4023 initializing, so temporarily block all signals. */
4024 sigfillset(&sigmask);
4025 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4027 ret = pthread_create(&info.thread, &attr, clone_func, &info);
4028 /* TODO: Free new CPU state if thread creation failed. */
4030 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4031 pthread_attr_destroy(&attr);
4032 if (ret == 0) {
4033 /* Wait for the child to initialize. */
4034 pthread_cond_wait(&info.cond, &info.mutex);
4035 ret = info.tid;
4036 if (flags & CLONE_PARENT_SETTID)
4037 put_user_u32(ret, parent_tidptr);
4038 } else {
4039 ret = -1;
4041 pthread_mutex_unlock(&info.mutex);
4042 pthread_cond_destroy(&info.cond);
4043 pthread_mutex_destroy(&info.mutex);
4044 pthread_mutex_unlock(&clone_lock);
4045 #else
4046 if (flags & CLONE_NPTL_FLAGS2)
4047 return -EINVAL;
4048 /* This is probably going to die very quickly, but do it anyway. */
4049 new_stack = qemu_mallocz (NEW_STACK_SIZE);
4050 #ifdef __ia64__
4051 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
4052 #else
4053 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
4054 #endif
4055 #endif
4056 } else {
4057 /* if no CLONE_VM, we consider it is a fork */
4058 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
4059 return -EINVAL;
4060 fork_start();
4061 ret = fork();
4062 if (ret == 0) {
4063 /* Child Process. */
4064 cpu_clone_regs(env, newsp);
4065 fork_end(1);
4066 #if defined(CONFIG_USE_NPTL)
4067 /* There is a race condition here. The parent process could
4068 theoretically read the TID in the child process before the child
4069 tid is set. This would require using either ptrace
4070 (not implemented) or having *_tidptr to point at a shared memory
4071 mapping. We can't repeat the spinlock hack used above because
4072 the child process gets its own copy of the lock. */
4073 if (flags & CLONE_CHILD_SETTID)
4074 put_user_u32(gettid(), child_tidptr);
4075 if (flags & CLONE_PARENT_SETTID)
4076 put_user_u32(gettid(), parent_tidptr);
4077 ts = (TaskState *)env->opaque;
4078 if (flags & CLONE_SETTLS)
4079 cpu_set_tls (env, newtls);
4080 if (flags & CLONE_CHILD_CLEARTID)
4081 ts->child_tidptr = child_tidptr;
4082 #endif
4083 } else {
4084 fork_end(0);
4087 return ret;
4090 /* warning : doesn't handle linux specific flags... */
4091 static int target_to_host_fcntl_cmd(int cmd)
4093 switch(cmd) {
4094 case TARGET_F_DUPFD:
4095 case TARGET_F_GETFD:
4096 case TARGET_F_SETFD:
4097 case TARGET_F_GETFL:
4098 case TARGET_F_SETFL:
4099 return cmd;
4100 case TARGET_F_GETLK:
4101 return F_GETLK;
4102 case TARGET_F_SETLK:
4103 return F_SETLK;
4104 case TARGET_F_SETLKW:
4105 return F_SETLKW;
4106 case TARGET_F_GETOWN:
4107 return F_GETOWN;
4108 case TARGET_F_SETOWN:
4109 return F_SETOWN;
4110 case TARGET_F_GETSIG:
4111 return F_GETSIG;
4112 case TARGET_F_SETSIG:
4113 return F_SETSIG;
4114 #if TARGET_ABI_BITS == 32
4115 case TARGET_F_GETLK64:
4116 return F_GETLK64;
4117 case TARGET_F_SETLK64:
4118 return F_SETLK64;
4119 case TARGET_F_SETLKW64:
4120 return F_SETLKW64;
4121 #endif
4122 case TARGET_F_SETLEASE:
4123 return F_SETLEASE;
4124 case TARGET_F_GETLEASE:
4125 return F_GETLEASE;
4126 #ifdef F_DUPFD_CLOEXEC
4127 case TARGET_F_DUPFD_CLOEXEC:
4128 return F_DUPFD_CLOEXEC;
4129 #endif
4130 case TARGET_F_NOTIFY:
4131 return F_NOTIFY;
4132 default:
4133 return -TARGET_EINVAL;
4135 return -TARGET_EINVAL;
4138 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
4140 struct flock fl;
4141 struct target_flock *target_fl;
4142 struct flock64 fl64;
4143 struct target_flock64 *target_fl64;
4144 abi_long ret;
4145 int host_cmd = target_to_host_fcntl_cmd(cmd);
4147 if (host_cmd == -TARGET_EINVAL)
4148 return host_cmd;
4150 switch(cmd) {
4151 case TARGET_F_GETLK:
4152 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4153 return -TARGET_EFAULT;
4154 fl.l_type = tswap16(target_fl->l_type);
4155 fl.l_whence = tswap16(target_fl->l_whence);
4156 fl.l_start = tswapl(target_fl->l_start);
4157 fl.l_len = tswapl(target_fl->l_len);
4158 fl.l_pid = tswap32(target_fl->l_pid);
4159 unlock_user_struct(target_fl, arg, 0);
4160 ret = get_errno(fcntl(fd, host_cmd, &fl));
4161 if (ret == 0) {
4162 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4163 return -TARGET_EFAULT;
4164 target_fl->l_type = tswap16(fl.l_type);
4165 target_fl->l_whence = tswap16(fl.l_whence);
4166 target_fl->l_start = tswapl(fl.l_start);
4167 target_fl->l_len = tswapl(fl.l_len);
4168 target_fl->l_pid = tswap32(fl.l_pid);
4169 unlock_user_struct(target_fl, arg, 1);
4171 break;
4173 case TARGET_F_SETLK:
4174 case TARGET_F_SETLKW:
4175 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4176 return -TARGET_EFAULT;
4177 fl.l_type = tswap16(target_fl->l_type);
4178 fl.l_whence = tswap16(target_fl->l_whence);
4179 fl.l_start = tswapl(target_fl->l_start);
4180 fl.l_len = tswapl(target_fl->l_len);
4181 fl.l_pid = tswap32(target_fl->l_pid);
4182 unlock_user_struct(target_fl, arg, 0);
4183 ret = get_errno(fcntl(fd, host_cmd, &fl));
4184 break;
4186 case TARGET_F_GETLK64:
4187 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4188 return -TARGET_EFAULT;
4189 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4190 fl64.l_whence = tswap16(target_fl64->l_whence);
4191 fl64.l_start = tswapl(target_fl64->l_start);
4192 fl64.l_len = tswapl(target_fl64->l_len);
4193 fl64.l_pid = tswap32(target_fl64->l_pid);
4194 unlock_user_struct(target_fl64, arg, 0);
4195 ret = get_errno(fcntl(fd, host_cmd, &fl64));
4196 if (ret == 0) {
4197 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4198 return -TARGET_EFAULT;
4199 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
4200 target_fl64->l_whence = tswap16(fl64.l_whence);
4201 target_fl64->l_start = tswapl(fl64.l_start);
4202 target_fl64->l_len = tswapl(fl64.l_len);
4203 target_fl64->l_pid = tswap32(fl64.l_pid);
4204 unlock_user_struct(target_fl64, arg, 1);
4206 break;
4207 case TARGET_F_SETLK64:
4208 case TARGET_F_SETLKW64:
4209 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4210 return -TARGET_EFAULT;
4211 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4212 fl64.l_whence = tswap16(target_fl64->l_whence);
4213 fl64.l_start = tswapl(target_fl64->l_start);
4214 fl64.l_len = tswapl(target_fl64->l_len);
4215 fl64.l_pid = tswap32(target_fl64->l_pid);
4216 unlock_user_struct(target_fl64, arg, 0);
4217 ret = get_errno(fcntl(fd, host_cmd, &fl64));
4218 break;
4220 case TARGET_F_GETFL:
4221 ret = get_errno(fcntl(fd, host_cmd, arg));
4222 if (ret >= 0) {
4223 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4225 break;
4227 case TARGET_F_SETFL:
4228 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4229 break;
4231 case TARGET_F_SETOWN:
4232 case TARGET_F_GETOWN:
4233 case TARGET_F_SETSIG:
4234 case TARGET_F_GETSIG:
4235 case TARGET_F_SETLEASE:
4236 case TARGET_F_GETLEASE:
4237 ret = get_errno(fcntl(fd, host_cmd, arg));
4238 break;
4240 default:
4241 ret = get_errno(fcntl(fd, cmd, arg));
4242 break;
4244 return ret;
4247 #ifdef USE_UID16
4249 static inline int high2lowuid(int uid)
4251 if (uid > 65535)
4252 return 65534;
4253 else
4254 return uid;
4257 static inline int high2lowgid(int gid)
4259 if (gid > 65535)
4260 return 65534;
4261 else
4262 return gid;
4265 static inline int low2highuid(int uid)
4267 if ((int16_t)uid == -1)
4268 return -1;
4269 else
4270 return uid;
4273 static inline int low2highgid(int gid)
4275 if ((int16_t)gid == -1)
4276 return -1;
4277 else
4278 return gid;
4280 static inline int tswapid(int id)
4282 return tswap16(id);
4284 #else /* !USE_UID16 */
4285 static inline int high2lowuid(int uid)
4287 return uid;
4289 static inline int high2lowgid(int gid)
4291 return gid;
4293 static inline int low2highuid(int uid)
4295 return uid;
4297 static inline int low2highgid(int gid)
4299 return gid;
4301 static inline int tswapid(int id)
4303 return tswap32(id);
4305 #endif /* USE_UID16 */
4307 void syscall_init(void)
4309 IOCTLEntry *ie;
4310 const argtype *arg_type;
4311 int size;
4312 int i;
4314 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
4315 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
4316 #include "syscall_types.h"
4317 #undef STRUCT
4318 #undef STRUCT_SPECIAL
4320 /* we patch the ioctl size if necessary. We rely on the fact that
4321 no ioctl has all the bits at '1' in the size field */
4322 ie = ioctl_entries;
4323 while (ie->target_cmd != 0) {
4324 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4325 TARGET_IOC_SIZEMASK) {
4326 arg_type = ie->arg_type;
4327 if (arg_type[0] != TYPE_PTR) {
4328 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
4329 ie->target_cmd);
4330 exit(1);
4332 arg_type++;
4333 size = thunk_type_size(arg_type, 0);
4334 ie->target_cmd = (ie->target_cmd &
4335 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4336 (size << TARGET_IOC_SIZESHIFT);
4339 /* Build target_to_host_errno_table[] table from
4340 * host_to_target_errno_table[]. */
4341 for (i=0; i < ERRNO_TABLE_SIZE; i++)
4342 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4344 /* automatic consistency check if same arch */
4345 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4346 (defined(__x86_64__) && defined(TARGET_X86_64))
4347 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4348 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4349 ie->name, ie->target_cmd, ie->host_cmd);
4351 #endif
4352 ie++;
4356 #if TARGET_ABI_BITS == 32
4357 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4359 #ifdef TARGET_WORDS_BIGENDIAN
4360 return ((uint64_t)word0 << 32) | word1;
4361 #else
4362 return ((uint64_t)word1 << 32) | word0;
4363 #endif
4365 #else /* TARGET_ABI_BITS == 32 */
4366 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4368 return word0;
4370 #endif /* TARGET_ABI_BITS != 32 */
4372 #ifdef TARGET_NR_truncate64
4373 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4374 abi_long arg2,
4375 abi_long arg3,
4376 abi_long arg4)
4378 #ifdef TARGET_ARM
4379 if (((CPUARMState *)cpu_env)->eabi)
4381 arg2 = arg3;
4382 arg3 = arg4;
4384 #endif
4385 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4387 #endif
4389 #ifdef TARGET_NR_ftruncate64
4390 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4391 abi_long arg2,
4392 abi_long arg3,
4393 abi_long arg4)
4395 #ifdef TARGET_ARM
4396 if (((CPUARMState *)cpu_env)->eabi)
4398 arg2 = arg3;
4399 arg3 = arg4;
4401 #endif
4402 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4404 #endif
4406 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4407 abi_ulong target_addr)
4409 struct target_timespec *target_ts;
4411 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4412 return -TARGET_EFAULT;
4413 host_ts->tv_sec = tswapl(target_ts->tv_sec);
4414 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
4415 unlock_user_struct(target_ts, target_addr, 0);
4416 return 0;
4419 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4420 struct timespec *host_ts)
4422 struct target_timespec *target_ts;
4424 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4425 return -TARGET_EFAULT;
4426 target_ts->tv_sec = tswapl(host_ts->tv_sec);
4427 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
4428 unlock_user_struct(target_ts, target_addr, 1);
4429 return 0;
4432 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
4433 static inline abi_long host_to_target_stat64(void *cpu_env,
4434 abi_ulong target_addr,
4435 struct stat *host_st)
4437 #ifdef TARGET_ARM
4438 if (((CPUARMState *)cpu_env)->eabi) {
4439 struct target_eabi_stat64 *target_st;
4441 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4442 return -TARGET_EFAULT;
4443 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4444 __put_user(host_st->st_dev, &target_st->st_dev);
4445 __put_user(host_st->st_ino, &target_st->st_ino);
4446 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4447 __put_user(host_st->st_ino, &target_st->__st_ino);
4448 #endif
4449 __put_user(host_st->st_mode, &target_st->st_mode);
4450 __put_user(host_st->st_nlink, &target_st->st_nlink);
4451 __put_user(host_st->st_uid, &target_st->st_uid);
4452 __put_user(host_st->st_gid, &target_st->st_gid);
4453 __put_user(host_st->st_rdev, &target_st->st_rdev);
4454 __put_user(host_st->st_size, &target_st->st_size);
4455 __put_user(host_st->st_blksize, &target_st->st_blksize);
4456 __put_user(host_st->st_blocks, &target_st->st_blocks);
4457 __put_user(host_st->st_atime, &target_st->target_st_atime);
4458 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4459 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4460 unlock_user_struct(target_st, target_addr, 1);
4461 } else
4462 #endif
4464 #if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
4465 struct target_stat *target_st;
4466 #else
4467 struct target_stat64 *target_st;
4468 #endif
4470 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4471 return -TARGET_EFAULT;
4472 memset(target_st, 0, sizeof(*target_st));
4473 __put_user(host_st->st_dev, &target_st->st_dev);
4474 __put_user(host_st->st_ino, &target_st->st_ino);
4475 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4476 __put_user(host_st->st_ino, &target_st->__st_ino);
4477 #endif
4478 __put_user(host_st->st_mode, &target_st->st_mode);
4479 __put_user(host_st->st_nlink, &target_st->st_nlink);
4480 __put_user(host_st->st_uid, &target_st->st_uid);
4481 __put_user(host_st->st_gid, &target_st->st_gid);
4482 __put_user(host_st->st_rdev, &target_st->st_rdev);
4483 /* XXX: better use of kernel struct */
4484 __put_user(host_st->st_size, &target_st->st_size);
4485 __put_user(host_st->st_blksize, &target_st->st_blksize);
4486 __put_user(host_st->st_blocks, &target_st->st_blocks);
4487 __put_user(host_st->st_atime, &target_st->target_st_atime);
4488 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4489 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4490 unlock_user_struct(target_st, target_addr, 1);
4493 return 0;
4495 #endif
4497 #if defined(CONFIG_USE_NPTL)
4498 /* ??? Using host futex calls even when target atomic operations
4499 are not really atomic probably breaks things. However implementing
4500 futexes locally would make futexes shared between multiple processes
4501 tricky. However they're probably useless because guest atomic
4502 operations won't work either. */
4503 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4504 target_ulong uaddr2, int val3)
4506 struct timespec ts, *pts;
4507 int base_op;
4509 /* ??? We assume FUTEX_* constants are the same on both host
4510 and target. */
4511 #ifdef FUTEX_CMD_MASK
4512 base_op = op & FUTEX_CMD_MASK;
4513 #else
4514 base_op = op;
4515 #endif
4516 switch (base_op) {
4517 case FUTEX_WAIT:
4518 if (timeout) {
4519 pts = &ts;
4520 target_to_host_timespec(pts, timeout);
4521 } else {
4522 pts = NULL;
4524 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
4525 pts, NULL, 0));
4526 case FUTEX_WAKE:
4527 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4528 case FUTEX_FD:
4529 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4530 case FUTEX_REQUEUE:
4531 case FUTEX_CMP_REQUEUE:
4532 case FUTEX_WAKE_OP:
4533 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4534 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4535 But the prototype takes a `struct timespec *'; insert casts
4536 to satisfy the compiler. We do not need to tswap TIMEOUT
4537 since it's not compared to guest memory. */
4538 pts = (struct timespec *)(uintptr_t) timeout;
4539 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4540 g2h(uaddr2),
4541 (base_op == FUTEX_CMP_REQUEUE
4542 ? tswap32(val3)
4543 : val3)));
4544 default:
4545 return -TARGET_ENOSYS;
4548 #endif
4550 /* Map host to target signal numbers for the wait family of syscalls.
4551 Assume all other status bits are the same. */
4552 static int host_to_target_waitstatus(int status)
4554 if (WIFSIGNALED(status)) {
4555 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4557 if (WIFSTOPPED(status)) {
4558 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4559 | (status & 0xff);
4561 return status;
4564 int get_osversion(void)
4566 static int osversion;
4567 struct new_utsname buf;
4568 const char *s;
4569 int i, n, tmp;
4570 if (osversion)
4571 return osversion;
4572 if (qemu_uname_release && *qemu_uname_release) {
4573 s = qemu_uname_release;
4574 } else {
4575 if (sys_uname(&buf))
4576 return 0;
4577 s = buf.release;
4579 tmp = 0;
4580 for (i = 0; i < 3; i++) {
4581 n = 0;
4582 while (*s >= '0' && *s <= '9') {
4583 n *= 10;
4584 n += *s - '0';
4585 s++;
4587 tmp = (tmp << 8) + n;
4588 if (*s == '.')
4589 s++;
4591 osversion = tmp;
4592 return osversion;
4595 /* do_syscall() should always have a single exit point at the end so
4596 that actions, such as logging of syscall results, can be performed.
4597 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
4598 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4599 abi_long arg2, abi_long arg3, abi_long arg4,
4600 abi_long arg5, abi_long arg6, abi_long arg7,
4601 abi_long arg8)
4603 abi_long ret;
4604 struct stat st;
4605 struct statfs stfs;
4606 void *p;
4608 #ifdef DEBUG
4609 gemu_log("syscall %d", num);
4610 #endif
4611 if(do_strace)
4612 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4614 switch(num) {
4615 case TARGET_NR_exit:
4616 #ifdef CONFIG_USE_NPTL
4617 /* In old applications this may be used to implement _exit(2).
4618 However in threaded applictions it is used for thread termination,
4619 and _exit_group is used for application termination.
4620 Do thread termination if we have more then one thread. */
4621 /* FIXME: This probably breaks if a signal arrives. We should probably
4622 be disabling signals. */
4623 if (first_cpu->next_cpu) {
4624 TaskState *ts;
4625 CPUState **lastp;
4626 CPUState *p;
4628 cpu_list_lock();
4629 lastp = &first_cpu;
4630 p = first_cpu;
4631 while (p && p != (CPUState *)cpu_env) {
4632 lastp = &p->next_cpu;
4633 p = p->next_cpu;
4635 /* If we didn't find the CPU for this thread then something is
4636 horribly wrong. */
4637 if (!p)
4638 abort();
4639 /* Remove the CPU from the list. */
4640 *lastp = p->next_cpu;
4641 cpu_list_unlock();
4642 ts = ((CPUState *)cpu_env)->opaque;
4643 if (ts->child_tidptr) {
4644 put_user_u32(0, ts->child_tidptr);
4645 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4646 NULL, NULL, 0);
4648 thread_env = NULL;
4649 qemu_free(cpu_env);
4650 qemu_free(ts);
4651 pthread_exit(NULL);
4653 #endif
4654 #ifdef TARGET_GPROF
4655 _mcleanup();
4656 #endif
4657 gdb_exit(cpu_env, arg1);
4658 _exit(arg1);
4659 ret = 0; /* avoid warning */
4660 break;
4661 case TARGET_NR_read:
4662 if (arg3 == 0)
4663 ret = 0;
4664 else {
4665 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4666 goto efault;
4667 ret = get_errno(read(arg1, p, arg3));
4668 unlock_user(p, arg2, ret);
4670 break;
4671 case TARGET_NR_write:
4672 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4673 goto efault;
4674 ret = get_errno(write(arg1, p, arg3));
4675 unlock_user(p, arg2, 0);
4676 break;
4677 case TARGET_NR_open:
4678 if (!(p = lock_user_string(arg1)))
4679 goto efault;
4680 ret = get_errno(open(path(p),
4681 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4682 arg3));
4683 unlock_user(p, arg1, 0);
4684 break;
4685 #if defined(TARGET_NR_openat) && defined(__NR_openat)
4686 case TARGET_NR_openat:
4687 if (!(p = lock_user_string(arg2)))
4688 goto efault;
4689 ret = get_errno(sys_openat(arg1,
4690 path(p),
4691 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4692 arg4));
4693 unlock_user(p, arg2, 0);
4694 break;
4695 #endif
4696 case TARGET_NR_close:
4697 ret = get_errno(close(arg1));
4698 break;
4699 case TARGET_NR_brk:
4700 ret = do_brk(arg1);
4701 break;
4702 case TARGET_NR_fork:
4703 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
4704 break;
4705 #ifdef TARGET_NR_waitpid
4706 case TARGET_NR_waitpid:
4708 int status;
4709 ret = get_errno(waitpid(arg1, &status, arg3));
4710 if (!is_error(ret) && arg2
4711 && put_user_s32(host_to_target_waitstatus(status), arg2))
4712 goto efault;
4714 break;
4715 #endif
4716 #ifdef TARGET_NR_waitid
4717 case TARGET_NR_waitid:
4719 siginfo_t info;
4720 info.si_pid = 0;
4721 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4722 if (!is_error(ret) && arg3 && info.si_pid != 0) {
4723 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
4724 goto efault;
4725 host_to_target_siginfo(p, &info);
4726 unlock_user(p, arg3, sizeof(target_siginfo_t));
4729 break;
4730 #endif
4731 #ifdef TARGET_NR_creat /* not on alpha */
4732 case TARGET_NR_creat:
4733 if (!(p = lock_user_string(arg1)))
4734 goto efault;
4735 ret = get_errno(creat(p, arg2));
4736 unlock_user(p, arg1, 0);
4737 break;
4738 #endif
4739 case TARGET_NR_link:
4741 void * p2;
4742 p = lock_user_string(arg1);
4743 p2 = lock_user_string(arg2);
4744 if (!p || !p2)
4745 ret = -TARGET_EFAULT;
4746 else
4747 ret = get_errno(link(p, p2));
4748 unlock_user(p2, arg2, 0);
4749 unlock_user(p, arg1, 0);
4751 break;
4752 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4753 case TARGET_NR_linkat:
4755 void * p2 = NULL;
4756 if (!arg2 || !arg4)
4757 goto efault;
4758 p = lock_user_string(arg2);
4759 p2 = lock_user_string(arg4);
4760 if (!p || !p2)
4761 ret = -TARGET_EFAULT;
4762 else
4763 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
4764 unlock_user(p, arg2, 0);
4765 unlock_user(p2, arg4, 0);
4767 break;
4768 #endif
4769 case TARGET_NR_unlink:
4770 if (!(p = lock_user_string(arg1)))
4771 goto efault;
4772 ret = get_errno(unlink(p));
4773 unlock_user(p, arg1, 0);
4774 break;
4775 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4776 case TARGET_NR_unlinkat:
4777 if (!(p = lock_user_string(arg2)))
4778 goto efault;
4779 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4780 unlock_user(p, arg2, 0);
4781 break;
4782 #endif
4783 case TARGET_NR_execve:
4785 char **argp, **envp;
4786 int argc, envc;
4787 abi_ulong gp;
4788 abi_ulong guest_argp;
4789 abi_ulong guest_envp;
4790 abi_ulong addr;
4791 char **q;
4793 argc = 0;
4794 guest_argp = arg2;
4795 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
4796 if (get_user_ual(addr, gp))
4797 goto efault;
4798 if (!addr)
4799 break;
4800 argc++;
4802 envc = 0;
4803 guest_envp = arg3;
4804 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
4805 if (get_user_ual(addr, gp))
4806 goto efault;
4807 if (!addr)
4808 break;
4809 envc++;
4812 argp = alloca((argc + 1) * sizeof(void *));
4813 envp = alloca((envc + 1) * sizeof(void *));
4815 for (gp = guest_argp, q = argp; gp;
4816 gp += sizeof(abi_ulong), q++) {
4817 if (get_user_ual(addr, gp))
4818 goto execve_efault;
4819 if (!addr)
4820 break;
4821 if (!(*q = lock_user_string(addr)))
4822 goto execve_efault;
4824 *q = NULL;
4826 for (gp = guest_envp, q = envp; gp;
4827 gp += sizeof(abi_ulong), q++) {
4828 if (get_user_ual(addr, gp))
4829 goto execve_efault;
4830 if (!addr)
4831 break;
4832 if (!(*q = lock_user_string(addr)))
4833 goto execve_efault;
4835 *q = NULL;
4837 if (!(p = lock_user_string(arg1)))
4838 goto execve_efault;
4839 ret = get_errno(execve(p, argp, envp));
4840 unlock_user(p, arg1, 0);
4842 goto execve_end;
4844 execve_efault:
4845 ret = -TARGET_EFAULT;
4847 execve_end:
4848 for (gp = guest_argp, q = argp; *q;
4849 gp += sizeof(abi_ulong), q++) {
4850 if (get_user_ual(addr, gp)
4851 || !addr)
4852 break;
4853 unlock_user(*q, addr, 0);
4855 for (gp = guest_envp, q = envp; *q;
4856 gp += sizeof(abi_ulong), q++) {
4857 if (get_user_ual(addr, gp)
4858 || !addr)
4859 break;
4860 unlock_user(*q, addr, 0);
4863 break;
4864 case TARGET_NR_chdir:
4865 if (!(p = lock_user_string(arg1)))
4866 goto efault;
4867 ret = get_errno(chdir(p));
4868 unlock_user(p, arg1, 0);
4869 break;
4870 #ifdef TARGET_NR_time
4871 case TARGET_NR_time:
4873 time_t host_time;
4874 ret = get_errno(time(&host_time));
4875 if (!is_error(ret)
4876 && arg1
4877 && put_user_sal(host_time, arg1))
4878 goto efault;
4880 break;
4881 #endif
4882 case TARGET_NR_mknod:
4883 if (!(p = lock_user_string(arg1)))
4884 goto efault;
4885 ret = get_errno(mknod(p, arg2, arg3));
4886 unlock_user(p, arg1, 0);
4887 break;
4888 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4889 case TARGET_NR_mknodat:
4890 if (!(p = lock_user_string(arg2)))
4891 goto efault;
4892 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4893 unlock_user(p, arg2, 0);
4894 break;
4895 #endif
4896 case TARGET_NR_chmod:
4897 if (!(p = lock_user_string(arg1)))
4898 goto efault;
4899 ret = get_errno(chmod(p, arg2));
4900 unlock_user(p, arg1, 0);
4901 break;
4902 #ifdef TARGET_NR_break
4903 case TARGET_NR_break:
4904 goto unimplemented;
4905 #endif
4906 #ifdef TARGET_NR_oldstat
4907 case TARGET_NR_oldstat:
4908 goto unimplemented;
4909 #endif
4910 case TARGET_NR_lseek:
4911 ret = get_errno(lseek(arg1, arg2, arg3));
4912 break;
4913 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
4914 /* Alpha specific */
4915 case TARGET_NR_getxpid:
4916 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
4917 ret = get_errno(getpid());
4918 break;
4919 #endif
4920 #ifdef TARGET_NR_getpid
4921 case TARGET_NR_getpid:
4922 ret = get_errno(getpid());
4923 break;
4924 #endif
4925 case TARGET_NR_mount:
4927 /* need to look at the data field */
4928 void *p2, *p3;
4929 p = lock_user_string(arg1);
4930 p2 = lock_user_string(arg2);
4931 p3 = lock_user_string(arg3);
4932 if (!p || !p2 || !p3)
4933 ret = -TARGET_EFAULT;
4934 else {
4935 /* FIXME - arg5 should be locked, but it isn't clear how to
4936 * do that since it's not guaranteed to be a NULL-terminated
4937 * string.
4939 if ( ! arg5 )
4940 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
4941 else
4942 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4944 unlock_user(p, arg1, 0);
4945 unlock_user(p2, arg2, 0);
4946 unlock_user(p3, arg3, 0);
4947 break;
4949 #ifdef TARGET_NR_umount
4950 case TARGET_NR_umount:
4951 if (!(p = lock_user_string(arg1)))
4952 goto efault;
4953 ret = get_errno(umount(p));
4954 unlock_user(p, arg1, 0);
4955 break;
4956 #endif
4957 #ifdef TARGET_NR_stime /* not on alpha */
4958 case TARGET_NR_stime:
4960 time_t host_time;
4961 if (get_user_sal(host_time, arg1))
4962 goto efault;
4963 ret = get_errno(stime(&host_time));
4965 break;
4966 #endif
4967 case TARGET_NR_ptrace:
4968 goto unimplemented;
4969 #ifdef TARGET_NR_alarm /* not on alpha */
4970 case TARGET_NR_alarm:
4971 ret = alarm(arg1);
4972 break;
4973 #endif
4974 #ifdef TARGET_NR_oldfstat
4975 case TARGET_NR_oldfstat:
4976 goto unimplemented;
4977 #endif
4978 #ifdef TARGET_NR_pause /* not on alpha */
4979 case TARGET_NR_pause:
4980 ret = get_errno(pause());
4981 break;
4982 #endif
4983 #ifdef TARGET_NR_utime
4984 case TARGET_NR_utime:
4986 struct utimbuf tbuf, *host_tbuf;
4987 struct target_utimbuf *target_tbuf;
4988 if (arg2) {
4989 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4990 goto efault;
4991 tbuf.actime = tswapl(target_tbuf->actime);
4992 tbuf.modtime = tswapl(target_tbuf->modtime);
4993 unlock_user_struct(target_tbuf, arg2, 0);
4994 host_tbuf = &tbuf;
4995 } else {
4996 host_tbuf = NULL;
4998 if (!(p = lock_user_string(arg1)))
4999 goto efault;
5000 ret = get_errno(utime(p, host_tbuf));
5001 unlock_user(p, arg1, 0);
5003 break;
5004 #endif
5005 case TARGET_NR_utimes:
5007 struct timeval *tvp, tv[2];
5008 if (arg2) {
5009 if (copy_from_user_timeval(&tv[0], arg2)
5010 || copy_from_user_timeval(&tv[1],
5011 arg2 + sizeof(struct target_timeval)))
5012 goto efault;
5013 tvp = tv;
5014 } else {
5015 tvp = NULL;
5017 if (!(p = lock_user_string(arg1)))
5018 goto efault;
5019 ret = get_errno(utimes(p, tvp));
5020 unlock_user(p, arg1, 0);
5022 break;
5023 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
5024 case TARGET_NR_futimesat:
5026 struct timeval *tvp, tv[2];
5027 if (arg3) {
5028 if (copy_from_user_timeval(&tv[0], arg3)
5029 || copy_from_user_timeval(&tv[1],
5030 arg3 + sizeof(struct target_timeval)))
5031 goto efault;
5032 tvp = tv;
5033 } else {
5034 tvp = NULL;
5036 if (!(p = lock_user_string(arg2)))
5037 goto efault;
5038 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
5039 unlock_user(p, arg2, 0);
5041 break;
5042 #endif
5043 #ifdef TARGET_NR_stty
5044 case TARGET_NR_stty:
5045 goto unimplemented;
5046 #endif
5047 #ifdef TARGET_NR_gtty
5048 case TARGET_NR_gtty:
5049 goto unimplemented;
5050 #endif
5051 case TARGET_NR_access:
5052 if (!(p = lock_user_string(arg1)))
5053 goto efault;
5054 ret = get_errno(access(path(p), arg2));
5055 unlock_user(p, arg1, 0);
5056 break;
5057 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5058 case TARGET_NR_faccessat:
5059 if (!(p = lock_user_string(arg2)))
5060 goto efault;
5061 ret = get_errno(sys_faccessat(arg1, p, arg3));
5062 unlock_user(p, arg2, 0);
5063 break;
5064 #endif
5065 #ifdef TARGET_NR_nice /* not on alpha */
5066 case TARGET_NR_nice:
5067 ret = get_errno(nice(arg1));
5068 break;
5069 #endif
5070 #ifdef TARGET_NR_ftime
5071 case TARGET_NR_ftime:
5072 goto unimplemented;
5073 #endif
5074 case TARGET_NR_sync:
5075 sync();
5076 ret = 0;
5077 break;
5078 case TARGET_NR_kill:
5079 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
5080 break;
5081 case TARGET_NR_rename:
5083 void *p2;
5084 p = lock_user_string(arg1);
5085 p2 = lock_user_string(arg2);
5086 if (!p || !p2)
5087 ret = -TARGET_EFAULT;
5088 else
5089 ret = get_errno(rename(p, p2));
5090 unlock_user(p2, arg2, 0);
5091 unlock_user(p, arg1, 0);
5093 break;
5094 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
5095 case TARGET_NR_renameat:
5097 void *p2;
5098 p = lock_user_string(arg2);
5099 p2 = lock_user_string(arg4);
5100 if (!p || !p2)
5101 ret = -TARGET_EFAULT;
5102 else
5103 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
5104 unlock_user(p2, arg4, 0);
5105 unlock_user(p, arg2, 0);
5107 break;
5108 #endif
5109 case TARGET_NR_mkdir:
5110 if (!(p = lock_user_string(arg1)))
5111 goto efault;
5112 ret = get_errno(mkdir(p, arg2));
5113 unlock_user(p, arg1, 0);
5114 break;
5115 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
5116 case TARGET_NR_mkdirat:
5117 if (!(p = lock_user_string(arg2)))
5118 goto efault;
5119 ret = get_errno(sys_mkdirat(arg1, p, arg3));
5120 unlock_user(p, arg2, 0);
5121 break;
5122 #endif
5123 case TARGET_NR_rmdir:
5124 if (!(p = lock_user_string(arg1)))
5125 goto efault;
5126 ret = get_errno(rmdir(p));
5127 unlock_user(p, arg1, 0);
5128 break;
5129 case TARGET_NR_dup:
5130 ret = get_errno(dup(arg1));
5131 break;
5132 case TARGET_NR_pipe:
5133 ret = do_pipe(cpu_env, arg1, 0, 0);
5134 break;
5135 #ifdef TARGET_NR_pipe2
5136 case TARGET_NR_pipe2:
5137 ret = do_pipe(cpu_env, arg1, arg2, 1);
5138 break;
5139 #endif
5140 case TARGET_NR_times:
5142 struct target_tms *tmsp;
5143 struct tms tms;
5144 ret = get_errno(times(&tms));
5145 if (arg1) {
5146 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5147 if (!tmsp)
5148 goto efault;
5149 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
5150 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
5151 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
5152 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
5154 if (!is_error(ret))
5155 ret = host_to_target_clock_t(ret);
5157 break;
5158 #ifdef TARGET_NR_prof
5159 case TARGET_NR_prof:
5160 goto unimplemented;
5161 #endif
5162 #ifdef TARGET_NR_signal
5163 case TARGET_NR_signal:
5164 goto unimplemented;
5165 #endif
5166 case TARGET_NR_acct:
5167 if (arg1 == 0) {
5168 ret = get_errno(acct(NULL));
5169 } else {
5170 if (!(p = lock_user_string(arg1)))
5171 goto efault;
5172 ret = get_errno(acct(path(p)));
5173 unlock_user(p, arg1, 0);
5175 break;
5176 #ifdef TARGET_NR_umount2 /* not on alpha */
5177 case TARGET_NR_umount2:
5178 if (!(p = lock_user_string(arg1)))
5179 goto efault;
5180 ret = get_errno(umount2(p, arg2));
5181 unlock_user(p, arg1, 0);
5182 break;
5183 #endif
5184 #ifdef TARGET_NR_lock
5185 case TARGET_NR_lock:
5186 goto unimplemented;
5187 #endif
5188 case TARGET_NR_ioctl:
5189 ret = do_ioctl(arg1, arg2, arg3);
5190 break;
5191 case TARGET_NR_fcntl:
5192 ret = do_fcntl(arg1, arg2, arg3);
5193 break;
5194 #ifdef TARGET_NR_mpx
5195 case TARGET_NR_mpx:
5196 goto unimplemented;
5197 #endif
5198 case TARGET_NR_setpgid:
5199 ret = get_errno(setpgid(arg1, arg2));
5200 break;
5201 #ifdef TARGET_NR_ulimit
5202 case TARGET_NR_ulimit:
5203 goto unimplemented;
5204 #endif
5205 #ifdef TARGET_NR_oldolduname
5206 case TARGET_NR_oldolduname:
5207 goto unimplemented;
5208 #endif
5209 case TARGET_NR_umask:
5210 ret = get_errno(umask(arg1));
5211 break;
5212 case TARGET_NR_chroot:
5213 if (!(p = lock_user_string(arg1)))
5214 goto efault;
5215 ret = get_errno(chroot(p));
5216 unlock_user(p, arg1, 0);
5217 break;
5218 case TARGET_NR_ustat:
5219 goto unimplemented;
5220 case TARGET_NR_dup2:
5221 ret = get_errno(dup2(arg1, arg2));
5222 break;
5223 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5224 case TARGET_NR_dup3:
5225 ret = get_errno(dup3(arg1, arg2, arg3));
5226 break;
5227 #endif
5228 #ifdef TARGET_NR_getppid /* not on alpha */
5229 case TARGET_NR_getppid:
5230 ret = get_errno(getppid());
5231 break;
5232 #endif
5233 case TARGET_NR_getpgrp:
5234 ret = get_errno(getpgrp());
5235 break;
5236 case TARGET_NR_setsid:
5237 ret = get_errno(setsid());
5238 break;
5239 #ifdef TARGET_NR_sigaction
5240 case TARGET_NR_sigaction:
5242 #if defined(TARGET_ALPHA)
5243 struct target_sigaction act, oact, *pact = 0;
5244 struct target_old_sigaction *old_act;
5245 if (arg2) {
5246 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5247 goto efault;
5248 act._sa_handler = old_act->_sa_handler;
5249 target_siginitset(&act.sa_mask, old_act->sa_mask);
5250 act.sa_flags = old_act->sa_flags;
5251 act.sa_restorer = 0;
5252 unlock_user_struct(old_act, arg2, 0);
5253 pact = &act;
5255 ret = get_errno(do_sigaction(arg1, pact, &oact));
5256 if (!is_error(ret) && arg3) {
5257 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5258 goto efault;
5259 old_act->_sa_handler = oact._sa_handler;
5260 old_act->sa_mask = oact.sa_mask.sig[0];
5261 old_act->sa_flags = oact.sa_flags;
5262 unlock_user_struct(old_act, arg3, 1);
5264 #elif defined(TARGET_MIPS)
5265 struct target_sigaction act, oact, *pact, *old_act;
5267 if (arg2) {
5268 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5269 goto efault;
5270 act._sa_handler = old_act->_sa_handler;
5271 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5272 act.sa_flags = old_act->sa_flags;
5273 unlock_user_struct(old_act, arg2, 0);
5274 pact = &act;
5275 } else {
5276 pact = NULL;
5279 ret = get_errno(do_sigaction(arg1, pact, &oact));
5281 if (!is_error(ret) && arg3) {
5282 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5283 goto efault;
5284 old_act->_sa_handler = oact._sa_handler;
5285 old_act->sa_flags = oact.sa_flags;
5286 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5287 old_act->sa_mask.sig[1] = 0;
5288 old_act->sa_mask.sig[2] = 0;
5289 old_act->sa_mask.sig[3] = 0;
5290 unlock_user_struct(old_act, arg3, 1);
5292 #else
5293 struct target_old_sigaction *old_act;
5294 struct target_sigaction act, oact, *pact;
5295 if (arg2) {
5296 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5297 goto efault;
5298 act._sa_handler = old_act->_sa_handler;
5299 target_siginitset(&act.sa_mask, old_act->sa_mask);
5300 act.sa_flags = old_act->sa_flags;
5301 act.sa_restorer = old_act->sa_restorer;
5302 unlock_user_struct(old_act, arg2, 0);
5303 pact = &act;
5304 } else {
5305 pact = NULL;
5307 ret = get_errno(do_sigaction(arg1, pact, &oact));
5308 if (!is_error(ret) && arg3) {
5309 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5310 goto efault;
5311 old_act->_sa_handler = oact._sa_handler;
5312 old_act->sa_mask = oact.sa_mask.sig[0];
5313 old_act->sa_flags = oact.sa_flags;
5314 old_act->sa_restorer = oact.sa_restorer;
5315 unlock_user_struct(old_act, arg3, 1);
5317 #endif
5319 break;
5320 #endif
5321 case TARGET_NR_rt_sigaction:
5323 #if defined(TARGET_ALPHA)
5324 struct target_sigaction act, oact, *pact = 0;
5325 struct target_rt_sigaction *rt_act;
5326 /* ??? arg4 == sizeof(sigset_t). */
5327 if (arg2) {
5328 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5329 goto efault;
5330 act._sa_handler = rt_act->_sa_handler;
5331 act.sa_mask = rt_act->sa_mask;
5332 act.sa_flags = rt_act->sa_flags;
5333 act.sa_restorer = arg5;
5334 unlock_user_struct(rt_act, arg2, 0);
5335 pact = &act;
5337 ret = get_errno(do_sigaction(arg1, pact, &oact));
5338 if (!is_error(ret) && arg3) {
5339 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5340 goto efault;
5341 rt_act->_sa_handler = oact._sa_handler;
5342 rt_act->sa_mask = oact.sa_mask;
5343 rt_act->sa_flags = oact.sa_flags;
5344 unlock_user_struct(rt_act, arg3, 1);
5346 #else
5347 struct target_sigaction *act;
5348 struct target_sigaction *oact;
5350 if (arg2) {
5351 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5352 goto efault;
5353 } else
5354 act = NULL;
5355 if (arg3) {
5356 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5357 ret = -TARGET_EFAULT;
5358 goto rt_sigaction_fail;
5360 } else
5361 oact = NULL;
5362 ret = get_errno(do_sigaction(arg1, act, oact));
5363 rt_sigaction_fail:
5364 if (act)
5365 unlock_user_struct(act, arg2, 0);
5366 if (oact)
5367 unlock_user_struct(oact, arg3, 1);
5368 #endif
5370 break;
5371 #ifdef TARGET_NR_sgetmask /* not on alpha */
5372 case TARGET_NR_sgetmask:
5374 sigset_t cur_set;
5375 abi_ulong target_set;
5376 sigprocmask(0, NULL, &cur_set);
5377 host_to_target_old_sigset(&target_set, &cur_set);
5378 ret = target_set;
5380 break;
5381 #endif
5382 #ifdef TARGET_NR_ssetmask /* not on alpha */
5383 case TARGET_NR_ssetmask:
5385 sigset_t set, oset, cur_set;
5386 abi_ulong target_set = arg1;
5387 sigprocmask(0, NULL, &cur_set);
5388 target_to_host_old_sigset(&set, &target_set);
5389 sigorset(&set, &set, &cur_set);
5390 sigprocmask(SIG_SETMASK, &set, &oset);
5391 host_to_target_old_sigset(&target_set, &oset);
5392 ret = target_set;
5394 break;
5395 #endif
5396 #ifdef TARGET_NR_sigprocmask
5397 case TARGET_NR_sigprocmask:
5399 #if defined(TARGET_ALPHA)
5400 sigset_t set, oldset;
5401 abi_ulong mask;
5402 int how;
5404 switch (arg1) {
5405 case TARGET_SIG_BLOCK:
5406 how = SIG_BLOCK;
5407 break;
5408 case TARGET_SIG_UNBLOCK:
5409 how = SIG_UNBLOCK;
5410 break;
5411 case TARGET_SIG_SETMASK:
5412 how = SIG_SETMASK;
5413 break;
5414 default:
5415 ret = -TARGET_EINVAL;
5416 goto fail;
5418 mask = arg2;
5419 target_to_host_old_sigset(&set, &mask);
5421 ret = get_errno(sigprocmask(how, &set, &oldset));
5423 if (!is_error(ret)) {
5424 host_to_target_old_sigset(&mask, &oldset);
5425 ret = mask;
5426 ((CPUAlphaState *)cpu_env)->[IR_V0] = 0; /* force no error */
5428 #else
5429 sigset_t set, oldset, *set_ptr;
5430 int how;
5432 if (arg2) {
5433 switch (arg1) {
5434 case TARGET_SIG_BLOCK:
5435 how = SIG_BLOCK;
5436 break;
5437 case TARGET_SIG_UNBLOCK:
5438 how = SIG_UNBLOCK;
5439 break;
5440 case TARGET_SIG_SETMASK:
5441 how = SIG_SETMASK;
5442 break;
5443 default:
5444 ret = -TARGET_EINVAL;
5445 goto fail;
5447 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
5448 goto efault;
5449 target_to_host_old_sigset(&set, p);
5450 unlock_user(p, arg2, 0);
5451 set_ptr = &set;
5452 } else {
5453 how = 0;
5454 set_ptr = NULL;
5456 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
5457 if (!is_error(ret) && arg3) {
5458 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
5459 goto efault;
5460 host_to_target_old_sigset(p, &oldset);
5461 unlock_user(p, arg3, sizeof(target_sigset_t));
5463 #endif
5465 break;
5466 #endif
5467 case TARGET_NR_rt_sigprocmask:
5469 int how = arg1;
5470 sigset_t set, oldset, *set_ptr;
5472 if (arg2) {
5473 switch(how) {
5474 case TARGET_SIG_BLOCK:
5475 how = SIG_BLOCK;
5476 break;
5477 case TARGET_SIG_UNBLOCK:
5478 how = SIG_UNBLOCK;
5479 break;
5480 case TARGET_SIG_SETMASK:
5481 how = SIG_SETMASK;
5482 break;
5483 default:
5484 ret = -TARGET_EINVAL;
5485 goto fail;
5487 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
5488 goto efault;
5489 target_to_host_sigset(&set, p);
5490 unlock_user(p, arg2, 0);
5491 set_ptr = &set;
5492 } else {
5493 how = 0;
5494 set_ptr = NULL;
5496 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
5497 if (!is_error(ret) && arg3) {
5498 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
5499 goto efault;
5500 host_to_target_sigset(p, &oldset);
5501 unlock_user(p, arg3, sizeof(target_sigset_t));
5504 break;
5505 #ifdef TARGET_NR_sigpending
5506 case TARGET_NR_sigpending:
5508 sigset_t set;
5509 ret = get_errno(sigpending(&set));
5510 if (!is_error(ret)) {
5511 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
5512 goto efault;
5513 host_to_target_old_sigset(p, &set);
5514 unlock_user(p, arg1, sizeof(target_sigset_t));
5517 break;
5518 #endif
5519 case TARGET_NR_rt_sigpending:
5521 sigset_t set;
5522 ret = get_errno(sigpending(&set));
5523 if (!is_error(ret)) {
5524 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
5525 goto efault;
5526 host_to_target_sigset(p, &set);
5527 unlock_user(p, arg1, sizeof(target_sigset_t));
5530 break;
5531 #ifdef TARGET_NR_sigsuspend
5532 case TARGET_NR_sigsuspend:
5534 sigset_t set;
5535 #if defined(TARGET_ALPHA)
5536 abi_ulong mask = arg1;
5537 target_to_host_old_sigset(&set, &mask);
5538 #else
5539 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
5540 goto efault;
5541 target_to_host_old_sigset(&set, p);
5542 unlock_user(p, arg1, 0);
5543 #endif
5544 ret = get_errno(sigsuspend(&set));
5546 break;
5547 #endif
5548 case TARGET_NR_rt_sigsuspend:
5550 sigset_t set;
5551 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
5552 goto efault;
5553 target_to_host_sigset(&set, p);
5554 unlock_user(p, arg1, 0);
5555 ret = get_errno(sigsuspend(&set));
5557 break;
5558 case TARGET_NR_rt_sigtimedwait:
5560 sigset_t set;
5561 struct timespec uts, *puts;
5562 siginfo_t uinfo;
5564 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
5565 goto efault;
5566 target_to_host_sigset(&set, p);
5567 unlock_user(p, arg1, 0);
5568 if (arg3) {
5569 puts = &uts;
5570 target_to_host_timespec(puts, arg3);
5571 } else {
5572 puts = NULL;
5574 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
5575 if (!is_error(ret) && arg2) {
5576 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
5577 goto efault;
5578 host_to_target_siginfo(p, &uinfo);
5579 unlock_user(p, arg2, sizeof(target_siginfo_t));
5582 break;
5583 case TARGET_NR_rt_sigqueueinfo:
5585 siginfo_t uinfo;
5586 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
5587 goto efault;
5588 target_to_host_siginfo(&uinfo, p);
5589 unlock_user(p, arg1, 0);
5590 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5592 break;
5593 #ifdef TARGET_NR_sigreturn
5594 case TARGET_NR_sigreturn:
5595 /* NOTE: ret is eax, so not transcoding must be done */
5596 ret = do_sigreturn(cpu_env);
5597 break;
5598 #endif
5599 case TARGET_NR_rt_sigreturn:
5600 /* NOTE: ret is eax, so not transcoding must be done */
5601 ret = do_rt_sigreturn(cpu_env);
5602 break;
5603 case TARGET_NR_sethostname:
5604 if (!(p = lock_user_string(arg1)))
5605 goto efault;
5606 ret = get_errno(sethostname(p, arg2));
5607 unlock_user(p, arg1, 0);
5608 break;
5609 case TARGET_NR_setrlimit:
5611 int resource = target_to_host_resource(arg1);
5612 struct target_rlimit *target_rlim;
5613 struct rlimit rlim;
5614 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5615 goto efault;
5616 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
5617 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
5618 unlock_user_struct(target_rlim, arg2, 0);
5619 ret = get_errno(setrlimit(resource, &rlim));
5621 break;
5622 case TARGET_NR_getrlimit:
5624 int resource = target_to_host_resource(arg1);
5625 struct target_rlimit *target_rlim;
5626 struct rlimit rlim;
5628 ret = get_errno(getrlimit(resource, &rlim));
5629 if (!is_error(ret)) {
5630 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5631 goto efault;
5632 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
5633 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
5634 unlock_user_struct(target_rlim, arg2, 1);
5637 break;
5638 case TARGET_NR_getrusage:
5640 struct rusage rusage;
5641 ret = get_errno(getrusage(arg1, &rusage));
5642 if (!is_error(ret)) {
5643 host_to_target_rusage(arg2, &rusage);
5646 break;
5647 case TARGET_NR_gettimeofday:
5649 struct timeval tv;
5650 ret = get_errno(gettimeofday(&tv, NULL));
5651 if (!is_error(ret)) {
5652 if (copy_to_user_timeval(arg1, &tv))
5653 goto efault;
5656 break;
5657 case TARGET_NR_settimeofday:
5659 struct timeval tv;
5660 if (copy_from_user_timeval(&tv, arg1))
5661 goto efault;
5662 ret = get_errno(settimeofday(&tv, NULL));
5664 break;
5665 #if defined(TARGET_NR_select) && !defined(TARGET_S390X) && !defined(TARGET_S390)
5666 case TARGET_NR_select:
5668 struct target_sel_arg_struct *sel;
5669 abi_ulong inp, outp, exp, tvp;
5670 long nsel;
5672 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5673 goto efault;
5674 nsel = tswapl(sel->n);
5675 inp = tswapl(sel->inp);
5676 outp = tswapl(sel->outp);
5677 exp = tswapl(sel->exp);
5678 tvp = tswapl(sel->tvp);
5679 unlock_user_struct(sel, arg1, 0);
5680 ret = do_select(nsel, inp, outp, exp, tvp);
5682 break;
5683 #endif
5684 #ifdef TARGET_NR_pselect6
5685 case TARGET_NR_pselect6:
5687 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
5688 fd_set rfds, wfds, efds;
5689 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
5690 struct timespec ts, *ts_ptr;
5693 * The 6th arg is actually two args smashed together,
5694 * so we cannot use the C library.
5696 sigset_t set;
5697 struct {
5698 sigset_t *set;
5699 size_t size;
5700 } sig, *sig_ptr;
5702 abi_ulong arg_sigset, arg_sigsize, *arg7;
5703 target_sigset_t *target_sigset;
5705 n = arg1;
5706 rfd_addr = arg2;
5707 wfd_addr = arg3;
5708 efd_addr = arg4;
5709 ts_addr = arg5;
5711 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
5712 if (ret) {
5713 goto fail;
5715 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
5716 if (ret) {
5717 goto fail;
5719 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
5720 if (ret) {
5721 goto fail;
5725 * This takes a timespec, and not a timeval, so we cannot
5726 * use the do_select() helper ...
5728 if (ts_addr) {
5729 if (target_to_host_timespec(&ts, ts_addr)) {
5730 goto efault;
5732 ts_ptr = &ts;
5733 } else {
5734 ts_ptr = NULL;
5737 /* Extract the two packed args for the sigset */
5738 if (arg6) {
5739 sig_ptr = &sig;
5740 sig.size = _NSIG / 8;
5742 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
5743 if (!arg7) {
5744 goto efault;
5746 arg_sigset = tswapl(arg7[0]);
5747 arg_sigsize = tswapl(arg7[1]);
5748 unlock_user(arg7, arg6, 0);
5750 if (arg_sigset) {
5751 sig.set = &set;
5752 if (arg_sigsize != sizeof(*target_sigset)) {
5753 /* Like the kernel, we enforce correct size sigsets */
5754 ret = -TARGET_EINVAL;
5755 goto fail;
5757 target_sigset = lock_user(VERIFY_READ, arg_sigset,
5758 sizeof(*target_sigset), 1);
5759 if (!target_sigset) {
5760 goto efault;
5762 target_to_host_sigset(&set, target_sigset);
5763 unlock_user(target_sigset, arg_sigset, 0);
5764 } else {
5765 sig.set = NULL;
5767 } else {
5768 sig_ptr = NULL;
5771 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
5772 ts_ptr, sig_ptr));
5774 if (!is_error(ret)) {
5775 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
5776 goto efault;
5777 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
5778 goto efault;
5779 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
5780 goto efault;
5782 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
5783 goto efault;
5786 break;
5787 #endif
5788 case TARGET_NR_symlink:
5790 void *p2;
5791 p = lock_user_string(arg1);
5792 p2 = lock_user_string(arg2);
5793 if (!p || !p2)
5794 ret = -TARGET_EFAULT;
5795 else
5796 ret = get_errno(symlink(p, p2));
5797 unlock_user(p2, arg2, 0);
5798 unlock_user(p, arg1, 0);
5800 break;
5801 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5802 case TARGET_NR_symlinkat:
5804 void *p2;
5805 p = lock_user_string(arg1);
5806 p2 = lock_user_string(arg3);
5807 if (!p || !p2)
5808 ret = -TARGET_EFAULT;
5809 else
5810 ret = get_errno(sys_symlinkat(p, arg2, p2));
5811 unlock_user(p2, arg3, 0);
5812 unlock_user(p, arg1, 0);
5814 break;
5815 #endif
5816 #ifdef TARGET_NR_oldlstat
5817 case TARGET_NR_oldlstat:
5818 goto unimplemented;
5819 #endif
5820 case TARGET_NR_readlink:
5822 void *p2, *temp;
5823 p = lock_user_string(arg1);
5824 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5825 if (!p || !p2)
5826 ret = -TARGET_EFAULT;
5827 else {
5828 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5829 char real[PATH_MAX];
5830 temp = realpath(exec_path,real);
5831 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5832 snprintf((char *)p2, arg3, "%s", real);
5834 else
5835 ret = get_errno(readlink(path(p), p2, arg3));
5837 unlock_user(p2, arg2, ret);
5838 unlock_user(p, arg1, 0);
5840 break;
5841 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5842 case TARGET_NR_readlinkat:
5844 void *p2;
5845 p = lock_user_string(arg2);
5846 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5847 if (!p || !p2)
5848 ret = -TARGET_EFAULT;
5849 else
5850 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
5851 unlock_user(p2, arg3, ret);
5852 unlock_user(p, arg2, 0);
5854 break;
5855 #endif
5856 #ifdef TARGET_NR_uselib
5857 case TARGET_NR_uselib:
5858 goto unimplemented;
5859 #endif
5860 #ifdef TARGET_NR_swapon
5861 case TARGET_NR_swapon:
5862 if (!(p = lock_user_string(arg1)))
5863 goto efault;
5864 ret = get_errno(swapon(p, arg2));
5865 unlock_user(p, arg1, 0);
5866 break;
5867 #endif
5868 case TARGET_NR_reboot:
5869 goto unimplemented;
5870 #ifdef TARGET_NR_readdir
5871 case TARGET_NR_readdir:
5872 goto unimplemented;
5873 #endif
5874 #ifdef TARGET_NR_mmap
5875 case TARGET_NR_mmap:
5876 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || \
5877 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
5878 || defined(TARGET_S390X)
5880 abi_ulong *v;
5881 abi_ulong v1, v2, v3, v4, v5, v6;
5882 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5883 goto efault;
5884 v1 = tswapl(v[0]);
5885 v2 = tswapl(v[1]);
5886 v3 = tswapl(v[2]);
5887 v4 = tswapl(v[3]);
5888 v5 = tswapl(v[4]);
5889 v6 = tswapl(v[5]);
5890 unlock_user(v, arg1, 0);
5891 ret = get_errno(target_mmap(v1, v2, v3,
5892 target_to_host_bitmask(v4, mmap_flags_tbl),
5893 v5, v6));
5895 #else
5896 ret = get_errno(target_mmap(arg1, arg2, arg3,
5897 target_to_host_bitmask(arg4, mmap_flags_tbl),
5898 arg5,
5899 arg6));
5900 #endif
5901 break;
5902 #endif
5903 #ifdef TARGET_NR_mmap2
5904 case TARGET_NR_mmap2:
5905 #ifndef MMAP_SHIFT
5906 #define MMAP_SHIFT 12
5907 #endif
5908 ret = get_errno(target_mmap(arg1, arg2, arg3,
5909 target_to_host_bitmask(arg4, mmap_flags_tbl),
5910 arg5,
5911 arg6 << MMAP_SHIFT));
5912 break;
5913 #endif
5914 case TARGET_NR_munmap:
5915 ret = get_errno(target_munmap(arg1, arg2));
5916 break;
5917 case TARGET_NR_mprotect:
5919 TaskState *ts = ((CPUState *)cpu_env)->opaque;
5920 /* Special hack to detect libc making the stack executable. */
5921 if ((arg3 & PROT_GROWSDOWN)
5922 && arg1 >= ts->info->stack_limit
5923 && arg1 <= ts->info->start_stack) {
5924 arg3 &= ~PROT_GROWSDOWN;
5925 arg2 = arg2 + arg1 - ts->info->stack_limit;
5926 arg1 = ts->info->stack_limit;
5929 ret = get_errno(target_mprotect(arg1, arg2, arg3));
5930 break;
5931 #ifdef TARGET_NR_mremap
5932 case TARGET_NR_mremap:
5933 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
5934 break;
5935 #endif
5936 /* ??? msync/mlock/munlock are broken for softmmu. */
5937 #ifdef TARGET_NR_msync
5938 case TARGET_NR_msync:
5939 ret = get_errno(msync(g2h(arg1), arg2, arg3));
5940 break;
5941 #endif
5942 #ifdef TARGET_NR_mlock
5943 case TARGET_NR_mlock:
5944 ret = get_errno(mlock(g2h(arg1), arg2));
5945 break;
5946 #endif
5947 #ifdef TARGET_NR_munlock
5948 case TARGET_NR_munlock:
5949 ret = get_errno(munlock(g2h(arg1), arg2));
5950 break;
5951 #endif
5952 #ifdef TARGET_NR_mlockall
5953 case TARGET_NR_mlockall:
5954 ret = get_errno(mlockall(arg1));
5955 break;
5956 #endif
5957 #ifdef TARGET_NR_munlockall
5958 case TARGET_NR_munlockall:
5959 ret = get_errno(munlockall());
5960 break;
5961 #endif
5962 case TARGET_NR_truncate:
5963 if (!(p = lock_user_string(arg1)))
5964 goto efault;
5965 ret = get_errno(truncate(p, arg2));
5966 unlock_user(p, arg1, 0);
5967 break;
5968 case TARGET_NR_ftruncate:
5969 ret = get_errno(ftruncate(arg1, arg2));
5970 break;
5971 case TARGET_NR_fchmod:
5972 ret = get_errno(fchmod(arg1, arg2));
5973 break;
5974 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5975 case TARGET_NR_fchmodat:
5976 if (!(p = lock_user_string(arg2)))
5977 goto efault;
5978 ret = get_errno(sys_fchmodat(arg1, p, arg3));
5979 unlock_user(p, arg2, 0);
5980 break;
5981 #endif
5982 case TARGET_NR_getpriority:
5983 /* libc does special remapping of the return value of
5984 * sys_getpriority() so it's just easiest to call
5985 * sys_getpriority() directly rather than through libc. */
5986 ret = get_errno(sys_getpriority(arg1, arg2));
5987 break;
5988 case TARGET_NR_setpriority:
5989 ret = get_errno(setpriority(arg1, arg2, arg3));
5990 break;
5991 #ifdef TARGET_NR_profil
5992 case TARGET_NR_profil:
5993 goto unimplemented;
5994 #endif
5995 case TARGET_NR_statfs:
5996 if (!(p = lock_user_string(arg1)))
5997 goto efault;
5998 ret = get_errno(statfs(path(p), &stfs));
5999 unlock_user(p, arg1, 0);
6000 convert_statfs:
6001 if (!is_error(ret)) {
6002 struct target_statfs *target_stfs;
6004 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6005 goto efault;
6006 __put_user(stfs.f_type, &target_stfs->f_type);
6007 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6008 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6009 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6010 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6011 __put_user(stfs.f_files, &target_stfs->f_files);
6012 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6013 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6014 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6015 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
6016 unlock_user_struct(target_stfs, arg2, 1);
6018 break;
6019 case TARGET_NR_fstatfs:
6020 ret = get_errno(fstatfs(arg1, &stfs));
6021 goto convert_statfs;
6022 #ifdef TARGET_NR_statfs64
6023 case TARGET_NR_statfs64:
6024 if (!(p = lock_user_string(arg1)))
6025 goto efault;
6026 ret = get_errno(statfs(path(p), &stfs));
6027 unlock_user(p, arg1, 0);
6028 convert_statfs64:
6029 if (!is_error(ret)) {
6030 struct target_statfs64 *target_stfs;
6032 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6033 goto efault;
6034 __put_user(stfs.f_type, &target_stfs->f_type);
6035 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6036 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6037 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6038 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6039 __put_user(stfs.f_files, &target_stfs->f_files);
6040 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6041 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6042 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6043 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
6044 unlock_user_struct(target_stfs, arg3, 1);
6046 break;
6047 case TARGET_NR_fstatfs64:
6048 ret = get_errno(fstatfs(arg1, &stfs));
6049 goto convert_statfs64;
6050 #endif
6051 #ifdef TARGET_NR_ioperm
6052 case TARGET_NR_ioperm:
6053 goto unimplemented;
6054 #endif
6055 #ifdef TARGET_NR_socketcall
6056 case TARGET_NR_socketcall:
6057 ret = do_socketcall(arg1, arg2);
6058 break;
6059 #endif
6060 #ifdef TARGET_NR_accept
6061 case TARGET_NR_accept:
6062 ret = do_accept(arg1, arg2, arg3);
6063 break;
6064 #endif
6065 #ifdef TARGET_NR_bind
6066 case TARGET_NR_bind:
6067 ret = do_bind(arg1, arg2, arg3);
6068 break;
6069 #endif
6070 #ifdef TARGET_NR_connect
6071 case TARGET_NR_connect:
6072 ret = do_connect(arg1, arg2, arg3);
6073 break;
6074 #endif
6075 #ifdef TARGET_NR_getpeername
6076 case TARGET_NR_getpeername:
6077 ret = do_getpeername(arg1, arg2, arg3);
6078 break;
6079 #endif
6080 #ifdef TARGET_NR_getsockname
6081 case TARGET_NR_getsockname:
6082 ret = do_getsockname(arg1, arg2, arg3);
6083 break;
6084 #endif
6085 #ifdef TARGET_NR_getsockopt
6086 case TARGET_NR_getsockopt:
6087 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6088 break;
6089 #endif
6090 #ifdef TARGET_NR_listen
6091 case TARGET_NR_listen:
6092 ret = get_errno(listen(arg1, arg2));
6093 break;
6094 #endif
6095 #ifdef TARGET_NR_recv
6096 case TARGET_NR_recv:
6097 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
6098 break;
6099 #endif
6100 #ifdef TARGET_NR_recvfrom
6101 case TARGET_NR_recvfrom:
6102 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
6103 break;
6104 #endif
6105 #ifdef TARGET_NR_recvmsg
6106 case TARGET_NR_recvmsg:
6107 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6108 break;
6109 #endif
6110 #ifdef TARGET_NR_send
6111 case TARGET_NR_send:
6112 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
6113 break;
6114 #endif
6115 #ifdef TARGET_NR_sendmsg
6116 case TARGET_NR_sendmsg:
6117 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
6118 break;
6119 #endif
6120 #ifdef TARGET_NR_sendto
6121 case TARGET_NR_sendto:
6122 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
6123 break;
6124 #endif
6125 #ifdef TARGET_NR_shutdown
6126 case TARGET_NR_shutdown:
6127 ret = get_errno(shutdown(arg1, arg2));
6128 break;
6129 #endif
6130 #ifdef TARGET_NR_socket
6131 case TARGET_NR_socket:
6132 ret = do_socket(arg1, arg2, arg3);
6133 break;
6134 #endif
6135 #ifdef TARGET_NR_socketpair
6136 case TARGET_NR_socketpair:
6137 ret = do_socketpair(arg1, arg2, arg3, arg4);
6138 break;
6139 #endif
6140 #ifdef TARGET_NR_setsockopt
6141 case TARGET_NR_setsockopt:
6142 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
6143 break;
6144 #endif
6146 case TARGET_NR_syslog:
6147 if (!(p = lock_user_string(arg2)))
6148 goto efault;
6149 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
6150 unlock_user(p, arg2, 0);
6151 break;
6153 case TARGET_NR_setitimer:
6155 struct itimerval value, ovalue, *pvalue;
6157 if (arg2) {
6158 pvalue = &value;
6159 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
6160 || copy_from_user_timeval(&pvalue->it_value,
6161 arg2 + sizeof(struct target_timeval)))
6162 goto efault;
6163 } else {
6164 pvalue = NULL;
6166 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
6167 if (!is_error(ret) && arg3) {
6168 if (copy_to_user_timeval(arg3,
6169 &ovalue.it_interval)
6170 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
6171 &ovalue.it_value))
6172 goto efault;
6175 break;
6176 case TARGET_NR_getitimer:
6178 struct itimerval value;
6180 ret = get_errno(getitimer(arg1, &value));
6181 if (!is_error(ret) && arg2) {
6182 if (copy_to_user_timeval(arg2,
6183 &value.it_interval)
6184 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
6185 &value.it_value))
6186 goto efault;
6189 break;
6190 case TARGET_NR_stat:
6191 if (!(p = lock_user_string(arg1)))
6192 goto efault;
6193 ret = get_errno(stat(path(p), &st));
6194 unlock_user(p, arg1, 0);
6195 goto do_stat;
6196 case TARGET_NR_lstat:
6197 if (!(p = lock_user_string(arg1)))
6198 goto efault;
6199 ret = get_errno(lstat(path(p), &st));
6200 unlock_user(p, arg1, 0);
6201 goto do_stat;
6202 case TARGET_NR_fstat:
6204 ret = get_errno(fstat(arg1, &st));
6205 do_stat:
6206 if (!is_error(ret)) {
6207 struct target_stat *target_st;
6209 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
6210 goto efault;
6211 memset(target_st, 0, sizeof(*target_st));
6212 __put_user(st.st_dev, &target_st->st_dev);
6213 __put_user(st.st_ino, &target_st->st_ino);
6214 __put_user(st.st_mode, &target_st->st_mode);
6215 __put_user(st.st_uid, &target_st->st_uid);
6216 __put_user(st.st_gid, &target_st->st_gid);
6217 __put_user(st.st_nlink, &target_st->st_nlink);
6218 __put_user(st.st_rdev, &target_st->st_rdev);
6219 __put_user(st.st_size, &target_st->st_size);
6220 __put_user(st.st_blksize, &target_st->st_blksize);
6221 __put_user(st.st_blocks, &target_st->st_blocks);
6222 __put_user(st.st_atime, &target_st->target_st_atime);
6223 __put_user(st.st_mtime, &target_st->target_st_mtime);
6224 __put_user(st.st_ctime, &target_st->target_st_ctime);
6225 unlock_user_struct(target_st, arg2, 1);
6228 break;
6229 #ifdef TARGET_NR_olduname
6230 case TARGET_NR_olduname:
6231 goto unimplemented;
6232 #endif
6233 #ifdef TARGET_NR_iopl
6234 case TARGET_NR_iopl:
6235 goto unimplemented;
6236 #endif
6237 case TARGET_NR_vhangup:
6238 ret = get_errno(vhangup());
6239 break;
6240 #ifdef TARGET_NR_idle
6241 case TARGET_NR_idle:
6242 goto unimplemented;
6243 #endif
6244 #ifdef TARGET_NR_syscall
6245 case TARGET_NR_syscall:
6246 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
6247 arg6, arg7, arg8, 0);
6248 break;
6249 #endif
6250 case TARGET_NR_wait4:
6252 int status;
6253 abi_long status_ptr = arg2;
6254 struct rusage rusage, *rusage_ptr;
6255 abi_ulong target_rusage = arg4;
6256 if (target_rusage)
6257 rusage_ptr = &rusage;
6258 else
6259 rusage_ptr = NULL;
6260 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6261 if (!is_error(ret)) {
6262 if (status_ptr) {
6263 status = host_to_target_waitstatus(status);
6264 if (put_user_s32(status, status_ptr))
6265 goto efault;
6267 if (target_rusage)
6268 host_to_target_rusage(target_rusage, &rusage);
6271 break;
6272 #ifdef TARGET_NR_swapoff
6273 case TARGET_NR_swapoff:
6274 if (!(p = lock_user_string(arg1)))
6275 goto efault;
6276 ret = get_errno(swapoff(p));
6277 unlock_user(p, arg1, 0);
6278 break;
6279 #endif
6280 case TARGET_NR_sysinfo:
6282 struct target_sysinfo *target_value;
6283 struct sysinfo value;
6284 ret = get_errno(sysinfo(&value));
6285 if (!is_error(ret) && arg1)
6287 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6288 goto efault;
6289 __put_user(value.uptime, &target_value->uptime);
6290 __put_user(value.loads[0], &target_value->loads[0]);
6291 __put_user(value.loads[1], &target_value->loads[1]);
6292 __put_user(value.loads[2], &target_value->loads[2]);
6293 __put_user(value.totalram, &target_value->totalram);
6294 __put_user(value.freeram, &target_value->freeram);
6295 __put_user(value.sharedram, &target_value->sharedram);
6296 __put_user(value.bufferram, &target_value->bufferram);
6297 __put_user(value.totalswap, &target_value->totalswap);
6298 __put_user(value.freeswap, &target_value->freeswap);
6299 __put_user(value.procs, &target_value->procs);
6300 __put_user(value.totalhigh, &target_value->totalhigh);
6301 __put_user(value.freehigh, &target_value->freehigh);
6302 __put_user(value.mem_unit, &target_value->mem_unit);
6303 unlock_user_struct(target_value, arg1, 1);
6306 break;
6307 #ifdef TARGET_NR_ipc
6308 case TARGET_NR_ipc:
6309 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6310 break;
6311 #endif
6312 #ifdef TARGET_NR_semget
6313 case TARGET_NR_semget:
6314 ret = get_errno(semget(arg1, arg2, arg3));
6315 break;
6316 #endif
6317 #ifdef TARGET_NR_semop
6318 case TARGET_NR_semop:
6319 ret = get_errno(do_semop(arg1, arg2, arg3));
6320 break;
6321 #endif
6322 #ifdef TARGET_NR_semctl
6323 case TARGET_NR_semctl:
6324 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
6325 break;
6326 #endif
6327 #ifdef TARGET_NR_msgctl
6328 case TARGET_NR_msgctl:
6329 ret = do_msgctl(arg1, arg2, arg3);
6330 break;
6331 #endif
6332 #ifdef TARGET_NR_msgget
6333 case TARGET_NR_msgget:
6334 ret = get_errno(msgget(arg1, arg2));
6335 break;
6336 #endif
6337 #ifdef TARGET_NR_msgrcv
6338 case TARGET_NR_msgrcv:
6339 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
6340 break;
6341 #endif
6342 #ifdef TARGET_NR_msgsnd
6343 case TARGET_NR_msgsnd:
6344 ret = do_msgsnd(arg1, arg2, arg3, arg4);
6345 break;
6346 #endif
6347 #ifdef TARGET_NR_shmget
6348 case TARGET_NR_shmget:
6349 ret = get_errno(shmget(arg1, arg2, arg3));
6350 break;
6351 #endif
6352 #ifdef TARGET_NR_shmctl
6353 case TARGET_NR_shmctl:
6354 ret = do_shmctl(arg1, arg2, arg3);
6355 break;
6356 #endif
6357 #ifdef TARGET_NR_shmat
6358 case TARGET_NR_shmat:
6359 ret = do_shmat(arg1, arg2, arg3);
6360 break;
6361 #endif
6362 #ifdef TARGET_NR_shmdt
6363 case TARGET_NR_shmdt:
6364 ret = do_shmdt(arg1);
6365 break;
6366 #endif
6367 case TARGET_NR_fsync:
6368 ret = get_errno(fsync(arg1));
6369 break;
6370 case TARGET_NR_clone:
6371 #if defined(TARGET_SH4) || defined(TARGET_ALPHA)
6372 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
6373 #elif defined(TARGET_CRIS)
6374 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
6375 #elif defined(TARGET_S390X)
6376 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
6377 #else
6378 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
6379 #endif
6380 break;
6381 #ifdef __NR_exit_group
6382 /* new thread calls */
6383 case TARGET_NR_exit_group:
6384 #ifdef TARGET_GPROF
6385 _mcleanup();
6386 #endif
6387 gdb_exit(cpu_env, arg1);
6388 ret = get_errno(exit_group(arg1));
6389 break;
6390 #endif
6391 case TARGET_NR_setdomainname:
6392 if (!(p = lock_user_string(arg1)))
6393 goto efault;
6394 ret = get_errno(setdomainname(p, arg2));
6395 unlock_user(p, arg1, 0);
6396 break;
6397 case TARGET_NR_uname:
6398 /* no need to transcode because we use the linux syscall */
6400 struct new_utsname * buf;
6402 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
6403 goto efault;
6404 ret = get_errno(sys_uname(buf));
6405 if (!is_error(ret)) {
6406 /* Overrite the native machine name with whatever is being
6407 emulated. */
6408 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
6409 /* Allow the user to override the reported release. */
6410 if (qemu_uname_release && *qemu_uname_release)
6411 strcpy (buf->release, qemu_uname_release);
6413 unlock_user_struct(buf, arg1, 1);
6415 break;
6416 #ifdef TARGET_I386
6417 case TARGET_NR_modify_ldt:
6418 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
6419 break;
6420 #if !defined(TARGET_X86_64)
6421 case TARGET_NR_vm86old:
6422 goto unimplemented;
6423 case TARGET_NR_vm86:
6424 ret = do_vm86(cpu_env, arg1, arg2);
6425 break;
6426 #endif
6427 #endif
6428 case TARGET_NR_adjtimex:
6429 goto unimplemented;
6430 #ifdef TARGET_NR_create_module
6431 case TARGET_NR_create_module:
6432 #endif
6433 case TARGET_NR_init_module:
6434 case TARGET_NR_delete_module:
6435 #ifdef TARGET_NR_get_kernel_syms
6436 case TARGET_NR_get_kernel_syms:
6437 #endif
6438 goto unimplemented;
6439 case TARGET_NR_quotactl:
6440 goto unimplemented;
6441 case TARGET_NR_getpgid:
6442 ret = get_errno(getpgid(arg1));
6443 break;
6444 case TARGET_NR_fchdir:
6445 ret = get_errno(fchdir(arg1));
6446 break;
6447 #ifdef TARGET_NR_bdflush /* not on x86_64 */
6448 case TARGET_NR_bdflush:
6449 goto unimplemented;
6450 #endif
6451 #ifdef TARGET_NR_sysfs
6452 case TARGET_NR_sysfs:
6453 goto unimplemented;
6454 #endif
6455 case TARGET_NR_personality:
6456 ret = get_errno(personality(arg1));
6457 break;
6458 #ifdef TARGET_NR_afs_syscall
6459 case TARGET_NR_afs_syscall:
6460 goto unimplemented;
6461 #endif
6462 #ifdef TARGET_NR__llseek /* Not on alpha */
6463 case TARGET_NR__llseek:
6465 int64_t res;
6466 #if !defined(__NR_llseek)
6467 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
6468 if (res == -1) {
6469 ret = get_errno(res);
6470 } else {
6471 ret = 0;
6473 #else
6474 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
6475 #endif
6476 if ((ret == 0) && put_user_s64(res, arg4)) {
6477 goto efault;
6480 break;
6481 #endif
6482 case TARGET_NR_getdents:
6483 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
6485 struct target_dirent *target_dirp;
6486 struct linux_dirent *dirp;
6487 abi_long count = arg3;
6489 dirp = malloc(count);
6490 if (!dirp) {
6491 ret = -TARGET_ENOMEM;
6492 goto fail;
6495 ret = get_errno(sys_getdents(arg1, dirp, count));
6496 if (!is_error(ret)) {
6497 struct linux_dirent *de;
6498 struct target_dirent *tde;
6499 int len = ret;
6500 int reclen, treclen;
6501 int count1, tnamelen;
6503 count1 = 0;
6504 de = dirp;
6505 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6506 goto efault;
6507 tde = target_dirp;
6508 while (len > 0) {
6509 reclen = de->d_reclen;
6510 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
6511 tde->d_reclen = tswap16(treclen);
6512 tde->d_ino = tswapl(de->d_ino);
6513 tde->d_off = tswapl(de->d_off);
6514 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
6515 if (tnamelen > 256)
6516 tnamelen = 256;
6517 /* XXX: may not be correct */
6518 pstrcpy(tde->d_name, tnamelen, de->d_name);
6519 de = (struct linux_dirent *)((char *)de + reclen);
6520 len -= reclen;
6521 tde = (struct target_dirent *)((char *)tde + treclen);
6522 count1 += treclen;
6524 ret = count1;
6525 unlock_user(target_dirp, arg2, ret);
6527 free(dirp);
6529 #else
6531 struct linux_dirent *dirp;
6532 abi_long count = arg3;
6534 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6535 goto efault;
6536 ret = get_errno(sys_getdents(arg1, dirp, count));
6537 if (!is_error(ret)) {
6538 struct linux_dirent *de;
6539 int len = ret;
6540 int reclen;
6541 de = dirp;
6542 while (len > 0) {
6543 reclen = de->d_reclen;
6544 if (reclen > len)
6545 break;
6546 de->d_reclen = tswap16(reclen);
6547 tswapls(&de->d_ino);
6548 tswapls(&de->d_off);
6549 de = (struct linux_dirent *)((char *)de + reclen);
6550 len -= reclen;
6553 unlock_user(dirp, arg2, ret);
6555 #endif
6556 break;
6557 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
6558 case TARGET_NR_getdents64:
6560 struct linux_dirent64 *dirp;
6561 abi_long count = arg3;
6562 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6563 goto efault;
6564 ret = get_errno(sys_getdents64(arg1, dirp, count));
6565 if (!is_error(ret)) {
6566 struct linux_dirent64 *de;
6567 int len = ret;
6568 int reclen;
6569 de = dirp;
6570 while (len > 0) {
6571 reclen = de->d_reclen;
6572 if (reclen > len)
6573 break;
6574 de->d_reclen = tswap16(reclen);
6575 tswap64s((uint64_t *)&de->d_ino);
6576 tswap64s((uint64_t *)&de->d_off);
6577 de = (struct linux_dirent64 *)((char *)de + reclen);
6578 len -= reclen;
6581 unlock_user(dirp, arg2, ret);
6583 break;
6584 #endif /* TARGET_NR_getdents64 */
6585 #if defined(TARGET_NR__newselect) || defined(TARGET_S390X)
6586 #ifdef TARGET_S390X
6587 case TARGET_NR_select:
6588 #else
6589 case TARGET_NR__newselect:
6590 #endif
6591 ret = do_select(arg1, arg2, arg3, arg4, arg5);
6592 break;
6593 #endif
6594 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
6595 # ifdef TARGET_NR_poll
6596 case TARGET_NR_poll:
6597 # endif
6598 # ifdef TARGET_NR_ppoll
6599 case TARGET_NR_ppoll:
6600 # endif
6602 struct target_pollfd *target_pfd;
6603 unsigned int nfds = arg2;
6604 int timeout = arg3;
6605 struct pollfd *pfd;
6606 unsigned int i;
6608 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
6609 if (!target_pfd)
6610 goto efault;
6612 pfd = alloca(sizeof(struct pollfd) * nfds);
6613 for(i = 0; i < nfds; i++) {
6614 pfd[i].fd = tswap32(target_pfd[i].fd);
6615 pfd[i].events = tswap16(target_pfd[i].events);
6618 # ifdef TARGET_NR_ppoll
6619 if (num == TARGET_NR_ppoll) {
6620 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
6621 target_sigset_t *target_set;
6622 sigset_t _set, *set = &_set;
6624 if (arg3) {
6625 if (target_to_host_timespec(timeout_ts, arg3)) {
6626 unlock_user(target_pfd, arg1, 0);
6627 goto efault;
6629 } else {
6630 timeout_ts = NULL;
6633 if (arg4) {
6634 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
6635 if (!target_set) {
6636 unlock_user(target_pfd, arg1, 0);
6637 goto efault;
6639 target_to_host_sigset(set, target_set);
6640 } else {
6641 set = NULL;
6644 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
6646 if (!is_error(ret) && arg3) {
6647 host_to_target_timespec(arg3, timeout_ts);
6649 if (arg4) {
6650 unlock_user(target_set, arg4, 0);
6652 } else
6653 # endif
6654 ret = get_errno(poll(pfd, nfds, timeout));
6656 if (!is_error(ret)) {
6657 for(i = 0; i < nfds; i++) {
6658 target_pfd[i].revents = tswap16(pfd[i].revents);
6661 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
6663 break;
6664 #endif
6665 case TARGET_NR_flock:
6666 /* NOTE: the flock constant seems to be the same for every
6667 Linux platform */
6668 ret = get_errno(flock(arg1, arg2));
6669 break;
6670 case TARGET_NR_readv:
6672 int count = arg3;
6673 struct iovec *vec;
6675 vec = alloca(count * sizeof(struct iovec));
6676 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6677 goto efault;
6678 ret = get_errno(readv(arg1, vec, count));
6679 unlock_iovec(vec, arg2, count, 1);
6681 break;
6682 case TARGET_NR_writev:
6684 int count = arg3;
6685 struct iovec *vec;
6687 vec = alloca(count * sizeof(struct iovec));
6688 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6689 goto efault;
6690 ret = get_errno(writev(arg1, vec, count));
6691 unlock_iovec(vec, arg2, count, 0);
6693 break;
6694 case TARGET_NR_getsid:
6695 ret = get_errno(getsid(arg1));
6696 break;
6697 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
6698 case TARGET_NR_fdatasync:
6699 ret = get_errno(fdatasync(arg1));
6700 break;
6701 #endif
6702 case TARGET_NR__sysctl:
6703 /* We don't implement this, but ENOTDIR is always a safe
6704 return value. */
6705 ret = -TARGET_ENOTDIR;
6706 break;
6707 case TARGET_NR_sched_getaffinity:
6709 unsigned int mask_size;
6710 unsigned long *mask;
6713 * sched_getaffinity needs multiples of ulong, so need to take
6714 * care of mismatches between target ulong and host ulong sizes.
6716 if (arg2 & (sizeof(abi_ulong) - 1)) {
6717 ret = -TARGET_EINVAL;
6718 break;
6720 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
6722 mask = alloca(mask_size);
6723 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
6725 if (!is_error(ret)) {
6726 if (copy_to_user(arg3, mask, ret)) {
6727 goto efault;
6731 break;
6732 case TARGET_NR_sched_setaffinity:
6734 unsigned int mask_size;
6735 unsigned long *mask;
6738 * sched_setaffinity needs multiples of ulong, so need to take
6739 * care of mismatches between target ulong and host ulong sizes.
6741 if (arg2 & (sizeof(abi_ulong) - 1)) {
6742 ret = -TARGET_EINVAL;
6743 break;
6745 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
6747 mask = alloca(mask_size);
6748 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
6749 goto efault;
6751 memcpy(mask, p, arg2);
6752 unlock_user_struct(p, arg2, 0);
6754 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
6756 break;
6757 case TARGET_NR_sched_setparam:
6759 struct sched_param *target_schp;
6760 struct sched_param schp;
6762 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
6763 goto efault;
6764 schp.sched_priority = tswap32(target_schp->sched_priority);
6765 unlock_user_struct(target_schp, arg2, 0);
6766 ret = get_errno(sched_setparam(arg1, &schp));
6768 break;
6769 case TARGET_NR_sched_getparam:
6771 struct sched_param *target_schp;
6772 struct sched_param schp;
6773 ret = get_errno(sched_getparam(arg1, &schp));
6774 if (!is_error(ret)) {
6775 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6776 goto efault;
6777 target_schp->sched_priority = tswap32(schp.sched_priority);
6778 unlock_user_struct(target_schp, arg2, 1);
6781 break;
6782 case TARGET_NR_sched_setscheduler:
6784 struct sched_param *target_schp;
6785 struct sched_param schp;
6786 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6787 goto efault;
6788 schp.sched_priority = tswap32(target_schp->sched_priority);
6789 unlock_user_struct(target_schp, arg3, 0);
6790 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6792 break;
6793 case TARGET_NR_sched_getscheduler:
6794 ret = get_errno(sched_getscheduler(arg1));
6795 break;
6796 case TARGET_NR_sched_yield:
6797 ret = get_errno(sched_yield());
6798 break;
6799 case TARGET_NR_sched_get_priority_max:
6800 ret = get_errno(sched_get_priority_max(arg1));
6801 break;
6802 case TARGET_NR_sched_get_priority_min:
6803 ret = get_errno(sched_get_priority_min(arg1));
6804 break;
6805 case TARGET_NR_sched_rr_get_interval:
6807 struct timespec ts;
6808 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6809 if (!is_error(ret)) {
6810 host_to_target_timespec(arg2, &ts);
6813 break;
6814 case TARGET_NR_nanosleep:
6816 struct timespec req, rem;
6817 target_to_host_timespec(&req, arg1);
6818 ret = get_errno(nanosleep(&req, &rem));
6819 if (is_error(ret) && arg2) {
6820 host_to_target_timespec(arg2, &rem);
6823 break;
6824 #ifdef TARGET_NR_query_module
6825 case TARGET_NR_query_module:
6826 goto unimplemented;
6827 #endif
6828 #ifdef TARGET_NR_nfsservctl
6829 case TARGET_NR_nfsservctl:
6830 goto unimplemented;
6831 #endif
6832 case TARGET_NR_prctl:
6833 switch (arg1)
6835 case PR_GET_PDEATHSIG:
6837 int deathsig;
6838 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
6839 if (!is_error(ret) && arg2
6840 && put_user_ual(deathsig, arg2))
6841 goto efault;
6843 break;
6844 default:
6845 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6846 break;
6848 break;
6849 #ifdef TARGET_NR_arch_prctl
6850 case TARGET_NR_arch_prctl:
6851 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
6852 ret = do_arch_prctl(cpu_env, arg1, arg2);
6853 break;
6854 #else
6855 goto unimplemented;
6856 #endif
6857 #endif
6858 #ifdef TARGET_NR_pread
6859 case TARGET_NR_pread:
6860 #ifdef TARGET_ARM
6861 if (((CPUARMState *)cpu_env)->eabi)
6862 arg4 = arg5;
6863 #endif
6864 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6865 goto efault;
6866 ret = get_errno(pread(arg1, p, arg3, arg4));
6867 unlock_user(p, arg2, ret);
6868 break;
6869 case TARGET_NR_pwrite:
6870 #ifdef TARGET_ARM
6871 if (((CPUARMState *)cpu_env)->eabi)
6872 arg4 = arg5;
6873 #endif
6874 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6875 goto efault;
6876 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6877 unlock_user(p, arg2, 0);
6878 break;
6879 #endif
6880 #ifdef TARGET_NR_pread64
6881 case TARGET_NR_pread64:
6882 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6883 goto efault;
6884 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6885 unlock_user(p, arg2, ret);
6886 break;
6887 case TARGET_NR_pwrite64:
6888 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6889 goto efault;
6890 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6891 unlock_user(p, arg2, 0);
6892 break;
6893 #endif
6894 case TARGET_NR_getcwd:
6895 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6896 goto efault;
6897 ret = get_errno(sys_getcwd1(p, arg2));
6898 unlock_user(p, arg1, ret);
6899 break;
6900 case TARGET_NR_capget:
6901 goto unimplemented;
6902 case TARGET_NR_capset:
6903 goto unimplemented;
6904 case TARGET_NR_sigaltstack:
6905 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
6906 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6907 defined(TARGET_M68K) || defined(TARGET_S390X)
6908 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
6909 break;
6910 #else
6911 goto unimplemented;
6912 #endif
6913 case TARGET_NR_sendfile:
6914 goto unimplemented;
6915 #ifdef TARGET_NR_getpmsg
6916 case TARGET_NR_getpmsg:
6917 goto unimplemented;
6918 #endif
6919 #ifdef TARGET_NR_putpmsg
6920 case TARGET_NR_putpmsg:
6921 goto unimplemented;
6922 #endif
6923 #ifdef TARGET_NR_vfork
6924 case TARGET_NR_vfork:
6925 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6926 0, 0, 0, 0));
6927 break;
6928 #endif
6929 #ifdef TARGET_NR_ugetrlimit
6930 case TARGET_NR_ugetrlimit:
6932 struct rlimit rlim;
6933 int resource = target_to_host_resource(arg1);
6934 ret = get_errno(getrlimit(resource, &rlim));
6935 if (!is_error(ret)) {
6936 struct target_rlimit *target_rlim;
6937 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6938 goto efault;
6939 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6940 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
6941 unlock_user_struct(target_rlim, arg2, 1);
6943 break;
6945 #endif
6946 #ifdef TARGET_NR_truncate64
6947 case TARGET_NR_truncate64:
6948 if (!(p = lock_user_string(arg1)))
6949 goto efault;
6950 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6951 unlock_user(p, arg1, 0);
6952 break;
6953 #endif
6954 #ifdef TARGET_NR_ftruncate64
6955 case TARGET_NR_ftruncate64:
6956 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
6957 break;
6958 #endif
6959 #ifdef TARGET_NR_stat64
6960 case TARGET_NR_stat64:
6961 if (!(p = lock_user_string(arg1)))
6962 goto efault;
6963 ret = get_errno(stat(path(p), &st));
6964 unlock_user(p, arg1, 0);
6965 if (!is_error(ret))
6966 ret = host_to_target_stat64(cpu_env, arg2, &st);
6967 break;
6968 #endif
6969 #ifdef TARGET_NR_lstat64
6970 case TARGET_NR_lstat64:
6971 if (!(p = lock_user_string(arg1)))
6972 goto efault;
6973 ret = get_errno(lstat(path(p), &st));
6974 unlock_user(p, arg1, 0);
6975 if (!is_error(ret))
6976 ret = host_to_target_stat64(cpu_env, arg2, &st);
6977 break;
6978 #endif
6979 #ifdef TARGET_NR_fstat64
6980 case TARGET_NR_fstat64:
6981 ret = get_errno(fstat(arg1, &st));
6982 if (!is_error(ret))
6983 ret = host_to_target_stat64(cpu_env, arg2, &st);
6984 break;
6985 #endif
6986 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6987 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6988 #ifdef TARGET_NR_fstatat64
6989 case TARGET_NR_fstatat64:
6990 #endif
6991 #ifdef TARGET_NR_newfstatat
6992 case TARGET_NR_newfstatat:
6993 #endif
6994 if (!(p = lock_user_string(arg2)))
6995 goto efault;
6996 #ifdef __NR_fstatat64
6997 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
6998 #else
6999 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
7000 #endif
7001 if (!is_error(ret))
7002 ret = host_to_target_stat64(cpu_env, arg3, &st);
7003 break;
7004 #endif
7005 case TARGET_NR_lchown:
7006 if (!(p = lock_user_string(arg1)))
7007 goto efault;
7008 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
7009 unlock_user(p, arg1, 0);
7010 break;
7011 #ifdef TARGET_NR_getuid
7012 case TARGET_NR_getuid:
7013 ret = get_errno(high2lowuid(getuid()));
7014 break;
7015 #endif
7016 #ifdef TARGET_NR_getgid
7017 case TARGET_NR_getgid:
7018 ret = get_errno(high2lowgid(getgid()));
7019 break;
7020 #endif
7021 #ifdef TARGET_NR_geteuid
7022 case TARGET_NR_geteuid:
7023 ret = get_errno(high2lowuid(geteuid()));
7024 break;
7025 #endif
7026 #ifdef TARGET_NR_getegid
7027 case TARGET_NR_getegid:
7028 ret = get_errno(high2lowgid(getegid()));
7029 break;
7030 #endif
7031 case TARGET_NR_setreuid:
7032 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
7033 break;
7034 case TARGET_NR_setregid:
7035 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
7036 break;
7037 case TARGET_NR_getgroups:
7039 int gidsetsize = arg1;
7040 target_id *target_grouplist;
7041 gid_t *grouplist;
7042 int i;
7044 grouplist = alloca(gidsetsize * sizeof(gid_t));
7045 ret = get_errno(getgroups(gidsetsize, grouplist));
7046 if (gidsetsize == 0)
7047 break;
7048 if (!is_error(ret)) {
7049 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
7050 if (!target_grouplist)
7051 goto efault;
7052 for(i = 0;i < ret; i++)
7053 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
7054 unlock_user(target_grouplist, arg2, gidsetsize * 2);
7057 break;
7058 case TARGET_NR_setgroups:
7060 int gidsetsize = arg1;
7061 target_id *target_grouplist;
7062 gid_t *grouplist;
7063 int i;
7065 grouplist = alloca(gidsetsize * sizeof(gid_t));
7066 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
7067 if (!target_grouplist) {
7068 ret = -TARGET_EFAULT;
7069 goto fail;
7071 for(i = 0;i < gidsetsize; i++)
7072 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
7073 unlock_user(target_grouplist, arg2, 0);
7074 ret = get_errno(setgroups(gidsetsize, grouplist));
7076 break;
7077 case TARGET_NR_fchown:
7078 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
7079 break;
7080 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
7081 case TARGET_NR_fchownat:
7082 if (!(p = lock_user_string(arg2)))
7083 goto efault;
7084 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
7085 unlock_user(p, arg2, 0);
7086 break;
7087 #endif
7088 #ifdef TARGET_NR_setresuid
7089 case TARGET_NR_setresuid:
7090 ret = get_errno(setresuid(low2highuid(arg1),
7091 low2highuid(arg2),
7092 low2highuid(arg3)));
7093 break;
7094 #endif
7095 #ifdef TARGET_NR_getresuid
7096 case TARGET_NR_getresuid:
7098 uid_t ruid, euid, suid;
7099 ret = get_errno(getresuid(&ruid, &euid, &suid));
7100 if (!is_error(ret)) {
7101 if (put_user_u16(high2lowuid(ruid), arg1)
7102 || put_user_u16(high2lowuid(euid), arg2)
7103 || put_user_u16(high2lowuid(suid), arg3))
7104 goto efault;
7107 break;
7108 #endif
7109 #ifdef TARGET_NR_getresgid
7110 case TARGET_NR_setresgid:
7111 ret = get_errno(setresgid(low2highgid(arg1),
7112 low2highgid(arg2),
7113 low2highgid(arg3)));
7114 break;
7115 #endif
7116 #ifdef TARGET_NR_getresgid
7117 case TARGET_NR_getresgid:
7119 gid_t rgid, egid, sgid;
7120 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7121 if (!is_error(ret)) {
7122 if (put_user_u16(high2lowgid(rgid), arg1)
7123 || put_user_u16(high2lowgid(egid), arg2)
7124 || put_user_u16(high2lowgid(sgid), arg3))
7125 goto efault;
7128 break;
7129 #endif
7130 case TARGET_NR_chown:
7131 if (!(p = lock_user_string(arg1)))
7132 goto efault;
7133 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
7134 unlock_user(p, arg1, 0);
7135 break;
7136 case TARGET_NR_setuid:
7137 ret = get_errno(setuid(low2highuid(arg1)));
7138 break;
7139 case TARGET_NR_setgid:
7140 ret = get_errno(setgid(low2highgid(arg1)));
7141 break;
7142 case TARGET_NR_setfsuid:
7143 ret = get_errno(setfsuid(arg1));
7144 break;
7145 case TARGET_NR_setfsgid:
7146 ret = get_errno(setfsgid(arg1));
7147 break;
7149 #ifdef TARGET_NR_lchown32
7150 case TARGET_NR_lchown32:
7151 if (!(p = lock_user_string(arg1)))
7152 goto efault;
7153 ret = get_errno(lchown(p, arg2, arg3));
7154 unlock_user(p, arg1, 0);
7155 break;
7156 #endif
7157 #ifdef TARGET_NR_getuid32
7158 case TARGET_NR_getuid32:
7159 ret = get_errno(getuid());
7160 break;
7161 #endif
7163 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7164 /* Alpha specific */
7165 case TARGET_NR_getxuid:
7167 uid_t euid;
7168 euid=geteuid();
7169 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
7171 ret = get_errno(getuid());
7172 break;
7173 #endif
7174 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
7175 /* Alpha specific */
7176 case TARGET_NR_getxgid:
7178 uid_t egid;
7179 egid=getegid();
7180 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
7182 ret = get_errno(getgid());
7183 break;
7184 #endif
7185 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
7186 /* Alpha specific */
7187 case TARGET_NR_osf_getsysinfo:
7188 ret = -TARGET_EOPNOTSUPP;
7189 switch (arg1) {
7190 case TARGET_GSI_IEEE_FP_CONTROL:
7192 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
7194 /* Copied from linux ieee_fpcr_to_swcr. */
7195 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
7196 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
7197 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
7198 | SWCR_TRAP_ENABLE_DZE
7199 | SWCR_TRAP_ENABLE_OVF);
7200 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
7201 | SWCR_TRAP_ENABLE_INE);
7202 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
7203 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
7205 if (put_user_u64 (swcr, arg2))
7206 goto efault;
7207 ret = 0;
7209 break;
7211 /* case GSI_IEEE_STATE_AT_SIGNAL:
7212 -- Not implemented in linux kernel.
7213 case GSI_UACPROC:
7214 -- Retrieves current unaligned access state; not much used.
7215 case GSI_PROC_TYPE:
7216 -- Retrieves implver information; surely not used.
7217 case GSI_GET_HWRPB:
7218 -- Grabs a copy of the HWRPB; surely not used.
7221 break;
7222 #endif
7223 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
7224 /* Alpha specific */
7225 case TARGET_NR_osf_setsysinfo:
7226 ret = -TARGET_EOPNOTSUPP;
7227 switch (arg1) {
7228 case TARGET_SSI_IEEE_FP_CONTROL:
7229 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
7231 uint64_t swcr, fpcr, orig_fpcr;
7233 if (get_user_u64 (swcr, arg2))
7234 goto efault;
7235 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
7236 fpcr = orig_fpcr & FPCR_DYN_MASK;
7238 /* Copied from linux ieee_swcr_to_fpcr. */
7239 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
7240 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
7241 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
7242 | SWCR_TRAP_ENABLE_DZE
7243 | SWCR_TRAP_ENABLE_OVF)) << 48;
7244 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
7245 | SWCR_TRAP_ENABLE_INE)) << 57;
7246 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
7247 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
7249 cpu_alpha_store_fpcr (cpu_env, fpcr);
7250 ret = 0;
7252 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
7253 /* Old exceptions are not signaled. */
7254 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
7256 /* If any exceptions set by this call, and are unmasked,
7257 send a signal. */
7258 /* ??? FIXME */
7261 break;
7263 /* case SSI_NVPAIRS:
7264 -- Used with SSIN_UACPROC to enable unaligned accesses.
7265 case SSI_IEEE_STATE_AT_SIGNAL:
7266 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
7267 -- Not implemented in linux kernel
7270 break;
7271 #endif
7272 #ifdef TARGET_NR_osf_sigprocmask
7273 /* Alpha specific. */
7274 case TARGET_NR_osf_sigprocmask:
7276 abi_ulong mask;
7277 int how;
7278 sigset_t set, oldset;
7280 switch(arg1) {
7281 case TARGET_SIG_BLOCK:
7282 how = SIG_BLOCK;
7283 break;
7284 case TARGET_SIG_UNBLOCK:
7285 how = SIG_UNBLOCK;
7286 break;
7287 case TARGET_SIG_SETMASK:
7288 how = SIG_SETMASK;
7289 break;
7290 default:
7291 ret = -TARGET_EINVAL;
7292 goto fail;
7294 mask = arg2;
7295 target_to_host_old_sigset(&set, &mask);
7296 sigprocmask(how, &set, &oldset);
7297 host_to_target_old_sigset(&mask, &oldset);
7298 ret = mask;
7300 break;
7301 #endif
7303 #ifdef TARGET_NR_getgid32
7304 case TARGET_NR_getgid32:
7305 ret = get_errno(getgid());
7306 break;
7307 #endif
7308 #ifdef TARGET_NR_geteuid32
7309 case TARGET_NR_geteuid32:
7310 ret = get_errno(geteuid());
7311 break;
7312 #endif
7313 #ifdef TARGET_NR_getegid32
7314 case TARGET_NR_getegid32:
7315 ret = get_errno(getegid());
7316 break;
7317 #endif
7318 #ifdef TARGET_NR_setreuid32
7319 case TARGET_NR_setreuid32:
7320 ret = get_errno(setreuid(arg1, arg2));
7321 break;
7322 #endif
7323 #ifdef TARGET_NR_setregid32
7324 case TARGET_NR_setregid32:
7325 ret = get_errno(setregid(arg1, arg2));
7326 break;
7327 #endif
7328 #ifdef TARGET_NR_getgroups32
7329 case TARGET_NR_getgroups32:
7331 int gidsetsize = arg1;
7332 uint32_t *target_grouplist;
7333 gid_t *grouplist;
7334 int i;
7336 grouplist = alloca(gidsetsize * sizeof(gid_t));
7337 ret = get_errno(getgroups(gidsetsize, grouplist));
7338 if (gidsetsize == 0)
7339 break;
7340 if (!is_error(ret)) {
7341 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
7342 if (!target_grouplist) {
7343 ret = -TARGET_EFAULT;
7344 goto fail;
7346 for(i = 0;i < ret; i++)
7347 target_grouplist[i] = tswap32(grouplist[i]);
7348 unlock_user(target_grouplist, arg2, gidsetsize * 4);
7351 break;
7352 #endif
7353 #ifdef TARGET_NR_setgroups32
7354 case TARGET_NR_setgroups32:
7356 int gidsetsize = arg1;
7357 uint32_t *target_grouplist;
7358 gid_t *grouplist;
7359 int i;
7361 grouplist = alloca(gidsetsize * sizeof(gid_t));
7362 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
7363 if (!target_grouplist) {
7364 ret = -TARGET_EFAULT;
7365 goto fail;
7367 for(i = 0;i < gidsetsize; i++)
7368 grouplist[i] = tswap32(target_grouplist[i]);
7369 unlock_user(target_grouplist, arg2, 0);
7370 ret = get_errno(setgroups(gidsetsize, grouplist));
7372 break;
7373 #endif
7374 #ifdef TARGET_NR_fchown32
7375 case TARGET_NR_fchown32:
7376 ret = get_errno(fchown(arg1, arg2, arg3));
7377 break;
7378 #endif
7379 #ifdef TARGET_NR_setresuid32
7380 case TARGET_NR_setresuid32:
7381 ret = get_errno(setresuid(arg1, arg2, arg3));
7382 break;
7383 #endif
7384 #ifdef TARGET_NR_getresuid32
7385 case TARGET_NR_getresuid32:
7387 uid_t ruid, euid, suid;
7388 ret = get_errno(getresuid(&ruid, &euid, &suid));
7389 if (!is_error(ret)) {
7390 if (put_user_u32(ruid, arg1)
7391 || put_user_u32(euid, arg2)
7392 || put_user_u32(suid, arg3))
7393 goto efault;
7396 break;
7397 #endif
7398 #ifdef TARGET_NR_setresgid32
7399 case TARGET_NR_setresgid32:
7400 ret = get_errno(setresgid(arg1, arg2, arg3));
7401 break;
7402 #endif
7403 #ifdef TARGET_NR_getresgid32
7404 case TARGET_NR_getresgid32:
7406 gid_t rgid, egid, sgid;
7407 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7408 if (!is_error(ret)) {
7409 if (put_user_u32(rgid, arg1)
7410 || put_user_u32(egid, arg2)
7411 || put_user_u32(sgid, arg3))
7412 goto efault;
7415 break;
7416 #endif
7417 #ifdef TARGET_NR_chown32
7418 case TARGET_NR_chown32:
7419 if (!(p = lock_user_string(arg1)))
7420 goto efault;
7421 ret = get_errno(chown(p, arg2, arg3));
7422 unlock_user(p, arg1, 0);
7423 break;
7424 #endif
7425 #ifdef TARGET_NR_setuid32
7426 case TARGET_NR_setuid32:
7427 ret = get_errno(setuid(arg1));
7428 break;
7429 #endif
7430 #ifdef TARGET_NR_setgid32
7431 case TARGET_NR_setgid32:
7432 ret = get_errno(setgid(arg1));
7433 break;
7434 #endif
7435 #ifdef TARGET_NR_setfsuid32
7436 case TARGET_NR_setfsuid32:
7437 ret = get_errno(setfsuid(arg1));
7438 break;
7439 #endif
7440 #ifdef TARGET_NR_setfsgid32
7441 case TARGET_NR_setfsgid32:
7442 ret = get_errno(setfsgid(arg1));
7443 break;
7444 #endif
7446 case TARGET_NR_pivot_root:
7447 goto unimplemented;
7448 #ifdef TARGET_NR_mincore
7449 case TARGET_NR_mincore:
7451 void *a;
7452 ret = -TARGET_EFAULT;
7453 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
7454 goto efault;
7455 if (!(p = lock_user_string(arg3)))
7456 goto mincore_fail;
7457 ret = get_errno(mincore(a, arg2, p));
7458 unlock_user(p, arg3, ret);
7459 mincore_fail:
7460 unlock_user(a, arg1, 0);
7462 break;
7463 #endif
7464 #ifdef TARGET_NR_arm_fadvise64_64
7465 case TARGET_NR_arm_fadvise64_64:
7468 * arm_fadvise64_64 looks like fadvise64_64 but
7469 * with different argument order
7471 abi_long temp;
7472 temp = arg3;
7473 arg3 = arg4;
7474 arg4 = temp;
7476 #endif
7477 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
7478 #ifdef TARGET_NR_fadvise64_64
7479 case TARGET_NR_fadvise64_64:
7480 #endif
7481 #ifdef TARGET_NR_fadvise64
7482 case TARGET_NR_fadvise64:
7483 #endif
7484 #ifdef TARGET_S390X
7485 switch (arg4) {
7486 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
7487 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
7488 case 6: arg4 = POSIX_FADV_DONTNEED; break;
7489 case 7: arg4 = POSIX_FADV_NOREUSE; break;
7490 default: break;
7492 #endif
7493 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
7494 break;
7495 #endif
7496 #ifdef TARGET_NR_madvise
7497 case TARGET_NR_madvise:
7498 /* A straight passthrough may not be safe because qemu sometimes
7499 turns private flie-backed mappings into anonymous mappings.
7500 This will break MADV_DONTNEED.
7501 This is a hint, so ignoring and returning success is ok. */
7502 ret = get_errno(0);
7503 break;
7504 #endif
7505 #if TARGET_ABI_BITS == 32
7506 case TARGET_NR_fcntl64:
7508 int cmd;
7509 struct flock64 fl;
7510 struct target_flock64 *target_fl;
7511 #ifdef TARGET_ARM
7512 struct target_eabi_flock64 *target_efl;
7513 #endif
7515 cmd = target_to_host_fcntl_cmd(arg2);
7516 if (cmd == -TARGET_EINVAL)
7517 return cmd;
7519 switch(arg2) {
7520 case TARGET_F_GETLK64:
7521 #ifdef TARGET_ARM
7522 if (((CPUARMState *)cpu_env)->eabi) {
7523 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7524 goto efault;
7525 fl.l_type = tswap16(target_efl->l_type);
7526 fl.l_whence = tswap16(target_efl->l_whence);
7527 fl.l_start = tswap64(target_efl->l_start);
7528 fl.l_len = tswap64(target_efl->l_len);
7529 fl.l_pid = tswap32(target_efl->l_pid);
7530 unlock_user_struct(target_efl, arg3, 0);
7531 } else
7532 #endif
7534 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7535 goto efault;
7536 fl.l_type = tswap16(target_fl->l_type);
7537 fl.l_whence = tswap16(target_fl->l_whence);
7538 fl.l_start = tswap64(target_fl->l_start);
7539 fl.l_len = tswap64(target_fl->l_len);
7540 fl.l_pid = tswap32(target_fl->l_pid);
7541 unlock_user_struct(target_fl, arg3, 0);
7543 ret = get_errno(fcntl(arg1, cmd, &fl));
7544 if (ret == 0) {
7545 #ifdef TARGET_ARM
7546 if (((CPUARMState *)cpu_env)->eabi) {
7547 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
7548 goto efault;
7549 target_efl->l_type = tswap16(fl.l_type);
7550 target_efl->l_whence = tswap16(fl.l_whence);
7551 target_efl->l_start = tswap64(fl.l_start);
7552 target_efl->l_len = tswap64(fl.l_len);
7553 target_efl->l_pid = tswap32(fl.l_pid);
7554 unlock_user_struct(target_efl, arg3, 1);
7555 } else
7556 #endif
7558 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
7559 goto efault;
7560 target_fl->l_type = tswap16(fl.l_type);
7561 target_fl->l_whence = tswap16(fl.l_whence);
7562 target_fl->l_start = tswap64(fl.l_start);
7563 target_fl->l_len = tswap64(fl.l_len);
7564 target_fl->l_pid = tswap32(fl.l_pid);
7565 unlock_user_struct(target_fl, arg3, 1);
7568 break;
7570 case TARGET_F_SETLK64:
7571 case TARGET_F_SETLKW64:
7572 #ifdef TARGET_ARM
7573 if (((CPUARMState *)cpu_env)->eabi) {
7574 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7575 goto efault;
7576 fl.l_type = tswap16(target_efl->l_type);
7577 fl.l_whence = tswap16(target_efl->l_whence);
7578 fl.l_start = tswap64(target_efl->l_start);
7579 fl.l_len = tswap64(target_efl->l_len);
7580 fl.l_pid = tswap32(target_efl->l_pid);
7581 unlock_user_struct(target_efl, arg3, 0);
7582 } else
7583 #endif
7585 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7586 goto efault;
7587 fl.l_type = tswap16(target_fl->l_type);
7588 fl.l_whence = tswap16(target_fl->l_whence);
7589 fl.l_start = tswap64(target_fl->l_start);
7590 fl.l_len = tswap64(target_fl->l_len);
7591 fl.l_pid = tswap32(target_fl->l_pid);
7592 unlock_user_struct(target_fl, arg3, 0);
7594 ret = get_errno(fcntl(arg1, cmd, &fl));
7595 break;
7596 default:
7597 ret = do_fcntl(arg1, arg2, arg3);
7598 break;
7600 break;
7602 #endif
7603 #ifdef TARGET_NR_cacheflush
7604 case TARGET_NR_cacheflush:
7605 /* self-modifying code is handled automatically, so nothing needed */
7606 ret = 0;
7607 break;
7608 #endif
7609 #ifdef TARGET_NR_security
7610 case TARGET_NR_security:
7611 goto unimplemented;
7612 #endif
7613 #ifdef TARGET_NR_getpagesize
7614 case TARGET_NR_getpagesize:
7615 ret = TARGET_PAGE_SIZE;
7616 break;
7617 #endif
7618 case TARGET_NR_gettid:
7619 ret = get_errno(gettid());
7620 break;
7621 #ifdef TARGET_NR_readahead
7622 case TARGET_NR_readahead:
7623 #if TARGET_ABI_BITS == 32
7624 #ifdef TARGET_ARM
7625 if (((CPUARMState *)cpu_env)->eabi)
7627 arg2 = arg3;
7628 arg3 = arg4;
7629 arg4 = arg5;
7631 #endif
7632 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
7633 #else
7634 ret = get_errno(readahead(arg1, arg2, arg3));
7635 #endif
7636 break;
7637 #endif
7638 #ifdef TARGET_NR_setxattr
7639 case TARGET_NR_setxattr:
7640 case TARGET_NR_lsetxattr:
7641 case TARGET_NR_fsetxattr:
7642 case TARGET_NR_getxattr:
7643 case TARGET_NR_lgetxattr:
7644 case TARGET_NR_fgetxattr:
7645 case TARGET_NR_listxattr:
7646 case TARGET_NR_llistxattr:
7647 case TARGET_NR_flistxattr:
7648 case TARGET_NR_removexattr:
7649 case TARGET_NR_lremovexattr:
7650 case TARGET_NR_fremovexattr:
7651 ret = -TARGET_EOPNOTSUPP;
7652 break;
7653 #endif
7654 #ifdef TARGET_NR_set_thread_area
7655 case TARGET_NR_set_thread_area:
7656 #if defined(TARGET_MIPS)
7657 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
7658 ret = 0;
7659 break;
7660 #elif defined(TARGET_CRIS)
7661 if (arg1 & 0xff)
7662 ret = -TARGET_EINVAL;
7663 else {
7664 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
7665 ret = 0;
7667 break;
7668 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
7669 ret = do_set_thread_area(cpu_env, arg1);
7670 break;
7671 #else
7672 goto unimplemented_nowarn;
7673 #endif
7674 #endif
7675 #ifdef TARGET_NR_get_thread_area
7676 case TARGET_NR_get_thread_area:
7677 #if defined(TARGET_I386) && defined(TARGET_ABI32)
7678 ret = do_get_thread_area(cpu_env, arg1);
7679 #else
7680 goto unimplemented_nowarn;
7681 #endif
7682 #endif
7683 #ifdef TARGET_NR_getdomainname
7684 case TARGET_NR_getdomainname:
7685 goto unimplemented_nowarn;
7686 #endif
7688 #ifdef TARGET_NR_clock_gettime
7689 case TARGET_NR_clock_gettime:
7691 struct timespec ts;
7692 ret = get_errno(clock_gettime(arg1, &ts));
7693 if (!is_error(ret)) {
7694 host_to_target_timespec(arg2, &ts);
7696 break;
7698 #endif
7699 #ifdef TARGET_NR_clock_getres
7700 case TARGET_NR_clock_getres:
7702 struct timespec ts;
7703 ret = get_errno(clock_getres(arg1, &ts));
7704 if (!is_error(ret)) {
7705 host_to_target_timespec(arg2, &ts);
7707 break;
7709 #endif
7710 #ifdef TARGET_NR_clock_nanosleep
7711 case TARGET_NR_clock_nanosleep:
7713 struct timespec ts;
7714 target_to_host_timespec(&ts, arg3);
7715 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
7716 if (arg4)
7717 host_to_target_timespec(arg4, &ts);
7718 break;
7720 #endif
7722 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
7723 case TARGET_NR_set_tid_address:
7724 ret = get_errno(set_tid_address((int *)g2h(arg1)));
7725 break;
7726 #endif
7728 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
7729 case TARGET_NR_tkill:
7730 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
7731 break;
7732 #endif
7734 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
7735 case TARGET_NR_tgkill:
7736 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
7737 target_to_host_signal(arg3)));
7738 break;
7739 #endif
7741 #ifdef TARGET_NR_set_robust_list
7742 case TARGET_NR_set_robust_list:
7743 goto unimplemented_nowarn;
7744 #endif
7746 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
7747 case TARGET_NR_utimensat:
7749 struct timespec *tsp, ts[2];
7750 if (!arg3) {
7751 tsp = NULL;
7752 } else {
7753 target_to_host_timespec(ts, arg3);
7754 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
7755 tsp = ts;
7757 if (!arg2)
7758 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
7759 else {
7760 if (!(p = lock_user_string(arg2))) {
7761 ret = -TARGET_EFAULT;
7762 goto fail;
7764 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
7765 unlock_user(p, arg2, 0);
7768 break;
7769 #endif
7770 #if defined(CONFIG_USE_NPTL)
7771 case TARGET_NR_futex:
7772 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
7773 break;
7774 #endif
7775 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
7776 case TARGET_NR_inotify_init:
7777 ret = get_errno(sys_inotify_init());
7778 break;
7779 #endif
7780 #ifdef CONFIG_INOTIFY1
7781 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
7782 case TARGET_NR_inotify_init1:
7783 ret = get_errno(sys_inotify_init1(arg1));
7784 break;
7785 #endif
7786 #endif
7787 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
7788 case TARGET_NR_inotify_add_watch:
7789 p = lock_user_string(arg2);
7790 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
7791 unlock_user(p, arg2, 0);
7792 break;
7793 #endif
7794 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
7795 case TARGET_NR_inotify_rm_watch:
7796 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
7797 break;
7798 #endif
7800 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
7801 case TARGET_NR_mq_open:
7803 struct mq_attr posix_mq_attr;
7805 p = lock_user_string(arg1 - 1);
7806 if (arg4 != 0)
7807 copy_from_user_mq_attr (&posix_mq_attr, arg4);
7808 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
7809 unlock_user (p, arg1, 0);
7811 break;
7813 case TARGET_NR_mq_unlink:
7814 p = lock_user_string(arg1 - 1);
7815 ret = get_errno(mq_unlink(p));
7816 unlock_user (p, arg1, 0);
7817 break;
7819 case TARGET_NR_mq_timedsend:
7821 struct timespec ts;
7823 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7824 if (arg5 != 0) {
7825 target_to_host_timespec(&ts, arg5);
7826 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
7827 host_to_target_timespec(arg5, &ts);
7829 else
7830 ret = get_errno(mq_send(arg1, p, arg3, arg4));
7831 unlock_user (p, arg2, arg3);
7833 break;
7835 case TARGET_NR_mq_timedreceive:
7837 struct timespec ts;
7838 unsigned int prio;
7840 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7841 if (arg5 != 0) {
7842 target_to_host_timespec(&ts, arg5);
7843 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
7844 host_to_target_timespec(arg5, &ts);
7846 else
7847 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
7848 unlock_user (p, arg2, arg3);
7849 if (arg4 != 0)
7850 put_user_u32(prio, arg4);
7852 break;
7854 /* Not implemented for now... */
7855 /* case TARGET_NR_mq_notify: */
7856 /* break; */
7858 case TARGET_NR_mq_getsetattr:
7860 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
7861 ret = 0;
7862 if (arg3 != 0) {
7863 ret = mq_getattr(arg1, &posix_mq_attr_out);
7864 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
7866 if (arg2 != 0) {
7867 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
7868 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
7872 break;
7873 #endif
7875 #ifdef CONFIG_SPLICE
7876 #ifdef TARGET_NR_tee
7877 case TARGET_NR_tee:
7879 ret = get_errno(tee(arg1,arg2,arg3,arg4));
7881 break;
7882 #endif
7883 #ifdef TARGET_NR_splice
7884 case TARGET_NR_splice:
7886 loff_t loff_in, loff_out;
7887 loff_t *ploff_in = NULL, *ploff_out = NULL;
7888 if(arg2) {
7889 get_user_u64(loff_in, arg2);
7890 ploff_in = &loff_in;
7892 if(arg4) {
7893 get_user_u64(loff_out, arg2);
7894 ploff_out = &loff_out;
7896 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
7898 break;
7899 #endif
7900 #ifdef TARGET_NR_vmsplice
7901 case TARGET_NR_vmsplice:
7903 int count = arg3;
7904 struct iovec *vec;
7906 vec = alloca(count * sizeof(struct iovec));
7907 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7908 goto efault;
7909 ret = get_errno(vmsplice(arg1, vec, count, arg4));
7910 unlock_iovec(vec, arg2, count, 0);
7912 break;
7913 #endif
7914 #endif /* CONFIG_SPLICE */
7915 #ifdef CONFIG_EVENTFD
7916 #if defined(TARGET_NR_eventfd)
7917 case TARGET_NR_eventfd:
7918 ret = get_errno(eventfd(arg1, 0));
7919 break;
7920 #endif
7921 #if defined(TARGET_NR_eventfd2)
7922 case TARGET_NR_eventfd2:
7923 ret = get_errno(eventfd(arg1, arg2));
7924 break;
7925 #endif
7926 #endif /* CONFIG_EVENTFD */
7927 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7928 case TARGET_NR_fallocate:
7929 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7930 break;
7931 #endif
7932 #if defined(CONFIG_SYNC_FILE_RANGE)
7933 #if defined(TARGET_NR_sync_file_range)
7934 case TARGET_NR_sync_file_range:
7935 #if TARGET_ABI_BITS == 32
7936 #if defined(TARGET_MIPS)
7937 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
7938 target_offset64(arg5, arg6), arg7));
7939 #else
7940 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
7941 target_offset64(arg4, arg5), arg6));
7942 #endif /* !TARGET_MIPS */
7943 #else
7944 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
7945 #endif
7946 break;
7947 #endif
7948 #if defined(TARGET_NR_sync_file_range2)
7949 case TARGET_NR_sync_file_range2:
7950 /* This is like sync_file_range but the arguments are reordered */
7951 #if TARGET_ABI_BITS == 32
7952 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
7953 target_offset64(arg5, arg6), arg2));
7954 #else
7955 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
7956 #endif
7957 break;
7958 #endif
7959 #endif
7960 #if defined(CONFIG_EPOLL)
7961 #if defined(TARGET_NR_epoll_create)
7962 case TARGET_NR_epoll_create:
7963 ret = get_errno(epoll_create(arg1));
7964 break;
7965 #endif
7966 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
7967 case TARGET_NR_epoll_create1:
7968 ret = get_errno(epoll_create1(arg1));
7969 break;
7970 #endif
7971 #if defined(TARGET_NR_epoll_ctl)
7972 case TARGET_NR_epoll_ctl:
7974 struct epoll_event ep;
7975 struct epoll_event *epp = 0;
7976 if (arg4) {
7977 struct target_epoll_event *target_ep;
7978 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
7979 goto efault;
7981 ep.events = tswap32(target_ep->events);
7982 /* The epoll_data_t union is just opaque data to the kernel,
7983 * so we transfer all 64 bits across and need not worry what
7984 * actual data type it is.
7986 ep.data.u64 = tswap64(target_ep->data.u64);
7987 unlock_user_struct(target_ep, arg4, 0);
7988 epp = &ep;
7990 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
7991 break;
7993 #endif
7995 #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
7996 #define IMPLEMENT_EPOLL_PWAIT
7997 #endif
7998 #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
7999 #if defined(TARGET_NR_epoll_wait)
8000 case TARGET_NR_epoll_wait:
8001 #endif
8002 #if defined(IMPLEMENT_EPOLL_PWAIT)
8003 case TARGET_NR_epoll_pwait:
8004 #endif
8006 struct target_epoll_event *target_ep;
8007 struct epoll_event *ep;
8008 int epfd = arg1;
8009 int maxevents = arg3;
8010 int timeout = arg4;
8012 target_ep = lock_user(VERIFY_WRITE, arg2,
8013 maxevents * sizeof(struct target_epoll_event), 1);
8014 if (!target_ep) {
8015 goto efault;
8018 ep = alloca(maxevents * sizeof(struct epoll_event));
8020 switch (num) {
8021 #if defined(IMPLEMENT_EPOLL_PWAIT)
8022 case TARGET_NR_epoll_pwait:
8024 target_sigset_t *target_set;
8025 sigset_t _set, *set = &_set;
8027 if (arg5) {
8028 target_set = lock_user(VERIFY_READ, arg5,
8029 sizeof(target_sigset_t), 1);
8030 if (!target_set) {
8031 unlock_user(target_ep, arg2, 0);
8032 goto efault;
8034 target_to_host_sigset(set, target_set);
8035 unlock_user(target_set, arg5, 0);
8036 } else {
8037 set = NULL;
8040 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
8041 break;
8043 #endif
8044 #if defined(TARGET_NR_epoll_wait)
8045 case TARGET_NR_epoll_wait:
8046 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
8047 break;
8048 #endif
8049 default:
8050 ret = -TARGET_ENOSYS;
8052 if (!is_error(ret)) {
8053 int i;
8054 for (i = 0; i < ret; i++) {
8055 target_ep[i].events = tswap32(ep[i].events);
8056 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
8059 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
8060 break;
8062 #endif
8063 #endif
8064 #ifdef TARGET_NR_prlimit64
8065 case TARGET_NR_prlimit64:
8067 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
8068 struct target_rlimit64 *target_rnew, *target_rold;
8069 struct host_rlimit64 rnew, rold, *rnewp = 0;
8070 if (arg3) {
8071 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
8072 goto efault;
8074 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
8075 rnew.rlim_max = tswap64(target_rnew->rlim_max);
8076 unlock_user_struct(target_rnew, arg3, 0);
8077 rnewp = &rnew;
8080 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
8081 if (!is_error(ret) && arg4) {
8082 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
8083 goto efault;
8085 target_rold->rlim_cur = tswap64(rold.rlim_cur);
8086 target_rold->rlim_max = tswap64(rold.rlim_max);
8087 unlock_user_struct(target_rold, arg4, 1);
8089 break;
8091 #endif
8092 default:
8093 unimplemented:
8094 gemu_log("qemu: Unsupported syscall: %d\n", num);
8095 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
8096 unimplemented_nowarn:
8097 #endif
8098 ret = -TARGET_ENOSYS;
8099 break;
8101 fail:
8102 #ifdef DEBUG
8103 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
8104 #endif
8105 if(do_strace)
8106 print_syscall_ret(num, ret);
8107 return ret;
8108 efault:
8109 ret = -TARGET_EFAULT;
8110 goto fail;