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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_MMAN_H
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
34 #define NONAMELESSUNION
36 #define WIN32_NO_STATUS
38 #include "wine/library.h"
39 #include "wine/server.h"
40 #include "wine/debug.h"
41 #include "ntdll_misc.h"
43 #include "wine/exception.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
46 WINE_DECLARE_DEBUG_CHANNEL(relay
);
48 struct _KUSER_SHARED_DATA
*user_shared_data
= NULL
;
50 PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter
= NULL
;
52 /* info passed to a starting thread */
56 PRTL_THREAD_START_ROUTINE entry_point
;
60 static PEB_LDR_DATA ldr
;
61 static RTL_USER_PROCESS_PARAMETERS params
; /* default parameters if no parent */
62 static WCHAR current_dir
[MAX_NT_PATH_LENGTH
];
63 static RTL_BITMAP tls_bitmap
;
64 static RTL_BITMAP tls_expansion_bitmap
;
65 static RTL_BITMAP fls_bitmap
;
66 static LIST_ENTRY tls_links
;
67 static int nb_threads
= 1;
69 /***********************************************************************
72 * Copy a unicode string from the startup info.
74 static inline void get_unicode_string( UNICODE_STRING
*str
, WCHAR
**src
, WCHAR
**dst
, UINT len
)
78 str
->MaximumLength
= len
+ sizeof(WCHAR
);
79 memcpy( str
->Buffer
, *src
, len
);
80 str
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
81 *src
+= len
/ sizeof(WCHAR
);
82 *dst
+= len
/ sizeof(WCHAR
) + 1;
85 /***********************************************************************
86 * init_user_process_params
88 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
90 static NTSTATUS
init_user_process_params( SIZE_T data_size
, HANDLE
*exe_file
)
94 SIZE_T info_size
, env_size
, size
, alloc_size
;
97 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
99 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, data_size
)))
100 return STATUS_NO_MEMORY
;
102 SERVER_START_REQ( get_startup_info
)
104 wine_server_set_reply( req
, info
, data_size
);
105 if (!(status
= wine_server_call( req
)))
107 data_size
= wine_server_reply_size( reply
);
108 info_size
= reply
->info_size
;
109 env_size
= data_size
- info_size
;
110 *exe_file
= wine_server_ptr_handle( reply
->exe_file
);
114 if (status
!= STATUS_SUCCESS
) goto done
;
116 size
= sizeof(*params
);
117 size
+= MAX_NT_PATH_LENGTH
* sizeof(WCHAR
);
118 size
+= info
->dllpath_len
+ sizeof(WCHAR
);
119 size
+= info
->imagepath_len
+ sizeof(WCHAR
);
120 size
+= info
->cmdline_len
+ sizeof(WCHAR
);
121 size
+= info
->title_len
+ sizeof(WCHAR
);
122 size
+= info
->desktop_len
+ sizeof(WCHAR
);
123 size
+= info
->shellinfo_len
+ sizeof(WCHAR
);
124 size
+= info
->runtime_len
+ sizeof(WCHAR
);
127 status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &alloc_size
,
128 MEM_COMMIT
, PAGE_READWRITE
);
129 if (status
!= STATUS_SUCCESS
) goto done
;
131 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
132 params
->AllocationSize
= alloc_size
;
134 params
->Flags
= PROCESS_PARAMS_FLAG_NORMALIZED
;
135 params
->DebugFlags
= info
->debug_flags
;
136 params
->ConsoleHandle
= wine_server_ptr_handle( info
->console
);
137 params
->ConsoleFlags
= info
->console_flags
;
138 params
->hStdInput
= wine_server_ptr_handle( info
->hstdin
);
139 params
->hStdOutput
= wine_server_ptr_handle( info
->hstdout
);
140 params
->hStdError
= wine_server_ptr_handle( info
->hstderr
);
141 params
->dwX
= info
->x
;
142 params
->dwY
= info
->y
;
143 params
->dwXSize
= info
->xsize
;
144 params
->dwYSize
= info
->ysize
;
145 params
->dwXCountChars
= info
->xchars
;
146 params
->dwYCountChars
= info
->ychars
;
147 params
->dwFillAttribute
= info
->attribute
;
148 params
->dwFlags
= info
->flags
;
149 params
->wShowWindow
= info
->show
;
151 src
= (WCHAR
*)(info
+ 1);
152 dst
= (WCHAR
*)(params
+ 1);
154 /* current directory needs more space */
155 get_unicode_string( ¶ms
->CurrentDirectory
.DosPath
, &src
, &dst
, info
->curdir_len
);
156 params
->CurrentDirectory
.DosPath
.MaximumLength
= MAX_NT_PATH_LENGTH
* sizeof(WCHAR
);
157 dst
= (WCHAR
*)(params
+ 1) + MAX_NT_PATH_LENGTH
;
159 get_unicode_string( ¶ms
->DllPath
, &src
, &dst
, info
->dllpath_len
);
160 get_unicode_string( ¶ms
->ImagePathName
, &src
, &dst
, info
->imagepath_len
);
161 get_unicode_string( ¶ms
->CommandLine
, &src
, &dst
, info
->cmdline_len
);
162 get_unicode_string( ¶ms
->WindowTitle
, &src
, &dst
, info
->title_len
);
163 get_unicode_string( ¶ms
->Desktop
, &src
, &dst
, info
->desktop_len
);
164 get_unicode_string( ¶ms
->ShellInfo
, &src
, &dst
, info
->shellinfo_len
);
166 /* runtime info isn't a real string */
167 params
->RuntimeInfo
.Buffer
= dst
;
168 params
->RuntimeInfo
.Length
= params
->RuntimeInfo
.MaximumLength
= info
->runtime_len
;
169 memcpy( dst
, src
, info
->runtime_len
);
171 /* environment needs to be a separate memory block */
173 alloc_size
= max( 1, env_size
);
174 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &alloc_size
,
175 MEM_COMMIT
, PAGE_READWRITE
);
176 if (status
!= STATUS_SUCCESS
) goto done
;
177 memcpy( ptr
, (char *)info
+ info_size
, env_size
);
178 params
->Environment
= ptr
;
181 RtlFreeHeap( GetProcessHeap(), 0, info
);
185 /***********************************************************************
188 * This is called before the process heap is created,
189 * but after the connection to the server is established.
190 * No windows heap allocation is permitted.
192 static DWORD
get_global_flag(void)
194 static const WCHAR sessionman_keyW
[] = {'M','a','c','h','i','n','e','\\',
195 'S','y','s','t','e','m','\\',
196 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
197 'C','o','n','t','r','o','l','\\',
198 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
199 static const WCHAR global_flagW
[] = {'G','l','o','b','a','l','F','l','a','g',0};
200 OBJECT_ATTRIBUTES attr
;
201 UNICODE_STRING nameW
, valueW
;
205 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)tmp
;
208 attr
.Length
= sizeof(attr
);
209 attr
.RootDirectory
= 0;
210 attr
.ObjectName
= &nameW
;
212 attr
.SecurityDescriptor
= NULL
;
213 attr
.SecurityQualityOfService
= NULL
;
214 RtlInitUnicodeString( &nameW
, sessionman_keyW
);
216 status
= NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
);
217 if (status
!= STATUS_SUCCESS
)
220 RtlInitUnicodeString( &valueW
, global_flagW
);
221 status
= NtQueryValueKey( hkey
, &valueW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
)-1, &count
);
222 if (status
!= STATUS_SUCCESS
)
225 /* Some documents say this can be a string, so handle either type */
226 if (info
->Type
== REG_DWORD
)
227 return *(DWORD
*)info
->Data
;
228 if (info
->Type
== REG_SZ
)
229 return strtol((char *)info
->Data
, NULL
, 16);
233 /***********************************************************************
236 * Setup the initial thread.
238 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
240 HANDLE
thread_init(void)
245 SIZE_T size
, info_size
;
248 struct ntdll_thread_data
*thread_data
;
249 static struct debug_info debug_info
; /* debug info for initial thread */
253 /* reserve space for shared user data */
255 addr
= (void *)0x7ffe0000;
257 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
, MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
258 user_shared_data
= addr
;
260 /* allocate and initialize the PEB */
264 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 1, &size
,
265 MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
268 peb
->ProcessParameters
= ¶ms
;
269 peb
->TlsBitmap
= &tls_bitmap
;
270 peb
->TlsExpansionBitmap
= &tls_expansion_bitmap
;
271 peb
->FlsBitmap
= &fls_bitmap
;
273 params
.CurrentDirectory
.DosPath
.Buffer
= current_dir
;
274 params
.CurrentDirectory
.DosPath
.MaximumLength
= sizeof(current_dir
);
275 params
.wShowWindow
= 1; /* SW_SHOWNORMAL */
276 RtlInitializeBitMap( &tls_bitmap
, peb
->TlsBitmapBits
, sizeof(peb
->TlsBitmapBits
) * 8 );
277 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
->TlsExpansionBitmapBits
,
278 sizeof(peb
->TlsExpansionBitmapBits
) * 8 );
279 RtlInitializeBitMap( &fls_bitmap
, peb
->FlsBitmapBits
, sizeof(peb
->FlsBitmapBits
) * 8 );
280 InitializeListHead( &peb
->FlsListHead
);
281 InitializeListHead( &ldr
.InLoadOrderModuleList
);
282 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
283 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
284 InitializeListHead( &tls_links
);
286 /* allocate and initialize the initial TEB */
288 signal_alloc_thread( &teb
);
290 teb
->Tib
.StackBase
= (void *)~0UL;
291 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
292 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
294 thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
295 thread_data
->request_fd
= -1;
296 thread_data
->reply_fd
= -1;
297 thread_data
->wait_fd
[0] = -1;
298 thread_data
->wait_fd
[1] = -1;
299 thread_data
->debug_info
= &debug_info
;
300 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
302 signal_init_thread( teb
);
303 virtual_init_threading();
305 debug_info
.str_pos
= debug_info
.strings
;
306 debug_info
.out_pos
= debug_info
.output
;
309 /* setup the server connection */
310 server_init_process();
311 info_size
= server_init_thread( peb
);
313 /* retrieve the global flags settings from the registry */
314 peb
->NtGlobalFlag
= get_global_flag();
316 /* create the process heap */
317 if (!(peb
->ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
319 MESSAGE( "wine: failed to create the process heap\n" );
323 /* allocate user parameters */
326 init_user_process_params( info_size
, &exe_file
);
330 /* This is wine specific: we have no parent (we're started from unix)
331 * so, create a simple console with bare handles to unix stdio
333 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdInput
);
334 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
335 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
338 /* initialize time values in user_shared_data */
339 NtQuerySystemTime( &now
);
340 user_shared_data
->SystemTime
.LowPart
= now
.u
.LowPart
;
341 user_shared_data
->SystemTime
.High1Time
= user_shared_data
->SystemTime
.High2Time
= now
.u
.HighPart
;
342 user_shared_data
->u
.TickCountQuad
= (now
.QuadPart
- server_start_time
) / 10000;
343 user_shared_data
->u
.TickCount
.High2Time
= user_shared_data
->u
.TickCount
.High1Time
;
344 user_shared_data
->TickCountLowDeprecated
= user_shared_data
->u
.TickCount
.LowPart
;
345 user_shared_data
->TickCountMultiplier
= 1 << 24;
353 /***********************************************************************
356 void terminate_thread( int status
)
358 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
359 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1) _exit( status
);
361 close( ntdll_get_thread_data()->wait_fd
[0] );
362 close( ntdll_get_thread_data()->wait_fd
[1] );
363 close( ntdll_get_thread_data()->reply_fd
);
364 close( ntdll_get_thread_data()->request_fd
);
365 pthread_exit( UIntToPtr(status
) );
369 /***********************************************************************
372 void exit_thread( int status
)
374 static void *prev_teb
;
377 if (status
) /* send the exit code to the server (0 is already the default) */
379 SERVER_START_REQ( terminate_thread
)
381 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
382 req
->exit_code
= status
;
383 wine_server_call( req
);
388 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1)
390 LdrShutdownProcess();
396 RemoveEntryList( &NtCurrentTeb()->TlsLinks
);
398 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->FlsSlots
);
399 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->TlsExpansionSlots
);
401 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
403 if ((teb
= interlocked_xchg_ptr( &prev_teb
, NtCurrentTeb() )))
405 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
407 pthread_join( thread_data
->pthread_id
, NULL
);
408 signal_free_thread( teb
);
411 close( ntdll_get_thread_data()->wait_fd
[0] );
412 close( ntdll_get_thread_data()->wait_fd
[1] );
413 close( ntdll_get_thread_data()->reply_fd
);
414 close( ntdll_get_thread_data()->request_fd
);
415 pthread_exit( UIntToPtr(status
) );
419 /***********************************************************************
422 * Startup routine for a newly created thread.
424 static void start_thread( struct startup_info
*info
)
426 TEB
*teb
= info
->teb
;
427 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
428 PRTL_THREAD_START_ROUTINE func
= info
->entry_point
;
429 void *arg
= info
->entry_arg
;
430 struct debug_info debug_info
;
432 debug_info
.str_pos
= debug_info
.strings
;
433 debug_info
.out_pos
= debug_info
.output
;
434 thread_data
->debug_info
= &debug_info
;
435 thread_data
->pthread_id
= pthread_self();
437 signal_init_thread( teb
);
438 server_init_thread( func
);
439 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
442 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
445 MODULE_DllThreadAttach( NULL
);
448 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), func
, arg
);
450 call_thread_entry_point( (LPTHREAD_START_ROUTINE
)func
, arg
);
454 /***********************************************************************
455 * RtlCreateUserThread (NTDLL.@)
457 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
458 BOOLEAN suspended
, PVOID stack_addr
,
459 SIZE_T stack_reserve
, SIZE_T stack_commit
,
460 PRTL_THREAD_START_ROUTINE start
, void *param
,
461 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
464 pthread_t pthread_id
;
466 struct ntdll_thread_data
*thread_data
;
467 struct startup_info
*info
= NULL
;
474 if (process
!= NtCurrentProcess())
479 memset( &call
, 0, sizeof(call
) );
481 call
.create_thread
.type
= APC_CREATE_THREAD
;
482 call
.create_thread
.func
= wine_server_client_ptr( start
);
483 call
.create_thread
.arg
= wine_server_client_ptr( param
);
484 call
.create_thread
.reserve
= stack_reserve
;
485 call
.create_thread
.commit
= stack_commit
;
486 call
.create_thread
.suspend
= suspended
;
487 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
488 if (status
!= STATUS_SUCCESS
) return status
;
490 if (result
.create_thread
.status
== STATUS_SUCCESS
)
492 if (id
) id
->UniqueThread
= ULongToHandle(result
.create_thread
.tid
);
493 if (handle_ptr
) *handle_ptr
= wine_server_ptr_handle( result
.create_thread
.handle
);
494 else NtClose( wine_server_ptr_handle( result
.create_thread
.handle
));
496 return result
.create_thread
.status
;
499 if (server_pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
500 wine_server_send_fd( request_pipe
[0] );
502 SERVER_START_REQ( new_thread
)
504 req
->access
= THREAD_ALL_ACCESS
;
505 req
->attributes
= 0; /* FIXME */
506 req
->suspend
= suspended
;
507 req
->request_fd
= request_pipe
[0];
508 if (!(status
= wine_server_call( req
)))
510 handle
= wine_server_ptr_handle( reply
->handle
);
513 close( request_pipe
[0] );
519 close( request_pipe
[1] );
523 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
525 if ((status
= signal_alloc_thread( &teb
))) goto error
;
527 teb
->Peb
= NtCurrentTeb()->Peb
;
528 teb
->ClientId
.UniqueProcess
= ULongToHandle(GetCurrentProcessId());
529 teb
->ClientId
.UniqueThread
= ULongToHandle(tid
);
530 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
531 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
533 info
= (struct startup_info
*)(teb
+ 1);
535 info
->entry_point
= start
;
536 info
->entry_arg
= param
;
538 thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
539 thread_data
->request_fd
= request_pipe
[1];
540 thread_data
->reply_fd
= -1;
541 thread_data
->wait_fd
[0] = -1;
542 thread_data
->wait_fd
[1] = -1;
544 if ((status
= virtual_alloc_thread_stack( teb
, stack_reserve
, stack_commit
))) goto error
;
546 pthread_attr_init( &attr
);
547 pthread_attr_setstack( &attr
, teb
->DeallocationStack
,
548 (char *)teb
->Tib
.StackBase
- (char *)teb
->DeallocationStack
);
549 pthread_attr_setscope( &attr
, PTHREAD_SCOPE_SYSTEM
); /* force creating a kernel thread */
550 interlocked_xchg_add( &nb_threads
, 1 );
551 if (pthread_create( &pthread_id
, &attr
, (void * (*)(void *))start_thread
, info
))
553 interlocked_xchg_add( &nb_threads
, -1 );
554 pthread_attr_destroy( &attr
);
555 status
= STATUS_NO_MEMORY
;
558 pthread_attr_destroy( &attr
);
559 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
561 if (id
) id
->UniqueThread
= ULongToHandle(tid
);
562 if (handle_ptr
) *handle_ptr
= handle
;
563 else NtClose( handle
);
565 return STATUS_SUCCESS
;
568 if (teb
) signal_free_thread( teb
);
569 if (handle
) NtClose( handle
);
570 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
571 close( request_pipe
[1] );
576 /***********************************************************************
577 * NtOpenThread (NTDLL.@)
578 * ZwOpenThread (NTDLL.@)
580 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
581 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
585 SERVER_START_REQ( open_thread
)
587 req
->tid
= HandleToULong(id
->UniqueThread
);
588 req
->access
= access
;
589 req
->attributes
= attr
? attr
->Attributes
: 0;
590 ret
= wine_server_call( req
);
591 *handle
= wine_server_ptr_handle( reply
->handle
);
598 /******************************************************************************
599 * NtSuspendThread (NTDLL.@)
600 * ZwSuspendThread (NTDLL.@)
602 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
606 SERVER_START_REQ( suspend_thread
)
608 req
->handle
= wine_server_obj_handle( handle
);
609 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
616 /******************************************************************************
617 * NtResumeThread (NTDLL.@)
618 * ZwResumeThread (NTDLL.@)
620 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
624 SERVER_START_REQ( resume_thread
)
626 req
->handle
= wine_server_obj_handle( handle
);
627 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
634 /******************************************************************************
635 * NtAlertResumeThread (NTDLL.@)
636 * ZwAlertResumeThread (NTDLL.@)
638 NTSTATUS WINAPI
NtAlertResumeThread( HANDLE handle
, PULONG count
)
640 FIXME( "stub: should alert thread %p\n", handle
);
641 return NtResumeThread( handle
, count
);
645 /******************************************************************************
646 * NtAlertThread (NTDLL.@)
647 * ZwAlertThread (NTDLL.@)
649 NTSTATUS WINAPI
NtAlertThread( HANDLE handle
)
651 FIXME( "stub: %p\n", handle
);
652 return STATUS_NOT_IMPLEMENTED
;
656 /******************************************************************************
657 * NtTerminateThread (NTDLL.@)
658 * ZwTerminateThread (NTDLL.@)
660 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
665 SERVER_START_REQ( terminate_thread
)
667 req
->handle
= wine_server_obj_handle( handle
);
668 req
->exit_code
= exit_code
;
669 ret
= wine_server_call( req
);
670 self
= !ret
&& reply
->self
;
674 if (self
) abort_thread( exit_code
);
679 /******************************************************************************
680 * NtQueueApcThread (NTDLL.@)
682 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
683 ULONG_PTR arg2
, ULONG_PTR arg3
)
686 SERVER_START_REQ( queue_apc
)
688 req
->handle
= wine_server_obj_handle( handle
);
691 req
->call
.type
= APC_USER
;
692 req
->call
.user
.func
= wine_server_client_ptr( func
);
693 req
->call
.user
.args
[0] = arg1
;
694 req
->call
.user
.args
[1] = arg2
;
695 req
->call
.user
.args
[2] = arg3
;
697 else req
->call
.type
= APC_NONE
; /* wake up only */
698 ret
= wine_server_call( req
);
705 /***********************************************************************
706 * NtSetContextThread (NTDLL.@)
707 * ZwSetContextThread (NTDLL.@)
709 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
716 /* on i386 debug registers always require a server call */
717 self
= (handle
== GetCurrentThread());
718 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
720 self
= (ntdll_get_thread_data()->dr0
== context
->Dr0
&&
721 ntdll_get_thread_data()->dr1
== context
->Dr1
&&
722 ntdll_get_thread_data()->dr2
== context
->Dr2
&&
723 ntdll_get_thread_data()->dr3
== context
->Dr3
&&
724 ntdll_get_thread_data()->dr6
== context
->Dr6
&&
725 ntdll_get_thread_data()->dr7
== context
->Dr7
);
731 context_t server_context
;
733 context_to_server( &server_context
, context
);
735 SERVER_START_REQ( set_thread_context
)
737 req
->handle
= wine_server_obj_handle( handle
);
739 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
740 ret
= wine_server_call( req
);
745 if (ret
== STATUS_PENDING
)
747 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
749 for (i
= 0; i
< 100; i
++)
751 SERVER_START_REQ( set_thread_context
)
753 req
->handle
= wine_server_obj_handle( handle
);
755 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
756 ret
= wine_server_call( req
);
759 if (ret
== STATUS_PENDING
)
761 LARGE_INTEGER timeout
;
762 timeout
.QuadPart
= -10000;
763 NtDelayExecution( FALSE
, &timeout
);
767 NtResumeThread( handle
, &dummy
);
769 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
775 if (self
) set_cpu_context( context
);
776 return STATUS_SUCCESS
;
780 /* convert CPU-specific flags to generic server flags */
781 static inline unsigned int get_server_context_flags( DWORD flags
)
783 unsigned int ret
= 0;
785 flags
&= 0x3f; /* mask CPU id flags */
786 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
787 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
788 #ifdef CONTEXT_SEGMENTS
789 if (flags
& CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
791 #ifdef CONTEXT_FLOATING_POINT
792 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
794 #ifdef CONTEXT_DEBUG_REGISTERS
795 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
797 #ifdef CONTEXT_EXTENDED_REGISTERS
798 if (flags
& CONTEXT_EXTENDED_REGISTERS
) ret
|= SERVER_CTX_EXTENDED_REGISTERS
;
803 /***********************************************************************
804 * NtGetContextThread (NTDLL.@)
805 * ZwGetContextThread (NTDLL.@)
807 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
811 DWORD needed_flags
= context
->ContextFlags
;
812 BOOL self
= (handle
== GetCurrentThread());
815 /* on i386 debug registers always require a server call */
816 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)) self
= FALSE
;
821 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
822 context_t server_context
;
824 SERVER_START_REQ( get_thread_context
)
826 req
->handle
= wine_server_obj_handle( handle
);
827 req
->flags
= server_flags
;
829 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
830 ret
= wine_server_call( req
);
835 if (ret
== STATUS_PENDING
)
837 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
839 for (i
= 0; i
< 100; i
++)
841 SERVER_START_REQ( get_thread_context
)
843 req
->handle
= wine_server_obj_handle( handle
);
844 req
->flags
= server_flags
;
846 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
847 ret
= wine_server_call( req
);
850 if (ret
== STATUS_PENDING
)
852 LARGE_INTEGER timeout
;
853 timeout
.QuadPart
= -10000;
854 NtDelayExecution( FALSE
, &timeout
);
858 NtResumeThread( handle
, &dummy
);
860 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
862 if (!ret
) ret
= context_from_server( context
, &server_context
);
864 needed_flags
&= ~context
->ContextFlags
;
872 RtlCaptureContext( &ctx
);
873 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
874 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
877 /* update the cached version of the debug registers */
878 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
880 ntdll_get_thread_data()->dr0
= context
->Dr0
;
881 ntdll_get_thread_data()->dr1
= context
->Dr1
;
882 ntdll_get_thread_data()->dr2
= context
->Dr2
;
883 ntdll_get_thread_data()->dr3
= context
->Dr3
;
884 ntdll_get_thread_data()->dr6
= context
->Dr6
;
885 ntdll_get_thread_data()->dr7
= context
->Dr7
;
889 return STATUS_SUCCESS
;
893 /******************************************************************************
894 * NtQueryInformationThread (NTDLL.@)
895 * ZwQueryInformationThread (NTDLL.@)
897 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
898 void *data
, ULONG length
, ULONG
*ret_len
)
904 case ThreadBasicInformation
:
906 THREAD_BASIC_INFORMATION info
;
907 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
909 SERVER_START_REQ( get_thread_info
)
911 req
->handle
= wine_server_obj_handle( handle
);
913 if (!(status
= wine_server_call( req
)))
915 info
.ExitStatus
= reply
->exit_code
;
916 info
.TebBaseAddress
= wine_server_get_ptr( reply
->teb
);
917 info
.ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
918 info
.ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
919 info
.AffinityMask
= reply
->affinity
& affinity_mask
;
920 info
.Priority
= reply
->priority
;
921 info
.BasePriority
= reply
->priority
; /* FIXME */
925 if (status
== STATUS_SUCCESS
)
927 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
928 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
932 case ThreadAffinityMask
:
934 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
935 ULONG_PTR affinity
= 0;
937 SERVER_START_REQ( get_thread_info
)
939 req
->handle
= wine_server_obj_handle( handle
);
941 if (!(status
= wine_server_call( req
)))
942 affinity
= reply
->affinity
& affinity_mask
;
945 if (status
== STATUS_SUCCESS
)
947 if (data
) memcpy( data
, &affinity
, min( length
, sizeof(affinity
) ));
948 if (ret_len
) *ret_len
= min( length
, sizeof(affinity
) );
954 KERNEL_USER_TIMES kusrt
;
955 /* We need to do a server call to get the creation time or exit time */
956 /* This works on any thread */
957 SERVER_START_REQ( get_thread_info
)
959 req
->handle
= wine_server_obj_handle( handle
);
961 status
= wine_server_call( req
);
962 if (status
== STATUS_SUCCESS
)
964 kusrt
.CreateTime
.QuadPart
= reply
->creation_time
;
965 kusrt
.ExitTime
.QuadPart
= reply
->exit_time
;
969 if (status
== STATUS_SUCCESS
)
971 /* We call times(2) for kernel time or user time */
972 /* We can only (portably) do this for the current thread */
973 if (handle
== GetCurrentThread())
976 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
979 kusrt
.KernelTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
980 kusrt
.UserTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
984 static BOOL reported
= FALSE
;
986 kusrt
.KernelTime
.QuadPart
= 0;
987 kusrt
.UserTime
.QuadPart
= 0;
989 TRACE("Cannot get kerneltime or usertime of other threads\n");
992 FIXME("Cannot get kerneltime or usertime of other threads\n");
996 if (data
) memcpy( data
, &kusrt
, min( length
, sizeof(kusrt
) ));
997 if (ret_len
) *ret_len
= min( length
, sizeof(kusrt
) );
1001 case ThreadDescriptorTableEntry
:
1004 THREAD_DESCRIPTOR_INFORMATION
* tdi
= data
;
1005 if (length
< sizeof(*tdi
))
1006 status
= STATUS_INFO_LENGTH_MISMATCH
;
1007 else if (!(tdi
->Selector
& 4)) /* GDT selector */
1009 unsigned sel
= tdi
->Selector
& ~3; /* ignore RPL */
1010 status
= STATUS_SUCCESS
;
1011 if (!sel
) /* null selector */
1012 memset( &tdi
->Entry
, 0, sizeof(tdi
->Entry
) );
1015 tdi
->Entry
.BaseLow
= 0;
1016 tdi
->Entry
.HighWord
.Bits
.BaseMid
= 0;
1017 tdi
->Entry
.HighWord
.Bits
.BaseHi
= 0;
1018 tdi
->Entry
.LimitLow
= 0xffff;
1019 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0xf;
1020 tdi
->Entry
.HighWord
.Bits
.Dpl
= 3;
1021 tdi
->Entry
.HighWord
.Bits
.Sys
= 0;
1022 tdi
->Entry
.HighWord
.Bits
.Pres
= 1;
1023 tdi
->Entry
.HighWord
.Bits
.Granularity
= 1;
1024 tdi
->Entry
.HighWord
.Bits
.Default_Big
= 1;
1025 tdi
->Entry
.HighWord
.Bits
.Type
= 0x12;
1026 /* it has to be one of the system GDT selectors */
1027 if (sel
!= (wine_get_ds() & ~3) && sel
!= (wine_get_ss() & ~3))
1029 if (sel
== (wine_get_cs() & ~3))
1030 tdi
->Entry
.HighWord
.Bits
.Type
|= 8; /* code segment */
1031 else status
= STATUS_ACCESS_DENIED
;
1037 SERVER_START_REQ( get_selector_entry
)
1039 req
->handle
= wine_server_obj_handle( handle
);
1040 req
->entry
= tdi
->Selector
>> 3;
1041 status
= wine_server_call( req
);
1044 if (!(reply
->flags
& WINE_LDT_FLAGS_ALLOCATED
))
1045 status
= STATUS_ACCESS_VIOLATION
;
1048 wine_ldt_set_base ( &tdi
->Entry
, (void *)reply
->base
);
1049 wine_ldt_set_limit( &tdi
->Entry
, reply
->limit
);
1050 wine_ldt_set_flags( &tdi
->Entry
, reply
->flags
);
1056 if (status
== STATUS_SUCCESS
&& ret_len
)
1057 /* yes, that's a bit strange, but it's the way it is */
1058 *ret_len
= sizeof(LDT_ENTRY
);
1060 status
= STATUS_NOT_IMPLEMENTED
;
1064 case ThreadAmILastThread
:
1066 SERVER_START_REQ(get_thread_info
)
1068 req
->handle
= wine_server_obj_handle( handle
);
1070 status
= wine_server_call( req
);
1071 if (status
== STATUS_SUCCESS
)
1073 BOOLEAN last
= reply
->last
;
1074 if (data
) memcpy( data
, &last
, min( length
, sizeof(last
) ));
1075 if (ret_len
) *ret_len
= min( length
, sizeof(last
) );
1081 case ThreadPriority
:
1082 case ThreadBasePriority
:
1083 case ThreadImpersonationToken
:
1084 case ThreadEnableAlignmentFaultFixup
:
1085 case ThreadEventPair_Reusable
:
1086 case ThreadQuerySetWin32StartAddress
:
1087 case ThreadZeroTlsCell
:
1088 case ThreadPerformanceCount
:
1089 case ThreadIdealProcessor
:
1090 case ThreadPriorityBoost
:
1091 case ThreadSetTlsArrayAddress
:
1092 case ThreadIsIoPending
:
1094 FIXME( "info class %d not supported yet\n", class );
1095 return STATUS_NOT_IMPLEMENTED
;
1100 /******************************************************************************
1101 * NtSetInformationThread (NTDLL.@)
1102 * ZwSetInformationThread (NTDLL.@)
1104 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1105 LPCVOID data
, ULONG length
)
1110 case ThreadZeroTlsCell
:
1111 if (handle
== GetCurrentThread())
1116 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1117 index
= *(const DWORD
*)data
;
1118 if (index
< TLS_MINIMUM_AVAILABLE
)
1120 RtlAcquirePebLock();
1121 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1123 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1124 teb
->TlsSlots
[index
] = 0;
1126 RtlReleasePebLock();
1130 index
-= TLS_MINIMUM_AVAILABLE
;
1131 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
1132 return STATUS_INVALID_PARAMETER
;
1133 RtlAcquirePebLock();
1134 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1136 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1137 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
1139 RtlReleasePebLock();
1141 return STATUS_SUCCESS
;
1143 FIXME( "ZeroTlsCell not supported on other threads\n" );
1144 return STATUS_NOT_IMPLEMENTED
;
1146 case ThreadImpersonationToken
:
1148 const HANDLE
*phToken
= data
;
1149 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
1150 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
1151 SERVER_START_REQ( set_thread_info
)
1153 req
->handle
= wine_server_obj_handle( handle
);
1154 req
->token
= wine_server_obj_handle( *phToken
);
1155 req
->mask
= SET_THREAD_INFO_TOKEN
;
1156 status
= wine_server_call( req
);
1161 case ThreadBasePriority
:
1163 const DWORD
*pprio
= data
;
1164 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1165 SERVER_START_REQ( set_thread_info
)
1167 req
->handle
= wine_server_obj_handle( handle
);
1168 req
->priority
= *pprio
;
1169 req
->mask
= SET_THREAD_INFO_PRIORITY
;
1170 status
= wine_server_call( req
);
1175 case ThreadAffinityMask
:
1177 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1178 const ULONG_PTR
*paff
= data
;
1179 if (length
!= sizeof(ULONG_PTR
)) return STATUS_INVALID_PARAMETER
;
1180 if (*paff
& ~affinity_mask
) return STATUS_INVALID_PARAMETER
;
1181 if (!*paff
) return STATUS_INVALID_PARAMETER
;
1182 SERVER_START_REQ( set_thread_info
)
1184 req
->handle
= wine_server_obj_handle( handle
);
1185 req
->affinity
= *paff
;
1186 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1187 status
= wine_server_call( req
);
1192 case ThreadHideFromDebugger
:
1193 /* pretend the call succeeded to satisfy some code protectors */
1194 return STATUS_SUCCESS
;
1196 case ThreadBasicInformation
:
1198 case ThreadPriority
:
1199 case ThreadDescriptorTableEntry
:
1200 case ThreadEnableAlignmentFaultFixup
:
1201 case ThreadEventPair_Reusable
:
1202 case ThreadQuerySetWin32StartAddress
:
1203 case ThreadPerformanceCount
:
1204 case ThreadAmILastThread
:
1205 case ThreadIdealProcessor
:
1206 case ThreadPriorityBoost
:
1207 case ThreadSetTlsArrayAddress
:
1208 case ThreadIsIoPending
:
1210 FIXME( "info class %d not supported yet\n", class );
1211 return STATUS_NOT_IMPLEMENTED
;