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