Allow multiple graphics devices.
[qemu/mini2440.git] / linux-user / syscall.c
blob2b132477f07db20d47d7dfbe3134ebf3f5ca19da
1 /*
2 * Linux syscalls
3 *
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <elf.h>
25 #include <endian.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <sys/time.h>
33 #include <sys/stat.h>
34 #include <sys/mount.h>
35 #include <sys/resource.h>
36 #include <sys/mman.h>
37 #include <sys/swap.h>
38 #include <signal.h>
39 #include <sched.h>
40 #include <sys/socket.h>
41 #include <sys/uio.h>
42 #include <sys/poll.h>
43 #include <sys/times.h>
44 #include <sys/shm.h>
45 #include <sys/statfs.h>
46 #include <utime.h>
47 #include <sys/sysinfo.h>
48 //#include <sys/user.h>
49 #include <netinet/ip.h>
50 #include <netinet/tcp.h>
52 #define termios host_termios
53 #define winsize host_winsize
54 #define termio host_termio
55 #define sgttyb host_sgttyb /* same as target */
56 #define tchars host_tchars /* same as target */
57 #define ltchars host_ltchars /* same as target */
59 #include <linux/termios.h>
60 #include <linux/unistd.h>
61 #include <linux/utsname.h>
62 #include <linux/cdrom.h>
63 #include <linux/hdreg.h>
64 #include <linux/soundcard.h>
65 #include <linux/dirent.h>
66 #include <linux/kd.h>
68 #include "qemu.h"
70 //#define DEBUG
72 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC)
73 /* 16 bit uid wrappers emulation */
74 #define USE_UID16
75 #endif
77 //#include <linux/msdos_fs.h>
78 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
79 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
82 #if defined(__powerpc__)
83 #undef __syscall_nr
84 #undef __sc_loadargs_0
85 #undef __sc_loadargs_1
86 #undef __sc_loadargs_2
87 #undef __sc_loadargs_3
88 #undef __sc_loadargs_4
89 #undef __sc_loadargs_5
90 #undef __sc_asm_input_0
91 #undef __sc_asm_input_1
92 #undef __sc_asm_input_2
93 #undef __sc_asm_input_3
94 #undef __sc_asm_input_4
95 #undef __sc_asm_input_5
96 #undef _syscall0
97 #undef _syscall1
98 #undef _syscall2
99 #undef _syscall3
100 #undef _syscall4
101 #undef _syscall5
103 /* need to redefine syscalls as Linux kernel defines are incorrect for
104 the clobber list */
105 /* On powerpc a system call basically clobbers the same registers like a
106 * function call, with the exception of LR (which is needed for the
107 * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
108 * an error return status).
111 #define __syscall_nr(nr, type, name, args...) \
112 unsigned long __sc_ret, __sc_err; \
114 register unsigned long __sc_0 __asm__ ("r0"); \
115 register unsigned long __sc_3 __asm__ ("r3"); \
116 register unsigned long __sc_4 __asm__ ("r4"); \
117 register unsigned long __sc_5 __asm__ ("r5"); \
118 register unsigned long __sc_6 __asm__ ("r6"); \
119 register unsigned long __sc_7 __asm__ ("r7"); \
121 __sc_loadargs_##nr(name, args); \
122 __asm__ __volatile__ \
123 ("sc \n\t" \
124 "mfcr %0 " \
125 : "=&r" (__sc_0), \
126 "=&r" (__sc_3), "=&r" (__sc_4), \
127 "=&r" (__sc_5), "=&r" (__sc_6), \
128 "=&r" (__sc_7) \
129 : __sc_asm_input_##nr \
130 : "cr0", "ctr", "memory", \
131 "r8", "r9", "r10","r11", "r12"); \
132 __sc_ret = __sc_3; \
133 __sc_err = __sc_0; \
135 if (__sc_err & 0x10000000) \
137 errno = __sc_ret; \
138 __sc_ret = -1; \
140 return (type) __sc_ret
142 #define __sc_loadargs_0(name, dummy...) \
143 __sc_0 = __NR_##name
144 #define __sc_loadargs_1(name, arg1) \
145 __sc_loadargs_0(name); \
146 __sc_3 = (unsigned long) (arg1)
147 #define __sc_loadargs_2(name, arg1, arg2) \
148 __sc_loadargs_1(name, arg1); \
149 __sc_4 = (unsigned long) (arg2)
150 #define __sc_loadargs_3(name, arg1, arg2, arg3) \
151 __sc_loadargs_2(name, arg1, arg2); \
152 __sc_5 = (unsigned long) (arg3)
153 #define __sc_loadargs_4(name, arg1, arg2, arg3, arg4) \
154 __sc_loadargs_3(name, arg1, arg2, arg3); \
155 __sc_6 = (unsigned long) (arg4)
156 #define __sc_loadargs_5(name, arg1, arg2, arg3, arg4, arg5) \
157 __sc_loadargs_4(name, arg1, arg2, arg3, arg4); \
158 __sc_7 = (unsigned long) (arg5)
160 #define __sc_asm_input_0 "0" (__sc_0)
161 #define __sc_asm_input_1 __sc_asm_input_0, "1" (__sc_3)
162 #define __sc_asm_input_2 __sc_asm_input_1, "2" (__sc_4)
163 #define __sc_asm_input_3 __sc_asm_input_2, "3" (__sc_5)
164 #define __sc_asm_input_4 __sc_asm_input_3, "4" (__sc_6)
165 #define __sc_asm_input_5 __sc_asm_input_4, "5" (__sc_7)
167 #define _syscall0(type,name) \
168 type name(void) \
170 __syscall_nr(0, type, name); \
173 #define _syscall1(type,name,type1,arg1) \
174 type name(type1 arg1) \
176 __syscall_nr(1, type, name, arg1); \
179 #define _syscall2(type,name,type1,arg1,type2,arg2) \
180 type name(type1 arg1, type2 arg2) \
182 __syscall_nr(2, type, name, arg1, arg2); \
185 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
186 type name(type1 arg1, type2 arg2, type3 arg3) \
188 __syscall_nr(3, type, name, arg1, arg2, arg3); \
191 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
192 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
194 __syscall_nr(4, type, name, arg1, arg2, arg3, arg4); \
197 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
198 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \
200 __syscall_nr(5, type, name, arg1, arg2, arg3, arg4, arg5); \
202 #endif
204 #define __NR_sys_uname __NR_uname
205 #define __NR_sys_getcwd1 __NR_getcwd
206 #define __NR_sys_getdents __NR_getdents
207 #define __NR_sys_getdents64 __NR_getdents64
208 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
210 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
211 #define __NR__llseek __NR_lseek
212 #endif
214 #ifdef __NR_gettid
215 _syscall0(int, gettid)
216 #else
217 static int gettid(void) {
218 return -ENOSYS;
220 #endif
221 _syscall1(int,sys_uname,struct new_utsname *,buf)
222 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
223 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
224 _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
225 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
226 loff_t *, res, uint, wh);
227 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
228 #ifdef __NR_exit_group
229 _syscall1(int,exit_group,int,error_code)
230 #endif
232 extern int personality(int);
233 extern int flock(int, int);
234 extern int setfsuid(int);
235 extern int setfsgid(int);
236 extern int setresuid(uid_t, uid_t, uid_t);
237 extern int getresuid(uid_t *, uid_t *, uid_t *);
238 extern int setresgid(gid_t, gid_t, gid_t);
239 extern int getresgid(gid_t *, gid_t *, gid_t *);
240 extern int setgroups(int, gid_t *);
242 static inline long get_errno(long ret)
244 if (ret == -1)
245 return -errno;
246 else
247 return ret;
250 static inline int is_error(long ret)
252 return (unsigned long)ret >= (unsigned long)(-4096);
255 static target_ulong target_brk;
256 static target_ulong target_original_brk;
258 void target_set_brk(target_ulong new_brk)
260 target_original_brk = target_brk = new_brk;
263 long do_brk(target_ulong new_brk)
265 target_ulong brk_page;
266 long mapped_addr;
267 int new_alloc_size;
269 if (!new_brk)
270 return target_brk;
271 if (new_brk < target_original_brk)
272 return -ENOMEM;
274 brk_page = HOST_PAGE_ALIGN(target_brk);
276 /* If the new brk is less than this, set it and we're done... */
277 if (new_brk < brk_page) {
278 target_brk = new_brk;
279 return target_brk;
282 /* We need to allocate more memory after the brk... */
283 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
284 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
285 PROT_READ|PROT_WRITE,
286 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
287 if (is_error(mapped_addr)) {
288 return mapped_addr;
289 } else {
290 target_brk = new_brk;
291 return target_brk;
295 static inline fd_set *target_to_host_fds(fd_set *fds,
296 target_long *target_fds, int n)
298 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
299 return (fd_set *)target_fds;
300 #else
301 int i, b;
302 if (target_fds) {
303 FD_ZERO(fds);
304 for(i = 0;i < n; i++) {
305 b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
306 (i & (TARGET_LONG_BITS - 1))) & 1;
307 if (b)
308 FD_SET(i, fds);
310 return fds;
311 } else {
312 return NULL;
314 #endif
317 static inline void host_to_target_fds(target_long *target_fds,
318 fd_set *fds, int n)
320 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
321 /* nothing to do */
322 #else
323 int i, nw, j, k;
324 target_long v;
326 if (target_fds) {
327 nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
328 k = 0;
329 for(i = 0;i < nw; i++) {
330 v = 0;
331 for(j = 0; j < TARGET_LONG_BITS; j++) {
332 v |= ((FD_ISSET(k, fds) != 0) << j);
333 k++;
335 target_fds[i] = tswapl(v);
338 #endif
341 #if defined(__alpha__)
342 #define HOST_HZ 1024
343 #else
344 #define HOST_HZ 100
345 #endif
347 static inline long host_to_target_clock_t(long ticks)
349 #if HOST_HZ == TARGET_HZ
350 return ticks;
351 #else
352 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
353 #endif
356 static inline void host_to_target_rusage(target_ulong target_addr,
357 const struct rusage *rusage)
359 struct target_rusage *target_rusage;
361 lock_user_struct(target_rusage, target_addr, 0);
362 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
363 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
364 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
365 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
366 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
367 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
368 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
369 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
370 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
371 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
372 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
373 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
374 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
375 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
376 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
377 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
378 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
379 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
380 unlock_user_struct(target_rusage, target_addr, 1);
383 static inline void target_to_host_timeval(struct timeval *tv,
384 target_ulong target_addr)
386 struct target_timeval *target_tv;
388 lock_user_struct(target_tv, target_addr, 1);
389 tv->tv_sec = tswapl(target_tv->tv_sec);
390 tv->tv_usec = tswapl(target_tv->tv_usec);
391 unlock_user_struct(target_tv, target_addr, 0);
394 static inline void host_to_target_timeval(target_ulong target_addr,
395 const struct timeval *tv)
397 struct target_timeval *target_tv;
399 lock_user_struct(target_tv, target_addr, 0);
400 target_tv->tv_sec = tswapl(tv->tv_sec);
401 target_tv->tv_usec = tswapl(tv->tv_usec);
402 unlock_user_struct(target_tv, target_addr, 1);
406 static long do_select(long n,
407 target_ulong rfd_p, target_ulong wfd_p,
408 target_ulong efd_p, target_ulong target_tv)
410 fd_set rfds, wfds, efds;
411 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
412 target_long *target_rfds, *target_wfds, *target_efds;
413 struct timeval tv, *tv_ptr;
414 long ret;
415 int ok;
417 if (rfd_p) {
418 target_rfds = lock_user(rfd_p, sizeof(target_long) * n, 1);
419 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
420 } else {
421 target_rfds = NULL;
422 rfds_ptr = NULL;
424 if (wfd_p) {
425 target_wfds = lock_user(wfd_p, sizeof(target_long) * n, 1);
426 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
427 } else {
428 target_wfds = NULL;
429 wfds_ptr = NULL;
431 if (efd_p) {
432 target_efds = lock_user(efd_p, sizeof(target_long) * n, 1);
433 efds_ptr = target_to_host_fds(&efds, target_efds, n);
434 } else {
435 target_efds = NULL;
436 efds_ptr = NULL;
439 if (target_tv) {
440 target_to_host_timeval(&tv, target_tv);
441 tv_ptr = &tv;
442 } else {
443 tv_ptr = NULL;
445 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
446 ok = !is_error(ret);
448 if (ok) {
449 host_to_target_fds(target_rfds, rfds_ptr, n);
450 host_to_target_fds(target_wfds, wfds_ptr, n);
451 host_to_target_fds(target_efds, efds_ptr, n);
453 if (target_tv) {
454 host_to_target_timeval(target_tv, &tv);
457 if (target_rfds)
458 unlock_user(target_rfds, rfd_p, ok ? sizeof(target_long) * n : 0);
459 if (target_wfds)
460 unlock_user(target_wfds, wfd_p, ok ? sizeof(target_long) * n : 0);
461 if (target_efds)
462 unlock_user(target_efds, efd_p, ok ? sizeof(target_long) * n : 0);
464 return ret;
467 static inline void target_to_host_sockaddr(struct sockaddr *addr,
468 target_ulong target_addr,
469 socklen_t len)
471 struct target_sockaddr *target_saddr;
473 target_saddr = lock_user(target_addr, len, 1);
474 memcpy(addr, target_saddr, len);
475 addr->sa_family = tswap16(target_saddr->sa_family);
476 unlock_user(target_saddr, target_addr, 0);
479 static inline void host_to_target_sockaddr(target_ulong target_addr,
480 struct sockaddr *addr,
481 socklen_t len)
483 struct target_sockaddr *target_saddr;
485 target_saddr = lock_user(target_addr, len, 0);
486 memcpy(target_saddr, addr, len);
487 target_saddr->sa_family = tswap16(addr->sa_family);
488 unlock_user(target_saddr, target_addr, len);
491 /* ??? Should this also swap msgh->name? */
492 static inline void target_to_host_cmsg(struct msghdr *msgh,
493 struct target_msghdr *target_msgh)
495 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
496 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
497 socklen_t space = 0;
499 while (cmsg && target_cmsg) {
500 void *data = CMSG_DATA(cmsg);
501 void *target_data = TARGET_CMSG_DATA(target_cmsg);
503 int len = tswapl(target_cmsg->cmsg_len)
504 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
506 space += CMSG_SPACE(len);
507 if (space > msgh->msg_controllen) {
508 space -= CMSG_SPACE(len);
509 gemu_log("Host cmsg overflow\n");
510 break;
513 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
514 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
515 cmsg->cmsg_len = CMSG_LEN(len);
517 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
518 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
519 memcpy(data, target_data, len);
520 } else {
521 int *fd = (int *)data;
522 int *target_fd = (int *)target_data;
523 int i, numfds = len / sizeof(int);
525 for (i = 0; i < numfds; i++)
526 fd[i] = tswap32(target_fd[i]);
529 cmsg = CMSG_NXTHDR(msgh, cmsg);
530 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
533 msgh->msg_controllen = space;
536 /* ??? Should this also swap msgh->name? */
537 static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
538 struct msghdr *msgh)
540 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
541 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
542 socklen_t space = 0;
544 while (cmsg && target_cmsg) {
545 void *data = CMSG_DATA(cmsg);
546 void *target_data = TARGET_CMSG_DATA(target_cmsg);
548 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
550 space += TARGET_CMSG_SPACE(len);
551 if (space > tswapl(target_msgh->msg_controllen)) {
552 space -= TARGET_CMSG_SPACE(len);
553 gemu_log("Target cmsg overflow\n");
554 break;
557 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
558 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
559 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
561 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
562 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
563 memcpy(target_data, data, len);
564 } else {
565 int *fd = (int *)data;
566 int *target_fd = (int *)target_data;
567 int i, numfds = len / sizeof(int);
569 for (i = 0; i < numfds; i++)
570 target_fd[i] = tswap32(fd[i]);
573 cmsg = CMSG_NXTHDR(msgh, cmsg);
574 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
577 msgh->msg_controllen = tswapl(space);
580 static long do_setsockopt(int sockfd, int level, int optname,
581 target_ulong optval, socklen_t optlen)
583 int val, ret;
585 switch(level) {
586 case SOL_TCP:
587 /* TCP options all take an 'int' value. */
588 if (optlen < sizeof(uint32_t))
589 return -EINVAL;
591 val = tget32(optval);
592 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
593 break;
594 case SOL_IP:
595 switch(optname) {
596 case IP_TOS:
597 case IP_TTL:
598 case IP_HDRINCL:
599 case IP_ROUTER_ALERT:
600 case IP_RECVOPTS:
601 case IP_RETOPTS:
602 case IP_PKTINFO:
603 case IP_MTU_DISCOVER:
604 case IP_RECVERR:
605 case IP_RECVTOS:
606 #ifdef IP_FREEBIND
607 case IP_FREEBIND:
608 #endif
609 case IP_MULTICAST_TTL:
610 case IP_MULTICAST_LOOP:
611 val = 0;
612 if (optlen >= sizeof(uint32_t)) {
613 val = tget32(optval);
614 } else if (optlen >= 1) {
615 val = tget8(optval);
617 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
618 break;
619 default:
620 goto unimplemented;
622 break;
623 case SOL_SOCKET:
624 switch (optname) {
625 /* Options with 'int' argument. */
626 case SO_DEBUG:
627 case SO_REUSEADDR:
628 case SO_TYPE:
629 case SO_ERROR:
630 case SO_DONTROUTE:
631 case SO_BROADCAST:
632 case SO_SNDBUF:
633 case SO_RCVBUF:
634 case SO_KEEPALIVE:
635 case SO_OOBINLINE:
636 case SO_NO_CHECK:
637 case SO_PRIORITY:
638 #ifdef SO_BSDCOMPAT
639 case SO_BSDCOMPAT:
640 #endif
641 case SO_PASSCRED:
642 case SO_TIMESTAMP:
643 case SO_RCVLOWAT:
644 case SO_RCVTIMEO:
645 case SO_SNDTIMEO:
646 if (optlen < sizeof(uint32_t))
647 return -EINVAL;
649 val = tget32(optval);
650 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
651 break;
652 default:
653 goto unimplemented;
655 break;
656 default:
657 unimplemented:
658 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
659 ret = -ENOSYS;
661 return ret;
664 static long do_getsockopt(int sockfd, int level, int optname,
665 target_ulong optval, target_ulong optlen)
667 int len, lv, val, ret;
669 switch(level) {
670 case SOL_SOCKET:
671 switch (optname) {
672 case SO_LINGER:
673 case SO_RCVTIMEO:
674 case SO_SNDTIMEO:
675 case SO_PEERCRED:
676 case SO_PEERNAME:
677 /* These don't just return a single integer */
678 goto unimplemented;
679 default:
680 goto int_case;
682 break;
683 case SOL_TCP:
684 /* TCP options all take an 'int' value. */
685 int_case:
686 len = tget32(optlen);
687 if (len < 0)
688 return -EINVAL;
689 lv = sizeof(int);
690 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
691 if (ret < 0)
692 return ret;
693 val = tswap32(val);
694 if (len > lv)
695 len = lv;
696 if (len == 4)
697 tput32(optval, val);
698 else
699 tput8(optval, val);
700 tput32(optlen, len);
701 break;
702 case SOL_IP:
703 switch(optname) {
704 case IP_TOS:
705 case IP_TTL:
706 case IP_HDRINCL:
707 case IP_ROUTER_ALERT:
708 case IP_RECVOPTS:
709 case IP_RETOPTS:
710 case IP_PKTINFO:
711 case IP_MTU_DISCOVER:
712 case IP_RECVERR:
713 case IP_RECVTOS:
714 #ifdef IP_FREEBIND
715 case IP_FREEBIND:
716 #endif
717 case IP_MULTICAST_TTL:
718 case IP_MULTICAST_LOOP:
719 len = tget32(optlen);
720 if (len < 0)
721 return -EINVAL;
722 lv = sizeof(int);
723 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
724 if (ret < 0)
725 return ret;
726 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
727 len = 1;
728 tput32(optlen, len);
729 tput8(optval, val);
730 } else {
731 if (len > sizeof(int))
732 len = sizeof(int);
733 tput32(optlen, len);
734 tput32(optval, val);
736 break;
737 default:
738 goto unimplemented;
740 break;
741 default:
742 unimplemented:
743 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
744 level, optname);
745 ret = -ENOSYS;
746 break;
748 return ret;
751 static void lock_iovec(struct iovec *vec, target_ulong target_addr,
752 int count, int copy)
754 struct target_iovec *target_vec;
755 target_ulong base;
756 int i;
758 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
759 for(i = 0;i < count; i++) {
760 base = tswapl(target_vec[i].iov_base);
761 vec[i].iov_len = tswapl(target_vec[i].iov_len);
762 vec[i].iov_base = lock_user(base, vec[i].iov_len, copy);
764 unlock_user (target_vec, target_addr, 0);
767 static void unlock_iovec(struct iovec *vec, target_ulong target_addr,
768 int count, int copy)
770 struct target_iovec *target_vec;
771 target_ulong base;
772 int i;
774 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
775 for(i = 0;i < count; i++) {
776 base = tswapl(target_vec[i].iov_base);
777 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
779 unlock_user (target_vec, target_addr, 0);
782 static long do_socketcall(int num, target_ulong vptr)
784 long ret;
785 const int n = sizeof(target_ulong);
787 switch(num) {
788 case SOCKOP_socket:
790 int domain = tgetl(vptr);
791 int type = tgetl(vptr + n);
792 int protocol = tgetl(vptr + 2 * n);
794 ret = get_errno(socket(domain, type, protocol));
796 break;
797 case SOCKOP_bind:
799 int sockfd = tgetl(vptr);
800 target_ulong target_addr = tgetl(vptr + n);
801 socklen_t addrlen = tgetl(vptr + 2 * n);
802 void *addr = alloca(addrlen);
804 target_to_host_sockaddr(addr, target_addr, addrlen);
805 ret = get_errno(bind(sockfd, addr, addrlen));
807 break;
808 case SOCKOP_connect:
810 int sockfd = tgetl(vptr);
811 target_ulong target_addr = tgetl(vptr + n);
812 socklen_t addrlen = tgetl(vptr + 2 * n);
813 void *addr = alloca(addrlen);
815 target_to_host_sockaddr(addr, target_addr, addrlen);
816 ret = get_errno(connect(sockfd, addr, addrlen));
818 break;
819 case SOCKOP_listen:
821 int sockfd = tgetl(vptr);
822 int backlog = tgetl(vptr + n);
824 ret = get_errno(listen(sockfd, backlog));
826 break;
827 case SOCKOP_accept:
829 int sockfd = tgetl(vptr);
830 target_ulong target_addr = tgetl(vptr + n);
831 target_ulong target_addrlen = tgetl(vptr + 2 * n);
832 socklen_t addrlen = tget32(target_addrlen);
833 void *addr = alloca(addrlen);
835 ret = get_errno(accept(sockfd, addr, &addrlen));
836 if (!is_error(ret)) {
837 host_to_target_sockaddr(target_addr, addr, addrlen);
838 tput32(target_addrlen, addrlen);
841 break;
842 case SOCKOP_getsockname:
844 int sockfd = tgetl(vptr);
845 target_ulong target_addr = tgetl(vptr + n);
846 target_ulong target_addrlen = tgetl(vptr + 2 * n);
847 socklen_t addrlen = tget32(target_addrlen);
848 void *addr = alloca(addrlen);
850 ret = get_errno(getsockname(sockfd, addr, &addrlen));
851 if (!is_error(ret)) {
852 host_to_target_sockaddr(target_addr, addr, addrlen);
853 tput32(target_addrlen, addrlen);
856 break;
857 case SOCKOP_getpeername:
859 int sockfd = tgetl(vptr);
860 target_ulong target_addr = tgetl(vptr + n);
861 target_ulong target_addrlen = tgetl(vptr + 2 * n);
862 socklen_t addrlen = tget32(target_addrlen);
863 void *addr = alloca(addrlen);
865 ret = get_errno(getpeername(sockfd, addr, &addrlen));
866 if (!is_error(ret)) {
867 host_to_target_sockaddr(target_addr, addr, addrlen);
868 tput32(target_addrlen, addrlen);
871 break;
872 case SOCKOP_socketpair:
874 int domain = tgetl(vptr);
875 int type = tgetl(vptr + n);
876 int protocol = tgetl(vptr + 2 * n);
877 target_ulong target_tab = tgetl(vptr + 3 * n);
878 int tab[2];
880 ret = get_errno(socketpair(domain, type, protocol, tab));
881 if (!is_error(ret)) {
882 tput32(target_tab, tab[0]);
883 tput32(target_tab + 4, tab[1]);
886 break;
887 case SOCKOP_send:
889 int sockfd = tgetl(vptr);
890 target_ulong msg = tgetl(vptr + n);
891 size_t len = tgetl(vptr + 2 * n);
892 int flags = tgetl(vptr + 3 * n);
893 void *host_msg;
895 host_msg = lock_user(msg, len, 1);
896 ret = get_errno(send(sockfd, host_msg, len, flags));
897 unlock_user(host_msg, msg, 0);
899 break;
900 case SOCKOP_recv:
902 int sockfd = tgetl(vptr);
903 target_ulong msg = tgetl(vptr + n);
904 size_t len = tgetl(vptr + 2 * n);
905 int flags = tgetl(vptr + 3 * n);
906 void *host_msg;
908 host_msg = lock_user(msg, len, 0);
909 ret = get_errno(recv(sockfd, host_msg, len, flags));
910 unlock_user(host_msg, msg, ret);
912 break;
913 case SOCKOP_sendto:
915 int sockfd = tgetl(vptr);
916 target_ulong msg = tgetl(vptr + n);
917 size_t len = tgetl(vptr + 2 * n);
918 int flags = tgetl(vptr + 3 * n);
919 target_ulong target_addr = tgetl(vptr + 4 * n);
920 socklen_t addrlen = tgetl(vptr + 5 * n);
921 void *addr = alloca(addrlen);
922 void *host_msg;
924 host_msg = lock_user(msg, len, 1);
925 target_to_host_sockaddr(addr, target_addr, addrlen);
926 ret = get_errno(sendto(sockfd, host_msg, len, flags, addr, addrlen));
927 unlock_user(host_msg, msg, 0);
929 break;
930 case SOCKOP_recvfrom:
932 int sockfd = tgetl(vptr);
933 target_ulong msg = tgetl(vptr + n);
934 size_t len = tgetl(vptr + 2 * n);
935 int flags = tgetl(vptr + 3 * n);
936 target_ulong target_addr = tgetl(vptr + 4 * n);
937 target_ulong target_addrlen = tgetl(vptr + 5 * n);
938 socklen_t addrlen = tget32(target_addrlen);
939 void *addr = alloca(addrlen);
940 void *host_msg;
942 host_msg = lock_user(msg, len, 0);
943 ret = get_errno(recvfrom(sockfd, host_msg, len, flags, addr, &addrlen));
944 if (!is_error(ret)) {
945 host_to_target_sockaddr(target_addr, addr, addrlen);
946 tput32(target_addrlen, addrlen);
947 unlock_user(host_msg, msg, len);
948 } else {
949 unlock_user(host_msg, msg, 0);
952 break;
953 case SOCKOP_shutdown:
955 int sockfd = tgetl(vptr);
956 int how = tgetl(vptr + n);
958 ret = get_errno(shutdown(sockfd, how));
960 break;
961 case SOCKOP_sendmsg:
962 case SOCKOP_recvmsg:
964 int fd;
965 target_ulong target_msg;
966 struct target_msghdr *msgp;
967 struct msghdr msg;
968 int flags, count;
969 struct iovec *vec;
970 target_ulong target_vec;
971 int send = (num == SOCKOP_sendmsg);
973 target_msg = tgetl(vptr + n);
974 lock_user_struct(msgp, target_msg, 1);
975 if (msgp->msg_name) {
976 msg.msg_namelen = tswap32(msgp->msg_namelen);
977 msg.msg_name = alloca(msg.msg_namelen);
978 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
979 msg.msg_namelen);
980 } else {
981 msg.msg_name = NULL;
982 msg.msg_namelen = 0;
984 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
985 msg.msg_control = alloca(msg.msg_controllen);
986 msg.msg_flags = tswap32(msgp->msg_flags);
988 count = tswapl(msgp->msg_iovlen);
989 vec = alloca(count * sizeof(struct iovec));
990 target_vec = tswapl(msgp->msg_iov);
991 lock_iovec(vec, target_vec, count, send);
992 msg.msg_iovlen = count;
993 msg.msg_iov = vec;
995 fd = tgetl(vptr);
996 flags = tgetl(vptr + 2 * n);
997 if (send) {
998 target_to_host_cmsg(&msg, msgp);
999 ret = get_errno(sendmsg(fd, &msg, flags));
1000 } else {
1001 ret = get_errno(recvmsg(fd, &msg, flags));
1002 if (!is_error(ret))
1003 host_to_target_cmsg(msgp, &msg);
1005 unlock_iovec(vec, target_vec, count, !send);
1007 break;
1008 case SOCKOP_setsockopt:
1010 int sockfd = tgetl(vptr);
1011 int level = tgetl(vptr + n);
1012 int optname = tgetl(vptr + 2 * n);
1013 target_ulong optval = tgetl(vptr + 3 * n);
1014 socklen_t optlen = tgetl(vptr + 4 * n);
1016 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1018 break;
1019 case SOCKOP_getsockopt:
1021 int sockfd = tgetl(vptr);
1022 int level = tgetl(vptr + n);
1023 int optname = tgetl(vptr + 2 * n);
1024 target_ulong optval = tgetl(vptr + 3 * n);
1025 target_ulong poptlen = tgetl(vptr + 4 * n);
1027 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
1029 break;
1030 default:
1031 gemu_log("Unsupported socketcall: %d\n", num);
1032 ret = -ENOSYS;
1033 break;
1035 return ret;
1039 #define N_SHM_REGIONS 32
1041 static struct shm_region {
1042 uint32_t start;
1043 uint32_t size;
1044 } shm_regions[N_SHM_REGIONS];
1046 /* ??? This only works with linear mappings. */
1047 static long do_ipc(long call, long first, long second, long third,
1048 long ptr, long fifth)
1050 int version;
1051 long ret = 0;
1052 unsigned long raddr;
1053 struct shmid_ds shm_info;
1054 int i;
1056 version = call >> 16;
1057 call &= 0xffff;
1059 switch (call) {
1060 case IPCOP_shmat:
1061 /* SHM_* flags are the same on all linux platforms */
1062 ret = get_errno((long) shmat(first, (void *) ptr, second));
1063 if (is_error(ret))
1064 break;
1065 raddr = ret;
1066 /* find out the length of the shared memory segment */
1068 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1069 if (is_error(ret)) {
1070 /* can't get length, bail out */
1071 shmdt((void *) raddr);
1072 break;
1074 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1075 PAGE_VALID | PAGE_READ |
1076 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1077 for (i = 0; i < N_SHM_REGIONS; ++i) {
1078 if (shm_regions[i].start == 0) {
1079 shm_regions[i].start = raddr;
1080 shm_regions[i].size = shm_info.shm_segsz;
1081 break;
1084 if (put_user(raddr, (uint32_t *)third))
1085 return -EFAULT;
1086 ret = 0;
1087 break;
1088 case IPCOP_shmdt:
1089 for (i = 0; i < N_SHM_REGIONS; ++i) {
1090 if (shm_regions[i].start == ptr) {
1091 shm_regions[i].start = 0;
1092 page_set_flags(ptr, shm_regions[i].size, 0);
1093 break;
1096 ret = get_errno(shmdt((void *) ptr));
1097 break;
1099 case IPCOP_shmget:
1100 /* IPC_* flag values are the same on all linux platforms */
1101 ret = get_errno(shmget(first, second, third));
1102 break;
1104 /* IPC_* and SHM_* command values are the same on all linux platforms */
1105 case IPCOP_shmctl:
1106 switch(second) {
1107 case IPC_RMID:
1108 case SHM_LOCK:
1109 case SHM_UNLOCK:
1110 ret = get_errno(shmctl(first, second, NULL));
1111 break;
1112 default:
1113 goto unimplemented;
1115 break;
1116 default:
1117 unimplemented:
1118 gemu_log("Unsupported ipc call: %ld (version %d)\n", call, version);
1119 ret = -ENOSYS;
1120 break;
1122 return ret;
1125 /* kernel structure types definitions */
1126 #define IFNAMSIZ 16
1128 #define STRUCT(name, list...) STRUCT_ ## name,
1129 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
1130 enum {
1131 #include "syscall_types.h"
1133 #undef STRUCT
1134 #undef STRUCT_SPECIAL
1136 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1137 #define STRUCT_SPECIAL(name)
1138 #include "syscall_types.h"
1139 #undef STRUCT
1140 #undef STRUCT_SPECIAL
1142 typedef struct IOCTLEntry {
1143 unsigned int target_cmd;
1144 unsigned int host_cmd;
1145 const char *name;
1146 int access;
1147 const argtype arg_type[5];
1148 } IOCTLEntry;
1150 #define IOC_R 0x0001
1151 #define IOC_W 0x0002
1152 #define IOC_RW (IOC_R | IOC_W)
1154 #define MAX_STRUCT_SIZE 4096
1156 IOCTLEntry ioctl_entries[] = {
1157 #define IOCTL(cmd, access, types...) \
1158 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1159 #include "ioctls.h"
1160 { 0, 0, },
1163 /* ??? Implement proper locking for ioctls. */
1164 static long do_ioctl(long fd, long cmd, long arg)
1166 const IOCTLEntry *ie;
1167 const argtype *arg_type;
1168 long ret;
1169 uint8_t buf_temp[MAX_STRUCT_SIZE];
1170 int target_size;
1171 void *argptr;
1173 ie = ioctl_entries;
1174 for(;;) {
1175 if (ie->target_cmd == 0) {
1176 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
1177 return -ENOSYS;
1179 if (ie->target_cmd == cmd)
1180 break;
1181 ie++;
1183 arg_type = ie->arg_type;
1184 #if defined(DEBUG)
1185 gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
1186 #endif
1187 switch(arg_type[0]) {
1188 case TYPE_NULL:
1189 /* no argument */
1190 ret = get_errno(ioctl(fd, ie->host_cmd));
1191 break;
1192 case TYPE_PTRVOID:
1193 case TYPE_INT:
1194 /* int argment */
1195 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1196 break;
1197 case TYPE_PTR:
1198 arg_type++;
1199 target_size = thunk_type_size(arg_type, 0);
1200 switch(ie->access) {
1201 case IOC_R:
1202 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1203 if (!is_error(ret)) {
1204 argptr = lock_user(arg, target_size, 0);
1205 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1206 unlock_user(argptr, arg, target_size);
1208 break;
1209 case IOC_W:
1210 argptr = lock_user(arg, target_size, 1);
1211 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1212 unlock_user(argptr, arg, 0);
1213 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1214 break;
1215 default:
1216 case IOC_RW:
1217 argptr = lock_user(arg, target_size, 1);
1218 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1219 unlock_user(argptr, arg, 0);
1220 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1221 if (!is_error(ret)) {
1222 argptr = lock_user(arg, target_size, 0);
1223 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1224 unlock_user(argptr, arg, target_size);
1226 break;
1228 break;
1229 default:
1230 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
1231 ret = -ENOSYS;
1232 break;
1234 return ret;
1237 bitmask_transtbl iflag_tbl[] = {
1238 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1239 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1240 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1241 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1242 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1243 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1244 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1245 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1246 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1247 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1248 { TARGET_IXON, TARGET_IXON, IXON, IXON },
1249 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1250 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1251 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1252 { 0, 0, 0, 0 }
1255 bitmask_transtbl oflag_tbl[] = {
1256 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1257 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1258 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1259 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1260 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1261 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1262 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1263 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1264 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1265 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1266 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1267 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1268 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1269 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1270 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1271 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1272 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1273 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1274 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1275 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1276 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1277 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1278 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1279 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1280 { 0, 0, 0, 0 }
1283 bitmask_transtbl cflag_tbl[] = {
1284 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1285 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1286 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1287 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1288 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1289 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1290 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1291 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1292 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1293 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1294 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1295 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1296 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1297 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1298 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1299 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1300 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1301 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1302 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1303 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1304 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1305 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1306 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1307 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1308 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1309 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1310 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1311 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1312 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1313 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1314 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1315 { 0, 0, 0, 0 }
1318 bitmask_transtbl lflag_tbl[] = {
1319 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1320 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1321 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1322 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1323 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1324 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1325 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1326 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1327 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1328 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1329 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1330 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1331 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1332 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1333 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1334 { 0, 0, 0, 0 }
1337 static void target_to_host_termios (void *dst, const void *src)
1339 struct host_termios *host = dst;
1340 const struct target_termios *target = src;
1342 host->c_iflag =
1343 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
1344 host->c_oflag =
1345 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
1346 host->c_cflag =
1347 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
1348 host->c_lflag =
1349 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
1350 host->c_line = target->c_line;
1352 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
1353 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
1354 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
1355 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
1356 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
1357 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
1358 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
1359 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
1360 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
1361 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
1362 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
1363 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
1364 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
1365 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
1366 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
1367 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
1368 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
1371 static void host_to_target_termios (void *dst, const void *src)
1373 struct target_termios *target = dst;
1374 const struct host_termios *host = src;
1376 target->c_iflag =
1377 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
1378 target->c_oflag =
1379 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
1380 target->c_cflag =
1381 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
1382 target->c_lflag =
1383 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
1384 target->c_line = host->c_line;
1386 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
1387 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
1388 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
1389 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
1390 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
1391 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
1392 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
1393 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
1394 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
1395 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
1396 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
1397 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
1398 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
1399 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
1400 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
1401 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
1402 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
1405 StructEntry struct_termios_def = {
1406 .convert = { host_to_target_termios, target_to_host_termios },
1407 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
1408 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
1411 static bitmask_transtbl mmap_flags_tbl[] = {
1412 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
1413 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
1414 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
1415 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
1416 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
1417 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
1418 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
1419 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
1420 { 0, 0, 0, 0 }
1423 static bitmask_transtbl fcntl_flags_tbl[] = {
1424 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
1425 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
1426 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
1427 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
1428 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
1429 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
1430 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
1431 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
1432 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
1433 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
1434 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
1435 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
1436 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
1437 #if defined(O_DIRECT)
1438 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
1439 #endif
1440 { 0, 0, 0, 0 }
1443 #if defined(TARGET_I386)
1445 /* NOTE: there is really one LDT for all the threads */
1446 uint8_t *ldt_table;
1448 static int read_ldt(target_ulong ptr, unsigned long bytecount)
1450 int size;
1451 void *p;
1453 if (!ldt_table)
1454 return 0;
1455 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
1456 if (size > bytecount)
1457 size = bytecount;
1458 p = lock_user(ptr, size, 0);
1459 /* ??? Shoudl this by byteswapped? */
1460 memcpy(p, ldt_table, size);
1461 unlock_user(p, ptr, size);
1462 return size;
1465 /* XXX: add locking support */
1466 static int write_ldt(CPUX86State *env,
1467 target_ulong ptr, unsigned long bytecount, int oldmode)
1469 struct target_modify_ldt_ldt_s ldt_info;
1470 struct target_modify_ldt_ldt_s *target_ldt_info;
1471 int seg_32bit, contents, read_exec_only, limit_in_pages;
1472 int seg_not_present, useable;
1473 uint32_t *lp, entry_1, entry_2;
1475 if (bytecount != sizeof(ldt_info))
1476 return -EINVAL;
1477 lock_user_struct(target_ldt_info, ptr, 1);
1478 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
1479 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
1480 ldt_info.limit = tswap32(target_ldt_info->limit);
1481 ldt_info.flags = tswap32(target_ldt_info->flags);
1482 unlock_user_struct(target_ldt_info, ptr, 0);
1484 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
1485 return -EINVAL;
1486 seg_32bit = ldt_info.flags & 1;
1487 contents = (ldt_info.flags >> 1) & 3;
1488 read_exec_only = (ldt_info.flags >> 3) & 1;
1489 limit_in_pages = (ldt_info.flags >> 4) & 1;
1490 seg_not_present = (ldt_info.flags >> 5) & 1;
1491 useable = (ldt_info.flags >> 6) & 1;
1493 if (contents == 3) {
1494 if (oldmode)
1495 return -EINVAL;
1496 if (seg_not_present == 0)
1497 return -EINVAL;
1499 /* allocate the LDT */
1500 if (!ldt_table) {
1501 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1502 if (!ldt_table)
1503 return -ENOMEM;
1504 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1505 env->ldt.base = h2g(ldt_table);
1506 env->ldt.limit = 0xffff;
1509 /* NOTE: same code as Linux kernel */
1510 /* Allow LDTs to be cleared by the user. */
1511 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
1512 if (oldmode ||
1513 (contents == 0 &&
1514 read_exec_only == 1 &&
1515 seg_32bit == 0 &&
1516 limit_in_pages == 0 &&
1517 seg_not_present == 1 &&
1518 useable == 0 )) {
1519 entry_1 = 0;
1520 entry_2 = 0;
1521 goto install;
1525 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
1526 (ldt_info.limit & 0x0ffff);
1527 entry_2 = (ldt_info.base_addr & 0xff000000) |
1528 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
1529 (ldt_info.limit & 0xf0000) |
1530 ((read_exec_only ^ 1) << 9) |
1531 (contents << 10) |
1532 ((seg_not_present ^ 1) << 15) |
1533 (seg_32bit << 22) |
1534 (limit_in_pages << 23) |
1535 0x7000;
1536 if (!oldmode)
1537 entry_2 |= (useable << 20);
1539 /* Install the new entry ... */
1540 install:
1541 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
1542 lp[0] = tswap32(entry_1);
1543 lp[1] = tswap32(entry_2);
1544 return 0;
1547 /* specific and weird i386 syscalls */
1548 int do_modify_ldt(CPUX86State *env, int func, target_ulong ptr, unsigned long bytecount)
1550 int ret = -ENOSYS;
1552 switch (func) {
1553 case 0:
1554 ret = read_ldt(ptr, bytecount);
1555 break;
1556 case 1:
1557 ret = write_ldt(env, ptr, bytecount, 1);
1558 break;
1559 case 0x11:
1560 ret = write_ldt(env, ptr, bytecount, 0);
1561 break;
1563 return ret;
1566 #endif /* defined(TARGET_I386) */
1568 /* this stack is the equivalent of the kernel stack associated with a
1569 thread/process */
1570 #define NEW_STACK_SIZE 8192
1572 static int clone_func(void *arg)
1574 CPUState *env = arg;
1575 cpu_loop(env);
1576 /* never exits */
1577 return 0;
1580 int do_fork(CPUState *env, unsigned int flags, unsigned long newsp)
1582 int ret;
1583 TaskState *ts;
1584 uint8_t *new_stack;
1585 CPUState *new_env;
1587 if (flags & CLONE_VM) {
1588 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
1589 memset(ts, 0, sizeof(TaskState));
1590 new_stack = ts->stack;
1591 ts->used = 1;
1592 /* add in task state list */
1593 ts->next = first_task_state;
1594 first_task_state = ts;
1595 /* we create a new CPU instance. */
1596 new_env = cpu_init();
1597 memcpy(new_env, env, sizeof(CPUState));
1598 #if defined(TARGET_I386)
1599 if (!newsp)
1600 newsp = env->regs[R_ESP];
1601 new_env->regs[R_ESP] = newsp;
1602 new_env->regs[R_EAX] = 0;
1603 #elif defined(TARGET_ARM)
1604 if (!newsp)
1605 newsp = env->regs[13];
1606 new_env->regs[13] = newsp;
1607 new_env->regs[0] = 0;
1608 #elif defined(TARGET_SPARC)
1609 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
1610 #elif defined(TARGET_MIPS)
1611 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
1612 #elif defined(TARGET_PPC)
1613 if (!newsp)
1614 newsp = env->gpr[1];
1615 new_env->gpr[1] = newsp;
1617 int i;
1618 for (i = 7; i < 32; i++)
1619 new_env->gpr[i] = 0;
1621 #else
1622 #error unsupported target CPU
1623 #endif
1624 new_env->opaque = ts;
1625 #ifdef __ia64__
1626 ret = clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1627 #else
1628 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1629 #endif
1630 } else {
1631 /* if no CLONE_VM, we consider it is a fork */
1632 if ((flags & ~CSIGNAL) != 0)
1633 return -EINVAL;
1634 ret = fork();
1636 return ret;
1639 static long do_fcntl(int fd, int cmd, target_ulong arg)
1641 struct flock fl;
1642 struct target_flock *target_fl;
1643 long ret;
1645 switch(cmd) {
1646 case TARGET_F_GETLK:
1647 ret = fcntl(fd, cmd, &fl);
1648 if (ret == 0) {
1649 lock_user_struct(target_fl, arg, 0);
1650 target_fl->l_type = tswap16(fl.l_type);
1651 target_fl->l_whence = tswap16(fl.l_whence);
1652 target_fl->l_start = tswapl(fl.l_start);
1653 target_fl->l_len = tswapl(fl.l_len);
1654 target_fl->l_pid = tswapl(fl.l_pid);
1655 unlock_user_struct(target_fl, arg, 1);
1657 break;
1659 case TARGET_F_SETLK:
1660 case TARGET_F_SETLKW:
1661 lock_user_struct(target_fl, arg, 1);
1662 fl.l_type = tswap16(target_fl->l_type);
1663 fl.l_whence = tswap16(target_fl->l_whence);
1664 fl.l_start = tswapl(target_fl->l_start);
1665 fl.l_len = tswapl(target_fl->l_len);
1666 fl.l_pid = tswapl(target_fl->l_pid);
1667 unlock_user_struct(target_fl, arg, 0);
1668 ret = fcntl(fd, cmd, &fl);
1669 break;
1671 case TARGET_F_GETLK64:
1672 case TARGET_F_SETLK64:
1673 case TARGET_F_SETLKW64:
1674 ret = -1;
1675 errno = EINVAL;
1676 break;
1678 case F_GETFL:
1679 ret = fcntl(fd, cmd, arg);
1680 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
1681 break;
1683 case F_SETFL:
1684 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
1685 break;
1687 default:
1688 ret = fcntl(fd, cmd, arg);
1689 break;
1691 return ret;
1694 #ifdef USE_UID16
1696 static inline int high2lowuid(int uid)
1698 if (uid > 65535)
1699 return 65534;
1700 else
1701 return uid;
1704 static inline int high2lowgid(int gid)
1706 if (gid > 65535)
1707 return 65534;
1708 else
1709 return gid;
1712 static inline int low2highuid(int uid)
1714 if ((int16_t)uid == -1)
1715 return -1;
1716 else
1717 return uid;
1720 static inline int low2highgid(int gid)
1722 if ((int16_t)gid == -1)
1723 return -1;
1724 else
1725 return gid;
1728 #endif /* USE_UID16 */
1730 void syscall_init(void)
1732 IOCTLEntry *ie;
1733 const argtype *arg_type;
1734 int size;
1736 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
1737 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
1738 #include "syscall_types.h"
1739 #undef STRUCT
1740 #undef STRUCT_SPECIAL
1742 /* we patch the ioctl size if necessary. We rely on the fact that
1743 no ioctl has all the bits at '1' in the size field */
1744 ie = ioctl_entries;
1745 while (ie->target_cmd != 0) {
1746 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
1747 TARGET_IOC_SIZEMASK) {
1748 arg_type = ie->arg_type;
1749 if (arg_type[0] != TYPE_PTR) {
1750 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
1751 ie->target_cmd);
1752 exit(1);
1754 arg_type++;
1755 size = thunk_type_size(arg_type, 0);
1756 ie->target_cmd = (ie->target_cmd &
1757 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
1758 (size << TARGET_IOC_SIZESHIFT);
1760 /* automatic consistency check if same arch */
1761 #if defined(__i386__) && defined(TARGET_I386)
1762 if (ie->target_cmd != ie->host_cmd) {
1763 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
1764 ie->target_cmd, ie->host_cmd);
1766 #endif
1767 ie++;
1771 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
1773 #ifdef TARGET_WORDS_BIG_ENDIAN
1774 return ((uint64_t)word0 << 32) | word1;
1775 #else
1776 return ((uint64_t)word1 << 32) | word0;
1777 #endif
1780 #ifdef TARGET_NR_truncate64
1781 static inline long target_truncate64(void *cpu_env, const char *arg1,
1782 long arg2, long arg3, long arg4)
1784 #ifdef TARGET_ARM
1785 if (((CPUARMState *)cpu_env)->eabi)
1787 arg2 = arg3;
1788 arg3 = arg4;
1790 #endif
1791 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
1793 #endif
1795 #ifdef TARGET_NR_ftruncate64
1796 static inline long target_ftruncate64(void *cpu_env, long arg1, long arg2,
1797 long arg3, long arg4)
1799 #ifdef TARGET_ARM
1800 if (((CPUARMState *)cpu_env)->eabi)
1802 arg2 = arg3;
1803 arg3 = arg4;
1805 #endif
1806 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
1808 #endif
1810 static inline void target_to_host_timespec(struct timespec *host_ts,
1811 target_ulong target_addr)
1813 struct target_timespec *target_ts;
1815 lock_user_struct(target_ts, target_addr, 1);
1816 host_ts->tv_sec = tswapl(target_ts->tv_sec);
1817 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
1818 unlock_user_struct(target_ts, target_addr, 0);
1821 static inline void host_to_target_timespec(target_ulong target_addr,
1822 struct timespec *host_ts)
1824 struct target_timespec *target_ts;
1826 lock_user_struct(target_ts, target_addr, 0);
1827 target_ts->tv_sec = tswapl(host_ts->tv_sec);
1828 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
1829 unlock_user_struct(target_ts, target_addr, 1);
1832 long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
1833 long arg4, long arg5, long arg6)
1835 long ret;
1836 struct stat st;
1837 struct statfs stfs;
1838 void *p;
1840 #ifdef DEBUG
1841 gemu_log("syscall %d", num);
1842 #endif
1843 switch(num) {
1844 case TARGET_NR_exit:
1845 #ifdef HAVE_GPROF
1846 _mcleanup();
1847 #endif
1848 gdb_exit(cpu_env, arg1);
1849 /* XXX: should free thread stack and CPU env */
1850 _exit(arg1);
1851 ret = 0; /* avoid warning */
1852 break;
1853 case TARGET_NR_read:
1854 page_unprotect_range(arg2, arg3);
1855 p = lock_user(arg2, arg3, 0);
1856 ret = get_errno(read(arg1, p, arg3));
1857 unlock_user(p, arg2, ret);
1858 break;
1859 case TARGET_NR_write:
1860 p = lock_user(arg2, arg3, 1);
1861 ret = get_errno(write(arg1, p, arg3));
1862 unlock_user(p, arg2, 0);
1863 break;
1864 case TARGET_NR_open:
1865 p = lock_user_string(arg1);
1866 ret = get_errno(open(path(p),
1867 target_to_host_bitmask(arg2, fcntl_flags_tbl),
1868 arg3));
1869 unlock_user(p, arg1, 0);
1870 break;
1871 case TARGET_NR_close:
1872 ret = get_errno(close(arg1));
1873 break;
1874 case TARGET_NR_brk:
1875 ret = do_brk(arg1);
1876 break;
1877 case TARGET_NR_fork:
1878 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
1879 break;
1880 case TARGET_NR_waitpid:
1882 int status;
1883 ret = get_errno(waitpid(arg1, &status, arg3));
1884 if (!is_error(ret) && arg2)
1885 tput32(arg2, status);
1887 break;
1888 case TARGET_NR_creat:
1889 p = lock_user_string(arg1);
1890 ret = get_errno(creat(p, arg2));
1891 unlock_user(p, arg1, 0);
1892 break;
1893 case TARGET_NR_link:
1895 void * p2;
1896 p = lock_user_string(arg1);
1897 p2 = lock_user_string(arg2);
1898 ret = get_errno(link(p, p2));
1899 unlock_user(p2, arg2, 0);
1900 unlock_user(p, arg1, 0);
1902 break;
1903 case TARGET_NR_unlink:
1904 p = lock_user_string(arg1);
1905 ret = get_errno(unlink(p));
1906 unlock_user(p, arg1, 0);
1907 break;
1908 case TARGET_NR_execve:
1910 char **argp, **envp;
1911 int argc, envc;
1912 target_ulong gp;
1913 target_ulong guest_argp;
1914 target_ulong guest_envp;
1915 target_ulong addr;
1916 char **q;
1918 argc = 0;
1919 guest_argp = arg2;
1920 for (gp = guest_argp; tgetl(gp); gp++)
1921 argc++;
1922 envc = 0;
1923 guest_envp = arg3;
1924 for (gp = guest_envp; tgetl(gp); gp++)
1925 envc++;
1927 argp = alloca((argc + 1) * sizeof(void *));
1928 envp = alloca((envc + 1) * sizeof(void *));
1930 for (gp = guest_argp, q = argp; ;
1931 gp += sizeof(target_ulong), q++) {
1932 addr = tgetl(gp);
1933 if (!addr)
1934 break;
1935 *q = lock_user_string(addr);
1937 *q = NULL;
1939 for (gp = guest_envp, q = envp; ;
1940 gp += sizeof(target_ulong), q++) {
1941 addr = tgetl(gp);
1942 if (!addr)
1943 break;
1944 *q = lock_user_string(addr);
1946 *q = NULL;
1948 p = lock_user_string(arg1);
1949 ret = get_errno(execve(p, argp, envp));
1950 unlock_user(p, arg1, 0);
1952 for (gp = guest_argp, q = argp; *q;
1953 gp += sizeof(target_ulong), q++) {
1954 addr = tgetl(gp);
1955 unlock_user(*q, addr, 0);
1957 for (gp = guest_envp, q = envp; *q;
1958 gp += sizeof(target_ulong), q++) {
1959 addr = tgetl(gp);
1960 unlock_user(*q, addr, 0);
1963 break;
1964 case TARGET_NR_chdir:
1965 p = lock_user_string(arg1);
1966 ret = get_errno(chdir(p));
1967 unlock_user(p, arg1, 0);
1968 break;
1969 #ifdef TARGET_NR_time
1970 case TARGET_NR_time:
1972 time_t host_time;
1973 ret = get_errno(time(&host_time));
1974 if (!is_error(ret) && arg1)
1975 tputl(arg1, host_time);
1977 break;
1978 #endif
1979 case TARGET_NR_mknod:
1980 p = lock_user_string(arg1);
1981 ret = get_errno(mknod(p, arg2, arg3));
1982 unlock_user(p, arg1, 0);
1983 break;
1984 case TARGET_NR_chmod:
1985 p = lock_user_string(arg1);
1986 ret = get_errno(chmod(p, arg2));
1987 unlock_user(p, arg1, 0);
1988 break;
1989 #ifdef TARGET_NR_break
1990 case TARGET_NR_break:
1991 goto unimplemented;
1992 #endif
1993 #ifdef TARGET_NR_oldstat
1994 case TARGET_NR_oldstat:
1995 goto unimplemented;
1996 #endif
1997 case TARGET_NR_lseek:
1998 ret = get_errno(lseek(arg1, arg2, arg3));
1999 break;
2000 case TARGET_NR_getpid:
2001 ret = get_errno(getpid());
2002 break;
2003 case TARGET_NR_mount:
2004 /* need to look at the data field */
2005 goto unimplemented;
2006 case TARGET_NR_umount:
2007 p = lock_user_string(arg1);
2008 ret = get_errno(umount(p));
2009 unlock_user(p, arg1, 0);
2010 break;
2011 case TARGET_NR_stime:
2013 time_t host_time;
2014 host_time = tgetl(arg1);
2015 ret = get_errno(stime(&host_time));
2017 break;
2018 case TARGET_NR_ptrace:
2019 goto unimplemented;
2020 case TARGET_NR_alarm:
2021 ret = alarm(arg1);
2022 break;
2023 #ifdef TARGET_NR_oldfstat
2024 case TARGET_NR_oldfstat:
2025 goto unimplemented;
2026 #endif
2027 case TARGET_NR_pause:
2028 ret = get_errno(pause());
2029 break;
2030 case TARGET_NR_utime:
2032 struct utimbuf tbuf, *host_tbuf;
2033 struct target_utimbuf *target_tbuf;
2034 if (arg2) {
2035 lock_user_struct(target_tbuf, arg2, 1);
2036 tbuf.actime = tswapl(target_tbuf->actime);
2037 tbuf.modtime = tswapl(target_tbuf->modtime);
2038 unlock_user_struct(target_tbuf, arg2, 0);
2039 host_tbuf = &tbuf;
2040 } else {
2041 host_tbuf = NULL;
2043 p = lock_user_string(arg1);
2044 ret = get_errno(utime(p, host_tbuf));
2045 unlock_user(p, arg1, 0);
2047 break;
2048 case TARGET_NR_utimes:
2050 struct timeval *tvp, tv[2];
2051 if (arg2) {
2052 target_to_host_timeval(&tv[0], arg2);
2053 target_to_host_timeval(&tv[1],
2054 arg2 + sizeof (struct target_timeval));
2055 tvp = tv;
2056 } else {
2057 tvp = NULL;
2059 p = lock_user_string(arg1);
2060 ret = get_errno(utimes(p, tvp));
2061 unlock_user(p, arg1, 0);
2063 break;
2064 #ifdef TARGET_NR_stty
2065 case TARGET_NR_stty:
2066 goto unimplemented;
2067 #endif
2068 #ifdef TARGET_NR_gtty
2069 case TARGET_NR_gtty:
2070 goto unimplemented;
2071 #endif
2072 case TARGET_NR_access:
2073 p = lock_user_string(arg1);
2074 ret = get_errno(access(p, arg2));
2075 unlock_user(p, arg1, 0);
2076 break;
2077 case TARGET_NR_nice:
2078 ret = get_errno(nice(arg1));
2079 break;
2080 #ifdef TARGET_NR_ftime
2081 case TARGET_NR_ftime:
2082 goto unimplemented;
2083 #endif
2084 case TARGET_NR_sync:
2085 sync();
2086 ret = 0;
2087 break;
2088 case TARGET_NR_kill:
2089 ret = get_errno(kill(arg1, arg2));
2090 break;
2091 case TARGET_NR_rename:
2093 void *p2;
2094 p = lock_user_string(arg1);
2095 p2 = lock_user_string(arg2);
2096 ret = get_errno(rename(p, p2));
2097 unlock_user(p2, arg2, 0);
2098 unlock_user(p, arg1, 0);
2100 break;
2101 case TARGET_NR_mkdir:
2102 p = lock_user_string(arg1);
2103 ret = get_errno(mkdir(p, arg2));
2104 unlock_user(p, arg1, 0);
2105 break;
2106 case TARGET_NR_rmdir:
2107 p = lock_user_string(arg1);
2108 ret = get_errno(rmdir(p));
2109 unlock_user(p, arg1, 0);
2110 break;
2111 case TARGET_NR_dup:
2112 ret = get_errno(dup(arg1));
2113 break;
2114 case TARGET_NR_pipe:
2116 int host_pipe[2];
2117 ret = get_errno(pipe(host_pipe));
2118 if (!is_error(ret)) {
2119 tput32(arg1, host_pipe[0]);
2120 tput32(arg1 + 4, host_pipe[1]);
2123 break;
2124 case TARGET_NR_times:
2126 struct target_tms *tmsp;
2127 struct tms tms;
2128 ret = get_errno(times(&tms));
2129 if (arg1) {
2130 tmsp = lock_user(arg1, sizeof(struct target_tms), 0);
2131 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
2132 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
2133 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
2134 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
2136 if (!is_error(ret))
2137 ret = host_to_target_clock_t(ret);
2139 break;
2140 #ifdef TARGET_NR_prof
2141 case TARGET_NR_prof:
2142 goto unimplemented;
2143 #endif
2144 case TARGET_NR_signal:
2145 goto unimplemented;
2147 case TARGET_NR_acct:
2148 goto unimplemented;
2149 case TARGET_NR_umount2:
2150 p = lock_user_string(arg1);
2151 ret = get_errno(umount2(p, arg2));
2152 unlock_user(p, arg1, 0);
2153 break;
2154 #ifdef TARGET_NR_lock
2155 case TARGET_NR_lock:
2156 goto unimplemented;
2157 #endif
2158 case TARGET_NR_ioctl:
2159 ret = do_ioctl(arg1, arg2, arg3);
2160 break;
2161 case TARGET_NR_fcntl:
2162 ret = get_errno(do_fcntl(arg1, arg2, arg3));
2163 break;
2164 #ifdef TARGET_NR_mpx
2165 case TARGET_NR_mpx:
2166 goto unimplemented;
2167 #endif
2168 case TARGET_NR_setpgid:
2169 ret = get_errno(setpgid(arg1, arg2));
2170 break;
2171 #ifdef TARGET_NR_ulimit
2172 case TARGET_NR_ulimit:
2173 goto unimplemented;
2174 #endif
2175 #ifdef TARGET_NR_oldolduname
2176 case TARGET_NR_oldolduname:
2177 goto unimplemented;
2178 #endif
2179 case TARGET_NR_umask:
2180 ret = get_errno(umask(arg1));
2181 break;
2182 case TARGET_NR_chroot:
2183 p = lock_user_string(arg1);
2184 ret = get_errno(chroot(p));
2185 unlock_user(p, arg1, 0);
2186 break;
2187 case TARGET_NR_ustat:
2188 goto unimplemented;
2189 case TARGET_NR_dup2:
2190 ret = get_errno(dup2(arg1, arg2));
2191 break;
2192 case TARGET_NR_getppid:
2193 ret = get_errno(getppid());
2194 break;
2195 case TARGET_NR_getpgrp:
2196 ret = get_errno(getpgrp());
2197 break;
2198 case TARGET_NR_setsid:
2199 ret = get_errno(setsid());
2200 break;
2201 case TARGET_NR_sigaction:
2203 struct target_old_sigaction *old_act;
2204 struct target_sigaction act, oact, *pact;
2205 if (arg2) {
2206 lock_user_struct(old_act, arg2, 1);
2207 act._sa_handler = old_act->_sa_handler;
2208 target_siginitset(&act.sa_mask, old_act->sa_mask);
2209 act.sa_flags = old_act->sa_flags;
2210 act.sa_restorer = old_act->sa_restorer;
2211 unlock_user_struct(old_act, arg2, 0);
2212 pact = &act;
2213 } else {
2214 pact = NULL;
2216 ret = get_errno(do_sigaction(arg1, pact, &oact));
2217 if (!is_error(ret) && arg3) {
2218 lock_user_struct(old_act, arg3, 0);
2219 old_act->_sa_handler = oact._sa_handler;
2220 old_act->sa_mask = oact.sa_mask.sig[0];
2221 old_act->sa_flags = oact.sa_flags;
2222 old_act->sa_restorer = oact.sa_restorer;
2223 unlock_user_struct(old_act, arg3, 1);
2226 break;
2227 case TARGET_NR_rt_sigaction:
2229 struct target_sigaction *act;
2230 struct target_sigaction *oact;
2232 if (arg2)
2233 lock_user_struct(act, arg2, 1);
2234 else
2235 act = NULL;
2236 if (arg3)
2237 lock_user_struct(oact, arg3, 0);
2238 else
2239 oact = NULL;
2240 ret = get_errno(do_sigaction(arg1, act, oact));
2241 if (arg2)
2242 unlock_user_struct(act, arg2, 0);
2243 if (arg3)
2244 unlock_user_struct(oact, arg3, 1);
2246 break;
2247 case TARGET_NR_sgetmask:
2249 sigset_t cur_set;
2250 target_ulong target_set;
2251 sigprocmask(0, NULL, &cur_set);
2252 host_to_target_old_sigset(&target_set, &cur_set);
2253 ret = target_set;
2255 break;
2256 case TARGET_NR_ssetmask:
2258 sigset_t set, oset, cur_set;
2259 target_ulong target_set = arg1;
2260 sigprocmask(0, NULL, &cur_set);
2261 target_to_host_old_sigset(&set, &target_set);
2262 sigorset(&set, &set, &cur_set);
2263 sigprocmask(SIG_SETMASK, &set, &oset);
2264 host_to_target_old_sigset(&target_set, &oset);
2265 ret = target_set;
2267 break;
2268 case TARGET_NR_sigprocmask:
2270 int how = arg1;
2271 sigset_t set, oldset, *set_ptr;
2273 if (arg2) {
2274 switch(how) {
2275 case TARGET_SIG_BLOCK:
2276 how = SIG_BLOCK;
2277 break;
2278 case TARGET_SIG_UNBLOCK:
2279 how = SIG_UNBLOCK;
2280 break;
2281 case TARGET_SIG_SETMASK:
2282 how = SIG_SETMASK;
2283 break;
2284 default:
2285 ret = -EINVAL;
2286 goto fail;
2288 p = lock_user(arg2, sizeof(target_sigset_t), 1);
2289 target_to_host_old_sigset(&set, p);
2290 unlock_user(p, arg2, 0);
2291 set_ptr = &set;
2292 } else {
2293 how = 0;
2294 set_ptr = NULL;
2296 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
2297 if (!is_error(ret) && arg3) {
2298 p = lock_user(arg3, sizeof(target_sigset_t), 0);
2299 host_to_target_old_sigset(p, &oldset);
2300 unlock_user(p, arg3, sizeof(target_sigset_t));
2303 break;
2304 case TARGET_NR_rt_sigprocmask:
2306 int how = arg1;
2307 sigset_t set, oldset, *set_ptr;
2309 if (arg2) {
2310 switch(how) {
2311 case TARGET_SIG_BLOCK:
2312 how = SIG_BLOCK;
2313 break;
2314 case TARGET_SIG_UNBLOCK:
2315 how = SIG_UNBLOCK;
2316 break;
2317 case TARGET_SIG_SETMASK:
2318 how = SIG_SETMASK;
2319 break;
2320 default:
2321 ret = -EINVAL;
2322 goto fail;
2324 p = lock_user(arg2, sizeof(target_sigset_t), 1);
2325 target_to_host_sigset(&set, p);
2326 unlock_user(p, arg2, 0);
2327 set_ptr = &set;
2328 } else {
2329 how = 0;
2330 set_ptr = NULL;
2332 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
2333 if (!is_error(ret) && arg3) {
2334 p = lock_user(arg3, sizeof(target_sigset_t), 0);
2335 host_to_target_sigset(p, &oldset);
2336 unlock_user(p, arg3, sizeof(target_sigset_t));
2339 break;
2340 case TARGET_NR_sigpending:
2342 sigset_t set;
2343 ret = get_errno(sigpending(&set));
2344 if (!is_error(ret)) {
2345 p = lock_user(arg1, sizeof(target_sigset_t), 0);
2346 host_to_target_old_sigset(p, &set);
2347 unlock_user(p, arg1, sizeof(target_sigset_t));
2350 break;
2351 case TARGET_NR_rt_sigpending:
2353 sigset_t set;
2354 ret = get_errno(sigpending(&set));
2355 if (!is_error(ret)) {
2356 p = lock_user(arg1, sizeof(target_sigset_t), 0);
2357 host_to_target_sigset(p, &set);
2358 unlock_user(p, arg1, sizeof(target_sigset_t));
2361 break;
2362 case TARGET_NR_sigsuspend:
2364 sigset_t set;
2365 p = lock_user(arg1, sizeof(target_sigset_t), 1);
2366 target_to_host_old_sigset(&set, p);
2367 unlock_user(p, arg1, 0);
2368 ret = get_errno(sigsuspend(&set));
2370 break;
2371 case TARGET_NR_rt_sigsuspend:
2373 sigset_t set;
2374 p = lock_user(arg1, sizeof(target_sigset_t), 1);
2375 target_to_host_sigset(&set, p);
2376 unlock_user(p, arg1, 0);
2377 ret = get_errno(sigsuspend(&set));
2379 break;
2380 case TARGET_NR_rt_sigtimedwait:
2382 sigset_t set;
2383 struct timespec uts, *puts;
2384 siginfo_t uinfo;
2386 p = lock_user(arg1, sizeof(target_sigset_t), 1);
2387 target_to_host_sigset(&set, p);
2388 unlock_user(p, arg1, 0);
2389 if (arg3) {
2390 puts = &uts;
2391 target_to_host_timespec(puts, arg3);
2392 } else {
2393 puts = NULL;
2395 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
2396 if (!is_error(ret) && arg2) {
2397 p = lock_user(arg2, sizeof(target_sigset_t), 0);
2398 host_to_target_siginfo(p, &uinfo);
2399 unlock_user(p, arg2, sizeof(target_sigset_t));
2402 break;
2403 case TARGET_NR_rt_sigqueueinfo:
2405 siginfo_t uinfo;
2406 p = lock_user(arg3, sizeof(target_sigset_t), 1);
2407 target_to_host_siginfo(&uinfo, p);
2408 unlock_user(p, arg1, 0);
2409 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
2411 break;
2412 case TARGET_NR_sigreturn:
2413 /* NOTE: ret is eax, so not transcoding must be done */
2414 ret = do_sigreturn(cpu_env);
2415 break;
2416 case TARGET_NR_rt_sigreturn:
2417 /* NOTE: ret is eax, so not transcoding must be done */
2418 ret = do_rt_sigreturn(cpu_env);
2419 break;
2420 case TARGET_NR_sethostname:
2421 p = lock_user_string(arg1);
2422 ret = get_errno(sethostname(p, arg2));
2423 unlock_user(p, arg1, 0);
2424 break;
2425 case TARGET_NR_setrlimit:
2427 /* XXX: convert resource ? */
2428 int resource = arg1;
2429 struct target_rlimit *target_rlim;
2430 struct rlimit rlim;
2431 lock_user_struct(target_rlim, arg2, 1);
2432 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
2433 rlim.rlim_max = tswapl(target_rlim->rlim_max);
2434 unlock_user_struct(target_rlim, arg2, 0);
2435 ret = get_errno(setrlimit(resource, &rlim));
2437 break;
2438 case TARGET_NR_getrlimit:
2440 /* XXX: convert resource ? */
2441 int resource = arg1;
2442 struct target_rlimit *target_rlim;
2443 struct rlimit rlim;
2445 ret = get_errno(getrlimit(resource, &rlim));
2446 if (!is_error(ret)) {
2447 lock_user_struct(target_rlim, arg2, 0);
2448 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
2449 rlim.rlim_max = tswapl(target_rlim->rlim_max);
2450 unlock_user_struct(target_rlim, arg2, 1);
2453 break;
2454 case TARGET_NR_getrusage:
2456 struct rusage rusage;
2457 ret = get_errno(getrusage(arg1, &rusage));
2458 if (!is_error(ret)) {
2459 host_to_target_rusage(arg2, &rusage);
2462 break;
2463 case TARGET_NR_gettimeofday:
2465 struct timeval tv;
2466 ret = get_errno(gettimeofday(&tv, NULL));
2467 if (!is_error(ret)) {
2468 host_to_target_timeval(arg1, &tv);
2471 break;
2472 case TARGET_NR_settimeofday:
2474 struct timeval tv;
2475 target_to_host_timeval(&tv, arg1);
2476 ret = get_errno(settimeofday(&tv, NULL));
2478 break;
2479 #ifdef TARGET_NR_select
2480 case TARGET_NR_select:
2482 struct target_sel_arg_struct *sel;
2483 target_ulong inp, outp, exp, tvp;
2484 long nsel;
2486 lock_user_struct(sel, arg1, 1);
2487 nsel = tswapl(sel->n);
2488 inp = tswapl(sel->inp);
2489 outp = tswapl(sel->outp);
2490 exp = tswapl(sel->exp);
2491 tvp = tswapl(sel->tvp);
2492 unlock_user_struct(sel, arg1, 0);
2493 ret = do_select(nsel, inp, outp, exp, tvp);
2495 break;
2496 #endif
2497 case TARGET_NR_symlink:
2499 void *p2;
2500 p = lock_user_string(arg1);
2501 p2 = lock_user_string(arg2);
2502 ret = get_errno(symlink(p, p2));
2503 unlock_user(p2, arg2, 0);
2504 unlock_user(p, arg1, 0);
2506 break;
2507 #ifdef TARGET_NR_oldlstat
2508 case TARGET_NR_oldlstat:
2509 goto unimplemented;
2510 #endif
2511 case TARGET_NR_readlink:
2513 void *p2;
2514 p = lock_user_string(arg1);
2515 p2 = lock_user(arg2, arg3, 0);
2516 ret = get_errno(readlink(path(p), p2, arg3));
2517 unlock_user(p2, arg2, ret);
2518 unlock_user(p, arg1, 0);
2520 break;
2521 case TARGET_NR_uselib:
2522 goto unimplemented;
2523 case TARGET_NR_swapon:
2524 p = lock_user_string(arg1);
2525 ret = get_errno(swapon(p, arg2));
2526 unlock_user(p, arg1, 0);
2527 break;
2528 case TARGET_NR_reboot:
2529 goto unimplemented;
2530 case TARGET_NR_readdir:
2531 goto unimplemented;
2532 case TARGET_NR_mmap:
2533 #if defined(TARGET_I386) || defined(TARGET_ARM)
2535 target_ulong *v;
2536 target_ulong v1, v2, v3, v4, v5, v6;
2537 v = lock_user(arg1, 6 * sizeof(target_ulong), 1);
2538 v1 = tswapl(v[0]);
2539 v2 = tswapl(v[1]);
2540 v3 = tswapl(v[2]);
2541 v4 = tswapl(v[3]);
2542 v5 = tswapl(v[4]);
2543 v6 = tswapl(v[5]);
2544 unlock_user(v, arg1, 0);
2545 ret = get_errno(target_mmap(v1, v2, v3,
2546 target_to_host_bitmask(v4, mmap_flags_tbl),
2547 v5, v6));
2549 #else
2550 ret = get_errno(target_mmap(arg1, arg2, arg3,
2551 target_to_host_bitmask(arg4, mmap_flags_tbl),
2552 arg5,
2553 arg6));
2554 #endif
2555 break;
2556 #ifdef TARGET_NR_mmap2
2557 case TARGET_NR_mmap2:
2558 #if defined(TARGET_SPARC)
2559 #define MMAP_SHIFT 12
2560 #else
2561 #define MMAP_SHIFT TARGET_PAGE_BITS
2562 #endif
2563 ret = get_errno(target_mmap(arg1, arg2, arg3,
2564 target_to_host_bitmask(arg4, mmap_flags_tbl),
2565 arg5,
2566 arg6 << MMAP_SHIFT));
2567 break;
2568 #endif
2569 case TARGET_NR_munmap:
2570 ret = get_errno(target_munmap(arg1, arg2));
2571 break;
2572 case TARGET_NR_mprotect:
2573 ret = get_errno(target_mprotect(arg1, arg2, arg3));
2574 break;
2575 case TARGET_NR_mremap:
2576 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
2577 break;
2578 /* ??? msync/mlock/munlock are broken for softmmu. */
2579 case TARGET_NR_msync:
2580 ret = get_errno(msync(g2h(arg1), arg2, arg3));
2581 break;
2582 case TARGET_NR_mlock:
2583 ret = get_errno(mlock(g2h(arg1), arg2));
2584 break;
2585 case TARGET_NR_munlock:
2586 ret = get_errno(munlock(g2h(arg1), arg2));
2587 break;
2588 case TARGET_NR_mlockall:
2589 ret = get_errno(mlockall(arg1));
2590 break;
2591 case TARGET_NR_munlockall:
2592 ret = get_errno(munlockall());
2593 break;
2594 case TARGET_NR_truncate:
2595 p = lock_user_string(arg1);
2596 ret = get_errno(truncate(p, arg2));
2597 unlock_user(p, arg1, 0);
2598 break;
2599 case TARGET_NR_ftruncate:
2600 ret = get_errno(ftruncate(arg1, arg2));
2601 break;
2602 case TARGET_NR_fchmod:
2603 ret = get_errno(fchmod(arg1, arg2));
2604 break;
2605 case TARGET_NR_getpriority:
2606 ret = get_errno(getpriority(arg1, arg2));
2607 break;
2608 case TARGET_NR_setpriority:
2609 ret = get_errno(setpriority(arg1, arg2, arg3));
2610 break;
2611 #ifdef TARGET_NR_profil
2612 case TARGET_NR_profil:
2613 goto unimplemented;
2614 #endif
2615 case TARGET_NR_statfs:
2616 p = lock_user_string(arg1);
2617 ret = get_errno(statfs(path(p), &stfs));
2618 unlock_user(p, arg1, 0);
2619 convert_statfs:
2620 if (!is_error(ret)) {
2621 struct target_statfs *target_stfs;
2623 lock_user_struct(target_stfs, arg2, 0);
2624 /* ??? put_user is probably wrong. */
2625 put_user(stfs.f_type, &target_stfs->f_type);
2626 put_user(stfs.f_bsize, &target_stfs->f_bsize);
2627 put_user(stfs.f_blocks, &target_stfs->f_blocks);
2628 put_user(stfs.f_bfree, &target_stfs->f_bfree);
2629 put_user(stfs.f_bavail, &target_stfs->f_bavail);
2630 put_user(stfs.f_files, &target_stfs->f_files);
2631 put_user(stfs.f_ffree, &target_stfs->f_ffree);
2632 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid);
2633 put_user(stfs.f_namelen, &target_stfs->f_namelen);
2634 unlock_user_struct(target_stfs, arg2, 1);
2636 break;
2637 case TARGET_NR_fstatfs:
2638 ret = get_errno(fstatfs(arg1, &stfs));
2639 goto convert_statfs;
2640 #ifdef TARGET_NR_statfs64
2641 case TARGET_NR_statfs64:
2642 p = lock_user_string(arg1);
2643 ret = get_errno(statfs(path(p), &stfs));
2644 unlock_user(p, arg1, 0);
2645 convert_statfs64:
2646 if (!is_error(ret)) {
2647 struct target_statfs64 *target_stfs;
2649 lock_user_struct(target_stfs, arg3, 0);
2650 /* ??? put_user is probably wrong. */
2651 put_user(stfs.f_type, &target_stfs->f_type);
2652 put_user(stfs.f_bsize, &target_stfs->f_bsize);
2653 put_user(stfs.f_blocks, &target_stfs->f_blocks);
2654 put_user(stfs.f_bfree, &target_stfs->f_bfree);
2655 put_user(stfs.f_bavail, &target_stfs->f_bavail);
2656 put_user(stfs.f_files, &target_stfs->f_files);
2657 put_user(stfs.f_ffree, &target_stfs->f_ffree);
2658 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid);
2659 put_user(stfs.f_namelen, &target_stfs->f_namelen);
2660 unlock_user_struct(target_stfs, arg3, 0);
2662 break;
2663 case TARGET_NR_fstatfs64:
2664 ret = get_errno(fstatfs(arg1, &stfs));
2665 goto convert_statfs64;
2666 #endif
2667 #ifdef TARGET_NR_ioperm
2668 case TARGET_NR_ioperm:
2669 goto unimplemented;
2670 #endif
2671 case TARGET_NR_socketcall:
2672 ret = do_socketcall(arg1, arg2);
2673 break;
2674 case TARGET_NR_syslog:
2675 goto unimplemented;
2676 case TARGET_NR_setitimer:
2678 struct itimerval value, ovalue, *pvalue;
2680 if (arg2) {
2681 pvalue = &value;
2682 target_to_host_timeval(&pvalue->it_interval,
2683 arg2);
2684 target_to_host_timeval(&pvalue->it_value,
2685 arg2 + sizeof(struct target_timeval));
2686 } else {
2687 pvalue = NULL;
2689 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
2690 if (!is_error(ret) && arg3) {
2691 host_to_target_timeval(arg3,
2692 &ovalue.it_interval);
2693 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
2694 &ovalue.it_value);
2697 break;
2698 case TARGET_NR_getitimer:
2700 struct itimerval value;
2702 ret = get_errno(getitimer(arg1, &value));
2703 if (!is_error(ret) && arg2) {
2704 host_to_target_timeval(arg2,
2705 &value.it_interval);
2706 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
2707 &value.it_value);
2710 break;
2711 case TARGET_NR_stat:
2712 p = lock_user_string(arg1);
2713 ret = get_errno(stat(path(p), &st));
2714 unlock_user(p, arg1, 0);
2715 goto do_stat;
2716 case TARGET_NR_lstat:
2717 p = lock_user_string(arg1);
2718 ret = get_errno(lstat(path(p), &st));
2719 unlock_user(p, arg1, 0);
2720 goto do_stat;
2721 case TARGET_NR_fstat:
2723 ret = get_errno(fstat(arg1, &st));
2724 do_stat:
2725 if (!is_error(ret)) {
2726 struct target_stat *target_st;
2728 lock_user_struct(target_st, arg2, 0);
2729 target_st->st_dev = tswap16(st.st_dev);
2730 target_st->st_ino = tswapl(st.st_ino);
2731 #if defined(TARGET_PPC)
2732 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
2733 target_st->st_uid = tswap32(st.st_uid);
2734 target_st->st_gid = tswap32(st.st_gid);
2735 #else
2736 target_st->st_mode = tswap16(st.st_mode);
2737 target_st->st_uid = tswap16(st.st_uid);
2738 target_st->st_gid = tswap16(st.st_gid);
2739 #endif
2740 target_st->st_nlink = tswap16(st.st_nlink);
2741 target_st->st_rdev = tswap16(st.st_rdev);
2742 target_st->st_size = tswapl(st.st_size);
2743 target_st->st_blksize = tswapl(st.st_blksize);
2744 target_st->st_blocks = tswapl(st.st_blocks);
2745 target_st->target_st_atime = tswapl(st.st_atime);
2746 target_st->target_st_mtime = tswapl(st.st_mtime);
2747 target_st->target_st_ctime = tswapl(st.st_ctime);
2748 unlock_user_struct(target_st, arg2, 1);
2751 break;
2752 #ifdef TARGET_NR_olduname
2753 case TARGET_NR_olduname:
2754 goto unimplemented;
2755 #endif
2756 #ifdef TARGET_NR_iopl
2757 case TARGET_NR_iopl:
2758 goto unimplemented;
2759 #endif
2760 case TARGET_NR_vhangup:
2761 ret = get_errno(vhangup());
2762 break;
2763 #ifdef TARGET_NR_idle
2764 case TARGET_NR_idle:
2765 goto unimplemented;
2766 #endif
2767 #ifdef TARGET_NR_syscall
2768 case TARGET_NR_syscall:
2769 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
2770 break;
2771 #endif
2772 case TARGET_NR_wait4:
2774 int status;
2775 target_long status_ptr = arg2;
2776 struct rusage rusage, *rusage_ptr;
2777 target_ulong target_rusage = arg4;
2778 if (target_rusage)
2779 rusage_ptr = &rusage;
2780 else
2781 rusage_ptr = NULL;
2782 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
2783 if (!is_error(ret)) {
2784 if (status_ptr)
2785 tputl(status_ptr, status);
2786 if (target_rusage) {
2787 host_to_target_rusage(target_rusage, &rusage);
2791 break;
2792 case TARGET_NR_swapoff:
2793 p = lock_user_string(arg1);
2794 ret = get_errno(swapoff(p));
2795 unlock_user(p, arg1, 0);
2796 break;
2797 case TARGET_NR_sysinfo:
2799 struct target_sysinfo *target_value;
2800 struct sysinfo value;
2801 ret = get_errno(sysinfo(&value));
2802 if (!is_error(ret) && arg1)
2804 /* ??? __put_user is probably wrong. */
2805 lock_user_struct(target_value, arg1, 0);
2806 __put_user(value.uptime, &target_value->uptime);
2807 __put_user(value.loads[0], &target_value->loads[0]);
2808 __put_user(value.loads[1], &target_value->loads[1]);
2809 __put_user(value.loads[2], &target_value->loads[2]);
2810 __put_user(value.totalram, &target_value->totalram);
2811 __put_user(value.freeram, &target_value->freeram);
2812 __put_user(value.sharedram, &target_value->sharedram);
2813 __put_user(value.bufferram, &target_value->bufferram);
2814 __put_user(value.totalswap, &target_value->totalswap);
2815 __put_user(value.freeswap, &target_value->freeswap);
2816 __put_user(value.procs, &target_value->procs);
2817 __put_user(value.totalhigh, &target_value->totalhigh);
2818 __put_user(value.freehigh, &target_value->freehigh);
2819 __put_user(value.mem_unit, &target_value->mem_unit);
2820 unlock_user_struct(target_value, arg1, 1);
2823 break;
2824 case TARGET_NR_ipc:
2825 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
2826 break;
2827 case TARGET_NR_fsync:
2828 ret = get_errno(fsync(arg1));
2829 break;
2830 case TARGET_NR_clone:
2831 ret = get_errno(do_fork(cpu_env, arg1, arg2));
2832 break;
2833 #ifdef __NR_exit_group
2834 /* new thread calls */
2835 case TARGET_NR_exit_group:
2836 gdb_exit(cpu_env, arg1);
2837 ret = get_errno(exit_group(arg1));
2838 break;
2839 #endif
2840 case TARGET_NR_setdomainname:
2841 p = lock_user_string(arg1);
2842 ret = get_errno(setdomainname(p, arg2));
2843 unlock_user(p, arg1, 0);
2844 break;
2845 case TARGET_NR_uname:
2846 /* no need to transcode because we use the linux syscall */
2848 struct new_utsname * buf;
2850 lock_user_struct(buf, arg1, 0);
2851 ret = get_errno(sys_uname(buf));
2852 if (!is_error(ret)) {
2853 /* Overrite the native machine name with whatever is being
2854 emulated. */
2855 strcpy (buf->machine, UNAME_MACHINE);
2857 unlock_user_struct(buf, arg1, 1);
2859 break;
2860 #ifdef TARGET_I386
2861 case TARGET_NR_modify_ldt:
2862 ret = get_errno(do_modify_ldt(cpu_env, arg1, arg2, arg3));
2863 break;
2864 case TARGET_NR_vm86old:
2865 goto unimplemented;
2866 case TARGET_NR_vm86:
2867 ret = do_vm86(cpu_env, arg1, arg2);
2868 break;
2869 #endif
2870 case TARGET_NR_adjtimex:
2871 goto unimplemented;
2872 case TARGET_NR_create_module:
2873 case TARGET_NR_init_module:
2874 case TARGET_NR_delete_module:
2875 case TARGET_NR_get_kernel_syms:
2876 goto unimplemented;
2877 case TARGET_NR_quotactl:
2878 goto unimplemented;
2879 case TARGET_NR_getpgid:
2880 ret = get_errno(getpgid(arg1));
2881 break;
2882 case TARGET_NR_fchdir:
2883 ret = get_errno(fchdir(arg1));
2884 break;
2885 case TARGET_NR_bdflush:
2886 goto unimplemented;
2887 case TARGET_NR_sysfs:
2888 goto unimplemented;
2889 case TARGET_NR_personality:
2890 ret = get_errno(personality(arg1));
2891 break;
2892 case TARGET_NR_afs_syscall:
2893 goto unimplemented;
2894 case TARGET_NR__llseek:
2896 #if defined (__x86_64__)
2897 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
2898 tput64(arg4, ret);
2899 #else
2900 int64_t res;
2901 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
2902 tput64(arg4, res);
2903 #endif
2905 break;
2906 case TARGET_NR_getdents:
2907 #if TARGET_LONG_SIZE != 4
2908 goto unimplemented;
2909 #warning not supported
2910 #elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
2912 struct target_dirent *target_dirp;
2913 struct dirent *dirp;
2914 long count = arg3;
2916 dirp = malloc(count);
2917 if (!dirp)
2918 return -ENOMEM;
2920 ret = get_errno(sys_getdents(arg1, dirp, count));
2921 if (!is_error(ret)) {
2922 struct dirent *de;
2923 struct target_dirent *tde;
2924 int len = ret;
2925 int reclen, treclen;
2926 int count1, tnamelen;
2928 count1 = 0;
2929 de = dirp;
2930 target_dirp = lock_user(arg2, count, 0);
2931 tde = target_dirp;
2932 while (len > 0) {
2933 reclen = de->d_reclen;
2934 treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
2935 tde->d_reclen = tswap16(treclen);
2936 tde->d_ino = tswapl(de->d_ino);
2937 tde->d_off = tswapl(de->d_off);
2938 tnamelen = treclen - (2 * sizeof(target_long) + 2);
2939 if (tnamelen > 256)
2940 tnamelen = 256;
2941 /* XXX: may not be correct */
2942 strncpy(tde->d_name, de->d_name, tnamelen);
2943 de = (struct dirent *)((char *)de + reclen);
2944 len -= reclen;
2945 tde = (struct dirent *)((char *)tde + treclen);
2946 count1 += treclen;
2948 ret = count1;
2950 unlock_user(target_dirp, arg2, ret);
2951 free(dirp);
2953 #else
2955 struct dirent *dirp;
2956 long count = arg3;
2958 dirp = lock_user(arg2, count, 0);
2959 ret = get_errno(sys_getdents(arg1, dirp, count));
2960 if (!is_error(ret)) {
2961 struct dirent *de;
2962 int len = ret;
2963 int reclen;
2964 de = dirp;
2965 while (len > 0) {
2966 reclen = de->d_reclen;
2967 if (reclen > len)
2968 break;
2969 de->d_reclen = tswap16(reclen);
2970 tswapls(&de->d_ino);
2971 tswapls(&de->d_off);
2972 de = (struct dirent *)((char *)de + reclen);
2973 len -= reclen;
2976 unlock_user(dirp, arg2, ret);
2978 #endif
2979 break;
2980 #ifdef TARGET_NR_getdents64
2981 case TARGET_NR_getdents64:
2983 struct dirent64 *dirp;
2984 long count = arg3;
2985 dirp = lock_user(arg2, count, 0);
2986 ret = get_errno(sys_getdents64(arg1, dirp, count));
2987 if (!is_error(ret)) {
2988 struct dirent64 *de;
2989 int len = ret;
2990 int reclen;
2991 de = dirp;
2992 while (len > 0) {
2993 reclen = de->d_reclen;
2994 if (reclen > len)
2995 break;
2996 de->d_reclen = tswap16(reclen);
2997 tswap64s(&de->d_ino);
2998 tswap64s(&de->d_off);
2999 de = (struct dirent64 *)((char *)de + reclen);
3000 len -= reclen;
3003 unlock_user(dirp, arg2, ret);
3005 break;
3006 #endif /* TARGET_NR_getdents64 */
3007 case TARGET_NR__newselect:
3008 ret = do_select(arg1, arg2, arg3, arg4, arg5);
3009 break;
3010 case TARGET_NR_poll:
3012 struct target_pollfd *target_pfd;
3013 unsigned int nfds = arg2;
3014 int timeout = arg3;
3015 struct pollfd *pfd;
3016 unsigned int i;
3018 target_pfd = lock_user(arg1, sizeof(struct target_pollfd) * nfds, 1);
3019 pfd = alloca(sizeof(struct pollfd) * nfds);
3020 for(i = 0; i < nfds; i++) {
3021 pfd[i].fd = tswap32(target_pfd[i].fd);
3022 pfd[i].events = tswap16(target_pfd[i].events);
3024 ret = get_errno(poll(pfd, nfds, timeout));
3025 if (!is_error(ret)) {
3026 for(i = 0; i < nfds; i++) {
3027 target_pfd[i].revents = tswap16(pfd[i].revents);
3029 ret += nfds * (sizeof(struct target_pollfd)
3030 - sizeof(struct pollfd));
3032 unlock_user(target_pfd, arg1, ret);
3034 break;
3035 case TARGET_NR_flock:
3036 /* NOTE: the flock constant seems to be the same for every
3037 Linux platform */
3038 ret = get_errno(flock(arg1, arg2));
3039 break;
3040 case TARGET_NR_readv:
3042 int count = arg3;
3043 struct iovec *vec;
3045 vec = alloca(count * sizeof(struct iovec));
3046 lock_iovec(vec, arg2, count, 0);
3047 ret = get_errno(readv(arg1, vec, count));
3048 unlock_iovec(vec, arg2, count, 1);
3050 break;
3051 case TARGET_NR_writev:
3053 int count = arg3;
3054 struct iovec *vec;
3056 vec = alloca(count * sizeof(struct iovec));
3057 lock_iovec(vec, arg2, count, 1);
3058 ret = get_errno(writev(arg1, vec, count));
3059 unlock_iovec(vec, arg2, count, 0);
3061 break;
3062 case TARGET_NR_getsid:
3063 ret = get_errno(getsid(arg1));
3064 break;
3065 case TARGET_NR_fdatasync:
3066 ret = get_errno(fdatasync(arg1));
3067 break;
3068 case TARGET_NR__sysctl:
3069 /* We don't implement this, but ENODIR is always a safe
3070 return value. */
3071 return -ENOTDIR;
3072 case TARGET_NR_sched_setparam:
3074 struct sched_param *target_schp;
3075 struct sched_param schp;
3077 lock_user_struct(target_schp, arg2, 1);
3078 schp.sched_priority = tswap32(target_schp->sched_priority);
3079 unlock_user_struct(target_schp, arg2, 0);
3080 ret = get_errno(sched_setparam(arg1, &schp));
3082 break;
3083 case TARGET_NR_sched_getparam:
3085 struct sched_param *target_schp;
3086 struct sched_param schp;
3087 ret = get_errno(sched_getparam(arg1, &schp));
3088 if (!is_error(ret)) {
3089 lock_user_struct(target_schp, arg2, 0);
3090 target_schp->sched_priority = tswap32(schp.sched_priority);
3091 unlock_user_struct(target_schp, arg2, 1);
3094 break;
3095 case TARGET_NR_sched_setscheduler:
3097 struct sched_param *target_schp;
3098 struct sched_param schp;
3099 lock_user_struct(target_schp, arg3, 1);
3100 schp.sched_priority = tswap32(target_schp->sched_priority);
3101 unlock_user_struct(target_schp, arg3, 0);
3102 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
3104 break;
3105 case TARGET_NR_sched_getscheduler:
3106 ret = get_errno(sched_getscheduler(arg1));
3107 break;
3108 case TARGET_NR_sched_yield:
3109 ret = get_errno(sched_yield());
3110 break;
3111 case TARGET_NR_sched_get_priority_max:
3112 ret = get_errno(sched_get_priority_max(arg1));
3113 break;
3114 case TARGET_NR_sched_get_priority_min:
3115 ret = get_errno(sched_get_priority_min(arg1));
3116 break;
3117 case TARGET_NR_sched_rr_get_interval:
3119 struct timespec ts;
3120 ret = get_errno(sched_rr_get_interval(arg1, &ts));
3121 if (!is_error(ret)) {
3122 host_to_target_timespec(arg2, &ts);
3125 break;
3126 case TARGET_NR_nanosleep:
3128 struct timespec req, rem;
3129 target_to_host_timespec(&req, arg1);
3130 ret = get_errno(nanosleep(&req, &rem));
3131 if (is_error(ret) && arg2) {
3132 host_to_target_timespec(arg2, &rem);
3135 break;
3136 case TARGET_NR_query_module:
3137 goto unimplemented;
3138 case TARGET_NR_nfsservctl:
3139 goto unimplemented;
3140 case TARGET_NR_prctl:
3141 goto unimplemented;
3142 #ifdef TARGET_NR_pread
3143 case TARGET_NR_pread:
3144 page_unprotect_range(arg2, arg3);
3145 p = lock_user(arg2, arg3, 0);
3146 ret = get_errno(pread(arg1, p, arg3, arg4));
3147 unlock_user(p, arg2, ret);
3148 break;
3149 case TARGET_NR_pwrite:
3150 p = lock_user(arg2, arg3, 1);
3151 ret = get_errno(pwrite(arg1, p, arg3, arg4));
3152 unlock_user(p, arg2, 0);
3153 break;
3154 #endif
3155 case TARGET_NR_getcwd:
3156 p = lock_user(arg1, arg2, 0);
3157 ret = get_errno(sys_getcwd1(p, arg2));
3158 unlock_user(p, arg1, ret);
3159 break;
3160 case TARGET_NR_capget:
3161 goto unimplemented;
3162 case TARGET_NR_capset:
3163 goto unimplemented;
3164 case TARGET_NR_sigaltstack:
3165 goto unimplemented;
3166 case TARGET_NR_sendfile:
3167 goto unimplemented;
3168 #ifdef TARGET_NR_getpmsg
3169 case TARGET_NR_getpmsg:
3170 goto unimplemented;
3171 #endif
3172 #ifdef TARGET_NR_putpmsg
3173 case TARGET_NR_putpmsg:
3174 goto unimplemented;
3175 #endif
3176 #ifdef TARGET_NR_vfork
3177 case TARGET_NR_vfork:
3178 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
3179 break;
3180 #endif
3181 #ifdef TARGET_NR_ugetrlimit
3182 case TARGET_NR_ugetrlimit:
3184 struct rlimit rlim;
3185 ret = get_errno(getrlimit(arg1, &rlim));
3186 if (!is_error(ret)) {
3187 struct target_rlimit *target_rlim;
3188 lock_user_struct(target_rlim, arg2, 0);
3189 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
3190 target_rlim->rlim_max = tswapl(rlim.rlim_max);
3191 unlock_user_struct(target_rlim, arg2, 1);
3193 break;
3195 #endif
3196 #ifdef TARGET_NR_truncate64
3197 case TARGET_NR_truncate64:
3198 p = lock_user_string(arg1);
3199 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
3200 unlock_user(p, arg1, 0);
3201 break;
3202 #endif
3203 #ifdef TARGET_NR_ftruncate64
3204 case TARGET_NR_ftruncate64:
3205 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
3206 break;
3207 #endif
3208 #ifdef TARGET_NR_stat64
3209 case TARGET_NR_stat64:
3210 p = lock_user_string(arg1);
3211 ret = get_errno(stat(path(p), &st));
3212 unlock_user(p, arg1, 0);
3213 goto do_stat64;
3214 #endif
3215 #ifdef TARGET_NR_lstat64
3216 case TARGET_NR_lstat64:
3217 p = lock_user_string(arg1);
3218 ret = get_errno(lstat(path(p), &st));
3219 unlock_user(p, arg1, 0);
3220 goto do_stat64;
3221 #endif
3222 #ifdef TARGET_NR_fstat64
3223 case TARGET_NR_fstat64:
3225 ret = get_errno(fstat(arg1, &st));
3226 do_stat64:
3227 if (!is_error(ret)) {
3228 #ifdef TARGET_ARM
3229 if (((CPUARMState *)cpu_env)->eabi) {
3230 struct target_eabi_stat64 *target_st;
3231 lock_user_struct(target_st, arg2, 1);
3232 memset(target_st, 0, sizeof(struct target_eabi_stat64));
3233 /* put_user is probably wrong. */
3234 put_user(st.st_dev, &target_st->st_dev);
3235 put_user(st.st_ino, &target_st->st_ino);
3236 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3237 put_user(st.st_ino, &target_st->__st_ino);
3238 #endif
3239 put_user(st.st_mode, &target_st->st_mode);
3240 put_user(st.st_nlink, &target_st->st_nlink);
3241 put_user(st.st_uid, &target_st->st_uid);
3242 put_user(st.st_gid, &target_st->st_gid);
3243 put_user(st.st_rdev, &target_st->st_rdev);
3244 /* XXX: better use of kernel struct */
3245 put_user(st.st_size, &target_st->st_size);
3246 put_user(st.st_blksize, &target_st->st_blksize);
3247 put_user(st.st_blocks, &target_st->st_blocks);
3248 put_user(st.st_atime, &target_st->target_st_atime);
3249 put_user(st.st_mtime, &target_st->target_st_mtime);
3250 put_user(st.st_ctime, &target_st->target_st_ctime);
3251 unlock_user_struct(target_st, arg2, 0);
3252 } else
3253 #endif
3255 struct target_stat64 *target_st;
3256 lock_user_struct(target_st, arg2, 1);
3257 memset(target_st, 0, sizeof(struct target_stat64));
3258 /* ??? put_user is probably wrong. */
3259 put_user(st.st_dev, &target_st->st_dev);
3260 put_user(st.st_ino, &target_st->st_ino);
3261 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3262 put_user(st.st_ino, &target_st->__st_ino);
3263 #endif
3264 put_user(st.st_mode, &target_st->st_mode);
3265 put_user(st.st_nlink, &target_st->st_nlink);
3266 put_user(st.st_uid, &target_st->st_uid);
3267 put_user(st.st_gid, &target_st->st_gid);
3268 put_user(st.st_rdev, &target_st->st_rdev);
3269 /* XXX: better use of kernel struct */
3270 put_user(st.st_size, &target_st->st_size);
3271 put_user(st.st_blksize, &target_st->st_blksize);
3272 put_user(st.st_blocks, &target_st->st_blocks);
3273 put_user(st.st_atime, &target_st->target_st_atime);
3274 put_user(st.st_mtime, &target_st->target_st_mtime);
3275 put_user(st.st_ctime, &target_st->target_st_ctime);
3276 unlock_user_struct(target_st, arg2, 0);
3280 break;
3281 #endif
3282 #ifdef USE_UID16
3283 case TARGET_NR_lchown:
3284 p = lock_user_string(arg1);
3285 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
3286 unlock_user(p, arg1, 0);
3287 break;
3288 case TARGET_NR_getuid:
3289 ret = get_errno(high2lowuid(getuid()));
3290 break;
3291 case TARGET_NR_getgid:
3292 ret = get_errno(high2lowgid(getgid()));
3293 break;
3294 case TARGET_NR_geteuid:
3295 ret = get_errno(high2lowuid(geteuid()));
3296 break;
3297 case TARGET_NR_getegid:
3298 ret = get_errno(high2lowgid(getegid()));
3299 break;
3300 case TARGET_NR_setreuid:
3301 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
3302 break;
3303 case TARGET_NR_setregid:
3304 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
3305 break;
3306 case TARGET_NR_getgroups:
3308 int gidsetsize = arg1;
3309 uint16_t *target_grouplist;
3310 gid_t *grouplist;
3311 int i;
3313 grouplist = alloca(gidsetsize * sizeof(gid_t));
3314 ret = get_errno(getgroups(gidsetsize, grouplist));
3315 if (!is_error(ret)) {
3316 target_grouplist = lock_user(arg2, gidsetsize * 2, 0);
3317 for(i = 0;i < gidsetsize; i++)
3318 target_grouplist[i] = tswap16(grouplist[i]);
3319 unlock_user(target_grouplist, arg2, gidsetsize * 2);
3322 break;
3323 case TARGET_NR_setgroups:
3325 int gidsetsize = arg1;
3326 uint16_t *target_grouplist;
3327 gid_t *grouplist;
3328 int i;
3330 grouplist = alloca(gidsetsize * sizeof(gid_t));
3331 target_grouplist = lock_user(arg2, gidsetsize * 2, 1);
3332 for(i = 0;i < gidsetsize; i++)
3333 grouplist[i] = tswap16(target_grouplist[i]);
3334 unlock_user(target_grouplist, arg2, 0);
3335 ret = get_errno(setgroups(gidsetsize, grouplist));
3337 break;
3338 case TARGET_NR_fchown:
3339 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
3340 break;
3341 #ifdef TARGET_NR_setresuid
3342 case TARGET_NR_setresuid:
3343 ret = get_errno(setresuid(low2highuid(arg1),
3344 low2highuid(arg2),
3345 low2highuid(arg3)));
3346 break;
3347 #endif
3348 #ifdef TARGET_NR_getresuid
3349 case TARGET_NR_getresuid:
3351 uid_t ruid, euid, suid;
3352 ret = get_errno(getresuid(&ruid, &euid, &suid));
3353 if (!is_error(ret)) {
3354 tput16(arg1, tswap16(high2lowuid(ruid)));
3355 tput16(arg2, tswap16(high2lowuid(euid)));
3356 tput16(arg3, tswap16(high2lowuid(suid)));
3359 break;
3360 #endif
3361 #ifdef TARGET_NR_getresgid
3362 case TARGET_NR_setresgid:
3363 ret = get_errno(setresgid(low2highgid(arg1),
3364 low2highgid(arg2),
3365 low2highgid(arg3)));
3366 break;
3367 #endif
3368 #ifdef TARGET_NR_getresgid
3369 case TARGET_NR_getresgid:
3371 gid_t rgid, egid, sgid;
3372 ret = get_errno(getresgid(&rgid, &egid, &sgid));
3373 if (!is_error(ret)) {
3374 tput16(arg1, tswap16(high2lowgid(rgid)));
3375 tput16(arg2, tswap16(high2lowgid(egid)));
3376 tput16(arg3, tswap16(high2lowgid(sgid)));
3379 break;
3380 #endif
3381 case TARGET_NR_chown:
3382 p = lock_user_string(arg1);
3383 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
3384 unlock_user(p, arg1, 0);
3385 break;
3386 case TARGET_NR_setuid:
3387 ret = get_errno(setuid(low2highuid(arg1)));
3388 break;
3389 case TARGET_NR_setgid:
3390 ret = get_errno(setgid(low2highgid(arg1)));
3391 break;
3392 case TARGET_NR_setfsuid:
3393 ret = get_errno(setfsuid(arg1));
3394 break;
3395 case TARGET_NR_setfsgid:
3396 ret = get_errno(setfsgid(arg1));
3397 break;
3398 #endif /* USE_UID16 */
3400 #ifdef TARGET_NR_lchown32
3401 case TARGET_NR_lchown32:
3402 p = lock_user_string(arg1);
3403 ret = get_errno(lchown(p, arg2, arg3));
3404 unlock_user(p, arg1, 0);
3405 break;
3406 #endif
3407 #ifdef TARGET_NR_getuid32
3408 case TARGET_NR_getuid32:
3409 ret = get_errno(getuid());
3410 break;
3411 #endif
3412 #ifdef TARGET_NR_getgid32
3413 case TARGET_NR_getgid32:
3414 ret = get_errno(getgid());
3415 break;
3416 #endif
3417 #ifdef TARGET_NR_geteuid32
3418 case TARGET_NR_geteuid32:
3419 ret = get_errno(geteuid());
3420 break;
3421 #endif
3422 #ifdef TARGET_NR_getegid32
3423 case TARGET_NR_getegid32:
3424 ret = get_errno(getegid());
3425 break;
3426 #endif
3427 #ifdef TARGET_NR_setreuid32
3428 case TARGET_NR_setreuid32:
3429 ret = get_errno(setreuid(arg1, arg2));
3430 break;
3431 #endif
3432 #ifdef TARGET_NR_setregid32
3433 case TARGET_NR_setregid32:
3434 ret = get_errno(setregid(arg1, arg2));
3435 break;
3436 #endif
3437 #ifdef TARGET_NR_getgroups32
3438 case TARGET_NR_getgroups32:
3440 int gidsetsize = arg1;
3441 uint32_t *target_grouplist;
3442 gid_t *grouplist;
3443 int i;
3445 grouplist = alloca(gidsetsize * sizeof(gid_t));
3446 ret = get_errno(getgroups(gidsetsize, grouplist));
3447 if (!is_error(ret)) {
3448 target_grouplist = lock_user(arg2, gidsetsize * 4, 0);
3449 for(i = 0;i < gidsetsize; i++)
3450 target_grouplist[i] = tswap32(grouplist[i]);
3451 unlock_user(target_grouplist, arg2, gidsetsize * 4);
3454 break;
3455 #endif
3456 #ifdef TARGET_NR_setgroups32
3457 case TARGET_NR_setgroups32:
3459 int gidsetsize = arg1;
3460 uint32_t *target_grouplist;
3461 gid_t *grouplist;
3462 int i;
3464 grouplist = alloca(gidsetsize * sizeof(gid_t));
3465 target_grouplist = lock_user(arg2, gidsetsize * 4, 1);
3466 for(i = 0;i < gidsetsize; i++)
3467 grouplist[i] = tswap32(target_grouplist[i]);
3468 unlock_user(target_grouplist, arg2, 0);
3469 ret = get_errno(setgroups(gidsetsize, grouplist));
3471 break;
3472 #endif
3473 #ifdef TARGET_NR_fchown32
3474 case TARGET_NR_fchown32:
3475 ret = get_errno(fchown(arg1, arg2, arg3));
3476 break;
3477 #endif
3478 #ifdef TARGET_NR_setresuid32
3479 case TARGET_NR_setresuid32:
3480 ret = get_errno(setresuid(arg1, arg2, arg3));
3481 break;
3482 #endif
3483 #ifdef TARGET_NR_getresuid32
3484 case TARGET_NR_getresuid32:
3486 uid_t ruid, euid, suid;
3487 ret = get_errno(getresuid(&ruid, &euid, &suid));
3488 if (!is_error(ret)) {
3489 tput32(arg1, tswap32(ruid));
3490 tput32(arg2, tswap32(euid));
3491 tput32(arg3, tswap32(suid));
3494 break;
3495 #endif
3496 #ifdef TARGET_NR_setresgid32
3497 case TARGET_NR_setresgid32:
3498 ret = get_errno(setresgid(arg1, arg2, arg3));
3499 break;
3500 #endif
3501 #ifdef TARGET_NR_getresgid32
3502 case TARGET_NR_getresgid32:
3504 gid_t rgid, egid, sgid;
3505 ret = get_errno(getresgid(&rgid, &egid, &sgid));
3506 if (!is_error(ret)) {
3507 tput32(arg1, tswap32(rgid));
3508 tput32(arg2, tswap32(egid));
3509 tput32(arg3, tswap32(sgid));
3512 break;
3513 #endif
3514 #ifdef TARGET_NR_chown32
3515 case TARGET_NR_chown32:
3516 p = lock_user_string(arg1);
3517 ret = get_errno(chown(p, arg2, arg3));
3518 unlock_user(p, arg1, 0);
3519 break;
3520 #endif
3521 #ifdef TARGET_NR_setuid32
3522 case TARGET_NR_setuid32:
3523 ret = get_errno(setuid(arg1));
3524 break;
3525 #endif
3526 #ifdef TARGET_NR_setgid32
3527 case TARGET_NR_setgid32:
3528 ret = get_errno(setgid(arg1));
3529 break;
3530 #endif
3531 #ifdef TARGET_NR_setfsuid32
3532 case TARGET_NR_setfsuid32:
3533 ret = get_errno(setfsuid(arg1));
3534 break;
3535 #endif
3536 #ifdef TARGET_NR_setfsgid32
3537 case TARGET_NR_setfsgid32:
3538 ret = get_errno(setfsgid(arg1));
3539 break;
3540 #endif
3542 case TARGET_NR_pivot_root:
3543 goto unimplemented;
3544 #ifdef TARGET_NR_mincore
3545 case TARGET_NR_mincore:
3546 goto unimplemented;
3547 #endif
3548 #ifdef TARGET_NR_madvise
3549 case TARGET_NR_madvise:
3550 goto unimplemented;
3551 #endif
3552 #if TARGET_LONG_BITS == 32
3553 case TARGET_NR_fcntl64:
3555 struct flock64 fl;
3556 struct target_flock64 *target_fl;
3557 #ifdef TARGET_ARM
3558 struct target_eabi_flock64 *target_efl;
3559 #endif
3561 switch(arg2) {
3562 case F_GETLK64:
3563 ret = get_errno(fcntl(arg1, arg2, &fl));
3564 if (ret == 0) {
3565 #ifdef TARGET_ARM
3566 if (((CPUARMState *)cpu_env)->eabi) {
3567 lock_user_struct(target_efl, arg3, 0);
3568 target_efl->l_type = tswap16(fl.l_type);
3569 target_efl->l_whence = tswap16(fl.l_whence);
3570 target_efl->l_start = tswap64(fl.l_start);
3571 target_efl->l_len = tswap64(fl.l_len);
3572 target_efl->l_pid = tswapl(fl.l_pid);
3573 unlock_user_struct(target_efl, arg3, 1);
3574 } else
3575 #endif
3577 lock_user_struct(target_fl, arg3, 0);
3578 target_fl->l_type = tswap16(fl.l_type);
3579 target_fl->l_whence = tswap16(fl.l_whence);
3580 target_fl->l_start = tswap64(fl.l_start);
3581 target_fl->l_len = tswap64(fl.l_len);
3582 target_fl->l_pid = tswapl(fl.l_pid);
3583 unlock_user_struct(target_fl, arg3, 1);
3586 break;
3588 case F_SETLK64:
3589 case F_SETLKW64:
3590 #ifdef TARGET_ARM
3591 if (((CPUARMState *)cpu_env)->eabi) {
3592 lock_user_struct(target_efl, arg3, 1);
3593 fl.l_type = tswap16(target_efl->l_type);
3594 fl.l_whence = tswap16(target_efl->l_whence);
3595 fl.l_start = tswap64(target_efl->l_start);
3596 fl.l_len = tswap64(target_efl->l_len);
3597 fl.l_pid = tswapl(target_efl->l_pid);
3598 unlock_user_struct(target_efl, arg3, 0);
3599 } else
3600 #endif
3602 lock_user_struct(target_fl, arg3, 1);
3603 fl.l_type = tswap16(target_fl->l_type);
3604 fl.l_whence = tswap16(target_fl->l_whence);
3605 fl.l_start = tswap64(target_fl->l_start);
3606 fl.l_len = tswap64(target_fl->l_len);
3607 fl.l_pid = tswapl(target_fl->l_pid);
3608 unlock_user_struct(target_fl, arg3, 0);
3610 ret = get_errno(fcntl(arg1, arg2, &fl));
3611 break;
3612 default:
3613 ret = get_errno(do_fcntl(arg1, arg2, arg3));
3614 break;
3616 break;
3618 #endif
3619 #ifdef TARGET_NR_security
3620 case TARGET_NR_security:
3621 goto unimplemented;
3622 #endif
3623 #ifdef TARGET_NR_getpagesize
3624 case TARGET_NR_getpagesize:
3625 ret = TARGET_PAGE_SIZE;
3626 break;
3627 #endif
3628 case TARGET_NR_gettid:
3629 ret = get_errno(gettid());
3630 break;
3631 case TARGET_NR_readahead:
3632 goto unimplemented;
3633 #ifdef TARGET_NR_setxattr
3634 case TARGET_NR_setxattr:
3635 case TARGET_NR_lsetxattr:
3636 case TARGET_NR_fsetxattr:
3637 case TARGET_NR_getxattr:
3638 case TARGET_NR_lgetxattr:
3639 case TARGET_NR_fgetxattr:
3640 case TARGET_NR_listxattr:
3641 case TARGET_NR_llistxattr:
3642 case TARGET_NR_flistxattr:
3643 case TARGET_NR_removexattr:
3644 case TARGET_NR_lremovexattr:
3645 case TARGET_NR_fremovexattr:
3646 goto unimplemented_nowarn;
3647 #endif
3648 #ifdef TARGET_NR_set_thread_area
3649 case TARGET_NR_set_thread_area:
3650 case TARGET_NR_get_thread_area:
3651 goto unimplemented_nowarn;
3652 #endif
3653 default:
3654 unimplemented:
3655 gemu_log("qemu: Unsupported syscall: %d\n", num);
3656 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_set_thread_area)
3657 unimplemented_nowarn:
3658 #endif
3659 ret = -ENOSYS;
3660 break;
3662 fail:
3663 #ifdef DEBUG
3664 gemu_log(" = %ld\n", ret);
3665 #endif
3666 return ret;