comdlg32/tests: Avoid "misleading indentation" warnings.
[wine.git] / server / thread.c
blob467ccd1f0db5289752511ba0f81e4b76f23d9aaf
1 /*
2 * Server-side thread management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <time.h>
34 #include <poll.h>
35 #ifdef HAVE_SCHED_H
36 /* FreeBSD needs this for cpu_set_t instead of its cpuset_t */
37 #define _WITH_CPU_SET_T
38 #include <sched.h>
39 #endif
41 #include "ntstatus.h"
42 #define WIN32_NO_STATUS
43 #include "windef.h"
44 #include "winternl.h"
46 #include "file.h"
47 #include "handle.h"
48 #include "process.h"
49 #include "thread.h"
50 #include "request.h"
51 #include "user.h"
52 #include "security.h"
55 /* thread queues */
57 struct thread_wait
59 struct thread_wait *next; /* next wait structure for this thread */
60 struct thread *thread; /* owner thread */
61 int count; /* count of objects */
62 int flags;
63 int abandoned;
64 enum select_op select;
65 client_ptr_t key; /* wait key for keyed events */
66 client_ptr_t cookie; /* magic cookie to return to client */
67 abstime_t when;
68 struct timeout_user *user;
69 int status; /* status to return (unless STATUS_PENDING) */
70 struct wait_queue_entry queues[1];
73 /* asynchronous procedure calls */
75 struct thread_apc
77 struct object obj; /* object header */
78 struct list entry; /* queue linked list */
79 struct thread *caller; /* thread that queued this apc */
80 struct object *owner; /* object that queued this apc */
81 int executed; /* has it been executed by the client? */
82 apc_call_t call; /* call arguments */
83 apc_result_t result; /* call results once executed */
86 static void dump_thread_apc( struct object *obj, int verbose );
87 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
88 static void thread_apc_destroy( struct object *obj );
89 static void clear_apc_queue( struct list *queue );
91 static const struct object_ops thread_apc_ops =
93 sizeof(struct thread_apc), /* size */
94 &no_type, /* type */
95 dump_thread_apc, /* dump */
96 add_queue, /* add_queue */
97 remove_queue, /* remove_queue */
98 thread_apc_signaled, /* signaled */
99 no_satisfied, /* satisfied */
100 no_signal, /* signal */
101 no_get_fd, /* get_fd */
102 default_map_access, /* map_access */
103 default_get_sd, /* get_sd */
104 default_set_sd, /* set_sd */
105 no_get_full_name, /* get_full_name */
106 no_lookup_name, /* lookup_name */
107 no_link_name, /* link_name */
108 NULL, /* unlink_name */
109 no_open_file, /* open_file */
110 no_kernel_obj_list, /* get_kernel_obj_list */
111 no_close_handle, /* close_handle */
112 thread_apc_destroy /* destroy */
116 /* thread CPU context */
118 struct context
120 struct object obj; /* object header */
121 unsigned int status; /* status of the context */
122 context_t regs[3]; /* context data */
124 #define CTX_NATIVE 0 /* context for native machine */
125 #define CTX_WOW 1 /* context if thread is inside WoW */
126 #define CTX_PENDING 2 /* pending native context when we don't know whether thread is inside WoW */
128 /* flags for registers that always need to be set from the server side */
129 static const unsigned int system_flags = SERVER_CTX_DEBUG_REGISTERS;
130 /* flags for registers that are set from the native context even in WoW mode */
131 static const unsigned int always_native_flags = SERVER_CTX_DEBUG_REGISTERS | SERVER_CTX_FLOATING_POINT | SERVER_CTX_YMM_REGISTERS;
133 static void dump_context( struct object *obj, int verbose );
134 static int context_signaled( struct object *obj, struct wait_queue_entry *entry );
136 static const struct object_ops context_ops =
138 sizeof(struct context), /* size */
139 &no_type, /* type */
140 dump_context, /* dump */
141 add_queue, /* add_queue */
142 remove_queue, /* remove_queue */
143 context_signaled, /* signaled */
144 no_satisfied, /* satisfied */
145 no_signal, /* signal */
146 no_get_fd, /* get_fd */
147 default_map_access, /* map_access */
148 default_get_sd, /* get_sd */
149 default_set_sd, /* set_sd */
150 no_get_full_name, /* get_full_name */
151 no_lookup_name, /* lookup_name */
152 no_link_name, /* link_name */
153 NULL, /* unlink_name */
154 no_open_file, /* open_file */
155 no_kernel_obj_list, /* get_kernel_obj_list */
156 no_close_handle, /* close_handle */
157 no_destroy /* destroy */
161 /* thread operations */
163 static const WCHAR thread_name[] = {'T','h','r','e','a','d'};
165 struct type_descr thread_type =
167 { thread_name, sizeof(thread_name) }, /* name */
168 THREAD_ALL_ACCESS, /* valid_access */
169 { /* mapping */
170 STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT,
171 STANDARD_RIGHTS_WRITE | THREAD_SET_LIMITED_INFORMATION | THREAD_SET_INFORMATION
172 | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_TERMINATE | 0x04,
173 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_RESUME | THREAD_QUERY_LIMITED_INFORMATION,
174 THREAD_ALL_ACCESS
178 static void dump_thread( struct object *obj, int verbose );
179 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
180 static unsigned int thread_map_access( struct object *obj, unsigned int access );
181 static void thread_poll_event( struct fd *fd, int event );
182 static struct list *thread_get_kernel_obj_list( struct object *obj );
183 static void destroy_thread( struct object *obj );
185 static const struct object_ops thread_ops =
187 sizeof(struct thread), /* size */
188 &thread_type, /* type */
189 dump_thread, /* dump */
190 add_queue, /* add_queue */
191 remove_queue, /* remove_queue */
192 thread_signaled, /* signaled */
193 no_satisfied, /* satisfied */
194 no_signal, /* signal */
195 no_get_fd, /* get_fd */
196 thread_map_access, /* map_access */
197 default_get_sd, /* get_sd */
198 default_set_sd, /* set_sd */
199 no_get_full_name, /* get_full_name */
200 no_lookup_name, /* lookup_name */
201 no_link_name, /* link_name */
202 NULL, /* unlink_name */
203 no_open_file, /* open_file */
204 thread_get_kernel_obj_list, /* get_kernel_obj_list */
205 no_close_handle, /* close_handle */
206 destroy_thread /* destroy */
209 static const struct fd_ops thread_fd_ops =
211 NULL, /* get_poll_events */
212 thread_poll_event, /* poll_event */
213 NULL, /* flush */
214 NULL, /* get_fd_type */
215 NULL, /* ioctl */
216 NULL, /* queue_async */
217 NULL /* reselect_async */
220 static struct list thread_list = LIST_INIT(thread_list);
222 /* initialize the structure for a newly allocated thread */
223 static inline void init_thread_structure( struct thread *thread )
225 int i;
227 thread->unix_pid = -1; /* not known yet */
228 thread->unix_tid = -1; /* not known yet */
229 thread->context = NULL;
230 thread->teb = 0;
231 thread->entry_point = 0;
232 thread->system_regs = 0;
233 thread->queue = NULL;
234 thread->wait = NULL;
235 thread->error = 0;
236 thread->req_data = NULL;
237 thread->req_toread = 0;
238 thread->reply_data = NULL;
239 thread->reply_towrite = 0;
240 thread->request_fd = NULL;
241 thread->reply_fd = NULL;
242 thread->wait_fd = NULL;
243 thread->state = RUNNING;
244 thread->exit_code = 0;
245 thread->priority = 0;
246 thread->suspend = 0;
247 thread->dbg_hidden = 0;
248 thread->desktop_users = 0;
249 thread->token = NULL;
250 thread->desc = NULL;
251 thread->desc_len = 0;
253 thread->creation_time = current_time;
254 thread->exit_time = 0;
256 list_init( &thread->mutex_list );
257 list_init( &thread->system_apc );
258 list_init( &thread->user_apc );
259 list_init( &thread->kernel_object );
261 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
262 thread->inflight[i].server = thread->inflight[i].client = -1;
265 /* check if address looks valid for a client-side data structure (TEB etc.) */
266 static inline int is_valid_address( client_ptr_t addr )
268 return addr && !(addr % sizeof(int));
272 /* dump a context on stdout for debugging purposes */
273 static void dump_context( struct object *obj, int verbose )
275 struct context *context = (struct context *)obj;
276 assert( obj->ops == &context_ops );
278 fprintf( stderr, "context flags=%x/%x\n",
279 context->regs[CTX_NATIVE].flags, context->regs[CTX_WOW].flags );
283 static int context_signaled( struct object *obj, struct wait_queue_entry *entry )
285 struct context *context = (struct context *)obj;
286 return context->status != STATUS_PENDING;
290 static struct context *create_thread_context( struct thread *thread )
292 struct context *context;
293 if (!(context = alloc_object( &context_ops ))) return NULL;
294 context->status = STATUS_PENDING;
295 memset( &context->regs, 0, sizeof(context->regs) );
296 context->regs[CTX_NATIVE].machine = native_machine;
297 context->regs[CTX_PENDING].machine = native_machine;
298 return context;
302 /* create a new thread */
303 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
305 struct desktop *desktop;
306 struct thread *thread;
307 int request_pipe[2];
309 if (fd == -1)
311 if (pipe( request_pipe ) == -1)
313 file_set_error();
314 return NULL;
316 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
318 close( request_pipe[0] );
319 close( request_pipe[1] );
320 return NULL;
322 close( request_pipe[1] );
323 fd = request_pipe[0];
326 if (process->is_terminating)
328 close( fd );
329 set_error( STATUS_PROCESS_IS_TERMINATING );
330 return NULL;
333 if (!(thread = alloc_object( &thread_ops )))
335 close( fd );
336 return NULL;
339 init_thread_structure( thread );
341 thread->process = (struct process *)grab_object( process );
342 thread->desktop = 0;
343 thread->affinity = process->affinity;
344 if (!current) current = thread;
346 list_add_tail( &thread_list, &thread->entry );
348 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
349 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
350 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
351 process->token ))
353 close( fd );
354 release_object( thread );
355 return NULL;
357 if (!(thread->id = alloc_ptid( thread )))
359 close( fd );
360 release_object( thread );
361 return NULL;
363 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
365 release_object( thread );
366 return NULL;
369 if (process->desktop)
371 if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) clear_error(); /* ignore errors */
372 else
374 set_thread_default_desktop( thread, desktop, process->desktop );
375 release_object( desktop );
379 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
380 add_process_thread( thread->process, thread );
381 return thread;
384 /* handle a client event */
385 static void thread_poll_event( struct fd *fd, int event )
387 struct thread *thread = get_fd_user( fd );
388 assert( thread->obj.ops == &thread_ops );
390 grab_object( thread );
391 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
392 else if (event & POLLIN) read_request( thread );
393 else if (event & POLLOUT) write_reply( thread );
394 release_object( thread );
397 static struct list *thread_get_kernel_obj_list( struct object *obj )
399 struct thread *thread = (struct thread *)obj;
400 return &thread->kernel_object;
403 /* cleanup everything that is no longer needed by a dead thread */
404 /* used by destroy_thread and kill_thread */
405 static void cleanup_thread( struct thread *thread )
407 int i;
409 if (thread->context)
411 thread->context->status = STATUS_ACCESS_DENIED;
412 wake_up( &thread->context->obj, 0 );
413 release_object( thread->context );
414 thread->context = NULL;
416 clear_apc_queue( &thread->system_apc );
417 clear_apc_queue( &thread->user_apc );
418 free( thread->req_data );
419 free( thread->reply_data );
420 if (thread->request_fd) release_object( thread->request_fd );
421 if (thread->reply_fd) release_object( thread->reply_fd );
422 if (thread->wait_fd) release_object( thread->wait_fd );
423 cleanup_clipboard_thread(thread);
424 destroy_thread_windows( thread );
425 free_msg_queue( thread );
426 release_thread_desktop( thread, 1 );
427 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
429 if (thread->inflight[i].client != -1)
431 close( thread->inflight[i].server );
432 thread->inflight[i].client = thread->inflight[i].server = -1;
435 free( thread->desc );
436 thread->req_data = NULL;
437 thread->reply_data = NULL;
438 thread->request_fd = NULL;
439 thread->reply_fd = NULL;
440 thread->wait_fd = NULL;
441 thread->desktop = 0;
442 thread->desc = NULL;
443 thread->desc_len = 0;
446 /* destroy a thread when its refcount is 0 */
447 static void destroy_thread( struct object *obj )
449 struct thread *thread = (struct thread *)obj;
450 assert( obj->ops == &thread_ops );
452 list_remove( &thread->entry );
453 cleanup_thread( thread );
454 release_object( thread->process );
455 if (thread->id) free_ptid( thread->id );
456 if (thread->token) release_object( thread->token );
459 /* dump a thread on stdout for debugging purposes */
460 static void dump_thread( struct object *obj, int verbose )
462 struct thread *thread = (struct thread *)obj;
463 assert( obj->ops == &thread_ops );
465 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
466 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
469 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
471 struct thread *mythread = (struct thread *)obj;
472 return (mythread->state == TERMINATED);
475 static unsigned int thread_map_access( struct object *obj, unsigned int access )
477 access = default_map_access( obj, access );
478 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
479 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
480 return access;
483 static void dump_thread_apc( struct object *obj, int verbose )
485 struct thread_apc *apc = (struct thread_apc *)obj;
486 assert( obj->ops == &thread_apc_ops );
488 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
491 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
493 struct thread_apc *apc = (struct thread_apc *)obj;
494 return apc->executed;
497 static void thread_apc_destroy( struct object *obj )
499 struct thread_apc *apc = (struct thread_apc *)obj;
501 if (apc->caller) release_object( apc->caller );
502 if (apc->owner)
504 if (apc->result.type == APC_ASYNC_IO)
505 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
506 else if (apc->call.type == APC_ASYNC_IO)
507 async_set_result( apc->owner, apc->call.async_io.status, 0 );
508 release_object( apc->owner );
512 /* queue an async procedure call */
513 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
515 struct thread_apc *apc;
517 if ((apc = alloc_object( &thread_apc_ops )))
519 apc->call = *call_data;
520 apc->caller = NULL;
521 apc->owner = owner;
522 apc->executed = 0;
523 apc->result.type = APC_NONE;
524 if (owner) grab_object( owner );
526 return apc;
529 /* get a thread pointer from a thread id (and increment the refcount) */
530 struct thread *get_thread_from_id( thread_id_t id )
532 struct object *obj = get_ptid_entry( id );
534 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
535 set_error( STATUS_INVALID_CID );
536 return NULL;
539 /* get a thread from a handle (and increment the refcount) */
540 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
542 return (struct thread *)get_handle_obj( current->process, handle,
543 access, &thread_ops );
546 /* find a thread from a Unix tid */
547 struct thread *get_thread_from_tid( int tid )
549 struct thread *thread;
551 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
553 if (thread->unix_tid == tid) return thread;
555 return NULL;
558 /* find a thread from a Unix pid */
559 struct thread *get_thread_from_pid( int pid )
561 struct thread *thread;
563 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
565 if (thread->unix_pid == pid) return thread;
567 return NULL;
570 int set_thread_affinity( struct thread *thread, affinity_t affinity )
572 int ret = 0;
573 #ifdef HAVE_SCHED_SETAFFINITY
574 if (thread->unix_tid != -1)
576 cpu_set_t set;
577 int i;
578 affinity_t mask;
580 CPU_ZERO( &set );
581 for (i = 0, mask = 1; mask; i++, mask <<= 1)
582 if (affinity & mask) CPU_SET( i, &set );
584 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
586 #endif
587 if (!ret) thread->affinity = affinity;
588 return ret;
591 affinity_t get_thread_affinity( struct thread *thread )
593 affinity_t mask = 0;
594 #ifdef HAVE_SCHED_SETAFFINITY
595 if (thread->unix_tid != -1)
597 cpu_set_t set;
598 unsigned int i;
600 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
601 for (i = 0; i < 8 * sizeof(mask); i++)
602 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
604 #endif
605 if (!mask) mask = ~(affinity_t)0;
606 return mask;
609 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
610 #define THREAD_PRIORITY_REALTIME_LOWEST -7
612 /* set all information about a thread */
613 static void set_thread_info( struct thread *thread,
614 const struct set_thread_info_request *req )
616 if (req->mask & SET_THREAD_INFO_PRIORITY)
618 int max = THREAD_PRIORITY_HIGHEST;
619 int min = THREAD_PRIORITY_LOWEST;
620 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
622 max = THREAD_PRIORITY_REALTIME_HIGHEST;
623 min = THREAD_PRIORITY_REALTIME_LOWEST;
625 if ((req->priority >= min && req->priority <= max) ||
626 req->priority == THREAD_PRIORITY_IDLE ||
627 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
628 thread->priority = req->priority;
629 else
630 set_error( STATUS_INVALID_PARAMETER );
632 if (req->mask & SET_THREAD_INFO_AFFINITY)
634 if ((req->affinity & thread->process->affinity) != req->affinity)
635 set_error( STATUS_INVALID_PARAMETER );
636 else if (thread->state == TERMINATED)
637 set_error( STATUS_THREAD_IS_TERMINATING );
638 else if (set_thread_affinity( thread, req->affinity ))
639 file_set_error();
641 if (req->mask & SET_THREAD_INFO_TOKEN)
642 security_set_thread_token( thread, req->token );
643 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
644 thread->entry_point = req->entry_point;
645 if (req->mask & SET_THREAD_INFO_DBG_HIDDEN)
646 thread->dbg_hidden = 1;
647 if (req->mask & SET_THREAD_INFO_DESCRIPTION)
649 WCHAR *desc;
650 data_size_t desc_len = get_req_data_size();
652 if (desc_len)
654 if ((desc = mem_alloc( desc_len )))
656 memcpy( desc, get_req_data(), desc_len );
657 free( thread->desc );
658 thread->desc = desc;
659 thread->desc_len = desc_len;
662 else
664 free( thread->desc );
665 thread->desc = NULL;
666 thread->desc_len = 0;
671 /* stop a thread (at the Unix level) */
672 void stop_thread( struct thread *thread )
674 if (thread->context) return; /* already suspended, no need for a signal */
675 if (!(thread->context = create_thread_context( thread ))) return;
676 /* can't stop a thread while initialisation is in progress */
677 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
680 /* suspend a thread */
681 int suspend_thread( struct thread *thread )
683 int old_count = thread->suspend;
684 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
686 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
688 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
689 return old_count;
692 /* resume a thread */
693 int resume_thread( struct thread *thread )
695 int old_count = thread->suspend;
696 if (thread->suspend > 0)
698 if (!(--thread->suspend)) resume_delayed_debug_events( thread );
699 if (!(thread->suspend + thread->process->suspend)) wake_thread( thread );
701 return old_count;
704 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
705 int add_queue( struct object *obj, struct wait_queue_entry *entry )
707 grab_object( obj );
708 entry->obj = obj;
709 list_add_tail( &obj->wait_queue, &entry->entry );
710 return 1;
713 /* remove a thread from an object wait queue */
714 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
716 list_remove( &entry->entry );
717 release_object( obj );
720 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
722 return entry->wait->thread;
725 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
727 return entry->wait->select;
730 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
732 return entry->wait->key;
735 void make_wait_abandoned( struct wait_queue_entry *entry )
737 entry->wait->abandoned = 1;
740 void set_wait_status( struct wait_queue_entry *entry, int status )
742 entry->wait->status = status;
745 /* finish waiting */
746 static unsigned int end_wait( struct thread *thread, unsigned int status )
748 struct thread_wait *wait = thread->wait;
749 struct wait_queue_entry *entry;
750 int i;
752 assert( wait );
753 thread->wait = wait->next;
755 if (status < wait->count) /* wait satisfied, tell it to the objects */
757 wait->status = status;
758 if (wait->select == SELECT_WAIT_ALL)
760 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
761 entry->obj->ops->satisfied( entry->obj, entry );
763 else
765 entry = wait->queues + status;
766 entry->obj->ops->satisfied( entry->obj, entry );
768 status = wait->status;
769 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
771 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
772 entry->obj->ops->remove_queue( entry->obj, entry );
773 if (wait->user) remove_timeout_user( wait->user );
774 free( wait );
775 return status;
778 /* build the thread wait structure */
779 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
780 int flags, abstime_t when )
782 struct thread_wait *wait;
783 struct wait_queue_entry *entry;
784 unsigned int i;
786 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
787 wait->next = current->wait;
788 wait->thread = current;
789 wait->count = count;
790 wait->flags = flags;
791 wait->select = select_op->op;
792 wait->cookie = 0;
793 wait->user = NULL;
794 wait->when = when;
795 wait->abandoned = 0;
796 current->wait = wait;
798 for (i = 0, entry = wait->queues; i < count; i++, entry++)
800 struct object *obj = objects[i];
801 entry->wait = wait;
802 if (!obj->ops->add_queue( obj, entry ))
804 wait->count = i;
805 end_wait( current, get_error() );
806 return 0;
809 return 1;
812 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
813 int flags, abstime_t when )
815 struct object *objects[MAXIMUM_WAIT_OBJECTS];
816 unsigned int i;
817 int ret = 0;
819 assert( count <= MAXIMUM_WAIT_OBJECTS );
821 for (i = 0; i < count; i++)
822 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
823 break;
825 if (i == count) ret = wait_on( select_op, count, objects, flags, when );
827 while (i > 0) release_object( objects[--i] );
828 return ret;
831 /* check if the thread waiting condition is satisfied */
832 static int check_wait( struct thread *thread )
834 int i;
835 struct thread_wait *wait = thread->wait;
836 struct wait_queue_entry *entry;
838 assert( wait );
840 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
841 return STATUS_KERNEL_APC;
843 /* Suspended threads may not acquire locks, but they can run system APCs */
844 if (thread->process->suspend + thread->suspend > 0) return -1;
846 if (wait->select == SELECT_WAIT_ALL)
848 int not_ok = 0;
849 /* Note: we must check them all anyway, as some objects may
850 * want to do something when signaled, even if others are not */
851 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
852 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
853 if (!not_ok) return STATUS_WAIT_0;
855 else
857 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
858 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
861 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
862 if (wait->when >= 0 && wait->when <= current_time) return STATUS_TIMEOUT;
863 if (wait->when < 0 && -wait->when <= monotonic_time) return STATUS_TIMEOUT;
864 return -1;
867 /* send the wakeup signal to a thread */
868 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
870 struct wake_up_reply reply;
871 int ret;
873 /* check if we're waking current suspend wait */
874 if (thread->context && thread->suspend_cookie == cookie
875 && signaled != STATUS_KERNEL_APC && signaled != STATUS_USER_APC)
877 if (!thread->context->regs[CTX_NATIVE].flags && !thread->context->regs[CTX_WOW].flags)
879 release_object( thread->context );
880 thread->context = NULL;
882 else signaled = STATUS_KERNEL_APC; /* signal a fake APC so that client calls select to get a new context */
885 memset( &reply, 0, sizeof(reply) );
886 reply.cookie = cookie;
887 reply.signaled = signaled;
888 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
889 return 0;
890 if (ret >= 0)
891 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
892 else if (errno == EPIPE)
893 kill_thread( thread, 0 ); /* normal death */
894 else
895 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
896 return -1;
899 /* attempt to wake up a thread */
900 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
901 int wake_thread( struct thread *thread )
903 int signaled, count;
904 client_ptr_t cookie;
906 for (count = 0; thread->wait; count++)
908 if ((signaled = check_wait( thread )) == -1) break;
910 cookie = thread->wait->cookie;
911 signaled = end_wait( thread, signaled );
912 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
913 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
915 if (!count) count = -1;
916 break;
919 return count;
922 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
923 int wake_thread_queue_entry( struct wait_queue_entry *entry )
925 struct thread_wait *wait = entry->wait;
926 struct thread *thread = wait->thread;
927 int signaled;
928 client_ptr_t cookie;
930 if (thread->wait != wait) return 0; /* not the current wait */
931 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
933 assert( wait->select != SELECT_WAIT_ALL );
935 cookie = wait->cookie;
936 signaled = end_wait( thread, entry - wait->queues );
937 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
939 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
940 wake_thread( thread ); /* check other waits too */
942 return 1;
945 /* thread wait timeout */
946 static void thread_timeout( void *ptr )
948 struct thread_wait *wait = ptr;
949 struct thread *thread = wait->thread;
950 client_ptr_t cookie = wait->cookie;
952 wait->user = NULL;
953 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
954 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
956 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
957 end_wait( thread, STATUS_TIMEOUT );
959 assert( cookie );
960 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
961 /* check if other objects have become signaled in the meantime */
962 wake_thread( thread );
965 /* try signaling an event flag, a semaphore or a mutex */
966 static int signal_object( obj_handle_t handle )
968 struct object *obj;
969 int ret = 0;
971 obj = get_handle_obj( current->process, handle, 0, NULL );
972 if (obj)
974 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
975 release_object( obj );
977 return ret;
980 /* select on a list of handles */
981 static int select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
982 int flags, abstime_t when )
984 int ret;
985 unsigned int count;
986 struct object *object;
988 switch (select_op->op)
990 case SELECT_NONE:
991 if (!wait_on( select_op, 0, NULL, flags, when )) return 1;
992 break;
994 case SELECT_WAIT:
995 case SELECT_WAIT_ALL:
996 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
997 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
999 set_error( STATUS_INVALID_PARAMETER );
1000 return 1;
1002 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, when ))
1003 return 1;
1004 break;
1006 case SELECT_SIGNAL_AND_WAIT:
1007 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, when ))
1008 return 1;
1009 if (select_op->signal_and_wait.signal)
1011 if (!signal_object( select_op->signal_and_wait.signal ))
1013 end_wait( current, get_error() );
1014 return 1;
1016 /* check if we woke ourselves up */
1017 if (!current->wait) return 1;
1019 break;
1021 case SELECT_KEYED_EVENT_WAIT:
1022 case SELECT_KEYED_EVENT_RELEASE:
1023 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
1024 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
1025 if (!object) return 1;
1026 ret = wait_on( select_op, 1, &object, flags, when );
1027 release_object( object );
1028 if (!ret) return 1;
1029 current->wait->key = select_op->keyed_event.key;
1030 break;
1032 default:
1033 set_error( STATUS_INVALID_PARAMETER );
1034 return 1;
1037 if ((ret = check_wait( current )) != -1)
1039 /* condition is already satisfied */
1040 set_error( end_wait( current, ret ));
1041 return 1;
1044 /* now we need to wait */
1045 if (current->wait->when != TIMEOUT_INFINITE)
1047 if (!(current->wait->user = add_timeout_user( abstime_to_timeout(current->wait->when),
1048 thread_timeout, current->wait )))
1050 end_wait( current, get_error() );
1051 return 1;
1054 current->wait->cookie = cookie;
1055 set_error( STATUS_PENDING );
1056 return 0;
1059 /* attempt to wake threads sleeping on the object wait queue */
1060 void wake_up( struct object *obj, int max )
1062 struct list *ptr;
1063 int ret;
1065 LIST_FOR_EACH( ptr, &obj->wait_queue )
1067 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
1068 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
1069 if (ret > 0 && max && !--max) break;
1070 /* restart at the head of the list since a wake up can change the object wait queue */
1071 ptr = &obj->wait_queue;
1075 /* return the apc queue to use for a given apc type */
1076 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
1078 switch(type)
1080 case APC_NONE:
1081 return NULL;
1082 case APC_USER:
1083 return &thread->user_apc;
1084 default:
1085 return &thread->system_apc;
1089 /* check if thread is currently waiting for a (system) apc */
1090 static inline int is_in_apc_wait( struct thread *thread )
1092 return (thread->process->suspend || thread->suspend ||
1093 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
1096 /* queue an existing APC to a given thread */
1097 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
1099 struct list *queue;
1101 if (thread && thread->state == TERMINATED && process)
1102 thread = NULL;
1104 if (!thread) /* find a suitable thread inside the process */
1106 struct thread *candidate;
1108 /* first try to find a waiting thread */
1109 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1111 if (candidate->state == TERMINATED) continue;
1112 if (is_in_apc_wait( candidate ))
1114 thread = candidate;
1115 break;
1118 if (!thread)
1120 /* then use the first one that accepts a signal */
1121 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1123 if (send_thread_signal( candidate, SIGUSR1 ))
1125 thread = candidate;
1126 break;
1130 if (!thread) return 0; /* nothing found */
1131 if (!(queue = get_apc_queue( thread, apc->call.type ))) return 1;
1133 else
1135 if (thread->state == TERMINATED) return 0;
1136 if (!(queue = get_apc_queue( thread, apc->call.type ))) return 1;
1137 /* send signal for system APCs if needed */
1138 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1140 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1142 /* cancel a possible previous APC with the same owner */
1143 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1146 grab_object( apc );
1147 list_add_tail( queue, &apc->entry );
1148 if (!list_prev( queue, &apc->entry )) /* first one */
1149 wake_thread( thread );
1151 return 1;
1154 /* queue an async procedure call */
1155 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1157 struct thread_apc *apc;
1158 int ret = 0;
1160 if ((apc = create_apc( owner, call_data )))
1162 ret = queue_apc( process, thread, apc );
1163 release_object( apc );
1165 return ret;
1168 /* cancel the async procedure call owned by a specific object */
1169 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1171 struct thread_apc *apc;
1172 struct list *queue = get_apc_queue( thread, type );
1174 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1176 if (apc->owner != owner) continue;
1177 list_remove( &apc->entry );
1178 apc->executed = 1;
1179 wake_up( &apc->obj, 0 );
1180 release_object( apc );
1181 return;
1185 /* remove the head apc from the queue; the returned object must be released by the caller */
1186 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system )
1188 struct thread_apc *apc = NULL;
1189 struct list *ptr = list_head( system ? &thread->system_apc : &thread->user_apc );
1191 if (ptr)
1193 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1194 list_remove( ptr );
1196 return apc;
1199 /* clear an APC queue, cancelling all the APCs on it */
1200 static void clear_apc_queue( struct list *queue )
1202 struct list *ptr;
1204 while ((ptr = list_head( queue )))
1206 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1207 list_remove( &apc->entry );
1208 apc->executed = 1;
1209 wake_up( &apc->obj, 0 );
1210 release_object( apc );
1214 /* add an fd to the inflight list */
1215 /* return list index, or -1 on error */
1216 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1218 int i;
1220 if (server == -1) return -1;
1221 if (client == -1)
1223 close( server );
1224 return -1;
1227 /* first check if we already have an entry for this fd */
1228 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1229 if (thread->inflight[i].client == client)
1231 close( thread->inflight[i].server );
1232 thread->inflight[i].server = server;
1233 return i;
1236 /* now find a free spot to store it */
1237 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1238 if (thread->inflight[i].client == -1)
1240 thread->inflight[i].client = client;
1241 thread->inflight[i].server = server;
1242 return i;
1245 close( server );
1246 return -1;
1249 /* get an inflight fd and purge it from the list */
1250 /* the fd must be closed when no longer used */
1251 int thread_get_inflight_fd( struct thread *thread, int client )
1253 int i, ret;
1255 if (client == -1) return -1;
1259 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1261 if (thread->inflight[i].client == client)
1263 ret = thread->inflight[i].server;
1264 thread->inflight[i].server = thread->inflight[i].client = -1;
1265 return ret;
1268 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1269 return -1;
1272 /* kill a thread on the spot */
1273 void kill_thread( struct thread *thread, int violent_death )
1275 if (thread->state == TERMINATED) return; /* already killed */
1276 thread->state = TERMINATED;
1277 thread->exit_time = current_time;
1278 if (current == thread) current = NULL;
1279 if (debug_level)
1280 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1281 thread->id, thread->exit_code );
1282 if (thread->wait)
1284 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1285 send_thread_wakeup( thread, 0, thread->exit_code );
1286 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1287 violent_death = 0;
1289 kill_console_processes( thread, 0 );
1290 abandon_mutexes( thread );
1291 wake_up( &thread->obj, 0 );
1292 if (violent_death) send_thread_signal( thread, SIGQUIT );
1293 cleanup_thread( thread );
1294 remove_process_thread( thread->process, thread );
1295 release_object( thread );
1298 /* copy parts of a context structure */
1299 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1301 assert( to->machine == from->machine );
1302 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1303 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1304 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1305 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1306 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1307 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1308 if (flags & SERVER_CTX_YMM_REGISTERS) to->ymm = from->ymm;
1311 /* gets the current impersonation token */
1312 struct token *thread_get_impersonation_token( struct thread *thread )
1314 if (thread->token)
1315 return thread->token;
1316 else
1317 return thread->process->token;
1320 /* create a new thread */
1321 DECL_HANDLER(new_thread)
1323 struct thread *thread;
1324 struct process *process;
1325 struct unicode_str name;
1326 const struct security_descriptor *sd;
1327 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1328 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1330 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1332 if (request_fd != -1) close( request_fd );
1333 return;
1336 if (process != current->process)
1338 if (request_fd != -1) /* can't create a request fd in a different process */
1340 close( request_fd );
1341 set_error( STATUS_INVALID_PARAMETER );
1342 goto done;
1344 if (process->running_threads) /* only the initial thread can be created in another process */
1346 set_error( STATUS_ACCESS_DENIED );
1347 goto done;
1350 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1352 if (request_fd != -1) close( request_fd );
1353 set_error( STATUS_INVALID_HANDLE );
1354 goto done;
1357 if ((thread = create_thread( request_fd, process, sd )))
1359 thread->system_regs = current->system_regs;
1360 if (req->flags & THREAD_CREATE_FLAGS_CREATE_SUSPENDED) thread->suspend++;
1361 thread->dbg_hidden = !!(req->flags & THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER);
1362 reply->tid = get_thread_id( thread );
1363 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1364 req->access, objattr->attributes )))
1366 /* thread object will be released when the thread gets killed */
1367 goto done;
1369 kill_thread( thread, 1 );
1371 done:
1372 release_object( process );
1375 static int init_thread( struct thread *thread, int reply_fd, int wait_fd )
1377 if ((reply_fd = thread_get_inflight_fd( thread, reply_fd )) == -1)
1379 set_error( STATUS_TOO_MANY_OPENED_FILES );
1380 return 0;
1382 if ((wait_fd = thread_get_inflight_fd( thread, wait_fd )) == -1)
1384 set_error( STATUS_TOO_MANY_OPENED_FILES );
1385 goto error;
1388 if (thread->reply_fd) /* already initialised */
1390 set_error( STATUS_INVALID_PARAMETER );
1391 goto error;
1394 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1396 thread->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &thread->obj, 0 );
1397 thread->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &thread->obj, 0 );
1398 return thread->reply_fd && thread->wait_fd;
1400 error:
1401 if (reply_fd != -1) close( reply_fd );
1402 if (wait_fd != -1) close( wait_fd );
1403 return 0;
1406 /* initialize the first thread of a new process */
1407 DECL_HANDLER(init_first_thread)
1409 struct process *process = current->process;
1411 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1413 current->unix_pid = process->unix_pid = req->unix_pid;
1414 current->unix_tid = req->unix_tid;
1416 if (!process->parent_id)
1417 process->affinity = current->affinity = get_thread_affinity( current );
1418 else
1419 set_thread_affinity( current, current->affinity );
1421 debug_level = max( debug_level, req->debug_level );
1423 reply->pid = get_process_id( process );
1424 reply->tid = get_thread_id( current );
1425 reply->session_id = process->session_id;
1426 reply->info_size = get_process_startup_info_size( process );
1427 reply->server_start = server_start_time;
1428 set_reply_data( supported_machines,
1429 min( supported_machines_count * sizeof(unsigned short), get_reply_max_size() ));
1432 /* initialize a new thread */
1433 DECL_HANDLER(init_thread)
1435 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1437 if (!is_valid_address(req->teb))
1439 set_error( STATUS_INVALID_PARAMETER );
1440 return;
1443 current->unix_pid = current->process->unix_pid;
1444 current->unix_tid = req->unix_tid;
1445 current->teb = req->teb;
1446 current->entry_point = req->entry;
1448 init_thread_context( current );
1449 generate_debug_event( current, DbgCreateThreadStateChange, &req->entry );
1450 set_thread_affinity( current, current->affinity );
1452 reply->suspend = (current->suspend || current->process->suspend || current->context != NULL);
1455 /* terminate a thread */
1456 DECL_HANDLER(terminate_thread)
1458 struct thread *thread;
1460 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1462 thread->exit_code = req->exit_code;
1463 if (thread != current) kill_thread( thread, 1 );
1464 else reply->self = 1;
1465 release_object( thread );
1469 /* open a handle to a thread */
1470 DECL_HANDLER(open_thread)
1472 struct thread *thread = get_thread_from_id( req->tid );
1474 reply->handle = 0;
1475 if (thread)
1477 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1478 release_object( thread );
1482 /* fetch information about a thread */
1483 DECL_HANDLER(get_thread_info)
1485 struct thread *thread;
1486 unsigned int access = req->access & (THREAD_QUERY_INFORMATION | THREAD_QUERY_LIMITED_INFORMATION);
1488 if (!access) access = THREAD_QUERY_LIMITED_INFORMATION;
1489 thread = get_thread_from_handle( req->handle, access );
1490 if (thread)
1492 reply->pid = get_process_id( thread->process );
1493 reply->tid = get_thread_id( thread );
1494 reply->teb = thread->teb;
1495 reply->entry_point = thread->entry_point;
1496 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1497 reply->priority = thread->priority;
1498 reply->affinity = thread->affinity;
1499 reply->last = thread->process->running_threads == 1;
1500 reply->suspend_count = thread->suspend;
1501 reply->dbg_hidden = thread->dbg_hidden;
1502 reply->desc_len = thread->desc_len;
1504 if (thread->desc && get_reply_max_size())
1506 if (thread->desc_len <= get_reply_max_size())
1507 set_reply_data( thread->desc, thread->desc_len );
1508 else
1509 set_error( STATUS_BUFFER_TOO_SMALL );
1512 release_object( thread );
1516 /* fetch information about thread times */
1517 DECL_HANDLER(get_thread_times)
1519 struct thread *thread;
1521 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION )))
1523 reply->creation_time = thread->creation_time;
1524 reply->exit_time = thread->exit_time;
1525 reply->unix_pid = thread->unix_pid;
1526 reply->unix_tid = thread->unix_tid;
1528 release_object( thread );
1532 /* set information about a thread */
1533 DECL_HANDLER(set_thread_info)
1535 struct thread *thread;
1537 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1539 set_thread_info( thread, req );
1540 release_object( thread );
1544 /* suspend a thread */
1545 DECL_HANDLER(suspend_thread)
1547 struct thread *thread;
1549 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1551 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1552 else reply->count = suspend_thread( thread );
1553 release_object( thread );
1557 /* resume a thread */
1558 DECL_HANDLER(resume_thread)
1560 struct thread *thread;
1562 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1564 reply->count = resume_thread( thread );
1565 release_object( thread );
1569 /* select on a handle list */
1570 DECL_HANDLER(select)
1572 select_op_t select_op;
1573 data_size_t op_size, ctx_size;
1574 struct context *ctx;
1575 struct thread_apc *apc;
1576 const apc_result_t *result = get_req_data();
1577 unsigned int ctx_count;
1579 if (get_req_data_size() < sizeof(*result)) goto invalid_param;
1580 if (get_req_data_size() - sizeof(*result) < req->size) goto invalid_param;
1581 if (req->size & 3) goto invalid_param;
1582 ctx_size = get_req_data_size() - sizeof(*result) - req->size;
1583 ctx_count = ctx_size / sizeof(context_t);
1584 if (ctx_count * sizeof(context_t) != ctx_size) goto invalid_param;
1585 if (ctx_count > 1 + (current->process->machine != native_machine)) goto invalid_param;
1587 if (ctx_count)
1589 const context_t *native_context = (const context_t *)((const char *)(result + 1) + req->size);
1590 const context_t *wow_context = (ctx_count > 1) ? native_context + 1 : NULL;
1592 if (current->context && current->context->status != STATUS_PENDING) goto invalid_param;
1594 if (native_context->machine == native_machine)
1596 if (wow_context && wow_context->machine != current->process->machine) goto invalid_param;
1598 else if (native_context->machine == current->process->machine)
1600 if (wow_context) goto invalid_param;
1601 wow_context = native_context;
1602 native_context = NULL;
1604 else goto invalid_param;
1606 if (!current->context && !(current->context = create_thread_context( current ))) return;
1608 ctx = current->context;
1609 if (native_context)
1611 copy_context( &ctx->regs[CTX_NATIVE], native_context,
1612 native_context->flags & ~(ctx->regs[CTX_NATIVE].flags | system_flags) );
1614 if (wow_context)
1616 ctx->regs[CTX_WOW].machine = current->process->machine;
1617 copy_context( &ctx->regs[CTX_WOW], wow_context, wow_context->flags & ~ctx->regs[CTX_WOW].flags );
1619 else if (ctx->regs[CTX_PENDING].flags)
1621 unsigned int flags = ctx->regs[CTX_PENDING].flags & ~ctx->regs[CTX_NATIVE].flags;
1622 copy_context( &ctx->regs[CTX_NATIVE], &ctx->regs[CTX_PENDING], flags );
1623 ctx->regs[CTX_NATIVE].flags |= flags;
1625 ctx->regs[CTX_PENDING].flags = 0;
1626 ctx->status = STATUS_SUCCESS;
1627 current->suspend_cookie = req->cookie;
1628 wake_up( &ctx->obj, 0 );
1631 if (!req->cookie) goto invalid_param;
1633 op_size = min( req->size, sizeof(select_op) );
1634 memset( &select_op, 0, sizeof(select_op) );
1635 memcpy( &select_op, result + 1, op_size );
1637 /* first store results of previous apc */
1638 if (req->prev_apc)
1640 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1641 0, &thread_apc_ops ))) return;
1642 apc->result = *result;
1643 apc->executed = 1;
1644 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1646 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1647 apc->caller->process, 0, 0, DUPLICATE_SAME_ACCESS );
1648 close_handle( current->process, apc->result.create_thread.handle );
1649 apc->result.create_thread.handle = handle;
1650 clear_error(); /* ignore errors from the above calls */
1652 wake_up( &apc->obj, 0 );
1653 close_handle( current->process, req->prev_apc );
1654 release_object( apc );
1657 reply->signaled = select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1659 if (get_error() == STATUS_USER_APC)
1661 apc = thread_dequeue_apc( current, 0 );
1662 reply->call = apc->call;
1663 release_object( apc );
1665 else if (get_error() == STATUS_KERNEL_APC)
1667 apc = thread_dequeue_apc( current, 1 );
1668 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1669 reply->call = apc->call;
1670 else
1672 apc->executed = 1;
1673 wake_up( &apc->obj, 0 );
1675 release_object( apc );
1677 else if (reply->signaled && get_reply_max_size() >= sizeof(context_t) &&
1678 current->context && current->suspend_cookie == req->cookie)
1680 ctx = current->context;
1681 if (ctx->regs[CTX_NATIVE].flags || ctx->regs[CTX_WOW].flags)
1683 data_size_t size = (ctx->regs[CTX_WOW].flags ? 2 : 1) * sizeof(context_t);
1684 unsigned int flags = system_flags & ctx->regs[CTX_NATIVE].flags;
1685 if (flags) set_thread_context( current, &ctx->regs[CTX_NATIVE], flags );
1686 set_reply_data( ctx->regs, min( size, get_reply_max_size() ));
1688 release_object( ctx );
1689 current->context = NULL;
1691 return;
1693 invalid_param:
1694 set_error( STATUS_INVALID_PARAMETER );
1697 /* queue an APC for a thread or process */
1698 DECL_HANDLER(queue_apc)
1700 struct thread *thread = NULL;
1701 struct process *process = NULL;
1702 struct thread_apc *apc;
1704 if (!(apc = create_apc( NULL, &req->call ))) return;
1706 switch (apc->call.type)
1708 case APC_NONE:
1709 case APC_USER:
1710 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1711 break;
1712 case APC_VIRTUAL_ALLOC:
1713 case APC_VIRTUAL_FREE:
1714 case APC_VIRTUAL_PROTECT:
1715 case APC_VIRTUAL_FLUSH:
1716 case APC_VIRTUAL_LOCK:
1717 case APC_VIRTUAL_UNLOCK:
1718 case APC_UNMAP_VIEW:
1719 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1720 break;
1721 case APC_VIRTUAL_QUERY:
1722 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1723 break;
1724 case APC_MAP_VIEW:
1725 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1726 if (process && process != current->process)
1728 /* duplicate the handle into the target process */
1729 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1730 process, 0, 0, DUPLICATE_SAME_ACCESS );
1731 if (handle) apc->call.map_view.handle = handle;
1732 else
1734 release_object( process );
1735 process = NULL;
1738 break;
1739 case APC_CREATE_THREAD:
1740 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1741 break;
1742 case APC_DUP_HANDLE:
1743 process = get_process_from_handle( req->handle, PROCESS_DUP_HANDLE );
1744 if (process && process != current->process)
1746 /* duplicate the destination process handle into the target process */
1747 obj_handle_t handle = duplicate_handle( current->process, apc->call.dup_handle.dst_process,
1748 process, 0, 0, DUPLICATE_SAME_ACCESS );
1749 if (handle) apc->call.dup_handle.dst_process = handle;
1750 else
1752 release_object( process );
1753 process = NULL;
1756 break;
1757 default:
1758 set_error( STATUS_INVALID_PARAMETER );
1759 break;
1762 if (thread)
1764 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_UNSUCCESSFUL );
1765 release_object( thread );
1767 else if (process)
1769 reply->self = (process == current->process);
1770 if (!reply->self)
1772 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1773 if (handle)
1775 if (queue_apc( process, NULL, apc ))
1777 apc->caller = (struct thread *)grab_object( current );
1778 reply->handle = handle;
1780 else
1782 close_handle( current->process, handle );
1783 set_error( STATUS_PROCESS_IS_TERMINATING );
1787 release_object( process );
1790 release_object( apc );
1793 /* Get the result of an APC call */
1794 DECL_HANDLER(get_apc_result)
1796 struct thread_apc *apc;
1798 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1799 0, &thread_apc_ops ))) return;
1801 if (apc->executed) reply->result = apc->result;
1802 else set_error( STATUS_PENDING );
1804 /* close the handle directly to avoid an extra round-trip */
1805 close_handle( current->process, req->handle );
1806 release_object( apc );
1809 /* retrieve the current context of a thread */
1810 DECL_HANDLER(get_thread_context)
1812 struct context *thread_context = NULL;
1813 struct thread *thread;
1814 context_t *context;
1816 if (get_reply_max_size() < 2 * sizeof(context_t))
1818 set_error( STATUS_INVALID_PARAMETER );
1819 return;
1822 if (req->context)
1824 if (!(thread_context = (struct context *)get_handle_obj( current->process, req->context,
1825 0, &context_ops )))
1826 return;
1827 close_handle( current->process, req->context ); /* avoid extra server call */
1829 else
1831 if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
1832 if (req->machine != native_machine && req->machine != thread->process->machine)
1833 set_error( STATUS_INVALID_PARAMETER );
1834 else if (thread->state != RUNNING)
1835 set_error( STATUS_UNSUCCESSFUL );
1836 else
1838 reply->self = (thread == current);
1839 if (thread != current) stop_thread( thread );
1840 if (thread->context)
1842 /* make sure that system regs are valid in thread context */
1843 if (thread->unix_tid != -1 && (system_flags & ~thread->context->regs[CTX_NATIVE].flags))
1844 get_thread_context( thread, &thread->context->regs[CTX_NATIVE], system_flags );
1845 if (!get_error()) thread_context = (struct context *)grab_object( thread->context );
1847 else if (!get_error() && (context = set_reply_data_size( sizeof(context_t) )))
1849 assert( reply->self );
1850 memset( context, 0, sizeof(context_t) );
1851 context->machine = native_machine;
1852 if (system_flags) get_thread_context( thread, context, system_flags );
1855 release_object( thread );
1856 if (!thread_context) return;
1859 if (!thread_context->status)
1861 unsigned int native_flags = req->flags, wow_flags = 0;
1863 if (req->machine == thread_context->regs[CTX_WOW].machine)
1865 native_flags = req->flags & always_native_flags;
1866 wow_flags = req->flags & ~always_native_flags;
1868 if ((context = set_reply_data_size( (!!native_flags + !!wow_flags) * sizeof(context_t) )))
1870 if (native_flags)
1872 memset( context, 0, sizeof(*context) );
1873 context->machine = thread_context->regs[CTX_NATIVE].machine;
1874 copy_context( context, &thread_context->regs[CTX_NATIVE], native_flags );
1875 context->flags = native_flags;
1876 context++;
1878 if (wow_flags)
1880 memset( context, 0, sizeof(*context) );
1881 context->machine = thread_context->regs[CTX_WOW].machine;
1882 copy_context( context, &thread_context->regs[CTX_WOW], wow_flags );
1883 context->flags = wow_flags;
1887 else
1889 set_error( thread_context->status );
1890 if (thread_context->status == STATUS_PENDING)
1891 reply->handle = alloc_handle( current->process, thread_context, SYNCHRONIZE, 0 );
1894 release_object( thread_context );
1897 /* set the current context of a thread */
1898 DECL_HANDLER(set_thread_context)
1900 struct thread *thread;
1901 const context_t *contexts = get_req_data();
1902 unsigned int ctx_count = get_req_data_size() / sizeof(context_t);
1904 if (!ctx_count || ctx_count > 2 || ctx_count * sizeof(context_t) != get_req_data_size())
1906 set_error( STATUS_INVALID_PARAMETER );
1907 return;
1910 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1911 reply->self = (thread == current);
1913 if (contexts[CTX_NATIVE].machine != native_machine ||
1914 (ctx_count == 2 && contexts[CTX_WOW].machine != thread->process->machine))
1915 set_error( STATUS_INVALID_PARAMETER );
1916 else if (thread->state != TERMINATED)
1918 unsigned int ctx = CTX_NATIVE;
1919 const context_t *context = &contexts[CTX_NATIVE];
1920 unsigned int flags = system_flags & context->flags;
1921 unsigned int native_flags = always_native_flags & context->flags;
1923 if (thread != current) stop_thread( thread );
1924 else if (flags) set_thread_context( thread, context, flags );
1925 if (thread->context && !get_error())
1927 if (ctx_count == 2)
1929 /* If the target thread doesn't have a WoW context, set native instead.
1930 * If we don't know yet whether we have a WoW context, store native context
1931 * in CTX_PENDING and update when the target thread sends its context(s). */
1932 if (thread->context->status != STATUS_PENDING)
1934 ctx = thread->context->regs[CTX_WOW].machine ? CTX_WOW : CTX_NATIVE;
1935 context = &contexts[ctx];
1937 else ctx = CTX_PENDING;
1939 flags = context->flags;
1940 if (native_flags && ctx != CTX_NATIVE) /* some regs are always set from the native context */
1942 copy_context( &thread->context->regs[CTX_NATIVE], &contexts[CTX_NATIVE], native_flags );
1943 thread->context->regs[CTX_NATIVE].flags |= native_flags;
1944 flags &= ~native_flags;
1946 copy_context( &thread->context->regs[ctx], context, flags );
1947 thread->context->regs[ctx].flags |= flags;
1950 else set_error( STATUS_UNSUCCESSFUL );
1952 release_object( thread );
1955 /* fetch a selector entry for a thread */
1956 DECL_HANDLER(get_selector_entry)
1958 struct thread *thread;
1959 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1961 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1962 release_object( thread );
1966 /* Iterate thread list for process. Use global thread list to also
1967 * return terminated but not yet destroyed threads. */
1968 DECL_HANDLER(get_next_thread)
1970 struct thread *thread;
1971 struct process *process;
1972 struct list *ptr;
1974 if (req->flags > 1)
1976 set_error( STATUS_INVALID_PARAMETER );
1977 return;
1980 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1981 return;
1983 if (!req->last)
1985 ptr = req->flags ? list_tail( &thread_list ) : list_head( &thread_list );
1987 else if ((thread = get_thread_from_handle( req->last, 0 )))
1989 ptr = req->flags ? list_prev( &thread_list, &thread->entry )
1990 : list_next( &thread_list, &thread->entry );
1991 release_object( thread );
1993 else
1995 release_object( process );
1996 return;
1999 while (ptr)
2001 thread = LIST_ENTRY( ptr, struct thread, entry );
2002 if (thread->process == process)
2004 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
2005 release_object( process );
2006 return;
2008 ptr = req->flags ? list_prev( &thread_list, &thread->entry )
2009 : list_next( &thread_list, &thread->entry );
2011 set_error( STATUS_NO_MORE_ENTRIES );
2012 release_object( process );