4 * Copyright 1996, 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
29 #ifdef HAVE_SYS_TIMES_H
30 #include <sys/times.h>
33 #define NONAMELESSUNION
35 #define WIN32_NO_STATUS
38 #include "wine/library.h"
39 #include "wine/server.h"
40 #include "wine/pthread.h"
41 #include "wine/debug.h"
42 #include "ntdll_misc.h"
43 #include "wine/exception.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(thread);
47 /* info passed to a starting thread */
50 struct wine_pthread_thread_info pthread_info;
51 PRTL_THREAD_START_ROUTINE entry_point;
55 static PEB_LDR_DATA ldr;
56 static RTL_USER_PROCESS_PARAMETERS params; /* default parameters if no parent */
57 static WCHAR current_dir[MAX_NT_PATH_LENGTH];
58 static RTL_BITMAP tls_bitmap;
59 static RTL_BITMAP tls_expansion_bitmap;
60 static LIST_ENTRY tls_links;
61 static size_t sigstack_total_size;
62 static ULONG sigstack_zero_bits;
64 struct wine_pthread_functions pthread_functions = { NULL };
66 /***********************************************************************
69 static inline NTSTATUS init_teb( TEB *teb )
71 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
72 struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
74 teb->Tib.ExceptionList = (void *)~0UL;
75 teb->Tib.StackBase = (void *)~0UL;
76 teb->Tib.Self = &teb->Tib;
77 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
78 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
80 if (!(thread_regs->fs = wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS;
81 thread_data->request_fd = -1;
82 thread_data->reply_fd = -1;
83 thread_data->wait_fd[0] = -1;
84 thread_data->wait_fd[1] = -1;
86 return STATUS_SUCCESS;
90 /***********************************************************************
93 * Make sure the unicode string doesn't point beyond the end pointer
95 static inline void fix_unicode_string( UNICODE_STRING *str, char *end_ptr )
97 if ((char *)str->Buffer >= end_ptr)
99 str->Length = str->MaximumLength = 0;
103 if ((char *)str->Buffer + str->MaximumLength > end_ptr)
105 str->MaximumLength = (end_ptr - (char *)str->Buffer) & ~(sizeof(WCHAR) - 1);
107 if (str->Length >= str->MaximumLength)
109 if (str->MaximumLength >= sizeof(WCHAR))
110 str->Length = str->MaximumLength - sizeof(WCHAR);
112 str->Length = str->MaximumLength = 0;
117 /***********************************************************************
118 * init_user_process_params
120 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
122 static NTSTATUS init_user_process_params( SIZE_T info_size, HANDLE *exe_file )
127 RTL_USER_PROCESS_PARAMETERS *params = NULL;
129 status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms, 0, &info_size,
130 MEM_COMMIT, PAGE_READWRITE );
131 if (status != STATUS_SUCCESS) return status;
133 params->AllocationSize = info_size;
134 NtCurrentTeb()->Peb->ProcessParameters = params;
136 SERVER_START_REQ( get_startup_info )
138 wine_server_set_reply( req, params, info_size );
139 if (!(status = wine_server_call( req )))
141 info_size = wine_server_reply_size( reply );
142 *exe_file = reply->exe_file;
143 params->hStdInput = reply->hstdin;
144 params->hStdOutput = reply->hstdout;
145 params->hStdError = reply->hstderr;
149 if (status != STATUS_SUCCESS) return status;
151 if (params->Size > info_size) params->Size = info_size;
153 /* make sure the strings are valid */
154 fix_unicode_string( ¶ms->CurrentDirectory.DosPath, (char *)info_size );
155 fix_unicode_string( ¶ms->DllPath, (char *)info_size );
156 fix_unicode_string( ¶ms->ImagePathName, (char *)info_size );
157 fix_unicode_string( ¶ms->CommandLine, (char *)info_size );
158 fix_unicode_string( ¶ms->WindowTitle, (char *)info_size );
159 fix_unicode_string( ¶ms->Desktop, (char *)info_size );
160 fix_unicode_string( ¶ms->ShellInfo, (char *)info_size );
161 fix_unicode_string( ¶ms->RuntimeInfo, (char *)info_size );
163 /* environment needs to be a separate memory block */
164 env_size = info_size - params->Size;
165 if (!env_size) env_size = 1;
167 status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &env_size,
168 MEM_COMMIT, PAGE_READWRITE );
169 if (status != STATUS_SUCCESS) return status;
170 memcpy( ptr, (char *)params + params->Size, info_size - params->Size );
171 params->Environment = ptr;
173 RtlNormalizeProcessParams( params );
178 /***********************************************************************
181 * Setup the initial thread.
183 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
185 HANDLE thread_init(void)
190 SIZE_T size, info_size;
192 struct ntdll_thread_data *thread_data;
193 struct ntdll_thread_regs *thread_regs;
194 struct wine_pthread_thread_info thread_info;
195 static struct debug_info debug_info; /* debug info for initial thread */
199 /* reserve space for shared user data */
201 addr = (void *)0x7ffe0000;
203 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 0, &size, MEM_RESERVE, PAGE_READONLY );
205 /* allocate and initialize the PEB */
209 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 1, &size,
210 MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
213 peb->NumberOfProcessors = 1;
214 peb->ProcessParameters = ¶ms;
215 peb->TlsBitmap = &tls_bitmap;
216 peb->TlsExpansionBitmap = &tls_expansion_bitmap;
218 params.CurrentDirectory.DosPath.Buffer = current_dir;
219 params.CurrentDirectory.DosPath.MaximumLength = sizeof(current_dir);
220 params.wShowWindow = 1; /* SW_SHOWNORMAL */
221 RtlInitializeBitMap( &tls_bitmap, peb->TlsBitmapBits, sizeof(peb->TlsBitmapBits) * 8 );
222 RtlInitializeBitMap( &tls_expansion_bitmap, peb->TlsExpansionBitmapBits,
223 sizeof(peb->TlsExpansionBitmapBits) * 8 );
224 InitializeListHead( &ldr.InLoadOrderModuleList );
225 InitializeListHead( &ldr.InMemoryOrderModuleList );
226 InitializeListHead( &ldr.InInitializationOrderModuleList );
227 InitializeListHead( &tls_links );
229 /* allocate and initialize the initial TEB */
231 sigstack_total_size = get_signal_stack_total_size();
232 while (1 << sigstack_zero_bits < sigstack_total_size) sigstack_zero_bits++;
233 assert( 1 << sigstack_zero_bits == sigstack_total_size ); /* must be a power of 2 */
234 assert( sigstack_total_size >= sizeof(TEB) + sizeof(struct startup_info) );
235 thread_info.teb_size = sigstack_total_size;
238 size = sigstack_total_size;
239 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
240 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
243 thread_info.teb_size = size;
245 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
246 thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
247 thread_data->debug_info = &debug_info;
248 InsertHeadList( &tls_links, &teb->TlsLinks );
250 thread_info.stack_base = NULL;
251 thread_info.stack_size = 0;
252 thread_info.teb_base = teb;
253 thread_info.teb_sel = thread_regs->fs;
254 wine_pthread_get_functions( &pthread_functions, sizeof(pthread_functions) );
255 pthread_functions.init_current_teb( &thread_info );
256 pthread_functions.init_thread( &thread_info );
257 virtual_init_threading();
259 debug_info.str_pos = debug_info.strings;
260 debug_info.out_pos = debug_info.output;
263 /* setup the server connection */
264 server_init_process();
265 info_size = server_init_thread( thread_info.pid, thread_info.tid, NULL );
267 /* create the process heap */
268 if (!(peb->ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL )))
270 MESSAGE( "wine: failed to create the process heap\n" );
274 /* allocate user parameters */
277 init_user_process_params( info_size, &exe_file );
281 /* This is wine specific: we have no parent (we're started from unix)
282 * so, create a simple console with bare handles to unix stdio
284 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, OBJ_INHERIT, ¶ms.hStdInput );
285 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, ¶ms.hStdOutput );
286 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, ¶ms.hStdError );
291 typedef LONG (WINAPI *PUNHANDLED_EXCEPTION_FILTER)(PEXCEPTION_POINTERS);
292 static PUNHANDLED_EXCEPTION_FILTER get_unhandled_exception_filter(void)
294 static PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter;
295 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
296 UNICODE_STRING module_name;
297 ANSI_STRING func_name;
298 HMODULE kernel32_handle;
300 if (unhandled_exception_filter) return unhandled_exception_filter;
302 RtlInitUnicodeString(&module_name, kernel32W);
303 RtlInitAnsiString( &func_name, "UnhandledExceptionFilter" );
305 if (LdrGetDllHandle( 0, 0, &module_name, &kernel32_handle ) == STATUS_SUCCESS)
306 LdrGetProcedureAddress( kernel32_handle, &func_name, 0,
307 (void **)&unhandled_exception_filter );
309 return unhandled_exception_filter;
312 /***********************************************************************
315 * Startup routine for a newly created thread.
317 static void start_thread( struct wine_pthread_thread_info *info )
319 TEB *teb = info->teb_base;
320 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
321 struct startup_info *startup_info = (struct startup_info *)info;
322 PRTL_THREAD_START_ROUTINE func = startup_info->entry_point;
323 void *arg = startup_info->entry_arg;
324 struct debug_info debug_info;
325 SIZE_T size, page_size = getpagesize();
327 debug_info.str_pos = debug_info.strings;
328 debug_info.out_pos = debug_info.output;
329 thread_data->debug_info = &debug_info;
331 pthread_functions.init_current_teb( info );
333 server_init_thread( info->pid, info->tid, func );
334 pthread_functions.init_thread( info );
336 /* allocate a memory view for the stack */
337 size = info->stack_size;
338 teb->DeallocationStack = info->stack_base;
339 NtAllocateVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, 0,
340 &size, MEM_SYSTEM, PAGE_READWRITE );
341 /* limit is lower than base since the stack grows down */
342 teb->Tib.StackBase = (char *)info->stack_base + info->stack_size;
343 teb->Tib.StackLimit = (char *)info->stack_base + page_size;
345 /* setup the guard page */
347 NtProtectVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, &size, PAGE_NOACCESS, NULL );
350 InsertHeadList( &tls_links, &teb->TlsLinks );
353 /* NOTE: Windows does not have an exception handler around the call to
354 * the thread attach. We do for ease of debugging */
355 if (get_unhandled_exception_filter())
359 MODULE_DllThreadAttach( NULL );
361 __EXCEPT(get_unhandled_exception_filter())
363 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
368 MODULE_DllThreadAttach( NULL );
374 /***********************************************************************
375 * RtlCreateUserThread (NTDLL.@)
377 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
378 BOOLEAN suspended, PVOID stack_addr,
379 SIZE_T stack_reserve, SIZE_T stack_commit,
380 PRTL_THREAD_START_ROUTINE start, void *param,
381 HANDLE *handle_ptr, CLIENT_ID *id )
383 struct ntdll_thread_data *thread_data;
384 struct ntdll_thread_regs *thread_regs = NULL;
385 struct startup_info *info = NULL;
392 SIZE_T size, page_size = getpagesize();
394 if( ! is_current_process( process ) )
396 ERR("Unsupported on other process\n");
397 return STATUS_ACCESS_DENIED;
400 if (pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
401 fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
402 wine_server_send_fd( request_pipe[0] );
404 SERVER_START_REQ( new_thread )
406 req->access = THREAD_ALL_ACCESS;
407 req->attributes = 0; /* FIXME */
408 req->suspend = suspended;
409 req->request_fd = request_pipe[0];
410 if (!(status = wine_server_call( req )))
412 handle = reply->handle;
415 close( request_pipe[0] );
419 if (status) goto error;
422 size = sigstack_total_size;
423 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
424 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
427 teb->Peb = NtCurrentTeb()->Peb;
428 info = (struct startup_info *)(teb + 1);
429 info->pthread_info.teb_size = size;
430 if ((status = init_teb( teb ))) goto error;
432 teb->ClientId.UniqueProcess = (HANDLE)GetCurrentProcessId();
433 teb->ClientId.UniqueThread = (HANDLE)tid;
435 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
436 thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
437 thread_data->request_fd = request_pipe[1];
439 info->pthread_info.teb_base = teb;
440 info->pthread_info.teb_sel = thread_regs->fs;
442 /* inherit debug registers from parent thread */
443 thread_regs->dr0 = ntdll_get_thread_regs()->dr0;
444 thread_regs->dr1 = ntdll_get_thread_regs()->dr1;
445 thread_regs->dr2 = ntdll_get_thread_regs()->dr2;
446 thread_regs->dr3 = ntdll_get_thread_regs()->dr3;
447 thread_regs->dr6 = ntdll_get_thread_regs()->dr6;
448 thread_regs->dr7 = ntdll_get_thread_regs()->dr7;
450 if (!stack_reserve || !stack_commit)
452 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
453 if (!stack_reserve) stack_reserve = nt->OptionalHeader.SizeOfStackReserve;
454 if (!stack_commit) stack_commit = nt->OptionalHeader.SizeOfStackCommit;
456 if (stack_reserve < stack_commit) stack_reserve = stack_commit;
457 stack_reserve += page_size; /* for the guard page */
458 stack_reserve = (stack_reserve + 0xffff) & ~0xffff; /* round to 64K boundary */
459 if (stack_reserve < 1024 * 1024) stack_reserve = 1024 * 1024; /* Xlib needs a large stack */
461 info->pthread_info.stack_base = NULL;
462 info->pthread_info.stack_size = stack_reserve;
463 info->pthread_info.entry = start_thread;
464 info->entry_point = start;
465 info->entry_arg = param;
467 if (pthread_functions.create_thread( &info->pthread_info ) == -1)
469 status = STATUS_NO_MEMORY;
473 if (id) id->UniqueThread = (HANDLE)tid;
474 if (handle_ptr) *handle_ptr = handle;
475 else NtClose( handle );
477 return STATUS_SUCCESS;
480 if (thread_regs) wine_ldt_free_fs( thread_regs->fs );
484 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
486 if (handle) NtClose( handle );
487 close( request_pipe[1] );
492 /***********************************************************************
493 * RtlExitUserThread (NTDLL.@)
495 void WINAPI RtlExitUserThread( ULONG status )
498 server_exit_thread( status );
502 /***********************************************************************
503 * NtOpenThread (NTDLL.@)
504 * ZwOpenThread (NTDLL.@)
506 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
507 const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
511 SERVER_START_REQ( open_thread )
513 req->tid = (thread_id_t)id->UniqueThread;
514 req->access = access;
515 req->attributes = attr ? attr->Attributes : 0;
516 ret = wine_server_call( req );
517 *handle = reply->handle;
524 /******************************************************************************
525 * NtSuspendThread (NTDLL.@)
526 * ZwSuspendThread (NTDLL.@)
528 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
532 SERVER_START_REQ( suspend_thread )
534 req->handle = handle;
535 if (!(ret = wine_server_call( req ))) *count = reply->count;
542 /******************************************************************************
543 * NtResumeThread (NTDLL.@)
544 * ZwResumeThread (NTDLL.@)
546 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
550 SERVER_START_REQ( resume_thread )
552 req->handle = handle;
553 if (!(ret = wine_server_call( req ))) *count = reply->count;
560 /******************************************************************************
561 * NtAlertResumeThread (NTDLL.@)
562 * ZwAlertResumeThread (NTDLL.@)
564 NTSTATUS WINAPI NtAlertResumeThread( HANDLE handle, PULONG count )
566 FIXME( "stub: should alert thread %p\n", handle );
567 return NtResumeThread( handle, count );
571 /******************************************************************************
572 * NtAlertThread (NTDLL.@)
573 * ZwAlertThread (NTDLL.@)
575 NTSTATUS WINAPI NtAlertThread( HANDLE handle )
577 FIXME( "stub: %p\n", handle );
578 return STATUS_NOT_IMPLEMENTED;
582 /******************************************************************************
583 * NtTerminateThread (NTDLL.@)
584 * ZwTerminateThread (NTDLL.@)
586 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
591 SERVER_START_REQ( terminate_thread )
593 req->handle = handle;
594 req->exit_code = exit_code;
595 ret = wine_server_call( req );
596 self = !ret && reply->self;
603 if (last) exit( exit_code );
604 else server_abort_thread( exit_code );
610 /******************************************************************************
611 * NtQueueApcThread (NTDLL.@)
613 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
614 ULONG_PTR arg2, ULONG_PTR arg3 )
617 SERVER_START_REQ( queue_apc )
619 req->thread = handle;
622 req->call.type = APC_USER;
623 req->call.user.func = func;
624 req->call.user.args[0] = arg1;
625 req->call.user.args[1] = arg2;
626 req->call.user.args[2] = arg3;
628 else req->call.type = APC_NONE; /* wake up only */
629 ret = wine_server_call( req );
636 /***********************************************************************
637 * NtSetContextThread (NTDLL.@)
638 * ZwSetContextThread (NTDLL.@)
640 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
647 /* on i386 debug registers always require a server call */
648 self = (handle == GetCurrentThread());
649 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
651 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
652 self = (regs->dr0 == context->Dr0 && regs->dr1 == context->Dr1 &&
653 regs->dr2 == context->Dr2 && regs->dr3 == context->Dr3 &&
654 regs->dr6 == context->Dr6 && regs->dr7 == context->Dr7);
660 SERVER_START_REQ( set_thread_context )
662 req->handle = handle;
663 req->flags = context->ContextFlags;
665 wine_server_add_data( req, context, sizeof(*context) );
666 ret = wine_server_call( req );
671 if (ret == STATUS_PENDING)
673 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
675 for (i = 0; i < 100; i++)
677 SERVER_START_REQ( set_thread_context )
679 req->handle = handle;
680 req->flags = context->ContextFlags;
682 wine_server_add_data( req, context, sizeof(*context) );
683 ret = wine_server_call( req );
686 if (ret != STATUS_PENDING) break;
689 NtResumeThread( handle, &dummy );
691 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
697 if (self) set_cpu_context( context );
698 return STATUS_SUCCESS;
702 /* copy a context structure according to the flags */
703 static inline void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
706 flags &= ~CONTEXT_i386; /* get rid of CPU id */
707 if (flags & CONTEXT_INTEGER)
716 if (flags & CONTEXT_CONTROL)
721 to->SegCs = from->SegCs;
722 to->SegSs = from->SegSs;
723 to->EFlags = from->EFlags;
725 if (flags & CONTEXT_SEGMENTS)
727 to->SegDs = from->SegDs;
728 to->SegEs = from->SegEs;
729 to->SegFs = from->SegFs;
730 to->SegGs = from->SegGs;
732 if (flags & CONTEXT_DEBUG_REGISTERS)
741 if (flags & CONTEXT_FLOATING_POINT)
743 to->FloatSave = from->FloatSave;
745 #elif defined(__x86_64__)
746 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
747 if (flags & CONTEXT_CONTROL)
752 to->SegCs = from->SegCs;
753 to->SegSs = from->SegSs;
754 to->EFlags = from->EFlags;
755 to->MxCsr = from->MxCsr;
757 if (flags & CONTEXT_INTEGER)
774 if (flags & CONTEXT_SEGMENTS)
776 to->SegDs = from->SegDs;
777 to->SegEs = from->SegEs;
778 to->SegFs = from->SegFs;
779 to->SegGs = from->SegGs;
781 if (flags & CONTEXT_FLOATING_POINT)
783 to->u.FltSave = from->u.FltSave;
785 if (flags & CONTEXT_DEBUG_REGISTERS)
794 #elif defined(__sparc__)
795 flags &= ~CONTEXT_SPARC; /* get rid of CPU id */
796 if (flags & CONTEXT_CONTROL)
805 if (flags & CONTEXT_INTEGER)
840 if (flags & CONTEXT_FLOATING_POINT)
844 #elif defined(__powerpc__)
846 if (flags & CONTEXT_CONTROL)
852 if (flags & CONTEXT_INTEGER)
854 to->Gpr0 = from->Gpr0;
855 to->Gpr1 = from->Gpr1;
856 to->Gpr2 = from->Gpr2;
857 to->Gpr3 = from->Gpr3;
858 to->Gpr4 = from->Gpr4;
859 to->Gpr5 = from->Gpr5;
860 to->Gpr6 = from->Gpr6;
861 to->Gpr7 = from->Gpr7;
862 to->Gpr8 = from->Gpr8;
863 to->Gpr9 = from->Gpr9;
864 to->Gpr10 = from->Gpr10;
865 to->Gpr11 = from->Gpr11;
866 to->Gpr12 = from->Gpr12;
867 to->Gpr13 = from->Gpr13;
868 to->Gpr14 = from->Gpr14;
869 to->Gpr15 = from->Gpr15;
870 to->Gpr16 = from->Gpr16;
871 to->Gpr17 = from->Gpr17;
872 to->Gpr18 = from->Gpr18;
873 to->Gpr19 = from->Gpr19;
874 to->Gpr20 = from->Gpr20;
875 to->Gpr21 = from->Gpr21;
876 to->Gpr22 = from->Gpr22;
877 to->Gpr23 = from->Gpr23;
878 to->Gpr24 = from->Gpr24;
879 to->Gpr25 = from->Gpr25;
880 to->Gpr26 = from->Gpr26;
881 to->Gpr27 = from->Gpr27;
882 to->Gpr28 = from->Gpr28;
883 to->Gpr29 = from->Gpr29;
884 to->Gpr30 = from->Gpr30;
885 to->Gpr31 = from->Gpr31;
889 if (flags & CONTEXT_FLOATING_POINT)
891 to->Fpr0 = from->Fpr0;
892 to->Fpr1 = from->Fpr1;
893 to->Fpr2 = from->Fpr2;
894 to->Fpr3 = from->Fpr3;
895 to->Fpr4 = from->Fpr4;
896 to->Fpr5 = from->Fpr5;
897 to->Fpr6 = from->Fpr6;
898 to->Fpr7 = from->Fpr7;
899 to->Fpr8 = from->Fpr8;
900 to->Fpr9 = from->Fpr9;
901 to->Fpr10 = from->Fpr10;
902 to->Fpr11 = from->Fpr11;
903 to->Fpr12 = from->Fpr12;
904 to->Fpr13 = from->Fpr13;
905 to->Fpr14 = from->Fpr14;
906 to->Fpr15 = from->Fpr15;
907 to->Fpr16 = from->Fpr16;
908 to->Fpr17 = from->Fpr17;
909 to->Fpr18 = from->Fpr18;
910 to->Fpr19 = from->Fpr19;
911 to->Fpr20 = from->Fpr20;
912 to->Fpr21 = from->Fpr21;
913 to->Fpr22 = from->Fpr22;
914 to->Fpr23 = from->Fpr23;
915 to->Fpr24 = from->Fpr24;
916 to->Fpr25 = from->Fpr25;
917 to->Fpr26 = from->Fpr26;
918 to->Fpr27 = from->Fpr27;
919 to->Fpr28 = from->Fpr28;
920 to->Fpr29 = from->Fpr29;
921 to->Fpr30 = from->Fpr30;
922 to->Fpr31 = from->Fpr31;
923 to->Fpscr = from->Fpscr;
926 #error You must implement context copying for your CPU
931 /***********************************************************************
932 * NtGetContextThread (NTDLL.@)
933 * ZwGetContextThread (NTDLL.@)
935 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
940 DWORD needed_flags = context->ContextFlags;
941 BOOL self = (handle == GetCurrentThread());
944 /* on i386 debug registers always require a server call */
945 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)) self = FALSE;
950 SERVER_START_REQ( get_thread_context )
952 req->handle = handle;
953 req->flags = context->ContextFlags;
955 wine_server_set_reply( req, &ctx, sizeof(ctx) );
956 ret = wine_server_call( req );
961 if (ret == STATUS_PENDING)
963 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
965 for (i = 0; i < 100; i++)
967 SERVER_START_REQ( get_thread_context )
969 req->handle = handle;
970 req->flags = context->ContextFlags;
972 wine_server_set_reply( req, &ctx, sizeof(ctx) );
973 ret = wine_server_call( req );
976 if (ret != STATUS_PENDING) break;
979 NtResumeThread( handle, &dummy );
981 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
984 copy_context( context, &ctx, context->ContextFlags & ctx.ContextFlags );
985 needed_flags &= ~ctx.ContextFlags;
992 get_cpu_context( &ctx );
993 copy_context( context, &ctx, ctx.ContextFlags & needed_flags );
996 /* update the cached version of the debug registers */
997 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
999 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1000 regs->dr0 = context->Dr0;
1001 regs->dr1 = context->Dr1;
1002 regs->dr2 = context->Dr2;
1003 regs->dr3 = context->Dr3;
1004 regs->dr6 = context->Dr6;
1005 regs->dr7 = context->Dr7;
1009 return STATUS_SUCCESS;
1013 /******************************************************************************
1014 * NtQueryInformationThread (NTDLL.@)
1015 * ZwQueryInformationThread (NTDLL.@)
1017 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
1018 void *data, ULONG length, ULONG *ret_len )
1024 case ThreadBasicInformation:
1026 THREAD_BASIC_INFORMATION info;
1028 SERVER_START_REQ( get_thread_info )
1030 req->handle = handle;
1032 if (!(status = wine_server_call( req )))
1034 info.ExitStatus = reply->exit_code;
1035 info.TebBaseAddress = reply->teb;
1036 info.ClientId.UniqueProcess = (HANDLE)reply->pid;
1037 info.ClientId.UniqueThread = (HANDLE)reply->tid;
1038 info.AffinityMask = reply->affinity;
1039 info.Priority = reply->priority;
1040 info.BasePriority = reply->priority; /* FIXME */
1044 if (status == STATUS_SUCCESS)
1046 if (data) memcpy( data, &info, min( length, sizeof(info) ));
1047 if (ret_len) *ret_len = min( length, sizeof(info) );
1053 KERNEL_USER_TIMES kusrt;
1054 /* We need to do a server call to get the creation time or exit time */
1055 /* This works on any thread */
1056 SERVER_START_REQ( get_thread_info )
1058 req->handle = handle;
1060 status = wine_server_call( req );
1061 if (status == STATUS_SUCCESS)
1063 NTDLL_from_server_abstime( &kusrt.CreateTime, &reply->creation_time );
1064 NTDLL_from_server_abstime( &kusrt.ExitTime, &reply->exit_time );
1068 if (status == STATUS_SUCCESS)
1070 /* We call times(2) for kernel time or user time */
1071 /* We can only (portably) do this for the current thread */
1072 if (handle == GetCurrentThread())
1074 struct tms time_buf;
1075 long clocks_per_sec = sysconf(_SC_CLK_TCK);
1078 kusrt.KernelTime.QuadPart = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
1079 kusrt.UserTime.QuadPart = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
1083 kusrt.KernelTime.QuadPart = 0;
1084 kusrt.UserTime.QuadPart = 0;
1085 FIXME("Cannot get kerneltime or usertime of other threads\n");
1087 if (data) memcpy( data, &kusrt, min( length, sizeof(kusrt) ));
1088 if (ret_len) *ret_len = min( length, sizeof(kusrt) );
1092 case ThreadDescriptorTableEntry:
1095 THREAD_DESCRIPTOR_INFORMATION* tdi = data;
1096 if (length < sizeof(*tdi))
1097 status = STATUS_INFO_LENGTH_MISMATCH;
1098 else if (!(tdi->Selector & 4)) /* GDT selector */
1100 unsigned sel = tdi->Selector & ~3; /* ignore RPL */
1101 status = STATUS_SUCCESS;
1102 if (!sel) /* null selector */
1103 memset( &tdi->Entry, 0, sizeof(tdi->Entry) );
1106 tdi->Entry.BaseLow = 0;
1107 tdi->Entry.HighWord.Bits.BaseMid = 0;
1108 tdi->Entry.HighWord.Bits.BaseHi = 0;
1109 tdi->Entry.LimitLow = 0xffff;
1110 tdi->Entry.HighWord.Bits.LimitHi = 0xf;
1111 tdi->Entry.HighWord.Bits.Dpl = 3;
1112 tdi->Entry.HighWord.Bits.Sys = 0;
1113 tdi->Entry.HighWord.Bits.Pres = 1;
1114 tdi->Entry.HighWord.Bits.Granularity = 1;
1115 tdi->Entry.HighWord.Bits.Default_Big = 1;
1116 tdi->Entry.HighWord.Bits.Type = 0x12;
1117 /* it has to be one of the system GDT selectors */
1118 if (sel != (wine_get_ds() & ~3) && sel != (wine_get_ss() & ~3))
1120 if (sel == (wine_get_cs() & ~3))
1121 tdi->Entry.HighWord.Bits.Type |= 8; /* code segment */
1122 else status = STATUS_ACCESS_DENIED;
1128 SERVER_START_REQ( get_selector_entry )
1130 req->handle = handle;
1131 req->entry = tdi->Selector >> 3;
1132 status = wine_server_call( req );
1135 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
1136 status = STATUS_ACCESS_VIOLATION;
1139 wine_ldt_set_base ( &tdi->Entry, (void *)reply->base );
1140 wine_ldt_set_limit( &tdi->Entry, reply->limit );
1141 wine_ldt_set_flags( &tdi->Entry, reply->flags );
1147 if (status == STATUS_SUCCESS && ret_len)
1148 /* yes, that's a bit strange, but it's the way it is */
1149 *ret_len = sizeof(LDT_ENTRY);
1151 status = STATUS_NOT_IMPLEMENTED;
1155 case ThreadAmILastThread:
1157 SERVER_START_REQ(get_thread_info)
1159 req->handle = handle;
1161 status = wine_server_call( req );
1162 if (status == STATUS_SUCCESS)
1164 BOOLEAN last = reply->last;
1165 if (data) memcpy( data, &last, min( length, sizeof(last) ));
1166 if (ret_len) *ret_len = min( length, sizeof(last) );
1172 case ThreadPriority:
1173 case ThreadBasePriority:
1174 case ThreadAffinityMask:
1175 case ThreadImpersonationToken:
1176 case ThreadEnableAlignmentFaultFixup:
1177 case ThreadEventPair_Reusable:
1178 case ThreadQuerySetWin32StartAddress:
1179 case ThreadZeroTlsCell:
1180 case ThreadPerformanceCount:
1181 case ThreadIdealProcessor:
1182 case ThreadPriorityBoost:
1183 case ThreadSetTlsArrayAddress:
1184 case ThreadIsIoPending:
1186 FIXME( "info class %d not supported yet\n", class );
1187 return STATUS_NOT_IMPLEMENTED;
1192 /******************************************************************************
1193 * NtSetInformationThread (NTDLL.@)
1194 * ZwSetInformationThread (NTDLL.@)
1196 NTSTATUS WINAPI NtSetInformationThread( HANDLE handle, THREADINFOCLASS class,
1197 LPCVOID data, ULONG length )
1202 case ThreadZeroTlsCell:
1203 if (handle == GetCurrentThread())
1208 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1209 index = *(const DWORD *)data;
1210 if (index < TLS_MINIMUM_AVAILABLE)
1212 RtlAcquirePebLock();
1213 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1215 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1216 teb->TlsSlots[index] = 0;
1218 RtlReleasePebLock();
1222 index -= TLS_MINIMUM_AVAILABLE;
1223 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
1224 return STATUS_INVALID_PARAMETER;
1225 RtlAcquirePebLock();
1226 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1228 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1229 if (teb->TlsExpansionSlots) teb->TlsExpansionSlots[index] = 0;
1231 RtlReleasePebLock();
1233 return STATUS_SUCCESS;
1235 FIXME( "ZeroTlsCell not supported on other threads\n" );
1236 return STATUS_NOT_IMPLEMENTED;
1238 case ThreadImpersonationToken:
1240 const HANDLE *phToken = data;
1241 if (length != sizeof(HANDLE)) return STATUS_INVALID_PARAMETER;
1242 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken );
1243 SERVER_START_REQ( set_thread_info )
1245 req->handle = handle;
1246 req->token = *phToken;
1247 req->mask = SET_THREAD_INFO_TOKEN;
1248 status = wine_server_call( req );
1253 case ThreadBasePriority:
1255 const DWORD *pprio = data;
1256 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1257 SERVER_START_REQ( set_thread_info )
1259 req->handle = handle;
1260 req->priority = *pprio;
1261 req->mask = SET_THREAD_INFO_PRIORITY;
1262 status = wine_server_call( req );
1267 case ThreadAffinityMask:
1269 const DWORD *paff = data;
1270 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1271 SERVER_START_REQ( set_thread_info )
1273 req->handle = handle;
1274 req->affinity = *paff;
1275 req->mask = SET_THREAD_INFO_AFFINITY;
1276 status = wine_server_call( req );
1281 case ThreadBasicInformation:
1283 case ThreadPriority:
1284 case ThreadDescriptorTableEntry:
1285 case ThreadEnableAlignmentFaultFixup:
1286 case ThreadEventPair_Reusable:
1287 case ThreadQuerySetWin32StartAddress:
1288 case ThreadPerformanceCount:
1289 case ThreadAmILastThread:
1290 case ThreadIdealProcessor:
1291 case ThreadPriorityBoost:
1292 case ThreadSetTlsArrayAddress:
1293 case ThreadIsIoPending:
1295 FIXME( "info class %d not supported yet\n", class );
1296 return STATUS_NOT_IMPLEMENTED;
1301 /**********************************************************************
1302 * NtCurrentTeb (NTDLL.@)
1304 #if defined(__i386__) && defined(__GNUC__)
1306 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
1308 #elif defined(__i386__) && defined(_MSC_VER)
1310 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
1314 /**********************************************************************/
1316 TEB * WINAPI NtCurrentTeb(void)
1318 return pthread_functions.get_current_teb();
1321 #endif /* __i386__ */