aboutsummaryrefslogtreecommitdiff
path: root/linux-user
diff options
context:
space:
mode:
authorbellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>2003-02-18 22:55:36 +0000
committerbellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>2003-02-18 22:55:36 +0000
commit31e31b8a248ffa216223dad49f75efbdfca5df23 (patch)
tree7a2e4a2e20da5bd5d61f906f6d75dab2d3418e5f /linux-user
parente63c3dc74bfb90e4522d075d0d5a7600c5145745 (diff)
This commit was generated by cvs2svn to compensate for changes in r2,
which included commits to RCS files with non-trunk default branches. git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@3 c046a42c-6fe2-441c-8c8c-71466251a162
Diffstat (limited to 'linux-user')
-rw-r--r--linux-user/elfload.c973
-rw-r--r--linux-user/ioctls.h282
-rw-r--r--linux-user/main.c310
-rw-r--r--linux-user/qemu.h57
-rw-r--r--linux-user/signal.c105
-rw-r--r--linux-user/syscall.c1349
-rw-r--r--linux-user/syscall_defs.h283
-rw-r--r--linux-user/syscall_types.h64
8 files changed, 3423 insertions, 0 deletions
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
new file mode 100644
index 0000000000..0ee4f1365b
--- /dev/null
+++ b/linux-user/elfload.c
@@ -0,0 +1,973 @@
+/* This is the Linux kernel elf-loading code, ported into user space */
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "gemu.h"
+
+#include "linux_bin.h"
+#include "elf.h"
+#include "segment.h"
+
+/* Necessary parameters */
+#define ALPHA_PAGE_SIZE 4096
+#define X86_PAGE_SIZE 4096
+
+#define ALPHA_PAGE_MASK (~(ALPHA_PAGE_SIZE-1))
+#define X86_PAGE_MASK (~(X86_PAGE_SIZE-1))
+
+#define ALPHA_PAGE_ALIGN(addr) ((((addr)+ALPHA_PAGE_SIZE)-1)&ALPHA_PAGE_MASK)
+#define X86_PAGE_ALIGN(addr) ((((addr)+X86_PAGE_SIZE)-1)&X86_PAGE_MASK)
+
+#define NGROUPS 32
+
+#define X86_ELF_EXEC_PAGESIZE X86_PAGE_SIZE
+#define X86_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(X86_ELF_EXEC_PAGESIZE-1))
+#define X86_ELF_PAGEOFFSET(_v) ((_v) & (X86_ELF_EXEC_PAGESIZE-1))
+
+#define ALPHA_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ALPHA_PAGE_SIZE-1))
+#define ALPHA_ELF_PAGEOFFSET(_v) ((_v) & (ALPHA_PAGE_SIZE-1))
+
+#define INTERPRETER_NONE 0
+#define INTERPRETER_AOUT 1
+#define INTERPRETER_ELF 2
+
+#define DLINFO_ITEMS 12
+
+/* Where we find X86 libraries... */
+//#define X86_DEFAULT_LIB_DIR "/usr/x86/"
+#define X86_DEFAULT_LIB_DIR "/"
+
+//extern void * mmap4k();
+#define mmap4k(a, b, c, d, e, f) mmap((void *)(a), b, c, d, e, f)
+
+extern unsigned long x86_stack_size;
+
+static int load_aout_interp(void * exptr, int interp_fd);
+
+#ifdef BSWAP_NEEDED
+static void bswap_ehdr(Elf32_Ehdr *ehdr)
+{
+ bswap16s(&ehdr->e_type); /* Object file type */
+ bswap16s(&ehdr->e_machine); /* Architecture */
+ bswap32s(&ehdr->e_version); /* Object file version */
+ bswap32s(&ehdr->e_entry); /* Entry point virtual address */
+ bswap32s(&ehdr->e_phoff); /* Program header table file offset */
+ bswap32s(&ehdr->e_shoff); /* Section header table file offset */
+ bswap32s(&ehdr->e_flags); /* Processor-specific flags */
+ bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
+ bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
+ bswap16s(&ehdr->e_phnum); /* Program header table entry count */
+ bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
+ bswap16s(&ehdr->e_shnum); /* Section header table entry count */
+ bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
+}
+
+static void bswap_phdr(Elf32_Phdr *phdr)
+{
+ bswap32s(&phdr->p_type); /* Segment type */
+ bswap32s(&phdr->p_offset); /* Segment file offset */
+ bswap32s(&phdr->p_vaddr); /* Segment virtual address */
+ bswap32s(&phdr->p_paddr); /* Segment physical address */
+ bswap32s(&phdr->p_filesz); /* Segment size in file */
+ bswap32s(&phdr->p_memsz); /* Segment size in memory */
+ bswap32s(&phdr->p_flags); /* Segment flags */
+ bswap32s(&phdr->p_align); /* Segment alignment */
+}
+#endif
+
+static void * get_free_page(void)
+{
+ void * retval;
+
+ /* User-space version of kernel get_free_page. Returns a page-aligned
+ * page-sized chunk of memory.
+ */
+ retval = mmap4k(0, ALPHA_PAGE_SIZE, PROT_READ|PROT_WRITE,
+ MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+
+ if((long)retval == -1) {
+ perror("get_free_page");
+ exit(-1);
+ }
+ else {
+ return(retval);
+ }
+}
+
+static void free_page(void * pageaddr)
+{
+ (void)munmap(pageaddr, ALPHA_PAGE_SIZE);
+}
+
+/*
+ * 'copy_string()' copies argument/envelope strings from user
+ * memory to free pages in kernel mem. These are in a format ready
+ * to be put directly into the top of new user memory.
+ *
+ */
+static unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
+ unsigned long p)
+{
+ char *tmp, *tmp1, *pag = NULL;
+ int len, offset = 0;
+
+ if (!p) {
+ return 0; /* bullet-proofing */
+ }
+ while (argc-- > 0) {
+ if (!(tmp1 = tmp = get_user(argv+argc))) {
+ fprintf(stderr, "VFS: argc is wrong");
+ exit(-1);
+ }
+ while (get_user(tmp++));
+ len = tmp - tmp1;
+ if (p < len) { /* this shouldn't happen - 128kB */
+ return 0;
+ }
+ while (len) {
+ --p; --tmp; --len;
+ if (--offset < 0) {
+ offset = p % X86_PAGE_SIZE;
+ if (!(pag = (char *) page[p/X86_PAGE_SIZE]) &&
+ !(pag = (char *) page[p/X86_PAGE_SIZE] =
+ (unsigned long *) get_free_page())) {
+ return 0;
+ }
+ }
+ if (len == 0 || offset == 0) {
+ *(pag + offset) = get_user(tmp);
+ }
+ else {
+ int bytes_to_copy = (len > offset) ? offset : len;
+ tmp -= bytes_to_copy;
+ p -= bytes_to_copy;
+ offset -= bytes_to_copy;
+ len -= bytes_to_copy;
+ memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
+ }
+ }
+ }
+ return p;
+}
+
+static int in_group_p(gid_t g)
+{
+ /* return TRUE if we're in the specified group, FALSE otherwise */
+ int ngroup;
+ int i;
+ gid_t grouplist[NGROUPS];
+
+ ngroup = getgroups(NGROUPS, grouplist);
+ for(i = 0; i < ngroup; i++) {
+ if(grouplist[i] == g) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int count(char ** vec)
+{
+ int i;
+
+ for(i = 0; *vec; i++) {
+ vec++;
+ }
+
+ return(i);
+}
+
+static int prepare_binprm(struct linux_binprm *bprm)
+{
+ struct stat st;
+ int mode;
+ int retval, id_change;
+
+ if(fstat(bprm->fd, &st) < 0) {
+ return(-errno);
+ }
+
+ mode = st.st_mode;
+ if(!S_ISREG(mode)) { /* Must be regular file */
+ return(-EACCES);
+ }
+ if(!(mode & 0111)) { /* Must have at least one execute bit set */
+ return(-EACCES);
+ }
+
+ bprm->e_uid = geteuid();
+ bprm->e_gid = getegid();
+ id_change = 0;
+
+ /* Set-uid? */
+ if(mode & S_ISUID) {
+ bprm->e_uid = st.st_uid;
+ if(bprm->e_uid != geteuid()) {
+ id_change = 1;
+ }
+ }
+
+ /* Set-gid? */
+ /*
+ * If setgid is set but no group execute bit then this
+ * is a candidate for mandatory locking, not a setgid
+ * executable.
+ */
+ if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
+ bprm->e_gid = st.st_gid;
+ if (!in_group_p(bprm->e_gid)) {
+ id_change = 1;
+ }
+ }
+
+ memset(bprm->buf, 0, sizeof(bprm->buf));
+ retval = lseek(bprm->fd, 0L, SEEK_SET);
+ if(retval >= 0) {
+ retval = read(bprm->fd, bprm->buf, 128);
+ }
+ if(retval < 0) {
+ perror("prepare_binprm");
+ exit(-1);
+ /* return(-errno); */
+ }
+ else {
+ return(retval);
+ }
+}
+
+unsigned long setup_arg_pages(unsigned long p, struct linux_binprm * bprm,
+ struct image_info * info)
+{
+ unsigned long stack_base;
+ int i;
+ extern unsigned long stktop;
+
+ stack_base = X86_STACK_TOP - MAX_ARG_PAGES*X86_PAGE_SIZE;
+
+ p += stack_base;
+ if (bprm->loader) {
+ bprm->loader += stack_base;
+ }
+ bprm->exec += stack_base;
+
+ /* Create enough stack to hold everything. If we don't use
+ * it for args, we'll use it for something else...
+ */
+ if(x86_stack_size > MAX_ARG_PAGES*X86_PAGE_SIZE) {
+ if((long)mmap4k((void *)(X86_STACK_TOP-x86_stack_size), x86_stack_size + X86_PAGE_SIZE,
+ PROT_READ | PROT_WRITE,
+ MAP_GROWSDOWN | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
+ perror("stk mmap");
+ exit(-1);
+ }
+ }
+ else {
+ if((long)mmap4k((void *)stack_base, (MAX_ARG_PAGES+1)*X86_PAGE_SIZE,
+ PROT_READ | PROT_WRITE,
+ MAP_GROWSDOWN | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
+ perror("stk mmap");
+ exit(-1);
+ }
+ }
+
+ stktop = stack_base;
+
+ for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
+ if (bprm->page[i]) {
+ info->rss++;
+
+ memcpy((void *)stack_base, (void *)bprm->page[i], X86_PAGE_SIZE);
+ free_page((void *)bprm->page[i]);
+ }
+ stack_base += X86_PAGE_SIZE;
+ }
+ return p;
+}
+
+static void set_brk(unsigned long start, unsigned long end)
+{
+ /* page-align the start and end addresses... */
+ start = ALPHA_PAGE_ALIGN(start);
+ end = ALPHA_PAGE_ALIGN(end);
+ if (end <= start)
+ return;
+ if((long)mmap4k(start, end - start,
+ PROT_READ | PROT_WRITE | PROT_EXEC,
+ MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) == -1) {
+ perror("cannot mmap brk");
+ exit(-1);
+ }
+}
+
+
+/* We need to explicitly zero any fractional pages
+ after the data section (i.e. bss). This would
+ contain the junk from the file that should not
+ be in memory */
+
+
+static void padzero(unsigned long elf_bss)
+{
+ unsigned long nbyte;
+ char * fpnt;
+
+ nbyte = elf_bss & (ALPHA_PAGE_SIZE-1); /* was X86_PAGE_SIZE - JRP */
+ if (nbyte) {
+ nbyte = ALPHA_PAGE_SIZE - nbyte;
+ fpnt = (char *) elf_bss;
+ do {
+ *fpnt++ = 0;
+ } while (--nbyte);
+ }
+}
+
+static unsigned int * create_elf_tables(char *p, int argc, int envc,
+ struct elfhdr * exec,
+ unsigned long load_addr,
+ unsigned long interp_load_addr, int ibcs,
+ struct image_info *info)
+{
+ unsigned int *argv, *envp, *dlinfo;
+ unsigned int *sp;
+ char **alpha_envp;
+
+ /*
+ * Force 16 byte alignment here for generality.
+ */
+ sp = (unsigned int *) (~15UL & (unsigned long) p);
+ sp -= exec ? DLINFO_ITEMS*2 : 2;
+ dlinfo = sp;
+ sp -= envc+1;
+ envp = sp;
+ sp -= argc+1;
+ argv = sp;
+ if (!ibcs) {
+ put_user(envp,--sp);
+ put_user(argv,--sp);
+ }
+ alpha_envp = (char **)malloc((envc+1) * sizeof(char *));
+
+#define NEW_AUX_ENT(id, val) \
+ put_user ((id), dlinfo++); \
+ put_user ((val), dlinfo++)
+
+ if (exec) { /* Put this here for an ELF program interpreter */
+ struct elf_phdr * eppnt;
+ eppnt = (struct elf_phdr *)((unsigned long)exec->e_phoff);
+
+ NEW_AUX_ENT (AT_PHDR, (unsigned int)(load_addr + exec->e_phoff));
+ NEW_AUX_ENT (AT_PHENT, (unsigned int)(sizeof (struct elf_phdr)));
+ NEW_AUX_ENT (AT_PHNUM, (unsigned int)(exec->e_phnum));
+ NEW_AUX_ENT (AT_PAGESZ, (unsigned int)(ALPHA_PAGE_SIZE));
+ NEW_AUX_ENT (AT_BASE, (unsigned int)(interp_load_addr));
+ NEW_AUX_ENT (AT_FLAGS, (unsigned int)0);
+ NEW_AUX_ENT (AT_ENTRY, (unsigned int) exec->e_entry);
+ NEW_AUX_ENT (AT_UID, (unsigned int) getuid());
+ NEW_AUX_ENT (AT_EUID, (unsigned int) geteuid());
+ NEW_AUX_ENT (AT_GID, (unsigned int) getgid());
+ NEW_AUX_ENT (AT_EGID, (unsigned int) getegid());
+ }
+ NEW_AUX_ENT (AT_NULL, 0);
+#undef NEW_AUX_ENT
+ put_user((unsigned int)argc,--sp);
+ info->arg_start = (unsigned int)((unsigned long)p & 0xffffffff);
+ while (argc-->0) {
+ put_user(p,argv++);
+ while (get_user(p++)) /* nothing */ ;
+ }
+ put_user(0,argv);
+ info->arg_end = info->env_start = (unsigned int)((unsigned long)p & 0xffffffff);
+ __environ = alpha_envp;
+ while (envc-->0) {
+ *alpha_envp++ = (char *)p;
+ put_user(p,envp++);
+ while (get_user(p++)) /* nothing */ ;
+ }
+ put_user(0,envp);
+ *alpha_envp = 0;
+ info->env_end = (unsigned int)((unsigned long)p & 0xffffffff);
+ return sp;
+}
+
+
+
+static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
+ int interpreter_fd,
+ unsigned long *interp_load_addr)
+{
+ struct elf_phdr *elf_phdata = NULL;
+ struct elf_phdr *eppnt;
+ unsigned long load_addr;
+ int load_addr_set = 0;
+ int retval;
+ unsigned long last_bss, elf_bss;
+ unsigned long error;
+ int i;
+
+ elf_bss = 0;
+ last_bss = 0;
+ error = 0;
+
+ /* We put this here so that mmap will search for the *first*
+ * available memory...
+ */
+ load_addr = INTERP_LOADADDR;
+
+ /* First of all, some simple consistency checks */
+ if ((interp_elf_ex->e_type != ET_EXEC &&
+ interp_elf_ex->e_type != ET_DYN) ||
+ !elf_check_arch(interp_elf_ex->e_machine)) {
+ return ~0UL;
+ }
+
+ /* Now read in all of the header information */
+
+ if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > X86_PAGE_SIZE)
+ return ~0UL;
+
+ elf_phdata = (struct elf_phdr *)
+ malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
+
+ if (!elf_phdata)
+ return ~0UL;
+
+ /*
+ * If the size of this structure has changed, then punt, since
+ * we will be doing the wrong thing.
+ */
+ if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
+ {
+ free(elf_phdata);
+ return ~0UL;
+ }
+
+ retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
+ if(retval >= 0) {
+ retval = read(interpreter_fd,
+ (char *) elf_phdata,
+ sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
+ }
+
+ if (retval < 0) {
+ perror("load_elf_interp");
+ exit(-1);
+ free (elf_phdata);
+ return retval;
+ }
+#ifdef BSWAP_NEEDED
+ eppnt = elf_phdata;
+ for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
+ bswap_phdr(eppnt);
+ }
+#endif
+ eppnt = elf_phdata;
+ for(i=0; i<interp_elf_ex->e_phnum; i++, eppnt++)
+ if (eppnt->p_type == PT_LOAD) {
+ int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
+ int elf_prot = 0;
+ unsigned long vaddr = 0;
+ unsigned long k;
+
+ if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
+ if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
+ if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
+ if (interp_elf_ex->e_type == ET_EXEC || load_addr_set) {
+ elf_type |= MAP_FIXED;
+ vaddr = eppnt->p_vaddr;
+ }
+ error = (unsigned long)mmap4k(load_addr+X86_ELF_PAGESTART(vaddr),
+ eppnt->p_filesz + X86_ELF_PAGEOFFSET(eppnt->p_vaddr),
+ elf_prot,
+ elf_type,
+ interpreter_fd,
+ eppnt->p_offset - X86_ELF_PAGEOFFSET(eppnt->p_vaddr));
+
+ if (error > -1024UL) {
+ /* Real error */
+ close(interpreter_fd);
+ free(elf_phdata);
+ return ~0UL;
+ }
+
+ if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
+ load_addr = error;
+ load_addr_set = 1;
+ }
+
+ /*
+ * Find the end of the file mapping for this phdr, and keep
+ * track of the largest address we see for this.
+ */
+ k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
+ if (k > elf_bss) elf_bss = k;
+
+ /*
+ * Do the same thing for the memory mapping - between
+ * elf_bss and last_bss is the bss section.
+ */
+ k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
+ if (k > last_bss) last_bss = k;
+ }
+
+ /* Now use mmap to map the library into memory. */
+
+ close(interpreter_fd);
+
+ /*
+ * Now fill out the bss section. First pad the last page up
+ * to the page boundary, and then perform a mmap to make sure
+ * that there are zeromapped pages up to and including the last
+ * bss page.
+ */
+ padzero(elf_bss);
+ elf_bss = X86_ELF_PAGESTART(elf_bss + ALPHA_PAGE_SIZE - 1); /* What we have mapped so far */
+
+ /* Map the last of the bss segment */
+ if (last_bss > elf_bss) {
+ mmap4k(elf_bss, last_bss-elf_bss,
+ PROT_READ|PROT_WRITE|PROT_EXEC,
+ MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+ }
+ free(elf_phdata);
+
+ *interp_load_addr = load_addr;
+ return ((unsigned long) interp_elf_ex->e_entry) + load_addr;
+}
+
+
+
+static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs,
+ struct image_info * info)
+{
+ struct elfhdr elf_ex;
+ struct elfhdr interp_elf_ex;
+ struct exec interp_ex;
+ int interpreter_fd = -1; /* avoid warning */
+ unsigned long load_addr;
+ int load_addr_set = 0;
+ unsigned int interpreter_type = INTERPRETER_NONE;
+ unsigned char ibcs2_interpreter;
+ int i;
+ void * mapped_addr;
+ struct elf_phdr * elf_ppnt;
+ struct elf_phdr *elf_phdata;
+ unsigned long elf_bss, k, elf_brk;
+ int retval;
+ char * elf_interpreter;
+ unsigned long elf_entry, interp_load_addr = 0;
+ int status;
+ unsigned long start_code, end_code, end_data;
+ unsigned long elf_stack;
+ char passed_fileno[6];
+
+ ibcs2_interpreter = 0;
+ status = 0;
+ load_addr = 0;
+ elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
+#ifdef BSWAP_NEEDED
+ bswap_ehdr(&elf_ex);
+#endif
+
+ if (elf_ex.e_ident[0] != 0x7f ||
+ strncmp(&elf_ex.e_ident[1], "ELF",3) != 0) {
+ return -ENOEXEC;
+ }
+
+
+ /* First of all, some simple consistency checks */
+ if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
+ (! elf_check_arch(elf_ex.e_machine))) {
+ return -ENOEXEC;
+ }
+
+ /* Now read in all of the header information */
+
+ elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_phnum);
+ if (elf_phdata == NULL) {
+ return -ENOMEM;
+ }
+
+ retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
+ if(retval > 0) {
+ retval = read(bprm->fd, (char *) elf_phdata,
+ elf_ex.e_phentsize * elf_ex.e_phnum);
+ }
+
+ if (retval < 0) {
+ perror("load_elf_binary");
+ exit(-1);
+ free (elf_phdata);
+ return -errno;
+ }
+
+ elf_ppnt = elf_phdata;
+
+ elf_bss = 0;
+ elf_brk = 0;
+
+
+ elf_stack = ~0UL;
+ elf_interpreter = NULL;
+ start_code = ~0UL;
+ end_code = 0;
+ end_data = 0;
+
+ for(i=0;i < elf_ex.e_phnum; i++) {
+ if (elf_ppnt->p_type == PT_INTERP) {
+ if ( elf_interpreter != NULL )
+ {
+ free (elf_phdata);
+ free(elf_interpreter);
+ close(bprm->fd);
+ return -EINVAL;
+ }
+
+ /* This is the program interpreter used for
+ * shared libraries - for now assume that this
+ * is an a.out format binary
+ */
+
+ elf_interpreter = (char *)malloc(elf_ppnt->p_filesz+strlen(X86_DEFAULT_LIB_DIR));
+
+ if (elf_interpreter == NULL) {
+ free (elf_phdata);
+ close(bprm->fd);
+ return -ENOMEM;
+ }
+
+ strcpy(elf_interpreter, X86_DEFAULT_LIB_DIR);
+ retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
+ if(retval >= 0) {
+ retval = read(bprm->fd,
+ elf_interpreter+strlen(X86_DEFAULT_LIB_DIR),
+ elf_ppnt->p_filesz);
+ }
+ if(retval < 0) {
+ perror("load_elf_binary2");
+ exit(-1);
+ }
+
+ /* If the program interpreter is one of these two,
+ then assume an iBCS2 image. Otherwise assume
+ a native linux image. */
+
+ /* JRP - Need to add X86 lib dir stuff here... */
+
+ if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
+ strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0) {
+ ibcs2_interpreter = 1;
+ }
+
+#if 0
+ printf("Using ELF interpreter %s\n", elf_interpreter);
+#endif
+ if (retval >= 0) {
+ retval = open(elf_interpreter, O_RDONLY);
+ if(retval >= 0) {
+ interpreter_fd = retval;
+ }
+ else {
+ perror(elf_interpreter);
+ exit(-1);
+ /* retval = -errno; */
+ }
+ }
+
+ if (retval >= 0) {
+ retval = lseek(interpreter_fd, 0, SEEK_SET);
+ if(retval >= 0) {
+ retval = read(interpreter_fd,bprm->buf,128);
+ }
+ }
+ if (retval >= 0) {
+ interp_ex = *((struct exec *) bprm->buf); /* aout exec-header */
+ interp_elf_ex=*((struct elfhdr *) bprm->buf); /* elf exec-header */
+ }
+ if (retval < 0) {
+ perror("load_elf_binary3");
+ exit(-1);
+ free (elf_phdata);
+ free(elf_interpreter);
+ close(bprm->fd);
+ return retval;
+ }
+ }
+ elf_ppnt++;
+ }
+
+ /* Some simple consistency checks for the interpreter */
+ if (elf_interpreter){
+ interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
+
+ /* Now figure out which format our binary is */
+ if ((N_MAGIC(interp_ex) != OMAGIC) && (N_MAGIC(interp_ex) != ZMAGIC) &&
+ (N_MAGIC(interp_ex) != QMAGIC)) {
+ interpreter_type = INTERPRETER_ELF;
+ }
+
+ if (interp_elf_ex.e_ident[0] != 0x7f ||
+ strncmp(&interp_elf_ex.e_ident[1], "ELF",3) != 0) {
+ interpreter_type &= ~INTERPRETER_ELF;
+ }
+
+ if (!interpreter_type) {
+ free(elf_interpreter);
+ free(elf_phdata);
+ close(bprm->fd);
+ return -ELIBBAD;
+ }
+ }
+
+ /* OK, we are done with that, now set up the arg stuff,
+ and then start this sucker up */
+
+ if (!bprm->sh_bang) {
+ char * passed_p;
+
+ if (interpreter_type == INTERPRETER_AOUT) {
+ sprintf(passed_fileno, "%d", bprm->fd);
+ passed_p = passed_fileno;
+
+ if (elf_interpreter) {
+ bprm->p = copy_strings(1,&passed_p,bprm->page,bprm->p);
+ bprm->argc++;
+ }
+ }
+ if (!bprm->p) {
+ if (elf_interpreter) {
+ free(elf_interpreter);
+ }
+ free (elf_phdata);
+ close(bprm->fd);
+ return -E2BIG;
+ }
+ }
+
+ /* OK, This is the point of no return */
+ info->end_data = 0;
+ info->end_code = 0;
+ info->start_mmap = (unsigned long)ELF_START_MMAP;
+ info->mmap = 0;
+ elf_entry = (unsigned long) elf_ex.e_entry;
+
+ /* Do this so that we can load the interpreter, if need be. We will
+ change some of these later */
+ info->rss = 0;
+ bprm->p = setup_arg_pages(bprm->p, bprm, info);
+ info->start_stack = bprm->p;
+
+ /* Now we do a little grungy work by mmaping the ELF image into
+ * the correct location in memory. At this point, we assume that
+ * the image should be loaded at fixed address, not at a variable
+ * address.
+ */
+
+
+
+ for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
+ if (elf_ppnt->p_type == PT_LOAD) {
+ int elf_prot = 0;
+ if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
+ if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
+ if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
+
+ mapped_addr = mmap4k(X86_ELF_PAGESTART(elf_ppnt->p_vaddr),
+ (elf_ppnt->p_filesz +
+ X86_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
+ elf_prot,
+ (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
+ bprm->fd,
+ (elf_ppnt->p_offset -
+ X86_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
+
+ if((unsigned long)mapped_addr == 0xffffffffffffffff) {
+ perror("mmap");
+ exit(-1);
+ }
+
+
+
+#ifdef LOW_ELF_STACK
+ if (X86_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
+ elf_stack = X86_ELF_PAGESTART(elf_ppnt->p_vaddr);
+#endif
+
+ if (!load_addr_set) {
+ load_addr = elf_ppnt->p_vaddr - elf_ppnt->p_offset;
+ load_addr_set = 1;
+ }
+ k = elf_ppnt->p_vaddr;
+ if (k < start_code) start_code = k;
+ k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
+ if (k > elf_bss) elf_bss = k;
+#if 1
+ if ((elf_ppnt->p_flags & PF_X) && end_code < k)
+#else
+ if ( !(elf_ppnt->p_flags & PF_W) && end_code < k)
+#endif
+ end_code = k;
+ if (end_data < k) end_data = k;
+ k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
+ if (k > elf_brk) elf_brk = k;
+ }
+ }
+
+ if (elf_interpreter) {
+ if (interpreter_type & 1) {
+ elf_entry = load_aout_interp(&interp_ex, interpreter_fd);
+ }
+ else if (interpreter_type & 2) {
+ elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
+ &interp_load_addr);
+ }
+
+ close(interpreter_fd);
+ free(elf_interpreter);
+
+ if (elf_entry == ~0UL) {
+ printf("Unable to load interpreter\n");
+ free(elf_phdata);
+ exit(-1);
+ return 0;
+ }
+ }
+
+ free(elf_phdata);
+
+ if (interpreter_type != INTERPRETER_AOUT) close(bprm->fd);
+ info->personality = (ibcs2_interpreter ? PER_SVR4 : PER_LINUX);
+
+#ifdef LOW_ELF_STACK
+ info->start_stack = bprm->p = elf_stack - 4;
+#endif
+ bprm->p = (unsigned long)
+ create_elf_tables((char *)bprm->p,
+ bprm->argc,
+ bprm->envc,
+ (interpreter_type == INTERPRETER_ELF ? &elf_ex : NULL),
+ load_addr,
+ interp_load_addr,
+ (interpreter_type == INTERPRETER_AOUT ? 0 : 1),
+ info);
+ if (interpreter_type == INTERPRETER_AOUT)
+ info->arg_start += strlen(passed_fileno) + 1;
+ info->start_brk = info->brk = elf_brk;
+ info->end_code = end_code;
+ info->start_code = start_code;
+ info->end_data = end_data;
+ info->start_stack = bprm->p;
+
+ /* Calling set_brk effectively mmaps the pages that we need for the bss and break
+ sections */
+ set_brk(elf_bss, elf_brk);
+
+ padzero(elf_bss);
+
+#if 0
+ printf("(start_brk) %x\n" , info->start_brk);
+ printf("(end_code) %x\n" , info->end_code);
+ printf("(start_code) %x\n" , info->start_code);
+ printf("(end_data) %x\n" , info->end_data);
+ printf("(start_stack) %x\n" , info->start_stack);
+ printf("(brk) %x\n" , info->brk);
+#endif
+
+ if ( info->personality == PER_SVR4 )
+ {
+ /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
+ and some applications "depend" upon this behavior.
+ Since we do not have the power to recompile these, we
+ emulate the SVr4 behavior. Sigh. */
+ mapped_addr = mmap4k(NULL, ALPHA_PAGE_SIZE, PROT_READ | PROT_EXEC,
+ MAP_FIXED | MAP_PRIVATE, -1, 0);
+ }
+
+#ifdef ELF_PLAT_INIT
+ /*
+ * The ABI may specify that certain registers be set up in special
+ * ways (on i386 %edx is the address of a DT_FINI function, for
+ * example. This macro performs whatever initialization to
+ * the regs structure is required.
+ */
+ ELF_PLAT_INIT(regs);
+#endif
+
+
+ info->entry = elf_entry;
+
+ return 0;
+}
+
+
+
+int elf_exec(const char * filename, char ** argv, char ** envp,
+ struct pt_regs * regs, struct image_info *infop)
+{
+ struct linux_binprm bprm;
+ int retval;
+ int i;
+
+ bprm.p = X86_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
+ for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */
+ bprm.page[i] = 0;
+ retval = open(filename, O_RDONLY);
+ if (retval == -1) {
+ perror(filename);
+ exit(-1);
+ /* return retval; */
+ }
+ else {
+ bprm.fd = retval;
+ }
+ bprm.filename = (char *)filename;
+ bprm.sh_bang = 0;
+ bprm.loader = 0;
+ bprm.exec = 0;
+ bprm.dont_iput = 0;
+ bprm.argc = count(argv);
+ bprm.envc = count(envp);
+
+ retval = prepare_binprm(&bprm);
+
+ if(retval>=0) {
+ bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p);
+ bprm.exec = bprm.p;
+ bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p);
+ bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p);
+ if (!bprm.p) {
+ retval = -E2BIG;
+ }
+ }
+
+ if(retval>=0) {
+ retval = load_elf_binary(&bprm,regs,infop);
+ }
+ if(retval>=0) {
+ /* success. Initialize important registers */
+ regs->esp = infop->start_stack;
+ regs->eip = infop->entry;
+ return retval;
+ }
+
+ /* Something went wrong, return the inode and free the argument pages*/
+ for (i=0 ; i<MAX_ARG_PAGES ; i++) {
+ free_page((void *)bprm.page[i]);
+ }
+ return(retval);
+}
+
+
+static int load_aout_interp(void * exptr, int interp_fd)
+{
+ printf("a.out interpreter not yet supported\n");
+ return(0);
+}
+
diff --git a/linux-user/ioctls.h b/linux-user/ioctls.h
new file mode 100644
index 0000000000..49deb09da4
--- /dev/null
+++ b/linux-user/ioctls.h
@@ -0,0 +1,282 @@
+ /* emulated ioctl list */
+
+ IOCTL(TCGETS, IOC_R, MK_PTR(MK_STRUCT(STRUCT_termios)))
+ IOCTL(TCGETS, IOC_W, MK_PTR(MK_STRUCT(STRUCT_termios)))
+ IOCTL(TCSETSF, IOC_W, MK_PTR(MK_STRUCT(STRUCT_termios)))
+ IOCTL(TCSETSW, IOC_W, MK_PTR(MK_STRUCT(STRUCT_termios)))
+ IOCTL(TIOCGWINSZ, IOC_R, MK_PTR(MK_STRUCT(STRUCT_winsize)))
+ IOCTL(TIOCSWINSZ, IOC_W, MK_PTR(MK_STRUCT(STRUCT_winsize)))
+ IOCTL(FIONREAD, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(TCGETA, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(TCSETA, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TCSETAW, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TCSETAF, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TCSBRK, 0, TYPE_INT)
+ IOCTL(TCSBRKP, 0, TYPE_INT)
+ IOCTL(TCXONC, 0, TYPE_INT)
+ IOCTL(TCFLSH, 0, TYPE_INT)
+ IOCTL(TIOCEXCL, 0, TYPE_NULL)
+ IOCTL(TIOCNXCL, 0, TYPE_NULL)
+ IOCTL(TIOCSCTTY, 0, TYPE_INT)
+ IOCTL(TIOCGPGRP, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(TIOCSPGRP, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCOUTQ, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(TIOCSTI, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCMGET, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(TIOCMBIS, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCMBIC, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCMSET, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCGSOFTCAR, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(TIOCSSOFTCAR, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCLINUX, IOC_R | IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCCONS, 0, TYPE_NULL)
+ IOCTL(TIOCGSERIAL, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(TIOCSSERIAL, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCPKT, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(FIONBIO, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCNOTTY, 0, TYPE_NULL)
+ IOCTL(TIOCGETD, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(TIOCSETD, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(FIOCLEX, 0, TYPE_NULL)
+ IOCTL(FIONCLEX, 0, TYPE_NULL)
+ IOCTL(FIOASYNC, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(TIOCGLCKTRMIOS, IOC_R, MK_PTR(MK_STRUCT(STRUCT_termios)))
+ IOCTL(TIOCSLCKTRMIOS, IOC_W, MK_PTR(MK_STRUCT(STRUCT_termios)))
+ IOCTL(TIOCSERCONFIG, 0, TYPE_NULL)
+ IOCTL(TIOCSERGETLSR, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(TIOCSERGETMULTI, IOC_R, MK_PTR(MK_STRUCT(STRUCT_serial_multiport_struct)))
+ IOCTL(TIOCSERSETMULTI, IOC_W, MK_PTR(MK_STRUCT(STRUCT_serial_multiport_struct)))
+ IOCTL(TIOCMIWAIT, 0, TYPE_INT)
+ IOCTL(TIOCGICOUNT, IOC_R, MK_PTR(MK_STRUCT(STRUCT_serial_icounter_struct)))
+
+ IOCTL(BLKROSET, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(BLKROGET, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(BLKRRPART, 0, TYPE_NULL)
+ IOCTL(BLKGETSIZE, IOC_R, MK_PTR(TYPE_ULONG))
+#ifdef BLKGETSIZE64
+ IOCTL(BLKGETSIZE64, IOC_R, MK_PTR(TYPE_ULONGLONG))
+#endif
+ IOCTL(BLKFLSBUF, 0, TYPE_NULL)
+ IOCTL(BLKRASET, 0, TYPE_INT)
+ IOCTL(BLKRAGET, IOC_R, MK_PTR(TYPE_LONG))
+#ifdef FIBMAP
+ IOCTL(FIBMAP, IOC_W | IOC_R, MK_PTR(TYPE_LONG))
+#endif
+#ifdef FIGETBSZ
+ IOCTL(FIGETBSZ, IOC_R, MK_PTR(TYPE_LONG))
+#endif
+
+ IOCTL(SIOCADDRT, IOC_W, MK_PTR(MK_STRUCT(STRUCT_rtentry)))
+ IOCTL(SIOCDELRT, IOC_W, MK_PTR(MK_STRUCT(STRUCT_rtentry)))
+ IOCTL(SIOCGIFNAME, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SIOCGIFFLAGS, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_short_ifreq)))
+ IOCTL(SIOCSIFFLAGS, IOC_W, MK_PTR(MK_STRUCT(STRUCT_short_ifreq)))
+ IOCTL(SIOCGIFADDR, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCSIFADDR, IOC_W, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCGIFBRDADDR, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCSIFBRDADDR, IOC_W, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCGIFDSTADDR, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCSIFDSTADDR, IOC_W, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCGIFNETMASK, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCSIFNETMASK, IOC_W, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCGIFHWADDR, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCSIFHWADDR, IOC_W, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCGIFTXQLEN, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCSIFTXQLEN, IOC_W, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCGIFMETRIC, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_int_ifreq)))
+ IOCTL(SIOCSIFMETRIC, IOC_W, MK_PTR(MK_STRUCT(STRUCT_int_ifreq)))
+ IOCTL(SIOCGIFMTU, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_int_ifreq)))
+ IOCTL(SIOCSIFMTU, IOC_W, MK_PTR(MK_STRUCT(STRUCT_int_ifreq)))
+ IOCTL(SIOCGIFMAP, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_ifmap_ifreq)))
+ IOCTL(SIOCSIFMAP, IOC_W, MK_PTR(MK_STRUCT(STRUCT_ifmap_ifreq)))
+ IOCTL(SIOCGIFSLAVE, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_char_ifreq)))
+ IOCTL(SIOCSIFSLAVE, IOC_W, MK_PTR(MK_STRUCT(STRUCT_char_ifreq)))
+ IOCTL(SIOCGIFMEM, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_ptr_ifreq)))
+ IOCTL(SIOCSIFMEM, IOC_W, MK_PTR(MK_STRUCT(STRUCT_ptr_ifreq)))
+ IOCTL(SIOCADDMULTI, IOC_W, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCDELMULTI, IOC_W, MK_PTR(MK_STRUCT(STRUCT_sockaddr_ifreq)))
+ IOCTL(SIOCSIFLINK, 0, TYPE_NULL)
+ IOCTL(SIOCGIFCONF, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_ifconf)))
+ IOCTL(SIOCGIFENCAP, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SIOCSIFENCAP, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SIOCDARP, IOC_W, MK_PTR(MK_STRUCT(STRUCT_arpreq)))
+ IOCTL(SIOCSARP, IOC_W, MK_PTR(MK_STRUCT(STRUCT_arpreq)))
+ IOCTL(SIOCGARP, IOC_R, MK_PTR(MK_STRUCT(STRUCT_arpreq)))
+ IOCTL(SIOCDRARP, IOC_W, MK_PTR(MK_STRUCT(STRUCT_arpreq)))
+ IOCTL(SIOCSRARP, IOC_W, MK_PTR(MK_STRUCT(STRUCT_arpreq)))
+ IOCTL(SIOCGRARP, IOC_R, MK_PTR(MK_STRUCT(STRUCT_arpreq)))
+
+ IOCTL(CDROMPAUSE, 0, TYPE_NULL)
+ IOCTL(CDROMSTART, 0, TYPE_NULL)
+ IOCTL(CDROMSTOP, 0, TYPE_NULL)
+ IOCTL(CDROMRESUME, 0, TYPE_NULL)
+ IOCTL(CDROMEJECT, 0, TYPE_NULL)
+ IOCTL(CDROMEJECT_SW, 0, TYPE_INT)
+ IOCTL(CDROMCLOSETRAY, 0, TYPE_NULL)
+ IOCTL(CDROMRESET, 0, TYPE_NULL)
+ IOCTL(CDROMPLAYMSF, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(CDROMPLAYTRKIND, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(CDROMREADTOCHDR, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(CDROMREADTOCENTRY, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(CDROMVOLCTRL, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(CDROMSUBCHNL, IOC_RW, MK_PTR(TYPE_INT))
+ /* XXX: incorrect (need specific handling) */
+ IOCTL(CDROMREADAUDIO, IOC_W, MK_PTR(MK_STRUCT(STRUCT_cdrom_read_audio)))
+ IOCTL(CDROMREADCOOKED, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(CDROMREADRAW, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(CDROMREADMODE1, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(CDROMREADMODE2, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(CDROMREADALL, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(CDROMMULTISESSION, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(CDROM_GET_UPC, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(CDROMVOLREAD, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(CDROMSEEK, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(CDROMPLAYBLK, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(CDROM_MEDIA_CHANGED, 0, TYPE_NULL)
+ IOCTL(CDROM_SET_OPTIONS, 0, TYPE_INT)
+ IOCTL(CDROM_CLEAR_OPTIONS, 0, TYPE_INT)
+ IOCTL(CDROM_SELECT_SPEED, 0, TYPE_INT)
+ IOCTL(CDROM_SELECT_DISC, 0, TYPE_INT)
+ IOCTL(CDROM_DRIVE_STATUS, 0, TYPE_NULL)
+ IOCTL(CDROM_DISC_STATUS, 0, TYPE_NULL)
+ IOCTL(CDROMAUDIOBUFSIZ, 0, TYPE_INT)
+
+ IOCTL(SNDCTL_COPR_HALT, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_COPR_LOAD, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_COPR_RCODE, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_COPR_RCVMSG, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_COPR_RDATA, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_COPR_RESET, 0, TYPE_NULL)
+ IOCTL(SNDCTL_COPR_RUN, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_COPR_SENDMSG, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_COPR_WCODE, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_COPR_WDATA, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_CHANNELS, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_GETBLKSIZE, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_GETCAPS, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_GETFMTS, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_GETIPTR, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_GETISPACE, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_GETOPTR, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_GETOSPACE, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_GETTRIGGER, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_MAPINBUF, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_MAPOUTBUF, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_NONBLOCK, 0, TYPE_NULL)
+ IOCTL(SNDCTL_DSP_POST, 0, TYPE_NULL)
+ IOCTL(SNDCTL_DSP_RESET, 0, TYPE_NULL)
+ IOCTL(SNDCTL_DSP_SETDUPLEX, 0, TYPE_NULL)
+ IOCTL(SNDCTL_DSP_SETFMT, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_SETFRAGMENT, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_SETSYNCRO, 0, TYPE_NULL)
+ IOCTL(SNDCTL_DSP_SETTRIGGER, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_SPEED, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_STEREO, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_SUBDIVIDE, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_DSP_SYNC, 0, TYPE_NULL)
+ IOCTL(SNDCTL_FM_4OP_ENABLE, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_FM_LOAD_INSTR, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_MIDI_INFO, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_MIDI_MPUCMD, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_MIDI_MPUMODE, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_MIDI_PRETIME, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_CTRLRATE, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_GETINCOUNT, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_GETOUTCOUNT, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_NRMIDIS, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_NRSYNTHS, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_OUTOFBAND, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_PANIC, 0, TYPE_NULL)
+ IOCTL(SNDCTL_SEQ_PERCMODE, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_RESET, 0, TYPE_NULL)
+ IOCTL(SNDCTL_SEQ_RESETSAMPLES, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_SYNC, 0, TYPE_NULL)
+ IOCTL(SNDCTL_SEQ_TESTMIDI, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SEQ_THRESHOLD, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SYNTH_INFO, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_SYNTH_MEMAVL, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_TMR_CONTINUE, 0, TYPE_NULL)
+ IOCTL(SNDCTL_TMR_METRONOME, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_TMR_SELECT, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_TMR_SOURCE, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_TMR_START, 0, TYPE_NULL)
+ IOCTL(SNDCTL_TMR_STOP, 0, TYPE_NULL)
+ IOCTL(SNDCTL_TMR_TEMPO, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SNDCTL_TMR_TIMEBASE, IOC_RW, MK_PTR(TYPE_INT))
+
+ IOCTL(SOUND_PCM_WRITE_FILTER, IOC_W | IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_PCM_READ_RATE, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_PCM_READ_CHANNELS, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_PCM_READ_BITS, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_PCM_READ_FILTER, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_INFO, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_ACCESS, 0, TYPE_PTRVOID)
+ IOCTL(SOUND_MIXER_PRIVATE1, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_PRIVATE2, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_PRIVATE3, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_PRIVATE4, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_PRIVATE5, IOC_RW, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_VOLUME, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_BASS, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_TREBLE, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_SYNTH, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_PCM, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_SPEAKER, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_LINE, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_MIC, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_CD, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_IMIX, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_ALTPCM, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_RECLEV, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_IGAIN, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_OGAIN, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_LINE1, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_LINE2, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_LINE3, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_MUTE, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_ENHANCE, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_LOUD, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_RECSRC, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_DEVMASK, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_RECMASK, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_STEREODEVS, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_READ_CAPS, IOC_R, MK_PTR(TYPE_INT))
+
+ IOCTL(SOUND_MIXER_WRITE_VOLUME, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_BASS, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_TREBLE, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_SYNTH, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_PCM, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_SPEAKER, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_LINE, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_MIC, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_CD, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_IMIX, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_ALTPCM, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_RECLEV, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_IGAIN, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_OGAIN, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_LINE1, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_LINE2, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_LINE3, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_MUTE, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_ENHANCE, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_LOUD, IOC_W, MK_PTR(TYPE_INT))
+ IOCTL(SOUND_MIXER_WRITE_RECSRC, IOC_W, MK_PTR(TYPE_INT))
+
+ IOCTL(HDIO_GETGEO, IOC_R, MK_PTR(MK_STRUCT(STRUCT_hd_geometry)))
+ IOCTL(HDIO_GET_UNMASKINTR, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(HDIO_GET_MULTCOUNT, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(HDIO_GET_IDENTITY, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(HDIO_GET_KEEPSETTINGS, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(HDIO_GET_NOWERR, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(HDIO_GET_DMA, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(HDIO_GET_32BIT, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(HDIO_DRIVE_CMD, IOC_R, MK_PTR(TYPE_INT))
+ IOCTL(HDIO_SET_UNMASKINTR, 0, TYPE_INT)
+ IOCTL(HDIO_SET_MULTCOUNT, 0, TYPE_INT)
+ IOCTL(HDIO_SET_KEEPSETTINGS, 0, TYPE_INT)
+ IOCTL(HDIO_SET_NOWERR, 0, TYPE_INT)
+ IOCTL(HDIO_SET_DMA, 0, TYPE_INT)
+ IOCTL(HDIO_SET_32BIT, 0, TYPE_INT)
+ IOCTL(HDIO_SET_PIO_MODE, 0, TYPE_INT)
diff --git a/linux-user/main.c b/linux-user/main.c
new file mode 100644
index 0000000000..e3950835e3
--- /dev/null
+++ b/linux-user/main.c
@@ -0,0 +1,310 @@
+/*
+ * emu main
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <elf.h>
+#include <endian.h>
+#include <errno.h>
+
+#include "gemu.h"
+
+#include "i386/hsw_interp.h"
+
+unsigned long x86_stack_size;
+unsigned long stktop;
+
+void gemu_log(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+}
+
+/* virtual x86 CPU stuff */
+
+extern int invoke_code16(Interp_ENV *, int, int);
+extern int invoke_code32(Interp_ENV *, int);
+extern char *e_print_cpuemu_regs(ENVPARAMS, int is32);
+extern char *e_emu_disasm(ENVPARAMS, unsigned char *org, int is32);
+extern void init_npu(void);
+
+Interp_ENV env_global;
+Interp_ENV *envp_global;
+
+QWORD EMUtime = 0;
+
+int CEmuStat = 0;
+
+long instr_count;
+
+/* who will initialize this? */
+unsigned long io_bitmap[IO_BITMAP_SIZE+1];
+
+/* debug flag, 0=disable 1..9=level */
+int d_emu = 0;
+
+unsigned long CRs[5] =
+{
+ 0x00000013, /* valid bits: 0xe005003f */
+ 0x00000000, /* invalid */
+ 0x00000000,
+ 0x00000000,
+ 0x00000000
+};
+
+/*
+ * DR0-3 = linear address of breakpoint 0-3
+ * DR4=5 = reserved
+ * DR6 b0-b3 = BP active
+ * b13 = BD
+ * b14 = BS
+ * b15 = BT
+ * DR7 b0-b1 = G:L bp#0
+ * b2-b3 = G:L bp#1
+ * b4-b5 = G:L bp#2
+ * b6-b7 = G:L bp#3
+ * b8-b9 = GE:LE
+ * b13 = GD
+ * b16-19= LLRW bp#0 LL=00(1),01(2),11(4)
+ * b20-23= LLRW bp#1 RW=00(x),01(w),11(rw)
+ * b24-27= LLRW bp#2
+ * b28-31= LLRW bp#3
+ */
+unsigned long DRs[8] =
+{
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0x00000000,
+ 0xffff1ff0,
+ 0x00000400,
+ 0xffff1ff0,
+ 0x00000400
+};
+
+unsigned long TRs[2] =
+{
+ 0x00000000,
+ 0x00000000
+};
+
+void FatalAppExit(UINT wAction, LPCSTR lpText)
+{
+ fprintf(stderr, "Fatal error '%s' in CPU\n", lpText);
+ exit(1);
+}
+
+int e_debug_check(unsigned char *PC)
+{
+ register unsigned long d7 = DRs[7];
+
+ if (d7&0x03) {
+ if (d7&0x30000) return 0; /* only execute(00) bkp */
+ if ((long)PC==DRs[0]) {
+ e_printf("DBRK: DR0 hit at %p\n",PC);
+ DRs[6] |= 1;
+ return 1;
+ }
+ }
+ if (d7&0x0c) {
+ if (d7&0x300000) return 0;
+ if ((long)PC==DRs[1]) {
+ e_printf("DBRK: DR1 hit at %p\n",PC);
+ DRs[6] |= 2;
+ return 1;
+ }
+ }
+ if (d7&0x30) {
+ if (d7&0x3000000) return 0;
+ if ((long)PC==DRs[2]) {
+ e_printf("DBRK: DR2 hit at %p\n",PC);
+ DRs[6] |= 4;
+ return 1;
+ }
+ }
+ if (d7&0xc0) {
+ if (d7&0x30000000) return 0;
+ if ((long)PC==DRs[3]) {
+ e_printf("DBRK: DR3 hit at %p\n",PC);
+ DRs[6] |= 8;
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/* Debug stuff */
+void logstr(unsigned long mask, const char *fmt,...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+}
+
+/* unconditional message into debug log and stderr */
+#undef error
+void error(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ exit(1);
+}
+
+int PortIO(DWORD port, DWORD value, UINT size, BOOL is_write)
+{
+ fprintf(stderr, "IO: %s port=0x%lx value=0x%lx size=%d",
+ is_write ? "write" : "read", port, value, size);
+ return value;
+}
+
+void LogProcName(WORD wSel, WORD wOff, WORD wAction)
+{
+
+}
+
+void INT_handler(int num, void *env)
+{
+ fprintf(stderr, "EM86: int %d\n", num);
+}
+
+/***********************************************************/
+
+/* XXX: currently we use LDT entries */
+#define __USER_CS (0x23|4)
+#define __USER_DS (0x2B|4)
+
+void usage(void)
+{
+ printf("gemu version 0.1, Copyright (c) 2003 Fabrice Bellard\n"
+ "usage: gemu program [arguments...]\n"
+ "Linux x86 emulator\n"
+ );
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ const char *filename;
+ struct pt_regs regs1, *regs = &regs1;
+ struct image_info info1, *info = &info1;
+ Interp_ENV *env;
+
+ if (argc <= 1)
+ usage();
+
+ filename = argv[1];
+
+ /* Zero out regs */
+ memset(regs, 0, sizeof(struct pt_regs));
+
+ /* Zero out image_info */
+ memset(info, 0, sizeof(struct image_info));
+
+ if(elf_exec(filename, argv+1, __environ, regs, info) != 0) {
+ printf("Error loading %s\n", filename);
+ exit(1);
+ }
+
+#if 0
+ printf("start_brk 0x%08lx\n" , info->start_brk);
+ printf("end_code 0x%08lx\n" , info->end_code);
+ printf("start_code 0x%08lx\n" , info->start_code);
+ printf("end_data 0x%08lx\n" , info->end_data);
+ printf("start_stack 0x%08lx\n" , info->start_stack);
+ printf("brk 0x%08lx\n" , info->brk);
+ printf("esp 0x%08lx\n" , regs->esp);
+ printf("eip 0x%08lx\n" , regs->eip);
+#endif
+
+ target_set_brk((char *)info->brk);
+ syscall_init();
+
+ env = &env_global;
+ envp_global = env;
+ memset(env, 0, sizeof(Interp_ENV));
+
+ env->rax.e = regs->eax;
+ env->rbx.e = regs->ebx;
+ env->rcx.e = regs->ecx;
+ env->rdx.e = regs->edx;
+ env->rsi.esi = regs->esi;
+ env->rdi.edi = regs->edi;
+ env->rbp.ebp = regs->ebp;
+ env->rsp.esp = regs->esp;
+ env->cs.cs = __USER_CS;
+ env->ds.ds = __USER_DS;
+ env->es.es = __USER_DS;
+ env->ss.ss = __USER_DS;
+ env->fs.fs = __USER_DS;
+ env->gs.gs = __USER_DS;
+ env->trans_addr = regs->eip;
+
+ LDT[__USER_CS >> 3].w86Flags = DF_PRESENT | DF_PAGES | DF_32;
+ LDT[__USER_CS >> 3].dwSelLimit = 0xfffff;
+ LDT[__USER_CS >> 3].lpSelBase = NULL;
+
+ LDT[__USER_DS >> 3].w86Flags = DF_PRESENT | DF_PAGES | DF_32;
+ LDT[__USER_DS >> 3].dwSelLimit = 0xfffff;
+ LDT[__USER_DS >> 3].lpSelBase = NULL;
+ init_npu();
+
+ for(;;) {
+ int err;
+ uint8_t *pc;
+
+ err = invoke_code32(env, -1);
+ env->trans_addr = env->return_addr;
+ pc = env->seg_regs[0] + env->trans_addr;
+ switch(err) {
+ case EXCP0D_GPF:
+ if (pc[0] == 0xcd && pc[1] == 0x80) {
+ /* syscall */
+ env->trans_addr += 2;
+ env->rax.e = do_syscall(env->rax.e,
+ env->rbx.e,
+ env->rcx.e,
+ env->rdx.e,
+ env->rsi.esi,
+ env->rdi.edi,
+ env->rbp.ebp);
+ } else {
+ goto trap_error;
+ }
+ break;
+ default:
+ trap_error:
+ fprintf(stderr, "GEMU: Unknown error %d, aborting\n", err);
+ d_emu = 9;
+ fprintf(stderr, "%s\n%s\n",
+ e_print_cpuemu_regs(env, 1),
+ e_emu_disasm(env,pc,1));
+ abort();
+ }
+ }
+ return 0;
+}
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
new file mode 100644
index 0000000000..fa40d4d845
--- /dev/null
+++ b/linux-user/qemu.h
@@ -0,0 +1,57 @@
+#ifndef GEMU_H
+#define GEMU_H
+
+#include "thunk.h"
+
+struct pt_regs {
+ long ebx;
+ long ecx;
+ long edx;
+ long esi;
+ long edi;
+ long ebp;
+ long eax;
+ int xds;
+ int xes;
+ long orig_eax;
+ long eip;
+ int xcs;
+ long eflags;
+ long esp;
+ int xss;
+};
+
+/* This struct is used to hold certain information about the image.
+ * Basically, it replicates in user space what would be certain
+ * task_struct fields in the kernel
+ */
+struct image_info {
+ unsigned long start_code;
+ unsigned long end_code;
+ unsigned long end_data;
+ unsigned long start_brk;
+ unsigned long brk;
+ unsigned long start_mmap;
+ unsigned long mmap;
+ unsigned long rss;
+ unsigned long start_stack;
+ unsigned long arg_start;
+ unsigned long arg_end;
+ unsigned long env_start;
+ unsigned long env_end;
+ unsigned long entry;
+ int personality;
+};
+
+int elf_exec(const char * filename, char ** argv, char ** envp,
+ struct pt_regs * regs, struct image_info *infop);
+
+void target_set_brk(char *new_brk);
+void syscall_init(void);
+long do_syscall(int num, long arg1, long arg2, long arg3,
+ long arg4, long arg5, long arg6);
+void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
+
+
+
+#endif
diff --git a/linux-user/signal.c b/linux-user/signal.c
new file mode 100644
index 0000000000..2e0d599553
--- /dev/null
+++ b/linux-user/signal.c
@@ -0,0 +1,105 @@
+/*
+ * Emulation of Linux signal handling
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <signal.h>
+#include <sys/ucontext.h>
+
+/* Algorithm strongly inspired from em86 : we queue the signals so
+ that we can handle them at precise points in the emulated code. */
+
+struct emulated_sigaction {
+ struct target_sigaction sa;
+ int nb_pending;
+ struct target_siginfo info;
+};
+
+struct emulated_sigaction sigact_table[NSIG];
+int signal_pending;
+
+static inline int host_to_target_signal(int sig)
+{
+ return sig;
+}
+
+static inline int target_to_host_signal(int sig)
+{
+ return sig;
+}
+
+void signal_init(void)
+{
+ struct sigaction act;
+ int i;
+
+ /* set all host signal handlers */
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = SA_SIGINFO;
+ act.sa_sigaction = host_signal_handler;
+ for(i = 1; i < NSIG; i++) {
+ sigaction(i, &sa, NULL);
+ }
+
+ memset(sigact_table, 0, sizeof(sigact_table));
+}
+
+static void host_signal_handler(int host_signum, siginfo_t *info,
+ void *puc)
+{
+ struct ucontext *uc = puc;
+ int signum;
+ /* get target signal number */
+ signum = host_to_target(host_signum);
+ if (signum >= TARGET_NSIG)
+ return;
+ /* we save the old mask */
+
+
+}
+
+
+void process_pending_signals(void)
+{
+ int signum;
+ target_ulong _sa_handler;
+
+ struct emulated_sigaction *esig;
+
+ if (!signal_pending)
+ return;
+
+ esig = sigact_table;
+ for(signum = 1; signum < TARGET_NSIG; signum++) {
+ if (esig->nb_pending != 0)
+ goto handle_signal;
+ esig++;
+ }
+ /* if no signal is pending, just return */
+ signal_pending = 0;
+ return;
+ handle_signal:
+ _sa_handler = esig->sa._sa_handler;
+ if (_sa_handler == TARGET_SIG_DFL) {
+ /* default handling
+ }
+
+
+}
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
new file mode 100644
index 0000000000..d5909b2215
--- /dev/null
+++ b/linux-user/syscall.c
@@ -0,0 +1,1349 @@
+/*
+ * Linux syscalls
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <elf.h>
+#include <endian.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+#include <sys/resource.h>
+#include <sys/mman.h>
+#include <sys/swap.h>
+#include <signal.h>
+#include <sched.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+#include <sys/user.h>
+
+#define termios host_termios
+#define winsize host_winsize
+#define termio host_termio
+
+#include <linux/termios.h>
+#include <linux/unistd.h>
+#include <linux/utsname.h>
+#include <linux/cdrom.h>
+#include <linux/hdreg.h>
+#include <linux/soundcard.h>
+
+#include "gemu.h"
+
+#define DEBUG
+
+#ifndef PAGE_SIZE
+#define PAGE_SIZE 4096
+#define PAGE_MASK ~(PAGE_SIZE - 1)
+#endif
+
+struct dirent {
+ long d_ino;
+ long d_off;
+ unsigned short d_reclen;
+ char d_name[256]; /* We must not include limits.h! */
+};
+
+#include "syscall_defs.h"
+
+#ifdef TARGET_I386
+#include "syscall-i386.h"
+#endif
+
+#define __NR_sys_uname __NR_uname
+#define __NR_sys_getcwd __NR_getcwd
+#define __NR_sys_statfs __NR_statfs
+#define __NR_sys_fstatfs __NR_fstatfs
+
+_syscall0(int, gettid)
+_syscall1(int,sys_uname,struct new_utsname *,buf)
+_syscall2(int,sys_getcwd,char *,buf,size_t,size)
+_syscall3(int, getdents, uint, fd, struct dirent *, dirp, uint, count);
+_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
+ loff_t *, res, uint, wh);
+_syscall2(int,sys_statfs,const char *,path,struct statfs *,buf)
+_syscall2(int,sys_fstatfs,int,fd,struct statfs *,buf)
+
+static inline long get_errno(long ret)
+{
+ if (ret == -1)
+ return -errno;
+ else
+ return ret;
+}
+
+static inline int is_error(long ret)
+{
+ return (unsigned long)ret >= (unsigned long)(-4096);
+}
+
+static char *target_brk;
+static char *target_original_brk;
+
+void target_set_brk(char *new_brk)
+{
+ target_brk = new_brk;
+ target_original_brk = new_brk;
+}
+
+static long do_brk(char *new_brk)
+{
+ char *brk_page;
+ long mapped_addr;
+ int new_alloc_size;
+
+ if (!new_brk)
+ return (long)target_brk;
+ if (new_brk < target_original_brk)
+ return -ENOMEM;
+
+ brk_page = (char *)(((unsigned long)target_brk + PAGE_SIZE - 1) & PAGE_MASK);
+
+ /* If the new brk is less than this, set it and we're done... */
+ if (new_brk < brk_page) {
+ target_brk = new_brk;
+ return (long)target_brk;
+ }
+
+ /* We need to allocate more memory after the brk... */
+ new_alloc_size = ((new_brk - brk_page + 1)+(PAGE_SIZE-1)) & PAGE_MASK;
+ mapped_addr = get_errno((long)mmap((caddr_t)brk_page, new_alloc_size,
+ PROT_READ|PROT_WRITE,
+ MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
+
+ if (is_error(mapped_addr)) {
+ return mapped_addr;
+ } else {
+ target_brk = new_brk;
+ return (long)target_brk;
+ }
+}
+
+static inline fd_set *target_to_host_fds(fd_set *fds,
+ target_long *target_fds, int n)
+{
+#if !defined(BSWP_NEEDED) && !defined(WORD_BIGENDIAN)
+ return (fd_set *)target_fds;
+#else
+ int i, b;
+ if (target_fds) {
+ FD_ZERO(fds);
+ for(i = 0;i < n; i++) {
+ b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
+ (i & (TARGET_LONG_BITS - 1))) & 1;
+ if (b)
+ FD_SET(i, fds);
+ }
+ return fds;
+ } else {
+ return NULL;
+ }
+#endif
+}
+
+static inline void host_to_target_fds(target_long *target_fds,
+ fd_set *fds, int n)
+{
+#if !defined(BSWP_NEEDED) && !defined(WORD_BIGENDIAN)
+ /* nothing to do */
+#else
+ int i, nw, j, k;
+ target_long v;
+
+ if (target_fds) {
+ nw = n / TARGET_LONG_BITS;
+ k = 0;
+ for(i = 0;i < nw; i++) {
+ v = 0;
+ for(j = 0; j < TARGET_LONG_BITS; j++) {
+ v |= ((FD_ISSET(k, fds) != 0) << j);
+ k++;
+ }
+ target_fds[i] = tswapl(v);
+ }
+ }
+#endif
+}
+
+/* XXX: incorrect for some archs */
+static void host_to_target_old_sigset(target_ulong *old_sigset,
+ const sigset_t *sigset)
+{
+ *old_sigset = tswap32(*(unsigned long *)sigset & 0xffffffff);
+}
+
+static void target_to_host_old_sigset(sigset_t *sigset,
+ const target_ulong *old_sigset)
+{
+ sigemptyset(sigset);
+ *(unsigned long *)sigset = tswapl(*old_sigset);
+}
+
+
+static long do_select(long n,
+ target_long *target_rfds, target_long *target_wfds,
+ target_long *target_efds, struct target_timeval *target_tv)
+{
+ fd_set rfds, wfds, efds;
+ fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
+ struct timeval tv, *tv_ptr;
+ long ret;
+
+ rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
+ wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
+ efds_ptr = target_to_host_fds(&efds, target_efds, n);
+
+ if (target_tv) {
+ tv.tv_sec = tswapl(target_tv->tv_sec);
+ tv.tv_usec = tswapl(target_tv->tv_usec);
+ tv_ptr = &tv;
+ } else {
+ tv_ptr = NULL;
+ }
+ ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
+ if (!is_error(ret)) {
+ host_to_target_fds(target_rfds, rfds_ptr, n);
+ host_to_target_fds(target_wfds, wfds_ptr, n);
+ host_to_target_fds(target_efds, efds_ptr, n);
+
+ if (target_tv) {
+ target_tv->tv_sec = tswapl(tv.tv_sec);
+ target_tv->tv_usec = tswapl(tv.tv_usec);
+ }
+ }
+ return ret;
+}
+
+static long do_socketcall(int num, long *vptr)
+{
+ long ret;
+
+ switch(num) {
+ case SOCKOP_socket:
+ ret = get_errno(socket(vptr[0], vptr[1], vptr[2]));
+ break;
+ case SOCKOP_bind:
+ ret = get_errno(bind(vptr[0], (struct sockaddr *)vptr[1], vptr[2]));
+ break;
+ case SOCKOP_connect:
+ ret = get_errno(connect(vptr[0], (struct sockaddr *)vptr[1], vptr[2]));
+ break;
+ case SOCKOP_listen:
+ ret = get_errno(listen(vptr[0], vptr[1]));
+ break;
+ case SOCKOP_accept:
+ {
+ socklen_t size;
+ size = tswap32(*(int32_t *)vptr[2]);
+ ret = get_errno(accept(vptr[0], (struct sockaddr *)vptr[1], &size));
+ if (!is_error(ret))
+ *(int32_t *)vptr[2] = size;
+ }
+ break;
+ case SOCKOP_getsockname:
+ {
+ socklen_t size;
+ size = tswap32(*(int32_t *)vptr[2]);
+ ret = get_errno(getsockname(vptr[0], (struct sockaddr *)vptr[1], &size));
+ if (!is_error(ret))
+ *(int32_t *)vptr[2] = size;
+ }
+ break;
+ case SOCKOP_getpeername:
+ {
+ socklen_t size;
+ size = tswap32(*(int32_t *)vptr[2]);
+ ret = get_errno(getpeername(vptr[0], (struct sockaddr *)vptr[1], &size));
+ if (!is_error(ret))
+ *(int32_t *)vptr[2] = size;
+ }
+ break;
+ case SOCKOP_socketpair:
+ {
+ int tab[2];
+ int32_t *target_tab = (int32_t *)vptr[3];
+ ret = get_errno(socketpair(vptr[0], vptr[1], vptr[2], tab));
+ if (!is_error(ret)) {
+ target_tab[0] = tswap32(tab[0]);
+ target_tab[1] = tswap32(tab[1]);
+ }
+ }
+ break;
+ case SOCKOP_send:
+ ret = get_errno(send(vptr[0], (void *)vptr[1], vptr[2], vptr[3]));
+ break;
+ case SOCKOP_recv:
+ ret = get_errno(recv(vptr[0], (void *)vptr[1], vptr[2], vptr[3]));
+ break;
+ case SOCKOP_sendto:
+ ret = get_errno(sendto(vptr[0], (void *)vptr[1], vptr[2], vptr[3],
+ (struct sockaddr *)vptr[4], vptr[5]));
+ break;
+ case SOCKOP_recvfrom:
+ {
+ socklen_t size;
+ size = tswap32(*(int32_t *)vptr[5]);
+ ret = get_errno(recvfrom(vptr[0], (void *)vptr[1], vptr[2],
+ vptr[3], (struct sockaddr *)vptr[4], &size));
+ if (!is_error(ret))
+ *(int32_t *)vptr[5] = size;
+ }
+ break;
+ case SOCKOP_shutdown:
+ ret = get_errno(shutdown(vptr[0], vptr[1]));
+ break;
+ case SOCKOP_sendmsg:
+ case SOCKOP_recvmsg:
+ case SOCKOP_setsockopt:
+ case SOCKOP_getsockopt:
+ default:
+ gemu_log("Unsupported socketcall: %d\n", num);
+ ret = -ENOSYS;
+ break;
+ }
+ return ret;
+}
+
+/* kernel structure types definitions */
+#define IFNAMSIZ 16
+
+#define STRUCT(name, list...) STRUCT_ ## name,
+#define STRUCT_SPECIAL(name) STRUCT_ ## name,
+enum {
+#include "syscall_types.h"
+};
+#undef STRUCT
+#undef STRUCT_SPECIAL
+
+#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
+#define STRUCT_SPECIAL(name)
+#include "syscall_types.h"
+#undef STRUCT
+#undef STRUCT_SPECIAL
+
+typedef struct IOCTLEntry {
+ int target_cmd;
+ int host_cmd;
+ const char *name;
+ int access;
+ const argtype arg_type[3];
+} IOCTLEntry;
+
+#define IOC_R 0x0001
+#define IOC_W 0x0002
+#define IOC_RW (IOC_R | IOC_W)
+
+#define MAX_STRUCT_SIZE 4096
+
+const IOCTLEntry ioctl_entries[] = {
+#define IOCTL(cmd, access, types...) \
+ { TARGET_ ## cmd, cmd, #cmd, access, { types } },
+#include "ioctls.h"
+ { 0, 0, },
+};
+
+static long do_ioctl(long fd, long cmd, long arg)
+{
+ const IOCTLEntry *ie;
+ const argtype *arg_type;
+ long ret;
+ uint8_t buf_temp[MAX_STRUCT_SIZE];
+
+ ie = ioctl_entries;
+ for(;;) {
+ if (ie->target_cmd == 0) {
+ gemu_log("Unsupported ioctl: cmd=0x%04lx\n", cmd);
+ return -ENOSYS;
+ }
+ if (ie->target_cmd == cmd)
+ break;
+ ie++;
+ }
+ arg_type = ie->arg_type;
+ // gemu_log("ioctl: cmd=0x%04lx (%s)\n", cmd, ie->name);
+ switch(arg_type[0]) {
+ case TYPE_NULL:
+ /* no argument */
+ ret = get_errno(ioctl(fd, ie->host_cmd));
+ break;
+ case TYPE_PTRVOID:
+ case TYPE_INT:
+ /* int argment */
+ ret = get_errno(ioctl(fd, ie->host_cmd, arg));
+ break;
+ case TYPE_PTR:
+ arg_type++;
+ switch(ie->access) {
+ case IOC_R:
+ ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
+ if (!is_error(ret)) {
+ thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
+ }
+ break;
+ case IOC_W:
+ thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
+ ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
+ break;
+ default:
+ case IOC_RW:
+ thunk_convert(buf_temp, (void *)arg, arg_type, THUNK_HOST);
+ ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
+ if (!is_error(ret)) {
+ thunk_convert((void *)arg, buf_temp, arg_type, THUNK_TARGET);
+ }
+ break;
+ }
+ break;
+ default:
+ gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n", cmd, arg_type[0]);
+ ret = -ENOSYS;
+ break;
+ }
+ return ret;
+}
+
+bitmask_transtbl iflag_tbl[] = {
+ { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
+ { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
+ { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
+ { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
+ { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
+ { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
+ { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
+ { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
+ { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
+ { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
+ { TARGET_IXON, TARGET_IXON, IXON, IXON },
+ { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
+ { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
+ { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
+ { 0, 0, 0, 0 }
+};
+
+bitmask_transtbl oflag_tbl[] = {
+ { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
+ { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
+ { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
+ { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
+ { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
+ { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
+ { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
+ { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
+ { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
+ { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
+ { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
+ { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
+ { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
+ { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
+ { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
+ { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
+ { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
+ { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
+ { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
+ { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
+ { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
+ { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
+ { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
+ { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
+ { 0, 0, 0, 0 }
+};
+
+bitmask_transtbl cflag_tbl[] = {
+ { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
+ { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
+ { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
+ { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
+ { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
+ { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
+ { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
+ { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
+ { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
+ { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
+ { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
+ { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
+ { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
+ { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
+ { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
+ { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
+ { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
+ { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
+ { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
+ { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
+ { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
+ { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
+ { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
+ { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
+ { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
+ { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
+ { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
+ { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
+ { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
+ { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
+ { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
+ { 0, 0, 0, 0 }
+};
+
+bitmask_transtbl lflag_tbl[] = {
+ { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
+ { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
+ { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
+ { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
+ { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
+ { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
+ { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
+ { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
+ { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
+ { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
+ { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
+ { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
+ { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
+ { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
+ { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
+ { 0, 0, 0, 0 }
+};
+
+static void target_to_host_termios (void *dst, const void *src)
+{
+ struct host_termios *host = dst;
+ const struct target_termios *target = src;
+
+ host->c_iflag =
+ target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
+ host->c_oflag =
+ target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
+ host->c_cflag =
+ target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
+ host->c_lflag =
+ target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
+ host->c_line = target->c_line;
+
+ host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
+ host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
+ host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
+ host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
+ host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
+ host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
+ host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
+ host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
+ host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
+ host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
+ host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
+ host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
+ host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
+ host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
+ host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
+ host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
+ host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
+}
+
+static void host_to_target_termios (void *dst, const void *src)
+{
+ struct target_termios *target = dst;
+ const struct host_termios *host = src;
+
+ target->c_iflag =
+ tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
+ target->c_oflag =
+ tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
+ target->c_cflag =
+ tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
+ target->c_lflag =
+ tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
+ target->c_line = host->c_line;
+
+ target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
+ target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
+ target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
+ target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
+ target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
+ target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
+ target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
+ target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
+ target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
+ target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
+ target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
+ target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
+ target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
+ target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
+ target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
+ target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
+ target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
+}
+
+StructEntry struct_termios_def = {
+ .convert = { host_to_target_termios, target_to_host_termios },
+ .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
+ .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
+};
+
+void syscall_init(void)
+{
+#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
+#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
+#include "syscall_types.h"
+#undef STRUCT
+#undef STRUCT_SPECIAL
+}
+
+long do_syscall(int num, long arg1, long arg2, long arg3,
+ long arg4, long arg5, long arg6)
+{
+ long ret;
+ struct stat st;
+ struct statfs *stfs;
+
+ // gemu_log("syscall %d\n", num);
+ switch(num) {
+ case TARGET_NR_exit:
+ _exit(arg1);
+ ret = 0; /* avoid warning */
+ break;
+ case TARGET_NR_read:
+ ret = get_errno(read(arg1, (void *)arg2, arg3));
+ break;
+ case TARGET_NR_write:
+ ret = get_errno(write(arg1, (void *)arg2, arg3));
+ break;
+ case TARGET_NR_open:
+ ret = get_errno(open((const char *)arg1, arg2, arg3));
+ break;
+ case TARGET_NR_close:
+ ret = get_errno(close(arg1));
+ break;
+ case TARGET_NR_brk:
+ ret = do_brk((char *)arg1);
+ break;
+ case TARGET_NR_fork:
+ ret = get_errno(fork());
+ break;
+ case TARGET_NR_waitpid:
+ {
+ int *status = (int *)arg2;
+ ret = get_errno(waitpid(arg1, status, arg3));
+ if (!is_error(ret) && status)
+ tswapls((long *)&status);
+ }
+ break;
+ case TARGET_NR_creat:
+ ret = get_errno(creat((const char *)arg1, arg2));
+ break;
+ case TARGET_NR_link:
+ ret = get_errno(link((const char *)arg1, (const char *)arg2));
+ break;
+ case TARGET_NR_unlink:
+ ret = get_errno(unlink((const char *)arg1));
+ break;
+ case TARGET_NR_execve:
+ ret = get_errno(execve((const char *)arg1, (void *)arg2, (void *)arg3));
+ break;
+ case TARGET_NR_chdir:
+ ret = get_errno(chdir((const char *)arg1));
+ break;
+ case TARGET_NR_time:
+ {
+ int *time_ptr = (int *)arg1;
+ ret = get_errno(time((time_t *)time_ptr));
+ if (!is_error(ret) && time_ptr)
+ tswap32s(time_ptr);
+ }
+ break;
+ case TARGET_NR_mknod:
+ ret = get_errno(mknod((const char *)arg1, arg2, arg3));
+ break;
+ case TARGET_NR_chmod:
+ ret = get_errno(chmod((const char *)arg1, arg2));
+ break;
+ case TARGET_NR_lchown:
+ ret = get_errno(chown((const char *)arg1, arg2, arg3));
+ break;
+ case TARGET_NR_break:
+ goto unimplemented;
+ case TARGET_NR_oldstat:
+ goto unimplemented;
+ case TARGET_NR_lseek:
+ ret = get_errno(lseek(arg1, arg2, arg3));
+ break;
+ case TARGET_NR_getpid:
+ ret = get_errno(getpid());
+ break;
+ case TARGET_NR_mount:
+ /* need to look at the data field */
+ goto unimplemented;
+ case TARGET_NR_umount:
+ ret = get_errno(umount((const char *)arg1));
+ break;
+ case TARGET_NR_setuid:
+ ret = get_errno(setuid(arg1));
+ break;
+ case TARGET_NR_getuid:
+ ret = get_errno(getuid());
+ break;
+ case TARGET_NR_stime:
+ {
+ int *time_ptr = (int *)arg1;
+ if (time_ptr)
+ tswap32s(time_ptr);
+ ret = get_errno(stime((time_t *)time_ptr));
+ }
+ break;
+ case TARGET_NR_ptrace:
+ goto unimplemented;
+ case TARGET_NR_alarm:
+ ret = alarm(arg1);
+ break;
+ case TARGET_NR_oldfstat:
+ goto unimplemented;
+ case TARGET_NR_pause:
+ ret = get_errno(pause());
+ break;
+ case TARGET_NR_utime:
+ goto unimplemented;
+ case TARGET_NR_stty:
+ goto unimplemented;
+ case TARGET_NR_gtty:
+ goto unimplemented;
+ case TARGET_NR_access:
+ ret = get_errno(access((const char *)arg1, arg2));
+ break;
+ case TARGET_NR_nice:
+ ret = get_errno(nice(arg1));
+ break;
+ case TARGET_NR_ftime:
+ goto unimplemented;
+ case TARGET_NR_sync:
+ ret = get_errno(sync());
+ break;
+ case TARGET_NR_kill:
+ ret = get_errno(kill(arg1, arg2));
+ break;
+ case TARGET_NR_rename:
+ ret = get_errno(rename((const char *)arg1, (const char *)arg2));
+ break;
+ case TARGET_NR_mkdir:
+ ret = get_errno(mkdir((const char *)arg1, arg2));
+ break;
+ case TARGET_NR_rmdir:
+ ret = get_errno(rmdir((const char *)arg1));
+ break;
+ case TARGET_NR_dup:
+ ret = get_errno(dup(arg1));
+ break;
+ case TARGET_NR_pipe:
+ {
+ int *pipe_ptr = (int *)arg1;
+ ret = get_errno(pipe(pipe_ptr));
+ if (!is_error(ret)) {
+ tswap32s(&pipe_ptr[0]);
+ tswap32s(&pipe_ptr[1]);
+ }
+ }
+ break;
+ case TARGET_NR_times:
+ goto unimplemented;
+ case TARGET_NR_prof:
+ goto unimplemented;
+ case TARGET_NR_setgid:
+ ret = get_errno(setgid(arg1));
+ break;
+ case TARGET_NR_getgid:
+ ret = get_errno(getgid());
+ break;
+ case TARGET_NR_signal:
+ goto unimplemented;
+ case TARGET_NR_geteuid:
+ ret = get_errno(geteuid());
+ break;
+ case TARGET_NR_getegid:
+ ret = get_errno(getegid());
+ break;
+ case TARGET_NR_acct:
+ goto unimplemented;
+ case TARGET_NR_umount2:
+ ret = get_errno(umount2((const char *)arg1, arg2));
+ break;
+ case TARGET_NR_lock:
+ goto unimplemented;
+ case TARGET_NR_ioctl:
+ ret = do_ioctl(arg1, arg2, arg3);
+ break;
+ case TARGET_NR_fcntl:
+ switch(arg2) {
+ case F_GETLK:
+ case F_SETLK:
+ case F_SETLKW:
+ goto unimplemented;
+ default:
+ ret = get_errno(fcntl(arg1, arg2, arg3));
+ break;
+ }
+ break;
+ case TARGET_NR_mpx:
+ goto unimplemented;
+ case TARGET_NR_setpgid:
+ ret = get_errno(setpgid(arg1, arg2));
+ break;
+ case TARGET_NR_ulimit:
+ goto unimplemented;
+ case TARGET_NR_oldolduname:
+ goto unimplemented;
+ case TARGET_NR_umask:
+ ret = get_errno(umask(arg1));
+ break;
+ case TARGET_NR_chroot:
+ ret = get_errno(chroot((const char *)arg1));
+ break;
+ case TARGET_NR_ustat:
+ goto unimplemented;
+ case TARGET_NR_dup2:
+ ret = get_errno(dup2(arg1, arg2));
+ break;
+ case TARGET_NR_getppid:
+ ret = get_errno(getppid());
+ break;
+ case TARGET_NR_getpgrp:
+ ret = get_errno(getpgrp());
+ break;
+ case TARGET_NR_setsid:
+ ret = get_errno(setsid());
+ break;
+ case TARGET_NR_sigaction:
+#if 0
+ {
+ int signum = arg1;
+ struct target_old_sigaction *tact = arg2, *toldact = arg3;
+ ret = get_errno(setsid());
+
+
+ }
+ break;
+#else
+ goto unimplemented;
+#endif
+ case TARGET_NR_sgetmask:
+ goto unimplemented;
+ case TARGET_NR_ssetmask:
+ goto unimplemented;
+ case TARGET_NR_setreuid:
+ ret = get_errno(setreuid(arg1, arg2));
+ break;
+ case TARGET_NR_setregid:
+ ret = get_errno(setregid(arg1, arg2));
+ break;
+ case TARGET_NR_sigsuspend:
+ goto unimplemented;
+ case TARGET_NR_sigpending:
+ goto unimplemented;
+ case TARGET_NR_sethostname:
+ ret = get_errno(sethostname((const char *)arg1, arg2));
+ break;
+ case TARGET_NR_setrlimit:
+ goto unimplemented;
+ case TARGET_NR_getrlimit:
+ goto unimplemented;
+ case TARGET_NR_getrusage:
+ goto unimplemented;
+ case TARGET_NR_gettimeofday:
+ {
+ struct target_timeval *target_tv = (void *)arg1;
+ struct timeval tv;
+ ret = get_errno(gettimeofday(&tv, NULL));
+ if (!is_error(ret)) {
+ target_tv->tv_sec = tswapl(tv.tv_sec);
+ target_tv->tv_usec = tswapl(tv.tv_usec);
+ }
+ }
+ break;
+ case TARGET_NR_settimeofday:
+ {
+ struct target_timeval *target_tv = (void *)arg1;
+ struct timeval tv;
+ tv.tv_sec = tswapl(target_tv->tv_sec);
+ tv.tv_usec = tswapl(target_tv->tv_usec);
+ ret = get_errno(settimeofday(&tv, NULL));
+ }
+ break;
+ case TARGET_NR_getgroups:
+ goto unimplemented;
+ case TARGET_NR_setgroups:
+ goto unimplemented;
+ case TARGET_NR_select:
+ goto unimplemented;
+ case TARGET_NR_symlink:
+ ret = get_errno(symlink((const char *)arg1, (const char *)arg2));
+ break;
+ case TARGET_NR_oldlstat:
+ goto unimplemented;
+ case TARGET_NR_readlink:
+ ret = get_errno(readlink((const char *)arg1, (char *)arg2, arg3));
+ break;
+ case TARGET_NR_uselib:
+ goto unimplemented;
+ case TARGET_NR_swapon:
+ ret = get_errno(swapon((const char *)arg1, arg2));
+ break;
+ case TARGET_NR_reboot:
+ goto unimplemented;
+ case TARGET_NR_readdir:
+ goto unimplemented;
+#ifdef TARGET_I386
+ case TARGET_NR_mmap:
+ {
+ uint32_t v1, v2, v3, v4, v5, v6, *vptr;
+ vptr = (uint32_t *)arg1;
+ v1 = tswap32(vptr[0]);
+ v2 = tswap32(vptr[1]);
+ v3 = tswap32(vptr[2]);
+ v4 = tswap32(vptr[3]);
+ v5 = tswap32(vptr[4]);
+ v6 = tswap32(vptr[5]);
+ ret = get_errno((long)mmap((void *)v1, v2, v3, v4, v5, v6));
+ }
+ break;
+#endif
+#ifdef TARGET_I386
+ case TARGET_NR_mmap2:
+#else
+ case TARGET_NR_mmap:
+#endif
+ ret = get_errno((long)mmap((void *)arg1, arg2, arg3, arg4, arg5, arg6));
+ break;
+ case TARGET_NR_munmap:
+ ret = get_errno(munmap((void *)arg1, arg2));
+ break;
+ case TARGET_NR_truncate:
+ ret = get_errno(truncate((const char *)arg1, arg2));
+ break;
+ case TARGET_NR_ftruncate:
+ ret = get_errno(ftruncate(arg1, arg2));
+ break;
+ case TARGET_NR_fchmod:
+ ret = get_errno(fchmod(arg1, arg2));
+ break;
+ case TARGET_NR_fchown:
+ ret = get_errno(fchown(arg1, arg2, arg3));
+ break;
+ case TARGET_NR_getpriority:
+ ret = get_errno(getpriority(arg1, arg2));
+ break;
+ case TARGET_NR_setpriority:
+ ret = get_errno(setpriority(arg1, arg2, arg3));
+ break;
+ case TARGET_NR_profil:
+ goto unimplemented;
+ case TARGET_NR_statfs:
+ stfs = (void *)arg2;
+ ret = get_errno(sys_statfs((const char *)arg1, stfs));
+ convert_statfs:
+ if (!is_error(ret)) {
+ tswap32s(&stfs->f_type);
+ tswap32s(&stfs->f_bsize);
+ tswap32s(&stfs->f_blocks);
+ tswap32s(&stfs->f_bfree);
+ tswap32s(&stfs->f_bavail);
+ tswap32s(&stfs->f_files);
+ tswap32s(&stfs->f_ffree);
+ tswap32s(&stfs->f_fsid.val[0]);
+ tswap32s(&stfs->f_fsid.val[1]);
+ tswap32s(&stfs->f_namelen);
+ }
+ break;
+ case TARGET_NR_fstatfs:
+ stfs = (void *)arg2;
+ ret = get_errno(sys_fstatfs(arg1, stfs));
+ goto convert_statfs;
+ case TARGET_NR_ioperm:
+ goto unimplemented;
+ case TARGET_NR_socketcall:
+ ret = do_socketcall(arg1, (long *)arg2);
+ break;
+ case TARGET_NR_syslog:
+ goto unimplemented;
+ case TARGET_NR_setitimer:
+ goto unimplemented;
+ case TARGET_NR_getitimer:
+ goto unimplemented;
+ case TARGET_NR_stat:
+ ret = get_errno(stat((const char *)arg1, &st));
+ goto do_stat;
+ case TARGET_NR_lstat:
+ ret = get_errno(lstat((const char *)arg1, &st));
+ goto do_stat;
+ case TARGET_NR_fstat:
+ {
+ ret = get_errno(fstat(arg1, &st));
+ do_stat:
+ if (!is_error(ret)) {
+ struct target_stat *target_st = (void *)arg2;
+ target_st->st_dev = tswap16(st.st_dev);
+ target_st->st_ino = tswapl(st.st_ino);
+ target_st->st_mode = tswap16(st.st_mode);
+ target_st->st_nlink = tswap16(st.st_nlink);
+ target_st->st_uid = tswap16(st.st_uid);
+ target_st->st_gid = tswap16(st.st_gid);
+ target_st->st_rdev = tswap16(st.st_rdev);
+ target_st->st_size = tswapl(st.st_size);
+ target_st->st_blksize = tswapl(st.st_blksize);
+ target_st->st_blocks = tswapl(st.st_blocks);
+ target_st->st_atime = tswapl(st.st_atime);
+ target_st->st_mtime = tswapl(st.st_mtime);
+ target_st->st_ctime = tswapl(st.st_ctime);
+ }
+ }
+ break;
+ case TARGET_NR_olduname:
+ goto unimplemented;
+ case TARGET_NR_iopl:
+ goto unimplemented;
+ case TARGET_NR_vhangup:
+ ret = get_errno(vhangup());
+ break;
+ case TARGET_NR_idle:
+ goto unimplemented;
+ case TARGET_NR_vm86old:
+ goto unimplemented;
+ case TARGET_NR_wait4:
+ {
+ int status;
+ target_long *status_ptr = (void *)arg2;
+ struct rusage rusage, *rusage_ptr;
+ struct target_rusage *target_rusage = (void *)arg4;
+ if (target_rusage)
+ rusage_ptr = &rusage;
+ else
+ rusage_ptr = NULL;
+ ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
+ if (!is_error(ret)) {
+ if (status_ptr)
+ *status_ptr = tswap32(status);
+ if (target_rusage) {
+ target_rusage->ru_utime.tv_sec = tswapl(rusage.ru_utime.tv_sec);
+ target_rusage->ru_utime.tv_usec = tswapl(rusage.ru_utime.tv_usec);
+ target_rusage->ru_stime.tv_sec = tswapl(rusage.ru_stime.tv_sec);
+ target_rusage->ru_stime.tv_usec = tswapl(rusage.ru_stime.tv_usec);
+ target_rusage->ru_maxrss = tswapl(rusage.ru_maxrss);
+ target_rusage->ru_ixrss = tswapl(rusage.ru_ixrss);
+ target_rusage->ru_idrss = tswapl(rusage.ru_idrss);
+ target_rusage->ru_isrss = tswapl(rusage.ru_isrss);
+ target_rusage->ru_minflt = tswapl(rusage.ru_minflt);
+ target_rusage->ru_majflt = tswapl(rusage.ru_majflt);
+ target_rusage->ru_nswap = tswapl(rusage.ru_nswap);
+ target_rusage->ru_inblock = tswapl(rusage.ru_inblock);
+ target_rusage->ru_oublock = tswapl(rusage.ru_oublock);
+ target_rusage->ru_msgsnd = tswapl(rusage.ru_msgsnd);
+ target_rusage->ru_msgrcv = tswapl(rusage.ru_msgrcv);
+ target_rusage->ru_nsignals = tswapl(rusage.ru_nsignals);
+ target_rusage->ru_nvcsw = tswapl(rusage.ru_nvcsw);
+ target_rusage->ru_nivcsw = tswapl(rusage.ru_nivcsw);
+ }
+ }
+ }
+ break;
+ case TARGET_NR_swapoff:
+ ret = get_errno(swapoff((const char *)arg1));
+ break;
+ case TARGET_NR_sysinfo:
+ goto unimplemented;
+ case TARGET_NR_ipc:
+ goto unimplemented;
+ case TARGET_NR_fsync:
+ ret = get_errno(fsync(arg1));
+ break;
+ case TARGET_NR_sigreturn:
+ goto unimplemented;
+ case TARGET_NR_clone:
+ goto unimplemented;
+ case TARGET_NR_setdomainname:
+ ret = get_errno(setdomainname((const char *)arg1, arg2));
+ break;
+ case TARGET_NR_uname:
+ /* no need to transcode because we use the linux syscall */
+ ret = get_errno(sys_uname((struct new_utsname *)arg1));
+ break;
+ case TARGET_NR_modify_ldt:
+ goto unimplemented;
+ case TARGET_NR_adjtimex:
+ goto unimplemented;
+ case TARGET_NR_mprotect:
+ ret = get_errno(mprotect((void *)arg1, arg2, arg3));
+ break;
+ case TARGET_NR_sigprocmask:
+ {
+ int how = arg1;
+ sigset_t set, oldset, *set_ptr;
+ target_ulong *pset = (void *)arg2, *poldset = (void *)arg3;
+
+ switch(how) {
+ case TARGET_SIG_BLOCK:
+ how = SIG_BLOCK;
+ break;
+ case TARGET_SIG_UNBLOCK:
+ how = SIG_UNBLOCK;
+ break;
+ case TARGET_SIG_SETMASK:
+ how = SIG_SETMASK;
+ break;
+ default:
+ ret = -EINVAL;
+ goto fail;
+ }
+
+ if (pset) {
+ target_to_host_old_sigset(&set, pset);
+ set_ptr = &set;
+ } else {
+ set_ptr = NULL;
+ }
+ ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
+ if (!is_error(ret) && poldset) {
+ host_to_target_old_sigset(poldset, &oldset);
+ }
+ }
+ break;
+ case TARGET_NR_create_module:
+ case TARGET_NR_init_module:
+ case TARGET_NR_delete_module:
+ case TARGET_NR_get_kernel_syms:
+ goto unimplemented;
+ case TARGET_NR_quotactl:
+ goto unimplemented;
+ case TARGET_NR_getpgid:
+ ret = get_errno(getpgid(arg1));
+ break;
+ case TARGET_NR_fchdir:
+ ret = get_errno(fchdir(arg1));
+ break;
+ case TARGET_NR_bdflush:
+ goto unimplemented;
+ case TARGET_NR_sysfs:
+ goto unimplemented;
+ case TARGET_NR_personality:
+ ret = get_errno(mprotect((void *)arg1, arg2, arg3));
+ break;
+ case TARGET_NR_afs_syscall:
+ goto unimplemented;
+ case TARGET_NR_setfsuid:
+ goto unimplemented;
+ case TARGET_NR_setfsgid:
+ goto unimplemented;
+ case TARGET_NR__llseek:
+ {
+ int64_t res;
+ ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
+ *(int64_t *)arg4 = tswap64(res);
+ }
+ break;
+ case TARGET_NR_getdents:
+#if TARGET_LONG_SIZE != 4
+#error not supported
+#endif
+ {
+ struct dirent *dirp = (void *)arg2;
+ long count = arg3;
+ ret = get_errno(getdents(arg1, dirp, count));
+ if (!is_error(ret)) {
+ struct dirent *de;
+ int len = ret;
+ int reclen;
+ de = dirp;
+ while (len > 0) {
+ reclen = tswap16(de->d_reclen);
+ if (reclen > len)
+ break;
+ de->d_reclen = reclen;
+ tswapls(&de->d_ino);
+ tswapls(&de->d_off);
+ de = (struct dirent *)((char *)de + reclen);
+ len -= reclen;
+ }
+ }
+ }
+ break;
+ case TARGET_NR__newselect:
+ ret = do_select(arg1, (void *)arg2, (void *)arg3, (void *)arg4,
+ (void *)arg5);
+ break;
+ case TARGET_NR_flock:
+ goto unimplemented;
+ case TARGET_NR_msync:
+ ret = get_errno(msync((void *)arg1, arg2, arg3));
+ break;
+ case TARGET_NR_readv:
+ {
+ int count = arg3;
+ int i;
+ struct iovec *vec;
+ struct target_iovec *target_vec = (void *)arg2;
+
+ vec = alloca(count * sizeof(struct iovec));
+ for(i = 0;i < count; i++) {
+ vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
+ vec[i].iov_len = tswapl(target_vec[i].iov_len);
+ }
+ ret = get_errno(readv(arg1, vec, count));
+ }
+ break;
+ case TARGET_NR_writev:
+ {
+ int count = arg3;
+ int i;
+ struct iovec *vec;
+ struct target_iovec *target_vec = (void *)arg2;
+
+ vec = alloca(count * sizeof(struct iovec));
+ for(i = 0;i < count; i++) {
+ vec[i].iov_base = (void *)tswapl(target_vec[i].iov_base);
+ vec[i].iov_len = tswapl(target_vec[i].iov_len);
+ }
+ ret = get_errno(writev(arg1, vec, count));
+ }
+ break;
+ case TARGET_NR_getsid:
+ ret = get_errno(getsid(arg1));
+ break;
+ case TARGET_NR_fdatasync:
+ goto unimplemented;
+ case TARGET_NR__sysctl:
+ goto unimplemented;
+ case TARGET_NR_mlock:
+ ret = get_errno(mlock((void *)arg1, arg2));
+ break;
+ case TARGET_NR_munlock:
+ ret = get_errno(munlock((void *)arg1, arg2));
+ break;
+ case TARGET_NR_mlockall:
+ ret = get_errno(mlockall(arg1));
+ break;
+ case TARGET_NR_munlockall:
+ ret = get_errno(munlockall());
+ break;
+ case TARGET_NR_sched_setparam:
+ goto unimplemented;
+ case TARGET_NR_sched_getparam:
+ goto unimplemented;
+ case TARGET_NR_sched_setscheduler:
+ goto unimplemented;
+ case TARGET_NR_sched_getscheduler:
+ goto unimplemented;
+ case TARGET_NR_sched_yield:
+ ret = get_errno(sched_yield());
+ break;
+ case TARGET_NR_sched_get_priority_max:
+ case TARGET_NR_sched_get_priority_min:
+ case TARGET_NR_sched_rr_get_interval:
+ case TARGET_NR_nanosleep:
+ case TARGET_NR_mremap:
+ case TARGET_NR_setresuid:
+ case TARGET_NR_getresuid:
+ case TARGET_NR_vm86:
+ case TARGET_NR_query_module:
+ case TARGET_NR_poll:
+ case TARGET_NR_nfsservctl:
+ case TARGET_NR_setresgid:
+ case TARGET_NR_getresgid:
+ case TARGET_NR_prctl:
+ case TARGET_NR_rt_sigreturn:
+ case TARGET_NR_rt_sigaction:
+ case TARGET_NR_rt_sigprocmask:
+ case TARGET_NR_rt_sigpending:
+ case TARGET_NR_rt_sigtimedwait:
+ case TARGET_NR_rt_sigqueueinfo:
+ case TARGET_NR_rt_sigsuspend:
+ case TARGET_NR_pread:
+ case TARGET_NR_pwrite:
+ goto unimplemented;
+ case TARGET_NR_chown:
+ ret = get_errno(chown((const char *)arg1, arg2, arg3));
+ break;
+ case TARGET_NR_getcwd:
+ ret = get_errno(sys_getcwd((char *)arg1, arg2));
+ break;
+ case TARGET_NR_capget:
+ case TARGET_NR_capset:
+ case TARGET_NR_sigaltstack:
+ case TARGET_NR_sendfile:
+ case TARGET_NR_getpmsg:
+ case TARGET_NR_putpmsg:
+ case TARGET_NR_vfork:
+ ret = get_errno(vfork());
+ break;
+ case TARGET_NR_ugetrlimit:
+ case TARGET_NR_truncate64:
+ case TARGET_NR_ftruncate64:
+ case TARGET_NR_stat64:
+ case TARGET_NR_lstat64:
+ case TARGET_NR_fstat64:
+ case TARGET_NR_lchown32:
+ case TARGET_NR_getuid32:
+ case TARGET_NR_getgid32:
+ case TARGET_NR_geteuid32:
+ case TARGET_NR_getegid32:
+ case TARGET_NR_setreuid32:
+ case TARGET_NR_setregid32:
+ case TARGET_NR_getgroups32:
+ case TARGET_NR_setgroups32:
+ case TARGET_NR_fchown32:
+ case TARGET_NR_setresuid32:
+ case TARGET_NR_getresuid32:
+ case TARGET_NR_setresgid32:
+ case TARGET_NR_getresgid32:
+ case TARGET_NR_chown32:
+ case TARGET_NR_setuid32:
+ case TARGET_NR_setgid32:
+ case TARGET_NR_setfsuid32:
+ case TARGET_NR_setfsgid32:
+ case TARGET_NR_pivot_root:
+ case TARGET_NR_mincore:
+ case TARGET_NR_madvise:
+ case TARGET_NR_getdents64:
+ case TARGET_NR_fcntl64:
+ case TARGET_NR_security:
+ goto unimplemented;
+ case TARGET_NR_gettid:
+ ret = get_errno(gettid());
+ break;
+ case TARGET_NR_readahead:
+ case TARGET_NR_setxattr:
+ case TARGET_NR_lsetxattr:
+ case TARGET_NR_fsetxattr:
+ case TARGET_NR_getxattr:
+ case TARGET_NR_lgetxattr:
+ case TARGET_NR_fgetxattr:
+ case TARGET_NR_listxattr:
+ case TARGET_NR_llistxattr:
+ case TARGET_NR_flistxattr:
+ case TARGET_NR_removexattr:
+ case TARGET_NR_lremovexattr:
+ case TARGET_NR_fremovexattr:
+ goto unimplemented;
+ default:
+ unimplemented:
+ gemu_log("Unsupported syscall: %d\n", num);
+ ret = -ENOSYS;
+ break;
+ }
+ fail:
+ return ret;
+}
+
diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h
new file mode 100644
index 0000000000..c8f25bb5b4
--- /dev/null
+++ b/linux-user/syscall_defs.h
@@ -0,0 +1,283 @@
+
+/* common syscall defines for all architectures */
+
+#define SOCKOP_socket 1
+#define SOCKOP_bind 2
+#define SOCKOP_connect 3
+#define SOCKOP_listen 4
+#define SOCKOP_accept 5
+#define SOCKOP_getsockname 6
+#define SOCKOP_getpeername 7
+#define SOCKOP_socketpair 8
+#define SOCKOP_send 9
+#define SOCKOP_recv 10
+#define SOCKOP_sendto 11
+#define SOCKOP_recvfrom 12
+#define SOCKOP_shutdown 13
+#define SOCKOP_setsockopt 14
+#define SOCKOP_getsockopt 15
+#define SOCKOP_sendmsg 16
+#define SOCKOP_recvmsg 17
+
+struct target_timeval {
+ target_long tv_sec;
+ target_long tv_usec;
+};
+
+struct target_iovec {
+ target_long iov_base; /* Starting address */
+ target_long iov_len; /* Number of bytes */
+};
+
+struct target_rusage {
+ struct target_timeval ru_utime; /* user time used */
+ struct target_timeval ru_stime; /* system time used */
+ target_long ru_maxrss; /* maximum resident set size */
+ target_long ru_ixrss; /* integral shared memory size */
+ target_long ru_idrss; /* integral unshared data size */
+ target_long ru_isrss; /* integral unshared stack size */
+ target_long ru_minflt; /* page reclaims */
+ target_long ru_majflt; /* page faults */
+ target_long ru_nswap; /* swaps */
+ target_long ru_inblock; /* block input operations */
+ target_long ru_oublock; /* block output operations */
+ target_long ru_msgsnd; /* messages sent */
+ target_long ru_msgrcv; /* messages received */
+ target_long ru_nsignals; /* signals received */
+ target_long ru_nvcsw; /* voluntary context switches */
+ target_long ru_nivcsw; /* involuntary " */
+};
+
+typedef struct {
+ int val[2];
+} kernel_fsid_t;
+
+struct statfs {
+ int f_type;
+ int f_bsize;
+ int f_blocks;
+ int f_bfree;
+ int f_bavail;
+ int f_files;
+ int f_ffree;
+ kernel_fsid_t f_fsid;
+ int f_namelen;
+ int f_spare[6];
+};
+
+/* mostly generic signal stuff */
+#define TARGET_SIG_DFL ((target_long)0) /* default signal handling */
+#define TARGET_SIG_IGN ((target_long)1) /* ignore signal */
+#define TARGET_SIG_ERR ((target_long)-1) /* error return from signal */
+
+#ifdef TARGET_MIPS
+#define TARGET_NSIG 128
+#else
+#define TARGET_NSIG 64
+#endif
+#define TARGET_NSIG_BPW TARGET_LONG_BITS
+#define TARGET_NSIG_WORDS (TARGET_NSIG / TARGET_NSIG_BPW)
+
+typedef struct {
+ target_ulong sig[TARGET_NSIG_WORDS];
+} target_sigset_t;
+
+/* Networking ioctls */
+#define TARGET_SIOCADDRT 0x890B /* add routing table entry */
+#define TARGET_SIOCDELRT 0x890C /* delete routing table entry */
+#define TARGET_SIOCGIFNAME 0x8910 /* get iface name */
+#define TARGET_SIOCSIFLINK 0x8911 /* set iface channel */
+#define TARGET_SIOCGIFCONF 0x8912 /* get iface list */
+#define TARGET_SIOCGIFFLAGS 0x8913 /* get flags */
+#define TARGET_SIOCSIFFLAGS 0x8914 /* set flags */
+#define TARGET_SIOCGIFADDR 0x8915 /* get PA address */
+#define TARGET_SIOCSIFADDR 0x8916 /* set PA address */
+#define TARGET_SIOCGIFDSTADDR 0x8917 /* get remote PA address */
+#define TARGET_SIOCSIFDSTADDR 0x8918 /* set remote PA address */
+#define TARGET_SIOCGIFBRDADDR 0x8919 /* get broadcast PA address */
+#define TARGET_SIOCSIFBRDADDR 0x891a /* set broadcast PA address */
+#define TARGET_SIOCGIFNETMASK 0x891b /* get network PA mask */
+#define TARGET_SIOCSIFNETMASK 0x891c /* set network PA mask */
+#define TARGET_SIOCGIFMETRIC 0x891d /* get metric */
+#define TARGET_SIOCSIFMETRIC 0x891e /* set metric */
+#define TARGET_SIOCGIFMEM 0x891f /* get memory address (BSD) */
+#define TARGET_SIOCSIFMEM 0x8920 /* set memory address (BSD) */
+#define TARGET_SIOCGIFMTU 0x8921 /* get MTU size */
+#define TARGET_SIOCSIFMTU 0x8922 /* set MTU size */
+#define TARGET_SIOCSIFHWADDR 0x8924 /* set hardware address (NI) */
+#define TARGET_SIOCGIFENCAP 0x8925 /* get/set slip encapsulation */
+#define TARGET_SIOCSIFENCAP 0x8926
+#define TARGET_SIOCGIFHWADDR 0x8927 /* Get hardware address */
+#define TARGET_SIOCGIFSLAVE 0x8929 /* Driver slaving support */
+#define TARGET_SIOCSIFSLAVE 0x8930
+#define TARGET_SIOCADDMULTI 0x8931 /* Multicast address lists */
+#define TARGET_SIOCDELMULTI 0x8932
+
+/* Bridging control calls */
+#define TARGET_SIOCGIFBR 0x8940 /* Bridging support */
+#define TARGET_SIOCSIFBR 0x8941 /* Set bridging options */
+
+#define TARGET_SIOCGIFTXQLEN 0x8942 /* Get the tx queue length */
+#define TARGET_SIOCSIFTXQLEN 0x8943 /* Set the tx queue length */
+
+/* ARP cache control calls. */
+#define TARGET_OLD_SIOCDARP 0x8950 /* old delete ARP table entry */
+#define TARGET_OLD_SIOCGARP 0x8951 /* old get ARP table entry */
+#define TARGET_OLD_SIOCSARP 0x8952 /* old set ARP table entry */
+#define TARGET_SIOCDARP 0x8953 /* delete ARP table entry */
+#define TARGET_SIOCGARP 0x8954 /* get ARP table entry */
+#define TARGET_SIOCSARP 0x8955 /* set ARP table entry */
+
+/* RARP cache control calls. */
+#define TARGET_SIOCDRARP 0x8960 /* delete RARP table entry */
+#define TARGET_SIOCGRARP 0x8961 /* get RARP table entry */
+#define TARGET_SIOCSRARP 0x8962 /* set RARP table entry */
+
+/* Driver configuration calls */
+#define TARGET_SIOCGIFMAP 0x8970 /* Get device parameters */
+#define TARGET_SIOCSIFMAP 0x8971 /* Set device parameters */
+
+/* DLCI configuration calls */
+#define TARGET_SIOCADDDLCI 0x8980 /* Create new DLCI device */
+#define TARGET_SIOCDELDLCI 0x8981 /* Delete DLCI device */
+
+
+/* From <linux/fs.h> */
+
+#define TARGET_BLKROSET TARGET_IO(0x12,93) /* set device read-only (0 = read-write) */
+#define TARGET_BLKROGET TARGET_IO(0x12,94) /* get read-only status (0 = read_write) */
+#define TARGET_BLKRRPART TARGET_IO(0x12,95) /* re-read partition table */
+#define TARGET_BLKGETSIZE TARGET_IO(0x12,96) /* return device size /512 (long *arg) */
+#define TARGET_BLKFLSBUF TARGET_IO(0x12,97) /* flush buffer cache */
+#define TARGET_BLKRASET TARGET_IO(0x12,98) /* Set read ahead for block device */
+#define TARGET_BLKRAGET TARGET_IO(0x12,99) /* get current read ahead setting */
+#define TARGET_BLKFRASET TARGET_IO(0x12,100)/* set filesystem (mm/filemap.c) read-ahead */
+#define TARGET_BLKFRAGET TARGET_IO(0x12,101)/* get filesystem (mm/filemap.c) read-ahead */
+#define TARGET_BLKSECTSET TARGET_IO(0x12,102)/* set max sectors per request (ll_rw_blk.c) */
+#define TARGET_BLKSECTGET TARGET_IO(0x12,103)/* get max sectors per request (ll_rw_blk.c) */
+#define TARGET_BLKSSZGET TARGET_IO(0x12,104)/* get block device sector size */
+/* A jump here: 108-111 have been used for various private purposes. */
+#define TARGET_BLKBSZGET TARGET_IOR(0x12,112,sizeof(int))
+#define TARGET_BLKBSZSET TARGET_IOW(0x12,113,sizeof(int))
+#define TARGET_BLKGETSIZE64 TARGET_IOR(0x12,114,sizeof(uint64_t)) /* return device size in bytes (u64 *arg) */
+#define TARGET_FIBMAP TARGET_IO(0x00,1) /* bmap access */
+#define TARGET_FIGETBSZ TARGET_IO(0x00,2) /* get the block size used for bmap */
+
+/* cdrom commands */
+#define TARGET_CDROMPAUSE 0x5301 /* Pause Audio Operation */
+#define TARGET_CDROMRESUME 0x5302 /* Resume paused Audio Operation */
+#define TARGET_CDROMPLAYMSF 0x5303 /* Play Audio MSF (struct cdrom_msf) */
+#define TARGET_CDROMPLAYTRKIND 0x5304 /* Play Audio Track/index
+ (struct cdrom_ti) */
+#define TARGET_CDROMREADTOCHDR 0x5305 /* Read TOC header
+ (struct cdrom_tochdr) */
+#define TARGET_CDROMREADTOCENTRY 0x5306 /* Read TOC entry
+ (struct cdrom_tocentry) */
+#define TARGET_CDROMSTOP 0x5307 /* Stop the cdrom drive */
+#define TARGET_CDROMSTART 0x5308 /* Start the cdrom drive */
+#define TARGET_CDROMEJECT 0x5309 /* Ejects the cdrom media */
+#define TARGET_CDROMVOLCTRL 0x530a /* Control output volume
+ (struct cdrom_volctrl) */
+#define TARGET_CDROMSUBCHNL 0x530b /* Read subchannel data
+ (struct cdrom_subchnl) */
+#define TARGET_CDROMREADMODE2 0x530c /* Read TARGET_CDROM mode 2 data (2336 Bytes)
+ (struct cdrom_read) */
+#define TARGET_CDROMREADMODE1 0x530d /* Read TARGET_CDROM mode 1 data (2048 Bytes)
+ (struct cdrom_read) */
+#define TARGET_CDROMREADAUDIO 0x530e /* (struct cdrom_read_audio) */
+#define TARGET_CDROMEJECT_SW 0x530f /* enable(1)/disable(0) auto-ejecting */
+#define TARGET_CDROMMULTISESSION 0x5310 /* Obtain the start-of-last-session
+ address of multi session disks
+ (struct cdrom_multisession) */
+#define TARGET_CDROM_GET_MCN 0x5311 /* Obtain the "Universal Product Code"
+ if available (struct cdrom_mcn) */
+#define TARGET_CDROM_GET_UPC TARGET_CDROM_GET_MCN /* This one is depricated,
+ but here anyway for compatability */
+#define TARGET_CDROMRESET 0x5312 /* hard-reset the drive */
+#define TARGET_CDROMVOLREAD 0x5313 /* Get the drive's volume setting
+ (struct cdrom_volctrl) */
+#define TARGET_CDROMREADRAW 0x5314 /* read data in raw mode (2352 Bytes)
+ (struct cdrom_read) */
+/*
+ * These ioctls are used only used in aztcd.c and optcd.c
+ */
+#define TARGET_CDROMREADCOOKED 0x5315 /* read data in cooked mode */
+#define TARGET_CDROMSEEK 0x5316 /* seek msf address */
+
+/*
+ * This ioctl is only used by the scsi-cd driver.
+ It is for playing audio in logical block addressing mode.
+ */
+#define TARGET_CDROMPLAYBLK 0x5317 /* (struct cdrom_blk) */
+
+/*
+ * These ioctls are only used in optcd.c
+ */
+#define TARGET_CDROMREADALL 0x5318 /* read all 2646 bytes */
+
+/*
+ * These ioctls are (now) only in ide-cd.c for controlling
+ * drive spindown time. They should be implemented in the
+ * Uniform driver, via generic packet commands, GPCMD_MODE_SELECT_10,
+ * GPCMD_MODE_SENSE_10 and the GPMODE_POWER_PAGE...
+ * -Erik
+ */
+#define TARGET_CDROMGETSPINDOWN 0x531d
+#define TARGET_CDROMSETSPINDOWN 0x531e
+
+/*
+ * These ioctls are implemented through the uniform CD-ROM driver
+ * They _will_ be adopted by all CD-ROM drivers, when all the CD-ROM
+ * drivers are eventually ported to the uniform CD-ROM driver interface.
+ */
+#define TARGET_CDROMCLOSETRAY 0x5319 /* pendant of CDROMEJECT */
+#define TARGET_CDROM_SET_OPTIONS 0x5320 /* Set behavior options */
+#define TARGET_CDROM_CLEAR_OPTIONS 0x5321 /* Clear behavior options */
+#define TARGET_CDROM_SELECT_SPEED 0x5322 /* Set the CD-ROM speed */
+#define TARGET_CDROM_SELECT_DISC 0x5323 /* Select disc (for juke-boxes) */
+#define TARGET_CDROM_MEDIA_CHANGED 0x5325 /* Check is media changed */
+#define TARGET_CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */
+#define TARGET_CDROM_DISC_STATUS 0x5327 /* Get disc type, etc. */
+#define TARGET_CDROM_CHANGER_NSLOTS 0x5328 /* Get number of slots */
+#define TARGET_CDROM_LOCKDOOR 0x5329 /* lock or unlock door */
+#define TARGET_CDROM_DEBUG 0x5330 /* Turn debug messages on/off */
+#define TARGET_CDROM_GET_CAPABILITY 0x5331 /* get capabilities */
+
+/* Note that scsi/scsi_ioctl.h also uses 0x5382 - 0x5386.
+ * Future CDROM ioctls should be kept below 0x537F
+ */
+
+/* This ioctl is only used by sbpcd at the moment */
+#define TARGET_CDROMAUDIOBUFSIZ 0x5382 /* set the audio buffer size */
+ /* conflict with SCSI_IOCTL_GET_IDLUN */
+
+/* DVD-ROM Specific ioctls */
+#define TARGET_DVD_READ_STRUCT 0x5390 /* Read structure */
+#define TARGET_DVD_WRITE_STRUCT 0x5391 /* Write structure */
+#define TARGET_DVD_AUTH 0x5392 /* Authentication */
+
+#define TARGET_CDROM_SEND_PACKET 0x5393 /* send a packet to the drive */
+#define TARGET_CDROM_NEXT_WRITABLE 0x5394 /* get next writable block */
+#define TARGET_CDROM_LAST_WRITTEN 0x5395 /* get last block written on disc */
+
+/* HD commands */
+
+/* hd/ide ctl's that pass (arg) ptrs to user space are numbered 0x030n/0x031n */
+#define TARGET_HDIO_GETGEO 0x0301 /* get device geometry */
+#define TARGET_HDIO_GET_UNMASKINTR 0x0302 /* get current unmask setting */
+#define TARGET_HDIO_GET_MULTCOUNT 0x0304 /* get current IDE blockmode setting */
+#define TARGET_HDIO_GET_IDENTITY 0x0307 /* get IDE identification info */
+#define TARGET_HDIO_GET_KEEPSETTINGS 0x0308 /* get keep-settings-on-reset flag */
+#define TARGET_HDIO_GET_32BIT 0x0309 /* get current io_32bit setting */
+#define TARGET_HDIO_GET_NOWERR 0x030a /* get ignore-write-error flag */
+#define TARGET_HDIO_GET_DMA 0x030b /* get use-dma flag */
+#define TARGET_HDIO_DRIVE_CMD 0x031f /* execute a special drive command */
+
+/* hd/ide ctl's that pass (arg) non-ptr values are numbered 0x032n/0x033n */
+#define TARGET_HDIO_SET_MULTCOUNT 0x0321 /* change IDE blockmode */
+#define TARGET_HDIO_SET_UNMASKINTR 0x0322 /* permit other irqs during I/O */
+#define TARGET_HDIO_SET_KEEPSETTINGS 0x0323 /* keep ioctl settings on reset */
+#define TARGET_HDIO_SET_32BIT 0x0324 /* change io_32bit flags */
+#define TARGET_HDIO_SET_NOWERR 0x0325 /* change ignore-write-error flag */
+#define TARGET_HDIO_SET_DMA 0x0326 /* change use-dma flag */
+#define TARGET_HDIO_SET_PIO_MODE 0x0327 /* reconfig interface to new speed */
diff --git a/linux-user/syscall_types.h b/linux-user/syscall_types.h
new file mode 100644
index 0000000000..63852d3af0
--- /dev/null
+++ b/linux-user/syscall_types.h
@@ -0,0 +1,64 @@
+STRUCT_SPECIAL(termios)
+
+STRUCT(winsize,
+ TYPE_SHORT, TYPE_SHORT, TYPE_SHORT, TYPE_SHORT)
+
+STRUCT(serial_multiport_struct,
+ TYPE_INT, TYPE_INT, TYPE_CHAR, TYPE_CHAR, TYPE_INT, TYPE_CHAR, TYPE_CHAR,
+ TYPE_INT, TYPE_CHAR, TYPE_CHAR, TYPE_INT, TYPE_CHAR, TYPE_CHAR, TYPE_INT,
+ MK_ARRAY(TYPE_INT, 32))
+
+STRUCT(serial_icounter_struct,
+ TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, MK_ARRAY(TYPE_INT, 16))
+
+STRUCT(sockaddr,
+ TYPE_SHORT, MK_ARRAY(TYPE_CHAR, 14))
+
+STRUCT(rtentry,
+ TYPE_ULONG, MK_STRUCT(STRUCT_sockaddr), MK_STRUCT(STRUCT_sockaddr), MK_STRUCT(STRUCT_sockaddr),
+ TYPE_SHORT, TYPE_SHORT, TYPE_ULONG, TYPE_PTRVOID, TYPE_SHORT, TYPE_PTRVOID,
+ TYPE_ULONG, TYPE_ULONG, TYPE_SHORT)
+
+STRUCT(ifmap,
+ TYPE_ULONG, TYPE_ULONG, TYPE_SHORT, TYPE_CHAR, TYPE_CHAR, TYPE_CHAR,
+ /* Spare 3 bytes */
+ TYPE_CHAR, TYPE_CHAR, TYPE_CHAR)
+
+/* The *_ifreq_list arrays deal with the fact that struct ifreq has unions */
+
+STRUCT(sockaddr_ifreq,
+ MK_ARRAY(TYPE_CHAR, IFNAMSIZ), MK_STRUCT(STRUCT_sockaddr))
+
+STRUCT(short_ifreq,
+ MK_ARRAY(TYPE_CHAR, IFNAMSIZ), TYPE_SHORT)
+
+STRUCT(int_ifreq,
+ MK_ARRAY(TYPE_CHAR, IFNAMSIZ), TYPE_INT)
+
+STRUCT(ifmap_ifreq,
+ MK_ARRAY(TYPE_CHAR, IFNAMSIZ), MK_STRUCT(STRUCT_ifmap))
+
+STRUCT(char_ifreq,
+ MK_ARRAY(TYPE_CHAR, IFNAMSIZ),
+ MK_ARRAY(TYPE_CHAR, IFNAMSIZ))
+
+STRUCT(ptr_ifreq,
+ MK_ARRAY(TYPE_CHAR, IFNAMSIZ), TYPE_PTRVOID)
+
+STRUCT(ifconf,
+ TYPE_INT, TYPE_PTRVOID)
+
+STRUCT(arpreq,
+ MK_STRUCT(STRUCT_sockaddr), MK_STRUCT(STRUCT_sockaddr), TYPE_INT, MK_STRUCT(STRUCT_sockaddr),
+ MK_ARRAY(TYPE_CHAR, 16))
+
+STRUCT(arpreq_old,
+ MK_STRUCT(STRUCT_sockaddr), MK_STRUCT(STRUCT_sockaddr), TYPE_INT, MK_STRUCT(STRUCT_sockaddr))
+
+STRUCT(cdrom_read_audio,
+ TYPE_CHAR, TYPE_CHAR, TYPE_CHAR, TYPE_CHAR, TYPE_CHAR, TYPE_INT, TYPE_PTRVOID,
+ TYPE_NULL)
+
+STRUCT(hd_geometry,
+ TYPE_CHAR, TYPE_CHAR, TYPE_SHORT, TYPE_ULONG)
+