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"
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
29 #ifdef HAVE_SYS_TIMES_H
30 #include <sys/times.h>
33 #define NONAMELESSUNION
35 #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"
43 #include "wine/exception.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
46 WINE_DECLARE_DEBUG_CHANNEL(relay
);
48 /* info passed to a starting thread */
51 struct wine_pthread_thread_info pthread_info
;
52 PRTL_THREAD_START_ROUTINE entry_point
;
56 static PEB_LDR_DATA ldr
;
57 static RTL_USER_PROCESS_PARAMETERS params
; /* default parameters if no parent */
58 static WCHAR current_dir
[MAX_NT_PATH_LENGTH
];
59 static RTL_BITMAP tls_bitmap
;
60 static RTL_BITMAP tls_expansion_bitmap
;
61 static LIST_ENTRY tls_links
;
62 static size_t sigstack_total_size
;
63 static ULONG sigstack_zero_bits
;
65 struct wine_pthread_functions pthread_functions
= { NULL
};
68 static RTL_CRITICAL_SECTION ldt_section
;
69 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
72 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
73 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
75 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
76 static sigset_t ldt_sigset
;
78 /***********************************************************************
79 * locking for LDT routines
81 static void ldt_lock(void)
85 pthread_functions
.sigprocmask( SIG_BLOCK
, &server_block_set
, &sigset
);
86 RtlEnterCriticalSection( &ldt_section
);
87 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
90 static void ldt_unlock(void)
92 if (ldt_section
.RecursionCount
== 1)
94 sigset_t sigset
= ldt_sigset
;
95 RtlLeaveCriticalSection( &ldt_section
);
96 pthread_functions
.sigprocmask( SIG_SETMASK
, &sigset
, NULL
);
98 else RtlLeaveCriticalSection( &ldt_section
);
102 /***********************************************************************
105 static inline NTSTATUS
init_teb( TEB
*teb
)
107 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
108 struct ntdll_thread_regs
*thread_regs
= (struct ntdll_thread_regs
*)teb
->SpareBytes1
;
110 teb
->Tib
.ExceptionList
= (void *)~0UL;
111 teb
->Tib
.StackBase
= (void *)~0UL;
112 teb
->Tib
.Self
= &teb
->Tib
;
113 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
114 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
116 if (!(thread_regs
->fs
= wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS
;
117 thread_data
->request_fd
= -1;
118 thread_data
->reply_fd
= -1;
119 thread_data
->wait_fd
[0] = -1;
120 thread_data
->wait_fd
[1] = -1;
122 return STATUS_SUCCESS
;
126 /***********************************************************************
129 * Make sure the unicode string doesn't point beyond the end pointer
131 static inline void fix_unicode_string( UNICODE_STRING
*str
, char *end_ptr
)
133 if ((char *)str
->Buffer
>= end_ptr
)
135 str
->Length
= str
->MaximumLength
= 0;
139 if ((char *)str
->Buffer
+ str
->MaximumLength
> end_ptr
)
141 str
->MaximumLength
= (end_ptr
- (char *)str
->Buffer
) & ~(sizeof(WCHAR
) - 1);
143 if (str
->Length
>= str
->MaximumLength
)
145 if (str
->MaximumLength
>= sizeof(WCHAR
))
146 str
->Length
= str
->MaximumLength
- sizeof(WCHAR
);
148 str
->Length
= str
->MaximumLength
= 0;
153 /***********************************************************************
154 * init_user_process_params
156 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
158 static NTSTATUS
init_user_process_params( SIZE_T info_size
, HANDLE
*exe_file
)
163 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
165 status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &info_size
,
166 MEM_COMMIT
, PAGE_READWRITE
);
167 if (status
!= STATUS_SUCCESS
) return status
;
169 params
->AllocationSize
= info_size
;
170 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
172 SERVER_START_REQ( get_startup_info
)
174 wine_server_set_reply( req
, params
, info_size
);
175 if (!(status
= wine_server_call( req
)))
177 info_size
= wine_server_reply_size( reply
);
178 *exe_file
= reply
->exe_file
;
179 params
->hStdInput
= reply
->hstdin
;
180 params
->hStdOutput
= reply
->hstdout
;
181 params
->hStdError
= reply
->hstderr
;
185 if (status
!= STATUS_SUCCESS
) return status
;
187 if (params
->Size
> info_size
) params
->Size
= info_size
;
189 /* make sure the strings are valid */
190 fix_unicode_string( ¶ms
->CurrentDirectory
.DosPath
, (char *)info_size
);
191 fix_unicode_string( ¶ms
->DllPath
, (char *)info_size
);
192 fix_unicode_string( ¶ms
->ImagePathName
, (char *)info_size
);
193 fix_unicode_string( ¶ms
->CommandLine
, (char *)info_size
);
194 fix_unicode_string( ¶ms
->WindowTitle
, (char *)info_size
);
195 fix_unicode_string( ¶ms
->Desktop
, (char *)info_size
);
196 fix_unicode_string( ¶ms
->ShellInfo
, (char *)info_size
);
197 fix_unicode_string( ¶ms
->RuntimeInfo
, (char *)info_size
);
199 /* environment needs to be a separate memory block */
200 env_size
= info_size
- params
->Size
;
201 if (!env_size
) env_size
= 1;
203 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &env_size
,
204 MEM_COMMIT
, PAGE_READWRITE
);
205 if (status
!= STATUS_SUCCESS
) return status
;
206 memcpy( ptr
, (char *)params
+ params
->Size
, info_size
- params
->Size
);
207 params
->Environment
= ptr
;
209 RtlNormalizeProcessParams( params
);
214 /***********************************************************************
217 * Setup the initial thread.
219 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
221 HANDLE
thread_init(void)
226 SIZE_T size
, info_size
;
228 struct ntdll_thread_data
*thread_data
;
229 struct ntdll_thread_regs
*thread_regs
;
230 struct wine_pthread_thread_info thread_info
;
231 static struct debug_info debug_info
; /* debug info for initial thread */
235 /* reserve space for shared user data */
237 addr
= (void *)0x7ffe0000;
239 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
, MEM_RESERVE
, PAGE_READONLY
);
241 /* allocate and initialize the PEB */
245 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 1, &size
,
246 MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
249 peb
->NumberOfProcessors
= 1;
250 peb
->ProcessParameters
= ¶ms
;
251 peb
->TlsBitmap
= &tls_bitmap
;
252 peb
->TlsExpansionBitmap
= &tls_expansion_bitmap
;
254 params
.CurrentDirectory
.DosPath
.Buffer
= current_dir
;
255 params
.CurrentDirectory
.DosPath
.MaximumLength
= sizeof(current_dir
);
256 params
.wShowWindow
= 1; /* SW_SHOWNORMAL */
257 RtlInitializeBitMap( &tls_bitmap
, peb
->TlsBitmapBits
, sizeof(peb
->TlsBitmapBits
) * 8 );
258 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
->TlsExpansionBitmapBits
,
259 sizeof(peb
->TlsExpansionBitmapBits
) * 8 );
260 InitializeListHead( &ldr
.InLoadOrderModuleList
);
261 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
262 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
263 InitializeListHead( &tls_links
);
265 /* allocate and initialize the initial TEB */
267 sigstack_total_size
= get_signal_stack_total_size();
268 while (1 << sigstack_zero_bits
< sigstack_total_size
) sigstack_zero_bits
++;
269 assert( 1 << sigstack_zero_bits
== sigstack_total_size
); /* must be a power of 2 */
270 assert( sigstack_total_size
>= sizeof(TEB
) + sizeof(struct startup_info
) );
271 thread_info
.teb_size
= sigstack_total_size
;
274 size
= sigstack_total_size
;
275 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
276 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
279 thread_info
.teb_size
= size
;
281 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
282 thread_regs
= (struct ntdll_thread_regs
*)teb
->SpareBytes1
;
283 thread_data
->debug_info
= &debug_info
;
284 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
286 thread_info
.stack_base
= NULL
;
287 thread_info
.stack_size
= 0;
288 thread_info
.teb_base
= teb
;
289 thread_info
.teb_sel
= thread_regs
->fs
;
290 wine_pthread_get_functions( &pthread_functions
, sizeof(pthread_functions
) );
291 pthread_functions
.init_current_teb( &thread_info
);
292 pthread_functions
.init_thread( &thread_info
);
293 virtual_init_threading();
295 debug_info
.str_pos
= debug_info
.strings
;
296 debug_info
.out_pos
= debug_info
.output
;
299 /* setup the server connection */
300 server_init_process();
301 info_size
= server_init_thread( thread_info
.pid
, thread_info
.tid
, NULL
);
303 /* create the process heap */
304 if (!(peb
->ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
306 MESSAGE( "wine: failed to create the process heap\n" );
310 /* allocate user parameters */
313 init_user_process_params( info_size
, &exe_file
);
317 /* This is wine specific: we have no parent (we're started from unix)
318 * so, create a simple console with bare handles to unix stdio
320 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdInput
);
321 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
322 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
325 /* initialize LDT locking */
326 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
331 typedef LONG (WINAPI
*PUNHANDLED_EXCEPTION_FILTER
)(PEXCEPTION_POINTERS
);
332 static PUNHANDLED_EXCEPTION_FILTER
get_unhandled_exception_filter(void)
334 static PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter
;
335 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
336 UNICODE_STRING module_name
;
337 ANSI_STRING func_name
;
338 HMODULE kernel32_handle
;
340 if (unhandled_exception_filter
) return unhandled_exception_filter
;
342 RtlInitUnicodeString(&module_name
, kernel32W
);
343 RtlInitAnsiString( &func_name
, "UnhandledExceptionFilter" );
345 if (LdrGetDllHandle( 0, 0, &module_name
, &kernel32_handle
) == STATUS_SUCCESS
)
346 LdrGetProcedureAddress( kernel32_handle
, &func_name
, 0,
347 (void **)&unhandled_exception_filter
);
349 return unhandled_exception_filter
;
353 /* wrapper for apps that don't declare the thread function correctly */
354 extern DWORD
call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry
, void *arg
);
355 __ASM_GLOBAL_FUNC(call_thread_entry_point
,
360 "movl 8(%ebp),%eax\n\t"
365 static inline DWORD
call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry
, void *arg
)
367 LPTHREAD_START_ROUTINE func
= (LPTHREAD_START_ROUTINE
)entry
;
372 /***********************************************************************
375 * Hack to make things compatible with the thread procedures used by kernel32.CreateThread.
377 static void DECLSPEC_NORETURN
call_thread_func( PRTL_THREAD_START_ROUTINE rtl_func
, void *arg
)
382 MODULE_DllThreadAttach( NULL
);
385 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), rtl_func
, arg
);
387 exit_code
= call_thread_entry_point( rtl_func
, arg
);
389 /* send the exit code to the server */
390 SERVER_START_REQ( terminate_thread
)
392 req
->handle
= GetCurrentThread();
393 req
->exit_code
= exit_code
;
394 wine_server_call( req
);
401 LdrShutdownProcess();
407 server_exit_thread( exit_code
);
412 /***********************************************************************
415 * Startup routine for a newly created thread.
417 static void start_thread( struct wine_pthread_thread_info
*info
)
419 TEB
*teb
= info
->teb_base
;
420 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
421 struct startup_info
*startup_info
= (struct startup_info
*)info
;
422 PRTL_THREAD_START_ROUTINE func
= startup_info
->entry_point
;
423 void *arg
= startup_info
->entry_arg
;
424 struct debug_info debug_info
;
425 SIZE_T size
, page_size
= getpagesize();
427 debug_info
.str_pos
= debug_info
.strings
;
428 debug_info
.out_pos
= debug_info
.output
;
429 thread_data
->debug_info
= &debug_info
;
431 pthread_functions
.init_current_teb( info
);
433 server_init_thread( info
->pid
, info
->tid
, func
);
434 pthread_functions
.init_thread( info
);
436 /* allocate a memory view for the stack */
437 size
= info
->stack_size
;
438 teb
->DeallocationStack
= info
->stack_base
;
439 NtAllocateVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, 0,
440 &size
, MEM_SYSTEM
, PAGE_READWRITE
);
441 /* limit is lower than base since the stack grows down */
442 teb
->Tib
.StackBase
= (char *)info
->stack_base
+ info
->stack_size
;
443 teb
->Tib
.StackLimit
= (char *)info
->stack_base
+ page_size
;
445 /* setup the guard page */
447 NtProtectVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, &size
, PAGE_NOACCESS
, NULL
);
449 pthread_functions
.sigprocmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
452 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
455 /* NOTE: Windows does not have an exception handler around the call to
456 * the thread attach. We do for ease of debugging */
457 if (get_unhandled_exception_filter())
461 call_thread_func( func
, arg
);
463 __EXCEPT(get_unhandled_exception_filter())
465 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
470 call_thread_func( func
, arg
);
474 /***********************************************************************
475 * RtlCreateUserThread (NTDLL.@)
477 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
478 BOOLEAN suspended
, PVOID stack_addr
,
479 SIZE_T stack_reserve
, SIZE_T stack_commit
,
480 PRTL_THREAD_START_ROUTINE start
, void *param
,
481 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
484 struct ntdll_thread_data
*thread_data
;
485 struct ntdll_thread_regs
*thread_regs
= NULL
;
486 struct startup_info
*info
= NULL
;
493 SIZE_T size
, page_size
= getpagesize();
495 if (process
!= NtCurrentProcess())
500 call
.create_thread
.type
= APC_CREATE_THREAD
;
501 call
.create_thread
.func
= start
;
502 call
.create_thread
.arg
= param
;
503 call
.create_thread
.reserve
= stack_reserve
;
504 call
.create_thread
.commit
= stack_commit
;
505 call
.create_thread
.suspend
= suspended
;
506 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
507 if (status
!= STATUS_SUCCESS
) return status
;
509 if (result
.create_thread
.status
== STATUS_SUCCESS
)
511 if (id
) id
->UniqueThread
= (HANDLE
)result
.create_thread
.tid
;
512 if (handle_ptr
) *handle_ptr
= result
.create_thread
.handle
;
513 else NtClose( result
.create_thread
.handle
);
515 return result
.create_thread
.status
;
518 if (pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
519 fcntl( request_pipe
[1], F_SETFD
, 1 ); /* set close on exec flag */
520 wine_server_send_fd( request_pipe
[0] );
522 SERVER_START_REQ( new_thread
)
524 req
->access
= THREAD_ALL_ACCESS
;
525 req
->attributes
= 0; /* FIXME */
526 req
->suspend
= suspended
;
527 req
->request_fd
= request_pipe
[0];
528 if (!(status
= wine_server_call( req
)))
530 handle
= reply
->handle
;
533 close( request_pipe
[0] );
539 close( request_pipe
[1] );
543 pthread_functions
.sigprocmask( SIG_BLOCK
, &server_block_set
, &sigset
);
546 size
= sigstack_total_size
;
547 if ((status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
548 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
551 teb
->Peb
= NtCurrentTeb()->Peb
;
552 info
= (struct startup_info
*)(teb
+ 1);
553 info
->pthread_info
.teb_size
= size
;
554 if ((status
= init_teb( teb
))) goto error
;
556 teb
->ClientId
.UniqueProcess
= (HANDLE
)GetCurrentProcessId();
557 teb
->ClientId
.UniqueThread
= (HANDLE
)tid
;
559 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
560 thread_regs
= (struct ntdll_thread_regs
*)teb
->SpareBytes1
;
561 thread_data
->request_fd
= request_pipe
[1];
563 info
->pthread_info
.teb_base
= teb
;
564 info
->pthread_info
.teb_sel
= thread_regs
->fs
;
566 /* inherit debug registers from parent thread */
567 thread_regs
->dr0
= ntdll_get_thread_regs()->dr0
;
568 thread_regs
->dr1
= ntdll_get_thread_regs()->dr1
;
569 thread_regs
->dr2
= ntdll_get_thread_regs()->dr2
;
570 thread_regs
->dr3
= ntdll_get_thread_regs()->dr3
;
571 thread_regs
->dr6
= ntdll_get_thread_regs()->dr6
;
572 thread_regs
->dr7
= ntdll_get_thread_regs()->dr7
;
574 if (!stack_reserve
|| !stack_commit
)
576 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
577 if (!stack_reserve
) stack_reserve
= nt
->OptionalHeader
.SizeOfStackReserve
;
578 if (!stack_commit
) stack_commit
= nt
->OptionalHeader
.SizeOfStackCommit
;
580 if (stack_reserve
< stack_commit
) stack_reserve
= stack_commit
;
581 stack_reserve
+= page_size
; /* for the guard page */
582 stack_reserve
= (stack_reserve
+ 0xffff) & ~0xffff; /* round to 64K boundary */
583 if (stack_reserve
< 1024 * 1024) stack_reserve
= 1024 * 1024; /* Xlib needs a large stack */
585 info
->pthread_info
.stack_base
= NULL
;
586 info
->pthread_info
.stack_size
= stack_reserve
;
587 info
->pthread_info
.entry
= start_thread
;
588 info
->entry_point
= start
;
589 info
->entry_arg
= param
;
591 if (pthread_functions
.create_thread( &info
->pthread_info
) == -1)
593 status
= STATUS_NO_MEMORY
;
596 pthread_functions
.sigprocmask( SIG_SETMASK
, &sigset
, NULL
);
598 if (id
) id
->UniqueThread
= (HANDLE
)tid
;
599 if (handle_ptr
) *handle_ptr
= handle
;
600 else NtClose( handle
);
602 return STATUS_SUCCESS
;
605 if (thread_regs
) wine_ldt_free_fs( thread_regs
->fs
);
609 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
611 if (handle
) NtClose( handle
);
612 pthread_functions
.sigprocmask( SIG_SETMASK
, &sigset
, NULL
);
613 close( request_pipe
[1] );
618 /***********************************************************************
619 * RtlExitUserThread (NTDLL.@)
621 void WINAPI
RtlExitUserThread( ULONG status
)
624 server_exit_thread( status
);
628 /***********************************************************************
629 * NtOpenThread (NTDLL.@)
630 * ZwOpenThread (NTDLL.@)
632 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
633 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
637 SERVER_START_REQ( open_thread
)
639 req
->tid
= (thread_id_t
)id
->UniqueThread
;
640 req
->access
= access
;
641 req
->attributes
= attr
? attr
->Attributes
: 0;
642 ret
= wine_server_call( req
);
643 *handle
= reply
->handle
;
650 /******************************************************************************
651 * NtSuspendThread (NTDLL.@)
652 * ZwSuspendThread (NTDLL.@)
654 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
658 SERVER_START_REQ( suspend_thread
)
660 req
->handle
= handle
;
661 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
668 /******************************************************************************
669 * NtResumeThread (NTDLL.@)
670 * ZwResumeThread (NTDLL.@)
672 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
676 SERVER_START_REQ( resume_thread
)
678 req
->handle
= handle
;
679 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
686 /******************************************************************************
687 * NtAlertResumeThread (NTDLL.@)
688 * ZwAlertResumeThread (NTDLL.@)
690 NTSTATUS WINAPI
NtAlertResumeThread( HANDLE handle
, PULONG count
)
692 FIXME( "stub: should alert thread %p\n", handle
);
693 return NtResumeThread( handle
, count
);
697 /******************************************************************************
698 * NtAlertThread (NTDLL.@)
699 * ZwAlertThread (NTDLL.@)
701 NTSTATUS WINAPI
NtAlertThread( HANDLE handle
)
703 FIXME( "stub: %p\n", handle
);
704 return STATUS_NOT_IMPLEMENTED
;
708 /******************************************************************************
709 * NtTerminateThread (NTDLL.@)
710 * ZwTerminateThread (NTDLL.@)
712 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
717 SERVER_START_REQ( terminate_thread
)
719 req
->handle
= handle
;
720 req
->exit_code
= exit_code
;
721 ret
= wine_server_call( req
);
722 self
= !ret
&& reply
->self
;
729 if (last
) exit( exit_code
);
730 else server_abort_thread( exit_code
);
736 /******************************************************************************
737 * NtQueueApcThread (NTDLL.@)
739 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
740 ULONG_PTR arg2
, ULONG_PTR arg3
)
743 SERVER_START_REQ( queue_apc
)
745 req
->thread
= handle
;
748 req
->call
.type
= APC_USER
;
749 req
->call
.user
.func
= func
;
750 req
->call
.user
.args
[0] = arg1
;
751 req
->call
.user
.args
[1] = arg2
;
752 req
->call
.user
.args
[2] = arg3
;
754 else req
->call
.type
= APC_NONE
; /* wake up only */
755 ret
= wine_server_call( req
);
762 /***********************************************************************
763 * NtSetContextThread (NTDLL.@)
764 * ZwSetContextThread (NTDLL.@)
766 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
773 /* on i386 debug registers always require a server call */
774 self
= (handle
== GetCurrentThread());
775 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
777 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
778 self
= (regs
->dr0
== context
->Dr0
&& regs
->dr1
== context
->Dr1
&&
779 regs
->dr2
== context
->Dr2
&& regs
->dr3
== context
->Dr3
&&
780 regs
->dr6
== context
->Dr6
&& regs
->dr7
== context
->Dr7
);
786 SERVER_START_REQ( set_thread_context
)
788 req
->handle
= handle
;
789 req
->flags
= context
->ContextFlags
;
791 wine_server_add_data( req
, context
, sizeof(*context
) );
792 ret
= wine_server_call( req
);
797 if (ret
== STATUS_PENDING
)
799 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
801 for (i
= 0; i
< 100; i
++)
803 SERVER_START_REQ( set_thread_context
)
805 req
->handle
= handle
;
806 req
->flags
= context
->ContextFlags
;
808 wine_server_add_data( req
, context
, sizeof(*context
) );
809 ret
= wine_server_call( req
);
812 if (ret
!= STATUS_PENDING
) break;
815 NtResumeThread( handle
, &dummy
);
817 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
823 if (self
) set_cpu_context( context
);
824 return STATUS_SUCCESS
;
828 /* copy a context structure according to the flags */
829 static inline void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
832 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
833 if (flags
& CONTEXT_INTEGER
)
842 if (flags
& CONTEXT_CONTROL
)
847 to
->SegCs
= from
->SegCs
;
848 to
->SegSs
= from
->SegSs
;
849 to
->EFlags
= from
->EFlags
;
851 if (flags
& CONTEXT_SEGMENTS
)
853 to
->SegDs
= from
->SegDs
;
854 to
->SegEs
= from
->SegEs
;
855 to
->SegFs
= from
->SegFs
;
856 to
->SegGs
= from
->SegGs
;
858 if (flags
& CONTEXT_DEBUG_REGISTERS
)
867 if (flags
& CONTEXT_FLOATING_POINT
)
869 to
->FloatSave
= from
->FloatSave
;
871 #elif defined(__x86_64__)
872 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
873 if (flags
& CONTEXT_CONTROL
)
878 to
->SegCs
= from
->SegCs
;
879 to
->SegSs
= from
->SegSs
;
880 to
->EFlags
= from
->EFlags
;
881 to
->MxCsr
= from
->MxCsr
;
883 if (flags
& CONTEXT_INTEGER
)
900 if (flags
& CONTEXT_SEGMENTS
)
902 to
->SegDs
= from
->SegDs
;
903 to
->SegEs
= from
->SegEs
;
904 to
->SegFs
= from
->SegFs
;
905 to
->SegGs
= from
->SegGs
;
907 if (flags
& CONTEXT_FLOATING_POINT
)
909 to
->u
.FltSave
= from
->u
.FltSave
;
911 if (flags
& CONTEXT_DEBUG_REGISTERS
)
920 #elif defined(__sparc__)
921 flags
&= ~CONTEXT_SPARC
; /* get rid of CPU id */
922 if (flags
& CONTEXT_CONTROL
)
931 if (flags
& CONTEXT_INTEGER
)
966 if (flags
& CONTEXT_FLOATING_POINT
)
970 #elif defined(__powerpc__)
972 if (flags
& CONTEXT_CONTROL
)
978 if (flags
& CONTEXT_INTEGER
)
980 to
->Gpr0
= from
->Gpr0
;
981 to
->Gpr1
= from
->Gpr1
;
982 to
->Gpr2
= from
->Gpr2
;
983 to
->Gpr3
= from
->Gpr3
;
984 to
->Gpr4
= from
->Gpr4
;
985 to
->Gpr5
= from
->Gpr5
;
986 to
->Gpr6
= from
->Gpr6
;
987 to
->Gpr7
= from
->Gpr7
;
988 to
->Gpr8
= from
->Gpr8
;
989 to
->Gpr9
= from
->Gpr9
;
990 to
->Gpr10
= from
->Gpr10
;
991 to
->Gpr11
= from
->Gpr11
;
992 to
->Gpr12
= from
->Gpr12
;
993 to
->Gpr13
= from
->Gpr13
;
994 to
->Gpr14
= from
->Gpr14
;
995 to
->Gpr15
= from
->Gpr15
;
996 to
->Gpr16
= from
->Gpr16
;
997 to
->Gpr17
= from
->Gpr17
;
998 to
->Gpr18
= from
->Gpr18
;
999 to
->Gpr19
= from
->Gpr19
;
1000 to
->Gpr20
= from
->Gpr20
;
1001 to
->Gpr21
= from
->Gpr21
;
1002 to
->Gpr22
= from
->Gpr22
;
1003 to
->Gpr23
= from
->Gpr23
;
1004 to
->Gpr24
= from
->Gpr24
;
1005 to
->Gpr25
= from
->Gpr25
;
1006 to
->Gpr26
= from
->Gpr26
;
1007 to
->Gpr27
= from
->Gpr27
;
1008 to
->Gpr28
= from
->Gpr28
;
1009 to
->Gpr29
= from
->Gpr29
;
1010 to
->Gpr30
= from
->Gpr30
;
1011 to
->Gpr31
= from
->Gpr31
;
1012 to
->Xer
= from
->Xer
;
1015 if (flags
& CONTEXT_FLOATING_POINT
)
1017 to
->Fpr0
= from
->Fpr0
;
1018 to
->Fpr1
= from
->Fpr1
;
1019 to
->Fpr2
= from
->Fpr2
;
1020 to
->Fpr3
= from
->Fpr3
;
1021 to
->Fpr4
= from
->Fpr4
;
1022 to
->Fpr5
= from
->Fpr5
;
1023 to
->Fpr6
= from
->Fpr6
;
1024 to
->Fpr7
= from
->Fpr7
;
1025 to
->Fpr8
= from
->Fpr8
;
1026 to
->Fpr9
= from
->Fpr9
;
1027 to
->Fpr10
= from
->Fpr10
;
1028 to
->Fpr11
= from
->Fpr11
;
1029 to
->Fpr12
= from
->Fpr12
;
1030 to
->Fpr13
= from
->Fpr13
;
1031 to
->Fpr14
= from
->Fpr14
;
1032 to
->Fpr15
= from
->Fpr15
;
1033 to
->Fpr16
= from
->Fpr16
;
1034 to
->Fpr17
= from
->Fpr17
;
1035 to
->Fpr18
= from
->Fpr18
;
1036 to
->Fpr19
= from
->Fpr19
;
1037 to
->Fpr20
= from
->Fpr20
;
1038 to
->Fpr21
= from
->Fpr21
;
1039 to
->Fpr22
= from
->Fpr22
;
1040 to
->Fpr23
= from
->Fpr23
;
1041 to
->Fpr24
= from
->Fpr24
;
1042 to
->Fpr25
= from
->Fpr25
;
1043 to
->Fpr26
= from
->Fpr26
;
1044 to
->Fpr27
= from
->Fpr27
;
1045 to
->Fpr28
= from
->Fpr28
;
1046 to
->Fpr29
= from
->Fpr29
;
1047 to
->Fpr30
= from
->Fpr30
;
1048 to
->Fpr31
= from
->Fpr31
;
1049 to
->Fpscr
= from
->Fpscr
;
1052 #error You must implement context copying for your CPU
1057 /***********************************************************************
1058 * NtGetContextThread (NTDLL.@)
1059 * ZwGetContextThread (NTDLL.@)
1061 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
1066 DWORD needed_flags
= context
->ContextFlags
;
1067 BOOL self
= (handle
== GetCurrentThread());
1070 /* on i386 debug registers always require a server call */
1071 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)) self
= FALSE
;
1076 SERVER_START_REQ( get_thread_context
)
1078 req
->handle
= handle
;
1079 req
->flags
= context
->ContextFlags
;
1081 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
1082 ret
= wine_server_call( req
);
1087 if (ret
== STATUS_PENDING
)
1089 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
1091 for (i
= 0; i
< 100; i
++)
1093 SERVER_START_REQ( get_thread_context
)
1095 req
->handle
= handle
;
1096 req
->flags
= context
->ContextFlags
;
1098 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
1099 ret
= wine_server_call( req
);
1102 if (ret
!= STATUS_PENDING
) break;
1105 NtResumeThread( handle
, &dummy
);
1107 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
1109 if (ret
) return ret
;
1110 copy_context( context
, &ctx
, context
->ContextFlags
& ctx
.ContextFlags
);
1111 needed_flags
&= ~ctx
.ContextFlags
;
1118 get_cpu_context( &ctx
);
1119 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
1122 /* update the cached version of the debug registers */
1123 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1125 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
1126 regs
->dr0
= context
->Dr0
;
1127 regs
->dr1
= context
->Dr1
;
1128 regs
->dr2
= context
->Dr2
;
1129 regs
->dr3
= context
->Dr3
;
1130 regs
->dr6
= context
->Dr6
;
1131 regs
->dr7
= context
->Dr7
;
1135 return STATUS_SUCCESS
;
1139 /******************************************************************************
1140 * NtQueryInformationThread (NTDLL.@)
1141 * ZwQueryInformationThread (NTDLL.@)
1143 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1144 void *data
, ULONG length
, ULONG
*ret_len
)
1150 case ThreadBasicInformation
:
1152 THREAD_BASIC_INFORMATION info
;
1154 SERVER_START_REQ( get_thread_info
)
1156 req
->handle
= handle
;
1158 if (!(status
= wine_server_call( req
)))
1160 info
.ExitStatus
= reply
->exit_code
;
1161 info
.TebBaseAddress
= reply
->teb
;
1162 info
.ClientId
.UniqueProcess
= (HANDLE
)reply
->pid
;
1163 info
.ClientId
.UniqueThread
= (HANDLE
)reply
->tid
;
1164 info
.AffinityMask
= reply
->affinity
;
1165 info
.Priority
= reply
->priority
;
1166 info
.BasePriority
= reply
->priority
; /* FIXME */
1170 if (status
== STATUS_SUCCESS
)
1172 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
1173 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
1179 KERNEL_USER_TIMES kusrt
;
1180 /* We need to do a server call to get the creation time or exit time */
1181 /* This works on any thread */
1182 SERVER_START_REQ( get_thread_info
)
1184 req
->handle
= handle
;
1186 status
= wine_server_call( req
);
1187 if (status
== STATUS_SUCCESS
)
1189 NTDLL_from_server_abstime( &kusrt
.CreateTime
, &reply
->creation_time
);
1190 NTDLL_from_server_abstime( &kusrt
.ExitTime
, &reply
->exit_time
);
1194 if (status
== STATUS_SUCCESS
)
1196 /* We call times(2) for kernel time or user time */
1197 /* We can only (portably) do this for the current thread */
1198 if (handle
== GetCurrentThread())
1200 struct tms time_buf
;
1201 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
1204 kusrt
.KernelTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
1205 kusrt
.UserTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
1209 kusrt
.KernelTime
.QuadPart
= 0;
1210 kusrt
.UserTime
.QuadPart
= 0;
1211 FIXME("Cannot get kerneltime or usertime of other threads\n");
1213 if (data
) memcpy( data
, &kusrt
, min( length
, sizeof(kusrt
) ));
1214 if (ret_len
) *ret_len
= min( length
, sizeof(kusrt
) );
1218 case ThreadDescriptorTableEntry
:
1221 THREAD_DESCRIPTOR_INFORMATION
* tdi
= data
;
1222 if (length
< sizeof(*tdi
))
1223 status
= STATUS_INFO_LENGTH_MISMATCH
;
1224 else if (!(tdi
->Selector
& 4)) /* GDT selector */
1226 unsigned sel
= tdi
->Selector
& ~3; /* ignore RPL */
1227 status
= STATUS_SUCCESS
;
1228 if (!sel
) /* null selector */
1229 memset( &tdi
->Entry
, 0, sizeof(tdi
->Entry
) );
1232 tdi
->Entry
.BaseLow
= 0;
1233 tdi
->Entry
.HighWord
.Bits
.BaseMid
= 0;
1234 tdi
->Entry
.HighWord
.Bits
.BaseHi
= 0;
1235 tdi
->Entry
.LimitLow
= 0xffff;
1236 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0xf;
1237 tdi
->Entry
.HighWord
.Bits
.Dpl
= 3;
1238 tdi
->Entry
.HighWord
.Bits
.Sys
= 0;
1239 tdi
->Entry
.HighWord
.Bits
.Pres
= 1;
1240 tdi
->Entry
.HighWord
.Bits
.Granularity
= 1;
1241 tdi
->Entry
.HighWord
.Bits
.Default_Big
= 1;
1242 tdi
->Entry
.HighWord
.Bits
.Type
= 0x12;
1243 /* it has to be one of the system GDT selectors */
1244 if (sel
!= (wine_get_ds() & ~3) && sel
!= (wine_get_ss() & ~3))
1246 if (sel
== (wine_get_cs() & ~3))
1247 tdi
->Entry
.HighWord
.Bits
.Type
|= 8; /* code segment */
1248 else status
= STATUS_ACCESS_DENIED
;
1254 SERVER_START_REQ( get_selector_entry
)
1256 req
->handle
= handle
;
1257 req
->entry
= tdi
->Selector
>> 3;
1258 status
= wine_server_call( req
);
1261 if (!(reply
->flags
& WINE_LDT_FLAGS_ALLOCATED
))
1262 status
= STATUS_ACCESS_VIOLATION
;
1265 wine_ldt_set_base ( &tdi
->Entry
, (void *)reply
->base
);
1266 wine_ldt_set_limit( &tdi
->Entry
, reply
->limit
);
1267 wine_ldt_set_flags( &tdi
->Entry
, reply
->flags
);
1273 if (status
== STATUS_SUCCESS
&& ret_len
)
1274 /* yes, that's a bit strange, but it's the way it is */
1275 *ret_len
= sizeof(LDT_ENTRY
);
1277 status
= STATUS_NOT_IMPLEMENTED
;
1281 case ThreadAmILastThread
:
1283 SERVER_START_REQ(get_thread_info
)
1285 req
->handle
= handle
;
1287 status
= wine_server_call( req
);
1288 if (status
== STATUS_SUCCESS
)
1290 BOOLEAN last
= reply
->last
;
1291 if (data
) memcpy( data
, &last
, min( length
, sizeof(last
) ));
1292 if (ret_len
) *ret_len
= min( length
, sizeof(last
) );
1298 case ThreadPriority
:
1299 case ThreadBasePriority
:
1300 case ThreadAffinityMask
:
1301 case ThreadImpersonationToken
:
1302 case ThreadEnableAlignmentFaultFixup
:
1303 case ThreadEventPair_Reusable
:
1304 case ThreadQuerySetWin32StartAddress
:
1305 case ThreadZeroTlsCell
:
1306 case ThreadPerformanceCount
:
1307 case ThreadIdealProcessor
:
1308 case ThreadPriorityBoost
:
1309 case ThreadSetTlsArrayAddress
:
1310 case ThreadIsIoPending
:
1312 FIXME( "info class %d not supported yet\n", class );
1313 return STATUS_NOT_IMPLEMENTED
;
1318 /******************************************************************************
1319 * NtSetInformationThread (NTDLL.@)
1320 * ZwSetInformationThread (NTDLL.@)
1322 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1323 LPCVOID data
, ULONG length
)
1328 case ThreadZeroTlsCell
:
1329 if (handle
== GetCurrentThread())
1334 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1335 index
= *(const DWORD
*)data
;
1336 if (index
< TLS_MINIMUM_AVAILABLE
)
1338 RtlAcquirePebLock();
1339 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1341 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1342 teb
->TlsSlots
[index
] = 0;
1344 RtlReleasePebLock();
1348 index
-= TLS_MINIMUM_AVAILABLE
;
1349 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
1350 return STATUS_INVALID_PARAMETER
;
1351 RtlAcquirePebLock();
1352 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1354 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1355 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
1357 RtlReleasePebLock();
1359 return STATUS_SUCCESS
;
1361 FIXME( "ZeroTlsCell not supported on other threads\n" );
1362 return STATUS_NOT_IMPLEMENTED
;
1364 case ThreadImpersonationToken
:
1366 const HANDLE
*phToken
= data
;
1367 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
1368 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
1369 SERVER_START_REQ( set_thread_info
)
1371 req
->handle
= handle
;
1372 req
->token
= *phToken
;
1373 req
->mask
= SET_THREAD_INFO_TOKEN
;
1374 status
= wine_server_call( req
);
1379 case ThreadBasePriority
:
1381 const DWORD
*pprio
= data
;
1382 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1383 SERVER_START_REQ( set_thread_info
)
1385 req
->handle
= handle
;
1386 req
->priority
= *pprio
;
1387 req
->mask
= SET_THREAD_INFO_PRIORITY
;
1388 status
= wine_server_call( req
);
1393 case ThreadAffinityMask
:
1395 const DWORD
*paff
= data
;
1396 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1397 SERVER_START_REQ( set_thread_info
)
1399 req
->handle
= handle
;
1400 req
->affinity
= *paff
;
1401 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1402 status
= wine_server_call( req
);
1407 case ThreadBasicInformation
:
1409 case ThreadPriority
:
1410 case ThreadDescriptorTableEntry
:
1411 case ThreadEnableAlignmentFaultFixup
:
1412 case ThreadEventPair_Reusable
:
1413 case ThreadQuerySetWin32StartAddress
:
1414 case ThreadPerformanceCount
:
1415 case ThreadAmILastThread
:
1416 case ThreadIdealProcessor
:
1417 case ThreadPriorityBoost
:
1418 case ThreadSetTlsArrayAddress
:
1419 case ThreadIsIoPending
:
1421 FIXME( "info class %d not supported yet\n", class );
1422 return STATUS_NOT_IMPLEMENTED
;
1427 /**********************************************************************
1428 * NtCurrentTeb (NTDLL.@)
1430 #if defined(__i386__) && defined(__GNUC__)
1432 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
1434 #elif defined(__i386__) && defined(_MSC_VER)
1436 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
1440 /**********************************************************************/
1442 TEB
* WINAPI
NtCurrentTeb(void)
1444 return pthread_functions
.get_current_teb();
1447 #endif /* __i386__ */