]> git.proxmox.com Git - qemu.git/blob - linux-user/syscall.c
endian fixes by Ulrich weigand
[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/user.h>
44 #include <netinet/tcp.h>
45
46 #define termios host_termios
47 #define winsize host_winsize
48 #define termio host_termio
49 #define sgttyb host_sgttyb /* same as target */
50 #define tchars host_tchars /* same as target */
51 #define ltchars host_ltchars /* same as target */
52
53 #include <linux/termios.h>
54 #include <linux/unistd.h>
55 #include <linux/utsname.h>
56 #include <linux/cdrom.h>
57 #include <linux/hdreg.h>
58 #include <linux/soundcard.h>
59 #include <linux/dirent.h>
60
61 #include "qemu.h"
62
63 //#define DEBUG
64
65 #ifndef PAGE_SIZE
66 #define PAGE_SIZE 4096
67 #define PAGE_MASK ~(PAGE_SIZE - 1)
68 #endif
69
70 //#include <linux/msdos_fs.h>
71 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
72 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
73
74 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
75 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
76 long do_sigreturn(CPUX86State *env);
77 long do_rt_sigreturn(CPUX86State *env);
78
79 #define __NR_sys_uname __NR_uname
80 #define __NR_sys_getcwd1 __NR_getcwd
81 #define __NR_sys_statfs __NR_statfs
82 #define __NR_sys_fstatfs __NR_fstatfs
83 #define __NR_sys_getdents __NR_getdents
84 #define __NR_sys_getdents64 __NR_getdents64
85 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
86
87 #ifdef __NR_gettid
88 _syscall0(int, gettid)
89 #else
90 static int gettid(void) {
91 return -ENOSYS;
92 }
93 #endif
94 _syscall1(int,sys_uname,struct new_utsname *,buf)
95 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
96 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
97 _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
98 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
99 loff_t *, res, uint, wh);
100 _syscall2(int,sys_statfs,const char *,path,struct kernel_statfs *,buf)
101 _syscall2(int,sys_fstatfs,int,fd,struct kernel_statfs *,buf)
102 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
103
104 extern int personality(int);
105 extern int flock(int, int);
106 extern int setfsuid(int);
107 extern int setfsgid(int);
108 extern int setresuid(uid_t, uid_t, uid_t);
109 extern int getresuid(uid_t *, uid_t *, uid_t *);
110 extern int setresgid(gid_t, gid_t, gid_t);
111 extern int getresgid(gid_t *, gid_t *, gid_t *);
112
113 static inline long get_errno(long ret)
114 {
115 if (ret == -1)
116 return -errno;
117 else
118 return ret;
119 }
120
121 static inline int is_error(long ret)
122 {
123 return (unsigned long)ret >= (unsigned long)(-4096);
124 }
125
126 static char *target_brk;
127 static char *target_original_brk;
128
129 void target_set_brk(char *new_brk)
130 {
131 target_brk = new_brk;
132 target_original_brk = new_brk;
133 }
134
135 static long do_brk(char *new_brk)
136 {
137 char *brk_page;
138 long mapped_addr;
139 int new_alloc_size;
140
141 if (!new_brk)
142 return (long)target_brk;
143 if (new_brk < target_original_brk)
144 return -ENOMEM;
145
146 brk_page = (char *)(((unsigned long)target_brk + PAGE_SIZE - 1) & PAGE_MASK);
147
148 /* If the new brk is less than this, set it and we're done... */
149 if (new_brk < brk_page) {
150 target_brk = new_brk;
151 return (long)target_brk;
152 }
153
154 /* We need to allocate more memory after the brk... */
155 new_alloc_size = ((new_brk - brk_page + 1)+(PAGE_SIZE-1)) & PAGE_MASK;
156 mapped_addr = get_errno((long)mmap((caddr_t)brk_page, new_alloc_size,
157 PROT_READ|PROT_WRITE,
158 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
159
160 if (is_error(mapped_addr)) {
161 return mapped_addr;
162 } else {
163 target_brk = new_brk;
164 return (long)target_brk;
165 }
166 }
167
168 static inline fd_set *target_to_host_fds(fd_set *fds,
169 target_long *target_fds, int n)
170 {
171 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
172 return (fd_set *)target_fds;
173 #else
174 int i, b;
175 if (target_fds) {
176 FD_ZERO(fds);
177 for(i = 0;i < n; i++) {
178 b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
179 (i & (TARGET_LONG_BITS - 1))) & 1;
180 if (b)
181 FD_SET(i, fds);
182 }
183 return fds;
184 } else {
185 return NULL;
186 }
187 #endif
188 }
189
190 static inline void host_to_target_fds(target_long *target_fds,
191 fd_set *fds, int n)
192 {
193 #if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
194 /* nothing to do */
195 #else
196 int i, nw, j, k;
197 target_long v;
198
199 if (target_fds) {
200 nw = n / TARGET_LONG_BITS;
201 k = 0;
202 for(i = 0;i < nw; i++) {
203 v = 0;
204 for(j = 0; j < TARGET_LONG_BITS; j++) {
205 v |= ((FD_ISSET(k, fds) != 0) << j);
206 k++;
207 }
208 target_fds[i] = tswapl(v);
209 }
210 }
211 #endif
212 }
213
214 static inline void target_to_host_timeval(struct timeval *tv,
215 const struct target_timeval *target_tv)
216 {
217 tv->tv_sec = tswapl(target_tv->tv_sec);
218 tv->tv_usec = tswapl(target_tv->tv_usec);
219 }
220
221 static inline void host_to_target_timeval(struct target_timeval *target_tv,
222 const struct timeval *tv)
223 {
224 target_tv->tv_sec = tswapl(tv->tv_sec);
225 target_tv->tv_usec = tswapl(tv->tv_usec);
226 }
227
228
229 static long do_select(long n,
230 target_long *target_rfds, target_long *target_wfds,
231 target_long *target_efds, struct target_timeval *target_tv)
232 {
233 fd_set rfds, wfds, efds;
234 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
235 struct timeval tv, *tv_ptr;
236 long ret;
237
238 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
239 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
240 efds_ptr = target_to_host_fds(&efds, target_efds, n);
241
242 if (target_tv) {
243 target_to_host_timeval(&tv, target_tv);
244 tv_ptr = &tv;
245 } else {
246 tv_ptr = NULL;
247 }
248 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
249 if (!is_error(ret)) {
250 host_to_target_fds(target_rfds, rfds_ptr, n);
251 host_to_target_fds(target_wfds, wfds_ptr, n);
252 host_to_target_fds(target_efds, efds_ptr, n);
253
254 if (target_tv) {
255 host_to_target_timeval(target_tv, &tv);
256 }
257 }
258 return ret;
259 }
260
261 static inline void target_to_host_sockaddr(struct sockaddr *addr,
262 struct target_sockaddr *target_addr,
263 socklen_t len)
264 {
265 memcpy(addr, target_addr, len);
266 addr->sa_family = tswap16(target_addr->sa_family);
267 }
268
269 static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
270 struct sockaddr *addr,
271 socklen_t len)
272 {
273 memcpy(target_addr, addr, len);
274 target_addr->sa_family = tswap16(addr->sa_family);
275 }
276
277 static inline void target_to_host_cmsg(struct msghdr *msgh,
278 struct target_msghdr *target_msgh)
279 {
280 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
281 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
282 socklen_t space = 0;
283
284 while (cmsg && target_cmsg) {
285 void *data = CMSG_DATA(cmsg);
286 void *target_data = TARGET_CMSG_DATA(target_cmsg);
287
288 int len = tswapl(target_cmsg->cmsg_len)
289 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
290
291 space += CMSG_SPACE(len);
292 if (space > msgh->msg_controllen) {
293 space -= CMSG_SPACE(len);
294 gemu_log("Host cmsg overflow");
295 break;
296 }
297
298 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
299 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
300 cmsg->cmsg_len = CMSG_LEN(len);
301
302 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
303 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
304 memcpy(data, target_data, len);
305 } else {
306 int *fd = (int *)data;
307 int *target_fd = (int *)target_data;
308 int i, numfds = len / sizeof(int);
309
310 for (i = 0; i < numfds; i++)
311 fd[i] = tswap32(target_fd[i]);
312 }
313
314 cmsg = CMSG_NXTHDR(msgh, cmsg);
315 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
316 }
317
318 msgh->msg_controllen = space;
319 }
320
321 static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
322 struct msghdr *msgh)
323 {
324 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
325 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
326 socklen_t space = 0;
327
328 while (cmsg && target_cmsg) {
329 void *data = CMSG_DATA(cmsg);
330 void *target_data = TARGET_CMSG_DATA(target_cmsg);
331
332 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
333
334 space += TARGET_CMSG_SPACE(len);
335 if (space > tswapl(target_msgh->msg_controllen)) {
336 space -= TARGET_CMSG_SPACE(len);
337 gemu_log("Target cmsg overflow");
338 break;
339 }
340
341 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
342 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
343 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
344
345 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
346 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
347 memcpy(target_data, data, len);
348 } else {
349 int *fd = (int *)data;
350 int *target_fd = (int *)target_data;
351 int i, numfds = len / sizeof(int);
352
353 for (i = 0; i < numfds; i++)
354 target_fd[i] = tswap32(fd[i]);
355 }
356
357 cmsg = CMSG_NXTHDR(msgh, cmsg);
358 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
359 }
360
361 msgh->msg_controllen = tswapl(space);
362 }
363
364 static long do_setsockopt(int sockfd, int level, int optname,
365 void *optval, socklen_t optlen)
366 {
367 if (level == SOL_TCP) {
368 /* TCP options all take an 'int' value. */
369 int val;
370
371 if (optlen < sizeof(uint32_t))
372 return -EINVAL;
373
374 val = tswap32(*(uint32_t *)optval);
375 return get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
376 }
377
378 else if (level != SOL_SOCKET) {
379 gemu_log("Unsupported setsockopt level: %d\n", level);
380 return -ENOSYS;
381 }
382
383 switch (optname) {
384 /* Options with 'int' argument. */
385 case SO_DEBUG:
386 case SO_REUSEADDR:
387 case SO_TYPE:
388 case SO_ERROR:
389 case SO_DONTROUTE:
390 case SO_BROADCAST:
391 case SO_SNDBUF:
392 case SO_RCVBUF:
393 case SO_KEEPALIVE:
394 case SO_OOBINLINE:
395 case SO_NO_CHECK:
396 case SO_PRIORITY:
397 case SO_BSDCOMPAT:
398 case SO_PASSCRED:
399 case SO_TIMESTAMP:
400 case SO_RCVLOWAT:
401 case SO_RCVTIMEO:
402 case SO_SNDTIMEO:
403 {
404 int val;
405 if (optlen < sizeof(uint32_t))
406 return -EINVAL;
407 val = tswap32(*(uint32_t *)optval);
408 return get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
409 }
410
411 default:
412 gemu_log("Unsupported setsockopt SOL_SOCKET option: %d\n", optname);
413 return -ENOSYS;
414 }
415 }
416
417 static long do_getsockopt(int sockfd, int level, int optname,
418 void *optval, socklen_t *optlen)
419 {
420 gemu_log("getsockopt not yet supported\n");
421 return -ENOSYS;
422 }
423
424 static long do_socketcall(int num, int32_t *vptr)
425 {
426 long ret;
427
428 switch(num) {
429 case SOCKOP_socket:
430 {
431 int domain = tswap32(vptr[0]);
432 int type = tswap32(vptr[1]);
433 int protocol = tswap32(vptr[2]);
434
435 ret = get_errno(socket(domain, type, protocol));
436 }
437 break;
438 case SOCKOP_bind:
439 {
440 int sockfd = tswap32(vptr[0]);
441 void *target_addr = (void *)tswap32(vptr[1]);
442 socklen_t addrlen = tswap32(vptr[2]);
443 void *addr = alloca(addrlen);
444
445 target_to_host_sockaddr(addr, target_addr, addrlen);
446 ret = get_errno(bind(sockfd, addr, addrlen));
447 }
448 break;
449 case SOCKOP_connect:
450 {
451 int sockfd = tswap32(vptr[0]);
452 void *target_addr = (void *)tswap32(vptr[1]);
453 socklen_t addrlen = tswap32(vptr[2]);
454 void *addr = alloca(addrlen);
455
456 target_to_host_sockaddr(addr, target_addr, addrlen);
457 ret = get_errno(connect(sockfd, addr, addrlen));
458 }
459 break;
460 case SOCKOP_listen:
461 {
462 int sockfd = tswap32(vptr[0]);
463 int backlog = tswap32(vptr[1]);
464
465 ret = get_errno(listen(sockfd, backlog));
466 }
467 break;
468 case SOCKOP_accept:
469 {
470 int sockfd = tswap32(vptr[0]);
471 void *target_addr = (void *)tswap32(vptr[1]);
472 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
473 socklen_t addrlen = tswap32(*target_addrlen);
474 void *addr = alloca(addrlen);
475
476 ret = get_errno(accept(sockfd, addr, &addrlen));
477 if (!is_error(ret)) {
478 host_to_target_sockaddr(target_addr, addr, addrlen);
479 *target_addrlen = tswap32(addrlen);
480 }
481 }
482 break;
483 case SOCKOP_getsockname:
484 {
485 int sockfd = tswap32(vptr[0]);
486 void *target_addr = (void *)tswap32(vptr[1]);
487 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
488 socklen_t addrlen = tswap32(*target_addrlen);
489 void *addr = alloca(addrlen);
490
491 ret = get_errno(getsockname(sockfd, addr, &addrlen));
492 if (!is_error(ret)) {
493 host_to_target_sockaddr(target_addr, addr, addrlen);
494 *target_addrlen = tswap32(addrlen);
495 }
496 }
497 break;
498 case SOCKOP_getpeername:
499 {
500 int sockfd = tswap32(vptr[0]);
501 void *target_addr = (void *)tswap32(vptr[1]);
502 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
503 socklen_t addrlen = tswap32(*target_addrlen);
504 void *addr = alloca(addrlen);
505
506 ret = get_errno(getpeername(sockfd, addr, &addrlen));
507 if (!is_error(ret)) {
508 host_to_target_sockaddr(target_addr, addr, addrlen);
509 *target_addrlen = tswap32(addrlen);
510 }
511 }
512 break;
513 case SOCKOP_socketpair:
514 {
515 int domain = tswap32(vptr[0]);
516 int type = tswap32(vptr[1]);
517 int protocol = tswap32(vptr[2]);
518 int32_t *target_tab = (void *)tswap32(vptr[3]);
519 int tab[2];
520
521 ret = get_errno(socketpair(domain, type, protocol, tab));
522 if (!is_error(ret)) {
523 target_tab[0] = tswap32(tab[0]);
524 target_tab[1] = tswap32(tab[1]);
525 }
526 }
527 break;
528 case SOCKOP_send:
529 {
530 int sockfd = tswap32(vptr[0]);
531 void *msg = (void *)tswap32(vptr[1]);
532 size_t len = tswap32(vptr[2]);
533 int flags = tswap32(vptr[3]);
534
535 ret = get_errno(send(sockfd, msg, len, flags));
536 }
537 break;
538 case SOCKOP_recv:
539 {
540 int sockfd = tswap32(vptr[0]);
541 void *msg = (void *)tswap32(vptr[1]);
542 size_t len = tswap32(vptr[2]);
543 int flags = tswap32(vptr[3]);
544
545 ret = get_errno(recv(sockfd, msg, len, flags));
546 }
547 break;
548 case SOCKOP_sendto:
549 {
550 int sockfd = tswap32(vptr[0]);
551 void *msg = (void *)tswap32(vptr[1]);
552 size_t len = tswap32(vptr[2]);
553 int flags = tswap32(vptr[3]);
554 void *target_addr = (void *)tswap32(vptr[4]);
555 socklen_t addrlen = tswap32(vptr[5]);
556 void *addr = alloca(addrlen);
557
558 target_to_host_sockaddr(addr, target_addr, addrlen);
559 ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
560 }
561 break;
562 case SOCKOP_recvfrom:
563 {
564 int sockfd = tswap32(vptr[0]);
565 void *msg = (void *)tswap32(vptr[1]);
566 size_t len = tswap32(vptr[2]);
567 int flags = tswap32(vptr[3]);
568 void *target_addr = (void *)tswap32(vptr[4]);
569 uint32_t *target_addrlen = (void *)tswap32(vptr[5]);
570 socklen_t addrlen = tswap32(*target_addrlen);
571 void *addr = alloca(addrlen);
572
573 ret = get_errno(recvfrom(sockfd, msg, len, flags, addr, &addrlen));
574 if (!is_error(ret)) {
575 host_to_target_sockaddr(target_addr, addr, addrlen);
576 *target_addrlen = tswap32(addrlen);
577 }
578 }
579 break;
580 case SOCKOP_shutdown:
581 {
582 int sockfd = tswap32(vptr[0]);
583 int how = tswap32(vptr[1]);
584
585 ret = get_errno(shutdown(sockfd, how));
586 }
587 break;
588 case SOCKOP_sendmsg:
589 case SOCKOP_recvmsg:
590 {
591 int fd;
592 struct target_msghdr *msgp;
593 struct msghdr msg;
594 int flags, count, i;
595 struct iovec *vec;
596 struct target_iovec *target_vec;
597
598 msgp = (void *)tswap32(vptr[1]);
599 msg.msg_name = (void *)tswapl(msgp->msg_name);
600 msg.msg_namelen = tswapl(msgp->msg_namelen);
601 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
602 msg.msg_control = alloca(msg.msg_controllen);
603 msg.msg_flags = tswap32(msgp->msg_flags);
604
605 count = tswapl(msgp->msg_iovlen);
606 vec = alloca(count * sizeof(struct iovec));
607 target_vec = (void *)tswapl(msgp->msg_iov);
608 for(i = 0;i < count; i++) {
609 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
610 vec[i].iov_len = tswapl(target_vec[i].iov_len);
611 }
612 msg.msg_iovlen = count;
613 msg.msg_iov = vec;
614
615 fd = tswap32(vptr[0]);
616 flags = tswap32(vptr[2]);
617 if (num == SOCKOP_sendmsg) {
618 target_to_host_cmsg(&msg, msgp);
619 ret = get_errno(sendmsg(fd, &msg, flags));
620 } else {
621 ret = get_errno(recvmsg(fd, &msg, flags));
622 if (!is_error(ret))
623 host_to_target_cmsg(msgp, &msg);
624 }
625 }
626 break;
627 case SOCKOP_setsockopt:
628 {
629 int sockfd = tswap32(vptr[0]);
630 int level = tswap32(vptr[1]);
631 int optname = tswap32(vptr[2]);
632 void *optval = (void *)tswap32(vptr[3]);
633 socklen_t optlen = tswap32(vptr[4]);
634
635 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
636 }
637 break;
638 case SOCKOP_getsockopt:
639 {
640 int sockfd = tswap32(vptr[0]);
641 int level = tswap32(vptr[1]);
642 int optname = tswap32(vptr[2]);
643 void *optval = (void *)tswap32(vptr[3]);
644 uint32_t *target_len = (void *)tswap32(vptr[4]);
645 socklen_t optlen = tswap32(*target_len);
646
647 ret = do_getsockopt(sockfd, level, optname, optval, &optlen);
648 if (!is_error(ret))
649 *target_len = tswap32(optlen);
650 }
651 break;
652 default:
653 gemu_log("Unsupported socketcall: %d\n", num);
654 ret = -ENOSYS;
655 break;
656 }
657 return ret;
658 }
659
660 /* kernel structure types definitions */
661 #define IFNAMSIZ 16
662
663 #define STRUCT(name, list...) STRUCT_ ## name,
664 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
665 enum {
666 #include "syscall_types.h"
667 };
668 #undef STRUCT
669 #undef STRUCT_SPECIAL
670
671 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
672 #define STRUCT_SPECIAL(name)
673 #include "syscall_types.h"
674 #undef STRUCT
675 #undef STRUCT_SPECIAL
676
677 typedef struct IOCTLEntry {
678 int target_cmd;
679 int host_cmd;
680 const char *name;
681 int access;
682 const argtype arg_type[5];
683 } IOCTLEntry;
684
685 #define IOC_R 0x0001
686 #define IOC_W 0x0002
687 #define IOC_RW (IOC_R | IOC_W)
688
689 #define MAX_STRUCT_SIZE 4096
690
691 const IOCTLEntry ioctl_entries[] = {
692 #define IOCTL(cmd, access, types...) \
693 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
694 #include "ioctls.h"
695 { 0, 0, },
696 };
697
698 static long do_ioctl(long fd, long cmd, long arg)
699 {
700 const IOCTLEntry *ie;
701 const argtype *arg_type;
702 long ret;
703 uint8_t buf_temp[MAX_STRUCT_SIZE];
704
705 ie = ioctl_entries;
706 for(;;) {
707 if (ie->target_cmd == 0) {
708 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
709 return -ENOSYS;
710 }
711 if (ie->target_cmd == cmd)
712 break;
713 ie++;
714 }
715 arg_type = ie->arg_type;
716 #if defined(DEBUG)
717 gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
718 #endif
719 switch(arg_type[0]) {
720 case TYPE_NULL:
721 /* no argument */
722 ret = get_errno(ioctl(fd, ie->host_cmd));
723 break;
724 case TYPE_PTRVOID:
725 case TYPE_INT:
726 /* int argment */
727 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
728 break;
729 case TYPE_PTR:
730 arg_type++;
731 switch(ie->access) {
732 case IOC_R:
733 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
734 if (!is_error(ret)) {
735 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
736 }
737 break;
738 case IOC_W:
739 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
740 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
741 break;
742 default:
743 case IOC_RW:
744 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
745 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
746 if (!is_error(ret)) {
747 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
748 }
749 break;
750 }
751 break;
752 default:
753 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
754 ret = -ENOSYS;
755 break;
756 }
757 return ret;
758 }
759
760 bitmask_transtbl iflag_tbl[] = {
761 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
762 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
763 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
764 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
765 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
766 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
767 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
768 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
769 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
770 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
771 { TARGET_IXON, TARGET_IXON, IXON, IXON },
772 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
773 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
774 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
775 { 0, 0, 0, 0 }
776 };
777
778 bitmask_transtbl oflag_tbl[] = {
779 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
780 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
781 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
782 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
783 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
784 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
785 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
786 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
787 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
788 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
789 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
790 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
791 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
792 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
793 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
794 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
795 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
796 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
797 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
798 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
799 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
800 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
801 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
802 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
803 { 0, 0, 0, 0 }
804 };
805
806 bitmask_transtbl cflag_tbl[] = {
807 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
808 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
809 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
810 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
811 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
812 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
813 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
814 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
815 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
816 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
817 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
818 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
819 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
820 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
821 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
822 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
823 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
824 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
825 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
826 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
827 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
828 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
829 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
830 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
831 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
832 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
833 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
834 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
835 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
836 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
837 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
838 { 0, 0, 0, 0 }
839 };
840
841 bitmask_transtbl lflag_tbl[] = {
842 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
843 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
844 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
845 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
846 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
847 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
848 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
849 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
850 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
851 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
852 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
853 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
854 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
855 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
856 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
857 { 0, 0, 0, 0 }
858 };
859
860 static void target_to_host_termios (void *dst, const void *src)
861 {
862 struct host_termios *host = dst;
863 const struct target_termios *target = src;
864
865 host->c_iflag =
866 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
867 host->c_oflag =
868 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
869 host->c_cflag =
870 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
871 host->c_lflag =
872 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
873 host->c_line = target->c_line;
874
875 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
876 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
877 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
878 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
879 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
880 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
881 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
882 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
883 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
884 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
885 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
886 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
887 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
888 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
889 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
890 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
891 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
892 }
893
894 static void host_to_target_termios (void *dst, const void *src)
895 {
896 struct target_termios *target = dst;
897 const struct host_termios *host = src;
898
899 target->c_iflag =
900 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
901 target->c_oflag =
902 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
903 target->c_cflag =
904 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
905 target->c_lflag =
906 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
907 target->c_line = host->c_line;
908
909 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
910 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
911 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
912 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
913 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
914 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
915 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
916 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
917 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
918 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
919 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
920 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
921 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
922 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
923 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
924 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
925 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
926 }
927
928 StructEntry struct_termios_def = {
929 .convert = { host_to_target_termios, target_to_host_termios },
930 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
931 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
932 };
933
934 #ifdef TARGET_I386
935
936 /* NOTE: there is really one LDT for all the threads */
937 uint8_t *ldt_table;
938
939 static int read_ldt(void *ptr, unsigned long bytecount)
940 {
941 int size;
942
943 if (!ldt_table)
944 return 0;
945 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
946 if (size > bytecount)
947 size = bytecount;
948 memcpy(ptr, ldt_table, size);
949 return size;
950 }
951
952 /* XXX: add locking support */
953 static int write_ldt(CPUX86State *env,
954 void *ptr, unsigned long bytecount, int oldmode)
955 {
956 struct target_modify_ldt_ldt_s ldt_info;
957 int seg_32bit, contents, read_exec_only, limit_in_pages;
958 int seg_not_present, useable;
959 uint32_t *lp, entry_1, entry_2;
960
961 if (bytecount != sizeof(ldt_info))
962 return -EINVAL;
963 memcpy(&ldt_info, ptr, sizeof(ldt_info));
964 tswap32s(&ldt_info.entry_number);
965 tswapls((long *)&ldt_info.base_addr);
966 tswap32s(&ldt_info.limit);
967 tswap32s(&ldt_info.flags);
968
969 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
970 return -EINVAL;
971 seg_32bit = ldt_info.flags & 1;
972 contents = (ldt_info.flags >> 1) & 3;
973 read_exec_only = (ldt_info.flags >> 3) & 1;
974 limit_in_pages = (ldt_info.flags >> 4) & 1;
975 seg_not_present = (ldt_info.flags >> 5) & 1;
976 useable = (ldt_info.flags >> 6) & 1;
977
978 if (contents == 3) {
979 if (oldmode)
980 return -EINVAL;
981 if (seg_not_present == 0)
982 return -EINVAL;
983 }
984 /* allocate the LDT */
985 if (!ldt_table) {
986 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
987 if (!ldt_table)
988 return -ENOMEM;
989 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
990 env->ldt.base = ldt_table;
991 env->ldt.limit = 0xffff;
992 }
993
994 /* NOTE: same code as Linux kernel */
995 /* Allow LDTs to be cleared by the user. */
996 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
997 if (oldmode ||
998 (contents == 0 &&
999 read_exec_only == 1 &&
1000 seg_32bit == 0 &&
1001 limit_in_pages == 0 &&
1002 seg_not_present == 1 &&
1003 useable == 0 )) {
1004 entry_1 = 0;
1005 entry_2 = 0;
1006 goto install;
1007 }
1008 }
1009
1010 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
1011 (ldt_info.limit & 0x0ffff);
1012 entry_2 = (ldt_info.base_addr & 0xff000000) |
1013 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
1014 (ldt_info.limit & 0xf0000) |
1015 ((read_exec_only ^ 1) << 9) |
1016 (contents << 10) |
1017 ((seg_not_present ^ 1) << 15) |
1018 (seg_32bit << 22) |
1019 (limit_in_pages << 23) |
1020 0x7000;
1021 if (!oldmode)
1022 entry_2 |= (useable << 20);
1023
1024 /* Install the new entry ... */
1025 install:
1026 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
1027 lp[0] = tswap32(entry_1);
1028 lp[1] = tswap32(entry_2);
1029 return 0;
1030 }
1031
1032 /* specific and weird i386 syscalls */
1033 int do_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
1034 {
1035 int ret = -ENOSYS;
1036
1037 switch (func) {
1038 case 0:
1039 ret = read_ldt(ptr, bytecount);
1040 break;
1041 case 1:
1042 ret = write_ldt(env, ptr, bytecount, 1);
1043 break;
1044 case 0x11:
1045 ret = write_ldt(env, ptr, bytecount, 0);
1046 break;
1047 }
1048 return ret;
1049 }
1050
1051 /* vm86 emulation */
1052
1053 #define SAFE_MASK (0xDD5)
1054
1055 int do_vm86(CPUX86State *env, long subfunction,
1056 struct target_vm86plus_struct * target_v86)
1057 {
1058 TaskState *ts = env->opaque;
1059 int ret;
1060
1061 switch (subfunction) {
1062 case TARGET_VM86_REQUEST_IRQ:
1063 case TARGET_VM86_FREE_IRQ:
1064 case TARGET_VM86_GET_IRQ_BITS:
1065 case TARGET_VM86_GET_AND_RESET_IRQ:
1066 gemu_log("qemu: unsupported vm86 subfunction (%ld)\n", subfunction);
1067 ret = -EINVAL;
1068 goto out;
1069 case TARGET_VM86_PLUS_INSTALL_CHECK:
1070 /* NOTE: on old vm86 stuff this will return the error
1071 from verify_area(), because the subfunction is
1072 interpreted as (invalid) address to vm86_struct.
1073 So the installation check works.
1074 */
1075 ret = 0;
1076 goto out;
1077 }
1078
1079 ts->target_v86 = target_v86;
1080
1081 /* save current CPU regs */
1082 ts->vm86_saved_regs.eax = 0; /* default vm86 syscall return code */
1083 ts->vm86_saved_regs.ebx = env->regs[R_EBX];
1084 ts->vm86_saved_regs.ecx = env->regs[R_ECX];
1085 ts->vm86_saved_regs.edx = env->regs[R_EDX];
1086 ts->vm86_saved_regs.esi = env->regs[R_ESI];
1087 ts->vm86_saved_regs.edi = env->regs[R_EDI];
1088 ts->vm86_saved_regs.ebp = env->regs[R_EBP];
1089 ts->vm86_saved_regs.esp = env->regs[R_ESP];
1090 ts->vm86_saved_regs.eflags = env->eflags;
1091 ts->vm86_saved_regs.eip = env->eip;
1092 ts->vm86_saved_regs.cs = env->segs[R_CS];
1093 ts->vm86_saved_regs.ss = env->segs[R_SS];
1094 ts->vm86_saved_regs.ds = env->segs[R_DS];
1095 ts->vm86_saved_regs.es = env->segs[R_ES];
1096 ts->vm86_saved_regs.fs = env->segs[R_FS];
1097 ts->vm86_saved_regs.gs = env->segs[R_GS];
1098
1099 /* build vm86 CPU state */
1100 env->eflags = (env->eflags & ~SAFE_MASK) |
1101 (tswap32(target_v86->regs.eflags) & SAFE_MASK) | VM_MASK;
1102
1103 env->regs[R_EBX] = tswap32(target_v86->regs.ebx);
1104 env->regs[R_ECX] = tswap32(target_v86->regs.ecx);
1105 env->regs[R_EDX] = tswap32(target_v86->regs.edx);
1106 env->regs[R_ESI] = tswap32(target_v86->regs.esi);
1107 env->regs[R_EDI] = tswap32(target_v86->regs.edi);
1108 env->regs[R_EBP] = tswap32(target_v86->regs.ebp);
1109 env->regs[R_ESP] = tswap32(target_v86->regs.esp);
1110 env->eip = tswap32(target_v86->regs.eip);
1111 cpu_x86_load_seg(env, R_CS, tswap16(target_v86->regs.cs));
1112 cpu_x86_load_seg(env, R_SS, tswap16(target_v86->regs.ss));
1113 cpu_x86_load_seg(env, R_DS, tswap16(target_v86->regs.ds));
1114 cpu_x86_load_seg(env, R_ES, tswap16(target_v86->regs.es));
1115 cpu_x86_load_seg(env, R_FS, tswap16(target_v86->regs.fs));
1116 cpu_x86_load_seg(env, R_GS, tswap16(target_v86->regs.gs));
1117 ret = tswap32(target_v86->regs.eax); /* eax will be restored at
1118 the end of the syscall */
1119 /* now the virtual CPU is ready for vm86 execution ! */
1120 out:
1121 return ret;
1122 }
1123
1124 /* this stack is the equivalent of the kernel stack associated with a
1125 thread/process */
1126 #define NEW_STACK_SIZE 8192
1127
1128 static int clone_func(void *arg)
1129 {
1130 CPUX86State *env = arg;
1131 cpu_loop(env);
1132 /* never exits */
1133 return 0;
1134 }
1135
1136 int do_fork(CPUX86State *env, unsigned int flags, unsigned long newsp)
1137 {
1138 int ret;
1139 TaskState *ts;
1140 uint8_t *new_stack;
1141 CPUX86State *new_env;
1142
1143 if (flags & CLONE_VM) {
1144 if (!newsp)
1145 newsp = env->regs[R_ESP];
1146 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
1147 memset(ts, 0, sizeof(TaskState));
1148 new_stack = ts->stack;
1149 ts->used = 1;
1150 /* add in task state list */
1151 ts->next = first_task_state;
1152 first_task_state = ts;
1153 /* we create a new CPU instance. */
1154 new_env = cpu_x86_init();
1155 memcpy(new_env, env, sizeof(CPUX86State));
1156 new_env->regs[R_ESP] = newsp;
1157 new_env->regs[R_EAX] = 0;
1158 new_env->opaque = ts;
1159 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1160 } else {
1161 /* if no CLONE_VM, we consider it is a fork */
1162 if ((flags & ~CSIGNAL) != 0)
1163 return -EINVAL;
1164 ret = fork();
1165 }
1166 return ret;
1167 }
1168
1169 #endif
1170
1171 #define high2lowuid(x) (x)
1172 #define high2lowgid(x) (x)
1173 #define low2highuid(x) (x)
1174 #define low2highgid(x) (x)
1175
1176 void syscall_init(void)
1177 {
1178 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
1179 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
1180 #include "syscall_types.h"
1181 #undef STRUCT
1182 #undef STRUCT_SPECIAL
1183 }
1184
1185 long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
1186 long arg4, long arg5, long arg6)
1187 {
1188 long ret;
1189 struct stat st;
1190 struct kernel_statfs *stfs;
1191
1192 #ifdef DEBUG
1193 gemu_log("syscall %d\n", num);
1194 #endif
1195 switch(num) {
1196 case TARGET_NR_exit:
1197 #ifdef HAVE_GPROF
1198 _mcleanup();
1199 #endif
1200 /* XXX: should free thread stack and CPU env */
1201 _exit(arg1);
1202 ret = 0; /* avoid warning */
1203 break;
1204 case TARGET_NR_read:
1205 ret = get_errno(read(arg1, (void *)arg2, arg3));
1206 break;
1207 case TARGET_NR_write:
1208 ret = get_errno(write(arg1, (void *)arg2, arg3));
1209 break;
1210 case TARGET_NR_open:
1211 ret = get_errno(open((const char *)arg1, arg2, arg3));
1212 break;
1213 case TARGET_NR_close:
1214 ret = get_errno(close(arg1));
1215 break;
1216 case TARGET_NR_brk:
1217 ret = do_brk((char *)arg1);
1218 break;
1219 case TARGET_NR_fork:
1220 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
1221 break;
1222 case TARGET_NR_waitpid:
1223 {
1224 int *status = (int *)arg2;
1225 ret = get_errno(waitpid(arg1, status, arg3));
1226 if (!is_error(ret) && status)
1227 tswapls((long *)&status);
1228 }
1229 break;
1230 case TARGET_NR_creat:
1231 ret = get_errno(creat((const char *)arg1, arg2));
1232 break;
1233 case TARGET_NR_link:
1234 ret = get_errno(link((const char *)arg1, (const char *)arg2));
1235 break;
1236 case TARGET_NR_unlink:
1237 ret = get_errno(unlink((const char *)arg1));
1238 break;
1239 case TARGET_NR_execve:
1240 {
1241 char **argp, **envp;
1242 int argc = 0, envc = 0;
1243 uint32_t *p;
1244 char **q;
1245
1246 for (p = (void *)arg2; *p; p++)
1247 argc++;
1248 for (p = (void *)arg3; *p; p++)
1249 envc++;
1250
1251 argp = alloca(argc * sizeof(void *));
1252 envp = alloca(envc * sizeof(void *));
1253
1254 for (p = (void *)arg2, q = argp; *p; p++, q++)
1255 *q = (void *)tswap32(*p);
1256 for (p = (void *)arg3, q = envp; *p; p++, q++)
1257 *q = (void *)tswap32(*p);
1258
1259 ret = get_errno(execve((const char *)arg1, argp, envp));
1260 }
1261 break;
1262 case TARGET_NR_chdir:
1263 ret = get_errno(chdir((const char *)arg1));
1264 break;
1265 case TARGET_NR_time:
1266 {
1267 int *time_ptr = (int *)arg1;
1268 ret = get_errno(time((time_t *)time_ptr));
1269 if (!is_error(ret) && time_ptr)
1270 tswap32s(time_ptr);
1271 }
1272 break;
1273 case TARGET_NR_mknod:
1274 ret = get_errno(mknod((const char *)arg1, arg2, arg3));
1275 break;
1276 case TARGET_NR_chmod:
1277 ret = get_errno(chmod((const char *)arg1, arg2));
1278 break;
1279 case TARGET_NR_lchown:
1280 ret = get_errno(chown((const char *)arg1, arg2, arg3));
1281 break;
1282 case TARGET_NR_break:
1283 goto unimplemented;
1284 case TARGET_NR_oldstat:
1285 goto unimplemented;
1286 case TARGET_NR_lseek:
1287 ret = get_errno(lseek(arg1, arg2, arg3));
1288 break;
1289 case TARGET_NR_getpid:
1290 ret = get_errno(getpid());
1291 break;
1292 case TARGET_NR_mount:
1293 /* need to look at the data field */
1294 goto unimplemented;
1295 case TARGET_NR_umount:
1296 ret = get_errno(umount((const char *)arg1));
1297 break;
1298 case TARGET_NR_setuid:
1299 ret = get_errno(setuid(low2highuid(arg1)));
1300 break;
1301 case TARGET_NR_getuid:
1302 ret = get_errno(getuid());
1303 break;
1304 case TARGET_NR_stime:
1305 {
1306 int *time_ptr = (int *)arg1;
1307 if (time_ptr)
1308 tswap32s(time_ptr);
1309 ret = get_errno(stime((time_t *)time_ptr));
1310 }
1311 break;
1312 case TARGET_NR_ptrace:
1313 goto unimplemented;
1314 case TARGET_NR_alarm:
1315 ret = alarm(arg1);
1316 break;
1317 case TARGET_NR_oldfstat:
1318 goto unimplemented;
1319 case TARGET_NR_pause:
1320 ret = get_errno(pause());
1321 break;
1322 case TARGET_NR_utime:
1323 goto unimplemented;
1324 case TARGET_NR_stty:
1325 goto unimplemented;
1326 case TARGET_NR_gtty:
1327 goto unimplemented;
1328 case TARGET_NR_access:
1329 ret = get_errno(access((const char *)arg1, arg2));
1330 break;
1331 case TARGET_NR_nice:
1332 ret = get_errno(nice(arg1));
1333 break;
1334 case TARGET_NR_ftime:
1335 goto unimplemented;
1336 case TARGET_NR_sync:
1337 sync();
1338 ret = 0;
1339 break;
1340 case TARGET_NR_kill:
1341 ret = get_errno(kill(arg1, arg2));
1342 break;
1343 case TARGET_NR_rename:
1344 ret = get_errno(rename((const char *)arg1, (const char *)arg2));
1345 break;
1346 case TARGET_NR_mkdir:
1347 ret = get_errno(mkdir((const char *)arg1, arg2));
1348 break;
1349 case TARGET_NR_rmdir:
1350 ret = get_errno(rmdir((const char *)arg1));
1351 break;
1352 case TARGET_NR_dup:
1353 ret = get_errno(dup(arg1));
1354 break;
1355 case TARGET_NR_pipe:
1356 {
1357 int *pipe_ptr = (int *)arg1;
1358 ret = get_errno(pipe(pipe_ptr));
1359 if (!is_error(ret)) {
1360 tswap32s(&pipe_ptr[0]);
1361 tswap32s(&pipe_ptr[1]);
1362 }
1363 }
1364 break;
1365 case TARGET_NR_times:
1366 goto unimplemented;
1367 case TARGET_NR_prof:
1368 goto unimplemented;
1369 case TARGET_NR_setgid:
1370 ret = get_errno(setgid(low2highgid(arg1)));
1371 break;
1372 case TARGET_NR_getgid:
1373 ret = get_errno(getgid());
1374 break;
1375 case TARGET_NR_signal:
1376 goto unimplemented;
1377 case TARGET_NR_geteuid:
1378 ret = get_errno(geteuid());
1379 break;
1380 case TARGET_NR_getegid:
1381 ret = get_errno(getegid());
1382 break;
1383 case TARGET_NR_acct:
1384 goto unimplemented;
1385 case TARGET_NR_umount2:
1386 ret = get_errno(umount2((const char *)arg1, arg2));
1387 break;
1388 case TARGET_NR_lock:
1389 goto unimplemented;
1390 case TARGET_NR_ioctl:
1391 ret = do_ioctl(arg1, arg2, arg3);
1392 break;
1393 case TARGET_NR_fcntl:
1394 switch(arg2) {
1395 case F_GETLK:
1396 case F_SETLK:
1397 case F_SETLKW:
1398 goto unimplemented;
1399 default:
1400 ret = get_errno(fcntl(arg1, arg2, arg3));
1401 break;
1402 }
1403 break;
1404 case TARGET_NR_mpx:
1405 goto unimplemented;
1406 case TARGET_NR_setpgid:
1407 ret = get_errno(setpgid(arg1, arg2));
1408 break;
1409 case TARGET_NR_ulimit:
1410 goto unimplemented;
1411 case TARGET_NR_oldolduname:
1412 goto unimplemented;
1413 case TARGET_NR_umask:
1414 ret = get_errno(umask(arg1));
1415 break;
1416 case TARGET_NR_chroot:
1417 ret = get_errno(chroot((const char *)arg1));
1418 break;
1419 case TARGET_NR_ustat:
1420 goto unimplemented;
1421 case TARGET_NR_dup2:
1422 ret = get_errno(dup2(arg1, arg2));
1423 break;
1424 case TARGET_NR_getppid:
1425 ret = get_errno(getppid());
1426 break;
1427 case TARGET_NR_getpgrp:
1428 ret = get_errno(getpgrp());
1429 break;
1430 case TARGET_NR_setsid:
1431 ret = get_errno(setsid());
1432 break;
1433 case TARGET_NR_sigaction:
1434 {
1435 struct target_old_sigaction *old_act = (void *)arg2;
1436 struct target_old_sigaction *old_oact = (void *)arg3;
1437 struct target_sigaction act, oact, *pact;
1438 if (old_act) {
1439 act._sa_handler = old_act->_sa_handler;
1440 target_siginitset(&act.sa_mask, old_act->sa_mask);
1441 act.sa_flags = old_act->sa_flags;
1442 act.sa_restorer = old_act->sa_restorer;
1443 pact = &act;
1444 } else {
1445 pact = NULL;
1446 }
1447 ret = get_errno(do_sigaction(arg1, pact, &oact));
1448 if (!is_error(ret) && old_oact) {
1449 old_oact->_sa_handler = oact._sa_handler;
1450 old_oact->sa_mask = oact.sa_mask.sig[0];
1451 old_oact->sa_flags = oact.sa_flags;
1452 old_oact->sa_restorer = oact.sa_restorer;
1453 }
1454 }
1455 break;
1456 case TARGET_NR_rt_sigaction:
1457 ret = get_errno(do_sigaction(arg1, (void *)arg2, (void *)arg3));
1458 break;
1459 case TARGET_NR_sgetmask:
1460 {
1461 sigset_t cur_set;
1462 target_ulong target_set;
1463 sigprocmask(0, NULL, &cur_set);
1464 host_to_target_old_sigset(&target_set, &cur_set);
1465 ret = target_set;
1466 }
1467 break;
1468 case TARGET_NR_ssetmask:
1469 {
1470 sigset_t set, oset, cur_set;
1471 target_ulong target_set = arg1;
1472 sigprocmask(0, NULL, &cur_set);
1473 target_to_host_old_sigset(&set, &target_set);
1474 sigorset(&set, &set, &cur_set);
1475 sigprocmask(SIG_SETMASK, &set, &oset);
1476 host_to_target_old_sigset(&target_set, &oset);
1477 ret = target_set;
1478 }
1479 break;
1480 case TARGET_NR_sigprocmask:
1481 {
1482 int how = arg1;
1483 sigset_t set, oldset, *set_ptr;
1484 target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
1485
1486 if (pset) {
1487 switch(how) {
1488 case TARGET_SIG_BLOCK:
1489 how = SIG_BLOCK;
1490 break;
1491 case TARGET_SIG_UNBLOCK:
1492 how = SIG_UNBLOCK;
1493 break;
1494 case TARGET_SIG_SETMASK:
1495 how = SIG_SETMASK;
1496 break;
1497 default:
1498 ret = -EINVAL;
1499 goto fail;
1500 }
1501 target_to_host_old_sigset(&set, pset);
1502 set_ptr = &set;
1503 } else {
1504 how = 0;
1505 set_ptr = NULL;
1506 }
1507 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
1508 if (!is_error(ret) && poldset) {
1509 host_to_target_old_sigset(poldset, &oldset);
1510 }
1511 }
1512 break;
1513 case TARGET_NR_rt_sigprocmask:
1514 {
1515 int how = arg1;
1516 sigset_t set, oldset, *set_ptr;
1517 target_sigset_t *pset = (void *)arg2;
1518 target_sigset_t *poldset = (void *)arg3;
1519
1520 if (pset) {
1521 switch(how) {
1522 case TARGET_SIG_BLOCK:
1523 how = SIG_BLOCK;
1524 break;
1525 case TARGET_SIG_UNBLOCK:
1526 how = SIG_UNBLOCK;
1527 break;
1528 case TARGET_SIG_SETMASK:
1529 how = SIG_SETMASK;
1530 break;
1531 default:
1532 ret = -EINVAL;
1533 goto fail;
1534 }
1535 target_to_host_sigset(&set, pset);
1536 set_ptr = &set;
1537 } else {
1538 how = 0;
1539 set_ptr = NULL;
1540 }
1541 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
1542 if (!is_error(ret) && poldset) {
1543 host_to_target_sigset(poldset, &oldset);
1544 }
1545 }
1546 break;
1547 case TARGET_NR_sigpending:
1548 {
1549 sigset_t set;
1550 ret = get_errno(sigpending(&set));
1551 if (!is_error(ret)) {
1552 host_to_target_old_sigset((target_ulong *)arg1, &set);
1553 }
1554 }
1555 break;
1556 case TARGET_NR_rt_sigpending:
1557 {
1558 sigset_t set;
1559 ret = get_errno(sigpending(&set));
1560 if (!is_error(ret)) {
1561 host_to_target_sigset((target_sigset_t *)arg1, &set);
1562 }
1563 }
1564 break;
1565 case TARGET_NR_sigsuspend:
1566 {
1567 sigset_t set;
1568 target_to_host_old_sigset(&set, (target_ulong *)arg1);
1569 ret = get_errno(sigsuspend(&set));
1570 }
1571 break;
1572 case TARGET_NR_rt_sigsuspend:
1573 {
1574 sigset_t set;
1575 target_to_host_sigset(&set, (target_sigset_t *)arg1);
1576 ret = get_errno(sigsuspend(&set));
1577 }
1578 break;
1579 case TARGET_NR_rt_sigtimedwait:
1580 {
1581 target_sigset_t *target_set = (void *)arg1;
1582 target_siginfo_t *target_uinfo = (void *)arg2;
1583 struct target_timespec *target_uts = (void *)arg3;
1584 sigset_t set;
1585 struct timespec uts, *puts;
1586 siginfo_t uinfo;
1587
1588 target_to_host_sigset(&set, target_set);
1589 if (target_uts) {
1590 puts = &uts;
1591 puts->tv_sec = tswapl(target_uts->tv_sec);
1592 puts->tv_nsec = tswapl(target_uts->tv_nsec);
1593 } else {
1594 puts = NULL;
1595 }
1596 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
1597 if (!is_error(ret) && target_uinfo) {
1598 host_to_target_siginfo(target_uinfo, &uinfo);
1599 }
1600 }
1601 break;
1602 case TARGET_NR_rt_sigqueueinfo:
1603 {
1604 siginfo_t uinfo;
1605 target_to_host_siginfo(&uinfo, (target_siginfo_t *)arg3);
1606 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
1607 }
1608 break;
1609 case TARGET_NR_sigreturn:
1610 /* NOTE: ret is eax, so not transcoding must be done */
1611 ret = do_sigreturn(cpu_env);
1612 break;
1613 case TARGET_NR_rt_sigreturn:
1614 /* NOTE: ret is eax, so not transcoding must be done */
1615 ret = do_rt_sigreturn(cpu_env);
1616 break;
1617 case TARGET_NR_setreuid:
1618 ret = get_errno(setreuid(arg1, arg2));
1619 break;
1620 case TARGET_NR_setregid:
1621 ret = get_errno(setregid(arg1, arg2));
1622 break;
1623 case TARGET_NR_sethostname:
1624 ret = get_errno(sethostname((const char *)arg1, arg2));
1625 break;
1626 case TARGET_NR_setrlimit:
1627 {
1628 /* XXX: convert resource ? */
1629 int resource = arg1;
1630 struct target_rlimit *target_rlim = (void *)arg2;
1631 struct rlimit rlim;
1632 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
1633 rlim.rlim_max = tswapl(target_rlim->rlim_max);
1634 ret = get_errno(setrlimit(resource, &rlim));
1635 }
1636 break;
1637 case TARGET_NR_getrlimit:
1638 {
1639 /* XXX: convert resource ? */
1640 int resource = arg1;
1641 struct target_rlimit *target_rlim = (void *)arg2;
1642 struct rlimit rlim;
1643
1644 ret = get_errno(getrlimit(resource, &rlim));
1645 if (!is_error(ret)) {
1646 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
1647 target_rlim->rlim_max = tswapl(rlim.rlim_max);
1648 }
1649 }
1650 break;
1651 case TARGET_NR_getrusage:
1652 goto unimplemented;
1653 case TARGET_NR_gettimeofday:
1654 {
1655 struct target_timeval *target_tv = (void *)arg1;
1656 struct timeval tv;
1657 ret = get_errno(gettimeofday(&tv, NULL));
1658 if (!is_error(ret)) {
1659 host_to_target_timeval(target_tv, &tv);
1660 }
1661 }
1662 break;
1663 case TARGET_NR_settimeofday:
1664 {
1665 struct target_timeval *target_tv = (void *)arg1;
1666 struct timeval tv;
1667 target_to_host_timeval(&tv, target_tv);
1668 ret = get_errno(settimeofday(&tv, NULL));
1669 }
1670 break;
1671 case TARGET_NR_getgroups:
1672 goto unimplemented;
1673 case TARGET_NR_setgroups:
1674 goto unimplemented;
1675 case TARGET_NR_select:
1676 goto unimplemented;
1677 case TARGET_NR_symlink:
1678 ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
1679 break;
1680 case TARGET_NR_oldlstat:
1681 goto unimplemented;
1682 case TARGET_NR_readlink:
1683 ret = get_errno(readlink((const char *)arg1, (char *)arg2, arg3));
1684 break;
1685 case TARGET_NR_uselib:
1686 goto unimplemented;
1687 case TARGET_NR_swapon:
1688 ret = get_errno(swapon((const char *)arg1, arg2));
1689 break;
1690 case TARGET_NR_reboot:
1691 goto unimplemented;
1692 case TARGET_NR_readdir:
1693 goto unimplemented;
1694 #ifdef TARGET_I386
1695 case TARGET_NR_mmap:
1696 {
1697 uint32_t v1, v2, v3, v4, v5, v6, *vptr;
1698 vptr = (uint32_t *)arg1;
1699 v1 = tswap32(vptr[0]);
1700 v2 = tswap32(vptr[1]);
1701 v3 = tswap32(vptr[2]);
1702 v4 = tswap32(vptr[3]);
1703 v5 = tswap32(vptr[4]);
1704 v6 = tswap32(vptr[5]);
1705 ret = get_errno((long)mmap((void *)v1, v2, v3, v4, v5, v6));
1706 }
1707 break;
1708 #endif
1709 #ifdef TARGET_I386
1710 case TARGET_NR_mmap2:
1711 #else
1712 case TARGET_NR_mmap:
1713 #endif
1714 ret = get_errno((long)mmap((void *)arg1, arg2, arg3, arg4, arg5, arg6));
1715 break;
1716 case TARGET_NR_munmap:
1717 ret = get_errno(munmap((void *)arg1, arg2));
1718 break;
1719 case TARGET_NR_mprotect:
1720 ret = get_errno(mprotect((void *)arg1, arg2, arg3));
1721 break;
1722 case TARGET_NR_mremap:
1723 ret = get_errno((long)mremap((void *)arg1, arg2, arg3, arg4));
1724 break;
1725 case TARGET_NR_msync:
1726 ret = get_errno(msync((void *)arg1, arg2, arg3));
1727 break;
1728 case TARGET_NR_mlock:
1729 ret = get_errno(mlock((void *)arg1, arg2));
1730 break;
1731 case TARGET_NR_munlock:
1732 ret = get_errno(munlock((void *)arg1, arg2));
1733 break;
1734 case TARGET_NR_mlockall:
1735 ret = get_errno(mlockall(arg1));
1736 break;
1737 case TARGET_NR_munlockall:
1738 ret = get_errno(munlockall());
1739 break;
1740 case TARGET_NR_truncate:
1741 ret = get_errno(truncate((const char *)arg1, arg2));
1742 break;
1743 case TARGET_NR_ftruncate:
1744 ret = get_errno(ftruncate(arg1, arg2));
1745 break;
1746 case TARGET_NR_fchmod:
1747 ret = get_errno(fchmod(arg1, arg2));
1748 break;
1749 case TARGET_NR_fchown:
1750 ret = get_errno(fchown(arg1, arg2, arg3));
1751 break;
1752 case TARGET_NR_getpriority:
1753 ret = get_errno(getpriority(arg1, arg2));
1754 break;
1755 case TARGET_NR_setpriority:
1756 ret = get_errno(setpriority(arg1, arg2, arg3));
1757 break;
1758 case TARGET_NR_profil:
1759 goto unimplemented;
1760 case TARGET_NR_statfs:
1761 stfs = (void *)arg2;
1762 ret = get_errno(sys_statfs((const char *)arg1, stfs));
1763 convert_statfs:
1764 if (!is_error(ret)) {
1765 tswap32s(&stfs->f_type);
1766 tswap32s(&stfs->f_bsize);
1767 tswap32s(&stfs->f_blocks);
1768 tswap32s(&stfs->f_bfree);
1769 tswap32s(&stfs->f_bavail);
1770 tswap32s(&stfs->f_files);
1771 tswap32s(&stfs->f_ffree);
1772 tswap32s(&stfs->f_fsid.val[0]);
1773 tswap32s(&stfs->f_fsid.val[1]);
1774 tswap32s(&stfs->f_namelen);
1775 }
1776 break;
1777 case TARGET_NR_fstatfs:
1778 stfs = (void *)arg2;
1779 ret = get_errno(sys_fstatfs(arg1, stfs));
1780 goto convert_statfs;
1781 case TARGET_NR_ioperm:
1782 goto unimplemented;
1783 case TARGET_NR_socketcall:
1784 ret = do_socketcall(arg1, (int32_t *)arg2);
1785 break;
1786 case TARGET_NR_syslog:
1787 goto unimplemented;
1788 case TARGET_NR_setitimer:
1789 {
1790 struct target_itimerval *target_value = (void *)arg2;
1791 struct target_itimerval *target_ovalue = (void *)arg3;
1792 struct itimerval value, ovalue, *pvalue;
1793
1794 if (target_value) {
1795 pvalue = &value;
1796 target_to_host_timeval(&pvalue->it_interval,
1797 &target_value->it_interval);
1798 target_to_host_timeval(&pvalue->it_value,
1799 &target_value->it_value);
1800 } else {
1801 pvalue = NULL;
1802 }
1803 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
1804 if (!is_error(ret) && target_ovalue) {
1805 host_to_target_timeval(&target_ovalue->it_interval,
1806 &ovalue.it_interval);
1807 host_to_target_timeval(&target_ovalue->it_value,
1808 &ovalue.it_value);
1809 }
1810 }
1811 break;
1812 case TARGET_NR_getitimer:
1813 {
1814 struct target_itimerval *target_value = (void *)arg2;
1815 struct itimerval value;
1816
1817 ret = get_errno(getitimer(arg1, &value));
1818 if (!is_error(ret) && target_value) {
1819 host_to_target_timeval(&target_value->it_interval,
1820 &value.it_interval);
1821 host_to_target_timeval(&target_value->it_value,
1822 &value.it_value);
1823 }
1824 }
1825 break;
1826 case TARGET_NR_stat:
1827 ret = get_errno(stat((const char *)arg1, &st));
1828 goto do_stat;
1829 case TARGET_NR_lstat:
1830 ret = get_errno(lstat((const char *)arg1, &st));
1831 goto do_stat;
1832 case TARGET_NR_fstat:
1833 {
1834 ret = get_errno(fstat(arg1, &st));
1835 do_stat:
1836 if (!is_error(ret)) {
1837 struct target_stat *target_st = (void *)arg2;
1838 target_st->st_dev = tswap16(st.st_dev);
1839 target_st->st_ino = tswapl(st.st_ino);
1840 target_st->st_mode = tswap16(st.st_mode);
1841 target_st->st_nlink = tswap16(st.st_nlink);
1842 target_st->st_uid = tswap16(st.st_uid);
1843 target_st->st_gid = tswap16(st.st_gid);
1844 target_st->st_rdev = tswap16(st.st_rdev);
1845 target_st->st_size = tswapl(st.st_size);
1846 target_st->st_blksize = tswapl(st.st_blksize);
1847 target_st->st_blocks = tswapl(st.st_blocks);
1848 target_st->target_st_atime = tswapl(st.st_atime);
1849 target_st->target_st_mtime = tswapl(st.st_mtime);
1850 target_st->target_st_ctime = tswapl(st.st_ctime);
1851 }
1852 }
1853 break;
1854 case TARGET_NR_olduname:
1855 goto unimplemented;
1856 case TARGET_NR_iopl:
1857 goto unimplemented;
1858 case TARGET_NR_vhangup:
1859 ret = get_errno(vhangup());
1860 break;
1861 case TARGET_NR_idle:
1862 goto unimplemented;
1863 case TARGET_NR_wait4:
1864 {
1865 int status;
1866 target_long *status_ptr = (void *)arg2;
1867 struct rusage rusage, *rusage_ptr;
1868 struct target_rusage *target_rusage = (void *)arg4;
1869 if (target_rusage)
1870 rusage_ptr = &rusage;
1871 else
1872 rusage_ptr = NULL;
1873 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
1874 if (!is_error(ret)) {
1875 if (status_ptr)
1876 *status_ptr = tswap32(status);
1877 if (target_rusage) {
1878 target_rusage->ru_utime.tv_sec = tswapl(rusage.ru_utime.tv_sec);
1879 target_rusage->ru_utime.tv_usec = tswapl(rusage.ru_utime.tv_usec);
1880 target_rusage->ru_stime.tv_sec = tswapl(rusage.ru_stime.tv_sec);
1881 target_rusage->ru_stime.tv_usec = tswapl(rusage.ru_stime.tv_usec);
1882 target_rusage->ru_maxrss = tswapl(rusage.ru_maxrss);
1883 target_rusage->ru_ixrss = tswapl(rusage.ru_ixrss);
1884 target_rusage->ru_idrss = tswapl(rusage.ru_idrss);
1885 target_rusage->ru_isrss = tswapl(rusage.ru_isrss);
1886 target_rusage->ru_minflt = tswapl(rusage.ru_minflt);
1887 target_rusage->ru_majflt = tswapl(rusage.ru_majflt);
1888 target_rusage->ru_nswap = tswapl(rusage.ru_nswap);
1889 target_rusage->ru_inblock = tswapl(rusage.ru_inblock);
1890 target_rusage->ru_oublock = tswapl(rusage.ru_oublock);
1891 target_rusage->ru_msgsnd = tswapl(rusage.ru_msgsnd);
1892 target_rusage->ru_msgrcv = tswapl(rusage.ru_msgrcv);
1893 target_rusage->ru_nsignals = tswapl(rusage.ru_nsignals);
1894 target_rusage->ru_nvcsw = tswapl(rusage.ru_nvcsw);
1895 target_rusage->ru_nivcsw = tswapl(rusage.ru_nivcsw);
1896 }
1897 }
1898 }
1899 break;
1900 case TARGET_NR_swapoff:
1901 ret = get_errno(swapoff((const char *)arg1));
1902 break;
1903 case TARGET_NR_sysinfo:
1904 goto unimplemented;
1905 case TARGET_NR_ipc:
1906 goto unimplemented;
1907 case TARGET_NR_fsync:
1908 ret = get_errno(fsync(arg1));
1909 break;
1910 case TARGET_NR_clone:
1911 ret = get_errno(do_fork(cpu_env, arg1, arg2));
1912 break;
1913 case TARGET_NR_setdomainname:
1914 ret = get_errno(setdomainname((const char *)arg1, arg2));
1915 break;
1916 case TARGET_NR_uname:
1917 /* no need to transcode because we use the linux syscall */
1918 ret = get_errno(sys_uname((struct new_utsname *)arg1));
1919 break;
1920 #ifdef TARGET_I386
1921 case TARGET_NR_modify_ldt:
1922 ret = get_errno(do_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
1923 break;
1924 case TARGET_NR_vm86old:
1925 goto unimplemented;
1926 case TARGET_NR_vm86:
1927 ret = do_vm86(cpu_env, arg1, (void *)arg2);
1928 break;
1929 #endif
1930 case TARGET_NR_adjtimex:
1931 goto unimplemented;
1932 case TARGET_NR_create_module:
1933 case TARGET_NR_init_module:
1934 case TARGET_NR_delete_module:
1935 case TARGET_NR_get_kernel_syms:
1936 goto unimplemented;
1937 case TARGET_NR_quotactl:
1938 goto unimplemented;
1939 case TARGET_NR_getpgid:
1940 ret = get_errno(getpgid(arg1));
1941 break;
1942 case TARGET_NR_fchdir:
1943 ret = get_errno(fchdir(arg1));
1944 break;
1945 case TARGET_NR_bdflush:
1946 goto unimplemented;
1947 case TARGET_NR_sysfs:
1948 goto unimplemented;
1949 case TARGET_NR_personality:
1950 ret = get_errno(personality(arg1));
1951 break;
1952 case TARGET_NR_afs_syscall:
1953 goto unimplemented;
1954 case TARGET_NR_setfsuid:
1955 ret = get_errno(setfsuid(arg1));
1956 break;
1957 case TARGET_NR_setfsgid:
1958 ret = get_errno(setfsgid(arg1));
1959 break;
1960 case TARGET_NR__llseek:
1961 {
1962 int64_t res;
1963 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
1964 *(int64_t *)arg4 = tswap64(res);
1965 }
1966 break;
1967 case TARGET_NR_getdents:
1968 #if TARGET_LONG_SIZE != 4
1969 #error not supported
1970 #endif
1971 {
1972 struct dirent *dirp = (void *)arg2;
1973 long count = arg3;
1974
1975 ret = get_errno(sys_getdents(arg1, dirp, count));
1976 if (!is_error(ret)) {
1977 struct dirent *de;
1978 int len = ret;
1979 int reclen;
1980 de = dirp;
1981 while (len > 0) {
1982 reclen = de->d_reclen;
1983 if (reclen > len)
1984 break;
1985 de->d_reclen = tswap16(reclen);
1986 tswapls(&de->d_ino);
1987 tswapls(&de->d_off);
1988 de = (struct dirent *)((char *)de + reclen);
1989 len -= reclen;
1990 }
1991 }
1992 }
1993 break;
1994 case TARGET_NR_getdents64:
1995 {
1996 struct dirent64 *dirp = (void *)arg2;
1997 long count = arg3;
1998 ret = get_errno(sys_getdents64(arg1, dirp, count));
1999 if (!is_error(ret)) {
2000 struct dirent64 *de;
2001 int len = ret;
2002 int reclen;
2003 de = dirp;
2004 while (len > 0) {
2005 reclen = de->d_reclen;
2006 if (reclen > len)
2007 break;
2008 de->d_reclen = tswap16(reclen);
2009 tswap64s(&de->d_ino);
2010 tswap64s(&de->d_off);
2011 de = (struct dirent64 *)((char *)de + reclen);
2012 len -= reclen;
2013 }
2014 }
2015 }
2016 break;
2017 case TARGET_NR__newselect:
2018 ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4,
2019 (void *)arg5);
2020 break;
2021 case TARGET_NR_poll:
2022 {
2023 struct target_pollfd *target_pfd = (void *)arg1;
2024 unsigned int nfds = arg2;
2025 int timeout = arg3;
2026 struct pollfd *pfd;
2027 unsigned int i;
2028
2029 pfd = alloca(sizeof(struct pollfd) * nfds);
2030 for(i = 0; i < nfds; i++) {
2031 pfd[i].fd = tswap32(target_pfd[i].fd);
2032 pfd[i].events = tswap16(target_pfd[i].events);
2033 }
2034 ret = get_errno(poll(pfd, nfds, timeout));
2035 if (!is_error(ret)) {
2036 for(i = 0; i < nfds; i++) {
2037 target_pfd[i].revents = tswap16(pfd[i].revents);
2038 }
2039 }
2040 }
2041 break;
2042 case TARGET_NR_flock:
2043 /* NOTE: the flock constant seems to be the same for every
2044 Linux platform */
2045 ret = get_errno(flock(arg1, arg2));
2046 break;
2047 case TARGET_NR_readv:
2048 {
2049 int count = arg3;
2050 int i;
2051 struct iovec *vec;
2052 struct target_iovec *target_vec = (void *)arg2;
2053
2054 vec = alloca(count * sizeof(struct iovec));
2055 for(i = 0;i < count; i++) {
2056 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2057 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2058 }
2059 ret = get_errno(readv(arg1, vec, count));
2060 }
2061 break;
2062 case TARGET_NR_writev:
2063 {
2064 int count = arg3;
2065 int i;
2066 struct iovec *vec;
2067 struct target_iovec *target_vec = (void *)arg2;
2068
2069 vec = alloca(count * sizeof(struct iovec));
2070 for(i = 0;i < count; i++) {
2071 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2072 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2073 }
2074 ret = get_errno(writev(arg1, vec, count));
2075 }
2076 break;
2077 case TARGET_NR_getsid:
2078 ret = get_errno(getsid(arg1));
2079 break;
2080 case TARGET_NR_fdatasync:
2081 ret = get_errno(fdatasync(arg1));
2082 break;
2083 case TARGET_NR__sysctl:
2084 goto unimplemented;
2085 case TARGET_NR_sched_setparam:
2086 {
2087 struct sched_param *target_schp = (void *)arg2;
2088 struct sched_param schp;
2089 schp.sched_priority = tswap32(target_schp->sched_priority);
2090 ret = get_errno(sched_setparam(arg1, &schp));
2091 }
2092 break;
2093 case TARGET_NR_sched_getparam:
2094 {
2095 struct sched_param *target_schp = (void *)arg2;
2096 struct sched_param schp;
2097 ret = get_errno(sched_getparam(arg1, &schp));
2098 if (!is_error(ret)) {
2099 target_schp->sched_priority = tswap32(schp.sched_priority);
2100 }
2101 }
2102 break;
2103 case TARGET_NR_sched_setscheduler:
2104 {
2105 struct sched_param *target_schp = (void *)arg3;
2106 struct sched_param schp;
2107 schp.sched_priority = tswap32(target_schp->sched_priority);
2108 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
2109 }
2110 break;
2111 case TARGET_NR_sched_getscheduler:
2112 ret = get_errno(sched_getscheduler(arg1));
2113 break;
2114 case TARGET_NR_sched_yield:
2115 ret = get_errno(sched_yield());
2116 break;
2117 case TARGET_NR_sched_get_priority_max:
2118 ret = get_errno(sched_get_priority_max(arg1));
2119 break;
2120 case TARGET_NR_sched_get_priority_min:
2121 ret = get_errno(sched_get_priority_min(arg1));
2122 break;
2123 case TARGET_NR_sched_rr_get_interval:
2124 {
2125 struct target_timespec *target_ts = (void *)arg2;
2126 struct timespec ts;
2127 ret = get_errno(sched_rr_get_interval(arg1, &ts));
2128 if (!is_error(ret)) {
2129 target_ts->tv_sec = tswapl(ts.tv_sec);
2130 target_ts->tv_nsec = tswapl(ts.tv_nsec);
2131 }
2132 }
2133 break;
2134 case TARGET_NR_nanosleep:
2135 {
2136 struct target_timespec *target_req = (void *)arg1;
2137 struct target_timespec *target_rem = (void *)arg2;
2138 struct timespec req, rem;
2139 req.tv_sec = tswapl(target_req->tv_sec);
2140 req.tv_nsec = tswapl(target_req->tv_nsec);
2141 ret = get_errno(nanosleep(&req, &rem));
2142 if (target_rem) {
2143 target_rem->tv_sec = tswapl(rem.tv_sec);
2144 target_rem->tv_nsec = tswapl(rem.tv_nsec);
2145 }
2146 }
2147 break;
2148 case TARGET_NR_setresuid:
2149 ret = get_errno(setresuid(low2highuid(arg1),
2150 low2highuid(arg2),
2151 low2highuid(arg3)));
2152 break;
2153 case TARGET_NR_getresuid:
2154 {
2155 int ruid, euid, suid;
2156 ret = get_errno(getresuid(&ruid, &euid, &suid));
2157 if (!is_error(ret)) {
2158 *(uint16_t *)arg1 = tswap16(high2lowuid(ruid));
2159 *(uint16_t *)arg2 = tswap16(high2lowuid(euid));
2160 *(uint16_t *)arg3 = tswap16(high2lowuid(suid));
2161 }
2162 }
2163 break;
2164 case TARGET_NR_setresgid:
2165 ret = get_errno(setresgid(low2highgid(arg1),
2166 low2highgid(arg2),
2167 low2highgid(arg3)));
2168 break;
2169 case TARGET_NR_getresgid:
2170 {
2171 int rgid, egid, sgid;
2172 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2173 if (!is_error(ret)) {
2174 *(uint16_t *)arg1 = high2lowgid(tswap16(rgid));
2175 *(uint16_t *)arg2 = high2lowgid(tswap16(egid));
2176 *(uint16_t *)arg3 = high2lowgid(tswap16(sgid));
2177 }
2178 }
2179 break;
2180 case TARGET_NR_query_module:
2181 goto unimplemented;
2182 case TARGET_NR_nfsservctl:
2183 goto unimplemented;
2184 case TARGET_NR_prctl:
2185 goto unimplemented;
2186 case TARGET_NR_pread:
2187 goto unimplemented;
2188 case TARGET_NR_pwrite:
2189 goto unimplemented;
2190 case TARGET_NR_chown:
2191 ret = get_errno(chown((const char *)arg1, arg2, arg3));
2192 break;
2193 case TARGET_NR_getcwd:
2194 ret = get_errno(sys_getcwd1((char *)arg1, arg2));
2195 break;
2196 case TARGET_NR_capget:
2197 goto unimplemented;
2198 case TARGET_NR_capset:
2199 goto unimplemented;
2200 case TARGET_NR_sigaltstack:
2201 goto unimplemented;
2202 case TARGET_NR_sendfile:
2203 goto unimplemented;
2204 case TARGET_NR_getpmsg:
2205 goto unimplemented;
2206 case TARGET_NR_putpmsg:
2207 goto unimplemented;
2208 case TARGET_NR_vfork:
2209 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
2210 break;
2211 case TARGET_NR_ugetrlimit:
2212 goto unimplemented;
2213 case TARGET_NR_truncate64:
2214 goto unimplemented;
2215 case TARGET_NR_ftruncate64:
2216 goto unimplemented;
2217 case TARGET_NR_stat64:
2218 ret = get_errno(stat((const char *)arg1, &st));
2219 goto do_stat64;
2220 case TARGET_NR_lstat64:
2221 ret = get_errno(lstat((const char *)arg1, &st));
2222 goto do_stat64;
2223 case TARGET_NR_fstat64:
2224 {
2225 ret = get_errno(fstat(arg1, &st));
2226 do_stat64:
2227 if (!is_error(ret)) {
2228 struct target_stat64 *target_st = (void *)arg2;
2229 target_st->st_dev = tswap16(st.st_dev);
2230 target_st->st_ino = tswapl(st.st_ino);
2231 target_st->st_mode = tswap16(st.st_mode);
2232 target_st->st_nlink = tswap16(st.st_nlink);
2233 target_st->st_uid = tswap16(st.st_uid);
2234 target_st->st_gid = tswap16(st.st_gid);
2235 target_st->st_rdev = tswap16(st.st_rdev);
2236 /* XXX: better use of kernel struct */
2237 target_st->st_size = tswapl(st.st_size);
2238 target_st->st_blksize = tswapl(st.st_blksize);
2239 target_st->st_blocks = tswapl(st.st_blocks);
2240 target_st->target_st_atime = tswapl(st.st_atime);
2241 target_st->target_st_mtime = tswapl(st.st_mtime);
2242 target_st->target_st_ctime = tswapl(st.st_ctime);
2243 }
2244 }
2245 break;
2246
2247 case TARGET_NR_lchown32:
2248 ret = get_errno(lchown((const char *)arg1, arg2, arg3));
2249 break;
2250 case TARGET_NR_getuid32:
2251 ret = get_errno(getuid());
2252 break;
2253 case TARGET_NR_getgid32:
2254 ret = get_errno(getgid());
2255 break;
2256 case TARGET_NR_geteuid32:
2257 ret = get_errno(geteuid());
2258 break;
2259 case TARGET_NR_getegid32:
2260 ret = get_errno(getegid());
2261 break;
2262 case TARGET_NR_setreuid32:
2263 ret = get_errno(setreuid(arg1, arg2));
2264 break;
2265 case TARGET_NR_setregid32:
2266 ret = get_errno(setregid(arg1, arg2));
2267 break;
2268 case TARGET_NR_getgroups32:
2269 goto unimplemented;
2270 case TARGET_NR_setgroups32:
2271 goto unimplemented;
2272 case TARGET_NR_fchown32:
2273 ret = get_errno(fchown(arg1, arg2, arg3));
2274 break;
2275 case TARGET_NR_setresuid32:
2276 ret = get_errno(setresuid(arg1, arg2, arg3));
2277 break;
2278 case TARGET_NR_getresuid32:
2279 {
2280 int ruid, euid, suid;
2281 ret = get_errno(getresuid(&ruid, &euid, &suid));
2282 if (!is_error(ret)) {
2283 *(uint32_t *)arg1 = tswap32(ruid);
2284 *(uint32_t *)arg2 = tswap32(euid);
2285 *(uint32_t *)arg3 = tswap32(suid);
2286 }
2287 }
2288 break;
2289 case TARGET_NR_setresgid32:
2290 ret = get_errno(setresgid(arg1, arg2, arg3));
2291 break;
2292 case TARGET_NR_getresgid32:
2293 {
2294 int rgid, egid, sgid;
2295 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2296 if (!is_error(ret)) {
2297 *(uint32_t *)arg1 = tswap32(rgid);
2298 *(uint32_t *)arg2 = tswap32(egid);
2299 *(uint32_t *)arg3 = tswap32(sgid);
2300 }
2301 }
2302 break;
2303 case TARGET_NR_chown32:
2304 ret = get_errno(chown((const char *)arg1, arg2, arg3));
2305 break;
2306 case TARGET_NR_setuid32:
2307 ret = get_errno(setuid(arg1));
2308 break;
2309 case TARGET_NR_setgid32:
2310 ret = get_errno(setgid(arg1));
2311 break;
2312 case TARGET_NR_setfsuid32:
2313 ret = get_errno(setfsuid(arg1));
2314 break;
2315 case TARGET_NR_setfsgid32:
2316 ret = get_errno(setfsgid(arg1));
2317 break;
2318 case TARGET_NR_pivot_root:
2319 goto unimplemented;
2320 case TARGET_NR_mincore:
2321 goto unimplemented;
2322 case TARGET_NR_madvise:
2323 goto unimplemented;
2324 #if TARGET_LONG_BITS == 32
2325 case TARGET_NR_fcntl64:
2326 switch(arg2) {
2327 case F_GETLK64:
2328 case F_SETLK64:
2329 case F_SETLKW64:
2330 goto unimplemented;
2331 default:
2332 ret = get_errno(fcntl(arg1, arg2, arg3));
2333 break;
2334 }
2335 break;
2336 #endif
2337 case TARGET_NR_security:
2338 goto unimplemented;
2339 case TARGET_NR_gettid:
2340 ret = get_errno(gettid());
2341 break;
2342 case TARGET_NR_readahead:
2343 goto unimplemented;
2344 case TARGET_NR_setxattr:
2345 case TARGET_NR_lsetxattr:
2346 case TARGET_NR_fsetxattr:
2347 case TARGET_NR_getxattr:
2348 case TARGET_NR_lgetxattr:
2349 case TARGET_NR_fgetxattr:
2350 case TARGET_NR_listxattr:
2351 case TARGET_NR_llistxattr:
2352 case TARGET_NR_flistxattr:
2353 case TARGET_NR_removexattr:
2354 case TARGET_NR_lremovexattr:
2355 case TARGET_NR_fremovexattr:
2356 goto unimplemented_nowarn;
2357 case TARGET_NR_set_thread_area:
2358 case TARGET_NR_get_thread_area:
2359 goto unimplemented_nowarn;
2360 default:
2361 unimplemented:
2362 gemu_log("qemu: Unsupported syscall: %d\n", num);
2363 unimplemented_nowarn:
2364 ret = -ENOSYS;
2365 break;
2366 }
2367 fail:
2368 return ret;
2369 }
2370