TEB.StackLimit should not include the guard page.
[wine/multimedia.git] / dlls / ntdll / thread.c
blob07cd08d1b4f15ad17f0b8f0ba712c2bf2e46bebc
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;
71 teb->Tib.ExceptionList = (void *)~0UL;
72 teb->Tib.StackBase = (void *)~0UL;
73 teb->Tib.Self = &teb->Tib;
74 teb->Peb = &peb;
75 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
76 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
78 if (!(thread_data->teb_sel = wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS;
79 thread_data->request_fd = -1;
80 thread_data->reply_fd = -1;
81 thread_data->wait_fd[0] = -1;
82 thread_data->wait_fd[1] = -1;
84 return STATUS_SUCCESS;
88 /***********************************************************************
89 * free_teb
91 static inline void free_teb( TEB *teb )
93 SIZE_T size = 0;
94 void *addr = teb;
95 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
97 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
98 wine_ldt_free_fs( thread_data->teb_sel );
99 munmap( teb, sigstack_total_size );
103 /***********************************************************************
104 * thread_init
106 * Setup the initial thread.
108 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
110 void thread_init(void)
112 TEB *teb;
113 void *addr;
114 SIZE_T info_size;
115 struct ntdll_thread_data *thread_data;
116 struct wine_pthread_thread_info thread_info;
117 static struct debug_info debug_info; /* debug info for initial thread */
119 peb.NumberOfProcessors = 1;
120 peb.ProcessParameters = &params;
121 peb.TlsBitmap = &tls_bitmap;
122 peb.TlsExpansionBitmap = &tls_expansion_bitmap;
123 peb.LdrData = &ldr;
124 params.CurrentDirectory.DosPath.Buffer = current_dir;
125 params.CurrentDirectory.DosPath.MaximumLength = sizeof(current_dir);
126 params.wShowWindow = 1; /* SW_SHOWNORMAL */
127 RtlInitializeBitMap( &tls_bitmap, peb.TlsBitmapBits, sizeof(peb.TlsBitmapBits) * 8 );
128 RtlInitializeBitMap( &tls_expansion_bitmap, peb.TlsExpansionBitmapBits,
129 sizeof(peb.TlsExpansionBitmapBits) * 8 );
130 InitializeListHead( &ldr.InLoadOrderModuleList );
131 InitializeListHead( &ldr.InMemoryOrderModuleList );
132 InitializeListHead( &ldr.InInitializationOrderModuleList );
133 InitializeListHead( &tls_links );
135 sigstack_total_size = get_signal_stack_total_size();
136 thread_info.teb_size = sigstack_total_size;
137 VIRTUAL_alloc_teb( &addr, thread_info.teb_size, TRUE );
138 teb = addr;
139 init_teb( teb );
140 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
141 thread_data->debug_info = &debug_info;
142 InsertHeadList( &tls_links, &teb->TlsLinks );
144 thread_info.stack_base = NULL;
145 thread_info.stack_size = 0;
146 thread_info.teb_base = teb;
147 thread_info.teb_sel = thread_data->teb_sel;
148 wine_pthread_get_functions( &pthread_functions, sizeof(pthread_functions) );
149 pthread_functions.init_current_teb( &thread_info );
150 pthread_functions.init_thread( &thread_info );
152 debug_info.str_pos = debug_info.strings;
153 debug_info.out_pos = debug_info.output;
154 debug_init();
156 /* setup the server connection */
157 server_init_process();
158 info_size = server_init_thread( thread_info.pid, thread_info.tid, NULL );
160 /* create the process heap */
161 if (!(peb.ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL )))
163 MESSAGE( "wine: failed to create the process heap\n" );
164 exit(1);
167 /* allocate user parameters */
168 if (info_size)
170 RTL_USER_PROCESS_PARAMETERS *params = NULL;
172 if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&params, 0, &info_size,
173 MEM_COMMIT, PAGE_READWRITE ) == STATUS_SUCCESS)
175 params->AllocationSize = info_size;
176 NtCurrentTeb()->Peb->ProcessParameters = params;
179 else
181 /* This is wine specific: we have no parent (we're started from unix)
182 * so, create a simple console with bare handles to unix stdio
184 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, OBJ_INHERIT, &params.hStdInput );
185 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdOutput );
186 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdError );
191 /***********************************************************************
192 * start_thread
194 * Startup routine for a newly created thread.
196 static void start_thread( struct wine_pthread_thread_info *info )
198 TEB *teb = info->teb_base;
199 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
200 struct startup_info *startup_info = (struct startup_info *)info;
201 PRTL_THREAD_START_ROUTINE func = startup_info->entry_point;
202 void *arg = startup_info->entry_arg;
203 struct debug_info debug_info;
204 SIZE_T size, page_size = getpagesize();
206 debug_info.str_pos = debug_info.strings;
207 debug_info.out_pos = debug_info.output;
208 thread_data->debug_info = &debug_info;
210 pthread_functions.init_current_teb( info );
211 SIGNAL_Init();
212 server_init_thread( info->pid, info->tid, func );
213 pthread_functions.init_thread( info );
215 /* allocate a memory view for the stack */
216 size = info->stack_size;
217 teb->DeallocationStack = info->stack_base;
218 NtAllocateVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, 0,
219 &size, MEM_SYSTEM, PAGE_READWRITE );
220 /* limit is lower than base since the stack grows down */
221 teb->Tib.StackBase = (char *)info->stack_base + info->stack_size;
222 teb->Tib.StackLimit = (char *)info->stack_base + page_size;
224 /* setup the guard page */
225 size = page_size;
226 NtProtectVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, &size,
227 PAGE_READWRITE | PAGE_GUARD, NULL );
228 RtlFreeHeap( GetProcessHeap(), 0, info );
230 RtlAcquirePebLock();
231 InsertHeadList( &tls_links, &teb->TlsLinks );
232 RtlReleasePebLock();
234 func( arg );
238 /***********************************************************************
239 * RtlCreateUserThread (NTDLL.@)
241 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
242 BOOLEAN suspended, PVOID stack_addr,
243 SIZE_T stack_reserve, SIZE_T stack_commit,
244 PRTL_THREAD_START_ROUTINE start, void *param,
245 HANDLE *handle_ptr, CLIENT_ID *id )
247 struct ntdll_thread_data *thread_data = NULL;
248 struct startup_info *info = NULL;
249 void *addr;
250 HANDLE handle = 0;
251 TEB *teb;
252 DWORD tid = 0;
253 int request_pipe[2];
254 NTSTATUS status;
255 SIZE_T page_size = getpagesize();
257 if( ! is_current_process( process ) )
259 ERR("Unsupported on other process\n");
260 return STATUS_ACCESS_DENIED;
263 if (pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
264 fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
265 wine_server_send_fd( request_pipe[0] );
267 SERVER_START_REQ( new_thread )
269 req->access = THREAD_ALL_ACCESS;
270 req->attributes = 0; /* FIXME */
271 req->suspend = suspended;
272 req->request_fd = request_pipe[0];
273 if (!(status = wine_server_call( req )))
275 handle = reply->handle;
276 tid = reply->tid;
278 close( request_pipe[0] );
280 SERVER_END_REQ;
282 if (status) goto error;
284 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) )))
286 status = STATUS_NO_MEMORY;
287 goto error;
290 info->pthread_info.teb_size = sigstack_total_size;
291 if ((status = VIRTUAL_alloc_teb( &addr, info->pthread_info.teb_size, FALSE ))) goto error;
292 teb = addr;
293 if ((status = init_teb( teb ))) goto error;
295 teb->ClientId.UniqueProcess = (HANDLE)GetCurrentProcessId();
296 teb->ClientId.UniqueThread = (HANDLE)tid;
298 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
299 thread_data->request_fd = request_pipe[1];
301 info->pthread_info.teb_base = teb;
302 info->pthread_info.teb_sel = thread_data->teb_sel;
304 if (!stack_reserve || !stack_commit)
306 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
307 if (!stack_reserve) stack_reserve = nt->OptionalHeader.SizeOfStackReserve;
308 if (!stack_commit) stack_commit = nt->OptionalHeader.SizeOfStackCommit;
310 if (stack_reserve < stack_commit) stack_reserve = stack_commit;
311 stack_reserve += page_size; /* for the guard page */
312 stack_reserve = (stack_reserve + 0xffff) & ~0xffff; /* round to 64K boundary */
313 if (stack_reserve < 1024 * 1024) stack_reserve = 1024 * 1024; /* Xlib needs a large stack */
315 info->pthread_info.stack_base = NULL;
316 info->pthread_info.stack_size = stack_reserve;
317 info->pthread_info.entry = start_thread;
318 info->entry_point = start;
319 info->entry_arg = param;
321 if (pthread_functions.create_thread( &info->pthread_info ) == -1)
323 status = STATUS_NO_MEMORY;
324 goto error;
327 if (id) id->UniqueThread = (HANDLE)tid;
328 if (handle_ptr) *handle_ptr = handle;
329 else NtClose( handle );
331 return STATUS_SUCCESS;
333 error:
334 if (thread_data) wine_ldt_free_fs( thread_data->teb_sel );
335 if (addr)
337 SIZE_T size = 0;
338 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
340 if (info) RtlFreeHeap( GetProcessHeap(), 0, info );
341 if (handle) NtClose( handle );
342 close( request_pipe[1] );
343 return status;
347 /***********************************************************************
348 * RtlExitUserThread (NTDLL.@)
350 void WINAPI RtlExitUserThread( ULONG status )
352 LdrShutdownThread();
353 server_exit_thread( status );
357 /***********************************************************************
358 * NtOpenThread (NTDLL.@)
359 * ZwOpenThread (NTDLL.@)
361 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
362 const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
364 NTSTATUS ret;
366 SERVER_START_REQ( open_thread )
368 req->tid = (thread_id_t)id->UniqueThread;
369 req->access = access;
370 req->attributes = attr ? attr->Attributes : 0;
371 ret = wine_server_call( req );
372 *handle = reply->handle;
374 SERVER_END_REQ;
375 return ret;
379 /******************************************************************************
380 * NtSuspendThread (NTDLL.@)
381 * ZwSuspendThread (NTDLL.@)
383 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
385 NTSTATUS ret;
387 SERVER_START_REQ( suspend_thread )
389 req->handle = handle;
390 if (!(ret = wine_server_call( req ))) *count = reply->count;
392 SERVER_END_REQ;
393 return ret;
397 /******************************************************************************
398 * NtResumeThread (NTDLL.@)
399 * ZwResumeThread (NTDLL.@)
401 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
403 NTSTATUS ret;
405 SERVER_START_REQ( resume_thread )
407 req->handle = handle;
408 if (!(ret = wine_server_call( req ))) *count = reply->count;
410 SERVER_END_REQ;
411 return ret;
415 /******************************************************************************
416 * NtAlertResumeThread (NTDLL.@)
417 * ZwAlertResumeThread (NTDLL.@)
419 NTSTATUS WINAPI NtAlertResumeThread( HANDLE handle, PULONG count )
421 FIXME( "stub: should alert thread %p\n", handle );
422 return NtResumeThread( handle, count );
426 /******************************************************************************
427 * NtAlertThread (NTDLL.@)
428 * ZwAlertThread (NTDLL.@)
430 NTSTATUS WINAPI NtAlertThread( HANDLE handle )
432 FIXME( "stub: %p\n", handle );
433 return STATUS_NOT_IMPLEMENTED;
437 /******************************************************************************
438 * NtTerminateThread (NTDLL.@)
439 * ZwTerminateThread (NTDLL.@)
441 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
443 NTSTATUS ret;
444 BOOL self, last;
446 SERVER_START_REQ( terminate_thread )
448 req->handle = handle;
449 req->exit_code = exit_code;
450 ret = wine_server_call( req );
451 self = !ret && reply->self;
452 last = reply->last;
454 SERVER_END_REQ;
456 if (self)
458 if (last) exit( exit_code );
459 else server_abort_thread( exit_code );
461 return ret;
465 /******************************************************************************
466 * NtQueueApcThread (NTDLL.@)
468 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
469 ULONG_PTR arg2, ULONG_PTR arg3 )
471 NTSTATUS ret;
472 SERVER_START_REQ( queue_apc )
474 req->handle = handle;
475 req->user = 1;
476 req->func = func;
477 req->arg1 = (void *)arg1;
478 req->arg2 = (void *)arg2;
479 req->arg3 = (void *)arg3;
480 ret = wine_server_call( req );
482 SERVER_END_REQ;
483 return ret;
487 /***********************************************************************
488 * NtSetContextThread (NTDLL.@)
489 * ZwSetContextThread (NTDLL.@)
491 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
493 NTSTATUS ret;
494 DWORD dummy, i;
496 SERVER_START_REQ( set_thread_context )
498 req->handle = handle;
499 req->flags = context->ContextFlags;
500 req->suspend = 0;
501 wine_server_add_data( req, context, sizeof(*context) );
502 ret = wine_server_call( req );
504 SERVER_END_REQ;
506 if (ret == STATUS_PENDING)
508 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
510 for (i = 0; i < 100; i++)
512 SERVER_START_REQ( set_thread_context )
514 req->handle = handle;
515 req->flags = context->ContextFlags;
516 req->suspend = 0;
517 wine_server_add_data( req, context, sizeof(*context) );
518 ret = wine_server_call( req );
520 SERVER_END_REQ;
521 if (ret != STATUS_PENDING) break;
522 NtYieldExecution();
524 NtResumeThread( handle, &dummy );
528 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
529 return ret;
533 /* copy a context structure according to the flags */
534 static inline void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
536 #ifdef __i386__
537 flags &= ~CONTEXT_i386; /* get rid of CPU id */
538 if (flags & CONTEXT_INTEGER)
540 to->Eax = from->Eax;
541 to->Ebx = from->Ebx;
542 to->Ecx = from->Ecx;
543 to->Edx = from->Edx;
544 to->Esi = from->Esi;
545 to->Edi = from->Edi;
547 if (flags & CONTEXT_CONTROL)
549 to->Ebp = from->Ebp;
550 to->Esp = from->Esp;
551 to->Eip = from->Eip;
552 to->SegCs = from->SegCs;
553 to->SegSs = from->SegSs;
554 to->EFlags = from->EFlags;
556 if (flags & CONTEXT_SEGMENTS)
558 to->SegDs = from->SegDs;
559 to->SegEs = from->SegEs;
560 to->SegFs = from->SegFs;
561 to->SegGs = from->SegGs;
563 if (flags & CONTEXT_DEBUG_REGISTERS)
565 to->Dr0 = from->Dr0;
566 to->Dr1 = from->Dr1;
567 to->Dr2 = from->Dr2;
568 to->Dr3 = from->Dr3;
569 to->Dr6 = from->Dr6;
570 to->Dr7 = from->Dr7;
572 if (flags & CONTEXT_FLOATING_POINT)
574 to->FloatSave = from->FloatSave;
576 #elif defined(__x86_64__)
577 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
578 if (flags & CONTEXT_CONTROL)
580 to->Rbp = from->Rbp;
581 to->Rip = from->Rip;
582 to->Rsp = from->Rsp;
583 to->SegCs = from->SegCs;
584 to->SegSs = from->SegSs;
585 to->EFlags = from->EFlags;
586 to->MxCsr = from->MxCsr;
588 if (flags & CONTEXT_INTEGER)
590 to->Rax = from->Rax;
591 to->Rcx = from->Rcx;
592 to->Rdx = from->Rdx;
593 to->Rbx = from->Rbx;
594 to->Rsi = from->Rsi;
595 to->Rdi = from->Rdi;
596 to->R8 = from->R8;
597 to->R9 = from->R9;
598 to->R10 = from->R10;
599 to->R11 = from->R11;
600 to->R12 = from->R12;
601 to->R13 = from->R13;
602 to->R14 = from->R14;
603 to->R15 = from->R15;
605 if (flags & CONTEXT_SEGMENTS)
607 to->SegDs = from->SegDs;
608 to->SegEs = from->SegEs;
609 to->SegFs = from->SegFs;
610 to->SegGs = from->SegGs;
612 if (flags & CONTEXT_FLOATING_POINT)
614 to->u.FltSave = from->u.FltSave;
616 if (flags & CONTEXT_DEBUG_REGISTERS)
618 to->Dr0 = from->Dr0;
619 to->Dr1 = from->Dr1;
620 to->Dr2 = from->Dr2;
621 to->Dr3 = from->Dr3;
622 to->Dr6 = from->Dr6;
623 to->Dr7 = from->Dr7;
625 #elif defined(__sparc__)
626 flags &= ~CONTEXT_SPARC; /* get rid of CPU id */
627 if (flags & CONTEXT_CONTROL)
629 to->psr = from->psr;
630 to->pc = from->pc;
631 to->npc = from->npc;
632 to->y = from->y;
633 to->wim = from->wim;
634 to->tbr = from->tbr;
636 if (flags & CONTEXT_INTEGER)
638 to->g0 = from->g0;
639 to->g1 = from->g1;
640 to->g2 = from->g2;
641 to->g3 = from->g3;
642 to->g4 = from->g4;
643 to->g5 = from->g5;
644 to->g6 = from->g6;
645 to->g7 = from->g7;
646 to->o0 = from->o0;
647 to->o1 = from->o1;
648 to->o2 = from->o2;
649 to->o3 = from->o3;
650 to->o4 = from->o4;
651 to->o5 = from->o5;
652 to->o6 = from->o6;
653 to->o7 = from->o7;
654 to->l0 = from->l0;
655 to->l1 = from->l1;
656 to->l2 = from->l2;
657 to->l3 = from->l3;
658 to->l4 = from->l4;
659 to->l5 = from->l5;
660 to->l6 = from->l6;
661 to->l7 = from->l7;
662 to->i0 = from->i0;
663 to->i1 = from->i1;
664 to->i2 = from->i2;
665 to->i3 = from->i3;
666 to->i4 = from->i4;
667 to->i5 = from->i5;
668 to->i6 = from->i6;
669 to->i7 = from->i7;
671 if (flags & CONTEXT_FLOATING_POINT)
673 /* FIXME */
675 #elif defined(__powerpc__)
676 /* Has no CPU id */
677 if (flags & CONTEXT_CONTROL)
679 to->Msr = from->Msr;
680 to->Ctr = from->Ctr;
681 to->Iar = from->Iar;
683 if (flags & CONTEXT_INTEGER)
685 to->Gpr0 = from->Gpr0;
686 to->Gpr1 = from->Gpr1;
687 to->Gpr2 = from->Gpr2;
688 to->Gpr3 = from->Gpr3;
689 to->Gpr4 = from->Gpr4;
690 to->Gpr5 = from->Gpr5;
691 to->Gpr6 = from->Gpr6;
692 to->Gpr7 = from->Gpr7;
693 to->Gpr8 = from->Gpr8;
694 to->Gpr9 = from->Gpr9;
695 to->Gpr10 = from->Gpr10;
696 to->Gpr11 = from->Gpr11;
697 to->Gpr12 = from->Gpr12;
698 to->Gpr13 = from->Gpr13;
699 to->Gpr14 = from->Gpr14;
700 to->Gpr15 = from->Gpr15;
701 to->Gpr16 = from->Gpr16;
702 to->Gpr17 = from->Gpr17;
703 to->Gpr18 = from->Gpr18;
704 to->Gpr19 = from->Gpr19;
705 to->Gpr20 = from->Gpr20;
706 to->Gpr21 = from->Gpr21;
707 to->Gpr22 = from->Gpr22;
708 to->Gpr23 = from->Gpr23;
709 to->Gpr24 = from->Gpr24;
710 to->Gpr25 = from->Gpr25;
711 to->Gpr26 = from->Gpr26;
712 to->Gpr27 = from->Gpr27;
713 to->Gpr28 = from->Gpr28;
714 to->Gpr29 = from->Gpr29;
715 to->Gpr30 = from->Gpr30;
716 to->Gpr31 = from->Gpr31;
717 to->Xer = from->Xer;
718 to->Cr = from->Cr;
720 if (flags & CONTEXT_FLOATING_POINT)
722 to->Fpr0 = from->Fpr0;
723 to->Fpr1 = from->Fpr1;
724 to->Fpr2 = from->Fpr2;
725 to->Fpr3 = from->Fpr3;
726 to->Fpr4 = from->Fpr4;
727 to->Fpr5 = from->Fpr5;
728 to->Fpr6 = from->Fpr6;
729 to->Fpr7 = from->Fpr7;
730 to->Fpr8 = from->Fpr8;
731 to->Fpr9 = from->Fpr9;
732 to->Fpr10 = from->Fpr10;
733 to->Fpr11 = from->Fpr11;
734 to->Fpr12 = from->Fpr12;
735 to->Fpr13 = from->Fpr13;
736 to->Fpr14 = from->Fpr14;
737 to->Fpr15 = from->Fpr15;
738 to->Fpr16 = from->Fpr16;
739 to->Fpr17 = from->Fpr17;
740 to->Fpr18 = from->Fpr18;
741 to->Fpr19 = from->Fpr19;
742 to->Fpr20 = from->Fpr20;
743 to->Fpr21 = from->Fpr21;
744 to->Fpr22 = from->Fpr22;
745 to->Fpr23 = from->Fpr23;
746 to->Fpr24 = from->Fpr24;
747 to->Fpr25 = from->Fpr25;
748 to->Fpr26 = from->Fpr26;
749 to->Fpr27 = from->Fpr27;
750 to->Fpr28 = from->Fpr28;
751 to->Fpr29 = from->Fpr29;
752 to->Fpr30 = from->Fpr30;
753 to->Fpr31 = from->Fpr31;
754 to->Fpscr = from->Fpscr;
756 #else
757 #error You must implement context copying for your CPU
758 #endif
762 /***********************************************************************
763 * NtGetContextThread (NTDLL.@)
764 * ZwGetContextThread (NTDLL.@)
766 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
768 NTSTATUS ret;
769 CONTEXT ctx;
770 DWORD dummy, i;
772 SERVER_START_REQ( get_thread_context )
774 req->handle = handle;
775 req->flags = context->ContextFlags;
776 req->suspend = 0;
777 wine_server_set_reply( req, &ctx, sizeof(ctx) );
778 ret = wine_server_call( req );
780 SERVER_END_REQ;
782 if (ret == STATUS_PENDING)
784 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
786 for (i = 0; i < 100; i++)
788 SERVER_START_REQ( get_thread_context )
790 req->handle = handle;
791 req->flags = context->ContextFlags;
792 req->suspend = 0;
793 wine_server_set_reply( req, &ctx, sizeof(ctx) );
794 ret = wine_server_call( req );
796 SERVER_END_REQ;
797 if (ret != STATUS_PENDING) break;
798 NtYieldExecution();
800 NtResumeThread( handle, &dummy );
804 if (ret == STATUS_SUCCESS) copy_context( context, &ctx, context->ContextFlags );
805 else if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
806 return ret;
810 /******************************************************************************
811 * NtQueryInformationThread (NTDLL.@)
812 * ZwQueryInformationThread (NTDLL.@)
814 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
815 void *data, ULONG length, ULONG *ret_len )
817 NTSTATUS status;
819 switch(class)
821 case ThreadBasicInformation:
823 THREAD_BASIC_INFORMATION info;
825 SERVER_START_REQ( get_thread_info )
827 req->handle = handle;
828 req->tid_in = 0;
829 if (!(status = wine_server_call( req )))
831 info.ExitStatus = reply->exit_code;
832 info.TebBaseAddress = reply->teb;
833 info.ClientId.UniqueProcess = (HANDLE)reply->pid;
834 info.ClientId.UniqueThread = (HANDLE)reply->tid;
835 info.AffinityMask = reply->affinity;
836 info.Priority = reply->priority;
837 info.BasePriority = reply->priority; /* FIXME */
840 SERVER_END_REQ;
841 if (status == STATUS_SUCCESS)
843 if (data) memcpy( data, &info, min( length, sizeof(info) ));
844 if (ret_len) *ret_len = min( length, sizeof(info) );
847 return status;
848 case ThreadTimes:
850 KERNEL_USER_TIMES kusrt;
851 /* We need to do a server call to get the creation time or exit time */
852 /* This works on any thread */
853 SERVER_START_REQ( get_thread_info )
855 req->handle = handle;
856 req->tid_in = 0;
857 status = wine_server_call( req );
858 if (status == STATUS_SUCCESS)
860 RtlSecondsSince1970ToTime( reply->creation_time, &kusrt.CreateTime );
861 RtlSecondsSince1970ToTime( reply->exit_time, &kusrt.ExitTime );
864 SERVER_END_REQ;
865 if (status == STATUS_SUCCESS)
867 /* We call times(2) for kernel time or user time */
868 /* We can only (portably) do this for the current thread */
869 if (handle == GetCurrentThread())
871 struct tms time_buf;
872 long clocks_per_sec = sysconf(_SC_CLK_TCK);
874 times(&time_buf);
875 kusrt.KernelTime.QuadPart = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
876 kusrt.UserTime.QuadPart = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
878 else
880 kusrt.KernelTime.QuadPart = 0;
881 kusrt.UserTime.QuadPart = 0;
882 FIXME("Cannot get kerneltime or usertime of other threads\n");
884 if (data) memcpy( data, &kusrt, min( length, sizeof(kusrt) ));
885 if (ret_len) *ret_len = min( length, sizeof(kusrt) );
888 return status;
889 case ThreadPriority:
890 case ThreadBasePriority:
891 case ThreadAffinityMask:
892 case ThreadImpersonationToken:
893 case ThreadDescriptorTableEntry:
894 case ThreadEnableAlignmentFaultFixup:
895 case ThreadEventPair_Reusable:
896 case ThreadQuerySetWin32StartAddress:
897 case ThreadZeroTlsCell:
898 case ThreadPerformanceCount:
899 case ThreadAmILastThread:
900 case ThreadIdealProcessor:
901 case ThreadPriorityBoost:
902 case ThreadSetTlsArrayAddress:
903 case ThreadIsIoPending:
904 default:
905 FIXME( "info class %d not supported yet\n", class );
906 return STATUS_NOT_IMPLEMENTED;
911 /******************************************************************************
912 * NtSetInformationThread (NTDLL.@)
913 * ZwSetInformationThread (NTDLL.@)
915 NTSTATUS WINAPI NtSetInformationThread( HANDLE handle, THREADINFOCLASS class,
916 LPCVOID data, ULONG length )
918 NTSTATUS status;
919 switch(class)
921 case ThreadZeroTlsCell:
922 if (handle == GetCurrentThread())
924 LIST_ENTRY *entry;
925 DWORD index;
927 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
928 index = *(const DWORD *)data;
929 if (index < TLS_MINIMUM_AVAILABLE)
931 RtlAcquirePebLock();
932 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
934 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
935 teb->TlsSlots[index] = 0;
937 RtlReleasePebLock();
939 else
941 index -= TLS_MINIMUM_AVAILABLE;
942 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
943 return STATUS_INVALID_PARAMETER;
944 RtlAcquirePebLock();
945 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
947 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
948 if (teb->TlsExpansionSlots) teb->TlsExpansionSlots[index] = 0;
950 RtlReleasePebLock();
952 return STATUS_SUCCESS;
954 FIXME( "ZeroTlsCell not supported on other threads\n" );
955 return STATUS_NOT_IMPLEMENTED;
957 case ThreadImpersonationToken:
959 const HANDLE *phToken = data;
960 if (length != sizeof(HANDLE)) return STATUS_INVALID_PARAMETER;
961 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken );
962 SERVER_START_REQ( set_thread_info )
964 req->handle = handle;
965 req->token = *phToken;
966 req->mask = SET_THREAD_INFO_TOKEN;
967 status = wine_server_call( req );
969 SERVER_END_REQ;
971 return status;
972 case ThreadBasePriority:
974 const DWORD *pprio = data;
975 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
976 SERVER_START_REQ( set_thread_info )
978 req->handle = handle;
979 req->priority = *pprio;
980 req->mask = SET_THREAD_INFO_PRIORITY;
981 status = wine_server_call( req );
983 SERVER_END_REQ;
985 return status;
986 case ThreadBasicInformation:
987 case ThreadTimes:
988 case ThreadPriority:
989 case ThreadAffinityMask:
990 case ThreadDescriptorTableEntry:
991 case ThreadEnableAlignmentFaultFixup:
992 case ThreadEventPair_Reusable:
993 case ThreadQuerySetWin32StartAddress:
994 case ThreadPerformanceCount:
995 case ThreadAmILastThread:
996 case ThreadIdealProcessor:
997 case ThreadPriorityBoost:
998 case ThreadSetTlsArrayAddress:
999 case ThreadIsIoPending:
1000 default:
1001 FIXME( "info class %d not supported yet\n", class );
1002 return STATUS_NOT_IMPLEMENTED;
1007 /**********************************************************************
1008 * NtCurrentTeb (NTDLL.@)
1010 #if defined(__i386__) && defined(__GNUC__)
1012 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" );
1014 #elif defined(__i386__) && defined(_MSC_VER)
1016 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
1018 #else
1020 /**********************************************************************/
1022 TEB * WINAPI NtCurrentTeb(void)
1024 return pthread_functions.get_current_teb();
1027 #endif /* __i386__ */