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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_MMAN_H
32 #include "wine/library.h"
33 #include "wine/server.h"
34 #include "wine/pthread.h"
35 #include "wine/debug.h"
36 #include "ntdll_misc.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
40 /* info passed to a starting thread */
43 struct wine_pthread_thread_info pthread_info
;
44 PRTL_THREAD_START_ROUTINE entry_point
;
49 static PEB_LDR_DATA ldr
;
50 static RTL_USER_PROCESS_PARAMETERS params
; /* default parameters if no parent */
51 static RTL_BITMAP tls_bitmap
;
52 static RTL_BITMAP tls_expansion_bitmap
;
53 static LIST_ENTRY tls_links
;
56 /***********************************************************************
59 static TEB
*alloc_teb( ULONG
*size
)
62 struct ntdll_thread_data
*thread_data
;
64 *size
= SIGNAL_STACK_SIZE
+ sizeof(TEB
);
65 teb
= wine_anon_mmap( NULL
, *size
, PROT_READ
| PROT_WRITE
, 0 );
66 if (teb
== (TEB
*)-1) return NULL
;
67 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
68 if (!(thread_data
->teb_sel
= wine_ldt_alloc_fs()))
73 teb
->Tib
.ExceptionList
= (void *)~0UL;
74 teb
->Tib
.StackBase
= (void *)~0UL;
75 teb
->Tib
.Self
= &teb
->Tib
;
77 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
78 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
83 /***********************************************************************
86 static inline void free_teb( TEB
*teb
)
90 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
92 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
93 wine_ldt_free_fs( thread_data
->teb_sel
);
94 munmap( teb
, SIGNAL_STACK_SIZE
+ sizeof(TEB
) );
98 /***********************************************************************
101 * Setup the initial thread.
103 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
105 void thread_init(void)
110 struct ntdll_thread_data
*thread_data
;
111 struct wine_pthread_thread_info thread_info
;
112 static struct debug_info debug_info
; /* debug info for initial thread */
114 peb
.NumberOfProcessors
= 1;
115 peb
.ProcessParameters
= ¶ms
;
116 peb
.TlsBitmap
= &tls_bitmap
;
117 peb
.TlsExpansionBitmap
= &tls_expansion_bitmap
;
119 RtlInitializeBitMap( &tls_bitmap
, peb
.TlsBitmapBits
, sizeof(peb
.TlsBitmapBits
) * 8 );
120 RtlInitializeBitMap( &tls_expansion_bitmap
, peb
.TlsExpansionBitmapBits
,
121 sizeof(peb
.TlsExpansionBitmapBits
) * 8 );
122 InitializeListHead( &ldr
.InLoadOrderModuleList
);
123 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
124 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
125 InitializeListHead( &tls_links
);
127 teb
= alloc_teb( &size
);
128 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
129 thread_data
->request_fd
= -1;
130 thread_data
->reply_fd
= -1;
131 thread_data
->wait_fd
[0] = -1;
132 thread_data
->wait_fd
[1] = -1;
133 thread_data
->debug_info
= &debug_info
;
134 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
136 thread_info
.stack_base
= NULL
;
137 thread_info
.stack_size
= 0;
138 thread_info
.teb_base
= teb
;
139 thread_info
.teb_size
= size
;
140 thread_info
.teb_sel
= thread_data
->teb_sel
;
141 wine_pthread_init_current_teb( &thread_info
);
142 wine_pthread_init_thread( &thread_info
);
144 debug_info
.str_pos
= debug_info
.strings
;
145 debug_info
.out_pos
= debug_info
.output
;
149 /* setup the server connection */
150 server_init_process();
151 server_init_thread( thread_info
.pid
, thread_info
.tid
, NULL
);
153 /* create a memory view for the TEB */
155 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
,
156 MEM_SYSTEM
, PAGE_READWRITE
);
158 /* create the process heap */
159 if (!(peb
.ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
161 MESSAGE( "wine: failed to create the process heap\n" );
167 /***********************************************************************
170 * Startup routine for a newly created thread.
172 static void start_thread( struct wine_pthread_thread_info
*info
)
174 TEB
*teb
= info
->teb_base
;
175 struct ntdll_thread_data
*thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
176 struct startup_info
*startup_info
= (struct startup_info
*)info
;
177 PRTL_THREAD_START_ROUTINE func
= startup_info
->entry_point
;
178 void *arg
= startup_info
->entry_arg
;
179 struct debug_info debug_info
;
182 debug_info
.str_pos
= debug_info
.strings
;
183 debug_info
.out_pos
= debug_info
.output
;
184 thread_data
->debug_info
= &debug_info
;
186 wine_pthread_init_current_teb( info
);
188 server_init_thread( info
->pid
, info
->tid
, func
);
189 wine_pthread_init_thread( info
);
191 /* allocate a memory view for the stack */
192 size
= info
->stack_size
;
193 teb
->DeallocationStack
= info
->stack_base
;
194 NtAllocateVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, 0,
195 &size
, MEM_SYSTEM
, PAGE_EXECUTE_READWRITE
);
196 /* limit is lower than base since the stack grows down */
197 teb
->Tib
.StackBase
= (char *)info
->stack_base
+ info
->stack_size
;
198 teb
->Tib
.StackLimit
= info
->stack_base
;
200 /* setup the guard page */
202 NtProtectVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, &size
,
203 PAGE_EXECUTE_READWRITE
| PAGE_GUARD
, NULL
);
204 RtlFreeHeap( GetProcessHeap(), 0, info
);
207 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
214 /***********************************************************************
215 * RtlCreateUserThread (NTDLL.@)
217 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
218 BOOLEAN suspended
, PVOID stack_addr
,
219 SIZE_T stack_reserve
, SIZE_T stack_commit
,
220 PRTL_THREAD_START_ROUTINE start
, void *param
,
221 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
223 struct ntdll_thread_data
*thread_data
;
224 struct startup_info
*info
= NULL
;
232 if( ! is_current_process( process
) )
234 ERR("Unsupported on other process\n");
235 return STATUS_ACCESS_DENIED
;
238 if (pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
239 fcntl( request_pipe
[1], F_SETFD
, 1 ); /* set close on exec flag */
240 wine_server_send_fd( request_pipe
[0] );
242 SERVER_START_REQ( new_thread
)
244 req
->suspend
= suspended
;
245 req
->inherit
= 0; /* FIXME */
246 req
->request_fd
= request_pipe
[0];
247 if (!(status
= wine_server_call( req
)))
249 handle
= reply
->handle
;
252 close( request_pipe
[0] );
256 if (status
) goto error
;
258 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info
) )))
260 status
= STATUS_NO_MEMORY
;
264 if (!(teb
= alloc_teb( &size
)))
266 status
= STATUS_NO_MEMORY
;
269 teb
->ClientId
.UniqueProcess
= (HANDLE
)GetCurrentProcessId();
270 teb
->ClientId
.UniqueThread
= (HANDLE
)tid
;
272 thread_data
= (struct ntdll_thread_data
*)teb
->SystemReserved2
;
273 thread_data
->request_fd
= request_pipe
[1];
274 thread_data
->reply_fd
= -1;
275 thread_data
->wait_fd
[0] = -1;
276 thread_data
->wait_fd
[1] = -1;
278 info
->pthread_info
.teb_base
= teb
;
279 NtAllocateVirtualMemory( NtCurrentProcess(), &info
->pthread_info
.teb_base
, 0, &size
,
280 MEM_SYSTEM
, PAGE_READWRITE
);
281 info
->pthread_info
.teb_size
= size
;
282 info
->pthread_info
.teb_sel
= thread_data
->teb_sel
;
284 if (!stack_reserve
|| !stack_commit
)
286 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
287 if (!stack_reserve
) stack_reserve
= nt
->OptionalHeader
.SizeOfStackReserve
;
288 if (!stack_commit
) stack_commit
= nt
->OptionalHeader
.SizeOfStackCommit
;
290 if (stack_reserve
< stack_commit
) stack_reserve
= stack_commit
;
291 stack_reserve
= (stack_reserve
+ 0xffff) & ~0xffff; /* round to 64K boundary */
292 if (stack_reserve
< 1024 * 1024) stack_reserve
= 1024 * 1024; /* Xlib needs a large stack */
294 info
->pthread_info
.stack_base
= NULL
;
295 info
->pthread_info
.stack_size
= stack_reserve
;
296 info
->pthread_info
.entry
= start_thread
;
297 info
->entry_point
= start
;
298 info
->entry_arg
= param
;
300 if (wine_pthread_create_thread( &info
->pthread_info
) == -1)
302 status
= STATUS_NO_MEMORY
;
306 if (id
) id
->UniqueThread
= (HANDLE
)tid
;
307 if (handle_ptr
) *handle_ptr
= handle
;
308 else NtClose( handle
);
310 return STATUS_SUCCESS
;
313 if (teb
) free_teb( teb
);
314 if (info
) RtlFreeHeap( GetProcessHeap(), 0, info
);
315 if (handle
) NtClose( handle
);
316 close( request_pipe
[1] );
321 /***********************************************************************
322 * NtOpenThread (NTDLL.@)
323 * ZwOpenThread (NTDLL.@)
325 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
326 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
330 SERVER_START_REQ( open_thread
)
332 req
->tid
= (thread_id_t
)id
->UniqueThread
;
333 req
->access
= access
;
334 req
->inherit
= attr
&& (attr
->Attributes
& OBJ_INHERIT
);
335 ret
= wine_server_call( req
);
336 *handle
= reply
->handle
;
343 /******************************************************************************
344 * NtSuspendThread (NTDLL.@)
345 * ZwSuspendThread (NTDLL.@)
347 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
351 SERVER_START_REQ( suspend_thread
)
353 req
->handle
= handle
;
354 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
361 /******************************************************************************
362 * NtResumeThread (NTDLL.@)
363 * ZwResumeThread (NTDLL.@)
365 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
369 SERVER_START_REQ( resume_thread
)
371 req
->handle
= handle
;
372 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
379 /******************************************************************************
380 * NtTerminateThread (NTDLL.@)
381 * ZwTerminateThread (NTDLL.@)
383 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
388 SERVER_START_REQ( terminate_thread
)
390 req
->handle
= handle
;
391 req
->exit_code
= exit_code
;
392 ret
= wine_server_call( req
);
393 self
= !ret
&& reply
->self
;
400 if (last
) exit( exit_code
);
401 else server_abort_thread( exit_code
);
407 /******************************************************************************
408 * NtQueueApcThread (NTDLL.@)
410 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
411 ULONG_PTR arg2
, ULONG_PTR arg3
)
414 SERVER_START_REQ( queue_apc
)
416 req
->handle
= handle
;
419 req
->arg1
= (void *)arg1
;
420 req
->arg2
= (void *)arg2
;
421 req
->arg3
= (void *)arg3
;
422 ret
= wine_server_call( req
);
429 /***********************************************************************
430 * NtSetContextThread (NTDLL.@)
431 * ZwSetContextThread (NTDLL.@)
433 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
437 SERVER_START_REQ( set_thread_context
)
439 req
->handle
= handle
;
440 req
->flags
= context
->ContextFlags
;
441 wine_server_add_data( req
, context
, sizeof(*context
) );
442 ret
= wine_server_call( req
);
449 /***********************************************************************
450 * NtGetContextThread (NTDLL.@)
451 * ZwGetContextThread (NTDLL.@)
453 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
457 SERVER_START_REQ( get_thread_context
)
459 req
->handle
= handle
;
460 req
->flags
= context
->ContextFlags
;
461 wine_server_add_data( req
, context
, sizeof(*context
) );
462 wine_server_set_reply( req
, context
, sizeof(*context
) );
463 ret
= wine_server_call( req
);
470 /******************************************************************************
471 * NtQueryInformationThread (NTDLL.@)
472 * ZwQueryInformationThread (NTDLL.@)
474 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
475 void *data
, ULONG length
, ULONG
*ret_len
)
481 case ThreadBasicInformation
:
483 THREAD_BASIC_INFORMATION info
;
485 SERVER_START_REQ( get_thread_info
)
487 req
->handle
= handle
;
489 if (!(status
= wine_server_call( req
)))
491 info
.ExitStatus
= reply
->exit_code
;
492 info
.TebBaseAddress
= reply
->teb
;
493 info
.ClientId
.UniqueProcess
= (HANDLE
)reply
->pid
;
494 info
.ClientId
.UniqueThread
= (HANDLE
)reply
->tid
;
495 info
.AffinityMask
= reply
->affinity
;
496 info
.Priority
= reply
->priority
;
497 info
.BasePriority
= reply
->priority
; /* FIXME */
501 if (status
== STATUS_SUCCESS
)
503 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
504 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
510 case ThreadBasePriority
:
511 case ThreadAffinityMask
:
512 case ThreadImpersonationToken
:
513 case ThreadDescriptorTableEntry
:
514 case ThreadEnableAlignmentFaultFixup
:
515 case ThreadEventPair_Reusable
:
516 case ThreadQuerySetWin32StartAddress
:
517 case ThreadZeroTlsCell
:
518 case ThreadPerformanceCount
:
519 case ThreadAmILastThread
:
520 case ThreadIdealProcessor
:
521 case ThreadPriorityBoost
:
522 case ThreadSetTlsArrayAddress
:
523 case ThreadIsIoPending
:
525 FIXME( "info class %d not supported yet\n", class );
526 return STATUS_NOT_IMPLEMENTED
;
531 /******************************************************************************
532 * NtSetInformationThread (NTDLL.@)
533 * ZwSetInformationThread (NTDLL.@)
535 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
536 LPCVOID data
, ULONG length
)
541 case ThreadZeroTlsCell
:
542 if (handle
== GetCurrentThread())
547 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
548 index
= *(const DWORD
*)data
;
549 if (index
< TLS_MINIMUM_AVAILABLE
)
552 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
554 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
555 teb
->TlsSlots
[index
] = 0;
561 index
-= TLS_MINIMUM_AVAILABLE
;
562 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
563 return STATUS_INVALID_PARAMETER
;
565 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
567 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
568 if (teb
->TlsExpansionSlots
) teb
->TlsExpansionSlots
[index
] = 0;
572 return STATUS_SUCCESS
;
574 FIXME( "ZeroTlsCell not supported on other threads\n" );
575 return STATUS_NOT_IMPLEMENTED
;
577 case ThreadImpersonationToken
:
579 const HANDLE
*phToken
= data
;
580 if (length
!= sizeof(HANDLE
)) return STATUS_INVALID_PARAMETER
;
581 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken
);
582 SERVER_START_REQ( set_thread_info
)
584 req
->handle
= handle
;
585 req
->token
= *phToken
;
586 req
->mask
= SET_THREAD_INFO_TOKEN
;
587 status
= wine_server_call( req
);
592 case ThreadBasicInformation
:
595 case ThreadBasePriority
:
596 case ThreadAffinityMask
:
597 case ThreadDescriptorTableEntry
:
598 case ThreadEnableAlignmentFaultFixup
:
599 case ThreadEventPair_Reusable
:
600 case ThreadQuerySetWin32StartAddress
:
601 case ThreadPerformanceCount
:
602 case ThreadAmILastThread
:
603 case ThreadIdealProcessor
:
604 case ThreadPriorityBoost
:
605 case ThreadSetTlsArrayAddress
:
606 case ThreadIsIoPending
:
608 FIXME( "info class %d not supported yet\n", class );
609 return STATUS_NOT_IMPLEMENTED
;
614 /**********************************************************************
615 * NtCurrentTeb (NTDLL.@)
617 #if defined(__i386__) && defined(__GNUC__)
619 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
621 #elif defined(__i386__) && defined(_MSC_VER)
623 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
627 /**********************************************************************/
629 TEB
* WINAPI
NtCurrentTeb(void)
631 return wine_pthread_get_current_teb();
634 #endif /* __i386__ */