kernel: Moved the get_startup_info call to ntdll.
[wine/gsoc_dplay.git] / dlls / ntdll / thread.c
blob5bfdceef4708084a30f6e0a8194f35894152c802
1 /*
2 * NT threads support
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_MMAN_H
26 #include <sys/mman.h>
27 #endif
28 #ifdef HAVE_SYS_TIMES_H
29 #include <sys/times.h>
30 #endif
32 #define NONAMELESSUNION
33 #include "ntstatus.h"
34 #define WIN32_NO_STATUS
35 #include "thread.h"
36 #include "winternl.h"
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 */
46 struct startup_info
48 struct wine_pthread_thread_info pthread_info;
49 PRTL_THREAD_START_ROUTINE entry_point;
50 void *entry_arg;
53 static PEB peb;
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 /***********************************************************************
65 * init_teb
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;
75 teb->Peb = &peb;
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 /***********************************************************************
90 * free_teb
92 static inline void free_teb( TEB *teb )
94 SIZE_T size = 0;
95 void *addr = 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 /***********************************************************************
105 * fix_unicode_string
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;
114 str->Buffer = NULL;
115 return;
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);
125 else
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 )
138 void *ptr;
139 SIZE_T env_size;
140 NTSTATUS status;
141 RTL_USER_PROCESS_PARAMETERS *params = NULL;
143 status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&params, 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;
162 SERVER_END_REQ;
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( &params->CurrentDirectory.DosPath, (char *)info_size );
169 fix_unicode_string( &params->DllPath, (char *)info_size );
170 fix_unicode_string( &params->ImagePathName, (char *)info_size );
171 fix_unicode_string( &params->CommandLine, (char *)info_size );
172 fix_unicode_string( &params->WindowTitle, (char *)info_size );
173 fix_unicode_string( &params->Desktop, (char *)info_size );
174 fix_unicode_string( &params->ShellInfo, (char *)info_size );
175 fix_unicode_string( &params->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;
180 ptr = NULL;
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 );
188 return status;
192 /***********************************************************************
193 * thread_init
195 * Setup the initial thread.
197 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
199 HANDLE thread_init(void)
201 TEB *teb;
202 void *addr;
203 SIZE_T info_size;
204 HANDLE exe_file = 0;
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 = &params;
212 peb.TlsBitmap = &tls_bitmap;
213 peb.TlsExpansionBitmap = &tls_expansion_bitmap;
214 peb.LdrData = &ldr;
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 );
229 teb = addr;
230 init_teb( teb );
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;
246 debug_init();
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" );
256 exit(1);
259 /* allocate user parameters */
260 if (info_size)
262 init_user_process_params( info_size, &exe_file );
264 else
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, &params.hStdInput );
270 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdOutput );
271 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdError );
273 return exe_file;
277 /***********************************************************************
278 * start_thread
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 );
297 SIGNAL_Init();
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 */
311 size = page_size;
312 NtProtectVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, &size, PAGE_NOACCESS, NULL );
313 RtlFreeHeap( GetProcessHeap(), 0, info );
315 RtlAcquirePebLock();
316 InsertHeadList( &tls_links, &teb->TlsLinks );
317 RtlReleasePebLock();
319 func( arg );
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;
335 void *addr;
336 HANDLE handle = 0;
337 TEB *teb;
338 DWORD tid = 0;
339 int request_pipe[2];
340 NTSTATUS status;
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;
362 tid = reply->tid;
364 close( request_pipe[0] );
366 SERVER_END_REQ;
368 if (status) goto error;
370 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) )))
372 status = STATUS_NO_MEMORY;
373 goto error;
376 info->pthread_info.teb_size = sigstack_total_size;
377 if ((status = VIRTUAL_alloc_teb( &addr, info->pthread_info.teb_size, FALSE ))) goto error;
378 teb = addr;
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;
419 goto error;
422 if (id) id->UniqueThread = (HANDLE)tid;
423 if (handle_ptr) *handle_ptr = handle;
424 else NtClose( handle );
426 return STATUS_SUCCESS;
428 error:
429 if (thread_regs) wine_ldt_free_fs( thread_regs->fs );
430 if (addr)
432 SIZE_T size = 0;
433 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
435 if (info) RtlFreeHeap( GetProcessHeap(), 0, info );
436 if (handle) NtClose( handle );
437 close( request_pipe[1] );
438 return status;
442 /***********************************************************************
443 * RtlExitUserThread (NTDLL.@)
445 void WINAPI RtlExitUserThread( ULONG status )
447 LdrShutdownThread();
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 )
459 NTSTATUS ret;
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;
469 SERVER_END_REQ;
470 return ret;
474 /******************************************************************************
475 * NtSuspendThread (NTDLL.@)
476 * ZwSuspendThread (NTDLL.@)
478 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
480 NTSTATUS ret;
482 SERVER_START_REQ( suspend_thread )
484 req->handle = handle;
485 if (!(ret = wine_server_call( req ))) *count = reply->count;
487 SERVER_END_REQ;
488 return ret;
492 /******************************************************************************
493 * NtResumeThread (NTDLL.@)
494 * ZwResumeThread (NTDLL.@)
496 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
498 NTSTATUS ret;
500 SERVER_START_REQ( resume_thread )
502 req->handle = handle;
503 if (!(ret = wine_server_call( req ))) *count = reply->count;
505 SERVER_END_REQ;
506 return ret;
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 )
538 NTSTATUS ret;
539 BOOL self, last;
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;
547 last = reply->last;
549 SERVER_END_REQ;
551 if (self)
553 if (last) exit( exit_code );
554 else server_abort_thread( exit_code );
556 return ret;
560 /******************************************************************************
561 * NtQueueApcThread (NTDLL.@)
563 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
564 ULONG_PTR arg2, ULONG_PTR arg3 )
566 NTSTATUS ret;
567 SERVER_START_REQ( queue_apc )
569 req->handle = handle;
570 req->user = 1;
571 req->func = func;
572 req->arg1 = (void *)arg1;
573 req->arg2 = (void *)arg2;
574 req->arg3 = (void *)arg3;
575 ret = wine_server_call( req );
577 SERVER_END_REQ;
578 return ret;
582 /***********************************************************************
583 * NtSetContextThread (NTDLL.@)
584 * ZwSetContextThread (NTDLL.@)
586 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
588 NTSTATUS ret;
589 DWORD dummy, i;
590 BOOL self = FALSE;
592 #ifdef __i386__
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);
602 #endif
604 if (!self)
606 SERVER_START_REQ( set_thread_context )
608 req->handle = handle;
609 req->flags = context->ContextFlags;
610 req->suspend = 0;
611 wine_server_add_data( req, context, sizeof(*context) );
612 ret = wine_server_call( req );
613 self = reply->self;
615 SERVER_END_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;
627 req->suspend = 0;
628 wine_server_add_data( req, context, sizeof(*context) );
629 ret = wine_server_call( req );
631 SERVER_END_REQ;
632 if (ret != STATUS_PENDING) break;
633 NtYieldExecution();
635 NtResumeThread( handle, &dummy );
637 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
640 if (ret) return ret;
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 )
651 #ifdef __i386__
652 flags &= ~CONTEXT_i386; /* get rid of CPU id */
653 if (flags & CONTEXT_INTEGER)
655 to->Eax = from->Eax;
656 to->Ebx = from->Ebx;
657 to->Ecx = from->Ecx;
658 to->Edx = from->Edx;
659 to->Esi = from->Esi;
660 to->Edi = from->Edi;
662 if (flags & CONTEXT_CONTROL)
664 to->Ebp = from->Ebp;
665 to->Esp = from->Esp;
666 to->Eip = from->Eip;
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)
680 to->Dr0 = from->Dr0;
681 to->Dr1 = from->Dr1;
682 to->Dr2 = from->Dr2;
683 to->Dr3 = from->Dr3;
684 to->Dr6 = from->Dr6;
685 to->Dr7 = from->Dr7;
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)
695 to->Rbp = from->Rbp;
696 to->Rip = from->Rip;
697 to->Rsp = from->Rsp;
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)
705 to->Rax = from->Rax;
706 to->Rcx = from->Rcx;
707 to->Rdx = from->Rdx;
708 to->Rbx = from->Rbx;
709 to->Rsi = from->Rsi;
710 to->Rdi = from->Rdi;
711 to->R8 = from->R8;
712 to->R9 = from->R9;
713 to->R10 = from->R10;
714 to->R11 = from->R11;
715 to->R12 = from->R12;
716 to->R13 = from->R13;
717 to->R14 = from->R14;
718 to->R15 = from->R15;
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)
733 to->Dr0 = from->Dr0;
734 to->Dr1 = from->Dr1;
735 to->Dr2 = from->Dr2;
736 to->Dr3 = from->Dr3;
737 to->Dr6 = from->Dr6;
738 to->Dr7 = from->Dr7;
740 #elif defined(__sparc__)
741 flags &= ~CONTEXT_SPARC; /* get rid of CPU id */
742 if (flags & CONTEXT_CONTROL)
744 to->psr = from->psr;
745 to->pc = from->pc;
746 to->npc = from->npc;
747 to->y = from->y;
748 to->wim = from->wim;
749 to->tbr = from->tbr;
751 if (flags & CONTEXT_INTEGER)
753 to->g0 = from->g0;
754 to->g1 = from->g1;
755 to->g2 = from->g2;
756 to->g3 = from->g3;
757 to->g4 = from->g4;
758 to->g5 = from->g5;
759 to->g6 = from->g6;
760 to->g7 = from->g7;
761 to->o0 = from->o0;
762 to->o1 = from->o1;
763 to->o2 = from->o2;
764 to->o3 = from->o3;
765 to->o4 = from->o4;
766 to->o5 = from->o5;
767 to->o6 = from->o6;
768 to->o7 = from->o7;
769 to->l0 = from->l0;
770 to->l1 = from->l1;
771 to->l2 = from->l2;
772 to->l3 = from->l3;
773 to->l4 = from->l4;
774 to->l5 = from->l5;
775 to->l6 = from->l6;
776 to->l7 = from->l7;
777 to->i0 = from->i0;
778 to->i1 = from->i1;
779 to->i2 = from->i2;
780 to->i3 = from->i3;
781 to->i4 = from->i4;
782 to->i5 = from->i5;
783 to->i6 = from->i6;
784 to->i7 = from->i7;
786 if (flags & CONTEXT_FLOATING_POINT)
788 /* FIXME */
790 #elif defined(__powerpc__)
791 /* Has no CPU id */
792 if (flags & CONTEXT_CONTROL)
794 to->Msr = from->Msr;
795 to->Ctr = from->Ctr;
796 to->Iar = from->Iar;
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;
832 to->Xer = from->Xer;
833 to->Cr = from->Cr;
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;
871 #else
872 #error You must implement context copying for your CPU
873 #endif
877 /***********************************************************************
878 * NtGetContextThread (NTDLL.@)
879 * ZwGetContextThread (NTDLL.@)
881 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
883 NTSTATUS ret;
884 CONTEXT ctx;
885 DWORD dummy, i;
886 BOOL self = FALSE;
888 SERVER_START_REQ( get_thread_context )
890 req->handle = handle;
891 req->flags = context->ContextFlags;
892 req->suspend = 0;
893 wine_server_set_reply( req, &ctx, sizeof(ctx) );
894 ret = wine_server_call( req );
895 self = reply->self;
897 SERVER_END_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;
909 req->suspend = 0;
910 wine_server_set_reply( req, &ctx, sizeof(ctx) );
911 ret = wine_server_call( req );
913 SERVER_END_REQ;
914 if (ret != STATUS_PENDING) break;
915 NtYieldExecution();
917 NtResumeThread( handle, &dummy );
921 if (ret == STATUS_SUCCESS)
923 copy_context( context, &ctx, context->ContextFlags );
924 #ifdef __i386__
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;
936 #endif
938 else if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
939 return ret;
943 /******************************************************************************
944 * NtQueryInformationThread (NTDLL.@)
945 * ZwQueryInformationThread (NTDLL.@)
947 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
948 void *data, ULONG length, ULONG *ret_len )
950 NTSTATUS status;
952 switch(class)
954 case ThreadBasicInformation:
956 THREAD_BASIC_INFORMATION info;
958 SERVER_START_REQ( get_thread_info )
960 req->handle = handle;
961 req->tid_in = 0;
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 */
973 SERVER_END_REQ;
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) );
980 return status;
981 case ThreadTimes:
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;
989 req->tid_in = 0;
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 );
997 SERVER_END_REQ;
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);
1007 times(&time_buf);
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;
1011 else
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) );
1021 return status;
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:
1037 default:
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 )
1051 NTSTATUS status;
1052 switch(class)
1054 case ThreadZeroTlsCell:
1055 if (handle == GetCurrentThread())
1057 LIST_ENTRY *entry;
1058 DWORD index;
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();
1072 else
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 );
1102 SERVER_END_REQ;
1104 return status;
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 );
1116 SERVER_END_REQ;
1118 return status;
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 );
1130 SERVER_END_REQ;
1132 return status;
1133 case ThreadBasicInformation:
1134 case ThreadTimes:
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:
1146 default:
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 */
1164 #else
1166 /**********************************************************************/
1168 TEB * WINAPI NtCurrentTeb(void)
1170 return pthread_functions.get_current_teb();
1173 #endif /* __i386__ */