4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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.
28 #include <sys/types.h>
32 #include <sys/mount.h>
33 #include <sys/resource.h>
38 #include <sys/socket.h>
40 //#include <sys/user.h>
42 #define termios host_termios
43 #define winsize host_winsize
44 #define termio host_termio
46 #include <linux/termios.h>
47 #include <linux/unistd.h>
48 #include <linux/utsname.h>
49 #include <linux/cdrom.h>
50 #include <linux/hdreg.h>
51 #include <linux/soundcard.h>
58 #define PAGE_SIZE 4096
59 #define PAGE_MASK ~(PAGE_SIZE - 1)
65 unsigned short d_reclen;
66 char d_name[256]; /* We must not include limits.h! */
69 #include "syscall_defs.h"
73 #include "syscall-i386.h"
76 #define __NR_sys_uname __NR_uname
77 #define __NR_sys_getcwd1 __NR_getcwd
78 #define __NR_sys_statfs __NR_statfs
79 #define __NR_sys_fstatfs __NR_fstatfs
80 #define __NR_sys_getdents __NR_getdents
83 _syscall0(int, gettid)
85 static int gettid(void) {
89 _syscall1(int,sys_uname,struct new_utsname *,buf)
90 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
91 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
92 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
93 loff_t *, res, uint, wh);
94 _syscall2(int,sys_statfs,const char *,path,struct kernel_statfs *,buf)
95 _syscall2(int,sys_fstatfs,int,fd,struct kernel_statfs *,buf)
97 static inline long get_errno(long ret)
105 static inline int is_error(long ret)
107 return (unsigned long)ret >= (unsigned long)(-4096);
110 static char *target_brk;
111 static char *target_original_brk;
113 void target_set_brk(char *new_brk)
115 target_brk = new_brk;
116 target_original_brk = new_brk;
119 static long do_brk(char *new_brk)
126 return (long)target_brk;
127 if (new_brk < target_original_brk)
130 brk_page = (char *)(((unsigned long)target_brk + PAGE_SIZE - 1) & PAGE_MASK);
132 /* If the new brk is less than this, set it and we're done... */
133 if (new_brk < brk_page) {
134 target_brk = new_brk;
135 return (long)target_brk;
138 /* We need to allocate more memory after the brk... */
139 new_alloc_size = ((new_brk - brk_page + 1)+(PAGE_SIZE-1)) & PAGE_MASK;
140 mapped_addr = get_errno((long)mmap((caddr_t)brk_page, new_alloc_size,
141 PROT_READ|PROT_WRITE,
142 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
144 if (is_error(mapped_addr)) {
147 target_brk = new_brk;
148 return (long)target_brk;
152 static inline fd_set *target_to_host_fds(fd_set *fds,
153 target_long *target_fds, int n)
155 #if !defined(BSWP_NEEDED) && !defined(WORD_BIGENDIAN)
156 return (fd_set *)target_fds;
161 for(i = 0;i < n; i++) {
162 b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
163 (i & (TARGET_LONG_BITS - 1))) & 1;
174 static inline void host_to_target_fds(target_long *target_fds,
177 #if !defined(BSWP_NEEDED) && !defined(WORD_BIGENDIAN)
184 nw = n / TARGET_LONG_BITS;
186 for(i = 0;i < nw; i++) {
188 for(j = 0; j < TARGET_LONG_BITS; j++) {
189 v |= ((FD_ISSET(k, fds) != 0) << j);
192 target_fds[i] = tswapl(v);
198 /* XXX: incorrect for some archs */
199 static void host_to_target_old_sigset(target_ulong *old_sigset,
200 const sigset_t *sigset)
202 *old_sigset = tswap32(*(unsigned long *)sigset & 0xffffffff);
205 static void target_to_host_old_sigset(sigset_t *sigset,
206 const target_ulong *old_sigset)
209 *(unsigned long *)sigset = tswapl(*old_sigset);
213 static long do_select(long n,
214 target_long *target_rfds, target_long *target_wfds,
215 target_long *target_efds, struct target_timeval *target_tv)
217 fd_set rfds, wfds, efds;
218 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
219 struct timeval tv, *tv_ptr;
222 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
223 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
224 efds_ptr = target_to_host_fds(&efds, target_efds, n);
227 tv.tv_sec = tswapl(target_tv->tv_sec);
228 tv.tv_usec = tswapl(target_tv->tv_usec);
233 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
234 if (!is_error(ret)) {
235 host_to_target_fds(target_rfds, rfds_ptr, n);
236 host_to_target_fds(target_wfds, wfds_ptr, n);
237 host_to_target_fds(target_efds, efds_ptr, n);
240 target_tv->tv_sec = tswapl(tv.tv_sec);
241 target_tv->tv_usec = tswapl(tv.tv_usec);
247 static long do_socketcall(int num, long *vptr)
253 ret = get_errno(socket(vptr[0], vptr[1], vptr[2]));
256 ret = get_errno(bind(vptr[0], (struct sockaddr *)vptr[1], vptr[2]));
259 ret = get_errno(connect(vptr[0], (struct sockaddr *)vptr[1], vptr[2]));
262 ret = get_errno(listen(vptr[0], vptr[1]));
267 size = tswap32(*(int32_t *)vptr[2]);
268 ret = get_errno(accept(vptr[0], (struct sockaddr *)vptr[1], &size));
270 *(int32_t *)vptr[2] = size;
273 case SOCKOP_getsockname:
276 size = tswap32(*(int32_t *)vptr[2]);
277 ret = get_errno(getsockname(vptr[0], (struct sockaddr *)vptr[1], &size));
279 *(int32_t *)vptr[2] = size;
282 case SOCKOP_getpeername:
285 size = tswap32(*(int32_t *)vptr[2]);
286 ret = get_errno(getpeername(vptr[0], (struct sockaddr *)vptr[1], &size));
288 *(int32_t *)vptr[2] = size;
291 case SOCKOP_socketpair:
294 int32_t *target_tab = (int32_t *)vptr[3];
295 ret = get_errno(socketpair(vptr[0], vptr[1], vptr[2], tab));
296 if (!is_error(ret)) {
297 target_tab[0] = tswap32(tab[0]);
298 target_tab[1] = tswap32(tab[1]);
303 ret = get_errno(send(vptr[0], (void *)vptr[1], vptr[2], vptr[3]));
306 ret = get_errno(recv(vptr[0], (void *)vptr[1], vptr[2], vptr[3]));
309 ret = get_errno(sendto(vptr[0], (void *)vptr[1], vptr[2], vptr[3],
310 (struct sockaddr *)vptr[4], vptr[5]));
312 case SOCKOP_recvfrom:
315 size = tswap32(*(int32_t *)vptr[5]);
316 ret = get_errno(recvfrom(vptr[0], (void *)vptr[1], vptr[2],
317 vptr[3], (struct sockaddr *)vptr[4], &size));
319 *(int32_t *)vptr[5] = size;
322 case SOCKOP_shutdown:
323 ret = get_errno(shutdown(vptr[0], vptr[1]));
327 case SOCKOP_setsockopt:
328 case SOCKOP_getsockopt:
330 gemu_log("Unsupported socketcall: %d\n", num);
337 /* kernel structure types definitions */
340 #define STRUCT(name, list...) STRUCT_ ## name,
341 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
343 #include "syscall_types.h"
346 #undef STRUCT_SPECIAL
348 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
349 #define STRUCT_SPECIAL(name)
350 #include "syscall_types.h"
352 #undef STRUCT_SPECIAL
354 typedef struct IOCTLEntry {
359 const argtype arg_type[3];
364 #define IOC_RW (IOC_R | IOC_W)
366 #define MAX_STRUCT_SIZE 4096
368 const IOCTLEntry ioctl_entries[] = {
369 #define IOCTL(cmd, access, types...) \
370 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
375 static long do_ioctl(long fd, long cmd, long arg)
377 const IOCTLEntry *ie;
378 const argtype *arg_type;
380 uint8_t buf_temp[MAX_STRUCT_SIZE];
384 if (ie->target_cmd == 0) {
385 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
388 if (ie->target_cmd == cmd)
392 arg_type = ie->arg_type;
394 gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
396 switch(arg_type[0]) {
399 ret = get_errno(ioctl(fd, ie->host_cmd));
404 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
410 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
411 if (!is_error(ret)) {
412 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
416 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
417 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
421 thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
422 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
423 if (!is_error(ret)) {
424 thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
430 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
437 bitmask_transtbl iflag_tbl[] = {
438 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
439 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
440 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
441 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
442 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
443 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
444 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
445 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
446 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
447 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
448 { TARGET_IXON, TARGET_IXON, IXON, IXON },
449 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
450 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
451 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
455 bitmask_transtbl oflag_tbl[] = {
456 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
457 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
458 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
459 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
460 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
461 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
462 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
463 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
464 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
465 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
466 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
467 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
468 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
469 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
470 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
471 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
472 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
473 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
474 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
475 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
476 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
477 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
478 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
479 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
483 bitmask_transtbl cflag_tbl[] = {
484 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
485 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
486 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
487 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
488 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
489 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
490 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
491 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
492 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
493 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
494 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
495 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
496 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
497 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
498 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
499 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
500 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
501 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
502 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
503 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
504 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
505 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
506 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
507 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
508 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
509 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
510 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
511 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
512 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
513 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
514 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
518 bitmask_transtbl lflag_tbl[] = {
519 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
520 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
521 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
522 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
523 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
524 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
525 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
526 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
527 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
528 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
529 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
530 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
531 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
532 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
533 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
537 static void target_to_host_termios (void *dst, const void *src)
539 struct host_termios *host = dst;
540 const struct target_termios *target = src;
543 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
545 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
547 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
549 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
550 host->c_line = target->c_line;
552 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
553 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
554 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
555 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
556 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
557 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
558 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
559 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
560 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
561 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
562 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
563 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
564 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
565 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
566 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
567 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
568 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
571 static void host_to_target_termios (void *dst, const void *src)
573 struct target_termios *target = dst;
574 const struct host_termios *host = src;
577 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
579 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
581 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
583 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
584 target->c_line = host->c_line;
586 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
587 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
588 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
589 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
590 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
591 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
592 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
593 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
594 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
595 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
596 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
597 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
598 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
599 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
600 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
601 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
602 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
605 StructEntry struct_termios_def = {
606 .convert = { host_to_target_termios, target_to_host_termios },
607 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
608 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
613 /* NOTE: there is really one LDT for all the threads */
616 static int read_ldt(void *ptr, unsigned long bytecount)
622 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
623 if (size > bytecount)
625 memcpy(ptr, ldt_table, size);
629 /* XXX: add locking support */
630 static int write_ldt(CPUX86State *env,
631 void *ptr, unsigned long bytecount, int oldmode)
633 struct target_modify_ldt_ldt_s ldt_info;
634 int seg_32bit, contents, read_exec_only, limit_in_pages;
635 int seg_not_present, useable;
636 uint32_t *lp, entry_1, entry_2;
638 if (bytecount != sizeof(ldt_info))
640 memcpy(&ldt_info, ptr, sizeof(ldt_info));
641 tswap32s(&ldt_info.entry_number);
642 tswapls((long *)&ldt_info.base_addr);
643 tswap32s(&ldt_info.limit);
644 tswap32s(&ldt_info.flags);
646 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
648 seg_32bit = ldt_info.flags & 1;
649 contents = (ldt_info.flags >> 1) & 3;
650 read_exec_only = (ldt_info.flags >> 3) & 1;
651 limit_in_pages = (ldt_info.flags >> 4) & 1;
652 seg_not_present = (ldt_info.flags >> 5) & 1;
653 useable = (ldt_info.flags >> 6) & 1;
658 if (seg_not_present == 0)
661 /* allocate the LDT */
663 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
666 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
667 env->ldt.base = ldt_table;
668 env->ldt.limit = 0xffff;
671 /* NOTE: same code as Linux kernel */
672 /* Allow LDTs to be cleared by the user. */
673 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
676 read_exec_only == 1 &&
678 limit_in_pages == 0 &&
679 seg_not_present == 1 &&
687 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
688 (ldt_info.limit & 0x0ffff);
689 entry_2 = (ldt_info.base_addr & 0xff000000) |
690 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
691 (ldt_info.limit & 0xf0000) |
692 ((read_exec_only ^ 1) << 9) |
694 ((seg_not_present ^ 1) << 15) |
696 (limit_in_pages << 23) |
699 entry_2 |= (useable << 20);
701 /* Install the new entry ... */
703 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
704 lp[0] = tswap32(entry_1);
705 lp[1] = tswap32(entry_2);
709 /* specific and weird i386 syscalls */
710 int gemu_modify_ldt(CPUX86State *env, int func, void *ptr, unsigned long bytecount)
716 ret = read_ldt(ptr, bytecount);
719 ret = write_ldt(env, ptr, bytecount, 1);
722 ret = write_ldt(env, ptr, bytecount, 0);
729 void syscall_init(void)
731 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
732 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
733 #include "syscall_types.h"
735 #undef STRUCT_SPECIAL
738 long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
739 long arg4, long arg5, long arg6)
743 struct kernel_statfs *stfs;
746 gemu_log("syscall %d\n", num);
754 ret = 0; /* avoid warning */
757 ret = get_errno(read(arg1, (void *)arg2, arg3));
759 case TARGET_NR_write:
760 ret = get_errno(write(arg1, (void *)arg2, arg3));
763 ret = get_errno(open((const char *)arg1, arg2, arg3));
765 case TARGET_NR_close:
766 ret = get_errno(close(arg1));
769 ret = do_brk((char *)arg1);
772 ret = get_errno(fork());
774 case TARGET_NR_waitpid:
776 int *status = (int *)arg2;
777 ret = get_errno(waitpid(arg1, status, arg3));
778 if (!is_error(ret) && status)
779 tswapls((long *)&status);
782 case TARGET_NR_creat:
783 ret = get_errno(creat((const char *)arg1, arg2));
786 ret = get_errno(link((const char *)arg1, (const char *)arg2));
788 case TARGET_NR_unlink:
789 ret = get_errno(unlink((const char *)arg1));
791 case TARGET_NR_execve:
792 ret = get_errno(execve((const char *)arg1, (void *)arg2, (void *)arg3));
794 case TARGET_NR_chdir:
795 ret = get_errno(chdir((const char *)arg1));
799 int *time_ptr = (int *)arg1;
800 ret = get_errno(time((time_t *)time_ptr));
801 if (!is_error(ret) && time_ptr)
805 case TARGET_NR_mknod:
806 ret = get_errno(mknod((const char *)arg1, arg2, arg3));
808 case TARGET_NR_chmod:
809 ret = get_errno(chmod((const char *)arg1, arg2));
811 case TARGET_NR_lchown:
812 ret = get_errno(chown((const char *)arg1, arg2, arg3));
814 case TARGET_NR_break:
816 case TARGET_NR_oldstat:
818 case TARGET_NR_lseek:
819 ret = get_errno(lseek(arg1, arg2, arg3));
821 case TARGET_NR_getpid:
822 ret = get_errno(getpid());
824 case TARGET_NR_mount:
825 /* need to look at the data field */
827 case TARGET_NR_umount:
828 ret = get_errno(umount((const char *)arg1));
830 case TARGET_NR_setuid:
831 ret = get_errno(setuid(arg1));
833 case TARGET_NR_getuid:
834 ret = get_errno(getuid());
836 case TARGET_NR_stime:
838 int *time_ptr = (int *)arg1;
841 ret = get_errno(stime((time_t *)time_ptr));
844 case TARGET_NR_ptrace:
846 case TARGET_NR_alarm:
849 case TARGET_NR_oldfstat:
851 case TARGET_NR_pause:
852 ret = get_errno(pause());
854 case TARGET_NR_utime:
860 case TARGET_NR_access:
861 ret = get_errno(access((const char *)arg1, arg2));
864 ret = get_errno(nice(arg1));
866 case TARGET_NR_ftime:
869 ret = get_errno(sync());
872 ret = get_errno(kill(arg1, arg2));
874 case TARGET_NR_rename:
875 ret = get_errno(rename((const char *)arg1, (const char *)arg2));
877 case TARGET_NR_mkdir:
878 ret = get_errno(mkdir((const char *)arg1, arg2));
880 case TARGET_NR_rmdir:
881 ret = get_errno(rmdir((const char *)arg1));
884 ret = get_errno(dup(arg1));
888 int *pipe_ptr = (int *)arg1;
889 ret = get_errno(pipe(pipe_ptr));
890 if (!is_error(ret)) {
891 tswap32s(&pipe_ptr[0]);
892 tswap32s(&pipe_ptr[1]);
896 case TARGET_NR_times:
900 case TARGET_NR_setgid:
901 ret = get_errno(setgid(arg1));
903 case TARGET_NR_getgid:
904 ret = get_errno(getgid());
906 case TARGET_NR_signal:
908 case TARGET_NR_geteuid:
909 ret = get_errno(geteuid());
911 case TARGET_NR_getegid:
912 ret = get_errno(getegid());
916 case TARGET_NR_umount2:
917 ret = get_errno(umount2((const char *)arg1, arg2));
921 case TARGET_NR_ioctl:
922 ret = do_ioctl(arg1, arg2, arg3);
924 case TARGET_NR_fcntl:
931 ret = get_errno(fcntl(arg1, arg2, arg3));
937 case TARGET_NR_setpgid:
938 ret = get_errno(setpgid(arg1, arg2));
940 case TARGET_NR_ulimit:
942 case TARGET_NR_oldolduname:
944 case TARGET_NR_umask:
945 ret = get_errno(umask(arg1));
947 case TARGET_NR_chroot:
948 ret = get_errno(chroot((const char *)arg1));
950 case TARGET_NR_ustat:
953 ret = get_errno(dup2(arg1, arg2));
955 case TARGET_NR_getppid:
956 ret = get_errno(getppid());
958 case TARGET_NR_getpgrp:
959 ret = get_errno(getpgrp());
961 case TARGET_NR_setsid:
962 ret = get_errno(setsid());
964 case TARGET_NR_sigaction:
968 struct target_old_sigaction *tact = arg2, *toldact = arg3;
969 ret = get_errno(setsid());
977 case TARGET_NR_sgetmask:
979 case TARGET_NR_ssetmask:
981 case TARGET_NR_setreuid:
982 ret = get_errno(setreuid(arg1, arg2));
984 case TARGET_NR_setregid:
985 ret = get_errno(setregid(arg1, arg2));
987 case TARGET_NR_sigsuspend:
989 case TARGET_NR_sigpending:
991 case TARGET_NR_sethostname:
992 ret = get_errno(sethostname((const char *)arg1, arg2));
994 case TARGET_NR_setrlimit:
996 case TARGET_NR_getrlimit:
998 case TARGET_NR_getrusage:
1000 case TARGET_NR_gettimeofday:
1002 struct target_timeval *target_tv = (void *)arg1;
1004 ret = get_errno(gettimeofday(&tv, NULL));
1005 if (!is_error(ret)) {
1006 target_tv->tv_sec = tswapl(tv.tv_sec);
1007 target_tv->tv_usec = tswapl(tv.tv_usec);
1011 case TARGET_NR_settimeofday:
1013 struct target_timeval *target_tv = (void *)arg1;
1015 tv.tv_sec = tswapl(target_tv->tv_sec);
1016 tv.tv_usec = tswapl(target_tv->tv_usec);
1017 ret = get_errno(settimeofday(&tv, NULL));
1020 case TARGET_NR_getgroups:
1022 case TARGET_NR_setgroups:
1024 case TARGET_NR_select:
1026 case TARGET_NR_symlink:
1027 ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
1029 case TARGET_NR_oldlstat:
1031 case TARGET_NR_readlink:
1032 ret = get_errno(readlink((const char *)arg1, (char *)arg2, arg3));
1034 case TARGET_NR_uselib:
1036 case TARGET_NR_swapon:
1037 ret = get_errno(swapon((const char *)arg1, arg2));
1039 case TARGET_NR_reboot:
1041 case TARGET_NR_readdir:
1044 case TARGET_NR_mmap:
1046 uint32_t v1, v2, v3, v4, v5, v6, *vptr;
1047 vptr = (uint32_t *)arg1;
1048 v1 = tswap32(vptr[0]);
1049 v2 = tswap32(vptr[1]);
1050 v3 = tswap32(vptr[2]);
1051 v4 = tswap32(vptr[3]);
1052 v5 = tswap32(vptr[4]);
1053 v6 = tswap32(vptr[5]);
1054 ret = get_errno((long)mmap((void *)v1, v2, v3, v4, v5, v6));
1059 case TARGET_NR_mmap2:
1061 case TARGET_NR_mmap:
1063 ret = get_errno((long)mmap((void *)arg1, arg2, arg3, arg4, arg5, arg6));
1065 case TARGET_NR_munmap:
1066 ret = get_errno(munmap((void *)arg1, arg2));
1068 case TARGET_NR_truncate:
1069 ret = get_errno(truncate((const char *)arg1, arg2));
1071 case TARGET_NR_ftruncate:
1072 ret = get_errno(ftruncate(arg1, arg2));
1074 case TARGET_NR_fchmod:
1075 ret = get_errno(fchmod(arg1, arg2));
1077 case TARGET_NR_fchown:
1078 ret = get_errno(fchown(arg1, arg2, arg3));
1080 case TARGET_NR_getpriority:
1081 ret = get_errno(getpriority(arg1, arg2));
1083 case TARGET_NR_setpriority:
1084 ret = get_errno(setpriority(arg1, arg2, arg3));
1086 case TARGET_NR_profil:
1088 case TARGET_NR_statfs:
1089 stfs = (void *)arg2;
1090 ret = get_errno(sys_statfs((const char *)arg1, stfs));
1092 if (!is_error(ret)) {
1093 tswap32s(&stfs->f_type);
1094 tswap32s(&stfs->f_bsize);
1095 tswap32s(&stfs->f_blocks);
1096 tswap32s(&stfs->f_bfree);
1097 tswap32s(&stfs->f_bavail);
1098 tswap32s(&stfs->f_files);
1099 tswap32s(&stfs->f_ffree);
1100 tswap32s(&stfs->f_fsid.val[0]);
1101 tswap32s(&stfs->f_fsid.val[1]);
1102 tswap32s(&stfs->f_namelen);
1105 case TARGET_NR_fstatfs:
1106 stfs = (void *)arg2;
1107 ret = get_errno(sys_fstatfs(arg1, stfs));
1108 goto convert_statfs;
1109 case TARGET_NR_ioperm:
1111 case TARGET_NR_socketcall:
1112 ret = do_socketcall(arg1, (long *)arg2);
1114 case TARGET_NR_syslog:
1116 case TARGET_NR_setitimer:
1118 case TARGET_NR_getitimer:
1120 case TARGET_NR_stat:
1121 ret = get_errno(stat((const char *)arg1, &st));
1123 case TARGET_NR_lstat:
1124 ret = get_errno(lstat((const char *)arg1, &st));
1126 case TARGET_NR_fstat:
1128 ret = get_errno(fstat(arg1, &st));
1130 if (!is_error(ret)) {
1131 struct target_stat *target_st = (void *)arg2;
1132 target_st->st_dev = tswap16(st.st_dev);
1133 target_st->st_ino = tswapl(st.st_ino);
1134 target_st->st_mode = tswap16(st.st_mode);
1135 target_st->st_nlink = tswap16(st.st_nlink);
1136 target_st->st_uid = tswap16(st.st_uid);
1137 target_st->st_gid = tswap16(st.st_gid);
1138 target_st->st_rdev = tswap16(st.st_rdev);
1139 target_st->st_size = tswapl(st.st_size);
1140 target_st->st_blksize = tswapl(st.st_blksize);
1141 target_st->st_blocks = tswapl(st.st_blocks);
1142 target_st->st_atime = tswapl(st.st_atime);
1143 target_st->st_mtime = tswapl(st.st_mtime);
1144 target_st->st_ctime = tswapl(st.st_ctime);
1148 case TARGET_NR_olduname:
1150 case TARGET_NR_iopl:
1152 case TARGET_NR_vhangup:
1153 ret = get_errno(vhangup());
1155 case TARGET_NR_idle:
1157 case TARGET_NR_vm86old:
1159 case TARGET_NR_wait4:
1162 target_long *status_ptr = (void *)arg2;
1163 struct rusage rusage, *rusage_ptr;
1164 struct target_rusage *target_rusage = (void *)arg4;
1166 rusage_ptr = &rusage;
1169 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
1170 if (!is_error(ret)) {
1172 *status_ptr = tswap32(status);
1173 if (target_rusage) {
1174 target_rusage->ru_utime.tv_sec = tswapl(rusage.ru_utime.tv_sec);
1175 target_rusage->ru_utime.tv_usec = tswapl(rusage.ru_utime.tv_usec);
1176 target_rusage->ru_stime.tv_sec = tswapl(rusage.ru_stime.tv_sec);
1177 target_rusage->ru_stime.tv_usec = tswapl(rusage.ru_stime.tv_usec);
1178 target_rusage->ru_maxrss = tswapl(rusage.ru_maxrss);
1179 target_rusage->ru_ixrss = tswapl(rusage.ru_ixrss);
1180 target_rusage->ru_idrss = tswapl(rusage.ru_idrss);
1181 target_rusage->ru_isrss = tswapl(rusage.ru_isrss);
1182 target_rusage->ru_minflt = tswapl(rusage.ru_minflt);
1183 target_rusage->ru_majflt = tswapl(rusage.ru_majflt);
1184 target_rusage->ru_nswap = tswapl(rusage.ru_nswap);
1185 target_rusage->ru_inblock = tswapl(rusage.ru_inblock);
1186 target_rusage->ru_oublock = tswapl(rusage.ru_oublock);
1187 target_rusage->ru_msgsnd = tswapl(rusage.ru_msgsnd);
1188 target_rusage->ru_msgrcv = tswapl(rusage.ru_msgrcv);
1189 target_rusage->ru_nsignals = tswapl(rusage.ru_nsignals);
1190 target_rusage->ru_nvcsw = tswapl(rusage.ru_nvcsw);
1191 target_rusage->ru_nivcsw = tswapl(rusage.ru_nivcsw);
1196 case TARGET_NR_swapoff:
1197 ret = get_errno(swapoff((const char *)arg1));
1199 case TARGET_NR_sysinfo:
1203 case TARGET_NR_fsync:
1204 ret = get_errno(fsync(arg1));
1206 case TARGET_NR_sigreturn:
1208 case TARGET_NR_clone:
1210 case TARGET_NR_setdomainname:
1211 ret = get_errno(setdomainname((const char *)arg1, arg2));
1213 case TARGET_NR_uname:
1214 /* no need to transcode because we use the linux syscall */
1215 ret = get_errno(sys_uname((struct new_utsname *)arg1));
1218 case TARGET_NR_modify_ldt:
1219 ret = get_errno(gemu_modify_ldt(cpu_env, arg1, (void *)arg2, arg3));
1222 case TARGET_NR_adjtimex:
1224 case TARGET_NR_mprotect:
1225 ret = get_errno(mprotect((void *)arg1, arg2, arg3));
1227 case TARGET_NR_sigprocmask:
1230 sigset_t set, oldset, *set_ptr;
1231 target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
1234 case TARGET_SIG_BLOCK:
1237 case TARGET_SIG_UNBLOCK:
1240 case TARGET_SIG_SETMASK:
1249 target_to_host_old_sigset(&set, pset);
1254 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
1255 if (!is_error(ret) && poldset) {
1256 host_to_target_old_sigset(poldset, &oldset);
1260 case TARGET_NR_create_module:
1261 case TARGET_NR_init_module:
1262 case TARGET_NR_delete_module:
1263 case TARGET_NR_get_kernel_syms:
1265 case TARGET_NR_quotactl:
1267 case TARGET_NR_getpgid:
1268 ret = get_errno(getpgid(arg1));
1270 case TARGET_NR_fchdir:
1271 ret = get_errno(fchdir(arg1));
1273 case TARGET_NR_bdflush:
1275 case TARGET_NR_sysfs:
1277 case TARGET_NR_personality:
1278 ret = get_errno(mprotect((void *)arg1, arg2, arg3));
1280 case TARGET_NR_afs_syscall:
1282 case TARGET_NR_setfsuid:
1284 case TARGET_NR_setfsgid:
1286 case TARGET_NR__llseek:
1289 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
1290 *(int64_t *)arg4 = tswap64(res);
1293 case TARGET_NR_getdents:
1294 #if TARGET_LONG_SIZE != 4
1295 #error not supported
1298 struct dirent *dirp = (void *)arg2;
1300 ret = get_errno(sys_getdents(arg1, dirp, count));
1301 if (!is_error(ret)) {
1307 reclen = tswap16(de->d_reclen);
1310 de->d_reclen = reclen;
1311 tswapls(&de->d_ino);
1312 tswapls(&de->d_off);
1313 de = (struct dirent *)((char *)de + reclen);
1319 case TARGET_NR__newselect:
1320 ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4,
1323 case TARGET_NR_flock:
1325 case TARGET_NR_msync:
1326 ret = get_errno(msync((void *)arg1, arg2, arg3));
1328 case TARGET_NR_readv:
1333 struct target_iovec *target_vec = (void *)arg2;
1335 vec = alloca(count * sizeof(struct iovec));
1336 for(i = 0;i < count; i++) {
1337 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
1338 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1340 ret = get_errno(readv(arg1, vec, count));
1343 case TARGET_NR_writev:
1348 struct target_iovec *target_vec = (void *)arg2;
1350 vec = alloca(count * sizeof(struct iovec));
1351 for(i = 0;i < count; i++) {
1352 vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
1353 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1355 ret = get_errno(writev(arg1, vec, count));
1358 case TARGET_NR_getsid:
1359 ret = get_errno(getsid(arg1));
1361 case TARGET_NR_fdatasync:
1363 case TARGET_NR__sysctl:
1365 case TARGET_NR_mlock:
1366 ret = get_errno(mlock((void *)arg1, arg2));
1368 case TARGET_NR_munlock:
1369 ret = get_errno(munlock((void *)arg1, arg2));
1371 case TARGET_NR_mlockall:
1372 ret = get_errno(mlockall(arg1));
1374 case TARGET_NR_munlockall:
1375 ret = get_errno(munlockall());
1377 case TARGET_NR_sched_setparam:
1379 case TARGET_NR_sched_getparam:
1381 case TARGET_NR_sched_setscheduler:
1383 case TARGET_NR_sched_getscheduler:
1385 case TARGET_NR_sched_yield:
1386 ret = get_errno(sched_yield());
1388 case TARGET_NR_sched_get_priority_max:
1389 case TARGET_NR_sched_get_priority_min:
1390 case TARGET_NR_sched_rr_get_interval:
1391 case TARGET_NR_nanosleep:
1392 case TARGET_NR_mremap:
1393 case TARGET_NR_setresuid:
1394 case TARGET_NR_getresuid:
1395 case TARGET_NR_vm86:
1396 case TARGET_NR_query_module:
1397 case TARGET_NR_poll:
1398 case TARGET_NR_nfsservctl:
1399 case TARGET_NR_setresgid:
1400 case TARGET_NR_getresgid:
1401 case TARGET_NR_prctl:
1402 case TARGET_NR_rt_sigreturn:
1403 case TARGET_NR_rt_sigaction:
1404 case TARGET_NR_rt_sigprocmask:
1405 case TARGET_NR_rt_sigpending:
1406 case TARGET_NR_rt_sigtimedwait:
1407 case TARGET_NR_rt_sigqueueinfo:
1408 case TARGET_NR_rt_sigsuspend:
1409 case TARGET_NR_pread:
1410 case TARGET_NR_pwrite:
1412 case TARGET_NR_chown:
1413 ret = get_errno(chown((const char *)arg1, arg2, arg3));
1415 case TARGET_NR_getcwd:
1416 ret = get_errno(sys_getcwd1((char *)arg1, arg2));
1418 case TARGET_NR_capget:
1419 case TARGET_NR_capset:
1420 case TARGET_NR_sigaltstack:
1421 case TARGET_NR_sendfile:
1422 case TARGET_NR_getpmsg:
1423 case TARGET_NR_putpmsg:
1424 case TARGET_NR_vfork:
1425 ret = get_errno(vfork());
1427 case TARGET_NR_ugetrlimit:
1428 case TARGET_NR_truncate64:
1429 case TARGET_NR_ftruncate64:
1430 case TARGET_NR_stat64:
1431 case TARGET_NR_lstat64:
1432 case TARGET_NR_fstat64:
1433 case TARGET_NR_lchown32:
1434 case TARGET_NR_getuid32:
1435 case TARGET_NR_getgid32:
1436 case TARGET_NR_geteuid32:
1437 case TARGET_NR_getegid32:
1438 case TARGET_NR_setreuid32:
1439 case TARGET_NR_setregid32:
1440 case TARGET_NR_getgroups32:
1441 case TARGET_NR_setgroups32:
1442 case TARGET_NR_fchown32:
1443 case TARGET_NR_setresuid32:
1444 case TARGET_NR_getresuid32:
1445 case TARGET_NR_setresgid32:
1446 case TARGET_NR_getresgid32:
1447 case TARGET_NR_chown32:
1448 case TARGET_NR_setuid32:
1449 case TARGET_NR_setgid32:
1450 case TARGET_NR_setfsuid32:
1451 case TARGET_NR_setfsgid32:
1452 case TARGET_NR_pivot_root:
1453 case TARGET_NR_mincore:
1454 case TARGET_NR_madvise:
1455 case TARGET_NR_getdents64:
1456 case TARGET_NR_fcntl64:
1457 case TARGET_NR_security:
1459 case TARGET_NR_gettid:
1460 ret = get_errno(gettid());
1462 case TARGET_NR_readahead:
1463 case TARGET_NR_setxattr:
1464 case TARGET_NR_lsetxattr:
1465 case TARGET_NR_fsetxattr:
1466 case TARGET_NR_getxattr:
1467 case TARGET_NR_lgetxattr:
1468 case TARGET_NR_fgetxattr:
1469 case TARGET_NR_listxattr:
1470 case TARGET_NR_llistxattr:
1471 case TARGET_NR_flistxattr:
1472 case TARGET_NR_removexattr:
1473 case TARGET_NR_lremovexattr:
1474 case TARGET_NR_fremovexattr:
1478 gemu_log("Unsupported syscall: %d\n", num);