Implementing TLS: step 1

Julian Elischer julian at elischer.org
Thu Jun 19 18:57:38 PDT 2003



On Thu, 19 Jun 2003, Daniel Eischen wrote:

> On Thu, 19 Jun 2003, Marcel Moolenaar wrote:
> > On Thu, Jun 19, 2003 at 04:12:50PM -0700, Julian Elischer wrote:
> > > 
> > > 
> > > BTW Marcel, I think that we can get from where we are now with kse to
> > > what we need by just a little massaging of what points to what.
> > > it is possible that the register %gs could be pointed to the thread
> > > structure directly and we could derive the KSE from that. it will just
> > > make the context switches a fraction more expensive if we need to
> > > change the segment register..
> > 
> > Yes. On i386 because the indirection through %gs:0x0 allows having %gs
> > point anything we like: the KSE. On ia64 there's room for a pointer at
> > offset 8 in the TLS that we can use to point to whatever we like.  That
> > context switches involve an additional pointer fiddle to make sure we
> > always have the indirection to the KSE is something I don't worry about.
> 
> Currently, the libkse TLS pointer has to point to the KSE mailbox.
> It is necessary to be able to set a word in the KSE mailbox in 1
> instruction.  The KSE mailbox "current thread pointer" must be
> set to NULL to prevent upcalls.  Indirecting to get to the
> mailbox pointer in order to set it leaves open a race condition
> where the TLS changes out from under us before we set it.
> So if TP can't point to the KSE mailbox, then there needs to
> be other changes in the kernel and libkse.
> 
> Julian, please clarify what I've said if it doesn't seem
> clear.


What you have said is true, but it desn't reflect on where %gs:0 points
becasue the kernel doesn't use %gs to find the user KSE mailbox. it just
"knows" . In the same what the UTS "just KNOWS"  where the thread
mailbox and KSE mailboxes are. it doesn't need to get them via %gs:0 so
we can still do teh singl instruction stuff. We can set %gs befoer we
set the KSE->thread pointer, while we are in the UTS. No-one will follow
it until we start running as the thread because it is only used by code
using the __thread keyword.

In other words, the KSE_mbox->thread_mbox link is completely separate
(orthogonal if you want) from the TLS pointer.. they are separate
issues.
The info is slightly redundant (only slightly) but they don't impact on
each other.

I think we are just fine.. we can set teh forst entry in the KSE mailbox
to be a pointer to the TLS for teh thread we are switching in.
It will ONLY be read by the thread itself so we know that it must be
correct as we set it when we schedule it in..

A thread can never switch KSEs without doing so via an upcall, or
otherwise involving the UTS, so we know that %gs:0 always points to a
pointer to the TLS.. Everythings cool.

For that matter %gs:0 need not point to the mailbox at all,
since anyoen who needs to find it either has teh time to get it slowely
(via wherever it DOES point), or has other ways of finding it.

It comes in as the argument to the upcall for example (i.e the kernel
tells it where it is). Wherever it points we can get to teh mailbox or
teh TLS or both.. it's not important. It's just a shell-game with
pointers.

> 
> -- 
> Dan Eischen
> 
> 



More information about the freebsd-threads mailing list