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 size_t sigstack_total_size
;
68 static ULONG sigstack_zero_bits
;
69 static int nb_threads
= 1;
71 static RTL_CRITICAL_SECTION ldt_section
;
72 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
75 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
76 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
78 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
79 static sigset_t ldt_sigset
;
81 /***********************************************************************
82 * locking for LDT routines
84 static void ldt_lock(void)
88 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
89 RtlEnterCriticalSection( &ldt_section
);
90 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
93 static void ldt_unlock(void)
95 if (ldt_section
.RecursionCount
== 1)
97 sigset_t sigset
= ldt_sigset
;
98 RtlLeaveCriticalSection( &ldt_section
);
99 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
101 else RtlLeaveCriticalSection( &ldt_section
);
105 /***********************************************************************
108 static inline NTSTATUS
init_teb( TEB
*teb
)
110 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
112 teb
->Tib
.ExceptionList
= (void *)~0UL;
113 teb
->Tib
.StackBase
= (void *)~0UL;
114 teb
->Tib
.Self
= &teb
->Tib
;
115 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
116 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
118 if (!(thread_data
->fs
= wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS
;
119 thread_data
->request_fd
= -1;
120 thread_data
->reply_fd
= -1;
121 thread_data
->wait_fd
[0] = -1;
122 thread_data
->wait_fd
[1] = -1;
124 return STATUS_SUCCESS
;
128 /***********************************************************************
131 * Copy a unicode string from the startup info.
133 static inline void get_unicode_string( UNICODE_STRING
*str
, WCHAR
**src
, WCHAR
**dst
, UINT len
)
137 str
->MaximumLength
= len
+ sizeof(WCHAR
);
138 memcpy( str
->Buffer
, *src
, len
);
139 str
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
140 *src
+= len
/ sizeof(WCHAR
);
141 *dst
+= len
/ sizeof(WCHAR
) + 1;
144 /***********************************************************************
145 * init_user_process_params
147 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
149 static NTSTATUS
init_user_process_params( SIZE_T data_size
, HANDLE
*exe_file
)
153 SIZE_T info_size
, env_size
, size
, alloc_size
;
155 startup_info_t
*info
;
156 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
158 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, data_size
)))
159 return STATUS_NO_MEMORY
;
161 SERVER_START_REQ( get_startup_info
)
163 wine_server_set_reply( req
, info
, data_size
);
164 if (!(status
= wine_server_call( req
)))
166 data_size
= wine_server_reply_size( reply
);
167 info_size
= reply
->info_size
;
168 env_size
= data_size
- info_size
;
169 *exe_file
= wine_server_ptr_handle( reply
->exe_file
);
173 if (status
!= STATUS_SUCCESS
) goto done
;
175 size
= sizeof(*params
);
176 size
+= MAX_NT_PATH_LENGTH
* sizeof(WCHAR
);
177 size
+= info
->dllpath_len
+ sizeof(WCHAR
);
178 size
+= info
->imagepath_len
+ sizeof(WCHAR
);
179 size
+= info
->cmdline_len
+ sizeof(WCHAR
);
180 size
+= info
->title_len
+ sizeof(WCHAR
);
181 size
+= info
->desktop_len
+ sizeof(WCHAR
);
182 size
+= info
->shellinfo_len
+ sizeof(WCHAR
);
183 size
+= info
->runtime_len
+ sizeof(WCHAR
);
186 status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &alloc_size
,
187 MEM_COMMIT
, PAGE_READWRITE
);
188 if (status
!= STATUS_SUCCESS
) goto done
;
190 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
191 params
->AllocationSize
= alloc_size
;
193 params
->Flags
= PROCESS_PARAMS_FLAG_NORMALIZED
;
194 params
->DebugFlags
= info
->debug_flags
;
195 params
->ConsoleHandle
= wine_server_ptr_handle( info
->console
);
196 params
->ConsoleFlags
= info
->console_flags
;
197 params
->hStdInput
= wine_server_ptr_handle( info
->hstdin
);
198 params
->hStdOutput
= wine_server_ptr_handle( info
->hstdout
);
199 params
->hStdError
= wine_server_ptr_handle( info
->hstderr
);
200 params
->dwX
= info
->x
;
201 params
->dwY
= info
->y
;
202 params
->dwXSize
= info
->xsize
;
203 params
->dwYSize
= info
->ysize
;
204 params
->dwXCountChars
= info
->xchars
;
205 params
->dwYCountChars
= info
->ychars
;
206 params
->dwFillAttribute
= info
->attribute
;
207 params
->dwFlags
= info
->flags
;
208 params
->wShowWindow
= info
->show
;
210 src
= (WCHAR
*)(info
+ 1);
211 dst
= (WCHAR
*)(params
+ 1);
213 /* current directory needs more space */
214 get_unicode_string( ¶ms
->CurrentDirectory
.DosPath
, &src
, &dst
, info
->curdir_len
);
215 params
->CurrentDirectory
.DosPath
.MaximumLength
= MAX_NT_PATH_LENGTH
* sizeof(WCHAR
);
216 dst
= (WCHAR
*)(params
+ 1) + MAX_NT_PATH_LENGTH
;
218 get_unicode_string( ¶ms
->DllPath
, &src
, &dst
, info
->dllpath_len
);
219 get_unicode_string( ¶ms
->ImagePathName
, &src
, &dst
, info
->imagepath_len
);
220 get_unicode_string( ¶ms
->CommandLine
, &src
, &dst
, info
->cmdline_len
);
221 get_unicode_string( ¶ms
->WindowTitle
, &src
, &dst
, info
->title_len
);
222 get_unicode_string( ¶ms
->Desktop
, &src
, &dst
, info
->desktop_len
);
223 get_unicode_string( ¶ms
->ShellInfo
, &src
, &dst
, info
->shellinfo_len
);
225 /* runtime info isn't a real string */
226 params
->RuntimeInfo
.Buffer
= dst
;
227 params
->RuntimeInfo
.Length
= params
->RuntimeInfo
.MaximumLength
= info
->runtime_len
;
228 memcpy( dst
, src
, info
->runtime_len
);
230 /* environment needs to be a separate memory block */
232 alloc_size
= max( 1, env_size
);
233 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &alloc_size
,
234 MEM_COMMIT
, PAGE_READWRITE
);
235 if (status
!= STATUS_SUCCESS
) goto done
;
236 memcpy( ptr
, (char *)info
+ info_size
, env_size
);
237 params
->Environment
= ptr
;
240 RtlFreeHeap( GetProcessHeap(), 0, info
);
245 /***********************************************************************
248 * Setup the initial thread.
250 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
252 HANDLE
thread_init(void)
257 SIZE_T size
, info_size
;
260 struct ntdll_thread_data
*thread_data
;
261 static struct debug_info debug_info
; /* debug info for initial thread */
265 /* reserve space for shared user data */
267 addr
= (void *)0x7ffe0000;
269 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
, MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
270 user_shared_data
= addr
;
272 /* allocate and initialize the PEB */
276 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 1, &size
,
277 MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
280 peb
->ProcessParameters
= ¶ms
;
281 peb
->TlsBitmap
= &tls_bitmap
;
282 peb
->TlsExpansionBitmap
= &tls_expansion_bitmap
;
283 peb
->FlsBitmap
= &fls_bitmap
;
285 params
.CurrentDirectory
.DosPath
.Buffer
= current_dir
;
286 params
.CurrentDirectory
.DosPath
.MaximumLength
= sizeof(current_dir
);
287 params
.wShowWindow
= 1; /* SW_SHOWNORMAL */
288 RtlInitializeBitMap( &tls_bitmap
, peb
->TlsBitmapBits
, sizeof(peb
->TlsBitmapBits
) * 8 );
289 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
->TlsExpansionBitmapBits
,
290 sizeof(peb
->TlsExpansionBitmapBits
) * 8 );
291 RtlInitializeBitMap( &fls_bitmap
, peb
->FlsBitmapBits
, sizeof(peb
->FlsBitmapBits
) * 8 );
292 InitializeListHead( &peb
->FlsListHead
);
293 InitializeListHead( &ldr
.InLoadOrderModuleList
);
294 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
295 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
296 InitializeListHead( &tls_links
);
298 /* allocate and initialize the initial TEB */
300 sigstack_total_size
= get_signal_stack_total_size();
301 while (1U << sigstack_zero_bits
< sigstack_total_size
) sigstack_zero_bits
++;
302 assert( 1U << sigstack_zero_bits
== sigstack_total_size
); /* must be a power of 2 */
303 assert( sigstack_total_size
>= sizeof(TEB
) + sizeof(struct startup_info
) );
306 size
= sigstack_total_size
;
307 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
308 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
312 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
313 thread_data
->debug_info
= &debug_info
;
314 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
316 signal_init_thread( teb
);
317 virtual_init_threading();
319 debug_info
.str_pos
= debug_info
.strings
;
320 debug_info
.out_pos
= debug_info
.output
;
323 /* setup the server connection */
324 server_init_process();
325 info_size
= server_init_thread( peb
);
327 /* create the process heap */
328 if (!(peb
->ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
330 MESSAGE( "wine: failed to create the process heap\n" );
334 /* allocate user parameters */
337 init_user_process_params( info_size
, &exe_file
);
341 /* This is wine specific: we have no parent (we're started from unix)
342 * so, create a simple console with bare handles to unix stdio
344 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdInput
);
345 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
346 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
349 /* initialize LDT locking */
350 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
352 /* initialize time values in user_shared_data */
353 NtQuerySystemTime( &now
);
354 user_shared_data
->SystemTime
.LowPart
= now
.u
.LowPart
;
355 user_shared_data
->SystemTime
.High1Time
= user_shared_data
->SystemTime
.High2Time
= now
.u
.HighPart
;
356 user_shared_data
->u
.TickCountQuad
= (now
.QuadPart
- server_start_time
) / 10000;
357 user_shared_data
->u
.TickCount
.High2Time
= user_shared_data
->u
.TickCount
.High1Time
;
358 user_shared_data
->TickCountLowDeprecated
= user_shared_data
->u
.TickCount
.LowPart
;
359 user_shared_data
->TickCountMultiplier
= 1 << 24;
367 /***********************************************************************
370 void terminate_thread( int status
)
372 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
373 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1) _exit( status
);
375 close( ntdll_get_thread_data()->wait_fd
[0] );
376 close( ntdll_get_thread_data()->wait_fd
[1] );
377 close( ntdll_get_thread_data()->reply_fd
);
378 close( ntdll_get_thread_data()->request_fd
);
379 pthread_exit( UIntToPtr(status
) );
383 /***********************************************************************
386 void exit_thread( int status
)
388 static void *prev_teb
;
391 if (status
) /* send the exit code to the server (0 is already the default) */
393 SERVER_START_REQ( terminate_thread
)
395 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
396 req
->exit_code
= status
;
397 wine_server_call( req
);
402 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1)
404 LdrShutdownProcess();
410 RemoveEntryList( &NtCurrentTeb()->TlsLinks
);
412 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->FlsSlots
);
413 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->TlsExpansionSlots
);
415 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
417 if ((teb
= interlocked_xchg_ptr( &prev_teb
, NtCurrentTeb() )))
419 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
422 pthread_join( thread_data
->pthread_id
, NULL
);
423 wine_ldt_free_fs( thread_data
->fs
);
425 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
427 NtFreeVirtualMemory( GetCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
430 close( ntdll_get_thread_data()->wait_fd
[0] );
431 close( ntdll_get_thread_data()->wait_fd
[1] );
432 close( ntdll_get_thread_data()->reply_fd
);
433 close( ntdll_get_thread_data()->request_fd
);
434 pthread_exit( UIntToPtr(status
) );
438 /***********************************************************************
441 * Startup routine for a newly created thread.
443 static void start_thread( struct startup_info
*info
)
445 TEB
*teb
= info
->teb
;
446 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
447 PRTL_THREAD_START_ROUTINE func
= info
->entry_point
;
448 void *arg
= info
->entry_arg
;
449 struct debug_info debug_info
;
451 debug_info
.str_pos
= debug_info
.strings
;
452 debug_info
.out_pos
= debug_info
.output
;
453 thread_data
->debug_info
= &debug_info
;
454 thread_data
->pthread_id
= pthread_self();
456 signal_init_thread( teb
);
457 server_init_thread( func
);
458 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
461 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
464 MODULE_DllThreadAttach( NULL
);
467 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), func
, arg
);
469 call_thread_entry_point( (LPTHREAD_START_ROUTINE
)func
, arg
);
473 /***********************************************************************
474 * RtlCreateUserThread (NTDLL.@)
476 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
477 BOOLEAN suspended
, PVOID stack_addr
,
478 SIZE_T stack_reserve
, SIZE_T stack_commit
,
479 PRTL_THREAD_START_ROUTINE start
, void *param
,
480 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
483 pthread_t pthread_id
;
485 struct ntdll_thread_data
*thread_data
= NULL
;
486 struct ntdll_thread_regs
*thread_regs
;
487 struct startup_info
*info
= NULL
;
496 if (process
!= NtCurrentProcess())
501 memset( &call
, 0, sizeof(call
) );
503 call
.create_thread
.type
= APC_CREATE_THREAD
;
504 call
.create_thread
.func
= wine_server_client_ptr( start
);
505 call
.create_thread
.arg
= wine_server_client_ptr( param
);
506 call
.create_thread
.reserve
= stack_reserve
;
507 call
.create_thread
.commit
= stack_commit
;
508 call
.create_thread
.suspend
= suspended
;
509 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
510 if (status
!= STATUS_SUCCESS
) return status
;
512 if (result
.create_thread
.status
== STATUS_SUCCESS
)
514 if (id
) id
->UniqueThread
= ULongToHandle(result
.create_thread
.tid
);
515 if (handle_ptr
) *handle_ptr
= wine_server_ptr_handle( result
.create_thread
.handle
);
516 else NtClose( wine_server_ptr_handle( result
.create_thread
.handle
));
518 return result
.create_thread
.status
;
521 if (server_pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
522 wine_server_send_fd( request_pipe
[0] );
524 SERVER_START_REQ( new_thread
)
526 req
->access
= THREAD_ALL_ACCESS
;
527 req
->attributes
= 0; /* FIXME */
528 req
->suspend
= suspended
;
529 req
->request_fd
= request_pipe
[0];
530 if (!(status
= wine_server_call( req
)))
532 handle
= wine_server_ptr_handle( reply
->handle
);
535 close( request_pipe
[0] );
541 close( request_pipe
[1] );
545 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
548 size
= sigstack_total_size
;
549 if ((status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
550 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
553 teb
->Peb
= NtCurrentTeb()->Peb
;
554 info
= (struct startup_info
*)(teb
+ 1);
556 info
->entry_point
= start
;
557 info
->entry_arg
= param
;
559 if ((status
= init_teb( teb
))) goto error
;
561 teb
->ClientId
.UniqueProcess
= ULongToHandle(GetCurrentProcessId());
562 teb
->ClientId
.UniqueThread
= ULongToHandle(tid
);
564 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
565 thread_regs
= (struct ntdll_thread_regs
*)teb
->SpareBytes1
;
566 thread_data
->request_fd
= request_pipe
[1];
568 /* inherit debug registers from parent thread */
569 thread_regs
->dr0
= ntdll_get_thread_regs()->dr0
;
570 thread_regs
->dr1
= ntdll_get_thread_regs()->dr1
;
571 thread_regs
->dr2
= ntdll_get_thread_regs()->dr2
;
572 thread_regs
->dr3
= ntdll_get_thread_regs()->dr3
;
573 thread_regs
->dr6
= ntdll_get_thread_regs()->dr6
;
574 thread_regs
->dr7
= ntdll_get_thread_regs()->dr7
;
576 if ((status
= virtual_alloc_thread_stack( teb
, stack_reserve
, stack_commit
))) goto error
;
578 pthread_attr_init( &attr
);
579 pthread_attr_setstack( &attr
, teb
->DeallocationStack
,
580 (char *)teb
->Tib
.StackBase
- (char *)teb
->DeallocationStack
);
581 pthread_attr_setscope( &attr
, PTHREAD_SCOPE_SYSTEM
); /* force creating a kernel thread */
582 interlocked_xchg_add( &nb_threads
, 1 );
583 if (pthread_create( &pthread_id
, &attr
, (void * (*)(void *))start_thread
, info
))
585 interlocked_xchg_add( &nb_threads
, -1 );
586 pthread_attr_destroy( &attr
);
588 NtFreeVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
589 status
= STATUS_NO_MEMORY
;
592 pthread_attr_destroy( &attr
);
593 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
595 if (id
) id
->UniqueThread
= ULongToHandle(tid
);
596 if (handle_ptr
) *handle_ptr
= handle
;
597 else NtClose( handle
);
599 return STATUS_SUCCESS
;
602 if (thread_data
) wine_ldt_free_fs( thread_data
->fs
);
606 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
608 if (handle
) NtClose( handle
);
609 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
610 close( request_pipe
[1] );
615 /***********************************************************************
616 * NtOpenThread (NTDLL.@)
617 * ZwOpenThread (NTDLL.@)
619 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
620 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
624 SERVER_START_REQ( open_thread
)
626 req
->tid
= HandleToULong(id
->UniqueThread
);
627 req
->access
= access
;
628 req
->attributes
= attr
? attr
->Attributes
: 0;
629 ret
= wine_server_call( req
);
630 *handle
= wine_server_ptr_handle( reply
->handle
);
637 /******************************************************************************
638 * NtSuspendThread (NTDLL.@)
639 * ZwSuspendThread (NTDLL.@)
641 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
645 SERVER_START_REQ( suspend_thread
)
647 req
->handle
= wine_server_obj_handle( handle
);
648 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
655 /******************************************************************************
656 * NtResumeThread (NTDLL.@)
657 * ZwResumeThread (NTDLL.@)
659 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
663 SERVER_START_REQ( resume_thread
)
665 req
->handle
= wine_server_obj_handle( handle
);
666 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
673 /******************************************************************************
674 * NtAlertResumeThread (NTDLL.@)
675 * ZwAlertResumeThread (NTDLL.@)
677 NTSTATUS WINAPI
NtAlertResumeThread( HANDLE handle
, PULONG count
)
679 FIXME( "stub: should alert thread %p\n", handle
);
680 return NtResumeThread( handle
, count
);
684 /******************************************************************************
685 * NtAlertThread (NTDLL.@)
686 * ZwAlertThread (NTDLL.@)
688 NTSTATUS WINAPI
NtAlertThread( HANDLE handle
)
690 FIXME( "stub: %p\n", handle
);
691 return STATUS_NOT_IMPLEMENTED
;
695 /******************************************************************************
696 * NtTerminateThread (NTDLL.@)
697 * ZwTerminateThread (NTDLL.@)
699 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
704 SERVER_START_REQ( terminate_thread
)
706 req
->handle
= wine_server_obj_handle( handle
);
707 req
->exit_code
= exit_code
;
708 ret
= wine_server_call( req
);
709 self
= !ret
&& reply
->self
;
713 if (self
) abort_thread( exit_code
);
718 /******************************************************************************
719 * NtQueueApcThread (NTDLL.@)
721 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
722 ULONG_PTR arg2
, ULONG_PTR arg3
)
725 SERVER_START_REQ( queue_apc
)
727 req
->handle
= wine_server_obj_handle( handle
);
730 req
->call
.type
= APC_USER
;
731 req
->call
.user
.func
= wine_server_client_ptr( func
);
732 req
->call
.user
.args
[0] = arg1
;
733 req
->call
.user
.args
[1] = arg2
;
734 req
->call
.user
.args
[2] = arg3
;
736 else req
->call
.type
= APC_NONE
; /* wake up only */
737 ret
= wine_server_call( req
);
744 /***********************************************************************
745 * NtSetContextThread (NTDLL.@)
746 * ZwSetContextThread (NTDLL.@)
748 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
755 /* on i386 debug registers always require a server call */
756 self
= (handle
== GetCurrentThread());
757 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
759 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
760 self
= (regs
->dr0
== context
->Dr0
&& regs
->dr1
== context
->Dr1
&&
761 regs
->dr2
== context
->Dr2
&& regs
->dr3
== context
->Dr3
&&
762 regs
->dr6
== context
->Dr6
&& regs
->dr7
== context
->Dr7
);
768 context_t server_context
;
770 context_to_server( &server_context
, context
);
772 SERVER_START_REQ( set_thread_context
)
774 req
->handle
= wine_server_obj_handle( handle
);
776 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
777 ret
= wine_server_call( req
);
782 if (ret
== STATUS_PENDING
)
784 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
786 for (i
= 0; i
< 100; i
++)
788 SERVER_START_REQ( set_thread_context
)
790 req
->handle
= wine_server_obj_handle( handle
);
792 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
793 ret
= wine_server_call( req
);
796 if (ret
== STATUS_PENDING
)
798 LARGE_INTEGER timeout
;
799 timeout
.QuadPart
= -10000;
800 NtDelayExecution( FALSE
, &timeout
);
804 NtResumeThread( handle
, &dummy
);
806 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
812 if (self
) set_cpu_context( context
);
813 return STATUS_SUCCESS
;
817 /* convert CPU-specific flags to generic server flags */
818 static inline unsigned int get_server_context_flags( DWORD flags
)
820 unsigned int ret
= 0;
822 flags
&= 0x3f; /* mask CPU id flags */
823 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
824 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
825 #ifdef CONTEXT_SEGMENTS
826 if (flags
& CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
828 #ifdef CONTEXT_FLOATING_POINT
829 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
831 #ifdef CONTEXT_DEBUG_REGISTERS
832 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
834 #ifdef CONTEXT_EXTENDED_REGISTERS
835 if (flags
& CONTEXT_EXTENDED_REGISTERS
) ret
|= SERVER_CTX_EXTENDED_REGISTERS
;
840 /***********************************************************************
841 * NtGetContextThread (NTDLL.@)
842 * ZwGetContextThread (NTDLL.@)
844 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
848 DWORD needed_flags
= context
->ContextFlags
;
849 BOOL self
= (handle
== GetCurrentThread());
852 /* on i386 debug registers always require a server call */
853 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)) self
= FALSE
;
858 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
859 context_t server_context
;
861 SERVER_START_REQ( get_thread_context
)
863 req
->handle
= wine_server_obj_handle( handle
);
864 req
->flags
= server_flags
;
866 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
867 ret
= wine_server_call( req
);
872 if (ret
== STATUS_PENDING
)
874 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
876 for (i
= 0; i
< 100; i
++)
878 SERVER_START_REQ( get_thread_context
)
880 req
->handle
= wine_server_obj_handle( handle
);
881 req
->flags
= server_flags
;
883 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
884 ret
= wine_server_call( req
);
887 if (ret
== STATUS_PENDING
)
889 LARGE_INTEGER timeout
;
890 timeout
.QuadPart
= -10000;
891 NtDelayExecution( FALSE
, &timeout
);
895 NtResumeThread( handle
, &dummy
);
897 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
899 if (!ret
) ret
= context_from_server( context
, &server_context
);
901 needed_flags
&= ~context
->ContextFlags
;
909 RtlCaptureContext( &ctx
);
910 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
911 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
914 /* update the cached version of the debug registers */
915 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
917 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
918 regs
->dr0
= context
->Dr0
;
919 regs
->dr1
= context
->Dr1
;
920 regs
->dr2
= context
->Dr2
;
921 regs
->dr3
= context
->Dr3
;
922 regs
->dr6
= context
->Dr6
;
923 regs
->dr7
= context
->Dr7
;
927 return STATUS_SUCCESS
;
931 /******************************************************************************
932 * NtQueryInformationThread (NTDLL.@)
933 * ZwQueryInformationThread (NTDLL.@)
935 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
936 void *data
, ULONG length
, ULONG
*ret_len
)
942 case ThreadBasicInformation
:
944 THREAD_BASIC_INFORMATION info
;
945 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
947 SERVER_START_REQ( get_thread_info
)
949 req
->handle
= wine_server_obj_handle( handle
);
951 if (!(status
= wine_server_call( req
)))
953 info
.ExitStatus
= reply
->exit_code
;
954 info
.TebBaseAddress
= wine_server_get_ptr( reply
->teb
);
955 info
.ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
956 info
.ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
957 info
.AffinityMask
= reply
->affinity
& affinity_mask
;
958 info
.Priority
= reply
->priority
;
959 info
.BasePriority
= reply
->priority
; /* FIXME */
963 if (status
== STATUS_SUCCESS
)
965 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
966 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
970 case ThreadAffinityMask
:
972 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
973 ULONG_PTR affinity
= 0;
975 SERVER_START_REQ( get_thread_info
)
977 req
->handle
= wine_server_obj_handle( handle
);
979 if (!(status
= wine_server_call( req
)))
980 affinity
= reply
->affinity
& affinity_mask
;
983 if (status
== STATUS_SUCCESS
)
985 if (data
) memcpy( data
, &affinity
, min( length
, sizeof(affinity
) ));
986 if (ret_len
) *ret_len
= min( length
, sizeof(affinity
) );
992 KERNEL_USER_TIMES kusrt
;
993 /* We need to do a server call to get the creation time or exit time */
994 /* This works on any thread */
995 SERVER_START_REQ( get_thread_info
)
997 req
->handle
= wine_server_obj_handle( handle
);
999 status
= wine_server_call( req
);
1000 if (status
== STATUS_SUCCESS
)
1002 kusrt
.CreateTime
.QuadPart
= reply
->creation_time
;
1003 kusrt
.ExitTime
.QuadPart
= reply
->exit_time
;
1007 if (status
== STATUS_SUCCESS
)
1009 /* We call times(2) for kernel time or user time */
1010 /* We can only (portably) do this for the current thread */
1011 if (handle
== GetCurrentThread())
1013 struct tms time_buf
;
1014 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
1017 kusrt
.KernelTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
1018 kusrt
.UserTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
1022 static BOOL reported
= FALSE
;
1024 kusrt
.KernelTime
.QuadPart
= 0;
1025 kusrt
.UserTime
.QuadPart
= 0;
1027 TRACE("Cannot get kerneltime or usertime of other threads\n");
1030 FIXME("Cannot get kerneltime or usertime of other threads\n");
1034 if (data
) memcpy( data
, &kusrt
, min( length
, sizeof(kusrt
) ));
1035 if (ret_len
) *ret_len
= min( length
, sizeof(kusrt
) );
1039 case ThreadDescriptorTableEntry
:
1042 THREAD_DESCRIPTOR_INFORMATION
* tdi
= data
;
1043 if (length
< sizeof(*tdi
))
1044 status
= STATUS_INFO_LENGTH_MISMATCH
;
1045 else if (!(tdi
->Selector
& 4)) /* GDT selector */
1047 unsigned sel
= tdi
->Selector
& ~3; /* ignore RPL */
1048 status
= STATUS_SUCCESS
;
1049 if (!sel
) /* null selector */
1050 memset( &tdi
->Entry
, 0, sizeof(tdi
->Entry
) );
1053 tdi
->Entry
.BaseLow
= 0;
1054 tdi
->Entry
.HighWord
.Bits
.BaseMid
= 0;
1055 tdi
->Entry
.HighWord
.Bits
.BaseHi
= 0;
1056 tdi
->Entry
.LimitLow
= 0xffff;
1057 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0xf;
1058 tdi
->Entry
.HighWord
.Bits
.Dpl
= 3;
1059 tdi
->Entry
.HighWord
.Bits
.Sys
= 0;
1060 tdi
->Entry
.HighWord
.Bits
.Pres
= 1;
1061 tdi
->Entry
.HighWord
.Bits
.Granularity
= 1;
1062 tdi
->Entry
.HighWord
.Bits
.Default_Big
= 1;
1063 tdi
->Entry
.HighWord
.Bits
.Type
= 0x12;
1064 /* it has to be one of the system GDT selectors */
1065 if (sel
!= (wine_get_ds() & ~3) && sel
!= (wine_get_ss() & ~3))
1067 if (sel
== (wine_get_cs() & ~3))
1068 tdi
->Entry
.HighWord
.Bits
.Type
|= 8; /* code segment */
1069 else status
= STATUS_ACCESS_DENIED
;
1075 SERVER_START_REQ( get_selector_entry
)
1077 req
->handle
= wine_server_obj_handle( handle
);
1078 req
->entry
= tdi
->Selector
>> 3;
1079 status
= wine_server_call( req
);
1082 if (!(reply
->flags
& WINE_LDT_FLAGS_ALLOCATED
))
1083 status
= STATUS_ACCESS_VIOLATION
;
1086 wine_ldt_set_base ( &tdi
->Entry
, (void *)reply
->base
);
1087 wine_ldt_set_limit( &tdi
->Entry
, reply
->limit
);
1088 wine_ldt_set_flags( &tdi
->Entry
, reply
->flags
);
1094 if (status
== STATUS_SUCCESS
&& ret_len
)
1095 /* yes, that's a bit strange, but it's the way it is */
1096 *ret_len
= sizeof(LDT_ENTRY
);
1098 status
= STATUS_NOT_IMPLEMENTED
;
1102 case ThreadAmILastThread
:
1104 SERVER_START_REQ(get_thread_info
)
1106 req
->handle
= wine_server_obj_handle( handle
);
1108 status
= wine_server_call( req
);
1109 if (status
== STATUS_SUCCESS
)
1111 BOOLEAN last
= reply
->last
;
1112 if (data
) memcpy( data
, &last
, min( length
, sizeof(last
) ));
1113 if (ret_len
) *ret_len
= min( length
, sizeof(last
) );
1119 case ThreadPriority
:
1120 case ThreadBasePriority
:
1121 case ThreadImpersonationToken
:
1122 case ThreadEnableAlignmentFaultFixup
:
1123 case ThreadEventPair_Reusable
:
1124 case ThreadQuerySetWin32StartAddress
:
1125 case ThreadZeroTlsCell
:
1126 case ThreadPerformanceCount
:
1127 case ThreadIdealProcessor
:
1128 case ThreadPriorityBoost
:
1129 case ThreadSetTlsArrayAddress
:
1130 case ThreadIsIoPending
:
1132 FIXME( "info class %d not supported yet\n", class );
1133 return STATUS_NOT_IMPLEMENTED
;
1138 /******************************************************************************
1139 * NtSetInformationThread (NTDLL.@)
1140 * ZwSetInformationThread (NTDLL.@)
1142 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1143 LPCVOID data
, ULONG length
)
1148 case ThreadZeroTlsCell
:
1149 if (handle
== GetCurrentThread())
1154 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1155 index
= *(const DWORD
*)data
;
1156 if (index
< TLS_MINIMUM_AVAILABLE
)
1158 RtlAcquirePebLock();
1159 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1161 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1162 teb
->TlsSlots
[index
] = 0;
1164 RtlReleasePebLock();
1168 index
-= TLS_MINIMUM_AVAILABLE
;
1169 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
1170 return STATUS_INVALID_PARAMETER
;
1171 RtlAcquirePebLock();
1172 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1174 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1175 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
1177 RtlReleasePebLock();
1179 return STATUS_SUCCESS
;
1181 FIXME( "ZeroTlsCell not supported on other threads\n" );
1182 return STATUS_NOT_IMPLEMENTED
;
1184 case ThreadImpersonationToken
:
1186 const HANDLE
*phToken
= data
;
1187 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
1188 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
1189 SERVER_START_REQ( set_thread_info
)
1191 req
->handle
= wine_server_obj_handle( handle
);
1192 req
->token
= wine_server_obj_handle( *phToken
);
1193 req
->mask
= SET_THREAD_INFO_TOKEN
;
1194 status
= wine_server_call( req
);
1199 case ThreadBasePriority
:
1201 const DWORD
*pprio
= data
;
1202 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1203 SERVER_START_REQ( set_thread_info
)
1205 req
->handle
= wine_server_obj_handle( handle
);
1206 req
->priority
= *pprio
;
1207 req
->mask
= SET_THREAD_INFO_PRIORITY
;
1208 status
= wine_server_call( req
);
1213 case ThreadAffinityMask
:
1215 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1216 const ULONG_PTR
*paff
= data
;
1217 if (length
!= sizeof(ULONG_PTR
)) return STATUS_INVALID_PARAMETER
;
1218 if (*paff
& ~affinity_mask
) return STATUS_INVALID_PARAMETER
;
1219 if (!*paff
) return STATUS_INVALID_PARAMETER
;
1220 SERVER_START_REQ( set_thread_info
)
1222 req
->handle
= wine_server_obj_handle( handle
);
1223 req
->affinity
= *paff
;
1224 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1225 status
= wine_server_call( req
);
1230 case ThreadBasicInformation
:
1232 case ThreadPriority
:
1233 case ThreadDescriptorTableEntry
:
1234 case ThreadEnableAlignmentFaultFixup
:
1235 case ThreadEventPair_Reusable
:
1236 case ThreadQuerySetWin32StartAddress
:
1237 case ThreadPerformanceCount
:
1238 case ThreadAmILastThread
:
1239 case ThreadIdealProcessor
:
1240 case ThreadPriorityBoost
:
1241 case ThreadSetTlsArrayAddress
:
1242 case ThreadIsIoPending
:
1244 FIXME( "info class %d not supported yet\n", class );
1245 return STATUS_NOT_IMPLEMENTED
;