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 LIST_ENTRY tls_links
;
55 /***********************************************************************
58 static TEB
*alloc_teb( ULONG
*size
)
62 *size
= SIGNAL_STACK_SIZE
+ sizeof(TEB
);
63 teb
= wine_anon_mmap( NULL
, *size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 );
64 if (teb
== (TEB
*)-1) return NULL
;
65 if (!(teb
->teb_sel
= wine_ldt_alloc_fs()))
70 teb
->Tib
.ExceptionList
= (void *)~0UL;
71 teb
->Tib
.StackBase
= (void *)~0UL;
72 teb
->Tib
.Self
= &teb
->Tib
;
74 teb
->StaticUnicodeString
.Buffer
= teb
->StaticUnicodeBuffer
;
75 teb
->StaticUnicodeString
.MaximumLength
= sizeof(teb
->StaticUnicodeBuffer
);
80 /***********************************************************************
83 static inline void free_teb( TEB
*teb
)
88 NtFreeVirtualMemory( GetCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
89 wine_ldt_free_fs( teb
->teb_sel
);
90 munmap( teb
, SIGNAL_STACK_SIZE
+ sizeof(TEB
) );
94 /***********************************************************************
97 * Setup the initial thread.
99 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
101 void thread_init(void)
106 struct wine_pthread_thread_info thread_info
;
107 static struct debug_info debug_info
; /* debug info for initial thread */
109 peb
.ProcessParameters
= ¶ms
;
110 peb
.TlsBitmap
= &tls_bitmap
;
112 RtlInitializeBitMap( &tls_bitmap
, (BYTE
*)peb
.TlsBitmapBits
, sizeof(peb
.TlsBitmapBits
) * 8 );
113 InitializeListHead( &ldr
.InLoadOrderModuleList
);
114 InitializeListHead( &ldr
.InMemoryOrderModuleList
);
115 InitializeListHead( &ldr
.InInitializationOrderModuleList
);
116 InitializeListHead( &tls_links
);
118 teb
= alloc_teb( &size
);
119 teb
->tibflags
= TEBF_WIN32
;
120 teb
->request_fd
= -1;
122 teb
->wait_fd
[0] = -1;
123 teb
->wait_fd
[1] = -1;
124 teb
->debug_info
= &debug_info
;
125 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
127 thread_info
.stack_base
= NULL
;
128 thread_info
.stack_size
= 0;
129 thread_info
.teb_base
= teb
;
130 thread_info
.teb_size
= size
;
131 thread_info
.teb_sel
= teb
->teb_sel
;
132 wine_pthread_init_current_teb( &thread_info
);
133 wine_pthread_init_thread( &thread_info
);
135 debug_info
.str_pos
= debug_info
.strings
;
136 debug_info
.out_pos
= debug_info
.output
;
140 /* setup the server connection */
141 server_init_process();
142 server_init_thread( thread_info
.pid
, thread_info
.tid
, NULL
);
144 /* create a memory view for the TEB */
145 NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, teb
, &size
,
146 MEM_SYSTEM
, PAGE_EXECUTE_READWRITE
);
148 /* create the process heap */
149 if (!(peb
.ProcessHeap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
)))
151 MESSAGE( "wine: failed to create the process heap\n" );
157 /***********************************************************************
160 * Startup routine for a newly created thread.
162 static void start_thread( struct wine_pthread_thread_info
*info
)
164 TEB
*teb
= info
->teb_base
;
165 struct startup_info
*startup_info
= (struct startup_info
*)info
;
166 PRTL_THREAD_START_ROUTINE func
= startup_info
->entry_point
;
167 void *arg
= startup_info
->entry_arg
;
168 struct debug_info debug_info
;
171 debug_info
.str_pos
= debug_info
.strings
;
172 debug_info
.out_pos
= debug_info
.output
;
173 teb
->debug_info
= &debug_info
;
175 wine_pthread_init_current_teb( info
);
177 server_init_thread( info
->pid
, info
->tid
, func
);
178 wine_pthread_init_thread( info
);
180 /* allocate a memory view for the stack */
181 size
= info
->stack_size
;
182 NtAllocateVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, info
->stack_base
,
183 &size
, MEM_SYSTEM
, PAGE_EXECUTE_READWRITE
);
184 /* limit is lower than base since the stack grows down */
185 teb
->Tib
.StackBase
= (char *)info
->stack_base
+ info
->stack_size
;
186 teb
->Tib
.StackLimit
= info
->stack_base
;
188 /* setup the guard page */
190 NtProtectVirtualMemory( GetCurrentProcess(), &teb
->DeallocationStack
, &size
,
191 PAGE_EXECUTE_READWRITE
| PAGE_GUARD
, NULL
);
192 RtlFreeHeap( GetProcessHeap(), 0, info
);
195 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
202 /***********************************************************************
203 * RtlCreateUserThread (NTDLL.@)
205 NTSTATUS WINAPI
RtlCreateUserThread( HANDLE process
, const SECURITY_DESCRIPTOR
*descr
,
206 BOOLEAN suspended
, PVOID stack_addr
,
207 SIZE_T stack_reserve
, SIZE_T stack_commit
,
208 PRTL_THREAD_START_ROUTINE start
, void *param
,
209 HANDLE
*handle_ptr
, CLIENT_ID
*id
)
211 struct startup_info
*info
= NULL
;
219 if (pipe( request_pipe
) == -1) return STATUS_TOO_MANY_OPENED_FILES
;
220 fcntl( request_pipe
[1], F_SETFD
, 1 ); /* set close on exec flag */
221 wine_server_send_fd( request_pipe
[0] );
223 SERVER_START_REQ( new_thread
)
225 req
->suspend
= suspended
;
226 req
->inherit
= 0; /* FIXME */
227 req
->request_fd
= request_pipe
[0];
228 if (!(status
= wine_server_call( req
)))
230 handle
= reply
->handle
;
233 close( request_pipe
[0] );
237 if (status
) goto error
;
239 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info
) )))
241 status
= STATUS_NO_MEMORY
;
245 if (!(teb
= alloc_teb( &size
)))
247 status
= STATUS_NO_MEMORY
;
250 teb
->ClientId
.UniqueProcess
= (HANDLE
)GetCurrentProcessId();
251 teb
->ClientId
.UniqueThread
= (HANDLE
)tid
;
253 teb
->tibflags
= TEBF_WIN32
;
254 teb
->exit_code
= STILL_ACTIVE
;
255 teb
->request_fd
= request_pipe
[1];
257 teb
->wait_fd
[0] = -1;
258 teb
->wait_fd
[1] = -1;
259 teb
->htask16
= NtCurrentTeb()->htask16
;
261 NtAllocateVirtualMemory( GetCurrentProcess(), &info
->pthread_info
.teb_base
, teb
, &size
,
262 MEM_SYSTEM
, PAGE_EXECUTE_READWRITE
);
263 info
->pthread_info
.teb_size
= size
;
264 info
->pthread_info
.teb_sel
= teb
->teb_sel
;
266 if (!stack_reserve
|| !stack_commit
)
268 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
);
269 if (!stack_reserve
) stack_reserve
= nt
->OptionalHeader
.SizeOfStackReserve
;
270 if (!stack_commit
) stack_commit
= nt
->OptionalHeader
.SizeOfStackCommit
;
272 if (stack_reserve
< stack_commit
) stack_reserve
= stack_commit
;
273 stack_reserve
= (stack_reserve
+ 0xffff) & ~0xffff; /* round to 64K boundary */
274 if (stack_reserve
< 1024 * 1024) stack_reserve
= 1024 * 1024; /* Xlib needs a large stack */
276 info
->pthread_info
.stack_base
= NULL
;
277 info
->pthread_info
.stack_size
= stack_reserve
;
278 info
->pthread_info
.entry
= start_thread
;
279 info
->entry_point
= start
;
280 info
->entry_arg
= param
;
282 if (wine_pthread_create_thread( &info
->pthread_info
) == -1)
284 status
= STATUS_NO_MEMORY
;
288 if (id
) id
->UniqueThread
= (HANDLE
)tid
;
289 if (handle_ptr
) *handle_ptr
= handle
;
290 else NtClose( handle
);
292 return STATUS_SUCCESS
;
295 if (teb
) free_teb( teb
);
296 if (info
) RtlFreeHeap( GetProcessHeap(), 0, info
);
297 if (handle
) NtClose( handle
);
298 close( request_pipe
[1] );
303 /***********************************************************************
304 * NtOpenThread (NTDLL.@)
305 * ZwOpenThread (NTDLL.@)
307 NTSTATUS WINAPI
NtOpenThread( HANDLE
*handle
, ACCESS_MASK access
,
308 const OBJECT_ATTRIBUTES
*attr
, const CLIENT_ID
*id
)
312 SERVER_START_REQ( open_thread
)
314 req
->tid
= (thread_id_t
)id
->UniqueThread
;
315 req
->access
= access
;
316 req
->inherit
= attr
&& (attr
->Attributes
& OBJ_INHERIT
);
317 ret
= wine_server_call( req
);
318 *handle
= reply
->handle
;
325 /******************************************************************************
326 * NtSuspendThread (NTDLL.@)
327 * ZwSuspendThread (NTDLL.@)
329 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
333 SERVER_START_REQ( suspend_thread
)
335 req
->handle
= handle
;
336 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
343 /******************************************************************************
344 * NtResumeThread (NTDLL.@)
345 * ZwResumeThread (NTDLL.@)
347 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
351 SERVER_START_REQ( resume_thread
)
353 req
->handle
= handle
;
354 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
361 /******************************************************************************
362 * NtTerminateThread (NTDLL.@)
363 * ZwTerminateThread (NTDLL.@)
365 NTSTATUS WINAPI
NtTerminateThread( HANDLE handle
, LONG exit_code
)
370 SERVER_START_REQ( terminate_thread
)
372 req
->handle
= handle
;
373 req
->exit_code
= exit_code
;
374 ret
= wine_server_call( req
);
375 self
= !ret
&& reply
->self
;
382 if (last
) exit( exit_code
);
383 else server_abort_thread( exit_code
);
389 /******************************************************************************
390 * NtQueueApcThread (NTDLL.@)
392 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
393 ULONG_PTR arg2
, ULONG_PTR arg3
)
396 SERVER_START_REQ( queue_apc
)
398 req
->handle
= handle
;
401 req
->arg1
= (void *)arg1
;
402 req
->arg2
= (void *)arg2
;
403 req
->arg3
= (void *)arg3
;
404 ret
= wine_server_call( req
);
411 /***********************************************************************
412 * NtSetContextThread (NTDLL.@)
413 * ZwSetContextThread (NTDLL.@)
415 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
419 SERVER_START_REQ( set_thread_context
)
421 req
->handle
= handle
;
422 req
->flags
= context
->ContextFlags
;
423 wine_server_add_data( req
, context
, sizeof(*context
) );
424 ret
= wine_server_call( req
);
431 /***********************************************************************
432 * NtGetContextThread (NTDLL.@)
433 * ZwGetContextThread (NTDLL.@)
435 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
439 SERVER_START_REQ( get_thread_context
)
441 req
->handle
= handle
;
442 req
->flags
= context
->ContextFlags
;
443 wine_server_add_data( req
, context
, sizeof(*context
) );
444 wine_server_set_reply( req
, context
, sizeof(*context
) );
445 ret
= wine_server_call( req
);
452 /******************************************************************************
453 * NtQueryInformationThread (NTDLL.@)
454 * ZwQueryInformationThread (NTDLL.@)
456 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
457 void *data
, ULONG length
, ULONG
*ret_len
)
463 case ThreadBasicInformation
:
465 THREAD_BASIC_INFORMATION info
;
467 SERVER_START_REQ( get_thread_info
)
469 req
->handle
= handle
;
471 if (!(status
= wine_server_call( req
)))
473 info
.ExitStatus
= reply
->exit_code
;
474 info
.TebBaseAddress
= reply
->teb
;
475 info
.ClientId
.UniqueProcess
= (HANDLE
)reply
->pid
;
476 info
.ClientId
.UniqueThread
= (HANDLE
)reply
->tid
;
477 info
.AffinityMask
= reply
->affinity
;
478 info
.Priority
= reply
->priority
;
479 info
.BasePriority
= reply
->priority
; /* FIXME */
483 if (status
== STATUS_SUCCESS
)
485 if (data
) memcpy( data
, &info
, min( length
, sizeof(info
) ));
486 if (ret_len
) *ret_len
= min( length
, sizeof(info
) );
492 case ThreadBasePriority
:
493 case ThreadAffinityMask
:
494 case ThreadImpersonationToken
:
495 case ThreadDescriptorTableEntry
:
496 case ThreadEnableAlignmentFaultFixup
:
497 case ThreadEventPair_Reusable
:
498 case ThreadQuerySetWin32StartAddress
:
499 case ThreadZeroTlsCell
:
500 case ThreadPerformanceCount
:
501 case ThreadAmILastThread
:
502 case ThreadIdealProcessor
:
503 case ThreadPriorityBoost
:
504 case ThreadSetTlsArrayAddress
:
505 case ThreadIsIoPending
:
507 FIXME( "info class %d not supported yet\n", class );
508 return STATUS_NOT_IMPLEMENTED
;
513 /******************************************************************************
514 * NtSetInformationThread (NTDLL.@)
515 * ZwSetInformationThread (NTDLL.@)
517 NTSTATUS WINAPI
NtSetInformationThread( HANDLE handle
, THREADINFOCLASS
class,
518 LPCVOID data
, ULONG length
)
522 case ThreadZeroTlsCell
:
523 if (handle
== GetCurrentThread())
528 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
529 index
= *(DWORD
*)data
;
530 if (index
>= 64) return STATUS_INVALID_PARAMETER
;
532 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
534 TEB
*teb
= CONTAINING_RECORD(entry
, TEB
, TlsLinks
);
535 teb
->TlsSlots
[index
] = 0;
538 return STATUS_SUCCESS
;
540 FIXME( "ZeroTlsCell not supported on other threads\n" );
541 return STATUS_NOT_IMPLEMENTED
;
543 case ThreadBasicInformation
:
546 case ThreadBasePriority
:
547 case ThreadAffinityMask
:
548 case ThreadImpersonationToken
:
549 case ThreadDescriptorTableEntry
:
550 case ThreadEnableAlignmentFaultFixup
:
551 case ThreadEventPair_Reusable
:
552 case ThreadQuerySetWin32StartAddress
:
553 case ThreadPerformanceCount
:
554 case ThreadAmILastThread
:
555 case ThreadIdealProcessor
:
556 case ThreadPriorityBoost
:
557 case ThreadSetTlsArrayAddress
:
558 case ThreadIsIoPending
:
560 FIXME( "info class %d not supported yet\n", class );
561 return STATUS_NOT_IMPLEMENTED
;
566 /**********************************************************************
567 * NtCurrentTeb (NTDLL.@)
569 #if defined(__i386__) && defined(__GNUC__)
570 __ASM_GLOBAL_FUNC( NtCurrentTeb
, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
571 #elif defined(__i386__) && defined(_MSC_VER)
572 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
574 TEB
* WINAPI
NtCurrentTeb(void)
576 return wine_pthread_get_current_teb();
578 #endif /* __i386__ */