Index: qemu.bkp/linux-user/main.c ================================================================================ --- qemu-0.9.1/linux-user/syscall.c +++ qemu-0.9.1/linux-user/syscall.c @@ -159,6 +159,7 @@ #define __NR_sys_tkill __NR_tkill #define __NR_sys_unlinkat __NR_unlinkat #define __NR_sys_utimensat __NR_utimensat +#define __NR_sys_clone __NR_clone #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) #define __NR__llseek __NR_lseek @@ -227,6 +228,9 @@ #if defined(TARGET_NR_tkill) && defined(__NR_tkill) _syscall2(int,sys_tkill,int,tid,int,sig) #endif +#ifdef __NR_sys_clone +_syscall5(int,sys_clone, int, flags, void *, child_stack, int *, parent_tidptr, void *, newtls, int *, child_tidptr) +#endif #ifdef __NR_exit_group _syscall1(int,exit_group,int,error_code) #endif @@ -2705,15 +2783,20 @@ /* do_fork() Must return host values and target errnos (unlike most do_*() functions). */ -int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp) +int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp, abi_ulong parent_tidptr, abi_ulong newtls, abi_ulong child_tidptr) { int ret; + unsigned long parent_tid = gettid(); TaskState *ts; uint8_t *new_stack; CPUState *new_env; +#if defined(TARGET_I386) + uint64_t *new_gdt_table; +#endif if (flags & CLONE_VM) { ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE); + if (!ts) return -ENOMEM; memset(ts, 0, sizeof(TaskState)); new_stack = ts->stack; ts->used = 1; @@ -2725,6 +2808,27 @@ #if defined(TARGET_I386) if (!newsp) newsp = env->regs[R_ESP]; + new_gdt_table = malloc(9 * 8); + if (!new_gdt_table) { + free(new_env); + return -ENOMEM; + } + /* Copy main GDT table from parent, but clear TLS entries */ + memcpy(new_gdt_table, g2h(env->gdt.base), 6 * 8); + memset(&new_gdt_table[6], 0, 3 * 8); + new_env->gdt.base = h2g(new_gdt_table); + if (flags & CLONE_SETTLS) { + ret = do_set_thread_area(new_env, newtls); + if (ret) { + free(new_gdt_table); + free(new_env); + return ret; + } + } + + cpu_x86_load_seg(new_env, R_FS, new_env->segs[R_FS].selector); + cpu_x86_load_seg(new_env, R_GS, new_env->segs[R_GS].selector); + new_env->regs[R_ESP] = newsp; new_env->regs[R_EAX] = 0; #elif defined(TARGET_ARM) @@ -2782,15 +2886,27 @@ #endif new_env->opaque = ts; #ifdef __ia64__ - ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env); + ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags & ~(CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID), new_env); #else - ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env); + ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags & ~(CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID), new_env); #endif } else { /* if no CLONE_VM, we consider it is a fork */ - if ((flags & ~CSIGNAL) != 0) - return -EINVAL; - ret = fork(); + ret = sys_clone(flags & ~(CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID), 0, g2h(parent_tidptr), NULL, g2h(child_tidptr)); + } + /* Store child thread ID at location parent_tidptr in parent and child memory. + Currently this is only done in client memory */ + if(flags & CLONE_PARENT_SETTID) { + put_user_u32(parent_tidptr, parent_tid); + } + + /* Store child thread ID at location child_tidptr in child memory. */ + if(flags & CLONE_CHILD_SETTID) { + if(ret==0) { /* only in client memory for fork() */ + put_user_u32(child_tidptr, gettid()); + } else if(flags & CLONE_VM) { /* real threads need it too */ + put_user_u32(child_tidptr, ret); + } } return ret; } @@ -3073,7 +3189,7 @@ _mcleanup(); #endif gdb_exit(cpu_env, arg1); - /* XXX: should free thread stack and CPU env */ + /* XXX: should free thread stack, GDT and CPU env */ _exit(arg1); ret = 0; /* avoid warning */ break; @@ -3115,7 +3231,7 @@ ret = do_brk(arg1); break; case TARGET_NR_fork: - ret = get_errno(do_fork(cpu_env, SIGCHLD, 0)); + ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0,0,0)); break; #ifdef TARGET_NR_waitpid case TARGET_NR_waitpid: @@ -4477,7 +4593,7 @@ ret = get_errno(fsync(arg1)); break; case TARGET_NR_clone: - ret = get_errno(do_fork(cpu_env, arg1, arg2)); + ret = get_errno(do_fork(cpu_env, arg1, arg2,arg3,arg4,arg5)); break; #ifdef __NR_exit_group /* new thread calls */ @@ -4897,7 +5013,7 @@ #endif #ifdef TARGET_NR_vfork case TARGET_NR_vfork: - ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0)); + ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0, 0,0,0)); break; #endif #ifdef TARGET_NR_ugetrlimit