ntdll: Block async signals during process init and thread creation.
[wine] / dlls / ntdll / thread.c
1 /*
2  * NT threads support
3  *
4  * Copyright 1996, 2003 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29 #ifdef HAVE_SYS_TIMES_H
30 #include <sys/times.h>
31 #endif
32
33 #define NONAMELESSUNION
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "thread.h"
37 #include "winternl.h"
38 #include "wine/library.h"
39 #include "wine/server.h"
40 #include "wine/pthread.h"
41 #include "wine/debug.h"
42 #include "ntdll_misc.h"
43 #include "wine/exception.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(thread);
46
47 /* info passed to a starting thread */
48 struct startup_info
49 {
50     struct wine_pthread_thread_info pthread_info;
51     PRTL_THREAD_START_ROUTINE       entry_point;
52     void                           *entry_arg;
53 };
54
55 static PEB_LDR_DATA ldr;
56 static RTL_USER_PROCESS_PARAMETERS params;  /* default parameters if no parent */
57 static WCHAR current_dir[MAX_NT_PATH_LENGTH];
58 static RTL_BITMAP tls_bitmap;
59 static RTL_BITMAP tls_expansion_bitmap;
60 static LIST_ENTRY tls_links;
61 static size_t sigstack_total_size;
62 static ULONG sigstack_zero_bits;
63
64 struct wine_pthread_functions pthread_functions = { NULL };
65
66 /***********************************************************************
67  *           init_teb
68  */
69 static inline NTSTATUS init_teb( TEB *teb )
70 {
71     struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
72     struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
73
74     teb->Tib.ExceptionList = (void *)~0UL;
75     teb->Tib.StackBase     = (void *)~0UL;
76     teb->Tib.Self          = &teb->Tib;
77     teb->StaticUnicodeString.Buffer        = teb->StaticUnicodeBuffer;
78     teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
79
80     if (!(thread_regs->fs = wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS;
81     thread_data->request_fd = -1;
82     thread_data->reply_fd   = -1;
83     thread_data->wait_fd[0] = -1;
84     thread_data->wait_fd[1] = -1;
85
86     return STATUS_SUCCESS;
87 }
88
89
90 /***********************************************************************
91  *           fix_unicode_string
92  *
93  * Make sure the unicode string doesn't point beyond the end pointer
94  */
95 static inline void fix_unicode_string( UNICODE_STRING *str, char *end_ptr )
96 {
97     if ((char *)str->Buffer >= end_ptr)
98     {
99         str->Length = str->MaximumLength = 0;
100         str->Buffer = NULL;
101         return;
102     }
103     if ((char *)str->Buffer + str->MaximumLength > end_ptr)
104     {
105         str->MaximumLength = (end_ptr - (char *)str->Buffer) & ~(sizeof(WCHAR) - 1);
106     }
107     if (str->Length >= str->MaximumLength)
108     {
109         if (str->MaximumLength >= sizeof(WCHAR))
110             str->Length = str->MaximumLength - sizeof(WCHAR);
111         else
112             str->Length = str->MaximumLength = 0;
113     }
114 }
115
116
117 /***********************************************************************
118  *           init_user_process_params
119  *
120  * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
121  */
122 static NTSTATUS init_user_process_params( SIZE_T info_size, HANDLE *exe_file )
123 {
124     void *ptr;
125     SIZE_T env_size;
126     NTSTATUS status;
127     RTL_USER_PROCESS_PARAMETERS *params = NULL;
128
129     status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&params, 0, &info_size,
130                                       MEM_COMMIT, PAGE_READWRITE );
131     if (status != STATUS_SUCCESS) return status;
132
133     params->AllocationSize = info_size;
134     NtCurrentTeb()->Peb->ProcessParameters = params;
135
136     SERVER_START_REQ( get_startup_info )
137     {
138         wine_server_set_reply( req, params, info_size );
139         if (!(status = wine_server_call( req )))
140         {
141             info_size = wine_server_reply_size( reply );
142             *exe_file = reply->exe_file;
143             params->hStdInput  = reply->hstdin;
144             params->hStdOutput = reply->hstdout;
145             params->hStdError  = reply->hstderr;
146         }
147     }
148     SERVER_END_REQ;
149     if (status != STATUS_SUCCESS) return status;
150
151     if (params->Size > info_size) params->Size = info_size;
152
153     /* make sure the strings are valid */
154     fix_unicode_string( &params->CurrentDirectory.DosPath, (char *)info_size );
155     fix_unicode_string( &params->DllPath, (char *)info_size );
156     fix_unicode_string( &params->ImagePathName, (char *)info_size );
157     fix_unicode_string( &params->CommandLine, (char *)info_size );
158     fix_unicode_string( &params->WindowTitle, (char *)info_size );
159     fix_unicode_string( &params->Desktop, (char *)info_size );
160     fix_unicode_string( &params->ShellInfo, (char *)info_size );
161     fix_unicode_string( &params->RuntimeInfo, (char *)info_size );
162
163     /* environment needs to be a separate memory block */
164     env_size = info_size - params->Size;
165     if (!env_size) env_size = 1;
166     ptr = NULL;
167     status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &env_size,
168                                       MEM_COMMIT, PAGE_READWRITE );
169     if (status != STATUS_SUCCESS) return status;
170     memcpy( ptr, (char *)params + params->Size, info_size - params->Size );
171     params->Environment = ptr;
172
173     RtlNormalizeProcessParams( params );
174     return status;
175 }
176
177
178 /***********************************************************************
179  *           thread_init
180  *
181  * Setup the initial thread.
182  *
183  * NOTES: The first allocated TEB on NT is at 0x7ffde000.
184  */
185 HANDLE thread_init(void)
186 {
187     PEB *peb;
188     TEB *teb;
189     void *addr;
190     SIZE_T size, info_size;
191     HANDLE exe_file = 0;
192     struct ntdll_thread_data *thread_data;
193     struct ntdll_thread_regs *thread_regs;
194     struct wine_pthread_thread_info thread_info;
195     static struct debug_info debug_info;  /* debug info for initial thread */
196
197     virtual_init();
198
199     /* reserve space for shared user data */
200
201     addr = (void *)0x7ffe0000;
202     size = 0x10000;
203     NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 0, &size, MEM_RESERVE, PAGE_READONLY );
204
205     /* allocate and initialize the PEB */
206
207     addr = NULL;
208     size = sizeof(*peb);
209     NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 1, &size,
210                              MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
211     peb = addr;
212
213     peb->NumberOfProcessors = 1;
214     peb->ProcessParameters  = &params;
215     peb->TlsBitmap          = &tls_bitmap;
216     peb->TlsExpansionBitmap = &tls_expansion_bitmap;
217     peb->LdrData            = &ldr;
218     params.CurrentDirectory.DosPath.Buffer = current_dir;
219     params.CurrentDirectory.DosPath.MaximumLength = sizeof(current_dir);
220     params.wShowWindow = 1; /* SW_SHOWNORMAL */
221     RtlInitializeBitMap( &tls_bitmap, peb->TlsBitmapBits, sizeof(peb->TlsBitmapBits) * 8 );
222     RtlInitializeBitMap( &tls_expansion_bitmap, peb->TlsExpansionBitmapBits,
223                          sizeof(peb->TlsExpansionBitmapBits) * 8 );
224     InitializeListHead( &ldr.InLoadOrderModuleList );
225     InitializeListHead( &ldr.InMemoryOrderModuleList );
226     InitializeListHead( &ldr.InInitializationOrderModuleList );
227     InitializeListHead( &tls_links );
228
229     /* allocate and initialize the initial TEB */
230
231     sigstack_total_size = get_signal_stack_total_size();
232     while (1 << sigstack_zero_bits < sigstack_total_size) sigstack_zero_bits++;
233     assert( 1 << sigstack_zero_bits == sigstack_total_size );  /* must be a power of 2 */
234     assert( sigstack_total_size >= sizeof(TEB) + sizeof(struct startup_info) );
235     thread_info.teb_size = sigstack_total_size;
236
237     addr = NULL;
238     size = sigstack_total_size;
239     NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
240                              &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
241     teb = addr;
242     teb->Peb = peb;
243     thread_info.teb_size = size;
244     init_teb( teb );
245     thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
246     thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
247     thread_data->debug_info = &debug_info;
248     InsertHeadList( &tls_links, &teb->TlsLinks );
249
250     thread_info.stack_base = NULL;
251     thread_info.stack_size = 0;
252     thread_info.teb_base   = teb;
253     thread_info.teb_sel    = thread_regs->fs;
254     wine_pthread_get_functions( &pthread_functions, sizeof(pthread_functions) );
255     pthread_functions.init_current_teb( &thread_info );
256     pthread_functions.init_thread( &thread_info );
257     virtual_init_threading();
258
259     debug_info.str_pos = debug_info.strings;
260     debug_info.out_pos = debug_info.output;
261     debug_init();
262
263     /* setup the server connection */
264     server_init_process();
265     info_size = server_init_thread( thread_info.pid, thread_info.tid, NULL );
266
267     /* create the process heap */
268     if (!(peb->ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL )))
269     {
270         MESSAGE( "wine: failed to create the process heap\n" );
271         exit(1);
272     }
273
274     /* allocate user parameters */
275     if (info_size)
276     {
277         init_user_process_params( info_size, &exe_file );
278     }
279     else
280     {
281         /* This is wine specific: we have no parent (we're started from unix)
282          * so, create a simple console with bare handles to unix stdio
283          */
284         wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE,  OBJ_INHERIT, &params.hStdInput );
285         wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdOutput );
286         wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdError );
287     }
288     return exe_file;
289 }
290
291 typedef LONG (WINAPI *PUNHANDLED_EXCEPTION_FILTER)(PEXCEPTION_POINTERS);
292 static PUNHANDLED_EXCEPTION_FILTER get_unhandled_exception_filter(void)
293 {
294     static PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter;
295     static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
296     UNICODE_STRING module_name;
297     ANSI_STRING func_name;
298     HMODULE kernel32_handle;
299
300     if (unhandled_exception_filter) return unhandled_exception_filter;
301
302     RtlInitUnicodeString(&module_name, kernel32W);
303     RtlInitAnsiString( &func_name, "UnhandledExceptionFilter" );
304
305     if (LdrGetDllHandle( 0, 0, &module_name, &kernel32_handle ) == STATUS_SUCCESS)
306         LdrGetProcedureAddress( kernel32_handle, &func_name, 0,
307                                 (void **)&unhandled_exception_filter );
308
309     return unhandled_exception_filter;
310 }
311
312 /***********************************************************************
313  *           start_thread
314  *
315  * Startup routine for a newly created thread.
316  */
317 static void start_thread( struct wine_pthread_thread_info *info )
318 {
319     TEB *teb = info->teb_base;
320     struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
321     struct startup_info *startup_info = (struct startup_info *)info;
322     PRTL_THREAD_START_ROUTINE func = startup_info->entry_point;
323     void *arg = startup_info->entry_arg;
324     struct debug_info debug_info;
325     SIZE_T size, page_size = getpagesize();
326
327     debug_info.str_pos = debug_info.strings;
328     debug_info.out_pos = debug_info.output;
329     thread_data->debug_info = &debug_info;
330
331     pthread_functions.init_current_teb( info );
332     SIGNAL_Init();
333     server_init_thread( info->pid, info->tid, func );
334     pthread_functions.init_thread( info );
335
336     /* allocate a memory view for the stack */
337     size = info->stack_size;
338     teb->DeallocationStack = info->stack_base;
339     NtAllocateVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, 0,
340                              &size, MEM_SYSTEM, PAGE_READWRITE );
341     /* limit is lower than base since the stack grows down */
342     teb->Tib.StackBase  = (char *)info->stack_base + info->stack_size;
343     teb->Tib.StackLimit = (char *)info->stack_base + page_size;
344
345     /* setup the guard page */
346     size = page_size;
347     NtProtectVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, &size, PAGE_NOACCESS, NULL );
348
349     pthread_functions.sigprocmask( SIG_UNBLOCK, &server_block_set, NULL );
350
351     RtlAcquirePebLock();
352     InsertHeadList( &tls_links, &teb->TlsLinks );
353     RtlReleasePebLock();
354
355     /* NOTE: Windows does not have an exception handler around the call to
356      * the thread attach. We do for ease of debugging */
357     if (get_unhandled_exception_filter())
358     {
359         __TRY
360         {
361             MODULE_DllThreadAttach( NULL );
362         }
363         __EXCEPT(get_unhandled_exception_filter())
364         {
365             NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
366         }
367         __ENDTRY
368     }
369     else
370         MODULE_DllThreadAttach( NULL );
371
372     func( arg );
373 }
374
375
376 /***********************************************************************
377  *              RtlCreateUserThread   (NTDLL.@)
378  */
379 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
380                                      BOOLEAN suspended, PVOID stack_addr,
381                                      SIZE_T stack_reserve, SIZE_T stack_commit,
382                                      PRTL_THREAD_START_ROUTINE start, void *param,
383                                      HANDLE *handle_ptr, CLIENT_ID *id )
384 {
385     sigset_t sigset;
386     struct ntdll_thread_data *thread_data;
387     struct ntdll_thread_regs *thread_regs = NULL;
388     struct startup_info *info = NULL;
389     void *addr = NULL;
390     HANDLE handle = 0;
391     TEB *teb;
392     DWORD tid = 0;
393     int request_pipe[2];
394     NTSTATUS status;
395     SIZE_T size, page_size = getpagesize();
396
397     if( ! is_current_process( process ) )
398     {
399         ERR("Unsupported on other process\n");
400         return STATUS_ACCESS_DENIED;
401     }
402
403     if (pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
404     fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
405     wine_server_send_fd( request_pipe[0] );
406
407     SERVER_START_REQ( new_thread )
408     {
409         req->access     = THREAD_ALL_ACCESS;
410         req->attributes = 0;  /* FIXME */
411         req->suspend    = suspended;
412         req->request_fd = request_pipe[0];
413         if (!(status = wine_server_call( req )))
414         {
415             handle = reply->handle;
416             tid = reply->tid;
417         }
418         close( request_pipe[0] );
419     }
420     SERVER_END_REQ;
421
422     if (status)
423     {
424         close( request_pipe[1] );
425         return status;
426     }
427
428     pthread_functions.sigprocmask( SIG_BLOCK, &server_block_set, &sigset );
429
430     addr = NULL;
431     size = sigstack_total_size;
432     if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
433                                            &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
434         goto error;
435     teb = addr;
436     teb->Peb = NtCurrentTeb()->Peb;
437     info = (struct startup_info *)(teb + 1);
438     info->pthread_info.teb_size = size;
439     if ((status = init_teb( teb ))) goto error;
440
441     teb->ClientId.UniqueProcess = (HANDLE)GetCurrentProcessId();
442     teb->ClientId.UniqueThread  = (HANDLE)tid;
443
444     thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
445     thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
446     thread_data->request_fd  = request_pipe[1];
447
448     info->pthread_info.teb_base = teb;
449     info->pthread_info.teb_sel  = thread_regs->fs;
450
451     /* inherit debug registers from parent thread */
452     thread_regs->dr0 = ntdll_get_thread_regs()->dr0;
453     thread_regs->dr1 = ntdll_get_thread_regs()->dr1;
454     thread_regs->dr2 = ntdll_get_thread_regs()->dr2;
455     thread_regs->dr3 = ntdll_get_thread_regs()->dr3;
456     thread_regs->dr6 = ntdll_get_thread_regs()->dr6;
457     thread_regs->dr7 = ntdll_get_thread_regs()->dr7;
458
459     if (!stack_reserve || !stack_commit)
460     {
461         IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
462         if (!stack_reserve) stack_reserve = nt->OptionalHeader.SizeOfStackReserve;
463         if (!stack_commit) stack_commit = nt->OptionalHeader.SizeOfStackCommit;
464     }
465     if (stack_reserve < stack_commit) stack_reserve = stack_commit;
466     stack_reserve += page_size;  /* for the guard page */
467     stack_reserve = (stack_reserve + 0xffff) & ~0xffff;  /* round to 64K boundary */
468     if (stack_reserve < 1024 * 1024) stack_reserve = 1024 * 1024;  /* Xlib needs a large stack */
469
470     info->pthread_info.stack_base = NULL;
471     info->pthread_info.stack_size = stack_reserve;
472     info->pthread_info.entry      = start_thread;
473     info->entry_point             = start;
474     info->entry_arg               = param;
475
476     if (pthread_functions.create_thread( &info->pthread_info ) == -1)
477     {
478         status = STATUS_NO_MEMORY;
479         goto error;
480     }
481     pthread_functions.sigprocmask( SIG_SETMASK, &sigset, NULL );
482
483     if (id) id->UniqueThread = (HANDLE)tid;
484     if (handle_ptr) *handle_ptr = handle;
485     else NtClose( handle );
486
487     return STATUS_SUCCESS;
488
489 error:
490     if (thread_regs) wine_ldt_free_fs( thread_regs->fs );
491     if (addr)
492     {
493         SIZE_T size = 0;
494         NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
495     }
496     if (handle) NtClose( handle );
497     pthread_functions.sigprocmask( SIG_SETMASK, &sigset, NULL );
498     close( request_pipe[1] );
499     return status;
500 }
501
502
503 /***********************************************************************
504  *           RtlExitUserThread  (NTDLL.@)
505  */
506 void WINAPI RtlExitUserThread( ULONG status )
507 {
508     LdrShutdownThread();
509     server_exit_thread( status );
510 }
511
512
513 /***********************************************************************
514  *              NtOpenThread   (NTDLL.@)
515  *              ZwOpenThread   (NTDLL.@)
516  */
517 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
518                               const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
519 {
520     NTSTATUS ret;
521
522     SERVER_START_REQ( open_thread )
523     {
524         req->tid        = (thread_id_t)id->UniqueThread;
525         req->access     = access;
526         req->attributes = attr ? attr->Attributes : 0;
527         ret = wine_server_call( req );
528         *handle = reply->handle;
529     }
530     SERVER_END_REQ;
531     return ret;
532 }
533
534
535 /******************************************************************************
536  *              NtSuspendThread   (NTDLL.@)
537  *              ZwSuspendThread   (NTDLL.@)
538  */
539 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
540 {
541     NTSTATUS ret;
542
543     SERVER_START_REQ( suspend_thread )
544     {
545         req->handle = handle;
546         if (!(ret = wine_server_call( req ))) *count = reply->count;
547     }
548     SERVER_END_REQ;
549     return ret;
550 }
551
552
553 /******************************************************************************
554  *              NtResumeThread   (NTDLL.@)
555  *              ZwResumeThread   (NTDLL.@)
556  */
557 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
558 {
559     NTSTATUS ret;
560
561     SERVER_START_REQ( resume_thread )
562     {
563         req->handle = handle;
564         if (!(ret = wine_server_call( req ))) *count = reply->count;
565     }
566     SERVER_END_REQ;
567     return ret;
568 }
569
570
571 /******************************************************************************
572  *              NtAlertResumeThread   (NTDLL.@)
573  *              ZwAlertResumeThread   (NTDLL.@)
574  */
575 NTSTATUS WINAPI NtAlertResumeThread( HANDLE handle, PULONG count )
576 {
577     FIXME( "stub: should alert thread %p\n", handle );
578     return NtResumeThread( handle, count );
579 }
580
581
582 /******************************************************************************
583  *              NtAlertThread   (NTDLL.@)
584  *              ZwAlertThread   (NTDLL.@)
585  */
586 NTSTATUS WINAPI NtAlertThread( HANDLE handle )
587 {
588     FIXME( "stub: %p\n", handle );
589     return STATUS_NOT_IMPLEMENTED;
590 }
591
592
593 /******************************************************************************
594  *              NtTerminateThread  (NTDLL.@)
595  *              ZwTerminateThread  (NTDLL.@)
596  */
597 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
598 {
599     NTSTATUS ret;
600     BOOL self, last;
601
602     SERVER_START_REQ( terminate_thread )
603     {
604         req->handle    = handle;
605         req->exit_code = exit_code;
606         ret = wine_server_call( req );
607         self = !ret && reply->self;
608         last = reply->last;
609     }
610     SERVER_END_REQ;
611
612     if (self)
613     {
614         if (last) exit( exit_code );
615         else server_abort_thread( exit_code );
616     }
617     return ret;
618 }
619
620
621 /******************************************************************************
622  *              NtQueueApcThread  (NTDLL.@)
623  */
624 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
625                                   ULONG_PTR arg2, ULONG_PTR arg3 )
626 {
627     NTSTATUS ret;
628     SERVER_START_REQ( queue_apc )
629     {
630         req->thread = handle;
631         if (func)
632         {
633             req->call.type         = APC_USER;
634             req->call.user.func    = func;
635             req->call.user.args[0] = arg1;
636             req->call.user.args[1] = arg2;
637             req->call.user.args[2] = arg3;
638         }
639         else req->call.type = APC_NONE;  /* wake up only */
640         ret = wine_server_call( req );
641     }
642     SERVER_END_REQ;
643     return ret;
644 }
645
646
647 /***********************************************************************
648  *              NtSetContextThread  (NTDLL.@)
649  *              ZwSetContextThread  (NTDLL.@)
650  */
651 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
652 {
653     NTSTATUS ret;
654     DWORD dummy, i;
655     BOOL self = FALSE;
656
657 #ifdef __i386__
658     /* on i386 debug registers always require a server call */
659     self = (handle == GetCurrentThread());
660     if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
661     {
662         struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
663         self = (regs->dr0 == context->Dr0 && regs->dr1 == context->Dr1 &&
664                 regs->dr2 == context->Dr2 && regs->dr3 == context->Dr3 &&
665                 regs->dr6 == context->Dr6 && regs->dr7 == context->Dr7);
666     }
667 #endif
668
669     if (!self)
670     {
671         SERVER_START_REQ( set_thread_context )
672         {
673             req->handle  = handle;
674             req->flags   = context->ContextFlags;
675             req->suspend = 0;
676             wine_server_add_data( req, context, sizeof(*context) );
677             ret = wine_server_call( req );
678             self = reply->self;
679         }
680         SERVER_END_REQ;
681
682         if (ret == STATUS_PENDING)
683         {
684             if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
685             {
686                 for (i = 0; i < 100; i++)
687                 {
688                     SERVER_START_REQ( set_thread_context )
689                     {
690                         req->handle  = handle;
691                         req->flags   = context->ContextFlags;
692                         req->suspend = 0;
693                         wine_server_add_data( req, context, sizeof(*context) );
694                         ret = wine_server_call( req );
695                     }
696                     SERVER_END_REQ;
697                     if (ret != STATUS_PENDING) break;
698                     NtYieldExecution();
699                 }
700                 NtResumeThread( handle, &dummy );
701             }
702             if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
703         }
704
705         if (ret) return ret;
706     }
707
708     if (self) set_cpu_context( context );
709     return STATUS_SUCCESS;
710 }
711
712
713 /* copy a context structure according to the flags */
714 static inline void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
715 {
716 #ifdef __i386__
717     flags &= ~CONTEXT_i386;  /* get rid of CPU id */
718     if (flags & CONTEXT_INTEGER)
719     {
720         to->Eax = from->Eax;
721         to->Ebx = from->Ebx;
722         to->Ecx = from->Ecx;
723         to->Edx = from->Edx;
724         to->Esi = from->Esi;
725         to->Edi = from->Edi;
726     }
727     if (flags & CONTEXT_CONTROL)
728     {
729         to->Ebp    = from->Ebp;
730         to->Esp    = from->Esp;
731         to->Eip    = from->Eip;
732         to->SegCs  = from->SegCs;
733         to->SegSs  = from->SegSs;
734         to->EFlags = from->EFlags;
735     }
736     if (flags & CONTEXT_SEGMENTS)
737     {
738         to->SegDs = from->SegDs;
739         to->SegEs = from->SegEs;
740         to->SegFs = from->SegFs;
741         to->SegGs = from->SegGs;
742     }
743     if (flags & CONTEXT_DEBUG_REGISTERS)
744     {
745         to->Dr0 = from->Dr0;
746         to->Dr1 = from->Dr1;
747         to->Dr2 = from->Dr2;
748         to->Dr3 = from->Dr3;
749         to->Dr6 = from->Dr6;
750         to->Dr7 = from->Dr7;
751     }
752     if (flags & CONTEXT_FLOATING_POINT)
753     {
754         to->FloatSave = from->FloatSave;
755     }
756 #elif defined(__x86_64__)
757     flags &= ~CONTEXT_AMD64;  /* get rid of CPU id */
758     if (flags & CONTEXT_CONTROL)
759     {
760         to->Rbp    = from->Rbp;
761         to->Rip    = from->Rip;
762         to->Rsp    = from->Rsp;
763         to->SegCs  = from->SegCs;
764         to->SegSs  = from->SegSs;
765         to->EFlags = from->EFlags;
766         to->MxCsr  = from->MxCsr;
767     }
768     if (flags & CONTEXT_INTEGER)
769     {
770         to->Rax = from->Rax;
771         to->Rcx = from->Rcx;
772         to->Rdx = from->Rdx;
773         to->Rbx = from->Rbx;
774         to->Rsi = from->Rsi;
775         to->Rdi = from->Rdi;
776         to->R8  = from->R8;
777         to->R9  = from->R9;
778         to->R10 = from->R10;
779         to->R11 = from->R11;
780         to->R12 = from->R12;
781         to->R13 = from->R13;
782         to->R14 = from->R14;
783         to->R15 = from->R15;
784     }
785     if (flags & CONTEXT_SEGMENTS)
786     {
787         to->SegDs = from->SegDs;
788         to->SegEs = from->SegEs;
789         to->SegFs = from->SegFs;
790         to->SegGs = from->SegGs;
791     }
792     if (flags & CONTEXT_FLOATING_POINT)
793     {
794         to->u.FltSave = from->u.FltSave;
795     }
796     if (flags & CONTEXT_DEBUG_REGISTERS)
797     {
798         to->Dr0 = from->Dr0;
799         to->Dr1 = from->Dr1;
800         to->Dr2 = from->Dr2;
801         to->Dr3 = from->Dr3;
802         to->Dr6 = from->Dr6;
803         to->Dr7 = from->Dr7;
804     }
805 #elif defined(__sparc__)
806     flags &= ~CONTEXT_SPARC;  /* get rid of CPU id */
807     if (flags & CONTEXT_CONTROL)
808     {
809         to->psr = from->psr;
810         to->pc  = from->pc;
811         to->npc = from->npc;
812         to->y   = from->y;
813         to->wim = from->wim;
814         to->tbr = from->tbr;
815     }
816     if (flags & CONTEXT_INTEGER)
817     {
818         to->g0 = from->g0;
819         to->g1 = from->g1;
820         to->g2 = from->g2;
821         to->g3 = from->g3;
822         to->g4 = from->g4;
823         to->g5 = from->g5;
824         to->g6 = from->g6;
825         to->g7 = from->g7;
826         to->o0 = from->o0;
827         to->o1 = from->o1;
828         to->o2 = from->o2;
829         to->o3 = from->o3;
830         to->o4 = from->o4;
831         to->o5 = from->o5;
832         to->o6 = from->o6;
833         to->o7 = from->o7;
834         to->l0 = from->l0;
835         to->l1 = from->l1;
836         to->l2 = from->l2;
837         to->l3 = from->l3;
838         to->l4 = from->l4;
839         to->l5 = from->l5;
840         to->l6 = from->l6;
841         to->l7 = from->l7;
842         to->i0 = from->i0;
843         to->i1 = from->i1;
844         to->i2 = from->i2;
845         to->i3 = from->i3;
846         to->i4 = from->i4;
847         to->i5 = from->i5;
848         to->i6 = from->i6;
849         to->i7 = from->i7;
850     }
851     if (flags & CONTEXT_FLOATING_POINT)
852     {
853         /* FIXME */
854     }
855 #elif defined(__powerpc__)
856     /* Has no CPU id */
857     if (flags & CONTEXT_CONTROL)
858     {
859         to->Msr = from->Msr;
860         to->Ctr = from->Ctr;
861         to->Iar = from->Iar;
862     }
863     if (flags & CONTEXT_INTEGER)
864     {
865         to->Gpr0  = from->Gpr0;
866         to->Gpr1  = from->Gpr1;
867         to->Gpr2  = from->Gpr2;
868         to->Gpr3  = from->Gpr3;
869         to->Gpr4  = from->Gpr4;
870         to->Gpr5  = from->Gpr5;
871         to->Gpr6  = from->Gpr6;
872         to->Gpr7  = from->Gpr7;
873         to->Gpr8  = from->Gpr8;
874         to->Gpr9  = from->Gpr9;
875         to->Gpr10 = from->Gpr10;
876         to->Gpr11 = from->Gpr11;
877         to->Gpr12 = from->Gpr12;
878         to->Gpr13 = from->Gpr13;
879         to->Gpr14 = from->Gpr14;
880         to->Gpr15 = from->Gpr15;
881         to->Gpr16 = from->Gpr16;
882         to->Gpr17 = from->Gpr17;
883         to->Gpr18 = from->Gpr18;
884         to->Gpr19 = from->Gpr19;
885         to->Gpr20 = from->Gpr20;
886         to->Gpr21 = from->Gpr21;
887         to->Gpr22 = from->Gpr22;
888         to->Gpr23 = from->Gpr23;
889         to->Gpr24 = from->Gpr24;
890         to->Gpr25 = from->Gpr25;
891         to->Gpr26 = from->Gpr26;
892         to->Gpr27 = from->Gpr27;
893         to->Gpr28 = from->Gpr28;
894         to->Gpr29 = from->Gpr29;
895         to->Gpr30 = from->Gpr30;
896         to->Gpr31 = from->Gpr31;
897         to->Xer   = from->Xer;
898         to->Cr    = from->Cr;
899     }
900     if (flags & CONTEXT_FLOATING_POINT)
901     {
902         to->Fpr0  = from->Fpr0;
903         to->Fpr1  = from->Fpr1;
904         to->Fpr2  = from->Fpr2;
905         to->Fpr3  = from->Fpr3;
906         to->Fpr4  = from->Fpr4;
907         to->Fpr5  = from->Fpr5;
908         to->Fpr6  = from->Fpr6;
909         to->Fpr7  = from->Fpr7;
910         to->Fpr8  = from->Fpr8;
911         to->Fpr9  = from->Fpr9;
912         to->Fpr10 = from->Fpr10;
913         to->Fpr11 = from->Fpr11;
914         to->Fpr12 = from->Fpr12;
915         to->Fpr13 = from->Fpr13;
916         to->Fpr14 = from->Fpr14;
917         to->Fpr15 = from->Fpr15;
918         to->Fpr16 = from->Fpr16;
919         to->Fpr17 = from->Fpr17;
920         to->Fpr18 = from->Fpr18;
921         to->Fpr19 = from->Fpr19;
922         to->Fpr20 = from->Fpr20;
923         to->Fpr21 = from->Fpr21;
924         to->Fpr22 = from->Fpr22;
925         to->Fpr23 = from->Fpr23;
926         to->Fpr24 = from->Fpr24;
927         to->Fpr25 = from->Fpr25;
928         to->Fpr26 = from->Fpr26;
929         to->Fpr27 = from->Fpr27;
930         to->Fpr28 = from->Fpr28;
931         to->Fpr29 = from->Fpr29;
932         to->Fpr30 = from->Fpr30;
933         to->Fpr31 = from->Fpr31;
934         to->Fpscr = from->Fpscr;
935     }
936 #else
937 #error You must implement context copying for your CPU
938 #endif
939 }
940
941
942 /***********************************************************************
943  *              NtGetContextThread  (NTDLL.@)
944  *              ZwGetContextThread  (NTDLL.@)
945  */
946 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
947 {
948     NTSTATUS ret;
949     CONTEXT ctx;
950     DWORD dummy, i;
951     DWORD needed_flags = context->ContextFlags;
952     BOOL self = (handle == GetCurrentThread());
953
954 #ifdef __i386__
955     /* on i386 debug registers always require a server call */
956     if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)) self = FALSE;
957 #endif
958
959     if (!self)
960     {
961         SERVER_START_REQ( get_thread_context )
962         {
963             req->handle  = handle;
964             req->flags   = context->ContextFlags;
965             req->suspend = 0;
966             wine_server_set_reply( req, &ctx, sizeof(ctx) );
967             ret = wine_server_call( req );
968             self = reply->self;
969         }
970         SERVER_END_REQ;
971
972         if (ret == STATUS_PENDING)
973         {
974             if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
975             {
976                 for (i = 0; i < 100; i++)
977                 {
978                     SERVER_START_REQ( get_thread_context )
979                     {
980                         req->handle  = handle;
981                         req->flags   = context->ContextFlags;
982                         req->suspend = 0;
983                         wine_server_set_reply( req, &ctx, sizeof(ctx) );
984                         ret = wine_server_call( req );
985                     }
986                     SERVER_END_REQ;
987                     if (ret != STATUS_PENDING) break;
988                     NtYieldExecution();
989                 }
990                 NtResumeThread( handle, &dummy );
991             }
992             if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
993         }
994         if (ret) return ret;
995         copy_context( context, &ctx, context->ContextFlags & ctx.ContextFlags );
996         needed_flags &= ~ctx.ContextFlags;
997     }
998
999     if (self)
1000     {
1001         if (needed_flags)
1002         {
1003             get_cpu_context( &ctx );
1004             copy_context( context, &ctx, ctx.ContextFlags & needed_flags );
1005         }
1006 #ifdef __i386__
1007         /* update the cached version of the debug registers */
1008         if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1009         {
1010             struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1011             regs->dr0 = context->Dr0;
1012             regs->dr1 = context->Dr1;
1013             regs->dr2 = context->Dr2;
1014             regs->dr3 = context->Dr3;
1015             regs->dr6 = context->Dr6;
1016             regs->dr7 = context->Dr7;
1017         }
1018 #endif
1019     }
1020     return STATUS_SUCCESS;
1021 }
1022
1023
1024 /******************************************************************************
1025  *              NtQueryInformationThread  (NTDLL.@)
1026  *              ZwQueryInformationThread  (NTDLL.@)
1027  */
1028 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
1029                                           void *data, ULONG length, ULONG *ret_len )
1030 {
1031     NTSTATUS status;
1032
1033     switch(class)
1034     {
1035     case ThreadBasicInformation:
1036         {
1037             THREAD_BASIC_INFORMATION info;
1038
1039             SERVER_START_REQ( get_thread_info )
1040             {
1041                 req->handle = handle;
1042                 req->tid_in = 0;
1043                 if (!(status = wine_server_call( req )))
1044                 {
1045                     info.ExitStatus             = reply->exit_code;
1046                     info.TebBaseAddress         = reply->teb;
1047                     info.ClientId.UniqueProcess = (HANDLE)reply->pid;
1048                     info.ClientId.UniqueThread  = (HANDLE)reply->tid;
1049                     info.AffinityMask           = reply->affinity;
1050                     info.Priority               = reply->priority;
1051                     info.BasePriority           = reply->priority;  /* FIXME */
1052                 }
1053             }
1054             SERVER_END_REQ;
1055             if (status == STATUS_SUCCESS)
1056             {
1057                 if (data) memcpy( data, &info, min( length, sizeof(info) ));
1058                 if (ret_len) *ret_len = min( length, sizeof(info) );
1059             }
1060         }
1061         return status;
1062     case ThreadTimes:
1063         {
1064             KERNEL_USER_TIMES   kusrt;
1065             /* We need to do a server call to get the creation time or exit time */
1066             /* This works on any thread */
1067             SERVER_START_REQ( get_thread_info )
1068             {
1069                 req->handle = handle;
1070                 req->tid_in = 0;
1071                 status = wine_server_call( req );
1072                 if (status == STATUS_SUCCESS)
1073                 {
1074                     NTDLL_from_server_abstime( &kusrt.CreateTime, &reply->creation_time );
1075                     NTDLL_from_server_abstime( &kusrt.ExitTime, &reply->exit_time );
1076                 }
1077             }
1078             SERVER_END_REQ;
1079             if (status == STATUS_SUCCESS)
1080             {
1081                 /* We call times(2) for kernel time or user time */
1082                 /* We can only (portably) do this for the current thread */
1083                 if (handle == GetCurrentThread())
1084                 {
1085                     struct tms time_buf;
1086                     long clocks_per_sec = sysconf(_SC_CLK_TCK);
1087
1088                     times(&time_buf);
1089                     kusrt.KernelTime.QuadPart = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
1090                     kusrt.UserTime.QuadPart = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
1091                 }
1092                 else
1093                 {
1094                     kusrt.KernelTime.QuadPart = 0;
1095                     kusrt.UserTime.QuadPart = 0;
1096                     FIXME("Cannot get kerneltime or usertime of other threads\n");
1097                 }
1098                 if (data) memcpy( data, &kusrt, min( length, sizeof(kusrt) ));
1099                 if (ret_len) *ret_len = min( length, sizeof(kusrt) );
1100             }
1101         }
1102         return status;
1103     case ThreadDescriptorTableEntry:
1104         {
1105 #ifdef __i386__
1106             THREAD_DESCRIPTOR_INFORMATION*      tdi = data;
1107             if (length < sizeof(*tdi))
1108                 status = STATUS_INFO_LENGTH_MISMATCH;
1109             else if (!(tdi->Selector & 4))  /* GDT selector */
1110             {
1111                 unsigned sel = tdi->Selector & ~3;  /* ignore RPL */
1112                 status = STATUS_SUCCESS;
1113                 if (!sel)  /* null selector */
1114                     memset( &tdi->Entry, 0, sizeof(tdi->Entry) );
1115                 else
1116                 {
1117                     tdi->Entry.BaseLow                   = 0;
1118                     tdi->Entry.HighWord.Bits.BaseMid     = 0;
1119                     tdi->Entry.HighWord.Bits.BaseHi      = 0;
1120                     tdi->Entry.LimitLow                  = 0xffff;
1121                     tdi->Entry.HighWord.Bits.LimitHi     = 0xf;
1122                     tdi->Entry.HighWord.Bits.Dpl         = 3;
1123                     tdi->Entry.HighWord.Bits.Sys         = 0;
1124                     tdi->Entry.HighWord.Bits.Pres        = 1;
1125                     tdi->Entry.HighWord.Bits.Granularity = 1;
1126                     tdi->Entry.HighWord.Bits.Default_Big = 1;
1127                     tdi->Entry.HighWord.Bits.Type        = 0x12;
1128                     /* it has to be one of the system GDT selectors */
1129                     if (sel != (wine_get_ds() & ~3) && sel != (wine_get_ss() & ~3))
1130                     {
1131                         if (sel == (wine_get_cs() & ~3))
1132                             tdi->Entry.HighWord.Bits.Type |= 8;  /* code segment */
1133                         else status = STATUS_ACCESS_DENIED;
1134                     }
1135                 }
1136             }
1137             else
1138             {
1139                 SERVER_START_REQ( get_selector_entry )
1140                 {
1141                     req->handle = handle;
1142                     req->entry = tdi->Selector >> 3;
1143                     status = wine_server_call( req );
1144                     if (!status)
1145                     {
1146                         if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
1147                             status = STATUS_ACCESS_VIOLATION;
1148                         else
1149                         {
1150                             wine_ldt_set_base ( &tdi->Entry, (void *)reply->base );
1151                             wine_ldt_set_limit( &tdi->Entry, reply->limit );
1152                             wine_ldt_set_flags( &tdi->Entry, reply->flags );
1153                         }
1154                     }
1155                 }
1156                 SERVER_END_REQ;
1157             }
1158             if (status == STATUS_SUCCESS && ret_len)
1159                 /* yes, that's a bit strange, but it's the way it is */
1160                 *ret_len = sizeof(LDT_ENTRY);
1161 #else
1162             status = STATUS_NOT_IMPLEMENTED;
1163 #endif
1164             return status;
1165         }
1166     case ThreadAmILastThread:
1167         {
1168             SERVER_START_REQ(get_thread_info)
1169             {
1170                 req->handle = handle;
1171                 req->tid_in = 0;
1172                 status = wine_server_call( req );
1173                 if (status == STATUS_SUCCESS)
1174                 {
1175                     BOOLEAN last = reply->last;
1176                     if (data) memcpy( data, &last, min( length, sizeof(last) ));
1177                     if (ret_len) *ret_len = min( length, sizeof(last) );
1178                 }
1179             }
1180             SERVER_END_REQ;
1181             return status;
1182         }
1183     case ThreadPriority:
1184     case ThreadBasePriority:
1185     case ThreadAffinityMask:
1186     case ThreadImpersonationToken:
1187     case ThreadEnableAlignmentFaultFixup:
1188     case ThreadEventPair_Reusable:
1189     case ThreadQuerySetWin32StartAddress:
1190     case ThreadZeroTlsCell:
1191     case ThreadPerformanceCount:
1192     case ThreadIdealProcessor:
1193     case ThreadPriorityBoost:
1194     case ThreadSetTlsArrayAddress:
1195     case ThreadIsIoPending:
1196     default:
1197         FIXME( "info class %d not supported yet\n", class );
1198         return STATUS_NOT_IMPLEMENTED;
1199     }
1200 }
1201
1202
1203 /******************************************************************************
1204  *              NtSetInformationThread  (NTDLL.@)
1205  *              ZwSetInformationThread  (NTDLL.@)
1206  */
1207 NTSTATUS WINAPI NtSetInformationThread( HANDLE handle, THREADINFOCLASS class,
1208                                         LPCVOID data, ULONG length )
1209 {
1210     NTSTATUS status;
1211     switch(class)
1212     {
1213     case ThreadZeroTlsCell:
1214         if (handle == GetCurrentThread())
1215         {
1216             LIST_ENTRY *entry;
1217             DWORD index;
1218
1219             if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1220             index = *(const DWORD *)data;
1221             if (index < TLS_MINIMUM_AVAILABLE)
1222             {
1223                 RtlAcquirePebLock();
1224                 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1225                 {
1226                     TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1227                     teb->TlsSlots[index] = 0;
1228                 }
1229                 RtlReleasePebLock();
1230             }
1231             else
1232             {
1233                 index -= TLS_MINIMUM_AVAILABLE;
1234                 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
1235                     return STATUS_INVALID_PARAMETER;
1236                 RtlAcquirePebLock();
1237                 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1238                 {
1239                     TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1240                     if (teb->TlsExpansionSlots) teb->TlsExpansionSlots[index] = 0;
1241                 }
1242                 RtlReleasePebLock();
1243             }
1244             return STATUS_SUCCESS;
1245         }
1246         FIXME( "ZeroTlsCell not supported on other threads\n" );
1247         return STATUS_NOT_IMPLEMENTED;
1248
1249     case ThreadImpersonationToken:
1250         {
1251             const HANDLE *phToken = data;
1252             if (length != sizeof(HANDLE)) return STATUS_INVALID_PARAMETER;
1253             TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken );
1254             SERVER_START_REQ( set_thread_info )
1255             {
1256                 req->handle   = handle;
1257                 req->token    = *phToken;
1258                 req->mask     = SET_THREAD_INFO_TOKEN;
1259                 status = wine_server_call( req );
1260             }
1261             SERVER_END_REQ;
1262         }
1263         return status;
1264     case ThreadBasePriority:
1265         {
1266             const DWORD *pprio = data;
1267             if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1268             SERVER_START_REQ( set_thread_info )
1269             {
1270                 req->handle   = handle;
1271                 req->priority = *pprio;
1272                 req->mask     = SET_THREAD_INFO_PRIORITY;
1273                 status = wine_server_call( req );
1274             }
1275             SERVER_END_REQ;
1276         }
1277         return status;
1278     case ThreadAffinityMask:
1279         {
1280             const DWORD *paff = data;
1281             if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1282             SERVER_START_REQ( set_thread_info )
1283             {
1284                 req->handle   = handle;
1285                 req->affinity = *paff;
1286                 req->mask     = SET_THREAD_INFO_AFFINITY;
1287                 status = wine_server_call( req );
1288             }
1289             SERVER_END_REQ;
1290         }
1291         return status;
1292     case ThreadBasicInformation:
1293     case ThreadTimes:
1294     case ThreadPriority:
1295     case ThreadDescriptorTableEntry:
1296     case ThreadEnableAlignmentFaultFixup:
1297     case ThreadEventPair_Reusable:
1298     case ThreadQuerySetWin32StartAddress:
1299     case ThreadPerformanceCount:
1300     case ThreadAmILastThread:
1301     case ThreadIdealProcessor:
1302     case ThreadPriorityBoost:
1303     case ThreadSetTlsArrayAddress:
1304     case ThreadIsIoPending:
1305     default:
1306         FIXME( "info class %d not supported yet\n", class );
1307         return STATUS_NOT_IMPLEMENTED;
1308     }
1309 }
1310
1311
1312 /**********************************************************************
1313  *           NtCurrentTeb   (NTDLL.@)
1314  */
1315 #if defined(__i386__) && defined(__GNUC__)
1316
1317 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
1318
1319 #elif defined(__i386__) && defined(_MSC_VER)
1320
1321 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
1322
1323 #else
1324
1325 /**********************************************************************/
1326
1327 TEB * WINAPI NtCurrentTeb(void)
1328 {
1329     return pthread_functions.get_current_teb();
1330 }
1331
1332 #endif  /* __i386__ */