4 * Copyright 1996, 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_MMAN_H
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
33 #ifdef HAVE_SYS_SYSCALL_H
34 #include <sys/syscall.h>
37 #define NONAMELESSUNION
39 #define WIN32_NO_STATUS
41 #include "wine/library.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
44 #include "ntdll_misc.h"
46 #include "wine/exception.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
49 WINE_DECLARE_DEBUG_CHANNEL(relay
);
51 struct _KUSER_SHARED_DATA
*user_shared_data
= NULL
;
53 PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter
= NULL
;
55 /* info passed to a starting thread */
59 PRTL_THREAD_START_ROUTINE entry_point
;
64 static PEB_LDR_DATA ldr
;
65 static RTL_USER_PROCESS_PARAMETERS params
; /* default parameters if no parent */
66 static WCHAR current_dir
[MAX_NT_PATH_LENGTH
];
67 static RTL_BITMAP tls_bitmap
;
68 static RTL_BITMAP tls_expansion_bitmap
;
69 static RTL_BITMAP fls_bitmap
;
70 static int nb_threads
= 1;
72 /***********************************************************************
75 * Copy a unicode string from the startup info.
77 static inline void get_unicode_string( UNICODE_STRING
*str
, WCHAR
**src
, WCHAR
**dst
, UINT len
)
81 str
->MaximumLength
= len
+ sizeof(WCHAR
);
82 memcpy( str
->Buffer
, *src
, len
);
83 str
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
84 *src
+= len
/ sizeof(WCHAR
);
85 *dst
+= len
/ sizeof(WCHAR
) + 1;
88 /***********************************************************************
89 * init_user_process_params
91 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
93 static NTSTATUS
init_user_process_params( SIZE_T data_size
, HANDLE
*exe_file
)
97 SIZE_T info_size
, env_size
, size
, alloc_size
;
100 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
102 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, data_size
)))
103 return STATUS_NO_MEMORY
;
105 SERVER_START_REQ( get_startup_info
)
107 wine_server_set_reply( req
, info
, data_size
);
108 if (!(status
= wine_server_call( req
)))
110 data_size
= wine_server_reply_size( reply
);
111 info_size
= reply
->info_size
;
112 env_size
= data_size
- info_size
;
113 *exe_file
= wine_server_ptr_handle( reply
->exe_file
);
117 if (status
!= STATUS_SUCCESS
) goto done
;
119 size
= sizeof(*params
);
120 size
+= MAX_NT_PATH_LENGTH
* sizeof(WCHAR
);
121 size
+= info
->dllpath_len
+ sizeof(WCHAR
);
122 size
+= info
->imagepath_len
+ sizeof(WCHAR
);
123 size
+= info
->cmdline_len
+ sizeof(WCHAR
);
124 size
+= info
->title_len
+ sizeof(WCHAR
);
125 size
+= info
->desktop_len
+ sizeof(WCHAR
);
126 size
+= info
->shellinfo_len
+ sizeof(WCHAR
);
127 size
+= info
->runtime_len
+ sizeof(WCHAR
);
130 status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &alloc_size
,
131 MEM_COMMIT
, PAGE_READWRITE
);
132 if (status
!= STATUS_SUCCESS
) goto done
;
134 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
135 params
->AllocationSize
= alloc_size
;
137 params
->Flags
= PROCESS_PARAMS_FLAG_NORMALIZED
;
138 params
->DebugFlags
= info
->debug_flags
;
139 params
->ConsoleHandle
= wine_server_ptr_handle( info
->console
);
140 params
->ConsoleFlags
= info
->console_flags
;
141 params
->hStdInput
= wine_server_ptr_handle( info
->hstdin
);
142 params
->hStdOutput
= wine_server_ptr_handle( info
->hstdout
);
143 params
->hStdError
= wine_server_ptr_handle( info
->hstderr
);
144 params
->dwX
= info
->x
;
145 params
->dwY
= info
->y
;
146 params
->dwXSize
= info
->xsize
;
147 params
->dwYSize
= info
->ysize
;
148 params
->dwXCountChars
= info
->xchars
;
149 params
->dwYCountChars
= info
->ychars
;
150 params
->dwFillAttribute
= info
->attribute
;
151 params
->dwFlags
= info
->flags
;
152 params
->wShowWindow
= info
->show
;
154 src
= (WCHAR
*)(info
+ 1);
155 dst
= (WCHAR
*)(params
+ 1);
157 /* current directory needs more space */
158 get_unicode_string( ¶ms
->CurrentDirectory
.DosPath
, &src
, &dst
, info
->curdir_len
);
159 params
->CurrentDirectory
.DosPath
.MaximumLength
= MAX_NT_PATH_LENGTH
* sizeof(WCHAR
);
160 dst
= (WCHAR
*)(params
+ 1) + MAX_NT_PATH_LENGTH
;
162 get_unicode_string( ¶ms
->DllPath
, &src
, &dst
, info
->dllpath_len
);
163 get_unicode_string( ¶ms
->ImagePathName
, &src
, &dst
, info
->imagepath_len
);
164 get_unicode_string( ¶ms
->CommandLine
, &src
, &dst
, info
->cmdline_len
);
165 get_unicode_string( ¶ms
->WindowTitle
, &src
, &dst
, info
->title_len
);
166 get_unicode_string( ¶ms
->Desktop
, &src
, &dst
, info
->desktop_len
);
167 get_unicode_string( ¶ms
->ShellInfo
, &src
, &dst
, info
->shellinfo_len
);
169 /* runtime info isn't a real string */
170 params
->RuntimeInfo
.Buffer
= dst
;
171 params
->RuntimeInfo
.Length
= params
->RuntimeInfo
.MaximumLength
= info
->runtime_len
;
172 memcpy( dst
, src
, info
->runtime_len
);
174 /* environment needs to be a separate memory block */
176 alloc_size
= max( 1, env_size
);
177 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &alloc_size
,
178 MEM_COMMIT
, PAGE_READWRITE
);
179 if (status
!= STATUS_SUCCESS
) goto done
;
180 memcpy( ptr
, (char *)info
+ info_size
, env_size
);
181 params
->Environment
= ptr
;
184 RtlFreeHeap( GetProcessHeap(), 0, info
);
189 #include <mach/mach.h>
190 #include <mach/mach_error.h>
192 static ULONG
get_dyld_image_info_addr(void)
195 #ifdef TASK_DYLD_INFO
196 struct task_dyld_info dyld_info
;
197 mach_msg_type_number_t size
= TASK_DYLD_INFO_COUNT
;
198 if (task_info(mach_task_self(), TASK_DYLD_INFO
, (task_info_t
)&dyld_info
, &size
) == KERN_SUCCESS
)
199 ret
= dyld_info
.all_image_info_addr
;
203 #endif /* __APPLE__ */
205 /***********************************************************************
208 * Setup the initial thread.
210 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
212 HANDLE
thread_init(void)
216 SIZE_T size
, info_size
;
220 struct ntdll_thread_data
*thread_data
;
221 static struct debug_info debug_info
; /* debug info for initial thread */
225 /* reserve space for shared user data */
227 addr
= (void *)0x7ffe0000;
229 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
,
230 MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
233 MESSAGE( "wine: failed to map the shared user data: %08x\n", status
);
236 user_shared_data
= addr
;
238 /* allocate and initialize the PEB */
242 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 1, &size
,
243 MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
246 peb
->ProcessParameters
= ¶ms
;
247 peb
->TlsBitmap
= &tls_bitmap
;
248 peb
->TlsExpansionBitmap
= &tls_expansion_bitmap
;
249 peb
->FlsBitmap
= &fls_bitmap
;
251 params
.CurrentDirectory
.DosPath
.Buffer
= current_dir
;
252 params
.CurrentDirectory
.DosPath
.MaximumLength
= sizeof(current_dir
);
253 params
.wShowWindow
= 1; /* SW_SHOWNORMAL */
254 ldr
.Length
= sizeof(ldr
);
255 RtlInitializeBitMap( &tls_bitmap
, peb
->TlsBitmapBits
, sizeof(peb
->TlsBitmapBits
) * 8 );
256 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
->TlsExpansionBitmapBits
,
257 sizeof(peb
->TlsExpansionBitmapBits
) * 8 );
258 RtlInitializeBitMap( &fls_bitmap
, peb
->FlsBitmapBits
, sizeof(peb
->FlsBitmapBits
) * 8 );
259 InitializeListHead( &peb
->FlsListHead
);
260 InitializeListHead( &ldr
.InLoadOrderModuleList
);
261 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
262 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
264 peb
->Reserved
[0] = get_dyld_image_info_addr();
267 /* allocate and initialize the initial TEB */
269 signal_alloc_thread( &teb
);
271 teb
->Tib
.StackBase
= (void *)~0UL;
272 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
273 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
275 thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
276 thread_data
->request_fd
= -1;
277 thread_data
->reply_fd
= -1;
278 thread_data
->wait_fd
[0] = -1;
279 thread_data
->wait_fd
[1] = -1;
280 thread_data
->debug_info
= &debug_info
;
281 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
283 signal_init_thread( teb
);
284 virtual_init_threading();
286 debug_info
.str_pos
= debug_info
.strings
;
287 debug_info
.out_pos
= debug_info
.output
;
290 /* setup the server connection */
291 server_init_process();
292 info_size
= server_init_thread( peb
);
294 /* create the process heap */
295 if (!(peb
->ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
297 MESSAGE( "wine: failed to create the process heap\n" );
301 /* allocate user parameters */
304 init_user_process_params( info_size
, &exe_file
);
308 if (isatty(0) || isatty(1) || isatty(2))
309 params
.ConsoleHandle
= (HANDLE
)2; /* see kernel32/kernel_private.h */
311 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdInput
);
313 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
315 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
318 /* initialize time values in user_shared_data */
319 NtQuerySystemTime( &now
);
320 user_shared_data
->SystemTime
.LowPart
= now
.u
.LowPart
;
321 user_shared_data
->SystemTime
.High1Time
= user_shared_data
->SystemTime
.High2Time
= now
.u
.HighPart
;
322 user_shared_data
->u
.TickCountQuad
= (now
.QuadPart
- server_start_time
) / 10000;
323 user_shared_data
->u
.TickCount
.High2Time
= user_shared_data
->u
.TickCount
.High1Time
;
324 user_shared_data
->TickCountLowDeprecated
= user_shared_data
->u
.TickCount
.LowPart
;
325 user_shared_data
->TickCountMultiplier
= 1 << 24;
329 NtCreateKeyedEvent( &keyed_event
, GENERIC_READ
| GENERIC_WRITE
, NULL
, 0 );
335 /***********************************************************************
338 void terminate_thread( int status
)
340 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
341 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1) _exit( status
);
343 close( ntdll_get_thread_data()->wait_fd
[0] );
344 close( ntdll_get_thread_data()->wait_fd
[1] );
345 close( ntdll_get_thread_data()->reply_fd
);
346 close( ntdll_get_thread_data()->request_fd
);
347 pthread_exit( UIntToPtr(status
) );
351 /***********************************************************************
354 void exit_thread( int status
)
356 static void *prev_teb
;
359 if (status
) /* send the exit code to the server (0 is already the default) */
361 SERVER_START_REQ( terminate_thread
)
363 req
->handle
= wine_server_obj_handle( GetCurrentThread() );
364 req
->exit_code
= status
;
365 wine_server_call( req
);
370 if (interlocked_xchg_add( &nb_threads
, -1 ) <= 1)
372 LdrShutdownProcess();
378 pthread_sigmask( SIG_BLOCK
, &server_block_set
, NULL
);
380 if ((teb
= interlocked_xchg_ptr( &prev_teb
, NtCurrentTeb() )))
382 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
384 if (thread_data
->pthread_id
)
386 pthread_join( thread_data
->pthread_id
, NULL
);
387 signal_free_thread( teb
);
391 close( ntdll_get_thread_data()->wait_fd
[0] );
392 close( ntdll_get_thread_data()->wait_fd
[1] );
393 close( ntdll_get_thread_data()->reply_fd
);
394 close( ntdll_get_thread_data()->request_fd
);
395 pthread_exit( UIntToPtr(status
) );
399 /***********************************************************************
402 * Startup routine for a newly created thread.
404 static void start_thread( struct startup_info
*info
)
406 TEB
*teb
= info
->teb
;
407 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
408 PRTL_THREAD_START_ROUTINE func
= info
->entry_point
;
409 void *arg
= info
->entry_arg
;
410 struct debug_info debug_info
;
412 debug_info
.str_pos
= debug_info
.strings
;
413 debug_info
.out_pos
= debug_info
.output
;
414 thread_data
->debug_info
= &debug_info
;
415 thread_data
->pthread_id
= pthread_self();
417 signal_init_thread( teb
);
418 server_init_thread( func
);
419 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
421 MODULE_DllThreadAttach( NULL
);
424 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), func
, arg
);
426 call_thread_entry_point( (LPTHREAD_START_ROUTINE
)func
, arg
);
430 /***********************************************************************
431 * RtlCreateUserThread (NTDLL.@)
433 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
434 BOOLEAN suspended
, PVOID stack_addr
,
435 SIZE_T stack_reserve
, SIZE_T stack_commit
,
436 PRTL_THREAD_START_ROUTINE start
, void *param
,
437 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
440 pthread_t pthread_id
;
442 struct ntdll_thread_data
*thread_data
;
443 struct startup_info
*info
= NULL
;
444 HANDLE handle
= 0, actctx
= 0;
450 if (process
!= NtCurrentProcess())
455 memset( &call
, 0, sizeof(call
) );
457 call
.create_thread
.type
= APC_CREATE_THREAD
;
458 call
.create_thread
.func
= wine_server_client_ptr( start
);
459 call
.create_thread
.arg
= wine_server_client_ptr( param
);
460 call
.create_thread
.reserve
= stack_reserve
;
461 call
.create_thread
.commit
= stack_commit
;
462 call
.create_thread
.suspend
= suspended
;
463 status
= server_queue_process_apc( process
, &call
, &result
);
464 if (status
!= STATUS_SUCCESS
) return status
;
466 if (result
.create_thread
.status
== STATUS_SUCCESS
)
468 if (id
) id
->UniqueThread
= ULongToHandle(result
.create_thread
.tid
);
469 if (handle_ptr
) *handle_ptr
= wine_server_ptr_handle( result
.create_thread
.handle
);
470 else NtClose( wine_server_ptr_handle( result
.create_thread
.handle
));
472 return result
.create_thread
.status
;
475 if (server_pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
476 wine_server_send_fd( request_pipe
[0] );
478 SERVER_START_REQ( new_thread
)
480 req
->access
= THREAD_ALL_ACCESS
;
481 req
->attributes
= 0; /* FIXME */
482 req
->suspend
= suspended
;
483 req
->request_fd
= request_pipe
[0];
484 if (!(status
= wine_server_call( req
)))
486 handle
= wine_server_ptr_handle( reply
->handle
);
489 close( request_pipe
[0] );
495 close( request_pipe
[1] );
499 pthread_sigmask( SIG_BLOCK
, &server_block_set
, &sigset
);
501 if ((status
= signal_alloc_thread( &teb
))) goto error
;
503 teb
->Peb
= NtCurrentTeb()->Peb
;
504 teb
->ClientId
.UniqueProcess
= ULongToHandle(GetCurrentProcessId());
505 teb
->ClientId
.UniqueThread
= ULongToHandle(tid
);
506 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
507 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
509 /* create default activation context frame for new thread */
510 RtlGetActiveActivationContext(&actctx
);
513 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
;
515 frame
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*frame
));
516 frame
->Previous
= NULL
;
517 frame
->ActivationContext
= actctx
;
519 teb
->ActivationContextStack
.ActiveFrame
= frame
;
521 RtlAddRefActivationContext(actctx
);
524 info
= (struct startup_info
*)(teb
+ 1);
526 info
->entry_point
= start
;
527 info
->entry_arg
= param
;
529 thread_data
= (struct ntdll_thread_data
*)teb
->SpareBytes1
;
530 thread_data
->request_fd
= request_pipe
[1];
531 thread_data
->reply_fd
= -1;
532 thread_data
->wait_fd
[0] = -1;
533 thread_data
->wait_fd
[1] = -1;
535 if ((status
= virtual_alloc_thread_stack( teb
, stack_reserve
, stack_commit
))) goto error
;
537 pthread_attr_init( &attr
);
538 pthread_attr_setstack( &attr
, teb
->DeallocationStack
,
539 (char *)teb
->Tib
.StackBase
- (char *)teb
->DeallocationStack
);
540 pthread_attr_setscope( &attr
, PTHREAD_SCOPE_SYSTEM
); /* force creating a kernel thread */
541 interlocked_xchg_add( &nb_threads
, 1 );
542 if (pthread_create( &pthread_id
, &attr
, (void * (*)(void *))start_thread
, info
))
544 interlocked_xchg_add( &nb_threads
, -1 );
545 pthread_attr_destroy( &attr
);
546 status
= STATUS_NO_MEMORY
;
549 pthread_attr_destroy( &attr
);
550 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
552 if (id
) id
->UniqueThread
= ULongToHandle(tid
);
553 if (handle_ptr
) *handle_ptr
= handle
;
554 else NtClose( handle
);
556 return STATUS_SUCCESS
;
559 if (teb
) signal_free_thread( teb
);
560 if (handle
) NtClose( handle
);
561 pthread_sigmask( SIG_SETMASK
, &sigset
, NULL
);
562 close( request_pipe
[1] );
567 /******************************************************************************
568 * RtlGetNtGlobalFlags (NTDLL.@)
570 ULONG WINAPI
RtlGetNtGlobalFlags(void)
572 if (!peb
) return 0; /* init not done yet */
573 return peb
->NtGlobalFlag
;
577 /***********************************************************************
578 * NtOpenThread (NTDLL.@)
579 * ZwOpenThread (NTDLL.@)
581 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
582 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
586 SERVER_START_REQ( open_thread
)
588 req
->tid
= HandleToULong(id
->UniqueThread
);
589 req
->access
= access
;
590 req
->attributes
= attr
? attr
->Attributes
: 0;
591 ret
= wine_server_call( req
);
592 *handle
= wine_server_ptr_handle( reply
->handle
);
599 /******************************************************************************
600 * NtSuspendThread (NTDLL.@)
601 * ZwSuspendThread (NTDLL.@)
603 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
607 SERVER_START_REQ( suspend_thread
)
609 req
->handle
= wine_server_obj_handle( handle
);
610 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
617 /******************************************************************************
618 * NtResumeThread (NTDLL.@)
619 * ZwResumeThread (NTDLL.@)
621 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
625 SERVER_START_REQ( resume_thread
)
627 req
->handle
= wine_server_obj_handle( handle
);
628 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
635 /******************************************************************************
636 * NtAlertResumeThread (NTDLL.@)
637 * ZwAlertResumeThread (NTDLL.@)
639 NTSTATUS WINAPI
NtAlertResumeThread( HANDLE handle
, PULONG count
)
641 FIXME( "stub: should alert thread %p\n", handle
);
642 return NtResumeThread( handle
, count
);
646 /******************************************************************************
647 * NtAlertThread (NTDLL.@)
648 * ZwAlertThread (NTDLL.@)
650 NTSTATUS WINAPI
NtAlertThread( HANDLE handle
)
652 FIXME( "stub: %p\n", handle
);
653 return STATUS_NOT_IMPLEMENTED
;
657 /******************************************************************************
658 * NtTerminateThread (NTDLL.@)
659 * ZwTerminateThread (NTDLL.@)
661 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
666 SERVER_START_REQ( terminate_thread
)
668 req
->handle
= wine_server_obj_handle( handle
);
669 req
->exit_code
= exit_code
;
670 ret
= wine_server_call( req
);
671 self
= !ret
&& reply
->self
;
675 if (self
) abort_thread( exit_code
);
680 /******************************************************************************
681 * NtQueueApcThread (NTDLL.@)
683 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
684 ULONG_PTR arg2
, ULONG_PTR arg3
)
687 SERVER_START_REQ( queue_apc
)
689 req
->handle
= wine_server_obj_handle( handle
);
692 req
->call
.type
= APC_USER
;
693 req
->call
.user
.func
= wine_server_client_ptr( func
);
694 req
->call
.user
.args
[0] = arg1
;
695 req
->call
.user
.args
[1] = arg2
;
696 req
->call
.user
.args
[2] = arg3
;
698 else req
->call
.type
= APC_NONE
; /* wake up only */
699 ret
= wine_server_call( req
);
706 /***********************************************************************
707 * NtSetContextThread (NTDLL.@)
708 * ZwSetContextThread (NTDLL.@)
710 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
717 /* on i386 debug registers always require a server call */
718 self
= (handle
== GetCurrentThread());
719 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
721 self
= (ntdll_get_thread_data()->dr0
== context
->Dr0
&&
722 ntdll_get_thread_data()->dr1
== context
->Dr1
&&
723 ntdll_get_thread_data()->dr2
== context
->Dr2
&&
724 ntdll_get_thread_data()->dr3
== context
->Dr3
&&
725 ntdll_get_thread_data()->dr6
== context
->Dr6
&&
726 ntdll_get_thread_data()->dr7
== context
->Dr7
);
732 context_t server_context
;
734 context_to_server( &server_context
, context
);
736 SERVER_START_REQ( set_thread_context
)
738 req
->handle
= wine_server_obj_handle( handle
);
740 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
741 ret
= wine_server_call( req
);
746 if (ret
== STATUS_PENDING
)
748 for (i
= 0; i
< 100; i
++)
750 SERVER_START_REQ( set_thread_context
)
752 req
->handle
= wine_server_obj_handle( handle
);
754 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
755 ret
= wine_server_call( req
);
758 if (ret
== STATUS_PENDING
)
760 LARGE_INTEGER timeout
;
761 timeout
.QuadPart
= -10000;
762 NtDelayExecution( FALSE
, &timeout
);
766 NtResumeThread( handle
, &dummy
);
767 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
773 if (self
) set_cpu_context( context
);
774 return STATUS_SUCCESS
;
778 /* convert CPU-specific flags to generic server flags */
779 static inline unsigned int get_server_context_flags( DWORD flags
)
781 unsigned int ret
= 0;
783 flags
&= 0x3f; /* mask CPU id flags */
784 if (flags
& CONTEXT_CONTROL
) ret
|= SERVER_CTX_CONTROL
;
785 if (flags
& CONTEXT_INTEGER
) ret
|= SERVER_CTX_INTEGER
;
786 #ifdef CONTEXT_SEGMENTS
787 if (flags
& CONTEXT_SEGMENTS
) ret
|= SERVER_CTX_SEGMENTS
;
789 #ifdef CONTEXT_FLOATING_POINT
790 if (flags
& CONTEXT_FLOATING_POINT
) ret
|= SERVER_CTX_FLOATING_POINT
;
792 #ifdef CONTEXT_DEBUG_REGISTERS
793 if (flags
& CONTEXT_DEBUG_REGISTERS
) ret
|= SERVER_CTX_DEBUG_REGISTERS
;
795 #ifdef CONTEXT_EXTENDED_REGISTERS
796 if (flags
& CONTEXT_EXTENDED_REGISTERS
) ret
|= SERVER_CTX_EXTENDED_REGISTERS
;
801 /***********************************************************************
802 * NtGetContextThread (NTDLL.@)
803 * ZwGetContextThread (NTDLL.@)
805 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
809 DWORD needed_flags
= context
->ContextFlags
;
810 BOOL self
= (handle
== GetCurrentThread());
813 /* on i386 debug registers always require a server call */
814 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)) self
= FALSE
;
819 unsigned int server_flags
= get_server_context_flags( context
->ContextFlags
);
820 context_t server_context
;
822 SERVER_START_REQ( get_thread_context
)
824 req
->handle
= wine_server_obj_handle( handle
);
825 req
->flags
= server_flags
;
827 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
828 ret
= wine_server_call( req
);
833 if (ret
== STATUS_PENDING
)
835 for (i
= 0; i
< 100; i
++)
837 SERVER_START_REQ( get_thread_context
)
839 req
->handle
= wine_server_obj_handle( handle
);
840 req
->flags
= server_flags
;
842 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
843 ret
= wine_server_call( req
);
846 if (ret
== STATUS_PENDING
)
848 LARGE_INTEGER timeout
;
849 timeout
.QuadPart
= -10000;
850 NtDelayExecution( FALSE
, &timeout
);
854 NtResumeThread( handle
, &dummy
);
855 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
857 if (!ret
) ret
= context_from_server( context
, &server_context
);
859 needed_flags
&= ~context
->ContextFlags
;
867 RtlCaptureContext( &ctx
);
868 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
869 context
->ContextFlags
|= ctx
.ContextFlags
& needed_flags
;
872 /* update the cached version of the debug registers */
873 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
875 ntdll_get_thread_data()->dr0
= context
->Dr0
;
876 ntdll_get_thread_data()->dr1
= context
->Dr1
;
877 ntdll_get_thread_data()->dr2
= context
->Dr2
;
878 ntdll_get_thread_data()->dr3
= context
->Dr3
;
879 ntdll_get_thread_data()->dr6
= context
->Dr6
;
880 ntdll_get_thread_data()->dr7
= context
->Dr7
;
884 return STATUS_SUCCESS
;
888 /******************************************************************************
889 * NtQueryInformationThread (NTDLL.@)
890 * ZwQueryInformationThread (NTDLL.@)
892 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
893 void *data
, ULONG length
, ULONG
*ret_len
)
899 case ThreadBasicInformation
:
901 THREAD_BASIC_INFORMATION info
;
902 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
904 SERVER_START_REQ( get_thread_info
)
906 req
->handle
= wine_server_obj_handle( handle
);
908 if (!(status
= wine_server_call( req
)))
910 info
.ExitStatus
= reply
->exit_code
;
911 info
.TebBaseAddress
= wine_server_get_ptr( reply
->teb
);
912 info
.ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
913 info
.ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
914 info
.AffinityMask
= reply
->affinity
& affinity_mask
;
915 info
.Priority
= reply
->priority
;
916 info
.BasePriority
= reply
->priority
; /* FIXME */
920 if (status
== STATUS_SUCCESS
)
922 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
923 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
927 case ThreadAffinityMask
:
929 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
930 ULONG_PTR affinity
= 0;
932 SERVER_START_REQ( get_thread_info
)
934 req
->handle
= wine_server_obj_handle( handle
);
936 if (!(status
= wine_server_call( req
)))
937 affinity
= reply
->affinity
& affinity_mask
;
940 if (status
== STATUS_SUCCESS
)
942 if (data
) memcpy( data
, &affinity
, min( length
, sizeof(affinity
) ));
943 if (ret_len
) *ret_len
= min( length
, sizeof(affinity
) );
949 KERNEL_USER_TIMES kusrt
;
950 /* We need to do a server call to get the creation time or exit time */
951 /* This works on any thread */
952 SERVER_START_REQ( get_thread_info
)
954 req
->handle
= wine_server_obj_handle( handle
);
956 status
= wine_server_call( req
);
957 if (status
== STATUS_SUCCESS
)
959 kusrt
.CreateTime
.QuadPart
= reply
->creation_time
;
960 kusrt
.ExitTime
.QuadPart
= reply
->exit_time
;
964 if (status
== STATUS_SUCCESS
)
966 /* We call times(2) for kernel time or user time */
967 /* We can only (portably) do this for the current thread */
968 if (handle
== GetCurrentThread())
971 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
974 kusrt
.KernelTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
975 kusrt
.UserTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
979 static BOOL reported
= FALSE
;
981 kusrt
.KernelTime
.QuadPart
= 0;
982 kusrt
.UserTime
.QuadPart
= 0;
984 TRACE("Cannot get kerneltime or usertime of other threads\n");
987 FIXME("Cannot get kerneltime or usertime of other threads\n");
991 if (data
) memcpy( data
, &kusrt
, min( length
, sizeof(kusrt
) ));
992 if (ret_len
) *ret_len
= min( length
, sizeof(kusrt
) );
996 case ThreadDescriptorTableEntry
:
999 THREAD_DESCRIPTOR_INFORMATION
* tdi
= data
;
1000 if (length
< sizeof(*tdi
))
1001 status
= STATUS_INFO_LENGTH_MISMATCH
;
1002 else if (!(tdi
->Selector
& 4)) /* GDT selector */
1004 unsigned sel
= tdi
->Selector
& ~3; /* ignore RPL */
1005 status
= STATUS_SUCCESS
;
1006 if (!sel
) /* null selector */
1007 memset( &tdi
->Entry
, 0, sizeof(tdi
->Entry
) );
1010 tdi
->Entry
.BaseLow
= 0;
1011 tdi
->Entry
.HighWord
.Bits
.BaseMid
= 0;
1012 tdi
->Entry
.HighWord
.Bits
.BaseHi
= 0;
1013 tdi
->Entry
.LimitLow
= 0xffff;
1014 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0xf;
1015 tdi
->Entry
.HighWord
.Bits
.Dpl
= 3;
1016 tdi
->Entry
.HighWord
.Bits
.Sys
= 0;
1017 tdi
->Entry
.HighWord
.Bits
.Pres
= 1;
1018 tdi
->Entry
.HighWord
.Bits
.Granularity
= 1;
1019 tdi
->Entry
.HighWord
.Bits
.Default_Big
= 1;
1020 tdi
->Entry
.HighWord
.Bits
.Type
= 0x12;
1021 /* it has to be one of the system GDT selectors */
1022 if (sel
!= (wine_get_ds() & ~3) && sel
!= (wine_get_ss() & ~3))
1024 if (sel
== (wine_get_cs() & ~3))
1025 tdi
->Entry
.HighWord
.Bits
.Type
|= 8; /* code segment */
1026 else status
= STATUS_ACCESS_DENIED
;
1032 SERVER_START_REQ( get_selector_entry
)
1034 req
->handle
= wine_server_obj_handle( handle
);
1035 req
->entry
= tdi
->Selector
>> 3;
1036 status
= wine_server_call( req
);
1039 if (!(reply
->flags
& WINE_LDT_FLAGS_ALLOCATED
))
1040 status
= STATUS_ACCESS_VIOLATION
;
1043 wine_ldt_set_base ( &tdi
->Entry
, (void *)reply
->base
);
1044 wine_ldt_set_limit( &tdi
->Entry
, reply
->limit
);
1045 wine_ldt_set_flags( &tdi
->Entry
, reply
->flags
);
1051 if (status
== STATUS_SUCCESS
&& ret_len
)
1052 /* yes, that's a bit strange, but it's the way it is */
1053 *ret_len
= sizeof(LDT_ENTRY
);
1055 status
= STATUS_NOT_IMPLEMENTED
;
1059 case ThreadAmILastThread
:
1061 SERVER_START_REQ(get_thread_info
)
1063 req
->handle
= wine_server_obj_handle( handle
);
1065 status
= wine_server_call( req
);
1066 if (status
== STATUS_SUCCESS
)
1068 BOOLEAN last
= reply
->last
;
1069 if (data
) memcpy( data
, &last
, min( length
, sizeof(last
) ));
1070 if (ret_len
) *ret_len
= min( length
, sizeof(last
) );
1076 case ThreadPriority
:
1077 case ThreadBasePriority
:
1078 case ThreadImpersonationToken
:
1079 case ThreadEnableAlignmentFaultFixup
:
1080 case ThreadEventPair_Reusable
:
1081 case ThreadQuerySetWin32StartAddress
:
1082 case ThreadZeroTlsCell
:
1083 case ThreadPerformanceCount
:
1084 case ThreadIdealProcessor
:
1085 case ThreadPriorityBoost
:
1086 case ThreadSetTlsArrayAddress
:
1087 case ThreadIsIoPending
:
1089 FIXME( "info class %d not supported yet\n", class );
1090 return STATUS_NOT_IMPLEMENTED
;
1095 /******************************************************************************
1096 * NtSetInformationThread (NTDLL.@)
1097 * ZwSetInformationThread (NTDLL.@)
1099 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1100 LPCVOID data
, ULONG length
)
1105 case ThreadZeroTlsCell
:
1106 if (handle
== GetCurrentThread())
1111 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1112 index
= *(const DWORD
*)data
;
1113 if (index
< TLS_MINIMUM_AVAILABLE
)
1115 RtlAcquirePebLock();
1116 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1118 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1119 teb
->TlsSlots
[index
] = 0;
1121 RtlReleasePebLock();
1125 index
-= TLS_MINIMUM_AVAILABLE
;
1126 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
1127 return STATUS_INVALID_PARAMETER
;
1128 RtlAcquirePebLock();
1129 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1131 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1132 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
1134 RtlReleasePebLock();
1136 return STATUS_SUCCESS
;
1138 FIXME( "ZeroTlsCell not supported on other threads\n" );
1139 return STATUS_NOT_IMPLEMENTED
;
1141 case ThreadImpersonationToken
:
1143 const HANDLE
*phToken
= data
;
1144 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
1145 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
1146 SERVER_START_REQ( set_thread_info
)
1148 req
->handle
= wine_server_obj_handle( handle
);
1149 req
->token
= wine_server_obj_handle( *phToken
);
1150 req
->mask
= SET_THREAD_INFO_TOKEN
;
1151 status
= wine_server_call( req
);
1156 case ThreadBasePriority
:
1158 const DWORD
*pprio
= data
;
1159 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1160 SERVER_START_REQ( set_thread_info
)
1162 req
->handle
= wine_server_obj_handle( handle
);
1163 req
->priority
= *pprio
;
1164 req
->mask
= SET_THREAD_INFO_PRIORITY
;
1165 status
= wine_server_call( req
);
1170 case ThreadAffinityMask
:
1172 const ULONG_PTR affinity_mask
= ((ULONG_PTR
)1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1175 if (length
!= sizeof(ULONG_PTR
)) return STATUS_INVALID_PARAMETER
;
1176 req_aff
= *(const ULONG_PTR
*)data
;
1177 if ((ULONG
)req_aff
== ~0u) req_aff
= affinity_mask
;
1178 else if (req_aff
& ~affinity_mask
) return STATUS_INVALID_PARAMETER
;
1179 else if (!req_aff
) return STATUS_INVALID_PARAMETER
;
1180 SERVER_START_REQ( set_thread_info
)
1182 req
->handle
= wine_server_obj_handle( handle
);
1183 req
->affinity
= req_aff
;
1184 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1185 status
= wine_server_call( req
);
1190 case ThreadHideFromDebugger
:
1191 /* pretend the call succeeded to satisfy some code protectors */
1192 return STATUS_SUCCESS
;
1194 case ThreadBasicInformation
:
1196 case ThreadPriority
:
1197 case ThreadDescriptorTableEntry
:
1198 case ThreadEnableAlignmentFaultFixup
:
1199 case ThreadEventPair_Reusable
:
1200 case ThreadQuerySetWin32StartAddress
:
1201 case ThreadPerformanceCount
:
1202 case ThreadAmILastThread
:
1203 case ThreadIdealProcessor
:
1204 case ThreadPriorityBoost
:
1205 case ThreadSetTlsArrayAddress
:
1206 case ThreadIsIoPending
:
1208 FIXME( "info class %d not supported yet\n", class );
1209 return STATUS_NOT_IMPLEMENTED
;
1213 /******************************************************************************
1214 * NtGetCurrentProcessorNumber (NTDLL.@)
1216 * Return the processor, on which the thread is running
1219 ULONG WINAPI
NtGetCurrentProcessorNumber(void)
1223 #if defined(__linux__) && defined(__NR_getcpu)
1224 int res
= syscall(__NR_getcpu
, &processor
, NULL
, NULL
);
1225 if (res
!= -1) return processor
;
1228 if (NtCurrentTeb()->Peb
->NumberOfProcessors
> 1)
1230 ULONG_PTR thread_mask
, processor_mask
;
1233 status
= NtQueryInformationThread(GetCurrentThread(), ThreadAffinityMask
,
1234 &thread_mask
, sizeof(thread_mask
), NULL
);
1235 if (status
== STATUS_SUCCESS
)
1237 for (processor
= 0; processor
< NtCurrentTeb()->Peb
->NumberOfProcessors
; processor
++)
1239 processor_mask
= (1 << processor
);
1240 if (thread_mask
& processor_mask
)
1242 if (thread_mask
!= processor_mask
)
1243 FIXME("need multicore support (%d processors)\n",
1244 NtCurrentTeb()->Peb
->NumberOfProcessors
);
1251 /* fallback to the first processor */