Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ a69d83b6

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