reg/tests: Use string literals instead of a char buffer for REG_MULTI_SZ tests.
[wine.git] / server / thread.c
blob2f11d7d51c2413645081dd029d02ca13fdb7c178
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"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <time.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
38 #ifdef HAVE_SCHED_H
39 #include <sched.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
45 #include "winternl.h"
47 #include "file.h"
48 #include "handle.h"
49 #include "process.h"
50 #include "thread.h"
51 #include "request.h"
52 #include "user.h"
53 #include "security.h"
56 /* thread queues */
58 struct thread_wait
60 struct thread_wait *next; /* next wait structure for this thread */
61 struct thread *thread; /* owner thread */
62 int count; /* count of objects */
63 int flags;
64 int abandoned;
65 enum select_op select;
66 client_ptr_t key; /* wait key for keyed events */
67 client_ptr_t cookie; /* magic cookie to return to client */
68 abstime_t when;
69 struct timeout_user *user;
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; /* context data */
125 static void dump_context( struct object *obj, int verbose );
126 static int context_signaled( struct object *obj, struct wait_queue_entry *entry );
128 static const struct object_ops context_ops =
130 sizeof(struct context), /* size */
131 &no_type, /* type */
132 dump_context, /* dump */
133 add_queue, /* add_queue */
134 remove_queue, /* remove_queue */
135 context_signaled, /* signaled */
136 no_satisfied, /* satisfied */
137 no_signal, /* signal */
138 no_get_fd, /* get_fd */
139 default_map_access, /* map_access */
140 default_get_sd, /* get_sd */
141 default_set_sd, /* set_sd */
142 no_get_full_name, /* get_full_name */
143 no_lookup_name, /* lookup_name */
144 no_link_name, /* link_name */
145 NULL, /* unlink_name */
146 no_open_file, /* open_file */
147 no_kernel_obj_list, /* get_kernel_obj_list */
148 no_close_handle, /* close_handle */
149 no_destroy /* destroy */
153 /* thread operations */
155 static const WCHAR thread_name[] = {'T','h','r','e','a','d'};
157 struct type_descr thread_type =
159 { thread_name, sizeof(thread_name) }, /* name */
160 THREAD_ALL_ACCESS, /* valid_access */
161 { /* mapping */
162 STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT,
163 STANDARD_RIGHTS_WRITE | THREAD_SET_LIMITED_INFORMATION | THREAD_SET_INFORMATION
164 | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_TERMINATE | 0x04,
165 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_RESUME | THREAD_QUERY_LIMITED_INFORMATION,
166 THREAD_ALL_ACCESS
170 static void dump_thread( struct object *obj, int verbose );
171 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
172 static unsigned int thread_map_access( struct object *obj, unsigned int access );
173 static void thread_poll_event( struct fd *fd, int event );
174 static struct list *thread_get_kernel_obj_list( struct object *obj );
175 static void destroy_thread( struct object *obj );
177 static const struct object_ops thread_ops =
179 sizeof(struct thread), /* size */
180 &thread_type, /* type */
181 dump_thread, /* dump */
182 add_queue, /* add_queue */
183 remove_queue, /* remove_queue */
184 thread_signaled, /* signaled */
185 no_satisfied, /* satisfied */
186 no_signal, /* signal */
187 no_get_fd, /* get_fd */
188 thread_map_access, /* map_access */
189 default_get_sd, /* get_sd */
190 default_set_sd, /* set_sd */
191 no_get_full_name, /* get_full_name */
192 no_lookup_name, /* lookup_name */
193 no_link_name, /* link_name */
194 NULL, /* unlink_name */
195 no_open_file, /* open_file */
196 thread_get_kernel_obj_list, /* get_kernel_obj_list */
197 no_close_handle, /* close_handle */
198 destroy_thread /* destroy */
201 static const struct fd_ops thread_fd_ops =
203 NULL, /* get_poll_events */
204 thread_poll_event, /* poll_event */
205 NULL, /* flush */
206 NULL, /* get_fd_type */
207 NULL, /* ioctl */
208 NULL, /* queue_async */
209 NULL /* reselect_async */
212 static struct list thread_list = LIST_INIT(thread_list);
214 /* initialize the structure for a newly allocated thread */
215 static inline void init_thread_structure( struct thread *thread )
217 int i;
219 thread->unix_pid = -1; /* not known yet */
220 thread->unix_tid = -1; /* not known yet */
221 thread->context = NULL;
222 thread->teb = 0;
223 thread->entry_point = 0;
224 thread->system_regs = 0;
225 thread->queue = NULL;
226 thread->wait = NULL;
227 thread->error = 0;
228 thread->req_data = NULL;
229 thread->req_toread = 0;
230 thread->reply_data = NULL;
231 thread->reply_towrite = 0;
232 thread->request_fd = NULL;
233 thread->reply_fd = NULL;
234 thread->wait_fd = NULL;
235 thread->state = RUNNING;
236 thread->exit_code = 0;
237 thread->priority = 0;
238 thread->suspend = 0;
239 thread->dbg_hidden = 0;
240 thread->desktop_users = 0;
241 thread->token = NULL;
242 thread->desc = NULL;
243 thread->desc_len = 0;
245 thread->creation_time = current_time;
246 thread->exit_time = 0;
248 list_init( &thread->mutex_list );
249 list_init( &thread->system_apc );
250 list_init( &thread->user_apc );
251 list_init( &thread->kernel_object );
253 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
254 thread->inflight[i].server = thread->inflight[i].client = -1;
257 /* check if address looks valid for a client-side data structure (TEB etc.) */
258 static inline int is_valid_address( client_ptr_t addr )
260 return addr && !(addr % sizeof(int));
264 /* dump a context on stdout for debugging purposes */
265 static void dump_context( struct object *obj, int verbose )
267 struct context *context = (struct context *)obj;
268 assert( obj->ops == &context_ops );
270 fprintf( stderr, "context flags=%x\n", context->regs.flags );
274 static int context_signaled( struct object *obj, struct wait_queue_entry *entry )
276 struct context *context = (struct context *)obj;
277 return context->status != STATUS_PENDING;
281 static struct context *create_thread_context( struct thread *thread )
283 struct context *context;
284 if (!(context = alloc_object( &context_ops ))) return NULL;
285 context->status = STATUS_PENDING;
286 memset( &context->regs, 0, sizeof(context->regs) );
287 context->regs.machine = thread->process->machine;
288 return context;
292 /* create a new thread */
293 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
295 struct desktop *desktop;
296 struct thread *thread;
297 int request_pipe[2];
299 if (fd == -1)
301 if (pipe( request_pipe ) == -1)
303 file_set_error();
304 return NULL;
306 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
308 close( request_pipe[0] );
309 close( request_pipe[1] );
310 return NULL;
312 close( request_pipe[1] );
313 fd = request_pipe[0];
316 if (process->is_terminating)
318 close( fd );
319 set_error( STATUS_PROCESS_IS_TERMINATING );
320 return NULL;
323 if (!(thread = alloc_object( &thread_ops )))
325 close( fd );
326 return NULL;
329 init_thread_structure( thread );
331 thread->process = (struct process *)grab_object( process );
332 thread->desktop = 0;
333 thread->affinity = process->affinity;
334 if (!current) current = thread;
336 list_add_tail( &thread_list, &thread->entry );
338 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
339 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
340 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
341 process->token ))
343 close( fd );
344 release_object( thread );
345 return NULL;
347 if (!(thread->id = alloc_ptid( thread )))
349 close( fd );
350 release_object( thread );
351 return NULL;
353 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
355 release_object( thread );
356 return NULL;
359 if (process->desktop)
361 if (!(desktop = get_desktop_obj( process, process->desktop, 0 ))) clear_error(); /* ignore errors */
362 else
364 set_thread_default_desktop( thread, desktop, process->desktop );
365 release_object( desktop );
369 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
370 add_process_thread( thread->process, thread );
371 return thread;
374 /* handle a client event */
375 static void thread_poll_event( struct fd *fd, int event )
377 struct thread *thread = get_fd_user( fd );
378 assert( thread->obj.ops == &thread_ops );
380 grab_object( thread );
381 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
382 else if (event & POLLIN) read_request( thread );
383 else if (event & POLLOUT) write_reply( thread );
384 release_object( thread );
387 static struct list *thread_get_kernel_obj_list( struct object *obj )
389 struct thread *thread = (struct thread *)obj;
390 return &thread->kernel_object;
393 /* cleanup everything that is no longer needed by a dead thread */
394 /* used by destroy_thread and kill_thread */
395 static void cleanup_thread( struct thread *thread )
397 int i;
399 if (thread->context)
401 thread->context->status = STATUS_ACCESS_DENIED;
402 wake_up( &thread->context->obj, 0 );
403 release_object( thread->context );
404 thread->context = NULL;
406 clear_apc_queue( &thread->system_apc );
407 clear_apc_queue( &thread->user_apc );
408 free( thread->req_data );
409 free( thread->reply_data );
410 if (thread->request_fd) release_object( thread->request_fd );
411 if (thread->reply_fd) release_object( thread->reply_fd );
412 if (thread->wait_fd) release_object( thread->wait_fd );
413 cleanup_clipboard_thread(thread);
414 destroy_thread_windows( thread );
415 free_msg_queue( thread );
416 release_thread_desktop( thread, 1 );
417 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
419 if (thread->inflight[i].client != -1)
421 close( thread->inflight[i].server );
422 thread->inflight[i].client = thread->inflight[i].server = -1;
425 free( thread->desc );
426 thread->req_data = NULL;
427 thread->reply_data = NULL;
428 thread->request_fd = NULL;
429 thread->reply_fd = NULL;
430 thread->wait_fd = NULL;
431 thread->desktop = 0;
432 thread->desc = NULL;
433 thread->desc_len = 0;
436 /* destroy a thread when its refcount is 0 */
437 static void destroy_thread( struct object *obj )
439 struct thread *thread = (struct thread *)obj;
440 assert( obj->ops == &thread_ops );
442 list_remove( &thread->entry );
443 cleanup_thread( thread );
444 release_object( thread->process );
445 if (thread->id) free_ptid( thread->id );
446 if (thread->token) release_object( thread->token );
449 /* dump a thread on stdout for debugging purposes */
450 static void dump_thread( struct object *obj, int verbose )
452 struct thread *thread = (struct thread *)obj;
453 assert( obj->ops == &thread_ops );
455 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
456 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
459 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
461 struct thread *mythread = (struct thread *)obj;
462 return (mythread->state == TERMINATED);
465 static unsigned int thread_map_access( struct object *obj, unsigned int access )
467 access = default_map_access( obj, access );
468 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
469 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
470 return access;
473 static void dump_thread_apc( struct object *obj, int verbose )
475 struct thread_apc *apc = (struct thread_apc *)obj;
476 assert( obj->ops == &thread_apc_ops );
478 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
481 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
483 struct thread_apc *apc = (struct thread_apc *)obj;
484 return apc->executed;
487 static void thread_apc_destroy( struct object *obj )
489 struct thread_apc *apc = (struct thread_apc *)obj;
491 if (apc->caller) release_object( apc->caller );
492 if (apc->owner)
494 if (apc->result.type == APC_ASYNC_IO)
495 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
496 else if (apc->call.type == APC_ASYNC_IO)
497 async_set_result( apc->owner, apc->call.async_io.status, 0 );
498 release_object( apc->owner );
502 /* queue an async procedure call */
503 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
505 struct thread_apc *apc;
507 if ((apc = alloc_object( &thread_apc_ops )))
509 apc->call = *call_data;
510 apc->caller = NULL;
511 apc->owner = owner;
512 apc->executed = 0;
513 apc->result.type = APC_NONE;
514 if (owner) grab_object( owner );
516 return apc;
519 /* get a thread pointer from a thread id (and increment the refcount) */
520 struct thread *get_thread_from_id( thread_id_t id )
522 struct object *obj = get_ptid_entry( id );
524 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
525 set_error( STATUS_INVALID_CID );
526 return NULL;
529 /* get a thread from a handle (and increment the refcount) */
530 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
532 return (struct thread *)get_handle_obj( current->process, handle,
533 access, &thread_ops );
536 /* find a thread from a Unix tid */
537 struct thread *get_thread_from_tid( int tid )
539 struct thread *thread;
541 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
543 if (thread->unix_tid == tid) return thread;
545 return NULL;
548 /* find a thread from a Unix pid */
549 struct thread *get_thread_from_pid( int pid )
551 struct thread *thread;
553 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
555 if (thread->unix_pid == pid) return thread;
557 return NULL;
560 int set_thread_affinity( struct thread *thread, affinity_t affinity )
562 int ret = 0;
563 #ifdef HAVE_SCHED_SETAFFINITY
564 if (thread->unix_tid != -1)
566 cpu_set_t set;
567 int i;
568 affinity_t mask;
570 CPU_ZERO( &set );
571 for (i = 0, mask = 1; mask; i++, mask <<= 1)
572 if (affinity & mask) CPU_SET( i, &set );
574 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
576 #endif
577 if (!ret) thread->affinity = affinity;
578 return ret;
581 affinity_t get_thread_affinity( struct thread *thread )
583 affinity_t mask = 0;
584 #ifdef HAVE_SCHED_SETAFFINITY
585 if (thread->unix_tid != -1)
587 cpu_set_t set;
588 unsigned int i;
590 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
591 for (i = 0; i < 8 * sizeof(mask); i++)
592 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
594 #endif
595 if (!mask) mask = ~(affinity_t)0;
596 return mask;
599 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
600 #define THREAD_PRIORITY_REALTIME_LOWEST -7
602 /* set all information about a thread */
603 static void set_thread_info( struct thread *thread,
604 const struct set_thread_info_request *req )
606 if (req->mask & SET_THREAD_INFO_PRIORITY)
608 int max = THREAD_PRIORITY_HIGHEST;
609 int min = THREAD_PRIORITY_LOWEST;
610 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
612 max = THREAD_PRIORITY_REALTIME_HIGHEST;
613 min = THREAD_PRIORITY_REALTIME_LOWEST;
615 if ((req->priority >= min && req->priority <= max) ||
616 req->priority == THREAD_PRIORITY_IDLE ||
617 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
618 thread->priority = req->priority;
619 else
620 set_error( STATUS_INVALID_PARAMETER );
622 if (req->mask & SET_THREAD_INFO_AFFINITY)
624 if ((req->affinity & thread->process->affinity) != req->affinity)
625 set_error( STATUS_INVALID_PARAMETER );
626 else if (thread->state == TERMINATED)
627 set_error( STATUS_THREAD_IS_TERMINATING );
628 else if (set_thread_affinity( thread, req->affinity ))
629 file_set_error();
631 if (req->mask & SET_THREAD_INFO_TOKEN)
632 security_set_thread_token( thread, req->token );
633 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
634 thread->entry_point = req->entry_point;
635 if (req->mask & SET_THREAD_INFO_DBG_HIDDEN)
636 thread->dbg_hidden = 1;
637 if (req->mask & SET_THREAD_INFO_DESCRIPTION)
639 WCHAR *desc;
640 data_size_t desc_len = get_req_data_size();
642 if (desc_len)
644 if ((desc = mem_alloc( desc_len )))
646 memcpy( desc, get_req_data(), desc_len );
647 free( thread->desc );
648 thread->desc = desc;
649 thread->desc_len = desc_len;
652 else
654 free( thread->desc );
655 thread->desc = NULL;
656 thread->desc_len = 0;
661 /* stop a thread (at the Unix level) */
662 void stop_thread( struct thread *thread )
664 if (thread->context) return; /* already suspended, no need for a signal */
665 if (!(thread->context = create_thread_context( thread ))) return;
666 /* can't stop a thread while initialisation is in progress */
667 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
670 /* suspend a thread */
671 int suspend_thread( struct thread *thread )
673 int old_count = thread->suspend;
674 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
676 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
678 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
679 return old_count;
682 /* resume a thread */
683 int resume_thread( struct thread *thread )
685 int old_count = thread->suspend;
686 if (thread->suspend > 0)
688 if (!(--thread->suspend)) resume_delayed_debug_events( thread );
689 if (!(thread->suspend + thread->process->suspend)) wake_thread( thread );
691 return old_count;
694 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
695 int add_queue( struct object *obj, struct wait_queue_entry *entry )
697 grab_object( obj );
698 entry->obj = obj;
699 list_add_tail( &obj->wait_queue, &entry->entry );
700 return 1;
703 /* remove a thread from an object wait queue */
704 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
706 list_remove( &entry->entry );
707 release_object( obj );
710 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
712 return entry->wait->thread;
715 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
717 return entry->wait->select;
720 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
722 return entry->wait->key;
725 void make_wait_abandoned( struct wait_queue_entry *entry )
727 entry->wait->abandoned = 1;
730 /* finish waiting */
731 static unsigned int end_wait( struct thread *thread, unsigned int status )
733 struct thread_wait *wait = thread->wait;
734 struct wait_queue_entry *entry;
735 int i;
737 assert( wait );
738 thread->wait = wait->next;
740 if (status < wait->count) /* wait satisfied, tell it to the objects */
742 if (wait->select == SELECT_WAIT_ALL)
744 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
745 entry->obj->ops->satisfied( entry->obj, entry );
747 else
749 entry = wait->queues + status;
750 entry->obj->ops->satisfied( entry->obj, entry );
752 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
754 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
755 entry->obj->ops->remove_queue( entry->obj, entry );
756 if (wait->user) remove_timeout_user( wait->user );
757 free( wait );
758 return status;
761 /* build the thread wait structure */
762 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
763 int flags, abstime_t when )
765 struct thread_wait *wait;
766 struct wait_queue_entry *entry;
767 unsigned int i;
769 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
770 wait->next = current->wait;
771 wait->thread = current;
772 wait->count = count;
773 wait->flags = flags;
774 wait->select = select_op->op;
775 wait->cookie = 0;
776 wait->user = NULL;
777 wait->when = when;
778 wait->abandoned = 0;
779 current->wait = wait;
781 for (i = 0, entry = wait->queues; i < count; i++, entry++)
783 struct object *obj = objects[i];
784 entry->wait = wait;
785 if (!obj->ops->add_queue( obj, entry ))
787 wait->count = i;
788 end_wait( current, get_error() );
789 return 0;
792 return 1;
795 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
796 int flags, abstime_t when )
798 struct object *objects[MAXIMUM_WAIT_OBJECTS];
799 unsigned int i;
800 int ret = 0;
802 assert( count <= MAXIMUM_WAIT_OBJECTS );
804 for (i = 0; i < count; i++)
805 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
806 break;
808 if (i == count) ret = wait_on( select_op, count, objects, flags, when );
810 while (i > 0) release_object( objects[--i] );
811 return ret;
814 /* check if the thread waiting condition is satisfied */
815 static int check_wait( struct thread *thread )
817 int i;
818 struct thread_wait *wait = thread->wait;
819 struct wait_queue_entry *entry;
821 assert( wait );
823 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
824 return STATUS_KERNEL_APC;
826 /* Suspended threads may not acquire locks, but they can run system APCs */
827 if (thread->process->suspend + thread->suspend > 0) return -1;
829 if (wait->select == SELECT_WAIT_ALL)
831 int not_ok = 0;
832 /* Note: we must check them all anyway, as some objects may
833 * want to do something when signaled, even if others are not */
834 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
835 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
836 if (!not_ok) return STATUS_WAIT_0;
838 else
840 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
841 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
844 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
845 if (wait->when >= 0 && wait->when <= current_time) return STATUS_TIMEOUT;
846 if (wait->when < 0 && -wait->when <= monotonic_time) return STATUS_TIMEOUT;
847 return -1;
850 /* send the wakeup signal to a thread */
851 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
853 struct wake_up_reply reply;
854 int ret;
856 /* check if we're waking current suspend wait */
857 if (thread->context && thread->suspend_cookie == cookie
858 && signaled != STATUS_KERNEL_APC && signaled != STATUS_USER_APC)
860 if (!thread->context->regs.flags)
862 release_object( thread->context );
863 thread->context = NULL;
865 else signaled = STATUS_KERNEL_APC; /* signal a fake APC so that client calls select to get a new context */
868 memset( &reply, 0, sizeof(reply) );
869 reply.cookie = cookie;
870 reply.signaled = signaled;
871 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
872 return 0;
873 if (ret >= 0)
874 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
875 else if (errno == EPIPE)
876 kill_thread( thread, 0 ); /* normal death */
877 else
878 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
879 return -1;
882 /* attempt to wake up a thread */
883 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
884 int wake_thread( struct thread *thread )
886 int signaled, count;
887 client_ptr_t cookie;
889 for (count = 0; thread->wait; count++)
891 if ((signaled = check_wait( thread )) == -1) break;
893 cookie = thread->wait->cookie;
894 signaled = end_wait( thread, signaled );
895 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
896 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
898 if (!count) count = -1;
899 break;
902 return count;
905 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
906 int wake_thread_queue_entry( struct wait_queue_entry *entry )
908 struct thread_wait *wait = entry->wait;
909 struct thread *thread = wait->thread;
910 int signaled;
911 client_ptr_t cookie;
913 if (thread->wait != wait) return 0; /* not the current wait */
914 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
916 assert( wait->select != SELECT_WAIT_ALL );
918 cookie = wait->cookie;
919 signaled = end_wait( thread, entry - wait->queues );
920 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
922 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
923 wake_thread( thread ); /* check other waits too */
925 return 1;
928 /* thread wait timeout */
929 static void thread_timeout( void *ptr )
931 struct thread_wait *wait = ptr;
932 struct thread *thread = wait->thread;
933 client_ptr_t cookie = wait->cookie;
935 wait->user = NULL;
936 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
937 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
939 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
940 end_wait( thread, STATUS_TIMEOUT );
942 assert( cookie );
943 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
944 /* check if other objects have become signaled in the meantime */
945 wake_thread( thread );
948 /* try signaling an event flag, a semaphore or a mutex */
949 static int signal_object( obj_handle_t handle )
951 struct object *obj;
952 int ret = 0;
954 obj = get_handle_obj( current->process, handle, 0, NULL );
955 if (obj)
957 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
958 release_object( obj );
960 return ret;
963 /* select on a list of handles */
964 static void select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
965 int flags, abstime_t when )
967 int ret;
968 unsigned int count;
969 struct object *object;
971 switch (select_op->op)
973 case SELECT_NONE:
974 if (!wait_on( select_op, 0, NULL, flags, when )) return;
975 break;
977 case SELECT_WAIT:
978 case SELECT_WAIT_ALL:
979 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
980 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
982 set_error( STATUS_INVALID_PARAMETER );
983 return;
985 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, when ))
986 return;
987 break;
989 case SELECT_SIGNAL_AND_WAIT:
990 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, when ))
991 return;
992 if (select_op->signal_and_wait.signal)
994 if (!signal_object( select_op->signal_and_wait.signal ))
996 end_wait( current, get_error() );
997 return;
999 /* check if we woke ourselves up */
1000 if (!current->wait) return;
1002 break;
1004 case SELECT_KEYED_EVENT_WAIT:
1005 case SELECT_KEYED_EVENT_RELEASE:
1006 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
1007 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
1008 if (!object) return;
1009 ret = wait_on( select_op, 1, &object, flags, when );
1010 release_object( object );
1011 if (!ret) return;
1012 current->wait->key = select_op->keyed_event.key;
1013 break;
1015 default:
1016 set_error( STATUS_INVALID_PARAMETER );
1017 return;
1020 if ((ret = check_wait( current )) != -1)
1022 /* condition is already satisfied */
1023 set_error( end_wait( current, ret ));
1024 return;
1027 /* now we need to wait */
1028 if (current->wait->when != TIMEOUT_INFINITE)
1030 if (!(current->wait->user = add_timeout_user( abstime_to_timeout(current->wait->when),
1031 thread_timeout, current->wait )))
1033 end_wait( current, get_error() );
1034 return;
1037 current->wait->cookie = cookie;
1038 set_error( STATUS_PENDING );
1039 return;
1042 /* attempt to wake threads sleeping on the object wait queue */
1043 void wake_up( struct object *obj, int max )
1045 struct list *ptr;
1046 int ret;
1048 LIST_FOR_EACH( ptr, &obj->wait_queue )
1050 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
1051 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
1052 if (ret > 0 && max && !--max) break;
1053 /* restart at the head of the list since a wake up can change the object wait queue */
1054 ptr = &obj->wait_queue;
1058 /* return the apc queue to use for a given apc type */
1059 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
1061 switch(type)
1063 case APC_NONE:
1064 return NULL;
1065 case APC_USER:
1066 case APC_TIMER:
1067 return &thread->user_apc;
1068 default:
1069 return &thread->system_apc;
1073 /* check if thread is currently waiting for a (system) apc */
1074 static inline int is_in_apc_wait( struct thread *thread )
1076 return (thread->process->suspend || thread->suspend ||
1077 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
1080 /* queue an existing APC to a given thread */
1081 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
1083 struct list *queue;
1085 if (thread && thread->state == TERMINATED && process)
1086 thread = NULL;
1088 if (!thread) /* find a suitable thread inside the process */
1090 struct thread *candidate;
1092 /* first try to find a waiting thread */
1093 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1095 if (candidate->state == TERMINATED) continue;
1096 if (is_in_apc_wait( candidate ))
1098 thread = candidate;
1099 break;
1102 if (!thread)
1104 /* then use the first one that accepts a signal */
1105 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1107 if (send_thread_signal( candidate, SIGUSR1 ))
1109 thread = candidate;
1110 break;
1114 if (!thread) return 0; /* nothing found */
1115 if (!(queue = get_apc_queue( thread, apc->call.type ))) return 1;
1117 else
1119 if (thread->state == TERMINATED) return 0;
1120 if (!(queue = get_apc_queue( thread, apc->call.type ))) return 1;
1121 /* send signal for system APCs if needed */
1122 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1124 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1126 /* cancel a possible previous APC with the same owner */
1127 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1130 grab_object( apc );
1131 list_add_tail( queue, &apc->entry );
1132 if (!list_prev( queue, &apc->entry )) /* first one */
1133 wake_thread( thread );
1135 return 1;
1138 /* queue an async procedure call */
1139 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1141 struct thread_apc *apc;
1142 int ret = 0;
1144 if ((apc = create_apc( owner, call_data )))
1146 ret = queue_apc( process, thread, apc );
1147 release_object( apc );
1149 return ret;
1152 /* cancel the async procedure call owned by a specific object */
1153 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1155 struct thread_apc *apc;
1156 struct list *queue = get_apc_queue( thread, type );
1158 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1160 if (apc->owner != owner) continue;
1161 list_remove( &apc->entry );
1162 apc->executed = 1;
1163 wake_up( &apc->obj, 0 );
1164 release_object( apc );
1165 return;
1169 /* remove the head apc from the queue; the returned object must be released by the caller */
1170 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system )
1172 struct thread_apc *apc = NULL;
1173 struct list *ptr = list_head( system ? &thread->system_apc : &thread->user_apc );
1175 if (ptr)
1177 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1178 list_remove( ptr );
1180 return apc;
1183 /* clear an APC queue, cancelling all the APCs on it */
1184 static void clear_apc_queue( struct list *queue )
1186 struct list *ptr;
1188 while ((ptr = list_head( queue )))
1190 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1191 list_remove( &apc->entry );
1192 apc->executed = 1;
1193 wake_up( &apc->obj, 0 );
1194 release_object( apc );
1198 /* add an fd to the inflight list */
1199 /* return list index, or -1 on error */
1200 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1202 int i;
1204 if (server == -1) return -1;
1205 if (client == -1)
1207 close( server );
1208 return -1;
1211 /* first check if we already have an entry for this fd */
1212 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1213 if (thread->inflight[i].client == client)
1215 close( thread->inflight[i].server );
1216 thread->inflight[i].server = server;
1217 return i;
1220 /* now find a free spot to store it */
1221 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1222 if (thread->inflight[i].client == -1)
1224 thread->inflight[i].client = client;
1225 thread->inflight[i].server = server;
1226 return i;
1229 close( server );
1230 return -1;
1233 /* get an inflight fd and purge it from the list */
1234 /* the fd must be closed when no longer used */
1235 int thread_get_inflight_fd( struct thread *thread, int client )
1237 int i, ret;
1239 if (client == -1) return -1;
1243 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1245 if (thread->inflight[i].client == client)
1247 ret = thread->inflight[i].server;
1248 thread->inflight[i].server = thread->inflight[i].client = -1;
1249 return ret;
1252 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1253 return -1;
1256 /* kill a thread on the spot */
1257 void kill_thread( struct thread *thread, int violent_death )
1259 if (thread->state == TERMINATED) return; /* already killed */
1260 thread->state = TERMINATED;
1261 thread->exit_time = current_time;
1262 if (current == thread) current = NULL;
1263 if (debug_level)
1264 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1265 thread->id, thread->exit_code );
1266 if (thread->wait)
1268 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1269 send_thread_wakeup( thread, 0, thread->exit_code );
1270 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1271 violent_death = 0;
1273 kill_console_processes( thread, 0 );
1274 abandon_mutexes( thread );
1275 wake_up( &thread->obj, 0 );
1276 if (violent_death) send_thread_signal( thread, SIGQUIT );
1277 cleanup_thread( thread );
1278 remove_process_thread( thread->process, thread );
1279 release_object( thread );
1282 /* copy parts of a context structure */
1283 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1285 assert( to->machine == from->machine );
1286 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1287 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1288 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1289 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1290 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1291 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1292 if (flags & SERVER_CTX_YMM_REGISTERS) to->ymm = from->ymm;
1295 /* return the context flags that correspond to system regs */
1296 /* (system regs are the ones we can't access on the client side) */
1297 static unsigned int get_context_system_regs( unsigned short machine )
1299 switch (machine)
1301 case IMAGE_FILE_MACHINE_I386: return SERVER_CTX_DEBUG_REGISTERS;
1302 case IMAGE_FILE_MACHINE_AMD64: return SERVER_CTX_DEBUG_REGISTERS;
1303 case IMAGE_FILE_MACHINE_ARMNT: return SERVER_CTX_DEBUG_REGISTERS;
1304 case IMAGE_FILE_MACHINE_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1306 return 0;
1309 /* gets the current impersonation token */
1310 struct token *thread_get_impersonation_token( struct thread *thread )
1312 if (thread->token)
1313 return thread->token;
1314 else
1315 return thread->process->token;
1318 /* create a new thread */
1319 DECL_HANDLER(new_thread)
1321 struct thread *thread;
1322 struct process *process;
1323 struct unicode_str name;
1324 const struct security_descriptor *sd;
1325 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1326 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1328 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1330 if (request_fd != -1) close( request_fd );
1331 return;
1334 if (process != current->process)
1336 if (request_fd != -1) /* can't create a request fd in a different process */
1338 close( request_fd );
1339 set_error( STATUS_INVALID_PARAMETER );
1340 goto done;
1342 if (process->running_threads) /* only the initial thread can be created in another process */
1344 set_error( STATUS_ACCESS_DENIED );
1345 goto done;
1348 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1350 if (request_fd != -1) close( request_fd );
1351 set_error( STATUS_INVALID_HANDLE );
1352 goto done;
1355 if ((thread = create_thread( request_fd, process, sd )))
1357 thread->system_regs = current->system_regs;
1358 if (req->suspend) thread->suspend++;
1359 reply->tid = get_thread_id( thread );
1360 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1361 req->access, objattr->attributes )))
1363 /* thread object will be released when the thread gets killed */
1364 goto done;
1366 kill_thread( thread, 1 );
1368 done:
1369 release_object( process );
1372 static int init_thread( struct thread *thread, int reply_fd, int wait_fd )
1374 if ((reply_fd = thread_get_inflight_fd( thread, reply_fd )) == -1)
1376 set_error( STATUS_TOO_MANY_OPENED_FILES );
1377 return 0;
1379 if ((wait_fd = thread_get_inflight_fd( thread, wait_fd )) == -1)
1381 set_error( STATUS_TOO_MANY_OPENED_FILES );
1382 goto error;
1385 if (thread->reply_fd) /* already initialised */
1387 set_error( STATUS_INVALID_PARAMETER );
1388 goto error;
1391 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1393 thread->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &thread->obj, 0 );
1394 thread->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &thread->obj, 0 );
1395 return thread->reply_fd && thread->wait_fd;
1397 error:
1398 if (reply_fd != -1) close( reply_fd );
1399 if (wait_fd != -1) close( wait_fd );
1400 return 0;
1403 /* initialize the first thread of a new process */
1404 DECL_HANDLER(init_first_thread)
1406 struct process *process = current->process;
1408 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1410 if (!is_valid_address(req->teb) || !is_valid_address(req->peb))
1412 set_error( STATUS_INVALID_PARAMETER );
1413 return;
1416 current->unix_pid = process->unix_pid = req->unix_pid;
1417 current->unix_tid = req->unix_tid;
1418 current->teb = req->teb;
1419 process->peb = req->peb;
1420 process->ldt_copy = req->ldt_copy;
1422 if (!process->parent_id)
1423 process->affinity = current->affinity = get_thread_affinity( current );
1424 else
1425 set_thread_affinity( current, current->affinity );
1427 debug_level = max( debug_level, req->debug_level );
1429 reply->pid = get_process_id( process );
1430 reply->tid = get_thread_id( current );
1431 reply->info_size = get_process_startup_info_size( process );
1432 reply->server_start = server_start_time;
1433 set_reply_data( supported_machines,
1434 min( supported_machines_count * sizeof(unsigned short), get_reply_max_size() ));
1437 /* initialize a new thread */
1438 DECL_HANDLER(init_thread)
1440 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1442 if (!is_valid_address(req->teb))
1444 set_error( STATUS_INVALID_PARAMETER );
1445 return;
1448 current->unix_pid = current->process->unix_pid;
1449 current->unix_tid = req->unix_tid;
1450 current->teb = req->teb;
1451 current->entry_point = req->entry;
1453 init_thread_context( current );
1454 generate_debug_event( current, DbgCreateThreadStateChange, &req->entry );
1455 set_thread_affinity( current, current->affinity );
1457 reply->pid = get_process_id( current->process );
1458 reply->tid = get_thread_id( current );
1459 reply->suspend = (current->suspend || current->process->suspend || current->context != NULL);
1462 /* terminate a thread */
1463 DECL_HANDLER(terminate_thread)
1465 struct thread *thread;
1467 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1469 thread->exit_code = req->exit_code;
1470 if (thread != current) kill_thread( thread, 1 );
1471 else reply->self = 1;
1472 release_object( thread );
1476 /* open a handle to a thread */
1477 DECL_HANDLER(open_thread)
1479 struct thread *thread = get_thread_from_id( req->tid );
1481 reply->handle = 0;
1482 if (thread)
1484 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1485 release_object( thread );
1489 /* fetch information about a thread */
1490 DECL_HANDLER(get_thread_info)
1492 struct thread *thread;
1493 unsigned int access = req->access & (THREAD_QUERY_INFORMATION | THREAD_QUERY_LIMITED_INFORMATION);
1495 if (!access) access = THREAD_QUERY_LIMITED_INFORMATION;
1496 thread = get_thread_from_handle( req->handle, access );
1497 if (thread)
1499 reply->pid = get_process_id( thread->process );
1500 reply->tid = get_thread_id( thread );
1501 reply->teb = thread->teb;
1502 reply->entry_point = thread->entry_point;
1503 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1504 reply->priority = thread->priority;
1505 reply->affinity = thread->affinity;
1506 reply->last = thread->process->running_threads == 1;
1507 reply->suspend_count = thread->suspend;
1508 reply->dbg_hidden = thread->dbg_hidden;
1509 reply->desc_len = thread->desc_len;
1511 if (thread->desc && get_reply_max_size())
1513 if (thread->desc_len <= get_reply_max_size())
1514 set_reply_data( thread->desc, thread->desc_len );
1515 else
1516 set_error( STATUS_BUFFER_TOO_SMALL );
1519 release_object( thread );
1523 /* fetch information about thread times */
1524 DECL_HANDLER(get_thread_times)
1526 struct thread *thread;
1528 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION )))
1530 reply->creation_time = thread->creation_time;
1531 reply->exit_time = thread->exit_time;
1532 reply->unix_pid = thread->unix_pid;
1533 reply->unix_tid = thread->unix_tid;
1535 release_object( thread );
1539 /* set information about a thread */
1540 DECL_HANDLER(set_thread_info)
1542 struct thread *thread;
1544 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1546 set_thread_info( thread, req );
1547 release_object( thread );
1551 /* suspend a thread */
1552 DECL_HANDLER(suspend_thread)
1554 struct thread *thread;
1556 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1558 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1559 else reply->count = suspend_thread( thread );
1560 release_object( thread );
1564 /* resume a thread */
1565 DECL_HANDLER(resume_thread)
1567 struct thread *thread;
1569 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1571 reply->count = resume_thread( thread );
1572 release_object( thread );
1576 /* select on a handle list */
1577 DECL_HANDLER(select)
1579 select_op_t select_op;
1580 data_size_t op_size;
1581 struct thread_apc *apc;
1582 const apc_result_t *result = get_req_data();
1584 if (get_req_data_size() < sizeof(*result) ||
1585 get_req_data_size() - sizeof(*result) < req->size ||
1586 req->size & 3)
1588 set_error( STATUS_INVALID_PARAMETER );
1589 return;
1592 if (get_req_data_size() - sizeof(*result) - req->size == sizeof(context_t))
1594 const context_t *context = (const context_t *)((const char *)(result + 1) + req->size);
1595 if ((current->context && current->context->status != STATUS_PENDING) ||
1596 context->machine != current->process->machine)
1598 set_error( STATUS_INVALID_PARAMETER );
1599 return;
1602 if (!current->context && !(current->context = create_thread_context( current ))) return;
1603 copy_context( &current->context->regs, context,
1604 context->flags & ~(current->context->regs.flags | get_context_system_regs(current->process->machine)) );
1605 current->context->status = STATUS_SUCCESS;
1606 current->suspend_cookie = req->cookie;
1607 wake_up( &current->context->obj, 0 );
1610 if (!req->cookie)
1612 set_error( STATUS_INVALID_PARAMETER );
1613 return;
1616 op_size = min( req->size, sizeof(select_op) );
1617 memset( &select_op, 0, sizeof(select_op) );
1618 memcpy( &select_op, result + 1, op_size );
1620 /* first store results of previous apc */
1621 if (req->prev_apc)
1623 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1624 0, &thread_apc_ops ))) return;
1625 apc->result = *result;
1626 apc->executed = 1;
1627 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1629 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1630 apc->caller->process, 0, 0, DUPLICATE_SAME_ACCESS );
1631 close_handle( current->process, apc->result.create_thread.handle );
1632 apc->result.create_thread.handle = handle;
1633 clear_error(); /* ignore errors from the above calls */
1635 wake_up( &apc->obj, 0 );
1636 close_handle( current->process, req->prev_apc );
1637 release_object( apc );
1640 select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1642 if (get_error() == STATUS_USER_APC)
1644 apc = thread_dequeue_apc( current, 0 );
1645 reply->call = apc->call;
1646 release_object( apc );
1648 else if (get_error() == STATUS_KERNEL_APC)
1650 apc = thread_dequeue_apc( current, 1 );
1651 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1652 reply->call = apc->call;
1653 else
1655 apc->executed = 1;
1656 wake_up( &apc->obj, 0 );
1658 release_object( apc );
1660 else if (get_error() != STATUS_PENDING && get_reply_max_size() == sizeof(context_t) &&
1661 current->context && current->suspend_cookie == req->cookie)
1663 if (current->context->regs.flags)
1665 unsigned int system_flags = get_context_system_regs(current->process->machine) &
1666 current->context->regs.flags;
1667 if (system_flags) set_thread_context( current, &current->context->regs, system_flags );
1668 set_reply_data( &current->context->regs, sizeof(context_t) );
1670 release_object( current->context );
1671 current->context = NULL;
1675 /* queue an APC for a thread or process */
1676 DECL_HANDLER(queue_apc)
1678 struct thread *thread = NULL;
1679 struct process *process = NULL;
1680 struct thread_apc *apc;
1682 if (!(apc = create_apc( NULL, &req->call ))) return;
1684 switch (apc->call.type)
1686 case APC_NONE:
1687 case APC_USER:
1688 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1689 break;
1690 case APC_VIRTUAL_ALLOC:
1691 case APC_VIRTUAL_FREE:
1692 case APC_VIRTUAL_PROTECT:
1693 case APC_VIRTUAL_FLUSH:
1694 case APC_VIRTUAL_LOCK:
1695 case APC_VIRTUAL_UNLOCK:
1696 case APC_UNMAP_VIEW:
1697 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1698 break;
1699 case APC_VIRTUAL_QUERY:
1700 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1701 break;
1702 case APC_MAP_VIEW:
1703 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1704 if (process && process != current->process)
1706 /* duplicate the handle into the target process */
1707 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1708 process, 0, 0, DUPLICATE_SAME_ACCESS );
1709 if (handle) apc->call.map_view.handle = handle;
1710 else
1712 release_object( process );
1713 process = NULL;
1716 break;
1717 case APC_CREATE_THREAD:
1718 case APC_BREAK_PROCESS:
1719 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1720 break;
1721 case APC_DUP_HANDLE:
1722 process = get_process_from_handle( req->handle, PROCESS_DUP_HANDLE );
1723 if (process && process != current->process)
1725 /* duplicate the destination process handle into the target process */
1726 obj_handle_t handle = duplicate_handle( current->process, apc->call.dup_handle.dst_process,
1727 process, 0, 0, DUPLICATE_SAME_ACCESS );
1728 if (handle) apc->call.dup_handle.dst_process = handle;
1729 else
1731 release_object( process );
1732 process = NULL;
1735 break;
1736 default:
1737 set_error( STATUS_INVALID_PARAMETER );
1738 break;
1741 if (thread)
1743 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_UNSUCCESSFUL );
1744 release_object( thread );
1746 else if (process)
1748 reply->self = (process == current->process);
1749 if (!reply->self)
1751 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1752 if (handle)
1754 if (queue_apc( process, NULL, apc ))
1756 apc->caller = (struct thread *)grab_object( current );
1757 reply->handle = handle;
1759 else
1761 close_handle( current->process, handle );
1762 set_error( STATUS_PROCESS_IS_TERMINATING );
1766 release_object( process );
1769 release_object( apc );
1772 /* Get the result of an APC call */
1773 DECL_HANDLER(get_apc_result)
1775 struct thread_apc *apc;
1777 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1778 0, &thread_apc_ops ))) return;
1780 if (apc->executed) reply->result = apc->result;
1781 else set_error( STATUS_PENDING );
1783 /* close the handle directly to avoid an extra round-trip */
1784 close_handle( current->process, req->handle );
1785 release_object( apc );
1788 /* retrieve the current context of a thread */
1789 DECL_HANDLER(get_thread_context)
1791 struct context *thread_context = NULL;
1792 unsigned int system_flags;
1793 struct thread *thread;
1794 context_t *context;
1796 if (get_reply_max_size() < sizeof(context_t))
1798 set_error( STATUS_INVALID_PARAMETER );
1799 return;
1802 if ((thread_context = (struct context *)get_handle_obj( current->process, req->handle, 0, &context_ops )))
1804 close_handle( current->process, req->handle ); /* avoid extra server call */
1805 system_flags = get_context_system_regs( thread_context->regs.machine );
1807 else if ((thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT )))
1809 clear_error();
1810 system_flags = get_context_system_regs( thread->process->machine );
1811 if (thread->state == RUNNING)
1813 reply->self = (thread == current);
1814 if (thread != current) stop_thread( thread );
1815 if (thread->context)
1817 /* make sure that system regs are valid in thread context */
1818 if (thread->unix_tid != -1 && (req->flags & system_flags & ~thread->context->regs.flags))
1819 get_thread_context( thread, &thread->context->regs, req->flags & system_flags );
1820 if (!get_error()) thread_context = (struct context *)grab_object( thread->context );
1822 else if (!get_error() && (context = set_reply_data_size( sizeof(context_t) )))
1824 assert( reply->self );
1825 memset( context, 0, sizeof(context_t) );
1826 context->machine = thread->process->machine;
1827 if (req->flags & system_flags)
1829 get_thread_context( thread, context, req->flags & system_flags );
1830 context->flags |= req->flags & system_flags;
1834 else set_error( STATUS_UNSUCCESSFUL );
1835 release_object( thread );
1837 if (get_error() || !thread_context) return;
1839 set_error( thread_context->status );
1840 if (!thread_context->status && (context = set_reply_data_size( sizeof(context_t) )))
1842 memset( context, 0, sizeof(context_t) );
1843 context->machine = thread_context->regs.machine;
1844 copy_context( context, &thread_context->regs, req->flags );
1845 context->flags = req->flags;
1847 else if (thread_context->status == STATUS_PENDING)
1849 reply->handle = alloc_handle( current->process, thread_context, SYNCHRONIZE, 0 );
1852 release_object( thread_context );
1855 /* set the current context of a thread */
1856 DECL_HANDLER(set_thread_context)
1858 struct thread *thread;
1859 const context_t *context = get_req_data();
1861 if (get_req_data_size() < sizeof(context_t))
1863 set_error( STATUS_INVALID_PARAMETER );
1864 return;
1866 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1867 reply->self = (thread == current);
1869 if (thread->state == TERMINATED) set_error( STATUS_UNSUCCESSFUL );
1870 else if (context->machine == thread->process->machine)
1872 unsigned int system_flags = get_context_system_regs( context->machine ) & context->flags;
1874 if (thread != current) stop_thread( thread );
1875 else if (system_flags) set_thread_context( thread, context, system_flags );
1876 if (thread->context && !get_error())
1878 copy_context( &thread->context->regs, context, context->flags );
1879 thread->context->regs.flags |= context->flags;
1882 else if (context->machine == IMAGE_FILE_MACHINE_AMD64 &&
1883 thread->process->machine == IMAGE_FILE_MACHINE_I386)
1885 /* convert the WoW64 context */
1886 unsigned int system_flags = get_context_system_regs( context->machine ) & context->flags;
1887 if (system_flags)
1889 set_thread_context( thread, context, system_flags );
1890 if (thread->context && !get_error())
1892 thread->context->regs.debug.i386_regs.dr0 = context->debug.x86_64_regs.dr0;
1893 thread->context->regs.debug.i386_regs.dr1 = context->debug.x86_64_regs.dr1;
1894 thread->context->regs.debug.i386_regs.dr2 = context->debug.x86_64_regs.dr2;
1895 thread->context->regs.debug.i386_regs.dr3 = context->debug.x86_64_regs.dr3;
1896 thread->context->regs.debug.i386_regs.dr6 = context->debug.x86_64_regs.dr6;
1897 thread->context->regs.debug.i386_regs.dr7 = context->debug.x86_64_regs.dr7;
1901 else set_error( STATUS_INVALID_PARAMETER );
1903 release_object( thread );
1906 /* fetch a selector entry for a thread */
1907 DECL_HANDLER(get_selector_entry)
1909 struct thread *thread;
1910 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1912 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1913 release_object( thread );
1917 /* Iterate thread list for process. Use global thread list to also
1918 * return terminated but not yet destroyed threads. */
1919 DECL_HANDLER(get_next_thread)
1921 struct thread *thread;
1922 struct process *process;
1923 struct list *ptr;
1925 if (req->flags > 1)
1927 set_error( STATUS_INVALID_PARAMETER );
1928 return;
1931 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1932 return;
1934 if (!req->last)
1936 ptr = req->flags ? list_tail( &thread_list ) : list_head( &thread_list );
1938 else if ((thread = get_thread_from_handle( req->last, 0 )))
1940 ptr = req->flags ? list_prev( &thread_list, &thread->entry )
1941 : list_next( &thread_list, &thread->entry );
1942 release_object( thread );
1944 else
1946 release_object( process );
1947 return;
1950 while (ptr)
1952 thread = LIST_ENTRY( ptr, struct thread, entry );
1953 if (thread->process == process)
1955 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1956 release_object( process );
1957 return;
1959 ptr = req->flags ? list_prev( &thread_list, &thread->entry )
1960 : list_next( &thread_list, &thread->entry );
1962 set_error( STATUS_NO_MORE_ENTRIES );
1963 release_object( process );