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
28 #ifdef HAVE_SYS_TIMES_H
29 #include <sys/times.h>
32 #define NONAMELESSUNION
34 #define WIN32_NO_STATUS
37 #include "wine/library.h"
38 #include "wine/server.h"
39 #include "wine/pthread.h"
40 #include "wine/debug.h"
41 #include "ntdll_misc.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(thread);
45 /* info passed to a starting thread */
48 struct wine_pthread_thread_info pthread_info;
49 PRTL_THREAD_START_ROUTINE entry_point;
54 static PEB_LDR_DATA ldr;
55 static RTL_USER_PROCESS_PARAMETERS params; /* default parameters if no parent */
56 static WCHAR current_dir[MAX_NT_PATH_LENGTH];
57 static RTL_BITMAP tls_bitmap;
58 static RTL_BITMAP tls_expansion_bitmap;
59 static LIST_ENTRY tls_links;
60 static size_t sigstack_total_size;
62 struct wine_pthread_functions pthread_functions = { NULL };
64 /***********************************************************************
67 static inline NTSTATUS init_teb( TEB *teb )
69 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
70 struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
72 teb->Tib.ExceptionList = (void *)~0UL;
73 teb->Tib.StackBase = (void *)~0UL;
74 teb->Tib.Self = &teb->Tib;
76 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
77 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
79 if (!(thread_regs->fs = wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS;
80 thread_data->request_fd = -1;
81 thread_data->reply_fd = -1;
82 thread_data->wait_fd[0] = -1;
83 thread_data->wait_fd[1] = -1;
85 return STATUS_SUCCESS;
89 /***********************************************************************
92 static inline void free_teb( TEB *teb )
96 struct ntdll_thread_regs *thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
98 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
99 wine_ldt_free_fs( thread_regs->fs );
100 munmap( teb, sigstack_total_size );
104 /***********************************************************************
107 * Make sure the unicode string doesn't point beyond the end pointer
109 static inline void fix_unicode_string( UNICODE_STRING *str, char *end_ptr )
111 if ((char *)str->Buffer >= end_ptr)
113 str->Length = str->MaximumLength = 0;
117 if ((char *)str->Buffer + str->MaximumLength > end_ptr)
119 str->MaximumLength = (end_ptr - (char *)str->Buffer) & ~(sizeof(WCHAR) - 1);
121 if (str->Length >= str->MaximumLength)
123 if (str->MaximumLength >= sizeof(WCHAR))
124 str->Length = str->MaximumLength - sizeof(WCHAR);
126 str->Length = str->MaximumLength = 0;
131 /***********************************************************************
132 * init_user_process_params
134 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
136 static NTSTATUS init_user_process_params( SIZE_T info_size, HANDLE *exe_file )
141 RTL_USER_PROCESS_PARAMETERS *params = NULL;
143 status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms, 0, &info_size,
144 MEM_COMMIT, PAGE_READWRITE );
145 if (status != STATUS_SUCCESS) return status;
147 params->AllocationSize = info_size;
148 peb.ProcessParameters = params;
150 SERVER_START_REQ( get_startup_info )
152 wine_server_set_reply( req, params, info_size );
153 if (!(status = wine_server_call( req )))
155 info_size = wine_server_reply_size( reply );
156 *exe_file = reply->exe_file;
157 params->hStdInput = reply->hstdin;
158 params->hStdOutput = reply->hstdout;
159 params->hStdError = reply->hstderr;
163 if (status != STATUS_SUCCESS) return status;
165 if (params->Size > info_size) params->Size = info_size;
167 /* make sure the strings are valid */
168 fix_unicode_string( ¶ms->CurrentDirectory.DosPath, (char *)info_size );
169 fix_unicode_string( ¶ms->DllPath, (char *)info_size );
170 fix_unicode_string( ¶ms->ImagePathName, (char *)info_size );
171 fix_unicode_string( ¶ms->CommandLine, (char *)info_size );
172 fix_unicode_string( ¶ms->WindowTitle, (char *)info_size );
173 fix_unicode_string( ¶ms->Desktop, (char *)info_size );
174 fix_unicode_string( ¶ms->ShellInfo, (char *)info_size );
175 fix_unicode_string( ¶ms->RuntimeInfo, (char *)info_size );
177 /* environment needs to be a separate memory block */
178 env_size = info_size - params->Size;
179 if (!env_size) env_size = 1;
181 status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &env_size,
182 MEM_COMMIT, PAGE_READWRITE );
183 if (status != STATUS_SUCCESS) return status;
184 memcpy( ptr, (char *)params + params->Size, info_size - params->Size );
185 params->Environment = ptr;
187 RtlNormalizeProcessParams( params );
192 /***********************************************************************
195 * Setup the initial thread.
197 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
199 HANDLE thread_init(void)
205 struct ntdll_thread_data *thread_data;
206 struct ntdll_thread_regs *thread_regs;
207 struct wine_pthread_thread_info thread_info;
208 static struct debug_info debug_info; /* debug info for initial thread */
210 peb.NumberOfProcessors = 1;
211 peb.ProcessParameters = ¶ms;
212 peb.TlsBitmap = &tls_bitmap;
213 peb.TlsExpansionBitmap = &tls_expansion_bitmap;
215 params.CurrentDirectory.DosPath.Buffer = current_dir;
216 params.CurrentDirectory.DosPath.MaximumLength = sizeof(current_dir);
217 params.wShowWindow = 1; /* SW_SHOWNORMAL */
218 RtlInitializeBitMap( &tls_bitmap, peb.TlsBitmapBits, sizeof(peb.TlsBitmapBits) * 8 );
219 RtlInitializeBitMap( &tls_expansion_bitmap, peb.TlsExpansionBitmapBits,
220 sizeof(peb.TlsExpansionBitmapBits) * 8 );
221 InitializeListHead( &ldr.InLoadOrderModuleList );
222 InitializeListHead( &ldr.InMemoryOrderModuleList );
223 InitializeListHead( &ldr.InInitializationOrderModuleList );
224 InitializeListHead( &tls_links );
226 sigstack_total_size = get_signal_stack_total_size();
227 thread_info.teb_size = sigstack_total_size;
228 VIRTUAL_alloc_teb( &addr, thread_info.teb_size, TRUE );
231 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
232 thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
233 thread_data->debug_info = &debug_info;
234 InsertHeadList( &tls_links, &teb->TlsLinks );
236 thread_info.stack_base = NULL;
237 thread_info.stack_size = 0;
238 thread_info.teb_base = teb;
239 thread_info.teb_sel = thread_regs->fs;
240 wine_pthread_get_functions( &pthread_functions, sizeof(pthread_functions) );
241 pthread_functions.init_current_teb( &thread_info );
242 pthread_functions.init_thread( &thread_info );
244 debug_info.str_pos = debug_info.strings;
245 debug_info.out_pos = debug_info.output;
248 /* setup the server connection */
249 server_init_process();
250 info_size = server_init_thread( thread_info.pid, thread_info.tid, NULL );
252 /* create the process heap */
253 if (!(peb.ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL )))
255 MESSAGE( "wine: failed to create the process heap\n" );
259 /* allocate user parameters */
262 init_user_process_params( info_size, &exe_file );
266 /* This is wine specific: we have no parent (we're started from unix)
267 * so, create a simple console with bare handles to unix stdio
269 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, OBJ_INHERIT, ¶ms.hStdInput );
270 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, ¶ms.hStdOutput );
271 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, ¶ms.hStdError );
277 /***********************************************************************
280 * Startup routine for a newly created thread.
282 static void start_thread( struct wine_pthread_thread_info *info )
284 TEB *teb = info->teb_base;
285 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
286 struct startup_info *startup_info = (struct startup_info *)info;
287 PRTL_THREAD_START_ROUTINE func = startup_info->entry_point;
288 void *arg = startup_info->entry_arg;
289 struct debug_info debug_info;
290 SIZE_T size, page_size = getpagesize();
292 debug_info.str_pos = debug_info.strings;
293 debug_info.out_pos = debug_info.output;
294 thread_data->debug_info = &debug_info;
296 pthread_functions.init_current_teb( info );
298 server_init_thread( info->pid, info->tid, func );
299 pthread_functions.init_thread( info );
301 /* allocate a memory view for the stack */
302 size = info->stack_size;
303 teb->DeallocationStack = info->stack_base;
304 NtAllocateVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, 0,
305 &size, MEM_SYSTEM, PAGE_READWRITE );
306 /* limit is lower than base since the stack grows down */
307 teb->Tib.StackBase = (char *)info->stack_base + info->stack_size;
308 teb->Tib.StackLimit = (char *)info->stack_base + page_size;
310 /* setup the guard page */
312 NtProtectVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, &size, PAGE_NOACCESS, NULL );
313 RtlFreeHeap( GetProcessHeap(), 0, info );
316 InsertHeadList( &tls_links, &teb->TlsLinks );
323 /***********************************************************************
324 * RtlCreateUserThread (NTDLL.@)
326 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
327 BOOLEAN suspended, PVOID stack_addr,
328 SIZE_T stack_reserve, SIZE_T stack_commit,
329 PRTL_THREAD_START_ROUTINE start, void *param,
330 HANDLE *handle_ptr, CLIENT_ID *id )
332 struct ntdll_thread_data *thread_data;
333 struct ntdll_thread_regs *thread_regs = NULL;
334 struct startup_info *info = NULL;
341 SIZE_T page_size = getpagesize();
343 if( ! is_current_process( process ) )
345 ERR("Unsupported on other process\n");
346 return STATUS_ACCESS_DENIED;
349 if (pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
350 fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
351 wine_server_send_fd( request_pipe[0] );
353 SERVER_START_REQ( new_thread )
355 req->access = THREAD_ALL_ACCESS;
356 req->attributes = 0; /* FIXME */
357 req->suspend = suspended;
358 req->request_fd = request_pipe[0];
359 if (!(status = wine_server_call( req )))
361 handle = reply->handle;
364 close( request_pipe[0] );
368 if (status) goto error;
370 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) )))
372 status = STATUS_NO_MEMORY;
376 info->pthread_info.teb_size = sigstack_total_size;
377 if ((status = VIRTUAL_alloc_teb( &addr, info->pthread_info.teb_size, FALSE ))) goto error;
379 if ((status = init_teb( teb ))) goto error;
381 teb->ClientId.UniqueProcess = (HANDLE)GetCurrentProcessId();
382 teb->ClientId.UniqueThread = (HANDLE)tid;
384 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
385 thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
386 thread_data->request_fd = request_pipe[1];
388 info->pthread_info.teb_base = teb;
389 info->pthread_info.teb_sel = thread_regs->fs;
391 /* inherit debug registers from parent thread */
392 thread_regs->dr0 = ntdll_get_thread_regs()->dr0;
393 thread_regs->dr1 = ntdll_get_thread_regs()->dr1;
394 thread_regs->dr2 = ntdll_get_thread_regs()->dr2;
395 thread_regs->dr3 = ntdll_get_thread_regs()->dr3;
396 thread_regs->dr6 = ntdll_get_thread_regs()->dr6;
397 thread_regs->dr7 = ntdll_get_thread_regs()->dr7;
399 if (!stack_reserve || !stack_commit)
401 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
402 if (!stack_reserve) stack_reserve = nt->OptionalHeader.SizeOfStackReserve;
403 if (!stack_commit) stack_commit = nt->OptionalHeader.SizeOfStackCommit;
405 if (stack_reserve < stack_commit) stack_reserve = stack_commit;
406 stack_reserve += page_size; /* for the guard page */
407 stack_reserve = (stack_reserve + 0xffff) & ~0xffff; /* round to 64K boundary */
408 if (stack_reserve < 1024 * 1024) stack_reserve = 1024 * 1024; /* Xlib needs a large stack */
410 info->pthread_info.stack_base = NULL;
411 info->pthread_info.stack_size = stack_reserve;
412 info->pthread_info.entry = start_thread;
413 info->entry_point = start;
414 info->entry_arg = param;
416 if (pthread_functions.create_thread( &info->pthread_info ) == -1)
418 status = STATUS_NO_MEMORY;
422 if (id) id->UniqueThread = (HANDLE)tid;
423 if (handle_ptr) *handle_ptr = handle;
424 else NtClose( handle );
426 return STATUS_SUCCESS;
429 if (thread_regs) wine_ldt_free_fs( thread_regs->fs );
433 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
435 if (info) RtlFreeHeap( GetProcessHeap(), 0, info );
436 if (handle) NtClose( handle );
437 close( request_pipe[1] );
442 /***********************************************************************
443 * RtlExitUserThread (NTDLL.@)
445 void WINAPI RtlExitUserThread( ULONG status )
448 server_exit_thread( status );
452 /***********************************************************************
453 * NtOpenThread (NTDLL.@)
454 * ZwOpenThread (NTDLL.@)
456 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
457 const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
461 SERVER_START_REQ( open_thread )
463 req->tid = (thread_id_t)id->UniqueThread;
464 req->access = access;
465 req->attributes = attr ? attr->Attributes : 0;
466 ret = wine_server_call( req );
467 *handle = reply->handle;
474 /******************************************************************************
475 * NtSuspendThread (NTDLL.@)
476 * ZwSuspendThread (NTDLL.@)
478 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
482 SERVER_START_REQ( suspend_thread )
484 req->handle = handle;
485 if (!(ret = wine_server_call( req ))) *count = reply->count;
492 /******************************************************************************
493 * NtResumeThread (NTDLL.@)
494 * ZwResumeThread (NTDLL.@)
496 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
500 SERVER_START_REQ( resume_thread )
502 req->handle = handle;
503 if (!(ret = wine_server_call( req ))) *count = reply->count;
510 /******************************************************************************
511 * NtAlertResumeThread (NTDLL.@)
512 * ZwAlertResumeThread (NTDLL.@)
514 NTSTATUS WINAPI NtAlertResumeThread( HANDLE handle, PULONG count )
516 FIXME( "stub: should alert thread %p\n", handle );
517 return NtResumeThread( handle, count );
521 /******************************************************************************
522 * NtAlertThread (NTDLL.@)
523 * ZwAlertThread (NTDLL.@)
525 NTSTATUS WINAPI NtAlertThread( HANDLE handle )
527 FIXME( "stub: %p\n", handle );
528 return STATUS_NOT_IMPLEMENTED;
532 /******************************************************************************
533 * NtTerminateThread (NTDLL.@)
534 * ZwTerminateThread (NTDLL.@)
536 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
541 SERVER_START_REQ( terminate_thread )
543 req->handle = handle;
544 req->exit_code = exit_code;
545 ret = wine_server_call( req );
546 self = !ret && reply->self;
553 if (last) exit( exit_code );
554 else server_abort_thread( exit_code );
560 /******************************************************************************
561 * NtQueueApcThread (NTDLL.@)
563 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
564 ULONG_PTR arg2, ULONG_PTR arg3 )
567 SERVER_START_REQ( queue_apc )
569 req->handle = handle;
572 req->arg1 = (void *)arg1;
573 req->arg2 = (void *)arg2;
574 req->arg3 = (void *)arg3;
575 ret = wine_server_call( req );
582 /***********************************************************************
583 * NtSetContextThread (NTDLL.@)
584 * ZwSetContextThread (NTDLL.@)
586 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
593 /* on i386 debug registers always require a server call */
594 self = (handle == GetCurrentThread());
595 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
597 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
598 self = (regs->dr0 == context->Dr0 && regs->dr1 == context->Dr1 &&
599 regs->dr2 == context->Dr2 && regs->dr3 == context->Dr3 &&
600 regs->dr6 == context->Dr6 && regs->dr7 == context->Dr7);
606 SERVER_START_REQ( set_thread_context )
608 req->handle = handle;
609 req->flags = context->ContextFlags;
611 wine_server_add_data( req, context, sizeof(*context) );
612 ret = wine_server_call( req );
617 if (ret == STATUS_PENDING)
619 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
621 for (i = 0; i < 100; i++)
623 SERVER_START_REQ( set_thread_context )
625 req->handle = handle;
626 req->flags = context->ContextFlags;
628 wine_server_add_data( req, context, sizeof(*context) );
629 ret = wine_server_call( req );
632 if (ret != STATUS_PENDING) break;
635 NtResumeThread( handle, &dummy );
637 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
643 if (self) set_cpu_context( context );
644 return STATUS_SUCCESS;
648 /* copy a context structure according to the flags */
649 static inline void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
652 flags &= ~CONTEXT_i386; /* get rid of CPU id */
653 if (flags & CONTEXT_INTEGER)
662 if (flags & CONTEXT_CONTROL)
667 to->SegCs = from->SegCs;
668 to->SegSs = from->SegSs;
669 to->EFlags = from->EFlags;
671 if (flags & CONTEXT_SEGMENTS)
673 to->SegDs = from->SegDs;
674 to->SegEs = from->SegEs;
675 to->SegFs = from->SegFs;
676 to->SegGs = from->SegGs;
678 if (flags & CONTEXT_DEBUG_REGISTERS)
687 if (flags & CONTEXT_FLOATING_POINT)
689 to->FloatSave = from->FloatSave;
691 #elif defined(__x86_64__)
692 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
693 if (flags & CONTEXT_CONTROL)
698 to->SegCs = from->SegCs;
699 to->SegSs = from->SegSs;
700 to->EFlags = from->EFlags;
701 to->MxCsr = from->MxCsr;
703 if (flags & CONTEXT_INTEGER)
720 if (flags & CONTEXT_SEGMENTS)
722 to->SegDs = from->SegDs;
723 to->SegEs = from->SegEs;
724 to->SegFs = from->SegFs;
725 to->SegGs = from->SegGs;
727 if (flags & CONTEXT_FLOATING_POINT)
729 to->u.FltSave = from->u.FltSave;
731 if (flags & CONTEXT_DEBUG_REGISTERS)
740 #elif defined(__sparc__)
741 flags &= ~CONTEXT_SPARC; /* get rid of CPU id */
742 if (flags & CONTEXT_CONTROL)
751 if (flags & CONTEXT_INTEGER)
786 if (flags & CONTEXT_FLOATING_POINT)
790 #elif defined(__powerpc__)
792 if (flags & CONTEXT_CONTROL)
798 if (flags & CONTEXT_INTEGER)
800 to->Gpr0 = from->Gpr0;
801 to->Gpr1 = from->Gpr1;
802 to->Gpr2 = from->Gpr2;
803 to->Gpr3 = from->Gpr3;
804 to->Gpr4 = from->Gpr4;
805 to->Gpr5 = from->Gpr5;
806 to->Gpr6 = from->Gpr6;
807 to->Gpr7 = from->Gpr7;
808 to->Gpr8 = from->Gpr8;
809 to->Gpr9 = from->Gpr9;
810 to->Gpr10 = from->Gpr10;
811 to->Gpr11 = from->Gpr11;
812 to->Gpr12 = from->Gpr12;
813 to->Gpr13 = from->Gpr13;
814 to->Gpr14 = from->Gpr14;
815 to->Gpr15 = from->Gpr15;
816 to->Gpr16 = from->Gpr16;
817 to->Gpr17 = from->Gpr17;
818 to->Gpr18 = from->Gpr18;
819 to->Gpr19 = from->Gpr19;
820 to->Gpr20 = from->Gpr20;
821 to->Gpr21 = from->Gpr21;
822 to->Gpr22 = from->Gpr22;
823 to->Gpr23 = from->Gpr23;
824 to->Gpr24 = from->Gpr24;
825 to->Gpr25 = from->Gpr25;
826 to->Gpr26 = from->Gpr26;
827 to->Gpr27 = from->Gpr27;
828 to->Gpr28 = from->Gpr28;
829 to->Gpr29 = from->Gpr29;
830 to->Gpr30 = from->Gpr30;
831 to->Gpr31 = from->Gpr31;
835 if (flags & CONTEXT_FLOATING_POINT)
837 to->Fpr0 = from->Fpr0;
838 to->Fpr1 = from->Fpr1;
839 to->Fpr2 = from->Fpr2;
840 to->Fpr3 = from->Fpr3;
841 to->Fpr4 = from->Fpr4;
842 to->Fpr5 = from->Fpr5;
843 to->Fpr6 = from->Fpr6;
844 to->Fpr7 = from->Fpr7;
845 to->Fpr8 = from->Fpr8;
846 to->Fpr9 = from->Fpr9;
847 to->Fpr10 = from->Fpr10;
848 to->Fpr11 = from->Fpr11;
849 to->Fpr12 = from->Fpr12;
850 to->Fpr13 = from->Fpr13;
851 to->Fpr14 = from->Fpr14;
852 to->Fpr15 = from->Fpr15;
853 to->Fpr16 = from->Fpr16;
854 to->Fpr17 = from->Fpr17;
855 to->Fpr18 = from->Fpr18;
856 to->Fpr19 = from->Fpr19;
857 to->Fpr20 = from->Fpr20;
858 to->Fpr21 = from->Fpr21;
859 to->Fpr22 = from->Fpr22;
860 to->Fpr23 = from->Fpr23;
861 to->Fpr24 = from->Fpr24;
862 to->Fpr25 = from->Fpr25;
863 to->Fpr26 = from->Fpr26;
864 to->Fpr27 = from->Fpr27;
865 to->Fpr28 = from->Fpr28;
866 to->Fpr29 = from->Fpr29;
867 to->Fpr30 = from->Fpr30;
868 to->Fpr31 = from->Fpr31;
869 to->Fpscr = from->Fpscr;
872 #error You must implement context copying for your CPU
877 /***********************************************************************
878 * NtGetContextThread (NTDLL.@)
879 * ZwGetContextThread (NTDLL.@)
881 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
888 SERVER_START_REQ( get_thread_context )
890 req->handle = handle;
891 req->flags = context->ContextFlags;
893 wine_server_set_reply( req, &ctx, sizeof(ctx) );
894 ret = wine_server_call( req );
899 if (ret == STATUS_PENDING)
901 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
903 for (i = 0; i < 100; i++)
905 SERVER_START_REQ( get_thread_context )
907 req->handle = handle;
908 req->flags = context->ContextFlags;
910 wine_server_set_reply( req, &ctx, sizeof(ctx) );
911 ret = wine_server_call( req );
914 if (ret != STATUS_PENDING) break;
917 NtResumeThread( handle, &dummy );
921 if (ret == STATUS_SUCCESS)
923 copy_context( context, &ctx, context->ContextFlags );
925 /* update the cached version of the debug registers */
926 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
928 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
929 regs->dr0 = context->Dr0;
930 regs->dr1 = context->Dr1;
931 regs->dr2 = context->Dr2;
932 regs->dr3 = context->Dr3;
933 regs->dr6 = context->Dr6;
934 regs->dr7 = context->Dr7;
938 else if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
943 /******************************************************************************
944 * NtQueryInformationThread (NTDLL.@)
945 * ZwQueryInformationThread (NTDLL.@)
947 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
948 void *data, ULONG length, ULONG *ret_len )
954 case ThreadBasicInformation:
956 THREAD_BASIC_INFORMATION info;
958 SERVER_START_REQ( get_thread_info )
960 req->handle = handle;
962 if (!(status = wine_server_call( req )))
964 info.ExitStatus = reply->exit_code;
965 info.TebBaseAddress = reply->teb;
966 info.ClientId.UniqueProcess = (HANDLE)reply->pid;
967 info.ClientId.UniqueThread = (HANDLE)reply->tid;
968 info.AffinityMask = reply->affinity;
969 info.Priority = reply->priority;
970 info.BasePriority = reply->priority; /* FIXME */
974 if (status == STATUS_SUCCESS)
976 if (data) memcpy( data, &info, min( length, sizeof(info) ));
977 if (ret_len) *ret_len = min( length, sizeof(info) );
983 KERNEL_USER_TIMES kusrt;
984 /* We need to do a server call to get the creation time or exit time */
985 /* This works on any thread */
986 SERVER_START_REQ( get_thread_info )
988 req->handle = handle;
990 status = wine_server_call( req );
991 if (status == STATUS_SUCCESS)
993 RtlSecondsSince1970ToTime( reply->creation_time, &kusrt.CreateTime );
994 RtlSecondsSince1970ToTime( reply->exit_time, &kusrt.ExitTime );
998 if (status == STATUS_SUCCESS)
1000 /* We call times(2) for kernel time or user time */
1001 /* We can only (portably) do this for the current thread */
1002 if (handle == GetCurrentThread())
1004 struct tms time_buf;
1005 long clocks_per_sec = sysconf(_SC_CLK_TCK);
1008 kusrt.KernelTime.QuadPart = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
1009 kusrt.UserTime.QuadPart = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
1013 kusrt.KernelTime.QuadPart = 0;
1014 kusrt.UserTime.QuadPart = 0;
1015 FIXME("Cannot get kerneltime or usertime of other threads\n");
1017 if (data) memcpy( data, &kusrt, min( length, sizeof(kusrt) ));
1018 if (ret_len) *ret_len = min( length, sizeof(kusrt) );
1022 case ThreadPriority:
1023 case ThreadBasePriority:
1024 case ThreadAffinityMask:
1025 case ThreadImpersonationToken:
1026 case ThreadDescriptorTableEntry:
1027 case ThreadEnableAlignmentFaultFixup:
1028 case ThreadEventPair_Reusable:
1029 case ThreadQuerySetWin32StartAddress:
1030 case ThreadZeroTlsCell:
1031 case ThreadPerformanceCount:
1032 case ThreadAmILastThread:
1033 case ThreadIdealProcessor:
1034 case ThreadPriorityBoost:
1035 case ThreadSetTlsArrayAddress:
1036 case ThreadIsIoPending:
1038 FIXME( "info class %d not supported yet\n", class );
1039 return STATUS_NOT_IMPLEMENTED;
1044 /******************************************************************************
1045 * NtSetInformationThread (NTDLL.@)
1046 * ZwSetInformationThread (NTDLL.@)
1048 NTSTATUS WINAPI NtSetInformationThread( HANDLE handle, THREADINFOCLASS class,
1049 LPCVOID data, ULONG length )
1054 case ThreadZeroTlsCell:
1055 if (handle == GetCurrentThread())
1060 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1061 index = *(const DWORD *)data;
1062 if (index < TLS_MINIMUM_AVAILABLE)
1064 RtlAcquirePebLock();
1065 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1067 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1068 teb->TlsSlots[index] = 0;
1070 RtlReleasePebLock();
1074 index -= TLS_MINIMUM_AVAILABLE;
1075 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
1076 return STATUS_INVALID_PARAMETER;
1077 RtlAcquirePebLock();
1078 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1080 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1081 if (teb->TlsExpansionSlots) teb->TlsExpansionSlots[index] = 0;
1083 RtlReleasePebLock();
1085 return STATUS_SUCCESS;
1087 FIXME( "ZeroTlsCell not supported on other threads\n" );
1088 return STATUS_NOT_IMPLEMENTED;
1090 case ThreadImpersonationToken:
1092 const HANDLE *phToken = data;
1093 if (length != sizeof(HANDLE)) return STATUS_INVALID_PARAMETER;
1094 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken );
1095 SERVER_START_REQ( set_thread_info )
1097 req->handle = handle;
1098 req->token = *phToken;
1099 req->mask = SET_THREAD_INFO_TOKEN;
1100 status = wine_server_call( req );
1105 case ThreadBasePriority:
1107 const DWORD *pprio = data;
1108 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1109 SERVER_START_REQ( set_thread_info )
1111 req->handle = handle;
1112 req->priority = *pprio;
1113 req->mask = SET_THREAD_INFO_PRIORITY;
1114 status = wine_server_call( req );
1119 case ThreadAffinityMask:
1121 const DWORD *paff = data;
1122 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1123 SERVER_START_REQ( set_thread_info )
1125 req->handle = handle;
1126 req->affinity = *paff;
1127 req->mask = SET_THREAD_INFO_AFFINITY;
1128 status = wine_server_call( req );
1133 case ThreadBasicInformation:
1135 case ThreadPriority:
1136 case ThreadDescriptorTableEntry:
1137 case ThreadEnableAlignmentFaultFixup:
1138 case ThreadEventPair_Reusable:
1139 case ThreadQuerySetWin32StartAddress:
1140 case ThreadPerformanceCount:
1141 case ThreadAmILastThread:
1142 case ThreadIdealProcessor:
1143 case ThreadPriorityBoost:
1144 case ThreadSetTlsArrayAddress:
1145 case ThreadIsIoPending:
1147 FIXME( "info class %d not supported yet\n", class );
1148 return STATUS_NOT_IMPLEMENTED;
1153 /**********************************************************************
1154 * NtCurrentTeb (NTDLL.@)
1156 #if defined(__i386__) && defined(__GNUC__)
1158 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
1160 #elif defined(__i386__) && defined(_MSC_VER)
1162 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
1166 /**********************************************************************/
1168 TEB * WINAPI NtCurrentTeb(void)
1170 return pthread_functions.get_current_teb();
1173 #endif /* __i386__ */