]> git.proxmox.com Git - qemu.git/blob - linux-user/syscall.c
fcntl flags convertion (Jocelyn Mayer)
[qemu.git] / linux-user / syscall.c
1 /*
2 * Linux syscalls
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
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 <utime.h>
45 //#include <sys/user.h>
46 #include <netinet/tcp.h>
47
48 #define termios host_termios
49 #define winsize host_winsize
50 #define termio host_termio
51 #define sgttyb host_sgttyb /* same as target */
52 #define tchars host_tchars /* same as target */
53 #define ltchars host_ltchars /* same as target */
54
55 #include <linux/termios.h>
56 #include <linux/unistd.h>
57 #include <linux/utsname.h>
58 #include <linux/cdrom.h>
59 #include <linux/hdreg.h>
60 #include <linux/soundcard.h>
61 #include <linux/dirent.h>
62 #include <linux/kd.h>
63
64 #include "qemu.h"
65
66 //#define DEBUG
67
68 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC)
69 /* 16 bit uid wrappers emulation */
70 #define USE_UID16
71 #endif
72
73 //#include <linux/msdos_fs.h>
74 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
75 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
76
77
78 #if defined(__powerpc__)
79 #undef __syscall_nr
80 #undef __sc_loadargs_0
81 #undef __sc_loadargs_1
82 #undef __sc_loadargs_2
83 #undef __sc_loadargs_3
84 #undef __sc_loadargs_4
85 #undef __sc_loadargs_5
86 #undef __sc_asm_input_0
87 #undef __sc_asm_input_1
88 #undef __sc_asm_input_2
89 #undef __sc_asm_input_3
90 #undef __sc_asm_input_4
91 #undef __sc_asm_input_5
92 #undef _syscall0
93 #undef _syscall1
94 #undef _syscall2
95 #undef _syscall3
96 #undef _syscall4
97 #undef _syscall5
98
99 /* need to redefine syscalls as Linux kernel defines are incorrect for
100 the clobber list */
101 /* On powerpc a system call basically clobbers the same registers like a
102 * function call, with the exception of LR (which is needed for the
103 * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
104 * an error return status).
105 */
106
107 #define __syscall_nr(nr, type, name, args...) \
108 unsigned long __sc_ret, __sc_err; \
109 { \
110 register unsigned long __sc_0 __asm__ ("r0"); \
111 register unsigned long __sc_3 __asm__ ("r3"); \
112 register unsigned long __sc_4 __asm__ ("r4"); \
113 register unsigned long __sc_5 __asm__ ("r5"); \
114 register unsigned long __sc_6 __asm__ ("r6"); \
115 register unsigned long __sc_7 __asm__ ("r7"); \
116 \
117 __sc_loadargs_##nr(name, args); \
118 __asm__ __volatile__ \
119 ("sc \n\t" \
120 "mfcr %0 " \
121 : "=&r" (__sc_0), \
122 "=&r" (__sc_3), "=&r" (__sc_4), \
123 "=&r" (__sc_5), "=&r" (__sc_6), \
124 "=&r" (__sc_7) \
125 : __sc_asm_input_##nr \
126 : "cr0", "ctr", "memory", \
127 "r8", "r9", "r10","r11", "r12"); \
128 __sc_ret = __sc_3; \
129 __sc_err = __sc_0; \
130 } \
131 if (__sc_err & 0x10000000) \
132 { \
133 errno = __sc_ret; \
134 __sc_ret = -1; \
135 } \
136 return (type) __sc_ret
137
138 #define __sc_loadargs_0(name, dummy...) \
139 __sc_0 = __NR_##name
140 #define __sc_loadargs_1(name, arg1) \
141 __sc_loadargs_0(name); \
142 __sc_3 = (unsigned long) (arg1)
143 #define __sc_loadargs_2(name, arg1, arg2) \
144 __sc_loadargs_1(name, arg1); \
145 __sc_4 = (unsigned long) (arg2)
146 #define __sc_loadargs_3(name, arg1, arg2, arg3) \
147 __sc_loadargs_2(name, arg1, arg2); \
148 __sc_5 = (unsigned long) (arg3)
149 #define __sc_loadargs_4(name, arg1, arg2, arg3, arg4) \
150 __sc_loadargs_3(name, arg1, arg2, arg3); \
151 __sc_6 = (unsigned long) (arg4)
152 #define __sc_loadargs_5(name, arg1, arg2, arg3, arg4, arg5) \
153 __sc_loadargs_4(name, arg1, arg2, arg3, arg4); \
154 __sc_7 = (unsigned long) (arg5)
155
156 #define __sc_asm_input_0 "0" (__sc_0)
157 #define __sc_asm_input_1 __sc_asm_input_0, "1" (__sc_3)
158 #define __sc_asm_input_2 __sc_asm_input_1, "2" (__sc_4)
159 #define __sc_asm_input_3 __sc_asm_input_2, "3" (__sc_5)
160 #define __sc_asm_input_4 __sc_asm_input_3, "4" (__sc_6)
161 #define __sc_asm_input_5 __sc_asm_input_4, "5" (__sc_7)
162
163 #define _syscall0(type,name) \
164 type name(void) \
165 { \
166 __syscall_nr(0, type, name); \
167 }
168
169 #define _syscall1(type,name,type1,arg1) \
170 type name(type1 arg1) \
171 { \
172 __syscall_nr(1, type, name, arg1); \
173 }
174
175 #define _syscall2(type,name,type1,arg1,type2,arg2) \
176 type name(type1 arg1, type2 arg2) \
177 { \
178 __syscall_nr(2, type, name, arg1, arg2); \
179 }
180
181 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
182 type name(type1 arg1, type2 arg2, type3 arg3) \
183 { \
184 __syscall_nr(3, type, name, arg1, arg2, arg3); \
185 }
186
187 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
188 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
189 { \
190 __syscall_nr(4, type, name, arg1, arg2, arg3, arg4); \
191 }
192
193 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5) \
194 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \
195 { \
196 __syscall_nr(5, type, name, arg1, arg2, arg3, arg4, arg5); \
197 }
198 #endif
199
200 #define __NR_sys_uname __NR_uname
201 #define __NR_sys_getcwd1 __NR_getcwd
202 #define __NR_sys_statfs __NR_statfs
203 #define __NR_sys_fstatfs __NR_fstatfs
204 #define __NR_sys_getdents __NR_getdents
205 #define __NR_sys_getdents64 __NR_getdents64
206 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
207
208 #if defined(__alpha__) || defined (__ia64__)
209 #define __NR__llseek __NR_lseek
210 #endif
211
212 #ifdef __NR_gettid
213 _syscall0(int, gettid)
214 #else
215 static int gettid(void) {
216 return -ENOSYS;
217 }
218 #endif
219 _syscall1(int,sys_uname,struct new_utsname *,buf)
220 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
221 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
222 _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
223 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
224 loff_t *, res, uint, wh);
225 _syscall2(int,sys_statfs,const char *,path,struct kernel_statfs *,buf)
226 _syscall2(int,sys_fstatfs,int,fd,struct kernel_statfs *,buf)
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
231
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 *);
241
242 #define put_user(x,ptr)\
243 ({\
244 int size = sizeof(*ptr);\
245 switch(size) {\
246 case 1:\
247 stb(ptr, (typeof(*ptr))(x));\
248 break;\
249 case 2:\
250 stw(ptr, (typeof(*ptr))(x));\
251 break;\
252 case 4:\
253 stl(ptr, (typeof(*ptr))(x));\
254 break;\
255 case 8:\
256 stq(ptr, (typeof(*ptr))(x));\
257 break;\
258 default:\
259 abort();\
260 }\
261 0;\
262 })
263 static inline long get_errno(long ret)
264 {
265 if (ret == -1)
266 return -errno;
267 else
268 return ret;
269 }
270
271 static inline int is_error(long ret)
272 {
273 return (unsigned long)ret >= (unsigned long)(-4096);
274 }
275
276 static char *target_brk;
277 static char *target_original_brk;
278
279 void target_set_brk(char *new_brk)
280 {
281 target_brk = new_brk;
282 target_original_brk = new_brk;
283 }
284
285 static long do_brk(char *new_brk)
286 {
287 char *brk_page;
288 long mapped_addr;
289 int new_alloc_size;
290
291 if (!new_brk)
292 return (long)target_brk;
293 if (new_brk < target_original_brk)
294 return -ENOMEM;
295
296 brk_page = (char *)HOST_PAGE_ALIGN((unsigned long)target_brk);
297
298 /* If the new brk is less than this, set it and we're done... */
299 if (new_brk < brk_page) {
300 target_brk = new_brk;
301 return (long)target_brk;
302 }
303
304 /* We need to allocate more memory after the brk... */
305 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
306 mapped_addr = get_errno(target_mmap((unsigned long)brk_page, new_alloc_size,
307 PROT_READ|PROT_WRITE,
308 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
309 if (is_error(mapped_addr)) {
310 return mapped_addr;
311 } else {
312 target_brk = new_brk;
313 return (long)target_brk;
314 }
315 }
316
317 static inline fd_set *target_to_host_fds(fd_set *fds,
318 target_long *target_fds, int n)
319 {
320 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
321 return (fd_set *)target_fds;
322 #else
323 int i, b;
324 if (target_fds) {
325 FD_ZERO(fds);
326 for(i = 0;i < n; i++) {
327 b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
328 (i & (TARGET_LONG_BITS - 1))) & 1;
329 if (b)
330 FD_SET(i, fds);
331 }
332 return fds;
333 } else {
334 return NULL;
335 }
336 #endif
337 }
338
339 static inline void host_to_target_fds(target_long *target_fds,
340 fd_set *fds, int n)
341 {
342 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
343 /* nothing to do */
344 #else
345 int i, nw, j, k;
346 target_long v;
347
348 if (target_fds) {
349 nw = n / TARGET_LONG_BITS;
350 k = 0;
351 for(i = 0;i < nw; i++) {
352 v = 0;
353 for(j = 0; j < TARGET_LONG_BITS; j++) {
354 v |= ((FD_ISSET(k, fds) != 0) << j);
355 k++;
356 }
357 target_fds[i] = tswapl(v);
358 }
359 }
360 #endif
361 }
362
363 #if defined(__alpha__)
364 #define HOST_HZ 1024
365 #else
366 #define HOST_HZ 100
367 #endif
368
369 static inline long host_to_target_clock_t(long ticks)
370 {
371 #if HOST_HZ == TARGET_HZ
372 return ticks;
373 #else
374 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
375 #endif
376 }
377
378 static inline void host_to_target_rusage(struct target_rusage *target_rusage,
379 const struct rusage *rusage)
380 {
381 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
382 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
383 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
384 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
385 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
386 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
387 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
388 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
389 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
390 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
391 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
392 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
393 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
394 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
395 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
396 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
397 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
398 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
399 }
400
401 static inline void target_to_host_timeval(struct timeval *tv,
402 const struct target_timeval *target_tv)
403 {
404 tv->tv_sec = tswapl(target_tv->tv_sec);
405 tv->tv_usec = tswapl(target_tv->tv_usec);
406 }
407
408 static inline void host_to_target_timeval(struct target_timeval *target_tv,
409 const struct timeval *tv)
410 {
411 target_tv->tv_sec = tswapl(tv->tv_sec);
412 target_tv->tv_usec = tswapl(tv->tv_usec);
413 }
414
415
416 static long do_select(long n,
417 target_long *target_rfds, target_long *target_wfds,
418 target_long *target_efds, struct target_timeval *target_tv)
419 {
420 fd_set rfds, wfds, efds;
421 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
422 struct timeval tv, *tv_ptr;
423 long ret;
424
425 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
426 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
427 efds_ptr = target_to_host_fds(&efds, target_efds, n);
428
429 if (target_tv) {
430 target_to_host_timeval(&tv, target_tv);
431 tv_ptr = &tv;
432 } else {
433 tv_ptr = NULL;
434 }
435 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
436 if (!is_error(ret)) {
437 host_to_target_fds(target_rfds, rfds_ptr, n);
438 host_to_target_fds(target_wfds, wfds_ptr, n);
439 host_to_target_fds(target_efds, efds_ptr, n);
440
441 if (target_tv) {
442 host_to_target_timeval(target_tv, &tv);
443 }
444 }
445 return ret;
446 }
447
448 static inline void target_to_host_sockaddr(struct sockaddr *addr,
449 struct target_sockaddr *target_addr,
450 socklen_t len)
451 {
452 memcpy(addr, target_addr, len);
453 addr->sa_family = tswap16(target_addr->sa_family);
454 }
455
456 static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
457 struct sockaddr *addr,
458 socklen_t len)
459 {
460 memcpy(target_addr, addr, len);
461 target_addr->sa_family = tswap16(addr->sa_family);
462 }
463
464 static inline void target_to_host_cmsg(struct msghdr *msgh,
465 struct target_msghdr *target_msgh)
466 {
467 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
468 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
469 socklen_t space = 0;
470
471 while (cmsg && target_cmsg) {
472 void *data = CMSG_DATA(cmsg);
473 void *target_data = TARGET_CMSG_DATA(target_cmsg);
474
475 int len = tswapl(target_cmsg->cmsg_len)
476 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
477
478 space += CMSG_SPACE(len);
479 if (space > msgh->msg_controllen) {
480 space -= CMSG_SPACE(len);
481 gemu_log("Host cmsg overflow");
482 break;
483 }
484
485 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
486 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
487 cmsg->cmsg_len = CMSG_LEN(len);
488
489 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
490 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
491 memcpy(data, target_data, len);
492 } else {
493 int *fd = (int *)data;
494 int *target_fd = (int *)target_data;
495 int i, numfds = len / sizeof(int);
496
497 for (i = 0; i < numfds; i++)
498 fd[i] = tswap32(target_fd[i]);
499 }
500
501 cmsg = CMSG_NXTHDR(msgh, cmsg);
502 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
503 }
504
505 msgh->msg_controllen = space;
506 }
507
508 static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
509 struct msghdr *msgh)
510 {
511 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
512 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
513 socklen_t space = 0;
514
515 while (cmsg && target_cmsg) {
516 void *data = CMSG_DATA(cmsg);
517 void *target_data = TARGET_CMSG_DATA(target_cmsg);
518
519 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
520
521 space += TARGET_CMSG_SPACE(len);
522 if (space > tswapl(target_msgh->msg_controllen)) {
523 space -= TARGET_CMSG_SPACE(len);
524 gemu_log("Target cmsg overflow");
525 break;
526 }
527
528 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
529 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
530 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
531
532 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
533 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
534 memcpy(target_data, data, len);
535 } else {
536 int *fd = (int *)data;
537 int *target_fd = (int *)target_data;
538 int i, numfds = len / sizeof(int);
539
540 for (i = 0; i < numfds; i++)
541 target_fd[i] = tswap32(fd[i]);
542 }
543
544 cmsg = CMSG_NXTHDR(msgh, cmsg);
545 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
546 }
547
548 msgh->msg_controllen = tswapl(space);
549 }
550
551 static long do_setsockopt(int sockfd, int level, int optname,
552 void *optval, socklen_t optlen)
553 {
554 if (level == SOL_TCP) {
555 /* TCP options all take an 'int' value. */
556 int val;
557
558 if (optlen < sizeof(uint32_t))
559 return -EINVAL;
560
561 val = tswap32(*(uint32_t *)optval);
562 return get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
563 }
564
565 else if (level != SOL_SOCKET) {
566 gemu_log("Unsupported setsockopt level: %d\n", level);
567 return -ENOSYS;
568 }
569
570 switch (optname) {
571 /* Options with 'int' argument. */
572 case SO_DEBUG:
573 case SO_REUSEADDR:
574 case SO_TYPE:
575 case SO_ERROR:
576 case SO_DONTROUTE:
577 case SO_BROADCAST:
578 case SO_SNDBUF:
579 case SO_RCVBUF:
580 case SO_KEEPALIVE:
581 case SO_OOBINLINE:
582 case SO_NO_CHECK:
583 case SO_PRIORITY:
584 case SO_BSDCOMPAT:
585 case SO_PASSCRED:
586 case SO_TIMESTAMP:
587 case SO_RCVLOWAT:
588 case SO_RCVTIMEO:
589 case SO_SNDTIMEO:
590 {
591 int val;
592 if (optlen < sizeof(uint32_t))
593 return -EINVAL;
594 val = tswap32(*(uint32_t *)optval);
595 return get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
596 }
597
598 default:
599 gemu_log("Unsupported setsockopt SOL_SOCKET option: %d\n", optname);
600 return -ENOSYS;
601 }
602 }
603
604 static long do_getsockopt(int sockfd, int level, int optname,
605 void *optval, socklen_t *optlen)
606 {
607 gemu_log("getsockopt not yet supported\n");
608 return -ENOSYS;
609 }
610
611 static long do_socketcall(int num, int32_t *vptr)
612 {
613 long ret;
614
615 switch(num) {
616 case SOCKOP_socket:
617 {
618 int domain = tswap32(vptr[0]);
619 int type = tswap32(vptr[1]);
620 int protocol = tswap32(vptr[2]);
621
622 ret = get_errno(socket(domain, type, protocol));
623 }
624 break;
625 case SOCKOP_bind:
626 {
627 int sockfd = tswap32(vptr[0]);
628 void *target_addr = (void *)tswap32(vptr[1]);
629 socklen_t addrlen = tswap32(vptr[2]);
630 void *addr = alloca(addrlen);
631
632 target_to_host_sockaddr(addr, target_addr, addrlen);
633 ret = get_errno(bind(sockfd, addr, addrlen));
634 }
635 break;
636 case SOCKOP_connect:
637 {
638 int sockfd = tswap32(vptr[0]);
639 void *target_addr = (void *)tswap32(vptr[1]);
640 socklen_t addrlen = tswap32(vptr[2]);
641 void *addr = alloca(addrlen);
642
643 target_to_host_sockaddr(addr, target_addr, addrlen);
644 ret = get_errno(connect(sockfd, addr, addrlen));
645 }
646 break;
647 case SOCKOP_listen:
648 {
649 int sockfd = tswap32(vptr[0]);
650 int backlog = tswap32(vptr[1]);
651
652 ret = get_errno(listen(sockfd, backlog));
653 }
654 break;
655 case SOCKOP_accept:
656 {
657 int sockfd = tswap32(vptr[0]);
658 void *target_addr = (void *)tswap32(vptr[1]);
659 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
660 socklen_t addrlen = tswap32(*target_addrlen);
661 void *addr = alloca(addrlen);
662
663 ret = get_errno(accept(sockfd, addr, &addrlen));
664 if (!is_error(ret)) {
665 host_to_target_sockaddr(target_addr, addr, addrlen);
666 *target_addrlen = tswap32(addrlen);
667 }
668 }
669 break;
670 case SOCKOP_getsockname:
671 {
672 int sockfd = tswap32(vptr[0]);
673 void *target_addr = (void *)tswap32(vptr[1]);
674 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
675 socklen_t addrlen = tswap32(*target_addrlen);
676 void *addr = alloca(addrlen);
677
678 ret = get_errno(getsockname(sockfd, addr, &addrlen));
679 if (!is_error(ret)) {
680 host_to_target_sockaddr(target_addr, addr, addrlen);
681 *target_addrlen = tswap32(addrlen);
682 }
683 }
684 break;
685 case SOCKOP_getpeername:
686 {
687 int sockfd = tswap32(vptr[0]);
688 void *target_addr = (void *)tswap32(vptr[1]);
689 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
690 socklen_t addrlen = tswap32(*target_addrlen);
691 void *addr = alloca(addrlen);
692
693 ret = get_errno(getpeername(sockfd, addr, &addrlen));
694 if (!is_error(ret)) {
695 host_to_target_sockaddr(target_addr, addr, addrlen);
696 *target_addrlen = tswap32(addrlen);
697 }
698 }
699 break;
700 case SOCKOP_socketpair:
701 {
702 int domain = tswap32(vptr[0]);
703 int type = tswap32(vptr[1]);
704 int protocol = tswap32(vptr[2]);
705 int32_t *target_tab = (void *)tswap32(vptr[3]);
706 int tab[2];
707
708 ret = get_errno(socketpair(domain, type, protocol, tab));
709 if (!is_error(ret)) {
710 target_tab[0] = tswap32(tab[0]);
711 target_tab[1] = tswap32(tab[1]);
712 }
713 }
714 break;
715 case SOCKOP_send:
716 {
717 int sockfd = tswap32(vptr[0]);
718 void *msg = (void *)tswap32(vptr[1]);
719 size_t len = tswap32(vptr[2]);
720 int flags = tswap32(vptr[3]);
721
722 ret = get_errno(send(sockfd, msg, len, flags));
723 }
724 break;
725 case SOCKOP_recv:
726 {
727 int sockfd = tswap32(vptr[0]);
728 void *msg = (void *)tswap32(vptr[1]);
729 size_t len = tswap32(vptr[2]);
730 int flags = tswap32(vptr[3]);
731
732 ret = get_errno(recv(sockfd, msg, len, flags));
733 }
734 break;
735 case SOCKOP_sendto:
736 {
737 int sockfd = tswap32(vptr[0]);
738 void *msg = (void *)tswap32(vptr[1]);
739 size_t len = tswap32(vptr[2]);
740 int flags = tswap32(vptr[3]);
741 void *target_addr = (void *)tswap32(vptr[4]);
742 socklen_t addrlen = tswap32(vptr[5]);
743 void *addr = alloca(addrlen);
744
745 target_to_host_sockaddr(addr, target_addr, addrlen);
746 ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
747 }
748 break;
749 case SOCKOP_recvfrom:
750 {
751 int sockfd = tswap32(vptr[0]);
752 void *msg = (void *)tswap32(vptr[1]);
753 size_t len = tswap32(vptr[2]);
754 int flags = tswap32(vptr[3]);
755 void *target_addr = (void *)tswap32(vptr[4]);
756 uint32_t *target_addrlen = (void *)tswap32(vptr[5]);
757 socklen_t addrlen = tswap32(*target_addrlen);
758 void *addr = alloca(addrlen);
759
760 ret = get_errno(recvfrom(sockfd, msg, len, flags, addr, &addrlen));
761 if (!is_error(ret)) {
762 host_to_target_sockaddr(target_addr, addr, addrlen);
763 *target_addrlen = tswap32(addrlen);
764 }
765 }
766 break;
767 case SOCKOP_shutdown:
768 {
769 int sockfd = tswap32(vptr[0]);
770 int how = tswap32(vptr[1]);
771
772 ret = get_errno(shutdown(sockfd, how));
773 }
774 break;
775 case SOCKOP_sendmsg:
776 case SOCKOP_recvmsg:
777 {
778 int fd;
779 struct target_msghdr *msgp;
780 struct msghdr msg;
781 int flags, count, i;
782 struct iovec *vec;
783 struct target_iovec *target_vec;
784
785 msgp = (void *)tswap32(vptr[1]);
786 msg.msg_name = (void *)tswapl(msgp->msg_name);
787 msg.msg_namelen = tswapl(msgp->msg_namelen);
788 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
789 msg.msg_control = alloca(msg.msg_controllen);
790 msg.msg_flags = tswap32(msgp->msg_flags);
791
792 count = tswapl(msgp->msg_iovlen);
793 vec = alloca(count * sizeof(struct iovec));
794 target_vec = (void *)tswapl(msgp->msg_iov);
795 for(i = 0;i < count; i++) {
796 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
797 vec[i].iov_len = tswapl(target_vec[i].iov_len);
798 }
799 msg.msg_iovlen = count;
800 msg.msg_iov = vec;
801
802 fd = tswap32(vptr[0]);
803 flags = tswap32(vptr[2]);
804 if (num == SOCKOP_sendmsg) {
805 target_to_host_cmsg(&msg, msgp);
806 ret = get_errno(sendmsg(fd, &msg, flags));
807 } else {
808 ret = get_errno(recvmsg(fd, &msg, flags));
809 if (!is_error(ret))
810 host_to_target_cmsg(msgp, &msg);
811 }
812 }
813 break;
814 case SOCKOP_setsockopt:
815 {
816 int sockfd = tswap32(vptr[0]);
817 int level = tswap32(vptr[1]);
818 int optname = tswap32(vptr[2]);
819 void *optval = (void *)tswap32(vptr[3]);
820 socklen_t optlen = tswap32(vptr[4]);
821
822 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
823 }
824 break;
825 case SOCKOP_getsockopt:
826 {
827 int sockfd = tswap32(vptr[0]);
828 int level = tswap32(vptr[1]);
829 int optname = tswap32(vptr[2]);
830 void *optval = (void *)tswap32(vptr[3]);
831 uint32_t *target_len = (void *)tswap32(vptr[4]);
832 socklen_t optlen = tswap32(*target_len);
833
834 ret = do_getsockopt(sockfd, level, optname, optval, &optlen);
835 if (!is_error(ret))
836 *target_len = tswap32(optlen);
837 }
838 break;
839 default:
840 gemu_log("Unsupported socketcall: %d\n", num);
841 ret = -ENOSYS;
842 break;
843 }
844 return ret;
845 }
846
847 /* kernel structure types definitions */
848 #define IFNAMSIZ 16
849
850 #define STRUCT(name, list...) STRUCT_ ## name,
851 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
852 enum {
853 #include "syscall_types.h"
854 };
855 #undef STRUCT
856 #undef STRUCT_SPECIAL
857
858 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
859 #define STRUCT_SPECIAL(name)
860 #include "syscall_types.h"
861 #undef STRUCT
862 #undef STRUCT_SPECIAL
863
864 typedef struct IOCTLEntry {
865 unsigned int target_cmd;
866 unsigned int host_cmd;
867 const char *name;
868 int access;
869 const argtype arg_type[5];
870 } IOCTLEntry;
871
872 #define IOC_R 0x0001
873 #define IOC_W 0x0002
874 #define IOC_RW (IOC_R | IOC_W)
875
876 #define MAX_STRUCT_SIZE 4096
877
878 IOCTLEntry ioctl_entries[] = {
879 #define IOCTL(cmd, access, types...) \
880 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
881 #include "ioctls.h"
882 { 0, 0, },
883 };
884
885 static long do_ioctl(long fd, long cmd, long arg)
886 {
887 const IOCTLEntry *ie;
888 const argtype *arg_type;
889 long ret;
890 uint8_t buf_temp[MAX_STRUCT_SIZE];
891
892 ie = ioctl_entries;
893 for(;;) {
894 if (ie->target_cmd == 0) {
895 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
896 return -ENOSYS;
897 }
898 if (ie->target_cmd == cmd)
899 break;
900 ie++;
901 }
902 arg_type = ie->arg_type;
903 #if defined(DEBUG)
904 gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
905 #endif
906 switch(arg_type[0]) {
907 case TYPE_NULL:
908 /* no argument */
909 ret = get_errno(ioctl(fd, ie->host_cmd));
910 break;
911 case TYPE_PTRVOID:
912 case TYPE_INT:
913 /* int argment */
914 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
915 break;
916 case TYPE_PTR:
917 arg_type++;
918 switch(ie->access) {
919 case IOC_R:
920 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
921 if (!is_error(ret)) {
922 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
923 }
924 break;
925 case IOC_W:
926 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
927 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
928 break;
929 default:
930 case IOC_RW:
931 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
932 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
933 if (!is_error(ret)) {
934 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
935 }
936 break;
937 }
938 break;
939 default:
940 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
941 ret = -ENOSYS;
942 break;
943 }
944 return ret;
945 }
946
947 bitmask_transtbl iflag_tbl[] = {
948 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
949 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
950 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
951 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
952 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
953 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
954 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
955 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
956 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
957 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
958 { TARGET_IXON, TARGET_IXON, IXON, IXON },
959 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
960 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
961 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
962 { 0, 0, 0, 0 }
963 };
964
965 bitmask_transtbl oflag_tbl[] = {
966 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
967 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
968 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
969 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
970 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
971 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
972 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
973 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
974 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
975 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
976 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
977 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
978 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
979 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
980 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
981 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
982 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
983 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
984 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
985 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
986 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
987 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
988 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
989 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
990 { 0, 0, 0, 0 }
991 };
992
993 bitmask_transtbl cflag_tbl[] = {
994 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
995 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
996 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
997 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
998 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
999 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1000 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1001 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1002 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1003 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1004 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1005 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1006 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1007 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1008 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1009 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1010 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1011 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1012 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1013 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1014 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1015 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1016 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1017 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1018 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1019 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1020 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1021 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1022 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1023 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1024 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1025 { 0, 0, 0, 0 }
1026 };
1027
1028 bitmask_transtbl lflag_tbl[] = {
1029 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1030 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1031 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1032 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1033 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1034 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1035 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1036 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1037 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1038 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1039 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1040 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1041 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1042 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1043 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1044 { 0, 0, 0, 0 }
1045 };
1046
1047 static void target_to_host_termios (void *dst, const void *src)
1048 {
1049 struct host_termios *host = dst;
1050 const struct target_termios *target = src;
1051
1052 host->c_iflag =
1053 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
1054 host->c_oflag =
1055 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
1056 host->c_cflag =
1057 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
1058 host->c_lflag =
1059 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
1060 host->c_line = target->c_line;
1061
1062 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
1063 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
1064 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
1065 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
1066 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
1067 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
1068 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
1069 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
1070 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
1071 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
1072 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
1073 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
1074 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
1075 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
1076 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
1077 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
1078 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
1079 }
1080
1081 static void host_to_target_termios (void *dst, const void *src)
1082 {
1083 struct target_termios *target = dst;
1084 const struct host_termios *host = src;
1085
1086 target->c_iflag =
1087 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
1088 target->c_oflag =
1089 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
1090 target->c_cflag =
1091 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
1092 target->c_lflag =
1093 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
1094 target->c_line = host->c_line;
1095
1096 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
1097 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
1098 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
1099 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
1100 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
1101 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
1102 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
1103 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
1104 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
1105 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
1106 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
1107 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
1108 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
1109 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
1110 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
1111 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
1112 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
1113 }
1114
1115 StructEntry struct_termios_def = {
1116 .convert = { host_to_target_termios, target_to_host_termios },
1117 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
1118 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
1119 };
1120
1121 static bitmask_transtbl mmap_flags_tbl[] = {
1122 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
1123 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
1124 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
1125 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
1126 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
1127 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
1128 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
1129 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
1130 { 0, 0, 0, 0 }
1131 };
1132
1133 static bitmask_transtbl fcntl_flags_tbl[] = {
1134 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
1135 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
1136 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
1137 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
1138 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
1139 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
1140 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
1141 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
1142 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
1143 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
1144 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
1145 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
1146 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
1147 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
1148 { 0, 0, 0, 0 }
1149 };
1150
1151 #if defined(TARGET_I386)
1152
1153 /* NOTE: there is really one LDT for all the threads */
1154 uint8_t *ldt_table;
1155
1156 static int read_ldt(void *ptr, unsigned long bytecount)
1157 {
1158 int size;
1159
1160 if (!ldt_table)
1161 return 0;
1162 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
1163 if (size > bytecount)
1164 size = bytecount;
1165 memcpy(ptr, ldt_table, size);
1166 return size;
1167 }
1168
1169 /* XXX: add locking support */
1170 static int write_ldt(CPUX86State *env,
1171 void *ptr, unsigned long bytecount, int oldmode)
1172 {
1173 struct target_modify_ldt_ldt_s ldt_info;
1174 int seg_32bit, contents, read_exec_only, limit_in_pages;
1175 int seg_not_present, useable;
1176 uint32_t *lp, entry_1, entry_2;
1177
1178 if (bytecount != sizeof(ldt_info))
1179 return -EINVAL;
1180 memcpy(&ldt_info, ptr, sizeof(ldt_info));
1181 tswap32s(&ldt_info.entry_number);
1182 tswapls((long *)&ldt_info.base_addr);
1183 tswap32s(&ldt_info.limit);
1184 tswap32s(&ldt_info.flags);
1185
1186 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
1187 return -EINVAL;
1188 seg_32bit = ldt_info.flags & 1;
1189 contents = (ldt_info.flags >> 1) & 3;
1190 read_exec_only = (ldt_info.flags >> 3) & 1;
1191 limit_in_pages = (ldt_info.flags >> 4) & 1;
1192 seg_not_present = (ldt_info.flags >> 5) & 1;
1193 useable = (ldt_info.flags >> 6) & 1;
1194
1195 if (contents == 3) {
1196 if (oldmode)
1197 return -EINVAL;
1198 if (seg_not_present == 0)
1199 return -EINVAL;
1200 }
1201 /* allocate the LDT */
1202 if (!ldt_table) {
1203 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1204 if (!ldt_table)
1205 return -ENOMEM;
1206 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
1207 env->ldt.base = ldt_table;
1208 env->ldt.limit = 0xffff;
1209 }
1210
1211 /* NOTE: same code as Linux kernel */
1212 /* Allow LDTs to be cleared by the user. */
1213 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
1214 if (oldmode ||
1215 (contents == 0 &&
1216 read_exec_only == 1 &&
1217 seg_32bit == 0 &&
1218 limit_in_pages == 0 &&
1219 seg_not_present == 1 &&
1220 useable == 0 )) {
1221 entry_1 = 0;
1222 entry_2 = 0;
1223 goto install;
1224 }
1225 }
1226
1227 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
1228 (ldt_info.limit & 0x0ffff);
1229 entry_2 = (ldt_info.base_addr & 0xff000000) |
1230 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
1231 (ldt_info.limit & 0xf0000) |
1232 ((read_exec_only ^ 1) << 9) |
1233 (contents << 10) |
1234 ((seg_not_present ^ 1) << 15) |
1235 (seg_32bit << 22) |
1236 (limit_in_pages << 23) |
1237 0x7000;
1238 if (!oldmode)
1239 entry_2 |= (useable << 20);
1240
1241 /* Install the new entry ... */
1242 install:
1243 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
1244 lp[0] = tswap32(entry_1);
1245 lp[1] = tswap32(entry_2);
1246 return 0;
1247 }
1248
1249 /* specific and weird i386 syscalls */
1250 int do_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
1251 {
1252 int ret = -ENOSYS;
1253
1254 switch (func) {
1255 case 0:
1256 ret = read_ldt(ptr, bytecount);
1257 break;
1258 case 1:
1259 ret = write_ldt(env, ptr, bytecount, 1);
1260 break;
1261 case 0x11:
1262 ret = write_ldt(env, ptr, bytecount, 0);
1263 break;
1264 }
1265 return ret;
1266 }
1267
1268 #endif /* defined(TARGET_I386) */
1269
1270 /* this stack is the equivalent of the kernel stack associated with a
1271 thread/process */
1272 #define NEW_STACK_SIZE 8192
1273
1274 static int clone_func(void *arg)
1275 {
1276 CPUState *env = arg;
1277 cpu_loop(env);
1278 /* never exits */
1279 return 0;
1280 }
1281
1282 int do_fork(CPUState *env, unsigned int flags, unsigned long newsp)
1283 {
1284 int ret;
1285 TaskState *ts;
1286 uint8_t *new_stack;
1287 CPUState *new_env;
1288
1289 if (flags & CLONE_VM) {
1290 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
1291 memset(ts, 0, sizeof(TaskState));
1292 new_stack = ts->stack;
1293 ts->used = 1;
1294 /* add in task state list */
1295 ts->next = first_task_state;
1296 first_task_state = ts;
1297 /* we create a new CPU instance. */
1298 new_env = cpu_init();
1299 memcpy(new_env, env, sizeof(CPUState));
1300 #if defined(TARGET_I386)
1301 if (!newsp)
1302 newsp = env->regs[R_ESP];
1303 new_env->regs[R_ESP] = newsp;
1304 new_env->regs[R_EAX] = 0;
1305 #elif defined(TARGET_ARM)
1306 if (!newsp)
1307 newsp = env->regs[13];
1308 new_env->regs[13] = newsp;
1309 new_env->regs[0] = 0;
1310 #elif defined(TARGET_SPARC)
1311 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
1312 #elif defined(TARGET_PPC)
1313 if (!newsp)
1314 newsp = env->gpr[1];
1315 new_env->gpr[1] = newsp;
1316 {
1317 int i;
1318 for (i = 7; i < 32; i++)
1319 new_env->gpr[i] = 0;
1320 }
1321 #else
1322 #error unsupported target CPU
1323 #endif
1324 new_env->opaque = ts;
1325 #ifdef __ia64__
1326 ret = clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1327 #else
1328 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1329 #endif
1330 } else {
1331 /* if no CLONE_VM, we consider it is a fork */
1332 if ((flags & ~CSIGNAL) != 0)
1333 return -EINVAL;
1334 ret = fork();
1335 }
1336 return ret;
1337 }
1338
1339 static long do_fcntl(int fd, int cmd, unsigned long arg)
1340 {
1341 struct flock fl;
1342 struct target_flock *target_fl = (void *)arg;
1343 long ret;
1344
1345 switch(cmd) {
1346 case TARGET_F_GETLK:
1347 ret = fcntl(fd, cmd, &fl);
1348 if (ret == 0) {
1349 target_fl->l_type = tswap16(fl.l_type);
1350 target_fl->l_whence = tswap16(fl.l_whence);
1351 target_fl->l_start = tswapl(fl.l_start);
1352 target_fl->l_len = tswapl(fl.l_len);
1353 target_fl->l_pid = tswapl(fl.l_pid);
1354 }
1355 break;
1356
1357 case TARGET_F_SETLK:
1358 case TARGET_F_SETLKW:
1359 fl.l_type = tswap16(target_fl->l_type);
1360 fl.l_whence = tswap16(target_fl->l_whence);
1361 fl.l_start = tswapl(target_fl->l_start);
1362 fl.l_len = tswapl(target_fl->l_len);
1363 fl.l_pid = tswapl(target_fl->l_pid);
1364 ret = fcntl(fd, cmd, &fl);
1365 break;
1366
1367 case TARGET_F_GETLK64:
1368 case TARGET_F_SETLK64:
1369 case TARGET_F_SETLKW64:
1370 ret = -1;
1371 errno = EINVAL;
1372 break;
1373
1374 case F_GETFL:
1375 ret = fcntl(fd, cmd, arg);
1376 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
1377 break;
1378
1379 case F_SETFL:
1380 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
1381 break;
1382
1383 default:
1384 ret = fcntl(fd, cmd, arg);
1385 break;
1386 }
1387 return ret;
1388 }
1389
1390 #ifdef USE_UID16
1391
1392 static inline int high2lowuid(int uid)
1393 {
1394 if (uid > 65535)
1395 return 65534;
1396 else
1397 return uid;
1398 }
1399
1400 static inline int high2lowgid(int gid)
1401 {
1402 if (gid > 65535)
1403 return 65534;
1404 else
1405 return gid;
1406 }
1407
1408 static inline int low2highuid(int uid)
1409 {
1410 if ((int16_t)uid == -1)
1411 return -1;
1412 else
1413 return uid;
1414 }
1415
1416 static inline int low2highgid(int gid)
1417 {
1418 if ((int16_t)gid == -1)
1419 return -1;
1420 else
1421 return gid;
1422 }
1423
1424 #endif /* USE_UID16 */
1425
1426 void syscall_init(void)
1427 {
1428 IOCTLEntry *ie;
1429 const argtype *arg_type;
1430 int size;
1431
1432 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
1433 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
1434 #include "syscall_types.h"
1435 #undef STRUCT
1436 #undef STRUCT_SPECIAL
1437
1438 /* we patch the ioctl size if necessary. We rely on the fact that
1439 no ioctl has all the bits at '1' in the size field */
1440 ie = ioctl_entries;
1441 while (ie->target_cmd != 0) {
1442 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
1443 TARGET_IOC_SIZEMASK) {
1444 arg_type = ie->arg_type;
1445 if (arg_type[0] != TYPE_PTR) {
1446 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
1447 ie->target_cmd);
1448 exit(1);
1449 }
1450 arg_type++;
1451 size = thunk_type_size(arg_type, 0);
1452 ie->target_cmd = (ie->target_cmd &
1453 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
1454 (size << TARGET_IOC_SIZESHIFT);
1455 }
1456 /* automatic consistency check if same arch */
1457 #if defined(__i386__) && defined(TARGET_I386)
1458 if (ie->target_cmd != ie->host_cmd) {
1459 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
1460 ie->target_cmd, ie->host_cmd);
1461 }
1462 #endif
1463 ie++;
1464 }
1465 }
1466
1467 long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
1468 long arg4, long arg5, long arg6)
1469 {
1470 long ret;
1471 struct stat st;
1472 struct kernel_statfs *stfs;
1473
1474 #ifdef DEBUG
1475 gemu_log("syscall %d", num);
1476 #endif
1477 switch(num) {
1478 case TARGET_NR_exit:
1479 #ifdef HAVE_GPROF
1480 _mcleanup();
1481 #endif
1482 /* XXX: should free thread stack and CPU env */
1483 _exit(arg1);
1484 ret = 0; /* avoid warning */
1485 break;
1486 case TARGET_NR_read:
1487 page_unprotect_range((void *)arg2, arg3);
1488 ret = get_errno(read(arg1, (void *)arg2, arg3));
1489 break;
1490 case TARGET_NR_write:
1491 ret = get_errno(write(arg1, (void *)arg2, arg3));
1492 break;
1493 case TARGET_NR_open:
1494 ret = get_errno(open(path((const char *)arg1),
1495 target_to_host_bitmask(arg2, fcntl_flags_tbl),
1496 arg3));
1497 break;
1498 case TARGET_NR_close:
1499 ret = get_errno(close(arg1));
1500 break;
1501 case TARGET_NR_brk:
1502 ret = do_brk((char *)arg1);
1503 break;
1504 case TARGET_NR_fork:
1505 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
1506 break;
1507 case TARGET_NR_waitpid:
1508 {
1509 int *status = (int *)arg2;
1510 ret = get_errno(waitpid(arg1, status, arg3));
1511 if (!is_error(ret) && status)
1512 tswapls((long *)&status);
1513 }
1514 break;
1515 case TARGET_NR_creat:
1516 ret = get_errno(creat((const char *)arg1, arg2));
1517 break;
1518 case TARGET_NR_link:
1519 ret = get_errno(link((const char *)arg1, (const char *)arg2));
1520 break;
1521 case TARGET_NR_unlink:
1522 ret = get_errno(unlink((const char *)arg1));
1523 break;
1524 case TARGET_NR_execve:
1525 {
1526 char **argp, **envp;
1527 int argc, envc;
1528 uint32_t *p;
1529 char **q;
1530
1531 argc = 0;
1532 for (p = (void *)arg2; *p; p++)
1533 argc++;
1534 envc = 0;
1535 for (p = (void *)arg3; *p; p++)
1536 envc++;
1537
1538 argp = alloca((argc + 1) * sizeof(void *));
1539 envp = alloca((envc + 1) * sizeof(void *));
1540
1541 for (p = (void *)arg2, q = argp; *p; p++, q++)
1542 *q = (void *)tswap32(*p);
1543 *q = NULL;
1544
1545 for (p = (void *)arg3, q = envp; *p; p++, q++)
1546 *q = (void *)tswap32(*p);
1547 *q = NULL;
1548
1549 ret = get_errno(execve((const char *)arg1, argp, envp));
1550 }
1551 break;
1552 case TARGET_NR_chdir:
1553 ret = get_errno(chdir((const char *)arg1));
1554 break;
1555 case TARGET_NR_time:
1556 {
1557 int *time_ptr = (int *)arg1;
1558 ret = get_errno(time((time_t *)time_ptr));
1559 if (!is_error(ret) && time_ptr)
1560 tswap32s(time_ptr);
1561 }
1562 break;
1563 case TARGET_NR_mknod:
1564 ret = get_errno(mknod((const char *)arg1, arg2, arg3));
1565 break;
1566 case TARGET_NR_chmod:
1567 ret = get_errno(chmod((const char *)arg1, arg2));
1568 break;
1569 #ifdef TARGET_NR_break
1570 case TARGET_NR_break:
1571 goto unimplemented;
1572 #endif
1573 #ifdef TARGET_NR_oldstat
1574 case TARGET_NR_oldstat:
1575 goto unimplemented;
1576 #endif
1577 case TARGET_NR_lseek:
1578 ret = get_errno(lseek(arg1, arg2, arg3));
1579 break;
1580 case TARGET_NR_getpid:
1581 ret = get_errno(getpid());
1582 break;
1583 case TARGET_NR_mount:
1584 /* need to look at the data field */
1585 goto unimplemented;
1586 case TARGET_NR_umount:
1587 ret = get_errno(umount((const char *)arg1));
1588 break;
1589 case TARGET_NR_stime:
1590 {
1591 int *time_ptr = (int *)arg1;
1592 if (time_ptr)
1593 tswap32s(time_ptr);
1594 ret = get_errno(stime((time_t *)time_ptr));
1595 }
1596 break;
1597 case TARGET_NR_ptrace:
1598 goto unimplemented;
1599 case TARGET_NR_alarm:
1600 ret = alarm(arg1);
1601 break;
1602 #ifdef TARGET_NR_oldfstat
1603 case TARGET_NR_oldfstat:
1604 goto unimplemented;
1605 #endif
1606 case TARGET_NR_pause:
1607 ret = get_errno(pause());
1608 break;
1609 case TARGET_NR_utime:
1610 {
1611 struct utimbuf tbuf;
1612 struct target_utimbuf *target_tbuf = (void *)arg2;
1613 tbuf.actime = tswapl(target_tbuf->actime);
1614 tbuf.modtime = tswapl(target_tbuf->modtime);
1615 ret = get_errno(utime((const char *)arg1, &tbuf));
1616 }
1617 break;
1618 #ifdef TARGET_NR_stty
1619 case TARGET_NR_stty:
1620 goto unimplemented;
1621 #endif
1622 #ifdef TARGET_NR_gtty
1623 case TARGET_NR_gtty:
1624 goto unimplemented;
1625 #endif
1626 case TARGET_NR_access:
1627 ret = get_errno(access((const char *)arg1, arg2));
1628 break;
1629 case TARGET_NR_nice:
1630 ret = get_errno(nice(arg1));
1631 break;
1632 #ifdef TARGET_NR_ftime
1633 case TARGET_NR_ftime:
1634 goto unimplemented;
1635 #endif
1636 case TARGET_NR_sync:
1637 sync();
1638 ret = 0;
1639 break;
1640 case TARGET_NR_kill:
1641 ret = get_errno(kill(arg1, arg2));
1642 break;
1643 case TARGET_NR_rename:
1644 ret = get_errno(rename((const char *)arg1, (const char *)arg2));
1645 break;
1646 case TARGET_NR_mkdir:
1647 ret = get_errno(mkdir((const char *)arg1, arg2));
1648 break;
1649 case TARGET_NR_rmdir:
1650 ret = get_errno(rmdir((const char *)arg1));
1651 break;
1652 case TARGET_NR_dup:
1653 ret = get_errno(dup(arg1));
1654 break;
1655 case TARGET_NR_pipe:
1656 {
1657 int *pipe_ptr = (int *)arg1;
1658 ret = get_errno(pipe(pipe_ptr));
1659 if (!is_error(ret)) {
1660 tswap32s(&pipe_ptr[0]);
1661 tswap32s(&pipe_ptr[1]);
1662 }
1663 }
1664 break;
1665 case TARGET_NR_times:
1666 {
1667 struct target_tms *tmsp = (void *)arg1;
1668 struct tms tms;
1669 ret = get_errno(times(&tms));
1670 if (tmsp) {
1671 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
1672 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
1673 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
1674 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
1675 }
1676 if (!is_error(ret))
1677 ret = host_to_target_clock_t(ret);
1678 }
1679 break;
1680 #ifdef TARGET_NR_prof
1681 case TARGET_NR_prof:
1682 goto unimplemented;
1683 #endif
1684 case TARGET_NR_signal:
1685 goto unimplemented;
1686
1687 case TARGET_NR_acct:
1688 goto unimplemented;
1689 case TARGET_NR_umount2:
1690 ret = get_errno(umount2((const char *)arg1, arg2));
1691 break;
1692 #ifdef TARGET_NR_lock
1693 case TARGET_NR_lock:
1694 goto unimplemented;
1695 #endif
1696 case TARGET_NR_ioctl:
1697 ret = do_ioctl(arg1, arg2, arg3);
1698 break;
1699 case TARGET_NR_fcntl:
1700 ret = get_errno(do_fcntl(arg1, arg2, arg3));
1701 break;
1702 #ifdef TARGET_NR_mpx
1703 case TARGET_NR_mpx:
1704 goto unimplemented;
1705 #endif
1706 case TARGET_NR_setpgid:
1707 ret = get_errno(setpgid(arg1, arg2));
1708 break;
1709 #ifdef TARGET_NR_ulimit
1710 case TARGET_NR_ulimit:
1711 goto unimplemented;
1712 #endif
1713 #ifdef TARGET_NR_oldolduname
1714 case TARGET_NR_oldolduname:
1715 goto unimplemented;
1716 #endif
1717 case TARGET_NR_umask:
1718 ret = get_errno(umask(arg1));
1719 break;
1720 case TARGET_NR_chroot:
1721 ret = get_errno(chroot((const char *)arg1));
1722 break;
1723 case TARGET_NR_ustat:
1724 goto unimplemented;
1725 case TARGET_NR_dup2:
1726 ret = get_errno(dup2(arg1, arg2));
1727 break;
1728 case TARGET_NR_getppid:
1729 ret = get_errno(getppid());
1730 break;
1731 case TARGET_NR_getpgrp:
1732 ret = get_errno(getpgrp());
1733 break;
1734 case TARGET_NR_setsid:
1735 ret = get_errno(setsid());
1736 break;
1737 case TARGET_NR_sigaction:
1738 {
1739 struct target_old_sigaction *old_act = (void *)arg2;
1740 struct target_old_sigaction *old_oact = (void *)arg3;
1741 struct target_sigaction act, oact, *pact;
1742 if (old_act) {
1743 act._sa_handler = old_act->_sa_handler;
1744 target_siginitset(&act.sa_mask, old_act->sa_mask);
1745 act.sa_flags = old_act->sa_flags;
1746 act.sa_restorer = old_act->sa_restorer;
1747 pact = &act;
1748 } else {
1749 pact = NULL;
1750 }
1751 ret = get_errno(do_sigaction(arg1, pact, &oact));
1752 if (!is_error(ret) && old_oact) {
1753 old_oact->_sa_handler = oact._sa_handler;
1754 old_oact->sa_mask = oact.sa_mask.sig[0];
1755 old_oact->sa_flags = oact.sa_flags;
1756 old_oact->sa_restorer = oact.sa_restorer;
1757 }
1758 }
1759 break;
1760 case TARGET_NR_rt_sigaction:
1761 ret = get_errno(do_sigaction(arg1, (void *)arg2, (void *)arg3));
1762 break;
1763 case TARGET_NR_sgetmask:
1764 {
1765 sigset_t cur_set;
1766 target_ulong target_set;
1767 sigprocmask(0, NULL, &cur_set);
1768 host_to_target_old_sigset(&target_set, &cur_set);
1769 ret = target_set;
1770 }
1771 break;
1772 case TARGET_NR_ssetmask:
1773 {
1774 sigset_t set, oset, cur_set;
1775 target_ulong target_set = arg1;
1776 sigprocmask(0, NULL, &cur_set);
1777 target_to_host_old_sigset(&set, &target_set);
1778 sigorset(&set, &set, &cur_set);
1779 sigprocmask(SIG_SETMASK, &set, &oset);
1780 host_to_target_old_sigset(&target_set, &oset);
1781 ret = target_set;
1782 }
1783 break;
1784 case TARGET_NR_sigprocmask:
1785 {
1786 int how = arg1;
1787 sigset_t set, oldset, *set_ptr;
1788 target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
1789
1790 if (pset) {
1791 switch(how) {
1792 case TARGET_SIG_BLOCK:
1793 how = SIG_BLOCK;
1794 break;
1795 case TARGET_SIG_UNBLOCK:
1796 how = SIG_UNBLOCK;
1797 break;
1798 case TARGET_SIG_SETMASK:
1799 how = SIG_SETMASK;
1800 break;
1801 default:
1802 ret = -EINVAL;
1803 goto fail;
1804 }
1805 target_to_host_old_sigset(&set, pset);
1806 set_ptr = &set;
1807 } else {
1808 how = 0;
1809 set_ptr = NULL;
1810 }
1811 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
1812 if (!is_error(ret) && poldset) {
1813 host_to_target_old_sigset(poldset, &oldset);
1814 }
1815 }
1816 break;
1817 case TARGET_NR_rt_sigprocmask:
1818 {
1819 int how = arg1;
1820 sigset_t set, oldset, *set_ptr;
1821 target_sigset_t *pset = (void *)arg2;
1822 target_sigset_t *poldset = (void *)arg3;
1823
1824 if (pset) {
1825 switch(how) {
1826 case TARGET_SIG_BLOCK:
1827 how = SIG_BLOCK;
1828 break;
1829 case TARGET_SIG_UNBLOCK:
1830 how = SIG_UNBLOCK;
1831 break;
1832 case TARGET_SIG_SETMASK:
1833 how = SIG_SETMASK;
1834 break;
1835 default:
1836 ret = -EINVAL;
1837 goto fail;
1838 }
1839 target_to_host_sigset(&set, pset);
1840 set_ptr = &set;
1841 } else {
1842 how = 0;
1843 set_ptr = NULL;
1844 }
1845 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
1846 if (!is_error(ret) && poldset) {
1847 host_to_target_sigset(poldset, &oldset);
1848 }
1849 }
1850 break;
1851 case TARGET_NR_sigpending:
1852 {
1853 sigset_t set;
1854 ret = get_errno(sigpending(&set));
1855 if (!is_error(ret)) {
1856 host_to_target_old_sigset((target_ulong *)arg1, &set);
1857 }
1858 }
1859 break;
1860 case TARGET_NR_rt_sigpending:
1861 {
1862 sigset_t set;
1863 ret = get_errno(sigpending(&set));
1864 if (!is_error(ret)) {
1865 host_to_target_sigset((target_sigset_t *)arg1, &set);
1866 }
1867 }
1868 break;
1869 case TARGET_NR_sigsuspend:
1870 {
1871 sigset_t set;
1872 target_to_host_old_sigset(&set, (target_ulong *)arg1);
1873 ret = get_errno(sigsuspend(&set));
1874 }
1875 break;
1876 case TARGET_NR_rt_sigsuspend:
1877 {
1878 sigset_t set;
1879 target_to_host_sigset(&set, (target_sigset_t *)arg1);
1880 ret = get_errno(sigsuspend(&set));
1881 }
1882 break;
1883 case TARGET_NR_rt_sigtimedwait:
1884 {
1885 target_sigset_t *target_set = (void *)arg1;
1886 target_siginfo_t *target_uinfo = (void *)arg2;
1887 struct target_timespec *target_uts = (void *)arg3;
1888 sigset_t set;
1889 struct timespec uts, *puts;
1890 siginfo_t uinfo;
1891
1892 target_to_host_sigset(&set, target_set);
1893 if (target_uts) {
1894 puts = &uts;
1895 puts->tv_sec = tswapl(target_uts->tv_sec);
1896 puts->tv_nsec = tswapl(target_uts->tv_nsec);
1897 } else {
1898 puts = NULL;
1899 }
1900 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
1901 if (!is_error(ret) && target_uinfo) {
1902 host_to_target_siginfo(target_uinfo, &uinfo);
1903 }
1904 }
1905 break;
1906 case TARGET_NR_rt_sigqueueinfo:
1907 {
1908 siginfo_t uinfo;
1909 target_to_host_siginfo(&uinfo, (target_siginfo_t *)arg3);
1910 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
1911 }
1912 break;
1913 case TARGET_NR_sigreturn:
1914 /* NOTE: ret is eax, so not transcoding must be done */
1915 ret = do_sigreturn(cpu_env);
1916 break;
1917 case TARGET_NR_rt_sigreturn:
1918 /* NOTE: ret is eax, so not transcoding must be done */
1919 ret = do_rt_sigreturn(cpu_env);
1920 break;
1921 case TARGET_NR_sethostname:
1922 ret = get_errno(sethostname((const char *)arg1, arg2));
1923 break;
1924 case TARGET_NR_setrlimit:
1925 {
1926 /* XXX: convert resource ? */
1927 int resource = arg1;
1928 struct target_rlimit *target_rlim = (void *)arg2;
1929 struct rlimit rlim;
1930 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
1931 rlim.rlim_max = tswapl(target_rlim->rlim_max);
1932 ret = get_errno(setrlimit(resource, &rlim));
1933 }
1934 break;
1935 case TARGET_NR_getrlimit:
1936 {
1937 /* XXX: convert resource ? */
1938 int resource = arg1;
1939 struct target_rlimit *target_rlim = (void *)arg2;
1940 struct rlimit rlim;
1941
1942 ret = get_errno(getrlimit(resource, &rlim));
1943 if (!is_error(ret)) {
1944 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
1945 target_rlim->rlim_max = tswapl(rlim.rlim_max);
1946 }
1947 }
1948 break;
1949 case TARGET_NR_getrusage:
1950 {
1951 struct rusage rusage;
1952 struct target_rusage *target_rusage = (void *)arg2;
1953 ret = get_errno(getrusage(arg1, &rusage));
1954 if (!is_error(ret)) {
1955 host_to_target_rusage(target_rusage, &rusage);
1956 }
1957 }
1958 break;
1959 case TARGET_NR_gettimeofday:
1960 {
1961 struct target_timeval *target_tv = (void *)arg1;
1962 struct timeval tv;
1963 ret = get_errno(gettimeofday(&tv, NULL));
1964 if (!is_error(ret)) {
1965 host_to_target_timeval(target_tv, &tv);
1966 }
1967 }
1968 break;
1969 case TARGET_NR_settimeofday:
1970 {
1971 struct target_timeval *target_tv = (void *)arg1;
1972 struct timeval tv;
1973 target_to_host_timeval(&tv, target_tv);
1974 ret = get_errno(settimeofday(&tv, NULL));
1975 }
1976 break;
1977 case TARGET_NR_select:
1978 {
1979 struct target_sel_arg_struct *sel = (void *)arg1;
1980 sel->n = tswapl(sel->n);
1981 sel->inp = tswapl(sel->inp);
1982 sel->outp = tswapl(sel->outp);
1983 sel->exp = tswapl(sel->exp);
1984 sel->tvp = tswapl(sel->tvp);
1985 ret = do_select(sel->n, (void *)sel->inp, (void *)sel->outp,
1986 (void *)sel->exp, (void *)sel->tvp);
1987 }
1988 break;
1989 case TARGET_NR_symlink:
1990 ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
1991 break;
1992 #ifdef TARGET_NR_oldlstat
1993 case TARGET_NR_oldlstat:
1994 goto unimplemented;
1995 #endif
1996 case TARGET_NR_readlink:
1997 ret = get_errno(readlink(path((const char *)arg1), (char *)arg2, arg3));
1998 break;
1999 case TARGET_NR_uselib:
2000 goto unimplemented;
2001 case TARGET_NR_swapon:
2002 ret = get_errno(swapon((const char *)arg1, arg2));
2003 break;
2004 case TARGET_NR_reboot:
2005 goto unimplemented;
2006 case TARGET_NR_readdir:
2007 goto unimplemented;
2008 case TARGET_NR_mmap:
2009 #if defined(TARGET_I386) || defined(TARGET_ARM)
2010 {
2011 uint32_t v1, v2, v3, v4, v5, v6, *vptr;
2012 vptr = (uint32_t *)arg1;
2013 v1 = tswap32(vptr[0]);
2014 v2 = tswap32(vptr[1]);
2015 v3 = tswap32(vptr[2]);
2016 v4 = tswap32(vptr[3]);
2017 v5 = tswap32(vptr[4]);
2018 v6 = tswap32(vptr[5]);
2019 ret = get_errno(target_mmap(v1, v2, v3,
2020 target_to_host_bitmask(v4, mmap_flags_tbl),
2021 v5, v6));
2022 }
2023 #else
2024 ret = get_errno(target_mmap(arg1, arg2, arg3,
2025 target_to_host_bitmask(arg4, mmap_flags_tbl),
2026 arg5,
2027 arg6));
2028 #endif
2029 break;
2030 case TARGET_NR_mmap2:
2031 #if defined(TARGET_SPARC)
2032 #define MMAP_SHIFT 12
2033 #else
2034 #define MMAP_SHIFT TARGET_PAGE_BITS
2035 #endif
2036 ret = get_errno(target_mmap(arg1, arg2, arg3,
2037 target_to_host_bitmask(arg4, mmap_flags_tbl),
2038 arg5,
2039 arg6 << MMAP_SHIFT));
2040 break;
2041 case TARGET_NR_munmap:
2042 ret = get_errno(target_munmap(arg1, arg2));
2043 break;
2044 case TARGET_NR_mprotect:
2045 ret = get_errno(target_mprotect(arg1, arg2, arg3));
2046 break;
2047 case TARGET_NR_mremap:
2048 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
2049 break;
2050 case TARGET_NR_msync:
2051 ret = get_errno(msync((void *)arg1, arg2, arg3));
2052 break;
2053 case TARGET_NR_mlock:
2054 ret = get_errno(mlock((void *)arg1, arg2));
2055 break;
2056 case TARGET_NR_munlock:
2057 ret = get_errno(munlock((void *)arg1, arg2));
2058 break;
2059 case TARGET_NR_mlockall:
2060 ret = get_errno(mlockall(arg1));
2061 break;
2062 case TARGET_NR_munlockall:
2063 ret = get_errno(munlockall());
2064 break;
2065 case TARGET_NR_truncate:
2066 ret = get_errno(truncate((const char *)arg1, arg2));
2067 break;
2068 case TARGET_NR_ftruncate:
2069 ret = get_errno(ftruncate(arg1, arg2));
2070 break;
2071 case TARGET_NR_fchmod:
2072 ret = get_errno(fchmod(arg1, arg2));
2073 break;
2074 case TARGET_NR_getpriority:
2075 ret = get_errno(getpriority(arg1, arg2));
2076 break;
2077 case TARGET_NR_setpriority:
2078 ret = get_errno(setpriority(arg1, arg2, arg3));
2079 break;
2080 #ifdef TARGET_NR_profil
2081 case TARGET_NR_profil:
2082 goto unimplemented;
2083 #endif
2084 case TARGET_NR_statfs:
2085 stfs = (void *)arg2;
2086 ret = get_errno(sys_statfs(path((const char *)arg1), stfs));
2087 convert_statfs:
2088 if (!is_error(ret)) {
2089 tswap32s(&stfs->f_type);
2090 tswap32s(&stfs->f_bsize);
2091 tswap32s(&stfs->f_blocks);
2092 tswap32s(&stfs->f_bfree);
2093 tswap32s(&stfs->f_bavail);
2094 tswap32s(&stfs->f_files);
2095 tswap32s(&stfs->f_ffree);
2096 tswap32s(&stfs->f_fsid.val[0]);
2097 tswap32s(&stfs->f_fsid.val[1]);
2098 tswap32s(&stfs->f_namelen);
2099 }
2100 break;
2101 case TARGET_NR_fstatfs:
2102 stfs = (void *)arg2;
2103 ret = get_errno(sys_fstatfs(arg1, stfs));
2104 goto convert_statfs;
2105 #ifdef TARGET_NR_ioperm
2106 case TARGET_NR_ioperm:
2107 goto unimplemented;
2108 #endif
2109 case TARGET_NR_socketcall:
2110 ret = do_socketcall(arg1, (int32_t *)arg2);
2111 break;
2112 case TARGET_NR_syslog:
2113 goto unimplemented;
2114 case TARGET_NR_setitimer:
2115 {
2116 struct target_itimerval *target_value = (void *)arg2;
2117 struct target_itimerval *target_ovalue = (void *)arg3;
2118 struct itimerval value, ovalue, *pvalue;
2119
2120 if (target_value) {
2121 pvalue = &value;
2122 target_to_host_timeval(&pvalue->it_interval,
2123 &target_value->it_interval);
2124 target_to_host_timeval(&pvalue->it_value,
2125 &target_value->it_value);
2126 } else {
2127 pvalue = NULL;
2128 }
2129 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
2130 if (!is_error(ret) && target_ovalue) {
2131 host_to_target_timeval(&target_ovalue->it_interval,
2132 &ovalue.it_interval);
2133 host_to_target_timeval(&target_ovalue->it_value,
2134 &ovalue.it_value);
2135 }
2136 }
2137 break;
2138 case TARGET_NR_getitimer:
2139 {
2140 struct target_itimerval *target_value = (void *)arg2;
2141 struct itimerval value;
2142
2143 ret = get_errno(getitimer(arg1, &value));
2144 if (!is_error(ret) && target_value) {
2145 host_to_target_timeval(&target_value->it_interval,
2146 &value.it_interval);
2147 host_to_target_timeval(&target_value->it_value,
2148 &value.it_value);
2149 }
2150 }
2151 break;
2152 case TARGET_NR_stat:
2153 ret = get_errno(stat(path((const char *)arg1), &st));
2154 goto do_stat;
2155 case TARGET_NR_lstat:
2156 ret = get_errno(lstat(path((const char *)arg1), &st));
2157 goto do_stat;
2158 case TARGET_NR_fstat:
2159 {
2160 ret = get_errno(fstat(arg1, &st));
2161 do_stat:
2162 if (!is_error(ret)) {
2163 struct target_stat *target_st = (void *)arg2;
2164 target_st->st_dev = tswap16(st.st_dev);
2165 target_st->st_ino = tswapl(st.st_ino);
2166 #if defined(TARGET_PPC)
2167 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
2168 target_st->st_uid = tswap32(st.st_uid);
2169 target_st->st_gid = tswap32(st.st_gid);
2170 #else
2171 target_st->st_mode = tswap16(st.st_mode);
2172 target_st->st_uid = tswap16(st.st_uid);
2173 target_st->st_gid = tswap16(st.st_gid);
2174 #endif
2175 target_st->st_nlink = tswap16(st.st_nlink);
2176 target_st->st_rdev = tswap16(st.st_rdev);
2177 target_st->st_size = tswapl(st.st_size);
2178 target_st->st_blksize = tswapl(st.st_blksize);
2179 target_st->st_blocks = tswapl(st.st_blocks);
2180 target_st->target_st_atime = tswapl(st.st_atime);
2181 target_st->target_st_mtime = tswapl(st.st_mtime);
2182 target_st->target_st_ctime = tswapl(st.st_ctime);
2183 }
2184 }
2185 break;
2186 #ifdef TARGET_NR_olduname
2187 case TARGET_NR_olduname:
2188 goto unimplemented;
2189 #endif
2190 #ifdef TARGET_NR_iopl
2191 case TARGET_NR_iopl:
2192 goto unimplemented;
2193 #endif
2194 case TARGET_NR_vhangup:
2195 ret = get_errno(vhangup());
2196 break;
2197 #ifdef TARGET_NR_idle
2198 case TARGET_NR_idle:
2199 goto unimplemented;
2200 #endif
2201 case TARGET_NR_wait4:
2202 {
2203 int status;
2204 target_long *status_ptr = (void *)arg2;
2205 struct rusage rusage, *rusage_ptr;
2206 struct target_rusage *target_rusage = (void *)arg4;
2207 if (target_rusage)
2208 rusage_ptr = &rusage;
2209 else
2210 rusage_ptr = NULL;
2211 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
2212 if (!is_error(ret)) {
2213 if (status_ptr)
2214 *status_ptr = tswap32(status);
2215 if (target_rusage) {
2216 host_to_target_rusage(target_rusage, &rusage);
2217 }
2218 }
2219 }
2220 break;
2221 case TARGET_NR_swapoff:
2222 ret = get_errno(swapoff((const char *)arg1));
2223 break;
2224 case TARGET_NR_sysinfo:
2225 goto unimplemented;
2226 case TARGET_NR_ipc:
2227 goto unimplemented;
2228 case TARGET_NR_fsync:
2229 ret = get_errno(fsync(arg1));
2230 break;
2231 case TARGET_NR_clone:
2232 ret = get_errno(do_fork(cpu_env, arg1, arg2));
2233 break;
2234 #ifdef __NR_exit_group
2235 /* new thread calls */
2236 case TARGET_NR_exit_group:
2237 ret = get_errno(exit_group(arg1));
2238 break;
2239 #endif
2240 case TARGET_NR_setdomainname:
2241 ret = get_errno(setdomainname((const char *)arg1, arg2));
2242 break;
2243 case TARGET_NR_uname:
2244 /* no need to transcode because we use the linux syscall */
2245 ret = get_errno(sys_uname((struct new_utsname *)arg1));
2246 break;
2247 #ifdef TARGET_I386
2248 case TARGET_NR_modify_ldt:
2249 ret = get_errno(do_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
2250 break;
2251 case TARGET_NR_vm86old:
2252 goto unimplemented;
2253 case TARGET_NR_vm86:
2254 ret = do_vm86(cpu_env, arg1, (void *)arg2);
2255 break;
2256 #endif
2257 case TARGET_NR_adjtimex:
2258 goto unimplemented;
2259 case TARGET_NR_create_module:
2260 case TARGET_NR_init_module:
2261 case TARGET_NR_delete_module:
2262 case TARGET_NR_get_kernel_syms:
2263 goto unimplemented;
2264 case TARGET_NR_quotactl:
2265 goto unimplemented;
2266 case TARGET_NR_getpgid:
2267 ret = get_errno(getpgid(arg1));
2268 break;
2269 case TARGET_NR_fchdir:
2270 ret = get_errno(fchdir(arg1));
2271 break;
2272 case TARGET_NR_bdflush:
2273 goto unimplemented;
2274 case TARGET_NR_sysfs:
2275 goto unimplemented;
2276 case TARGET_NR_personality:
2277 ret = get_errno(personality(arg1));
2278 break;
2279 case TARGET_NR_afs_syscall:
2280 goto unimplemented;
2281 case TARGET_NR__llseek:
2282 {
2283 int64_t res;
2284 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
2285 *(int64_t *)arg4 = tswap64(res);
2286 }
2287 break;
2288 case TARGET_NR_getdents:
2289 #if TARGET_LONG_SIZE != 4
2290 #error not supported
2291 #elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
2292 {
2293 struct target_dirent *target_dirp = (void *)arg2;
2294 struct dirent *dirp;
2295 long count = arg3;
2296
2297 dirp = malloc(count);
2298 if (!dirp)
2299 return -ENOMEM;
2300
2301 ret = get_errno(sys_getdents(arg1, dirp, count));
2302 if (!is_error(ret)) {
2303 struct dirent *de;
2304 struct target_dirent *tde;
2305 int len = ret;
2306 int reclen, treclen;
2307 int count1, tnamelen;
2308
2309 count1 = 0;
2310 de = dirp;
2311 tde = target_dirp;
2312 while (len > 0) {
2313 reclen = de->d_reclen;
2314 treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
2315 tde->d_reclen = tswap16(treclen);
2316 tde->d_ino = tswapl(de->d_ino);
2317 tde->d_off = tswapl(de->d_off);
2318 tnamelen = treclen - (2 * sizeof(target_long) + 2);
2319 if (tnamelen > 256)
2320 tnamelen = 256;
2321 strncpy(tde->d_name, de->d_name, tnamelen);
2322 de = (struct dirent *)((char *)de + reclen);
2323 len -= reclen;
2324 tde = (struct dirent *)((char *)tde + treclen);
2325 count1 += treclen;
2326 }
2327 ret = count1;
2328 }
2329 free(dirp);
2330 }
2331 #else
2332 {
2333 struct dirent *dirp = (void *)arg2;
2334 long count = arg3;
2335
2336 ret = get_errno(sys_getdents(arg1, dirp, count));
2337 if (!is_error(ret)) {
2338 struct dirent *de;
2339 int len = ret;
2340 int reclen;
2341 de = dirp;
2342 while (len > 0) {
2343 reclen = de->d_reclen;
2344 if (reclen > len)
2345 break;
2346 de->d_reclen = tswap16(reclen);
2347 tswapls(&de->d_ino);
2348 tswapls(&de->d_off);
2349 de = (struct dirent *)((char *)de + reclen);
2350 len -= reclen;
2351 }
2352 }
2353 }
2354 #endif
2355 break;
2356 case TARGET_NR_getdents64:
2357 {
2358 struct dirent64 *dirp = (void *)arg2;
2359 long count = arg3;
2360 ret = get_errno(sys_getdents64(arg1, dirp, count));
2361 if (!is_error(ret)) {
2362 struct dirent64 *de;
2363 int len = ret;
2364 int reclen;
2365 de = dirp;
2366 while (len > 0) {
2367 reclen = de->d_reclen;
2368 if (reclen > len)
2369 break;
2370 de->d_reclen = tswap16(reclen);
2371 tswap64s(&de->d_ino);
2372 tswap64s(&de->d_off);
2373 de = (struct dirent64 *)((char *)de + reclen);
2374 len -= reclen;
2375 }
2376 }
2377 }
2378 break;
2379 case TARGET_NR__newselect:
2380 ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4,
2381 (void *)arg5);
2382 break;
2383 case TARGET_NR_poll:
2384 {
2385 struct target_pollfd *target_pfd = (void *)arg1;
2386 unsigned int nfds = arg2;
2387 int timeout = arg3;
2388 struct pollfd *pfd;
2389 unsigned int i;
2390
2391 pfd = alloca(sizeof(struct pollfd) * nfds);
2392 for(i = 0; i < nfds; i++) {
2393 pfd[i].fd = tswap32(target_pfd[i].fd);
2394 pfd[i].events = tswap16(target_pfd[i].events);
2395 }
2396 ret = get_errno(poll(pfd, nfds, timeout));
2397 if (!is_error(ret)) {
2398 for(i = 0; i < nfds; i++) {
2399 target_pfd[i].revents = tswap16(pfd[i].revents);
2400 }
2401 }
2402 }
2403 break;
2404 case TARGET_NR_flock:
2405 /* NOTE: the flock constant seems to be the same for every
2406 Linux platform */
2407 ret = get_errno(flock(arg1, arg2));
2408 break;
2409 case TARGET_NR_readv:
2410 {
2411 int count = arg3;
2412 int i;
2413 struct iovec *vec;
2414 struct target_iovec *target_vec = (void *)arg2;
2415
2416 vec = alloca(count * sizeof(struct iovec));
2417 for(i = 0;i < count; i++) {
2418 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2419 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2420 }
2421 ret = get_errno(readv(arg1, vec, count));
2422 }
2423 break;
2424 case TARGET_NR_writev:
2425 {
2426 int count = arg3;
2427 int i;
2428 struct iovec *vec;
2429 struct target_iovec *target_vec = (void *)arg2;
2430
2431 vec = alloca(count * sizeof(struct iovec));
2432 for(i = 0;i < count; i++) {
2433 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2434 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2435 }
2436 ret = get_errno(writev(arg1, vec, count));
2437 }
2438 break;
2439 case TARGET_NR_getsid:
2440 ret = get_errno(getsid(arg1));
2441 break;
2442 case TARGET_NR_fdatasync:
2443 ret = get_errno(fdatasync(arg1));
2444 break;
2445 case TARGET_NR__sysctl:
2446 goto unimplemented;
2447 case TARGET_NR_sched_setparam:
2448 {
2449 struct sched_param *target_schp = (void *)arg2;
2450 struct sched_param schp;
2451 schp.sched_priority = tswap32(target_schp->sched_priority);
2452 ret = get_errno(sched_setparam(arg1, &schp));
2453 }
2454 break;
2455 case TARGET_NR_sched_getparam:
2456 {
2457 struct sched_param *target_schp = (void *)arg2;
2458 struct sched_param schp;
2459 ret = get_errno(sched_getparam(arg1, &schp));
2460 if (!is_error(ret)) {
2461 target_schp->sched_priority = tswap32(schp.sched_priority);
2462 }
2463 }
2464 break;
2465 case TARGET_NR_sched_setscheduler:
2466 {
2467 struct sched_param *target_schp = (void *)arg3;
2468 struct sched_param schp;
2469 schp.sched_priority = tswap32(target_schp->sched_priority);
2470 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
2471 }
2472 break;
2473 case TARGET_NR_sched_getscheduler:
2474 ret = get_errno(sched_getscheduler(arg1));
2475 break;
2476 case TARGET_NR_sched_yield:
2477 ret = get_errno(sched_yield());
2478 break;
2479 case TARGET_NR_sched_get_priority_max:
2480 ret = get_errno(sched_get_priority_max(arg1));
2481 break;
2482 case TARGET_NR_sched_get_priority_min:
2483 ret = get_errno(sched_get_priority_min(arg1));
2484 break;
2485 case TARGET_NR_sched_rr_get_interval:
2486 {
2487 struct target_timespec *target_ts = (void *)arg2;
2488 struct timespec ts;
2489 ret = get_errno(sched_rr_get_interval(arg1, &ts));
2490 if (!is_error(ret)) {
2491 target_ts->tv_sec = tswapl(ts.tv_sec);
2492 target_ts->tv_nsec = tswapl(ts.tv_nsec);
2493 }
2494 }
2495 break;
2496 case TARGET_NR_nanosleep:
2497 {
2498 struct target_timespec *target_req = (void *)arg1;
2499 struct target_timespec *target_rem = (void *)arg2;
2500 struct timespec req, rem;
2501 req.tv_sec = tswapl(target_req->tv_sec);
2502 req.tv_nsec = tswapl(target_req->tv_nsec);
2503 ret = get_errno(nanosleep(&req, &rem));
2504 if (is_error(ret) && target_rem) {
2505 target_rem->tv_sec = tswapl(rem.tv_sec);
2506 target_rem->tv_nsec = tswapl(rem.tv_nsec);
2507 }
2508 }
2509 break;
2510 case TARGET_NR_query_module:
2511 goto unimplemented;
2512 case TARGET_NR_nfsservctl:
2513 goto unimplemented;
2514 case TARGET_NR_prctl:
2515 goto unimplemented;
2516 #ifdef TARGET_NR_pread
2517 case TARGET_NR_pread:
2518 page_unprotect_range((void *)arg2, arg3);
2519 ret = get_errno(pread(arg1, (void *)arg2, arg3, arg4));
2520 break;
2521 case TARGET_NR_pwrite:
2522 ret = get_errno(pwrite(arg1, (void *)arg2, arg3, arg4));
2523 break;
2524 #endif
2525 case TARGET_NR_getcwd:
2526 ret = get_errno(sys_getcwd1((char *)arg1, arg2));
2527 break;
2528 case TARGET_NR_capget:
2529 goto unimplemented;
2530 case TARGET_NR_capset:
2531 goto unimplemented;
2532 case TARGET_NR_sigaltstack:
2533 goto unimplemented;
2534 case TARGET_NR_sendfile:
2535 goto unimplemented;
2536 #ifdef TARGET_NR_getpmsg
2537 case TARGET_NR_getpmsg:
2538 goto unimplemented;
2539 #endif
2540 #ifdef TARGET_NR_putpmsg
2541 case TARGET_NR_putpmsg:
2542 goto unimplemented;
2543 #endif
2544 case TARGET_NR_vfork:
2545 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
2546 break;
2547 #ifdef TARGET_NR_ugetrlimit
2548 case TARGET_NR_ugetrlimit:
2549 {
2550 struct rlimit rlim;
2551 ret = get_errno(getrlimit(arg1, &rlim));
2552 if (!is_error(ret)) {
2553 struct target_rlimit *target_rlim = (void *)arg2;
2554 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
2555 target_rlim->rlim_max = tswapl(rlim.rlim_max);
2556 }
2557 break;
2558 }
2559 #endif
2560 case TARGET_NR_truncate64:
2561 goto unimplemented;
2562 case TARGET_NR_ftruncate64:
2563 goto unimplemented;
2564 case TARGET_NR_stat64:
2565 ret = get_errno(stat(path((const char *)arg1), &st));
2566 goto do_stat64;
2567 case TARGET_NR_lstat64:
2568 ret = get_errno(lstat(path((const char *)arg1), &st));
2569 goto do_stat64;
2570 case TARGET_NR_fstat64:
2571 {
2572 ret = get_errno(fstat(arg1, &st));
2573 do_stat64:
2574 if (!is_error(ret)) {
2575 struct target_stat64 *target_st = (void *)arg2;
2576 memset(target_st, 0, sizeof(struct target_stat64));
2577 put_user(st.st_dev, &target_st->st_dev);
2578 put_user(st.st_ino, &target_st->st_ino);
2579 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
2580 put_user(st.st_ino, &target_st->__st_ino);
2581 #endif
2582 put_user(st.st_mode, &target_st->st_mode);
2583 put_user(st.st_nlink, &target_st->st_nlink);
2584 put_user(st.st_uid, &target_st->st_uid);
2585 put_user(st.st_gid, &target_st->st_gid);
2586 put_user(st.st_rdev, &target_st->st_rdev);
2587 /* XXX: better use of kernel struct */
2588 put_user(st.st_size, &target_st->st_size);
2589 put_user(st.st_blksize, &target_st->st_blksize);
2590 put_user(st.st_blocks, &target_st->st_blocks);
2591 put_user(st.st_atime, &target_st->target_st_atime);
2592 put_user(st.st_mtime, &target_st->target_st_mtime);
2593 put_user(st.st_ctime, &target_st->target_st_ctime);
2594 }
2595 }
2596 break;
2597
2598 #ifdef USE_UID16
2599 case TARGET_NR_lchown:
2600 ret = get_errno(lchown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
2601 break;
2602 case TARGET_NR_getuid:
2603 ret = get_errno(high2lowuid(getuid()));
2604 break;
2605 case TARGET_NR_getgid:
2606 ret = get_errno(high2lowgid(getgid()));
2607 break;
2608 case TARGET_NR_geteuid:
2609 ret = get_errno(high2lowuid(geteuid()));
2610 break;
2611 case TARGET_NR_getegid:
2612 ret = get_errno(high2lowgid(getegid()));
2613 break;
2614 case TARGET_NR_setreuid:
2615 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
2616 break;
2617 case TARGET_NR_setregid:
2618 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
2619 break;
2620 case TARGET_NR_getgroups:
2621 {
2622 int gidsetsize = arg1;
2623 uint16_t *target_grouplist = (void *)arg2;
2624 gid_t *grouplist;
2625 int i;
2626
2627 grouplist = alloca(gidsetsize * sizeof(gid_t));
2628 ret = get_errno(getgroups(gidsetsize, grouplist));
2629 if (!is_error(ret)) {
2630 for(i = 0;i < gidsetsize; i++)
2631 target_grouplist[i] = tswap16(grouplist[i]);
2632 }
2633 }
2634 break;
2635 case TARGET_NR_setgroups:
2636 {
2637 int gidsetsize = arg1;
2638 uint16_t *target_grouplist = (void *)arg2;
2639 gid_t *grouplist;
2640 int i;
2641
2642 grouplist = alloca(gidsetsize * sizeof(gid_t));
2643 for(i = 0;i < gidsetsize; i++)
2644 grouplist[i] = tswap16(target_grouplist[i]);
2645 ret = get_errno(setgroups(gidsetsize, grouplist));
2646 }
2647 break;
2648 case TARGET_NR_fchown:
2649 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
2650 break;
2651 #ifdef TARGET_NR_setresuid
2652 case TARGET_NR_setresuid:
2653 ret = get_errno(setresuid(low2highuid(arg1),
2654 low2highuid(arg2),
2655 low2highuid(arg3)));
2656 break;
2657 #endif
2658 #ifdef TARGET_NR_getresuid
2659 case TARGET_NR_getresuid:
2660 {
2661 int ruid, euid, suid;
2662 ret = get_errno(getresuid(&ruid, &euid, &suid));
2663 if (!is_error(ret)) {
2664 *(uint16_t *)arg1 = tswap16(high2lowuid(ruid));
2665 *(uint16_t *)arg2 = tswap16(high2lowuid(euid));
2666 *(uint16_t *)arg3 = tswap16(high2lowuid(suid));
2667 }
2668 }
2669 break;
2670 #endif
2671 #ifdef TARGET_NR_getresgid
2672 case TARGET_NR_setresgid:
2673 ret = get_errno(setresgid(low2highgid(arg1),
2674 low2highgid(arg2),
2675 low2highgid(arg3)));
2676 break;
2677 #endif
2678 #ifdef TARGET_NR_getresgid
2679 case TARGET_NR_getresgid:
2680 {
2681 int rgid, egid, sgid;
2682 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2683 if (!is_error(ret)) {
2684 *(uint16_t *)arg1 = tswap16(high2lowgid(rgid));
2685 *(uint16_t *)arg2 = tswap16(high2lowgid(egid));
2686 *(uint16_t *)arg3 = tswap16(high2lowgid(sgid));
2687 }
2688 }
2689 break;
2690 #endif
2691 case TARGET_NR_chown:
2692 ret = get_errno(chown((const char *)arg1, low2highuid(arg2), low2highgid(arg3)));
2693 break;
2694 case TARGET_NR_setuid:
2695 ret = get_errno(setuid(low2highuid(arg1)));
2696 break;
2697 case TARGET_NR_setgid:
2698 ret = get_errno(setgid(low2highgid(arg1)));
2699 break;
2700 case TARGET_NR_setfsuid:
2701 ret = get_errno(setfsuid(arg1));
2702 break;
2703 case TARGET_NR_setfsgid:
2704 ret = get_errno(setfsgid(arg1));
2705 break;
2706 #endif /* USE_UID16 */
2707
2708 case TARGET_NR_lchown32:
2709 ret = get_errno(lchown((const char *)arg1, arg2, arg3));
2710 break;
2711 case TARGET_NR_getuid32:
2712 ret = get_errno(getuid());
2713 break;
2714 case TARGET_NR_getgid32:
2715 ret = get_errno(getgid());
2716 break;
2717 case TARGET_NR_geteuid32:
2718 ret = get_errno(geteuid());
2719 break;
2720 case TARGET_NR_getegid32:
2721 ret = get_errno(getegid());
2722 break;
2723 case TARGET_NR_setreuid32:
2724 ret = get_errno(setreuid(arg1, arg2));
2725 break;
2726 case TARGET_NR_setregid32:
2727 ret = get_errno(setregid(arg1, arg2));
2728 break;
2729 case TARGET_NR_getgroups32:
2730 goto unimplemented;
2731 case TARGET_NR_setgroups32:
2732 goto unimplemented;
2733 case TARGET_NR_fchown32:
2734 ret = get_errno(fchown(arg1, arg2, arg3));
2735 break;
2736 case TARGET_NR_setresuid32:
2737 ret = get_errno(setresuid(arg1, arg2, arg3));
2738 break;
2739 case TARGET_NR_getresuid32:
2740 {
2741 int ruid, euid, suid;
2742 ret = get_errno(getresuid(&ruid, &euid, &suid));
2743 if (!is_error(ret)) {
2744 *(uint32_t *)arg1 = tswap32(ruid);
2745 *(uint32_t *)arg2 = tswap32(euid);
2746 *(uint32_t *)arg3 = tswap32(suid);
2747 }
2748 }
2749 break;
2750 case TARGET_NR_setresgid32:
2751 ret = get_errno(setresgid(arg1, arg2, arg3));
2752 break;
2753 case TARGET_NR_getresgid32:
2754 {
2755 int rgid, egid, sgid;
2756 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2757 if (!is_error(ret)) {
2758 *(uint32_t *)arg1 = tswap32(rgid);
2759 *(uint32_t *)arg2 = tswap32(egid);
2760 *(uint32_t *)arg3 = tswap32(sgid);
2761 }
2762 }
2763 break;
2764 case TARGET_NR_chown32:
2765 ret = get_errno(chown((const char *)arg1, arg2, arg3));
2766 break;
2767 case TARGET_NR_setuid32:
2768 ret = get_errno(setuid(arg1));
2769 break;
2770 case TARGET_NR_setgid32:
2771 ret = get_errno(setgid(arg1));
2772 break;
2773 case TARGET_NR_setfsuid32:
2774 ret = get_errno(setfsuid(arg1));
2775 break;
2776 case TARGET_NR_setfsgid32:
2777 ret = get_errno(setfsgid(arg1));
2778 break;
2779
2780 case TARGET_NR_pivot_root:
2781 goto unimplemented;
2782 #ifdef TARGET_NR_mincore
2783 case TARGET_NR_mincore:
2784 goto unimplemented;
2785 #endif
2786 #ifdef TARGET_NR_madvise
2787 case TARGET_NR_madvise:
2788 goto unimplemented;
2789 #endif
2790 #if TARGET_LONG_BITS == 32
2791 case TARGET_NR_fcntl64:
2792 {
2793 struct flock64 fl;
2794 struct target_flock64 *target_fl = (void *)arg3;
2795
2796 switch(arg2) {
2797 case F_GETLK64:
2798 ret = get_errno(fcntl(arg1, arg2, &fl));
2799 if (ret == 0) {
2800 target_fl->l_type = tswap16(fl.l_type);
2801 target_fl->l_whence = tswap16(fl.l_whence);
2802 target_fl->l_start = tswap64(fl.l_start);
2803 target_fl->l_len = tswap64(fl.l_len);
2804 target_fl->l_pid = tswapl(fl.l_pid);
2805 }
2806 break;
2807
2808 case F_SETLK64:
2809 case F_SETLKW64:
2810 fl.l_type = tswap16(target_fl->l_type);
2811 fl.l_whence = tswap16(target_fl->l_whence);
2812 fl.l_start = tswap64(target_fl->l_start);
2813 fl.l_len = tswap64(target_fl->l_len);
2814 fl.l_pid = tswapl(target_fl->l_pid);
2815 ret = get_errno(fcntl(arg1, arg2, &fl));
2816 break;
2817 default:
2818 ret = get_errno(do_fcntl(arg1, arg2, arg3));
2819 break;
2820 }
2821 break;
2822 }
2823 #endif
2824 #ifdef TARGET_NR_security
2825 case TARGET_NR_security:
2826 goto unimplemented;
2827 #endif
2828 #ifdef TARGET_NR_getpagesize
2829 case TARGET_NR_getpagesize:
2830 ret = TARGET_PAGE_SIZE;
2831 break;
2832 #endif
2833 case TARGET_NR_gettid:
2834 ret = get_errno(gettid());
2835 break;
2836 case TARGET_NR_readahead:
2837 goto unimplemented;
2838 #ifdef TARGET_NR_setxattr
2839 case TARGET_NR_setxattr:
2840 case TARGET_NR_lsetxattr:
2841 case TARGET_NR_fsetxattr:
2842 case TARGET_NR_getxattr:
2843 case TARGET_NR_lgetxattr:
2844 case TARGET_NR_fgetxattr:
2845 case TARGET_NR_listxattr:
2846 case TARGET_NR_llistxattr:
2847 case TARGET_NR_flistxattr:
2848 case TARGET_NR_removexattr:
2849 case TARGET_NR_lremovexattr:
2850 case TARGET_NR_fremovexattr:
2851 goto unimplemented_nowarn;
2852 #endif
2853 #ifdef TARGET_NR_set_thread_area
2854 case TARGET_NR_set_thread_area:
2855 case TARGET_NR_get_thread_area:
2856 goto unimplemented_nowarn;
2857 #endif
2858 default:
2859 unimplemented:
2860 gemu_log("qemu: Unsupported syscall: %d\n", num);
2861 unimplemented_nowarn:
2862 ret = -ENOSYS;
2863 break;
2864 }
2865 fail:
2866 #ifdef DEBUG
2867 gemu_log(" = %ld\n", ret);
2868 #endif
2869 return ret;
2870 }
2871