da54a0a186819ee6ae5bd085c98c00402680cf9b
[qemu] / 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 <sys/types.h>
30 #include <sys/wait.h>
31 #include <sys/time.h>
32 #include <sys/stat.h>
33 #include <sys/mount.h>
34 #include <sys/resource.h>
35 #include <sys/mman.h>
36 #include <sys/swap.h>
37 #include <signal.h>
38 #include <sched.h>
39 #include <sys/socket.h>
40 #include <sys/uio.h>
41 //#include <sys/user.h>
42
43 #define termios host_termios
44 #define winsize host_winsize
45 #define termio host_termio
46 #define sgttyb host_sgttyb /* same as target */
47 #define tchars host_tchars /* same as target */
48 #define ltchars host_ltchars /* same as target */
49
50 #include <linux/termios.h>
51 #include <linux/unistd.h>
52 #include <linux/utsname.h>
53 #include <linux/cdrom.h>
54 #include <linux/hdreg.h>
55 #include <linux/soundcard.h>
56 #include <linux/dirent.h>
57
58 #include "gemu.h"
59
60 //#define DEBUG
61
62 #ifndef PAGE_SIZE
63 #define PAGE_SIZE 4096
64 #define PAGE_MASK ~(PAGE_SIZE - 1)
65 #endif
66
67 //#include <linux/msdos_fs.h>
68 #define VFAT_IOCTL_READDIR_BOTH         _IOR('r', 1, struct dirent [2])
69 #define VFAT_IOCTL_READDIR_SHORT        _IOR('r', 2, struct dirent [2])
70
71 #include "syscall_defs.h"
72
73 #ifdef TARGET_I386
74 #include "cpu-i386.h"
75 #include "syscall-i386.h"
76 #endif
77
78 #define __NR_sys_uname __NR_uname
79 #define __NR_sys_getcwd1 __NR_getcwd
80 #define __NR_sys_statfs __NR_statfs
81 #define __NR_sys_fstatfs __NR_fstatfs
82 #define __NR_sys_getdents __NR_getdents
83 #define __NR_sys_getdents64 __NR_getdents64
84
85 #ifdef __NR_gettid
86 _syscall0(int, gettid)
87 #else
88 static int gettid(void) {
89     return -ENOSYS;
90 }
91 #endif
92 _syscall1(int,sys_uname,struct new_utsname *,buf)
93 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
94 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
95 _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
96 _syscall5(int, _llseek,  uint,  fd, ulong, hi, ulong, lo,
97           loff_t *, res, uint, wh);
98 _syscall2(int,sys_statfs,const char *,path,struct kernel_statfs *,buf)
99 _syscall2(int,sys_fstatfs,int,fd,struct kernel_statfs *,buf)
100
101 static inline long get_errno(long ret)
102 {
103     if (ret == -1)
104         return -errno;
105     else
106         return ret;
107 }
108
109 static inline int is_error(long ret)
110 {
111     return (unsigned long)ret >= (unsigned long)(-4096);
112 }
113
114 static char *target_brk;
115 static char *target_original_brk;
116
117 void target_set_brk(char *new_brk)
118 {
119     target_brk = new_brk;
120     target_original_brk = new_brk;
121 }
122
123 static long do_brk(char *new_brk)
124 {
125     char *brk_page;
126     long mapped_addr;
127     int new_alloc_size;
128
129     if (!new_brk)
130         return (long)target_brk;
131     if (new_brk < target_original_brk)
132         return -ENOMEM;
133     
134     brk_page = (char *)(((unsigned long)target_brk + PAGE_SIZE - 1) & PAGE_MASK);
135
136     /* If the new brk is less than this, set it and we're done... */
137     if (new_brk < brk_page) {
138         target_brk = new_brk;
139         return (long)target_brk;
140     }
141
142     /* We need to allocate more memory after the brk... */
143     new_alloc_size = ((new_brk - brk_page + 1)+(PAGE_SIZE-1)) & PAGE_MASK;
144     mapped_addr = get_errno((long)mmap((caddr_t)brk_page, new_alloc_size, 
145                                        PROT_READ|PROT_WRITE,
146                                        MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
147     
148     if (is_error(mapped_addr)) {
149         return mapped_addr;
150     } else {
151         target_brk = new_brk;
152         return (long)target_brk;
153     }
154 }
155
156 static inline fd_set *target_to_host_fds(fd_set *fds, 
157                                          target_long *target_fds, int n)
158 {
159 #if !defined(BSWP_NEEDED) && !defined(WORD_BIGENDIAN)
160     return (fd_set *)target_fds;
161 #else
162     int i, b;
163     if (target_fds) {
164         FD_ZERO(fds);
165         for(i = 0;i < n; i++) {
166             b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
167                  (i & (TARGET_LONG_BITS - 1))) & 1;
168             if (b)
169                 FD_SET(i, fds);
170         }
171         return fds;
172     } else {
173         return NULL;
174     }
175 #endif
176 }
177
178 static inline void host_to_target_fds(target_long *target_fds, 
179                                       fd_set *fds, int n)
180 {
181 #if !defined(BSWP_NEEDED) && !defined(WORD_BIGENDIAN)
182     /* nothing to do */
183 #else
184     int i, nw, j, k;
185     target_long v;
186
187     if (target_fds) {
188         nw = n / TARGET_LONG_BITS;
189         k = 0;
190         for(i = 0;i < nw; i++) {
191             v = 0;
192             for(j = 0; j < TARGET_LONG_BITS; j++) {
193                 v |= ((FD_ISSET(k, fds) != 0) << j);
194                 k++;
195             }
196             target_fds[i] = tswapl(v);
197         }
198     }
199 #endif
200 }
201
202 /* XXX: incorrect for some archs */
203 static void host_to_target_old_sigset(target_ulong *old_sigset, 
204                                       const sigset_t *sigset)
205 {
206     *old_sigset = tswap32(*(unsigned long *)sigset & 0xffffffff);
207 }
208
209 static void target_to_host_old_sigset(sigset_t *sigset, 
210                                       const target_ulong *old_sigset)
211 {
212     sigemptyset(sigset);
213     *(unsigned long *)sigset = tswapl(*old_sigset);
214 }
215
216
217 static long do_select(long n, 
218                       target_long *target_rfds, target_long *target_wfds, 
219                       target_long *target_efds, struct target_timeval *target_tv)
220 {
221     fd_set rfds, wfds, efds;
222     fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
223     struct timeval tv, *tv_ptr;
224     long ret;
225
226     rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
227     wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
228     efds_ptr = target_to_host_fds(&efds, target_efds, n);
229             
230     if (target_tv) {
231         tv.tv_sec = tswapl(target_tv->tv_sec);
232         tv.tv_usec = tswapl(target_tv->tv_usec);
233         tv_ptr = &tv;
234     } else {
235         tv_ptr = NULL;
236     }
237     ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
238     if (!is_error(ret)) {
239         host_to_target_fds(target_rfds, rfds_ptr, n);
240         host_to_target_fds(target_wfds, wfds_ptr, n);
241         host_to_target_fds(target_efds, efds_ptr, n);
242
243         if (target_tv) {
244             target_tv->tv_sec = tswapl(tv.tv_sec);
245             target_tv->tv_usec = tswapl(tv.tv_usec);
246         }
247     }
248     return ret;
249 }
250
251 static long do_socketcall(int num, long *vptr)
252 {
253     long ret;
254
255     switch(num) {
256     case SOCKOP_socket:
257         ret = get_errno(socket(vptr[0], vptr[1], vptr[2]));
258         break;
259     case SOCKOP_bind:
260         ret = get_errno(bind(vptr[0], (struct sockaddr *)vptr[1], vptr[2]));
261         break;
262     case SOCKOP_connect:
263         ret = get_errno(connect(vptr[0], (struct sockaddr *)vptr[1], vptr[2]));
264         break;
265     case SOCKOP_listen:
266         ret = get_errno(listen(vptr[0], vptr[1]));
267         break;
268     case SOCKOP_accept:
269         {
270             socklen_t size;
271             size = tswap32(*(int32_t *)vptr[2]);
272             ret = get_errno(accept(vptr[0], (struct sockaddr *)vptr[1], &size));
273             if (!is_error(ret)) 
274                 *(int32_t *)vptr[2] = size;
275         }
276         break;
277     case SOCKOP_getsockname:
278         {
279             socklen_t size;
280             size = tswap32(*(int32_t *)vptr[2]);
281             ret = get_errno(getsockname(vptr[0], (struct sockaddr *)vptr[1], &size));
282             if (!is_error(ret)) 
283                 *(int32_t *)vptr[2] = size;
284         }
285         break;
286     case SOCKOP_getpeername:
287         {
288             socklen_t size;
289             size = tswap32(*(int32_t *)vptr[2]);
290             ret = get_errno(getpeername(vptr[0], (struct sockaddr *)vptr[1], &size));
291             if (!is_error(ret)) 
292                 *(int32_t *)vptr[2] = size;
293         }
294         break;
295     case SOCKOP_socketpair:
296         {
297             int tab[2];
298             int32_t *target_tab = (int32_t *)vptr[3];
299             ret = get_errno(socketpair(vptr[0], vptr[1], vptr[2], tab));
300             if (!is_error(ret)) {
301                 target_tab[0] = tswap32(tab[0]);
302                 target_tab[1] = tswap32(tab[1]);
303             }
304         }
305         break;
306     case SOCKOP_send:
307         ret = get_errno(send(vptr[0], (void *)vptr[1], vptr[2], vptr[3]));
308         break;
309     case SOCKOP_recv:
310         ret = get_errno(recv(vptr[0], (void *)vptr[1], vptr[2], vptr[3]));
311         break;
312     case SOCKOP_sendto:
313         ret = get_errno(sendto(vptr[0], (void *)vptr[1], vptr[2], vptr[3], 
314                                (struct sockaddr *)vptr[4], vptr[5]));
315         break;
316     case SOCKOP_recvfrom:
317         {
318             socklen_t size;
319             size = tswap32(*(int32_t *)vptr[5]);
320             ret = get_errno(recvfrom(vptr[0], (void *)vptr[1], vptr[2], 
321                                      vptr[3], (struct sockaddr *)vptr[4], &size));
322             if (!is_error(ret)) 
323                 *(int32_t *)vptr[5] = size;
324         }
325         break;
326     case SOCKOP_shutdown:
327         ret = get_errno(shutdown(vptr[0], vptr[1]));
328         break;
329     case SOCKOP_sendmsg:
330     case SOCKOP_recvmsg:
331         {
332             int fd;
333             struct target_msghdr *msgp;
334             struct msghdr msg;
335             int flags, count, i;
336             struct iovec *vec;
337             struct target_iovec *target_vec;
338
339             msgp = (void *)vptr[1];
340             msg.msg_name = (void *)tswapl(msgp->msg_name);
341             msg.msg_namelen = tswapl(msgp->msg_namelen);
342             msg.msg_control = (void *)tswapl(msgp->msg_control);
343             msg.msg_controllen = tswapl(msgp->msg_controllen);
344             msg.msg_flags = tswap32(msgp->msg_flags);
345
346             count = tswapl(msgp->msg_iovlen);
347             vec = alloca(count * sizeof(struct iovec));
348             target_vec = (void *)tswapl(msgp->msg_iov);
349             for(i = 0;i < count; i++) {
350                 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
351                 vec[i].iov_len = tswapl(target_vec[i].iov_len);
352             }
353             msg.msg_iovlen = count;
354             msg.msg_iov = vec;
355
356             fd = vptr[0];
357             flags = vptr[2];
358             if (num == SOCKOP_sendmsg)
359                 ret = sendmsg(fd, &msg, flags);
360             else
361                 ret = recvmsg(fd, &msg, flags);
362             ret = get_errno(ret);
363         }
364         break;
365     case SOCKOP_setsockopt:
366     case SOCKOP_getsockopt:
367     default:
368         gemu_log("Unsupported socketcall: %d\n", num);
369         ret = -ENOSYS;
370         break;
371     }
372     return ret;
373 }
374
375 /* kernel structure types definitions */
376 #define IFNAMSIZ        16
377
378 #define STRUCT(name, list...) STRUCT_ ## name,
379 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
380 enum {
381 #include "syscall_types.h"
382 };
383 #undef STRUCT
384 #undef STRUCT_SPECIAL
385
386 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
387 #define STRUCT_SPECIAL(name)
388 #include "syscall_types.h"
389 #undef STRUCT
390 #undef STRUCT_SPECIAL
391
392 typedef struct IOCTLEntry {
393     int target_cmd;
394     int host_cmd;
395     const char *name;
396     int access;
397     const argtype arg_type[5];
398 } IOCTLEntry;
399
400 #define IOC_R 0x0001
401 #define IOC_W 0x0002
402 #define IOC_RW (IOC_R | IOC_W)
403
404 #define MAX_STRUCT_SIZE 4096
405
406 const IOCTLEntry ioctl_entries[] = {
407 #define IOCTL(cmd, access, types...) \
408     { TARGET_ ## cmd, cmd, #cmd, access, { types } },
409 #include "ioctls.h"
410     { 0, 0, },
411 };
412
413 static long do_ioctl(long fd, long cmd, long arg)
414 {
415     const IOCTLEntry *ie;
416     const argtype *arg_type;
417     long ret;
418     uint8_t buf_temp[MAX_STRUCT_SIZE];
419
420     ie = ioctl_entries;
421     for(;;) {
422         if (ie->target_cmd == 0) {
423             gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
424             return -ENOSYS;
425         }
426         if (ie->target_cmd == cmd)
427             break;
428         ie++;
429     }
430     arg_type = ie->arg_type;
431 #ifdef DEBUG
432     gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
433 #endif
434     switch(arg_type[0]) {
435     case TYPE_NULL:
436         /* no argument */
437         ret = get_errno(ioctl(fd, ie->host_cmd));
438         break;
439     case TYPE_PTRVOID:
440     case TYPE_INT:
441         /* int argment */
442         ret = get_errno(ioctl(fd, ie->host_cmd, arg));
443         break;
444     case TYPE_PTR:
445         arg_type++;
446         switch(ie->access) {
447         case IOC_R:
448             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
449             if (!is_error(ret)) {
450                 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
451             }
452             break;
453         case IOC_W:
454             thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
455             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
456             break;
457         default:
458         case IOC_RW:
459             thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
460             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
461             if (!is_error(ret)) {
462                 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
463             }
464             break;
465         }
466         break;
467     default:
468         gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
469         ret = -ENOSYS;
470         break;
471     }
472     return ret;
473 }
474
475 bitmask_transtbl iflag_tbl[] = {
476         { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
477         { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
478         { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
479         { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
480         { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
481         { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
482         { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
483         { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
484         { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
485         { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
486         { TARGET_IXON, TARGET_IXON, IXON, IXON },
487         { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
488         { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
489         { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
490         { 0, 0, 0, 0 }
491 };
492
493 bitmask_transtbl oflag_tbl[] = {
494         { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
495         { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
496         { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
497         { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
498         { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
499         { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
500         { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
501         { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
502         { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
503         { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
504         { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
505         { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
506         { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
507         { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
508         { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
509         { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
510         { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
511         { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
512         { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
513         { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
514         { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
515         { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
516         { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
517         { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
518         { 0, 0, 0, 0 }
519 };
520
521 bitmask_transtbl cflag_tbl[] = {
522         { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
523         { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
524         { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
525         { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
526         { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
527         { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
528         { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
529         { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
530         { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
531         { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
532         { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
533         { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
534         { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
535         { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
536         { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
537         { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
538         { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
539         { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
540         { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
541         { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
542         { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
543         { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
544         { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
545         { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
546         { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
547         { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
548         { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
549         { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
550         { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
551         { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
552         { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
553         { 0, 0, 0, 0 }
554 };
555
556 bitmask_transtbl lflag_tbl[] = {
557         { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
558         { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
559         { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
560         { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
561         { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
562         { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
563         { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
564         { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
565         { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
566         { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
567         { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
568         { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
569         { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
570         { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
571         { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
572         { 0, 0, 0, 0 }
573 };
574
575 static void target_to_host_termios (void *dst, const void *src)
576 {
577     struct host_termios *host = dst;
578     const struct target_termios *target = src;
579     
580     host->c_iflag = 
581         target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
582     host->c_oflag = 
583         target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
584     host->c_cflag = 
585         target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
586     host->c_lflag = 
587         target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
588     host->c_line = target->c_line;
589     
590     host->c_cc[VINTR] = target->c_cc[TARGET_VINTR]; 
591     host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT]; 
592     host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];       
593     host->c_cc[VKILL] = target->c_cc[TARGET_VKILL]; 
594     host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];   
595     host->c_cc[VTIME] = target->c_cc[TARGET_VTIME]; 
596     host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];   
597     host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC]; 
598     host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];       
599     host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP]; 
600     host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP]; 
601     host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];   
602     host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];   
603     host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];   
604     host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];     
605     host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];       
606     host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2]; 
607 }
608   
609 static void host_to_target_termios (void *dst, const void *src)
610 {
611     struct target_termios *target = dst;
612     const struct host_termios *host = src;
613
614     target->c_iflag = 
615         tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
616     target->c_oflag = 
617         tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
618     target->c_cflag = 
619         tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
620     target->c_lflag = 
621         tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
622     target->c_line = host->c_line;
623   
624     target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
625     target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
626     target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
627     target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
628     target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
629     target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
630     target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
631     target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
632     target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
633     target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
634     target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
635     target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
636     target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
637     target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
638     target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
639     target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
640     target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
641 }
642
643 StructEntry struct_termios_def = {
644     .convert = { host_to_target_termios, target_to_host_termios },
645     .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
646     .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
647 };
648
649 #ifdef TARGET_I386
650
651 /* NOTE: there is really one LDT for all the threads */
652 uint8_t *ldt_table;
653
654 static int read_ldt(void *ptr, unsigned long bytecount)
655 {
656     int size;
657
658     if (!ldt_table)
659         return 0;
660     size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
661     if (size > bytecount)
662         size = bytecount;
663     memcpy(ptr, ldt_table, size);
664     return size;
665 }
666
667 /* XXX: add locking support */
668 static int write_ldt(CPUX86State *env, 
669                      void *ptr, unsigned long bytecount, int oldmode)
670 {
671     struct target_modify_ldt_ldt_s ldt_info;
672     int seg_32bit, contents, read_exec_only, limit_in_pages;
673     int seg_not_present, useable;
674     uint32_t *lp, entry_1, entry_2;
675
676     if (bytecount != sizeof(ldt_info))
677         return -EINVAL;
678     memcpy(&ldt_info, ptr, sizeof(ldt_info));
679     tswap32s(&ldt_info.entry_number);
680     tswapls((long *)&ldt_info.base_addr);
681     tswap32s(&ldt_info.limit);
682     tswap32s(&ldt_info.flags);
683     
684     if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
685         return -EINVAL;
686     seg_32bit = ldt_info.flags & 1;
687     contents = (ldt_info.flags >> 1) & 3;
688     read_exec_only = (ldt_info.flags >> 3) & 1;
689     limit_in_pages = (ldt_info.flags >> 4) & 1;
690     seg_not_present = (ldt_info.flags >> 5) & 1;
691     useable = (ldt_info.flags >> 6) & 1;
692
693     if (contents == 3) {
694         if (oldmode)
695             return -EINVAL;
696         if (seg_not_present == 0)
697             return -EINVAL;
698     }
699     /* allocate the LDT */
700     if (!ldt_table) {
701         ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
702         if (!ldt_table)
703             return -ENOMEM;
704         memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
705         env->ldt.base = ldt_table;
706         env->ldt.limit = 0xffff;
707     }
708
709     /* NOTE: same code as Linux kernel */
710     /* Allow LDTs to be cleared by the user. */
711     if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
712         if (oldmode ||
713             (contents == 0              &&
714              read_exec_only == 1        &&
715              seg_32bit == 0             &&
716              limit_in_pages == 0        &&
717              seg_not_present == 1       &&
718              useable == 0 )) {
719             entry_1 = 0;
720             entry_2 = 0;
721             goto install;
722         }
723     }
724     
725     entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
726         (ldt_info.limit & 0x0ffff);
727     entry_2 = (ldt_info.base_addr & 0xff000000) |
728         ((ldt_info.base_addr & 0x00ff0000) >> 16) |
729         (ldt_info.limit & 0xf0000) |
730         ((read_exec_only ^ 1) << 9) |
731         (contents << 10) |
732         ((seg_not_present ^ 1) << 15) |
733         (seg_32bit << 22) |
734         (limit_in_pages << 23) |
735         0x7000;
736     if (!oldmode)
737         entry_2 |= (useable << 20);
738     
739     /* Install the new entry ...  */
740 install:
741     lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
742     lp[0] = tswap32(entry_1);
743     lp[1] = tswap32(entry_2);
744     return 0;
745 }
746
747 /* specific and weird i386 syscalls */
748 int gemu_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
749 {
750     int ret = -ENOSYS;
751     
752     switch (func) {
753     case 0:
754         ret = read_ldt(ptr, bytecount);
755         break;
756     case 1:
757         ret = write_ldt(env, ptr, bytecount, 1);
758         break;
759     case 0x11:
760         ret = write_ldt(env, ptr, bytecount, 0);
761         break;
762     }
763     return ret;
764 }
765
766 /* this stack is the equivalent of the kernel stack associated with a
767    thread/process */
768 #define NEW_STACK_SIZE 8192
769
770 static int clone_func(void *arg)
771 {
772     CPUX86State *env = arg;
773     cpu_loop(env);
774     /* never exits */
775     return 0;
776 }
777
778 int do_fork(CPUX86State *env, unsigned int flags, unsigned long newsp)
779 {
780     int ret;
781     uint8_t *new_stack;
782     CPUX86State *new_env;
783     
784     if (flags & CLONE_VM) {
785         if (!newsp)
786             newsp = env->regs[R_ESP];
787         new_stack = malloc(NEW_STACK_SIZE);
788         
789         /* we create a new CPU instance. */
790         new_env = cpu_x86_init();
791         memcpy(new_env, env, sizeof(CPUX86State));
792         new_env->regs[R_ESP] = newsp;
793         new_env->regs[R_EAX] = 0;
794         ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
795     } else {
796         /* if no CLONE_VM, we consider it is a fork */
797         if ((flags & ~CSIGNAL) != 0)
798             return -EINVAL;
799         ret = fork();
800     }
801     return ret;
802 }
803
804 #endif
805
806
807 void syscall_init(void)
808 {
809 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def); 
810 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def); 
811 #include "syscall_types.h"
812 #undef STRUCT
813 #undef STRUCT_SPECIAL
814 }
815                                  
816 long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3, 
817                 long arg4, long arg5, long arg6)
818 {
819     long ret;
820     struct stat st;
821     struct kernel_statfs *stfs;
822     
823 #ifdef DEBUG
824     gemu_log("syscall %d\n", num);
825 #endif
826     switch(num) {
827     case TARGET_NR_exit:
828 #ifdef HAVE_GPROF
829         _mcleanup();
830 #endif
831         /* XXX: should free thread stack and CPU env */
832         _exit(arg1);
833         ret = 0; /* avoid warning */
834         break;
835     case TARGET_NR_read:
836         ret = get_errno(read(arg1, (void *)arg2, arg3));
837         break;
838     case TARGET_NR_write:
839         ret = get_errno(write(arg1, (void *)arg2, arg3));
840         break;
841     case TARGET_NR_open:
842         ret = get_errno(open((const char *)arg1, arg2, arg3));
843         break;
844     case TARGET_NR_close:
845         ret = get_errno(close(arg1));
846         break;
847     case TARGET_NR_brk:
848         ret = do_brk((char *)arg1);
849         break;
850     case TARGET_NR_fork:
851         ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
852         break;
853     case TARGET_NR_waitpid:
854         {
855             int *status = (int *)arg2;
856             ret = get_errno(waitpid(arg1, status, arg3));
857             if (!is_error(ret) && status)
858                 tswapls((long *)&status);
859         }
860         break;
861     case TARGET_NR_creat:
862         ret = get_errno(creat((const char *)arg1, arg2));
863         break;
864     case TARGET_NR_link:
865         ret = get_errno(link((const char *)arg1, (const char *)arg2));
866         break;
867     case TARGET_NR_unlink:
868         ret = get_errno(unlink((const char *)arg1));
869         break;
870     case TARGET_NR_execve:
871         ret = get_errno(execve((const char *)arg1, (void *)arg2, (void *)arg3));
872         break;
873     case TARGET_NR_chdir:
874         ret = get_errno(chdir((const char *)arg1));
875         break;
876     case TARGET_NR_time:
877         {
878             int *time_ptr = (int *)arg1;
879             ret = get_errno(time((time_t *)time_ptr));
880             if (!is_error(ret) && time_ptr)
881                 tswap32s(time_ptr);
882         }
883         break;
884     case TARGET_NR_mknod:
885         ret = get_errno(mknod((const char *)arg1, arg2, arg3));
886         break;
887     case TARGET_NR_chmod:
888         ret = get_errno(chmod((const char *)arg1, arg2));
889         break;
890     case TARGET_NR_lchown:
891         ret = get_errno(chown((const char *)arg1, arg2, arg3));
892         break;
893     case TARGET_NR_break:
894         goto unimplemented;
895     case TARGET_NR_oldstat:
896         goto unimplemented;
897     case TARGET_NR_lseek:
898         ret = get_errno(lseek(arg1, arg2, arg3));
899         break;
900     case TARGET_NR_getpid:
901         ret = get_errno(getpid());
902         break;
903     case TARGET_NR_mount:
904         /* need to look at the data field */
905         goto unimplemented;
906     case TARGET_NR_umount:
907         ret = get_errno(umount((const char *)arg1));
908         break;
909     case TARGET_NR_setuid:
910         ret = get_errno(setuid(arg1));
911         break;
912     case TARGET_NR_getuid:
913         ret = get_errno(getuid());
914         break;
915     case TARGET_NR_stime:
916         {
917             int *time_ptr = (int *)arg1;
918             if (time_ptr)
919                 tswap32s(time_ptr);
920             ret = get_errno(stime((time_t *)time_ptr));
921         }
922         break;
923     case TARGET_NR_ptrace:
924         goto unimplemented;
925     case TARGET_NR_alarm:
926         ret = alarm(arg1);
927         break;
928     case TARGET_NR_oldfstat:
929         goto unimplemented;
930     case TARGET_NR_pause:
931         ret = get_errno(pause());
932         break;
933     case TARGET_NR_utime:
934         goto unimplemented;
935     case TARGET_NR_stty:
936         goto unimplemented;
937     case TARGET_NR_gtty:
938         goto unimplemented;
939     case TARGET_NR_access:
940         ret = get_errno(access((const char *)arg1, arg2));
941         break;
942     case TARGET_NR_nice:
943         ret = get_errno(nice(arg1));
944         break;
945     case TARGET_NR_ftime:
946         goto unimplemented;
947     case TARGET_NR_sync:
948         sync();
949         ret = 0;
950         break;
951     case TARGET_NR_kill:
952         ret = get_errno(kill(arg1, arg2));
953         break;
954     case TARGET_NR_rename:
955         ret = get_errno(rename((const char *)arg1, (const char *)arg2));
956         break;
957     case TARGET_NR_mkdir:
958         ret = get_errno(mkdir((const char *)arg1, arg2));
959         break;
960     case TARGET_NR_rmdir:
961         ret = get_errno(rmdir((const char *)arg1));
962         break;
963     case TARGET_NR_dup:
964         ret = get_errno(dup(arg1));
965         break;
966     case TARGET_NR_pipe:
967         {
968             int *pipe_ptr = (int *)arg1;
969             ret = get_errno(pipe(pipe_ptr));
970             if (!is_error(ret)) {
971                 tswap32s(&pipe_ptr[0]);
972                 tswap32s(&pipe_ptr[1]);
973             }
974         }
975         break;
976     case TARGET_NR_times:
977         goto unimplemented;
978     case TARGET_NR_prof:
979         goto unimplemented;
980     case TARGET_NR_setgid:
981         ret = get_errno(setgid(arg1));
982         break;
983     case TARGET_NR_getgid:
984         ret = get_errno(getgid());
985         break;
986     case TARGET_NR_signal:
987         goto unimplemented;
988     case TARGET_NR_geteuid:
989         ret = get_errno(geteuid());
990         break;
991     case TARGET_NR_getegid:
992         ret = get_errno(getegid());
993         break;
994     case TARGET_NR_acct:
995         goto unimplemented;
996     case TARGET_NR_umount2:
997         ret = get_errno(umount2((const char *)arg1, arg2));
998         break;
999     case TARGET_NR_lock:
1000         goto unimplemented;
1001     case TARGET_NR_ioctl:
1002         ret = do_ioctl(arg1, arg2, arg3);
1003         break;
1004     case TARGET_NR_fcntl:
1005         switch(arg2) {
1006         case F_GETLK:
1007         case F_SETLK:
1008         case F_SETLKW:
1009             goto unimplemented;
1010         default:
1011             ret = get_errno(fcntl(arg1, arg2, arg3));
1012             break;
1013         }
1014         break;
1015     case TARGET_NR_mpx:
1016         goto unimplemented;
1017     case TARGET_NR_setpgid:
1018         ret = get_errno(setpgid(arg1, arg2));
1019         break;
1020     case TARGET_NR_ulimit:
1021         goto unimplemented;
1022     case TARGET_NR_oldolduname:
1023         goto unimplemented;
1024     case TARGET_NR_umask:
1025         ret = get_errno(umask(arg1));
1026         break;
1027     case TARGET_NR_chroot:
1028         ret = get_errno(chroot((const char *)arg1));
1029         break;
1030     case TARGET_NR_ustat:
1031         goto unimplemented;
1032     case TARGET_NR_dup2:
1033         ret = get_errno(dup2(arg1, arg2));
1034         break;
1035     case TARGET_NR_getppid:
1036         ret = get_errno(getppid());
1037         break;
1038     case TARGET_NR_getpgrp:
1039         ret = get_errno(getpgrp());
1040         break;
1041     case TARGET_NR_setsid:
1042         ret = get_errno(setsid());
1043         break;
1044     case TARGET_NR_sigaction:
1045 #if 1
1046         {
1047             ret = 0;
1048         }
1049         break;
1050 #else
1051         goto unimplemented;
1052 #endif
1053     case TARGET_NR_sgetmask:
1054         goto unimplemented;
1055     case TARGET_NR_ssetmask:
1056         goto unimplemented;
1057     case TARGET_NR_setreuid:
1058         ret = get_errno(setreuid(arg1, arg2));
1059         break;
1060     case TARGET_NR_setregid:
1061         ret = get_errno(setregid(arg1, arg2));
1062         break;
1063     case TARGET_NR_sigsuspend:
1064         goto unimplemented;
1065     case TARGET_NR_sigpending:
1066         goto unimplemented;
1067     case TARGET_NR_sethostname:
1068         ret = get_errno(sethostname((const char *)arg1, arg2));
1069         break;
1070     case TARGET_NR_setrlimit:
1071         goto unimplemented;
1072     case TARGET_NR_getrlimit:
1073         goto unimplemented;
1074     case TARGET_NR_getrusage:
1075         goto unimplemented;
1076     case TARGET_NR_gettimeofday:
1077         {
1078             struct target_timeval *target_tv = (void *)arg1;
1079             struct timeval tv;
1080             ret = get_errno(gettimeofday(&tv, NULL));
1081             if (!is_error(ret)) {
1082                 target_tv->tv_sec = tswapl(tv.tv_sec);
1083                 target_tv->tv_usec = tswapl(tv.tv_usec);
1084             }
1085         }
1086         break;
1087     case TARGET_NR_settimeofday:
1088         {
1089             struct target_timeval *target_tv = (void *)arg1;
1090             struct timeval tv;
1091             tv.tv_sec = tswapl(target_tv->tv_sec);
1092             tv.tv_usec = tswapl(target_tv->tv_usec);
1093             ret = get_errno(settimeofday(&tv, NULL));
1094         }
1095         break;
1096     case TARGET_NR_getgroups:
1097         goto unimplemented;
1098     case TARGET_NR_setgroups:
1099         goto unimplemented;
1100     case TARGET_NR_select:
1101         goto unimplemented;
1102     case TARGET_NR_symlink:
1103         ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
1104         break;
1105     case TARGET_NR_oldlstat:
1106         goto unimplemented;
1107     case TARGET_NR_readlink:
1108         ret = get_errno(readlink((const char *)arg1, (char *)arg2, arg3));
1109         break;
1110     case TARGET_NR_uselib:
1111         goto unimplemented;
1112     case TARGET_NR_swapon:
1113         ret = get_errno(swapon((const char *)arg1, arg2));
1114         break;
1115     case TARGET_NR_reboot:
1116         goto unimplemented;
1117     case TARGET_NR_readdir:
1118         goto unimplemented;
1119 #ifdef TARGET_I386
1120     case TARGET_NR_mmap:
1121         {
1122             uint32_t v1, v2, v3, v4, v5, v6, *vptr;
1123             vptr = (uint32_t *)arg1;
1124             v1 = tswap32(vptr[0]);
1125             v2 = tswap32(vptr[1]);
1126             v3 = tswap32(vptr[2]);
1127             v4 = tswap32(vptr[3]);
1128             v5 = tswap32(vptr[4]);
1129             v6 = tswap32(vptr[5]);
1130             ret = get_errno((long)mmap((void *)v1, v2, v3, v4, v5, v6));
1131         }
1132         break;
1133 #endif
1134 #ifdef TARGET_I386
1135     case TARGET_NR_mmap2:
1136 #else
1137     case TARGET_NR_mmap:
1138 #endif
1139         ret = get_errno((long)mmap((void *)arg1, arg2, arg3, arg4, arg5, arg6));
1140         break;
1141     case TARGET_NR_munmap:
1142         ret = get_errno(munmap((void *)arg1, arg2));
1143         break;
1144     case TARGET_NR_truncate:
1145         ret = get_errno(truncate((const char *)arg1, arg2));
1146         break;
1147     case TARGET_NR_ftruncate:
1148         ret = get_errno(ftruncate(arg1, arg2));
1149         break;
1150     case TARGET_NR_fchmod:
1151         ret = get_errno(fchmod(arg1, arg2));
1152         break;
1153     case TARGET_NR_fchown:
1154         ret = get_errno(fchown(arg1, arg2, arg3));
1155         break;
1156     case TARGET_NR_getpriority:
1157         ret = get_errno(getpriority(arg1, arg2));
1158         break;
1159     case TARGET_NR_setpriority:
1160         ret = get_errno(setpriority(arg1, arg2, arg3));
1161         break;
1162     case TARGET_NR_profil:
1163         goto unimplemented;
1164     case TARGET_NR_statfs:
1165         stfs = (void *)arg2;
1166         ret = get_errno(sys_statfs((const char *)arg1, stfs));
1167     convert_statfs:
1168         if (!is_error(ret)) {
1169             tswap32s(&stfs->f_type);
1170             tswap32s(&stfs->f_bsize);
1171             tswap32s(&stfs->f_blocks);
1172             tswap32s(&stfs->f_bfree);
1173             tswap32s(&stfs->f_bavail);
1174             tswap32s(&stfs->f_files);
1175             tswap32s(&stfs->f_ffree);
1176             tswap32s(&stfs->f_fsid.val[0]);
1177             tswap32s(&stfs->f_fsid.val[1]);
1178             tswap32s(&stfs->f_namelen);
1179         }
1180         break;
1181     case TARGET_NR_fstatfs:
1182         stfs = (void *)arg2;
1183         ret = get_errno(sys_fstatfs(arg1, stfs));
1184         goto convert_statfs;
1185     case TARGET_NR_ioperm:
1186         goto unimplemented;
1187     case TARGET_NR_socketcall:
1188         ret = do_socketcall(arg1, (long *)arg2);
1189         break;
1190     case TARGET_NR_syslog:
1191         goto unimplemented;
1192     case TARGET_NR_setitimer:
1193         goto unimplemented;
1194     case TARGET_NR_getitimer:
1195         goto unimplemented;
1196     case TARGET_NR_stat:
1197         ret = get_errno(stat((const char *)arg1, &st));
1198         goto do_stat;
1199     case TARGET_NR_lstat:
1200         ret = get_errno(lstat((const char *)arg1, &st));
1201         goto do_stat;
1202     case TARGET_NR_fstat:
1203         {
1204             ret = get_errno(fstat(arg1, &st));
1205         do_stat:
1206             if (!is_error(ret)) {
1207                 struct target_stat *target_st = (void *)arg2;
1208                 target_st->st_dev = tswap16(st.st_dev);
1209                 target_st->st_ino = tswapl(st.st_ino);
1210                 target_st->st_mode = tswap16(st.st_mode);
1211                 target_st->st_nlink = tswap16(st.st_nlink);
1212                 target_st->st_uid = tswap16(st.st_uid);
1213                 target_st->st_gid = tswap16(st.st_gid);
1214                 target_st->st_rdev = tswap16(st.st_rdev);
1215                 target_st->st_size = tswapl(st.st_size);
1216                 target_st->st_blksize = tswapl(st.st_blksize);
1217                 target_st->st_blocks = tswapl(st.st_blocks);
1218                 target_st->st_atime = tswapl(st.st_atime);
1219                 target_st->st_mtime = tswapl(st.st_mtime);
1220                 target_st->st_ctime = tswapl(st.st_ctime);
1221             }
1222         }
1223         break;
1224     case TARGET_NR_olduname:
1225         goto unimplemented;
1226     case TARGET_NR_iopl:
1227         goto unimplemented;
1228     case TARGET_NR_vhangup:
1229         ret = get_errno(vhangup());
1230         break;
1231     case TARGET_NR_idle:
1232         goto unimplemented;
1233     case TARGET_NR_vm86old:
1234         goto unimplemented;
1235     case TARGET_NR_wait4:
1236         {
1237             int status;
1238             target_long *status_ptr = (void *)arg2;
1239             struct rusage rusage, *rusage_ptr;
1240             struct target_rusage *target_rusage = (void *)arg4;
1241             if (target_rusage)
1242                 rusage_ptr = &rusage;
1243             else
1244                 rusage_ptr = NULL;
1245             ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
1246             if (!is_error(ret)) {
1247                 if (status_ptr)
1248                     *status_ptr = tswap32(status);
1249                 if (target_rusage) {
1250                     target_rusage->ru_utime.tv_sec = tswapl(rusage.ru_utime.tv_sec);
1251                     target_rusage->ru_utime.tv_usec = tswapl(rusage.ru_utime.tv_usec);
1252                     target_rusage->ru_stime.tv_sec = tswapl(rusage.ru_stime.tv_sec);
1253                     target_rusage->ru_stime.tv_usec = tswapl(rusage.ru_stime.tv_usec);
1254                     target_rusage->ru_maxrss = tswapl(rusage.ru_maxrss);
1255                     target_rusage->ru_ixrss = tswapl(rusage.ru_ixrss);
1256                     target_rusage->ru_idrss = tswapl(rusage.ru_idrss);
1257                     target_rusage->ru_isrss = tswapl(rusage.ru_isrss);
1258                     target_rusage->ru_minflt = tswapl(rusage.ru_minflt);
1259                     target_rusage->ru_majflt = tswapl(rusage.ru_majflt);
1260                     target_rusage->ru_nswap = tswapl(rusage.ru_nswap);
1261                     target_rusage->ru_inblock = tswapl(rusage.ru_inblock);
1262                     target_rusage->ru_oublock = tswapl(rusage.ru_oublock);
1263                     target_rusage->ru_msgsnd = tswapl(rusage.ru_msgsnd);
1264                     target_rusage->ru_msgrcv = tswapl(rusage.ru_msgrcv);
1265                     target_rusage->ru_nsignals = tswapl(rusage.ru_nsignals);
1266                     target_rusage->ru_nvcsw = tswapl(rusage.ru_nvcsw);
1267                     target_rusage->ru_nivcsw = tswapl(rusage.ru_nivcsw);
1268                 }
1269             }
1270         }
1271         break;
1272     case TARGET_NR_swapoff:
1273         ret = get_errno(swapoff((const char *)arg1));
1274         break;
1275     case TARGET_NR_sysinfo:
1276         goto unimplemented;
1277     case TARGET_NR_ipc:
1278         goto unimplemented;
1279     case TARGET_NR_fsync:
1280         ret = get_errno(fsync(arg1));
1281         break;
1282     case TARGET_NR_sigreturn:
1283         goto unimplemented;
1284     case TARGET_NR_clone:
1285         ret = get_errno(do_fork(cpu_env, arg1, arg2));
1286         break;
1287     case TARGET_NR_setdomainname:
1288         ret = get_errno(setdomainname((const char *)arg1, arg2));
1289         break;
1290     case TARGET_NR_uname:
1291         /* no need to transcode because we use the linux syscall */
1292         ret = get_errno(sys_uname((struct new_utsname *)arg1));
1293         break;
1294 #ifdef TARGET_I386
1295     case TARGET_NR_modify_ldt:
1296         ret = get_errno(gemu_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
1297         break;
1298 #endif
1299     case TARGET_NR_adjtimex:
1300         goto unimplemented;
1301     case TARGET_NR_mprotect:
1302         ret = get_errno(mprotect((void *)arg1, arg2, arg3));
1303         break;
1304     case TARGET_NR_sigprocmask:
1305         {
1306             int how = arg1;
1307             sigset_t set, oldset, *set_ptr;
1308             target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
1309             
1310             switch(how) {
1311             case TARGET_SIG_BLOCK:
1312                 how = SIG_BLOCK;
1313                 break;
1314             case TARGET_SIG_UNBLOCK:
1315                 how = SIG_UNBLOCK;
1316                 break;
1317             case TARGET_SIG_SETMASK:
1318                 how = SIG_SETMASK;
1319                 break;
1320             default:
1321                 ret = -EINVAL;
1322                 goto fail;
1323             }
1324             
1325             if (pset) {
1326                 target_to_host_old_sigset(&set, pset);
1327                 set_ptr = &set;
1328             } else {
1329                 set_ptr = NULL;
1330             }
1331             ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
1332             if (!is_error(ret) && poldset) {
1333                 host_to_target_old_sigset(poldset, &oldset);
1334             }
1335         }
1336         break;
1337     case TARGET_NR_create_module:
1338     case TARGET_NR_init_module:
1339     case TARGET_NR_delete_module:
1340     case TARGET_NR_get_kernel_syms:
1341         goto unimplemented;
1342     case TARGET_NR_quotactl:
1343         goto unimplemented;
1344     case TARGET_NR_getpgid:
1345         ret = get_errno(getpgid(arg1));
1346         break;
1347     case TARGET_NR_fchdir:
1348         ret = get_errno(fchdir(arg1));
1349         break;
1350     case TARGET_NR_bdflush:
1351         goto unimplemented;
1352     case TARGET_NR_sysfs:
1353         goto unimplemented;
1354     case TARGET_NR_personality:
1355         ret = get_errno(personality(arg1));
1356         break;
1357     case TARGET_NR_afs_syscall:
1358         goto unimplemented;
1359     case TARGET_NR_setfsuid:
1360         goto unimplemented;
1361     case TARGET_NR_setfsgid:
1362         goto unimplemented;
1363     case TARGET_NR__llseek:
1364         {
1365             int64_t res;
1366             ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
1367             *(int64_t *)arg4 = tswap64(res);
1368         }
1369         break;
1370     case TARGET_NR_getdents:
1371 #if TARGET_LONG_SIZE != 4
1372 #error not supported
1373 #endif
1374         {
1375             struct dirent *dirp = (void *)arg2;
1376             long count = arg3;
1377
1378             ret = get_errno(sys_getdents(arg1, dirp, count));
1379             if (!is_error(ret)) {
1380                 struct dirent *de;
1381                 int len = ret;
1382                 int reclen;
1383                 de = dirp;
1384                 while (len > 0) {
1385                     reclen = tswap16(de->d_reclen);
1386                     if (reclen > len)
1387                         break;
1388                     de->d_reclen = reclen;
1389                     tswapls(&de->d_ino);
1390                     tswapls(&de->d_off);
1391                     de = (struct dirent *)((char *)de + reclen);
1392                     len -= reclen;
1393                 }
1394             }
1395         }
1396         break;
1397     case TARGET_NR_getdents64:
1398         {
1399             struct dirent64 *dirp = (void *)arg2;
1400             long count = arg3;
1401             ret = get_errno(sys_getdents64(arg1, dirp, count));
1402             if (!is_error(ret)) {
1403                 struct dirent64 *de;
1404                 int len = ret;
1405                 int reclen;
1406                 de = dirp;
1407                 while (len > 0) {
1408                     reclen = tswap16(de->d_reclen);
1409                     if (reclen > len)
1410                         break;
1411                     de->d_reclen = reclen;
1412                     tswap64s(&de->d_ino);
1413                     tswap64s(&de->d_off);
1414                     de = (struct dirent64 *)((char *)de + reclen);
1415                     len -= reclen;
1416                 }
1417             }
1418         }
1419         break;
1420     case TARGET_NR__newselect:
1421         ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4, 
1422                         (void *)arg5);
1423         break;
1424     case TARGET_NR_flock:
1425         goto unimplemented;
1426     case TARGET_NR_msync:
1427         ret = get_errno(msync((void *)arg1, arg2, arg3));
1428         break;
1429     case TARGET_NR_readv:
1430         {
1431             int count = arg3;
1432             int i;
1433             struct iovec *vec;
1434             struct target_iovec *target_vec = (void *)arg2;
1435
1436             vec = alloca(count * sizeof(struct iovec));
1437             for(i = 0;i < count; i++) {
1438                 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
1439                 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1440             }
1441             ret = get_errno(readv(arg1, vec, count));
1442         }
1443         break;
1444     case TARGET_NR_writev:
1445         {
1446             int count = arg3;
1447             int i;
1448             struct iovec *vec;
1449             struct target_iovec *target_vec = (void *)arg2;
1450
1451             vec = alloca(count * sizeof(struct iovec));
1452             for(i = 0;i < count; i++) {
1453                 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
1454                 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1455             }
1456             ret = get_errno(writev(arg1, vec, count));
1457         }
1458         break;
1459     case TARGET_NR_getsid:
1460         ret = get_errno(getsid(arg1));
1461         break;
1462     case TARGET_NR_fdatasync:
1463         goto unimplemented;
1464     case TARGET_NR__sysctl:
1465         goto unimplemented;
1466     case TARGET_NR_mlock:
1467         ret = get_errno(mlock((void *)arg1, arg2));
1468         break;
1469     case TARGET_NR_munlock:
1470         ret = get_errno(munlock((void *)arg1, arg2));
1471         break;
1472     case TARGET_NR_mlockall:
1473         ret = get_errno(mlockall(arg1));
1474         break;
1475     case TARGET_NR_munlockall:
1476         ret = get_errno(munlockall());
1477         break;
1478     case TARGET_NR_sched_setparam:
1479         goto unimplemented;
1480     case TARGET_NR_sched_getparam:
1481         goto unimplemented;
1482     case TARGET_NR_sched_setscheduler:
1483         goto unimplemented;
1484     case TARGET_NR_sched_getscheduler:
1485         goto unimplemented;
1486     case TARGET_NR_sched_yield:
1487         ret = get_errno(sched_yield());
1488         break;
1489     case TARGET_NR_sched_get_priority_max:
1490     case TARGET_NR_sched_get_priority_min:
1491     case TARGET_NR_sched_rr_get_interval:
1492         goto unimplemented;
1493         
1494     case TARGET_NR_nanosleep:
1495         {
1496             struct target_timespec *target_req = (void *)arg1;
1497             struct target_timespec *target_rem = (void *)arg2;
1498             struct timespec req, rem;
1499             req.tv_sec = tswapl(target_req->tv_sec);
1500             req.tv_nsec = tswapl(target_req->tv_nsec);
1501             ret = get_errno(nanosleep(&req, &rem));
1502             if (target_rem) {
1503                 target_rem->tv_sec = tswapl(rem.tv_sec);
1504                 target_rem->tv_nsec = tswapl(rem.tv_nsec);
1505             }
1506         }
1507         break;
1508
1509     case TARGET_NR_mremap:
1510     case TARGET_NR_setresuid:
1511     case TARGET_NR_getresuid:
1512     case TARGET_NR_vm86:
1513     case TARGET_NR_query_module:
1514     case TARGET_NR_poll:
1515     case TARGET_NR_nfsservctl:
1516     case TARGET_NR_setresgid:
1517     case TARGET_NR_getresgid:
1518     case TARGET_NR_prctl:
1519     case TARGET_NR_rt_sigreturn:
1520     case TARGET_NR_rt_sigaction:
1521     case TARGET_NR_rt_sigprocmask:
1522     case TARGET_NR_rt_sigpending:
1523     case TARGET_NR_rt_sigtimedwait:
1524     case TARGET_NR_rt_sigqueueinfo:
1525     case TARGET_NR_rt_sigsuspend:
1526     case TARGET_NR_pread:
1527     case TARGET_NR_pwrite:
1528         goto unimplemented;
1529     case TARGET_NR_chown:
1530         ret = get_errno(chown((const char *)arg1, arg2, arg3));
1531         break;
1532     case TARGET_NR_getcwd:
1533         ret = get_errno(sys_getcwd1((char *)arg1, arg2));
1534         break;
1535     case TARGET_NR_capget:
1536     case TARGET_NR_capset:
1537     case TARGET_NR_sigaltstack:
1538     case TARGET_NR_sendfile:
1539     case TARGET_NR_getpmsg:
1540     case TARGET_NR_putpmsg:
1541     case TARGET_NR_vfork:
1542         ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
1543         break;
1544     case TARGET_NR_ugetrlimit:
1545     case TARGET_NR_truncate64:
1546     case TARGET_NR_ftruncate64:
1547         goto unimplemented;
1548     case TARGET_NR_stat64:
1549         ret = get_errno(stat((const char *)arg1, &st));
1550         goto do_stat64;
1551     case TARGET_NR_lstat64:
1552         ret = get_errno(lstat((const char *)arg1, &st));
1553         goto do_stat64;
1554     case TARGET_NR_fstat64:
1555         {
1556             ret = get_errno(fstat(arg1, &st));
1557         do_stat64:
1558             if (!is_error(ret)) {
1559                 struct target_stat64 *target_st = (void *)arg2;
1560                 target_st->st_dev = tswap16(st.st_dev);
1561                 target_st->st_ino = tswapl(st.st_ino);
1562                 target_st->st_mode = tswap16(st.st_mode);
1563                 target_st->st_nlink = tswap16(st.st_nlink);
1564                 target_st->st_uid = tswap16(st.st_uid);
1565                 target_st->st_gid = tswap16(st.st_gid);
1566                 target_st->st_rdev = tswap16(st.st_rdev);
1567                 /* XXX: better use of kernel struct */
1568                 target_st->st_size = tswapl(st.st_size);
1569                 target_st->st_blksize = tswapl(st.st_blksize);
1570                 target_st->st_blocks = tswapl(st.st_blocks);
1571                 target_st->st_atime = tswapl(st.st_atime);
1572                 target_st->st_mtime = tswapl(st.st_mtime);
1573                 target_st->st_ctime = tswapl(st.st_ctime);
1574             }
1575         }
1576         break;
1577
1578     case TARGET_NR_lchown32:
1579     case TARGET_NR_getuid32:
1580     case TARGET_NR_getgid32:
1581     case TARGET_NR_geteuid32:
1582     case TARGET_NR_getegid32:
1583     case TARGET_NR_setreuid32:
1584     case TARGET_NR_setregid32:
1585     case TARGET_NR_getgroups32:
1586     case TARGET_NR_setgroups32:
1587     case TARGET_NR_fchown32:
1588     case TARGET_NR_setresuid32:
1589     case TARGET_NR_getresuid32:
1590     case TARGET_NR_setresgid32:
1591     case TARGET_NR_getresgid32:
1592     case TARGET_NR_chown32:
1593     case TARGET_NR_setuid32:
1594     case TARGET_NR_setgid32:
1595     case TARGET_NR_setfsuid32:
1596     case TARGET_NR_setfsgid32:
1597     case TARGET_NR_pivot_root:
1598     case TARGET_NR_mincore:
1599     case TARGET_NR_madvise:
1600         goto unimplemented;
1601 #if TARGET_LONG_BITS == 32
1602     case TARGET_NR_fcntl64:
1603         switch(arg2) {
1604         case F_GETLK64:
1605         case F_SETLK64:
1606         case F_SETLKW64:
1607             goto unimplemented;
1608         default:
1609             ret = get_errno(fcntl(arg1, arg2, arg3));
1610             break;
1611         }
1612         break;
1613 #endif
1614     case TARGET_NR_security:
1615         goto unimplemented;
1616     case TARGET_NR_gettid:
1617         ret = get_errno(gettid());
1618         break;
1619     case TARGET_NR_readahead:
1620     case TARGET_NR_setxattr:
1621     case TARGET_NR_lsetxattr:
1622     case TARGET_NR_fsetxattr:
1623     case TARGET_NR_getxattr:
1624     case TARGET_NR_lgetxattr:
1625     case TARGET_NR_fgetxattr:
1626     case TARGET_NR_listxattr:
1627     case TARGET_NR_llistxattr:
1628     case TARGET_NR_flistxattr:
1629     case TARGET_NR_removexattr:
1630     case TARGET_NR_lremovexattr:
1631     case TARGET_NR_fremovexattr:
1632         goto unimplemented;
1633     default:
1634     unimplemented:
1635         gemu_log("Unsupported syscall: %d\n", num);
1636         ret = -ENOSYS;
1637         break;
1638     }
1639  fail:
1640     return ret;
1641 }
1642