kern_threads.c.. upcall question..

David Xu davidxu at freebsd.org
Mon May 5 23:38:06 PDT 2003


----- Original Message ----- 
From: "Julian Elischer" <julian at elischer.org>
To: "Daniel Eischen" <eischen at pcnet1.pcnet.com>
Cc: <threads at freebsd.org>
Sent: Tuesday, May 06, 2003 8:39 AM
Subject: Re: kern_threads.c.. upcall question..


> 
> 
> On Mon, 5 May 2003, Daniel Eischen wrote:
> 
> > On Mon, 5 May 2003, Julian Elischer wrote:
> > 
> > > 
> > > In kern_threads.c, in function thread_export_context()
> > > it first does a copyin() of the
> > > context storage area. I think this is un-needed (and a complete waste of
> > > cycles) but I am not sure if there is some strange condition
> > > regarding floating point registers or something that may want this..
> > > 
> > > Dan, Jon, David?
> > > do any of you have a good reason why I shouldn't remove the copyin().??
> > 
> > Yeah, the threads library keeps the thread's active signal
> > mask in the context area.  The stack and flags may also
> > be used in the context.
> > 
> > You should be able to safely copy out the mcontext without
> > a copyin().  Is that what you're currently doing?  Or is
> > the entire context (ucontext) being exported?
> 
> it does:
> 
> *) copyin the ucontext_t
> 
> *) load context into it using thread_getcontext() which uses
>   get_mcontext() and adds the kernel thread sigmask (this must have
>   been added by jeff in some way) (note it doesn't OR, just writes)
> 
> *) copyout of the ucontext_t
> 
> The ucontext is:
> 
>         sigset_t        uc_sigmask;   <-- gets over-written (!)
>         mcontext_t      uc_mcontext;  <-- gets over-written
>         struct __ucontext *uc_link;   <-- one in userland MAY be
> valuable
>         stack_t         uc_stack;     <-- probably worth saving..
>         int             uc_flags;     <-- probably worth saving
>         int             __spare__[4];
> 
> 
> 
> 
> It seems to me that just copying out the sigset_t and mcontext_t
> may be all that is needed. (and I'm not so sure about the sigset_t
> at that..
> 
> 
> > 
> > -- 
> > Dan Eischen
> > 
> > 

I think the following patch is enough:

Index: kern_thread.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/kern_thread.c,v
retrieving revision 1.129
diff -u -r1.129 kern_thread.c
--- kern_thread.c 1 May 2003 12:16:06 -0000 1.129
+++ kern_thread.c 6 May 2003 06:32:10 -0000
@@ -721,41 +721,6 @@
 }
 
 /*
- * Fill a ucontext_t with a thread's context information.
- *
- * This is an analogue to getcontext(3).
- */
-void
-thread_getcontext(struct thread *td, ucontext_t *uc)
-{
-
- get_mcontext(td, &uc->uc_mcontext, 0);
- PROC_LOCK(td->td_proc);
- uc->uc_sigmask = td->td_sigmask;
- PROC_UNLOCK(td->td_proc);
-}
-
-/*
- * Set a thread's context from a ucontext_t.
- *
- * This is an analogue to setcontext(3).
- */
-int
-thread_setcontext(struct thread *td, ucontext_t *uc)
-{
- int ret;
-
- ret = set_mcontext(td, &uc->uc_mcontext);
- if (ret == 0) {
-  SIG_CANTMASK(uc->uc_sigmask);
-  PROC_LOCK(td->td_proc);
-  td->td_sigmask = uc->uc_sigmask;
-  PROC_UNLOCK(td->td_proc);
- }
- return (ret);
-}
-
-/*
  * Initialize global thread allocation resources.
  */
 void
@@ -962,27 +927,25 @@
  uintptr_t mbx;
  void *addr;
  int error,temp;
- ucontext_t uc;
+ mcontext_t mc;
 
  p = td->td_proc;
  kg = td->td_ksegrp;
 
  /* Export the user/machine context. */
- addr = (void *)(&td->td_mailbox->tm_context);
- error = copyin(addr, &uc, sizeof(ucontext_t));
- if (error) 
-  goto bad;
-
- thread_getcontext(td, &uc);
- error = copyout(&uc, addr, sizeof(ucontext_t));
- if (error) 
+ get_mcontext(td, &mc, 0);
+ addr = (void *)(&td->td_mailbox->tm_context.uc_mcontext);
+ error = copyout(&mc, addr, sizeof(mcontext_t));
+ if (error)
   goto bad;
 
  /* Exports clock ticks in kernel mode */
  addr = (caddr_t)(&td->td_mailbox->tm_sticks);
  temp = fuword(addr) + td->td_usticks;
- if (suword(addr, temp))
+ if (suword(addr, temp)) {
+  error = EFAULT;
   goto bad;
+ }
 
  /* Get address in latest mbox of list pointer */
  addr = (void *)(&td->td_mailbox->tm_next);


And should we disable single threading testing or do
double checking in thread_user_enter()? I think per-syscall
PROC_LOCK is too expensive for us.

David Xu




More information about the freebsd-threads mailing list