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
;
71 teb
->Tib
.ExceptionList
= (void *)~0UL;
72 teb
->Tib
.StackBase
= (void *)~0UL;
73 teb
->Tib
.Self
= &teb
->Tib
;
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 /***********************************************************************
91 static inline void free_teb( TEB
*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 /***********************************************************************
106 * Setup the initial thread.
108 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
110 void thread_init(void)
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
= ¶ms
;
121 peb
.TlsBitmap
= &tls_bitmap
;
122 peb
.TlsExpansionBitmap
= &tls_expansion_bitmap
;
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
);
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
;
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" );
167 /* allocate user parameters */
170 RTL_USER_PROCESS_PARAMETERS
*params
= NULL
;
172 if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)¶ms
, 0, &info_size
,
173 MEM_COMMIT
, PAGE_READWRITE
) == STATUS_SUCCESS
)
175 params
->AllocationSize
= info_size
;
176 NtCurrentTeb()->Peb
->ProcessParameters
= params
;
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
, ¶ms
.hStdInput
);
185 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdOutput
);
186 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, OBJ_INHERIT
, ¶ms
.hStdError
);
191 /***********************************************************************
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
);
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 */
226 NtProtectVirtualMemory( NtCurrentProcess(), &teb
->DeallocationStack
, &size
,
227 PAGE_READWRITE
| PAGE_GUARD
, NULL
);
228 RtlFreeHeap( GetProcessHeap(), 0, info
);
231 InsertHeadList( &tls_links
, &teb
->TlsLinks
);
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
;
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
;
278 close( request_pipe
[0] );
282 if (status
) goto error
;
284 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info
) )))
286 status
= STATUS_NO_MEMORY
;
290 info
->pthread_info
.teb_size
= sigstack_total_size
;
291 if ((status
= VIRTUAL_alloc_teb( &addr
, info
->pthread_info
.teb_size
, FALSE
))) goto error
;
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
;
327 if (id
) id
->UniqueThread
= (HANDLE
)tid
;
328 if (handle_ptr
) *handle_ptr
= handle
;
329 else NtClose( handle
);
331 return STATUS_SUCCESS
;
334 if (thread_data
) wine_ldt_free_fs( thread_data
->teb_sel
);
338 NtFreeVirtualMemory( NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
340 if (info
) RtlFreeHeap( GetProcessHeap(), 0, info
);
341 if (handle
) NtClose( handle
);
342 close( request_pipe
[1] );
347 /***********************************************************************
348 * RtlExitUserThread (NTDLL.@)
350 void WINAPI
RtlExitUserThread( ULONG status
)
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
)
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
;
379 /******************************************************************************
380 * NtSuspendThread (NTDLL.@)
381 * ZwSuspendThread (NTDLL.@)
383 NTSTATUS WINAPI
NtSuspendThread( HANDLE handle
, PULONG count
)
387 SERVER_START_REQ( suspend_thread
)
389 req
->handle
= handle
;
390 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
397 /******************************************************************************
398 * NtResumeThread (NTDLL.@)
399 * ZwResumeThread (NTDLL.@)
401 NTSTATUS WINAPI
NtResumeThread( HANDLE handle
, PULONG count
)
405 SERVER_START_REQ( resume_thread
)
407 req
->handle
= handle
;
408 if (!(ret
= wine_server_call( req
))) *count
= reply
->count
;
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
)
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
;
458 if (last
) exit( exit_code
);
459 else server_abort_thread( exit_code
);
465 /******************************************************************************
466 * NtQueueApcThread (NTDLL.@)
468 NTSTATUS WINAPI
NtQueueApcThread( HANDLE handle
, PNTAPCFUNC func
, ULONG_PTR arg1
,
469 ULONG_PTR arg2
, ULONG_PTR arg3
)
472 SERVER_START_REQ( queue_apc
)
474 req
->handle
= handle
;
477 req
->arg1
= (void *)arg1
;
478 req
->arg2
= (void *)arg2
;
479 req
->arg3
= (void *)arg3
;
480 ret
= wine_server_call( req
);
487 /***********************************************************************
488 * NtSetContextThread (NTDLL.@)
489 * ZwSetContextThread (NTDLL.@)
491 NTSTATUS WINAPI
NtSetContextThread( HANDLE handle
, const CONTEXT
*context
)
496 SERVER_START_REQ( set_thread_context
)
498 req
->handle
= handle
;
499 req
->flags
= context
->ContextFlags
;
501 wine_server_add_data( req
, context
, sizeof(*context
) );
502 ret
= wine_server_call( 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
;
517 wine_server_add_data( req
, context
, sizeof(*context
) );
518 ret
= wine_server_call( req
);
521 if (ret
!= STATUS_PENDING
) break;
524 NtResumeThread( handle
, &dummy
);
528 if (ret
== STATUS_PENDING
) ret
= STATUS_ACCESS_DENIED
;
533 /* copy a context structure according to the flags */
534 static inline void copy_context( CONTEXT
*to
, const CONTEXT
*from
, DWORD flags
)
537 flags
&= ~CONTEXT_i386
; /* get rid of CPU id */
538 if (flags
& CONTEXT_INTEGER
)
547 if (flags
& CONTEXT_CONTROL
)
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
)
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
)
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
)
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
)
625 #elif defined(__sparc__)
626 flags
&= ~CONTEXT_SPARC
; /* get rid of CPU id */
627 if (flags
& CONTEXT_CONTROL
)
636 if (flags
& CONTEXT_INTEGER
)
671 if (flags
& CONTEXT_FLOATING_POINT
)
675 #elif defined(__powerpc__)
677 if (flags
& CONTEXT_CONTROL
)
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
;
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
;
757 #error You must implement context copying for your CPU
762 /***********************************************************************
763 * NtGetContextThread (NTDLL.@)
764 * ZwGetContextThread (NTDLL.@)
766 NTSTATUS WINAPI
NtGetContextThread( HANDLE handle
, CONTEXT
*context
)
772 SERVER_START_REQ( get_thread_context
)
774 req
->handle
= handle
;
775 req
->flags
= context
->ContextFlags
;
777 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
778 ret
= wine_server_call( 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
;
793 wine_server_set_reply( req
, &ctx
, sizeof(ctx
) );
794 ret
= wine_server_call( req
);
797 if (ret
!= STATUS_PENDING
) break;
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
;
810 /******************************************************************************
811 * NtQueryInformationThread (NTDLL.@)
812 * ZwQueryInformationThread (NTDLL.@)
814 NTSTATUS WINAPI
NtQueryInformationThread( HANDLE handle
, THREADINFOCLASS
class,
815 void *data
, ULONG length
, ULONG
*ret_len
)
821 case ThreadBasicInformation
:
823 THREAD_BASIC_INFORMATION info
;
825 SERVER_START_REQ( get_thread_info
)
827 req
->handle
= handle
;
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 */
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
) );
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
;
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
);
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())
872 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
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
;
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
) );
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
:
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
)
921 case ThreadZeroTlsCell
:
922 if (handle
== GetCurrentThread())
927 if (length
!= sizeof(DWORD
)) return STATUS_INVALID_PARAMETER
;
928 index
= *(const DWORD
*)data
;
929 if (index
< TLS_MINIMUM_AVAILABLE
)
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;
941 index
-= TLS_MINIMUM_AVAILABLE
;
942 if (index
>= 8 * sizeof(NtCurrentTeb()->Peb
->TlsExpansionBitmapBits
))
943 return STATUS_INVALID_PARAMETER
;
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;
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
);
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
);
986 case ThreadBasicInformation
:
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
:
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 */
1020 /**********************************************************************/
1022 TEB
* WINAPI
NtCurrentTeb(void)
1024 return pthread_functions
.get_current_teb();
1027 #endif /* __i386__ */