Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 10 Nov 2008 19:54:24 GMT
From:      Peter Wemm <peter@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 152759 for review
Message-ID:  <200811101954.mAAJsO1B015695@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=152759

Change 152759 by peter@peter_hammer on 2008/11/10 19:54:15

	Baseline thr_new() for i386.  Doesn't have the MD parts to set up
	the args to the thread trampoline, and doesn't set tls yet.

Affected files ...

.. //depot/projects/valgrind/coregrind/m_syswrap/syswrap-x86-freebsd.c#11 edit

Differences ...

==== //depot/projects/valgrind/coregrind/m_syswrap/syswrap-x86-freebsd.c#11 (text+ko) ====

@@ -256,11 +256,144 @@
 #define PRE(name)       DEFN_PRE_TEMPLATE(freebsd, name)
 #define POST(name)      DEFN_POST_TEMPLATE(freebsd, name)
 
+#if 0
+struct thr_param {
+    void        (*start_func)(void *);  /* thread entry function. */
+    void        *arg;                   /* argument for entry function. */
+    char        *stack_base;            /* stack base address. */
+    size_t      stack_size;             /* stack size. */
+    char        *tls_base;              /* tls base address. */
+    size_t      tls_size;               /* tls size. */
+    long        *child_tid;             /* address to store new TID. */
+    long        *parent_tid;            /* parent accesses the new TID here. */
+    int         flags;                  /* thread flags. */
+    struct rtprio       *rtp;           /* Real-time scheduling priority */
+    void        *spare[3];              /* TODO: cpu affinity mask etc. */
+};
+int thr_new(struct thr_param *param, int param_size);
+#endif
 
 PRE(sys_thr_new)
 {
+   static const Bool debug = False;
+
+   ThreadId     ctid = VG_(alloc_ThreadState)();
+   ThreadState* ptst = VG_(get_ThreadState)(tid);
+   ThreadState* ctst = VG_(get_ThreadState)(ctid);
+   SysRes       res;
+   vki_sigset_t blockall, savedmask;
+   struct vki_thr_param tp;
+   Addr stk;
+
    PRINT("thr_new ( %#lx, %ld )",ARG1,ARG2);
-   SET_STATUS_Failure( VKI_EINVAL );
+   PRE_REG_READ2(int, "thr_new",
+                 struct thr_param *, param,
+                 int, param_size);
+   
+   PRE_MEM_READ( "thr_new(param)", ARG1, offsetof(struct vki_thr_param, spare));
+   if (!ML_(safe_to_deref)( (void*)ARG1, offsetof(struct vki_thr_param, spare))) {
+      SET_STATUS_Failure( VKI_EFAULT );
+      return;
+   }
+   VG_(memset)(&tp, 0, sizeof(tp));
+   VG_(memcpy)(&tp, (void *)ARG1, offsetof(struct vki_thr_param, spare));
+   PRE_MEM_WRITE("clone(parent_tidptr)", (Addr)tp.parent_tid, sizeof(long));
+   PRE_MEM_WRITE("clone(child_tidptr)", (Addr)tp.child_tid, sizeof(long));
+
+   VG_(sigfillset)(&blockall);
+
+   vg_assert(VG_(is_running_thread)(tid));
+   vg_assert(VG_(is_valid_tid)(ctid));
+
+   /* Copy register state
+
+      On linux, both parent and child return to the same place, and the code
+      following the clone syscall works out which is which, so we
+      don't need to worry about it.
+      On FreeBSD, thr_new arranges a direct call.  We don't actually need any
+      of this gunk.
+
+      The parent gets the child's new tid returned from clone, but the
+      child gets 0.
+
+      If the clone call specifies a NULL rsp for the new thread, then
+      it actually gets a copy of the parent's rsp.
+   */
+   /* We inherit our parent's guest state. */
+   ctst->arch.vex = ptst->arch.vex;
+   ctst->arch.vex_shadow1 = ptst->arch.vex_shadow1;
+   ctst->arch.vex_shadow2 = ptst->arch.vex_shadow2;
+
+   /* Make sys_clone appear to have returned Success(0) in the
+      child. */
+   ctst->arch.vex.guest_EAX = 0;
+   ctst->arch.vex.guest_EDX = 0;
+   LibVEX_GuestX86_put_eflag_c(0, &ctst->arch.vex);
+
+   ctst->os_state.parent = tid;
+
+   /* inherit signal mask */
+   ctst->sig_mask = ptst->sig_mask;
+   ctst->tmp_sig_mask = ptst->sig_mask;
+
+   /* Linux has to guess, we don't */
+   VG_(register_stack)((Addr)tp.stack_base, (Addr)tp.stack_base + tp.stack_size);
+
+   /* Assume the clone will succeed, and tell any tool that wants to
+      know that this thread has come into existence.  If the clone
+      fails, we'll send out a ll_exit notification for it at the out:
+      label below, to clean up. */
+   VG_TRACK ( pre_thread_ll_create, tid, ctid );
+
+   if (debug)
+      VG_(printf)("clone child has SETTLS: tls at %#lx\n", (Addr)tp.tls_base);
+#if 0	/* XXX implement. use set_thread_area stuff */
+   ctst->arch.vex.guest_FS_ZERO = (UWord)tp.tls_base;
+#endif
+   tp.tls_base = 0;	/* Don't have the kernel do it too */
+
+   /* start the thread with everything blocked */
+   VG_(sigprocmask)(VKI_SIG_SETMASK, &blockall, &savedmask);
+
+#if 0	/* XXX implement. either use stack args or change to regparm */
+   /* Set the client state for scheduler to run libthr's trampoline */
+   ctst->arch.vex.guest_RDI = (Addr)tp.arg;
+   ctst->arch.vex.guest_RSP = (Addr)tp.stack_base + tp.stack_size - 8;
+   ctst->arch.vex.guest_RIP = (Addr)tp.start_func;
+#endif
+
+   /* But this is for thr_new() to run valgrind's trampoline */
+   tp.start_func = (void *)ML_(start_thread_NORETURN);
+   tp.arg = &VG_(threads)[ctid];
+
+   /* And valgrind's trampoline on its own stack */
+   stk = ML_(allocstack)(ctid);
+   tp.stack_base = (void *)ctst->os_state.valgrind_stack_base;
+   tp.stack_size = (Addr)stk - (Addr)tp.stack_base;
+
+   /* Create the new thread */
+   res = VG_(do_syscall2)(__NR_thr_new, (UWord)&tp, sizeof(tp));
+
+   VG_(sigprocmask)(VKI_SIG_SETMASK, &savedmask, NULL);
+
+   if (res.isError) {
+      /* clone failed */
+      VG_(cleanup_thread)(&ctst->arch);
+      ctst->status = VgTs_Empty;
+      /* oops.  Better tell the tool the thread exited in a hurry :-) */
+      VG_TRACK( pre_thread_ll_exit, ctid );
+   } else {
+
+      POST_MEM_WRITE((Addr)tp.parent_tid, sizeof(long));
+      POST_MEM_WRITE((Addr)tp.child_tid, sizeof(long));
+
+      /* Thread creation was successful; let the child have the chance
+         to run */
+      *flags |= SfYieldAfter;
+   }
+
+   /* "Complete" the syscall so that the wrapper doesn't call the kernel again. */
+   SET_STATUS_from_SysRes(res);
 }
 
 PRE(sys_sigreturn)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200811101954.mAAJsO1B015695>