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"
44 #include "wine/exception.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
47 WINE_DECLARE_DEBUG_CHANNEL(relay
);
49 struct _KUSER_SHARED_DATA
*user_shared_data
= NULL
;
51 PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter
= NULL
;
53 /* info passed to a starting thread */
56 struct wine_pthread_thread_info pthread_info
;
57 PRTL_THREAD_START_ROUTINE entry_point
;
61 static PEB_LDR_DATA ldr
;
62 static RTL_USER_PROCESS_PARAMETERS params
; /* default parameters if no parent */
63 static WCHAR current_dir
[MAX_NT_PATH_LENGTH
];
64 static RTL_BITMAP tls_bitmap
;
65 static RTL_BITMAP tls_expansion_bitmap
;
66 static LIST_ENTRY tls_links
;
67 static size_t sigstack_total_size
;
68 static ULONG sigstack_zero_bits
;
70 struct wine_pthread_functions pthread_functions
= { NULL
};
73 static RTL_CRITICAL_SECTION ldt_section
;
74 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
77 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
78 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
80 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
81 static sigset_t ldt_sigset
;
83 /***********************************************************************
84 * locking for LDT routines
86 static void ldt_lock(void)
90 pthread_functions
.sigprocmask( SIG_BLOCK
, &server_block_set
, &sigset
);
91 RtlEnterCriticalSection( &ldt_section
);
92 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
95 static void ldt_unlock(void)
97 if (ldt_section
.RecursionCount
== 1)
99 sigset_t sigset
= ldt_sigset
;
100 RtlLeaveCriticalSection( &ldt_section
);
101 pthread_functions
.sigprocmask( SIG_SETMASK
, &sigset
, NULL
);
103 else RtlLeaveCriticalSection( &ldt_section
);
107 /***********************************************************************
110 static inline NTSTATUS
init_teb( TEB
*teb
)
112 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
114 teb
->Tib
.ExceptionList
= (void *)~0UL;
115 teb
->Tib
.StackBase
= (void *)~0UL;
116 teb
->Tib
.Self
= &teb
->Tib
;
117 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
118 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
120 if (!(thread_data
->fs
= wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS
;
121 thread_data
->request_fd
= -1;
122 thread_data
->reply_fd
= -1;
123 thread_data
->wait_fd
[0] = -1;
124 thread_data
->wait_fd
[1] = -1;
126 return STATUS_SUCCESS
;
130 /***********************************************************************
133 * Make sure the unicode string doesn't point beyond the end pointer
135 static inline void fix_unicode_string( UNICODE_STRING
*str
, const char *end_ptr
)
137 if ((char *)str
->Buffer
>= end_ptr
)
139 str
->Length
= str
->MaximumLength
= 0;
143 if ((char *)str
->Buffer
+ str
->MaximumLength
> end_ptr
)
145 str
->MaximumLength
= (end_ptr
- (char *)str
->Buffer
) & ~(sizeof(WCHAR
) - 1);
147 if (str
->Length
>= str
->MaximumLength
)
149 if (str
->MaximumLength
>= sizeof(WCHAR
))
150 str
->Length
= str
->MaximumLength
- sizeof(WCHAR
);
152 str
->Length
= str
->MaximumLength
= 0;
157 /***********************************************************************
158 * init_user_process_params
160 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
162 static NTSTATUS
init_user_process_params( SIZE_T info_size
, HANDLE
*exe_file
)
167 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
169 status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &info_size
,
170 MEM_COMMIT
, PAGE_READWRITE
);
171 if (status
!= STATUS_SUCCESS
) return status
;
173 params
->AllocationSize
= info_size
;
174 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
176 SERVER_START_REQ( get_startup_info
)
178 wine_server_set_reply( req
, params
, info_size
);
179 if (!(status
= wine_server_call( req
)))
181 info_size
= wine_server_reply_size( reply
);
182 *exe_file
= reply
->exe_file
;
183 params
->hStdInput
= reply
->hstdin
;
184 params
->hStdOutput
= reply
->hstdout
;
185 params
->hStdError
= reply
->hstderr
;
189 if (status
!= STATUS_SUCCESS
) return status
;
191 if (params
->Size
> info_size
) params
->Size
= info_size
;
193 /* make sure the strings are valid */
194 fix_unicode_string( ¶ms
->CurrentDirectory
.DosPath
, (char *)info_size
);
195 fix_unicode_string( ¶ms
->DllPath
, (char *)info_size
);
196 fix_unicode_string( ¶ms
->ImagePathName
, (char *)info_size
);
197 fix_unicode_string( ¶ms
->CommandLine
, (char *)info_size
);
198 fix_unicode_string( ¶ms
->WindowTitle
, (char *)info_size
);
199 fix_unicode_string( ¶ms
->Desktop
, (char *)info_size
);
200 fix_unicode_string( ¶ms
->ShellInfo
, (char *)info_size
);
201 fix_unicode_string( ¶ms
->RuntimeInfo
, (char *)info_size
);
203 /* environment needs to be a separate memory block */
204 env_size
= info_size
- params
->Size
;
205 if (!env_size
) env_size
= 1;
207 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &env_size
,
208 MEM_COMMIT
, PAGE_READWRITE
);
209 if (status
!= STATUS_SUCCESS
) return status
;
210 memcpy( ptr
, (char *)params
+ params
->Size
, info_size
- params
->Size
);
211 params
->Environment
= ptr
;
213 RtlNormalizeProcessParams( params
);
218 /***********************************************************************
221 * Setup the initial thread.
223 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
225 HANDLE
thread_init(void)
230 SIZE_T size
, info_size
;
233 struct ntdll_thread_data
*thread_data
;
234 struct wine_pthread_thread_info thread_info
;
235 static struct debug_info debug_info
; /* debug info for initial thread */
239 /* reserve space for shared user data */
241 addr
= (void *)0x7ffe0000;
243 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
, MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
244 user_shared_data
= addr
;
246 /* allocate and initialize the PEB */
250 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 1, &size
,
251 MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
254 peb
->NumberOfProcessors
= 1;
255 peb
->ProcessParameters
= ¶ms
;
256 peb
->TlsBitmap
= &tls_bitmap
;
257 peb
->TlsExpansionBitmap
= &tls_expansion_bitmap
;
259 params
.CurrentDirectory
.DosPath
.Buffer
= current_dir
;
260 params
.CurrentDirectory
.DosPath
.MaximumLength
= sizeof(current_dir
);
261 params
.wShowWindow
= 1; /* SW_SHOWNORMAL */
262 RtlInitializeBitMap( &tls_bitmap
, peb
->TlsBitmapBits
, sizeof(peb
->TlsBitmapBits
) * 8 );
263 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
->TlsExpansionBitmapBits
,
264 sizeof(peb
->TlsExpansionBitmapBits
) * 8 );
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 (1 << sigstack_zero_bits
< sigstack_total_size
) sigstack_zero_bits
++;
274 assert( 1 << sigstack_zero_bits
== sigstack_total_size
); /* must be a power of 2 */
275 assert( sigstack_total_size
>= sizeof(TEB
) + sizeof(struct startup_info
) );
276 thread_info
.teb_size
= sigstack_total_size
;
279 size
= sigstack_total_size
;
280 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
281 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
284 thread_info
.teb_size
= size
;
286 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
287 thread_data
->debug_info
= &debug_info
;
288 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
290 thread_info
.stack_base
= NULL
;
291 thread_info
.stack_size
= 0;
292 thread_info
.teb_base
= teb
;
293 thread_info
.teb_sel
= thread_data
->fs
;
294 wine_pthread_get_functions( &pthread_functions
, sizeof(pthread_functions
) );
295 pthread_functions
.init_current_teb( &thread_info
);
296 pthread_functions
.init_thread( &thread_info
);
297 virtual_init_threading();
299 debug_info
.str_pos
= debug_info
.strings
;
300 debug_info
.out_pos
= debug_info
.output
;
303 /* setup the server connection */
304 server_init_process();
305 info_size
= server_init_thread( thread_info
.pid
, thread_info
.tid
, NULL
);
307 /* create the process heap */
308 if (!(peb
->ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
310 MESSAGE( "wine: failed to create the process heap\n" );
314 /* allocate user parameters */
317 init_user_process_params( info_size
, &exe_file
);
321 /* This is wine specific: we have no parent (we're started from unix)
322 * so, create a simple console with bare handles to unix stdio
324 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdInput
);
325 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
326 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
329 /* initialize LDT locking */
330 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
332 /* initialize time values in user_shared_data */
333 NtQuerySystemTime( &now
);
334 user_shared_data
->SystemTime
.LowPart
= now
.u
.LowPart
;
335 user_shared_data
->SystemTime
.High1Time
= user_shared_data
->SystemTime
.High2Time
= now
.u
.HighPart
;
336 user_shared_data
->u
.TickCountQuad
= (now
.QuadPart
- server_start_time
) / 10000;
337 user_shared_data
->u
.TickCount
.High2Time
= user_shared_data
->u
.TickCount
.High1Time
;
338 user_shared_data
->TickCountLowDeprecated
= user_shared_data
->u
.TickCount
.LowPart
;
339 user_shared_data
->TickCountMultiplier
= 1 << 24;
345 /* wrapper for apps that don't declare the thread function correctly */
346 extern DWORD
call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry
, void *arg
);
347 __ASM_GLOBAL_FUNC(call_thread_entry_point
,
352 "movl 8(%ebp),%eax\n\t"
357 static inline DWORD
call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry
, void *arg
)
359 LPTHREAD_START_ROUTINE func
= (LPTHREAD_START_ROUTINE
)entry
;
364 /***********************************************************************
367 * Hack to make things compatible with the thread procedures used by kernel32.CreateThread.
369 static void DECLSPEC_NORETURN
call_thread_func( PRTL_THREAD_START_ROUTINE rtl_func
, void *arg
)
374 MODULE_DllThreadAttach( NULL
);
377 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), rtl_func
, arg
);
379 exit_code
= call_thread_entry_point( rtl_func
, arg
);
381 /* send the exit code to the server */
382 SERVER_START_REQ( terminate_thread
)
384 req
->handle
= GetCurrentThread();
385 req
->exit_code
= exit_code
;
386 wine_server_call( req
);
393 LdrShutdownProcess();
399 server_exit_thread( exit_code
);
404 /***********************************************************************
407 * Startup routine for a newly created thread.
409 static void start_thread( struct wine_pthread_thread_info
*info
)
411 TEB
*teb
= info
->teb_base
;
412 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
413 struct startup_info
*startup_info
= (struct startup_info
*)info
;
414 PRTL_THREAD_START_ROUTINE func
= startup_info
->entry_point
;
415 void *arg
= startup_info
->entry_arg
;
416 struct debug_info debug_info
;
417 SIZE_T size
, page_size
= getpagesize();
419 debug_info
.str_pos
= debug_info
.strings
;
420 debug_info
.out_pos
= debug_info
.output
;
421 thread_data
->debug_info
= &debug_info
;
423 pthread_functions
.init_current_teb( info
);
425 server_init_thread( info
->pid
, info
->tid
, func
);
426 pthread_functions
.init_thread( info
);
428 /* allocate a memory view for the stack */
429 size
= info
->stack_size
;
430 teb
->DeallocationStack
= info
->stack_base
;
431 NtAllocateVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, 0,
432 &size
, MEM_SYSTEM
, PAGE_READWRITE
);
433 /* limit is lower than base since the stack grows down */
434 teb
->Tib
.StackBase
= (char *)info
->stack_base
+ info
->stack_size
;
435 teb
->Tib
.StackLimit
= (char *)info
->stack_base
+ page_size
;
437 /* setup the guard page */
439 NtProtectVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, &size
, PAGE_NOACCESS
, NULL
);
441 pthread_functions
.sigprocmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
444 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
447 /* NOTE: Windows does not have an exception handler around the call to
448 * the thread attach. We do for ease of debugging */
449 if (unhandled_exception_filter
)
453 call_thread_func( func
, arg
);
455 __EXCEPT(unhandled_exception_filter
)
457 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
462 call_thread_func( func
, arg
);
466 /***********************************************************************
467 * RtlCreateUserThread (NTDLL.@)
469 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
470 BOOLEAN suspended
, PVOID stack_addr
,
471 SIZE_T stack_reserve
, SIZE_T stack_commit
,
472 PRTL_THREAD_START_ROUTINE start
, void *param
,
473 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
476 struct ntdll_thread_data
*thread_data
= NULL
;
477 struct ntdll_thread_regs
*thread_regs
;
478 struct startup_info
*info
= NULL
;
485 SIZE_T size
, page_size
= getpagesize();
487 if (process
!= NtCurrentProcess())
492 memset( &call
, 0, sizeof(call
) );
494 call
.create_thread
.type
= APC_CREATE_THREAD
;
495 call
.create_thread
.func
= start
;
496 call
.create_thread
.arg
= param
;
497 call
.create_thread
.reserve
= stack_reserve
;
498 call
.create_thread
.commit
= stack_commit
;
499 call
.create_thread
.suspend
= suspended
;
500 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
501 if (status
!= STATUS_SUCCESS
) return status
;
503 if (result
.create_thread
.status
== STATUS_SUCCESS
)
505 if (id
) id
->UniqueThread
= ULongToHandle(result
.create_thread
.tid
);
506 if (handle_ptr
) *handle_ptr
= result
.create_thread
.handle
;
507 else NtClose( result
.create_thread
.handle
);
509 return result
.create_thread
.status
;
512 if (pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
513 fcntl( request_pipe
[1], F_SETFD
, 1 ); /* set close on exec flag */
514 wine_server_send_fd( request_pipe
[0] );
516 SERVER_START_REQ( new_thread
)
518 req
->access
= THREAD_ALL_ACCESS
;
519 req
->attributes
= 0; /* FIXME */
520 req
->suspend
= suspended
;
521 req
->request_fd
= request_pipe
[0];
522 if (!(status
= wine_server_call( req
)))
524 handle
= reply
->handle
;
527 close( request_pipe
[0] );
533 close( request_pipe
[1] );
537 pthread_functions
.sigprocmask( SIG_BLOCK
, &server_block_set
, &sigset
);
540 size
= sigstack_total_size
;
541 if ((status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
542 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
545 teb
->Peb
= NtCurrentTeb()->Peb
;
546 info
= (struct startup_info
*)(teb
+ 1);
547 info
->pthread_info
.teb_size
= size
;
548 if ((status
= init_teb( teb
))) goto error
;
550 teb
->ClientId
.UniqueProcess
= ULongToHandle(GetCurrentProcessId());
551 teb
->ClientId
.UniqueThread
= ULongToHandle(tid
);
553 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
554 thread_regs
= (struct ntdll_thread_regs
*)teb
->SpareBytes1
;
555 thread_data
->request_fd
= request_pipe
[1];
557 info
->pthread_info
.teb_base
= teb
;
558 info
->pthread_info
.teb_sel
= thread_data
->fs
;
560 /* inherit debug registers from parent thread */
561 thread_regs
->dr0
= ntdll_get_thread_regs()->dr0
;
562 thread_regs
->dr1
= ntdll_get_thread_regs()->dr1
;
563 thread_regs
->dr2
= ntdll_get_thread_regs()->dr2
;
564 thread_regs
->dr3
= ntdll_get_thread_regs()->dr3
;
565 thread_regs
->dr6
= ntdll_get_thread_regs()->dr6
;
566 thread_regs
->dr7
= ntdll_get_thread_regs()->dr7
;
568 if (!stack_reserve
|| !stack_commit
)
570 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
571 if (!stack_reserve
) stack_reserve
= nt
->OptionalHeader
.SizeOfStackReserve
;
572 if (!stack_commit
) stack_commit
= nt
->OptionalHeader
.SizeOfStackCommit
;
574 if (stack_reserve
< stack_commit
) stack_reserve
= stack_commit
;
575 stack_reserve
+= page_size
; /* for the guard page */
576 stack_reserve
= (stack_reserve
+ 0xffff) & ~0xffff; /* round to 64K boundary */
577 if (stack_reserve
< 1024 * 1024) stack_reserve
= 1024 * 1024; /* Xlib needs a large stack */
579 info
->pthread_info
.stack_base
= NULL
;
580 info
->pthread_info
.stack_size
= stack_reserve
;
581 info
->pthread_info
.entry
= start_thread
;
582 info
->entry_point
= start
;
583 info
->entry_arg
= param
;
585 if (pthread_functions
.create_thread( &info
->pthread_info
) == -1)
587 status
= STATUS_NO_MEMORY
;
590 pthread_functions
.sigprocmask( SIG_SETMASK
, &sigset
, NULL
);
592 if (id
) id
->UniqueThread
= ULongToHandle(tid
);
593 if (handle_ptr
) *handle_ptr
= handle
;
594 else NtClose( handle
);
596 return STATUS_SUCCESS
;
599 if (thread_data
) wine_ldt_free_fs( thread_data
->fs
);
603 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
605 if (handle
) NtClose( handle
);
606 pthread_functions
.sigprocmask( SIG_SETMASK
, &sigset
, NULL
);
607 close( request_pipe
[1] );
612 /***********************************************************************
613 * RtlExitUserThread (NTDLL.@)
615 void WINAPI
RtlExitUserThread( ULONG status
)
618 server_exit_thread( status
);
622 /***********************************************************************
623 * NtOpenThread (NTDLL.@)
624 * ZwOpenThread (NTDLL.@)
626 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
627 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
631 SERVER_START_REQ( open_thread
)
633 req
->tid
= HandleToULong(id
->UniqueThread
);
634 req
->access
= access
;
635 req
->attributes
= attr
? attr
->Attributes
: 0;
636 ret
= wine_server_call( req
);
637 *handle
= reply
->handle
;
644 /******************************************************************************
645 * NtSuspendThread (NTDLL.@)
646 * ZwSuspendThread (NTDLL.@)
648 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
652 SERVER_START_REQ( suspend_thread
)
654 req
->handle
= handle
;
655 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
662 /******************************************************************************
663 * NtResumeThread (NTDLL.@)
664 * ZwResumeThread (NTDLL.@)
666 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
670 SERVER_START_REQ( resume_thread
)
672 req
->handle
= handle
;
673 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
680 /******************************************************************************
681 * NtAlertResumeThread (NTDLL.@)
682 * ZwAlertResumeThread (NTDLL.@)
684 NTSTATUS WINAPI
NtAlertResumeThread( HANDLE handle
, PULONG count
)
686 FIXME( "stub: should alert thread %p\n", handle
);
687 return NtResumeThread( handle
, count
);
691 /******************************************************************************
692 * NtAlertThread (NTDLL.@)
693 * ZwAlertThread (NTDLL.@)
695 NTSTATUS WINAPI
NtAlertThread( HANDLE handle
)
697 FIXME( "stub: %p\n", handle
);
698 return STATUS_NOT_IMPLEMENTED
;
702 /******************************************************************************
703 * NtTerminateThread (NTDLL.@)
704 * ZwTerminateThread (NTDLL.@)
706 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
711 SERVER_START_REQ( terminate_thread
)
713 req
->handle
= handle
;
714 req
->exit_code
= exit_code
;
715 ret
= wine_server_call( req
);
716 self
= !ret
&& reply
->self
;
723 if (last
) exit( exit_code
);
724 else server_abort_thread( exit_code
);
730 /******************************************************************************
731 * NtQueueApcThread (NTDLL.@)
733 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
734 ULONG_PTR arg2
, ULONG_PTR arg3
)
737 SERVER_START_REQ( queue_apc
)
739 req
->thread
= handle
;
742 req
->call
.type
= APC_USER
;
743 req
->call
.user
.func
= func
;
744 req
->call
.user
.args
[0] = arg1
;
745 req
->call
.user
.args
[1] = arg2
;
746 req
->call
.user
.args
[2] = arg3
;
748 else req
->call
.type
= APC_NONE
; /* wake up only */
749 ret
= wine_server_call( req
);
756 /***********************************************************************
757 * NtSetContextThread (NTDLL.@)
758 * ZwSetContextThread (NTDLL.@)
760 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
767 /* on i386 debug registers always require a server call */
768 self
= (handle
== GetCurrentThread());
769 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
771 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
772 self
= (regs
->dr0
== context
->Dr0
&& regs
->dr1
== context
->Dr1
&&
773 regs
->dr2
== context
->Dr2
&& regs
->dr3
== context
->Dr3
&&
774 regs
->dr6
== context
->Dr6
&& regs
->dr7
== context
->Dr7
);
780 SERVER_START_REQ( set_thread_context
)
782 req
->handle
= handle
;
783 req
->flags
= context
->ContextFlags
;
785 wine_server_add_data( req
, context
, sizeof(*context
) );
786 ret
= wine_server_call( req
);
791 if (ret
== STATUS_PENDING
)
793 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
795 for (i
= 0; i
< 100; i
++)
797 SERVER_START_REQ( set_thread_context
)
799 req
->handle
= handle
;
800 req
->flags
= context
->ContextFlags
;
802 wine_server_add_data( req
, context
, sizeof(*context
) );
803 ret
= wine_server_call( req
);
806 if (ret
== STATUS_PENDING
)
808 LARGE_INTEGER timeout
;
809 timeout
.QuadPart
= -10000;
810 NtDelayExecution( FALSE
, &timeout
);
814 NtResumeThread( handle
, &dummy
);
816 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
822 if (self
) set_cpu_context( context
);
823 return STATUS_SUCCESS
;
827 /* copy a context structure according to the flags */
828 static inline void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
831 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
832 if (flags
& CONTEXT_INTEGER
)
841 if (flags
& CONTEXT_CONTROL
)
846 to
->SegCs
= from
->SegCs
;
847 to
->SegSs
= from
->SegSs
;
848 to
->EFlags
= from
->EFlags
;
850 if (flags
& CONTEXT_SEGMENTS
)
852 to
->SegDs
= from
->SegDs
;
853 to
->SegEs
= from
->SegEs
;
854 to
->SegFs
= from
->SegFs
;
855 to
->SegGs
= from
->SegGs
;
857 if (flags
& CONTEXT_DEBUG_REGISTERS
)
866 if (flags
& CONTEXT_FLOATING_POINT
)
868 to
->FloatSave
= from
->FloatSave
;
870 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
872 memcpy( to
->ExtendedRegisters
, from
->ExtendedRegisters
, sizeof(to
->ExtendedRegisters
) );
874 #elif defined(__x86_64__)
875 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
876 if (flags
& CONTEXT_CONTROL
)
881 to
->SegCs
= from
->SegCs
;
882 to
->SegSs
= from
->SegSs
;
883 to
->EFlags
= from
->EFlags
;
884 to
->MxCsr
= from
->MxCsr
;
886 if (flags
& CONTEXT_INTEGER
)
903 if (flags
& CONTEXT_SEGMENTS
)
905 to
->SegDs
= from
->SegDs
;
906 to
->SegEs
= from
->SegEs
;
907 to
->SegFs
= from
->SegFs
;
908 to
->SegGs
= from
->SegGs
;
910 if (flags
& CONTEXT_FLOATING_POINT
)
912 to
->u
.FltSave
= from
->u
.FltSave
;
914 if (flags
& CONTEXT_DEBUG_REGISTERS
)
923 #elif defined(__sparc__)
924 flags
&= ~CONTEXT_SPARC
; /* get rid of CPU id */
925 if (flags
& CONTEXT_CONTROL
)
934 if (flags
& CONTEXT_INTEGER
)
969 if (flags
& CONTEXT_FLOATING_POINT
)
973 #elif defined(__powerpc__)
975 if (flags
& CONTEXT_CONTROL
)
981 if (flags
& CONTEXT_INTEGER
)
983 to
->Gpr0
= from
->Gpr0
;
984 to
->Gpr1
= from
->Gpr1
;
985 to
->Gpr2
= from
->Gpr2
;
986 to
->Gpr3
= from
->Gpr3
;
987 to
->Gpr4
= from
->Gpr4
;
988 to
->Gpr5
= from
->Gpr5
;
989 to
->Gpr6
= from
->Gpr6
;
990 to
->Gpr7
= from
->Gpr7
;
991 to
->Gpr8
= from
->Gpr8
;
992 to
->Gpr9
= from
->Gpr9
;
993 to
->Gpr10
= from
->Gpr10
;
994 to
->Gpr11
= from
->Gpr11
;
995 to
->Gpr12
= from
->Gpr12
;
996 to
->Gpr13
= from
->Gpr13
;
997 to
->Gpr14
= from
->Gpr14
;
998 to
->Gpr15
= from
->Gpr15
;
999 to
->Gpr16
= from
->Gpr16
;
1000 to
->Gpr17
= from
->Gpr17
;
1001 to
->Gpr18
= from
->Gpr18
;
1002 to
->Gpr19
= from
->Gpr19
;
1003 to
->Gpr20
= from
->Gpr20
;
1004 to
->Gpr21
= from
->Gpr21
;
1005 to
->Gpr22
= from
->Gpr22
;
1006 to
->Gpr23
= from
->Gpr23
;
1007 to
->Gpr24
= from
->Gpr24
;
1008 to
->Gpr25
= from
->Gpr25
;
1009 to
->Gpr26
= from
->Gpr26
;
1010 to
->Gpr27
= from
->Gpr27
;
1011 to
->Gpr28
= from
->Gpr28
;
1012 to
->Gpr29
= from
->Gpr29
;
1013 to
->Gpr30
= from
->Gpr30
;
1014 to
->Gpr31
= from
->Gpr31
;
1015 to
->Xer
= from
->Xer
;
1018 if (flags
& CONTEXT_FLOATING_POINT
)
1020 to
->Fpr0
= from
->Fpr0
;
1021 to
->Fpr1
= from
->Fpr1
;
1022 to
->Fpr2
= from
->Fpr2
;
1023 to
->Fpr3
= from
->Fpr3
;
1024 to
->Fpr4
= from
->Fpr4
;
1025 to
->Fpr5
= from
->Fpr5
;
1026 to
->Fpr6
= from
->Fpr6
;
1027 to
->Fpr7
= from
->Fpr7
;
1028 to
->Fpr8
= from
->Fpr8
;
1029 to
->Fpr9
= from
->Fpr9
;
1030 to
->Fpr10
= from
->Fpr10
;
1031 to
->Fpr11
= from
->Fpr11
;
1032 to
->Fpr12
= from
->Fpr12
;
1033 to
->Fpr13
= from
->Fpr13
;
1034 to
->Fpr14
= from
->Fpr14
;
1035 to
->Fpr15
= from
->Fpr15
;
1036 to
->Fpr16
= from
->Fpr16
;
1037 to
->Fpr17
= from
->Fpr17
;
1038 to
->Fpr18
= from
->Fpr18
;
1039 to
->Fpr19
= from
->Fpr19
;
1040 to
->Fpr20
= from
->Fpr20
;
1041 to
->Fpr21
= from
->Fpr21
;
1042 to
->Fpr22
= from
->Fpr22
;
1043 to
->Fpr23
= from
->Fpr23
;
1044 to
->Fpr24
= from
->Fpr24
;
1045 to
->Fpr25
= from
->Fpr25
;
1046 to
->Fpr26
= from
->Fpr26
;
1047 to
->Fpr27
= from
->Fpr27
;
1048 to
->Fpr28
= from
->Fpr28
;
1049 to
->Fpr29
= from
->Fpr29
;
1050 to
->Fpr30
= from
->Fpr30
;
1051 to
->Fpr31
= from
->Fpr31
;
1052 to
->Fpscr
= from
->Fpscr
;
1055 #error You must implement context copying for your CPU
1060 /***********************************************************************
1061 * NtGetContextThread (NTDLL.@)
1062 * ZwGetContextThread (NTDLL.@)
1064 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
1069 DWORD needed_flags
= context
->ContextFlags
;
1070 BOOL self
= (handle
== GetCurrentThread());
1073 /* on i386 debug registers always require a server call */
1074 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)) self
= FALSE
;
1079 SERVER_START_REQ( get_thread_context
)
1081 req
->handle
= handle
;
1082 req
->flags
= context
->ContextFlags
;
1084 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
1085 ret
= wine_server_call( req
);
1090 if (ret
== STATUS_PENDING
)
1092 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
1094 for (i
= 0; i
< 100; i
++)
1096 SERVER_START_REQ( get_thread_context
)
1098 req
->handle
= handle
;
1099 req
->flags
= context
->ContextFlags
;
1101 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
1102 ret
= wine_server_call( req
);
1105 if (ret
== STATUS_PENDING
)
1107 LARGE_INTEGER timeout
;
1108 timeout
.QuadPart
= -10000;
1109 NtDelayExecution( FALSE
, &timeout
);
1113 NtResumeThread( handle
, &dummy
);
1115 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
1117 if (ret
) return ret
;
1118 copy_context( context
, &ctx
, context
->ContextFlags
& ctx
.ContextFlags
);
1119 needed_flags
&= ~ctx
.ContextFlags
;
1126 get_cpu_context( &ctx
);
1127 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
1130 /* update the cached version of the debug registers */
1131 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1133 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
1134 regs
->dr0
= context
->Dr0
;
1135 regs
->dr1
= context
->Dr1
;
1136 regs
->dr2
= context
->Dr2
;
1137 regs
->dr3
= context
->Dr3
;
1138 regs
->dr6
= context
->Dr6
;
1139 regs
->dr7
= context
->Dr7
;
1143 return STATUS_SUCCESS
;
1147 /******************************************************************************
1148 * NtQueryInformationThread (NTDLL.@)
1149 * ZwQueryInformationThread (NTDLL.@)
1151 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1152 void *data
, ULONG length
, ULONG
*ret_len
)
1158 case ThreadBasicInformation
:
1160 THREAD_BASIC_INFORMATION info
;
1161 const unsigned int affinity_mask
= (1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1163 SERVER_START_REQ( get_thread_info
)
1165 req
->handle
= handle
;
1167 if (!(status
= wine_server_call( req
)))
1169 info
.ExitStatus
= reply
->exit_code
;
1170 info
.TebBaseAddress
= reply
->teb
;
1171 info
.ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
1172 info
.ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
1173 info
.AffinityMask
= reply
->affinity
& affinity_mask
;
1174 info
.Priority
= reply
->priority
;
1175 info
.BasePriority
= reply
->priority
; /* FIXME */
1179 if (status
== STATUS_SUCCESS
)
1181 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
1182 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
1188 KERNEL_USER_TIMES kusrt
;
1189 /* We need to do a server call to get the creation time or exit time */
1190 /* This works on any thread */
1191 SERVER_START_REQ( get_thread_info
)
1193 req
->handle
= handle
;
1195 status
= wine_server_call( req
);
1196 if (status
== STATUS_SUCCESS
)
1198 kusrt
.CreateTime
.QuadPart
= reply
->creation_time
;
1199 kusrt
.ExitTime
.QuadPart
= reply
->exit_time
;
1203 if (status
== STATUS_SUCCESS
)
1205 /* We call times(2) for kernel time or user time */
1206 /* We can only (portably) do this for the current thread */
1207 if (handle
== GetCurrentThread())
1209 struct tms time_buf
;
1210 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
1213 kusrt
.KernelTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
1214 kusrt
.UserTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
1218 static BOOL reported
= FALSE
;
1220 kusrt
.KernelTime
.QuadPart
= 0;
1221 kusrt
.UserTime
.QuadPart
= 0;
1223 TRACE("Cannot get kerneltime or usertime of other threads\n");
1226 FIXME("Cannot get kerneltime or usertime of other threads\n");
1230 if (data
) memcpy( data
, &kusrt
, min( length
, sizeof(kusrt
) ));
1231 if (ret_len
) *ret_len
= min( length
, sizeof(kusrt
) );
1235 case ThreadDescriptorTableEntry
:
1238 THREAD_DESCRIPTOR_INFORMATION
* tdi
= data
;
1239 if (length
< sizeof(*tdi
))
1240 status
= STATUS_INFO_LENGTH_MISMATCH
;
1241 else if (!(tdi
->Selector
& 4)) /* GDT selector */
1243 unsigned sel
= tdi
->Selector
& ~3; /* ignore RPL */
1244 status
= STATUS_SUCCESS
;
1245 if (!sel
) /* null selector */
1246 memset( &tdi
->Entry
, 0, sizeof(tdi
->Entry
) );
1249 tdi
->Entry
.BaseLow
= 0;
1250 tdi
->Entry
.HighWord
.Bits
.BaseMid
= 0;
1251 tdi
->Entry
.HighWord
.Bits
.BaseHi
= 0;
1252 tdi
->Entry
.LimitLow
= 0xffff;
1253 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0xf;
1254 tdi
->Entry
.HighWord
.Bits
.Dpl
= 3;
1255 tdi
->Entry
.HighWord
.Bits
.Sys
= 0;
1256 tdi
->Entry
.HighWord
.Bits
.Pres
= 1;
1257 tdi
->Entry
.HighWord
.Bits
.Granularity
= 1;
1258 tdi
->Entry
.HighWord
.Bits
.Default_Big
= 1;
1259 tdi
->Entry
.HighWord
.Bits
.Type
= 0x12;
1260 /* it has to be one of the system GDT selectors */
1261 if (sel
!= (wine_get_ds() & ~3) && sel
!= (wine_get_ss() & ~3))
1263 if (sel
== (wine_get_cs() & ~3))
1264 tdi
->Entry
.HighWord
.Bits
.Type
|= 8; /* code segment */
1265 else status
= STATUS_ACCESS_DENIED
;
1271 SERVER_START_REQ( get_selector_entry
)
1273 req
->handle
= handle
;
1274 req
->entry
= tdi
->Selector
>> 3;
1275 status
= wine_server_call( req
);
1278 if (!(reply
->flags
& WINE_LDT_FLAGS_ALLOCATED
))
1279 status
= STATUS_ACCESS_VIOLATION
;
1282 wine_ldt_set_base ( &tdi
->Entry
, (void *)reply
->base
);
1283 wine_ldt_set_limit( &tdi
->Entry
, reply
->limit
);
1284 wine_ldt_set_flags( &tdi
->Entry
, reply
->flags
);
1290 if (status
== STATUS_SUCCESS
&& ret_len
)
1291 /* yes, that's a bit strange, but it's the way it is */
1292 *ret_len
= sizeof(LDT_ENTRY
);
1294 status
= STATUS_NOT_IMPLEMENTED
;
1298 case ThreadAmILastThread
:
1300 SERVER_START_REQ(get_thread_info
)
1302 req
->handle
= handle
;
1304 status
= wine_server_call( req
);
1305 if (status
== STATUS_SUCCESS
)
1307 BOOLEAN last
= reply
->last
;
1308 if (data
) memcpy( data
, &last
, min( length
, sizeof(last
) ));
1309 if (ret_len
) *ret_len
= min( length
, sizeof(last
) );
1315 case ThreadPriority
:
1316 case ThreadBasePriority
:
1317 case ThreadAffinityMask
:
1318 case ThreadImpersonationToken
:
1319 case ThreadEnableAlignmentFaultFixup
:
1320 case ThreadEventPair_Reusable
:
1321 case ThreadQuerySetWin32StartAddress
:
1322 case ThreadZeroTlsCell
:
1323 case ThreadPerformanceCount
:
1324 case ThreadIdealProcessor
:
1325 case ThreadPriorityBoost
:
1326 case ThreadSetTlsArrayAddress
:
1327 case ThreadIsIoPending
:
1329 FIXME( "info class %d not supported yet\n", class );
1330 return STATUS_NOT_IMPLEMENTED
;
1335 /******************************************************************************
1336 * NtSetInformationThread (NTDLL.@)
1337 * ZwSetInformationThread (NTDLL.@)
1339 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1340 LPCVOID data
, ULONG length
)
1345 case ThreadZeroTlsCell
:
1346 if (handle
== GetCurrentThread())
1351 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1352 index
= *(const DWORD
*)data
;
1353 if (index
< TLS_MINIMUM_AVAILABLE
)
1355 RtlAcquirePebLock();
1356 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1358 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1359 teb
->TlsSlots
[index
] = 0;
1361 RtlReleasePebLock();
1365 index
-= TLS_MINIMUM_AVAILABLE
;
1366 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
1367 return STATUS_INVALID_PARAMETER
;
1368 RtlAcquirePebLock();
1369 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1371 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1372 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
1374 RtlReleasePebLock();
1376 return STATUS_SUCCESS
;
1378 FIXME( "ZeroTlsCell not supported on other threads\n" );
1379 return STATUS_NOT_IMPLEMENTED
;
1381 case ThreadImpersonationToken
:
1383 const HANDLE
*phToken
= data
;
1384 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
1385 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
1386 SERVER_START_REQ( set_thread_info
)
1388 req
->handle
= handle
;
1389 req
->token
= *phToken
;
1390 req
->mask
= SET_THREAD_INFO_TOKEN
;
1391 status
= wine_server_call( req
);
1396 case ThreadBasePriority
:
1398 const DWORD
*pprio
= data
;
1399 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1400 SERVER_START_REQ( set_thread_info
)
1402 req
->handle
= handle
;
1403 req
->priority
= *pprio
;
1404 req
->mask
= SET_THREAD_INFO_PRIORITY
;
1405 status
= wine_server_call( req
);
1410 case ThreadAffinityMask
:
1412 const DWORD affinity_mask
= (1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1413 const DWORD
*paff
= data
;
1414 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1415 if (*paff
& ~affinity_mask
) return STATUS_INVALID_PARAMETER
;
1416 SERVER_START_REQ( set_thread_info
)
1418 req
->handle
= handle
;
1419 req
->affinity
= *paff
;
1420 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1421 status
= wine_server_call( req
);
1426 case ThreadBasicInformation
:
1428 case ThreadPriority
:
1429 case ThreadDescriptorTableEntry
:
1430 case ThreadEnableAlignmentFaultFixup
:
1431 case ThreadEventPair_Reusable
:
1432 case ThreadQuerySetWin32StartAddress
:
1433 case ThreadPerformanceCount
:
1434 case ThreadAmILastThread
:
1435 case ThreadIdealProcessor
:
1436 case ThreadPriorityBoost
:
1437 case ThreadSetTlsArrayAddress
:
1438 case ThreadIsIoPending
:
1440 FIXME( "info class %d not supported yet\n", class );
1441 return STATUS_NOT_IMPLEMENTED
;
1446 /**********************************************************************
1447 * NtCurrentTeb (NTDLL.@)
1449 #if defined(__i386__) && defined(__GNUC__)
1451 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
1453 #elif defined(__i386__) && defined(_MSC_VER)
1455 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
1459 /**********************************************************************/
1461 TEB
* WINAPI
NtCurrentTeb(void)
1463 return pthread_functions
.get_current_teb();
1466 #endif /* __i386__ */