blob: cde0dd3242abc068b24d4dab85979c05f4347bbc [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>
bellard31e31b82003-02-18 22:55:36 +000030#include <sys/types.h>
thsd08d3bb2007-03-19 13:09:22 +000031#include <sys/ipc.h>
32#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000033#include <sys/wait.h>
34#include <sys/time.h>
35#include <sys/stat.h>
36#include <sys/mount.h>
ths39b9aae2007-02-11 18:36:44 +000037#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000038#include <sys/resource.h>
39#include <sys/mman.h>
40#include <sys/swap.h>
41#include <signal.h>
42#include <sched.h>
43#include <sys/socket.h>
44#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000045#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000046#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000047#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000048#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000049#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000050#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000051#include <sys/sysinfo.h>
bellard72f03902003-02-18 23:33:18 +000052//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000053#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000054#include <netinet/tcp.h>
bellard31e31b82003-02-18 22:55:36 +000055
56#define termios host_termios
57#define winsize host_winsize
58#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000059#define sgttyb host_sgttyb /* same as target */
60#define tchars host_tchars /* same as target */
61#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000062
63#include <linux/termios.h>
64#include <linux/unistd.h>
65#include <linux/utsname.h>
66#include <linux/cdrom.h>
67#include <linux/hdreg.h>
68#include <linux/soundcard.h>
bellarddab2ed92003-03-22 15:23:14 +000069#include <linux/dirent.h>
bellard19b84f32003-05-08 15:41:49 +000070#include <linux/kd.h>
bellard31e31b82003-02-18 22:55:36 +000071
bellard3ef693a2003-03-23 20:17:16 +000072#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +000073
bellard72f03902003-02-18 23:33:18 +000074//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +000075
pbrooke6e59062006-10-22 00:18:54 +000076#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
ths56e07942007-04-26 23:49:45 +000077 || defined(TARGET_M68K) || defined(TARGET_SH4)
bellard67867302003-11-23 17:05:30 +000078/* 16 bit uid wrappers emulation */
79#define USE_UID16
80#endif
81
bellard1a9353d2003-03-16 20:28:50 +000082//#include <linux/msdos_fs.h>
83#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
84#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
85
bellard70a194b2003-08-11 22:20:16 +000086
bellard70a194b2003-08-11 22:20:16 +000087#undef _syscall0
88#undef _syscall1
89#undef _syscall2
90#undef _syscall3
91#undef _syscall4
92#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +000093#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +000094
bellard83fcb512006-06-14 13:37:16 +000095#define _syscall0(type,name) \
96type name (void) \
97{ \
98 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +000099}
100
bellard83fcb512006-06-14 13:37:16 +0000101#define _syscall1(type,name,type1,arg1) \
102type name (type1 arg1) \
103{ \
104 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000105}
106
bellard83fcb512006-06-14 13:37:16 +0000107#define _syscall2(type,name,type1,arg1,type2,arg2) \
108type name (type1 arg1,type2 arg2) \
109{ \
110 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000111}
112
bellard83fcb512006-06-14 13:37:16 +0000113#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
114type name (type1 arg1,type2 arg2,type3 arg3) \
115{ \
116 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000117}
118
bellard83fcb512006-06-14 13:37:16 +0000119#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
120type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
121{ \
122 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000123}
124
bellard83fcb512006-06-14 13:37:16 +0000125#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
126 type5,arg5) \
127type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
128{ \
129 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000130}
bellard83fcb512006-06-14 13:37:16 +0000131
132
133#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
134 type5,arg5,type6,arg6) \
135type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
136{ \
137 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
138}
139
bellard70a194b2003-08-11 22:20:16 +0000140
bellard31e31b82003-02-18 22:55:36 +0000141#define __NR_sys_uname __NR_uname
bellard72f03902003-02-18 23:33:18 +0000142#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000143#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000144#define __NR_sys_getdents64 __NR_getdents64
ths82424832007-09-24 09:21:55 +0000145#define __NR_sys_openat __NR_openat
bellard66fb9762003-03-23 01:06:05 +0000146#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
ths7494b0f2007-02-11 18:26:53 +0000147#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000148#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000149#define __NR_sys_tkill __NR_tkill
bellard31e31b82003-02-18 22:55:36 +0000150
bellardbc51c5c2004-03-17 23:46:04 +0000151#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
bellard9af9eaa2003-04-07 21:34:41 +0000152#define __NR__llseek __NR_lseek
153#endif
154
bellard72f03902003-02-18 23:33:18 +0000155#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000156_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000157#else
158static int gettid(void) {
159 return -ENOSYS;
160}
161#endif
bellard31e31b82003-02-18 22:55:36 +0000162_syscall1(int,sys_uname,struct new_utsname *,buf)
bellard72f03902003-02-18 23:33:18 +0000163_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
164_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000165#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +0000166_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000167#endif
bellard31e31b82003-02-18 22:55:36 +0000168_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
169 loff_t *, res, uint, wh);
ths82424832007-09-24 09:21:55 +0000170#if defined(TARGET_NR_openat) && defined(__NR_openat)
171_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
172#endif
bellard66fb9762003-03-23 01:06:05 +0000173_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
ths7494b0f2007-02-11 18:26:53 +0000174_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
ths3ae43202007-09-16 21:39:48 +0000175#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +0000176_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
ths4cae1d12007-07-12 11:06:53 +0000177#endif
ths3ae43202007-09-16 21:39:48 +0000178#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +0000179_syscall2(int,sys_tkill,int,tid,int,sig)
180#endif
bellardec86b0f2003-04-11 00:15:04 +0000181#ifdef __NR_exit_group
182_syscall1(int,exit_group,int,error_code)
183#endif
ths6f5b89a2007-03-02 20:48:00 +0000184#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
185_syscall1(int,set_tid_address,int *,tidptr)
186#endif
bellard66fb9762003-03-23 01:06:05 +0000187
188extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000189extern int flock(int, int);
190extern int setfsuid(int);
191extern int setfsgid(int);
bellard5cd43932003-03-29 16:54:36 +0000192extern int setresuid(uid_t, uid_t, uid_t);
193extern int getresuid(uid_t *, uid_t *, uid_t *);
194extern int setresgid(gid_t, gid_t, gid_t);
195extern int getresgid(gid_t *, gid_t *, gid_t *);
bellard19b84f32003-05-08 15:41:49 +0000196extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000197
ths637947f2007-06-01 12:09:19 +0000198/*
thsfe8f0962007-07-12 10:59:21 +0000199 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000200 * minus the errnos that are not actually generic to all archs.
201 */
202static uint16_t host_to_target_errno_table[1200] = {
203 [EIDRM] = TARGET_EIDRM,
204 [ECHRNG] = TARGET_ECHRNG,
205 [EL2NSYNC] = TARGET_EL2NSYNC,
206 [EL3HLT] = TARGET_EL3HLT,
207 [EL3RST] = TARGET_EL3RST,
208 [ELNRNG] = TARGET_ELNRNG,
209 [EUNATCH] = TARGET_EUNATCH,
210 [ENOCSI] = TARGET_ENOCSI,
211 [EL2HLT] = TARGET_EL2HLT,
212 [EDEADLK] = TARGET_EDEADLK,
213 [ENOLCK] = TARGET_ENOLCK,
214 [EBADE] = TARGET_EBADE,
215 [EBADR] = TARGET_EBADR,
216 [EXFULL] = TARGET_EXFULL,
217 [ENOANO] = TARGET_ENOANO,
218 [EBADRQC] = TARGET_EBADRQC,
219 [EBADSLT] = TARGET_EBADSLT,
220 [EBFONT] = TARGET_EBFONT,
221 [ENOSTR] = TARGET_ENOSTR,
222 [ENODATA] = TARGET_ENODATA,
223 [ETIME] = TARGET_ETIME,
224 [ENOSR] = TARGET_ENOSR,
225 [ENONET] = TARGET_ENONET,
226 [ENOPKG] = TARGET_ENOPKG,
227 [EREMOTE] = TARGET_EREMOTE,
228 [ENOLINK] = TARGET_ENOLINK,
229 [EADV] = TARGET_EADV,
230 [ESRMNT] = TARGET_ESRMNT,
231 [ECOMM] = TARGET_ECOMM,
232 [EPROTO] = TARGET_EPROTO,
233 [EDOTDOT] = TARGET_EDOTDOT,
234 [EMULTIHOP] = TARGET_EMULTIHOP,
235 [EBADMSG] = TARGET_EBADMSG,
236 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
237 [EOVERFLOW] = TARGET_EOVERFLOW,
238 [ENOTUNIQ] = TARGET_ENOTUNIQ,
239 [EBADFD] = TARGET_EBADFD,
240 [EREMCHG] = TARGET_EREMCHG,
241 [ELIBACC] = TARGET_ELIBACC,
242 [ELIBBAD] = TARGET_ELIBBAD,
243 [ELIBSCN] = TARGET_ELIBSCN,
244 [ELIBMAX] = TARGET_ELIBMAX,
245 [ELIBEXEC] = TARGET_ELIBEXEC,
246 [EILSEQ] = TARGET_EILSEQ,
247 [ENOSYS] = TARGET_ENOSYS,
248 [ELOOP] = TARGET_ELOOP,
249 [ERESTART] = TARGET_ERESTART,
250 [ESTRPIPE] = TARGET_ESTRPIPE,
251 [ENOTEMPTY] = TARGET_ENOTEMPTY,
252 [EUSERS] = TARGET_EUSERS,
253 [ENOTSOCK] = TARGET_ENOTSOCK,
254 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
255 [EMSGSIZE] = TARGET_EMSGSIZE,
256 [EPROTOTYPE] = TARGET_EPROTOTYPE,
257 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
258 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
259 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
260 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
261 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
262 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
263 [EADDRINUSE] = TARGET_EADDRINUSE,
264 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
265 [ENETDOWN] = TARGET_ENETDOWN,
266 [ENETUNREACH] = TARGET_ENETUNREACH,
267 [ENETRESET] = TARGET_ENETRESET,
268 [ECONNABORTED] = TARGET_ECONNABORTED,
269 [ECONNRESET] = TARGET_ECONNRESET,
270 [ENOBUFS] = TARGET_ENOBUFS,
271 [EISCONN] = TARGET_EISCONN,
272 [ENOTCONN] = TARGET_ENOTCONN,
273 [EUCLEAN] = TARGET_EUCLEAN,
274 [ENOTNAM] = TARGET_ENOTNAM,
275 [ENAVAIL] = TARGET_ENAVAIL,
276 [EISNAM] = TARGET_EISNAM,
277 [EREMOTEIO] = TARGET_EREMOTEIO,
278 [ESHUTDOWN] = TARGET_ESHUTDOWN,
279 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
280 [ETIMEDOUT] = TARGET_ETIMEDOUT,
281 [ECONNREFUSED] = TARGET_ECONNREFUSED,
282 [EHOSTDOWN] = TARGET_EHOSTDOWN,
283 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
284 [EALREADY] = TARGET_EALREADY,
285 [EINPROGRESS] = TARGET_EINPROGRESS,
286 [ESTALE] = TARGET_ESTALE,
287 [ECANCELED] = TARGET_ECANCELED,
288 [ENOMEDIUM] = TARGET_ENOMEDIUM,
289 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000290#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000291 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000292#endif
293#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000294 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000295#endif
296#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000297 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000298#endif
299#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000300 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000301#endif
302#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000303 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000304#endif
305#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000306 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000307#endif
ths637947f2007-06-01 12:09:19 +0000308 };
309
310static inline int host_to_target_errno(int err)
311{
312 if(host_to_target_errno_table[err])
313 return host_to_target_errno_table[err];
314 return err;
315}
316
bellard31e31b82003-02-18 22:55:36 +0000317static inline long get_errno(long ret)
318{
319 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000320 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000321 else
322 return ret;
323}
324
325static inline int is_error(long ret)
326{
327 return (unsigned long)ret >= (unsigned long)(-4096);
328}
329
pbrook53a59602006-03-25 19:31:22 +0000330static target_ulong target_brk;
331static target_ulong target_original_brk;
bellard31e31b82003-02-18 22:55:36 +0000332
pbrook53a59602006-03-25 19:31:22 +0000333void target_set_brk(target_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000334{
blueswir14c1de732007-07-07 20:45:44 +0000335 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
bellard31e31b82003-02-18 22:55:36 +0000336}
337
pbrook53a59602006-03-25 19:31:22 +0000338long do_brk(target_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000339{
pbrook53a59602006-03-25 19:31:22 +0000340 target_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000341 long mapped_addr;
342 int new_alloc_size;
343
344 if (!new_brk)
pbrook53a59602006-03-25 19:31:22 +0000345 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000346 if (new_brk < target_original_brk)
347 return -ENOMEM;
ths3b46e622007-09-17 08:09:54 +0000348
pbrook53a59602006-03-25 19:31:22 +0000349 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000350
351 /* If the new brk is less than this, set it and we're done... */
352 if (new_brk < brk_page) {
353 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000354 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000355 }
356
357 /* We need to allocate more memory after the brk... */
bellard54936002003-05-13 00:25:15 +0000358 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
ths5fafdf22007-09-16 21:08:06 +0000359 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000360 PROT_READ|PROT_WRITE,
361 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
bellard31e31b82003-02-18 22:55:36 +0000362 if (is_error(mapped_addr)) {
363 return mapped_addr;
364 } else {
365 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000366 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000367 }
368}
369
ths5fafdf22007-09-16 21:08:06 +0000370static inline fd_set *target_to_host_fds(fd_set *fds,
bellard31e31b82003-02-18 22:55:36 +0000371 target_long *target_fds, int n)
372{
bellard7854b052003-03-29 17:22:23 +0000373#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000374 return (fd_set *)target_fds;
375#else
376 int i, b;
377 if (target_fds) {
378 FD_ZERO(fds);
379 for(i = 0;i < n; i++) {
380 b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
381 (i & (TARGET_LONG_BITS - 1))) & 1;
382 if (b)
383 FD_SET(i, fds);
384 }
385 return fds;
386 } else {
387 return NULL;
388 }
389#endif
390}
391
ths5fafdf22007-09-16 21:08:06 +0000392static inline void host_to_target_fds(target_long *target_fds,
bellard31e31b82003-02-18 22:55:36 +0000393 fd_set *fds, int n)
394{
bellard7854b052003-03-29 17:22:23 +0000395#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000396 /* nothing to do */
397#else
398 int i, nw, j, k;
399 target_long v;
400
401 if (target_fds) {
bellard8853f862004-02-22 14:57:26 +0000402 nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
bellard31e31b82003-02-18 22:55:36 +0000403 k = 0;
404 for(i = 0;i < nw; i++) {
405 v = 0;
406 for(j = 0; j < TARGET_LONG_BITS; j++) {
407 v |= ((FD_ISSET(k, fds) != 0) << j);
408 k++;
409 }
410 target_fds[i] = tswapl(v);
411 }
412 }
413#endif
414}
415
bellardc596ed12003-07-13 17:32:31 +0000416#if defined(__alpha__)
417#define HOST_HZ 1024
418#else
419#define HOST_HZ 100
420#endif
421
422static inline long host_to_target_clock_t(long ticks)
423{
424#if HOST_HZ == TARGET_HZ
425 return ticks;
426#else
427 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
428#endif
429}
430
pbrook53a59602006-03-25 19:31:22 +0000431static inline void host_to_target_rusage(target_ulong target_addr,
bellardb4091862003-05-16 15:39:34 +0000432 const struct rusage *rusage)
433{
pbrook53a59602006-03-25 19:31:22 +0000434 struct target_rusage *target_rusage;
435
436 lock_user_struct(target_rusage, target_addr, 0);
bellardb4091862003-05-16 15:39:34 +0000437 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
438 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
439 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
440 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
441 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
442 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
443 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
444 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
445 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
446 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
447 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
448 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
449 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
450 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
451 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
452 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
453 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
454 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000455 unlock_user_struct(target_rusage, target_addr, 1);
bellardb4091862003-05-16 15:39:34 +0000456}
457
pbrook53a59602006-03-25 19:31:22 +0000458static inline void target_to_host_timeval(struct timeval *tv,
459 target_ulong target_addr)
bellard31e31b82003-02-18 22:55:36 +0000460{
pbrook53a59602006-03-25 19:31:22 +0000461 struct target_timeval *target_tv;
462
463 lock_user_struct(target_tv, target_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000464 tv->tv_sec = tswapl(target_tv->tv_sec);
465 tv->tv_usec = tswapl(target_tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000466 unlock_user_struct(target_tv, target_addr, 0);
bellard31e31b82003-02-18 22:55:36 +0000467}
468
pbrook53a59602006-03-25 19:31:22 +0000469static inline void host_to_target_timeval(target_ulong target_addr,
bellard5cd43932003-03-29 16:54:36 +0000470 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000471{
pbrook53a59602006-03-25 19:31:22 +0000472 struct target_timeval *target_tv;
473
474 lock_user_struct(target_tv, target_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000475 target_tv->tv_sec = tswapl(tv->tv_sec);
476 target_tv->tv_usec = tswapl(tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000477 unlock_user_struct(target_tv, target_addr, 1);
bellard31e31b82003-02-18 22:55:36 +0000478}
479
480
ths5fafdf22007-09-16 21:08:06 +0000481static long do_select(long n,
482 target_ulong rfd_p, target_ulong wfd_p,
pbrook53a59602006-03-25 19:31:22 +0000483 target_ulong efd_p, target_ulong target_tv)
bellard31e31b82003-02-18 22:55:36 +0000484{
485 fd_set rfds, wfds, efds;
486 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
pbrook53a59602006-03-25 19:31:22 +0000487 target_long *target_rfds, *target_wfds, *target_efds;
bellard31e31b82003-02-18 22:55:36 +0000488 struct timeval tv, *tv_ptr;
489 long ret;
pbrook53a59602006-03-25 19:31:22 +0000490 int ok;
bellard31e31b82003-02-18 22:55:36 +0000491
pbrook53a59602006-03-25 19:31:22 +0000492 if (rfd_p) {
493 target_rfds = lock_user(rfd_p, sizeof(target_long) * n, 1);
494 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
495 } else {
496 target_rfds = NULL;
497 rfds_ptr = NULL;
498 }
499 if (wfd_p) {
500 target_wfds = lock_user(wfd_p, sizeof(target_long) * n, 1);
501 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
502 } else {
503 target_wfds = NULL;
504 wfds_ptr = NULL;
505 }
506 if (efd_p) {
507 target_efds = lock_user(efd_p, sizeof(target_long) * n, 1);
508 efds_ptr = target_to_host_fds(&efds, target_efds, n);
509 } else {
510 target_efds = NULL;
511 efds_ptr = NULL;
512 }
ths3b46e622007-09-17 08:09:54 +0000513
bellard31e31b82003-02-18 22:55:36 +0000514 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000515 target_to_host_timeval(&tv, target_tv);
bellard31e31b82003-02-18 22:55:36 +0000516 tv_ptr = &tv;
517 } else {
518 tv_ptr = NULL;
519 }
520 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000521 ok = !is_error(ret);
522
523 if (ok) {
bellard31e31b82003-02-18 22:55:36 +0000524 host_to_target_fds(target_rfds, rfds_ptr, n);
525 host_to_target_fds(target_wfds, wfds_ptr, n);
526 host_to_target_fds(target_efds, efds_ptr, n);
527
528 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000529 host_to_target_timeval(target_tv, &tv);
bellard31e31b82003-02-18 22:55:36 +0000530 }
531 }
pbrook53a59602006-03-25 19:31:22 +0000532 if (target_rfds)
533 unlock_user(target_rfds, rfd_p, ok ? sizeof(target_long) * n : 0);
534 if (target_wfds)
535 unlock_user(target_wfds, wfd_p, ok ? sizeof(target_long) * n : 0);
536 if (target_efds)
537 unlock_user(target_efds, efd_p, ok ? sizeof(target_long) * n : 0);
538
bellard31e31b82003-02-18 22:55:36 +0000539 return ret;
540}
541
bellard7854b052003-03-29 17:22:23 +0000542static inline void target_to_host_sockaddr(struct sockaddr *addr,
pbrook53a59602006-03-25 19:31:22 +0000543 target_ulong target_addr,
bellard7854b052003-03-29 17:22:23 +0000544 socklen_t len)
545{
pbrook53a59602006-03-25 19:31:22 +0000546 struct target_sockaddr *target_saddr;
547
548 target_saddr = lock_user(target_addr, len, 1);
549 memcpy(addr, target_saddr, len);
550 addr->sa_family = tswap16(target_saddr->sa_family);
551 unlock_user(target_saddr, target_addr, 0);
bellard7854b052003-03-29 17:22:23 +0000552}
553
pbrook53a59602006-03-25 19:31:22 +0000554static inline void host_to_target_sockaddr(target_ulong target_addr,
bellard7854b052003-03-29 17:22:23 +0000555 struct sockaddr *addr,
556 socklen_t len)
557{
pbrook53a59602006-03-25 19:31:22 +0000558 struct target_sockaddr *target_saddr;
559
560 target_saddr = lock_user(target_addr, len, 0);
561 memcpy(target_saddr, addr, len);
562 target_saddr->sa_family = tswap16(addr->sa_family);
563 unlock_user(target_saddr, target_addr, len);
bellard7854b052003-03-29 17:22:23 +0000564}
565
pbrook53a59602006-03-25 19:31:22 +0000566/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000567static inline void target_to_host_cmsg(struct msghdr *msgh,
568 struct target_msghdr *target_msgh)
569{
570 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
571 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
572 socklen_t space = 0;
573
574 while (cmsg && target_cmsg) {
575 void *data = CMSG_DATA(cmsg);
576 void *target_data = TARGET_CMSG_DATA(target_cmsg);
577
ths5fafdf22007-09-16 21:08:06 +0000578 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +0000579 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
580
581 space += CMSG_SPACE(len);
582 if (space > msgh->msg_controllen) {
583 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000584 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000585 break;
586 }
587
588 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
589 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
590 cmsg->cmsg_len = CMSG_LEN(len);
591
bellard3532fa72006-06-24 15:06:03 +0000592 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000593 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
594 memcpy(data, target_data, len);
595 } else {
596 int *fd = (int *)data;
597 int *target_fd = (int *)target_data;
598 int i, numfds = len / sizeof(int);
599
600 for (i = 0; i < numfds; i++)
601 fd[i] = tswap32(target_fd[i]);
602 }
603
604 cmsg = CMSG_NXTHDR(msgh, cmsg);
605 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
606 }
607
608 msgh->msg_controllen = space;
609}
610
pbrook53a59602006-03-25 19:31:22 +0000611/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000612static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
613 struct msghdr *msgh)
614{
615 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
616 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
617 socklen_t space = 0;
618
619 while (cmsg && target_cmsg) {
620 void *data = CMSG_DATA(cmsg);
621 void *target_data = TARGET_CMSG_DATA(target_cmsg);
622
623 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
624
625 space += TARGET_CMSG_SPACE(len);
626 if (space > tswapl(target_msgh->msg_controllen)) {
627 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000628 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000629 break;
630 }
631
632 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
633 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
634 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
635
bellard3532fa72006-06-24 15:06:03 +0000636 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000637 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
638 memcpy(target_data, data, len);
639 } else {
640 int *fd = (int *)data;
641 int *target_fd = (int *)target_data;
642 int i, numfds = len / sizeof(int);
643
644 for (i = 0; i < numfds; i++)
645 target_fd[i] = tswap32(fd[i]);
646 }
647
648 cmsg = CMSG_NXTHDR(msgh, cmsg);
649 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
650 }
651
652 msgh->msg_controllen = tswapl(space);
653}
654
ths5fafdf22007-09-16 21:08:06 +0000655static long do_setsockopt(int sockfd, int level, int optname,
pbrook53a59602006-03-25 19:31:22 +0000656 target_ulong optval, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +0000657{
bellard8853f862004-02-22 14:57:26 +0000658 int val, ret;
ths3b46e622007-09-17 08:09:54 +0000659
bellard8853f862004-02-22 14:57:26 +0000660 switch(level) {
661 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +0000662 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +0000663 if (optlen < sizeof(uint32_t))
664 return -EINVAL;
ths3b46e622007-09-17 08:09:54 +0000665
pbrook53a59602006-03-25 19:31:22 +0000666 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000667 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
668 break;
669 case SOL_IP:
670 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +0000671 case IP_TOS:
672 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +0000673 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +0000674 case IP_ROUTER_ALERT:
675 case IP_RECVOPTS:
676 case IP_RETOPTS:
677 case IP_PKTINFO:
678 case IP_MTU_DISCOVER:
679 case IP_RECVERR:
680 case IP_RECVTOS:
681#ifdef IP_FREEBIND
682 case IP_FREEBIND:
683#endif
684 case IP_MULTICAST_TTL:
685 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +0000686 val = 0;
687 if (optlen >= sizeof(uint32_t)) {
pbrook53a59602006-03-25 19:31:22 +0000688 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000689 } else if (optlen >= 1) {
pbrook53a59602006-03-25 19:31:22 +0000690 val = tget8(optval);
bellard8853f862004-02-22 14:57:26 +0000691 }
692 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
693 break;
694 default:
695 goto unimplemented;
696 }
697 break;
bellard3532fa72006-06-24 15:06:03 +0000698 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +0000699 switch (optname) {
700 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +0000701 case TARGET_SO_DEBUG:
702 optname = SO_DEBUG;
703 break;
704 case TARGET_SO_REUSEADDR:
705 optname = SO_REUSEADDR;
706 break;
707 case TARGET_SO_TYPE:
708 optname = SO_TYPE;
709 break;
710 case TARGET_SO_ERROR:
711 optname = SO_ERROR;
712 break;
713 case TARGET_SO_DONTROUTE:
714 optname = SO_DONTROUTE;
715 break;
716 case TARGET_SO_BROADCAST:
717 optname = SO_BROADCAST;
718 break;
719 case TARGET_SO_SNDBUF:
720 optname = SO_SNDBUF;
721 break;
722 case TARGET_SO_RCVBUF:
723 optname = SO_RCVBUF;
724 break;
725 case TARGET_SO_KEEPALIVE:
726 optname = SO_KEEPALIVE;
727 break;
728 case TARGET_SO_OOBINLINE:
729 optname = SO_OOBINLINE;
730 break;
731 case TARGET_SO_NO_CHECK:
732 optname = SO_NO_CHECK;
733 break;
734 case TARGET_SO_PRIORITY:
735 optname = SO_PRIORITY;
736 break;
bellard5e83e8e2005-03-01 22:32:06 +0000737#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +0000738 case TARGET_SO_BSDCOMPAT:
739 optname = SO_BSDCOMPAT;
740 break;
bellard5e83e8e2005-03-01 22:32:06 +0000741#endif
bellard3532fa72006-06-24 15:06:03 +0000742 case TARGET_SO_PASSCRED:
743 optname = SO_PASSCRED;
744 break;
745 case TARGET_SO_TIMESTAMP:
746 optname = SO_TIMESTAMP;
747 break;
748 case TARGET_SO_RCVLOWAT:
749 optname = SO_RCVLOWAT;
750 break;
751 case TARGET_SO_RCVTIMEO:
752 optname = SO_RCVTIMEO;
753 break;
754 case TARGET_SO_SNDTIMEO:
755 optname = SO_SNDTIMEO;
756 break;
bellard8853f862004-02-22 14:57:26 +0000757 break;
758 default:
759 goto unimplemented;
760 }
bellard3532fa72006-06-24 15:06:03 +0000761 if (optlen < sizeof(uint32_t))
762 return -EINVAL;
763
764 val = tget32(optval);
765 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +0000766 break;
bellard7854b052003-03-29 17:22:23 +0000767 default:
bellard8853f862004-02-22 14:57:26 +0000768 unimplemented:
769 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
770 ret = -ENOSYS;
bellard7854b052003-03-29 17:22:23 +0000771 }
bellard8853f862004-02-22 14:57:26 +0000772 return ret;
bellard7854b052003-03-29 17:22:23 +0000773}
774
ths5fafdf22007-09-16 21:08:06 +0000775static long do_getsockopt(int sockfd, int level, int optname,
pbrook53a59602006-03-25 19:31:22 +0000776 target_ulong optval, target_ulong optlen)
bellard7854b052003-03-29 17:22:23 +0000777{
bellard8853f862004-02-22 14:57:26 +0000778 int len, lv, val, ret;
779
780 switch(level) {
bellard3532fa72006-06-24 15:06:03 +0000781 case TARGET_SOL_SOCKET:
782 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +0000783 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +0000784 case TARGET_SO_LINGER:
785 case TARGET_SO_RCVTIMEO:
786 case TARGET_SO_SNDTIMEO:
787 case TARGET_SO_PEERCRED:
788 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +0000789 /* These don't just return a single integer */
790 goto unimplemented;
791 default:
bellard2efbe912005-07-23 15:10:20 +0000792 goto int_case;
793 }
794 break;
795 case SOL_TCP:
796 /* TCP options all take an 'int' value. */
797 int_case:
pbrook53a59602006-03-25 19:31:22 +0000798 len = tget32(optlen);
bellard2efbe912005-07-23 15:10:20 +0000799 if (len < 0)
800 return -EINVAL;
801 lv = sizeof(int);
802 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
803 if (ret < 0)
804 return ret;
805 val = tswap32(val);
806 if (len > lv)
807 len = lv;
pbrook53a59602006-03-25 19:31:22 +0000808 if (len == 4)
809 tput32(optval, val);
810 else
811 tput8(optval, val);
812 tput32(optlen, len);
bellard2efbe912005-07-23 15:10:20 +0000813 break;
814 case SOL_IP:
815 switch(optname) {
816 case IP_TOS:
817 case IP_TTL:
818 case IP_HDRINCL:
819 case IP_ROUTER_ALERT:
820 case IP_RECVOPTS:
821 case IP_RETOPTS:
822 case IP_PKTINFO:
823 case IP_MTU_DISCOVER:
824 case IP_RECVERR:
825 case IP_RECVTOS:
826#ifdef IP_FREEBIND
827 case IP_FREEBIND:
828#endif
829 case IP_MULTICAST_TTL:
830 case IP_MULTICAST_LOOP:
pbrook53a59602006-03-25 19:31:22 +0000831 len = tget32(optlen);
bellard8853f862004-02-22 14:57:26 +0000832 if (len < 0)
833 return -EINVAL;
834 lv = sizeof(int);
835 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
836 if (ret < 0)
837 return ret;
bellard2efbe912005-07-23 15:10:20 +0000838 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +0000839 len = 1;
pbrook53a59602006-03-25 19:31:22 +0000840 tput32(optlen, len);
841 tput8(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000842 } else {
bellard2efbe912005-07-23 15:10:20 +0000843 if (len > sizeof(int))
844 len = sizeof(int);
pbrook53a59602006-03-25 19:31:22 +0000845 tput32(optlen, len);
846 tput32(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000847 }
bellard8853f862004-02-22 14:57:26 +0000848 break;
bellard2efbe912005-07-23 15:10:20 +0000849 default:
850 goto unimplemented;
bellard8853f862004-02-22 14:57:26 +0000851 }
852 break;
853 default:
854 unimplemented:
855 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
856 level, optname);
857 ret = -ENOSYS;
858 break;
859 }
860 return ret;
bellard7854b052003-03-29 17:22:23 +0000861}
862
pbrook53a59602006-03-25 19:31:22 +0000863static void lock_iovec(struct iovec *vec, target_ulong target_addr,
864 int count, int copy)
865{
866 struct target_iovec *target_vec;
867 target_ulong base;
868 int i;
869
870 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
871 for(i = 0;i < count; i++) {
872 base = tswapl(target_vec[i].iov_base);
873 vec[i].iov_len = tswapl(target_vec[i].iov_len);
874 vec[i].iov_base = lock_user(base, vec[i].iov_len, copy);
875 }
876 unlock_user (target_vec, target_addr, 0);
877}
878
879static void unlock_iovec(struct iovec *vec, target_ulong target_addr,
880 int count, int copy)
881{
882 struct target_iovec *target_vec;
883 target_ulong base;
884 int i;
885
886 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
887 for(i = 0;i < count; i++) {
888 base = tswapl(target_vec[i].iov_base);
889 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
890 }
891 unlock_user (target_vec, target_addr, 0);
892}
893
bellard3532fa72006-06-24 15:06:03 +0000894static long do_socket(int domain, int type, int protocol)
895{
896#if defined(TARGET_MIPS)
897 switch(type) {
898 case TARGET_SOCK_DGRAM:
899 type = SOCK_DGRAM;
900 break;
901 case TARGET_SOCK_STREAM:
902 type = SOCK_STREAM;
903 break;
904 case TARGET_SOCK_RAW:
905 type = SOCK_RAW;
906 break;
907 case TARGET_SOCK_RDM:
908 type = SOCK_RDM;
909 break;
910 case TARGET_SOCK_SEQPACKET:
911 type = SOCK_SEQPACKET;
912 break;
913 case TARGET_SOCK_PACKET:
914 type = SOCK_PACKET;
915 break;
916 }
917#endif
918 return get_errno(socket(domain, type, protocol));
919}
920
921static long do_bind(int sockfd, target_ulong target_addr,
922 socklen_t addrlen)
923{
924 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +0000925
bellard3532fa72006-06-24 15:06:03 +0000926 target_to_host_sockaddr(addr, target_addr, addrlen);
927 return get_errno(bind(sockfd, addr, addrlen));
928}
929
930static long do_connect(int sockfd, target_ulong target_addr,
931 socklen_t addrlen)
932{
933 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +0000934
bellard3532fa72006-06-24 15:06:03 +0000935 target_to_host_sockaddr(addr, target_addr, addrlen);
936 return get_errno(connect(sockfd, addr, addrlen));
937}
938
939static long do_sendrecvmsg(int fd, target_ulong target_msg,
940 int flags, int send)
941{
942 long ret;
943 struct target_msghdr *msgp;
944 struct msghdr msg;
945 int count;
946 struct iovec *vec;
947 target_ulong target_vec;
948
949 lock_user_struct(msgp, target_msg, 1);
950 if (msgp->msg_name) {
951 msg.msg_namelen = tswap32(msgp->msg_namelen);
952 msg.msg_name = alloca(msg.msg_namelen);
953 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
954 msg.msg_namelen);
955 } else {
956 msg.msg_name = NULL;
957 msg.msg_namelen = 0;
958 }
959 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
960 msg.msg_control = alloca(msg.msg_controllen);
961 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +0000962
bellard3532fa72006-06-24 15:06:03 +0000963 count = tswapl(msgp->msg_iovlen);
964 vec = alloca(count * sizeof(struct iovec));
965 target_vec = tswapl(msgp->msg_iov);
966 lock_iovec(vec, target_vec, count, send);
967 msg.msg_iovlen = count;
968 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +0000969
bellard3532fa72006-06-24 15:06:03 +0000970 if (send) {
971 target_to_host_cmsg(&msg, msgp);
972 ret = get_errno(sendmsg(fd, &msg, flags));
973 } else {
974 ret = get_errno(recvmsg(fd, &msg, flags));
975 if (!is_error(ret))
976 host_to_target_cmsg(msgp, &msg);
977 }
978 unlock_iovec(vec, target_vec, count, !send);
979 return ret;
980}
981
pbrook1be9e1d2006-11-19 15:26:04 +0000982static long do_accept(int fd, target_ulong target_addr,
983 target_ulong target_addrlen)
984{
985 socklen_t addrlen = tget32(target_addrlen);
ths197ea352006-12-11 19:13:59 +0000986 void *addr = alloca(addrlen);
pbrook1be9e1d2006-11-19 15:26:04 +0000987 long ret;
988
989 ret = get_errno(accept(fd, addr, &addrlen));
990 if (!is_error(ret)) {
991 host_to_target_sockaddr(target_addr, addr, addrlen);
992 tput32(target_addrlen, addrlen);
993 }
994 return ret;
995}
996
997static long do_getpeername(int fd, target_ulong target_addr,
998 target_ulong target_addrlen)
999{
1000 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001001 void *addr = alloca(addrlen);
pbrook1be9e1d2006-11-19 15:26:04 +00001002 long ret;
1003
1004 ret = get_errno(getpeername(fd, addr, &addrlen));
1005 if (!is_error(ret)) {
1006 host_to_target_sockaddr(target_addr, addr, addrlen);
1007 tput32(target_addrlen, addrlen);
1008 }
1009 return ret;
1010}
1011
1012static long do_getsockname(int fd, target_ulong target_addr,
1013 target_ulong target_addrlen)
1014{
1015 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001016 void *addr = alloca(addrlen);
pbrook1be9e1d2006-11-19 15:26:04 +00001017 long ret;
1018
1019 ret = get_errno(getsockname(fd, addr, &addrlen));
1020 if (!is_error(ret)) {
1021 host_to_target_sockaddr(target_addr, addr, addrlen);
1022 tput32(target_addrlen, addrlen);
1023 }
1024 return ret;
1025}
1026
1027static long do_socketpair(int domain, int type, int protocol,
1028 target_ulong target_tab)
1029{
1030 int tab[2];
1031 long ret;
1032
1033 ret = get_errno(socketpair(domain, type, protocol, tab));
1034 if (!is_error(ret)) {
1035 tput32(target_tab, tab[0]);
1036 tput32(target_tab + 4, tab[1]);
1037 }
1038 return ret;
1039}
1040
1041static long do_sendto(int fd, target_ulong msg, size_t len, int flags,
1042 target_ulong target_addr, socklen_t addrlen)
1043{
1044 void *addr;
1045 void *host_msg;
1046 long ret;
1047
1048 host_msg = lock_user(msg, len, 1);
1049 if (target_addr) {
1050 addr = alloca(addrlen);
1051 target_to_host_sockaddr(addr, target_addr, addrlen);
1052 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1053 } else {
1054 ret = get_errno(send(fd, host_msg, len, flags));
1055 }
1056 unlock_user(host_msg, msg, 0);
1057 return ret;
1058}
1059
1060static long do_recvfrom(int fd, target_ulong msg, size_t len, int flags,
1061 target_ulong target_addr, target_ulong target_addrlen)
1062{
1063 socklen_t addrlen;
1064 void *addr;
1065 void *host_msg;
1066 long ret;
1067
1068 host_msg = lock_user(msg, len, 0);
1069 if (target_addr) {
1070 addrlen = tget32(target_addrlen);
1071 addr = alloca(addrlen);
1072 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1073 } else {
1074 addr = NULL; /* To keep compiler quiet. */
1075 ret = get_errno(recv(fd, host_msg, len, flags));
1076 }
1077 if (!is_error(ret)) {
1078 if (target_addr) {
1079 host_to_target_sockaddr(target_addr, addr, addrlen);
1080 tput32(target_addrlen, addrlen);
1081 }
1082 unlock_user(host_msg, msg, len);
1083 } else {
1084 unlock_user(host_msg, msg, 0);
1085 }
1086 return ret;
1087}
1088
ths85aa1992007-05-28 20:07:13 +00001089static long do_socketcall(int num, target_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001090{
1091 long ret;
pbrook53a59602006-03-25 19:31:22 +00001092 const int n = sizeof(target_ulong);
bellard31e31b82003-02-18 22:55:36 +00001093
1094 switch(num) {
1095 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001096 {
pbrook53a59602006-03-25 19:31:22 +00001097 int domain = tgetl(vptr);
1098 int type = tgetl(vptr + n);
1099 int protocol = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001100 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001101 }
bellard31e31b82003-02-18 22:55:36 +00001102 break;
1103 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001104 {
pbrook53a59602006-03-25 19:31:22 +00001105 int sockfd = tgetl(vptr);
1106 target_ulong target_addr = tgetl(vptr + n);
1107 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001108 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001109 }
bellard31e31b82003-02-18 22:55:36 +00001110 break;
1111 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001112 {
pbrook53a59602006-03-25 19:31:22 +00001113 int sockfd = tgetl(vptr);
1114 target_ulong target_addr = tgetl(vptr + n);
1115 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001116 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001117 }
bellard31e31b82003-02-18 22:55:36 +00001118 break;
1119 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001120 {
pbrook53a59602006-03-25 19:31:22 +00001121 int sockfd = tgetl(vptr);
1122 int backlog = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001123 ret = get_errno(listen(sockfd, backlog));
1124 }
bellard31e31b82003-02-18 22:55:36 +00001125 break;
1126 case SOCKOP_accept:
1127 {
pbrook53a59602006-03-25 19:31:22 +00001128 int sockfd = tgetl(vptr);
1129 target_ulong target_addr = tgetl(vptr + n);
1130 target_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001131 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001132 }
1133 break;
1134 case SOCKOP_getsockname:
1135 {
pbrook53a59602006-03-25 19:31:22 +00001136 int sockfd = tgetl(vptr);
1137 target_ulong target_addr = tgetl(vptr + n);
1138 target_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001139 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001140 }
1141 break;
1142 case SOCKOP_getpeername:
1143 {
pbrook53a59602006-03-25 19:31:22 +00001144 int sockfd = tgetl(vptr);
1145 target_ulong target_addr = tgetl(vptr + n);
1146 target_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001147 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001148 }
1149 break;
1150 case SOCKOP_socketpair:
1151 {
pbrook53a59602006-03-25 19:31:22 +00001152 int domain = tgetl(vptr);
1153 int type = tgetl(vptr + n);
1154 int protocol = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001155 target_ulong tab = tgetl(vptr + 3 * n);
1156 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001157 }
1158 break;
1159 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001160 {
pbrook53a59602006-03-25 19:31:22 +00001161 int sockfd = tgetl(vptr);
1162 target_ulong msg = tgetl(vptr + n);
1163 size_t len = tgetl(vptr + 2 * n);
1164 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001165 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001166 }
bellard31e31b82003-02-18 22:55:36 +00001167 break;
1168 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001169 {
pbrook53a59602006-03-25 19:31:22 +00001170 int sockfd = tgetl(vptr);
1171 target_ulong msg = tgetl(vptr + n);
1172 size_t len = tgetl(vptr + 2 * n);
1173 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001174 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001175 }
bellard31e31b82003-02-18 22:55:36 +00001176 break;
1177 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001178 {
pbrook53a59602006-03-25 19:31:22 +00001179 int sockfd = tgetl(vptr);
1180 target_ulong msg = tgetl(vptr + n);
1181 size_t len = tgetl(vptr + 2 * n);
1182 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001183 target_ulong addr = tgetl(vptr + 4 * n);
pbrook53a59602006-03-25 19:31:22 +00001184 socklen_t addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001185 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001186 }
bellard31e31b82003-02-18 22:55:36 +00001187 break;
1188 case SOCKOP_recvfrom:
1189 {
pbrook53a59602006-03-25 19:31:22 +00001190 int sockfd = tgetl(vptr);
1191 target_ulong msg = tgetl(vptr + n);
1192 size_t len = tgetl(vptr + 2 * n);
1193 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001194 target_ulong addr = tgetl(vptr + 4 * n);
1195 target_ulong addrlen = tgetl(vptr + 5 * n);
1196 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001197 }
1198 break;
1199 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001200 {
pbrook53a59602006-03-25 19:31:22 +00001201 int sockfd = tgetl(vptr);
1202 int how = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001203
1204 ret = get_errno(shutdown(sockfd, how));
1205 }
bellard31e31b82003-02-18 22:55:36 +00001206 break;
1207 case SOCKOP_sendmsg:
1208 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001209 {
1210 int fd;
pbrook53a59602006-03-25 19:31:22 +00001211 target_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001212 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001213
pbrook53a59602006-03-25 19:31:22 +00001214 fd = tgetl(vptr);
bellard3532fa72006-06-24 15:06:03 +00001215 target_msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001216 flags = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001217
ths5fafdf22007-09-16 21:08:06 +00001218 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001219 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001220 }
1221 break;
bellard31e31b82003-02-18 22:55:36 +00001222 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001223 {
pbrook53a59602006-03-25 19:31:22 +00001224 int sockfd = tgetl(vptr);
1225 int level = tgetl(vptr + n);
1226 int optname = tgetl(vptr + 2 * n);
1227 target_ulong optval = tgetl(vptr + 3 * n);
1228 socklen_t optlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001229
1230 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1231 }
1232 break;
bellard31e31b82003-02-18 22:55:36 +00001233 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001234 {
pbrook53a59602006-03-25 19:31:22 +00001235 int sockfd = tgetl(vptr);
1236 int level = tgetl(vptr + n);
1237 int optname = tgetl(vptr + 2 * n);
1238 target_ulong optval = tgetl(vptr + 3 * n);
1239 target_ulong poptlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001240
bellard8853f862004-02-22 14:57:26 +00001241 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard7854b052003-03-29 17:22:23 +00001242 }
1243 break;
bellard31e31b82003-02-18 22:55:36 +00001244 default:
1245 gemu_log("Unsupported socketcall: %d\n", num);
1246 ret = -ENOSYS;
1247 break;
1248 }
1249 return ret;
1250}
1251
bellard8853f862004-02-22 14:57:26 +00001252#define N_SHM_REGIONS 32
1253
1254static struct shm_region {
1255 uint32_t start;
1256 uint32_t size;
1257} shm_regions[N_SHM_REGIONS];
1258
ths3eb6b042007-06-03 14:26:27 +00001259struct target_ipc_perm
1260{
1261 target_long __key;
1262 target_ulong uid;
1263 target_ulong gid;
1264 target_ulong cuid;
1265 target_ulong cgid;
1266 unsigned short int mode;
1267 unsigned short int __pad1;
1268 unsigned short int __seq;
1269 unsigned short int __pad2;
1270 target_ulong __unused1;
1271 target_ulong __unused2;
1272};
1273
1274struct target_semid_ds
1275{
1276 struct target_ipc_perm sem_perm;
1277 target_ulong sem_otime;
1278 target_ulong __unused1;
1279 target_ulong sem_ctime;
1280 target_ulong __unused2;
1281 target_ulong sem_nsems;
1282 target_ulong __unused3;
1283 target_ulong __unused4;
1284};
1285
1286static inline void target_to_host_ipc_perm(struct ipc_perm *host_ip,
1287 target_ulong target_addr)
1288{
1289 struct target_ipc_perm *target_ip;
1290 struct target_semid_ds *target_sd;
1291
1292 lock_user_struct(target_sd, target_addr, 1);
1293 target_ip=&(target_sd->sem_perm);
1294 host_ip->__key = tswapl(target_ip->__key);
1295 host_ip->uid = tswapl(target_ip->uid);
1296 host_ip->gid = tswapl(target_ip->gid);
1297 host_ip->cuid = tswapl(target_ip->cuid);
1298 host_ip->cgid = tswapl(target_ip->cgid);
1299 host_ip->mode = tswapl(target_ip->mode);
1300 unlock_user_struct(target_sd, target_addr, 0);
1301}
1302
1303static inline void host_to_target_ipc_perm(target_ulong target_addr,
1304 struct ipc_perm *host_ip)
1305{
1306 struct target_ipc_perm *target_ip;
1307 struct target_semid_ds *target_sd;
1308
1309 lock_user_struct(target_sd, target_addr, 0);
1310 target_ip = &(target_sd->sem_perm);
1311 target_ip->__key = tswapl(host_ip->__key);
1312 target_ip->uid = tswapl(host_ip->uid);
1313 target_ip->gid = tswapl(host_ip->gid);
1314 target_ip->cuid = tswapl(host_ip->cuid);
1315 target_ip->cgid = tswapl(host_ip->cgid);
1316 target_ip->mode = tswapl(host_ip->mode);
1317 unlock_user_struct(target_sd, target_addr, 1);
1318}
1319
1320static inline void target_to_host_semid_ds(struct semid_ds *host_sd,
1321 target_ulong target_addr)
1322{
1323 struct target_semid_ds *target_sd;
1324
1325 lock_user_struct(target_sd, target_addr, 1);
1326 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1327 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1328 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1329 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1330 unlock_user_struct(target_sd, target_addr, 0);
1331}
1332
1333static inline void host_to_target_semid_ds(target_ulong target_addr,
1334 struct semid_ds *host_sd)
1335{
1336 struct target_semid_ds *target_sd;
1337
1338 lock_user_struct(target_sd, target_addr, 0);
1339 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1340 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1341 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1342 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1343 unlock_user_struct(target_sd, target_addr, 1);
1344}
1345
thsfa294812007-02-02 22:05:00 +00001346union semun {
1347 int val;
ths3eb6b042007-06-03 14:26:27 +00001348 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001349 unsigned short *array;
1350};
1351
ths3eb6b042007-06-03 14:26:27 +00001352union target_semun {
1353 int val;
1354 target_long buf;
1355 unsigned short int *array;
1356};
1357
1358static inline void target_to_host_semun(unsigned long cmd,
1359 union semun *host_su,
1360 target_ulong target_addr,
1361 struct semid_ds *ds)
1362{
1363 union target_semun *target_su;
1364
1365 switch( cmd ) {
1366 case IPC_STAT:
1367 case IPC_SET:
1368 lock_user_struct(target_su, target_addr, 1);
1369 target_to_host_semid_ds(ds,target_su->buf);
1370 host_su->buf = ds;
1371 unlock_user_struct(target_su, target_addr, 0);
1372 break;
1373 case GETVAL:
1374 case SETVAL:
1375 lock_user_struct(target_su, target_addr, 1);
1376 host_su->val = tswapl(target_su->val);
1377 unlock_user_struct(target_su, target_addr, 0);
1378 break;
1379 case GETALL:
1380 case SETALL:
1381 lock_user_struct(target_su, target_addr, 1);
1382 *host_su->array = tswap16(*target_su->array);
1383 unlock_user_struct(target_su, target_addr, 0);
1384 break;
1385 default:
1386 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1387 }
1388}
1389
1390static inline void host_to_target_semun(unsigned long cmd,
1391 target_ulong target_addr,
1392 union semun *host_su,
1393 struct semid_ds *ds)
1394{
1395 union target_semun *target_su;
1396
1397 switch( cmd ) {
1398 case IPC_STAT:
1399 case IPC_SET:
1400 lock_user_struct(target_su, target_addr, 0);
1401 host_to_target_semid_ds(target_su->buf,ds);
1402 unlock_user_struct(target_su, target_addr, 1);
1403 break;
1404 case GETVAL:
1405 case SETVAL:
1406 lock_user_struct(target_su, target_addr, 0);
1407 target_su->val = tswapl(host_su->val);
1408 unlock_user_struct(target_su, target_addr, 1);
1409 break;
1410 case GETALL:
1411 case SETALL:
1412 lock_user_struct(target_su, target_addr, 0);
1413 *target_su->array = tswap16(*host_su->array);
1414 unlock_user_struct(target_su, target_addr, 1);
1415 break;
1416 default:
1417 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1418 }
1419}
1420
1421static inline long do_semctl(long first, long second, long third, long ptr)
1422{
1423 union semun arg;
1424 struct semid_ds dsarg;
1425 int cmd = third&0xff;
1426 long ret = 0;
1427
1428 switch( cmd ) {
1429 case GETVAL:
1430 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1431 ret = get_errno(semctl(first, second, cmd, arg));
1432 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1433 break;
1434 case SETVAL:
1435 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1436 ret = get_errno(semctl(first, second, cmd, arg));
1437 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1438 break;
1439 case GETALL:
1440 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1441 ret = get_errno(semctl(first, second, cmd, arg));
1442 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1443 break;
1444 case SETALL:
1445 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1446 ret = get_errno(semctl(first, second, cmd, arg));
1447 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1448 break;
1449 case IPC_STAT:
1450 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1451 ret = get_errno(semctl(first, second, cmd, arg));
1452 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1453 break;
1454 case IPC_SET:
1455 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1456 ret = get_errno(semctl(first, second, cmd, arg));
1457 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1458 break;
1459 default:
1460 ret = get_errno(semctl(first, second, cmd, arg));
1461 }
1462
1463 return ret;
1464}
1465
ths1bc012f2007-06-03 14:27:49 +00001466struct target_msqid_ds
1467{
1468 struct target_ipc_perm msg_perm;
1469 target_ulong msg_stime;
1470 target_ulong __unused1;
1471 target_ulong msg_rtime;
1472 target_ulong __unused2;
1473 target_ulong msg_ctime;
1474 target_ulong __unused3;
1475 target_ulong __msg_cbytes;
1476 target_ulong msg_qnum;
1477 target_ulong msg_qbytes;
1478 target_ulong msg_lspid;
1479 target_ulong msg_lrpid;
1480 target_ulong __unused4;
1481 target_ulong __unused5;
1482};
1483
1484static inline void target_to_host_msqid_ds(struct msqid_ds *host_md,
1485 target_ulong target_addr)
1486{
1487 struct target_msqid_ds *target_md;
1488
1489 lock_user_struct(target_md, target_addr, 1);
1490 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1491 host_md->msg_stime = tswapl(target_md->msg_stime);
1492 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1493 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1494 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1495 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1496 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1497 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1498 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1499 unlock_user_struct(target_md, target_addr, 0);
1500}
1501
1502static inline void host_to_target_msqid_ds(target_ulong target_addr,
1503 struct msqid_ds *host_md)
1504{
1505 struct target_msqid_ds *target_md;
1506
1507 lock_user_struct(target_md, target_addr, 0);
1508 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1509 target_md->msg_stime = tswapl(host_md->msg_stime);
1510 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1511 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1512 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1513 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1514 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1515 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1516 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1517 unlock_user_struct(target_md, target_addr, 1);
1518}
1519
1520static inline long do_msgctl(long first, long second, long ptr)
1521{
1522 struct msqid_ds dsarg;
1523 int cmd = second&0xff;
1524 long ret = 0;
1525 switch( cmd ) {
1526 case IPC_STAT:
1527 case IPC_SET:
1528 target_to_host_msqid_ds(&dsarg,ptr);
1529 ret = get_errno(msgctl(first, cmd, &dsarg));
1530 host_to_target_msqid_ds(ptr,&dsarg);
1531 default:
1532 ret = get_errno(msgctl(first, cmd, &dsarg));
1533 }
1534 return ret;
1535}
1536
1537struct target_msgbuf {
1538 target_ulong mtype;
1539 char mtext[1];
1540};
1541
1542static inline long do_msgsnd(long msqid, long msgp, long msgsz, long msgflg)
1543{
1544 struct target_msgbuf *target_mb;
1545 struct msgbuf *host_mb;
1546 long ret = 0;
1547
1548 lock_user_struct(target_mb,msgp,0);
1549 host_mb = malloc(msgsz+sizeof(long));
1550 host_mb->mtype = tswapl(target_mb->mtype);
1551 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1552 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1553 free(host_mb);
1554 unlock_user_struct(target_mb, msgp, 0);
1555
1556 return ret;
1557}
1558
1559static inline long do_msgrcv(long msqid, long msgp, long msgsz, long msgtype, long msgflg)
1560{
1561 struct target_msgbuf *target_mb;
1562 struct msgbuf *host_mb;
1563 long ret = 0;
1564
1565 lock_user_struct(target_mb, msgp, 0);
1566 host_mb = malloc(msgsz+sizeof(long));
1567 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1568 if (ret > 0)
1569 memcpy(target_mb->mtext, host_mb->mtext, ret);
1570 target_mb->mtype = tswapl(host_mb->mtype);
1571 free(host_mb);
1572 unlock_user_struct(target_mb, msgp, 0);
1573
1574 return ret;
1575}
1576
pbrook53a59602006-03-25 19:31:22 +00001577/* ??? This only works with linear mappings. */
bellard8853f862004-02-22 14:57:26 +00001578static long do_ipc(long call, long first, long second, long third,
1579 long ptr, long fifth)
1580{
1581 int version;
1582 long ret = 0;
1583 unsigned long raddr;
1584 struct shmid_ds shm_info;
1585 int i;
1586
1587 version = call >> 16;
1588 call &= 0xffff;
1589
1590 switch (call) {
thsfa294812007-02-02 22:05:00 +00001591 case IPCOP_semop:
1592 ret = get_errno(semop(first,(struct sembuf *) ptr, second));
1593 break;
1594
1595 case IPCOP_semget:
1596 ret = get_errno(semget(first, second, third));
1597 break;
1598
1599 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001600 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001601 break;
1602
1603 case IPCOP_semtimedop:
1604 gemu_log("Unsupported ipc call: %ld (version %d)\n", call, version);
1605 ret = -ENOSYS;
1606 break;
thsd96372e2007-02-02 22:05:44 +00001607
1608 case IPCOP_msgget:
1609 ret = get_errno(msgget(first, second));
1610 break;
1611
1612 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001613 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001614 break;
1615
1616 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001617 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001618 break;
1619
1620 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001621 {
1622 struct ipc_kludge
1623 {
1624 void *__unbounded msgp;
1625 long int msgtyp;
1626 };
thsd96372e2007-02-02 22:05:44 +00001627
ths1bc012f2007-06-03 14:27:49 +00001628 struct ipc_kludge *foo = (struct ipc_kludge *) ptr;
1629 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001630
ths1bc012f2007-06-03 14:27:49 +00001631 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001632
ths1bc012f2007-06-03 14:27:49 +00001633 }
thsd96372e2007-02-02 22:05:44 +00001634 break;
1635
bellard8853f862004-02-22 14:57:26 +00001636 case IPCOP_shmat:
1637 /* SHM_* flags are the same on all linux platforms */
1638 ret = get_errno((long) shmat(first, (void *) ptr, second));
1639 if (is_error(ret))
1640 break;
1641 raddr = ret;
1642 /* find out the length of the shared memory segment */
ths3b46e622007-09-17 08:09:54 +00001643
bellard8853f862004-02-22 14:57:26 +00001644 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1645 if (is_error(ret)) {
1646 /* can't get length, bail out */
1647 shmdt((void *) raddr);
1648 break;
1649 }
1650 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1651 PAGE_VALID | PAGE_READ |
1652 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1653 for (i = 0; i < N_SHM_REGIONS; ++i) {
1654 if (shm_regions[i].start == 0) {
1655 shm_regions[i].start = raddr;
1656 shm_regions[i].size = shm_info.shm_segsz;
1657 break;
1658 }
1659 }
1660 if (put_user(raddr, (uint32_t *)third))
1661 return -EFAULT;
1662 ret = 0;
1663 break;
1664 case IPCOP_shmdt:
1665 for (i = 0; i < N_SHM_REGIONS; ++i) {
1666 if (shm_regions[i].start == ptr) {
1667 shm_regions[i].start = 0;
1668 page_set_flags(ptr, shm_regions[i].size, 0);
1669 break;
1670 }
1671 }
1672 ret = get_errno(shmdt((void *) ptr));
1673 break;
1674
1675 case IPCOP_shmget:
1676 /* IPC_* flag values are the same on all linux platforms */
1677 ret = get_errno(shmget(first, second, third));
1678 break;
1679
1680 /* IPC_* and SHM_* command values are the same on all linux platforms */
1681 case IPCOP_shmctl:
1682 switch(second) {
1683 case IPC_RMID:
1684 case SHM_LOCK:
1685 case SHM_UNLOCK:
1686 ret = get_errno(shmctl(first, second, NULL));
1687 break;
1688 default:
1689 goto unimplemented;
1690 }
1691 break;
1692 default:
1693 unimplemented:
1694 gemu_log("Unsupported ipc call: %ld (version %d)\n", call, version);
1695 ret = -ENOSYS;
1696 break;
1697 }
1698 return ret;
1699}
1700
bellard31e31b82003-02-18 22:55:36 +00001701/* kernel structure types definitions */
1702#define IFNAMSIZ 16
1703
1704#define STRUCT(name, list...) STRUCT_ ## name,
1705#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1706enum {
1707#include "syscall_types.h"
1708};
1709#undef STRUCT
1710#undef STRUCT_SPECIAL
1711
1712#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1713#define STRUCT_SPECIAL(name)
1714#include "syscall_types.h"
1715#undef STRUCT
1716#undef STRUCT_SPECIAL
1717
1718typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00001719 unsigned int target_cmd;
1720 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00001721 const char *name;
1722 int access;
bellard1a9353d2003-03-16 20:28:50 +00001723 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00001724} IOCTLEntry;
1725
1726#define IOC_R 0x0001
1727#define IOC_W 0x0002
1728#define IOC_RW (IOC_R | IOC_W)
1729
1730#define MAX_STRUCT_SIZE 4096
1731
bellard2ab83ea2003-06-15 19:56:46 +00001732IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00001733#define IOCTL(cmd, access, types...) \
1734 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1735#include "ioctls.h"
1736 { 0, 0, },
1737};
1738
pbrook53a59602006-03-25 19:31:22 +00001739/* ??? Implement proper locking for ioctls. */
bellard31e31b82003-02-18 22:55:36 +00001740static long do_ioctl(long fd, long cmd, long arg)
1741{
1742 const IOCTLEntry *ie;
1743 const argtype *arg_type;
1744 long ret;
1745 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00001746 int target_size;
1747 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00001748
1749 ie = ioctl_entries;
1750 for(;;) {
1751 if (ie->target_cmd == 0) {
1752 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
1753 return -ENOSYS;
1754 }
1755 if (ie->target_cmd == cmd)
1756 break;
1757 ie++;
1758 }
1759 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00001760#if defined(DEBUG)
bellard72f03902003-02-18 23:33:18 +00001761 gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
1762#endif
bellard31e31b82003-02-18 22:55:36 +00001763 switch(arg_type[0]) {
1764 case TYPE_NULL:
1765 /* no argument */
1766 ret = get_errno(ioctl(fd, ie->host_cmd));
1767 break;
1768 case TYPE_PTRVOID:
1769 case TYPE_INT:
1770 /* int argment */
1771 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1772 break;
1773 case TYPE_PTR:
1774 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00001775 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00001776 switch(ie->access) {
1777 case IOC_R:
1778 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1779 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001780 argptr = lock_user(arg, target_size, 0);
1781 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1782 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001783 }
1784 break;
1785 case IOC_W:
pbrook53a59602006-03-25 19:31:22 +00001786 argptr = lock_user(arg, target_size, 1);
1787 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1788 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001789 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1790 break;
1791 default:
1792 case IOC_RW:
pbrook53a59602006-03-25 19:31:22 +00001793 argptr = lock_user(arg, target_size, 1);
1794 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1795 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001796 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1797 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001798 argptr = lock_user(arg, target_size, 0);
1799 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1800 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001801 }
1802 break;
1803 }
1804 break;
1805 default:
1806 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
1807 ret = -ENOSYS;
1808 break;
1809 }
1810 return ret;
1811}
1812
1813bitmask_transtbl iflag_tbl[] = {
1814 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1815 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1816 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1817 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1818 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1819 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1820 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1821 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1822 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1823 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1824 { TARGET_IXON, TARGET_IXON, IXON, IXON },
1825 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1826 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1827 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1828 { 0, 0, 0, 0 }
1829};
1830
1831bitmask_transtbl oflag_tbl[] = {
1832 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1833 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1834 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1835 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1836 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1837 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1838 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1839 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1840 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1841 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1842 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1843 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1844 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1845 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1846 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1847 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1848 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1849 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1850 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1851 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1852 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1853 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1854 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1855 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1856 { 0, 0, 0, 0 }
1857};
1858
1859bitmask_transtbl cflag_tbl[] = {
1860 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1861 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1862 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1863 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1864 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1865 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1866 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1867 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1868 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1869 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1870 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1871 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1872 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1873 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1874 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1875 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1876 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1877 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1878 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1879 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1880 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1881 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1882 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1883 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1884 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1885 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1886 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1887 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1888 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1889 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1890 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1891 { 0, 0, 0, 0 }
1892};
1893
1894bitmask_transtbl lflag_tbl[] = {
1895 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1896 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1897 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1898 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1899 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1900 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1901 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1902 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1903 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1904 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1905 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1906 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1907 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1908 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1909 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1910 { 0, 0, 0, 0 }
1911};
1912
1913static void target_to_host_termios (void *dst, const void *src)
1914{
1915 struct host_termios *host = dst;
1916 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00001917
ths5fafdf22007-09-16 21:08:06 +00001918 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00001919 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00001920 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00001921 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00001922 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00001923 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00001924 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00001925 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
1926 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00001927
ths5fafdf22007-09-16 21:08:06 +00001928 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
1929 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00001930 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00001931 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00001932 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00001933 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00001934 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00001935 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00001936 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00001937 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
1938 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00001939 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
1940 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
1941 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
1942 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
1943 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00001944 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00001945}
ths3b46e622007-09-17 08:09:54 +00001946
bellard31e31b82003-02-18 22:55:36 +00001947static void host_to_target_termios (void *dst, const void *src)
1948{
1949 struct target_termios *target = dst;
1950 const struct host_termios *host = src;
1951
ths5fafdf22007-09-16 21:08:06 +00001952 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00001953 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00001954 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00001955 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00001956 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00001957 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00001958 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00001959 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
1960 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00001961
bellard31e31b82003-02-18 22:55:36 +00001962 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
1963 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
1964 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
1965 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
1966 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
1967 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
1968 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
1969 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
1970 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
1971 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
1972 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
1973 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
1974 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
1975 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
1976 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
1977 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
1978 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
1979}
1980
1981StructEntry struct_termios_def = {
1982 .convert = { host_to_target_termios, target_to_host_termios },
1983 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
1984 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
1985};
1986
bellard5286db72003-06-05 00:57:30 +00001987static bitmask_transtbl mmap_flags_tbl[] = {
1988 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
1989 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
1990 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
1991 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
1992 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
1993 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
1994 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
1995 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
1996 { 0, 0, 0, 0 }
1997};
1998
bellardffa65c32004-01-04 23:57:22 +00001999static bitmask_transtbl fcntl_flags_tbl[] = {
2000 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2001 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2002 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2003 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2004 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2005 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2006 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2007 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2008 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2009 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2010 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2011 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2012 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002013#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002014 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002015#endif
bellardffa65c32004-01-04 23:57:22 +00002016 { 0, 0, 0, 0 }
2017};
2018
bellard2ab83ea2003-06-15 19:56:46 +00002019#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002020
2021/* NOTE: there is really one LDT for all the threads */
2022uint8_t *ldt_table;
2023
pbrook53a59602006-03-25 19:31:22 +00002024static int read_ldt(target_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002025{
2026 int size;
pbrook53a59602006-03-25 19:31:22 +00002027 void *p;
bellard6dbad632003-03-16 18:05:05 +00002028
2029 if (!ldt_table)
2030 return 0;
2031 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2032 if (size > bytecount)
2033 size = bytecount;
pbrook53a59602006-03-25 19:31:22 +00002034 p = lock_user(ptr, size, 0);
2035 /* ??? Shoudl this by byteswapped? */
2036 memcpy(p, ldt_table, size);
2037 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002038 return size;
2039}
2040
2041/* XXX: add locking support */
ths5fafdf22007-09-16 21:08:06 +00002042static int write_ldt(CPUX86State *env,
pbrook53a59602006-03-25 19:31:22 +00002043 target_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002044{
2045 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002046 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002047 int seg_32bit, contents, read_exec_only, limit_in_pages;
2048 int seg_not_present, useable;
2049 uint32_t *lp, entry_1, entry_2;
2050
2051 if (bytecount != sizeof(ldt_info))
2052 return -EINVAL;
pbrook53a59602006-03-25 19:31:22 +00002053 lock_user_struct(target_ldt_info, ptr, 1);
2054 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2055 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2056 ldt_info.limit = tswap32(target_ldt_info->limit);
2057 ldt_info.flags = tswap32(target_ldt_info->flags);
2058 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002059
bellard6dbad632003-03-16 18:05:05 +00002060 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2061 return -EINVAL;
2062 seg_32bit = ldt_info.flags & 1;
2063 contents = (ldt_info.flags >> 1) & 3;
2064 read_exec_only = (ldt_info.flags >> 3) & 1;
2065 limit_in_pages = (ldt_info.flags >> 4) & 1;
2066 seg_not_present = (ldt_info.flags >> 5) & 1;
2067 useable = (ldt_info.flags >> 6) & 1;
2068
2069 if (contents == 3) {
2070 if (oldmode)
2071 return -EINVAL;
2072 if (seg_not_present == 0)
2073 return -EINVAL;
2074 }
2075 /* allocate the LDT */
2076 if (!ldt_table) {
2077 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2078 if (!ldt_table)
2079 return -ENOMEM;
2080 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
pbrook53a59602006-03-25 19:31:22 +00002081 env->ldt.base = h2g(ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002082 env->ldt.limit = 0xffff;
2083 }
2084
2085 /* NOTE: same code as Linux kernel */
2086 /* Allow LDTs to be cleared by the user. */
2087 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2088 if (oldmode ||
2089 (contents == 0 &&
2090 read_exec_only == 1 &&
2091 seg_32bit == 0 &&
2092 limit_in_pages == 0 &&
2093 seg_not_present == 1 &&
2094 useable == 0 )) {
2095 entry_1 = 0;
2096 entry_2 = 0;
2097 goto install;
2098 }
2099 }
ths3b46e622007-09-17 08:09:54 +00002100
bellard6dbad632003-03-16 18:05:05 +00002101 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2102 (ldt_info.limit & 0x0ffff);
2103 entry_2 = (ldt_info.base_addr & 0xff000000) |
2104 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2105 (ldt_info.limit & 0xf0000) |
2106 ((read_exec_only ^ 1) << 9) |
2107 (contents << 10) |
2108 ((seg_not_present ^ 1) << 15) |
2109 (seg_32bit << 22) |
2110 (limit_in_pages << 23) |
2111 0x7000;
2112 if (!oldmode)
2113 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002114
bellard6dbad632003-03-16 18:05:05 +00002115 /* Install the new entry ... */
2116install:
2117 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2118 lp[0] = tswap32(entry_1);
2119 lp[1] = tswap32(entry_2);
2120 return 0;
2121}
2122
2123/* specific and weird i386 syscalls */
pbrook53a59602006-03-25 19:31:22 +00002124int do_modify_ldt(CPUX86State *env, int func, target_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002125{
2126 int ret = -ENOSYS;
ths3b46e622007-09-17 08:09:54 +00002127
bellard6dbad632003-03-16 18:05:05 +00002128 switch (func) {
2129 case 0:
2130 ret = read_ldt(ptr, bytecount);
2131 break;
2132 case 1:
2133 ret = write_ldt(env, ptr, bytecount, 1);
2134 break;
2135 case 0x11:
2136 ret = write_ldt(env, ptr, bytecount, 0);
2137 break;
2138 }
2139 return ret;
2140}
bellard1b6b0292003-03-22 17:31:38 +00002141
bellard2ab83ea2003-06-15 19:56:46 +00002142#endif /* defined(TARGET_I386) */
2143
bellard1b6b0292003-03-22 17:31:38 +00002144/* this stack is the equivalent of the kernel stack associated with a
2145 thread/process */
2146#define NEW_STACK_SIZE 8192
2147
2148static int clone_func(void *arg)
2149{
bellard2ab83ea2003-06-15 19:56:46 +00002150 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002151 cpu_loop(env);
2152 /* never exits */
2153 return 0;
2154}
2155
bellard2ab83ea2003-06-15 19:56:46 +00002156int do_fork(CPUState *env, unsigned int flags, unsigned long newsp)
bellard1b6b0292003-03-22 17:31:38 +00002157{
2158 int ret;
bellard5cd43932003-03-29 16:54:36 +00002159 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002160 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002161 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002162
bellard1b6b0292003-03-22 17:31:38 +00002163 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002164 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2165 memset(ts, 0, sizeof(TaskState));
2166 new_stack = ts->stack;
2167 ts->used = 1;
2168 /* add in task state list */
2169 ts->next = first_task_state;
2170 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002171 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002172 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002173#if defined(TARGET_I386)
2174 if (!newsp)
2175 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002176 new_env->regs[R_ESP] = newsp;
2177 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002178#elif defined(TARGET_ARM)
2179 if (!newsp)
2180 newsp = env->regs[13];
2181 new_env->regs[13] = newsp;
2182 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002183#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002184 if (!newsp)
2185 newsp = env->regwptr[22];
2186 new_env->regwptr[22] = newsp;
2187 new_env->regwptr[0] = 0;
2188 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002189 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002190#elif defined(TARGET_M68K)
2191 if (!newsp)
2192 newsp = env->aregs[7];
2193 new_env->aregs[7] = newsp;
2194 new_env->dregs[0] = 0;
2195 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002196#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002197 if (!newsp)
thsead93602007-09-06 00:18:15 +00002198 newsp = env->gpr[29][env->current_tc];
2199 new_env->gpr[29][env->current_tc] = newsp;
bellard67867302003-11-23 17:05:30 +00002200#elif defined(TARGET_PPC)
2201 if (!newsp)
2202 newsp = env->gpr[1];
2203 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002204 {
bellard67867302003-11-23 17:05:30 +00002205 int i;
2206 for (i = 7; i < 32; i++)
2207 new_env->gpr[i] = 0;
2208 }
bellardfdf9b3e2006-04-27 21:07:38 +00002209#elif defined(TARGET_SH4)
2210 if (!newsp)
2211 newsp = env->gregs[15];
2212 new_env->gregs[15] = newsp;
2213 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002214#elif defined(TARGET_ALPHA)
2215 if (!newsp)
2216 newsp = env->ir[30];
2217 new_env->ir[30] = newsp;
2218 /* ? */
2219 {
2220 int i;
2221 for (i = 7; i < 30; i++)
2222 new_env->ir[i] = 0;
2223 }
bellard2ab83ea2003-06-15 19:56:46 +00002224#else
2225#error unsupported target CPU
2226#endif
bellard5cd43932003-03-29 16:54:36 +00002227 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002228#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002229 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002230#else
2231 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2232#endif
bellard1b6b0292003-03-22 17:31:38 +00002233 } else {
2234 /* if no CLONE_VM, we consider it is a fork */
2235 if ((flags & ~CSIGNAL) != 0)
2236 return -EINVAL;
2237 ret = fork();
2238 }
2239 return ret;
2240}
2241
pbrook53a59602006-03-25 19:31:22 +00002242static long do_fcntl(int fd, int cmd, target_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002243{
2244 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002245 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002246 struct flock64 fl64;
2247 struct target_flock64 *target_fl64;
bellard7775e9e2003-05-14 22:46:48 +00002248 long ret;
pbrook53a59602006-03-25 19:31:22 +00002249
bellard7775e9e2003-05-14 22:46:48 +00002250 switch(cmd) {
2251 case TARGET_F_GETLK:
ths58134272007-03-31 18:59:32 +00002252 lock_user_struct(target_fl, arg, 1);
2253 fl.l_type = tswap16(target_fl->l_type);
2254 fl.l_whence = tswap16(target_fl->l_whence);
2255 fl.l_start = tswapl(target_fl->l_start);
2256 fl.l_len = tswapl(target_fl->l_len);
2257 fl.l_pid = tswapl(target_fl->l_pid);
2258 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002259 ret = fcntl(fd, cmd, &fl);
2260 if (ret == 0) {
pbrook53a59602006-03-25 19:31:22 +00002261 lock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002262 target_fl->l_type = tswap16(fl.l_type);
2263 target_fl->l_whence = tswap16(fl.l_whence);
2264 target_fl->l_start = tswapl(fl.l_start);
2265 target_fl->l_len = tswapl(fl.l_len);
2266 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002267 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002268 }
2269 break;
ths3b46e622007-09-17 08:09:54 +00002270
bellard7775e9e2003-05-14 22:46:48 +00002271 case TARGET_F_SETLK:
2272 case TARGET_F_SETLKW:
pbrook53a59602006-03-25 19:31:22 +00002273 lock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002274 fl.l_type = tswap16(target_fl->l_type);
2275 fl.l_whence = tswap16(target_fl->l_whence);
2276 fl.l_start = tswapl(target_fl->l_start);
2277 fl.l_len = tswapl(target_fl->l_len);
2278 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002279 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002280 ret = fcntl(fd, cmd, &fl);
2281 break;
ths3b46e622007-09-17 08:09:54 +00002282
bellard7775e9e2003-05-14 22:46:48 +00002283 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00002284 lock_user_struct(target_fl64, arg, 1);
2285 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2286 fl64.l_whence = tswap16(target_fl64->l_whence);
2287 fl64.l_start = tswapl(target_fl64->l_start);
2288 fl64.l_len = tswapl(target_fl64->l_len);
2289 fl64.l_pid = tswap16(target_fl64->l_pid);
2290 unlock_user_struct(target_fl64, arg, 0);
ths43f238d2007-01-05 20:55:49 +00002291 ret = fcntl(fd, cmd >> 1, &fl64);
2292 if (ret == 0) {
2293 lock_user_struct(target_fl64, arg, 0);
2294 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2295 target_fl64->l_whence = tswap16(fl64.l_whence);
2296 target_fl64->l_start = tswapl(fl64.l_start);
2297 target_fl64->l_len = tswapl(fl64.l_len);
2298 target_fl64->l_pid = tswapl(fl64.l_pid);
2299 unlock_user_struct(target_fl64, arg, 1);
2300 }
2301 break;
bellard7775e9e2003-05-14 22:46:48 +00002302 case TARGET_F_SETLK64:
2303 case TARGET_F_SETLKW64:
ths43f238d2007-01-05 20:55:49 +00002304 lock_user_struct(target_fl64, arg, 1);
2305 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2306 fl64.l_whence = tswap16(target_fl64->l_whence);
2307 fl64.l_start = tswapl(target_fl64->l_start);
2308 fl64.l_len = tswapl(target_fl64->l_len);
2309 fl64.l_pid = tswap16(target_fl64->l_pid);
2310 unlock_user_struct(target_fl64, arg, 0);
2311 ret = fcntl(fd, cmd >> 1, &fl64);
bellard7775e9e2003-05-14 22:46:48 +00002312 break;
2313
bellardffa65c32004-01-04 23:57:22 +00002314 case F_GETFL:
2315 ret = fcntl(fd, cmd, arg);
2316 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2317 break;
2318
2319 case F_SETFL:
2320 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2321 break;
2322
bellard7775e9e2003-05-14 22:46:48 +00002323 default:
2324 ret = fcntl(fd, cmd, arg);
2325 break;
2326 }
2327 return ret;
2328}
2329
bellard67867302003-11-23 17:05:30 +00002330#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002331
bellard67867302003-11-23 17:05:30 +00002332static inline int high2lowuid(int uid)
2333{
2334 if (uid > 65535)
2335 return 65534;
2336 else
2337 return uid;
2338}
2339
2340static inline int high2lowgid(int gid)
2341{
2342 if (gid > 65535)
2343 return 65534;
2344 else
2345 return gid;
2346}
2347
2348static inline int low2highuid(int uid)
2349{
2350 if ((int16_t)uid == -1)
2351 return -1;
2352 else
2353 return uid;
2354}
2355
2356static inline int low2highgid(int gid)
2357{
2358 if ((int16_t)gid == -1)
2359 return -1;
2360 else
2361 return gid;
2362}
2363
2364#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002365
bellard31e31b82003-02-18 22:55:36 +00002366void syscall_init(void)
2367{
bellard2ab83ea2003-06-15 19:56:46 +00002368 IOCTLEntry *ie;
2369 const argtype *arg_type;
2370 int size;
2371
ths5fafdf22007-09-16 21:08:06 +00002372#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2373#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002374#include "syscall_types.h"
2375#undef STRUCT
2376#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002377
2378 /* we patch the ioctl size if necessary. We rely on the fact that
2379 no ioctl has all the bits at '1' in the size field */
2380 ie = ioctl_entries;
2381 while (ie->target_cmd != 0) {
2382 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2383 TARGET_IOC_SIZEMASK) {
2384 arg_type = ie->arg_type;
2385 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002386 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002387 ie->target_cmd);
2388 exit(1);
2389 }
2390 arg_type++;
2391 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002392 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002393 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2394 (size << TARGET_IOC_SIZESHIFT);
2395 }
2396 /* automatic consistency check if same arch */
2397#if defined(__i386__) && defined(TARGET_I386)
2398 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002399 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002400 ie->target_cmd, ie->host_cmd);
2401 }
2402#endif
2403 ie++;
2404 }
bellard31e31b82003-02-18 22:55:36 +00002405}
bellardc573ff62004-01-04 15:51:36 +00002406
pbrookce4defa2006-02-09 16:49:55 +00002407static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2408{
2409#ifdef TARGET_WORDS_BIG_ENDIAN
2410 return ((uint64_t)word0 << 32) | word1;
2411#else
2412 return ((uint64_t)word1 << 32) | word0;
2413#endif
2414}
2415
2416#ifdef TARGET_NR_truncate64
2417static inline long target_truncate64(void *cpu_env, const char *arg1,
2418 long arg2, long arg3, long arg4)
2419{
2420#ifdef TARGET_ARM
2421 if (((CPUARMState *)cpu_env)->eabi)
2422 {
2423 arg2 = arg3;
2424 arg3 = arg4;
2425 }
2426#endif
2427 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2428}
2429#endif
2430
2431#ifdef TARGET_NR_ftruncate64
2432static inline long target_ftruncate64(void *cpu_env, long arg1, long arg2,
2433 long arg3, long arg4)
2434{
2435#ifdef TARGET_ARM
2436 if (((CPUARMState *)cpu_env)->eabi)
2437 {
2438 arg2 = arg3;
2439 arg3 = arg4;
2440 }
2441#endif
2442 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2443}
2444#endif
2445
pbrook53a59602006-03-25 19:31:22 +00002446static inline void target_to_host_timespec(struct timespec *host_ts,
2447 target_ulong target_addr)
2448{
2449 struct target_timespec *target_ts;
2450
2451 lock_user_struct(target_ts, target_addr, 1);
2452 host_ts->tv_sec = tswapl(target_ts->tv_sec);
2453 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2454 unlock_user_struct(target_ts, target_addr, 0);
2455}
2456
2457static inline void host_to_target_timespec(target_ulong target_addr,
2458 struct timespec *host_ts)
2459{
2460 struct target_timespec *target_ts;
2461
2462 lock_user_struct(target_ts, target_addr, 0);
2463 target_ts->tv_sec = tswapl(host_ts->tv_sec);
2464 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2465 unlock_user_struct(target_ts, target_addr, 1);
2466}
2467
ths5fafdf22007-09-16 21:08:06 +00002468long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
bellard31e31b82003-02-18 22:55:36 +00002469 long arg4, long arg5, long arg6)
2470{
2471 long ret;
2472 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00002473 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00002474 void *p;
ths3b46e622007-09-17 08:09:54 +00002475
bellard72f03902003-02-18 23:33:18 +00002476#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00002477 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00002478#endif
bellard31e31b82003-02-18 22:55:36 +00002479 switch(num) {
2480 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00002481#ifdef HAVE_GPROF
2482 _mcleanup();
2483#endif
bellarde9009672005-04-26 20:42:36 +00002484 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00002485 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00002486 _exit(arg1);
2487 ret = 0; /* avoid warning */
2488 break;
2489 case TARGET_NR_read:
pbrook53a59602006-03-25 19:31:22 +00002490 page_unprotect_range(arg2, arg3);
2491 p = lock_user(arg2, arg3, 0);
2492 ret = get_errno(read(arg1, p, arg3));
2493 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00002494 break;
2495 case TARGET_NR_write:
pbrook53a59602006-03-25 19:31:22 +00002496 p = lock_user(arg2, arg3, 1);
2497 ret = get_errno(write(arg1, p, arg3));
2498 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00002499 break;
2500 case TARGET_NR_open:
pbrook53a59602006-03-25 19:31:22 +00002501 p = lock_user_string(arg1);
2502 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00002503 target_to_host_bitmask(arg2, fcntl_flags_tbl),
2504 arg3));
pbrook53a59602006-03-25 19:31:22 +00002505 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002506 break;
ths82424832007-09-24 09:21:55 +00002507#if defined(TARGET_NR_openat) && defined(__NR_openat)
2508 case TARGET_NR_openat:
2509 if (!arg2) {
2510 ret = -EFAULT;
2511 goto fail;
2512 }
2513 p = lock_user_string(arg2);
2514 if (!access_ok(VERIFY_READ, p, 1))
2515 ret = -EFAULT;
2516 else
2517 ret = get_errno(sys_openat(arg1,
2518 path(p),
2519 target_to_host_bitmask(arg3, fcntl_flags_tbl),
2520 arg4));
2521 if (p)
2522 unlock_user(p, arg2, 0);
2523 break;
2524#endif
bellard31e31b82003-02-18 22:55:36 +00002525 case TARGET_NR_close:
2526 ret = get_errno(close(arg1));
2527 break;
2528 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00002529 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00002530 break;
2531 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00002532 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00002533 break;
thse5febef2007-04-01 18:31:35 +00002534#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00002535 case TARGET_NR_waitpid:
2536 {
pbrook53a59602006-03-25 19:31:22 +00002537 int status;
2538 ret = get_errno(waitpid(arg1, &status, arg3));
2539 if (!is_error(ret) && arg2)
2540 tput32(arg2, status);
bellard31e31b82003-02-18 22:55:36 +00002541 }
2542 break;
thse5febef2007-04-01 18:31:35 +00002543#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002544#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002545 case TARGET_NR_creat:
pbrook53a59602006-03-25 19:31:22 +00002546 p = lock_user_string(arg1);
2547 ret = get_errno(creat(p, arg2));
2548 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002549 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002550#endif
bellard31e31b82003-02-18 22:55:36 +00002551 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00002552 {
2553 void * p2;
2554 p = lock_user_string(arg1);
2555 p2 = lock_user_string(arg2);
2556 ret = get_errno(link(p, p2));
2557 unlock_user(p2, arg2, 0);
2558 unlock_user(p, arg1, 0);
2559 }
bellard31e31b82003-02-18 22:55:36 +00002560 break;
2561 case TARGET_NR_unlink:
pbrook53a59602006-03-25 19:31:22 +00002562 p = lock_user_string(arg1);
2563 ret = get_errno(unlink(p));
2564 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002565 break;
2566 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00002567 {
2568 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00002569 int argc, envc;
pbrook53a59602006-03-25 19:31:22 +00002570 target_ulong gp;
2571 target_ulong guest_argp;
2572 target_ulong guest_envp;
2573 target_ulong addr;
bellard7854b052003-03-29 17:22:23 +00002574 char **q;
2575
bellardf7341ff2003-03-30 21:00:25 +00002576 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00002577 guest_argp = arg2;
2578 for (gp = guest_argp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002579 argc++;
bellardf7341ff2003-03-30 21:00:25 +00002580 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00002581 guest_envp = arg3;
2582 for (gp = guest_envp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002583 envc++;
2584
bellardf7341ff2003-03-30 21:00:25 +00002585 argp = alloca((argc + 1) * sizeof(void *));
2586 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00002587
pbrook53a59602006-03-25 19:31:22 +00002588 for (gp = guest_argp, q = argp; ;
2589 gp += sizeof(target_ulong), q++) {
2590 addr = tgetl(gp);
2591 if (!addr)
2592 break;
2593 *q = lock_user_string(addr);
2594 }
bellardf7341ff2003-03-30 21:00:25 +00002595 *q = NULL;
2596
pbrook53a59602006-03-25 19:31:22 +00002597 for (gp = guest_envp, q = envp; ;
2598 gp += sizeof(target_ulong), q++) {
2599 addr = tgetl(gp);
2600 if (!addr)
2601 break;
2602 *q = lock_user_string(addr);
2603 }
bellardf7341ff2003-03-30 21:00:25 +00002604 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00002605
pbrook53a59602006-03-25 19:31:22 +00002606 p = lock_user_string(arg1);
2607 ret = get_errno(execve(p, argp, envp));
2608 unlock_user(p, arg1, 0);
2609
2610 for (gp = guest_argp, q = argp; *q;
2611 gp += sizeof(target_ulong), q++) {
2612 addr = tgetl(gp);
2613 unlock_user(*q, addr, 0);
2614 }
2615 for (gp = guest_envp, q = envp; *q;
2616 gp += sizeof(target_ulong), q++) {
2617 addr = tgetl(gp);
2618 unlock_user(*q, addr, 0);
2619 }
bellard7854b052003-03-29 17:22:23 +00002620 }
bellard31e31b82003-02-18 22:55:36 +00002621 break;
2622 case TARGET_NR_chdir:
pbrook53a59602006-03-25 19:31:22 +00002623 p = lock_user_string(arg1);
2624 ret = get_errno(chdir(p));
2625 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002626 break;
bellarda315a142005-01-30 22:59:18 +00002627#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00002628 case TARGET_NR_time:
2629 {
pbrook53a59602006-03-25 19:31:22 +00002630 time_t host_time;
2631 ret = get_errno(time(&host_time));
2632 if (!is_error(ret) && arg1)
2633 tputl(arg1, host_time);
bellard31e31b82003-02-18 22:55:36 +00002634 }
2635 break;
bellarda315a142005-01-30 22:59:18 +00002636#endif
bellard31e31b82003-02-18 22:55:36 +00002637 case TARGET_NR_mknod:
pbrook53a59602006-03-25 19:31:22 +00002638 p = lock_user_string(arg1);
2639 ret = get_errno(mknod(p, arg2, arg3));
2640 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002641 break;
2642 case TARGET_NR_chmod:
pbrook53a59602006-03-25 19:31:22 +00002643 p = lock_user_string(arg1);
2644 ret = get_errno(chmod(p, arg2));
2645 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002646 break;
bellardebc05482003-09-30 21:08:41 +00002647#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00002648 case TARGET_NR_break:
2649 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002650#endif
2651#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00002652 case TARGET_NR_oldstat:
2653 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002654#endif
bellard31e31b82003-02-18 22:55:36 +00002655 case TARGET_NR_lseek:
2656 ret = get_errno(lseek(arg1, arg2, arg3));
2657 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002658#ifdef TARGET_NR_getxpid
2659 case TARGET_NR_getxpid:
2660#else
bellard31e31b82003-02-18 22:55:36 +00002661 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00002662#endif
bellard31e31b82003-02-18 22:55:36 +00002663 ret = get_errno(getpid());
2664 break;
2665 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00002666 {
2667 /* need to look at the data field */
2668 void *p2, *p3;
2669 p = lock_user_string(arg1);
2670 p2 = lock_user_string(arg2);
2671 p3 = lock_user_string(arg3);
2672 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, (const void *)arg5));
2673 unlock_user(p, arg1, 0);
2674 unlock_user(p2, arg2, 0);
2675 unlock_user(p3, arg3, 0);
2676 break;
2677 }
thse5febef2007-04-01 18:31:35 +00002678#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00002679 case TARGET_NR_umount:
pbrook53a59602006-03-25 19:31:22 +00002680 p = lock_user_string(arg1);
2681 ret = get_errno(umount(p));
2682 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002683 break;
thse5febef2007-04-01 18:31:35 +00002684#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002685#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002686 case TARGET_NR_stime:
2687 {
pbrook53a59602006-03-25 19:31:22 +00002688 time_t host_time;
2689 host_time = tgetl(arg1);
2690 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00002691 }
2692 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002693#endif
bellard31e31b82003-02-18 22:55:36 +00002694 case TARGET_NR_ptrace:
2695 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00002696#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002697 case TARGET_NR_alarm:
2698 ret = alarm(arg1);
2699 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002700#endif
bellardebc05482003-09-30 21:08:41 +00002701#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00002702 case TARGET_NR_oldfstat:
2703 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002704#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002705#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002706 case TARGET_NR_pause:
2707 ret = get_errno(pause());
2708 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002709#endif
thse5febef2007-04-01 18:31:35 +00002710#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00002711 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00002712 {
pbrook53a59602006-03-25 19:31:22 +00002713 struct utimbuf tbuf, *host_tbuf;
2714 struct target_utimbuf *target_tbuf;
2715 if (arg2) {
2716 lock_user_struct(target_tbuf, arg2, 1);
2717 tbuf.actime = tswapl(target_tbuf->actime);
2718 tbuf.modtime = tswapl(target_tbuf->modtime);
2719 unlock_user_struct(target_tbuf, arg2, 0);
2720 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00002721 } else {
pbrook53a59602006-03-25 19:31:22 +00002722 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00002723 }
pbrook53a59602006-03-25 19:31:22 +00002724 p = lock_user_string(arg1);
2725 ret = get_errno(utime(p, host_tbuf));
2726 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00002727 }
2728 break;
thse5febef2007-04-01 18:31:35 +00002729#endif
bellard978a66f2004-12-06 22:58:05 +00002730 case TARGET_NR_utimes:
2731 {
bellard978a66f2004-12-06 22:58:05 +00002732 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00002733 if (arg2) {
2734 target_to_host_timeval(&tv[0], arg2);
2735 target_to_host_timeval(&tv[1],
2736 arg2 + sizeof (struct target_timeval));
bellard978a66f2004-12-06 22:58:05 +00002737 tvp = tv;
2738 } else {
2739 tvp = NULL;
2740 }
pbrook53a59602006-03-25 19:31:22 +00002741 p = lock_user_string(arg1);
2742 ret = get_errno(utimes(p, tvp));
2743 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00002744 }
2745 break;
bellardebc05482003-09-30 21:08:41 +00002746#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00002747 case TARGET_NR_stty:
2748 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002749#endif
2750#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00002751 case TARGET_NR_gtty:
2752 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002753#endif
bellard31e31b82003-02-18 22:55:36 +00002754 case TARGET_NR_access:
pbrook53a59602006-03-25 19:31:22 +00002755 p = lock_user_string(arg1);
2756 ret = get_errno(access(p, arg2));
2757 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002758 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002759#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002760 case TARGET_NR_nice:
2761 ret = get_errno(nice(arg1));
2762 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002763#endif
bellardebc05482003-09-30 21:08:41 +00002764#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00002765 case TARGET_NR_ftime:
2766 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002767#endif
bellard31e31b82003-02-18 22:55:36 +00002768 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00002769 sync();
2770 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00002771 break;
2772 case TARGET_NR_kill:
2773 ret = get_errno(kill(arg1, arg2));
2774 break;
2775 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00002776 {
2777 void *p2;
2778 p = lock_user_string(arg1);
2779 p2 = lock_user_string(arg2);
2780 ret = get_errno(rename(p, p2));
2781 unlock_user(p2, arg2, 0);
2782 unlock_user(p, arg1, 0);
2783 }
bellard31e31b82003-02-18 22:55:36 +00002784 break;
2785 case TARGET_NR_mkdir:
pbrook53a59602006-03-25 19:31:22 +00002786 p = lock_user_string(arg1);
2787 ret = get_errno(mkdir(p, arg2));
2788 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002789 break;
2790 case TARGET_NR_rmdir:
pbrook53a59602006-03-25 19:31:22 +00002791 p = lock_user_string(arg1);
2792 ret = get_errno(rmdir(p));
2793 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002794 break;
2795 case TARGET_NR_dup:
2796 ret = get_errno(dup(arg1));
2797 break;
2798 case TARGET_NR_pipe:
2799 {
pbrook53a59602006-03-25 19:31:22 +00002800 int host_pipe[2];
2801 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00002802 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00002803#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00002804 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
2805 env->gpr[3][env->current_tc] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00002806 ret = host_pipe[0];
2807#else
pbrook53a59602006-03-25 19:31:22 +00002808 tput32(arg1, host_pipe[0]);
2809 tput32(arg1 + 4, host_pipe[1]);
thsc12ab052007-06-01 11:50:36 +00002810#endif
bellard31e31b82003-02-18 22:55:36 +00002811 }
2812 }
2813 break;
2814 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00002815 {
pbrook53a59602006-03-25 19:31:22 +00002816 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00002817 struct tms tms;
2818 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00002819 if (arg1) {
2820 tmsp = lock_user(arg1, sizeof(struct target_tms), 0);
bellardc596ed12003-07-13 17:32:31 +00002821 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
2822 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
2823 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
2824 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00002825 }
bellardc596ed12003-07-13 17:32:31 +00002826 if (!is_error(ret))
2827 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00002828 }
2829 break;
bellardebc05482003-09-30 21:08:41 +00002830#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00002831 case TARGET_NR_prof:
2832 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002833#endif
thse5febef2007-04-01 18:31:35 +00002834#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00002835 case TARGET_NR_signal:
2836 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00002837#endif
bellard31e31b82003-02-18 22:55:36 +00002838 case TARGET_NR_acct:
pbrook24836682006-04-16 14:14:53 +00002839 p = lock_user_string(arg1);
2840 ret = get_errno(acct(path(p)));
2841 unlock_user(p, arg1, 0);
2842 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002843#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002844 case TARGET_NR_umount2:
pbrook53a59602006-03-25 19:31:22 +00002845 p = lock_user_string(arg1);
2846 ret = get_errno(umount2(p, arg2));
2847 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002848 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002849#endif
bellardebc05482003-09-30 21:08:41 +00002850#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00002851 case TARGET_NR_lock:
2852 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002853#endif
bellard31e31b82003-02-18 22:55:36 +00002854 case TARGET_NR_ioctl:
2855 ret = do_ioctl(arg1, arg2, arg3);
2856 break;
2857 case TARGET_NR_fcntl:
bellard7775e9e2003-05-14 22:46:48 +00002858 ret = get_errno(do_fcntl(arg1, arg2, arg3));
bellard31e31b82003-02-18 22:55:36 +00002859 break;
bellardebc05482003-09-30 21:08:41 +00002860#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00002861 case TARGET_NR_mpx:
2862 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002863#endif
bellard31e31b82003-02-18 22:55:36 +00002864 case TARGET_NR_setpgid:
2865 ret = get_errno(setpgid(arg1, arg2));
2866 break;
bellardebc05482003-09-30 21:08:41 +00002867#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00002868 case TARGET_NR_ulimit:
2869 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002870#endif
2871#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00002872 case TARGET_NR_oldolduname:
2873 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002874#endif
bellard31e31b82003-02-18 22:55:36 +00002875 case TARGET_NR_umask:
2876 ret = get_errno(umask(arg1));
2877 break;
2878 case TARGET_NR_chroot:
pbrook53a59602006-03-25 19:31:22 +00002879 p = lock_user_string(arg1);
2880 ret = get_errno(chroot(p));
2881 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002882 break;
2883 case TARGET_NR_ustat:
2884 goto unimplemented;
2885 case TARGET_NR_dup2:
2886 ret = get_errno(dup2(arg1, arg2));
2887 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002888#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002889 case TARGET_NR_getppid:
2890 ret = get_errno(getppid());
2891 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002892#endif
bellard31e31b82003-02-18 22:55:36 +00002893 case TARGET_NR_getpgrp:
2894 ret = get_errno(getpgrp());
2895 break;
2896 case TARGET_NR_setsid:
2897 ret = get_errno(setsid());
2898 break;
thse5febef2007-04-01 18:31:35 +00002899#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00002900 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00002901 {
ths388bb212007-05-13 13:58:00 +00002902#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00002903 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00002904 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00002905 if (arg2) {
2906 lock_user_struct(old_act, arg2, 1);
bellard66fb9762003-03-23 01:06:05 +00002907 act._sa_handler = old_act->_sa_handler;
2908 target_siginitset(&act.sa_mask, old_act->sa_mask);
2909 act.sa_flags = old_act->sa_flags;
2910 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00002911 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00002912 pact = &act;
2913 } else {
2914 pact = NULL;
2915 }
2916 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00002917 if (!is_error(ret) && arg3) {
2918 lock_user_struct(old_act, arg3, 0);
2919 old_act->_sa_handler = oact._sa_handler;
2920 old_act->sa_mask = oact.sa_mask.sig[0];
2921 old_act->sa_flags = oact.sa_flags;
2922 old_act->sa_restorer = oact.sa_restorer;
2923 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00002924 }
ths388bb212007-05-13 13:58:00 +00002925#else
bellard106ec872006-06-27 21:08:10 +00002926 struct target_sigaction act, oact, *pact, *old_act;
2927
2928 if (arg2) {
2929 lock_user_struct(old_act, arg2, 1);
2930 act._sa_handler = old_act->_sa_handler;
2931 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
2932 act.sa_flags = old_act->sa_flags;
2933 unlock_user_struct(old_act, arg2, 0);
2934 pact = &act;
2935 } else {
2936 pact = NULL;
2937 }
2938
2939 ret = get_errno(do_sigaction(arg1, pact, &oact));
2940
2941 if (!is_error(ret) && arg3) {
2942 lock_user_struct(old_act, arg3, 0);
2943 old_act->_sa_handler = oact._sa_handler;
2944 old_act->sa_flags = oact.sa_flags;
2945 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
2946 old_act->sa_mask.sig[1] = 0;
2947 old_act->sa_mask.sig[2] = 0;
2948 old_act->sa_mask.sig[3] = 0;
2949 unlock_user_struct(old_act, arg3, 1);
2950 }
ths388bb212007-05-13 13:58:00 +00002951#endif
bellard31e31b82003-02-18 22:55:36 +00002952 }
2953 break;
thse5febef2007-04-01 18:31:35 +00002954#endif
bellard66fb9762003-03-23 01:06:05 +00002955 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00002956 {
2957 struct target_sigaction *act;
2958 struct target_sigaction *oact;
2959
2960 if (arg2)
2961 lock_user_struct(act, arg2, 1);
2962 else
2963 act = NULL;
2964 if (arg3)
2965 lock_user_struct(oact, arg3, 0);
2966 else
2967 oact = NULL;
2968 ret = get_errno(do_sigaction(arg1, act, oact));
2969 if (arg2)
2970 unlock_user_struct(act, arg2, 0);
2971 if (arg3)
2972 unlock_user_struct(oact, arg3, 1);
2973 }
bellard66fb9762003-03-23 01:06:05 +00002974 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002975#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002976 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00002977 {
2978 sigset_t cur_set;
2979 target_ulong target_set;
2980 sigprocmask(0, NULL, &cur_set);
2981 host_to_target_old_sigset(&target_set, &cur_set);
2982 ret = target_set;
2983 }
2984 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002985#endif
2986#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002987 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00002988 {
2989 sigset_t set, oset, cur_set;
2990 target_ulong target_set = arg1;
2991 sigprocmask(0, NULL, &cur_set);
2992 target_to_host_old_sigset(&set, &target_set);
2993 sigorset(&set, &set, &cur_set);
2994 sigprocmask(SIG_SETMASK, &set, &oset);
2995 host_to_target_old_sigset(&target_set, &oset);
2996 ret = target_set;
2997 }
2998 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002999#endif
thse5febef2007-04-01 18:31:35 +00003000#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003001 case TARGET_NR_sigprocmask:
3002 {
3003 int how = arg1;
3004 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003005
pbrook53a59602006-03-25 19:31:22 +00003006 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003007 switch(how) {
3008 case TARGET_SIG_BLOCK:
3009 how = SIG_BLOCK;
3010 break;
3011 case TARGET_SIG_UNBLOCK:
3012 how = SIG_UNBLOCK;
3013 break;
3014 case TARGET_SIG_SETMASK:
3015 how = SIG_SETMASK;
3016 break;
3017 default:
3018 ret = -EINVAL;
3019 goto fail;
3020 }
pbrook53a59602006-03-25 19:31:22 +00003021 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3022 target_to_host_old_sigset(&set, p);
3023 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003024 set_ptr = &set;
3025 } else {
3026 how = 0;
3027 set_ptr = NULL;
3028 }
3029 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003030 if (!is_error(ret) && arg3) {
3031 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3032 host_to_target_old_sigset(p, &oldset);
3033 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003034 }
3035 }
3036 break;
thse5febef2007-04-01 18:31:35 +00003037#endif
bellard66fb9762003-03-23 01:06:05 +00003038 case TARGET_NR_rt_sigprocmask:
3039 {
3040 int how = arg1;
3041 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003042
pbrook53a59602006-03-25 19:31:22 +00003043 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003044 switch(how) {
3045 case TARGET_SIG_BLOCK:
3046 how = SIG_BLOCK;
3047 break;
3048 case TARGET_SIG_UNBLOCK:
3049 how = SIG_UNBLOCK;
3050 break;
3051 case TARGET_SIG_SETMASK:
3052 how = SIG_SETMASK;
3053 break;
3054 default:
3055 ret = -EINVAL;
3056 goto fail;
3057 }
pbrook53a59602006-03-25 19:31:22 +00003058 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3059 target_to_host_sigset(&set, p);
3060 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003061 set_ptr = &set;
3062 } else {
3063 how = 0;
3064 set_ptr = NULL;
3065 }
3066 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003067 if (!is_error(ret) && arg3) {
3068 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3069 host_to_target_sigset(p, &oldset);
3070 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003071 }
3072 }
3073 break;
thse5febef2007-04-01 18:31:35 +00003074#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003075 case TARGET_NR_sigpending:
3076 {
3077 sigset_t set;
3078 ret = get_errno(sigpending(&set));
3079 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003080 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3081 host_to_target_old_sigset(p, &set);
3082 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003083 }
3084 }
3085 break;
thse5febef2007-04-01 18:31:35 +00003086#endif
bellard66fb9762003-03-23 01:06:05 +00003087 case TARGET_NR_rt_sigpending:
3088 {
3089 sigset_t set;
3090 ret = get_errno(sigpending(&set));
3091 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003092 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3093 host_to_target_sigset(p, &set);
3094 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003095 }
3096 }
3097 break;
thse5febef2007-04-01 18:31:35 +00003098#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003099 case TARGET_NR_sigsuspend:
3100 {
3101 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003102 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3103 target_to_host_old_sigset(&set, p);
3104 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003105 ret = get_errno(sigsuspend(&set));
3106 }
3107 break;
thse5febef2007-04-01 18:31:35 +00003108#endif
bellard66fb9762003-03-23 01:06:05 +00003109 case TARGET_NR_rt_sigsuspend:
3110 {
3111 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003112 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3113 target_to_host_sigset(&set, p);
3114 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003115 ret = get_errno(sigsuspend(&set));
3116 }
3117 break;
3118 case TARGET_NR_rt_sigtimedwait:
3119 {
bellard66fb9762003-03-23 01:06:05 +00003120 sigset_t set;
3121 struct timespec uts, *puts;
3122 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003123
pbrook53a59602006-03-25 19:31:22 +00003124 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3125 target_to_host_sigset(&set, p);
3126 unlock_user(p, arg1, 0);
3127 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003128 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003129 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003130 } else {
3131 puts = NULL;
3132 }
3133 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003134 if (!is_error(ret) && arg2) {
3135 p = lock_user(arg2, sizeof(target_sigset_t), 0);
3136 host_to_target_siginfo(p, &uinfo);
3137 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003138 }
3139 }
3140 break;
3141 case TARGET_NR_rt_sigqueueinfo:
3142 {
3143 siginfo_t uinfo;
pbrook53a59602006-03-25 19:31:22 +00003144 p = lock_user(arg3, sizeof(target_sigset_t), 1);
3145 target_to_host_siginfo(&uinfo, p);
3146 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003147 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3148 }
3149 break;
thse5febef2007-04-01 18:31:35 +00003150#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003151 case TARGET_NR_sigreturn:
3152 /* NOTE: ret is eax, so not transcoding must be done */
3153 ret = do_sigreturn(cpu_env);
3154 break;
thse5febef2007-04-01 18:31:35 +00003155#endif
bellard66fb9762003-03-23 01:06:05 +00003156 case TARGET_NR_rt_sigreturn:
3157 /* NOTE: ret is eax, so not transcoding must be done */
3158 ret = do_rt_sigreturn(cpu_env);
3159 break;
bellard31e31b82003-02-18 22:55:36 +00003160 case TARGET_NR_sethostname:
pbrook53a59602006-03-25 19:31:22 +00003161 p = lock_user_string(arg1);
3162 ret = get_errno(sethostname(p, arg2));
3163 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003164 break;
3165 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003166 {
3167 /* XXX: convert resource ? */
3168 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003169 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003170 struct rlimit rlim;
pbrook53a59602006-03-25 19:31:22 +00003171 lock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003172 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3173 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003174 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003175 ret = get_errno(setrlimit(resource, &rlim));
3176 }
3177 break;
bellard31e31b82003-02-18 22:55:36 +00003178 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003179 {
3180 /* XXX: convert resource ? */
3181 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003182 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003183 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003184
bellard9de5e442003-03-23 16:49:39 +00003185 ret = get_errno(getrlimit(resource, &rlim));
3186 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003187 lock_user_struct(target_rlim, arg2, 0);
3188 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3189 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3190 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003191 }
3192 }
3193 break;
bellard31e31b82003-02-18 22:55:36 +00003194 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003195 {
3196 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003197 ret = get_errno(getrusage(arg1, &rusage));
3198 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003199 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003200 }
3201 }
3202 break;
bellard31e31b82003-02-18 22:55:36 +00003203 case TARGET_NR_gettimeofday:
3204 {
bellard31e31b82003-02-18 22:55:36 +00003205 struct timeval tv;
3206 ret = get_errno(gettimeofday(&tv, NULL));
3207 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003208 host_to_target_timeval(arg1, &tv);
bellard31e31b82003-02-18 22:55:36 +00003209 }
3210 }
3211 break;
3212 case TARGET_NR_settimeofday:
3213 {
bellard31e31b82003-02-18 22:55:36 +00003214 struct timeval tv;
pbrook53a59602006-03-25 19:31:22 +00003215 target_to_host_timeval(&tv, arg1);
bellard31e31b82003-02-18 22:55:36 +00003216 ret = get_errno(settimeofday(&tv, NULL));
3217 }
3218 break;
bellard048f6b42005-11-26 18:47:20 +00003219#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003220 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003221 {
pbrook53a59602006-03-25 19:31:22 +00003222 struct target_sel_arg_struct *sel;
3223 target_ulong inp, outp, exp, tvp;
3224 long nsel;
3225
3226 lock_user_struct(sel, arg1, 1);
3227 nsel = tswapl(sel->n);
3228 inp = tswapl(sel->inp);
3229 outp = tswapl(sel->outp);
3230 exp = tswapl(sel->exp);
3231 tvp = tswapl(sel->tvp);
3232 unlock_user_struct(sel, arg1, 0);
3233 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003234 }
3235 break;
bellard048f6b42005-11-26 18:47:20 +00003236#endif
bellard31e31b82003-02-18 22:55:36 +00003237 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003238 {
3239 void *p2;
3240 p = lock_user_string(arg1);
3241 p2 = lock_user_string(arg2);
3242 ret = get_errno(symlink(p, p2));
3243 unlock_user(p2, arg2, 0);
3244 unlock_user(p, arg1, 0);
3245 }
bellard31e31b82003-02-18 22:55:36 +00003246 break;
bellardebc05482003-09-30 21:08:41 +00003247#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00003248 case TARGET_NR_oldlstat:
3249 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003250#endif
bellard31e31b82003-02-18 22:55:36 +00003251 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00003252 {
3253 void *p2;
3254 p = lock_user_string(arg1);
3255 p2 = lock_user(arg2, arg3, 0);
3256 ret = get_errno(readlink(path(p), p2, arg3));
3257 unlock_user(p2, arg2, ret);
3258 unlock_user(p, arg1, 0);
3259 }
bellard31e31b82003-02-18 22:55:36 +00003260 break;
thse5febef2007-04-01 18:31:35 +00003261#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00003262 case TARGET_NR_uselib:
3263 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003264#endif
3265#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00003266 case TARGET_NR_swapon:
pbrook53a59602006-03-25 19:31:22 +00003267 p = lock_user_string(arg1);
3268 ret = get_errno(swapon(p, arg2));
3269 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003270 break;
thse5febef2007-04-01 18:31:35 +00003271#endif
bellard31e31b82003-02-18 22:55:36 +00003272 case TARGET_NR_reboot:
3273 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003274#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00003275 case TARGET_NR_readdir:
3276 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003277#endif
3278#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00003279 case TARGET_NR_mmap:
pbrooke6e59062006-10-22 00:18:54 +00003280#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K)
bellard31e31b82003-02-18 22:55:36 +00003281 {
pbrook53a59602006-03-25 19:31:22 +00003282 target_ulong *v;
3283 target_ulong v1, v2, v3, v4, v5, v6;
3284 v = lock_user(arg1, 6 * sizeof(target_ulong), 1);
3285 v1 = tswapl(v[0]);
3286 v2 = tswapl(v[1]);
3287 v3 = tswapl(v[2]);
3288 v4 = tswapl(v[3]);
3289 v5 = tswapl(v[4]);
3290 v6 = tswapl(v[5]);
3291 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00003292 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00003293 target_to_host_bitmask(v4, mmap_flags_tbl),
3294 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00003295 }
bellard31e31b82003-02-18 22:55:36 +00003296#else
ths5fafdf22007-09-16 21:08:06 +00003297 ret = get_errno(target_mmap(arg1, arg2, arg3,
3298 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00003299 arg5,
3300 arg6));
bellard31e31b82003-02-18 22:55:36 +00003301#endif
bellard6fb883e2003-07-09 17:12:39 +00003302 break;
thse5febef2007-04-01 18:31:35 +00003303#endif
bellarda315a142005-01-30 22:59:18 +00003304#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00003305 case TARGET_NR_mmap2:
ths0d3267a2006-12-11 22:04:25 +00003306#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
bellardc573ff62004-01-04 15:51:36 +00003307#define MMAP_SHIFT 12
3308#else
3309#define MMAP_SHIFT TARGET_PAGE_BITS
3310#endif
ths5fafdf22007-09-16 21:08:06 +00003311 ret = get_errno(target_mmap(arg1, arg2, arg3,
3312 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00003313 arg5,
bellardc573ff62004-01-04 15:51:36 +00003314 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00003315 break;
bellarda315a142005-01-30 22:59:18 +00003316#endif
bellard31e31b82003-02-18 22:55:36 +00003317 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00003318 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00003319 break;
bellard9de5e442003-03-23 16:49:39 +00003320 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00003321 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003322 break;
thse5febef2007-04-01 18:31:35 +00003323#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00003324 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00003325 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00003326 break;
thse5febef2007-04-01 18:31:35 +00003327#endif
pbrook53a59602006-03-25 19:31:22 +00003328 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00003329#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00003330 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00003331 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003332 break;
thse5febef2007-04-01 18:31:35 +00003333#endif
3334#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00003335 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00003336 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003337 break;
thse5febef2007-04-01 18:31:35 +00003338#endif
3339#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00003340 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00003341 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003342 break;
thse5febef2007-04-01 18:31:35 +00003343#endif
3344#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00003345 case TARGET_NR_mlockall:
3346 ret = get_errno(mlockall(arg1));
3347 break;
thse5febef2007-04-01 18:31:35 +00003348#endif
3349#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00003350 case TARGET_NR_munlockall:
3351 ret = get_errno(munlockall());
3352 break;
thse5febef2007-04-01 18:31:35 +00003353#endif
bellard31e31b82003-02-18 22:55:36 +00003354 case TARGET_NR_truncate:
pbrook53a59602006-03-25 19:31:22 +00003355 p = lock_user_string(arg1);
3356 ret = get_errno(truncate(p, arg2));
3357 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003358 break;
3359 case TARGET_NR_ftruncate:
3360 ret = get_errno(ftruncate(arg1, arg2));
3361 break;
3362 case TARGET_NR_fchmod:
3363 ret = get_errno(fchmod(arg1, arg2));
3364 break;
bellard31e31b82003-02-18 22:55:36 +00003365 case TARGET_NR_getpriority:
3366 ret = get_errno(getpriority(arg1, arg2));
3367 break;
3368 case TARGET_NR_setpriority:
3369 ret = get_errno(setpriority(arg1, arg2, arg3));
3370 break;
bellardebc05482003-09-30 21:08:41 +00003371#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00003372 case TARGET_NR_profil:
3373 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003374#endif
bellard31e31b82003-02-18 22:55:36 +00003375 case TARGET_NR_statfs:
pbrook53a59602006-03-25 19:31:22 +00003376 p = lock_user_string(arg1);
3377 ret = get_errno(statfs(path(p), &stfs));
3378 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003379 convert_statfs:
3380 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003381 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003382
pbrook53a59602006-03-25 19:31:22 +00003383 lock_user_struct(target_stfs, arg2, 0);
3384 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003385 put_user(stfs.f_type, &target_stfs->f_type);
3386 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3387 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3388 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3389 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3390 put_user(stfs.f_files, &target_stfs->f_files);
3391 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003392 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3393 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003394 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003395 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003396 }
3397 break;
3398 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00003399 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00003400 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00003401#ifdef TARGET_NR_statfs64
3402 case TARGET_NR_statfs64:
pbrook53a59602006-03-25 19:31:22 +00003403 p = lock_user_string(arg1);
3404 ret = get_errno(statfs(path(p), &stfs));
3405 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00003406 convert_statfs64:
3407 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003408 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003409
pbrook53a59602006-03-25 19:31:22 +00003410 lock_user_struct(target_stfs, arg3, 0);
3411 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003412 put_user(stfs.f_type, &target_stfs->f_type);
3413 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3414 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3415 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3416 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3417 put_user(stfs.f_files, &target_stfs->f_files);
3418 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003419 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3420 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003421 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003422 unlock_user_struct(target_stfs, arg3, 0);
bellard56c8f682005-11-28 22:28:41 +00003423 }
3424 break;
3425 case TARGET_NR_fstatfs64:
3426 ret = get_errno(fstatfs(arg1, &stfs));
3427 goto convert_statfs64;
3428#endif
bellardebc05482003-09-30 21:08:41 +00003429#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00003430 case TARGET_NR_ioperm:
3431 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003432#endif
thse5febef2007-04-01 18:31:35 +00003433#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00003434 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00003435 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003436 break;
thse5febef2007-04-01 18:31:35 +00003437#endif
bellard3532fa72006-06-24 15:06:03 +00003438#ifdef TARGET_NR_accept
3439 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00003440 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003441 break;
3442#endif
3443#ifdef TARGET_NR_bind
3444 case TARGET_NR_bind:
3445 ret = do_bind(arg1, arg2, arg3);
3446 break;
3447#endif
3448#ifdef TARGET_NR_connect
3449 case TARGET_NR_connect:
3450 ret = do_connect(arg1, arg2, arg3);
3451 break;
3452#endif
3453#ifdef TARGET_NR_getpeername
3454 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00003455 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003456 break;
3457#endif
3458#ifdef TARGET_NR_getsockname
3459 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00003460 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003461 break;
3462#endif
3463#ifdef TARGET_NR_getsockopt
3464 case TARGET_NR_getsockopt:
3465 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3466 break;
3467#endif
3468#ifdef TARGET_NR_listen
3469 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00003470 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003471 break;
3472#endif
3473#ifdef TARGET_NR_recv
3474 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00003475 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003476 break;
3477#endif
3478#ifdef TARGET_NR_recvfrom
3479 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00003480 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003481 break;
3482#endif
3483#ifdef TARGET_NR_recvmsg
3484 case TARGET_NR_recvmsg:
3485 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3486 break;
3487#endif
3488#ifdef TARGET_NR_send
3489 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00003490 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003491 break;
3492#endif
3493#ifdef TARGET_NR_sendmsg
3494 case TARGET_NR_sendmsg:
3495 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3496 break;
3497#endif
3498#ifdef TARGET_NR_sendto
3499 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00003500 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003501 break;
3502#endif
3503#ifdef TARGET_NR_shutdown
3504 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00003505 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003506 break;
3507#endif
3508#ifdef TARGET_NR_socket
3509 case TARGET_NR_socket:
3510 ret = do_socket(arg1, arg2, arg3);
3511 break;
3512#endif
3513#ifdef TARGET_NR_socketpair
3514 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00003515 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00003516 break;
3517#endif
3518#ifdef TARGET_NR_setsockopt
3519 case TARGET_NR_setsockopt:
3520 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3521 break;
3522#endif
ths7494b0f2007-02-11 18:26:53 +00003523
bellard31e31b82003-02-18 22:55:36 +00003524 case TARGET_NR_syslog:
thse5574482007-02-11 20:03:13 +00003525 p = lock_user_string(arg2);
3526 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3527 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00003528 break;
3529
bellard31e31b82003-02-18 22:55:36 +00003530 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00003531 {
bellard66fb9762003-03-23 01:06:05 +00003532 struct itimerval value, ovalue, *pvalue;
3533
pbrook53a59602006-03-25 19:31:22 +00003534 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003535 pvalue = &value;
ths5fafdf22007-09-16 21:08:06 +00003536 target_to_host_timeval(&pvalue->it_interval,
pbrook53a59602006-03-25 19:31:22 +00003537 arg2);
ths5fafdf22007-09-16 21:08:06 +00003538 target_to_host_timeval(&pvalue->it_value,
pbrook53a59602006-03-25 19:31:22 +00003539 arg2 + sizeof(struct target_timeval));
bellard66fb9762003-03-23 01:06:05 +00003540 } else {
3541 pvalue = NULL;
3542 }
3543 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00003544 if (!is_error(ret) && arg3) {
3545 host_to_target_timeval(arg3,
bellard66fb9762003-03-23 01:06:05 +00003546 &ovalue.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003547 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003548 &ovalue.it_value);
3549 }
3550 }
3551 break;
bellard31e31b82003-02-18 22:55:36 +00003552 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00003553 {
bellard66fb9762003-03-23 01:06:05 +00003554 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00003555
bellard66fb9762003-03-23 01:06:05 +00003556 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00003557 if (!is_error(ret) && arg2) {
3558 host_to_target_timeval(arg2,
bellard66fb9762003-03-23 01:06:05 +00003559 &value.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003560 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003561 &value.it_value);
3562 }
3563 }
3564 break;
bellard31e31b82003-02-18 22:55:36 +00003565 case TARGET_NR_stat:
pbrook53a59602006-03-25 19:31:22 +00003566 p = lock_user_string(arg1);
3567 ret = get_errno(stat(path(p), &st));
3568 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003569 goto do_stat;
3570 case TARGET_NR_lstat:
pbrook53a59602006-03-25 19:31:22 +00003571 p = lock_user_string(arg1);
3572 ret = get_errno(lstat(path(p), &st));
3573 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003574 goto do_stat;
3575 case TARGET_NR_fstat:
3576 {
3577 ret = get_errno(fstat(arg1, &st));
3578 do_stat:
3579 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003580 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00003581
pbrook53a59602006-03-25 19:31:22 +00003582 lock_user_struct(target_st, arg2, 0);
blueswir11b8dd642007-07-08 10:08:24 +00003583#if defined(TARGET_MIPS) || defined(TARGET_SPARC64)
thse3584652007-06-01 11:49:38 +00003584 target_st->st_dev = tswap32(st.st_dev);
3585#else
bellard31e31b82003-02-18 22:55:36 +00003586 target_st->st_dev = tswap16(st.st_dev);
thse3584652007-06-01 11:49:38 +00003587#endif
bellard31e31b82003-02-18 22:55:36 +00003588 target_st->st_ino = tswapl(st.st_ino);
ths7d600c82006-12-08 01:32:58 +00003589#if defined(TARGET_PPC) || defined(TARGET_MIPS)
bellard67867302003-11-23 17:05:30 +00003590 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
3591 target_st->st_uid = tswap32(st.st_uid);
3592 target_st->st_gid = tswap32(st.st_gid);
blueswir11b8dd642007-07-08 10:08:24 +00003593#elif defined(TARGET_SPARC64)
3594 target_st->st_mode = tswap32(st.st_mode);
3595 target_st->st_uid = tswap32(st.st_uid);
3596 target_st->st_gid = tswap32(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003597#else
bellardec86b0f2003-04-11 00:15:04 +00003598 target_st->st_mode = tswap16(st.st_mode);
bellard31e31b82003-02-18 22:55:36 +00003599 target_st->st_uid = tswap16(st.st_uid);
3600 target_st->st_gid = tswap16(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003601#endif
thse3584652007-06-01 11:49:38 +00003602#if defined(TARGET_MIPS)
3603 /* If this is the same on PPC, then just merge w/ the above ifdef */
3604 target_st->st_nlink = tswapl(st.st_nlink);
3605 target_st->st_rdev = tswapl(st.st_rdev);
blueswir11b8dd642007-07-08 10:08:24 +00003606#elif defined(TARGET_SPARC64)
3607 target_st->st_nlink = tswap32(st.st_nlink);
3608 target_st->st_rdev = tswap32(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003609#else
bellard67867302003-11-23 17:05:30 +00003610 target_st->st_nlink = tswap16(st.st_nlink);
bellard31e31b82003-02-18 22:55:36 +00003611 target_st->st_rdev = tswap16(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003612#endif
bellard31e31b82003-02-18 22:55:36 +00003613 target_st->st_size = tswapl(st.st_size);
3614 target_st->st_blksize = tswapl(st.st_blksize);
3615 target_st->st_blocks = tswapl(st.st_blocks);
bellard7854b052003-03-29 17:22:23 +00003616 target_st->target_st_atime = tswapl(st.st_atime);
3617 target_st->target_st_mtime = tswapl(st.st_mtime);
3618 target_st->target_st_ctime = tswapl(st.st_ctime);
pbrook53a59602006-03-25 19:31:22 +00003619 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003620 }
3621 }
3622 break;
bellardebc05482003-09-30 21:08:41 +00003623#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00003624 case TARGET_NR_olduname:
3625 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003626#endif
3627#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00003628 case TARGET_NR_iopl:
3629 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003630#endif
bellard31e31b82003-02-18 22:55:36 +00003631 case TARGET_NR_vhangup:
3632 ret = get_errno(vhangup());
3633 break;
bellardebc05482003-09-30 21:08:41 +00003634#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00003635 case TARGET_NR_idle:
3636 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003637#endif
bellard42ad6ae2005-01-03 22:48:11 +00003638#ifdef TARGET_NR_syscall
3639 case TARGET_NR_syscall:
3640 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
3641 break;
3642#endif
bellard31e31b82003-02-18 22:55:36 +00003643 case TARGET_NR_wait4:
3644 {
3645 int status;
pbrook53a59602006-03-25 19:31:22 +00003646 target_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00003647 struct rusage rusage, *rusage_ptr;
pbrook53a59602006-03-25 19:31:22 +00003648 target_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00003649 if (target_rusage)
3650 rusage_ptr = &rusage;
3651 else
3652 rusage_ptr = NULL;
3653 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
3654 if (!is_error(ret)) {
3655 if (status_ptr)
pbrook53a59602006-03-25 19:31:22 +00003656 tputl(status_ptr, status);
bellard31e31b82003-02-18 22:55:36 +00003657 if (target_rusage) {
bellardb4091862003-05-16 15:39:34 +00003658 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00003659 }
3660 }
3661 }
3662 break;
thse5febef2007-04-01 18:31:35 +00003663#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00003664 case TARGET_NR_swapoff:
pbrook53a59602006-03-25 19:31:22 +00003665 p = lock_user_string(arg1);
3666 ret = get_errno(swapoff(p));
3667 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003668 break;
thse5febef2007-04-01 18:31:35 +00003669#endif
bellard31e31b82003-02-18 22:55:36 +00003670 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00003671 {
pbrook53a59602006-03-25 19:31:22 +00003672 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00003673 struct sysinfo value;
3674 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00003675 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00003676 {
pbrook53a59602006-03-25 19:31:22 +00003677 /* ??? __put_user is probably wrong. */
3678 lock_user_struct(target_value, arg1, 0);
bellarda5448a72004-06-19 16:59:03 +00003679 __put_user(value.uptime, &target_value->uptime);
3680 __put_user(value.loads[0], &target_value->loads[0]);
3681 __put_user(value.loads[1], &target_value->loads[1]);
3682 __put_user(value.loads[2], &target_value->loads[2]);
3683 __put_user(value.totalram, &target_value->totalram);
3684 __put_user(value.freeram, &target_value->freeram);
3685 __put_user(value.sharedram, &target_value->sharedram);
3686 __put_user(value.bufferram, &target_value->bufferram);
3687 __put_user(value.totalswap, &target_value->totalswap);
3688 __put_user(value.freeswap, &target_value->freeswap);
3689 __put_user(value.procs, &target_value->procs);
3690 __put_user(value.totalhigh, &target_value->totalhigh);
3691 __put_user(value.freehigh, &target_value->freehigh);
3692 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00003693 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00003694 }
3695 }
3696 break;
thse5febef2007-04-01 18:31:35 +00003697#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00003698 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00003699 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
3700 break;
thse5febef2007-04-01 18:31:35 +00003701#endif
bellard31e31b82003-02-18 22:55:36 +00003702 case TARGET_NR_fsync:
3703 ret = get_errno(fsync(arg1));
3704 break;
bellard31e31b82003-02-18 22:55:36 +00003705 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00003706 ret = get_errno(do_fork(cpu_env, arg1, arg2));
3707 break;
bellardec86b0f2003-04-11 00:15:04 +00003708#ifdef __NR_exit_group
3709 /* new thread calls */
3710 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00003711 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00003712 ret = get_errno(exit_group(arg1));
3713 break;
3714#endif
bellard31e31b82003-02-18 22:55:36 +00003715 case TARGET_NR_setdomainname:
pbrook53a59602006-03-25 19:31:22 +00003716 p = lock_user_string(arg1);
3717 ret = get_errno(setdomainname(p, arg2));
3718 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003719 break;
3720 case TARGET_NR_uname:
3721 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00003722 {
3723 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00003724
pbrook53a59602006-03-25 19:31:22 +00003725 lock_user_struct(buf, arg1, 0);
bellard29e619b2004-09-13 21:41:04 +00003726 ret = get_errno(sys_uname(buf));
3727 if (!is_error(ret)) {
3728 /* Overrite the native machine name with whatever is being
3729 emulated. */
3730 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00003731 /* Allow the user to override the reported release. */
3732 if (qemu_uname_release && *qemu_uname_release)
3733 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00003734 }
pbrook53a59602006-03-25 19:31:22 +00003735 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00003736 }
bellard31e31b82003-02-18 22:55:36 +00003737 break;
bellard6dbad632003-03-16 18:05:05 +00003738#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00003739 case TARGET_NR_modify_ldt:
pbrook53a59602006-03-25 19:31:22 +00003740 ret = get_errno(do_modify_ldt(cpu_env, arg1, arg2, arg3));
bellard5cd43932003-03-29 16:54:36 +00003741 break;
j_mayer84409dd2007-04-06 08:56:50 +00003742#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00003743 case TARGET_NR_vm86old:
3744 goto unimplemented;
3745 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00003746 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00003747 break;
3748#endif
j_mayer84409dd2007-04-06 08:56:50 +00003749#endif
bellard31e31b82003-02-18 22:55:36 +00003750 case TARGET_NR_adjtimex:
3751 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003752#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00003753 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00003754#endif
bellard31e31b82003-02-18 22:55:36 +00003755 case TARGET_NR_init_module:
3756 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00003757#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00003758 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00003759#endif
bellard31e31b82003-02-18 22:55:36 +00003760 goto unimplemented;
3761 case TARGET_NR_quotactl:
3762 goto unimplemented;
3763 case TARGET_NR_getpgid:
3764 ret = get_errno(getpgid(arg1));
3765 break;
3766 case TARGET_NR_fchdir:
3767 ret = get_errno(fchdir(arg1));
3768 break;
j_mayer84409dd2007-04-06 08:56:50 +00003769#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00003770 case TARGET_NR_bdflush:
3771 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00003772#endif
thse5febef2007-04-01 18:31:35 +00003773#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00003774 case TARGET_NR_sysfs:
3775 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003776#endif
bellard31e31b82003-02-18 22:55:36 +00003777 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00003778 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00003779 break;
thse5febef2007-04-01 18:31:35 +00003780#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00003781 case TARGET_NR_afs_syscall:
3782 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003783#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003784#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003785 case TARGET_NR__llseek:
3786 {
bellard4f2ac232004-04-26 19:44:02 +00003787#if defined (__x86_64__)
3788 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
pbrook53a59602006-03-25 19:31:22 +00003789 tput64(arg4, ret);
bellard4f2ac232004-04-26 19:44:02 +00003790#else
bellard31e31b82003-02-18 22:55:36 +00003791 int64_t res;
3792 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
pbrook53a59602006-03-25 19:31:22 +00003793 tput64(arg4, res);
bellard4f2ac232004-04-26 19:44:02 +00003794#endif
bellard31e31b82003-02-18 22:55:36 +00003795 }
3796 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003797#endif
bellard31e31b82003-02-18 22:55:36 +00003798 case TARGET_NR_getdents:
3799#if TARGET_LONG_SIZE != 4
pbrook53a59602006-03-25 19:31:22 +00003800 goto unimplemented;
bellarda315a142005-01-30 22:59:18 +00003801#warning not supported
bellard4add45b2003-06-05 01:52:59 +00003802#elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
3803 {
pbrook53a59602006-03-25 19:31:22 +00003804 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00003805 struct dirent *dirp;
3806 long count = arg3;
3807
3808 dirp = malloc(count);
3809 if (!dirp)
3810 return -ENOMEM;
ths3b46e622007-09-17 08:09:54 +00003811
bellard4add45b2003-06-05 01:52:59 +00003812 ret = get_errno(sys_getdents(arg1, dirp, count));
3813 if (!is_error(ret)) {
3814 struct dirent *de;
3815 struct target_dirent *tde;
3816 int len = ret;
3817 int reclen, treclen;
3818 int count1, tnamelen;
3819
3820 count1 = 0;
3821 de = dirp;
pbrook53a59602006-03-25 19:31:22 +00003822 target_dirp = lock_user(arg2, count, 0);
bellard4add45b2003-06-05 01:52:59 +00003823 tde = target_dirp;
3824 while (len > 0) {
3825 reclen = de->d_reclen;
3826 treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
3827 tde->d_reclen = tswap16(treclen);
3828 tde->d_ino = tswapl(de->d_ino);
3829 tde->d_off = tswapl(de->d_off);
3830 tnamelen = treclen - (2 * sizeof(target_long) + 2);
3831 if (tnamelen > 256)
3832 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00003833 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00003834 strncpy(tde->d_name, de->d_name, tnamelen);
3835 de = (struct dirent *)((char *)de + reclen);
3836 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00003837 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00003838 count1 += treclen;
3839 }
3840 ret = count1;
3841 }
pbrook53a59602006-03-25 19:31:22 +00003842 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00003843 free(dirp);
3844 }
3845#else
bellard31e31b82003-02-18 22:55:36 +00003846 {
pbrook53a59602006-03-25 19:31:22 +00003847 struct dirent *dirp;
bellard31e31b82003-02-18 22:55:36 +00003848 long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00003849
pbrook53a59602006-03-25 19:31:22 +00003850 dirp = lock_user(arg2, count, 0);
bellard72f03902003-02-18 23:33:18 +00003851 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00003852 if (!is_error(ret)) {
3853 struct dirent *de;
3854 int len = ret;
3855 int reclen;
3856 de = dirp;
3857 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00003858 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00003859 if (reclen > len)
3860 break;
bellard8083a3e2003-03-24 23:12:16 +00003861 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00003862 tswapls(&de->d_ino);
3863 tswapls(&de->d_off);
3864 de = (struct dirent *)((char *)de + reclen);
3865 len -= reclen;
3866 }
3867 }
pbrook53a59602006-03-25 19:31:22 +00003868 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00003869 }
bellard4add45b2003-06-05 01:52:59 +00003870#endif
bellard31e31b82003-02-18 22:55:36 +00003871 break;
ths3ae43202007-09-16 21:39:48 +00003872#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00003873 case TARGET_NR_getdents64:
3874 {
pbrook53a59602006-03-25 19:31:22 +00003875 struct dirent64 *dirp;
bellarddab2ed92003-03-22 15:23:14 +00003876 long count = arg3;
pbrook53a59602006-03-25 19:31:22 +00003877 dirp = lock_user(arg2, count, 0);
bellarddab2ed92003-03-22 15:23:14 +00003878 ret = get_errno(sys_getdents64(arg1, dirp, count));
3879 if (!is_error(ret)) {
3880 struct dirent64 *de;
3881 int len = ret;
3882 int reclen;
3883 de = dirp;
3884 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00003885 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00003886 if (reclen > len)
3887 break;
bellard8083a3e2003-03-24 23:12:16 +00003888 de->d_reclen = tswap16(reclen);
bellarddab2ed92003-03-22 15:23:14 +00003889 tswap64s(&de->d_ino);
3890 tswap64s(&de->d_off);
3891 de = (struct dirent64 *)((char *)de + reclen);
3892 len -= reclen;
3893 }
3894 }
pbrook53a59602006-03-25 19:31:22 +00003895 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00003896 }
3897 break;
bellarda541f292004-04-12 20:39:29 +00003898#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00003899#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00003900 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00003901 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00003902 break;
thse5febef2007-04-01 18:31:35 +00003903#endif
3904#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00003905 case TARGET_NR_poll:
3906 {
pbrook53a59602006-03-25 19:31:22 +00003907 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00003908 unsigned int nfds = arg2;
3909 int timeout = arg3;
3910 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00003911 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00003912
pbrook53a59602006-03-25 19:31:22 +00003913 target_pfd = lock_user(arg1, sizeof(struct target_pollfd) * nfds, 1);
bellard9de5e442003-03-23 16:49:39 +00003914 pfd = alloca(sizeof(struct pollfd) * nfds);
3915 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00003916 pfd[i].fd = tswap32(target_pfd[i].fd);
3917 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00003918 }
3919 ret = get_errno(poll(pfd, nfds, timeout));
3920 if (!is_error(ret)) {
3921 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00003922 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00003923 }
pbrook53a59602006-03-25 19:31:22 +00003924 ret += nfds * (sizeof(struct target_pollfd)
3925 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00003926 }
pbrook53a59602006-03-25 19:31:22 +00003927 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00003928 }
3929 break;
thse5febef2007-04-01 18:31:35 +00003930#endif
bellard31e31b82003-02-18 22:55:36 +00003931 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00003932 /* NOTE: the flock constant seems to be the same for every
3933 Linux platform */
3934 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00003935 break;
3936 case TARGET_NR_readv:
3937 {
3938 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00003939 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00003940
3941 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00003942 lock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00003943 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00003944 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00003945 }
3946 break;
3947 case TARGET_NR_writev:
3948 {
3949 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00003950 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00003951
3952 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00003953 lock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00003954 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00003955 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00003956 }
3957 break;
3958 case TARGET_NR_getsid:
3959 ret = get_errno(getsid(arg1));
3960 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003961#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00003962 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00003963 ret = get_errno(fdatasync(arg1));
3964 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003965#endif
bellard31e31b82003-02-18 22:55:36 +00003966 case TARGET_NR__sysctl:
bellard29e619b2004-09-13 21:41:04 +00003967 /* We don't implement this, but ENODIR is always a safe
3968 return value. */
3969 return -ENOTDIR;
bellard31e31b82003-02-18 22:55:36 +00003970 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00003971 {
pbrook53a59602006-03-25 19:31:22 +00003972 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00003973 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00003974
3975 lock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00003976 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00003977 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00003978 ret = get_errno(sched_setparam(arg1, &schp));
3979 }
3980 break;
bellard31e31b82003-02-18 22:55:36 +00003981 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00003982 {
pbrook53a59602006-03-25 19:31:22 +00003983 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00003984 struct sched_param schp;
3985 ret = get_errno(sched_getparam(arg1, &schp));
3986 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003987 lock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00003988 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00003989 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00003990 }
3991 }
3992 break;
bellard31e31b82003-02-18 22:55:36 +00003993 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00003994 {
pbrook53a59602006-03-25 19:31:22 +00003995 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00003996 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00003997 lock_user_struct(target_schp, arg3, 1);
bellard5cd43932003-03-29 16:54:36 +00003998 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00003999 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004000 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4001 }
4002 break;
bellard31e31b82003-02-18 22:55:36 +00004003 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004004 ret = get_errno(sched_getscheduler(arg1));
4005 break;
bellard31e31b82003-02-18 22:55:36 +00004006 case TARGET_NR_sched_yield:
4007 ret = get_errno(sched_yield());
4008 break;
4009 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004010 ret = get_errno(sched_get_priority_max(arg1));
4011 break;
bellard31e31b82003-02-18 22:55:36 +00004012 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004013 ret = get_errno(sched_get_priority_min(arg1));
4014 break;
bellard31e31b82003-02-18 22:55:36 +00004015 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004016 {
bellard5cd43932003-03-29 16:54:36 +00004017 struct timespec ts;
4018 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4019 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004020 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004021 }
4022 }
4023 break;
bellard31e31b82003-02-18 22:55:36 +00004024 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004025 {
bellard1b6b0292003-03-22 17:31:38 +00004026 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004027 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004028 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004029 if (is_error(ret) && arg2) {
4030 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004031 }
4032 }
4033 break;
thse5febef2007-04-01 18:31:35 +00004034#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004035 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004036 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004037#endif
4038#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004039 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004040 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004041#endif
bellard31e31b82003-02-18 22:55:36 +00004042 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004043 switch (arg1)
4044 {
4045 case PR_GET_PDEATHSIG:
4046 {
4047 int deathsig;
4048 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4049 if (!is_error(ret) && arg2)
4050 tput32(arg2, deathsig);
4051 }
4052 break;
4053 default:
4054 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4055 break;
4056 }
ths39b9aae2007-02-11 18:36:44 +00004057 break;
bellard67867302003-11-23 17:05:30 +00004058#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004059 case TARGET_NR_pread:
pbrook53a59602006-03-25 19:31:22 +00004060 page_unprotect_range(arg2, arg3);
4061 p = lock_user(arg2, arg3, 0);
4062 ret = get_errno(pread(arg1, p, arg3, arg4));
4063 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004064 break;
bellard31e31b82003-02-18 22:55:36 +00004065 case TARGET_NR_pwrite:
pbrook53a59602006-03-25 19:31:22 +00004066 p = lock_user(arg2, arg3, 1);
4067 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4068 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004069 break;
bellard67867302003-11-23 17:05:30 +00004070#endif
bellard31e31b82003-02-18 22:55:36 +00004071 case TARGET_NR_getcwd:
pbrook53a59602006-03-25 19:31:22 +00004072 p = lock_user(arg1, arg2, 0);
4073 ret = get_errno(sys_getcwd1(p, arg2));
4074 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004075 break;
4076 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004077 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004078 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004079 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004080 case TARGET_NR_sigaltstack:
bellard5cd43932003-03-29 16:54:36 +00004081 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004082 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004083 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004084#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004085 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004086 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004087#endif
4088#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004089 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004090 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004091#endif
bellard048f6b42005-11-26 18:47:20 +00004092#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004093 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004094 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004095 break;
bellard048f6b42005-11-26 18:47:20 +00004096#endif
bellardebc05482003-09-30 21:08:41 +00004097#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004098 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004099 {
4100 struct rlimit rlim;
4101 ret = get_errno(getrlimit(arg1, &rlim));
4102 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004103 struct target_rlimit *target_rlim;
4104 lock_user_struct(target_rlim, arg2, 0);
bellard728584b2003-04-29 20:43:36 +00004105 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4106 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004107 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004108 }
4109 break;
4110 }
bellardebc05482003-09-30 21:08:41 +00004111#endif
bellarda315a142005-01-30 22:59:18 +00004112#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004113 case TARGET_NR_truncate64:
pbrook53a59602006-03-25 19:31:22 +00004114 p = lock_user_string(arg1);
4115 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4116 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004117 break;
bellarda315a142005-01-30 22:59:18 +00004118#endif
4119#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004120 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004121 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004122 break;
bellarda315a142005-01-30 22:59:18 +00004123#endif
4124#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004125 case TARGET_NR_stat64:
pbrook53a59602006-03-25 19:31:22 +00004126 p = lock_user_string(arg1);
4127 ret = get_errno(stat(path(p), &st));
4128 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004129 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004130#endif
4131#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004132 case TARGET_NR_lstat64:
pbrook53a59602006-03-25 19:31:22 +00004133 p = lock_user_string(arg1);
4134 ret = get_errno(lstat(path(p), &st));
4135 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004136 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004137#endif
4138#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004139 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004140 {
4141 ret = get_errno(fstat(arg1, &st));
4142 do_stat64:
4143 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004144#ifdef TARGET_ARM
4145 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004146 struct target_eabi_stat64 *target_st;
4147 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004148 memset(target_st, 0, sizeof(struct target_eabi_stat64));
pbrook53a59602006-03-25 19:31:22 +00004149 /* put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004150 put_user(st.st_dev, &target_st->st_dev);
4151 put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004152#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
pbrookce4defa2006-02-09 16:49:55 +00004153 put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004154#endif
pbrookce4defa2006-02-09 16:49:55 +00004155 put_user(st.st_mode, &target_st->st_mode);
4156 put_user(st.st_nlink, &target_st->st_nlink);
4157 put_user(st.st_uid, &target_st->st_uid);
4158 put_user(st.st_gid, &target_st->st_gid);
4159 put_user(st.st_rdev, &target_st->st_rdev);
4160 /* XXX: better use of kernel struct */
4161 put_user(st.st_size, &target_st->st_size);
4162 put_user(st.st_blksize, &target_st->st_blksize);
4163 put_user(st.st_blocks, &target_st->st_blocks);
4164 put_user(st.st_atime, &target_st->target_st_atime);
4165 put_user(st.st_mtime, &target_st->target_st_mtime);
4166 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004167 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004168 } else
4169#endif
4170 {
pbrook53a59602006-03-25 19:31:22 +00004171 struct target_stat64 *target_st;
4172 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004173 memset(target_st, 0, sizeof(struct target_stat64));
pbrook53a59602006-03-25 19:31:22 +00004174 /* ??? put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004175 put_user(st.st_dev, &target_st->st_dev);
4176 put_user(st.st_ino, &target_st->st_ino);
4177#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4178 put_user(st.st_ino, &target_st->__st_ino);
4179#endif
4180 put_user(st.st_mode, &target_st->st_mode);
4181 put_user(st.st_nlink, &target_st->st_nlink);
4182 put_user(st.st_uid, &target_st->st_uid);
4183 put_user(st.st_gid, &target_st->st_gid);
4184 put_user(st.st_rdev, &target_st->st_rdev);
4185 /* XXX: better use of kernel struct */
4186 put_user(st.st_size, &target_st->st_size);
4187 put_user(st.st_blksize, &target_st->st_blksize);
4188 put_user(st.st_blocks, &target_st->st_blocks);
4189 put_user(st.st_atime, &target_st->target_st_atime);
4190 put_user(st.st_mtime, &target_st->target_st_mtime);
4191 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004192 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004193 }
bellard60cd49d2003-03-16 22:53:56 +00004194 }
4195 }
4196 break;
bellarda315a142005-01-30 22:59:18 +00004197#endif
bellard67867302003-11-23 17:05:30 +00004198#ifdef USE_UID16
4199 case TARGET_NR_lchown:
pbrook53a59602006-03-25 19:31:22 +00004200 p = lock_user_string(arg1);
4201 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4202 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004203 break;
4204 case TARGET_NR_getuid:
4205 ret = get_errno(high2lowuid(getuid()));
4206 break;
4207 case TARGET_NR_getgid:
4208 ret = get_errno(high2lowgid(getgid()));
4209 break;
4210 case TARGET_NR_geteuid:
4211 ret = get_errno(high2lowuid(geteuid()));
4212 break;
4213 case TARGET_NR_getegid:
4214 ret = get_errno(high2lowgid(getegid()));
4215 break;
4216 case TARGET_NR_setreuid:
4217 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4218 break;
4219 case TARGET_NR_setregid:
4220 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4221 break;
4222 case TARGET_NR_getgroups:
4223 {
4224 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004225 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004226 gid_t *grouplist;
4227 int i;
4228
4229 grouplist = alloca(gidsetsize * sizeof(gid_t));
4230 ret = get_errno(getgroups(gidsetsize, grouplist));
4231 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004232 target_grouplist = lock_user(arg2, gidsetsize * 2, 0);
bellard67867302003-11-23 17:05:30 +00004233 for(i = 0;i < gidsetsize; i++)
4234 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004235 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00004236 }
4237 }
4238 break;
4239 case TARGET_NR_setgroups:
4240 {
4241 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004242 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004243 gid_t *grouplist;
4244 int i;
4245
4246 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004247 target_grouplist = lock_user(arg2, gidsetsize * 2, 1);
bellard67867302003-11-23 17:05:30 +00004248 for(i = 0;i < gidsetsize; i++)
4249 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004250 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00004251 ret = get_errno(setgroups(gidsetsize, grouplist));
4252 }
4253 break;
4254 case TARGET_NR_fchown:
4255 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4256 break;
4257#ifdef TARGET_NR_setresuid
4258 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00004259 ret = get_errno(setresuid(low2highuid(arg1),
4260 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00004261 low2highuid(arg3)));
4262 break;
4263#endif
4264#ifdef TARGET_NR_getresuid
4265 case TARGET_NR_getresuid:
4266 {
pbrook53a59602006-03-25 19:31:22 +00004267 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00004268 ret = get_errno(getresuid(&ruid, &euid, &suid));
4269 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004270 tput16(arg1, tswap16(high2lowuid(ruid)));
4271 tput16(arg2, tswap16(high2lowuid(euid)));
4272 tput16(arg3, tswap16(high2lowuid(suid)));
bellard67867302003-11-23 17:05:30 +00004273 }
4274 }
4275 break;
4276#endif
4277#ifdef TARGET_NR_getresgid
4278 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00004279 ret = get_errno(setresgid(low2highgid(arg1),
4280 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00004281 low2highgid(arg3)));
4282 break;
4283#endif
4284#ifdef TARGET_NR_getresgid
4285 case TARGET_NR_getresgid:
4286 {
pbrook53a59602006-03-25 19:31:22 +00004287 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00004288 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4289 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004290 tput16(arg1, tswap16(high2lowgid(rgid)));
4291 tput16(arg2, tswap16(high2lowgid(egid)));
4292 tput16(arg3, tswap16(high2lowgid(sgid)));
bellard67867302003-11-23 17:05:30 +00004293 }
4294 }
4295 break;
4296#endif
4297 case TARGET_NR_chown:
pbrook53a59602006-03-25 19:31:22 +00004298 p = lock_user_string(arg1);
4299 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4300 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004301 break;
4302 case TARGET_NR_setuid:
4303 ret = get_errno(setuid(low2highuid(arg1)));
4304 break;
4305 case TARGET_NR_setgid:
4306 ret = get_errno(setgid(low2highgid(arg1)));
4307 break;
4308 case TARGET_NR_setfsuid:
4309 ret = get_errno(setfsuid(arg1));
4310 break;
4311 case TARGET_NR_setfsgid:
4312 ret = get_errno(setfsgid(arg1));
4313 break;
4314#endif /* USE_UID16 */
4315
bellarda315a142005-01-30 22:59:18 +00004316#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00004317 case TARGET_NR_lchown32:
pbrook53a59602006-03-25 19:31:22 +00004318 p = lock_user_string(arg1);
4319 ret = get_errno(lchown(p, arg2, arg3));
4320 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004321 break;
bellarda315a142005-01-30 22:59:18 +00004322#endif
4323#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00004324 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00004325 ret = get_errno(getuid());
4326 break;
bellarda315a142005-01-30 22:59:18 +00004327#endif
4328#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00004329 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00004330 ret = get_errno(getgid());
4331 break;
bellarda315a142005-01-30 22:59:18 +00004332#endif
4333#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00004334 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00004335 ret = get_errno(geteuid());
4336 break;
bellarda315a142005-01-30 22:59:18 +00004337#endif
4338#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00004339 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00004340 ret = get_errno(getegid());
4341 break;
bellarda315a142005-01-30 22:59:18 +00004342#endif
4343#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00004344 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00004345 ret = get_errno(setreuid(arg1, arg2));
4346 break;
bellarda315a142005-01-30 22:59:18 +00004347#endif
4348#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00004349 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00004350 ret = get_errno(setregid(arg1, arg2));
4351 break;
bellarda315a142005-01-30 22:59:18 +00004352#endif
4353#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00004354 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00004355 {
4356 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004357 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004358 gid_t *grouplist;
4359 int i;
4360
4361 grouplist = alloca(gidsetsize * sizeof(gid_t));
4362 ret = get_errno(getgroups(gidsetsize, grouplist));
4363 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004364 target_grouplist = lock_user(arg2, gidsetsize * 4, 0);
bellard99c475a2005-01-31 20:45:13 +00004365 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004366 target_grouplist[i] = tswap32(grouplist[i]);
4367 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00004368 }
4369 }
4370 break;
bellarda315a142005-01-30 22:59:18 +00004371#endif
4372#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00004373 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00004374 {
4375 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004376 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004377 gid_t *grouplist;
4378 int i;
ths3b46e622007-09-17 08:09:54 +00004379
bellard99c475a2005-01-31 20:45:13 +00004380 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004381 target_grouplist = lock_user(arg2, gidsetsize * 4, 1);
bellard99c475a2005-01-31 20:45:13 +00004382 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004383 grouplist[i] = tswap32(target_grouplist[i]);
4384 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00004385 ret = get_errno(setgroups(gidsetsize, grouplist));
4386 }
4387 break;
bellarda315a142005-01-30 22:59:18 +00004388#endif
4389#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00004390 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00004391 ret = get_errno(fchown(arg1, arg2, arg3));
4392 break;
bellarda315a142005-01-30 22:59:18 +00004393#endif
4394#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00004395 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004396 ret = get_errno(setresuid(arg1, arg2, arg3));
4397 break;
bellarda315a142005-01-30 22:59:18 +00004398#endif
4399#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00004400 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004401 {
pbrook53a59602006-03-25 19:31:22 +00004402 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00004403 ret = get_errno(getresuid(&ruid, &euid, &suid));
4404 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004405 tput32(arg1, tswap32(ruid));
4406 tput32(arg2, tswap32(euid));
4407 tput32(arg3, tswap32(suid));
bellardb03c60f2003-03-23 17:19:56 +00004408 }
4409 }
4410 break;
bellarda315a142005-01-30 22:59:18 +00004411#endif
4412#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00004413 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004414 ret = get_errno(setresgid(arg1, arg2, arg3));
4415 break;
bellarda315a142005-01-30 22:59:18 +00004416#endif
4417#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00004418 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004419 {
pbrook53a59602006-03-25 19:31:22 +00004420 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00004421 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4422 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004423 tput32(arg1, tswap32(rgid));
4424 tput32(arg2, tswap32(egid));
4425 tput32(arg3, tswap32(sgid));
bellardb03c60f2003-03-23 17:19:56 +00004426 }
4427 }
4428 break;
bellarda315a142005-01-30 22:59:18 +00004429#endif
4430#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00004431 case TARGET_NR_chown32:
pbrook53a59602006-03-25 19:31:22 +00004432 p = lock_user_string(arg1);
4433 ret = get_errno(chown(p, arg2, arg3));
4434 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004435 break;
bellarda315a142005-01-30 22:59:18 +00004436#endif
4437#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00004438 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00004439 ret = get_errno(setuid(arg1));
4440 break;
bellarda315a142005-01-30 22:59:18 +00004441#endif
4442#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00004443 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00004444 ret = get_errno(setgid(arg1));
4445 break;
bellarda315a142005-01-30 22:59:18 +00004446#endif
4447#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00004448 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00004449 ret = get_errno(setfsuid(arg1));
4450 break;
bellarda315a142005-01-30 22:59:18 +00004451#endif
4452#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00004453 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00004454 ret = get_errno(setfsgid(arg1));
4455 break;
bellarda315a142005-01-30 22:59:18 +00004456#endif
bellard67867302003-11-23 17:05:30 +00004457
bellard31e31b82003-02-18 22:55:36 +00004458 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00004459 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004460#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00004461 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00004462 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004463#endif
4464#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00004465 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00004466 /* A straight passthrough may not be safe because qemu sometimes
4467 turns private flie-backed mappings into anonymous mappings.
4468 This will break MADV_DONTNEED.
4469 This is a hint, so ignoring and returning success is ok. */
4470 ret = get_errno(0);
4471 break;
bellardffa65c32004-01-04 23:57:22 +00004472#endif
bellard60cd49d2003-03-16 22:53:56 +00004473#if TARGET_LONG_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00004474 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00004475 {
thsb1e341e2007-03-20 21:50:52 +00004476 int cmd;
bellard77e46722003-04-29 20:39:06 +00004477 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00004478 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00004479#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00004480 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00004481#endif
bellard77e46722003-04-29 20:39:06 +00004482
thsb1e341e2007-03-20 21:50:52 +00004483 switch(arg2){
4484 case TARGET_F_GETLK64:
4485 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00004486 break;
thsb1e341e2007-03-20 21:50:52 +00004487 case TARGET_F_SETLK64:
4488 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004489 break;
thsb1e341e2007-03-20 21:50:52 +00004490 case TARGET_F_SETLKW64:
4491 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004492 break;
thsb1e341e2007-03-20 21:50:52 +00004493 default:
4494 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00004495 break;
thsb1e341e2007-03-20 21:50:52 +00004496 }
4497
bellard60cd49d2003-03-16 22:53:56 +00004498 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00004499 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00004500#ifdef TARGET_ARM
4501 if (((CPUARMState *)cpu_env)->eabi) {
4502 lock_user_struct(target_efl, arg3, 1);
4503 fl.l_type = tswap16(target_efl->l_type);
4504 fl.l_whence = tswap16(target_efl->l_whence);
4505 fl.l_start = tswap64(target_efl->l_start);
4506 fl.l_len = tswap64(target_efl->l_len);
4507 fl.l_pid = tswapl(target_efl->l_pid);
4508 unlock_user_struct(target_efl, arg3, 0);
4509 } else
4510#endif
4511 {
4512 lock_user_struct(target_fl, arg3, 1);
4513 fl.l_type = tswap16(target_fl->l_type);
4514 fl.l_whence = tswap16(target_fl->l_whence);
4515 fl.l_start = tswap64(target_fl->l_start);
4516 fl.l_len = tswap64(target_fl->l_len);
4517 fl.l_pid = tswapl(target_fl->l_pid);
4518 unlock_user_struct(target_fl, arg3, 0);
4519 }
thsb1e341e2007-03-20 21:50:52 +00004520 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004521 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00004522#ifdef TARGET_ARM
4523 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004524 lock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004525 target_efl->l_type = tswap16(fl.l_type);
4526 target_efl->l_whence = tswap16(fl.l_whence);
4527 target_efl->l_start = tswap64(fl.l_start);
4528 target_efl->l_len = tswap64(fl.l_len);
4529 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004530 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004531 } else
4532#endif
4533 {
pbrook53a59602006-03-25 19:31:22 +00004534 lock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004535 target_fl->l_type = tswap16(fl.l_type);
4536 target_fl->l_whence = tswap16(fl.l_whence);
4537 target_fl->l_start = tswap64(fl.l_start);
4538 target_fl->l_len = tswap64(fl.l_len);
4539 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004540 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004541 }
bellard77e46722003-04-29 20:39:06 +00004542 }
4543 break;
4544
thsb1e341e2007-03-20 21:50:52 +00004545 case TARGET_F_SETLK64:
4546 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00004547#ifdef TARGET_ARM
4548 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004549 lock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004550 fl.l_type = tswap16(target_efl->l_type);
4551 fl.l_whence = tswap16(target_efl->l_whence);
4552 fl.l_start = tswap64(target_efl->l_start);
4553 fl.l_len = tswap64(target_efl->l_len);
4554 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004555 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004556 } else
4557#endif
4558 {
pbrook53a59602006-03-25 19:31:22 +00004559 lock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004560 fl.l_type = tswap16(target_fl->l_type);
4561 fl.l_whence = tswap16(target_fl->l_whence);
4562 fl.l_start = tswap64(target_fl->l_start);
4563 fl.l_len = tswap64(target_fl->l_len);
4564 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004565 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004566 }
thsb1e341e2007-03-20 21:50:52 +00004567 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004568 break;
bellard60cd49d2003-03-16 22:53:56 +00004569 default:
thsb1e341e2007-03-20 21:50:52 +00004570 ret = get_errno(do_fcntl(arg1, cmd, arg3));
bellard60cd49d2003-03-16 22:53:56 +00004571 break;
4572 }
bellard77e46722003-04-29 20:39:06 +00004573 break;
4574 }
bellard60cd49d2003-03-16 22:53:56 +00004575#endif
ths7d600c82006-12-08 01:32:58 +00004576#ifdef TARGET_NR_cacheflush
4577 case TARGET_NR_cacheflush:
4578 /* self-modifying code is handled automatically, so nothing needed */
4579 ret = 0;
4580 break;
4581#endif
bellardebc05482003-09-30 21:08:41 +00004582#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00004583 case TARGET_NR_security:
4584 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004585#endif
bellardc573ff62004-01-04 15:51:36 +00004586#ifdef TARGET_NR_getpagesize
4587 case TARGET_NR_getpagesize:
4588 ret = TARGET_PAGE_SIZE;
4589 break;
4590#endif
bellard31e31b82003-02-18 22:55:36 +00004591 case TARGET_NR_gettid:
4592 ret = get_errno(gettid());
4593 break;
thse5febef2007-04-01 18:31:35 +00004594#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00004595 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00004596 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004597#endif
bellardebc05482003-09-30 21:08:41 +00004598#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00004599 case TARGET_NR_setxattr:
4600 case TARGET_NR_lsetxattr:
4601 case TARGET_NR_fsetxattr:
4602 case TARGET_NR_getxattr:
4603 case TARGET_NR_lgetxattr:
4604 case TARGET_NR_fgetxattr:
4605 case TARGET_NR_listxattr:
4606 case TARGET_NR_llistxattr:
4607 case TARGET_NR_flistxattr:
4608 case TARGET_NR_removexattr:
4609 case TARGET_NR_lremovexattr:
4610 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00004611 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004612#endif
4613#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00004614 case TARGET_NR_set_thread_area:
ths6f5b89a2007-03-02 20:48:00 +00004615#ifdef TARGET_MIPS
4616 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
4617 ret = 0;
4618 break;
4619#else
4620 goto unimplemented_nowarn;
4621#endif
4622#endif
4623#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00004624 case TARGET_NR_get_thread_area:
4625 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004626#endif
bellard48dc41e2006-06-21 18:15:50 +00004627#ifdef TARGET_NR_getdomainname
4628 case TARGET_NR_getdomainname:
4629 goto unimplemented_nowarn;
4630#endif
ths6f5b89a2007-03-02 20:48:00 +00004631
thsb5906f92007-03-19 13:32:45 +00004632#ifdef TARGET_NR_clock_gettime
4633 case TARGET_NR_clock_gettime:
4634 {
4635 struct timespec ts;
4636 ret = get_errno(clock_gettime(arg1, &ts));
4637 if (!is_error(ret)) {
4638 host_to_target_timespec(arg2, &ts);
4639 }
4640 break;
4641 }
4642#endif
4643#ifdef TARGET_NR_clock_getres
4644 case TARGET_NR_clock_getres:
4645 {
4646 struct timespec ts;
4647 ret = get_errno(clock_getres(arg1, &ts));
4648 if (!is_error(ret)) {
4649 host_to_target_timespec(arg2, &ts);
4650 }
4651 break;
4652 }
4653#endif
4654
ths6f5b89a2007-03-02 20:48:00 +00004655#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
4656 case TARGET_NR_set_tid_address:
4657 ret = get_errno(set_tid_address((int *) arg1));
4658 break;
4659#endif
4660
ths3ae43202007-09-16 21:39:48 +00004661#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00004662 case TARGET_NR_tkill:
4663 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
4664 break;
4665#endif
4666
ths3ae43202007-09-16 21:39:48 +00004667#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00004668 case TARGET_NR_tgkill:
4669 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
4670 break;
4671#endif
4672
ths4f2b1fe2007-06-21 21:57:12 +00004673#ifdef TARGET_NR_set_robust_list
4674 case TARGET_NR_set_robust_list:
4675 goto unimplemented_nowarn;
4676#endif
4677
bellard31e31b82003-02-18 22:55:36 +00004678 default:
4679 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00004680 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00004681#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 +00004682 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00004683#endif
bellard31e31b82003-02-18 22:55:36 +00004684 ret = -ENOSYS;
4685 break;
4686 }
4687 fail:
bellardc573ff62004-01-04 15:51:36 +00004688#ifdef DEBUG
4689 gemu_log(" = %ld\n", ret);
4690#endif
bellard31e31b82003-02-18 22:55:36 +00004691 return ret;
4692}
4693