blob: ca52eb4e9e843617e3fac8187334a3149f2531bf [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)
bellardec86b0f2003-04-11 00:15:04 +0000108#ifdef __NR_exit_group
109_syscall1(int,exit_group,int,error_code)
110#endif
bellard66fb9762003-03-23 01:06:05 +0000111
112extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000113extern int flock(int, int);
114extern int setfsuid(int);
115extern int setfsgid(int);
bellard5cd43932003-03-29 16:54:36 +0000116extern int setresuid(uid_t, uid_t, uid_t);
117extern int getresuid(uid_t *, uid_t *, uid_t *);
118extern int setresgid(gid_t, gid_t, gid_t);
119extern int getresgid(gid_t *, gid_t *, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000120
121static inline long get_errno(long ret)
122{
123 if (ret == -1)
124 return -errno;
125 else
126 return ret;
127}
128
129static inline int is_error(long ret)
130{
131 return (unsigned long)ret >= (unsigned long)(-4096);
132}
133
134static char *target_brk;
135static char *target_original_brk;
136
137void target_set_brk(char *new_brk)
138{
139 target_brk = new_brk;
140 target_original_brk = new_brk;
141}
142
143static long do_brk(char *new_brk)
144{
145 char *brk_page;
146 long mapped_addr;
147 int new_alloc_size;
148
149 if (!new_brk)
150 return (long)target_brk;
151 if (new_brk < target_original_brk)
152 return -ENOMEM;
153
154 brk_page = (char *)(((unsigned long)target_brk + PAGE_SIZE - 1) & PAGE_MASK);
155
156 /* If the new brk is less than this, set it and we're done... */
157 if (new_brk < brk_page) {
158 target_brk = new_brk;
159 return (long)target_brk;
160 }
161
162 /* We need to allocate more memory after the brk... */
163 new_alloc_size = ((new_brk - brk_page + 1)+(PAGE_SIZE-1)) & PAGE_MASK;
164 mapped_addr = get_errno((long)mmap((caddr_t)brk_page, new_alloc_size,
165 PROT_READ|PROT_WRITE,
166 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
167
168 if (is_error(mapped_addr)) {
169 return mapped_addr;
170 } else {
171 target_brk = new_brk;
172 return (long)target_brk;
173 }
174}
175
176static inline fd_set *target_to_host_fds(fd_set *fds,
177 target_long *target_fds, int n)
178{
bellard7854b052003-03-29 17:22:23 +0000179#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000180 return (fd_set *)target_fds;
181#else
182 int i, b;
183 if (target_fds) {
184 FD_ZERO(fds);
185 for(i = 0;i < n; i++) {
186 b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
187 (i & (TARGET_LONG_BITS - 1))) & 1;
188 if (b)
189 FD_SET(i, fds);
190 }
191 return fds;
192 } else {
193 return NULL;
194 }
195#endif
196}
197
198static inline void host_to_target_fds(target_long *target_fds,
199 fd_set *fds, int n)
200{
bellard7854b052003-03-29 17:22:23 +0000201#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000202 /* nothing to do */
203#else
204 int i, nw, j, k;
205 target_long v;
206
207 if (target_fds) {
208 nw = n / TARGET_LONG_BITS;
209 k = 0;
210 for(i = 0;i < nw; i++) {
211 v = 0;
212 for(j = 0; j < TARGET_LONG_BITS; j++) {
213 v |= ((FD_ISSET(k, fds) != 0) << j);
214 k++;
215 }
216 target_fds[i] = tswapl(v);
217 }
218 }
219#endif
220}
221
bellard66fb9762003-03-23 01:06:05 +0000222static inline void target_to_host_timeval(struct timeval *tv,
bellard5cd43932003-03-29 16:54:36 +0000223 const struct target_timeval *target_tv)
bellard31e31b82003-02-18 22:55:36 +0000224{
bellard66fb9762003-03-23 01:06:05 +0000225 tv->tv_sec = tswapl(target_tv->tv_sec);
226 tv->tv_usec = tswapl(target_tv->tv_usec);
bellard31e31b82003-02-18 22:55:36 +0000227}
228
bellard66fb9762003-03-23 01:06:05 +0000229static inline void host_to_target_timeval(struct target_timeval *target_tv,
bellard5cd43932003-03-29 16:54:36 +0000230 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000231{
bellard66fb9762003-03-23 01:06:05 +0000232 target_tv->tv_sec = tswapl(tv->tv_sec);
233 target_tv->tv_usec = tswapl(tv->tv_usec);
bellard31e31b82003-02-18 22:55:36 +0000234}
235
236
237static long do_select(long n,
238 target_long *target_rfds, target_long *target_wfds,
239 target_long *target_efds, struct target_timeval *target_tv)
240{
241 fd_set rfds, wfds, efds;
242 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
243 struct timeval tv, *tv_ptr;
244 long ret;
245
246 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
247 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
248 efds_ptr = target_to_host_fds(&efds, target_efds, n);
249
250 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000251 target_to_host_timeval(&tv, target_tv);
bellard31e31b82003-02-18 22:55:36 +0000252 tv_ptr = &tv;
253 } else {
254 tv_ptr = NULL;
255 }
256 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
257 if (!is_error(ret)) {
258 host_to_target_fds(target_rfds, rfds_ptr, n);
259 host_to_target_fds(target_wfds, wfds_ptr, n);
260 host_to_target_fds(target_efds, efds_ptr, n);
261
262 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000263 host_to_target_timeval(target_tv, &tv);
bellard31e31b82003-02-18 22:55:36 +0000264 }
265 }
266 return ret;
267}
268
bellard7854b052003-03-29 17:22:23 +0000269static inline void target_to_host_sockaddr(struct sockaddr *addr,
270 struct target_sockaddr *target_addr,
271 socklen_t len)
272{
273 memcpy(addr, target_addr, len);
274 addr->sa_family = tswap16(target_addr->sa_family);
275}
276
277static inline void host_to_target_sockaddr(struct target_sockaddr *target_addr,
278 struct sockaddr *addr,
279 socklen_t len)
280{
281 memcpy(target_addr, addr, len);
282 target_addr->sa_family = tswap16(addr->sa_family);
283}
284
285static inline void target_to_host_cmsg(struct msghdr *msgh,
286 struct target_msghdr *target_msgh)
287{
288 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
289 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
290 socklen_t space = 0;
291
292 while (cmsg && target_cmsg) {
293 void *data = CMSG_DATA(cmsg);
294 void *target_data = TARGET_CMSG_DATA(target_cmsg);
295
296 int len = tswapl(target_cmsg->cmsg_len)
297 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
298
299 space += CMSG_SPACE(len);
300 if (space > msgh->msg_controllen) {
301 space -= CMSG_SPACE(len);
302 gemu_log("Host cmsg overflow");
303 break;
304 }
305
306 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
307 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
308 cmsg->cmsg_len = CMSG_LEN(len);
309
310 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
311 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
312 memcpy(data, target_data, len);
313 } else {
314 int *fd = (int *)data;
315 int *target_fd = (int *)target_data;
316 int i, numfds = len / sizeof(int);
317
318 for (i = 0; i < numfds; i++)
319 fd[i] = tswap32(target_fd[i]);
320 }
321
322 cmsg = CMSG_NXTHDR(msgh, cmsg);
323 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
324 }
325
326 msgh->msg_controllen = space;
327}
328
329static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
330 struct msghdr *msgh)
331{
332 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
333 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
334 socklen_t space = 0;
335
336 while (cmsg && target_cmsg) {
337 void *data = CMSG_DATA(cmsg);
338 void *target_data = TARGET_CMSG_DATA(target_cmsg);
339
340 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
341
342 space += TARGET_CMSG_SPACE(len);
343 if (space > tswapl(target_msgh->msg_controllen)) {
344 space -= TARGET_CMSG_SPACE(len);
345 gemu_log("Target cmsg overflow");
346 break;
347 }
348
349 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
350 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
351 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
352
353 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
354 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
355 memcpy(target_data, data, len);
356 } else {
357 int *fd = (int *)data;
358 int *target_fd = (int *)target_data;
359 int i, numfds = len / sizeof(int);
360
361 for (i = 0; i < numfds; i++)
362 target_fd[i] = tswap32(fd[i]);
363 }
364
365 cmsg = CMSG_NXTHDR(msgh, cmsg);
366 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
367 }
368
369 msgh->msg_controllen = tswapl(space);
370}
371
372static long do_setsockopt(int sockfd, int level, int optname,
373 void *optval, socklen_t optlen)
374{
375 if (level == SOL_TCP) {
376 /* TCP options all take an 'int' value. */
377 int val;
378
379 if (optlen < sizeof(uint32_t))
380 return -EINVAL;
381
382 val = tswap32(*(uint32_t *)optval);
383 return get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
384 }
385
386 else if (level != SOL_SOCKET) {
387 gemu_log("Unsupported setsockopt level: %d\n", level);
388 return -ENOSYS;
389 }
390
391 switch (optname) {
392 /* Options with 'int' argument. */
393 case SO_DEBUG:
394 case SO_REUSEADDR:
395 case SO_TYPE:
396 case SO_ERROR:
397 case SO_DONTROUTE:
398 case SO_BROADCAST:
399 case SO_SNDBUF:
400 case SO_RCVBUF:
401 case SO_KEEPALIVE:
402 case SO_OOBINLINE:
403 case SO_NO_CHECK:
404 case SO_PRIORITY:
405 case SO_BSDCOMPAT:
406 case SO_PASSCRED:
407 case SO_TIMESTAMP:
408 case SO_RCVLOWAT:
409 case SO_RCVTIMEO:
410 case SO_SNDTIMEO:
411 {
412 int val;
413 if (optlen < sizeof(uint32_t))
414 return -EINVAL;
415 val = tswap32(*(uint32_t *)optval);
416 return get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
417 }
418
419 default:
420 gemu_log("Unsupported setsockopt SOL_SOCKET option: %d\n", optname);
421 return -ENOSYS;
422 }
423}
424
425static long do_getsockopt(int sockfd, int level, int optname,
426 void *optval, socklen_t *optlen)
427{
428 gemu_log("getsockopt not yet supported\n");
429 return -ENOSYS;
430}
431
432static long do_socketcall(int num, int32_t *vptr)
bellard31e31b82003-02-18 22:55:36 +0000433{
434 long ret;
435
436 switch(num) {
437 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +0000438 {
439 int domain = tswap32(vptr[0]);
440 int type = tswap32(vptr[1]);
441 int protocol = tswap32(vptr[2]);
442
443 ret = get_errno(socket(domain, type, protocol));
444 }
bellard31e31b82003-02-18 22:55:36 +0000445 break;
446 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +0000447 {
448 int sockfd = tswap32(vptr[0]);
449 void *target_addr = (void *)tswap32(vptr[1]);
450 socklen_t addrlen = tswap32(vptr[2]);
451 void *addr = alloca(addrlen);
452
453 target_to_host_sockaddr(addr, target_addr, addrlen);
454 ret = get_errno(bind(sockfd, addr, addrlen));
455 }
bellard31e31b82003-02-18 22:55:36 +0000456 break;
457 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +0000458 {
459 int sockfd = tswap32(vptr[0]);
460 void *target_addr = (void *)tswap32(vptr[1]);
461 socklen_t addrlen = tswap32(vptr[2]);
462 void *addr = alloca(addrlen);
463
464 target_to_host_sockaddr(addr, target_addr, addrlen);
465 ret = get_errno(connect(sockfd, addr, addrlen));
466 }
bellard31e31b82003-02-18 22:55:36 +0000467 break;
468 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +0000469 {
470 int sockfd = tswap32(vptr[0]);
471 int backlog = tswap32(vptr[1]);
472
473 ret = get_errno(listen(sockfd, backlog));
474 }
bellard31e31b82003-02-18 22:55:36 +0000475 break;
476 case SOCKOP_accept:
477 {
bellard7854b052003-03-29 17:22:23 +0000478 int sockfd = tswap32(vptr[0]);
479 void *target_addr = (void *)tswap32(vptr[1]);
480 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
481 socklen_t addrlen = tswap32(*target_addrlen);
482 void *addr = alloca(addrlen);
483
484 ret = get_errno(accept(sockfd, addr, &addrlen));
485 if (!is_error(ret)) {
486 host_to_target_sockaddr(target_addr, addr, addrlen);
487 *target_addrlen = tswap32(addrlen);
488 }
bellard31e31b82003-02-18 22:55:36 +0000489 }
490 break;
491 case SOCKOP_getsockname:
492 {
bellard7854b052003-03-29 17:22:23 +0000493 int sockfd = tswap32(vptr[0]);
494 void *target_addr = (void *)tswap32(vptr[1]);
495 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
496 socklen_t addrlen = tswap32(*target_addrlen);
497 void *addr = alloca(addrlen);
498
499 ret = get_errno(getsockname(sockfd, addr, &addrlen));
500 if (!is_error(ret)) {
501 host_to_target_sockaddr(target_addr, addr, addrlen);
502 *target_addrlen = tswap32(addrlen);
503 }
bellard31e31b82003-02-18 22:55:36 +0000504 }
505 break;
506 case SOCKOP_getpeername:
507 {
bellard7854b052003-03-29 17:22:23 +0000508 int sockfd = tswap32(vptr[0]);
509 void *target_addr = (void *)tswap32(vptr[1]);
510 uint32_t *target_addrlen = (void *)tswap32(vptr[2]);
511 socklen_t addrlen = tswap32(*target_addrlen);
512 void *addr = alloca(addrlen);
513
514 ret = get_errno(getpeername(sockfd, addr, &addrlen));
515 if (!is_error(ret)) {
516 host_to_target_sockaddr(target_addr, addr, addrlen);
517 *target_addrlen = tswap32(addrlen);
518 }
bellard31e31b82003-02-18 22:55:36 +0000519 }
520 break;
521 case SOCKOP_socketpair:
522 {
bellard7854b052003-03-29 17:22:23 +0000523 int domain = tswap32(vptr[0]);
524 int type = tswap32(vptr[1]);
525 int protocol = tswap32(vptr[2]);
526 int32_t *target_tab = (void *)tswap32(vptr[3]);
bellard31e31b82003-02-18 22:55:36 +0000527 int tab[2];
bellard7854b052003-03-29 17:22:23 +0000528
529 ret = get_errno(socketpair(domain, type, protocol, tab));
bellard31e31b82003-02-18 22:55:36 +0000530 if (!is_error(ret)) {
531 target_tab[0] = tswap32(tab[0]);
532 target_tab[1] = tswap32(tab[1]);
533 }
534 }
535 break;
536 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +0000537 {
538 int sockfd = tswap32(vptr[0]);
539 void *msg = (void *)tswap32(vptr[1]);
540 size_t len = tswap32(vptr[2]);
541 int flags = tswap32(vptr[3]);
542
543 ret = get_errno(send(sockfd, msg, len, flags));
544 }
bellard31e31b82003-02-18 22:55:36 +0000545 break;
546 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +0000547 {
548 int sockfd = tswap32(vptr[0]);
549 void *msg = (void *)tswap32(vptr[1]);
550 size_t len = tswap32(vptr[2]);
551 int flags = tswap32(vptr[3]);
552
553 ret = get_errno(recv(sockfd, msg, len, flags));
554 }
bellard31e31b82003-02-18 22:55:36 +0000555 break;
556 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +0000557 {
558 int sockfd = tswap32(vptr[0]);
559 void *msg = (void *)tswap32(vptr[1]);
560 size_t len = tswap32(vptr[2]);
561 int flags = tswap32(vptr[3]);
562 void *target_addr = (void *)tswap32(vptr[4]);
563 socklen_t addrlen = tswap32(vptr[5]);
564 void *addr = alloca(addrlen);
565
566 target_to_host_sockaddr(addr, target_addr, addrlen);
567 ret = get_errno(sendto(sockfd, msg, len, flags, addr, addrlen));
568 }
bellard31e31b82003-02-18 22:55:36 +0000569 break;
570 case SOCKOP_recvfrom:
571 {
bellard7854b052003-03-29 17:22:23 +0000572 int sockfd = tswap32(vptr[0]);
573 void *msg = (void *)tswap32(vptr[1]);
574 size_t len = tswap32(vptr[2]);
575 int flags = tswap32(vptr[3]);
576 void *target_addr = (void *)tswap32(vptr[4]);
577 uint32_t *target_addrlen = (void *)tswap32(vptr[5]);
578 socklen_t addrlen = tswap32(*target_addrlen);
579 void *addr = alloca(addrlen);
580
581 ret = get_errno(recvfrom(sockfd, msg, len, flags, addr, &addrlen));
582 if (!is_error(ret)) {
583 host_to_target_sockaddr(target_addr, addr, addrlen);
584 *target_addrlen = tswap32(addrlen);
585 }
bellard31e31b82003-02-18 22:55:36 +0000586 }
587 break;
588 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +0000589 {
590 int sockfd = tswap32(vptr[0]);
591 int how = tswap32(vptr[1]);
592
593 ret = get_errno(shutdown(sockfd, how));
594 }
bellard31e31b82003-02-18 22:55:36 +0000595 break;
596 case SOCKOP_sendmsg:
597 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +0000598 {
599 int fd;
600 struct target_msghdr *msgp;
601 struct msghdr msg;
602 int flags, count, i;
603 struct iovec *vec;
604 struct target_iovec *target_vec;
605
bellard7854b052003-03-29 17:22:23 +0000606 msgp = (void *)tswap32(vptr[1]);
bellard1a9353d2003-03-16 20:28:50 +0000607 msg.msg_name = (void *)tswapl(msgp->msg_name);
608 msg.msg_namelen = tswapl(msgp->msg_namelen);
bellard7854b052003-03-29 17:22:23 +0000609 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
610 msg.msg_control = alloca(msg.msg_controllen);
bellard1a9353d2003-03-16 20:28:50 +0000611 msg.msg_flags = tswap32(msgp->msg_flags);
612
613 count = tswapl(msgp->msg_iovlen);
614 vec = alloca(count * sizeof(struct iovec));
615 target_vec = (void *)tswapl(msgp->msg_iov);
616 for(i = 0;i < count; i++) {
617 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
618 vec[i].iov_len = tswapl(target_vec[i].iov_len);
619 }
620 msg.msg_iovlen = count;
621 msg.msg_iov = vec;
622
bellard7854b052003-03-29 17:22:23 +0000623 fd = tswap32(vptr[0]);
624 flags = tswap32(vptr[2]);
625 if (num == SOCKOP_sendmsg) {
626 target_to_host_cmsg(&msg, msgp);
627 ret = get_errno(sendmsg(fd, &msg, flags));
628 } else {
629 ret = get_errno(recvmsg(fd, &msg, flags));
630 if (!is_error(ret))
631 host_to_target_cmsg(msgp, &msg);
632 }
bellard1a9353d2003-03-16 20:28:50 +0000633 }
634 break;
bellard31e31b82003-02-18 22:55:36 +0000635 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +0000636 {
637 int sockfd = tswap32(vptr[0]);
638 int level = tswap32(vptr[1]);
639 int optname = tswap32(vptr[2]);
640 void *optval = (void *)tswap32(vptr[3]);
641 socklen_t optlen = tswap32(vptr[4]);
642
643 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
644 }
645 break;
bellard31e31b82003-02-18 22:55:36 +0000646 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +0000647 {
648 int sockfd = tswap32(vptr[0]);
649 int level = tswap32(vptr[1]);
650 int optname = tswap32(vptr[2]);
651 void *optval = (void *)tswap32(vptr[3]);
652 uint32_t *target_len = (void *)tswap32(vptr[4]);
653 socklen_t optlen = tswap32(*target_len);
654
655 ret = do_getsockopt(sockfd, level, optname, optval, &optlen);
656 if (!is_error(ret))
657 *target_len = tswap32(optlen);
658 }
659 break;
bellard31e31b82003-02-18 22:55:36 +0000660 default:
661 gemu_log("Unsupported socketcall: %d\n", num);
662 ret = -ENOSYS;
663 break;
664 }
665 return ret;
666}
667
668/* kernel structure types definitions */
669#define IFNAMSIZ 16
670
671#define STRUCT(name, list...) STRUCT_ ## name,
672#define STRUCT_SPECIAL(name) STRUCT_ ## name,
673enum {
674#include "syscall_types.h"
675};
676#undef STRUCT
677#undef STRUCT_SPECIAL
678
679#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
680#define STRUCT_SPECIAL(name)
681#include "syscall_types.h"
682#undef STRUCT
683#undef STRUCT_SPECIAL
684
685typedef struct IOCTLEntry {
686 int target_cmd;
687 int host_cmd;
688 const char *name;
689 int access;
bellard1a9353d2003-03-16 20:28:50 +0000690 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +0000691} IOCTLEntry;
692
693#define IOC_R 0x0001
694#define IOC_W 0x0002
695#define IOC_RW (IOC_R | IOC_W)
696
697#define MAX_STRUCT_SIZE 4096
698
699const IOCTLEntry ioctl_entries[] = {
700#define IOCTL(cmd, access, types...) \
701 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
702#include "ioctls.h"
703 { 0, 0, },
704};
705
706static long do_ioctl(long fd, long cmd, long arg)
707{
708 const IOCTLEntry *ie;
709 const argtype *arg_type;
710 long ret;
711 uint8_t buf_temp[MAX_STRUCT_SIZE];
712
713 ie = ioctl_entries;
714 for(;;) {
715 if (ie->target_cmd == 0) {
716 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
717 return -ENOSYS;
718 }
719 if (ie->target_cmd == cmd)
720 break;
721 ie++;
722 }
723 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +0000724#if defined(DEBUG)
bellard72f03902003-02-18 23:33:18 +0000725 gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
726#endif
bellard31e31b82003-02-18 22:55:36 +0000727 switch(arg_type[0]) {
728 case TYPE_NULL:
729 /* no argument */
730 ret = get_errno(ioctl(fd, ie->host_cmd));
731 break;
732 case TYPE_PTRVOID:
733 case TYPE_INT:
734 /* int argment */
735 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
736 break;
737 case TYPE_PTR:
738 arg_type++;
739 switch(ie->access) {
740 case IOC_R:
741 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
742 if (!is_error(ret)) {
743 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
744 }
745 break;
746 case IOC_W:
747 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
748 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
749 break;
750 default:
751 case IOC_RW:
752 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
753 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
754 if (!is_error(ret)) {
755 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
756 }
757 break;
758 }
759 break;
760 default:
761 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
762 ret = -ENOSYS;
763 break;
764 }
765 return ret;
766}
767
768bitmask_transtbl iflag_tbl[] = {
769 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
770 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
771 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
772 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
773 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
774 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
775 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
776 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
777 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
778 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
779 { TARGET_IXON, TARGET_IXON, IXON, IXON },
780 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
781 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
782 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
783 { 0, 0, 0, 0 }
784};
785
786bitmask_transtbl oflag_tbl[] = {
787 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
788 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
789 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
790 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
791 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
792 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
793 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
794 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
795 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
796 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
797 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
798 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
799 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
800 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
801 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
802 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
803 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
804 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
805 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
806 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
807 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
808 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
809 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
810 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
811 { 0, 0, 0, 0 }
812};
813
814bitmask_transtbl cflag_tbl[] = {
815 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
816 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
817 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
818 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
819 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
820 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
821 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
822 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
823 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
824 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
825 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
826 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
827 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
828 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
829 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
830 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
831 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
832 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
833 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
834 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
835 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
836 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
837 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
838 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
839 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
840 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
841 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
842 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
843 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
844 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
845 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
846 { 0, 0, 0, 0 }
847};
848
849bitmask_transtbl lflag_tbl[] = {
850 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
851 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
852 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
853 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
854 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
855 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
856 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
857 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
858 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
859 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
860 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
861 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
862 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
863 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
864 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
865 { 0, 0, 0, 0 }
866};
867
868static void target_to_host_termios (void *dst, const void *src)
869{
870 struct host_termios *host = dst;
871 const struct target_termios *target = src;
872
873 host->c_iflag =
874 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
875 host->c_oflag =
876 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
877 host->c_cflag =
878 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
879 host->c_lflag =
880 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
881 host->c_line = target->c_line;
882
883 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
884 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
885 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
886 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
887 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
888 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
889 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
890 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
891 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
892 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
893 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
894 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
895 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
896 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
897 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
898 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
899 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
900}
901
902static void host_to_target_termios (void *dst, const void *src)
903{
904 struct target_termios *target = dst;
905 const struct host_termios *host = src;
906
907 target->c_iflag =
908 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
909 target->c_oflag =
910 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
911 target->c_cflag =
912 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
913 target->c_lflag =
914 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
915 target->c_line = host->c_line;
916
917 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
918 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
919 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
920 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
921 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
922 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
923 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
924 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
925 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
926 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
927 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
928 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
929 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
930 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
931 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
932 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
933 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
934}
935
936StructEntry struct_termios_def = {
937 .convert = { host_to_target_termios, target_to_host_termios },
938 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
939 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
940};
941
bellard6dbad632003-03-16 18:05:05 +0000942#ifdef TARGET_I386
943
944/* NOTE: there is really one LDT for all the threads */
945uint8_t *ldt_table;
946
947static int read_ldt(void *ptr, unsigned long bytecount)
948{
949 int size;
950
951 if (!ldt_table)
952 return 0;
953 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
954 if (size > bytecount)
955 size = bytecount;
956 memcpy(ptr, ldt_table, size);
957 return size;
958}
959
960/* XXX: add locking support */
961static int write_ldt(CPUX86State *env,
962 void *ptr, unsigned long bytecount, int oldmode)
963{
964 struct target_modify_ldt_ldt_s ldt_info;
965 int seg_32bit, contents, read_exec_only, limit_in_pages;
966 int seg_not_present, useable;
967 uint32_t *lp, entry_1, entry_2;
968
969 if (bytecount != sizeof(ldt_info))
970 return -EINVAL;
971 memcpy(&ldt_info, ptr, sizeof(ldt_info));
972 tswap32s(&ldt_info.entry_number);
973 tswapls((long *)&ldt_info.base_addr);
974 tswap32s(&ldt_info.limit);
975 tswap32s(&ldt_info.flags);
976
977 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
978 return -EINVAL;
979 seg_32bit = ldt_info.flags & 1;
980 contents = (ldt_info.flags >> 1) & 3;
981 read_exec_only = (ldt_info.flags >> 3) & 1;
982 limit_in_pages = (ldt_info.flags >> 4) & 1;
983 seg_not_present = (ldt_info.flags >> 5) & 1;
984 useable = (ldt_info.flags >> 6) & 1;
985
986 if (contents == 3) {
987 if (oldmode)
988 return -EINVAL;
989 if (seg_not_present == 0)
990 return -EINVAL;
991 }
992 /* allocate the LDT */
993 if (!ldt_table) {
994 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
995 if (!ldt_table)
996 return -ENOMEM;
997 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
998 env->ldt.base = ldt_table;
999 env->ldt.limit = 0xffff;
1000 }
1001
1002 /* NOTE: same code as Linux kernel */
1003 /* Allow LDTs to be cleared by the user. */
1004 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
1005 if (oldmode ||
1006 (contents == 0 &&
1007 read_exec_only == 1 &&
1008 seg_32bit == 0 &&
1009 limit_in_pages == 0 &&
1010 seg_not_present == 1 &&
1011 useable == 0 )) {
1012 entry_1 = 0;
1013 entry_2 = 0;
1014 goto install;
1015 }
1016 }
1017
1018 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
1019 (ldt_info.limit & 0x0ffff);
1020 entry_2 = (ldt_info.base_addr & 0xff000000) |
1021 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
1022 (ldt_info.limit & 0xf0000) |
1023 ((read_exec_only ^ 1) << 9) |
1024 (contents << 10) |
1025 ((seg_not_present ^ 1) << 15) |
1026 (seg_32bit << 22) |
1027 (limit_in_pages << 23) |
1028 0x7000;
1029 if (!oldmode)
1030 entry_2 |= (useable << 20);
1031
1032 /* Install the new entry ... */
1033install:
1034 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
1035 lp[0] = tswap32(entry_1);
1036 lp[1] = tswap32(entry_2);
1037 return 0;
1038}
1039
1040/* specific and weird i386 syscalls */
bellard5cd43932003-03-29 16:54:36 +00001041int do_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00001042{
1043 int ret = -ENOSYS;
1044
1045 switch (func) {
1046 case 0:
1047 ret = read_ldt(ptr, bytecount);
1048 break;
1049 case 1:
1050 ret = write_ldt(env, ptr, bytecount, 1);
1051 break;
1052 case 0x11:
1053 ret = write_ldt(env, ptr, bytecount, 0);
1054 break;
1055 }
1056 return ret;
1057}
bellard1b6b0292003-03-22 17:31:38 +00001058
bellard5cd43932003-03-29 16:54:36 +00001059/* vm86 emulation */
1060
1061#define SAFE_MASK (0xDD5)
1062
1063int do_vm86(CPUX86State *env, long subfunction,
1064 struct target_vm86plus_struct * target_v86)
1065{
1066 TaskState *ts = env->opaque;
1067 int ret;
1068
1069 switch (subfunction) {
1070 case TARGET_VM86_REQUEST_IRQ:
1071 case TARGET_VM86_FREE_IRQ:
1072 case TARGET_VM86_GET_IRQ_BITS:
1073 case TARGET_VM86_GET_AND_RESET_IRQ:
1074 gemu_log("qemu: unsupported vm86 subfunction (%ld)\n", subfunction);
1075 ret = -EINVAL;
1076 goto out;
1077 case TARGET_VM86_PLUS_INSTALL_CHECK:
1078 /* NOTE: on old vm86 stuff this will return the error
1079 from verify_area(), because the subfunction is
1080 interpreted as (invalid) address to vm86_struct.
1081 So the installation check works.
1082 */
1083 ret = 0;
1084 goto out;
1085 }
1086
1087 ts->target_v86 = target_v86;
bellard5cd43932003-03-29 16:54:36 +00001088 /* save current CPU regs */
1089 ts->vm86_saved_regs.eax = 0; /* default vm86 syscall return code */
1090 ts->vm86_saved_regs.ebx = env->regs[R_EBX];
1091 ts->vm86_saved_regs.ecx = env->regs[R_ECX];
1092 ts->vm86_saved_regs.edx = env->regs[R_EDX];
1093 ts->vm86_saved_regs.esi = env->regs[R_ESI];
1094 ts->vm86_saved_regs.edi = env->regs[R_EDI];
1095 ts->vm86_saved_regs.ebp = env->regs[R_EBP];
1096 ts->vm86_saved_regs.esp = env->regs[R_ESP];
1097 ts->vm86_saved_regs.eflags = env->eflags;
1098 ts->vm86_saved_regs.eip = env->eip;
1099 ts->vm86_saved_regs.cs = env->segs[R_CS];
1100 ts->vm86_saved_regs.ss = env->segs[R_SS];
1101 ts->vm86_saved_regs.ds = env->segs[R_DS];
1102 ts->vm86_saved_regs.es = env->segs[R_ES];
1103 ts->vm86_saved_regs.fs = env->segs[R_FS];
1104 ts->vm86_saved_regs.gs = env->segs[R_GS];
1105
1106 /* build vm86 CPU state */
1107 env->eflags = (env->eflags & ~SAFE_MASK) |
1108 (tswap32(target_v86->regs.eflags) & SAFE_MASK) | VM_MASK;
1109
1110 env->regs[R_EBX] = tswap32(target_v86->regs.ebx);
1111 env->regs[R_ECX] = tswap32(target_v86->regs.ecx);
1112 env->regs[R_EDX] = tswap32(target_v86->regs.edx);
1113 env->regs[R_ESI] = tswap32(target_v86->regs.esi);
1114 env->regs[R_EDI] = tswap32(target_v86->regs.edi);
1115 env->regs[R_EBP] = tswap32(target_v86->regs.ebp);
1116 env->regs[R_ESP] = tswap32(target_v86->regs.esp);
1117 env->eip = tswap32(target_v86->regs.eip);
1118 cpu_x86_load_seg(env, R_CS, tswap16(target_v86->regs.cs));
1119 cpu_x86_load_seg(env, R_SS, tswap16(target_v86->regs.ss));
1120 cpu_x86_load_seg(env, R_DS, tswap16(target_v86->regs.ds));
1121 cpu_x86_load_seg(env, R_ES, tswap16(target_v86->regs.es));
1122 cpu_x86_load_seg(env, R_FS, tswap16(target_v86->regs.fs));
1123 cpu_x86_load_seg(env, R_GS, tswap16(target_v86->regs.gs));
1124 ret = tswap32(target_v86->regs.eax); /* eax will be restored at
1125 the end of the syscall */
1126 /* now the virtual CPU is ready for vm86 execution ! */
1127 out:
1128 return ret;
1129}
1130
bellard1b6b0292003-03-22 17:31:38 +00001131/* this stack is the equivalent of the kernel stack associated with a
1132 thread/process */
1133#define NEW_STACK_SIZE 8192
1134
1135static int clone_func(void *arg)
1136{
1137 CPUX86State *env = arg;
1138 cpu_loop(env);
1139 /* never exits */
1140 return 0;
1141}
1142
1143int do_fork(CPUX86State *env, unsigned int flags, unsigned long newsp)
1144{
1145 int ret;
bellard5cd43932003-03-29 16:54:36 +00001146 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00001147 uint8_t *new_stack;
1148 CPUX86State *new_env;
1149
1150 if (flags & CLONE_VM) {
1151 if (!newsp)
1152 newsp = env->regs[R_ESP];
bellard5cd43932003-03-29 16:54:36 +00001153 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
1154 memset(ts, 0, sizeof(TaskState));
1155 new_stack = ts->stack;
1156 ts->used = 1;
1157 /* add in task state list */
1158 ts->next = first_task_state;
1159 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00001160 /* we create a new CPU instance. */
1161 new_env = cpu_x86_init();
1162 memcpy(new_env, env, sizeof(CPUX86State));
1163 new_env->regs[R_ESP] = newsp;
1164 new_env->regs[R_EAX] = 0;
bellard5cd43932003-03-29 16:54:36 +00001165 new_env->opaque = ts;
bellard1b6b0292003-03-22 17:31:38 +00001166 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
1167 } else {
1168 /* if no CLONE_VM, we consider it is a fork */
1169 if ((flags & ~CSIGNAL) != 0)
1170 return -EINVAL;
1171 ret = fork();
1172 }
1173 return ret;
1174}
1175
bellard6dbad632003-03-16 18:05:05 +00001176#endif
1177
bellardb03c60f2003-03-23 17:19:56 +00001178#define high2lowuid(x) (x)
1179#define high2lowgid(x) (x)
1180#define low2highuid(x) (x)
1181#define low2highgid(x) (x)
bellard1b6b0292003-03-22 17:31:38 +00001182
bellard31e31b82003-02-18 22:55:36 +00001183void syscall_init(void)
1184{
1185#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
1186#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
1187#include "syscall_types.h"
1188#undef STRUCT
1189#undef STRUCT_SPECIAL
1190}
1191
bellard6dbad632003-03-16 18:05:05 +00001192long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
bellard31e31b82003-02-18 22:55:36 +00001193 long arg4, long arg5, long arg6)
1194{
1195 long ret;
1196 struct stat st;
bellard72f03902003-02-18 23:33:18 +00001197 struct kernel_statfs *stfs;
bellard31e31b82003-02-18 22:55:36 +00001198
bellard72f03902003-02-18 23:33:18 +00001199#ifdef DEBUG
1200 gemu_log("syscall %d\n", num);
1201#endif
bellard31e31b82003-02-18 22:55:36 +00001202 switch(num) {
1203 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00001204#ifdef HAVE_GPROF
1205 _mcleanup();
1206#endif
bellard1b6b0292003-03-22 17:31:38 +00001207 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00001208 _exit(arg1);
1209 ret = 0; /* avoid warning */
1210 break;
1211 case TARGET_NR_read:
1212 ret = get_errno(read(arg1, (void *)arg2, arg3));
1213 break;
1214 case TARGET_NR_write:
1215 ret = get_errno(write(arg1, (void *)arg2, arg3));
1216 break;
1217 case TARGET_NR_open:
bellardec86b0f2003-04-11 00:15:04 +00001218 ret = get_errno(open(path((const char *)arg1), arg2, arg3));
bellard31e31b82003-02-18 22:55:36 +00001219 break;
1220 case TARGET_NR_close:
1221 ret = get_errno(close(arg1));
1222 break;
1223 case TARGET_NR_brk:
1224 ret = do_brk((char *)arg1);
1225 break;
1226 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00001227 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00001228 break;
1229 case TARGET_NR_waitpid:
1230 {
1231 int *status = (int *)arg2;
1232 ret = get_errno(waitpid(arg1, status, arg3));
1233 if (!is_error(ret) && status)
1234 tswapls((long *)&status);
1235 }
1236 break;
1237 case TARGET_NR_creat:
1238 ret = get_errno(creat((const char *)arg1, arg2));
1239 break;
1240 case TARGET_NR_link:
1241 ret = get_errno(link((const char *)arg1, (const char *)arg2));
1242 break;
1243 case TARGET_NR_unlink:
1244 ret = get_errno(unlink((const char *)arg1));
1245 break;
1246 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00001247 {
1248 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00001249 int argc, envc;
bellard7854b052003-03-29 17:22:23 +00001250 uint32_t *p;
1251 char **q;
1252
bellardf7341ff2003-03-30 21:00:25 +00001253 argc = 0;
bellard7854b052003-03-29 17:22:23 +00001254 for (p = (void *)arg2; *p; p++)
1255 argc++;
bellardf7341ff2003-03-30 21:00:25 +00001256 envc = 0;
bellard7854b052003-03-29 17:22:23 +00001257 for (p = (void *)arg3; *p; p++)
1258 envc++;
1259
bellardf7341ff2003-03-30 21:00:25 +00001260 argp = alloca((argc + 1) * sizeof(void *));
1261 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00001262
1263 for (p = (void *)arg2, q = argp; *p; p++, q++)
1264 *q = (void *)tswap32(*p);
bellardf7341ff2003-03-30 21:00:25 +00001265 *q = NULL;
1266
bellard7854b052003-03-29 17:22:23 +00001267 for (p = (void *)arg3, q = envp; *p; p++, q++)
1268 *q = (void *)tswap32(*p);
bellardf7341ff2003-03-30 21:00:25 +00001269 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00001270
1271 ret = get_errno(execve((const char *)arg1, argp, envp));
1272 }
bellard31e31b82003-02-18 22:55:36 +00001273 break;
1274 case TARGET_NR_chdir:
1275 ret = get_errno(chdir((const char *)arg1));
1276 break;
1277 case TARGET_NR_time:
1278 {
1279 int *time_ptr = (int *)arg1;
1280 ret = get_errno(time((time_t *)time_ptr));
1281 if (!is_error(ret) && time_ptr)
1282 tswap32s(time_ptr);
1283 }
1284 break;
1285 case TARGET_NR_mknod:
1286 ret = get_errno(mknod((const char *)arg1, arg2, arg3));
1287 break;
1288 case TARGET_NR_chmod:
1289 ret = get_errno(chmod((const char *)arg1, arg2));
1290 break;
1291 case TARGET_NR_lchown:
1292 ret = get_errno(chown((const char *)arg1, arg2, arg3));
1293 break;
1294 case TARGET_NR_break:
1295 goto unimplemented;
1296 case TARGET_NR_oldstat:
1297 goto unimplemented;
1298 case TARGET_NR_lseek:
1299 ret = get_errno(lseek(arg1, arg2, arg3));
1300 break;
1301 case TARGET_NR_getpid:
1302 ret = get_errno(getpid());
1303 break;
1304 case TARGET_NR_mount:
1305 /* need to look at the data field */
1306 goto unimplemented;
1307 case TARGET_NR_umount:
1308 ret = get_errno(umount((const char *)arg1));
1309 break;
1310 case TARGET_NR_setuid:
bellardb03c60f2003-03-23 17:19:56 +00001311 ret = get_errno(setuid(low2highuid(arg1)));
bellard31e31b82003-02-18 22:55:36 +00001312 break;
1313 case TARGET_NR_getuid:
1314 ret = get_errno(getuid());
1315 break;
1316 case TARGET_NR_stime:
1317 {
1318 int *time_ptr = (int *)arg1;
1319 if (time_ptr)
1320 tswap32s(time_ptr);
1321 ret = get_errno(stime((time_t *)time_ptr));
1322 }
1323 break;
1324 case TARGET_NR_ptrace:
1325 goto unimplemented;
1326 case TARGET_NR_alarm:
1327 ret = alarm(arg1);
1328 break;
1329 case TARGET_NR_oldfstat:
1330 goto unimplemented;
1331 case TARGET_NR_pause:
1332 ret = get_errno(pause());
1333 break;
1334 case TARGET_NR_utime:
1335 goto unimplemented;
1336 case TARGET_NR_stty:
1337 goto unimplemented;
1338 case TARGET_NR_gtty:
1339 goto unimplemented;
1340 case TARGET_NR_access:
1341 ret = get_errno(access((const char *)arg1, arg2));
1342 break;
1343 case TARGET_NR_nice:
1344 ret = get_errno(nice(arg1));
1345 break;
1346 case TARGET_NR_ftime:
1347 goto unimplemented;
1348 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00001349 sync();
1350 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00001351 break;
1352 case TARGET_NR_kill:
1353 ret = get_errno(kill(arg1, arg2));
1354 break;
1355 case TARGET_NR_rename:
1356 ret = get_errno(rename((const char *)arg1, (const char *)arg2));
1357 break;
1358 case TARGET_NR_mkdir:
1359 ret = get_errno(mkdir((const char *)arg1, arg2));
1360 break;
1361 case TARGET_NR_rmdir:
1362 ret = get_errno(rmdir((const char *)arg1));
1363 break;
1364 case TARGET_NR_dup:
1365 ret = get_errno(dup(arg1));
1366 break;
1367 case TARGET_NR_pipe:
1368 {
1369 int *pipe_ptr = (int *)arg1;
1370 ret = get_errno(pipe(pipe_ptr));
1371 if (!is_error(ret)) {
1372 tswap32s(&pipe_ptr[0]);
1373 tswap32s(&pipe_ptr[1]);
1374 }
1375 }
1376 break;
1377 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00001378 {
1379 struct target_tms *tmsp = (void *)arg1;
1380 struct tms tms;
1381 ret = get_errno(times(&tms));
1382 if (tmsp) {
1383 tmsp->tms_utime = tswapl(tms.tms_utime);
1384 tmsp->tms_stime = tswapl(tms.tms_stime);
1385 tmsp->tms_cutime = tswapl(tms.tms_cutime);
1386 tmsp->tms_cstime = tswapl(tms.tms_cstime);
1387 }
1388 }
1389 break;
bellard31e31b82003-02-18 22:55:36 +00001390 case TARGET_NR_prof:
1391 goto unimplemented;
1392 case TARGET_NR_setgid:
bellardb03c60f2003-03-23 17:19:56 +00001393 ret = get_errno(setgid(low2highgid(arg1)));
bellard31e31b82003-02-18 22:55:36 +00001394 break;
1395 case TARGET_NR_getgid:
1396 ret = get_errno(getgid());
1397 break;
1398 case TARGET_NR_signal:
1399 goto unimplemented;
1400 case TARGET_NR_geteuid:
1401 ret = get_errno(geteuid());
1402 break;
1403 case TARGET_NR_getegid:
1404 ret = get_errno(getegid());
1405 break;
1406 case TARGET_NR_acct:
1407 goto unimplemented;
1408 case TARGET_NR_umount2:
1409 ret = get_errno(umount2((const char *)arg1, arg2));
1410 break;
1411 case TARGET_NR_lock:
1412 goto unimplemented;
1413 case TARGET_NR_ioctl:
1414 ret = do_ioctl(arg1, arg2, arg3);
1415 break;
1416 case TARGET_NR_fcntl:
1417 switch(arg2) {
1418 case F_GETLK:
1419 case F_SETLK:
1420 case F_SETLKW:
1421 goto unimplemented;
1422 default:
1423 ret = get_errno(fcntl(arg1, arg2, arg3));
1424 break;
1425 }
1426 break;
1427 case TARGET_NR_mpx:
1428 goto unimplemented;
1429 case TARGET_NR_setpgid:
1430 ret = get_errno(setpgid(arg1, arg2));
1431 break;
1432 case TARGET_NR_ulimit:
1433 goto unimplemented;
1434 case TARGET_NR_oldolduname:
1435 goto unimplemented;
1436 case TARGET_NR_umask:
1437 ret = get_errno(umask(arg1));
1438 break;
1439 case TARGET_NR_chroot:
1440 ret = get_errno(chroot((const char *)arg1));
1441 break;
1442 case TARGET_NR_ustat:
1443 goto unimplemented;
1444 case TARGET_NR_dup2:
1445 ret = get_errno(dup2(arg1, arg2));
1446 break;
1447 case TARGET_NR_getppid:
1448 ret = get_errno(getppid());
1449 break;
1450 case TARGET_NR_getpgrp:
1451 ret = get_errno(getpgrp());
1452 break;
1453 case TARGET_NR_setsid:
1454 ret = get_errno(setsid());
1455 break;
1456 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00001457 {
bellard66fb9762003-03-23 01:06:05 +00001458 struct target_old_sigaction *old_act = (void *)arg2;
1459 struct target_old_sigaction *old_oact = (void *)arg3;
1460 struct target_sigaction act, oact, *pact;
1461 if (old_act) {
1462 act._sa_handler = old_act->_sa_handler;
1463 target_siginitset(&act.sa_mask, old_act->sa_mask);
1464 act.sa_flags = old_act->sa_flags;
1465 act.sa_restorer = old_act->sa_restorer;
1466 pact = &act;
1467 } else {
1468 pact = NULL;
1469 }
1470 ret = get_errno(do_sigaction(arg1, pact, &oact));
1471 if (!is_error(ret) && old_oact) {
1472 old_oact->_sa_handler = oact._sa_handler;
1473 old_oact->sa_mask = oact.sa_mask.sig[0];
1474 old_oact->sa_flags = oact.sa_flags;
1475 old_oact->sa_restorer = oact.sa_restorer;
1476 }
bellard31e31b82003-02-18 22:55:36 +00001477 }
1478 break;
bellard66fb9762003-03-23 01:06:05 +00001479 case TARGET_NR_rt_sigaction:
1480 ret = get_errno(do_sigaction(arg1, (void *)arg2, (void *)arg3));
1481 break;
bellard31e31b82003-02-18 22:55:36 +00001482 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00001483 {
1484 sigset_t cur_set;
1485 target_ulong target_set;
1486 sigprocmask(0, NULL, &cur_set);
1487 host_to_target_old_sigset(&target_set, &cur_set);
1488 ret = target_set;
1489 }
1490 break;
bellard31e31b82003-02-18 22:55:36 +00001491 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00001492 {
1493 sigset_t set, oset, cur_set;
1494 target_ulong target_set = arg1;
1495 sigprocmask(0, NULL, &cur_set);
1496 target_to_host_old_sigset(&set, &target_set);
1497 sigorset(&set, &set, &cur_set);
1498 sigprocmask(SIG_SETMASK, &set, &oset);
1499 host_to_target_old_sigset(&target_set, &oset);
1500 ret = target_set;
1501 }
1502 break;
1503 case TARGET_NR_sigprocmask:
1504 {
1505 int how = arg1;
1506 sigset_t set, oldset, *set_ptr;
1507 target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
1508
1509 if (pset) {
1510 switch(how) {
1511 case TARGET_SIG_BLOCK:
1512 how = SIG_BLOCK;
1513 break;
1514 case TARGET_SIG_UNBLOCK:
1515 how = SIG_UNBLOCK;
1516 break;
1517 case TARGET_SIG_SETMASK:
1518 how = SIG_SETMASK;
1519 break;
1520 default:
1521 ret = -EINVAL;
1522 goto fail;
1523 }
1524 target_to_host_old_sigset(&set, pset);
1525 set_ptr = &set;
1526 } else {
1527 how = 0;
1528 set_ptr = NULL;
1529 }
1530 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
1531 if (!is_error(ret) && poldset) {
1532 host_to_target_old_sigset(poldset, &oldset);
1533 }
1534 }
1535 break;
1536 case TARGET_NR_rt_sigprocmask:
1537 {
1538 int how = arg1;
1539 sigset_t set, oldset, *set_ptr;
1540 target_sigset_t *pset = (void *)arg2;
1541 target_sigset_t *poldset = (void *)arg3;
1542
1543 if (pset) {
1544 switch(how) {
1545 case TARGET_SIG_BLOCK:
1546 how = SIG_BLOCK;
1547 break;
1548 case TARGET_SIG_UNBLOCK:
1549 how = SIG_UNBLOCK;
1550 break;
1551 case TARGET_SIG_SETMASK:
1552 how = SIG_SETMASK;
1553 break;
1554 default:
1555 ret = -EINVAL;
1556 goto fail;
1557 }
1558 target_to_host_sigset(&set, pset);
1559 set_ptr = &set;
1560 } else {
1561 how = 0;
1562 set_ptr = NULL;
1563 }
1564 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
1565 if (!is_error(ret) && poldset) {
1566 host_to_target_sigset(poldset, &oldset);
1567 }
1568 }
1569 break;
1570 case TARGET_NR_sigpending:
1571 {
1572 sigset_t set;
1573 ret = get_errno(sigpending(&set));
1574 if (!is_error(ret)) {
1575 host_to_target_old_sigset((target_ulong *)arg1, &set);
1576 }
1577 }
1578 break;
1579 case TARGET_NR_rt_sigpending:
1580 {
1581 sigset_t set;
1582 ret = get_errno(sigpending(&set));
1583 if (!is_error(ret)) {
1584 host_to_target_sigset((target_sigset_t *)arg1, &set);
1585 }
1586 }
1587 break;
1588 case TARGET_NR_sigsuspend:
1589 {
1590 sigset_t set;
1591 target_to_host_old_sigset(&set, (target_ulong *)arg1);
1592 ret = get_errno(sigsuspend(&set));
1593 }
1594 break;
1595 case TARGET_NR_rt_sigsuspend:
1596 {
1597 sigset_t set;
1598 target_to_host_sigset(&set, (target_sigset_t *)arg1);
1599 ret = get_errno(sigsuspend(&set));
1600 }
1601 break;
1602 case TARGET_NR_rt_sigtimedwait:
1603 {
1604 target_sigset_t *target_set = (void *)arg1;
1605 target_siginfo_t *target_uinfo = (void *)arg2;
1606 struct target_timespec *target_uts = (void *)arg3;
1607 sigset_t set;
1608 struct timespec uts, *puts;
1609 siginfo_t uinfo;
1610
1611 target_to_host_sigset(&set, target_set);
1612 if (target_uts) {
1613 puts = &uts;
1614 puts->tv_sec = tswapl(target_uts->tv_sec);
1615 puts->tv_nsec = tswapl(target_uts->tv_nsec);
1616 } else {
1617 puts = NULL;
1618 }
1619 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
1620 if (!is_error(ret) && target_uinfo) {
1621 host_to_target_siginfo(target_uinfo, &uinfo);
1622 }
1623 }
1624 break;
1625 case TARGET_NR_rt_sigqueueinfo:
1626 {
1627 siginfo_t uinfo;
1628 target_to_host_siginfo(&uinfo, (target_siginfo_t *)arg3);
1629 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
1630 }
1631 break;
1632 case TARGET_NR_sigreturn:
1633 /* NOTE: ret is eax, so not transcoding must be done */
1634 ret = do_sigreturn(cpu_env);
1635 break;
1636 case TARGET_NR_rt_sigreturn:
1637 /* NOTE: ret is eax, so not transcoding must be done */
1638 ret = do_rt_sigreturn(cpu_env);
1639 break;
bellard31e31b82003-02-18 22:55:36 +00001640 case TARGET_NR_setreuid:
1641 ret = get_errno(setreuid(arg1, arg2));
1642 break;
1643 case TARGET_NR_setregid:
1644 ret = get_errno(setregid(arg1, arg2));
1645 break;
bellard31e31b82003-02-18 22:55:36 +00001646 case TARGET_NR_sethostname:
1647 ret = get_errno(sethostname((const char *)arg1, arg2));
1648 break;
1649 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00001650 {
1651 /* XXX: convert resource ? */
1652 int resource = arg1;
1653 struct target_rlimit *target_rlim = (void *)arg2;
1654 struct rlimit rlim;
1655 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
1656 rlim.rlim_max = tswapl(target_rlim->rlim_max);
1657 ret = get_errno(setrlimit(resource, &rlim));
1658 }
1659 break;
bellard31e31b82003-02-18 22:55:36 +00001660 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00001661 {
1662 /* XXX: convert resource ? */
1663 int resource = arg1;
1664 struct target_rlimit *target_rlim = (void *)arg2;
1665 struct rlimit rlim;
1666
1667 ret = get_errno(getrlimit(resource, &rlim));
1668 if (!is_error(ret)) {
1669 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
1670 target_rlim->rlim_max = tswapl(rlim.rlim_max);
1671 }
1672 }
1673 break;
bellard31e31b82003-02-18 22:55:36 +00001674 case TARGET_NR_getrusage:
1675 goto unimplemented;
1676 case TARGET_NR_gettimeofday:
1677 {
1678 struct target_timeval *target_tv = (void *)arg1;
1679 struct timeval tv;
1680 ret = get_errno(gettimeofday(&tv, NULL));
1681 if (!is_error(ret)) {
bellard5cd43932003-03-29 16:54:36 +00001682 host_to_target_timeval(target_tv, &tv);
bellard31e31b82003-02-18 22:55:36 +00001683 }
1684 }
1685 break;
1686 case TARGET_NR_settimeofday:
1687 {
1688 struct target_timeval *target_tv = (void *)arg1;
1689 struct timeval tv;
bellard5cd43932003-03-29 16:54:36 +00001690 target_to_host_timeval(&tv, target_tv);
bellard31e31b82003-02-18 22:55:36 +00001691 ret = get_errno(settimeofday(&tv, NULL));
1692 }
1693 break;
1694 case TARGET_NR_getgroups:
1695 goto unimplemented;
1696 case TARGET_NR_setgroups:
1697 goto unimplemented;
1698 case TARGET_NR_select:
1699 goto unimplemented;
1700 case TARGET_NR_symlink:
1701 ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
1702 break;
1703 case TARGET_NR_oldlstat:
1704 goto unimplemented;
1705 case TARGET_NR_readlink:
bellardec86b0f2003-04-11 00:15:04 +00001706 ret = get_errno(readlink(path((const char *)arg1), (char *)arg2, arg3));
bellard31e31b82003-02-18 22:55:36 +00001707 break;
1708 case TARGET_NR_uselib:
1709 goto unimplemented;
1710 case TARGET_NR_swapon:
1711 ret = get_errno(swapon((const char *)arg1, arg2));
1712 break;
1713 case TARGET_NR_reboot:
1714 goto unimplemented;
1715 case TARGET_NR_readdir:
1716 goto unimplemented;
1717#ifdef TARGET_I386
1718 case TARGET_NR_mmap:
1719 {
1720 uint32_t v1, v2, v3, v4, v5, v6, *vptr;
1721 vptr = (uint32_t *)arg1;
1722 v1 = tswap32(vptr[0]);
1723 v2 = tswap32(vptr[1]);
1724 v3 = tswap32(vptr[2]);
1725 v4 = tswap32(vptr[3]);
1726 v5 = tswap32(vptr[4]);
1727 v6 = tswap32(vptr[5]);
1728 ret = get_errno((long)mmap((void *)v1, v2, v3, v4, v5, v6));
1729 }
1730 break;
1731#endif
1732#ifdef TARGET_I386
1733 case TARGET_NR_mmap2:
1734#else
1735 case TARGET_NR_mmap:
1736#endif
1737 ret = get_errno((long)mmap((void *)arg1, arg2, arg3, arg4, arg5, arg6));
1738 break;
1739 case TARGET_NR_munmap:
1740 ret = get_errno(munmap((void *)arg1, arg2));
1741 break;
bellard9de5e442003-03-23 16:49:39 +00001742 case TARGET_NR_mprotect:
1743 ret = get_errno(mprotect((void *)arg1, arg2, arg3));
1744 break;
1745 case TARGET_NR_mremap:
1746 ret = get_errno((long)mremap((void *)arg1, arg2, arg3, arg4));
1747 break;
1748 case TARGET_NR_msync:
1749 ret = get_errno(msync((void *)arg1, arg2, arg3));
1750 break;
1751 case TARGET_NR_mlock:
1752 ret = get_errno(mlock((void *)arg1, arg2));
1753 break;
1754 case TARGET_NR_munlock:
1755 ret = get_errno(munlock((void *)arg1, arg2));
1756 break;
1757 case TARGET_NR_mlockall:
1758 ret = get_errno(mlockall(arg1));
1759 break;
1760 case TARGET_NR_munlockall:
1761 ret = get_errno(munlockall());
1762 break;
bellard31e31b82003-02-18 22:55:36 +00001763 case TARGET_NR_truncate:
1764 ret = get_errno(truncate((const char *)arg1, arg2));
1765 break;
1766 case TARGET_NR_ftruncate:
1767 ret = get_errno(ftruncate(arg1, arg2));
1768 break;
1769 case TARGET_NR_fchmod:
1770 ret = get_errno(fchmod(arg1, arg2));
1771 break;
1772 case TARGET_NR_fchown:
1773 ret = get_errno(fchown(arg1, arg2, arg3));
1774 break;
1775 case TARGET_NR_getpriority:
1776 ret = get_errno(getpriority(arg1, arg2));
1777 break;
1778 case TARGET_NR_setpriority:
1779 ret = get_errno(setpriority(arg1, arg2, arg3));
1780 break;
1781 case TARGET_NR_profil:
1782 goto unimplemented;
1783 case TARGET_NR_statfs:
1784 stfs = (void *)arg2;
bellardec86b0f2003-04-11 00:15:04 +00001785 ret = get_errno(sys_statfs(path((const char *)arg1), stfs));
bellard31e31b82003-02-18 22:55:36 +00001786 convert_statfs:
1787 if (!is_error(ret)) {
1788 tswap32s(&stfs->f_type);
1789 tswap32s(&stfs->f_bsize);
1790 tswap32s(&stfs->f_blocks);
1791 tswap32s(&stfs->f_bfree);
1792 tswap32s(&stfs->f_bavail);
1793 tswap32s(&stfs->f_files);
1794 tswap32s(&stfs->f_ffree);
1795 tswap32s(&stfs->f_fsid.val[0]);
1796 tswap32s(&stfs->f_fsid.val[1]);
1797 tswap32s(&stfs->f_namelen);
1798 }
1799 break;
1800 case TARGET_NR_fstatfs:
1801 stfs = (void *)arg2;
1802 ret = get_errno(sys_fstatfs(arg1, stfs));
1803 goto convert_statfs;
1804 case TARGET_NR_ioperm:
1805 goto unimplemented;
1806 case TARGET_NR_socketcall:
bellard7854b052003-03-29 17:22:23 +00001807 ret = do_socketcall(arg1, (int32_t *)arg2);
bellard31e31b82003-02-18 22:55:36 +00001808 break;
1809 case TARGET_NR_syslog:
1810 goto unimplemented;
1811 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00001812 {
1813 struct target_itimerval *target_value = (void *)arg2;
1814 struct target_itimerval *target_ovalue = (void *)arg3;
1815 struct itimerval value, ovalue, *pvalue;
1816
1817 if (target_value) {
1818 pvalue = &value;
1819 target_to_host_timeval(&pvalue->it_interval,
1820 &target_value->it_interval);
1821 target_to_host_timeval(&pvalue->it_value,
1822 &target_value->it_value);
1823 } else {
1824 pvalue = NULL;
1825 }
1826 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
1827 if (!is_error(ret) && target_ovalue) {
1828 host_to_target_timeval(&target_ovalue->it_interval,
1829 &ovalue.it_interval);
1830 host_to_target_timeval(&target_ovalue->it_value,
1831 &ovalue.it_value);
1832 }
1833 }
1834 break;
bellard31e31b82003-02-18 22:55:36 +00001835 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00001836 {
1837 struct target_itimerval *target_value = (void *)arg2;
1838 struct itimerval value;
1839
1840 ret = get_errno(getitimer(arg1, &value));
1841 if (!is_error(ret) && target_value) {
1842 host_to_target_timeval(&target_value->it_interval,
1843 &value.it_interval);
1844 host_to_target_timeval(&target_value->it_value,
1845 &value.it_value);
1846 }
1847 }
1848 break;
bellard31e31b82003-02-18 22:55:36 +00001849 case TARGET_NR_stat:
bellardec86b0f2003-04-11 00:15:04 +00001850 ret = get_errno(stat(path((const char *)arg1), &st));
bellard31e31b82003-02-18 22:55:36 +00001851 goto do_stat;
1852 case TARGET_NR_lstat:
bellardec86b0f2003-04-11 00:15:04 +00001853 ret = get_errno(lstat(path((const char *)arg1), &st));
bellard31e31b82003-02-18 22:55:36 +00001854 goto do_stat;
1855 case TARGET_NR_fstat:
1856 {
1857 ret = get_errno(fstat(arg1, &st));
1858 do_stat:
1859 if (!is_error(ret)) {
1860 struct target_stat *target_st = (void *)arg2;
1861 target_st->st_dev = tswap16(st.st_dev);
1862 target_st->st_ino = tswapl(st.st_ino);
bellardec86b0f2003-04-11 00:15:04 +00001863 target_st->st_mode = tswap16(st.st_mode);
bellard31e31b82003-02-18 22:55:36 +00001864 target_st->st_nlink = tswap16(st.st_nlink);
1865 target_st->st_uid = tswap16(st.st_uid);
1866 target_st->st_gid = tswap16(st.st_gid);
1867 target_st->st_rdev = tswap16(st.st_rdev);
1868 target_st->st_size = tswapl(st.st_size);
1869 target_st->st_blksize = tswapl(st.st_blksize);
1870 target_st->st_blocks = tswapl(st.st_blocks);
bellard7854b052003-03-29 17:22:23 +00001871 target_st->target_st_atime = tswapl(st.st_atime);
1872 target_st->target_st_mtime = tswapl(st.st_mtime);
1873 target_st->target_st_ctime = tswapl(st.st_ctime);
bellard31e31b82003-02-18 22:55:36 +00001874 }
1875 }
1876 break;
1877 case TARGET_NR_olduname:
1878 goto unimplemented;
1879 case TARGET_NR_iopl:
1880 goto unimplemented;
1881 case TARGET_NR_vhangup:
1882 ret = get_errno(vhangup());
1883 break;
1884 case TARGET_NR_idle:
1885 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00001886 case TARGET_NR_wait4:
1887 {
1888 int status;
1889 target_long *status_ptr = (void *)arg2;
1890 struct rusage rusage, *rusage_ptr;
1891 struct target_rusage *target_rusage = (void *)arg4;
1892 if (target_rusage)
1893 rusage_ptr = &rusage;
1894 else
1895 rusage_ptr = NULL;
1896 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
1897 if (!is_error(ret)) {
1898 if (status_ptr)
1899 *status_ptr = tswap32(status);
1900 if (target_rusage) {
1901 target_rusage->ru_utime.tv_sec = tswapl(rusage.ru_utime.tv_sec);
1902 target_rusage->ru_utime.tv_usec = tswapl(rusage.ru_utime.tv_usec);
1903 target_rusage->ru_stime.tv_sec = tswapl(rusage.ru_stime.tv_sec);
1904 target_rusage->ru_stime.tv_usec = tswapl(rusage.ru_stime.tv_usec);
1905 target_rusage->ru_maxrss = tswapl(rusage.ru_maxrss);
1906 target_rusage->ru_ixrss = tswapl(rusage.ru_ixrss);
1907 target_rusage->ru_idrss = tswapl(rusage.ru_idrss);
1908 target_rusage->ru_isrss = tswapl(rusage.ru_isrss);
1909 target_rusage->ru_minflt = tswapl(rusage.ru_minflt);
1910 target_rusage->ru_majflt = tswapl(rusage.ru_majflt);
1911 target_rusage->ru_nswap = tswapl(rusage.ru_nswap);
1912 target_rusage->ru_inblock = tswapl(rusage.ru_inblock);
1913 target_rusage->ru_oublock = tswapl(rusage.ru_oublock);
1914 target_rusage->ru_msgsnd = tswapl(rusage.ru_msgsnd);
1915 target_rusage->ru_msgrcv = tswapl(rusage.ru_msgrcv);
1916 target_rusage->ru_nsignals = tswapl(rusage.ru_nsignals);
1917 target_rusage->ru_nvcsw = tswapl(rusage.ru_nvcsw);
1918 target_rusage->ru_nivcsw = tswapl(rusage.ru_nivcsw);
1919 }
1920 }
1921 }
1922 break;
1923 case TARGET_NR_swapoff:
1924 ret = get_errno(swapoff((const char *)arg1));
1925 break;
1926 case TARGET_NR_sysinfo:
1927 goto unimplemented;
1928 case TARGET_NR_ipc:
1929 goto unimplemented;
1930 case TARGET_NR_fsync:
1931 ret = get_errno(fsync(arg1));
1932 break;
bellard31e31b82003-02-18 22:55:36 +00001933 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00001934 ret = get_errno(do_fork(cpu_env, arg1, arg2));
1935 break;
bellardec86b0f2003-04-11 00:15:04 +00001936#ifdef __NR_exit_group
1937 /* new thread calls */
1938 case TARGET_NR_exit_group:
1939 ret = get_errno(exit_group(arg1));
1940 break;
1941#endif
bellard31e31b82003-02-18 22:55:36 +00001942 case TARGET_NR_setdomainname:
1943 ret = get_errno(setdomainname((const char *)arg1, arg2));
1944 break;
1945 case TARGET_NR_uname:
1946 /* no need to transcode because we use the linux syscall */
1947 ret = get_errno(sys_uname((struct new_utsname *)arg1));
1948 break;
bellard6dbad632003-03-16 18:05:05 +00001949#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00001950 case TARGET_NR_modify_ldt:
bellard5cd43932003-03-29 16:54:36 +00001951 ret = get_errno(do_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
1952 break;
1953 case TARGET_NR_vm86old:
1954 goto unimplemented;
1955 case TARGET_NR_vm86:
1956 ret = do_vm86(cpu_env, arg1, (void *)arg2);
bellard6dbad632003-03-16 18:05:05 +00001957 break;
1958#endif
bellard31e31b82003-02-18 22:55:36 +00001959 case TARGET_NR_adjtimex:
1960 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00001961 case TARGET_NR_create_module:
1962 case TARGET_NR_init_module:
1963 case TARGET_NR_delete_module:
1964 case TARGET_NR_get_kernel_syms:
1965 goto unimplemented;
1966 case TARGET_NR_quotactl:
1967 goto unimplemented;
1968 case TARGET_NR_getpgid:
1969 ret = get_errno(getpgid(arg1));
1970 break;
1971 case TARGET_NR_fchdir:
1972 ret = get_errno(fchdir(arg1));
1973 break;
1974 case TARGET_NR_bdflush:
1975 goto unimplemented;
1976 case TARGET_NR_sysfs:
1977 goto unimplemented;
1978 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00001979 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00001980 break;
1981 case TARGET_NR_afs_syscall:
1982 goto unimplemented;
1983 case TARGET_NR_setfsuid:
bellard9de5e442003-03-23 16:49:39 +00001984 ret = get_errno(setfsuid(arg1));
1985 break;
bellard31e31b82003-02-18 22:55:36 +00001986 case TARGET_NR_setfsgid:
bellard9de5e442003-03-23 16:49:39 +00001987 ret = get_errno(setfsgid(arg1));
1988 break;
bellard31e31b82003-02-18 22:55:36 +00001989 case TARGET_NR__llseek:
1990 {
1991 int64_t res;
1992 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
1993 *(int64_t *)arg4 = tswap64(res);
1994 }
1995 break;
1996 case TARGET_NR_getdents:
1997#if TARGET_LONG_SIZE != 4
1998#error not supported
1999#endif
2000 {
2001 struct dirent *dirp = (void *)arg2;
2002 long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00002003
bellard72f03902003-02-18 23:33:18 +00002004 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00002005 if (!is_error(ret)) {
2006 struct dirent *de;
2007 int len = ret;
2008 int reclen;
2009 de = dirp;
2010 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00002011 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00002012 if (reclen > len)
2013 break;
bellard8083a3e2003-03-24 23:12:16 +00002014 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00002015 tswapls(&de->d_ino);
2016 tswapls(&de->d_off);
2017 de = (struct dirent *)((char *)de + reclen);
2018 len -= reclen;
2019 }
2020 }
2021 }
2022 break;
bellarddab2ed92003-03-22 15:23:14 +00002023 case TARGET_NR_getdents64:
2024 {
2025 struct dirent64 *dirp = (void *)arg2;
2026 long count = arg3;
2027 ret = get_errno(sys_getdents64(arg1, dirp, count));
2028 if (!is_error(ret)) {
2029 struct dirent64 *de;
2030 int len = ret;
2031 int reclen;
2032 de = dirp;
2033 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00002034 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00002035 if (reclen > len)
2036 break;
bellard8083a3e2003-03-24 23:12:16 +00002037 de->d_reclen = tswap16(reclen);
bellarddab2ed92003-03-22 15:23:14 +00002038 tswap64s(&de->d_ino);
2039 tswap64s(&de->d_off);
2040 de = (struct dirent64 *)((char *)de + reclen);
2041 len -= reclen;
2042 }
2043 }
2044 }
2045 break;
bellard31e31b82003-02-18 22:55:36 +00002046 case TARGET_NR__newselect:
2047 ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4,
2048 (void *)arg5);
2049 break;
bellard9de5e442003-03-23 16:49:39 +00002050 case TARGET_NR_poll:
2051 {
2052 struct target_pollfd *target_pfd = (void *)arg1;
2053 unsigned int nfds = arg2;
2054 int timeout = arg3;
2055 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00002056 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00002057
2058 pfd = alloca(sizeof(struct pollfd) * nfds);
2059 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00002060 pfd[i].fd = tswap32(target_pfd[i].fd);
2061 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00002062 }
2063 ret = get_errno(poll(pfd, nfds, timeout));
2064 if (!is_error(ret)) {
2065 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00002066 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00002067 }
2068 }
2069 }
2070 break;
bellard31e31b82003-02-18 22:55:36 +00002071 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00002072 /* NOTE: the flock constant seems to be the same for every
2073 Linux platform */
2074 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00002075 break;
2076 case TARGET_NR_readv:
2077 {
2078 int count = arg3;
2079 int i;
2080 struct iovec *vec;
2081 struct target_iovec *target_vec = (void *)arg2;
2082
2083 vec = alloca(count * sizeof(struct iovec));
2084 for(i = 0;i < count; i++) {
2085 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2086 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2087 }
2088 ret = get_errno(readv(arg1, vec, count));
2089 }
2090 break;
2091 case TARGET_NR_writev:
2092 {
2093 int count = arg3;
2094 int i;
2095 struct iovec *vec;
2096 struct target_iovec *target_vec = (void *)arg2;
2097
2098 vec = alloca(count * sizeof(struct iovec));
2099 for(i = 0;i < count; i++) {
2100 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
2101 vec[i].iov_len = tswapl(target_vec[i].iov_len);
2102 }
2103 ret = get_errno(writev(arg1, vec, count));
2104 }
2105 break;
2106 case TARGET_NR_getsid:
2107 ret = get_errno(getsid(arg1));
2108 break;
2109 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00002110 ret = get_errno(fdatasync(arg1));
2111 break;
bellard31e31b82003-02-18 22:55:36 +00002112 case TARGET_NR__sysctl:
2113 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002114 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00002115 {
2116 struct sched_param *target_schp = (void *)arg2;
2117 struct sched_param schp;
2118 schp.sched_priority = tswap32(target_schp->sched_priority);
2119 ret = get_errno(sched_setparam(arg1, &schp));
2120 }
2121 break;
bellard31e31b82003-02-18 22:55:36 +00002122 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00002123 {
2124 struct sched_param *target_schp = (void *)arg2;
2125 struct sched_param schp;
2126 ret = get_errno(sched_getparam(arg1, &schp));
2127 if (!is_error(ret)) {
2128 target_schp->sched_priority = tswap32(schp.sched_priority);
2129 }
2130 }
2131 break;
bellard31e31b82003-02-18 22:55:36 +00002132 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00002133 {
2134 struct sched_param *target_schp = (void *)arg3;
2135 struct sched_param schp;
2136 schp.sched_priority = tswap32(target_schp->sched_priority);
2137 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
2138 }
2139 break;
bellard31e31b82003-02-18 22:55:36 +00002140 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00002141 ret = get_errno(sched_getscheduler(arg1));
2142 break;
bellard31e31b82003-02-18 22:55:36 +00002143 case TARGET_NR_sched_yield:
2144 ret = get_errno(sched_yield());
2145 break;
2146 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00002147 ret = get_errno(sched_get_priority_max(arg1));
2148 break;
bellard31e31b82003-02-18 22:55:36 +00002149 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00002150 ret = get_errno(sched_get_priority_min(arg1));
2151 break;
bellard31e31b82003-02-18 22:55:36 +00002152 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00002153 {
2154 struct target_timespec *target_ts = (void *)arg2;
2155 struct timespec ts;
2156 ret = get_errno(sched_rr_get_interval(arg1, &ts));
2157 if (!is_error(ret)) {
2158 target_ts->tv_sec = tswapl(ts.tv_sec);
2159 target_ts->tv_nsec = tswapl(ts.tv_nsec);
2160 }
2161 }
2162 break;
bellard31e31b82003-02-18 22:55:36 +00002163 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00002164 {
2165 struct target_timespec *target_req = (void *)arg1;
2166 struct target_timespec *target_rem = (void *)arg2;
2167 struct timespec req, rem;
2168 req.tv_sec = tswapl(target_req->tv_sec);
2169 req.tv_nsec = tswapl(target_req->tv_nsec);
2170 ret = get_errno(nanosleep(&req, &rem));
2171 if (target_rem) {
2172 target_rem->tv_sec = tswapl(rem.tv_sec);
2173 target_rem->tv_nsec = tswapl(rem.tv_nsec);
2174 }
2175 }
2176 break;
bellard31e31b82003-02-18 22:55:36 +00002177 case TARGET_NR_setresuid:
bellardb03c60f2003-03-23 17:19:56 +00002178 ret = get_errno(setresuid(low2highuid(arg1),
2179 low2highuid(arg2),
2180 low2highuid(arg3)));
2181 break;
bellard31e31b82003-02-18 22:55:36 +00002182 case TARGET_NR_getresuid:
bellardb03c60f2003-03-23 17:19:56 +00002183 {
2184 int ruid, euid, suid;
2185 ret = get_errno(getresuid(&ruid, &euid, &suid));
2186 if (!is_error(ret)) {
2187 *(uint16_t *)arg1 = tswap16(high2lowuid(ruid));
2188 *(uint16_t *)arg2 = tswap16(high2lowuid(euid));
2189 *(uint16_t *)arg3 = tswap16(high2lowuid(suid));
2190 }
2191 }
2192 break;
2193 case TARGET_NR_setresgid:
2194 ret = get_errno(setresgid(low2highgid(arg1),
2195 low2highgid(arg2),
2196 low2highgid(arg3)));
2197 break;
2198 case TARGET_NR_getresgid:
2199 {
2200 int rgid, egid, sgid;
2201 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2202 if (!is_error(ret)) {
2203 *(uint16_t *)arg1 = high2lowgid(tswap16(rgid));
2204 *(uint16_t *)arg2 = high2lowgid(tswap16(egid));
2205 *(uint16_t *)arg3 = high2lowgid(tswap16(sgid));
2206 }
2207 }
2208 break;
bellard31e31b82003-02-18 22:55:36 +00002209 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00002210 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002211 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00002212 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002213 case TARGET_NR_prctl:
bellard5cd43932003-03-29 16:54:36 +00002214 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002215 case TARGET_NR_pread:
bellard5cd43932003-03-29 16:54:36 +00002216 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002217 case TARGET_NR_pwrite:
2218 goto unimplemented;
2219 case TARGET_NR_chown:
2220 ret = get_errno(chown((const char *)arg1, arg2, arg3));
2221 break;
2222 case TARGET_NR_getcwd:
bellard72f03902003-02-18 23:33:18 +00002223 ret = get_errno(sys_getcwd1((char *)arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00002224 break;
2225 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00002226 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002227 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00002228 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002229 case TARGET_NR_sigaltstack:
bellard5cd43932003-03-29 16:54:36 +00002230 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002231 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00002232 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002233 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00002234 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002235 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00002236 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002237 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00002238 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00002239 break;
2240 case TARGET_NR_ugetrlimit:
bellard5cd43932003-03-29 16:54:36 +00002241 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002242 case TARGET_NR_truncate64:
bellard5cd43932003-03-29 16:54:36 +00002243 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002244 case TARGET_NR_ftruncate64:
bellard60cd49d2003-03-16 22:53:56 +00002245 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002246 case TARGET_NR_stat64:
bellardec86b0f2003-04-11 00:15:04 +00002247 ret = get_errno(stat(path((const char *)arg1), &st));
bellard60cd49d2003-03-16 22:53:56 +00002248 goto do_stat64;
bellard31e31b82003-02-18 22:55:36 +00002249 case TARGET_NR_lstat64:
bellardec86b0f2003-04-11 00:15:04 +00002250 ret = get_errno(lstat(path((const char *)arg1), &st));
bellard60cd49d2003-03-16 22:53:56 +00002251 goto do_stat64;
bellard31e31b82003-02-18 22:55:36 +00002252 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00002253 {
2254 ret = get_errno(fstat(arg1, &st));
2255 do_stat64:
2256 if (!is_error(ret)) {
2257 struct target_stat64 *target_st = (void *)arg2;
bellardec86b0f2003-04-11 00:15:04 +00002258 memset(target_st, 0, sizeof(struct target_stat64));
bellard60cd49d2003-03-16 22:53:56 +00002259 target_st->st_dev = tswap16(st.st_dev);
2260 target_st->st_ino = tswapl(st.st_ino);
bellardec86b0f2003-04-11 00:15:04 +00002261#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
2262 target_st->__st_ino = tswapl(st.st_ino);
2263#endif
bellard9af9eaa2003-04-07 21:34:41 +00002264 target_st->st_mode = tswap32(st.st_mode);
bellardec86b0f2003-04-11 00:15:04 +00002265 target_st->st_nlink = tswap32(st.st_nlink);
2266 target_st->st_uid = tswapl(st.st_uid);
2267 target_st->st_gid = tswapl(st.st_gid);
bellard60cd49d2003-03-16 22:53:56 +00002268 target_st->st_rdev = tswap16(st.st_rdev);
2269 /* XXX: better use of kernel struct */
bellardec86b0f2003-04-11 00:15:04 +00002270 target_st->st_size = tswap64(st.st_size);
bellard60cd49d2003-03-16 22:53:56 +00002271 target_st->st_blksize = tswapl(st.st_blksize);
2272 target_st->st_blocks = tswapl(st.st_blocks);
bellard7854b052003-03-29 17:22:23 +00002273 target_st->target_st_atime = tswapl(st.st_atime);
2274 target_st->target_st_mtime = tswapl(st.st_mtime);
2275 target_st->target_st_ctime = tswapl(st.st_ctime);
bellard60cd49d2003-03-16 22:53:56 +00002276 }
2277 }
2278 break;
2279
bellard31e31b82003-02-18 22:55:36 +00002280 case TARGET_NR_lchown32:
bellardb03c60f2003-03-23 17:19:56 +00002281 ret = get_errno(lchown((const char *)arg1, arg2, arg3));
2282 break;
bellard31e31b82003-02-18 22:55:36 +00002283 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00002284 ret = get_errno(getuid());
2285 break;
bellard31e31b82003-02-18 22:55:36 +00002286 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00002287 ret = get_errno(getgid());
2288 break;
bellard31e31b82003-02-18 22:55:36 +00002289 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00002290 ret = get_errno(geteuid());
2291 break;
bellard31e31b82003-02-18 22:55:36 +00002292 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00002293 ret = get_errno(getegid());
2294 break;
bellard31e31b82003-02-18 22:55:36 +00002295 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00002296 ret = get_errno(setreuid(arg1, arg2));
2297 break;
bellard31e31b82003-02-18 22:55:36 +00002298 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00002299 ret = get_errno(setregid(arg1, arg2));
2300 break;
bellard31e31b82003-02-18 22:55:36 +00002301 case TARGET_NR_getgroups32:
bellardb03c60f2003-03-23 17:19:56 +00002302 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002303 case TARGET_NR_setgroups32:
bellardb03c60f2003-03-23 17:19:56 +00002304 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002305 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00002306 ret = get_errno(fchown(arg1, arg2, arg3));
2307 break;
bellard31e31b82003-02-18 22:55:36 +00002308 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00002309 ret = get_errno(setresuid(arg1, arg2, arg3));
2310 break;
bellard31e31b82003-02-18 22:55:36 +00002311 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00002312 {
2313 int ruid, euid, suid;
2314 ret = get_errno(getresuid(&ruid, &euid, &suid));
2315 if (!is_error(ret)) {
2316 *(uint32_t *)arg1 = tswap32(ruid);
2317 *(uint32_t *)arg2 = tswap32(euid);
2318 *(uint32_t *)arg3 = tswap32(suid);
2319 }
2320 }
2321 break;
bellard31e31b82003-02-18 22:55:36 +00002322 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00002323 ret = get_errno(setresgid(arg1, arg2, arg3));
2324 break;
bellard31e31b82003-02-18 22:55:36 +00002325 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00002326 {
2327 int rgid, egid, sgid;
2328 ret = get_errno(getresgid(&rgid, &egid, &sgid));
2329 if (!is_error(ret)) {
2330 *(uint32_t *)arg1 = tswap32(rgid);
2331 *(uint32_t *)arg2 = tswap32(egid);
2332 *(uint32_t *)arg3 = tswap32(sgid);
2333 }
2334 }
2335 break;
bellard31e31b82003-02-18 22:55:36 +00002336 case TARGET_NR_chown32:
bellardb03c60f2003-03-23 17:19:56 +00002337 ret = get_errno(chown((const char *)arg1, arg2, arg3));
2338 break;
bellard31e31b82003-02-18 22:55:36 +00002339 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00002340 ret = get_errno(setuid(arg1));
2341 break;
bellard31e31b82003-02-18 22:55:36 +00002342 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00002343 ret = get_errno(setgid(arg1));
2344 break;
bellard31e31b82003-02-18 22:55:36 +00002345 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00002346 ret = get_errno(setfsuid(arg1));
2347 break;
bellard31e31b82003-02-18 22:55:36 +00002348 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00002349 ret = get_errno(setfsgid(arg1));
2350 break;
bellard31e31b82003-02-18 22:55:36 +00002351 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00002352 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002353 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00002354 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002355 case TARGET_NR_madvise:
bellard60cd49d2003-03-16 22:53:56 +00002356 goto unimplemented;
2357#if TARGET_LONG_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00002358 case TARGET_NR_fcntl64:
bellard60cd49d2003-03-16 22:53:56 +00002359 switch(arg2) {
2360 case F_GETLK64:
2361 case F_SETLK64:
2362 case F_SETLKW64:
2363 goto unimplemented;
2364 default:
2365 ret = get_errno(fcntl(arg1, arg2, arg3));
2366 break;
2367 }
2368 break;
2369#endif
bellard31e31b82003-02-18 22:55:36 +00002370 case TARGET_NR_security:
2371 goto unimplemented;
2372 case TARGET_NR_gettid:
2373 ret = get_errno(gettid());
2374 break;
2375 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00002376 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00002377 case TARGET_NR_setxattr:
2378 case TARGET_NR_lsetxattr:
2379 case TARGET_NR_fsetxattr:
2380 case TARGET_NR_getxattr:
2381 case TARGET_NR_lgetxattr:
2382 case TARGET_NR_fgetxattr:
2383 case TARGET_NR_listxattr:
2384 case TARGET_NR_llistxattr:
2385 case TARGET_NR_flistxattr:
2386 case TARGET_NR_removexattr:
2387 case TARGET_NR_lremovexattr:
2388 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00002389 goto unimplemented_nowarn;
2390 case TARGET_NR_set_thread_area:
2391 case TARGET_NR_get_thread_area:
2392 goto unimplemented_nowarn;
bellard31e31b82003-02-18 22:55:36 +00002393 default:
2394 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00002395 gemu_log("qemu: Unsupported syscall: %d\n", num);
2396 unimplemented_nowarn:
bellard31e31b82003-02-18 22:55:36 +00002397 ret = -ENOSYS;
2398 break;
2399 }
2400 fail:
2401 return ret;
2402}
2403