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/pthread.h"
41 #include "wine/debug.h"
42 #include "ntdll_misc.h"
44 #include "wine/exception.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
47 WINE_DECLARE_DEBUG_CHANNEL(relay
);
49 struct _KUSER_SHARED_DATA
*user_shared_data
= NULL
;
51 PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter
= NULL
;
53 /* info passed to a starting thread */
57 PRTL_THREAD_START_ROUTINE entry_point
;
61 static PEB_LDR_DATA ldr
;
62 static RTL_USER_PROCESS_PARAMETERS params
; /* default parameters if no parent */
63 static WCHAR current_dir
[MAX_NT_PATH_LENGTH
];
64 static RTL_BITMAP tls_bitmap
;
65 static RTL_BITMAP tls_expansion_bitmap
;
66 static RTL_BITMAP fls_bitmap
;
67 static LIST_ENTRY tls_links
;
68 static size_t sigstack_total_size
;
69 static ULONG sigstack_zero_bits
;
70 static int nb_threads
= 1;
72 static struct wine_pthread_functions pthread_functions
;
75 static RTL_CRITICAL_SECTION ldt_section
;
76 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
79 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
80 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
82 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
83 static sigset_t ldt_sigset
;
85 /***********************************************************************
86 * locking for LDT routines
88 static void ldt_lock(void)
92 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
93 RtlEnterCriticalSection( &ldt_section
);
94 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
97 static void ldt_unlock(void)
99 if (ldt_section
.RecursionCount
== 1)
101 sigset_t sigset
= ldt_sigset
;
102 RtlLeaveCriticalSection( &ldt_section
);
103 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
105 else RtlLeaveCriticalSection( &ldt_section
);
109 /***********************************************************************
112 static inline NTSTATUS
init_teb( TEB
*teb
)
114 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
116 teb
->Tib
.ExceptionList
= (void *)~0UL;
117 teb
->Tib
.StackBase
= (void *)~0UL;
118 teb
->Tib
.Self
= &teb
->Tib
;
119 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
120 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
122 if (!(thread_data
->fs
= wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS
;
123 thread_data
->request_fd
= -1;
124 thread_data
->reply_fd
= -1;
125 thread_data
->wait_fd
[0] = -1;
126 thread_data
->wait_fd
[1] = -1;
128 return STATUS_SUCCESS
;
132 /***********************************************************************
135 * Make sure the unicode string doesn't point beyond the end pointer
137 static inline void fix_unicode_string( UNICODE_STRING
*str
, const char *end_ptr
)
139 if ((char *)str
->Buffer
>= end_ptr
)
141 str
->Length
= str
->MaximumLength
= 0;
145 if ((char *)str
->Buffer
+ str
->MaximumLength
> end_ptr
)
147 str
->MaximumLength
= (end_ptr
- (char *)str
->Buffer
) & ~(sizeof(WCHAR
) - 1);
149 if (str
->Length
>= str
->MaximumLength
)
151 if (str
->MaximumLength
>= sizeof(WCHAR
))
152 str
->Length
= str
->MaximumLength
- sizeof(WCHAR
);
154 str
->Length
= str
->MaximumLength
= 0;
159 /***********************************************************************
160 * init_user_process_params
162 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
164 static NTSTATUS
init_user_process_params( SIZE_T info_size
, HANDLE
*exe_file
)
169 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
171 status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &info_size
,
172 MEM_COMMIT
, PAGE_READWRITE
);
173 if (status
!= STATUS_SUCCESS
) return status
;
175 params
->AllocationSize
= info_size
;
176 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
178 SERVER_START_REQ( get_startup_info
)
180 wine_server_set_reply( req
, params
, info_size
);
181 if (!(status
= wine_server_call( req
)))
183 info_size
= wine_server_reply_size( reply
);
184 *exe_file
= wine_server_ptr_handle( reply
->exe_file
);
185 params
->hStdInput
= wine_server_ptr_handle( reply
->hstdin
);
186 params
->hStdOutput
= wine_server_ptr_handle( reply
->hstdout
);
187 params
->hStdError
= wine_server_ptr_handle( reply
->hstderr
);
191 if (status
!= STATUS_SUCCESS
) return status
;
193 if (params
->Size
> info_size
) params
->Size
= info_size
;
195 /* make sure the strings are valid */
196 fix_unicode_string( ¶ms
->CurrentDirectory
.DosPath
, (char *)info_size
);
197 fix_unicode_string( ¶ms
->DllPath
, (char *)info_size
);
198 fix_unicode_string( ¶ms
->ImagePathName
, (char *)info_size
);
199 fix_unicode_string( ¶ms
->CommandLine
, (char *)info_size
);
200 fix_unicode_string( ¶ms
->WindowTitle
, (char *)info_size
);
201 fix_unicode_string( ¶ms
->Desktop
, (char *)info_size
);
202 fix_unicode_string( ¶ms
->ShellInfo
, (char *)info_size
);
203 fix_unicode_string( ¶ms
->RuntimeInfo
, (char *)info_size
);
205 /* environment needs to be a separate memory block */
206 env_size
= info_size
- params
->Size
;
207 if (!env_size
) env_size
= 1;
209 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &env_size
,
210 MEM_COMMIT
, PAGE_READWRITE
);
211 if (status
!= STATUS_SUCCESS
) return status
;
212 memcpy( ptr
, (char *)params
+ params
->Size
, info_size
- params
->Size
);
213 params
->Environment
= ptr
;
215 RtlNormalizeProcessParams( params
);
220 /***********************************************************************
223 * Setup the initial thread.
225 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
227 HANDLE
thread_init(void)
232 SIZE_T size
, info_size
;
235 struct ntdll_thread_data
*thread_data
;
236 static struct debug_info debug_info
; /* debug info for initial thread */
240 /* reserve space for shared user data */
242 addr
= (void *)0x7ffe0000;
244 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
, MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
245 user_shared_data
= addr
;
247 /* allocate and initialize the PEB */
251 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 1, &size
,
252 MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
255 peb
->NumberOfProcessors
= 1;
256 peb
->ProcessParameters
= ¶ms
;
257 peb
->TlsBitmap
= &tls_bitmap
;
258 peb
->TlsExpansionBitmap
= &tls_expansion_bitmap
;
259 peb
->FlsBitmap
= &fls_bitmap
;
261 params
.CurrentDirectory
.DosPath
.Buffer
= current_dir
;
262 params
.CurrentDirectory
.DosPath
.MaximumLength
= sizeof(current_dir
);
263 params
.wShowWindow
= 1; /* SW_SHOWNORMAL */
264 RtlInitializeBitMap( &tls_bitmap
, peb
->TlsBitmapBits
, sizeof(peb
->TlsBitmapBits
) * 8 );
265 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
->TlsExpansionBitmapBits
,
266 sizeof(peb
->TlsExpansionBitmapBits
) * 8 );
267 RtlInitializeBitMap( &fls_bitmap
, peb
->FlsBitmapBits
, sizeof(peb
->FlsBitmapBits
) * 8 );
268 InitializeListHead( &peb
->FlsListHead
);
269 InitializeListHead( &ldr
.InLoadOrderModuleList
);
270 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
271 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
272 InitializeListHead( &tls_links
);
274 /* allocate and initialize the initial TEB */
276 sigstack_total_size
= get_signal_stack_total_size();
277 while (1U << sigstack_zero_bits
< sigstack_total_size
) sigstack_zero_bits
++;
278 assert( 1U << sigstack_zero_bits
== sigstack_total_size
); /* must be a power of 2 */
279 assert( sigstack_total_size
>= sizeof(TEB
) + sizeof(struct startup_info
) );
282 size
= sigstack_total_size
;
283 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
284 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
288 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
289 thread_data
->debug_info
= &debug_info
;
290 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
292 wine_pthread_get_functions( &pthread_functions
, sizeof(pthread_functions
) );
293 signal_init_thread( teb
);
294 virtual_init_threading();
296 debug_info
.str_pos
= debug_info
.strings
;
297 debug_info
.out_pos
= debug_info
.output
;
300 /* setup the server connection */
301 server_init_process();
302 info_size
= server_init_thread( NULL
);
304 /* create the process heap */
305 if (!(peb
->ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
307 MESSAGE( "wine: failed to create the process heap\n" );
311 /* allocate user parameters */
314 init_user_process_params( info_size
, &exe_file
);
318 /* This is wine specific: we have no parent (we're started from unix)
319 * so, create a simple console with bare handles to unix stdio
321 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdInput
);
322 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
323 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
326 /* initialize LDT locking */
327 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
329 /* initialize time values in user_shared_data */
330 NtQuerySystemTime( &now
);
331 user_shared_data
->SystemTime
.LowPart
= now
.u
.LowPart
;
332 user_shared_data
->SystemTime
.High1Time
= user_shared_data
->SystemTime
.High2Time
= now
.u
.HighPart
;
333 user_shared_data
->u
.TickCountQuad
= (now
.QuadPart
- server_start_time
) / 10000;
334 user_shared_data
->u
.TickCount
.High2Time
= user_shared_data
->u
.TickCount
.High1Time
;
335 user_shared_data
->TickCountLowDeprecated
= user_shared_data
->u
.TickCount
.LowPart
;
336 user_shared_data
->TickCountMultiplier
= 1 << 24;
343 /***********************************************************************
346 void abort_thread( int status
)
348 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
349 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1) _exit( status
);
351 close( ntdll_get_thread_data()->wait_fd
[0] );
352 close( ntdll_get_thread_data()->wait_fd
[1] );
353 close( ntdll_get_thread_data()->reply_fd
);
354 close( ntdll_get_thread_data()->request_fd
);
355 pthread_exit( UIntToPtr(status
) );
359 /***********************************************************************
362 static void DECLSPEC_NORETURN
exit_thread( int status
)
364 static void *prev_teb
;
368 RemoveEntryList( &NtCurrentTeb()->TlsLinks
);
370 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->FlsSlots
);
371 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->TlsExpansionSlots
);
373 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
374 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1) exit( status
);
376 if ((teb
= interlocked_xchg_ptr( &prev_teb
, NtCurrentTeb() )))
378 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
381 pthread_join( thread_data
->pthread_id
, NULL
);
382 wine_ldt_free_fs( thread_data
->fs
);
384 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
386 NtFreeVirtualMemory( GetCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
389 close( ntdll_get_thread_data()->wait_fd
[0] );
390 close( ntdll_get_thread_data()->wait_fd
[1] );
391 close( ntdll_get_thread_data()->reply_fd
);
392 close( ntdll_get_thread_data()->request_fd
);
393 pthread_exit( UIntToPtr(status
) );
398 /* wrapper for apps that don't declare the thread function correctly */
399 extern DWORD
call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry
, void *arg
);
400 __ASM_GLOBAL_FUNC(call_thread_entry_point
,
405 "movl 8(%ebp),%eax\n\t"
410 static inline DWORD
call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry
, void *arg
)
412 LPTHREAD_START_ROUTINE func
= (LPTHREAD_START_ROUTINE
)entry
;
417 /***********************************************************************
420 * Hack to make things compatible with the thread procedures used by kernel32.CreateThread.
422 static void DECLSPEC_NORETURN
call_thread_func( PRTL_THREAD_START_ROUTINE rtl_func
, void *arg
)
427 MODULE_DllThreadAttach( NULL
);
430 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), rtl_func
, arg
);
432 exit_code
= call_thread_entry_point( rtl_func
, arg
);
434 /* send the exit code to the server */
435 SERVER_START_REQ( terminate_thread
)
437 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
438 req
->exit_code
= exit_code
;
439 wine_server_call( req
);
446 LdrShutdownProcess();
452 exit_thread( exit_code
);
457 /***********************************************************************
460 * Startup routine for a newly created thread.
462 static void start_thread( struct startup_info
*info
)
464 TEB
*teb
= info
->teb
;
465 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
466 PRTL_THREAD_START_ROUTINE func
= info
->entry_point
;
467 void *arg
= info
->entry_arg
;
468 struct debug_info debug_info
;
470 debug_info
.str_pos
= debug_info
.strings
;
471 debug_info
.out_pos
= debug_info
.output
;
472 thread_data
->debug_info
= &debug_info
;
473 thread_data
->pthread_id
= pthread_self();
475 signal_init_thread( teb
);
476 server_init_thread( func
);
477 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
480 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
483 /* NOTE: Windows does not have an exception handler around the call to
484 * the thread attach. We do for ease of debugging */
485 if (unhandled_exception_filter
)
489 call_thread_func( func
, arg
);
491 __EXCEPT(unhandled_exception_filter
)
493 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
498 call_thread_func( func
, arg
);
502 /***********************************************************************
503 * RtlCreateUserThread (NTDLL.@)
505 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
506 BOOLEAN suspended
, PVOID stack_addr
,
507 SIZE_T stack_reserve
, SIZE_T stack_commit
,
508 PRTL_THREAD_START_ROUTINE start
, void *param
,
509 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
512 pthread_t pthread_id
;
514 struct ntdll_thread_data
*thread_data
= NULL
;
515 struct ntdll_thread_regs
*thread_regs
;
516 struct startup_info
*info
= NULL
;
525 if (process
!= NtCurrentProcess())
530 memset( &call
, 0, sizeof(call
) );
532 call
.create_thread
.type
= APC_CREATE_THREAD
;
533 call
.create_thread
.func
= wine_server_client_ptr( start
);
534 call
.create_thread
.arg
= wine_server_client_ptr( param
);
535 call
.create_thread
.reserve
= stack_reserve
;
536 call
.create_thread
.commit
= stack_commit
;
537 call
.create_thread
.suspend
= suspended
;
538 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
539 if (status
!= STATUS_SUCCESS
) return status
;
541 if (result
.create_thread
.status
== STATUS_SUCCESS
)
543 if (id
) id
->UniqueThread
= ULongToHandle(result
.create_thread
.tid
);
544 if (handle_ptr
) *handle_ptr
= wine_server_ptr_handle( result
.create_thread
.handle
);
545 else NtClose( wine_server_ptr_handle( result
.create_thread
.handle
));
547 return result
.create_thread
.status
;
550 if (pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
551 fcntl( request_pipe
[1], F_SETFD
, 1 ); /* set close on exec flag */
552 wine_server_send_fd( request_pipe
[0] );
554 SERVER_START_REQ( new_thread
)
556 req
->access
= THREAD_ALL_ACCESS
;
557 req
->attributes
= 0; /* FIXME */
558 req
->suspend
= suspended
;
559 req
->request_fd
= request_pipe
[0];
560 if (!(status
= wine_server_call( req
)))
562 handle
= wine_server_ptr_handle( reply
->handle
);
565 close( request_pipe
[0] );
571 close( request_pipe
[1] );
575 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
578 size
= sigstack_total_size
;
579 if ((status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
580 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
583 teb
->Peb
= NtCurrentTeb()->Peb
;
584 info
= (struct startup_info
*)(teb
+ 1);
586 info
->entry_point
= start
;
587 info
->entry_arg
= param
;
589 if ((status
= init_teb( teb
))) goto error
;
591 teb
->ClientId
.UniqueProcess
= ULongToHandle(GetCurrentProcessId());
592 teb
->ClientId
.UniqueThread
= ULongToHandle(tid
);
594 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
595 thread_regs
= (struct ntdll_thread_regs
*)teb
->SpareBytes1
;
596 thread_data
->request_fd
= request_pipe
[1];
598 /* inherit debug registers from parent thread */
599 thread_regs
->dr0
= ntdll_get_thread_regs()->dr0
;
600 thread_regs
->dr1
= ntdll_get_thread_regs()->dr1
;
601 thread_regs
->dr2
= ntdll_get_thread_regs()->dr2
;
602 thread_regs
->dr3
= ntdll_get_thread_regs()->dr3
;
603 thread_regs
->dr6
= ntdll_get_thread_regs()->dr6
;
604 thread_regs
->dr7
= ntdll_get_thread_regs()->dr7
;
606 if ((status
= virtual_alloc_thread_stack( teb
, stack_reserve
, stack_commit
))) goto error
;
608 pthread_attr_init( &attr
);
609 pthread_attr_setstack( &attr
, teb
->DeallocationStack
,
610 (char *)teb
->Tib
.StackBase
- (char *)teb
->DeallocationStack
);
611 pthread_attr_setscope( &attr
, PTHREAD_SCOPE_SYSTEM
); /* force creating a kernel thread */
612 interlocked_xchg_add( &nb_threads
, 1 );
613 if (pthread_create( &pthread_id
, &attr
, (void * (*)(void *))start_thread
, info
))
615 interlocked_xchg_add( &nb_threads
, -1 );
616 pthread_attr_destroy( &attr
);
618 NtFreeVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
619 status
= STATUS_NO_MEMORY
;
622 pthread_attr_destroy( &attr
);
623 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
625 if (id
) id
->UniqueThread
= ULongToHandle(tid
);
626 if (handle_ptr
) *handle_ptr
= handle
;
627 else NtClose( handle
);
629 return STATUS_SUCCESS
;
632 if (thread_data
) wine_ldt_free_fs( thread_data
->fs
);
636 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
638 if (handle
) NtClose( handle
);
639 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
640 close( request_pipe
[1] );
645 /***********************************************************************
646 * RtlExitUserThread (NTDLL.@)
648 void WINAPI
RtlExitUserThread( ULONG status
)
652 SERVER_START_REQ( terminate_thread
)
654 /* send the exit code to the server */
655 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
656 req
->exit_code
= status
;
657 wine_server_call( req
);
664 LdrShutdownProcess();
670 exit_thread( status
);
675 /***********************************************************************
676 * NtOpenThread (NTDLL.@)
677 * ZwOpenThread (NTDLL.@)
679 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
680 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
684 SERVER_START_REQ( open_thread
)
686 req
->tid
= HandleToULong(id
->UniqueThread
);
687 req
->access
= access
;
688 req
->attributes
= attr
? attr
->Attributes
: 0;
689 ret
= wine_server_call( req
);
690 *handle
= wine_server_ptr_handle( reply
->handle
);
697 /******************************************************************************
698 * NtSuspendThread (NTDLL.@)
699 * ZwSuspendThread (NTDLL.@)
701 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
705 SERVER_START_REQ( suspend_thread
)
707 req
->handle
= wine_server_obj_handle( handle
);
708 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
715 /******************************************************************************
716 * NtResumeThread (NTDLL.@)
717 * ZwResumeThread (NTDLL.@)
719 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
723 SERVER_START_REQ( resume_thread
)
725 req
->handle
= wine_server_obj_handle( handle
);
726 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
733 /******************************************************************************
734 * NtAlertResumeThread (NTDLL.@)
735 * ZwAlertResumeThread (NTDLL.@)
737 NTSTATUS WINAPI
NtAlertResumeThread( HANDLE handle
, PULONG count
)
739 FIXME( "stub: should alert thread %p\n", handle
);
740 return NtResumeThread( handle
, count
);
744 /******************************************************************************
745 * NtAlertThread (NTDLL.@)
746 * ZwAlertThread (NTDLL.@)
748 NTSTATUS WINAPI
NtAlertThread( HANDLE handle
)
750 FIXME( "stub: %p\n", handle
);
751 return STATUS_NOT_IMPLEMENTED
;
755 /******************************************************************************
756 * NtTerminateThread (NTDLL.@)
757 * ZwTerminateThread (NTDLL.@)
759 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
764 SERVER_START_REQ( terminate_thread
)
766 req
->handle
= wine_server_obj_handle( handle
);
767 req
->exit_code
= exit_code
;
768 ret
= wine_server_call( req
);
769 self
= !ret
&& reply
->self
;
776 if (last
) _exit( exit_code
);
777 else abort_thread( exit_code
);
783 /******************************************************************************
784 * NtQueueApcThread (NTDLL.@)
786 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
787 ULONG_PTR arg2
, ULONG_PTR arg3
)
790 SERVER_START_REQ( queue_apc
)
792 req
->handle
= wine_server_obj_handle( handle
);
795 req
->call
.type
= APC_USER
;
796 req
->call
.user
.func
= wine_server_client_ptr( func
);
797 req
->call
.user
.args
[0] = arg1
;
798 req
->call
.user
.args
[1] = arg2
;
799 req
->call
.user
.args
[2] = arg3
;
801 else req
->call
.type
= APC_NONE
; /* wake up only */
802 ret
= wine_server_call( req
);
809 /***********************************************************************
810 * NtSetContextThread (NTDLL.@)
811 * ZwSetContextThread (NTDLL.@)
813 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
820 /* on i386 debug registers always require a server call */
821 self
= (handle
== GetCurrentThread());
822 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
824 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
825 self
= (regs
->dr0
== context
->Dr0
&& regs
->dr1
== context
->Dr1
&&
826 regs
->dr2
== context
->Dr2
&& regs
->dr3
== context
->Dr3
&&
827 regs
->dr6
== context
->Dr6
&& regs
->dr7
== context
->Dr7
);
833 SERVER_START_REQ( set_thread_context
)
835 req
->handle
= wine_server_obj_handle( handle
);
836 req
->flags
= context
->ContextFlags
;
838 wine_server_add_data( req
, context
, sizeof(*context
) );
839 ret
= wine_server_call( req
);
844 if (ret
== STATUS_PENDING
)
846 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
848 for (i
= 0; i
< 100; i
++)
850 SERVER_START_REQ( set_thread_context
)
852 req
->handle
= wine_server_obj_handle( handle
);
853 req
->flags
= context
->ContextFlags
;
855 wine_server_add_data( req
, context
, sizeof(*context
) );
856 ret
= wine_server_call( req
);
859 if (ret
== STATUS_PENDING
)
861 LARGE_INTEGER timeout
;
862 timeout
.QuadPart
= -10000;
863 NtDelayExecution( FALSE
, &timeout
);
867 NtResumeThread( handle
, &dummy
);
869 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
875 if (self
) set_cpu_context( context
);
876 return STATUS_SUCCESS
;
880 /***********************************************************************
881 * NtGetContextThread (NTDLL.@)
882 * ZwGetContextThread (NTDLL.@)
884 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
889 DWORD needed_flags
= context
->ContextFlags
;
890 BOOL self
= (handle
== GetCurrentThread());
893 /* on i386 debug registers always require a server call */
894 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)) self
= FALSE
;
899 SERVER_START_REQ( get_thread_context
)
901 req
->handle
= wine_server_obj_handle( handle
);
902 req
->flags
= context
->ContextFlags
;
904 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
905 ret
= wine_server_call( req
);
910 if (ret
== STATUS_PENDING
)
912 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
914 for (i
= 0; i
< 100; i
++)
916 SERVER_START_REQ( get_thread_context
)
918 req
->handle
= wine_server_obj_handle( handle
);
919 req
->flags
= context
->ContextFlags
;
921 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
922 ret
= wine_server_call( req
);
925 if (ret
== STATUS_PENDING
)
927 LARGE_INTEGER timeout
;
928 timeout
.QuadPart
= -10000;
929 NtDelayExecution( FALSE
, &timeout
);
933 NtResumeThread( handle
, &dummy
);
935 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
938 copy_context( context
, &ctx
, context
->ContextFlags
& ctx
.ContextFlags
);
939 needed_flags
&= ~ctx
.ContextFlags
;
946 RtlCaptureContext( &ctx
);
947 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
950 /* update the cached version of the debug registers */
951 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
953 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
954 regs
->dr0
= context
->Dr0
;
955 regs
->dr1
= context
->Dr1
;
956 regs
->dr2
= context
->Dr2
;
957 regs
->dr3
= context
->Dr3
;
958 regs
->dr6
= context
->Dr6
;
959 regs
->dr7
= context
->Dr7
;
963 return STATUS_SUCCESS
;
967 /******************************************************************************
968 * NtQueryInformationThread (NTDLL.@)
969 * ZwQueryInformationThread (NTDLL.@)
971 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
972 void *data
, ULONG length
, ULONG
*ret_len
)
978 case ThreadBasicInformation
:
980 THREAD_BASIC_INFORMATION info
;
981 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
983 SERVER_START_REQ( get_thread_info
)
985 req
->handle
= wine_server_obj_handle( handle
);
987 if (!(status
= wine_server_call( req
)))
989 info
.ExitStatus
= reply
->exit_code
;
990 info
.TebBaseAddress
= wine_server_get_ptr( reply
->teb
);
991 info
.ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
992 info
.ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
993 info
.AffinityMask
= reply
->affinity
& affinity_mask
;
994 info
.Priority
= reply
->priority
;
995 info
.BasePriority
= reply
->priority
; /* FIXME */
999 if (status
== STATUS_SUCCESS
)
1001 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
1002 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
1006 case ThreadAffinityMask
:
1008 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1009 ULONG_PTR affinity
= 0;
1011 SERVER_START_REQ( get_thread_info
)
1013 req
->handle
= wine_server_obj_handle( handle
);
1015 if (!(status
= wine_server_call( req
)))
1016 affinity
= reply
->affinity
& affinity_mask
;
1019 if (status
== STATUS_SUCCESS
)
1021 if (data
) memcpy( data
, &affinity
, min( length
, sizeof(affinity
) ));
1022 if (ret_len
) *ret_len
= min( length
, sizeof(affinity
) );
1028 KERNEL_USER_TIMES kusrt
;
1029 /* We need to do a server call to get the creation time or exit time */
1030 /* This works on any thread */
1031 SERVER_START_REQ( get_thread_info
)
1033 req
->handle
= wine_server_obj_handle( handle
);
1035 status
= wine_server_call( req
);
1036 if (status
== STATUS_SUCCESS
)
1038 kusrt
.CreateTime
.QuadPart
= reply
->creation_time
;
1039 kusrt
.ExitTime
.QuadPart
= reply
->exit_time
;
1043 if (status
== STATUS_SUCCESS
)
1045 /* We call times(2) for kernel time or user time */
1046 /* We can only (portably) do this for the current thread */
1047 if (handle
== GetCurrentThread())
1049 struct tms time_buf
;
1050 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
1053 kusrt
.KernelTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
1054 kusrt
.UserTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
1058 static BOOL reported
= FALSE
;
1060 kusrt
.KernelTime
.QuadPart
= 0;
1061 kusrt
.UserTime
.QuadPart
= 0;
1063 TRACE("Cannot get kerneltime or usertime of other threads\n");
1066 FIXME("Cannot get kerneltime or usertime of other threads\n");
1070 if (data
) memcpy( data
, &kusrt
, min( length
, sizeof(kusrt
) ));
1071 if (ret_len
) *ret_len
= min( length
, sizeof(kusrt
) );
1075 case ThreadDescriptorTableEntry
:
1078 THREAD_DESCRIPTOR_INFORMATION
* tdi
= data
;
1079 if (length
< sizeof(*tdi
))
1080 status
= STATUS_INFO_LENGTH_MISMATCH
;
1081 else if (!(tdi
->Selector
& 4)) /* GDT selector */
1083 unsigned sel
= tdi
->Selector
& ~3; /* ignore RPL */
1084 status
= STATUS_SUCCESS
;
1085 if (!sel
) /* null selector */
1086 memset( &tdi
->Entry
, 0, sizeof(tdi
->Entry
) );
1089 tdi
->Entry
.BaseLow
= 0;
1090 tdi
->Entry
.HighWord
.Bits
.BaseMid
= 0;
1091 tdi
->Entry
.HighWord
.Bits
.BaseHi
= 0;
1092 tdi
->Entry
.LimitLow
= 0xffff;
1093 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0xf;
1094 tdi
->Entry
.HighWord
.Bits
.Dpl
= 3;
1095 tdi
->Entry
.HighWord
.Bits
.Sys
= 0;
1096 tdi
->Entry
.HighWord
.Bits
.Pres
= 1;
1097 tdi
->Entry
.HighWord
.Bits
.Granularity
= 1;
1098 tdi
->Entry
.HighWord
.Bits
.Default_Big
= 1;
1099 tdi
->Entry
.HighWord
.Bits
.Type
= 0x12;
1100 /* it has to be one of the system GDT selectors */
1101 if (sel
!= (wine_get_ds() & ~3) && sel
!= (wine_get_ss() & ~3))
1103 if (sel
== (wine_get_cs() & ~3))
1104 tdi
->Entry
.HighWord
.Bits
.Type
|= 8; /* code segment */
1105 else status
= STATUS_ACCESS_DENIED
;
1111 SERVER_START_REQ( get_selector_entry
)
1113 req
->handle
= wine_server_obj_handle( handle
);
1114 req
->entry
= tdi
->Selector
>> 3;
1115 status
= wine_server_call( req
);
1118 if (!(reply
->flags
& WINE_LDT_FLAGS_ALLOCATED
))
1119 status
= STATUS_ACCESS_VIOLATION
;
1122 wine_ldt_set_base ( &tdi
->Entry
, (void *)reply
->base
);
1123 wine_ldt_set_limit( &tdi
->Entry
, reply
->limit
);
1124 wine_ldt_set_flags( &tdi
->Entry
, reply
->flags
);
1130 if (status
== STATUS_SUCCESS
&& ret_len
)
1131 /* yes, that's a bit strange, but it's the way it is */
1132 *ret_len
= sizeof(LDT_ENTRY
);
1134 status
= STATUS_NOT_IMPLEMENTED
;
1138 case ThreadAmILastThread
:
1140 SERVER_START_REQ(get_thread_info
)
1142 req
->handle
= wine_server_obj_handle( handle
);
1144 status
= wine_server_call( req
);
1145 if (status
== STATUS_SUCCESS
)
1147 BOOLEAN last
= reply
->last
;
1148 if (data
) memcpy( data
, &last
, min( length
, sizeof(last
) ));
1149 if (ret_len
) *ret_len
= min( length
, sizeof(last
) );
1155 case ThreadPriority
:
1156 case ThreadBasePriority
:
1157 case ThreadImpersonationToken
:
1158 case ThreadEnableAlignmentFaultFixup
:
1159 case ThreadEventPair_Reusable
:
1160 case ThreadQuerySetWin32StartAddress
:
1161 case ThreadZeroTlsCell
:
1162 case ThreadPerformanceCount
:
1163 case ThreadIdealProcessor
:
1164 case ThreadPriorityBoost
:
1165 case ThreadSetTlsArrayAddress
:
1166 case ThreadIsIoPending
:
1168 FIXME( "info class %d not supported yet\n", class );
1169 return STATUS_NOT_IMPLEMENTED
;
1174 /******************************************************************************
1175 * NtSetInformationThread (NTDLL.@)
1176 * ZwSetInformationThread (NTDLL.@)
1178 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1179 LPCVOID data
, ULONG length
)
1184 case ThreadZeroTlsCell
:
1185 if (handle
== GetCurrentThread())
1190 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1191 index
= *(const DWORD
*)data
;
1192 if (index
< TLS_MINIMUM_AVAILABLE
)
1194 RtlAcquirePebLock();
1195 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1197 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1198 teb
->TlsSlots
[index
] = 0;
1200 RtlReleasePebLock();
1204 index
-= TLS_MINIMUM_AVAILABLE
;
1205 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
1206 return STATUS_INVALID_PARAMETER
;
1207 RtlAcquirePebLock();
1208 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1210 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1211 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
1213 RtlReleasePebLock();
1215 return STATUS_SUCCESS
;
1217 FIXME( "ZeroTlsCell not supported on other threads\n" );
1218 return STATUS_NOT_IMPLEMENTED
;
1220 case ThreadImpersonationToken
:
1222 const HANDLE
*phToken
= data
;
1223 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
1224 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
1225 SERVER_START_REQ( set_thread_info
)
1227 req
->handle
= wine_server_obj_handle( handle
);
1228 req
->token
= wine_server_obj_handle( *phToken
);
1229 req
->mask
= SET_THREAD_INFO_TOKEN
;
1230 status
= wine_server_call( req
);
1235 case ThreadBasePriority
:
1237 const DWORD
*pprio
= data
;
1238 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1239 SERVER_START_REQ( set_thread_info
)
1241 req
->handle
= wine_server_obj_handle( handle
);
1242 req
->priority
= *pprio
;
1243 req
->mask
= SET_THREAD_INFO_PRIORITY
;
1244 status
= wine_server_call( req
);
1249 case ThreadAffinityMask
:
1251 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1252 const ULONG_PTR
*paff
= data
;
1253 if (length
!= sizeof(ULONG_PTR
)) return STATUS_INVALID_PARAMETER
;
1254 if (*paff
& ~affinity_mask
) return STATUS_INVALID_PARAMETER
;
1255 SERVER_START_REQ( set_thread_info
)
1257 req
->handle
= wine_server_obj_handle( handle
);
1258 req
->affinity
= *paff
;
1259 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1260 status
= wine_server_call( req
);
1265 case ThreadBasicInformation
:
1267 case ThreadPriority
:
1268 case ThreadDescriptorTableEntry
:
1269 case ThreadEnableAlignmentFaultFixup
:
1270 case ThreadEventPair_Reusable
:
1271 case ThreadQuerySetWin32StartAddress
:
1272 case ThreadPerformanceCount
:
1273 case ThreadAmILastThread
:
1274 case ThreadIdealProcessor
:
1275 case ThreadPriorityBoost
:
1276 case ThreadSetTlsArrayAddress
:
1277 case ThreadIsIoPending
:
1279 FIXME( "info class %d not supported yet\n", class );
1280 return STATUS_NOT_IMPLEMENTED
;