Avoid TLS calls in services/device drivers while processing IPC calls on WinCE


This post is another of the pretty low-level, under the hood about interprocess communication on WinCE.  I considered not even posting it, but it meets my geek threshold (5 people in the world will need it sooner or later, but probably not more than 5) so I'm going ahead.


One of the main functions of Services.exe on Windows CE is that it provides glue to tie services to applications running on the same device.  There's a bunch of low-level kernel magic that has to happen to make Interprocess Communication (IPC) work and services.exe hides that from you.  An example of this is that an application call to DeviceIoControl() gets turned into the specified service's xxx_IOControl() handler.


When writing these xxx_ handlers (like xxx_Open, xxx_Init, xxx_IOControl -- see here) you must not make any calls that require TLS (thread local storage).  The reason is that the kernel does not support TLS when doing interprocess calls.


The main user of TLS you'll run into is COM.  On PSL threads you can call pre-existing COM objects (provided you know they're clear of TLS calls), but you can't call COM API's like CoCreateInstance and friends.   Fortunately most services avoid COM in the first place, but should you absolutely need to do COM from within a xxx_IOControl() you'll need to spin up a worker thread to do the work, and block until it's completed.  We're aware this is inconvenient and are looking into adding cross-process TLS support in a future version of WinCE (it's not in CE 6.0).


Also note that a lot of DLLs in their DllMain() tend to do TLS just by virtue of doing TlsAlloc, since if they'll  do TLS at any point they'll want the slot reserved.  This means that loading or unloading DLLs from within an xxx_* function can cause problems.  Services.exe in fact spins up a worker thread to load your service when processing an ActivateService() API call (rather than using the same thread that the ActivateService() came in on) in case your service is using TLS in its DllMain.  This worker thread is also used to call your xxx_Init().  Similarly, services.exe spins up a worker thread to unload your service and call its xxx_DeInit().


Everything I say here (save one point) applies to device drivers as well.  I just am biased toward services since that's where most of my experience is so that's why I use it as my examples.  The only difference is that device.exe doesn't spin up worker threads for DLL Load/unload/xxx_Init/xxx_DeInit, since for whatever reason service DLLs are much more likely to bring in stuff that wants to use TLS than device drivers.


As far as what happens with TLS messing up, it depends.  On a fully DEBUG build you'll get a DEBUGCHK if you try to do TLS across a PSL call.  In any event, it's up to the caller to deal with the error gracefully.  This is a good reminder always to handle error conditions even on functions that "shouldn't" fail like the TLS fcns.


[Author: John Spaith]

Comments (5)

  1. Roger Williamson says:

    I suppose I’m one of those five people.

    I’m currently working on a Smart Card Driver for WinCE (Windows Mobile 5.0), and I’ve run into a tangled web of problems just trying to get one example to work.

    I believe that this article may help us when we create our own driver.

  2. cenet says:

    Roger – I’m sorry to hear you’re having problems and I hope this post helped you some.  If you’re stuck, we have a pretty active newsgroup community; check out http://blogs.msdn.com/cenet/archive/2005/12/05/500181.aspx for a list.

    I’m not at all familiar with smartcard drivers so I’m afraid I can’t personally help.

    John

  3. Mark Moeller says:

    This is an excellent post.  I’d like to see more like this.  While I appreciate articles of broad interest it is articles like these that help get that last 10% needed to get a product shipped.  I’ve been working with CE for 10 years and had no idea about this limitation.  

  4. Richard R. Lee says:

    John, I think you just saved me a bundle of greif.

    I am porting code from L* user mode, which made clever use of TLS. As the end result of my work may be embedded in a CE device driver, I now plan to use a different design pattern.

    Thanks in advance (of writing code requireing nasty debugging!)

    And in all fairness, there are at least 6 of us 🙂

  5. Evgeny says:

    Thanks a lot for the post. If I didn’t find it, I would never fix the problem that I had.

Skip to main content