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 * Make sure the unicode string doesn't point beyond the end pointer
133 static inline void fix_unicode_string( UNICODE_STRING
*str
, const char *end_ptr
)
135 if ((char *)str
->Buffer
>= end_ptr
)
137 str
->Length
= str
->MaximumLength
= 0;
141 if ((char *)str
->Buffer
+ str
->MaximumLength
> end_ptr
)
143 str
->MaximumLength
= (end_ptr
- (char *)str
->Buffer
) & ~(sizeof(WCHAR
) - 1);
145 if (str
->Length
>= str
->MaximumLength
)
147 if (str
->MaximumLength
>= sizeof(WCHAR
))
148 str
->Length
= str
->MaximumLength
- sizeof(WCHAR
);
150 str
->Length
= str
->MaximumLength
= 0;
155 /***********************************************************************
156 * init_user_process_params
158 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
160 static NTSTATUS
init_user_process_params( SIZE_T info_size
, HANDLE
*exe_file
)
165 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
167 status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &info_size
,
168 MEM_COMMIT
, PAGE_READWRITE
);
169 if (status
!= STATUS_SUCCESS
) return status
;
171 params
->AllocationSize
= info_size
;
172 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
174 SERVER_START_REQ( get_startup_info
)
176 wine_server_set_reply( req
, params
, info_size
);
177 if (!(status
= wine_server_call( req
)))
179 info_size
= wine_server_reply_size( reply
);
180 *exe_file
= wine_server_ptr_handle( reply
->exe_file
);
181 params
->hStdInput
= wine_server_ptr_handle( reply
->hstdin
);
182 params
->hStdOutput
= wine_server_ptr_handle( reply
->hstdout
);
183 params
->hStdError
= wine_server_ptr_handle( reply
->hstderr
);
187 if (status
!= STATUS_SUCCESS
) return status
;
189 if (params
->Size
> info_size
) params
->Size
= info_size
;
191 /* make sure the strings are valid */
192 fix_unicode_string( ¶ms
->CurrentDirectory
.DosPath
, (char *)info_size
);
193 fix_unicode_string( ¶ms
->DllPath
, (char *)info_size
);
194 fix_unicode_string( ¶ms
->ImagePathName
, (char *)info_size
);
195 fix_unicode_string( ¶ms
->CommandLine
, (char *)info_size
);
196 fix_unicode_string( ¶ms
->WindowTitle
, (char *)info_size
);
197 fix_unicode_string( ¶ms
->Desktop
, (char *)info_size
);
198 fix_unicode_string( ¶ms
->ShellInfo
, (char *)info_size
);
199 fix_unicode_string( ¶ms
->RuntimeInfo
, (char *)info_size
);
201 /* environment needs to be a separate memory block */
202 env_size
= info_size
- params
->Size
;
203 if (!env_size
) env_size
= 1;
205 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &env_size
,
206 MEM_COMMIT
, PAGE_READWRITE
);
207 if (status
!= STATUS_SUCCESS
) return status
;
208 memcpy( ptr
, (char *)params
+ params
->Size
, info_size
- params
->Size
);
209 params
->Environment
= ptr
;
211 RtlNormalizeProcessParams( params
);
216 /***********************************************************************
219 * Setup the initial thread.
221 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
223 HANDLE
thread_init(void)
228 SIZE_T size
, info_size
;
231 struct ntdll_thread_data
*thread_data
;
232 static struct debug_info debug_info
; /* debug info for initial thread */
236 /* reserve space for shared user data */
238 addr
= (void *)0x7ffe0000;
240 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
, MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
241 user_shared_data
= addr
;
243 /* allocate and initialize the PEB */
247 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 1, &size
,
248 MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
251 peb
->NumberOfProcessors
= 1;
252 peb
->ProcessParameters
= ¶ms
;
253 peb
->TlsBitmap
= &tls_bitmap
;
254 peb
->TlsExpansionBitmap
= &tls_expansion_bitmap
;
255 peb
->FlsBitmap
= &fls_bitmap
;
257 params
.CurrentDirectory
.DosPath
.Buffer
= current_dir
;
258 params
.CurrentDirectory
.DosPath
.MaximumLength
= sizeof(current_dir
);
259 params
.wShowWindow
= 1; /* SW_SHOWNORMAL */
260 RtlInitializeBitMap( &tls_bitmap
, peb
->TlsBitmapBits
, sizeof(peb
->TlsBitmapBits
) * 8 );
261 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
->TlsExpansionBitmapBits
,
262 sizeof(peb
->TlsExpansionBitmapBits
) * 8 );
263 RtlInitializeBitMap( &fls_bitmap
, peb
->FlsBitmapBits
, sizeof(peb
->FlsBitmapBits
) * 8 );
264 InitializeListHead( &peb
->FlsListHead
);
265 InitializeListHead( &ldr
.InLoadOrderModuleList
);
266 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
267 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
268 InitializeListHead( &tls_links
);
270 /* allocate and initialize the initial TEB */
272 sigstack_total_size
= get_signal_stack_total_size();
273 while (1U << sigstack_zero_bits
< sigstack_total_size
) sigstack_zero_bits
++;
274 assert( 1U << sigstack_zero_bits
== sigstack_total_size
); /* must be a power of 2 */
275 assert( sigstack_total_size
>= sizeof(TEB
) + sizeof(struct startup_info
) );
278 size
= sigstack_total_size
;
279 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
280 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
284 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
285 thread_data
->debug_info
= &debug_info
;
286 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
288 signal_init_thread( teb
);
289 virtual_init_threading();
291 debug_info
.str_pos
= debug_info
.strings
;
292 debug_info
.out_pos
= debug_info
.output
;
295 /* setup the server connection */
296 server_init_process();
297 info_size
= server_init_thread( peb
);
299 /* create the process heap */
300 if (!(peb
->ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
302 MESSAGE( "wine: failed to create the process heap\n" );
306 /* allocate user parameters */
309 init_user_process_params( info_size
, &exe_file
);
313 /* This is wine specific: we have no parent (we're started from unix)
314 * so, create a simple console with bare handles to unix stdio
316 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdInput
);
317 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
318 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
321 /* initialize LDT locking */
322 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
324 /* initialize time values in user_shared_data */
325 NtQuerySystemTime( &now
);
326 user_shared_data
->SystemTime
.LowPart
= now
.u
.LowPart
;
327 user_shared_data
->SystemTime
.High1Time
= user_shared_data
->SystemTime
.High2Time
= now
.u
.HighPart
;
328 user_shared_data
->u
.TickCountQuad
= (now
.QuadPart
- server_start_time
) / 10000;
329 user_shared_data
->u
.TickCount
.High2Time
= user_shared_data
->u
.TickCount
.High1Time
;
330 user_shared_data
->TickCountLowDeprecated
= user_shared_data
->u
.TickCount
.LowPart
;
331 user_shared_data
->TickCountMultiplier
= 1 << 24;
337 /***********************************************************************
340 void abort_thread( int status
)
342 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
343 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1) _exit( status
);
345 close( ntdll_get_thread_data()->wait_fd
[0] );
346 close( ntdll_get_thread_data()->wait_fd
[1] );
347 close( ntdll_get_thread_data()->reply_fd
);
348 close( ntdll_get_thread_data()->request_fd
);
349 pthread_exit( UIntToPtr(status
) );
353 /***********************************************************************
356 void exit_thread( int status
)
358 static void *prev_teb
;
361 if (status
) /* send the exit code to the server (0 is already the default) */
363 SERVER_START_REQ( terminate_thread
)
365 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
366 req
->exit_code
= status
;
367 wine_server_call( req
);
372 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1)
374 LdrShutdownProcess();
380 RemoveEntryList( &NtCurrentTeb()->TlsLinks
);
382 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->FlsSlots
);
383 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->TlsExpansionSlots
);
385 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
387 if ((teb
= interlocked_xchg_ptr( &prev_teb
, NtCurrentTeb() )))
389 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
392 pthread_join( thread_data
->pthread_id
, NULL
);
393 wine_ldt_free_fs( thread_data
->fs
);
395 NtFreeVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
397 NtFreeVirtualMemory( GetCurrentProcess(), (void **)&teb
, &size
, MEM_RELEASE
);
400 close( ntdll_get_thread_data()->wait_fd
[0] );
401 close( ntdll_get_thread_data()->wait_fd
[1] );
402 close( ntdll_get_thread_data()->reply_fd
);
403 close( ntdll_get_thread_data()->request_fd
);
404 pthread_exit( UIntToPtr(status
) );
408 /***********************************************************************
411 * Startup routine for a newly created thread.
413 static void start_thread( struct startup_info
*info
)
415 TEB
*teb
= info
->teb
;
416 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
417 PRTL_THREAD_START_ROUTINE func
= info
->entry_point
;
418 void *arg
= info
->entry_arg
;
419 struct debug_info debug_info
;
421 debug_info
.str_pos
= debug_info
.strings
;
422 debug_info
.out_pos
= debug_info
.output
;
423 thread_data
->debug_info
= &debug_info
;
424 thread_data
->pthread_id
= pthread_self();
426 signal_init_thread( teb
);
427 server_init_thread( func
);
428 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
431 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
434 MODULE_DllThreadAttach( NULL
);
437 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), func
, arg
);
439 call_thread_entry_point( (LPTHREAD_START_ROUTINE
)func
, arg
);
443 /***********************************************************************
444 * RtlCreateUserThread (NTDLL.@)
446 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
447 BOOLEAN suspended
, PVOID stack_addr
,
448 SIZE_T stack_reserve
, SIZE_T stack_commit
,
449 PRTL_THREAD_START_ROUTINE start
, void *param
,
450 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
453 pthread_t pthread_id
;
455 struct ntdll_thread_data
*thread_data
= NULL
;
456 struct ntdll_thread_regs
*thread_regs
;
457 struct startup_info
*info
= NULL
;
466 if (process
!= NtCurrentProcess())
471 memset( &call
, 0, sizeof(call
) );
473 call
.create_thread
.type
= APC_CREATE_THREAD
;
474 call
.create_thread
.func
= wine_server_client_ptr( start
);
475 call
.create_thread
.arg
= wine_server_client_ptr( param
);
476 call
.create_thread
.reserve
= stack_reserve
;
477 call
.create_thread
.commit
= stack_commit
;
478 call
.create_thread
.suspend
= suspended
;
479 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
480 if (status
!= STATUS_SUCCESS
) return status
;
482 if (result
.create_thread
.status
== STATUS_SUCCESS
)
484 if (id
) id
->UniqueThread
= ULongToHandle(result
.create_thread
.tid
);
485 if (handle_ptr
) *handle_ptr
= wine_server_ptr_handle( result
.create_thread
.handle
);
486 else NtClose( wine_server_ptr_handle( result
.create_thread
.handle
));
488 return result
.create_thread
.status
;
491 if (server_pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
492 wine_server_send_fd( request_pipe
[0] );
494 SERVER_START_REQ( new_thread
)
496 req
->access
= THREAD_ALL_ACCESS
;
497 req
->attributes
= 0; /* FIXME */
498 req
->suspend
= suspended
;
499 req
->request_fd
= request_pipe
[0];
500 if (!(status
= wine_server_call( req
)))
502 handle
= wine_server_ptr_handle( reply
->handle
);
505 close( request_pipe
[0] );
511 close( request_pipe
[1] );
515 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
518 size
= sigstack_total_size
;
519 if ((status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
520 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
523 teb
->Peb
= NtCurrentTeb()->Peb
;
524 info
= (struct startup_info
*)(teb
+ 1);
526 info
->entry_point
= start
;
527 info
->entry_arg
= param
;
529 if ((status
= init_teb( teb
))) goto error
;
531 teb
->ClientId
.UniqueProcess
= ULongToHandle(GetCurrentProcessId());
532 teb
->ClientId
.UniqueThread
= ULongToHandle(tid
);
534 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
535 thread_regs
= (struct ntdll_thread_regs
*)teb
->SpareBytes1
;
536 thread_data
->request_fd
= request_pipe
[1];
538 /* inherit debug registers from parent thread */
539 thread_regs
->dr0
= ntdll_get_thread_regs()->dr0
;
540 thread_regs
->dr1
= ntdll_get_thread_regs()->dr1
;
541 thread_regs
->dr2
= ntdll_get_thread_regs()->dr2
;
542 thread_regs
->dr3
= ntdll_get_thread_regs()->dr3
;
543 thread_regs
->dr6
= ntdll_get_thread_regs()->dr6
;
544 thread_regs
->dr7
= ntdll_get_thread_regs()->dr7
;
546 if ((status
= virtual_alloc_thread_stack( teb
, stack_reserve
, stack_commit
))) goto error
;
548 pthread_attr_init( &attr
);
549 pthread_attr_setstack( &attr
, teb
->DeallocationStack
,
550 (char *)teb
->Tib
.StackBase
- (char *)teb
->DeallocationStack
);
551 pthread_attr_setscope( &attr
, PTHREAD_SCOPE_SYSTEM
); /* force creating a kernel thread */
552 interlocked_xchg_add( &nb_threads
, 1 );
553 if (pthread_create( &pthread_id
, &attr
, (void * (*)(void *))start_thread
, info
))
555 interlocked_xchg_add( &nb_threads
, -1 );
556 pthread_attr_destroy( &attr
);
558 NtFreeVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, &size
, MEM_RELEASE
);
559 status
= STATUS_NO_MEMORY
;
562 pthread_attr_destroy( &attr
);
563 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
565 if (id
) id
->UniqueThread
= ULongToHandle(tid
);
566 if (handle_ptr
) *handle_ptr
= handle
;
567 else NtClose( handle
);
569 return STATUS_SUCCESS
;
572 if (thread_data
) wine_ldt_free_fs( thread_data
->fs
);
576 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
578 if (handle
) NtClose( handle
);
579 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
580 close( request_pipe
[1] );
585 /***********************************************************************
586 * NtOpenThread (NTDLL.@)
587 * ZwOpenThread (NTDLL.@)
589 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
590 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
594 SERVER_START_REQ( open_thread
)
596 req
->tid
= HandleToULong(id
->UniqueThread
);
597 req
->access
= access
;
598 req
->attributes
= attr
? attr
->Attributes
: 0;
599 ret
= wine_server_call( req
);
600 *handle
= wine_server_ptr_handle( reply
->handle
);
607 /******************************************************************************
608 * NtSuspendThread (NTDLL.@)
609 * ZwSuspendThread (NTDLL.@)
611 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
615 SERVER_START_REQ( suspend_thread
)
617 req
->handle
= wine_server_obj_handle( handle
);
618 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
625 /******************************************************************************
626 * NtResumeThread (NTDLL.@)
627 * ZwResumeThread (NTDLL.@)
629 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
633 SERVER_START_REQ( resume_thread
)
635 req
->handle
= wine_server_obj_handle( handle
);
636 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
643 /******************************************************************************
644 * NtAlertResumeThread (NTDLL.@)
645 * ZwAlertResumeThread (NTDLL.@)
647 NTSTATUS WINAPI
NtAlertResumeThread( HANDLE handle
, PULONG count
)
649 FIXME( "stub: should alert thread %p\n", handle
);
650 return NtResumeThread( handle
, count
);
654 /******************************************************************************
655 * NtAlertThread (NTDLL.@)
656 * ZwAlertThread (NTDLL.@)
658 NTSTATUS WINAPI
NtAlertThread( HANDLE handle
)
660 FIXME( "stub: %p\n", handle
);
661 return STATUS_NOT_IMPLEMENTED
;
665 /******************************************************************************
666 * NtTerminateThread (NTDLL.@)
667 * ZwTerminateThread (NTDLL.@)
669 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
674 SERVER_START_REQ( terminate_thread
)
676 req
->handle
= wine_server_obj_handle( handle
);
677 req
->exit_code
= exit_code
;
678 ret
= wine_server_call( req
);
679 self
= !ret
&& reply
->self
;
686 if (last
) _exit( exit_code
);
687 else abort_thread( exit_code
);
693 /******************************************************************************
694 * NtQueueApcThread (NTDLL.@)
696 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
697 ULONG_PTR arg2
, ULONG_PTR arg3
)
700 SERVER_START_REQ( queue_apc
)
702 req
->handle
= wine_server_obj_handle( handle
);
705 req
->call
.type
= APC_USER
;
706 req
->call
.user
.func
= wine_server_client_ptr( func
);
707 req
->call
.user
.args
[0] = arg1
;
708 req
->call
.user
.args
[1] = arg2
;
709 req
->call
.user
.args
[2] = arg3
;
711 else req
->call
.type
= APC_NONE
; /* wake up only */
712 ret
= wine_server_call( req
);
719 /***********************************************************************
720 * NtSetContextThread (NTDLL.@)
721 * ZwSetContextThread (NTDLL.@)
723 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
730 /* on i386 debug registers always require a server call */
731 self
= (handle
== GetCurrentThread());
732 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
734 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
735 self
= (regs
->dr0
== context
->Dr0
&& regs
->dr1
== context
->Dr1
&&
736 regs
->dr2
== context
->Dr2
&& regs
->dr3
== context
->Dr3
&&
737 regs
->dr6
== context
->Dr6
&& regs
->dr7
== context
->Dr7
);
743 context_t server_context
;
745 context_to_server( &server_context
, context
);
747 SERVER_START_REQ( set_thread_context
)
749 req
->handle
= wine_server_obj_handle( handle
);
751 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
752 ret
= wine_server_call( req
);
757 if (ret
== STATUS_PENDING
)
759 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
761 for (i
= 0; i
< 100; i
++)
763 SERVER_START_REQ( set_thread_context
)
765 req
->handle
= wine_server_obj_handle( handle
);
767 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
768 ret
= wine_server_call( req
);
771 if (ret
== STATUS_PENDING
)
773 LARGE_INTEGER timeout
;
774 timeout
.QuadPart
= -10000;
775 NtDelayExecution( FALSE
, &timeout
);
779 NtResumeThread( handle
, &dummy
);
781 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
787 if (self
) set_cpu_context( context
);
788 return STATUS_SUCCESS
;
792 /* convert CPU-specific flags to generic server flags */
793 static inline unsigned int get_server_context_flags( DWORD flags
)
795 unsigned int ret
= 0;
797 flags
&= 0x3f; /* mask CPU id flags */
798 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
799 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
800 #ifdef CONTEXT_SEGMENTS
801 if (flags
& CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
803 #ifdef CONTEXT_FLOATING_POINT
804 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
806 #ifdef CONTEXT_DEBUG_REGISTERS
807 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
809 #ifdef CONTEXT_EXTENDED_REGISTERS
810 if (flags
& CONTEXT_EXTENDED_REGISTERS
) ret
|= SERVER_CTX_EXTENDED_REGISTERS
;
815 /***********************************************************************
816 * NtGetContextThread (NTDLL.@)
817 * ZwGetContextThread (NTDLL.@)
819 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
823 DWORD needed_flags
= context
->ContextFlags
;
824 BOOL self
= (handle
== GetCurrentThread());
827 /* on i386 debug registers always require a server call */
828 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)) self
= FALSE
;
833 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
834 context_t server_context
;
836 SERVER_START_REQ( get_thread_context
)
838 req
->handle
= wine_server_obj_handle( handle
);
839 req
->flags
= server_flags
;
841 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
842 ret
= wine_server_call( req
);
847 if (ret
== STATUS_PENDING
)
849 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
851 for (i
= 0; i
< 100; i
++)
853 SERVER_START_REQ( get_thread_context
)
855 req
->handle
= wine_server_obj_handle( handle
);
856 req
->flags
= server_flags
;
858 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
859 ret
= wine_server_call( req
);
862 if (ret
== STATUS_PENDING
)
864 LARGE_INTEGER timeout
;
865 timeout
.QuadPart
= -10000;
866 NtDelayExecution( FALSE
, &timeout
);
870 NtResumeThread( handle
, &dummy
);
872 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
874 if (!ret
) ret
= context_from_server( context
, &server_context
);
876 needed_flags
&= ~context
->ContextFlags
;
884 RtlCaptureContext( &ctx
);
885 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
886 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
889 /* update the cached version of the debug registers */
890 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
892 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
893 regs
->dr0
= context
->Dr0
;
894 regs
->dr1
= context
->Dr1
;
895 regs
->dr2
= context
->Dr2
;
896 regs
->dr3
= context
->Dr3
;
897 regs
->dr6
= context
->Dr6
;
898 regs
->dr7
= context
->Dr7
;
902 return STATUS_SUCCESS
;
906 /******************************************************************************
907 * NtQueryInformationThread (NTDLL.@)
908 * ZwQueryInformationThread (NTDLL.@)
910 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
911 void *data
, ULONG length
, ULONG
*ret_len
)
917 case ThreadBasicInformation
:
919 THREAD_BASIC_INFORMATION info
;
920 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
922 SERVER_START_REQ( get_thread_info
)
924 req
->handle
= wine_server_obj_handle( handle
);
926 if (!(status
= wine_server_call( req
)))
928 info
.ExitStatus
= reply
->exit_code
;
929 info
.TebBaseAddress
= wine_server_get_ptr( reply
->teb
);
930 info
.ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
931 info
.ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
932 info
.AffinityMask
= reply
->affinity
& affinity_mask
;
933 info
.Priority
= reply
->priority
;
934 info
.BasePriority
= reply
->priority
; /* FIXME */
938 if (status
== STATUS_SUCCESS
)
940 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
941 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
945 case ThreadAffinityMask
:
947 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
948 ULONG_PTR affinity
= 0;
950 SERVER_START_REQ( get_thread_info
)
952 req
->handle
= wine_server_obj_handle( handle
);
954 if (!(status
= wine_server_call( req
)))
955 affinity
= reply
->affinity
& affinity_mask
;
958 if (status
== STATUS_SUCCESS
)
960 if (data
) memcpy( data
, &affinity
, min( length
, sizeof(affinity
) ));
961 if (ret_len
) *ret_len
= min( length
, sizeof(affinity
) );
967 KERNEL_USER_TIMES kusrt
;
968 /* We need to do a server call to get the creation time or exit time */
969 /* This works on any thread */
970 SERVER_START_REQ( get_thread_info
)
972 req
->handle
= wine_server_obj_handle( handle
);
974 status
= wine_server_call( req
);
975 if (status
== STATUS_SUCCESS
)
977 kusrt
.CreateTime
.QuadPart
= reply
->creation_time
;
978 kusrt
.ExitTime
.QuadPart
= reply
->exit_time
;
982 if (status
== STATUS_SUCCESS
)
984 /* We call times(2) for kernel time or user time */
985 /* We can only (portably) do this for the current thread */
986 if (handle
== GetCurrentThread())
989 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
992 kusrt
.KernelTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
993 kusrt
.UserTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
997 static BOOL reported
= FALSE
;
999 kusrt
.KernelTime
.QuadPart
= 0;
1000 kusrt
.UserTime
.QuadPart
= 0;
1002 TRACE("Cannot get kerneltime or usertime of other threads\n");
1005 FIXME("Cannot get kerneltime or usertime of other threads\n");
1009 if (data
) memcpy( data
, &kusrt
, min( length
, sizeof(kusrt
) ));
1010 if (ret_len
) *ret_len
= min( length
, sizeof(kusrt
) );
1014 case ThreadDescriptorTableEntry
:
1017 THREAD_DESCRIPTOR_INFORMATION
* tdi
= data
;
1018 if (length
< sizeof(*tdi
))
1019 status
= STATUS_INFO_LENGTH_MISMATCH
;
1020 else if (!(tdi
->Selector
& 4)) /* GDT selector */
1022 unsigned sel
= tdi
->Selector
& ~3; /* ignore RPL */
1023 status
= STATUS_SUCCESS
;
1024 if (!sel
) /* null selector */
1025 memset( &tdi
->Entry
, 0, sizeof(tdi
->Entry
) );
1028 tdi
->Entry
.BaseLow
= 0;
1029 tdi
->Entry
.HighWord
.Bits
.BaseMid
= 0;
1030 tdi
->Entry
.HighWord
.Bits
.BaseHi
= 0;
1031 tdi
->Entry
.LimitLow
= 0xffff;
1032 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0xf;
1033 tdi
->Entry
.HighWord
.Bits
.Dpl
= 3;
1034 tdi
->Entry
.HighWord
.Bits
.Sys
= 0;
1035 tdi
->Entry
.HighWord
.Bits
.Pres
= 1;
1036 tdi
->Entry
.HighWord
.Bits
.Granularity
= 1;
1037 tdi
->Entry
.HighWord
.Bits
.Default_Big
= 1;
1038 tdi
->Entry
.HighWord
.Bits
.Type
= 0x12;
1039 /* it has to be one of the system GDT selectors */
1040 if (sel
!= (wine_get_ds() & ~3) && sel
!= (wine_get_ss() & ~3))
1042 if (sel
== (wine_get_cs() & ~3))
1043 tdi
->Entry
.HighWord
.Bits
.Type
|= 8; /* code segment */
1044 else status
= STATUS_ACCESS_DENIED
;
1050 SERVER_START_REQ( get_selector_entry
)
1052 req
->handle
= wine_server_obj_handle( handle
);
1053 req
->entry
= tdi
->Selector
>> 3;
1054 status
= wine_server_call( req
);
1057 if (!(reply
->flags
& WINE_LDT_FLAGS_ALLOCATED
))
1058 status
= STATUS_ACCESS_VIOLATION
;
1061 wine_ldt_set_base ( &tdi
->Entry
, (void *)reply
->base
);
1062 wine_ldt_set_limit( &tdi
->Entry
, reply
->limit
);
1063 wine_ldt_set_flags( &tdi
->Entry
, reply
->flags
);
1069 if (status
== STATUS_SUCCESS
&& ret_len
)
1070 /* yes, that's a bit strange, but it's the way it is */
1071 *ret_len
= sizeof(LDT_ENTRY
);
1073 status
= STATUS_NOT_IMPLEMENTED
;
1077 case ThreadAmILastThread
:
1079 SERVER_START_REQ(get_thread_info
)
1081 req
->handle
= wine_server_obj_handle( handle
);
1083 status
= wine_server_call( req
);
1084 if (status
== STATUS_SUCCESS
)
1086 BOOLEAN last
= reply
->last
;
1087 if (data
) memcpy( data
, &last
, min( length
, sizeof(last
) ));
1088 if (ret_len
) *ret_len
= min( length
, sizeof(last
) );
1094 case ThreadPriority
:
1095 case ThreadBasePriority
:
1096 case ThreadImpersonationToken
:
1097 case ThreadEnableAlignmentFaultFixup
:
1098 case ThreadEventPair_Reusable
:
1099 case ThreadQuerySetWin32StartAddress
:
1100 case ThreadZeroTlsCell
:
1101 case ThreadPerformanceCount
:
1102 case ThreadIdealProcessor
:
1103 case ThreadPriorityBoost
:
1104 case ThreadSetTlsArrayAddress
:
1105 case ThreadIsIoPending
:
1107 FIXME( "info class %d not supported yet\n", class );
1108 return STATUS_NOT_IMPLEMENTED
;
1113 /******************************************************************************
1114 * NtSetInformationThread (NTDLL.@)
1115 * ZwSetInformationThread (NTDLL.@)
1117 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1118 LPCVOID data
, ULONG length
)
1123 case ThreadZeroTlsCell
:
1124 if (handle
== GetCurrentThread())
1129 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1130 index
= *(const DWORD
*)data
;
1131 if (index
< TLS_MINIMUM_AVAILABLE
)
1133 RtlAcquirePebLock();
1134 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1136 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1137 teb
->TlsSlots
[index
] = 0;
1139 RtlReleasePebLock();
1143 index
-= TLS_MINIMUM_AVAILABLE
;
1144 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
1145 return STATUS_INVALID_PARAMETER
;
1146 RtlAcquirePebLock();
1147 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1149 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1150 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
1152 RtlReleasePebLock();
1154 return STATUS_SUCCESS
;
1156 FIXME( "ZeroTlsCell not supported on other threads\n" );
1157 return STATUS_NOT_IMPLEMENTED
;
1159 case ThreadImpersonationToken
:
1161 const HANDLE
*phToken
= data
;
1162 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
1163 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
1164 SERVER_START_REQ( set_thread_info
)
1166 req
->handle
= wine_server_obj_handle( handle
);
1167 req
->token
= wine_server_obj_handle( *phToken
);
1168 req
->mask
= SET_THREAD_INFO_TOKEN
;
1169 status
= wine_server_call( req
);
1174 case ThreadBasePriority
:
1176 const DWORD
*pprio
= data
;
1177 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1178 SERVER_START_REQ( set_thread_info
)
1180 req
->handle
= wine_server_obj_handle( handle
);
1181 req
->priority
= *pprio
;
1182 req
->mask
= SET_THREAD_INFO_PRIORITY
;
1183 status
= wine_server_call( req
);
1188 case ThreadAffinityMask
:
1190 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1191 const ULONG_PTR
*paff
= data
;
1192 if (length
!= sizeof(ULONG_PTR
)) return STATUS_INVALID_PARAMETER
;
1193 if (*paff
& ~affinity_mask
) return STATUS_INVALID_PARAMETER
;
1194 SERVER_START_REQ( set_thread_info
)
1196 req
->handle
= wine_server_obj_handle( handle
);
1197 req
->affinity
= *paff
;
1198 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1199 status
= wine_server_call( req
);
1204 case ThreadBasicInformation
:
1206 case ThreadPriority
:
1207 case ThreadDescriptorTableEntry
:
1208 case ThreadEnableAlignmentFaultFixup
:
1209 case ThreadEventPair_Reusable
:
1210 case ThreadQuerySetWin32StartAddress
:
1211 case ThreadPerformanceCount
:
1212 case ThreadAmILastThread
:
1213 case ThreadIdealProcessor
:
1214 case ThreadPriorityBoost
:
1215 case ThreadSetTlsArrayAddress
:
1216 case ThreadIsIoPending
:
1218 FIXME( "info class %d not supported yet\n", class );
1219 return STATUS_NOT_IMPLEMENTED
;