blob: 88513f6c5d45be3dd125e1d0496deb3af7251962 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
2 * Linux syscalls
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <stdlib.h>
21#include <stdio.h>
22#include <stdarg.h>
bellard04369ff2003-03-20 22:33:23 +000023#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000024#include <elf.h>
25#include <endian.h>
26#include <errno.h>
27#include <unistd.h>
28#include <fcntl.h>
bellard7854b052003-03-29 17:22:23 +000029#include <time.h>
pbrook82e671d2008-06-09 12:10:22 +000030#include <limits.h>
balrog8d5c28e2008-09-20 02:29:38 +000031#include <dirent.h>
bellard31e31b82003-02-18 22:55:36 +000032#include <sys/types.h>
thsd08d3bb2007-03-19 13:09:22 +000033#include <sys/ipc.h>
34#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000035#include <sys/wait.h>
36#include <sys/time.h>
37#include <sys/stat.h>
38#include <sys/mount.h>
ths39b9aae2007-02-11 18:36:44 +000039#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000040#include <sys/resource.h>
41#include <sys/mman.h>
42#include <sys/swap.h>
43#include <signal.h>
44#include <sched.h>
45#include <sys/socket.h>
46#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000047#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000048#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000049#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000050#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000051#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000052#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000053#include <sys/sysinfo.h>
bellard72f03902003-02-18 23:33:18 +000054//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000055#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000056#include <netinet/tcp.h>
aurel320b6d3ae2008-09-15 07:43:43 +000057#include <qemu-common.h>
bellard31e31b82003-02-18 22:55:36 +000058
59#define termios host_termios
60#define winsize host_winsize
61#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000062#define sgttyb host_sgttyb /* same as target */
63#define tchars host_tchars /* same as target */
64#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000065
66#include <linux/termios.h>
67#include <linux/unistd.h>
68#include <linux/utsname.h>
69#include <linux/cdrom.h>
70#include <linux/hdreg.h>
71#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000072#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000073#include <linux/mtio.h>
pbrookd7e40362008-05-23 16:06:43 +000074#include "linux_loop.h"
bellard31e31b82003-02-18 22:55:36 +000075
bellard3ef693a2003-03-23 20:17:16 +000076#include "qemu.h"
balrog526ccb72008-07-16 12:13:52 +000077#include "qemu-common.h"
bellard31e31b82003-02-18 22:55:36 +000078
pbrook30813ce2008-06-02 15:45:44 +000079#if defined(USE_NPTL)
80#include <linux/futex.h>
pbrookd865bab2008-06-07 22:12:17 +000081#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
82 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
83#else
84/* XXX: Hardcode the above values. */
85#define CLONE_NPTL_FLAGS2 0
pbrook30813ce2008-06-02 15:45:44 +000086#endif
87
bellard72f03902003-02-18 23:33:18 +000088//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +000089
pbrooke6e59062006-10-22 00:18:54 +000090#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
ths48733d12007-10-08 13:36:46 +000091 || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
bellard67867302003-11-23 17:05:30 +000092/* 16 bit uid wrappers emulation */
93#define USE_UID16
94#endif
95
bellard1a9353d2003-03-16 20:28:50 +000096//#include <linux/msdos_fs.h>
97#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
98#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
99
bellard70a194b2003-08-11 22:20:16 +0000100
bellard70a194b2003-08-11 22:20:16 +0000101#undef _syscall0
102#undef _syscall1
103#undef _syscall2
104#undef _syscall3
105#undef _syscall4
106#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000107#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000108
bellard83fcb512006-06-14 13:37:16 +0000109#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000110static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000111{ \
112 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000113}
114
bellard83fcb512006-06-14 13:37:16 +0000115#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000116static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000117{ \
118 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000119}
120
bellard83fcb512006-06-14 13:37:16 +0000121#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000122static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000123{ \
124 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000125}
126
bellard83fcb512006-06-14 13:37:16 +0000127#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000128static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000129{ \
130 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000131}
132
bellard83fcb512006-06-14 13:37:16 +0000133#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000134static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000135{ \
136 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000137}
138
bellard83fcb512006-06-14 13:37:16 +0000139#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
140 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000141static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000142{ \
143 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000144}
bellard83fcb512006-06-14 13:37:16 +0000145
146
147#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
148 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000149static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
150 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000151{ \
152 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
153}
154
bellard70a194b2003-08-11 22:20:16 +0000155
bellard31e31b82003-02-18 22:55:36 +0000156#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000157#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000158#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000159#define __NR_sys_fchownat __NR_fchownat
balrog6a24a772008-09-20 02:23:36 +0000160#define __NR_sys_fstatat64 __NR_fstatat64
balrogac8a6552008-09-20 02:25:39 +0000161#define __NR_sys_futimesat __NR_futimesat
bellard72f03902003-02-18 23:33:18 +0000162#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000163#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000164#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000165#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000166#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000167#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000168#define __NR_sys_mknodat __NR_mknodat
ths82424832007-09-24 09:21:55 +0000169#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000170#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000171#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000172#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000173#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000174#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000175#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000176#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000177#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000178#define __NR_sys_utimensat __NR_utimensat
pbrookbd0c5662008-05-29 14:34:11 +0000179#define __NR_sys_futex __NR_futex
bellard31e31b82003-02-18 22:55:36 +0000180
bellardbc51c5c2004-03-17 23:46:04 +0000181#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
bellard9af9eaa2003-04-07 21:34:41 +0000182#define __NR__llseek __NR_lseek
183#endif
184
bellard72f03902003-02-18 23:33:18 +0000185#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000186_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000187#else
ths0da46a62007-10-20 20:23:07 +0000188/* This is a replacement for the host gettid() and must return a host
189 errno. */
bellard72f03902003-02-18 23:33:18 +0000190static int gettid(void) {
191 return -ENOSYS;
192}
193#endif
bellard31e31b82003-02-18 22:55:36 +0000194_syscall1(int,sys_uname,struct new_utsname *,buf)
ths92a34c12007-09-24 09:27:49 +0000195#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
196_syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
197#endif
ths814d7972007-09-24 09:26:51 +0000198#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
199_syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
200 mode_t,mode,int,flags)
201#endif
blueswir14583f582008-08-24 10:35:55 +0000202#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
thsccfa72b2007-09-24 09:23:34 +0000203_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
204 uid_t,owner,gid_t,group,int,flags)
205#endif
balrog6a24a772008-09-20 02:23:36 +0000206#if defined(TARGET_NR_fstatat64) && defined(__NR_fstatat64)
207_syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
208 struct stat *,buf,int,flags)
209#endif
balrogac8a6552008-09-20 02:25:39 +0000210#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
211_syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
212 const struct timeval *,times)
213#endif
bellard72f03902003-02-18 23:33:18 +0000214_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
blueswir14583f582008-08-24 10:35:55 +0000215#if TARGET_ABI_BITS == 32
bellard72f03902003-02-18 23:33:18 +0000216_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
blueswir14583f582008-08-24 10:35:55 +0000217#endif
ths3ae43202007-09-16 21:39:48 +0000218#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +0000219_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000220#endif
thsc6cda172007-10-09 03:42:34 +0000221_syscall2(int, sys_getpriority, int, which, int, who);
blueswir18fcd3692008-08-17 20:26:25 +0000222#if !defined (__x86_64__)
bellard31e31b82003-02-18 22:55:36 +0000223_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
224 loff_t *, res, uint, wh);
blueswir18fcd3692008-08-17 20:26:25 +0000225#endif
ths64f0ce42007-09-24 09:25:06 +0000226#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
227_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
228 int,newdirfd,const char *,newpath,int,flags)
229#endif
ths4472ad02007-09-24 09:22:32 +0000230#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
231_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
232#endif
ths75ac37a2007-09-24 09:23:05 +0000233#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
234_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
235 mode_t,mode,dev_t,dev)
236#endif
ths82424832007-09-24 09:21:55 +0000237#if defined(TARGET_NR_openat) && defined(__NR_openat)
238_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
239#endif
ths5e0ccb12007-09-24 09:26:10 +0000240#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
241_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
242 char *,buf,size_t,bufsize)
243#endif
ths722183f2007-09-24 09:24:37 +0000244#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
245_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
246 int,newdirfd,const char *,newpath)
247#endif
bellard66fb9762003-03-23 01:06:05 +0000248_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
thsb51eaa82007-09-25 16:09:22 +0000249#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000250_syscall3(int,sys_symlinkat,const char *,oldpath,
251 int,newdirfd,const char *,newpath)
252#endif
ths7494b0f2007-02-11 18:26:53 +0000253_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
ths3ae43202007-09-16 21:39:48 +0000254#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +0000255_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
ths4cae1d12007-07-12 11:06:53 +0000256#endif
ths3ae43202007-09-16 21:39:48 +0000257#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +0000258_syscall2(int,sys_tkill,int,tid,int,sig)
259#endif
bellardec86b0f2003-04-11 00:15:04 +0000260#ifdef __NR_exit_group
261_syscall1(int,exit_group,int,error_code)
262#endif
ths6f5b89a2007-03-02 20:48:00 +0000263#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
264_syscall1(int,set_tid_address,int *,tidptr)
265#endif
ths8170f562007-09-24 09:24:11 +0000266#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
267_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
268#endif
ths9007f0e2007-09-25 17:50:37 +0000269#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
270_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
271 const struct timespec *,tsp,int,flags)
272#endif
blueswir18fcd3692008-08-17 20:26:25 +0000273#if defined(USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +0000274#if defined(TARGET_NR_futex) && defined(__NR_futex)
275_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
276 const struct timespec *,timeout,int *,uaddr2,int,val3)
blueswir18fcd3692008-08-17 20:26:25 +0000277#endif
pbrookbd0c5662008-05-29 14:34:11 +0000278#endif
bellard66fb9762003-03-23 01:06:05 +0000279
280extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000281extern int flock(int, int);
282extern int setfsuid(int);
283extern int setfsgid(int);
bellard19b84f32003-05-08 15:41:49 +0000284extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000285
thsb92c47c2007-11-01 00:07:38 +0000286#define ERRNO_TABLE_SIZE 1200
287
288/* target_to_host_errno_table[] is initialized from
289 * host_to_target_errno_table[] in syscall_init(). */
290static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
291};
292
ths637947f2007-06-01 12:09:19 +0000293/*
thsfe8f0962007-07-12 10:59:21 +0000294 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000295 * minus the errnos that are not actually generic to all archs.
296 */
thsb92c47c2007-11-01 00:07:38 +0000297static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000298 [EIDRM] = TARGET_EIDRM,
299 [ECHRNG] = TARGET_ECHRNG,
300 [EL2NSYNC] = TARGET_EL2NSYNC,
301 [EL3HLT] = TARGET_EL3HLT,
302 [EL3RST] = TARGET_EL3RST,
303 [ELNRNG] = TARGET_ELNRNG,
304 [EUNATCH] = TARGET_EUNATCH,
305 [ENOCSI] = TARGET_ENOCSI,
306 [EL2HLT] = TARGET_EL2HLT,
307 [EDEADLK] = TARGET_EDEADLK,
308 [ENOLCK] = TARGET_ENOLCK,
309 [EBADE] = TARGET_EBADE,
310 [EBADR] = TARGET_EBADR,
311 [EXFULL] = TARGET_EXFULL,
312 [ENOANO] = TARGET_ENOANO,
313 [EBADRQC] = TARGET_EBADRQC,
314 [EBADSLT] = TARGET_EBADSLT,
315 [EBFONT] = TARGET_EBFONT,
316 [ENOSTR] = TARGET_ENOSTR,
317 [ENODATA] = TARGET_ENODATA,
318 [ETIME] = TARGET_ETIME,
319 [ENOSR] = TARGET_ENOSR,
320 [ENONET] = TARGET_ENONET,
321 [ENOPKG] = TARGET_ENOPKG,
322 [EREMOTE] = TARGET_EREMOTE,
323 [ENOLINK] = TARGET_ENOLINK,
324 [EADV] = TARGET_EADV,
325 [ESRMNT] = TARGET_ESRMNT,
326 [ECOMM] = TARGET_ECOMM,
327 [EPROTO] = TARGET_EPROTO,
328 [EDOTDOT] = TARGET_EDOTDOT,
329 [EMULTIHOP] = TARGET_EMULTIHOP,
330 [EBADMSG] = TARGET_EBADMSG,
331 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
332 [EOVERFLOW] = TARGET_EOVERFLOW,
333 [ENOTUNIQ] = TARGET_ENOTUNIQ,
334 [EBADFD] = TARGET_EBADFD,
335 [EREMCHG] = TARGET_EREMCHG,
336 [ELIBACC] = TARGET_ELIBACC,
337 [ELIBBAD] = TARGET_ELIBBAD,
338 [ELIBSCN] = TARGET_ELIBSCN,
339 [ELIBMAX] = TARGET_ELIBMAX,
340 [ELIBEXEC] = TARGET_ELIBEXEC,
341 [EILSEQ] = TARGET_EILSEQ,
342 [ENOSYS] = TARGET_ENOSYS,
343 [ELOOP] = TARGET_ELOOP,
344 [ERESTART] = TARGET_ERESTART,
345 [ESTRPIPE] = TARGET_ESTRPIPE,
346 [ENOTEMPTY] = TARGET_ENOTEMPTY,
347 [EUSERS] = TARGET_EUSERS,
348 [ENOTSOCK] = TARGET_ENOTSOCK,
349 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
350 [EMSGSIZE] = TARGET_EMSGSIZE,
351 [EPROTOTYPE] = TARGET_EPROTOTYPE,
352 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
353 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
354 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
355 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
356 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
357 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
358 [EADDRINUSE] = TARGET_EADDRINUSE,
359 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
360 [ENETDOWN] = TARGET_ENETDOWN,
361 [ENETUNREACH] = TARGET_ENETUNREACH,
362 [ENETRESET] = TARGET_ENETRESET,
363 [ECONNABORTED] = TARGET_ECONNABORTED,
364 [ECONNRESET] = TARGET_ECONNRESET,
365 [ENOBUFS] = TARGET_ENOBUFS,
366 [EISCONN] = TARGET_EISCONN,
367 [ENOTCONN] = TARGET_ENOTCONN,
368 [EUCLEAN] = TARGET_EUCLEAN,
369 [ENOTNAM] = TARGET_ENOTNAM,
370 [ENAVAIL] = TARGET_ENAVAIL,
371 [EISNAM] = TARGET_EISNAM,
372 [EREMOTEIO] = TARGET_EREMOTEIO,
373 [ESHUTDOWN] = TARGET_ESHUTDOWN,
374 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
375 [ETIMEDOUT] = TARGET_ETIMEDOUT,
376 [ECONNREFUSED] = TARGET_ECONNREFUSED,
377 [EHOSTDOWN] = TARGET_EHOSTDOWN,
378 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
379 [EALREADY] = TARGET_EALREADY,
380 [EINPROGRESS] = TARGET_EINPROGRESS,
381 [ESTALE] = TARGET_ESTALE,
382 [ECANCELED] = TARGET_ECANCELED,
383 [ENOMEDIUM] = TARGET_ENOMEDIUM,
384 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000385#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000386 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000387#endif
388#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000389 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000390#endif
391#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000392 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000393#endif
394#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000395 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000396#endif
397#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000398 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000399#endif
400#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000401 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000402#endif
thsb92c47c2007-11-01 00:07:38 +0000403};
ths637947f2007-06-01 12:09:19 +0000404
405static inline int host_to_target_errno(int err)
406{
407 if(host_to_target_errno_table[err])
408 return host_to_target_errno_table[err];
409 return err;
410}
411
thsb92c47c2007-11-01 00:07:38 +0000412static inline int target_to_host_errno(int err)
413{
414 if (target_to_host_errno_table[err])
415 return target_to_host_errno_table[err];
416 return err;
417}
418
blueswir1992f48a2007-10-14 16:27:31 +0000419static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000420{
421 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000422 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000423 else
424 return ret;
425}
426
blueswir1992f48a2007-10-14 16:27:31 +0000427static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000428{
blueswir1992f48a2007-10-14 16:27:31 +0000429 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000430}
431
thsb92c47c2007-11-01 00:07:38 +0000432char *target_strerror(int err)
433{
434 return strerror(target_to_host_errno(err));
435}
436
blueswir1992f48a2007-10-14 16:27:31 +0000437static abi_ulong target_brk;
438static abi_ulong target_original_brk;
bellard31e31b82003-02-18 22:55:36 +0000439
blueswir1992f48a2007-10-14 16:27:31 +0000440void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000441{
blueswir14c1de732007-07-07 20:45:44 +0000442 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
bellard31e31b82003-02-18 22:55:36 +0000443}
444
ths0da46a62007-10-20 20:23:07 +0000445/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000446abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000447{
blueswir1992f48a2007-10-14 16:27:31 +0000448 abi_ulong brk_page;
449 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000450 int new_alloc_size;
451
452 if (!new_brk)
pbrook53a59602006-03-25 19:31:22 +0000453 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000454 if (new_brk < target_original_brk)
balrog7ab240a2008-04-26 12:17:34 +0000455 return target_brk;
ths3b46e622007-09-17 08:09:54 +0000456
pbrook53a59602006-03-25 19:31:22 +0000457 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000458
459 /* If the new brk is less than this, set it and we're done... */
460 if (new_brk < brk_page) {
461 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000462 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000463 }
464
465 /* We need to allocate more memory after the brk... */
bellard54936002003-05-13 00:25:15 +0000466 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
ths5fafdf22007-09-16 21:08:06 +0000467 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000468 PROT_READ|PROT_WRITE,
469 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
balrog7ab240a2008-04-26 12:17:34 +0000470
471 if (!is_error(mapped_addr))
bellard31e31b82003-02-18 22:55:36 +0000472 target_brk = new_brk;
balrog7ab240a2008-04-26 12:17:34 +0000473
474 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000475}
476
ths26edcf42007-12-09 02:25:24 +0000477static inline abi_long copy_from_user_fdset(fd_set *fds,
478 abi_ulong target_fds_addr,
479 int n)
bellard31e31b82003-02-18 22:55:36 +0000480{
ths26edcf42007-12-09 02:25:24 +0000481 int i, nw, j, k;
482 abi_ulong b, *target_fds;
483
484 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
485 if (!(target_fds = lock_user(VERIFY_READ,
486 target_fds_addr,
487 sizeof(abi_ulong) * nw,
488 1)))
489 return -TARGET_EFAULT;
490
491 FD_ZERO(fds);
492 k = 0;
493 for (i = 0; i < nw; i++) {
494 /* grab the abi_ulong */
495 __get_user(b, &target_fds[i]);
496 for (j = 0; j < TARGET_ABI_BITS; j++) {
497 /* check the bit inside the abi_ulong */
498 if ((b >> j) & 1)
499 FD_SET(k, fds);
500 k++;
bellard31e31b82003-02-18 22:55:36 +0000501 }
bellard31e31b82003-02-18 22:55:36 +0000502 }
ths26edcf42007-12-09 02:25:24 +0000503
504 unlock_user(target_fds, target_fds_addr, 0);
505
506 return 0;
bellard31e31b82003-02-18 22:55:36 +0000507}
508
ths26edcf42007-12-09 02:25:24 +0000509static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
510 const fd_set *fds,
511 int n)
bellard31e31b82003-02-18 22:55:36 +0000512{
bellard31e31b82003-02-18 22:55:36 +0000513 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000514 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000515 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000516
ths26edcf42007-12-09 02:25:24 +0000517 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
518 if (!(target_fds = lock_user(VERIFY_WRITE,
519 target_fds_addr,
520 sizeof(abi_ulong) * nw,
521 0)))
522 return -TARGET_EFAULT;
523
524 k = 0;
525 for (i = 0; i < nw; i++) {
526 v = 0;
527 for (j = 0; j < TARGET_ABI_BITS; j++) {
528 v |= ((FD_ISSET(k, fds) != 0) << j);
529 k++;
bellard31e31b82003-02-18 22:55:36 +0000530 }
ths26edcf42007-12-09 02:25:24 +0000531 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000532 }
ths26edcf42007-12-09 02:25:24 +0000533
534 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
535
536 return 0;
bellard31e31b82003-02-18 22:55:36 +0000537}
538
bellardc596ed12003-07-13 17:32:31 +0000539#if defined(__alpha__)
540#define HOST_HZ 1024
541#else
542#define HOST_HZ 100
543#endif
544
blueswir1992f48a2007-10-14 16:27:31 +0000545static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000546{
547#if HOST_HZ == TARGET_HZ
548 return ticks;
549#else
550 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
551#endif
552}
553
bellard579a97f2007-11-11 14:26:47 +0000554static inline abi_long host_to_target_rusage(abi_ulong target_addr,
555 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000556{
pbrook53a59602006-03-25 19:31:22 +0000557 struct target_rusage *target_rusage;
558
bellard579a97f2007-11-11 14:26:47 +0000559 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
560 return -TARGET_EFAULT;
bellardb4091862003-05-16 15:39:34 +0000561 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
562 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
563 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
564 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
565 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
566 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
567 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
568 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
569 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
570 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
571 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
572 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
573 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
574 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
575 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
576 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
577 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
578 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000579 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000580
581 return 0;
bellardb4091862003-05-16 15:39:34 +0000582}
583
ths788f5ec2007-12-09 02:37:05 +0000584static inline abi_long copy_from_user_timeval(struct timeval *tv,
585 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000586{
pbrook53a59602006-03-25 19:31:22 +0000587 struct target_timeval *target_tv;
588
ths788f5ec2007-12-09 02:37:05 +0000589 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000590 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000591
592 __get_user(tv->tv_sec, &target_tv->tv_sec);
593 __get_user(tv->tv_usec, &target_tv->tv_usec);
594
595 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000596
597 return 0;
bellard31e31b82003-02-18 22:55:36 +0000598}
599
ths788f5ec2007-12-09 02:37:05 +0000600static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
601 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000602{
pbrook53a59602006-03-25 19:31:22 +0000603 struct target_timeval *target_tv;
604
ths788f5ec2007-12-09 02:37:05 +0000605 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000606 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000607
608 __put_user(tv->tv_sec, &target_tv->tv_sec);
609 __put_user(tv->tv_usec, &target_tv->tv_usec);
610
611 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000612
613 return 0;
bellard31e31b82003-02-18 22:55:36 +0000614}
615
616
ths0da46a62007-10-20 20:23:07 +0000617/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000618static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000619 abi_ulong rfd_addr, abi_ulong wfd_addr,
620 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000621{
622 fd_set rfds, wfds, efds;
623 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
624 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000625 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000626
ths26edcf42007-12-09 02:25:24 +0000627 if (rfd_addr) {
628 if (copy_from_user_fdset(&rfds, rfd_addr, n))
629 return -TARGET_EFAULT;
630 rfds_ptr = &rfds;
pbrook53a59602006-03-25 19:31:22 +0000631 } else {
pbrook53a59602006-03-25 19:31:22 +0000632 rfds_ptr = NULL;
633 }
ths26edcf42007-12-09 02:25:24 +0000634 if (wfd_addr) {
635 if (copy_from_user_fdset(&wfds, wfd_addr, n))
636 return -TARGET_EFAULT;
637 wfds_ptr = &wfds;
pbrook53a59602006-03-25 19:31:22 +0000638 } else {
pbrook53a59602006-03-25 19:31:22 +0000639 wfds_ptr = NULL;
640 }
ths26edcf42007-12-09 02:25:24 +0000641 if (efd_addr) {
642 if (copy_from_user_fdset(&efds, efd_addr, n))
643 return -TARGET_EFAULT;
644 efds_ptr = &efds;
pbrook53a59602006-03-25 19:31:22 +0000645 } else {
pbrook53a59602006-03-25 19:31:22 +0000646 efds_ptr = NULL;
647 }
ths3b46e622007-09-17 08:09:54 +0000648
ths26edcf42007-12-09 02:25:24 +0000649 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +0000650 if (copy_from_user_timeval(&tv, target_tv_addr))
651 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000652 tv_ptr = &tv;
653 } else {
654 tv_ptr = NULL;
655 }
ths26edcf42007-12-09 02:25:24 +0000656
bellard31e31b82003-02-18 22:55:36 +0000657 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000658
ths26edcf42007-12-09 02:25:24 +0000659 if (!is_error(ret)) {
660 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
661 return -TARGET_EFAULT;
662 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
663 return -TARGET_EFAULT;
664 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
665 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000666
ths788f5ec2007-12-09 02:37:05 +0000667 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
668 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000669 }
bellard579a97f2007-11-11 14:26:47 +0000670
bellard31e31b82003-02-18 22:55:36 +0000671 return ret;
672}
673
bellard579a97f2007-11-11 14:26:47 +0000674static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
675 abi_ulong target_addr,
676 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000677{
pbrook53a59602006-03-25 19:31:22 +0000678 struct target_sockaddr *target_saddr;
679
bellard579a97f2007-11-11 14:26:47 +0000680 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
681 if (!target_saddr)
682 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000683 memcpy(addr, target_saddr, len);
684 addr->sa_family = tswap16(target_saddr->sa_family);
685 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000686
687 return 0;
bellard7854b052003-03-29 17:22:23 +0000688}
689
bellard579a97f2007-11-11 14:26:47 +0000690static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
691 struct sockaddr *addr,
692 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000693{
pbrook53a59602006-03-25 19:31:22 +0000694 struct target_sockaddr *target_saddr;
695
bellard579a97f2007-11-11 14:26:47 +0000696 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
697 if (!target_saddr)
698 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000699 memcpy(target_saddr, addr, len);
700 target_saddr->sa_family = tswap16(addr->sa_family);
701 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +0000702
703 return 0;
bellard7854b052003-03-29 17:22:23 +0000704}
705
pbrook53a59602006-03-25 19:31:22 +0000706/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +0000707static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
708 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +0000709{
710 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +0000711 abi_long msg_controllen;
712 abi_ulong target_cmsg_addr;
713 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +0000714 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +0000715
716 msg_controllen = tswapl(target_msgh->msg_controllen);
717 if (msg_controllen < sizeof (struct target_cmsghdr))
718 goto the_end;
719 target_cmsg_addr = tswapl(target_msgh->msg_control);
720 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
721 if (!target_cmsg)
722 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +0000723
724 while (cmsg && target_cmsg) {
725 void *data = CMSG_DATA(cmsg);
726 void *target_data = TARGET_CMSG_DATA(target_cmsg);
727
ths5fafdf22007-09-16 21:08:06 +0000728 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +0000729 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
730
731 space += CMSG_SPACE(len);
732 if (space > msgh->msg_controllen) {
733 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000734 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000735 break;
736 }
737
738 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
739 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
740 cmsg->cmsg_len = CMSG_LEN(len);
741
bellard3532fa72006-06-24 15:06:03 +0000742 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000743 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
744 memcpy(data, target_data, len);
745 } else {
746 int *fd = (int *)data;
747 int *target_fd = (int *)target_data;
748 int i, numfds = len / sizeof(int);
749
750 for (i = 0; i < numfds; i++)
751 fd[i] = tswap32(target_fd[i]);
752 }
753
754 cmsg = CMSG_NXTHDR(msgh, cmsg);
755 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
756 }
bellard5a4a8982007-11-11 17:39:18 +0000757 unlock_user(target_cmsg, target_cmsg_addr, 0);
758 the_end:
bellard7854b052003-03-29 17:22:23 +0000759 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +0000760 return 0;
bellard7854b052003-03-29 17:22:23 +0000761}
762
pbrook53a59602006-03-25 19:31:22 +0000763/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +0000764static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
765 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +0000766{
767 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +0000768 abi_long msg_controllen;
769 abi_ulong target_cmsg_addr;
770 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +0000771 socklen_t space = 0;
772
bellard5a4a8982007-11-11 17:39:18 +0000773 msg_controllen = tswapl(target_msgh->msg_controllen);
774 if (msg_controllen < sizeof (struct target_cmsghdr))
775 goto the_end;
776 target_cmsg_addr = tswapl(target_msgh->msg_control);
777 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
778 if (!target_cmsg)
779 return -TARGET_EFAULT;
780
bellard7854b052003-03-29 17:22:23 +0000781 while (cmsg && target_cmsg) {
782 void *data = CMSG_DATA(cmsg);
783 void *target_data = TARGET_CMSG_DATA(target_cmsg);
784
785 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
786
787 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +0000788 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +0000789 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000790 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000791 break;
792 }
793
794 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
795 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
796 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
797
bellard3532fa72006-06-24 15:06:03 +0000798 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000799 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
800 memcpy(target_data, data, len);
801 } else {
802 int *fd = (int *)data;
803 int *target_fd = (int *)target_data;
804 int i, numfds = len / sizeof(int);
805
806 for (i = 0; i < numfds; i++)
807 target_fd[i] = tswap32(fd[i]);
808 }
809
810 cmsg = CMSG_NXTHDR(msgh, cmsg);
811 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
812 }
bellard5a4a8982007-11-11 17:39:18 +0000813 unlock_user(target_cmsg, target_cmsg_addr, space);
814 the_end:
815 target_msgh->msg_controllen = tswapl(space);
816 return 0;
bellard7854b052003-03-29 17:22:23 +0000817}
818
ths0da46a62007-10-20 20:23:07 +0000819/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000820static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +0000821 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +0000822{
blueswir1992f48a2007-10-14 16:27:31 +0000823 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000824 int val;
ths3b46e622007-09-17 08:09:54 +0000825
bellard8853f862004-02-22 14:57:26 +0000826 switch(level) {
827 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +0000828 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +0000829 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +0000830 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +0000831
bellard2f619692007-11-16 10:46:05 +0000832 if (get_user_u32(val, optval_addr))
833 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000834 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
835 break;
836 case SOL_IP:
837 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +0000838 case IP_TOS:
839 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +0000840 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +0000841 case IP_ROUTER_ALERT:
842 case IP_RECVOPTS:
843 case IP_RETOPTS:
844 case IP_PKTINFO:
845 case IP_MTU_DISCOVER:
846 case IP_RECVERR:
847 case IP_RECVTOS:
848#ifdef IP_FREEBIND
849 case IP_FREEBIND:
850#endif
851 case IP_MULTICAST_TTL:
852 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +0000853 val = 0;
854 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +0000855 if (get_user_u32(val, optval_addr))
856 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000857 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +0000858 if (get_user_u8(val, optval_addr))
859 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000860 }
861 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
862 break;
863 default:
864 goto unimplemented;
865 }
866 break;
bellard3532fa72006-06-24 15:06:03 +0000867 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +0000868 switch (optname) {
869 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +0000870 case TARGET_SO_DEBUG:
871 optname = SO_DEBUG;
872 break;
873 case TARGET_SO_REUSEADDR:
874 optname = SO_REUSEADDR;
875 break;
876 case TARGET_SO_TYPE:
877 optname = SO_TYPE;
878 break;
879 case TARGET_SO_ERROR:
880 optname = SO_ERROR;
881 break;
882 case TARGET_SO_DONTROUTE:
883 optname = SO_DONTROUTE;
884 break;
885 case TARGET_SO_BROADCAST:
886 optname = SO_BROADCAST;
887 break;
888 case TARGET_SO_SNDBUF:
889 optname = SO_SNDBUF;
890 break;
891 case TARGET_SO_RCVBUF:
892 optname = SO_RCVBUF;
893 break;
894 case TARGET_SO_KEEPALIVE:
895 optname = SO_KEEPALIVE;
896 break;
897 case TARGET_SO_OOBINLINE:
898 optname = SO_OOBINLINE;
899 break;
900 case TARGET_SO_NO_CHECK:
901 optname = SO_NO_CHECK;
902 break;
903 case TARGET_SO_PRIORITY:
904 optname = SO_PRIORITY;
905 break;
bellard5e83e8e2005-03-01 22:32:06 +0000906#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +0000907 case TARGET_SO_BSDCOMPAT:
908 optname = SO_BSDCOMPAT;
909 break;
bellard5e83e8e2005-03-01 22:32:06 +0000910#endif
bellard3532fa72006-06-24 15:06:03 +0000911 case TARGET_SO_PASSCRED:
912 optname = SO_PASSCRED;
913 break;
914 case TARGET_SO_TIMESTAMP:
915 optname = SO_TIMESTAMP;
916 break;
917 case TARGET_SO_RCVLOWAT:
918 optname = SO_RCVLOWAT;
919 break;
920 case TARGET_SO_RCVTIMEO:
921 optname = SO_RCVTIMEO;
922 break;
923 case TARGET_SO_SNDTIMEO:
924 optname = SO_SNDTIMEO;
925 break;
bellard8853f862004-02-22 14:57:26 +0000926 break;
927 default:
928 goto unimplemented;
929 }
bellard3532fa72006-06-24 15:06:03 +0000930 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +0000931 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +0000932
bellard2f619692007-11-16 10:46:05 +0000933 if (get_user_u32(val, optval_addr))
934 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +0000935 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +0000936 break;
bellard7854b052003-03-29 17:22:23 +0000937 default:
bellard8853f862004-02-22 14:57:26 +0000938 unimplemented:
939 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths6fa13c12007-12-18 02:41:04 +0000940 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +0000941 }
bellard8853f862004-02-22 14:57:26 +0000942 return ret;
bellard7854b052003-03-29 17:22:23 +0000943}
944
ths0da46a62007-10-20 20:23:07 +0000945/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000946static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +0000947 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +0000948{
blueswir1992f48a2007-10-14 16:27:31 +0000949 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +0000950 int len, val;
951 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +0000952
953 switch(level) {
bellard3532fa72006-06-24 15:06:03 +0000954 case TARGET_SOL_SOCKET:
955 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +0000956 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +0000957 case TARGET_SO_LINGER:
958 case TARGET_SO_RCVTIMEO:
959 case TARGET_SO_SNDTIMEO:
960 case TARGET_SO_PEERCRED:
961 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +0000962 /* These don't just return a single integer */
963 goto unimplemented;
964 default:
bellard2efbe912005-07-23 15:10:20 +0000965 goto int_case;
966 }
967 break;
968 case SOL_TCP:
969 /* TCP options all take an 'int' value. */
970 int_case:
bellard2f619692007-11-16 10:46:05 +0000971 if (get_user_u32(len, optlen))
972 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000973 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000974 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +0000975 lv = sizeof(int);
976 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
977 if (ret < 0)
978 return ret;
979 val = tswap32(val);
980 if (len > lv)
981 len = lv;
bellard2f619692007-11-16 10:46:05 +0000982 if (len == 4) {
983 if (put_user_u32(val, optval_addr))
984 return -TARGET_EFAULT;
985 } else {
986 if (put_user_u8(val, optval_addr))
987 return -TARGET_EFAULT;
988 }
989 if (put_user_u32(len, optlen))
990 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000991 break;
992 case SOL_IP:
993 switch(optname) {
994 case IP_TOS:
995 case IP_TTL:
996 case IP_HDRINCL:
997 case IP_ROUTER_ALERT:
998 case IP_RECVOPTS:
999 case IP_RETOPTS:
1000 case IP_PKTINFO:
1001 case IP_MTU_DISCOVER:
1002 case IP_RECVERR:
1003 case IP_RECVTOS:
1004#ifdef IP_FREEBIND
1005 case IP_FREEBIND:
1006#endif
1007 case IP_MULTICAST_TTL:
1008 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001009 if (get_user_u32(len, optlen))
1010 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001011 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001012 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +00001013 lv = sizeof(int);
1014 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1015 if (ret < 0)
1016 return ret;
bellard2efbe912005-07-23 15:10:20 +00001017 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001018 len = 1;
bellard2f619692007-11-16 10:46:05 +00001019 if (put_user_u32(len, optlen)
1020 || put_user_u8(val, optval_addr))
1021 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001022 } else {
bellard2efbe912005-07-23 15:10:20 +00001023 if (len > sizeof(int))
1024 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001025 if (put_user_u32(len, optlen)
1026 || put_user_u32(val, optval_addr))
1027 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001028 }
bellard8853f862004-02-22 14:57:26 +00001029 break;
bellard2efbe912005-07-23 15:10:20 +00001030 default:
thsc02f4992007-12-18 02:39:59 +00001031 ret = -TARGET_ENOPROTOOPT;
1032 break;
bellard8853f862004-02-22 14:57:26 +00001033 }
1034 break;
1035 default:
1036 unimplemented:
1037 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1038 level, optname);
thsc02f4992007-12-18 02:39:59 +00001039 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001040 break;
1041 }
1042 return ret;
bellard7854b052003-03-29 17:22:23 +00001043}
1044
bellard579a97f2007-11-11 14:26:47 +00001045/* FIXME
1046 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1047 * other lock functions have a return code of 0 for failure.
1048 */
1049static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1050 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001051{
1052 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001053 abi_ulong base;
bellard579a97f2007-11-11 14:26:47 +00001054 int i, j;
pbrook53a59602006-03-25 19:31:22 +00001055
bellard579a97f2007-11-11 14:26:47 +00001056 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1057 if (!target_vec)
1058 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001059 for(i = 0;i < count; i++) {
1060 base = tswapl(target_vec[i].iov_base);
1061 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001062 if (vec[i].iov_len != 0) {
1063 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrog7bedce82008-02-10 13:28:08 +00001064 if (!vec[i].iov_base && vec[i].iov_len)
bellard41df8412008-02-04 22:26:57 +00001065 goto fail;
1066 } else {
1067 /* zero length pointer is ignored */
1068 vec[i].iov_base = NULL;
1069 }
pbrook53a59602006-03-25 19:31:22 +00001070 }
1071 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001072 return 0;
1073 fail:
1074 /* failure - unwind locks */
1075 for (j = 0; j < i; j++) {
1076 base = tswapl(target_vec[j].iov_base);
1077 unlock_user(vec[j].iov_base, base, 0);
1078 }
1079 unlock_user (target_vec, target_addr, 0);
1080 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001081}
1082
bellard579a97f2007-11-11 14:26:47 +00001083static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1084 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001085{
1086 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001087 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001088 int i;
1089
bellard579a97f2007-11-11 14:26:47 +00001090 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1091 if (!target_vec)
1092 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001093 for(i = 0;i < count; i++) {
1094 base = tswapl(target_vec[i].iov_base);
1095 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1096 }
1097 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001098
1099 return 0;
pbrook53a59602006-03-25 19:31:22 +00001100}
1101
ths0da46a62007-10-20 20:23:07 +00001102/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001103static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001104{
1105#if defined(TARGET_MIPS)
1106 switch(type) {
1107 case TARGET_SOCK_DGRAM:
1108 type = SOCK_DGRAM;
1109 break;
1110 case TARGET_SOCK_STREAM:
1111 type = SOCK_STREAM;
1112 break;
1113 case TARGET_SOCK_RAW:
1114 type = SOCK_RAW;
1115 break;
1116 case TARGET_SOCK_RDM:
1117 type = SOCK_RDM;
1118 break;
1119 case TARGET_SOCK_SEQPACKET:
1120 type = SOCK_SEQPACKET;
1121 break;
1122 case TARGET_SOCK_PACKET:
1123 type = SOCK_PACKET;
1124 break;
1125 }
1126#endif
balrog12bc92a2007-10-30 21:06:14 +00001127 if (domain == PF_NETLINK)
1128 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001129 return get_errno(socket(domain, type, protocol));
1130}
1131
ths0da46a62007-10-20 20:23:07 +00001132/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001133static abi_long do_bind(int sockfd, abi_ulong target_addr,
1134 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001135{
1136 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001137
bellard3532fa72006-06-24 15:06:03 +00001138 target_to_host_sockaddr(addr, target_addr, addrlen);
1139 return get_errno(bind(sockfd, addr, addrlen));
1140}
1141
ths0da46a62007-10-20 20:23:07 +00001142/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001143static abi_long do_connect(int sockfd, abi_ulong target_addr,
1144 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001145{
1146 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001147
bellard3532fa72006-06-24 15:06:03 +00001148 target_to_host_sockaddr(addr, target_addr, addrlen);
1149 return get_errno(connect(sockfd, addr, addrlen));
1150}
1151
ths0da46a62007-10-20 20:23:07 +00001152/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001153static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1154 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001155{
blueswir1992f48a2007-10-14 16:27:31 +00001156 abi_long ret;
bellard3532fa72006-06-24 15:06:03 +00001157 struct target_msghdr *msgp;
1158 struct msghdr msg;
1159 int count;
1160 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001161 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001162
bellard579a97f2007-11-11 14:26:47 +00001163 /* FIXME */
1164 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1165 msgp,
1166 target_msg,
1167 send ? 1 : 0))
1168 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001169 if (msgp->msg_name) {
1170 msg.msg_namelen = tswap32(msgp->msg_namelen);
1171 msg.msg_name = alloca(msg.msg_namelen);
1172 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1173 msg.msg_namelen);
1174 } else {
1175 msg.msg_name = NULL;
1176 msg.msg_namelen = 0;
1177 }
1178 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1179 msg.msg_control = alloca(msg.msg_controllen);
1180 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001181
bellard3532fa72006-06-24 15:06:03 +00001182 count = tswapl(msgp->msg_iovlen);
1183 vec = alloca(count * sizeof(struct iovec));
1184 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001185 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001186 msg.msg_iovlen = count;
1187 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001188
bellard3532fa72006-06-24 15:06:03 +00001189 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001190 ret = target_to_host_cmsg(&msg, msgp);
1191 if (ret == 0)
1192 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001193 } else {
1194 ret = get_errno(recvmsg(fd, &msg, flags));
1195 if (!is_error(ret))
bellard5a4a8982007-11-11 17:39:18 +00001196 ret = host_to_target_cmsg(msgp, &msg);
bellard3532fa72006-06-24 15:06:03 +00001197 }
1198 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001199 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001200 return ret;
1201}
1202
ths0da46a62007-10-20 20:23:07 +00001203/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001204static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001205 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001206{
bellard2f619692007-11-16 10:46:05 +00001207 socklen_t addrlen;
1208 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001209 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001210
bellard2f619692007-11-16 10:46:05 +00001211 if (get_user_u32(addrlen, target_addrlen_addr))
1212 return -TARGET_EFAULT;
1213
1214 addr = alloca(addrlen);
1215
pbrook1be9e1d2006-11-19 15:26:04 +00001216 ret = get_errno(accept(fd, addr, &addrlen));
1217 if (!is_error(ret)) {
1218 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001219 if (put_user_u32(addrlen, target_addrlen_addr))
1220 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001221 }
1222 return ret;
1223}
1224
ths0da46a62007-10-20 20:23:07 +00001225/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001226static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001227 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001228{
bellard2f619692007-11-16 10:46:05 +00001229 socklen_t addrlen;
1230 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001231 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001232
bellard2f619692007-11-16 10:46:05 +00001233 if (get_user_u32(addrlen, target_addrlen_addr))
1234 return -TARGET_EFAULT;
1235
1236 addr = alloca(addrlen);
1237
pbrook1be9e1d2006-11-19 15:26:04 +00001238 ret = get_errno(getpeername(fd, addr, &addrlen));
1239 if (!is_error(ret)) {
1240 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001241 if (put_user_u32(addrlen, target_addrlen_addr))
1242 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001243 }
1244 return ret;
1245}
1246
ths0da46a62007-10-20 20:23:07 +00001247/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001248static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001249 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001250{
bellard2f619692007-11-16 10:46:05 +00001251 socklen_t addrlen;
1252 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001253 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001254
bellard2f619692007-11-16 10:46:05 +00001255 if (get_user_u32(addrlen, target_addrlen_addr))
1256 return -TARGET_EFAULT;
1257
1258 addr = alloca(addrlen);
1259
pbrook1be9e1d2006-11-19 15:26:04 +00001260 ret = get_errno(getsockname(fd, addr, &addrlen));
1261 if (!is_error(ret)) {
1262 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001263 if (put_user_u32(addrlen, target_addrlen_addr))
1264 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001265 }
1266 return ret;
1267}
1268
ths0da46a62007-10-20 20:23:07 +00001269/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001270static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001271 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001272{
1273 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001274 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001275
1276 ret = get_errno(socketpair(domain, type, protocol, tab));
1277 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001278 if (put_user_s32(tab[0], target_tab_addr)
1279 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1280 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001281 }
1282 return ret;
1283}
1284
ths0da46a62007-10-20 20:23:07 +00001285/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001286static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1287 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001288{
1289 void *addr;
1290 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001291 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001292
bellard579a97f2007-11-11 14:26:47 +00001293 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1294 if (!host_msg)
1295 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001296 if (target_addr) {
1297 addr = alloca(addrlen);
1298 target_to_host_sockaddr(addr, target_addr, addrlen);
1299 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1300 } else {
1301 ret = get_errno(send(fd, host_msg, len, flags));
1302 }
1303 unlock_user(host_msg, msg, 0);
1304 return ret;
1305}
1306
ths0da46a62007-10-20 20:23:07 +00001307/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001308static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1309 abi_ulong target_addr,
1310 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001311{
1312 socklen_t addrlen;
1313 void *addr;
1314 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001315 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001316
bellard579a97f2007-11-11 14:26:47 +00001317 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1318 if (!host_msg)
1319 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001320 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001321 if (get_user_u32(addrlen, target_addrlen)) {
1322 ret = -TARGET_EFAULT;
1323 goto fail;
1324 }
pbrook1be9e1d2006-11-19 15:26:04 +00001325 addr = alloca(addrlen);
1326 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1327 } else {
1328 addr = NULL; /* To keep compiler quiet. */
1329 ret = get_errno(recv(fd, host_msg, len, flags));
1330 }
1331 if (!is_error(ret)) {
1332 if (target_addr) {
1333 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001334 if (put_user_u32(addrlen, target_addrlen)) {
1335 ret = -TARGET_EFAULT;
1336 goto fail;
1337 }
pbrook1be9e1d2006-11-19 15:26:04 +00001338 }
1339 unlock_user(host_msg, msg, len);
1340 } else {
bellard2f619692007-11-16 10:46:05 +00001341fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001342 unlock_user(host_msg, msg, 0);
1343 }
1344 return ret;
1345}
1346
j_mayer32407102007-09-26 23:01:49 +00001347#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001348/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001349static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001350{
blueswir1992f48a2007-10-14 16:27:31 +00001351 abi_long ret;
1352 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001353
1354 switch(num) {
1355 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001356 {
bellard2f619692007-11-16 10:46:05 +00001357 int domain, type, protocol;
1358
1359 if (get_user_s32(domain, vptr)
1360 || get_user_s32(type, vptr + n)
1361 || get_user_s32(protocol, vptr + 2 * n))
1362 return -TARGET_EFAULT;
1363
bellard3532fa72006-06-24 15:06:03 +00001364 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001365 }
bellard31e31b82003-02-18 22:55:36 +00001366 break;
1367 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001368 {
bellard2f619692007-11-16 10:46:05 +00001369 int sockfd;
1370 abi_ulong target_addr;
1371 socklen_t addrlen;
1372
1373 if (get_user_s32(sockfd, vptr)
1374 || get_user_ual(target_addr, vptr + n)
1375 || get_user_u32(addrlen, vptr + 2 * n))
1376 return -TARGET_EFAULT;
1377
bellard3532fa72006-06-24 15:06:03 +00001378 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001379 }
bellard31e31b82003-02-18 22:55:36 +00001380 break;
1381 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001382 {
bellard2f619692007-11-16 10:46:05 +00001383 int sockfd;
1384 abi_ulong target_addr;
1385 socklen_t addrlen;
1386
1387 if (get_user_s32(sockfd, vptr)
1388 || get_user_ual(target_addr, vptr + n)
1389 || get_user_u32(addrlen, vptr + 2 * n))
1390 return -TARGET_EFAULT;
1391
bellard3532fa72006-06-24 15:06:03 +00001392 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001393 }
bellard31e31b82003-02-18 22:55:36 +00001394 break;
1395 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001396 {
bellard2f619692007-11-16 10:46:05 +00001397 int sockfd, backlog;
1398
1399 if (get_user_s32(sockfd, vptr)
1400 || get_user_s32(backlog, vptr + n))
1401 return -TARGET_EFAULT;
1402
bellard7854b052003-03-29 17:22:23 +00001403 ret = get_errno(listen(sockfd, backlog));
1404 }
bellard31e31b82003-02-18 22:55:36 +00001405 break;
1406 case SOCKOP_accept:
1407 {
bellard2f619692007-11-16 10:46:05 +00001408 int sockfd;
1409 abi_ulong target_addr, target_addrlen;
1410
1411 if (get_user_s32(sockfd, vptr)
1412 || get_user_ual(target_addr, vptr + n)
1413 || get_user_u32(target_addrlen, vptr + 2 * n))
1414 return -TARGET_EFAULT;
1415
pbrook1be9e1d2006-11-19 15:26:04 +00001416 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001417 }
1418 break;
1419 case SOCKOP_getsockname:
1420 {
bellard2f619692007-11-16 10:46:05 +00001421 int sockfd;
1422 abi_ulong target_addr, target_addrlen;
1423
1424 if (get_user_s32(sockfd, vptr)
1425 || get_user_ual(target_addr, vptr + n)
1426 || get_user_u32(target_addrlen, vptr + 2 * n))
1427 return -TARGET_EFAULT;
1428
pbrook1be9e1d2006-11-19 15:26:04 +00001429 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001430 }
1431 break;
1432 case SOCKOP_getpeername:
1433 {
bellard2f619692007-11-16 10:46:05 +00001434 int sockfd;
1435 abi_ulong target_addr, target_addrlen;
1436
1437 if (get_user_s32(sockfd, vptr)
1438 || get_user_ual(target_addr, vptr + n)
1439 || get_user_u32(target_addrlen, vptr + 2 * n))
1440 return -TARGET_EFAULT;
1441
pbrook1be9e1d2006-11-19 15:26:04 +00001442 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001443 }
1444 break;
1445 case SOCKOP_socketpair:
1446 {
bellard2f619692007-11-16 10:46:05 +00001447 int domain, type, protocol;
1448 abi_ulong tab;
1449
1450 if (get_user_s32(domain, vptr)
1451 || get_user_s32(type, vptr + n)
1452 || get_user_s32(protocol, vptr + 2 * n)
1453 || get_user_ual(tab, vptr + 3 * n))
1454 return -TARGET_EFAULT;
1455
pbrook1be9e1d2006-11-19 15:26:04 +00001456 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001457 }
1458 break;
1459 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001460 {
bellard2f619692007-11-16 10:46:05 +00001461 int sockfd;
1462 abi_ulong msg;
1463 size_t len;
1464 int flags;
1465
1466 if (get_user_s32(sockfd, vptr)
1467 || get_user_ual(msg, vptr + n)
1468 || get_user_ual(len, vptr + 2 * n)
1469 || get_user_s32(flags, vptr + 3 * n))
1470 return -TARGET_EFAULT;
1471
pbrook1be9e1d2006-11-19 15:26:04 +00001472 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001473 }
bellard31e31b82003-02-18 22:55:36 +00001474 break;
1475 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001476 {
bellard2f619692007-11-16 10:46:05 +00001477 int sockfd;
1478 abi_ulong msg;
1479 size_t len;
1480 int flags;
1481
1482 if (get_user_s32(sockfd, vptr)
1483 || get_user_ual(msg, vptr + n)
1484 || get_user_ual(len, vptr + 2 * n)
1485 || get_user_s32(flags, vptr + 3 * n))
1486 return -TARGET_EFAULT;
1487
pbrook1be9e1d2006-11-19 15:26:04 +00001488 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001489 }
bellard31e31b82003-02-18 22:55:36 +00001490 break;
1491 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001492 {
bellard2f619692007-11-16 10:46:05 +00001493 int sockfd;
1494 abi_ulong msg;
1495 size_t len;
1496 int flags;
1497 abi_ulong addr;
1498 socklen_t addrlen;
1499
1500 if (get_user_s32(sockfd, vptr)
1501 || get_user_ual(msg, vptr + n)
1502 || get_user_ual(len, vptr + 2 * n)
1503 || get_user_s32(flags, vptr + 3 * n)
1504 || get_user_ual(addr, vptr + 4 * n)
1505 || get_user_u32(addrlen, vptr + 5 * n))
1506 return -TARGET_EFAULT;
1507
pbrook1be9e1d2006-11-19 15:26:04 +00001508 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001509 }
bellard31e31b82003-02-18 22:55:36 +00001510 break;
1511 case SOCKOP_recvfrom:
1512 {
bellard2f619692007-11-16 10:46:05 +00001513 int sockfd;
1514 abi_ulong msg;
1515 size_t len;
1516 int flags;
1517 abi_ulong addr;
1518 socklen_t addrlen;
1519
1520 if (get_user_s32(sockfd, vptr)
1521 || get_user_ual(msg, vptr + n)
1522 || get_user_ual(len, vptr + 2 * n)
1523 || get_user_s32(flags, vptr + 3 * n)
1524 || get_user_ual(addr, vptr + 4 * n)
1525 || get_user_u32(addrlen, vptr + 5 * n))
1526 return -TARGET_EFAULT;
1527
pbrook1be9e1d2006-11-19 15:26:04 +00001528 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001529 }
1530 break;
1531 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001532 {
bellard2f619692007-11-16 10:46:05 +00001533 int sockfd, how;
1534
1535 if (get_user_s32(sockfd, vptr)
1536 || get_user_s32(how, vptr + n))
1537 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001538
1539 ret = get_errno(shutdown(sockfd, how));
1540 }
bellard31e31b82003-02-18 22:55:36 +00001541 break;
1542 case SOCKOP_sendmsg:
1543 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001544 {
1545 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001546 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001547 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001548
bellard2f619692007-11-16 10:46:05 +00001549 if (get_user_s32(fd, vptr)
1550 || get_user_ual(target_msg, vptr + n)
1551 || get_user_s32(flags, vptr + 2 * n))
1552 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001553
ths5fafdf22007-09-16 21:08:06 +00001554 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001555 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001556 }
1557 break;
bellard31e31b82003-02-18 22:55:36 +00001558 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001559 {
bellard2f619692007-11-16 10:46:05 +00001560 int sockfd;
1561 int level;
1562 int optname;
1563 abi_ulong optval;
1564 socklen_t optlen;
1565
1566 if (get_user_s32(sockfd, vptr)
1567 || get_user_s32(level, vptr + n)
1568 || get_user_s32(optname, vptr + 2 * n)
1569 || get_user_ual(optval, vptr + 3 * n)
1570 || get_user_u32(optlen, vptr + 4 * n))
1571 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001572
1573 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1574 }
1575 break;
bellard31e31b82003-02-18 22:55:36 +00001576 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001577 {
bellard2f619692007-11-16 10:46:05 +00001578 int sockfd;
1579 int level;
1580 int optname;
1581 abi_ulong optval;
1582 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00001583
bellard2f619692007-11-16 10:46:05 +00001584 if (get_user_s32(sockfd, vptr)
1585 || get_user_s32(level, vptr + n)
1586 || get_user_s32(optname, vptr + 2 * n)
1587 || get_user_ual(optval, vptr + 3 * n)
1588 || get_user_u32(optlen, vptr + 4 * n))
1589 return -TARGET_EFAULT;
1590
1591 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00001592 }
1593 break;
bellard31e31b82003-02-18 22:55:36 +00001594 default:
1595 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00001596 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001597 break;
1598 }
1599 return ret;
1600}
j_mayer32407102007-09-26 23:01:49 +00001601#endif
bellard31e31b82003-02-18 22:55:36 +00001602
j_mayer32407102007-09-26 23:01:49 +00001603#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001604#define N_SHM_REGIONS 32
1605
1606static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00001607 abi_ulong start;
1608 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00001609} shm_regions[N_SHM_REGIONS];
1610
ths3eb6b042007-06-03 14:26:27 +00001611struct target_ipc_perm
1612{
blueswir1992f48a2007-10-14 16:27:31 +00001613 abi_long __key;
1614 abi_ulong uid;
1615 abi_ulong gid;
1616 abi_ulong cuid;
1617 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001618 unsigned short int mode;
1619 unsigned short int __pad1;
1620 unsigned short int __seq;
1621 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001622 abi_ulong __unused1;
1623 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001624};
1625
1626struct target_semid_ds
1627{
1628 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001629 abi_ulong sem_otime;
1630 abi_ulong __unused1;
1631 abi_ulong sem_ctime;
1632 abi_ulong __unused2;
1633 abi_ulong sem_nsems;
1634 abi_ulong __unused3;
1635 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001636};
1637
bellard579a97f2007-11-11 14:26:47 +00001638static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1639 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001640{
1641 struct target_ipc_perm *target_ip;
1642 struct target_semid_ds *target_sd;
1643
bellard579a97f2007-11-11 14:26:47 +00001644 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1645 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001646 target_ip=&(target_sd->sem_perm);
1647 host_ip->__key = tswapl(target_ip->__key);
1648 host_ip->uid = tswapl(target_ip->uid);
1649 host_ip->gid = tswapl(target_ip->gid);
1650 host_ip->cuid = tswapl(target_ip->cuid);
1651 host_ip->cgid = tswapl(target_ip->cgid);
1652 host_ip->mode = tswapl(target_ip->mode);
1653 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001654 return 0;
ths3eb6b042007-06-03 14:26:27 +00001655}
1656
bellard579a97f2007-11-11 14:26:47 +00001657static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1658 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00001659{
1660 struct target_ipc_perm *target_ip;
1661 struct target_semid_ds *target_sd;
1662
bellard579a97f2007-11-11 14:26:47 +00001663 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1664 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001665 target_ip = &(target_sd->sem_perm);
1666 target_ip->__key = tswapl(host_ip->__key);
1667 target_ip->uid = tswapl(host_ip->uid);
1668 target_ip->gid = tswapl(host_ip->gid);
1669 target_ip->cuid = tswapl(host_ip->cuid);
1670 target_ip->cgid = tswapl(host_ip->cgid);
1671 target_ip->mode = tswapl(host_ip->mode);
1672 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001673 return 0;
ths3eb6b042007-06-03 14:26:27 +00001674}
1675
bellard579a97f2007-11-11 14:26:47 +00001676static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1677 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001678{
1679 struct target_semid_ds *target_sd;
1680
bellard579a97f2007-11-11 14:26:47 +00001681 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1682 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001683 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1684 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1685 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1686 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1687 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001688 return 0;
ths3eb6b042007-06-03 14:26:27 +00001689}
1690
bellard579a97f2007-11-11 14:26:47 +00001691static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1692 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00001693{
1694 struct target_semid_ds *target_sd;
1695
bellard579a97f2007-11-11 14:26:47 +00001696 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1697 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001698 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1699 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1700 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1701 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1702 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001703 return 0;
ths3eb6b042007-06-03 14:26:27 +00001704}
1705
thsfa294812007-02-02 22:05:00 +00001706union semun {
1707 int val;
ths3eb6b042007-06-03 14:26:27 +00001708 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001709 unsigned short *array;
1710};
1711
ths3eb6b042007-06-03 14:26:27 +00001712union target_semun {
1713 int val;
blueswir1992f48a2007-10-14 16:27:31 +00001714 abi_long buf;
ths3eb6b042007-06-03 14:26:27 +00001715 unsigned short int *array;
1716};
1717
bellard579a97f2007-11-11 14:26:47 +00001718static inline abi_long target_to_host_semun(int cmd,
1719 union semun *host_su,
1720 abi_ulong target_addr,
1721 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001722{
1723 union target_semun *target_su;
1724
1725 switch( cmd ) {
1726 case IPC_STAT:
1727 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001728 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1729 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001730 target_to_host_semid_ds(ds,target_su->buf);
1731 host_su->buf = ds;
1732 unlock_user_struct(target_su, target_addr, 0);
1733 break;
1734 case GETVAL:
1735 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001736 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1737 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001738 host_su->val = tswapl(target_su->val);
1739 unlock_user_struct(target_su, target_addr, 0);
1740 break;
1741 case GETALL:
1742 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001743 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1744 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001745 *host_su->array = tswap16(*target_su->array);
1746 unlock_user_struct(target_su, target_addr, 0);
1747 break;
1748 default:
1749 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1750 }
bellard579a97f2007-11-11 14:26:47 +00001751 return 0;
ths3eb6b042007-06-03 14:26:27 +00001752}
1753
bellard579a97f2007-11-11 14:26:47 +00001754static inline abi_long host_to_target_semun(int cmd,
1755 abi_ulong target_addr,
1756 union semun *host_su,
1757 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001758{
1759 union target_semun *target_su;
1760
1761 switch( cmd ) {
1762 case IPC_STAT:
1763 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001764 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1765 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001766 host_to_target_semid_ds(target_su->buf,ds);
1767 unlock_user_struct(target_su, target_addr, 1);
1768 break;
1769 case GETVAL:
1770 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001771 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1772 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001773 target_su->val = tswapl(host_su->val);
1774 unlock_user_struct(target_su, target_addr, 1);
1775 break;
1776 case GETALL:
1777 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001778 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1779 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001780 *target_su->array = tswap16(*host_su->array);
1781 unlock_user_struct(target_su, target_addr, 1);
1782 break;
1783 default:
1784 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1785 }
bellard579a97f2007-11-11 14:26:47 +00001786 return 0;
ths3eb6b042007-06-03 14:26:27 +00001787}
1788
blueswir1992f48a2007-10-14 16:27:31 +00001789static inline abi_long do_semctl(int first, int second, int third,
1790 abi_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001791{
1792 union semun arg;
1793 struct semid_ds dsarg;
1794 int cmd = third&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001795 abi_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001796
1797 switch( cmd ) {
1798 case GETVAL:
1799 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1800 ret = get_errno(semctl(first, second, cmd, arg));
1801 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1802 break;
1803 case SETVAL:
1804 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1805 ret = get_errno(semctl(first, second, cmd, arg));
1806 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1807 break;
1808 case GETALL:
1809 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1810 ret = get_errno(semctl(first, second, cmd, arg));
1811 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1812 break;
1813 case SETALL:
1814 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1815 ret = get_errno(semctl(first, second, cmd, arg));
1816 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1817 break;
1818 case IPC_STAT:
1819 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1820 ret = get_errno(semctl(first, second, cmd, arg));
1821 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1822 break;
1823 case IPC_SET:
1824 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1825 ret = get_errno(semctl(first, second, cmd, arg));
1826 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1827 break;
1828 default:
1829 ret = get_errno(semctl(first, second, cmd, arg));
1830 }
1831
1832 return ret;
1833}
1834
ths1bc012f2007-06-03 14:27:49 +00001835struct target_msqid_ds
1836{
1837 struct target_ipc_perm msg_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001838 abi_ulong msg_stime;
1839 abi_ulong __unused1;
1840 abi_ulong msg_rtime;
1841 abi_ulong __unused2;
1842 abi_ulong msg_ctime;
1843 abi_ulong __unused3;
1844 abi_ulong __msg_cbytes;
1845 abi_ulong msg_qnum;
1846 abi_ulong msg_qbytes;
1847 abi_ulong msg_lspid;
1848 abi_ulong msg_lrpid;
1849 abi_ulong __unused4;
1850 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00001851};
1852
bellard579a97f2007-11-11 14:26:47 +00001853static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1854 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001855{
1856 struct target_msqid_ds *target_md;
1857
bellard579a97f2007-11-11 14:26:47 +00001858 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1859 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001860 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1861 host_md->msg_stime = tswapl(target_md->msg_stime);
1862 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1863 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1864 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1865 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1866 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1867 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1868 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1869 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001870 return 0;
ths1bc012f2007-06-03 14:27:49 +00001871}
1872
bellard579a97f2007-11-11 14:26:47 +00001873static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1874 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00001875{
1876 struct target_msqid_ds *target_md;
1877
bellard579a97f2007-11-11 14:26:47 +00001878 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1879 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001880 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1881 target_md->msg_stime = tswapl(host_md->msg_stime);
1882 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1883 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1884 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1885 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1886 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1887 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1888 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1889 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001890 return 0;
ths1bc012f2007-06-03 14:27:49 +00001891}
1892
blueswir1992f48a2007-10-14 16:27:31 +00001893static inline abi_long do_msgctl(int first, int second, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001894{
1895 struct msqid_ds dsarg;
1896 int cmd = second&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001897 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001898 switch( cmd ) {
1899 case IPC_STAT:
1900 case IPC_SET:
1901 target_to_host_msqid_ds(&dsarg,ptr);
1902 ret = get_errno(msgctl(first, cmd, &dsarg));
1903 host_to_target_msqid_ds(ptr,&dsarg);
1904 default:
1905 ret = get_errno(msgctl(first, cmd, &dsarg));
1906 }
1907 return ret;
1908}
1909
1910struct target_msgbuf {
blueswir1992f48a2007-10-14 16:27:31 +00001911 abi_ulong mtype;
ths1bc012f2007-06-03 14:27:49 +00001912 char mtext[1];
1913};
1914
blueswir1992f48a2007-10-14 16:27:31 +00001915static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1916 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001917{
1918 struct target_msgbuf *target_mb;
1919 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001920 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001921
bellard579a97f2007-11-11 14:26:47 +00001922 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1923 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001924 host_mb = malloc(msgsz+sizeof(long));
1925 host_mb->mtype = tswapl(target_mb->mtype);
1926 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1927 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1928 free(host_mb);
1929 unlock_user_struct(target_mb, msgp, 0);
1930
1931 return ret;
1932}
1933
blueswir1992f48a2007-10-14 16:27:31 +00001934static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1935 unsigned int msgsz, int msgtype,
1936 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001937{
1938 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00001939 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00001940 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001941 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001942
bellard579a97f2007-11-11 14:26:47 +00001943 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1944 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001945 host_mb = malloc(msgsz+sizeof(long));
1946 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
bellard579a97f2007-11-11 14:26:47 +00001947 if (ret > 0) {
1948 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1949 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1950 if (!target_mtext) {
1951 ret = -TARGET_EFAULT;
1952 goto end;
1953 }
ths1bc012f2007-06-03 14:27:49 +00001954 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00001955 unlock_user(target_mtext, target_mtext_addr, ret);
1956 }
ths1bc012f2007-06-03 14:27:49 +00001957 target_mb->mtype = tswapl(host_mb->mtype);
1958 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00001959
bellard579a97f2007-11-11 14:26:47 +00001960end:
1961 if (target_mb)
1962 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00001963 return ret;
1964}
1965
pbrook53a59602006-03-25 19:31:22 +00001966/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00001967/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001968static abi_long do_ipc(unsigned int call, int first,
1969 int second, int third,
1970 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00001971{
1972 int version;
blueswir1992f48a2007-10-14 16:27:31 +00001973 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001974 struct shmid_ds shm_info;
1975 int i;
1976
1977 version = call >> 16;
1978 call &= 0xffff;
1979
1980 switch (call) {
thsfa294812007-02-02 22:05:00 +00001981 case IPCOP_semop:
bellard579a97f2007-11-11 14:26:47 +00001982 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
thsfa294812007-02-02 22:05:00 +00001983 break;
1984
1985 case IPCOP_semget:
1986 ret = get_errno(semget(first, second, third));
1987 break;
1988
1989 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001990 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001991 break;
1992
1993 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001994 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001995 ret = -TARGET_ENOSYS;
thsfa294812007-02-02 22:05:00 +00001996 break;
thsd96372e2007-02-02 22:05:44 +00001997
1998 case IPCOP_msgget:
1999 ret = get_errno(msgget(first, second));
2000 break;
2001
2002 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00002003 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00002004 break;
2005
2006 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00002007 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00002008 break;
2009
2010 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00002011 {
bellard579a97f2007-11-11 14:26:47 +00002012 /* XXX: this code is not correct */
ths1bc012f2007-06-03 14:27:49 +00002013 struct ipc_kludge
2014 {
2015 void *__unbounded msgp;
2016 long int msgtyp;
2017 };
thsd96372e2007-02-02 22:05:44 +00002018
bellard579a97f2007-11-11 14:26:47 +00002019 struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
ths1bc012f2007-06-03 14:27:49 +00002020 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00002021
ths1bc012f2007-06-03 14:27:49 +00002022 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00002023
ths1bc012f2007-06-03 14:27:49 +00002024 }
thsd96372e2007-02-02 22:05:44 +00002025 break;
2026
bellard8853f862004-02-22 14:57:26 +00002027 case IPCOP_shmat:
bellard5a4a8982007-11-11 17:39:18 +00002028 {
2029 abi_ulong raddr;
2030 void *host_addr;
2031 /* SHM_* flags are the same on all linux platforms */
2032 host_addr = shmat(first, (void *)g2h(ptr), second);
2033 if (host_addr == (void *)-1) {
2034 ret = get_errno((long)host_addr);
bellard8853f862004-02-22 14:57:26 +00002035 break;
bellard5a4a8982007-11-11 17:39:18 +00002036 }
2037 raddr = h2g((unsigned long)host_addr);
2038 /* find out the length of the shared memory segment */
2039
2040 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
2041 if (is_error(ret)) {
2042 /* can't get length, bail out */
2043 shmdt(host_addr);
2044 break;
2045 }
2046 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2047 PAGE_VALID | PAGE_READ |
2048 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2049 for (i = 0; i < N_SHM_REGIONS; ++i) {
2050 if (shm_regions[i].start == 0) {
2051 shm_regions[i].start = raddr;
2052 shm_regions[i].size = shm_info.shm_segsz;
2053 break;
2054 }
2055 }
bellard2f619692007-11-16 10:46:05 +00002056 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002057 return -TARGET_EFAULT;
2058 ret = 0;
2059 }
bellard8853f862004-02-22 14:57:26 +00002060 break;
2061 case IPCOP_shmdt:
2062 for (i = 0; i < N_SHM_REGIONS; ++i) {
2063 if (shm_regions[i].start == ptr) {
2064 shm_regions[i].start = 0;
2065 page_set_flags(ptr, shm_regions[i].size, 0);
2066 break;
2067 }
2068 }
bellard5a4a8982007-11-11 17:39:18 +00002069 ret = get_errno(shmdt((void *)g2h(ptr)));
bellard8853f862004-02-22 14:57:26 +00002070 break;
2071
2072 case IPCOP_shmget:
2073 /* IPC_* flag values are the same on all linux platforms */
2074 ret = get_errno(shmget(first, second, third));
2075 break;
2076
2077 /* IPC_* and SHM_* command values are the same on all linux platforms */
2078 case IPCOP_shmctl:
2079 switch(second) {
2080 case IPC_RMID:
2081 case SHM_LOCK:
2082 case SHM_UNLOCK:
2083 ret = get_errno(shmctl(first, second, NULL));
2084 break;
2085 default:
2086 goto unimplemented;
2087 }
2088 break;
2089 default:
2090 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00002091 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002092 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002093 break;
2094 }
2095 return ret;
2096}
j_mayer32407102007-09-26 23:01:49 +00002097#endif
bellard8853f862004-02-22 14:57:26 +00002098
bellard31e31b82003-02-18 22:55:36 +00002099/* kernel structure types definitions */
2100#define IFNAMSIZ 16
2101
2102#define STRUCT(name, list...) STRUCT_ ## name,
2103#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2104enum {
2105#include "syscall_types.h"
2106};
2107#undef STRUCT
2108#undef STRUCT_SPECIAL
2109
2110#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2111#define STRUCT_SPECIAL(name)
2112#include "syscall_types.h"
2113#undef STRUCT
2114#undef STRUCT_SPECIAL
2115
2116typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002117 unsigned int target_cmd;
2118 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002119 const char *name;
2120 int access;
bellard1a9353d2003-03-16 20:28:50 +00002121 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00002122} IOCTLEntry;
2123
2124#define IOC_R 0x0001
2125#define IOC_W 0x0002
2126#define IOC_RW (IOC_R | IOC_W)
2127
2128#define MAX_STRUCT_SIZE 4096
2129
bellard2ab83ea2003-06-15 19:56:46 +00002130IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00002131#define IOCTL(cmd, access, types...) \
2132 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2133#include "ioctls.h"
2134 { 0, 0, },
2135};
2136
pbrook53a59602006-03-25 19:31:22 +00002137/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00002138/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002139static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00002140{
2141 const IOCTLEntry *ie;
2142 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00002143 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002144 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00002145 int target_size;
2146 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00002147
2148 ie = ioctl_entries;
2149 for(;;) {
2150 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00002151 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00002152 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002153 }
2154 if (ie->target_cmd == cmd)
2155 break;
2156 ie++;
2157 }
2158 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00002159#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00002160 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00002161#endif
bellard31e31b82003-02-18 22:55:36 +00002162 switch(arg_type[0]) {
2163 case TYPE_NULL:
2164 /* no argument */
2165 ret = get_errno(ioctl(fd, ie->host_cmd));
2166 break;
2167 case TYPE_PTRVOID:
2168 case TYPE_INT:
2169 /* int argment */
2170 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2171 break;
2172 case TYPE_PTR:
2173 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00002174 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00002175 switch(ie->access) {
2176 case IOC_R:
2177 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2178 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002179 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2180 if (!argptr)
2181 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002182 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2183 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002184 }
2185 break;
2186 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00002187 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2188 if (!argptr)
2189 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002190 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2191 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002192 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2193 break;
2194 default:
2195 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00002196 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2197 if (!argptr)
2198 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002199 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2200 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002201 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2202 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002203 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2204 if (!argptr)
2205 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002206 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2207 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002208 }
2209 break;
2210 }
2211 break;
2212 default:
j_mayer32407102007-09-26 23:01:49 +00002213 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2214 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00002215 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002216 break;
2217 }
2218 return ret;
2219}
2220
2221bitmask_transtbl iflag_tbl[] = {
2222 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2223 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2224 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2225 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2226 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2227 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2228 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2229 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2230 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2231 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2232 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2233 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2234 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2235 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2236 { 0, 0, 0, 0 }
2237};
2238
2239bitmask_transtbl oflag_tbl[] = {
2240 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2241 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2242 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2243 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2244 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2245 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2246 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2247 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2248 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2249 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2250 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2251 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2252 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2253 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2254 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2255 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2256 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2257 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2258 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2259 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2260 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2261 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2262 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2263 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2264 { 0, 0, 0, 0 }
2265};
2266
2267bitmask_transtbl cflag_tbl[] = {
2268 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2269 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2270 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2271 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2272 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2273 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2274 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2275 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2276 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2277 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2278 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2279 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2280 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2281 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2282 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2283 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2284 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2285 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2286 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2287 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2288 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2289 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2290 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2291 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2292 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2293 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2294 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2295 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2296 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2297 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2298 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2299 { 0, 0, 0, 0 }
2300};
2301
2302bitmask_transtbl lflag_tbl[] = {
2303 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2304 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2305 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2306 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2307 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2308 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2309 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2310 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2311 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2312 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2313 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2314 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2315 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2316 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2317 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2318 { 0, 0, 0, 0 }
2319};
2320
2321static void target_to_host_termios (void *dst, const void *src)
2322{
2323 struct host_termios *host = dst;
2324 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00002325
ths5fafdf22007-09-16 21:08:06 +00002326 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002327 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002328 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002329 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002330 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002331 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002332 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002333 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2334 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00002335
ths5fafdf22007-09-16 21:08:06 +00002336 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2337 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00002338 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00002339 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00002340 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002341 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002342 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002343 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002344 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002345 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2346 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002347 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2348 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2349 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2350 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2351 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002352 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002353}
ths3b46e622007-09-17 08:09:54 +00002354
bellard31e31b82003-02-18 22:55:36 +00002355static void host_to_target_termios (void *dst, const void *src)
2356{
2357 struct target_termios *target = dst;
2358 const struct host_termios *host = src;
2359
ths5fafdf22007-09-16 21:08:06 +00002360 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002361 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002362 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002363 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002364 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002365 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002366 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002367 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2368 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002369
bellard31e31b82003-02-18 22:55:36 +00002370 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2371 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2372 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2373 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2374 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2375 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2376 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2377 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2378 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2379 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2380 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2381 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2382 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2383 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2384 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2385 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2386 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2387}
2388
2389StructEntry struct_termios_def = {
2390 .convert = { host_to_target_termios, target_to_host_termios },
2391 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2392 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2393};
2394
bellard5286db72003-06-05 00:57:30 +00002395static bitmask_transtbl mmap_flags_tbl[] = {
2396 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2397 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2398 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2399 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2400 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2401 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2402 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2403 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2404 { 0, 0, 0, 0 }
2405};
2406
bellardffa65c32004-01-04 23:57:22 +00002407static bitmask_transtbl fcntl_flags_tbl[] = {
2408 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2409 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2410 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2411 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2412 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2413 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2414 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2415 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2416 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2417 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2418 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2419 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2420 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002421#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002422 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002423#endif
bellardffa65c32004-01-04 23:57:22 +00002424 { 0, 0, 0, 0 }
2425};
2426
bellard2ab83ea2003-06-15 19:56:46 +00002427#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002428
2429/* NOTE: there is really one LDT for all the threads */
2430uint8_t *ldt_table;
2431
bellard03acab62007-11-11 14:57:14 +00002432static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002433{
2434 int size;
pbrook53a59602006-03-25 19:31:22 +00002435 void *p;
bellard6dbad632003-03-16 18:05:05 +00002436
2437 if (!ldt_table)
2438 return 0;
2439 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2440 if (size > bytecount)
2441 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00002442 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2443 if (!p)
bellard03acab62007-11-11 14:57:14 +00002444 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00002445 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00002446 memcpy(p, ldt_table, size);
2447 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002448 return size;
2449}
2450
2451/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00002452static abi_long write_ldt(CPUX86State *env,
2453 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002454{
2455 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002456 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002457 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00002458 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00002459 uint32_t *lp, entry_1, entry_2;
2460
2461 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00002462 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00002463 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00002464 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002465 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2466 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2467 ldt_info.limit = tswap32(target_ldt_info->limit);
2468 ldt_info.flags = tswap32(target_ldt_info->flags);
2469 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002470
bellard6dbad632003-03-16 18:05:05 +00002471 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00002472 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002473 seg_32bit = ldt_info.flags & 1;
2474 contents = (ldt_info.flags >> 1) & 3;
2475 read_exec_only = (ldt_info.flags >> 3) & 1;
2476 limit_in_pages = (ldt_info.flags >> 4) & 1;
2477 seg_not_present = (ldt_info.flags >> 5) & 1;
2478 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00002479#ifdef TARGET_ABI32
2480 lm = 0;
2481#else
2482 lm = (ldt_info.flags >> 7) & 1;
2483#endif
bellard6dbad632003-03-16 18:05:05 +00002484 if (contents == 3) {
2485 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00002486 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002487 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00002488 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002489 }
2490 /* allocate the LDT */
2491 if (!ldt_table) {
2492 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2493 if (!ldt_table)
bellard03acab62007-11-11 14:57:14 +00002494 return -TARGET_ENOMEM;
bellard6dbad632003-03-16 18:05:05 +00002495 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellardeeeac3f2007-11-11 19:34:32 +00002496 env->ldt.base = h2g((unsigned long)ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002497 env->ldt.limit = 0xffff;
2498 }
2499
2500 /* NOTE: same code as Linux kernel */
2501 /* Allow LDTs to be cleared by the user. */
2502 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2503 if (oldmode ||
2504 (contents == 0 &&
2505 read_exec_only == 1 &&
2506 seg_32bit == 0 &&
2507 limit_in_pages == 0 &&
2508 seg_not_present == 1 &&
2509 useable == 0 )) {
2510 entry_1 = 0;
2511 entry_2 = 0;
2512 goto install;
2513 }
2514 }
ths3b46e622007-09-17 08:09:54 +00002515
bellard6dbad632003-03-16 18:05:05 +00002516 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2517 (ldt_info.limit & 0x0ffff);
2518 entry_2 = (ldt_info.base_addr & 0xff000000) |
2519 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2520 (ldt_info.limit & 0xf0000) |
2521 ((read_exec_only ^ 1) << 9) |
2522 (contents << 10) |
2523 ((seg_not_present ^ 1) << 15) |
2524 (seg_32bit << 22) |
2525 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00002526 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00002527 0x7000;
2528 if (!oldmode)
2529 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002530
bellard6dbad632003-03-16 18:05:05 +00002531 /* Install the new entry ... */
2532install:
2533 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2534 lp[0] = tswap32(entry_1);
2535 lp[1] = tswap32(entry_2);
2536 return 0;
2537}
2538
2539/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00002540static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2541 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002542{
bellard03acab62007-11-11 14:57:14 +00002543 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00002544
bellard6dbad632003-03-16 18:05:05 +00002545 switch (func) {
2546 case 0:
2547 ret = read_ldt(ptr, bytecount);
2548 break;
2549 case 1:
2550 ret = write_ldt(env, ptr, bytecount, 1);
2551 break;
2552 case 0x11:
2553 ret = write_ldt(env, ptr, bytecount, 0);
2554 break;
bellard03acab62007-11-11 14:57:14 +00002555 default:
2556 ret = -TARGET_ENOSYS;
2557 break;
bellard6dbad632003-03-16 18:05:05 +00002558 }
2559 return ret;
2560}
bellard1b6b0292003-03-22 17:31:38 +00002561
blueswir14583f582008-08-24 10:35:55 +00002562#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00002563static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00002564{
2565 uint64_t *gdt_table = g2h(env->gdt.base);
2566 struct target_modify_ldt_ldt_s ldt_info;
2567 struct target_modify_ldt_ldt_s *target_ldt_info;
2568 int seg_32bit, contents, read_exec_only, limit_in_pages;
2569 int seg_not_present, useable, lm;
2570 uint32_t *lp, entry_1, entry_2;
2571 int i;
2572
2573 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2574 if (!target_ldt_info)
2575 return -TARGET_EFAULT;
2576 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2577 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2578 ldt_info.limit = tswap32(target_ldt_info->limit);
2579 ldt_info.flags = tswap32(target_ldt_info->flags);
2580 if (ldt_info.entry_number == -1) {
2581 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
2582 if (gdt_table[i] == 0) {
2583 ldt_info.entry_number = i;
2584 target_ldt_info->entry_number = tswap32(i);
2585 break;
2586 }
2587 }
2588 }
2589 unlock_user_struct(target_ldt_info, ptr, 1);
2590
2591 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
2592 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
2593 return -TARGET_EINVAL;
2594 seg_32bit = ldt_info.flags & 1;
2595 contents = (ldt_info.flags >> 1) & 3;
2596 read_exec_only = (ldt_info.flags >> 3) & 1;
2597 limit_in_pages = (ldt_info.flags >> 4) & 1;
2598 seg_not_present = (ldt_info.flags >> 5) & 1;
2599 useable = (ldt_info.flags >> 6) & 1;
2600#ifdef TARGET_ABI32
2601 lm = 0;
2602#else
2603 lm = (ldt_info.flags >> 7) & 1;
2604#endif
2605
2606 if (contents == 3) {
2607 if (seg_not_present == 0)
2608 return -TARGET_EINVAL;
2609 }
2610
2611 /* NOTE: same code as Linux kernel */
2612 /* Allow LDTs to be cleared by the user. */
2613 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2614 if ((contents == 0 &&
2615 read_exec_only == 1 &&
2616 seg_32bit == 0 &&
2617 limit_in_pages == 0 &&
2618 seg_not_present == 1 &&
2619 useable == 0 )) {
2620 entry_1 = 0;
2621 entry_2 = 0;
2622 goto install;
2623 }
2624 }
2625
2626 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2627 (ldt_info.limit & 0x0ffff);
2628 entry_2 = (ldt_info.base_addr & 0xff000000) |
2629 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2630 (ldt_info.limit & 0xf0000) |
2631 ((read_exec_only ^ 1) << 9) |
2632 (contents << 10) |
2633 ((seg_not_present ^ 1) << 15) |
2634 (seg_32bit << 22) |
2635 (limit_in_pages << 23) |
2636 (useable << 20) |
2637 (lm << 21) |
2638 0x7000;
2639
2640 /* Install the new entry ... */
2641install:
2642 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
2643 lp[0] = tswap32(entry_1);
2644 lp[1] = tswap32(entry_2);
2645 return 0;
2646}
2647
blueswir18fcd3692008-08-17 20:26:25 +00002648static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00002649{
2650 struct target_modify_ldt_ldt_s *target_ldt_info;
2651 uint64_t *gdt_table = g2h(env->gdt.base);
2652 uint32_t base_addr, limit, flags;
2653 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
2654 int seg_not_present, useable, lm;
2655 uint32_t *lp, entry_1, entry_2;
2656
2657 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2658 if (!target_ldt_info)
2659 return -TARGET_EFAULT;
2660 idx = tswap32(target_ldt_info->entry_number);
2661 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
2662 idx > TARGET_GDT_ENTRY_TLS_MAX) {
2663 unlock_user_struct(target_ldt_info, ptr, 1);
2664 return -TARGET_EINVAL;
2665 }
2666 lp = (uint32_t *)(gdt_table + idx);
2667 entry_1 = tswap32(lp[0]);
2668 entry_2 = tswap32(lp[1]);
2669
2670 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
2671 contents = (entry_2 >> 10) & 3;
2672 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
2673 seg_32bit = (entry_2 >> 22) & 1;
2674 limit_in_pages = (entry_2 >> 23) & 1;
2675 useable = (entry_2 >> 20) & 1;
2676#ifdef TARGET_ABI32
2677 lm = 0;
2678#else
2679 lm = (entry_2 >> 21) & 1;
2680#endif
2681 flags = (seg_32bit << 0) | (contents << 1) |
2682 (read_exec_only << 3) | (limit_in_pages << 4) |
2683 (seg_not_present << 5) | (useable << 6) | (lm << 7);
2684 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
2685 base_addr = (entry_1 >> 16) |
2686 (entry_2 & 0xff000000) |
2687 ((entry_2 & 0xff) << 16);
2688 target_ldt_info->base_addr = tswapl(base_addr);
2689 target_ldt_info->limit = tswap32(limit);
2690 target_ldt_info->flags = tswap32(flags);
2691 unlock_user_struct(target_ldt_info, ptr, 1);
2692 return 0;
2693}
blueswir14583f582008-08-24 10:35:55 +00002694#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00002695
bellardd2fd1af2007-11-14 18:08:56 +00002696#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00002697static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00002698{
2699 abi_long ret;
2700 abi_ulong val;
2701 int idx;
2702
2703 switch(code) {
2704 case TARGET_ARCH_SET_GS:
2705 case TARGET_ARCH_SET_FS:
2706 if (code == TARGET_ARCH_SET_GS)
2707 idx = R_GS;
2708 else
2709 idx = R_FS;
2710 cpu_x86_load_seg(env, idx, 0);
2711 env->segs[idx].base = addr;
2712 break;
2713 case TARGET_ARCH_GET_GS:
2714 case TARGET_ARCH_GET_FS:
2715 if (code == TARGET_ARCH_GET_GS)
2716 idx = R_GS;
2717 else
2718 idx = R_FS;
2719 val = env->segs[idx].base;
2720 if (put_user(val, addr, abi_ulong))
2721 return -TARGET_EFAULT;
2722 break;
2723 default:
2724 ret = -TARGET_EINVAL;
2725 break;
2726 }
2727 return 0;
2728}
2729#endif
2730
bellard2ab83ea2003-06-15 19:56:46 +00002731#endif /* defined(TARGET_I386) */
2732
pbrookd865bab2008-06-07 22:12:17 +00002733#if defined(USE_NPTL)
2734
2735#define NEW_STACK_SIZE PTHREAD_STACK_MIN
2736
2737static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
2738typedef struct {
2739 CPUState *env;
2740 pthread_mutex_t mutex;
2741 pthread_cond_t cond;
2742 pthread_t thread;
2743 uint32_t tid;
2744 abi_ulong child_tidptr;
2745 abi_ulong parent_tidptr;
2746 sigset_t sigmask;
2747} new_thread_info;
2748
2749static void *clone_func(void *arg)
2750{
2751 new_thread_info *info = arg;
2752 CPUState *env;
2753
2754 env = info->env;
2755 thread_env = env;
2756 info->tid = gettid();
2757 if (info->child_tidptr)
2758 put_user_u32(info->tid, info->child_tidptr);
2759 if (info->parent_tidptr)
2760 put_user_u32(info->tid, info->parent_tidptr);
2761 /* Enable signals. */
2762 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
2763 /* Signal to the parent that we're ready. */
2764 pthread_mutex_lock(&info->mutex);
2765 pthread_cond_broadcast(&info->cond);
2766 pthread_mutex_unlock(&info->mutex);
2767 /* Wait until the parent has finshed initializing the tls state. */
2768 pthread_mutex_lock(&clone_lock);
2769 pthread_mutex_unlock(&clone_lock);
2770 cpu_loop(env);
2771 /* never exits */
2772 return NULL;
2773}
2774#else
bellard1b6b0292003-03-22 17:31:38 +00002775/* this stack is the equivalent of the kernel stack associated with a
2776 thread/process */
2777#define NEW_STACK_SIZE 8192
2778
2779static int clone_func(void *arg)
2780{
bellard2ab83ea2003-06-15 19:56:46 +00002781 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002782 cpu_loop(env);
2783 /* never exits */
2784 return 0;
2785}
pbrookd865bab2008-06-07 22:12:17 +00002786#endif
bellard1b6b0292003-03-22 17:31:38 +00002787
ths0da46a62007-10-20 20:23:07 +00002788/* do_fork() Must return host values and target errnos (unlike most
2789 do_*() functions). */
pbrookd865bab2008-06-07 22:12:17 +00002790static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
2791 abi_ulong parent_tidptr, target_ulong newtls,
2792 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00002793{
2794 int ret;
bellard5cd43932003-03-29 16:54:36 +00002795 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002796 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002797 CPUState *new_env;
pbrookd865bab2008-06-07 22:12:17 +00002798#if defined(USE_NPTL)
2799 unsigned int nptl_flags;
2800 sigset_t sigmask;
2801#endif
ths3b46e622007-09-17 08:09:54 +00002802
balrog436d1242008-09-21 02:39:45 +00002803 /* Emulate vfork() with fork() */
2804 if (flags & CLONE_VFORK)
2805 flags &= ~(CLONE_VFORK | CLONE_VM);
2806
bellard1b6b0292003-03-22 17:31:38 +00002807 if (flags & CLONE_VM) {
pbrookbd0c5662008-05-29 14:34:11 +00002808#if defined(USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00002809 new_thread_info info;
2810 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00002811#endif
pbrookc3a92832008-06-09 14:02:50 +00002812 ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
pbrook624f7972008-05-31 16:11:38 +00002813 init_task_state(ts);
bellard5cd43932003-03-29 16:54:36 +00002814 new_stack = ts->stack;
bellard1b6b0292003-03-22 17:31:38 +00002815 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002816 new_env = cpu_copy(env);
pbrook6e68e072008-05-30 17:22:15 +00002817 /* Init regs that differ from the parent. */
2818 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00002819 new_env->opaque = ts;
pbrookd865bab2008-06-07 22:12:17 +00002820#if defined(USE_NPTL)
2821 nptl_flags = flags;
2822 flags &= ~CLONE_NPTL_FLAGS2;
2823
2824 /* TODO: Implement CLONE_CHILD_CLEARTID. */
2825 if (nptl_flags & CLONE_SETTLS)
2826 cpu_set_tls (new_env, newtls);
2827
2828 /* Grab a mutex so that thread setup appears atomic. */
2829 pthread_mutex_lock(&clone_lock);
2830
2831 memset(&info, 0, sizeof(info));
2832 pthread_mutex_init(&info.mutex, NULL);
2833 pthread_mutex_lock(&info.mutex);
2834 pthread_cond_init(&info.cond, NULL);
2835 info.env = new_env;
2836 if (nptl_flags & CLONE_CHILD_SETTID)
2837 info.child_tidptr = child_tidptr;
2838 if (nptl_flags & CLONE_PARENT_SETTID)
2839 info.parent_tidptr = parent_tidptr;
2840
2841 ret = pthread_attr_init(&attr);
2842 ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
2843 /* It is not safe to deliver signals until the child has finished
2844 initializing, so temporarily block all signals. */
2845 sigfillset(&sigmask);
2846 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
2847
2848 ret = pthread_create(&info.thread, &attr, clone_func, &info);
2849
2850 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
2851 pthread_attr_destroy(&attr);
2852 if (ret == 0) {
2853 /* Wait for the child to initialize. */
2854 pthread_cond_wait(&info.cond, &info.mutex);
2855 ret = info.tid;
2856 if (flags & CLONE_PARENT_SETTID)
2857 put_user_u32(ret, parent_tidptr);
2858 } else {
2859 ret = -1;
2860 }
2861 pthread_mutex_unlock(&info.mutex);
2862 pthread_cond_destroy(&info.cond);
2863 pthread_mutex_destroy(&info.mutex);
2864 pthread_mutex_unlock(&clone_lock);
2865#else
2866 if (flags & CLONE_NPTL_FLAGS2)
2867 return -EINVAL;
2868 /* This is probably going to die very quickly, but do it anyway. */
bellard27725c12003-04-29 21:08:18 +00002869#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002870 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002871#else
2872 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2873#endif
pbrookd865bab2008-06-07 22:12:17 +00002874#endif
bellard1b6b0292003-03-22 17:31:38 +00002875 } else {
2876 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00002877 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00002878 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00002879 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00002880 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00002881#if defined(USE_NPTL)
2882 /* There is a race condition here. The parent process could
2883 theoretically read the TID in the child process before the child
2884 tid is set. This would require using either ptrace
2885 (not implemented) or having *_tidptr to point at a shared memory
2886 mapping. We can't repeat the spinlock hack used above because
2887 the child process gets its own copy of the lock. */
2888 if (ret == 0) {
2889 cpu_clone_regs(env, newsp);
2890 fork_end(1);
2891 /* Child Process. */
2892 if (flags & CLONE_CHILD_SETTID)
2893 put_user_u32(gettid(), child_tidptr);
2894 if (flags & CLONE_PARENT_SETTID)
2895 put_user_u32(gettid(), parent_tidptr);
2896 ts = (TaskState *)env->opaque;
2897 if (flags & CLONE_SETTLS)
2898 cpu_set_tls (env, newtls);
2899 /* TODO: Implement CLONE_CHILD_CLEARTID. */
2900 } else {
2901 fork_end(0);
2902 }
2903#else
pbrook6e68e072008-05-30 17:22:15 +00002904 if (ret == 0) {
2905 cpu_clone_regs(env, newsp);
2906 }
pbrookd865bab2008-06-07 22:12:17 +00002907#endif
bellard1b6b0292003-03-22 17:31:38 +00002908 }
2909 return ret;
2910}
2911
blueswir1992f48a2007-10-14 16:27:31 +00002912static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002913{
2914 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002915 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002916 struct flock64 fl64;
2917 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00002918 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00002919
bellard7775e9e2003-05-14 22:46:48 +00002920 switch(cmd) {
2921 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00002922 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2923 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002924 fl.l_type = tswap16(target_fl->l_type);
2925 fl.l_whence = tswap16(target_fl->l_whence);
2926 fl.l_start = tswapl(target_fl->l_start);
2927 fl.l_len = tswapl(target_fl->l_len);
2928 fl.l_pid = tswapl(target_fl->l_pid);
2929 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002930 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002931 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002932 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2933 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002934 target_fl->l_type = tswap16(fl.l_type);
2935 target_fl->l_whence = tswap16(fl.l_whence);
2936 target_fl->l_start = tswapl(fl.l_start);
2937 target_fl->l_len = tswapl(fl.l_len);
2938 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002939 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002940 }
2941 break;
ths3b46e622007-09-17 08:09:54 +00002942
bellard7775e9e2003-05-14 22:46:48 +00002943 case TARGET_F_SETLK:
2944 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00002945 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2946 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002947 fl.l_type = tswap16(target_fl->l_type);
2948 fl.l_whence = tswap16(target_fl->l_whence);
2949 fl.l_start = tswapl(target_fl->l_start);
2950 fl.l_len = tswapl(target_fl->l_len);
2951 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002952 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002953 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002954 break;
ths3b46e622007-09-17 08:09:54 +00002955
bellard7775e9e2003-05-14 22:46:48 +00002956 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00002957 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2958 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002959 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2960 fl64.l_whence = tswap16(target_fl64->l_whence);
2961 fl64.l_start = tswapl(target_fl64->l_start);
2962 fl64.l_len = tswapl(target_fl64->l_len);
2963 fl64.l_pid = tswap16(target_fl64->l_pid);
2964 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002965 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
ths43f238d2007-01-05 20:55:49 +00002966 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002967 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2968 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002969 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2970 target_fl64->l_whence = tswap16(fl64.l_whence);
2971 target_fl64->l_start = tswapl(fl64.l_start);
2972 target_fl64->l_len = tswapl(fl64.l_len);
2973 target_fl64->l_pid = tswapl(fl64.l_pid);
2974 unlock_user_struct(target_fl64, arg, 1);
2975 }
bellard9ee1fa22007-11-11 15:11:19 +00002976 break;
bellard7775e9e2003-05-14 22:46:48 +00002977 case TARGET_F_SETLK64:
2978 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00002979 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2980 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002981 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2982 fl64.l_whence = tswap16(target_fl64->l_whence);
2983 fl64.l_start = tswapl(target_fl64->l_start);
2984 fl64.l_len = tswapl(target_fl64->l_len);
2985 fl64.l_pid = tswap16(target_fl64->l_pid);
2986 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002987 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00002988 break;
2989
bellardffa65c32004-01-04 23:57:22 +00002990 case F_GETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002991 ret = get_errno(fcntl(fd, cmd, arg));
2992 if (ret >= 0) {
2993 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2994 }
bellardffa65c32004-01-04 23:57:22 +00002995 break;
2996
2997 case F_SETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002998 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
bellardffa65c32004-01-04 23:57:22 +00002999 break;
3000
bellard7775e9e2003-05-14 22:46:48 +00003001 default:
bellard9ee1fa22007-11-11 15:11:19 +00003002 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00003003 break;
3004 }
3005 return ret;
3006}
3007
bellard67867302003-11-23 17:05:30 +00003008#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00003009
bellard67867302003-11-23 17:05:30 +00003010static inline int high2lowuid(int uid)
3011{
3012 if (uid > 65535)
3013 return 65534;
3014 else
3015 return uid;
3016}
3017
3018static inline int high2lowgid(int gid)
3019{
3020 if (gid > 65535)
3021 return 65534;
3022 else
3023 return gid;
3024}
3025
3026static inline int low2highuid(int uid)
3027{
3028 if ((int16_t)uid == -1)
3029 return -1;
3030 else
3031 return uid;
3032}
3033
3034static inline int low2highgid(int gid)
3035{
3036 if ((int16_t)gid == -1)
3037 return -1;
3038 else
3039 return gid;
3040}
3041
3042#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00003043
bellard31e31b82003-02-18 22:55:36 +00003044void syscall_init(void)
3045{
bellard2ab83ea2003-06-15 19:56:46 +00003046 IOCTLEntry *ie;
3047 const argtype *arg_type;
3048 int size;
thsb92c47c2007-11-01 00:07:38 +00003049 int i;
bellard2ab83ea2003-06-15 19:56:46 +00003050
ths5fafdf22007-09-16 21:08:06 +00003051#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
3052#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00003053#include "syscall_types.h"
3054#undef STRUCT
3055#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00003056
3057 /* we patch the ioctl size if necessary. We rely on the fact that
3058 no ioctl has all the bits at '1' in the size field */
3059 ie = ioctl_entries;
3060 while (ie->target_cmd != 0) {
3061 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3062 TARGET_IOC_SIZEMASK) {
3063 arg_type = ie->arg_type;
3064 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00003065 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00003066 ie->target_cmd);
3067 exit(1);
3068 }
3069 arg_type++;
3070 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00003071 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00003072 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3073 (size << TARGET_IOC_SIZESHIFT);
3074 }
thsb92c47c2007-11-01 00:07:38 +00003075
3076 /* Build target_to_host_errno_table[] table from
3077 * host_to_target_errno_table[]. */
3078 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3079 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3080
bellard2ab83ea2003-06-15 19:56:46 +00003081 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00003082#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3083 (defined(__x86_64__) && defined(TARGET_X86_64))
3084 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3085 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3086 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00003087 }
3088#endif
3089 ie++;
3090 }
bellard31e31b82003-02-18 22:55:36 +00003091}
bellardc573ff62004-01-04 15:51:36 +00003092
blueswir1992f48a2007-10-14 16:27:31 +00003093#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00003094static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3095{
thsaf325d32008-06-10 15:29:15 +00003096#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00003097 return ((uint64_t)word0 << 32) | word1;
3098#else
3099 return ((uint64_t)word1 << 32) | word0;
3100#endif
3101}
blueswir1992f48a2007-10-14 16:27:31 +00003102#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00003103static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3104{
3105 return word0;
3106}
blueswir1992f48a2007-10-14 16:27:31 +00003107#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00003108
3109#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00003110static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3111 abi_long arg2,
3112 abi_long arg3,
3113 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003114{
3115#ifdef TARGET_ARM
3116 if (((CPUARMState *)cpu_env)->eabi)
3117 {
3118 arg2 = arg3;
3119 arg3 = arg4;
3120 }
3121#endif
3122 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3123}
3124#endif
3125
3126#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00003127static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3128 abi_long arg2,
3129 abi_long arg3,
3130 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003131{
3132#ifdef TARGET_ARM
3133 if (((CPUARMState *)cpu_env)->eabi)
3134 {
3135 arg2 = arg3;
3136 arg3 = arg4;
3137 }
3138#endif
3139 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3140}
3141#endif
3142
bellard579a97f2007-11-11 14:26:47 +00003143static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3144 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00003145{
3146 struct target_timespec *target_ts;
3147
bellard579a97f2007-11-11 14:26:47 +00003148 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3149 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003150 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3151 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3152 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00003153 return 0;
pbrook53a59602006-03-25 19:31:22 +00003154}
3155
bellard579a97f2007-11-11 14:26:47 +00003156static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3157 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00003158{
3159 struct target_timespec *target_ts;
3160
bellard579a97f2007-11-11 14:26:47 +00003161 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3162 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003163 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3164 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3165 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00003166 return 0;
pbrook53a59602006-03-25 19:31:22 +00003167}
3168
balrog6a24a772008-09-20 02:23:36 +00003169#ifdef TARGET_NR_stat64
3170static inline abi_long host_to_target_stat64(void *cpu_env,
3171 abi_ulong target_addr,
3172 struct stat *host_st)
3173{
3174#ifdef TARGET_ARM
3175 if (((CPUARMState *)cpu_env)->eabi) {
3176 struct target_eabi_stat64 *target_st;
3177
3178 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3179 return -TARGET_EFAULT;
3180 memset(target_st, 0, sizeof(struct target_eabi_stat64));
3181 __put_user(host_st->st_dev, &target_st->st_dev);
3182 __put_user(host_st->st_ino, &target_st->st_ino);
3183#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3184 __put_user(host_st->st_ino, &target_st->__st_ino);
3185#endif
3186 __put_user(host_st->st_mode, &target_st->st_mode);
3187 __put_user(host_st->st_nlink, &target_st->st_nlink);
3188 __put_user(host_st->st_uid, &target_st->st_uid);
3189 __put_user(host_st->st_gid, &target_st->st_gid);
3190 __put_user(host_st->st_rdev, &target_st->st_rdev);
3191 __put_user(host_st->st_size, &target_st->st_size);
3192 __put_user(host_st->st_blksize, &target_st->st_blksize);
3193 __put_user(host_st->st_blocks, &target_st->st_blocks);
3194 __put_user(host_st->st_atime, &target_st->target_st_atime);
3195 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3196 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3197 unlock_user_struct(target_st, target_addr, 1);
3198 } else
3199#endif
3200 {
3201 struct target_stat64 *target_st;
3202
3203 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3204 return -TARGET_EFAULT;
3205 memset(target_st, 0, sizeof(struct target_stat64));
3206 __put_user(host_st->st_dev, &target_st->st_dev);
3207 __put_user(host_st->st_ino, &target_st->st_ino);
3208#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3209 __put_user(host_st->st_ino, &target_st->__st_ino);
3210#endif
3211 __put_user(host_st->st_mode, &target_st->st_mode);
3212 __put_user(host_st->st_nlink, &target_st->st_nlink);
3213 __put_user(host_st->st_uid, &target_st->st_uid);
3214 __put_user(host_st->st_gid, &target_st->st_gid);
3215 __put_user(host_st->st_rdev, &target_st->st_rdev);
3216 /* XXX: better use of kernel struct */
3217 __put_user(host_st->st_size, &target_st->st_size);
3218 __put_user(host_st->st_blksize, &target_st->st_blksize);
3219 __put_user(host_st->st_blocks, &target_st->st_blocks);
3220 __put_user(host_st->st_atime, &target_st->target_st_atime);
3221 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3222 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3223 unlock_user_struct(target_st, target_addr, 1);
3224 }
3225
3226 return 0;
3227}
3228#endif
3229
pbrookbd0c5662008-05-29 14:34:11 +00003230#if defined(USE_NPTL)
3231/* ??? Using host futex calls even when target atomic operations
3232 are not really atomic probably breaks things. However implementing
3233 futexes locally would make futexes shared between multiple processes
3234 tricky. However they're probably useless because guest atomic
3235 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00003236static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
3237 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00003238{
3239 struct timespec ts, *pts;
3240
3241 /* ??? We assume FUTEX_* constants are the same on both host
3242 and target. */
3243 switch (op) {
3244 case FUTEX_WAIT:
3245 if (timeout) {
3246 pts = &ts;
3247 target_to_host_timespec(pts, timeout);
3248 } else {
3249 pts = NULL;
3250 }
3251 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAIT, tswap32(val),
3252 pts, NULL, 0));
3253 case FUTEX_WAKE:
3254 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAKE, val, NULL, NULL, 0));
3255 case FUTEX_FD:
3256 return get_errno(sys_futex(g2h(uaddr), FUTEX_FD, val, NULL, NULL, 0));
3257 case FUTEX_REQUEUE:
3258 return get_errno(sys_futex(g2h(uaddr), FUTEX_REQUEUE, val,
3259 NULL, g2h(uaddr2), 0));
3260 case FUTEX_CMP_REQUEUE:
3261 return get_errno(sys_futex(g2h(uaddr), FUTEX_CMP_REQUEUE, val,
3262 NULL, g2h(uaddr2), tswap32(val3)));
3263 default:
3264 return -TARGET_ENOSYS;
3265 }
3266}
3267#endif
3268
pbrooka745ec62008-05-06 15:36:17 +00003269int get_osversion(void)
3270{
3271 static int osversion;
3272 struct new_utsname buf;
3273 const char *s;
3274 int i, n, tmp;
3275 if (osversion)
3276 return osversion;
3277 if (qemu_uname_release && *qemu_uname_release) {
3278 s = qemu_uname_release;
3279 } else {
3280 if (sys_uname(&buf))
3281 return 0;
3282 s = buf.release;
3283 }
3284 tmp = 0;
3285 for (i = 0; i < 3; i++) {
3286 n = 0;
3287 while (*s >= '0' && *s <= '9') {
3288 n *= 10;
3289 n += *s - '0';
3290 s++;
3291 }
3292 tmp = (tmp << 8) + n;
3293 if (*s == '.')
3294 s++;
3295 }
3296 osversion = tmp;
3297 return osversion;
3298}
3299
ths0da46a62007-10-20 20:23:07 +00003300/* do_syscall() should always have a single exit point at the end so
3301 that actions, such as logging of syscall results, can be performed.
3302 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00003303abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3304 abi_long arg2, abi_long arg3, abi_long arg4,
3305 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00003306{
blueswir1992f48a2007-10-14 16:27:31 +00003307 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003308 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00003309 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00003310 void *p;
ths3b46e622007-09-17 08:09:54 +00003311
bellard72f03902003-02-18 23:33:18 +00003312#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00003313 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00003314#endif
thsb92c47c2007-11-01 00:07:38 +00003315 if(do_strace)
3316 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3317
bellard31e31b82003-02-18 22:55:36 +00003318 switch(num) {
3319 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00003320#ifdef HAVE_GPROF
3321 _mcleanup();
3322#endif
bellarde9009672005-04-26 20:42:36 +00003323 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00003324 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00003325 _exit(arg1);
3326 ret = 0; /* avoid warning */
3327 break;
3328 case TARGET_NR_read:
bellard579a97f2007-11-11 14:26:47 +00003329 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3330 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003331 ret = get_errno(read(arg1, p, arg3));
3332 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00003333 break;
3334 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00003335 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3336 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003337 ret = get_errno(write(arg1, p, arg3));
3338 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00003339 break;
3340 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00003341 if (!(p = lock_user_string(arg1)))
3342 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003343 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00003344 target_to_host_bitmask(arg2, fcntl_flags_tbl),
3345 arg3));
pbrook53a59602006-03-25 19:31:22 +00003346 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003347 break;
ths82424832007-09-24 09:21:55 +00003348#if defined(TARGET_NR_openat) && defined(__NR_openat)
3349 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00003350 if (!(p = lock_user_string(arg2)))
3351 goto efault;
3352 ret = get_errno(sys_openat(arg1,
3353 path(p),
3354 target_to_host_bitmask(arg3, fcntl_flags_tbl),
3355 arg4));
3356 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00003357 break;
3358#endif
bellard31e31b82003-02-18 22:55:36 +00003359 case TARGET_NR_close:
3360 ret = get_errno(close(arg1));
3361 break;
3362 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00003363 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00003364 break;
3365 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00003366 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00003367 break;
thse5febef2007-04-01 18:31:35 +00003368#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00003369 case TARGET_NR_waitpid:
3370 {
pbrook53a59602006-03-25 19:31:22 +00003371 int status;
3372 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00003373 if (!is_error(ret) && arg2
3374 && put_user_s32(status, arg2))
3375 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003376 }
3377 break;
thse5febef2007-04-01 18:31:35 +00003378#endif
pbrookf0cbb612008-05-30 18:20:05 +00003379#ifdef TARGET_NR_waitid
3380 case TARGET_NR_waitid:
3381 {
3382 siginfo_t info;
3383 info.si_pid = 0;
3384 ret = get_errno(waitid(arg1, arg2, &info, arg4));
3385 if (!is_error(ret) && arg3 && info.si_pid != 0) {
3386 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
3387 goto efault;
3388 host_to_target_siginfo(p, &info);
3389 unlock_user(p, arg3, sizeof(target_siginfo_t));
3390 }
3391 }
3392 break;
3393#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003394#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003395 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00003396 if (!(p = lock_user_string(arg1)))
3397 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003398 ret = get_errno(creat(p, arg2));
3399 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003400 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003401#endif
bellard31e31b82003-02-18 22:55:36 +00003402 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00003403 {
3404 void * p2;
3405 p = lock_user_string(arg1);
3406 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003407 if (!p || !p2)
3408 ret = -TARGET_EFAULT;
3409 else
3410 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003411 unlock_user(p2, arg2, 0);
3412 unlock_user(p, arg1, 0);
3413 }
bellard31e31b82003-02-18 22:55:36 +00003414 break;
ths64f0ce42007-09-24 09:25:06 +00003415#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3416 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00003417 {
3418 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00003419 if (!arg2 || !arg4)
3420 goto efault;
ths64f0ce42007-09-24 09:25:06 +00003421 p = lock_user_string(arg2);
3422 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003423 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003424 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00003425 else
3426 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00003427 unlock_user(p, arg2, 0);
3428 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00003429 }
3430 break;
3431#endif
bellard31e31b82003-02-18 22:55:36 +00003432 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00003433 if (!(p = lock_user_string(arg1)))
3434 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003435 ret = get_errno(unlink(p));
3436 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003437 break;
ths8170f562007-09-24 09:24:11 +00003438#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3439 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00003440 if (!(p = lock_user_string(arg2)))
3441 goto efault;
3442 ret = get_errno(sys_unlinkat(arg1, p, arg3));
3443 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00003444 break;
balrogb7d35e62007-12-12 00:40:24 +00003445#endif
bellard31e31b82003-02-18 22:55:36 +00003446 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00003447 {
3448 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00003449 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00003450 abi_ulong gp;
3451 abi_ulong guest_argp;
3452 abi_ulong guest_envp;
3453 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00003454 char **q;
3455
bellardf7341ff2003-03-30 21:00:25 +00003456 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00003457 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00003458 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00003459 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003460 goto efault;
ths03aa1972007-12-02 06:28:08 +00003461 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003462 break;
bellard7854b052003-03-29 17:22:23 +00003463 argc++;
bellard2f619692007-11-16 10:46:05 +00003464 }
bellardf7341ff2003-03-30 21:00:25 +00003465 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00003466 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00003467 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00003468 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003469 goto efault;
ths03aa1972007-12-02 06:28:08 +00003470 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003471 break;
bellard7854b052003-03-29 17:22:23 +00003472 envc++;
bellard2f619692007-11-16 10:46:05 +00003473 }
bellard7854b052003-03-29 17:22:23 +00003474
bellardf7341ff2003-03-30 21:00:25 +00003475 argp = alloca((argc + 1) * sizeof(void *));
3476 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00003477
pbrookda94d262008-05-30 18:24:00 +00003478 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00003479 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003480 if (get_user_ual(addr, gp))
3481 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003482 if (!addr)
3483 break;
bellard2f619692007-11-16 10:46:05 +00003484 if (!(*q = lock_user_string(addr)))
3485 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003486 }
bellardf7341ff2003-03-30 21:00:25 +00003487 *q = NULL;
3488
pbrookda94d262008-05-30 18:24:00 +00003489 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00003490 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003491 if (get_user_ual(addr, gp))
3492 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003493 if (!addr)
3494 break;
bellard2f619692007-11-16 10:46:05 +00003495 if (!(*q = lock_user_string(addr)))
3496 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003497 }
bellardf7341ff2003-03-30 21:00:25 +00003498 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00003499
bellard2f619692007-11-16 10:46:05 +00003500 if (!(p = lock_user_string(arg1)))
3501 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003502 ret = get_errno(execve(p, argp, envp));
3503 unlock_user(p, arg1, 0);
3504
bellard2f619692007-11-16 10:46:05 +00003505 goto execve_end;
3506
3507 execve_efault:
3508 ret = -TARGET_EFAULT;
3509
3510 execve_end:
pbrook53a59602006-03-25 19:31:22 +00003511 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00003512 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003513 if (get_user_ual(addr, gp)
3514 || !addr)
3515 break;
pbrook53a59602006-03-25 19:31:22 +00003516 unlock_user(*q, addr, 0);
3517 }
3518 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00003519 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003520 if (get_user_ual(addr, gp)
3521 || !addr)
3522 break;
pbrook53a59602006-03-25 19:31:22 +00003523 unlock_user(*q, addr, 0);
3524 }
bellard7854b052003-03-29 17:22:23 +00003525 }
bellard31e31b82003-02-18 22:55:36 +00003526 break;
3527 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00003528 if (!(p = lock_user_string(arg1)))
3529 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003530 ret = get_errno(chdir(p));
3531 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003532 break;
bellarda315a142005-01-30 22:59:18 +00003533#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00003534 case TARGET_NR_time:
3535 {
pbrook53a59602006-03-25 19:31:22 +00003536 time_t host_time;
3537 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00003538 if (!is_error(ret)
3539 && arg1
3540 && put_user_sal(host_time, arg1))
3541 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003542 }
3543 break;
bellarda315a142005-01-30 22:59:18 +00003544#endif
bellard31e31b82003-02-18 22:55:36 +00003545 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00003546 if (!(p = lock_user_string(arg1)))
3547 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003548 ret = get_errno(mknod(p, arg2, arg3));
3549 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003550 break;
ths75ac37a2007-09-24 09:23:05 +00003551#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
3552 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00003553 if (!(p = lock_user_string(arg2)))
3554 goto efault;
3555 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
3556 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00003557 break;
3558#endif
bellard31e31b82003-02-18 22:55:36 +00003559 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00003560 if (!(p = lock_user_string(arg1)))
3561 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003562 ret = get_errno(chmod(p, arg2));
3563 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003564 break;
bellardebc05482003-09-30 21:08:41 +00003565#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00003566 case TARGET_NR_break:
3567 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003568#endif
3569#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00003570 case TARGET_NR_oldstat:
3571 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003572#endif
bellard31e31b82003-02-18 22:55:36 +00003573 case TARGET_NR_lseek:
3574 ret = get_errno(lseek(arg1, arg2, arg3));
3575 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003576#ifdef TARGET_NR_getxpid
3577 case TARGET_NR_getxpid:
3578#else
bellard31e31b82003-02-18 22:55:36 +00003579 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00003580#endif
bellard31e31b82003-02-18 22:55:36 +00003581 ret = get_errno(getpid());
3582 break;
3583 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00003584 {
3585 /* need to look at the data field */
3586 void *p2, *p3;
3587 p = lock_user_string(arg1);
3588 p2 = lock_user_string(arg2);
3589 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00003590 if (!p || !p2 || !p3)
3591 ret = -TARGET_EFAULT;
3592 else
3593 /* FIXME - arg5 should be locked, but it isn't clear how to
3594 * do that since it's not guaranteed to be a NULL-terminated
3595 * string.
3596 */
3597 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3598 unlock_user(p, arg1, 0);
3599 unlock_user(p2, arg2, 0);
3600 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00003601 break;
3602 }
thse5febef2007-04-01 18:31:35 +00003603#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00003604 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00003605 if (!(p = lock_user_string(arg1)))
3606 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003607 ret = get_errno(umount(p));
3608 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003609 break;
thse5febef2007-04-01 18:31:35 +00003610#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003611#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003612 case TARGET_NR_stime:
3613 {
pbrook53a59602006-03-25 19:31:22 +00003614 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00003615 if (get_user_sal(host_time, arg1))
3616 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003617 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00003618 }
3619 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003620#endif
bellard31e31b82003-02-18 22:55:36 +00003621 case TARGET_NR_ptrace:
3622 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00003623#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003624 case TARGET_NR_alarm:
3625 ret = alarm(arg1);
3626 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003627#endif
bellardebc05482003-09-30 21:08:41 +00003628#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00003629 case TARGET_NR_oldfstat:
3630 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003631#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003632#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003633 case TARGET_NR_pause:
3634 ret = get_errno(pause());
3635 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003636#endif
thse5febef2007-04-01 18:31:35 +00003637#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00003638 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00003639 {
pbrook53a59602006-03-25 19:31:22 +00003640 struct utimbuf tbuf, *host_tbuf;
3641 struct target_utimbuf *target_tbuf;
3642 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003643 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3644 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003645 tbuf.actime = tswapl(target_tbuf->actime);
3646 tbuf.modtime = tswapl(target_tbuf->modtime);
3647 unlock_user_struct(target_tbuf, arg2, 0);
3648 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00003649 } else {
pbrook53a59602006-03-25 19:31:22 +00003650 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00003651 }
bellard579a97f2007-11-11 14:26:47 +00003652 if (!(p = lock_user_string(arg1)))
3653 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003654 ret = get_errno(utime(p, host_tbuf));
3655 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00003656 }
3657 break;
thse5febef2007-04-01 18:31:35 +00003658#endif
bellard978a66f2004-12-06 22:58:05 +00003659 case TARGET_NR_utimes:
3660 {
bellard978a66f2004-12-06 22:58:05 +00003661 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00003662 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00003663 if (copy_from_user_timeval(&tv[0], arg2)
3664 || copy_from_user_timeval(&tv[1],
3665 arg2 + sizeof(struct target_timeval)))
3666 goto efault;
bellard978a66f2004-12-06 22:58:05 +00003667 tvp = tv;
3668 } else {
3669 tvp = NULL;
3670 }
bellard579a97f2007-11-11 14:26:47 +00003671 if (!(p = lock_user_string(arg1)))
3672 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003673 ret = get_errno(utimes(p, tvp));
3674 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00003675 }
3676 break;
balrogac8a6552008-09-20 02:25:39 +00003677#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
3678 case TARGET_NR_futimesat:
3679 {
3680 struct timeval *tvp, tv[2];
3681 if (arg3) {
3682 if (copy_from_user_timeval(&tv[0], arg3)
3683 || copy_from_user_timeval(&tv[1],
3684 arg3 + sizeof(struct target_timeval)))
3685 goto efault;
3686 tvp = tv;
3687 } else {
3688 tvp = NULL;
3689 }
3690 if (!(p = lock_user_string(arg2)))
3691 goto efault;
3692 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
3693 unlock_user(p, arg2, 0);
3694 }
3695 break;
3696#endif
bellardebc05482003-09-30 21:08:41 +00003697#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00003698 case TARGET_NR_stty:
3699 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003700#endif
3701#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00003702 case TARGET_NR_gtty:
3703 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003704#endif
bellard31e31b82003-02-18 22:55:36 +00003705 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00003706 if (!(p = lock_user_string(arg1)))
3707 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003708 ret = get_errno(access(p, arg2));
3709 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003710 break;
ths92a34c12007-09-24 09:27:49 +00003711#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3712 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00003713 if (!(p = lock_user_string(arg2)))
3714 goto efault;
3715 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3716 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00003717 break;
3718#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003719#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003720 case TARGET_NR_nice:
3721 ret = get_errno(nice(arg1));
3722 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003723#endif
bellardebc05482003-09-30 21:08:41 +00003724#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00003725 case TARGET_NR_ftime:
3726 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003727#endif
bellard31e31b82003-02-18 22:55:36 +00003728 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00003729 sync();
3730 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00003731 break;
3732 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00003733 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00003734 break;
3735 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00003736 {
3737 void *p2;
3738 p = lock_user_string(arg1);
3739 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003740 if (!p || !p2)
3741 ret = -TARGET_EFAULT;
3742 else
3743 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003744 unlock_user(p2, arg2, 0);
3745 unlock_user(p, arg1, 0);
3746 }
bellard31e31b82003-02-18 22:55:36 +00003747 break;
ths722183f2007-09-24 09:24:37 +00003748#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3749 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00003750 {
bellard579a97f2007-11-11 14:26:47 +00003751 void *p2;
ths722183f2007-09-24 09:24:37 +00003752 p = lock_user_string(arg2);
3753 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003754 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003755 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00003756 else
3757 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00003758 unlock_user(p2, arg4, 0);
3759 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00003760 }
3761 break;
3762#endif
bellard31e31b82003-02-18 22:55:36 +00003763 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00003764 if (!(p = lock_user_string(arg1)))
3765 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003766 ret = get_errno(mkdir(p, arg2));
3767 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003768 break;
ths4472ad02007-09-24 09:22:32 +00003769#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3770 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00003771 if (!(p = lock_user_string(arg2)))
3772 goto efault;
3773 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3774 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00003775 break;
3776#endif
bellard31e31b82003-02-18 22:55:36 +00003777 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00003778 if (!(p = lock_user_string(arg1)))
3779 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003780 ret = get_errno(rmdir(p));
3781 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003782 break;
3783 case TARGET_NR_dup:
3784 ret = get_errno(dup(arg1));
3785 break;
3786 case TARGET_NR_pipe:
3787 {
pbrook53a59602006-03-25 19:31:22 +00003788 int host_pipe[2];
3789 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00003790 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00003791#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00003792 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
thsb5dc7732008-06-27 10:02:35 +00003793 env->active_tc.gpr[3] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00003794 ret = host_pipe[0];
aurel32b5eff352008-03-11 23:30:22 +00003795#elif defined(TARGET_SH4)
3796 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
3797 ret = host_pipe[0];
thsc12ab052007-06-01 11:50:36 +00003798#else
bellard2f619692007-11-16 10:46:05 +00003799 if (put_user_s32(host_pipe[0], arg1)
3800 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
3801 goto efault;
thsc12ab052007-06-01 11:50:36 +00003802#endif
bellard31e31b82003-02-18 22:55:36 +00003803 }
3804 }
3805 break;
3806 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00003807 {
pbrook53a59602006-03-25 19:31:22 +00003808 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00003809 struct tms tms;
3810 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003811 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00003812 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3813 if (!tmsp)
3814 goto efault;
bellardc596ed12003-07-13 17:32:31 +00003815 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3816 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3817 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3818 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003819 }
bellardc596ed12003-07-13 17:32:31 +00003820 if (!is_error(ret))
3821 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003822 }
3823 break;
bellardebc05482003-09-30 21:08:41 +00003824#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003825 case TARGET_NR_prof:
3826 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003827#endif
thse5febef2007-04-01 18:31:35 +00003828#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003829 case TARGET_NR_signal:
3830 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003831#endif
bellard31e31b82003-02-18 22:55:36 +00003832 case TARGET_NR_acct:
bellard579a97f2007-11-11 14:26:47 +00003833 if (!(p = lock_user_string(arg1)))
3834 goto efault;
pbrook24836682006-04-16 14:14:53 +00003835 ret = get_errno(acct(path(p)));
3836 unlock_user(p, arg1, 0);
3837 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003838#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003839 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00003840 if (!(p = lock_user_string(arg1)))
3841 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003842 ret = get_errno(umount2(p, arg2));
3843 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003844 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003845#endif
bellardebc05482003-09-30 21:08:41 +00003846#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003847 case TARGET_NR_lock:
3848 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003849#endif
bellard31e31b82003-02-18 22:55:36 +00003850 case TARGET_NR_ioctl:
3851 ret = do_ioctl(arg1, arg2, arg3);
3852 break;
3853 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00003854 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00003855 break;
bellardebc05482003-09-30 21:08:41 +00003856#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003857 case TARGET_NR_mpx:
3858 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003859#endif
bellard31e31b82003-02-18 22:55:36 +00003860 case TARGET_NR_setpgid:
3861 ret = get_errno(setpgid(arg1, arg2));
3862 break;
bellardebc05482003-09-30 21:08:41 +00003863#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003864 case TARGET_NR_ulimit:
3865 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003866#endif
3867#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003868 case TARGET_NR_oldolduname:
3869 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003870#endif
bellard31e31b82003-02-18 22:55:36 +00003871 case TARGET_NR_umask:
3872 ret = get_errno(umask(arg1));
3873 break;
3874 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00003875 if (!(p = lock_user_string(arg1)))
3876 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003877 ret = get_errno(chroot(p));
3878 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003879 break;
3880 case TARGET_NR_ustat:
3881 goto unimplemented;
3882 case TARGET_NR_dup2:
3883 ret = get_errno(dup2(arg1, arg2));
3884 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003885#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003886 case TARGET_NR_getppid:
3887 ret = get_errno(getppid());
3888 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003889#endif
bellard31e31b82003-02-18 22:55:36 +00003890 case TARGET_NR_getpgrp:
3891 ret = get_errno(getpgrp());
3892 break;
3893 case TARGET_NR_setsid:
3894 ret = get_errno(setsid());
3895 break;
thse5febef2007-04-01 18:31:35 +00003896#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003897 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003898 {
ths388bb212007-05-13 13:58:00 +00003899#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003900 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003901 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003902 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003903 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3904 goto efault;
bellard66fb9762003-03-23 01:06:05 +00003905 act._sa_handler = old_act->_sa_handler;
3906 target_siginitset(&act.sa_mask, old_act->sa_mask);
3907 act.sa_flags = old_act->sa_flags;
3908 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003909 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003910 pact = &act;
3911 } else {
3912 pact = NULL;
3913 }
3914 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003915 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003916 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3917 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003918 old_act->_sa_handler = oact._sa_handler;
3919 old_act->sa_mask = oact.sa_mask.sig[0];
3920 old_act->sa_flags = oact.sa_flags;
3921 old_act->sa_restorer = oact.sa_restorer;
3922 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003923 }
ths388bb212007-05-13 13:58:00 +00003924#else
bellard106ec872006-06-27 21:08:10 +00003925 struct target_sigaction act, oact, *pact, *old_act;
3926
3927 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003928 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3929 goto efault;
bellard106ec872006-06-27 21:08:10 +00003930 act._sa_handler = old_act->_sa_handler;
3931 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3932 act.sa_flags = old_act->sa_flags;
3933 unlock_user_struct(old_act, arg2, 0);
3934 pact = &act;
3935 } else {
3936 pact = NULL;
3937 }
3938
3939 ret = get_errno(do_sigaction(arg1, pact, &oact));
3940
3941 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003942 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3943 goto efault;
bellard106ec872006-06-27 21:08:10 +00003944 old_act->_sa_handler = oact._sa_handler;
3945 old_act->sa_flags = oact.sa_flags;
3946 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3947 old_act->sa_mask.sig[1] = 0;
3948 old_act->sa_mask.sig[2] = 0;
3949 old_act->sa_mask.sig[3] = 0;
3950 unlock_user_struct(old_act, arg3, 1);
3951 }
ths388bb212007-05-13 13:58:00 +00003952#endif
bellard31e31b82003-02-18 22:55:36 +00003953 }
3954 break;
thse5febef2007-04-01 18:31:35 +00003955#endif
bellard66fb9762003-03-23 01:06:05 +00003956 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003957 {
3958 struct target_sigaction *act;
3959 struct target_sigaction *oact;
3960
bellard579a97f2007-11-11 14:26:47 +00003961 if (arg2) {
3962 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3963 goto efault;
3964 } else
pbrook53a59602006-03-25 19:31:22 +00003965 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00003966 if (arg3) {
3967 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3968 ret = -TARGET_EFAULT;
3969 goto rt_sigaction_fail;
3970 }
3971 } else
pbrook53a59602006-03-25 19:31:22 +00003972 oact = NULL;
3973 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00003974 rt_sigaction_fail:
3975 if (act)
pbrook53a59602006-03-25 19:31:22 +00003976 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00003977 if (oact)
pbrook53a59602006-03-25 19:31:22 +00003978 unlock_user_struct(oact, arg3, 1);
3979 }
bellard66fb9762003-03-23 01:06:05 +00003980 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003981#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003982 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003983 {
3984 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003985 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00003986 sigprocmask(0, NULL, &cur_set);
3987 host_to_target_old_sigset(&target_set, &cur_set);
3988 ret = target_set;
3989 }
3990 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003991#endif
3992#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003993 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003994 {
3995 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003996 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00003997 sigprocmask(0, NULL, &cur_set);
3998 target_to_host_old_sigset(&set, &target_set);
3999 sigorset(&set, &set, &cur_set);
4000 sigprocmask(SIG_SETMASK, &set, &oset);
4001 host_to_target_old_sigset(&target_set, &oset);
4002 ret = target_set;
4003 }
4004 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004005#endif
thse5febef2007-04-01 18:31:35 +00004006#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00004007 case TARGET_NR_sigprocmask:
4008 {
4009 int how = arg1;
4010 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00004011
pbrook53a59602006-03-25 19:31:22 +00004012 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004013 switch(how) {
4014 case TARGET_SIG_BLOCK:
4015 how = SIG_BLOCK;
4016 break;
4017 case TARGET_SIG_UNBLOCK:
4018 how = SIG_UNBLOCK;
4019 break;
4020 case TARGET_SIG_SETMASK:
4021 how = SIG_SETMASK;
4022 break;
4023 default:
ths0da46a62007-10-20 20:23:07 +00004024 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00004025 goto fail;
4026 }
bellard579a97f2007-11-11 14:26:47 +00004027 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4028 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004029 target_to_host_old_sigset(&set, p);
4030 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004031 set_ptr = &set;
4032 } else {
4033 how = 0;
4034 set_ptr = NULL;
4035 }
4036 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00004037 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004038 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4039 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004040 host_to_target_old_sigset(p, &oldset);
4041 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00004042 }
4043 }
4044 break;
thse5febef2007-04-01 18:31:35 +00004045#endif
bellard66fb9762003-03-23 01:06:05 +00004046 case TARGET_NR_rt_sigprocmask:
4047 {
4048 int how = arg1;
4049 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00004050
pbrook53a59602006-03-25 19:31:22 +00004051 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004052 switch(how) {
4053 case TARGET_SIG_BLOCK:
4054 how = SIG_BLOCK;
4055 break;
4056 case TARGET_SIG_UNBLOCK:
4057 how = SIG_UNBLOCK;
4058 break;
4059 case TARGET_SIG_SETMASK:
4060 how = SIG_SETMASK;
4061 break;
4062 default:
ths0da46a62007-10-20 20:23:07 +00004063 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00004064 goto fail;
4065 }
bellard579a97f2007-11-11 14:26:47 +00004066 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4067 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004068 target_to_host_sigset(&set, p);
4069 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004070 set_ptr = &set;
4071 } else {
4072 how = 0;
4073 set_ptr = NULL;
4074 }
4075 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00004076 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004077 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4078 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004079 host_to_target_sigset(p, &oldset);
4080 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00004081 }
4082 }
4083 break;
thse5febef2007-04-01 18:31:35 +00004084#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00004085 case TARGET_NR_sigpending:
4086 {
4087 sigset_t set;
4088 ret = get_errno(sigpending(&set));
4089 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004090 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4091 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004092 host_to_target_old_sigset(p, &set);
4093 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00004094 }
4095 }
4096 break;
thse5febef2007-04-01 18:31:35 +00004097#endif
bellard66fb9762003-03-23 01:06:05 +00004098 case TARGET_NR_rt_sigpending:
4099 {
4100 sigset_t set;
4101 ret = get_errno(sigpending(&set));
4102 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004103 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4104 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004105 host_to_target_sigset(p, &set);
4106 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00004107 }
4108 }
4109 break;
thse5febef2007-04-01 18:31:35 +00004110#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00004111 case TARGET_NR_sigsuspend:
4112 {
4113 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00004114 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4115 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004116 target_to_host_old_sigset(&set, p);
4117 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00004118 ret = get_errno(sigsuspend(&set));
4119 }
4120 break;
thse5febef2007-04-01 18:31:35 +00004121#endif
bellard66fb9762003-03-23 01:06:05 +00004122 case TARGET_NR_rt_sigsuspend:
4123 {
4124 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00004125 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4126 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004127 target_to_host_sigset(&set, p);
4128 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00004129 ret = get_errno(sigsuspend(&set));
4130 }
4131 break;
4132 case TARGET_NR_rt_sigtimedwait:
4133 {
bellard66fb9762003-03-23 01:06:05 +00004134 sigset_t set;
4135 struct timespec uts, *puts;
4136 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00004137
bellard579a97f2007-11-11 14:26:47 +00004138 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4139 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004140 target_to_host_sigset(&set, p);
4141 unlock_user(p, arg1, 0);
4142 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00004143 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00004144 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00004145 } else {
4146 puts = NULL;
4147 }
4148 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00004149 if (!is_error(ret) && arg2) {
pbrooke1e3f302008-05-30 21:53:38 +00004150 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00004151 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004152 host_to_target_siginfo(p, &uinfo);
pbrooke1e3f302008-05-30 21:53:38 +00004153 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00004154 }
4155 }
4156 break;
4157 case TARGET_NR_rt_sigqueueinfo:
4158 {
4159 siginfo_t uinfo;
bellard579a97f2007-11-11 14:26:47 +00004160 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
4161 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004162 target_to_host_siginfo(&uinfo, p);
4163 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00004164 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
4165 }
4166 break;
thse5febef2007-04-01 18:31:35 +00004167#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00004168 case TARGET_NR_sigreturn:
4169 /* NOTE: ret is eax, so not transcoding must be done */
4170 ret = do_sigreturn(cpu_env);
4171 break;
thse5febef2007-04-01 18:31:35 +00004172#endif
bellard66fb9762003-03-23 01:06:05 +00004173 case TARGET_NR_rt_sigreturn:
4174 /* NOTE: ret is eax, so not transcoding must be done */
4175 ret = do_rt_sigreturn(cpu_env);
4176 break;
bellard31e31b82003-02-18 22:55:36 +00004177 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00004178 if (!(p = lock_user_string(arg1)))
4179 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004180 ret = get_errno(sethostname(p, arg2));
4181 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004182 break;
4183 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00004184 {
4185 /* XXX: convert resource ? */
4186 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00004187 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00004188 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00004189 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
4190 goto efault;
bellard9de5e442003-03-23 16:49:39 +00004191 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4192 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004193 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00004194 ret = get_errno(setrlimit(resource, &rlim));
4195 }
4196 break;
bellard31e31b82003-02-18 22:55:36 +00004197 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00004198 {
4199 /* XXX: convert resource ? */
4200 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00004201 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00004202 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00004203
bellard9de5e442003-03-23 16:49:39 +00004204 ret = get_errno(getrlimit(resource, &rlim));
4205 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004206 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4207 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004208 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4209 rlim.rlim_max = tswapl(target_rlim->rlim_max);
4210 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00004211 }
4212 }
4213 break;
bellard31e31b82003-02-18 22:55:36 +00004214 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00004215 {
4216 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00004217 ret = get_errno(getrusage(arg1, &rusage));
4218 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004219 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00004220 }
4221 }
4222 break;
bellard31e31b82003-02-18 22:55:36 +00004223 case TARGET_NR_gettimeofday:
4224 {
bellard31e31b82003-02-18 22:55:36 +00004225 struct timeval tv;
4226 ret = get_errno(gettimeofday(&tv, NULL));
4227 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00004228 if (copy_to_user_timeval(arg1, &tv))
4229 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004230 }
4231 }
4232 break;
4233 case TARGET_NR_settimeofday:
4234 {
bellard31e31b82003-02-18 22:55:36 +00004235 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00004236 if (copy_from_user_timeval(&tv, arg1))
4237 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004238 ret = get_errno(settimeofday(&tv, NULL));
4239 }
4240 break;
bellard048f6b42005-11-26 18:47:20 +00004241#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00004242 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00004243 {
pbrook53a59602006-03-25 19:31:22 +00004244 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00004245 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00004246 long nsel;
4247
bellard579a97f2007-11-11 14:26:47 +00004248 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
4249 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004250 nsel = tswapl(sel->n);
4251 inp = tswapl(sel->inp);
4252 outp = tswapl(sel->outp);
4253 exp = tswapl(sel->exp);
4254 tvp = tswapl(sel->tvp);
4255 unlock_user_struct(sel, arg1, 0);
4256 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00004257 }
4258 break;
bellard048f6b42005-11-26 18:47:20 +00004259#endif
bellard31e31b82003-02-18 22:55:36 +00004260 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00004261 {
4262 void *p2;
4263 p = lock_user_string(arg1);
4264 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004265 if (!p || !p2)
4266 ret = -TARGET_EFAULT;
4267 else
4268 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004269 unlock_user(p2, arg2, 0);
4270 unlock_user(p, arg1, 0);
4271 }
bellard31e31b82003-02-18 22:55:36 +00004272 break;
thsf0b62432007-09-24 09:25:40 +00004273#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
4274 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00004275 {
bellard579a97f2007-11-11 14:26:47 +00004276 void *p2;
thsf0b62432007-09-24 09:25:40 +00004277 p = lock_user_string(arg1);
4278 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004279 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004280 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00004281 else
4282 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00004283 unlock_user(p2, arg3, 0);
4284 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00004285 }
4286 break;
4287#endif
bellardebc05482003-09-30 21:08:41 +00004288#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00004289 case TARGET_NR_oldlstat:
4290 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004291#endif
bellard31e31b82003-02-18 22:55:36 +00004292 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00004293 {
4294 void *p2;
4295 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00004296 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4297 if (!p || !p2)
4298 ret = -TARGET_EFAULT;
4299 else
4300 ret = get_errno(readlink(path(p), p2, arg3));
pbrook53a59602006-03-25 19:31:22 +00004301 unlock_user(p2, arg2, ret);
4302 unlock_user(p, arg1, 0);
4303 }
bellard31e31b82003-02-18 22:55:36 +00004304 break;
ths5e0ccb12007-09-24 09:26:10 +00004305#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4306 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00004307 {
bellard579a97f2007-11-11 14:26:47 +00004308 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00004309 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004310 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4311 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004312 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00004313 else
4314 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00004315 unlock_user(p2, arg3, ret);
4316 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00004317 }
4318 break;
4319#endif
thse5febef2007-04-01 18:31:35 +00004320#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00004321 case TARGET_NR_uselib:
4322 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004323#endif
4324#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00004325 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00004326 if (!(p = lock_user_string(arg1)))
4327 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004328 ret = get_errno(swapon(p, arg2));
4329 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004330 break;
thse5febef2007-04-01 18:31:35 +00004331#endif
bellard31e31b82003-02-18 22:55:36 +00004332 case TARGET_NR_reboot:
4333 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004334#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00004335 case TARGET_NR_readdir:
4336 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004337#endif
4338#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00004339 case TARGET_NR_mmap:
bellardd2fd1af2007-11-14 18:08:56 +00004340#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
bellard31e31b82003-02-18 22:55:36 +00004341 {
blueswir1992f48a2007-10-14 16:27:31 +00004342 abi_ulong *v;
4343 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00004344 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4345 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004346 v1 = tswapl(v[0]);
4347 v2 = tswapl(v[1]);
4348 v3 = tswapl(v[2]);
4349 v4 = tswapl(v[3]);
4350 v5 = tswapl(v[4]);
4351 v6 = tswapl(v[5]);
4352 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00004353 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00004354 target_to_host_bitmask(v4, mmap_flags_tbl),
4355 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00004356 }
bellard31e31b82003-02-18 22:55:36 +00004357#else
ths5fafdf22007-09-16 21:08:06 +00004358 ret = get_errno(target_mmap(arg1, arg2, arg3,
4359 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00004360 arg5,
4361 arg6));
bellard31e31b82003-02-18 22:55:36 +00004362#endif
bellard6fb883e2003-07-09 17:12:39 +00004363 break;
thse5febef2007-04-01 18:31:35 +00004364#endif
bellarda315a142005-01-30 22:59:18 +00004365#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00004366 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00004367#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00004368#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00004369#endif
ths5fafdf22007-09-16 21:08:06 +00004370 ret = get_errno(target_mmap(arg1, arg2, arg3,
4371 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00004372 arg5,
bellardc573ff62004-01-04 15:51:36 +00004373 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00004374 break;
bellarda315a142005-01-30 22:59:18 +00004375#endif
bellard31e31b82003-02-18 22:55:36 +00004376 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00004377 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004378 break;
bellard9de5e442003-03-23 16:49:39 +00004379 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00004380 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004381 break;
thse5febef2007-04-01 18:31:35 +00004382#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00004383 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00004384 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00004385 break;
thse5febef2007-04-01 18:31:35 +00004386#endif
pbrook53a59602006-03-25 19:31:22 +00004387 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00004388#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00004389 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00004390 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004391 break;
thse5febef2007-04-01 18:31:35 +00004392#endif
4393#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00004394 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00004395 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004396 break;
thse5febef2007-04-01 18:31:35 +00004397#endif
4398#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00004399 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00004400 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004401 break;
thse5febef2007-04-01 18:31:35 +00004402#endif
4403#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00004404 case TARGET_NR_mlockall:
4405 ret = get_errno(mlockall(arg1));
4406 break;
thse5febef2007-04-01 18:31:35 +00004407#endif
4408#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00004409 case TARGET_NR_munlockall:
4410 ret = get_errno(munlockall());
4411 break;
thse5febef2007-04-01 18:31:35 +00004412#endif
bellard31e31b82003-02-18 22:55:36 +00004413 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00004414 if (!(p = lock_user_string(arg1)))
4415 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004416 ret = get_errno(truncate(p, arg2));
4417 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004418 break;
4419 case TARGET_NR_ftruncate:
4420 ret = get_errno(ftruncate(arg1, arg2));
4421 break;
4422 case TARGET_NR_fchmod:
4423 ret = get_errno(fchmod(arg1, arg2));
4424 break;
ths814d7972007-09-24 09:26:51 +00004425#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
4426 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00004427 if (!(p = lock_user_string(arg2)))
4428 goto efault;
4429 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
4430 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00004431 break;
4432#endif
bellard31e31b82003-02-18 22:55:36 +00004433 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00004434 /* libc does special remapping of the return value of
4435 * sys_getpriority() so it's just easiest to call
4436 * sys_getpriority() directly rather than through libc. */
4437 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00004438 break;
4439 case TARGET_NR_setpriority:
4440 ret = get_errno(setpriority(arg1, arg2, arg3));
4441 break;
bellardebc05482003-09-30 21:08:41 +00004442#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00004443 case TARGET_NR_profil:
4444 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004445#endif
bellard31e31b82003-02-18 22:55:36 +00004446 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00004447 if (!(p = lock_user_string(arg1)))
4448 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004449 ret = get_errno(statfs(path(p), &stfs));
4450 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004451 convert_statfs:
4452 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004453 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00004454
bellard579a97f2007-11-11 14:26:47 +00004455 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
4456 goto efault;
4457 __put_user(stfs.f_type, &target_stfs->f_type);
4458 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4459 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4460 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4461 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4462 __put_user(stfs.f_files, &target_stfs->f_files);
4463 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4464 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4465 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4466 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00004467 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004468 }
4469 break;
4470 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00004471 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00004472 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00004473#ifdef TARGET_NR_statfs64
4474 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00004475 if (!(p = lock_user_string(arg1)))
4476 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004477 ret = get_errno(statfs(path(p), &stfs));
4478 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00004479 convert_statfs64:
4480 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004481 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00004482
bellard579a97f2007-11-11 14:26:47 +00004483 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
4484 goto efault;
4485 __put_user(stfs.f_type, &target_stfs->f_type);
4486 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4487 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4488 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4489 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4490 __put_user(stfs.f_files, &target_stfs->f_files);
4491 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4492 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4493 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4494 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4495 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00004496 }
4497 break;
4498 case TARGET_NR_fstatfs64:
4499 ret = get_errno(fstatfs(arg1, &stfs));
4500 goto convert_statfs64;
4501#endif
bellardebc05482003-09-30 21:08:41 +00004502#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00004503 case TARGET_NR_ioperm:
4504 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004505#endif
thse5febef2007-04-01 18:31:35 +00004506#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00004507 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00004508 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00004509 break;
thse5febef2007-04-01 18:31:35 +00004510#endif
bellard3532fa72006-06-24 15:06:03 +00004511#ifdef TARGET_NR_accept
4512 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00004513 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004514 break;
4515#endif
4516#ifdef TARGET_NR_bind
4517 case TARGET_NR_bind:
4518 ret = do_bind(arg1, arg2, arg3);
4519 break;
4520#endif
4521#ifdef TARGET_NR_connect
4522 case TARGET_NR_connect:
4523 ret = do_connect(arg1, arg2, arg3);
4524 break;
4525#endif
4526#ifdef TARGET_NR_getpeername
4527 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00004528 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004529 break;
4530#endif
4531#ifdef TARGET_NR_getsockname
4532 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00004533 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004534 break;
4535#endif
4536#ifdef TARGET_NR_getsockopt
4537 case TARGET_NR_getsockopt:
4538 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
4539 break;
4540#endif
4541#ifdef TARGET_NR_listen
4542 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00004543 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00004544 break;
4545#endif
4546#ifdef TARGET_NR_recv
4547 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00004548 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00004549 break;
4550#endif
4551#ifdef TARGET_NR_recvfrom
4552 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00004553 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00004554 break;
4555#endif
4556#ifdef TARGET_NR_recvmsg
4557 case TARGET_NR_recvmsg:
4558 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
4559 break;
4560#endif
4561#ifdef TARGET_NR_send
4562 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00004563 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00004564 break;
4565#endif
4566#ifdef TARGET_NR_sendmsg
4567 case TARGET_NR_sendmsg:
4568 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
4569 break;
4570#endif
4571#ifdef TARGET_NR_sendto
4572 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00004573 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00004574 break;
4575#endif
4576#ifdef TARGET_NR_shutdown
4577 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00004578 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00004579 break;
4580#endif
4581#ifdef TARGET_NR_socket
4582 case TARGET_NR_socket:
4583 ret = do_socket(arg1, arg2, arg3);
4584 break;
4585#endif
4586#ifdef TARGET_NR_socketpair
4587 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00004588 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00004589 break;
4590#endif
4591#ifdef TARGET_NR_setsockopt
4592 case TARGET_NR_setsockopt:
4593 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
4594 break;
4595#endif
ths7494b0f2007-02-11 18:26:53 +00004596
bellard31e31b82003-02-18 22:55:36 +00004597 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00004598 if (!(p = lock_user_string(arg2)))
4599 goto efault;
thse5574482007-02-11 20:03:13 +00004600 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
4601 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00004602 break;
4603
bellard31e31b82003-02-18 22:55:36 +00004604 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00004605 {
bellard66fb9762003-03-23 01:06:05 +00004606 struct itimerval value, ovalue, *pvalue;
4607
pbrook53a59602006-03-25 19:31:22 +00004608 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004609 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00004610 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
4611 || copy_from_user_timeval(&pvalue->it_value,
4612 arg2 + sizeof(struct target_timeval)))
4613 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004614 } else {
4615 pvalue = NULL;
4616 }
4617 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00004618 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00004619 if (copy_to_user_timeval(arg3,
4620 &ovalue.it_interval)
4621 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
4622 &ovalue.it_value))
4623 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004624 }
4625 }
4626 break;
bellard31e31b82003-02-18 22:55:36 +00004627 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00004628 {
bellard66fb9762003-03-23 01:06:05 +00004629 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00004630
bellard66fb9762003-03-23 01:06:05 +00004631 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00004632 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00004633 if (copy_to_user_timeval(arg2,
4634 &value.it_interval)
4635 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
4636 &value.it_value))
4637 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004638 }
4639 }
4640 break;
bellard31e31b82003-02-18 22:55:36 +00004641 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00004642 if (!(p = lock_user_string(arg1)))
4643 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004644 ret = get_errno(stat(path(p), &st));
4645 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004646 goto do_stat;
4647 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00004648 if (!(p = lock_user_string(arg1)))
4649 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004650 ret = get_errno(lstat(path(p), &st));
4651 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004652 goto do_stat;
4653 case TARGET_NR_fstat:
4654 {
4655 ret = get_errno(fstat(arg1, &st));
4656 do_stat:
4657 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004658 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00004659
bellard579a97f2007-11-11 14:26:47 +00004660 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4661 goto efault;
bellardd2fd1af2007-11-14 18:08:56 +00004662 __put_user(st.st_dev, &target_st->st_dev);
4663 __put_user(st.st_ino, &target_st->st_ino);
4664 __put_user(st.st_mode, &target_st->st_mode);
4665 __put_user(st.st_uid, &target_st->st_uid);
4666 __put_user(st.st_gid, &target_st->st_gid);
4667 __put_user(st.st_nlink, &target_st->st_nlink);
4668 __put_user(st.st_rdev, &target_st->st_rdev);
4669 __put_user(st.st_size, &target_st->st_size);
4670 __put_user(st.st_blksize, &target_st->st_blksize);
4671 __put_user(st.st_blocks, &target_st->st_blocks);
4672 __put_user(st.st_atime, &target_st->target_st_atime);
4673 __put_user(st.st_mtime, &target_st->target_st_mtime);
4674 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004675 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004676 }
4677 }
4678 break;
bellardebc05482003-09-30 21:08:41 +00004679#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00004680 case TARGET_NR_olduname:
4681 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004682#endif
4683#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00004684 case TARGET_NR_iopl:
4685 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004686#endif
bellard31e31b82003-02-18 22:55:36 +00004687 case TARGET_NR_vhangup:
4688 ret = get_errno(vhangup());
4689 break;
bellardebc05482003-09-30 21:08:41 +00004690#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00004691 case TARGET_NR_idle:
4692 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004693#endif
bellard42ad6ae2005-01-03 22:48:11 +00004694#ifdef TARGET_NR_syscall
4695 case TARGET_NR_syscall:
4696 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4697 break;
4698#endif
bellard31e31b82003-02-18 22:55:36 +00004699 case TARGET_NR_wait4:
4700 {
4701 int status;
blueswir1992f48a2007-10-14 16:27:31 +00004702 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00004703 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00004704 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00004705 if (target_rusage)
4706 rusage_ptr = &rusage;
4707 else
4708 rusage_ptr = NULL;
4709 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4710 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00004711 if (status_ptr) {
4712 if (put_user_s32(status, status_ptr))
4713 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004714 }
bellard2f619692007-11-16 10:46:05 +00004715 if (target_rusage)
4716 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00004717 }
4718 }
4719 break;
thse5febef2007-04-01 18:31:35 +00004720#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00004721 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00004722 if (!(p = lock_user_string(arg1)))
4723 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004724 ret = get_errno(swapoff(p));
4725 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004726 break;
thse5febef2007-04-01 18:31:35 +00004727#endif
bellard31e31b82003-02-18 22:55:36 +00004728 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00004729 {
pbrook53a59602006-03-25 19:31:22 +00004730 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00004731 struct sysinfo value;
4732 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00004733 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00004734 {
bellard579a97f2007-11-11 14:26:47 +00004735 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4736 goto efault;
bellarda5448a72004-06-19 16:59:03 +00004737 __put_user(value.uptime, &target_value->uptime);
4738 __put_user(value.loads[0], &target_value->loads[0]);
4739 __put_user(value.loads[1], &target_value->loads[1]);
4740 __put_user(value.loads[2], &target_value->loads[2]);
4741 __put_user(value.totalram, &target_value->totalram);
4742 __put_user(value.freeram, &target_value->freeram);
4743 __put_user(value.sharedram, &target_value->sharedram);
4744 __put_user(value.bufferram, &target_value->bufferram);
4745 __put_user(value.totalswap, &target_value->totalswap);
4746 __put_user(value.freeswap, &target_value->freeswap);
4747 __put_user(value.procs, &target_value->procs);
4748 __put_user(value.totalhigh, &target_value->totalhigh);
4749 __put_user(value.freehigh, &target_value->freehigh);
4750 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00004751 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00004752 }
4753 }
4754 break;
thse5febef2007-04-01 18:31:35 +00004755#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00004756 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00004757 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4758 break;
thse5febef2007-04-01 18:31:35 +00004759#endif
bellard31e31b82003-02-18 22:55:36 +00004760 case TARGET_NR_fsync:
4761 ret = get_errno(fsync(arg1));
4762 break;
bellard31e31b82003-02-18 22:55:36 +00004763 case TARGET_NR_clone:
aurel320b6d3ae2008-09-15 07:43:43 +00004764#if defined(TARGET_SH4)
4765 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
4766#else
pbrookd865bab2008-06-07 22:12:17 +00004767 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00004768#endif
bellard1b6b0292003-03-22 17:31:38 +00004769 break;
bellardec86b0f2003-04-11 00:15:04 +00004770#ifdef __NR_exit_group
4771 /* new thread calls */
4772 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00004773 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00004774 ret = get_errno(exit_group(arg1));
4775 break;
4776#endif
bellard31e31b82003-02-18 22:55:36 +00004777 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00004778 if (!(p = lock_user_string(arg1)))
4779 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004780 ret = get_errno(setdomainname(p, arg2));
4781 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004782 break;
4783 case TARGET_NR_uname:
4784 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00004785 {
4786 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00004787
bellard579a97f2007-11-11 14:26:47 +00004788 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4789 goto efault;
bellard29e619b2004-09-13 21:41:04 +00004790 ret = get_errno(sys_uname(buf));
4791 if (!is_error(ret)) {
4792 /* Overrite the native machine name with whatever is being
4793 emulated. */
4794 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00004795 /* Allow the user to override the reported release. */
4796 if (qemu_uname_release && *qemu_uname_release)
4797 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00004798 }
pbrook53a59602006-03-25 19:31:22 +00004799 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00004800 }
bellard31e31b82003-02-18 22:55:36 +00004801 break;
bellard6dbad632003-03-16 18:05:05 +00004802#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00004803 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00004804 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00004805 break;
j_mayer84409dd2007-04-06 08:56:50 +00004806#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00004807 case TARGET_NR_vm86old:
4808 goto unimplemented;
4809 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00004810 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00004811 break;
4812#endif
j_mayer84409dd2007-04-06 08:56:50 +00004813#endif
bellard31e31b82003-02-18 22:55:36 +00004814 case TARGET_NR_adjtimex:
4815 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004816#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00004817 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00004818#endif
bellard31e31b82003-02-18 22:55:36 +00004819 case TARGET_NR_init_module:
4820 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00004821#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00004822 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00004823#endif
bellard31e31b82003-02-18 22:55:36 +00004824 goto unimplemented;
4825 case TARGET_NR_quotactl:
4826 goto unimplemented;
4827 case TARGET_NR_getpgid:
4828 ret = get_errno(getpgid(arg1));
4829 break;
4830 case TARGET_NR_fchdir:
4831 ret = get_errno(fchdir(arg1));
4832 break;
j_mayer84409dd2007-04-06 08:56:50 +00004833#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004834 case TARGET_NR_bdflush:
4835 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004836#endif
thse5febef2007-04-01 18:31:35 +00004837#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004838 case TARGET_NR_sysfs:
4839 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004840#endif
bellard31e31b82003-02-18 22:55:36 +00004841 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004842 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004843 break;
thse5febef2007-04-01 18:31:35 +00004844#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004845 case TARGET_NR_afs_syscall:
4846 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004847#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004848#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004849 case TARGET_NR__llseek:
4850 {
bellard4f2ac232004-04-26 19:44:02 +00004851#if defined (__x86_64__)
4852 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
bellard2f619692007-11-16 10:46:05 +00004853 if (put_user_s64(ret, arg4))
4854 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00004855#else
bellard31e31b82003-02-18 22:55:36 +00004856 int64_t res;
4857 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard2f619692007-11-16 10:46:05 +00004858 if (put_user_s64(res, arg4))
4859 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00004860#endif
bellard31e31b82003-02-18 22:55:36 +00004861 }
4862 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004863#endif
bellard31e31b82003-02-18 22:55:36 +00004864 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00004865#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00004866 goto unimplemented;
blueswir1992f48a2007-10-14 16:27:31 +00004867#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00004868 {
pbrook53a59602006-03-25 19:31:22 +00004869 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004870 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004871 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004872
4873 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00004874 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00004875 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00004876 goto fail;
4877 }
ths3b46e622007-09-17 08:09:54 +00004878
bellard4add45b2003-06-05 01:52:59 +00004879 ret = get_errno(sys_getdents(arg1, dirp, count));
4880 if (!is_error(ret)) {
4881 struct dirent *de;
4882 struct target_dirent *tde;
4883 int len = ret;
4884 int reclen, treclen;
4885 int count1, tnamelen;
4886
4887 count1 = 0;
4888 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00004889 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4890 goto efault;
bellard4add45b2003-06-05 01:52:59 +00004891 tde = target_dirp;
4892 while (len > 0) {
4893 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00004894 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00004895 tde->d_reclen = tswap16(treclen);
4896 tde->d_ino = tswapl(de->d_ino);
4897 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00004898 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00004899 if (tnamelen > 256)
4900 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004901 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004902 strncpy(tde->d_name, de->d_name, tnamelen);
4903 de = (struct dirent *)((char *)de + reclen);
4904 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004905 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004906 count1 += treclen;
4907 }
4908 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00004909 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004910 }
4911 free(dirp);
4912 }
4913#else
bellard31e31b82003-02-18 22:55:36 +00004914 {
pbrook53a59602006-03-25 19:31:22 +00004915 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004916 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004917
bellard579a97f2007-11-11 14:26:47 +00004918 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4919 goto efault;
bellard72f03902003-02-18 23:33:18 +00004920 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004921 if (!is_error(ret)) {
4922 struct dirent *de;
4923 int len = ret;
4924 int reclen;
4925 de = dirp;
4926 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004927 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004928 if (reclen > len)
4929 break;
bellard8083a3e2003-03-24 23:12:16 +00004930 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004931 tswapls(&de->d_ino);
4932 tswapls(&de->d_off);
4933 de = (struct dirent *)((char *)de + reclen);
4934 len -= reclen;
4935 }
4936 }
pbrook53a59602006-03-25 19:31:22 +00004937 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004938 }
bellard4add45b2003-06-05 01:52:59 +00004939#endif
bellard31e31b82003-02-18 22:55:36 +00004940 break;
ths3ae43202007-09-16 21:39:48 +00004941#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004942 case TARGET_NR_getdents64:
4943 {
pbrook53a59602006-03-25 19:31:22 +00004944 struct dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004945 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00004946 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4947 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00004948 ret = get_errno(sys_getdents64(arg1, dirp, count));
4949 if (!is_error(ret)) {
4950 struct dirent64 *de;
4951 int len = ret;
4952 int reclen;
4953 de = dirp;
4954 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004955 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004956 if (reclen > len)
4957 break;
bellard8083a3e2003-03-24 23:12:16 +00004958 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00004959 tswap64s((uint64_t *)&de->d_ino);
4960 tswap64s((uint64_t *)&de->d_off);
bellarddab2ed92003-03-22 15:23:14 +00004961 de = (struct dirent64 *)((char *)de + reclen);
4962 len -= reclen;
4963 }
4964 }
pbrook53a59602006-03-25 19:31:22 +00004965 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004966 }
4967 break;
bellarda541f292004-04-12 20:39:29 +00004968#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004969#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004970 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004971 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004972 break;
thse5febef2007-04-01 18:31:35 +00004973#endif
4974#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004975 case TARGET_NR_poll:
4976 {
pbrook53a59602006-03-25 19:31:22 +00004977 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004978 unsigned int nfds = arg2;
4979 int timeout = arg3;
4980 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004981 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004982
bellard579a97f2007-11-11 14:26:47 +00004983 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4984 if (!target_pfd)
4985 goto efault;
bellard9de5e442003-03-23 16:49:39 +00004986 pfd = alloca(sizeof(struct pollfd) * nfds);
4987 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004988 pfd[i].fd = tswap32(target_pfd[i].fd);
4989 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004990 }
4991 ret = get_errno(poll(pfd, nfds, timeout));
4992 if (!is_error(ret)) {
4993 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004994 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004995 }
pbrook53a59602006-03-25 19:31:22 +00004996 ret += nfds * (sizeof(struct target_pollfd)
4997 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004998 }
pbrook53a59602006-03-25 19:31:22 +00004999 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00005000 }
5001 break;
thse5febef2007-04-01 18:31:35 +00005002#endif
bellard31e31b82003-02-18 22:55:36 +00005003 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00005004 /* NOTE: the flock constant seems to be the same for every
5005 Linux platform */
5006 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005007 break;
5008 case TARGET_NR_readv:
5009 {
5010 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00005011 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00005012
5013 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00005014 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
5015 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005016 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00005017 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00005018 }
5019 break;
5020 case TARGET_NR_writev:
5021 {
5022 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00005023 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00005024
5025 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00005026 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
5027 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005028 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00005029 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00005030 }
5031 break;
5032 case TARGET_NR_getsid:
5033 ret = get_errno(getsid(arg1));
5034 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005035#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00005036 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00005037 ret = get_errno(fdatasync(arg1));
5038 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005039#endif
bellard31e31b82003-02-18 22:55:36 +00005040 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00005041 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00005042 return value. */
ths0da46a62007-10-20 20:23:07 +00005043 ret = -TARGET_ENOTDIR;
5044 break;
bellard31e31b82003-02-18 22:55:36 +00005045 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00005046 {
pbrook53a59602006-03-25 19:31:22 +00005047 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00005048 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00005049
bellard579a97f2007-11-11 14:26:47 +00005050 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
5051 goto efault;
bellard5cd43932003-03-29 16:54:36 +00005052 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00005053 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00005054 ret = get_errno(sched_setparam(arg1, &schp));
5055 }
5056 break;
bellard31e31b82003-02-18 22:55:36 +00005057 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00005058 {
pbrook53a59602006-03-25 19:31:22 +00005059 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00005060 struct sched_param schp;
5061 ret = get_errno(sched_getparam(arg1, &schp));
5062 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005063 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
5064 goto efault;
bellard5cd43932003-03-29 16:54:36 +00005065 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00005066 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00005067 }
5068 }
5069 break;
bellard31e31b82003-02-18 22:55:36 +00005070 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00005071 {
pbrook53a59602006-03-25 19:31:22 +00005072 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00005073 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00005074 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
5075 goto efault;
bellard5cd43932003-03-29 16:54:36 +00005076 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00005077 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00005078 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
5079 }
5080 break;
bellard31e31b82003-02-18 22:55:36 +00005081 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00005082 ret = get_errno(sched_getscheduler(arg1));
5083 break;
bellard31e31b82003-02-18 22:55:36 +00005084 case TARGET_NR_sched_yield:
5085 ret = get_errno(sched_yield());
5086 break;
5087 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00005088 ret = get_errno(sched_get_priority_max(arg1));
5089 break;
bellard31e31b82003-02-18 22:55:36 +00005090 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00005091 ret = get_errno(sched_get_priority_min(arg1));
5092 break;
bellard31e31b82003-02-18 22:55:36 +00005093 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00005094 {
bellard5cd43932003-03-29 16:54:36 +00005095 struct timespec ts;
5096 ret = get_errno(sched_rr_get_interval(arg1, &ts));
5097 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005098 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00005099 }
5100 }
5101 break;
bellard31e31b82003-02-18 22:55:36 +00005102 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00005103 {
bellard1b6b0292003-03-22 17:31:38 +00005104 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00005105 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00005106 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00005107 if (is_error(ret) && arg2) {
5108 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00005109 }
5110 }
5111 break;
thse5febef2007-04-01 18:31:35 +00005112#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00005113 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00005114 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005115#endif
5116#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00005117 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00005118 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005119#endif
bellard31e31b82003-02-18 22:55:36 +00005120 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00005121 switch (arg1)
5122 {
5123 case PR_GET_PDEATHSIG:
5124 {
5125 int deathsig;
5126 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00005127 if (!is_error(ret) && arg2
5128 && put_user_ual(deathsig, arg2))
5129 goto efault;
thse5574482007-02-11 20:03:13 +00005130 }
5131 break;
5132 default:
5133 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
5134 break;
5135 }
ths39b9aae2007-02-11 18:36:44 +00005136 break;
bellardd2fd1af2007-11-14 18:08:56 +00005137#ifdef TARGET_NR_arch_prctl
5138 case TARGET_NR_arch_prctl:
5139#if defined(TARGET_I386) && !defined(TARGET_ABI32)
5140 ret = do_arch_prctl(cpu_env, arg1, arg2);
5141 break;
5142#else
5143 goto unimplemented;
5144#endif
5145#endif
bellard67867302003-11-23 17:05:30 +00005146#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00005147 case TARGET_NR_pread:
balroga4ae00b2008-09-20 03:14:14 +00005148#ifdef TARGET_ARM
5149 if (((CPUARMState *)cpu_env)->eabi)
5150 arg4 = arg5;
5151#endif
bellard579a97f2007-11-11 14:26:47 +00005152 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5153 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005154 ret = get_errno(pread(arg1, p, arg3, arg4));
5155 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00005156 break;
bellard31e31b82003-02-18 22:55:36 +00005157 case TARGET_NR_pwrite:
balroga4ae00b2008-09-20 03:14:14 +00005158#ifdef TARGET_ARM
5159 if (((CPUARMState *)cpu_env)->eabi)
5160 arg4 = arg5;
5161#endif
bellard579a97f2007-11-11 14:26:47 +00005162 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5163 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005164 ret = get_errno(pwrite(arg1, p, arg3, arg4));
5165 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00005166 break;
bellard67867302003-11-23 17:05:30 +00005167#endif
aurel32f2c7ba12008-03-28 22:32:06 +00005168#ifdef TARGET_NR_pread64
5169 case TARGET_NR_pread64:
5170 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5171 goto efault;
5172 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
5173 unlock_user(p, arg2, ret);
5174 break;
5175 case TARGET_NR_pwrite64:
5176 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5177 goto efault;
5178 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
5179 unlock_user(p, arg2, 0);
5180 break;
5181#endif
bellard31e31b82003-02-18 22:55:36 +00005182 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00005183 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
5184 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005185 ret = get_errno(sys_getcwd1(p, arg2));
5186 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00005187 break;
5188 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00005189 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00005190 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00005191 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00005192 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00005193#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
5194 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
bellard579a97f2007-11-11 14:26:47 +00005195 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00005196 break;
5197#else
bellard5cd43932003-03-29 16:54:36 +00005198 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00005199#endif
bellard31e31b82003-02-18 22:55:36 +00005200 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00005201 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005202#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00005203 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00005204 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005205#endif
5206#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00005207 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00005208 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005209#endif
bellard048f6b42005-11-26 18:47:20 +00005210#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00005211 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00005212 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
5213 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00005214 break;
bellard048f6b42005-11-26 18:47:20 +00005215#endif
bellardebc05482003-09-30 21:08:41 +00005216#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00005217 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00005218 {
5219 struct rlimit rlim;
5220 ret = get_errno(getrlimit(arg1, &rlim));
5221 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005222 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00005223 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5224 goto efault;
bellard728584b2003-04-29 20:43:36 +00005225 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
5226 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005227 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00005228 }
5229 break;
5230 }
bellardebc05482003-09-30 21:08:41 +00005231#endif
bellarda315a142005-01-30 22:59:18 +00005232#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00005233 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00005234 if (!(p = lock_user_string(arg1)))
5235 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005236 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
5237 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00005238 break;
bellarda315a142005-01-30 22:59:18 +00005239#endif
5240#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00005241 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00005242 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00005243 break;
bellarda315a142005-01-30 22:59:18 +00005244#endif
5245#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00005246 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00005247 if (!(p = lock_user_string(arg1)))
5248 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005249 ret = get_errno(stat(path(p), &st));
5250 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00005251 if (!is_error(ret))
5252 ret = host_to_target_stat64(cpu_env, arg2, &st);
5253 break;
bellarda315a142005-01-30 22:59:18 +00005254#endif
5255#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00005256 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00005257 if (!(p = lock_user_string(arg1)))
5258 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005259 ret = get_errno(lstat(path(p), &st));
5260 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00005261 if (!is_error(ret))
5262 ret = host_to_target_stat64(cpu_env, arg2, &st);
5263 break;
bellarda315a142005-01-30 22:59:18 +00005264#endif
5265#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00005266 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00005267 ret = get_errno(fstat(arg1, &st));
5268 if (!is_error(ret))
5269 ret = host_to_target_stat64(cpu_env, arg2, &st);
5270 break;
bellardec86b0f2003-04-11 00:15:04 +00005271#endif
balrog6a24a772008-09-20 02:23:36 +00005272#if defined(TARGET_NR_fstatat64) && defined(__NR_fstatat64)
5273 case TARGET_NR_fstatat64:
5274 if (!(p = lock_user_string(arg2)))
5275 goto efault;
5276 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
5277 if (!is_error(ret))
5278 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00005279 break;
bellarda315a142005-01-30 22:59:18 +00005280#endif
bellard67867302003-11-23 17:05:30 +00005281#ifdef USE_UID16
5282 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00005283 if (!(p = lock_user_string(arg1)))
5284 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005285 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5286 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005287 break;
5288 case TARGET_NR_getuid:
5289 ret = get_errno(high2lowuid(getuid()));
5290 break;
5291 case TARGET_NR_getgid:
5292 ret = get_errno(high2lowgid(getgid()));
5293 break;
5294 case TARGET_NR_geteuid:
5295 ret = get_errno(high2lowuid(geteuid()));
5296 break;
5297 case TARGET_NR_getegid:
5298 ret = get_errno(high2lowgid(getegid()));
5299 break;
5300 case TARGET_NR_setreuid:
5301 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5302 break;
5303 case TARGET_NR_setregid:
5304 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5305 break;
5306 case TARGET_NR_getgroups:
5307 {
5308 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005309 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005310 gid_t *grouplist;
5311 int i;
5312
5313 grouplist = alloca(gidsetsize * sizeof(gid_t));
5314 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00005315 if (gidsetsize == 0)
5316 break;
bellard67867302003-11-23 17:05:30 +00005317 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005318 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5319 if (!target_grouplist)
5320 goto efault;
balroga2155fc2008-09-20 02:12:08 +00005321 for(i = 0;i < ret; i++)
bellard67867302003-11-23 17:05:30 +00005322 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005323 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00005324 }
5325 }
5326 break;
5327 case TARGET_NR_setgroups:
5328 {
5329 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005330 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005331 gid_t *grouplist;
5332 int i;
5333
5334 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00005335 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5336 if (!target_grouplist) {
5337 ret = -TARGET_EFAULT;
5338 goto fail;
5339 }
bellard67867302003-11-23 17:05:30 +00005340 for(i = 0;i < gidsetsize; i++)
5341 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005342 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00005343 ret = get_errno(setgroups(gidsetsize, grouplist));
5344 }
5345 break;
5346 case TARGET_NR_fchown:
5347 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5348 break;
thsccfa72b2007-09-24 09:23:34 +00005349#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5350 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00005351 if (!(p = lock_user_string(arg2)))
5352 goto efault;
5353 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5354 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00005355 break;
5356#endif
bellard67867302003-11-23 17:05:30 +00005357#ifdef TARGET_NR_setresuid
5358 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00005359 ret = get_errno(setresuid(low2highuid(arg1),
5360 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00005361 low2highuid(arg3)));
5362 break;
5363#endif
5364#ifdef TARGET_NR_getresuid
5365 case TARGET_NR_getresuid:
5366 {
pbrook53a59602006-03-25 19:31:22 +00005367 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00005368 ret = get_errno(getresuid(&ruid, &euid, &suid));
5369 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005370 if (put_user_u16(high2lowuid(ruid), arg1)
5371 || put_user_u16(high2lowuid(euid), arg2)
5372 || put_user_u16(high2lowuid(suid), arg3))
5373 goto efault;
bellard67867302003-11-23 17:05:30 +00005374 }
5375 }
5376 break;
5377#endif
5378#ifdef TARGET_NR_getresgid
5379 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00005380 ret = get_errno(setresgid(low2highgid(arg1),
5381 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00005382 low2highgid(arg3)));
5383 break;
5384#endif
5385#ifdef TARGET_NR_getresgid
5386 case TARGET_NR_getresgid:
5387 {
pbrook53a59602006-03-25 19:31:22 +00005388 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00005389 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5390 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005391 if (put_user_u16(high2lowgid(rgid), arg1)
5392 || put_user_u16(high2lowgid(egid), arg2)
5393 || put_user_u16(high2lowgid(sgid), arg3))
5394 goto efault;
bellard67867302003-11-23 17:05:30 +00005395 }
5396 }
5397 break;
5398#endif
5399 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00005400 if (!(p = lock_user_string(arg1)))
5401 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005402 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5403 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005404 break;
5405 case TARGET_NR_setuid:
5406 ret = get_errno(setuid(low2highuid(arg1)));
5407 break;
5408 case TARGET_NR_setgid:
5409 ret = get_errno(setgid(low2highgid(arg1)));
5410 break;
5411 case TARGET_NR_setfsuid:
5412 ret = get_errno(setfsuid(arg1));
5413 break;
5414 case TARGET_NR_setfsgid:
5415 ret = get_errno(setfsgid(arg1));
5416 break;
5417#endif /* USE_UID16 */
5418
bellarda315a142005-01-30 22:59:18 +00005419#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00005420 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00005421 if (!(p = lock_user_string(arg1)))
5422 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005423 ret = get_errno(lchown(p, arg2, arg3));
5424 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00005425 break;
bellarda315a142005-01-30 22:59:18 +00005426#endif
5427#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00005428 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00005429 ret = get_errno(getuid());
5430 break;
bellarda315a142005-01-30 22:59:18 +00005431#endif
5432#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00005433 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00005434 ret = get_errno(getgid());
5435 break;
bellarda315a142005-01-30 22:59:18 +00005436#endif
5437#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00005438 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00005439 ret = get_errno(geteuid());
5440 break;
bellarda315a142005-01-30 22:59:18 +00005441#endif
5442#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00005443 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00005444 ret = get_errno(getegid());
5445 break;
bellarda315a142005-01-30 22:59:18 +00005446#endif
5447#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00005448 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00005449 ret = get_errno(setreuid(arg1, arg2));
5450 break;
bellarda315a142005-01-30 22:59:18 +00005451#endif
5452#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00005453 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00005454 ret = get_errno(setregid(arg1, arg2));
5455 break;
bellarda315a142005-01-30 22:59:18 +00005456#endif
5457#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00005458 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00005459 {
5460 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005461 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00005462 gid_t *grouplist;
5463 int i;
5464
5465 grouplist = alloca(gidsetsize * sizeof(gid_t));
5466 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00005467 if (gidsetsize == 0)
5468 break;
bellard99c475a2005-01-31 20:45:13 +00005469 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005470 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
5471 if (!target_grouplist) {
5472 ret = -TARGET_EFAULT;
5473 goto fail;
5474 }
balroga2155fc2008-09-20 02:12:08 +00005475 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00005476 target_grouplist[i] = tswap32(grouplist[i]);
5477 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00005478 }
5479 }
5480 break;
bellarda315a142005-01-30 22:59:18 +00005481#endif
5482#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00005483 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00005484 {
5485 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005486 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00005487 gid_t *grouplist;
5488 int i;
ths3b46e622007-09-17 08:09:54 +00005489
bellard99c475a2005-01-31 20:45:13 +00005490 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00005491 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
5492 if (!target_grouplist) {
5493 ret = -TARGET_EFAULT;
5494 goto fail;
5495 }
bellard99c475a2005-01-31 20:45:13 +00005496 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00005497 grouplist[i] = tswap32(target_grouplist[i]);
5498 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00005499 ret = get_errno(setgroups(gidsetsize, grouplist));
5500 }
5501 break;
bellarda315a142005-01-30 22:59:18 +00005502#endif
5503#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00005504 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00005505 ret = get_errno(fchown(arg1, arg2, arg3));
5506 break;
bellarda315a142005-01-30 22:59:18 +00005507#endif
5508#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00005509 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00005510 ret = get_errno(setresuid(arg1, arg2, arg3));
5511 break;
bellarda315a142005-01-30 22:59:18 +00005512#endif
5513#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00005514 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00005515 {
pbrook53a59602006-03-25 19:31:22 +00005516 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00005517 ret = get_errno(getresuid(&ruid, &euid, &suid));
5518 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005519 if (put_user_u32(ruid, arg1)
5520 || put_user_u32(euid, arg2)
5521 || put_user_u32(suid, arg3))
5522 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00005523 }
5524 }
5525 break;
bellarda315a142005-01-30 22:59:18 +00005526#endif
5527#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00005528 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00005529 ret = get_errno(setresgid(arg1, arg2, arg3));
5530 break;
bellarda315a142005-01-30 22:59:18 +00005531#endif
5532#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00005533 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00005534 {
pbrook53a59602006-03-25 19:31:22 +00005535 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00005536 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5537 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005538 if (put_user_u32(rgid, arg1)
5539 || put_user_u32(egid, arg2)
5540 || put_user_u32(sgid, arg3))
5541 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00005542 }
5543 }
5544 break;
bellarda315a142005-01-30 22:59:18 +00005545#endif
5546#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00005547 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00005548 if (!(p = lock_user_string(arg1)))
5549 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005550 ret = get_errno(chown(p, arg2, arg3));
5551 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00005552 break;
bellarda315a142005-01-30 22:59:18 +00005553#endif
5554#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00005555 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00005556 ret = get_errno(setuid(arg1));
5557 break;
bellarda315a142005-01-30 22:59:18 +00005558#endif
5559#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00005560 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00005561 ret = get_errno(setgid(arg1));
5562 break;
bellarda315a142005-01-30 22:59:18 +00005563#endif
5564#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00005565 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00005566 ret = get_errno(setfsuid(arg1));
5567 break;
bellarda315a142005-01-30 22:59:18 +00005568#endif
5569#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00005570 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00005571 ret = get_errno(setfsgid(arg1));
5572 break;
bellarda315a142005-01-30 22:59:18 +00005573#endif
bellard67867302003-11-23 17:05:30 +00005574
bellard31e31b82003-02-18 22:55:36 +00005575 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00005576 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00005577#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00005578 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00005579 {
5580 void *a;
5581 ret = -TARGET_EFAULT;
5582 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
5583 goto efault;
5584 if (!(p = lock_user_string(arg3)))
5585 goto mincore_fail;
5586 ret = get_errno(mincore(a, arg2, p));
5587 unlock_user(p, arg3, ret);
5588 mincore_fail:
5589 unlock_user(a, arg1, 0);
5590 }
5591 break;
bellardffa65c32004-01-04 23:57:22 +00005592#endif
aurel32408321b2008-10-01 21:46:32 +00005593#ifdef TARGET_NR_arm_fadvise64_64
5594 case TARGET_NR_arm_fadvise64_64:
5595 {
5596 /*
5597 * arm_fadvise64_64 looks like fadvise64_64 but
5598 * with different argument order
5599 */
5600 abi_long temp;
5601 temp = arg3;
5602 arg3 = arg4;
5603 arg4 = temp;
5604 }
5605#endif
5606#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64)
5607#ifdef TARGET_NR_fadvise64_64
5608 case TARGET_NR_fadvise64_64:
5609#endif
5610 /* This is a hint, so ignoring and returning success is ok. */
5611 ret = get_errno(0);
5612 break;
5613#endif
bellardffa65c32004-01-04 23:57:22 +00005614#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00005615 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00005616 /* A straight passthrough may not be safe because qemu sometimes
5617 turns private flie-backed mappings into anonymous mappings.
5618 This will break MADV_DONTNEED.
5619 This is a hint, so ignoring and returning success is ok. */
5620 ret = get_errno(0);
5621 break;
bellardffa65c32004-01-04 23:57:22 +00005622#endif
blueswir1992f48a2007-10-14 16:27:31 +00005623#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00005624 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00005625 {
thsb1e341e2007-03-20 21:50:52 +00005626 int cmd;
bellard77e46722003-04-29 20:39:06 +00005627 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00005628 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00005629#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00005630 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00005631#endif
bellard77e46722003-04-29 20:39:06 +00005632
thsb1e341e2007-03-20 21:50:52 +00005633 switch(arg2){
5634 case TARGET_F_GETLK64:
5635 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00005636 break;
thsb1e341e2007-03-20 21:50:52 +00005637 case TARGET_F_SETLK64:
5638 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005639 break;
thsb1e341e2007-03-20 21:50:52 +00005640 case TARGET_F_SETLKW64:
5641 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005642 break;
thsb1e341e2007-03-20 21:50:52 +00005643 default:
5644 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00005645 break;
thsb1e341e2007-03-20 21:50:52 +00005646 }
5647
bellard60cd49d2003-03-16 22:53:56 +00005648 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00005649 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00005650#ifdef TARGET_ARM
5651 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005652 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5653 goto efault;
ths58134272007-03-31 18:59:32 +00005654 fl.l_type = tswap16(target_efl->l_type);
5655 fl.l_whence = tswap16(target_efl->l_whence);
5656 fl.l_start = tswap64(target_efl->l_start);
5657 fl.l_len = tswap64(target_efl->l_len);
5658 fl.l_pid = tswapl(target_efl->l_pid);
5659 unlock_user_struct(target_efl, arg3, 0);
5660 } else
5661#endif
5662 {
bellard9ee1fa22007-11-11 15:11:19 +00005663 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5664 goto efault;
ths58134272007-03-31 18:59:32 +00005665 fl.l_type = tswap16(target_fl->l_type);
5666 fl.l_whence = tswap16(target_fl->l_whence);
5667 fl.l_start = tswap64(target_fl->l_start);
5668 fl.l_len = tswap64(target_fl->l_len);
5669 fl.l_pid = tswapl(target_fl->l_pid);
5670 unlock_user_struct(target_fl, arg3, 0);
5671 }
thsb1e341e2007-03-20 21:50:52 +00005672 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005673 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00005674#ifdef TARGET_ARM
5675 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005676 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5677 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005678 target_efl->l_type = tswap16(fl.l_type);
5679 target_efl->l_whence = tswap16(fl.l_whence);
5680 target_efl->l_start = tswap64(fl.l_start);
5681 target_efl->l_len = tswap64(fl.l_len);
5682 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005683 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005684 } else
5685#endif
5686 {
bellard9ee1fa22007-11-11 15:11:19 +00005687 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5688 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005689 target_fl->l_type = tswap16(fl.l_type);
5690 target_fl->l_whence = tswap16(fl.l_whence);
5691 target_fl->l_start = tswap64(fl.l_start);
5692 target_fl->l_len = tswap64(fl.l_len);
5693 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005694 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005695 }
bellard77e46722003-04-29 20:39:06 +00005696 }
5697 break;
5698
thsb1e341e2007-03-20 21:50:52 +00005699 case TARGET_F_SETLK64:
5700 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00005701#ifdef TARGET_ARM
5702 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005703 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5704 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005705 fl.l_type = tswap16(target_efl->l_type);
5706 fl.l_whence = tswap16(target_efl->l_whence);
5707 fl.l_start = tswap64(target_efl->l_start);
5708 fl.l_len = tswap64(target_efl->l_len);
5709 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005710 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005711 } else
5712#endif
5713 {
bellard9ee1fa22007-11-11 15:11:19 +00005714 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5715 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005716 fl.l_type = tswap16(target_fl->l_type);
5717 fl.l_whence = tswap16(target_fl->l_whence);
5718 fl.l_start = tswap64(target_fl->l_start);
5719 fl.l_len = tswap64(target_fl->l_len);
5720 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005721 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005722 }
thsb1e341e2007-03-20 21:50:52 +00005723 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005724 break;
bellard60cd49d2003-03-16 22:53:56 +00005725 default:
bellard9ee1fa22007-11-11 15:11:19 +00005726 ret = do_fcntl(arg1, cmd, arg3);
bellard60cd49d2003-03-16 22:53:56 +00005727 break;
5728 }
bellard77e46722003-04-29 20:39:06 +00005729 break;
5730 }
bellard60cd49d2003-03-16 22:53:56 +00005731#endif
ths7d600c82006-12-08 01:32:58 +00005732#ifdef TARGET_NR_cacheflush
5733 case TARGET_NR_cacheflush:
5734 /* self-modifying code is handled automatically, so nothing needed */
5735 ret = 0;
5736 break;
5737#endif
bellardebc05482003-09-30 21:08:41 +00005738#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00005739 case TARGET_NR_security:
5740 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005741#endif
bellardc573ff62004-01-04 15:51:36 +00005742#ifdef TARGET_NR_getpagesize
5743 case TARGET_NR_getpagesize:
5744 ret = TARGET_PAGE_SIZE;
5745 break;
5746#endif
bellard31e31b82003-02-18 22:55:36 +00005747 case TARGET_NR_gettid:
5748 ret = get_errno(gettid());
5749 break;
thse5febef2007-04-01 18:31:35 +00005750#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00005751 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00005752 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005753#endif
bellardebc05482003-09-30 21:08:41 +00005754#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00005755 case TARGET_NR_setxattr:
5756 case TARGET_NR_lsetxattr:
5757 case TARGET_NR_fsetxattr:
5758 case TARGET_NR_getxattr:
5759 case TARGET_NR_lgetxattr:
5760 case TARGET_NR_fgetxattr:
5761 case TARGET_NR_listxattr:
5762 case TARGET_NR_llistxattr:
5763 case TARGET_NR_flistxattr:
5764 case TARGET_NR_removexattr:
5765 case TARGET_NR_lremovexattr:
5766 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00005767 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005768#endif
5769#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00005770 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00005771#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00005772 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5773 ret = 0;
5774 break;
bellard8d18e892007-11-14 15:18:40 +00005775#elif defined(TARGET_I386) && defined(TARGET_ABI32)
5776 ret = do_set_thread_area(cpu_env, arg1);
5777 break;
ths6f5b89a2007-03-02 20:48:00 +00005778#else
5779 goto unimplemented_nowarn;
5780#endif
5781#endif
5782#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00005783 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00005784#if defined(TARGET_I386) && defined(TARGET_ABI32)
5785 ret = do_get_thread_area(cpu_env, arg1);
5786#else
bellard5cd43932003-03-29 16:54:36 +00005787 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005788#endif
bellard8d18e892007-11-14 15:18:40 +00005789#endif
bellard48dc41e2006-06-21 18:15:50 +00005790#ifdef TARGET_NR_getdomainname
5791 case TARGET_NR_getdomainname:
5792 goto unimplemented_nowarn;
5793#endif
ths6f5b89a2007-03-02 20:48:00 +00005794
thsb5906f92007-03-19 13:32:45 +00005795#ifdef TARGET_NR_clock_gettime
5796 case TARGET_NR_clock_gettime:
5797 {
5798 struct timespec ts;
5799 ret = get_errno(clock_gettime(arg1, &ts));
5800 if (!is_error(ret)) {
5801 host_to_target_timespec(arg2, &ts);
5802 }
5803 break;
5804 }
5805#endif
5806#ifdef TARGET_NR_clock_getres
5807 case TARGET_NR_clock_getres:
5808 {
5809 struct timespec ts;
5810 ret = get_errno(clock_getres(arg1, &ts));
5811 if (!is_error(ret)) {
5812 host_to_target_timespec(arg2, &ts);
5813 }
5814 break;
5815 }
5816#endif
pbrook63d76512008-05-29 13:43:29 +00005817#ifdef TARGET_NR_clock_nanosleep
5818 case TARGET_NR_clock_nanosleep:
5819 {
5820 struct timespec ts;
5821 target_to_host_timespec(&ts, arg3);
5822 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
5823 if (arg4)
5824 host_to_target_timespec(arg4, &ts);
5825 break;
5826 }
5827#endif
thsb5906f92007-03-19 13:32:45 +00005828
ths6f5b89a2007-03-02 20:48:00 +00005829#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5830 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00005831 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5832 break;
ths6f5b89a2007-03-02 20:48:00 +00005833#endif
5834
ths3ae43202007-09-16 21:39:48 +00005835#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00005836 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00005837 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00005838 break;
5839#endif
5840
ths3ae43202007-09-16 21:39:48 +00005841#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00005842 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00005843 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
5844 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00005845 break;
5846#endif
5847
ths4f2b1fe2007-06-21 21:57:12 +00005848#ifdef TARGET_NR_set_robust_list
5849 case TARGET_NR_set_robust_list:
5850 goto unimplemented_nowarn;
5851#endif
5852
ths9007f0e2007-09-25 17:50:37 +00005853#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5854 case TARGET_NR_utimensat:
5855 {
5856 struct timespec ts[2];
5857 target_to_host_timespec(ts, arg3);
5858 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5859 if (!arg2)
5860 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5861 else {
bellard579a97f2007-11-11 14:26:47 +00005862 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00005863 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00005864 goto fail;
5865 }
5866 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5867 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00005868 }
5869 }
5870 break;
5871#endif
pbrookbd0c5662008-05-29 14:34:11 +00005872#if defined(USE_NPTL)
5873 case TARGET_NR_futex:
5874 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
5875 break;
5876#endif
ths9007f0e2007-09-25 17:50:37 +00005877
bellard31e31b82003-02-18 22:55:36 +00005878 default:
5879 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00005880 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00005881#if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
bellard5cd43932003-03-29 16:54:36 +00005882 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00005883#endif
ths0da46a62007-10-20 20:23:07 +00005884 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005885 break;
5886 }
bellard579a97f2007-11-11 14:26:47 +00005887fail:
bellardc573ff62004-01-04 15:51:36 +00005888#ifdef DEBUG
5889 gemu_log(" = %ld\n", ret);
5890#endif
thsb92c47c2007-11-01 00:07:38 +00005891 if(do_strace)
5892 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00005893 return ret;
bellard579a97f2007-11-11 14:26:47 +00005894efault:
5895 ret = -TARGET_EFAULT;
5896 goto fail;
bellard31e31b82003-02-18 22:55:36 +00005897}