]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/syscall.c
always define TARGET_ABI32 if 32 bit user
[mirror_qemu.git] / linux-user / syscall.c
CommitLineData
31e31b8a
FB
1/*
2 * Linux syscalls
5fafdf24 3 *
31e31b8a
FB
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, 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>
04369ff2 23#include <string.h>
31e31b8a
FB
24#include <elf.h>
25#include <endian.h>
26#include <errno.h>
27#include <unistd.h>
28#include <fcntl.h>
7854b056 29#include <time.h>
31e31b8a 30#include <sys/types.h>
d08d3bb8
TS
31#include <sys/ipc.h>
32#include <sys/msg.h>
31e31b8a
FB
33#include <sys/wait.h>
34#include <sys/time.h>
35#include <sys/stat.h>
36#include <sys/mount.h>
39b9aae1 37#include <sys/prctl.h>
31e31b8a
FB
38#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>
9de5e440 45#include <sys/poll.h>
32f36bce 46#include <sys/times.h>
8853f86e 47#include <sys/shm.h>
fa294816 48#include <sys/sem.h>
56c8f68f 49#include <sys/statfs.h>
ebc05488 50#include <utime.h>
a5448a7d 51#include <sys/sysinfo.h>
72f03900 52//#include <sys/user.h>
8853f86e 53#include <netinet/ip.h>
7854b056 54#include <netinet/tcp.h>
31e31b8a
FB
55
56#define termios host_termios
57#define winsize host_winsize
58#define termio host_termio
04369ff2
FB
59#define sgttyb host_sgttyb /* same as target */
60#define tchars host_tchars /* same as target */
61#define ltchars host_ltchars /* same as target */
31e31b8a
FB
62
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>
dab2ed99 69#include <linux/dirent.h>
19b84f3c 70#include <linux/kd.h>
31e31b8a 71
3ef693a0 72#include "qemu.h"
31e31b8a 73
72f03900 74//#define DEBUG
31e31b8a 75
e6e5906b 76#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
48733d19 77 || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
67867308
FB
78/* 16 bit uid wrappers emulation */
79#define USE_UID16
80#endif
81
1a9353d2
FB
82//#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
70a194b9 86
70a194b9
FB
87#undef _syscall0
88#undef _syscall1
89#undef _syscall2
90#undef _syscall3
91#undef _syscall4
92#undef _syscall5
83fcb515 93#undef _syscall6
70a194b9 94
83fcb515
FB
95#define _syscall0(type,name) \
96type name (void) \
97{ \
98 return syscall(__NR_##name); \
99}
70a194b9 100
83fcb515
FB
101#define _syscall1(type,name,type1,arg1) \
102type name (type1 arg1) \
103{ \
104 return syscall(__NR_##name, arg1); \
70a194b9
FB
105}
106
83fcb515
FB
107#define _syscall2(type,name,type1,arg1,type2,arg2) \
108type name (type1 arg1,type2 arg2) \
109{ \
110 return syscall(__NR_##name, arg1, arg2); \
70a194b9
FB
111}
112
83fcb515
FB
113#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); \
70a194b9
FB
117}
118
83fcb515
FB
119#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); \
70a194b9
FB
123}
124
83fcb515
FB
125#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); \
70a194b9
FB
130}
131
83fcb515
FB
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); \
70a194b9 138}
83fcb515 139
70a194b9 140
31e31b8a 141#define __NR_sys_uname __NR_uname
92a34c10 142#define __NR_sys_faccessat __NR_faccessat
814d7977 143#define __NR_sys_fchmodat __NR_fchmodat
ccfa72b7 144#define __NR_sys_fchownat __NR_fchownat
72f03900 145#define __NR_sys_getcwd1 __NR_getcwd
72f03900 146#define __NR_sys_getdents __NR_getdents
dab2ed99 147#define __NR_sys_getdents64 __NR_getdents64
c6cda17a 148#define __NR_sys_getpriority __NR_getpriority
64f0ce4c 149#define __NR_sys_linkat __NR_linkat
4472ad0d 150#define __NR_sys_mkdirat __NR_mkdirat
75ac37a0 151#define __NR_sys_mknodat __NR_mknodat
82424832 152#define __NR_sys_openat __NR_openat
5e0ccb18 153#define __NR_sys_readlinkat __NR_readlinkat
722183f6 154#define __NR_sys_renameat __NR_renameat
66fb9763 155#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
f0b6243d 156#define __NR_sys_symlinkat __NR_symlinkat
7494b0f9 157#define __NR_sys_syslog __NR_syslog
71455574 158#define __NR_sys_tgkill __NR_tgkill
4cae1d16 159#define __NR_sys_tkill __NR_tkill
8170f56b 160#define __NR_sys_unlinkat __NR_unlinkat
9007f0ef 161#define __NR_sys_utimensat __NR_utimensat
31e31b8a 162
bc51c5c9 163#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
9af9eaaa
FB
164#define __NR__llseek __NR_lseek
165#endif
166
72f03900 167#ifdef __NR_gettid
31e31b8a 168_syscall0(int, gettid)
72f03900 169#else
0da46a6e
TS
170/* This is a replacement for the host gettid() and must return a host
171 errno. */
72f03900
FB
172static int gettid(void) {
173 return -ENOSYS;
174}
175#endif
31e31b8a 176_syscall1(int,sys_uname,struct new_utsname *,buf)
92a34c10
TS
177#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
178_syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
179#endif
814d7977
TS
180#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
181_syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
182 mode_t,mode,int,flags)
183#endif
ccfa72b7
TS
184#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
185_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
186 uid_t,owner,gid_t,group,int,flags)
187#endif
72f03900
FB
188_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
189_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
3ae43202 190#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
dab2ed99 191_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
3ae43202 192#endif
c6cda17a 193_syscall2(int, sys_getpriority, int, which, int, who);
31e31b8a
FB
194_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
195 loff_t *, res, uint, wh);
64f0ce4c
TS
196#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
197_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
198 int,newdirfd,const char *,newpath,int,flags)
199#endif
4472ad0d
TS
200#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
201_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
202#endif
75ac37a0
TS
203#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
204_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
205 mode_t,mode,dev_t,dev)
206#endif
82424832
TS
207#if defined(TARGET_NR_openat) && defined(__NR_openat)
208_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
209#endif
5e0ccb18
TS
210#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
211_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
212 char *,buf,size_t,bufsize)
213#endif
722183f6
TS
214#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
215_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
216 int,newdirfd,const char *,newpath)
217#endif
66fb9763 218_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
b51eaa82 219#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
f0b6243d
TS
220_syscall3(int,sys_symlinkat,const char *,oldpath,
221 int,newdirfd,const char *,newpath)
222#endif
7494b0f9 223_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
3ae43202 224#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
71455574 225_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
4cae1d16 226#endif
3ae43202 227#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
4cae1d16
TS
228_syscall2(int,sys_tkill,int,tid,int,sig)
229#endif
ec86b0fb
FB
230#ifdef __NR_exit_group
231_syscall1(int,exit_group,int,error_code)
232#endif
6f5b89a0
TS
233#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234_syscall1(int,set_tid_address,int *,tidptr)
235#endif
8170f56b
TS
236#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
237_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
238#endif
9007f0ef
TS
239#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
240_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
241 const struct timespec *,tsp,int,flags)
242#endif
66fb9763
FB
243
244extern int personality(int);
9de5e440
FB
245extern int flock(int, int);
246extern int setfsuid(int);
247extern int setfsgid(int);
5cd4393b
FB
248extern int setresuid(uid_t, uid_t, uid_t);
249extern int getresuid(uid_t *, uid_t *, uid_t *);
250extern int setresgid(gid_t, gid_t, gid_t);
251extern int getresgid(gid_t *, gid_t *, gid_t *);
19b84f3c 252extern int setgroups(int, gid_t *);
31e31b8a 253
b92c47c1
TS
254#define ERRNO_TABLE_SIZE 1200
255
256/* target_to_host_errno_table[] is initialized from
257 * host_to_target_errno_table[] in syscall_init(). */
258static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
259};
260
637947f1 261/*
fe8f096b 262 * This list is the union of errno values overridden in asm-<arch>/errno.h
637947f1
TS
263 * minus the errnos that are not actually generic to all archs.
264 */
b92c47c1 265static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
637947f1
TS
266 [EIDRM] = TARGET_EIDRM,
267 [ECHRNG] = TARGET_ECHRNG,
268 [EL2NSYNC] = TARGET_EL2NSYNC,
269 [EL3HLT] = TARGET_EL3HLT,
270 [EL3RST] = TARGET_EL3RST,
271 [ELNRNG] = TARGET_ELNRNG,
272 [EUNATCH] = TARGET_EUNATCH,
273 [ENOCSI] = TARGET_ENOCSI,
274 [EL2HLT] = TARGET_EL2HLT,
275 [EDEADLK] = TARGET_EDEADLK,
276 [ENOLCK] = TARGET_ENOLCK,
277 [EBADE] = TARGET_EBADE,
278 [EBADR] = TARGET_EBADR,
279 [EXFULL] = TARGET_EXFULL,
280 [ENOANO] = TARGET_ENOANO,
281 [EBADRQC] = TARGET_EBADRQC,
282 [EBADSLT] = TARGET_EBADSLT,
283 [EBFONT] = TARGET_EBFONT,
284 [ENOSTR] = TARGET_ENOSTR,
285 [ENODATA] = TARGET_ENODATA,
286 [ETIME] = TARGET_ETIME,
287 [ENOSR] = TARGET_ENOSR,
288 [ENONET] = TARGET_ENONET,
289 [ENOPKG] = TARGET_ENOPKG,
290 [EREMOTE] = TARGET_EREMOTE,
291 [ENOLINK] = TARGET_ENOLINK,
292 [EADV] = TARGET_EADV,
293 [ESRMNT] = TARGET_ESRMNT,
294 [ECOMM] = TARGET_ECOMM,
295 [EPROTO] = TARGET_EPROTO,
296 [EDOTDOT] = TARGET_EDOTDOT,
297 [EMULTIHOP] = TARGET_EMULTIHOP,
298 [EBADMSG] = TARGET_EBADMSG,
299 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
300 [EOVERFLOW] = TARGET_EOVERFLOW,
301 [ENOTUNIQ] = TARGET_ENOTUNIQ,
302 [EBADFD] = TARGET_EBADFD,
303 [EREMCHG] = TARGET_EREMCHG,
304 [ELIBACC] = TARGET_ELIBACC,
305 [ELIBBAD] = TARGET_ELIBBAD,
306 [ELIBSCN] = TARGET_ELIBSCN,
307 [ELIBMAX] = TARGET_ELIBMAX,
308 [ELIBEXEC] = TARGET_ELIBEXEC,
309 [EILSEQ] = TARGET_EILSEQ,
310 [ENOSYS] = TARGET_ENOSYS,
311 [ELOOP] = TARGET_ELOOP,
312 [ERESTART] = TARGET_ERESTART,
313 [ESTRPIPE] = TARGET_ESTRPIPE,
314 [ENOTEMPTY] = TARGET_ENOTEMPTY,
315 [EUSERS] = TARGET_EUSERS,
316 [ENOTSOCK] = TARGET_ENOTSOCK,
317 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
318 [EMSGSIZE] = TARGET_EMSGSIZE,
319 [EPROTOTYPE] = TARGET_EPROTOTYPE,
320 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
321 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
322 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
323 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
324 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
325 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
326 [EADDRINUSE] = TARGET_EADDRINUSE,
327 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
328 [ENETDOWN] = TARGET_ENETDOWN,
329 [ENETUNREACH] = TARGET_ENETUNREACH,
330 [ENETRESET] = TARGET_ENETRESET,
331 [ECONNABORTED] = TARGET_ECONNABORTED,
332 [ECONNRESET] = TARGET_ECONNRESET,
333 [ENOBUFS] = TARGET_ENOBUFS,
334 [EISCONN] = TARGET_EISCONN,
335 [ENOTCONN] = TARGET_ENOTCONN,
336 [EUCLEAN] = TARGET_EUCLEAN,
337 [ENOTNAM] = TARGET_ENOTNAM,
338 [ENAVAIL] = TARGET_ENAVAIL,
339 [EISNAM] = TARGET_EISNAM,
340 [EREMOTEIO] = TARGET_EREMOTEIO,
341 [ESHUTDOWN] = TARGET_ESHUTDOWN,
342 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
343 [ETIMEDOUT] = TARGET_ETIMEDOUT,
344 [ECONNREFUSED] = TARGET_ECONNREFUSED,
345 [EHOSTDOWN] = TARGET_EHOSTDOWN,
346 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
347 [EALREADY] = TARGET_EALREADY,
348 [EINPROGRESS] = TARGET_EINPROGRESS,
349 [ESTALE] = TARGET_ESTALE,
350 [ECANCELED] = TARGET_ECANCELED,
351 [ENOMEDIUM] = TARGET_ENOMEDIUM,
352 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
b7fe5db7 353#ifdef ENOKEY
637947f1 354 [ENOKEY] = TARGET_ENOKEY,
b7fe5db7
TS
355#endif
356#ifdef EKEYEXPIRED
637947f1 357 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
b7fe5db7
TS
358#endif
359#ifdef EKEYREVOKED
637947f1 360 [EKEYREVOKED] = TARGET_EKEYREVOKED,
b7fe5db7
TS
361#endif
362#ifdef EKEYREJECTED
637947f1 363 [EKEYREJECTED] = TARGET_EKEYREJECTED,
b7fe5db7
TS
364#endif
365#ifdef EOWNERDEAD
637947f1 366 [EOWNERDEAD] = TARGET_EOWNERDEAD,
b7fe5db7
TS
367#endif
368#ifdef ENOTRECOVERABLE
637947f1 369 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
b7fe5db7 370#endif
b92c47c1 371};
637947f1
TS
372
373static inline int host_to_target_errno(int err)
374{
375 if(host_to_target_errno_table[err])
376 return host_to_target_errno_table[err];
377 return err;
378}
379
b92c47c1
TS
380static inline int target_to_host_errno(int err)
381{
382 if (target_to_host_errno_table[err])
383 return target_to_host_errno_table[err];
384 return err;
385}
386
992f48a0 387static inline abi_long get_errno(abi_long ret)
31e31b8a
FB
388{
389 if (ret == -1)
637947f1 390 return -host_to_target_errno(errno);
31e31b8a
FB
391 else
392 return ret;
393}
394
992f48a0 395static inline int is_error(abi_long ret)
31e31b8a 396{
992f48a0 397 return (abi_ulong)ret >= (abi_ulong)(-4096);
31e31b8a
FB
398}
399
b92c47c1
TS
400char *target_strerror(int err)
401{
402 return strerror(target_to_host_errno(err));
403}
404
992f48a0
BS
405static abi_ulong target_brk;
406static abi_ulong target_original_brk;
31e31b8a 407
992f48a0 408void target_set_brk(abi_ulong new_brk)
31e31b8a 409{
4c1de73d 410 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
31e31b8a
FB
411}
412
0da46a6e 413/* do_brk() must return target values and target errnos. */
992f48a0 414abi_long do_brk(abi_ulong new_brk)
31e31b8a 415{
992f48a0
BS
416 abi_ulong brk_page;
417 abi_long mapped_addr;
31e31b8a
FB
418 int new_alloc_size;
419
420 if (!new_brk)
53a5960a 421 return target_brk;
31e31b8a 422 if (new_brk < target_original_brk)
0da46a6e 423 return -TARGET_ENOMEM;
3b46e624 424
53a5960a 425 brk_page = HOST_PAGE_ALIGN(target_brk);
31e31b8a
FB
426
427 /* If the new brk is less than this, set it and we're done... */
428 if (new_brk < brk_page) {
429 target_brk = new_brk;
53a5960a 430 return target_brk;
31e31b8a
FB
431 }
432
433 /* We need to allocate more memory after the brk... */
54936004 434 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
5fafdf24 435 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
54936004
FB
436 PROT_READ|PROT_WRITE,
437 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
31e31b8a
FB
438 if (is_error(mapped_addr)) {
439 return mapped_addr;
440 } else {
441 target_brk = new_brk;
53a5960a 442 return target_brk;
31e31b8a
FB
443 }
444}
445
5fafdf24 446static inline fd_set *target_to_host_fds(fd_set *fds,
992f48a0 447 abi_long *target_fds, int n)
31e31b8a 448{
7854b056 449#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
31e31b8a
FB
450 return (fd_set *)target_fds;
451#else
452 int i, b;
453 if (target_fds) {
454 FD_ZERO(fds);
455 for(i = 0;i < n; i++) {
992f48a0
BS
456 b = (tswapl(target_fds[i / TARGET_ABI_BITS]) >>
457 (i & (TARGET_ABI_BITS - 1))) & 1;
31e31b8a
FB
458 if (b)
459 FD_SET(i, fds);
460 }
461 return fds;
462 } else {
463 return NULL;
464 }
465#endif
466}
467
992f48a0 468static inline void host_to_target_fds(abi_long *target_fds,
31e31b8a
FB
469 fd_set *fds, int n)
470{
7854b056 471#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
31e31b8a
FB
472 /* nothing to do */
473#else
474 int i, nw, j, k;
992f48a0 475 abi_long v;
31e31b8a
FB
476
477 if (target_fds) {
992f48a0 478 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
31e31b8a
FB
479 k = 0;
480 for(i = 0;i < nw; i++) {
481 v = 0;
992f48a0 482 for(j = 0; j < TARGET_ABI_BITS; j++) {
31e31b8a
FB
483 v |= ((FD_ISSET(k, fds) != 0) << j);
484 k++;
485 }
486 target_fds[i] = tswapl(v);
487 }
488 }
489#endif
490}
491
c596ed17
FB
492#if defined(__alpha__)
493#define HOST_HZ 1024
494#else
495#define HOST_HZ 100
496#endif
497
992f48a0 498static inline abi_long host_to_target_clock_t(long ticks)
c596ed17
FB
499{
500#if HOST_HZ == TARGET_HZ
501 return ticks;
502#else
503 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
504#endif
505}
506
579a97f7
FB
507static inline abi_long host_to_target_rusage(abi_ulong target_addr,
508 const struct rusage *rusage)
b409186b 509{
53a5960a
PB
510 struct target_rusage *target_rusage;
511
579a97f7
FB
512 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
513 return -TARGET_EFAULT;
b409186b
FB
514 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
515 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
516 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
517 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
518 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
519 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
520 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
521 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
522 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
523 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
524 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
525 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
526 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
527 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
528 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
529 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
530 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
531 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
53a5960a 532 unlock_user_struct(target_rusage, target_addr, 1);
579a97f7
FB
533
534 return 0;
b409186b
FB
535}
536
579a97f7
FB
537static inline abi_long target_to_host_timeval(struct timeval *tv,
538 abi_ulong target_addr)
31e31b8a 539{
53a5960a
PB
540 struct target_timeval *target_tv;
541
579a97f7
FB
542 if (!lock_user_struct(VERIFY_READ, target_tv, target_addr, 1))
543 return -TARGET_EFAULT;
66fb9763
FB
544 tv->tv_sec = tswapl(target_tv->tv_sec);
545 tv->tv_usec = tswapl(target_tv->tv_usec);
53a5960a 546 unlock_user_struct(target_tv, target_addr, 0);
579a97f7
FB
547
548 return 0;
31e31b8a
FB
549}
550
579a97f7
FB
551static inline abi_long host_to_target_timeval(abi_ulong target_addr,
552 const struct timeval *tv)
31e31b8a 553{
53a5960a
PB
554 struct target_timeval *target_tv;
555
579a97f7
FB
556 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_addr, 0))
557 return -TARGET_EFAULT;
66fb9763
FB
558 target_tv->tv_sec = tswapl(tv->tv_sec);
559 target_tv->tv_usec = tswapl(tv->tv_usec);
53a5960a 560 unlock_user_struct(target_tv, target_addr, 1);
579a97f7
FB
561
562 return 0;
31e31b8a
FB
563}
564
565
0da46a6e 566/* do_select() must return target values and target errnos. */
992f48a0
BS
567static abi_long do_select(int n,
568 abi_ulong rfd_p, abi_ulong wfd_p,
569 abi_ulong efd_p, abi_ulong target_tv)
31e31b8a
FB
570{
571 fd_set rfds, wfds, efds;
572 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
992f48a0 573 abi_long *target_rfds, *target_wfds, *target_efds;
31e31b8a 574 struct timeval tv, *tv_ptr;
992f48a0 575 abi_long ret;
53a5960a 576 int ok;
31e31b8a 577
53a5960a 578 if (rfd_p) {
579a97f7
FB
579 target_rfds = lock_user(VERIFY_WRITE, rfd_p, sizeof(abi_long) * n, 1);
580 if (!target_rfds) {
581 ret = -TARGET_EFAULT;
582 goto end;
583 }
53a5960a
PB
584 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
585 } else {
586 target_rfds = NULL;
587 rfds_ptr = NULL;
588 }
589 if (wfd_p) {
579a97f7
FB
590 target_wfds = lock_user(VERIFY_WRITE, wfd_p, sizeof(abi_long) * n, 1);
591 if (!target_wfds) {
592 ret = -TARGET_EFAULT;
593 goto end;
594 }
53a5960a
PB
595 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
596 } else {
597 target_wfds = NULL;
598 wfds_ptr = NULL;
599 }
600 if (efd_p) {
579a97f7
FB
601 target_efds = lock_user(VERIFY_WRITE, efd_p, sizeof(abi_long) * n, 1);
602 if (!target_efds) {
603 ret = -TARGET_EFAULT;
604 goto end;
605 }
53a5960a
PB
606 efds_ptr = target_to_host_fds(&efds, target_efds, n);
607 } else {
608 target_efds = NULL;
609 efds_ptr = NULL;
610 }
3b46e624 611
31e31b8a 612 if (target_tv) {
5cd4393b 613 target_to_host_timeval(&tv, target_tv);
31e31b8a
FB
614 tv_ptr = &tv;
615 } else {
616 tv_ptr = NULL;
617 }
618 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
53a5960a
PB
619 ok = !is_error(ret);
620
621 if (ok) {
31e31b8a
FB
622 host_to_target_fds(target_rfds, rfds_ptr, n);
623 host_to_target_fds(target_wfds, wfds_ptr, n);
624 host_to_target_fds(target_efds, efds_ptr, n);
625
626 if (target_tv) {
5cd4393b 627 host_to_target_timeval(target_tv, &tv);
31e31b8a
FB
628 }
629 }
579a97f7
FB
630
631end:
632 unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0);
633 unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0);
634 unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0);
53a5960a 635
31e31b8a
FB
636 return ret;
637}
638
579a97f7
FB
639static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
640 abi_ulong target_addr,
641 socklen_t len)
7854b056 642{
53a5960a
PB
643 struct target_sockaddr *target_saddr;
644
579a97f7
FB
645 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
646 if (!target_saddr)
647 return -TARGET_EFAULT;
53a5960a
PB
648 memcpy(addr, target_saddr, len);
649 addr->sa_family = tswap16(target_saddr->sa_family);
650 unlock_user(target_saddr, target_addr, 0);
579a97f7
FB
651
652 return 0;
7854b056
FB
653}
654
579a97f7
FB
655static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
656 struct sockaddr *addr,
657 socklen_t len)
7854b056 658{
53a5960a
PB
659 struct target_sockaddr *target_saddr;
660
579a97f7
FB
661 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
662 if (!target_saddr)
663 return -TARGET_EFAULT;
53a5960a
PB
664 memcpy(target_saddr, addr, len);
665 target_saddr->sa_family = tswap16(addr->sa_family);
666 unlock_user(target_saddr, target_addr, len);
579a97f7
FB
667
668 return 0;
7854b056
FB
669}
670
53a5960a 671/* ??? Should this also swap msgh->name? */
5a4a898d
FB
672static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
673 struct target_msghdr *target_msgh)
7854b056
FB
674{
675 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
676 abi_long msg_controllen;
677 abi_ulong target_cmsg_addr;
678 struct target_cmsghdr *target_cmsg;
7854b056 679 socklen_t space = 0;
5a4a898d
FB
680
681 msg_controllen = tswapl(target_msgh->msg_controllen);
682 if (msg_controllen < sizeof (struct target_cmsghdr))
683 goto the_end;
684 target_cmsg_addr = tswapl(target_msgh->msg_control);
685 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
686 if (!target_cmsg)
687 return -TARGET_EFAULT;
7854b056
FB
688
689 while (cmsg && target_cmsg) {
690 void *data = CMSG_DATA(cmsg);
691 void *target_data = TARGET_CMSG_DATA(target_cmsg);
692
5fafdf24 693 int len = tswapl(target_cmsg->cmsg_len)
7854b056
FB
694 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
695
696 space += CMSG_SPACE(len);
697 if (space > msgh->msg_controllen) {
698 space -= CMSG_SPACE(len);
31febb71 699 gemu_log("Host cmsg overflow\n");
7854b056
FB
700 break;
701 }
702
703 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
704 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
705 cmsg->cmsg_len = CMSG_LEN(len);
706
3532fa74 707 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
7854b056
FB
708 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
709 memcpy(data, target_data, len);
710 } else {
711 int *fd = (int *)data;
712 int *target_fd = (int *)target_data;
713 int i, numfds = len / sizeof(int);
714
715 for (i = 0; i < numfds; i++)
716 fd[i] = tswap32(target_fd[i]);
717 }
718
719 cmsg = CMSG_NXTHDR(msgh, cmsg);
720 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
721 }
5a4a898d
FB
722 unlock_user(target_cmsg, target_cmsg_addr, 0);
723 the_end:
7854b056 724 msgh->msg_controllen = space;
5a4a898d 725 return 0;
7854b056
FB
726}
727
53a5960a 728/* ??? Should this also swap msgh->name? */
5a4a898d
FB
729static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
730 struct msghdr *msgh)
7854b056
FB
731{
732 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
733 abi_long msg_controllen;
734 abi_ulong target_cmsg_addr;
735 struct target_cmsghdr *target_cmsg;
7854b056
FB
736 socklen_t space = 0;
737
5a4a898d
FB
738 msg_controllen = tswapl(target_msgh->msg_controllen);
739 if (msg_controllen < sizeof (struct target_cmsghdr))
740 goto the_end;
741 target_cmsg_addr = tswapl(target_msgh->msg_control);
742 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
743 if (!target_cmsg)
744 return -TARGET_EFAULT;
745
7854b056
FB
746 while (cmsg && target_cmsg) {
747 void *data = CMSG_DATA(cmsg);
748 void *target_data = TARGET_CMSG_DATA(target_cmsg);
749
750 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
751
752 space += TARGET_CMSG_SPACE(len);
5a4a898d 753 if (space > msg_controllen) {
7854b056 754 space -= TARGET_CMSG_SPACE(len);
31febb71 755 gemu_log("Target cmsg overflow\n");
7854b056
FB
756 break;
757 }
758
759 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
760 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
761 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
762
3532fa74 763 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
7854b056
FB
764 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
765 memcpy(target_data, data, len);
766 } else {
767 int *fd = (int *)data;
768 int *target_fd = (int *)target_data;
769 int i, numfds = len / sizeof(int);
770
771 for (i = 0; i < numfds; i++)
772 target_fd[i] = tswap32(fd[i]);
773 }
774
775 cmsg = CMSG_NXTHDR(msgh, cmsg);
776 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
777 }
5a4a898d
FB
778 unlock_user(target_cmsg, target_cmsg_addr, space);
779 the_end:
780 target_msgh->msg_controllen = tswapl(space);
781 return 0;
7854b056
FB
782}
783
0da46a6e 784/* do_setsockopt() Must return target values and target errnos. */
992f48a0
BS
785static abi_long do_setsockopt(int sockfd, int level, int optname,
786 abi_ulong optval, socklen_t optlen)
7854b056 787{
992f48a0 788 abi_long ret;
32407103 789 int val;
3b46e624 790
8853f86e
FB
791 switch(level) {
792 case SOL_TCP:
7854b056 793 /* TCP options all take an 'int' value. */
7854b056 794 if (optlen < sizeof(uint32_t))
0da46a6e 795 return -TARGET_EINVAL;
3b46e624 796
53a5960a 797 val = tget32(optval);
8853f86e
FB
798 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
799 break;
800 case SOL_IP:
801 switch(optname) {
2efbe911
FB
802 case IP_TOS:
803 case IP_TTL:
8853f86e 804 case IP_HDRINCL:
2efbe911
FB
805 case IP_ROUTER_ALERT:
806 case IP_RECVOPTS:
807 case IP_RETOPTS:
808 case IP_PKTINFO:
809 case IP_MTU_DISCOVER:
810 case IP_RECVERR:
811 case IP_RECVTOS:
812#ifdef IP_FREEBIND
813 case IP_FREEBIND:
814#endif
815 case IP_MULTICAST_TTL:
816 case IP_MULTICAST_LOOP:
8853f86e
FB
817 val = 0;
818 if (optlen >= sizeof(uint32_t)) {
53a5960a 819 val = tget32(optval);
8853f86e 820 } else if (optlen >= 1) {
53a5960a 821 val = tget8(optval);
8853f86e
FB
822 }
823 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
824 break;
825 default:
826 goto unimplemented;
827 }
828 break;
3532fa74 829 case TARGET_SOL_SOCKET:
8853f86e
FB
830 switch (optname) {
831 /* Options with 'int' argument. */
3532fa74
FB
832 case TARGET_SO_DEBUG:
833 optname = SO_DEBUG;
834 break;
835 case TARGET_SO_REUSEADDR:
836 optname = SO_REUSEADDR;
837 break;
838 case TARGET_SO_TYPE:
839 optname = SO_TYPE;
840 break;
841 case TARGET_SO_ERROR:
842 optname = SO_ERROR;
843 break;
844 case TARGET_SO_DONTROUTE:
845 optname = SO_DONTROUTE;
846 break;
847 case TARGET_SO_BROADCAST:
848 optname = SO_BROADCAST;
849 break;
850 case TARGET_SO_SNDBUF:
851 optname = SO_SNDBUF;
852 break;
853 case TARGET_SO_RCVBUF:
854 optname = SO_RCVBUF;
855 break;
856 case TARGET_SO_KEEPALIVE:
857 optname = SO_KEEPALIVE;
858 break;
859 case TARGET_SO_OOBINLINE:
860 optname = SO_OOBINLINE;
861 break;
862 case TARGET_SO_NO_CHECK:
863 optname = SO_NO_CHECK;
864 break;
865 case TARGET_SO_PRIORITY:
866 optname = SO_PRIORITY;
867 break;
5e83e8e3 868#ifdef SO_BSDCOMPAT
3532fa74
FB
869 case TARGET_SO_BSDCOMPAT:
870 optname = SO_BSDCOMPAT;
871 break;
5e83e8e3 872#endif
3532fa74
FB
873 case TARGET_SO_PASSCRED:
874 optname = SO_PASSCRED;
875 break;
876 case TARGET_SO_TIMESTAMP:
877 optname = SO_TIMESTAMP;
878 break;
879 case TARGET_SO_RCVLOWAT:
880 optname = SO_RCVLOWAT;
881 break;
882 case TARGET_SO_RCVTIMEO:
883 optname = SO_RCVTIMEO;
884 break;
885 case TARGET_SO_SNDTIMEO:
886 optname = SO_SNDTIMEO;
887 break;
8853f86e
FB
888 break;
889 default:
890 goto unimplemented;
891 }
3532fa74 892 if (optlen < sizeof(uint32_t))
0da46a6e 893 return -TARGET_EINVAL;
3532fa74
FB
894
895 val = tget32(optval);
896 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
8853f86e 897 break;
7854b056 898 default:
8853f86e
FB
899 unimplemented:
900 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
0da46a6e 901 ret = -TARGET_ENOSYS;
7854b056 902 }
8853f86e 903 return ret;
7854b056
FB
904}
905
0da46a6e 906/* do_getsockopt() Must return target values and target errnos. */
992f48a0
BS
907static abi_long do_getsockopt(int sockfd, int level, int optname,
908 abi_ulong optval, abi_ulong optlen)
7854b056 909{
992f48a0 910 abi_long ret;
32407103 911 int len, lv, val;
8853f86e
FB
912
913 switch(level) {
3532fa74
FB
914 case TARGET_SOL_SOCKET:
915 level = SOL_SOCKET;
8853f86e 916 switch (optname) {
3532fa74
FB
917 case TARGET_SO_LINGER:
918 case TARGET_SO_RCVTIMEO:
919 case TARGET_SO_SNDTIMEO:
920 case TARGET_SO_PEERCRED:
921 case TARGET_SO_PEERNAME:
8853f86e
FB
922 /* These don't just return a single integer */
923 goto unimplemented;
924 default:
2efbe911
FB
925 goto int_case;
926 }
927 break;
928 case SOL_TCP:
929 /* TCP options all take an 'int' value. */
930 int_case:
53a5960a 931 len = tget32(optlen);
2efbe911 932 if (len < 0)
0da46a6e 933 return -TARGET_EINVAL;
2efbe911
FB
934 lv = sizeof(int);
935 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
936 if (ret < 0)
937 return ret;
938 val = tswap32(val);
939 if (len > lv)
940 len = lv;
53a5960a
PB
941 if (len == 4)
942 tput32(optval, val);
943 else
944 tput8(optval, val);
945 tput32(optlen, len);
2efbe911
FB
946 break;
947 case SOL_IP:
948 switch(optname) {
949 case IP_TOS:
950 case IP_TTL:
951 case IP_HDRINCL:
952 case IP_ROUTER_ALERT:
953 case IP_RECVOPTS:
954 case IP_RETOPTS:
955 case IP_PKTINFO:
956 case IP_MTU_DISCOVER:
957 case IP_RECVERR:
958 case IP_RECVTOS:
959#ifdef IP_FREEBIND
960 case IP_FREEBIND:
961#endif
962 case IP_MULTICAST_TTL:
963 case IP_MULTICAST_LOOP:
53a5960a 964 len = tget32(optlen);
8853f86e 965 if (len < 0)
0da46a6e 966 return -TARGET_EINVAL;
8853f86e
FB
967 lv = sizeof(int);
968 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
969 if (ret < 0)
970 return ret;
2efbe911 971 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2efbe911 972 len = 1;
53a5960a
PB
973 tput32(optlen, len);
974 tput8(optval, val);
2efbe911 975 } else {
2efbe911
FB
976 if (len > sizeof(int))
977 len = sizeof(int);
53a5960a
PB
978 tput32(optlen, len);
979 tput32(optval, val);
2efbe911 980 }
8853f86e 981 break;
2efbe911
FB
982 default:
983 goto unimplemented;
8853f86e
FB
984 }
985 break;
986 default:
987 unimplemented:
988 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
989 level, optname);
0da46a6e 990 ret = -TARGET_ENOSYS;
8853f86e
FB
991 break;
992 }
993 return ret;
7854b056
FB
994}
995
579a97f7
FB
996/* FIXME
997 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
998 * other lock functions have a return code of 0 for failure.
999 */
1000static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1001 int count, int copy)
53a5960a
PB
1002{
1003 struct target_iovec *target_vec;
992f48a0 1004 abi_ulong base;
579a97f7 1005 int i, j;
53a5960a 1006
579a97f7
FB
1007 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1008 if (!target_vec)
1009 return -TARGET_EFAULT;
53a5960a
PB
1010 for(i = 0;i < count; i++) {
1011 base = tswapl(target_vec[i].iov_base);
1012 vec[i].iov_len = tswapl(target_vec[i].iov_len);
579a97f7
FB
1013 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1014 if (!vec[i].iov_base)
1015 goto fail;
1016 }
1017 unlock_user (target_vec, target_addr, 0);
1018 return 0;
1019 fail:
1020 /* failure - unwind locks */
1021 for (j = 0; j < i; j++) {
1022 base = tswapl(target_vec[j].iov_base);
1023 unlock_user(vec[j].iov_base, base, 0);
53a5960a
PB
1024 }
1025 unlock_user (target_vec, target_addr, 0);
579a97f7 1026 return -TARGET_EFAULT;
53a5960a
PB
1027}
1028
579a97f7
FB
1029static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1030 int count, int copy)
53a5960a
PB
1031{
1032 struct target_iovec *target_vec;
992f48a0 1033 abi_ulong base;
53a5960a
PB
1034 int i;
1035
579a97f7
FB
1036 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1037 if (!target_vec)
1038 return -TARGET_EFAULT;
53a5960a
PB
1039 for(i = 0;i < count; i++) {
1040 base = tswapl(target_vec[i].iov_base);
1041 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1042 }
1043 unlock_user (target_vec, target_addr, 0);
579a97f7
FB
1044
1045 return 0;
53a5960a
PB
1046}
1047
0da46a6e 1048/* do_socket() Must return target values and target errnos. */
992f48a0 1049static abi_long do_socket(int domain, int type, int protocol)
3532fa74
FB
1050{
1051#if defined(TARGET_MIPS)
1052 switch(type) {
1053 case TARGET_SOCK_DGRAM:
1054 type = SOCK_DGRAM;
1055 break;
1056 case TARGET_SOCK_STREAM:
1057 type = SOCK_STREAM;
1058 break;
1059 case TARGET_SOCK_RAW:
1060 type = SOCK_RAW;
1061 break;
1062 case TARGET_SOCK_RDM:
1063 type = SOCK_RDM;
1064 break;
1065 case TARGET_SOCK_SEQPACKET:
1066 type = SOCK_SEQPACKET;
1067 break;
1068 case TARGET_SOCK_PACKET:
1069 type = SOCK_PACKET;
1070 break;
1071 }
1072#endif
12bc92ab
AZ
1073 if (domain == PF_NETLINK)
1074 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
3532fa74
FB
1075 return get_errno(socket(domain, type, protocol));
1076}
1077
0da46a6e 1078/* do_bind() Must return target values and target errnos. */
992f48a0
BS
1079static abi_long do_bind(int sockfd, abi_ulong target_addr,
1080 socklen_t addrlen)
3532fa74
FB
1081{
1082 void *addr = alloca(addrlen);
3b46e624 1083
3532fa74
FB
1084 target_to_host_sockaddr(addr, target_addr, addrlen);
1085 return get_errno(bind(sockfd, addr, addrlen));
1086}
1087
0da46a6e 1088/* do_connect() Must return target values and target errnos. */
992f48a0
BS
1089static abi_long do_connect(int sockfd, abi_ulong target_addr,
1090 socklen_t addrlen)
3532fa74
FB
1091{
1092 void *addr = alloca(addrlen);
3b46e624 1093
3532fa74
FB
1094 target_to_host_sockaddr(addr, target_addr, addrlen);
1095 return get_errno(connect(sockfd, addr, addrlen));
1096}
1097
0da46a6e 1098/* do_sendrecvmsg() Must return target values and target errnos. */
992f48a0
BS
1099static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1100 int flags, int send)
3532fa74 1101{
992f48a0 1102 abi_long ret;
3532fa74
FB
1103 struct target_msghdr *msgp;
1104 struct msghdr msg;
1105 int count;
1106 struct iovec *vec;
992f48a0 1107 abi_ulong target_vec;
3532fa74 1108
579a97f7
FB
1109 /* FIXME */
1110 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1111 msgp,
1112 target_msg,
1113 send ? 1 : 0))
1114 return -TARGET_EFAULT;
3532fa74
FB
1115 if (msgp->msg_name) {
1116 msg.msg_namelen = tswap32(msgp->msg_namelen);
1117 msg.msg_name = alloca(msg.msg_namelen);
1118 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1119 msg.msg_namelen);
1120 } else {
1121 msg.msg_name = NULL;
1122 msg.msg_namelen = 0;
1123 }
1124 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1125 msg.msg_control = alloca(msg.msg_controllen);
1126 msg.msg_flags = tswap32(msgp->msg_flags);
3b46e624 1127
3532fa74
FB
1128 count = tswapl(msgp->msg_iovlen);
1129 vec = alloca(count * sizeof(struct iovec));
1130 target_vec = tswapl(msgp->msg_iov);
579a97f7 1131 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
3532fa74
FB
1132 msg.msg_iovlen = count;
1133 msg.msg_iov = vec;
3b46e624 1134
3532fa74 1135 if (send) {
5a4a898d
FB
1136 ret = target_to_host_cmsg(&msg, msgp);
1137 if (ret == 0)
1138 ret = get_errno(sendmsg(fd, &msg, flags));
3532fa74
FB
1139 } else {
1140 ret = get_errno(recvmsg(fd, &msg, flags));
1141 if (!is_error(ret))
5a4a898d 1142 ret = host_to_target_cmsg(msgp, &msg);
3532fa74
FB
1143 }
1144 unlock_iovec(vec, target_vec, count, !send);
579a97f7 1145 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3532fa74
FB
1146 return ret;
1147}
1148
0da46a6e 1149/* do_accept() Must return target values and target errnos. */
992f48a0
BS
1150static abi_long do_accept(int fd, abi_ulong target_addr,
1151 abi_ulong target_addrlen)
1be9e1dc
PB
1152{
1153 socklen_t addrlen = tget32(target_addrlen);
197ea35a 1154 void *addr = alloca(addrlen);
992f48a0 1155 abi_long ret;
1be9e1dc
PB
1156
1157 ret = get_errno(accept(fd, addr, &addrlen));
1158 if (!is_error(ret)) {
1159 host_to_target_sockaddr(target_addr, addr, addrlen);
1160 tput32(target_addrlen, addrlen);
1161 }
1162 return ret;
1163}
1164
0da46a6e 1165/* do_getpeername() Must return target values and target errnos. */
992f48a0
BS
1166static abi_long do_getpeername(int fd, abi_ulong target_addr,
1167 abi_ulong target_addrlen)
1be9e1dc
PB
1168{
1169 socklen_t addrlen = tget32(target_addrlen);
01c227fb 1170 void *addr = alloca(addrlen);
992f48a0 1171 abi_long ret;
1be9e1dc
PB
1172
1173 ret = get_errno(getpeername(fd, addr, &addrlen));
1174 if (!is_error(ret)) {
1175 host_to_target_sockaddr(target_addr, addr, addrlen);
1176 tput32(target_addrlen, addrlen);
1177 }
1178 return ret;
1179}
1180
0da46a6e 1181/* do_getsockname() Must return target values and target errnos. */
992f48a0
BS
1182static abi_long do_getsockname(int fd, abi_ulong target_addr,
1183 abi_ulong target_addrlen)
1be9e1dc
PB
1184{
1185 socklen_t addrlen = tget32(target_addrlen);
01c227fb 1186 void *addr = alloca(addrlen);
992f48a0 1187 abi_long ret;
1be9e1dc
PB
1188
1189 ret = get_errno(getsockname(fd, addr, &addrlen));
1190 if (!is_error(ret)) {
1191 host_to_target_sockaddr(target_addr, addr, addrlen);
1192 tput32(target_addrlen, addrlen);
1193 }
1194 return ret;
1195}
1196
0da46a6e 1197/* do_socketpair() Must return target values and target errnos. */
992f48a0
BS
1198static abi_long do_socketpair(int domain, int type, int protocol,
1199 abi_ulong target_tab)
1be9e1dc
PB
1200{
1201 int tab[2];
992f48a0 1202 abi_long ret;
1be9e1dc
PB
1203
1204 ret = get_errno(socketpair(domain, type, protocol, tab));
1205 if (!is_error(ret)) {
1206 tput32(target_tab, tab[0]);
1207 tput32(target_tab + 4, tab[1]);
1208 }
1209 return ret;
1210}
1211
0da46a6e 1212/* do_sendto() Must return target values and target errnos. */
992f48a0
BS
1213static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1214 abi_ulong target_addr, socklen_t addrlen)
1be9e1dc
PB
1215{
1216 void *addr;
1217 void *host_msg;
992f48a0 1218 abi_long ret;
1be9e1dc 1219
579a97f7
FB
1220 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1221 if (!host_msg)
1222 return -TARGET_EFAULT;
1be9e1dc
PB
1223 if (target_addr) {
1224 addr = alloca(addrlen);
1225 target_to_host_sockaddr(addr, target_addr, addrlen);
1226 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1227 } else {
1228 ret = get_errno(send(fd, host_msg, len, flags));
1229 }
1230 unlock_user(host_msg, msg, 0);
1231 return ret;
1232}
1233
0da46a6e 1234/* do_recvfrom() Must return target values and target errnos. */
992f48a0
BS
1235static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1236 abi_ulong target_addr,
1237 abi_ulong target_addrlen)
1be9e1dc
PB
1238{
1239 socklen_t addrlen;
1240 void *addr;
1241 void *host_msg;
992f48a0 1242 abi_long ret;
1be9e1dc 1243
579a97f7
FB
1244 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1245 if (!host_msg)
1246 return -TARGET_EFAULT;
1be9e1dc
PB
1247 if (target_addr) {
1248 addrlen = tget32(target_addrlen);
1249 addr = alloca(addrlen);
1250 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1251 } else {
1252 addr = NULL; /* To keep compiler quiet. */
1253 ret = get_errno(recv(fd, host_msg, len, flags));
1254 }
1255 if (!is_error(ret)) {
1256 if (target_addr) {
1257 host_to_target_sockaddr(target_addr, addr, addrlen);
1258 tput32(target_addrlen, addrlen);
1259 }
1260 unlock_user(host_msg, msg, len);
1261 } else {
1262 unlock_user(host_msg, msg, 0);
1263 }
1264 return ret;
1265}
1266
32407103 1267#ifdef TARGET_NR_socketcall
0da46a6e 1268/* do_socketcall() Must return target values and target errnos. */
992f48a0 1269static abi_long do_socketcall(int num, abi_ulong vptr)
31e31b8a 1270{
992f48a0
BS
1271 abi_long ret;
1272 const int n = sizeof(abi_ulong);
31e31b8a
FB
1273
1274 switch(num) {
1275 case SOCKOP_socket:
7854b056 1276 {
53a5960a
PB
1277 int domain = tgetl(vptr);
1278 int type = tgetl(vptr + n);
1279 int protocol = tgetl(vptr + 2 * n);
3532fa74 1280 ret = do_socket(domain, type, protocol);
7854b056 1281 }
31e31b8a
FB
1282 break;
1283 case SOCKOP_bind:
7854b056 1284 {
53a5960a 1285 int sockfd = tgetl(vptr);
992f48a0 1286 abi_ulong target_addr = tgetl(vptr + n);
53a5960a 1287 socklen_t addrlen = tgetl(vptr + 2 * n);
3532fa74 1288 ret = do_bind(sockfd, target_addr, addrlen);
7854b056 1289 }
31e31b8a
FB
1290 break;
1291 case SOCKOP_connect:
7854b056 1292 {
53a5960a 1293 int sockfd = tgetl(vptr);
992f48a0 1294 abi_ulong target_addr = tgetl(vptr + n);
53a5960a 1295 socklen_t addrlen = tgetl(vptr + 2 * n);
3532fa74 1296 ret = do_connect(sockfd, target_addr, addrlen);
7854b056 1297 }
31e31b8a
FB
1298 break;
1299 case SOCKOP_listen:
7854b056 1300 {
53a5960a
PB
1301 int sockfd = tgetl(vptr);
1302 int backlog = tgetl(vptr + n);
7854b056
FB
1303 ret = get_errno(listen(sockfd, backlog));
1304 }
31e31b8a
FB
1305 break;
1306 case SOCKOP_accept:
1307 {
53a5960a 1308 int sockfd = tgetl(vptr);
992f48a0
BS
1309 abi_ulong target_addr = tgetl(vptr + n);
1310 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1be9e1dc 1311 ret = do_accept(sockfd, target_addr, target_addrlen);
31e31b8a
FB
1312 }
1313 break;
1314 case SOCKOP_getsockname:
1315 {
53a5960a 1316 int sockfd = tgetl(vptr);
992f48a0
BS
1317 abi_ulong target_addr = tgetl(vptr + n);
1318 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1be9e1dc 1319 ret = do_getsockname(sockfd, target_addr, target_addrlen);
31e31b8a
FB
1320 }
1321 break;
1322 case SOCKOP_getpeername:
1323 {
53a5960a 1324 int sockfd = tgetl(vptr);
992f48a0
BS
1325 abi_ulong target_addr = tgetl(vptr + n);
1326 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
1be9e1dc 1327 ret = do_getpeername(sockfd, target_addr, target_addrlen);
31e31b8a
FB
1328 }
1329 break;
1330 case SOCKOP_socketpair:
1331 {
53a5960a
PB
1332 int domain = tgetl(vptr);
1333 int type = tgetl(vptr + n);
1334 int protocol = tgetl(vptr + 2 * n);
992f48a0 1335 abi_ulong tab = tgetl(vptr + 3 * n);
1be9e1dc 1336 ret = do_socketpair(domain, type, protocol, tab);
31e31b8a
FB
1337 }
1338 break;
1339 case SOCKOP_send:
7854b056 1340 {
53a5960a 1341 int sockfd = tgetl(vptr);
992f48a0 1342 abi_ulong msg = tgetl(vptr + n);
53a5960a
PB
1343 size_t len = tgetl(vptr + 2 * n);
1344 int flags = tgetl(vptr + 3 * n);
1be9e1dc 1345 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
7854b056 1346 }
31e31b8a
FB
1347 break;
1348 case SOCKOP_recv:
7854b056 1349 {
53a5960a 1350 int sockfd = tgetl(vptr);
992f48a0 1351 abi_ulong msg = tgetl(vptr + n);
53a5960a
PB
1352 size_t len = tgetl(vptr + 2 * n);
1353 int flags = tgetl(vptr + 3 * n);
1be9e1dc 1354 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
7854b056 1355 }
31e31b8a
FB
1356 break;
1357 case SOCKOP_sendto:
7854b056 1358 {
53a5960a 1359 int sockfd = tgetl(vptr);
992f48a0 1360 abi_ulong msg = tgetl(vptr + n);
53a5960a
PB
1361 size_t len = tgetl(vptr + 2 * n);
1362 int flags = tgetl(vptr + 3 * n);
992f48a0 1363 abi_ulong addr = tgetl(vptr + 4 * n);
53a5960a 1364 socklen_t addrlen = tgetl(vptr + 5 * n);
1be9e1dc 1365 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
7854b056 1366 }
31e31b8a
FB
1367 break;
1368 case SOCKOP_recvfrom:
1369 {
53a5960a 1370 int sockfd = tgetl(vptr);
992f48a0 1371 abi_ulong msg = tgetl(vptr + n);
53a5960a
PB
1372 size_t len = tgetl(vptr + 2 * n);
1373 int flags = tgetl(vptr + 3 * n);
992f48a0
BS
1374 abi_ulong addr = tgetl(vptr + 4 * n);
1375 abi_ulong addrlen = tgetl(vptr + 5 * n);
1be9e1dc 1376 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
31e31b8a
FB
1377 }
1378 break;
1379 case SOCKOP_shutdown:
7854b056 1380 {
53a5960a
PB
1381 int sockfd = tgetl(vptr);
1382 int how = tgetl(vptr + n);
7854b056
FB
1383
1384 ret = get_errno(shutdown(sockfd, how));
1385 }
31e31b8a
FB
1386 break;
1387 case SOCKOP_sendmsg:
1388 case SOCKOP_recvmsg:
1a9353d2
FB
1389 {
1390 int fd;
992f48a0 1391 abi_ulong target_msg;
3532fa74 1392 int flags;
1a9353d2 1393
53a5960a 1394 fd = tgetl(vptr);
3532fa74 1395 target_msg = tgetl(vptr + n);
53a5960a 1396 flags = tgetl(vptr + 2 * n);
3532fa74 1397
5fafdf24 1398 ret = do_sendrecvmsg(fd, target_msg, flags,
3532fa74 1399 (num == SOCKOP_sendmsg));
1a9353d2
FB
1400 }
1401 break;
31e31b8a 1402 case SOCKOP_setsockopt:
7854b056 1403 {
53a5960a
PB
1404 int sockfd = tgetl(vptr);
1405 int level = tgetl(vptr + n);
1406 int optname = tgetl(vptr + 2 * n);
992f48a0 1407 abi_ulong optval = tgetl(vptr + 3 * n);
53a5960a 1408 socklen_t optlen = tgetl(vptr + 4 * n);
7854b056
FB
1409
1410 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1411 }
1412 break;
31e31b8a 1413 case SOCKOP_getsockopt:
7854b056 1414 {
53a5960a
PB
1415 int sockfd = tgetl(vptr);
1416 int level = tgetl(vptr + n);
1417 int optname = tgetl(vptr + 2 * n);
992f48a0
BS
1418 abi_ulong optval = tgetl(vptr + 3 * n);
1419 abi_ulong poptlen = tgetl(vptr + 4 * n);
7854b056 1420
8853f86e 1421 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
7854b056
FB
1422 }
1423 break;
31e31b8a
FB
1424 default:
1425 gemu_log("Unsupported socketcall: %d\n", num);
0da46a6e 1426 ret = -TARGET_ENOSYS;
31e31b8a
FB
1427 break;
1428 }
1429 return ret;
1430}
32407103 1431#endif
31e31b8a 1432
32407103 1433#ifdef TARGET_NR_ipc
8853f86e
FB
1434#define N_SHM_REGIONS 32
1435
1436static struct shm_region {
5a4a898d
FB
1437 abi_ulong start;
1438 abi_ulong size;
8853f86e
FB
1439} shm_regions[N_SHM_REGIONS];
1440
3eb6b044
TS
1441struct target_ipc_perm
1442{
992f48a0
BS
1443 abi_long __key;
1444 abi_ulong uid;
1445 abi_ulong gid;
1446 abi_ulong cuid;
1447 abi_ulong cgid;
3eb6b044
TS
1448 unsigned short int mode;
1449 unsigned short int __pad1;
1450 unsigned short int __seq;
1451 unsigned short int __pad2;
992f48a0
BS
1452 abi_ulong __unused1;
1453 abi_ulong __unused2;
3eb6b044
TS
1454};
1455
1456struct target_semid_ds
1457{
1458 struct target_ipc_perm sem_perm;
992f48a0
BS
1459 abi_ulong sem_otime;
1460 abi_ulong __unused1;
1461 abi_ulong sem_ctime;
1462 abi_ulong __unused2;
1463 abi_ulong sem_nsems;
1464 abi_ulong __unused3;
1465 abi_ulong __unused4;
3eb6b044
TS
1466};
1467
579a97f7
FB
1468static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1469 abi_ulong target_addr)
3eb6b044
TS
1470{
1471 struct target_ipc_perm *target_ip;
1472 struct target_semid_ds *target_sd;
1473
579a97f7
FB
1474 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1475 return -TARGET_EFAULT;
3eb6b044
TS
1476 target_ip=&(target_sd->sem_perm);
1477 host_ip->__key = tswapl(target_ip->__key);
1478 host_ip->uid = tswapl(target_ip->uid);
1479 host_ip->gid = tswapl(target_ip->gid);
1480 host_ip->cuid = tswapl(target_ip->cuid);
1481 host_ip->cgid = tswapl(target_ip->cgid);
1482 host_ip->mode = tswapl(target_ip->mode);
1483 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 1484 return 0;
3eb6b044
TS
1485}
1486
579a97f7
FB
1487static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1488 struct ipc_perm *host_ip)
3eb6b044
TS
1489{
1490 struct target_ipc_perm *target_ip;
1491 struct target_semid_ds *target_sd;
1492
579a97f7
FB
1493 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1494 return -TARGET_EFAULT;
3eb6b044
TS
1495 target_ip = &(target_sd->sem_perm);
1496 target_ip->__key = tswapl(host_ip->__key);
1497 target_ip->uid = tswapl(host_ip->uid);
1498 target_ip->gid = tswapl(host_ip->gid);
1499 target_ip->cuid = tswapl(host_ip->cuid);
1500 target_ip->cgid = tswapl(host_ip->cgid);
1501 target_ip->mode = tswapl(host_ip->mode);
1502 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 1503 return 0;
3eb6b044
TS
1504}
1505
579a97f7
FB
1506static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1507 abi_ulong target_addr)
3eb6b044
TS
1508{
1509 struct target_semid_ds *target_sd;
1510
579a97f7
FB
1511 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1512 return -TARGET_EFAULT;
3eb6b044
TS
1513 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1514 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1515 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1516 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1517 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 1518 return 0;
3eb6b044
TS
1519}
1520
579a97f7
FB
1521static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1522 struct semid_ds *host_sd)
3eb6b044
TS
1523{
1524 struct target_semid_ds *target_sd;
1525
579a97f7
FB
1526 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1527 return -TARGET_EFAULT;
3eb6b044
TS
1528 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1529 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1530 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1531 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1532 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 1533 return 0;
3eb6b044
TS
1534}
1535
fa294816
TS
1536union semun {
1537 int val;
3eb6b044 1538 struct semid_ds *buf;
fa294816
TS
1539 unsigned short *array;
1540};
1541
3eb6b044
TS
1542union target_semun {
1543 int val;
992f48a0 1544 abi_long buf;
3eb6b044
TS
1545 unsigned short int *array;
1546};
1547
579a97f7
FB
1548static inline abi_long target_to_host_semun(int cmd,
1549 union semun *host_su,
1550 abi_ulong target_addr,
1551 struct semid_ds *ds)
3eb6b044
TS
1552{
1553 union target_semun *target_su;
1554
1555 switch( cmd ) {
1556 case IPC_STAT:
1557 case IPC_SET:
579a97f7
FB
1558 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1559 return -TARGET_EFAULT;
3eb6b044
TS
1560 target_to_host_semid_ds(ds,target_su->buf);
1561 host_su->buf = ds;
1562 unlock_user_struct(target_su, target_addr, 0);
1563 break;
1564 case GETVAL:
1565 case SETVAL:
579a97f7
FB
1566 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1567 return -TARGET_EFAULT;
3eb6b044
TS
1568 host_su->val = tswapl(target_su->val);
1569 unlock_user_struct(target_su, target_addr, 0);
1570 break;
1571 case GETALL:
1572 case SETALL:
579a97f7
FB
1573 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1574 return -TARGET_EFAULT;
3eb6b044
TS
1575 *host_su->array = tswap16(*target_su->array);
1576 unlock_user_struct(target_su, target_addr, 0);
1577 break;
1578 default:
1579 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1580 }
579a97f7 1581 return 0;
3eb6b044
TS
1582}
1583
579a97f7
FB
1584static inline abi_long host_to_target_semun(int cmd,
1585 abi_ulong target_addr,
1586 union semun *host_su,
1587 struct semid_ds *ds)
3eb6b044
TS
1588{
1589 union target_semun *target_su;
1590
1591 switch( cmd ) {
1592 case IPC_STAT:
1593 case IPC_SET:
579a97f7
FB
1594 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1595 return -TARGET_EFAULT;
3eb6b044
TS
1596 host_to_target_semid_ds(target_su->buf,ds);
1597 unlock_user_struct(target_su, target_addr, 1);
1598 break;
1599 case GETVAL:
1600 case SETVAL:
579a97f7
FB
1601 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1602 return -TARGET_EFAULT;
3eb6b044
TS
1603 target_su->val = tswapl(host_su->val);
1604 unlock_user_struct(target_su, target_addr, 1);
1605 break;
1606 case GETALL:
1607 case SETALL:
579a97f7
FB
1608 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1609 return -TARGET_EFAULT;
3eb6b044
TS
1610 *target_su->array = tswap16(*host_su->array);
1611 unlock_user_struct(target_su, target_addr, 1);
1612 break;
1613 default:
1614 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1615 }
579a97f7 1616 return 0;
3eb6b044
TS
1617}
1618
992f48a0
BS
1619static inline abi_long do_semctl(int first, int second, int third,
1620 abi_long ptr)
3eb6b044
TS
1621{
1622 union semun arg;
1623 struct semid_ds dsarg;
1624 int cmd = third&0xff;
992f48a0 1625 abi_long ret = 0;
3eb6b044
TS
1626
1627 switch( cmd ) {
1628 case GETVAL:
1629 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1630 ret = get_errno(semctl(first, second, cmd, arg));
1631 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1632 break;
1633 case SETVAL:
1634 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1635 ret = get_errno(semctl(first, second, cmd, arg));
1636 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1637 break;
1638 case GETALL:
1639 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1640 ret = get_errno(semctl(first, second, cmd, arg));
1641 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1642 break;
1643 case SETALL:
1644 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1645 ret = get_errno(semctl(first, second, cmd, arg));
1646 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1647 break;
1648 case IPC_STAT:
1649 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1650 ret = get_errno(semctl(first, second, cmd, arg));
1651 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1652 break;
1653 case IPC_SET:
1654 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1655 ret = get_errno(semctl(first, second, cmd, arg));
1656 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1657 break;
1658 default:
1659 ret = get_errno(semctl(first, second, cmd, arg));
1660 }
1661
1662 return ret;
1663}
1664
1bc012f6
TS
1665struct target_msqid_ds
1666{
1667 struct target_ipc_perm msg_perm;
992f48a0
BS
1668 abi_ulong msg_stime;
1669 abi_ulong __unused1;
1670 abi_ulong msg_rtime;
1671 abi_ulong __unused2;
1672 abi_ulong msg_ctime;
1673 abi_ulong __unused3;
1674 abi_ulong __msg_cbytes;
1675 abi_ulong msg_qnum;
1676 abi_ulong msg_qbytes;
1677 abi_ulong msg_lspid;
1678 abi_ulong msg_lrpid;
1679 abi_ulong __unused4;
1680 abi_ulong __unused5;
1bc012f6
TS
1681};
1682
579a97f7
FB
1683static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1684 abi_ulong target_addr)
1bc012f6
TS
1685{
1686 struct target_msqid_ds *target_md;
1687
579a97f7
FB
1688 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1689 return -TARGET_EFAULT;
1bc012f6
TS
1690 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1691 host_md->msg_stime = tswapl(target_md->msg_stime);
1692 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1693 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1694 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1695 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1696 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1697 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1698 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1699 unlock_user_struct(target_md, target_addr, 0);
579a97f7 1700 return 0;
1bc012f6
TS
1701}
1702
579a97f7
FB
1703static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1704 struct msqid_ds *host_md)
1bc012f6
TS
1705{
1706 struct target_msqid_ds *target_md;
1707
579a97f7
FB
1708 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1709 return -TARGET_EFAULT;
1bc012f6
TS
1710 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1711 target_md->msg_stime = tswapl(host_md->msg_stime);
1712 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1713 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1714 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1715 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1716 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1717 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1718 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1719 unlock_user_struct(target_md, target_addr, 1);
579a97f7 1720 return 0;
1bc012f6
TS
1721}
1722
992f48a0 1723static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1bc012f6
TS
1724{
1725 struct msqid_ds dsarg;
1726 int cmd = second&0xff;
992f48a0 1727 abi_long ret = 0;
1bc012f6
TS
1728 switch( cmd ) {
1729 case IPC_STAT:
1730 case IPC_SET:
1731 target_to_host_msqid_ds(&dsarg,ptr);
1732 ret = get_errno(msgctl(first, cmd, &dsarg));
1733 host_to_target_msqid_ds(ptr,&dsarg);
1734 default:
1735 ret = get_errno(msgctl(first, cmd, &dsarg));
1736 }
1737 return ret;
1738}
1739
1740struct target_msgbuf {
992f48a0 1741 abi_ulong mtype;
1bc012f6
TS
1742 char mtext[1];
1743};
1744
992f48a0
BS
1745static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1746 unsigned int msgsz, int msgflg)
1bc012f6
TS
1747{
1748 struct target_msgbuf *target_mb;
1749 struct msgbuf *host_mb;
992f48a0 1750 abi_long ret = 0;
1bc012f6 1751
579a97f7
FB
1752 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1753 return -TARGET_EFAULT;
1bc012f6
TS
1754 host_mb = malloc(msgsz+sizeof(long));
1755 host_mb->mtype = tswapl(target_mb->mtype);
1756 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1757 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1758 free(host_mb);
1759 unlock_user_struct(target_mb, msgp, 0);
1760
1761 return ret;
1762}
1763
992f48a0
BS
1764static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1765 unsigned int msgsz, int msgtype,
1766 int msgflg)
1bc012f6
TS
1767{
1768 struct target_msgbuf *target_mb;
579a97f7 1769 char *target_mtext;
1bc012f6 1770 struct msgbuf *host_mb;
992f48a0 1771 abi_long ret = 0;
1bc012f6 1772
579a97f7
FB
1773 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1774 return -TARGET_EFAULT;
1bc012f6
TS
1775 host_mb = malloc(msgsz+sizeof(long));
1776 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
579a97f7
FB
1777 if (ret > 0) {
1778 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1779 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1780 if (!target_mtext) {
1781 ret = -TARGET_EFAULT;
1782 goto end;
1783 }
1bc012f6 1784 memcpy(target_mb->mtext, host_mb->mtext, ret);
579a97f7
FB
1785 unlock_user(target_mtext, target_mtext_addr, ret);
1786 }
1bc012f6
TS
1787 target_mb->mtype = tswapl(host_mb->mtype);
1788 free(host_mb);
1bc012f6 1789
579a97f7
FB
1790end:
1791 if (target_mb)
1792 unlock_user_struct(target_mb, msgp, 1);
1bc012f6
TS
1793 return ret;
1794}
1795
53a5960a 1796/* ??? This only works with linear mappings. */
0da46a6e 1797/* do_ipc() must return target values and target errnos. */
992f48a0
BS
1798static abi_long do_ipc(unsigned int call, int first,
1799 int second, int third,
1800 abi_long ptr, abi_long fifth)
8853f86e
FB
1801{
1802 int version;
992f48a0 1803 abi_long ret = 0;
8853f86e
FB
1804 struct shmid_ds shm_info;
1805 int i;
1806
1807 version = call >> 16;
1808 call &= 0xffff;
1809
1810 switch (call) {
fa294816 1811 case IPCOP_semop:
579a97f7 1812 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
fa294816
TS
1813 break;
1814
1815 case IPCOP_semget:
1816 ret = get_errno(semget(first, second, third));
1817 break;
1818
1819 case IPCOP_semctl:
3eb6b044 1820 ret = do_semctl(first, second, third, ptr);
fa294816
TS
1821 break;
1822
1823 case IPCOP_semtimedop:
32407103 1824 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
0da46a6e 1825 ret = -TARGET_ENOSYS;
fa294816 1826 break;
d96372ef
TS
1827
1828 case IPCOP_msgget:
1829 ret = get_errno(msgget(first, second));
1830 break;
1831
1832 case IPCOP_msgsnd:
1bc012f6 1833 ret = do_msgsnd(first, ptr, second, third);
d96372ef
TS
1834 break;
1835
1836 case IPCOP_msgctl:
1bc012f6 1837 ret = do_msgctl(first, second, ptr);
d96372ef
TS
1838 break;
1839
1840 case IPCOP_msgrcv:
1bc012f6 1841 {
579a97f7 1842 /* XXX: this code is not correct */
1bc012f6
TS
1843 struct ipc_kludge
1844 {
1845 void *__unbounded msgp;
1846 long int msgtyp;
1847 };
d96372ef 1848
579a97f7 1849 struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
1bc012f6 1850 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
d96372ef 1851
1bc012f6 1852 ret = do_msgrcv(first, (long)msgp, second, 0, third);
d96372ef 1853
1bc012f6 1854 }
d96372ef
TS
1855 break;
1856
8853f86e 1857 case IPCOP_shmat:
5a4a898d
FB
1858 {
1859 abi_ulong raddr;
1860 void *host_addr;
1861 /* SHM_* flags are the same on all linux platforms */
1862 host_addr = shmat(first, (void *)g2h(ptr), second);
1863 if (host_addr == (void *)-1) {
1864 ret = get_errno((long)host_addr);
8853f86e 1865 break;
5a4a898d
FB
1866 }
1867 raddr = h2g((unsigned long)host_addr);
1868 /* find out the length of the shared memory segment */
1869
1870 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1871 if (is_error(ret)) {
1872 /* can't get length, bail out */
1873 shmdt(host_addr);
1874 break;
1875 }
1876 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1877 PAGE_VALID | PAGE_READ |
1878 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1879 for (i = 0; i < N_SHM_REGIONS; ++i) {
1880 if (shm_regions[i].start == 0) {
1881 shm_regions[i].start = raddr;
1882 shm_regions[i].size = shm_info.shm_segsz;
1883 break;
1884 }
1885 }
1886 if (put_user(raddr, third, abi_ulong))
1887 return -TARGET_EFAULT;
1888 ret = 0;
1889 }
8853f86e
FB
1890 break;
1891 case IPCOP_shmdt:
1892 for (i = 0; i < N_SHM_REGIONS; ++i) {
1893 if (shm_regions[i].start == ptr) {
1894 shm_regions[i].start = 0;
1895 page_set_flags(ptr, shm_regions[i].size, 0);
1896 break;
1897 }
1898 }
5a4a898d 1899 ret = get_errno(shmdt((void *)g2h(ptr)));
8853f86e
FB
1900 break;
1901
1902 case IPCOP_shmget:
1903 /* IPC_* flag values are the same on all linux platforms */
1904 ret = get_errno(shmget(first, second, third));
1905 break;
1906
1907 /* IPC_* and SHM_* command values are the same on all linux platforms */
1908 case IPCOP_shmctl:
1909 switch(second) {
1910 case IPC_RMID:
1911 case SHM_LOCK:
1912 case SHM_UNLOCK:
1913 ret = get_errno(shmctl(first, second, NULL));
1914 break;
1915 default:
1916 goto unimplemented;
1917 }
1918 break;
1919 default:
1920 unimplemented:
32407103 1921 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
0da46a6e 1922 ret = -TARGET_ENOSYS;
8853f86e
FB
1923 break;
1924 }
1925 return ret;
1926}
32407103 1927#endif
8853f86e 1928
31e31b8a
FB
1929/* kernel structure types definitions */
1930#define IFNAMSIZ 16
1931
1932#define STRUCT(name, list...) STRUCT_ ## name,
1933#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1934enum {
1935#include "syscall_types.h"
1936};
1937#undef STRUCT
1938#undef STRUCT_SPECIAL
1939
1940#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1941#define STRUCT_SPECIAL(name)
1942#include "syscall_types.h"
1943#undef STRUCT
1944#undef STRUCT_SPECIAL
1945
1946typedef struct IOCTLEntry {
2ab83ea7
FB
1947 unsigned int target_cmd;
1948 unsigned int host_cmd;
31e31b8a
FB
1949 const char *name;
1950 int access;
1a9353d2 1951 const argtype arg_type[5];
31e31b8a
FB
1952} IOCTLEntry;
1953
1954#define IOC_R 0x0001
1955#define IOC_W 0x0002
1956#define IOC_RW (IOC_R | IOC_W)
1957
1958#define MAX_STRUCT_SIZE 4096
1959
2ab83ea7 1960IOCTLEntry ioctl_entries[] = {
31e31b8a
FB
1961#define IOCTL(cmd, access, types...) \
1962 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1963#include "ioctls.h"
1964 { 0, 0, },
1965};
1966
53a5960a 1967/* ??? Implement proper locking for ioctls. */
0da46a6e 1968/* do_ioctl() Must return target values and target errnos. */
992f48a0 1969static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
31e31b8a
FB
1970{
1971 const IOCTLEntry *ie;
1972 const argtype *arg_type;
992f48a0 1973 abi_long ret;
31e31b8a 1974 uint8_t buf_temp[MAX_STRUCT_SIZE];
53a5960a
PB
1975 int target_size;
1976 void *argptr;
31e31b8a
FB
1977
1978 ie = ioctl_entries;
1979 for(;;) {
1980 if (ie->target_cmd == 0) {
32407103 1981 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
0da46a6e 1982 return -TARGET_ENOSYS;
31e31b8a
FB
1983 }
1984 if (ie->target_cmd == cmd)
1985 break;
1986 ie++;
1987 }
1988 arg_type = ie->arg_type;
9de5e440 1989#if defined(DEBUG)
32407103 1990 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
72f03900 1991#endif
31e31b8a
FB
1992 switch(arg_type[0]) {
1993 case TYPE_NULL:
1994 /* no argument */
1995 ret = get_errno(ioctl(fd, ie->host_cmd));
1996 break;
1997 case TYPE_PTRVOID:
1998 case TYPE_INT:
1999 /* int argment */
2000 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2001 break;
2002 case TYPE_PTR:
2003 arg_type++;
53a5960a 2004 target_size = thunk_type_size(arg_type, 0);
31e31b8a
FB
2005 switch(ie->access) {
2006 case IOC_R:
2007 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2008 if (!is_error(ret)) {
579a97f7
FB
2009 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2010 if (!argptr)
2011 return -TARGET_EFAULT;
53a5960a
PB
2012 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2013 unlock_user(argptr, arg, target_size);
31e31b8a
FB
2014 }
2015 break;
2016 case IOC_W:
579a97f7
FB
2017 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2018 if (!argptr)
2019 return -TARGET_EFAULT;
53a5960a
PB
2020 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2021 unlock_user(argptr, arg, 0);
31e31b8a
FB
2022 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2023 break;
2024 default:
2025 case IOC_RW:
579a97f7
FB
2026 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2027 if (!argptr)
2028 return -TARGET_EFAULT;
53a5960a
PB
2029 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2030 unlock_user(argptr, arg, 0);
31e31b8a
FB
2031 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2032 if (!is_error(ret)) {
579a97f7
FB
2033 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2034 if (!argptr)
2035 return -TARGET_EFAULT;
53a5960a
PB
2036 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2037 unlock_user(argptr, arg, target_size);
31e31b8a
FB
2038 }
2039 break;
2040 }
2041 break;
2042 default:
32407103
JM
2043 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2044 (long)cmd, arg_type[0]);
0da46a6e 2045 ret = -TARGET_ENOSYS;
31e31b8a
FB
2046 break;
2047 }
2048 return ret;
2049}
2050
2051bitmask_transtbl iflag_tbl[] = {
2052 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2053 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2054 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2055 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2056 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2057 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2058 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2059 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2060 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2061 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2062 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2063 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2064 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2065 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2066 { 0, 0, 0, 0 }
2067};
2068
2069bitmask_transtbl oflag_tbl[] = {
2070 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2071 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2072 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2073 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2074 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2075 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2076 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2077 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2078 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2079 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2080 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2081 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2082 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2083 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2084 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2085 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2086 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2087 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2088 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2089 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2090 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2091 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2092 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2093 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2094 { 0, 0, 0, 0 }
2095};
2096
2097bitmask_transtbl cflag_tbl[] = {
2098 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2099 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2100 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2101 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2102 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2103 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2104 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2105 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2106 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2107 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2108 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2109 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2110 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2111 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2112 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2113 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2114 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2115 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2116 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2117 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2118 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2119 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2120 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2121 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2122 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2123 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2124 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2125 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2126 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2127 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2128 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2129 { 0, 0, 0, 0 }
2130};
2131
2132bitmask_transtbl lflag_tbl[] = {
2133 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2134 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2135 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2136 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2137 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2138 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2139 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2140 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2141 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2142 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2143 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2144 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2145 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2146 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2147 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2148 { 0, 0, 0, 0 }
2149};
2150
2151static void target_to_host_termios (void *dst, const void *src)
2152{
2153 struct host_termios *host = dst;
2154 const struct target_termios *target = src;
3b46e624 2155
5fafdf24 2156 host->c_iflag =
31e31b8a 2157 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
5fafdf24 2158 host->c_oflag =
31e31b8a 2159 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
5fafdf24 2160 host->c_cflag =
31e31b8a 2161 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
5fafdf24 2162 host->c_lflag =
31e31b8a
FB
2163 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2164 host->c_line = target->c_line;
3b46e624 2165
5fafdf24
TS
2166 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2167 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3b46e624 2168 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
5fafdf24 2169 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3b46e624 2170 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
5fafdf24 2171 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3b46e624 2172 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
5fafdf24 2173 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3b46e624 2174 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
5fafdf24
TS
2175 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2176 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3b46e624
TS
2177 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2178 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2179 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2180 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2181 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
5fafdf24 2182 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
31e31b8a 2183}
3b46e624 2184
31e31b8a
FB
2185static void host_to_target_termios (void *dst, const void *src)
2186{
2187 struct target_termios *target = dst;
2188 const struct host_termios *host = src;
2189
5fafdf24 2190 target->c_iflag =
31e31b8a 2191 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
5fafdf24 2192 target->c_oflag =
31e31b8a 2193 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
5fafdf24 2194 target->c_cflag =
31e31b8a 2195 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
5fafdf24 2196 target->c_lflag =
31e31b8a
FB
2197 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2198 target->c_line = host->c_line;
3b46e624 2199
31e31b8a
FB
2200 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2201 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2202 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2203 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2204 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2205 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2206 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2207 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2208 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2209 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2210 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2211 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2212 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2213 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2214 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2215 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2216 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2217}
2218
2219StructEntry struct_termios_def = {
2220 .convert = { host_to_target_termios, target_to_host_termios },
2221 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2222 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2223};
2224
5286db75
FB
2225static bitmask_transtbl mmap_flags_tbl[] = {
2226 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2227 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2228 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2229 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2230 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2231 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2232 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2233 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2234 { 0, 0, 0, 0 }
2235};
2236
ffa65c3b
FB
2237static bitmask_transtbl fcntl_flags_tbl[] = {
2238 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2239 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2240 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2241 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2242 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2243 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2244 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2245 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2246 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2247 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2248 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2249 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2250 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
121061dc 2251#if defined(O_DIRECT)
ffa65c3b 2252 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
121061dc 2253#endif
ffa65c3b
FB
2254 { 0, 0, 0, 0 }
2255};
2256
2ab83ea7 2257#if defined(TARGET_I386)
6dbad63e
FB
2258
2259/* NOTE: there is really one LDT for all the threads */
2260uint8_t *ldt_table;
2261
03acab66 2262static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
6dbad63e
FB
2263{
2264 int size;
53a5960a 2265 void *p;
6dbad63e
FB
2266
2267 if (!ldt_table)
2268 return 0;
2269 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2270 if (size > bytecount)
2271 size = bytecount;
579a97f7
FB
2272 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2273 if (!p)
03acab66 2274 return -TARGET_EFAULT;
579a97f7 2275 /* ??? Should this by byteswapped? */
53a5960a
PB
2276 memcpy(p, ldt_table, size);
2277 unlock_user(p, ptr, size);
6dbad63e
FB
2278 return size;
2279}
2280
2281/* XXX: add locking support */
03acab66
FB
2282static abi_long write_ldt(CPUX86State *env,
2283 abi_ulong ptr, unsigned long bytecount, int oldmode)
6dbad63e
FB
2284{
2285 struct target_modify_ldt_ldt_s ldt_info;
53a5960a 2286 struct target_modify_ldt_ldt_s *target_ldt_info;
6dbad63e
FB
2287 int seg_32bit, contents, read_exec_only, limit_in_pages;
2288 int seg_not_present, useable;
2289 uint32_t *lp, entry_1, entry_2;
2290
2291 if (bytecount != sizeof(ldt_info))
03acab66 2292 return -TARGET_EINVAL;
579a97f7 2293 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
03acab66 2294 return -TARGET_EFAULT;
53a5960a
PB
2295 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2296 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2297 ldt_info.limit = tswap32(target_ldt_info->limit);
2298 ldt_info.flags = tswap32(target_ldt_info->flags);
2299 unlock_user_struct(target_ldt_info, ptr, 0);
3b46e624 2300
6dbad63e 2301 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
03acab66 2302 return -TARGET_EINVAL;
6dbad63e
FB
2303 seg_32bit = ldt_info.flags & 1;
2304 contents = (ldt_info.flags >> 1) & 3;
2305 read_exec_only = (ldt_info.flags >> 3) & 1;
2306 limit_in_pages = (ldt_info.flags >> 4) & 1;
2307 seg_not_present = (ldt_info.flags >> 5) & 1;
2308 useable = (ldt_info.flags >> 6) & 1;
2309
2310 if (contents == 3) {
2311 if (oldmode)
03acab66 2312 return -TARGET_EINVAL;
6dbad63e 2313 if (seg_not_present == 0)
03acab66 2314 return -TARGET_EINVAL;
6dbad63e
FB
2315 }
2316 /* allocate the LDT */
2317 if (!ldt_table) {
2318 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2319 if (!ldt_table)
03acab66 2320 return -TARGET_ENOMEM;
6dbad63e 2321 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
eeeac3f3 2322 env->ldt.base = h2g((unsigned long)ldt_table);
6dbad63e
FB
2323 env->ldt.limit = 0xffff;
2324 }
2325
2326 /* NOTE: same code as Linux kernel */
2327 /* Allow LDTs to be cleared by the user. */
2328 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2329 if (oldmode ||
2330 (contents == 0 &&
2331 read_exec_only == 1 &&
2332 seg_32bit == 0 &&
2333 limit_in_pages == 0 &&
2334 seg_not_present == 1 &&
2335 useable == 0 )) {
2336 entry_1 = 0;
2337 entry_2 = 0;
2338 goto install;
2339 }
2340 }
3b46e624 2341
6dbad63e
FB
2342 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2343 (ldt_info.limit & 0x0ffff);
2344 entry_2 = (ldt_info.base_addr & 0xff000000) |
2345 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2346 (ldt_info.limit & 0xf0000) |
2347 ((read_exec_only ^ 1) << 9) |
2348 (contents << 10) |
2349 ((seg_not_present ^ 1) << 15) |
2350 (seg_32bit << 22) |
2351 (limit_in_pages << 23) |
2352 0x7000;
2353 if (!oldmode)
2354 entry_2 |= (useable << 20);
14ae3ba7 2355
6dbad63e
FB
2356 /* Install the new entry ... */
2357install:
2358 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2359 lp[0] = tswap32(entry_1);
2360 lp[1] = tswap32(entry_2);
2361 return 0;
2362}
2363
2364/* specific and weird i386 syscalls */
03acab66
FB
2365abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2366 unsigned long bytecount)
6dbad63e 2367{
03acab66 2368 abi_long ret;
3b46e624 2369
6dbad63e
FB
2370 switch (func) {
2371 case 0:
2372 ret = read_ldt(ptr, bytecount);
2373 break;
2374 case 1:
2375 ret = write_ldt(env, ptr, bytecount, 1);
2376 break;
2377 case 0x11:
2378 ret = write_ldt(env, ptr, bytecount, 0);
2379 break;
03acab66
FB
2380 default:
2381 ret = -TARGET_ENOSYS;
2382 break;
6dbad63e
FB
2383 }
2384 return ret;
2385}
1b6b029e 2386
2ab83ea7
FB
2387#endif /* defined(TARGET_I386) */
2388
1b6b029e
FB
2389/* this stack is the equivalent of the kernel stack associated with a
2390 thread/process */
2391#define NEW_STACK_SIZE 8192
2392
2393static int clone_func(void *arg)
2394{
2ab83ea7 2395 CPUState *env = arg;
1b6b029e
FB
2396 cpu_loop(env);
2397 /* never exits */
2398 return 0;
2399}
2400
0da46a6e
TS
2401/* do_fork() Must return host values and target errnos (unlike most
2402 do_*() functions). */
992f48a0 2403int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
1b6b029e
FB
2404{
2405 int ret;
5cd4393b 2406 TaskState *ts;
1b6b029e 2407 uint8_t *new_stack;
2ab83ea7 2408 CPUState *new_env;
3b46e624 2409
1b6b029e 2410 if (flags & CLONE_VM) {
5cd4393b
FB
2411 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2412 memset(ts, 0, sizeof(TaskState));
2413 new_stack = ts->stack;
2414 ts->used = 1;
2415 /* add in task state list */
2416 ts->next = first_task_state;
2417 first_task_state = ts;
1b6b029e 2418 /* we create a new CPU instance. */
c5be9f08 2419 new_env = cpu_copy(env);
2ab83ea7
FB
2420#if defined(TARGET_I386)
2421 if (!newsp)
2422 newsp = env->regs[R_ESP];
1b6b029e
FB
2423 new_env->regs[R_ESP] = newsp;
2424 new_env->regs[R_EAX] = 0;
2ab83ea7
FB
2425#elif defined(TARGET_ARM)
2426 if (!newsp)
2427 newsp = env->regs[13];
2428 new_env->regs[13] = newsp;
2429 new_env->regs[0] = 0;
ebc05488 2430#elif defined(TARGET_SPARC)
48dc41eb
FB
2431 if (!newsp)
2432 newsp = env->regwptr[22];
2433 new_env->regwptr[22] = newsp;
2434 new_env->regwptr[0] = 0;
2435 /* XXXXX */
67867308 2436 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
e6e5906b
PB
2437#elif defined(TARGET_M68K)
2438 if (!newsp)
2439 newsp = env->aregs[7];
2440 new_env->aregs[7] = newsp;
2441 new_env->dregs[0] = 0;
2442 /* ??? is this sufficient? */
048f6b4d 2443#elif defined(TARGET_MIPS)
388bb21a 2444 if (!newsp)
ead9360e
TS
2445 newsp = env->gpr[29][env->current_tc];
2446 new_env->gpr[29][env->current_tc] = newsp;
67867308
FB
2447#elif defined(TARGET_PPC)
2448 if (!newsp)
2449 newsp = env->gpr[1];
2450 new_env->gpr[1] = newsp;
5fafdf24 2451 {
67867308
FB
2452 int i;
2453 for (i = 7; i < 32; i++)
2454 new_env->gpr[i] = 0;
2455 }
fdf9b3e8
FB
2456#elif defined(TARGET_SH4)
2457 if (!newsp)
2458 newsp = env->gregs[15];
2459 new_env->gregs[15] = newsp;
2460 /* XXXXX */
7a3148a9
JM
2461#elif defined(TARGET_ALPHA)
2462 if (!newsp)
2463 newsp = env->ir[30];
2464 new_env->ir[30] = newsp;
2465 /* ? */
2466 {
2467 int i;
2468 for (i = 7; i < 30; i++)
2469 new_env->ir[i] = 0;
2470 }
48733d19
TS
2471#elif defined(TARGET_CRIS)
2472 if (!newsp)
2473 newsp = env->regs[14];
2474 new_env->regs[14] = newsp;
2ab83ea7
FB
2475#else
2476#error unsupported target CPU
2477#endif
5cd4393b 2478 new_env->opaque = ts;
27725c1d 2479#ifdef __ia64__
fd4a43e4 2480 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
27725c1d
FB
2481#else
2482 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2483#endif
1b6b029e
FB
2484 } else {
2485 /* if no CLONE_VM, we consider it is a fork */
2486 if ((flags & ~CSIGNAL) != 0)
2487 return -EINVAL;
2488 ret = fork();
2489 }
2490 return ret;
2491}
2492
992f48a0 2493static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
7775e9ec
FB
2494{
2495 struct flock fl;
53a5960a 2496 struct target_flock *target_fl;
43f238d7
TS
2497 struct flock64 fl64;
2498 struct target_flock64 *target_fl64;
992f48a0 2499 abi_long ret;
53a5960a 2500
7775e9ec
FB
2501 switch(cmd) {
2502 case TARGET_F_GETLK:
579a97f7
FB
2503 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2504 return -TARGET_EFAULT;
5813427b
TS
2505 fl.l_type = tswap16(target_fl->l_type);
2506 fl.l_whence = tswap16(target_fl->l_whence);
2507 fl.l_start = tswapl(target_fl->l_start);
2508 fl.l_len = tswapl(target_fl->l_len);
2509 fl.l_pid = tswapl(target_fl->l_pid);
2510 unlock_user_struct(target_fl, arg, 0);
9ee1fa2c 2511 ret = get_errno(fcntl(fd, cmd, &fl));
7775e9ec 2512 if (ret == 0) {
579a97f7
FB
2513 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2514 return -TARGET_EFAULT;
7775e9ec
FB
2515 target_fl->l_type = tswap16(fl.l_type);
2516 target_fl->l_whence = tswap16(fl.l_whence);
2517 target_fl->l_start = tswapl(fl.l_start);
2518 target_fl->l_len = tswapl(fl.l_len);
2519 target_fl->l_pid = tswapl(fl.l_pid);
53a5960a 2520 unlock_user_struct(target_fl, arg, 1);
7775e9ec
FB
2521 }
2522 break;
3b46e624 2523
7775e9ec
FB
2524 case TARGET_F_SETLK:
2525 case TARGET_F_SETLKW:
579a97f7
FB
2526 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2527 return -TARGET_EFAULT;
7775e9ec
FB
2528 fl.l_type = tswap16(target_fl->l_type);
2529 fl.l_whence = tswap16(target_fl->l_whence);
2530 fl.l_start = tswapl(target_fl->l_start);
2531 fl.l_len = tswapl(target_fl->l_len);
2532 fl.l_pid = tswapl(target_fl->l_pid);
53a5960a 2533 unlock_user_struct(target_fl, arg, 0);
9ee1fa2c 2534 ret = get_errno(fcntl(fd, cmd, &fl));
7775e9ec 2535 break;
3b46e624 2536
7775e9ec 2537 case TARGET_F_GETLK64:
579a97f7
FB
2538 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2539 return -TARGET_EFAULT;
5813427b
TS
2540 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2541 fl64.l_whence = tswap16(target_fl64->l_whence);
2542 fl64.l_start = tswapl(target_fl64->l_start);
2543 fl64.l_len = tswapl(target_fl64->l_len);
2544 fl64.l_pid = tswap16(target_fl64->l_pid);
2545 unlock_user_struct(target_fl64, arg, 0);
9ee1fa2c 2546 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
43f238d7 2547 if (ret == 0) {
579a97f7
FB
2548 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2549 return -TARGET_EFAULT;
43f238d7
TS
2550 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2551 target_fl64->l_whence = tswap16(fl64.l_whence);
2552 target_fl64->l_start = tswapl(fl64.l_start);
2553 target_fl64->l_len = tswapl(fl64.l_len);
2554 target_fl64->l_pid = tswapl(fl64.l_pid);
2555 unlock_user_struct(target_fl64, arg, 1);
2556 }
9ee1fa2c 2557 break;
7775e9ec
FB
2558 case TARGET_F_SETLK64:
2559 case TARGET_F_SETLKW64:
579a97f7
FB
2560 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2561 return -TARGET_EFAULT;
43f238d7
TS
2562 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2563 fl64.l_whence = tswap16(target_fl64->l_whence);
2564 fl64.l_start = tswapl(target_fl64->l_start);
2565 fl64.l_len = tswapl(target_fl64->l_len);
2566 fl64.l_pid = tswap16(target_fl64->l_pid);
2567 unlock_user_struct(target_fl64, arg, 0);
9ee1fa2c 2568 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
7775e9ec
FB
2569 break;
2570
ffa65c3b 2571 case F_GETFL:
9ee1fa2c
FB
2572 ret = get_errno(fcntl(fd, cmd, arg));
2573 if (ret >= 0) {
2574 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2575 }
ffa65c3b
FB
2576 break;
2577
2578 case F_SETFL:
9ee1fa2c 2579 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
ffa65c3b
FB
2580 break;
2581
7775e9ec 2582 default:
9ee1fa2c 2583 ret = get_errno(fcntl(fd, cmd, arg));
7775e9ec
FB
2584 break;
2585 }
2586 return ret;
2587}
2588
67867308 2589#ifdef USE_UID16
7775e9ec 2590
67867308
FB
2591static inline int high2lowuid(int uid)
2592{
2593 if (uid > 65535)
2594 return 65534;
2595 else
2596 return uid;
2597}
2598
2599static inline int high2lowgid(int gid)
2600{
2601 if (gid > 65535)
2602 return 65534;
2603 else
2604 return gid;
2605}
2606
2607static inline int low2highuid(int uid)
2608{
2609 if ((int16_t)uid == -1)
2610 return -1;
2611 else
2612 return uid;
2613}
2614
2615static inline int low2highgid(int gid)
2616{
2617 if ((int16_t)gid == -1)
2618 return -1;
2619 else
2620 return gid;
2621}
2622
2623#endif /* USE_UID16 */
1b6b029e 2624
31e31b8a
FB
2625void syscall_init(void)
2626{
2ab83ea7
FB
2627 IOCTLEntry *ie;
2628 const argtype *arg_type;
2629 int size;
b92c47c1 2630 int i;
2ab83ea7 2631
5fafdf24
TS
2632#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2633#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
31e31b8a
FB
2634#include "syscall_types.h"
2635#undef STRUCT
2636#undef STRUCT_SPECIAL
2ab83ea7
FB
2637
2638 /* we patch the ioctl size if necessary. We rely on the fact that
2639 no ioctl has all the bits at '1' in the size field */
2640 ie = ioctl_entries;
2641 while (ie->target_cmd != 0) {
2642 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2643 TARGET_IOC_SIZEMASK) {
2644 arg_type = ie->arg_type;
2645 if (arg_type[0] != TYPE_PTR) {
5fafdf24 2646 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2ab83ea7
FB
2647 ie->target_cmd);
2648 exit(1);
2649 }
2650 arg_type++;
2651 size = thunk_type_size(arg_type, 0);
5fafdf24 2652 ie->target_cmd = (ie->target_cmd &
2ab83ea7
FB
2653 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2654 (size << TARGET_IOC_SIZESHIFT);
2655 }
b92c47c1
TS
2656
2657 /* Build target_to_host_errno_table[] table from
2658 * host_to_target_errno_table[]. */
2659 for (i=0; i < ERRNO_TABLE_SIZE; i++)
2660 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2661
2ab83ea7
FB
2662 /* automatic consistency check if same arch */
2663#if defined(__i386__) && defined(TARGET_I386)
2664 if (ie->target_cmd != ie->host_cmd) {
5fafdf24 2665 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2ab83ea7
FB
2666 ie->target_cmd, ie->host_cmd);
2667 }
2668#endif
2669 ie++;
2670 }
31e31b8a 2671}
c573ff67 2672
992f48a0 2673#if TARGET_ABI_BITS == 32
ce4defa0
PB
2674static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2675{
2676#ifdef TARGET_WORDS_BIG_ENDIAN
2677 return ((uint64_t)word0 << 32) | word1;
2678#else
2679 return ((uint64_t)word1 << 32) | word0;
2680#endif
2681}
992f48a0 2682#else /* TARGET_ABI_BITS == 32 */
32407103
JM
2683static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2684{
2685 return word0;
2686}
992f48a0 2687#endif /* TARGET_ABI_BITS != 32 */
ce4defa0
PB
2688
2689#ifdef TARGET_NR_truncate64
992f48a0
BS
2690static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2691 abi_long arg2,
2692 abi_long arg3,
2693 abi_long arg4)
ce4defa0
PB
2694{
2695#ifdef TARGET_ARM
2696 if (((CPUARMState *)cpu_env)->eabi)
2697 {
2698 arg2 = arg3;
2699 arg3 = arg4;
2700 }
2701#endif
2702 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2703}
2704#endif
2705
2706#ifdef TARGET_NR_ftruncate64
992f48a0
BS
2707static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2708 abi_long arg2,
2709 abi_long arg3,
2710 abi_long arg4)
ce4defa0
PB
2711{
2712#ifdef TARGET_ARM
2713 if (((CPUARMState *)cpu_env)->eabi)
2714 {
2715 arg2 = arg3;
2716 arg3 = arg4;
2717 }
2718#endif
2719 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2720}
2721#endif
2722
579a97f7
FB
2723static inline abi_long target_to_host_timespec(struct timespec *host_ts,
2724 abi_ulong target_addr)
53a5960a
PB
2725{
2726 struct target_timespec *target_ts;
2727
579a97f7
FB
2728 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
2729 return -TARGET_EFAULT;
53a5960a
PB
2730 host_ts->tv_sec = tswapl(target_ts->tv_sec);
2731 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2732 unlock_user_struct(target_ts, target_addr, 0);
2733}
2734
579a97f7
FB
2735static inline abi_long host_to_target_timespec(abi_ulong target_addr,
2736 struct timespec *host_ts)
53a5960a
PB
2737{
2738 struct target_timespec *target_ts;
2739
579a97f7
FB
2740 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
2741 return -TARGET_EFAULT;
53a5960a
PB
2742 target_ts->tv_sec = tswapl(host_ts->tv_sec);
2743 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2744 unlock_user_struct(target_ts, target_addr, 1);
2745}
2746
0da46a6e
TS
2747/* do_syscall() should always have a single exit point at the end so
2748 that actions, such as logging of syscall results, can be performed.
2749 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
992f48a0
BS
2750abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2751 abi_long arg2, abi_long arg3, abi_long arg4,
2752 abi_long arg5, abi_long arg6)
31e31b8a 2753{
992f48a0 2754 abi_long ret;
31e31b8a 2755 struct stat st;
56c8f68f 2756 struct statfs stfs;
53a5960a 2757 void *p;
3b46e624 2758
72f03900 2759#ifdef DEBUG
c573ff67 2760 gemu_log("syscall %d", num);
72f03900 2761#endif
b92c47c1
TS
2762 if(do_strace)
2763 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
2764
31e31b8a
FB
2765 switch(num) {
2766 case TARGET_NR_exit:
7d13299d
FB
2767#ifdef HAVE_GPROF
2768 _mcleanup();
2769#endif
e9009676 2770 gdb_exit(cpu_env, arg1);
1b6b029e 2771 /* XXX: should free thread stack and CPU env */
31e31b8a
FB
2772 _exit(arg1);
2773 ret = 0; /* avoid warning */
2774 break;
2775 case TARGET_NR_read:
579a97f7
FB
2776 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
2777 goto efault;
53a5960a
PB
2778 ret = get_errno(read(arg1, p, arg3));
2779 unlock_user(p, arg2, ret);
31e31b8a
FB
2780 break;
2781 case TARGET_NR_write:
579a97f7
FB
2782 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
2783 goto efault;
53a5960a
PB
2784 ret = get_errno(write(arg1, p, arg3));
2785 unlock_user(p, arg2, 0);
31e31b8a
FB
2786 break;
2787 case TARGET_NR_open:
579a97f7
FB
2788 if (!(p = lock_user_string(arg1))) {
2789 return -TARGET_EFAULT;
2790 goto fail;
2791 }
53a5960a 2792 ret = get_errno(open(path(p),
ffa65c3b
FB
2793 target_to_host_bitmask(arg2, fcntl_flags_tbl),
2794 arg3));
53a5960a 2795 unlock_user(p, arg1, 0);
31e31b8a 2796 break;
82424832
TS
2797#if defined(TARGET_NR_openat) && defined(__NR_openat)
2798 case TARGET_NR_openat:
579a97f7
FB
2799 if (!(p = lock_user_string(arg2)))
2800 goto efault;
2801 ret = get_errno(sys_openat(arg1,
2802 path(p),
2803 target_to_host_bitmask(arg3, fcntl_flags_tbl),
2804 arg4));
2805 unlock_user(p, arg2, 0);
82424832
TS
2806 break;
2807#endif
31e31b8a
FB
2808 case TARGET_NR_close:
2809 ret = get_errno(close(arg1));
2810 break;
2811 case TARGET_NR_brk:
53a5960a 2812 ret = do_brk(arg1);
31e31b8a
FB
2813 break;
2814 case TARGET_NR_fork:
1b6b029e 2815 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
31e31b8a 2816 break;
e5febef5 2817#ifdef TARGET_NR_waitpid
31e31b8a
FB
2818 case TARGET_NR_waitpid:
2819 {
53a5960a
PB
2820 int status;
2821 ret = get_errno(waitpid(arg1, &status, arg3));
2822 if (!is_error(ret) && arg2)
2823 tput32(arg2, status);
31e31b8a
FB
2824 }
2825 break;
e5febef5 2826#endif
7a3148a9 2827#ifdef TARGET_NR_creat /* not on alpha */
31e31b8a 2828 case TARGET_NR_creat:
579a97f7
FB
2829 if (!(p = lock_user_string(arg1)))
2830 goto efault;
53a5960a
PB
2831 ret = get_errno(creat(p, arg2));
2832 unlock_user(p, arg1, 0);
31e31b8a 2833 break;
7a3148a9 2834#endif
31e31b8a 2835 case TARGET_NR_link:
53a5960a
PB
2836 {
2837 void * p2;
2838 p = lock_user_string(arg1);
2839 p2 = lock_user_string(arg2);
579a97f7
FB
2840 if (!p || !p2)
2841 ret = -TARGET_EFAULT;
2842 else
2843 ret = get_errno(link(p, p2));
53a5960a
PB
2844 unlock_user(p2, arg2, 0);
2845 unlock_user(p, arg1, 0);
2846 }
31e31b8a 2847 break;
64f0ce4c
TS
2848#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2849 case TARGET_NR_linkat:
64f0ce4c
TS
2850 {
2851 void * p2 = NULL;
579a97f7
FB
2852 if (!arg2 || !arg4)
2853 goto efault;
64f0ce4c
TS
2854 p = lock_user_string(arg2);
2855 p2 = lock_user_string(arg4);
579a97f7 2856 if (!p || !p2)
0da46a6e 2857 ret = -TARGET_EFAULT;
64f0ce4c
TS
2858 else
2859 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
579a97f7
FB
2860 unlock_user(p, arg2, 0);
2861 unlock_user(p2, arg4, 0);
64f0ce4c
TS
2862 }
2863 break;
2864#endif
31e31b8a 2865 case TARGET_NR_unlink:
579a97f7
FB
2866 if (!(p = lock_user_string(arg1)))
2867 goto efault;
53a5960a
PB
2868 ret = get_errno(unlink(p));
2869 unlock_user(p, arg1, 0);
31e31b8a 2870 break;
8170f56b
TS
2871#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2872 case TARGET_NR_unlinkat:
579a97f7
FB
2873 if (!(p = lock_user_string(arg2)))
2874 goto efault;
2875 ret = get_errno(sys_unlinkat(arg1, p, arg3));
2876 unlock_user(p, arg2, 0);
8170f56b 2877#endif
31e31b8a 2878 case TARGET_NR_execve:
7854b056
FB
2879 {
2880 char **argp, **envp;
f7341ff4 2881 int argc, envc;
992f48a0
BS
2882 abi_ulong gp;
2883 abi_ulong guest_argp;
2884 abi_ulong guest_envp;
2885 abi_ulong addr;
7854b056
FB
2886 char **q;
2887
f7341ff4 2888 argc = 0;
53a5960a
PB
2889 guest_argp = arg2;
2890 for (gp = guest_argp; tgetl(gp); gp++)
7854b056 2891 argc++;
f7341ff4 2892 envc = 0;
53a5960a
PB
2893 guest_envp = arg3;
2894 for (gp = guest_envp; tgetl(gp); gp++)
7854b056
FB
2895 envc++;
2896
f7341ff4
FB
2897 argp = alloca((argc + 1) * sizeof(void *));
2898 envp = alloca((envc + 1) * sizeof(void *));
7854b056 2899
53a5960a 2900 for (gp = guest_argp, q = argp; ;
992f48a0 2901 gp += sizeof(abi_ulong), q++) {
53a5960a
PB
2902 addr = tgetl(gp);
2903 if (!addr)
2904 break;
579a97f7
FB
2905 if (!(*q = lock_user_string(addr))) {
2906 ret = -TARGET_EFAULT;
2907 goto execve_fail;
2908 }
53a5960a 2909 }
f7341ff4
FB
2910 *q = NULL;
2911
53a5960a 2912 for (gp = guest_envp, q = envp; ;
992f48a0 2913 gp += sizeof(abi_ulong), q++) {
53a5960a
PB
2914 addr = tgetl(gp);
2915 if (!addr)
2916 break;
579a97f7
FB
2917 if (!(*q = lock_user_string(addr))) {
2918 ret = -TARGET_EFAULT;
2919 goto execve_fail;
2920 }
53a5960a 2921 }
f7341ff4 2922 *q = NULL;
7854b056 2923
579a97f7
FB
2924 if (!(p = lock_user_string(arg1))) {
2925 ret = -TARGET_EFAULT;
2926 goto execve_fail;
2927 }
53a5960a
PB
2928 ret = get_errno(execve(p, argp, envp));
2929 unlock_user(p, arg1, 0);
2930
579a97f7 2931 execve_fail:
53a5960a 2932 for (gp = guest_argp, q = argp; *q;
992f48a0 2933 gp += sizeof(abi_ulong), q++) {
53a5960a
PB
2934 addr = tgetl(gp);
2935 unlock_user(*q, addr, 0);
2936 }
2937 for (gp = guest_envp, q = envp; *q;
992f48a0 2938 gp += sizeof(abi_ulong), q++) {
53a5960a
PB
2939 addr = tgetl(gp);
2940 unlock_user(*q, addr, 0);
2941 }
7854b056 2942 }
31e31b8a
FB
2943 break;
2944 case TARGET_NR_chdir:
579a97f7
FB
2945 if (!(p = lock_user_string(arg1)))
2946 goto efault;
53a5960a
PB
2947 ret = get_errno(chdir(p));
2948 unlock_user(p, arg1, 0);
31e31b8a 2949 break;
a315a145 2950#ifdef TARGET_NR_time
31e31b8a
FB
2951 case TARGET_NR_time:
2952 {
53a5960a
PB
2953 time_t host_time;
2954 ret = get_errno(time(&host_time));
2955 if (!is_error(ret) && arg1)
2956 tputl(arg1, host_time);
31e31b8a
FB
2957 }
2958 break;
a315a145 2959#endif
31e31b8a 2960 case TARGET_NR_mknod:
579a97f7
FB
2961 if (!(p = lock_user_string(arg1)))
2962 goto efault;
53a5960a
PB
2963 ret = get_errno(mknod(p, arg2, arg3));
2964 unlock_user(p, arg1, 0);
31e31b8a 2965 break;
75ac37a0
TS
2966#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2967 case TARGET_NR_mknodat:
579a97f7
FB
2968 if (!(p = lock_user_string(arg2)))
2969 goto efault;
2970 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2971 unlock_user(p, arg2, 0);
75ac37a0
TS
2972 break;
2973#endif
31e31b8a 2974 case TARGET_NR_chmod:
579a97f7
FB
2975 if (!(p = lock_user_string(arg1)))
2976 goto efault;
53a5960a
PB
2977 ret = get_errno(chmod(p, arg2));
2978 unlock_user(p, arg1, 0);
31e31b8a 2979 break;
ebc05488 2980#ifdef TARGET_NR_break
31e31b8a
FB
2981 case TARGET_NR_break:
2982 goto unimplemented;
ebc05488
FB
2983#endif
2984#ifdef TARGET_NR_oldstat
31e31b8a
FB
2985 case TARGET_NR_oldstat:
2986 goto unimplemented;
ebc05488 2987#endif
31e31b8a
FB
2988 case TARGET_NR_lseek:
2989 ret = get_errno(lseek(arg1, arg2, arg3));
2990 break;
7a3148a9
JM
2991#ifdef TARGET_NR_getxpid
2992 case TARGET_NR_getxpid:
2993#else
31e31b8a 2994 case TARGET_NR_getpid:
7a3148a9 2995#endif
31e31b8a
FB
2996 ret = get_errno(getpid());
2997 break;
2998 case TARGET_NR_mount:
80265918
TS
2999 {
3000 /* need to look at the data field */
3001 void *p2, *p3;
3002 p = lock_user_string(arg1);
3003 p2 = lock_user_string(arg2);
3004 p3 = lock_user_string(arg3);
579a97f7
FB
3005 if (!p || !p2 || !p3)
3006 ret = -TARGET_EFAULT;
3007 else
3008 /* FIXME - arg5 should be locked, but it isn't clear how to
3009 * do that since it's not guaranteed to be a NULL-terminated
3010 * string.
3011 */
3012 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3013 unlock_user(p, arg1, 0);
3014 unlock_user(p2, arg2, 0);
3015 unlock_user(p3, arg3, 0);
80265918
TS
3016 break;
3017 }
e5febef5 3018#ifdef TARGET_NR_umount
31e31b8a 3019 case TARGET_NR_umount:
579a97f7
FB
3020 if (!(p = lock_user_string(arg1)))
3021 goto efault;
53a5960a
PB
3022 ret = get_errno(umount(p));
3023 unlock_user(p, arg1, 0);
31e31b8a 3024 break;
e5febef5 3025#endif
7a3148a9 3026#ifdef TARGET_NR_stime /* not on alpha */
31e31b8a
FB
3027 case TARGET_NR_stime:
3028 {
53a5960a
PB
3029 time_t host_time;
3030 host_time = tgetl(arg1);
3031 ret = get_errno(stime(&host_time));
31e31b8a
FB
3032 }
3033 break;
7a3148a9 3034#endif
31e31b8a
FB
3035 case TARGET_NR_ptrace:
3036 goto unimplemented;
7a3148a9 3037#ifdef TARGET_NR_alarm /* not on alpha */
31e31b8a
FB
3038 case TARGET_NR_alarm:
3039 ret = alarm(arg1);
3040 break;
7a3148a9 3041#endif
ebc05488 3042#ifdef TARGET_NR_oldfstat
31e31b8a
FB
3043 case TARGET_NR_oldfstat:
3044 goto unimplemented;
ebc05488 3045#endif
7a3148a9 3046#ifdef TARGET_NR_pause /* not on alpha */
31e31b8a
FB
3047 case TARGET_NR_pause:
3048 ret = get_errno(pause());
3049 break;
7a3148a9 3050#endif
e5febef5 3051#ifdef TARGET_NR_utime
31e31b8a 3052 case TARGET_NR_utime:
ebc05488 3053 {
53a5960a
PB
3054 struct utimbuf tbuf, *host_tbuf;
3055 struct target_utimbuf *target_tbuf;
3056 if (arg2) {
579a97f7
FB
3057 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3058 goto efault;
53a5960a
PB
3059 tbuf.actime = tswapl(target_tbuf->actime);
3060 tbuf.modtime = tswapl(target_tbuf->modtime);
3061 unlock_user_struct(target_tbuf, arg2, 0);
3062 host_tbuf = &tbuf;
f72e8ff4 3063 } else {
53a5960a 3064 host_tbuf = NULL;
f72e8ff4 3065 }
579a97f7
FB
3066 if (!(p = lock_user_string(arg1)))
3067 goto efault;
53a5960a
PB
3068 ret = get_errno(utime(p, host_tbuf));
3069 unlock_user(p, arg1, 0);
ebc05488
FB
3070 }
3071 break;
e5febef5 3072#endif
978a66ff
FB
3073 case TARGET_NR_utimes:
3074 {
978a66ff 3075 struct timeval *tvp, tv[2];
53a5960a
PB
3076 if (arg2) {
3077 target_to_host_timeval(&tv[0], arg2);
3078 target_to_host_timeval(&tv[1],
3079 arg2 + sizeof (struct target_timeval));
978a66ff
FB
3080 tvp = tv;
3081 } else {
3082 tvp = NULL;
3083 }
579a97f7
FB
3084 if (!(p = lock_user_string(arg1)))
3085 goto efault;
53a5960a
PB
3086 ret = get_errno(utimes(p, tvp));
3087 unlock_user(p, arg1, 0);
978a66ff
FB
3088 }
3089 break;
ebc05488 3090#ifdef TARGET_NR_stty
31e31b8a
FB
3091 case TARGET_NR_stty:
3092 goto unimplemented;
ebc05488
FB
3093#endif
3094#ifdef TARGET_NR_gtty
31e31b8a
FB
3095 case TARGET_NR_gtty:
3096 goto unimplemented;
ebc05488 3097#endif
31e31b8a 3098 case TARGET_NR_access:
579a97f7
FB
3099 if (!(p = lock_user_string(arg1)))
3100 goto efault;
53a5960a
PB
3101 ret = get_errno(access(p, arg2));
3102 unlock_user(p, arg1, 0);
31e31b8a 3103 break;
92a34c10
TS
3104#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3105 case TARGET_NR_faccessat:
579a97f7
FB
3106 if (!(p = lock_user_string(arg2)))
3107 goto efault;
3108 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3109 unlock_user(p, arg2, 0);
92a34c10
TS
3110 break;
3111#endif
7a3148a9 3112#ifdef TARGET_NR_nice /* not on alpha */
31e31b8a
FB
3113 case TARGET_NR_nice:
3114 ret = get_errno(nice(arg1));
3115 break;
7a3148a9 3116#endif
ebc05488 3117#ifdef TARGET_NR_ftime
31e31b8a
FB
3118 case TARGET_NR_ftime:
3119 goto unimplemented;
ebc05488 3120#endif
31e31b8a 3121 case TARGET_NR_sync:
04369ff2
FB
3122 sync();
3123 ret = 0;
31e31b8a
FB
3124 break;
3125 case TARGET_NR_kill:
3126 ret = get_errno(kill(arg1, arg2));
3127 break;
3128 case TARGET_NR_rename:
53a5960a
PB
3129 {
3130 void *p2;
3131 p = lock_user_string(arg1);
3132 p2 = lock_user_string(arg2);
579a97f7
FB
3133 if (!p || !p2)
3134 ret = -TARGET_EFAULT;
3135 else
3136 ret = get_errno(rename(p, p2));
53a5960a
PB
3137 unlock_user(p2, arg2, 0);
3138 unlock_user(p, arg1, 0);
3139 }
31e31b8a 3140 break;
722183f6
TS
3141#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3142 case TARGET_NR_renameat:
722183f6 3143 {
579a97f7 3144 void *p2;
722183f6
TS
3145 p = lock_user_string(arg2);
3146 p2 = lock_user_string(arg4);
579a97f7 3147 if (!p || !p2)
0da46a6e 3148 ret = -TARGET_EFAULT;
722183f6
TS
3149 else
3150 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
579a97f7
FB
3151 unlock_user(p2, arg4, 0);
3152 unlock_user(p, arg2, 0);
722183f6
TS
3153 }
3154 break;
3155#endif
31e31b8a 3156 case TARGET_NR_mkdir:
579a97f7
FB
3157 if (!(p = lock_user_string(arg1)))
3158 goto efault;
53a5960a
PB
3159 ret = get_errno(mkdir(p, arg2));
3160 unlock_user(p, arg1, 0);
31e31b8a 3161 break;
4472ad0d
TS
3162#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3163 case TARGET_NR_mkdirat:
579a97f7
FB
3164 if (!(p = lock_user_string(arg2)))
3165 goto efault;
3166 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3167 unlock_user(p, arg2, 0);
4472ad0d
TS
3168 break;
3169#endif
31e31b8a 3170 case TARGET_NR_rmdir:
579a97f7
FB
3171 if (!(p = lock_user_string(arg1)))
3172 goto efault;
53a5960a
PB
3173 ret = get_errno(rmdir(p));
3174 unlock_user(p, arg1, 0);
31e31b8a
FB
3175 break;
3176 case TARGET_NR_dup:
3177 ret = get_errno(dup(arg1));
3178 break;
3179 case TARGET_NR_pipe:
3180 {
53a5960a
PB
3181 int host_pipe[2];
3182 ret = get_errno(pipe(host_pipe));
31e31b8a 3183 if (!is_error(ret)) {
c12ab05c 3184#if defined(TARGET_MIPS)
ead9360e
TS
3185 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3186 env->gpr[3][env->current_tc] = host_pipe[1];
c12ab05c
TS
3187 ret = host_pipe[0];
3188#else
53a5960a
PB
3189 tput32(arg1, host_pipe[0]);
3190 tput32(arg1 + 4, host_pipe[1]);
c12ab05c 3191#endif
31e31b8a
FB
3192 }
3193 }
3194 break;
3195 case TARGET_NR_times:
32f36bce 3196 {
53a5960a 3197 struct target_tms *tmsp;
32f36bce
FB
3198 struct tms tms;
3199 ret = get_errno(times(&tms));
53a5960a 3200 if (arg1) {
579a97f7
FB
3201 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3202 if (!tmsp)
3203 goto efault;
c596ed17
FB
3204 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3205 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3206 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3207 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
32f36bce 3208 }
c596ed17
FB
3209 if (!is_error(ret))
3210 ret = host_to_target_clock_t(ret);
32f36bce
FB
3211 }
3212 break;
ebc05488 3213#ifdef TARGET_NR_prof
31e31b8a
FB
3214 case TARGET_NR_prof:
3215 goto unimplemented;
ebc05488 3216#endif
e5febef5 3217#ifdef TARGET_NR_signal
31e31b8a
FB
3218 case TARGET_NR_signal:
3219 goto unimplemented;
e5febef5 3220#endif
31e31b8a 3221 case TARGET_NR_acct:
579a97f7
FB
3222 if (!(p = lock_user_string(arg1)))
3223 goto efault;
24836689
PB
3224 ret = get_errno(acct(path(p)));
3225 unlock_user(p, arg1, 0);
3226 break;
7a3148a9 3227#ifdef TARGET_NR_umount2 /* not on alpha */
31e31b8a 3228 case TARGET_NR_umount2:
579a97f7
FB
3229 if (!(p = lock_user_string(arg1)))
3230 goto efault;
53a5960a
PB
3231 ret = get_errno(umount2(p, arg2));
3232 unlock_user(p, arg1, 0);
31e31b8a 3233 break;
7a3148a9 3234#endif
ebc05488 3235#ifdef TARGET_NR_lock
31e31b8a
FB
3236 case TARGET_NR_lock:
3237 goto unimplemented;
ebc05488 3238#endif
31e31b8a
FB
3239 case TARGET_NR_ioctl:
3240 ret = do_ioctl(arg1, arg2, arg3);
3241 break;
3242 case TARGET_NR_fcntl:
9ee1fa2c 3243 ret = do_fcntl(arg1, arg2, arg3);
31e31b8a 3244 break;
ebc05488 3245#ifdef TARGET_NR_mpx
31e31b8a
FB
3246 case TARGET_NR_mpx:
3247 goto unimplemented;
ebc05488 3248#endif
31e31b8a
FB
3249 case TARGET_NR_setpgid:
3250 ret = get_errno(setpgid(arg1, arg2));
3251 break;
ebc05488 3252#ifdef TARGET_NR_ulimit
31e31b8a
FB
3253 case TARGET_NR_ulimit:
3254 goto unimplemented;
ebc05488
FB
3255#endif
3256#ifdef TARGET_NR_oldolduname
31e31b8a
FB
3257 case TARGET_NR_oldolduname:
3258 goto unimplemented;
ebc05488 3259#endif
31e31b8a
FB
3260 case TARGET_NR_umask:
3261 ret = get_errno(umask(arg1));
3262 break;
3263 case TARGET_NR_chroot:
579a97f7
FB
3264 if (!(p = lock_user_string(arg1)))
3265 goto efault;
53a5960a
PB
3266 ret = get_errno(chroot(p));
3267 unlock_user(p, arg1, 0);
31e31b8a
FB
3268 break;
3269 case TARGET_NR_ustat:
3270 goto unimplemented;
3271 case TARGET_NR_dup2:
3272 ret = get_errno(dup2(arg1, arg2));
3273 break;
7a3148a9 3274#ifdef TARGET_NR_getppid /* not on alpha */
31e31b8a
FB
3275 case TARGET_NR_getppid:
3276 ret = get_errno(getppid());
3277 break;
7a3148a9 3278#endif
31e31b8a
FB
3279 case TARGET_NR_getpgrp:
3280 ret = get_errno(getpgrp());
3281 break;
3282 case TARGET_NR_setsid:
3283 ret = get_errno(setsid());
3284 break;
e5febef5 3285#ifdef TARGET_NR_sigaction
31e31b8a 3286 case TARGET_NR_sigaction:
31e31b8a 3287 {
388bb21a 3288#if !defined(TARGET_MIPS)
53a5960a 3289 struct target_old_sigaction *old_act;
66fb9763 3290 struct target_sigaction act, oact, *pact;
53a5960a 3291 if (arg2) {
579a97f7
FB
3292 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3293 goto efault;
66fb9763
FB
3294 act._sa_handler = old_act->_sa_handler;
3295 target_siginitset(&act.sa_mask, old_act->sa_mask);
3296 act.sa_flags = old_act->sa_flags;
3297 act.sa_restorer = old_act->sa_restorer;
53a5960a 3298 unlock_user_struct(old_act, arg2, 0);
66fb9763
FB
3299 pact = &act;
3300 } else {
3301 pact = NULL;
3302 }
3303 ret = get_errno(do_sigaction(arg1, pact, &oact));
53a5960a 3304 if (!is_error(ret) && arg3) {
579a97f7
FB
3305 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3306 goto efault;
53a5960a
PB
3307 old_act->_sa_handler = oact._sa_handler;
3308 old_act->sa_mask = oact.sa_mask.sig[0];
3309 old_act->sa_flags = oact.sa_flags;
3310 old_act->sa_restorer = oact.sa_restorer;
3311 unlock_user_struct(old_act, arg3, 1);
66fb9763 3312 }
388bb21a 3313#else
106ec879
FB
3314 struct target_sigaction act, oact, *pact, *old_act;
3315
3316 if (arg2) {
579a97f7
FB
3317 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3318 goto efault;
106ec879
FB
3319 act._sa_handler = old_act->_sa_handler;
3320 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3321 act.sa_flags = old_act->sa_flags;
3322 unlock_user_struct(old_act, arg2, 0);
3323 pact = &act;
3324 } else {
3325 pact = NULL;
3326 }
3327
3328 ret = get_errno(do_sigaction(arg1, pact, &oact));
3329
3330 if (!is_error(ret) && arg3) {
579a97f7
FB
3331 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3332 goto efault;
106ec879
FB
3333 old_act->_sa_handler = oact._sa_handler;
3334 old_act->sa_flags = oact.sa_flags;
3335 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3336 old_act->sa_mask.sig[1] = 0;
3337 old_act->sa_mask.sig[2] = 0;
3338 old_act->sa_mask.sig[3] = 0;
3339 unlock_user_struct(old_act, arg3, 1);
3340 }
388bb21a 3341#endif
31e31b8a
FB
3342 }
3343 break;
e5febef5 3344#endif
66fb9763 3345 case TARGET_NR_rt_sigaction:
53a5960a
PB
3346 {
3347 struct target_sigaction *act;
3348 struct target_sigaction *oact;
3349
579a97f7
FB
3350 if (arg2) {
3351 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3352 goto efault;
3353 } else
53a5960a 3354 act = NULL;
579a97f7
FB
3355 if (arg3) {
3356 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3357 ret = -TARGET_EFAULT;
3358 goto rt_sigaction_fail;
3359 }
3360 } else
53a5960a
PB
3361 oact = NULL;
3362 ret = get_errno(do_sigaction(arg1, act, oact));
579a97f7
FB
3363 rt_sigaction_fail:
3364 if (act)
53a5960a 3365 unlock_user_struct(act, arg2, 0);
579a97f7 3366 if (oact)
53a5960a
PB
3367 unlock_user_struct(oact, arg3, 1);
3368 }
66fb9763 3369 break;
7a3148a9 3370#ifdef TARGET_NR_sgetmask /* not on alpha */
31e31b8a 3371 case TARGET_NR_sgetmask:
66fb9763
FB
3372 {
3373 sigset_t cur_set;
992f48a0 3374 abi_ulong target_set;
66fb9763
FB
3375 sigprocmask(0, NULL, &cur_set);
3376 host_to_target_old_sigset(&target_set, &cur_set);
3377 ret = target_set;
3378 }
3379 break;
7a3148a9
JM
3380#endif
3381#ifdef TARGET_NR_ssetmask /* not on alpha */
31e31b8a 3382 case TARGET_NR_ssetmask:
66fb9763
FB
3383 {
3384 sigset_t set, oset, cur_set;
992f48a0 3385 abi_ulong target_set = arg1;
66fb9763
FB
3386 sigprocmask(0, NULL, &cur_set);
3387 target_to_host_old_sigset(&set, &target_set);
3388 sigorset(&set, &set, &cur_set);
3389 sigprocmask(SIG_SETMASK, &set, &oset);
3390 host_to_target_old_sigset(&target_set, &oset);
3391 ret = target_set;
3392 }
3393 break;
7a3148a9 3394#endif
e5febef5 3395#ifdef TARGET_NR_sigprocmask
66fb9763
FB
3396 case TARGET_NR_sigprocmask:
3397 {
3398 int how = arg1;
3399 sigset_t set, oldset, *set_ptr;
3b46e624 3400
53a5960a 3401 if (arg2) {
66fb9763
FB
3402 switch(how) {
3403 case TARGET_SIG_BLOCK:
3404 how = SIG_BLOCK;
3405 break;
3406 case TARGET_SIG_UNBLOCK:
3407 how = SIG_UNBLOCK;
3408 break;
3409 case TARGET_SIG_SETMASK:
3410 how = SIG_SETMASK;
3411 break;
3412 default:
0da46a6e 3413 ret = -TARGET_EINVAL;
66fb9763
FB
3414 goto fail;
3415 }
579a97f7
FB
3416 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3417 goto efault;
53a5960a
PB
3418 target_to_host_old_sigset(&set, p);
3419 unlock_user(p, arg2, 0);
66fb9763
FB
3420 set_ptr = &set;
3421 } else {
3422 how = 0;
3423 set_ptr = NULL;
3424 }
3425 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
53a5960a 3426 if (!is_error(ret) && arg3) {
579a97f7
FB
3427 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3428 goto efault;
53a5960a
PB
3429 host_to_target_old_sigset(p, &oldset);
3430 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763
FB
3431 }
3432 }
3433 break;
e5febef5 3434#endif
66fb9763
FB
3435 case TARGET_NR_rt_sigprocmask:
3436 {
3437 int how = arg1;
3438 sigset_t set, oldset, *set_ptr;
3b46e624 3439
53a5960a 3440 if (arg2) {
66fb9763
FB
3441 switch(how) {
3442 case TARGET_SIG_BLOCK:
3443 how = SIG_BLOCK;
3444 break;
3445 case TARGET_SIG_UNBLOCK:
3446 how = SIG_UNBLOCK;
3447 break;
3448 case TARGET_SIG_SETMASK:
3449 how = SIG_SETMASK;
3450 break;
3451 default:
0da46a6e 3452 ret = -TARGET_EINVAL;
66fb9763
FB
3453 goto fail;
3454 }
579a97f7
FB
3455 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3456 goto efault;
53a5960a
PB
3457 target_to_host_sigset(&set, p);
3458 unlock_user(p, arg2, 0);
66fb9763
FB
3459 set_ptr = &set;
3460 } else {
3461 how = 0;
3462 set_ptr = NULL;
3463 }
3464 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
53a5960a 3465 if (!is_error(ret) && arg3) {
579a97f7
FB
3466 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3467 goto efault;
53a5960a
PB
3468 host_to_target_sigset(p, &oldset);
3469 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763
FB
3470 }
3471 }
3472 break;
e5febef5 3473#ifdef TARGET_NR_sigpending
66fb9763
FB
3474 case TARGET_NR_sigpending:
3475 {
3476 sigset_t set;
3477 ret = get_errno(sigpending(&set));
3478 if (!is_error(ret)) {
579a97f7
FB
3479 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3480 goto efault;
53a5960a
PB
3481 host_to_target_old_sigset(p, &set);
3482 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
3483 }
3484 }
3485 break;
e5febef5 3486#endif
66fb9763
FB
3487 case TARGET_NR_rt_sigpending:
3488 {
3489 sigset_t set;
3490 ret = get_errno(sigpending(&set));
3491 if (!is_error(ret)) {
579a97f7
FB
3492 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3493 goto efault;
53a5960a
PB
3494 host_to_target_sigset(p, &set);
3495 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
3496 }
3497 }
3498 break;
e5febef5 3499#ifdef TARGET_NR_sigsuspend
66fb9763
FB
3500 case TARGET_NR_sigsuspend:
3501 {
3502 sigset_t set;
579a97f7
FB
3503 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3504 goto efault;
53a5960a
PB
3505 target_to_host_old_sigset(&set, p);
3506 unlock_user(p, arg1, 0);
66fb9763
FB
3507 ret = get_errno(sigsuspend(&set));
3508 }
3509 break;
e5febef5 3510#endif
66fb9763
FB
3511 case TARGET_NR_rt_sigsuspend:
3512 {
3513 sigset_t set;
579a97f7
FB
3514 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3515 goto efault;
53a5960a
PB
3516 target_to_host_sigset(&set, p);
3517 unlock_user(p, arg1, 0);
66fb9763
FB
3518 ret = get_errno(sigsuspend(&set));
3519 }
3520 break;
3521 case TARGET_NR_rt_sigtimedwait:
3522 {
66fb9763
FB
3523 sigset_t set;
3524 struct timespec uts, *puts;
3525 siginfo_t uinfo;
3b46e624 3526
579a97f7
FB
3527 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3528 goto efault;
53a5960a
PB
3529 target_to_host_sigset(&set, p);
3530 unlock_user(p, arg1, 0);
3531 if (arg3) {
66fb9763 3532 puts = &uts;
53a5960a 3533 target_to_host_timespec(puts, arg3);
66fb9763
FB
3534 } else {
3535 puts = NULL;
3536 }
3537 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
53a5960a 3538 if (!is_error(ret) && arg2) {
579a97f7
FB
3539 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3540 goto efault;
53a5960a
PB
3541 host_to_target_siginfo(p, &uinfo);
3542 unlock_user(p, arg2, sizeof(target_sigset_t));
66fb9763
FB
3543 }
3544 }
3545 break;
3546 case TARGET_NR_rt_sigqueueinfo:
3547 {
3548 siginfo_t uinfo;
579a97f7
FB
3549 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3550 goto efault;
53a5960a
PB
3551 target_to_host_siginfo(&uinfo, p);
3552 unlock_user(p, arg1, 0);
66fb9763
FB
3553 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3554 }
3555 break;
e5febef5 3556#ifdef TARGET_NR_sigreturn
66fb9763
FB
3557 case TARGET_NR_sigreturn:
3558 /* NOTE: ret is eax, so not transcoding must be done */
3559 ret = do_sigreturn(cpu_env);
3560 break;
e5febef5 3561#endif
66fb9763
FB
3562 case TARGET_NR_rt_sigreturn:
3563 /* NOTE: ret is eax, so not transcoding must be done */
3564 ret = do_rt_sigreturn(cpu_env);
3565 break;
31e31b8a 3566 case TARGET_NR_sethostname:
579a97f7
FB
3567 if (!(p = lock_user_string(arg1)))
3568 goto efault;
53a5960a
PB
3569 ret = get_errno(sethostname(p, arg2));
3570 unlock_user(p, arg1, 0);
31e31b8a
FB
3571 break;
3572 case TARGET_NR_setrlimit:
9de5e440
FB
3573 {
3574 /* XXX: convert resource ? */
3575 int resource = arg1;
53a5960a 3576 struct target_rlimit *target_rlim;
9de5e440 3577 struct rlimit rlim;
579a97f7
FB
3578 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3579 goto efault;
9de5e440
FB
3580 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3581 rlim.rlim_max = tswapl(target_rlim->rlim_max);
53a5960a 3582 unlock_user_struct(target_rlim, arg2, 0);
9de5e440
FB
3583 ret = get_errno(setrlimit(resource, &rlim));
3584 }
3585 break;
31e31b8a 3586 case TARGET_NR_getrlimit:
9de5e440
FB
3587 {
3588 /* XXX: convert resource ? */
3589 int resource = arg1;
53a5960a 3590 struct target_rlimit *target_rlim;
9de5e440 3591 struct rlimit rlim;
3b46e624 3592
9de5e440
FB
3593 ret = get_errno(getrlimit(resource, &rlim));
3594 if (!is_error(ret)) {
579a97f7
FB
3595 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3596 goto efault;
53a5960a
PB
3597 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3598 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3599 unlock_user_struct(target_rlim, arg2, 1);
9de5e440
FB
3600 }
3601 }
3602 break;
31e31b8a 3603 case TARGET_NR_getrusage:
b409186b
FB
3604 {
3605 struct rusage rusage;
b409186b
FB
3606 ret = get_errno(getrusage(arg1, &rusage));
3607 if (!is_error(ret)) {
53a5960a 3608 host_to_target_rusage(arg2, &rusage);
b409186b
FB
3609 }
3610 }
3611 break;
31e31b8a
FB
3612 case TARGET_NR_gettimeofday:
3613 {
31e31b8a
FB
3614 struct timeval tv;
3615 ret = get_errno(gettimeofday(&tv, NULL));
3616 if (!is_error(ret)) {
53a5960a 3617 host_to_target_timeval(arg1, &tv);
31e31b8a
FB
3618 }
3619 }
3620 break;
3621 case TARGET_NR_settimeofday:
3622 {
31e31b8a 3623 struct timeval tv;
53a5960a 3624 target_to_host_timeval(&tv, arg1);
31e31b8a
FB
3625 ret = get_errno(settimeofday(&tv, NULL));
3626 }
3627 break;
048f6b4d 3628#ifdef TARGET_NR_select
31e31b8a 3629 case TARGET_NR_select:
f2674e31 3630 {
53a5960a 3631 struct target_sel_arg_struct *sel;
992f48a0 3632 abi_ulong inp, outp, exp, tvp;
53a5960a
PB
3633 long nsel;
3634
579a97f7
FB
3635 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3636 goto efault;
53a5960a
PB
3637 nsel = tswapl(sel->n);
3638 inp = tswapl(sel->inp);
3639 outp = tswapl(sel->outp);
3640 exp = tswapl(sel->exp);
3641 tvp = tswapl(sel->tvp);
3642 unlock_user_struct(sel, arg1, 0);
3643 ret = do_select(nsel, inp, outp, exp, tvp);
f2674e31
FB
3644 }
3645 break;
048f6b4d 3646#endif
31e31b8a 3647 case TARGET_NR_symlink:
53a5960a
PB
3648 {
3649 void *p2;
3650 p = lock_user_string(arg1);
3651 p2 = lock_user_string(arg2);
579a97f7
FB
3652 if (!p || !p2)
3653 ret = -TARGET_EFAULT;
3654 else
3655 ret = get_errno(symlink(p, p2));
53a5960a
PB
3656 unlock_user(p2, arg2, 0);
3657 unlock_user(p, arg1, 0);
3658 }
31e31b8a 3659 break;
f0b6243d
TS
3660#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3661 case TARGET_NR_symlinkat:
f0b6243d 3662 {
579a97f7 3663 void *p2;
f0b6243d
TS
3664 p = lock_user_string(arg1);
3665 p2 = lock_user_string(arg3);
579a97f7 3666 if (!p || !p2)
0da46a6e 3667 ret = -TARGET_EFAULT;
f0b6243d
TS
3668 else
3669 ret = get_errno(sys_symlinkat(p, arg2, p2));
579a97f7
FB
3670 unlock_user(p2, arg3, 0);
3671 unlock_user(p, arg1, 0);
f0b6243d
TS
3672 }
3673 break;
3674#endif
ebc05488 3675#ifdef TARGET_NR_oldlstat
31e31b8a
FB
3676 case TARGET_NR_oldlstat:
3677 goto unimplemented;
ebc05488 3678#endif
31e31b8a 3679 case TARGET_NR_readlink:
53a5960a
PB
3680 {
3681 void *p2;
3682 p = lock_user_string(arg1);
579a97f7
FB
3683 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
3684 if (!p || !p2)
3685 ret = -TARGET_EFAULT;
3686 else
3687 ret = get_errno(readlink(path(p), p2, arg3));
53a5960a
PB
3688 unlock_user(p2, arg2, ret);
3689 unlock_user(p, arg1, 0);
3690 }
31e31b8a 3691 break;
5e0ccb18
TS
3692#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3693 case TARGET_NR_readlinkat:
5e0ccb18 3694 {
579a97f7 3695 void *p2;
5e0ccb18 3696 p = lock_user_string(arg2);
579a97f7
FB
3697 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
3698 if (!p || !p2)
0da46a6e 3699 ret = -TARGET_EFAULT;
5e0ccb18
TS
3700 else
3701 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
579a97f7
FB
3702 unlock_user(p2, arg3, ret);
3703 unlock_user(p, arg2, 0);
5e0ccb18
TS
3704 }
3705 break;
3706#endif
e5febef5 3707#ifdef TARGET_NR_uselib
31e31b8a
FB
3708 case TARGET_NR_uselib:
3709 goto unimplemented;
e5febef5
TS
3710#endif
3711#ifdef TARGET_NR_swapon
31e31b8a 3712 case TARGET_NR_swapon:
579a97f7
FB
3713 if (!(p = lock_user_string(arg1)))
3714 goto efault;
53a5960a
PB
3715 ret = get_errno(swapon(p, arg2));
3716 unlock_user(p, arg1, 0);
31e31b8a 3717 break;
e5febef5 3718#endif
31e31b8a
FB
3719 case TARGET_NR_reboot:
3720 goto unimplemented;
e5febef5 3721#ifdef TARGET_NR_readdir
31e31b8a
FB
3722 case TARGET_NR_readdir:
3723 goto unimplemented;
e5febef5
TS
3724#endif
3725#ifdef TARGET_NR_mmap
31e31b8a 3726 case TARGET_NR_mmap:
48733d19 3727#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
31e31b8a 3728 {
992f48a0
BS
3729 abi_ulong *v;
3730 abi_ulong v1, v2, v3, v4, v5, v6;
579a97f7
FB
3731 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
3732 goto efault;
53a5960a
PB
3733 v1 = tswapl(v[0]);
3734 v2 = tswapl(v[1]);
3735 v3 = tswapl(v[2]);
3736 v4 = tswapl(v[3]);
3737 v5 = tswapl(v[4]);
3738 v6 = tswapl(v[5]);
3739 unlock_user(v, arg1, 0);
5fafdf24 3740 ret = get_errno(target_mmap(v1, v2, v3,
5286db75
FB
3741 target_to_host_bitmask(v4, mmap_flags_tbl),
3742 v5, v6));
31e31b8a 3743 }
31e31b8a 3744#else
5fafdf24
TS
3745 ret = get_errno(target_mmap(arg1, arg2, arg3,
3746 target_to_host_bitmask(arg4, mmap_flags_tbl),
6fb883e8
FB
3747 arg5,
3748 arg6));
31e31b8a 3749#endif
6fb883e8 3750 break;
e5febef5 3751#endif
a315a145 3752#ifdef TARGET_NR_mmap2
6fb883e8 3753 case TARGET_NR_mmap2:
0d3267a7 3754#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
c573ff67
FB
3755#define MMAP_SHIFT 12
3756#else
3757#define MMAP_SHIFT TARGET_PAGE_BITS
3758#endif
5fafdf24
TS
3759 ret = get_errno(target_mmap(arg1, arg2, arg3,
3760 target_to_host_bitmask(arg4, mmap_flags_tbl),
5286db75 3761 arg5,
c573ff67 3762 arg6 << MMAP_SHIFT));
31e31b8a 3763 break;
a315a145 3764#endif
31e31b8a 3765 case TARGET_NR_munmap:
54936004 3766 ret = get_errno(target_munmap(arg1, arg2));
31e31b8a 3767 break;
9de5e440 3768 case TARGET_NR_mprotect:
54936004 3769 ret = get_errno(target_mprotect(arg1, arg2, arg3));
9de5e440 3770 break;
e5febef5 3771#ifdef TARGET_NR_mremap
9de5e440 3772 case TARGET_NR_mremap:
54936004 3773 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
9de5e440 3774 break;
e5febef5 3775#endif
53a5960a 3776 /* ??? msync/mlock/munlock are broken for softmmu. */
e5febef5 3777#ifdef TARGET_NR_msync
9de5e440 3778 case TARGET_NR_msync:
53a5960a 3779 ret = get_errno(msync(g2h(arg1), arg2, arg3));
9de5e440 3780 break;
e5febef5
TS
3781#endif
3782#ifdef TARGET_NR_mlock
9de5e440 3783 case TARGET_NR_mlock:
53a5960a 3784 ret = get_errno(mlock(g2h(arg1), arg2));
9de5e440 3785 break;
e5febef5
TS
3786#endif
3787#ifdef TARGET_NR_munlock
9de5e440 3788 case TARGET_NR_munlock:
53a5960a 3789 ret = get_errno(munlock(g2h(arg1), arg2));
9de5e440 3790 break;
e5febef5
TS
3791#endif
3792#ifdef TARGET_NR_mlockall
9de5e440
FB
3793 case TARGET_NR_mlockall:
3794 ret = get_errno(mlockall(arg1));
3795 break;
e5febef5
TS
3796#endif
3797#ifdef TARGET_NR_munlockall
9de5e440
FB
3798 case TARGET_NR_munlockall:
3799 ret = get_errno(munlockall());
3800 break;
e5febef5 3801#endif
31e31b8a 3802 case TARGET_NR_truncate:
579a97f7
FB
3803 if (!(p = lock_user_string(arg1)))
3804 goto efault;
53a5960a
PB
3805 ret = get_errno(truncate(p, arg2));
3806 unlock_user(p, arg1, 0);
31e31b8a
FB
3807 break;
3808 case TARGET_NR_ftruncate:
3809 ret = get_errno(ftruncate(arg1, arg2));
3810 break;
3811 case TARGET_NR_fchmod:
3812 ret = get_errno(fchmod(arg1, arg2));
3813 break;
814d7977
TS
3814#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3815 case TARGET_NR_fchmodat:
579a97f7
FB
3816 if (!(p = lock_user_string(arg2)))
3817 goto efault;
3818 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3819 unlock_user(p, arg2, 0);
814d7977
TS
3820 break;
3821#endif
31e31b8a 3822 case TARGET_NR_getpriority:
c6cda17a
TS
3823 /* libc does special remapping of the return value of
3824 * sys_getpriority() so it's just easiest to call
3825 * sys_getpriority() directly rather than through libc. */
3826 ret = sys_getpriority(arg1, arg2);
31e31b8a
FB
3827 break;
3828 case TARGET_NR_setpriority:
3829 ret = get_errno(setpriority(arg1, arg2, arg3));
3830 break;
ebc05488 3831#ifdef TARGET_NR_profil
31e31b8a
FB
3832 case TARGET_NR_profil:
3833 goto unimplemented;
ebc05488 3834#endif
31e31b8a 3835 case TARGET_NR_statfs:
579a97f7
FB
3836 if (!(p = lock_user_string(arg1)))
3837 goto efault;
53a5960a
PB
3838 ret = get_errno(statfs(path(p), &stfs));
3839 unlock_user(p, arg1, 0);
31e31b8a
FB
3840 convert_statfs:
3841 if (!is_error(ret)) {
53a5960a 3842 struct target_statfs *target_stfs;
3b46e624 3843
579a97f7
FB
3844 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
3845 goto efault;
3846 __put_user(stfs.f_type, &target_stfs->f_type);
3847 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3848 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3849 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3850 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3851 __put_user(stfs.f_files, &target_stfs->f_files);
3852 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3853 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3854 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3855 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
53a5960a 3856 unlock_user_struct(target_stfs, arg2, 1);
31e31b8a
FB
3857 }
3858 break;
3859 case TARGET_NR_fstatfs:
56c8f68f 3860 ret = get_errno(fstatfs(arg1, &stfs));
31e31b8a 3861 goto convert_statfs;
56c8f68f
FB
3862#ifdef TARGET_NR_statfs64
3863 case TARGET_NR_statfs64:
579a97f7
FB
3864 if (!(p = lock_user_string(arg1)))
3865 goto efault;
53a5960a
PB
3866 ret = get_errno(statfs(path(p), &stfs));
3867 unlock_user(p, arg1, 0);
56c8f68f
FB
3868 convert_statfs64:
3869 if (!is_error(ret)) {
53a5960a 3870 struct target_statfs64 *target_stfs;
3b46e624 3871
579a97f7
FB
3872 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
3873 goto efault;
3874 __put_user(stfs.f_type, &target_stfs->f_type);
3875 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3876 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3877 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3878 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3879 __put_user(stfs.f_files, &target_stfs->f_files);
3880 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3881 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3882 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3883 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
3884 unlock_user_struct(target_stfs, arg3, 1);
56c8f68f
FB
3885 }
3886 break;
3887 case TARGET_NR_fstatfs64:
3888 ret = get_errno(fstatfs(arg1, &stfs));
3889 goto convert_statfs64;
3890#endif
ebc05488 3891#ifdef TARGET_NR_ioperm
31e31b8a
FB
3892 case TARGET_NR_ioperm:
3893 goto unimplemented;
ebc05488 3894#endif
e5febef5 3895#ifdef TARGET_NR_socketcall
31e31b8a 3896 case TARGET_NR_socketcall:
53a5960a 3897 ret = do_socketcall(arg1, arg2);
31e31b8a 3898 break;
e5febef5 3899#endif
3532fa74
FB
3900#ifdef TARGET_NR_accept
3901 case TARGET_NR_accept:
1be9e1dc 3902 ret = do_accept(arg1, arg2, arg3);
3532fa74
FB
3903 break;
3904#endif
3905#ifdef TARGET_NR_bind
3906 case TARGET_NR_bind:
3907 ret = do_bind(arg1, arg2, arg3);
3908 break;
3909#endif
3910#ifdef TARGET_NR_connect
3911 case TARGET_NR_connect:
3912 ret = do_connect(arg1, arg2, arg3);
3913 break;
3914#endif
3915#ifdef TARGET_NR_getpeername
3916 case TARGET_NR_getpeername:
1be9e1dc 3917 ret = do_getpeername(arg1, arg2, arg3);
3532fa74
FB
3918 break;
3919#endif
3920#ifdef TARGET_NR_getsockname
3921 case TARGET_NR_getsockname:
1be9e1dc 3922 ret = do_getsockname(arg1, arg2, arg3);
3532fa74
FB
3923 break;
3924#endif
3925#ifdef TARGET_NR_getsockopt
3926 case TARGET_NR_getsockopt:
3927 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3928 break;
3929#endif
3930#ifdef TARGET_NR_listen
3931 case TARGET_NR_listen:
1be9e1dc 3932 ret = get_errno(listen(arg1, arg2));
3532fa74
FB
3933 break;
3934#endif
3935#ifdef TARGET_NR_recv
3936 case TARGET_NR_recv:
214201bd 3937 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
3938 break;
3939#endif
3940#ifdef TARGET_NR_recvfrom
3941 case TARGET_NR_recvfrom:
214201bd 3942 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
3943 break;
3944#endif
3945#ifdef TARGET_NR_recvmsg
3946 case TARGET_NR_recvmsg:
3947 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3948 break;
3949#endif
3950#ifdef TARGET_NR_send
3951 case TARGET_NR_send:
1be9e1dc 3952 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
3953 break;
3954#endif
3955#ifdef TARGET_NR_sendmsg
3956 case TARGET_NR_sendmsg:
3957 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3958 break;
3959#endif
3960#ifdef TARGET_NR_sendto
3961 case TARGET_NR_sendto:
1be9e1dc 3962 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
3963 break;
3964#endif
3965#ifdef TARGET_NR_shutdown
3966 case TARGET_NR_shutdown:
1be9e1dc 3967 ret = get_errno(shutdown(arg1, arg2));
3532fa74
FB
3968 break;
3969#endif
3970#ifdef TARGET_NR_socket
3971 case TARGET_NR_socket:
3972 ret = do_socket(arg1, arg2, arg3);
3973 break;
3974#endif
3975#ifdef TARGET_NR_socketpair
3976 case TARGET_NR_socketpair:
1be9e1dc 3977 ret = do_socketpair(arg1, arg2, arg3, arg4);
3532fa74
FB
3978 break;
3979#endif
3980#ifdef TARGET_NR_setsockopt
3981 case TARGET_NR_setsockopt:
3982 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3983 break;
3984#endif
7494b0f9 3985
31e31b8a 3986 case TARGET_NR_syslog:
579a97f7
FB
3987 if (!(p = lock_user_string(arg2)))
3988 goto efault;
e5574487
TS
3989 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3990 unlock_user(p, arg2, 0);
7494b0f9
TS
3991 break;
3992
31e31b8a 3993 case TARGET_NR_setitimer:
66fb9763 3994 {
66fb9763
FB
3995 struct itimerval value, ovalue, *pvalue;
3996
53a5960a 3997 if (arg2) {
66fb9763 3998 pvalue = &value;
5fafdf24 3999 target_to_host_timeval(&pvalue->it_interval,
53a5960a 4000 arg2);
5fafdf24 4001 target_to_host_timeval(&pvalue->it_value,
53a5960a 4002 arg2 + sizeof(struct target_timeval));
66fb9763
FB
4003 } else {
4004 pvalue = NULL;
4005 }
4006 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
53a5960a
PB
4007 if (!is_error(ret) && arg3) {
4008 host_to_target_timeval(arg3,
66fb9763 4009 &ovalue.it_interval);
53a5960a 4010 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
66fb9763
FB
4011 &ovalue.it_value);
4012 }
4013 }
4014 break;
31e31b8a 4015 case TARGET_NR_getitimer:
66fb9763 4016 {
66fb9763 4017 struct itimerval value;
3b46e624 4018
66fb9763 4019 ret = get_errno(getitimer(arg1, &value));
53a5960a
PB
4020 if (!is_error(ret) && arg2) {
4021 host_to_target_timeval(arg2,
66fb9763 4022 &value.it_interval);
53a5960a 4023 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
66fb9763
FB
4024 &value.it_value);
4025 }
4026 }
4027 break;
31e31b8a 4028 case TARGET_NR_stat:
579a97f7
FB
4029 if (!(p = lock_user_string(arg1)))
4030 goto efault;
53a5960a
PB
4031 ret = get_errno(stat(path(p), &st));
4032 unlock_user(p, arg1, 0);
31e31b8a
FB
4033 goto do_stat;
4034 case TARGET_NR_lstat:
579a97f7
FB
4035 if (!(p = lock_user_string(arg1)))
4036 goto efault;
53a5960a
PB
4037 ret = get_errno(lstat(path(p), &st));
4038 unlock_user(p, arg1, 0);
31e31b8a
FB
4039 goto do_stat;
4040 case TARGET_NR_fstat:
4041 {
4042 ret = get_errno(fstat(arg1, &st));
4043 do_stat:
4044 if (!is_error(ret)) {
53a5960a 4045 struct target_stat *target_st;
e3584658 4046
579a97f7
FB
4047 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4048 goto efault;
992f48a0 4049#if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32))
e3584658
TS
4050 target_st->st_dev = tswap32(st.st_dev);
4051#else
31e31b8a 4052 target_st->st_dev = tswap16(st.st_dev);
e3584658 4053#endif
31e31b8a 4054 target_st->st_ino = tswapl(st.st_ino);
7d600c80 4055#if defined(TARGET_PPC) || defined(TARGET_MIPS)
67867308
FB
4056 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
4057 target_st->st_uid = tswap32(st.st_uid);
4058 target_st->st_gid = tswap32(st.st_gid);
992f48a0 4059#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1b8dd648
BS
4060 target_st->st_mode = tswap32(st.st_mode);
4061 target_st->st_uid = tswap32(st.st_uid);
4062 target_st->st_gid = tswap32(st.st_gid);
67867308 4063#else
ec86b0fb 4064 target_st->st_mode = tswap16(st.st_mode);
31e31b8a
FB
4065 target_st->st_uid = tswap16(st.st_uid);
4066 target_st->st_gid = tswap16(st.st_gid);
67867308 4067#endif
e3584658
TS
4068#if defined(TARGET_MIPS)
4069 /* If this is the same on PPC, then just merge w/ the above ifdef */
4070 target_st->st_nlink = tswapl(st.st_nlink);
4071 target_st->st_rdev = tswapl(st.st_rdev);
992f48a0 4072#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1b8dd648
BS
4073 target_st->st_nlink = tswap32(st.st_nlink);
4074 target_st->st_rdev = tswap32(st.st_rdev);
e3584658 4075#else
67867308 4076 target_st->st_nlink = tswap16(st.st_nlink);
31e31b8a 4077 target_st->st_rdev = tswap16(st.st_rdev);
e3584658 4078#endif
31e31b8a
FB
4079 target_st->st_size = tswapl(st.st_size);
4080 target_st->st_blksize = tswapl(st.st_blksize);
4081 target_st->st_blocks = tswapl(st.st_blocks);
7854b056
FB
4082 target_st->target_st_atime = tswapl(st.st_atime);
4083 target_st->target_st_mtime = tswapl(st.st_mtime);
4084 target_st->target_st_ctime = tswapl(st.st_ctime);
53a5960a 4085 unlock_user_struct(target_st, arg2, 1);
31e31b8a
FB
4086 }
4087 }
4088 break;
ebc05488 4089#ifdef TARGET_NR_olduname
31e31b8a
FB
4090 case TARGET_NR_olduname:
4091 goto unimplemented;
ebc05488
FB
4092#endif
4093#ifdef TARGET_NR_iopl
31e31b8a
FB
4094 case TARGET_NR_iopl:
4095 goto unimplemented;
ebc05488 4096#endif
31e31b8a
FB
4097 case TARGET_NR_vhangup:
4098 ret = get_errno(vhangup());
4099 break;
ebc05488 4100#ifdef TARGET_NR_idle
31e31b8a
FB
4101 case TARGET_NR_idle:
4102 goto unimplemented;
42ad6ae9
FB
4103#endif
4104#ifdef TARGET_NR_syscall
4105 case TARGET_NR_syscall:
4106 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4107 break;
ebc05488 4108#endif
31e31b8a
FB
4109 case TARGET_NR_wait4:
4110 {
4111 int status;
992f48a0 4112 abi_long status_ptr = arg2;
31e31b8a 4113 struct rusage rusage, *rusage_ptr;
992f48a0 4114 abi_ulong target_rusage = arg4;
31e31b8a
FB
4115 if (target_rusage)
4116 rusage_ptr = &rusage;
4117 else
4118 rusage_ptr = NULL;
4119 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4120 if (!is_error(ret)) {
4121 if (status_ptr)
53a5960a 4122 tputl(status_ptr, status);
31e31b8a 4123 if (target_rusage) {
b409186b 4124 host_to_target_rusage(target_rusage, &rusage);
31e31b8a
FB
4125 }
4126 }
4127 }
4128 break;
e5febef5 4129#ifdef TARGET_NR_swapoff
31e31b8a 4130 case TARGET_NR_swapoff:
579a97f7
FB
4131 if (!(p = lock_user_string(arg1)))
4132 goto efault;
53a5960a
PB
4133 ret = get_errno(swapoff(p));
4134 unlock_user(p, arg1, 0);
31e31b8a 4135 break;
e5febef5 4136#endif
31e31b8a 4137 case TARGET_NR_sysinfo:
a5448a7d 4138 {
53a5960a 4139 struct target_sysinfo *target_value;
a5448a7d
FB
4140 struct sysinfo value;
4141 ret = get_errno(sysinfo(&value));
53a5960a 4142 if (!is_error(ret) && arg1)
a5448a7d 4143 {
579a97f7
FB
4144 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4145 goto efault;
a5448a7d
FB
4146 __put_user(value.uptime, &target_value->uptime);
4147 __put_user(value.loads[0], &target_value->loads[0]);
4148 __put_user(value.loads[1], &target_value->loads[1]);
4149 __put_user(value.loads[2], &target_value->loads[2]);
4150 __put_user(value.totalram, &target_value->totalram);
4151 __put_user(value.freeram, &target_value->freeram);
4152 __put_user(value.sharedram, &target_value->sharedram);
4153 __put_user(value.bufferram, &target_value->bufferram);
4154 __put_user(value.totalswap, &target_value->totalswap);
4155 __put_user(value.freeswap, &target_value->freeswap);
4156 __put_user(value.procs, &target_value->procs);
4157 __put_user(value.totalhigh, &target_value->totalhigh);
4158 __put_user(value.freehigh, &target_value->freehigh);
4159 __put_user(value.mem_unit, &target_value->mem_unit);
53a5960a 4160 unlock_user_struct(target_value, arg1, 1);
a5448a7d
FB
4161 }
4162 }
4163 break;
e5febef5 4164#ifdef TARGET_NR_ipc
31e31b8a 4165 case TARGET_NR_ipc:
8853f86e
FB
4166 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4167 break;
e5febef5 4168#endif
31e31b8a
FB
4169 case TARGET_NR_fsync:
4170 ret = get_errno(fsync(arg1));
4171 break;
31e31b8a 4172 case TARGET_NR_clone:
1b6b029e
FB
4173 ret = get_errno(do_fork(cpu_env, arg1, arg2));
4174 break;
ec86b0fb
FB
4175#ifdef __NR_exit_group
4176 /* new thread calls */
4177 case TARGET_NR_exit_group:
e9009676 4178 gdb_exit(cpu_env, arg1);
ec86b0fb
FB
4179 ret = get_errno(exit_group(arg1));
4180 break;
4181#endif
31e31b8a 4182 case TARGET_NR_setdomainname:
579a97f7
FB
4183 if (!(p = lock_user_string(arg1)))
4184 goto efault;
53a5960a
PB
4185 ret = get_errno(setdomainname(p, arg2));
4186 unlock_user(p, arg1, 0);
31e31b8a
FB
4187 break;
4188 case TARGET_NR_uname:
4189 /* no need to transcode because we use the linux syscall */
29e619b1
FB
4190 {
4191 struct new_utsname * buf;
3b46e624 4192
579a97f7
FB
4193 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4194 goto efault;
29e619b1
FB
4195 ret = get_errno(sys_uname(buf));
4196 if (!is_error(ret)) {
4197 /* Overrite the native machine name with whatever is being
4198 emulated. */
4199 strcpy (buf->machine, UNAME_MACHINE);
c5937220
PB
4200 /* Allow the user to override the reported release. */
4201 if (qemu_uname_release && *qemu_uname_release)
4202 strcpy (buf->release, qemu_uname_release);
29e619b1 4203 }
53a5960a 4204 unlock_user_struct(buf, arg1, 1);
29e619b1 4205 }
31e31b8a 4206 break;
6dbad63e 4207#ifdef TARGET_I386
31e31b8a 4208 case TARGET_NR_modify_ldt:
03acab66 4209 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
5cd4393b 4210 break;
84409ddb 4211#if !defined(TARGET_X86_64)
5cd4393b
FB
4212 case TARGET_NR_vm86old:
4213 goto unimplemented;
4214 case TARGET_NR_vm86:
53a5960a 4215 ret = do_vm86(cpu_env, arg1, arg2);
6dbad63e 4216 break;
84409ddb 4217#endif
6dbad63e 4218#endif
31e31b8a
FB
4219 case TARGET_NR_adjtimex:
4220 goto unimplemented;
e5febef5 4221#ifdef TARGET_NR_create_module
31e31b8a 4222 case TARGET_NR_create_module:
e5febef5 4223#endif
31e31b8a
FB
4224 case TARGET_NR_init_module:
4225 case TARGET_NR_delete_module:
e5febef5 4226#ifdef TARGET_NR_get_kernel_syms
31e31b8a 4227 case TARGET_NR_get_kernel_syms:
e5febef5 4228#endif
31e31b8a
FB
4229 goto unimplemented;
4230 case TARGET_NR_quotactl:
4231 goto unimplemented;
4232 case TARGET_NR_getpgid:
4233 ret = get_errno(getpgid(arg1));
4234 break;
4235 case TARGET_NR_fchdir:
4236 ret = get_errno(fchdir(arg1));
4237 break;
84409ddb 4238#ifdef TARGET_NR_bdflush /* not on x86_64 */
31e31b8a
FB
4239 case TARGET_NR_bdflush:
4240 goto unimplemented;
84409ddb 4241#endif
e5febef5 4242#ifdef TARGET_NR_sysfs
31e31b8a
FB
4243 case TARGET_NR_sysfs:
4244 goto unimplemented;
e5febef5 4245#endif
31e31b8a 4246 case TARGET_NR_personality:
1b6b029e 4247 ret = get_errno(personality(arg1));
31e31b8a 4248 break;
e5febef5 4249#ifdef TARGET_NR_afs_syscall
31e31b8a
FB
4250 case TARGET_NR_afs_syscall:
4251 goto unimplemented;
e5febef5 4252#endif
7a3148a9 4253#ifdef TARGET_NR__llseek /* Not on alpha */
31e31b8a
FB
4254 case TARGET_NR__llseek:
4255 {
4f2ac237
FB
4256#if defined (__x86_64__)
4257 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
53a5960a 4258 tput64(arg4, ret);
4f2ac237 4259#else
31e31b8a
FB
4260 int64_t res;
4261 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
53a5960a 4262 tput64(arg4, res);
4f2ac237 4263#endif
31e31b8a
FB
4264 }
4265 break;
7a3148a9 4266#endif
31e31b8a 4267 case TARGET_NR_getdents:
992f48a0 4268#if TARGET_ABI_BITS != 32
53a5960a 4269 goto unimplemented;
992f48a0 4270#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4add45b4 4271 {
53a5960a 4272 struct target_dirent *target_dirp;
4add45b4 4273 struct dirent *dirp;
992f48a0 4274 abi_long count = arg3;
4add45b4
FB
4275
4276 dirp = malloc(count);
0da46a6e 4277 if (!dirp) {
579a97f7 4278 ret = -TARGET_ENOMEM;
0da46a6e
TS
4279 goto fail;
4280 }
3b46e624 4281
4add45b4
FB
4282 ret = get_errno(sys_getdents(arg1, dirp, count));
4283 if (!is_error(ret)) {
4284 struct dirent *de;
4285 struct target_dirent *tde;
4286 int len = ret;
4287 int reclen, treclen;
4288 int count1, tnamelen;
4289
4290 count1 = 0;
4291 de = dirp;
579a97f7
FB
4292 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4293 goto efault;
4add45b4
FB
4294 tde = target_dirp;
4295 while (len > 0) {
4296 reclen = de->d_reclen;
992f48a0 4297 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4add45b4
FB
4298 tde->d_reclen = tswap16(treclen);
4299 tde->d_ino = tswapl(de->d_ino);
4300 tde->d_off = tswapl(de->d_off);
992f48a0 4301 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4add45b4
FB
4302 if (tnamelen > 256)
4303 tnamelen = 256;
80a9d035 4304 /* XXX: may not be correct */
4add45b4
FB
4305 strncpy(tde->d_name, de->d_name, tnamelen);
4306 de = (struct dirent *)((char *)de + reclen);
4307 len -= reclen;
1c5bf3bf 4308 tde = (struct target_dirent *)((char *)tde + treclen);
4add45b4
FB
4309 count1 += treclen;
4310 }
4311 ret = count1;
579a97f7 4312 unlock_user(target_dirp, arg2, ret);
4add45b4
FB
4313 }
4314 free(dirp);
4315 }
4316#else
31e31b8a 4317 {
53a5960a 4318 struct dirent *dirp;
992f48a0 4319 abi_long count = arg3;
dab2ed99 4320
579a97f7
FB
4321 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4322 goto efault;
72f03900 4323 ret = get_errno(sys_getdents(arg1, dirp, count));
31e31b8a
FB
4324 if (!is_error(ret)) {
4325 struct dirent *de;
4326 int len = ret;
4327 int reclen;
4328 de = dirp;
4329 while (len > 0) {
8083a3e5 4330 reclen = de->d_reclen;
31e31b8a
FB
4331 if (reclen > len)
4332 break;
8083a3e5 4333 de->d_reclen = tswap16(reclen);
31e31b8a
FB
4334 tswapls(&de->d_ino);
4335 tswapls(&de->d_off);
4336 de = (struct dirent *)((char *)de + reclen);
4337 len -= reclen;
4338 }
4339 }
53a5960a 4340 unlock_user(dirp, arg2, ret);
31e31b8a 4341 }
4add45b4 4342#endif
31e31b8a 4343 break;
3ae43202 4344#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
dab2ed99
FB
4345 case TARGET_NR_getdents64:
4346 {
53a5960a 4347 struct dirent64 *dirp;
992f48a0 4348 abi_long count = arg3;
579a97f7
FB
4349 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4350 goto efault;
dab2ed99
FB
4351 ret = get_errno(sys_getdents64(arg1, dirp, count));
4352 if (!is_error(ret)) {
4353 struct dirent64 *de;
4354 int len = ret;
4355 int reclen;
4356 de = dirp;
4357 while (len > 0) {
8083a3e5 4358 reclen = de->d_reclen;
dab2ed99
FB
4359 if (reclen > len)
4360 break;
8083a3e5 4361 de->d_reclen = tswap16(reclen);
8582a53a
FB
4362 tswap64s((uint64_t *)&de->d_ino);
4363 tswap64s((uint64_t *)&de->d_off);
dab2ed99
FB
4364 de = (struct dirent64 *)((char *)de + reclen);
4365 len -= reclen;
4366 }
4367 }
53a5960a 4368 unlock_user(dirp, arg2, ret);
dab2ed99
FB
4369 }
4370 break;
a541f297 4371#endif /* TARGET_NR_getdents64 */
e5febef5 4372#ifdef TARGET_NR__newselect
31e31b8a 4373 case TARGET_NR__newselect:
53a5960a 4374 ret = do_select(arg1, arg2, arg3, arg4, arg5);
31e31b8a 4375 break;
e5febef5
TS
4376#endif
4377#ifdef TARGET_NR_poll
9de5e440
FB
4378 case TARGET_NR_poll:
4379 {
53a5960a 4380 struct target_pollfd *target_pfd;
9de5e440
FB
4381 unsigned int nfds = arg2;
4382 int timeout = arg3;
4383 struct pollfd *pfd;
7854b056 4384 unsigned int i;
9de5e440 4385
579a97f7
FB
4386 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4387 if (!target_pfd)
4388 goto efault;
9de5e440
FB
4389 pfd = alloca(sizeof(struct pollfd) * nfds);
4390 for(i = 0; i < nfds; i++) {
5cd4393b
FB
4391 pfd[i].fd = tswap32(target_pfd[i].fd);
4392 pfd[i].events = tswap16(target_pfd[i].events);
9de5e440
FB
4393 }
4394 ret = get_errno(poll(pfd, nfds, timeout));
4395 if (!is_error(ret)) {
4396 for(i = 0; i < nfds; i++) {
5cd4393b 4397 target_pfd[i].revents = tswap16(pfd[i].revents);
9de5e440 4398 }
53a5960a
PB
4399 ret += nfds * (sizeof(struct target_pollfd)
4400 - sizeof(struct pollfd));
9de5e440 4401 }
53a5960a 4402 unlock_user(target_pfd, arg1, ret);
9de5e440
FB
4403 }
4404 break;
e5febef5 4405#endif
31e31b8a 4406 case TARGET_NR_flock:
9de5e440
FB
4407 /* NOTE: the flock constant seems to be the same for every
4408 Linux platform */
4409 ret = get_errno(flock(arg1, arg2));
31e31b8a
FB
4410 break;
4411 case TARGET_NR_readv:
4412 {
4413 int count = arg3;
31e31b8a 4414 struct iovec *vec;
31e31b8a
FB
4415
4416 vec = alloca(count * sizeof(struct iovec));
579a97f7 4417 lock_iovec(VERIFY_WRITE, vec, arg2, count, 0);
31e31b8a 4418 ret = get_errno(readv(arg1, vec, count));
53a5960a 4419 unlock_iovec(vec, arg2, count, 1);
31e31b8a
FB
4420 }
4421 break;
4422 case TARGET_NR_writev:
4423 {
4424 int count = arg3;
31e31b8a 4425 struct iovec *vec;
31e31b8a
FB
4426
4427 vec = alloca(count * sizeof(struct iovec));
579a97f7 4428 lock_iovec(VERIFY_READ, vec, arg2, count, 1);
31e31b8a 4429 ret = get_errno(writev(arg1, vec, count));
53a5960a 4430 unlock_iovec(vec, arg2, count, 0);
31e31b8a
FB
4431 }
4432 break;
4433 case TARGET_NR_getsid:
4434 ret = get_errno(getsid(arg1));
4435 break;
7a3148a9 4436#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
31e31b8a 4437 case TARGET_NR_fdatasync:
5cd4393b
FB
4438 ret = get_errno(fdatasync(arg1));
4439 break;
7a3148a9 4440#endif
31e31b8a 4441 case TARGET_NR__sysctl:
0da46a6e 4442 /* We don't implement this, but ENOTDIR is always a safe
29e619b1 4443 return value. */
0da46a6e
TS
4444 ret = -TARGET_ENOTDIR;
4445 break;
31e31b8a 4446 case TARGET_NR_sched_setparam:
5cd4393b 4447 {
53a5960a 4448 struct sched_param *target_schp;
5cd4393b 4449 struct sched_param schp;
53a5960a 4450
579a97f7
FB
4451 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4452 goto efault;
5cd4393b 4453 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 4454 unlock_user_struct(target_schp, arg2, 0);
5cd4393b
FB
4455 ret = get_errno(sched_setparam(arg1, &schp));
4456 }
4457 break;
31e31b8a 4458 case TARGET_NR_sched_getparam:
5cd4393b 4459 {
53a5960a 4460 struct sched_param *target_schp;
5cd4393b
FB
4461 struct sched_param schp;
4462 ret = get_errno(sched_getparam(arg1, &schp));
4463 if (!is_error(ret)) {
579a97f7
FB
4464 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4465 goto efault;
5cd4393b 4466 target_schp->sched_priority = tswap32(schp.sched_priority);
53a5960a 4467 unlock_user_struct(target_schp, arg2, 1);
5cd4393b
FB
4468 }
4469 }
4470 break;
31e31b8a 4471 case TARGET_NR_sched_setscheduler:
5cd4393b 4472 {
53a5960a 4473 struct sched_param *target_schp;
5cd4393b 4474 struct sched_param schp;
579a97f7
FB
4475 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4476 goto efault;
5cd4393b 4477 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 4478 unlock_user_struct(target_schp, arg3, 0);
5cd4393b
FB
4479 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4480 }
4481 break;
31e31b8a 4482 case TARGET_NR_sched_getscheduler:
5cd4393b
FB
4483 ret = get_errno(sched_getscheduler(arg1));
4484 break;
31e31b8a
FB
4485 case TARGET_NR_sched_yield:
4486 ret = get_errno(sched_yield());
4487 break;
4488 case TARGET_NR_sched_get_priority_max:
5cd4393b
FB
4489 ret = get_errno(sched_get_priority_max(arg1));
4490 break;
31e31b8a 4491 case TARGET_NR_sched_get_priority_min:
5cd4393b
FB
4492 ret = get_errno(sched_get_priority_min(arg1));
4493 break;
31e31b8a 4494 case TARGET_NR_sched_rr_get_interval:
5cd4393b 4495 {
5cd4393b
FB
4496 struct timespec ts;
4497 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4498 if (!is_error(ret)) {
53a5960a 4499 host_to_target_timespec(arg2, &ts);
5cd4393b
FB
4500 }
4501 }
4502 break;
31e31b8a 4503 case TARGET_NR_nanosleep:
1b6b029e 4504 {
1b6b029e 4505 struct timespec req, rem;
53a5960a 4506 target_to_host_timespec(&req, arg1);
1b6b029e 4507 ret = get_errno(nanosleep(&req, &rem));
53a5960a
PB
4508 if (is_error(ret) && arg2) {
4509 host_to_target_timespec(arg2, &rem);
1b6b029e
FB
4510 }
4511 }
4512 break;
e5febef5 4513#ifdef TARGET_NR_query_module
31e31b8a 4514 case TARGET_NR_query_module:
5cd4393b 4515 goto unimplemented;
e5febef5
TS
4516#endif
4517#ifdef TARGET_NR_nfsservctl
31e31b8a 4518 case TARGET_NR_nfsservctl:
5cd4393b 4519 goto unimplemented;
e5febef5 4520#endif
31e31b8a 4521 case TARGET_NR_prctl:
e5574487
TS
4522 switch (arg1)
4523 {
4524 case PR_GET_PDEATHSIG:
4525 {
4526 int deathsig;
4527 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4528 if (!is_error(ret) && arg2)
4529 tput32(arg2, deathsig);
4530 }
4531 break;
4532 default:
4533 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4534 break;
4535 }
39b9aae1 4536 break;
67867308 4537#ifdef TARGET_NR_pread
31e31b8a 4538 case TARGET_NR_pread:
579a97f7
FB
4539 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4540 goto efault;
53a5960a
PB
4541 ret = get_errno(pread(arg1, p, arg3, arg4));
4542 unlock_user(p, arg2, ret);
206f0fa7 4543 break;
31e31b8a 4544 case TARGET_NR_pwrite:
579a97f7
FB
4545 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4546 goto efault;
53a5960a
PB
4547 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4548 unlock_user(p, arg2, 0);
206f0fa7 4549 break;
67867308 4550#endif
31e31b8a 4551 case TARGET_NR_getcwd:
579a97f7
FB
4552 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4553 goto efault;
53a5960a
PB
4554 ret = get_errno(sys_getcwd1(p, arg2));
4555 unlock_user(p, arg1, ret);
31e31b8a
FB
4556 break;
4557 case TARGET_NR_capget:
5cd4393b 4558 goto unimplemented;
31e31b8a 4559 case TARGET_NR_capset:
5cd4393b 4560 goto unimplemented;
31e31b8a 4561 case TARGET_NR_sigaltstack:
198a74de
TS
4562#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4563 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
579a97f7 4564 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
a04e134a
TS
4565 break;
4566#else
5cd4393b 4567 goto unimplemented;
a04e134a 4568#endif
31e31b8a 4569 case TARGET_NR_sendfile:
5cd4393b 4570 goto unimplemented;
ebc05488 4571#ifdef TARGET_NR_getpmsg
31e31b8a 4572 case TARGET_NR_getpmsg:
5cd4393b 4573 goto unimplemented;
ebc05488
FB
4574#endif
4575#ifdef TARGET_NR_putpmsg
31e31b8a 4576 case TARGET_NR_putpmsg:
5cd4393b 4577 goto unimplemented;
ebc05488 4578#endif
048f6b4d 4579#ifdef TARGET_NR_vfork
31e31b8a 4580 case TARGET_NR_vfork:
1b6b029e 4581 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
31e31b8a 4582 break;
048f6b4d 4583#endif
ebc05488 4584#ifdef TARGET_NR_ugetrlimit
31e31b8a 4585 case TARGET_NR_ugetrlimit:
728584be
FB
4586 {
4587 struct rlimit rlim;
4588 ret = get_errno(getrlimit(arg1, &rlim));
4589 if (!is_error(ret)) {
53a5960a 4590 struct target_rlimit *target_rlim;
579a97f7
FB
4591 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4592 goto efault;
728584be
FB
4593 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4594 target_rlim->rlim_max = tswapl(rlim.rlim_max);
53a5960a 4595 unlock_user_struct(target_rlim, arg2, 1);
728584be
FB
4596 }
4597 break;
4598 }
ebc05488 4599#endif
a315a145 4600#ifdef TARGET_NR_truncate64
31e31b8a 4601 case TARGET_NR_truncate64:
579a97f7
FB
4602 if (!(p = lock_user_string(arg1)))
4603 goto efault;
53a5960a
PB
4604 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4605 unlock_user(p, arg1, 0);
667f38b1 4606 break;
a315a145
FB
4607#endif
4608#ifdef TARGET_NR_ftruncate64
31e31b8a 4609 case TARGET_NR_ftruncate64:
ce4defa0 4610 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
667f38b1 4611 break;
a315a145
FB
4612#endif
4613#ifdef TARGET_NR_stat64
31e31b8a 4614 case TARGET_NR_stat64:
579a97f7
FB
4615 if (!(p = lock_user_string(arg1)))
4616 goto efault;
53a5960a
PB
4617 ret = get_errno(stat(path(p), &st));
4618 unlock_user(p, arg1, 0);
60cd49d5 4619 goto do_stat64;
a315a145
FB
4620#endif
4621#ifdef TARGET_NR_lstat64
31e31b8a 4622 case TARGET_NR_lstat64:
579a97f7
FB
4623 if (!(p = lock_user_string(arg1)))
4624 goto efault;
53a5960a
PB
4625 ret = get_errno(lstat(path(p), &st));
4626 unlock_user(p, arg1, 0);
60cd49d5 4627 goto do_stat64;
a315a145
FB
4628#endif
4629#ifdef TARGET_NR_fstat64
31e31b8a 4630 case TARGET_NR_fstat64:
60cd49d5
FB
4631 {
4632 ret = get_errno(fstat(arg1, &st));
4633 do_stat64:
4634 if (!is_error(ret)) {
ce4defa0
PB
4635#ifdef TARGET_ARM
4636 if (((CPUARMState *)cpu_env)->eabi) {
53a5960a 4637 struct target_eabi_stat64 *target_st;
579a97f7
FB
4638
4639 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4640 goto efault;
ce4defa0 4641 memset(target_st, 0, sizeof(struct target_eabi_stat64));
579a97f7
FB
4642 __put_user(st.st_dev, &target_st->st_dev);
4643 __put_user(st.st_ino, &target_st->st_ino);
ce4defa0 4644#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
579a97f7
FB
4645 __put_user(st.st_ino, &target_st->__st_ino);
4646#endif
4647 __put_user(st.st_mode, &target_st->st_mode);
4648 __put_user(st.st_nlink, &target_st->st_nlink);
4649 __put_user(st.st_uid, &target_st->st_uid);
4650 __put_user(st.st_gid, &target_st->st_gid);
4651 __put_user(st.st_rdev, &target_st->st_rdev);
4652 __put_user(st.st_size, &target_st->st_size);
4653 __put_user(st.st_blksize, &target_st->st_blksize);
4654 __put_user(st.st_blocks, &target_st->st_blocks);
4655 __put_user(st.st_atime, &target_st->target_st_atime);
4656 __put_user(st.st_mtime, &target_st->target_st_mtime);
4657 __put_user(st.st_ctime, &target_st->target_st_ctime);
4658 unlock_user_struct(target_st, arg2, 1);
ce4defa0
PB
4659 } else
4660#endif
4661 {
53a5960a 4662 struct target_stat64 *target_st;
579a97f7
FB
4663
4664 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4665 goto efault;
ce4defa0 4666 memset(target_st, 0, sizeof(struct target_stat64));
579a97f7
FB
4667 __put_user(st.st_dev, &target_st->st_dev);
4668 __put_user(st.st_ino, &target_st->st_ino);
ec86b0fb 4669#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
579a97f7 4670 __put_user(st.st_ino, &target_st->__st_ino);
ce4defa0 4671#endif
579a97f7
FB
4672 __put_user(st.st_mode, &target_st->st_mode);
4673 __put_user(st.st_nlink, &target_st->st_nlink);
4674 __put_user(st.st_uid, &target_st->st_uid);
4675 __put_user(st.st_gid, &target_st->st_gid);
4676 __put_user(st.st_rdev, &target_st->st_rdev);
ce4defa0 4677 /* XXX: better use of kernel struct */
579a97f7
FB
4678 __put_user(st.st_size, &target_st->st_size);
4679 __put_user(st.st_blksize, &target_st->st_blksize);
4680 __put_user(st.st_blocks, &target_st->st_blocks);
4681 __put_user(st.st_atime, &target_st->target_st_atime);
4682 __put_user(st.st_mtime, &target_st->target_st_mtime);
4683 __put_user(st.st_ctime, &target_st->target_st_ctime);
4684 unlock_user_struct(target_st, arg2, 1);
ce4defa0 4685 }
60cd49d5
FB
4686 }
4687 }
4688 break;
a315a145 4689#endif
67867308
FB
4690#ifdef USE_UID16
4691 case TARGET_NR_lchown:
579a97f7
FB
4692 if (!(p = lock_user_string(arg1)))
4693 goto efault;
53a5960a
PB
4694 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4695 unlock_user(p, arg1, 0);
67867308
FB
4696 break;
4697 case TARGET_NR_getuid:
4698 ret = get_errno(high2lowuid(getuid()));
4699 break;
4700 case TARGET_NR_getgid:
4701 ret = get_errno(high2lowgid(getgid()));
4702 break;
4703 case TARGET_NR_geteuid:
4704 ret = get_errno(high2lowuid(geteuid()));
4705 break;
4706 case TARGET_NR_getegid:
4707 ret = get_errno(high2lowgid(getegid()));
4708 break;
4709 case TARGET_NR_setreuid:
4710 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4711 break;
4712 case TARGET_NR_setregid:
4713 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4714 break;
4715 case TARGET_NR_getgroups:
4716 {
4717 int gidsetsize = arg1;
53a5960a 4718 uint16_t *target_grouplist;
67867308
FB
4719 gid_t *grouplist;
4720 int i;
4721
4722 grouplist = alloca(gidsetsize * sizeof(gid_t));
4723 ret = get_errno(getgroups(gidsetsize, grouplist));
4724 if (!is_error(ret)) {
579a97f7
FB
4725 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
4726 if (!target_grouplist)
4727 goto efault;
67867308
FB
4728 for(i = 0;i < gidsetsize; i++)
4729 target_grouplist[i] = tswap16(grouplist[i]);
53a5960a 4730 unlock_user(target_grouplist, arg2, gidsetsize * 2);
67867308
FB
4731 }
4732 }
4733 break;
4734 case TARGET_NR_setgroups:
4735 {
4736 int gidsetsize = arg1;
53a5960a 4737 uint16_t *target_grouplist;
67867308
FB
4738 gid_t *grouplist;
4739 int i;
4740
4741 grouplist = alloca(gidsetsize * sizeof(gid_t));
579a97f7
FB
4742 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
4743 if (!target_grouplist) {
4744 ret = -TARGET_EFAULT;
4745 goto fail;
4746 }
67867308
FB
4747 for(i = 0;i < gidsetsize; i++)
4748 grouplist[i] = tswap16(target_grouplist[i]);
53a5960a 4749 unlock_user(target_grouplist, arg2, 0);
67867308
FB
4750 ret = get_errno(setgroups(gidsetsize, grouplist));
4751 }
4752 break;
4753 case TARGET_NR_fchown:
4754 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4755 break;
ccfa72b7
TS
4756#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4757 case TARGET_NR_fchownat:
579a97f7
FB
4758 if (!(p = lock_user_string(arg2)))
4759 goto efault;
4760 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4761 unlock_user(p, arg2, 0);
ccfa72b7
TS
4762 break;
4763#endif
67867308
FB
4764#ifdef TARGET_NR_setresuid
4765 case TARGET_NR_setresuid:
5fafdf24
TS
4766 ret = get_errno(setresuid(low2highuid(arg1),
4767 low2highuid(arg2),
67867308
FB
4768 low2highuid(arg3)));
4769 break;
4770#endif
4771#ifdef TARGET_NR_getresuid
4772 case TARGET_NR_getresuid:
4773 {
53a5960a 4774 uid_t ruid, euid, suid;
67867308
FB
4775 ret = get_errno(getresuid(&ruid, &euid, &suid));
4776 if (!is_error(ret)) {
53a5960a
PB
4777 tput16(arg1, tswap16(high2lowuid(ruid)));
4778 tput16(arg2, tswap16(high2lowuid(euid)));
4779 tput16(arg3, tswap16(high2lowuid(suid)));
67867308
FB
4780 }
4781 }
4782 break;
4783#endif
4784#ifdef TARGET_NR_getresgid
4785 case TARGET_NR_setresgid:
5fafdf24
TS
4786 ret = get_errno(setresgid(low2highgid(arg1),
4787 low2highgid(arg2),
67867308
FB
4788 low2highgid(arg3)));
4789 break;
4790#endif
4791#ifdef TARGET_NR_getresgid
4792 case TARGET_NR_getresgid:
4793 {
53a5960a 4794 gid_t rgid, egid, sgid;
67867308
FB
4795 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4796 if (!is_error(ret)) {
53a5960a
PB
4797 tput16(arg1, tswap16(high2lowgid(rgid)));
4798 tput16(arg2, tswap16(high2lowgid(egid)));
4799 tput16(arg3, tswap16(high2lowgid(sgid)));
67867308
FB
4800 }
4801 }
4802 break;
4803#endif
4804 case TARGET_NR_chown:
579a97f7
FB
4805 if (!(p = lock_user_string(arg1)))
4806 goto efault;
53a5960a
PB
4807 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4808 unlock_user(p, arg1, 0);
67867308
FB
4809 break;
4810 case TARGET_NR_setuid:
4811 ret = get_errno(setuid(low2highuid(arg1)));
4812 break;
4813 case TARGET_NR_setgid:
4814 ret = get_errno(setgid(low2highgid(arg1)));
4815 break;
4816 case TARGET_NR_setfsuid:
4817 ret = get_errno(setfsuid(arg1));
4818 break;
4819 case TARGET_NR_setfsgid:
4820 ret = get_errno(setfsgid(arg1));
4821 break;
4822#endif /* USE_UID16 */
4823
a315a145 4824#ifdef TARGET_NR_lchown32
31e31b8a 4825 case TARGET_NR_lchown32:
579a97f7
FB
4826 if (!(p = lock_user_string(arg1)))
4827 goto efault;
53a5960a
PB
4828 ret = get_errno(lchown(p, arg2, arg3));
4829 unlock_user(p, arg1, 0);
b03c60f3 4830 break;
a315a145
FB
4831#endif
4832#ifdef TARGET_NR_getuid32
31e31b8a 4833 case TARGET_NR_getuid32:
b03c60f3
FB
4834 ret = get_errno(getuid());
4835 break;
a315a145
FB
4836#endif
4837#ifdef TARGET_NR_getgid32
31e31b8a 4838 case TARGET_NR_getgid32:
b03c60f3
FB
4839 ret = get_errno(getgid());
4840 break;
a315a145
FB
4841#endif
4842#ifdef TARGET_NR_geteuid32
31e31b8a 4843 case TARGET_NR_geteuid32:
b03c60f3
FB
4844 ret = get_errno(geteuid());
4845 break;
a315a145
FB
4846#endif
4847#ifdef TARGET_NR_getegid32
31e31b8a 4848 case TARGET_NR_getegid32:
b03c60f3
FB
4849 ret = get_errno(getegid());
4850 break;
a315a145
FB
4851#endif
4852#ifdef TARGET_NR_setreuid32
31e31b8a 4853 case TARGET_NR_setreuid32:
b03c60f3
FB
4854 ret = get_errno(setreuid(arg1, arg2));
4855 break;
a315a145
FB
4856#endif
4857#ifdef TARGET_NR_setregid32
31e31b8a 4858 case TARGET_NR_setregid32:
b03c60f3
FB
4859 ret = get_errno(setregid(arg1, arg2));
4860 break;
a315a145
FB
4861#endif
4862#ifdef TARGET_NR_getgroups32
31e31b8a 4863 case TARGET_NR_getgroups32:
99c475ab
FB
4864 {
4865 int gidsetsize = arg1;
53a5960a 4866 uint32_t *target_grouplist;
99c475ab
FB
4867 gid_t *grouplist;
4868 int i;
4869
4870 grouplist = alloca(gidsetsize * sizeof(gid_t));
4871 ret = get_errno(getgroups(gidsetsize, grouplist));
4872 if (!is_error(ret)) {
579a97f7
FB
4873 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
4874 if (!target_grouplist) {
4875 ret = -TARGET_EFAULT;
4876 goto fail;
4877 }
99c475ab 4878 for(i = 0;i < gidsetsize; i++)
53a5960a
PB
4879 target_grouplist[i] = tswap32(grouplist[i]);
4880 unlock_user(target_grouplist, arg2, gidsetsize * 4);
99c475ab
FB
4881 }
4882 }
4883 break;
a315a145
FB
4884#endif
4885#ifdef TARGET_NR_setgroups32
31e31b8a 4886 case TARGET_NR_setgroups32:
99c475ab
FB
4887 {
4888 int gidsetsize = arg1;
53a5960a 4889 uint32_t *target_grouplist;
99c475ab
FB
4890 gid_t *grouplist;
4891 int i;
3b46e624 4892
99c475ab 4893 grouplist = alloca(gidsetsize * sizeof(gid_t));
579a97f7
FB
4894 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
4895 if (!target_grouplist) {
4896 ret = -TARGET_EFAULT;
4897 goto fail;
4898 }
99c475ab 4899 for(i = 0;i < gidsetsize; i++)
53a5960a
PB
4900 grouplist[i] = tswap32(target_grouplist[i]);
4901 unlock_user(target_grouplist, arg2, 0);
99c475ab
FB
4902 ret = get_errno(setgroups(gidsetsize, grouplist));
4903 }
4904 break;
a315a145
FB
4905#endif
4906#ifdef TARGET_NR_fchown32
31e31b8a 4907 case TARGET_NR_fchown32:
b03c60f3
FB
4908 ret = get_errno(fchown(arg1, arg2, arg3));
4909 break;
a315a145
FB
4910#endif
4911#ifdef TARGET_NR_setresuid32
31e31b8a 4912 case TARGET_NR_setresuid32:
b03c60f3
FB
4913 ret = get_errno(setresuid(arg1, arg2, arg3));
4914 break;
a315a145
FB
4915#endif
4916#ifdef TARGET_NR_getresuid32
31e31b8a 4917 case TARGET_NR_getresuid32:
b03c60f3 4918 {
53a5960a 4919 uid_t ruid, euid, suid;
b03c60f3
FB
4920 ret = get_errno(getresuid(&ruid, &euid, &suid));
4921 if (!is_error(ret)) {
53a5960a
PB
4922 tput32(arg1, tswap32(ruid));
4923 tput32(arg2, tswap32(euid));
4924 tput32(arg3, tswap32(suid));
b03c60f3
FB
4925 }
4926 }
4927 break;
a315a145
FB
4928#endif
4929#ifdef TARGET_NR_setresgid32
31e31b8a 4930 case TARGET_NR_setresgid32:
b03c60f3
FB
4931 ret = get_errno(setresgid(arg1, arg2, arg3));
4932 break;
a315a145
FB
4933#endif
4934#ifdef TARGET_NR_getresgid32
31e31b8a 4935 case TARGET_NR_getresgid32:
b03c60f3 4936 {
53a5960a 4937 gid_t rgid, egid, sgid;
b03c60f3
FB
4938 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4939 if (!is_error(ret)) {
53a5960a
PB
4940 tput32(arg1, tswap32(rgid));
4941 tput32(arg2, tswap32(egid));
4942 tput32(arg3, tswap32(sgid));
b03c60f3
FB
4943 }
4944 }
4945 break;
a315a145
FB
4946#endif
4947#ifdef TARGET_NR_chown32
31e31b8a 4948 case TARGET_NR_chown32:
579a97f7
FB
4949 if (!(p = lock_user_string(arg1)))
4950 goto efault;
53a5960a
PB
4951 ret = get_errno(chown(p, arg2, arg3));
4952 unlock_user(p, arg1, 0);
b03c60f3 4953 break;
a315a145
FB
4954#endif
4955#ifdef TARGET_NR_setuid32
31e31b8a 4956 case TARGET_NR_setuid32:
b03c60f3
FB
4957 ret = get_errno(setuid(arg1));
4958 break;
a315a145
FB
4959#endif
4960#ifdef TARGET_NR_setgid32
31e31b8a 4961 case TARGET_NR_setgid32:
b03c60f3
FB
4962 ret = get_errno(setgid(arg1));
4963 break;
a315a145
FB
4964#endif
4965#ifdef TARGET_NR_setfsuid32
31e31b8a 4966 case TARGET_NR_setfsuid32:
b03c60f3
FB
4967 ret = get_errno(setfsuid(arg1));
4968 break;
a315a145
FB
4969#endif
4970#ifdef TARGET_NR_setfsgid32
31e31b8a 4971 case TARGET_NR_setfsgid32:
b03c60f3
FB
4972 ret = get_errno(setfsgid(arg1));
4973 break;
a315a145 4974#endif
67867308 4975
31e31b8a 4976 case TARGET_NR_pivot_root:
b03c60f3 4977 goto unimplemented;
ffa65c3b 4978#ifdef TARGET_NR_mincore
31e31b8a 4979 case TARGET_NR_mincore:
b03c60f3 4980 goto unimplemented;
ffa65c3b
FB
4981#endif
4982#ifdef TARGET_NR_madvise
31e31b8a 4983 case TARGET_NR_madvise:
24836689
PB
4984 /* A straight passthrough may not be safe because qemu sometimes
4985 turns private flie-backed mappings into anonymous mappings.
4986 This will break MADV_DONTNEED.
4987 This is a hint, so ignoring and returning success is ok. */
4988 ret = get_errno(0);
4989 break;
ffa65c3b 4990#endif
992f48a0 4991#if TARGET_ABI_BITS == 32
31e31b8a 4992 case TARGET_NR_fcntl64:
77e4672d 4993 {
b1e341eb 4994 int cmd;
77e4672d 4995 struct flock64 fl;
53a5960a 4996 struct target_flock64 *target_fl;
ce4defa0 4997#ifdef TARGET_ARM
53a5960a 4998 struct target_eabi_flock64 *target_efl;
ce4defa0 4999#endif
77e4672d 5000
b1e341eb
TS
5001 switch(arg2){
5002 case TARGET_F_GETLK64:
5003 cmd = F_GETLK64;
a7222580 5004 break;
b1e341eb
TS
5005 case TARGET_F_SETLK64:
5006 cmd = F_SETLK64;
a7222580 5007 break;
b1e341eb
TS
5008 case TARGET_F_SETLKW64:
5009 cmd = F_SETLK64;
a7222580 5010 break;
b1e341eb
TS
5011 default:
5012 cmd = arg2;
a7222580 5013 break;
b1e341eb
TS
5014 }
5015
60cd49d5 5016 switch(arg2) {
b1e341eb 5017 case TARGET_F_GETLK64:
5813427b
TS
5018#ifdef TARGET_ARM
5019 if (((CPUARMState *)cpu_env)->eabi) {
9ee1fa2c
FB
5020 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5021 goto efault;
5813427b
TS
5022 fl.l_type = tswap16(target_efl->l_type);
5023 fl.l_whence = tswap16(target_efl->l_whence);
5024 fl.l_start = tswap64(target_efl->l_start);
5025 fl.l_len = tswap64(target_efl->l_len);
5026 fl.l_pid = tswapl(target_efl->l_pid);
5027 unlock_user_struct(target_efl, arg3, 0);
5028 } else
5029#endif
5030 {
9ee1fa2c
FB
5031 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5032 goto efault;
5813427b
TS
5033 fl.l_type = tswap16(target_fl->l_type);
5034 fl.l_whence = tswap16(target_fl->l_whence);
5035 fl.l_start = tswap64(target_fl->l_start);
5036 fl.l_len = tswap64(target_fl->l_len);
5037 fl.l_pid = tswapl(target_fl->l_pid);
5038 unlock_user_struct(target_fl, arg3, 0);
5039 }
b1e341eb 5040 ret = get_errno(fcntl(arg1, cmd, &fl));
77e4672d 5041 if (ret == 0) {
ce4defa0
PB
5042#ifdef TARGET_ARM
5043 if (((CPUARMState *)cpu_env)->eabi) {
9ee1fa2c
FB
5044 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5045 goto efault;
ce4defa0
PB
5046 target_efl->l_type = tswap16(fl.l_type);
5047 target_efl->l_whence = tswap16(fl.l_whence);
5048 target_efl->l_start = tswap64(fl.l_start);
5049 target_efl->l_len = tswap64(fl.l_len);
5050 target_efl->l_pid = tswapl(fl.l_pid);
53a5960a 5051 unlock_user_struct(target_efl, arg3, 1);
ce4defa0
PB
5052 } else
5053#endif
5054 {
9ee1fa2c
FB
5055 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5056 goto efault;
ce4defa0
PB
5057 target_fl->l_type = tswap16(fl.l_type);
5058 target_fl->l_whence = tswap16(fl.l_whence);
5059 target_fl->l_start = tswap64(fl.l_start);
5060 target_fl->l_len = tswap64(fl.l_len);
5061 target_fl->l_pid = tswapl(fl.l_pid);
53a5960a 5062 unlock_user_struct(target_fl, arg3, 1);
ce4defa0 5063 }
77e4672d
FB
5064 }
5065 break;
5066
b1e341eb
TS
5067 case TARGET_F_SETLK64:
5068 case TARGET_F_SETLKW64:
ce4defa0
PB
5069#ifdef TARGET_ARM
5070 if (((CPUARMState *)cpu_env)->eabi) {
9ee1fa2c
FB
5071 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5072 goto efault;
ce4defa0
PB
5073 fl.l_type = tswap16(target_efl->l_type);
5074 fl.l_whence = tswap16(target_efl->l_whence);
5075 fl.l_start = tswap64(target_efl->l_start);
5076 fl.l_len = tswap64(target_efl->l_len);
5077 fl.l_pid = tswapl(target_efl->l_pid);
53a5960a 5078 unlock_user_struct(target_efl, arg3, 0);
ce4defa0
PB
5079 } else
5080#endif
5081 {
9ee1fa2c
FB
5082 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5083 goto efault;
ce4defa0
PB
5084 fl.l_type = tswap16(target_fl->l_type);
5085 fl.l_whence = tswap16(target_fl->l_whence);
5086 fl.l_start = tswap64(target_fl->l_start);
5087 fl.l_len = tswap64(target_fl->l_len);
5088 fl.l_pid = tswapl(target_fl->l_pid);
53a5960a 5089 unlock_user_struct(target_fl, arg3, 0);
ce4defa0 5090 }
b1e341eb 5091 ret = get_errno(fcntl(arg1, cmd, &fl));
77e4672d 5092 break;
60cd49d5 5093 default:
9ee1fa2c 5094 ret = do_fcntl(arg1, cmd, arg3);
60cd49d5
FB
5095 break;
5096 }
77e4672d
FB
5097 break;
5098 }
60cd49d5 5099#endif
7d600c80
TS
5100#ifdef TARGET_NR_cacheflush
5101 case TARGET_NR_cacheflush:
5102 /* self-modifying code is handled automatically, so nothing needed */
5103 ret = 0;
5104 break;
5105#endif
ebc05488 5106#ifdef TARGET_NR_security
31e31b8a
FB
5107 case TARGET_NR_security:
5108 goto unimplemented;
c573ff67
FB
5109#endif
5110#ifdef TARGET_NR_getpagesize
5111 case TARGET_NR_getpagesize:
5112 ret = TARGET_PAGE_SIZE;
5113 break;
ebc05488 5114#endif
31e31b8a
FB
5115 case TARGET_NR_gettid:
5116 ret = get_errno(gettid());
5117 break;
e5febef5 5118#ifdef TARGET_NR_readahead
31e31b8a 5119 case TARGET_NR_readahead:
5cd4393b 5120 goto unimplemented;
e5febef5 5121#endif
ebc05488 5122#ifdef TARGET_NR_setxattr
31e31b8a
FB
5123 case TARGET_NR_setxattr:
5124 case TARGET_NR_lsetxattr:
5125 case TARGET_NR_fsetxattr:
5126 case TARGET_NR_getxattr:
5127 case TARGET_NR_lgetxattr:
5128 case TARGET_NR_fgetxattr:
5129 case TARGET_NR_listxattr:
5130 case TARGET_NR_llistxattr:
5131 case TARGET_NR_flistxattr:
5132 case TARGET_NR_removexattr:
5133 case TARGET_NR_lremovexattr:
5134 case TARGET_NR_fremovexattr:
5cd4393b 5135 goto unimplemented_nowarn;
ebc05488
FB
5136#endif
5137#ifdef TARGET_NR_set_thread_area
5cd4393b 5138 case TARGET_NR_set_thread_area:
6f5b89a0
TS
5139#ifdef TARGET_MIPS
5140 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5141 ret = 0;
5142 break;
5143#else
5144 goto unimplemented_nowarn;
5145#endif
5146#endif
5147#ifdef TARGET_NR_get_thread_area
5cd4393b
FB
5148 case TARGET_NR_get_thread_area:
5149 goto unimplemented_nowarn;
48dc41eb
FB
5150#endif
5151#ifdef TARGET_NR_getdomainname
5152 case TARGET_NR_getdomainname:
5153 goto unimplemented_nowarn;
ebc05488 5154#endif
6f5b89a0 5155
b5906f95
TS
5156#ifdef TARGET_NR_clock_gettime
5157 case TARGET_NR_clock_gettime:
5158 {
5159 struct timespec ts;
5160 ret = get_errno(clock_gettime(arg1, &ts));
5161 if (!is_error(ret)) {
5162 host_to_target_timespec(arg2, &ts);
5163 }
5164 break;
5165 }
5166#endif
5167#ifdef TARGET_NR_clock_getres
5168 case TARGET_NR_clock_getres:
5169 {
5170 struct timespec ts;
5171 ret = get_errno(clock_getres(arg1, &ts));
5172 if (!is_error(ret)) {
5173 host_to_target_timespec(arg2, &ts);
5174 }
5175 break;
5176 }
5177#endif
5178
6f5b89a0
TS
5179#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5180 case TARGET_NR_set_tid_address:
579a97f7
FB
5181 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5182 break;
6f5b89a0
TS
5183#endif
5184
3ae43202 5185#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
4cae1d16
TS
5186 case TARGET_NR_tkill:
5187 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5188 break;
5189#endif
5190
3ae43202 5191#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
71455574
TS
5192 case TARGET_NR_tgkill:
5193 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5194 break;
5195#endif
5196
4f2b1fe8
TS
5197#ifdef TARGET_NR_set_robust_list
5198 case TARGET_NR_set_robust_list:
5199 goto unimplemented_nowarn;
5200#endif
5201
9007f0ef
TS
5202#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5203 case TARGET_NR_utimensat:
5204 {
5205 struct timespec ts[2];
5206 target_to_host_timespec(ts, arg3);
5207 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5208 if (!arg2)
5209 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5210 else {
579a97f7 5211 if (!(p = lock_user_string(arg2))) {
0da46a6e 5212 ret = -TARGET_EFAULT;
579a97f7
FB
5213 goto fail;
5214 }
5215 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5216 unlock_user(p, arg2, 0);
9007f0ef
TS
5217 }
5218 }
5219 break;
5220#endif
5221
31e31b8a
FB
5222 default:
5223 unimplemented:
5cd4393b 5224 gemu_log("qemu: Unsupported syscall: %d\n", num);
4f2b1fe8 5225#if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5cd4393b 5226 unimplemented_nowarn:
80a9d035 5227#endif
0da46a6e 5228 ret = -TARGET_ENOSYS;
31e31b8a
FB
5229 break;
5230 }
579a97f7 5231fail:
c573ff67
FB
5232#ifdef DEBUG
5233 gemu_log(" = %ld\n", ret);
5234#endif
b92c47c1
TS
5235 if(do_strace)
5236 print_syscall_ret(num, ret);
31e31b8a 5237 return ret;
579a97f7
FB
5238efault:
5239 ret = -TARGET_EFAULT;
5240 goto fail;
31e31b8a 5241}