Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ 982b4315

History | View | Annotate | Download (79.5 kB)

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