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