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>
33 #ifdef HAVE_SYS_SYSCALL_H
34 #include <sys/syscall.h>
37 #define NONAMELESSUNION
39 #define WIN32_NO_STATUS
41 #include "wine/library.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
44 #include "ntdll_misc.h"
46 #include "wine/exception.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
49 WINE_DECLARE_DEBUG_CHANNEL(relay
);
51 struct _KUSER_SHARED_DATA
*user_shared_data
= NULL
;
53 PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter
= NULL
;
55 /* info passed to a starting thread */
59 PRTL_THREAD_START_ROUTINE entry_point
;
64 static PEB_LDR_DATA ldr
;
65 static RTL_USER_PROCESS_PARAMETERS params
; /* default parameters if no parent */
66 static WCHAR current_dir
[MAX_NT_PATH_LENGTH
];
67 static RTL_BITMAP tls_bitmap
;
68 static RTL_BITMAP tls_expansion_bitmap
;
69 static RTL_BITMAP fls_bitmap
;
70 static int nb_threads
= 1;
72 /***********************************************************************
75 * Copy a unicode string from the startup info.
77 static inline void get_unicode_string( UNICODE_STRING
*str
, WCHAR
**src
, WCHAR
**dst
, UINT len
)
81 str
->MaximumLength
= len
+ sizeof(WCHAR
);
82 memcpy( str
->Buffer
, *src
, len
);
83 str
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
84 *src
+= len
/ sizeof(WCHAR
);
85 *dst
+= len
/ sizeof(WCHAR
) + 1;
88 /***********************************************************************
89 * init_user_process_params
91 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
93 static NTSTATUS
init_user_process_params( SIZE_T data_size
, HANDLE
*exe_file
)
97 SIZE_T info_size
, env_size
, size
, alloc_size
;
100 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
102 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, data_size
)))
103 return STATUS_NO_MEMORY
;
105 SERVER_START_REQ( get_startup_info
)
107 wine_server_set_reply( req
, info
, data_size
);
108 if (!(status
= wine_server_call( req
)))
110 data_size
= wine_server_reply_size( reply
);
111 info_size
= reply
->info_size
;
112 env_size
= data_size
- info_size
;
113 *exe_file
= wine_server_ptr_handle( reply
->exe_file
);
117 if (status
!= STATUS_SUCCESS
) goto done
;
119 size
= sizeof(*params
);
120 size
+= MAX_NT_PATH_LENGTH
* sizeof(WCHAR
);
121 size
+= info
->dllpath_len
+ sizeof(WCHAR
);
122 size
+= info
->imagepath_len
+ sizeof(WCHAR
);
123 size
+= info
->cmdline_len
+ sizeof(WCHAR
);
124 size
+= info
->title_len
+ sizeof(WCHAR
);
125 size
+= info
->desktop_len
+ sizeof(WCHAR
);
126 size
+= info
->shellinfo_len
+ sizeof(WCHAR
);
127 size
+= info
->runtime_len
+ sizeof(WCHAR
);
130 status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &alloc_size
,
131 MEM_COMMIT
, PAGE_READWRITE
);
132 if (status
!= STATUS_SUCCESS
) goto done
;
134 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
135 params
->AllocationSize
= alloc_size
;
137 params
->Flags
= PROCESS_PARAMS_FLAG_NORMALIZED
;
138 params
->DebugFlags
= info
->debug_flags
;
139 params
->ConsoleHandle
= wine_server_ptr_handle( info
->console
);
140 params
->ConsoleFlags
= info
->console_flags
;
141 params
->hStdInput
= wine_server_ptr_handle( info
->hstdin
);
142 params
->hStdOutput
= wine_server_ptr_handle( info
->hstdout
);
143 params
->hStdError
= wine_server_ptr_handle( info
->hstderr
);
144 params
->dwX
= info
->x
;
145 params
->dwY
= info
->y
;
146 params
->dwXSize
= info
->xsize
;
147 params
->dwYSize
= info
->ysize
;
148 params
->dwXCountChars
= info
->xchars
;
149 params
->dwYCountChars
= info
->ychars
;
150 params
->dwFillAttribute
= info
->attribute
;
151 params
->dwFlags
= info
->flags
;
152 params
->wShowWindow
= info
->show
;
154 src
= (WCHAR
*)(info
+ 1);
155 dst
= (WCHAR
*)(params
+ 1);
157 /* current directory needs more space */
158 get_unicode_string( ¶ms
->CurrentDirectory
.DosPath
, &src
, &dst
, info
->curdir_len
);
159 params
->CurrentDirectory
.DosPath
.MaximumLength
= MAX_NT_PATH_LENGTH
* sizeof(WCHAR
);
160 dst
= (WCHAR
*)(params
+ 1) + MAX_NT_PATH_LENGTH
;
162 get_unicode_string( ¶ms
->DllPath
, &src
, &dst
, info
->dllpath_len
);
163 get_unicode_string( ¶ms
->ImagePathName
, &src
, &dst
, info
->imagepath_len
);
164 get_unicode_string( ¶ms
->CommandLine
, &src
, &dst
, info
->cmdline_len
);
165 get_unicode_string( ¶ms
->WindowTitle
, &src
, &dst
, info
->title_len
);
166 get_unicode_string( ¶ms
->Desktop
, &src
, &dst
, info
->desktop_len
);
167 get_unicode_string( ¶ms
->ShellInfo
, &src
, &dst
, info
->shellinfo_len
);
169 /* runtime info isn't a real string */
170 params
->RuntimeInfo
.Buffer
= dst
;
171 params
->RuntimeInfo
.Length
= params
->RuntimeInfo
.MaximumLength
= info
->runtime_len
;
172 memcpy( dst
, src
, info
->runtime_len
);
174 /* environment needs to be a separate memory block */
176 alloc_size
= max( 1, env_size
);
177 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &alloc_size
,
178 MEM_COMMIT
, PAGE_READWRITE
);
179 if (status
!= STATUS_SUCCESS
) goto done
;
180 memcpy( ptr
, (char *)info
+ info_size
, env_size
);
181 params
->Environment
= ptr
;
184 RtlFreeHeap( GetProcessHeap(), 0, info
);
189 #include <mach/mach.h>
190 #include <mach/mach_error.h>
192 static ULONG64
get_dyld_image_info_addr(void)
195 #ifdef TASK_DYLD_INFO
196 struct task_dyld_info dyld_info
;
197 mach_msg_type_number_t size
= TASK_DYLD_INFO_COUNT
;
198 if (task_info(mach_task_self(), TASK_DYLD_INFO
, (task_info_t
)&dyld_info
, &size
) == KERN_SUCCESS
)
199 ret
= dyld_info
.all_image_info_addr
;
203 #endif /* __APPLE__ */
205 /***********************************************************************
208 * Setup the initial thread.
210 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
212 HANDLE
thread_init(void)
216 SIZE_T size
, info_size
;
220 struct ntdll_thread_data
*thread_data
;
221 static struct debug_info debug_info
; /* debug info for initial thread */
223 ULONG64 dyld_image_info
;
228 /* reserve space for shared user data */
230 addr
= (void *)0x7ffe0000;
232 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
,
233 MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
236 MESSAGE( "wine: failed to map the shared user data: %08x\n", status
);
239 user_shared_data
= addr
;
241 /* allocate and initialize the PEB */
245 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 1, &size
,
246 MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
249 peb
->ProcessParameters
= ¶ms
;
250 peb
->TlsBitmap
= &tls_bitmap
;
251 peb
->TlsExpansionBitmap
= &tls_expansion_bitmap
;
252 peb
->FlsBitmap
= &fls_bitmap
;
254 peb
->OSMajorVersion
= 5;
255 peb
->OSMinorVersion
= 1;
256 peb
->OSBuildNumber
= 0xA28;
257 peb
->OSPlatformId
= VER_PLATFORM_WIN32_NT
;
258 params
.CurrentDirectory
.DosPath
.Buffer
= current_dir
;
259 params
.CurrentDirectory
.DosPath
.MaximumLength
= sizeof(current_dir
);
260 params
.wShowWindow
= 1; /* SW_SHOWNORMAL */
261 ldr
.Length
= sizeof(ldr
);
262 RtlInitializeBitMap( &tls_bitmap
, peb
->TlsBitmapBits
, sizeof(peb
->TlsBitmapBits
) * 8 );
263 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
->TlsExpansionBitmapBits
,
264 sizeof(peb
->TlsExpansionBitmapBits
) * 8 );
265 RtlInitializeBitMap( &fls_bitmap
, peb
->FlsBitmapBits
, sizeof(peb
->FlsBitmapBits
) * 8 );
266 RtlSetBits( peb
->TlsBitmap
, 0, 1 ); /* TLS index 0 is reserved and should be initialized to NULL. */
267 RtlSetBits( peb
->FlsBitmap
, 0, 1 );
268 InitializeListHead( &peb
->FlsListHead
);
269 InitializeListHead( &ldr
.InLoadOrderModuleList
);
270 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
271 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
273 dyld_image_info
= get_dyld_image_info_addr();
275 #ifdef WORDS_BIGENDIAN
276 peb
->Reserved
[1] = dyld_image_info
& 0xFFFFFFFF;
277 peb
->Reserved
[0] = dyld_image_info
>> 32;
279 peb
->Reserved
[0] = dyld_image_info
& 0xFFFFFFFF;
280 peb
->Reserved
[1] = dyld_image_info
>> 32;
283 peb
->Reserved
[0] = dyld_image_info
& 0xFFFFFFFF;
288 * Starting with Vista, the first user to log on has session id 1.
289 * Session id 0 is for processes that don't interact with the user (like services).
293 /* allocate and initialize the initial TEB */
295 signal_alloc_thread( &teb
);
297 teb
->Tib
.StackBase
= (void *)~0UL;
298 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
299 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
301 thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
302 thread_data
->request_fd
= -1;
303 thread_data
->reply_fd
= -1;
304 thread_data
->wait_fd
[0] = -1;
305 thread_data
->wait_fd
[1] = -1;
306 thread_data
->debug_info
= &debug_info
;
307 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
309 signal_init_thread( teb
);
310 virtual_init_threading();
312 debug_info
.str_pos
= debug_info
.strings
;
313 debug_info
.out_pos
= debug_info
.output
;
316 /* setup the server connection */
317 server_init_process();
318 info_size
= server_init_thread( peb
);
320 /* create the process heap */
321 if (!(peb
->ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
323 MESSAGE( "wine: failed to create the process heap\n" );
327 /* allocate user parameters */
330 init_user_process_params( info_size
, &exe_file
);
334 if (isatty(0) || isatty(1) || isatty(2))
335 params
.ConsoleHandle
= (HANDLE
)2; /* see kernel32/kernel_private.h */
337 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdInput
);
339 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
341 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
344 /* initialize time values in user_shared_data */
345 NtQuerySystemTime( &now
);
346 user_shared_data
->SystemTime
.LowPart
= now
.u
.LowPart
;
347 user_shared_data
->SystemTime
.High1Time
= user_shared_data
->SystemTime
.High2Time
= now
.u
.HighPart
;
348 user_shared_data
->u
.TickCountQuad
= (now
.QuadPart
- server_start_time
) / 10000;
349 user_shared_data
->u
.TickCount
.High2Time
= user_shared_data
->u
.TickCount
.High1Time
;
350 user_shared_data
->TickCountLowDeprecated
= user_shared_data
->u
.TickCount
.LowPart
;
351 user_shared_data
->TickCountMultiplier
= 1 << 24;
355 NtCreateKeyedEvent( &keyed_event
, GENERIC_READ
| GENERIC_WRITE
, NULL
, 0 );
361 /***********************************************************************
364 void terminate_thread( int status
)
366 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
367 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1) _exit( status
);
369 close( ntdll_get_thread_data()->wait_fd
[0] );
370 close( ntdll_get_thread_data()->wait_fd
[1] );
371 close( ntdll_get_thread_data()->reply_fd
);
372 close( ntdll_get_thread_data()->request_fd
);
373 pthread_exit( UIntToPtr(status
) );
377 /***********************************************************************
380 void exit_thread( int status
)
382 static void *prev_teb
;
385 if (status
) /* send the exit code to the server (0 is already the default) */
387 SERVER_START_REQ( terminate_thread
)
389 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
390 req
->exit_code
= status
;
391 wine_server_call( req
);
396 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1)
398 LdrShutdownProcess();
403 RtlFreeThreadActivationContextStack();
405 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
407 if ((teb
= interlocked_xchg_ptr( &prev_teb
, NtCurrentTeb() )))
409 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
411 if (thread_data
->pthread_id
)
413 pthread_join( thread_data
->pthread_id
, NULL
);
414 signal_free_thread( teb
);
418 close( ntdll_get_thread_data()->wait_fd
[0] );
419 close( ntdll_get_thread_data()->wait_fd
[1] );
420 close( ntdll_get_thread_data()->reply_fd
);
421 close( ntdll_get_thread_data()->request_fd
);
422 pthread_exit( UIntToPtr(status
) );
426 /***********************************************************************
429 * Startup routine for a newly created thread.
431 static void start_thread( struct startup_info
*info
)
433 TEB
*teb
= info
->teb
;
434 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
435 PRTL_THREAD_START_ROUTINE func
= info
->entry_point
;
436 void *arg
= info
->entry_arg
;
437 struct debug_info debug_info
;
439 debug_info
.str_pos
= debug_info
.strings
;
440 debug_info
.out_pos
= debug_info
.output
;
441 thread_data
->debug_info
= &debug_info
;
442 thread_data
->pthread_id
= pthread_self();
444 signal_init_thread( teb
);
445 server_init_thread( func
);
446 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
448 MODULE_DllThreadAttach( NULL
);
451 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), func
, arg
);
453 call_thread_entry_point( (LPTHREAD_START_ROUTINE
)func
, arg
);
457 /***********************************************************************
458 * RtlCreateUserThread (NTDLL.@)
460 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
461 BOOLEAN suspended
, PVOID stack_addr
,
462 SIZE_T stack_reserve
, SIZE_T stack_commit
,
463 PRTL_THREAD_START_ROUTINE start
, void *param
,
464 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
467 pthread_t pthread_id
;
469 struct ntdll_thread_data
*thread_data
;
470 struct startup_info
*info
;
471 HANDLE handle
= 0, actctx
= 0;
477 if (process
!= NtCurrentProcess())
482 memset( &call
, 0, sizeof(call
) );
484 call
.create_thread
.type
= APC_CREATE_THREAD
;
485 call
.create_thread
.func
= wine_server_client_ptr( start
);
486 call
.create_thread
.arg
= wine_server_client_ptr( param
);
487 call
.create_thread
.reserve
= stack_reserve
;
488 call
.create_thread
.commit
= stack_commit
;
489 call
.create_thread
.suspend
= suspended
;
490 status
= server_queue_process_apc( process
, &call
, &result
);
491 if (status
!= STATUS_SUCCESS
) return status
;
493 if (result
.create_thread
.status
== STATUS_SUCCESS
)
495 if (id
) id
->UniqueThread
= ULongToHandle(result
.create_thread
.tid
);
496 if (handle_ptr
) *handle_ptr
= wine_server_ptr_handle( result
.create_thread
.handle
);
497 else NtClose( wine_server_ptr_handle( result
.create_thread
.handle
));
499 return result
.create_thread
.status
;
502 if (server_pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
503 wine_server_send_fd( request_pipe
[0] );
505 SERVER_START_REQ( new_thread
)
507 req
->access
= THREAD_ALL_ACCESS
;
508 req
->attributes
= 0; /* FIXME */
509 req
->suspend
= suspended
;
510 req
->request_fd
= request_pipe
[0];
511 if (!(status
= wine_server_call( req
)))
513 handle
= wine_server_ptr_handle( reply
->handle
);
516 close( request_pipe
[0] );
522 close( request_pipe
[1] );
526 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
528 if ((status
= signal_alloc_thread( &teb
))) goto error
;
530 teb
->Peb
= NtCurrentTeb()->Peb
;
531 teb
->ClientId
.UniqueProcess
= ULongToHandle(GetCurrentProcessId());
532 teb
->ClientId
.UniqueThread
= ULongToHandle(tid
);
533 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
534 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
536 /* create default activation context frame for new thread */
537 RtlGetActiveActivationContext(&actctx
);
540 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
;
542 frame
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*frame
));
543 frame
->Previous
= NULL
;
544 frame
->ActivationContext
= actctx
;
546 teb
->ActivationContextStack
.ActiveFrame
= frame
;
549 info
= (struct startup_info
*)(teb
+ 1);
551 info
->entry_point
= start
;
552 info
->entry_arg
= param
;
554 thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
555 thread_data
->request_fd
= request_pipe
[1];
556 thread_data
->reply_fd
= -1;
557 thread_data
->wait_fd
[0] = -1;
558 thread_data
->wait_fd
[1] = -1;
560 if ((status
= virtual_alloc_thread_stack( teb
, stack_reserve
, stack_commit
))) goto error
;
562 pthread_attr_init( &attr
);
563 pthread_attr_setstack( &attr
, teb
->DeallocationStack
,
564 (char *)teb
->Tib
.StackBase
- (char *)teb
->DeallocationStack
);
565 pthread_attr_setscope( &attr
, PTHREAD_SCOPE_SYSTEM
); /* force creating a kernel thread */
566 interlocked_xchg_add( &nb_threads
, 1 );
567 if (pthread_create( &pthread_id
, &attr
, (void * (*)(void *))start_thread
, info
))
569 interlocked_xchg_add( &nb_threads
, -1 );
570 pthread_attr_destroy( &attr
);
571 status
= STATUS_NO_MEMORY
;
574 pthread_attr_destroy( &attr
);
575 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
577 if (id
) id
->UniqueThread
= ULongToHandle(tid
);
578 if (handle_ptr
) *handle_ptr
= handle
;
579 else NtClose( handle
);
581 return STATUS_SUCCESS
;
584 if (teb
) signal_free_thread( teb
);
585 if (handle
) NtClose( handle
);
586 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
587 close( request_pipe
[1] );
592 /******************************************************************************
593 * RtlGetNtGlobalFlags (NTDLL.@)
595 ULONG WINAPI
RtlGetNtGlobalFlags(void)
597 if (!peb
) return 0; /* init not done yet */
598 return peb
->NtGlobalFlag
;
602 /***********************************************************************
603 * NtOpenThread (NTDLL.@)
604 * ZwOpenThread (NTDLL.@)
606 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
607 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
611 SERVER_START_REQ( open_thread
)
613 req
->tid
= HandleToULong(id
->UniqueThread
);
614 req
->access
= access
;
615 req
->attributes
= attr
? attr
->Attributes
: 0;
616 ret
= wine_server_call( req
);
617 *handle
= wine_server_ptr_handle( reply
->handle
);
624 /******************************************************************************
625 * NtSuspendThread (NTDLL.@)
626 * ZwSuspendThread (NTDLL.@)
628 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
632 SERVER_START_REQ( suspend_thread
)
634 req
->handle
= wine_server_obj_handle( handle
);
635 if (!(ret
= wine_server_call( req
)))
637 if (count
) *count
= reply
->count
;
645 /******************************************************************************
646 * NtResumeThread (NTDLL.@)
647 * ZwResumeThread (NTDLL.@)
649 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
653 SERVER_START_REQ( resume_thread
)
655 req
->handle
= wine_server_obj_handle( handle
);
656 if (!(ret
= wine_server_call( req
)))
658 if (count
) *count
= reply
->count
;
666 /******************************************************************************
667 * NtAlertResumeThread (NTDLL.@)
668 * ZwAlertResumeThread (NTDLL.@)
670 NTSTATUS WINAPI
NtAlertResumeThread( HANDLE handle
, PULONG count
)
672 FIXME( "stub: should alert thread %p\n", handle
);
673 return NtResumeThread( handle
, count
);
677 /******************************************************************************
678 * NtAlertThread (NTDLL.@)
679 * ZwAlertThread (NTDLL.@)
681 NTSTATUS WINAPI
NtAlertThread( HANDLE handle
)
683 FIXME( "stub: %p\n", handle
);
684 return STATUS_NOT_IMPLEMENTED
;
688 /******************************************************************************
689 * NtTerminateThread (NTDLL.@)
690 * ZwTerminateThread (NTDLL.@)
692 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
697 SERVER_START_REQ( terminate_thread
)
699 req
->handle
= wine_server_obj_handle( handle
);
700 req
->exit_code
= exit_code
;
701 ret
= wine_server_call( req
);
702 self
= !ret
&& reply
->self
;
706 if (self
) abort_thread( exit_code
);
711 /******************************************************************************
712 * NtQueueApcThread (NTDLL.@)
714 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
715 ULONG_PTR arg2
, ULONG_PTR arg3
)
718 SERVER_START_REQ( queue_apc
)
720 req
->handle
= wine_server_obj_handle( handle
);
723 req
->call
.type
= APC_USER
;
724 req
->call
.user
.func
= wine_server_client_ptr( func
);
725 req
->call
.user
.args
[0] = arg1
;
726 req
->call
.user
.args
[1] = arg2
;
727 req
->call
.user
.args
[2] = arg3
;
729 else req
->call
.type
= APC_NONE
; /* wake up only */
730 ret
= wine_server_call( req
);
737 /***********************************************************************
738 * NtSetContextThread (NTDLL.@)
739 * ZwSetContextThread (NTDLL.@)
741 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
748 /* on i386 debug registers always require a server call */
749 self
= (handle
== GetCurrentThread());
750 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
752 self
= (ntdll_get_thread_data()->dr0
== context
->Dr0
&&
753 ntdll_get_thread_data()->dr1
== context
->Dr1
&&
754 ntdll_get_thread_data()->dr2
== context
->Dr2
&&
755 ntdll_get_thread_data()->dr3
== context
->Dr3
&&
756 ntdll_get_thread_data()->dr6
== context
->Dr6
&&
757 ntdll_get_thread_data()->dr7
== context
->Dr7
);
765 context_t server_context
;
767 context_to_server( &server_context
, context
);
769 SERVER_START_REQ( set_thread_context
)
771 req
->handle
= wine_server_obj_handle( handle
);
773 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
774 ret
= wine_server_call( req
);
779 if (ret
== STATUS_PENDING
)
781 for (i
= 0; i
< 100; i
++)
783 SERVER_START_REQ( set_thread_context
)
785 req
->handle
= wine_server_obj_handle( handle
);
787 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
788 ret
= wine_server_call( req
);
791 if (ret
== STATUS_PENDING
)
793 LARGE_INTEGER timeout
;
794 timeout
.QuadPart
= -10000;
795 NtDelayExecution( FALSE
, &timeout
);
799 NtResumeThread( handle
, &dummy
);
800 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
806 if (self
) set_cpu_context( context
);
807 return STATUS_SUCCESS
;
811 /* convert CPU-specific flags to generic server flags */
812 static inline unsigned int get_server_context_flags( DWORD flags
)
814 unsigned int ret
= 0;
816 flags
&= 0x3f; /* mask CPU id flags */
817 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
818 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
819 #ifdef CONTEXT_SEGMENTS
820 if (flags
& CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
822 #ifdef CONTEXT_FLOATING_POINT
823 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
825 #ifdef CONTEXT_DEBUG_REGISTERS
826 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
828 #ifdef CONTEXT_EXTENDED_REGISTERS
829 if (flags
& CONTEXT_EXTENDED_REGISTERS
) ret
|= SERVER_CTX_EXTENDED_REGISTERS
;
834 /***********************************************************************
835 * NtGetContextThread (NTDLL.@)
836 * ZwGetContextThread (NTDLL.@)
838 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
842 DWORD needed_flags
= context
->ContextFlags
;
843 BOOL self
= (handle
== GetCurrentThread());
845 /* on i386/amd64 debug registers always require a server call */
847 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)) self
= FALSE
;
848 #elif defined(__x86_64__)
849 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_AMD64
)) self
= FALSE
;
854 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
855 context_t server_context
;
857 SERVER_START_REQ( get_thread_context
)
859 req
->handle
= wine_server_obj_handle( handle
);
860 req
->flags
= server_flags
;
862 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
863 ret
= wine_server_call( req
);
868 if (ret
== STATUS_PENDING
)
870 for (i
= 0; i
< 100; i
++)
872 SERVER_START_REQ( get_thread_context
)
874 req
->handle
= wine_server_obj_handle( handle
);
875 req
->flags
= server_flags
;
877 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
878 ret
= wine_server_call( req
);
881 if (ret
== STATUS_PENDING
)
883 LARGE_INTEGER timeout
;
884 timeout
.QuadPart
= -10000;
885 NtDelayExecution( FALSE
, &timeout
);
889 NtResumeThread( handle
, &dummy
);
890 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
892 if (!ret
) ret
= context_from_server( context
, &server_context
);
894 needed_flags
&= ~context
->ContextFlags
;
902 RtlCaptureContext( &ctx
);
903 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
904 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
907 /* update the cached version of the debug registers */
908 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
910 ntdll_get_thread_data()->dr0
= context
->Dr0
;
911 ntdll_get_thread_data()->dr1
= context
->Dr1
;
912 ntdll_get_thread_data()->dr2
= context
->Dr2
;
913 ntdll_get_thread_data()->dr3
= context
->Dr3
;
914 ntdll_get_thread_data()->dr6
= context
->Dr6
;
915 ntdll_get_thread_data()->dr7
= context
->Dr7
;
919 return STATUS_SUCCESS
;
923 /******************************************************************************
924 * NtQueryInformationThread (NTDLL.@)
925 * ZwQueryInformationThread (NTDLL.@)
927 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
928 void *data
, ULONG length
, ULONG
*ret_len
)
934 case ThreadBasicInformation
:
936 THREAD_BASIC_INFORMATION info
;
937 const ULONG_PTR affinity_mask
= get_system_affinity_mask();
939 SERVER_START_REQ( get_thread_info
)
941 req
->handle
= wine_server_obj_handle( handle
);
943 if (!(status
= wine_server_call( req
)))
945 info
.ExitStatus
= reply
->exit_code
;
946 info
.TebBaseAddress
= wine_server_get_ptr( reply
->teb
);
947 info
.ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
948 info
.ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
949 info
.AffinityMask
= reply
->affinity
& affinity_mask
;
950 info
.Priority
= reply
->priority
;
951 info
.BasePriority
= reply
->priority
; /* FIXME */
955 if (status
== STATUS_SUCCESS
)
957 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
958 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
962 case ThreadAffinityMask
:
964 const ULONG_PTR affinity_mask
= get_system_affinity_mask();
965 ULONG_PTR affinity
= 0;
967 SERVER_START_REQ( get_thread_info
)
969 req
->handle
= wine_server_obj_handle( handle
);
971 if (!(status
= wine_server_call( req
)))
972 affinity
= reply
->affinity
& affinity_mask
;
975 if (status
== STATUS_SUCCESS
)
977 if (data
) memcpy( data
, &affinity
, min( length
, sizeof(affinity
) ));
978 if (ret_len
) *ret_len
= min( length
, sizeof(affinity
) );
984 KERNEL_USER_TIMES kusrt
;
986 SERVER_START_REQ( get_thread_times
)
988 req
->handle
= wine_server_obj_handle( handle
);
989 status
= wine_server_call( req
);
990 if (status
== STATUS_SUCCESS
)
992 kusrt
.CreateTime
.QuadPart
= reply
->creation_time
;
993 kusrt
.ExitTime
.QuadPart
= reply
->exit_time
;
997 if (status
== STATUS_SUCCESS
)
999 /* We call times(2) for kernel time or user time */
1000 /* We can only (portably) do this for the current thread */
1001 if (handle
== GetCurrentThread())
1003 struct tms time_buf
;
1004 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
1007 kusrt
.KernelTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
1008 kusrt
.UserTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
1012 static BOOL reported
= FALSE
;
1014 kusrt
.KernelTime
.QuadPart
= 0;
1015 kusrt
.UserTime
.QuadPart
= 0;
1017 TRACE("Cannot get kerneltime or usertime of other threads\n");
1020 FIXME("Cannot get kerneltime or usertime of other threads\n");
1024 if (data
) memcpy( data
, &kusrt
, min( length
, sizeof(kusrt
) ));
1025 if (ret_len
) *ret_len
= min( length
, sizeof(kusrt
) );
1029 case ThreadDescriptorTableEntry
:
1032 THREAD_DESCRIPTOR_INFORMATION
* tdi
= data
;
1033 if (length
< sizeof(*tdi
))
1034 status
= STATUS_INFO_LENGTH_MISMATCH
;
1035 else if (!(tdi
->Selector
& 4)) /* GDT selector */
1037 unsigned sel
= LOWORD(tdi
->Selector
) & ~3; /* ignore RPL */
1038 status
= STATUS_SUCCESS
;
1039 if (!sel
) /* null selector */
1040 memset( &tdi
->Entry
, 0, sizeof(tdi
->Entry
) );
1043 tdi
->Entry
.BaseLow
= 0;
1044 tdi
->Entry
.HighWord
.Bits
.BaseMid
= 0;
1045 tdi
->Entry
.HighWord
.Bits
.BaseHi
= 0;
1046 tdi
->Entry
.LimitLow
= 0xffff;
1047 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0xf;
1048 tdi
->Entry
.HighWord
.Bits
.Dpl
= 3;
1049 tdi
->Entry
.HighWord
.Bits
.Sys
= 0;
1050 tdi
->Entry
.HighWord
.Bits
.Pres
= 1;
1051 tdi
->Entry
.HighWord
.Bits
.Granularity
= 1;
1052 tdi
->Entry
.HighWord
.Bits
.Default_Big
= 1;
1053 tdi
->Entry
.HighWord
.Bits
.Type
= 0x12;
1054 tdi
->Entry
.HighWord
.Bits
.Reserved_0
= 0;
1055 /* it has to be one of the system GDT selectors */
1056 if (sel
!= (wine_get_ds() & ~3) && sel
!= (wine_get_ss() & ~3))
1058 if (sel
== (wine_get_cs() & ~3))
1059 tdi
->Entry
.HighWord
.Bits
.Type
|= 8; /* code segment */
1060 else if (sel
== (ntdll_get_thread_data()->fs
& ~3))
1062 ULONG_PTR fs_base
= (ULONG_PTR
)NtCurrentTeb();
1063 tdi
->Entry
.BaseLow
= fs_base
& 0xffff;
1064 tdi
->Entry
.HighWord
.Bits
.BaseMid
= (fs_base
>> 16) & 0xff;
1065 tdi
->Entry
.HighWord
.Bits
.BaseHi
= (fs_base
>> 24) & 0xff;
1066 tdi
->Entry
.LimitLow
= 0x0fff;
1067 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0;
1068 tdi
->Entry
.HighWord
.Bits
.Granularity
= 0;
1070 else status
= STATUS_ACCESS_DENIED
;
1076 SERVER_START_REQ( get_selector_entry
)
1078 req
->handle
= wine_server_obj_handle( handle
);
1079 req
->entry
= LOWORD(tdi
->Selector
) >> 3;
1080 status
= wine_server_call( req
);
1083 if (!(reply
->flags
& WINE_LDT_FLAGS_ALLOCATED
))
1084 status
= STATUS_ACCESS_VIOLATION
;
1087 wine_ldt_set_base ( &tdi
->Entry
, (void *)reply
->base
);
1088 wine_ldt_set_limit( &tdi
->Entry
, reply
->limit
);
1089 wine_ldt_set_flags( &tdi
->Entry
, reply
->flags
);
1095 if (status
== STATUS_SUCCESS
&& ret_len
)
1096 /* yes, that's a bit strange, but it's the way it is */
1097 *ret_len
= sizeof(LDT_ENTRY
);
1099 status
= STATUS_NOT_IMPLEMENTED
;
1103 case ThreadAmILastThread
:
1105 SERVER_START_REQ(get_thread_info
)
1107 req
->handle
= wine_server_obj_handle( handle
);
1109 status
= wine_server_call( req
);
1110 if (status
== STATUS_SUCCESS
)
1112 BOOLEAN last
= reply
->last
;
1113 if (data
) memcpy( data
, &last
, min( length
, sizeof(last
) ));
1114 if (ret_len
) *ret_len
= min( length
, sizeof(last
) );
1120 case ThreadQuerySetWin32StartAddress
:
1122 SERVER_START_REQ( get_thread_info
)
1124 req
->handle
= wine_server_obj_handle( handle
);
1126 status
= wine_server_call( req
);
1127 if (status
== STATUS_SUCCESS
)
1129 PRTL_THREAD_START_ROUTINE entry
= wine_server_get_ptr( reply
->entry_point
);
1130 if (data
) memcpy( data
, &entry
, min( length
, sizeof(entry
) ) );
1131 if (ret_len
) *ret_len
= min( length
, sizeof(entry
) );
1137 case ThreadGroupInformation
:
1139 const ULONG_PTR affinity_mask
= get_system_affinity_mask();
1140 GROUP_AFFINITY affinity
;
1142 memset(&affinity
, 0, sizeof(affinity
));
1143 affinity
.Group
= 0; /* Wine only supports max 64 processors */
1145 SERVER_START_REQ( get_thread_info
)
1147 req
->handle
= wine_server_obj_handle( handle
);
1149 if (!(status
= wine_server_call( req
)))
1150 affinity
.Mask
= reply
->affinity
& affinity_mask
;
1153 if (status
== STATUS_SUCCESS
)
1155 if (data
) memcpy( data
, &affinity
, min( length
, sizeof(affinity
) ));
1156 if (ret_len
) *ret_len
= min( length
, sizeof(affinity
) );
1160 case ThreadPriority
:
1161 case ThreadBasePriority
:
1162 case ThreadImpersonationToken
:
1163 case ThreadEnableAlignmentFaultFixup
:
1164 case ThreadEventPair_Reusable
:
1165 case ThreadZeroTlsCell
:
1166 case ThreadPerformanceCount
:
1167 case ThreadIdealProcessor
:
1168 case ThreadPriorityBoost
:
1169 case ThreadSetTlsArrayAddress
:
1170 case ThreadIsIoPending
:
1172 FIXME( "info class %d not supported yet\n", class );
1173 return STATUS_NOT_IMPLEMENTED
;
1178 /******************************************************************************
1179 * NtSetInformationThread (NTDLL.@)
1180 * ZwSetInformationThread (NTDLL.@)
1182 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1183 LPCVOID data
, ULONG length
)
1188 case ThreadZeroTlsCell
:
1189 if (handle
== GetCurrentThread())
1194 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1195 index
= *(const DWORD
*)data
;
1196 if (index
< TLS_MINIMUM_AVAILABLE
)
1198 RtlAcquirePebLock();
1199 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1201 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1202 teb
->TlsSlots
[index
] = 0;
1204 RtlReleasePebLock();
1208 index
-= TLS_MINIMUM_AVAILABLE
;
1209 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
1210 return STATUS_INVALID_PARAMETER
;
1211 RtlAcquirePebLock();
1212 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1214 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1215 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
1217 RtlReleasePebLock();
1219 return STATUS_SUCCESS
;
1221 FIXME( "ZeroTlsCell not supported on other threads\n" );
1222 return STATUS_NOT_IMPLEMENTED
;
1224 case ThreadImpersonationToken
:
1226 const HANDLE
*phToken
= data
;
1227 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
1228 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
1229 SERVER_START_REQ( set_thread_info
)
1231 req
->handle
= wine_server_obj_handle( handle
);
1232 req
->token
= wine_server_obj_handle( *phToken
);
1233 req
->mask
= SET_THREAD_INFO_TOKEN
;
1234 status
= wine_server_call( req
);
1239 case ThreadBasePriority
:
1241 const DWORD
*pprio
= data
;
1242 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1243 SERVER_START_REQ( set_thread_info
)
1245 req
->handle
= wine_server_obj_handle( handle
);
1246 req
->priority
= *pprio
;
1247 req
->mask
= SET_THREAD_INFO_PRIORITY
;
1248 status
= wine_server_call( req
);
1253 case ThreadAffinityMask
:
1255 const ULONG_PTR affinity_mask
= get_system_affinity_mask();
1258 if (length
!= sizeof(ULONG_PTR
)) return STATUS_INVALID_PARAMETER
;
1259 req_aff
= *(const ULONG_PTR
*)data
;
1260 if ((ULONG
)req_aff
== ~0u) req_aff
= affinity_mask
;
1261 else if (req_aff
& ~affinity_mask
) return STATUS_INVALID_PARAMETER
;
1262 else if (!req_aff
) return STATUS_INVALID_PARAMETER
;
1263 SERVER_START_REQ( set_thread_info
)
1265 req
->handle
= wine_server_obj_handle( handle
);
1266 req
->affinity
= req_aff
;
1267 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1268 status
= wine_server_call( req
);
1273 case ThreadHideFromDebugger
:
1274 /* pretend the call succeeded to satisfy some code protectors */
1275 return STATUS_SUCCESS
;
1276 case ThreadQuerySetWin32StartAddress
:
1278 const PRTL_THREAD_START_ROUTINE
*entry
= data
;
1279 if (length
!= sizeof(PRTL_THREAD_START_ROUTINE
)) return STATUS_INVALID_PARAMETER
;
1280 SERVER_START_REQ( set_thread_info
)
1282 req
->handle
= wine_server_obj_handle( handle
);
1283 req
->mask
= SET_THREAD_INFO_ENTRYPOINT
;
1284 req
->entry_point
= wine_server_client_ptr( *entry
);
1285 status
= wine_server_call( req
);
1290 case ThreadGroupInformation
:
1292 const ULONG_PTR affinity_mask
= get_system_affinity_mask();
1293 const GROUP_AFFINITY
*req_aff
;
1295 if (length
!= sizeof(*req_aff
)) return STATUS_INVALID_PARAMETER
;
1296 if (!data
) return STATUS_ACCESS_VIOLATION
;
1299 /* On Windows the request fails if the reserved fields are set */
1300 if (req_aff
->Reserved
[0] || req_aff
->Reserved
[1] || req_aff
->Reserved
[2])
1301 return STATUS_INVALID_PARAMETER
;
1303 /* Wine only supports max 64 processors */
1304 if (req_aff
->Group
) return STATUS_INVALID_PARAMETER
;
1305 if (req_aff
->Mask
& ~affinity_mask
) return STATUS_INVALID_PARAMETER
;
1306 if (!req_aff
->Mask
) return STATUS_INVALID_PARAMETER
;
1307 SERVER_START_REQ( set_thread_info
)
1309 req
->handle
= wine_server_obj_handle( handle
);
1310 req
->affinity
= req_aff
->Mask
;
1311 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1312 status
= wine_server_call( req
);
1317 case ThreadBasicInformation
:
1319 case ThreadPriority
:
1320 case ThreadDescriptorTableEntry
:
1321 case ThreadEnableAlignmentFaultFixup
:
1322 case ThreadEventPair_Reusable
:
1323 case ThreadPerformanceCount
:
1324 case ThreadAmILastThread
:
1325 case ThreadIdealProcessor
:
1326 case ThreadPriorityBoost
:
1327 case ThreadSetTlsArrayAddress
:
1328 case ThreadIsIoPending
:
1330 FIXME( "info class %d not supported yet\n", class );
1331 return STATUS_NOT_IMPLEMENTED
;
1335 /******************************************************************************
1336 * NtGetCurrentProcessorNumber (NTDLL.@)
1338 * Return the processor, on which the thread is running
1341 ULONG WINAPI
NtGetCurrentProcessorNumber(void)
1345 #if defined(__linux__) && defined(__NR_getcpu)
1346 int res
= syscall(__NR_getcpu
, &processor
, NULL
, NULL
);
1347 if (res
!= -1) return processor
;
1350 if (NtCurrentTeb()->Peb
->NumberOfProcessors
> 1)
1352 ULONG_PTR thread_mask
, processor_mask
;
1355 status
= NtQueryInformationThread(GetCurrentThread(), ThreadAffinityMask
,
1356 &thread_mask
, sizeof(thread_mask
), NULL
);
1357 if (status
== STATUS_SUCCESS
)
1359 for (processor
= 0; processor
< NtCurrentTeb()->Peb
->NumberOfProcessors
; processor
++)
1361 processor_mask
= (1 << processor
);
1362 if (thread_mask
& processor_mask
)
1364 if (thread_mask
!= processor_mask
)
1365 FIXME("need multicore support (%d processors)\n",
1366 NtCurrentTeb()->Peb
->NumberOfProcessors
);
1373 /* fallback to the first processor */