widl: Always check the runtimeclass interfaces presence.
[wine/zf.git] / server / thread.c
blobe4abe4fab945b3ff7572cd323c4b44b6feb55339
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 #ifdef __i386__
57 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
58 #elif defined(__x86_64__)
59 static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
60 #elif defined(__powerpc__)
61 static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
62 #elif defined(__arm__)
63 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM);
64 #elif defined(__aarch64__)
65 static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM64) | CPU_FLAG(CPU_ARM);
66 #else
67 #error Unsupported CPU
68 #endif
70 /* thread queues */
72 struct thread_wait
74 struct thread_wait *next; /* next wait structure for this thread */
75 struct thread *thread; /* owner thread */
76 int count; /* count of objects */
77 int flags;
78 int abandoned;
79 enum select_op select;
80 client_ptr_t key; /* wait key for keyed events */
81 client_ptr_t cookie; /* magic cookie to return to client */
82 abstime_t when;
83 struct timeout_user *user;
84 struct wait_queue_entry queues[1];
87 /* asynchronous procedure calls */
89 struct thread_apc
91 struct object obj; /* object header */
92 struct list entry; /* queue linked list */
93 struct thread *caller; /* thread that queued this apc */
94 struct object *owner; /* object that queued this apc */
95 int executed; /* has it been executed by the client? */
96 apc_call_t call; /* call arguments */
97 apc_result_t result; /* call results once executed */
100 static void dump_thread_apc( struct object *obj, int verbose );
101 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
102 static void thread_apc_destroy( struct object *obj );
103 static void clear_apc_queue( struct list *queue );
105 static const struct object_ops thread_apc_ops =
107 sizeof(struct thread_apc), /* size */
108 &no_type, /* type */
109 dump_thread_apc, /* dump */
110 add_queue, /* add_queue */
111 remove_queue, /* remove_queue */
112 thread_apc_signaled, /* signaled */
113 no_satisfied, /* satisfied */
114 no_signal, /* signal */
115 no_get_fd, /* get_fd */
116 default_map_access, /* map_access */
117 default_get_sd, /* get_sd */
118 default_set_sd, /* set_sd */
119 no_get_full_name, /* get_full_name */
120 no_lookup_name, /* lookup_name */
121 no_link_name, /* link_name */
122 NULL, /* unlink_name */
123 no_open_file, /* open_file */
124 no_kernel_obj_list, /* get_kernel_obj_list */
125 no_close_handle, /* close_handle */
126 thread_apc_destroy /* destroy */
130 /* thread CPU context */
132 struct context
134 struct object obj; /* object header */
135 unsigned int status; /* status of the context */
136 context_t regs; /* context data */
139 static void dump_context( struct object *obj, int verbose );
140 static int context_signaled( struct object *obj, struct wait_queue_entry *entry );
142 static const struct object_ops context_ops =
144 sizeof(struct context), /* size */
145 &no_type, /* type */
146 dump_context, /* dump */
147 add_queue, /* add_queue */
148 remove_queue, /* remove_queue */
149 context_signaled, /* signaled */
150 no_satisfied, /* satisfied */
151 no_signal, /* signal */
152 no_get_fd, /* get_fd */
153 default_map_access, /* map_access */
154 default_get_sd, /* get_sd */
155 default_set_sd, /* set_sd */
156 no_get_full_name, /* get_full_name */
157 no_lookup_name, /* lookup_name */
158 no_link_name, /* link_name */
159 NULL, /* unlink_name */
160 no_open_file, /* open_file */
161 no_kernel_obj_list, /* get_kernel_obj_list */
162 no_close_handle, /* close_handle */
163 no_destroy /* destroy */
167 /* thread operations */
169 static const WCHAR thread_name[] = {'T','h','r','e','a','d'};
171 struct type_descr thread_type =
173 { thread_name, sizeof(thread_name) }, /* name */
174 THREAD_ALL_ACCESS, /* valid_access */
175 { /* mapping */
176 STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT,
177 STANDARD_RIGHTS_WRITE | THREAD_SET_LIMITED_INFORMATION | THREAD_SET_INFORMATION
178 | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_TERMINATE | 0x04,
179 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_RESUME | THREAD_QUERY_LIMITED_INFORMATION,
180 THREAD_ALL_ACCESS
184 static void dump_thread( struct object *obj, int verbose );
185 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
186 static unsigned int thread_map_access( struct object *obj, unsigned int access );
187 static void thread_poll_event( struct fd *fd, int event );
188 static struct list *thread_get_kernel_obj_list( struct object *obj );
189 static void destroy_thread( struct object *obj );
191 static const struct object_ops thread_ops =
193 sizeof(struct thread), /* size */
194 &thread_type, /* type */
195 dump_thread, /* dump */
196 add_queue, /* add_queue */
197 remove_queue, /* remove_queue */
198 thread_signaled, /* signaled */
199 no_satisfied, /* satisfied */
200 no_signal, /* signal */
201 no_get_fd, /* get_fd */
202 thread_map_access, /* map_access */
203 default_get_sd, /* get_sd */
204 default_set_sd, /* set_sd */
205 no_get_full_name, /* get_full_name */
206 no_lookup_name, /* lookup_name */
207 no_link_name, /* link_name */
208 NULL, /* unlink_name */
209 no_open_file, /* open_file */
210 thread_get_kernel_obj_list, /* get_kernel_obj_list */
211 no_close_handle, /* close_handle */
212 destroy_thread /* destroy */
215 static const struct fd_ops thread_fd_ops =
217 NULL, /* get_poll_events */
218 thread_poll_event, /* poll_event */
219 NULL, /* flush */
220 NULL, /* get_fd_type */
221 NULL, /* ioctl */
222 NULL, /* queue_async */
223 NULL /* reselect_async */
226 static struct list thread_list = LIST_INIT(thread_list);
228 /* initialize the structure for a newly allocated thread */
229 static inline void init_thread_structure( struct thread *thread )
231 int i;
233 thread->unix_pid = -1; /* not known yet */
234 thread->unix_tid = -1; /* not known yet */
235 thread->context = NULL;
236 thread->teb = 0;
237 thread->entry_point = 0;
238 thread->system_regs = 0;
239 thread->queue = NULL;
240 thread->wait = NULL;
241 thread->error = 0;
242 thread->req_data = NULL;
243 thread->req_toread = 0;
244 thread->reply_data = NULL;
245 thread->reply_towrite = 0;
246 thread->request_fd = NULL;
247 thread->reply_fd = NULL;
248 thread->wait_fd = NULL;
249 thread->state = RUNNING;
250 thread->exit_code = 0;
251 thread->priority = 0;
252 thread->suspend = 0;
253 thread->dbg_hidden = 0;
254 thread->desktop_users = 0;
255 thread->token = NULL;
256 thread->desc = NULL;
257 thread->desc_len = 0;
259 thread->creation_time = current_time;
260 thread->exit_time = 0;
262 list_init( &thread->mutex_list );
263 list_init( &thread->system_apc );
264 list_init( &thread->user_apc );
265 list_init( &thread->kernel_object );
267 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
268 thread->inflight[i].server = thread->inflight[i].client = -1;
271 /* check if address looks valid for a client-side data structure (TEB etc.) */
272 static inline int is_valid_address( client_ptr_t addr )
274 return addr && !(addr % sizeof(int));
278 /* dump a context on stdout for debugging purposes */
279 static void dump_context( struct object *obj, int verbose )
281 struct context *context = (struct context *)obj;
282 assert( obj->ops == &context_ops );
284 fprintf( stderr, "context flags=%x\n", context->regs.flags );
288 static int context_signaled( struct object *obj, struct wait_queue_entry *entry )
290 struct context *context = (struct context *)obj;
291 return context->status != STATUS_PENDING;
295 static struct context *create_thread_context( struct thread *thread )
297 struct context *context;
298 if (!(context = alloc_object( &context_ops ))) return NULL;
299 context->status = STATUS_PENDING;
300 memset( &context->regs, 0, sizeof(context->regs) );
301 context->regs.cpu = thread->process->cpu;
302 return context;
306 /* create a new thread */
307 struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
309 struct thread *thread;
310 int request_pipe[2];
312 if (fd == -1)
314 if (pipe( request_pipe ) == -1)
316 file_set_error();
317 return NULL;
319 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
321 close( request_pipe[0] );
322 close( request_pipe[1] );
323 return NULL;
325 close( request_pipe[1] );
326 fd = request_pipe[0];
329 if (process->is_terminating)
331 close( fd );
332 set_error( STATUS_PROCESS_IS_TERMINATING );
333 return NULL;
336 if (!(thread = alloc_object( &thread_ops )))
338 close( fd );
339 return NULL;
342 init_thread_structure( thread );
344 thread->process = (struct process *)grab_object( process );
345 thread->desktop = process->desktop;
346 thread->affinity = process->affinity;
347 if (!current) current = thread;
349 list_add_tail( &thread_list, &thread->entry );
351 if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
352 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
353 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
354 process->token ))
356 close( fd );
357 release_object( thread );
358 return NULL;
360 if (!(thread->id = alloc_ptid( thread )))
362 close( fd );
363 release_object( thread );
364 return NULL;
366 if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
368 release_object( thread );
369 return NULL;
372 set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
373 add_process_thread( thread->process, thread );
374 return thread;
377 /* handle a client event */
378 static void thread_poll_event( struct fd *fd, int event )
380 struct thread *thread = get_fd_user( fd );
381 assert( thread->obj.ops == &thread_ops );
383 grab_object( thread );
384 if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
385 else if (event & POLLIN) read_request( thread );
386 else if (event & POLLOUT) write_reply( thread );
387 release_object( thread );
390 static struct list *thread_get_kernel_obj_list( struct object *obj )
392 struct thread *thread = (struct thread *)obj;
393 return &thread->kernel_object;
396 /* cleanup everything that is no longer needed by a dead thread */
397 /* used by destroy_thread and kill_thread */
398 static void cleanup_thread( struct thread *thread )
400 int i;
402 if (thread->context)
404 thread->context->status = STATUS_ACCESS_DENIED;
405 wake_up( &thread->context->obj, 0 );
406 release_object( thread->context );
407 thread->context = NULL;
409 clear_apc_queue( &thread->system_apc );
410 clear_apc_queue( &thread->user_apc );
411 free( thread->req_data );
412 free( thread->reply_data );
413 if (thread->request_fd) release_object( thread->request_fd );
414 if (thread->reply_fd) release_object( thread->reply_fd );
415 if (thread->wait_fd) release_object( thread->wait_fd );
416 cleanup_clipboard_thread(thread);
417 destroy_thread_windows( thread );
418 free_msg_queue( thread );
419 close_thread_desktop( thread );
420 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
422 if (thread->inflight[i].client != -1)
424 close( thread->inflight[i].server );
425 thread->inflight[i].client = thread->inflight[i].server = -1;
428 free( thread->desc );
429 thread->req_data = NULL;
430 thread->reply_data = NULL;
431 thread->request_fd = NULL;
432 thread->reply_fd = NULL;
433 thread->wait_fd = NULL;
434 thread->desktop = 0;
435 thread->desc = NULL;
436 thread->desc_len = 0;
439 /* destroy a thread when its refcount is 0 */
440 static void destroy_thread( struct object *obj )
442 struct thread *thread = (struct thread *)obj;
443 assert( obj->ops == &thread_ops );
445 list_remove( &thread->entry );
446 cleanup_thread( thread );
447 release_object( thread->process );
448 if (thread->id) free_ptid( thread->id );
449 if (thread->token) release_object( thread->token );
452 /* dump a thread on stdout for debugging purposes */
453 static void dump_thread( struct object *obj, int verbose )
455 struct thread *thread = (struct thread *)obj;
456 assert( obj->ops == &thread_ops );
458 fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
459 thread->id, thread->unix_pid, thread->unix_tid, thread->state );
462 static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
464 struct thread *mythread = (struct thread *)obj;
465 return (mythread->state == TERMINATED);
468 static unsigned int thread_map_access( struct object *obj, unsigned int access )
470 access = default_map_access( obj, access );
471 if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
472 if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
473 return access;
476 static void dump_thread_apc( struct object *obj, int verbose )
478 struct thread_apc *apc = (struct thread_apc *)obj;
479 assert( obj->ops == &thread_apc_ops );
481 fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
484 static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
486 struct thread_apc *apc = (struct thread_apc *)obj;
487 return apc->executed;
490 static void thread_apc_destroy( struct object *obj )
492 struct thread_apc *apc = (struct thread_apc *)obj;
494 if (apc->caller) release_object( apc->caller );
495 if (apc->owner)
497 if (apc->result.type == APC_ASYNC_IO)
498 async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
499 else if (apc->call.type == APC_ASYNC_IO)
500 async_set_result( apc->owner, apc->call.async_io.status, 0 );
501 release_object( apc->owner );
505 /* queue an async procedure call */
506 static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
508 struct thread_apc *apc;
510 if ((apc = alloc_object( &thread_apc_ops )))
512 apc->call = *call_data;
513 apc->caller = NULL;
514 apc->owner = owner;
515 apc->executed = 0;
516 apc->result.type = APC_NONE;
517 if (owner) grab_object( owner );
519 return apc;
522 /* get a thread pointer from a thread id (and increment the refcount) */
523 struct thread *get_thread_from_id( thread_id_t id )
525 struct object *obj = get_ptid_entry( id );
527 if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
528 set_error( STATUS_INVALID_CID );
529 return NULL;
532 /* get a thread from a handle (and increment the refcount) */
533 struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
535 return (struct thread *)get_handle_obj( current->process, handle,
536 access, &thread_ops );
539 /* find a thread from a Unix tid */
540 struct thread *get_thread_from_tid( int tid )
542 struct thread *thread;
544 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
546 if (thread->unix_tid == tid) return thread;
548 return NULL;
551 /* find a thread from a Unix pid */
552 struct thread *get_thread_from_pid( int pid )
554 struct thread *thread;
556 LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
558 if (thread->unix_pid == pid) return thread;
560 return NULL;
563 int set_thread_affinity( struct thread *thread, affinity_t affinity )
565 int ret = 0;
566 #ifdef HAVE_SCHED_SETAFFINITY
567 if (thread->unix_tid != -1)
569 cpu_set_t set;
570 int i;
571 affinity_t mask;
573 CPU_ZERO( &set );
574 for (i = 0, mask = 1; mask; i++, mask <<= 1)
575 if (affinity & mask) CPU_SET( i, &set );
577 ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
579 #endif
580 if (!ret) thread->affinity = affinity;
581 return ret;
584 affinity_t get_thread_affinity( struct thread *thread )
586 affinity_t mask = 0;
587 #ifdef HAVE_SCHED_SETAFFINITY
588 if (thread->unix_tid != -1)
590 cpu_set_t set;
591 unsigned int i;
593 if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
594 for (i = 0; i < 8 * sizeof(mask); i++)
595 if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
597 #endif
598 if (!mask) mask = ~(affinity_t)0;
599 return mask;
602 #define THREAD_PRIORITY_REALTIME_HIGHEST 6
603 #define THREAD_PRIORITY_REALTIME_LOWEST -7
605 /* set all information about a thread */
606 static void set_thread_info( struct thread *thread,
607 const struct set_thread_info_request *req )
609 if (req->mask & SET_THREAD_INFO_PRIORITY)
611 int max = THREAD_PRIORITY_HIGHEST;
612 int min = THREAD_PRIORITY_LOWEST;
613 if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
615 max = THREAD_PRIORITY_REALTIME_HIGHEST;
616 min = THREAD_PRIORITY_REALTIME_LOWEST;
618 if ((req->priority >= min && req->priority <= max) ||
619 req->priority == THREAD_PRIORITY_IDLE ||
620 req->priority == THREAD_PRIORITY_TIME_CRITICAL)
621 thread->priority = req->priority;
622 else
623 set_error( STATUS_INVALID_PARAMETER );
625 if (req->mask & SET_THREAD_INFO_AFFINITY)
627 if ((req->affinity & thread->process->affinity) != req->affinity)
628 set_error( STATUS_INVALID_PARAMETER );
629 else if (thread->state == TERMINATED)
630 set_error( STATUS_THREAD_IS_TERMINATING );
631 else if (set_thread_affinity( thread, req->affinity ))
632 file_set_error();
634 if (req->mask & SET_THREAD_INFO_TOKEN)
635 security_set_thread_token( thread, req->token );
636 if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
637 thread->entry_point = req->entry_point;
638 if (req->mask & SET_THREAD_INFO_DBG_HIDDEN)
639 thread->dbg_hidden = 1;
640 if (req->mask & SET_THREAD_INFO_DESCRIPTION)
642 WCHAR *desc;
643 data_size_t desc_len = get_req_data_size();
645 if (desc_len)
647 if ((desc = mem_alloc( desc_len )))
649 memcpy( desc, get_req_data(), desc_len );
650 free( thread->desc );
651 thread->desc = desc;
652 thread->desc_len = desc_len;
655 else
657 free( thread->desc );
658 thread->desc = NULL;
659 thread->desc_len = 0;
664 /* stop a thread (at the Unix level) */
665 void stop_thread( struct thread *thread )
667 if (thread->context) return; /* already suspended, no need for a signal */
668 if (!(thread->context = create_thread_context( thread ))) return;
669 /* can't stop a thread while initialisation is in progress */
670 if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
673 /* suspend a thread */
674 int suspend_thread( struct thread *thread )
676 int old_count = thread->suspend;
677 if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
679 if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
681 else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
682 return old_count;
685 /* resume a thread */
686 int resume_thread( struct thread *thread )
688 int old_count = thread->suspend;
689 if (thread->suspend > 0)
691 if (!(--thread->suspend)) resume_delayed_debug_events( thread );
692 if (!(thread->suspend + thread->process->suspend)) wake_thread( thread );
694 return old_count;
697 /* add a thread to an object wait queue; return 1 if OK, 0 on error */
698 int add_queue( struct object *obj, struct wait_queue_entry *entry )
700 grab_object( obj );
701 entry->obj = obj;
702 list_add_tail( &obj->wait_queue, &entry->entry );
703 return 1;
706 /* remove a thread from an object wait queue */
707 void remove_queue( struct object *obj, struct wait_queue_entry *entry )
709 list_remove( &entry->entry );
710 release_object( obj );
713 struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
715 return entry->wait->thread;
718 enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
720 return entry->wait->select;
723 client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
725 return entry->wait->key;
728 void make_wait_abandoned( struct wait_queue_entry *entry )
730 entry->wait->abandoned = 1;
733 /* finish waiting */
734 static unsigned int end_wait( struct thread *thread, unsigned int status )
736 struct thread_wait *wait = thread->wait;
737 struct wait_queue_entry *entry;
738 int i;
740 assert( wait );
741 thread->wait = wait->next;
743 if (status < wait->count) /* wait satisfied, tell it to the objects */
745 if (wait->select == SELECT_WAIT_ALL)
747 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
748 entry->obj->ops->satisfied( entry->obj, entry );
750 else
752 entry = wait->queues + status;
753 entry->obj->ops->satisfied( entry->obj, entry );
755 if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
757 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
758 entry->obj->ops->remove_queue( entry->obj, entry );
759 if (wait->user) remove_timeout_user( wait->user );
760 free( wait );
761 return status;
764 /* build the thread wait structure */
765 static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
766 int flags, abstime_t when )
768 struct thread_wait *wait;
769 struct wait_queue_entry *entry;
770 unsigned int i;
772 if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
773 wait->next = current->wait;
774 wait->thread = current;
775 wait->count = count;
776 wait->flags = flags;
777 wait->select = select_op->op;
778 wait->cookie = 0;
779 wait->user = NULL;
780 wait->when = when;
781 wait->abandoned = 0;
782 current->wait = wait;
784 for (i = 0, entry = wait->queues; i < count; i++, entry++)
786 struct object *obj = objects[i];
787 entry->wait = wait;
788 if (!obj->ops->add_queue( obj, entry ))
790 wait->count = i;
791 end_wait( current, get_error() );
792 return 0;
795 return 1;
798 static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
799 int flags, abstime_t when )
801 struct object *objects[MAXIMUM_WAIT_OBJECTS];
802 unsigned int i;
803 int ret = 0;
805 assert( count <= MAXIMUM_WAIT_OBJECTS );
807 for (i = 0; i < count; i++)
808 if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
809 break;
811 if (i == count) ret = wait_on( select_op, count, objects, flags, when );
813 while (i > 0) release_object( objects[--i] );
814 return ret;
817 /* check if the thread waiting condition is satisfied */
818 static int check_wait( struct thread *thread )
820 int i;
821 struct thread_wait *wait = thread->wait;
822 struct wait_queue_entry *entry;
824 assert( wait );
826 if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
827 return STATUS_KERNEL_APC;
829 /* Suspended threads may not acquire locks, but they can run system APCs */
830 if (thread->process->suspend + thread->suspend > 0) return -1;
832 if (wait->select == SELECT_WAIT_ALL)
834 int not_ok = 0;
835 /* Note: we must check them all anyway, as some objects may
836 * want to do something when signaled, even if others are not */
837 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
838 not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
839 if (!not_ok) return STATUS_WAIT_0;
841 else
843 for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
844 if (entry->obj->ops->signaled( entry->obj, entry )) return i;
847 if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
848 if (wait->when >= 0 && wait->when <= current_time) return STATUS_TIMEOUT;
849 if (wait->when < 0 && -wait->when <= monotonic_time) return STATUS_TIMEOUT;
850 return -1;
853 /* send the wakeup signal to a thread */
854 static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
856 struct wake_up_reply reply;
857 int ret;
859 /* check if we're waking current suspend wait */
860 if (thread->context && thread->suspend_cookie == cookie
861 && signaled != STATUS_KERNEL_APC && signaled != STATUS_USER_APC)
863 if (!thread->context->regs.flags)
865 release_object( thread->context );
866 thread->context = NULL;
868 else signaled = STATUS_KERNEL_APC; /* signal a fake APC so that client calls select to get a new context */
871 memset( &reply, 0, sizeof(reply) );
872 reply.cookie = cookie;
873 reply.signaled = signaled;
874 if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
875 return 0;
876 if (ret >= 0)
877 fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
878 else if (errno == EPIPE)
879 kill_thread( thread, 0 ); /* normal death */
880 else
881 fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
882 return -1;
885 /* attempt to wake up a thread */
886 /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
887 int wake_thread( struct thread *thread )
889 int signaled, count;
890 client_ptr_t cookie;
892 for (count = 0; thread->wait; count++)
894 if ((signaled = check_wait( thread )) == -1) break;
896 cookie = thread->wait->cookie;
897 signaled = end_wait( thread, signaled );
898 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
899 if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
901 if (!count) count = -1;
902 break;
905 return count;
908 /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
909 int wake_thread_queue_entry( struct wait_queue_entry *entry )
911 struct thread_wait *wait = entry->wait;
912 struct thread *thread = wait->thread;
913 int signaled;
914 client_ptr_t cookie;
916 if (thread->wait != wait) return 0; /* not the current wait */
917 if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
919 assert( wait->select != SELECT_WAIT_ALL );
921 cookie = wait->cookie;
922 signaled = end_wait( thread, entry - wait->queues );
923 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
925 if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
926 wake_thread( thread ); /* check other waits too */
928 return 1;
931 /* thread wait timeout */
932 static void thread_timeout( void *ptr )
934 struct thread_wait *wait = ptr;
935 struct thread *thread = wait->thread;
936 client_ptr_t cookie = wait->cookie;
938 wait->user = NULL;
939 if (thread->wait != wait) return; /* not the top-level wait, ignore it */
940 if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
942 if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
943 end_wait( thread, STATUS_TIMEOUT );
945 assert( cookie );
946 if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
947 /* check if other objects have become signaled in the meantime */
948 wake_thread( thread );
951 /* try signaling an event flag, a semaphore or a mutex */
952 static int signal_object( obj_handle_t handle )
954 struct object *obj;
955 int ret = 0;
957 obj = get_handle_obj( current->process, handle, 0, NULL );
958 if (obj)
960 ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
961 release_object( obj );
963 return ret;
966 /* select on a list of handles */
967 static void select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
968 int flags, abstime_t when )
970 int ret;
971 unsigned int count;
972 struct object *object;
974 switch (select_op->op)
976 case SELECT_NONE:
977 if (!wait_on( select_op, 0, NULL, flags, when )) return;
978 break;
980 case SELECT_WAIT:
981 case SELECT_WAIT_ALL:
982 count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
983 if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
985 set_error( STATUS_INVALID_PARAMETER );
986 return;
988 if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, when ))
989 return;
990 break;
992 case SELECT_SIGNAL_AND_WAIT:
993 if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, when ))
994 return;
995 if (select_op->signal_and_wait.signal)
997 if (!signal_object( select_op->signal_and_wait.signal ))
999 end_wait( current, get_error() );
1000 return;
1002 /* check if we woke ourselves up */
1003 if (!current->wait) return;
1005 break;
1007 case SELECT_KEYED_EVENT_WAIT:
1008 case SELECT_KEYED_EVENT_RELEASE:
1009 object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
1010 select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
1011 if (!object) return;
1012 ret = wait_on( select_op, 1, &object, flags, when );
1013 release_object( object );
1014 if (!ret) return;
1015 current->wait->key = select_op->keyed_event.key;
1016 break;
1018 default:
1019 set_error( STATUS_INVALID_PARAMETER );
1020 return;
1023 if ((ret = check_wait( current )) != -1)
1025 /* condition is already satisfied */
1026 set_error( end_wait( current, ret ));
1027 return;
1030 /* now we need to wait */
1031 if (current->wait->when != TIMEOUT_INFINITE)
1033 if (!(current->wait->user = add_timeout_user( abstime_to_timeout(current->wait->when),
1034 thread_timeout, current->wait )))
1036 end_wait( current, get_error() );
1037 return;
1040 current->wait->cookie = cookie;
1041 set_error( STATUS_PENDING );
1042 return;
1045 /* attempt to wake threads sleeping on the object wait queue */
1046 void wake_up( struct object *obj, int max )
1048 struct list *ptr;
1049 int ret;
1051 LIST_FOR_EACH( ptr, &obj->wait_queue )
1053 struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
1054 if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
1055 if (ret > 0 && max && !--max) break;
1056 /* restart at the head of the list since a wake up can change the object wait queue */
1057 ptr = &obj->wait_queue;
1061 /* return the apc queue to use for a given apc type */
1062 static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
1064 switch(type)
1066 case APC_NONE:
1067 case APC_USER:
1068 case APC_TIMER:
1069 return &thread->user_apc;
1070 default:
1071 return &thread->system_apc;
1075 /* check if thread is currently waiting for a (system) apc */
1076 static inline int is_in_apc_wait( struct thread *thread )
1078 return (thread->process->suspend || thread->suspend ||
1079 (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
1082 /* queue an existing APC to a given thread */
1083 static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
1085 struct list *queue;
1087 if (thread && thread->state == TERMINATED && process)
1088 thread = NULL;
1090 if (!thread) /* find a suitable thread inside the process */
1092 struct thread *candidate;
1094 /* first try to find a waiting thread */
1095 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1097 if (candidate->state == TERMINATED) continue;
1098 if (is_in_apc_wait( candidate ))
1100 thread = candidate;
1101 break;
1104 if (!thread)
1106 /* then use the first one that accepts a signal */
1107 LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
1109 if (send_thread_signal( candidate, SIGUSR1 ))
1111 thread = candidate;
1112 break;
1116 if (!thread) return 0; /* nothing found */
1117 queue = get_apc_queue( thread, apc->call.type );
1119 else
1121 if (thread->state == TERMINATED) return 0;
1122 queue = get_apc_queue( thread, apc->call.type );
1123 /* send signal for system APCs if needed */
1124 if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
1126 if (!send_thread_signal( thread, SIGUSR1 )) return 0;
1128 /* cancel a possible previous APC with the same owner */
1129 if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
1132 grab_object( apc );
1133 list_add_tail( queue, &apc->entry );
1134 if (!list_prev( queue, &apc->entry )) /* first one */
1135 wake_thread( thread );
1137 return 1;
1140 /* queue an async procedure call */
1141 int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
1143 struct thread_apc *apc;
1144 int ret = 0;
1146 if ((apc = create_apc( owner, call_data )))
1148 ret = queue_apc( process, thread, apc );
1149 release_object( apc );
1151 return ret;
1154 /* cancel the async procedure call owned by a specific object */
1155 void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
1157 struct thread_apc *apc;
1158 struct list *queue = get_apc_queue( thread, type );
1160 LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
1162 if (apc->owner != owner) continue;
1163 list_remove( &apc->entry );
1164 apc->executed = 1;
1165 wake_up( &apc->obj, 0 );
1166 release_object( apc );
1167 return;
1171 /* remove the head apc from the queue; the returned object must be released by the caller */
1172 static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system )
1174 struct thread_apc *apc = NULL;
1175 struct list *ptr = list_head( system ? &thread->system_apc : &thread->user_apc );
1177 if (ptr)
1179 apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1180 list_remove( ptr );
1182 return apc;
1185 /* clear an APC queue, cancelling all the APCs on it */
1186 static void clear_apc_queue( struct list *queue )
1188 struct list *ptr;
1190 while ((ptr = list_head( queue )))
1192 struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
1193 list_remove( &apc->entry );
1194 apc->executed = 1;
1195 wake_up( &apc->obj, 0 );
1196 release_object( apc );
1200 /* add an fd to the inflight list */
1201 /* return list index, or -1 on error */
1202 int thread_add_inflight_fd( struct thread *thread, int client, int server )
1204 int i;
1206 if (server == -1) return -1;
1207 if (client == -1)
1209 close( server );
1210 return -1;
1213 /* first check if we already have an entry for this fd */
1214 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1215 if (thread->inflight[i].client == client)
1217 close( thread->inflight[i].server );
1218 thread->inflight[i].server = server;
1219 return i;
1222 /* now find a free spot to store it */
1223 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1224 if (thread->inflight[i].client == -1)
1226 thread->inflight[i].client = client;
1227 thread->inflight[i].server = server;
1228 return i;
1231 close( server );
1232 return -1;
1235 /* get an inflight fd and purge it from the list */
1236 /* the fd must be closed when no longer used */
1237 int thread_get_inflight_fd( struct thread *thread, int client )
1239 int i, ret;
1241 if (client == -1) return -1;
1245 for (i = 0; i < MAX_INFLIGHT_FDS; i++)
1247 if (thread->inflight[i].client == client)
1249 ret = thread->inflight[i].server;
1250 thread->inflight[i].server = thread->inflight[i].client = -1;
1251 return ret;
1254 } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
1255 return -1;
1258 /* kill a thread on the spot */
1259 void kill_thread( struct thread *thread, int violent_death )
1261 if (thread->state == TERMINATED) return; /* already killed */
1262 thread->state = TERMINATED;
1263 thread->exit_time = current_time;
1264 if (current == thread) current = NULL;
1265 if (debug_level)
1266 fprintf( stderr,"%04x: *killed* exit_code=%d\n",
1267 thread->id, thread->exit_code );
1268 if (thread->wait)
1270 while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
1271 send_thread_wakeup( thread, 0, thread->exit_code );
1272 /* if it is waiting on the socket, we don't need to send a SIGQUIT */
1273 violent_death = 0;
1275 kill_console_processes( thread, 0 );
1276 abandon_mutexes( thread );
1277 wake_up( &thread->obj, 0 );
1278 if (violent_death) send_thread_signal( thread, SIGQUIT );
1279 cleanup_thread( thread );
1280 remove_process_thread( thread->process, thread );
1281 release_object( thread );
1284 /* copy parts of a context structure */
1285 static void copy_context( context_t *to, const context_t *from, unsigned int flags )
1287 assert( to->cpu == from->cpu );
1288 if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
1289 if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
1290 if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
1291 if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
1292 if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
1293 if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
1294 if (flags & SERVER_CTX_YMM_REGISTERS) to->ymm = from->ymm;
1297 /* return the context flags that correspond to system regs */
1298 /* (system regs are the ones we can't access on the client side) */
1299 static unsigned int get_context_system_regs( enum cpu_type cpu )
1301 switch (cpu)
1303 case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
1304 case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
1305 case CPU_POWERPC: return 0;
1306 case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
1307 case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
1309 return 0;
1312 /* gets the current impersonation token */
1313 struct token *thread_get_impersonation_token( struct thread *thread )
1315 if (thread->token)
1316 return thread->token;
1317 else
1318 return thread->process->token;
1321 /* check if a cpu type can be supported on this server */
1322 int is_cpu_supported( enum cpu_type cpu )
1324 unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
1326 if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
1327 if (!(supported_cpus & prefix_cpu_mask))
1328 set_error( STATUS_NOT_SUPPORTED );
1329 else if (supported_cpus & CPU_FLAG(cpu))
1330 set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
1331 else
1332 set_error( STATUS_INVALID_IMAGE_FORMAT );
1333 return 0;
1336 /* return the cpu mask for supported cpus */
1337 unsigned int get_supported_cpu_mask(void)
1339 return supported_cpus & get_prefix_cpu_mask();
1342 /* create a new thread */
1343 DECL_HANDLER(new_thread)
1345 struct thread *thread;
1346 struct process *process;
1347 struct unicode_str name;
1348 const struct security_descriptor *sd;
1349 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1350 int request_fd = thread_get_inflight_fd( current, req->request_fd );
1352 if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
1354 if (request_fd != -1) close( request_fd );
1355 return;
1358 if (process != current->process)
1360 if (request_fd != -1) /* can't create a request fd in a different process */
1362 close( request_fd );
1363 set_error( STATUS_INVALID_PARAMETER );
1364 goto done;
1366 if (process->running_threads) /* only the initial thread can be created in another process */
1368 set_error( STATUS_ACCESS_DENIED );
1369 goto done;
1372 else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
1374 if (request_fd != -1) close( request_fd );
1375 set_error( STATUS_INVALID_HANDLE );
1376 goto done;
1379 if ((thread = create_thread( request_fd, process, sd )))
1381 thread->system_regs = current->system_regs;
1382 if (req->suspend) thread->suspend++;
1383 reply->tid = get_thread_id( thread );
1384 if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
1385 req->access, objattr->attributes )))
1387 /* thread object will be released when the thread gets killed */
1388 goto done;
1390 kill_thread( thread, 1 );
1392 done:
1393 release_object( process );
1396 static int init_thread( struct thread *thread, int reply_fd, int wait_fd )
1398 if ((reply_fd = thread_get_inflight_fd( thread, reply_fd )) == -1)
1400 set_error( STATUS_TOO_MANY_OPENED_FILES );
1401 return 0;
1403 if ((wait_fd = thread_get_inflight_fd( thread, wait_fd )) == -1)
1405 set_error( STATUS_TOO_MANY_OPENED_FILES );
1406 goto error;
1409 if (thread->reply_fd) /* already initialised */
1411 set_error( STATUS_INVALID_PARAMETER );
1412 goto error;
1415 if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
1417 thread->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &thread->obj, 0 );
1418 thread->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &thread->obj, 0 );
1419 return thread->reply_fd && thread->wait_fd;
1421 error:
1422 if (reply_fd != -1) close( reply_fd );
1423 if (wait_fd != -1) close( wait_fd );
1424 return 0;
1427 /* initialize the first thread of a new process */
1428 DECL_HANDLER(init_first_thread)
1430 struct process *process = current->process;
1432 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1434 if (!is_valid_address(req->teb) || !is_valid_address(req->peb))
1436 set_error( STATUS_INVALID_PARAMETER );
1437 return;
1440 if (!is_cpu_supported( req->cpu )) return;
1442 current->unix_pid = process->unix_pid = req->unix_pid;
1443 current->unix_tid = req->unix_tid;
1444 current->teb = req->teb;
1445 process->peb = req->peb;
1446 process->ldt_copy = req->ldt_copy;
1447 process->cpu = req->cpu;
1449 if (!process->parent_id)
1450 process->affinity = current->affinity = get_thread_affinity( current );
1451 else
1452 set_thread_affinity( current, current->affinity );
1454 debug_level = max( debug_level, req->debug_level );
1456 reply->pid = get_process_id( process );
1457 reply->tid = get_thread_id( current );
1458 reply->info_size = get_process_startup_info_size( process );
1459 reply->server_start = server_start_time;
1460 reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
1463 /* initialize a new thread */
1464 DECL_HANDLER(init_thread)
1466 if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
1468 if (!is_valid_address(req->teb))
1470 set_error( STATUS_INVALID_PARAMETER );
1471 return;
1474 current->unix_pid = current->process->unix_pid;
1475 current->unix_tid = req->unix_tid;
1476 current->teb = req->teb;
1477 current->entry_point = req->entry;
1479 init_thread_context( current );
1480 generate_debug_event( current, DbgCreateThreadStateChange, &req->entry );
1481 set_thread_affinity( current, current->affinity );
1483 reply->pid = get_process_id( current->process );
1484 reply->tid = get_thread_id( current );
1485 reply->suspend = (current->suspend || current->process->suspend || current->context != NULL);
1488 /* terminate a thread */
1489 DECL_HANDLER(terminate_thread)
1491 struct thread *thread;
1493 if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
1495 thread->exit_code = req->exit_code;
1496 if (thread != current) kill_thread( thread, 1 );
1497 else reply->self = 1;
1498 release_object( thread );
1502 /* open a handle to a thread */
1503 DECL_HANDLER(open_thread)
1505 struct thread *thread = get_thread_from_id( req->tid );
1507 reply->handle = 0;
1508 if (thread)
1510 reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
1511 release_object( thread );
1515 /* fetch information about a thread */
1516 DECL_HANDLER(get_thread_info)
1518 struct thread *thread;
1519 unsigned int access = req->access & (THREAD_QUERY_INFORMATION | THREAD_QUERY_LIMITED_INFORMATION);
1521 if (!access) access = THREAD_QUERY_LIMITED_INFORMATION;
1522 thread = get_thread_from_handle( req->handle, access );
1523 if (thread)
1525 reply->pid = get_process_id( thread->process );
1526 reply->tid = get_thread_id( thread );
1527 reply->teb = thread->teb;
1528 reply->entry_point = thread->entry_point;
1529 reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
1530 reply->priority = thread->priority;
1531 reply->affinity = thread->affinity;
1532 reply->last = thread->process->running_threads == 1;
1533 reply->suspend_count = thread->suspend;
1534 reply->dbg_hidden = thread->dbg_hidden;
1535 reply->desc_len = thread->desc_len;
1537 if (thread->desc && get_reply_max_size())
1539 if (thread->desc_len <= get_reply_max_size())
1540 set_reply_data( thread->desc, thread->desc_len );
1541 else
1542 set_error( STATUS_BUFFER_TOO_SMALL );
1545 release_object( thread );
1549 /* fetch information about thread times */
1550 DECL_HANDLER(get_thread_times)
1552 struct thread *thread;
1554 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION )))
1556 reply->creation_time = thread->creation_time;
1557 reply->exit_time = thread->exit_time;
1558 reply->unix_pid = thread->unix_pid;
1559 reply->unix_tid = thread->unix_tid;
1561 release_object( thread );
1565 /* set information about a thread */
1566 DECL_HANDLER(set_thread_info)
1568 struct thread *thread;
1570 if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
1572 set_thread_info( thread, req );
1573 release_object( thread );
1577 /* suspend a thread */
1578 DECL_HANDLER(suspend_thread)
1580 struct thread *thread;
1582 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1584 if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
1585 else reply->count = suspend_thread( thread );
1586 release_object( thread );
1590 /* resume a thread */
1591 DECL_HANDLER(resume_thread)
1593 struct thread *thread;
1595 if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
1597 reply->count = resume_thread( thread );
1598 release_object( thread );
1602 /* select on a handle list */
1603 DECL_HANDLER(select)
1605 select_op_t select_op;
1606 data_size_t op_size;
1607 struct thread_apc *apc;
1608 const apc_result_t *result = get_req_data();
1610 if (get_req_data_size() < sizeof(*result) ||
1611 get_req_data_size() - sizeof(*result) < req->size ||
1612 req->size & 3)
1614 set_error( STATUS_INVALID_PARAMETER );
1615 return;
1618 if (get_req_data_size() - sizeof(*result) - req->size == sizeof(context_t))
1620 const context_t *context = (const context_t *)((const char *)(result + 1) + req->size);
1621 if ((current->context && current->context->status != STATUS_PENDING) || context->cpu != current->process->cpu)
1623 set_error( STATUS_INVALID_PARAMETER );
1624 return;
1627 if (!current->context && !(current->context = create_thread_context( current ))) return;
1628 copy_context( &current->context->regs, context,
1629 context->flags & ~(current->context->regs.flags | get_context_system_regs(current->process->cpu)) );
1630 current->context->status = STATUS_SUCCESS;
1631 current->suspend_cookie = req->cookie;
1632 wake_up( &current->context->obj, 0 );
1635 if (!req->cookie)
1637 set_error( STATUS_INVALID_PARAMETER );
1638 return;
1641 op_size = min( req->size, sizeof(select_op) );
1642 memset( &select_op, 0, sizeof(select_op) );
1643 memcpy( &select_op, result + 1, op_size );
1645 /* first store results of previous apc */
1646 if (req->prev_apc)
1648 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
1649 0, &thread_apc_ops ))) return;
1650 apc->result = *result;
1651 apc->executed = 1;
1652 if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
1654 obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
1655 apc->caller->process, 0, 0, DUPLICATE_SAME_ACCESS );
1656 close_handle( current->process, apc->result.create_thread.handle );
1657 apc->result.create_thread.handle = handle;
1658 clear_error(); /* ignore errors from the above calls */
1660 wake_up( &apc->obj, 0 );
1661 close_handle( current->process, req->prev_apc );
1662 release_object( apc );
1665 select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
1667 while (get_error() == STATUS_USER_APC)
1669 if (!(apc = thread_dequeue_apc( current, 0 )))
1670 break;
1671 /* Optimization: ignore APC_NONE calls, they are only used to
1672 * wake up a thread, but since we got here the thread woke up already.
1674 if (apc->call.type != APC_NONE &&
1675 (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1677 reply->call = apc->call;
1678 release_object( apc );
1679 break;
1681 apc->executed = 1;
1682 wake_up( &apc->obj, 0 );
1683 release_object( apc );
1686 if (get_error() == STATUS_KERNEL_APC)
1688 apc = thread_dequeue_apc( current, 1 );
1689 if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
1690 reply->call = apc->call;
1691 else
1693 apc->executed = 1;
1694 wake_up( &apc->obj, 0 );
1696 release_object( apc );
1698 else if (get_error() != STATUS_PENDING && get_reply_max_size() == sizeof(context_t) &&
1699 current->context && current->suspend_cookie == req->cookie)
1701 if (current->context->regs.flags)
1703 unsigned int system_flags = get_context_system_regs(current->process->cpu) &
1704 current->context->regs.flags;
1705 if (system_flags) set_thread_context( current, &current->context->regs, system_flags );
1706 set_reply_data( &current->context->regs, sizeof(context_t) );
1708 release_object( current->context );
1709 current->context = NULL;
1713 /* queue an APC for a thread or process */
1714 DECL_HANDLER(queue_apc)
1716 struct thread *thread = NULL;
1717 struct process *process = NULL;
1718 struct thread_apc *apc;
1720 if (!(apc = create_apc( NULL, &req->call ))) return;
1722 switch (apc->call.type)
1724 case APC_NONE:
1725 case APC_USER:
1726 thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
1727 break;
1728 case APC_VIRTUAL_ALLOC:
1729 case APC_VIRTUAL_FREE:
1730 case APC_VIRTUAL_PROTECT:
1731 case APC_VIRTUAL_FLUSH:
1732 case APC_VIRTUAL_LOCK:
1733 case APC_VIRTUAL_UNLOCK:
1734 case APC_UNMAP_VIEW:
1735 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1736 break;
1737 case APC_VIRTUAL_QUERY:
1738 process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
1739 break;
1740 case APC_MAP_VIEW:
1741 process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
1742 if (process && process != current->process)
1744 /* duplicate the handle into the target process */
1745 obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
1746 process, 0, 0, DUPLICATE_SAME_ACCESS );
1747 if (handle) apc->call.map_view.handle = handle;
1748 else
1750 release_object( process );
1751 process = NULL;
1754 break;
1755 case APC_CREATE_THREAD:
1756 case APC_BREAK_PROCESS:
1757 process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
1758 break;
1759 default:
1760 set_error( STATUS_INVALID_PARAMETER );
1761 break;
1764 if (thread)
1766 if (!queue_apc( NULL, thread, apc )) set_error( STATUS_UNSUCCESSFUL );
1767 release_object( thread );
1769 else if (process)
1771 reply->self = (process == current->process);
1772 if (!reply->self)
1774 obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
1775 if (handle)
1777 if (queue_apc( process, NULL, apc ))
1779 apc->caller = (struct thread *)grab_object( current );
1780 reply->handle = handle;
1782 else
1784 close_handle( current->process, handle );
1785 set_error( STATUS_PROCESS_IS_TERMINATING );
1789 release_object( process );
1792 release_object( apc );
1795 /* Get the result of an APC call */
1796 DECL_HANDLER(get_apc_result)
1798 struct thread_apc *apc;
1800 if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
1801 0, &thread_apc_ops ))) return;
1803 if (apc->executed) reply->result = apc->result;
1804 else set_error( STATUS_PENDING );
1806 /* close the handle directly to avoid an extra round-trip */
1807 close_handle( current->process, req->handle );
1808 release_object( apc );
1811 /* retrieve the current context of a thread */
1812 DECL_HANDLER(get_thread_context)
1814 struct context *thread_context = NULL;
1815 unsigned int system_flags;
1816 struct thread *thread;
1817 context_t *context;
1819 if (get_reply_max_size() < sizeof(context_t))
1821 set_error( STATUS_INVALID_PARAMETER );
1822 return;
1825 if ((thread_context = (struct context *)get_handle_obj( current->process, req->handle, 0, &context_ops )))
1827 close_handle( current->process, req->handle ); /* avoid extra server call */
1828 system_flags = get_context_system_regs( thread_context->regs.cpu );
1830 else if ((thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT )))
1832 clear_error();
1833 system_flags = get_context_system_regs( thread->process->cpu );
1834 if (thread->state == RUNNING)
1836 reply->self = (thread == current);
1837 if (thread != current) stop_thread( thread );
1838 if (thread->context)
1840 /* make sure that system regs are valid in thread context */
1841 if (thread->unix_tid != -1 && (req->flags & system_flags & ~thread->context->regs.flags))
1842 get_thread_context( thread, &thread->context->regs, req->flags & system_flags );
1843 if (!get_error()) thread_context = (struct context *)grab_object( thread->context );
1845 else if (!get_error() && (context = set_reply_data_size( sizeof(context_t) )))
1847 assert( reply->self );
1848 memset( context, 0, sizeof(context_t) );
1849 context->cpu = thread->process->cpu;
1850 if (req->flags & system_flags)
1852 get_thread_context( thread, context, req->flags & system_flags );
1853 context->flags |= req->flags & system_flags;
1857 else set_error( STATUS_UNSUCCESSFUL );
1858 release_object( thread );
1860 if (get_error() || !thread_context) return;
1862 set_error( thread_context->status );
1863 if (!thread_context->status && (context = set_reply_data_size( sizeof(context_t) )))
1865 memset( context, 0, sizeof(context_t) );
1866 context->cpu = thread_context->regs.cpu;
1867 copy_context( context, &thread_context->regs, req->flags );
1868 context->flags = req->flags;
1870 else if (thread_context->status == STATUS_PENDING)
1872 reply->handle = alloc_handle( current->process, thread_context, SYNCHRONIZE, 0 );
1875 release_object( thread_context );
1878 /* set the current context of a thread */
1879 DECL_HANDLER(set_thread_context)
1881 struct thread *thread;
1882 const context_t *context = get_req_data();
1884 if (get_req_data_size() < sizeof(context_t))
1886 set_error( STATUS_INVALID_PARAMETER );
1887 return;
1889 if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
1890 reply->self = (thread == current);
1892 if (thread->state == TERMINATED) set_error( STATUS_UNSUCCESSFUL );
1893 else if (context->cpu == thread->process->cpu)
1895 unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
1897 if (thread != current) stop_thread( thread );
1898 else if (system_flags) set_thread_context( thread, context, system_flags );
1899 if (thread->context && !get_error())
1901 copy_context( &thread->context->regs, context, context->flags );
1902 thread->context->regs.flags |= context->flags;
1905 else if (context->cpu == CPU_x86_64 && thread->process->cpu == CPU_x86)
1907 /* convert the WoW64 context */
1908 unsigned int system_flags = get_context_system_regs( context->cpu ) & context->flags;
1909 if (system_flags)
1911 set_thread_context( thread, context, system_flags );
1912 if (thread->context && !get_error())
1914 thread->context->regs.debug.i386_regs.dr0 = context->debug.x86_64_regs.dr0;
1915 thread->context->regs.debug.i386_regs.dr1 = context->debug.x86_64_regs.dr1;
1916 thread->context->regs.debug.i386_regs.dr2 = context->debug.x86_64_regs.dr2;
1917 thread->context->regs.debug.i386_regs.dr3 = context->debug.x86_64_regs.dr3;
1918 thread->context->regs.debug.i386_regs.dr6 = context->debug.x86_64_regs.dr6;
1919 thread->context->regs.debug.i386_regs.dr7 = context->debug.x86_64_regs.dr7;
1923 else set_error( STATUS_INVALID_PARAMETER );
1925 release_object( thread );
1928 /* fetch a selector entry for a thread */
1929 DECL_HANDLER(get_selector_entry)
1931 struct thread *thread;
1932 if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
1934 get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
1935 release_object( thread );