4 * Copyright 1996, 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_MMAN_H
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
34 #define NONAMELESSUNION
36 #define WIN32_NO_STATUS
38 #include "wine/library.h"
39 #include "wine/server.h"
40 #include "wine/pthread.h"
41 #include "wine/debug.h"
42 #include "ntdll_misc.h"
44 #include "wine/exception.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
47 WINE_DECLARE_DEBUG_CHANNEL(relay
);
49 struct _KUSER_SHARED_DATA
*user_shared_data
= NULL
;
51 PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter
= NULL
;
53 /* info passed to a starting thread */
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 RTL_BITMAP fls_bitmap
;
67 static LIST_ENTRY tls_links
;
68 static size_t sigstack_total_size
;
69 static ULONG sigstack_zero_bits
;
71 struct wine_pthread_functions pthread_functions
= { NULL
};
74 static RTL_CRITICAL_SECTION ldt_section
;
75 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
78 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
79 0, 0, { (DWORD_PTR
)(__FILE__
": ldt_section") }
81 static RTL_CRITICAL_SECTION ldt_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
82 static sigset_t ldt_sigset
;
84 /***********************************************************************
85 * locking for LDT routines
87 static void ldt_lock(void)
91 pthread_functions
.sigprocmask( SIG_BLOCK
, &server_block_set
, &sigset
);
92 RtlEnterCriticalSection( &ldt_section
);
93 if (ldt_section
.RecursionCount
== 1) ldt_sigset
= sigset
;
96 static void ldt_unlock(void)
98 if (ldt_section
.RecursionCount
== 1)
100 sigset_t sigset
= ldt_sigset
;
101 RtlLeaveCriticalSection( &ldt_section
);
102 pthread_functions
.sigprocmask( SIG_SETMASK
, &sigset
, NULL
);
104 else RtlLeaveCriticalSection( &ldt_section
);
108 /***********************************************************************
111 static inline NTSTATUS
init_teb( TEB
*teb
)
113 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
115 teb
->Tib
.ExceptionList
= (void *)~0UL;
116 teb
->Tib
.StackBase
= (void *)~0UL;
117 teb
->Tib
.Self
= &teb
->Tib
;
118 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
119 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
121 if (!(thread_data
->fs
= wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS
;
122 thread_data
->request_fd
= -1;
123 thread_data
->reply_fd
= -1;
124 thread_data
->wait_fd
[0] = -1;
125 thread_data
->wait_fd
[1] = -1;
127 return STATUS_SUCCESS
;
131 /***********************************************************************
134 * Make sure the unicode string doesn't point beyond the end pointer
136 static inline void fix_unicode_string( UNICODE_STRING
*str
, const char *end_ptr
)
138 if ((char *)str
->Buffer
>= end_ptr
)
140 str
->Length
= str
->MaximumLength
= 0;
144 if ((char *)str
->Buffer
+ str
->MaximumLength
> end_ptr
)
146 str
->MaximumLength
= (end_ptr
- (char *)str
->Buffer
) & ~(sizeof(WCHAR
) - 1);
148 if (str
->Length
>= str
->MaximumLength
)
150 if (str
->MaximumLength
>= sizeof(WCHAR
))
151 str
->Length
= str
->MaximumLength
- sizeof(WCHAR
);
153 str
->Length
= str
->MaximumLength
= 0;
158 /***********************************************************************
159 * init_user_process_params
161 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
163 static NTSTATUS
init_user_process_params( SIZE_T info_size
, HANDLE
*exe_file
)
168 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
170 status
= NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &info_size
,
171 MEM_COMMIT
, PAGE_READWRITE
);
172 if (status
!= STATUS_SUCCESS
) return status
;
174 params
->AllocationSize
= info_size
;
175 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
177 SERVER_START_REQ( get_startup_info
)
179 wine_server_set_reply( req
, params
, info_size
);
180 if (!(status
= wine_server_call( req
)))
182 info_size
= wine_server_reply_size( reply
);
183 *exe_file
= reply
->exe_file
;
184 params
->hStdInput
= reply
->hstdin
;
185 params
->hStdOutput
= reply
->hstdout
;
186 params
->hStdError
= reply
->hstderr
;
190 if (status
!= STATUS_SUCCESS
) return status
;
192 if (params
->Size
> info_size
) params
->Size
= info_size
;
194 /* make sure the strings are valid */
195 fix_unicode_string( ¶ms
->CurrentDirectory
.DosPath
, (char *)info_size
);
196 fix_unicode_string( ¶ms
->DllPath
, (char *)info_size
);
197 fix_unicode_string( ¶ms
->ImagePathName
, (char *)info_size
);
198 fix_unicode_string( ¶ms
->CommandLine
, (char *)info_size
);
199 fix_unicode_string( ¶ms
->WindowTitle
, (char *)info_size
);
200 fix_unicode_string( ¶ms
->Desktop
, (char *)info_size
);
201 fix_unicode_string( ¶ms
->ShellInfo
, (char *)info_size
);
202 fix_unicode_string( ¶ms
->RuntimeInfo
, (char *)info_size
);
204 /* environment needs to be a separate memory block */
205 env_size
= info_size
- params
->Size
;
206 if (!env_size
) env_size
= 1;
208 status
= NtAllocateVirtualMemory( NtCurrentProcess(), &ptr
, 0, &env_size
,
209 MEM_COMMIT
, PAGE_READWRITE
);
210 if (status
!= STATUS_SUCCESS
) return status
;
211 memcpy( ptr
, (char *)params
+ params
->Size
, info_size
- params
->Size
);
212 params
->Environment
= ptr
;
214 RtlNormalizeProcessParams( params
);
219 /***********************************************************************
222 * Setup the initial thread.
224 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
226 HANDLE
thread_init(void)
231 SIZE_T size
, info_size
;
234 struct ntdll_thread_data
*thread_data
;
235 struct wine_pthread_thread_info thread_info
;
236 static struct debug_info debug_info
; /* debug info for initial thread */
240 /* reserve space for shared user data */
242 addr
= (void *)0x7ffe0000;
244 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
, MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
245 user_shared_data
= addr
;
247 /* allocate and initialize the PEB */
251 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 1, &size
,
252 MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
255 peb
->NumberOfProcessors
= 1;
256 peb
->ProcessParameters
= ¶ms
;
257 peb
->TlsBitmap
= &tls_bitmap
;
258 peb
->TlsExpansionBitmap
= &tls_expansion_bitmap
;
259 peb
->FlsBitmap
= &fls_bitmap
;
261 params
.CurrentDirectory
.DosPath
.Buffer
= current_dir
;
262 params
.CurrentDirectory
.DosPath
.MaximumLength
= sizeof(current_dir
);
263 params
.wShowWindow
= 1; /* SW_SHOWNORMAL */
264 RtlInitializeBitMap( &tls_bitmap
, peb
->TlsBitmapBits
, sizeof(peb
->TlsBitmapBits
) * 8 );
265 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
->TlsExpansionBitmapBits
,
266 sizeof(peb
->TlsExpansionBitmapBits
) * 8 );
267 RtlInitializeBitMap( &fls_bitmap
, peb
->FlsBitmapBits
, sizeof(peb
->FlsBitmapBits
) * 8 );
268 InitializeListHead( &peb
->FlsListHead
);
269 InitializeListHead( &ldr
.InLoadOrderModuleList
);
270 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
271 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
272 InitializeListHead( &tls_links
);
274 /* allocate and initialize the initial TEB */
276 sigstack_total_size
= get_signal_stack_total_size();
277 while (1 << sigstack_zero_bits
< sigstack_total_size
) sigstack_zero_bits
++;
278 assert( 1 << sigstack_zero_bits
== sigstack_total_size
); /* must be a power of 2 */
279 assert( sigstack_total_size
>= sizeof(TEB
) + sizeof(struct startup_info
) );
280 thread_info
.teb_size
= sigstack_total_size
;
283 size
= sigstack_total_size
;
284 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
285 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
);
288 thread_info
.teb_size
= size
;
290 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
291 thread_data
->debug_info
= &debug_info
;
292 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
294 thread_info
.stack_base
= NULL
;
295 thread_info
.stack_size
= 0;
296 thread_info
.teb_base
= teb
;
297 thread_info
.teb_sel
= thread_data
->fs
;
298 wine_pthread_get_functions( &pthread_functions
, sizeof(pthread_functions
) );
299 pthread_functions
.init_current_teb( &thread_info
);
300 pthread_functions
.init_thread( &thread_info
);
301 virtual_init_threading();
303 debug_info
.str_pos
= debug_info
.strings
;
304 debug_info
.out_pos
= debug_info
.output
;
307 /* setup the server connection */
308 server_init_process();
309 info_size
= server_init_thread( thread_info
.pid
, thread_info
.tid
, NULL
);
311 /* create the process heap */
312 if (!(peb
->ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
314 MESSAGE( "wine: failed to create the process heap\n" );
318 /* allocate user parameters */
321 init_user_process_params( info_size
, &exe_file
);
325 /* This is wine specific: we have no parent (we're started from unix)
326 * so, create a simple console with bare handles to unix stdio
328 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdInput
);
329 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
330 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
333 /* initialize LDT locking */
334 wine_ldt_init_locking( ldt_lock
, ldt_unlock
);
336 /* initialize time values in user_shared_data */
337 NtQuerySystemTime( &now
);
338 user_shared_data
->SystemTime
.LowPart
= now
.u
.LowPart
;
339 user_shared_data
->SystemTime
.High1Time
= user_shared_data
->SystemTime
.High2Time
= now
.u
.HighPart
;
340 user_shared_data
->u
.TickCountQuad
= (now
.QuadPart
- server_start_time
) / 10000;
341 user_shared_data
->u
.TickCount
.High2Time
= user_shared_data
->u
.TickCount
.High1Time
;
342 user_shared_data
->TickCountLowDeprecated
= user_shared_data
->u
.TickCount
.LowPart
;
343 user_shared_data
->TickCountMultiplier
= 1 << 24;
349 /* wrapper for apps that don't declare the thread function correctly */
350 extern DWORD
call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry
, void *arg
);
351 __ASM_GLOBAL_FUNC(call_thread_entry_point
,
356 "movl 8(%ebp),%eax\n\t"
361 static inline DWORD
call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry
, void *arg
)
363 LPTHREAD_START_ROUTINE func
= (LPTHREAD_START_ROUTINE
)entry
;
368 /***********************************************************************
371 * Hack to make things compatible with the thread procedures used by kernel32.CreateThread.
373 static void DECLSPEC_NORETURN
call_thread_func( PRTL_THREAD_START_ROUTINE rtl_func
, void *arg
)
378 MODULE_DllThreadAttach( NULL
);
381 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), rtl_func
, arg
);
383 exit_code
= call_thread_entry_point( rtl_func
, arg
);
385 /* send the exit code to the server */
386 SERVER_START_REQ( terminate_thread
)
388 req
->handle
= GetCurrentThread();
389 req
->exit_code
= exit_code
;
390 wine_server_call( req
);
397 LdrShutdownProcess();
403 server_exit_thread( exit_code
);
408 /***********************************************************************
411 * Startup routine for a newly created thread.
413 static void start_thread( struct wine_pthread_thread_info
*info
)
415 TEB
*teb
= info
->teb_base
;
416 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
417 struct startup_info
*startup_info
= (struct startup_info
*)info
;
418 PRTL_THREAD_START_ROUTINE func
= startup_info
->entry_point
;
419 void *arg
= startup_info
->entry_arg
;
420 struct debug_info debug_info
;
421 SIZE_T size
, page_size
= getpagesize();
423 debug_info
.str_pos
= debug_info
.strings
;
424 debug_info
.out_pos
= debug_info
.output
;
425 thread_data
->debug_info
= &debug_info
;
427 pthread_functions
.init_current_teb( info
);
429 server_init_thread( info
->pid
, info
->tid
, func
);
430 pthread_functions
.init_thread( info
);
432 /* allocate a memory view for the stack */
433 size
= info
->stack_size
;
434 teb
->DeallocationStack
= info
->stack_base
;
435 NtAllocateVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, 0,
436 &size
, MEM_SYSTEM
, PAGE_READWRITE
);
437 /* limit is lower than base since the stack grows down */
438 teb
->Tib
.StackBase
= (char *)info
->stack_base
+ info
->stack_size
;
439 teb
->Tib
.StackLimit
= (char *)info
->stack_base
+ page_size
;
441 /* setup the guard page */
443 NtProtectVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, &size
, PAGE_NOACCESS
, NULL
);
445 pthread_functions
.sigprocmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
448 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
451 /* NOTE: Windows does not have an exception handler around the call to
452 * the thread attach. We do for ease of debugging */
453 if (unhandled_exception_filter
)
457 call_thread_func( func
, arg
);
459 __EXCEPT(unhandled_exception_filter
)
461 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
466 call_thread_func( func
, arg
);
470 /***********************************************************************
471 * RtlCreateUserThread (NTDLL.@)
473 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
474 BOOLEAN suspended
, PVOID stack_addr
,
475 SIZE_T stack_reserve
, SIZE_T stack_commit
,
476 PRTL_THREAD_START_ROUTINE start
, void *param
,
477 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
480 struct ntdll_thread_data
*thread_data
= NULL
;
481 struct ntdll_thread_regs
*thread_regs
;
482 struct startup_info
*info
= NULL
;
489 SIZE_T size
, page_size
= getpagesize();
491 if (process
!= NtCurrentProcess())
496 memset( &call
, 0, sizeof(call
) );
498 call
.create_thread
.type
= APC_CREATE_THREAD
;
499 call
.create_thread
.func
= start
;
500 call
.create_thread
.arg
= param
;
501 call
.create_thread
.reserve
= stack_reserve
;
502 call
.create_thread
.commit
= stack_commit
;
503 call
.create_thread
.suspend
= suspended
;
504 status
= NTDLL_queue_process_apc( process
, &call
, &result
);
505 if (status
!= STATUS_SUCCESS
) return status
;
507 if (result
.create_thread
.status
== STATUS_SUCCESS
)
509 if (id
) id
->UniqueThread
= ULongToHandle(result
.create_thread
.tid
);
510 if (handle_ptr
) *handle_ptr
= result
.create_thread
.handle
;
511 else NtClose( result
.create_thread
.handle
);
513 return result
.create_thread
.status
;
516 if (pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
517 fcntl( request_pipe
[1], F_SETFD
, 1 ); /* set close on exec flag */
518 wine_server_send_fd( request_pipe
[0] );
520 SERVER_START_REQ( new_thread
)
522 req
->access
= THREAD_ALL_ACCESS
;
523 req
->attributes
= 0; /* FIXME */
524 req
->suspend
= suspended
;
525 req
->request_fd
= request_pipe
[0];
526 if (!(status
= wine_server_call( req
)))
528 handle
= reply
->handle
;
531 close( request_pipe
[0] );
537 close( request_pipe
[1] );
541 pthread_functions
.sigprocmask( SIG_BLOCK
, &server_block_set
, &sigset
);
544 size
= sigstack_total_size
;
545 if ((status
= NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, sigstack_zero_bits
,
546 &size
, MEM_COMMIT
| MEM_TOP_DOWN
, PAGE_READWRITE
)))
549 teb
->Peb
= NtCurrentTeb()->Peb
;
550 info
= (struct startup_info
*)(teb
+ 1);
551 info
->pthread_info
.teb_size
= size
;
552 if ((status
= init_teb( teb
))) goto error
;
554 teb
->ClientId
.UniqueProcess
= ULongToHandle(GetCurrentProcessId());
555 teb
->ClientId
.UniqueThread
= ULongToHandle(tid
);
557 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
558 thread_regs
= (struct ntdll_thread_regs
*)teb
->SpareBytes1
;
559 thread_data
->request_fd
= request_pipe
[1];
561 info
->pthread_info
.teb_base
= teb
;
562 info
->pthread_info
.teb_sel
= thread_data
->fs
;
564 /* inherit debug registers from parent thread */
565 thread_regs
->dr0
= ntdll_get_thread_regs()->dr0
;
566 thread_regs
->dr1
= ntdll_get_thread_regs()->dr1
;
567 thread_regs
->dr2
= ntdll_get_thread_regs()->dr2
;
568 thread_regs
->dr3
= ntdll_get_thread_regs()->dr3
;
569 thread_regs
->dr6
= ntdll_get_thread_regs()->dr6
;
570 thread_regs
->dr7
= ntdll_get_thread_regs()->dr7
;
572 if (!stack_reserve
|| !stack_commit
)
574 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
575 if (!stack_reserve
) stack_reserve
= nt
->OptionalHeader
.SizeOfStackReserve
;
576 if (!stack_commit
) stack_commit
= nt
->OptionalHeader
.SizeOfStackCommit
;
578 if (stack_reserve
< stack_commit
) stack_reserve
= stack_commit
;
579 stack_reserve
+= page_size
; /* for the guard page */
580 stack_reserve
= (stack_reserve
+ 0xffff) & ~0xffff; /* round to 64K boundary */
581 if (stack_reserve
< 1024 * 1024) stack_reserve
= 1024 * 1024; /* Xlib needs a large stack */
583 info
->pthread_info
.stack_base
= NULL
;
584 info
->pthread_info
.stack_size
= stack_reserve
;
585 info
->pthread_info
.entry
= start_thread
;
586 info
->entry_point
= start
;
587 info
->entry_arg
= param
;
589 if (pthread_functions
.create_thread( &info
->pthread_info
) == -1)
591 status
= STATUS_NO_MEMORY
;
594 pthread_functions
.sigprocmask( SIG_SETMASK
, &sigset
, NULL
);
596 if (id
) id
->UniqueThread
= ULongToHandle(tid
);
597 if (handle_ptr
) *handle_ptr
= handle
;
598 else NtClose( handle
);
600 return STATUS_SUCCESS
;
603 if (thread_data
) wine_ldt_free_fs( thread_data
->fs
);
607 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
609 if (handle
) NtClose( handle
);
610 pthread_functions
.sigprocmask( SIG_SETMASK
, &sigset
, NULL
);
611 close( request_pipe
[1] );
616 /***********************************************************************
617 * RtlExitUserThread (NTDLL.@)
619 void WINAPI
RtlExitUserThread( ULONG status
)
622 server_exit_thread( status
);
626 /***********************************************************************
627 * NtOpenThread (NTDLL.@)
628 * ZwOpenThread (NTDLL.@)
630 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
631 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
635 SERVER_START_REQ( open_thread
)
637 req
->tid
= HandleToULong(id
->UniqueThread
);
638 req
->access
= access
;
639 req
->attributes
= attr
? attr
->Attributes
: 0;
640 ret
= wine_server_call( req
);
641 *handle
= reply
->handle
;
648 /******************************************************************************
649 * NtSuspendThread (NTDLL.@)
650 * ZwSuspendThread (NTDLL.@)
652 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
656 SERVER_START_REQ( suspend_thread
)
658 req
->handle
= handle
;
659 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
666 /******************************************************************************
667 * NtResumeThread (NTDLL.@)
668 * ZwResumeThread (NTDLL.@)
670 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
674 SERVER_START_REQ( resume_thread
)
676 req
->handle
= handle
;
677 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
684 /******************************************************************************
685 * NtAlertResumeThread (NTDLL.@)
686 * ZwAlertResumeThread (NTDLL.@)
688 NTSTATUS WINAPI
NtAlertResumeThread( HANDLE handle
, PULONG count
)
690 FIXME( "stub: should alert thread %p\n", handle
);
691 return NtResumeThread( handle
, count
);
695 /******************************************************************************
696 * NtAlertThread (NTDLL.@)
697 * ZwAlertThread (NTDLL.@)
699 NTSTATUS WINAPI
NtAlertThread( HANDLE handle
)
701 FIXME( "stub: %p\n", handle
);
702 return STATUS_NOT_IMPLEMENTED
;
706 /******************************************************************************
707 * NtTerminateThread (NTDLL.@)
708 * ZwTerminateThread (NTDLL.@)
710 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
715 SERVER_START_REQ( terminate_thread
)
717 req
->handle
= handle
;
718 req
->exit_code
= exit_code
;
719 ret
= wine_server_call( req
);
720 self
= !ret
&& reply
->self
;
727 if (last
) exit( exit_code
);
728 else server_abort_thread( exit_code
);
734 /******************************************************************************
735 * NtQueueApcThread (NTDLL.@)
737 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
738 ULONG_PTR arg2
, ULONG_PTR arg3
)
741 SERVER_START_REQ( queue_apc
)
743 req
->thread
= handle
;
746 req
->call
.type
= APC_USER
;
747 req
->call
.user
.func
= func
;
748 req
->call
.user
.args
[0] = arg1
;
749 req
->call
.user
.args
[1] = arg2
;
750 req
->call
.user
.args
[2] = arg3
;
752 else req
->call
.type
= APC_NONE
; /* wake up only */
753 ret
= wine_server_call( req
);
760 /***********************************************************************
761 * NtSetContextThread (NTDLL.@)
762 * ZwSetContextThread (NTDLL.@)
764 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
771 /* on i386 debug registers always require a server call */
772 self
= (handle
== GetCurrentThread());
773 if (self
&& (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)))
775 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
776 self
= (regs
->dr0
== context
->Dr0
&& regs
->dr1
== context
->Dr1
&&
777 regs
->dr2
== context
->Dr2
&& regs
->dr3
== context
->Dr3
&&
778 regs
->dr6
== context
->Dr6
&& regs
->dr7
== context
->Dr7
);
784 SERVER_START_REQ( set_thread_context
)
786 req
->handle
= handle
;
787 req
->flags
= context
->ContextFlags
;
789 wine_server_add_data( req
, context
, sizeof(*context
) );
790 ret
= wine_server_call( req
);
795 if (ret
== STATUS_PENDING
)
797 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
799 for (i
= 0; i
< 100; i
++)
801 SERVER_START_REQ( set_thread_context
)
803 req
->handle
= handle
;
804 req
->flags
= context
->ContextFlags
;
806 wine_server_add_data( req
, context
, sizeof(*context
) );
807 ret
= wine_server_call( req
);
810 if (ret
== STATUS_PENDING
)
812 LARGE_INTEGER timeout
;
813 timeout
.QuadPart
= -10000;
814 NtDelayExecution( FALSE
, &timeout
);
818 NtResumeThread( handle
, &dummy
);
820 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
826 if (self
) set_cpu_context( context
);
827 return STATUS_SUCCESS
;
831 /* copy a context structure according to the flags */
832 static inline void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
835 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
836 if (flags
& CONTEXT_INTEGER
)
845 if (flags
& CONTEXT_CONTROL
)
850 to
->SegCs
= from
->SegCs
;
851 to
->SegSs
= from
->SegSs
;
852 to
->EFlags
= from
->EFlags
;
854 if (flags
& CONTEXT_SEGMENTS
)
856 to
->SegDs
= from
->SegDs
;
857 to
->SegEs
= from
->SegEs
;
858 to
->SegFs
= from
->SegFs
;
859 to
->SegGs
= from
->SegGs
;
861 if (flags
& CONTEXT_DEBUG_REGISTERS
)
870 if (flags
& CONTEXT_FLOATING_POINT
)
872 to
->FloatSave
= from
->FloatSave
;
874 if (flags
& CONTEXT_EXTENDED_REGISTERS
)
876 memcpy( to
->ExtendedRegisters
, from
->ExtendedRegisters
, sizeof(to
->ExtendedRegisters
) );
878 #elif defined(__x86_64__)
879 flags
&= ~CONTEXT_AMD64
; /* get rid of CPU id */
880 if (flags
& CONTEXT_CONTROL
)
885 to
->SegCs
= from
->SegCs
;
886 to
->SegSs
= from
->SegSs
;
887 to
->EFlags
= from
->EFlags
;
888 to
->MxCsr
= from
->MxCsr
;
890 if (flags
& CONTEXT_INTEGER
)
907 if (flags
& CONTEXT_SEGMENTS
)
909 to
->SegDs
= from
->SegDs
;
910 to
->SegEs
= from
->SegEs
;
911 to
->SegFs
= from
->SegFs
;
912 to
->SegGs
= from
->SegGs
;
914 if (flags
& CONTEXT_FLOATING_POINT
)
916 to
->u
.FltSave
= from
->u
.FltSave
;
918 if (flags
& CONTEXT_DEBUG_REGISTERS
)
927 #elif defined(__sparc__)
928 flags
&= ~CONTEXT_SPARC
; /* get rid of CPU id */
929 if (flags
& CONTEXT_CONTROL
)
938 if (flags
& CONTEXT_INTEGER
)
973 if (flags
& CONTEXT_FLOATING_POINT
)
977 #elif defined(__powerpc__)
979 if (flags
& CONTEXT_CONTROL
)
985 if (flags
& CONTEXT_INTEGER
)
987 to
->Gpr0
= from
->Gpr0
;
988 to
->Gpr1
= from
->Gpr1
;
989 to
->Gpr2
= from
->Gpr2
;
990 to
->Gpr3
= from
->Gpr3
;
991 to
->Gpr4
= from
->Gpr4
;
992 to
->Gpr5
= from
->Gpr5
;
993 to
->Gpr6
= from
->Gpr6
;
994 to
->Gpr7
= from
->Gpr7
;
995 to
->Gpr8
= from
->Gpr8
;
996 to
->Gpr9
= from
->Gpr9
;
997 to
->Gpr10
= from
->Gpr10
;
998 to
->Gpr11
= from
->Gpr11
;
999 to
->Gpr12
= from
->Gpr12
;
1000 to
->Gpr13
= from
->Gpr13
;
1001 to
->Gpr14
= from
->Gpr14
;
1002 to
->Gpr15
= from
->Gpr15
;
1003 to
->Gpr16
= from
->Gpr16
;
1004 to
->Gpr17
= from
->Gpr17
;
1005 to
->Gpr18
= from
->Gpr18
;
1006 to
->Gpr19
= from
->Gpr19
;
1007 to
->Gpr20
= from
->Gpr20
;
1008 to
->Gpr21
= from
->Gpr21
;
1009 to
->Gpr22
= from
->Gpr22
;
1010 to
->Gpr23
= from
->Gpr23
;
1011 to
->Gpr24
= from
->Gpr24
;
1012 to
->Gpr25
= from
->Gpr25
;
1013 to
->Gpr26
= from
->Gpr26
;
1014 to
->Gpr27
= from
->Gpr27
;
1015 to
->Gpr28
= from
->Gpr28
;
1016 to
->Gpr29
= from
->Gpr29
;
1017 to
->Gpr30
= from
->Gpr30
;
1018 to
->Gpr31
= from
->Gpr31
;
1019 to
->Xer
= from
->Xer
;
1022 if (flags
& CONTEXT_FLOATING_POINT
)
1024 to
->Fpr0
= from
->Fpr0
;
1025 to
->Fpr1
= from
->Fpr1
;
1026 to
->Fpr2
= from
->Fpr2
;
1027 to
->Fpr3
= from
->Fpr3
;
1028 to
->Fpr4
= from
->Fpr4
;
1029 to
->Fpr5
= from
->Fpr5
;
1030 to
->Fpr6
= from
->Fpr6
;
1031 to
->Fpr7
= from
->Fpr7
;
1032 to
->Fpr8
= from
->Fpr8
;
1033 to
->Fpr9
= from
->Fpr9
;
1034 to
->Fpr10
= from
->Fpr10
;
1035 to
->Fpr11
= from
->Fpr11
;
1036 to
->Fpr12
= from
->Fpr12
;
1037 to
->Fpr13
= from
->Fpr13
;
1038 to
->Fpr14
= from
->Fpr14
;
1039 to
->Fpr15
= from
->Fpr15
;
1040 to
->Fpr16
= from
->Fpr16
;
1041 to
->Fpr17
= from
->Fpr17
;
1042 to
->Fpr18
= from
->Fpr18
;
1043 to
->Fpr19
= from
->Fpr19
;
1044 to
->Fpr20
= from
->Fpr20
;
1045 to
->Fpr21
= from
->Fpr21
;
1046 to
->Fpr22
= from
->Fpr22
;
1047 to
->Fpr23
= from
->Fpr23
;
1048 to
->Fpr24
= from
->Fpr24
;
1049 to
->Fpr25
= from
->Fpr25
;
1050 to
->Fpr26
= from
->Fpr26
;
1051 to
->Fpr27
= from
->Fpr27
;
1052 to
->Fpr28
= from
->Fpr28
;
1053 to
->Fpr29
= from
->Fpr29
;
1054 to
->Fpr30
= from
->Fpr30
;
1055 to
->Fpr31
= from
->Fpr31
;
1056 to
->Fpscr
= from
->Fpscr
;
1059 #error You must implement context copying for your CPU
1064 /***********************************************************************
1065 * NtGetContextThread (NTDLL.@)
1066 * ZwGetContextThread (NTDLL.@)
1068 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
1073 DWORD needed_flags
= context
->ContextFlags
;
1074 BOOL self
= (handle
== GetCurrentThread());
1077 /* on i386 debug registers always require a server call */
1078 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
)) self
= FALSE
;
1083 SERVER_START_REQ( get_thread_context
)
1085 req
->handle
= handle
;
1086 req
->flags
= context
->ContextFlags
;
1088 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
1089 ret
= wine_server_call( req
);
1094 if (ret
== STATUS_PENDING
)
1096 if (NtSuspendThread( handle
, &dummy
) == STATUS_SUCCESS
)
1098 for (i
= 0; i
< 100; i
++)
1100 SERVER_START_REQ( get_thread_context
)
1102 req
->handle
= handle
;
1103 req
->flags
= context
->ContextFlags
;
1105 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
1106 ret
= wine_server_call( req
);
1109 if (ret
== STATUS_PENDING
)
1111 LARGE_INTEGER timeout
;
1112 timeout
.QuadPart
= -10000;
1113 NtDelayExecution( FALSE
, &timeout
);
1117 NtResumeThread( handle
, &dummy
);
1119 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
1121 if (ret
) return ret
;
1122 copy_context( context
, &ctx
, context
->ContextFlags
& ctx
.ContextFlags
);
1123 needed_flags
&= ~ctx
.ContextFlags
;
1130 get_cpu_context( &ctx
);
1131 copy_context( context
, &ctx
, ctx
.ContextFlags
& needed_flags
);
1134 /* update the cached version of the debug registers */
1135 if (context
->ContextFlags
& (CONTEXT_DEBUG_REGISTERS
& ~CONTEXT_i386
))
1137 struct ntdll_thread_regs
* const regs
= ntdll_get_thread_regs();
1138 regs
->dr0
= context
->Dr0
;
1139 regs
->dr1
= context
->Dr1
;
1140 regs
->dr2
= context
->Dr2
;
1141 regs
->dr3
= context
->Dr3
;
1142 regs
->dr6
= context
->Dr6
;
1143 regs
->dr7
= context
->Dr7
;
1147 return STATUS_SUCCESS
;
1151 /******************************************************************************
1152 * NtQueryInformationThread (NTDLL.@)
1153 * ZwQueryInformationThread (NTDLL.@)
1155 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1156 void *data
, ULONG length
, ULONG
*ret_len
)
1162 case ThreadBasicInformation
:
1164 THREAD_BASIC_INFORMATION info
;
1165 const unsigned int affinity_mask
= (1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1167 SERVER_START_REQ( get_thread_info
)
1169 req
->handle
= handle
;
1171 if (!(status
= wine_server_call( req
)))
1173 info
.ExitStatus
= reply
->exit_code
;
1174 info
.TebBaseAddress
= reply
->teb
;
1175 info
.ClientId
.UniqueProcess
= ULongToHandle(reply
->pid
);
1176 info
.ClientId
.UniqueThread
= ULongToHandle(reply
->tid
);
1177 info
.AffinityMask
= reply
->affinity
& affinity_mask
;
1178 info
.Priority
= reply
->priority
;
1179 info
.BasePriority
= reply
->priority
; /* FIXME */
1183 if (status
== STATUS_SUCCESS
)
1185 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
1186 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
1192 KERNEL_USER_TIMES kusrt
;
1193 /* We need to do a server call to get the creation time or exit time */
1194 /* This works on any thread */
1195 SERVER_START_REQ( get_thread_info
)
1197 req
->handle
= handle
;
1199 status
= wine_server_call( req
);
1200 if (status
== STATUS_SUCCESS
)
1202 kusrt
.CreateTime
.QuadPart
= reply
->creation_time
;
1203 kusrt
.ExitTime
.QuadPart
= reply
->exit_time
;
1207 if (status
== STATUS_SUCCESS
)
1209 /* We call times(2) for kernel time or user time */
1210 /* We can only (portably) do this for the current thread */
1211 if (handle
== GetCurrentThread())
1213 struct tms time_buf
;
1214 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
1217 kusrt
.KernelTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
1218 kusrt
.UserTime
.QuadPart
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
1222 static BOOL reported
= FALSE
;
1224 kusrt
.KernelTime
.QuadPart
= 0;
1225 kusrt
.UserTime
.QuadPart
= 0;
1227 TRACE("Cannot get kerneltime or usertime of other threads\n");
1230 FIXME("Cannot get kerneltime or usertime of other threads\n");
1234 if (data
) memcpy( data
, &kusrt
, min( length
, sizeof(kusrt
) ));
1235 if (ret_len
) *ret_len
= min( length
, sizeof(kusrt
) );
1239 case ThreadDescriptorTableEntry
:
1242 THREAD_DESCRIPTOR_INFORMATION
* tdi
= data
;
1243 if (length
< sizeof(*tdi
))
1244 status
= STATUS_INFO_LENGTH_MISMATCH
;
1245 else if (!(tdi
->Selector
& 4)) /* GDT selector */
1247 unsigned sel
= tdi
->Selector
& ~3; /* ignore RPL */
1248 status
= STATUS_SUCCESS
;
1249 if (!sel
) /* null selector */
1250 memset( &tdi
->Entry
, 0, sizeof(tdi
->Entry
) );
1253 tdi
->Entry
.BaseLow
= 0;
1254 tdi
->Entry
.HighWord
.Bits
.BaseMid
= 0;
1255 tdi
->Entry
.HighWord
.Bits
.BaseHi
= 0;
1256 tdi
->Entry
.LimitLow
= 0xffff;
1257 tdi
->Entry
.HighWord
.Bits
.LimitHi
= 0xf;
1258 tdi
->Entry
.HighWord
.Bits
.Dpl
= 3;
1259 tdi
->Entry
.HighWord
.Bits
.Sys
= 0;
1260 tdi
->Entry
.HighWord
.Bits
.Pres
= 1;
1261 tdi
->Entry
.HighWord
.Bits
.Granularity
= 1;
1262 tdi
->Entry
.HighWord
.Bits
.Default_Big
= 1;
1263 tdi
->Entry
.HighWord
.Bits
.Type
= 0x12;
1264 /* it has to be one of the system GDT selectors */
1265 if (sel
!= (wine_get_ds() & ~3) && sel
!= (wine_get_ss() & ~3))
1267 if (sel
== (wine_get_cs() & ~3))
1268 tdi
->Entry
.HighWord
.Bits
.Type
|= 8; /* code segment */
1269 else status
= STATUS_ACCESS_DENIED
;
1275 SERVER_START_REQ( get_selector_entry
)
1277 req
->handle
= handle
;
1278 req
->entry
= tdi
->Selector
>> 3;
1279 status
= wine_server_call( req
);
1282 if (!(reply
->flags
& WINE_LDT_FLAGS_ALLOCATED
))
1283 status
= STATUS_ACCESS_VIOLATION
;
1286 wine_ldt_set_base ( &tdi
->Entry
, (void *)reply
->base
);
1287 wine_ldt_set_limit( &tdi
->Entry
, reply
->limit
);
1288 wine_ldt_set_flags( &tdi
->Entry
, reply
->flags
);
1294 if (status
== STATUS_SUCCESS
&& ret_len
)
1295 /* yes, that's a bit strange, but it's the way it is */
1296 *ret_len
= sizeof(LDT_ENTRY
);
1298 status
= STATUS_NOT_IMPLEMENTED
;
1302 case ThreadAmILastThread
:
1304 SERVER_START_REQ(get_thread_info
)
1306 req
->handle
= handle
;
1308 status
= wine_server_call( req
);
1309 if (status
== STATUS_SUCCESS
)
1311 BOOLEAN last
= reply
->last
;
1312 if (data
) memcpy( data
, &last
, min( length
, sizeof(last
) ));
1313 if (ret_len
) *ret_len
= min( length
, sizeof(last
) );
1319 case ThreadPriority
:
1320 case ThreadBasePriority
:
1321 case ThreadAffinityMask
:
1322 case ThreadImpersonationToken
:
1323 case ThreadEnableAlignmentFaultFixup
:
1324 case ThreadEventPair_Reusable
:
1325 case ThreadQuerySetWin32StartAddress
:
1326 case ThreadZeroTlsCell
:
1327 case ThreadPerformanceCount
:
1328 case ThreadIdealProcessor
:
1329 case ThreadPriorityBoost
:
1330 case ThreadSetTlsArrayAddress
:
1331 case ThreadIsIoPending
:
1333 FIXME( "info class %d not supported yet\n", class );
1334 return STATUS_NOT_IMPLEMENTED
;
1339 /******************************************************************************
1340 * NtSetInformationThread (NTDLL.@)
1341 * ZwSetInformationThread (NTDLL.@)
1343 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
1344 LPCVOID data
, ULONG length
)
1349 case ThreadZeroTlsCell
:
1350 if (handle
== GetCurrentThread())
1355 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1356 index
= *(const DWORD
*)data
;
1357 if (index
< TLS_MINIMUM_AVAILABLE
)
1359 RtlAcquirePebLock();
1360 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1362 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1363 teb
->TlsSlots
[index
] = 0;
1365 RtlReleasePebLock();
1369 index
-= TLS_MINIMUM_AVAILABLE
;
1370 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
1371 return STATUS_INVALID_PARAMETER
;
1372 RtlAcquirePebLock();
1373 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
1375 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
1376 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
1378 RtlReleasePebLock();
1380 return STATUS_SUCCESS
;
1382 FIXME( "ZeroTlsCell not supported on other threads\n" );
1383 return STATUS_NOT_IMPLEMENTED
;
1385 case ThreadImpersonationToken
:
1387 const HANDLE
*phToken
= data
;
1388 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
1389 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
1390 SERVER_START_REQ( set_thread_info
)
1392 req
->handle
= handle
;
1393 req
->token
= *phToken
;
1394 req
->mask
= SET_THREAD_INFO_TOKEN
;
1395 status
= wine_server_call( req
);
1400 case ThreadBasePriority
:
1402 const DWORD
*pprio
= data
;
1403 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1404 SERVER_START_REQ( set_thread_info
)
1406 req
->handle
= handle
;
1407 req
->priority
= *pprio
;
1408 req
->mask
= SET_THREAD_INFO_PRIORITY
;
1409 status
= wine_server_call( req
);
1414 case ThreadAffinityMask
:
1416 const DWORD affinity_mask
= (1 << NtCurrentTeb()->Peb
->NumberOfProcessors
) - 1;
1417 const DWORD
*paff
= data
;
1418 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
1419 if (*paff
& ~affinity_mask
) return STATUS_INVALID_PARAMETER
;
1420 SERVER_START_REQ( set_thread_info
)
1422 req
->handle
= handle
;
1423 req
->affinity
= *paff
;
1424 req
->mask
= SET_THREAD_INFO_AFFINITY
;
1425 status
= wine_server_call( req
);
1430 case ThreadBasicInformation
:
1432 case ThreadPriority
:
1433 case ThreadDescriptorTableEntry
:
1434 case ThreadEnableAlignmentFaultFixup
:
1435 case ThreadEventPair_Reusable
:
1436 case ThreadQuerySetWin32StartAddress
:
1437 case ThreadPerformanceCount
:
1438 case ThreadAmILastThread
:
1439 case ThreadIdealProcessor
:
1440 case ThreadPriorityBoost
:
1441 case ThreadSetTlsArrayAddress
:
1442 case ThreadIsIoPending
:
1444 FIXME( "info class %d not supported yet\n", class );
1445 return STATUS_NOT_IMPLEMENTED
;
1450 /**********************************************************************
1451 * NtCurrentTeb (NTDLL.@)
1453 #if defined(__i386__) && defined(__GNUC__)
1455 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
1457 #elif defined(__i386__) && defined(_MSC_VER)
1459 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
1463 /**********************************************************************/
1465 TEB
* WINAPI
NtCurrentTeb(void)
1467 return pthread_functions
.get_current_teb();
1470 #endif /* __i386__ */