4 * Copyright 1996, 2003 Alexandre Julliard
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_MMAN_H
28 #ifdef HAVE_SYS_TIMES_H
29 #include <sys/times.h>
32 #define NONAMELESSUNION
34 #define WIN32_NO_STATUS
37 #include "wine/library.h"
38 #include "wine/server.h"
39 #include "wine/pthread.h"
40 #include "wine/debug.h"
41 #include "ntdll_misc.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(thread);
45 /* info passed to a starting thread */
48 struct wine_pthread_thread_info pthread_info;
49 PRTL_THREAD_START_ROUTINE entry_point;
54 static PEB_LDR_DATA ldr;
55 static RTL_USER_PROCESS_PARAMETERS params; /* default parameters if no parent */
56 static WCHAR current_dir[MAX_NT_PATH_LENGTH];
57 static RTL_BITMAP tls_bitmap;
58 static RTL_BITMAP tls_expansion_bitmap;
59 static LIST_ENTRY tls_links;
60 static size_t sigstack_total_size;
62 struct wine_pthread_functions pthread_functions = { NULL };
64 /***********************************************************************
67 static inline NTSTATUS init_teb( TEB *teb )
69 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
70 struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
72 teb->Tib.ExceptionList = (void *)~0UL;
73 teb->Tib.StackBase = (void *)~0UL;
74 teb->Tib.Self = &teb->Tib;
76 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
77 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
79 if (!(thread_regs->fs = wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS;
80 thread_data->request_fd = -1;
81 thread_data->reply_fd = -1;
82 thread_data->wait_fd[0] = -1;
83 thread_data->wait_fd[1] = -1;
85 return STATUS_SUCCESS;
89 /***********************************************************************
92 static inline void free_teb( TEB *teb )
96 struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
98 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
99 wine_ldt_free_fs( thread_regs->fs );
100 munmap( teb, sigstack_total_size );
104 /***********************************************************************
107 * Setup the initial thread.
109 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
111 void thread_init(void)
116 struct ntdll_thread_data *thread_data;
117 struct ntdll_thread_regs *thread_regs;
118 struct wine_pthread_thread_info thread_info;
119 static struct debug_info debug_info; /* debug info for initial thread */
121 peb.NumberOfProcessors = 1;
122 peb.ProcessParameters = ¶ms;
123 peb.TlsBitmap = &tls_bitmap;
124 peb.TlsExpansionBitmap = &tls_expansion_bitmap;
126 params.CurrentDirectory.DosPath.Buffer = current_dir;
127 params.CurrentDirectory.DosPath.MaximumLength = sizeof(current_dir);
128 params.wShowWindow = 1; /* SW_SHOWNORMAL */
129 RtlInitializeBitMap( &tls_bitmap, peb.TlsBitmapBits, sizeof(peb.TlsBitmapBits) * 8 );
130 RtlInitializeBitMap( &tls_expansion_bitmap, peb.TlsExpansionBitmapBits,
131 sizeof(peb.TlsExpansionBitmapBits) * 8 );
132 InitializeListHead( &ldr.InLoadOrderModuleList );
133 InitializeListHead( &ldr.InMemoryOrderModuleList );
134 InitializeListHead( &ldr.InInitializationOrderModuleList );
135 InitializeListHead( &tls_links );
137 sigstack_total_size = get_signal_stack_total_size();
138 thread_info.teb_size = sigstack_total_size;
139 VIRTUAL_alloc_teb( &addr, thread_info.teb_size, TRUE );
142 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
143 thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
144 thread_data->debug_info = &debug_info;
145 InsertHeadList( &tls_links, &teb->TlsLinks );
147 thread_info.stack_base = NULL;
148 thread_info.stack_size = 0;
149 thread_info.teb_base = teb;
150 thread_info.teb_sel = thread_regs->fs;
151 wine_pthread_get_functions( &pthread_functions, sizeof(pthread_functions) );
152 pthread_functions.init_current_teb( &thread_info );
153 pthread_functions.init_thread( &thread_info );
155 debug_info.str_pos = debug_info.strings;
156 debug_info.out_pos = debug_info.output;
159 /* setup the server connection */
160 server_init_process();
161 info_size = server_init_thread( thread_info.pid, thread_info.tid, NULL );
163 /* create the process heap */
164 if (!(peb.ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL )))
166 MESSAGE( "wine: failed to create the process heap\n" );
170 /* allocate user parameters */
173 RTL_USER_PROCESS_PARAMETERS *params = NULL;
175 if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms, 0, &info_size,
176 MEM_COMMIT, PAGE_READWRITE ) == STATUS_SUCCESS)
178 params->AllocationSize = info_size;
179 NtCurrentTeb()->Peb->ProcessParameters = params;
184 /* This is wine specific: we have no parent (we're started from unix)
185 * so, create a simple console with bare handles to unix stdio
187 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, OBJ_INHERIT, ¶ms.hStdInput );
188 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, ¶ms.hStdOutput );
189 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, ¶ms.hStdError );
194 /***********************************************************************
197 * Startup routine for a newly created thread.
199 static void start_thread( struct wine_pthread_thread_info *info )
201 TEB *teb = info->teb_base;
202 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
203 struct startup_info *startup_info = (struct startup_info *)info;
204 PRTL_THREAD_START_ROUTINE func = startup_info->entry_point;
205 void *arg = startup_info->entry_arg;
206 struct debug_info debug_info;
207 SIZE_T size, page_size = getpagesize();
209 debug_info.str_pos = debug_info.strings;
210 debug_info.out_pos = debug_info.output;
211 thread_data->debug_info = &debug_info;
213 pthread_functions.init_current_teb( info );
215 server_init_thread( info->pid, info->tid, func );
216 pthread_functions.init_thread( info );
218 /* allocate a memory view for the stack */
219 size = info->stack_size;
220 teb->DeallocationStack = info->stack_base;
221 NtAllocateVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, 0,
222 &size, MEM_SYSTEM, PAGE_READWRITE );
223 /* limit is lower than base since the stack grows down */
224 teb->Tib.StackBase = (char *)info->stack_base + info->stack_size;
225 teb->Tib.StackLimit = (char *)info->stack_base + page_size;
227 /* setup the guard page */
229 NtProtectVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, &size, PAGE_NOACCESS, NULL );
230 RtlFreeHeap( GetProcessHeap(), 0, info );
233 InsertHeadList( &tls_links, &teb->TlsLinks );
240 /***********************************************************************
241 * RtlCreateUserThread (NTDLL.@)
243 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
244 BOOLEAN suspended, PVOID stack_addr,
245 SIZE_T stack_reserve, SIZE_T stack_commit,
246 PRTL_THREAD_START_ROUTINE start, void *param,
247 HANDLE *handle_ptr, CLIENT_ID *id )
249 struct ntdll_thread_data *thread_data;
250 struct ntdll_thread_regs *thread_regs = NULL;
251 struct startup_info *info = NULL;
258 SIZE_T page_size = getpagesize();
260 if( ! is_current_process( process ) )
262 ERR("Unsupported on other process\n");
263 return STATUS_ACCESS_DENIED;
266 if (pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
267 fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
268 wine_server_send_fd( request_pipe[0] );
270 SERVER_START_REQ( new_thread )
272 req->access = THREAD_ALL_ACCESS;
273 req->attributes = 0; /* FIXME */
274 req->suspend = suspended;
275 req->request_fd = request_pipe[0];
276 if (!(status = wine_server_call( req )))
278 handle = reply->handle;
281 close( request_pipe[0] );
285 if (status) goto error;
287 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) )))
289 status = STATUS_NO_MEMORY;
293 info->pthread_info.teb_size = sigstack_total_size;
294 if ((status = VIRTUAL_alloc_teb( &addr, info->pthread_info.teb_size, FALSE ))) goto error;
296 if ((status = init_teb( teb ))) goto error;
298 teb->ClientId.UniqueProcess = (HANDLE)GetCurrentProcessId();
299 teb->ClientId.UniqueThread = (HANDLE)tid;
301 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
302 thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
303 thread_data->request_fd = request_pipe[1];
305 info->pthread_info.teb_base = teb;
306 info->pthread_info.teb_sel = thread_regs->fs;
308 /* inherit debug registers from parent thread */
309 thread_regs->dr0 = ntdll_get_thread_regs()->dr0;
310 thread_regs->dr1 = ntdll_get_thread_regs()->dr1;
311 thread_regs->dr2 = ntdll_get_thread_regs()->dr2;
312 thread_regs->dr3 = ntdll_get_thread_regs()->dr3;
313 thread_regs->dr6 = ntdll_get_thread_regs()->dr6;
314 thread_regs->dr7 = ntdll_get_thread_regs()->dr7;
316 if (!stack_reserve || !stack_commit)
318 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
319 if (!stack_reserve) stack_reserve = nt->OptionalHeader.SizeOfStackReserve;
320 if (!stack_commit) stack_commit = nt->OptionalHeader.SizeOfStackCommit;
322 if (stack_reserve < stack_commit) stack_reserve = stack_commit;
323 stack_reserve += page_size; /* for the guard page */
324 stack_reserve = (stack_reserve + 0xffff) & ~0xffff; /* round to 64K boundary */
325 if (stack_reserve < 1024 * 1024) stack_reserve = 1024 * 1024; /* Xlib needs a large stack */
327 info->pthread_info.stack_base = NULL;
328 info->pthread_info.stack_size = stack_reserve;
329 info->pthread_info.entry = start_thread;
330 info->entry_point = start;
331 info->entry_arg = param;
333 if (pthread_functions.create_thread( &info->pthread_info ) == -1)
335 status = STATUS_NO_MEMORY;
339 if (id) id->UniqueThread = (HANDLE)tid;
340 if (handle_ptr) *handle_ptr = handle;
341 else NtClose( handle );
343 return STATUS_SUCCESS;
346 if (thread_regs) wine_ldt_free_fs( thread_regs->fs );
350 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
352 if (info) RtlFreeHeap( GetProcessHeap(), 0, info );
353 if (handle) NtClose( handle );
354 close( request_pipe[1] );
359 /***********************************************************************
360 * RtlExitUserThread (NTDLL.@)
362 void WINAPI RtlExitUserThread( ULONG status )
365 server_exit_thread( status );
369 /***********************************************************************
370 * NtOpenThread (NTDLL.@)
371 * ZwOpenThread (NTDLL.@)
373 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
374 const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
378 SERVER_START_REQ( open_thread )
380 req->tid = (thread_id_t)id->UniqueThread;
381 req->access = access;
382 req->attributes = attr ? attr->Attributes : 0;
383 ret = wine_server_call( req );
384 *handle = reply->handle;
391 /******************************************************************************
392 * NtSuspendThread (NTDLL.@)
393 * ZwSuspendThread (NTDLL.@)
395 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
399 SERVER_START_REQ( suspend_thread )
401 req->handle = handle;
402 if (!(ret = wine_server_call( req ))) *count = reply->count;
409 /******************************************************************************
410 * NtResumeThread (NTDLL.@)
411 * ZwResumeThread (NTDLL.@)
413 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
417 SERVER_START_REQ( resume_thread )
419 req->handle = handle;
420 if (!(ret = wine_server_call( req ))) *count = reply->count;
427 /******************************************************************************
428 * NtAlertResumeThread (NTDLL.@)
429 * ZwAlertResumeThread (NTDLL.@)
431 NTSTATUS WINAPI NtAlertResumeThread( HANDLE handle, PULONG count )
433 FIXME( "stub: should alert thread %p\n", handle );
434 return NtResumeThread( handle, count );
438 /******************************************************************************
439 * NtAlertThread (NTDLL.@)
440 * ZwAlertThread (NTDLL.@)
442 NTSTATUS WINAPI NtAlertThread( HANDLE handle )
444 FIXME( "stub: %p\n", handle );
445 return STATUS_NOT_IMPLEMENTED;
449 /******************************************************************************
450 * NtTerminateThread (NTDLL.@)
451 * ZwTerminateThread (NTDLL.@)
453 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
458 SERVER_START_REQ( terminate_thread )
460 req->handle = handle;
461 req->exit_code = exit_code;
462 ret = wine_server_call( req );
463 self = !ret && reply->self;
470 if (last) exit( exit_code );
471 else server_abort_thread( exit_code );
477 /******************************************************************************
478 * NtQueueApcThread (NTDLL.@)
480 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
481 ULONG_PTR arg2, ULONG_PTR arg3 )
484 SERVER_START_REQ( queue_apc )
486 req->handle = handle;
489 req->arg1 = (void *)arg1;
490 req->arg2 = (void *)arg2;
491 req->arg3 = (void *)arg3;
492 ret = wine_server_call( req );
499 /***********************************************************************
500 * NtSetContextThread (NTDLL.@)
501 * ZwSetContextThread (NTDLL.@)
503 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
510 /* on i386 debug registers always require a server call */
511 self = (handle == GetCurrentThread());
512 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
514 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
515 self = (regs->dr0 == context->Dr0 && regs->dr1 == context->Dr1 &&
516 regs->dr2 == context->Dr2 && regs->dr3 == context->Dr3 &&
517 regs->dr6 == context->Dr6 && regs->dr7 == context->Dr7);
523 SERVER_START_REQ( set_thread_context )
525 req->handle = handle;
526 req->flags = context->ContextFlags;
528 wine_server_add_data( req, context, sizeof(*context) );
529 ret = wine_server_call( req );
534 if (ret == STATUS_PENDING)
536 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
538 for (i = 0; i < 100; i++)
540 SERVER_START_REQ( set_thread_context )
542 req->handle = handle;
543 req->flags = context->ContextFlags;
545 wine_server_add_data( req, context, sizeof(*context) );
546 ret = wine_server_call( req );
549 if (ret != STATUS_PENDING) break;
552 NtResumeThread( handle, &dummy );
554 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
560 if (self) set_cpu_context( context );
561 return STATUS_SUCCESS;
565 /* copy a context structure according to the flags */
566 static inline void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
569 flags &= ~CONTEXT_i386; /* get rid of CPU id */
570 if (flags & CONTEXT_INTEGER)
579 if (flags & CONTEXT_CONTROL)
584 to->SegCs = from->SegCs;
585 to->SegSs = from->SegSs;
586 to->EFlags = from->EFlags;
588 if (flags & CONTEXT_SEGMENTS)
590 to->SegDs = from->SegDs;
591 to->SegEs = from->SegEs;
592 to->SegFs = from->SegFs;
593 to->SegGs = from->SegGs;
595 if (flags & CONTEXT_DEBUG_REGISTERS)
604 if (flags & CONTEXT_FLOATING_POINT)
606 to->FloatSave = from->FloatSave;
608 #elif defined(__x86_64__)
609 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
610 if (flags & CONTEXT_CONTROL)
615 to->SegCs = from->SegCs;
616 to->SegSs = from->SegSs;
617 to->EFlags = from->EFlags;
618 to->MxCsr = from->MxCsr;
620 if (flags & CONTEXT_INTEGER)
637 if (flags & CONTEXT_SEGMENTS)
639 to->SegDs = from->SegDs;
640 to->SegEs = from->SegEs;
641 to->SegFs = from->SegFs;
642 to->SegGs = from->SegGs;
644 if (flags & CONTEXT_FLOATING_POINT)
646 to->u.FltSave = from->u.FltSave;
648 if (flags & CONTEXT_DEBUG_REGISTERS)
657 #elif defined(__sparc__)
658 flags &= ~CONTEXT_SPARC; /* get rid of CPU id */
659 if (flags & CONTEXT_CONTROL)
668 if (flags & CONTEXT_INTEGER)
703 if (flags & CONTEXT_FLOATING_POINT)
707 #elif defined(__powerpc__)
709 if (flags & CONTEXT_CONTROL)
715 if (flags & CONTEXT_INTEGER)
717 to->Gpr0 = from->Gpr0;
718 to->Gpr1 = from->Gpr1;
719 to->Gpr2 = from->Gpr2;
720 to->Gpr3 = from->Gpr3;
721 to->Gpr4 = from->Gpr4;
722 to->Gpr5 = from->Gpr5;
723 to->Gpr6 = from->Gpr6;
724 to->Gpr7 = from->Gpr7;
725 to->Gpr8 = from->Gpr8;
726 to->Gpr9 = from->Gpr9;
727 to->Gpr10 = from->Gpr10;
728 to->Gpr11 = from->Gpr11;
729 to->Gpr12 = from->Gpr12;
730 to->Gpr13 = from->Gpr13;
731 to->Gpr14 = from->Gpr14;
732 to->Gpr15 = from->Gpr15;
733 to->Gpr16 = from->Gpr16;
734 to->Gpr17 = from->Gpr17;
735 to->Gpr18 = from->Gpr18;
736 to->Gpr19 = from->Gpr19;
737 to->Gpr20 = from->Gpr20;
738 to->Gpr21 = from->Gpr21;
739 to->Gpr22 = from->Gpr22;
740 to->Gpr23 = from->Gpr23;
741 to->Gpr24 = from->Gpr24;
742 to->Gpr25 = from->Gpr25;
743 to->Gpr26 = from->Gpr26;
744 to->Gpr27 = from->Gpr27;
745 to->Gpr28 = from->Gpr28;
746 to->Gpr29 = from->Gpr29;
747 to->Gpr30 = from->Gpr30;
748 to->Gpr31 = from->Gpr31;
752 if (flags & CONTEXT_FLOATING_POINT)
754 to->Fpr0 = from->Fpr0;
755 to->Fpr1 = from->Fpr1;
756 to->Fpr2 = from->Fpr2;
757 to->Fpr3 = from->Fpr3;
758 to->Fpr4 = from->Fpr4;
759 to->Fpr5 = from->Fpr5;
760 to->Fpr6 = from->Fpr6;
761 to->Fpr7 = from->Fpr7;
762 to->Fpr8 = from->Fpr8;
763 to->Fpr9 = from->Fpr9;
764 to->Fpr10 = from->Fpr10;
765 to->Fpr11 = from->Fpr11;
766 to->Fpr12 = from->Fpr12;
767 to->Fpr13 = from->Fpr13;
768 to->Fpr14 = from->Fpr14;
769 to->Fpr15 = from->Fpr15;
770 to->Fpr16 = from->Fpr16;
771 to->Fpr17 = from->Fpr17;
772 to->Fpr18 = from->Fpr18;
773 to->Fpr19 = from->Fpr19;
774 to->Fpr20 = from->Fpr20;
775 to->Fpr21 = from->Fpr21;
776 to->Fpr22 = from->Fpr22;
777 to->Fpr23 = from->Fpr23;
778 to->Fpr24 = from->Fpr24;
779 to->Fpr25 = from->Fpr25;
780 to->Fpr26 = from->Fpr26;
781 to->Fpr27 = from->Fpr27;
782 to->Fpr28 = from->Fpr28;
783 to->Fpr29 = from->Fpr29;
784 to->Fpr30 = from->Fpr30;
785 to->Fpr31 = from->Fpr31;
786 to->Fpscr = from->Fpscr;
789 #error You must implement context copying for your CPU
794 /***********************************************************************
795 * NtGetContextThread (NTDLL.@)
796 * ZwGetContextThread (NTDLL.@)
798 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
805 SERVER_START_REQ( get_thread_context )
807 req->handle = handle;
808 req->flags = context->ContextFlags;
810 wine_server_set_reply( req, &ctx, sizeof(ctx) );
811 ret = wine_server_call( req );
816 if (ret == STATUS_PENDING)
818 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
820 for (i = 0; i < 100; i++)
822 SERVER_START_REQ( get_thread_context )
824 req->handle = handle;
825 req->flags = context->ContextFlags;
827 wine_server_set_reply( req, &ctx, sizeof(ctx) );
828 ret = wine_server_call( req );
831 if (ret != STATUS_PENDING) break;
834 NtResumeThread( handle, &dummy );
838 if (ret == STATUS_SUCCESS)
840 copy_context( context, &ctx, context->ContextFlags );
842 /* update the cached version of the debug registers */
843 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
845 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
846 regs->dr0 = context->Dr0;
847 regs->dr1 = context->Dr1;
848 regs->dr2 = context->Dr2;
849 regs->dr3 = context->Dr3;
850 regs->dr6 = context->Dr6;
851 regs->dr7 = context->Dr7;
855 else if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
860 /******************************************************************************
861 * NtQueryInformationThread (NTDLL.@)
862 * ZwQueryInformationThread (NTDLL.@)
864 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
865 void *data, ULONG length, ULONG *ret_len )
871 case ThreadBasicInformation:
873 THREAD_BASIC_INFORMATION info;
875 SERVER_START_REQ( get_thread_info )
877 req->handle = handle;
879 if (!(status = wine_server_call( req )))
881 info.ExitStatus = reply->exit_code;
882 info.TebBaseAddress = reply->teb;
883 info.ClientId.UniqueProcess = (HANDLE)reply->pid;
884 info.ClientId.UniqueThread = (HANDLE)reply->tid;
885 info.AffinityMask = reply->affinity;
886 info.Priority = reply->priority;
887 info.BasePriority = reply->priority; /* FIXME */
891 if (status == STATUS_SUCCESS)
893 if (data) memcpy( data, &info, min( length, sizeof(info) ));
894 if (ret_len) *ret_len = min( length, sizeof(info) );
900 KERNEL_USER_TIMES kusrt;
901 /* We need to do a server call to get the creation time or exit time */
902 /* This works on any thread */
903 SERVER_START_REQ( get_thread_info )
905 req->handle = handle;
907 status = wine_server_call( req );
908 if (status == STATUS_SUCCESS)
910 RtlSecondsSince1970ToTime( reply->creation_time, &kusrt.CreateTime );
911 RtlSecondsSince1970ToTime( reply->exit_time, &kusrt.ExitTime );
915 if (status == STATUS_SUCCESS)
917 /* We call times(2) for kernel time or user time */
918 /* We can only (portably) do this for the current thread */
919 if (handle == GetCurrentThread())
922 long clocks_per_sec = sysconf(_SC_CLK_TCK);
925 kusrt.KernelTime.QuadPart = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
926 kusrt.UserTime.QuadPart = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
930 kusrt.KernelTime.QuadPart = 0;
931 kusrt.UserTime.QuadPart = 0;
932 FIXME("Cannot get kerneltime or usertime of other threads\n");
934 if (data) memcpy( data, &kusrt, min( length, sizeof(kusrt) ));
935 if (ret_len) *ret_len = min( length, sizeof(kusrt) );
940 case ThreadBasePriority:
941 case ThreadAffinityMask:
942 case ThreadImpersonationToken:
943 case ThreadDescriptorTableEntry:
944 case ThreadEnableAlignmentFaultFixup:
945 case ThreadEventPair_Reusable:
946 case ThreadQuerySetWin32StartAddress:
947 case ThreadZeroTlsCell:
948 case ThreadPerformanceCount:
949 case ThreadAmILastThread:
950 case ThreadIdealProcessor:
951 case ThreadPriorityBoost:
952 case ThreadSetTlsArrayAddress:
953 case ThreadIsIoPending:
955 FIXME( "info class %d not supported yet\n", class );
956 return STATUS_NOT_IMPLEMENTED;
961 /******************************************************************************
962 * NtSetInformationThread (NTDLL.@)
963 * ZwSetInformationThread (NTDLL.@)
965 NTSTATUS WINAPI NtSetInformationThread( HANDLE handle, THREADINFOCLASS class,
966 LPCVOID data, ULONG length )
971 case ThreadZeroTlsCell:
972 if (handle == GetCurrentThread())
977 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
978 index = *(const DWORD *)data;
979 if (index < TLS_MINIMUM_AVAILABLE)
982 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
984 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
985 teb->TlsSlots[index] = 0;
991 index -= TLS_MINIMUM_AVAILABLE;
992 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
993 return STATUS_INVALID_PARAMETER;
995 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
997 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
998 if (teb->TlsExpansionSlots) teb->TlsExpansionSlots[index] = 0;
1000 RtlReleasePebLock();
1002 return STATUS_SUCCESS;
1004 FIXME( "ZeroTlsCell not supported on other threads\n" );
1005 return STATUS_NOT_IMPLEMENTED;
1007 case ThreadImpersonationToken:
1009 const HANDLE *phToken = data;
1010 if (length != sizeof(HANDLE)) return STATUS_INVALID_PARAMETER;
1011 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken );
1012 SERVER_START_REQ( set_thread_info )
1014 req->handle = handle;
1015 req->token = *phToken;
1016 req->mask = SET_THREAD_INFO_TOKEN;
1017 status = wine_server_call( req );
1022 case ThreadBasePriority:
1024 const DWORD *pprio = data;
1025 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1026 SERVER_START_REQ( set_thread_info )
1028 req->handle = handle;
1029 req->priority = *pprio;
1030 req->mask = SET_THREAD_INFO_PRIORITY;
1031 status = wine_server_call( req );
1036 case ThreadAffinityMask:
1038 const DWORD *paff = data;
1039 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1040 SERVER_START_REQ( set_thread_info )
1042 req->handle = handle;
1043 req->affinity = *paff;
1044 req->mask = SET_THREAD_INFO_AFFINITY;
1045 status = wine_server_call( req );
1050 case ThreadBasicInformation:
1052 case ThreadPriority:
1053 case ThreadDescriptorTableEntry:
1054 case ThreadEnableAlignmentFaultFixup:
1055 case ThreadEventPair_Reusable:
1056 case ThreadQuerySetWin32StartAddress:
1057 case ThreadPerformanceCount:
1058 case ThreadAmILastThread:
1059 case ThreadIdealProcessor:
1060 case ThreadPriorityBoost:
1061 case ThreadSetTlsArrayAddress:
1062 case ThreadIsIoPending:
1064 FIXME( "info class %d not supported yet\n", class );
1065 return STATUS_NOT_IMPLEMENTED;
1070 /**********************************************************************
1071 * NtCurrentTeb (NTDLL.@)
1073 #if defined(__i386__) && defined(__GNUC__)
1075 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
1077 #elif defined(__i386__) && defined(_MSC_VER)
1079 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
1083 /**********************************************************************/
1085 TEB * WINAPI NtCurrentTeb(void)
1087 return pthread_functions.get_current_teb();
1090 #endif /* __i386__ */